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"
53 #include "fold-const.h"
54 #include "stor-layout.h"
56 #include "tree-iterator.h"
58 #include "insn-config.h"
68 #include "diagnostic-core.h"
70 #include "langhooks.h"
72 #include "internal-fn.h"
78 #include "generic-match.h"
79 #include "optabs-query.h"
80 #include "gimple-fold.h"
82 #include "tree-ssa-operands.h"
83 #include "tree-into-ssa.h"
85 #ifndef LOAD_EXTEND_OP
86 #define LOAD_EXTEND_OP(M) UNKNOWN
89 /* Nonzero if we are folding constants inside an initializer; zero
91 int folding_initializer
= 0;
93 /* The following constants represent a bit based encoding of GCC's
94 comparison operators. This encoding simplifies transformations
95 on relational comparison operators, such as AND and OR. */
96 enum comparison_code
{
115 static bool negate_expr_p (tree
);
116 static tree
negate_expr (tree
);
117 static tree
split_tree (tree
, enum tree_code
, tree
*, tree
*, tree
*, int);
118 static tree
associate_trees (location_t
, tree
, tree
, enum tree_code
, tree
);
119 static enum comparison_code
comparison_to_compcode (enum tree_code
);
120 static enum tree_code
compcode_to_comparison (enum comparison_code
);
121 static int operand_equal_for_comparison_p (tree
, tree
, tree
);
122 static int twoval_comparison_p (tree
, tree
*, tree
*, int *);
123 static tree
eval_subst (location_t
, tree
, tree
, 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_div_compare (location_t
, enum tree_code
, tree
, tree
, tree
);
149 static bool reorder_operands_p (const_tree
, const_tree
);
150 static tree
fold_negate_const (tree
, tree
);
151 static tree
fold_not_const (const_tree
, tree
);
152 static tree
fold_relational_const (enum tree_code
, tree
, tree
, tree
);
153 static tree
fold_convert_const (enum tree_code
, tree
, tree
);
154 static tree
fold_view_convert_expr (tree
, tree
);
155 static bool vec_cst_ctor_to_array (tree
, tree
*);
158 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
159 Otherwise, return LOC. */
162 expr_location_or (tree t
, location_t loc
)
164 location_t tloc
= EXPR_LOCATION (t
);
165 return tloc
== UNKNOWN_LOCATION
? loc
: tloc
;
168 /* Similar to protected_set_expr_location, but never modify x in place,
169 if location can and needs to be set, unshare it. */
172 protected_set_expr_location_unshare (tree x
, location_t loc
)
174 if (CAN_HAVE_LOCATION_P (x
)
175 && EXPR_LOCATION (x
) != loc
176 && !(TREE_CODE (x
) == SAVE_EXPR
177 || TREE_CODE (x
) == TARGET_EXPR
178 || TREE_CODE (x
) == BIND_EXPR
))
181 SET_EXPR_LOCATION (x
, loc
);
186 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
187 division and returns the quotient. Otherwise returns
191 div_if_zero_remainder (const_tree arg1
, const_tree arg2
)
195 if (wi::multiple_of_p (wi::to_widest (arg1
), wi::to_widest (arg2
),
197 return wide_int_to_tree (TREE_TYPE (arg1
), quo
);
202 /* This is nonzero if we should defer warnings about undefined
203 overflow. This facility exists because these warnings are a
204 special case. The code to estimate loop iterations does not want
205 to issue any warnings, since it works with expressions which do not
206 occur in user code. Various bits of cleanup code call fold(), but
207 only use the result if it has certain characteristics (e.g., is a
208 constant); that code only wants to issue a warning if the result is
211 static int fold_deferring_overflow_warnings
;
213 /* If a warning about undefined overflow is deferred, this is the
214 warning. Note that this may cause us to turn two warnings into
215 one, but that is fine since it is sufficient to only give one
216 warning per expression. */
218 static const char* fold_deferred_overflow_warning
;
220 /* If a warning about undefined overflow is deferred, this is the
221 level at which the warning should be emitted. */
223 static enum warn_strict_overflow_code fold_deferred_overflow_code
;
225 /* Start deferring overflow warnings. We could use a stack here to
226 permit nested calls, but at present it is not necessary. */
229 fold_defer_overflow_warnings (void)
231 ++fold_deferring_overflow_warnings
;
234 /* Stop deferring overflow warnings. If there is a pending warning,
235 and ISSUE is true, then issue the warning if appropriate. STMT is
236 the statement with which the warning should be associated (used for
237 location information); STMT may be NULL. CODE is the level of the
238 warning--a warn_strict_overflow_code value. This function will use
239 the smaller of CODE and the deferred code when deciding whether to
240 issue the warning. CODE may be zero to mean to always use the
244 fold_undefer_overflow_warnings (bool issue
, const gimple
*stmt
, int code
)
249 gcc_assert (fold_deferring_overflow_warnings
> 0);
250 --fold_deferring_overflow_warnings
;
251 if (fold_deferring_overflow_warnings
> 0)
253 if (fold_deferred_overflow_warning
!= NULL
255 && code
< (int) fold_deferred_overflow_code
)
256 fold_deferred_overflow_code
= (enum warn_strict_overflow_code
) code
;
260 warnmsg
= fold_deferred_overflow_warning
;
261 fold_deferred_overflow_warning
= NULL
;
263 if (!issue
|| warnmsg
== NULL
)
266 if (gimple_no_warning_p (stmt
))
269 /* Use the smallest code level when deciding to issue the
271 if (code
== 0 || code
> (int) fold_deferred_overflow_code
)
272 code
= fold_deferred_overflow_code
;
274 if (!issue_strict_overflow_warning (code
))
278 locus
= input_location
;
280 locus
= gimple_location (stmt
);
281 warning_at (locus
, OPT_Wstrict_overflow
, "%s", warnmsg
);
284 /* Stop deferring overflow warnings, ignoring any deferred
288 fold_undefer_and_ignore_overflow_warnings (void)
290 fold_undefer_overflow_warnings (false, NULL
, 0);
293 /* Whether we are deferring overflow warnings. */
296 fold_deferring_overflow_warnings_p (void)
298 return fold_deferring_overflow_warnings
> 0;
301 /* This is called when we fold something based on the fact that signed
302 overflow is undefined. */
305 fold_overflow_warning (const char* gmsgid
, enum warn_strict_overflow_code wc
)
307 if (fold_deferring_overflow_warnings
> 0)
309 if (fold_deferred_overflow_warning
== NULL
310 || wc
< fold_deferred_overflow_code
)
312 fold_deferred_overflow_warning
= gmsgid
;
313 fold_deferred_overflow_code
= wc
;
316 else if (issue_strict_overflow_warning (wc
))
317 warning (OPT_Wstrict_overflow
, gmsgid
);
320 /* Return true if the built-in mathematical function specified by CODE
321 is odd, i.e. -f(x) == f(-x). */
324 negate_mathfn_p (enum built_in_function code
)
328 CASE_FLT_FN (BUILT_IN_ASIN
):
329 CASE_FLT_FN (BUILT_IN_ASINH
):
330 CASE_FLT_FN (BUILT_IN_ATAN
):
331 CASE_FLT_FN (BUILT_IN_ATANH
):
332 CASE_FLT_FN (BUILT_IN_CASIN
):
333 CASE_FLT_FN (BUILT_IN_CASINH
):
334 CASE_FLT_FN (BUILT_IN_CATAN
):
335 CASE_FLT_FN (BUILT_IN_CATANH
):
336 CASE_FLT_FN (BUILT_IN_CBRT
):
337 CASE_FLT_FN (BUILT_IN_CPROJ
):
338 CASE_FLT_FN (BUILT_IN_CSIN
):
339 CASE_FLT_FN (BUILT_IN_CSINH
):
340 CASE_FLT_FN (BUILT_IN_CTAN
):
341 CASE_FLT_FN (BUILT_IN_CTANH
):
342 CASE_FLT_FN (BUILT_IN_ERF
):
343 CASE_FLT_FN (BUILT_IN_LLROUND
):
344 CASE_FLT_FN (BUILT_IN_LROUND
):
345 CASE_FLT_FN (BUILT_IN_ROUND
):
346 CASE_FLT_FN (BUILT_IN_SIN
):
347 CASE_FLT_FN (BUILT_IN_SINH
):
348 CASE_FLT_FN (BUILT_IN_TAN
):
349 CASE_FLT_FN (BUILT_IN_TANH
):
350 CASE_FLT_FN (BUILT_IN_TRUNC
):
353 CASE_FLT_FN (BUILT_IN_LLRINT
):
354 CASE_FLT_FN (BUILT_IN_LRINT
):
355 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
356 CASE_FLT_FN (BUILT_IN_RINT
):
357 return !flag_rounding_math
;
365 /* Check whether we may negate an integer constant T without causing
369 may_negate_without_overflow_p (const_tree t
)
373 gcc_assert (TREE_CODE (t
) == INTEGER_CST
);
375 type
= TREE_TYPE (t
);
376 if (TYPE_UNSIGNED (type
))
379 return !wi::only_sign_bit_p (t
);
382 /* Determine whether an expression T can be cheaply negated using
383 the function negate_expr without introducing undefined overflow. */
386 negate_expr_p (tree t
)
393 type
= TREE_TYPE (t
);
396 switch (TREE_CODE (t
))
399 if (INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_WRAPS (type
))
402 /* Check that -CST will not overflow type. */
403 return may_negate_without_overflow_p (t
);
405 return (INTEGRAL_TYPE_P (type
)
406 && TYPE_OVERFLOW_WRAPS (type
));
412 return !TYPE_OVERFLOW_SANITIZED (type
);
415 /* We want to canonicalize to positive real constants. Pretend
416 that only negative ones can be easily negated. */
417 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
420 return negate_expr_p (TREE_REALPART (t
))
421 && negate_expr_p (TREE_IMAGPART (t
));
425 if (FLOAT_TYPE_P (TREE_TYPE (type
)) || TYPE_OVERFLOW_WRAPS (type
))
428 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
430 for (i
= 0; i
< count
; i
++)
431 if (!negate_expr_p (VECTOR_CST_ELT (t
, i
)))
438 return negate_expr_p (TREE_OPERAND (t
, 0))
439 && negate_expr_p (TREE_OPERAND (t
, 1));
442 return negate_expr_p (TREE_OPERAND (t
, 0));
445 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
446 || HONOR_SIGNED_ZEROS (element_mode (type
))
447 || (INTEGRAL_TYPE_P (type
)
448 && ! TYPE_OVERFLOW_WRAPS (type
)))
450 /* -(A + B) -> (-B) - A. */
451 if (negate_expr_p (TREE_OPERAND (t
, 1))
452 && reorder_operands_p (TREE_OPERAND (t
, 0),
453 TREE_OPERAND (t
, 1)))
455 /* -(A + B) -> (-A) - B. */
456 return negate_expr_p (TREE_OPERAND (t
, 0));
459 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
460 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
461 && !HONOR_SIGNED_ZEROS (element_mode (type
))
462 && (! INTEGRAL_TYPE_P (type
)
463 || TYPE_OVERFLOW_WRAPS (type
))
464 && reorder_operands_p (TREE_OPERAND (t
, 0),
465 TREE_OPERAND (t
, 1));
468 if (TYPE_UNSIGNED (type
))
470 /* INT_MIN/n * n doesn't overflow while negating one operand it does
471 if n is a power of two. */
472 if (INTEGRAL_TYPE_P (TREE_TYPE (t
))
473 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
474 && ! ((TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
475 && ! integer_pow2p (TREE_OPERAND (t
, 0)))
476 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
477 && ! integer_pow2p (TREE_OPERAND (t
, 1)))))
483 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t
))))
484 return negate_expr_p (TREE_OPERAND (t
, 1))
485 || negate_expr_p (TREE_OPERAND (t
, 0));
491 if (TYPE_UNSIGNED (type
))
493 if (negate_expr_p (TREE_OPERAND (t
, 0)))
495 /* In general we can't negate B in A / B, because if A is INT_MIN and
496 B is 1, we may turn this into INT_MIN / -1 which is undefined
497 and actually traps on some architectures. */
498 if (! INTEGRAL_TYPE_P (TREE_TYPE (t
))
499 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
500 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
501 && ! integer_onep (TREE_OPERAND (t
, 1))))
502 return negate_expr_p (TREE_OPERAND (t
, 1));
506 /* Negate -((double)float) as (double)(-float). */
507 if (TREE_CODE (type
) == REAL_TYPE
)
509 tree tem
= strip_float_extensions (t
);
511 return negate_expr_p (tem
);
516 /* Negate -f(x) as f(-x). */
517 if (negate_mathfn_p (builtin_mathfn_code (t
)))
518 return negate_expr_p (CALL_EXPR_ARG (t
, 0));
522 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
523 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
525 tree op1
= TREE_OPERAND (t
, 1);
526 if (wi::eq_p (op1
, TYPE_PRECISION (type
) - 1))
537 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
538 simplification is possible.
539 If negate_expr_p would return true for T, NULL_TREE will never be
543 fold_negate_expr (location_t loc
, tree t
)
545 tree type
= TREE_TYPE (t
);
548 switch (TREE_CODE (t
))
550 /* Convert - (~A) to A + 1. */
552 if (INTEGRAL_TYPE_P (type
))
553 return fold_build2_loc (loc
, PLUS_EXPR
, type
, TREE_OPERAND (t
, 0),
554 build_one_cst (type
));
558 tem
= fold_negate_const (t
, type
);
559 if (TREE_OVERFLOW (tem
) == TREE_OVERFLOW (t
)
560 || (ANY_INTEGRAL_TYPE_P (type
)
561 && !TYPE_OVERFLOW_TRAPS (type
)
562 && TYPE_OVERFLOW_WRAPS (type
))
563 || (flag_sanitize
& SANITIZE_SI_OVERFLOW
) == 0)
568 tem
= fold_negate_const (t
, type
);
572 tem
= fold_negate_const (t
, type
);
577 tree rpart
= fold_negate_expr (loc
, TREE_REALPART (t
));
578 tree ipart
= fold_negate_expr (loc
, TREE_IMAGPART (t
));
580 return build_complex (type
, rpart
, ipart
);
586 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
587 tree
*elts
= XALLOCAVEC (tree
, count
);
589 for (i
= 0; i
< count
; i
++)
591 elts
[i
] = fold_negate_expr (loc
, VECTOR_CST_ELT (t
, i
));
592 if (elts
[i
] == NULL_TREE
)
596 return build_vector (type
, elts
);
600 if (negate_expr_p (t
))
601 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
602 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)),
603 fold_negate_expr (loc
, TREE_OPERAND (t
, 1)));
607 if (negate_expr_p (t
))
608 return fold_build1_loc (loc
, CONJ_EXPR
, type
,
609 fold_negate_expr (loc
, TREE_OPERAND (t
, 0)));
613 if (!TYPE_OVERFLOW_SANITIZED (type
))
614 return TREE_OPERAND (t
, 0);
618 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
619 && !HONOR_SIGNED_ZEROS (element_mode (type
)))
621 /* -(A + B) -> (-B) - A. */
622 if (negate_expr_p (TREE_OPERAND (t
, 1))
623 && reorder_operands_p (TREE_OPERAND (t
, 0),
624 TREE_OPERAND (t
, 1)))
626 tem
= negate_expr (TREE_OPERAND (t
, 1));
627 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
628 tem
, TREE_OPERAND (t
, 0));
631 /* -(A + B) -> (-A) - B. */
632 if (negate_expr_p (TREE_OPERAND (t
, 0)))
634 tem
= negate_expr (TREE_OPERAND (t
, 0));
635 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
636 tem
, TREE_OPERAND (t
, 1));
642 /* - (A - B) -> B - A */
643 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
))
644 && !HONOR_SIGNED_ZEROS (element_mode (type
))
645 && reorder_operands_p (TREE_OPERAND (t
, 0), TREE_OPERAND (t
, 1)))
646 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
647 TREE_OPERAND (t
, 1), TREE_OPERAND (t
, 0));
651 if (TYPE_UNSIGNED (type
))
657 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
)))
659 tem
= TREE_OPERAND (t
, 1);
660 if (negate_expr_p (tem
))
661 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
662 TREE_OPERAND (t
, 0), negate_expr (tem
));
663 tem
= TREE_OPERAND (t
, 0);
664 if (negate_expr_p (tem
))
665 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
666 negate_expr (tem
), TREE_OPERAND (t
, 1));
673 if (TYPE_UNSIGNED (type
))
675 if (negate_expr_p (TREE_OPERAND (t
, 0)))
676 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
677 negate_expr (TREE_OPERAND (t
, 0)),
678 TREE_OPERAND (t
, 1));
679 /* In general we can't negate B in A / B, because if A is INT_MIN and
680 B is 1, we may turn this into INT_MIN / -1 which is undefined
681 and actually traps on some architectures. */
682 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t
))
683 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t
))
684 || (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
685 && ! integer_onep (TREE_OPERAND (t
, 1))))
686 && negate_expr_p (TREE_OPERAND (t
, 1)))
687 return fold_build2_loc (loc
, TREE_CODE (t
), type
,
689 negate_expr (TREE_OPERAND (t
, 1)));
693 /* Convert -((double)float) into (double)(-float). */
694 if (TREE_CODE (type
) == REAL_TYPE
)
696 tem
= strip_float_extensions (t
);
697 if (tem
!= t
&& negate_expr_p (tem
))
698 return fold_convert_loc (loc
, type
, negate_expr (tem
));
703 /* Negate -f(x) as f(-x). */
704 if (negate_mathfn_p (builtin_mathfn_code (t
))
705 && negate_expr_p (CALL_EXPR_ARG (t
, 0)))
709 fndecl
= get_callee_fndecl (t
);
710 arg
= negate_expr (CALL_EXPR_ARG (t
, 0));
711 return build_call_expr_loc (loc
, fndecl
, 1, arg
);
716 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
717 if (TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
719 tree op1
= TREE_OPERAND (t
, 1);
720 if (wi::eq_p (op1
, TYPE_PRECISION (type
) - 1))
722 tree ntype
= TYPE_UNSIGNED (type
)
723 ? signed_type_for (type
)
724 : unsigned_type_for (type
);
725 tree temp
= fold_convert_loc (loc
, ntype
, TREE_OPERAND (t
, 0));
726 temp
= fold_build2_loc (loc
, RSHIFT_EXPR
, ntype
, temp
, op1
);
727 return fold_convert_loc (loc
, type
, temp
);
739 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
740 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
752 loc
= EXPR_LOCATION (t
);
753 type
= TREE_TYPE (t
);
756 tem
= fold_negate_expr (loc
, t
);
758 tem
= build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (t
), t
);
759 return fold_convert_loc (loc
, type
, tem
);
762 /* Split a tree IN into a constant, literal and variable parts that could be
763 combined with CODE to make IN. "constant" means an expression with
764 TREE_CONSTANT but that isn't an actual constant. CODE must be a
765 commutative arithmetic operation. Store the constant part into *CONP,
766 the literal in *LITP and return the variable part. If a part isn't
767 present, set it to null. If the tree does not decompose in this way,
768 return the entire tree as the variable part and the other parts as null.
770 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
771 case, we negate an operand that was subtracted. Except if it is a
772 literal for which we use *MINUS_LITP instead.
774 If NEGATE_P is true, we are negating all of IN, again except a literal
775 for which we use *MINUS_LITP instead.
777 If IN is itself a literal or constant, return it as appropriate.
779 Note that we do not guarantee that any of the three values will be the
780 same type as IN, but they will have the same signedness and mode. */
783 split_tree (tree in
, enum tree_code code
, tree
*conp
, tree
*litp
,
784 tree
*minus_litp
, int negate_p
)
792 /* Strip any conversions that don't change the machine mode or signedness. */
793 STRIP_SIGN_NOPS (in
);
795 if (TREE_CODE (in
) == INTEGER_CST
|| TREE_CODE (in
) == REAL_CST
796 || TREE_CODE (in
) == FIXED_CST
)
798 else if (TREE_CODE (in
) == code
799 || ((! FLOAT_TYPE_P (TREE_TYPE (in
)) || flag_associative_math
)
800 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in
))
801 /* We can associate addition and subtraction together (even
802 though the C standard doesn't say so) for integers because
803 the value is not affected. For reals, the value might be
804 affected, so we can't. */
805 && ((code
== PLUS_EXPR
&& TREE_CODE (in
) == MINUS_EXPR
)
806 || (code
== MINUS_EXPR
&& TREE_CODE (in
) == PLUS_EXPR
))))
808 tree op0
= TREE_OPERAND (in
, 0);
809 tree op1
= TREE_OPERAND (in
, 1);
810 int neg1_p
= TREE_CODE (in
) == MINUS_EXPR
;
811 int neg_litp_p
= 0, neg_conp_p
= 0, neg_var_p
= 0;
813 /* First see if either of the operands is a literal, then a constant. */
814 if (TREE_CODE (op0
) == INTEGER_CST
|| TREE_CODE (op0
) == REAL_CST
815 || TREE_CODE (op0
) == FIXED_CST
)
816 *litp
= op0
, op0
= 0;
817 else if (TREE_CODE (op1
) == INTEGER_CST
|| TREE_CODE (op1
) == REAL_CST
818 || TREE_CODE (op1
) == FIXED_CST
)
819 *litp
= op1
, neg_litp_p
= neg1_p
, op1
= 0;
821 if (op0
!= 0 && TREE_CONSTANT (op0
))
822 *conp
= op0
, op0
= 0;
823 else if (op1
!= 0 && TREE_CONSTANT (op1
))
824 *conp
= op1
, neg_conp_p
= neg1_p
, op1
= 0;
826 /* If we haven't dealt with either operand, this is not a case we can
827 decompose. Otherwise, VAR is either of the ones remaining, if any. */
828 if (op0
!= 0 && op1
!= 0)
833 var
= op1
, neg_var_p
= neg1_p
;
835 /* Now do any needed negations. */
837 *minus_litp
= *litp
, *litp
= 0;
839 *conp
= negate_expr (*conp
);
841 var
= negate_expr (var
);
843 else if (TREE_CODE (in
) == BIT_NOT_EXPR
844 && code
== PLUS_EXPR
)
846 /* -X - 1 is folded to ~X, undo that here. */
847 *minus_litp
= build_one_cst (TREE_TYPE (in
));
848 var
= negate_expr (TREE_OPERAND (in
, 0));
850 else if (TREE_CONSTANT (in
))
858 *minus_litp
= *litp
, *litp
= 0;
859 else if (*minus_litp
)
860 *litp
= *minus_litp
, *minus_litp
= 0;
861 *conp
= negate_expr (*conp
);
862 var
= negate_expr (var
);
868 /* Re-associate trees split by the above function. T1 and T2 are
869 either expressions to associate or null. Return the new
870 expression, if any. LOC is the location of the new expression. If
871 we build an operation, do it in TYPE and with CODE. */
874 associate_trees (location_t loc
, tree t1
, tree t2
, enum tree_code code
, tree type
)
881 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
882 try to fold this since we will have infinite recursion. But do
883 deal with any NEGATE_EXPRs. */
884 if (TREE_CODE (t1
) == code
|| TREE_CODE (t2
) == code
885 || TREE_CODE (t1
) == MINUS_EXPR
|| TREE_CODE (t2
) == MINUS_EXPR
)
887 if (code
== PLUS_EXPR
)
889 if (TREE_CODE (t1
) == NEGATE_EXPR
)
890 return build2_loc (loc
, MINUS_EXPR
, type
,
891 fold_convert_loc (loc
, type
, t2
),
892 fold_convert_loc (loc
, type
,
893 TREE_OPERAND (t1
, 0)));
894 else if (TREE_CODE (t2
) == NEGATE_EXPR
)
895 return build2_loc (loc
, MINUS_EXPR
, type
,
896 fold_convert_loc (loc
, type
, t1
),
897 fold_convert_loc (loc
, type
,
898 TREE_OPERAND (t2
, 0)));
899 else if (integer_zerop (t2
))
900 return fold_convert_loc (loc
, type
, t1
);
902 else if (code
== MINUS_EXPR
)
904 if (integer_zerop (t2
))
905 return fold_convert_loc (loc
, type
, t1
);
908 return build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
909 fold_convert_loc (loc
, type
, t2
));
912 return fold_build2_loc (loc
, code
, type
, fold_convert_loc (loc
, type
, t1
),
913 fold_convert_loc (loc
, type
, t2
));
916 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
917 for use in int_const_binop, size_binop and size_diffop. */
920 int_binop_types_match_p (enum tree_code code
, const_tree type1
, const_tree type2
)
922 if (!INTEGRAL_TYPE_P (type1
) && !POINTER_TYPE_P (type1
))
924 if (!INTEGRAL_TYPE_P (type2
) && !POINTER_TYPE_P (type2
))
939 return TYPE_UNSIGNED (type1
) == TYPE_UNSIGNED (type2
)
940 && TYPE_PRECISION (type1
) == TYPE_PRECISION (type2
)
941 && TYPE_MODE (type1
) == TYPE_MODE (type2
);
945 /* Combine two integer constants ARG1 and ARG2 under operation CODE
946 to produce a new constant. Return NULL_TREE if we don't know how
947 to evaluate CODE at compile-time. */
950 int_const_binop_1 (enum tree_code code
, const_tree arg1
, const_tree parg2
,
955 tree type
= TREE_TYPE (arg1
);
956 signop sign
= TYPE_SIGN (type
);
957 bool overflow
= false;
959 wide_int arg2
= wide_int::from (parg2
, TYPE_PRECISION (type
),
960 TYPE_SIGN (TREE_TYPE (parg2
)));
965 res
= wi::bit_or (arg1
, arg2
);
969 res
= wi::bit_xor (arg1
, arg2
);
973 res
= wi::bit_and (arg1
, arg2
);
978 if (wi::neg_p (arg2
))
981 if (code
== RSHIFT_EXPR
)
987 if (code
== RSHIFT_EXPR
)
988 /* It's unclear from the C standard whether shifts can overflow.
989 The following code ignores overflow; perhaps a C standard
990 interpretation ruling is needed. */
991 res
= wi::rshift (arg1
, arg2
, sign
);
993 res
= wi::lshift (arg1
, arg2
);
998 if (wi::neg_p (arg2
))
1001 if (code
== RROTATE_EXPR
)
1002 code
= LROTATE_EXPR
;
1004 code
= RROTATE_EXPR
;
1007 if (code
== RROTATE_EXPR
)
1008 res
= wi::rrotate (arg1
, arg2
);
1010 res
= wi::lrotate (arg1
, arg2
);
1014 res
= wi::add (arg1
, arg2
, sign
, &overflow
);
1018 res
= wi::sub (arg1
, arg2
, sign
, &overflow
);
1022 res
= wi::mul (arg1
, arg2
, sign
, &overflow
);
1025 case MULT_HIGHPART_EXPR
:
1026 res
= wi::mul_high (arg1
, arg2
, sign
);
1029 case TRUNC_DIV_EXPR
:
1030 case EXACT_DIV_EXPR
:
1033 res
= wi::div_trunc (arg1
, arg2
, sign
, &overflow
);
1036 case FLOOR_DIV_EXPR
:
1039 res
= wi::div_floor (arg1
, arg2
, sign
, &overflow
);
1045 res
= wi::div_ceil (arg1
, arg2
, sign
, &overflow
);
1048 case ROUND_DIV_EXPR
:
1051 res
= wi::div_round (arg1
, arg2
, sign
, &overflow
);
1054 case TRUNC_MOD_EXPR
:
1057 res
= wi::mod_trunc (arg1
, arg2
, sign
, &overflow
);
1060 case FLOOR_MOD_EXPR
:
1063 res
= wi::mod_floor (arg1
, arg2
, sign
, &overflow
);
1069 res
= wi::mod_ceil (arg1
, arg2
, sign
, &overflow
);
1072 case ROUND_MOD_EXPR
:
1075 res
= wi::mod_round (arg1
, arg2
, sign
, &overflow
);
1079 res
= wi::min (arg1
, arg2
, sign
);
1083 res
= wi::max (arg1
, arg2
, sign
);
1090 t
= force_fit_type (type
, res
, overflowable
,
1091 (((sign
== SIGNED
|| overflowable
== -1)
1093 | TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (parg2
)));
1099 int_const_binop (enum tree_code code
, const_tree arg1
, const_tree arg2
)
1101 return int_const_binop_1 (code
, arg1
, arg2
, 1);
1104 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1105 constant. We assume ARG1 and ARG2 have the same data type, or at least
1106 are the same kind of constant and the same machine mode. Return zero if
1107 combining the constants is not allowed in the current operating mode. */
1110 const_binop (enum tree_code code
, tree arg1
, tree arg2
)
1112 /* Sanity check for the recursive cases. */
1119 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg2
) == INTEGER_CST
)
1121 if (code
== POINTER_PLUS_EXPR
)
1122 return int_const_binop (PLUS_EXPR
,
1123 arg1
, fold_convert (TREE_TYPE (arg1
), arg2
));
1125 return int_const_binop (code
, arg1
, arg2
);
1128 if (TREE_CODE (arg1
) == REAL_CST
&& TREE_CODE (arg2
) == REAL_CST
)
1133 REAL_VALUE_TYPE value
;
1134 REAL_VALUE_TYPE result
;
1138 /* The following codes are handled by real_arithmetic. */
1153 d1
= TREE_REAL_CST (arg1
);
1154 d2
= TREE_REAL_CST (arg2
);
1156 type
= TREE_TYPE (arg1
);
1157 mode
= TYPE_MODE (type
);
1159 /* Don't perform operation if we honor signaling NaNs and
1160 either operand is a NaN. */
1161 if (HONOR_SNANS (mode
)
1162 && (REAL_VALUE_ISNAN (d1
) || REAL_VALUE_ISNAN (d2
)))
1165 /* Don't perform operation if it would raise a division
1166 by zero exception. */
1167 if (code
== RDIV_EXPR
1168 && real_equal (&d2
, &dconst0
)
1169 && (flag_trapping_math
|| ! MODE_HAS_INFINITIES (mode
)))
1172 /* If either operand is a NaN, just return it. Otherwise, set up
1173 for floating-point trap; we return an overflow. */
1174 if (REAL_VALUE_ISNAN (d1
))
1176 else if (REAL_VALUE_ISNAN (d2
))
1179 inexact
= real_arithmetic (&value
, code
, &d1
, &d2
);
1180 real_convert (&result
, mode
, &value
);
1182 /* Don't constant fold this floating point operation if
1183 the result has overflowed and flag_trapping_math. */
1184 if (flag_trapping_math
1185 && MODE_HAS_INFINITIES (mode
)
1186 && REAL_VALUE_ISINF (result
)
1187 && !REAL_VALUE_ISINF (d1
)
1188 && !REAL_VALUE_ISINF (d2
))
1191 /* Don't constant fold this floating point operation if the
1192 result may dependent upon the run-time rounding mode and
1193 flag_rounding_math is set, or if GCC's software emulation
1194 is unable to accurately represent the result. */
1195 if ((flag_rounding_math
1196 || (MODE_COMPOSITE_P (mode
) && !flag_unsafe_math_optimizations
))
1197 && (inexact
|| !real_identical (&result
, &value
)))
1200 t
= build_real (type
, result
);
1202 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
);
1206 if (TREE_CODE (arg1
) == FIXED_CST
)
1208 FIXED_VALUE_TYPE f1
;
1209 FIXED_VALUE_TYPE f2
;
1210 FIXED_VALUE_TYPE result
;
1215 /* The following codes are handled by fixed_arithmetic. */
1221 case TRUNC_DIV_EXPR
:
1222 if (TREE_CODE (arg2
) != FIXED_CST
)
1224 f2
= TREE_FIXED_CST (arg2
);
1230 if (TREE_CODE (arg2
) != INTEGER_CST
)
1233 f2
.data
.high
= w2
.elt (1);
1234 f2
.data
.low
= w2
.elt (0);
1243 f1
= TREE_FIXED_CST (arg1
);
1244 type
= TREE_TYPE (arg1
);
1245 sat_p
= TYPE_SATURATING (type
);
1246 overflow_p
= fixed_arithmetic (&result
, code
, &f1
, &f2
, sat_p
);
1247 t
= build_fixed (type
, result
);
1248 /* Propagate overflow flags. */
1249 if (overflow_p
| TREE_OVERFLOW (arg1
) | TREE_OVERFLOW (arg2
))
1250 TREE_OVERFLOW (t
) = 1;
1254 if (TREE_CODE (arg1
) == COMPLEX_CST
&& TREE_CODE (arg2
) == COMPLEX_CST
)
1256 tree type
= TREE_TYPE (arg1
);
1257 tree r1
= TREE_REALPART (arg1
);
1258 tree i1
= TREE_IMAGPART (arg1
);
1259 tree r2
= TREE_REALPART (arg2
);
1260 tree i2
= TREE_IMAGPART (arg2
);
1267 real
= const_binop (code
, r1
, r2
);
1268 imag
= const_binop (code
, i1
, i2
);
1272 if (COMPLEX_FLOAT_TYPE_P (type
))
1273 return do_mpc_arg2 (arg1
, arg2
, type
,
1274 /* do_nonfinite= */ folding_initializer
,
1277 real
= const_binop (MINUS_EXPR
,
1278 const_binop (MULT_EXPR
, r1
, r2
),
1279 const_binop (MULT_EXPR
, i1
, i2
));
1280 imag
= const_binop (PLUS_EXPR
,
1281 const_binop (MULT_EXPR
, r1
, i2
),
1282 const_binop (MULT_EXPR
, i1
, r2
));
1286 if (COMPLEX_FLOAT_TYPE_P (type
))
1287 return do_mpc_arg2 (arg1
, arg2
, type
,
1288 /* do_nonfinite= */ folding_initializer
,
1291 case TRUNC_DIV_EXPR
:
1293 case FLOOR_DIV_EXPR
:
1294 case ROUND_DIV_EXPR
:
1295 if (flag_complex_method
== 0)
1297 /* Keep this algorithm in sync with
1298 tree-complex.c:expand_complex_div_straight().
1300 Expand complex division to scalars, straightforward algorithm.
1301 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1305 = const_binop (PLUS_EXPR
,
1306 const_binop (MULT_EXPR
, r2
, r2
),
1307 const_binop (MULT_EXPR
, i2
, i2
));
1309 = const_binop (PLUS_EXPR
,
1310 const_binop (MULT_EXPR
, r1
, r2
),
1311 const_binop (MULT_EXPR
, i1
, i2
));
1313 = const_binop (MINUS_EXPR
,
1314 const_binop (MULT_EXPR
, i1
, r2
),
1315 const_binop (MULT_EXPR
, r1
, i2
));
1317 real
= const_binop (code
, t1
, magsquared
);
1318 imag
= const_binop (code
, t2
, magsquared
);
1322 /* Keep this algorithm in sync with
1323 tree-complex.c:expand_complex_div_wide().
1325 Expand complex division to scalars, modified algorithm to minimize
1326 overflow with wide input ranges. */
1327 tree compare
= fold_build2 (LT_EXPR
, boolean_type_node
,
1328 fold_abs_const (r2
, TREE_TYPE (type
)),
1329 fold_abs_const (i2
, TREE_TYPE (type
)));
1331 if (integer_nonzerop (compare
))
1333 /* In the TRUE branch, we compute
1335 div = (br * ratio) + bi;
1336 tr = (ar * ratio) + ai;
1337 ti = (ai * ratio) - ar;
1340 tree ratio
= const_binop (code
, r2
, i2
);
1341 tree div
= const_binop (PLUS_EXPR
, i2
,
1342 const_binop (MULT_EXPR
, r2
, ratio
));
1343 real
= const_binop (MULT_EXPR
, r1
, ratio
);
1344 real
= const_binop (PLUS_EXPR
, real
, i1
);
1345 real
= const_binop (code
, real
, div
);
1347 imag
= const_binop (MULT_EXPR
, i1
, ratio
);
1348 imag
= const_binop (MINUS_EXPR
, imag
, r1
);
1349 imag
= const_binop (code
, imag
, div
);
1353 /* In the FALSE branch, we compute
1355 divisor = (d * ratio) + c;
1356 tr = (b * ratio) + a;
1357 ti = b - (a * ratio);
1360 tree ratio
= const_binop (code
, i2
, r2
);
1361 tree div
= const_binop (PLUS_EXPR
, r2
,
1362 const_binop (MULT_EXPR
, i2
, ratio
));
1364 real
= const_binop (MULT_EXPR
, i1
, ratio
);
1365 real
= const_binop (PLUS_EXPR
, real
, r1
);
1366 real
= const_binop (code
, real
, div
);
1368 imag
= const_binop (MULT_EXPR
, r1
, ratio
);
1369 imag
= const_binop (MINUS_EXPR
, i1
, imag
);
1370 imag
= const_binop (code
, imag
, div
);
1380 return build_complex (type
, real
, imag
);
1383 if (TREE_CODE (arg1
) == VECTOR_CST
1384 && TREE_CODE (arg2
) == VECTOR_CST
)
1386 tree type
= TREE_TYPE (arg1
);
1387 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
1388 tree
*elts
= XALLOCAVEC (tree
, count
);
1390 for (i
= 0; i
< count
; i
++)
1392 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1393 tree elem2
= VECTOR_CST_ELT (arg2
, i
);
1395 elts
[i
] = const_binop (code
, elem1
, elem2
);
1397 /* It is possible that const_binop cannot handle the given
1398 code and return NULL_TREE */
1399 if (elts
[i
] == NULL_TREE
)
1403 return build_vector (type
, elts
);
1406 /* Shifts allow a scalar offset for a vector. */
1407 if (TREE_CODE (arg1
) == VECTOR_CST
1408 && TREE_CODE (arg2
) == INTEGER_CST
)
1410 tree type
= TREE_TYPE (arg1
);
1411 int count
= TYPE_VECTOR_SUBPARTS (type
), i
;
1412 tree
*elts
= XALLOCAVEC (tree
, count
);
1414 for (i
= 0; i
< count
; i
++)
1416 tree elem1
= VECTOR_CST_ELT (arg1
, i
);
1418 elts
[i
] = const_binop (code
, elem1
, arg2
);
1420 /* It is possible that const_binop cannot handle the given
1421 code and return NULL_TREE. */
1422 if (elts
[i
] == NULL_TREE
)
1426 return build_vector (type
, elts
);
1431 /* Overload that adds a TYPE parameter to be able to dispatch
1432 to fold_relational_const. */
1435 const_binop (enum tree_code code
, tree type
, tree arg1
, tree arg2
)
1437 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
1438 return fold_relational_const (code
, type
, arg1
, arg2
);
1440 /* ??? Until we make the const_binop worker take the type of the
1441 result as argument put those cases that need it here. */
1445 if ((TREE_CODE (arg1
) == REAL_CST
1446 && TREE_CODE (arg2
) == REAL_CST
)
1447 || (TREE_CODE (arg1
) == INTEGER_CST
1448 && TREE_CODE (arg2
) == INTEGER_CST
))
1449 return build_complex (type
, arg1
, arg2
);
1452 case VEC_PACK_TRUNC_EXPR
:
1453 case VEC_PACK_FIX_TRUNC_EXPR
:
1455 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
1458 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
/ 2
1459 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
)) == nelts
/ 2);
1460 if (TREE_CODE (arg1
) != VECTOR_CST
1461 || TREE_CODE (arg2
) != VECTOR_CST
)
1464 elts
= XALLOCAVEC (tree
, nelts
);
1465 if (!vec_cst_ctor_to_array (arg1
, elts
)
1466 || !vec_cst_ctor_to_array (arg2
, elts
+ nelts
/ 2))
1469 for (i
= 0; i
< nelts
; i
++)
1471 elts
[i
] = fold_convert_const (code
== VEC_PACK_TRUNC_EXPR
1472 ? NOP_EXPR
: FIX_TRUNC_EXPR
,
1473 TREE_TYPE (type
), elts
[i
]);
1474 if (elts
[i
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[i
]))
1478 return build_vector (type
, elts
);
1481 case VEC_WIDEN_MULT_LO_EXPR
:
1482 case VEC_WIDEN_MULT_HI_EXPR
:
1483 case VEC_WIDEN_MULT_EVEN_EXPR
:
1484 case VEC_WIDEN_MULT_ODD_EXPR
:
1486 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
);
1487 unsigned int out
, ofs
, scale
;
1490 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
* 2
1491 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2
)) == nelts
* 2);
1492 if (TREE_CODE (arg1
) != VECTOR_CST
|| TREE_CODE (arg2
) != VECTOR_CST
)
1495 elts
= XALLOCAVEC (tree
, nelts
* 4);
1496 if (!vec_cst_ctor_to_array (arg1
, elts
)
1497 || !vec_cst_ctor_to_array (arg2
, elts
+ nelts
* 2))
1500 if (code
== VEC_WIDEN_MULT_LO_EXPR
)
1501 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? nelts
: 0;
1502 else if (code
== VEC_WIDEN_MULT_HI_EXPR
)
1503 scale
= 0, ofs
= BYTES_BIG_ENDIAN
? 0 : nelts
;
1504 else if (code
== VEC_WIDEN_MULT_EVEN_EXPR
)
1506 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1509 for (out
= 0; out
< nelts
; out
++)
1511 unsigned int in1
= (out
<< scale
) + ofs
;
1512 unsigned int in2
= in1
+ nelts
* 2;
1515 t1
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), elts
[in1
]);
1516 t2
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), elts
[in2
]);
1518 if (t1
== NULL_TREE
|| t2
== NULL_TREE
)
1520 elts
[out
] = const_binop (MULT_EXPR
, t1
, t2
);
1521 if (elts
[out
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[out
]))
1525 return build_vector (type
, elts
);
1531 if (TREE_CODE_CLASS (code
) != tcc_binary
)
1534 /* Make sure type and arg0 have the same saturating flag. */
1535 gcc_checking_assert (TYPE_SATURATING (type
)
1536 == TYPE_SATURATING (TREE_TYPE (arg1
)));
1538 return const_binop (code
, arg1
, arg2
);
1541 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1542 Return zero if computing the constants is not possible. */
1545 const_unop (enum tree_code code
, tree type
, tree arg0
)
1551 case FIX_TRUNC_EXPR
:
1552 case FIXED_CONVERT_EXPR
:
1553 return fold_convert_const (code
, type
, arg0
);
1555 case ADDR_SPACE_CONVERT_EXPR
:
1556 if (integer_zerop (arg0
))
1557 return fold_convert_const (code
, type
, arg0
);
1560 case VIEW_CONVERT_EXPR
:
1561 return fold_view_convert_expr (type
, arg0
);
1565 /* Can't call fold_negate_const directly here as that doesn't
1566 handle all cases and we might not be able to negate some
1568 tree tem
= fold_negate_expr (UNKNOWN_LOCATION
, arg0
);
1569 if (tem
&& CONSTANT_CLASS_P (tem
))
1575 if (TREE_CODE (arg0
) == INTEGER_CST
|| TREE_CODE (arg0
) == REAL_CST
)
1576 return fold_abs_const (arg0
, type
);
1580 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1582 tree ipart
= fold_negate_const (TREE_IMAGPART (arg0
),
1584 return build_complex (type
, TREE_REALPART (arg0
), ipart
);
1589 if (TREE_CODE (arg0
) == INTEGER_CST
)
1590 return fold_not_const (arg0
, type
);
1591 /* Perform BIT_NOT_EXPR on each element individually. */
1592 else if (TREE_CODE (arg0
) == VECTOR_CST
)
1596 unsigned count
= VECTOR_CST_NELTS (arg0
), i
;
1598 elements
= XALLOCAVEC (tree
, count
);
1599 for (i
= 0; i
< count
; i
++)
1601 elem
= VECTOR_CST_ELT (arg0
, i
);
1602 elem
= const_unop (BIT_NOT_EXPR
, TREE_TYPE (type
), elem
);
1603 if (elem
== NULL_TREE
)
1608 return build_vector (type
, elements
);
1612 case TRUTH_NOT_EXPR
:
1613 if (TREE_CODE (arg0
) == INTEGER_CST
)
1614 return constant_boolean_node (integer_zerop (arg0
), type
);
1618 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1619 return fold_convert (type
, TREE_REALPART (arg0
));
1623 if (TREE_CODE (arg0
) == COMPLEX_CST
)
1624 return fold_convert (type
, TREE_IMAGPART (arg0
));
1627 case VEC_UNPACK_LO_EXPR
:
1628 case VEC_UNPACK_HI_EXPR
:
1629 case VEC_UNPACK_FLOAT_LO_EXPR
:
1630 case VEC_UNPACK_FLOAT_HI_EXPR
:
1632 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
1634 enum tree_code subcode
;
1636 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
* 2);
1637 if (TREE_CODE (arg0
) != VECTOR_CST
)
1640 elts
= XALLOCAVEC (tree
, nelts
* 2);
1641 if (!vec_cst_ctor_to_array (arg0
, elts
))
1644 if ((!BYTES_BIG_ENDIAN
) ^ (code
== VEC_UNPACK_LO_EXPR
1645 || code
== VEC_UNPACK_FLOAT_LO_EXPR
))
1648 if (code
== VEC_UNPACK_LO_EXPR
|| code
== VEC_UNPACK_HI_EXPR
)
1651 subcode
= FLOAT_EXPR
;
1653 for (i
= 0; i
< nelts
; i
++)
1655 elts
[i
] = fold_convert_const (subcode
, TREE_TYPE (type
), elts
[i
]);
1656 if (elts
[i
] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[i
]))
1660 return build_vector (type
, elts
);
1663 case REDUC_MIN_EXPR
:
1664 case REDUC_MAX_EXPR
:
1665 case REDUC_PLUS_EXPR
:
1667 unsigned int nelts
, i
;
1669 enum tree_code subcode
;
1671 if (TREE_CODE (arg0
) != VECTOR_CST
)
1673 nelts
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
));
1675 elts
= XALLOCAVEC (tree
, nelts
);
1676 if (!vec_cst_ctor_to_array (arg0
, elts
))
1681 case REDUC_MIN_EXPR
: subcode
= MIN_EXPR
; break;
1682 case REDUC_MAX_EXPR
: subcode
= MAX_EXPR
; break;
1683 case REDUC_PLUS_EXPR
: subcode
= PLUS_EXPR
; break;
1684 default: gcc_unreachable ();
1687 for (i
= 1; i
< nelts
; i
++)
1689 elts
[0] = const_binop (subcode
, elts
[0], elts
[i
]);
1690 if (elts
[0] == NULL_TREE
|| !CONSTANT_CLASS_P (elts
[0]))
1704 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1705 indicates which particular sizetype to create. */
1708 size_int_kind (HOST_WIDE_INT number
, enum size_type_kind kind
)
1710 return build_int_cst (sizetype_tab
[(int) kind
], number
);
1713 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1714 is a tree code. The type of the result is taken from the operands.
1715 Both must be equivalent integer types, ala int_binop_types_match_p.
1716 If the operands are constant, so is the result. */
1719 size_binop_loc (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
)
1721 tree type
= TREE_TYPE (arg0
);
1723 if (arg0
== error_mark_node
|| arg1
== error_mark_node
)
1724 return error_mark_node
;
1726 gcc_assert (int_binop_types_match_p (code
, TREE_TYPE (arg0
),
1729 /* Handle the special case of two integer constants faster. */
1730 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
1732 /* And some specific cases even faster than that. */
1733 if (code
== PLUS_EXPR
)
1735 if (integer_zerop (arg0
) && !TREE_OVERFLOW (arg0
))
1737 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1740 else if (code
== MINUS_EXPR
)
1742 if (integer_zerop (arg1
) && !TREE_OVERFLOW (arg1
))
1745 else if (code
== MULT_EXPR
)
1747 if (integer_onep (arg0
) && !TREE_OVERFLOW (arg0
))
1751 /* Handle general case of two integer constants. For sizetype
1752 constant calculations we always want to know about overflow,
1753 even in the unsigned case. */
1754 return int_const_binop_1 (code
, arg0
, arg1
, -1);
1757 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
1760 /* Given two values, either both of sizetype or both of bitsizetype,
1761 compute the difference between the two values. Return the value
1762 in signed type corresponding to the type of the operands. */
1765 size_diffop_loc (location_t loc
, tree arg0
, tree arg1
)
1767 tree type
= TREE_TYPE (arg0
);
1770 gcc_assert (int_binop_types_match_p (MINUS_EXPR
, TREE_TYPE (arg0
),
1773 /* If the type is already signed, just do the simple thing. */
1774 if (!TYPE_UNSIGNED (type
))
1775 return size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
);
1777 if (type
== sizetype
)
1779 else if (type
== bitsizetype
)
1780 ctype
= sbitsizetype
;
1782 ctype
= signed_type_for (type
);
1784 /* If either operand is not a constant, do the conversions to the signed
1785 type and subtract. The hardware will do the right thing with any
1786 overflow in the subtraction. */
1787 if (TREE_CODE (arg0
) != INTEGER_CST
|| TREE_CODE (arg1
) != INTEGER_CST
)
1788 return size_binop_loc (loc
, MINUS_EXPR
,
1789 fold_convert_loc (loc
, ctype
, arg0
),
1790 fold_convert_loc (loc
, ctype
, arg1
));
1792 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1793 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1794 overflow) and negate (which can't either). Special-case a result
1795 of zero while we're here. */
1796 if (tree_int_cst_equal (arg0
, arg1
))
1797 return build_int_cst (ctype
, 0);
1798 else if (tree_int_cst_lt (arg1
, arg0
))
1799 return fold_convert_loc (loc
, ctype
,
1800 size_binop_loc (loc
, MINUS_EXPR
, arg0
, arg1
));
1802 return size_binop_loc (loc
, MINUS_EXPR
, build_int_cst (ctype
, 0),
1803 fold_convert_loc (loc
, ctype
,
1804 size_binop_loc (loc
,
1809 /* A subroutine of fold_convert_const handling conversions of an
1810 INTEGER_CST to another integer type. */
1813 fold_convert_const_int_from_int (tree type
, const_tree arg1
)
1815 /* Given an integer constant, make new constant with new type,
1816 appropriately sign-extended or truncated. Use widest_int
1817 so that any extension is done according ARG1's type. */
1818 return force_fit_type (type
, wi::to_widest (arg1
),
1819 !POINTER_TYPE_P (TREE_TYPE (arg1
)),
1820 TREE_OVERFLOW (arg1
));
1823 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1824 to an integer type. */
1827 fold_convert_const_int_from_real (enum tree_code code
, tree type
, const_tree arg1
)
1829 bool overflow
= false;
1832 /* The following code implements the floating point to integer
1833 conversion rules required by the Java Language Specification,
1834 that IEEE NaNs are mapped to zero and values that overflow
1835 the target precision saturate, i.e. values greater than
1836 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1837 are mapped to INT_MIN. These semantics are allowed by the
1838 C and C++ standards that simply state that the behavior of
1839 FP-to-integer conversion is unspecified upon overflow. */
1843 REAL_VALUE_TYPE x
= TREE_REAL_CST (arg1
);
1847 case FIX_TRUNC_EXPR
:
1848 real_trunc (&r
, VOIDmode
, &x
);
1855 /* If R is NaN, return zero and show we have an overflow. */
1856 if (REAL_VALUE_ISNAN (r
))
1859 val
= wi::zero (TYPE_PRECISION (type
));
1862 /* See if R is less than the lower bound or greater than the
1867 tree lt
= TYPE_MIN_VALUE (type
);
1868 REAL_VALUE_TYPE l
= real_value_from_int_cst (NULL_TREE
, lt
);
1869 if (real_less (&r
, &l
))
1878 tree ut
= TYPE_MAX_VALUE (type
);
1881 REAL_VALUE_TYPE u
= real_value_from_int_cst (NULL_TREE
, ut
);
1882 if (real_less (&u
, &r
))
1891 val
= real_to_integer (&r
, &overflow
, TYPE_PRECISION (type
));
1893 t
= force_fit_type (type
, val
, -1, overflow
| TREE_OVERFLOW (arg1
));
1897 /* A subroutine of fold_convert_const handling conversions of a
1898 FIXED_CST to an integer type. */
1901 fold_convert_const_int_from_fixed (tree type
, const_tree arg1
)
1904 double_int temp
, temp_trunc
;
1907 /* Right shift FIXED_CST to temp by fbit. */
1908 temp
= TREE_FIXED_CST (arg1
).data
;
1909 mode
= TREE_FIXED_CST (arg1
).mode
;
1910 if (GET_MODE_FBIT (mode
) < HOST_BITS_PER_DOUBLE_INT
)
1912 temp
= temp
.rshift (GET_MODE_FBIT (mode
),
1913 HOST_BITS_PER_DOUBLE_INT
,
1914 SIGNED_FIXED_POINT_MODE_P (mode
));
1916 /* Left shift temp to temp_trunc by fbit. */
1917 temp_trunc
= temp
.lshift (GET_MODE_FBIT (mode
),
1918 HOST_BITS_PER_DOUBLE_INT
,
1919 SIGNED_FIXED_POINT_MODE_P (mode
));
1923 temp
= double_int_zero
;
1924 temp_trunc
= double_int_zero
;
1927 /* If FIXED_CST is negative, we need to round the value toward 0.
1928 By checking if the fractional bits are not zero to add 1 to temp. */
1929 if (SIGNED_FIXED_POINT_MODE_P (mode
)
1930 && temp_trunc
.is_negative ()
1931 && TREE_FIXED_CST (arg1
).data
!= temp_trunc
)
1932 temp
+= double_int_one
;
1934 /* Given a fixed-point constant, make new constant with new type,
1935 appropriately sign-extended or truncated. */
1936 t
= force_fit_type (type
, temp
, -1,
1937 (temp
.is_negative ()
1938 && (TYPE_UNSIGNED (type
)
1939 < TYPE_UNSIGNED (TREE_TYPE (arg1
))))
1940 | TREE_OVERFLOW (arg1
));
1945 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1946 to another floating point type. */
1949 fold_convert_const_real_from_real (tree type
, const_tree arg1
)
1951 REAL_VALUE_TYPE value
;
1954 real_convert (&value
, TYPE_MODE (type
), &TREE_REAL_CST (arg1
));
1955 t
= build_real (type
, value
);
1957 /* If converting an infinity or NAN to a representation that doesn't
1958 have one, set the overflow bit so that we can produce some kind of
1959 error message at the appropriate point if necessary. It's not the
1960 most user-friendly message, but it's better than nothing. */
1961 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1
))
1962 && !MODE_HAS_INFINITIES (TYPE_MODE (type
)))
1963 TREE_OVERFLOW (t
) = 1;
1964 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1
))
1965 && !MODE_HAS_NANS (TYPE_MODE (type
)))
1966 TREE_OVERFLOW (t
) = 1;
1967 /* Regular overflow, conversion produced an infinity in a mode that
1968 can't represent them. */
1969 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type
))
1970 && REAL_VALUE_ISINF (value
)
1971 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1
)))
1972 TREE_OVERFLOW (t
) = 1;
1974 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
1978 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1979 to a floating point type. */
1982 fold_convert_const_real_from_fixed (tree type
, const_tree arg1
)
1984 REAL_VALUE_TYPE value
;
1987 real_convert_from_fixed (&value
, TYPE_MODE (type
), &TREE_FIXED_CST (arg1
));
1988 t
= build_real (type
, value
);
1990 TREE_OVERFLOW (t
) = TREE_OVERFLOW (arg1
);
1994 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1995 to another fixed-point type. */
1998 fold_convert_const_fixed_from_fixed (tree type
, const_tree arg1
)
2000 FIXED_VALUE_TYPE value
;
2004 overflow_p
= fixed_convert (&value
, TYPE_MODE (type
), &TREE_FIXED_CST (arg1
),
2005 TYPE_SATURATING (type
));
2006 t
= build_fixed (type
, value
);
2008 /* Propagate overflow flags. */
2009 if (overflow_p
| TREE_OVERFLOW (arg1
))
2010 TREE_OVERFLOW (t
) = 1;
2014 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2015 to a fixed-point type. */
2018 fold_convert_const_fixed_from_int (tree type
, const_tree arg1
)
2020 FIXED_VALUE_TYPE value
;
2025 gcc_assert (TREE_INT_CST_NUNITS (arg1
) <= 2);
2027 di
.low
= TREE_INT_CST_ELT (arg1
, 0);
2028 if (TREE_INT_CST_NUNITS (arg1
) == 1)
2029 di
.high
= (HOST_WIDE_INT
) di
.low
< 0 ? (HOST_WIDE_INT
) -1 : 0;
2031 di
.high
= TREE_INT_CST_ELT (arg1
, 1);
2033 overflow_p
= fixed_convert_from_int (&value
, TYPE_MODE (type
), di
,
2034 TYPE_UNSIGNED (TREE_TYPE (arg1
)),
2035 TYPE_SATURATING (type
));
2036 t
= build_fixed (type
, value
);
2038 /* Propagate overflow flags. */
2039 if (overflow_p
| TREE_OVERFLOW (arg1
))
2040 TREE_OVERFLOW (t
) = 1;
2044 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2045 to a fixed-point type. */
2048 fold_convert_const_fixed_from_real (tree type
, const_tree arg1
)
2050 FIXED_VALUE_TYPE value
;
2054 overflow_p
= fixed_convert_from_real (&value
, TYPE_MODE (type
),
2055 &TREE_REAL_CST (arg1
),
2056 TYPE_SATURATING (type
));
2057 t
= build_fixed (type
, value
);
2059 /* Propagate overflow flags. */
2060 if (overflow_p
| TREE_OVERFLOW (arg1
))
2061 TREE_OVERFLOW (t
) = 1;
2065 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2066 type TYPE. If no simplification can be done return NULL_TREE. */
2069 fold_convert_const (enum tree_code code
, tree type
, tree arg1
)
2071 if (TREE_TYPE (arg1
) == type
)
2074 if (POINTER_TYPE_P (type
) || INTEGRAL_TYPE_P (type
)
2075 || TREE_CODE (type
) == OFFSET_TYPE
)
2077 if (TREE_CODE (arg1
) == INTEGER_CST
)
2078 return fold_convert_const_int_from_int (type
, arg1
);
2079 else if (TREE_CODE (arg1
) == REAL_CST
)
2080 return fold_convert_const_int_from_real (code
, type
, arg1
);
2081 else if (TREE_CODE (arg1
) == FIXED_CST
)
2082 return fold_convert_const_int_from_fixed (type
, arg1
);
2084 else if (TREE_CODE (type
) == REAL_TYPE
)
2086 if (TREE_CODE (arg1
) == INTEGER_CST
)
2087 return build_real_from_int_cst (type
, arg1
);
2088 else if (TREE_CODE (arg1
) == REAL_CST
)
2089 return fold_convert_const_real_from_real (type
, arg1
);
2090 else if (TREE_CODE (arg1
) == FIXED_CST
)
2091 return fold_convert_const_real_from_fixed (type
, arg1
);
2093 else if (TREE_CODE (type
) == FIXED_POINT_TYPE
)
2095 if (TREE_CODE (arg1
) == FIXED_CST
)
2096 return fold_convert_const_fixed_from_fixed (type
, arg1
);
2097 else if (TREE_CODE (arg1
) == INTEGER_CST
)
2098 return fold_convert_const_fixed_from_int (type
, arg1
);
2099 else if (TREE_CODE (arg1
) == REAL_CST
)
2100 return fold_convert_const_fixed_from_real (type
, arg1
);
2105 /* Construct a vector of zero elements of vector type TYPE. */
2108 build_zero_vector (tree type
)
2112 t
= fold_convert_const (NOP_EXPR
, TREE_TYPE (type
), integer_zero_node
);
2113 return build_vector_from_val (type
, t
);
2116 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2119 fold_convertible_p (const_tree type
, const_tree arg
)
2121 tree orig
= TREE_TYPE (arg
);
2126 if (TREE_CODE (arg
) == ERROR_MARK
2127 || TREE_CODE (type
) == ERROR_MARK
2128 || TREE_CODE (orig
) == ERROR_MARK
)
2131 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2134 switch (TREE_CODE (type
))
2136 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2137 case POINTER_TYPE
: case REFERENCE_TYPE
:
2139 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2140 || TREE_CODE (orig
) == OFFSET_TYPE
)
2142 return (TREE_CODE (orig
) == VECTOR_TYPE
2143 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2146 case FIXED_POINT_TYPE
:
2150 return TREE_CODE (type
) == TREE_CODE (orig
);
2157 /* Convert expression ARG to type TYPE. Used by the middle-end for
2158 simple conversions in preference to calling the front-end's convert. */
2161 fold_convert_loc (location_t loc
, tree type
, tree arg
)
2163 tree orig
= TREE_TYPE (arg
);
2169 if (TREE_CODE (arg
) == ERROR_MARK
2170 || TREE_CODE (type
) == ERROR_MARK
2171 || TREE_CODE (orig
) == ERROR_MARK
)
2172 return error_mark_node
;
2174 switch (TREE_CODE (type
))
2177 case REFERENCE_TYPE
:
2178 /* Handle conversions between pointers to different address spaces. */
2179 if (POINTER_TYPE_P (orig
)
2180 && (TYPE_ADDR_SPACE (TREE_TYPE (type
))
2181 != TYPE_ADDR_SPACE (TREE_TYPE (orig
))))
2182 return fold_build1_loc (loc
, ADDR_SPACE_CONVERT_EXPR
, type
, arg
);
2185 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
2187 if (TREE_CODE (arg
) == INTEGER_CST
)
2189 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2190 if (tem
!= NULL_TREE
)
2193 if (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2194 || TREE_CODE (orig
) == OFFSET_TYPE
)
2195 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2196 if (TREE_CODE (orig
) == COMPLEX_TYPE
)
2197 return fold_convert_loc (loc
, type
,
2198 fold_build1_loc (loc
, REALPART_EXPR
,
2199 TREE_TYPE (orig
), arg
));
2200 gcc_assert (TREE_CODE (orig
) == VECTOR_TYPE
2201 && tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2202 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2205 if (TREE_CODE (arg
) == INTEGER_CST
)
2207 tem
= fold_convert_const (FLOAT_EXPR
, type
, arg
);
2208 if (tem
!= NULL_TREE
)
2211 else if (TREE_CODE (arg
) == REAL_CST
)
2213 tem
= fold_convert_const (NOP_EXPR
, type
, arg
);
2214 if (tem
!= NULL_TREE
)
2217 else if (TREE_CODE (arg
) == FIXED_CST
)
2219 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2220 if (tem
!= NULL_TREE
)
2224 switch (TREE_CODE (orig
))
2227 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2228 case POINTER_TYPE
: case REFERENCE_TYPE
:
2229 return fold_build1_loc (loc
, FLOAT_EXPR
, type
, arg
);
2232 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2234 case FIXED_POINT_TYPE
:
2235 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2238 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2239 return fold_convert_loc (loc
, type
, tem
);
2245 case FIXED_POINT_TYPE
:
2246 if (TREE_CODE (arg
) == FIXED_CST
|| TREE_CODE (arg
) == INTEGER_CST
2247 || TREE_CODE (arg
) == REAL_CST
)
2249 tem
= fold_convert_const (FIXED_CONVERT_EXPR
, type
, arg
);
2250 if (tem
!= NULL_TREE
)
2251 goto fold_convert_exit
;
2254 switch (TREE_CODE (orig
))
2256 case FIXED_POINT_TYPE
:
2261 return fold_build1_loc (loc
, FIXED_CONVERT_EXPR
, type
, arg
);
2264 tem
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2265 return fold_convert_loc (loc
, type
, tem
);
2272 switch (TREE_CODE (orig
))
2275 case BOOLEAN_TYPE
: case ENUMERAL_TYPE
:
2276 case POINTER_TYPE
: case REFERENCE_TYPE
:
2278 case FIXED_POINT_TYPE
:
2279 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
2280 fold_convert_loc (loc
, TREE_TYPE (type
), arg
),
2281 fold_convert_loc (loc
, TREE_TYPE (type
),
2282 integer_zero_node
));
2287 if (TREE_CODE (arg
) == COMPLEX_EXPR
)
2289 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2290 TREE_OPERAND (arg
, 0));
2291 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
),
2292 TREE_OPERAND (arg
, 1));
2293 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2296 arg
= save_expr (arg
);
2297 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, TREE_TYPE (orig
), arg
);
2298 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, TREE_TYPE (orig
), arg
);
2299 rpart
= fold_convert_loc (loc
, TREE_TYPE (type
), rpart
);
2300 ipart
= fold_convert_loc (loc
, TREE_TYPE (type
), ipart
);
2301 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rpart
, ipart
);
2309 if (integer_zerop (arg
))
2310 return build_zero_vector (type
);
2311 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type
), TYPE_SIZE (orig
)));
2312 gcc_assert (INTEGRAL_TYPE_P (orig
) || POINTER_TYPE_P (orig
)
2313 || TREE_CODE (orig
) == VECTOR_TYPE
);
2314 return fold_build1_loc (loc
, VIEW_CONVERT_EXPR
, type
, arg
);
2317 tem
= fold_ignored_result (arg
);
2318 return fold_build1_loc (loc
, NOP_EXPR
, type
, tem
);
2321 if (TYPE_MAIN_VARIANT (type
) == TYPE_MAIN_VARIANT (orig
))
2322 return fold_build1_loc (loc
, NOP_EXPR
, type
, arg
);
2326 protected_set_expr_location_unshare (tem
, loc
);
2330 /* Return false if expr can be assumed not to be an lvalue, true
2334 maybe_lvalue_p (const_tree x
)
2336 /* We only need to wrap lvalue tree codes. */
2337 switch (TREE_CODE (x
))
2350 case ARRAY_RANGE_REF
:
2356 case PREINCREMENT_EXPR
:
2357 case PREDECREMENT_EXPR
:
2359 case TRY_CATCH_EXPR
:
2360 case WITH_CLEANUP_EXPR
:
2369 /* Assume the worst for front-end tree codes. */
2370 if ((int)TREE_CODE (x
) >= NUM_TREE_CODES
)
2378 /* Return an expr equal to X but certainly not valid as an lvalue. */
2381 non_lvalue_loc (location_t loc
, tree x
)
2383 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2388 if (! maybe_lvalue_p (x
))
2390 return build1_loc (loc
, NON_LVALUE_EXPR
, TREE_TYPE (x
), x
);
2393 /* When pedantic, return an expr equal to X but certainly not valid as a
2394 pedantic lvalue. Otherwise, return X. */
2397 pedantic_non_lvalue_loc (location_t loc
, tree x
)
2399 return protected_set_expr_location_unshare (x
, loc
);
2402 /* Given a tree comparison code, return the code that is the logical inverse.
2403 It is generally not safe to do this for floating-point comparisons, except
2404 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2405 ERROR_MARK in this case. */
2408 invert_tree_comparison (enum tree_code code
, bool honor_nans
)
2410 if (honor_nans
&& flag_trapping_math
&& code
!= EQ_EXPR
&& code
!= NE_EXPR
2411 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
)
2421 return honor_nans
? UNLE_EXPR
: LE_EXPR
;
2423 return honor_nans
? UNLT_EXPR
: LT_EXPR
;
2425 return honor_nans
? UNGE_EXPR
: GE_EXPR
;
2427 return honor_nans
? UNGT_EXPR
: GT_EXPR
;
2441 return UNORDERED_EXPR
;
2442 case UNORDERED_EXPR
:
2443 return ORDERED_EXPR
;
2449 /* Similar, but return the comparison that results if the operands are
2450 swapped. This is safe for floating-point. */
2453 swap_tree_comparison (enum tree_code code
)
2460 case UNORDERED_EXPR
:
2486 /* Convert a comparison tree code from an enum tree_code representation
2487 into a compcode bit-based encoding. This function is the inverse of
2488 compcode_to_comparison. */
2490 static enum comparison_code
2491 comparison_to_compcode (enum tree_code code
)
2508 return COMPCODE_ORD
;
2509 case UNORDERED_EXPR
:
2510 return COMPCODE_UNORD
;
2512 return COMPCODE_UNLT
;
2514 return COMPCODE_UNEQ
;
2516 return COMPCODE_UNLE
;
2518 return COMPCODE_UNGT
;
2520 return COMPCODE_LTGT
;
2522 return COMPCODE_UNGE
;
2528 /* Convert a compcode bit-based encoding of a comparison operator back
2529 to GCC's enum tree_code representation. This function is the
2530 inverse of comparison_to_compcode. */
2532 static enum tree_code
2533 compcode_to_comparison (enum comparison_code code
)
2550 return ORDERED_EXPR
;
2551 case COMPCODE_UNORD
:
2552 return UNORDERED_EXPR
;
2570 /* Return a tree for the comparison which is the combination of
2571 doing the AND or OR (depending on CODE) of the two operations LCODE
2572 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2573 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2574 if this makes the transformation invalid. */
2577 combine_comparisons (location_t loc
,
2578 enum tree_code code
, enum tree_code lcode
,
2579 enum tree_code rcode
, tree truth_type
,
2580 tree ll_arg
, tree lr_arg
)
2582 bool honor_nans
= HONOR_NANS (ll_arg
);
2583 enum comparison_code lcompcode
= comparison_to_compcode (lcode
);
2584 enum comparison_code rcompcode
= comparison_to_compcode (rcode
);
2589 case TRUTH_AND_EXPR
: case TRUTH_ANDIF_EXPR
:
2590 compcode
= lcompcode
& rcompcode
;
2593 case TRUTH_OR_EXPR
: case TRUTH_ORIF_EXPR
:
2594 compcode
= lcompcode
| rcompcode
;
2603 /* Eliminate unordered comparisons, as well as LTGT and ORD
2604 which are not used unless the mode has NaNs. */
2605 compcode
&= ~COMPCODE_UNORD
;
2606 if (compcode
== COMPCODE_LTGT
)
2607 compcode
= COMPCODE_NE
;
2608 else if (compcode
== COMPCODE_ORD
)
2609 compcode
= COMPCODE_TRUE
;
2611 else if (flag_trapping_math
)
2613 /* Check that the original operation and the optimized ones will trap
2614 under the same condition. */
2615 bool ltrap
= (lcompcode
& COMPCODE_UNORD
) == 0
2616 && (lcompcode
!= COMPCODE_EQ
)
2617 && (lcompcode
!= COMPCODE_ORD
);
2618 bool rtrap
= (rcompcode
& COMPCODE_UNORD
) == 0
2619 && (rcompcode
!= COMPCODE_EQ
)
2620 && (rcompcode
!= COMPCODE_ORD
);
2621 bool trap
= (compcode
& COMPCODE_UNORD
) == 0
2622 && (compcode
!= COMPCODE_EQ
)
2623 && (compcode
!= COMPCODE_ORD
);
2625 /* In a short-circuited boolean expression the LHS might be
2626 such that the RHS, if evaluated, will never trap. For
2627 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2628 if neither x nor y is NaN. (This is a mixed blessing: for
2629 example, the expression above will never trap, hence
2630 optimizing it to x < y would be invalid). */
2631 if ((code
== TRUTH_ORIF_EXPR
&& (lcompcode
& COMPCODE_UNORD
))
2632 || (code
== TRUTH_ANDIF_EXPR
&& !(lcompcode
& COMPCODE_UNORD
)))
2635 /* If the comparison was short-circuited, and only the RHS
2636 trapped, we may now generate a spurious trap. */
2638 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
2641 /* If we changed the conditions that cause a trap, we lose. */
2642 if ((ltrap
|| rtrap
) != trap
)
2646 if (compcode
== COMPCODE_TRUE
)
2647 return constant_boolean_node (true, truth_type
);
2648 else if (compcode
== COMPCODE_FALSE
)
2649 return constant_boolean_node (false, truth_type
);
2652 enum tree_code tcode
;
2654 tcode
= compcode_to_comparison ((enum comparison_code
) compcode
);
2655 return fold_build2_loc (loc
, tcode
, truth_type
, ll_arg
, lr_arg
);
2659 /* Return nonzero if two operands (typically of the same tree node)
2660 are necessarily equal. If either argument has side-effects this
2661 function returns zero. FLAGS modifies behavior as follows:
2663 If OEP_ONLY_CONST is set, only return nonzero for constants.
2664 This function tests whether the operands are indistinguishable;
2665 it does not test whether they are equal using C's == operation.
2666 The distinction is important for IEEE floating point, because
2667 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2668 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2670 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2671 even though it may hold multiple values during a function.
2672 This is because a GCC tree node guarantees that nothing else is
2673 executed between the evaluation of its "operands" (which may often
2674 be evaluated in arbitrary order). Hence if the operands themselves
2675 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2676 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2677 unset means assuming isochronic (or instantaneous) tree equivalence.
2678 Unless comparing arbitrary expression trees, such as from different
2679 statements, this flag can usually be left unset.
2681 If OEP_PURE_SAME is set, then pure functions with identical arguments
2682 are considered the same. It is used when the caller has other ways
2683 to ensure that global memory is unchanged in between.
2685 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2686 not values of expressions. OEP_CONSTANT_ADDRESS_OF in addition to
2687 OEP_ADDRESS_OF is used for ADDR_EXPR with TREE_CONSTANT flag set and we
2688 further ignore any side effects on SAVE_EXPRs then. */
2691 operand_equal_p (const_tree arg0
, const_tree arg1
, unsigned int flags
)
2693 /* If either is ERROR_MARK, they aren't equal. */
2694 if (TREE_CODE (arg0
) == ERROR_MARK
|| TREE_CODE (arg1
) == ERROR_MARK
2695 || TREE_TYPE (arg0
) == error_mark_node
2696 || TREE_TYPE (arg1
) == error_mark_node
)
2699 /* Similar, if either does not have a type (like a released SSA name),
2700 they aren't equal. */
2701 if (!TREE_TYPE (arg0
) || !TREE_TYPE (arg1
))
2704 /* Check equality of integer constants before bailing out due to
2705 precision differences. */
2706 if (TREE_CODE (arg0
) == INTEGER_CST
&& TREE_CODE (arg1
) == INTEGER_CST
)
2708 /* Address of INTEGER_CST is not defined; check that we did not forget
2709 to drop the OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2710 gcc_checking_assert (!(flags
2711 & (OEP_ADDRESS_OF
| OEP_CONSTANT_ADDRESS_OF
)));
2712 return tree_int_cst_equal (arg0
, arg1
);
2715 if (!(flags
& OEP_ADDRESS_OF
))
2717 /* If both types don't have the same signedness, then we can't consider
2718 them equal. We must check this before the STRIP_NOPS calls
2719 because they may change the signedness of the arguments. As pointers
2720 strictly don't have a signedness, require either two pointers or
2721 two non-pointers as well. */
2722 if (TYPE_UNSIGNED (TREE_TYPE (arg0
)) != TYPE_UNSIGNED (TREE_TYPE (arg1
))
2723 || POINTER_TYPE_P (TREE_TYPE (arg0
))
2724 != POINTER_TYPE_P (TREE_TYPE (arg1
)))
2727 /* We cannot consider pointers to different address space equal. */
2728 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
2729 && POINTER_TYPE_P (TREE_TYPE (arg1
))
2730 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0
)))
2731 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1
)))))
2734 /* If both types don't have the same precision, then it is not safe
2736 if (element_precision (TREE_TYPE (arg0
))
2737 != element_precision (TREE_TYPE (arg1
)))
2744 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2745 sanity check once the issue is solved. */
2747 /* Addresses of conversions and SSA_NAMEs (and many other things)
2748 are not defined. Check that we did not forget to drop the
2749 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2750 gcc_checking_assert (!CONVERT_EXPR_P (arg0
) && !CONVERT_EXPR_P (arg1
)
2751 && TREE_CODE (arg0
) != SSA_NAME
);
2754 /* In case both args are comparisons but with different comparison
2755 code, try to swap the comparison operands of one arg to produce
2756 a match and compare that variant. */
2757 if (TREE_CODE (arg0
) != TREE_CODE (arg1
)
2758 && COMPARISON_CLASS_P (arg0
)
2759 && COMPARISON_CLASS_P (arg1
))
2761 enum tree_code swap_code
= swap_tree_comparison (TREE_CODE (arg1
));
2763 if (TREE_CODE (arg0
) == swap_code
)
2764 return operand_equal_p (TREE_OPERAND (arg0
, 0),
2765 TREE_OPERAND (arg1
, 1), flags
)
2766 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2767 TREE_OPERAND (arg1
, 0), flags
);
2770 if (TREE_CODE (arg0
) != TREE_CODE (arg1
))
2772 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2773 if (CONVERT_EXPR_P (arg0
) && CONVERT_EXPR_P (arg1
))
2775 else if (flags
& OEP_ADDRESS_OF
)
2777 /* If we are interested in comparing addresses ignore
2778 MEM_REF wrappings of the base that can appear just for
2780 if (TREE_CODE (arg0
) == MEM_REF
2782 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ADDR_EXPR
2783 && TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0) == arg1
2784 && integer_zerop (TREE_OPERAND (arg0
, 1)))
2786 else if (TREE_CODE (arg1
) == MEM_REF
2788 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ADDR_EXPR
2789 && TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0) == arg0
2790 && integer_zerop (TREE_OPERAND (arg1
, 1)))
2798 /* When not checking adddresses, this is needed for conversions and for
2799 COMPONENT_REF. Might as well play it safe and always test this. */
2800 if (TREE_CODE (TREE_TYPE (arg0
)) == ERROR_MARK
2801 || TREE_CODE (TREE_TYPE (arg1
)) == ERROR_MARK
2802 || (TYPE_MODE (TREE_TYPE (arg0
)) != TYPE_MODE (TREE_TYPE (arg1
))
2803 && !(flags
& OEP_ADDRESS_OF
)))
2806 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2807 We don't care about side effects in that case because the SAVE_EXPR
2808 takes care of that for us. In all other cases, two expressions are
2809 equal if they have no side effects. If we have two identical
2810 expressions with side effects that should be treated the same due
2811 to the only side effects being identical SAVE_EXPR's, that will
2812 be detected in the recursive calls below.
2813 If we are taking an invariant address of two identical objects
2814 they are necessarily equal as well. */
2815 if (arg0
== arg1
&& ! (flags
& OEP_ONLY_CONST
)
2816 && (TREE_CODE (arg0
) == SAVE_EXPR
2817 || (flags
& OEP_CONSTANT_ADDRESS_OF
)
2818 || (! TREE_SIDE_EFFECTS (arg0
) && ! TREE_SIDE_EFFECTS (arg1
))))
2821 /* Next handle constant cases, those for which we can return 1 even
2822 if ONLY_CONST is set. */
2823 if (TREE_CONSTANT (arg0
) && TREE_CONSTANT (arg1
))
2824 switch (TREE_CODE (arg0
))
2827 return tree_int_cst_equal (arg0
, arg1
);
2830 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0
),
2831 TREE_FIXED_CST (arg1
));
2834 if (real_identical (&TREE_REAL_CST (arg0
), &TREE_REAL_CST (arg1
)))
2838 if (!HONOR_SIGNED_ZEROS (arg0
))
2840 /* If we do not distinguish between signed and unsigned zero,
2841 consider them equal. */
2842 if (real_zerop (arg0
) && real_zerop (arg1
))
2851 if (VECTOR_CST_NELTS (arg0
) != VECTOR_CST_NELTS (arg1
))
2854 for (i
= 0; i
< VECTOR_CST_NELTS (arg0
); ++i
)
2856 if (!operand_equal_p (VECTOR_CST_ELT (arg0
, i
),
2857 VECTOR_CST_ELT (arg1
, i
), flags
))
2864 return (operand_equal_p (TREE_REALPART (arg0
), TREE_REALPART (arg1
),
2866 && operand_equal_p (TREE_IMAGPART (arg0
), TREE_IMAGPART (arg1
),
2870 return (TREE_STRING_LENGTH (arg0
) == TREE_STRING_LENGTH (arg1
)
2871 && ! memcmp (TREE_STRING_POINTER (arg0
),
2872 TREE_STRING_POINTER (arg1
),
2873 TREE_STRING_LENGTH (arg0
)));
2876 gcc_checking_assert (!(flags
2877 & (OEP_ADDRESS_OF
| OEP_CONSTANT_ADDRESS_OF
)));
2878 return operand_equal_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 0),
2879 flags
| OEP_ADDRESS_OF
2880 | OEP_CONSTANT_ADDRESS_OF
);
2882 /* In GIMPLE empty constructors are allowed in initializers of
2884 return (!vec_safe_length (CONSTRUCTOR_ELTS (arg0
))
2885 && !vec_safe_length (CONSTRUCTOR_ELTS (arg1
)));
2890 if (flags
& OEP_ONLY_CONST
)
2893 /* Define macros to test an operand from arg0 and arg1 for equality and a
2894 variant that allows null and views null as being different from any
2895 non-null value. In the latter case, if either is null, the both
2896 must be; otherwise, do the normal comparison. */
2897 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2898 TREE_OPERAND (arg1, N), flags)
2900 #define OP_SAME_WITH_NULL(N) \
2901 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2902 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2904 switch (TREE_CODE_CLASS (TREE_CODE (arg0
)))
2907 /* Two conversions are equal only if signedness and modes match. */
2908 switch (TREE_CODE (arg0
))
2911 case FIX_TRUNC_EXPR
:
2912 if (TYPE_UNSIGNED (TREE_TYPE (arg0
))
2913 != TYPE_UNSIGNED (TREE_TYPE (arg1
)))
2923 case tcc_comparison
:
2925 if (OP_SAME (0) && OP_SAME (1))
2928 /* For commutative ops, allow the other order. */
2929 return (commutative_tree_code (TREE_CODE (arg0
))
2930 && operand_equal_p (TREE_OPERAND (arg0
, 0),
2931 TREE_OPERAND (arg1
, 1), flags
)
2932 && operand_equal_p (TREE_OPERAND (arg0
, 1),
2933 TREE_OPERAND (arg1
, 0), flags
));
2936 /* If either of the pointer (or reference) expressions we are
2937 dereferencing contain a side effect, these cannot be equal,
2938 but their addresses can be. */
2939 if ((flags
& OEP_CONSTANT_ADDRESS_OF
) == 0
2940 && (TREE_SIDE_EFFECTS (arg0
)
2941 || TREE_SIDE_EFFECTS (arg1
)))
2944 switch (TREE_CODE (arg0
))
2947 if (!(flags
& (OEP_ADDRESS_OF
| OEP_CONSTANT_ADDRESS_OF
))
2948 && (TYPE_ALIGN (TREE_TYPE (arg0
))
2949 != TYPE_ALIGN (TREE_TYPE (arg1
))))
2951 flags
&= ~(OEP_CONSTANT_ADDRESS_OF
|OEP_ADDRESS_OF
);
2956 case VIEW_CONVERT_EXPR
:
2959 case TARGET_MEM_REF
:
2961 if (!(flags
& (OEP_ADDRESS_OF
| OEP_CONSTANT_ADDRESS_OF
)))
2963 /* Require equal access sizes */
2964 if (TYPE_SIZE (TREE_TYPE (arg0
)) != TYPE_SIZE (TREE_TYPE (arg1
))
2965 && (!TYPE_SIZE (TREE_TYPE (arg0
))
2966 || !TYPE_SIZE (TREE_TYPE (arg1
))
2967 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0
)),
2968 TYPE_SIZE (TREE_TYPE (arg1
)),
2971 /* Verify that accesses are TBAA compatible. */
2972 if (flag_strict_aliasing
2973 && (!alias_ptr_types_compatible_p
2974 (TREE_TYPE (TREE_OPERAND (arg0
, 1)),
2975 TREE_TYPE (TREE_OPERAND (arg1
, 1)))
2976 || (MR_DEPENDENCE_CLIQUE (arg0
)
2977 != MR_DEPENDENCE_CLIQUE (arg1
))
2978 || (MR_DEPENDENCE_BASE (arg0
)
2979 != MR_DEPENDENCE_BASE (arg1
))))
2981 /* Verify that alignment is compatible. */
2982 if (TYPE_ALIGN (TREE_TYPE (arg0
))
2983 != TYPE_ALIGN (TREE_TYPE (arg1
)))
2986 flags
&= ~(OEP_CONSTANT_ADDRESS_OF
|OEP_ADDRESS_OF
);
2987 return (OP_SAME (0) && OP_SAME (1)
2988 /* TARGET_MEM_REF require equal extra operands. */
2989 && (TREE_CODE (arg0
) != TARGET_MEM_REF
2990 || (OP_SAME_WITH_NULL (2)
2991 && OP_SAME_WITH_NULL (3)
2992 && OP_SAME_WITH_NULL (4))));
2995 case ARRAY_RANGE_REF
:
2996 /* Operands 2 and 3 may be null.
2997 Compare the array index by value if it is constant first as we
2998 may have different types but same value here. */
3001 flags
&= ~(OEP_CONSTANT_ADDRESS_OF
|OEP_ADDRESS_OF
);
3002 return ((tree_int_cst_equal (TREE_OPERAND (arg0
, 1),
3003 TREE_OPERAND (arg1
, 1))
3005 && OP_SAME_WITH_NULL (2)
3006 && OP_SAME_WITH_NULL (3));
3009 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3010 may be NULL when we're called to compare MEM_EXPRs. */
3011 if (!OP_SAME_WITH_NULL (0)
3014 flags
&= ~(OEP_CONSTANT_ADDRESS_OF
|OEP_ADDRESS_OF
);
3015 return OP_SAME_WITH_NULL (2);
3020 flags
&= ~(OEP_CONSTANT_ADDRESS_OF
|OEP_ADDRESS_OF
);
3021 return OP_SAME (1) && OP_SAME (2);
3027 case tcc_expression
:
3028 switch (TREE_CODE (arg0
))
3031 /* Be sure we pass right ADDRESS_OF flag. */
3032 gcc_checking_assert (!(flags
3034 | OEP_CONSTANT_ADDRESS_OF
)));
3035 return operand_equal_p (TREE_OPERAND (arg0
, 0),
3036 TREE_OPERAND (arg1
, 0),
3037 flags
| OEP_ADDRESS_OF
);
3039 case TRUTH_NOT_EXPR
:
3042 case TRUTH_ANDIF_EXPR
:
3043 case TRUTH_ORIF_EXPR
:
3044 return OP_SAME (0) && OP_SAME (1);
3047 case WIDEN_MULT_PLUS_EXPR
:
3048 case WIDEN_MULT_MINUS_EXPR
:
3051 /* The multiplcation operands are commutative. */
3054 case TRUTH_AND_EXPR
:
3056 case TRUTH_XOR_EXPR
:
3057 if (OP_SAME (0) && OP_SAME (1))
3060 /* Otherwise take into account this is a commutative operation. */
3061 return (operand_equal_p (TREE_OPERAND (arg0
, 0),
3062 TREE_OPERAND (arg1
, 1), flags
)
3063 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3064 TREE_OPERAND (arg1
, 0), flags
));
3069 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3076 switch (TREE_CODE (arg0
))
3079 if ((CALL_EXPR_FN (arg0
) == NULL_TREE
)
3080 != (CALL_EXPR_FN (arg1
) == NULL_TREE
))
3081 /* If not both CALL_EXPRs are either internal or normal function
3082 functions, then they are not equal. */
3084 else if (CALL_EXPR_FN (arg0
) == NULL_TREE
)
3086 /* If the CALL_EXPRs call different internal functions, then they
3088 if (CALL_EXPR_IFN (arg0
) != CALL_EXPR_IFN (arg1
))
3093 /* If the CALL_EXPRs call different functions, then they are not
3095 if (! operand_equal_p (CALL_EXPR_FN (arg0
), CALL_EXPR_FN (arg1
),
3101 unsigned int cef
= call_expr_flags (arg0
);
3102 if (flags
& OEP_PURE_SAME
)
3103 cef
&= ECF_CONST
| ECF_PURE
;
3110 /* Now see if all the arguments are the same. */
3112 const_call_expr_arg_iterator iter0
, iter1
;
3114 for (a0
= first_const_call_expr_arg (arg0
, &iter0
),
3115 a1
= first_const_call_expr_arg (arg1
, &iter1
);
3117 a0
= next_const_call_expr_arg (&iter0
),
3118 a1
= next_const_call_expr_arg (&iter1
))
3119 if (! operand_equal_p (a0
, a1
, flags
))
3122 /* If we get here and both argument lists are exhausted
3123 then the CALL_EXPRs are equal. */
3124 return ! (a0
|| a1
);
3130 case tcc_declaration
:
3131 /* Consider __builtin_sqrt equal to sqrt. */
3132 return (TREE_CODE (arg0
) == FUNCTION_DECL
3133 && DECL_BUILT_IN (arg0
) && DECL_BUILT_IN (arg1
)
3134 && DECL_BUILT_IN_CLASS (arg0
) == DECL_BUILT_IN_CLASS (arg1
)
3135 && DECL_FUNCTION_CODE (arg0
) == DECL_FUNCTION_CODE (arg1
));
3137 case tcc_exceptional
:
3138 if (TREE_CODE (arg0
) == CONSTRUCTOR
)
3140 /* In GIMPLE constructors are used only to build vectors from
3141 elements. Individual elements in the constructor must be
3142 indexed in increasing order and form an initial sequence.
3144 We make no effort to compare constructors in generic.
3145 (see sem_variable::equals in ipa-icf which can do so for
3147 if (!VECTOR_TYPE_P (TREE_TYPE (arg0
))
3148 || !VECTOR_TYPE_P (TREE_TYPE (arg1
)))
3151 /* Be sure that vectors constructed have the same representation.
3152 We only tested element precision and modes to match.
3153 Vectors may be BLKmode and thus also check that the number of
3155 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
))
3156 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)))
3159 vec
<constructor_elt
, va_gc
> *v0
= CONSTRUCTOR_ELTS (arg0
);
3160 vec
<constructor_elt
, va_gc
> *v1
= CONSTRUCTOR_ELTS (arg1
);
3161 unsigned int len
= vec_safe_length (v0
);
3163 if (len
!= vec_safe_length (v1
))
3166 for (unsigned int i
= 0; i
< len
; i
++)
3168 constructor_elt
*c0
= &(*v0
)[i
];
3169 constructor_elt
*c1
= &(*v1
)[i
];
3171 if (!operand_equal_p (c0
->value
, c1
->value
, flags
)
3172 /* In GIMPLE the indexes can be either NULL or matching i.
3173 Double check this so we won't get false
3174 positives for GENERIC. */
3176 && (TREE_CODE (c0
->index
) != INTEGER_CST
3177 || !compare_tree_int (c0
->index
, i
)))
3179 && (TREE_CODE (c1
->index
) != INTEGER_CST
3180 || !compare_tree_int (c1
->index
, i
))))
3192 #undef OP_SAME_WITH_NULL
3195 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3196 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3198 When in doubt, return 0. */
3201 operand_equal_for_comparison_p (tree arg0
, tree arg1
, tree other
)
3203 int unsignedp1
, unsignedpo
;
3204 tree primarg0
, primarg1
, primother
;
3205 unsigned int correct_width
;
3207 if (operand_equal_p (arg0
, arg1
, 0))
3210 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
3211 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1
)))
3214 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3215 and see if the inner values are the same. This removes any
3216 signedness comparison, which doesn't matter here. */
3217 primarg0
= arg0
, primarg1
= arg1
;
3218 STRIP_NOPS (primarg0
);
3219 STRIP_NOPS (primarg1
);
3220 if (operand_equal_p (primarg0
, primarg1
, 0))
3223 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3224 actual comparison operand, ARG0.
3226 First throw away any conversions to wider types
3227 already present in the operands. */
3229 primarg1
= get_narrower (arg1
, &unsignedp1
);
3230 primother
= get_narrower (other
, &unsignedpo
);
3232 correct_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
3233 if (unsignedp1
== unsignedpo
3234 && TYPE_PRECISION (TREE_TYPE (primarg1
)) < correct_width
3235 && TYPE_PRECISION (TREE_TYPE (primother
)) < correct_width
)
3237 tree type
= TREE_TYPE (arg0
);
3239 /* Make sure shorter operand is extended the right way
3240 to match the longer operand. */
3241 primarg1
= fold_convert (signed_or_unsigned_type_for
3242 (unsignedp1
, TREE_TYPE (primarg1
)), primarg1
);
3244 if (operand_equal_p (arg0
, fold_convert (type
, primarg1
), 0))
3251 /* See if ARG is an expression that is either a comparison or is performing
3252 arithmetic on comparisons. The comparisons must only be comparing
3253 two different values, which will be stored in *CVAL1 and *CVAL2; if
3254 they are nonzero it means that some operands have already been found.
3255 No variables may be used anywhere else in the expression except in the
3256 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3257 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3259 If this is true, return 1. Otherwise, return zero. */
3262 twoval_comparison_p (tree arg
, tree
*cval1
, tree
*cval2
, int *save_p
)
3264 enum tree_code code
= TREE_CODE (arg
);
3265 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3267 /* We can handle some of the tcc_expression cases here. */
3268 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3270 else if (tclass
== tcc_expression
3271 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
3272 || code
== COMPOUND_EXPR
))
3273 tclass
= tcc_binary
;
3275 else if (tclass
== tcc_expression
&& code
== SAVE_EXPR
3276 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg
, 0)))
3278 /* If we've already found a CVAL1 or CVAL2, this expression is
3279 two complex to handle. */
3280 if (*cval1
|| *cval2
)
3290 return twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
);
3293 return (twoval_comparison_p (TREE_OPERAND (arg
, 0), cval1
, cval2
, save_p
)
3294 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
3295 cval1
, cval2
, save_p
));
3300 case tcc_expression
:
3301 if (code
== COND_EXPR
)
3302 return (twoval_comparison_p (TREE_OPERAND (arg
, 0),
3303 cval1
, cval2
, save_p
)
3304 && twoval_comparison_p (TREE_OPERAND (arg
, 1),
3305 cval1
, cval2
, save_p
)
3306 && twoval_comparison_p (TREE_OPERAND (arg
, 2),
3307 cval1
, cval2
, save_p
));
3310 case tcc_comparison
:
3311 /* First see if we can handle the first operand, then the second. For
3312 the second operand, we know *CVAL1 can't be zero. It must be that
3313 one side of the comparison is each of the values; test for the
3314 case where this isn't true by failing if the two operands
3317 if (operand_equal_p (TREE_OPERAND (arg
, 0),
3318 TREE_OPERAND (arg
, 1), 0))
3322 *cval1
= TREE_OPERAND (arg
, 0);
3323 else if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 0), 0))
3325 else if (*cval2
== 0)
3326 *cval2
= TREE_OPERAND (arg
, 0);
3327 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 0), 0))
3332 if (operand_equal_p (*cval1
, TREE_OPERAND (arg
, 1), 0))
3334 else if (*cval2
== 0)
3335 *cval2
= TREE_OPERAND (arg
, 1);
3336 else if (operand_equal_p (*cval2
, TREE_OPERAND (arg
, 1), 0))
3348 /* ARG is a tree that is known to contain just arithmetic operations and
3349 comparisons. Evaluate the operations in the tree substituting NEW0 for
3350 any occurrence of OLD0 as an operand of a comparison and likewise for
3354 eval_subst (location_t loc
, tree arg
, tree old0
, tree new0
,
3355 tree old1
, tree new1
)
3357 tree type
= TREE_TYPE (arg
);
3358 enum tree_code code
= TREE_CODE (arg
);
3359 enum tree_code_class tclass
= TREE_CODE_CLASS (code
);
3361 /* We can handle some of the tcc_expression cases here. */
3362 if (tclass
== tcc_expression
&& code
== TRUTH_NOT_EXPR
)
3364 else if (tclass
== tcc_expression
3365 && (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_ORIF_EXPR
))
3366 tclass
= tcc_binary
;
3371 return fold_build1_loc (loc
, code
, type
,
3372 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3373 old0
, new0
, old1
, new1
));
3376 return fold_build2_loc (loc
, code
, type
,
3377 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3378 old0
, new0
, old1
, new1
),
3379 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3380 old0
, new0
, old1
, new1
));
3382 case tcc_expression
:
3386 return eval_subst (loc
, TREE_OPERAND (arg
, 0), old0
, new0
,
3390 return eval_subst (loc
, TREE_OPERAND (arg
, 1), old0
, new0
,
3394 return fold_build3_loc (loc
, code
, type
,
3395 eval_subst (loc
, TREE_OPERAND (arg
, 0),
3396 old0
, new0
, old1
, new1
),
3397 eval_subst (loc
, TREE_OPERAND (arg
, 1),
3398 old0
, new0
, old1
, new1
),
3399 eval_subst (loc
, TREE_OPERAND (arg
, 2),
3400 old0
, new0
, old1
, new1
));
3404 /* Fall through - ??? */
3406 case tcc_comparison
:
3408 tree arg0
= TREE_OPERAND (arg
, 0);
3409 tree arg1
= TREE_OPERAND (arg
, 1);
3411 /* We need to check both for exact equality and tree equality. The
3412 former will be true if the operand has a side-effect. In that
3413 case, we know the operand occurred exactly once. */
3415 if (arg0
== old0
|| operand_equal_p (arg0
, old0
, 0))
3417 else if (arg0
== old1
|| operand_equal_p (arg0
, old1
, 0))
3420 if (arg1
== old0
|| operand_equal_p (arg1
, old0
, 0))
3422 else if (arg1
== old1
|| operand_equal_p (arg1
, old1
, 0))
3425 return fold_build2_loc (loc
, code
, type
, arg0
, arg1
);
3433 /* Return a tree for the case when the result of an expression is RESULT
3434 converted to TYPE and OMITTED was previously an operand of the expression
3435 but is now not needed (e.g., we folded OMITTED * 0).
3437 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3438 the conversion of RESULT to TYPE. */
3441 omit_one_operand_loc (location_t loc
, tree type
, tree result
, tree omitted
)
3443 tree t
= fold_convert_loc (loc
, type
, result
);
3445 /* If the resulting operand is an empty statement, just return the omitted
3446 statement casted to void. */
3447 if (IS_EMPTY_STMT (t
) && TREE_SIDE_EFFECTS (omitted
))
3448 return build1_loc (loc
, NOP_EXPR
, void_type_node
,
3449 fold_ignored_result (omitted
));
3451 if (TREE_SIDE_EFFECTS (omitted
))
3452 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3453 fold_ignored_result (omitted
), t
);
3455 return non_lvalue_loc (loc
, t
);
3458 /* Return a tree for the case when the result of an expression is RESULT
3459 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3460 of the expression but are now not needed.
3462 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3463 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3464 evaluated before OMITTED2. Otherwise, if neither has side effects,
3465 just do the conversion of RESULT to TYPE. */
3468 omit_two_operands_loc (location_t loc
, tree type
, tree result
,
3469 tree omitted1
, tree omitted2
)
3471 tree t
= fold_convert_loc (loc
, type
, result
);
3473 if (TREE_SIDE_EFFECTS (omitted2
))
3474 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted2
, t
);
3475 if (TREE_SIDE_EFFECTS (omitted1
))
3476 t
= build2_loc (loc
, COMPOUND_EXPR
, type
, omitted1
, t
);
3478 return TREE_CODE (t
) != COMPOUND_EXPR
? non_lvalue_loc (loc
, t
) : t
;
3482 /* Return a simplified tree node for the truth-negation of ARG. This
3483 never alters ARG itself. We assume that ARG is an operation that
3484 returns a truth value (0 or 1).
3486 FIXME: one would think we would fold the result, but it causes
3487 problems with the dominator optimizer. */
3490 fold_truth_not_expr (location_t loc
, tree arg
)
3492 tree type
= TREE_TYPE (arg
);
3493 enum tree_code code
= TREE_CODE (arg
);
3494 location_t loc1
, loc2
;
3496 /* If this is a comparison, we can simply invert it, except for
3497 floating-point non-equality comparisons, in which case we just
3498 enclose a TRUTH_NOT_EXPR around what we have. */
3500 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
3502 tree op_type
= TREE_TYPE (TREE_OPERAND (arg
, 0));
3503 if (FLOAT_TYPE_P (op_type
)
3504 && flag_trapping_math
3505 && code
!= ORDERED_EXPR
&& code
!= UNORDERED_EXPR
3506 && code
!= NE_EXPR
&& code
!= EQ_EXPR
)
3509 code
= invert_tree_comparison (code
, HONOR_NANS (op_type
));
3510 if (code
== ERROR_MARK
)
3513 return build2_loc (loc
, code
, type
, TREE_OPERAND (arg
, 0),
3514 TREE_OPERAND (arg
, 1));
3520 return constant_boolean_node (integer_zerop (arg
), type
);
3522 case TRUTH_AND_EXPR
:
3523 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3524 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3525 return build2_loc (loc
, TRUTH_OR_EXPR
, type
,
3526 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3527 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3530 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3531 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3532 return build2_loc (loc
, TRUTH_AND_EXPR
, type
,
3533 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3534 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3536 case TRUTH_XOR_EXPR
:
3537 /* Here we can invert either operand. We invert the first operand
3538 unless the second operand is a TRUTH_NOT_EXPR in which case our
3539 result is the XOR of the first operand with the inside of the
3540 negation of the second operand. */
3542 if (TREE_CODE (TREE_OPERAND (arg
, 1)) == TRUTH_NOT_EXPR
)
3543 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
, TREE_OPERAND (arg
, 0),
3544 TREE_OPERAND (TREE_OPERAND (arg
, 1), 0));
3546 return build2_loc (loc
, TRUTH_XOR_EXPR
, type
,
3547 invert_truthvalue_loc (loc
, TREE_OPERAND (arg
, 0)),
3548 TREE_OPERAND (arg
, 1));
3550 case TRUTH_ANDIF_EXPR
:
3551 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3552 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3553 return build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
3554 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3555 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3557 case TRUTH_ORIF_EXPR
:
3558 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3559 loc2
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3560 return build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
3561 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)),
3562 invert_truthvalue_loc (loc2
, TREE_OPERAND (arg
, 1)));
3564 case TRUTH_NOT_EXPR
:
3565 return TREE_OPERAND (arg
, 0);
3569 tree arg1
= TREE_OPERAND (arg
, 1);
3570 tree arg2
= TREE_OPERAND (arg
, 2);
3572 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3573 loc2
= expr_location_or (TREE_OPERAND (arg
, 2), loc
);
3575 /* A COND_EXPR may have a throw as one operand, which
3576 then has void type. Just leave void operands
3578 return build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg
, 0),
3579 VOID_TYPE_P (TREE_TYPE (arg1
))
3580 ? arg1
: invert_truthvalue_loc (loc1
, arg1
),
3581 VOID_TYPE_P (TREE_TYPE (arg2
))
3582 ? arg2
: invert_truthvalue_loc (loc2
, arg2
));
3586 loc1
= expr_location_or (TREE_OPERAND (arg
, 1), loc
);
3587 return build2_loc (loc
, COMPOUND_EXPR
, type
,
3588 TREE_OPERAND (arg
, 0),
3589 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 1)));
3591 case NON_LVALUE_EXPR
:
3592 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3593 return invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0));
3596 if (TREE_CODE (TREE_TYPE (arg
)) == BOOLEAN_TYPE
)
3597 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3599 /* ... fall through ... */
3602 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3603 return build1_loc (loc
, TREE_CODE (arg
), type
,
3604 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3607 if (!integer_onep (TREE_OPERAND (arg
, 1)))
3609 return build2_loc (loc
, EQ_EXPR
, type
, arg
, build_int_cst (type
, 0));
3612 return build1_loc (loc
, TRUTH_NOT_EXPR
, type
, arg
);
3614 case CLEANUP_POINT_EXPR
:
3615 loc1
= expr_location_or (TREE_OPERAND (arg
, 0), loc
);
3616 return build1_loc (loc
, CLEANUP_POINT_EXPR
, type
,
3617 invert_truthvalue_loc (loc1
, TREE_OPERAND (arg
, 0)));
3624 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3625 assume that ARG is an operation that returns a truth value (0 or 1
3626 for scalars, 0 or -1 for vectors). Return the folded expression if
3627 folding is successful. Otherwise, return NULL_TREE. */
3630 fold_invert_truthvalue (location_t loc
, tree arg
)
3632 tree type
= TREE_TYPE (arg
);
3633 return fold_unary_loc (loc
, VECTOR_TYPE_P (type
)
3639 /* Return a simplified tree node for the truth-negation of ARG. This
3640 never alters ARG itself. We assume that ARG is an operation that
3641 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3644 invert_truthvalue_loc (location_t loc
, tree arg
)
3646 if (TREE_CODE (arg
) == ERROR_MARK
)
3649 tree type
= TREE_TYPE (arg
);
3650 return fold_build1_loc (loc
, VECTOR_TYPE_P (type
)
3656 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3657 with code CODE. This optimization is unsafe. */
3659 distribute_real_division (location_t loc
, enum tree_code code
, tree type
,
3660 tree arg0
, tree arg1
)
3662 bool mul0
= TREE_CODE (arg0
) == MULT_EXPR
;
3663 bool mul1
= TREE_CODE (arg1
) == MULT_EXPR
;
3665 /* (A / C) +- (B / C) -> (A +- B) / C. */
3667 && operand_equal_p (TREE_OPERAND (arg0
, 1),
3668 TREE_OPERAND (arg1
, 1), 0))
3669 return fold_build2_loc (loc
, mul0
? MULT_EXPR
: RDIV_EXPR
, type
,
3670 fold_build2_loc (loc
, code
, type
,
3671 TREE_OPERAND (arg0
, 0),
3672 TREE_OPERAND (arg1
, 0)),
3673 TREE_OPERAND (arg0
, 1));
3675 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3676 if (operand_equal_p (TREE_OPERAND (arg0
, 0),
3677 TREE_OPERAND (arg1
, 0), 0)
3678 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
3679 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == REAL_CST
)
3681 REAL_VALUE_TYPE r0
, r1
;
3682 r0
= TREE_REAL_CST (TREE_OPERAND (arg0
, 1));
3683 r1
= TREE_REAL_CST (TREE_OPERAND (arg1
, 1));
3685 real_arithmetic (&r0
, RDIV_EXPR
, &dconst1
, &r0
);
3687 real_arithmetic (&r1
, RDIV_EXPR
, &dconst1
, &r1
);
3688 real_arithmetic (&r0
, code
, &r0
, &r1
);
3689 return fold_build2_loc (loc
, MULT_EXPR
, type
,
3690 TREE_OPERAND (arg0
, 0),
3691 build_real (type
, r0
));
3697 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3698 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3701 make_bit_field_ref (location_t loc
, tree inner
, tree type
,
3702 HOST_WIDE_INT bitsize
, HOST_WIDE_INT bitpos
, int unsignedp
)
3704 tree result
, bftype
;
3708 tree size
= TYPE_SIZE (TREE_TYPE (inner
));
3709 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner
))
3710 || POINTER_TYPE_P (TREE_TYPE (inner
)))
3711 && tree_fits_shwi_p (size
)
3712 && tree_to_shwi (size
) == bitsize
)
3713 return fold_convert_loc (loc
, type
, inner
);
3717 if (TYPE_PRECISION (bftype
) != bitsize
3718 || TYPE_UNSIGNED (bftype
) == !unsignedp
)
3719 bftype
= build_nonstandard_integer_type (bitsize
, 0);
3721 result
= build3_loc (loc
, BIT_FIELD_REF
, bftype
, inner
,
3722 size_int (bitsize
), bitsize_int (bitpos
));
3725 result
= fold_convert_loc (loc
, type
, result
);
3730 /* Optimize a bit-field compare.
3732 There are two cases: First is a compare against a constant and the
3733 second is a comparison of two items where the fields are at the same
3734 bit position relative to the start of a chunk (byte, halfword, word)
3735 large enough to contain it. In these cases we can avoid the shift
3736 implicit in bitfield extractions.
3738 For constants, we emit a compare of the shifted constant with the
3739 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3740 compared. For two fields at the same position, we do the ANDs with the
3741 similar mask and compare the result of the ANDs.
3743 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3744 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3745 are the left and right operands of the comparison, respectively.
3747 If the optimization described above can be done, we return the resulting
3748 tree. Otherwise we return zero. */
3751 optimize_bit_field_compare (location_t loc
, enum tree_code code
,
3752 tree compare_type
, tree lhs
, tree rhs
)
3754 HOST_WIDE_INT lbitpos
, lbitsize
, rbitpos
, rbitsize
, nbitpos
, nbitsize
;
3755 tree type
= TREE_TYPE (lhs
);
3757 int const_p
= TREE_CODE (rhs
) == INTEGER_CST
;
3758 machine_mode lmode
, rmode
, nmode
;
3759 int lunsignedp
, runsignedp
;
3760 int lvolatilep
= 0, rvolatilep
= 0;
3761 tree linner
, rinner
= NULL_TREE
;
3765 /* Get all the information about the extractions being done. If the bit size
3766 if the same as the size of the underlying object, we aren't doing an
3767 extraction at all and so can do nothing. We also don't want to
3768 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3769 then will no longer be able to replace it. */
3770 linner
= get_inner_reference (lhs
, &lbitsize
, &lbitpos
, &offset
, &lmode
,
3771 &lunsignedp
, &lvolatilep
, false);
3772 if (linner
== lhs
|| lbitsize
== GET_MODE_BITSIZE (lmode
) || lbitsize
< 0
3773 || offset
!= 0 || TREE_CODE (linner
) == PLACEHOLDER_EXPR
|| lvolatilep
)
3778 /* If this is not a constant, we can only do something if bit positions,
3779 sizes, and signedness are the same. */
3780 rinner
= get_inner_reference (rhs
, &rbitsize
, &rbitpos
, &offset
, &rmode
,
3781 &runsignedp
, &rvolatilep
, false);
3783 if (rinner
== rhs
|| lbitpos
!= rbitpos
|| lbitsize
!= rbitsize
3784 || lunsignedp
!= runsignedp
|| offset
!= 0
3785 || TREE_CODE (rinner
) == PLACEHOLDER_EXPR
|| rvolatilep
)
3789 /* See if we can find a mode to refer to this field. We should be able to,
3790 but fail if we can't. */
3791 nmode
= get_best_mode (lbitsize
, lbitpos
, 0, 0,
3792 const_p
? TYPE_ALIGN (TREE_TYPE (linner
))
3793 : MIN (TYPE_ALIGN (TREE_TYPE (linner
)),
3794 TYPE_ALIGN (TREE_TYPE (rinner
))),
3796 if (nmode
== VOIDmode
)
3799 /* Set signed and unsigned types of the precision of this mode for the
3801 unsigned_type
= lang_hooks
.types
.type_for_mode (nmode
, 1);
3803 /* Compute the bit position and size for the new reference and our offset
3804 within it. If the new reference is the same size as the original, we
3805 won't optimize anything, so return zero. */
3806 nbitsize
= GET_MODE_BITSIZE (nmode
);
3807 nbitpos
= lbitpos
& ~ (nbitsize
- 1);
3809 if (nbitsize
== lbitsize
)
3812 if (BYTES_BIG_ENDIAN
)
3813 lbitpos
= nbitsize
- lbitsize
- lbitpos
;
3815 /* Make the mask to be used against the extracted field. */
3816 mask
= build_int_cst_type (unsigned_type
, -1);
3817 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (nbitsize
- lbitsize
));
3818 mask
= const_binop (RSHIFT_EXPR
, mask
,
3819 size_int (nbitsize
- lbitsize
- lbitpos
));
3822 /* If not comparing with constant, just rework the comparison
3824 return fold_build2_loc (loc
, code
, compare_type
,
3825 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
3826 make_bit_field_ref (loc
, linner
,
3831 fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
3832 make_bit_field_ref (loc
, rinner
,
3838 /* Otherwise, we are handling the constant case. See if the constant is too
3839 big for the field. Warn and return a tree of for 0 (false) if so. We do
3840 this not only for its own sake, but to avoid having to test for this
3841 error case below. If we didn't, we might generate wrong code.
3843 For unsigned fields, the constant shifted right by the field length should
3844 be all zero. For signed fields, the high-order bits should agree with
3849 if (wi::lrshift (rhs
, lbitsize
) != 0)
3851 warning (0, "comparison is always %d due to width of bit-field",
3853 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
3858 wide_int tem
= wi::arshift (rhs
, lbitsize
- 1);
3859 if (tem
!= 0 && tem
!= -1)
3861 warning (0, "comparison is always %d due to width of bit-field",
3863 return constant_boolean_node (code
== NE_EXPR
, compare_type
);
3867 /* Single-bit compares should always be against zero. */
3868 if (lbitsize
== 1 && ! integer_zerop (rhs
))
3870 code
= code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
;
3871 rhs
= build_int_cst (type
, 0);
3874 /* Make a new bitfield reference, shift the constant over the
3875 appropriate number of bits and mask it with the computed mask
3876 (in case this was a signed field). If we changed it, make a new one. */
3877 lhs
= make_bit_field_ref (loc
, linner
, unsigned_type
, nbitsize
, nbitpos
, 1);
3879 rhs
= const_binop (BIT_AND_EXPR
,
3880 const_binop (LSHIFT_EXPR
,
3881 fold_convert_loc (loc
, unsigned_type
, rhs
),
3882 size_int (lbitpos
)),
3885 lhs
= build2_loc (loc
, code
, compare_type
,
3886 build2 (BIT_AND_EXPR
, unsigned_type
, lhs
, mask
), rhs
);
3890 /* Subroutine for fold_truth_andor_1: decode a field reference.
3892 If EXP is a comparison reference, we return the innermost reference.
3894 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3895 set to the starting bit number.
3897 If the innermost field can be completely contained in a mode-sized
3898 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3900 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3901 otherwise it is not changed.
3903 *PUNSIGNEDP is set to the signedness of the field.
3905 *PMASK is set to the mask used. This is either contained in a
3906 BIT_AND_EXPR or derived from the width of the field.
3908 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3910 Return 0 if this is not a component reference or is one that we can't
3911 do anything with. */
3914 decode_field_reference (location_t loc
, tree exp
, HOST_WIDE_INT
*pbitsize
,
3915 HOST_WIDE_INT
*pbitpos
, machine_mode
*pmode
,
3916 int *punsignedp
, int *pvolatilep
,
3917 tree
*pmask
, tree
*pand_mask
)
3919 tree outer_type
= 0;
3921 tree mask
, inner
, offset
;
3923 unsigned int precision
;
3925 /* All the optimizations using this function assume integer fields.
3926 There are problems with FP fields since the type_for_size call
3927 below can fail for, e.g., XFmode. */
3928 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp
)))
3931 /* We are interested in the bare arrangement of bits, so strip everything
3932 that doesn't affect the machine mode. However, record the type of the
3933 outermost expression if it may matter below. */
3934 if (CONVERT_EXPR_P (exp
)
3935 || TREE_CODE (exp
) == NON_LVALUE_EXPR
)
3936 outer_type
= TREE_TYPE (exp
);
3939 if (TREE_CODE (exp
) == BIT_AND_EXPR
)
3941 and_mask
= TREE_OPERAND (exp
, 1);
3942 exp
= TREE_OPERAND (exp
, 0);
3943 STRIP_NOPS (exp
); STRIP_NOPS (and_mask
);
3944 if (TREE_CODE (and_mask
) != INTEGER_CST
)
3948 inner
= get_inner_reference (exp
, pbitsize
, pbitpos
, &offset
, pmode
,
3949 punsignedp
, pvolatilep
, false);
3950 if ((inner
== exp
&& and_mask
== 0)
3951 || *pbitsize
< 0 || offset
!= 0
3952 || TREE_CODE (inner
) == PLACEHOLDER_EXPR
)
3955 /* If the number of bits in the reference is the same as the bitsize of
3956 the outer type, then the outer type gives the signedness. Otherwise
3957 (in case of a small bitfield) the signedness is unchanged. */
3958 if (outer_type
&& *pbitsize
== TYPE_PRECISION (outer_type
))
3959 *punsignedp
= TYPE_UNSIGNED (outer_type
);
3961 /* Compute the mask to access the bitfield. */
3962 unsigned_type
= lang_hooks
.types
.type_for_size (*pbitsize
, 1);
3963 precision
= TYPE_PRECISION (unsigned_type
);
3965 mask
= build_int_cst_type (unsigned_type
, -1);
3967 mask
= const_binop (LSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
3968 mask
= const_binop (RSHIFT_EXPR
, mask
, size_int (precision
- *pbitsize
));
3970 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3972 mask
= fold_build2_loc (loc
, BIT_AND_EXPR
, unsigned_type
,
3973 fold_convert_loc (loc
, unsigned_type
, and_mask
), mask
);
3976 *pand_mask
= and_mask
;
3980 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3981 bit positions and MASK is SIGNED. */
3984 all_ones_mask_p (const_tree mask
, unsigned int size
)
3986 tree type
= TREE_TYPE (mask
);
3987 unsigned int precision
= TYPE_PRECISION (type
);
3989 /* If this function returns true when the type of the mask is
3990 UNSIGNED, then there will be errors. In particular see
3991 gcc.c-torture/execute/990326-1.c. There does not appear to be
3992 any documentation paper trail as to why this is so. But the pre
3993 wide-int worked with that restriction and it has been preserved
3995 if (size
> precision
|| TYPE_SIGN (type
) == UNSIGNED
)
3998 return wi::mask (size
, false, precision
) == mask
;
4001 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4002 represents the sign bit of EXP's type. If EXP represents a sign
4003 or zero extension, also test VAL against the unextended type.
4004 The return value is the (sub)expression whose sign bit is VAL,
4005 or NULL_TREE otherwise. */
4008 sign_bit_p (tree exp
, const_tree val
)
4013 /* Tree EXP must have an integral type. */
4014 t
= TREE_TYPE (exp
);
4015 if (! INTEGRAL_TYPE_P (t
))
4018 /* Tree VAL must be an integer constant. */
4019 if (TREE_CODE (val
) != INTEGER_CST
4020 || TREE_OVERFLOW (val
))
4023 width
= TYPE_PRECISION (t
);
4024 if (wi::only_sign_bit_p (val
, width
))
4027 /* Handle extension from a narrower type. */
4028 if (TREE_CODE (exp
) == NOP_EXPR
4029 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp
, 0))) < width
)
4030 return sign_bit_p (TREE_OPERAND (exp
, 0), val
);
4035 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4036 to be evaluated unconditionally. */
4039 simple_operand_p (const_tree exp
)
4041 /* Strip any conversions that don't change the machine mode. */
4044 return (CONSTANT_CLASS_P (exp
)
4045 || TREE_CODE (exp
) == SSA_NAME
4047 && ! TREE_ADDRESSABLE (exp
)
4048 && ! TREE_THIS_VOLATILE (exp
)
4049 && ! DECL_NONLOCAL (exp
)
4050 /* Don't regard global variables as simple. They may be
4051 allocated in ways unknown to the compiler (shared memory,
4052 #pragma weak, etc). */
4053 && ! TREE_PUBLIC (exp
)
4054 && ! DECL_EXTERNAL (exp
)
4055 /* Weakrefs are not safe to be read, since they can be NULL.
4056 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4057 have DECL_WEAK flag set. */
4058 && (! VAR_OR_FUNCTION_DECL_P (exp
) || ! DECL_WEAK (exp
))
4059 /* Loading a static variable is unduly expensive, but global
4060 registers aren't expensive. */
4061 && (! TREE_STATIC (exp
) || DECL_REGISTER (exp
))));
4064 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4065 to be evaluated unconditionally.
4066 I addition to simple_operand_p, we assume that comparisons, conversions,
4067 and logic-not operations are simple, if their operands are simple, too. */
4070 simple_operand_p_2 (tree exp
)
4072 enum tree_code code
;
4074 if (TREE_SIDE_EFFECTS (exp
)
4075 || tree_could_trap_p (exp
))
4078 while (CONVERT_EXPR_P (exp
))
4079 exp
= TREE_OPERAND (exp
, 0);
4081 code
= TREE_CODE (exp
);
4083 if (TREE_CODE_CLASS (code
) == tcc_comparison
)
4084 return (simple_operand_p (TREE_OPERAND (exp
, 0))
4085 && simple_operand_p (TREE_OPERAND (exp
, 1)));
4087 if (code
== TRUTH_NOT_EXPR
)
4088 return simple_operand_p_2 (TREE_OPERAND (exp
, 0));
4090 return simple_operand_p (exp
);
4094 /* The following functions are subroutines to fold_range_test and allow it to
4095 try to change a logical combination of comparisons into a range test.
4098 X == 2 || X == 3 || X == 4 || X == 5
4102 (unsigned) (X - 2) <= 3
4104 We describe each set of comparisons as being either inside or outside
4105 a range, using a variable named like IN_P, and then describe the
4106 range with a lower and upper bound. If one of the bounds is omitted,
4107 it represents either the highest or lowest value of the type.
4109 In the comments below, we represent a range by two numbers in brackets
4110 preceded by a "+" to designate being inside that range, or a "-" to
4111 designate being outside that range, so the condition can be inverted by
4112 flipping the prefix. An omitted bound is represented by a "-". For
4113 example, "- [-, 10]" means being outside the range starting at the lowest
4114 possible value and ending at 10, in other words, being greater than 10.
4115 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4118 We set up things so that the missing bounds are handled in a consistent
4119 manner so neither a missing bound nor "true" and "false" need to be
4120 handled using a special case. */
4122 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4123 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4124 and UPPER1_P are nonzero if the respective argument is an upper bound
4125 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4126 must be specified for a comparison. ARG1 will be converted to ARG0's
4127 type if both are specified. */
4130 range_binop (enum tree_code code
, tree type
, tree arg0
, int upper0_p
,
4131 tree arg1
, int upper1_p
)
4137 /* If neither arg represents infinity, do the normal operation.
4138 Else, if not a comparison, return infinity. Else handle the special
4139 comparison rules. Note that most of the cases below won't occur, but
4140 are handled for consistency. */
4142 if (arg0
!= 0 && arg1
!= 0)
4144 tem
= fold_build2 (code
, type
!= 0 ? type
: TREE_TYPE (arg0
),
4145 arg0
, fold_convert (TREE_TYPE (arg0
), arg1
));
4147 return TREE_CODE (tem
) == INTEGER_CST
? tem
: 0;
4150 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
4153 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4154 for neither. In real maths, we cannot assume open ended ranges are
4155 the same. But, this is computer arithmetic, where numbers are finite.
4156 We can therefore make the transformation of any unbounded range with
4157 the value Z, Z being greater than any representable number. This permits
4158 us to treat unbounded ranges as equal. */
4159 sgn0
= arg0
!= 0 ? 0 : (upper0_p
? 1 : -1);
4160 sgn1
= arg1
!= 0 ? 0 : (upper1_p
? 1 : -1);
4164 result
= sgn0
== sgn1
;
4167 result
= sgn0
!= sgn1
;
4170 result
= sgn0
< sgn1
;
4173 result
= sgn0
<= sgn1
;
4176 result
= sgn0
> sgn1
;
4179 result
= sgn0
>= sgn1
;
4185 return constant_boolean_node (result
, type
);
4188 /* Helper routine for make_range. Perform one step for it, return
4189 new expression if the loop should continue or NULL_TREE if it should
4193 make_range_step (location_t loc
, enum tree_code code
, tree arg0
, tree arg1
,
4194 tree exp_type
, tree
*p_low
, tree
*p_high
, int *p_in_p
,
4195 bool *strict_overflow_p
)
4197 tree arg0_type
= TREE_TYPE (arg0
);
4198 tree n_low
, n_high
, low
= *p_low
, high
= *p_high
;
4199 int in_p
= *p_in_p
, n_in_p
;
4203 case TRUTH_NOT_EXPR
:
4204 /* We can only do something if the range is testing for zero. */
4205 if (low
== NULL_TREE
|| high
== NULL_TREE
4206 || ! integer_zerop (low
) || ! integer_zerop (high
))
4211 case EQ_EXPR
: case NE_EXPR
:
4212 case LT_EXPR
: case LE_EXPR
: case GE_EXPR
: case GT_EXPR
:
4213 /* We can only do something if the range is testing for zero
4214 and if the second operand is an integer constant. Note that
4215 saying something is "in" the range we make is done by
4216 complementing IN_P since it will set in the initial case of
4217 being not equal to zero; "out" is leaving it alone. */
4218 if (low
== NULL_TREE
|| high
== NULL_TREE
4219 || ! integer_zerop (low
) || ! integer_zerop (high
)
4220 || TREE_CODE (arg1
) != INTEGER_CST
)
4225 case NE_EXPR
: /* - [c, c] */
4228 case EQ_EXPR
: /* + [c, c] */
4229 in_p
= ! in_p
, low
= high
= arg1
;
4231 case GT_EXPR
: /* - [-, c] */
4232 low
= 0, high
= arg1
;
4234 case GE_EXPR
: /* + [c, -] */
4235 in_p
= ! in_p
, low
= arg1
, high
= 0;
4237 case LT_EXPR
: /* - [c, -] */
4238 low
= arg1
, high
= 0;
4240 case LE_EXPR
: /* + [-, c] */
4241 in_p
= ! in_p
, low
= 0, high
= arg1
;
4247 /* If this is an unsigned comparison, we also know that EXP is
4248 greater than or equal to zero. We base the range tests we make
4249 on that fact, so we record it here so we can parse existing
4250 range tests. We test arg0_type since often the return type
4251 of, e.g. EQ_EXPR, is boolean. */
4252 if (TYPE_UNSIGNED (arg0_type
) && (low
== 0 || high
== 0))
4254 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
,
4256 build_int_cst (arg0_type
, 0),
4260 in_p
= n_in_p
, low
= n_low
, high
= n_high
;
4262 /* If the high bound is missing, but we have a nonzero low
4263 bound, reverse the range so it goes from zero to the low bound
4265 if (high
== 0 && low
&& ! integer_zerop (low
))
4268 high
= range_binop (MINUS_EXPR
, NULL_TREE
, low
, 0,
4269 build_int_cst (TREE_TYPE (low
), 1), 0);
4270 low
= build_int_cst (arg0_type
, 0);
4280 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4281 low and high are non-NULL, then normalize will DTRT. */
4282 if (!TYPE_UNSIGNED (arg0_type
)
4283 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4285 if (low
== NULL_TREE
)
4286 low
= TYPE_MIN_VALUE (arg0_type
);
4287 if (high
== NULL_TREE
)
4288 high
= TYPE_MAX_VALUE (arg0_type
);
4291 /* (-x) IN [a,b] -> x in [-b, -a] */
4292 n_low
= range_binop (MINUS_EXPR
, exp_type
,
4293 build_int_cst (exp_type
, 0),
4295 n_high
= range_binop (MINUS_EXPR
, exp_type
,
4296 build_int_cst (exp_type
, 0),
4298 if (n_high
!= 0 && TREE_OVERFLOW (n_high
))
4304 return build2_loc (loc
, MINUS_EXPR
, exp_type
, negate_expr (arg0
),
4305 build_int_cst (exp_type
, 1));
4309 if (TREE_CODE (arg1
) != INTEGER_CST
)
4312 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4313 move a constant to the other side. */
4314 if (!TYPE_UNSIGNED (arg0_type
)
4315 && !TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4318 /* If EXP is signed, any overflow in the computation is undefined,
4319 so we don't worry about it so long as our computations on
4320 the bounds don't overflow. For unsigned, overflow is defined
4321 and this is exactly the right thing. */
4322 n_low
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4323 arg0_type
, low
, 0, arg1
, 0);
4324 n_high
= range_binop (code
== MINUS_EXPR
? PLUS_EXPR
: MINUS_EXPR
,
4325 arg0_type
, high
, 1, arg1
, 0);
4326 if ((n_low
!= 0 && TREE_OVERFLOW (n_low
))
4327 || (n_high
!= 0 && TREE_OVERFLOW (n_high
)))
4330 if (TYPE_OVERFLOW_UNDEFINED (arg0_type
))
4331 *strict_overflow_p
= true;
4334 /* Check for an unsigned range which has wrapped around the maximum
4335 value thus making n_high < n_low, and normalize it. */
4336 if (n_low
&& n_high
&& tree_int_cst_lt (n_high
, n_low
))
4338 low
= range_binop (PLUS_EXPR
, arg0_type
, n_high
, 0,
4339 build_int_cst (TREE_TYPE (n_high
), 1), 0);
4340 high
= range_binop (MINUS_EXPR
, arg0_type
, n_low
, 0,
4341 build_int_cst (TREE_TYPE (n_low
), 1), 0);
4343 /* If the range is of the form +/- [ x+1, x ], we won't
4344 be able to normalize it. But then, it represents the
4345 whole range or the empty set, so make it
4347 if (tree_int_cst_equal (n_low
, low
)
4348 && tree_int_cst_equal (n_high
, high
))
4354 low
= n_low
, high
= n_high
;
4362 case NON_LVALUE_EXPR
:
4363 if (TYPE_PRECISION (arg0_type
) > TYPE_PRECISION (exp_type
))
4366 if (! INTEGRAL_TYPE_P (arg0_type
)
4367 || (low
!= 0 && ! int_fits_type_p (low
, arg0_type
))
4368 || (high
!= 0 && ! int_fits_type_p (high
, arg0_type
)))
4371 n_low
= low
, n_high
= high
;
4374 n_low
= fold_convert_loc (loc
, arg0_type
, n_low
);
4377 n_high
= fold_convert_loc (loc
, arg0_type
, n_high
);
4379 /* If we're converting arg0 from an unsigned type, to exp,
4380 a signed type, we will be doing the comparison as unsigned.
4381 The tests above have already verified that LOW and HIGH
4384 So we have to ensure that we will handle large unsigned
4385 values the same way that the current signed bounds treat
4388 if (!TYPE_UNSIGNED (exp_type
) && TYPE_UNSIGNED (arg0_type
))
4392 /* For fixed-point modes, we need to pass the saturating flag
4393 as the 2nd parameter. */
4394 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type
)))
4396 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
),
4397 TYPE_SATURATING (arg0_type
));
4400 = lang_hooks
.types
.type_for_mode (TYPE_MODE (arg0_type
), 1);
4402 /* A range without an upper bound is, naturally, unbounded.
4403 Since convert would have cropped a very large value, use
4404 the max value for the destination type. */
4406 = TYPE_MAX_VALUE (equiv_type
) ? TYPE_MAX_VALUE (equiv_type
)
4407 : TYPE_MAX_VALUE (arg0_type
);
4409 if (TYPE_PRECISION (exp_type
) == TYPE_PRECISION (arg0_type
))
4410 high_positive
= fold_build2_loc (loc
, RSHIFT_EXPR
, arg0_type
,
4411 fold_convert_loc (loc
, arg0_type
,
4413 build_int_cst (arg0_type
, 1));
4415 /* If the low bound is specified, "and" the range with the
4416 range for which the original unsigned value will be
4420 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 1, n_low
, n_high
,
4421 1, fold_convert_loc (loc
, arg0_type
,
4426 in_p
= (n_in_p
== in_p
);
4430 /* Otherwise, "or" the range with the range of the input
4431 that will be interpreted as negative. */
4432 if (! merge_ranges (&n_in_p
, &n_low
, &n_high
, 0, n_low
, n_high
,
4433 1, fold_convert_loc (loc
, arg0_type
,
4438 in_p
= (in_p
!= n_in_p
);
4452 /* Given EXP, a logical expression, set the range it is testing into
4453 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4454 actually being tested. *PLOW and *PHIGH will be made of the same
4455 type as the returned expression. If EXP is not a comparison, we
4456 will most likely not be returning a useful value and range. Set
4457 *STRICT_OVERFLOW_P to true if the return value is only valid
4458 because signed overflow is undefined; otherwise, do not change
4459 *STRICT_OVERFLOW_P. */
4462 make_range (tree exp
, int *pin_p
, tree
*plow
, tree
*phigh
,
4463 bool *strict_overflow_p
)
4465 enum tree_code code
;
4466 tree arg0
, arg1
= NULL_TREE
;
4467 tree exp_type
, nexp
;
4470 location_t loc
= EXPR_LOCATION (exp
);
4472 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4473 and see if we can refine the range. Some of the cases below may not
4474 happen, but it doesn't seem worth worrying about this. We "continue"
4475 the outer loop when we've changed something; otherwise we "break"
4476 the switch, which will "break" the while. */
4479 low
= high
= build_int_cst (TREE_TYPE (exp
), 0);
4483 code
= TREE_CODE (exp
);
4484 exp_type
= TREE_TYPE (exp
);
4487 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code
)))
4489 if (TREE_OPERAND_LENGTH (exp
) > 0)
4490 arg0
= TREE_OPERAND (exp
, 0);
4491 if (TREE_CODE_CLASS (code
) == tcc_binary
4492 || TREE_CODE_CLASS (code
) == tcc_comparison
4493 || (TREE_CODE_CLASS (code
) == tcc_expression
4494 && TREE_OPERAND_LENGTH (exp
) > 1))
4495 arg1
= TREE_OPERAND (exp
, 1);
4497 if (arg0
== NULL_TREE
)
4500 nexp
= make_range_step (loc
, code
, arg0
, arg1
, exp_type
, &low
,
4501 &high
, &in_p
, strict_overflow_p
);
4502 if (nexp
== NULL_TREE
)
4507 /* If EXP is a constant, we can evaluate whether this is true or false. */
4508 if (TREE_CODE (exp
) == INTEGER_CST
)
4510 in_p
= in_p
== (integer_onep (range_binop (GE_EXPR
, integer_type_node
,
4512 && integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4518 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
4522 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4523 type, TYPE, return an expression to test if EXP is in (or out of, depending
4524 on IN_P) the range. Return 0 if the test couldn't be created. */
4527 build_range_check (location_t loc
, tree type
, tree exp
, int in_p
,
4528 tree low
, tree high
)
4530 tree etype
= TREE_TYPE (exp
), value
;
4532 /* Disable this optimization for function pointer expressions
4533 on targets that require function pointer canonicalization. */
4534 if (targetm
.have_canonicalize_funcptr_for_compare ()
4535 && TREE_CODE (etype
) == POINTER_TYPE
4536 && TREE_CODE (TREE_TYPE (etype
)) == FUNCTION_TYPE
)
4541 value
= build_range_check (loc
, type
, exp
, 1, low
, high
);
4543 return invert_truthvalue_loc (loc
, value
);
4548 if (low
== 0 && high
== 0)
4549 return omit_one_operand_loc (loc
, type
, build_int_cst (type
, 1), exp
);
4552 return fold_build2_loc (loc
, LE_EXPR
, type
, exp
,
4553 fold_convert_loc (loc
, etype
, high
));
4556 return fold_build2_loc (loc
, GE_EXPR
, type
, exp
,
4557 fold_convert_loc (loc
, etype
, low
));
4559 if (operand_equal_p (low
, high
, 0))
4560 return fold_build2_loc (loc
, EQ_EXPR
, type
, exp
,
4561 fold_convert_loc (loc
, etype
, low
));
4563 if (integer_zerop (low
))
4565 if (! TYPE_UNSIGNED (etype
))
4567 etype
= unsigned_type_for (etype
);
4568 high
= fold_convert_loc (loc
, etype
, high
);
4569 exp
= fold_convert_loc (loc
, etype
, exp
);
4571 return build_range_check (loc
, type
, exp
, 1, 0, high
);
4574 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4575 if (integer_onep (low
) && TREE_CODE (high
) == INTEGER_CST
)
4577 int prec
= TYPE_PRECISION (etype
);
4579 if (wi::mask (prec
- 1, false, prec
) == high
)
4581 if (TYPE_UNSIGNED (etype
))
4583 tree signed_etype
= signed_type_for (etype
);
4584 if (TYPE_PRECISION (signed_etype
) != TYPE_PRECISION (etype
))
4586 = build_nonstandard_integer_type (TYPE_PRECISION (etype
), 0);
4588 etype
= signed_etype
;
4589 exp
= fold_convert_loc (loc
, etype
, exp
);
4591 return fold_build2_loc (loc
, GT_EXPR
, type
, exp
,
4592 build_int_cst (etype
, 0));
4596 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4597 This requires wrap-around arithmetics for the type of the expression.
4598 First make sure that arithmetics in this type is valid, then make sure
4599 that it wraps around. */
4600 if (TREE_CODE (etype
) == ENUMERAL_TYPE
|| TREE_CODE (etype
) == BOOLEAN_TYPE
)
4601 etype
= lang_hooks
.types
.type_for_size (TYPE_PRECISION (etype
),
4602 TYPE_UNSIGNED (etype
));
4604 if (TREE_CODE (etype
) == INTEGER_TYPE
&& !TYPE_OVERFLOW_WRAPS (etype
))
4606 tree utype
, minv
, maxv
;
4608 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4609 for the type in question, as we rely on this here. */
4610 utype
= unsigned_type_for (etype
);
4611 maxv
= fold_convert_loc (loc
, utype
, TYPE_MAX_VALUE (etype
));
4612 maxv
= range_binop (PLUS_EXPR
, NULL_TREE
, maxv
, 1,
4613 build_int_cst (TREE_TYPE (maxv
), 1), 1);
4614 minv
= fold_convert_loc (loc
, utype
, TYPE_MIN_VALUE (etype
));
4616 if (integer_zerop (range_binop (NE_EXPR
, integer_type_node
,
4623 high
= fold_convert_loc (loc
, etype
, high
);
4624 low
= fold_convert_loc (loc
, etype
, low
);
4625 exp
= fold_convert_loc (loc
, etype
, exp
);
4627 value
= const_binop (MINUS_EXPR
, high
, low
);
4630 if (POINTER_TYPE_P (etype
))
4632 if (value
!= 0 && !TREE_OVERFLOW (value
))
4634 low
= fold_build1_loc (loc
, NEGATE_EXPR
, TREE_TYPE (low
), low
);
4635 return build_range_check (loc
, type
,
4636 fold_build_pointer_plus_loc (loc
, exp
, low
),
4637 1, build_int_cst (etype
, 0), value
);
4642 if (value
!= 0 && !TREE_OVERFLOW (value
))
4643 return build_range_check (loc
, type
,
4644 fold_build2_loc (loc
, MINUS_EXPR
, etype
, exp
, low
),
4645 1, build_int_cst (etype
, 0), value
);
4650 /* Return the predecessor of VAL in its type, handling the infinite case. */
4653 range_predecessor (tree val
)
4655 tree type
= TREE_TYPE (val
);
4657 if (INTEGRAL_TYPE_P (type
)
4658 && operand_equal_p (val
, TYPE_MIN_VALUE (type
), 0))
4661 return range_binop (MINUS_EXPR
, NULL_TREE
, val
, 0,
4662 build_int_cst (TREE_TYPE (val
), 1), 0);
4665 /* Return the successor of VAL in its type, handling the infinite case. */
4668 range_successor (tree val
)
4670 tree type
= TREE_TYPE (val
);
4672 if (INTEGRAL_TYPE_P (type
)
4673 && operand_equal_p (val
, TYPE_MAX_VALUE (type
), 0))
4676 return range_binop (PLUS_EXPR
, NULL_TREE
, val
, 0,
4677 build_int_cst (TREE_TYPE (val
), 1), 0);
4680 /* Given two ranges, see if we can merge them into one. Return 1 if we
4681 can, 0 if we can't. Set the output range into the specified parameters. */
4684 merge_ranges (int *pin_p
, tree
*plow
, tree
*phigh
, int in0_p
, tree low0
,
4685 tree high0
, int in1_p
, tree low1
, tree high1
)
4693 int lowequal
= ((low0
== 0 && low1
== 0)
4694 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4695 low0
, 0, low1
, 0)));
4696 int highequal
= ((high0
== 0 && high1
== 0)
4697 || integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4698 high0
, 1, high1
, 1)));
4700 /* Make range 0 be the range that starts first, or ends last if they
4701 start at the same value. Swap them if it isn't. */
4702 if (integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4705 && integer_onep (range_binop (GT_EXPR
, integer_type_node
,
4706 high1
, 1, high0
, 1))))
4708 temp
= in0_p
, in0_p
= in1_p
, in1_p
= temp
;
4709 tem
= low0
, low0
= low1
, low1
= tem
;
4710 tem
= high0
, high0
= high1
, high1
= tem
;
4713 /* Now flag two cases, whether the ranges are disjoint or whether the
4714 second range is totally subsumed in the first. Note that the tests
4715 below are simplified by the ones above. */
4716 no_overlap
= integer_onep (range_binop (LT_EXPR
, integer_type_node
,
4717 high0
, 1, low1
, 0));
4718 subset
= integer_onep (range_binop (LE_EXPR
, integer_type_node
,
4719 high1
, 1, high0
, 1));
4721 /* We now have four cases, depending on whether we are including or
4722 excluding the two ranges. */
4725 /* If they don't overlap, the result is false. If the second range
4726 is a subset it is the result. Otherwise, the range is from the start
4727 of the second to the end of the first. */
4729 in_p
= 0, low
= high
= 0;
4731 in_p
= 1, low
= low1
, high
= high1
;
4733 in_p
= 1, low
= low1
, high
= high0
;
4736 else if (in0_p
&& ! in1_p
)
4738 /* If they don't overlap, the result is the first range. If they are
4739 equal, the result is false. If the second range is a subset of the
4740 first, and the ranges begin at the same place, we go from just after
4741 the end of the second range to the end of the first. If the second
4742 range is not a subset of the first, or if it is a subset and both
4743 ranges end at the same place, the range starts at the start of the
4744 first range and ends just before the second range.
4745 Otherwise, we can't describe this as a single range. */
4747 in_p
= 1, low
= low0
, high
= high0
;
4748 else if (lowequal
&& highequal
)
4749 in_p
= 0, low
= high
= 0;
4750 else if (subset
&& lowequal
)
4752 low
= range_successor (high1
);
4757 /* We are in the weird situation where high0 > high1 but
4758 high1 has no successor. Punt. */
4762 else if (! subset
|| highequal
)
4765 high
= range_predecessor (low1
);
4769 /* low0 < low1 but low1 has no predecessor. Punt. */
4777 else if (! in0_p
&& in1_p
)
4779 /* If they don't overlap, the result is the second range. If the second
4780 is a subset of the first, the result is false. Otherwise,
4781 the range starts just after the first range and ends at the
4782 end of the second. */
4784 in_p
= 1, low
= low1
, high
= high1
;
4785 else if (subset
|| highequal
)
4786 in_p
= 0, low
= high
= 0;
4789 low
= range_successor (high0
);
4794 /* high1 > high0 but high0 has no successor. Punt. */
4802 /* The case where we are excluding both ranges. Here the complex case
4803 is if they don't overlap. In that case, the only time we have a
4804 range is if they are adjacent. If the second is a subset of the
4805 first, the result is the first. Otherwise, the range to exclude
4806 starts at the beginning of the first range and ends at the end of the
4810 if (integer_onep (range_binop (EQ_EXPR
, integer_type_node
,
4811 range_successor (high0
),
4813 in_p
= 0, low
= low0
, high
= high1
;
4816 /* Canonicalize - [min, x] into - [-, x]. */
4817 if (low0
&& TREE_CODE (low0
) == INTEGER_CST
)
4818 switch (TREE_CODE (TREE_TYPE (low0
)))
4821 if (TYPE_PRECISION (TREE_TYPE (low0
))
4822 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0
))))
4826 if (tree_int_cst_equal (low0
,
4827 TYPE_MIN_VALUE (TREE_TYPE (low0
))))
4831 if (TYPE_UNSIGNED (TREE_TYPE (low0
))
4832 && integer_zerop (low0
))
4839 /* Canonicalize - [x, max] into - [x, -]. */
4840 if (high1
&& TREE_CODE (high1
) == INTEGER_CST
)
4841 switch (TREE_CODE (TREE_TYPE (high1
)))
4844 if (TYPE_PRECISION (TREE_TYPE (high1
))
4845 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1
))))
4849 if (tree_int_cst_equal (high1
,
4850 TYPE_MAX_VALUE (TREE_TYPE (high1
))))
4854 if (TYPE_UNSIGNED (TREE_TYPE (high1
))
4855 && integer_zerop (range_binop (PLUS_EXPR
, NULL_TREE
,
4857 build_int_cst (TREE_TYPE (high1
), 1),
4865 /* The ranges might be also adjacent between the maximum and
4866 minimum values of the given type. For
4867 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4868 return + [x + 1, y - 1]. */
4869 if (low0
== 0 && high1
== 0)
4871 low
= range_successor (high0
);
4872 high
= range_predecessor (low1
);
4873 if (low
== 0 || high
== 0)
4883 in_p
= 0, low
= low0
, high
= high0
;
4885 in_p
= 0, low
= low0
, high
= high1
;
4888 *pin_p
= in_p
, *plow
= low
, *phigh
= high
;
4893 /* Subroutine of fold, looking inside expressions of the form
4894 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4895 of the COND_EXPR. This function is being used also to optimize
4896 A op B ? C : A, by reversing the comparison first.
4898 Return a folded expression whose code is not a COND_EXPR
4899 anymore, or NULL_TREE if no folding opportunity is found. */
4902 fold_cond_expr_with_comparison (location_t loc
, tree type
,
4903 tree arg0
, tree arg1
, tree arg2
)
4905 enum tree_code comp_code
= TREE_CODE (arg0
);
4906 tree arg00
= TREE_OPERAND (arg0
, 0);
4907 tree arg01
= TREE_OPERAND (arg0
, 1);
4908 tree arg1_type
= TREE_TYPE (arg1
);
4914 /* If we have A op 0 ? A : -A, consider applying the following
4917 A == 0? A : -A same as -A
4918 A != 0? A : -A same as A
4919 A >= 0? A : -A same as abs (A)
4920 A > 0? A : -A same as abs (A)
4921 A <= 0? A : -A same as -abs (A)
4922 A < 0? A : -A same as -abs (A)
4924 None of these transformations work for modes with signed
4925 zeros. If A is +/-0, the first two transformations will
4926 change the sign of the result (from +0 to -0, or vice
4927 versa). The last four will fix the sign of the result,
4928 even though the original expressions could be positive or
4929 negative, depending on the sign of A.
4931 Note that all these transformations are correct if A is
4932 NaN, since the two alternatives (A and -A) are also NaNs. */
4933 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
4934 && (FLOAT_TYPE_P (TREE_TYPE (arg01
))
4935 ? real_zerop (arg01
)
4936 : integer_zerop (arg01
))
4937 && ((TREE_CODE (arg2
) == NEGATE_EXPR
4938 && operand_equal_p (TREE_OPERAND (arg2
, 0), arg1
, 0))
4939 /* In the case that A is of the form X-Y, '-A' (arg2) may
4940 have already been folded to Y-X, check for that. */
4941 || (TREE_CODE (arg1
) == MINUS_EXPR
4942 && TREE_CODE (arg2
) == MINUS_EXPR
4943 && operand_equal_p (TREE_OPERAND (arg1
, 0),
4944 TREE_OPERAND (arg2
, 1), 0)
4945 && operand_equal_p (TREE_OPERAND (arg1
, 1),
4946 TREE_OPERAND (arg2
, 0), 0))))
4951 tem
= fold_convert_loc (loc
, arg1_type
, arg1
);
4952 return pedantic_non_lvalue_loc (loc
,
4953 fold_convert_loc (loc
, type
,
4954 negate_expr (tem
)));
4957 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
4960 if (flag_trapping_math
)
4965 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
4966 arg1
= fold_convert_loc (loc
, signed_type_for
4967 (TREE_TYPE (arg1
)), arg1
);
4968 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
4969 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
4972 if (flag_trapping_math
)
4976 if (TYPE_UNSIGNED (TREE_TYPE (arg1
)))
4977 arg1
= fold_convert_loc (loc
, signed_type_for
4978 (TREE_TYPE (arg1
)), arg1
);
4979 tem
= fold_build1_loc (loc
, ABS_EXPR
, TREE_TYPE (arg1
), arg1
);
4980 return negate_expr (fold_convert_loc (loc
, type
, tem
));
4982 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
4986 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4987 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4988 both transformations are correct when A is NaN: A != 0
4989 is then true, and A == 0 is false. */
4991 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
4992 && integer_zerop (arg01
) && integer_zerop (arg2
))
4994 if (comp_code
== NE_EXPR
)
4995 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
4996 else if (comp_code
== EQ_EXPR
)
4997 return build_zero_cst (type
);
5000 /* Try some transformations of A op B ? A : B.
5002 A == B? A : B same as B
5003 A != B? A : B same as A
5004 A >= B? A : B same as max (A, B)
5005 A > B? A : B same as max (B, A)
5006 A <= B? A : B same as min (A, B)
5007 A < B? A : B same as min (B, A)
5009 As above, these transformations don't work in the presence
5010 of signed zeros. For example, if A and B are zeros of
5011 opposite sign, the first two transformations will change
5012 the sign of the result. In the last four, the original
5013 expressions give different results for (A=+0, B=-0) and
5014 (A=-0, B=+0), but the transformed expressions do not.
5016 The first two transformations are correct if either A or B
5017 is a NaN. In the first transformation, the condition will
5018 be false, and B will indeed be chosen. In the case of the
5019 second transformation, the condition A != B will be true,
5020 and A will be chosen.
5022 The conversions to max() and min() are not correct if B is
5023 a number and A is not. The conditions in the original
5024 expressions will be false, so all four give B. The min()
5025 and max() versions would give a NaN instead. */
5026 if (!HONOR_SIGNED_ZEROS (element_mode (type
))
5027 && operand_equal_for_comparison_p (arg01
, arg2
, arg00
)
5028 /* Avoid these transformations if the COND_EXPR may be used
5029 as an lvalue in the C++ front-end. PR c++/19199. */
5031 || VECTOR_TYPE_P (type
)
5032 || (! lang_GNU_CXX ()
5033 && strcmp (lang_hooks
.name
, "GNU Objective-C++") != 0)
5034 || ! maybe_lvalue_p (arg1
)
5035 || ! maybe_lvalue_p (arg2
)))
5037 tree comp_op0
= arg00
;
5038 tree comp_op1
= arg01
;
5039 tree comp_type
= TREE_TYPE (comp_op0
);
5041 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5042 if (TYPE_MAIN_VARIANT (comp_type
) == TYPE_MAIN_VARIANT (type
))
5052 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg2
));
5054 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
5059 /* In C++ a ?: expression can be an lvalue, so put the
5060 operand which will be used if they are equal first
5061 so that we can convert this back to the
5062 corresponding COND_EXPR. */
5063 if (!HONOR_NANS (arg1
))
5065 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5066 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5067 tem
= (comp_code
== LE_EXPR
|| comp_code
== UNLE_EXPR
)
5068 ? fold_build2_loc (loc
, MIN_EXPR
, comp_type
, comp_op0
, comp_op1
)
5069 : fold_build2_loc (loc
, MIN_EXPR
, comp_type
,
5070 comp_op1
, comp_op0
);
5071 return pedantic_non_lvalue_loc (loc
,
5072 fold_convert_loc (loc
, type
, tem
));
5079 if (!HONOR_NANS (arg1
))
5081 comp_op0
= fold_convert_loc (loc
, comp_type
, comp_op0
);
5082 comp_op1
= fold_convert_loc (loc
, comp_type
, comp_op1
);
5083 tem
= (comp_code
== GE_EXPR
|| comp_code
== UNGE_EXPR
)
5084 ? fold_build2_loc (loc
, MAX_EXPR
, comp_type
, comp_op0
, comp_op1
)
5085 : fold_build2_loc (loc
, MAX_EXPR
, comp_type
,
5086 comp_op1
, comp_op0
);
5087 return pedantic_non_lvalue_loc (loc
,
5088 fold_convert_loc (loc
, type
, tem
));
5092 if (!HONOR_NANS (arg1
))
5093 return pedantic_non_lvalue_loc (loc
,
5094 fold_convert_loc (loc
, type
, arg2
));
5097 if (!HONOR_NANS (arg1
))
5098 return pedantic_non_lvalue_loc (loc
,
5099 fold_convert_loc (loc
, type
, arg1
));
5102 gcc_assert (TREE_CODE_CLASS (comp_code
) == tcc_comparison
);
5107 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5108 we might still be able to simplify this. For example,
5109 if C1 is one less or one more than C2, this might have started
5110 out as a MIN or MAX and been transformed by this function.
5111 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5113 if (INTEGRAL_TYPE_P (type
)
5114 && TREE_CODE (arg01
) == INTEGER_CST
5115 && TREE_CODE (arg2
) == INTEGER_CST
)
5119 if (TREE_CODE (arg1
) == INTEGER_CST
)
5121 /* We can replace A with C1 in this case. */
5122 arg1
= fold_convert_loc (loc
, type
, arg01
);
5123 return fold_build3_loc (loc
, COND_EXPR
, type
, arg0
, arg1
, arg2
);
5126 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5127 MIN_EXPR, to preserve the signedness of the comparison. */
5128 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
),
5130 && operand_equal_p (arg01
,
5131 const_binop (PLUS_EXPR
, arg2
,
5132 build_int_cst (type
, 1)),
5135 tem
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (arg00
), arg00
,
5136 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5138 return pedantic_non_lvalue_loc (loc
,
5139 fold_convert_loc (loc
, type
, tem
));
5144 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5146 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
),
5148 && operand_equal_p (arg01
,
5149 const_binop (MINUS_EXPR
, arg2
,
5150 build_int_cst (type
, 1)),
5153 tem
= fold_build2_loc (loc
, MIN_EXPR
, TREE_TYPE (arg00
), arg00
,
5154 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5156 return pedantic_non_lvalue_loc (loc
,
5157 fold_convert_loc (loc
, type
, tem
));
5162 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5163 MAX_EXPR, to preserve the signedness of the comparison. */
5164 if (! operand_equal_p (arg2
, TYPE_MIN_VALUE (type
),
5166 && operand_equal_p (arg01
,
5167 const_binop (MINUS_EXPR
, arg2
,
5168 build_int_cst (type
, 1)),
5171 tem
= fold_build2_loc (loc
, MAX_EXPR
, TREE_TYPE (arg00
), arg00
,
5172 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5174 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
5179 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5180 if (! operand_equal_p (arg2
, TYPE_MAX_VALUE (type
),
5182 && operand_equal_p (arg01
,
5183 const_binop (PLUS_EXPR
, arg2
,
5184 build_int_cst (type
, 1)),
5187 tem
= fold_build2_loc (loc
, MAX_EXPR
, TREE_TYPE (arg00
), arg00
,
5188 fold_convert_loc (loc
, TREE_TYPE (arg00
),
5190 return pedantic_non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
5204 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5205 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5206 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5210 /* EXP is some logical combination of boolean tests. See if we can
5211 merge it into some range test. Return the new tree if so. */
5214 fold_range_test (location_t loc
, enum tree_code code
, tree type
,
5217 int or_op
= (code
== TRUTH_ORIF_EXPR
5218 || code
== TRUTH_OR_EXPR
);
5219 int in0_p
, in1_p
, in_p
;
5220 tree low0
, low1
, low
, high0
, high1
, high
;
5221 bool strict_overflow_p
= false;
5223 const char * const warnmsg
= G_("assuming signed overflow does not occur "
5224 "when simplifying range test");
5226 if (!INTEGRAL_TYPE_P (type
))
5229 lhs
= make_range (op0
, &in0_p
, &low0
, &high0
, &strict_overflow_p
);
5230 rhs
= make_range (op1
, &in1_p
, &low1
, &high1
, &strict_overflow_p
);
5232 /* If this is an OR operation, invert both sides; we will invert
5233 again at the end. */
5235 in0_p
= ! in0_p
, in1_p
= ! in1_p
;
5237 /* If both expressions are the same, if we can merge the ranges, and we
5238 can build the range test, return it or it inverted. If one of the
5239 ranges is always true or always false, consider it to be the same
5240 expression as the other. */
5241 if ((lhs
== 0 || rhs
== 0 || operand_equal_p (lhs
, rhs
, 0))
5242 && merge_ranges (&in_p
, &low
, &high
, in0_p
, low0
, high0
,
5244 && 0 != (tem
= (build_range_check (loc
, type
,
5246 : rhs
!= 0 ? rhs
: integer_zero_node
,
5249 if (strict_overflow_p
)
5250 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
5251 return or_op
? invert_truthvalue_loc (loc
, tem
) : tem
;
5254 /* On machines where the branch cost is expensive, if this is a
5255 short-circuited branch and the underlying object on both sides
5256 is the same, make a non-short-circuit operation. */
5257 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5258 && lhs
!= 0 && rhs
!= 0
5259 && (code
== TRUTH_ANDIF_EXPR
5260 || code
== TRUTH_ORIF_EXPR
)
5261 && operand_equal_p (lhs
, rhs
, 0))
5263 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5264 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5265 which cases we can't do this. */
5266 if (simple_operand_p (lhs
))
5267 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5268 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5271 else if (!lang_hooks
.decls
.global_bindings_p ()
5272 && !CONTAINS_PLACEHOLDER_P (lhs
))
5274 tree common
= save_expr (lhs
);
5276 if (0 != (lhs
= build_range_check (loc
, type
, common
,
5277 or_op
? ! in0_p
: in0_p
,
5279 && (0 != (rhs
= build_range_check (loc
, type
, common
,
5280 or_op
? ! in1_p
: in1_p
,
5283 if (strict_overflow_p
)
5284 fold_overflow_warning (warnmsg
,
5285 WARN_STRICT_OVERFLOW_COMPARISON
);
5286 return build2_loc (loc
, code
== TRUTH_ANDIF_EXPR
5287 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
,
5296 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5297 bit value. Arrange things so the extra bits will be set to zero if and
5298 only if C is signed-extended to its full width. If MASK is nonzero,
5299 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5302 unextend (tree c
, int p
, int unsignedp
, tree mask
)
5304 tree type
= TREE_TYPE (c
);
5305 int modesize
= GET_MODE_BITSIZE (TYPE_MODE (type
));
5308 if (p
== modesize
|| unsignedp
)
5311 /* We work by getting just the sign bit into the low-order bit, then
5312 into the high-order bit, then sign-extend. We then XOR that value
5314 temp
= build_int_cst (TREE_TYPE (c
), wi::extract_uhwi (c
, p
- 1, 1));
5316 /* We must use a signed type in order to get an arithmetic right shift.
5317 However, we must also avoid introducing accidental overflows, so that
5318 a subsequent call to integer_zerop will work. Hence we must
5319 do the type conversion here. At this point, the constant is either
5320 zero or one, and the conversion to a signed type can never overflow.
5321 We could get an overflow if this conversion is done anywhere else. */
5322 if (TYPE_UNSIGNED (type
))
5323 temp
= fold_convert (signed_type_for (type
), temp
);
5325 temp
= const_binop (LSHIFT_EXPR
, temp
, size_int (modesize
- 1));
5326 temp
= const_binop (RSHIFT_EXPR
, temp
, size_int (modesize
- p
- 1));
5328 temp
= const_binop (BIT_AND_EXPR
, temp
,
5329 fold_convert (TREE_TYPE (c
), mask
));
5330 /* If necessary, convert the type back to match the type of C. */
5331 if (TYPE_UNSIGNED (type
))
5332 temp
= fold_convert (type
, temp
);
5334 return fold_convert (type
, const_binop (BIT_XOR_EXPR
, c
, temp
));
5337 /* For an expression that has the form
5341 we can drop one of the inner expressions and simplify to
5345 LOC is the location of the resulting expression. OP is the inner
5346 logical operation; the left-hand side in the examples above, while CMPOP
5347 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5348 removing a condition that guards another, as in
5349 (A != NULL && A->...) || A == NULL
5350 which we must not transform. If RHS_ONLY is true, only eliminate the
5351 right-most operand of the inner logical operation. */
5354 merge_truthop_with_opposite_arm (location_t loc
, tree op
, tree cmpop
,
5357 tree type
= TREE_TYPE (cmpop
);
5358 enum tree_code code
= TREE_CODE (cmpop
);
5359 enum tree_code truthop_code
= TREE_CODE (op
);
5360 tree lhs
= TREE_OPERAND (op
, 0);
5361 tree rhs
= TREE_OPERAND (op
, 1);
5362 tree orig_lhs
= lhs
, orig_rhs
= rhs
;
5363 enum tree_code rhs_code
= TREE_CODE (rhs
);
5364 enum tree_code lhs_code
= TREE_CODE (lhs
);
5365 enum tree_code inv_code
;
5367 if (TREE_SIDE_EFFECTS (op
) || TREE_SIDE_EFFECTS (cmpop
))
5370 if (TREE_CODE_CLASS (code
) != tcc_comparison
)
5373 if (rhs_code
== truthop_code
)
5375 tree newrhs
= merge_truthop_with_opposite_arm (loc
, rhs
, cmpop
, rhs_only
);
5376 if (newrhs
!= NULL_TREE
)
5379 rhs_code
= TREE_CODE (rhs
);
5382 if (lhs_code
== truthop_code
&& !rhs_only
)
5384 tree newlhs
= merge_truthop_with_opposite_arm (loc
, lhs
, cmpop
, false);
5385 if (newlhs
!= NULL_TREE
)
5388 lhs_code
= TREE_CODE (lhs
);
5392 inv_code
= invert_tree_comparison (code
, HONOR_NANS (type
));
5393 if (inv_code
== rhs_code
5394 && operand_equal_p (TREE_OPERAND (rhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5395 && operand_equal_p (TREE_OPERAND (rhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5397 if (!rhs_only
&& inv_code
== lhs_code
5398 && operand_equal_p (TREE_OPERAND (lhs
, 0), TREE_OPERAND (cmpop
, 0), 0)
5399 && operand_equal_p (TREE_OPERAND (lhs
, 1), TREE_OPERAND (cmpop
, 1), 0))
5401 if (rhs
!= orig_rhs
|| lhs
!= orig_lhs
)
5402 return fold_build2_loc (loc
, truthop_code
, TREE_TYPE (cmpop
),
5407 /* Find ways of folding logical expressions of LHS and RHS:
5408 Try to merge two comparisons to the same innermost item.
5409 Look for range tests like "ch >= '0' && ch <= '9'".
5410 Look for combinations of simple terms on machines with expensive branches
5411 and evaluate the RHS unconditionally.
5413 For example, if we have p->a == 2 && p->b == 4 and we can make an
5414 object large enough to span both A and B, we can do this with a comparison
5415 against the object ANDed with the a mask.
5417 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5418 operations to do this with one comparison.
5420 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5421 function and the one above.
5423 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5424 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5426 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5429 We return the simplified tree or 0 if no optimization is possible. */
5432 fold_truth_andor_1 (location_t loc
, enum tree_code code
, tree truth_type
,
5435 /* If this is the "or" of two comparisons, we can do something if
5436 the comparisons are NE_EXPR. If this is the "and", we can do something
5437 if the comparisons are EQ_EXPR. I.e.,
5438 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5440 WANTED_CODE is this operation code. For single bit fields, we can
5441 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5442 comparison for one-bit fields. */
5444 enum tree_code wanted_code
;
5445 enum tree_code lcode
, rcode
;
5446 tree ll_arg
, lr_arg
, rl_arg
, rr_arg
;
5447 tree ll_inner
, lr_inner
, rl_inner
, rr_inner
;
5448 HOST_WIDE_INT ll_bitsize
, ll_bitpos
, lr_bitsize
, lr_bitpos
;
5449 HOST_WIDE_INT rl_bitsize
, rl_bitpos
, rr_bitsize
, rr_bitpos
;
5450 HOST_WIDE_INT xll_bitpos
, xlr_bitpos
, xrl_bitpos
, xrr_bitpos
;
5451 HOST_WIDE_INT lnbitsize
, lnbitpos
, rnbitsize
, rnbitpos
;
5452 int ll_unsignedp
, lr_unsignedp
, rl_unsignedp
, rr_unsignedp
;
5453 machine_mode ll_mode
, lr_mode
, rl_mode
, rr_mode
;
5454 machine_mode lnmode
, rnmode
;
5455 tree ll_mask
, lr_mask
, rl_mask
, rr_mask
;
5456 tree ll_and_mask
, lr_and_mask
, rl_and_mask
, rr_and_mask
;
5457 tree l_const
, r_const
;
5458 tree lntype
, rntype
, result
;
5459 HOST_WIDE_INT first_bit
, end_bit
;
5462 /* Start by getting the comparison codes. Fail if anything is volatile.
5463 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5464 it were surrounded with a NE_EXPR. */
5466 if (TREE_SIDE_EFFECTS (lhs
) || TREE_SIDE_EFFECTS (rhs
))
5469 lcode
= TREE_CODE (lhs
);
5470 rcode
= TREE_CODE (rhs
);
5472 if (lcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (lhs
, 1)))
5474 lhs
= build2 (NE_EXPR
, truth_type
, lhs
,
5475 build_int_cst (TREE_TYPE (lhs
), 0));
5479 if (rcode
== BIT_AND_EXPR
&& integer_onep (TREE_OPERAND (rhs
, 1)))
5481 rhs
= build2 (NE_EXPR
, truth_type
, rhs
,
5482 build_int_cst (TREE_TYPE (rhs
), 0));
5486 if (TREE_CODE_CLASS (lcode
) != tcc_comparison
5487 || TREE_CODE_CLASS (rcode
) != tcc_comparison
)
5490 ll_arg
= TREE_OPERAND (lhs
, 0);
5491 lr_arg
= TREE_OPERAND (lhs
, 1);
5492 rl_arg
= TREE_OPERAND (rhs
, 0);
5493 rr_arg
= TREE_OPERAND (rhs
, 1);
5495 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5496 if (simple_operand_p (ll_arg
)
5497 && simple_operand_p (lr_arg
))
5499 if (operand_equal_p (ll_arg
, rl_arg
, 0)
5500 && operand_equal_p (lr_arg
, rr_arg
, 0))
5502 result
= combine_comparisons (loc
, code
, lcode
, rcode
,
5503 truth_type
, ll_arg
, lr_arg
);
5507 else if (operand_equal_p (ll_arg
, rr_arg
, 0)
5508 && operand_equal_p (lr_arg
, rl_arg
, 0))
5510 result
= combine_comparisons (loc
, code
, lcode
,
5511 swap_tree_comparison (rcode
),
5512 truth_type
, ll_arg
, lr_arg
);
5518 code
= ((code
== TRUTH_AND_EXPR
|| code
== TRUTH_ANDIF_EXPR
)
5519 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
);
5521 /* If the RHS can be evaluated unconditionally and its operands are
5522 simple, it wins to evaluate the RHS unconditionally on machines
5523 with expensive branches. In this case, this isn't a comparison
5524 that can be merged. */
5526 if (BRANCH_COST (optimize_function_for_speed_p (cfun
),
5528 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg
))
5529 && simple_operand_p (rl_arg
)
5530 && simple_operand_p (rr_arg
))
5532 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5533 if (code
== TRUTH_OR_EXPR
5534 && lcode
== NE_EXPR
&& integer_zerop (lr_arg
)
5535 && rcode
== NE_EXPR
&& integer_zerop (rr_arg
)
5536 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5537 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5538 return build2_loc (loc
, NE_EXPR
, truth_type
,
5539 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5541 build_int_cst (TREE_TYPE (ll_arg
), 0));
5543 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5544 if (code
== TRUTH_AND_EXPR
5545 && lcode
== EQ_EXPR
&& integer_zerop (lr_arg
)
5546 && rcode
== EQ_EXPR
&& integer_zerop (rr_arg
)
5547 && TREE_TYPE (ll_arg
) == TREE_TYPE (rl_arg
)
5548 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg
)))
5549 return build2_loc (loc
, EQ_EXPR
, truth_type
,
5550 build2 (BIT_IOR_EXPR
, TREE_TYPE (ll_arg
),
5552 build_int_cst (TREE_TYPE (ll_arg
), 0));
5555 /* See if the comparisons can be merged. Then get all the parameters for
5558 if ((lcode
!= EQ_EXPR
&& lcode
!= NE_EXPR
)
5559 || (rcode
!= EQ_EXPR
&& rcode
!= NE_EXPR
))
5563 ll_inner
= decode_field_reference (loc
, ll_arg
,
5564 &ll_bitsize
, &ll_bitpos
, &ll_mode
,
5565 &ll_unsignedp
, &volatilep
, &ll_mask
,
5567 lr_inner
= decode_field_reference (loc
, lr_arg
,
5568 &lr_bitsize
, &lr_bitpos
, &lr_mode
,
5569 &lr_unsignedp
, &volatilep
, &lr_mask
,
5571 rl_inner
= decode_field_reference (loc
, rl_arg
,
5572 &rl_bitsize
, &rl_bitpos
, &rl_mode
,
5573 &rl_unsignedp
, &volatilep
, &rl_mask
,
5575 rr_inner
= decode_field_reference (loc
, rr_arg
,
5576 &rr_bitsize
, &rr_bitpos
, &rr_mode
,
5577 &rr_unsignedp
, &volatilep
, &rr_mask
,
5580 /* It must be true that the inner operation on the lhs of each
5581 comparison must be the same if we are to be able to do anything.
5582 Then see if we have constants. If not, the same must be true for
5584 if (volatilep
|| ll_inner
== 0 || rl_inner
== 0
5585 || ! operand_equal_p (ll_inner
, rl_inner
, 0))
5588 if (TREE_CODE (lr_arg
) == INTEGER_CST
5589 && TREE_CODE (rr_arg
) == INTEGER_CST
)
5590 l_const
= lr_arg
, r_const
= rr_arg
;
5591 else if (lr_inner
== 0 || rr_inner
== 0
5592 || ! operand_equal_p (lr_inner
, rr_inner
, 0))
5595 l_const
= r_const
= 0;
5597 /* If either comparison code is not correct for our logical operation,
5598 fail. However, we can convert a one-bit comparison against zero into
5599 the opposite comparison against that bit being set in the field. */
5601 wanted_code
= (code
== TRUTH_AND_EXPR
? EQ_EXPR
: NE_EXPR
);
5602 if (lcode
!= wanted_code
)
5604 if (l_const
&& integer_zerop (l_const
) && integer_pow2p (ll_mask
))
5606 /* Make the left operand unsigned, since we are only interested
5607 in the value of one bit. Otherwise we are doing the wrong
5616 /* This is analogous to the code for l_const above. */
5617 if (rcode
!= wanted_code
)
5619 if (r_const
&& integer_zerop (r_const
) && integer_pow2p (rl_mask
))
5628 /* See if we can find a mode that contains both fields being compared on
5629 the left. If we can't, fail. Otherwise, update all constants and masks
5630 to be relative to a field of that size. */
5631 first_bit
= MIN (ll_bitpos
, rl_bitpos
);
5632 end_bit
= MAX (ll_bitpos
+ ll_bitsize
, rl_bitpos
+ rl_bitsize
);
5633 lnmode
= get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5634 TYPE_ALIGN (TREE_TYPE (ll_inner
)), word_mode
,
5636 if (lnmode
== VOIDmode
)
5639 lnbitsize
= GET_MODE_BITSIZE (lnmode
);
5640 lnbitpos
= first_bit
& ~ (lnbitsize
- 1);
5641 lntype
= lang_hooks
.types
.type_for_size (lnbitsize
, 1);
5642 xll_bitpos
= ll_bitpos
- lnbitpos
, xrl_bitpos
= rl_bitpos
- lnbitpos
;
5644 if (BYTES_BIG_ENDIAN
)
5646 xll_bitpos
= lnbitsize
- xll_bitpos
- ll_bitsize
;
5647 xrl_bitpos
= lnbitsize
- xrl_bitpos
- rl_bitsize
;
5650 ll_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, ll_mask
),
5651 size_int (xll_bitpos
));
5652 rl_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
, lntype
, rl_mask
),
5653 size_int (xrl_bitpos
));
5657 l_const
= fold_convert_loc (loc
, lntype
, l_const
);
5658 l_const
= unextend (l_const
, ll_bitsize
, ll_unsignedp
, ll_and_mask
);
5659 l_const
= const_binop (LSHIFT_EXPR
, l_const
, size_int (xll_bitpos
));
5660 if (! integer_zerop (const_binop (BIT_AND_EXPR
, l_const
,
5661 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5664 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5666 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5671 r_const
= fold_convert_loc (loc
, lntype
, r_const
);
5672 r_const
= unextend (r_const
, rl_bitsize
, rl_unsignedp
, rl_and_mask
);
5673 r_const
= const_binop (LSHIFT_EXPR
, r_const
, size_int (xrl_bitpos
));
5674 if (! integer_zerop (const_binop (BIT_AND_EXPR
, r_const
,
5675 fold_build1_loc (loc
, BIT_NOT_EXPR
,
5678 warning (0, "comparison is always %d", wanted_code
== NE_EXPR
);
5680 return constant_boolean_node (wanted_code
== NE_EXPR
, truth_type
);
5684 /* If the right sides are not constant, do the same for it. Also,
5685 disallow this optimization if a size or signedness mismatch occurs
5686 between the left and right sides. */
5689 if (ll_bitsize
!= lr_bitsize
|| rl_bitsize
!= rr_bitsize
5690 || ll_unsignedp
!= lr_unsignedp
|| rl_unsignedp
!= rr_unsignedp
5691 /* Make sure the two fields on the right
5692 correspond to the left without being swapped. */
5693 || ll_bitpos
- rl_bitpos
!= lr_bitpos
- rr_bitpos
)
5696 first_bit
= MIN (lr_bitpos
, rr_bitpos
);
5697 end_bit
= MAX (lr_bitpos
+ lr_bitsize
, rr_bitpos
+ rr_bitsize
);
5698 rnmode
= get_best_mode (end_bit
- first_bit
, first_bit
, 0, 0,
5699 TYPE_ALIGN (TREE_TYPE (lr_inner
)), word_mode
,
5701 if (rnmode
== VOIDmode
)
5704 rnbitsize
= GET_MODE_BITSIZE (rnmode
);
5705 rnbitpos
= first_bit
& ~ (rnbitsize
- 1);
5706 rntype
= lang_hooks
.types
.type_for_size (rnbitsize
, 1);
5707 xlr_bitpos
= lr_bitpos
- rnbitpos
, xrr_bitpos
= rr_bitpos
- rnbitpos
;
5709 if (BYTES_BIG_ENDIAN
)
5711 xlr_bitpos
= rnbitsize
- xlr_bitpos
- lr_bitsize
;
5712 xrr_bitpos
= rnbitsize
- xrr_bitpos
- rr_bitsize
;
5715 lr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5717 size_int (xlr_bitpos
));
5718 rr_mask
= const_binop (LSHIFT_EXPR
, fold_convert_loc (loc
,
5720 size_int (xrr_bitpos
));
5722 /* Make a mask that corresponds to both fields being compared.
5723 Do this for both items being compared. If the operands are the
5724 same size and the bits being compared are in the same position
5725 then we can do this by masking both and comparing the masked
5727 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5728 lr_mask
= const_binop (BIT_IOR_EXPR
, lr_mask
, rr_mask
);
5729 if (lnbitsize
== rnbitsize
&& xll_bitpos
== xlr_bitpos
)
5731 lhs
= make_bit_field_ref (loc
, ll_inner
, lntype
, lnbitsize
, lnbitpos
,
5732 ll_unsignedp
|| rl_unsignedp
);
5733 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5734 lhs
= build2 (BIT_AND_EXPR
, lntype
, lhs
, ll_mask
);
5736 rhs
= make_bit_field_ref (loc
, lr_inner
, rntype
, rnbitsize
, rnbitpos
,
5737 lr_unsignedp
|| rr_unsignedp
);
5738 if (! all_ones_mask_p (lr_mask
, rnbitsize
))
5739 rhs
= build2 (BIT_AND_EXPR
, rntype
, rhs
, lr_mask
);
5741 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5744 /* There is still another way we can do something: If both pairs of
5745 fields being compared are adjacent, we may be able to make a wider
5746 field containing them both.
5748 Note that we still must mask the lhs/rhs expressions. Furthermore,
5749 the mask must be shifted to account for the shift done by
5750 make_bit_field_ref. */
5751 if ((ll_bitsize
+ ll_bitpos
== rl_bitpos
5752 && lr_bitsize
+ lr_bitpos
== rr_bitpos
)
5753 || (ll_bitpos
== rl_bitpos
+ rl_bitsize
5754 && lr_bitpos
== rr_bitpos
+ rr_bitsize
))
5758 lhs
= make_bit_field_ref (loc
, ll_inner
, lntype
,
5759 ll_bitsize
+ rl_bitsize
,
5760 MIN (ll_bitpos
, rl_bitpos
), ll_unsignedp
);
5761 rhs
= make_bit_field_ref (loc
, lr_inner
, rntype
,
5762 lr_bitsize
+ rr_bitsize
,
5763 MIN (lr_bitpos
, rr_bitpos
), lr_unsignedp
);
5765 ll_mask
= const_binop (RSHIFT_EXPR
, ll_mask
,
5766 size_int (MIN (xll_bitpos
, xrl_bitpos
)));
5767 lr_mask
= const_binop (RSHIFT_EXPR
, lr_mask
,
5768 size_int (MIN (xlr_bitpos
, xrr_bitpos
)));
5770 /* Convert to the smaller type before masking out unwanted bits. */
5772 if (lntype
!= rntype
)
5774 if (lnbitsize
> rnbitsize
)
5776 lhs
= fold_convert_loc (loc
, rntype
, lhs
);
5777 ll_mask
= fold_convert_loc (loc
, rntype
, ll_mask
);
5780 else if (lnbitsize
< rnbitsize
)
5782 rhs
= fold_convert_loc (loc
, lntype
, rhs
);
5783 lr_mask
= fold_convert_loc (loc
, lntype
, lr_mask
);
5788 if (! all_ones_mask_p (ll_mask
, ll_bitsize
+ rl_bitsize
))
5789 lhs
= build2 (BIT_AND_EXPR
, type
, lhs
, ll_mask
);
5791 if (! all_ones_mask_p (lr_mask
, lr_bitsize
+ rr_bitsize
))
5792 rhs
= build2 (BIT_AND_EXPR
, type
, rhs
, lr_mask
);
5794 return build2_loc (loc
, wanted_code
, truth_type
, lhs
, rhs
);
5800 /* Handle the case of comparisons with constants. If there is something in
5801 common between the masks, those bits of the constants must be the same.
5802 If not, the condition is always false. Test for this to avoid generating
5803 incorrect code below. */
5804 result
= const_binop (BIT_AND_EXPR
, ll_mask
, rl_mask
);
5805 if (! integer_zerop (result
)
5806 && simple_cst_equal (const_binop (BIT_AND_EXPR
, result
, l_const
),
5807 const_binop (BIT_AND_EXPR
, result
, r_const
)) != 1)
5809 if (wanted_code
== NE_EXPR
)
5811 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5812 return constant_boolean_node (true, truth_type
);
5816 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5817 return constant_boolean_node (false, truth_type
);
5821 /* Construct the expression we will return. First get the component
5822 reference we will make. Unless the mask is all ones the width of
5823 that field, perform the mask operation. Then compare with the
5825 result
= make_bit_field_ref (loc
, ll_inner
, lntype
, lnbitsize
, lnbitpos
,
5826 ll_unsignedp
|| rl_unsignedp
);
5828 ll_mask
= const_binop (BIT_IOR_EXPR
, ll_mask
, rl_mask
);
5829 if (! all_ones_mask_p (ll_mask
, lnbitsize
))
5830 result
= build2_loc (loc
, BIT_AND_EXPR
, lntype
, result
, ll_mask
);
5832 return build2_loc (loc
, wanted_code
, truth_type
, result
,
5833 const_binop (BIT_IOR_EXPR
, l_const
, r_const
));
5836 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5840 optimize_minmax_comparison (location_t loc
, enum tree_code code
, tree type
,
5844 enum tree_code op_code
;
5847 int consts_equal
, consts_lt
;
5850 STRIP_SIGN_NOPS (arg0
);
5852 op_code
= TREE_CODE (arg0
);
5853 minmax_const
= TREE_OPERAND (arg0
, 1);
5854 comp_const
= fold_convert_loc (loc
, TREE_TYPE (arg0
), op1
);
5855 consts_equal
= tree_int_cst_equal (minmax_const
, comp_const
);
5856 consts_lt
= tree_int_cst_lt (minmax_const
, comp_const
);
5857 inner
= TREE_OPERAND (arg0
, 0);
5859 /* If something does not permit us to optimize, return the original tree. */
5860 if ((op_code
!= MIN_EXPR
&& op_code
!= MAX_EXPR
)
5861 || TREE_CODE (comp_const
) != INTEGER_CST
5862 || TREE_OVERFLOW (comp_const
)
5863 || TREE_CODE (minmax_const
) != INTEGER_CST
5864 || TREE_OVERFLOW (minmax_const
))
5867 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5868 and GT_EXPR, doing the rest with recursive calls using logical
5872 case NE_EXPR
: case LT_EXPR
: case LE_EXPR
:
5875 = optimize_minmax_comparison (loc
,
5876 invert_tree_comparison (code
, false),
5879 return invert_truthvalue_loc (loc
, tem
);
5885 fold_build2_loc (loc
, TRUTH_ORIF_EXPR
, type
,
5886 optimize_minmax_comparison
5887 (loc
, EQ_EXPR
, type
, arg0
, comp_const
),
5888 optimize_minmax_comparison
5889 (loc
, GT_EXPR
, type
, arg0
, comp_const
));
5892 if (op_code
== MAX_EXPR
&& consts_equal
)
5893 /* MAX (X, 0) == 0 -> X <= 0 */
5894 return fold_build2_loc (loc
, LE_EXPR
, type
, inner
, comp_const
);
5896 else if (op_code
== MAX_EXPR
&& consts_lt
)
5897 /* MAX (X, 0) == 5 -> X == 5 */
5898 return fold_build2_loc (loc
, EQ_EXPR
, type
, inner
, comp_const
);
5900 else if (op_code
== MAX_EXPR
)
5901 /* MAX (X, 0) == -1 -> false */
5902 return omit_one_operand_loc (loc
, type
, integer_zero_node
, inner
);
5904 else if (consts_equal
)
5905 /* MIN (X, 0) == 0 -> X >= 0 */
5906 return fold_build2_loc (loc
, GE_EXPR
, type
, inner
, comp_const
);
5909 /* MIN (X, 0) == 5 -> false */
5910 return omit_one_operand_loc (loc
, type
, integer_zero_node
, inner
);
5913 /* MIN (X, 0) == -1 -> X == -1 */
5914 return fold_build2_loc (loc
, EQ_EXPR
, type
, inner
, comp_const
);
5917 if (op_code
== MAX_EXPR
&& (consts_equal
|| consts_lt
))
5918 /* MAX (X, 0) > 0 -> X > 0
5919 MAX (X, 0) > 5 -> X > 5 */
5920 return fold_build2_loc (loc
, GT_EXPR
, type
, inner
, comp_const
);
5922 else if (op_code
== MAX_EXPR
)
5923 /* MAX (X, 0) > -1 -> true */
5924 return omit_one_operand_loc (loc
, type
, integer_one_node
, inner
);
5926 else if (op_code
== MIN_EXPR
&& (consts_equal
|| consts_lt
))
5927 /* MIN (X, 0) > 0 -> false
5928 MIN (X, 0) > 5 -> false */
5929 return omit_one_operand_loc (loc
, type
, integer_zero_node
, inner
);
5932 /* MIN (X, 0) > -1 -> X > -1 */
5933 return fold_build2_loc (loc
, GT_EXPR
, type
, inner
, comp_const
);
5940 /* T is an integer expression that is being multiplied, divided, or taken a
5941 modulus (CODE says which and what kind of divide or modulus) by a
5942 constant C. See if we can eliminate that operation by folding it with
5943 other operations already in T. WIDE_TYPE, if non-null, is a type that
5944 should be used for the computation if wider than our type.
5946 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5947 (X * 2) + (Y * 4). We must, however, be assured that either the original
5948 expression would not overflow or that overflow is undefined for the type
5949 in the language in question.
5951 If we return a non-null expression, it is an equivalent form of the
5952 original computation, but need not be in the original type.
5954 We set *STRICT_OVERFLOW_P to true if the return values depends on
5955 signed overflow being undefined. Otherwise we do not change
5956 *STRICT_OVERFLOW_P. */
5959 extract_muldiv (tree t
, tree c
, enum tree_code code
, tree wide_type
,
5960 bool *strict_overflow_p
)
5962 /* To avoid exponential search depth, refuse to allow recursion past
5963 three levels. Beyond that (1) it's highly unlikely that we'll find
5964 something interesting and (2) we've probably processed it before
5965 when we built the inner expression. */
5974 ret
= extract_muldiv_1 (t
, c
, code
, wide_type
, strict_overflow_p
);
5981 extract_muldiv_1 (tree t
, tree c
, enum tree_code code
, tree wide_type
,
5982 bool *strict_overflow_p
)
5984 tree type
= TREE_TYPE (t
);
5985 enum tree_code tcode
= TREE_CODE (t
);
5986 tree ctype
= (wide_type
!= 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type
))
5987 > GET_MODE_SIZE (TYPE_MODE (type
)))
5988 ? wide_type
: type
);
5990 int same_p
= tcode
== code
;
5991 tree op0
= NULL_TREE
, op1
= NULL_TREE
;
5992 bool sub_strict_overflow_p
;
5994 /* Don't deal with constants of zero here; they confuse the code below. */
5995 if (integer_zerop (c
))
5998 if (TREE_CODE_CLASS (tcode
) == tcc_unary
)
5999 op0
= TREE_OPERAND (t
, 0);
6001 if (TREE_CODE_CLASS (tcode
) == tcc_binary
)
6002 op0
= TREE_OPERAND (t
, 0), op1
= TREE_OPERAND (t
, 1);
6004 /* Note that we need not handle conditional operations here since fold
6005 already handles those cases. So just do arithmetic here. */
6009 /* For a constant, we can always simplify if we are a multiply
6010 or (for divide and modulus) if it is a multiple of our constant. */
6011 if (code
== MULT_EXPR
6012 || wi::multiple_of_p (t
, c
, TYPE_SIGN (type
)))
6013 return const_binop (code
, fold_convert (ctype
, t
),
6014 fold_convert (ctype
, c
));
6017 CASE_CONVERT
: case NON_LVALUE_EXPR
:
6018 /* If op0 is an expression ... */
6019 if ((COMPARISON_CLASS_P (op0
)
6020 || UNARY_CLASS_P (op0
)
6021 || BINARY_CLASS_P (op0
)
6022 || VL_EXP_CLASS_P (op0
)
6023 || EXPRESSION_CLASS_P (op0
))
6024 /* ... and has wrapping overflow, and its type is smaller
6025 than ctype, then we cannot pass through as widening. */
6026 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6027 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0
)))
6028 && (TYPE_PRECISION (ctype
)
6029 > TYPE_PRECISION (TREE_TYPE (op0
))))
6030 /* ... or this is a truncation (t is narrower than op0),
6031 then we cannot pass through this narrowing. */
6032 || (TYPE_PRECISION (type
)
6033 < TYPE_PRECISION (TREE_TYPE (op0
)))
6034 /* ... or signedness changes for division or modulus,
6035 then we cannot pass through this conversion. */
6036 || (code
!= MULT_EXPR
6037 && (TYPE_UNSIGNED (ctype
)
6038 != TYPE_UNSIGNED (TREE_TYPE (op0
))))
6039 /* ... or has undefined overflow while the converted to
6040 type has not, we cannot do the operation in the inner type
6041 as that would introduce undefined overflow. */
6042 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0
))
6043 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0
)))
6044 && !TYPE_OVERFLOW_UNDEFINED (type
))))
6047 /* Pass the constant down and see if we can make a simplification. If
6048 we can, replace this expression with the inner simplification for
6049 possible later conversion to our or some other type. */
6050 if ((t2
= fold_convert (TREE_TYPE (op0
), c
)) != 0
6051 && TREE_CODE (t2
) == INTEGER_CST
6052 && !TREE_OVERFLOW (t2
)
6053 && (0 != (t1
= extract_muldiv (op0
, t2
, code
,
6055 ? ctype
: NULL_TREE
,
6056 strict_overflow_p
))))
6061 /* If widening the type changes it from signed to unsigned, then we
6062 must avoid building ABS_EXPR itself as unsigned. */
6063 if (TYPE_UNSIGNED (ctype
) && !TYPE_UNSIGNED (type
))
6065 tree cstype
= (*signed_type_for
) (ctype
);
6066 if ((t1
= extract_muldiv (op0
, c
, code
, cstype
, strict_overflow_p
))
6069 t1
= fold_build1 (tcode
, cstype
, fold_convert (cstype
, t1
));
6070 return fold_convert (ctype
, t1
);
6074 /* If the constant is negative, we cannot simplify this. */
6075 if (tree_int_cst_sgn (c
) == -1)
6079 /* For division and modulus, type can't be unsigned, as e.g.
6080 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6081 For signed types, even with wrapping overflow, this is fine. */
6082 if (code
!= MULT_EXPR
&& TYPE_UNSIGNED (type
))
6084 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
, strict_overflow_p
))
6086 return fold_build1 (tcode
, ctype
, fold_convert (ctype
, t1
));
6089 case MIN_EXPR
: case MAX_EXPR
:
6090 /* If widening the type changes the signedness, then we can't perform
6091 this optimization as that changes the result. */
6092 if (TYPE_UNSIGNED (ctype
) != TYPE_UNSIGNED (type
))
6095 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6096 sub_strict_overflow_p
= false;
6097 if ((t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6098 &sub_strict_overflow_p
)) != 0
6099 && (t2
= extract_muldiv (op1
, c
, code
, wide_type
,
6100 &sub_strict_overflow_p
)) != 0)
6102 if (tree_int_cst_sgn (c
) < 0)
6103 tcode
= (tcode
== MIN_EXPR
? MAX_EXPR
: MIN_EXPR
);
6104 if (sub_strict_overflow_p
)
6105 *strict_overflow_p
= true;
6106 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6107 fold_convert (ctype
, t2
));
6111 case LSHIFT_EXPR
: case RSHIFT_EXPR
:
6112 /* If the second operand is constant, this is a multiplication
6113 or floor division, by a power of two, so we can treat it that
6114 way unless the multiplier or divisor overflows. Signed
6115 left-shift overflow is implementation-defined rather than
6116 undefined in C90, so do not convert signed left shift into
6118 if (TREE_CODE (op1
) == INTEGER_CST
6119 && (tcode
== RSHIFT_EXPR
|| TYPE_UNSIGNED (TREE_TYPE (op0
)))
6120 /* const_binop may not detect overflow correctly,
6121 so check for it explicitly here. */
6122 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
6123 && 0 != (t1
= fold_convert (ctype
,
6124 const_binop (LSHIFT_EXPR
,
6127 && !TREE_OVERFLOW (t1
))
6128 return extract_muldiv (build2 (tcode
== LSHIFT_EXPR
6129 ? MULT_EXPR
: FLOOR_DIV_EXPR
,
6131 fold_convert (ctype
, op0
),
6133 c
, code
, wide_type
, strict_overflow_p
);
6136 case PLUS_EXPR
: case MINUS_EXPR
:
6137 /* See if we can eliminate the operation on both sides. If we can, we
6138 can return a new PLUS or MINUS. If we can't, the only remaining
6139 cases where we can do anything are if the second operand is a
6141 sub_strict_overflow_p
= false;
6142 t1
= extract_muldiv (op0
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6143 t2
= extract_muldiv (op1
, c
, code
, wide_type
, &sub_strict_overflow_p
);
6144 if (t1
!= 0 && t2
!= 0
6145 && (code
== MULT_EXPR
6146 /* If not multiplication, we can only do this if both operands
6147 are divisible by c. */
6148 || (multiple_of_p (ctype
, op0
, c
)
6149 && multiple_of_p (ctype
, op1
, c
))))
6151 if (sub_strict_overflow_p
)
6152 *strict_overflow_p
= true;
6153 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6154 fold_convert (ctype
, t2
));
6157 /* If this was a subtraction, negate OP1 and set it to be an addition.
6158 This simplifies the logic below. */
6159 if (tcode
== MINUS_EXPR
)
6161 tcode
= PLUS_EXPR
, op1
= negate_expr (op1
);
6162 /* If OP1 was not easily negatable, the constant may be OP0. */
6163 if (TREE_CODE (op0
) == INTEGER_CST
)
6165 std::swap (op0
, op1
);
6170 if (TREE_CODE (op1
) != INTEGER_CST
)
6173 /* If either OP1 or C are negative, this optimization is not safe for
6174 some of the division and remainder types while for others we need
6175 to change the code. */
6176 if (tree_int_cst_sgn (op1
) < 0 || tree_int_cst_sgn (c
) < 0)
6178 if (code
== CEIL_DIV_EXPR
)
6179 code
= FLOOR_DIV_EXPR
;
6180 else if (code
== FLOOR_DIV_EXPR
)
6181 code
= CEIL_DIV_EXPR
;
6182 else if (code
!= MULT_EXPR
6183 && code
!= CEIL_MOD_EXPR
&& code
!= FLOOR_MOD_EXPR
)
6187 /* If it's a multiply or a division/modulus operation of a multiple
6188 of our constant, do the operation and verify it doesn't overflow. */
6189 if (code
== MULT_EXPR
6190 || wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6192 op1
= const_binop (code
, fold_convert (ctype
, op1
),
6193 fold_convert (ctype
, c
));
6194 /* We allow the constant to overflow with wrapping semantics. */
6196 || (TREE_OVERFLOW (op1
) && !TYPE_OVERFLOW_WRAPS (ctype
)))
6202 /* If we have an unsigned type, we cannot widen the operation since it
6203 will change the result if the original computation overflowed. */
6204 if (TYPE_UNSIGNED (ctype
) && ctype
!= type
)
6207 /* If we were able to eliminate our operation from the first side,
6208 apply our operation to the second side and reform the PLUS. */
6209 if (t1
!= 0 && (TREE_CODE (t1
) != code
|| code
== MULT_EXPR
))
6210 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
), op1
);
6212 /* The last case is if we are a multiply. In that case, we can
6213 apply the distributive law to commute the multiply and addition
6214 if the multiplication of the constants doesn't overflow
6215 and overflow is defined. With undefined overflow
6216 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6217 if (code
== MULT_EXPR
&& TYPE_OVERFLOW_WRAPS (ctype
))
6218 return fold_build2 (tcode
, ctype
,
6219 fold_build2 (code
, ctype
,
6220 fold_convert (ctype
, op0
),
6221 fold_convert (ctype
, c
)),
6227 /* We have a special case here if we are doing something like
6228 (C * 8) % 4 since we know that's zero. */
6229 if ((code
== TRUNC_MOD_EXPR
|| code
== CEIL_MOD_EXPR
6230 || code
== FLOOR_MOD_EXPR
|| code
== ROUND_MOD_EXPR
)
6231 /* If the multiplication can overflow we cannot optimize this. */
6232 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t
))
6233 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
6234 && wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6236 *strict_overflow_p
= true;
6237 return omit_one_operand (type
, integer_zero_node
, op0
);
6240 /* ... fall through ... */
6242 case TRUNC_DIV_EXPR
: case CEIL_DIV_EXPR
: case FLOOR_DIV_EXPR
:
6243 case ROUND_DIV_EXPR
: case EXACT_DIV_EXPR
:
6244 /* If we can extract our operation from the LHS, do so and return a
6245 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6246 do something only if the second operand is a constant. */
6248 && (t1
= extract_muldiv (op0
, c
, code
, wide_type
,
6249 strict_overflow_p
)) != 0)
6250 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, t1
),
6251 fold_convert (ctype
, op1
));
6252 else if (tcode
== MULT_EXPR
&& code
== MULT_EXPR
6253 && (t1
= extract_muldiv (op1
, c
, code
, wide_type
,
6254 strict_overflow_p
)) != 0)
6255 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6256 fold_convert (ctype
, t1
));
6257 else if (TREE_CODE (op1
) != INTEGER_CST
)
6260 /* If these are the same operation types, we can associate them
6261 assuming no overflow. */
6264 bool overflow_p
= false;
6265 bool overflow_mul_p
;
6266 signop sign
= TYPE_SIGN (ctype
);
6267 wide_int mul
= wi::mul (op1
, c
, sign
, &overflow_mul_p
);
6268 overflow_p
= TREE_OVERFLOW (c
) | TREE_OVERFLOW (op1
);
6270 && ((sign
== UNSIGNED
&& tcode
!= MULT_EXPR
) || sign
== SIGNED
))
6274 mul
= wide_int::from (mul
, TYPE_PRECISION (ctype
),
6275 TYPE_SIGN (TREE_TYPE (op1
)));
6276 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6277 wide_int_to_tree (ctype
, mul
));
6281 /* If these operations "cancel" each other, we have the main
6282 optimizations of this pass, which occur when either constant is a
6283 multiple of the other, in which case we replace this with either an
6284 operation or CODE or TCODE.
6286 If we have an unsigned type, we cannot do this since it will change
6287 the result if the original computation overflowed. */
6288 if (TYPE_OVERFLOW_UNDEFINED (ctype
)
6289 && ((code
== MULT_EXPR
&& tcode
== EXACT_DIV_EXPR
)
6290 || (tcode
== MULT_EXPR
6291 && code
!= TRUNC_MOD_EXPR
&& code
!= CEIL_MOD_EXPR
6292 && code
!= FLOOR_MOD_EXPR
&& code
!= ROUND_MOD_EXPR
6293 && code
!= MULT_EXPR
)))
6295 if (wi::multiple_of_p (op1
, c
, TYPE_SIGN (type
)))
6297 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6298 *strict_overflow_p
= true;
6299 return fold_build2 (tcode
, ctype
, fold_convert (ctype
, op0
),
6300 fold_convert (ctype
,
6301 const_binop (TRUNC_DIV_EXPR
,
6304 else if (wi::multiple_of_p (c
, op1
, TYPE_SIGN (type
)))
6306 if (TYPE_OVERFLOW_UNDEFINED (ctype
))
6307 *strict_overflow_p
= true;
6308 return fold_build2 (code
, ctype
, fold_convert (ctype
, op0
),
6309 fold_convert (ctype
,
6310 const_binop (TRUNC_DIV_EXPR
,
6323 /* Return a node which has the indicated constant VALUE (either 0 or
6324 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6325 and is of the indicated TYPE. */
6328 constant_boolean_node (bool value
, tree type
)
6330 if (type
== integer_type_node
)
6331 return value
? integer_one_node
: integer_zero_node
;
6332 else if (type
== boolean_type_node
)
6333 return value
? boolean_true_node
: boolean_false_node
;
6334 else if (TREE_CODE (type
) == VECTOR_TYPE
)
6335 return build_vector_from_val (type
,
6336 build_int_cst (TREE_TYPE (type
),
6339 return fold_convert (type
, value
? integer_one_node
: integer_zero_node
);
6343 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6344 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6345 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6346 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6347 COND is the first argument to CODE; otherwise (as in the example
6348 given here), it is the second argument. TYPE is the type of the
6349 original expression. Return NULL_TREE if no simplification is
6353 fold_binary_op_with_conditional_arg (location_t loc
,
6354 enum tree_code code
,
6355 tree type
, tree op0
, tree op1
,
6356 tree cond
, tree arg
, int cond_first_p
)
6358 tree cond_type
= cond_first_p
? TREE_TYPE (op0
) : TREE_TYPE (op1
);
6359 tree arg_type
= cond_first_p
? TREE_TYPE (op1
) : TREE_TYPE (op0
);
6360 tree test
, true_value
, false_value
;
6361 tree lhs
= NULL_TREE
;
6362 tree rhs
= NULL_TREE
;
6363 enum tree_code cond_code
= COND_EXPR
;
6365 if (TREE_CODE (cond
) == COND_EXPR
6366 || TREE_CODE (cond
) == VEC_COND_EXPR
)
6368 test
= TREE_OPERAND (cond
, 0);
6369 true_value
= TREE_OPERAND (cond
, 1);
6370 false_value
= TREE_OPERAND (cond
, 2);
6371 /* If this operand throws an expression, then it does not make
6372 sense to try to perform a logical or arithmetic operation
6374 if (VOID_TYPE_P (TREE_TYPE (true_value
)))
6376 if (VOID_TYPE_P (TREE_TYPE (false_value
)))
6381 tree testtype
= TREE_TYPE (cond
);
6383 true_value
= constant_boolean_node (true, testtype
);
6384 false_value
= constant_boolean_node (false, testtype
);
6387 if (TREE_CODE (TREE_TYPE (test
)) == VECTOR_TYPE
)
6388 cond_code
= VEC_COND_EXPR
;
6390 /* This transformation is only worthwhile if we don't have to wrap ARG
6391 in a SAVE_EXPR and the operation can be simplified without recursing
6392 on at least one of the branches once its pushed inside the COND_EXPR. */
6393 if (!TREE_CONSTANT (arg
)
6394 && (TREE_SIDE_EFFECTS (arg
)
6395 || TREE_CODE (arg
) == COND_EXPR
|| TREE_CODE (arg
) == VEC_COND_EXPR
6396 || TREE_CONSTANT (true_value
) || TREE_CONSTANT (false_value
)))
6399 arg
= fold_convert_loc (loc
, arg_type
, arg
);
6402 true_value
= fold_convert_loc (loc
, cond_type
, true_value
);
6404 lhs
= fold_build2_loc (loc
, code
, type
, true_value
, arg
);
6406 lhs
= fold_build2_loc (loc
, code
, type
, arg
, true_value
);
6410 false_value
= fold_convert_loc (loc
, cond_type
, false_value
);
6412 rhs
= fold_build2_loc (loc
, code
, type
, false_value
, arg
);
6414 rhs
= fold_build2_loc (loc
, code
, type
, arg
, false_value
);
6417 /* Check that we have simplified at least one of the branches. */
6418 if (!TREE_CONSTANT (arg
) && !TREE_CONSTANT (lhs
) && !TREE_CONSTANT (rhs
))
6421 return fold_build3_loc (loc
, cond_code
, type
, test
, lhs
, rhs
);
6425 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6427 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6428 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6429 ADDEND is the same as X.
6431 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6432 and finite. The problematic cases are when X is zero, and its mode
6433 has signed zeros. In the case of rounding towards -infinity,
6434 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6435 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6438 fold_real_zero_addition_p (const_tree type
, const_tree addend
, int negate
)
6440 if (!real_zerop (addend
))
6443 /* Don't allow the fold with -fsignaling-nans. */
6444 if (HONOR_SNANS (element_mode (type
)))
6447 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6448 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
6451 /* In a vector or complex, we would need to check the sign of all zeros. */
6452 if (TREE_CODE (addend
) != REAL_CST
)
6455 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6456 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend
)))
6459 /* The mode has signed zeros, and we have to honor their sign.
6460 In this situation, there is only one case we can return true for.
6461 X - 0 is the same as X unless rounding towards -infinity is
6463 return negate
&& !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type
));
6466 /* Subroutine of fold() that optimizes comparisons of a division by
6467 a nonzero integer constant against an integer constant, i.e.
6470 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6471 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6472 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6474 The function returns the constant folded tree if a simplification
6475 can be made, and NULL_TREE otherwise. */
6478 fold_div_compare (location_t loc
,
6479 enum tree_code code
, tree type
, tree arg0
, tree arg1
)
6481 tree prod
, tmp
, hi
, lo
;
6482 tree arg00
= TREE_OPERAND (arg0
, 0);
6483 tree arg01
= TREE_OPERAND (arg0
, 1);
6484 signop sign
= TYPE_SIGN (TREE_TYPE (arg0
));
6485 bool neg_overflow
= false;
6488 /* We have to do this the hard way to detect unsigned overflow.
6489 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6490 wide_int val
= wi::mul (arg01
, arg1
, sign
, &overflow
);
6491 prod
= force_fit_type (TREE_TYPE (arg00
), val
, -1, overflow
);
6492 neg_overflow
= false;
6494 if (sign
== UNSIGNED
)
6496 tmp
= int_const_binop (MINUS_EXPR
, arg01
,
6497 build_int_cst (TREE_TYPE (arg01
), 1));
6500 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6501 val
= wi::add (prod
, tmp
, sign
, &overflow
);
6502 hi
= force_fit_type (TREE_TYPE (arg00
), val
,
6503 -1, overflow
| TREE_OVERFLOW (prod
));
6505 else if (tree_int_cst_sgn (arg01
) >= 0)
6507 tmp
= int_const_binop (MINUS_EXPR
, arg01
,
6508 build_int_cst (TREE_TYPE (arg01
), 1));
6509 switch (tree_int_cst_sgn (arg1
))
6512 neg_overflow
= true;
6513 lo
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6518 lo
= fold_negate_const (tmp
, TREE_TYPE (arg0
));
6523 hi
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6533 /* A negative divisor reverses the relational operators. */
6534 code
= swap_tree_comparison (code
);
6536 tmp
= int_const_binop (PLUS_EXPR
, arg01
,
6537 build_int_cst (TREE_TYPE (arg01
), 1));
6538 switch (tree_int_cst_sgn (arg1
))
6541 hi
= int_const_binop (MINUS_EXPR
, prod
, tmp
);
6546 hi
= fold_negate_const (tmp
, TREE_TYPE (arg0
));
6551 neg_overflow
= true;
6552 lo
= int_const_binop (PLUS_EXPR
, prod
, tmp
);
6564 if (TREE_OVERFLOW (lo
) && TREE_OVERFLOW (hi
))
6565 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg00
);
6566 if (TREE_OVERFLOW (hi
))
6567 return fold_build2_loc (loc
, GE_EXPR
, type
, arg00
, lo
);
6568 if (TREE_OVERFLOW (lo
))
6569 return fold_build2_loc (loc
, LE_EXPR
, type
, arg00
, hi
);
6570 return build_range_check (loc
, type
, arg00
, 1, lo
, hi
);
6573 if (TREE_OVERFLOW (lo
) && TREE_OVERFLOW (hi
))
6574 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg00
);
6575 if (TREE_OVERFLOW (hi
))
6576 return fold_build2_loc (loc
, LT_EXPR
, type
, arg00
, lo
);
6577 if (TREE_OVERFLOW (lo
))
6578 return fold_build2_loc (loc
, GT_EXPR
, type
, arg00
, hi
);
6579 return build_range_check (loc
, type
, arg00
, 0, lo
, hi
);
6582 if (TREE_OVERFLOW (lo
))
6584 tmp
= neg_overflow
? integer_zero_node
: integer_one_node
;
6585 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6587 return fold_build2_loc (loc
, LT_EXPR
, type
, arg00
, lo
);
6590 if (TREE_OVERFLOW (hi
))
6592 tmp
= neg_overflow
? integer_zero_node
: integer_one_node
;
6593 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6595 return fold_build2_loc (loc
, LE_EXPR
, type
, arg00
, hi
);
6598 if (TREE_OVERFLOW (hi
))
6600 tmp
= neg_overflow
? integer_one_node
: integer_zero_node
;
6601 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6603 return fold_build2_loc (loc
, GT_EXPR
, type
, arg00
, hi
);
6606 if (TREE_OVERFLOW (lo
))
6608 tmp
= neg_overflow
? integer_one_node
: integer_zero_node
;
6609 return omit_one_operand_loc (loc
, type
, tmp
, arg00
);
6611 return fold_build2_loc (loc
, GE_EXPR
, type
, arg00
, lo
);
6621 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6622 equality/inequality test, then return a simplified form of the test
6623 using a sign testing. Otherwise return NULL. TYPE is the desired
6627 fold_single_bit_test_into_sign_test (location_t loc
,
6628 enum tree_code code
, tree arg0
, tree arg1
,
6631 /* If this is testing a single bit, we can optimize the test. */
6632 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6633 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6634 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6636 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6637 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6638 tree arg00
= sign_bit_p (TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg0
, 1));
6640 if (arg00
!= NULL_TREE
6641 /* This is only a win if casting to a signed type is cheap,
6642 i.e. when arg00's type is not a partial mode. */
6643 && TYPE_PRECISION (TREE_TYPE (arg00
))
6644 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00
))))
6646 tree stype
= signed_type_for (TREE_TYPE (arg00
));
6647 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
6649 fold_convert_loc (loc
, stype
, arg00
),
6650 build_int_cst (stype
, 0));
6657 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6658 equality/inequality test, then return a simplified form of
6659 the test using shifts and logical operations. Otherwise return
6660 NULL. TYPE is the desired result type. */
6663 fold_single_bit_test (location_t loc
, enum tree_code code
,
6664 tree arg0
, tree arg1
, tree result_type
)
6666 /* If this is testing a single bit, we can optimize the test. */
6667 if ((code
== NE_EXPR
|| code
== EQ_EXPR
)
6668 && TREE_CODE (arg0
) == BIT_AND_EXPR
&& integer_zerop (arg1
)
6669 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
6671 tree inner
= TREE_OPERAND (arg0
, 0);
6672 tree type
= TREE_TYPE (arg0
);
6673 int bitnum
= tree_log2 (TREE_OPERAND (arg0
, 1));
6674 machine_mode operand_mode
= TYPE_MODE (type
);
6676 tree signed_type
, unsigned_type
, intermediate_type
;
6679 /* First, see if we can fold the single bit test into a sign-bit
6681 tem
= fold_single_bit_test_into_sign_test (loc
, code
, arg0
, arg1
,
6686 /* Otherwise we have (A & C) != 0 where C is a single bit,
6687 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6688 Similarly for (A & C) == 0. */
6690 /* If INNER is a right shift of a constant and it plus BITNUM does
6691 not overflow, adjust BITNUM and INNER. */
6692 if (TREE_CODE (inner
) == RSHIFT_EXPR
6693 && TREE_CODE (TREE_OPERAND (inner
, 1)) == INTEGER_CST
6694 && bitnum
< TYPE_PRECISION (type
)
6695 && wi::ltu_p (TREE_OPERAND (inner
, 1),
6696 TYPE_PRECISION (type
) - bitnum
))
6698 bitnum
+= tree_to_uhwi (TREE_OPERAND (inner
, 1));
6699 inner
= TREE_OPERAND (inner
, 0);
6702 /* If we are going to be able to omit the AND below, we must do our
6703 operations as unsigned. If we must use the AND, we have a choice.
6704 Normally unsigned is faster, but for some machines signed is. */
6705 ops_unsigned
= (LOAD_EXTEND_OP (operand_mode
) == SIGN_EXTEND
6706 && !flag_syntax_only
) ? 0 : 1;
6708 signed_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 0);
6709 unsigned_type
= lang_hooks
.types
.type_for_mode (operand_mode
, 1);
6710 intermediate_type
= ops_unsigned
? unsigned_type
: signed_type
;
6711 inner
= fold_convert_loc (loc
, intermediate_type
, inner
);
6714 inner
= build2 (RSHIFT_EXPR
, intermediate_type
,
6715 inner
, size_int (bitnum
));
6717 one
= build_int_cst (intermediate_type
, 1);
6719 if (code
== EQ_EXPR
)
6720 inner
= fold_build2_loc (loc
, BIT_XOR_EXPR
, intermediate_type
, inner
, one
);
6722 /* Put the AND last so it can combine with more things. */
6723 inner
= build2 (BIT_AND_EXPR
, intermediate_type
, inner
, one
);
6725 /* Make sure to return the proper type. */
6726 inner
= fold_convert_loc (loc
, result_type
, inner
);
6733 /* Check whether we are allowed to reorder operands arg0 and arg1,
6734 such that the evaluation of arg1 occurs before arg0. */
6737 reorder_operands_p (const_tree arg0
, const_tree arg1
)
6739 if (! flag_evaluation_order
)
6741 if (TREE_CONSTANT (arg0
) || TREE_CONSTANT (arg1
))
6743 return ! TREE_SIDE_EFFECTS (arg0
)
6744 && ! TREE_SIDE_EFFECTS (arg1
);
6747 /* Test whether it is preferable two swap two operands, ARG0 and
6748 ARG1, for example because ARG0 is an integer constant and ARG1
6749 isn't. If REORDER is true, only recommend swapping if we can
6750 evaluate the operands in reverse order. */
6753 tree_swap_operands_p (const_tree arg0
, const_tree arg1
, bool reorder
)
6755 if (CONSTANT_CLASS_P (arg1
))
6757 if (CONSTANT_CLASS_P (arg0
))
6763 if (TREE_CONSTANT (arg1
))
6765 if (TREE_CONSTANT (arg0
))
6768 if (reorder
&& flag_evaluation_order
6769 && (TREE_SIDE_EFFECTS (arg0
) || TREE_SIDE_EFFECTS (arg1
)))
6772 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6773 for commutative and comparison operators. Ensuring a canonical
6774 form allows the optimizers to find additional redundancies without
6775 having to explicitly check for both orderings. */
6776 if (TREE_CODE (arg0
) == SSA_NAME
6777 && TREE_CODE (arg1
) == SSA_NAME
6778 && SSA_NAME_VERSION (arg0
) > SSA_NAME_VERSION (arg1
))
6781 /* Put SSA_NAMEs last. */
6782 if (TREE_CODE (arg1
) == SSA_NAME
)
6784 if (TREE_CODE (arg0
) == SSA_NAME
)
6787 /* Put variables last. */
6797 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6798 means A >= Y && A != MAX, but in this case we know that
6799 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6802 fold_to_nonsharp_ineq_using_bound (location_t loc
, tree ineq
, tree bound
)
6804 tree a
, typea
, type
= TREE_TYPE (ineq
), a1
, diff
, y
;
6806 if (TREE_CODE (bound
) == LT_EXPR
)
6807 a
= TREE_OPERAND (bound
, 0);
6808 else if (TREE_CODE (bound
) == GT_EXPR
)
6809 a
= TREE_OPERAND (bound
, 1);
6813 typea
= TREE_TYPE (a
);
6814 if (!INTEGRAL_TYPE_P (typea
)
6815 && !POINTER_TYPE_P (typea
))
6818 if (TREE_CODE (ineq
) == LT_EXPR
)
6820 a1
= TREE_OPERAND (ineq
, 1);
6821 y
= TREE_OPERAND (ineq
, 0);
6823 else if (TREE_CODE (ineq
) == GT_EXPR
)
6825 a1
= TREE_OPERAND (ineq
, 0);
6826 y
= TREE_OPERAND (ineq
, 1);
6831 if (TREE_TYPE (a1
) != typea
)
6834 if (POINTER_TYPE_P (typea
))
6836 /* Convert the pointer types into integer before taking the difference. */
6837 tree ta
= fold_convert_loc (loc
, ssizetype
, a
);
6838 tree ta1
= fold_convert_loc (loc
, ssizetype
, a1
);
6839 diff
= fold_binary_loc (loc
, MINUS_EXPR
, ssizetype
, ta1
, ta
);
6842 diff
= fold_binary_loc (loc
, MINUS_EXPR
, typea
, a1
, a
);
6844 if (!diff
|| !integer_onep (diff
))
6847 return fold_build2_loc (loc
, GE_EXPR
, type
, a
, y
);
6850 /* Fold a sum or difference of at least one multiplication.
6851 Returns the folded tree or NULL if no simplification could be made. */
6854 fold_plusminus_mult_expr (location_t loc
, enum tree_code code
, tree type
,
6855 tree arg0
, tree arg1
)
6857 tree arg00
, arg01
, arg10
, arg11
;
6858 tree alt0
= NULL_TREE
, alt1
= NULL_TREE
, same
;
6860 /* (A * C) +- (B * C) -> (A+-B) * C.
6861 (A * C) +- A -> A * (C+-1).
6862 We are most concerned about the case where C is a constant,
6863 but other combinations show up during loop reduction. Since
6864 it is not difficult, try all four possibilities. */
6866 if (TREE_CODE (arg0
) == MULT_EXPR
)
6868 arg00
= TREE_OPERAND (arg0
, 0);
6869 arg01
= TREE_OPERAND (arg0
, 1);
6871 else if (TREE_CODE (arg0
) == INTEGER_CST
)
6873 arg00
= build_one_cst (type
);
6878 /* We cannot generate constant 1 for fract. */
6879 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
6882 arg01
= build_one_cst (type
);
6884 if (TREE_CODE (arg1
) == MULT_EXPR
)
6886 arg10
= TREE_OPERAND (arg1
, 0);
6887 arg11
= TREE_OPERAND (arg1
, 1);
6889 else if (TREE_CODE (arg1
) == INTEGER_CST
)
6891 arg10
= build_one_cst (type
);
6892 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6893 the purpose of this canonicalization. */
6894 if (wi::neg_p (arg1
, TYPE_SIGN (TREE_TYPE (arg1
)))
6895 && negate_expr_p (arg1
)
6896 && code
== PLUS_EXPR
)
6898 arg11
= negate_expr (arg1
);
6906 /* We cannot generate constant 1 for fract. */
6907 if (ALL_FRACT_MODE_P (TYPE_MODE (type
)))
6910 arg11
= build_one_cst (type
);
6914 if (operand_equal_p (arg01
, arg11
, 0))
6915 same
= arg01
, alt0
= arg00
, alt1
= arg10
;
6916 else if (operand_equal_p (arg00
, arg10
, 0))
6917 same
= arg00
, alt0
= arg01
, alt1
= arg11
;
6918 else if (operand_equal_p (arg00
, arg11
, 0))
6919 same
= arg00
, alt0
= arg01
, alt1
= arg10
;
6920 else if (operand_equal_p (arg01
, arg10
, 0))
6921 same
= arg01
, alt0
= arg00
, alt1
= arg11
;
6923 /* No identical multiplicands; see if we can find a common
6924 power-of-two factor in non-power-of-two multiplies. This
6925 can help in multi-dimensional array access. */
6926 else if (tree_fits_shwi_p (arg01
)
6927 && tree_fits_shwi_p (arg11
))
6929 HOST_WIDE_INT int01
, int11
, tmp
;
6932 int01
= tree_to_shwi (arg01
);
6933 int11
= tree_to_shwi (arg11
);
6935 /* Move min of absolute values to int11. */
6936 if (absu_hwi (int01
) < absu_hwi (int11
))
6938 tmp
= int01
, int01
= int11
, int11
= tmp
;
6939 alt0
= arg00
, arg00
= arg10
, arg10
= alt0
;
6946 if (exact_log2 (absu_hwi (int11
)) > 0 && int01
% int11
== 0
6947 /* The remainder should not be a constant, otherwise we
6948 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6949 increased the number of multiplications necessary. */
6950 && TREE_CODE (arg10
) != INTEGER_CST
)
6952 alt0
= fold_build2_loc (loc
, MULT_EXPR
, TREE_TYPE (arg00
), arg00
,
6953 build_int_cst (TREE_TYPE (arg00
),
6958 maybe_same
= alt0
, alt0
= alt1
, alt1
= maybe_same
;
6963 return fold_build2_loc (loc
, MULT_EXPR
, type
,
6964 fold_build2_loc (loc
, code
, type
,
6965 fold_convert_loc (loc
, type
, alt0
),
6966 fold_convert_loc (loc
, type
, alt1
)),
6967 fold_convert_loc (loc
, type
, same
));
6972 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
6973 specified by EXPR into the buffer PTR of length LEN bytes.
6974 Return the number of bytes placed in the buffer, or zero
6978 native_encode_int (const_tree expr
, unsigned char *ptr
, int len
, int off
)
6980 tree type
= TREE_TYPE (expr
);
6981 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
6982 int byte
, offset
, word
, words
;
6983 unsigned char value
;
6985 if ((off
== -1 && total_bytes
> len
)
6986 || off
>= total_bytes
)
6990 words
= total_bytes
/ UNITS_PER_WORD
;
6992 for (byte
= 0; byte
< total_bytes
; byte
++)
6994 int bitpos
= byte
* BITS_PER_UNIT
;
6995 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
6997 value
= wi::extract_uhwi (wi::to_widest (expr
), bitpos
, BITS_PER_UNIT
);
6999 if (total_bytes
> UNITS_PER_WORD
)
7001 word
= byte
/ UNITS_PER_WORD
;
7002 if (WORDS_BIG_ENDIAN
)
7003 word
= (words
- 1) - word
;
7004 offset
= word
* UNITS_PER_WORD
;
7005 if (BYTES_BIG_ENDIAN
)
7006 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7008 offset
+= byte
% UNITS_PER_WORD
;
7011 offset
= BYTES_BIG_ENDIAN
? (total_bytes
- 1) - byte
: byte
;
7013 && offset
- off
< len
)
7014 ptr
[offset
- off
] = value
;
7016 return MIN (len
, total_bytes
- off
);
7020 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7021 specified by EXPR into the buffer PTR of length LEN bytes.
7022 Return the number of bytes placed in the buffer, or zero
7026 native_encode_fixed (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7028 tree type
= TREE_TYPE (expr
);
7029 machine_mode mode
= TYPE_MODE (type
);
7030 int total_bytes
= GET_MODE_SIZE (mode
);
7031 FIXED_VALUE_TYPE value
;
7032 tree i_value
, i_type
;
7034 if (total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7037 i_type
= lang_hooks
.types
.type_for_size (GET_MODE_BITSIZE (mode
), 1);
7039 if (NULL_TREE
== i_type
7040 || TYPE_PRECISION (i_type
) != total_bytes
)
7043 value
= TREE_FIXED_CST (expr
);
7044 i_value
= double_int_to_tree (i_type
, value
.data
);
7046 return native_encode_int (i_value
, ptr
, len
, off
);
7050 /* Subroutine of native_encode_expr. Encode the REAL_CST
7051 specified by EXPR into the buffer PTR of length LEN bytes.
7052 Return the number of bytes placed in the buffer, or zero
7056 native_encode_real (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7058 tree type
= TREE_TYPE (expr
);
7059 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7060 int byte
, offset
, word
, words
, bitpos
;
7061 unsigned char value
;
7063 /* There are always 32 bits in each long, no matter the size of
7064 the hosts long. We handle floating point representations with
7068 if ((off
== -1 && total_bytes
> len
)
7069 || off
>= total_bytes
)
7073 words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7075 real_to_target (tmp
, TREE_REAL_CST_PTR (expr
), TYPE_MODE (type
));
7077 for (bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7078 bitpos
+= BITS_PER_UNIT
)
7080 byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7081 value
= (unsigned char) (tmp
[bitpos
/ 32] >> (bitpos
& 31));
7083 if (UNITS_PER_WORD
< 4)
7085 word
= byte
/ UNITS_PER_WORD
;
7086 if (WORDS_BIG_ENDIAN
)
7087 word
= (words
- 1) - word
;
7088 offset
= word
* UNITS_PER_WORD
;
7089 if (BYTES_BIG_ENDIAN
)
7090 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7092 offset
+= byte
% UNITS_PER_WORD
;
7095 offset
= BYTES_BIG_ENDIAN
? 3 - byte
: byte
;
7096 offset
= offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3);
7098 && offset
- off
< len
)
7099 ptr
[offset
- off
] = value
;
7101 return MIN (len
, total_bytes
- off
);
7104 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7105 specified by EXPR into the buffer PTR of length LEN bytes.
7106 Return the number of bytes placed in the buffer, or zero
7110 native_encode_complex (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7115 part
= TREE_REALPART (expr
);
7116 rsize
= native_encode_expr (part
, ptr
, len
, off
);
7120 part
= TREE_IMAGPART (expr
);
7122 off
= MAX (0, off
- GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part
))));
7123 isize
= native_encode_expr (part
, ptr
+rsize
, len
-rsize
, off
);
7127 return rsize
+ isize
;
7131 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7132 specified by EXPR into the buffer PTR of length LEN bytes.
7133 Return the number of bytes placed in the buffer, or zero
7137 native_encode_vector (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7144 count
= VECTOR_CST_NELTS (expr
);
7145 itype
= TREE_TYPE (TREE_TYPE (expr
));
7146 size
= GET_MODE_SIZE (TYPE_MODE (itype
));
7147 for (i
= 0; i
< count
; i
++)
7154 elem
= VECTOR_CST_ELT (expr
, i
);
7155 int res
= native_encode_expr (elem
, ptr
+offset
, len
-offset
, off
);
7156 if ((off
== -1 && res
!= size
)
7169 /* Subroutine of native_encode_expr. Encode the STRING_CST
7170 specified by EXPR into the buffer PTR of length LEN bytes.
7171 Return the number of bytes placed in the buffer, or zero
7175 native_encode_string (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7177 tree type
= TREE_TYPE (expr
);
7178 HOST_WIDE_INT total_bytes
;
7180 if (TREE_CODE (type
) != ARRAY_TYPE
7181 || TREE_CODE (TREE_TYPE (type
)) != INTEGER_TYPE
7182 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type
))) != BITS_PER_UNIT
7183 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type
)))
7185 total_bytes
= tree_to_shwi (TYPE_SIZE_UNIT (type
));
7186 if ((off
== -1 && total_bytes
> len
)
7187 || off
>= total_bytes
)
7191 if (TREE_STRING_LENGTH (expr
) - off
< MIN (total_bytes
, len
))
7194 if (off
< TREE_STRING_LENGTH (expr
))
7196 written
= MIN (len
, TREE_STRING_LENGTH (expr
) - off
);
7197 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, written
);
7199 memset (ptr
+ written
, 0,
7200 MIN (total_bytes
- written
, len
- written
));
7203 memcpy (ptr
, TREE_STRING_POINTER (expr
) + off
, MIN (total_bytes
, len
));
7204 return MIN (total_bytes
- off
, len
);
7208 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7209 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7210 buffer PTR of length LEN bytes. If OFF is not -1 then start
7211 the encoding at byte offset OFF and encode at most LEN bytes.
7212 Return the number of bytes placed in the buffer, or zero upon failure. */
7215 native_encode_expr (const_tree expr
, unsigned char *ptr
, int len
, int off
)
7217 /* We don't support starting at negative offset and -1 is special. */
7221 switch (TREE_CODE (expr
))
7224 return native_encode_int (expr
, ptr
, len
, off
);
7227 return native_encode_real (expr
, ptr
, len
, off
);
7230 return native_encode_fixed (expr
, ptr
, len
, off
);
7233 return native_encode_complex (expr
, ptr
, len
, off
);
7236 return native_encode_vector (expr
, ptr
, len
, off
);
7239 return native_encode_string (expr
, ptr
, len
, off
);
7247 /* Subroutine of native_interpret_expr. Interpret the contents of
7248 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7249 If the buffer cannot be interpreted, return NULL_TREE. */
7252 native_interpret_int (tree type
, const unsigned char *ptr
, int len
)
7254 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7256 if (total_bytes
> len
7257 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7260 wide_int result
= wi::from_buffer (ptr
, total_bytes
);
7262 return wide_int_to_tree (type
, result
);
7266 /* Subroutine of native_interpret_expr. Interpret the contents of
7267 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7268 If the buffer cannot be interpreted, return NULL_TREE. */
7271 native_interpret_fixed (tree type
, const unsigned char *ptr
, int len
)
7273 int total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7275 FIXED_VALUE_TYPE fixed_value
;
7277 if (total_bytes
> len
7278 || total_bytes
* BITS_PER_UNIT
> HOST_BITS_PER_DOUBLE_INT
)
7281 result
= double_int::from_buffer (ptr
, total_bytes
);
7282 fixed_value
= fixed_from_double_int (result
, TYPE_MODE (type
));
7284 return build_fixed (type
, fixed_value
);
7288 /* Subroutine of native_interpret_expr. Interpret the contents of
7289 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7290 If the buffer cannot be interpreted, return NULL_TREE. */
7293 native_interpret_real (tree type
, const unsigned char *ptr
, int len
)
7295 machine_mode mode
= TYPE_MODE (type
);
7296 int total_bytes
= GET_MODE_SIZE (mode
);
7297 unsigned char value
;
7298 /* There are always 32 bits in each long, no matter the size of
7299 the hosts long. We handle floating point representations with
7304 total_bytes
= GET_MODE_SIZE (TYPE_MODE (type
));
7305 if (total_bytes
> len
|| total_bytes
> 24)
7307 int words
= (32 / BITS_PER_UNIT
) / UNITS_PER_WORD
;
7309 memset (tmp
, 0, sizeof (tmp
));
7310 for (int bitpos
= 0; bitpos
< total_bytes
* BITS_PER_UNIT
;
7311 bitpos
+= BITS_PER_UNIT
)
7313 /* Both OFFSET and BYTE index within a long;
7314 bitpos indexes the whole float. */
7315 int offset
, byte
= (bitpos
/ BITS_PER_UNIT
) & 3;
7316 if (UNITS_PER_WORD
< 4)
7318 int word
= byte
/ UNITS_PER_WORD
;
7319 if (WORDS_BIG_ENDIAN
)
7320 word
= (words
- 1) - word
;
7321 offset
= word
* UNITS_PER_WORD
;
7322 if (BYTES_BIG_ENDIAN
)
7323 offset
+= (UNITS_PER_WORD
- 1) - (byte
% UNITS_PER_WORD
);
7325 offset
+= byte
% UNITS_PER_WORD
;
7330 if (BYTES_BIG_ENDIAN
)
7332 /* Reverse bytes within each long, or within the entire float
7333 if it's smaller than a long (for HFmode). */
7334 offset
= MIN (3, total_bytes
- 1) - offset
;
7335 gcc_assert (offset
>= 0);
7338 value
= ptr
[offset
+ ((bitpos
/ BITS_PER_UNIT
) & ~3)];
7340 tmp
[bitpos
/ 32] |= (unsigned long)value
<< (bitpos
& 31);
7343 real_from_target (&r
, tmp
, mode
);
7344 return build_real (type
, r
);
7348 /* Subroutine of native_interpret_expr. Interpret the contents of
7349 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7350 If the buffer cannot be interpreted, return NULL_TREE. */
7353 native_interpret_complex (tree type
, const unsigned char *ptr
, int len
)
7355 tree etype
, rpart
, ipart
;
7358 etype
= TREE_TYPE (type
);
7359 size
= GET_MODE_SIZE (TYPE_MODE (etype
));
7362 rpart
= native_interpret_expr (etype
, ptr
, size
);
7365 ipart
= native_interpret_expr (etype
, ptr
+size
, size
);
7368 return build_complex (type
, rpart
, ipart
);
7372 /* Subroutine of native_interpret_expr. Interpret the contents of
7373 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7374 If the buffer cannot be interpreted, return NULL_TREE. */
7377 native_interpret_vector (tree type
, const unsigned char *ptr
, int len
)
7383 etype
= TREE_TYPE (type
);
7384 size
= GET_MODE_SIZE (TYPE_MODE (etype
));
7385 count
= TYPE_VECTOR_SUBPARTS (type
);
7386 if (size
* count
> len
)
7389 elements
= XALLOCAVEC (tree
, count
);
7390 for (i
= count
- 1; i
>= 0; i
--)
7392 elem
= native_interpret_expr (etype
, ptr
+(i
*size
), size
);
7397 return build_vector (type
, elements
);
7401 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7402 the buffer PTR of length LEN as a constant of type TYPE. For
7403 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7404 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7405 return NULL_TREE. */
7408 native_interpret_expr (tree type
, const unsigned char *ptr
, int len
)
7410 switch (TREE_CODE (type
))
7416 case REFERENCE_TYPE
:
7417 return native_interpret_int (type
, ptr
, len
);
7420 return native_interpret_real (type
, ptr
, len
);
7422 case FIXED_POINT_TYPE
:
7423 return native_interpret_fixed (type
, ptr
, len
);
7426 return native_interpret_complex (type
, ptr
, len
);
7429 return native_interpret_vector (type
, ptr
, len
);
7436 /* Returns true if we can interpret the contents of a native encoding
7440 can_native_interpret_type_p (tree type
)
7442 switch (TREE_CODE (type
))
7448 case REFERENCE_TYPE
:
7449 case FIXED_POINT_TYPE
:
7459 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7460 TYPE at compile-time. If we're unable to perform the conversion
7461 return NULL_TREE. */
7464 fold_view_convert_expr (tree type
, tree expr
)
7466 /* We support up to 512-bit values (for V8DFmode). */
7467 unsigned char buffer
[64];
7470 /* Check that the host and target are sane. */
7471 if (CHAR_BIT
!= 8 || BITS_PER_UNIT
!= 8)
7474 len
= native_encode_expr (expr
, buffer
, sizeof (buffer
));
7478 return native_interpret_expr (type
, buffer
, len
);
7481 /* Build an expression for the address of T. Folds away INDIRECT_REF
7482 to avoid confusing the gimplify process. */
7485 build_fold_addr_expr_with_type_loc (location_t loc
, tree t
, tree ptrtype
)
7487 /* The size of the object is not relevant when talking about its address. */
7488 if (TREE_CODE (t
) == WITH_SIZE_EXPR
)
7489 t
= TREE_OPERAND (t
, 0);
7491 if (TREE_CODE (t
) == INDIRECT_REF
)
7493 t
= TREE_OPERAND (t
, 0);
7495 if (TREE_TYPE (t
) != ptrtype
)
7496 t
= build1_loc (loc
, NOP_EXPR
, ptrtype
, t
);
7498 else if (TREE_CODE (t
) == MEM_REF
7499 && integer_zerop (TREE_OPERAND (t
, 1)))
7500 return TREE_OPERAND (t
, 0);
7501 else if (TREE_CODE (t
) == MEM_REF
7502 && TREE_CODE (TREE_OPERAND (t
, 0)) == INTEGER_CST
)
7503 return fold_binary (POINTER_PLUS_EXPR
, ptrtype
,
7504 TREE_OPERAND (t
, 0),
7505 convert_to_ptrofftype (TREE_OPERAND (t
, 1)));
7506 else if (TREE_CODE (t
) == VIEW_CONVERT_EXPR
)
7508 t
= build_fold_addr_expr_loc (loc
, TREE_OPERAND (t
, 0));
7510 if (TREE_TYPE (t
) != ptrtype
)
7511 t
= fold_convert_loc (loc
, ptrtype
, t
);
7514 t
= build1_loc (loc
, ADDR_EXPR
, ptrtype
, t
);
7519 /* Build an expression for the address of T. */
7522 build_fold_addr_expr_loc (location_t loc
, tree t
)
7524 tree ptrtype
= build_pointer_type (TREE_TYPE (t
));
7526 return build_fold_addr_expr_with_type_loc (loc
, t
, ptrtype
);
7529 /* Fold a unary expression of code CODE and type TYPE with operand
7530 OP0. Return the folded expression if folding is successful.
7531 Otherwise, return NULL_TREE. */
7534 fold_unary_loc (location_t loc
, enum tree_code code
, tree type
, tree op0
)
7538 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
7540 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
7541 && TREE_CODE_LENGTH (code
) == 1);
7546 if (CONVERT_EXPR_CODE_P (code
)
7547 || code
== FLOAT_EXPR
|| code
== ABS_EXPR
|| code
== NEGATE_EXPR
)
7549 /* Don't use STRIP_NOPS, because signedness of argument type
7551 STRIP_SIGN_NOPS (arg0
);
7555 /* Strip any conversions that don't change the mode. This
7556 is safe for every expression, except for a comparison
7557 expression because its signedness is derived from its
7560 Note that this is done as an internal manipulation within
7561 the constant folder, in order to find the simplest
7562 representation of the arguments so that their form can be
7563 studied. In any cases, the appropriate type conversions
7564 should be put back in the tree that will get out of the
7569 if (CONSTANT_CLASS_P (arg0
))
7571 tree tem
= const_unop (code
, type
, arg0
);
7574 if (TREE_TYPE (tem
) != type
)
7575 tem
= fold_convert_loc (loc
, type
, tem
);
7581 tem
= generic_simplify (loc
, code
, type
, op0
);
7585 if (TREE_CODE_CLASS (code
) == tcc_unary
)
7587 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
7588 return build2 (COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7589 fold_build1_loc (loc
, code
, type
,
7590 fold_convert_loc (loc
, TREE_TYPE (op0
),
7591 TREE_OPERAND (arg0
, 1))));
7592 else if (TREE_CODE (arg0
) == COND_EXPR
)
7594 tree arg01
= TREE_OPERAND (arg0
, 1);
7595 tree arg02
= TREE_OPERAND (arg0
, 2);
7596 if (! VOID_TYPE_P (TREE_TYPE (arg01
)))
7597 arg01
= fold_build1_loc (loc
, code
, type
,
7598 fold_convert_loc (loc
,
7599 TREE_TYPE (op0
), arg01
));
7600 if (! VOID_TYPE_P (TREE_TYPE (arg02
)))
7601 arg02
= fold_build1_loc (loc
, code
, type
,
7602 fold_convert_loc (loc
,
7603 TREE_TYPE (op0
), arg02
));
7604 tem
= fold_build3_loc (loc
, COND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
7607 /* If this was a conversion, and all we did was to move into
7608 inside the COND_EXPR, bring it back out. But leave it if
7609 it is a conversion from integer to integer and the
7610 result precision is no wider than a word since such a
7611 conversion is cheap and may be optimized away by combine,
7612 while it couldn't if it were outside the COND_EXPR. Then return
7613 so we don't get into an infinite recursion loop taking the
7614 conversion out and then back in. */
7616 if ((CONVERT_EXPR_CODE_P (code
)
7617 || code
== NON_LVALUE_EXPR
)
7618 && TREE_CODE (tem
) == COND_EXPR
7619 && TREE_CODE (TREE_OPERAND (tem
, 1)) == code
7620 && TREE_CODE (TREE_OPERAND (tem
, 2)) == code
7621 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 1))
7622 && ! VOID_TYPE_P (TREE_OPERAND (tem
, 2))
7623 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))
7624 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 2), 0)))
7625 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
7627 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem
, 1), 0))))
7628 && TYPE_PRECISION (TREE_TYPE (tem
)) <= BITS_PER_WORD
)
7629 || flag_syntax_only
))
7630 tem
= build1_loc (loc
, code
, type
,
7632 TREE_TYPE (TREE_OPERAND
7633 (TREE_OPERAND (tem
, 1), 0)),
7634 TREE_OPERAND (tem
, 0),
7635 TREE_OPERAND (TREE_OPERAND (tem
, 1), 0),
7636 TREE_OPERAND (TREE_OPERAND (tem
, 2),
7644 case NON_LVALUE_EXPR
:
7645 if (!maybe_lvalue_p (op0
))
7646 return fold_convert_loc (loc
, type
, op0
);
7651 case FIX_TRUNC_EXPR
:
7652 if (COMPARISON_CLASS_P (op0
))
7654 /* If we have (type) (a CMP b) and type is an integral type, return
7655 new expression involving the new type. Canonicalize
7656 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7658 Do not fold the result as that would not simplify further, also
7659 folding again results in recursions. */
7660 if (TREE_CODE (type
) == BOOLEAN_TYPE
)
7661 return build2_loc (loc
, TREE_CODE (op0
), type
,
7662 TREE_OPERAND (op0
, 0),
7663 TREE_OPERAND (op0
, 1));
7664 else if (!INTEGRAL_TYPE_P (type
) && !VOID_TYPE_P (type
)
7665 && TREE_CODE (type
) != VECTOR_TYPE
)
7666 return build3_loc (loc
, COND_EXPR
, type
, op0
,
7667 constant_boolean_node (true, type
),
7668 constant_boolean_node (false, type
));
7671 /* Handle (T *)&A.B.C for A being of type T and B and C
7672 living at offset zero. This occurs frequently in
7673 C++ upcasting and then accessing the base. */
7674 if (TREE_CODE (op0
) == ADDR_EXPR
7675 && POINTER_TYPE_P (type
)
7676 && handled_component_p (TREE_OPERAND (op0
, 0)))
7678 HOST_WIDE_INT bitsize
, bitpos
;
7681 int unsignedp
, volatilep
;
7682 tree base
= TREE_OPERAND (op0
, 0);
7683 base
= get_inner_reference (base
, &bitsize
, &bitpos
, &offset
,
7684 &mode
, &unsignedp
, &volatilep
, false);
7685 /* If the reference was to a (constant) zero offset, we can use
7686 the address of the base if it has the same base type
7687 as the result type and the pointer type is unqualified. */
7688 if (! offset
&& bitpos
== 0
7689 && (TYPE_MAIN_VARIANT (TREE_TYPE (type
))
7690 == TYPE_MAIN_VARIANT (TREE_TYPE (base
)))
7691 && TYPE_QUALS (type
) == TYPE_UNQUALIFIED
)
7692 return fold_convert_loc (loc
, type
,
7693 build_fold_addr_expr_loc (loc
, base
));
7696 if (TREE_CODE (op0
) == MODIFY_EXPR
7697 && TREE_CONSTANT (TREE_OPERAND (op0
, 1))
7698 /* Detect assigning a bitfield. */
7699 && !(TREE_CODE (TREE_OPERAND (op0
, 0)) == COMPONENT_REF
7701 (TREE_OPERAND (TREE_OPERAND (op0
, 0), 1))))
7703 /* Don't leave an assignment inside a conversion
7704 unless assigning a bitfield. */
7705 tem
= fold_build1_loc (loc
, code
, type
, TREE_OPERAND (op0
, 1));
7706 /* First do the assignment, then return converted constant. */
7707 tem
= build2_loc (loc
, COMPOUND_EXPR
, TREE_TYPE (tem
), op0
, tem
);
7708 TREE_NO_WARNING (tem
) = 1;
7709 TREE_USED (tem
) = 1;
7713 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7714 constants (if x has signed type, the sign bit cannot be set
7715 in c). This folds extension into the BIT_AND_EXPR.
7716 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7717 very likely don't have maximal range for their precision and this
7718 transformation effectively doesn't preserve non-maximal ranges. */
7719 if (TREE_CODE (type
) == INTEGER_TYPE
7720 && TREE_CODE (op0
) == BIT_AND_EXPR
7721 && TREE_CODE (TREE_OPERAND (op0
, 1)) == INTEGER_CST
)
7723 tree and_expr
= op0
;
7724 tree and0
= TREE_OPERAND (and_expr
, 0);
7725 tree and1
= TREE_OPERAND (and_expr
, 1);
7728 if (TYPE_UNSIGNED (TREE_TYPE (and_expr
))
7729 || (TYPE_PRECISION (type
)
7730 <= TYPE_PRECISION (TREE_TYPE (and_expr
))))
7732 else if (TYPE_PRECISION (TREE_TYPE (and1
))
7733 <= HOST_BITS_PER_WIDE_INT
7734 && tree_fits_uhwi_p (and1
))
7736 unsigned HOST_WIDE_INT cst
;
7738 cst
= tree_to_uhwi (and1
);
7739 cst
&= HOST_WIDE_INT_M1U
7740 << (TYPE_PRECISION (TREE_TYPE (and1
)) - 1);
7741 change
= (cst
== 0);
7743 && !flag_syntax_only
7744 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0
)))
7747 tree uns
= unsigned_type_for (TREE_TYPE (and0
));
7748 and0
= fold_convert_loc (loc
, uns
, and0
);
7749 and1
= fold_convert_loc (loc
, uns
, and1
);
7754 tem
= force_fit_type (type
, wi::to_widest (and1
), 0,
7755 TREE_OVERFLOW (and1
));
7756 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
7757 fold_convert_loc (loc
, type
, and0
), tem
);
7761 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7762 when one of the new casts will fold away. Conservatively we assume
7763 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7764 if (POINTER_TYPE_P (type
)
7765 && TREE_CODE (arg0
) == POINTER_PLUS_EXPR
7766 && (!TYPE_RESTRICT (type
) || TYPE_RESTRICT (TREE_TYPE (arg0
)))
7767 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
7768 || TREE_CODE (TREE_OPERAND (arg0
, 0)) == NOP_EXPR
7769 || TREE_CODE (TREE_OPERAND (arg0
, 1)) == NOP_EXPR
))
7771 tree arg00
= TREE_OPERAND (arg0
, 0);
7772 tree arg01
= TREE_OPERAND (arg0
, 1);
7774 return fold_build_pointer_plus_loc
7775 (loc
, fold_convert_loc (loc
, type
, arg00
), arg01
);
7778 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7779 of the same precision, and X is an integer type not narrower than
7780 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7781 if (INTEGRAL_TYPE_P (type
)
7782 && TREE_CODE (op0
) == BIT_NOT_EXPR
7783 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7784 && CONVERT_EXPR_P (TREE_OPERAND (op0
, 0))
7785 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (op0
)))
7787 tem
= TREE_OPERAND (TREE_OPERAND (op0
, 0), 0);
7788 if (INTEGRAL_TYPE_P (TREE_TYPE (tem
))
7789 && TYPE_PRECISION (type
) <= TYPE_PRECISION (TREE_TYPE (tem
)))
7790 return fold_build1_loc (loc
, BIT_NOT_EXPR
, type
,
7791 fold_convert_loc (loc
, type
, tem
));
7794 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7795 type of X and Y (integer types only). */
7796 if (INTEGRAL_TYPE_P (type
)
7797 && TREE_CODE (op0
) == MULT_EXPR
7798 && INTEGRAL_TYPE_P (TREE_TYPE (op0
))
7799 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
7801 /* Be careful not to introduce new overflows. */
7803 if (TYPE_OVERFLOW_WRAPS (type
))
7806 mult_type
= unsigned_type_for (type
);
7808 if (TYPE_PRECISION (mult_type
) < TYPE_PRECISION (TREE_TYPE (op0
)))
7810 tem
= fold_build2_loc (loc
, MULT_EXPR
, mult_type
,
7811 fold_convert_loc (loc
, mult_type
,
7812 TREE_OPERAND (op0
, 0)),
7813 fold_convert_loc (loc
, mult_type
,
7814 TREE_OPERAND (op0
, 1)));
7815 return fold_convert_loc (loc
, type
, tem
);
7821 case VIEW_CONVERT_EXPR
:
7822 if (TREE_CODE (op0
) == MEM_REF
)
7823 return fold_build2_loc (loc
, MEM_REF
, type
,
7824 TREE_OPERAND (op0
, 0), TREE_OPERAND (op0
, 1));
7829 tem
= fold_negate_expr (loc
, arg0
);
7831 return fold_convert_loc (loc
, type
, tem
);
7835 /* Convert fabs((double)float) into (double)fabsf(float). */
7836 if (TREE_CODE (arg0
) == NOP_EXPR
7837 && TREE_CODE (type
) == REAL_TYPE
)
7839 tree targ0
= strip_float_extensions (arg0
);
7841 return fold_convert_loc (loc
, type
,
7842 fold_build1_loc (loc
, ABS_EXPR
,
7849 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7850 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
7851 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
7852 fold_convert_loc (loc
, type
,
7853 TREE_OPERAND (arg0
, 0)))))
7854 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
, tem
,
7855 fold_convert_loc (loc
, type
,
7856 TREE_OPERAND (arg0
, 1)));
7857 else if (TREE_CODE (arg0
) == BIT_XOR_EXPR
7858 && (tem
= fold_unary_loc (loc
, BIT_NOT_EXPR
, type
,
7859 fold_convert_loc (loc
, type
,
7860 TREE_OPERAND (arg0
, 1)))))
7861 return fold_build2_loc (loc
, BIT_XOR_EXPR
, type
,
7862 fold_convert_loc (loc
, type
,
7863 TREE_OPERAND (arg0
, 0)), tem
);
7867 case TRUTH_NOT_EXPR
:
7868 /* Note that the operand of this must be an int
7869 and its values must be 0 or 1.
7870 ("true" is a fixed value perhaps depending on the language,
7871 but we don't handle values other than 1 correctly yet.) */
7872 tem
= fold_truth_not_expr (loc
, arg0
);
7875 return fold_convert_loc (loc
, type
, tem
);
7878 /* Fold *&X to X if X is an lvalue. */
7879 if (TREE_CODE (op0
) == ADDR_EXPR
)
7881 tree op00
= TREE_OPERAND (op0
, 0);
7882 if ((TREE_CODE (op00
) == VAR_DECL
7883 || TREE_CODE (op00
) == PARM_DECL
7884 || TREE_CODE (op00
) == RESULT_DECL
)
7885 && !TREE_READONLY (op00
))
7892 } /* switch (code) */
7896 /* If the operation was a conversion do _not_ mark a resulting constant
7897 with TREE_OVERFLOW if the original constant was not. These conversions
7898 have implementation defined behavior and retaining the TREE_OVERFLOW
7899 flag here would confuse later passes such as VRP. */
7901 fold_unary_ignore_overflow_loc (location_t loc
, enum tree_code code
,
7902 tree type
, tree op0
)
7904 tree res
= fold_unary_loc (loc
, code
, type
, op0
);
7906 && TREE_CODE (res
) == INTEGER_CST
7907 && TREE_CODE (op0
) == INTEGER_CST
7908 && CONVERT_EXPR_CODE_P (code
))
7909 TREE_OVERFLOW (res
) = TREE_OVERFLOW (op0
);
7914 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7915 operands OP0 and OP1. LOC is the location of the resulting expression.
7916 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7917 Return the folded expression if folding is successful. Otherwise,
7918 return NULL_TREE. */
7920 fold_truth_andor (location_t loc
, enum tree_code code
, tree type
,
7921 tree arg0
, tree arg1
, tree op0
, tree op1
)
7925 /* We only do these simplifications if we are optimizing. */
7929 /* Check for things like (A || B) && (A || C). We can convert this
7930 to A || (B && C). Note that either operator can be any of the four
7931 truth and/or operations and the transformation will still be
7932 valid. Also note that we only care about order for the
7933 ANDIF and ORIF operators. If B contains side effects, this
7934 might change the truth-value of A. */
7935 if (TREE_CODE (arg0
) == TREE_CODE (arg1
)
7936 && (TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
7937 || TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
7938 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
7939 || TREE_CODE (arg0
) == TRUTH_OR_EXPR
)
7940 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0
, 1)))
7942 tree a00
= TREE_OPERAND (arg0
, 0);
7943 tree a01
= TREE_OPERAND (arg0
, 1);
7944 tree a10
= TREE_OPERAND (arg1
, 0);
7945 tree a11
= TREE_OPERAND (arg1
, 1);
7946 int commutative
= ((TREE_CODE (arg0
) == TRUTH_OR_EXPR
7947 || TREE_CODE (arg0
) == TRUTH_AND_EXPR
)
7948 && (code
== TRUTH_AND_EXPR
7949 || code
== TRUTH_OR_EXPR
));
7951 if (operand_equal_p (a00
, a10
, 0))
7952 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
7953 fold_build2_loc (loc
, code
, type
, a01
, a11
));
7954 else if (commutative
&& operand_equal_p (a00
, a11
, 0))
7955 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a00
,
7956 fold_build2_loc (loc
, code
, type
, a01
, a10
));
7957 else if (commutative
&& operand_equal_p (a01
, a10
, 0))
7958 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
, a01
,
7959 fold_build2_loc (loc
, code
, type
, a00
, a11
));
7961 /* This case if tricky because we must either have commutative
7962 operators or else A10 must not have side-effects. */
7964 else if ((commutative
|| ! TREE_SIDE_EFFECTS (a10
))
7965 && operand_equal_p (a01
, a11
, 0))
7966 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
7967 fold_build2_loc (loc
, code
, type
, a00
, a10
),
7971 /* See if we can build a range comparison. */
7972 if (0 != (tem
= fold_range_test (loc
, code
, type
, op0
, op1
)))
7975 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ORIF_EXPR
)
7976 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg0
) == TRUTH_ANDIF_EXPR
))
7978 tem
= merge_truthop_with_opposite_arm (loc
, arg0
, arg1
, true);
7980 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
7983 if ((code
== TRUTH_ANDIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ORIF_EXPR
)
7984 || (code
== TRUTH_ORIF_EXPR
&& TREE_CODE (arg1
) == TRUTH_ANDIF_EXPR
))
7986 tem
= merge_truthop_with_opposite_arm (loc
, arg1
, arg0
, false);
7988 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
7991 /* Check for the possibility of merging component references. If our
7992 lhs is another similar operation, try to merge its rhs with our
7993 rhs. Then try to merge our lhs and rhs. */
7994 if (TREE_CODE (arg0
) == code
7995 && 0 != (tem
= fold_truth_andor_1 (loc
, code
, type
,
7996 TREE_OPERAND (arg0
, 1), arg1
)))
7997 return fold_build2_loc (loc
, code
, type
, TREE_OPERAND (arg0
, 0), tem
);
7999 if ((tem
= fold_truth_andor_1 (loc
, code
, type
, arg0
, arg1
)) != 0)
8002 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8003 && (code
== TRUTH_AND_EXPR
8004 || code
== TRUTH_ANDIF_EXPR
8005 || code
== TRUTH_OR_EXPR
8006 || code
== TRUTH_ORIF_EXPR
))
8008 enum tree_code ncode
, icode
;
8010 ncode
= (code
== TRUTH_ANDIF_EXPR
|| code
== TRUTH_AND_EXPR
)
8011 ? TRUTH_AND_EXPR
: TRUTH_OR_EXPR
;
8012 icode
= ncode
== TRUTH_AND_EXPR
? TRUTH_ANDIF_EXPR
: TRUTH_ORIF_EXPR
;
8014 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8015 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8016 We don't want to pack more than two leafs to a non-IF AND/OR
8018 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8019 equal to IF-CODE, then we don't want to add right-hand operand.
8020 If the inner right-hand side of left-hand operand has
8021 side-effects, or isn't simple, then we can't add to it,
8022 as otherwise we might destroy if-sequence. */
8023 if (TREE_CODE (arg0
) == icode
8024 && simple_operand_p_2 (arg1
)
8025 /* Needed for sequence points to handle trappings, and
8027 && simple_operand_p_2 (TREE_OPERAND (arg0
, 1)))
8029 tem
= fold_build2_loc (loc
, ncode
, type
, TREE_OPERAND (arg0
, 1),
8031 return fold_build2_loc (loc
, icode
, type
, TREE_OPERAND (arg0
, 0),
8034 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8035 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8036 else if (TREE_CODE (arg1
) == icode
8037 && simple_operand_p_2 (arg0
)
8038 /* Needed for sequence points to handle trappings, and
8040 && simple_operand_p_2 (TREE_OPERAND (arg1
, 0)))
8042 tem
= fold_build2_loc (loc
, ncode
, type
,
8043 arg0
, TREE_OPERAND (arg1
, 0));
8044 return fold_build2_loc (loc
, icode
, type
, tem
,
8045 TREE_OPERAND (arg1
, 1));
8047 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8049 For sequence point consistancy, we need to check for trapping,
8050 and side-effects. */
8051 else if (code
== icode
&& simple_operand_p_2 (arg0
)
8052 && simple_operand_p_2 (arg1
))
8053 return fold_build2_loc (loc
, ncode
, type
, arg0
, arg1
);
8059 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8060 by changing CODE to reduce the magnitude of constants involved in
8061 ARG0 of the comparison.
8062 Returns a canonicalized comparison tree if a simplification was
8063 possible, otherwise returns NULL_TREE.
8064 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8065 valid if signed overflow is undefined. */
8068 maybe_canonicalize_comparison_1 (location_t loc
, enum tree_code code
, tree type
,
8069 tree arg0
, tree arg1
,
8070 bool *strict_overflow_p
)
8072 enum tree_code code0
= TREE_CODE (arg0
);
8073 tree t
, cst0
= NULL_TREE
;
8076 /* Match A +- CST code arg1. We can change this only if overflow
8078 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8079 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
)))
8080 /* In principle pointers also have undefined overflow behavior,
8081 but that causes problems elsewhere. */
8082 && !POINTER_TYPE_P (TREE_TYPE (arg0
))
8083 && (code0
== MINUS_EXPR
8084 || code0
== PLUS_EXPR
)
8085 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
))
8088 /* Identify the constant in arg0 and its sign. */
8089 cst0
= TREE_OPERAND (arg0
, 1);
8090 sgn0
= tree_int_cst_sgn (cst0
);
8092 /* Overflowed constants and zero will cause problems. */
8093 if (integer_zerop (cst0
)
8094 || TREE_OVERFLOW (cst0
))
8097 /* See if we can reduce the magnitude of the constant in
8098 arg0 by changing the comparison code. */
8099 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8101 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8103 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8104 else if (code
== GT_EXPR
8105 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8107 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8108 else if (code
== LE_EXPR
8109 && code0
== ((sgn0
== -1) ? MINUS_EXPR
: PLUS_EXPR
))
8111 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8112 else if (code
== GE_EXPR
8113 && code0
== ((sgn0
== -1) ? PLUS_EXPR
: MINUS_EXPR
))
8117 *strict_overflow_p
= true;
8119 /* Now build the constant reduced in magnitude. But not if that
8120 would produce one outside of its types range. */
8121 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0
))
8123 && TYPE_MIN_VALUE (TREE_TYPE (cst0
))
8124 && tree_int_cst_equal (cst0
, TYPE_MIN_VALUE (TREE_TYPE (cst0
))))
8126 && TYPE_MAX_VALUE (TREE_TYPE (cst0
))
8127 && tree_int_cst_equal (cst0
, TYPE_MAX_VALUE (TREE_TYPE (cst0
))))))
8130 t
= int_const_binop (sgn0
== -1 ? PLUS_EXPR
: MINUS_EXPR
,
8131 cst0
, build_int_cst (TREE_TYPE (cst0
), 1));
8132 t
= fold_build2_loc (loc
, code0
, TREE_TYPE (arg0
), TREE_OPERAND (arg0
, 0), t
);
8133 t
= fold_convert (TREE_TYPE (arg1
), t
);
8135 return fold_build2_loc (loc
, code
, type
, t
, arg1
);
8138 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8139 overflow further. Try to decrease the magnitude of constants involved
8140 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8141 and put sole constants at the second argument position.
8142 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8145 maybe_canonicalize_comparison (location_t loc
, enum tree_code code
, tree type
,
8146 tree arg0
, tree arg1
)
8149 bool strict_overflow_p
;
8150 const char * const warnmsg
= G_("assuming signed overflow does not occur "
8151 "when reducing constant in comparison");
8153 /* Try canonicalization by simplifying arg0. */
8154 strict_overflow_p
= false;
8155 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg0
, arg1
,
8156 &strict_overflow_p
);
8159 if (strict_overflow_p
)
8160 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8164 /* Try canonicalization by simplifying arg1 using the swapped
8166 code
= swap_tree_comparison (code
);
8167 strict_overflow_p
= false;
8168 t
= maybe_canonicalize_comparison_1 (loc
, code
, type
, arg1
, arg0
,
8169 &strict_overflow_p
);
8170 if (t
&& strict_overflow_p
)
8171 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_MAGNITUDE
);
8175 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8176 space. This is used to avoid issuing overflow warnings for
8177 expressions like &p->x which can not wrap. */
8180 pointer_may_wrap_p (tree base
, tree offset
, HOST_WIDE_INT bitpos
)
8182 if (!POINTER_TYPE_P (TREE_TYPE (base
)))
8189 int precision
= TYPE_PRECISION (TREE_TYPE (base
));
8190 if (offset
== NULL_TREE
)
8191 wi_offset
= wi::zero (precision
);
8192 else if (TREE_CODE (offset
) != INTEGER_CST
|| TREE_OVERFLOW (offset
))
8198 wide_int units
= wi::shwi (bitpos
/ BITS_PER_UNIT
, precision
);
8199 wide_int total
= wi::add (wi_offset
, units
, UNSIGNED
, &overflow
);
8203 if (!wi::fits_uhwi_p (total
))
8206 HOST_WIDE_INT size
= int_size_in_bytes (TREE_TYPE (TREE_TYPE (base
)));
8210 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8212 if (TREE_CODE (base
) == ADDR_EXPR
)
8214 HOST_WIDE_INT base_size
;
8216 base_size
= int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base
, 0)));
8217 if (base_size
> 0 && size
< base_size
)
8221 return total
.to_uhwi () > (unsigned HOST_WIDE_INT
) size
;
8224 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
8225 kind INTEGER_CST. This makes sure to properly sign-extend the
8228 static HOST_WIDE_INT
8229 size_low_cst (const_tree t
)
8231 HOST_WIDE_INT w
= TREE_INT_CST_ELT (t
, 0);
8232 int prec
= TYPE_PRECISION (TREE_TYPE (t
));
8233 if (prec
< HOST_BITS_PER_WIDE_INT
)
8234 return sext_hwi (w
, prec
);
8238 /* Subroutine of fold_binary. This routine performs all of the
8239 transformations that are common to the equality/inequality
8240 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8241 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8242 fold_binary should call fold_binary. Fold a comparison with
8243 tree code CODE and type TYPE with operands OP0 and OP1. Return
8244 the folded comparison or NULL_TREE. */
8247 fold_comparison (location_t loc
, enum tree_code code
, tree type
,
8250 const bool equality_code
= (code
== EQ_EXPR
|| code
== NE_EXPR
);
8251 tree arg0
, arg1
, tem
;
8256 STRIP_SIGN_NOPS (arg0
);
8257 STRIP_SIGN_NOPS (arg1
);
8259 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8260 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8262 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8263 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))))
8264 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8265 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1))
8266 && TREE_CODE (arg1
) == INTEGER_CST
8267 && !TREE_OVERFLOW (arg1
))
8269 const enum tree_code
8270 reverse_op
= TREE_CODE (arg0
) == PLUS_EXPR
? MINUS_EXPR
: PLUS_EXPR
;
8271 tree const1
= TREE_OPERAND (arg0
, 1);
8272 tree const2
= fold_convert_loc (loc
, TREE_TYPE (const1
), arg1
);
8273 tree variable
= TREE_OPERAND (arg0
, 0);
8274 tree new_const
= int_const_binop (reverse_op
, const2
, const1
);
8276 /* If the constant operation overflowed this can be
8277 simplified as a comparison against INT_MAX/INT_MIN. */
8278 if (TREE_OVERFLOW (new_const
)
8279 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
8281 int const1_sgn
= tree_int_cst_sgn (const1
);
8282 enum tree_code code2
= code
;
8284 /* Get the sign of the constant on the lhs if the
8285 operation were VARIABLE + CONST1. */
8286 if (TREE_CODE (arg0
) == MINUS_EXPR
)
8287 const1_sgn
= -const1_sgn
;
8289 /* The sign of the constant determines if we overflowed
8290 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8291 Canonicalize to the INT_MIN overflow by swapping the comparison
8293 if (const1_sgn
== -1)
8294 code2
= swap_tree_comparison (code
);
8296 /* We now can look at the canonicalized case
8297 VARIABLE + 1 CODE2 INT_MIN
8298 and decide on the result. */
8305 omit_one_operand_loc (loc
, type
, boolean_false_node
, variable
);
8311 omit_one_operand_loc (loc
, type
, boolean_true_node
, variable
);
8320 fold_overflow_warning ("assuming signed overflow does not occur "
8321 "when changing X +- C1 cmp C2 to "
8323 WARN_STRICT_OVERFLOW_COMPARISON
);
8324 return fold_build2_loc (loc
, code
, type
, variable
, new_const
);
8328 /* For comparisons of pointers we can decompose it to a compile time
8329 comparison of the base objects and the offsets into the object.
8330 This requires at least one operand being an ADDR_EXPR or a
8331 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8332 if (POINTER_TYPE_P (TREE_TYPE (arg0
))
8333 && (TREE_CODE (arg0
) == ADDR_EXPR
8334 || TREE_CODE (arg1
) == ADDR_EXPR
8335 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
8336 || TREE_CODE (arg1
) == POINTER_PLUS_EXPR
))
8338 tree base0
, base1
, offset0
= NULL_TREE
, offset1
= NULL_TREE
;
8339 HOST_WIDE_INT bitsize
, bitpos0
= 0, bitpos1
= 0;
8341 int volatilep
, unsignedp
;
8342 bool indirect_base0
= false, indirect_base1
= false;
8344 /* Get base and offset for the access. Strip ADDR_EXPR for
8345 get_inner_reference, but put it back by stripping INDIRECT_REF
8346 off the base object if possible. indirect_baseN will be true
8347 if baseN is not an address but refers to the object itself. */
8349 if (TREE_CODE (arg0
) == ADDR_EXPR
)
8351 base0
= get_inner_reference (TREE_OPERAND (arg0
, 0),
8352 &bitsize
, &bitpos0
, &offset0
, &mode
,
8353 &unsignedp
, &volatilep
, false);
8354 if (TREE_CODE (base0
) == INDIRECT_REF
)
8355 base0
= TREE_OPERAND (base0
, 0);
8357 indirect_base0
= true;
8359 else if (TREE_CODE (arg0
) == POINTER_PLUS_EXPR
)
8361 base0
= TREE_OPERAND (arg0
, 0);
8362 STRIP_SIGN_NOPS (base0
);
8363 if (TREE_CODE (base0
) == ADDR_EXPR
)
8365 base0
= TREE_OPERAND (base0
, 0);
8366 indirect_base0
= true;
8368 offset0
= TREE_OPERAND (arg0
, 1);
8369 if (tree_fits_shwi_p (offset0
))
8371 HOST_WIDE_INT off
= size_low_cst (offset0
);
8372 if ((HOST_WIDE_INT
) (((unsigned HOST_WIDE_INT
) off
)
8374 / BITS_PER_UNIT
== (HOST_WIDE_INT
) off
)
8376 bitpos0
= off
* BITS_PER_UNIT
;
8377 offset0
= NULL_TREE
;
8383 if (TREE_CODE (arg1
) == ADDR_EXPR
)
8385 base1
= get_inner_reference (TREE_OPERAND (arg1
, 0),
8386 &bitsize
, &bitpos1
, &offset1
, &mode
,
8387 &unsignedp
, &volatilep
, false);
8388 if (TREE_CODE (base1
) == INDIRECT_REF
)
8389 base1
= TREE_OPERAND (base1
, 0);
8391 indirect_base1
= true;
8393 else if (TREE_CODE (arg1
) == POINTER_PLUS_EXPR
)
8395 base1
= TREE_OPERAND (arg1
, 0);
8396 STRIP_SIGN_NOPS (base1
);
8397 if (TREE_CODE (base1
) == ADDR_EXPR
)
8399 base1
= TREE_OPERAND (base1
, 0);
8400 indirect_base1
= true;
8402 offset1
= TREE_OPERAND (arg1
, 1);
8403 if (tree_fits_shwi_p (offset1
))
8405 HOST_WIDE_INT off
= size_low_cst (offset1
);
8406 if ((HOST_WIDE_INT
) (((unsigned HOST_WIDE_INT
) off
)
8408 / BITS_PER_UNIT
== (HOST_WIDE_INT
) off
)
8410 bitpos1
= off
* BITS_PER_UNIT
;
8411 offset1
= NULL_TREE
;
8416 /* If we have equivalent bases we might be able to simplify. */
8417 if (indirect_base0
== indirect_base1
8418 && operand_equal_p (base0
, base1
,
8419 indirect_base0
? OEP_ADDRESS_OF
: 0))
8421 /* We can fold this expression to a constant if the non-constant
8422 offset parts are equal. */
8423 if ((offset0
== offset1
8424 || (offset0
&& offset1
8425 && operand_equal_p (offset0
, offset1
, 0)))
8428 || (indirect_base0
&& DECL_P (base0
))
8429 || POINTER_TYPE_OVERFLOW_UNDEFINED
))
8433 && bitpos0
!= bitpos1
8434 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8435 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8436 fold_overflow_warning (("assuming pointer wraparound does not "
8437 "occur when comparing P +- C1 with "
8439 WARN_STRICT_OVERFLOW_CONDITIONAL
);
8444 return constant_boolean_node (bitpos0
== bitpos1
, type
);
8446 return constant_boolean_node (bitpos0
!= bitpos1
, type
);
8448 return constant_boolean_node (bitpos0
< bitpos1
, type
);
8450 return constant_boolean_node (bitpos0
<= bitpos1
, type
);
8452 return constant_boolean_node (bitpos0
>= bitpos1
, type
);
8454 return constant_boolean_node (bitpos0
> bitpos1
, type
);
8458 /* We can simplify the comparison to a comparison of the variable
8459 offset parts if the constant offset parts are equal.
8460 Be careful to use signed sizetype here because otherwise we
8461 mess with array offsets in the wrong way. This is possible
8462 because pointer arithmetic is restricted to retain within an
8463 object and overflow on pointer differences is undefined as of
8464 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8465 else if (bitpos0
== bitpos1
8467 || (indirect_base0
&& DECL_P (base0
))
8468 || POINTER_TYPE_OVERFLOW_UNDEFINED
))
8470 /* By converting to signed sizetype we cover middle-end pointer
8471 arithmetic which operates on unsigned pointer types of size
8472 type size and ARRAY_REF offsets which are properly sign or
8473 zero extended from their type in case it is narrower than
8475 if (offset0
== NULL_TREE
)
8476 offset0
= build_int_cst (ssizetype
, 0);
8478 offset0
= fold_convert_loc (loc
, ssizetype
, offset0
);
8479 if (offset1
== NULL_TREE
)
8480 offset1
= build_int_cst (ssizetype
, 0);
8482 offset1
= fold_convert_loc (loc
, ssizetype
, offset1
);
8485 && (pointer_may_wrap_p (base0
, offset0
, bitpos0
)
8486 || pointer_may_wrap_p (base1
, offset1
, bitpos1
)))
8487 fold_overflow_warning (("assuming pointer wraparound does not "
8488 "occur when comparing P +- C1 with "
8490 WARN_STRICT_OVERFLOW_COMPARISON
);
8492 return fold_build2_loc (loc
, code
, type
, offset0
, offset1
);
8495 /* For equal offsets we can simplify to a comparison of the
8497 else if (bitpos0
== bitpos1
8499 ? base0
!= TREE_OPERAND (arg0
, 0) : base0
!= arg0
)
8501 ? base1
!= TREE_OPERAND (arg1
, 0) : base1
!= arg1
)
8502 && ((offset0
== offset1
)
8503 || (offset0
&& offset1
8504 && operand_equal_p (offset0
, offset1
, 0))))
8507 base0
= build_fold_addr_expr_loc (loc
, base0
);
8509 base1
= build_fold_addr_expr_loc (loc
, base1
);
8510 return fold_build2_loc (loc
, code
, type
, base0
, base1
);
8514 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8515 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8516 the resulting offset is smaller in absolute value than the
8517 original one and has the same sign. */
8518 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
8519 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0
))
8520 && (TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
8521 && (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8522 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1)))
8523 && (TREE_CODE (arg1
) == PLUS_EXPR
|| TREE_CODE (arg1
) == MINUS_EXPR
)
8524 && (TREE_CODE (TREE_OPERAND (arg1
, 1)) == INTEGER_CST
8525 && !TREE_OVERFLOW (TREE_OPERAND (arg1
, 1))))
8527 tree const1
= TREE_OPERAND (arg0
, 1);
8528 tree const2
= TREE_OPERAND (arg1
, 1);
8529 tree variable1
= TREE_OPERAND (arg0
, 0);
8530 tree variable2
= TREE_OPERAND (arg1
, 0);
8532 const char * const warnmsg
= G_("assuming signed overflow does not "
8533 "occur when combining constants around "
8536 /* Put the constant on the side where it doesn't overflow and is
8537 of lower absolute value and of same sign than before. */
8538 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8539 ? MINUS_EXPR
: PLUS_EXPR
,
8541 if (!TREE_OVERFLOW (cst
)
8542 && tree_int_cst_compare (const2
, cst
) == tree_int_cst_sgn (const2
)
8543 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const2
))
8545 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
8546 return fold_build2_loc (loc
, code
, type
,
8548 fold_build2_loc (loc
, TREE_CODE (arg1
),
8553 cst
= int_const_binop (TREE_CODE (arg0
) == TREE_CODE (arg1
)
8554 ? MINUS_EXPR
: PLUS_EXPR
,
8556 if (!TREE_OVERFLOW (cst
)
8557 && tree_int_cst_compare (const1
, cst
) == tree_int_cst_sgn (const1
)
8558 && tree_int_cst_sgn (cst
) == tree_int_cst_sgn (const1
))
8560 fold_overflow_warning (warnmsg
, WARN_STRICT_OVERFLOW_COMPARISON
);
8561 return fold_build2_loc (loc
, code
, type
,
8562 fold_build2_loc (loc
, TREE_CODE (arg0
),
8569 tem
= maybe_canonicalize_comparison (loc
, code
, type
, arg0
, arg1
);
8573 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8574 constant, we can simplify it. */
8575 if (TREE_CODE (arg1
) == INTEGER_CST
8576 && (TREE_CODE (arg0
) == MIN_EXPR
8577 || TREE_CODE (arg0
) == MAX_EXPR
)
8578 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
8580 tem
= optimize_minmax_comparison (loc
, code
, type
, op0
, op1
);
8585 /* If we are comparing an expression that just has comparisons
8586 of two integer values, arithmetic expressions of those comparisons,
8587 and constants, we can simplify it. There are only three cases
8588 to check: the two values can either be equal, the first can be
8589 greater, or the second can be greater. Fold the expression for
8590 those three values. Since each value must be 0 or 1, we have
8591 eight possibilities, each of which corresponds to the constant 0
8592 or 1 or one of the six possible comparisons.
8594 This handles common cases like (a > b) == 0 but also handles
8595 expressions like ((x > y) - (y > x)) > 0, which supposedly
8596 occur in macroized code. */
8598 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) != INTEGER_CST
)
8600 tree cval1
= 0, cval2
= 0;
8603 if (twoval_comparison_p (arg0
, &cval1
, &cval2
, &save_p
)
8604 /* Don't handle degenerate cases here; they should already
8605 have been handled anyway. */
8606 && cval1
!= 0 && cval2
!= 0
8607 && ! (TREE_CONSTANT (cval1
) && TREE_CONSTANT (cval2
))
8608 && TREE_TYPE (cval1
) == TREE_TYPE (cval2
)
8609 && INTEGRAL_TYPE_P (TREE_TYPE (cval1
))
8610 && TYPE_MAX_VALUE (TREE_TYPE (cval1
))
8611 && TYPE_MAX_VALUE (TREE_TYPE (cval2
))
8612 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1
)),
8613 TYPE_MAX_VALUE (TREE_TYPE (cval2
)), 0))
8615 tree maxval
= TYPE_MAX_VALUE (TREE_TYPE (cval1
));
8616 tree minval
= TYPE_MIN_VALUE (TREE_TYPE (cval1
));
8618 /* We can't just pass T to eval_subst in case cval1 or cval2
8619 was the same as ARG1. */
8622 = fold_build2_loc (loc
, code
, type
,
8623 eval_subst (loc
, arg0
, cval1
, maxval
,
8627 = fold_build2_loc (loc
, code
, type
,
8628 eval_subst (loc
, arg0
, cval1
, maxval
,
8632 = fold_build2_loc (loc
, code
, type
,
8633 eval_subst (loc
, arg0
, cval1
, minval
,
8637 /* All three of these results should be 0 or 1. Confirm they are.
8638 Then use those values to select the proper code to use. */
8640 if (TREE_CODE (high_result
) == INTEGER_CST
8641 && TREE_CODE (equal_result
) == INTEGER_CST
8642 && TREE_CODE (low_result
) == INTEGER_CST
)
8644 /* Make a 3-bit mask with the high-order bit being the
8645 value for `>', the next for '=', and the low for '<'. */
8646 switch ((integer_onep (high_result
) * 4)
8647 + (integer_onep (equal_result
) * 2)
8648 + integer_onep (low_result
))
8652 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
8673 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
8678 tem
= save_expr (build2 (code
, type
, cval1
, cval2
));
8679 SET_EXPR_LOCATION (tem
, loc
);
8682 return fold_build2_loc (loc
, code
, type
, cval1
, cval2
);
8687 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8688 into a single range test. */
8689 if ((TREE_CODE (arg0
) == TRUNC_DIV_EXPR
8690 || TREE_CODE (arg0
) == EXACT_DIV_EXPR
)
8691 && TREE_CODE (arg1
) == INTEGER_CST
8692 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
8693 && !integer_zerop (TREE_OPERAND (arg0
, 1))
8694 && !TREE_OVERFLOW (TREE_OPERAND (arg0
, 1))
8695 && !TREE_OVERFLOW (arg1
))
8697 tem
= fold_div_compare (loc
, code
, type
, arg0
, arg1
);
8698 if (tem
!= NULL_TREE
)
8706 /* Subroutine of fold_binary. Optimize complex multiplications of the
8707 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8708 argument EXPR represents the expression "z" of type TYPE. */
8711 fold_mult_zconjz (location_t loc
, tree type
, tree expr
)
8713 tree itype
= TREE_TYPE (type
);
8714 tree rpart
, ipart
, tem
;
8716 if (TREE_CODE (expr
) == COMPLEX_EXPR
)
8718 rpart
= TREE_OPERAND (expr
, 0);
8719 ipart
= TREE_OPERAND (expr
, 1);
8721 else if (TREE_CODE (expr
) == COMPLEX_CST
)
8723 rpart
= TREE_REALPART (expr
);
8724 ipart
= TREE_IMAGPART (expr
);
8728 expr
= save_expr (expr
);
8729 rpart
= fold_build1_loc (loc
, REALPART_EXPR
, itype
, expr
);
8730 ipart
= fold_build1_loc (loc
, IMAGPART_EXPR
, itype
, expr
);
8733 rpart
= save_expr (rpart
);
8734 ipart
= save_expr (ipart
);
8735 tem
= fold_build2_loc (loc
, PLUS_EXPR
, itype
,
8736 fold_build2_loc (loc
, MULT_EXPR
, itype
, rpart
, rpart
),
8737 fold_build2_loc (loc
, MULT_EXPR
, itype
, ipart
, ipart
));
8738 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, tem
,
8739 build_zero_cst (itype
));
8743 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8744 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8747 vec_cst_ctor_to_array (tree arg
, tree
*elts
)
8749 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg
)), i
;
8751 if (TREE_CODE (arg
) == VECTOR_CST
)
8753 for (i
= 0; i
< VECTOR_CST_NELTS (arg
); ++i
)
8754 elts
[i
] = VECTOR_CST_ELT (arg
, i
);
8756 else if (TREE_CODE (arg
) == CONSTRUCTOR
)
8758 constructor_elt
*elt
;
8760 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg
), i
, elt
)
8761 if (i
>= nelts
|| TREE_CODE (TREE_TYPE (elt
->value
)) == VECTOR_TYPE
)
8764 elts
[i
] = elt
->value
;
8768 for (; i
< nelts
; i
++)
8770 = fold_convert (TREE_TYPE (TREE_TYPE (arg
)), integer_zero_node
);
8774 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8775 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8776 NULL_TREE otherwise. */
8779 fold_vec_perm (tree type
, tree arg0
, tree arg1
, const unsigned char *sel
)
8781 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
8783 bool need_ctor
= false;
8785 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)) == nelts
8786 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1
)) == nelts
);
8787 if (TREE_TYPE (TREE_TYPE (arg0
)) != TREE_TYPE (type
)
8788 || TREE_TYPE (TREE_TYPE (arg1
)) != TREE_TYPE (type
))
8791 elts
= XALLOCAVEC (tree
, nelts
* 3);
8792 if (!vec_cst_ctor_to_array (arg0
, elts
)
8793 || !vec_cst_ctor_to_array (arg1
, elts
+ nelts
))
8796 for (i
= 0; i
< nelts
; i
++)
8798 if (!CONSTANT_CLASS_P (elts
[sel
[i
]]))
8800 elts
[i
+ 2 * nelts
] = unshare_expr (elts
[sel
[i
]]);
8805 vec
<constructor_elt
, va_gc
> *v
;
8806 vec_alloc (v
, nelts
);
8807 for (i
= 0; i
< nelts
; i
++)
8808 CONSTRUCTOR_APPEND_ELT (v
, NULL_TREE
, elts
[2 * nelts
+ i
]);
8809 return build_constructor (type
, v
);
8812 return build_vector (type
, &elts
[2 * nelts
]);
8815 /* Try to fold a pointer difference of type TYPE two address expressions of
8816 array references AREF0 and AREF1 using location LOC. Return a
8817 simplified expression for the difference or NULL_TREE. */
8820 fold_addr_of_array_ref_difference (location_t loc
, tree type
,
8821 tree aref0
, tree aref1
)
8823 tree base0
= TREE_OPERAND (aref0
, 0);
8824 tree base1
= TREE_OPERAND (aref1
, 0);
8825 tree base_offset
= build_int_cst (type
, 0);
8827 /* If the bases are array references as well, recurse. If the bases
8828 are pointer indirections compute the difference of the pointers.
8829 If the bases are equal, we are set. */
8830 if ((TREE_CODE (base0
) == ARRAY_REF
8831 && TREE_CODE (base1
) == ARRAY_REF
8833 = fold_addr_of_array_ref_difference (loc
, type
, base0
, base1
)))
8834 || (INDIRECT_REF_P (base0
)
8835 && INDIRECT_REF_P (base1
)
8837 = fold_binary_loc (loc
, MINUS_EXPR
, type
,
8838 fold_convert (type
, TREE_OPERAND (base0
, 0)),
8840 TREE_OPERAND (base1
, 0)))))
8841 || operand_equal_p (base0
, base1
, OEP_ADDRESS_OF
))
8843 tree op0
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref0
, 1));
8844 tree op1
= fold_convert_loc (loc
, type
, TREE_OPERAND (aref1
, 1));
8845 tree esz
= fold_convert_loc (loc
, type
, array_ref_element_size (aref0
));
8846 tree diff
= build2 (MINUS_EXPR
, type
, op0
, op1
);
8847 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
8849 fold_build2_loc (loc
, MULT_EXPR
, type
,
8855 /* If the real or vector real constant CST of type TYPE has an exact
8856 inverse, return it, else return NULL. */
8859 exact_inverse (tree type
, tree cst
)
8862 tree unit_type
, *elts
;
8864 unsigned vec_nelts
, i
;
8866 switch (TREE_CODE (cst
))
8869 r
= TREE_REAL_CST (cst
);
8871 if (exact_real_inverse (TYPE_MODE (type
), &r
))
8872 return build_real (type
, r
);
8877 vec_nelts
= VECTOR_CST_NELTS (cst
);
8878 elts
= XALLOCAVEC (tree
, vec_nelts
);
8879 unit_type
= TREE_TYPE (type
);
8880 mode
= TYPE_MODE (unit_type
);
8882 for (i
= 0; i
< vec_nelts
; i
++)
8884 r
= TREE_REAL_CST (VECTOR_CST_ELT (cst
, i
));
8885 if (!exact_real_inverse (mode
, &r
))
8887 elts
[i
] = build_real (unit_type
, r
);
8890 return build_vector (type
, elts
);
8897 /* Mask out the tz least significant bits of X of type TYPE where
8898 tz is the number of trailing zeroes in Y. */
8900 mask_with_tz (tree type
, const wide_int
&x
, const wide_int
&y
)
8902 int tz
= wi::ctz (y
);
8904 return wi::mask (tz
, true, TYPE_PRECISION (type
)) & x
;
8908 /* Return true when T is an address and is known to be nonzero.
8909 For floating point we further ensure that T is not denormal.
8910 Similar logic is present in nonzero_address in rtlanal.h.
8912 If the return value is based on the assumption that signed overflow
8913 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8914 change *STRICT_OVERFLOW_P. */
8917 tree_expr_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
8919 tree type
= TREE_TYPE (t
);
8920 enum tree_code code
;
8922 /* Doing something useful for floating point would need more work. */
8923 if (!INTEGRAL_TYPE_P (type
) && !POINTER_TYPE_P (type
))
8926 code
= TREE_CODE (t
);
8927 switch (TREE_CODE_CLASS (code
))
8930 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
8933 case tcc_comparison
:
8934 return tree_binary_nonzero_warnv_p (code
, type
,
8935 TREE_OPERAND (t
, 0),
8936 TREE_OPERAND (t
, 1),
8939 case tcc_declaration
:
8941 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
8949 case TRUTH_NOT_EXPR
:
8950 return tree_unary_nonzero_warnv_p (code
, type
, TREE_OPERAND (t
, 0),
8953 case TRUTH_AND_EXPR
:
8955 case TRUTH_XOR_EXPR
:
8956 return tree_binary_nonzero_warnv_p (code
, type
,
8957 TREE_OPERAND (t
, 0),
8958 TREE_OPERAND (t
, 1),
8966 case WITH_SIZE_EXPR
:
8968 return tree_single_nonzero_warnv_p (t
, strict_overflow_p
);
8973 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
8977 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 0),
8982 tree fndecl
= get_callee_fndecl (t
);
8983 if (!fndecl
) return false;
8984 if (flag_delete_null_pointer_checks
&& !flag_check_new
8985 && DECL_IS_OPERATOR_NEW (fndecl
)
8986 && !TREE_NOTHROW (fndecl
))
8988 if (flag_delete_null_pointer_checks
8989 && lookup_attribute ("returns_nonnull",
8990 TYPE_ATTRIBUTES (TREE_TYPE (fndecl
))))
8992 return alloca_call_p (t
);
9001 /* Return true when T is an address and is known to be nonzero.
9002 Handle warnings about undefined signed overflow. */
9005 tree_expr_nonzero_p (tree t
)
9007 bool ret
, strict_overflow_p
;
9009 strict_overflow_p
= false;
9010 ret
= tree_expr_nonzero_warnv_p (t
, &strict_overflow_p
);
9011 if (strict_overflow_p
)
9012 fold_overflow_warning (("assuming signed overflow does not occur when "
9013 "determining that expression is always "
9015 WARN_STRICT_OVERFLOW_MISC
);
9019 /* Fold a binary expression of code CODE and type TYPE with operands
9020 OP0 and OP1. LOC is the location of the resulting expression.
9021 Return the folded expression if folding is successful. Otherwise,
9022 return NULL_TREE. */
9025 fold_binary_loc (location_t loc
,
9026 enum tree_code code
, tree type
, tree op0
, tree op1
)
9028 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
9029 tree arg0
, arg1
, tem
;
9030 tree t1
= NULL_TREE
;
9031 bool strict_overflow_p
;
9034 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
9035 && TREE_CODE_LENGTH (code
) == 2
9037 && op1
!= NULL_TREE
);
9042 /* Strip any conversions that don't change the mode. This is
9043 safe for every expression, except for a comparison expression
9044 because its signedness is derived from its operands. So, in
9045 the latter case, only strip conversions that don't change the
9046 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9049 Note that this is done as an internal manipulation within the
9050 constant folder, in order to find the simplest representation
9051 of the arguments so that their form can be studied. In any
9052 cases, the appropriate type conversions should be put back in
9053 the tree that will get out of the constant folder. */
9055 if (kind
== tcc_comparison
|| code
== MIN_EXPR
|| code
== MAX_EXPR
)
9057 STRIP_SIGN_NOPS (arg0
);
9058 STRIP_SIGN_NOPS (arg1
);
9066 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9067 constant but we can't do arithmetic on them. */
9068 if (CONSTANT_CLASS_P (arg0
) && CONSTANT_CLASS_P (arg1
))
9070 tem
= const_binop (code
, type
, arg0
, arg1
);
9071 if (tem
!= NULL_TREE
)
9073 if (TREE_TYPE (tem
) != type
)
9074 tem
= fold_convert_loc (loc
, type
, tem
);
9079 /* If this is a commutative operation, and ARG0 is a constant, move it
9080 to ARG1 to reduce the number of tests below. */
9081 if (commutative_tree_code (code
)
9082 && tree_swap_operands_p (arg0
, arg1
, true))
9083 return fold_build2_loc (loc
, code
, type
, op1
, op0
);
9085 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9086 to ARG1 to reduce the number of tests below. */
9087 if (kind
== tcc_comparison
9088 && tree_swap_operands_p (arg0
, arg1
, true))
9089 return fold_build2_loc (loc
, swap_tree_comparison (code
), type
, op1
, op0
);
9091 tem
= generic_simplify (loc
, code
, type
, op0
, op1
);
9095 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9097 First check for cases where an arithmetic operation is applied to a
9098 compound, conditional, or comparison operation. Push the arithmetic
9099 operation inside the compound or conditional to see if any folding
9100 can then be done. Convert comparison to conditional for this purpose.
9101 The also optimizes non-constant cases that used to be done in
9104 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9105 one of the operands is a comparison and the other is a comparison, a
9106 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9107 code below would make the expression more complex. Change it to a
9108 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9109 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9111 if ((code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
9112 || code
== EQ_EXPR
|| code
== NE_EXPR
)
9113 && TREE_CODE (type
) != VECTOR_TYPE
9114 && ((truth_value_p (TREE_CODE (arg0
))
9115 && (truth_value_p (TREE_CODE (arg1
))
9116 || (TREE_CODE (arg1
) == BIT_AND_EXPR
9117 && integer_onep (TREE_OPERAND (arg1
, 1)))))
9118 || (truth_value_p (TREE_CODE (arg1
))
9119 && (truth_value_p (TREE_CODE (arg0
))
9120 || (TREE_CODE (arg0
) == BIT_AND_EXPR
9121 && integer_onep (TREE_OPERAND (arg0
, 1)))))))
9123 tem
= fold_build2_loc (loc
, code
== BIT_AND_EXPR
? TRUTH_AND_EXPR
9124 : code
== BIT_IOR_EXPR
? TRUTH_OR_EXPR
9127 fold_convert_loc (loc
, boolean_type_node
, arg0
),
9128 fold_convert_loc (loc
, boolean_type_node
, arg1
));
9130 if (code
== EQ_EXPR
)
9131 tem
= invert_truthvalue_loc (loc
, tem
);
9133 return fold_convert_loc (loc
, type
, tem
);
9136 if (TREE_CODE_CLASS (code
) == tcc_binary
9137 || TREE_CODE_CLASS (code
) == tcc_comparison
)
9139 if (TREE_CODE (arg0
) == COMPOUND_EXPR
)
9141 tem
= fold_build2_loc (loc
, code
, type
,
9142 fold_convert_loc (loc
, TREE_TYPE (op0
),
9143 TREE_OPERAND (arg0
, 1)), op1
);
9144 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg0
, 0),
9147 if (TREE_CODE (arg1
) == COMPOUND_EXPR
9148 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 0)))
9150 tem
= fold_build2_loc (loc
, code
, type
, op0
,
9151 fold_convert_loc (loc
, TREE_TYPE (op1
),
9152 TREE_OPERAND (arg1
, 1)));
9153 return build2_loc (loc
, COMPOUND_EXPR
, type
, TREE_OPERAND (arg1
, 0),
9157 if (TREE_CODE (arg0
) == COND_EXPR
9158 || TREE_CODE (arg0
) == VEC_COND_EXPR
9159 || COMPARISON_CLASS_P (arg0
))
9161 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9163 /*cond_first_p=*/1);
9164 if (tem
!= NULL_TREE
)
9168 if (TREE_CODE (arg1
) == COND_EXPR
9169 || TREE_CODE (arg1
) == VEC_COND_EXPR
9170 || COMPARISON_CLASS_P (arg1
))
9172 tem
= fold_binary_op_with_conditional_arg (loc
, code
, type
, op0
, op1
,
9174 /*cond_first_p=*/0);
9175 if (tem
!= NULL_TREE
)
9183 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9184 if (TREE_CODE (arg0
) == ADDR_EXPR
9185 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == MEM_REF
)
9187 tree iref
= TREE_OPERAND (arg0
, 0);
9188 return fold_build2 (MEM_REF
, type
,
9189 TREE_OPERAND (iref
, 0),
9190 int_const_binop (PLUS_EXPR
, arg1
,
9191 TREE_OPERAND (iref
, 1)));
9194 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9195 if (TREE_CODE (arg0
) == ADDR_EXPR
9196 && handled_component_p (TREE_OPERAND (arg0
, 0)))
9199 HOST_WIDE_INT coffset
;
9200 base
= get_addr_base_and_unit_offset (TREE_OPERAND (arg0
, 0),
9204 return fold_build2 (MEM_REF
, type
,
9205 build_fold_addr_expr (base
),
9206 int_const_binop (PLUS_EXPR
, arg1
,
9207 size_int (coffset
)));
9212 case POINTER_PLUS_EXPR
:
9213 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9214 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
9215 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
)))
9216 return fold_convert_loc (loc
, type
,
9217 fold_build2_loc (loc
, PLUS_EXPR
, sizetype
,
9218 fold_convert_loc (loc
, sizetype
,
9220 fold_convert_loc (loc
, sizetype
,
9226 if (INTEGRAL_TYPE_P (type
) || VECTOR_INTEGER_TYPE_P (type
))
9228 /* X + (X / CST) * -CST is X % CST. */
9229 if (TREE_CODE (arg1
) == MULT_EXPR
9230 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == TRUNC_DIV_EXPR
9231 && operand_equal_p (arg0
,
9232 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0), 0))
9234 tree cst0
= TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1);
9235 tree cst1
= TREE_OPERAND (arg1
, 1);
9236 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (cst1
),
9238 if (sum
&& integer_zerop (sum
))
9239 return fold_convert_loc (loc
, type
,
9240 fold_build2_loc (loc
, TRUNC_MOD_EXPR
,
9241 TREE_TYPE (arg0
), arg0
,
9246 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9247 one. Make sure the type is not saturating and has the signedness of
9248 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9249 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9250 if ((TREE_CODE (arg0
) == MULT_EXPR
9251 || TREE_CODE (arg1
) == MULT_EXPR
)
9252 && !TYPE_SATURATING (type
)
9253 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
9254 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
9255 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
9257 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
9262 if (! FLOAT_TYPE_P (type
))
9264 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9265 (plus (plus (mult) (mult)) (foo)) so that we can
9266 take advantage of the factoring cases below. */
9267 if (ANY_INTEGRAL_TYPE_P (type
)
9268 && TYPE_OVERFLOW_WRAPS (type
)
9269 && (((TREE_CODE (arg0
) == PLUS_EXPR
9270 || TREE_CODE (arg0
) == MINUS_EXPR
)
9271 && TREE_CODE (arg1
) == MULT_EXPR
)
9272 || ((TREE_CODE (arg1
) == PLUS_EXPR
9273 || TREE_CODE (arg1
) == MINUS_EXPR
)
9274 && TREE_CODE (arg0
) == MULT_EXPR
)))
9276 tree parg0
, parg1
, parg
, marg
;
9277 enum tree_code pcode
;
9279 if (TREE_CODE (arg1
) == MULT_EXPR
)
9280 parg
= arg0
, marg
= arg1
;
9282 parg
= arg1
, marg
= arg0
;
9283 pcode
= TREE_CODE (parg
);
9284 parg0
= TREE_OPERAND (parg
, 0);
9285 parg1
= TREE_OPERAND (parg
, 1);
9289 if (TREE_CODE (parg0
) == MULT_EXPR
9290 && TREE_CODE (parg1
) != MULT_EXPR
)
9291 return fold_build2_loc (loc
, pcode
, type
,
9292 fold_build2_loc (loc
, PLUS_EXPR
, type
,
9293 fold_convert_loc (loc
, type
,
9295 fold_convert_loc (loc
, type
,
9297 fold_convert_loc (loc
, type
, parg1
));
9298 if (TREE_CODE (parg0
) != MULT_EXPR
9299 && TREE_CODE (parg1
) == MULT_EXPR
)
9301 fold_build2_loc (loc
, PLUS_EXPR
, type
,
9302 fold_convert_loc (loc
, type
, parg0
),
9303 fold_build2_loc (loc
, pcode
, type
,
9304 fold_convert_loc (loc
, type
, marg
),
9305 fold_convert_loc (loc
, type
,
9311 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9312 to __complex__ ( x, y ). This is not the same for SNaNs or
9313 if signed zeros are involved. */
9314 if (!HONOR_SNANS (element_mode (arg0
))
9315 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9316 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9318 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9319 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
9320 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
9321 bool arg0rz
= false, arg0iz
= false;
9322 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
9323 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
9325 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
9326 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
9327 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
9329 tree rp
= arg1r
? arg1r
9330 : build1 (REALPART_EXPR
, rtype
, arg1
);
9331 tree ip
= arg0i
? arg0i
9332 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
9333 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9335 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
9337 tree rp
= arg0r
? arg0r
9338 : build1 (REALPART_EXPR
, rtype
, arg0
);
9339 tree ip
= arg1i
? arg1i
9340 : build1 (IMAGPART_EXPR
, rtype
, arg1
);
9341 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9346 if (flag_unsafe_math_optimizations
9347 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
9348 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
9349 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
9352 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9353 We associate floats only if the user has specified
9354 -fassociative-math. */
9355 if (flag_associative_math
9356 && TREE_CODE (arg1
) == PLUS_EXPR
9357 && TREE_CODE (arg0
) != MULT_EXPR
)
9359 tree tree10
= TREE_OPERAND (arg1
, 0);
9360 tree tree11
= TREE_OPERAND (arg1
, 1);
9361 if (TREE_CODE (tree11
) == MULT_EXPR
9362 && TREE_CODE (tree10
) == MULT_EXPR
)
9365 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, arg0
, tree10
);
9366 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree0
, tree11
);
9369 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9370 We associate floats only if the user has specified
9371 -fassociative-math. */
9372 if (flag_associative_math
9373 && TREE_CODE (arg0
) == PLUS_EXPR
9374 && TREE_CODE (arg1
) != MULT_EXPR
)
9376 tree tree00
= TREE_OPERAND (arg0
, 0);
9377 tree tree01
= TREE_OPERAND (arg0
, 1);
9378 if (TREE_CODE (tree01
) == MULT_EXPR
9379 && TREE_CODE (tree00
) == MULT_EXPR
)
9382 tree0
= fold_build2_loc (loc
, PLUS_EXPR
, type
, tree01
, arg1
);
9383 return fold_build2_loc (loc
, PLUS_EXPR
, type
, tree00
, tree0
);
9389 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9390 is a rotate of A by C1 bits. */
9391 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9392 is a rotate of A by B bits. */
9394 enum tree_code code0
, code1
;
9396 code0
= TREE_CODE (arg0
);
9397 code1
= TREE_CODE (arg1
);
9398 if (((code0
== RSHIFT_EXPR
&& code1
== LSHIFT_EXPR
)
9399 || (code1
== RSHIFT_EXPR
&& code0
== LSHIFT_EXPR
))
9400 && operand_equal_p (TREE_OPERAND (arg0
, 0),
9401 TREE_OPERAND (arg1
, 0), 0)
9402 && (rtype
= TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9403 TYPE_UNSIGNED (rtype
))
9404 /* Only create rotates in complete modes. Other cases are not
9405 expanded properly. */
9406 && (element_precision (rtype
)
9407 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype
))))
9409 tree tree01
, tree11
;
9410 enum tree_code code01
, code11
;
9412 tree01
= TREE_OPERAND (arg0
, 1);
9413 tree11
= TREE_OPERAND (arg1
, 1);
9414 STRIP_NOPS (tree01
);
9415 STRIP_NOPS (tree11
);
9416 code01
= TREE_CODE (tree01
);
9417 code11
= TREE_CODE (tree11
);
9418 if (code01
== INTEGER_CST
9419 && code11
== INTEGER_CST
9420 && (wi::to_widest (tree01
) + wi::to_widest (tree11
)
9421 == element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)))))
9423 tem
= build2_loc (loc
, LROTATE_EXPR
,
9424 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9425 TREE_OPERAND (arg0
, 0),
9426 code0
== LSHIFT_EXPR
9427 ? TREE_OPERAND (arg0
, 1)
9428 : TREE_OPERAND (arg1
, 1));
9429 return fold_convert_loc (loc
, type
, tem
);
9431 else if (code11
== MINUS_EXPR
)
9433 tree tree110
, tree111
;
9434 tree110
= TREE_OPERAND (tree11
, 0);
9435 tree111
= TREE_OPERAND (tree11
, 1);
9436 STRIP_NOPS (tree110
);
9437 STRIP_NOPS (tree111
);
9438 if (TREE_CODE (tree110
) == INTEGER_CST
9439 && 0 == compare_tree_int (tree110
,
9441 (TREE_TYPE (TREE_OPERAND
9443 && operand_equal_p (tree01
, tree111
, 0))
9445 fold_convert_loc (loc
, type
,
9446 build2 ((code0
== LSHIFT_EXPR
9449 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9450 TREE_OPERAND (arg0
, 0),
9451 TREE_OPERAND (arg0
, 1)));
9453 else if (code01
== MINUS_EXPR
)
9455 tree tree010
, tree011
;
9456 tree010
= TREE_OPERAND (tree01
, 0);
9457 tree011
= TREE_OPERAND (tree01
, 1);
9458 STRIP_NOPS (tree010
);
9459 STRIP_NOPS (tree011
);
9460 if (TREE_CODE (tree010
) == INTEGER_CST
9461 && 0 == compare_tree_int (tree010
,
9463 (TREE_TYPE (TREE_OPERAND
9465 && operand_equal_p (tree11
, tree011
, 0))
9466 return fold_convert_loc
9468 build2 ((code0
!= LSHIFT_EXPR
9471 TREE_TYPE (TREE_OPERAND (arg0
, 0)),
9472 TREE_OPERAND (arg0
, 0), TREE_OPERAND (arg1
, 1)));
9478 /* In most languages, can't associate operations on floats through
9479 parentheses. Rather than remember where the parentheses were, we
9480 don't associate floats at all, unless the user has specified
9482 And, we need to make sure type is not saturating. */
9484 if ((! FLOAT_TYPE_P (type
) || flag_associative_math
)
9485 && !TYPE_SATURATING (type
))
9487 tree var0
, con0
, lit0
, minus_lit0
;
9488 tree var1
, con1
, lit1
, minus_lit1
;
9492 /* Split both trees into variables, constants, and literals. Then
9493 associate each group together, the constants with literals,
9494 then the result with variables. This increases the chances of
9495 literals being recombined later and of generating relocatable
9496 expressions for the sum of a constant and literal. */
9497 var0
= split_tree (arg0
, code
, &con0
, &lit0
, &minus_lit0
, 0);
9498 var1
= split_tree (arg1
, code
, &con1
, &lit1
, &minus_lit1
,
9499 code
== MINUS_EXPR
);
9501 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9502 if (code
== MINUS_EXPR
)
9505 /* With undefined overflow prefer doing association in a type
9506 which wraps on overflow, if that is one of the operand types. */
9507 if ((POINTER_TYPE_P (type
) && POINTER_TYPE_OVERFLOW_UNDEFINED
)
9508 || (INTEGRAL_TYPE_P (type
) && !TYPE_OVERFLOW_WRAPS (type
)))
9510 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
9511 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
9512 atype
= TREE_TYPE (arg0
);
9513 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
9514 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1
)))
9515 atype
= TREE_TYPE (arg1
);
9516 gcc_assert (TYPE_PRECISION (atype
) == TYPE_PRECISION (type
));
9519 /* With undefined overflow we can only associate constants with one
9520 variable, and constants whose association doesn't overflow. */
9521 if ((POINTER_TYPE_P (atype
) && POINTER_TYPE_OVERFLOW_UNDEFINED
)
9522 || (INTEGRAL_TYPE_P (atype
) && !TYPE_OVERFLOW_WRAPS (atype
)))
9528 bool one_neg
= false;
9530 if (TREE_CODE (tmp0
) == NEGATE_EXPR
)
9532 tmp0
= TREE_OPERAND (tmp0
, 0);
9535 if (CONVERT_EXPR_P (tmp0
)
9536 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
9537 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0
, 0)))
9538 <= TYPE_PRECISION (atype
)))
9539 tmp0
= TREE_OPERAND (tmp0
, 0);
9540 if (TREE_CODE (tmp1
) == NEGATE_EXPR
)
9542 tmp1
= TREE_OPERAND (tmp1
, 0);
9545 if (CONVERT_EXPR_P (tmp1
)
9546 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
9547 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1
, 0)))
9548 <= TYPE_PRECISION (atype
)))
9549 tmp1
= TREE_OPERAND (tmp1
, 0);
9550 /* The only case we can still associate with two variables
9551 is if they cancel out. */
9553 || !operand_equal_p (tmp0
, tmp1
, 0))
9558 /* Only do something if we found more than two objects. Otherwise,
9559 nothing has changed and we risk infinite recursion. */
9561 && (2 < ((var0
!= 0) + (var1
!= 0)
9562 + (con0
!= 0) + (con1
!= 0)
9563 + (lit0
!= 0) + (lit1
!= 0)
9564 + (minus_lit0
!= 0) + (minus_lit1
!= 0))))
9566 bool any_overflows
= false;
9567 if (lit0
) any_overflows
|= TREE_OVERFLOW (lit0
);
9568 if (lit1
) any_overflows
|= TREE_OVERFLOW (lit1
);
9569 if (minus_lit0
) any_overflows
|= TREE_OVERFLOW (minus_lit0
);
9570 if (minus_lit1
) any_overflows
|= TREE_OVERFLOW (minus_lit1
);
9571 var0
= associate_trees (loc
, var0
, var1
, code
, atype
);
9572 con0
= associate_trees (loc
, con0
, con1
, code
, atype
);
9573 lit0
= associate_trees (loc
, lit0
, lit1
, code
, atype
);
9574 minus_lit0
= associate_trees (loc
, minus_lit0
, minus_lit1
,
9577 /* Preserve the MINUS_EXPR if the negative part of the literal is
9578 greater than the positive part. Otherwise, the multiplicative
9579 folding code (i.e extract_muldiv) may be fooled in case
9580 unsigned constants are subtracted, like in the following
9581 example: ((X*2 + 4) - 8U)/2. */
9582 if (minus_lit0
&& lit0
)
9584 if (TREE_CODE (lit0
) == INTEGER_CST
9585 && TREE_CODE (minus_lit0
) == INTEGER_CST
9586 && tree_int_cst_lt (lit0
, minus_lit0
))
9588 minus_lit0
= associate_trees (loc
, minus_lit0
, lit0
,
9594 lit0
= associate_trees (loc
, lit0
, minus_lit0
,
9600 /* Don't introduce overflows through reassociation. */
9602 && ((lit0
&& TREE_OVERFLOW_P (lit0
))
9603 || (minus_lit0
&& TREE_OVERFLOW_P (minus_lit0
))))
9610 fold_convert_loc (loc
, type
,
9611 associate_trees (loc
, var0
, minus_lit0
,
9612 MINUS_EXPR
, atype
));
9615 con0
= associate_trees (loc
, con0
, minus_lit0
,
9618 fold_convert_loc (loc
, type
,
9619 associate_trees (loc
, var0
, con0
,
9624 con0
= associate_trees (loc
, con0
, lit0
, code
, atype
);
9626 fold_convert_loc (loc
, type
, associate_trees (loc
, var0
, con0
,
9634 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9635 if (TREE_CODE (arg0
) == NEGATE_EXPR
9636 && negate_expr_p (arg1
)
9637 && reorder_operands_p (arg0
, arg1
))
9638 return fold_build2_loc (loc
, MINUS_EXPR
, type
,
9639 fold_convert_loc (loc
, type
,
9640 negate_expr (arg1
)),
9641 fold_convert_loc (loc
, type
,
9642 TREE_OPERAND (arg0
, 0)));
9644 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9645 __complex__ ( x, -y ). This is not the same for SNaNs or if
9646 signed zeros are involved. */
9647 if (!HONOR_SNANS (element_mode (arg0
))
9648 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9649 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
)))
9651 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9652 tree arg0r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg0
);
9653 tree arg0i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
);
9654 bool arg0rz
= false, arg0iz
= false;
9655 if ((arg0r
&& (arg0rz
= real_zerop (arg0r
)))
9656 || (arg0i
&& (arg0iz
= real_zerop (arg0i
))))
9658 tree arg1r
= fold_unary_loc (loc
, REALPART_EXPR
, rtype
, arg1
);
9659 tree arg1i
= fold_unary_loc (loc
, IMAGPART_EXPR
, rtype
, arg1
);
9660 if (arg0rz
&& arg1i
&& real_zerop (arg1i
))
9662 tree rp
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
9664 : build1 (REALPART_EXPR
, rtype
, arg1
));
9665 tree ip
= arg0i
? arg0i
9666 : build1 (IMAGPART_EXPR
, rtype
, arg0
);
9667 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9669 else if (arg0iz
&& arg1r
&& real_zerop (arg1r
))
9671 tree rp
= arg0r
? arg0r
9672 : build1 (REALPART_EXPR
, rtype
, arg0
);
9673 tree ip
= fold_build1_loc (loc
, NEGATE_EXPR
, rtype
,
9675 : build1 (IMAGPART_EXPR
, rtype
, arg1
));
9676 return fold_build2_loc (loc
, COMPLEX_EXPR
, type
, rp
, ip
);
9681 /* A - B -> A + (-B) if B is easily negatable. */
9682 if (negate_expr_p (arg1
)
9683 && !TYPE_OVERFLOW_SANITIZED (type
)
9684 && ((FLOAT_TYPE_P (type
)
9685 /* Avoid this transformation if B is a positive REAL_CST. */
9686 && (TREE_CODE (arg1
) != REAL_CST
9687 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1
))))
9688 || INTEGRAL_TYPE_P (type
)))
9689 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
9690 fold_convert_loc (loc
, type
, arg0
),
9691 fold_convert_loc (loc
, type
,
9692 negate_expr (arg1
)));
9694 /* Fold &a[i] - &a[j] to i-j. */
9695 if (TREE_CODE (arg0
) == ADDR_EXPR
9696 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == ARRAY_REF
9697 && TREE_CODE (arg1
) == ADDR_EXPR
9698 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == ARRAY_REF
)
9700 tree tem
= fold_addr_of_array_ref_difference (loc
, type
,
9701 TREE_OPERAND (arg0
, 0),
9702 TREE_OPERAND (arg1
, 0));
9707 if (FLOAT_TYPE_P (type
)
9708 && flag_unsafe_math_optimizations
9709 && (TREE_CODE (arg0
) == RDIV_EXPR
|| TREE_CODE (arg0
) == MULT_EXPR
)
9710 && (TREE_CODE (arg1
) == RDIV_EXPR
|| TREE_CODE (arg1
) == MULT_EXPR
)
9711 && (tem
= distribute_real_division (loc
, code
, type
, arg0
, arg1
)))
9714 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9715 one. Make sure the type is not saturating and has the signedness of
9716 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9717 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9718 if ((TREE_CODE (arg0
) == MULT_EXPR
9719 || TREE_CODE (arg1
) == MULT_EXPR
)
9720 && !TYPE_SATURATING (type
)
9721 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg0
))
9722 && TYPE_UNSIGNED (type
) == TYPE_UNSIGNED (TREE_TYPE (arg1
))
9723 && (!FLOAT_TYPE_P (type
) || flag_associative_math
))
9725 tree tem
= fold_plusminus_mult_expr (loc
, code
, type
, arg0
, arg1
);
9733 if (! FLOAT_TYPE_P (type
))
9735 /* Transform x * -C into -x * C if x is easily negatable. */
9736 if (TREE_CODE (arg1
) == INTEGER_CST
9737 && tree_int_cst_sgn (arg1
) == -1
9738 && negate_expr_p (arg0
)
9739 && (tem
= negate_expr (arg1
)) != arg1
9740 && !TREE_OVERFLOW (tem
))
9741 return fold_build2_loc (loc
, MULT_EXPR
, type
,
9742 fold_convert_loc (loc
, type
,
9743 negate_expr (arg0
)),
9746 /* (A + A) * C -> A * 2 * C */
9747 if (TREE_CODE (arg0
) == PLUS_EXPR
9748 && TREE_CODE (arg1
) == INTEGER_CST
9749 && operand_equal_p (TREE_OPERAND (arg0
, 0),
9750 TREE_OPERAND (arg0
, 1), 0))
9751 return fold_build2_loc (loc
, MULT_EXPR
, type
,
9752 omit_one_operand_loc (loc
, type
,
9753 TREE_OPERAND (arg0
, 0),
9754 TREE_OPERAND (arg0
, 1)),
9755 fold_build2_loc (loc
, MULT_EXPR
, type
,
9756 build_int_cst (type
, 2) , arg1
));
9758 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
9759 sign-changing only. */
9760 if (TREE_CODE (arg1
) == INTEGER_CST
9761 && TREE_CODE (arg0
) == EXACT_DIV_EXPR
9762 && operand_equal_p (arg1
, TREE_OPERAND (arg0
, 1), 0))
9763 return fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
9765 strict_overflow_p
= false;
9766 if (TREE_CODE (arg1
) == INTEGER_CST
9767 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
9768 &strict_overflow_p
)))
9770 if (strict_overflow_p
)
9771 fold_overflow_warning (("assuming signed overflow does not "
9772 "occur when simplifying "
9774 WARN_STRICT_OVERFLOW_MISC
);
9775 return fold_convert_loc (loc
, type
, tem
);
9778 /* Optimize z * conj(z) for integer complex numbers. */
9779 if (TREE_CODE (arg0
) == CONJ_EXPR
9780 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
9781 return fold_mult_zconjz (loc
, type
, arg1
);
9782 if (TREE_CODE (arg1
) == CONJ_EXPR
9783 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
9784 return fold_mult_zconjz (loc
, type
, arg0
);
9788 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9789 This is not the same for NaNs or if signed zeros are
9791 if (!HONOR_NANS (arg0
)
9792 && !HONOR_SIGNED_ZEROS (element_mode (arg0
))
9793 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0
))
9794 && TREE_CODE (arg1
) == COMPLEX_CST
9795 && real_zerop (TREE_REALPART (arg1
)))
9797 tree rtype
= TREE_TYPE (TREE_TYPE (arg0
));
9798 if (real_onep (TREE_IMAGPART (arg1
)))
9800 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
9801 negate_expr (fold_build1_loc (loc
, IMAGPART_EXPR
,
9803 fold_build1_loc (loc
, REALPART_EXPR
, rtype
, arg0
));
9804 else if (real_minus_onep (TREE_IMAGPART (arg1
)))
9806 fold_build2_loc (loc
, COMPLEX_EXPR
, type
,
9807 fold_build1_loc (loc
, IMAGPART_EXPR
, rtype
, arg0
),
9808 negate_expr (fold_build1_loc (loc
, REALPART_EXPR
,
9812 /* Optimize z * conj(z) for floating point complex numbers.
9813 Guarded by flag_unsafe_math_optimizations as non-finite
9814 imaginary components don't produce scalar results. */
9815 if (flag_unsafe_math_optimizations
9816 && TREE_CODE (arg0
) == CONJ_EXPR
9817 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
9818 return fold_mult_zconjz (loc
, type
, arg1
);
9819 if (flag_unsafe_math_optimizations
9820 && TREE_CODE (arg1
) == CONJ_EXPR
9821 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
9822 return fold_mult_zconjz (loc
, type
, arg0
);
9824 if (flag_unsafe_math_optimizations
)
9827 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
9830 && operand_equal_p (arg0
, arg1
, 0))
9832 tree powfn
= mathfn_built_in (type
, BUILT_IN_POW
);
9836 tree arg
= build_real (type
, dconst2
);
9837 return build_call_expr_loc (loc
, powfn
, 2, arg0
, arg
);
9845 /* Canonicalize (X & C1) | C2. */
9846 if (TREE_CODE (arg0
) == BIT_AND_EXPR
9847 && TREE_CODE (arg1
) == INTEGER_CST
9848 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
9850 int width
= TYPE_PRECISION (type
), w
;
9851 wide_int c1
= TREE_OPERAND (arg0
, 1);
9854 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9855 if ((c1
& c2
) == c1
)
9856 return omit_one_operand_loc (loc
, type
, arg1
,
9857 TREE_OPERAND (arg0
, 0));
9859 wide_int msk
= wi::mask (width
, false,
9860 TYPE_PRECISION (TREE_TYPE (arg1
)));
9862 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9863 if (msk
.and_not (c1
| c2
) == 0)
9864 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
,
9865 TREE_OPERAND (arg0
, 0), arg1
);
9867 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9868 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9869 mode which allows further optimizations. */
9872 wide_int c3
= c1
.and_not (c2
);
9873 for (w
= BITS_PER_UNIT
; w
<= width
; w
<<= 1)
9875 wide_int mask
= wi::mask (w
, false,
9876 TYPE_PRECISION (type
));
9877 if (((c1
| c2
) & mask
) == mask
&& c1
.and_not (mask
) == 0)
9885 return fold_build2_loc (loc
, BIT_IOR_EXPR
, type
,
9886 fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
9887 TREE_OPERAND (arg0
, 0),
9888 wide_int_to_tree (type
,
9893 /* See if this can be simplified into a rotate first. If that
9894 is unsuccessful continue in the association code. */
9898 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
9899 if (TREE_CODE (arg0
) == BIT_AND_EXPR
9900 && INTEGRAL_TYPE_P (type
)
9901 && integer_onep (TREE_OPERAND (arg0
, 1))
9902 && integer_onep (arg1
))
9903 return fold_build2_loc (loc
, EQ_EXPR
, type
, arg0
,
9904 build_zero_cst (TREE_TYPE (arg0
)));
9906 /* See if this can be simplified into a rotate first. If that
9907 is unsuccessful continue in the association code. */
9911 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
9912 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9913 && INTEGRAL_TYPE_P (type
)
9914 && integer_onep (TREE_OPERAND (arg0
, 1))
9915 && integer_onep (arg1
))
9918 tem
= TREE_OPERAND (arg0
, 0);
9919 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
9920 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
9922 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
9923 build_zero_cst (TREE_TYPE (tem
)));
9925 /* Fold ~X & 1 as (X & 1) == 0. */
9926 if (TREE_CODE (arg0
) == BIT_NOT_EXPR
9927 && INTEGRAL_TYPE_P (type
)
9928 && integer_onep (arg1
))
9931 tem
= TREE_OPERAND (arg0
, 0);
9932 tem2
= fold_convert_loc (loc
, TREE_TYPE (tem
), arg1
);
9933 tem2
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (tem
),
9935 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem2
,
9936 build_zero_cst (TREE_TYPE (tem
)));
9938 /* Fold !X & 1 as X == 0. */
9939 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
9940 && integer_onep (arg1
))
9942 tem
= TREE_OPERAND (arg0
, 0);
9943 return fold_build2_loc (loc
, EQ_EXPR
, type
, tem
,
9944 build_zero_cst (TREE_TYPE (tem
)));
9947 /* Fold (X ^ Y) & Y as ~X & Y. */
9948 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9949 && operand_equal_p (TREE_OPERAND (arg0
, 1), arg1
, 0))
9951 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
9952 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
9953 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
9954 fold_convert_loc (loc
, type
, arg1
));
9956 /* Fold (X ^ Y) & X as ~Y & X. */
9957 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
9958 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
9959 && reorder_operands_p (TREE_OPERAND (arg0
, 1), arg1
))
9961 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
9962 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
9963 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
9964 fold_convert_loc (loc
, type
, arg1
));
9966 /* Fold X & (X ^ Y) as X & ~Y. */
9967 if (TREE_CODE (arg1
) == BIT_XOR_EXPR
9968 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
9970 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
9971 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
9972 fold_convert_loc (loc
, type
, arg0
),
9973 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
));
9975 /* Fold X & (Y ^ X) as ~Y & X. */
9976 if (TREE_CODE (arg1
) == BIT_XOR_EXPR
9977 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 1), 0)
9978 && reorder_operands_p (arg0
, TREE_OPERAND (arg1
, 0)))
9980 tem
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
9981 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
9982 fold_build1_loc (loc
, BIT_NOT_EXPR
, type
, tem
),
9983 fold_convert_loc (loc
, type
, arg0
));
9986 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
9987 multiple of 1 << CST. */
9988 if (TREE_CODE (arg1
) == INTEGER_CST
)
9990 wide_int cst1
= arg1
;
9991 wide_int ncst1
= -cst1
;
9992 if ((cst1
& ncst1
) == ncst1
9993 && multiple_of_p (type
, arg0
,
9994 wide_int_to_tree (TREE_TYPE (arg1
), ncst1
)))
9995 return fold_convert_loc (loc
, type
, arg0
);
9998 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10000 if (TREE_CODE (arg1
) == INTEGER_CST
10001 && TREE_CODE (arg0
) == MULT_EXPR
10002 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10004 wide_int warg1
= arg1
;
10005 wide_int masked
= mask_with_tz (type
, warg1
, TREE_OPERAND (arg0
, 1));
10008 return omit_two_operands_loc (loc
, type
, build_zero_cst (type
),
10010 else if (masked
!= warg1
)
10012 /* Avoid the transform if arg1 is a mask of some
10013 mode which allows further optimizations. */
10014 int pop
= wi::popcount (warg1
);
10015 if (!(pop
>= BITS_PER_UNIT
10016 && exact_log2 (pop
) != -1
10017 && wi::mask (pop
, false, warg1
.get_precision ()) == warg1
))
10018 return fold_build2_loc (loc
, code
, type
, op0
,
10019 wide_int_to_tree (type
, masked
));
10023 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10024 ((A & N) + B) & M -> (A + B) & M
10025 Similarly if (N & M) == 0,
10026 ((A | N) + B) & M -> (A + B) & M
10027 and for - instead of + (or unary - instead of +)
10028 and/or ^ instead of |.
10029 If B is constant and (B & M) == 0, fold into A & M. */
10030 if (TREE_CODE (arg1
) == INTEGER_CST
)
10032 wide_int cst1
= arg1
;
10033 if ((~cst1
!= 0) && (cst1
& (cst1
+ 1)) == 0
10034 && INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10035 && (TREE_CODE (arg0
) == PLUS_EXPR
10036 || TREE_CODE (arg0
) == MINUS_EXPR
10037 || TREE_CODE (arg0
) == NEGATE_EXPR
)
10038 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
))
10039 || TREE_CODE (TREE_TYPE (arg0
)) == INTEGER_TYPE
))
10045 /* Now we know that arg0 is (C + D) or (C - D) or
10046 -C and arg1 (M) is == (1LL << cst) - 1.
10047 Store C into PMOP[0] and D into PMOP[1]. */
10048 pmop
[0] = TREE_OPERAND (arg0
, 0);
10050 if (TREE_CODE (arg0
) != NEGATE_EXPR
)
10052 pmop
[1] = TREE_OPERAND (arg0
, 1);
10056 if ((wi::max_value (TREE_TYPE (arg0
)) & cst1
) != cst1
)
10059 for (; which
>= 0; which
--)
10060 switch (TREE_CODE (pmop
[which
]))
10065 if (TREE_CODE (TREE_OPERAND (pmop
[which
], 1))
10068 cst0
= TREE_OPERAND (pmop
[which
], 1);
10070 if (TREE_CODE (pmop
[which
]) == BIT_AND_EXPR
)
10075 else if (cst0
!= 0)
10077 /* If C or D is of the form (A & N) where
10078 (N & M) == M, or of the form (A | N) or
10079 (A ^ N) where (N & M) == 0, replace it with A. */
10080 pmop
[which
] = TREE_OPERAND (pmop
[which
], 0);
10083 /* If C or D is a N where (N & M) == 0, it can be
10084 omitted (assumed 0). */
10085 if ((TREE_CODE (arg0
) == PLUS_EXPR
10086 || (TREE_CODE (arg0
) == MINUS_EXPR
&& which
== 0))
10087 && (cst1
& pmop
[which
]) == 0)
10088 pmop
[which
] = NULL
;
10094 /* Only build anything new if we optimized one or both arguments
10096 if (pmop
[0] != TREE_OPERAND (arg0
, 0)
10097 || (TREE_CODE (arg0
) != NEGATE_EXPR
10098 && pmop
[1] != TREE_OPERAND (arg0
, 1)))
10100 tree utype
= TREE_TYPE (arg0
);
10101 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0
)))
10103 /* Perform the operations in a type that has defined
10104 overflow behavior. */
10105 utype
= unsigned_type_for (TREE_TYPE (arg0
));
10106 if (pmop
[0] != NULL
)
10107 pmop
[0] = fold_convert_loc (loc
, utype
, pmop
[0]);
10108 if (pmop
[1] != NULL
)
10109 pmop
[1] = fold_convert_loc (loc
, utype
, pmop
[1]);
10112 if (TREE_CODE (arg0
) == NEGATE_EXPR
)
10113 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[0]);
10114 else if (TREE_CODE (arg0
) == PLUS_EXPR
)
10116 if (pmop
[0] != NULL
&& pmop
[1] != NULL
)
10117 tem
= fold_build2_loc (loc
, PLUS_EXPR
, utype
,
10119 else if (pmop
[0] != NULL
)
10121 else if (pmop
[1] != NULL
)
10124 return build_int_cst (type
, 0);
10126 else if (pmop
[0] == NULL
)
10127 tem
= fold_build1_loc (loc
, NEGATE_EXPR
, utype
, pmop
[1]);
10129 tem
= fold_build2_loc (loc
, MINUS_EXPR
, utype
,
10131 /* TEM is now the new binary +, - or unary - replacement. */
10132 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, utype
, tem
,
10133 fold_convert_loc (loc
, utype
, arg1
));
10134 return fold_convert_loc (loc
, type
, tem
);
10139 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10140 if (TREE_CODE (arg1
) == INTEGER_CST
&& TREE_CODE (arg0
) == NOP_EXPR
10141 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0
, 0))))
10143 prec
= element_precision (TREE_TYPE (TREE_OPERAND (arg0
, 0)));
10145 wide_int mask
= wide_int::from (arg1
, prec
, UNSIGNED
);
10148 fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10154 /* Don't touch a floating-point divide by zero unless the mode
10155 of the constant can represent infinity. */
10156 if (TREE_CODE (arg1
) == REAL_CST
10157 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1
)))
10158 && real_zerop (arg1
))
10161 /* (-A) / (-B) -> A / B */
10162 if (TREE_CODE (arg0
) == NEGATE_EXPR
&& negate_expr_p (arg1
))
10163 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
10164 TREE_OPERAND (arg0
, 0),
10165 negate_expr (arg1
));
10166 if (TREE_CODE (arg1
) == NEGATE_EXPR
&& negate_expr_p (arg0
))
10167 return fold_build2_loc (loc
, RDIV_EXPR
, type
,
10168 negate_expr (arg0
),
10169 TREE_OPERAND (arg1
, 0));
10171 /* Convert A/B/C to A/(B*C). */
10172 if (flag_reciprocal_math
10173 && TREE_CODE (arg0
) == RDIV_EXPR
)
10174 return fold_build2_loc (loc
, RDIV_EXPR
, type
, TREE_OPERAND (arg0
, 0),
10175 fold_build2_loc (loc
, MULT_EXPR
, type
,
10176 TREE_OPERAND (arg0
, 1), arg1
));
10178 /* Convert A/(B/C) to (A/B)*C. */
10179 if (flag_reciprocal_math
10180 && TREE_CODE (arg1
) == RDIV_EXPR
)
10181 return fold_build2_loc (loc
, MULT_EXPR
, type
,
10182 fold_build2_loc (loc
, RDIV_EXPR
, type
, arg0
,
10183 TREE_OPERAND (arg1
, 0)),
10184 TREE_OPERAND (arg1
, 1));
10186 /* Convert C1/(X*C2) into (C1/C2)/X. */
10187 if (flag_reciprocal_math
10188 && TREE_CODE (arg1
) == MULT_EXPR
10189 && TREE_CODE (arg0
) == REAL_CST
10190 && TREE_CODE (TREE_OPERAND (arg1
, 1)) == REAL_CST
)
10192 tree tem
= const_binop (RDIV_EXPR
, arg0
,
10193 TREE_OPERAND (arg1
, 1));
10195 return fold_build2_loc (loc
, RDIV_EXPR
, type
, tem
,
10196 TREE_OPERAND (arg1
, 0));
10201 case TRUNC_DIV_EXPR
:
10202 /* Optimize (X & (-A)) / A where A is a power of 2,
10204 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10205 && !TYPE_UNSIGNED (type
) && TREE_CODE (arg1
) == INTEGER_CST
10206 && integer_pow2p (arg1
) && tree_int_cst_sgn (arg1
) > 0)
10208 tree sum
= fold_binary_loc (loc
, PLUS_EXPR
, TREE_TYPE (arg1
),
10209 arg1
, TREE_OPERAND (arg0
, 1));
10210 if (sum
&& integer_zerop (sum
)) {
10211 tree pow2
= build_int_cst (integer_type_node
,
10212 wi::exact_log2 (arg1
));
10213 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
10214 TREE_OPERAND (arg0
, 0), pow2
);
10220 case FLOOR_DIV_EXPR
:
10221 /* Simplify A / (B << N) where A and B are positive and B is
10222 a power of 2, to A >> (N + log2(B)). */
10223 strict_overflow_p
= false;
10224 if (TREE_CODE (arg1
) == LSHIFT_EXPR
10225 && (TYPE_UNSIGNED (type
)
10226 || tree_expr_nonnegative_warnv_p (op0
, &strict_overflow_p
)))
10228 tree sval
= TREE_OPERAND (arg1
, 0);
10229 if (integer_pow2p (sval
) && tree_int_cst_sgn (sval
) > 0)
10231 tree sh_cnt
= TREE_OPERAND (arg1
, 1);
10232 tree pow2
= build_int_cst (TREE_TYPE (sh_cnt
),
10233 wi::exact_log2 (sval
));
10235 if (strict_overflow_p
)
10236 fold_overflow_warning (("assuming signed overflow does not "
10237 "occur when simplifying A / (B << N)"),
10238 WARN_STRICT_OVERFLOW_MISC
);
10240 sh_cnt
= fold_build2_loc (loc
, PLUS_EXPR
, TREE_TYPE (sh_cnt
),
10242 return fold_build2_loc (loc
, RSHIFT_EXPR
, type
,
10243 fold_convert_loc (loc
, type
, arg0
), sh_cnt
);
10249 case ROUND_DIV_EXPR
:
10250 case CEIL_DIV_EXPR
:
10251 case EXACT_DIV_EXPR
:
10252 if (integer_zerop (arg1
))
10255 /* Convert -A / -B to A / B when the type is signed and overflow is
10257 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
10258 && TREE_CODE (arg0
) == NEGATE_EXPR
10259 && negate_expr_p (arg1
))
10261 if (INTEGRAL_TYPE_P (type
))
10262 fold_overflow_warning (("assuming signed overflow does not occur "
10263 "when distributing negation across "
10265 WARN_STRICT_OVERFLOW_MISC
);
10266 return fold_build2_loc (loc
, code
, type
,
10267 fold_convert_loc (loc
, type
,
10268 TREE_OPERAND (arg0
, 0)),
10269 fold_convert_loc (loc
, type
,
10270 negate_expr (arg1
)));
10272 if ((!INTEGRAL_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
10273 && TREE_CODE (arg1
) == NEGATE_EXPR
10274 && negate_expr_p (arg0
))
10276 if (INTEGRAL_TYPE_P (type
))
10277 fold_overflow_warning (("assuming signed overflow does not occur "
10278 "when distributing negation across "
10280 WARN_STRICT_OVERFLOW_MISC
);
10281 return fold_build2_loc (loc
, code
, type
,
10282 fold_convert_loc (loc
, type
,
10283 negate_expr (arg0
)),
10284 fold_convert_loc (loc
, type
,
10285 TREE_OPERAND (arg1
, 0)));
10288 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10289 operation, EXACT_DIV_EXPR.
10291 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10292 At one time others generated faster code, it's not clear if they do
10293 after the last round to changes to the DIV code in expmed.c. */
10294 if ((code
== CEIL_DIV_EXPR
|| code
== FLOOR_DIV_EXPR
)
10295 && multiple_of_p (type
, arg0
, arg1
))
10296 return fold_build2_loc (loc
, EXACT_DIV_EXPR
, type
,
10297 fold_convert (type
, arg0
),
10298 fold_convert (type
, arg1
));
10300 strict_overflow_p
= false;
10301 if (TREE_CODE (arg1
) == INTEGER_CST
10302 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10303 &strict_overflow_p
)))
10305 if (strict_overflow_p
)
10306 fold_overflow_warning (("assuming signed overflow does not occur "
10307 "when simplifying division"),
10308 WARN_STRICT_OVERFLOW_MISC
);
10309 return fold_convert_loc (loc
, type
, tem
);
10314 case CEIL_MOD_EXPR
:
10315 case FLOOR_MOD_EXPR
:
10316 case ROUND_MOD_EXPR
:
10317 case TRUNC_MOD_EXPR
:
10318 strict_overflow_p
= false;
10319 if (TREE_CODE (arg1
) == INTEGER_CST
10320 && 0 != (tem
= extract_muldiv (op0
, arg1
, code
, NULL_TREE
,
10321 &strict_overflow_p
)))
10323 if (strict_overflow_p
)
10324 fold_overflow_warning (("assuming signed overflow does not occur "
10325 "when simplifying modulus"),
10326 WARN_STRICT_OVERFLOW_MISC
);
10327 return fold_convert_loc (loc
, type
, tem
);
10336 /* Since negative shift count is not well-defined,
10337 don't try to compute it in the compiler. */
10338 if (TREE_CODE (arg1
) == INTEGER_CST
&& tree_int_cst_sgn (arg1
) < 0)
10341 prec
= element_precision (type
);
10343 /* If we have a rotate of a bit operation with the rotate count and
10344 the second operand of the bit operation both constant,
10345 permute the two operations. */
10346 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
10347 && (TREE_CODE (arg0
) == BIT_AND_EXPR
10348 || TREE_CODE (arg0
) == BIT_IOR_EXPR
10349 || TREE_CODE (arg0
) == BIT_XOR_EXPR
)
10350 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10351 return fold_build2_loc (loc
, TREE_CODE (arg0
), type
,
10352 fold_build2_loc (loc
, code
, type
,
10353 TREE_OPERAND (arg0
, 0), arg1
),
10354 fold_build2_loc (loc
, code
, type
,
10355 TREE_OPERAND (arg0
, 1), arg1
));
10357 /* Two consecutive rotates adding up to the some integer
10358 multiple of the precision of the type can be ignored. */
10359 if (code
== RROTATE_EXPR
&& TREE_CODE (arg1
) == INTEGER_CST
10360 && TREE_CODE (arg0
) == RROTATE_EXPR
10361 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10362 && wi::umod_trunc (wi::add (arg1
, TREE_OPERAND (arg0
, 1)),
10364 return TREE_OPERAND (arg0
, 0);
10372 case TRUTH_ANDIF_EXPR
:
10373 /* Note that the operands of this must be ints
10374 and their values must be 0 or 1.
10375 ("true" is a fixed value perhaps depending on the language.) */
10376 /* If first arg is constant zero, return it. */
10377 if (integer_zerop (arg0
))
10378 return fold_convert_loc (loc
, type
, arg0
);
10379 case TRUTH_AND_EXPR
:
10380 /* If either arg is constant true, drop it. */
10381 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10382 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10383 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
)
10384 /* Preserve sequence points. */
10385 && (code
!= TRUTH_ANDIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
10386 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10387 /* If second arg is constant zero, result is zero, but first arg
10388 must be evaluated. */
10389 if (integer_zerop (arg1
))
10390 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
10391 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10392 case will be handled here. */
10393 if (integer_zerop (arg0
))
10394 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
10396 /* !X && X is always false. */
10397 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10398 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10399 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg1
);
10400 /* X && !X is always false. */
10401 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10402 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10403 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10405 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10406 means A >= Y && A != MAX, but in this case we know that
10409 if (!TREE_SIDE_EFFECTS (arg0
)
10410 && !TREE_SIDE_EFFECTS (arg1
))
10412 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg0
, arg1
);
10413 if (tem
&& !operand_equal_p (tem
, arg0
, 0))
10414 return fold_build2_loc (loc
, code
, type
, tem
, arg1
);
10416 tem
= fold_to_nonsharp_ineq_using_bound (loc
, arg1
, arg0
);
10417 if (tem
&& !operand_equal_p (tem
, arg1
, 0))
10418 return fold_build2_loc (loc
, code
, type
, arg0
, tem
);
10421 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
10427 case TRUTH_ORIF_EXPR
:
10428 /* Note that the operands of this must be ints
10429 and their values must be 0 or true.
10430 ("true" is a fixed value perhaps depending on the language.) */
10431 /* If first arg is constant true, return it. */
10432 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10433 return fold_convert_loc (loc
, type
, arg0
);
10434 case TRUTH_OR_EXPR
:
10435 /* If either arg is constant zero, drop it. */
10436 if (TREE_CODE (arg0
) == INTEGER_CST
&& integer_zerop (arg0
))
10437 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg1
));
10438 if (TREE_CODE (arg1
) == INTEGER_CST
&& integer_zerop (arg1
)
10439 /* Preserve sequence points. */
10440 && (code
!= TRUTH_ORIF_EXPR
|| ! TREE_SIDE_EFFECTS (arg0
)))
10441 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10442 /* If second arg is constant true, result is true, but we must
10443 evaluate first arg. */
10444 if (TREE_CODE (arg1
) == INTEGER_CST
&& ! integer_zerop (arg1
))
10445 return omit_one_operand_loc (loc
, type
, arg1
, arg0
);
10446 /* Likewise for first arg, but note this only occurs here for
10448 if (TREE_CODE (arg0
) == INTEGER_CST
&& ! integer_zerop (arg0
))
10449 return omit_one_operand_loc (loc
, type
, arg0
, arg1
);
10451 /* !X || X is always true. */
10452 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10453 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10454 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
10455 /* X || !X is always true. */
10456 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10457 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10458 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10460 /* (X && !Y) || (!X && Y) is X ^ Y */
10461 if (TREE_CODE (arg0
) == TRUTH_AND_EXPR
10462 && TREE_CODE (arg1
) == TRUTH_AND_EXPR
)
10464 tree a0
, a1
, l0
, l1
, n0
, n1
;
10466 a0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 0));
10467 a1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg1
, 1));
10469 l0
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 0));
10470 l1
= fold_convert_loc (loc
, type
, TREE_OPERAND (arg0
, 1));
10472 n0
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l0
);
10473 n1
= fold_build1_loc (loc
, TRUTH_NOT_EXPR
, type
, l1
);
10475 if ((operand_equal_p (n0
, a0
, 0)
10476 && operand_equal_p (n1
, a1
, 0))
10477 || (operand_equal_p (n0
, a1
, 0)
10478 && operand_equal_p (n1
, a0
, 0)))
10479 return fold_build2_loc (loc
, TRUTH_XOR_EXPR
, type
, l0
, n1
);
10482 if ((tem
= fold_truth_andor (loc
, code
, type
, arg0
, arg1
, op0
, op1
))
10488 case TRUTH_XOR_EXPR
:
10489 /* If the second arg is constant zero, drop it. */
10490 if (integer_zerop (arg1
))
10491 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10492 /* If the second arg is constant true, this is a logical inversion. */
10493 if (integer_onep (arg1
))
10495 tem
= invert_truthvalue_loc (loc
, arg0
);
10496 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, tem
));
10498 /* Identical arguments cancel to zero. */
10499 if (operand_equal_p (arg0
, arg1
, 0))
10500 return omit_one_operand_loc (loc
, type
, integer_zero_node
, arg0
);
10502 /* !X ^ X is always true. */
10503 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
10504 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0))
10505 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg1
);
10507 /* X ^ !X is always true. */
10508 if (TREE_CODE (arg1
) == TRUTH_NOT_EXPR
10509 && operand_equal_p (arg0
, TREE_OPERAND (arg1
, 0), 0))
10510 return omit_one_operand_loc (loc
, type
, integer_one_node
, arg0
);
10519 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
10520 if (tem
!= NULL_TREE
)
10523 /* bool_var != 1 becomes !bool_var. */
10524 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_onep (arg1
)
10525 && code
== NE_EXPR
)
10526 return fold_convert_loc (loc
, type
,
10527 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
10528 TREE_TYPE (arg0
), arg0
));
10530 /* bool_var == 0 becomes !bool_var. */
10531 if (TREE_CODE (TREE_TYPE (arg0
)) == BOOLEAN_TYPE
&& integer_zerop (arg1
)
10532 && code
== EQ_EXPR
)
10533 return fold_convert_loc (loc
, type
,
10534 fold_build1_loc (loc
, TRUTH_NOT_EXPR
,
10535 TREE_TYPE (arg0
), arg0
));
10537 /* !exp != 0 becomes !exp */
10538 if (TREE_CODE (arg0
) == TRUTH_NOT_EXPR
&& integer_zerop (arg1
)
10539 && code
== NE_EXPR
)
10540 return non_lvalue_loc (loc
, fold_convert_loc (loc
, type
, arg0
));
10542 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10543 if ((TREE_CODE (arg0
) == PLUS_EXPR
10544 || TREE_CODE (arg0
) == POINTER_PLUS_EXPR
10545 || TREE_CODE (arg0
) == MINUS_EXPR
)
10546 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
10549 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
10550 || POINTER_TYPE_P (TREE_TYPE (arg0
))))
10552 tree val
= TREE_OPERAND (arg0
, 1);
10553 return omit_two_operands_loc (loc
, type
,
10554 fold_build2_loc (loc
, code
, type
,
10556 build_int_cst (TREE_TYPE (val
),
10558 TREE_OPERAND (arg0
, 0), arg1
);
10561 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10562 if (TREE_CODE (arg0
) == MINUS_EXPR
10563 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == INTEGER_CST
10564 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0
,
10567 && wi::extract_uhwi (TREE_OPERAND (arg0
, 0), 0, 1) == 1)
10569 return omit_two_operands_loc (loc
, type
,
10571 ? boolean_true_node
: boolean_false_node
,
10572 TREE_OPERAND (arg0
, 1), arg1
);
10575 /* If this is an EQ or NE comparison with zero and ARG0 is
10576 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10577 two operations, but the latter can be done in one less insn
10578 on machines that have only two-operand insns or on which a
10579 constant cannot be the first operand. */
10580 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10581 && integer_zerop (arg1
))
10583 tree arg00
= TREE_OPERAND (arg0
, 0);
10584 tree arg01
= TREE_OPERAND (arg0
, 1);
10585 if (TREE_CODE (arg00
) == LSHIFT_EXPR
10586 && integer_onep (TREE_OPERAND (arg00
, 0)))
10588 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg00
),
10589 arg01
, TREE_OPERAND (arg00
, 1));
10590 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
10591 build_int_cst (TREE_TYPE (arg0
), 1));
10592 return fold_build2_loc (loc
, code
, type
,
10593 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
10596 else if (TREE_CODE (arg01
) == LSHIFT_EXPR
10597 && integer_onep (TREE_OPERAND (arg01
, 0)))
10599 tree tem
= fold_build2_loc (loc
, RSHIFT_EXPR
, TREE_TYPE (arg01
),
10600 arg00
, TREE_OPERAND (arg01
, 1));
10601 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
), tem
,
10602 build_int_cst (TREE_TYPE (arg0
), 1));
10603 return fold_build2_loc (loc
, code
, type
,
10604 fold_convert_loc (loc
, TREE_TYPE (arg1
), tem
),
10609 /* If this is an NE or EQ comparison of zero against the result of a
10610 signed MOD operation whose second operand is a power of 2, make
10611 the MOD operation unsigned since it is simpler and equivalent. */
10612 if (integer_zerop (arg1
)
10613 && !TYPE_UNSIGNED (TREE_TYPE (arg0
))
10614 && (TREE_CODE (arg0
) == TRUNC_MOD_EXPR
10615 || TREE_CODE (arg0
) == CEIL_MOD_EXPR
10616 || TREE_CODE (arg0
) == FLOOR_MOD_EXPR
10617 || TREE_CODE (arg0
) == ROUND_MOD_EXPR
)
10618 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
10620 tree newtype
= unsigned_type_for (TREE_TYPE (arg0
));
10621 tree newmod
= fold_build2_loc (loc
, TREE_CODE (arg0
), newtype
,
10622 fold_convert_loc (loc
, newtype
,
10623 TREE_OPERAND (arg0
, 0)),
10624 fold_convert_loc (loc
, newtype
,
10625 TREE_OPERAND (arg0
, 1)));
10627 return fold_build2_loc (loc
, code
, type
, newmod
,
10628 fold_convert_loc (loc
, newtype
, arg1
));
10631 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10632 C1 is a valid shift constant, and C2 is a power of two, i.e.
10634 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10635 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == RSHIFT_EXPR
10636 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1))
10638 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10639 && integer_zerop (arg1
))
10641 tree itype
= TREE_TYPE (arg0
);
10642 tree arg001
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1);
10643 prec
= TYPE_PRECISION (itype
);
10645 /* Check for a valid shift count. */
10646 if (wi::ltu_p (arg001
, prec
))
10648 tree arg01
= TREE_OPERAND (arg0
, 1);
10649 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
10650 unsigned HOST_WIDE_INT log2
= tree_log2 (arg01
);
10651 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10652 can be rewritten as (X & (C2 << C1)) != 0. */
10653 if ((log2
+ TREE_INT_CST_LOW (arg001
)) < prec
)
10655 tem
= fold_build2_loc (loc
, LSHIFT_EXPR
, itype
, arg01
, arg001
);
10656 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, itype
, arg000
, tem
);
10657 return fold_build2_loc (loc
, code
, type
, tem
,
10658 fold_convert_loc (loc
, itype
, arg1
));
10660 /* Otherwise, for signed (arithmetic) shifts,
10661 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10662 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10663 else if (!TYPE_UNSIGNED (itype
))
10664 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
, type
,
10665 arg000
, build_int_cst (itype
, 0));
10666 /* Otherwise, of unsigned (logical) shifts,
10667 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10668 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10670 return omit_one_operand_loc (loc
, type
,
10671 code
== EQ_EXPR
? integer_one_node
10672 : integer_zero_node
,
10677 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10678 Similarly for NE_EXPR. */
10679 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10680 && TREE_CODE (arg1
) == INTEGER_CST
10681 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10683 tree notc
= fold_build1_loc (loc
, BIT_NOT_EXPR
,
10684 TREE_TYPE (TREE_OPERAND (arg0
, 1)),
10685 TREE_OPERAND (arg0
, 1));
10687 = fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
10688 fold_convert_loc (loc
, TREE_TYPE (arg0
), arg1
),
10690 tree rslt
= code
== EQ_EXPR
? integer_zero_node
: integer_one_node
;
10691 if (integer_nonzerop (dandnotc
))
10692 return omit_one_operand_loc (loc
, type
, rslt
, arg0
);
10695 /* If this is a comparison of a field, we may be able to simplify it. */
10696 if ((TREE_CODE (arg0
) == COMPONENT_REF
10697 || TREE_CODE (arg0
) == BIT_FIELD_REF
)
10698 /* Handle the constant case even without -O
10699 to make sure the warnings are given. */
10700 && (optimize
|| TREE_CODE (arg1
) == INTEGER_CST
))
10702 t1
= optimize_bit_field_compare (loc
, code
, type
, arg0
, arg1
);
10707 /* Optimize comparisons of strlen vs zero to a compare of the
10708 first character of the string vs zero. To wit,
10709 strlen(ptr) == 0 => *ptr == 0
10710 strlen(ptr) != 0 => *ptr != 0
10711 Other cases should reduce to one of these two (or a constant)
10712 due to the return value of strlen being unsigned. */
10713 if (TREE_CODE (arg0
) == CALL_EXPR
10714 && integer_zerop (arg1
))
10716 tree fndecl
= get_callee_fndecl (arg0
);
10719 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
10720 && DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STRLEN
10721 && call_expr_nargs (arg0
) == 1
10722 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0
, 0))) == POINTER_TYPE
)
10724 tree iref
= build_fold_indirect_ref_loc (loc
,
10725 CALL_EXPR_ARG (arg0
, 0));
10726 return fold_build2_loc (loc
, code
, type
, iref
,
10727 build_int_cst (TREE_TYPE (iref
), 0));
10731 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10732 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10733 if (TREE_CODE (arg0
) == RSHIFT_EXPR
10734 && integer_zerop (arg1
)
10735 && TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
)
10737 tree arg00
= TREE_OPERAND (arg0
, 0);
10738 tree arg01
= TREE_OPERAND (arg0
, 1);
10739 tree itype
= TREE_TYPE (arg00
);
10740 if (wi::eq_p (arg01
, element_precision (itype
) - 1))
10742 if (TYPE_UNSIGNED (itype
))
10744 itype
= signed_type_for (itype
);
10745 arg00
= fold_convert_loc (loc
, itype
, arg00
);
10747 return fold_build2_loc (loc
, code
== EQ_EXPR
? GE_EXPR
: LT_EXPR
,
10748 type
, arg00
, build_zero_cst (itype
));
10752 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10753 (X & C) == 0 when C is a single bit. */
10754 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10755 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_NOT_EXPR
10756 && integer_zerop (arg1
)
10757 && integer_pow2p (TREE_OPERAND (arg0
, 1)))
10759 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg0
),
10760 TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0),
10761 TREE_OPERAND (arg0
, 1));
10762 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
,
10764 fold_convert_loc (loc
, TREE_TYPE (arg0
),
10768 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10769 constant C is a power of two, i.e. a single bit. */
10770 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10771 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
10772 && integer_zerop (arg1
)
10773 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10774 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
10775 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
10777 tree arg00
= TREE_OPERAND (arg0
, 0);
10778 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
10779 arg00
, build_int_cst (TREE_TYPE (arg00
), 0));
10782 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10783 when is C is a power of two, i.e. a single bit. */
10784 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10785 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_XOR_EXPR
10786 && integer_zerop (arg1
)
10787 && integer_pow2p (TREE_OPERAND (arg0
, 1))
10788 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
10789 TREE_OPERAND (arg0
, 1), OEP_ONLY_CONST
))
10791 tree arg000
= TREE_OPERAND (TREE_OPERAND (arg0
, 0), 0);
10792 tem
= fold_build2_loc (loc
, BIT_AND_EXPR
, TREE_TYPE (arg000
),
10793 arg000
, TREE_OPERAND (arg0
, 1));
10794 return fold_build2_loc (loc
, code
== EQ_EXPR
? NE_EXPR
: EQ_EXPR
, type
,
10795 tem
, build_int_cst (TREE_TYPE (tem
), 0));
10798 if (integer_zerop (arg1
)
10799 && tree_expr_nonzero_p (arg0
))
10801 tree res
= constant_boolean_node (code
==NE_EXPR
, type
);
10802 return omit_one_operand_loc (loc
, type
, res
, arg0
);
10805 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10806 if (TREE_CODE (arg0
) == BIT_AND_EXPR
10807 && TREE_CODE (arg1
) == BIT_AND_EXPR
)
10809 tree arg00
= TREE_OPERAND (arg0
, 0);
10810 tree arg01
= TREE_OPERAND (arg0
, 1);
10811 tree arg10
= TREE_OPERAND (arg1
, 0);
10812 tree arg11
= TREE_OPERAND (arg1
, 1);
10813 tree itype
= TREE_TYPE (arg0
);
10815 if (operand_equal_p (arg01
, arg11
, 0))
10816 return fold_build2_loc (loc
, code
, type
,
10817 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10818 fold_build2_loc (loc
,
10819 BIT_XOR_EXPR
, itype
,
10822 build_zero_cst (itype
));
10824 if (operand_equal_p (arg01
, arg10
, 0))
10825 return fold_build2_loc (loc
, code
, type
,
10826 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10827 fold_build2_loc (loc
,
10828 BIT_XOR_EXPR
, itype
,
10831 build_zero_cst (itype
));
10833 if (operand_equal_p (arg00
, arg11
, 0))
10834 return fold_build2_loc (loc
, code
, type
,
10835 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10836 fold_build2_loc (loc
,
10837 BIT_XOR_EXPR
, itype
,
10840 build_zero_cst (itype
));
10842 if (operand_equal_p (arg00
, arg10
, 0))
10843 return fold_build2_loc (loc
, code
, type
,
10844 fold_build2_loc (loc
, BIT_AND_EXPR
, itype
,
10845 fold_build2_loc (loc
,
10846 BIT_XOR_EXPR
, itype
,
10849 build_zero_cst (itype
));
10852 if (TREE_CODE (arg0
) == BIT_XOR_EXPR
10853 && TREE_CODE (arg1
) == BIT_XOR_EXPR
)
10855 tree arg00
= TREE_OPERAND (arg0
, 0);
10856 tree arg01
= TREE_OPERAND (arg0
, 1);
10857 tree arg10
= TREE_OPERAND (arg1
, 0);
10858 tree arg11
= TREE_OPERAND (arg1
, 1);
10859 tree itype
= TREE_TYPE (arg0
);
10861 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10862 operand_equal_p guarantees no side-effects so we don't need
10863 to use omit_one_operand on Z. */
10864 if (operand_equal_p (arg01
, arg11
, 0))
10865 return fold_build2_loc (loc
, code
, type
, arg00
,
10866 fold_convert_loc (loc
, TREE_TYPE (arg00
),
10868 if (operand_equal_p (arg01
, arg10
, 0))
10869 return fold_build2_loc (loc
, code
, type
, arg00
,
10870 fold_convert_loc (loc
, TREE_TYPE (arg00
),
10872 if (operand_equal_p (arg00
, arg11
, 0))
10873 return fold_build2_loc (loc
, code
, type
, arg01
,
10874 fold_convert_loc (loc
, TREE_TYPE (arg01
),
10876 if (operand_equal_p (arg00
, arg10
, 0))
10877 return fold_build2_loc (loc
, code
, type
, arg01
,
10878 fold_convert_loc (loc
, TREE_TYPE (arg01
),
10881 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10882 if (TREE_CODE (arg01
) == INTEGER_CST
10883 && TREE_CODE (arg11
) == INTEGER_CST
)
10885 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg01
,
10886 fold_convert_loc (loc
, itype
, arg11
));
10887 tem
= fold_build2_loc (loc
, BIT_XOR_EXPR
, itype
, arg00
, tem
);
10888 return fold_build2_loc (loc
, code
, type
, tem
,
10889 fold_convert_loc (loc
, itype
, arg10
));
10893 /* Attempt to simplify equality/inequality comparisons of complex
10894 values. Only lower the comparison if the result is known or
10895 can be simplified to a single scalar comparison. */
10896 if ((TREE_CODE (arg0
) == COMPLEX_EXPR
10897 || TREE_CODE (arg0
) == COMPLEX_CST
)
10898 && (TREE_CODE (arg1
) == COMPLEX_EXPR
10899 || TREE_CODE (arg1
) == COMPLEX_CST
))
10901 tree real0
, imag0
, real1
, imag1
;
10904 if (TREE_CODE (arg0
) == COMPLEX_EXPR
)
10906 real0
= TREE_OPERAND (arg0
, 0);
10907 imag0
= TREE_OPERAND (arg0
, 1);
10911 real0
= TREE_REALPART (arg0
);
10912 imag0
= TREE_IMAGPART (arg0
);
10915 if (TREE_CODE (arg1
) == COMPLEX_EXPR
)
10917 real1
= TREE_OPERAND (arg1
, 0);
10918 imag1
= TREE_OPERAND (arg1
, 1);
10922 real1
= TREE_REALPART (arg1
);
10923 imag1
= TREE_IMAGPART (arg1
);
10926 rcond
= fold_binary_loc (loc
, code
, type
, real0
, real1
);
10927 if (rcond
&& TREE_CODE (rcond
) == INTEGER_CST
)
10929 if (integer_zerop (rcond
))
10931 if (code
== EQ_EXPR
)
10932 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
10934 return fold_build2_loc (loc
, NE_EXPR
, type
, imag0
, imag1
);
10938 if (code
== NE_EXPR
)
10939 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
10941 return fold_build2_loc (loc
, EQ_EXPR
, type
, imag0
, imag1
);
10945 icond
= fold_binary_loc (loc
, code
, type
, imag0
, imag1
);
10946 if (icond
&& TREE_CODE (icond
) == INTEGER_CST
)
10948 if (integer_zerop (icond
))
10950 if (code
== EQ_EXPR
)
10951 return omit_two_operands_loc (loc
, type
, boolean_false_node
,
10953 return fold_build2_loc (loc
, NE_EXPR
, type
, real0
, real1
);
10957 if (code
== NE_EXPR
)
10958 return omit_two_operands_loc (loc
, type
, boolean_true_node
,
10960 return fold_build2_loc (loc
, EQ_EXPR
, type
, real0
, real1
);
10971 tem
= fold_comparison (loc
, code
, type
, op0
, op1
);
10972 if (tem
!= NULL_TREE
)
10975 /* Transform comparisons of the form X +- C CMP X. */
10976 if ((TREE_CODE (arg0
) == PLUS_EXPR
|| TREE_CODE (arg0
) == MINUS_EXPR
)
10977 && operand_equal_p (TREE_OPERAND (arg0
, 0), arg1
, 0)
10978 && ((TREE_CODE (TREE_OPERAND (arg0
, 1)) == REAL_CST
10979 && !HONOR_SNANS (arg0
))
10980 || (TREE_CODE (TREE_OPERAND (arg0
, 1)) == INTEGER_CST
10981 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))))
10983 tree arg01
= TREE_OPERAND (arg0
, 1);
10984 enum tree_code code0
= TREE_CODE (arg0
);
10987 if (TREE_CODE (arg01
) == REAL_CST
)
10988 is_positive
= REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01
)) ? -1 : 1;
10990 is_positive
= tree_int_cst_sgn (arg01
);
10992 /* (X - c) > X becomes false. */
10993 if (code
== GT_EXPR
10994 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
10995 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
10997 if (TREE_CODE (arg01
) == INTEGER_CST
10998 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
10999 fold_overflow_warning (("assuming signed overflow does not "
11000 "occur when assuming that (X - c) > X "
11001 "is always false"),
11002 WARN_STRICT_OVERFLOW_ALL
);
11003 return constant_boolean_node (0, type
);
11006 /* Likewise (X + c) < X becomes false. */
11007 if (code
== LT_EXPR
11008 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
11009 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
11011 if (TREE_CODE (arg01
) == INTEGER_CST
11012 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11013 fold_overflow_warning (("assuming signed overflow does not "
11014 "occur when assuming that "
11015 "(X + c) < X is always false"),
11016 WARN_STRICT_OVERFLOW_ALL
);
11017 return constant_boolean_node (0, type
);
11020 /* Convert (X - c) <= X to true. */
11021 if (!HONOR_NANS (arg1
)
11023 && ((code0
== MINUS_EXPR
&& is_positive
>= 0)
11024 || (code0
== PLUS_EXPR
&& is_positive
<= 0)))
11026 if (TREE_CODE (arg01
) == INTEGER_CST
11027 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11028 fold_overflow_warning (("assuming signed overflow does not "
11029 "occur when assuming that "
11030 "(X - c) <= X is always true"),
11031 WARN_STRICT_OVERFLOW_ALL
);
11032 return constant_boolean_node (1, type
);
11035 /* Convert (X + c) >= X to true. */
11036 if (!HONOR_NANS (arg1
)
11038 && ((code0
== PLUS_EXPR
&& is_positive
>= 0)
11039 || (code0
== MINUS_EXPR
&& is_positive
<= 0)))
11041 if (TREE_CODE (arg01
) == INTEGER_CST
11042 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11043 fold_overflow_warning (("assuming signed overflow does not "
11044 "occur when assuming that "
11045 "(X + c) >= X is always true"),
11046 WARN_STRICT_OVERFLOW_ALL
);
11047 return constant_boolean_node (1, type
);
11050 if (TREE_CODE (arg01
) == INTEGER_CST
)
11052 /* Convert X + c > X and X - c < X to true for integers. */
11053 if (code
== GT_EXPR
11054 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
11055 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
11057 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11058 fold_overflow_warning (("assuming signed overflow does "
11059 "not occur when assuming that "
11060 "(X + c) > X is always true"),
11061 WARN_STRICT_OVERFLOW_ALL
);
11062 return constant_boolean_node (1, type
);
11065 if (code
== LT_EXPR
11066 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
11067 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
11069 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11070 fold_overflow_warning (("assuming signed overflow does "
11071 "not occur when assuming that "
11072 "(X - c) < X is always true"),
11073 WARN_STRICT_OVERFLOW_ALL
);
11074 return constant_boolean_node (1, type
);
11077 /* Convert X + c <= X and X - c >= X to false for integers. */
11078 if (code
== LE_EXPR
11079 && ((code0
== PLUS_EXPR
&& is_positive
> 0)
11080 || (code0
== MINUS_EXPR
&& is_positive
< 0)))
11082 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11083 fold_overflow_warning (("assuming signed overflow does "
11084 "not occur when assuming that "
11085 "(X + c) <= X is always false"),
11086 WARN_STRICT_OVERFLOW_ALL
);
11087 return constant_boolean_node (0, type
);
11090 if (code
== GE_EXPR
11091 && ((code0
== MINUS_EXPR
&& is_positive
> 0)
11092 || (code0
== PLUS_EXPR
&& is_positive
< 0)))
11094 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1
)))
11095 fold_overflow_warning (("assuming signed overflow does "
11096 "not occur when assuming that "
11097 "(X - c) >= X is always false"),
11098 WARN_STRICT_OVERFLOW_ALL
);
11099 return constant_boolean_node (0, type
);
11104 /* If we are comparing an ABS_EXPR with a constant, we can
11105 convert all the cases into explicit comparisons, but they may
11106 well not be faster than doing the ABS and one comparison.
11107 But ABS (X) <= C is a range comparison, which becomes a subtraction
11108 and a comparison, and is probably faster. */
11109 if (code
== LE_EXPR
11110 && TREE_CODE (arg1
) == INTEGER_CST
11111 && TREE_CODE (arg0
) == ABS_EXPR
11112 && ! TREE_SIDE_EFFECTS (arg0
)
11113 && (0 != (tem
= negate_expr (arg1
)))
11114 && TREE_CODE (tem
) == INTEGER_CST
11115 && !TREE_OVERFLOW (tem
))
11116 return fold_build2_loc (loc
, TRUTH_ANDIF_EXPR
, type
,
11117 build2 (GE_EXPR
, type
,
11118 TREE_OPERAND (arg0
, 0), tem
),
11119 build2 (LE_EXPR
, type
,
11120 TREE_OPERAND (arg0
, 0), arg1
));
11122 /* Convert ABS_EXPR<x> >= 0 to true. */
11123 strict_overflow_p
= false;
11124 if (code
== GE_EXPR
11125 && (integer_zerop (arg1
)
11126 || (! HONOR_NANS (arg0
)
11127 && real_zerop (arg1
)))
11128 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
11130 if (strict_overflow_p
)
11131 fold_overflow_warning (("assuming signed overflow does not occur "
11132 "when simplifying comparison of "
11133 "absolute value and zero"),
11134 WARN_STRICT_OVERFLOW_CONDITIONAL
);
11135 return omit_one_operand_loc (loc
, type
,
11136 constant_boolean_node (true, type
),
11140 /* Convert ABS_EXPR<x> < 0 to false. */
11141 strict_overflow_p
= false;
11142 if (code
== LT_EXPR
11143 && (integer_zerop (arg1
) || real_zerop (arg1
))
11144 && tree_expr_nonnegative_warnv_p (arg0
, &strict_overflow_p
))
11146 if (strict_overflow_p
)
11147 fold_overflow_warning (("assuming signed overflow does not occur "
11148 "when simplifying comparison of "
11149 "absolute value and zero"),
11150 WARN_STRICT_OVERFLOW_CONDITIONAL
);
11151 return omit_one_operand_loc (loc
, type
,
11152 constant_boolean_node (false, type
),
11156 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11157 and similarly for >= into !=. */
11158 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
11159 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
11160 && TREE_CODE (arg1
) == LSHIFT_EXPR
11161 && integer_onep (TREE_OPERAND (arg1
, 0)))
11162 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
11163 build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
11164 TREE_OPERAND (arg1
, 1)),
11165 build_zero_cst (TREE_TYPE (arg0
)));
11167 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11168 otherwise Y might be >= # of bits in X's type and thus e.g.
11169 (unsigned char) (1 << Y) for Y 15 might be 0.
11170 If the cast is widening, then 1 << Y should have unsigned type,
11171 otherwise if Y is number of bits in the signed shift type minus 1,
11172 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11173 31 might be 0xffffffff80000000. */
11174 if ((code
== LT_EXPR
|| code
== GE_EXPR
)
11175 && TYPE_UNSIGNED (TREE_TYPE (arg0
))
11176 && CONVERT_EXPR_P (arg1
)
11177 && TREE_CODE (TREE_OPERAND (arg1
, 0)) == LSHIFT_EXPR
11178 && (element_precision (TREE_TYPE (arg1
))
11179 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0))))
11180 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1
, 0)))
11181 || (element_precision (TREE_TYPE (arg1
))
11182 == element_precision (TREE_TYPE (TREE_OPERAND (arg1
, 0)))))
11183 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1
, 0), 0)))
11185 tem
= build2 (RSHIFT_EXPR
, TREE_TYPE (arg0
), arg0
,
11186 TREE_OPERAND (TREE_OPERAND (arg1
, 0), 1));
11187 return build2_loc (loc
, code
== LT_EXPR
? EQ_EXPR
: NE_EXPR
, type
,
11188 fold_convert_loc (loc
, TREE_TYPE (arg0
), tem
),
11189 build_zero_cst (TREE_TYPE (arg0
)));
11194 case UNORDERED_EXPR
:
11202 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11204 tree targ0
= strip_float_extensions (arg0
);
11205 tree targ1
= strip_float_extensions (arg1
);
11206 tree newtype
= TREE_TYPE (targ0
);
11208 if (TYPE_PRECISION (TREE_TYPE (targ1
)) > TYPE_PRECISION (newtype
))
11209 newtype
= TREE_TYPE (targ1
);
11211 if (TYPE_PRECISION (newtype
) < TYPE_PRECISION (TREE_TYPE (arg0
)))
11212 return fold_build2_loc (loc
, code
, type
,
11213 fold_convert_loc (loc
, newtype
, targ0
),
11214 fold_convert_loc (loc
, newtype
, targ1
));
11219 case COMPOUND_EXPR
:
11220 /* When pedantic, a compound expression can be neither an lvalue
11221 nor an integer constant expression. */
11222 if (TREE_SIDE_EFFECTS (arg0
) || TREE_CONSTANT (arg1
))
11224 /* Don't let (0, 0) be null pointer constant. */
11225 tem
= integer_zerop (arg1
) ? build1 (NOP_EXPR
, type
, arg1
)
11226 : fold_convert_loc (loc
, type
, arg1
);
11227 return pedantic_non_lvalue_loc (loc
, tem
);
11230 /* An ASSERT_EXPR should never be passed to fold_binary. */
11231 gcc_unreachable ();
11235 } /* switch (code) */
11238 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11239 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11243 contains_label_1 (tree
*tp
, int *walk_subtrees
, void *data ATTRIBUTE_UNUSED
)
11245 switch (TREE_CODE (*tp
))
11251 *walk_subtrees
= 0;
11253 /* ... fall through ... */
11260 /* Return whether the sub-tree ST contains a label which is accessible from
11261 outside the sub-tree. */
11264 contains_label_p (tree st
)
11267 (walk_tree_without_duplicates (&st
, contains_label_1
, NULL
) != NULL_TREE
);
11270 /* Fold a ternary expression of code CODE and type TYPE with operands
11271 OP0, OP1, and OP2. Return the folded expression if folding is
11272 successful. Otherwise, return NULL_TREE. */
11275 fold_ternary_loc (location_t loc
, enum tree_code code
, tree type
,
11276 tree op0
, tree op1
, tree op2
)
11279 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, arg2
= NULL_TREE
;
11280 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11282 gcc_assert (IS_EXPR_CODE_CLASS (kind
)
11283 && TREE_CODE_LENGTH (code
) == 3);
11285 /* If this is a commutative operation, and OP0 is a constant, move it
11286 to OP1 to reduce the number of tests below. */
11287 if (commutative_ternary_tree_code (code
)
11288 && tree_swap_operands_p (op0
, op1
, true))
11289 return fold_build3_loc (loc
, code
, type
, op1
, op0
, op2
);
11291 tem
= generic_simplify (loc
, code
, type
, op0
, op1
, op2
);
11295 /* Strip any conversions that don't change the mode. This is safe
11296 for every expression, except for a comparison expression because
11297 its signedness is derived from its operands. So, in the latter
11298 case, only strip conversions that don't change the signedness.
11300 Note that this is done as an internal manipulation within the
11301 constant folder, in order to find the simplest representation of
11302 the arguments so that their form can be studied. In any cases,
11303 the appropriate type conversions should be put back in the tree
11304 that will get out of the constant folder. */
11325 case COMPONENT_REF
:
11326 if (TREE_CODE (arg0
) == CONSTRUCTOR
11327 && ! type_contains_placeholder_p (TREE_TYPE (arg0
)))
11329 unsigned HOST_WIDE_INT idx
;
11331 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0
), idx
, field
, value
)
11338 case VEC_COND_EXPR
:
11339 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11340 so all simple results must be passed through pedantic_non_lvalue. */
11341 if (TREE_CODE (arg0
) == INTEGER_CST
)
11343 tree unused_op
= integer_zerop (arg0
) ? op1
: op2
;
11344 tem
= integer_zerop (arg0
) ? op2
: op1
;
11345 /* Only optimize constant conditions when the selected branch
11346 has the same type as the COND_EXPR. This avoids optimizing
11347 away "c ? x : throw", where the throw has a void type.
11348 Avoid throwing away that operand which contains label. */
11349 if ((!TREE_SIDE_EFFECTS (unused_op
)
11350 || !contains_label_p (unused_op
))
11351 && (! VOID_TYPE_P (TREE_TYPE (tem
))
11352 || VOID_TYPE_P (type
)))
11353 return pedantic_non_lvalue_loc (loc
, tem
);
11356 else if (TREE_CODE (arg0
) == VECTOR_CST
)
11358 if ((TREE_CODE (arg1
) == VECTOR_CST
11359 || TREE_CODE (arg1
) == CONSTRUCTOR
)
11360 && (TREE_CODE (arg2
) == VECTOR_CST
11361 || TREE_CODE (arg2
) == CONSTRUCTOR
))
11363 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
;
11364 unsigned char *sel
= XALLOCAVEC (unsigned char, nelts
);
11365 gcc_assert (nelts
== VECTOR_CST_NELTS (arg0
));
11366 for (i
= 0; i
< nelts
; i
++)
11368 tree val
= VECTOR_CST_ELT (arg0
, i
);
11369 if (integer_all_onesp (val
))
11371 else if (integer_zerop (val
))
11372 sel
[i
] = nelts
+ i
;
11373 else /* Currently unreachable. */
11376 tree t
= fold_vec_perm (type
, arg1
, arg2
, sel
);
11377 if (t
!= NULL_TREE
)
11382 /* If we have A op B ? A : C, we may be able to convert this to a
11383 simpler expression, depending on the operation and the values
11384 of B and C. Signed zeros prevent all of these transformations,
11385 for reasons given above each one.
11387 Also try swapping the arguments and inverting the conditional. */
11388 if (COMPARISON_CLASS_P (arg0
)
11389 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
11390 arg1
, TREE_OPERAND (arg0
, 1))
11391 && !HONOR_SIGNED_ZEROS (element_mode (arg1
)))
11393 tem
= fold_cond_expr_with_comparison (loc
, type
, arg0
, op1
, op2
);
11398 if (COMPARISON_CLASS_P (arg0
)
11399 && operand_equal_for_comparison_p (TREE_OPERAND (arg0
, 0),
11401 TREE_OPERAND (arg0
, 1))
11402 && !HONOR_SIGNED_ZEROS (element_mode (op2
)))
11404 location_t loc0
= expr_location_or (arg0
, loc
);
11405 tem
= fold_invert_truthvalue (loc0
, arg0
);
11406 if (tem
&& COMPARISON_CLASS_P (tem
))
11408 tem
= fold_cond_expr_with_comparison (loc
, type
, tem
, op2
, op1
);
11414 /* If the second operand is simpler than the third, swap them
11415 since that produces better jump optimization results. */
11416 if (truth_value_p (TREE_CODE (arg0
))
11417 && tree_swap_operands_p (op1
, op2
, false))
11419 location_t loc0
= expr_location_or (arg0
, loc
);
11420 /* See if this can be inverted. If it can't, possibly because
11421 it was a floating-point inequality comparison, don't do
11423 tem
= fold_invert_truthvalue (loc0
, arg0
);
11425 return fold_build3_loc (loc
, code
, type
, tem
, op2
, op1
);
11428 /* Convert A ? 1 : 0 to simply A. */
11429 if ((code
== VEC_COND_EXPR
? integer_all_onesp (op1
)
11430 : (integer_onep (op1
)
11431 && !VECTOR_TYPE_P (type
)))
11432 && integer_zerop (op2
)
11433 /* If we try to convert OP0 to our type, the
11434 call to fold will try to move the conversion inside
11435 a COND, which will recurse. In that case, the COND_EXPR
11436 is probably the best choice, so leave it alone. */
11437 && type
== TREE_TYPE (arg0
))
11438 return pedantic_non_lvalue_loc (loc
, arg0
);
11440 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11441 over COND_EXPR in cases such as floating point comparisons. */
11442 if (integer_zerop (op1
)
11443 && (code
== VEC_COND_EXPR
? integer_all_onesp (op2
)
11444 : (integer_onep (op2
)
11445 && !VECTOR_TYPE_P (type
)))
11446 && truth_value_p (TREE_CODE (arg0
)))
11447 return pedantic_non_lvalue_loc (loc
,
11448 fold_convert_loc (loc
, type
,
11449 invert_truthvalue_loc (loc
,
11452 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11453 if (TREE_CODE (arg0
) == LT_EXPR
11454 && integer_zerop (TREE_OPERAND (arg0
, 1))
11455 && integer_zerop (op2
)
11456 && (tem
= sign_bit_p (TREE_OPERAND (arg0
, 0), arg1
)))
11458 /* sign_bit_p looks through both zero and sign extensions,
11459 but for this optimization only sign extensions are
11461 tree tem2
= TREE_OPERAND (arg0
, 0);
11462 while (tem
!= tem2
)
11464 if (TREE_CODE (tem2
) != NOP_EXPR
11465 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2
, 0))))
11470 tem2
= TREE_OPERAND (tem2
, 0);
11472 /* sign_bit_p only checks ARG1 bits within A's precision.
11473 If <sign bit of A> has wider type than A, bits outside
11474 of A's precision in <sign bit of A> need to be checked.
11475 If they are all 0, this optimization needs to be done
11476 in unsigned A's type, if they are all 1 in signed A's type,
11477 otherwise this can't be done. */
11479 && TYPE_PRECISION (TREE_TYPE (tem
))
11480 < TYPE_PRECISION (TREE_TYPE (arg1
))
11481 && TYPE_PRECISION (TREE_TYPE (tem
))
11482 < TYPE_PRECISION (type
))
11484 int inner_width
, outer_width
;
11487 inner_width
= TYPE_PRECISION (TREE_TYPE (tem
));
11488 outer_width
= TYPE_PRECISION (TREE_TYPE (arg1
));
11489 if (outer_width
> TYPE_PRECISION (type
))
11490 outer_width
= TYPE_PRECISION (type
);
11492 wide_int mask
= wi::shifted_mask
11493 (inner_width
, outer_width
- inner_width
, false,
11494 TYPE_PRECISION (TREE_TYPE (arg1
)));
11496 wide_int common
= mask
& arg1
;
11497 if (common
== mask
)
11499 tem_type
= signed_type_for (TREE_TYPE (tem
));
11500 tem
= fold_convert_loc (loc
, tem_type
, tem
);
11502 else if (common
== 0)
11504 tem_type
= unsigned_type_for (TREE_TYPE (tem
));
11505 tem
= fold_convert_loc (loc
, tem_type
, tem
);
11513 fold_convert_loc (loc
, type
,
11514 fold_build2_loc (loc
, BIT_AND_EXPR
,
11515 TREE_TYPE (tem
), tem
,
11516 fold_convert_loc (loc
,
11521 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11522 already handled above. */
11523 if (TREE_CODE (arg0
) == BIT_AND_EXPR
11524 && integer_onep (TREE_OPERAND (arg0
, 1))
11525 && integer_zerop (op2
)
11526 && integer_pow2p (arg1
))
11528 tree tem
= TREE_OPERAND (arg0
, 0);
11530 if (TREE_CODE (tem
) == RSHIFT_EXPR
11531 && tree_fits_uhwi_p (TREE_OPERAND (tem
, 1))
11532 && (unsigned HOST_WIDE_INT
) tree_log2 (arg1
) ==
11533 tree_to_uhwi (TREE_OPERAND (tem
, 1)))
11534 return fold_build2_loc (loc
, BIT_AND_EXPR
, type
,
11535 TREE_OPERAND (tem
, 0), arg1
);
11538 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11539 is probably obsolete because the first operand should be a
11540 truth value (that's why we have the two cases above), but let's
11541 leave it in until we can confirm this for all front-ends. */
11542 if (integer_zerop (op2
)
11543 && TREE_CODE (arg0
) == NE_EXPR
11544 && integer_zerop (TREE_OPERAND (arg0
, 1))
11545 && integer_pow2p (arg1
)
11546 && TREE_CODE (TREE_OPERAND (arg0
, 0)) == BIT_AND_EXPR
11547 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0
, 0), 1),
11548 arg1
, OEP_ONLY_CONST
))
11549 return pedantic_non_lvalue_loc (loc
,
11550 fold_convert_loc (loc
, type
,
11551 TREE_OPERAND (arg0
, 0)));
11553 /* Disable the transformations below for vectors, since
11554 fold_binary_op_with_conditional_arg may undo them immediately,
11555 yielding an infinite loop. */
11556 if (code
== VEC_COND_EXPR
)
11559 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11560 if (integer_zerop (op2
)
11561 && truth_value_p (TREE_CODE (arg0
))
11562 && truth_value_p (TREE_CODE (arg1
))
11563 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11564 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
? BIT_AND_EXPR
11565 : TRUTH_ANDIF_EXPR
,
11566 type
, fold_convert_loc (loc
, type
, arg0
), arg1
);
11568 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11569 if (code
== VEC_COND_EXPR
? integer_all_onesp (op2
) : integer_onep (op2
)
11570 && truth_value_p (TREE_CODE (arg0
))
11571 && truth_value_p (TREE_CODE (arg1
))
11572 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11574 location_t loc0
= expr_location_or (arg0
, loc
);
11575 /* Only perform transformation if ARG0 is easily inverted. */
11576 tem
= fold_invert_truthvalue (loc0
, arg0
);
11578 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11581 type
, fold_convert_loc (loc
, type
, tem
),
11585 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11586 if (integer_zerop (arg1
)
11587 && truth_value_p (TREE_CODE (arg0
))
11588 && truth_value_p (TREE_CODE (op2
))
11589 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11591 location_t loc0
= expr_location_or (arg0
, loc
);
11592 /* Only perform transformation if ARG0 is easily inverted. */
11593 tem
= fold_invert_truthvalue (loc0
, arg0
);
11595 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11596 ? BIT_AND_EXPR
: TRUTH_ANDIF_EXPR
,
11597 type
, fold_convert_loc (loc
, type
, tem
),
11601 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11602 if (code
== VEC_COND_EXPR
? integer_all_onesp (arg1
) : integer_onep (arg1
)
11603 && truth_value_p (TREE_CODE (arg0
))
11604 && truth_value_p (TREE_CODE (op2
))
11605 && (code
== VEC_COND_EXPR
|| !VECTOR_TYPE_P (type
)))
11606 return fold_build2_loc (loc
, code
== VEC_COND_EXPR
11607 ? BIT_IOR_EXPR
: TRUTH_ORIF_EXPR
,
11608 type
, fold_convert_loc (loc
, type
, arg0
), op2
);
11613 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11614 of fold_ternary on them. */
11615 gcc_unreachable ();
11617 case BIT_FIELD_REF
:
11618 if ((TREE_CODE (arg0
) == VECTOR_CST
11619 || (TREE_CODE (arg0
) == CONSTRUCTOR
11620 && TREE_CODE (TREE_TYPE (arg0
)) == VECTOR_TYPE
))
11621 && (type
== TREE_TYPE (TREE_TYPE (arg0
))
11622 || (TREE_CODE (type
) == VECTOR_TYPE
11623 && TREE_TYPE (type
) == TREE_TYPE (TREE_TYPE (arg0
)))))
11625 tree eltype
= TREE_TYPE (TREE_TYPE (arg0
));
11626 unsigned HOST_WIDE_INT width
= tree_to_uhwi (TYPE_SIZE (eltype
));
11627 unsigned HOST_WIDE_INT n
= tree_to_uhwi (arg1
);
11628 unsigned HOST_WIDE_INT idx
= tree_to_uhwi (op2
);
11631 && (idx
% width
) == 0
11632 && (n
% width
) == 0
11633 && ((idx
+ n
) / width
) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0
)))
11638 if (TREE_CODE (arg0
) == VECTOR_CST
)
11641 return VECTOR_CST_ELT (arg0
, idx
);
11643 tree
*vals
= XALLOCAVEC (tree
, n
);
11644 for (unsigned i
= 0; i
< n
; ++i
)
11645 vals
[i
] = VECTOR_CST_ELT (arg0
, idx
+ i
);
11646 return build_vector (type
, vals
);
11649 /* Constructor elements can be subvectors. */
11650 unsigned HOST_WIDE_INT k
= 1;
11651 if (CONSTRUCTOR_NELTS (arg0
) != 0)
11653 tree cons_elem
= TREE_TYPE (CONSTRUCTOR_ELT (arg0
, 0)->value
);
11654 if (TREE_CODE (cons_elem
) == VECTOR_TYPE
)
11655 k
= TYPE_VECTOR_SUBPARTS (cons_elem
);
11658 /* We keep an exact subset of the constructor elements. */
11659 if ((idx
% k
) == 0 && (n
% k
) == 0)
11661 if (CONSTRUCTOR_NELTS (arg0
) == 0)
11662 return build_constructor (type
, NULL
);
11667 if (idx
< CONSTRUCTOR_NELTS (arg0
))
11668 return CONSTRUCTOR_ELT (arg0
, idx
)->value
;
11669 return build_zero_cst (type
);
11672 vec
<constructor_elt
, va_gc
> *vals
;
11673 vec_alloc (vals
, n
);
11674 for (unsigned i
= 0;
11675 i
< n
&& idx
+ i
< CONSTRUCTOR_NELTS (arg0
);
11677 CONSTRUCTOR_APPEND_ELT (vals
, NULL_TREE
,
11679 (arg0
, idx
+ i
)->value
);
11680 return build_constructor (type
, vals
);
11682 /* The bitfield references a single constructor element. */
11683 else if (idx
+ n
<= (idx
/ k
+ 1) * k
)
11685 if (CONSTRUCTOR_NELTS (arg0
) <= idx
/ k
)
11686 return build_zero_cst (type
);
11688 return CONSTRUCTOR_ELT (arg0
, idx
/ k
)->value
;
11690 return fold_build3_loc (loc
, code
, type
,
11691 CONSTRUCTOR_ELT (arg0
, idx
/ k
)->value
, op1
,
11692 build_int_cst (TREE_TYPE (op2
), (idx
% k
) * width
));
11697 /* A bit-field-ref that referenced the full argument can be stripped. */
11698 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
11699 && TYPE_PRECISION (TREE_TYPE (arg0
)) == tree_to_uhwi (arg1
)
11700 && integer_zerop (op2
))
11701 return fold_convert_loc (loc
, type
, arg0
);
11703 /* On constants we can use native encode/interpret to constant
11704 fold (nearly) all BIT_FIELD_REFs. */
11705 if (CONSTANT_CLASS_P (arg0
)
11706 && can_native_interpret_type_p (type
)
11707 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0
)))
11708 /* This limitation should not be necessary, we just need to
11709 round this up to mode size. */
11710 && tree_to_uhwi (op1
) % BITS_PER_UNIT
== 0
11711 /* Need bit-shifting of the buffer to relax the following. */
11712 && tree_to_uhwi (op2
) % BITS_PER_UNIT
== 0)
11714 unsigned HOST_WIDE_INT bitpos
= tree_to_uhwi (op2
);
11715 unsigned HOST_WIDE_INT bitsize
= tree_to_uhwi (op1
);
11716 unsigned HOST_WIDE_INT clen
;
11717 clen
= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0
)));
11718 /* ??? We cannot tell native_encode_expr to start at
11719 some random byte only. So limit us to a reasonable amount
11723 unsigned char *b
= XALLOCAVEC (unsigned char, clen
);
11724 unsigned HOST_WIDE_INT len
= native_encode_expr (arg0
, b
, clen
);
11726 && len
* BITS_PER_UNIT
>= bitpos
+ bitsize
)
11728 tree v
= native_interpret_expr (type
,
11729 b
+ bitpos
/ BITS_PER_UNIT
,
11730 bitsize
/ BITS_PER_UNIT
);
11740 /* For integers we can decompose the FMA if possible. */
11741 if (TREE_CODE (arg0
) == INTEGER_CST
11742 && TREE_CODE (arg1
) == INTEGER_CST
)
11743 return fold_build2_loc (loc
, PLUS_EXPR
, type
,
11744 const_binop (MULT_EXPR
, arg0
, arg1
), arg2
);
11745 if (integer_zerop (arg2
))
11746 return fold_build2_loc (loc
, MULT_EXPR
, type
, arg0
, arg1
);
11748 return fold_fma (loc
, type
, arg0
, arg1
, arg2
);
11750 case VEC_PERM_EXPR
:
11751 if (TREE_CODE (arg2
) == VECTOR_CST
)
11753 unsigned int nelts
= TYPE_VECTOR_SUBPARTS (type
), i
, mask
, mask2
;
11754 unsigned char *sel
= XALLOCAVEC (unsigned char, 2 * nelts
);
11755 unsigned char *sel2
= sel
+ nelts
;
11756 bool need_mask_canon
= false;
11757 bool need_mask_canon2
= false;
11758 bool all_in_vec0
= true;
11759 bool all_in_vec1
= true;
11760 bool maybe_identity
= true;
11761 bool single_arg
= (op0
== op1
);
11762 bool changed
= false;
11764 mask2
= 2 * nelts
- 1;
11765 mask
= single_arg
? (nelts
- 1) : mask2
;
11766 gcc_assert (nelts
== VECTOR_CST_NELTS (arg2
));
11767 for (i
= 0; i
< nelts
; i
++)
11769 tree val
= VECTOR_CST_ELT (arg2
, i
);
11770 if (TREE_CODE (val
) != INTEGER_CST
)
11773 /* Make sure that the perm value is in an acceptable
11776 need_mask_canon
|= wi::gtu_p (t
, mask
);
11777 need_mask_canon2
|= wi::gtu_p (t
, mask2
);
11778 sel
[i
] = t
.to_uhwi () & mask
;
11779 sel2
[i
] = t
.to_uhwi () & mask2
;
11781 if (sel
[i
] < nelts
)
11782 all_in_vec1
= false;
11784 all_in_vec0
= false;
11786 if ((sel
[i
] & (nelts
-1)) != i
)
11787 maybe_identity
= false;
11790 if (maybe_identity
)
11800 else if (all_in_vec1
)
11803 for (i
= 0; i
< nelts
; i
++)
11805 need_mask_canon
= true;
11808 if ((TREE_CODE (op0
) == VECTOR_CST
11809 || TREE_CODE (op0
) == CONSTRUCTOR
)
11810 && (TREE_CODE (op1
) == VECTOR_CST
11811 || TREE_CODE (op1
) == CONSTRUCTOR
))
11813 tree t
= fold_vec_perm (type
, op0
, op1
, sel
);
11814 if (t
!= NULL_TREE
)
11818 if (op0
== op1
&& !single_arg
)
11821 /* Some targets are deficient and fail to expand a single
11822 argument permutation while still allowing an equivalent
11823 2-argument version. */
11824 if (need_mask_canon
&& arg2
== op2
11825 && !can_vec_perm_p (TYPE_MODE (type
), false, sel
)
11826 && can_vec_perm_p (TYPE_MODE (type
), false, sel2
))
11828 need_mask_canon
= need_mask_canon2
;
11832 if (need_mask_canon
&& arg2
== op2
)
11834 tree
*tsel
= XALLOCAVEC (tree
, nelts
);
11835 tree eltype
= TREE_TYPE (TREE_TYPE (arg2
));
11836 for (i
= 0; i
< nelts
; i
++)
11837 tsel
[i
] = build_int_cst (eltype
, sel
[i
]);
11838 op2
= build_vector (TREE_TYPE (arg2
), tsel
);
11843 return build3_loc (loc
, VEC_PERM_EXPR
, type
, op0
, op1
, op2
);
11849 } /* switch (code) */
11852 /* Perform constant folding and related simplification of EXPR.
11853 The related simplifications include x*1 => x, x*0 => 0, etc.,
11854 and application of the associative law.
11855 NOP_EXPR conversions may be removed freely (as long as we
11856 are careful not to change the type of the overall expression).
11857 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11858 but we can constant-fold them if they have constant operands. */
11860 #ifdef ENABLE_FOLD_CHECKING
11861 # define fold(x) fold_1 (x)
11862 static tree
fold_1 (tree
);
11868 const tree t
= expr
;
11869 enum tree_code code
= TREE_CODE (t
);
11870 enum tree_code_class kind
= TREE_CODE_CLASS (code
);
11872 location_t loc
= EXPR_LOCATION (expr
);
11874 /* Return right away if a constant. */
11875 if (kind
== tcc_constant
)
11878 /* CALL_EXPR-like objects with variable numbers of operands are
11879 treated specially. */
11880 if (kind
== tcc_vl_exp
)
11882 if (code
== CALL_EXPR
)
11884 tem
= fold_call_expr (loc
, expr
, false);
11885 return tem
? tem
: expr
;
11890 if (IS_EXPR_CODE_CLASS (kind
))
11892 tree type
= TREE_TYPE (t
);
11893 tree op0
, op1
, op2
;
11895 switch (TREE_CODE_LENGTH (code
))
11898 op0
= TREE_OPERAND (t
, 0);
11899 tem
= fold_unary_loc (loc
, code
, type
, op0
);
11900 return tem
? tem
: expr
;
11902 op0
= TREE_OPERAND (t
, 0);
11903 op1
= TREE_OPERAND (t
, 1);
11904 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
11905 return tem
? tem
: expr
;
11907 op0
= TREE_OPERAND (t
, 0);
11908 op1
= TREE_OPERAND (t
, 1);
11909 op2
= TREE_OPERAND (t
, 2);
11910 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
11911 return tem
? tem
: expr
;
11921 tree op0
= TREE_OPERAND (t
, 0);
11922 tree op1
= TREE_OPERAND (t
, 1);
11924 if (TREE_CODE (op1
) == INTEGER_CST
11925 && TREE_CODE (op0
) == CONSTRUCTOR
11926 && ! type_contains_placeholder_p (TREE_TYPE (op0
)))
11928 vec
<constructor_elt
, va_gc
> *elts
= CONSTRUCTOR_ELTS (op0
);
11929 unsigned HOST_WIDE_INT end
= vec_safe_length (elts
);
11930 unsigned HOST_WIDE_INT begin
= 0;
11932 /* Find a matching index by means of a binary search. */
11933 while (begin
!= end
)
11935 unsigned HOST_WIDE_INT middle
= (begin
+ end
) / 2;
11936 tree index
= (*elts
)[middle
].index
;
11938 if (TREE_CODE (index
) == INTEGER_CST
11939 && tree_int_cst_lt (index
, op1
))
11940 begin
= middle
+ 1;
11941 else if (TREE_CODE (index
) == INTEGER_CST
11942 && tree_int_cst_lt (op1
, index
))
11944 else if (TREE_CODE (index
) == RANGE_EXPR
11945 && tree_int_cst_lt (TREE_OPERAND (index
, 1), op1
))
11946 begin
= middle
+ 1;
11947 else if (TREE_CODE (index
) == RANGE_EXPR
11948 && tree_int_cst_lt (op1
, TREE_OPERAND (index
, 0)))
11951 return (*elts
)[middle
].value
;
11958 /* Return a VECTOR_CST if possible. */
11961 tree type
= TREE_TYPE (t
);
11962 if (TREE_CODE (type
) != VECTOR_TYPE
)
11965 tree
*vec
= XALLOCAVEC (tree
, TYPE_VECTOR_SUBPARTS (type
));
11966 unsigned HOST_WIDE_INT idx
, pos
= 0;
11969 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), idx
, value
)
11971 if (!CONSTANT_CLASS_P (value
))
11973 if (TREE_CODE (value
) == VECTOR_CST
)
11975 for (unsigned i
= 0; i
< VECTOR_CST_NELTS (value
); ++i
)
11976 vec
[pos
++] = VECTOR_CST_ELT (value
, i
);
11979 vec
[pos
++] = value
;
11981 for (; pos
< TYPE_VECTOR_SUBPARTS (type
); ++pos
)
11982 vec
[pos
] = build_zero_cst (TREE_TYPE (type
));
11984 return build_vector (type
, vec
);
11988 return fold (DECL_INITIAL (t
));
11992 } /* switch (code) */
11995 #ifdef ENABLE_FOLD_CHECKING
11998 static void fold_checksum_tree (const_tree
, struct md5_ctx
*,
11999 hash_table
<nofree_ptr_hash
<const tree_node
> > *);
12000 static void fold_check_failed (const_tree
, const_tree
);
12001 void print_fold_checksum (const_tree
);
12003 /* When --enable-checking=fold, compute a digest of expr before
12004 and after actual fold call to see if fold did not accidentally
12005 change original expr. */
12011 struct md5_ctx ctx
;
12012 unsigned char checksum_before
[16], checksum_after
[16];
12013 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12015 md5_init_ctx (&ctx
);
12016 fold_checksum_tree (expr
, &ctx
, &ht
);
12017 md5_finish_ctx (&ctx
, checksum_before
);
12020 ret
= fold_1 (expr
);
12022 md5_init_ctx (&ctx
);
12023 fold_checksum_tree (expr
, &ctx
, &ht
);
12024 md5_finish_ctx (&ctx
, checksum_after
);
12026 if (memcmp (checksum_before
, checksum_after
, 16))
12027 fold_check_failed (expr
, ret
);
12033 print_fold_checksum (const_tree expr
)
12035 struct md5_ctx ctx
;
12036 unsigned char checksum
[16], cnt
;
12037 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12039 md5_init_ctx (&ctx
);
12040 fold_checksum_tree (expr
, &ctx
, &ht
);
12041 md5_finish_ctx (&ctx
, checksum
);
12042 for (cnt
= 0; cnt
< 16; ++cnt
)
12043 fprintf (stderr
, "%02x", checksum
[cnt
]);
12044 putc ('\n', stderr
);
12048 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED
, const_tree ret ATTRIBUTE_UNUSED
)
12050 internal_error ("fold check: original tree changed by fold");
12054 fold_checksum_tree (const_tree expr
, struct md5_ctx
*ctx
,
12055 hash_table
<nofree_ptr_hash
<const tree_node
> > *ht
)
12057 const tree_node
**slot
;
12058 enum tree_code code
;
12059 union tree_node buf
;
12065 slot
= ht
->find_slot (expr
, INSERT
);
12069 code
= TREE_CODE (expr
);
12070 if (TREE_CODE_CLASS (code
) == tcc_declaration
12071 && HAS_DECL_ASSEMBLER_NAME_P (expr
))
12073 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12074 memcpy ((char *) &buf
, expr
, tree_size (expr
));
12075 SET_DECL_ASSEMBLER_NAME ((tree
)&buf
, NULL
);
12076 buf
.decl_with_vis
.symtab_node
= NULL
;
12077 expr
= (tree
) &buf
;
12079 else if (TREE_CODE_CLASS (code
) == tcc_type
12080 && (TYPE_POINTER_TO (expr
)
12081 || TYPE_REFERENCE_TO (expr
)
12082 || TYPE_CACHED_VALUES_P (expr
)
12083 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr
)
12084 || TYPE_NEXT_VARIANT (expr
)))
12086 /* Allow these fields to be modified. */
12088 memcpy ((char *) &buf
, expr
, tree_size (expr
));
12089 expr
= tmp
= (tree
) &buf
;
12090 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp
) = 0;
12091 TYPE_POINTER_TO (tmp
) = NULL
;
12092 TYPE_REFERENCE_TO (tmp
) = NULL
;
12093 TYPE_NEXT_VARIANT (tmp
) = NULL
;
12094 if (TYPE_CACHED_VALUES_P (tmp
))
12096 TYPE_CACHED_VALUES_P (tmp
) = 0;
12097 TYPE_CACHED_VALUES (tmp
) = NULL
;
12100 md5_process_bytes (expr
, tree_size (expr
), ctx
);
12101 if (CODE_CONTAINS_STRUCT (code
, TS_TYPED
))
12102 fold_checksum_tree (TREE_TYPE (expr
), ctx
, ht
);
12103 if (TREE_CODE_CLASS (code
) != tcc_type
12104 && TREE_CODE_CLASS (code
) != tcc_declaration
12105 && code
!= TREE_LIST
12106 && code
!= SSA_NAME
12107 && CODE_CONTAINS_STRUCT (code
, TS_COMMON
))
12108 fold_checksum_tree (TREE_CHAIN (expr
), ctx
, ht
);
12109 switch (TREE_CODE_CLASS (code
))
12115 md5_process_bytes (TREE_STRING_POINTER (expr
),
12116 TREE_STRING_LENGTH (expr
), ctx
);
12119 fold_checksum_tree (TREE_REALPART (expr
), ctx
, ht
);
12120 fold_checksum_tree (TREE_IMAGPART (expr
), ctx
, ht
);
12123 for (i
= 0; i
< (int) VECTOR_CST_NELTS (expr
); ++i
)
12124 fold_checksum_tree (VECTOR_CST_ELT (expr
, i
), ctx
, ht
);
12130 case tcc_exceptional
:
12134 fold_checksum_tree (TREE_PURPOSE (expr
), ctx
, ht
);
12135 fold_checksum_tree (TREE_VALUE (expr
), ctx
, ht
);
12136 expr
= TREE_CHAIN (expr
);
12137 goto recursive_label
;
12140 for (i
= 0; i
< TREE_VEC_LENGTH (expr
); ++i
)
12141 fold_checksum_tree (TREE_VEC_ELT (expr
, i
), ctx
, ht
);
12147 case tcc_expression
:
12148 case tcc_reference
:
12149 case tcc_comparison
:
12152 case tcc_statement
:
12154 len
= TREE_OPERAND_LENGTH (expr
);
12155 for (i
= 0; i
< len
; ++i
)
12156 fold_checksum_tree (TREE_OPERAND (expr
, i
), ctx
, ht
);
12158 case tcc_declaration
:
12159 fold_checksum_tree (DECL_NAME (expr
), ctx
, ht
);
12160 fold_checksum_tree (DECL_CONTEXT (expr
), ctx
, ht
);
12161 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_COMMON
))
12163 fold_checksum_tree (DECL_SIZE (expr
), ctx
, ht
);
12164 fold_checksum_tree (DECL_SIZE_UNIT (expr
), ctx
, ht
);
12165 fold_checksum_tree (DECL_INITIAL (expr
), ctx
, ht
);
12166 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr
), ctx
, ht
);
12167 fold_checksum_tree (DECL_ATTRIBUTES (expr
), ctx
, ht
);
12170 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr
), TS_DECL_NON_COMMON
))
12172 if (TREE_CODE (expr
) == FUNCTION_DECL
)
12174 fold_checksum_tree (DECL_VINDEX (expr
), ctx
, ht
);
12175 fold_checksum_tree (DECL_ARGUMENTS (expr
), ctx
, ht
);
12177 fold_checksum_tree (DECL_RESULT_FLD (expr
), ctx
, ht
);
12181 if (TREE_CODE (expr
) == ENUMERAL_TYPE
)
12182 fold_checksum_tree (TYPE_VALUES (expr
), ctx
, ht
);
12183 fold_checksum_tree (TYPE_SIZE (expr
), ctx
, ht
);
12184 fold_checksum_tree (TYPE_SIZE_UNIT (expr
), ctx
, ht
);
12185 fold_checksum_tree (TYPE_ATTRIBUTES (expr
), ctx
, ht
);
12186 fold_checksum_tree (TYPE_NAME (expr
), ctx
, ht
);
12187 if (INTEGRAL_TYPE_P (expr
)
12188 || SCALAR_FLOAT_TYPE_P (expr
))
12190 fold_checksum_tree (TYPE_MIN_VALUE (expr
), ctx
, ht
);
12191 fold_checksum_tree (TYPE_MAX_VALUE (expr
), ctx
, ht
);
12193 fold_checksum_tree (TYPE_MAIN_VARIANT (expr
), ctx
, ht
);
12194 if (TREE_CODE (expr
) == RECORD_TYPE
12195 || TREE_CODE (expr
) == UNION_TYPE
12196 || TREE_CODE (expr
) == QUAL_UNION_TYPE
)
12197 fold_checksum_tree (TYPE_BINFO (expr
), ctx
, ht
);
12198 fold_checksum_tree (TYPE_CONTEXT (expr
), ctx
, ht
);
12205 /* Helper function for outputting the checksum of a tree T. When
12206 debugging with gdb, you can "define mynext" to be "next" followed
12207 by "call debug_fold_checksum (op0)", then just trace down till the
12210 DEBUG_FUNCTION
void
12211 debug_fold_checksum (const_tree t
)
12214 unsigned char checksum
[16];
12215 struct md5_ctx ctx
;
12216 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12218 md5_init_ctx (&ctx
);
12219 fold_checksum_tree (t
, &ctx
, &ht
);
12220 md5_finish_ctx (&ctx
, checksum
);
12223 for (i
= 0; i
< 16; i
++)
12224 fprintf (stderr
, "%d ", checksum
[i
]);
12226 fprintf (stderr
, "\n");
12231 /* Fold a unary tree expression with code CODE of type TYPE with an
12232 operand OP0. LOC is the location of the resulting expression.
12233 Return a folded expression if successful. Otherwise, return a tree
12234 expression with code CODE of type TYPE with an operand OP0. */
12237 fold_build1_stat_loc (location_t loc
,
12238 enum tree_code code
, tree type
, tree op0 MEM_STAT_DECL
)
12241 #ifdef ENABLE_FOLD_CHECKING
12242 unsigned char checksum_before
[16], checksum_after
[16];
12243 struct md5_ctx ctx
;
12244 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12246 md5_init_ctx (&ctx
);
12247 fold_checksum_tree (op0
, &ctx
, &ht
);
12248 md5_finish_ctx (&ctx
, checksum_before
);
12252 tem
= fold_unary_loc (loc
, code
, type
, op0
);
12254 tem
= build1_stat_loc (loc
, code
, type
, op0 PASS_MEM_STAT
);
12256 #ifdef ENABLE_FOLD_CHECKING
12257 md5_init_ctx (&ctx
);
12258 fold_checksum_tree (op0
, &ctx
, &ht
);
12259 md5_finish_ctx (&ctx
, checksum_after
);
12261 if (memcmp (checksum_before
, checksum_after
, 16))
12262 fold_check_failed (op0
, tem
);
12267 /* Fold a binary tree expression with code CODE of type TYPE with
12268 operands OP0 and OP1. LOC is the location of the resulting
12269 expression. Return a folded expression if successful. Otherwise,
12270 return a tree expression with code CODE of type TYPE with operands
12274 fold_build2_stat_loc (location_t loc
,
12275 enum tree_code code
, tree type
, tree op0
, tree op1
12279 #ifdef ENABLE_FOLD_CHECKING
12280 unsigned char checksum_before_op0
[16],
12281 checksum_before_op1
[16],
12282 checksum_after_op0
[16],
12283 checksum_after_op1
[16];
12284 struct md5_ctx ctx
;
12285 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12287 md5_init_ctx (&ctx
);
12288 fold_checksum_tree (op0
, &ctx
, &ht
);
12289 md5_finish_ctx (&ctx
, checksum_before_op0
);
12292 md5_init_ctx (&ctx
);
12293 fold_checksum_tree (op1
, &ctx
, &ht
);
12294 md5_finish_ctx (&ctx
, checksum_before_op1
);
12298 tem
= fold_binary_loc (loc
, code
, type
, op0
, op1
);
12300 tem
= build2_stat_loc (loc
, code
, type
, op0
, op1 PASS_MEM_STAT
);
12302 #ifdef ENABLE_FOLD_CHECKING
12303 md5_init_ctx (&ctx
);
12304 fold_checksum_tree (op0
, &ctx
, &ht
);
12305 md5_finish_ctx (&ctx
, checksum_after_op0
);
12308 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
12309 fold_check_failed (op0
, tem
);
12311 md5_init_ctx (&ctx
);
12312 fold_checksum_tree (op1
, &ctx
, &ht
);
12313 md5_finish_ctx (&ctx
, checksum_after_op1
);
12315 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
12316 fold_check_failed (op1
, tem
);
12321 /* Fold a ternary tree expression with code CODE of type TYPE with
12322 operands OP0, OP1, and OP2. Return a folded expression if
12323 successful. Otherwise, return a tree expression with code CODE of
12324 type TYPE with operands OP0, OP1, and OP2. */
12327 fold_build3_stat_loc (location_t loc
, enum tree_code code
, tree type
,
12328 tree op0
, tree op1
, tree op2 MEM_STAT_DECL
)
12331 #ifdef ENABLE_FOLD_CHECKING
12332 unsigned char checksum_before_op0
[16],
12333 checksum_before_op1
[16],
12334 checksum_before_op2
[16],
12335 checksum_after_op0
[16],
12336 checksum_after_op1
[16],
12337 checksum_after_op2
[16];
12338 struct md5_ctx ctx
;
12339 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12341 md5_init_ctx (&ctx
);
12342 fold_checksum_tree (op0
, &ctx
, &ht
);
12343 md5_finish_ctx (&ctx
, checksum_before_op0
);
12346 md5_init_ctx (&ctx
);
12347 fold_checksum_tree (op1
, &ctx
, &ht
);
12348 md5_finish_ctx (&ctx
, checksum_before_op1
);
12351 md5_init_ctx (&ctx
);
12352 fold_checksum_tree (op2
, &ctx
, &ht
);
12353 md5_finish_ctx (&ctx
, checksum_before_op2
);
12357 gcc_assert (TREE_CODE_CLASS (code
) != tcc_vl_exp
);
12358 tem
= fold_ternary_loc (loc
, code
, type
, op0
, op1
, op2
);
12360 tem
= build3_stat_loc (loc
, code
, type
, op0
, op1
, op2 PASS_MEM_STAT
);
12362 #ifdef ENABLE_FOLD_CHECKING
12363 md5_init_ctx (&ctx
);
12364 fold_checksum_tree (op0
, &ctx
, &ht
);
12365 md5_finish_ctx (&ctx
, checksum_after_op0
);
12368 if (memcmp (checksum_before_op0
, checksum_after_op0
, 16))
12369 fold_check_failed (op0
, tem
);
12371 md5_init_ctx (&ctx
);
12372 fold_checksum_tree (op1
, &ctx
, &ht
);
12373 md5_finish_ctx (&ctx
, checksum_after_op1
);
12376 if (memcmp (checksum_before_op1
, checksum_after_op1
, 16))
12377 fold_check_failed (op1
, tem
);
12379 md5_init_ctx (&ctx
);
12380 fold_checksum_tree (op2
, &ctx
, &ht
);
12381 md5_finish_ctx (&ctx
, checksum_after_op2
);
12383 if (memcmp (checksum_before_op2
, checksum_after_op2
, 16))
12384 fold_check_failed (op2
, tem
);
12389 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12390 arguments in ARGARRAY, and a null static chain.
12391 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12392 of type TYPE from the given operands as constructed by build_call_array. */
12395 fold_build_call_array_loc (location_t loc
, tree type
, tree fn
,
12396 int nargs
, tree
*argarray
)
12399 #ifdef ENABLE_FOLD_CHECKING
12400 unsigned char checksum_before_fn
[16],
12401 checksum_before_arglist
[16],
12402 checksum_after_fn
[16],
12403 checksum_after_arglist
[16];
12404 struct md5_ctx ctx
;
12405 hash_table
<nofree_ptr_hash
<const tree_node
> > ht (32);
12408 md5_init_ctx (&ctx
);
12409 fold_checksum_tree (fn
, &ctx
, &ht
);
12410 md5_finish_ctx (&ctx
, checksum_before_fn
);
12413 md5_init_ctx (&ctx
);
12414 for (i
= 0; i
< nargs
; i
++)
12415 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
12416 md5_finish_ctx (&ctx
, checksum_before_arglist
);
12420 tem
= fold_builtin_call_array (loc
, type
, fn
, nargs
, argarray
);
12422 tem
= build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
12424 #ifdef ENABLE_FOLD_CHECKING
12425 md5_init_ctx (&ctx
);
12426 fold_checksum_tree (fn
, &ctx
, &ht
);
12427 md5_finish_ctx (&ctx
, checksum_after_fn
);
12430 if (memcmp (checksum_before_fn
, checksum_after_fn
, 16))
12431 fold_check_failed (fn
, tem
);
12433 md5_init_ctx (&ctx
);
12434 for (i
= 0; i
< nargs
; i
++)
12435 fold_checksum_tree (argarray
[i
], &ctx
, &ht
);
12436 md5_finish_ctx (&ctx
, checksum_after_arglist
);
12438 if (memcmp (checksum_before_arglist
, checksum_after_arglist
, 16))
12439 fold_check_failed (NULL_TREE
, tem
);
12444 /* Perform constant folding and related simplification of initializer
12445 expression EXPR. These behave identically to "fold_buildN" but ignore
12446 potential run-time traps and exceptions that fold must preserve. */
12448 #define START_FOLD_INIT \
12449 int saved_signaling_nans = flag_signaling_nans;\
12450 int saved_trapping_math = flag_trapping_math;\
12451 int saved_rounding_math = flag_rounding_math;\
12452 int saved_trapv = flag_trapv;\
12453 int saved_folding_initializer = folding_initializer;\
12454 flag_signaling_nans = 0;\
12455 flag_trapping_math = 0;\
12456 flag_rounding_math = 0;\
12458 folding_initializer = 1;
12460 #define END_FOLD_INIT \
12461 flag_signaling_nans = saved_signaling_nans;\
12462 flag_trapping_math = saved_trapping_math;\
12463 flag_rounding_math = saved_rounding_math;\
12464 flag_trapv = saved_trapv;\
12465 folding_initializer = saved_folding_initializer;
12468 fold_build1_initializer_loc (location_t loc
, enum tree_code code
,
12469 tree type
, tree op
)
12474 result
= fold_build1_loc (loc
, code
, type
, op
);
12481 fold_build2_initializer_loc (location_t loc
, enum tree_code code
,
12482 tree type
, tree op0
, tree op1
)
12487 result
= fold_build2_loc (loc
, code
, type
, op0
, op1
);
12494 fold_build_call_array_initializer_loc (location_t loc
, tree type
, tree fn
,
12495 int nargs
, tree
*argarray
)
12500 result
= fold_build_call_array_loc (loc
, type
, fn
, nargs
, argarray
);
12506 #undef START_FOLD_INIT
12507 #undef END_FOLD_INIT
12509 /* Determine if first argument is a multiple of second argument. Return 0 if
12510 it is not, or we cannot easily determined it to be.
12512 An example of the sort of thing we care about (at this point; this routine
12513 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12514 fold cases do now) is discovering that
12516 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12522 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12524 This code also handles discovering that
12526 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12528 is a multiple of 8 so we don't have to worry about dealing with a
12529 possible remainder.
12531 Note that we *look* inside a SAVE_EXPR only to determine how it was
12532 calculated; it is not safe for fold to do much of anything else with the
12533 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12534 at run time. For example, the latter example above *cannot* be implemented
12535 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12536 evaluation time of the original SAVE_EXPR is not necessarily the same at
12537 the time the new expression is evaluated. The only optimization of this
12538 sort that would be valid is changing
12540 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12544 SAVE_EXPR (I) * SAVE_EXPR (J)
12546 (where the same SAVE_EXPR (J) is used in the original and the
12547 transformed version). */
12550 multiple_of_p (tree type
, const_tree top
, const_tree bottom
)
12552 if (operand_equal_p (top
, bottom
, 0))
12555 if (TREE_CODE (type
) != INTEGER_TYPE
)
12558 switch (TREE_CODE (top
))
12561 /* Bitwise and provides a power of two multiple. If the mask is
12562 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12563 if (!integer_pow2p (bottom
))
12568 return (multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
)
12569 || multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
));
12573 return (multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
)
12574 && multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
));
12577 if (TREE_CODE (TREE_OPERAND (top
, 1)) == INTEGER_CST
)
12581 op1
= TREE_OPERAND (top
, 1);
12582 /* const_binop may not detect overflow correctly,
12583 so check for it explicitly here. */
12584 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node
)), op1
)
12585 && 0 != (t1
= fold_convert (type
,
12586 const_binop (LSHIFT_EXPR
,
12589 && !TREE_OVERFLOW (t1
))
12590 return multiple_of_p (type
, t1
, bottom
);
12595 /* Can't handle conversions from non-integral or wider integral type. */
12596 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top
, 0))) != INTEGER_TYPE
)
12597 || (TYPE_PRECISION (type
)
12598 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top
, 0)))))
12601 /* .. fall through ... */
12604 return multiple_of_p (type
, TREE_OPERAND (top
, 0), bottom
);
12607 return (multiple_of_p (type
, TREE_OPERAND (top
, 1), bottom
)
12608 && multiple_of_p (type
, TREE_OPERAND (top
, 2), bottom
));
12611 if (TREE_CODE (bottom
) != INTEGER_CST
12612 || integer_zerop (bottom
)
12613 || (TYPE_UNSIGNED (type
)
12614 && (tree_int_cst_sgn (top
) < 0
12615 || tree_int_cst_sgn (bottom
) < 0)))
12617 return wi::multiple_of_p (wi::to_widest (top
), wi::to_widest (bottom
),
12625 #define tree_expr_nonnegative_warnv_p(X, Y) \
12626 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12628 #define RECURSE(X) \
12629 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12631 /* Return true if CODE or TYPE is known to be non-negative. */
12634 tree_simple_nonnegative_warnv_p (enum tree_code code
, tree type
)
12636 if ((TYPE_PRECISION (type
) != 1 || TYPE_UNSIGNED (type
))
12637 && truth_value_p (code
))
12638 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12639 have a signed:1 type (where the value is -1 and 0). */
12644 /* Return true if (CODE OP0) is known to be non-negative. If the return
12645 value is based on the assumption that signed overflow is undefined,
12646 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12647 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12650 tree_unary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
12651 bool *strict_overflow_p
, int depth
)
12653 if (TYPE_UNSIGNED (type
))
12659 /* We can't return 1 if flag_wrapv is set because
12660 ABS_EXPR<INT_MIN> = INT_MIN. */
12661 if (!ANY_INTEGRAL_TYPE_P (type
))
12663 if (TYPE_OVERFLOW_UNDEFINED (type
))
12665 *strict_overflow_p
= true;
12670 case NON_LVALUE_EXPR
:
12672 case FIX_TRUNC_EXPR
:
12673 return RECURSE (op0
);
12677 tree inner_type
= TREE_TYPE (op0
);
12678 tree outer_type
= type
;
12680 if (TREE_CODE (outer_type
) == REAL_TYPE
)
12682 if (TREE_CODE (inner_type
) == REAL_TYPE
)
12683 return RECURSE (op0
);
12684 if (INTEGRAL_TYPE_P (inner_type
))
12686 if (TYPE_UNSIGNED (inner_type
))
12688 return RECURSE (op0
);
12691 else if (INTEGRAL_TYPE_P (outer_type
))
12693 if (TREE_CODE (inner_type
) == REAL_TYPE
)
12694 return RECURSE (op0
);
12695 if (INTEGRAL_TYPE_P (inner_type
))
12696 return TYPE_PRECISION (inner_type
) < TYPE_PRECISION (outer_type
)
12697 && TYPE_UNSIGNED (inner_type
);
12703 return tree_simple_nonnegative_warnv_p (code
, type
);
12706 /* We don't know sign of `t', so be conservative and return false. */
12710 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12711 value is based on the assumption that signed overflow is undefined,
12712 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12713 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12716 tree_binary_nonnegative_warnv_p (enum tree_code code
, tree type
, tree op0
,
12717 tree op1
, bool *strict_overflow_p
,
12720 if (TYPE_UNSIGNED (type
))
12725 case POINTER_PLUS_EXPR
:
12727 if (FLOAT_TYPE_P (type
))
12728 return RECURSE (op0
) && RECURSE (op1
);
12730 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12731 both unsigned and at least 2 bits shorter than the result. */
12732 if (TREE_CODE (type
) == INTEGER_TYPE
12733 && TREE_CODE (op0
) == NOP_EXPR
12734 && TREE_CODE (op1
) == NOP_EXPR
)
12736 tree inner1
= TREE_TYPE (TREE_OPERAND (op0
, 0));
12737 tree inner2
= TREE_TYPE (TREE_OPERAND (op1
, 0));
12738 if (TREE_CODE (inner1
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner1
)
12739 && TREE_CODE (inner2
) == INTEGER_TYPE
&& TYPE_UNSIGNED (inner2
))
12741 unsigned int prec
= MAX (TYPE_PRECISION (inner1
),
12742 TYPE_PRECISION (inner2
)) + 1;
12743 return prec
< TYPE_PRECISION (type
);
12749 if (FLOAT_TYPE_P (type
) || TYPE_OVERFLOW_UNDEFINED (type
))
12751 /* x * x is always non-negative for floating point x
12752 or without overflow. */
12753 if (operand_equal_p (op0
, op1
, 0)
12754 || (RECURSE (op0
) && RECURSE (op1
)))
12756 if (ANY_INTEGRAL_TYPE_P (type
)
12757 && TYPE_OVERFLOW_UNDEFINED (type
))
12758 *strict_overflow_p
= true;
12763 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12764 both unsigned and their total bits is shorter than the result. */
12765 if (TREE_CODE (type
) == INTEGER_TYPE
12766 && (TREE_CODE (op0
) == NOP_EXPR
|| TREE_CODE (op0
) == INTEGER_CST
)
12767 && (TREE_CODE (op1
) == NOP_EXPR
|| TREE_CODE (op1
) == INTEGER_CST
))
12769 tree inner0
= (TREE_CODE (op0
) == NOP_EXPR
)
12770 ? TREE_TYPE (TREE_OPERAND (op0
, 0))
12772 tree inner1
= (TREE_CODE (op1
) == NOP_EXPR
)
12773 ? TREE_TYPE (TREE_OPERAND (op1
, 0))
12776 bool unsigned0
= TYPE_UNSIGNED (inner0
);
12777 bool unsigned1
= TYPE_UNSIGNED (inner1
);
12779 if (TREE_CODE (op0
) == INTEGER_CST
)
12780 unsigned0
= unsigned0
|| tree_int_cst_sgn (op0
) >= 0;
12782 if (TREE_CODE (op1
) == INTEGER_CST
)
12783 unsigned1
= unsigned1
|| tree_int_cst_sgn (op1
) >= 0;
12785 if (TREE_CODE (inner0
) == INTEGER_TYPE
&& unsigned0
12786 && TREE_CODE (inner1
) == INTEGER_TYPE
&& unsigned1
)
12788 unsigned int precision0
= (TREE_CODE (op0
) == INTEGER_CST
)
12789 ? tree_int_cst_min_precision (op0
, UNSIGNED
)
12790 : TYPE_PRECISION (inner0
);
12792 unsigned int precision1
= (TREE_CODE (op1
) == INTEGER_CST
)
12793 ? tree_int_cst_min_precision (op1
, UNSIGNED
)
12794 : TYPE_PRECISION (inner1
);
12796 return precision0
+ precision1
< TYPE_PRECISION (type
);
12803 return RECURSE (op0
) || RECURSE (op1
);
12809 case TRUNC_DIV_EXPR
:
12810 case CEIL_DIV_EXPR
:
12811 case FLOOR_DIV_EXPR
:
12812 case ROUND_DIV_EXPR
:
12813 return RECURSE (op0
) && RECURSE (op1
);
12815 case TRUNC_MOD_EXPR
:
12816 return RECURSE (op0
);
12818 case FLOOR_MOD_EXPR
:
12819 return RECURSE (op1
);
12821 case CEIL_MOD_EXPR
:
12822 case ROUND_MOD_EXPR
:
12824 return tree_simple_nonnegative_warnv_p (code
, type
);
12827 /* We don't know sign of `t', so be conservative and return false. */
12831 /* Return true if T is known to be non-negative. If the return
12832 value is based on the assumption that signed overflow is undefined,
12833 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12834 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12837 tree_single_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
12839 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
12842 switch (TREE_CODE (t
))
12845 return tree_int_cst_sgn (t
) >= 0;
12848 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t
));
12851 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t
));
12854 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
12857 /* Limit the depth of recursion to avoid quadratic behavior.
12858 This is expected to catch almost all occurrences in practice.
12859 If this code misses important cases that unbounded recursion
12860 would not, passes that need this information could be revised
12861 to provide it through dataflow propagation. */
12862 return (!name_registered_for_update_p (t
)
12863 && depth
< PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH
)
12864 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t
),
12865 strict_overflow_p
, depth
));
12868 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
12872 /* Return true if T is known to be non-negative. If the return
12873 value is based on the assumption that signed overflow is undefined,
12874 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12875 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12878 tree_call_nonnegative_warnv_p (tree type
, tree fndecl
, tree arg0
, tree arg1
,
12879 bool *strict_overflow_p
, int depth
)
12881 if (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
12882 switch (DECL_FUNCTION_CODE (fndecl
))
12884 CASE_FLT_FN (BUILT_IN_ACOS
):
12885 CASE_FLT_FN (BUILT_IN_ACOSH
):
12886 CASE_FLT_FN (BUILT_IN_CABS
):
12887 CASE_FLT_FN (BUILT_IN_COSH
):
12888 CASE_FLT_FN (BUILT_IN_ERFC
):
12889 CASE_FLT_FN (BUILT_IN_EXP
):
12890 CASE_FLT_FN (BUILT_IN_EXP10
):
12891 CASE_FLT_FN (BUILT_IN_EXP2
):
12892 CASE_FLT_FN (BUILT_IN_FABS
):
12893 CASE_FLT_FN (BUILT_IN_FDIM
):
12894 CASE_FLT_FN (BUILT_IN_HYPOT
):
12895 CASE_FLT_FN (BUILT_IN_POW10
):
12896 CASE_INT_FN (BUILT_IN_FFS
):
12897 CASE_INT_FN (BUILT_IN_PARITY
):
12898 CASE_INT_FN (BUILT_IN_POPCOUNT
):
12899 CASE_INT_FN (BUILT_IN_CLZ
):
12900 CASE_INT_FN (BUILT_IN_CLRSB
):
12901 case BUILT_IN_BSWAP32
:
12902 case BUILT_IN_BSWAP64
:
12906 CASE_FLT_FN (BUILT_IN_SQRT
):
12907 /* sqrt(-0.0) is -0.0. */
12908 if (!HONOR_SIGNED_ZEROS (element_mode (type
)))
12910 return RECURSE (arg0
);
12912 CASE_FLT_FN (BUILT_IN_ASINH
):
12913 CASE_FLT_FN (BUILT_IN_ATAN
):
12914 CASE_FLT_FN (BUILT_IN_ATANH
):
12915 CASE_FLT_FN (BUILT_IN_CBRT
):
12916 CASE_FLT_FN (BUILT_IN_CEIL
):
12917 CASE_FLT_FN (BUILT_IN_ERF
):
12918 CASE_FLT_FN (BUILT_IN_EXPM1
):
12919 CASE_FLT_FN (BUILT_IN_FLOOR
):
12920 CASE_FLT_FN (BUILT_IN_FMOD
):
12921 CASE_FLT_FN (BUILT_IN_FREXP
):
12922 CASE_FLT_FN (BUILT_IN_ICEIL
):
12923 CASE_FLT_FN (BUILT_IN_IFLOOR
):
12924 CASE_FLT_FN (BUILT_IN_IRINT
):
12925 CASE_FLT_FN (BUILT_IN_IROUND
):
12926 CASE_FLT_FN (BUILT_IN_LCEIL
):
12927 CASE_FLT_FN (BUILT_IN_LDEXP
):
12928 CASE_FLT_FN (BUILT_IN_LFLOOR
):
12929 CASE_FLT_FN (BUILT_IN_LLCEIL
):
12930 CASE_FLT_FN (BUILT_IN_LLFLOOR
):
12931 CASE_FLT_FN (BUILT_IN_LLRINT
):
12932 CASE_FLT_FN (BUILT_IN_LLROUND
):
12933 CASE_FLT_FN (BUILT_IN_LRINT
):
12934 CASE_FLT_FN (BUILT_IN_LROUND
):
12935 CASE_FLT_FN (BUILT_IN_MODF
):
12936 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
12937 CASE_FLT_FN (BUILT_IN_RINT
):
12938 CASE_FLT_FN (BUILT_IN_ROUND
):
12939 CASE_FLT_FN (BUILT_IN_SCALB
):
12940 CASE_FLT_FN (BUILT_IN_SCALBLN
):
12941 CASE_FLT_FN (BUILT_IN_SCALBN
):
12942 CASE_FLT_FN (BUILT_IN_SIGNBIT
):
12943 CASE_FLT_FN (BUILT_IN_SIGNIFICAND
):
12944 CASE_FLT_FN (BUILT_IN_SINH
):
12945 CASE_FLT_FN (BUILT_IN_TANH
):
12946 CASE_FLT_FN (BUILT_IN_TRUNC
):
12947 /* True if the 1st argument is nonnegative. */
12948 return RECURSE (arg0
);
12950 CASE_FLT_FN (BUILT_IN_FMAX
):
12951 /* True if the 1st OR 2nd arguments are nonnegative. */
12952 return RECURSE (arg0
) || RECURSE (arg1
);
12954 CASE_FLT_FN (BUILT_IN_FMIN
):
12955 /* True if the 1st AND 2nd arguments are nonnegative. */
12956 return RECURSE (arg0
) && RECURSE (arg1
);
12958 CASE_FLT_FN (BUILT_IN_COPYSIGN
):
12959 /* True if the 2nd argument is nonnegative. */
12960 return RECURSE (arg1
);
12962 CASE_FLT_FN (BUILT_IN_POWI
):
12963 /* True if the 1st argument is nonnegative or the second
12964 argument is an even integer. */
12965 if (TREE_CODE (arg1
) == INTEGER_CST
12966 && (TREE_INT_CST_LOW (arg1
) & 1) == 0)
12968 return RECURSE (arg0
);
12970 CASE_FLT_FN (BUILT_IN_POW
):
12971 /* True if the 1st argument is nonnegative or the second
12972 argument is an even integer valued real. */
12973 if (TREE_CODE (arg1
) == REAL_CST
)
12978 c
= TREE_REAL_CST (arg1
);
12979 n
= real_to_integer (&c
);
12982 REAL_VALUE_TYPE cint
;
12983 real_from_integer (&cint
, VOIDmode
, n
, SIGNED
);
12984 if (real_identical (&c
, &cint
))
12988 return RECURSE (arg0
);
12993 return tree_simple_nonnegative_warnv_p (CALL_EXPR
, type
);
12996 /* Return true if T is known to be non-negative. If the return
12997 value is based on the assumption that signed overflow is undefined,
12998 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12999 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13002 tree_invalid_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
13004 enum tree_code code
= TREE_CODE (t
);
13005 if (TYPE_UNSIGNED (TREE_TYPE (t
)))
13012 tree temp
= TARGET_EXPR_SLOT (t
);
13013 t
= TARGET_EXPR_INITIAL (t
);
13015 /* If the initializer is non-void, then it's a normal expression
13016 that will be assigned to the slot. */
13017 if (!VOID_TYPE_P (t
))
13018 return RECURSE (t
);
13020 /* Otherwise, the initializer sets the slot in some way. One common
13021 way is an assignment statement at the end of the initializer. */
13024 if (TREE_CODE (t
) == BIND_EXPR
)
13025 t
= expr_last (BIND_EXPR_BODY (t
));
13026 else if (TREE_CODE (t
) == TRY_FINALLY_EXPR
13027 || TREE_CODE (t
) == TRY_CATCH_EXPR
)
13028 t
= expr_last (TREE_OPERAND (t
, 0));
13029 else if (TREE_CODE (t
) == STATEMENT_LIST
)
13034 if (TREE_CODE (t
) == MODIFY_EXPR
13035 && TREE_OPERAND (t
, 0) == temp
)
13036 return RECURSE (TREE_OPERAND (t
, 1));
13043 tree arg0
= call_expr_nargs (t
) > 0 ? CALL_EXPR_ARG (t
, 0) : NULL_TREE
;
13044 tree arg1
= call_expr_nargs (t
) > 1 ? CALL_EXPR_ARG (t
, 1) : NULL_TREE
;
13046 return tree_call_nonnegative_warnv_p (TREE_TYPE (t
),
13047 get_callee_fndecl (t
),
13050 strict_overflow_p
, depth
);
13052 case COMPOUND_EXPR
:
13054 return RECURSE (TREE_OPERAND (t
, 1));
13057 return RECURSE (expr_last (TREE_OPERAND (t
, 1)));
13060 return RECURSE (TREE_OPERAND (t
, 0));
13063 return tree_simple_nonnegative_warnv_p (TREE_CODE (t
), TREE_TYPE (t
));
13068 #undef tree_expr_nonnegative_warnv_p
13070 /* Return true if T is known to be non-negative. If the return
13071 value is based on the assumption that signed overflow is undefined,
13072 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13073 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13076 tree_expr_nonnegative_warnv_p (tree t
, bool *strict_overflow_p
, int depth
)
13078 enum tree_code code
;
13079 if (t
== error_mark_node
)
13082 code
= TREE_CODE (t
);
13083 switch (TREE_CODE_CLASS (code
))
13086 case tcc_comparison
:
13087 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
13089 TREE_OPERAND (t
, 0),
13090 TREE_OPERAND (t
, 1),
13091 strict_overflow_p
, depth
);
13094 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
13096 TREE_OPERAND (t
, 0),
13097 strict_overflow_p
, depth
);
13100 case tcc_declaration
:
13101 case tcc_reference
:
13102 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13110 case TRUTH_AND_EXPR
:
13111 case TRUTH_OR_EXPR
:
13112 case TRUTH_XOR_EXPR
:
13113 return tree_binary_nonnegative_warnv_p (TREE_CODE (t
),
13115 TREE_OPERAND (t
, 0),
13116 TREE_OPERAND (t
, 1),
13117 strict_overflow_p
, depth
);
13118 case TRUTH_NOT_EXPR
:
13119 return tree_unary_nonnegative_warnv_p (TREE_CODE (t
),
13121 TREE_OPERAND (t
, 0),
13122 strict_overflow_p
, depth
);
13129 case WITH_SIZE_EXPR
:
13131 return tree_single_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13134 return tree_invalid_nonnegative_warnv_p (t
, strict_overflow_p
, depth
);
13138 /* Return true if `t' is known to be non-negative. Handle warnings
13139 about undefined signed overflow. */
13142 tree_expr_nonnegative_p (tree t
)
13144 bool ret
, strict_overflow_p
;
13146 strict_overflow_p
= false;
13147 ret
= tree_expr_nonnegative_warnv_p (t
, &strict_overflow_p
);
13148 if (strict_overflow_p
)
13149 fold_overflow_warning (("assuming signed overflow does not occur when "
13150 "determining that expression is always "
13152 WARN_STRICT_OVERFLOW_MISC
);
13157 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13158 For floating point we further ensure that T is not denormal.
13159 Similar logic is present in nonzero_address in rtlanal.h.
13161 If the return value is based on the assumption that signed overflow
13162 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13163 change *STRICT_OVERFLOW_P. */
13166 tree_unary_nonzero_warnv_p (enum tree_code code
, tree type
, tree op0
,
13167 bool *strict_overflow_p
)
13172 return tree_expr_nonzero_warnv_p (op0
,
13173 strict_overflow_p
);
13177 tree inner_type
= TREE_TYPE (op0
);
13178 tree outer_type
= type
;
13180 return (TYPE_PRECISION (outer_type
) >= TYPE_PRECISION (inner_type
)
13181 && tree_expr_nonzero_warnv_p (op0
,
13182 strict_overflow_p
));
13186 case NON_LVALUE_EXPR
:
13187 return tree_expr_nonzero_warnv_p (op0
,
13188 strict_overflow_p
);
13197 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13198 For floating point we further ensure that T is not denormal.
13199 Similar logic is present in nonzero_address in rtlanal.h.
13201 If the return value is based on the assumption that signed overflow
13202 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13203 change *STRICT_OVERFLOW_P. */
13206 tree_binary_nonzero_warnv_p (enum tree_code code
,
13209 tree op1
, bool *strict_overflow_p
)
13211 bool sub_strict_overflow_p
;
13214 case POINTER_PLUS_EXPR
:
13216 if (ANY_INTEGRAL_TYPE_P (type
) && TYPE_OVERFLOW_UNDEFINED (type
))
13218 /* With the presence of negative values it is hard
13219 to say something. */
13220 sub_strict_overflow_p
= false;
13221 if (!tree_expr_nonnegative_warnv_p (op0
,
13222 &sub_strict_overflow_p
)
13223 || !tree_expr_nonnegative_warnv_p (op1
,
13224 &sub_strict_overflow_p
))
13226 /* One of operands must be positive and the other non-negative. */
13227 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13228 overflows, on a twos-complement machine the sum of two
13229 nonnegative numbers can never be zero. */
13230 return (tree_expr_nonzero_warnv_p (op0
,
13232 || tree_expr_nonzero_warnv_p (op1
,
13233 strict_overflow_p
));
13238 if (TYPE_OVERFLOW_UNDEFINED (type
))
13240 if (tree_expr_nonzero_warnv_p (op0
,
13242 && tree_expr_nonzero_warnv_p (op1
,
13243 strict_overflow_p
))
13245 *strict_overflow_p
= true;
13252 sub_strict_overflow_p
= false;
13253 if (tree_expr_nonzero_warnv_p (op0
,
13254 &sub_strict_overflow_p
)
13255 && tree_expr_nonzero_warnv_p (op1
,
13256 &sub_strict_overflow_p
))
13258 if (sub_strict_overflow_p
)
13259 *strict_overflow_p
= true;
13264 sub_strict_overflow_p
= false;
13265 if (tree_expr_nonzero_warnv_p (op0
,
13266 &sub_strict_overflow_p
))
13268 if (sub_strict_overflow_p
)
13269 *strict_overflow_p
= true;
13271 /* When both operands are nonzero, then MAX must be too. */
13272 if (tree_expr_nonzero_warnv_p (op1
,
13273 strict_overflow_p
))
13276 /* MAX where operand 0 is positive is positive. */
13277 return tree_expr_nonnegative_warnv_p (op0
,
13278 strict_overflow_p
);
13280 /* MAX where operand 1 is positive is positive. */
13281 else if (tree_expr_nonzero_warnv_p (op1
,
13282 &sub_strict_overflow_p
)
13283 && tree_expr_nonnegative_warnv_p (op1
,
13284 &sub_strict_overflow_p
))
13286 if (sub_strict_overflow_p
)
13287 *strict_overflow_p
= true;
13293 return (tree_expr_nonzero_warnv_p (op1
,
13295 || tree_expr_nonzero_warnv_p (op0
,
13296 strict_overflow_p
));
13305 /* Return true when T is an address and is known to be nonzero.
13306 For floating point we further ensure that T is not denormal.
13307 Similar logic is present in nonzero_address in rtlanal.h.
13309 If the return value is based on the assumption that signed overflow
13310 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13311 change *STRICT_OVERFLOW_P. */
13314 tree_single_nonzero_warnv_p (tree t
, bool *strict_overflow_p
)
13316 bool sub_strict_overflow_p
;
13317 switch (TREE_CODE (t
))
13320 return !integer_zerop (t
);
13324 tree base
= TREE_OPERAND (t
, 0);
13326 if (!DECL_P (base
))
13327 base
= get_base_address (base
);
13332 /* For objects in symbol table check if we know they are non-zero.
13333 Don't do anything for variables and functions before symtab is built;
13334 it is quite possible that they will be declared weak later. */
13335 if (DECL_P (base
) && decl_in_symtab_p (base
))
13337 struct symtab_node
*symbol
;
13339 symbol
= symtab_node::get_create (base
);
13341 return symbol
->nonzero_address ();
13346 /* Function local objects are never NULL. */
13348 && (DECL_CONTEXT (base
)
13349 && TREE_CODE (DECL_CONTEXT (base
)) == FUNCTION_DECL
13350 && auto_var_in_fn_p (base
, DECL_CONTEXT (base
))))
13353 /* Constants are never weak. */
13354 if (CONSTANT_CLASS_P (base
))
13361 sub_strict_overflow_p
= false;
13362 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 1),
13363 &sub_strict_overflow_p
)
13364 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t
, 2),
13365 &sub_strict_overflow_p
))
13367 if (sub_strict_overflow_p
)
13368 *strict_overflow_p
= true;
13379 #define integer_valued_real_p(X) \
13380 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13382 #define RECURSE(X) \
13383 ((integer_valued_real_p) (X, depth + 1))
13385 /* Return true if the floating point result of (CODE OP0) has an
13386 integer value. We also allow +Inf, -Inf and NaN to be considered
13389 DEPTH is the current nesting depth of the query. */
13392 integer_valued_real_unary_p (tree_code code
, tree op0
, int depth
)
13400 return RECURSE (op0
);
13404 tree type
= TREE_TYPE (op0
);
13405 if (TREE_CODE (type
) == INTEGER_TYPE
)
13407 if (TREE_CODE (type
) == REAL_TYPE
)
13408 return RECURSE (op0
);
13418 /* Return true if the floating point result of (CODE OP0 OP1) has an
13419 integer value. We also allow +Inf, -Inf and NaN to be considered
13422 DEPTH is the current nesting depth of the query. */
13425 integer_valued_real_binary_p (tree_code code
, tree op0
, tree op1
, int depth
)
13434 return RECURSE (op0
) && RECURSE (op1
);
13442 /* Return true if the floating point result of calling FNDECL with arguments
13443 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13444 considered integer values. If FNDECL takes fewer than 2 arguments,
13445 the remaining ARGn are null.
13447 DEPTH is the current nesting depth of the query. */
13450 integer_valued_real_call_p (tree fndecl
, tree arg0
, tree arg1
, int depth
)
13452 if (fndecl
&& DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
13453 switch (DECL_FUNCTION_CODE (fndecl
))
13455 CASE_FLT_FN (BUILT_IN_CEIL
):
13456 CASE_FLT_FN (BUILT_IN_FLOOR
):
13457 CASE_FLT_FN (BUILT_IN_NEARBYINT
):
13458 CASE_FLT_FN (BUILT_IN_RINT
):
13459 CASE_FLT_FN (BUILT_IN_ROUND
):
13460 CASE_FLT_FN (BUILT_IN_TRUNC
):
13463 CASE_FLT_FN (BUILT_IN_FMIN
):
13464 CASE_FLT_FN (BUILT_IN_FMAX
):
13465 return RECURSE (arg0
) && RECURSE (arg1
);
13473 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13474 has an integer value. We also allow +Inf, -Inf and NaN to be
13475 considered integer values.
13477 DEPTH is the current nesting depth of the query. */
13480 integer_valued_real_single_p (tree t
, int depth
)
13482 switch (TREE_CODE (t
))
13485 return real_isinteger (TREE_REAL_CST_PTR (t
), TYPE_MODE (TREE_TYPE (t
)));
13488 return RECURSE (TREE_OPERAND (t
, 1)) && RECURSE (TREE_OPERAND (t
, 2));
13491 /* Limit the depth of recursion to avoid quadratic behavior.
13492 This is expected to catch almost all occurrences in practice.
13493 If this code misses important cases that unbounded recursion
13494 would not, passes that need this information could be revised
13495 to provide it through dataflow propagation. */
13496 return (!name_registered_for_update_p (t
)
13497 && depth
< PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH
)
13498 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t
),
13507 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13508 has an integer value. We also allow +Inf, -Inf and NaN to be
13509 considered integer values.
13511 DEPTH is the current nesting depth of the query. */
13514 integer_valued_real_invalid_p (tree t
, int depth
)
13516 switch (TREE_CODE (t
))
13518 case COMPOUND_EXPR
:
13521 return RECURSE (TREE_OPERAND (t
, 1));
13524 return RECURSE (TREE_OPERAND (t
, 0));
13533 #undef integer_valued_real_p
13535 /* Return true if the floating point expression T has an integer value.
13536 We also allow +Inf, -Inf and NaN to be considered integer values.
13538 DEPTH is the current nesting depth of the query. */
13541 integer_valued_real_p (tree t
, int depth
)
13543 if (t
== error_mark_node
)
13546 tree_code code
= TREE_CODE (t
);
13547 switch (TREE_CODE_CLASS (code
))
13550 case tcc_comparison
:
13551 return integer_valued_real_binary_p (code
, TREE_OPERAND (t
, 0),
13552 TREE_OPERAND (t
, 1), depth
);
13555 return integer_valued_real_unary_p (code
, TREE_OPERAND (t
, 0), depth
);
13558 case tcc_declaration
:
13559 case tcc_reference
:
13560 return integer_valued_real_single_p (t
, depth
);
13570 return integer_valued_real_single_p (t
, depth
);
13574 tree arg0
= (call_expr_nargs (t
) > 0
13575 ? CALL_EXPR_ARG (t
, 0)
13577 tree arg1
= (call_expr_nargs (t
) > 1
13578 ? CALL_EXPR_ARG (t
, 1)
13580 return integer_valued_real_call_p (get_callee_fndecl (t
),
13581 arg0
, arg1
, depth
);
13585 return integer_valued_real_invalid_p (t
, depth
);
13589 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13590 attempt to fold the expression to a constant without modifying TYPE,
13593 If the expression could be simplified to a constant, then return
13594 the constant. If the expression would not be simplified to a
13595 constant, then return NULL_TREE. */
13598 fold_binary_to_constant (enum tree_code code
, tree type
, tree op0
, tree op1
)
13600 tree tem
= fold_binary (code
, type
, op0
, op1
);
13601 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
13604 /* Given the components of a unary expression CODE, TYPE and OP0,
13605 attempt to fold the expression to a constant without modifying
13608 If the expression could be simplified to a constant, then return
13609 the constant. If the expression would not be simplified to a
13610 constant, then return NULL_TREE. */
13613 fold_unary_to_constant (enum tree_code code
, tree type
, tree op0
)
13615 tree tem
= fold_unary (code
, type
, op0
);
13616 return (tem
&& TREE_CONSTANT (tem
)) ? tem
: NULL_TREE
;
13619 /* If EXP represents referencing an element in a constant string
13620 (either via pointer arithmetic or array indexing), return the
13621 tree representing the value accessed, otherwise return NULL. */
13624 fold_read_from_constant_string (tree exp
)
13626 if ((TREE_CODE (exp
) == INDIRECT_REF
13627 || TREE_CODE (exp
) == ARRAY_REF
)
13628 && TREE_CODE (TREE_TYPE (exp
)) == INTEGER_TYPE
)
13630 tree exp1
= TREE_OPERAND (exp
, 0);
13633 location_t loc
= EXPR_LOCATION (exp
);
13635 if (TREE_CODE (exp
) == INDIRECT_REF
)
13636 string
= string_constant (exp1
, &index
);
13639 tree low_bound
= array_ref_low_bound (exp
);
13640 index
= fold_convert_loc (loc
, sizetype
, TREE_OPERAND (exp
, 1));
13642 /* Optimize the special-case of a zero lower bound.
13644 We convert the low_bound to sizetype to avoid some problems
13645 with constant folding. (E.g. suppose the lower bound is 1,
13646 and its mode is QI. Without the conversion,l (ARRAY
13647 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13648 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13649 if (! integer_zerop (low_bound
))
13650 index
= size_diffop_loc (loc
, index
,
13651 fold_convert_loc (loc
, sizetype
, low_bound
));
13657 && TYPE_MODE (TREE_TYPE (exp
)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))
13658 && TREE_CODE (string
) == STRING_CST
13659 && TREE_CODE (index
) == INTEGER_CST
13660 && compare_tree_int (index
, TREE_STRING_LENGTH (string
)) < 0
13661 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
))))
13663 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string
)))) == 1))
13664 return build_int_cst_type (TREE_TYPE (exp
),
13665 (TREE_STRING_POINTER (string
)
13666 [TREE_INT_CST_LOW (index
)]));
13671 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13672 an integer constant, real, or fixed-point constant.
13674 TYPE is the type of the result. */
13677 fold_negate_const (tree arg0
, tree type
)
13679 tree t
= NULL_TREE
;
13681 switch (TREE_CODE (arg0
))
13686 wide_int val
= wi::neg (arg0
, &overflow
);
13687 t
= force_fit_type (type
, val
, 1,
13688 (overflow
| TREE_OVERFLOW (arg0
))
13689 && !TYPE_UNSIGNED (type
));
13694 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
13699 FIXED_VALUE_TYPE f
;
13700 bool overflow_p
= fixed_arithmetic (&f
, NEGATE_EXPR
,
13701 &(TREE_FIXED_CST (arg0
)), NULL
,
13702 TYPE_SATURATING (type
));
13703 t
= build_fixed (type
, f
);
13704 /* Propagate overflow flags. */
13705 if (overflow_p
| TREE_OVERFLOW (arg0
))
13706 TREE_OVERFLOW (t
) = 1;
13711 gcc_unreachable ();
13717 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13718 an integer constant or real constant.
13720 TYPE is the type of the result. */
13723 fold_abs_const (tree arg0
, tree type
)
13725 tree t
= NULL_TREE
;
13727 switch (TREE_CODE (arg0
))
13731 /* If the value is unsigned or non-negative, then the absolute value
13732 is the same as the ordinary value. */
13733 if (!wi::neg_p (arg0
, TYPE_SIGN (type
)))
13736 /* If the value is negative, then the absolute value is
13741 wide_int val
= wi::neg (arg0
, &overflow
);
13742 t
= force_fit_type (type
, val
, -1,
13743 overflow
| TREE_OVERFLOW (arg0
));
13749 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0
)))
13750 t
= build_real (type
, real_value_negate (&TREE_REAL_CST (arg0
)));
13756 gcc_unreachable ();
13762 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13763 constant. TYPE is the type of the result. */
13766 fold_not_const (const_tree arg0
, tree type
)
13768 gcc_assert (TREE_CODE (arg0
) == INTEGER_CST
);
13770 return force_fit_type (type
, wi::bit_not (arg0
), 0, TREE_OVERFLOW (arg0
));
13773 /* Given CODE, a relational operator, the target type, TYPE and two
13774 constant operands OP0 and OP1, return the result of the
13775 relational operation. If the result is not a compile time
13776 constant, then return NULL_TREE. */
13779 fold_relational_const (enum tree_code code
, tree type
, tree op0
, tree op1
)
13781 int result
, invert
;
13783 /* From here on, the only cases we handle are when the result is
13784 known to be a constant. */
13786 if (TREE_CODE (op0
) == REAL_CST
&& TREE_CODE (op1
) == REAL_CST
)
13788 const REAL_VALUE_TYPE
*c0
= TREE_REAL_CST_PTR (op0
);
13789 const REAL_VALUE_TYPE
*c1
= TREE_REAL_CST_PTR (op1
);
13791 /* Handle the cases where either operand is a NaN. */
13792 if (real_isnan (c0
) || real_isnan (c1
))
13802 case UNORDERED_EXPR
:
13816 if (flag_trapping_math
)
13822 gcc_unreachable ();
13825 return constant_boolean_node (result
, type
);
13828 return constant_boolean_node (real_compare (code
, c0
, c1
), type
);
13831 if (TREE_CODE (op0
) == FIXED_CST
&& TREE_CODE (op1
) == FIXED_CST
)
13833 const FIXED_VALUE_TYPE
*c0
= TREE_FIXED_CST_PTR (op0
);
13834 const FIXED_VALUE_TYPE
*c1
= TREE_FIXED_CST_PTR (op1
);
13835 return constant_boolean_node (fixed_compare (code
, c0
, c1
), type
);
13838 /* Handle equality/inequality of complex constants. */
13839 if (TREE_CODE (op0
) == COMPLEX_CST
&& TREE_CODE (op1
) == COMPLEX_CST
)
13841 tree rcond
= fold_relational_const (code
, type
,
13842 TREE_REALPART (op0
),
13843 TREE_REALPART (op1
));
13844 tree icond
= fold_relational_const (code
, type
,
13845 TREE_IMAGPART (op0
),
13846 TREE_IMAGPART (op1
));
13847 if (code
== EQ_EXPR
)
13848 return fold_build2 (TRUTH_ANDIF_EXPR
, type
, rcond
, icond
);
13849 else if (code
== NE_EXPR
)
13850 return fold_build2 (TRUTH_ORIF_EXPR
, type
, rcond
, icond
);
13855 if (TREE_CODE (op0
) == VECTOR_CST
&& TREE_CODE (op1
) == VECTOR_CST
)
13857 unsigned count
= VECTOR_CST_NELTS (op0
);
13858 tree
*elts
= XALLOCAVEC (tree
, count
);
13859 gcc_assert (VECTOR_CST_NELTS (op1
) == count
13860 && TYPE_VECTOR_SUBPARTS (type
) == count
);
13862 for (unsigned i
= 0; i
< count
; i
++)
13864 tree elem_type
= TREE_TYPE (type
);
13865 tree elem0
= VECTOR_CST_ELT (op0
, i
);
13866 tree elem1
= VECTOR_CST_ELT (op1
, i
);
13868 tree tem
= fold_relational_const (code
, elem_type
,
13871 if (tem
== NULL_TREE
)
13874 elts
[i
] = build_int_cst (elem_type
, integer_zerop (tem
) ? 0 : -1);
13877 return build_vector (type
, elts
);
13880 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13882 To compute GT, swap the arguments and do LT.
13883 To compute GE, do LT and invert the result.
13884 To compute LE, swap the arguments, do LT and invert the result.
13885 To compute NE, do EQ and invert the result.
13887 Therefore, the code below must handle only EQ and LT. */
13889 if (code
== LE_EXPR
|| code
== GT_EXPR
)
13891 std::swap (op0
, op1
);
13892 code
= swap_tree_comparison (code
);
13895 /* Note that it is safe to invert for real values here because we
13896 have already handled the one case that it matters. */
13899 if (code
== NE_EXPR
|| code
== GE_EXPR
)
13902 code
= invert_tree_comparison (code
, false);
13905 /* Compute a result for LT or EQ if args permit;
13906 Otherwise return T. */
13907 if (TREE_CODE (op0
) == INTEGER_CST
&& TREE_CODE (op1
) == INTEGER_CST
)
13909 if (code
== EQ_EXPR
)
13910 result
= tree_int_cst_equal (op0
, op1
);
13912 result
= tree_int_cst_lt (op0
, op1
);
13919 return constant_boolean_node (result
, type
);
13922 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13923 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13927 fold_build_cleanup_point_expr (tree type
, tree expr
)
13929 /* If the expression does not have side effects then we don't have to wrap
13930 it with a cleanup point expression. */
13931 if (!TREE_SIDE_EFFECTS (expr
))
13934 /* If the expression is a return, check to see if the expression inside the
13935 return has no side effects or the right hand side of the modify expression
13936 inside the return. If either don't have side effects set we don't need to
13937 wrap the expression in a cleanup point expression. Note we don't check the
13938 left hand side of the modify because it should always be a return decl. */
13939 if (TREE_CODE (expr
) == RETURN_EXPR
)
13941 tree op
= TREE_OPERAND (expr
, 0);
13942 if (!op
|| !TREE_SIDE_EFFECTS (op
))
13944 op
= TREE_OPERAND (op
, 1);
13945 if (!TREE_SIDE_EFFECTS (op
))
13949 return build1 (CLEANUP_POINT_EXPR
, type
, expr
);
13952 /* Given a pointer value OP0 and a type TYPE, return a simplified version
13953 of an indirection through OP0, or NULL_TREE if no simplification is
13957 fold_indirect_ref_1 (location_t loc
, tree type
, tree op0
)
13963 subtype
= TREE_TYPE (sub
);
13964 if (!POINTER_TYPE_P (subtype
))
13967 if (TREE_CODE (sub
) == ADDR_EXPR
)
13969 tree op
= TREE_OPERAND (sub
, 0);
13970 tree optype
= TREE_TYPE (op
);
13971 /* *&CONST_DECL -> to the value of the const decl. */
13972 if (TREE_CODE (op
) == CONST_DECL
)
13973 return DECL_INITIAL (op
);
13974 /* *&p => p; make sure to handle *&"str"[cst] here. */
13975 if (type
== optype
)
13977 tree fop
= fold_read_from_constant_string (op
);
13983 /* *(foo *)&fooarray => fooarray[0] */
13984 else if (TREE_CODE (optype
) == ARRAY_TYPE
13985 && type
== TREE_TYPE (optype
)
13986 && (!in_gimple_form
13987 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
13989 tree type_domain
= TYPE_DOMAIN (optype
);
13990 tree min_val
= size_zero_node
;
13991 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
13992 min_val
= TYPE_MIN_VALUE (type_domain
);
13994 && TREE_CODE (min_val
) != INTEGER_CST
)
13996 return build4_loc (loc
, ARRAY_REF
, type
, op
, min_val
,
13997 NULL_TREE
, NULL_TREE
);
13999 /* *(foo *)&complexfoo => __real__ complexfoo */
14000 else if (TREE_CODE (optype
) == COMPLEX_TYPE
14001 && type
== TREE_TYPE (optype
))
14002 return fold_build1_loc (loc
, REALPART_EXPR
, type
, op
);
14003 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14004 else if (TREE_CODE (optype
) == VECTOR_TYPE
14005 && type
== TREE_TYPE (optype
))
14007 tree part_width
= TYPE_SIZE (type
);
14008 tree index
= bitsize_int (0);
14009 return fold_build3_loc (loc
, BIT_FIELD_REF
, type
, op
, part_width
, index
);
14013 if (TREE_CODE (sub
) == POINTER_PLUS_EXPR
14014 && TREE_CODE (TREE_OPERAND (sub
, 1)) == INTEGER_CST
)
14016 tree op00
= TREE_OPERAND (sub
, 0);
14017 tree op01
= TREE_OPERAND (sub
, 1);
14020 if (TREE_CODE (op00
) == ADDR_EXPR
)
14023 op00
= TREE_OPERAND (op00
, 0);
14024 op00type
= TREE_TYPE (op00
);
14026 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14027 if (TREE_CODE (op00type
) == VECTOR_TYPE
14028 && type
== TREE_TYPE (op00type
))
14030 HOST_WIDE_INT offset
= tree_to_shwi (op01
);
14031 tree part_width
= TYPE_SIZE (type
);
14032 unsigned HOST_WIDE_INT part_widthi
= tree_to_shwi (part_width
)/BITS_PER_UNIT
;
14033 unsigned HOST_WIDE_INT indexi
= offset
* BITS_PER_UNIT
;
14034 tree index
= bitsize_int (indexi
);
14036 if (offset
/ part_widthi
< TYPE_VECTOR_SUBPARTS (op00type
))
14037 return fold_build3_loc (loc
,
14038 BIT_FIELD_REF
, type
, op00
,
14039 part_width
, index
);
14042 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14043 else if (TREE_CODE (op00type
) == COMPLEX_TYPE
14044 && type
== TREE_TYPE (op00type
))
14046 tree size
= TYPE_SIZE_UNIT (type
);
14047 if (tree_int_cst_equal (size
, op01
))
14048 return fold_build1_loc (loc
, IMAGPART_EXPR
, type
, op00
);
14050 /* ((foo *)&fooarray)[1] => fooarray[1] */
14051 else if (TREE_CODE (op00type
) == ARRAY_TYPE
14052 && type
== TREE_TYPE (op00type
))
14054 tree type_domain
= TYPE_DOMAIN (op00type
);
14055 tree min_val
= size_zero_node
;
14056 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14057 min_val
= TYPE_MIN_VALUE (type_domain
);
14058 op01
= size_binop_loc (loc
, EXACT_DIV_EXPR
, op01
,
14059 TYPE_SIZE_UNIT (type
));
14060 op01
= size_binop_loc (loc
, PLUS_EXPR
, op01
, min_val
);
14061 return build4_loc (loc
, ARRAY_REF
, type
, op00
, op01
,
14062 NULL_TREE
, NULL_TREE
);
14067 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14068 if (TREE_CODE (TREE_TYPE (subtype
)) == ARRAY_TYPE
14069 && type
== TREE_TYPE (TREE_TYPE (subtype
))
14070 && (!in_gimple_form
14071 || TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
))
14074 tree min_val
= size_zero_node
;
14075 sub
= build_fold_indirect_ref_loc (loc
, sub
);
14076 type_domain
= TYPE_DOMAIN (TREE_TYPE (sub
));
14077 if (type_domain
&& TYPE_MIN_VALUE (type_domain
))
14078 min_val
= TYPE_MIN_VALUE (type_domain
);
14080 && TREE_CODE (min_val
) != INTEGER_CST
)
14082 return build4_loc (loc
, ARRAY_REF
, type
, sub
, min_val
, NULL_TREE
,
14089 /* Builds an expression for an indirection through T, simplifying some
14093 build_fold_indirect_ref_loc (location_t loc
, tree t
)
14095 tree type
= TREE_TYPE (TREE_TYPE (t
));
14096 tree sub
= fold_indirect_ref_1 (loc
, type
, t
);
14101 return build1_loc (loc
, INDIRECT_REF
, type
, t
);
14104 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14107 fold_indirect_ref_loc (location_t loc
, tree t
)
14109 tree sub
= fold_indirect_ref_1 (loc
, TREE_TYPE (t
), TREE_OPERAND (t
, 0));
14117 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14118 whose result is ignored. The type of the returned tree need not be
14119 the same as the original expression. */
14122 fold_ignored_result (tree t
)
14124 if (!TREE_SIDE_EFFECTS (t
))
14125 return integer_zero_node
;
14128 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
14131 t
= TREE_OPERAND (t
, 0);
14135 case tcc_comparison
:
14136 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
14137 t
= TREE_OPERAND (t
, 0);
14138 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 0)))
14139 t
= TREE_OPERAND (t
, 1);
14144 case tcc_expression
:
14145 switch (TREE_CODE (t
))
14147 case COMPOUND_EXPR
:
14148 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1)))
14150 t
= TREE_OPERAND (t
, 0);
14154 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 1))
14155 || TREE_SIDE_EFFECTS (TREE_OPERAND (t
, 2)))
14157 t
= TREE_OPERAND (t
, 0);
14170 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14173 round_up_loc (location_t loc
, tree value
, unsigned int divisor
)
14175 tree div
= NULL_TREE
;
14180 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14181 have to do anything. Only do this when we are not given a const,
14182 because in that case, this check is more expensive than just
14184 if (TREE_CODE (value
) != INTEGER_CST
)
14186 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14188 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
14192 /* If divisor is a power of two, simplify this to bit manipulation. */
14193 if (divisor
== (divisor
& -divisor
))
14195 if (TREE_CODE (value
) == INTEGER_CST
)
14197 wide_int val
= value
;
14200 if ((val
& (divisor
- 1)) == 0)
14203 overflow_p
= TREE_OVERFLOW (value
);
14204 val
+= divisor
- 1;
14205 val
&= - (int) divisor
;
14209 return force_fit_type (TREE_TYPE (value
), val
, -1, overflow_p
);
14215 t
= build_int_cst (TREE_TYPE (value
), divisor
- 1);
14216 value
= size_binop_loc (loc
, PLUS_EXPR
, value
, t
);
14217 t
= build_int_cst (TREE_TYPE (value
), - (int) divisor
);
14218 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
14224 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14225 value
= size_binop_loc (loc
, CEIL_DIV_EXPR
, value
, div
);
14226 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
14232 /* Likewise, but round down. */
14235 round_down_loc (location_t loc
, tree value
, int divisor
)
14237 tree div
= NULL_TREE
;
14239 gcc_assert (divisor
> 0);
14243 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14244 have to do anything. Only do this when we are not given a const,
14245 because in that case, this check is more expensive than just
14247 if (TREE_CODE (value
) != INTEGER_CST
)
14249 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14251 if (multiple_of_p (TREE_TYPE (value
), value
, div
))
14255 /* If divisor is a power of two, simplify this to bit manipulation. */
14256 if (divisor
== (divisor
& -divisor
))
14260 t
= build_int_cst (TREE_TYPE (value
), -divisor
);
14261 value
= size_binop_loc (loc
, BIT_AND_EXPR
, value
, t
);
14266 div
= build_int_cst (TREE_TYPE (value
), divisor
);
14267 value
= size_binop_loc (loc
, FLOOR_DIV_EXPR
, value
, div
);
14268 value
= size_binop_loc (loc
, MULT_EXPR
, value
, div
);
14274 /* Returns the pointer to the base of the object addressed by EXP and
14275 extracts the information about the offset of the access, storing it
14276 to PBITPOS and POFFSET. */
14279 split_address_to_core_and_offset (tree exp
,
14280 HOST_WIDE_INT
*pbitpos
, tree
*poffset
)
14284 int unsignedp
, volatilep
;
14285 HOST_WIDE_INT bitsize
;
14286 location_t loc
= EXPR_LOCATION (exp
);
14288 if (TREE_CODE (exp
) == ADDR_EXPR
)
14290 core
= get_inner_reference (TREE_OPERAND (exp
, 0), &bitsize
, pbitpos
,
14291 poffset
, &mode
, &unsignedp
, &volatilep
,
14293 core
= build_fold_addr_expr_loc (loc
, core
);
14299 *poffset
= NULL_TREE
;
14305 /* Returns true if addresses of E1 and E2 differ by a constant, false
14306 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14309 ptr_difference_const (tree e1
, tree e2
, HOST_WIDE_INT
*diff
)
14312 HOST_WIDE_INT bitpos1
, bitpos2
;
14313 tree toffset1
, toffset2
, tdiff
, type
;
14315 core1
= split_address_to_core_and_offset (e1
, &bitpos1
, &toffset1
);
14316 core2
= split_address_to_core_and_offset (e2
, &bitpos2
, &toffset2
);
14318 if (bitpos1
% BITS_PER_UNIT
!= 0
14319 || bitpos2
% BITS_PER_UNIT
!= 0
14320 || !operand_equal_p (core1
, core2
, 0))
14323 if (toffset1
&& toffset2
)
14325 type
= TREE_TYPE (toffset1
);
14326 if (type
!= TREE_TYPE (toffset2
))
14327 toffset2
= fold_convert (type
, toffset2
);
14329 tdiff
= fold_build2 (MINUS_EXPR
, type
, toffset1
, toffset2
);
14330 if (!cst_and_fits_in_hwi (tdiff
))
14333 *diff
= int_cst_value (tdiff
);
14335 else if (toffset1
|| toffset2
)
14337 /* If only one of the offsets is non-constant, the difference cannot
14344 *diff
+= (bitpos1
- bitpos2
) / BITS_PER_UNIT
;
14348 /* Return OFF converted to a pointer offset type suitable as offset for
14349 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14351 convert_to_ptrofftype_loc (location_t loc
, tree off
)
14353 return fold_convert_loc (loc
, sizetype
, off
);
14356 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14358 fold_build_pointer_plus_loc (location_t loc
, tree ptr
, tree off
)
14360 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
14361 ptr
, convert_to_ptrofftype_loc (loc
, off
));
14364 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14366 fold_build_pointer_plus_hwi_loc (location_t loc
, tree ptr
, HOST_WIDE_INT off
)
14368 return fold_build2_loc (loc
, POINTER_PLUS_EXPR
, TREE_TYPE (ptr
),
14369 ptr
, size_int (off
));