gcc/
[official-gcc.git] / gcc / fold-const.c
blob6fa784a17cbf704975b62c4f8162113386cbf2a9
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
9 version.
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
14 for more details.
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. */
43 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "tm.h"
47 #include "flags.h"
48 #include "alias.h"
49 #include "symtab.h"
50 #include "tree.h"
51 #include "fold-const.h"
52 #include "stor-layout.h"
53 #include "calls.h"
54 #include "tree-iterator.h"
55 #include "realmpfr.h"
56 #include "rtl.h"
57 #include "hard-reg-set.h"
58 #include "function.h"
59 #include "insn-config.h"
60 #include "expmed.h"
61 #include "dojump.h"
62 #include "explow.h"
63 #include "emit-rtl.h"
64 #include "varasm.h"
65 #include "stmt.h"
66 #include "expr.h"
67 #include "tm_p.h"
68 #include "target.h"
69 #include "diagnostic-core.h"
70 #include "intl.h"
71 #include "langhooks.h"
72 #include "md5.h"
73 #include "predict.h"
74 #include "basic-block.h"
75 #include "tree-ssa-alias.h"
76 #include "internal-fn.h"
77 #include "tree-eh.h"
78 #include "gimple-expr.h"
79 #include "gimple.h"
80 #include "gimplify.h"
81 #include "tree-dfa.h"
82 #include "builtins.h"
83 #include "plugin-api.h"
84 #include "ipa-ref.h"
85 #include "cgraph.h"
86 #include "generic-match.h"
87 #include "optabs.h"
89 /* Nonzero if we are folding constants inside an initializer; zero
90 otherwise. */
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 {
97 COMPCODE_FALSE = 0,
98 COMPCODE_LT = 1,
99 COMPCODE_EQ = 2,
100 COMPCODE_LE = 3,
101 COMPCODE_GT = 4,
102 COMPCODE_LTGT = 5,
103 COMPCODE_GE = 6,
104 COMPCODE_ORD = 7,
105 COMPCODE_UNORD = 8,
106 COMPCODE_UNLT = 9,
107 COMPCODE_UNEQ = 10,
108 COMPCODE_UNLE = 11,
109 COMPCODE_UNGT = 12,
110 COMPCODE_NE = 13,
111 COMPCODE_UNGE = 14,
112 COMPCODE_TRUE = 15
115 static bool negate_mathfn_p (enum built_in_function);
116 static bool negate_expr_p (tree);
117 static tree negate_expr (tree);
118 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
119 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
120 static enum comparison_code comparison_to_compcode (enum tree_code);
121 static enum tree_code compcode_to_comparison (enum comparison_code);
122 static int operand_equal_for_comparison_p (tree, tree, tree);
123 static int twoval_comparison_p (tree, tree *, tree *, int *);
124 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
125 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
126 static tree make_bit_field_ref (location_t, tree, tree,
127 HOST_WIDE_INT, HOST_WIDE_INT, int);
128 static tree optimize_bit_field_compare (location_t, enum tree_code,
129 tree, tree, tree);
130 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
131 HOST_WIDE_INT *,
132 machine_mode *, int *, int *,
133 tree *, tree *);
134 static int simple_operand_p (const_tree);
135 static bool simple_operand_p_2 (tree);
136 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
137 static tree range_predecessor (tree);
138 static tree range_successor (tree);
139 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
140 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
141 static tree unextend (tree, int, int, tree);
142 static tree optimize_minmax_comparison (location_t, enum tree_code,
143 tree, tree, tree);
144 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
145 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
146 static tree fold_binary_op_with_conditional_arg (location_t,
147 enum tree_code, tree,
148 tree, tree,
149 tree, tree, int);
150 static tree fold_mathfn_compare (location_t,
151 enum built_in_function, enum tree_code,
152 tree, tree, tree);
153 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
154 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
155 static bool reorder_operands_p (const_tree, const_tree);
156 static tree fold_negate_const (tree, tree);
157 static tree fold_not_const (const_tree, tree);
158 static tree fold_relational_const (enum tree_code, tree, tree, tree);
159 static tree fold_convert_const (enum tree_code, tree, tree);
160 static tree fold_view_convert_expr (tree, tree);
161 static bool vec_cst_ctor_to_array (tree, tree *);
164 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
165 Otherwise, return LOC. */
167 static location_t
168 expr_location_or (tree t, location_t loc)
170 location_t tloc = EXPR_LOCATION (t);
171 return tloc == UNKNOWN_LOCATION ? loc : tloc;
174 /* Similar to protected_set_expr_location, but never modify x in place,
175 if location can and needs to be set, unshare it. */
177 static inline tree
178 protected_set_expr_location_unshare (tree x, location_t loc)
180 if (CAN_HAVE_LOCATION_P (x)
181 && EXPR_LOCATION (x) != loc
182 && !(TREE_CODE (x) == SAVE_EXPR
183 || TREE_CODE (x) == TARGET_EXPR
184 || TREE_CODE (x) == BIND_EXPR))
186 x = copy_node (x);
187 SET_EXPR_LOCATION (x, loc);
189 return x;
192 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
193 division and returns the quotient. Otherwise returns
194 NULL_TREE. */
196 tree
197 div_if_zero_remainder (const_tree arg1, const_tree arg2)
199 widest_int quo;
201 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
202 SIGNED, &quo))
203 return wide_int_to_tree (TREE_TYPE (arg1), quo);
205 return NULL_TREE;
208 /* This is nonzero if we should defer warnings about undefined
209 overflow. This facility exists because these warnings are a
210 special case. The code to estimate loop iterations does not want
211 to issue any warnings, since it works with expressions which do not
212 occur in user code. Various bits of cleanup code call fold(), but
213 only use the result if it has certain characteristics (e.g., is a
214 constant); that code only wants to issue a warning if the result is
215 used. */
217 static int fold_deferring_overflow_warnings;
219 /* If a warning about undefined overflow is deferred, this is the
220 warning. Note that this may cause us to turn two warnings into
221 one, but that is fine since it is sufficient to only give one
222 warning per expression. */
224 static const char* fold_deferred_overflow_warning;
226 /* If a warning about undefined overflow is deferred, this is the
227 level at which the warning should be emitted. */
229 static enum warn_strict_overflow_code fold_deferred_overflow_code;
231 /* Start deferring overflow warnings. We could use a stack here to
232 permit nested calls, but at present it is not necessary. */
234 void
235 fold_defer_overflow_warnings (void)
237 ++fold_deferring_overflow_warnings;
240 /* Stop deferring overflow warnings. If there is a pending warning,
241 and ISSUE is true, then issue the warning if appropriate. STMT is
242 the statement with which the warning should be associated (used for
243 location information); STMT may be NULL. CODE is the level of the
244 warning--a warn_strict_overflow_code value. This function will use
245 the smaller of CODE and the deferred code when deciding whether to
246 issue the warning. CODE may be zero to mean to always use the
247 deferred code. */
249 void
250 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
252 const char *warnmsg;
253 location_t locus;
255 gcc_assert (fold_deferring_overflow_warnings > 0);
256 --fold_deferring_overflow_warnings;
257 if (fold_deferring_overflow_warnings > 0)
259 if (fold_deferred_overflow_warning != NULL
260 && code != 0
261 && code < (int) fold_deferred_overflow_code)
262 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
263 return;
266 warnmsg = fold_deferred_overflow_warning;
267 fold_deferred_overflow_warning = NULL;
269 if (!issue || warnmsg == NULL)
270 return;
272 if (gimple_no_warning_p (stmt))
273 return;
275 /* Use the smallest code level when deciding to issue the
276 warning. */
277 if (code == 0 || code > (int) fold_deferred_overflow_code)
278 code = fold_deferred_overflow_code;
280 if (!issue_strict_overflow_warning (code))
281 return;
283 if (stmt == NULL)
284 locus = input_location;
285 else
286 locus = gimple_location (stmt);
287 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
290 /* Stop deferring overflow warnings, ignoring any deferred
291 warnings. */
293 void
294 fold_undefer_and_ignore_overflow_warnings (void)
296 fold_undefer_overflow_warnings (false, NULL, 0);
299 /* Whether we are deferring overflow warnings. */
301 bool
302 fold_deferring_overflow_warnings_p (void)
304 return fold_deferring_overflow_warnings > 0;
307 /* This is called when we fold something based on the fact that signed
308 overflow is undefined. */
310 static void
311 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
313 if (fold_deferring_overflow_warnings > 0)
315 if (fold_deferred_overflow_warning == NULL
316 || wc < fold_deferred_overflow_code)
318 fold_deferred_overflow_warning = gmsgid;
319 fold_deferred_overflow_code = wc;
322 else if (issue_strict_overflow_warning (wc))
323 warning (OPT_Wstrict_overflow, gmsgid);
326 /* Return true if the built-in mathematical function specified by CODE
327 is odd, i.e. -f(x) == f(-x). */
329 static bool
330 negate_mathfn_p (enum built_in_function code)
332 switch (code)
334 CASE_FLT_FN (BUILT_IN_ASIN):
335 CASE_FLT_FN (BUILT_IN_ASINH):
336 CASE_FLT_FN (BUILT_IN_ATAN):
337 CASE_FLT_FN (BUILT_IN_ATANH):
338 CASE_FLT_FN (BUILT_IN_CASIN):
339 CASE_FLT_FN (BUILT_IN_CASINH):
340 CASE_FLT_FN (BUILT_IN_CATAN):
341 CASE_FLT_FN (BUILT_IN_CATANH):
342 CASE_FLT_FN (BUILT_IN_CBRT):
343 CASE_FLT_FN (BUILT_IN_CPROJ):
344 CASE_FLT_FN (BUILT_IN_CSIN):
345 CASE_FLT_FN (BUILT_IN_CSINH):
346 CASE_FLT_FN (BUILT_IN_CTAN):
347 CASE_FLT_FN (BUILT_IN_CTANH):
348 CASE_FLT_FN (BUILT_IN_ERF):
349 CASE_FLT_FN (BUILT_IN_LLROUND):
350 CASE_FLT_FN (BUILT_IN_LROUND):
351 CASE_FLT_FN (BUILT_IN_ROUND):
352 CASE_FLT_FN (BUILT_IN_SIN):
353 CASE_FLT_FN (BUILT_IN_SINH):
354 CASE_FLT_FN (BUILT_IN_TAN):
355 CASE_FLT_FN (BUILT_IN_TANH):
356 CASE_FLT_FN (BUILT_IN_TRUNC):
357 return true;
359 CASE_FLT_FN (BUILT_IN_LLRINT):
360 CASE_FLT_FN (BUILT_IN_LRINT):
361 CASE_FLT_FN (BUILT_IN_NEARBYINT):
362 CASE_FLT_FN (BUILT_IN_RINT):
363 return !flag_rounding_math;
365 default:
366 break;
368 return false;
371 /* Check whether we may negate an integer constant T without causing
372 overflow. */
374 bool
375 may_negate_without_overflow_p (const_tree t)
377 tree type;
379 gcc_assert (TREE_CODE (t) == INTEGER_CST);
381 type = TREE_TYPE (t);
382 if (TYPE_UNSIGNED (type))
383 return false;
385 return !wi::only_sign_bit_p (t);
388 /* Determine whether an expression T can be cheaply negated using
389 the function negate_expr without introducing undefined overflow. */
391 static bool
392 negate_expr_p (tree t)
394 tree type;
396 if (t == 0)
397 return false;
399 type = TREE_TYPE (t);
401 STRIP_SIGN_NOPS (t);
402 switch (TREE_CODE (t))
404 case INTEGER_CST:
405 if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
406 return true;
408 /* Check that -CST will not overflow type. */
409 return may_negate_without_overflow_p (t);
410 case BIT_NOT_EXPR:
411 return (INTEGRAL_TYPE_P (type)
412 && TYPE_OVERFLOW_WRAPS (type));
414 case FIXED_CST:
415 return true;
417 case NEGATE_EXPR:
418 return !TYPE_OVERFLOW_SANITIZED (type);
420 case REAL_CST:
421 /* We want to canonicalize to positive real constants. Pretend
422 that only negative ones can be easily negated. */
423 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
425 case COMPLEX_CST:
426 return negate_expr_p (TREE_REALPART (t))
427 && negate_expr_p (TREE_IMAGPART (t));
429 case VECTOR_CST:
431 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
432 return true;
434 int count = TYPE_VECTOR_SUBPARTS (type), i;
436 for (i = 0; i < count; i++)
437 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
438 return false;
440 return true;
443 case COMPLEX_EXPR:
444 return negate_expr_p (TREE_OPERAND (t, 0))
445 && negate_expr_p (TREE_OPERAND (t, 1));
447 case CONJ_EXPR:
448 return negate_expr_p (TREE_OPERAND (t, 0));
450 case PLUS_EXPR:
451 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
452 || HONOR_SIGNED_ZEROS (element_mode (type)))
453 return false;
454 /* -(A + B) -> (-B) - A. */
455 if (negate_expr_p (TREE_OPERAND (t, 1))
456 && reorder_operands_p (TREE_OPERAND (t, 0),
457 TREE_OPERAND (t, 1)))
458 return true;
459 /* -(A + B) -> (-A) - B. */
460 return negate_expr_p (TREE_OPERAND (t, 0));
462 case MINUS_EXPR:
463 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
464 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
465 && !HONOR_SIGNED_ZEROS (element_mode (type))
466 && reorder_operands_p (TREE_OPERAND (t, 0),
467 TREE_OPERAND (t, 1));
469 case MULT_EXPR:
470 if (TYPE_UNSIGNED (TREE_TYPE (t)))
471 break;
473 /* Fall through. */
475 case RDIV_EXPR:
476 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
477 return negate_expr_p (TREE_OPERAND (t, 1))
478 || negate_expr_p (TREE_OPERAND (t, 0));
479 break;
481 case TRUNC_DIV_EXPR:
482 case ROUND_DIV_EXPR:
483 case EXACT_DIV_EXPR:
484 /* In general we can't negate A / B, because if A is INT_MIN and
485 B is 1, we may turn this into INT_MIN / -1 which is undefined
486 and actually traps on some architectures. But if overflow is
487 undefined, we can negate, because - (INT_MIN / 1) is an
488 overflow. */
489 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
491 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
492 break;
493 /* If overflow is undefined then we have to be careful because
494 we ask whether it's ok to associate the negate with the
495 division which is not ok for example for
496 -((a - b) / c) where (-(a - b)) / c may invoke undefined
497 overflow because of negating INT_MIN. So do not use
498 negate_expr_p here but open-code the two important cases. */
499 if (TREE_CODE (TREE_OPERAND (t, 0)) == NEGATE_EXPR
500 || (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
501 && may_negate_without_overflow_p (TREE_OPERAND (t, 0))))
502 return true;
504 else if (negate_expr_p (TREE_OPERAND (t, 0)))
505 return true;
506 return negate_expr_p (TREE_OPERAND (t, 1));
508 case NOP_EXPR:
509 /* Negate -((double)float) as (double)(-float). */
510 if (TREE_CODE (type) == REAL_TYPE)
512 tree tem = strip_float_extensions (t);
513 if (tem != t)
514 return negate_expr_p (tem);
516 break;
518 case CALL_EXPR:
519 /* Negate -f(x) as f(-x). */
520 if (negate_mathfn_p (builtin_mathfn_code (t)))
521 return negate_expr_p (CALL_EXPR_ARG (t, 0));
522 break;
524 case RSHIFT_EXPR:
525 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
526 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
528 tree op1 = TREE_OPERAND (t, 1);
529 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
530 return true;
532 break;
534 default:
535 break;
537 return false;
540 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
541 simplification is possible.
542 If negate_expr_p would return true for T, NULL_TREE will never be
543 returned. */
545 static tree
546 fold_negate_expr (location_t loc, tree t)
548 tree type = TREE_TYPE (t);
549 tree tem;
551 switch (TREE_CODE (t))
553 /* Convert - (~A) to A + 1. */
554 case BIT_NOT_EXPR:
555 if (INTEGRAL_TYPE_P (type))
556 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
557 build_one_cst (type));
558 break;
560 case INTEGER_CST:
561 tem = fold_negate_const (t, type);
562 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
563 || (ANY_INTEGRAL_TYPE_P (type)
564 && !TYPE_OVERFLOW_TRAPS (type)
565 && TYPE_OVERFLOW_WRAPS (type))
566 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
567 return tem;
568 break;
570 case REAL_CST:
571 tem = fold_negate_const (t, type);
572 return tem;
574 case FIXED_CST:
575 tem = fold_negate_const (t, type);
576 return tem;
578 case COMPLEX_CST:
580 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
581 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
582 if (rpart && ipart)
583 return build_complex (type, rpart, ipart);
585 break;
587 case VECTOR_CST:
589 int count = TYPE_VECTOR_SUBPARTS (type), i;
590 tree *elts = XALLOCAVEC (tree, count);
592 for (i = 0; i < count; i++)
594 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
595 if (elts[i] == NULL_TREE)
596 return NULL_TREE;
599 return build_vector (type, elts);
602 case COMPLEX_EXPR:
603 if (negate_expr_p (t))
604 return fold_build2_loc (loc, COMPLEX_EXPR, type,
605 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
606 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
607 break;
609 case CONJ_EXPR:
610 if (negate_expr_p (t))
611 return fold_build1_loc (loc, CONJ_EXPR, type,
612 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
613 break;
615 case NEGATE_EXPR:
616 if (!TYPE_OVERFLOW_SANITIZED (type))
617 return TREE_OPERAND (t, 0);
618 break;
620 case PLUS_EXPR:
621 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
622 && !HONOR_SIGNED_ZEROS (element_mode (type)))
624 /* -(A + B) -> (-B) - A. */
625 if (negate_expr_p (TREE_OPERAND (t, 1))
626 && reorder_operands_p (TREE_OPERAND (t, 0),
627 TREE_OPERAND (t, 1)))
629 tem = negate_expr (TREE_OPERAND (t, 1));
630 return fold_build2_loc (loc, MINUS_EXPR, type,
631 tem, TREE_OPERAND (t, 0));
634 /* -(A + B) -> (-A) - B. */
635 if (negate_expr_p (TREE_OPERAND (t, 0)))
637 tem = negate_expr (TREE_OPERAND (t, 0));
638 return fold_build2_loc (loc, MINUS_EXPR, type,
639 tem, TREE_OPERAND (t, 1));
642 break;
644 case MINUS_EXPR:
645 /* - (A - B) -> B - A */
646 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
647 && !HONOR_SIGNED_ZEROS (element_mode (type))
648 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
649 return fold_build2_loc (loc, MINUS_EXPR, type,
650 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
651 break;
653 case MULT_EXPR:
654 if (TYPE_UNSIGNED (type))
655 break;
657 /* Fall through. */
659 case RDIV_EXPR:
660 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
662 tem = TREE_OPERAND (t, 1);
663 if (negate_expr_p (tem))
664 return fold_build2_loc (loc, TREE_CODE (t), type,
665 TREE_OPERAND (t, 0), negate_expr (tem));
666 tem = TREE_OPERAND (t, 0);
667 if (negate_expr_p (tem))
668 return fold_build2_loc (loc, TREE_CODE (t), type,
669 negate_expr (tem), TREE_OPERAND (t, 1));
671 break;
673 case TRUNC_DIV_EXPR:
674 case ROUND_DIV_EXPR:
675 case EXACT_DIV_EXPR:
676 /* In general we can't negate A / B, because if A is INT_MIN and
677 B is 1, we may turn this into INT_MIN / -1 which is undefined
678 and actually traps on some architectures. But if overflow is
679 undefined, we can negate, because - (INT_MIN / 1) is an
680 overflow. */
681 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
683 const char * const warnmsg = G_("assuming signed overflow does not "
684 "occur when negating a division");
685 tem = TREE_OPERAND (t, 1);
686 if (negate_expr_p (tem))
688 if (INTEGRAL_TYPE_P (type)
689 && (TREE_CODE (tem) != INTEGER_CST
690 || integer_onep (tem)))
691 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
692 return fold_build2_loc (loc, TREE_CODE (t), type,
693 TREE_OPERAND (t, 0), negate_expr (tem));
695 /* If overflow is undefined then we have to be careful because
696 we ask whether it's ok to associate the negate with the
697 division which is not ok for example for
698 -((a - b) / c) where (-(a - b)) / c may invoke undefined
699 overflow because of negating INT_MIN. So do not use
700 negate_expr_p here but open-code the two important cases. */
701 tem = TREE_OPERAND (t, 0);
702 if ((INTEGRAL_TYPE_P (type)
703 && (TREE_CODE (tem) == NEGATE_EXPR
704 || (TREE_CODE (tem) == INTEGER_CST
705 && may_negate_without_overflow_p (tem))))
706 || !INTEGRAL_TYPE_P (type))
707 return fold_build2_loc (loc, TREE_CODE (t), type,
708 negate_expr (tem), TREE_OPERAND (t, 1));
710 break;
712 case NOP_EXPR:
713 /* Convert -((double)float) into (double)(-float). */
714 if (TREE_CODE (type) == REAL_TYPE)
716 tem = strip_float_extensions (t);
717 if (tem != t && negate_expr_p (tem))
718 return fold_convert_loc (loc, type, negate_expr (tem));
720 break;
722 case CALL_EXPR:
723 /* Negate -f(x) as f(-x). */
724 if (negate_mathfn_p (builtin_mathfn_code (t))
725 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
727 tree fndecl, arg;
729 fndecl = get_callee_fndecl (t);
730 arg = negate_expr (CALL_EXPR_ARG (t, 0));
731 return build_call_expr_loc (loc, fndecl, 1, arg);
733 break;
735 case RSHIFT_EXPR:
736 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
737 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
739 tree op1 = TREE_OPERAND (t, 1);
740 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
742 tree ntype = TYPE_UNSIGNED (type)
743 ? signed_type_for (type)
744 : unsigned_type_for (type);
745 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
746 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
747 return fold_convert_loc (loc, type, temp);
750 break;
752 default:
753 break;
756 return NULL_TREE;
759 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
760 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
761 return NULL_TREE. */
763 static tree
764 negate_expr (tree t)
766 tree type, tem;
767 location_t loc;
769 if (t == NULL_TREE)
770 return NULL_TREE;
772 loc = EXPR_LOCATION (t);
773 type = TREE_TYPE (t);
774 STRIP_SIGN_NOPS (t);
776 tem = fold_negate_expr (loc, t);
777 if (!tem)
778 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
779 return fold_convert_loc (loc, type, tem);
782 /* Split a tree IN into a constant, literal and variable parts that could be
783 combined with CODE to make IN. "constant" means an expression with
784 TREE_CONSTANT but that isn't an actual constant. CODE must be a
785 commutative arithmetic operation. Store the constant part into *CONP,
786 the literal in *LITP and return the variable part. If a part isn't
787 present, set it to null. If the tree does not decompose in this way,
788 return the entire tree as the variable part and the other parts as null.
790 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
791 case, we negate an operand that was subtracted. Except if it is a
792 literal for which we use *MINUS_LITP instead.
794 If NEGATE_P is true, we are negating all of IN, again except a literal
795 for which we use *MINUS_LITP instead.
797 If IN is itself a literal or constant, return it as appropriate.
799 Note that we do not guarantee that any of the three values will be the
800 same type as IN, but they will have the same signedness and mode. */
802 static tree
803 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
804 tree *minus_litp, int negate_p)
806 tree var = 0;
808 *conp = 0;
809 *litp = 0;
810 *minus_litp = 0;
812 /* Strip any conversions that don't change the machine mode or signedness. */
813 STRIP_SIGN_NOPS (in);
815 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
816 || TREE_CODE (in) == FIXED_CST)
817 *litp = in;
818 else if (TREE_CODE (in) == code
819 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
820 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
821 /* We can associate addition and subtraction together (even
822 though the C standard doesn't say so) for integers because
823 the value is not affected. For reals, the value might be
824 affected, so we can't. */
825 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
826 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
828 tree op0 = TREE_OPERAND (in, 0);
829 tree op1 = TREE_OPERAND (in, 1);
830 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
831 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
833 /* First see if either of the operands is a literal, then a constant. */
834 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
835 || TREE_CODE (op0) == FIXED_CST)
836 *litp = op0, op0 = 0;
837 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
838 || TREE_CODE (op1) == FIXED_CST)
839 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
841 if (op0 != 0 && TREE_CONSTANT (op0))
842 *conp = op0, op0 = 0;
843 else if (op1 != 0 && TREE_CONSTANT (op1))
844 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
846 /* If we haven't dealt with either operand, this is not a case we can
847 decompose. Otherwise, VAR is either of the ones remaining, if any. */
848 if (op0 != 0 && op1 != 0)
849 var = in;
850 else if (op0 != 0)
851 var = op0;
852 else
853 var = op1, neg_var_p = neg1_p;
855 /* Now do any needed negations. */
856 if (neg_litp_p)
857 *minus_litp = *litp, *litp = 0;
858 if (neg_conp_p)
859 *conp = negate_expr (*conp);
860 if (neg_var_p)
861 var = negate_expr (var);
863 else if (TREE_CODE (in) == BIT_NOT_EXPR
864 && code == PLUS_EXPR)
866 /* -X - 1 is folded to ~X, undo that here. */
867 *minus_litp = build_one_cst (TREE_TYPE (in));
868 var = negate_expr (TREE_OPERAND (in, 0));
870 else if (TREE_CONSTANT (in))
871 *conp = in;
872 else
873 var = in;
875 if (negate_p)
877 if (*litp)
878 *minus_litp = *litp, *litp = 0;
879 else if (*minus_litp)
880 *litp = *minus_litp, *minus_litp = 0;
881 *conp = negate_expr (*conp);
882 var = negate_expr (var);
885 return var;
888 /* Re-associate trees split by the above function. T1 and T2 are
889 either expressions to associate or null. Return the new
890 expression, if any. LOC is the location of the new expression. If
891 we build an operation, do it in TYPE and with CODE. */
893 static tree
894 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
896 if (t1 == 0)
897 return t2;
898 else if (t2 == 0)
899 return t1;
901 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
902 try to fold this since we will have infinite recursion. But do
903 deal with any NEGATE_EXPRs. */
904 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
905 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
907 if (code == PLUS_EXPR)
909 if (TREE_CODE (t1) == NEGATE_EXPR)
910 return build2_loc (loc, MINUS_EXPR, type,
911 fold_convert_loc (loc, type, t2),
912 fold_convert_loc (loc, type,
913 TREE_OPERAND (t1, 0)));
914 else if (TREE_CODE (t2) == NEGATE_EXPR)
915 return build2_loc (loc, MINUS_EXPR, type,
916 fold_convert_loc (loc, type, t1),
917 fold_convert_loc (loc, type,
918 TREE_OPERAND (t2, 0)));
919 else if (integer_zerop (t2))
920 return fold_convert_loc (loc, type, t1);
922 else if (code == MINUS_EXPR)
924 if (integer_zerop (t2))
925 return fold_convert_loc (loc, type, t1);
928 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
929 fold_convert_loc (loc, type, t2));
932 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
933 fold_convert_loc (loc, type, t2));
936 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
937 for use in int_const_binop, size_binop and size_diffop. */
939 static bool
940 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
942 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
943 return false;
944 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
945 return false;
947 switch (code)
949 case LSHIFT_EXPR:
950 case RSHIFT_EXPR:
951 case LROTATE_EXPR:
952 case RROTATE_EXPR:
953 return true;
955 default:
956 break;
959 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
960 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
961 && TYPE_MODE (type1) == TYPE_MODE (type2);
965 /* Combine two integer constants ARG1 and ARG2 under operation CODE
966 to produce a new constant. Return NULL_TREE if we don't know how
967 to evaluate CODE at compile-time. */
969 static tree
970 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
971 int overflowable)
973 wide_int res;
974 tree t;
975 tree type = TREE_TYPE (arg1);
976 signop sign = TYPE_SIGN (type);
977 bool overflow = false;
979 wide_int arg2 = wide_int::from (parg2, TYPE_PRECISION (type),
980 TYPE_SIGN (TREE_TYPE (parg2)));
982 switch (code)
984 case BIT_IOR_EXPR:
985 res = wi::bit_or (arg1, arg2);
986 break;
988 case BIT_XOR_EXPR:
989 res = wi::bit_xor (arg1, arg2);
990 break;
992 case BIT_AND_EXPR:
993 res = wi::bit_and (arg1, arg2);
994 break;
996 case RSHIFT_EXPR:
997 case LSHIFT_EXPR:
998 if (wi::neg_p (arg2))
1000 arg2 = -arg2;
1001 if (code == RSHIFT_EXPR)
1002 code = LSHIFT_EXPR;
1003 else
1004 code = RSHIFT_EXPR;
1007 if (code == RSHIFT_EXPR)
1008 /* It's unclear from the C standard whether shifts can overflow.
1009 The following code ignores overflow; perhaps a C standard
1010 interpretation ruling is needed. */
1011 res = wi::rshift (arg1, arg2, sign);
1012 else
1013 res = wi::lshift (arg1, arg2);
1014 break;
1016 case RROTATE_EXPR:
1017 case LROTATE_EXPR:
1018 if (wi::neg_p (arg2))
1020 arg2 = -arg2;
1021 if (code == RROTATE_EXPR)
1022 code = LROTATE_EXPR;
1023 else
1024 code = RROTATE_EXPR;
1027 if (code == RROTATE_EXPR)
1028 res = wi::rrotate (arg1, arg2);
1029 else
1030 res = wi::lrotate (arg1, arg2);
1031 break;
1033 case PLUS_EXPR:
1034 res = wi::add (arg1, arg2, sign, &overflow);
1035 break;
1037 case MINUS_EXPR:
1038 res = wi::sub (arg1, arg2, sign, &overflow);
1039 break;
1041 case MULT_EXPR:
1042 res = wi::mul (arg1, arg2, sign, &overflow);
1043 break;
1045 case MULT_HIGHPART_EXPR:
1046 res = wi::mul_high (arg1, arg2, sign);
1047 break;
1049 case TRUNC_DIV_EXPR:
1050 case EXACT_DIV_EXPR:
1051 if (arg2 == 0)
1052 return NULL_TREE;
1053 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1054 break;
1056 case FLOOR_DIV_EXPR:
1057 if (arg2 == 0)
1058 return NULL_TREE;
1059 res = wi::div_floor (arg1, arg2, sign, &overflow);
1060 break;
1062 case CEIL_DIV_EXPR:
1063 if (arg2 == 0)
1064 return NULL_TREE;
1065 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1066 break;
1068 case ROUND_DIV_EXPR:
1069 if (arg2 == 0)
1070 return NULL_TREE;
1071 res = wi::div_round (arg1, arg2, sign, &overflow);
1072 break;
1074 case TRUNC_MOD_EXPR:
1075 if (arg2 == 0)
1076 return NULL_TREE;
1077 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1078 break;
1080 case FLOOR_MOD_EXPR:
1081 if (arg2 == 0)
1082 return NULL_TREE;
1083 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1084 break;
1086 case CEIL_MOD_EXPR:
1087 if (arg2 == 0)
1088 return NULL_TREE;
1089 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1090 break;
1092 case ROUND_MOD_EXPR:
1093 if (arg2 == 0)
1094 return NULL_TREE;
1095 res = wi::mod_round (arg1, arg2, sign, &overflow);
1096 break;
1098 case MIN_EXPR:
1099 res = wi::min (arg1, arg2, sign);
1100 break;
1102 case MAX_EXPR:
1103 res = wi::max (arg1, arg2, sign);
1104 break;
1106 default:
1107 return NULL_TREE;
1110 t = force_fit_type (type, res, overflowable,
1111 (((sign == SIGNED || overflowable == -1)
1112 && overflow)
1113 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1115 return t;
1118 tree
1119 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1121 return int_const_binop_1 (code, arg1, arg2, 1);
1124 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1125 constant. We assume ARG1 and ARG2 have the same data type, or at least
1126 are the same kind of constant and the same machine mode. Return zero if
1127 combining the constants is not allowed in the current operating mode. */
1129 static tree
1130 const_binop (enum tree_code code, tree arg1, tree arg2)
1132 /* Sanity check for the recursive cases. */
1133 if (!arg1 || !arg2)
1134 return NULL_TREE;
1136 STRIP_NOPS (arg1);
1137 STRIP_NOPS (arg2);
1139 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1141 if (code == POINTER_PLUS_EXPR)
1142 return int_const_binop (PLUS_EXPR,
1143 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1145 return int_const_binop (code, arg1, arg2);
1148 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1150 machine_mode mode;
1151 REAL_VALUE_TYPE d1;
1152 REAL_VALUE_TYPE d2;
1153 REAL_VALUE_TYPE value;
1154 REAL_VALUE_TYPE result;
1155 bool inexact;
1156 tree t, type;
1158 /* The following codes are handled by real_arithmetic. */
1159 switch (code)
1161 case PLUS_EXPR:
1162 case MINUS_EXPR:
1163 case MULT_EXPR:
1164 case RDIV_EXPR:
1165 case MIN_EXPR:
1166 case MAX_EXPR:
1167 break;
1169 default:
1170 return NULL_TREE;
1173 d1 = TREE_REAL_CST (arg1);
1174 d2 = TREE_REAL_CST (arg2);
1176 type = TREE_TYPE (arg1);
1177 mode = TYPE_MODE (type);
1179 /* Don't perform operation if we honor signaling NaNs and
1180 either operand is a NaN. */
1181 if (HONOR_SNANS (mode)
1182 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1183 return NULL_TREE;
1185 /* Don't perform operation if it would raise a division
1186 by zero exception. */
1187 if (code == RDIV_EXPR
1188 && REAL_VALUES_EQUAL (d2, dconst0)
1189 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1190 return NULL_TREE;
1192 /* If either operand is a NaN, just return it. Otherwise, set up
1193 for floating-point trap; we return an overflow. */
1194 if (REAL_VALUE_ISNAN (d1))
1195 return arg1;
1196 else if (REAL_VALUE_ISNAN (d2))
1197 return arg2;
1199 inexact = real_arithmetic (&value, code, &d1, &d2);
1200 real_convert (&result, mode, &value);
1202 /* Don't constant fold this floating point operation if
1203 the result has overflowed and flag_trapping_math. */
1204 if (flag_trapping_math
1205 && MODE_HAS_INFINITIES (mode)
1206 && REAL_VALUE_ISINF (result)
1207 && !REAL_VALUE_ISINF (d1)
1208 && !REAL_VALUE_ISINF (d2))
1209 return NULL_TREE;
1211 /* Don't constant fold this floating point operation if the
1212 result may dependent upon the run-time rounding mode and
1213 flag_rounding_math is set, or if GCC's software emulation
1214 is unable to accurately represent the result. */
1215 if ((flag_rounding_math
1216 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1217 && (inexact || !real_identical (&result, &value)))
1218 return NULL_TREE;
1220 t = build_real (type, result);
1222 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1223 return t;
1226 if (TREE_CODE (arg1) == FIXED_CST)
1228 FIXED_VALUE_TYPE f1;
1229 FIXED_VALUE_TYPE f2;
1230 FIXED_VALUE_TYPE result;
1231 tree t, type;
1232 int sat_p;
1233 bool overflow_p;
1235 /* The following codes are handled by fixed_arithmetic. */
1236 switch (code)
1238 case PLUS_EXPR:
1239 case MINUS_EXPR:
1240 case MULT_EXPR:
1241 case TRUNC_DIV_EXPR:
1242 if (TREE_CODE (arg2) != FIXED_CST)
1243 return NULL_TREE;
1244 f2 = TREE_FIXED_CST (arg2);
1245 break;
1247 case LSHIFT_EXPR:
1248 case RSHIFT_EXPR:
1250 if (TREE_CODE (arg2) != INTEGER_CST)
1251 return NULL_TREE;
1252 wide_int w2 = arg2;
1253 f2.data.high = w2.elt (1);
1254 f2.data.low = w2.elt (0);
1255 f2.mode = SImode;
1257 break;
1259 default:
1260 return NULL_TREE;
1263 f1 = TREE_FIXED_CST (arg1);
1264 type = TREE_TYPE (arg1);
1265 sat_p = TYPE_SATURATING (type);
1266 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1267 t = build_fixed (type, result);
1268 /* Propagate overflow flags. */
1269 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1270 TREE_OVERFLOW (t) = 1;
1271 return t;
1274 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1276 tree type = TREE_TYPE (arg1);
1277 tree r1 = TREE_REALPART (arg1);
1278 tree i1 = TREE_IMAGPART (arg1);
1279 tree r2 = TREE_REALPART (arg2);
1280 tree i2 = TREE_IMAGPART (arg2);
1281 tree real, imag;
1283 switch (code)
1285 case PLUS_EXPR:
1286 case MINUS_EXPR:
1287 real = const_binop (code, r1, r2);
1288 imag = const_binop (code, i1, i2);
1289 break;
1291 case MULT_EXPR:
1292 if (COMPLEX_FLOAT_TYPE_P (type))
1293 return do_mpc_arg2 (arg1, arg2, type,
1294 /* do_nonfinite= */ folding_initializer,
1295 mpc_mul);
1297 real = const_binop (MINUS_EXPR,
1298 const_binop (MULT_EXPR, r1, r2),
1299 const_binop (MULT_EXPR, i1, i2));
1300 imag = const_binop (PLUS_EXPR,
1301 const_binop (MULT_EXPR, r1, i2),
1302 const_binop (MULT_EXPR, i1, r2));
1303 break;
1305 case RDIV_EXPR:
1306 if (COMPLEX_FLOAT_TYPE_P (type))
1307 return do_mpc_arg2 (arg1, arg2, type,
1308 /* do_nonfinite= */ folding_initializer,
1309 mpc_div);
1310 /* Fallthru ... */
1311 case TRUNC_DIV_EXPR:
1312 case CEIL_DIV_EXPR:
1313 case FLOOR_DIV_EXPR:
1314 case ROUND_DIV_EXPR:
1315 if (flag_complex_method == 0)
1317 /* Keep this algorithm in sync with
1318 tree-complex.c:expand_complex_div_straight().
1320 Expand complex division to scalars, straightforward algorithm.
1321 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1322 t = br*br + bi*bi
1324 tree magsquared
1325 = const_binop (PLUS_EXPR,
1326 const_binop (MULT_EXPR, r2, r2),
1327 const_binop (MULT_EXPR, i2, i2));
1328 tree t1
1329 = const_binop (PLUS_EXPR,
1330 const_binop (MULT_EXPR, r1, r2),
1331 const_binop (MULT_EXPR, i1, i2));
1332 tree t2
1333 = const_binop (MINUS_EXPR,
1334 const_binop (MULT_EXPR, i1, r2),
1335 const_binop (MULT_EXPR, r1, i2));
1337 real = const_binop (code, t1, magsquared);
1338 imag = const_binop (code, t2, magsquared);
1340 else
1342 /* Keep this algorithm in sync with
1343 tree-complex.c:expand_complex_div_wide().
1345 Expand complex division to scalars, modified algorithm to minimize
1346 overflow with wide input ranges. */
1347 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1348 fold_abs_const (r2, TREE_TYPE (type)),
1349 fold_abs_const (i2, TREE_TYPE (type)));
1351 if (integer_nonzerop (compare))
1353 /* In the TRUE branch, we compute
1354 ratio = br/bi;
1355 div = (br * ratio) + bi;
1356 tr = (ar * ratio) + ai;
1357 ti = (ai * ratio) - ar;
1358 tr = tr / div;
1359 ti = ti / div; */
1360 tree ratio = const_binop (code, r2, i2);
1361 tree div = const_binop (PLUS_EXPR, i2,
1362 const_binop (MULT_EXPR, r2, ratio));
1363 real = const_binop (MULT_EXPR, r1, ratio);
1364 real = const_binop (PLUS_EXPR, real, i1);
1365 real = const_binop (code, real, div);
1367 imag = const_binop (MULT_EXPR, i1, ratio);
1368 imag = const_binop (MINUS_EXPR, imag, r1);
1369 imag = const_binop (code, imag, div);
1371 else
1373 /* In the FALSE branch, we compute
1374 ratio = d/c;
1375 divisor = (d * ratio) + c;
1376 tr = (b * ratio) + a;
1377 ti = b - (a * ratio);
1378 tr = tr / div;
1379 ti = ti / div; */
1380 tree ratio = const_binop (code, i2, r2);
1381 tree div = const_binop (PLUS_EXPR, r2,
1382 const_binop (MULT_EXPR, i2, ratio));
1384 real = const_binop (MULT_EXPR, i1, ratio);
1385 real = const_binop (PLUS_EXPR, real, r1);
1386 real = const_binop (code, real, div);
1388 imag = const_binop (MULT_EXPR, r1, ratio);
1389 imag = const_binop (MINUS_EXPR, i1, imag);
1390 imag = const_binop (code, imag, div);
1393 break;
1395 default:
1396 return NULL_TREE;
1399 if (real && imag)
1400 return build_complex (type, real, imag);
1403 if (TREE_CODE (arg1) == VECTOR_CST
1404 && TREE_CODE (arg2) == VECTOR_CST)
1406 tree type = TREE_TYPE (arg1);
1407 int count = TYPE_VECTOR_SUBPARTS (type), i;
1408 tree *elts = XALLOCAVEC (tree, count);
1410 for (i = 0; i < count; i++)
1412 tree elem1 = VECTOR_CST_ELT (arg1, i);
1413 tree elem2 = VECTOR_CST_ELT (arg2, i);
1415 elts[i] = const_binop (code, elem1, elem2);
1417 /* It is possible that const_binop cannot handle the given
1418 code and return NULL_TREE */
1419 if (elts[i] == NULL_TREE)
1420 return NULL_TREE;
1423 return build_vector (type, elts);
1426 /* Shifts allow a scalar offset for a vector. */
1427 if (TREE_CODE (arg1) == VECTOR_CST
1428 && TREE_CODE (arg2) == INTEGER_CST)
1430 tree type = TREE_TYPE (arg1);
1431 int count = TYPE_VECTOR_SUBPARTS (type), i;
1432 tree *elts = XALLOCAVEC (tree, count);
1434 for (i = 0; i < count; i++)
1436 tree elem1 = VECTOR_CST_ELT (arg1, i);
1438 elts[i] = const_binop (code, elem1, arg2);
1440 /* It is possible that const_binop cannot handle the given
1441 code and return NULL_TREE. */
1442 if (elts[i] == NULL_TREE)
1443 return NULL_TREE;
1446 return build_vector (type, elts);
1448 return NULL_TREE;
1451 /* Overload that adds a TYPE parameter to be able to dispatch
1452 to fold_relational_const. */
1454 tree
1455 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1457 if (TREE_CODE_CLASS (code) == tcc_comparison)
1458 return fold_relational_const (code, type, arg1, arg2);
1460 /* ??? Until we make the const_binop worker take the type of the
1461 result as argument put those cases that need it here. */
1462 switch (code)
1464 case COMPLEX_EXPR:
1465 if ((TREE_CODE (arg1) == REAL_CST
1466 && TREE_CODE (arg2) == REAL_CST)
1467 || (TREE_CODE (arg1) == INTEGER_CST
1468 && TREE_CODE (arg2) == INTEGER_CST))
1469 return build_complex (type, arg1, arg2);
1470 return NULL_TREE;
1472 case VEC_PACK_TRUNC_EXPR:
1473 case VEC_PACK_FIX_TRUNC_EXPR:
1475 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1476 tree *elts;
1478 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1479 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1480 if (TREE_CODE (arg1) != VECTOR_CST
1481 || TREE_CODE (arg2) != VECTOR_CST)
1482 return NULL_TREE;
1484 elts = XALLOCAVEC (tree, nelts);
1485 if (!vec_cst_ctor_to_array (arg1, elts)
1486 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1487 return NULL_TREE;
1489 for (i = 0; i < nelts; i++)
1491 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1492 ? NOP_EXPR : FIX_TRUNC_EXPR,
1493 TREE_TYPE (type), elts[i]);
1494 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1495 return NULL_TREE;
1498 return build_vector (type, elts);
1501 case VEC_WIDEN_MULT_LO_EXPR:
1502 case VEC_WIDEN_MULT_HI_EXPR:
1503 case VEC_WIDEN_MULT_EVEN_EXPR:
1504 case VEC_WIDEN_MULT_ODD_EXPR:
1506 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1507 unsigned int out, ofs, scale;
1508 tree *elts;
1510 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1511 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1512 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1513 return NULL_TREE;
1515 elts = XALLOCAVEC (tree, nelts * 4);
1516 if (!vec_cst_ctor_to_array (arg1, elts)
1517 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1518 return NULL_TREE;
1520 if (code == VEC_WIDEN_MULT_LO_EXPR)
1521 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1522 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1523 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1524 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1525 scale = 1, ofs = 0;
1526 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1527 scale = 1, ofs = 1;
1529 for (out = 0; out < nelts; out++)
1531 unsigned int in1 = (out << scale) + ofs;
1532 unsigned int in2 = in1 + nelts * 2;
1533 tree t1, t2;
1535 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1536 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1538 if (t1 == NULL_TREE || t2 == NULL_TREE)
1539 return NULL_TREE;
1540 elts[out] = const_binop (MULT_EXPR, t1, t2);
1541 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1542 return NULL_TREE;
1545 return build_vector (type, elts);
1548 default:;
1551 if (TREE_CODE_CLASS (code) != tcc_binary)
1552 return NULL_TREE;
1554 /* Make sure type and arg0 have the same saturating flag. */
1555 gcc_checking_assert (TYPE_SATURATING (type)
1556 == TYPE_SATURATING (TREE_TYPE (arg1)));
1558 return const_binop (code, arg1, arg2);
1561 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1562 Return zero if computing the constants is not possible. */
1564 tree
1565 const_unop (enum tree_code code, tree type, tree arg0)
1567 switch (code)
1569 CASE_CONVERT:
1570 case FLOAT_EXPR:
1571 case FIX_TRUNC_EXPR:
1572 case FIXED_CONVERT_EXPR:
1573 return fold_convert_const (code, type, arg0);
1575 case ADDR_SPACE_CONVERT_EXPR:
1576 if (integer_zerop (arg0))
1577 return fold_convert_const (code, type, arg0);
1578 break;
1580 case VIEW_CONVERT_EXPR:
1581 return fold_view_convert_expr (type, arg0);
1583 case NEGATE_EXPR:
1585 /* Can't call fold_negate_const directly here as that doesn't
1586 handle all cases and we might not be able to negate some
1587 constants. */
1588 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1589 if (tem && CONSTANT_CLASS_P (tem))
1590 return tem;
1591 break;
1594 case ABS_EXPR:
1595 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1596 return fold_abs_const (arg0, type);
1597 break;
1599 case CONJ_EXPR:
1600 if (TREE_CODE (arg0) == COMPLEX_CST)
1602 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1603 TREE_TYPE (type));
1604 return build_complex (type, TREE_REALPART (arg0), ipart);
1606 break;
1608 case BIT_NOT_EXPR:
1609 if (TREE_CODE (arg0) == INTEGER_CST)
1610 return fold_not_const (arg0, type);
1611 /* Perform BIT_NOT_EXPR on each element individually. */
1612 else if (TREE_CODE (arg0) == VECTOR_CST)
1614 tree *elements;
1615 tree elem;
1616 unsigned count = VECTOR_CST_NELTS (arg0), i;
1618 elements = XALLOCAVEC (tree, count);
1619 for (i = 0; i < count; i++)
1621 elem = VECTOR_CST_ELT (arg0, i);
1622 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1623 if (elem == NULL_TREE)
1624 break;
1625 elements[i] = elem;
1627 if (i == count)
1628 return build_vector (type, elements);
1630 break;
1632 case TRUTH_NOT_EXPR:
1633 if (TREE_CODE (arg0) == INTEGER_CST)
1634 return constant_boolean_node (integer_zerop (arg0), type);
1635 break;
1637 case REALPART_EXPR:
1638 if (TREE_CODE (arg0) == COMPLEX_CST)
1639 return fold_convert (type, TREE_REALPART (arg0));
1640 break;
1642 case IMAGPART_EXPR:
1643 if (TREE_CODE (arg0) == COMPLEX_CST)
1644 return fold_convert (type, TREE_IMAGPART (arg0));
1645 break;
1647 case VEC_UNPACK_LO_EXPR:
1648 case VEC_UNPACK_HI_EXPR:
1649 case VEC_UNPACK_FLOAT_LO_EXPR:
1650 case VEC_UNPACK_FLOAT_HI_EXPR:
1652 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1653 tree *elts;
1654 enum tree_code subcode;
1656 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1657 if (TREE_CODE (arg0) != VECTOR_CST)
1658 return NULL_TREE;
1660 elts = XALLOCAVEC (tree, nelts * 2);
1661 if (!vec_cst_ctor_to_array (arg0, elts))
1662 return NULL_TREE;
1664 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1665 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1666 elts += nelts;
1668 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1669 subcode = NOP_EXPR;
1670 else
1671 subcode = FLOAT_EXPR;
1673 for (i = 0; i < nelts; i++)
1675 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1676 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1677 return NULL_TREE;
1680 return build_vector (type, elts);
1683 case REDUC_MIN_EXPR:
1684 case REDUC_MAX_EXPR:
1685 case REDUC_PLUS_EXPR:
1687 unsigned int nelts, i;
1688 tree *elts;
1689 enum tree_code subcode;
1691 if (TREE_CODE (arg0) != VECTOR_CST)
1692 return NULL_TREE;
1693 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1695 elts = XALLOCAVEC (tree, nelts);
1696 if (!vec_cst_ctor_to_array (arg0, elts))
1697 return NULL_TREE;
1699 switch (code)
1701 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1702 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1703 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1704 default: gcc_unreachable ();
1707 for (i = 1; i < nelts; i++)
1709 elts[0] = const_binop (subcode, elts[0], elts[i]);
1710 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1711 return NULL_TREE;
1714 return elts[0];
1717 default:
1718 break;
1721 return NULL_TREE;
1724 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1725 indicates which particular sizetype to create. */
1727 tree
1728 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1730 return build_int_cst (sizetype_tab[(int) kind], number);
1733 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1734 is a tree code. The type of the result is taken from the operands.
1735 Both must be equivalent integer types, ala int_binop_types_match_p.
1736 If the operands are constant, so is the result. */
1738 tree
1739 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1741 tree type = TREE_TYPE (arg0);
1743 if (arg0 == error_mark_node || arg1 == error_mark_node)
1744 return error_mark_node;
1746 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1747 TREE_TYPE (arg1)));
1749 /* Handle the special case of two integer constants faster. */
1750 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1752 /* And some specific cases even faster than that. */
1753 if (code == PLUS_EXPR)
1755 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1756 return arg1;
1757 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1758 return arg0;
1760 else if (code == MINUS_EXPR)
1762 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1763 return arg0;
1765 else if (code == MULT_EXPR)
1767 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1768 return arg1;
1771 /* Handle general case of two integer constants. For sizetype
1772 constant calculations we always want to know about overflow,
1773 even in the unsigned case. */
1774 return int_const_binop_1 (code, arg0, arg1, -1);
1777 return fold_build2_loc (loc, code, type, arg0, arg1);
1780 /* Given two values, either both of sizetype or both of bitsizetype,
1781 compute the difference between the two values. Return the value
1782 in signed type corresponding to the type of the operands. */
1784 tree
1785 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1787 tree type = TREE_TYPE (arg0);
1788 tree ctype;
1790 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1791 TREE_TYPE (arg1)));
1793 /* If the type is already signed, just do the simple thing. */
1794 if (!TYPE_UNSIGNED (type))
1795 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1797 if (type == sizetype)
1798 ctype = ssizetype;
1799 else if (type == bitsizetype)
1800 ctype = sbitsizetype;
1801 else
1802 ctype = signed_type_for (type);
1804 /* If either operand is not a constant, do the conversions to the signed
1805 type and subtract. The hardware will do the right thing with any
1806 overflow in the subtraction. */
1807 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1808 return size_binop_loc (loc, MINUS_EXPR,
1809 fold_convert_loc (loc, ctype, arg0),
1810 fold_convert_loc (loc, ctype, arg1));
1812 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1813 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1814 overflow) and negate (which can't either). Special-case a result
1815 of zero while we're here. */
1816 if (tree_int_cst_equal (arg0, arg1))
1817 return build_int_cst (ctype, 0);
1818 else if (tree_int_cst_lt (arg1, arg0))
1819 return fold_convert_loc (loc, ctype,
1820 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1821 else
1822 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1823 fold_convert_loc (loc, ctype,
1824 size_binop_loc (loc,
1825 MINUS_EXPR,
1826 arg1, arg0)));
1829 /* A subroutine of fold_convert_const handling conversions of an
1830 INTEGER_CST to another integer type. */
1832 static tree
1833 fold_convert_const_int_from_int (tree type, const_tree arg1)
1835 /* Given an integer constant, make new constant with new type,
1836 appropriately sign-extended or truncated. Use widest_int
1837 so that any extension is done according ARG1's type. */
1838 return force_fit_type (type, wi::to_widest (arg1),
1839 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1840 TREE_OVERFLOW (arg1));
1843 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1844 to an integer type. */
1846 static tree
1847 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1849 bool overflow = false;
1850 tree t;
1852 /* The following code implements the floating point to integer
1853 conversion rules required by the Java Language Specification,
1854 that IEEE NaNs are mapped to zero and values that overflow
1855 the target precision saturate, i.e. values greater than
1856 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1857 are mapped to INT_MIN. These semantics are allowed by the
1858 C and C++ standards that simply state that the behavior of
1859 FP-to-integer conversion is unspecified upon overflow. */
1861 wide_int val;
1862 REAL_VALUE_TYPE r;
1863 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1865 switch (code)
1867 case FIX_TRUNC_EXPR:
1868 real_trunc (&r, VOIDmode, &x);
1869 break;
1871 default:
1872 gcc_unreachable ();
1875 /* If R is NaN, return zero and show we have an overflow. */
1876 if (REAL_VALUE_ISNAN (r))
1878 overflow = true;
1879 val = wi::zero (TYPE_PRECISION (type));
1882 /* See if R is less than the lower bound or greater than the
1883 upper bound. */
1885 if (! overflow)
1887 tree lt = TYPE_MIN_VALUE (type);
1888 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1889 if (REAL_VALUES_LESS (r, l))
1891 overflow = true;
1892 val = lt;
1896 if (! overflow)
1898 tree ut = TYPE_MAX_VALUE (type);
1899 if (ut)
1901 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1902 if (REAL_VALUES_LESS (u, r))
1904 overflow = true;
1905 val = ut;
1910 if (! overflow)
1911 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1913 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1914 return t;
1917 /* A subroutine of fold_convert_const handling conversions of a
1918 FIXED_CST to an integer type. */
1920 static tree
1921 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1923 tree t;
1924 double_int temp, temp_trunc;
1925 unsigned int mode;
1927 /* Right shift FIXED_CST to temp by fbit. */
1928 temp = TREE_FIXED_CST (arg1).data;
1929 mode = TREE_FIXED_CST (arg1).mode;
1930 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1932 temp = temp.rshift (GET_MODE_FBIT (mode),
1933 HOST_BITS_PER_DOUBLE_INT,
1934 SIGNED_FIXED_POINT_MODE_P (mode));
1936 /* Left shift temp to temp_trunc by fbit. */
1937 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1938 HOST_BITS_PER_DOUBLE_INT,
1939 SIGNED_FIXED_POINT_MODE_P (mode));
1941 else
1943 temp = double_int_zero;
1944 temp_trunc = double_int_zero;
1947 /* If FIXED_CST is negative, we need to round the value toward 0.
1948 By checking if the fractional bits are not zero to add 1 to temp. */
1949 if (SIGNED_FIXED_POINT_MODE_P (mode)
1950 && temp_trunc.is_negative ()
1951 && TREE_FIXED_CST (arg1).data != temp_trunc)
1952 temp += double_int_one;
1954 /* Given a fixed-point constant, make new constant with new type,
1955 appropriately sign-extended or truncated. */
1956 t = force_fit_type (type, temp, -1,
1957 (temp.is_negative ()
1958 && (TYPE_UNSIGNED (type)
1959 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1960 | TREE_OVERFLOW (arg1));
1962 return t;
1965 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1966 to another floating point type. */
1968 static tree
1969 fold_convert_const_real_from_real (tree type, const_tree arg1)
1971 REAL_VALUE_TYPE value;
1972 tree t;
1974 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1975 t = build_real (type, value);
1977 /* If converting an infinity or NAN to a representation that doesn't
1978 have one, set the overflow bit so that we can produce some kind of
1979 error message at the appropriate point if necessary. It's not the
1980 most user-friendly message, but it's better than nothing. */
1981 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1982 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1983 TREE_OVERFLOW (t) = 1;
1984 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1985 && !MODE_HAS_NANS (TYPE_MODE (type)))
1986 TREE_OVERFLOW (t) = 1;
1987 /* Regular overflow, conversion produced an infinity in a mode that
1988 can't represent them. */
1989 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1990 && REAL_VALUE_ISINF (value)
1991 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1992 TREE_OVERFLOW (t) = 1;
1993 else
1994 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1995 return t;
1998 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1999 to a floating point type. */
2001 static tree
2002 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2004 REAL_VALUE_TYPE value;
2005 tree t;
2007 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2008 t = build_real (type, value);
2010 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2011 return t;
2014 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2015 to another fixed-point type. */
2017 static tree
2018 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2020 FIXED_VALUE_TYPE value;
2021 tree t;
2022 bool overflow_p;
2024 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2025 TYPE_SATURATING (type));
2026 t = build_fixed (type, value);
2028 /* Propagate overflow flags. */
2029 if (overflow_p | TREE_OVERFLOW (arg1))
2030 TREE_OVERFLOW (t) = 1;
2031 return t;
2034 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2035 to a fixed-point type. */
2037 static tree
2038 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2040 FIXED_VALUE_TYPE value;
2041 tree t;
2042 bool overflow_p;
2043 double_int di;
2045 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2047 di.low = TREE_INT_CST_ELT (arg1, 0);
2048 if (TREE_INT_CST_NUNITS (arg1) == 1)
2049 di.high = (HOST_WIDE_INT) di.low < 0 ? (HOST_WIDE_INT) -1 : 0;
2050 else
2051 di.high = TREE_INT_CST_ELT (arg1, 1);
2053 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2054 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2055 TYPE_SATURATING (type));
2056 t = build_fixed (type, value);
2058 /* Propagate overflow flags. */
2059 if (overflow_p | TREE_OVERFLOW (arg1))
2060 TREE_OVERFLOW (t) = 1;
2061 return t;
2064 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2065 to a fixed-point type. */
2067 static tree
2068 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2070 FIXED_VALUE_TYPE value;
2071 tree t;
2072 bool overflow_p;
2074 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2075 &TREE_REAL_CST (arg1),
2076 TYPE_SATURATING (type));
2077 t = build_fixed (type, value);
2079 /* Propagate overflow flags. */
2080 if (overflow_p | TREE_OVERFLOW (arg1))
2081 TREE_OVERFLOW (t) = 1;
2082 return t;
2085 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2086 type TYPE. If no simplification can be done return NULL_TREE. */
2088 static tree
2089 fold_convert_const (enum tree_code code, tree type, tree arg1)
2091 if (TREE_TYPE (arg1) == type)
2092 return arg1;
2094 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2095 || TREE_CODE (type) == OFFSET_TYPE)
2097 if (TREE_CODE (arg1) == INTEGER_CST)
2098 return fold_convert_const_int_from_int (type, arg1);
2099 else if (TREE_CODE (arg1) == REAL_CST)
2100 return fold_convert_const_int_from_real (code, type, arg1);
2101 else if (TREE_CODE (arg1) == FIXED_CST)
2102 return fold_convert_const_int_from_fixed (type, arg1);
2104 else if (TREE_CODE (type) == REAL_TYPE)
2106 if (TREE_CODE (arg1) == INTEGER_CST)
2107 return build_real_from_int_cst (type, arg1);
2108 else if (TREE_CODE (arg1) == REAL_CST)
2109 return fold_convert_const_real_from_real (type, arg1);
2110 else if (TREE_CODE (arg1) == FIXED_CST)
2111 return fold_convert_const_real_from_fixed (type, arg1);
2113 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2115 if (TREE_CODE (arg1) == FIXED_CST)
2116 return fold_convert_const_fixed_from_fixed (type, arg1);
2117 else if (TREE_CODE (arg1) == INTEGER_CST)
2118 return fold_convert_const_fixed_from_int (type, arg1);
2119 else if (TREE_CODE (arg1) == REAL_CST)
2120 return fold_convert_const_fixed_from_real (type, arg1);
2122 return NULL_TREE;
2125 /* Construct a vector of zero elements of vector type TYPE. */
2127 static tree
2128 build_zero_vector (tree type)
2130 tree t;
2132 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2133 return build_vector_from_val (type, t);
2136 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2138 bool
2139 fold_convertible_p (const_tree type, const_tree arg)
2141 tree orig = TREE_TYPE (arg);
2143 if (type == orig)
2144 return true;
2146 if (TREE_CODE (arg) == ERROR_MARK
2147 || TREE_CODE (type) == ERROR_MARK
2148 || TREE_CODE (orig) == ERROR_MARK)
2149 return false;
2151 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2152 return true;
2154 switch (TREE_CODE (type))
2156 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2157 case POINTER_TYPE: case REFERENCE_TYPE:
2158 case OFFSET_TYPE:
2159 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2160 || TREE_CODE (orig) == OFFSET_TYPE)
2161 return true;
2162 return (TREE_CODE (orig) == VECTOR_TYPE
2163 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2165 case REAL_TYPE:
2166 case FIXED_POINT_TYPE:
2167 case COMPLEX_TYPE:
2168 case VECTOR_TYPE:
2169 case VOID_TYPE:
2170 return TREE_CODE (type) == TREE_CODE (orig);
2172 default:
2173 return false;
2177 /* Convert expression ARG to type TYPE. Used by the middle-end for
2178 simple conversions in preference to calling the front-end's convert. */
2180 tree
2181 fold_convert_loc (location_t loc, tree type, tree arg)
2183 tree orig = TREE_TYPE (arg);
2184 tree tem;
2186 if (type == orig)
2187 return arg;
2189 if (TREE_CODE (arg) == ERROR_MARK
2190 || TREE_CODE (type) == ERROR_MARK
2191 || TREE_CODE (orig) == ERROR_MARK)
2192 return error_mark_node;
2194 switch (TREE_CODE (type))
2196 case POINTER_TYPE:
2197 case REFERENCE_TYPE:
2198 /* Handle conversions between pointers to different address spaces. */
2199 if (POINTER_TYPE_P (orig)
2200 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2201 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2202 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2203 /* fall through */
2205 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2206 case OFFSET_TYPE:
2207 if (TREE_CODE (arg) == INTEGER_CST)
2209 tem = fold_convert_const (NOP_EXPR, type, arg);
2210 if (tem != NULL_TREE)
2211 return tem;
2213 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2214 || TREE_CODE (orig) == OFFSET_TYPE)
2215 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2216 if (TREE_CODE (orig) == COMPLEX_TYPE)
2217 return fold_convert_loc (loc, type,
2218 fold_build1_loc (loc, REALPART_EXPR,
2219 TREE_TYPE (orig), arg));
2220 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2221 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2222 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2224 case REAL_TYPE:
2225 if (TREE_CODE (arg) == INTEGER_CST)
2227 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2228 if (tem != NULL_TREE)
2229 return tem;
2231 else if (TREE_CODE (arg) == REAL_CST)
2233 tem = fold_convert_const (NOP_EXPR, type, arg);
2234 if (tem != NULL_TREE)
2235 return tem;
2237 else if (TREE_CODE (arg) == FIXED_CST)
2239 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2240 if (tem != NULL_TREE)
2241 return tem;
2244 switch (TREE_CODE (orig))
2246 case INTEGER_TYPE:
2247 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2248 case POINTER_TYPE: case REFERENCE_TYPE:
2249 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2251 case REAL_TYPE:
2252 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2254 case FIXED_POINT_TYPE:
2255 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2257 case COMPLEX_TYPE:
2258 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2259 return fold_convert_loc (loc, type, tem);
2261 default:
2262 gcc_unreachable ();
2265 case FIXED_POINT_TYPE:
2266 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2267 || TREE_CODE (arg) == REAL_CST)
2269 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2270 if (tem != NULL_TREE)
2271 goto fold_convert_exit;
2274 switch (TREE_CODE (orig))
2276 case FIXED_POINT_TYPE:
2277 case INTEGER_TYPE:
2278 case ENUMERAL_TYPE:
2279 case BOOLEAN_TYPE:
2280 case REAL_TYPE:
2281 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2283 case COMPLEX_TYPE:
2284 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2285 return fold_convert_loc (loc, type, tem);
2287 default:
2288 gcc_unreachable ();
2291 case COMPLEX_TYPE:
2292 switch (TREE_CODE (orig))
2294 case INTEGER_TYPE:
2295 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2296 case POINTER_TYPE: case REFERENCE_TYPE:
2297 case REAL_TYPE:
2298 case FIXED_POINT_TYPE:
2299 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2300 fold_convert_loc (loc, TREE_TYPE (type), arg),
2301 fold_convert_loc (loc, TREE_TYPE (type),
2302 integer_zero_node));
2303 case COMPLEX_TYPE:
2305 tree rpart, ipart;
2307 if (TREE_CODE (arg) == COMPLEX_EXPR)
2309 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2310 TREE_OPERAND (arg, 0));
2311 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2312 TREE_OPERAND (arg, 1));
2313 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2316 arg = save_expr (arg);
2317 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2318 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2319 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2320 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2321 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2324 default:
2325 gcc_unreachable ();
2328 case VECTOR_TYPE:
2329 if (integer_zerop (arg))
2330 return build_zero_vector (type);
2331 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2332 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2333 || TREE_CODE (orig) == VECTOR_TYPE);
2334 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2336 case VOID_TYPE:
2337 tem = fold_ignored_result (arg);
2338 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2340 default:
2341 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2342 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2343 gcc_unreachable ();
2345 fold_convert_exit:
2346 protected_set_expr_location_unshare (tem, loc);
2347 return tem;
2350 /* Return false if expr can be assumed not to be an lvalue, true
2351 otherwise. */
2353 static bool
2354 maybe_lvalue_p (const_tree x)
2356 /* We only need to wrap lvalue tree codes. */
2357 switch (TREE_CODE (x))
2359 case VAR_DECL:
2360 case PARM_DECL:
2361 case RESULT_DECL:
2362 case LABEL_DECL:
2363 case FUNCTION_DECL:
2364 case SSA_NAME:
2366 case COMPONENT_REF:
2367 case MEM_REF:
2368 case INDIRECT_REF:
2369 case ARRAY_REF:
2370 case ARRAY_RANGE_REF:
2371 case BIT_FIELD_REF:
2372 case OBJ_TYPE_REF:
2374 case REALPART_EXPR:
2375 case IMAGPART_EXPR:
2376 case PREINCREMENT_EXPR:
2377 case PREDECREMENT_EXPR:
2378 case SAVE_EXPR:
2379 case TRY_CATCH_EXPR:
2380 case WITH_CLEANUP_EXPR:
2381 case COMPOUND_EXPR:
2382 case MODIFY_EXPR:
2383 case TARGET_EXPR:
2384 case COND_EXPR:
2385 case BIND_EXPR:
2386 break;
2388 default:
2389 /* Assume the worst for front-end tree codes. */
2390 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2391 break;
2392 return false;
2395 return true;
2398 /* Return an expr equal to X but certainly not valid as an lvalue. */
2400 tree
2401 non_lvalue_loc (location_t loc, tree x)
2403 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2404 us. */
2405 if (in_gimple_form)
2406 return x;
2408 if (! maybe_lvalue_p (x))
2409 return x;
2410 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2413 /* When pedantic, return an expr equal to X but certainly not valid as a
2414 pedantic lvalue. Otherwise, return X. */
2416 static tree
2417 pedantic_non_lvalue_loc (location_t loc, tree x)
2419 return protected_set_expr_location_unshare (x, loc);
2422 /* Given a tree comparison code, return the code that is the logical inverse.
2423 It is generally not safe to do this for floating-point comparisons, except
2424 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2425 ERROR_MARK in this case. */
2427 enum tree_code
2428 invert_tree_comparison (enum tree_code code, bool honor_nans)
2430 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2431 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2432 return ERROR_MARK;
2434 switch (code)
2436 case EQ_EXPR:
2437 return NE_EXPR;
2438 case NE_EXPR:
2439 return EQ_EXPR;
2440 case GT_EXPR:
2441 return honor_nans ? UNLE_EXPR : LE_EXPR;
2442 case GE_EXPR:
2443 return honor_nans ? UNLT_EXPR : LT_EXPR;
2444 case LT_EXPR:
2445 return honor_nans ? UNGE_EXPR : GE_EXPR;
2446 case LE_EXPR:
2447 return honor_nans ? UNGT_EXPR : GT_EXPR;
2448 case LTGT_EXPR:
2449 return UNEQ_EXPR;
2450 case UNEQ_EXPR:
2451 return LTGT_EXPR;
2452 case UNGT_EXPR:
2453 return LE_EXPR;
2454 case UNGE_EXPR:
2455 return LT_EXPR;
2456 case UNLT_EXPR:
2457 return GE_EXPR;
2458 case UNLE_EXPR:
2459 return GT_EXPR;
2460 case ORDERED_EXPR:
2461 return UNORDERED_EXPR;
2462 case UNORDERED_EXPR:
2463 return ORDERED_EXPR;
2464 default:
2465 gcc_unreachable ();
2469 /* Similar, but return the comparison that results if the operands are
2470 swapped. This is safe for floating-point. */
2472 enum tree_code
2473 swap_tree_comparison (enum tree_code code)
2475 switch (code)
2477 case EQ_EXPR:
2478 case NE_EXPR:
2479 case ORDERED_EXPR:
2480 case UNORDERED_EXPR:
2481 case LTGT_EXPR:
2482 case UNEQ_EXPR:
2483 return code;
2484 case GT_EXPR:
2485 return LT_EXPR;
2486 case GE_EXPR:
2487 return LE_EXPR;
2488 case LT_EXPR:
2489 return GT_EXPR;
2490 case LE_EXPR:
2491 return GE_EXPR;
2492 case UNGT_EXPR:
2493 return UNLT_EXPR;
2494 case UNGE_EXPR:
2495 return UNLE_EXPR;
2496 case UNLT_EXPR:
2497 return UNGT_EXPR;
2498 case UNLE_EXPR:
2499 return UNGE_EXPR;
2500 default:
2501 gcc_unreachable ();
2506 /* Convert a comparison tree code from an enum tree_code representation
2507 into a compcode bit-based encoding. This function is the inverse of
2508 compcode_to_comparison. */
2510 static enum comparison_code
2511 comparison_to_compcode (enum tree_code code)
2513 switch (code)
2515 case LT_EXPR:
2516 return COMPCODE_LT;
2517 case EQ_EXPR:
2518 return COMPCODE_EQ;
2519 case LE_EXPR:
2520 return COMPCODE_LE;
2521 case GT_EXPR:
2522 return COMPCODE_GT;
2523 case NE_EXPR:
2524 return COMPCODE_NE;
2525 case GE_EXPR:
2526 return COMPCODE_GE;
2527 case ORDERED_EXPR:
2528 return COMPCODE_ORD;
2529 case UNORDERED_EXPR:
2530 return COMPCODE_UNORD;
2531 case UNLT_EXPR:
2532 return COMPCODE_UNLT;
2533 case UNEQ_EXPR:
2534 return COMPCODE_UNEQ;
2535 case UNLE_EXPR:
2536 return COMPCODE_UNLE;
2537 case UNGT_EXPR:
2538 return COMPCODE_UNGT;
2539 case LTGT_EXPR:
2540 return COMPCODE_LTGT;
2541 case UNGE_EXPR:
2542 return COMPCODE_UNGE;
2543 default:
2544 gcc_unreachable ();
2548 /* Convert a compcode bit-based encoding of a comparison operator back
2549 to GCC's enum tree_code representation. This function is the
2550 inverse of comparison_to_compcode. */
2552 static enum tree_code
2553 compcode_to_comparison (enum comparison_code code)
2555 switch (code)
2557 case COMPCODE_LT:
2558 return LT_EXPR;
2559 case COMPCODE_EQ:
2560 return EQ_EXPR;
2561 case COMPCODE_LE:
2562 return LE_EXPR;
2563 case COMPCODE_GT:
2564 return GT_EXPR;
2565 case COMPCODE_NE:
2566 return NE_EXPR;
2567 case COMPCODE_GE:
2568 return GE_EXPR;
2569 case COMPCODE_ORD:
2570 return ORDERED_EXPR;
2571 case COMPCODE_UNORD:
2572 return UNORDERED_EXPR;
2573 case COMPCODE_UNLT:
2574 return UNLT_EXPR;
2575 case COMPCODE_UNEQ:
2576 return UNEQ_EXPR;
2577 case COMPCODE_UNLE:
2578 return UNLE_EXPR;
2579 case COMPCODE_UNGT:
2580 return UNGT_EXPR;
2581 case COMPCODE_LTGT:
2582 return LTGT_EXPR;
2583 case COMPCODE_UNGE:
2584 return UNGE_EXPR;
2585 default:
2586 gcc_unreachable ();
2590 /* Return a tree for the comparison which is the combination of
2591 doing the AND or OR (depending on CODE) of the two operations LCODE
2592 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2593 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2594 if this makes the transformation invalid. */
2596 tree
2597 combine_comparisons (location_t loc,
2598 enum tree_code code, enum tree_code lcode,
2599 enum tree_code rcode, tree truth_type,
2600 tree ll_arg, tree lr_arg)
2602 bool honor_nans = HONOR_NANS (ll_arg);
2603 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2604 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2605 int compcode;
2607 switch (code)
2609 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2610 compcode = lcompcode & rcompcode;
2611 break;
2613 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2614 compcode = lcompcode | rcompcode;
2615 break;
2617 default:
2618 return NULL_TREE;
2621 if (!honor_nans)
2623 /* Eliminate unordered comparisons, as well as LTGT and ORD
2624 which are not used unless the mode has NaNs. */
2625 compcode &= ~COMPCODE_UNORD;
2626 if (compcode == COMPCODE_LTGT)
2627 compcode = COMPCODE_NE;
2628 else if (compcode == COMPCODE_ORD)
2629 compcode = COMPCODE_TRUE;
2631 else if (flag_trapping_math)
2633 /* Check that the original operation and the optimized ones will trap
2634 under the same condition. */
2635 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2636 && (lcompcode != COMPCODE_EQ)
2637 && (lcompcode != COMPCODE_ORD);
2638 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2639 && (rcompcode != COMPCODE_EQ)
2640 && (rcompcode != COMPCODE_ORD);
2641 bool trap = (compcode & COMPCODE_UNORD) == 0
2642 && (compcode != COMPCODE_EQ)
2643 && (compcode != COMPCODE_ORD);
2645 /* In a short-circuited boolean expression the LHS might be
2646 such that the RHS, if evaluated, will never trap. For
2647 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2648 if neither x nor y is NaN. (This is a mixed blessing: for
2649 example, the expression above will never trap, hence
2650 optimizing it to x < y would be invalid). */
2651 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2652 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2653 rtrap = false;
2655 /* If the comparison was short-circuited, and only the RHS
2656 trapped, we may now generate a spurious trap. */
2657 if (rtrap && !ltrap
2658 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2659 return NULL_TREE;
2661 /* If we changed the conditions that cause a trap, we lose. */
2662 if ((ltrap || rtrap) != trap)
2663 return NULL_TREE;
2666 if (compcode == COMPCODE_TRUE)
2667 return constant_boolean_node (true, truth_type);
2668 else if (compcode == COMPCODE_FALSE)
2669 return constant_boolean_node (false, truth_type);
2670 else
2672 enum tree_code tcode;
2674 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2675 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2679 /* Return nonzero if two operands (typically of the same tree node)
2680 are necessarily equal. If either argument has side-effects this
2681 function returns zero. FLAGS modifies behavior as follows:
2683 If OEP_ONLY_CONST is set, only return nonzero for constants.
2684 This function tests whether the operands are indistinguishable;
2685 it does not test whether they are equal using C's == operation.
2686 The distinction is important for IEEE floating point, because
2687 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2688 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2690 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2691 even though it may hold multiple values during a function.
2692 This is because a GCC tree node guarantees that nothing else is
2693 executed between the evaluation of its "operands" (which may often
2694 be evaluated in arbitrary order). Hence if the operands themselves
2695 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2696 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2697 unset means assuming isochronic (or instantaneous) tree equivalence.
2698 Unless comparing arbitrary expression trees, such as from different
2699 statements, this flag can usually be left unset.
2701 If OEP_PURE_SAME is set, then pure functions with identical arguments
2702 are considered the same. It is used when the caller has other ways
2703 to ensure that global memory is unchanged in between. */
2706 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2708 /* If either is ERROR_MARK, they aren't equal. */
2709 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2710 || TREE_TYPE (arg0) == error_mark_node
2711 || TREE_TYPE (arg1) == error_mark_node)
2712 return 0;
2714 /* Similar, if either does not have a type (like a released SSA name),
2715 they aren't equal. */
2716 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2717 return 0;
2719 /* Check equality of integer constants before bailing out due to
2720 precision differences. */
2721 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2722 return tree_int_cst_equal (arg0, arg1);
2724 /* If both types don't have the same signedness, then we can't consider
2725 them equal. We must check this before the STRIP_NOPS calls
2726 because they may change the signedness of the arguments. As pointers
2727 strictly don't have a signedness, require either two pointers or
2728 two non-pointers as well. */
2729 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2730 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2731 return 0;
2733 /* We cannot consider pointers to different address space equal. */
2734 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2735 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2736 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2737 return 0;
2739 /* If both types don't have the same precision, then it is not safe
2740 to strip NOPs. */
2741 if (element_precision (TREE_TYPE (arg0))
2742 != element_precision (TREE_TYPE (arg1)))
2743 return 0;
2745 STRIP_NOPS (arg0);
2746 STRIP_NOPS (arg1);
2748 /* In case both args are comparisons but with different comparison
2749 code, try to swap the comparison operands of one arg to produce
2750 a match and compare that variant. */
2751 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2752 && COMPARISON_CLASS_P (arg0)
2753 && COMPARISON_CLASS_P (arg1))
2755 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2757 if (TREE_CODE (arg0) == swap_code)
2758 return operand_equal_p (TREE_OPERAND (arg0, 0),
2759 TREE_OPERAND (arg1, 1), flags)
2760 && operand_equal_p (TREE_OPERAND (arg0, 1),
2761 TREE_OPERAND (arg1, 0), flags);
2764 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2765 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2766 && !(CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1)))
2767 return 0;
2769 /* This is needed for conversions and for COMPONENT_REF.
2770 Might as well play it safe and always test this. */
2771 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2772 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2773 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2774 return 0;
2776 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2777 We don't care about side effects in that case because the SAVE_EXPR
2778 takes care of that for us. In all other cases, two expressions are
2779 equal if they have no side effects. If we have two identical
2780 expressions with side effects that should be treated the same due
2781 to the only side effects being identical SAVE_EXPR's, that will
2782 be detected in the recursive calls below.
2783 If we are taking an invariant address of two identical objects
2784 they are necessarily equal as well. */
2785 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2786 && (TREE_CODE (arg0) == SAVE_EXPR
2787 || (flags & OEP_CONSTANT_ADDRESS_OF)
2788 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2789 return 1;
2791 /* Next handle constant cases, those for which we can return 1 even
2792 if ONLY_CONST is set. */
2793 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2794 switch (TREE_CODE (arg0))
2796 case INTEGER_CST:
2797 return tree_int_cst_equal (arg0, arg1);
2799 case FIXED_CST:
2800 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2801 TREE_FIXED_CST (arg1));
2803 case REAL_CST:
2804 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2805 TREE_REAL_CST (arg1)))
2806 return 1;
2809 if (!HONOR_SIGNED_ZEROS (arg0))
2811 /* If we do not distinguish between signed and unsigned zero,
2812 consider them equal. */
2813 if (real_zerop (arg0) && real_zerop (arg1))
2814 return 1;
2816 return 0;
2818 case VECTOR_CST:
2820 unsigned i;
2822 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2823 return 0;
2825 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2827 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2828 VECTOR_CST_ELT (arg1, i), flags))
2829 return 0;
2831 return 1;
2834 case COMPLEX_CST:
2835 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2836 flags)
2837 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2838 flags));
2840 case STRING_CST:
2841 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2842 && ! memcmp (TREE_STRING_POINTER (arg0),
2843 TREE_STRING_POINTER (arg1),
2844 TREE_STRING_LENGTH (arg0)));
2846 case ADDR_EXPR:
2847 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2848 TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)
2849 ? OEP_CONSTANT_ADDRESS_OF | OEP_ADDRESS_OF : 0);
2850 default:
2851 break;
2854 if (flags & OEP_ONLY_CONST)
2855 return 0;
2857 /* Define macros to test an operand from arg0 and arg1 for equality and a
2858 variant that allows null and views null as being different from any
2859 non-null value. In the latter case, if either is null, the both
2860 must be; otherwise, do the normal comparison. */
2861 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2862 TREE_OPERAND (arg1, N), flags)
2864 #define OP_SAME_WITH_NULL(N) \
2865 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2866 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2868 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2870 case tcc_unary:
2871 /* Two conversions are equal only if signedness and modes match. */
2872 switch (TREE_CODE (arg0))
2874 CASE_CONVERT:
2875 case FIX_TRUNC_EXPR:
2876 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2877 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2878 return 0;
2879 break;
2880 default:
2881 break;
2884 return OP_SAME (0);
2887 case tcc_comparison:
2888 case tcc_binary:
2889 if (OP_SAME (0) && OP_SAME (1))
2890 return 1;
2892 /* For commutative ops, allow the other order. */
2893 return (commutative_tree_code (TREE_CODE (arg0))
2894 && operand_equal_p (TREE_OPERAND (arg0, 0),
2895 TREE_OPERAND (arg1, 1), flags)
2896 && operand_equal_p (TREE_OPERAND (arg0, 1),
2897 TREE_OPERAND (arg1, 0), flags));
2899 case tcc_reference:
2900 /* If either of the pointer (or reference) expressions we are
2901 dereferencing contain a side effect, these cannot be equal,
2902 but their addresses can be. */
2903 if ((flags & OEP_CONSTANT_ADDRESS_OF) == 0
2904 && (TREE_SIDE_EFFECTS (arg0)
2905 || TREE_SIDE_EFFECTS (arg1)))
2906 return 0;
2908 switch (TREE_CODE (arg0))
2910 case INDIRECT_REF:
2911 if (!(flags & OEP_ADDRESS_OF)
2912 && (TYPE_ALIGN (TREE_TYPE (arg0))
2913 != TYPE_ALIGN (TREE_TYPE (arg1))))
2914 return 0;
2915 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2916 return OP_SAME (0);
2918 case REALPART_EXPR:
2919 case IMAGPART_EXPR:
2920 return OP_SAME (0);
2922 case TARGET_MEM_REF:
2923 case MEM_REF:
2924 /* Require equal access sizes, and similar pointer types.
2925 We can have incomplete types for array references of
2926 variable-sized arrays from the Fortran frontend
2927 though. Also verify the types are compatible. */
2928 if (!((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2929 || (TYPE_SIZE (TREE_TYPE (arg0))
2930 && TYPE_SIZE (TREE_TYPE (arg1))
2931 && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2932 TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2933 && types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1))
2934 && ((flags & OEP_ADDRESS_OF)
2935 || (alias_ptr_types_compatible_p
2936 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
2937 TREE_TYPE (TREE_OPERAND (arg1, 1)))
2938 && (MR_DEPENDENCE_CLIQUE (arg0)
2939 == MR_DEPENDENCE_CLIQUE (arg1))
2940 && (MR_DEPENDENCE_BASE (arg0)
2941 == MR_DEPENDENCE_BASE (arg1))
2942 && (TYPE_ALIGN (TREE_TYPE (arg0))
2943 == TYPE_ALIGN (TREE_TYPE (arg1)))))))
2944 return 0;
2945 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2946 return (OP_SAME (0) && OP_SAME (1)
2947 /* TARGET_MEM_REF require equal extra operands. */
2948 && (TREE_CODE (arg0) != TARGET_MEM_REF
2949 || (OP_SAME_WITH_NULL (2)
2950 && OP_SAME_WITH_NULL (3)
2951 && OP_SAME_WITH_NULL (4))));
2953 case ARRAY_REF:
2954 case ARRAY_RANGE_REF:
2955 /* Operands 2 and 3 may be null.
2956 Compare the array index by value if it is constant first as we
2957 may have different types but same value here. */
2958 if (!OP_SAME (0))
2959 return 0;
2960 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2961 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2962 TREE_OPERAND (arg1, 1))
2963 || OP_SAME (1))
2964 && OP_SAME_WITH_NULL (2)
2965 && OP_SAME_WITH_NULL (3));
2967 case COMPONENT_REF:
2968 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2969 may be NULL when we're called to compare MEM_EXPRs. */
2970 if (!OP_SAME_WITH_NULL (0)
2971 || !OP_SAME (1))
2972 return 0;
2973 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2974 return OP_SAME_WITH_NULL (2);
2976 case BIT_FIELD_REF:
2977 if (!OP_SAME (0))
2978 return 0;
2979 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2980 return OP_SAME (1) && OP_SAME (2);
2982 default:
2983 return 0;
2986 case tcc_expression:
2987 switch (TREE_CODE (arg0))
2989 case ADDR_EXPR:
2990 return operand_equal_p (TREE_OPERAND (arg0, 0),
2991 TREE_OPERAND (arg1, 0),
2992 flags | OEP_ADDRESS_OF);
2994 case TRUTH_NOT_EXPR:
2995 return OP_SAME (0);
2997 case TRUTH_ANDIF_EXPR:
2998 case TRUTH_ORIF_EXPR:
2999 return OP_SAME (0) && OP_SAME (1);
3001 case FMA_EXPR:
3002 case WIDEN_MULT_PLUS_EXPR:
3003 case WIDEN_MULT_MINUS_EXPR:
3004 if (!OP_SAME (2))
3005 return 0;
3006 /* The multiplcation operands are commutative. */
3007 /* FALLTHRU */
3009 case TRUTH_AND_EXPR:
3010 case TRUTH_OR_EXPR:
3011 case TRUTH_XOR_EXPR:
3012 if (OP_SAME (0) && OP_SAME (1))
3013 return 1;
3015 /* Otherwise take into account this is a commutative operation. */
3016 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3017 TREE_OPERAND (arg1, 1), flags)
3018 && operand_equal_p (TREE_OPERAND (arg0, 1),
3019 TREE_OPERAND (arg1, 0), flags));
3021 case COND_EXPR:
3022 case VEC_COND_EXPR:
3023 case DOT_PROD_EXPR:
3024 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3026 default:
3027 return 0;
3030 case tcc_vl_exp:
3031 switch (TREE_CODE (arg0))
3033 case CALL_EXPR:
3034 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3035 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3036 /* If not both CALL_EXPRs are either internal or normal function
3037 functions, then they are not equal. */
3038 return 0;
3039 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3041 /* If the CALL_EXPRs call different internal functions, then they
3042 are not equal. */
3043 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3044 return 0;
3046 else
3048 /* If the CALL_EXPRs call different functions, then they are not
3049 equal. */
3050 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3051 flags))
3052 return 0;
3056 unsigned int cef = call_expr_flags (arg0);
3057 if (flags & OEP_PURE_SAME)
3058 cef &= ECF_CONST | ECF_PURE;
3059 else
3060 cef &= ECF_CONST;
3061 if (!cef)
3062 return 0;
3065 /* Now see if all the arguments are the same. */
3067 const_call_expr_arg_iterator iter0, iter1;
3068 const_tree a0, a1;
3069 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3070 a1 = first_const_call_expr_arg (arg1, &iter1);
3071 a0 && a1;
3072 a0 = next_const_call_expr_arg (&iter0),
3073 a1 = next_const_call_expr_arg (&iter1))
3074 if (! operand_equal_p (a0, a1, flags))
3075 return 0;
3077 /* If we get here and both argument lists are exhausted
3078 then the CALL_EXPRs are equal. */
3079 return ! (a0 || a1);
3081 default:
3082 return 0;
3085 case tcc_declaration:
3086 /* Consider __builtin_sqrt equal to sqrt. */
3087 return (TREE_CODE (arg0) == FUNCTION_DECL
3088 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3089 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3090 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3092 default:
3093 return 0;
3096 #undef OP_SAME
3097 #undef OP_SAME_WITH_NULL
3100 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3101 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3103 When in doubt, return 0. */
3105 static int
3106 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3108 int unsignedp1, unsignedpo;
3109 tree primarg0, primarg1, primother;
3110 unsigned int correct_width;
3112 if (operand_equal_p (arg0, arg1, 0))
3113 return 1;
3115 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3116 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3117 return 0;
3119 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3120 and see if the inner values are the same. This removes any
3121 signedness comparison, which doesn't matter here. */
3122 primarg0 = arg0, primarg1 = arg1;
3123 STRIP_NOPS (primarg0);
3124 STRIP_NOPS (primarg1);
3125 if (operand_equal_p (primarg0, primarg1, 0))
3126 return 1;
3128 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3129 actual comparison operand, ARG0.
3131 First throw away any conversions to wider types
3132 already present in the operands. */
3134 primarg1 = get_narrower (arg1, &unsignedp1);
3135 primother = get_narrower (other, &unsignedpo);
3137 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3138 if (unsignedp1 == unsignedpo
3139 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3140 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3142 tree type = TREE_TYPE (arg0);
3144 /* Make sure shorter operand is extended the right way
3145 to match the longer operand. */
3146 primarg1 = fold_convert (signed_or_unsigned_type_for
3147 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3149 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3150 return 1;
3153 return 0;
3156 /* See if ARG is an expression that is either a comparison or is performing
3157 arithmetic on comparisons. The comparisons must only be comparing
3158 two different values, which will be stored in *CVAL1 and *CVAL2; if
3159 they are nonzero it means that some operands have already been found.
3160 No variables may be used anywhere else in the expression except in the
3161 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3162 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3164 If this is true, return 1. Otherwise, return zero. */
3166 static int
3167 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3169 enum tree_code code = TREE_CODE (arg);
3170 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3172 /* We can handle some of the tcc_expression cases here. */
3173 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3174 tclass = tcc_unary;
3175 else if (tclass == tcc_expression
3176 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3177 || code == COMPOUND_EXPR))
3178 tclass = tcc_binary;
3180 else if (tclass == tcc_expression && code == SAVE_EXPR
3181 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3183 /* If we've already found a CVAL1 or CVAL2, this expression is
3184 two complex to handle. */
3185 if (*cval1 || *cval2)
3186 return 0;
3188 tclass = tcc_unary;
3189 *save_p = 1;
3192 switch (tclass)
3194 case tcc_unary:
3195 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3197 case tcc_binary:
3198 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3199 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3200 cval1, cval2, save_p));
3202 case tcc_constant:
3203 return 1;
3205 case tcc_expression:
3206 if (code == COND_EXPR)
3207 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3208 cval1, cval2, save_p)
3209 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3210 cval1, cval2, save_p)
3211 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3212 cval1, cval2, save_p));
3213 return 0;
3215 case tcc_comparison:
3216 /* First see if we can handle the first operand, then the second. For
3217 the second operand, we know *CVAL1 can't be zero. It must be that
3218 one side of the comparison is each of the values; test for the
3219 case where this isn't true by failing if the two operands
3220 are the same. */
3222 if (operand_equal_p (TREE_OPERAND (arg, 0),
3223 TREE_OPERAND (arg, 1), 0))
3224 return 0;
3226 if (*cval1 == 0)
3227 *cval1 = TREE_OPERAND (arg, 0);
3228 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3230 else if (*cval2 == 0)
3231 *cval2 = TREE_OPERAND (arg, 0);
3232 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3234 else
3235 return 0;
3237 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3239 else if (*cval2 == 0)
3240 *cval2 = TREE_OPERAND (arg, 1);
3241 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3243 else
3244 return 0;
3246 return 1;
3248 default:
3249 return 0;
3253 /* ARG is a tree that is known to contain just arithmetic operations and
3254 comparisons. Evaluate the operations in the tree substituting NEW0 for
3255 any occurrence of OLD0 as an operand of a comparison and likewise for
3256 NEW1 and OLD1. */
3258 static tree
3259 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3260 tree old1, tree new1)
3262 tree type = TREE_TYPE (arg);
3263 enum tree_code code = TREE_CODE (arg);
3264 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3266 /* We can handle some of the tcc_expression cases here. */
3267 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3268 tclass = tcc_unary;
3269 else if (tclass == tcc_expression
3270 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3271 tclass = tcc_binary;
3273 switch (tclass)
3275 case tcc_unary:
3276 return fold_build1_loc (loc, code, type,
3277 eval_subst (loc, TREE_OPERAND (arg, 0),
3278 old0, new0, old1, new1));
3280 case tcc_binary:
3281 return fold_build2_loc (loc, code, type,
3282 eval_subst (loc, TREE_OPERAND (arg, 0),
3283 old0, new0, old1, new1),
3284 eval_subst (loc, TREE_OPERAND (arg, 1),
3285 old0, new0, old1, new1));
3287 case tcc_expression:
3288 switch (code)
3290 case SAVE_EXPR:
3291 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3292 old1, new1);
3294 case COMPOUND_EXPR:
3295 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3296 old1, new1);
3298 case COND_EXPR:
3299 return fold_build3_loc (loc, code, type,
3300 eval_subst (loc, TREE_OPERAND (arg, 0),
3301 old0, new0, old1, new1),
3302 eval_subst (loc, TREE_OPERAND (arg, 1),
3303 old0, new0, old1, new1),
3304 eval_subst (loc, TREE_OPERAND (arg, 2),
3305 old0, new0, old1, new1));
3306 default:
3307 break;
3309 /* Fall through - ??? */
3311 case tcc_comparison:
3313 tree arg0 = TREE_OPERAND (arg, 0);
3314 tree arg1 = TREE_OPERAND (arg, 1);
3316 /* We need to check both for exact equality and tree equality. The
3317 former will be true if the operand has a side-effect. In that
3318 case, we know the operand occurred exactly once. */
3320 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3321 arg0 = new0;
3322 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3323 arg0 = new1;
3325 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3326 arg1 = new0;
3327 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3328 arg1 = new1;
3330 return fold_build2_loc (loc, code, type, arg0, arg1);
3333 default:
3334 return arg;
3338 /* Return a tree for the case when the result of an expression is RESULT
3339 converted to TYPE and OMITTED was previously an operand of the expression
3340 but is now not needed (e.g., we folded OMITTED * 0).
3342 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3343 the conversion of RESULT to TYPE. */
3345 tree
3346 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3348 tree t = fold_convert_loc (loc, type, result);
3350 /* If the resulting operand is an empty statement, just return the omitted
3351 statement casted to void. */
3352 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3353 return build1_loc (loc, NOP_EXPR, void_type_node,
3354 fold_ignored_result (omitted));
3356 if (TREE_SIDE_EFFECTS (omitted))
3357 return build2_loc (loc, COMPOUND_EXPR, type,
3358 fold_ignored_result (omitted), t);
3360 return non_lvalue_loc (loc, t);
3363 /* Return a tree for the case when the result of an expression is RESULT
3364 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3365 of the expression but are now not needed.
3367 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3368 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3369 evaluated before OMITTED2. Otherwise, if neither has side effects,
3370 just do the conversion of RESULT to TYPE. */
3372 tree
3373 omit_two_operands_loc (location_t loc, tree type, tree result,
3374 tree omitted1, tree omitted2)
3376 tree t = fold_convert_loc (loc, type, result);
3378 if (TREE_SIDE_EFFECTS (omitted2))
3379 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3380 if (TREE_SIDE_EFFECTS (omitted1))
3381 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3383 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3387 /* Return a simplified tree node for the truth-negation of ARG. This
3388 never alters ARG itself. We assume that ARG is an operation that
3389 returns a truth value (0 or 1).
3391 FIXME: one would think we would fold the result, but it causes
3392 problems with the dominator optimizer. */
3394 static tree
3395 fold_truth_not_expr (location_t loc, tree arg)
3397 tree type = TREE_TYPE (arg);
3398 enum tree_code code = TREE_CODE (arg);
3399 location_t loc1, loc2;
3401 /* If this is a comparison, we can simply invert it, except for
3402 floating-point non-equality comparisons, in which case we just
3403 enclose a TRUTH_NOT_EXPR around what we have. */
3405 if (TREE_CODE_CLASS (code) == tcc_comparison)
3407 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3408 if (FLOAT_TYPE_P (op_type)
3409 && flag_trapping_math
3410 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3411 && code != NE_EXPR && code != EQ_EXPR)
3412 return NULL_TREE;
3414 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3415 if (code == ERROR_MARK)
3416 return NULL_TREE;
3418 return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3419 TREE_OPERAND (arg, 1));
3422 switch (code)
3424 case INTEGER_CST:
3425 return constant_boolean_node (integer_zerop (arg), type);
3427 case TRUTH_AND_EXPR:
3428 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3429 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3430 return build2_loc (loc, TRUTH_OR_EXPR, type,
3431 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3432 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3434 case TRUTH_OR_EXPR:
3435 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3436 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3437 return build2_loc (loc, TRUTH_AND_EXPR, type,
3438 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3439 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3441 case TRUTH_XOR_EXPR:
3442 /* Here we can invert either operand. We invert the first operand
3443 unless the second operand is a TRUTH_NOT_EXPR in which case our
3444 result is the XOR of the first operand with the inside of the
3445 negation of the second operand. */
3447 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3448 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3449 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3450 else
3451 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3452 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3453 TREE_OPERAND (arg, 1));
3455 case TRUTH_ANDIF_EXPR:
3456 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3457 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3458 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3459 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3460 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3462 case TRUTH_ORIF_EXPR:
3463 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3464 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3465 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3466 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3467 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3469 case TRUTH_NOT_EXPR:
3470 return TREE_OPERAND (arg, 0);
3472 case COND_EXPR:
3474 tree arg1 = TREE_OPERAND (arg, 1);
3475 tree arg2 = TREE_OPERAND (arg, 2);
3477 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3478 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3480 /* A COND_EXPR may have a throw as one operand, which
3481 then has void type. Just leave void operands
3482 as they are. */
3483 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3484 VOID_TYPE_P (TREE_TYPE (arg1))
3485 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3486 VOID_TYPE_P (TREE_TYPE (arg2))
3487 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3490 case COMPOUND_EXPR:
3491 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3492 return build2_loc (loc, COMPOUND_EXPR, type,
3493 TREE_OPERAND (arg, 0),
3494 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3496 case NON_LVALUE_EXPR:
3497 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3498 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3500 CASE_CONVERT:
3501 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3502 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3504 /* ... fall through ... */
3506 case FLOAT_EXPR:
3507 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3508 return build1_loc (loc, TREE_CODE (arg), type,
3509 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3511 case BIT_AND_EXPR:
3512 if (!integer_onep (TREE_OPERAND (arg, 1)))
3513 return NULL_TREE;
3514 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3516 case SAVE_EXPR:
3517 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3519 case CLEANUP_POINT_EXPR:
3520 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3521 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3522 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3524 default:
3525 return NULL_TREE;
3529 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3530 assume that ARG is an operation that returns a truth value (0 or 1
3531 for scalars, 0 or -1 for vectors). Return the folded expression if
3532 folding is successful. Otherwise, return NULL_TREE. */
3534 static tree
3535 fold_invert_truthvalue (location_t loc, tree arg)
3537 tree type = TREE_TYPE (arg);
3538 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3539 ? BIT_NOT_EXPR
3540 : TRUTH_NOT_EXPR,
3541 type, arg);
3544 /* Return a simplified tree node for the truth-negation of ARG. This
3545 never alters ARG itself. We assume that ARG is an operation that
3546 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3548 tree
3549 invert_truthvalue_loc (location_t loc, tree arg)
3551 if (TREE_CODE (arg) == ERROR_MARK)
3552 return arg;
3554 tree type = TREE_TYPE (arg);
3555 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3556 ? BIT_NOT_EXPR
3557 : TRUTH_NOT_EXPR,
3558 type, arg);
3561 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3562 operands are another bit-wise operation with a common input. If so,
3563 distribute the bit operations to save an operation and possibly two if
3564 constants are involved. For example, convert
3565 (A | B) & (A | C) into A | (B & C)
3566 Further simplification will occur if B and C are constants.
3568 If this optimization cannot be done, 0 will be returned. */
3570 static tree
3571 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3572 tree arg0, tree arg1)
3574 tree common;
3575 tree left, right;
3577 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3578 || TREE_CODE (arg0) == code
3579 || (TREE_CODE (arg0) != BIT_AND_EXPR
3580 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3581 return 0;
3583 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3585 common = TREE_OPERAND (arg0, 0);
3586 left = TREE_OPERAND (arg0, 1);
3587 right = TREE_OPERAND (arg1, 1);
3589 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3591 common = TREE_OPERAND (arg0, 0);
3592 left = TREE_OPERAND (arg0, 1);
3593 right = TREE_OPERAND (arg1, 0);
3595 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3597 common = TREE_OPERAND (arg0, 1);
3598 left = TREE_OPERAND (arg0, 0);
3599 right = TREE_OPERAND (arg1, 1);
3601 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3603 common = TREE_OPERAND (arg0, 1);
3604 left = TREE_OPERAND (arg0, 0);
3605 right = TREE_OPERAND (arg1, 0);
3607 else
3608 return 0;
3610 common = fold_convert_loc (loc, type, common);
3611 left = fold_convert_loc (loc, type, left);
3612 right = fold_convert_loc (loc, type, right);
3613 return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3614 fold_build2_loc (loc, code, type, left, right));
3617 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3618 with code CODE. This optimization is unsafe. */
3619 static tree
3620 distribute_real_division (location_t loc, enum tree_code code, tree type,
3621 tree arg0, tree arg1)
3623 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3624 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3626 /* (A / C) +- (B / C) -> (A +- B) / C. */
3627 if (mul0 == mul1
3628 && operand_equal_p (TREE_OPERAND (arg0, 1),
3629 TREE_OPERAND (arg1, 1), 0))
3630 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3631 fold_build2_loc (loc, code, type,
3632 TREE_OPERAND (arg0, 0),
3633 TREE_OPERAND (arg1, 0)),
3634 TREE_OPERAND (arg0, 1));
3636 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3637 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3638 TREE_OPERAND (arg1, 0), 0)
3639 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3640 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3642 REAL_VALUE_TYPE r0, r1;
3643 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3644 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3645 if (!mul0)
3646 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3647 if (!mul1)
3648 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3649 real_arithmetic (&r0, code, &r0, &r1);
3650 return fold_build2_loc (loc, MULT_EXPR, type,
3651 TREE_OPERAND (arg0, 0),
3652 build_real (type, r0));
3655 return NULL_TREE;
3658 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3659 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3661 static tree
3662 make_bit_field_ref (location_t loc, tree inner, tree type,
3663 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3665 tree result, bftype;
3667 if (bitpos == 0)
3669 tree size = TYPE_SIZE (TREE_TYPE (inner));
3670 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3671 || POINTER_TYPE_P (TREE_TYPE (inner)))
3672 && tree_fits_shwi_p (size)
3673 && tree_to_shwi (size) == bitsize)
3674 return fold_convert_loc (loc, type, inner);
3677 bftype = type;
3678 if (TYPE_PRECISION (bftype) != bitsize
3679 || TYPE_UNSIGNED (bftype) == !unsignedp)
3680 bftype = build_nonstandard_integer_type (bitsize, 0);
3682 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3683 size_int (bitsize), bitsize_int (bitpos));
3685 if (bftype != type)
3686 result = fold_convert_loc (loc, type, result);
3688 return result;
3691 /* Optimize a bit-field compare.
3693 There are two cases: First is a compare against a constant and the
3694 second is a comparison of two items where the fields are at the same
3695 bit position relative to the start of a chunk (byte, halfword, word)
3696 large enough to contain it. In these cases we can avoid the shift
3697 implicit in bitfield extractions.
3699 For constants, we emit a compare of the shifted constant with the
3700 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3701 compared. For two fields at the same position, we do the ANDs with the
3702 similar mask and compare the result of the ANDs.
3704 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3705 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3706 are the left and right operands of the comparison, respectively.
3708 If the optimization described above can be done, we return the resulting
3709 tree. Otherwise we return zero. */
3711 static tree
3712 optimize_bit_field_compare (location_t loc, enum tree_code code,
3713 tree compare_type, tree lhs, tree rhs)
3715 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3716 tree type = TREE_TYPE (lhs);
3717 tree unsigned_type;
3718 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3719 machine_mode lmode, rmode, nmode;
3720 int lunsignedp, runsignedp;
3721 int lvolatilep = 0, rvolatilep = 0;
3722 tree linner, rinner = NULL_TREE;
3723 tree mask;
3724 tree offset;
3726 /* Get all the information about the extractions being done. If the bit size
3727 if the same as the size of the underlying object, we aren't doing an
3728 extraction at all and so can do nothing. We also don't want to
3729 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3730 then will no longer be able to replace it. */
3731 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3732 &lunsignedp, &lvolatilep, false);
3733 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3734 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3735 return 0;
3737 if (!const_p)
3739 /* If this is not a constant, we can only do something if bit positions,
3740 sizes, and signedness are the same. */
3741 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3742 &runsignedp, &rvolatilep, false);
3744 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3745 || lunsignedp != runsignedp || offset != 0
3746 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3747 return 0;
3750 /* See if we can find a mode to refer to this field. We should be able to,
3751 but fail if we can't. */
3752 nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3753 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3754 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3755 TYPE_ALIGN (TREE_TYPE (rinner))),
3756 word_mode, false);
3757 if (nmode == VOIDmode)
3758 return 0;
3760 /* Set signed and unsigned types of the precision of this mode for the
3761 shifts below. */
3762 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3764 /* Compute the bit position and size for the new reference and our offset
3765 within it. If the new reference is the same size as the original, we
3766 won't optimize anything, so return zero. */
3767 nbitsize = GET_MODE_BITSIZE (nmode);
3768 nbitpos = lbitpos & ~ (nbitsize - 1);
3769 lbitpos -= nbitpos;
3770 if (nbitsize == lbitsize)
3771 return 0;
3773 if (BYTES_BIG_ENDIAN)
3774 lbitpos = nbitsize - lbitsize - lbitpos;
3776 /* Make the mask to be used against the extracted field. */
3777 mask = build_int_cst_type (unsigned_type, -1);
3778 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3779 mask = const_binop (RSHIFT_EXPR, mask,
3780 size_int (nbitsize - lbitsize - lbitpos));
3782 if (! const_p)
3783 /* If not comparing with constant, just rework the comparison
3784 and return. */
3785 return fold_build2_loc (loc, code, compare_type,
3786 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3787 make_bit_field_ref (loc, linner,
3788 unsigned_type,
3789 nbitsize, nbitpos,
3791 mask),
3792 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3793 make_bit_field_ref (loc, rinner,
3794 unsigned_type,
3795 nbitsize, nbitpos,
3797 mask));
3799 /* Otherwise, we are handling the constant case. See if the constant is too
3800 big for the field. Warn and return a tree of for 0 (false) if so. We do
3801 this not only for its own sake, but to avoid having to test for this
3802 error case below. If we didn't, we might generate wrong code.
3804 For unsigned fields, the constant shifted right by the field length should
3805 be all zero. For signed fields, the high-order bits should agree with
3806 the sign bit. */
3808 if (lunsignedp)
3810 if (wi::lrshift (rhs, lbitsize) != 0)
3812 warning (0, "comparison is always %d due to width of bit-field",
3813 code == NE_EXPR);
3814 return constant_boolean_node (code == NE_EXPR, compare_type);
3817 else
3819 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3820 if (tem != 0 && tem != -1)
3822 warning (0, "comparison is always %d due to width of bit-field",
3823 code == NE_EXPR);
3824 return constant_boolean_node (code == NE_EXPR, compare_type);
3828 /* Single-bit compares should always be against zero. */
3829 if (lbitsize == 1 && ! integer_zerop (rhs))
3831 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3832 rhs = build_int_cst (type, 0);
3835 /* Make a new bitfield reference, shift the constant over the
3836 appropriate number of bits and mask it with the computed mask
3837 (in case this was a signed field). If we changed it, make a new one. */
3838 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3840 rhs = const_binop (BIT_AND_EXPR,
3841 const_binop (LSHIFT_EXPR,
3842 fold_convert_loc (loc, unsigned_type, rhs),
3843 size_int (lbitpos)),
3844 mask);
3846 lhs = build2_loc (loc, code, compare_type,
3847 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3848 return lhs;
3851 /* Subroutine for fold_truth_andor_1: decode a field reference.
3853 If EXP is a comparison reference, we return the innermost reference.
3855 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3856 set to the starting bit number.
3858 If the innermost field can be completely contained in a mode-sized
3859 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3861 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3862 otherwise it is not changed.
3864 *PUNSIGNEDP is set to the signedness of the field.
3866 *PMASK is set to the mask used. This is either contained in a
3867 BIT_AND_EXPR or derived from the width of the field.
3869 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3871 Return 0 if this is not a component reference or is one that we can't
3872 do anything with. */
3874 static tree
3875 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3876 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
3877 int *punsignedp, int *pvolatilep,
3878 tree *pmask, tree *pand_mask)
3880 tree outer_type = 0;
3881 tree and_mask = 0;
3882 tree mask, inner, offset;
3883 tree unsigned_type;
3884 unsigned int precision;
3886 /* All the optimizations using this function assume integer fields.
3887 There are problems with FP fields since the type_for_size call
3888 below can fail for, e.g., XFmode. */
3889 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3890 return 0;
3892 /* We are interested in the bare arrangement of bits, so strip everything
3893 that doesn't affect the machine mode. However, record the type of the
3894 outermost expression if it may matter below. */
3895 if (CONVERT_EXPR_P (exp)
3896 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3897 outer_type = TREE_TYPE (exp);
3898 STRIP_NOPS (exp);
3900 if (TREE_CODE (exp) == BIT_AND_EXPR)
3902 and_mask = TREE_OPERAND (exp, 1);
3903 exp = TREE_OPERAND (exp, 0);
3904 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3905 if (TREE_CODE (and_mask) != INTEGER_CST)
3906 return 0;
3909 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3910 punsignedp, pvolatilep, false);
3911 if ((inner == exp && and_mask == 0)
3912 || *pbitsize < 0 || offset != 0
3913 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3914 return 0;
3916 /* If the number of bits in the reference is the same as the bitsize of
3917 the outer type, then the outer type gives the signedness. Otherwise
3918 (in case of a small bitfield) the signedness is unchanged. */
3919 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3920 *punsignedp = TYPE_UNSIGNED (outer_type);
3922 /* Compute the mask to access the bitfield. */
3923 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3924 precision = TYPE_PRECISION (unsigned_type);
3926 mask = build_int_cst_type (unsigned_type, -1);
3928 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3929 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3931 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3932 if (and_mask != 0)
3933 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3934 fold_convert_loc (loc, unsigned_type, and_mask), mask);
3936 *pmask = mask;
3937 *pand_mask = and_mask;
3938 return inner;
3941 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3942 bit positions and MASK is SIGNED. */
3944 static int
3945 all_ones_mask_p (const_tree mask, unsigned int size)
3947 tree type = TREE_TYPE (mask);
3948 unsigned int precision = TYPE_PRECISION (type);
3950 /* If this function returns true when the type of the mask is
3951 UNSIGNED, then there will be errors. In particular see
3952 gcc.c-torture/execute/990326-1.c. There does not appear to be
3953 any documentation paper trail as to why this is so. But the pre
3954 wide-int worked with that restriction and it has been preserved
3955 here. */
3956 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
3957 return false;
3959 return wi::mask (size, false, precision) == mask;
3962 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3963 represents the sign bit of EXP's type. If EXP represents a sign
3964 or zero extension, also test VAL against the unextended type.
3965 The return value is the (sub)expression whose sign bit is VAL,
3966 or NULL_TREE otherwise. */
3968 tree
3969 sign_bit_p (tree exp, const_tree val)
3971 int width;
3972 tree t;
3974 /* Tree EXP must have an integral type. */
3975 t = TREE_TYPE (exp);
3976 if (! INTEGRAL_TYPE_P (t))
3977 return NULL_TREE;
3979 /* Tree VAL must be an integer constant. */
3980 if (TREE_CODE (val) != INTEGER_CST
3981 || TREE_OVERFLOW (val))
3982 return NULL_TREE;
3984 width = TYPE_PRECISION (t);
3985 if (wi::only_sign_bit_p (val, width))
3986 return exp;
3988 /* Handle extension from a narrower type. */
3989 if (TREE_CODE (exp) == NOP_EXPR
3990 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3991 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3993 return NULL_TREE;
3996 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
3997 to be evaluated unconditionally. */
3999 static int
4000 simple_operand_p (const_tree exp)
4002 /* Strip any conversions that don't change the machine mode. */
4003 STRIP_NOPS (exp);
4005 return (CONSTANT_CLASS_P (exp)
4006 || TREE_CODE (exp) == SSA_NAME
4007 || (DECL_P (exp)
4008 && ! TREE_ADDRESSABLE (exp)
4009 && ! TREE_THIS_VOLATILE (exp)
4010 && ! DECL_NONLOCAL (exp)
4011 /* Don't regard global variables as simple. They may be
4012 allocated in ways unknown to the compiler (shared memory,
4013 #pragma weak, etc). */
4014 && ! TREE_PUBLIC (exp)
4015 && ! DECL_EXTERNAL (exp)
4016 /* Weakrefs are not safe to be read, since they can be NULL.
4017 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4018 have DECL_WEAK flag set. */
4019 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4020 /* Loading a static variable is unduly expensive, but global
4021 registers aren't expensive. */
4022 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4025 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4026 to be evaluated unconditionally.
4027 I addition to simple_operand_p, we assume that comparisons, conversions,
4028 and logic-not operations are simple, if their operands are simple, too. */
4030 static bool
4031 simple_operand_p_2 (tree exp)
4033 enum tree_code code;
4035 if (TREE_SIDE_EFFECTS (exp)
4036 || tree_could_trap_p (exp))
4037 return false;
4039 while (CONVERT_EXPR_P (exp))
4040 exp = TREE_OPERAND (exp, 0);
4042 code = TREE_CODE (exp);
4044 if (TREE_CODE_CLASS (code) == tcc_comparison)
4045 return (simple_operand_p (TREE_OPERAND (exp, 0))
4046 && simple_operand_p (TREE_OPERAND (exp, 1)));
4048 if (code == TRUTH_NOT_EXPR)
4049 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4051 return simple_operand_p (exp);
4055 /* The following functions are subroutines to fold_range_test and allow it to
4056 try to change a logical combination of comparisons into a range test.
4058 For example, both
4059 X == 2 || X == 3 || X == 4 || X == 5
4061 X >= 2 && X <= 5
4062 are converted to
4063 (unsigned) (X - 2) <= 3
4065 We describe each set of comparisons as being either inside or outside
4066 a range, using a variable named like IN_P, and then describe the
4067 range with a lower and upper bound. If one of the bounds is omitted,
4068 it represents either the highest or lowest value of the type.
4070 In the comments below, we represent a range by two numbers in brackets
4071 preceded by a "+" to designate being inside that range, or a "-" to
4072 designate being outside that range, so the condition can be inverted by
4073 flipping the prefix. An omitted bound is represented by a "-". For
4074 example, "- [-, 10]" means being outside the range starting at the lowest
4075 possible value and ending at 10, in other words, being greater than 10.
4076 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4077 always false.
4079 We set up things so that the missing bounds are handled in a consistent
4080 manner so neither a missing bound nor "true" and "false" need to be
4081 handled using a special case. */
4083 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4084 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4085 and UPPER1_P are nonzero if the respective argument is an upper bound
4086 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4087 must be specified for a comparison. ARG1 will be converted to ARG0's
4088 type if both are specified. */
4090 static tree
4091 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4092 tree arg1, int upper1_p)
4094 tree tem;
4095 int result;
4096 int sgn0, sgn1;
4098 /* If neither arg represents infinity, do the normal operation.
4099 Else, if not a comparison, return infinity. Else handle the special
4100 comparison rules. Note that most of the cases below won't occur, but
4101 are handled for consistency. */
4103 if (arg0 != 0 && arg1 != 0)
4105 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4106 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4107 STRIP_NOPS (tem);
4108 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4111 if (TREE_CODE_CLASS (code) != tcc_comparison)
4112 return 0;
4114 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4115 for neither. In real maths, we cannot assume open ended ranges are
4116 the same. But, this is computer arithmetic, where numbers are finite.
4117 We can therefore make the transformation of any unbounded range with
4118 the value Z, Z being greater than any representable number. This permits
4119 us to treat unbounded ranges as equal. */
4120 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4121 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4122 switch (code)
4124 case EQ_EXPR:
4125 result = sgn0 == sgn1;
4126 break;
4127 case NE_EXPR:
4128 result = sgn0 != sgn1;
4129 break;
4130 case LT_EXPR:
4131 result = sgn0 < sgn1;
4132 break;
4133 case LE_EXPR:
4134 result = sgn0 <= sgn1;
4135 break;
4136 case GT_EXPR:
4137 result = sgn0 > sgn1;
4138 break;
4139 case GE_EXPR:
4140 result = sgn0 >= sgn1;
4141 break;
4142 default:
4143 gcc_unreachable ();
4146 return constant_boolean_node (result, type);
4149 /* Helper routine for make_range. Perform one step for it, return
4150 new expression if the loop should continue or NULL_TREE if it should
4151 stop. */
4153 tree
4154 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4155 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4156 bool *strict_overflow_p)
4158 tree arg0_type = TREE_TYPE (arg0);
4159 tree n_low, n_high, low = *p_low, high = *p_high;
4160 int in_p = *p_in_p, n_in_p;
4162 switch (code)
4164 case TRUTH_NOT_EXPR:
4165 /* We can only do something if the range is testing for zero. */
4166 if (low == NULL_TREE || high == NULL_TREE
4167 || ! integer_zerop (low) || ! integer_zerop (high))
4168 return NULL_TREE;
4169 *p_in_p = ! in_p;
4170 return arg0;
4172 case EQ_EXPR: case NE_EXPR:
4173 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4174 /* We can only do something if the range is testing for zero
4175 and if the second operand is an integer constant. Note that
4176 saying something is "in" the range we make is done by
4177 complementing IN_P since it will set in the initial case of
4178 being not equal to zero; "out" is leaving it alone. */
4179 if (low == NULL_TREE || high == NULL_TREE
4180 || ! integer_zerop (low) || ! integer_zerop (high)
4181 || TREE_CODE (arg1) != INTEGER_CST)
4182 return NULL_TREE;
4184 switch (code)
4186 case NE_EXPR: /* - [c, c] */
4187 low = high = arg1;
4188 break;
4189 case EQ_EXPR: /* + [c, c] */
4190 in_p = ! in_p, low = high = arg1;
4191 break;
4192 case GT_EXPR: /* - [-, c] */
4193 low = 0, high = arg1;
4194 break;
4195 case GE_EXPR: /* + [c, -] */
4196 in_p = ! in_p, low = arg1, high = 0;
4197 break;
4198 case LT_EXPR: /* - [c, -] */
4199 low = arg1, high = 0;
4200 break;
4201 case LE_EXPR: /* + [-, c] */
4202 in_p = ! in_p, low = 0, high = arg1;
4203 break;
4204 default:
4205 gcc_unreachable ();
4208 /* If this is an unsigned comparison, we also know that EXP is
4209 greater than or equal to zero. We base the range tests we make
4210 on that fact, so we record it here so we can parse existing
4211 range tests. We test arg0_type since often the return type
4212 of, e.g. EQ_EXPR, is boolean. */
4213 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4215 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4216 in_p, low, high, 1,
4217 build_int_cst (arg0_type, 0),
4218 NULL_TREE))
4219 return NULL_TREE;
4221 in_p = n_in_p, low = n_low, high = n_high;
4223 /* If the high bound is missing, but we have a nonzero low
4224 bound, reverse the range so it goes from zero to the low bound
4225 minus 1. */
4226 if (high == 0 && low && ! integer_zerop (low))
4228 in_p = ! in_p;
4229 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4230 build_int_cst (TREE_TYPE (low), 1), 0);
4231 low = build_int_cst (arg0_type, 0);
4235 *p_low = low;
4236 *p_high = high;
4237 *p_in_p = in_p;
4238 return arg0;
4240 case NEGATE_EXPR:
4241 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4242 low and high are non-NULL, then normalize will DTRT. */
4243 if (!TYPE_UNSIGNED (arg0_type)
4244 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4246 if (low == NULL_TREE)
4247 low = TYPE_MIN_VALUE (arg0_type);
4248 if (high == NULL_TREE)
4249 high = TYPE_MAX_VALUE (arg0_type);
4252 /* (-x) IN [a,b] -> x in [-b, -a] */
4253 n_low = range_binop (MINUS_EXPR, exp_type,
4254 build_int_cst (exp_type, 0),
4255 0, high, 1);
4256 n_high = range_binop (MINUS_EXPR, exp_type,
4257 build_int_cst (exp_type, 0),
4258 0, low, 0);
4259 if (n_high != 0 && TREE_OVERFLOW (n_high))
4260 return NULL_TREE;
4261 goto normalize;
4263 case BIT_NOT_EXPR:
4264 /* ~ X -> -X - 1 */
4265 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4266 build_int_cst (exp_type, 1));
4268 case PLUS_EXPR:
4269 case MINUS_EXPR:
4270 if (TREE_CODE (arg1) != INTEGER_CST)
4271 return NULL_TREE;
4273 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4274 move a constant to the other side. */
4275 if (!TYPE_UNSIGNED (arg0_type)
4276 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4277 return NULL_TREE;
4279 /* If EXP is signed, any overflow in the computation is undefined,
4280 so we don't worry about it so long as our computations on
4281 the bounds don't overflow. For unsigned, overflow is defined
4282 and this is exactly the right thing. */
4283 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4284 arg0_type, low, 0, arg1, 0);
4285 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4286 arg0_type, high, 1, arg1, 0);
4287 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4288 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4289 return NULL_TREE;
4291 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4292 *strict_overflow_p = true;
4294 normalize:
4295 /* Check for an unsigned range which has wrapped around the maximum
4296 value thus making n_high < n_low, and normalize it. */
4297 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4299 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4300 build_int_cst (TREE_TYPE (n_high), 1), 0);
4301 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4302 build_int_cst (TREE_TYPE (n_low), 1), 0);
4304 /* If the range is of the form +/- [ x+1, x ], we won't
4305 be able to normalize it. But then, it represents the
4306 whole range or the empty set, so make it
4307 +/- [ -, - ]. */
4308 if (tree_int_cst_equal (n_low, low)
4309 && tree_int_cst_equal (n_high, high))
4310 low = high = 0;
4311 else
4312 in_p = ! in_p;
4314 else
4315 low = n_low, high = n_high;
4317 *p_low = low;
4318 *p_high = high;
4319 *p_in_p = in_p;
4320 return arg0;
4322 CASE_CONVERT:
4323 case NON_LVALUE_EXPR:
4324 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4325 return NULL_TREE;
4327 if (! INTEGRAL_TYPE_P (arg0_type)
4328 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4329 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4330 return NULL_TREE;
4332 n_low = low, n_high = high;
4334 if (n_low != 0)
4335 n_low = fold_convert_loc (loc, arg0_type, n_low);
4337 if (n_high != 0)
4338 n_high = fold_convert_loc (loc, arg0_type, n_high);
4340 /* If we're converting arg0 from an unsigned type, to exp,
4341 a signed type, we will be doing the comparison as unsigned.
4342 The tests above have already verified that LOW and HIGH
4343 are both positive.
4345 So we have to ensure that we will handle large unsigned
4346 values the same way that the current signed bounds treat
4347 negative values. */
4349 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4351 tree high_positive;
4352 tree equiv_type;
4353 /* For fixed-point modes, we need to pass the saturating flag
4354 as the 2nd parameter. */
4355 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4356 equiv_type
4357 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4358 TYPE_SATURATING (arg0_type));
4359 else
4360 equiv_type
4361 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4363 /* A range without an upper bound is, naturally, unbounded.
4364 Since convert would have cropped a very large value, use
4365 the max value for the destination type. */
4366 high_positive
4367 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4368 : TYPE_MAX_VALUE (arg0_type);
4370 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4371 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4372 fold_convert_loc (loc, arg0_type,
4373 high_positive),
4374 build_int_cst (arg0_type, 1));
4376 /* If the low bound is specified, "and" the range with the
4377 range for which the original unsigned value will be
4378 positive. */
4379 if (low != 0)
4381 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4382 1, fold_convert_loc (loc, arg0_type,
4383 integer_zero_node),
4384 high_positive))
4385 return NULL_TREE;
4387 in_p = (n_in_p == in_p);
4389 else
4391 /* Otherwise, "or" the range with the range of the input
4392 that will be interpreted as negative. */
4393 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4394 1, fold_convert_loc (loc, arg0_type,
4395 integer_zero_node),
4396 high_positive))
4397 return NULL_TREE;
4399 in_p = (in_p != n_in_p);
4403 *p_low = n_low;
4404 *p_high = n_high;
4405 *p_in_p = in_p;
4406 return arg0;
4408 default:
4409 return NULL_TREE;
4413 /* Given EXP, a logical expression, set the range it is testing into
4414 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4415 actually being tested. *PLOW and *PHIGH will be made of the same
4416 type as the returned expression. If EXP is not a comparison, we
4417 will most likely not be returning a useful value and range. Set
4418 *STRICT_OVERFLOW_P to true if the return value is only valid
4419 because signed overflow is undefined; otherwise, do not change
4420 *STRICT_OVERFLOW_P. */
4422 tree
4423 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4424 bool *strict_overflow_p)
4426 enum tree_code code;
4427 tree arg0, arg1 = NULL_TREE;
4428 tree exp_type, nexp;
4429 int in_p;
4430 tree low, high;
4431 location_t loc = EXPR_LOCATION (exp);
4433 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4434 and see if we can refine the range. Some of the cases below may not
4435 happen, but it doesn't seem worth worrying about this. We "continue"
4436 the outer loop when we've changed something; otherwise we "break"
4437 the switch, which will "break" the while. */
4439 in_p = 0;
4440 low = high = build_int_cst (TREE_TYPE (exp), 0);
4442 while (1)
4444 code = TREE_CODE (exp);
4445 exp_type = TREE_TYPE (exp);
4446 arg0 = NULL_TREE;
4448 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4450 if (TREE_OPERAND_LENGTH (exp) > 0)
4451 arg0 = TREE_OPERAND (exp, 0);
4452 if (TREE_CODE_CLASS (code) == tcc_binary
4453 || TREE_CODE_CLASS (code) == tcc_comparison
4454 || (TREE_CODE_CLASS (code) == tcc_expression
4455 && TREE_OPERAND_LENGTH (exp) > 1))
4456 arg1 = TREE_OPERAND (exp, 1);
4458 if (arg0 == NULL_TREE)
4459 break;
4461 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4462 &high, &in_p, strict_overflow_p);
4463 if (nexp == NULL_TREE)
4464 break;
4465 exp = nexp;
4468 /* If EXP is a constant, we can evaluate whether this is true or false. */
4469 if (TREE_CODE (exp) == INTEGER_CST)
4471 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4472 exp, 0, low, 0))
4473 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4474 exp, 1, high, 1)));
4475 low = high = 0;
4476 exp = 0;
4479 *pin_p = in_p, *plow = low, *phigh = high;
4480 return exp;
4483 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4484 type, TYPE, return an expression to test if EXP is in (or out of, depending
4485 on IN_P) the range. Return 0 if the test couldn't be created. */
4487 tree
4488 build_range_check (location_t loc, tree type, tree exp, int in_p,
4489 tree low, tree high)
4491 tree etype = TREE_TYPE (exp), value;
4493 #ifdef HAVE_canonicalize_funcptr_for_compare
4494 /* Disable this optimization for function pointer expressions
4495 on targets that require function pointer canonicalization. */
4496 if (HAVE_canonicalize_funcptr_for_compare
4497 && TREE_CODE (etype) == POINTER_TYPE
4498 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4499 return NULL_TREE;
4500 #endif
4502 if (! in_p)
4504 value = build_range_check (loc, type, exp, 1, low, high);
4505 if (value != 0)
4506 return invert_truthvalue_loc (loc, value);
4508 return 0;
4511 if (low == 0 && high == 0)
4512 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4514 if (low == 0)
4515 return fold_build2_loc (loc, LE_EXPR, type, exp,
4516 fold_convert_loc (loc, etype, high));
4518 if (high == 0)
4519 return fold_build2_loc (loc, GE_EXPR, type, exp,
4520 fold_convert_loc (loc, etype, low));
4522 if (operand_equal_p (low, high, 0))
4523 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4524 fold_convert_loc (loc, etype, low));
4526 if (integer_zerop (low))
4528 if (! TYPE_UNSIGNED (etype))
4530 etype = unsigned_type_for (etype);
4531 high = fold_convert_loc (loc, etype, high);
4532 exp = fold_convert_loc (loc, etype, exp);
4534 return build_range_check (loc, type, exp, 1, 0, high);
4537 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4538 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4540 int prec = TYPE_PRECISION (etype);
4542 if (wi::mask (prec - 1, false, prec) == high)
4544 if (TYPE_UNSIGNED (etype))
4546 tree signed_etype = signed_type_for (etype);
4547 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4548 etype
4549 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4550 else
4551 etype = signed_etype;
4552 exp = fold_convert_loc (loc, etype, exp);
4554 return fold_build2_loc (loc, GT_EXPR, type, exp,
4555 build_int_cst (etype, 0));
4559 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4560 This requires wrap-around arithmetics for the type of the expression.
4561 First make sure that arithmetics in this type is valid, then make sure
4562 that it wraps around. */
4563 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4564 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4565 TYPE_UNSIGNED (etype));
4567 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4569 tree utype, minv, maxv;
4571 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4572 for the type in question, as we rely on this here. */
4573 utype = unsigned_type_for (etype);
4574 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4575 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4576 build_int_cst (TREE_TYPE (maxv), 1), 1);
4577 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4579 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4580 minv, 1, maxv, 1)))
4581 etype = utype;
4582 else
4583 return 0;
4586 high = fold_convert_loc (loc, etype, high);
4587 low = fold_convert_loc (loc, etype, low);
4588 exp = fold_convert_loc (loc, etype, exp);
4590 value = const_binop (MINUS_EXPR, high, low);
4593 if (POINTER_TYPE_P (etype))
4595 if (value != 0 && !TREE_OVERFLOW (value))
4597 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4598 return build_range_check (loc, type,
4599 fold_build_pointer_plus_loc (loc, exp, low),
4600 1, build_int_cst (etype, 0), value);
4602 return 0;
4605 if (value != 0 && !TREE_OVERFLOW (value))
4606 return build_range_check (loc, type,
4607 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4608 1, build_int_cst (etype, 0), value);
4610 return 0;
4613 /* Return the predecessor of VAL in its type, handling the infinite case. */
4615 static tree
4616 range_predecessor (tree val)
4618 tree type = TREE_TYPE (val);
4620 if (INTEGRAL_TYPE_P (type)
4621 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4622 return 0;
4623 else
4624 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4625 build_int_cst (TREE_TYPE (val), 1), 0);
4628 /* Return the successor of VAL in its type, handling the infinite case. */
4630 static tree
4631 range_successor (tree val)
4633 tree type = TREE_TYPE (val);
4635 if (INTEGRAL_TYPE_P (type)
4636 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4637 return 0;
4638 else
4639 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4640 build_int_cst (TREE_TYPE (val), 1), 0);
4643 /* Given two ranges, see if we can merge them into one. Return 1 if we
4644 can, 0 if we can't. Set the output range into the specified parameters. */
4646 bool
4647 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4648 tree high0, int in1_p, tree low1, tree high1)
4650 int no_overlap;
4651 int subset;
4652 int temp;
4653 tree tem;
4654 int in_p;
4655 tree low, high;
4656 int lowequal = ((low0 == 0 && low1 == 0)
4657 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4658 low0, 0, low1, 0)));
4659 int highequal = ((high0 == 0 && high1 == 0)
4660 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4661 high0, 1, high1, 1)));
4663 /* Make range 0 be the range that starts first, or ends last if they
4664 start at the same value. Swap them if it isn't. */
4665 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4666 low0, 0, low1, 0))
4667 || (lowequal
4668 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4669 high1, 1, high0, 1))))
4671 temp = in0_p, in0_p = in1_p, in1_p = temp;
4672 tem = low0, low0 = low1, low1 = tem;
4673 tem = high0, high0 = high1, high1 = tem;
4676 /* Now flag two cases, whether the ranges are disjoint or whether the
4677 second range is totally subsumed in the first. Note that the tests
4678 below are simplified by the ones above. */
4679 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4680 high0, 1, low1, 0));
4681 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4682 high1, 1, high0, 1));
4684 /* We now have four cases, depending on whether we are including or
4685 excluding the two ranges. */
4686 if (in0_p && in1_p)
4688 /* If they don't overlap, the result is false. If the second range
4689 is a subset it is the result. Otherwise, the range is from the start
4690 of the second to the end of the first. */
4691 if (no_overlap)
4692 in_p = 0, low = high = 0;
4693 else if (subset)
4694 in_p = 1, low = low1, high = high1;
4695 else
4696 in_p = 1, low = low1, high = high0;
4699 else if (in0_p && ! in1_p)
4701 /* If they don't overlap, the result is the first range. If they are
4702 equal, the result is false. If the second range is a subset of the
4703 first, and the ranges begin at the same place, we go from just after
4704 the end of the second range to the end of the first. If the second
4705 range is not a subset of the first, or if it is a subset and both
4706 ranges end at the same place, the range starts at the start of the
4707 first range and ends just before the second range.
4708 Otherwise, we can't describe this as a single range. */
4709 if (no_overlap)
4710 in_p = 1, low = low0, high = high0;
4711 else if (lowequal && highequal)
4712 in_p = 0, low = high = 0;
4713 else if (subset && lowequal)
4715 low = range_successor (high1);
4716 high = high0;
4717 in_p = 1;
4718 if (low == 0)
4720 /* We are in the weird situation where high0 > high1 but
4721 high1 has no successor. Punt. */
4722 return 0;
4725 else if (! subset || highequal)
4727 low = low0;
4728 high = range_predecessor (low1);
4729 in_p = 1;
4730 if (high == 0)
4732 /* low0 < low1 but low1 has no predecessor. Punt. */
4733 return 0;
4736 else
4737 return 0;
4740 else if (! in0_p && in1_p)
4742 /* If they don't overlap, the result is the second range. If the second
4743 is a subset of the first, the result is false. Otherwise,
4744 the range starts just after the first range and ends at the
4745 end of the second. */
4746 if (no_overlap)
4747 in_p = 1, low = low1, high = high1;
4748 else if (subset || highequal)
4749 in_p = 0, low = high = 0;
4750 else
4752 low = range_successor (high0);
4753 high = high1;
4754 in_p = 1;
4755 if (low == 0)
4757 /* high1 > high0 but high0 has no successor. Punt. */
4758 return 0;
4763 else
4765 /* The case where we are excluding both ranges. Here the complex case
4766 is if they don't overlap. In that case, the only time we have a
4767 range is if they are adjacent. If the second is a subset of the
4768 first, the result is the first. Otherwise, the range to exclude
4769 starts at the beginning of the first range and ends at the end of the
4770 second. */
4771 if (no_overlap)
4773 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4774 range_successor (high0),
4775 1, low1, 0)))
4776 in_p = 0, low = low0, high = high1;
4777 else
4779 /* Canonicalize - [min, x] into - [-, x]. */
4780 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4781 switch (TREE_CODE (TREE_TYPE (low0)))
4783 case ENUMERAL_TYPE:
4784 if (TYPE_PRECISION (TREE_TYPE (low0))
4785 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4786 break;
4787 /* FALLTHROUGH */
4788 case INTEGER_TYPE:
4789 if (tree_int_cst_equal (low0,
4790 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4791 low0 = 0;
4792 break;
4793 case POINTER_TYPE:
4794 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4795 && integer_zerop (low0))
4796 low0 = 0;
4797 break;
4798 default:
4799 break;
4802 /* Canonicalize - [x, max] into - [x, -]. */
4803 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4804 switch (TREE_CODE (TREE_TYPE (high1)))
4806 case ENUMERAL_TYPE:
4807 if (TYPE_PRECISION (TREE_TYPE (high1))
4808 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4809 break;
4810 /* FALLTHROUGH */
4811 case INTEGER_TYPE:
4812 if (tree_int_cst_equal (high1,
4813 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4814 high1 = 0;
4815 break;
4816 case POINTER_TYPE:
4817 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4818 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4819 high1, 1,
4820 build_int_cst (TREE_TYPE (high1), 1),
4821 1)))
4822 high1 = 0;
4823 break;
4824 default:
4825 break;
4828 /* The ranges might be also adjacent between the maximum and
4829 minimum values of the given type. For
4830 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4831 return + [x + 1, y - 1]. */
4832 if (low0 == 0 && high1 == 0)
4834 low = range_successor (high0);
4835 high = range_predecessor (low1);
4836 if (low == 0 || high == 0)
4837 return 0;
4839 in_p = 1;
4841 else
4842 return 0;
4845 else if (subset)
4846 in_p = 0, low = low0, high = high0;
4847 else
4848 in_p = 0, low = low0, high = high1;
4851 *pin_p = in_p, *plow = low, *phigh = high;
4852 return 1;
4856 /* Subroutine of fold, looking inside expressions of the form
4857 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4858 of the COND_EXPR. This function is being used also to optimize
4859 A op B ? C : A, by reversing the comparison first.
4861 Return a folded expression whose code is not a COND_EXPR
4862 anymore, or NULL_TREE if no folding opportunity is found. */
4864 static tree
4865 fold_cond_expr_with_comparison (location_t loc, tree type,
4866 tree arg0, tree arg1, tree arg2)
4868 enum tree_code comp_code = TREE_CODE (arg0);
4869 tree arg00 = TREE_OPERAND (arg0, 0);
4870 tree arg01 = TREE_OPERAND (arg0, 1);
4871 tree arg1_type = TREE_TYPE (arg1);
4872 tree tem;
4874 STRIP_NOPS (arg1);
4875 STRIP_NOPS (arg2);
4877 /* If we have A op 0 ? A : -A, consider applying the following
4878 transformations:
4880 A == 0? A : -A same as -A
4881 A != 0? A : -A same as A
4882 A >= 0? A : -A same as abs (A)
4883 A > 0? A : -A same as abs (A)
4884 A <= 0? A : -A same as -abs (A)
4885 A < 0? A : -A same as -abs (A)
4887 None of these transformations work for modes with signed
4888 zeros. If A is +/-0, the first two transformations will
4889 change the sign of the result (from +0 to -0, or vice
4890 versa). The last four will fix the sign of the result,
4891 even though the original expressions could be positive or
4892 negative, depending on the sign of A.
4894 Note that all these transformations are correct if A is
4895 NaN, since the two alternatives (A and -A) are also NaNs. */
4896 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4897 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4898 ? real_zerop (arg01)
4899 : integer_zerop (arg01))
4900 && ((TREE_CODE (arg2) == NEGATE_EXPR
4901 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4902 /* In the case that A is of the form X-Y, '-A' (arg2) may
4903 have already been folded to Y-X, check for that. */
4904 || (TREE_CODE (arg1) == MINUS_EXPR
4905 && TREE_CODE (arg2) == MINUS_EXPR
4906 && operand_equal_p (TREE_OPERAND (arg1, 0),
4907 TREE_OPERAND (arg2, 1), 0)
4908 && operand_equal_p (TREE_OPERAND (arg1, 1),
4909 TREE_OPERAND (arg2, 0), 0))))
4910 switch (comp_code)
4912 case EQ_EXPR:
4913 case UNEQ_EXPR:
4914 tem = fold_convert_loc (loc, arg1_type, arg1);
4915 return pedantic_non_lvalue_loc (loc,
4916 fold_convert_loc (loc, type,
4917 negate_expr (tem)));
4918 case NE_EXPR:
4919 case LTGT_EXPR:
4920 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4921 case UNGE_EXPR:
4922 case UNGT_EXPR:
4923 if (flag_trapping_math)
4924 break;
4925 /* Fall through. */
4926 case GE_EXPR:
4927 case GT_EXPR:
4928 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4929 arg1 = fold_convert_loc (loc, signed_type_for
4930 (TREE_TYPE (arg1)), arg1);
4931 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4932 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4933 case UNLE_EXPR:
4934 case UNLT_EXPR:
4935 if (flag_trapping_math)
4936 break;
4937 case LE_EXPR:
4938 case LT_EXPR:
4939 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4940 arg1 = fold_convert_loc (loc, signed_type_for
4941 (TREE_TYPE (arg1)), arg1);
4942 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4943 return negate_expr (fold_convert_loc (loc, type, tem));
4944 default:
4945 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4946 break;
4949 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4950 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4951 both transformations are correct when A is NaN: A != 0
4952 is then true, and A == 0 is false. */
4954 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4955 && integer_zerop (arg01) && integer_zerop (arg2))
4957 if (comp_code == NE_EXPR)
4958 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4959 else if (comp_code == EQ_EXPR)
4960 return build_zero_cst (type);
4963 /* Try some transformations of A op B ? A : B.
4965 A == B? A : B same as B
4966 A != B? A : B same as A
4967 A >= B? A : B same as max (A, B)
4968 A > B? A : B same as max (B, A)
4969 A <= B? A : B same as min (A, B)
4970 A < B? A : B same as min (B, A)
4972 As above, these transformations don't work in the presence
4973 of signed zeros. For example, if A and B are zeros of
4974 opposite sign, the first two transformations will change
4975 the sign of the result. In the last four, the original
4976 expressions give different results for (A=+0, B=-0) and
4977 (A=-0, B=+0), but the transformed expressions do not.
4979 The first two transformations are correct if either A or B
4980 is a NaN. In the first transformation, the condition will
4981 be false, and B will indeed be chosen. In the case of the
4982 second transformation, the condition A != B will be true,
4983 and A will be chosen.
4985 The conversions to max() and min() are not correct if B is
4986 a number and A is not. The conditions in the original
4987 expressions will be false, so all four give B. The min()
4988 and max() versions would give a NaN instead. */
4989 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4990 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4991 /* Avoid these transformations if the COND_EXPR may be used
4992 as an lvalue in the C++ front-end. PR c++/19199. */
4993 && (in_gimple_form
4994 || VECTOR_TYPE_P (type)
4995 || (! lang_GNU_CXX ()
4996 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4997 || ! maybe_lvalue_p (arg1)
4998 || ! maybe_lvalue_p (arg2)))
5000 tree comp_op0 = arg00;
5001 tree comp_op1 = arg01;
5002 tree comp_type = TREE_TYPE (comp_op0);
5004 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5005 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5007 comp_type = type;
5008 comp_op0 = arg1;
5009 comp_op1 = arg2;
5012 switch (comp_code)
5014 case EQ_EXPR:
5015 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5016 case NE_EXPR:
5017 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5018 case LE_EXPR:
5019 case LT_EXPR:
5020 case UNLE_EXPR:
5021 case UNLT_EXPR:
5022 /* In C++ a ?: expression can be an lvalue, so put the
5023 operand which will be used if they are equal first
5024 so that we can convert this back to the
5025 corresponding COND_EXPR. */
5026 if (!HONOR_NANS (arg1))
5028 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5029 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5030 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5031 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5032 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5033 comp_op1, comp_op0);
5034 return pedantic_non_lvalue_loc (loc,
5035 fold_convert_loc (loc, type, tem));
5037 break;
5038 case GE_EXPR:
5039 case GT_EXPR:
5040 case UNGE_EXPR:
5041 case UNGT_EXPR:
5042 if (!HONOR_NANS (arg1))
5044 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5045 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5046 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5047 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5048 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5049 comp_op1, comp_op0);
5050 return pedantic_non_lvalue_loc (loc,
5051 fold_convert_loc (loc, type, tem));
5053 break;
5054 case UNEQ_EXPR:
5055 if (!HONOR_NANS (arg1))
5056 return pedantic_non_lvalue_loc (loc,
5057 fold_convert_loc (loc, type, arg2));
5058 break;
5059 case LTGT_EXPR:
5060 if (!HONOR_NANS (arg1))
5061 return pedantic_non_lvalue_loc (loc,
5062 fold_convert_loc (loc, type, arg1));
5063 break;
5064 default:
5065 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5066 break;
5070 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5071 we might still be able to simplify this. For example,
5072 if C1 is one less or one more than C2, this might have started
5073 out as a MIN or MAX and been transformed by this function.
5074 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5076 if (INTEGRAL_TYPE_P (type)
5077 && TREE_CODE (arg01) == INTEGER_CST
5078 && TREE_CODE (arg2) == INTEGER_CST)
5079 switch (comp_code)
5081 case EQ_EXPR:
5082 if (TREE_CODE (arg1) == INTEGER_CST)
5083 break;
5084 /* We can replace A with C1 in this case. */
5085 arg1 = fold_convert_loc (loc, type, arg01);
5086 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5088 case LT_EXPR:
5089 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5090 MIN_EXPR, to preserve the signedness of the comparison. */
5091 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5092 OEP_ONLY_CONST)
5093 && operand_equal_p (arg01,
5094 const_binop (PLUS_EXPR, arg2,
5095 build_int_cst (type, 1)),
5096 OEP_ONLY_CONST))
5098 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5099 fold_convert_loc (loc, TREE_TYPE (arg00),
5100 arg2));
5101 return pedantic_non_lvalue_loc (loc,
5102 fold_convert_loc (loc, type, tem));
5104 break;
5106 case LE_EXPR:
5107 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5108 as above. */
5109 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5110 OEP_ONLY_CONST)
5111 && operand_equal_p (arg01,
5112 const_binop (MINUS_EXPR, arg2,
5113 build_int_cst (type, 1)),
5114 OEP_ONLY_CONST))
5116 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5117 fold_convert_loc (loc, TREE_TYPE (arg00),
5118 arg2));
5119 return pedantic_non_lvalue_loc (loc,
5120 fold_convert_loc (loc, type, tem));
5122 break;
5124 case GT_EXPR:
5125 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5126 MAX_EXPR, to preserve the signedness of the comparison. */
5127 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5128 OEP_ONLY_CONST)
5129 && operand_equal_p (arg01,
5130 const_binop (MINUS_EXPR, arg2,
5131 build_int_cst (type, 1)),
5132 OEP_ONLY_CONST))
5134 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5135 fold_convert_loc (loc, TREE_TYPE (arg00),
5136 arg2));
5137 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5139 break;
5141 case GE_EXPR:
5142 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5143 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5144 OEP_ONLY_CONST)
5145 && operand_equal_p (arg01,
5146 const_binop (PLUS_EXPR, arg2,
5147 build_int_cst (type, 1)),
5148 OEP_ONLY_CONST))
5150 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5151 fold_convert_loc (loc, TREE_TYPE (arg00),
5152 arg2));
5153 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5155 break;
5156 case NE_EXPR:
5157 break;
5158 default:
5159 gcc_unreachable ();
5162 return NULL_TREE;
5167 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5168 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5169 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5170 false) >= 2)
5171 #endif
5173 /* EXP is some logical combination of boolean tests. See if we can
5174 merge it into some range test. Return the new tree if so. */
5176 static tree
5177 fold_range_test (location_t loc, enum tree_code code, tree type,
5178 tree op0, tree op1)
5180 int or_op = (code == TRUTH_ORIF_EXPR
5181 || code == TRUTH_OR_EXPR);
5182 int in0_p, in1_p, in_p;
5183 tree low0, low1, low, high0, high1, high;
5184 bool strict_overflow_p = false;
5185 tree tem, lhs, rhs;
5186 const char * const warnmsg = G_("assuming signed overflow does not occur "
5187 "when simplifying range test");
5189 if (!INTEGRAL_TYPE_P (type))
5190 return 0;
5192 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5193 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5195 /* If this is an OR operation, invert both sides; we will invert
5196 again at the end. */
5197 if (or_op)
5198 in0_p = ! in0_p, in1_p = ! in1_p;
5200 /* If both expressions are the same, if we can merge the ranges, and we
5201 can build the range test, return it or it inverted. If one of the
5202 ranges is always true or always false, consider it to be the same
5203 expression as the other. */
5204 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5205 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5206 in1_p, low1, high1)
5207 && 0 != (tem = (build_range_check (loc, type,
5208 lhs != 0 ? lhs
5209 : rhs != 0 ? rhs : integer_zero_node,
5210 in_p, low, high))))
5212 if (strict_overflow_p)
5213 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5214 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5217 /* On machines where the branch cost is expensive, if this is a
5218 short-circuited branch and the underlying object on both sides
5219 is the same, make a non-short-circuit operation. */
5220 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5221 && lhs != 0 && rhs != 0
5222 && (code == TRUTH_ANDIF_EXPR
5223 || code == TRUTH_ORIF_EXPR)
5224 && operand_equal_p (lhs, rhs, 0))
5226 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5227 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5228 which cases we can't do this. */
5229 if (simple_operand_p (lhs))
5230 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5231 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5232 type, op0, op1);
5234 else if (!lang_hooks.decls.global_bindings_p ()
5235 && !CONTAINS_PLACEHOLDER_P (lhs))
5237 tree common = save_expr (lhs);
5239 if (0 != (lhs = build_range_check (loc, type, common,
5240 or_op ? ! in0_p : in0_p,
5241 low0, high0))
5242 && (0 != (rhs = build_range_check (loc, type, common,
5243 or_op ? ! in1_p : in1_p,
5244 low1, high1))))
5246 if (strict_overflow_p)
5247 fold_overflow_warning (warnmsg,
5248 WARN_STRICT_OVERFLOW_COMPARISON);
5249 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5250 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5251 type, lhs, rhs);
5256 return 0;
5259 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5260 bit value. Arrange things so the extra bits will be set to zero if and
5261 only if C is signed-extended to its full width. If MASK is nonzero,
5262 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5264 static tree
5265 unextend (tree c, int p, int unsignedp, tree mask)
5267 tree type = TREE_TYPE (c);
5268 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5269 tree temp;
5271 if (p == modesize || unsignedp)
5272 return c;
5274 /* We work by getting just the sign bit into the low-order bit, then
5275 into the high-order bit, then sign-extend. We then XOR that value
5276 with C. */
5277 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5279 /* We must use a signed type in order to get an arithmetic right shift.
5280 However, we must also avoid introducing accidental overflows, so that
5281 a subsequent call to integer_zerop will work. Hence we must
5282 do the type conversion here. At this point, the constant is either
5283 zero or one, and the conversion to a signed type can never overflow.
5284 We could get an overflow if this conversion is done anywhere else. */
5285 if (TYPE_UNSIGNED (type))
5286 temp = fold_convert (signed_type_for (type), temp);
5288 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5289 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5290 if (mask != 0)
5291 temp = const_binop (BIT_AND_EXPR, temp,
5292 fold_convert (TREE_TYPE (c), mask));
5293 /* If necessary, convert the type back to match the type of C. */
5294 if (TYPE_UNSIGNED (type))
5295 temp = fold_convert (type, temp);
5297 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5300 /* For an expression that has the form
5301 (A && B) || ~B
5303 (A || B) && ~B,
5304 we can drop one of the inner expressions and simplify to
5305 A || ~B
5307 A && ~B
5308 LOC is the location of the resulting expression. OP is the inner
5309 logical operation; the left-hand side in the examples above, while CMPOP
5310 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5311 removing a condition that guards another, as in
5312 (A != NULL && A->...) || A == NULL
5313 which we must not transform. If RHS_ONLY is true, only eliminate the
5314 right-most operand of the inner logical operation. */
5316 static tree
5317 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5318 bool rhs_only)
5320 tree type = TREE_TYPE (cmpop);
5321 enum tree_code code = TREE_CODE (cmpop);
5322 enum tree_code truthop_code = TREE_CODE (op);
5323 tree lhs = TREE_OPERAND (op, 0);
5324 tree rhs = TREE_OPERAND (op, 1);
5325 tree orig_lhs = lhs, orig_rhs = rhs;
5326 enum tree_code rhs_code = TREE_CODE (rhs);
5327 enum tree_code lhs_code = TREE_CODE (lhs);
5328 enum tree_code inv_code;
5330 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5331 return NULL_TREE;
5333 if (TREE_CODE_CLASS (code) != tcc_comparison)
5334 return NULL_TREE;
5336 if (rhs_code == truthop_code)
5338 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5339 if (newrhs != NULL_TREE)
5341 rhs = newrhs;
5342 rhs_code = TREE_CODE (rhs);
5345 if (lhs_code == truthop_code && !rhs_only)
5347 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5348 if (newlhs != NULL_TREE)
5350 lhs = newlhs;
5351 lhs_code = TREE_CODE (lhs);
5355 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5356 if (inv_code == rhs_code
5357 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5358 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5359 return lhs;
5360 if (!rhs_only && inv_code == lhs_code
5361 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5362 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5363 return rhs;
5364 if (rhs != orig_rhs || lhs != orig_lhs)
5365 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5366 lhs, rhs);
5367 return NULL_TREE;
5370 /* Find ways of folding logical expressions of LHS and RHS:
5371 Try to merge two comparisons to the same innermost item.
5372 Look for range tests like "ch >= '0' && ch <= '9'".
5373 Look for combinations of simple terms on machines with expensive branches
5374 and evaluate the RHS unconditionally.
5376 For example, if we have p->a == 2 && p->b == 4 and we can make an
5377 object large enough to span both A and B, we can do this with a comparison
5378 against the object ANDed with the a mask.
5380 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5381 operations to do this with one comparison.
5383 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5384 function and the one above.
5386 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5387 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5389 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5390 two operands.
5392 We return the simplified tree or 0 if no optimization is possible. */
5394 static tree
5395 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5396 tree lhs, tree rhs)
5398 /* If this is the "or" of two comparisons, we can do something if
5399 the comparisons are NE_EXPR. If this is the "and", we can do something
5400 if the comparisons are EQ_EXPR. I.e.,
5401 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5403 WANTED_CODE is this operation code. For single bit fields, we can
5404 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5405 comparison for one-bit fields. */
5407 enum tree_code wanted_code;
5408 enum tree_code lcode, rcode;
5409 tree ll_arg, lr_arg, rl_arg, rr_arg;
5410 tree ll_inner, lr_inner, rl_inner, rr_inner;
5411 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5412 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5413 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5414 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5415 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5416 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5417 machine_mode lnmode, rnmode;
5418 tree ll_mask, lr_mask, rl_mask, rr_mask;
5419 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5420 tree l_const, r_const;
5421 tree lntype, rntype, result;
5422 HOST_WIDE_INT first_bit, end_bit;
5423 int volatilep;
5425 /* Start by getting the comparison codes. Fail if anything is volatile.
5426 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5427 it were surrounded with a NE_EXPR. */
5429 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5430 return 0;
5432 lcode = TREE_CODE (lhs);
5433 rcode = TREE_CODE (rhs);
5435 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5437 lhs = build2 (NE_EXPR, truth_type, lhs,
5438 build_int_cst (TREE_TYPE (lhs), 0));
5439 lcode = NE_EXPR;
5442 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5444 rhs = build2 (NE_EXPR, truth_type, rhs,
5445 build_int_cst (TREE_TYPE (rhs), 0));
5446 rcode = NE_EXPR;
5449 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5450 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5451 return 0;
5453 ll_arg = TREE_OPERAND (lhs, 0);
5454 lr_arg = TREE_OPERAND (lhs, 1);
5455 rl_arg = TREE_OPERAND (rhs, 0);
5456 rr_arg = TREE_OPERAND (rhs, 1);
5458 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5459 if (simple_operand_p (ll_arg)
5460 && simple_operand_p (lr_arg))
5462 if (operand_equal_p (ll_arg, rl_arg, 0)
5463 && operand_equal_p (lr_arg, rr_arg, 0))
5465 result = combine_comparisons (loc, code, lcode, rcode,
5466 truth_type, ll_arg, lr_arg);
5467 if (result)
5468 return result;
5470 else if (operand_equal_p (ll_arg, rr_arg, 0)
5471 && operand_equal_p (lr_arg, rl_arg, 0))
5473 result = combine_comparisons (loc, code, lcode,
5474 swap_tree_comparison (rcode),
5475 truth_type, ll_arg, lr_arg);
5476 if (result)
5477 return result;
5481 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5482 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5484 /* If the RHS can be evaluated unconditionally and its operands are
5485 simple, it wins to evaluate the RHS unconditionally on machines
5486 with expensive branches. In this case, this isn't a comparison
5487 that can be merged. */
5489 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5490 false) >= 2
5491 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5492 && simple_operand_p (rl_arg)
5493 && simple_operand_p (rr_arg))
5495 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5496 if (code == TRUTH_OR_EXPR
5497 && lcode == NE_EXPR && integer_zerop (lr_arg)
5498 && rcode == NE_EXPR && integer_zerop (rr_arg)
5499 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5500 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5501 return build2_loc (loc, NE_EXPR, truth_type,
5502 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5503 ll_arg, rl_arg),
5504 build_int_cst (TREE_TYPE (ll_arg), 0));
5506 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5507 if (code == TRUTH_AND_EXPR
5508 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5509 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5510 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5511 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5512 return build2_loc (loc, EQ_EXPR, truth_type,
5513 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5514 ll_arg, rl_arg),
5515 build_int_cst (TREE_TYPE (ll_arg), 0));
5518 /* See if the comparisons can be merged. Then get all the parameters for
5519 each side. */
5521 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5522 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5523 return 0;
5525 volatilep = 0;
5526 ll_inner = decode_field_reference (loc, ll_arg,
5527 &ll_bitsize, &ll_bitpos, &ll_mode,
5528 &ll_unsignedp, &volatilep, &ll_mask,
5529 &ll_and_mask);
5530 lr_inner = decode_field_reference (loc, lr_arg,
5531 &lr_bitsize, &lr_bitpos, &lr_mode,
5532 &lr_unsignedp, &volatilep, &lr_mask,
5533 &lr_and_mask);
5534 rl_inner = decode_field_reference (loc, rl_arg,
5535 &rl_bitsize, &rl_bitpos, &rl_mode,
5536 &rl_unsignedp, &volatilep, &rl_mask,
5537 &rl_and_mask);
5538 rr_inner = decode_field_reference (loc, rr_arg,
5539 &rr_bitsize, &rr_bitpos, &rr_mode,
5540 &rr_unsignedp, &volatilep, &rr_mask,
5541 &rr_and_mask);
5543 /* It must be true that the inner operation on the lhs of each
5544 comparison must be the same if we are to be able to do anything.
5545 Then see if we have constants. If not, the same must be true for
5546 the rhs's. */
5547 if (volatilep || ll_inner == 0 || rl_inner == 0
5548 || ! operand_equal_p (ll_inner, rl_inner, 0))
5549 return 0;
5551 if (TREE_CODE (lr_arg) == INTEGER_CST
5552 && TREE_CODE (rr_arg) == INTEGER_CST)
5553 l_const = lr_arg, r_const = rr_arg;
5554 else if (lr_inner == 0 || rr_inner == 0
5555 || ! operand_equal_p (lr_inner, rr_inner, 0))
5556 return 0;
5557 else
5558 l_const = r_const = 0;
5560 /* If either comparison code is not correct for our logical operation,
5561 fail. However, we can convert a one-bit comparison against zero into
5562 the opposite comparison against that bit being set in the field. */
5564 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5565 if (lcode != wanted_code)
5567 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5569 /* Make the left operand unsigned, since we are only interested
5570 in the value of one bit. Otherwise we are doing the wrong
5571 thing below. */
5572 ll_unsignedp = 1;
5573 l_const = ll_mask;
5575 else
5576 return 0;
5579 /* This is analogous to the code for l_const above. */
5580 if (rcode != wanted_code)
5582 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5584 rl_unsignedp = 1;
5585 r_const = rl_mask;
5587 else
5588 return 0;
5591 /* See if we can find a mode that contains both fields being compared on
5592 the left. If we can't, fail. Otherwise, update all constants and masks
5593 to be relative to a field of that size. */
5594 first_bit = MIN (ll_bitpos, rl_bitpos);
5595 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5596 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5597 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5598 volatilep);
5599 if (lnmode == VOIDmode)
5600 return 0;
5602 lnbitsize = GET_MODE_BITSIZE (lnmode);
5603 lnbitpos = first_bit & ~ (lnbitsize - 1);
5604 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5605 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5607 if (BYTES_BIG_ENDIAN)
5609 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5610 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5613 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5614 size_int (xll_bitpos));
5615 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5616 size_int (xrl_bitpos));
5618 if (l_const)
5620 l_const = fold_convert_loc (loc, lntype, l_const);
5621 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5622 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5623 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5624 fold_build1_loc (loc, BIT_NOT_EXPR,
5625 lntype, ll_mask))))
5627 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5629 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5632 if (r_const)
5634 r_const = fold_convert_loc (loc, lntype, r_const);
5635 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5636 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5637 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5638 fold_build1_loc (loc, BIT_NOT_EXPR,
5639 lntype, rl_mask))))
5641 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5643 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5647 /* If the right sides are not constant, do the same for it. Also,
5648 disallow this optimization if a size or signedness mismatch occurs
5649 between the left and right sides. */
5650 if (l_const == 0)
5652 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5653 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5654 /* Make sure the two fields on the right
5655 correspond to the left without being swapped. */
5656 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5657 return 0;
5659 first_bit = MIN (lr_bitpos, rr_bitpos);
5660 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5661 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5662 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5663 volatilep);
5664 if (rnmode == VOIDmode)
5665 return 0;
5667 rnbitsize = GET_MODE_BITSIZE (rnmode);
5668 rnbitpos = first_bit & ~ (rnbitsize - 1);
5669 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5670 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5672 if (BYTES_BIG_ENDIAN)
5674 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5675 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5678 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5679 rntype, lr_mask),
5680 size_int (xlr_bitpos));
5681 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5682 rntype, rr_mask),
5683 size_int (xrr_bitpos));
5685 /* Make a mask that corresponds to both fields being compared.
5686 Do this for both items being compared. If the operands are the
5687 same size and the bits being compared are in the same position
5688 then we can do this by masking both and comparing the masked
5689 results. */
5690 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5691 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5692 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5694 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5695 ll_unsignedp || rl_unsignedp);
5696 if (! all_ones_mask_p (ll_mask, lnbitsize))
5697 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5699 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5700 lr_unsignedp || rr_unsignedp);
5701 if (! all_ones_mask_p (lr_mask, rnbitsize))
5702 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5704 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5707 /* There is still another way we can do something: If both pairs of
5708 fields being compared are adjacent, we may be able to make a wider
5709 field containing them both.
5711 Note that we still must mask the lhs/rhs expressions. Furthermore,
5712 the mask must be shifted to account for the shift done by
5713 make_bit_field_ref. */
5714 if ((ll_bitsize + ll_bitpos == rl_bitpos
5715 && lr_bitsize + lr_bitpos == rr_bitpos)
5716 || (ll_bitpos == rl_bitpos + rl_bitsize
5717 && lr_bitpos == rr_bitpos + rr_bitsize))
5719 tree type;
5721 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5722 ll_bitsize + rl_bitsize,
5723 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5724 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5725 lr_bitsize + rr_bitsize,
5726 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5728 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5729 size_int (MIN (xll_bitpos, xrl_bitpos)));
5730 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5731 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5733 /* Convert to the smaller type before masking out unwanted bits. */
5734 type = lntype;
5735 if (lntype != rntype)
5737 if (lnbitsize > rnbitsize)
5739 lhs = fold_convert_loc (loc, rntype, lhs);
5740 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5741 type = rntype;
5743 else if (lnbitsize < rnbitsize)
5745 rhs = fold_convert_loc (loc, lntype, rhs);
5746 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5747 type = lntype;
5751 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5752 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5754 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5755 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5757 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5760 return 0;
5763 /* Handle the case of comparisons with constants. If there is something in
5764 common between the masks, those bits of the constants must be the same.
5765 If not, the condition is always false. Test for this to avoid generating
5766 incorrect code below. */
5767 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5768 if (! integer_zerop (result)
5769 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5770 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5772 if (wanted_code == NE_EXPR)
5774 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5775 return constant_boolean_node (true, truth_type);
5777 else
5779 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5780 return constant_boolean_node (false, truth_type);
5784 /* Construct the expression we will return. First get the component
5785 reference we will make. Unless the mask is all ones the width of
5786 that field, perform the mask operation. Then compare with the
5787 merged constant. */
5788 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5789 ll_unsignedp || rl_unsignedp);
5791 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5792 if (! all_ones_mask_p (ll_mask, lnbitsize))
5793 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5795 return build2_loc (loc, wanted_code, truth_type, result,
5796 const_binop (BIT_IOR_EXPR, l_const, r_const));
5799 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5800 constant. */
5802 static tree
5803 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5804 tree op0, tree op1)
5806 tree arg0 = op0;
5807 enum tree_code op_code;
5808 tree comp_const;
5809 tree minmax_const;
5810 int consts_equal, consts_lt;
5811 tree inner;
5813 STRIP_SIGN_NOPS (arg0);
5815 op_code = TREE_CODE (arg0);
5816 minmax_const = TREE_OPERAND (arg0, 1);
5817 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5818 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5819 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5820 inner = TREE_OPERAND (arg0, 0);
5822 /* If something does not permit us to optimize, return the original tree. */
5823 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5824 || TREE_CODE (comp_const) != INTEGER_CST
5825 || TREE_OVERFLOW (comp_const)
5826 || TREE_CODE (minmax_const) != INTEGER_CST
5827 || TREE_OVERFLOW (minmax_const))
5828 return NULL_TREE;
5830 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5831 and GT_EXPR, doing the rest with recursive calls using logical
5832 simplifications. */
5833 switch (code)
5835 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5837 tree tem
5838 = optimize_minmax_comparison (loc,
5839 invert_tree_comparison (code, false),
5840 type, op0, op1);
5841 if (tem)
5842 return invert_truthvalue_loc (loc, tem);
5843 return NULL_TREE;
5846 case GE_EXPR:
5847 return
5848 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5849 optimize_minmax_comparison
5850 (loc, EQ_EXPR, type, arg0, comp_const),
5851 optimize_minmax_comparison
5852 (loc, GT_EXPR, type, arg0, comp_const));
5854 case EQ_EXPR:
5855 if (op_code == MAX_EXPR && consts_equal)
5856 /* MAX (X, 0) == 0 -> X <= 0 */
5857 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5859 else if (op_code == MAX_EXPR && consts_lt)
5860 /* MAX (X, 0) == 5 -> X == 5 */
5861 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5863 else if (op_code == MAX_EXPR)
5864 /* MAX (X, 0) == -1 -> false */
5865 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5867 else if (consts_equal)
5868 /* MIN (X, 0) == 0 -> X >= 0 */
5869 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5871 else if (consts_lt)
5872 /* MIN (X, 0) == 5 -> false */
5873 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5875 else
5876 /* MIN (X, 0) == -1 -> X == -1 */
5877 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5879 case GT_EXPR:
5880 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5881 /* MAX (X, 0) > 0 -> X > 0
5882 MAX (X, 0) > 5 -> X > 5 */
5883 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5885 else if (op_code == MAX_EXPR)
5886 /* MAX (X, 0) > -1 -> true */
5887 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5889 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5890 /* MIN (X, 0) > 0 -> false
5891 MIN (X, 0) > 5 -> false */
5892 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5894 else
5895 /* MIN (X, 0) > -1 -> X > -1 */
5896 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5898 default:
5899 return NULL_TREE;
5903 /* T is an integer expression that is being multiplied, divided, or taken a
5904 modulus (CODE says which and what kind of divide or modulus) by a
5905 constant C. See if we can eliminate that operation by folding it with
5906 other operations already in T. WIDE_TYPE, if non-null, is a type that
5907 should be used for the computation if wider than our type.
5909 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5910 (X * 2) + (Y * 4). We must, however, be assured that either the original
5911 expression would not overflow or that overflow is undefined for the type
5912 in the language in question.
5914 If we return a non-null expression, it is an equivalent form of the
5915 original computation, but need not be in the original type.
5917 We set *STRICT_OVERFLOW_P to true if the return values depends on
5918 signed overflow being undefined. Otherwise we do not change
5919 *STRICT_OVERFLOW_P. */
5921 static tree
5922 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5923 bool *strict_overflow_p)
5925 /* To avoid exponential search depth, refuse to allow recursion past
5926 three levels. Beyond that (1) it's highly unlikely that we'll find
5927 something interesting and (2) we've probably processed it before
5928 when we built the inner expression. */
5930 static int depth;
5931 tree ret;
5933 if (depth > 3)
5934 return NULL;
5936 depth++;
5937 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5938 depth--;
5940 return ret;
5943 static tree
5944 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5945 bool *strict_overflow_p)
5947 tree type = TREE_TYPE (t);
5948 enum tree_code tcode = TREE_CODE (t);
5949 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5950 > GET_MODE_SIZE (TYPE_MODE (type)))
5951 ? wide_type : type);
5952 tree t1, t2;
5953 int same_p = tcode == code;
5954 tree op0 = NULL_TREE, op1 = NULL_TREE;
5955 bool sub_strict_overflow_p;
5957 /* Don't deal with constants of zero here; they confuse the code below. */
5958 if (integer_zerop (c))
5959 return NULL_TREE;
5961 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5962 op0 = TREE_OPERAND (t, 0);
5964 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5965 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5967 /* Note that we need not handle conditional operations here since fold
5968 already handles those cases. So just do arithmetic here. */
5969 switch (tcode)
5971 case INTEGER_CST:
5972 /* For a constant, we can always simplify if we are a multiply
5973 or (for divide and modulus) if it is a multiple of our constant. */
5974 if (code == MULT_EXPR
5975 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
5976 return const_binop (code, fold_convert (ctype, t),
5977 fold_convert (ctype, c));
5978 break;
5980 CASE_CONVERT: case NON_LVALUE_EXPR:
5981 /* If op0 is an expression ... */
5982 if ((COMPARISON_CLASS_P (op0)
5983 || UNARY_CLASS_P (op0)
5984 || BINARY_CLASS_P (op0)
5985 || VL_EXP_CLASS_P (op0)
5986 || EXPRESSION_CLASS_P (op0))
5987 /* ... and has wrapping overflow, and its type is smaller
5988 than ctype, then we cannot pass through as widening. */
5989 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
5990 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
5991 && (TYPE_PRECISION (ctype)
5992 > TYPE_PRECISION (TREE_TYPE (op0))))
5993 /* ... or this is a truncation (t is narrower than op0),
5994 then we cannot pass through this narrowing. */
5995 || (TYPE_PRECISION (type)
5996 < TYPE_PRECISION (TREE_TYPE (op0)))
5997 /* ... or signedness changes for division or modulus,
5998 then we cannot pass through this conversion. */
5999 || (code != MULT_EXPR
6000 && (TYPE_UNSIGNED (ctype)
6001 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6002 /* ... or has undefined overflow while the converted to
6003 type has not, we cannot do the operation in the inner type
6004 as that would introduce undefined overflow. */
6005 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6006 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6007 && !TYPE_OVERFLOW_UNDEFINED (type))))
6008 break;
6010 /* Pass the constant down and see if we can make a simplification. If
6011 we can, replace this expression with the inner simplification for
6012 possible later conversion to our or some other type. */
6013 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6014 && TREE_CODE (t2) == INTEGER_CST
6015 && !TREE_OVERFLOW (t2)
6016 && (0 != (t1 = extract_muldiv (op0, t2, code,
6017 code == MULT_EXPR
6018 ? ctype : NULL_TREE,
6019 strict_overflow_p))))
6020 return t1;
6021 break;
6023 case ABS_EXPR:
6024 /* If widening the type changes it from signed to unsigned, then we
6025 must avoid building ABS_EXPR itself as unsigned. */
6026 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6028 tree cstype = (*signed_type_for) (ctype);
6029 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6030 != 0)
6032 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6033 return fold_convert (ctype, t1);
6035 break;
6037 /* If the constant is negative, we cannot simplify this. */
6038 if (tree_int_cst_sgn (c) == -1)
6039 break;
6040 /* FALLTHROUGH */
6041 case NEGATE_EXPR:
6042 /* For division and modulus, type can't be unsigned, as e.g.
6043 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6044 For signed types, even with wrapping overflow, this is fine. */
6045 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6046 break;
6047 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6048 != 0)
6049 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6050 break;
6052 case MIN_EXPR: case MAX_EXPR:
6053 /* If widening the type changes the signedness, then we can't perform
6054 this optimization as that changes the result. */
6055 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6056 break;
6058 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6059 sub_strict_overflow_p = false;
6060 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6061 &sub_strict_overflow_p)) != 0
6062 && (t2 = extract_muldiv (op1, c, code, wide_type,
6063 &sub_strict_overflow_p)) != 0)
6065 if (tree_int_cst_sgn (c) < 0)
6066 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6067 if (sub_strict_overflow_p)
6068 *strict_overflow_p = true;
6069 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6070 fold_convert (ctype, t2));
6072 break;
6074 case LSHIFT_EXPR: case RSHIFT_EXPR:
6075 /* If the second operand is constant, this is a multiplication
6076 or floor division, by a power of two, so we can treat it that
6077 way unless the multiplier or divisor overflows. Signed
6078 left-shift overflow is implementation-defined rather than
6079 undefined in C90, so do not convert signed left shift into
6080 multiplication. */
6081 if (TREE_CODE (op1) == INTEGER_CST
6082 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6083 /* const_binop may not detect overflow correctly,
6084 so check for it explicitly here. */
6085 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6086 && 0 != (t1 = fold_convert (ctype,
6087 const_binop (LSHIFT_EXPR,
6088 size_one_node,
6089 op1)))
6090 && !TREE_OVERFLOW (t1))
6091 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6092 ? MULT_EXPR : FLOOR_DIV_EXPR,
6093 ctype,
6094 fold_convert (ctype, op0),
6095 t1),
6096 c, code, wide_type, strict_overflow_p);
6097 break;
6099 case PLUS_EXPR: case MINUS_EXPR:
6100 /* See if we can eliminate the operation on both sides. If we can, we
6101 can return a new PLUS or MINUS. If we can't, the only remaining
6102 cases where we can do anything are if the second operand is a
6103 constant. */
6104 sub_strict_overflow_p = false;
6105 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6106 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6107 if (t1 != 0 && t2 != 0
6108 && (code == MULT_EXPR
6109 /* If not multiplication, we can only do this if both operands
6110 are divisible by c. */
6111 || (multiple_of_p (ctype, op0, c)
6112 && multiple_of_p (ctype, op1, c))))
6114 if (sub_strict_overflow_p)
6115 *strict_overflow_p = true;
6116 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6117 fold_convert (ctype, t2));
6120 /* If this was a subtraction, negate OP1 and set it to be an addition.
6121 This simplifies the logic below. */
6122 if (tcode == MINUS_EXPR)
6124 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6125 /* If OP1 was not easily negatable, the constant may be OP0. */
6126 if (TREE_CODE (op0) == INTEGER_CST)
6128 std::swap (op0, op1);
6129 std::swap (t1, t2);
6133 if (TREE_CODE (op1) != INTEGER_CST)
6134 break;
6136 /* If either OP1 or C are negative, this optimization is not safe for
6137 some of the division and remainder types while for others we need
6138 to change the code. */
6139 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6141 if (code == CEIL_DIV_EXPR)
6142 code = FLOOR_DIV_EXPR;
6143 else if (code == FLOOR_DIV_EXPR)
6144 code = CEIL_DIV_EXPR;
6145 else if (code != MULT_EXPR
6146 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6147 break;
6150 /* If it's a multiply or a division/modulus operation of a multiple
6151 of our constant, do the operation and verify it doesn't overflow. */
6152 if (code == MULT_EXPR
6153 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6155 op1 = const_binop (code, fold_convert (ctype, op1),
6156 fold_convert (ctype, c));
6157 /* We allow the constant to overflow with wrapping semantics. */
6158 if (op1 == 0
6159 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6160 break;
6162 else
6163 break;
6165 /* If we have an unsigned type, we cannot widen the operation since it
6166 will change the result if the original computation overflowed. */
6167 if (TYPE_UNSIGNED (ctype) && ctype != type)
6168 break;
6170 /* If we were able to eliminate our operation from the first side,
6171 apply our operation to the second side and reform the PLUS. */
6172 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6173 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6175 /* The last case is if we are a multiply. In that case, we can
6176 apply the distributive law to commute the multiply and addition
6177 if the multiplication of the constants doesn't overflow
6178 and overflow is defined. With undefined overflow
6179 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6180 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6181 return fold_build2 (tcode, ctype,
6182 fold_build2 (code, ctype,
6183 fold_convert (ctype, op0),
6184 fold_convert (ctype, c)),
6185 op1);
6187 break;
6189 case MULT_EXPR:
6190 /* We have a special case here if we are doing something like
6191 (C * 8) % 4 since we know that's zero. */
6192 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6193 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6194 /* If the multiplication can overflow we cannot optimize this. */
6195 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6196 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6197 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6199 *strict_overflow_p = true;
6200 return omit_one_operand (type, integer_zero_node, op0);
6203 /* ... fall through ... */
6205 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6206 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6207 /* If we can extract our operation from the LHS, do so and return a
6208 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6209 do something only if the second operand is a constant. */
6210 if (same_p
6211 && (t1 = extract_muldiv (op0, c, code, wide_type,
6212 strict_overflow_p)) != 0)
6213 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6214 fold_convert (ctype, op1));
6215 else if (tcode == MULT_EXPR && code == MULT_EXPR
6216 && (t1 = extract_muldiv (op1, c, code, wide_type,
6217 strict_overflow_p)) != 0)
6218 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6219 fold_convert (ctype, t1));
6220 else if (TREE_CODE (op1) != INTEGER_CST)
6221 return 0;
6223 /* If these are the same operation types, we can associate them
6224 assuming no overflow. */
6225 if (tcode == code)
6227 bool overflow_p = false;
6228 bool overflow_mul_p;
6229 signop sign = TYPE_SIGN (ctype);
6230 wide_int mul = wi::mul (op1, c, sign, &overflow_mul_p);
6231 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6232 if (overflow_mul_p
6233 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6234 overflow_p = true;
6235 if (!overflow_p)
6236 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6237 wide_int_to_tree (ctype, mul));
6240 /* If these operations "cancel" each other, we have the main
6241 optimizations of this pass, which occur when either constant is a
6242 multiple of the other, in which case we replace this with either an
6243 operation or CODE or TCODE.
6245 If we have an unsigned type, we cannot do this since it will change
6246 the result if the original computation overflowed. */
6247 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6248 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6249 || (tcode == MULT_EXPR
6250 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6251 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6252 && code != MULT_EXPR)))
6254 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6256 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6257 *strict_overflow_p = true;
6258 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6259 fold_convert (ctype,
6260 const_binop (TRUNC_DIV_EXPR,
6261 op1, c)));
6263 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6265 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6266 *strict_overflow_p = true;
6267 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6268 fold_convert (ctype,
6269 const_binop (TRUNC_DIV_EXPR,
6270 c, op1)));
6273 break;
6275 default:
6276 break;
6279 return 0;
6282 /* Return a node which has the indicated constant VALUE (either 0 or
6283 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6284 and is of the indicated TYPE. */
6286 tree
6287 constant_boolean_node (bool value, tree type)
6289 if (type == integer_type_node)
6290 return value ? integer_one_node : integer_zero_node;
6291 else if (type == boolean_type_node)
6292 return value ? boolean_true_node : boolean_false_node;
6293 else if (TREE_CODE (type) == VECTOR_TYPE)
6294 return build_vector_from_val (type,
6295 build_int_cst (TREE_TYPE (type),
6296 value ? -1 : 0));
6297 else
6298 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6302 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6303 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6304 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6305 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6306 COND is the first argument to CODE; otherwise (as in the example
6307 given here), it is the second argument. TYPE is the type of the
6308 original expression. Return NULL_TREE if no simplification is
6309 possible. */
6311 static tree
6312 fold_binary_op_with_conditional_arg (location_t loc,
6313 enum tree_code code,
6314 tree type, tree op0, tree op1,
6315 tree cond, tree arg, int cond_first_p)
6317 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6318 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6319 tree test, true_value, false_value;
6320 tree lhs = NULL_TREE;
6321 tree rhs = NULL_TREE;
6322 enum tree_code cond_code = COND_EXPR;
6324 if (TREE_CODE (cond) == COND_EXPR
6325 || TREE_CODE (cond) == VEC_COND_EXPR)
6327 test = TREE_OPERAND (cond, 0);
6328 true_value = TREE_OPERAND (cond, 1);
6329 false_value = TREE_OPERAND (cond, 2);
6330 /* If this operand throws an expression, then it does not make
6331 sense to try to perform a logical or arithmetic operation
6332 involving it. */
6333 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6334 lhs = true_value;
6335 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6336 rhs = false_value;
6338 else
6340 tree testtype = TREE_TYPE (cond);
6341 test = cond;
6342 true_value = constant_boolean_node (true, testtype);
6343 false_value = constant_boolean_node (false, testtype);
6346 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6347 cond_code = VEC_COND_EXPR;
6349 /* This transformation is only worthwhile if we don't have to wrap ARG
6350 in a SAVE_EXPR and the operation can be simplified without recursing
6351 on at least one of the branches once its pushed inside the COND_EXPR. */
6352 if (!TREE_CONSTANT (arg)
6353 && (TREE_SIDE_EFFECTS (arg)
6354 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6355 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6356 return NULL_TREE;
6358 arg = fold_convert_loc (loc, arg_type, arg);
6359 if (lhs == 0)
6361 true_value = fold_convert_loc (loc, cond_type, true_value);
6362 if (cond_first_p)
6363 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6364 else
6365 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6367 if (rhs == 0)
6369 false_value = fold_convert_loc (loc, cond_type, false_value);
6370 if (cond_first_p)
6371 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6372 else
6373 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6376 /* Check that we have simplified at least one of the branches. */
6377 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6378 return NULL_TREE;
6380 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6384 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6386 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6387 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6388 ADDEND is the same as X.
6390 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6391 and finite. The problematic cases are when X is zero, and its mode
6392 has signed zeros. In the case of rounding towards -infinity,
6393 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6394 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6396 bool
6397 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6399 if (!real_zerop (addend))
6400 return false;
6402 /* Don't allow the fold with -fsignaling-nans. */
6403 if (HONOR_SNANS (element_mode (type)))
6404 return false;
6406 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6407 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6408 return true;
6410 /* In a vector or complex, we would need to check the sign of all zeros. */
6411 if (TREE_CODE (addend) != REAL_CST)
6412 return false;
6414 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6415 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6416 negate = !negate;
6418 /* The mode has signed zeros, and we have to honor their sign.
6419 In this situation, there is only one case we can return true for.
6420 X - 0 is the same as X unless rounding towards -infinity is
6421 supported. */
6422 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6425 /* Subroutine of fold() that checks comparisons of built-in math
6426 functions against real constants.
6428 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6429 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6430 is the type of the result and ARG0 and ARG1 are the operands of the
6431 comparison. ARG1 must be a TREE_REAL_CST.
6433 The function returns the constant folded tree if a simplification
6434 can be made, and NULL_TREE otherwise. */
6436 static tree
6437 fold_mathfn_compare (location_t loc,
6438 enum built_in_function fcode, enum tree_code code,
6439 tree type, tree arg0, tree arg1)
6441 REAL_VALUE_TYPE c;
6443 if (BUILTIN_SQRT_P (fcode))
6445 tree arg = CALL_EXPR_ARG (arg0, 0);
6446 machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6448 c = TREE_REAL_CST (arg1);
6449 if (REAL_VALUE_NEGATIVE (c))
6451 /* sqrt(x) < y is always false, if y is negative. */
6452 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6453 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6455 /* sqrt(x) > y is always true, if y is negative and we
6456 don't care about NaNs, i.e. negative values of x. */
6457 if (code == NE_EXPR || !HONOR_NANS (mode))
6458 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6460 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6461 return fold_build2_loc (loc, GE_EXPR, type, arg,
6462 build_real (TREE_TYPE (arg), dconst0));
6464 else if (code == GT_EXPR || code == GE_EXPR)
6466 REAL_VALUE_TYPE c2;
6468 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6469 real_convert (&c2, mode, &c2);
6471 if (REAL_VALUE_ISINF (c2))
6473 /* sqrt(x) > y is x == +Inf, when y is very large. */
6474 if (HONOR_INFINITIES (mode))
6475 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6476 build_real (TREE_TYPE (arg), c2));
6478 /* sqrt(x) > y is always false, when y is very large
6479 and we don't care about infinities. */
6480 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6483 /* sqrt(x) > c is the same as x > c*c. */
6484 return fold_build2_loc (loc, code, type, arg,
6485 build_real (TREE_TYPE (arg), c2));
6487 else if (code == LT_EXPR || code == LE_EXPR)
6489 REAL_VALUE_TYPE c2;
6491 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6492 real_convert (&c2, mode, &c2);
6494 if (REAL_VALUE_ISINF (c2))
6496 /* sqrt(x) < y is always true, when y is a very large
6497 value and we don't care about NaNs or Infinities. */
6498 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6499 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6501 /* sqrt(x) < y is x != +Inf when y is very large and we
6502 don't care about NaNs. */
6503 if (! HONOR_NANS (mode))
6504 return fold_build2_loc (loc, NE_EXPR, type, arg,
6505 build_real (TREE_TYPE (arg), c2));
6507 /* sqrt(x) < y is x >= 0 when y is very large and we
6508 don't care about Infinities. */
6509 if (! HONOR_INFINITIES (mode))
6510 return fold_build2_loc (loc, GE_EXPR, type, arg,
6511 build_real (TREE_TYPE (arg), dconst0));
6513 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6514 arg = save_expr (arg);
6515 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6516 fold_build2_loc (loc, GE_EXPR, type, arg,
6517 build_real (TREE_TYPE (arg),
6518 dconst0)),
6519 fold_build2_loc (loc, NE_EXPR, type, arg,
6520 build_real (TREE_TYPE (arg),
6521 c2)));
6524 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6525 if (! HONOR_NANS (mode))
6526 return fold_build2_loc (loc, code, type, arg,
6527 build_real (TREE_TYPE (arg), c2));
6529 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6530 arg = save_expr (arg);
6531 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6532 fold_build2_loc (loc, GE_EXPR, type, arg,
6533 build_real (TREE_TYPE (arg),
6534 dconst0)),
6535 fold_build2_loc (loc, code, type, arg,
6536 build_real (TREE_TYPE (arg),
6537 c2)));
6541 return NULL_TREE;
6544 /* Subroutine of fold() that optimizes comparisons against Infinities,
6545 either +Inf or -Inf.
6547 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6548 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6549 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6551 The function returns the constant folded tree if a simplification
6552 can be made, and NULL_TREE otherwise. */
6554 static tree
6555 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6556 tree arg0, tree arg1)
6558 machine_mode mode;
6559 REAL_VALUE_TYPE max;
6560 tree temp;
6561 bool neg;
6563 mode = TYPE_MODE (TREE_TYPE (arg0));
6565 /* For negative infinity swap the sense of the comparison. */
6566 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6567 if (neg)
6568 code = swap_tree_comparison (code);
6570 switch (code)
6572 case GT_EXPR:
6573 /* x > +Inf is always false, if with ignore sNANs. */
6574 if (HONOR_SNANS (mode))
6575 return NULL_TREE;
6576 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6578 case LE_EXPR:
6579 /* x <= +Inf is always true, if we don't case about NaNs. */
6580 if (! HONOR_NANS (mode))
6581 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6583 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6584 arg0 = save_expr (arg0);
6585 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6587 case EQ_EXPR:
6588 case GE_EXPR:
6589 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6590 real_maxval (&max, neg, mode);
6591 return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6592 arg0, build_real (TREE_TYPE (arg0), max));
6594 case LT_EXPR:
6595 /* x < +Inf is always equal to x <= DBL_MAX. */
6596 real_maxval (&max, neg, mode);
6597 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6598 arg0, build_real (TREE_TYPE (arg0), max));
6600 case NE_EXPR:
6601 /* x != +Inf is always equal to !(x > DBL_MAX). */
6602 real_maxval (&max, neg, mode);
6603 if (! HONOR_NANS (mode))
6604 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6605 arg0, build_real (TREE_TYPE (arg0), max));
6607 temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6608 arg0, build_real (TREE_TYPE (arg0), max));
6609 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6611 default:
6612 break;
6615 return NULL_TREE;
6618 /* Subroutine of fold() that optimizes comparisons of a division by
6619 a nonzero integer constant against an integer constant, i.e.
6620 X/C1 op C2.
6622 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6623 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6624 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6626 The function returns the constant folded tree if a simplification
6627 can be made, and NULL_TREE otherwise. */
6629 static tree
6630 fold_div_compare (location_t loc,
6631 enum tree_code code, tree type, tree arg0, tree arg1)
6633 tree prod, tmp, hi, lo;
6634 tree arg00 = TREE_OPERAND (arg0, 0);
6635 tree arg01 = TREE_OPERAND (arg0, 1);
6636 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6637 bool neg_overflow = false;
6638 bool overflow;
6640 /* We have to do this the hard way to detect unsigned overflow.
6641 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6642 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6643 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6644 neg_overflow = false;
6646 if (sign == UNSIGNED)
6648 tmp = int_const_binop (MINUS_EXPR, arg01,
6649 build_int_cst (TREE_TYPE (arg01), 1));
6650 lo = prod;
6652 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6653 val = wi::add (prod, tmp, sign, &overflow);
6654 hi = force_fit_type (TREE_TYPE (arg00), val,
6655 -1, overflow | TREE_OVERFLOW (prod));
6657 else if (tree_int_cst_sgn (arg01) >= 0)
6659 tmp = int_const_binop (MINUS_EXPR, arg01,
6660 build_int_cst (TREE_TYPE (arg01), 1));
6661 switch (tree_int_cst_sgn (arg1))
6663 case -1:
6664 neg_overflow = true;
6665 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6666 hi = prod;
6667 break;
6669 case 0:
6670 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6671 hi = tmp;
6672 break;
6674 case 1:
6675 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6676 lo = prod;
6677 break;
6679 default:
6680 gcc_unreachable ();
6683 else
6685 /* A negative divisor reverses the relational operators. */
6686 code = swap_tree_comparison (code);
6688 tmp = int_const_binop (PLUS_EXPR, arg01,
6689 build_int_cst (TREE_TYPE (arg01), 1));
6690 switch (tree_int_cst_sgn (arg1))
6692 case -1:
6693 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6694 lo = prod;
6695 break;
6697 case 0:
6698 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6699 lo = tmp;
6700 break;
6702 case 1:
6703 neg_overflow = true;
6704 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6705 hi = prod;
6706 break;
6708 default:
6709 gcc_unreachable ();
6713 switch (code)
6715 case EQ_EXPR:
6716 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6717 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6718 if (TREE_OVERFLOW (hi))
6719 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6720 if (TREE_OVERFLOW (lo))
6721 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6722 return build_range_check (loc, type, arg00, 1, lo, hi);
6724 case NE_EXPR:
6725 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6726 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6727 if (TREE_OVERFLOW (hi))
6728 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6729 if (TREE_OVERFLOW (lo))
6730 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6731 return build_range_check (loc, type, arg00, 0, lo, hi);
6733 case LT_EXPR:
6734 if (TREE_OVERFLOW (lo))
6736 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6737 return omit_one_operand_loc (loc, type, tmp, arg00);
6739 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6741 case LE_EXPR:
6742 if (TREE_OVERFLOW (hi))
6744 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6745 return omit_one_operand_loc (loc, type, tmp, arg00);
6747 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6749 case GT_EXPR:
6750 if (TREE_OVERFLOW (hi))
6752 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6753 return omit_one_operand_loc (loc, type, tmp, arg00);
6755 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6757 case GE_EXPR:
6758 if (TREE_OVERFLOW (lo))
6760 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6761 return omit_one_operand_loc (loc, type, tmp, arg00);
6763 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6765 default:
6766 break;
6769 return NULL_TREE;
6773 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6774 equality/inequality test, then return a simplified form of the test
6775 using a sign testing. Otherwise return NULL. TYPE is the desired
6776 result type. */
6778 static tree
6779 fold_single_bit_test_into_sign_test (location_t loc,
6780 enum tree_code code, tree arg0, tree arg1,
6781 tree result_type)
6783 /* If this is testing a single bit, we can optimize the test. */
6784 if ((code == NE_EXPR || code == EQ_EXPR)
6785 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6786 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6788 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6789 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6790 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6792 if (arg00 != NULL_TREE
6793 /* This is only a win if casting to a signed type is cheap,
6794 i.e. when arg00's type is not a partial mode. */
6795 && TYPE_PRECISION (TREE_TYPE (arg00))
6796 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6798 tree stype = signed_type_for (TREE_TYPE (arg00));
6799 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6800 result_type,
6801 fold_convert_loc (loc, stype, arg00),
6802 build_int_cst (stype, 0));
6806 return NULL_TREE;
6809 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6810 equality/inequality test, then return a simplified form of
6811 the test using shifts and logical operations. Otherwise return
6812 NULL. TYPE is the desired result type. */
6814 tree
6815 fold_single_bit_test (location_t loc, enum tree_code code,
6816 tree arg0, tree arg1, tree result_type)
6818 /* If this is testing a single bit, we can optimize the test. */
6819 if ((code == NE_EXPR || code == EQ_EXPR)
6820 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6821 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6823 tree inner = TREE_OPERAND (arg0, 0);
6824 tree type = TREE_TYPE (arg0);
6825 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6826 machine_mode operand_mode = TYPE_MODE (type);
6827 int ops_unsigned;
6828 tree signed_type, unsigned_type, intermediate_type;
6829 tree tem, one;
6831 /* First, see if we can fold the single bit test into a sign-bit
6832 test. */
6833 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6834 result_type);
6835 if (tem)
6836 return tem;
6838 /* Otherwise we have (A & C) != 0 where C is a single bit,
6839 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6840 Similarly for (A & C) == 0. */
6842 /* If INNER is a right shift of a constant and it plus BITNUM does
6843 not overflow, adjust BITNUM and INNER. */
6844 if (TREE_CODE (inner) == RSHIFT_EXPR
6845 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6846 && bitnum < TYPE_PRECISION (type)
6847 && wi::ltu_p (TREE_OPERAND (inner, 1),
6848 TYPE_PRECISION (type) - bitnum))
6850 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6851 inner = TREE_OPERAND (inner, 0);
6854 /* If we are going to be able to omit the AND below, we must do our
6855 operations as unsigned. If we must use the AND, we have a choice.
6856 Normally unsigned is faster, but for some machines signed is. */
6857 #ifdef LOAD_EXTEND_OP
6858 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6859 && !flag_syntax_only) ? 0 : 1;
6860 #else
6861 ops_unsigned = 1;
6862 #endif
6864 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6865 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6866 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6867 inner = fold_convert_loc (loc, intermediate_type, inner);
6869 if (bitnum != 0)
6870 inner = build2 (RSHIFT_EXPR, intermediate_type,
6871 inner, size_int (bitnum));
6873 one = build_int_cst (intermediate_type, 1);
6875 if (code == EQ_EXPR)
6876 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6878 /* Put the AND last so it can combine with more things. */
6879 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6881 /* Make sure to return the proper type. */
6882 inner = fold_convert_loc (loc, result_type, inner);
6884 return inner;
6886 return NULL_TREE;
6889 /* Check whether we are allowed to reorder operands arg0 and arg1,
6890 such that the evaluation of arg1 occurs before arg0. */
6892 static bool
6893 reorder_operands_p (const_tree arg0, const_tree arg1)
6895 if (! flag_evaluation_order)
6896 return true;
6897 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6898 return true;
6899 return ! TREE_SIDE_EFFECTS (arg0)
6900 && ! TREE_SIDE_EFFECTS (arg1);
6903 /* Test whether it is preferable two swap two operands, ARG0 and
6904 ARG1, for example because ARG0 is an integer constant and ARG1
6905 isn't. If REORDER is true, only recommend swapping if we can
6906 evaluate the operands in reverse order. */
6908 bool
6909 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6911 if (CONSTANT_CLASS_P (arg1))
6912 return 0;
6913 if (CONSTANT_CLASS_P (arg0))
6914 return 1;
6916 STRIP_NOPS (arg0);
6917 STRIP_NOPS (arg1);
6919 if (TREE_CONSTANT (arg1))
6920 return 0;
6921 if (TREE_CONSTANT (arg0))
6922 return 1;
6924 if (reorder && flag_evaluation_order
6925 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6926 return 0;
6928 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6929 for commutative and comparison operators. Ensuring a canonical
6930 form allows the optimizers to find additional redundancies without
6931 having to explicitly check for both orderings. */
6932 if (TREE_CODE (arg0) == SSA_NAME
6933 && TREE_CODE (arg1) == SSA_NAME
6934 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6935 return 1;
6937 /* Put SSA_NAMEs last. */
6938 if (TREE_CODE (arg1) == SSA_NAME)
6939 return 0;
6940 if (TREE_CODE (arg0) == SSA_NAME)
6941 return 1;
6943 /* Put variables last. */
6944 if (DECL_P (arg1))
6945 return 0;
6946 if (DECL_P (arg0))
6947 return 1;
6949 return 0;
6952 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6953 ARG0 is extended to a wider type. */
6955 static tree
6956 fold_widened_comparison (location_t loc, enum tree_code code,
6957 tree type, tree arg0, tree arg1)
6959 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6960 tree arg1_unw;
6961 tree shorter_type, outer_type;
6962 tree min, max;
6963 bool above, below;
6965 if (arg0_unw == arg0)
6966 return NULL_TREE;
6967 shorter_type = TREE_TYPE (arg0_unw);
6969 #ifdef HAVE_canonicalize_funcptr_for_compare
6970 /* Disable this optimization if we're casting a function pointer
6971 type on targets that require function pointer canonicalization. */
6972 if (HAVE_canonicalize_funcptr_for_compare
6973 && TREE_CODE (shorter_type) == POINTER_TYPE
6974 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6975 return NULL_TREE;
6976 #endif
6978 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6979 return NULL_TREE;
6981 arg1_unw = get_unwidened (arg1, NULL_TREE);
6983 /* If possible, express the comparison in the shorter mode. */
6984 if ((code == EQ_EXPR || code == NE_EXPR
6985 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6986 && (TREE_TYPE (arg1_unw) == shorter_type
6987 || ((TYPE_PRECISION (shorter_type)
6988 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6989 && (TYPE_UNSIGNED (shorter_type)
6990 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6991 || (TREE_CODE (arg1_unw) == INTEGER_CST
6992 && (TREE_CODE (shorter_type) == INTEGER_TYPE
6993 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6994 && int_fits_type_p (arg1_unw, shorter_type))))
6995 return fold_build2_loc (loc, code, type, arg0_unw,
6996 fold_convert_loc (loc, shorter_type, arg1_unw));
6998 if (TREE_CODE (arg1_unw) != INTEGER_CST
6999 || TREE_CODE (shorter_type) != INTEGER_TYPE
7000 || !int_fits_type_p (arg1_unw, shorter_type))
7001 return NULL_TREE;
7003 /* If we are comparing with the integer that does not fit into the range
7004 of the shorter type, the result is known. */
7005 outer_type = TREE_TYPE (arg1_unw);
7006 min = lower_bound_in_type (outer_type, shorter_type);
7007 max = upper_bound_in_type (outer_type, shorter_type);
7009 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7010 max, arg1_unw));
7011 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7012 arg1_unw, min));
7014 switch (code)
7016 case EQ_EXPR:
7017 if (above || below)
7018 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7019 break;
7021 case NE_EXPR:
7022 if (above || below)
7023 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7024 break;
7026 case LT_EXPR:
7027 case LE_EXPR:
7028 if (above)
7029 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7030 else if (below)
7031 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7033 case GT_EXPR:
7034 case GE_EXPR:
7035 if (above)
7036 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7037 else if (below)
7038 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7040 default:
7041 break;
7044 return NULL_TREE;
7047 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7048 ARG0 just the signedness is changed. */
7050 static tree
7051 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
7052 tree arg0, tree arg1)
7054 tree arg0_inner;
7055 tree inner_type, outer_type;
7057 if (!CONVERT_EXPR_P (arg0))
7058 return NULL_TREE;
7060 outer_type = TREE_TYPE (arg0);
7061 arg0_inner = TREE_OPERAND (arg0, 0);
7062 inner_type = TREE_TYPE (arg0_inner);
7064 #ifdef HAVE_canonicalize_funcptr_for_compare
7065 /* Disable this optimization if we're casting a function pointer
7066 type on targets that require function pointer canonicalization. */
7067 if (HAVE_canonicalize_funcptr_for_compare
7068 && TREE_CODE (inner_type) == POINTER_TYPE
7069 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7070 return NULL_TREE;
7071 #endif
7073 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7074 return NULL_TREE;
7076 if (TREE_CODE (arg1) != INTEGER_CST
7077 && !(CONVERT_EXPR_P (arg1)
7078 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7079 return NULL_TREE;
7081 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7082 && code != NE_EXPR
7083 && code != EQ_EXPR)
7084 return NULL_TREE;
7086 if (POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7087 return NULL_TREE;
7089 if (TREE_CODE (arg1) == INTEGER_CST)
7090 arg1 = force_fit_type (inner_type, wi::to_widest (arg1), 0,
7091 TREE_OVERFLOW (arg1));
7092 else
7093 arg1 = fold_convert_loc (loc, inner_type, arg1);
7095 return fold_build2_loc (loc, code, type, arg0_inner, arg1);
7099 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7100 means A >= Y && A != MAX, but in this case we know that
7101 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7103 static tree
7104 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7106 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7108 if (TREE_CODE (bound) == LT_EXPR)
7109 a = TREE_OPERAND (bound, 0);
7110 else if (TREE_CODE (bound) == GT_EXPR)
7111 a = TREE_OPERAND (bound, 1);
7112 else
7113 return NULL_TREE;
7115 typea = TREE_TYPE (a);
7116 if (!INTEGRAL_TYPE_P (typea)
7117 && !POINTER_TYPE_P (typea))
7118 return NULL_TREE;
7120 if (TREE_CODE (ineq) == LT_EXPR)
7122 a1 = TREE_OPERAND (ineq, 1);
7123 y = TREE_OPERAND (ineq, 0);
7125 else if (TREE_CODE (ineq) == GT_EXPR)
7127 a1 = TREE_OPERAND (ineq, 0);
7128 y = TREE_OPERAND (ineq, 1);
7130 else
7131 return NULL_TREE;
7133 if (TREE_TYPE (a1) != typea)
7134 return NULL_TREE;
7136 if (POINTER_TYPE_P (typea))
7138 /* Convert the pointer types into integer before taking the difference. */
7139 tree ta = fold_convert_loc (loc, ssizetype, a);
7140 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7141 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7143 else
7144 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7146 if (!diff || !integer_onep (diff))
7147 return NULL_TREE;
7149 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7152 /* Fold a sum or difference of at least one multiplication.
7153 Returns the folded tree or NULL if no simplification could be made. */
7155 static tree
7156 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7157 tree arg0, tree arg1)
7159 tree arg00, arg01, arg10, arg11;
7160 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7162 /* (A * C) +- (B * C) -> (A+-B) * C.
7163 (A * C) +- A -> A * (C+-1).
7164 We are most concerned about the case where C is a constant,
7165 but other combinations show up during loop reduction. Since
7166 it is not difficult, try all four possibilities. */
7168 if (TREE_CODE (arg0) == MULT_EXPR)
7170 arg00 = TREE_OPERAND (arg0, 0);
7171 arg01 = TREE_OPERAND (arg0, 1);
7173 else if (TREE_CODE (arg0) == INTEGER_CST)
7175 arg00 = build_one_cst (type);
7176 arg01 = arg0;
7178 else
7180 /* We cannot generate constant 1 for fract. */
7181 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7182 return NULL_TREE;
7183 arg00 = arg0;
7184 arg01 = build_one_cst (type);
7186 if (TREE_CODE (arg1) == MULT_EXPR)
7188 arg10 = TREE_OPERAND (arg1, 0);
7189 arg11 = TREE_OPERAND (arg1, 1);
7191 else if (TREE_CODE (arg1) == INTEGER_CST)
7193 arg10 = build_one_cst (type);
7194 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7195 the purpose of this canonicalization. */
7196 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
7197 && negate_expr_p (arg1)
7198 && code == PLUS_EXPR)
7200 arg11 = negate_expr (arg1);
7201 code = MINUS_EXPR;
7203 else
7204 arg11 = arg1;
7206 else
7208 /* We cannot generate constant 1 for fract. */
7209 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7210 return NULL_TREE;
7211 arg10 = arg1;
7212 arg11 = build_one_cst (type);
7214 same = NULL_TREE;
7216 if (operand_equal_p (arg01, arg11, 0))
7217 same = arg01, alt0 = arg00, alt1 = arg10;
7218 else if (operand_equal_p (arg00, arg10, 0))
7219 same = arg00, alt0 = arg01, alt1 = arg11;
7220 else if (operand_equal_p (arg00, arg11, 0))
7221 same = arg00, alt0 = arg01, alt1 = arg10;
7222 else if (operand_equal_p (arg01, arg10, 0))
7223 same = arg01, alt0 = arg00, alt1 = arg11;
7225 /* No identical multiplicands; see if we can find a common
7226 power-of-two factor in non-power-of-two multiplies. This
7227 can help in multi-dimensional array access. */
7228 else if (tree_fits_shwi_p (arg01)
7229 && tree_fits_shwi_p (arg11))
7231 HOST_WIDE_INT int01, int11, tmp;
7232 bool swap = false;
7233 tree maybe_same;
7234 int01 = tree_to_shwi (arg01);
7235 int11 = tree_to_shwi (arg11);
7237 /* Move min of absolute values to int11. */
7238 if (absu_hwi (int01) < absu_hwi (int11))
7240 tmp = int01, int01 = int11, int11 = tmp;
7241 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7242 maybe_same = arg01;
7243 swap = true;
7245 else
7246 maybe_same = arg11;
7248 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7249 /* The remainder should not be a constant, otherwise we
7250 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7251 increased the number of multiplications necessary. */
7252 && TREE_CODE (arg10) != INTEGER_CST)
7254 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7255 build_int_cst (TREE_TYPE (arg00),
7256 int01 / int11));
7257 alt1 = arg10;
7258 same = maybe_same;
7259 if (swap)
7260 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7264 if (same)
7265 return fold_build2_loc (loc, MULT_EXPR, type,
7266 fold_build2_loc (loc, code, type,
7267 fold_convert_loc (loc, type, alt0),
7268 fold_convert_loc (loc, type, alt1)),
7269 fold_convert_loc (loc, type, same));
7271 return NULL_TREE;
7274 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7275 specified by EXPR into the buffer PTR of length LEN bytes.
7276 Return the number of bytes placed in the buffer, or zero
7277 upon failure. */
7279 static int
7280 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7282 tree type = TREE_TYPE (expr);
7283 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7284 int byte, offset, word, words;
7285 unsigned char value;
7287 if ((off == -1 && total_bytes > len)
7288 || off >= total_bytes)
7289 return 0;
7290 if (off == -1)
7291 off = 0;
7292 words = total_bytes / UNITS_PER_WORD;
7294 for (byte = 0; byte < total_bytes; byte++)
7296 int bitpos = byte * BITS_PER_UNIT;
7297 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7298 number of bytes. */
7299 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7301 if (total_bytes > UNITS_PER_WORD)
7303 word = byte / UNITS_PER_WORD;
7304 if (WORDS_BIG_ENDIAN)
7305 word = (words - 1) - word;
7306 offset = word * UNITS_PER_WORD;
7307 if (BYTES_BIG_ENDIAN)
7308 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7309 else
7310 offset += byte % UNITS_PER_WORD;
7312 else
7313 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7314 if (offset >= off
7315 && offset - off < len)
7316 ptr[offset - off] = value;
7318 return MIN (len, total_bytes - off);
7322 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7323 specified by EXPR into the buffer PTR of length LEN bytes.
7324 Return the number of bytes placed in the buffer, or zero
7325 upon failure. */
7327 static int
7328 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7330 tree type = TREE_TYPE (expr);
7331 machine_mode mode = TYPE_MODE (type);
7332 int total_bytes = GET_MODE_SIZE (mode);
7333 FIXED_VALUE_TYPE value;
7334 tree i_value, i_type;
7336 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7337 return 0;
7339 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7341 if (NULL_TREE == i_type
7342 || TYPE_PRECISION (i_type) != total_bytes)
7343 return 0;
7345 value = TREE_FIXED_CST (expr);
7346 i_value = double_int_to_tree (i_type, value.data);
7348 return native_encode_int (i_value, ptr, len, off);
7352 /* Subroutine of native_encode_expr. Encode the REAL_CST
7353 specified by EXPR into the buffer PTR of length LEN bytes.
7354 Return the number of bytes placed in the buffer, or zero
7355 upon failure. */
7357 static int
7358 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7360 tree type = TREE_TYPE (expr);
7361 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7362 int byte, offset, word, words, bitpos;
7363 unsigned char value;
7365 /* There are always 32 bits in each long, no matter the size of
7366 the hosts long. We handle floating point representations with
7367 up to 192 bits. */
7368 long tmp[6];
7370 if ((off == -1 && total_bytes > len)
7371 || off >= total_bytes)
7372 return 0;
7373 if (off == -1)
7374 off = 0;
7375 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7377 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7379 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7380 bitpos += BITS_PER_UNIT)
7382 byte = (bitpos / BITS_PER_UNIT) & 3;
7383 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7385 if (UNITS_PER_WORD < 4)
7387 word = byte / UNITS_PER_WORD;
7388 if (WORDS_BIG_ENDIAN)
7389 word = (words - 1) - word;
7390 offset = word * UNITS_PER_WORD;
7391 if (BYTES_BIG_ENDIAN)
7392 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7393 else
7394 offset += byte % UNITS_PER_WORD;
7396 else
7397 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7398 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7399 if (offset >= off
7400 && offset - off < len)
7401 ptr[offset - off] = value;
7403 return MIN (len, total_bytes - off);
7406 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7407 specified by EXPR into the buffer PTR of length LEN bytes.
7408 Return the number of bytes placed in the buffer, or zero
7409 upon failure. */
7411 static int
7412 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7414 int rsize, isize;
7415 tree part;
7417 part = TREE_REALPART (expr);
7418 rsize = native_encode_expr (part, ptr, len, off);
7419 if (off == -1
7420 && rsize == 0)
7421 return 0;
7422 part = TREE_IMAGPART (expr);
7423 if (off != -1)
7424 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7425 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7426 if (off == -1
7427 && isize != rsize)
7428 return 0;
7429 return rsize + isize;
7433 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7434 specified by EXPR into the buffer PTR of length LEN bytes.
7435 Return the number of bytes placed in the buffer, or zero
7436 upon failure. */
7438 static int
7439 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7441 unsigned i, count;
7442 int size, offset;
7443 tree itype, elem;
7445 offset = 0;
7446 count = VECTOR_CST_NELTS (expr);
7447 itype = TREE_TYPE (TREE_TYPE (expr));
7448 size = GET_MODE_SIZE (TYPE_MODE (itype));
7449 for (i = 0; i < count; i++)
7451 if (off >= size)
7453 off -= size;
7454 continue;
7456 elem = VECTOR_CST_ELT (expr, i);
7457 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7458 if ((off == -1 && res != size)
7459 || res == 0)
7460 return 0;
7461 offset += res;
7462 if (offset >= len)
7463 return offset;
7464 if (off != -1)
7465 off = 0;
7467 return offset;
7471 /* Subroutine of native_encode_expr. Encode the STRING_CST
7472 specified by EXPR into the buffer PTR of length LEN bytes.
7473 Return the number of bytes placed in the buffer, or zero
7474 upon failure. */
7476 static int
7477 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7479 tree type = TREE_TYPE (expr);
7480 HOST_WIDE_INT total_bytes;
7482 if (TREE_CODE (type) != ARRAY_TYPE
7483 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7484 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7485 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7486 return 0;
7487 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7488 if ((off == -1 && total_bytes > len)
7489 || off >= total_bytes)
7490 return 0;
7491 if (off == -1)
7492 off = 0;
7493 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7495 int written = 0;
7496 if (off < TREE_STRING_LENGTH (expr))
7498 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7499 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7501 memset (ptr + written, 0,
7502 MIN (total_bytes - written, len - written));
7504 else
7505 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7506 return MIN (total_bytes - off, len);
7510 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7511 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7512 buffer PTR of length LEN bytes. If OFF is not -1 then start
7513 the encoding at byte offset OFF and encode at most LEN bytes.
7514 Return the number of bytes placed in the buffer, or zero upon failure. */
7517 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7519 switch (TREE_CODE (expr))
7521 case INTEGER_CST:
7522 return native_encode_int (expr, ptr, len, off);
7524 case REAL_CST:
7525 return native_encode_real (expr, ptr, len, off);
7527 case FIXED_CST:
7528 return native_encode_fixed (expr, ptr, len, off);
7530 case COMPLEX_CST:
7531 return native_encode_complex (expr, ptr, len, off);
7533 case VECTOR_CST:
7534 return native_encode_vector (expr, ptr, len, off);
7536 case STRING_CST:
7537 return native_encode_string (expr, ptr, len, off);
7539 default:
7540 return 0;
7545 /* Subroutine of native_interpret_expr. Interpret the contents of
7546 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7547 If the buffer cannot be interpreted, return NULL_TREE. */
7549 static tree
7550 native_interpret_int (tree type, const unsigned char *ptr, int len)
7552 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7554 if (total_bytes > len
7555 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7556 return NULL_TREE;
7558 wide_int result = wi::from_buffer (ptr, total_bytes);
7560 return wide_int_to_tree (type, result);
7564 /* Subroutine of native_interpret_expr. Interpret the contents of
7565 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7566 If the buffer cannot be interpreted, return NULL_TREE. */
7568 static tree
7569 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7571 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7572 double_int result;
7573 FIXED_VALUE_TYPE fixed_value;
7575 if (total_bytes > len
7576 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7577 return NULL_TREE;
7579 result = double_int::from_buffer (ptr, total_bytes);
7580 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7582 return build_fixed (type, fixed_value);
7586 /* Subroutine of native_interpret_expr. Interpret the contents of
7587 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7588 If the buffer cannot be interpreted, return NULL_TREE. */
7590 static tree
7591 native_interpret_real (tree type, const unsigned char *ptr, int len)
7593 machine_mode mode = TYPE_MODE (type);
7594 int total_bytes = GET_MODE_SIZE (mode);
7595 int byte, offset, word, words, bitpos;
7596 unsigned char value;
7597 /* There are always 32 bits in each long, no matter the size of
7598 the hosts long. We handle floating point representations with
7599 up to 192 bits. */
7600 REAL_VALUE_TYPE r;
7601 long tmp[6];
7603 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7604 if (total_bytes > len || total_bytes > 24)
7605 return NULL_TREE;
7606 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7608 memset (tmp, 0, sizeof (tmp));
7609 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7610 bitpos += BITS_PER_UNIT)
7612 byte = (bitpos / BITS_PER_UNIT) & 3;
7613 if (UNITS_PER_WORD < 4)
7615 word = byte / UNITS_PER_WORD;
7616 if (WORDS_BIG_ENDIAN)
7617 word = (words - 1) - word;
7618 offset = word * UNITS_PER_WORD;
7619 if (BYTES_BIG_ENDIAN)
7620 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7621 else
7622 offset += byte % UNITS_PER_WORD;
7624 else
7625 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7626 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7628 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7631 real_from_target (&r, tmp, mode);
7632 return build_real (type, r);
7636 /* Subroutine of native_interpret_expr. Interpret the contents of
7637 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7638 If the buffer cannot be interpreted, return NULL_TREE. */
7640 static tree
7641 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7643 tree etype, rpart, ipart;
7644 int size;
7646 etype = TREE_TYPE (type);
7647 size = GET_MODE_SIZE (TYPE_MODE (etype));
7648 if (size * 2 > len)
7649 return NULL_TREE;
7650 rpart = native_interpret_expr (etype, ptr, size);
7651 if (!rpart)
7652 return NULL_TREE;
7653 ipart = native_interpret_expr (etype, ptr+size, size);
7654 if (!ipart)
7655 return NULL_TREE;
7656 return build_complex (type, rpart, ipart);
7660 /* Subroutine of native_interpret_expr. Interpret the contents of
7661 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7662 If the buffer cannot be interpreted, return NULL_TREE. */
7664 static tree
7665 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7667 tree etype, elem;
7668 int i, size, count;
7669 tree *elements;
7671 etype = TREE_TYPE (type);
7672 size = GET_MODE_SIZE (TYPE_MODE (etype));
7673 count = TYPE_VECTOR_SUBPARTS (type);
7674 if (size * count > len)
7675 return NULL_TREE;
7677 elements = XALLOCAVEC (tree, count);
7678 for (i = count - 1; i >= 0; i--)
7680 elem = native_interpret_expr (etype, ptr+(i*size), size);
7681 if (!elem)
7682 return NULL_TREE;
7683 elements[i] = elem;
7685 return build_vector (type, elements);
7689 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7690 the buffer PTR of length LEN as a constant of type TYPE. For
7691 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7692 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7693 return NULL_TREE. */
7695 tree
7696 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7698 switch (TREE_CODE (type))
7700 case INTEGER_TYPE:
7701 case ENUMERAL_TYPE:
7702 case BOOLEAN_TYPE:
7703 case POINTER_TYPE:
7704 case REFERENCE_TYPE:
7705 return native_interpret_int (type, ptr, len);
7707 case REAL_TYPE:
7708 return native_interpret_real (type, ptr, len);
7710 case FIXED_POINT_TYPE:
7711 return native_interpret_fixed (type, ptr, len);
7713 case COMPLEX_TYPE:
7714 return native_interpret_complex (type, ptr, len);
7716 case VECTOR_TYPE:
7717 return native_interpret_vector (type, ptr, len);
7719 default:
7720 return NULL_TREE;
7724 /* Returns true if we can interpret the contents of a native encoding
7725 as TYPE. */
7727 static bool
7728 can_native_interpret_type_p (tree type)
7730 switch (TREE_CODE (type))
7732 case INTEGER_TYPE:
7733 case ENUMERAL_TYPE:
7734 case BOOLEAN_TYPE:
7735 case POINTER_TYPE:
7736 case REFERENCE_TYPE:
7737 case FIXED_POINT_TYPE:
7738 case REAL_TYPE:
7739 case COMPLEX_TYPE:
7740 case VECTOR_TYPE:
7741 return true;
7742 default:
7743 return false;
7747 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7748 TYPE at compile-time. If we're unable to perform the conversion
7749 return NULL_TREE. */
7751 static tree
7752 fold_view_convert_expr (tree type, tree expr)
7754 /* We support up to 512-bit values (for V8DFmode). */
7755 unsigned char buffer[64];
7756 int len;
7758 /* Check that the host and target are sane. */
7759 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7760 return NULL_TREE;
7762 len = native_encode_expr (expr, buffer, sizeof (buffer));
7763 if (len == 0)
7764 return NULL_TREE;
7766 return native_interpret_expr (type, buffer, len);
7769 /* Build an expression for the address of T. Folds away INDIRECT_REF
7770 to avoid confusing the gimplify process. */
7772 tree
7773 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7775 /* The size of the object is not relevant when talking about its address. */
7776 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7777 t = TREE_OPERAND (t, 0);
7779 if (TREE_CODE (t) == INDIRECT_REF)
7781 t = TREE_OPERAND (t, 0);
7783 if (TREE_TYPE (t) != ptrtype)
7784 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7786 else if (TREE_CODE (t) == MEM_REF
7787 && integer_zerop (TREE_OPERAND (t, 1)))
7788 return TREE_OPERAND (t, 0);
7789 else if (TREE_CODE (t) == MEM_REF
7790 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7791 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7792 TREE_OPERAND (t, 0),
7793 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7794 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7796 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7798 if (TREE_TYPE (t) != ptrtype)
7799 t = fold_convert_loc (loc, ptrtype, t);
7801 else
7802 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7804 return t;
7807 /* Build an expression for the address of T. */
7809 tree
7810 build_fold_addr_expr_loc (location_t loc, tree t)
7812 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7814 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7817 /* Fold a unary expression of code CODE and type TYPE with operand
7818 OP0. Return the folded expression if folding is successful.
7819 Otherwise, return NULL_TREE. */
7821 tree
7822 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7824 tree tem;
7825 tree arg0;
7826 enum tree_code_class kind = TREE_CODE_CLASS (code);
7828 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7829 && TREE_CODE_LENGTH (code) == 1);
7831 arg0 = op0;
7832 if (arg0)
7834 if (CONVERT_EXPR_CODE_P (code)
7835 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7837 /* Don't use STRIP_NOPS, because signedness of argument type
7838 matters. */
7839 STRIP_SIGN_NOPS (arg0);
7841 else
7843 /* Strip any conversions that don't change the mode. This
7844 is safe for every expression, except for a comparison
7845 expression because its signedness is derived from its
7846 operands.
7848 Note that this is done as an internal manipulation within
7849 the constant folder, in order to find the simplest
7850 representation of the arguments so that their form can be
7851 studied. In any cases, the appropriate type conversions
7852 should be put back in the tree that will get out of the
7853 constant folder. */
7854 STRIP_NOPS (arg0);
7857 if (CONSTANT_CLASS_P (arg0))
7859 tree tem = const_unop (code, type, arg0);
7860 if (tem)
7862 if (TREE_TYPE (tem) != type)
7863 tem = fold_convert_loc (loc, type, tem);
7864 return tem;
7869 tem = generic_simplify (loc, code, type, op0);
7870 if (tem)
7871 return tem;
7873 if (TREE_CODE_CLASS (code) == tcc_unary)
7875 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7876 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7877 fold_build1_loc (loc, code, type,
7878 fold_convert_loc (loc, TREE_TYPE (op0),
7879 TREE_OPERAND (arg0, 1))));
7880 else if (TREE_CODE (arg0) == COND_EXPR)
7882 tree arg01 = TREE_OPERAND (arg0, 1);
7883 tree arg02 = TREE_OPERAND (arg0, 2);
7884 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7885 arg01 = fold_build1_loc (loc, code, type,
7886 fold_convert_loc (loc,
7887 TREE_TYPE (op0), arg01));
7888 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7889 arg02 = fold_build1_loc (loc, code, type,
7890 fold_convert_loc (loc,
7891 TREE_TYPE (op0), arg02));
7892 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7893 arg01, arg02);
7895 /* If this was a conversion, and all we did was to move into
7896 inside the COND_EXPR, bring it back out. But leave it if
7897 it is a conversion from integer to integer and the
7898 result precision is no wider than a word since such a
7899 conversion is cheap and may be optimized away by combine,
7900 while it couldn't if it were outside the COND_EXPR. Then return
7901 so we don't get into an infinite recursion loop taking the
7902 conversion out and then back in. */
7904 if ((CONVERT_EXPR_CODE_P (code)
7905 || code == NON_LVALUE_EXPR)
7906 && TREE_CODE (tem) == COND_EXPR
7907 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7908 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7909 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7910 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7911 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7912 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7913 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7914 && (INTEGRAL_TYPE_P
7915 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7916 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7917 || flag_syntax_only))
7918 tem = build1_loc (loc, code, type,
7919 build3 (COND_EXPR,
7920 TREE_TYPE (TREE_OPERAND
7921 (TREE_OPERAND (tem, 1), 0)),
7922 TREE_OPERAND (tem, 0),
7923 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7924 TREE_OPERAND (TREE_OPERAND (tem, 2),
7925 0)));
7926 return tem;
7930 switch (code)
7932 case NON_LVALUE_EXPR:
7933 if (!maybe_lvalue_p (op0))
7934 return fold_convert_loc (loc, type, op0);
7935 return NULL_TREE;
7937 CASE_CONVERT:
7938 case FLOAT_EXPR:
7939 case FIX_TRUNC_EXPR:
7940 if (COMPARISON_CLASS_P (op0))
7942 /* If we have (type) (a CMP b) and type is an integral type, return
7943 new expression involving the new type. Canonicalize
7944 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7945 non-integral type.
7946 Do not fold the result as that would not simplify further, also
7947 folding again results in recursions. */
7948 if (TREE_CODE (type) == BOOLEAN_TYPE)
7949 return build2_loc (loc, TREE_CODE (op0), type,
7950 TREE_OPERAND (op0, 0),
7951 TREE_OPERAND (op0, 1));
7952 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7953 && TREE_CODE (type) != VECTOR_TYPE)
7954 return build3_loc (loc, COND_EXPR, type, op0,
7955 constant_boolean_node (true, type),
7956 constant_boolean_node (false, type));
7959 /* Handle (T *)&A.B.C for A being of type T and B and C
7960 living at offset zero. This occurs frequently in
7961 C++ upcasting and then accessing the base. */
7962 if (TREE_CODE (op0) == ADDR_EXPR
7963 && POINTER_TYPE_P (type)
7964 && handled_component_p (TREE_OPERAND (op0, 0)))
7966 HOST_WIDE_INT bitsize, bitpos;
7967 tree offset;
7968 machine_mode mode;
7969 int unsignedp, volatilep;
7970 tree base = TREE_OPERAND (op0, 0);
7971 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7972 &mode, &unsignedp, &volatilep, false);
7973 /* If the reference was to a (constant) zero offset, we can use
7974 the address of the base if it has the same base type
7975 as the result type and the pointer type is unqualified. */
7976 if (! offset && bitpos == 0
7977 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7978 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7979 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7980 return fold_convert_loc (loc, type,
7981 build_fold_addr_expr_loc (loc, base));
7984 if (TREE_CODE (op0) == MODIFY_EXPR
7985 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7986 /* Detect assigning a bitfield. */
7987 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7988 && DECL_BIT_FIELD
7989 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7991 /* Don't leave an assignment inside a conversion
7992 unless assigning a bitfield. */
7993 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7994 /* First do the assignment, then return converted constant. */
7995 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7996 TREE_NO_WARNING (tem) = 1;
7997 TREE_USED (tem) = 1;
7998 return tem;
8001 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8002 constants (if x has signed type, the sign bit cannot be set
8003 in c). This folds extension into the BIT_AND_EXPR.
8004 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8005 very likely don't have maximal range for their precision and this
8006 transformation effectively doesn't preserve non-maximal ranges. */
8007 if (TREE_CODE (type) == INTEGER_TYPE
8008 && TREE_CODE (op0) == BIT_AND_EXPR
8009 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
8011 tree and_expr = op0;
8012 tree and0 = TREE_OPERAND (and_expr, 0);
8013 tree and1 = TREE_OPERAND (and_expr, 1);
8014 int change = 0;
8016 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
8017 || (TYPE_PRECISION (type)
8018 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
8019 change = 1;
8020 else if (TYPE_PRECISION (TREE_TYPE (and1))
8021 <= HOST_BITS_PER_WIDE_INT
8022 && tree_fits_uhwi_p (and1))
8024 unsigned HOST_WIDE_INT cst;
8026 cst = tree_to_uhwi (and1);
8027 cst &= HOST_WIDE_INT_M1U
8028 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8029 change = (cst == 0);
8030 #ifdef LOAD_EXTEND_OP
8031 if (change
8032 && !flag_syntax_only
8033 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8034 == ZERO_EXTEND))
8036 tree uns = unsigned_type_for (TREE_TYPE (and0));
8037 and0 = fold_convert_loc (loc, uns, and0);
8038 and1 = fold_convert_loc (loc, uns, and1);
8040 #endif
8042 if (change)
8044 tem = force_fit_type (type, wi::to_widest (and1), 0,
8045 TREE_OVERFLOW (and1));
8046 return fold_build2_loc (loc, BIT_AND_EXPR, type,
8047 fold_convert_loc (loc, type, and0), tem);
8051 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8052 when one of the new casts will fold away. Conservatively we assume
8053 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8054 if (POINTER_TYPE_P (type)
8055 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8056 && (!TYPE_RESTRICT (type) || TYPE_RESTRICT (TREE_TYPE (arg0)))
8057 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8058 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8059 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8061 tree arg00 = TREE_OPERAND (arg0, 0);
8062 tree arg01 = TREE_OPERAND (arg0, 1);
8064 return fold_build_pointer_plus_loc
8065 (loc, fold_convert_loc (loc, type, arg00), arg01);
8068 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8069 of the same precision, and X is an integer type not narrower than
8070 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8071 if (INTEGRAL_TYPE_P (type)
8072 && TREE_CODE (op0) == BIT_NOT_EXPR
8073 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8074 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8075 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8077 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8078 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8079 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8080 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
8081 fold_convert_loc (loc, type, tem));
8084 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8085 type of X and Y (integer types only). */
8086 if (INTEGRAL_TYPE_P (type)
8087 && TREE_CODE (op0) == MULT_EXPR
8088 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8089 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8091 /* Be careful not to introduce new overflows. */
8092 tree mult_type;
8093 if (TYPE_OVERFLOW_WRAPS (type))
8094 mult_type = type;
8095 else
8096 mult_type = unsigned_type_for (type);
8098 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8100 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8101 fold_convert_loc (loc, mult_type,
8102 TREE_OPERAND (op0, 0)),
8103 fold_convert_loc (loc, mult_type,
8104 TREE_OPERAND (op0, 1)));
8105 return fold_convert_loc (loc, type, tem);
8109 return NULL_TREE;
8111 case VIEW_CONVERT_EXPR:
8112 if (TREE_CODE (op0) == MEM_REF)
8113 return fold_build2_loc (loc, MEM_REF, type,
8114 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8116 return NULL_TREE;
8118 case NEGATE_EXPR:
8119 tem = fold_negate_expr (loc, arg0);
8120 if (tem)
8121 return fold_convert_loc (loc, type, tem);
8122 return NULL_TREE;
8124 case ABS_EXPR:
8125 /* Convert fabs((double)float) into (double)fabsf(float). */
8126 if (TREE_CODE (arg0) == NOP_EXPR
8127 && TREE_CODE (type) == REAL_TYPE)
8129 tree targ0 = strip_float_extensions (arg0);
8130 if (targ0 != arg0)
8131 return fold_convert_loc (loc, type,
8132 fold_build1_loc (loc, ABS_EXPR,
8133 TREE_TYPE (targ0),
8134 targ0));
8136 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8137 else if (TREE_CODE (arg0) == ABS_EXPR)
8138 return arg0;
8140 /* Strip sign ops from argument. */
8141 if (TREE_CODE (type) == REAL_TYPE)
8143 tem = fold_strip_sign_ops (arg0);
8144 if (tem)
8145 return fold_build1_loc (loc, ABS_EXPR, type,
8146 fold_convert_loc (loc, type, tem));
8148 return NULL_TREE;
8150 case CONJ_EXPR:
8151 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8152 return fold_convert_loc (loc, type, arg0);
8153 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8155 tree itype = TREE_TYPE (type);
8156 tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8157 tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8158 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8159 negate_expr (ipart));
8161 if (TREE_CODE (arg0) == CONJ_EXPR)
8162 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8163 return NULL_TREE;
8165 case BIT_NOT_EXPR:
8166 /* Convert ~ (-A) to A - 1. */
8167 if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8168 return fold_build2_loc (loc, MINUS_EXPR, type,
8169 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8170 build_int_cst (type, 1));
8171 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8172 else if (INTEGRAL_TYPE_P (type)
8173 && ((TREE_CODE (arg0) == MINUS_EXPR
8174 && integer_onep (TREE_OPERAND (arg0, 1)))
8175 || (TREE_CODE (arg0) == PLUS_EXPR
8176 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8178 /* Perform the negation in ARG0's type and only then convert
8179 to TYPE as to avoid introducing undefined behavior. */
8180 tree t = fold_build1_loc (loc, NEGATE_EXPR,
8181 TREE_TYPE (TREE_OPERAND (arg0, 0)),
8182 TREE_OPERAND (arg0, 0));
8183 return fold_convert_loc (loc, type, t);
8185 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8186 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8187 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8188 fold_convert_loc (loc, type,
8189 TREE_OPERAND (arg0, 0)))))
8190 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8191 fold_convert_loc (loc, type,
8192 TREE_OPERAND (arg0, 1)));
8193 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8194 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8195 fold_convert_loc (loc, type,
8196 TREE_OPERAND (arg0, 1)))))
8197 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8198 fold_convert_loc (loc, type,
8199 TREE_OPERAND (arg0, 0)), tem);
8201 return NULL_TREE;
8203 case TRUTH_NOT_EXPR:
8204 /* Note that the operand of this must be an int
8205 and its values must be 0 or 1.
8206 ("true" is a fixed value perhaps depending on the language,
8207 but we don't handle values other than 1 correctly yet.) */
8208 tem = fold_truth_not_expr (loc, arg0);
8209 if (!tem)
8210 return NULL_TREE;
8211 return fold_convert_loc (loc, type, tem);
8213 case REALPART_EXPR:
8214 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8215 return fold_convert_loc (loc, type, arg0);
8216 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8218 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8219 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8220 fold_build1_loc (loc, REALPART_EXPR, itype,
8221 TREE_OPERAND (arg0, 0)),
8222 fold_build1_loc (loc, REALPART_EXPR, itype,
8223 TREE_OPERAND (arg0, 1)));
8224 return fold_convert_loc (loc, type, tem);
8226 if (TREE_CODE (arg0) == CONJ_EXPR)
8228 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8229 tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8230 TREE_OPERAND (arg0, 0));
8231 return fold_convert_loc (loc, type, tem);
8233 if (TREE_CODE (arg0) == CALL_EXPR)
8235 tree fn = get_callee_fndecl (arg0);
8236 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8237 switch (DECL_FUNCTION_CODE (fn))
8239 CASE_FLT_FN (BUILT_IN_CEXPI):
8240 fn = mathfn_built_in (type, BUILT_IN_COS);
8241 if (fn)
8242 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8243 break;
8245 default:
8246 break;
8249 return NULL_TREE;
8251 case IMAGPART_EXPR:
8252 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8253 return build_zero_cst (type);
8254 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8256 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8257 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8258 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8259 TREE_OPERAND (arg0, 0)),
8260 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8261 TREE_OPERAND (arg0, 1)));
8262 return fold_convert_loc (loc, type, tem);
8264 if (TREE_CODE (arg0) == CONJ_EXPR)
8266 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8267 tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8268 return fold_convert_loc (loc, type, negate_expr (tem));
8270 if (TREE_CODE (arg0) == CALL_EXPR)
8272 tree fn = get_callee_fndecl (arg0);
8273 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8274 switch (DECL_FUNCTION_CODE (fn))
8276 CASE_FLT_FN (BUILT_IN_CEXPI):
8277 fn = mathfn_built_in (type, BUILT_IN_SIN);
8278 if (fn)
8279 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8280 break;
8282 default:
8283 break;
8286 return NULL_TREE;
8288 case INDIRECT_REF:
8289 /* Fold *&X to X if X is an lvalue. */
8290 if (TREE_CODE (op0) == ADDR_EXPR)
8292 tree op00 = TREE_OPERAND (op0, 0);
8293 if ((TREE_CODE (op00) == VAR_DECL
8294 || TREE_CODE (op00) == PARM_DECL
8295 || TREE_CODE (op00) == RESULT_DECL)
8296 && !TREE_READONLY (op00))
8297 return op00;
8299 return NULL_TREE;
8301 default:
8302 return NULL_TREE;
8303 } /* switch (code) */
8307 /* If the operation was a conversion do _not_ mark a resulting constant
8308 with TREE_OVERFLOW if the original constant was not. These conversions
8309 have implementation defined behavior and retaining the TREE_OVERFLOW
8310 flag here would confuse later passes such as VRP. */
8311 tree
8312 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8313 tree type, tree op0)
8315 tree res = fold_unary_loc (loc, code, type, op0);
8316 if (res
8317 && TREE_CODE (res) == INTEGER_CST
8318 && TREE_CODE (op0) == INTEGER_CST
8319 && CONVERT_EXPR_CODE_P (code))
8320 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8322 return res;
8325 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8326 operands OP0 and OP1. LOC is the location of the resulting expression.
8327 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8328 Return the folded expression if folding is successful. Otherwise,
8329 return NULL_TREE. */
8330 static tree
8331 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8332 tree arg0, tree arg1, tree op0, tree op1)
8334 tree tem;
8336 /* We only do these simplifications if we are optimizing. */
8337 if (!optimize)
8338 return NULL_TREE;
8340 /* Check for things like (A || B) && (A || C). We can convert this
8341 to A || (B && C). Note that either operator can be any of the four
8342 truth and/or operations and the transformation will still be
8343 valid. Also note that we only care about order for the
8344 ANDIF and ORIF operators. If B contains side effects, this
8345 might change the truth-value of A. */
8346 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8347 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8348 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8349 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8350 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8351 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8353 tree a00 = TREE_OPERAND (arg0, 0);
8354 tree a01 = TREE_OPERAND (arg0, 1);
8355 tree a10 = TREE_OPERAND (arg1, 0);
8356 tree a11 = TREE_OPERAND (arg1, 1);
8357 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8358 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8359 && (code == TRUTH_AND_EXPR
8360 || code == TRUTH_OR_EXPR));
8362 if (operand_equal_p (a00, a10, 0))
8363 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8364 fold_build2_loc (loc, code, type, a01, a11));
8365 else if (commutative && operand_equal_p (a00, a11, 0))
8366 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8367 fold_build2_loc (loc, code, type, a01, a10));
8368 else if (commutative && operand_equal_p (a01, a10, 0))
8369 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8370 fold_build2_loc (loc, code, type, a00, a11));
8372 /* This case if tricky because we must either have commutative
8373 operators or else A10 must not have side-effects. */
8375 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8376 && operand_equal_p (a01, a11, 0))
8377 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8378 fold_build2_loc (loc, code, type, a00, a10),
8379 a01);
8382 /* See if we can build a range comparison. */
8383 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8384 return tem;
8386 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8387 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8389 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8390 if (tem)
8391 return fold_build2_loc (loc, code, type, tem, arg1);
8394 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8395 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8397 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8398 if (tem)
8399 return fold_build2_loc (loc, code, type, arg0, tem);
8402 /* Check for the possibility of merging component references. If our
8403 lhs is another similar operation, try to merge its rhs with our
8404 rhs. Then try to merge our lhs and rhs. */
8405 if (TREE_CODE (arg0) == code
8406 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8407 TREE_OPERAND (arg0, 1), arg1)))
8408 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8410 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8411 return tem;
8413 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8414 && (code == TRUTH_AND_EXPR
8415 || code == TRUTH_ANDIF_EXPR
8416 || code == TRUTH_OR_EXPR
8417 || code == TRUTH_ORIF_EXPR))
8419 enum tree_code ncode, icode;
8421 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8422 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8423 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8425 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8426 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8427 We don't want to pack more than two leafs to a non-IF AND/OR
8428 expression.
8429 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8430 equal to IF-CODE, then we don't want to add right-hand operand.
8431 If the inner right-hand side of left-hand operand has
8432 side-effects, or isn't simple, then we can't add to it,
8433 as otherwise we might destroy if-sequence. */
8434 if (TREE_CODE (arg0) == icode
8435 && simple_operand_p_2 (arg1)
8436 /* Needed for sequence points to handle trappings, and
8437 side-effects. */
8438 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8440 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8441 arg1);
8442 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8443 tem);
8445 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8446 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8447 else if (TREE_CODE (arg1) == icode
8448 && simple_operand_p_2 (arg0)
8449 /* Needed for sequence points to handle trappings, and
8450 side-effects. */
8451 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8453 tem = fold_build2_loc (loc, ncode, type,
8454 arg0, TREE_OPERAND (arg1, 0));
8455 return fold_build2_loc (loc, icode, type, tem,
8456 TREE_OPERAND (arg1, 1));
8458 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8459 into (A OR B).
8460 For sequence point consistancy, we need to check for trapping,
8461 and side-effects. */
8462 else if (code == icode && simple_operand_p_2 (arg0)
8463 && simple_operand_p_2 (arg1))
8464 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8467 return NULL_TREE;
8470 /* Fold a binary expression of code CODE and type TYPE with operands
8471 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8472 Return the folded expression if folding is successful. Otherwise,
8473 return NULL_TREE. */
8475 static tree
8476 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8478 enum tree_code compl_code;
8480 if (code == MIN_EXPR)
8481 compl_code = MAX_EXPR;
8482 else if (code == MAX_EXPR)
8483 compl_code = MIN_EXPR;
8484 else
8485 gcc_unreachable ();
8487 /* MIN (MAX (a, b), b) == b. */
8488 if (TREE_CODE (op0) == compl_code
8489 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8490 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8492 /* MIN (MAX (b, a), b) == b. */
8493 if (TREE_CODE (op0) == compl_code
8494 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8495 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8496 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8498 /* MIN (a, MAX (a, b)) == a. */
8499 if (TREE_CODE (op1) == compl_code
8500 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8501 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8502 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8504 /* MIN (a, MAX (b, a)) == a. */
8505 if (TREE_CODE (op1) == compl_code
8506 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8507 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8508 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8510 return NULL_TREE;
8513 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8514 by changing CODE to reduce the magnitude of constants involved in
8515 ARG0 of the comparison.
8516 Returns a canonicalized comparison tree if a simplification was
8517 possible, otherwise returns NULL_TREE.
8518 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8519 valid if signed overflow is undefined. */
8521 static tree
8522 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8523 tree arg0, tree arg1,
8524 bool *strict_overflow_p)
8526 enum tree_code code0 = TREE_CODE (arg0);
8527 tree t, cst0 = NULL_TREE;
8528 int sgn0;
8529 bool swap = false;
8531 /* Match A +- CST code arg1 and CST code arg1. We can change the
8532 first form only if overflow is undefined. */
8533 if (!(((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8534 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8535 /* In principle pointers also have undefined overflow behavior,
8536 but that causes problems elsewhere. */
8537 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8538 && (code0 == MINUS_EXPR
8539 || code0 == PLUS_EXPR)
8540 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8541 || code0 == INTEGER_CST))
8542 return NULL_TREE;
8544 /* Identify the constant in arg0 and its sign. */
8545 if (code0 == INTEGER_CST)
8546 cst0 = arg0;
8547 else
8548 cst0 = TREE_OPERAND (arg0, 1);
8549 sgn0 = tree_int_cst_sgn (cst0);
8551 /* Overflowed constants and zero will cause problems. */
8552 if (integer_zerop (cst0)
8553 || TREE_OVERFLOW (cst0))
8554 return NULL_TREE;
8556 /* See if we can reduce the magnitude of the constant in
8557 arg0 by changing the comparison code. */
8558 if (code0 == INTEGER_CST)
8560 /* CST <= arg1 -> CST-1 < arg1. */
8561 if (code == LE_EXPR && sgn0 == 1)
8562 code = LT_EXPR;
8563 /* -CST < arg1 -> -CST-1 <= arg1. */
8564 else if (code == LT_EXPR && sgn0 == -1)
8565 code = LE_EXPR;
8566 /* CST > arg1 -> CST-1 >= arg1. */
8567 else if (code == GT_EXPR && sgn0 == 1)
8568 code = GE_EXPR;
8569 /* -CST >= arg1 -> -CST-1 > arg1. */
8570 else if (code == GE_EXPR && sgn0 == -1)
8571 code = GT_EXPR;
8572 else
8573 return NULL_TREE;
8574 /* arg1 code' CST' might be more canonical. */
8575 swap = true;
8577 else
8579 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8580 if (code == LT_EXPR
8581 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8582 code = LE_EXPR;
8583 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8584 else if (code == GT_EXPR
8585 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8586 code = GE_EXPR;
8587 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8588 else if (code == LE_EXPR
8589 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8590 code = LT_EXPR;
8591 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8592 else if (code == GE_EXPR
8593 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8594 code = GT_EXPR;
8595 else
8596 return NULL_TREE;
8597 *strict_overflow_p = true;
8600 /* Now build the constant reduced in magnitude. But not if that
8601 would produce one outside of its types range. */
8602 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8603 && ((sgn0 == 1
8604 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8605 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8606 || (sgn0 == -1
8607 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8608 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8609 /* We cannot swap the comparison here as that would cause us to
8610 endlessly recurse. */
8611 return NULL_TREE;
8613 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8614 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8615 if (code0 != INTEGER_CST)
8616 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8617 t = fold_convert (TREE_TYPE (arg1), t);
8619 /* If swapping might yield to a more canonical form, do so. */
8620 if (swap)
8621 return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8622 else
8623 return fold_build2_loc (loc, code, type, t, arg1);
8626 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8627 overflow further. Try to decrease the magnitude of constants involved
8628 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8629 and put sole constants at the second argument position.
8630 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8632 static tree
8633 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8634 tree arg0, tree arg1)
8636 tree t;
8637 bool strict_overflow_p;
8638 const char * const warnmsg = G_("assuming signed overflow does not occur "
8639 "when reducing constant in comparison");
8641 /* Try canonicalization by simplifying arg0. */
8642 strict_overflow_p = false;
8643 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8644 &strict_overflow_p);
8645 if (t)
8647 if (strict_overflow_p)
8648 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8649 return t;
8652 /* Try canonicalization by simplifying arg1 using the swapped
8653 comparison. */
8654 code = swap_tree_comparison (code);
8655 strict_overflow_p = false;
8656 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8657 &strict_overflow_p);
8658 if (t && strict_overflow_p)
8659 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8660 return t;
8663 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8664 space. This is used to avoid issuing overflow warnings for
8665 expressions like &p->x which can not wrap. */
8667 static bool
8668 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8670 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8671 return true;
8673 if (bitpos < 0)
8674 return true;
8676 wide_int wi_offset;
8677 int precision = TYPE_PRECISION (TREE_TYPE (base));
8678 if (offset == NULL_TREE)
8679 wi_offset = wi::zero (precision);
8680 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8681 return true;
8682 else
8683 wi_offset = offset;
8685 bool overflow;
8686 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8687 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8688 if (overflow)
8689 return true;
8691 if (!wi::fits_uhwi_p (total))
8692 return true;
8694 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8695 if (size <= 0)
8696 return true;
8698 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8699 array. */
8700 if (TREE_CODE (base) == ADDR_EXPR)
8702 HOST_WIDE_INT base_size;
8704 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8705 if (base_size > 0 && size < base_size)
8706 size = base_size;
8709 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8712 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
8713 kind INTEGER_CST. This makes sure to properly sign-extend the
8714 constant. */
8716 static HOST_WIDE_INT
8717 size_low_cst (const_tree t)
8719 HOST_WIDE_INT w = TREE_INT_CST_ELT (t, 0);
8720 int prec = TYPE_PRECISION (TREE_TYPE (t));
8721 if (prec < HOST_BITS_PER_WIDE_INT)
8722 return sext_hwi (w, prec);
8723 return w;
8726 /* Subroutine of fold_binary. This routine performs all of the
8727 transformations that are common to the equality/inequality
8728 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8729 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8730 fold_binary should call fold_binary. Fold a comparison with
8731 tree code CODE and type TYPE with operands OP0 and OP1. Return
8732 the folded comparison or NULL_TREE. */
8734 static tree
8735 fold_comparison (location_t loc, enum tree_code code, tree type,
8736 tree op0, tree op1)
8738 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8739 tree arg0, arg1, tem;
8741 arg0 = op0;
8742 arg1 = op1;
8744 STRIP_SIGN_NOPS (arg0);
8745 STRIP_SIGN_NOPS (arg1);
8747 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8748 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8749 && (equality_code
8750 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8751 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8752 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8753 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8754 && TREE_CODE (arg1) == INTEGER_CST
8755 && !TREE_OVERFLOW (arg1))
8757 const enum tree_code
8758 reverse_op = TREE_CODE (arg0) == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
8759 tree const1 = TREE_OPERAND (arg0, 1);
8760 tree const2 = fold_convert_loc (loc, TREE_TYPE (const1), arg1);
8761 tree variable = TREE_OPERAND (arg0, 0);
8762 tree new_const = int_const_binop (reverse_op, const2, const1);
8764 /* If the constant operation overflowed this can be
8765 simplified as a comparison against INT_MAX/INT_MIN. */
8766 if (TREE_OVERFLOW (new_const)
8767 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
8769 int const1_sgn = tree_int_cst_sgn (const1);
8770 enum tree_code code2 = code;
8772 /* Get the sign of the constant on the lhs if the
8773 operation were VARIABLE + CONST1. */
8774 if (TREE_CODE (arg0) == MINUS_EXPR)
8775 const1_sgn = -const1_sgn;
8777 /* The sign of the constant determines if we overflowed
8778 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8779 Canonicalize to the INT_MIN overflow by swapping the comparison
8780 if necessary. */
8781 if (const1_sgn == -1)
8782 code2 = swap_tree_comparison (code);
8784 /* We now can look at the canonicalized case
8785 VARIABLE + 1 CODE2 INT_MIN
8786 and decide on the result. */
8787 switch (code2)
8789 case EQ_EXPR:
8790 case LT_EXPR:
8791 case LE_EXPR:
8792 return
8793 omit_one_operand_loc (loc, type, boolean_false_node, variable);
8795 case NE_EXPR:
8796 case GE_EXPR:
8797 case GT_EXPR:
8798 return
8799 omit_one_operand_loc (loc, type, boolean_true_node, variable);
8801 default:
8802 gcc_unreachable ();
8805 else
8807 if (!equality_code)
8808 fold_overflow_warning ("assuming signed overflow does not occur "
8809 "when changing X +- C1 cmp C2 to "
8810 "X cmp C2 -+ C1",
8811 WARN_STRICT_OVERFLOW_COMPARISON);
8812 return fold_build2_loc (loc, code, type, variable, new_const);
8816 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y. */
8817 if (TREE_CODE (arg0) == MINUS_EXPR
8818 && equality_code
8819 && integer_zerop (arg1))
8821 /* ??? The transformation is valid for the other operators if overflow
8822 is undefined for the type, but performing it here badly interacts
8823 with the transformation in fold_cond_expr_with_comparison which
8824 attempts to synthetize ABS_EXPR. */
8825 if (!equality_code)
8826 fold_overflow_warning ("assuming signed overflow does not occur "
8827 "when changing X - Y cmp 0 to X cmp Y",
8828 WARN_STRICT_OVERFLOW_COMPARISON);
8829 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
8830 TREE_OPERAND (arg0, 1));
8833 /* For comparisons of pointers we can decompose it to a compile time
8834 comparison of the base objects and the offsets into the object.
8835 This requires at least one operand being an ADDR_EXPR or a
8836 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8837 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8838 && (TREE_CODE (arg0) == ADDR_EXPR
8839 || TREE_CODE (arg1) == ADDR_EXPR
8840 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8841 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8843 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8844 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8845 machine_mode mode;
8846 int volatilep, unsignedp;
8847 bool indirect_base0 = false, indirect_base1 = false;
8849 /* Get base and offset for the access. Strip ADDR_EXPR for
8850 get_inner_reference, but put it back by stripping INDIRECT_REF
8851 off the base object if possible. indirect_baseN will be true
8852 if baseN is not an address but refers to the object itself. */
8853 base0 = arg0;
8854 if (TREE_CODE (arg0) == ADDR_EXPR)
8856 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8857 &bitsize, &bitpos0, &offset0, &mode,
8858 &unsignedp, &volatilep, false);
8859 if (TREE_CODE (base0) == INDIRECT_REF)
8860 base0 = TREE_OPERAND (base0, 0);
8861 else
8862 indirect_base0 = true;
8864 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8866 base0 = TREE_OPERAND (arg0, 0);
8867 STRIP_SIGN_NOPS (base0);
8868 if (TREE_CODE (base0) == ADDR_EXPR)
8870 base0 = TREE_OPERAND (base0, 0);
8871 indirect_base0 = true;
8873 offset0 = TREE_OPERAND (arg0, 1);
8874 if (tree_fits_shwi_p (offset0))
8876 HOST_WIDE_INT off = size_low_cst (offset0);
8877 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8878 * BITS_PER_UNIT)
8879 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8881 bitpos0 = off * BITS_PER_UNIT;
8882 offset0 = NULL_TREE;
8887 base1 = arg1;
8888 if (TREE_CODE (arg1) == ADDR_EXPR)
8890 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8891 &bitsize, &bitpos1, &offset1, &mode,
8892 &unsignedp, &volatilep, false);
8893 if (TREE_CODE (base1) == INDIRECT_REF)
8894 base1 = TREE_OPERAND (base1, 0);
8895 else
8896 indirect_base1 = true;
8898 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8900 base1 = TREE_OPERAND (arg1, 0);
8901 STRIP_SIGN_NOPS (base1);
8902 if (TREE_CODE (base1) == ADDR_EXPR)
8904 base1 = TREE_OPERAND (base1, 0);
8905 indirect_base1 = true;
8907 offset1 = TREE_OPERAND (arg1, 1);
8908 if (tree_fits_shwi_p (offset1))
8910 HOST_WIDE_INT off = size_low_cst (offset1);
8911 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8912 * BITS_PER_UNIT)
8913 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8915 bitpos1 = off * BITS_PER_UNIT;
8916 offset1 = NULL_TREE;
8921 /* A local variable can never be pointed to by
8922 the default SSA name of an incoming parameter. */
8923 if ((TREE_CODE (arg0) == ADDR_EXPR
8924 && indirect_base0
8925 && TREE_CODE (base0) == VAR_DECL
8926 && auto_var_in_fn_p (base0, current_function_decl)
8927 && !indirect_base1
8928 && TREE_CODE (base1) == SSA_NAME
8929 && SSA_NAME_IS_DEFAULT_DEF (base1)
8930 && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL)
8931 || (TREE_CODE (arg1) == ADDR_EXPR
8932 && indirect_base1
8933 && TREE_CODE (base1) == VAR_DECL
8934 && auto_var_in_fn_p (base1, current_function_decl)
8935 && !indirect_base0
8936 && TREE_CODE (base0) == SSA_NAME
8937 && SSA_NAME_IS_DEFAULT_DEF (base0)
8938 && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL))
8940 if (code == NE_EXPR)
8941 return constant_boolean_node (1, type);
8942 else if (code == EQ_EXPR)
8943 return constant_boolean_node (0, type);
8945 /* If we have equivalent bases we might be able to simplify. */
8946 else if (indirect_base0 == indirect_base1
8947 && operand_equal_p (base0, base1, 0))
8949 /* We can fold this expression to a constant if the non-constant
8950 offset parts are equal. */
8951 if ((offset0 == offset1
8952 || (offset0 && offset1
8953 && operand_equal_p (offset0, offset1, 0)))
8954 && (code == EQ_EXPR
8955 || code == NE_EXPR
8956 || (indirect_base0 && DECL_P (base0))
8957 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8960 if (!equality_code
8961 && bitpos0 != bitpos1
8962 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8963 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8964 fold_overflow_warning (("assuming pointer wraparound does not "
8965 "occur when comparing P +- C1 with "
8966 "P +- C2"),
8967 WARN_STRICT_OVERFLOW_CONDITIONAL);
8969 switch (code)
8971 case EQ_EXPR:
8972 return constant_boolean_node (bitpos0 == bitpos1, type);
8973 case NE_EXPR:
8974 return constant_boolean_node (bitpos0 != bitpos1, type);
8975 case LT_EXPR:
8976 return constant_boolean_node (bitpos0 < bitpos1, type);
8977 case LE_EXPR:
8978 return constant_boolean_node (bitpos0 <= bitpos1, type);
8979 case GE_EXPR:
8980 return constant_boolean_node (bitpos0 >= bitpos1, type);
8981 case GT_EXPR:
8982 return constant_boolean_node (bitpos0 > bitpos1, type);
8983 default:;
8986 /* We can simplify the comparison to a comparison of the variable
8987 offset parts if the constant offset parts are equal.
8988 Be careful to use signed sizetype here because otherwise we
8989 mess with array offsets in the wrong way. This is possible
8990 because pointer arithmetic is restricted to retain within an
8991 object and overflow on pointer differences is undefined as of
8992 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8993 else if (bitpos0 == bitpos1
8994 && (equality_code
8995 || (indirect_base0 && DECL_P (base0))
8996 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8998 /* By converting to signed sizetype we cover middle-end pointer
8999 arithmetic which operates on unsigned pointer types of size
9000 type size and ARRAY_REF offsets which are properly sign or
9001 zero extended from their type in case it is narrower than
9002 sizetype. */
9003 if (offset0 == NULL_TREE)
9004 offset0 = build_int_cst (ssizetype, 0);
9005 else
9006 offset0 = fold_convert_loc (loc, ssizetype, offset0);
9007 if (offset1 == NULL_TREE)
9008 offset1 = build_int_cst (ssizetype, 0);
9009 else
9010 offset1 = fold_convert_loc (loc, ssizetype, offset1);
9012 if (!equality_code
9013 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9014 || pointer_may_wrap_p (base1, offset1, bitpos1)))
9015 fold_overflow_warning (("assuming pointer wraparound does not "
9016 "occur when comparing P +- C1 with "
9017 "P +- C2"),
9018 WARN_STRICT_OVERFLOW_COMPARISON);
9020 return fold_build2_loc (loc, code, type, offset0, offset1);
9023 /* For non-equal bases we can simplify if they are addresses
9024 declarations with different addresses. */
9025 else if (indirect_base0 && indirect_base1
9026 /* We know that !operand_equal_p (base0, base1, 0)
9027 because the if condition was false. But make
9028 sure two decls are not the same. */
9029 && base0 != base1
9030 && TREE_CODE (arg0) == ADDR_EXPR
9031 && TREE_CODE (arg1) == ADDR_EXPR
9032 && DECL_P (base0)
9033 && DECL_P (base1)
9034 /* Watch for aliases. */
9035 && (!decl_in_symtab_p (base0)
9036 || !decl_in_symtab_p (base1)
9037 || !symtab_node::get_create (base0)->equal_address_to
9038 (symtab_node::get_create (base1))))
9040 if (code == EQ_EXPR)
9041 return omit_two_operands_loc (loc, type, boolean_false_node,
9042 arg0, arg1);
9043 else if (code == NE_EXPR)
9044 return omit_two_operands_loc (loc, type, boolean_true_node,
9045 arg0, arg1);
9047 /* For equal offsets we can simplify to a comparison of the
9048 base addresses. */
9049 else if (bitpos0 == bitpos1
9050 && (indirect_base0
9051 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9052 && (indirect_base1
9053 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9054 && ((offset0 == offset1)
9055 || (offset0 && offset1
9056 && operand_equal_p (offset0, offset1, 0))))
9058 if (indirect_base0)
9059 base0 = build_fold_addr_expr_loc (loc, base0);
9060 if (indirect_base1)
9061 base1 = build_fold_addr_expr_loc (loc, base1);
9062 return fold_build2_loc (loc, code, type, base0, base1);
9066 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9067 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
9068 the resulting offset is smaller in absolute value than the
9069 original one and has the same sign. */
9070 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9071 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9072 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9073 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9074 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9075 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9076 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9077 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9079 tree const1 = TREE_OPERAND (arg0, 1);
9080 tree const2 = TREE_OPERAND (arg1, 1);
9081 tree variable1 = TREE_OPERAND (arg0, 0);
9082 tree variable2 = TREE_OPERAND (arg1, 0);
9083 tree cst;
9084 const char * const warnmsg = G_("assuming signed overflow does not "
9085 "occur when combining constants around "
9086 "a comparison");
9088 /* Put the constant on the side where it doesn't overflow and is
9089 of lower absolute value and of same sign than before. */
9090 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9091 ? MINUS_EXPR : PLUS_EXPR,
9092 const2, const1);
9093 if (!TREE_OVERFLOW (cst)
9094 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
9095 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
9097 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9098 return fold_build2_loc (loc, code, type,
9099 variable1,
9100 fold_build2_loc (loc, TREE_CODE (arg1),
9101 TREE_TYPE (arg1),
9102 variable2, cst));
9105 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9106 ? MINUS_EXPR : PLUS_EXPR,
9107 const1, const2);
9108 if (!TREE_OVERFLOW (cst)
9109 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
9110 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
9112 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9113 return fold_build2_loc (loc, code, type,
9114 fold_build2_loc (loc, TREE_CODE (arg0),
9115 TREE_TYPE (arg0),
9116 variable1, cst),
9117 variable2);
9121 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9122 signed arithmetic case. That form is created by the compiler
9123 often enough for folding it to be of value. One example is in
9124 computing loop trip counts after Operator Strength Reduction. */
9125 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9126 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9127 && TREE_CODE (arg0) == MULT_EXPR
9128 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9129 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9130 && integer_zerop (arg1))
9132 tree const1 = TREE_OPERAND (arg0, 1);
9133 tree const2 = arg1; /* zero */
9134 tree variable1 = TREE_OPERAND (arg0, 0);
9135 enum tree_code cmp_code = code;
9137 /* Handle unfolded multiplication by zero. */
9138 if (integer_zerop (const1))
9139 return fold_build2_loc (loc, cmp_code, type, const1, const2);
9141 fold_overflow_warning (("assuming signed overflow does not occur when "
9142 "eliminating multiplication in comparison "
9143 "with zero"),
9144 WARN_STRICT_OVERFLOW_COMPARISON);
9146 /* If const1 is negative we swap the sense of the comparison. */
9147 if (tree_int_cst_sgn (const1) < 0)
9148 cmp_code = swap_tree_comparison (cmp_code);
9150 return fold_build2_loc (loc, cmp_code, type, variable1, const2);
9153 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
9154 if (tem)
9155 return tem;
9157 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9159 tree targ0 = strip_float_extensions (arg0);
9160 tree targ1 = strip_float_extensions (arg1);
9161 tree newtype = TREE_TYPE (targ0);
9163 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9164 newtype = TREE_TYPE (targ1);
9166 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
9167 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9168 return fold_build2_loc (loc, code, type,
9169 fold_convert_loc (loc, newtype, targ0),
9170 fold_convert_loc (loc, newtype, targ1));
9172 if (TREE_CODE (arg1) == REAL_CST)
9174 REAL_VALUE_TYPE cst;
9175 cst = TREE_REAL_CST (arg1);
9177 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
9178 /* a CMP (-0) -> a CMP 0 */
9179 if (REAL_VALUE_MINUS_ZERO (cst))
9180 return fold_build2_loc (loc, code, type, arg0,
9181 build_real (TREE_TYPE (arg1), dconst0));
9183 /* x != NaN is always true, other ops are always false. */
9184 if (REAL_VALUE_ISNAN (cst)
9185 && ! HONOR_SNANS (arg1))
9187 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9188 return omit_one_operand_loc (loc, type, tem, arg0);
9191 /* Fold comparisons against infinity. */
9192 if (REAL_VALUE_ISINF (cst)
9193 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9195 tem = fold_inf_compare (loc, code, type, arg0, arg1);
9196 if (tem != NULL_TREE)
9197 return tem;
9201 /* If this is a comparison of a real constant with a PLUS_EXPR
9202 or a MINUS_EXPR of a real constant, we can convert it into a
9203 comparison with a revised real constant as long as no overflow
9204 occurs when unsafe_math_optimizations are enabled. */
9205 if (flag_unsafe_math_optimizations
9206 && TREE_CODE (arg1) == REAL_CST
9207 && (TREE_CODE (arg0) == PLUS_EXPR
9208 || TREE_CODE (arg0) == MINUS_EXPR)
9209 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9210 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9211 ? MINUS_EXPR : PLUS_EXPR,
9212 arg1, TREE_OPERAND (arg0, 1)))
9213 && !TREE_OVERFLOW (tem))
9214 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9216 /* Likewise, we can simplify a comparison of a real constant with
9217 a MINUS_EXPR whose first operand is also a real constant, i.e.
9218 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
9219 floating-point types only if -fassociative-math is set. */
9220 if (flag_associative_math
9221 && TREE_CODE (arg1) == REAL_CST
9222 && TREE_CODE (arg0) == MINUS_EXPR
9223 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9224 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9225 arg1))
9226 && !TREE_OVERFLOW (tem))
9227 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9228 TREE_OPERAND (arg0, 1), tem);
9230 /* Fold comparisons against built-in math functions. */
9231 if (TREE_CODE (arg1) == REAL_CST
9232 && flag_unsafe_math_optimizations
9233 && ! flag_errno_math)
9235 enum built_in_function fcode = builtin_mathfn_code (arg0);
9237 if (fcode != END_BUILTINS)
9239 tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9240 if (tem != NULL_TREE)
9241 return tem;
9246 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9247 && CONVERT_EXPR_P (arg0))
9249 /* If we are widening one operand of an integer comparison,
9250 see if the other operand is similarly being widened. Perhaps we
9251 can do the comparison in the narrower type. */
9252 tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9253 if (tem)
9254 return tem;
9256 /* Or if we are changing signedness. */
9257 tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9258 if (tem)
9259 return tem;
9262 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9263 constant, we can simplify it. */
9264 if (TREE_CODE (arg1) == INTEGER_CST
9265 && (TREE_CODE (arg0) == MIN_EXPR
9266 || TREE_CODE (arg0) == MAX_EXPR)
9267 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9269 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9270 if (tem)
9271 return tem;
9274 /* Simplify comparison of something with itself. (For IEEE
9275 floating-point, we can only do some of these simplifications.) */
9276 if (operand_equal_p (arg0, arg1, 0))
9278 switch (code)
9280 case EQ_EXPR:
9281 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9282 || ! HONOR_NANS (arg0))
9283 return constant_boolean_node (1, type);
9284 break;
9286 case GE_EXPR:
9287 case LE_EXPR:
9288 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9289 || ! HONOR_NANS (arg0))
9290 return constant_boolean_node (1, type);
9291 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9293 case NE_EXPR:
9294 /* For NE, we can only do this simplification if integer
9295 or we don't honor IEEE floating point NaNs. */
9296 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9297 && HONOR_NANS (arg0))
9298 break;
9299 /* ... fall through ... */
9300 case GT_EXPR:
9301 case LT_EXPR:
9302 return constant_boolean_node (0, type);
9303 default:
9304 gcc_unreachable ();
9308 /* If we are comparing an expression that just has comparisons
9309 of two integer values, arithmetic expressions of those comparisons,
9310 and constants, we can simplify it. There are only three cases
9311 to check: the two values can either be equal, the first can be
9312 greater, or the second can be greater. Fold the expression for
9313 those three values. Since each value must be 0 or 1, we have
9314 eight possibilities, each of which corresponds to the constant 0
9315 or 1 or one of the six possible comparisons.
9317 This handles common cases like (a > b) == 0 but also handles
9318 expressions like ((x > y) - (y > x)) > 0, which supposedly
9319 occur in macroized code. */
9321 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9323 tree cval1 = 0, cval2 = 0;
9324 int save_p = 0;
9326 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9327 /* Don't handle degenerate cases here; they should already
9328 have been handled anyway. */
9329 && cval1 != 0 && cval2 != 0
9330 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9331 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9332 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9333 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9334 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9335 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9336 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9338 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9339 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9341 /* We can't just pass T to eval_subst in case cval1 or cval2
9342 was the same as ARG1. */
9344 tree high_result
9345 = fold_build2_loc (loc, code, type,
9346 eval_subst (loc, arg0, cval1, maxval,
9347 cval2, minval),
9348 arg1);
9349 tree equal_result
9350 = fold_build2_loc (loc, code, type,
9351 eval_subst (loc, arg0, cval1, maxval,
9352 cval2, maxval),
9353 arg1);
9354 tree low_result
9355 = fold_build2_loc (loc, code, type,
9356 eval_subst (loc, arg0, cval1, minval,
9357 cval2, maxval),
9358 arg1);
9360 /* All three of these results should be 0 or 1. Confirm they are.
9361 Then use those values to select the proper code to use. */
9363 if (TREE_CODE (high_result) == INTEGER_CST
9364 && TREE_CODE (equal_result) == INTEGER_CST
9365 && TREE_CODE (low_result) == INTEGER_CST)
9367 /* Make a 3-bit mask with the high-order bit being the
9368 value for `>', the next for '=', and the low for '<'. */
9369 switch ((integer_onep (high_result) * 4)
9370 + (integer_onep (equal_result) * 2)
9371 + integer_onep (low_result))
9373 case 0:
9374 /* Always false. */
9375 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9376 case 1:
9377 code = LT_EXPR;
9378 break;
9379 case 2:
9380 code = EQ_EXPR;
9381 break;
9382 case 3:
9383 code = LE_EXPR;
9384 break;
9385 case 4:
9386 code = GT_EXPR;
9387 break;
9388 case 5:
9389 code = NE_EXPR;
9390 break;
9391 case 6:
9392 code = GE_EXPR;
9393 break;
9394 case 7:
9395 /* Always true. */
9396 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9399 if (save_p)
9401 tem = save_expr (build2 (code, type, cval1, cval2));
9402 SET_EXPR_LOCATION (tem, loc);
9403 return tem;
9405 return fold_build2_loc (loc, code, type, cval1, cval2);
9410 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9411 into a single range test. */
9412 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9413 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9414 && TREE_CODE (arg1) == INTEGER_CST
9415 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9416 && !integer_zerop (TREE_OPERAND (arg0, 1))
9417 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9418 && !TREE_OVERFLOW (arg1))
9420 tem = fold_div_compare (loc, code, type, arg0, arg1);
9421 if (tem != NULL_TREE)
9422 return tem;
9425 /* Fold ~X op ~Y as Y op X. */
9426 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9427 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9429 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9430 return fold_build2_loc (loc, code, type,
9431 fold_convert_loc (loc, cmp_type,
9432 TREE_OPERAND (arg1, 0)),
9433 TREE_OPERAND (arg0, 0));
9436 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9437 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9438 && (TREE_CODE (arg1) == INTEGER_CST || TREE_CODE (arg1) == VECTOR_CST))
9440 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9441 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9442 TREE_OPERAND (arg0, 0),
9443 fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9444 fold_convert_loc (loc, cmp_type, arg1)));
9447 return NULL_TREE;
9451 /* Subroutine of fold_binary. Optimize complex multiplications of the
9452 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9453 argument EXPR represents the expression "z" of type TYPE. */
9455 static tree
9456 fold_mult_zconjz (location_t loc, tree type, tree expr)
9458 tree itype = TREE_TYPE (type);
9459 tree rpart, ipart, tem;
9461 if (TREE_CODE (expr) == COMPLEX_EXPR)
9463 rpart = TREE_OPERAND (expr, 0);
9464 ipart = TREE_OPERAND (expr, 1);
9466 else if (TREE_CODE (expr) == COMPLEX_CST)
9468 rpart = TREE_REALPART (expr);
9469 ipart = TREE_IMAGPART (expr);
9471 else
9473 expr = save_expr (expr);
9474 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9475 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9478 rpart = save_expr (rpart);
9479 ipart = save_expr (ipart);
9480 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9481 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9482 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9483 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9484 build_zero_cst (itype));
9488 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9489 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9490 guarantees that P and N have the same least significant log2(M) bits.
9491 N is not otherwise constrained. In particular, N is not normalized to
9492 0 <= N < M as is common. In general, the precise value of P is unknown.
9493 M is chosen as large as possible such that constant N can be determined.
9495 Returns M and sets *RESIDUE to N.
9497 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9498 account. This is not always possible due to PR 35705.
9501 static unsigned HOST_WIDE_INT
9502 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9503 bool allow_func_align)
9505 enum tree_code code;
9507 *residue = 0;
9509 code = TREE_CODE (expr);
9510 if (code == ADDR_EXPR)
9512 unsigned int bitalign;
9513 get_object_alignment_1 (TREE_OPERAND (expr, 0), &bitalign, residue);
9514 *residue /= BITS_PER_UNIT;
9515 return bitalign / BITS_PER_UNIT;
9517 else if (code == POINTER_PLUS_EXPR)
9519 tree op0, op1;
9520 unsigned HOST_WIDE_INT modulus;
9521 enum tree_code inner_code;
9523 op0 = TREE_OPERAND (expr, 0);
9524 STRIP_NOPS (op0);
9525 modulus = get_pointer_modulus_and_residue (op0, residue,
9526 allow_func_align);
9528 op1 = TREE_OPERAND (expr, 1);
9529 STRIP_NOPS (op1);
9530 inner_code = TREE_CODE (op1);
9531 if (inner_code == INTEGER_CST)
9533 *residue += TREE_INT_CST_LOW (op1);
9534 return modulus;
9536 else if (inner_code == MULT_EXPR)
9538 op1 = TREE_OPERAND (op1, 1);
9539 if (TREE_CODE (op1) == INTEGER_CST)
9541 unsigned HOST_WIDE_INT align;
9543 /* Compute the greatest power-of-2 divisor of op1. */
9544 align = TREE_INT_CST_LOW (op1);
9545 align &= -align;
9547 /* If align is non-zero and less than *modulus, replace
9548 *modulus with align., If align is 0, then either op1 is 0
9549 or the greatest power-of-2 divisor of op1 doesn't fit in an
9550 unsigned HOST_WIDE_INT. In either case, no additional
9551 constraint is imposed. */
9552 if (align)
9553 modulus = MIN (modulus, align);
9555 return modulus;
9560 /* If we get here, we were unable to determine anything useful about the
9561 expression. */
9562 return 1;
9565 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
9566 CONSTRUCTOR ARG into array ELTS and return true if successful. */
9568 static bool
9569 vec_cst_ctor_to_array (tree arg, tree *elts)
9571 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
9573 if (TREE_CODE (arg) == VECTOR_CST)
9575 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
9576 elts[i] = VECTOR_CST_ELT (arg, i);
9578 else if (TREE_CODE (arg) == CONSTRUCTOR)
9580 constructor_elt *elt;
9582 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
9583 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
9584 return false;
9585 else
9586 elts[i] = elt->value;
9588 else
9589 return false;
9590 for (; i < nelts; i++)
9591 elts[i]
9592 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
9593 return true;
9596 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
9597 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
9598 NULL_TREE otherwise. */
9600 static tree
9601 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
9603 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
9604 tree *elts;
9605 bool need_ctor = false;
9607 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
9608 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
9609 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
9610 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
9611 return NULL_TREE;
9613 elts = XALLOCAVEC (tree, nelts * 3);
9614 if (!vec_cst_ctor_to_array (arg0, elts)
9615 || !vec_cst_ctor_to_array (arg1, elts + nelts))
9616 return NULL_TREE;
9618 for (i = 0; i < nelts; i++)
9620 if (!CONSTANT_CLASS_P (elts[sel[i]]))
9621 need_ctor = true;
9622 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
9625 if (need_ctor)
9627 vec<constructor_elt, va_gc> *v;
9628 vec_alloc (v, nelts);
9629 for (i = 0; i < nelts; i++)
9630 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
9631 return build_constructor (type, v);
9633 else
9634 return build_vector (type, &elts[2 * nelts]);
9637 /* Try to fold a pointer difference of type TYPE two address expressions of
9638 array references AREF0 and AREF1 using location LOC. Return a
9639 simplified expression for the difference or NULL_TREE. */
9641 static tree
9642 fold_addr_of_array_ref_difference (location_t loc, tree type,
9643 tree aref0, tree aref1)
9645 tree base0 = TREE_OPERAND (aref0, 0);
9646 tree base1 = TREE_OPERAND (aref1, 0);
9647 tree base_offset = build_int_cst (type, 0);
9649 /* If the bases are array references as well, recurse. If the bases
9650 are pointer indirections compute the difference of the pointers.
9651 If the bases are equal, we are set. */
9652 if ((TREE_CODE (base0) == ARRAY_REF
9653 && TREE_CODE (base1) == ARRAY_REF
9654 && (base_offset
9655 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
9656 || (INDIRECT_REF_P (base0)
9657 && INDIRECT_REF_P (base1)
9658 && (base_offset = fold_binary_loc (loc, MINUS_EXPR, type,
9659 TREE_OPERAND (base0, 0),
9660 TREE_OPERAND (base1, 0))))
9661 || operand_equal_p (base0, base1, 0))
9663 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9664 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9665 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9666 tree diff = build2 (MINUS_EXPR, type, op0, op1);
9667 return fold_build2_loc (loc, PLUS_EXPR, type,
9668 base_offset,
9669 fold_build2_loc (loc, MULT_EXPR, type,
9670 diff, esz));
9672 return NULL_TREE;
9675 /* If the real or vector real constant CST of type TYPE has an exact
9676 inverse, return it, else return NULL. */
9678 tree
9679 exact_inverse (tree type, tree cst)
9681 REAL_VALUE_TYPE r;
9682 tree unit_type, *elts;
9683 machine_mode mode;
9684 unsigned vec_nelts, i;
9686 switch (TREE_CODE (cst))
9688 case REAL_CST:
9689 r = TREE_REAL_CST (cst);
9691 if (exact_real_inverse (TYPE_MODE (type), &r))
9692 return build_real (type, r);
9694 return NULL_TREE;
9696 case VECTOR_CST:
9697 vec_nelts = VECTOR_CST_NELTS (cst);
9698 elts = XALLOCAVEC (tree, vec_nelts);
9699 unit_type = TREE_TYPE (type);
9700 mode = TYPE_MODE (unit_type);
9702 for (i = 0; i < vec_nelts; i++)
9704 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9705 if (!exact_real_inverse (mode, &r))
9706 return NULL_TREE;
9707 elts[i] = build_real (unit_type, r);
9710 return build_vector (type, elts);
9712 default:
9713 return NULL_TREE;
9717 /* Mask out the tz least significant bits of X of type TYPE where
9718 tz is the number of trailing zeroes in Y. */
9719 static wide_int
9720 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9722 int tz = wi::ctz (y);
9723 if (tz > 0)
9724 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9725 return x;
9728 /* Return true when T is an address and is known to be nonzero.
9729 For floating point we further ensure that T is not denormal.
9730 Similar logic is present in nonzero_address in rtlanal.h.
9732 If the return value is based on the assumption that signed overflow
9733 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9734 change *STRICT_OVERFLOW_P. */
9736 static bool
9737 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9739 tree type = TREE_TYPE (t);
9740 enum tree_code code;
9742 /* Doing something useful for floating point would need more work. */
9743 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9744 return false;
9746 code = TREE_CODE (t);
9747 switch (TREE_CODE_CLASS (code))
9749 case tcc_unary:
9750 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9751 strict_overflow_p);
9752 case tcc_binary:
9753 case tcc_comparison:
9754 return tree_binary_nonzero_warnv_p (code, type,
9755 TREE_OPERAND (t, 0),
9756 TREE_OPERAND (t, 1),
9757 strict_overflow_p);
9758 case tcc_constant:
9759 case tcc_declaration:
9760 case tcc_reference:
9761 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9763 default:
9764 break;
9767 switch (code)
9769 case TRUTH_NOT_EXPR:
9770 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9771 strict_overflow_p);
9773 case TRUTH_AND_EXPR:
9774 case TRUTH_OR_EXPR:
9775 case TRUTH_XOR_EXPR:
9776 return tree_binary_nonzero_warnv_p (code, type,
9777 TREE_OPERAND (t, 0),
9778 TREE_OPERAND (t, 1),
9779 strict_overflow_p);
9781 case COND_EXPR:
9782 case CONSTRUCTOR:
9783 case OBJ_TYPE_REF:
9784 case ASSERT_EXPR:
9785 case ADDR_EXPR:
9786 case WITH_SIZE_EXPR:
9787 case SSA_NAME:
9788 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9790 case COMPOUND_EXPR:
9791 case MODIFY_EXPR:
9792 case BIND_EXPR:
9793 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9794 strict_overflow_p);
9796 case SAVE_EXPR:
9797 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9798 strict_overflow_p);
9800 case CALL_EXPR:
9802 tree fndecl = get_callee_fndecl (t);
9803 if (!fndecl) return false;
9804 if (flag_delete_null_pointer_checks && !flag_check_new
9805 && DECL_IS_OPERATOR_NEW (fndecl)
9806 && !TREE_NOTHROW (fndecl))
9807 return true;
9808 if (flag_delete_null_pointer_checks
9809 && lookup_attribute ("returns_nonnull",
9810 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9811 return true;
9812 return alloca_call_p (t);
9815 default:
9816 break;
9818 return false;
9821 /* Return true when T is an address and is known to be nonzero.
9822 Handle warnings about undefined signed overflow. */
9824 static bool
9825 tree_expr_nonzero_p (tree t)
9827 bool ret, strict_overflow_p;
9829 strict_overflow_p = false;
9830 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9831 if (strict_overflow_p)
9832 fold_overflow_warning (("assuming signed overflow does not occur when "
9833 "determining that expression is always "
9834 "non-zero"),
9835 WARN_STRICT_OVERFLOW_MISC);
9836 return ret;
9839 /* Fold a binary expression of code CODE and type TYPE with operands
9840 OP0 and OP1. LOC is the location of the resulting expression.
9841 Return the folded expression if folding is successful. Otherwise,
9842 return NULL_TREE. */
9844 tree
9845 fold_binary_loc (location_t loc,
9846 enum tree_code code, tree type, tree op0, tree op1)
9848 enum tree_code_class kind = TREE_CODE_CLASS (code);
9849 tree arg0, arg1, tem;
9850 tree t1 = NULL_TREE;
9851 bool strict_overflow_p;
9852 unsigned int prec;
9854 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9855 && TREE_CODE_LENGTH (code) == 2
9856 && op0 != NULL_TREE
9857 && op1 != NULL_TREE);
9859 arg0 = op0;
9860 arg1 = op1;
9862 /* Strip any conversions that don't change the mode. This is
9863 safe for every expression, except for a comparison expression
9864 because its signedness is derived from its operands. So, in
9865 the latter case, only strip conversions that don't change the
9866 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9867 preserved.
9869 Note that this is done as an internal manipulation within the
9870 constant folder, in order to find the simplest representation
9871 of the arguments so that their form can be studied. In any
9872 cases, the appropriate type conversions should be put back in
9873 the tree that will get out of the constant folder. */
9875 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9877 STRIP_SIGN_NOPS (arg0);
9878 STRIP_SIGN_NOPS (arg1);
9880 else
9882 STRIP_NOPS (arg0);
9883 STRIP_NOPS (arg1);
9886 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9887 constant but we can't do arithmetic on them. */
9888 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9890 tem = const_binop (code, type, arg0, arg1);
9891 if (tem != NULL_TREE)
9893 if (TREE_TYPE (tem) != type)
9894 tem = fold_convert_loc (loc, type, tem);
9895 return tem;
9899 /* If this is a commutative operation, and ARG0 is a constant, move it
9900 to ARG1 to reduce the number of tests below. */
9901 if (commutative_tree_code (code)
9902 && tree_swap_operands_p (arg0, arg1, true))
9903 return fold_build2_loc (loc, code, type, op1, op0);
9905 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9906 to ARG1 to reduce the number of tests below. */
9907 if (kind == tcc_comparison
9908 && tree_swap_operands_p (arg0, arg1, true))
9909 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9911 tem = generic_simplify (loc, code, type, op0, op1);
9912 if (tem)
9913 return tem;
9915 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9917 First check for cases where an arithmetic operation is applied to a
9918 compound, conditional, or comparison operation. Push the arithmetic
9919 operation inside the compound or conditional to see if any folding
9920 can then be done. Convert comparison to conditional for this purpose.
9921 The also optimizes non-constant cases that used to be done in
9922 expand_expr.
9924 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9925 one of the operands is a comparison and the other is a comparison, a
9926 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9927 code below would make the expression more complex. Change it to a
9928 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9929 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9931 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9932 || code == EQ_EXPR || code == NE_EXPR)
9933 && TREE_CODE (type) != VECTOR_TYPE
9934 && ((truth_value_p (TREE_CODE (arg0))
9935 && (truth_value_p (TREE_CODE (arg1))
9936 || (TREE_CODE (arg1) == BIT_AND_EXPR
9937 && integer_onep (TREE_OPERAND (arg1, 1)))))
9938 || (truth_value_p (TREE_CODE (arg1))
9939 && (truth_value_p (TREE_CODE (arg0))
9940 || (TREE_CODE (arg0) == BIT_AND_EXPR
9941 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9943 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9944 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9945 : TRUTH_XOR_EXPR,
9946 boolean_type_node,
9947 fold_convert_loc (loc, boolean_type_node, arg0),
9948 fold_convert_loc (loc, boolean_type_node, arg1));
9950 if (code == EQ_EXPR)
9951 tem = invert_truthvalue_loc (loc, tem);
9953 return fold_convert_loc (loc, type, tem);
9956 if (TREE_CODE_CLASS (code) == tcc_binary
9957 || TREE_CODE_CLASS (code) == tcc_comparison)
9959 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9961 tem = fold_build2_loc (loc, code, type,
9962 fold_convert_loc (loc, TREE_TYPE (op0),
9963 TREE_OPERAND (arg0, 1)), op1);
9964 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9965 tem);
9967 if (TREE_CODE (arg1) == COMPOUND_EXPR
9968 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9970 tem = fold_build2_loc (loc, code, type, op0,
9971 fold_convert_loc (loc, TREE_TYPE (op1),
9972 TREE_OPERAND (arg1, 1)));
9973 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9974 tem);
9977 if (TREE_CODE (arg0) == COND_EXPR
9978 || TREE_CODE (arg0) == VEC_COND_EXPR
9979 || COMPARISON_CLASS_P (arg0))
9981 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9982 arg0, arg1,
9983 /*cond_first_p=*/1);
9984 if (tem != NULL_TREE)
9985 return tem;
9988 if (TREE_CODE (arg1) == COND_EXPR
9989 || TREE_CODE (arg1) == VEC_COND_EXPR
9990 || COMPARISON_CLASS_P (arg1))
9992 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9993 arg1, arg0,
9994 /*cond_first_p=*/0);
9995 if (tem != NULL_TREE)
9996 return tem;
10000 switch (code)
10002 case MEM_REF:
10003 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
10004 if (TREE_CODE (arg0) == ADDR_EXPR
10005 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
10007 tree iref = TREE_OPERAND (arg0, 0);
10008 return fold_build2 (MEM_REF, type,
10009 TREE_OPERAND (iref, 0),
10010 int_const_binop (PLUS_EXPR, arg1,
10011 TREE_OPERAND (iref, 1)));
10014 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
10015 if (TREE_CODE (arg0) == ADDR_EXPR
10016 && handled_component_p (TREE_OPERAND (arg0, 0)))
10018 tree base;
10019 HOST_WIDE_INT coffset;
10020 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
10021 &coffset);
10022 if (!base)
10023 return NULL_TREE;
10024 return fold_build2 (MEM_REF, type,
10025 build_fold_addr_expr (base),
10026 int_const_binop (PLUS_EXPR, arg1,
10027 size_int (coffset)));
10030 return NULL_TREE;
10032 case POINTER_PLUS_EXPR:
10033 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
10034 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10035 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10036 return fold_convert_loc (loc, type,
10037 fold_build2_loc (loc, PLUS_EXPR, sizetype,
10038 fold_convert_loc (loc, sizetype,
10039 arg1),
10040 fold_convert_loc (loc, sizetype,
10041 arg0)));
10043 return NULL_TREE;
10045 case PLUS_EXPR:
10046 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
10048 /* X + (X / CST) * -CST is X % CST. */
10049 if (TREE_CODE (arg1) == MULT_EXPR
10050 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10051 && operand_equal_p (arg0,
10052 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
10054 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
10055 tree cst1 = TREE_OPERAND (arg1, 1);
10056 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
10057 cst1, cst0);
10058 if (sum && integer_zerop (sum))
10059 return fold_convert_loc (loc, type,
10060 fold_build2_loc (loc, TRUNC_MOD_EXPR,
10061 TREE_TYPE (arg0), arg0,
10062 cst0));
10066 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
10067 one. Make sure the type is not saturating and has the signedness of
10068 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10069 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10070 if ((TREE_CODE (arg0) == MULT_EXPR
10071 || TREE_CODE (arg1) == MULT_EXPR)
10072 && !TYPE_SATURATING (type)
10073 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10074 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10075 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10077 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10078 if (tem)
10079 return tem;
10082 if (! FLOAT_TYPE_P (type))
10084 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
10085 with a constant, and the two constants have no bits in common,
10086 we should treat this as a BIT_IOR_EXPR since this may produce more
10087 simplifications. */
10088 if (TREE_CODE (arg0) == BIT_AND_EXPR
10089 && TREE_CODE (arg1) == BIT_AND_EXPR
10090 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10091 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10092 && wi::bit_and (TREE_OPERAND (arg0, 1),
10093 TREE_OPERAND (arg1, 1)) == 0)
10095 code = BIT_IOR_EXPR;
10096 goto bit_ior;
10099 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10100 (plus (plus (mult) (mult)) (foo)) so that we can
10101 take advantage of the factoring cases below. */
10102 if (ANY_INTEGRAL_TYPE_P (type)
10103 && TYPE_OVERFLOW_WRAPS (type)
10104 && (((TREE_CODE (arg0) == PLUS_EXPR
10105 || TREE_CODE (arg0) == MINUS_EXPR)
10106 && TREE_CODE (arg1) == MULT_EXPR)
10107 || ((TREE_CODE (arg1) == PLUS_EXPR
10108 || TREE_CODE (arg1) == MINUS_EXPR)
10109 && TREE_CODE (arg0) == MULT_EXPR)))
10111 tree parg0, parg1, parg, marg;
10112 enum tree_code pcode;
10114 if (TREE_CODE (arg1) == MULT_EXPR)
10115 parg = arg0, marg = arg1;
10116 else
10117 parg = arg1, marg = arg0;
10118 pcode = TREE_CODE (parg);
10119 parg0 = TREE_OPERAND (parg, 0);
10120 parg1 = TREE_OPERAND (parg, 1);
10121 STRIP_NOPS (parg0);
10122 STRIP_NOPS (parg1);
10124 if (TREE_CODE (parg0) == MULT_EXPR
10125 && TREE_CODE (parg1) != MULT_EXPR)
10126 return fold_build2_loc (loc, pcode, type,
10127 fold_build2_loc (loc, PLUS_EXPR, type,
10128 fold_convert_loc (loc, type,
10129 parg0),
10130 fold_convert_loc (loc, type,
10131 marg)),
10132 fold_convert_loc (loc, type, parg1));
10133 if (TREE_CODE (parg0) != MULT_EXPR
10134 && TREE_CODE (parg1) == MULT_EXPR)
10135 return
10136 fold_build2_loc (loc, PLUS_EXPR, type,
10137 fold_convert_loc (loc, type, parg0),
10138 fold_build2_loc (loc, pcode, type,
10139 fold_convert_loc (loc, type, marg),
10140 fold_convert_loc (loc, type,
10141 parg1)));
10144 else
10146 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10147 to __complex__ ( x, y ). This is not the same for SNaNs or
10148 if signed zeros are involved. */
10149 if (!HONOR_SNANS (element_mode (arg0))
10150 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10151 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10153 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10154 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10155 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10156 bool arg0rz = false, arg0iz = false;
10157 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10158 || (arg0i && (arg0iz = real_zerop (arg0i))))
10160 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10161 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10162 if (arg0rz && arg1i && real_zerop (arg1i))
10164 tree rp = arg1r ? arg1r
10165 : build1 (REALPART_EXPR, rtype, arg1);
10166 tree ip = arg0i ? arg0i
10167 : build1 (IMAGPART_EXPR, rtype, arg0);
10168 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10170 else if (arg0iz && arg1r && real_zerop (arg1r))
10172 tree rp = arg0r ? arg0r
10173 : build1 (REALPART_EXPR, rtype, arg0);
10174 tree ip = arg1i ? arg1i
10175 : build1 (IMAGPART_EXPR, rtype, arg1);
10176 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10181 if (flag_unsafe_math_optimizations
10182 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10183 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10184 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10185 return tem;
10187 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10188 We associate floats only if the user has specified
10189 -fassociative-math. */
10190 if (flag_associative_math
10191 && TREE_CODE (arg1) == PLUS_EXPR
10192 && TREE_CODE (arg0) != MULT_EXPR)
10194 tree tree10 = TREE_OPERAND (arg1, 0);
10195 tree tree11 = TREE_OPERAND (arg1, 1);
10196 if (TREE_CODE (tree11) == MULT_EXPR
10197 && TREE_CODE (tree10) == MULT_EXPR)
10199 tree tree0;
10200 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
10201 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
10204 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10205 We associate floats only if the user has specified
10206 -fassociative-math. */
10207 if (flag_associative_math
10208 && TREE_CODE (arg0) == PLUS_EXPR
10209 && TREE_CODE (arg1) != MULT_EXPR)
10211 tree tree00 = TREE_OPERAND (arg0, 0);
10212 tree tree01 = TREE_OPERAND (arg0, 1);
10213 if (TREE_CODE (tree01) == MULT_EXPR
10214 && TREE_CODE (tree00) == MULT_EXPR)
10216 tree tree0;
10217 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
10218 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
10223 bit_rotate:
10224 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10225 is a rotate of A by C1 bits. */
10226 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10227 is a rotate of A by B bits. */
10229 enum tree_code code0, code1;
10230 tree rtype;
10231 code0 = TREE_CODE (arg0);
10232 code1 = TREE_CODE (arg1);
10233 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10234 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10235 && operand_equal_p (TREE_OPERAND (arg0, 0),
10236 TREE_OPERAND (arg1, 0), 0)
10237 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10238 TYPE_UNSIGNED (rtype))
10239 /* Only create rotates in complete modes. Other cases are not
10240 expanded properly. */
10241 && (element_precision (rtype)
10242 == element_precision (TYPE_MODE (rtype))))
10244 tree tree01, tree11;
10245 enum tree_code code01, code11;
10247 tree01 = TREE_OPERAND (arg0, 1);
10248 tree11 = TREE_OPERAND (arg1, 1);
10249 STRIP_NOPS (tree01);
10250 STRIP_NOPS (tree11);
10251 code01 = TREE_CODE (tree01);
10252 code11 = TREE_CODE (tree11);
10253 if (code01 == INTEGER_CST
10254 && code11 == INTEGER_CST
10255 && (wi::to_widest (tree01) + wi::to_widest (tree11)
10256 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10258 tem = build2_loc (loc, LROTATE_EXPR,
10259 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10260 TREE_OPERAND (arg0, 0),
10261 code0 == LSHIFT_EXPR
10262 ? TREE_OPERAND (arg0, 1)
10263 : TREE_OPERAND (arg1, 1));
10264 return fold_convert_loc (loc, type, tem);
10266 else if (code11 == MINUS_EXPR)
10268 tree tree110, tree111;
10269 tree110 = TREE_OPERAND (tree11, 0);
10270 tree111 = TREE_OPERAND (tree11, 1);
10271 STRIP_NOPS (tree110);
10272 STRIP_NOPS (tree111);
10273 if (TREE_CODE (tree110) == INTEGER_CST
10274 && 0 == compare_tree_int (tree110,
10275 element_precision
10276 (TREE_TYPE (TREE_OPERAND
10277 (arg0, 0))))
10278 && operand_equal_p (tree01, tree111, 0))
10279 return
10280 fold_convert_loc (loc, type,
10281 build2 ((code0 == LSHIFT_EXPR
10282 ? LROTATE_EXPR
10283 : RROTATE_EXPR),
10284 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10285 TREE_OPERAND (arg0, 0),
10286 TREE_OPERAND (arg0, 1)));
10288 else if (code01 == MINUS_EXPR)
10290 tree tree010, tree011;
10291 tree010 = TREE_OPERAND (tree01, 0);
10292 tree011 = TREE_OPERAND (tree01, 1);
10293 STRIP_NOPS (tree010);
10294 STRIP_NOPS (tree011);
10295 if (TREE_CODE (tree010) == INTEGER_CST
10296 && 0 == compare_tree_int (tree010,
10297 element_precision
10298 (TREE_TYPE (TREE_OPERAND
10299 (arg0, 0))))
10300 && operand_equal_p (tree11, tree011, 0))
10301 return fold_convert_loc
10302 (loc, type,
10303 build2 ((code0 != LSHIFT_EXPR
10304 ? LROTATE_EXPR
10305 : RROTATE_EXPR),
10306 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10307 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
10312 associate:
10313 /* In most languages, can't associate operations on floats through
10314 parentheses. Rather than remember where the parentheses were, we
10315 don't associate floats at all, unless the user has specified
10316 -fassociative-math.
10317 And, we need to make sure type is not saturating. */
10319 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10320 && !TYPE_SATURATING (type))
10322 tree var0, con0, lit0, minus_lit0;
10323 tree var1, con1, lit1, minus_lit1;
10324 tree atype = type;
10325 bool ok = true;
10327 /* Split both trees into variables, constants, and literals. Then
10328 associate each group together, the constants with literals,
10329 then the result with variables. This increases the chances of
10330 literals being recombined later and of generating relocatable
10331 expressions for the sum of a constant and literal. */
10332 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10333 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10334 code == MINUS_EXPR);
10336 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
10337 if (code == MINUS_EXPR)
10338 code = PLUS_EXPR;
10340 /* With undefined overflow prefer doing association in a type
10341 which wraps on overflow, if that is one of the operand types. */
10342 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10343 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10345 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10346 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10347 atype = TREE_TYPE (arg0);
10348 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10349 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
10350 atype = TREE_TYPE (arg1);
10351 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
10354 /* With undefined overflow we can only associate constants with one
10355 variable, and constants whose association doesn't overflow. */
10356 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10357 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
10359 if (var0 && var1)
10361 tree tmp0 = var0;
10362 tree tmp1 = var1;
10364 if (TREE_CODE (tmp0) == NEGATE_EXPR)
10365 tmp0 = TREE_OPERAND (tmp0, 0);
10366 if (CONVERT_EXPR_P (tmp0)
10367 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
10368 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
10369 <= TYPE_PRECISION (atype)))
10370 tmp0 = TREE_OPERAND (tmp0, 0);
10371 if (TREE_CODE (tmp1) == NEGATE_EXPR)
10372 tmp1 = TREE_OPERAND (tmp1, 0);
10373 if (CONVERT_EXPR_P (tmp1)
10374 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
10375 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
10376 <= TYPE_PRECISION (atype)))
10377 tmp1 = TREE_OPERAND (tmp1, 0);
10378 /* The only case we can still associate with two variables
10379 is if they are the same, modulo negation and bit-pattern
10380 preserving conversions. */
10381 if (!operand_equal_p (tmp0, tmp1, 0))
10382 ok = false;
10386 /* Only do something if we found more than two objects. Otherwise,
10387 nothing has changed and we risk infinite recursion. */
10388 if (ok
10389 && (2 < ((var0 != 0) + (var1 != 0)
10390 + (con0 != 0) + (con1 != 0)
10391 + (lit0 != 0) + (lit1 != 0)
10392 + (minus_lit0 != 0) + (minus_lit1 != 0))))
10394 bool any_overflows = false;
10395 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
10396 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
10397 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
10398 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
10399 var0 = associate_trees (loc, var0, var1, code, atype);
10400 con0 = associate_trees (loc, con0, con1, code, atype);
10401 lit0 = associate_trees (loc, lit0, lit1, code, atype);
10402 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
10403 code, atype);
10405 /* Preserve the MINUS_EXPR if the negative part of the literal is
10406 greater than the positive part. Otherwise, the multiplicative
10407 folding code (i.e extract_muldiv) may be fooled in case
10408 unsigned constants are subtracted, like in the following
10409 example: ((X*2 + 4) - 8U)/2. */
10410 if (minus_lit0 && lit0)
10412 if (TREE_CODE (lit0) == INTEGER_CST
10413 && TREE_CODE (minus_lit0) == INTEGER_CST
10414 && tree_int_cst_lt (lit0, minus_lit0))
10416 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10417 MINUS_EXPR, atype);
10418 lit0 = 0;
10420 else
10422 lit0 = associate_trees (loc, lit0, minus_lit0,
10423 MINUS_EXPR, atype);
10424 minus_lit0 = 0;
10428 /* Don't introduce overflows through reassociation. */
10429 if (!any_overflows
10430 && ((lit0 && TREE_OVERFLOW_P (lit0))
10431 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
10432 return NULL_TREE;
10434 if (minus_lit0)
10436 if (con0 == 0)
10437 return
10438 fold_convert_loc (loc, type,
10439 associate_trees (loc, var0, minus_lit0,
10440 MINUS_EXPR, atype));
10441 else
10443 con0 = associate_trees (loc, con0, minus_lit0,
10444 MINUS_EXPR, atype);
10445 return
10446 fold_convert_loc (loc, type,
10447 associate_trees (loc, var0, con0,
10448 PLUS_EXPR, atype));
10452 con0 = associate_trees (loc, con0, lit0, code, atype);
10453 return
10454 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10455 code, atype));
10459 return NULL_TREE;
10461 case MINUS_EXPR:
10462 /* Pointer simplifications for subtraction, simple reassociations. */
10463 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10465 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10466 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10467 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10469 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10470 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10471 tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10472 tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10473 return fold_build2_loc (loc, PLUS_EXPR, type,
10474 fold_build2_loc (loc, MINUS_EXPR, type,
10475 arg00, arg10),
10476 fold_build2_loc (loc, MINUS_EXPR, type,
10477 arg01, arg11));
10479 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10480 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10482 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10483 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10484 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10485 fold_convert_loc (loc, type, arg1));
10486 if (tmp)
10487 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10489 /* PTR0 - (PTR1 p+ A) -> (PTR0 - PTR1) - A, assuming PTR0 - PTR1
10490 simplifies. */
10491 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10493 tree arg10 = fold_convert_loc (loc, type,
10494 TREE_OPERAND (arg1, 0));
10495 tree arg11 = fold_convert_loc (loc, type,
10496 TREE_OPERAND (arg1, 1));
10497 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type,
10498 fold_convert_loc (loc, type, arg0),
10499 arg10);
10500 if (tmp)
10501 return fold_build2_loc (loc, MINUS_EXPR, type, tmp, arg11);
10504 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10505 if (TREE_CODE (arg0) == NEGATE_EXPR
10506 && negate_expr_p (arg1)
10507 && reorder_operands_p (arg0, arg1))
10508 return fold_build2_loc (loc, MINUS_EXPR, type,
10509 fold_convert_loc (loc, type,
10510 negate_expr (arg1)),
10511 fold_convert_loc (loc, type,
10512 TREE_OPERAND (arg0, 0)));
10514 /* X - (X / Y) * Y is X % Y. */
10515 if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
10516 && TREE_CODE (arg1) == MULT_EXPR
10517 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10518 && operand_equal_p (arg0,
10519 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10520 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10521 TREE_OPERAND (arg1, 1), 0))
10522 return
10523 fold_convert_loc (loc, type,
10524 fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10525 arg0, TREE_OPERAND (arg1, 1)));
10527 if (! FLOAT_TYPE_P (type))
10529 /* Fold A - (A & B) into ~B & A. */
10530 if (!TREE_SIDE_EFFECTS (arg0)
10531 && TREE_CODE (arg1) == BIT_AND_EXPR)
10533 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10535 tree arg10 = fold_convert_loc (loc, type,
10536 TREE_OPERAND (arg1, 0));
10537 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10538 fold_build1_loc (loc, BIT_NOT_EXPR,
10539 type, arg10),
10540 fold_convert_loc (loc, type, arg0));
10542 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10544 tree arg11 = fold_convert_loc (loc,
10545 type, TREE_OPERAND (arg1, 1));
10546 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10547 fold_build1_loc (loc, BIT_NOT_EXPR,
10548 type, arg11),
10549 fold_convert_loc (loc, type, arg0));
10553 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10554 any power of 2 minus 1. */
10555 if (TREE_CODE (arg0) == BIT_AND_EXPR
10556 && TREE_CODE (arg1) == BIT_AND_EXPR
10557 && operand_equal_p (TREE_OPERAND (arg0, 0),
10558 TREE_OPERAND (arg1, 0), 0))
10560 tree mask0 = TREE_OPERAND (arg0, 1);
10561 tree mask1 = TREE_OPERAND (arg1, 1);
10562 tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10564 if (operand_equal_p (tem, mask1, 0))
10566 tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10567 TREE_OPERAND (arg0, 0), mask1);
10568 return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10573 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10574 __complex__ ( x, -y ). This is not the same for SNaNs or if
10575 signed zeros are involved. */
10576 if (!HONOR_SNANS (element_mode (arg0))
10577 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10578 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10580 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10581 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10582 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10583 bool arg0rz = false, arg0iz = false;
10584 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10585 || (arg0i && (arg0iz = real_zerop (arg0i))))
10587 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10588 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10589 if (arg0rz && arg1i && real_zerop (arg1i))
10591 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10592 arg1r ? arg1r
10593 : build1 (REALPART_EXPR, rtype, arg1));
10594 tree ip = arg0i ? arg0i
10595 : build1 (IMAGPART_EXPR, rtype, arg0);
10596 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10598 else if (arg0iz && arg1r && real_zerop (arg1r))
10600 tree rp = arg0r ? arg0r
10601 : build1 (REALPART_EXPR, rtype, arg0);
10602 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10603 arg1i ? arg1i
10604 : build1 (IMAGPART_EXPR, rtype, arg1));
10605 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10610 /* A - B -> A + (-B) if B is easily negatable. */
10611 if (negate_expr_p (arg1)
10612 && !TYPE_OVERFLOW_SANITIZED (type)
10613 && ((FLOAT_TYPE_P (type)
10614 /* Avoid this transformation if B is a positive REAL_CST. */
10615 && (TREE_CODE (arg1) != REAL_CST
10616 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10617 || INTEGRAL_TYPE_P (type)))
10618 return fold_build2_loc (loc, PLUS_EXPR, type,
10619 fold_convert_loc (loc, type, arg0),
10620 fold_convert_loc (loc, type,
10621 negate_expr (arg1)));
10623 /* Try folding difference of addresses. */
10625 HOST_WIDE_INT diff;
10627 if ((TREE_CODE (arg0) == ADDR_EXPR
10628 || TREE_CODE (arg1) == ADDR_EXPR)
10629 && ptr_difference_const (arg0, arg1, &diff))
10630 return build_int_cst_type (type, diff);
10633 /* Fold &a[i] - &a[j] to i-j. */
10634 if (TREE_CODE (arg0) == ADDR_EXPR
10635 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10636 && TREE_CODE (arg1) == ADDR_EXPR
10637 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10639 tree tem = fold_addr_of_array_ref_difference (loc, type,
10640 TREE_OPERAND (arg0, 0),
10641 TREE_OPERAND (arg1, 0));
10642 if (tem)
10643 return tem;
10646 if (FLOAT_TYPE_P (type)
10647 && flag_unsafe_math_optimizations
10648 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10649 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10650 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10651 return tem;
10653 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
10654 one. Make sure the type is not saturating and has the signedness of
10655 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10656 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10657 if ((TREE_CODE (arg0) == MULT_EXPR
10658 || TREE_CODE (arg1) == MULT_EXPR)
10659 && !TYPE_SATURATING (type)
10660 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10661 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10662 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10664 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10665 if (tem)
10666 return tem;
10669 goto associate;
10671 case MULT_EXPR:
10672 /* (-A) * (-B) -> A * B */
10673 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10674 return fold_build2_loc (loc, MULT_EXPR, type,
10675 fold_convert_loc (loc, type,
10676 TREE_OPERAND (arg0, 0)),
10677 fold_convert_loc (loc, type,
10678 negate_expr (arg1)));
10679 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10680 return fold_build2_loc (loc, MULT_EXPR, type,
10681 fold_convert_loc (loc, type,
10682 negate_expr (arg0)),
10683 fold_convert_loc (loc, type,
10684 TREE_OPERAND (arg1, 0)));
10686 if (! FLOAT_TYPE_P (type))
10688 /* Transform x * -C into -x * C if x is easily negatable. */
10689 if (TREE_CODE (arg1) == INTEGER_CST
10690 && tree_int_cst_sgn (arg1) == -1
10691 && negate_expr_p (arg0)
10692 && (tem = negate_expr (arg1)) != arg1
10693 && !TREE_OVERFLOW (tem))
10694 return fold_build2_loc (loc, MULT_EXPR, type,
10695 fold_convert_loc (loc, type,
10696 negate_expr (arg0)),
10697 tem);
10699 /* (a * (1 << b)) is (a << b) */
10700 if (TREE_CODE (arg1) == LSHIFT_EXPR
10701 && integer_onep (TREE_OPERAND (arg1, 0)))
10702 return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10703 TREE_OPERAND (arg1, 1));
10704 if (TREE_CODE (arg0) == LSHIFT_EXPR
10705 && integer_onep (TREE_OPERAND (arg0, 0)))
10706 return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10707 TREE_OPERAND (arg0, 1));
10709 /* (A + A) * C -> A * 2 * C */
10710 if (TREE_CODE (arg0) == PLUS_EXPR
10711 && TREE_CODE (arg1) == INTEGER_CST
10712 && operand_equal_p (TREE_OPERAND (arg0, 0),
10713 TREE_OPERAND (arg0, 1), 0))
10714 return fold_build2_loc (loc, MULT_EXPR, type,
10715 omit_one_operand_loc (loc, type,
10716 TREE_OPERAND (arg0, 0),
10717 TREE_OPERAND (arg0, 1)),
10718 fold_build2_loc (loc, MULT_EXPR, type,
10719 build_int_cst (type, 2) , arg1));
10721 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
10722 sign-changing only. */
10723 if (TREE_CODE (arg1) == INTEGER_CST
10724 && TREE_CODE (arg0) == EXACT_DIV_EXPR
10725 && operand_equal_p (arg1, TREE_OPERAND (arg0, 1), 0))
10726 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10728 strict_overflow_p = false;
10729 if (TREE_CODE (arg1) == INTEGER_CST
10730 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10731 &strict_overflow_p)))
10733 if (strict_overflow_p)
10734 fold_overflow_warning (("assuming signed overflow does not "
10735 "occur when simplifying "
10736 "multiplication"),
10737 WARN_STRICT_OVERFLOW_MISC);
10738 return fold_convert_loc (loc, type, tem);
10741 /* Optimize z * conj(z) for integer complex numbers. */
10742 if (TREE_CODE (arg0) == CONJ_EXPR
10743 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10744 return fold_mult_zconjz (loc, type, arg1);
10745 if (TREE_CODE (arg1) == CONJ_EXPR
10746 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10747 return fold_mult_zconjz (loc, type, arg0);
10749 else
10751 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10752 the result for floating point types due to rounding so it is applied
10753 only if -fassociative-math was specify. */
10754 if (flag_associative_math
10755 && TREE_CODE (arg0) == RDIV_EXPR
10756 && TREE_CODE (arg1) == REAL_CST
10757 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10759 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10760 arg1);
10761 if (tem)
10762 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10763 TREE_OPERAND (arg0, 1));
10766 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10767 if (operand_equal_p (arg0, arg1, 0))
10769 tree tem = fold_strip_sign_ops (arg0);
10770 if (tem != NULL_TREE)
10772 tem = fold_convert_loc (loc, type, tem);
10773 return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10777 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10778 This is not the same for NaNs or if signed zeros are
10779 involved. */
10780 if (!HONOR_NANS (arg0)
10781 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10782 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10783 && TREE_CODE (arg1) == COMPLEX_CST
10784 && real_zerop (TREE_REALPART (arg1)))
10786 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10787 if (real_onep (TREE_IMAGPART (arg1)))
10788 return
10789 fold_build2_loc (loc, COMPLEX_EXPR, type,
10790 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10791 rtype, arg0)),
10792 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10793 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10794 return
10795 fold_build2_loc (loc, COMPLEX_EXPR, type,
10796 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10797 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10798 rtype, arg0)));
10801 /* Optimize z * conj(z) for floating point complex numbers.
10802 Guarded by flag_unsafe_math_optimizations as non-finite
10803 imaginary components don't produce scalar results. */
10804 if (flag_unsafe_math_optimizations
10805 && TREE_CODE (arg0) == CONJ_EXPR
10806 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10807 return fold_mult_zconjz (loc, type, arg1);
10808 if (flag_unsafe_math_optimizations
10809 && TREE_CODE (arg1) == CONJ_EXPR
10810 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10811 return fold_mult_zconjz (loc, type, arg0);
10813 if (flag_unsafe_math_optimizations)
10815 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10816 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10818 /* Optimizations of root(...)*root(...). */
10819 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10821 tree rootfn, arg;
10822 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10823 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10825 /* Optimize sqrt(x)*sqrt(x) as x. */
10826 if (BUILTIN_SQRT_P (fcode0)
10827 && operand_equal_p (arg00, arg10, 0)
10828 && ! HONOR_SNANS (element_mode (type)))
10829 return arg00;
10831 /* Optimize root(x)*root(y) as root(x*y). */
10832 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10833 arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10834 return build_call_expr_loc (loc, rootfn, 1, arg);
10837 /* Optimize expN(x)*expN(y) as expN(x+y). */
10838 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10840 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10841 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10842 CALL_EXPR_ARG (arg0, 0),
10843 CALL_EXPR_ARG (arg1, 0));
10844 return build_call_expr_loc (loc, expfn, 1, arg);
10847 /* Optimizations of pow(...)*pow(...). */
10848 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10849 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10850 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10852 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10853 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10854 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10855 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10857 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10858 if (operand_equal_p (arg01, arg11, 0))
10860 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10861 tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10862 arg00, arg10);
10863 return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10866 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10867 if (operand_equal_p (arg00, arg10, 0))
10869 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10870 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10871 arg01, arg11);
10872 return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10876 /* Optimize tan(x)*cos(x) as sin(x). */
10877 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10878 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10879 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10880 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10881 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10882 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10883 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10884 CALL_EXPR_ARG (arg1, 0), 0))
10886 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10888 if (sinfn != NULL_TREE)
10889 return build_call_expr_loc (loc, sinfn, 1,
10890 CALL_EXPR_ARG (arg0, 0));
10893 /* Optimize x*pow(x,c) as pow(x,c+1). */
10894 if (fcode1 == BUILT_IN_POW
10895 || fcode1 == BUILT_IN_POWF
10896 || fcode1 == BUILT_IN_POWL)
10898 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10899 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10900 if (TREE_CODE (arg11) == REAL_CST
10901 && !TREE_OVERFLOW (arg11)
10902 && operand_equal_p (arg0, arg10, 0))
10904 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10905 REAL_VALUE_TYPE c;
10906 tree arg;
10908 c = TREE_REAL_CST (arg11);
10909 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10910 arg = build_real (type, c);
10911 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10915 /* Optimize pow(x,c)*x as pow(x,c+1). */
10916 if (fcode0 == BUILT_IN_POW
10917 || fcode0 == BUILT_IN_POWF
10918 || fcode0 == BUILT_IN_POWL)
10920 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10921 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10922 if (TREE_CODE (arg01) == REAL_CST
10923 && !TREE_OVERFLOW (arg01)
10924 && operand_equal_p (arg1, arg00, 0))
10926 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10927 REAL_VALUE_TYPE c;
10928 tree arg;
10930 c = TREE_REAL_CST (arg01);
10931 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10932 arg = build_real (type, c);
10933 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10937 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
10938 if (!in_gimple_form
10939 && optimize
10940 && operand_equal_p (arg0, arg1, 0))
10942 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10944 if (powfn)
10946 tree arg = build_real (type, dconst2);
10947 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10952 goto associate;
10954 case BIT_IOR_EXPR:
10955 bit_ior:
10956 /* ~X | X is -1. */
10957 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10958 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10960 t1 = build_zero_cst (type);
10961 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10962 return omit_one_operand_loc (loc, type, t1, arg1);
10965 /* X | ~X is -1. */
10966 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10967 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10969 t1 = build_zero_cst (type);
10970 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10971 return omit_one_operand_loc (loc, type, t1, arg0);
10974 /* Canonicalize (X & C1) | C2. */
10975 if (TREE_CODE (arg0) == BIT_AND_EXPR
10976 && TREE_CODE (arg1) == INTEGER_CST
10977 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10979 int width = TYPE_PRECISION (type), w;
10980 wide_int c1 = TREE_OPERAND (arg0, 1);
10981 wide_int c2 = arg1;
10983 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10984 if ((c1 & c2) == c1)
10985 return omit_one_operand_loc (loc, type, arg1,
10986 TREE_OPERAND (arg0, 0));
10988 wide_int msk = wi::mask (width, false,
10989 TYPE_PRECISION (TREE_TYPE (arg1)));
10991 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10992 if (msk.and_not (c1 | c2) == 0)
10993 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10994 TREE_OPERAND (arg0, 0), arg1);
10996 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10997 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10998 mode which allows further optimizations. */
10999 c1 &= msk;
11000 c2 &= msk;
11001 wide_int c3 = c1.and_not (c2);
11002 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11004 wide_int mask = wi::mask (w, false,
11005 TYPE_PRECISION (type));
11006 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
11008 c3 = mask;
11009 break;
11013 if (c3 != c1)
11014 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11015 fold_build2_loc (loc, BIT_AND_EXPR, type,
11016 TREE_OPERAND (arg0, 0),
11017 wide_int_to_tree (type,
11018 c3)),
11019 arg1);
11022 /* (X & ~Y) | (~X & Y) is X ^ Y */
11023 if (TREE_CODE (arg0) == BIT_AND_EXPR
11024 && TREE_CODE (arg1) == BIT_AND_EXPR)
11026 tree a0, a1, l0, l1, n0, n1;
11028 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11029 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11031 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11032 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11034 n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
11035 n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
11037 if ((operand_equal_p (n0, a0, 0)
11038 && operand_equal_p (n1, a1, 0))
11039 || (operand_equal_p (n0, a1, 0)
11040 && operand_equal_p (n1, a0, 0)))
11041 return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
11044 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11045 if (t1 != NULL_TREE)
11046 return t1;
11048 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
11050 This results in more efficient code for machines without a NAND
11051 instruction. Combine will canonicalize to the first form
11052 which will allow use of NAND instructions provided by the
11053 backend if they exist. */
11054 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11055 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11057 return
11058 fold_build1_loc (loc, BIT_NOT_EXPR, type,
11059 build2 (BIT_AND_EXPR, type,
11060 fold_convert_loc (loc, type,
11061 TREE_OPERAND (arg0, 0)),
11062 fold_convert_loc (loc, type,
11063 TREE_OPERAND (arg1, 0))));
11066 /* See if this can be simplified into a rotate first. If that
11067 is unsuccessful continue in the association code. */
11068 goto bit_rotate;
11070 case BIT_XOR_EXPR:
11071 /* ~X ^ X is -1. */
11072 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11073 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11075 t1 = build_zero_cst (type);
11076 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11077 return omit_one_operand_loc (loc, type, t1, arg1);
11080 /* X ^ ~X is -1. */
11081 if (TREE_CODE (arg1) == BIT_NOT_EXPR
11082 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11084 t1 = build_zero_cst (type);
11085 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11086 return omit_one_operand_loc (loc, type, t1, arg0);
11089 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11090 with a constant, and the two constants have no bits in common,
11091 we should treat this as a BIT_IOR_EXPR since this may produce more
11092 simplifications. */
11093 if (TREE_CODE (arg0) == BIT_AND_EXPR
11094 && TREE_CODE (arg1) == BIT_AND_EXPR
11095 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11096 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11097 && wi::bit_and (TREE_OPERAND (arg0, 1),
11098 TREE_OPERAND (arg1, 1)) == 0)
11100 code = BIT_IOR_EXPR;
11101 goto bit_ior;
11104 /* (X | Y) ^ X -> Y & ~ X*/
11105 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11106 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11108 tree t2 = TREE_OPERAND (arg0, 1);
11109 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11110 arg1);
11111 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11112 fold_convert_loc (loc, type, t2),
11113 fold_convert_loc (loc, type, t1));
11114 return t1;
11117 /* (Y | X) ^ X -> Y & ~ X*/
11118 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11119 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11121 tree t2 = TREE_OPERAND (arg0, 0);
11122 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11123 arg1);
11124 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11125 fold_convert_loc (loc, type, t2),
11126 fold_convert_loc (loc, type, t1));
11127 return t1;
11130 /* X ^ (X | Y) -> Y & ~ X*/
11131 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11132 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11134 tree t2 = TREE_OPERAND (arg1, 1);
11135 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11136 arg0);
11137 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11138 fold_convert_loc (loc, type, t2),
11139 fold_convert_loc (loc, type, t1));
11140 return t1;
11143 /* X ^ (Y | X) -> Y & ~ X*/
11144 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11145 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11147 tree t2 = TREE_OPERAND (arg1, 0);
11148 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11149 arg0);
11150 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11151 fold_convert_loc (loc, type, t2),
11152 fold_convert_loc (loc, type, t1));
11153 return t1;
11156 /* Convert ~X ^ ~Y to X ^ Y. */
11157 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11158 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11159 return fold_build2_loc (loc, code, type,
11160 fold_convert_loc (loc, type,
11161 TREE_OPERAND (arg0, 0)),
11162 fold_convert_loc (loc, type,
11163 TREE_OPERAND (arg1, 0)));
11165 /* Convert ~X ^ C to X ^ ~C. */
11166 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11167 && TREE_CODE (arg1) == INTEGER_CST)
11168 return fold_build2_loc (loc, code, type,
11169 fold_convert_loc (loc, type,
11170 TREE_OPERAND (arg0, 0)),
11171 fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
11173 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11174 if (TREE_CODE (arg0) == BIT_AND_EXPR
11175 && INTEGRAL_TYPE_P (type)
11176 && integer_onep (TREE_OPERAND (arg0, 1))
11177 && integer_onep (arg1))
11178 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11179 build_zero_cst (TREE_TYPE (arg0)));
11181 /* Fold (X & Y) ^ Y as ~X & Y. */
11182 if (TREE_CODE (arg0) == BIT_AND_EXPR
11183 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11185 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11186 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11187 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11188 fold_convert_loc (loc, type, arg1));
11190 /* Fold (X & Y) ^ X as ~Y & X. */
11191 if (TREE_CODE (arg0) == BIT_AND_EXPR
11192 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11193 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11195 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11196 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11197 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11198 fold_convert_loc (loc, type, arg1));
11200 /* Fold X ^ (X & Y) as X & ~Y. */
11201 if (TREE_CODE (arg1) == BIT_AND_EXPR
11202 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11204 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11205 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11206 fold_convert_loc (loc, type, arg0),
11207 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11209 /* Fold X ^ (Y & X) as ~Y & X. */
11210 if (TREE_CODE (arg1) == BIT_AND_EXPR
11211 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11212 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11214 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11215 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11216 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11217 fold_convert_loc (loc, type, arg0));
11220 /* See if this can be simplified into a rotate first. If that
11221 is unsuccessful continue in the association code. */
11222 goto bit_rotate;
11224 case BIT_AND_EXPR:
11225 /* ~X & X, (X == 0) & X, and !X & X are always zero. */
11226 if ((TREE_CODE (arg0) == BIT_NOT_EXPR
11227 || TREE_CODE (arg0) == TRUTH_NOT_EXPR
11228 || (TREE_CODE (arg0) == EQ_EXPR
11229 && integer_zerop (TREE_OPERAND (arg0, 1))))
11230 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11231 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11233 /* X & ~X , X & (X == 0), and X & !X are always zero. */
11234 if ((TREE_CODE (arg1) == BIT_NOT_EXPR
11235 || TREE_CODE (arg1) == TRUTH_NOT_EXPR
11236 || (TREE_CODE (arg1) == EQ_EXPR
11237 && integer_zerop (TREE_OPERAND (arg1, 1))))
11238 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11239 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11241 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11242 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11243 && INTEGRAL_TYPE_P (type)
11244 && integer_onep (TREE_OPERAND (arg0, 1))
11245 && integer_onep (arg1))
11247 tree tem2;
11248 tem = TREE_OPERAND (arg0, 0);
11249 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
11250 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
11251 tem, tem2);
11252 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
11253 build_zero_cst (TREE_TYPE (tem)));
11255 /* Fold ~X & 1 as (X & 1) == 0. */
11256 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11257 && INTEGRAL_TYPE_P (type)
11258 && integer_onep (arg1))
11260 tree tem2;
11261 tem = TREE_OPERAND (arg0, 0);
11262 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
11263 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
11264 tem, tem2);
11265 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
11266 build_zero_cst (TREE_TYPE (tem)));
11268 /* Fold !X & 1 as X == 0. */
11269 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11270 && integer_onep (arg1))
11272 tem = TREE_OPERAND (arg0, 0);
11273 return fold_build2_loc (loc, EQ_EXPR, type, tem,
11274 build_zero_cst (TREE_TYPE (tem)));
11277 /* Fold (X ^ Y) & Y as ~X & Y. */
11278 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11279 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11281 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11282 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11283 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11284 fold_convert_loc (loc, type, arg1));
11286 /* Fold (X ^ Y) & X as ~Y & X. */
11287 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11288 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11289 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11291 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11292 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11293 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11294 fold_convert_loc (loc, type, arg1));
11296 /* Fold X & (X ^ Y) as X & ~Y. */
11297 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11298 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11300 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11301 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11302 fold_convert_loc (loc, type, arg0),
11303 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11305 /* Fold X & (Y ^ X) as ~Y & X. */
11306 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11307 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11308 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11310 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11311 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11312 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11313 fold_convert_loc (loc, type, arg0));
11316 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11317 multiple of 1 << CST. */
11318 if (TREE_CODE (arg1) == INTEGER_CST)
11320 wide_int cst1 = arg1;
11321 wide_int ncst1 = -cst1;
11322 if ((cst1 & ncst1) == ncst1
11323 && multiple_of_p (type, arg0,
11324 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11325 return fold_convert_loc (loc, type, arg0);
11328 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11329 bits from CST2. */
11330 if (TREE_CODE (arg1) == INTEGER_CST
11331 && TREE_CODE (arg0) == MULT_EXPR
11332 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11334 wide_int warg1 = arg1;
11335 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
11337 if (masked == 0)
11338 return omit_two_operands_loc (loc, type, build_zero_cst (type),
11339 arg0, arg1);
11340 else if (masked != warg1)
11342 /* Avoid the transform if arg1 is a mask of some
11343 mode which allows further optimizations. */
11344 int pop = wi::popcount (warg1);
11345 if (!(pop >= BITS_PER_UNIT
11346 && exact_log2 (pop) != -1
11347 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11348 return fold_build2_loc (loc, code, type, op0,
11349 wide_int_to_tree (type, masked));
11353 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11354 ((A & N) + B) & M -> (A + B) & M
11355 Similarly if (N & M) == 0,
11356 ((A | N) + B) & M -> (A + B) & M
11357 and for - instead of + (or unary - instead of +)
11358 and/or ^ instead of |.
11359 If B is constant and (B & M) == 0, fold into A & M. */
11360 if (TREE_CODE (arg1) == INTEGER_CST)
11362 wide_int cst1 = arg1;
11363 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
11364 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11365 && (TREE_CODE (arg0) == PLUS_EXPR
11366 || TREE_CODE (arg0) == MINUS_EXPR
11367 || TREE_CODE (arg0) == NEGATE_EXPR)
11368 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11369 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11371 tree pmop[2];
11372 int which = 0;
11373 wide_int cst0;
11375 /* Now we know that arg0 is (C + D) or (C - D) or
11376 -C and arg1 (M) is == (1LL << cst) - 1.
11377 Store C into PMOP[0] and D into PMOP[1]. */
11378 pmop[0] = TREE_OPERAND (arg0, 0);
11379 pmop[1] = NULL;
11380 if (TREE_CODE (arg0) != NEGATE_EXPR)
11382 pmop[1] = TREE_OPERAND (arg0, 1);
11383 which = 1;
11386 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
11387 which = -1;
11389 for (; which >= 0; which--)
11390 switch (TREE_CODE (pmop[which]))
11392 case BIT_AND_EXPR:
11393 case BIT_IOR_EXPR:
11394 case BIT_XOR_EXPR:
11395 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11396 != INTEGER_CST)
11397 break;
11398 cst0 = TREE_OPERAND (pmop[which], 1);
11399 cst0 &= cst1;
11400 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11402 if (cst0 != cst1)
11403 break;
11405 else if (cst0 != 0)
11406 break;
11407 /* If C or D is of the form (A & N) where
11408 (N & M) == M, or of the form (A | N) or
11409 (A ^ N) where (N & M) == 0, replace it with A. */
11410 pmop[which] = TREE_OPERAND (pmop[which], 0);
11411 break;
11412 case INTEGER_CST:
11413 /* If C or D is a N where (N & M) == 0, it can be
11414 omitted (assumed 0). */
11415 if ((TREE_CODE (arg0) == PLUS_EXPR
11416 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11417 && (cst1 & pmop[which]) == 0)
11418 pmop[which] = NULL;
11419 break;
11420 default:
11421 break;
11424 /* Only build anything new if we optimized one or both arguments
11425 above. */
11426 if (pmop[0] != TREE_OPERAND (arg0, 0)
11427 || (TREE_CODE (arg0) != NEGATE_EXPR
11428 && pmop[1] != TREE_OPERAND (arg0, 1)))
11430 tree utype = TREE_TYPE (arg0);
11431 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11433 /* Perform the operations in a type that has defined
11434 overflow behavior. */
11435 utype = unsigned_type_for (TREE_TYPE (arg0));
11436 if (pmop[0] != NULL)
11437 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11438 if (pmop[1] != NULL)
11439 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11442 if (TREE_CODE (arg0) == NEGATE_EXPR)
11443 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11444 else if (TREE_CODE (arg0) == PLUS_EXPR)
11446 if (pmop[0] != NULL && pmop[1] != NULL)
11447 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11448 pmop[0], pmop[1]);
11449 else if (pmop[0] != NULL)
11450 tem = pmop[0];
11451 else if (pmop[1] != NULL)
11452 tem = pmop[1];
11453 else
11454 return build_int_cst (type, 0);
11456 else if (pmop[0] == NULL)
11457 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11458 else
11459 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11460 pmop[0], pmop[1]);
11461 /* TEM is now the new binary +, - or unary - replacement. */
11462 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11463 fold_convert_loc (loc, utype, arg1));
11464 return fold_convert_loc (loc, type, tem);
11469 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11470 if (t1 != NULL_TREE)
11471 return t1;
11472 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11473 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11474 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11476 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11478 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
11479 if (mask == -1)
11480 return
11481 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11484 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11486 This results in more efficient code for machines without a NOR
11487 instruction. Combine will canonicalize to the first form
11488 which will allow use of NOR instructions provided by the
11489 backend if they exist. */
11490 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11491 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11493 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11494 build2 (BIT_IOR_EXPR, type,
11495 fold_convert_loc (loc, type,
11496 TREE_OPERAND (arg0, 0)),
11497 fold_convert_loc (loc, type,
11498 TREE_OPERAND (arg1, 0))));
11501 /* If arg0 is derived from the address of an object or function, we may
11502 be able to fold this expression using the object or function's
11503 alignment. */
11504 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && tree_fits_uhwi_p (arg1))
11506 unsigned HOST_WIDE_INT modulus, residue;
11507 unsigned HOST_WIDE_INT low = tree_to_uhwi (arg1);
11509 modulus = get_pointer_modulus_and_residue (arg0, &residue,
11510 integer_onep (arg1));
11512 /* This works because modulus is a power of 2. If this weren't the
11513 case, we'd have to replace it by its greatest power-of-2
11514 divisor: modulus & -modulus. */
11515 if (low < modulus)
11516 return build_int_cst (type, residue & low);
11519 goto associate;
11521 case RDIV_EXPR:
11522 /* Don't touch a floating-point divide by zero unless the mode
11523 of the constant can represent infinity. */
11524 if (TREE_CODE (arg1) == REAL_CST
11525 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11526 && real_zerop (arg1))
11527 return NULL_TREE;
11529 /* (-A) / (-B) -> A / B */
11530 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11531 return fold_build2_loc (loc, RDIV_EXPR, type,
11532 TREE_OPERAND (arg0, 0),
11533 negate_expr (arg1));
11534 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11535 return fold_build2_loc (loc, RDIV_EXPR, type,
11536 negate_expr (arg0),
11537 TREE_OPERAND (arg1, 0));
11539 /* Convert A/B/C to A/(B*C). */
11540 if (flag_reciprocal_math
11541 && TREE_CODE (arg0) == RDIV_EXPR)
11542 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11543 fold_build2_loc (loc, MULT_EXPR, type,
11544 TREE_OPERAND (arg0, 1), arg1));
11546 /* Convert A/(B/C) to (A/B)*C. */
11547 if (flag_reciprocal_math
11548 && TREE_CODE (arg1) == RDIV_EXPR)
11549 return fold_build2_loc (loc, MULT_EXPR, type,
11550 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11551 TREE_OPERAND (arg1, 0)),
11552 TREE_OPERAND (arg1, 1));
11554 /* Convert C1/(X*C2) into (C1/C2)/X. */
11555 if (flag_reciprocal_math
11556 && TREE_CODE (arg1) == MULT_EXPR
11557 && TREE_CODE (arg0) == REAL_CST
11558 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11560 tree tem = const_binop (RDIV_EXPR, arg0,
11561 TREE_OPERAND (arg1, 1));
11562 if (tem)
11563 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11564 TREE_OPERAND (arg1, 0));
11567 if (flag_unsafe_math_optimizations)
11569 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11570 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11572 /* Optimize sin(x)/cos(x) as tan(x). */
11573 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11574 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11575 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11576 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11577 CALL_EXPR_ARG (arg1, 0), 0))
11579 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11581 if (tanfn != NULL_TREE)
11582 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11585 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11586 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11587 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11588 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11589 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11590 CALL_EXPR_ARG (arg1, 0), 0))
11592 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11594 if (tanfn != NULL_TREE)
11596 tree tmp = build_call_expr_loc (loc, tanfn, 1,
11597 CALL_EXPR_ARG (arg0, 0));
11598 return fold_build2_loc (loc, RDIV_EXPR, type,
11599 build_real (type, dconst1), tmp);
11603 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11604 NaNs or Infinities. */
11605 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11606 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11607 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11609 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11610 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11612 if (! HONOR_NANS (arg00)
11613 && ! HONOR_INFINITIES (element_mode (arg00))
11614 && operand_equal_p (arg00, arg01, 0))
11616 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11618 if (cosfn != NULL_TREE)
11619 return build_call_expr_loc (loc, cosfn, 1, arg00);
11623 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11624 NaNs or Infinities. */
11625 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11626 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11627 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11629 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11630 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11632 if (! HONOR_NANS (arg00)
11633 && ! HONOR_INFINITIES (element_mode (arg00))
11634 && operand_equal_p (arg00, arg01, 0))
11636 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11638 if (cosfn != NULL_TREE)
11640 tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11641 return fold_build2_loc (loc, RDIV_EXPR, type,
11642 build_real (type, dconst1),
11643 tmp);
11648 /* Optimize pow(x,c)/x as pow(x,c-1). */
11649 if (fcode0 == BUILT_IN_POW
11650 || fcode0 == BUILT_IN_POWF
11651 || fcode0 == BUILT_IN_POWL)
11653 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11654 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11655 if (TREE_CODE (arg01) == REAL_CST
11656 && !TREE_OVERFLOW (arg01)
11657 && operand_equal_p (arg1, arg00, 0))
11659 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11660 REAL_VALUE_TYPE c;
11661 tree arg;
11663 c = TREE_REAL_CST (arg01);
11664 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11665 arg = build_real (type, c);
11666 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11670 /* Optimize a/root(b/c) into a*root(c/b). */
11671 if (BUILTIN_ROOT_P (fcode1))
11673 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11675 if (TREE_CODE (rootarg) == RDIV_EXPR)
11677 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11678 tree b = TREE_OPERAND (rootarg, 0);
11679 tree c = TREE_OPERAND (rootarg, 1);
11681 tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11683 tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11684 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11688 /* Optimize x/expN(y) into x*expN(-y). */
11689 if (BUILTIN_EXPONENT_P (fcode1))
11691 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11692 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11693 arg1 = build_call_expr_loc (loc,
11694 expfn, 1,
11695 fold_convert_loc (loc, type, arg));
11696 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11699 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11700 if (fcode1 == BUILT_IN_POW
11701 || fcode1 == BUILT_IN_POWF
11702 || fcode1 == BUILT_IN_POWL)
11704 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11705 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11706 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11707 tree neg11 = fold_convert_loc (loc, type,
11708 negate_expr (arg11));
11709 arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11710 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11713 return NULL_TREE;
11715 case TRUNC_DIV_EXPR:
11716 /* Optimize (X & (-A)) / A where A is a power of 2,
11717 to X >> log2(A) */
11718 if (TREE_CODE (arg0) == BIT_AND_EXPR
11719 && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
11720 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
11722 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
11723 arg1, TREE_OPERAND (arg0, 1));
11724 if (sum && integer_zerop (sum)) {
11725 tree pow2 = build_int_cst (integer_type_node,
11726 wi::exact_log2 (arg1));
11727 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11728 TREE_OPERAND (arg0, 0), pow2);
11732 /* Fall through */
11734 case FLOOR_DIV_EXPR:
11735 /* Simplify A / (B << N) where A and B are positive and B is
11736 a power of 2, to A >> (N + log2(B)). */
11737 strict_overflow_p = false;
11738 if (TREE_CODE (arg1) == LSHIFT_EXPR
11739 && (TYPE_UNSIGNED (type)
11740 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11742 tree sval = TREE_OPERAND (arg1, 0);
11743 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11745 tree sh_cnt = TREE_OPERAND (arg1, 1);
11746 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
11747 wi::exact_log2 (sval));
11749 if (strict_overflow_p)
11750 fold_overflow_warning (("assuming signed overflow does not "
11751 "occur when simplifying A / (B << N)"),
11752 WARN_STRICT_OVERFLOW_MISC);
11754 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11755 sh_cnt, pow2);
11756 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11757 fold_convert_loc (loc, type, arg0), sh_cnt);
11761 /* Fall through */
11763 case ROUND_DIV_EXPR:
11764 case CEIL_DIV_EXPR:
11765 case EXACT_DIV_EXPR:
11766 if (integer_zerop (arg1))
11767 return NULL_TREE;
11769 /* Convert -A / -B to A / B when the type is signed and overflow is
11770 undefined. */
11771 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11772 && TREE_CODE (arg0) == NEGATE_EXPR
11773 && negate_expr_p (arg1))
11775 if (INTEGRAL_TYPE_P (type))
11776 fold_overflow_warning (("assuming signed overflow does not occur "
11777 "when distributing negation across "
11778 "division"),
11779 WARN_STRICT_OVERFLOW_MISC);
11780 return fold_build2_loc (loc, code, type,
11781 fold_convert_loc (loc, type,
11782 TREE_OPERAND (arg0, 0)),
11783 fold_convert_loc (loc, type,
11784 negate_expr (arg1)));
11786 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11787 && TREE_CODE (arg1) == NEGATE_EXPR
11788 && negate_expr_p (arg0))
11790 if (INTEGRAL_TYPE_P (type))
11791 fold_overflow_warning (("assuming signed overflow does not occur "
11792 "when distributing negation across "
11793 "division"),
11794 WARN_STRICT_OVERFLOW_MISC);
11795 return fold_build2_loc (loc, code, type,
11796 fold_convert_loc (loc, type,
11797 negate_expr (arg0)),
11798 fold_convert_loc (loc, type,
11799 TREE_OPERAND (arg1, 0)));
11802 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11803 operation, EXACT_DIV_EXPR.
11805 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11806 At one time others generated faster code, it's not clear if they do
11807 after the last round to changes to the DIV code in expmed.c. */
11808 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11809 && multiple_of_p (type, arg0, arg1))
11810 return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11812 strict_overflow_p = false;
11813 if (TREE_CODE (arg1) == INTEGER_CST
11814 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11815 &strict_overflow_p)))
11817 if (strict_overflow_p)
11818 fold_overflow_warning (("assuming signed overflow does not occur "
11819 "when simplifying division"),
11820 WARN_STRICT_OVERFLOW_MISC);
11821 return fold_convert_loc (loc, type, tem);
11824 return NULL_TREE;
11826 case CEIL_MOD_EXPR:
11827 case FLOOR_MOD_EXPR:
11828 case ROUND_MOD_EXPR:
11829 case TRUNC_MOD_EXPR:
11830 strict_overflow_p = false;
11831 if (TREE_CODE (arg1) == INTEGER_CST
11832 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11833 &strict_overflow_p)))
11835 if (strict_overflow_p)
11836 fold_overflow_warning (("assuming signed overflow does not occur "
11837 "when simplifying modulus"),
11838 WARN_STRICT_OVERFLOW_MISC);
11839 return fold_convert_loc (loc, type, tem);
11842 return NULL_TREE;
11844 case LROTATE_EXPR:
11845 case RROTATE_EXPR:
11846 case RSHIFT_EXPR:
11847 case LSHIFT_EXPR:
11848 /* Since negative shift count is not well-defined,
11849 don't try to compute it in the compiler. */
11850 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11851 return NULL_TREE;
11853 prec = element_precision (type);
11855 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11856 if (TREE_CODE (op0) == code && tree_fits_uhwi_p (arg1)
11857 && tree_to_uhwi (arg1) < prec
11858 && tree_fits_uhwi_p (TREE_OPERAND (arg0, 1))
11859 && tree_to_uhwi (TREE_OPERAND (arg0, 1)) < prec)
11861 unsigned int low = (tree_to_uhwi (TREE_OPERAND (arg0, 1))
11862 + tree_to_uhwi (arg1));
11864 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11865 being well defined. */
11866 if (low >= prec)
11868 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11869 low = low % prec;
11870 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11871 return omit_one_operand_loc (loc, type, build_zero_cst (type),
11872 TREE_OPERAND (arg0, 0));
11873 else
11874 low = prec - 1;
11877 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11878 build_int_cst (TREE_TYPE (arg1), low));
11881 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11882 into x & ((unsigned)-1 >> c) for unsigned types. */
11883 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11884 || (TYPE_UNSIGNED (type)
11885 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11886 && tree_fits_uhwi_p (arg1)
11887 && tree_to_uhwi (arg1) < prec
11888 && tree_fits_uhwi_p (TREE_OPERAND (arg0, 1))
11889 && tree_to_uhwi (TREE_OPERAND (arg0, 1)) < prec)
11891 HOST_WIDE_INT low0 = tree_to_uhwi (TREE_OPERAND (arg0, 1));
11892 HOST_WIDE_INT low1 = tree_to_uhwi (arg1);
11893 tree lshift;
11894 tree arg00;
11896 if (low0 == low1)
11898 arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11900 lshift = build_minus_one_cst (type);
11901 lshift = const_binop (code, lshift, arg1);
11903 return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
11907 /* If we have a rotate of a bit operation with the rotate count and
11908 the second operand of the bit operation both constant,
11909 permute the two operations. */
11910 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11911 && (TREE_CODE (arg0) == BIT_AND_EXPR
11912 || TREE_CODE (arg0) == BIT_IOR_EXPR
11913 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11914 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11915 return fold_build2_loc (loc, TREE_CODE (arg0), type,
11916 fold_build2_loc (loc, code, type,
11917 TREE_OPERAND (arg0, 0), arg1),
11918 fold_build2_loc (loc, code, type,
11919 TREE_OPERAND (arg0, 1), arg1));
11921 /* Two consecutive rotates adding up to the some integer
11922 multiple of the precision of the type can be ignored. */
11923 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11924 && TREE_CODE (arg0) == RROTATE_EXPR
11925 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11926 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
11927 prec) == 0)
11928 return TREE_OPERAND (arg0, 0);
11930 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11931 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11932 if the latter can be further optimized. */
11933 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11934 && TREE_CODE (arg0) == BIT_AND_EXPR
11935 && TREE_CODE (arg1) == INTEGER_CST
11936 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11938 tree mask = fold_build2_loc (loc, code, type,
11939 fold_convert_loc (loc, type,
11940 TREE_OPERAND (arg0, 1)),
11941 arg1);
11942 tree shift = fold_build2_loc (loc, code, type,
11943 fold_convert_loc (loc, type,
11944 TREE_OPERAND (arg0, 0)),
11945 arg1);
11946 tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
11947 if (tem)
11948 return tem;
11951 return NULL_TREE;
11953 case MIN_EXPR:
11954 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
11955 if (tem)
11956 return tem;
11957 goto associate;
11959 case MAX_EXPR:
11960 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
11961 if (tem)
11962 return tem;
11963 goto associate;
11965 case TRUTH_ANDIF_EXPR:
11966 /* Note that the operands of this must be ints
11967 and their values must be 0 or 1.
11968 ("true" is a fixed value perhaps depending on the language.) */
11969 /* If first arg is constant zero, return it. */
11970 if (integer_zerop (arg0))
11971 return fold_convert_loc (loc, type, arg0);
11972 case TRUTH_AND_EXPR:
11973 /* If either arg is constant true, drop it. */
11974 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11975 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11976 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11977 /* Preserve sequence points. */
11978 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11979 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11980 /* If second arg is constant zero, result is zero, but first arg
11981 must be evaluated. */
11982 if (integer_zerop (arg1))
11983 return omit_one_operand_loc (loc, type, arg1, arg0);
11984 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11985 case will be handled here. */
11986 if (integer_zerop (arg0))
11987 return omit_one_operand_loc (loc, type, arg0, arg1);
11989 /* !X && X is always false. */
11990 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11991 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11992 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11993 /* X && !X is always false. */
11994 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11995 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11996 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11998 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11999 means A >= Y && A != MAX, but in this case we know that
12000 A < X <= MAX. */
12002 if (!TREE_SIDE_EFFECTS (arg0)
12003 && !TREE_SIDE_EFFECTS (arg1))
12005 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12006 if (tem && !operand_equal_p (tem, arg0, 0))
12007 return fold_build2_loc (loc, code, type, tem, arg1);
12009 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12010 if (tem && !operand_equal_p (tem, arg1, 0))
12011 return fold_build2_loc (loc, code, type, arg0, tem);
12014 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12015 != NULL_TREE)
12016 return tem;
12018 return NULL_TREE;
12020 case TRUTH_ORIF_EXPR:
12021 /* Note that the operands of this must be ints
12022 and their values must be 0 or true.
12023 ("true" is a fixed value perhaps depending on the language.) */
12024 /* If first arg is constant true, return it. */
12025 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12026 return fold_convert_loc (loc, type, arg0);
12027 case TRUTH_OR_EXPR:
12028 /* If either arg is constant zero, drop it. */
12029 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12030 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12031 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12032 /* Preserve sequence points. */
12033 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12034 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12035 /* If second arg is constant true, result is true, but we must
12036 evaluate first arg. */
12037 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12038 return omit_one_operand_loc (loc, type, arg1, arg0);
12039 /* Likewise for first arg, but note this only occurs here for
12040 TRUTH_OR_EXPR. */
12041 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12042 return omit_one_operand_loc (loc, type, arg0, arg1);
12044 /* !X || X is always true. */
12045 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12046 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12047 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12048 /* X || !X is always true. */
12049 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12050 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12051 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12053 /* (X && !Y) || (!X && Y) is X ^ Y */
12054 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12055 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12057 tree a0, a1, l0, l1, n0, n1;
12059 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12060 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12062 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12063 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12065 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12066 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12068 if ((operand_equal_p (n0, a0, 0)
12069 && operand_equal_p (n1, a1, 0))
12070 || (operand_equal_p (n0, a1, 0)
12071 && operand_equal_p (n1, a0, 0)))
12072 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12075 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12076 != NULL_TREE)
12077 return tem;
12079 return NULL_TREE;
12081 case TRUTH_XOR_EXPR:
12082 /* If the second arg is constant zero, drop it. */
12083 if (integer_zerop (arg1))
12084 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12085 /* If the second arg is constant true, this is a logical inversion. */
12086 if (integer_onep (arg1))
12088 tem = invert_truthvalue_loc (loc, arg0);
12089 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12091 /* Identical arguments cancel to zero. */
12092 if (operand_equal_p (arg0, arg1, 0))
12093 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12095 /* !X ^ X is always true. */
12096 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12097 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12098 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12100 /* X ^ !X is always true. */
12101 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12102 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12103 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12105 return NULL_TREE;
12107 case EQ_EXPR:
12108 case NE_EXPR:
12109 STRIP_NOPS (arg0);
12110 STRIP_NOPS (arg1);
12112 tem = fold_comparison (loc, code, type, op0, op1);
12113 if (tem != NULL_TREE)
12114 return tem;
12116 /* bool_var != 0 becomes bool_var. */
12117 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12118 && code == NE_EXPR)
12119 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12121 /* bool_var == 1 becomes bool_var. */
12122 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12123 && code == EQ_EXPR)
12124 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12126 /* bool_var != 1 becomes !bool_var. */
12127 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12128 && code == NE_EXPR)
12129 return fold_convert_loc (loc, type,
12130 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12131 TREE_TYPE (arg0), arg0));
12133 /* bool_var == 0 becomes !bool_var. */
12134 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12135 && code == EQ_EXPR)
12136 return fold_convert_loc (loc, type,
12137 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12138 TREE_TYPE (arg0), arg0));
12140 /* !exp != 0 becomes !exp */
12141 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12142 && code == NE_EXPR)
12143 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12145 /* If this is an equality comparison of the address of two non-weak,
12146 unaliased symbols neither of which are extern (since we do not
12147 have access to attributes for externs), then we know the result. */
12148 if (TREE_CODE (arg0) == ADDR_EXPR
12149 && DECL_P (TREE_OPERAND (arg0, 0))
12150 && TREE_CODE (arg1) == ADDR_EXPR
12151 && DECL_P (TREE_OPERAND (arg1, 0)))
12153 int equal;
12155 if (decl_in_symtab_p (TREE_OPERAND (arg0, 0))
12156 && decl_in_symtab_p (TREE_OPERAND (arg1, 0)))
12157 equal = symtab_node::get_create (TREE_OPERAND (arg0, 0))
12158 ->equal_address_to (symtab_node::get_create
12159 (TREE_OPERAND (arg1, 0)));
12160 else
12161 equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12162 if (equal != 2)
12163 return constant_boolean_node (equal
12164 ? code == EQ_EXPR : code != EQ_EXPR,
12165 type);
12168 /* Similarly for a NEGATE_EXPR. */
12169 if (TREE_CODE (arg0) == NEGATE_EXPR
12170 && TREE_CODE (arg1) == INTEGER_CST
12171 && 0 != (tem = negate_expr (fold_convert_loc (loc, TREE_TYPE (arg0),
12172 arg1)))
12173 && TREE_CODE (tem) == INTEGER_CST
12174 && !TREE_OVERFLOW (tem))
12175 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12177 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12178 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12179 && TREE_CODE (arg1) == INTEGER_CST
12180 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12181 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12182 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12183 fold_convert_loc (loc,
12184 TREE_TYPE (arg0),
12185 arg1),
12186 TREE_OPERAND (arg0, 1)));
12188 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
12189 if ((TREE_CODE (arg0) == PLUS_EXPR
12190 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12191 || TREE_CODE (arg0) == MINUS_EXPR)
12192 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12193 0)),
12194 arg1, 0)
12195 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12196 || POINTER_TYPE_P (TREE_TYPE (arg0))))
12198 tree val = TREE_OPERAND (arg0, 1);
12199 return omit_two_operands_loc (loc, type,
12200 fold_build2_loc (loc, code, type,
12201 val,
12202 build_int_cst (TREE_TYPE (val),
12203 0)),
12204 TREE_OPERAND (arg0, 0), arg1);
12207 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
12208 if (TREE_CODE (arg0) == MINUS_EXPR
12209 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12210 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12211 1)),
12212 arg1, 0)
12213 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
12215 return omit_two_operands_loc (loc, type,
12216 code == NE_EXPR
12217 ? boolean_true_node : boolean_false_node,
12218 TREE_OPERAND (arg0, 1), arg1);
12221 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12222 if (TREE_CODE (arg0) == ABS_EXPR
12223 && (integer_zerop (arg1) || real_zerop (arg1)))
12224 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12226 /* If this is an EQ or NE comparison with zero and ARG0 is
12227 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12228 two operations, but the latter can be done in one less insn
12229 on machines that have only two-operand insns or on which a
12230 constant cannot be the first operand. */
12231 if (TREE_CODE (arg0) == BIT_AND_EXPR
12232 && integer_zerop (arg1))
12234 tree arg00 = TREE_OPERAND (arg0, 0);
12235 tree arg01 = TREE_OPERAND (arg0, 1);
12236 if (TREE_CODE (arg00) == LSHIFT_EXPR
12237 && integer_onep (TREE_OPERAND (arg00, 0)))
12239 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12240 arg01, TREE_OPERAND (arg00, 1));
12241 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12242 build_int_cst (TREE_TYPE (arg0), 1));
12243 return fold_build2_loc (loc, code, type,
12244 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12245 arg1);
12247 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12248 && integer_onep (TREE_OPERAND (arg01, 0)))
12250 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12251 arg00, TREE_OPERAND (arg01, 1));
12252 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12253 build_int_cst (TREE_TYPE (arg0), 1));
12254 return fold_build2_loc (loc, code, type,
12255 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12256 arg1);
12260 /* If this is an NE or EQ comparison of zero against the result of a
12261 signed MOD operation whose second operand is a power of 2, make
12262 the MOD operation unsigned since it is simpler and equivalent. */
12263 if (integer_zerop (arg1)
12264 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12265 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12266 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12267 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12268 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12269 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12271 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12272 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12273 fold_convert_loc (loc, newtype,
12274 TREE_OPERAND (arg0, 0)),
12275 fold_convert_loc (loc, newtype,
12276 TREE_OPERAND (arg0, 1)));
12278 return fold_build2_loc (loc, code, type, newmod,
12279 fold_convert_loc (loc, newtype, arg1));
12282 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12283 C1 is a valid shift constant, and C2 is a power of two, i.e.
12284 a single bit. */
12285 if (TREE_CODE (arg0) == BIT_AND_EXPR
12286 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12287 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12288 == INTEGER_CST
12289 && integer_pow2p (TREE_OPERAND (arg0, 1))
12290 && integer_zerop (arg1))
12292 tree itype = TREE_TYPE (arg0);
12293 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12294 prec = TYPE_PRECISION (itype);
12296 /* Check for a valid shift count. */
12297 if (wi::ltu_p (arg001, prec))
12299 tree arg01 = TREE_OPERAND (arg0, 1);
12300 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12301 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12302 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12303 can be rewritten as (X & (C2 << C1)) != 0. */
12304 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12306 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12307 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12308 return fold_build2_loc (loc, code, type, tem,
12309 fold_convert_loc (loc, itype, arg1));
12311 /* Otherwise, for signed (arithmetic) shifts,
12312 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12313 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12314 else if (!TYPE_UNSIGNED (itype))
12315 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12316 arg000, build_int_cst (itype, 0));
12317 /* Otherwise, of unsigned (logical) shifts,
12318 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12319 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12320 else
12321 return omit_one_operand_loc (loc, type,
12322 code == EQ_EXPR ? integer_one_node
12323 : integer_zero_node,
12324 arg000);
12328 /* If we have (A & C) == C where C is a power of 2, convert this into
12329 (A & C) != 0. Similarly for NE_EXPR. */
12330 if (TREE_CODE (arg0) == BIT_AND_EXPR
12331 && integer_pow2p (TREE_OPERAND (arg0, 1))
12332 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12333 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12334 arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12335 integer_zero_node));
12337 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12338 bit, then fold the expression into A < 0 or A >= 0. */
12339 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12340 if (tem)
12341 return tem;
12343 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12344 Similarly for NE_EXPR. */
12345 if (TREE_CODE (arg0) == BIT_AND_EXPR
12346 && TREE_CODE (arg1) == INTEGER_CST
12347 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12349 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12350 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12351 TREE_OPERAND (arg0, 1));
12352 tree dandnotc
12353 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12354 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
12355 notc);
12356 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12357 if (integer_nonzerop (dandnotc))
12358 return omit_one_operand_loc (loc, type, rslt, arg0);
12361 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12362 Similarly for NE_EXPR. */
12363 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12364 && TREE_CODE (arg1) == INTEGER_CST
12365 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12367 tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12368 tree candnotd
12369 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12370 TREE_OPERAND (arg0, 1),
12371 fold_convert_loc (loc, TREE_TYPE (arg0), notd));
12372 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12373 if (integer_nonzerop (candnotd))
12374 return omit_one_operand_loc (loc, type, rslt, arg0);
12377 /* If this is a comparison of a field, we may be able to simplify it. */
12378 if ((TREE_CODE (arg0) == COMPONENT_REF
12379 || TREE_CODE (arg0) == BIT_FIELD_REF)
12380 /* Handle the constant case even without -O
12381 to make sure the warnings are given. */
12382 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12384 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12385 if (t1)
12386 return t1;
12389 /* Optimize comparisons of strlen vs zero to a compare of the
12390 first character of the string vs zero. To wit,
12391 strlen(ptr) == 0 => *ptr == 0
12392 strlen(ptr) != 0 => *ptr != 0
12393 Other cases should reduce to one of these two (or a constant)
12394 due to the return value of strlen being unsigned. */
12395 if (TREE_CODE (arg0) == CALL_EXPR
12396 && integer_zerop (arg1))
12398 tree fndecl = get_callee_fndecl (arg0);
12400 if (fndecl
12401 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12402 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12403 && call_expr_nargs (arg0) == 1
12404 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12406 tree iref = build_fold_indirect_ref_loc (loc,
12407 CALL_EXPR_ARG (arg0, 0));
12408 return fold_build2_loc (loc, code, type, iref,
12409 build_int_cst (TREE_TYPE (iref), 0));
12413 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12414 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12415 if (TREE_CODE (arg0) == RSHIFT_EXPR
12416 && integer_zerop (arg1)
12417 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12419 tree arg00 = TREE_OPERAND (arg0, 0);
12420 tree arg01 = TREE_OPERAND (arg0, 1);
12421 tree itype = TREE_TYPE (arg00);
12422 if (wi::eq_p (arg01, element_precision (itype) - 1))
12424 if (TYPE_UNSIGNED (itype))
12426 itype = signed_type_for (itype);
12427 arg00 = fold_convert_loc (loc, itype, arg00);
12429 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12430 type, arg00, build_zero_cst (itype));
12434 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
12435 if (integer_zerop (arg1)
12436 && TREE_CODE (arg0) == BIT_XOR_EXPR)
12437 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12438 TREE_OPERAND (arg0, 1));
12440 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
12441 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12442 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12443 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12444 build_zero_cst (TREE_TYPE (arg0)));
12445 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
12446 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12447 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12448 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12449 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12450 build_zero_cst (TREE_TYPE (arg0)));
12452 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
12453 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12454 && TREE_CODE (arg1) == INTEGER_CST
12455 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12456 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12457 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12458 TREE_OPERAND (arg0, 1), arg1));
12460 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12461 (X & C) == 0 when C is a single bit. */
12462 if (TREE_CODE (arg0) == BIT_AND_EXPR
12463 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12464 && integer_zerop (arg1)
12465 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12467 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12468 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12469 TREE_OPERAND (arg0, 1));
12470 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12471 type, tem,
12472 fold_convert_loc (loc, TREE_TYPE (arg0),
12473 arg1));
12476 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12477 constant C is a power of two, i.e. a single bit. */
12478 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12479 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12480 && integer_zerop (arg1)
12481 && integer_pow2p (TREE_OPERAND (arg0, 1))
12482 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12483 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12485 tree arg00 = TREE_OPERAND (arg0, 0);
12486 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12487 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12490 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12491 when is C is a power of two, i.e. a single bit. */
12492 if (TREE_CODE (arg0) == BIT_AND_EXPR
12493 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12494 && integer_zerop (arg1)
12495 && integer_pow2p (TREE_OPERAND (arg0, 1))
12496 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12497 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12499 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12500 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12501 arg000, TREE_OPERAND (arg0, 1));
12502 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12503 tem, build_int_cst (TREE_TYPE (tem), 0));
12506 if (integer_zerop (arg1)
12507 && tree_expr_nonzero_p (arg0))
12509 tree res = constant_boolean_node (code==NE_EXPR, type);
12510 return omit_one_operand_loc (loc, type, res, arg0);
12513 /* Fold -X op -Y as X op Y, where op is eq/ne. */
12514 if (TREE_CODE (arg0) == NEGATE_EXPR
12515 && TREE_CODE (arg1) == NEGATE_EXPR)
12516 return fold_build2_loc (loc, code, type,
12517 TREE_OPERAND (arg0, 0),
12518 fold_convert_loc (loc, TREE_TYPE (arg0),
12519 TREE_OPERAND (arg1, 0)));
12521 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12522 if (TREE_CODE (arg0) == BIT_AND_EXPR
12523 && TREE_CODE (arg1) == BIT_AND_EXPR)
12525 tree arg00 = TREE_OPERAND (arg0, 0);
12526 tree arg01 = TREE_OPERAND (arg0, 1);
12527 tree arg10 = TREE_OPERAND (arg1, 0);
12528 tree arg11 = TREE_OPERAND (arg1, 1);
12529 tree itype = TREE_TYPE (arg0);
12531 if (operand_equal_p (arg01, arg11, 0))
12532 return fold_build2_loc (loc, code, type,
12533 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12534 fold_build2_loc (loc,
12535 BIT_XOR_EXPR, itype,
12536 arg00, arg10),
12537 arg01),
12538 build_zero_cst (itype));
12540 if (operand_equal_p (arg01, arg10, 0))
12541 return fold_build2_loc (loc, code, type,
12542 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12543 fold_build2_loc (loc,
12544 BIT_XOR_EXPR, itype,
12545 arg00, arg11),
12546 arg01),
12547 build_zero_cst (itype));
12549 if (operand_equal_p (arg00, arg11, 0))
12550 return fold_build2_loc (loc, code, type,
12551 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12552 fold_build2_loc (loc,
12553 BIT_XOR_EXPR, itype,
12554 arg01, arg10),
12555 arg00),
12556 build_zero_cst (itype));
12558 if (operand_equal_p (arg00, arg10, 0))
12559 return fold_build2_loc (loc, code, type,
12560 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12561 fold_build2_loc (loc,
12562 BIT_XOR_EXPR, itype,
12563 arg01, arg11),
12564 arg00),
12565 build_zero_cst (itype));
12568 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12569 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12571 tree arg00 = TREE_OPERAND (arg0, 0);
12572 tree arg01 = TREE_OPERAND (arg0, 1);
12573 tree arg10 = TREE_OPERAND (arg1, 0);
12574 tree arg11 = TREE_OPERAND (arg1, 1);
12575 tree itype = TREE_TYPE (arg0);
12577 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12578 operand_equal_p guarantees no side-effects so we don't need
12579 to use omit_one_operand on Z. */
12580 if (operand_equal_p (arg01, arg11, 0))
12581 return fold_build2_loc (loc, code, type, arg00,
12582 fold_convert_loc (loc, TREE_TYPE (arg00),
12583 arg10));
12584 if (operand_equal_p (arg01, arg10, 0))
12585 return fold_build2_loc (loc, code, type, arg00,
12586 fold_convert_loc (loc, TREE_TYPE (arg00),
12587 arg11));
12588 if (operand_equal_p (arg00, arg11, 0))
12589 return fold_build2_loc (loc, code, type, arg01,
12590 fold_convert_loc (loc, TREE_TYPE (arg01),
12591 arg10));
12592 if (operand_equal_p (arg00, arg10, 0))
12593 return fold_build2_loc (loc, code, type, arg01,
12594 fold_convert_loc (loc, TREE_TYPE (arg01),
12595 arg11));
12597 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12598 if (TREE_CODE (arg01) == INTEGER_CST
12599 && TREE_CODE (arg11) == INTEGER_CST)
12601 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12602 fold_convert_loc (loc, itype, arg11));
12603 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12604 return fold_build2_loc (loc, code, type, tem,
12605 fold_convert_loc (loc, itype, arg10));
12609 /* Attempt to simplify equality/inequality comparisons of complex
12610 values. Only lower the comparison if the result is known or
12611 can be simplified to a single scalar comparison. */
12612 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12613 || TREE_CODE (arg0) == COMPLEX_CST)
12614 && (TREE_CODE (arg1) == COMPLEX_EXPR
12615 || TREE_CODE (arg1) == COMPLEX_CST))
12617 tree real0, imag0, real1, imag1;
12618 tree rcond, icond;
12620 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12622 real0 = TREE_OPERAND (arg0, 0);
12623 imag0 = TREE_OPERAND (arg0, 1);
12625 else
12627 real0 = TREE_REALPART (arg0);
12628 imag0 = TREE_IMAGPART (arg0);
12631 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12633 real1 = TREE_OPERAND (arg1, 0);
12634 imag1 = TREE_OPERAND (arg1, 1);
12636 else
12638 real1 = TREE_REALPART (arg1);
12639 imag1 = TREE_IMAGPART (arg1);
12642 rcond = fold_binary_loc (loc, code, type, real0, real1);
12643 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12645 if (integer_zerop (rcond))
12647 if (code == EQ_EXPR)
12648 return omit_two_operands_loc (loc, type, boolean_false_node,
12649 imag0, imag1);
12650 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12652 else
12654 if (code == NE_EXPR)
12655 return omit_two_operands_loc (loc, type, boolean_true_node,
12656 imag0, imag1);
12657 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12661 icond = fold_binary_loc (loc, code, type, imag0, imag1);
12662 if (icond && TREE_CODE (icond) == INTEGER_CST)
12664 if (integer_zerop (icond))
12666 if (code == EQ_EXPR)
12667 return omit_two_operands_loc (loc, type, boolean_false_node,
12668 real0, real1);
12669 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12671 else
12673 if (code == NE_EXPR)
12674 return omit_two_operands_loc (loc, type, boolean_true_node,
12675 real0, real1);
12676 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12681 return NULL_TREE;
12683 case LT_EXPR:
12684 case GT_EXPR:
12685 case LE_EXPR:
12686 case GE_EXPR:
12687 tem = fold_comparison (loc, code, type, op0, op1);
12688 if (tem != NULL_TREE)
12689 return tem;
12691 /* Transform comparisons of the form X +- C CMP X. */
12692 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12693 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12694 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12695 && !HONOR_SNANS (arg0))
12696 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12697 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12699 tree arg01 = TREE_OPERAND (arg0, 1);
12700 enum tree_code code0 = TREE_CODE (arg0);
12701 int is_positive;
12703 if (TREE_CODE (arg01) == REAL_CST)
12704 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12705 else
12706 is_positive = tree_int_cst_sgn (arg01);
12708 /* (X - c) > X becomes false. */
12709 if (code == GT_EXPR
12710 && ((code0 == MINUS_EXPR && is_positive >= 0)
12711 || (code0 == PLUS_EXPR && is_positive <= 0)))
12713 if (TREE_CODE (arg01) == INTEGER_CST
12714 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12715 fold_overflow_warning (("assuming signed overflow does not "
12716 "occur when assuming that (X - c) > X "
12717 "is always false"),
12718 WARN_STRICT_OVERFLOW_ALL);
12719 return constant_boolean_node (0, type);
12722 /* Likewise (X + c) < X becomes false. */
12723 if (code == LT_EXPR
12724 && ((code0 == PLUS_EXPR && is_positive >= 0)
12725 || (code0 == MINUS_EXPR && is_positive <= 0)))
12727 if (TREE_CODE (arg01) == INTEGER_CST
12728 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12729 fold_overflow_warning (("assuming signed overflow does not "
12730 "occur when assuming that "
12731 "(X + c) < X is always false"),
12732 WARN_STRICT_OVERFLOW_ALL);
12733 return constant_boolean_node (0, type);
12736 /* Convert (X - c) <= X to true. */
12737 if (!HONOR_NANS (arg1)
12738 && code == LE_EXPR
12739 && ((code0 == MINUS_EXPR && is_positive >= 0)
12740 || (code0 == PLUS_EXPR && is_positive <= 0)))
12742 if (TREE_CODE (arg01) == INTEGER_CST
12743 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12744 fold_overflow_warning (("assuming signed overflow does not "
12745 "occur when assuming that "
12746 "(X - c) <= X is always true"),
12747 WARN_STRICT_OVERFLOW_ALL);
12748 return constant_boolean_node (1, type);
12751 /* Convert (X + c) >= X to true. */
12752 if (!HONOR_NANS (arg1)
12753 && code == GE_EXPR
12754 && ((code0 == PLUS_EXPR && is_positive >= 0)
12755 || (code0 == MINUS_EXPR && is_positive <= 0)))
12757 if (TREE_CODE (arg01) == INTEGER_CST
12758 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12759 fold_overflow_warning (("assuming signed overflow does not "
12760 "occur when assuming that "
12761 "(X + c) >= X is always true"),
12762 WARN_STRICT_OVERFLOW_ALL);
12763 return constant_boolean_node (1, type);
12766 if (TREE_CODE (arg01) == INTEGER_CST)
12768 /* Convert X + c > X and X - c < X to true for integers. */
12769 if (code == GT_EXPR
12770 && ((code0 == PLUS_EXPR && is_positive > 0)
12771 || (code0 == MINUS_EXPR && is_positive < 0)))
12773 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12774 fold_overflow_warning (("assuming signed overflow does "
12775 "not occur when assuming that "
12776 "(X + c) > X is always true"),
12777 WARN_STRICT_OVERFLOW_ALL);
12778 return constant_boolean_node (1, type);
12781 if (code == LT_EXPR
12782 && ((code0 == MINUS_EXPR && is_positive > 0)
12783 || (code0 == PLUS_EXPR && is_positive < 0)))
12785 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12786 fold_overflow_warning (("assuming signed overflow does "
12787 "not occur when assuming that "
12788 "(X - c) < X is always true"),
12789 WARN_STRICT_OVERFLOW_ALL);
12790 return constant_boolean_node (1, type);
12793 /* Convert X + c <= X and X - c >= X to false for integers. */
12794 if (code == LE_EXPR
12795 && ((code0 == PLUS_EXPR && is_positive > 0)
12796 || (code0 == MINUS_EXPR && is_positive < 0)))
12798 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12799 fold_overflow_warning (("assuming signed overflow does "
12800 "not occur when assuming that "
12801 "(X + c) <= X is always false"),
12802 WARN_STRICT_OVERFLOW_ALL);
12803 return constant_boolean_node (0, type);
12806 if (code == GE_EXPR
12807 && ((code0 == MINUS_EXPR && is_positive > 0)
12808 || (code0 == PLUS_EXPR && is_positive < 0)))
12810 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12811 fold_overflow_warning (("assuming signed overflow does "
12812 "not occur when assuming that "
12813 "(X - c) >= X is always false"),
12814 WARN_STRICT_OVERFLOW_ALL);
12815 return constant_boolean_node (0, type);
12820 /* Comparisons with the highest or lowest possible integer of
12821 the specified precision will have known values. */
12823 tree arg1_type = TREE_TYPE (arg1);
12824 unsigned int prec = TYPE_PRECISION (arg1_type);
12826 if (TREE_CODE (arg1) == INTEGER_CST
12827 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12829 wide_int max = wi::max_value (arg1_type);
12830 wide_int signed_max = wi::max_value (prec, SIGNED);
12831 wide_int min = wi::min_value (arg1_type);
12833 if (wi::eq_p (arg1, max))
12834 switch (code)
12836 case GT_EXPR:
12837 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12839 case GE_EXPR:
12840 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12842 case LE_EXPR:
12843 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12845 case LT_EXPR:
12846 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12848 /* The GE_EXPR and LT_EXPR cases above are not normally
12849 reached because of previous transformations. */
12851 default:
12852 break;
12854 else if (wi::eq_p (arg1, max - 1))
12855 switch (code)
12857 case GT_EXPR:
12858 arg1 = const_binop (PLUS_EXPR, arg1,
12859 build_int_cst (TREE_TYPE (arg1), 1));
12860 return fold_build2_loc (loc, EQ_EXPR, type,
12861 fold_convert_loc (loc,
12862 TREE_TYPE (arg1), arg0),
12863 arg1);
12864 case LE_EXPR:
12865 arg1 = const_binop (PLUS_EXPR, arg1,
12866 build_int_cst (TREE_TYPE (arg1), 1));
12867 return fold_build2_loc (loc, NE_EXPR, type,
12868 fold_convert_loc (loc, TREE_TYPE (arg1),
12869 arg0),
12870 arg1);
12871 default:
12872 break;
12874 else if (wi::eq_p (arg1, min))
12875 switch (code)
12877 case LT_EXPR:
12878 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12880 case LE_EXPR:
12881 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12883 case GE_EXPR:
12884 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12886 case GT_EXPR:
12887 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12889 default:
12890 break;
12892 else if (wi::eq_p (arg1, min + 1))
12893 switch (code)
12895 case GE_EXPR:
12896 arg1 = const_binop (MINUS_EXPR, arg1,
12897 build_int_cst (TREE_TYPE (arg1), 1));
12898 return fold_build2_loc (loc, NE_EXPR, type,
12899 fold_convert_loc (loc,
12900 TREE_TYPE (arg1), arg0),
12901 arg1);
12902 case LT_EXPR:
12903 arg1 = const_binop (MINUS_EXPR, arg1,
12904 build_int_cst (TREE_TYPE (arg1), 1));
12905 return fold_build2_loc (loc, EQ_EXPR, type,
12906 fold_convert_loc (loc, TREE_TYPE (arg1),
12907 arg0),
12908 arg1);
12909 default:
12910 break;
12913 else if (wi::eq_p (arg1, signed_max)
12914 && TYPE_UNSIGNED (arg1_type)
12915 /* We will flip the signedness of the comparison operator
12916 associated with the mode of arg1, so the sign bit is
12917 specified by this mode. Check that arg1 is the signed
12918 max associated with this sign bit. */
12919 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
12920 /* signed_type does not work on pointer types. */
12921 && INTEGRAL_TYPE_P (arg1_type))
12923 /* The following case also applies to X < signed_max+1
12924 and X >= signed_max+1 because previous transformations. */
12925 if (code == LE_EXPR || code == GT_EXPR)
12927 tree st = signed_type_for (arg1_type);
12928 return fold_build2_loc (loc,
12929 code == LE_EXPR ? GE_EXPR : LT_EXPR,
12930 type, fold_convert_loc (loc, st, arg0),
12931 build_int_cst (st, 0));
12937 /* If we are comparing an ABS_EXPR with a constant, we can
12938 convert all the cases into explicit comparisons, but they may
12939 well not be faster than doing the ABS and one comparison.
12940 But ABS (X) <= C is a range comparison, which becomes a subtraction
12941 and a comparison, and is probably faster. */
12942 if (code == LE_EXPR
12943 && TREE_CODE (arg1) == INTEGER_CST
12944 && TREE_CODE (arg0) == ABS_EXPR
12945 && ! TREE_SIDE_EFFECTS (arg0)
12946 && (0 != (tem = negate_expr (arg1)))
12947 && TREE_CODE (tem) == INTEGER_CST
12948 && !TREE_OVERFLOW (tem))
12949 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12950 build2 (GE_EXPR, type,
12951 TREE_OPERAND (arg0, 0), tem),
12952 build2 (LE_EXPR, type,
12953 TREE_OPERAND (arg0, 0), arg1));
12955 /* Convert ABS_EXPR<x> >= 0 to true. */
12956 strict_overflow_p = false;
12957 if (code == GE_EXPR
12958 && (integer_zerop (arg1)
12959 || (! HONOR_NANS (arg0)
12960 && real_zerop (arg1)))
12961 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12963 if (strict_overflow_p)
12964 fold_overflow_warning (("assuming signed overflow does not occur "
12965 "when simplifying comparison of "
12966 "absolute value and zero"),
12967 WARN_STRICT_OVERFLOW_CONDITIONAL);
12968 return omit_one_operand_loc (loc, type,
12969 constant_boolean_node (true, type),
12970 arg0);
12973 /* Convert ABS_EXPR<x> < 0 to false. */
12974 strict_overflow_p = false;
12975 if (code == LT_EXPR
12976 && (integer_zerop (arg1) || real_zerop (arg1))
12977 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12979 if (strict_overflow_p)
12980 fold_overflow_warning (("assuming signed overflow does not occur "
12981 "when simplifying comparison of "
12982 "absolute value and zero"),
12983 WARN_STRICT_OVERFLOW_CONDITIONAL);
12984 return omit_one_operand_loc (loc, type,
12985 constant_boolean_node (false, type),
12986 arg0);
12989 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12990 and similarly for >= into !=. */
12991 if ((code == LT_EXPR || code == GE_EXPR)
12992 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12993 && TREE_CODE (arg1) == LSHIFT_EXPR
12994 && integer_onep (TREE_OPERAND (arg1, 0)))
12995 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12996 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12997 TREE_OPERAND (arg1, 1)),
12998 build_zero_cst (TREE_TYPE (arg0)));
13000 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
13001 otherwise Y might be >= # of bits in X's type and thus e.g.
13002 (unsigned char) (1 << Y) for Y 15 might be 0.
13003 If the cast is widening, then 1 << Y should have unsigned type,
13004 otherwise if Y is number of bits in the signed shift type minus 1,
13005 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
13006 31 might be 0xffffffff80000000. */
13007 if ((code == LT_EXPR || code == GE_EXPR)
13008 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13009 && CONVERT_EXPR_P (arg1)
13010 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13011 && (element_precision (TREE_TYPE (arg1))
13012 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
13013 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
13014 || (element_precision (TREE_TYPE (arg1))
13015 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
13016 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13018 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13019 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
13020 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13021 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
13022 build_zero_cst (TREE_TYPE (arg0)));
13025 return NULL_TREE;
13027 case UNORDERED_EXPR:
13028 case ORDERED_EXPR:
13029 case UNLT_EXPR:
13030 case UNLE_EXPR:
13031 case UNGT_EXPR:
13032 case UNGE_EXPR:
13033 case UNEQ_EXPR:
13034 case LTGT_EXPR:
13035 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13037 t1 = fold_relational_const (code, type, arg0, arg1);
13038 if (t1 != NULL_TREE)
13039 return t1;
13042 /* If the first operand is NaN, the result is constant. */
13043 if (TREE_CODE (arg0) == REAL_CST
13044 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13045 && (code != LTGT_EXPR || ! flag_trapping_math))
13047 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13048 ? integer_zero_node
13049 : integer_one_node;
13050 return omit_one_operand_loc (loc, type, t1, arg1);
13053 /* If the second operand is NaN, the result is constant. */
13054 if (TREE_CODE (arg1) == REAL_CST
13055 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13056 && (code != LTGT_EXPR || ! flag_trapping_math))
13058 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13059 ? integer_zero_node
13060 : integer_one_node;
13061 return omit_one_operand_loc (loc, type, t1, arg0);
13064 /* Simplify unordered comparison of something with itself. */
13065 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13066 && operand_equal_p (arg0, arg1, 0))
13067 return constant_boolean_node (1, type);
13069 if (code == LTGT_EXPR
13070 && !flag_trapping_math
13071 && operand_equal_p (arg0, arg1, 0))
13072 return constant_boolean_node (0, type);
13074 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13076 tree targ0 = strip_float_extensions (arg0);
13077 tree targ1 = strip_float_extensions (arg1);
13078 tree newtype = TREE_TYPE (targ0);
13080 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13081 newtype = TREE_TYPE (targ1);
13083 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13084 return fold_build2_loc (loc, code, type,
13085 fold_convert_loc (loc, newtype, targ0),
13086 fold_convert_loc (loc, newtype, targ1));
13089 return NULL_TREE;
13091 case COMPOUND_EXPR:
13092 /* When pedantic, a compound expression can be neither an lvalue
13093 nor an integer constant expression. */
13094 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13095 return NULL_TREE;
13096 /* Don't let (0, 0) be null pointer constant. */
13097 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13098 : fold_convert_loc (loc, type, arg1);
13099 return pedantic_non_lvalue_loc (loc, tem);
13101 case ASSERT_EXPR:
13102 /* An ASSERT_EXPR should never be passed to fold_binary. */
13103 gcc_unreachable ();
13105 default:
13106 return NULL_TREE;
13107 } /* switch (code) */
13110 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
13111 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
13112 of GOTO_EXPR. */
13114 static tree
13115 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13117 switch (TREE_CODE (*tp))
13119 case LABEL_EXPR:
13120 return *tp;
13122 case GOTO_EXPR:
13123 *walk_subtrees = 0;
13125 /* ... fall through ... */
13127 default:
13128 return NULL_TREE;
13132 /* Return whether the sub-tree ST contains a label which is accessible from
13133 outside the sub-tree. */
13135 static bool
13136 contains_label_p (tree st)
13138 return
13139 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13142 /* Fold a ternary expression of code CODE and type TYPE with operands
13143 OP0, OP1, and OP2. Return the folded expression if folding is
13144 successful. Otherwise, return NULL_TREE. */
13146 tree
13147 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13148 tree op0, tree op1, tree op2)
13150 tree tem;
13151 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
13152 enum tree_code_class kind = TREE_CODE_CLASS (code);
13154 gcc_assert (IS_EXPR_CODE_CLASS (kind)
13155 && TREE_CODE_LENGTH (code) == 3);
13157 /* If this is a commutative operation, and OP0 is a constant, move it
13158 to OP1 to reduce the number of tests below. */
13159 if (commutative_ternary_tree_code (code)
13160 && tree_swap_operands_p (op0, op1, true))
13161 return fold_build3_loc (loc, code, type, op1, op0, op2);
13163 tem = generic_simplify (loc, code, type, op0, op1, op2);
13164 if (tem)
13165 return tem;
13167 /* Strip any conversions that don't change the mode. This is safe
13168 for every expression, except for a comparison expression because
13169 its signedness is derived from its operands. So, in the latter
13170 case, only strip conversions that don't change the signedness.
13172 Note that this is done as an internal manipulation within the
13173 constant folder, in order to find the simplest representation of
13174 the arguments so that their form can be studied. In any cases,
13175 the appropriate type conversions should be put back in the tree
13176 that will get out of the constant folder. */
13177 if (op0)
13179 arg0 = op0;
13180 STRIP_NOPS (arg0);
13183 if (op1)
13185 arg1 = op1;
13186 STRIP_NOPS (arg1);
13189 if (op2)
13191 arg2 = op2;
13192 STRIP_NOPS (arg2);
13195 switch (code)
13197 case COMPONENT_REF:
13198 if (TREE_CODE (arg0) == CONSTRUCTOR
13199 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13201 unsigned HOST_WIDE_INT idx;
13202 tree field, value;
13203 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13204 if (field == arg1)
13205 return value;
13207 return NULL_TREE;
13209 case COND_EXPR:
13210 case VEC_COND_EXPR:
13211 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13212 so all simple results must be passed through pedantic_non_lvalue. */
13213 if (TREE_CODE (arg0) == INTEGER_CST)
13215 tree unused_op = integer_zerop (arg0) ? op1 : op2;
13216 tem = integer_zerop (arg0) ? op2 : op1;
13217 /* Only optimize constant conditions when the selected branch
13218 has the same type as the COND_EXPR. This avoids optimizing
13219 away "c ? x : throw", where the throw has a void type.
13220 Avoid throwing away that operand which contains label. */
13221 if ((!TREE_SIDE_EFFECTS (unused_op)
13222 || !contains_label_p (unused_op))
13223 && (! VOID_TYPE_P (TREE_TYPE (tem))
13224 || VOID_TYPE_P (type)))
13225 return pedantic_non_lvalue_loc (loc, tem);
13226 return NULL_TREE;
13228 else if (TREE_CODE (arg0) == VECTOR_CST)
13230 if ((TREE_CODE (arg1) == VECTOR_CST
13231 || TREE_CODE (arg1) == CONSTRUCTOR)
13232 && (TREE_CODE (arg2) == VECTOR_CST
13233 || TREE_CODE (arg2) == CONSTRUCTOR))
13235 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
13236 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
13237 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
13238 for (i = 0; i < nelts; i++)
13240 tree val = VECTOR_CST_ELT (arg0, i);
13241 if (integer_all_onesp (val))
13242 sel[i] = i;
13243 else if (integer_zerop (val))
13244 sel[i] = nelts + i;
13245 else /* Currently unreachable. */
13246 return NULL_TREE;
13248 tree t = fold_vec_perm (type, arg1, arg2, sel);
13249 if (t != NULL_TREE)
13250 return t;
13254 /* If we have A op B ? A : C, we may be able to convert this to a
13255 simpler expression, depending on the operation and the values
13256 of B and C. Signed zeros prevent all of these transformations,
13257 for reasons given above each one.
13259 Also try swapping the arguments and inverting the conditional. */
13260 if (COMPARISON_CLASS_P (arg0)
13261 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13262 arg1, TREE_OPERAND (arg0, 1))
13263 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
13265 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13266 if (tem)
13267 return tem;
13270 if (COMPARISON_CLASS_P (arg0)
13271 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13272 op2,
13273 TREE_OPERAND (arg0, 1))
13274 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
13276 location_t loc0 = expr_location_or (arg0, loc);
13277 tem = fold_invert_truthvalue (loc0, arg0);
13278 if (tem && COMPARISON_CLASS_P (tem))
13280 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13281 if (tem)
13282 return tem;
13286 /* If the second operand is simpler than the third, swap them
13287 since that produces better jump optimization results. */
13288 if (truth_value_p (TREE_CODE (arg0))
13289 && tree_swap_operands_p (op1, op2, false))
13291 location_t loc0 = expr_location_or (arg0, loc);
13292 /* See if this can be inverted. If it can't, possibly because
13293 it was a floating-point inequality comparison, don't do
13294 anything. */
13295 tem = fold_invert_truthvalue (loc0, arg0);
13296 if (tem)
13297 return fold_build3_loc (loc, code, type, tem, op2, op1);
13300 /* Convert A ? 1 : 0 to simply A. */
13301 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
13302 : (integer_onep (op1)
13303 && !VECTOR_TYPE_P (type)))
13304 && integer_zerop (op2)
13305 /* If we try to convert OP0 to our type, the
13306 call to fold will try to move the conversion inside
13307 a COND, which will recurse. In that case, the COND_EXPR
13308 is probably the best choice, so leave it alone. */
13309 && type == TREE_TYPE (arg0))
13310 return pedantic_non_lvalue_loc (loc, arg0);
13312 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13313 over COND_EXPR in cases such as floating point comparisons. */
13314 if (integer_zerop (op1)
13315 && (code == VEC_COND_EXPR ? integer_all_onesp (op2)
13316 : (integer_onep (op2)
13317 && !VECTOR_TYPE_P (type)))
13318 && truth_value_p (TREE_CODE (arg0)))
13319 return pedantic_non_lvalue_loc (loc,
13320 fold_convert_loc (loc, type,
13321 invert_truthvalue_loc (loc,
13322 arg0)));
13324 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13325 if (TREE_CODE (arg0) == LT_EXPR
13326 && integer_zerop (TREE_OPERAND (arg0, 1))
13327 && integer_zerop (op2)
13328 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13330 /* sign_bit_p looks through both zero and sign extensions,
13331 but for this optimization only sign extensions are
13332 usable. */
13333 tree tem2 = TREE_OPERAND (arg0, 0);
13334 while (tem != tem2)
13336 if (TREE_CODE (tem2) != NOP_EXPR
13337 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
13339 tem = NULL_TREE;
13340 break;
13342 tem2 = TREE_OPERAND (tem2, 0);
13344 /* sign_bit_p only checks ARG1 bits within A's precision.
13345 If <sign bit of A> has wider type than A, bits outside
13346 of A's precision in <sign bit of A> need to be checked.
13347 If they are all 0, this optimization needs to be done
13348 in unsigned A's type, if they are all 1 in signed A's type,
13349 otherwise this can't be done. */
13350 if (tem
13351 && TYPE_PRECISION (TREE_TYPE (tem))
13352 < TYPE_PRECISION (TREE_TYPE (arg1))
13353 && TYPE_PRECISION (TREE_TYPE (tem))
13354 < TYPE_PRECISION (type))
13356 int inner_width, outer_width;
13357 tree tem_type;
13359 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13360 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13361 if (outer_width > TYPE_PRECISION (type))
13362 outer_width = TYPE_PRECISION (type);
13364 wide_int mask = wi::shifted_mask
13365 (inner_width, outer_width - inner_width, false,
13366 TYPE_PRECISION (TREE_TYPE (arg1)));
13368 wide_int common = mask & arg1;
13369 if (common == mask)
13371 tem_type = signed_type_for (TREE_TYPE (tem));
13372 tem = fold_convert_loc (loc, tem_type, tem);
13374 else if (common == 0)
13376 tem_type = unsigned_type_for (TREE_TYPE (tem));
13377 tem = fold_convert_loc (loc, tem_type, tem);
13379 else
13380 tem = NULL;
13383 if (tem)
13384 return
13385 fold_convert_loc (loc, type,
13386 fold_build2_loc (loc, BIT_AND_EXPR,
13387 TREE_TYPE (tem), tem,
13388 fold_convert_loc (loc,
13389 TREE_TYPE (tem),
13390 arg1)));
13393 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13394 already handled above. */
13395 if (TREE_CODE (arg0) == BIT_AND_EXPR
13396 && integer_onep (TREE_OPERAND (arg0, 1))
13397 && integer_zerop (op2)
13398 && integer_pow2p (arg1))
13400 tree tem = TREE_OPERAND (arg0, 0);
13401 STRIP_NOPS (tem);
13402 if (TREE_CODE (tem) == RSHIFT_EXPR
13403 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
13404 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13405 tree_to_uhwi (TREE_OPERAND (tem, 1)))
13406 return fold_build2_loc (loc, BIT_AND_EXPR, type,
13407 TREE_OPERAND (tem, 0), arg1);
13410 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13411 is probably obsolete because the first operand should be a
13412 truth value (that's why we have the two cases above), but let's
13413 leave it in until we can confirm this for all front-ends. */
13414 if (integer_zerop (op2)
13415 && TREE_CODE (arg0) == NE_EXPR
13416 && integer_zerop (TREE_OPERAND (arg0, 1))
13417 && integer_pow2p (arg1)
13418 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13419 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13420 arg1, OEP_ONLY_CONST))
13421 return pedantic_non_lvalue_loc (loc,
13422 fold_convert_loc (loc, type,
13423 TREE_OPERAND (arg0, 0)));
13425 /* Disable the transformations below for vectors, since
13426 fold_binary_op_with_conditional_arg may undo them immediately,
13427 yielding an infinite loop. */
13428 if (code == VEC_COND_EXPR)
13429 return NULL_TREE;
13431 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13432 if (integer_zerop (op2)
13433 && truth_value_p (TREE_CODE (arg0))
13434 && truth_value_p (TREE_CODE (arg1))
13435 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13436 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13437 : TRUTH_ANDIF_EXPR,
13438 type, fold_convert_loc (loc, type, arg0), arg1);
13440 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13441 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13442 && truth_value_p (TREE_CODE (arg0))
13443 && truth_value_p (TREE_CODE (arg1))
13444 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13446 location_t loc0 = expr_location_or (arg0, loc);
13447 /* Only perform transformation if ARG0 is easily inverted. */
13448 tem = fold_invert_truthvalue (loc0, arg0);
13449 if (tem)
13450 return fold_build2_loc (loc, code == VEC_COND_EXPR
13451 ? BIT_IOR_EXPR
13452 : TRUTH_ORIF_EXPR,
13453 type, fold_convert_loc (loc, type, tem),
13454 arg1);
13457 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13458 if (integer_zerop (arg1)
13459 && truth_value_p (TREE_CODE (arg0))
13460 && truth_value_p (TREE_CODE (op2))
13461 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13463 location_t loc0 = expr_location_or (arg0, loc);
13464 /* Only perform transformation if ARG0 is easily inverted. */
13465 tem = fold_invert_truthvalue (loc0, arg0);
13466 if (tem)
13467 return fold_build2_loc (loc, code == VEC_COND_EXPR
13468 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13469 type, fold_convert_loc (loc, type, tem),
13470 op2);
13473 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13474 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13475 && truth_value_p (TREE_CODE (arg0))
13476 && truth_value_p (TREE_CODE (op2))
13477 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13478 return fold_build2_loc (loc, code == VEC_COND_EXPR
13479 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13480 type, fold_convert_loc (loc, type, arg0), op2);
13482 return NULL_TREE;
13484 case CALL_EXPR:
13485 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13486 of fold_ternary on them. */
13487 gcc_unreachable ();
13489 case BIT_FIELD_REF:
13490 if ((TREE_CODE (arg0) == VECTOR_CST
13491 || (TREE_CODE (arg0) == CONSTRUCTOR
13492 && TREE_CODE (TREE_TYPE (arg0)) == VECTOR_TYPE))
13493 && (type == TREE_TYPE (TREE_TYPE (arg0))
13494 || (TREE_CODE (type) == VECTOR_TYPE
13495 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
13497 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13498 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
13499 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13500 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13502 if (n != 0
13503 && (idx % width) == 0
13504 && (n % width) == 0
13505 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13507 idx = idx / width;
13508 n = n / width;
13510 if (TREE_CODE (arg0) == VECTOR_CST)
13512 if (n == 1)
13513 return VECTOR_CST_ELT (arg0, idx);
13515 tree *vals = XALLOCAVEC (tree, n);
13516 for (unsigned i = 0; i < n; ++i)
13517 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
13518 return build_vector (type, vals);
13521 /* Constructor elements can be subvectors. */
13522 unsigned HOST_WIDE_INT k = 1;
13523 if (CONSTRUCTOR_NELTS (arg0) != 0)
13525 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (arg0, 0)->value);
13526 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
13527 k = TYPE_VECTOR_SUBPARTS (cons_elem);
13530 /* We keep an exact subset of the constructor elements. */
13531 if ((idx % k) == 0 && (n % k) == 0)
13533 if (CONSTRUCTOR_NELTS (arg0) == 0)
13534 return build_constructor (type, NULL);
13535 idx /= k;
13536 n /= k;
13537 if (n == 1)
13539 if (idx < CONSTRUCTOR_NELTS (arg0))
13540 return CONSTRUCTOR_ELT (arg0, idx)->value;
13541 return build_zero_cst (type);
13544 vec<constructor_elt, va_gc> *vals;
13545 vec_alloc (vals, n);
13546 for (unsigned i = 0;
13547 i < n && idx + i < CONSTRUCTOR_NELTS (arg0);
13548 ++i)
13549 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
13550 CONSTRUCTOR_ELT
13551 (arg0, idx + i)->value);
13552 return build_constructor (type, vals);
13554 /* The bitfield references a single constructor element. */
13555 else if (idx + n <= (idx / k + 1) * k)
13557 if (CONSTRUCTOR_NELTS (arg0) <= idx / k)
13558 return build_zero_cst (type);
13559 else if (n == k)
13560 return CONSTRUCTOR_ELT (arg0, idx / k)->value;
13561 else
13562 return fold_build3_loc (loc, code, type,
13563 CONSTRUCTOR_ELT (arg0, idx / k)->value, op1,
13564 build_int_cst (TREE_TYPE (op2), (idx % k) * width));
13569 /* A bit-field-ref that referenced the full argument can be stripped. */
13570 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13571 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_to_uhwi (arg1)
13572 && integer_zerop (op2))
13573 return fold_convert_loc (loc, type, arg0);
13575 /* On constants we can use native encode/interpret to constant
13576 fold (nearly) all BIT_FIELD_REFs. */
13577 if (CONSTANT_CLASS_P (arg0)
13578 && can_native_interpret_type_p (type)
13579 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0)))
13580 /* This limitation should not be necessary, we just need to
13581 round this up to mode size. */
13582 && tree_to_uhwi (op1) % BITS_PER_UNIT == 0
13583 /* Need bit-shifting of the buffer to relax the following. */
13584 && tree_to_uhwi (op2) % BITS_PER_UNIT == 0)
13586 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13587 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13588 unsigned HOST_WIDE_INT clen;
13589 clen = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0)));
13590 /* ??? We cannot tell native_encode_expr to start at
13591 some random byte only. So limit us to a reasonable amount
13592 of work. */
13593 if (clen <= 4096)
13595 unsigned char *b = XALLOCAVEC (unsigned char, clen);
13596 unsigned HOST_WIDE_INT len = native_encode_expr (arg0, b, clen);
13597 if (len > 0
13598 && len * BITS_PER_UNIT >= bitpos + bitsize)
13600 tree v = native_interpret_expr (type,
13601 b + bitpos / BITS_PER_UNIT,
13602 bitsize / BITS_PER_UNIT);
13603 if (v)
13604 return v;
13609 return NULL_TREE;
13611 case FMA_EXPR:
13612 /* For integers we can decompose the FMA if possible. */
13613 if (TREE_CODE (arg0) == INTEGER_CST
13614 && TREE_CODE (arg1) == INTEGER_CST)
13615 return fold_build2_loc (loc, PLUS_EXPR, type,
13616 const_binop (MULT_EXPR, arg0, arg1), arg2);
13617 if (integer_zerop (arg2))
13618 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
13620 return fold_fma (loc, type, arg0, arg1, arg2);
13622 case VEC_PERM_EXPR:
13623 if (TREE_CODE (arg2) == VECTOR_CST)
13625 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
13626 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
13627 unsigned char *sel2 = sel + nelts;
13628 bool need_mask_canon = false;
13629 bool need_mask_canon2 = false;
13630 bool all_in_vec0 = true;
13631 bool all_in_vec1 = true;
13632 bool maybe_identity = true;
13633 bool single_arg = (op0 == op1);
13634 bool changed = false;
13636 mask2 = 2 * nelts - 1;
13637 mask = single_arg ? (nelts - 1) : mask2;
13638 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
13639 for (i = 0; i < nelts; i++)
13641 tree val = VECTOR_CST_ELT (arg2, i);
13642 if (TREE_CODE (val) != INTEGER_CST)
13643 return NULL_TREE;
13645 /* Make sure that the perm value is in an acceptable
13646 range. */
13647 wide_int t = val;
13648 need_mask_canon |= wi::gtu_p (t, mask);
13649 need_mask_canon2 |= wi::gtu_p (t, mask2);
13650 sel[i] = t.to_uhwi () & mask;
13651 sel2[i] = t.to_uhwi () & mask2;
13653 if (sel[i] < nelts)
13654 all_in_vec1 = false;
13655 else
13656 all_in_vec0 = false;
13658 if ((sel[i] & (nelts-1)) != i)
13659 maybe_identity = false;
13662 if (maybe_identity)
13664 if (all_in_vec0)
13665 return op0;
13666 if (all_in_vec1)
13667 return op1;
13670 if (all_in_vec0)
13671 op1 = op0;
13672 else if (all_in_vec1)
13674 op0 = op1;
13675 for (i = 0; i < nelts; i++)
13676 sel[i] -= nelts;
13677 need_mask_canon = true;
13680 if ((TREE_CODE (op0) == VECTOR_CST
13681 || TREE_CODE (op0) == CONSTRUCTOR)
13682 && (TREE_CODE (op1) == VECTOR_CST
13683 || TREE_CODE (op1) == CONSTRUCTOR))
13685 tree t = fold_vec_perm (type, op0, op1, sel);
13686 if (t != NULL_TREE)
13687 return t;
13690 if (op0 == op1 && !single_arg)
13691 changed = true;
13693 /* Some targets are deficient and fail to expand a single
13694 argument permutation while still allowing an equivalent
13695 2-argument version. */
13696 if (need_mask_canon && arg2 == op2
13697 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
13698 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
13700 need_mask_canon = need_mask_canon2;
13701 sel = sel2;
13704 if (need_mask_canon && arg2 == op2)
13706 tree *tsel = XALLOCAVEC (tree, nelts);
13707 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
13708 for (i = 0; i < nelts; i++)
13709 tsel[i] = build_int_cst (eltype, sel[i]);
13710 op2 = build_vector (TREE_TYPE (arg2), tsel);
13711 changed = true;
13714 if (changed)
13715 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
13717 return NULL_TREE;
13719 default:
13720 return NULL_TREE;
13721 } /* switch (code) */
13724 /* Perform constant folding and related simplification of EXPR.
13725 The related simplifications include x*1 => x, x*0 => 0, etc.,
13726 and application of the associative law.
13727 NOP_EXPR conversions may be removed freely (as long as we
13728 are careful not to change the type of the overall expression).
13729 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13730 but we can constant-fold them if they have constant operands. */
13732 #ifdef ENABLE_FOLD_CHECKING
13733 # define fold(x) fold_1 (x)
13734 static tree fold_1 (tree);
13735 static
13736 #endif
13737 tree
13738 fold (tree expr)
13740 const tree t = expr;
13741 enum tree_code code = TREE_CODE (t);
13742 enum tree_code_class kind = TREE_CODE_CLASS (code);
13743 tree tem;
13744 location_t loc = EXPR_LOCATION (expr);
13746 /* Return right away if a constant. */
13747 if (kind == tcc_constant)
13748 return t;
13750 /* CALL_EXPR-like objects with variable numbers of operands are
13751 treated specially. */
13752 if (kind == tcc_vl_exp)
13754 if (code == CALL_EXPR)
13756 tem = fold_call_expr (loc, expr, false);
13757 return tem ? tem : expr;
13759 return expr;
13762 if (IS_EXPR_CODE_CLASS (kind))
13764 tree type = TREE_TYPE (t);
13765 tree op0, op1, op2;
13767 switch (TREE_CODE_LENGTH (code))
13769 case 1:
13770 op0 = TREE_OPERAND (t, 0);
13771 tem = fold_unary_loc (loc, code, type, op0);
13772 return tem ? tem : expr;
13773 case 2:
13774 op0 = TREE_OPERAND (t, 0);
13775 op1 = TREE_OPERAND (t, 1);
13776 tem = fold_binary_loc (loc, code, type, op0, op1);
13777 return tem ? tem : expr;
13778 case 3:
13779 op0 = TREE_OPERAND (t, 0);
13780 op1 = TREE_OPERAND (t, 1);
13781 op2 = TREE_OPERAND (t, 2);
13782 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13783 return tem ? tem : expr;
13784 default:
13785 break;
13789 switch (code)
13791 case ARRAY_REF:
13793 tree op0 = TREE_OPERAND (t, 0);
13794 tree op1 = TREE_OPERAND (t, 1);
13796 if (TREE_CODE (op1) == INTEGER_CST
13797 && TREE_CODE (op0) == CONSTRUCTOR
13798 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13800 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (op0);
13801 unsigned HOST_WIDE_INT end = vec_safe_length (elts);
13802 unsigned HOST_WIDE_INT begin = 0;
13804 /* Find a matching index by means of a binary search. */
13805 while (begin != end)
13807 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13808 tree index = (*elts)[middle].index;
13810 if (TREE_CODE (index) == INTEGER_CST
13811 && tree_int_cst_lt (index, op1))
13812 begin = middle + 1;
13813 else if (TREE_CODE (index) == INTEGER_CST
13814 && tree_int_cst_lt (op1, index))
13815 end = middle;
13816 else if (TREE_CODE (index) == RANGE_EXPR
13817 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13818 begin = middle + 1;
13819 else if (TREE_CODE (index) == RANGE_EXPR
13820 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13821 end = middle;
13822 else
13823 return (*elts)[middle].value;
13827 return t;
13830 /* Return a VECTOR_CST if possible. */
13831 case CONSTRUCTOR:
13833 tree type = TREE_TYPE (t);
13834 if (TREE_CODE (type) != VECTOR_TYPE)
13835 return t;
13837 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
13838 unsigned HOST_WIDE_INT idx, pos = 0;
13839 tree value;
13841 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, value)
13843 if (!CONSTANT_CLASS_P (value))
13844 return t;
13845 if (TREE_CODE (value) == VECTOR_CST)
13847 for (unsigned i = 0; i < VECTOR_CST_NELTS (value); ++i)
13848 vec[pos++] = VECTOR_CST_ELT (value, i);
13850 else
13851 vec[pos++] = value;
13853 for (; pos < TYPE_VECTOR_SUBPARTS (type); ++pos)
13854 vec[pos] = build_zero_cst (TREE_TYPE (type));
13856 return build_vector (type, vec);
13859 case CONST_DECL:
13860 return fold (DECL_INITIAL (t));
13862 default:
13863 return t;
13864 } /* switch (code) */
13867 #ifdef ENABLE_FOLD_CHECKING
13868 #undef fold
13870 static void fold_checksum_tree (const_tree, struct md5_ctx *,
13871 hash_table<pointer_hash<const tree_node> > *);
13872 static void fold_check_failed (const_tree, const_tree);
13873 void print_fold_checksum (const_tree);
13875 /* When --enable-checking=fold, compute a digest of expr before
13876 and after actual fold call to see if fold did not accidentally
13877 change original expr. */
13879 tree
13880 fold (tree expr)
13882 tree ret;
13883 struct md5_ctx ctx;
13884 unsigned char checksum_before[16], checksum_after[16];
13885 hash_table<pointer_hash<const tree_node> > ht (32);
13887 md5_init_ctx (&ctx);
13888 fold_checksum_tree (expr, &ctx, &ht);
13889 md5_finish_ctx (&ctx, checksum_before);
13890 ht.empty ();
13892 ret = fold_1 (expr);
13894 md5_init_ctx (&ctx);
13895 fold_checksum_tree (expr, &ctx, &ht);
13896 md5_finish_ctx (&ctx, checksum_after);
13898 if (memcmp (checksum_before, checksum_after, 16))
13899 fold_check_failed (expr, ret);
13901 return ret;
13904 void
13905 print_fold_checksum (const_tree expr)
13907 struct md5_ctx ctx;
13908 unsigned char checksum[16], cnt;
13909 hash_table<pointer_hash<const tree_node> > ht (32);
13911 md5_init_ctx (&ctx);
13912 fold_checksum_tree (expr, &ctx, &ht);
13913 md5_finish_ctx (&ctx, checksum);
13914 for (cnt = 0; cnt < 16; ++cnt)
13915 fprintf (stderr, "%02x", checksum[cnt]);
13916 putc ('\n', stderr);
13919 static void
13920 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13922 internal_error ("fold check: original tree changed by fold");
13925 static void
13926 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13927 hash_table<pointer_hash <const tree_node> > *ht)
13929 const tree_node **slot;
13930 enum tree_code code;
13931 union tree_node buf;
13932 int i, len;
13934 recursive_label:
13935 if (expr == NULL)
13936 return;
13937 slot = ht->find_slot (expr, INSERT);
13938 if (*slot != NULL)
13939 return;
13940 *slot = expr;
13941 code = TREE_CODE (expr);
13942 if (TREE_CODE_CLASS (code) == tcc_declaration
13943 && HAS_DECL_ASSEMBLER_NAME_P (expr))
13945 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13946 memcpy ((char *) &buf, expr, tree_size (expr));
13947 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13948 buf.decl_with_vis.symtab_node = NULL;
13949 expr = (tree) &buf;
13951 else if (TREE_CODE_CLASS (code) == tcc_type
13952 && (TYPE_POINTER_TO (expr)
13953 || TYPE_REFERENCE_TO (expr)
13954 || TYPE_CACHED_VALUES_P (expr)
13955 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13956 || TYPE_NEXT_VARIANT (expr)))
13958 /* Allow these fields to be modified. */
13959 tree tmp;
13960 memcpy ((char *) &buf, expr, tree_size (expr));
13961 expr = tmp = (tree) &buf;
13962 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13963 TYPE_POINTER_TO (tmp) = NULL;
13964 TYPE_REFERENCE_TO (tmp) = NULL;
13965 TYPE_NEXT_VARIANT (tmp) = NULL;
13966 if (TYPE_CACHED_VALUES_P (tmp))
13968 TYPE_CACHED_VALUES_P (tmp) = 0;
13969 TYPE_CACHED_VALUES (tmp) = NULL;
13972 md5_process_bytes (expr, tree_size (expr), ctx);
13973 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13974 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13975 if (TREE_CODE_CLASS (code) != tcc_type
13976 && TREE_CODE_CLASS (code) != tcc_declaration
13977 && code != TREE_LIST
13978 && code != SSA_NAME
13979 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13980 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13981 switch (TREE_CODE_CLASS (code))
13983 case tcc_constant:
13984 switch (code)
13986 case STRING_CST:
13987 md5_process_bytes (TREE_STRING_POINTER (expr),
13988 TREE_STRING_LENGTH (expr), ctx);
13989 break;
13990 case COMPLEX_CST:
13991 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13992 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13993 break;
13994 case VECTOR_CST:
13995 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
13996 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
13997 break;
13998 default:
13999 break;
14001 break;
14002 case tcc_exceptional:
14003 switch (code)
14005 case TREE_LIST:
14006 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
14007 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
14008 expr = TREE_CHAIN (expr);
14009 goto recursive_label;
14010 break;
14011 case TREE_VEC:
14012 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
14013 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
14014 break;
14015 default:
14016 break;
14018 break;
14019 case tcc_expression:
14020 case tcc_reference:
14021 case tcc_comparison:
14022 case tcc_unary:
14023 case tcc_binary:
14024 case tcc_statement:
14025 case tcc_vl_exp:
14026 len = TREE_OPERAND_LENGTH (expr);
14027 for (i = 0; i < len; ++i)
14028 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
14029 break;
14030 case tcc_declaration:
14031 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
14032 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
14033 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
14035 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
14036 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
14037 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
14038 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
14039 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
14042 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
14044 if (TREE_CODE (expr) == FUNCTION_DECL)
14046 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
14047 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
14049 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
14051 break;
14052 case tcc_type:
14053 if (TREE_CODE (expr) == ENUMERAL_TYPE)
14054 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
14055 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
14056 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
14057 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
14058 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
14059 if (INTEGRAL_TYPE_P (expr)
14060 || SCALAR_FLOAT_TYPE_P (expr))
14062 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
14063 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
14065 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
14066 if (TREE_CODE (expr) == RECORD_TYPE
14067 || TREE_CODE (expr) == UNION_TYPE
14068 || TREE_CODE (expr) == QUAL_UNION_TYPE)
14069 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
14070 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
14071 break;
14072 default:
14073 break;
14077 /* Helper function for outputting the checksum of a tree T. When
14078 debugging with gdb, you can "define mynext" to be "next" followed
14079 by "call debug_fold_checksum (op0)", then just trace down till the
14080 outputs differ. */
14082 DEBUG_FUNCTION void
14083 debug_fold_checksum (const_tree t)
14085 int i;
14086 unsigned char checksum[16];
14087 struct md5_ctx ctx;
14088 hash_table<pointer_hash<const tree_node> > ht (32);
14090 md5_init_ctx (&ctx);
14091 fold_checksum_tree (t, &ctx, &ht);
14092 md5_finish_ctx (&ctx, checksum);
14093 ht.empty ();
14095 for (i = 0; i < 16; i++)
14096 fprintf (stderr, "%d ", checksum[i]);
14098 fprintf (stderr, "\n");
14101 #endif
14103 /* Fold a unary tree expression with code CODE of type TYPE with an
14104 operand OP0. LOC is the location of the resulting expression.
14105 Return a folded expression if successful. Otherwise, return a tree
14106 expression with code CODE of type TYPE with an operand OP0. */
14108 tree
14109 fold_build1_stat_loc (location_t loc,
14110 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
14112 tree tem;
14113 #ifdef ENABLE_FOLD_CHECKING
14114 unsigned char checksum_before[16], checksum_after[16];
14115 struct md5_ctx ctx;
14116 hash_table<pointer_hash<const tree_node> > ht (32);
14118 md5_init_ctx (&ctx);
14119 fold_checksum_tree (op0, &ctx, &ht);
14120 md5_finish_ctx (&ctx, checksum_before);
14121 ht.empty ();
14122 #endif
14124 tem = fold_unary_loc (loc, code, type, op0);
14125 if (!tem)
14126 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
14128 #ifdef ENABLE_FOLD_CHECKING
14129 md5_init_ctx (&ctx);
14130 fold_checksum_tree (op0, &ctx, &ht);
14131 md5_finish_ctx (&ctx, checksum_after);
14133 if (memcmp (checksum_before, checksum_after, 16))
14134 fold_check_failed (op0, tem);
14135 #endif
14136 return tem;
14139 /* Fold a binary tree expression with code CODE of type TYPE with
14140 operands OP0 and OP1. LOC is the location of the resulting
14141 expression. Return a folded expression if successful. Otherwise,
14142 return a tree expression with code CODE of type TYPE with operands
14143 OP0 and OP1. */
14145 tree
14146 fold_build2_stat_loc (location_t loc,
14147 enum tree_code code, tree type, tree op0, tree op1
14148 MEM_STAT_DECL)
14150 tree tem;
14151 #ifdef ENABLE_FOLD_CHECKING
14152 unsigned char checksum_before_op0[16],
14153 checksum_before_op1[16],
14154 checksum_after_op0[16],
14155 checksum_after_op1[16];
14156 struct md5_ctx ctx;
14157 hash_table<pointer_hash<const tree_node> > ht (32);
14159 md5_init_ctx (&ctx);
14160 fold_checksum_tree (op0, &ctx, &ht);
14161 md5_finish_ctx (&ctx, checksum_before_op0);
14162 ht.empty ();
14164 md5_init_ctx (&ctx);
14165 fold_checksum_tree (op1, &ctx, &ht);
14166 md5_finish_ctx (&ctx, checksum_before_op1);
14167 ht.empty ();
14168 #endif
14170 tem = fold_binary_loc (loc, code, type, op0, op1);
14171 if (!tem)
14172 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
14174 #ifdef ENABLE_FOLD_CHECKING
14175 md5_init_ctx (&ctx);
14176 fold_checksum_tree (op0, &ctx, &ht);
14177 md5_finish_ctx (&ctx, checksum_after_op0);
14178 ht.empty ();
14180 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14181 fold_check_failed (op0, tem);
14183 md5_init_ctx (&ctx);
14184 fold_checksum_tree (op1, &ctx, &ht);
14185 md5_finish_ctx (&ctx, checksum_after_op1);
14187 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14188 fold_check_failed (op1, tem);
14189 #endif
14190 return tem;
14193 /* Fold a ternary tree expression with code CODE of type TYPE with
14194 operands OP0, OP1, and OP2. Return a folded expression if
14195 successful. Otherwise, return a tree expression with code CODE of
14196 type TYPE with operands OP0, OP1, and OP2. */
14198 tree
14199 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14200 tree op0, tree op1, tree op2 MEM_STAT_DECL)
14202 tree tem;
14203 #ifdef ENABLE_FOLD_CHECKING
14204 unsigned char checksum_before_op0[16],
14205 checksum_before_op1[16],
14206 checksum_before_op2[16],
14207 checksum_after_op0[16],
14208 checksum_after_op1[16],
14209 checksum_after_op2[16];
14210 struct md5_ctx ctx;
14211 hash_table<pointer_hash<const tree_node> > ht (32);
14213 md5_init_ctx (&ctx);
14214 fold_checksum_tree (op0, &ctx, &ht);
14215 md5_finish_ctx (&ctx, checksum_before_op0);
14216 ht.empty ();
14218 md5_init_ctx (&ctx);
14219 fold_checksum_tree (op1, &ctx, &ht);
14220 md5_finish_ctx (&ctx, checksum_before_op1);
14221 ht.empty ();
14223 md5_init_ctx (&ctx);
14224 fold_checksum_tree (op2, &ctx, &ht);
14225 md5_finish_ctx (&ctx, checksum_before_op2);
14226 ht.empty ();
14227 #endif
14229 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14230 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14231 if (!tem)
14232 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14234 #ifdef ENABLE_FOLD_CHECKING
14235 md5_init_ctx (&ctx);
14236 fold_checksum_tree (op0, &ctx, &ht);
14237 md5_finish_ctx (&ctx, checksum_after_op0);
14238 ht.empty ();
14240 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14241 fold_check_failed (op0, tem);
14243 md5_init_ctx (&ctx);
14244 fold_checksum_tree (op1, &ctx, &ht);
14245 md5_finish_ctx (&ctx, checksum_after_op1);
14246 ht.empty ();
14248 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14249 fold_check_failed (op1, tem);
14251 md5_init_ctx (&ctx);
14252 fold_checksum_tree (op2, &ctx, &ht);
14253 md5_finish_ctx (&ctx, checksum_after_op2);
14255 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14256 fold_check_failed (op2, tem);
14257 #endif
14258 return tem;
14261 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14262 arguments in ARGARRAY, and a null static chain.
14263 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14264 of type TYPE from the given operands as constructed by build_call_array. */
14266 tree
14267 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14268 int nargs, tree *argarray)
14270 tree tem;
14271 #ifdef ENABLE_FOLD_CHECKING
14272 unsigned char checksum_before_fn[16],
14273 checksum_before_arglist[16],
14274 checksum_after_fn[16],
14275 checksum_after_arglist[16];
14276 struct md5_ctx ctx;
14277 hash_table<pointer_hash<const tree_node> > ht (32);
14278 int i;
14280 md5_init_ctx (&ctx);
14281 fold_checksum_tree (fn, &ctx, &ht);
14282 md5_finish_ctx (&ctx, checksum_before_fn);
14283 ht.empty ();
14285 md5_init_ctx (&ctx);
14286 for (i = 0; i < nargs; i++)
14287 fold_checksum_tree (argarray[i], &ctx, &ht);
14288 md5_finish_ctx (&ctx, checksum_before_arglist);
14289 ht.empty ();
14290 #endif
14292 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14293 if (!tem)
14294 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
14296 #ifdef ENABLE_FOLD_CHECKING
14297 md5_init_ctx (&ctx);
14298 fold_checksum_tree (fn, &ctx, &ht);
14299 md5_finish_ctx (&ctx, checksum_after_fn);
14300 ht.empty ();
14302 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14303 fold_check_failed (fn, tem);
14305 md5_init_ctx (&ctx);
14306 for (i = 0; i < nargs; i++)
14307 fold_checksum_tree (argarray[i], &ctx, &ht);
14308 md5_finish_ctx (&ctx, checksum_after_arglist);
14310 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14311 fold_check_failed (NULL_TREE, tem);
14312 #endif
14313 return tem;
14316 /* Perform constant folding and related simplification of initializer
14317 expression EXPR. These behave identically to "fold_buildN" but ignore
14318 potential run-time traps and exceptions that fold must preserve. */
14320 #define START_FOLD_INIT \
14321 int saved_signaling_nans = flag_signaling_nans;\
14322 int saved_trapping_math = flag_trapping_math;\
14323 int saved_rounding_math = flag_rounding_math;\
14324 int saved_trapv = flag_trapv;\
14325 int saved_folding_initializer = folding_initializer;\
14326 flag_signaling_nans = 0;\
14327 flag_trapping_math = 0;\
14328 flag_rounding_math = 0;\
14329 flag_trapv = 0;\
14330 folding_initializer = 1;
14332 #define END_FOLD_INIT \
14333 flag_signaling_nans = saved_signaling_nans;\
14334 flag_trapping_math = saved_trapping_math;\
14335 flag_rounding_math = saved_rounding_math;\
14336 flag_trapv = saved_trapv;\
14337 folding_initializer = saved_folding_initializer;
14339 tree
14340 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14341 tree type, tree op)
14343 tree result;
14344 START_FOLD_INIT;
14346 result = fold_build1_loc (loc, code, type, op);
14348 END_FOLD_INIT;
14349 return result;
14352 tree
14353 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14354 tree type, tree op0, tree op1)
14356 tree result;
14357 START_FOLD_INIT;
14359 result = fold_build2_loc (loc, code, type, op0, op1);
14361 END_FOLD_INIT;
14362 return result;
14365 tree
14366 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14367 int nargs, tree *argarray)
14369 tree result;
14370 START_FOLD_INIT;
14372 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14374 END_FOLD_INIT;
14375 return result;
14378 #undef START_FOLD_INIT
14379 #undef END_FOLD_INIT
14381 /* Determine if first argument is a multiple of second argument. Return 0 if
14382 it is not, or we cannot easily determined it to be.
14384 An example of the sort of thing we care about (at this point; this routine
14385 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14386 fold cases do now) is discovering that
14388 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14390 is a multiple of
14392 SAVE_EXPR (J * 8)
14394 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14396 This code also handles discovering that
14398 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14400 is a multiple of 8 so we don't have to worry about dealing with a
14401 possible remainder.
14403 Note that we *look* inside a SAVE_EXPR only to determine how it was
14404 calculated; it is not safe for fold to do much of anything else with the
14405 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14406 at run time. For example, the latter example above *cannot* be implemented
14407 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14408 evaluation time of the original SAVE_EXPR is not necessarily the same at
14409 the time the new expression is evaluated. The only optimization of this
14410 sort that would be valid is changing
14412 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14414 divided by 8 to
14416 SAVE_EXPR (I) * SAVE_EXPR (J)
14418 (where the same SAVE_EXPR (J) is used in the original and the
14419 transformed version). */
14422 multiple_of_p (tree type, const_tree top, const_tree bottom)
14424 if (operand_equal_p (top, bottom, 0))
14425 return 1;
14427 if (TREE_CODE (type) != INTEGER_TYPE)
14428 return 0;
14430 switch (TREE_CODE (top))
14432 case BIT_AND_EXPR:
14433 /* Bitwise and provides a power of two multiple. If the mask is
14434 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14435 if (!integer_pow2p (bottom))
14436 return 0;
14437 /* FALLTHRU */
14439 case MULT_EXPR:
14440 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14441 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14443 case PLUS_EXPR:
14444 case MINUS_EXPR:
14445 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14446 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14448 case LSHIFT_EXPR:
14449 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14451 tree op1, t1;
14453 op1 = TREE_OPERAND (top, 1);
14454 /* const_binop may not detect overflow correctly,
14455 so check for it explicitly here. */
14456 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
14457 && 0 != (t1 = fold_convert (type,
14458 const_binop (LSHIFT_EXPR,
14459 size_one_node,
14460 op1)))
14461 && !TREE_OVERFLOW (t1))
14462 return multiple_of_p (type, t1, bottom);
14464 return 0;
14466 case NOP_EXPR:
14467 /* Can't handle conversions from non-integral or wider integral type. */
14468 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14469 || (TYPE_PRECISION (type)
14470 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14471 return 0;
14473 /* .. fall through ... */
14475 case SAVE_EXPR:
14476 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14478 case COND_EXPR:
14479 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14480 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14482 case INTEGER_CST:
14483 if (TREE_CODE (bottom) != INTEGER_CST
14484 || integer_zerop (bottom)
14485 || (TYPE_UNSIGNED (type)
14486 && (tree_int_cst_sgn (top) < 0
14487 || tree_int_cst_sgn (bottom) < 0)))
14488 return 0;
14489 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14490 SIGNED);
14492 default:
14493 return 0;
14497 /* Return true if CODE or TYPE is known to be non-negative. */
14499 static bool
14500 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14502 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14503 && truth_value_p (code))
14504 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14505 have a signed:1 type (where the value is -1 and 0). */
14506 return true;
14507 return false;
14510 /* Return true if (CODE OP0) is known to be non-negative. If the return
14511 value is based on the assumption that signed overflow is undefined,
14512 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14513 *STRICT_OVERFLOW_P. */
14515 bool
14516 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14517 bool *strict_overflow_p)
14519 if (TYPE_UNSIGNED (type))
14520 return true;
14522 switch (code)
14524 case ABS_EXPR:
14525 /* We can't return 1 if flag_wrapv is set because
14526 ABS_EXPR<INT_MIN> = INT_MIN. */
14527 if (!ANY_INTEGRAL_TYPE_P (type))
14528 return true;
14529 if (TYPE_OVERFLOW_UNDEFINED (type))
14531 *strict_overflow_p = true;
14532 return true;
14534 break;
14536 case NON_LVALUE_EXPR:
14537 case FLOAT_EXPR:
14538 case FIX_TRUNC_EXPR:
14539 return tree_expr_nonnegative_warnv_p (op0,
14540 strict_overflow_p);
14542 CASE_CONVERT:
14544 tree inner_type = TREE_TYPE (op0);
14545 tree outer_type = type;
14547 if (TREE_CODE (outer_type) == REAL_TYPE)
14549 if (TREE_CODE (inner_type) == REAL_TYPE)
14550 return tree_expr_nonnegative_warnv_p (op0,
14551 strict_overflow_p);
14552 if (INTEGRAL_TYPE_P (inner_type))
14554 if (TYPE_UNSIGNED (inner_type))
14555 return true;
14556 return tree_expr_nonnegative_warnv_p (op0,
14557 strict_overflow_p);
14560 else if (INTEGRAL_TYPE_P (outer_type))
14562 if (TREE_CODE (inner_type) == REAL_TYPE)
14563 return tree_expr_nonnegative_warnv_p (op0,
14564 strict_overflow_p);
14565 if (INTEGRAL_TYPE_P (inner_type))
14566 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14567 && TYPE_UNSIGNED (inner_type);
14570 break;
14572 default:
14573 return tree_simple_nonnegative_warnv_p (code, type);
14576 /* We don't know sign of `t', so be conservative and return false. */
14577 return false;
14580 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14581 value is based on the assumption that signed overflow is undefined,
14582 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14583 *STRICT_OVERFLOW_P. */
14585 bool
14586 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14587 tree op1, bool *strict_overflow_p)
14589 if (TYPE_UNSIGNED (type))
14590 return true;
14592 switch (code)
14594 case POINTER_PLUS_EXPR:
14595 case PLUS_EXPR:
14596 if (FLOAT_TYPE_P (type))
14597 return (tree_expr_nonnegative_warnv_p (op0,
14598 strict_overflow_p)
14599 && tree_expr_nonnegative_warnv_p (op1,
14600 strict_overflow_p));
14602 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14603 both unsigned and at least 2 bits shorter than the result. */
14604 if (TREE_CODE (type) == INTEGER_TYPE
14605 && TREE_CODE (op0) == NOP_EXPR
14606 && TREE_CODE (op1) == NOP_EXPR)
14608 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14609 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14610 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14611 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14613 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14614 TYPE_PRECISION (inner2)) + 1;
14615 return prec < TYPE_PRECISION (type);
14618 break;
14620 case MULT_EXPR:
14621 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14623 /* x * x is always non-negative for floating point x
14624 or without overflow. */
14625 if (operand_equal_p (op0, op1, 0)
14626 || (tree_expr_nonnegative_warnv_p (op0, strict_overflow_p)
14627 && tree_expr_nonnegative_warnv_p (op1, strict_overflow_p)))
14629 if (ANY_INTEGRAL_TYPE_P (type)
14630 && TYPE_OVERFLOW_UNDEFINED (type))
14631 *strict_overflow_p = true;
14632 return true;
14636 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14637 both unsigned and their total bits is shorter than the result. */
14638 if (TREE_CODE (type) == INTEGER_TYPE
14639 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14640 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14642 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14643 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14644 : TREE_TYPE (op0);
14645 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14646 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14647 : TREE_TYPE (op1);
14649 bool unsigned0 = TYPE_UNSIGNED (inner0);
14650 bool unsigned1 = TYPE_UNSIGNED (inner1);
14652 if (TREE_CODE (op0) == INTEGER_CST)
14653 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14655 if (TREE_CODE (op1) == INTEGER_CST)
14656 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14658 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14659 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14661 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14662 ? tree_int_cst_min_precision (op0, UNSIGNED)
14663 : TYPE_PRECISION (inner0);
14665 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14666 ? tree_int_cst_min_precision (op1, UNSIGNED)
14667 : TYPE_PRECISION (inner1);
14669 return precision0 + precision1 < TYPE_PRECISION (type);
14672 return false;
14674 case BIT_AND_EXPR:
14675 case MAX_EXPR:
14676 return (tree_expr_nonnegative_warnv_p (op0,
14677 strict_overflow_p)
14678 || tree_expr_nonnegative_warnv_p (op1,
14679 strict_overflow_p));
14681 case BIT_IOR_EXPR:
14682 case BIT_XOR_EXPR:
14683 case MIN_EXPR:
14684 case RDIV_EXPR:
14685 case TRUNC_DIV_EXPR:
14686 case CEIL_DIV_EXPR:
14687 case FLOOR_DIV_EXPR:
14688 case ROUND_DIV_EXPR:
14689 return (tree_expr_nonnegative_warnv_p (op0,
14690 strict_overflow_p)
14691 && tree_expr_nonnegative_warnv_p (op1,
14692 strict_overflow_p));
14694 case TRUNC_MOD_EXPR:
14695 case CEIL_MOD_EXPR:
14696 case FLOOR_MOD_EXPR:
14697 case ROUND_MOD_EXPR:
14698 return tree_expr_nonnegative_warnv_p (op0,
14699 strict_overflow_p);
14700 default:
14701 return tree_simple_nonnegative_warnv_p (code, type);
14704 /* We don't know sign of `t', so be conservative and return false. */
14705 return false;
14708 /* Return true if T is known to be non-negative. If the return
14709 value is based on the assumption that signed overflow is undefined,
14710 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14711 *STRICT_OVERFLOW_P. */
14713 bool
14714 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14716 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14717 return true;
14719 switch (TREE_CODE (t))
14721 case INTEGER_CST:
14722 return tree_int_cst_sgn (t) >= 0;
14724 case REAL_CST:
14725 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14727 case FIXED_CST:
14728 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14730 case COND_EXPR:
14731 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14732 strict_overflow_p)
14733 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14734 strict_overflow_p));
14735 default:
14736 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14737 TREE_TYPE (t));
14739 /* We don't know sign of `t', so be conservative and return false. */
14740 return false;
14743 /* Return true if T is known to be non-negative. If the return
14744 value is based on the assumption that signed overflow is undefined,
14745 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14746 *STRICT_OVERFLOW_P. */
14748 bool
14749 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14750 tree arg0, tree arg1, bool *strict_overflow_p)
14752 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14753 switch (DECL_FUNCTION_CODE (fndecl))
14755 CASE_FLT_FN (BUILT_IN_ACOS):
14756 CASE_FLT_FN (BUILT_IN_ACOSH):
14757 CASE_FLT_FN (BUILT_IN_CABS):
14758 CASE_FLT_FN (BUILT_IN_COSH):
14759 CASE_FLT_FN (BUILT_IN_ERFC):
14760 CASE_FLT_FN (BUILT_IN_EXP):
14761 CASE_FLT_FN (BUILT_IN_EXP10):
14762 CASE_FLT_FN (BUILT_IN_EXP2):
14763 CASE_FLT_FN (BUILT_IN_FABS):
14764 CASE_FLT_FN (BUILT_IN_FDIM):
14765 CASE_FLT_FN (BUILT_IN_HYPOT):
14766 CASE_FLT_FN (BUILT_IN_POW10):
14767 CASE_INT_FN (BUILT_IN_FFS):
14768 CASE_INT_FN (BUILT_IN_PARITY):
14769 CASE_INT_FN (BUILT_IN_POPCOUNT):
14770 CASE_INT_FN (BUILT_IN_CLZ):
14771 CASE_INT_FN (BUILT_IN_CLRSB):
14772 case BUILT_IN_BSWAP32:
14773 case BUILT_IN_BSWAP64:
14774 /* Always true. */
14775 return true;
14777 CASE_FLT_FN (BUILT_IN_SQRT):
14778 /* sqrt(-0.0) is -0.0. */
14779 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
14780 return true;
14781 return tree_expr_nonnegative_warnv_p (arg0,
14782 strict_overflow_p);
14784 CASE_FLT_FN (BUILT_IN_ASINH):
14785 CASE_FLT_FN (BUILT_IN_ATAN):
14786 CASE_FLT_FN (BUILT_IN_ATANH):
14787 CASE_FLT_FN (BUILT_IN_CBRT):
14788 CASE_FLT_FN (BUILT_IN_CEIL):
14789 CASE_FLT_FN (BUILT_IN_ERF):
14790 CASE_FLT_FN (BUILT_IN_EXPM1):
14791 CASE_FLT_FN (BUILT_IN_FLOOR):
14792 CASE_FLT_FN (BUILT_IN_FMOD):
14793 CASE_FLT_FN (BUILT_IN_FREXP):
14794 CASE_FLT_FN (BUILT_IN_ICEIL):
14795 CASE_FLT_FN (BUILT_IN_IFLOOR):
14796 CASE_FLT_FN (BUILT_IN_IRINT):
14797 CASE_FLT_FN (BUILT_IN_IROUND):
14798 CASE_FLT_FN (BUILT_IN_LCEIL):
14799 CASE_FLT_FN (BUILT_IN_LDEXP):
14800 CASE_FLT_FN (BUILT_IN_LFLOOR):
14801 CASE_FLT_FN (BUILT_IN_LLCEIL):
14802 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14803 CASE_FLT_FN (BUILT_IN_LLRINT):
14804 CASE_FLT_FN (BUILT_IN_LLROUND):
14805 CASE_FLT_FN (BUILT_IN_LRINT):
14806 CASE_FLT_FN (BUILT_IN_LROUND):
14807 CASE_FLT_FN (BUILT_IN_MODF):
14808 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14809 CASE_FLT_FN (BUILT_IN_RINT):
14810 CASE_FLT_FN (BUILT_IN_ROUND):
14811 CASE_FLT_FN (BUILT_IN_SCALB):
14812 CASE_FLT_FN (BUILT_IN_SCALBLN):
14813 CASE_FLT_FN (BUILT_IN_SCALBN):
14814 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14815 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14816 CASE_FLT_FN (BUILT_IN_SINH):
14817 CASE_FLT_FN (BUILT_IN_TANH):
14818 CASE_FLT_FN (BUILT_IN_TRUNC):
14819 /* True if the 1st argument is nonnegative. */
14820 return tree_expr_nonnegative_warnv_p (arg0,
14821 strict_overflow_p);
14823 CASE_FLT_FN (BUILT_IN_FMAX):
14824 /* True if the 1st OR 2nd arguments are nonnegative. */
14825 return (tree_expr_nonnegative_warnv_p (arg0,
14826 strict_overflow_p)
14827 || (tree_expr_nonnegative_warnv_p (arg1,
14828 strict_overflow_p)));
14830 CASE_FLT_FN (BUILT_IN_FMIN):
14831 /* True if the 1st AND 2nd arguments are nonnegative. */
14832 return (tree_expr_nonnegative_warnv_p (arg0,
14833 strict_overflow_p)
14834 && (tree_expr_nonnegative_warnv_p (arg1,
14835 strict_overflow_p)));
14837 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14838 /* True if the 2nd argument is nonnegative. */
14839 return tree_expr_nonnegative_warnv_p (arg1,
14840 strict_overflow_p);
14842 CASE_FLT_FN (BUILT_IN_POWI):
14843 /* True if the 1st argument is nonnegative or the second
14844 argument is an even integer. */
14845 if (TREE_CODE (arg1) == INTEGER_CST
14846 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14847 return true;
14848 return tree_expr_nonnegative_warnv_p (arg0,
14849 strict_overflow_p);
14851 CASE_FLT_FN (BUILT_IN_POW):
14852 /* True if the 1st argument is nonnegative or the second
14853 argument is an even integer valued real. */
14854 if (TREE_CODE (arg1) == REAL_CST)
14856 REAL_VALUE_TYPE c;
14857 HOST_WIDE_INT n;
14859 c = TREE_REAL_CST (arg1);
14860 n = real_to_integer (&c);
14861 if ((n & 1) == 0)
14863 REAL_VALUE_TYPE cint;
14864 real_from_integer (&cint, VOIDmode, n, SIGNED);
14865 if (real_identical (&c, &cint))
14866 return true;
14869 return tree_expr_nonnegative_warnv_p (arg0,
14870 strict_overflow_p);
14872 default:
14873 break;
14875 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14876 type);
14879 /* Return true if T is known to be non-negative. If the return
14880 value is based on the assumption that signed overflow is undefined,
14881 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14882 *STRICT_OVERFLOW_P. */
14884 static bool
14885 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14887 enum tree_code code = TREE_CODE (t);
14888 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14889 return true;
14891 switch (code)
14893 case TARGET_EXPR:
14895 tree temp = TARGET_EXPR_SLOT (t);
14896 t = TARGET_EXPR_INITIAL (t);
14898 /* If the initializer is non-void, then it's a normal expression
14899 that will be assigned to the slot. */
14900 if (!VOID_TYPE_P (t))
14901 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14903 /* Otherwise, the initializer sets the slot in some way. One common
14904 way is an assignment statement at the end of the initializer. */
14905 while (1)
14907 if (TREE_CODE (t) == BIND_EXPR)
14908 t = expr_last (BIND_EXPR_BODY (t));
14909 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14910 || TREE_CODE (t) == TRY_CATCH_EXPR)
14911 t = expr_last (TREE_OPERAND (t, 0));
14912 else if (TREE_CODE (t) == STATEMENT_LIST)
14913 t = expr_last (t);
14914 else
14915 break;
14917 if (TREE_CODE (t) == MODIFY_EXPR
14918 && TREE_OPERAND (t, 0) == temp)
14919 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14920 strict_overflow_p);
14922 return false;
14925 case CALL_EXPR:
14927 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14928 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14930 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14931 get_callee_fndecl (t),
14932 arg0,
14933 arg1,
14934 strict_overflow_p);
14936 case COMPOUND_EXPR:
14937 case MODIFY_EXPR:
14938 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14939 strict_overflow_p);
14940 case BIND_EXPR:
14941 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14942 strict_overflow_p);
14943 case SAVE_EXPR:
14944 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14945 strict_overflow_p);
14947 default:
14948 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14949 TREE_TYPE (t));
14952 /* We don't know sign of `t', so be conservative and return false. */
14953 return false;
14956 /* Return true if T is known to be non-negative. If the return
14957 value is based on the assumption that signed overflow is undefined,
14958 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14959 *STRICT_OVERFLOW_P. */
14961 bool
14962 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14964 enum tree_code code;
14965 if (t == error_mark_node)
14966 return false;
14968 code = TREE_CODE (t);
14969 switch (TREE_CODE_CLASS (code))
14971 case tcc_binary:
14972 case tcc_comparison:
14973 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14974 TREE_TYPE (t),
14975 TREE_OPERAND (t, 0),
14976 TREE_OPERAND (t, 1),
14977 strict_overflow_p);
14979 case tcc_unary:
14980 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14981 TREE_TYPE (t),
14982 TREE_OPERAND (t, 0),
14983 strict_overflow_p);
14985 case tcc_constant:
14986 case tcc_declaration:
14987 case tcc_reference:
14988 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14990 default:
14991 break;
14994 switch (code)
14996 case TRUTH_AND_EXPR:
14997 case TRUTH_OR_EXPR:
14998 case TRUTH_XOR_EXPR:
14999 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15000 TREE_TYPE (t),
15001 TREE_OPERAND (t, 0),
15002 TREE_OPERAND (t, 1),
15003 strict_overflow_p);
15004 case TRUTH_NOT_EXPR:
15005 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15006 TREE_TYPE (t),
15007 TREE_OPERAND (t, 0),
15008 strict_overflow_p);
15010 case COND_EXPR:
15011 case CONSTRUCTOR:
15012 case OBJ_TYPE_REF:
15013 case ASSERT_EXPR:
15014 case ADDR_EXPR:
15015 case WITH_SIZE_EXPR:
15016 case SSA_NAME:
15017 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15019 default:
15020 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
15024 /* Return true if `t' is known to be non-negative. Handle warnings
15025 about undefined signed overflow. */
15027 bool
15028 tree_expr_nonnegative_p (tree t)
15030 bool ret, strict_overflow_p;
15032 strict_overflow_p = false;
15033 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15034 if (strict_overflow_p)
15035 fold_overflow_warning (("assuming signed overflow does not occur when "
15036 "determining that expression is always "
15037 "non-negative"),
15038 WARN_STRICT_OVERFLOW_MISC);
15039 return ret;
15043 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15044 For floating point we further ensure that T is not denormal.
15045 Similar logic is present in nonzero_address in rtlanal.h.
15047 If the return value is based on the assumption that signed overflow
15048 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15049 change *STRICT_OVERFLOW_P. */
15051 bool
15052 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15053 bool *strict_overflow_p)
15055 switch (code)
15057 case ABS_EXPR:
15058 return tree_expr_nonzero_warnv_p (op0,
15059 strict_overflow_p);
15061 case NOP_EXPR:
15063 tree inner_type = TREE_TYPE (op0);
15064 tree outer_type = type;
15066 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15067 && tree_expr_nonzero_warnv_p (op0,
15068 strict_overflow_p));
15070 break;
15072 case NON_LVALUE_EXPR:
15073 return tree_expr_nonzero_warnv_p (op0,
15074 strict_overflow_p);
15076 default:
15077 break;
15080 return false;
15083 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15084 For floating point we further ensure that T is not denormal.
15085 Similar logic is present in nonzero_address in rtlanal.h.
15087 If the return value is based on the assumption that signed overflow
15088 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15089 change *STRICT_OVERFLOW_P. */
15091 bool
15092 tree_binary_nonzero_warnv_p (enum tree_code code,
15093 tree type,
15094 tree op0,
15095 tree op1, bool *strict_overflow_p)
15097 bool sub_strict_overflow_p;
15098 switch (code)
15100 case POINTER_PLUS_EXPR:
15101 case PLUS_EXPR:
15102 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15104 /* With the presence of negative values it is hard
15105 to say something. */
15106 sub_strict_overflow_p = false;
15107 if (!tree_expr_nonnegative_warnv_p (op0,
15108 &sub_strict_overflow_p)
15109 || !tree_expr_nonnegative_warnv_p (op1,
15110 &sub_strict_overflow_p))
15111 return false;
15112 /* One of operands must be positive and the other non-negative. */
15113 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15114 overflows, on a twos-complement machine the sum of two
15115 nonnegative numbers can never be zero. */
15116 return (tree_expr_nonzero_warnv_p (op0,
15117 strict_overflow_p)
15118 || tree_expr_nonzero_warnv_p (op1,
15119 strict_overflow_p));
15121 break;
15123 case MULT_EXPR:
15124 if (TYPE_OVERFLOW_UNDEFINED (type))
15126 if (tree_expr_nonzero_warnv_p (op0,
15127 strict_overflow_p)
15128 && tree_expr_nonzero_warnv_p (op1,
15129 strict_overflow_p))
15131 *strict_overflow_p = true;
15132 return true;
15135 break;
15137 case MIN_EXPR:
15138 sub_strict_overflow_p = false;
15139 if (tree_expr_nonzero_warnv_p (op0,
15140 &sub_strict_overflow_p)
15141 && tree_expr_nonzero_warnv_p (op1,
15142 &sub_strict_overflow_p))
15144 if (sub_strict_overflow_p)
15145 *strict_overflow_p = true;
15147 break;
15149 case MAX_EXPR:
15150 sub_strict_overflow_p = false;
15151 if (tree_expr_nonzero_warnv_p (op0,
15152 &sub_strict_overflow_p))
15154 if (sub_strict_overflow_p)
15155 *strict_overflow_p = true;
15157 /* When both operands are nonzero, then MAX must be too. */
15158 if (tree_expr_nonzero_warnv_p (op1,
15159 strict_overflow_p))
15160 return true;
15162 /* MAX where operand 0 is positive is positive. */
15163 return tree_expr_nonnegative_warnv_p (op0,
15164 strict_overflow_p);
15166 /* MAX where operand 1 is positive is positive. */
15167 else if (tree_expr_nonzero_warnv_p (op1,
15168 &sub_strict_overflow_p)
15169 && tree_expr_nonnegative_warnv_p (op1,
15170 &sub_strict_overflow_p))
15172 if (sub_strict_overflow_p)
15173 *strict_overflow_p = true;
15174 return true;
15176 break;
15178 case BIT_IOR_EXPR:
15179 return (tree_expr_nonzero_warnv_p (op1,
15180 strict_overflow_p)
15181 || tree_expr_nonzero_warnv_p (op0,
15182 strict_overflow_p));
15184 default:
15185 break;
15188 return false;
15191 /* Return true when T is an address and is known to be nonzero.
15192 For floating point we further ensure that T is not denormal.
15193 Similar logic is present in nonzero_address in rtlanal.h.
15195 If the return value is based on the assumption that signed overflow
15196 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15197 change *STRICT_OVERFLOW_P. */
15199 bool
15200 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15202 bool sub_strict_overflow_p;
15203 switch (TREE_CODE (t))
15205 case INTEGER_CST:
15206 return !integer_zerop (t);
15208 case ADDR_EXPR:
15210 tree base = TREE_OPERAND (t, 0);
15212 if (!DECL_P (base))
15213 base = get_base_address (base);
15215 if (!base)
15216 return false;
15218 /* For objects in symbol table check if we know they are non-zero.
15219 Don't do anything for variables and functions before symtab is built;
15220 it is quite possible that they will be declared weak later. */
15221 if (DECL_P (base) && decl_in_symtab_p (base))
15223 struct symtab_node *symbol;
15225 symbol = symtab_node::get_create (base);
15226 if (symbol)
15227 return symbol->nonzero_address ();
15228 else
15229 return false;
15232 /* Function local objects are never NULL. */
15233 if (DECL_P (base)
15234 && (DECL_CONTEXT (base)
15235 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
15236 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
15237 return true;
15239 /* Constants are never weak. */
15240 if (CONSTANT_CLASS_P (base))
15241 return true;
15243 return false;
15246 case COND_EXPR:
15247 sub_strict_overflow_p = false;
15248 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15249 &sub_strict_overflow_p)
15250 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15251 &sub_strict_overflow_p))
15253 if (sub_strict_overflow_p)
15254 *strict_overflow_p = true;
15255 return true;
15257 break;
15259 default:
15260 break;
15262 return false;
15265 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15266 attempt to fold the expression to a constant without modifying TYPE,
15267 OP0 or OP1.
15269 If the expression could be simplified to a constant, then return
15270 the constant. If the expression would not be simplified to a
15271 constant, then return NULL_TREE. */
15273 tree
15274 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15276 tree tem = fold_binary (code, type, op0, op1);
15277 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15280 /* Given the components of a unary expression CODE, TYPE and OP0,
15281 attempt to fold the expression to a constant without modifying
15282 TYPE or OP0.
15284 If the expression could be simplified to a constant, then return
15285 the constant. If the expression would not be simplified to a
15286 constant, then return NULL_TREE. */
15288 tree
15289 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15291 tree tem = fold_unary (code, type, op0);
15292 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15295 /* If EXP represents referencing an element in a constant string
15296 (either via pointer arithmetic or array indexing), return the
15297 tree representing the value accessed, otherwise return NULL. */
15299 tree
15300 fold_read_from_constant_string (tree exp)
15302 if ((TREE_CODE (exp) == INDIRECT_REF
15303 || TREE_CODE (exp) == ARRAY_REF)
15304 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15306 tree exp1 = TREE_OPERAND (exp, 0);
15307 tree index;
15308 tree string;
15309 location_t loc = EXPR_LOCATION (exp);
15311 if (TREE_CODE (exp) == INDIRECT_REF)
15312 string = string_constant (exp1, &index);
15313 else
15315 tree low_bound = array_ref_low_bound (exp);
15316 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15318 /* Optimize the special-case of a zero lower bound.
15320 We convert the low_bound to sizetype to avoid some problems
15321 with constant folding. (E.g. suppose the lower bound is 1,
15322 and its mode is QI. Without the conversion,l (ARRAY
15323 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15324 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15325 if (! integer_zerop (low_bound))
15326 index = size_diffop_loc (loc, index,
15327 fold_convert_loc (loc, sizetype, low_bound));
15329 string = exp1;
15332 if (string
15333 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15334 && TREE_CODE (string) == STRING_CST
15335 && TREE_CODE (index) == INTEGER_CST
15336 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15337 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15338 == MODE_INT)
15339 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15340 return build_int_cst_type (TREE_TYPE (exp),
15341 (TREE_STRING_POINTER (string)
15342 [TREE_INT_CST_LOW (index)]));
15344 return NULL;
15347 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15348 an integer constant, real, or fixed-point constant.
15350 TYPE is the type of the result. */
15352 static tree
15353 fold_negate_const (tree arg0, tree type)
15355 tree t = NULL_TREE;
15357 switch (TREE_CODE (arg0))
15359 case INTEGER_CST:
15361 bool overflow;
15362 wide_int val = wi::neg (arg0, &overflow);
15363 t = force_fit_type (type, val, 1,
15364 (overflow | TREE_OVERFLOW (arg0))
15365 && !TYPE_UNSIGNED (type));
15366 break;
15369 case REAL_CST:
15370 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15371 break;
15373 case FIXED_CST:
15375 FIXED_VALUE_TYPE f;
15376 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15377 &(TREE_FIXED_CST (arg0)), NULL,
15378 TYPE_SATURATING (type));
15379 t = build_fixed (type, f);
15380 /* Propagate overflow flags. */
15381 if (overflow_p | TREE_OVERFLOW (arg0))
15382 TREE_OVERFLOW (t) = 1;
15383 break;
15386 default:
15387 gcc_unreachable ();
15390 return t;
15393 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15394 an integer constant or real constant.
15396 TYPE is the type of the result. */
15398 tree
15399 fold_abs_const (tree arg0, tree type)
15401 tree t = NULL_TREE;
15403 switch (TREE_CODE (arg0))
15405 case INTEGER_CST:
15407 /* If the value is unsigned or non-negative, then the absolute value
15408 is the same as the ordinary value. */
15409 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
15410 t = arg0;
15412 /* If the value is negative, then the absolute value is
15413 its negation. */
15414 else
15416 bool overflow;
15417 wide_int val = wi::neg (arg0, &overflow);
15418 t = force_fit_type (type, val, -1,
15419 overflow | TREE_OVERFLOW (arg0));
15422 break;
15424 case REAL_CST:
15425 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15426 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15427 else
15428 t = arg0;
15429 break;
15431 default:
15432 gcc_unreachable ();
15435 return t;
15438 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15439 constant. TYPE is the type of the result. */
15441 static tree
15442 fold_not_const (const_tree arg0, tree type)
15444 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15446 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
15449 /* Given CODE, a relational operator, the target type, TYPE and two
15450 constant operands OP0 and OP1, return the result of the
15451 relational operation. If the result is not a compile time
15452 constant, then return NULL_TREE. */
15454 static tree
15455 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15457 int result, invert;
15459 /* From here on, the only cases we handle are when the result is
15460 known to be a constant. */
15462 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15464 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15465 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15467 /* Handle the cases where either operand is a NaN. */
15468 if (real_isnan (c0) || real_isnan (c1))
15470 switch (code)
15472 case EQ_EXPR:
15473 case ORDERED_EXPR:
15474 result = 0;
15475 break;
15477 case NE_EXPR:
15478 case UNORDERED_EXPR:
15479 case UNLT_EXPR:
15480 case UNLE_EXPR:
15481 case UNGT_EXPR:
15482 case UNGE_EXPR:
15483 case UNEQ_EXPR:
15484 result = 1;
15485 break;
15487 case LT_EXPR:
15488 case LE_EXPR:
15489 case GT_EXPR:
15490 case GE_EXPR:
15491 case LTGT_EXPR:
15492 if (flag_trapping_math)
15493 return NULL_TREE;
15494 result = 0;
15495 break;
15497 default:
15498 gcc_unreachable ();
15501 return constant_boolean_node (result, type);
15504 return constant_boolean_node (real_compare (code, c0, c1), type);
15507 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15509 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15510 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15511 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15514 /* Handle equality/inequality of complex constants. */
15515 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15517 tree rcond = fold_relational_const (code, type,
15518 TREE_REALPART (op0),
15519 TREE_REALPART (op1));
15520 tree icond = fold_relational_const (code, type,
15521 TREE_IMAGPART (op0),
15522 TREE_IMAGPART (op1));
15523 if (code == EQ_EXPR)
15524 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15525 else if (code == NE_EXPR)
15526 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15527 else
15528 return NULL_TREE;
15531 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15533 unsigned count = VECTOR_CST_NELTS (op0);
15534 tree *elts = XALLOCAVEC (tree, count);
15535 gcc_assert (VECTOR_CST_NELTS (op1) == count
15536 && TYPE_VECTOR_SUBPARTS (type) == count);
15538 for (unsigned i = 0; i < count; i++)
15540 tree elem_type = TREE_TYPE (type);
15541 tree elem0 = VECTOR_CST_ELT (op0, i);
15542 tree elem1 = VECTOR_CST_ELT (op1, i);
15544 tree tem = fold_relational_const (code, elem_type,
15545 elem0, elem1);
15547 if (tem == NULL_TREE)
15548 return NULL_TREE;
15550 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
15553 return build_vector (type, elts);
15556 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15558 To compute GT, swap the arguments and do LT.
15559 To compute GE, do LT and invert the result.
15560 To compute LE, swap the arguments, do LT and invert the result.
15561 To compute NE, do EQ and invert the result.
15563 Therefore, the code below must handle only EQ and LT. */
15565 if (code == LE_EXPR || code == GT_EXPR)
15567 std::swap (op0, op1);
15568 code = swap_tree_comparison (code);
15571 /* Note that it is safe to invert for real values here because we
15572 have already handled the one case that it matters. */
15574 invert = 0;
15575 if (code == NE_EXPR || code == GE_EXPR)
15577 invert = 1;
15578 code = invert_tree_comparison (code, false);
15581 /* Compute a result for LT or EQ if args permit;
15582 Otherwise return T. */
15583 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15585 if (code == EQ_EXPR)
15586 result = tree_int_cst_equal (op0, op1);
15587 else
15588 result = tree_int_cst_lt (op0, op1);
15590 else
15591 return NULL_TREE;
15593 if (invert)
15594 result ^= 1;
15595 return constant_boolean_node (result, type);
15598 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15599 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15600 itself. */
15602 tree
15603 fold_build_cleanup_point_expr (tree type, tree expr)
15605 /* If the expression does not have side effects then we don't have to wrap
15606 it with a cleanup point expression. */
15607 if (!TREE_SIDE_EFFECTS (expr))
15608 return expr;
15610 /* If the expression is a return, check to see if the expression inside the
15611 return has no side effects or the right hand side of the modify expression
15612 inside the return. If either don't have side effects set we don't need to
15613 wrap the expression in a cleanup point expression. Note we don't check the
15614 left hand side of the modify because it should always be a return decl. */
15615 if (TREE_CODE (expr) == RETURN_EXPR)
15617 tree op = TREE_OPERAND (expr, 0);
15618 if (!op || !TREE_SIDE_EFFECTS (op))
15619 return expr;
15620 op = TREE_OPERAND (op, 1);
15621 if (!TREE_SIDE_EFFECTS (op))
15622 return expr;
15625 return build1 (CLEANUP_POINT_EXPR, type, expr);
15628 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15629 of an indirection through OP0, or NULL_TREE if no simplification is
15630 possible. */
15632 tree
15633 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15635 tree sub = op0;
15636 tree subtype;
15638 STRIP_NOPS (sub);
15639 subtype = TREE_TYPE (sub);
15640 if (!POINTER_TYPE_P (subtype))
15641 return NULL_TREE;
15643 if (TREE_CODE (sub) == ADDR_EXPR)
15645 tree op = TREE_OPERAND (sub, 0);
15646 tree optype = TREE_TYPE (op);
15647 /* *&CONST_DECL -> to the value of the const decl. */
15648 if (TREE_CODE (op) == CONST_DECL)
15649 return DECL_INITIAL (op);
15650 /* *&p => p; make sure to handle *&"str"[cst] here. */
15651 if (type == optype)
15653 tree fop = fold_read_from_constant_string (op);
15654 if (fop)
15655 return fop;
15656 else
15657 return op;
15659 /* *(foo *)&fooarray => fooarray[0] */
15660 else if (TREE_CODE (optype) == ARRAY_TYPE
15661 && type == TREE_TYPE (optype)
15662 && (!in_gimple_form
15663 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15665 tree type_domain = TYPE_DOMAIN (optype);
15666 tree min_val = size_zero_node;
15667 if (type_domain && TYPE_MIN_VALUE (type_domain))
15668 min_val = TYPE_MIN_VALUE (type_domain);
15669 if (in_gimple_form
15670 && TREE_CODE (min_val) != INTEGER_CST)
15671 return NULL_TREE;
15672 return build4_loc (loc, ARRAY_REF, type, op, min_val,
15673 NULL_TREE, NULL_TREE);
15675 /* *(foo *)&complexfoo => __real__ complexfoo */
15676 else if (TREE_CODE (optype) == COMPLEX_TYPE
15677 && type == TREE_TYPE (optype))
15678 return fold_build1_loc (loc, REALPART_EXPR, type, op);
15679 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15680 else if (TREE_CODE (optype) == VECTOR_TYPE
15681 && type == TREE_TYPE (optype))
15683 tree part_width = TYPE_SIZE (type);
15684 tree index = bitsize_int (0);
15685 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15689 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15690 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15692 tree op00 = TREE_OPERAND (sub, 0);
15693 tree op01 = TREE_OPERAND (sub, 1);
15695 STRIP_NOPS (op00);
15696 if (TREE_CODE (op00) == ADDR_EXPR)
15698 tree op00type;
15699 op00 = TREE_OPERAND (op00, 0);
15700 op00type = TREE_TYPE (op00);
15702 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15703 if (TREE_CODE (op00type) == VECTOR_TYPE
15704 && type == TREE_TYPE (op00type))
15706 HOST_WIDE_INT offset = tree_to_shwi (op01);
15707 tree part_width = TYPE_SIZE (type);
15708 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
15709 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15710 tree index = bitsize_int (indexi);
15712 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
15713 return fold_build3_loc (loc,
15714 BIT_FIELD_REF, type, op00,
15715 part_width, index);
15718 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15719 else if (TREE_CODE (op00type) == COMPLEX_TYPE
15720 && type == TREE_TYPE (op00type))
15722 tree size = TYPE_SIZE_UNIT (type);
15723 if (tree_int_cst_equal (size, op01))
15724 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15726 /* ((foo *)&fooarray)[1] => fooarray[1] */
15727 else if (TREE_CODE (op00type) == ARRAY_TYPE
15728 && type == TREE_TYPE (op00type))
15730 tree type_domain = TYPE_DOMAIN (op00type);
15731 tree min_val = size_zero_node;
15732 if (type_domain && TYPE_MIN_VALUE (type_domain))
15733 min_val = TYPE_MIN_VALUE (type_domain);
15734 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
15735 TYPE_SIZE_UNIT (type));
15736 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
15737 return build4_loc (loc, ARRAY_REF, type, op00, op01,
15738 NULL_TREE, NULL_TREE);
15743 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15744 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15745 && type == TREE_TYPE (TREE_TYPE (subtype))
15746 && (!in_gimple_form
15747 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15749 tree type_domain;
15750 tree min_val = size_zero_node;
15751 sub = build_fold_indirect_ref_loc (loc, sub);
15752 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15753 if (type_domain && TYPE_MIN_VALUE (type_domain))
15754 min_val = TYPE_MIN_VALUE (type_domain);
15755 if (in_gimple_form
15756 && TREE_CODE (min_val) != INTEGER_CST)
15757 return NULL_TREE;
15758 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
15759 NULL_TREE);
15762 return NULL_TREE;
15765 /* Builds an expression for an indirection through T, simplifying some
15766 cases. */
15768 tree
15769 build_fold_indirect_ref_loc (location_t loc, tree t)
15771 tree type = TREE_TYPE (TREE_TYPE (t));
15772 tree sub = fold_indirect_ref_1 (loc, type, t);
15774 if (sub)
15775 return sub;
15777 return build1_loc (loc, INDIRECT_REF, type, t);
15780 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15782 tree
15783 fold_indirect_ref_loc (location_t loc, tree t)
15785 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15787 if (sub)
15788 return sub;
15789 else
15790 return t;
15793 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15794 whose result is ignored. The type of the returned tree need not be
15795 the same as the original expression. */
15797 tree
15798 fold_ignored_result (tree t)
15800 if (!TREE_SIDE_EFFECTS (t))
15801 return integer_zero_node;
15803 for (;;)
15804 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15806 case tcc_unary:
15807 t = TREE_OPERAND (t, 0);
15808 break;
15810 case tcc_binary:
15811 case tcc_comparison:
15812 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15813 t = TREE_OPERAND (t, 0);
15814 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15815 t = TREE_OPERAND (t, 1);
15816 else
15817 return t;
15818 break;
15820 case tcc_expression:
15821 switch (TREE_CODE (t))
15823 case COMPOUND_EXPR:
15824 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15825 return t;
15826 t = TREE_OPERAND (t, 0);
15827 break;
15829 case COND_EXPR:
15830 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15831 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15832 return t;
15833 t = TREE_OPERAND (t, 0);
15834 break;
15836 default:
15837 return t;
15839 break;
15841 default:
15842 return t;
15846 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
15848 tree
15849 round_up_loc (location_t loc, tree value, unsigned int divisor)
15851 tree div = NULL_TREE;
15853 if (divisor == 1)
15854 return value;
15856 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15857 have to do anything. Only do this when we are not given a const,
15858 because in that case, this check is more expensive than just
15859 doing it. */
15860 if (TREE_CODE (value) != INTEGER_CST)
15862 div = build_int_cst (TREE_TYPE (value), divisor);
15864 if (multiple_of_p (TREE_TYPE (value), value, div))
15865 return value;
15868 /* If divisor is a power of two, simplify this to bit manipulation. */
15869 if (divisor == (divisor & -divisor))
15871 if (TREE_CODE (value) == INTEGER_CST)
15873 wide_int val = value;
15874 bool overflow_p;
15876 if ((val & (divisor - 1)) == 0)
15877 return value;
15879 overflow_p = TREE_OVERFLOW (value);
15880 val += divisor - 1;
15881 val &= - (int) divisor;
15882 if (val == 0)
15883 overflow_p = true;
15885 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
15887 else
15889 tree t;
15891 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15892 value = size_binop_loc (loc, PLUS_EXPR, value, t);
15893 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
15894 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15897 else
15899 if (!div)
15900 div = build_int_cst (TREE_TYPE (value), divisor);
15901 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15902 value = size_binop_loc (loc, MULT_EXPR, value, div);
15905 return value;
15908 /* Likewise, but round down. */
15910 tree
15911 round_down_loc (location_t loc, tree value, int divisor)
15913 tree div = NULL_TREE;
15915 gcc_assert (divisor > 0);
15916 if (divisor == 1)
15917 return value;
15919 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15920 have to do anything. Only do this when we are not given a const,
15921 because in that case, this check is more expensive than just
15922 doing it. */
15923 if (TREE_CODE (value) != INTEGER_CST)
15925 div = build_int_cst (TREE_TYPE (value), divisor);
15927 if (multiple_of_p (TREE_TYPE (value), value, div))
15928 return value;
15931 /* If divisor is a power of two, simplify this to bit manipulation. */
15932 if (divisor == (divisor & -divisor))
15934 tree t;
15936 t = build_int_cst (TREE_TYPE (value), -divisor);
15937 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15939 else
15941 if (!div)
15942 div = build_int_cst (TREE_TYPE (value), divisor);
15943 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
15944 value = size_binop_loc (loc, MULT_EXPR, value, div);
15947 return value;
15950 /* Returns the pointer to the base of the object addressed by EXP and
15951 extracts the information about the offset of the access, storing it
15952 to PBITPOS and POFFSET. */
15954 static tree
15955 split_address_to_core_and_offset (tree exp,
15956 HOST_WIDE_INT *pbitpos, tree *poffset)
15958 tree core;
15959 machine_mode mode;
15960 int unsignedp, volatilep;
15961 HOST_WIDE_INT bitsize;
15962 location_t loc = EXPR_LOCATION (exp);
15964 if (TREE_CODE (exp) == ADDR_EXPR)
15966 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15967 poffset, &mode, &unsignedp, &volatilep,
15968 false);
15969 core = build_fold_addr_expr_loc (loc, core);
15971 else
15973 core = exp;
15974 *pbitpos = 0;
15975 *poffset = NULL_TREE;
15978 return core;
15981 /* Returns true if addresses of E1 and E2 differ by a constant, false
15982 otherwise. If they do, E1 - E2 is stored in *DIFF. */
15984 bool
15985 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15987 tree core1, core2;
15988 HOST_WIDE_INT bitpos1, bitpos2;
15989 tree toffset1, toffset2, tdiff, type;
15991 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15992 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15994 if (bitpos1 % BITS_PER_UNIT != 0
15995 || bitpos2 % BITS_PER_UNIT != 0
15996 || !operand_equal_p (core1, core2, 0))
15997 return false;
15999 if (toffset1 && toffset2)
16001 type = TREE_TYPE (toffset1);
16002 if (type != TREE_TYPE (toffset2))
16003 toffset2 = fold_convert (type, toffset2);
16005 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16006 if (!cst_and_fits_in_hwi (tdiff))
16007 return false;
16009 *diff = int_cst_value (tdiff);
16011 else if (toffset1 || toffset2)
16013 /* If only one of the offsets is non-constant, the difference cannot
16014 be a constant. */
16015 return false;
16017 else
16018 *diff = 0;
16020 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
16021 return true;
16024 /* Simplify the floating point expression EXP when the sign of the
16025 result is not significant. Return NULL_TREE if no simplification
16026 is possible. */
16028 tree
16029 fold_strip_sign_ops (tree exp)
16031 tree arg0, arg1;
16032 location_t loc = EXPR_LOCATION (exp);
16034 switch (TREE_CODE (exp))
16036 case ABS_EXPR:
16037 case NEGATE_EXPR:
16038 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16039 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
16041 case MULT_EXPR:
16042 case RDIV_EXPR:
16043 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (exp)))
16044 return NULL_TREE;
16045 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16046 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16047 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
16048 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
16049 arg0 ? arg0 : TREE_OPERAND (exp, 0),
16050 arg1 ? arg1 : TREE_OPERAND (exp, 1));
16051 break;
16053 case COMPOUND_EXPR:
16054 arg0 = TREE_OPERAND (exp, 0);
16055 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16056 if (arg1)
16057 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16058 break;
16060 case COND_EXPR:
16061 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16062 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16063 if (arg0 || arg1)
16064 return fold_build3_loc (loc,
16065 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16066 arg0 ? arg0 : TREE_OPERAND (exp, 1),
16067 arg1 ? arg1 : TREE_OPERAND (exp, 2));
16068 break;
16070 case CALL_EXPR:
16072 const enum built_in_function fcode = builtin_mathfn_code (exp);
16073 switch (fcode)
16075 CASE_FLT_FN (BUILT_IN_COPYSIGN):
16076 /* Strip copysign function call, return the 1st argument. */
16077 arg0 = CALL_EXPR_ARG (exp, 0);
16078 arg1 = CALL_EXPR_ARG (exp, 1);
16079 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16081 default:
16082 /* Strip sign ops from the argument of "odd" math functions. */
16083 if (negate_mathfn_p (fcode))
16085 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16086 if (arg0)
16087 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16089 break;
16092 break;
16094 default:
16095 break;
16097 return NULL_TREE;
16100 /* Return OFF converted to a pointer offset type suitable as offset for
16101 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16102 tree
16103 convert_to_ptrofftype_loc (location_t loc, tree off)
16105 return fold_convert_loc (loc, sizetype, off);
16108 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16109 tree
16110 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16112 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16113 ptr, convert_to_ptrofftype_loc (loc, off));
16116 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16117 tree
16118 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16120 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16121 ptr, size_int (off));