2015-06-23 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / fold-const.c
blob60aa210496005041d7f8e7bf1920dad9e79b898f
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 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11520 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11521 if the new mask might be further optimized. */
11522 if ((TREE_CODE (arg0) == LSHIFT_EXPR
11523 || TREE_CODE (arg0) == RSHIFT_EXPR)
11524 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11525 && TREE_CODE (arg1) == INTEGER_CST
11526 && tree_fits_uhwi_p (TREE_OPERAND (arg0, 1))
11527 && tree_to_uhwi (TREE_OPERAND (arg0, 1)) > 0
11528 && (tree_to_uhwi (TREE_OPERAND (arg0, 1))
11529 < TYPE_PRECISION (TREE_TYPE (arg0))))
11531 unsigned int shiftc = tree_to_uhwi (TREE_OPERAND (arg0, 1));
11532 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (arg1);
11533 unsigned HOST_WIDE_INT newmask, zerobits = 0;
11534 tree shift_type = TREE_TYPE (arg0);
11536 if (TREE_CODE (arg0) == LSHIFT_EXPR)
11537 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11538 else if (TREE_CODE (arg0) == RSHIFT_EXPR
11539 && TYPE_PRECISION (TREE_TYPE (arg0))
11540 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg0))))
11542 prec = TYPE_PRECISION (TREE_TYPE (arg0));
11543 tree arg00 = TREE_OPERAND (arg0, 0);
11544 /* See if more bits can be proven as zero because of
11545 zero extension. */
11546 if (TREE_CODE (arg00) == NOP_EXPR
11547 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11549 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11550 if (TYPE_PRECISION (inner_type)
11551 == GET_MODE_PRECISION (TYPE_MODE (inner_type))
11552 && TYPE_PRECISION (inner_type) < prec)
11554 prec = TYPE_PRECISION (inner_type);
11555 /* See if we can shorten the right shift. */
11556 if (shiftc < prec)
11557 shift_type = inner_type;
11558 /* Otherwise X >> C1 is all zeros, so we'll optimize
11559 it into (X, 0) later on by making sure zerobits
11560 is all ones. */
11563 zerobits = ~(unsigned HOST_WIDE_INT) 0;
11564 if (shiftc < prec)
11566 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11567 zerobits <<= prec - shiftc;
11569 /* For arithmetic shift if sign bit could be set, zerobits
11570 can contain actually sign bits, so no transformation is
11571 possible, unless MASK masks them all away. In that
11572 case the shift needs to be converted into logical shift. */
11573 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11574 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11576 if ((mask & zerobits) == 0)
11577 shift_type = unsigned_type_for (TREE_TYPE (arg0));
11578 else
11579 zerobits = 0;
11583 /* ((X << 16) & 0xff00) is (X, 0). */
11584 if ((mask & zerobits) == mask)
11585 return omit_one_operand_loc (loc, type,
11586 build_int_cst (type, 0), arg0);
11588 newmask = mask | zerobits;
11589 if (newmask != mask && (newmask & (newmask + 1)) == 0)
11591 /* Only do the transformation if NEWMASK is some integer
11592 mode's mask. */
11593 for (prec = BITS_PER_UNIT;
11594 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11595 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11596 break;
11597 if (prec < HOST_BITS_PER_WIDE_INT
11598 || newmask == ~(unsigned HOST_WIDE_INT) 0)
11600 tree newmaskt;
11602 if (shift_type != TREE_TYPE (arg0))
11604 tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11605 fold_convert_loc (loc, shift_type,
11606 TREE_OPERAND (arg0, 0)),
11607 TREE_OPERAND (arg0, 1));
11608 tem = fold_convert_loc (loc, type, tem);
11610 else
11611 tem = op0;
11612 newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11613 if (!tree_int_cst_equal (newmaskt, arg1))
11614 return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11619 goto associate;
11621 case RDIV_EXPR:
11622 /* Don't touch a floating-point divide by zero unless the mode
11623 of the constant can represent infinity. */
11624 if (TREE_CODE (arg1) == REAL_CST
11625 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11626 && real_zerop (arg1))
11627 return NULL_TREE;
11629 /* (-A) / (-B) -> A / B */
11630 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11631 return fold_build2_loc (loc, RDIV_EXPR, type,
11632 TREE_OPERAND (arg0, 0),
11633 negate_expr (arg1));
11634 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11635 return fold_build2_loc (loc, RDIV_EXPR, type,
11636 negate_expr (arg0),
11637 TREE_OPERAND (arg1, 0));
11639 /* Convert A/B/C to A/(B*C). */
11640 if (flag_reciprocal_math
11641 && TREE_CODE (arg0) == RDIV_EXPR)
11642 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11643 fold_build2_loc (loc, MULT_EXPR, type,
11644 TREE_OPERAND (arg0, 1), arg1));
11646 /* Convert A/(B/C) to (A/B)*C. */
11647 if (flag_reciprocal_math
11648 && TREE_CODE (arg1) == RDIV_EXPR)
11649 return fold_build2_loc (loc, MULT_EXPR, type,
11650 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11651 TREE_OPERAND (arg1, 0)),
11652 TREE_OPERAND (arg1, 1));
11654 /* Convert C1/(X*C2) into (C1/C2)/X. */
11655 if (flag_reciprocal_math
11656 && TREE_CODE (arg1) == MULT_EXPR
11657 && TREE_CODE (arg0) == REAL_CST
11658 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11660 tree tem = const_binop (RDIV_EXPR, arg0,
11661 TREE_OPERAND (arg1, 1));
11662 if (tem)
11663 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11664 TREE_OPERAND (arg1, 0));
11667 if (flag_unsafe_math_optimizations)
11669 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11670 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11672 /* Optimize sin(x)/cos(x) as tan(x). */
11673 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11674 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11675 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11676 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11677 CALL_EXPR_ARG (arg1, 0), 0))
11679 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11681 if (tanfn != NULL_TREE)
11682 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11685 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11686 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11687 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11688 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11689 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11690 CALL_EXPR_ARG (arg1, 0), 0))
11692 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11694 if (tanfn != NULL_TREE)
11696 tree tmp = build_call_expr_loc (loc, tanfn, 1,
11697 CALL_EXPR_ARG (arg0, 0));
11698 return fold_build2_loc (loc, RDIV_EXPR, type,
11699 build_real (type, dconst1), tmp);
11703 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11704 NaNs or Infinities. */
11705 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11706 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11707 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11709 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11710 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11712 if (! HONOR_NANS (arg00)
11713 && ! HONOR_INFINITIES (element_mode (arg00))
11714 && operand_equal_p (arg00, arg01, 0))
11716 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11718 if (cosfn != NULL_TREE)
11719 return build_call_expr_loc (loc, cosfn, 1, arg00);
11723 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11724 NaNs or Infinities. */
11725 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11726 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11727 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11729 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11730 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11732 if (! HONOR_NANS (arg00)
11733 && ! HONOR_INFINITIES (element_mode (arg00))
11734 && operand_equal_p (arg00, arg01, 0))
11736 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11738 if (cosfn != NULL_TREE)
11740 tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11741 return fold_build2_loc (loc, RDIV_EXPR, type,
11742 build_real (type, dconst1),
11743 tmp);
11748 /* Optimize pow(x,c)/x as pow(x,c-1). */
11749 if (fcode0 == BUILT_IN_POW
11750 || fcode0 == BUILT_IN_POWF
11751 || fcode0 == BUILT_IN_POWL)
11753 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11754 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11755 if (TREE_CODE (arg01) == REAL_CST
11756 && !TREE_OVERFLOW (arg01)
11757 && operand_equal_p (arg1, arg00, 0))
11759 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11760 REAL_VALUE_TYPE c;
11761 tree arg;
11763 c = TREE_REAL_CST (arg01);
11764 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11765 arg = build_real (type, c);
11766 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11770 /* Optimize a/root(b/c) into a*root(c/b). */
11771 if (BUILTIN_ROOT_P (fcode1))
11773 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11775 if (TREE_CODE (rootarg) == RDIV_EXPR)
11777 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11778 tree b = TREE_OPERAND (rootarg, 0);
11779 tree c = TREE_OPERAND (rootarg, 1);
11781 tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11783 tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11784 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11788 /* Optimize x/expN(y) into x*expN(-y). */
11789 if (BUILTIN_EXPONENT_P (fcode1))
11791 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11792 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11793 arg1 = build_call_expr_loc (loc,
11794 expfn, 1,
11795 fold_convert_loc (loc, type, arg));
11796 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11799 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11800 if (fcode1 == BUILT_IN_POW
11801 || fcode1 == BUILT_IN_POWF
11802 || fcode1 == BUILT_IN_POWL)
11804 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11805 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11806 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11807 tree neg11 = fold_convert_loc (loc, type,
11808 negate_expr (arg11));
11809 arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11810 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11813 return NULL_TREE;
11815 case TRUNC_DIV_EXPR:
11816 /* Optimize (X & (-A)) / A where A is a power of 2,
11817 to X >> log2(A) */
11818 if (TREE_CODE (arg0) == BIT_AND_EXPR
11819 && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
11820 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
11822 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
11823 arg1, TREE_OPERAND (arg0, 1));
11824 if (sum && integer_zerop (sum)) {
11825 tree pow2 = build_int_cst (integer_type_node,
11826 wi::exact_log2 (arg1));
11827 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11828 TREE_OPERAND (arg0, 0), pow2);
11832 /* Fall through */
11834 case FLOOR_DIV_EXPR:
11835 /* Simplify A / (B << N) where A and B are positive and B is
11836 a power of 2, to A >> (N + log2(B)). */
11837 strict_overflow_p = false;
11838 if (TREE_CODE (arg1) == LSHIFT_EXPR
11839 && (TYPE_UNSIGNED (type)
11840 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11842 tree sval = TREE_OPERAND (arg1, 0);
11843 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11845 tree sh_cnt = TREE_OPERAND (arg1, 1);
11846 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
11847 wi::exact_log2 (sval));
11849 if (strict_overflow_p)
11850 fold_overflow_warning (("assuming signed overflow does not "
11851 "occur when simplifying A / (B << N)"),
11852 WARN_STRICT_OVERFLOW_MISC);
11854 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11855 sh_cnt, pow2);
11856 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11857 fold_convert_loc (loc, type, arg0), sh_cnt);
11861 /* Fall through */
11863 case ROUND_DIV_EXPR:
11864 case CEIL_DIV_EXPR:
11865 case EXACT_DIV_EXPR:
11866 if (integer_zerop (arg1))
11867 return NULL_TREE;
11869 /* Convert -A / -B to A / B when the type is signed and overflow is
11870 undefined. */
11871 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11872 && TREE_CODE (arg0) == NEGATE_EXPR
11873 && negate_expr_p (arg1))
11875 if (INTEGRAL_TYPE_P (type))
11876 fold_overflow_warning (("assuming signed overflow does not occur "
11877 "when distributing negation across "
11878 "division"),
11879 WARN_STRICT_OVERFLOW_MISC);
11880 return fold_build2_loc (loc, code, type,
11881 fold_convert_loc (loc, type,
11882 TREE_OPERAND (arg0, 0)),
11883 fold_convert_loc (loc, type,
11884 negate_expr (arg1)));
11886 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11887 && TREE_CODE (arg1) == NEGATE_EXPR
11888 && negate_expr_p (arg0))
11890 if (INTEGRAL_TYPE_P (type))
11891 fold_overflow_warning (("assuming signed overflow does not occur "
11892 "when distributing negation across "
11893 "division"),
11894 WARN_STRICT_OVERFLOW_MISC);
11895 return fold_build2_loc (loc, code, type,
11896 fold_convert_loc (loc, type,
11897 negate_expr (arg0)),
11898 fold_convert_loc (loc, type,
11899 TREE_OPERAND (arg1, 0)));
11902 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11903 operation, EXACT_DIV_EXPR.
11905 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11906 At one time others generated faster code, it's not clear if they do
11907 after the last round to changes to the DIV code in expmed.c. */
11908 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11909 && multiple_of_p (type, arg0, arg1))
11910 return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11912 strict_overflow_p = false;
11913 if (TREE_CODE (arg1) == INTEGER_CST
11914 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11915 &strict_overflow_p)))
11917 if (strict_overflow_p)
11918 fold_overflow_warning (("assuming signed overflow does not occur "
11919 "when simplifying division"),
11920 WARN_STRICT_OVERFLOW_MISC);
11921 return fold_convert_loc (loc, type, tem);
11924 return NULL_TREE;
11926 case CEIL_MOD_EXPR:
11927 case FLOOR_MOD_EXPR:
11928 case ROUND_MOD_EXPR:
11929 case TRUNC_MOD_EXPR:
11930 strict_overflow_p = false;
11931 if (TREE_CODE (arg1) == INTEGER_CST
11932 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11933 &strict_overflow_p)))
11935 if (strict_overflow_p)
11936 fold_overflow_warning (("assuming signed overflow does not occur "
11937 "when simplifying modulus"),
11938 WARN_STRICT_OVERFLOW_MISC);
11939 return fold_convert_loc (loc, type, tem);
11942 return NULL_TREE;
11944 case LROTATE_EXPR:
11945 case RROTATE_EXPR:
11946 case RSHIFT_EXPR:
11947 case LSHIFT_EXPR:
11948 /* Since negative shift count is not well-defined,
11949 don't try to compute it in the compiler. */
11950 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11951 return NULL_TREE;
11953 prec = element_precision (type);
11955 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11956 if (TREE_CODE (op0) == code && tree_fits_uhwi_p (arg1)
11957 && tree_to_uhwi (arg1) < prec
11958 && tree_fits_uhwi_p (TREE_OPERAND (arg0, 1))
11959 && tree_to_uhwi (TREE_OPERAND (arg0, 1)) < prec)
11961 unsigned int low = (tree_to_uhwi (TREE_OPERAND (arg0, 1))
11962 + tree_to_uhwi (arg1));
11964 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11965 being well defined. */
11966 if (low >= prec)
11968 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11969 low = low % prec;
11970 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11971 return omit_one_operand_loc (loc, type, build_zero_cst (type),
11972 TREE_OPERAND (arg0, 0));
11973 else
11974 low = prec - 1;
11977 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11978 build_int_cst (TREE_TYPE (arg1), low));
11981 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11982 into x & ((unsigned)-1 >> c) for unsigned types. */
11983 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11984 || (TYPE_UNSIGNED (type)
11985 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11986 && tree_fits_uhwi_p (arg1)
11987 && tree_to_uhwi (arg1) < prec
11988 && tree_fits_uhwi_p (TREE_OPERAND (arg0, 1))
11989 && tree_to_uhwi (TREE_OPERAND (arg0, 1)) < prec)
11991 HOST_WIDE_INT low0 = tree_to_uhwi (TREE_OPERAND (arg0, 1));
11992 HOST_WIDE_INT low1 = tree_to_uhwi (arg1);
11993 tree lshift;
11994 tree arg00;
11996 if (low0 == low1)
11998 arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12000 lshift = build_minus_one_cst (type);
12001 lshift = const_binop (code, lshift, arg1);
12003 return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
12007 /* If we have a rotate of a bit operation with the rotate count and
12008 the second operand of the bit operation both constant,
12009 permute the two operations. */
12010 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12011 && (TREE_CODE (arg0) == BIT_AND_EXPR
12012 || TREE_CODE (arg0) == BIT_IOR_EXPR
12013 || TREE_CODE (arg0) == BIT_XOR_EXPR)
12014 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12015 return fold_build2_loc (loc, TREE_CODE (arg0), type,
12016 fold_build2_loc (loc, code, type,
12017 TREE_OPERAND (arg0, 0), arg1),
12018 fold_build2_loc (loc, code, type,
12019 TREE_OPERAND (arg0, 1), arg1));
12021 /* Two consecutive rotates adding up to the some integer
12022 multiple of the precision of the type can be ignored. */
12023 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12024 && TREE_CODE (arg0) == RROTATE_EXPR
12025 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12026 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
12027 prec) == 0)
12028 return TREE_OPERAND (arg0, 0);
12030 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
12031 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
12032 if the latter can be further optimized. */
12033 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
12034 && TREE_CODE (arg0) == BIT_AND_EXPR
12035 && TREE_CODE (arg1) == INTEGER_CST
12036 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12038 tree mask = fold_build2_loc (loc, code, type,
12039 fold_convert_loc (loc, type,
12040 TREE_OPERAND (arg0, 1)),
12041 arg1);
12042 tree shift = fold_build2_loc (loc, code, type,
12043 fold_convert_loc (loc, type,
12044 TREE_OPERAND (arg0, 0)),
12045 arg1);
12046 tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
12047 if (tem)
12048 return tem;
12051 return NULL_TREE;
12053 case MIN_EXPR:
12054 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
12055 if (tem)
12056 return tem;
12057 goto associate;
12059 case MAX_EXPR:
12060 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
12061 if (tem)
12062 return tem;
12063 goto associate;
12065 case TRUTH_ANDIF_EXPR:
12066 /* Note that the operands of this must be ints
12067 and their values must be 0 or 1.
12068 ("true" is a fixed value perhaps depending on the language.) */
12069 /* If first arg is constant zero, return it. */
12070 if (integer_zerop (arg0))
12071 return fold_convert_loc (loc, type, arg0);
12072 case TRUTH_AND_EXPR:
12073 /* If either arg is constant true, drop it. */
12074 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12075 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12076 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12077 /* Preserve sequence points. */
12078 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12079 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12080 /* If second arg is constant zero, result is zero, but first arg
12081 must be evaluated. */
12082 if (integer_zerop (arg1))
12083 return omit_one_operand_loc (loc, type, arg1, arg0);
12084 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12085 case will be handled here. */
12086 if (integer_zerop (arg0))
12087 return omit_one_operand_loc (loc, type, arg0, arg1);
12089 /* !X && X is always false. */
12090 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12091 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12092 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12093 /* X && !X is always false. */
12094 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12095 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12096 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12098 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12099 means A >= Y && A != MAX, but in this case we know that
12100 A < X <= MAX. */
12102 if (!TREE_SIDE_EFFECTS (arg0)
12103 && !TREE_SIDE_EFFECTS (arg1))
12105 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12106 if (tem && !operand_equal_p (tem, arg0, 0))
12107 return fold_build2_loc (loc, code, type, tem, arg1);
12109 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12110 if (tem && !operand_equal_p (tem, arg1, 0))
12111 return fold_build2_loc (loc, code, type, arg0, tem);
12114 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12115 != NULL_TREE)
12116 return tem;
12118 return NULL_TREE;
12120 case TRUTH_ORIF_EXPR:
12121 /* Note that the operands of this must be ints
12122 and their values must be 0 or true.
12123 ("true" is a fixed value perhaps depending on the language.) */
12124 /* If first arg is constant true, return it. */
12125 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12126 return fold_convert_loc (loc, type, arg0);
12127 case TRUTH_OR_EXPR:
12128 /* If either arg is constant zero, drop it. */
12129 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12130 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12131 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12132 /* Preserve sequence points. */
12133 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12134 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12135 /* If second arg is constant true, result is true, but we must
12136 evaluate first arg. */
12137 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12138 return omit_one_operand_loc (loc, type, arg1, arg0);
12139 /* Likewise for first arg, but note this only occurs here for
12140 TRUTH_OR_EXPR. */
12141 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12142 return omit_one_operand_loc (loc, type, arg0, arg1);
12144 /* !X || X is always true. */
12145 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12146 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12147 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12148 /* X || !X is always true. */
12149 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12150 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12151 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12153 /* (X && !Y) || (!X && Y) is X ^ Y */
12154 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12155 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12157 tree a0, a1, l0, l1, n0, n1;
12159 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12160 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12162 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12163 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12165 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12166 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12168 if ((operand_equal_p (n0, a0, 0)
12169 && operand_equal_p (n1, a1, 0))
12170 || (operand_equal_p (n0, a1, 0)
12171 && operand_equal_p (n1, a0, 0)))
12172 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12175 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12176 != NULL_TREE)
12177 return tem;
12179 return NULL_TREE;
12181 case TRUTH_XOR_EXPR:
12182 /* If the second arg is constant zero, drop it. */
12183 if (integer_zerop (arg1))
12184 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12185 /* If the second arg is constant true, this is a logical inversion. */
12186 if (integer_onep (arg1))
12188 tem = invert_truthvalue_loc (loc, arg0);
12189 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12191 /* Identical arguments cancel to zero. */
12192 if (operand_equal_p (arg0, arg1, 0))
12193 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12195 /* !X ^ X is always true. */
12196 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12197 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12198 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12200 /* X ^ !X is always true. */
12201 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12202 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12203 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12205 return NULL_TREE;
12207 case EQ_EXPR:
12208 case NE_EXPR:
12209 STRIP_NOPS (arg0);
12210 STRIP_NOPS (arg1);
12212 tem = fold_comparison (loc, code, type, op0, op1);
12213 if (tem != NULL_TREE)
12214 return tem;
12216 /* bool_var != 0 becomes bool_var. */
12217 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12218 && code == NE_EXPR)
12219 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12221 /* bool_var == 1 becomes bool_var. */
12222 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12223 && code == EQ_EXPR)
12224 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12226 /* bool_var != 1 becomes !bool_var. */
12227 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12228 && code == NE_EXPR)
12229 return fold_convert_loc (loc, type,
12230 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12231 TREE_TYPE (arg0), arg0));
12233 /* bool_var == 0 becomes !bool_var. */
12234 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12235 && code == EQ_EXPR)
12236 return fold_convert_loc (loc, type,
12237 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12238 TREE_TYPE (arg0), arg0));
12240 /* !exp != 0 becomes !exp */
12241 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12242 && code == NE_EXPR)
12243 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12245 /* If this is an equality comparison of the address of two non-weak,
12246 unaliased symbols neither of which are extern (since we do not
12247 have access to attributes for externs), then we know the result. */
12248 if (TREE_CODE (arg0) == ADDR_EXPR
12249 && DECL_P (TREE_OPERAND (arg0, 0))
12250 && TREE_CODE (arg1) == ADDR_EXPR
12251 && DECL_P (TREE_OPERAND (arg1, 0)))
12253 int equal;
12255 if (decl_in_symtab_p (TREE_OPERAND (arg0, 0))
12256 && decl_in_symtab_p (TREE_OPERAND (arg1, 0)))
12257 equal = symtab_node::get_create (TREE_OPERAND (arg0, 0))
12258 ->equal_address_to (symtab_node::get_create
12259 (TREE_OPERAND (arg1, 0)));
12260 else
12261 equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12262 if (equal != 2)
12263 return constant_boolean_node (equal
12264 ? code == EQ_EXPR : code != EQ_EXPR,
12265 type);
12268 /* Similarly for a NEGATE_EXPR. */
12269 if (TREE_CODE (arg0) == NEGATE_EXPR
12270 && TREE_CODE (arg1) == INTEGER_CST
12271 && 0 != (tem = negate_expr (fold_convert_loc (loc, TREE_TYPE (arg0),
12272 arg1)))
12273 && TREE_CODE (tem) == INTEGER_CST
12274 && !TREE_OVERFLOW (tem))
12275 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12277 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12278 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12279 && TREE_CODE (arg1) == INTEGER_CST
12280 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12281 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12282 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12283 fold_convert_loc (loc,
12284 TREE_TYPE (arg0),
12285 arg1),
12286 TREE_OPERAND (arg0, 1)));
12288 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
12289 if ((TREE_CODE (arg0) == PLUS_EXPR
12290 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12291 || TREE_CODE (arg0) == MINUS_EXPR)
12292 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12293 0)),
12294 arg1, 0)
12295 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12296 || POINTER_TYPE_P (TREE_TYPE (arg0))))
12298 tree val = TREE_OPERAND (arg0, 1);
12299 return omit_two_operands_loc (loc, type,
12300 fold_build2_loc (loc, code, type,
12301 val,
12302 build_int_cst (TREE_TYPE (val),
12303 0)),
12304 TREE_OPERAND (arg0, 0), arg1);
12307 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
12308 if (TREE_CODE (arg0) == MINUS_EXPR
12309 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12310 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12311 1)),
12312 arg1, 0)
12313 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
12315 return omit_two_operands_loc (loc, type,
12316 code == NE_EXPR
12317 ? boolean_true_node : boolean_false_node,
12318 TREE_OPERAND (arg0, 1), arg1);
12321 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12322 if (TREE_CODE (arg0) == ABS_EXPR
12323 && (integer_zerop (arg1) || real_zerop (arg1)))
12324 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12326 /* If this is an EQ or NE comparison with zero and ARG0 is
12327 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12328 two operations, but the latter can be done in one less insn
12329 on machines that have only two-operand insns or on which a
12330 constant cannot be the first operand. */
12331 if (TREE_CODE (arg0) == BIT_AND_EXPR
12332 && integer_zerop (arg1))
12334 tree arg00 = TREE_OPERAND (arg0, 0);
12335 tree arg01 = TREE_OPERAND (arg0, 1);
12336 if (TREE_CODE (arg00) == LSHIFT_EXPR
12337 && integer_onep (TREE_OPERAND (arg00, 0)))
12339 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12340 arg01, TREE_OPERAND (arg00, 1));
12341 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12342 build_int_cst (TREE_TYPE (arg0), 1));
12343 return fold_build2_loc (loc, code, type,
12344 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12345 arg1);
12347 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12348 && integer_onep (TREE_OPERAND (arg01, 0)))
12350 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12351 arg00, TREE_OPERAND (arg01, 1));
12352 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12353 build_int_cst (TREE_TYPE (arg0), 1));
12354 return fold_build2_loc (loc, code, type,
12355 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12356 arg1);
12360 /* If this is an NE or EQ comparison of zero against the result of a
12361 signed MOD operation whose second operand is a power of 2, make
12362 the MOD operation unsigned since it is simpler and equivalent. */
12363 if (integer_zerop (arg1)
12364 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12365 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12366 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12367 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12368 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12369 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12371 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12372 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12373 fold_convert_loc (loc, newtype,
12374 TREE_OPERAND (arg0, 0)),
12375 fold_convert_loc (loc, newtype,
12376 TREE_OPERAND (arg0, 1)));
12378 return fold_build2_loc (loc, code, type, newmod,
12379 fold_convert_loc (loc, newtype, arg1));
12382 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12383 C1 is a valid shift constant, and C2 is a power of two, i.e.
12384 a single bit. */
12385 if (TREE_CODE (arg0) == BIT_AND_EXPR
12386 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12387 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12388 == INTEGER_CST
12389 && integer_pow2p (TREE_OPERAND (arg0, 1))
12390 && integer_zerop (arg1))
12392 tree itype = TREE_TYPE (arg0);
12393 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12394 prec = TYPE_PRECISION (itype);
12396 /* Check for a valid shift count. */
12397 if (wi::ltu_p (arg001, prec))
12399 tree arg01 = TREE_OPERAND (arg0, 1);
12400 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12401 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12402 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12403 can be rewritten as (X & (C2 << C1)) != 0. */
12404 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12406 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12407 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12408 return fold_build2_loc (loc, code, type, tem,
12409 fold_convert_loc (loc, itype, arg1));
12411 /* Otherwise, for signed (arithmetic) shifts,
12412 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12413 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12414 else if (!TYPE_UNSIGNED (itype))
12415 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12416 arg000, build_int_cst (itype, 0));
12417 /* Otherwise, of unsigned (logical) shifts,
12418 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12419 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12420 else
12421 return omit_one_operand_loc (loc, type,
12422 code == EQ_EXPR ? integer_one_node
12423 : integer_zero_node,
12424 arg000);
12428 /* If we have (A & C) == C where C is a power of 2, convert this into
12429 (A & C) != 0. Similarly for NE_EXPR. */
12430 if (TREE_CODE (arg0) == BIT_AND_EXPR
12431 && integer_pow2p (TREE_OPERAND (arg0, 1))
12432 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12433 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12434 arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12435 integer_zero_node));
12437 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12438 bit, then fold the expression into A < 0 or A >= 0. */
12439 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12440 if (tem)
12441 return tem;
12443 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12444 Similarly for NE_EXPR. */
12445 if (TREE_CODE (arg0) == BIT_AND_EXPR
12446 && TREE_CODE (arg1) == INTEGER_CST
12447 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12449 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12450 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12451 TREE_OPERAND (arg0, 1));
12452 tree dandnotc
12453 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12454 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
12455 notc);
12456 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12457 if (integer_nonzerop (dandnotc))
12458 return omit_one_operand_loc (loc, type, rslt, arg0);
12461 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12462 Similarly for NE_EXPR. */
12463 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12464 && TREE_CODE (arg1) == INTEGER_CST
12465 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12467 tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12468 tree candnotd
12469 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12470 TREE_OPERAND (arg0, 1),
12471 fold_convert_loc (loc, TREE_TYPE (arg0), notd));
12472 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12473 if (integer_nonzerop (candnotd))
12474 return omit_one_operand_loc (loc, type, rslt, arg0);
12477 /* If this is a comparison of a field, we may be able to simplify it. */
12478 if ((TREE_CODE (arg0) == COMPONENT_REF
12479 || TREE_CODE (arg0) == BIT_FIELD_REF)
12480 /* Handle the constant case even without -O
12481 to make sure the warnings are given. */
12482 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12484 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12485 if (t1)
12486 return t1;
12489 /* Optimize comparisons of strlen vs zero to a compare of the
12490 first character of the string vs zero. To wit,
12491 strlen(ptr) == 0 => *ptr == 0
12492 strlen(ptr) != 0 => *ptr != 0
12493 Other cases should reduce to one of these two (or a constant)
12494 due to the return value of strlen being unsigned. */
12495 if (TREE_CODE (arg0) == CALL_EXPR
12496 && integer_zerop (arg1))
12498 tree fndecl = get_callee_fndecl (arg0);
12500 if (fndecl
12501 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12502 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12503 && call_expr_nargs (arg0) == 1
12504 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12506 tree iref = build_fold_indirect_ref_loc (loc,
12507 CALL_EXPR_ARG (arg0, 0));
12508 return fold_build2_loc (loc, code, type, iref,
12509 build_int_cst (TREE_TYPE (iref), 0));
12513 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12514 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12515 if (TREE_CODE (arg0) == RSHIFT_EXPR
12516 && integer_zerop (arg1)
12517 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12519 tree arg00 = TREE_OPERAND (arg0, 0);
12520 tree arg01 = TREE_OPERAND (arg0, 1);
12521 tree itype = TREE_TYPE (arg00);
12522 if (wi::eq_p (arg01, element_precision (itype) - 1))
12524 if (TYPE_UNSIGNED (itype))
12526 itype = signed_type_for (itype);
12527 arg00 = fold_convert_loc (loc, itype, arg00);
12529 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12530 type, arg00, build_zero_cst (itype));
12534 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
12535 if (integer_zerop (arg1)
12536 && TREE_CODE (arg0) == BIT_XOR_EXPR)
12537 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12538 TREE_OPERAND (arg0, 1));
12540 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
12541 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12542 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12543 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12544 build_zero_cst (TREE_TYPE (arg0)));
12545 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
12546 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12547 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12548 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12549 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12550 build_zero_cst (TREE_TYPE (arg0)));
12552 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
12553 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12554 && TREE_CODE (arg1) == INTEGER_CST
12555 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12556 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12557 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12558 TREE_OPERAND (arg0, 1), arg1));
12560 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12561 (X & C) == 0 when C is a single bit. */
12562 if (TREE_CODE (arg0) == BIT_AND_EXPR
12563 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12564 && integer_zerop (arg1)
12565 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12567 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12568 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12569 TREE_OPERAND (arg0, 1));
12570 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12571 type, tem,
12572 fold_convert_loc (loc, TREE_TYPE (arg0),
12573 arg1));
12576 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12577 constant C is a power of two, i.e. a single bit. */
12578 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12579 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12580 && integer_zerop (arg1)
12581 && integer_pow2p (TREE_OPERAND (arg0, 1))
12582 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12583 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12585 tree arg00 = TREE_OPERAND (arg0, 0);
12586 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12587 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12590 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12591 when is C is a power of two, i.e. a single bit. */
12592 if (TREE_CODE (arg0) == BIT_AND_EXPR
12593 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12594 && integer_zerop (arg1)
12595 && integer_pow2p (TREE_OPERAND (arg0, 1))
12596 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12597 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12599 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12600 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12601 arg000, TREE_OPERAND (arg0, 1));
12602 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12603 tem, build_int_cst (TREE_TYPE (tem), 0));
12606 if (integer_zerop (arg1)
12607 && tree_expr_nonzero_p (arg0))
12609 tree res = constant_boolean_node (code==NE_EXPR, type);
12610 return omit_one_operand_loc (loc, type, res, arg0);
12613 /* Fold -X op -Y as X op Y, where op is eq/ne. */
12614 if (TREE_CODE (arg0) == NEGATE_EXPR
12615 && TREE_CODE (arg1) == NEGATE_EXPR)
12616 return fold_build2_loc (loc, code, type,
12617 TREE_OPERAND (arg0, 0),
12618 fold_convert_loc (loc, TREE_TYPE (arg0),
12619 TREE_OPERAND (arg1, 0)));
12621 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12622 if (TREE_CODE (arg0) == BIT_AND_EXPR
12623 && TREE_CODE (arg1) == BIT_AND_EXPR)
12625 tree arg00 = TREE_OPERAND (arg0, 0);
12626 tree arg01 = TREE_OPERAND (arg0, 1);
12627 tree arg10 = TREE_OPERAND (arg1, 0);
12628 tree arg11 = TREE_OPERAND (arg1, 1);
12629 tree itype = TREE_TYPE (arg0);
12631 if (operand_equal_p (arg01, arg11, 0))
12632 return fold_build2_loc (loc, code, type,
12633 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12634 fold_build2_loc (loc,
12635 BIT_XOR_EXPR, itype,
12636 arg00, arg10),
12637 arg01),
12638 build_zero_cst (itype));
12640 if (operand_equal_p (arg01, arg10, 0))
12641 return fold_build2_loc (loc, code, type,
12642 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12643 fold_build2_loc (loc,
12644 BIT_XOR_EXPR, itype,
12645 arg00, arg11),
12646 arg01),
12647 build_zero_cst (itype));
12649 if (operand_equal_p (arg00, arg11, 0))
12650 return fold_build2_loc (loc, code, type,
12651 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12652 fold_build2_loc (loc,
12653 BIT_XOR_EXPR, itype,
12654 arg01, arg10),
12655 arg00),
12656 build_zero_cst (itype));
12658 if (operand_equal_p (arg00, arg10, 0))
12659 return fold_build2_loc (loc, code, type,
12660 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12661 fold_build2_loc (loc,
12662 BIT_XOR_EXPR, itype,
12663 arg01, arg11),
12664 arg00),
12665 build_zero_cst (itype));
12668 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12669 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12671 tree arg00 = TREE_OPERAND (arg0, 0);
12672 tree arg01 = TREE_OPERAND (arg0, 1);
12673 tree arg10 = TREE_OPERAND (arg1, 0);
12674 tree arg11 = TREE_OPERAND (arg1, 1);
12675 tree itype = TREE_TYPE (arg0);
12677 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12678 operand_equal_p guarantees no side-effects so we don't need
12679 to use omit_one_operand on Z. */
12680 if (operand_equal_p (arg01, arg11, 0))
12681 return fold_build2_loc (loc, code, type, arg00,
12682 fold_convert_loc (loc, TREE_TYPE (arg00),
12683 arg10));
12684 if (operand_equal_p (arg01, arg10, 0))
12685 return fold_build2_loc (loc, code, type, arg00,
12686 fold_convert_loc (loc, TREE_TYPE (arg00),
12687 arg11));
12688 if (operand_equal_p (arg00, arg11, 0))
12689 return fold_build2_loc (loc, code, type, arg01,
12690 fold_convert_loc (loc, TREE_TYPE (arg01),
12691 arg10));
12692 if (operand_equal_p (arg00, arg10, 0))
12693 return fold_build2_loc (loc, code, type, arg01,
12694 fold_convert_loc (loc, TREE_TYPE (arg01),
12695 arg11));
12697 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12698 if (TREE_CODE (arg01) == INTEGER_CST
12699 && TREE_CODE (arg11) == INTEGER_CST)
12701 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12702 fold_convert_loc (loc, itype, arg11));
12703 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12704 return fold_build2_loc (loc, code, type, tem,
12705 fold_convert_loc (loc, itype, arg10));
12709 /* Attempt to simplify equality/inequality comparisons of complex
12710 values. Only lower the comparison if the result is known or
12711 can be simplified to a single scalar comparison. */
12712 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12713 || TREE_CODE (arg0) == COMPLEX_CST)
12714 && (TREE_CODE (arg1) == COMPLEX_EXPR
12715 || TREE_CODE (arg1) == COMPLEX_CST))
12717 tree real0, imag0, real1, imag1;
12718 tree rcond, icond;
12720 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12722 real0 = TREE_OPERAND (arg0, 0);
12723 imag0 = TREE_OPERAND (arg0, 1);
12725 else
12727 real0 = TREE_REALPART (arg0);
12728 imag0 = TREE_IMAGPART (arg0);
12731 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12733 real1 = TREE_OPERAND (arg1, 0);
12734 imag1 = TREE_OPERAND (arg1, 1);
12736 else
12738 real1 = TREE_REALPART (arg1);
12739 imag1 = TREE_IMAGPART (arg1);
12742 rcond = fold_binary_loc (loc, code, type, real0, real1);
12743 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12745 if (integer_zerop (rcond))
12747 if (code == EQ_EXPR)
12748 return omit_two_operands_loc (loc, type, boolean_false_node,
12749 imag0, imag1);
12750 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12752 else
12754 if (code == NE_EXPR)
12755 return omit_two_operands_loc (loc, type, boolean_true_node,
12756 imag0, imag1);
12757 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12761 icond = fold_binary_loc (loc, code, type, imag0, imag1);
12762 if (icond && TREE_CODE (icond) == INTEGER_CST)
12764 if (integer_zerop (icond))
12766 if (code == EQ_EXPR)
12767 return omit_two_operands_loc (loc, type, boolean_false_node,
12768 real0, real1);
12769 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12771 else
12773 if (code == NE_EXPR)
12774 return omit_two_operands_loc (loc, type, boolean_true_node,
12775 real0, real1);
12776 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12781 return NULL_TREE;
12783 case LT_EXPR:
12784 case GT_EXPR:
12785 case LE_EXPR:
12786 case GE_EXPR:
12787 tem = fold_comparison (loc, code, type, op0, op1);
12788 if (tem != NULL_TREE)
12789 return tem;
12791 /* Transform comparisons of the form X +- C CMP X. */
12792 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12793 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12794 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12795 && !HONOR_SNANS (arg0))
12796 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12797 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12799 tree arg01 = TREE_OPERAND (arg0, 1);
12800 enum tree_code code0 = TREE_CODE (arg0);
12801 int is_positive;
12803 if (TREE_CODE (arg01) == REAL_CST)
12804 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12805 else
12806 is_positive = tree_int_cst_sgn (arg01);
12808 /* (X - c) > X becomes false. */
12809 if (code == GT_EXPR
12810 && ((code0 == MINUS_EXPR && is_positive >= 0)
12811 || (code0 == PLUS_EXPR && is_positive <= 0)))
12813 if (TREE_CODE (arg01) == INTEGER_CST
12814 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12815 fold_overflow_warning (("assuming signed overflow does not "
12816 "occur when assuming that (X - c) > X "
12817 "is always false"),
12818 WARN_STRICT_OVERFLOW_ALL);
12819 return constant_boolean_node (0, type);
12822 /* Likewise (X + c) < X becomes false. */
12823 if (code == LT_EXPR
12824 && ((code0 == PLUS_EXPR && is_positive >= 0)
12825 || (code0 == MINUS_EXPR && is_positive <= 0)))
12827 if (TREE_CODE (arg01) == INTEGER_CST
12828 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12829 fold_overflow_warning (("assuming signed overflow does not "
12830 "occur when assuming that "
12831 "(X + c) < X is always false"),
12832 WARN_STRICT_OVERFLOW_ALL);
12833 return constant_boolean_node (0, type);
12836 /* Convert (X - c) <= X to true. */
12837 if (!HONOR_NANS (arg1)
12838 && code == LE_EXPR
12839 && ((code0 == MINUS_EXPR && is_positive >= 0)
12840 || (code0 == PLUS_EXPR && is_positive <= 0)))
12842 if (TREE_CODE (arg01) == INTEGER_CST
12843 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12844 fold_overflow_warning (("assuming signed overflow does not "
12845 "occur when assuming that "
12846 "(X - c) <= X is always true"),
12847 WARN_STRICT_OVERFLOW_ALL);
12848 return constant_boolean_node (1, type);
12851 /* Convert (X + c) >= X to true. */
12852 if (!HONOR_NANS (arg1)
12853 && code == GE_EXPR
12854 && ((code0 == PLUS_EXPR && is_positive >= 0)
12855 || (code0 == MINUS_EXPR && is_positive <= 0)))
12857 if (TREE_CODE (arg01) == INTEGER_CST
12858 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12859 fold_overflow_warning (("assuming signed overflow does not "
12860 "occur when assuming that "
12861 "(X + c) >= X is always true"),
12862 WARN_STRICT_OVERFLOW_ALL);
12863 return constant_boolean_node (1, type);
12866 if (TREE_CODE (arg01) == INTEGER_CST)
12868 /* Convert X + c > X and X - c < X to true for integers. */
12869 if (code == GT_EXPR
12870 && ((code0 == PLUS_EXPR && is_positive > 0)
12871 || (code0 == MINUS_EXPR && is_positive < 0)))
12873 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12874 fold_overflow_warning (("assuming signed overflow does "
12875 "not occur when assuming that "
12876 "(X + c) > X is always true"),
12877 WARN_STRICT_OVERFLOW_ALL);
12878 return constant_boolean_node (1, type);
12881 if (code == LT_EXPR
12882 && ((code0 == MINUS_EXPR && is_positive > 0)
12883 || (code0 == PLUS_EXPR && is_positive < 0)))
12885 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12886 fold_overflow_warning (("assuming signed overflow does "
12887 "not occur when assuming that "
12888 "(X - c) < X is always true"),
12889 WARN_STRICT_OVERFLOW_ALL);
12890 return constant_boolean_node (1, type);
12893 /* Convert X + c <= X and X - c >= X to false for integers. */
12894 if (code == LE_EXPR
12895 && ((code0 == PLUS_EXPR && is_positive > 0)
12896 || (code0 == MINUS_EXPR && is_positive < 0)))
12898 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12899 fold_overflow_warning (("assuming signed overflow does "
12900 "not occur when assuming that "
12901 "(X + c) <= X is always false"),
12902 WARN_STRICT_OVERFLOW_ALL);
12903 return constant_boolean_node (0, type);
12906 if (code == GE_EXPR
12907 && ((code0 == MINUS_EXPR && is_positive > 0)
12908 || (code0 == PLUS_EXPR && is_positive < 0)))
12910 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12911 fold_overflow_warning (("assuming signed overflow does "
12912 "not occur when assuming that "
12913 "(X - c) >= X is always false"),
12914 WARN_STRICT_OVERFLOW_ALL);
12915 return constant_boolean_node (0, type);
12920 /* Comparisons with the highest or lowest possible integer of
12921 the specified precision will have known values. */
12923 tree arg1_type = TREE_TYPE (arg1);
12924 unsigned int prec = TYPE_PRECISION (arg1_type);
12926 if (TREE_CODE (arg1) == INTEGER_CST
12927 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12929 wide_int max = wi::max_value (arg1_type);
12930 wide_int signed_max = wi::max_value (prec, SIGNED);
12931 wide_int min = wi::min_value (arg1_type);
12933 if (wi::eq_p (arg1, max))
12934 switch (code)
12936 case GT_EXPR:
12937 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12939 case GE_EXPR:
12940 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12942 case LE_EXPR:
12943 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12945 case LT_EXPR:
12946 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12948 /* The GE_EXPR and LT_EXPR cases above are not normally
12949 reached because of previous transformations. */
12951 default:
12952 break;
12954 else if (wi::eq_p (arg1, max - 1))
12955 switch (code)
12957 case GT_EXPR:
12958 arg1 = const_binop (PLUS_EXPR, arg1,
12959 build_int_cst (TREE_TYPE (arg1), 1));
12960 return fold_build2_loc (loc, EQ_EXPR, type,
12961 fold_convert_loc (loc,
12962 TREE_TYPE (arg1), arg0),
12963 arg1);
12964 case LE_EXPR:
12965 arg1 = const_binop (PLUS_EXPR, arg1,
12966 build_int_cst (TREE_TYPE (arg1), 1));
12967 return fold_build2_loc (loc, NE_EXPR, type,
12968 fold_convert_loc (loc, TREE_TYPE (arg1),
12969 arg0),
12970 arg1);
12971 default:
12972 break;
12974 else if (wi::eq_p (arg1, min))
12975 switch (code)
12977 case LT_EXPR:
12978 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12980 case LE_EXPR:
12981 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12983 case GE_EXPR:
12984 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12986 case GT_EXPR:
12987 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12989 default:
12990 break;
12992 else if (wi::eq_p (arg1, min + 1))
12993 switch (code)
12995 case GE_EXPR:
12996 arg1 = const_binop (MINUS_EXPR, arg1,
12997 build_int_cst (TREE_TYPE (arg1), 1));
12998 return fold_build2_loc (loc, NE_EXPR, type,
12999 fold_convert_loc (loc,
13000 TREE_TYPE (arg1), arg0),
13001 arg1);
13002 case LT_EXPR:
13003 arg1 = const_binop (MINUS_EXPR, arg1,
13004 build_int_cst (TREE_TYPE (arg1), 1));
13005 return fold_build2_loc (loc, EQ_EXPR, type,
13006 fold_convert_loc (loc, TREE_TYPE (arg1),
13007 arg0),
13008 arg1);
13009 default:
13010 break;
13013 else if (wi::eq_p (arg1, signed_max)
13014 && TYPE_UNSIGNED (arg1_type)
13015 /* We will flip the signedness of the comparison operator
13016 associated with the mode of arg1, so the sign bit is
13017 specified by this mode. Check that arg1 is the signed
13018 max associated with this sign bit. */
13019 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
13020 /* signed_type does not work on pointer types. */
13021 && INTEGRAL_TYPE_P (arg1_type))
13023 /* The following case also applies to X < signed_max+1
13024 and X >= signed_max+1 because previous transformations. */
13025 if (code == LE_EXPR || code == GT_EXPR)
13027 tree st = signed_type_for (arg1_type);
13028 return fold_build2_loc (loc,
13029 code == LE_EXPR ? GE_EXPR : LT_EXPR,
13030 type, fold_convert_loc (loc, st, arg0),
13031 build_int_cst (st, 0));
13037 /* If we are comparing an ABS_EXPR with a constant, we can
13038 convert all the cases into explicit comparisons, but they may
13039 well not be faster than doing the ABS and one comparison.
13040 But ABS (X) <= C is a range comparison, which becomes a subtraction
13041 and a comparison, and is probably faster. */
13042 if (code == LE_EXPR
13043 && TREE_CODE (arg1) == INTEGER_CST
13044 && TREE_CODE (arg0) == ABS_EXPR
13045 && ! TREE_SIDE_EFFECTS (arg0)
13046 && (0 != (tem = negate_expr (arg1)))
13047 && TREE_CODE (tem) == INTEGER_CST
13048 && !TREE_OVERFLOW (tem))
13049 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13050 build2 (GE_EXPR, type,
13051 TREE_OPERAND (arg0, 0), tem),
13052 build2 (LE_EXPR, type,
13053 TREE_OPERAND (arg0, 0), arg1));
13055 /* Convert ABS_EXPR<x> >= 0 to true. */
13056 strict_overflow_p = false;
13057 if (code == GE_EXPR
13058 && (integer_zerop (arg1)
13059 || (! HONOR_NANS (arg0)
13060 && real_zerop (arg1)))
13061 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13063 if (strict_overflow_p)
13064 fold_overflow_warning (("assuming signed overflow does not occur "
13065 "when simplifying comparison of "
13066 "absolute value and zero"),
13067 WARN_STRICT_OVERFLOW_CONDITIONAL);
13068 return omit_one_operand_loc (loc, type,
13069 constant_boolean_node (true, type),
13070 arg0);
13073 /* Convert ABS_EXPR<x> < 0 to false. */
13074 strict_overflow_p = false;
13075 if (code == LT_EXPR
13076 && (integer_zerop (arg1) || real_zerop (arg1))
13077 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13079 if (strict_overflow_p)
13080 fold_overflow_warning (("assuming signed overflow does not occur "
13081 "when simplifying comparison of "
13082 "absolute value and zero"),
13083 WARN_STRICT_OVERFLOW_CONDITIONAL);
13084 return omit_one_operand_loc (loc, type,
13085 constant_boolean_node (false, type),
13086 arg0);
13089 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13090 and similarly for >= into !=. */
13091 if ((code == LT_EXPR || code == GE_EXPR)
13092 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13093 && TREE_CODE (arg1) == LSHIFT_EXPR
13094 && integer_onep (TREE_OPERAND (arg1, 0)))
13095 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13096 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13097 TREE_OPERAND (arg1, 1)),
13098 build_zero_cst (TREE_TYPE (arg0)));
13100 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
13101 otherwise Y might be >= # of bits in X's type and thus e.g.
13102 (unsigned char) (1 << Y) for Y 15 might be 0.
13103 If the cast is widening, then 1 << Y should have unsigned type,
13104 otherwise if Y is number of bits in the signed shift type minus 1,
13105 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
13106 31 might be 0xffffffff80000000. */
13107 if ((code == LT_EXPR || code == GE_EXPR)
13108 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13109 && CONVERT_EXPR_P (arg1)
13110 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13111 && (element_precision (TREE_TYPE (arg1))
13112 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
13113 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
13114 || (element_precision (TREE_TYPE (arg1))
13115 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
13116 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13118 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13119 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
13120 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13121 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
13122 build_zero_cst (TREE_TYPE (arg0)));
13125 return NULL_TREE;
13127 case UNORDERED_EXPR:
13128 case ORDERED_EXPR:
13129 case UNLT_EXPR:
13130 case UNLE_EXPR:
13131 case UNGT_EXPR:
13132 case UNGE_EXPR:
13133 case UNEQ_EXPR:
13134 case LTGT_EXPR:
13135 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13137 t1 = fold_relational_const (code, type, arg0, arg1);
13138 if (t1 != NULL_TREE)
13139 return t1;
13142 /* If the first operand is NaN, the result is constant. */
13143 if (TREE_CODE (arg0) == REAL_CST
13144 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13145 && (code != LTGT_EXPR || ! flag_trapping_math))
13147 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13148 ? integer_zero_node
13149 : integer_one_node;
13150 return omit_one_operand_loc (loc, type, t1, arg1);
13153 /* If the second operand is NaN, the result is constant. */
13154 if (TREE_CODE (arg1) == REAL_CST
13155 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13156 && (code != LTGT_EXPR || ! flag_trapping_math))
13158 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13159 ? integer_zero_node
13160 : integer_one_node;
13161 return omit_one_operand_loc (loc, type, t1, arg0);
13164 /* Simplify unordered comparison of something with itself. */
13165 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13166 && operand_equal_p (arg0, arg1, 0))
13167 return constant_boolean_node (1, type);
13169 if (code == LTGT_EXPR
13170 && !flag_trapping_math
13171 && operand_equal_p (arg0, arg1, 0))
13172 return constant_boolean_node (0, type);
13174 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13176 tree targ0 = strip_float_extensions (arg0);
13177 tree targ1 = strip_float_extensions (arg1);
13178 tree newtype = TREE_TYPE (targ0);
13180 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13181 newtype = TREE_TYPE (targ1);
13183 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13184 return fold_build2_loc (loc, code, type,
13185 fold_convert_loc (loc, newtype, targ0),
13186 fold_convert_loc (loc, newtype, targ1));
13189 return NULL_TREE;
13191 case COMPOUND_EXPR:
13192 /* When pedantic, a compound expression can be neither an lvalue
13193 nor an integer constant expression. */
13194 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13195 return NULL_TREE;
13196 /* Don't let (0, 0) be null pointer constant. */
13197 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13198 : fold_convert_loc (loc, type, arg1);
13199 return pedantic_non_lvalue_loc (loc, tem);
13201 case ASSERT_EXPR:
13202 /* An ASSERT_EXPR should never be passed to fold_binary. */
13203 gcc_unreachable ();
13205 default:
13206 return NULL_TREE;
13207 } /* switch (code) */
13210 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
13211 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
13212 of GOTO_EXPR. */
13214 static tree
13215 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13217 switch (TREE_CODE (*tp))
13219 case LABEL_EXPR:
13220 return *tp;
13222 case GOTO_EXPR:
13223 *walk_subtrees = 0;
13225 /* ... fall through ... */
13227 default:
13228 return NULL_TREE;
13232 /* Return whether the sub-tree ST contains a label which is accessible from
13233 outside the sub-tree. */
13235 static bool
13236 contains_label_p (tree st)
13238 return
13239 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13242 /* Fold a ternary expression of code CODE and type TYPE with operands
13243 OP0, OP1, and OP2. Return the folded expression if folding is
13244 successful. Otherwise, return NULL_TREE. */
13246 tree
13247 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13248 tree op0, tree op1, tree op2)
13250 tree tem;
13251 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
13252 enum tree_code_class kind = TREE_CODE_CLASS (code);
13254 gcc_assert (IS_EXPR_CODE_CLASS (kind)
13255 && TREE_CODE_LENGTH (code) == 3);
13257 /* If this is a commutative operation, and OP0 is a constant, move it
13258 to OP1 to reduce the number of tests below. */
13259 if (commutative_ternary_tree_code (code)
13260 && tree_swap_operands_p (op0, op1, true))
13261 return fold_build3_loc (loc, code, type, op1, op0, op2);
13263 tem = generic_simplify (loc, code, type, op0, op1, op2);
13264 if (tem)
13265 return tem;
13267 /* Strip any conversions that don't change the mode. This is safe
13268 for every expression, except for a comparison expression because
13269 its signedness is derived from its operands. So, in the latter
13270 case, only strip conversions that don't change the signedness.
13272 Note that this is done as an internal manipulation within the
13273 constant folder, in order to find the simplest representation of
13274 the arguments so that their form can be studied. In any cases,
13275 the appropriate type conversions should be put back in the tree
13276 that will get out of the constant folder. */
13277 if (op0)
13279 arg0 = op0;
13280 STRIP_NOPS (arg0);
13283 if (op1)
13285 arg1 = op1;
13286 STRIP_NOPS (arg1);
13289 if (op2)
13291 arg2 = op2;
13292 STRIP_NOPS (arg2);
13295 switch (code)
13297 case COMPONENT_REF:
13298 if (TREE_CODE (arg0) == CONSTRUCTOR
13299 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13301 unsigned HOST_WIDE_INT idx;
13302 tree field, value;
13303 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13304 if (field == arg1)
13305 return value;
13307 return NULL_TREE;
13309 case COND_EXPR:
13310 case VEC_COND_EXPR:
13311 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13312 so all simple results must be passed through pedantic_non_lvalue. */
13313 if (TREE_CODE (arg0) == INTEGER_CST)
13315 tree unused_op = integer_zerop (arg0) ? op1 : op2;
13316 tem = integer_zerop (arg0) ? op2 : op1;
13317 /* Only optimize constant conditions when the selected branch
13318 has the same type as the COND_EXPR. This avoids optimizing
13319 away "c ? x : throw", where the throw has a void type.
13320 Avoid throwing away that operand which contains label. */
13321 if ((!TREE_SIDE_EFFECTS (unused_op)
13322 || !contains_label_p (unused_op))
13323 && (! VOID_TYPE_P (TREE_TYPE (tem))
13324 || VOID_TYPE_P (type)))
13325 return pedantic_non_lvalue_loc (loc, tem);
13326 return NULL_TREE;
13328 else if (TREE_CODE (arg0) == VECTOR_CST)
13330 if ((TREE_CODE (arg1) == VECTOR_CST
13331 || TREE_CODE (arg1) == CONSTRUCTOR)
13332 && (TREE_CODE (arg2) == VECTOR_CST
13333 || TREE_CODE (arg2) == CONSTRUCTOR))
13335 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
13336 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
13337 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
13338 for (i = 0; i < nelts; i++)
13340 tree val = VECTOR_CST_ELT (arg0, i);
13341 if (integer_all_onesp (val))
13342 sel[i] = i;
13343 else if (integer_zerop (val))
13344 sel[i] = nelts + i;
13345 else /* Currently unreachable. */
13346 return NULL_TREE;
13348 tree t = fold_vec_perm (type, arg1, arg2, sel);
13349 if (t != NULL_TREE)
13350 return t;
13354 /* If we have A op B ? A : C, we may be able to convert this to a
13355 simpler expression, depending on the operation and the values
13356 of B and C. Signed zeros prevent all of these transformations,
13357 for reasons given above each one.
13359 Also try swapping the arguments and inverting the conditional. */
13360 if (COMPARISON_CLASS_P (arg0)
13361 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13362 arg1, TREE_OPERAND (arg0, 1))
13363 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
13365 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13366 if (tem)
13367 return tem;
13370 if (COMPARISON_CLASS_P (arg0)
13371 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13372 op2,
13373 TREE_OPERAND (arg0, 1))
13374 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
13376 location_t loc0 = expr_location_or (arg0, loc);
13377 tem = fold_invert_truthvalue (loc0, arg0);
13378 if (tem && COMPARISON_CLASS_P (tem))
13380 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13381 if (tem)
13382 return tem;
13386 /* If the second operand is simpler than the third, swap them
13387 since that produces better jump optimization results. */
13388 if (truth_value_p (TREE_CODE (arg0))
13389 && tree_swap_operands_p (op1, op2, false))
13391 location_t loc0 = expr_location_or (arg0, loc);
13392 /* See if this can be inverted. If it can't, possibly because
13393 it was a floating-point inequality comparison, don't do
13394 anything. */
13395 tem = fold_invert_truthvalue (loc0, arg0);
13396 if (tem)
13397 return fold_build3_loc (loc, code, type, tem, op2, op1);
13400 /* Convert A ? 1 : 0 to simply A. */
13401 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
13402 : (integer_onep (op1)
13403 && !VECTOR_TYPE_P (type)))
13404 && integer_zerop (op2)
13405 /* If we try to convert OP0 to our type, the
13406 call to fold will try to move the conversion inside
13407 a COND, which will recurse. In that case, the COND_EXPR
13408 is probably the best choice, so leave it alone. */
13409 && type == TREE_TYPE (arg0))
13410 return pedantic_non_lvalue_loc (loc, arg0);
13412 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13413 over COND_EXPR in cases such as floating point comparisons. */
13414 if (integer_zerop (op1)
13415 && (code == VEC_COND_EXPR ? integer_all_onesp (op2)
13416 : (integer_onep (op2)
13417 && !VECTOR_TYPE_P (type)))
13418 && truth_value_p (TREE_CODE (arg0)))
13419 return pedantic_non_lvalue_loc (loc,
13420 fold_convert_loc (loc, type,
13421 invert_truthvalue_loc (loc,
13422 arg0)));
13424 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13425 if (TREE_CODE (arg0) == LT_EXPR
13426 && integer_zerop (TREE_OPERAND (arg0, 1))
13427 && integer_zerop (op2)
13428 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13430 /* sign_bit_p looks through both zero and sign extensions,
13431 but for this optimization only sign extensions are
13432 usable. */
13433 tree tem2 = TREE_OPERAND (arg0, 0);
13434 while (tem != tem2)
13436 if (TREE_CODE (tem2) != NOP_EXPR
13437 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
13439 tem = NULL_TREE;
13440 break;
13442 tem2 = TREE_OPERAND (tem2, 0);
13444 /* sign_bit_p only checks ARG1 bits within A's precision.
13445 If <sign bit of A> has wider type than A, bits outside
13446 of A's precision in <sign bit of A> need to be checked.
13447 If they are all 0, this optimization needs to be done
13448 in unsigned A's type, if they are all 1 in signed A's type,
13449 otherwise this can't be done. */
13450 if (tem
13451 && TYPE_PRECISION (TREE_TYPE (tem))
13452 < TYPE_PRECISION (TREE_TYPE (arg1))
13453 && TYPE_PRECISION (TREE_TYPE (tem))
13454 < TYPE_PRECISION (type))
13456 int inner_width, outer_width;
13457 tree tem_type;
13459 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13460 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13461 if (outer_width > TYPE_PRECISION (type))
13462 outer_width = TYPE_PRECISION (type);
13464 wide_int mask = wi::shifted_mask
13465 (inner_width, outer_width - inner_width, false,
13466 TYPE_PRECISION (TREE_TYPE (arg1)));
13468 wide_int common = mask & arg1;
13469 if (common == mask)
13471 tem_type = signed_type_for (TREE_TYPE (tem));
13472 tem = fold_convert_loc (loc, tem_type, tem);
13474 else if (common == 0)
13476 tem_type = unsigned_type_for (TREE_TYPE (tem));
13477 tem = fold_convert_loc (loc, tem_type, tem);
13479 else
13480 tem = NULL;
13483 if (tem)
13484 return
13485 fold_convert_loc (loc, type,
13486 fold_build2_loc (loc, BIT_AND_EXPR,
13487 TREE_TYPE (tem), tem,
13488 fold_convert_loc (loc,
13489 TREE_TYPE (tem),
13490 arg1)));
13493 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13494 already handled above. */
13495 if (TREE_CODE (arg0) == BIT_AND_EXPR
13496 && integer_onep (TREE_OPERAND (arg0, 1))
13497 && integer_zerop (op2)
13498 && integer_pow2p (arg1))
13500 tree tem = TREE_OPERAND (arg0, 0);
13501 STRIP_NOPS (tem);
13502 if (TREE_CODE (tem) == RSHIFT_EXPR
13503 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
13504 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13505 tree_to_uhwi (TREE_OPERAND (tem, 1)))
13506 return fold_build2_loc (loc, BIT_AND_EXPR, type,
13507 TREE_OPERAND (tem, 0), arg1);
13510 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13511 is probably obsolete because the first operand should be a
13512 truth value (that's why we have the two cases above), but let's
13513 leave it in until we can confirm this for all front-ends. */
13514 if (integer_zerop (op2)
13515 && TREE_CODE (arg0) == NE_EXPR
13516 && integer_zerop (TREE_OPERAND (arg0, 1))
13517 && integer_pow2p (arg1)
13518 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13519 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13520 arg1, OEP_ONLY_CONST))
13521 return pedantic_non_lvalue_loc (loc,
13522 fold_convert_loc (loc, type,
13523 TREE_OPERAND (arg0, 0)));
13525 /* Disable the transformations below for vectors, since
13526 fold_binary_op_with_conditional_arg may undo them immediately,
13527 yielding an infinite loop. */
13528 if (code == VEC_COND_EXPR)
13529 return NULL_TREE;
13531 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13532 if (integer_zerop (op2)
13533 && truth_value_p (TREE_CODE (arg0))
13534 && truth_value_p (TREE_CODE (arg1))
13535 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13536 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13537 : TRUTH_ANDIF_EXPR,
13538 type, fold_convert_loc (loc, type, arg0), arg1);
13540 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13541 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13542 && truth_value_p (TREE_CODE (arg0))
13543 && truth_value_p (TREE_CODE (arg1))
13544 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13546 location_t loc0 = expr_location_or (arg0, loc);
13547 /* Only perform transformation if ARG0 is easily inverted. */
13548 tem = fold_invert_truthvalue (loc0, arg0);
13549 if (tem)
13550 return fold_build2_loc (loc, code == VEC_COND_EXPR
13551 ? BIT_IOR_EXPR
13552 : TRUTH_ORIF_EXPR,
13553 type, fold_convert_loc (loc, type, tem),
13554 arg1);
13557 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13558 if (integer_zerop (arg1)
13559 && truth_value_p (TREE_CODE (arg0))
13560 && truth_value_p (TREE_CODE (op2))
13561 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13563 location_t loc0 = expr_location_or (arg0, loc);
13564 /* Only perform transformation if ARG0 is easily inverted. */
13565 tem = fold_invert_truthvalue (loc0, arg0);
13566 if (tem)
13567 return fold_build2_loc (loc, code == VEC_COND_EXPR
13568 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13569 type, fold_convert_loc (loc, type, tem),
13570 op2);
13573 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13574 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13575 && truth_value_p (TREE_CODE (arg0))
13576 && truth_value_p (TREE_CODE (op2))
13577 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13578 return fold_build2_loc (loc, code == VEC_COND_EXPR
13579 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13580 type, fold_convert_loc (loc, type, arg0), op2);
13582 return NULL_TREE;
13584 case CALL_EXPR:
13585 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13586 of fold_ternary on them. */
13587 gcc_unreachable ();
13589 case BIT_FIELD_REF:
13590 if ((TREE_CODE (arg0) == VECTOR_CST
13591 || (TREE_CODE (arg0) == CONSTRUCTOR
13592 && TREE_CODE (TREE_TYPE (arg0)) == VECTOR_TYPE))
13593 && (type == TREE_TYPE (TREE_TYPE (arg0))
13594 || (TREE_CODE (type) == VECTOR_TYPE
13595 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
13597 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13598 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
13599 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13600 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13602 if (n != 0
13603 && (idx % width) == 0
13604 && (n % width) == 0
13605 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13607 idx = idx / width;
13608 n = n / width;
13610 if (TREE_CODE (arg0) == VECTOR_CST)
13612 if (n == 1)
13613 return VECTOR_CST_ELT (arg0, idx);
13615 tree *vals = XALLOCAVEC (tree, n);
13616 for (unsigned i = 0; i < n; ++i)
13617 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
13618 return build_vector (type, vals);
13621 /* Constructor elements can be subvectors. */
13622 unsigned HOST_WIDE_INT k = 1;
13623 if (CONSTRUCTOR_NELTS (arg0) != 0)
13625 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (arg0, 0)->value);
13626 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
13627 k = TYPE_VECTOR_SUBPARTS (cons_elem);
13630 /* We keep an exact subset of the constructor elements. */
13631 if ((idx % k) == 0 && (n % k) == 0)
13633 if (CONSTRUCTOR_NELTS (arg0) == 0)
13634 return build_constructor (type, NULL);
13635 idx /= k;
13636 n /= k;
13637 if (n == 1)
13639 if (idx < CONSTRUCTOR_NELTS (arg0))
13640 return CONSTRUCTOR_ELT (arg0, idx)->value;
13641 return build_zero_cst (type);
13644 vec<constructor_elt, va_gc> *vals;
13645 vec_alloc (vals, n);
13646 for (unsigned i = 0;
13647 i < n && idx + i < CONSTRUCTOR_NELTS (arg0);
13648 ++i)
13649 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
13650 CONSTRUCTOR_ELT
13651 (arg0, idx + i)->value);
13652 return build_constructor (type, vals);
13654 /* The bitfield references a single constructor element. */
13655 else if (idx + n <= (idx / k + 1) * k)
13657 if (CONSTRUCTOR_NELTS (arg0) <= idx / k)
13658 return build_zero_cst (type);
13659 else if (n == k)
13660 return CONSTRUCTOR_ELT (arg0, idx / k)->value;
13661 else
13662 return fold_build3_loc (loc, code, type,
13663 CONSTRUCTOR_ELT (arg0, idx / k)->value, op1,
13664 build_int_cst (TREE_TYPE (op2), (idx % k) * width));
13669 /* A bit-field-ref that referenced the full argument can be stripped. */
13670 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13671 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_to_uhwi (arg1)
13672 && integer_zerop (op2))
13673 return fold_convert_loc (loc, type, arg0);
13675 /* On constants we can use native encode/interpret to constant
13676 fold (nearly) all BIT_FIELD_REFs. */
13677 if (CONSTANT_CLASS_P (arg0)
13678 && can_native_interpret_type_p (type)
13679 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0)))
13680 /* This limitation should not be necessary, we just need to
13681 round this up to mode size. */
13682 && tree_to_uhwi (op1) % BITS_PER_UNIT == 0
13683 /* Need bit-shifting of the buffer to relax the following. */
13684 && tree_to_uhwi (op2) % BITS_PER_UNIT == 0)
13686 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13687 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13688 unsigned HOST_WIDE_INT clen;
13689 clen = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0)));
13690 /* ??? We cannot tell native_encode_expr to start at
13691 some random byte only. So limit us to a reasonable amount
13692 of work. */
13693 if (clen <= 4096)
13695 unsigned char *b = XALLOCAVEC (unsigned char, clen);
13696 unsigned HOST_WIDE_INT len = native_encode_expr (arg0, b, clen);
13697 if (len > 0
13698 && len * BITS_PER_UNIT >= bitpos + bitsize)
13700 tree v = native_interpret_expr (type,
13701 b + bitpos / BITS_PER_UNIT,
13702 bitsize / BITS_PER_UNIT);
13703 if (v)
13704 return v;
13709 return NULL_TREE;
13711 case FMA_EXPR:
13712 /* For integers we can decompose the FMA if possible. */
13713 if (TREE_CODE (arg0) == INTEGER_CST
13714 && TREE_CODE (arg1) == INTEGER_CST)
13715 return fold_build2_loc (loc, PLUS_EXPR, type,
13716 const_binop (MULT_EXPR, arg0, arg1), arg2);
13717 if (integer_zerop (arg2))
13718 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
13720 return fold_fma (loc, type, arg0, arg1, arg2);
13722 case VEC_PERM_EXPR:
13723 if (TREE_CODE (arg2) == VECTOR_CST)
13725 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
13726 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
13727 unsigned char *sel2 = sel + nelts;
13728 bool need_mask_canon = false;
13729 bool need_mask_canon2 = false;
13730 bool all_in_vec0 = true;
13731 bool all_in_vec1 = true;
13732 bool maybe_identity = true;
13733 bool single_arg = (op0 == op1);
13734 bool changed = false;
13736 mask2 = 2 * nelts - 1;
13737 mask = single_arg ? (nelts - 1) : mask2;
13738 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
13739 for (i = 0; i < nelts; i++)
13741 tree val = VECTOR_CST_ELT (arg2, i);
13742 if (TREE_CODE (val) != INTEGER_CST)
13743 return NULL_TREE;
13745 /* Make sure that the perm value is in an acceptable
13746 range. */
13747 wide_int t = val;
13748 need_mask_canon |= wi::gtu_p (t, mask);
13749 need_mask_canon2 |= wi::gtu_p (t, mask2);
13750 sel[i] = t.to_uhwi () & mask;
13751 sel2[i] = t.to_uhwi () & mask2;
13753 if (sel[i] < nelts)
13754 all_in_vec1 = false;
13755 else
13756 all_in_vec0 = false;
13758 if ((sel[i] & (nelts-1)) != i)
13759 maybe_identity = false;
13762 if (maybe_identity)
13764 if (all_in_vec0)
13765 return op0;
13766 if (all_in_vec1)
13767 return op1;
13770 if (all_in_vec0)
13771 op1 = op0;
13772 else if (all_in_vec1)
13774 op0 = op1;
13775 for (i = 0; i < nelts; i++)
13776 sel[i] -= nelts;
13777 need_mask_canon = true;
13780 if ((TREE_CODE (op0) == VECTOR_CST
13781 || TREE_CODE (op0) == CONSTRUCTOR)
13782 && (TREE_CODE (op1) == VECTOR_CST
13783 || TREE_CODE (op1) == CONSTRUCTOR))
13785 tree t = fold_vec_perm (type, op0, op1, sel);
13786 if (t != NULL_TREE)
13787 return t;
13790 if (op0 == op1 && !single_arg)
13791 changed = true;
13793 /* Some targets are deficient and fail to expand a single
13794 argument permutation while still allowing an equivalent
13795 2-argument version. */
13796 if (need_mask_canon && arg2 == op2
13797 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
13798 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
13800 need_mask_canon = need_mask_canon2;
13801 sel = sel2;
13804 if (need_mask_canon && arg2 == op2)
13806 tree *tsel = XALLOCAVEC (tree, nelts);
13807 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
13808 for (i = 0; i < nelts; i++)
13809 tsel[i] = build_int_cst (eltype, sel[i]);
13810 op2 = build_vector (TREE_TYPE (arg2), tsel);
13811 changed = true;
13814 if (changed)
13815 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
13817 return NULL_TREE;
13819 default:
13820 return NULL_TREE;
13821 } /* switch (code) */
13824 /* Perform constant folding and related simplification of EXPR.
13825 The related simplifications include x*1 => x, x*0 => 0, etc.,
13826 and application of the associative law.
13827 NOP_EXPR conversions may be removed freely (as long as we
13828 are careful not to change the type of the overall expression).
13829 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13830 but we can constant-fold them if they have constant operands. */
13832 #ifdef ENABLE_FOLD_CHECKING
13833 # define fold(x) fold_1 (x)
13834 static tree fold_1 (tree);
13835 static
13836 #endif
13837 tree
13838 fold (tree expr)
13840 const tree t = expr;
13841 enum tree_code code = TREE_CODE (t);
13842 enum tree_code_class kind = TREE_CODE_CLASS (code);
13843 tree tem;
13844 location_t loc = EXPR_LOCATION (expr);
13846 /* Return right away if a constant. */
13847 if (kind == tcc_constant)
13848 return t;
13850 /* CALL_EXPR-like objects with variable numbers of operands are
13851 treated specially. */
13852 if (kind == tcc_vl_exp)
13854 if (code == CALL_EXPR)
13856 tem = fold_call_expr (loc, expr, false);
13857 return tem ? tem : expr;
13859 return expr;
13862 if (IS_EXPR_CODE_CLASS (kind))
13864 tree type = TREE_TYPE (t);
13865 tree op0, op1, op2;
13867 switch (TREE_CODE_LENGTH (code))
13869 case 1:
13870 op0 = TREE_OPERAND (t, 0);
13871 tem = fold_unary_loc (loc, code, type, op0);
13872 return tem ? tem : expr;
13873 case 2:
13874 op0 = TREE_OPERAND (t, 0);
13875 op1 = TREE_OPERAND (t, 1);
13876 tem = fold_binary_loc (loc, code, type, op0, op1);
13877 return tem ? tem : expr;
13878 case 3:
13879 op0 = TREE_OPERAND (t, 0);
13880 op1 = TREE_OPERAND (t, 1);
13881 op2 = TREE_OPERAND (t, 2);
13882 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13883 return tem ? tem : expr;
13884 default:
13885 break;
13889 switch (code)
13891 case ARRAY_REF:
13893 tree op0 = TREE_OPERAND (t, 0);
13894 tree op1 = TREE_OPERAND (t, 1);
13896 if (TREE_CODE (op1) == INTEGER_CST
13897 && TREE_CODE (op0) == CONSTRUCTOR
13898 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13900 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (op0);
13901 unsigned HOST_WIDE_INT end = vec_safe_length (elts);
13902 unsigned HOST_WIDE_INT begin = 0;
13904 /* Find a matching index by means of a binary search. */
13905 while (begin != end)
13907 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13908 tree index = (*elts)[middle].index;
13910 if (TREE_CODE (index) == INTEGER_CST
13911 && tree_int_cst_lt (index, op1))
13912 begin = middle + 1;
13913 else if (TREE_CODE (index) == INTEGER_CST
13914 && tree_int_cst_lt (op1, index))
13915 end = middle;
13916 else if (TREE_CODE (index) == RANGE_EXPR
13917 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13918 begin = middle + 1;
13919 else if (TREE_CODE (index) == RANGE_EXPR
13920 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13921 end = middle;
13922 else
13923 return (*elts)[middle].value;
13927 return t;
13930 /* Return a VECTOR_CST if possible. */
13931 case CONSTRUCTOR:
13933 tree type = TREE_TYPE (t);
13934 if (TREE_CODE (type) != VECTOR_TYPE)
13935 return t;
13937 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
13938 unsigned HOST_WIDE_INT idx, pos = 0;
13939 tree value;
13941 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, value)
13943 if (!CONSTANT_CLASS_P (value))
13944 return t;
13945 if (TREE_CODE (value) == VECTOR_CST)
13947 for (unsigned i = 0; i < VECTOR_CST_NELTS (value); ++i)
13948 vec[pos++] = VECTOR_CST_ELT (value, i);
13950 else
13951 vec[pos++] = value;
13953 for (; pos < TYPE_VECTOR_SUBPARTS (type); ++pos)
13954 vec[pos] = build_zero_cst (TREE_TYPE (type));
13956 return build_vector (type, vec);
13959 case CONST_DECL:
13960 return fold (DECL_INITIAL (t));
13962 default:
13963 return t;
13964 } /* switch (code) */
13967 #ifdef ENABLE_FOLD_CHECKING
13968 #undef fold
13970 static void fold_checksum_tree (const_tree, struct md5_ctx *,
13971 hash_table<pointer_hash<const tree_node> > *);
13972 static void fold_check_failed (const_tree, const_tree);
13973 void print_fold_checksum (const_tree);
13975 /* When --enable-checking=fold, compute a digest of expr before
13976 and after actual fold call to see if fold did not accidentally
13977 change original expr. */
13979 tree
13980 fold (tree expr)
13982 tree ret;
13983 struct md5_ctx ctx;
13984 unsigned char checksum_before[16], checksum_after[16];
13985 hash_table<pointer_hash<const tree_node> > ht (32);
13987 md5_init_ctx (&ctx);
13988 fold_checksum_tree (expr, &ctx, &ht);
13989 md5_finish_ctx (&ctx, checksum_before);
13990 ht.empty ();
13992 ret = fold_1 (expr);
13994 md5_init_ctx (&ctx);
13995 fold_checksum_tree (expr, &ctx, &ht);
13996 md5_finish_ctx (&ctx, checksum_after);
13998 if (memcmp (checksum_before, checksum_after, 16))
13999 fold_check_failed (expr, ret);
14001 return ret;
14004 void
14005 print_fold_checksum (const_tree expr)
14007 struct md5_ctx ctx;
14008 unsigned char checksum[16], cnt;
14009 hash_table<pointer_hash<const tree_node> > ht (32);
14011 md5_init_ctx (&ctx);
14012 fold_checksum_tree (expr, &ctx, &ht);
14013 md5_finish_ctx (&ctx, checksum);
14014 for (cnt = 0; cnt < 16; ++cnt)
14015 fprintf (stderr, "%02x", checksum[cnt]);
14016 putc ('\n', stderr);
14019 static void
14020 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
14022 internal_error ("fold check: original tree changed by fold");
14025 static void
14026 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
14027 hash_table<pointer_hash <const tree_node> > *ht)
14029 const tree_node **slot;
14030 enum tree_code code;
14031 union tree_node buf;
14032 int i, len;
14034 recursive_label:
14035 if (expr == NULL)
14036 return;
14037 slot = ht->find_slot (expr, INSERT);
14038 if (*slot != NULL)
14039 return;
14040 *slot = expr;
14041 code = TREE_CODE (expr);
14042 if (TREE_CODE_CLASS (code) == tcc_declaration
14043 && HAS_DECL_ASSEMBLER_NAME_P (expr))
14045 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
14046 memcpy ((char *) &buf, expr, tree_size (expr));
14047 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
14048 buf.decl_with_vis.symtab_node = NULL;
14049 expr = (tree) &buf;
14051 else if (TREE_CODE_CLASS (code) == tcc_type
14052 && (TYPE_POINTER_TO (expr)
14053 || TYPE_REFERENCE_TO (expr)
14054 || TYPE_CACHED_VALUES_P (expr)
14055 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
14056 || TYPE_NEXT_VARIANT (expr)))
14058 /* Allow these fields to be modified. */
14059 tree tmp;
14060 memcpy ((char *) &buf, expr, tree_size (expr));
14061 expr = tmp = (tree) &buf;
14062 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
14063 TYPE_POINTER_TO (tmp) = NULL;
14064 TYPE_REFERENCE_TO (tmp) = NULL;
14065 TYPE_NEXT_VARIANT (tmp) = NULL;
14066 if (TYPE_CACHED_VALUES_P (tmp))
14068 TYPE_CACHED_VALUES_P (tmp) = 0;
14069 TYPE_CACHED_VALUES (tmp) = NULL;
14072 md5_process_bytes (expr, tree_size (expr), ctx);
14073 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
14074 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
14075 if (TREE_CODE_CLASS (code) != tcc_type
14076 && TREE_CODE_CLASS (code) != tcc_declaration
14077 && code != TREE_LIST
14078 && code != SSA_NAME
14079 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
14080 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
14081 switch (TREE_CODE_CLASS (code))
14083 case tcc_constant:
14084 switch (code)
14086 case STRING_CST:
14087 md5_process_bytes (TREE_STRING_POINTER (expr),
14088 TREE_STRING_LENGTH (expr), ctx);
14089 break;
14090 case COMPLEX_CST:
14091 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
14092 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
14093 break;
14094 case VECTOR_CST:
14095 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
14096 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
14097 break;
14098 default:
14099 break;
14101 break;
14102 case tcc_exceptional:
14103 switch (code)
14105 case TREE_LIST:
14106 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
14107 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
14108 expr = TREE_CHAIN (expr);
14109 goto recursive_label;
14110 break;
14111 case TREE_VEC:
14112 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
14113 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
14114 break;
14115 default:
14116 break;
14118 break;
14119 case tcc_expression:
14120 case tcc_reference:
14121 case tcc_comparison:
14122 case tcc_unary:
14123 case tcc_binary:
14124 case tcc_statement:
14125 case tcc_vl_exp:
14126 len = TREE_OPERAND_LENGTH (expr);
14127 for (i = 0; i < len; ++i)
14128 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
14129 break;
14130 case tcc_declaration:
14131 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
14132 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
14133 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
14135 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
14136 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
14137 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
14138 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
14139 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
14142 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
14144 if (TREE_CODE (expr) == FUNCTION_DECL)
14146 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
14147 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
14149 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
14151 break;
14152 case tcc_type:
14153 if (TREE_CODE (expr) == ENUMERAL_TYPE)
14154 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
14155 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
14156 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
14157 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
14158 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
14159 if (INTEGRAL_TYPE_P (expr)
14160 || SCALAR_FLOAT_TYPE_P (expr))
14162 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
14163 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
14165 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
14166 if (TREE_CODE (expr) == RECORD_TYPE
14167 || TREE_CODE (expr) == UNION_TYPE
14168 || TREE_CODE (expr) == QUAL_UNION_TYPE)
14169 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
14170 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
14171 break;
14172 default:
14173 break;
14177 /* Helper function for outputting the checksum of a tree T. When
14178 debugging with gdb, you can "define mynext" to be "next" followed
14179 by "call debug_fold_checksum (op0)", then just trace down till the
14180 outputs differ. */
14182 DEBUG_FUNCTION void
14183 debug_fold_checksum (const_tree t)
14185 int i;
14186 unsigned char checksum[16];
14187 struct md5_ctx ctx;
14188 hash_table<pointer_hash<const tree_node> > ht (32);
14190 md5_init_ctx (&ctx);
14191 fold_checksum_tree (t, &ctx, &ht);
14192 md5_finish_ctx (&ctx, checksum);
14193 ht.empty ();
14195 for (i = 0; i < 16; i++)
14196 fprintf (stderr, "%d ", checksum[i]);
14198 fprintf (stderr, "\n");
14201 #endif
14203 /* Fold a unary tree expression with code CODE of type TYPE with an
14204 operand OP0. LOC is the location of the resulting expression.
14205 Return a folded expression if successful. Otherwise, return a tree
14206 expression with code CODE of type TYPE with an operand OP0. */
14208 tree
14209 fold_build1_stat_loc (location_t loc,
14210 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
14212 tree tem;
14213 #ifdef ENABLE_FOLD_CHECKING
14214 unsigned char checksum_before[16], checksum_after[16];
14215 struct md5_ctx ctx;
14216 hash_table<pointer_hash<const tree_node> > ht (32);
14218 md5_init_ctx (&ctx);
14219 fold_checksum_tree (op0, &ctx, &ht);
14220 md5_finish_ctx (&ctx, checksum_before);
14221 ht.empty ();
14222 #endif
14224 tem = fold_unary_loc (loc, code, type, op0);
14225 if (!tem)
14226 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
14228 #ifdef ENABLE_FOLD_CHECKING
14229 md5_init_ctx (&ctx);
14230 fold_checksum_tree (op0, &ctx, &ht);
14231 md5_finish_ctx (&ctx, checksum_after);
14233 if (memcmp (checksum_before, checksum_after, 16))
14234 fold_check_failed (op0, tem);
14235 #endif
14236 return tem;
14239 /* Fold a binary tree expression with code CODE of type TYPE with
14240 operands OP0 and OP1. LOC is the location of the resulting
14241 expression. Return a folded expression if successful. Otherwise,
14242 return a tree expression with code CODE of type TYPE with operands
14243 OP0 and OP1. */
14245 tree
14246 fold_build2_stat_loc (location_t loc,
14247 enum tree_code code, tree type, tree op0, tree op1
14248 MEM_STAT_DECL)
14250 tree tem;
14251 #ifdef ENABLE_FOLD_CHECKING
14252 unsigned char checksum_before_op0[16],
14253 checksum_before_op1[16],
14254 checksum_after_op0[16],
14255 checksum_after_op1[16];
14256 struct md5_ctx ctx;
14257 hash_table<pointer_hash<const tree_node> > ht (32);
14259 md5_init_ctx (&ctx);
14260 fold_checksum_tree (op0, &ctx, &ht);
14261 md5_finish_ctx (&ctx, checksum_before_op0);
14262 ht.empty ();
14264 md5_init_ctx (&ctx);
14265 fold_checksum_tree (op1, &ctx, &ht);
14266 md5_finish_ctx (&ctx, checksum_before_op1);
14267 ht.empty ();
14268 #endif
14270 tem = fold_binary_loc (loc, code, type, op0, op1);
14271 if (!tem)
14272 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
14274 #ifdef ENABLE_FOLD_CHECKING
14275 md5_init_ctx (&ctx);
14276 fold_checksum_tree (op0, &ctx, &ht);
14277 md5_finish_ctx (&ctx, checksum_after_op0);
14278 ht.empty ();
14280 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14281 fold_check_failed (op0, tem);
14283 md5_init_ctx (&ctx);
14284 fold_checksum_tree (op1, &ctx, &ht);
14285 md5_finish_ctx (&ctx, checksum_after_op1);
14287 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14288 fold_check_failed (op1, tem);
14289 #endif
14290 return tem;
14293 /* Fold a ternary tree expression with code CODE of type TYPE with
14294 operands OP0, OP1, and OP2. Return a folded expression if
14295 successful. Otherwise, return a tree expression with code CODE of
14296 type TYPE with operands OP0, OP1, and OP2. */
14298 tree
14299 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14300 tree op0, tree op1, tree op2 MEM_STAT_DECL)
14302 tree tem;
14303 #ifdef ENABLE_FOLD_CHECKING
14304 unsigned char checksum_before_op0[16],
14305 checksum_before_op1[16],
14306 checksum_before_op2[16],
14307 checksum_after_op0[16],
14308 checksum_after_op1[16],
14309 checksum_after_op2[16];
14310 struct md5_ctx ctx;
14311 hash_table<pointer_hash<const tree_node> > ht (32);
14313 md5_init_ctx (&ctx);
14314 fold_checksum_tree (op0, &ctx, &ht);
14315 md5_finish_ctx (&ctx, checksum_before_op0);
14316 ht.empty ();
14318 md5_init_ctx (&ctx);
14319 fold_checksum_tree (op1, &ctx, &ht);
14320 md5_finish_ctx (&ctx, checksum_before_op1);
14321 ht.empty ();
14323 md5_init_ctx (&ctx);
14324 fold_checksum_tree (op2, &ctx, &ht);
14325 md5_finish_ctx (&ctx, checksum_before_op2);
14326 ht.empty ();
14327 #endif
14329 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14330 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14331 if (!tem)
14332 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14334 #ifdef ENABLE_FOLD_CHECKING
14335 md5_init_ctx (&ctx);
14336 fold_checksum_tree (op0, &ctx, &ht);
14337 md5_finish_ctx (&ctx, checksum_after_op0);
14338 ht.empty ();
14340 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14341 fold_check_failed (op0, tem);
14343 md5_init_ctx (&ctx);
14344 fold_checksum_tree (op1, &ctx, &ht);
14345 md5_finish_ctx (&ctx, checksum_after_op1);
14346 ht.empty ();
14348 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14349 fold_check_failed (op1, tem);
14351 md5_init_ctx (&ctx);
14352 fold_checksum_tree (op2, &ctx, &ht);
14353 md5_finish_ctx (&ctx, checksum_after_op2);
14355 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14356 fold_check_failed (op2, tem);
14357 #endif
14358 return tem;
14361 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14362 arguments in ARGARRAY, and a null static chain.
14363 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14364 of type TYPE from the given operands as constructed by build_call_array. */
14366 tree
14367 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14368 int nargs, tree *argarray)
14370 tree tem;
14371 #ifdef ENABLE_FOLD_CHECKING
14372 unsigned char checksum_before_fn[16],
14373 checksum_before_arglist[16],
14374 checksum_after_fn[16],
14375 checksum_after_arglist[16];
14376 struct md5_ctx ctx;
14377 hash_table<pointer_hash<const tree_node> > ht (32);
14378 int i;
14380 md5_init_ctx (&ctx);
14381 fold_checksum_tree (fn, &ctx, &ht);
14382 md5_finish_ctx (&ctx, checksum_before_fn);
14383 ht.empty ();
14385 md5_init_ctx (&ctx);
14386 for (i = 0; i < nargs; i++)
14387 fold_checksum_tree (argarray[i], &ctx, &ht);
14388 md5_finish_ctx (&ctx, checksum_before_arglist);
14389 ht.empty ();
14390 #endif
14392 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14393 if (!tem)
14394 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
14396 #ifdef ENABLE_FOLD_CHECKING
14397 md5_init_ctx (&ctx);
14398 fold_checksum_tree (fn, &ctx, &ht);
14399 md5_finish_ctx (&ctx, checksum_after_fn);
14400 ht.empty ();
14402 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14403 fold_check_failed (fn, tem);
14405 md5_init_ctx (&ctx);
14406 for (i = 0; i < nargs; i++)
14407 fold_checksum_tree (argarray[i], &ctx, &ht);
14408 md5_finish_ctx (&ctx, checksum_after_arglist);
14410 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14411 fold_check_failed (NULL_TREE, tem);
14412 #endif
14413 return tem;
14416 /* Perform constant folding and related simplification of initializer
14417 expression EXPR. These behave identically to "fold_buildN" but ignore
14418 potential run-time traps and exceptions that fold must preserve. */
14420 #define START_FOLD_INIT \
14421 int saved_signaling_nans = flag_signaling_nans;\
14422 int saved_trapping_math = flag_trapping_math;\
14423 int saved_rounding_math = flag_rounding_math;\
14424 int saved_trapv = flag_trapv;\
14425 int saved_folding_initializer = folding_initializer;\
14426 flag_signaling_nans = 0;\
14427 flag_trapping_math = 0;\
14428 flag_rounding_math = 0;\
14429 flag_trapv = 0;\
14430 folding_initializer = 1;
14432 #define END_FOLD_INIT \
14433 flag_signaling_nans = saved_signaling_nans;\
14434 flag_trapping_math = saved_trapping_math;\
14435 flag_rounding_math = saved_rounding_math;\
14436 flag_trapv = saved_trapv;\
14437 folding_initializer = saved_folding_initializer;
14439 tree
14440 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14441 tree type, tree op)
14443 tree result;
14444 START_FOLD_INIT;
14446 result = fold_build1_loc (loc, code, type, op);
14448 END_FOLD_INIT;
14449 return result;
14452 tree
14453 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14454 tree type, tree op0, tree op1)
14456 tree result;
14457 START_FOLD_INIT;
14459 result = fold_build2_loc (loc, code, type, op0, op1);
14461 END_FOLD_INIT;
14462 return result;
14465 tree
14466 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14467 int nargs, tree *argarray)
14469 tree result;
14470 START_FOLD_INIT;
14472 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14474 END_FOLD_INIT;
14475 return result;
14478 #undef START_FOLD_INIT
14479 #undef END_FOLD_INIT
14481 /* Determine if first argument is a multiple of second argument. Return 0 if
14482 it is not, or we cannot easily determined it to be.
14484 An example of the sort of thing we care about (at this point; this routine
14485 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14486 fold cases do now) is discovering that
14488 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14490 is a multiple of
14492 SAVE_EXPR (J * 8)
14494 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14496 This code also handles discovering that
14498 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14500 is a multiple of 8 so we don't have to worry about dealing with a
14501 possible remainder.
14503 Note that we *look* inside a SAVE_EXPR only to determine how it was
14504 calculated; it is not safe for fold to do much of anything else with the
14505 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14506 at run time. For example, the latter example above *cannot* be implemented
14507 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14508 evaluation time of the original SAVE_EXPR is not necessarily the same at
14509 the time the new expression is evaluated. The only optimization of this
14510 sort that would be valid is changing
14512 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14514 divided by 8 to
14516 SAVE_EXPR (I) * SAVE_EXPR (J)
14518 (where the same SAVE_EXPR (J) is used in the original and the
14519 transformed version). */
14522 multiple_of_p (tree type, const_tree top, const_tree bottom)
14524 if (operand_equal_p (top, bottom, 0))
14525 return 1;
14527 if (TREE_CODE (type) != INTEGER_TYPE)
14528 return 0;
14530 switch (TREE_CODE (top))
14532 case BIT_AND_EXPR:
14533 /* Bitwise and provides a power of two multiple. If the mask is
14534 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14535 if (!integer_pow2p (bottom))
14536 return 0;
14537 /* FALLTHRU */
14539 case MULT_EXPR:
14540 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14541 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14543 case PLUS_EXPR:
14544 case MINUS_EXPR:
14545 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14546 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14548 case LSHIFT_EXPR:
14549 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14551 tree op1, t1;
14553 op1 = TREE_OPERAND (top, 1);
14554 /* const_binop may not detect overflow correctly,
14555 so check for it explicitly here. */
14556 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
14557 && 0 != (t1 = fold_convert (type,
14558 const_binop (LSHIFT_EXPR,
14559 size_one_node,
14560 op1)))
14561 && !TREE_OVERFLOW (t1))
14562 return multiple_of_p (type, t1, bottom);
14564 return 0;
14566 case NOP_EXPR:
14567 /* Can't handle conversions from non-integral or wider integral type. */
14568 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14569 || (TYPE_PRECISION (type)
14570 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14571 return 0;
14573 /* .. fall through ... */
14575 case SAVE_EXPR:
14576 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14578 case COND_EXPR:
14579 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14580 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14582 case INTEGER_CST:
14583 if (TREE_CODE (bottom) != INTEGER_CST
14584 || integer_zerop (bottom)
14585 || (TYPE_UNSIGNED (type)
14586 && (tree_int_cst_sgn (top) < 0
14587 || tree_int_cst_sgn (bottom) < 0)))
14588 return 0;
14589 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14590 SIGNED);
14592 default:
14593 return 0;
14597 /* Return true if CODE or TYPE is known to be non-negative. */
14599 static bool
14600 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14602 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14603 && truth_value_p (code))
14604 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14605 have a signed:1 type (where the value is -1 and 0). */
14606 return true;
14607 return false;
14610 /* Return true if (CODE OP0) is known to be non-negative. If the return
14611 value is based on the assumption that signed overflow is undefined,
14612 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14613 *STRICT_OVERFLOW_P. */
14615 bool
14616 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14617 bool *strict_overflow_p)
14619 if (TYPE_UNSIGNED (type))
14620 return true;
14622 switch (code)
14624 case ABS_EXPR:
14625 /* We can't return 1 if flag_wrapv is set because
14626 ABS_EXPR<INT_MIN> = INT_MIN. */
14627 if (!ANY_INTEGRAL_TYPE_P (type))
14628 return true;
14629 if (TYPE_OVERFLOW_UNDEFINED (type))
14631 *strict_overflow_p = true;
14632 return true;
14634 break;
14636 case NON_LVALUE_EXPR:
14637 case FLOAT_EXPR:
14638 case FIX_TRUNC_EXPR:
14639 return tree_expr_nonnegative_warnv_p (op0,
14640 strict_overflow_p);
14642 CASE_CONVERT:
14644 tree inner_type = TREE_TYPE (op0);
14645 tree outer_type = type;
14647 if (TREE_CODE (outer_type) == REAL_TYPE)
14649 if (TREE_CODE (inner_type) == REAL_TYPE)
14650 return tree_expr_nonnegative_warnv_p (op0,
14651 strict_overflow_p);
14652 if (INTEGRAL_TYPE_P (inner_type))
14654 if (TYPE_UNSIGNED (inner_type))
14655 return true;
14656 return tree_expr_nonnegative_warnv_p (op0,
14657 strict_overflow_p);
14660 else if (INTEGRAL_TYPE_P (outer_type))
14662 if (TREE_CODE (inner_type) == REAL_TYPE)
14663 return tree_expr_nonnegative_warnv_p (op0,
14664 strict_overflow_p);
14665 if (INTEGRAL_TYPE_P (inner_type))
14666 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14667 && TYPE_UNSIGNED (inner_type);
14670 break;
14672 default:
14673 return tree_simple_nonnegative_warnv_p (code, type);
14676 /* We don't know sign of `t', so be conservative and return false. */
14677 return false;
14680 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14681 value is based on the assumption that signed overflow is undefined,
14682 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14683 *STRICT_OVERFLOW_P. */
14685 bool
14686 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14687 tree op1, bool *strict_overflow_p)
14689 if (TYPE_UNSIGNED (type))
14690 return true;
14692 switch (code)
14694 case POINTER_PLUS_EXPR:
14695 case PLUS_EXPR:
14696 if (FLOAT_TYPE_P (type))
14697 return (tree_expr_nonnegative_warnv_p (op0,
14698 strict_overflow_p)
14699 && tree_expr_nonnegative_warnv_p (op1,
14700 strict_overflow_p));
14702 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14703 both unsigned and at least 2 bits shorter than the result. */
14704 if (TREE_CODE (type) == INTEGER_TYPE
14705 && TREE_CODE (op0) == NOP_EXPR
14706 && TREE_CODE (op1) == NOP_EXPR)
14708 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14709 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14710 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14711 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14713 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14714 TYPE_PRECISION (inner2)) + 1;
14715 return prec < TYPE_PRECISION (type);
14718 break;
14720 case MULT_EXPR:
14721 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14723 /* x * x is always non-negative for floating point x
14724 or without overflow. */
14725 if (operand_equal_p (op0, op1, 0)
14726 || (tree_expr_nonnegative_warnv_p (op0, strict_overflow_p)
14727 && tree_expr_nonnegative_warnv_p (op1, strict_overflow_p)))
14729 if (ANY_INTEGRAL_TYPE_P (type)
14730 && TYPE_OVERFLOW_UNDEFINED (type))
14731 *strict_overflow_p = true;
14732 return true;
14736 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14737 both unsigned and their total bits is shorter than the result. */
14738 if (TREE_CODE (type) == INTEGER_TYPE
14739 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14740 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14742 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14743 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14744 : TREE_TYPE (op0);
14745 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14746 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14747 : TREE_TYPE (op1);
14749 bool unsigned0 = TYPE_UNSIGNED (inner0);
14750 bool unsigned1 = TYPE_UNSIGNED (inner1);
14752 if (TREE_CODE (op0) == INTEGER_CST)
14753 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14755 if (TREE_CODE (op1) == INTEGER_CST)
14756 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14758 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14759 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14761 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14762 ? tree_int_cst_min_precision (op0, UNSIGNED)
14763 : TYPE_PRECISION (inner0);
14765 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14766 ? tree_int_cst_min_precision (op1, UNSIGNED)
14767 : TYPE_PRECISION (inner1);
14769 return precision0 + precision1 < TYPE_PRECISION (type);
14772 return false;
14774 case BIT_AND_EXPR:
14775 case MAX_EXPR:
14776 return (tree_expr_nonnegative_warnv_p (op0,
14777 strict_overflow_p)
14778 || tree_expr_nonnegative_warnv_p (op1,
14779 strict_overflow_p));
14781 case BIT_IOR_EXPR:
14782 case BIT_XOR_EXPR:
14783 case MIN_EXPR:
14784 case RDIV_EXPR:
14785 case TRUNC_DIV_EXPR:
14786 case CEIL_DIV_EXPR:
14787 case FLOOR_DIV_EXPR:
14788 case ROUND_DIV_EXPR:
14789 return (tree_expr_nonnegative_warnv_p (op0,
14790 strict_overflow_p)
14791 && tree_expr_nonnegative_warnv_p (op1,
14792 strict_overflow_p));
14794 case TRUNC_MOD_EXPR:
14795 case CEIL_MOD_EXPR:
14796 case FLOOR_MOD_EXPR:
14797 case ROUND_MOD_EXPR:
14798 return tree_expr_nonnegative_warnv_p (op0,
14799 strict_overflow_p);
14800 default:
14801 return tree_simple_nonnegative_warnv_p (code, type);
14804 /* We don't know sign of `t', so be conservative and return false. */
14805 return false;
14808 /* Return true if T is known to be non-negative. If the return
14809 value is based on the assumption that signed overflow is undefined,
14810 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14811 *STRICT_OVERFLOW_P. */
14813 bool
14814 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14816 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14817 return true;
14819 switch (TREE_CODE (t))
14821 case INTEGER_CST:
14822 return tree_int_cst_sgn (t) >= 0;
14824 case REAL_CST:
14825 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14827 case FIXED_CST:
14828 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14830 case COND_EXPR:
14831 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14832 strict_overflow_p)
14833 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14834 strict_overflow_p));
14835 default:
14836 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14837 TREE_TYPE (t));
14839 /* We don't know sign of `t', so be conservative and return false. */
14840 return false;
14843 /* Return true if T is known to be non-negative. If the return
14844 value is based on the assumption that signed overflow is undefined,
14845 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14846 *STRICT_OVERFLOW_P. */
14848 bool
14849 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14850 tree arg0, tree arg1, bool *strict_overflow_p)
14852 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14853 switch (DECL_FUNCTION_CODE (fndecl))
14855 CASE_FLT_FN (BUILT_IN_ACOS):
14856 CASE_FLT_FN (BUILT_IN_ACOSH):
14857 CASE_FLT_FN (BUILT_IN_CABS):
14858 CASE_FLT_FN (BUILT_IN_COSH):
14859 CASE_FLT_FN (BUILT_IN_ERFC):
14860 CASE_FLT_FN (BUILT_IN_EXP):
14861 CASE_FLT_FN (BUILT_IN_EXP10):
14862 CASE_FLT_FN (BUILT_IN_EXP2):
14863 CASE_FLT_FN (BUILT_IN_FABS):
14864 CASE_FLT_FN (BUILT_IN_FDIM):
14865 CASE_FLT_FN (BUILT_IN_HYPOT):
14866 CASE_FLT_FN (BUILT_IN_POW10):
14867 CASE_INT_FN (BUILT_IN_FFS):
14868 CASE_INT_FN (BUILT_IN_PARITY):
14869 CASE_INT_FN (BUILT_IN_POPCOUNT):
14870 CASE_INT_FN (BUILT_IN_CLZ):
14871 CASE_INT_FN (BUILT_IN_CLRSB):
14872 case BUILT_IN_BSWAP32:
14873 case BUILT_IN_BSWAP64:
14874 /* Always true. */
14875 return true;
14877 CASE_FLT_FN (BUILT_IN_SQRT):
14878 /* sqrt(-0.0) is -0.0. */
14879 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
14880 return true;
14881 return tree_expr_nonnegative_warnv_p (arg0,
14882 strict_overflow_p);
14884 CASE_FLT_FN (BUILT_IN_ASINH):
14885 CASE_FLT_FN (BUILT_IN_ATAN):
14886 CASE_FLT_FN (BUILT_IN_ATANH):
14887 CASE_FLT_FN (BUILT_IN_CBRT):
14888 CASE_FLT_FN (BUILT_IN_CEIL):
14889 CASE_FLT_FN (BUILT_IN_ERF):
14890 CASE_FLT_FN (BUILT_IN_EXPM1):
14891 CASE_FLT_FN (BUILT_IN_FLOOR):
14892 CASE_FLT_FN (BUILT_IN_FMOD):
14893 CASE_FLT_FN (BUILT_IN_FREXP):
14894 CASE_FLT_FN (BUILT_IN_ICEIL):
14895 CASE_FLT_FN (BUILT_IN_IFLOOR):
14896 CASE_FLT_FN (BUILT_IN_IRINT):
14897 CASE_FLT_FN (BUILT_IN_IROUND):
14898 CASE_FLT_FN (BUILT_IN_LCEIL):
14899 CASE_FLT_FN (BUILT_IN_LDEXP):
14900 CASE_FLT_FN (BUILT_IN_LFLOOR):
14901 CASE_FLT_FN (BUILT_IN_LLCEIL):
14902 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14903 CASE_FLT_FN (BUILT_IN_LLRINT):
14904 CASE_FLT_FN (BUILT_IN_LLROUND):
14905 CASE_FLT_FN (BUILT_IN_LRINT):
14906 CASE_FLT_FN (BUILT_IN_LROUND):
14907 CASE_FLT_FN (BUILT_IN_MODF):
14908 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14909 CASE_FLT_FN (BUILT_IN_RINT):
14910 CASE_FLT_FN (BUILT_IN_ROUND):
14911 CASE_FLT_FN (BUILT_IN_SCALB):
14912 CASE_FLT_FN (BUILT_IN_SCALBLN):
14913 CASE_FLT_FN (BUILT_IN_SCALBN):
14914 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14915 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14916 CASE_FLT_FN (BUILT_IN_SINH):
14917 CASE_FLT_FN (BUILT_IN_TANH):
14918 CASE_FLT_FN (BUILT_IN_TRUNC):
14919 /* True if the 1st argument is nonnegative. */
14920 return tree_expr_nonnegative_warnv_p (arg0,
14921 strict_overflow_p);
14923 CASE_FLT_FN (BUILT_IN_FMAX):
14924 /* True if the 1st OR 2nd arguments are nonnegative. */
14925 return (tree_expr_nonnegative_warnv_p (arg0,
14926 strict_overflow_p)
14927 || (tree_expr_nonnegative_warnv_p (arg1,
14928 strict_overflow_p)));
14930 CASE_FLT_FN (BUILT_IN_FMIN):
14931 /* True if the 1st AND 2nd arguments are nonnegative. */
14932 return (tree_expr_nonnegative_warnv_p (arg0,
14933 strict_overflow_p)
14934 && (tree_expr_nonnegative_warnv_p (arg1,
14935 strict_overflow_p)));
14937 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14938 /* True if the 2nd argument is nonnegative. */
14939 return tree_expr_nonnegative_warnv_p (arg1,
14940 strict_overflow_p);
14942 CASE_FLT_FN (BUILT_IN_POWI):
14943 /* True if the 1st argument is nonnegative or the second
14944 argument is an even integer. */
14945 if (TREE_CODE (arg1) == INTEGER_CST
14946 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14947 return true;
14948 return tree_expr_nonnegative_warnv_p (arg0,
14949 strict_overflow_p);
14951 CASE_FLT_FN (BUILT_IN_POW):
14952 /* True if the 1st argument is nonnegative or the second
14953 argument is an even integer valued real. */
14954 if (TREE_CODE (arg1) == REAL_CST)
14956 REAL_VALUE_TYPE c;
14957 HOST_WIDE_INT n;
14959 c = TREE_REAL_CST (arg1);
14960 n = real_to_integer (&c);
14961 if ((n & 1) == 0)
14963 REAL_VALUE_TYPE cint;
14964 real_from_integer (&cint, VOIDmode, n, SIGNED);
14965 if (real_identical (&c, &cint))
14966 return true;
14969 return tree_expr_nonnegative_warnv_p (arg0,
14970 strict_overflow_p);
14972 default:
14973 break;
14975 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14976 type);
14979 /* Return true if T is known to be non-negative. If the return
14980 value is based on the assumption that signed overflow is undefined,
14981 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14982 *STRICT_OVERFLOW_P. */
14984 static bool
14985 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14987 enum tree_code code = TREE_CODE (t);
14988 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14989 return true;
14991 switch (code)
14993 case TARGET_EXPR:
14995 tree temp = TARGET_EXPR_SLOT (t);
14996 t = TARGET_EXPR_INITIAL (t);
14998 /* If the initializer is non-void, then it's a normal expression
14999 that will be assigned to the slot. */
15000 if (!VOID_TYPE_P (t))
15001 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
15003 /* Otherwise, the initializer sets the slot in some way. One common
15004 way is an assignment statement at the end of the initializer. */
15005 while (1)
15007 if (TREE_CODE (t) == BIND_EXPR)
15008 t = expr_last (BIND_EXPR_BODY (t));
15009 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15010 || TREE_CODE (t) == TRY_CATCH_EXPR)
15011 t = expr_last (TREE_OPERAND (t, 0));
15012 else if (TREE_CODE (t) == STATEMENT_LIST)
15013 t = expr_last (t);
15014 else
15015 break;
15017 if (TREE_CODE (t) == MODIFY_EXPR
15018 && TREE_OPERAND (t, 0) == temp)
15019 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15020 strict_overflow_p);
15022 return false;
15025 case CALL_EXPR:
15027 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15028 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15030 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15031 get_callee_fndecl (t),
15032 arg0,
15033 arg1,
15034 strict_overflow_p);
15036 case COMPOUND_EXPR:
15037 case MODIFY_EXPR:
15038 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15039 strict_overflow_p);
15040 case BIND_EXPR:
15041 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
15042 strict_overflow_p);
15043 case SAVE_EXPR:
15044 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
15045 strict_overflow_p);
15047 default:
15048 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15049 TREE_TYPE (t));
15052 /* We don't know sign of `t', so be conservative and return false. */
15053 return false;
15056 /* Return true if T is known to be non-negative. If the return
15057 value is based on the assumption that signed overflow is undefined,
15058 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15059 *STRICT_OVERFLOW_P. */
15061 bool
15062 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15064 enum tree_code code;
15065 if (t == error_mark_node)
15066 return false;
15068 code = TREE_CODE (t);
15069 switch (TREE_CODE_CLASS (code))
15071 case tcc_binary:
15072 case tcc_comparison:
15073 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15074 TREE_TYPE (t),
15075 TREE_OPERAND (t, 0),
15076 TREE_OPERAND (t, 1),
15077 strict_overflow_p);
15079 case tcc_unary:
15080 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15081 TREE_TYPE (t),
15082 TREE_OPERAND (t, 0),
15083 strict_overflow_p);
15085 case tcc_constant:
15086 case tcc_declaration:
15087 case tcc_reference:
15088 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15090 default:
15091 break;
15094 switch (code)
15096 case TRUTH_AND_EXPR:
15097 case TRUTH_OR_EXPR:
15098 case TRUTH_XOR_EXPR:
15099 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15100 TREE_TYPE (t),
15101 TREE_OPERAND (t, 0),
15102 TREE_OPERAND (t, 1),
15103 strict_overflow_p);
15104 case TRUTH_NOT_EXPR:
15105 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15106 TREE_TYPE (t),
15107 TREE_OPERAND (t, 0),
15108 strict_overflow_p);
15110 case COND_EXPR:
15111 case CONSTRUCTOR:
15112 case OBJ_TYPE_REF:
15113 case ASSERT_EXPR:
15114 case ADDR_EXPR:
15115 case WITH_SIZE_EXPR:
15116 case SSA_NAME:
15117 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15119 default:
15120 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
15124 /* Return true if `t' is known to be non-negative. Handle warnings
15125 about undefined signed overflow. */
15127 bool
15128 tree_expr_nonnegative_p (tree t)
15130 bool ret, strict_overflow_p;
15132 strict_overflow_p = false;
15133 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15134 if (strict_overflow_p)
15135 fold_overflow_warning (("assuming signed overflow does not occur when "
15136 "determining that expression is always "
15137 "non-negative"),
15138 WARN_STRICT_OVERFLOW_MISC);
15139 return ret;
15143 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15144 For floating point we further ensure that T is not denormal.
15145 Similar logic is present in nonzero_address in rtlanal.h.
15147 If the return value is based on the assumption that signed overflow
15148 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15149 change *STRICT_OVERFLOW_P. */
15151 bool
15152 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15153 bool *strict_overflow_p)
15155 switch (code)
15157 case ABS_EXPR:
15158 return tree_expr_nonzero_warnv_p (op0,
15159 strict_overflow_p);
15161 case NOP_EXPR:
15163 tree inner_type = TREE_TYPE (op0);
15164 tree outer_type = type;
15166 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15167 && tree_expr_nonzero_warnv_p (op0,
15168 strict_overflow_p));
15170 break;
15172 case NON_LVALUE_EXPR:
15173 return tree_expr_nonzero_warnv_p (op0,
15174 strict_overflow_p);
15176 default:
15177 break;
15180 return false;
15183 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15184 For floating point we further ensure that T is not denormal.
15185 Similar logic is present in nonzero_address in rtlanal.h.
15187 If the return value is based on the assumption that signed overflow
15188 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15189 change *STRICT_OVERFLOW_P. */
15191 bool
15192 tree_binary_nonzero_warnv_p (enum tree_code code,
15193 tree type,
15194 tree op0,
15195 tree op1, bool *strict_overflow_p)
15197 bool sub_strict_overflow_p;
15198 switch (code)
15200 case POINTER_PLUS_EXPR:
15201 case PLUS_EXPR:
15202 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15204 /* With the presence of negative values it is hard
15205 to say something. */
15206 sub_strict_overflow_p = false;
15207 if (!tree_expr_nonnegative_warnv_p (op0,
15208 &sub_strict_overflow_p)
15209 || !tree_expr_nonnegative_warnv_p (op1,
15210 &sub_strict_overflow_p))
15211 return false;
15212 /* One of operands must be positive and the other non-negative. */
15213 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15214 overflows, on a twos-complement machine the sum of two
15215 nonnegative numbers can never be zero. */
15216 return (tree_expr_nonzero_warnv_p (op0,
15217 strict_overflow_p)
15218 || tree_expr_nonzero_warnv_p (op1,
15219 strict_overflow_p));
15221 break;
15223 case MULT_EXPR:
15224 if (TYPE_OVERFLOW_UNDEFINED (type))
15226 if (tree_expr_nonzero_warnv_p (op0,
15227 strict_overflow_p)
15228 && tree_expr_nonzero_warnv_p (op1,
15229 strict_overflow_p))
15231 *strict_overflow_p = true;
15232 return true;
15235 break;
15237 case MIN_EXPR:
15238 sub_strict_overflow_p = false;
15239 if (tree_expr_nonzero_warnv_p (op0,
15240 &sub_strict_overflow_p)
15241 && tree_expr_nonzero_warnv_p (op1,
15242 &sub_strict_overflow_p))
15244 if (sub_strict_overflow_p)
15245 *strict_overflow_p = true;
15247 break;
15249 case MAX_EXPR:
15250 sub_strict_overflow_p = false;
15251 if (tree_expr_nonzero_warnv_p (op0,
15252 &sub_strict_overflow_p))
15254 if (sub_strict_overflow_p)
15255 *strict_overflow_p = true;
15257 /* When both operands are nonzero, then MAX must be too. */
15258 if (tree_expr_nonzero_warnv_p (op1,
15259 strict_overflow_p))
15260 return true;
15262 /* MAX where operand 0 is positive is positive. */
15263 return tree_expr_nonnegative_warnv_p (op0,
15264 strict_overflow_p);
15266 /* MAX where operand 1 is positive is positive. */
15267 else if (tree_expr_nonzero_warnv_p (op1,
15268 &sub_strict_overflow_p)
15269 && tree_expr_nonnegative_warnv_p (op1,
15270 &sub_strict_overflow_p))
15272 if (sub_strict_overflow_p)
15273 *strict_overflow_p = true;
15274 return true;
15276 break;
15278 case BIT_IOR_EXPR:
15279 return (tree_expr_nonzero_warnv_p (op1,
15280 strict_overflow_p)
15281 || tree_expr_nonzero_warnv_p (op0,
15282 strict_overflow_p));
15284 default:
15285 break;
15288 return false;
15291 /* Return true when T is an address and is known to be nonzero.
15292 For floating point we further ensure that T is not denormal.
15293 Similar logic is present in nonzero_address in rtlanal.h.
15295 If the return value is based on the assumption that signed overflow
15296 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15297 change *STRICT_OVERFLOW_P. */
15299 bool
15300 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15302 bool sub_strict_overflow_p;
15303 switch (TREE_CODE (t))
15305 case INTEGER_CST:
15306 return !integer_zerop (t);
15308 case ADDR_EXPR:
15310 tree base = TREE_OPERAND (t, 0);
15312 if (!DECL_P (base))
15313 base = get_base_address (base);
15315 if (!base)
15316 return false;
15318 /* For objects in symbol table check if we know they are non-zero.
15319 Don't do anything for variables and functions before symtab is built;
15320 it is quite possible that they will be declared weak later. */
15321 if (DECL_P (base) && decl_in_symtab_p (base))
15323 struct symtab_node *symbol;
15325 symbol = symtab_node::get_create (base);
15326 if (symbol)
15327 return symbol->nonzero_address ();
15328 else
15329 return false;
15332 /* Function local objects are never NULL. */
15333 if (DECL_P (base)
15334 && (DECL_CONTEXT (base)
15335 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
15336 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
15337 return true;
15339 /* Constants are never weak. */
15340 if (CONSTANT_CLASS_P (base))
15341 return true;
15343 return false;
15346 case COND_EXPR:
15347 sub_strict_overflow_p = false;
15348 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15349 &sub_strict_overflow_p)
15350 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15351 &sub_strict_overflow_p))
15353 if (sub_strict_overflow_p)
15354 *strict_overflow_p = true;
15355 return true;
15357 break;
15359 default:
15360 break;
15362 return false;
15365 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15366 attempt to fold the expression to a constant without modifying TYPE,
15367 OP0 or OP1.
15369 If the expression could be simplified to a constant, then return
15370 the constant. If the expression would not be simplified to a
15371 constant, then return NULL_TREE. */
15373 tree
15374 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15376 tree tem = fold_binary (code, type, op0, op1);
15377 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15380 /* Given the components of a unary expression CODE, TYPE and OP0,
15381 attempt to fold the expression to a constant without modifying
15382 TYPE or OP0.
15384 If the expression could be simplified to a constant, then return
15385 the constant. If the expression would not be simplified to a
15386 constant, then return NULL_TREE. */
15388 tree
15389 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15391 tree tem = fold_unary (code, type, op0);
15392 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15395 /* If EXP represents referencing an element in a constant string
15396 (either via pointer arithmetic or array indexing), return the
15397 tree representing the value accessed, otherwise return NULL. */
15399 tree
15400 fold_read_from_constant_string (tree exp)
15402 if ((TREE_CODE (exp) == INDIRECT_REF
15403 || TREE_CODE (exp) == ARRAY_REF)
15404 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15406 tree exp1 = TREE_OPERAND (exp, 0);
15407 tree index;
15408 tree string;
15409 location_t loc = EXPR_LOCATION (exp);
15411 if (TREE_CODE (exp) == INDIRECT_REF)
15412 string = string_constant (exp1, &index);
15413 else
15415 tree low_bound = array_ref_low_bound (exp);
15416 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15418 /* Optimize the special-case of a zero lower bound.
15420 We convert the low_bound to sizetype to avoid some problems
15421 with constant folding. (E.g. suppose the lower bound is 1,
15422 and its mode is QI. Without the conversion,l (ARRAY
15423 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15424 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15425 if (! integer_zerop (low_bound))
15426 index = size_diffop_loc (loc, index,
15427 fold_convert_loc (loc, sizetype, low_bound));
15429 string = exp1;
15432 if (string
15433 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15434 && TREE_CODE (string) == STRING_CST
15435 && TREE_CODE (index) == INTEGER_CST
15436 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15437 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15438 == MODE_INT)
15439 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15440 return build_int_cst_type (TREE_TYPE (exp),
15441 (TREE_STRING_POINTER (string)
15442 [TREE_INT_CST_LOW (index)]));
15444 return NULL;
15447 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15448 an integer constant, real, or fixed-point constant.
15450 TYPE is the type of the result. */
15452 static tree
15453 fold_negate_const (tree arg0, tree type)
15455 tree t = NULL_TREE;
15457 switch (TREE_CODE (arg0))
15459 case INTEGER_CST:
15461 bool overflow;
15462 wide_int val = wi::neg (arg0, &overflow);
15463 t = force_fit_type (type, val, 1,
15464 (overflow | TREE_OVERFLOW (arg0))
15465 && !TYPE_UNSIGNED (type));
15466 break;
15469 case REAL_CST:
15470 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15471 break;
15473 case FIXED_CST:
15475 FIXED_VALUE_TYPE f;
15476 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15477 &(TREE_FIXED_CST (arg0)), NULL,
15478 TYPE_SATURATING (type));
15479 t = build_fixed (type, f);
15480 /* Propagate overflow flags. */
15481 if (overflow_p | TREE_OVERFLOW (arg0))
15482 TREE_OVERFLOW (t) = 1;
15483 break;
15486 default:
15487 gcc_unreachable ();
15490 return t;
15493 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15494 an integer constant or real constant.
15496 TYPE is the type of the result. */
15498 tree
15499 fold_abs_const (tree arg0, tree type)
15501 tree t = NULL_TREE;
15503 switch (TREE_CODE (arg0))
15505 case INTEGER_CST:
15507 /* If the value is unsigned or non-negative, then the absolute value
15508 is the same as the ordinary value. */
15509 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
15510 t = arg0;
15512 /* If the value is negative, then the absolute value is
15513 its negation. */
15514 else
15516 bool overflow;
15517 wide_int val = wi::neg (arg0, &overflow);
15518 t = force_fit_type (type, val, -1,
15519 overflow | TREE_OVERFLOW (arg0));
15522 break;
15524 case REAL_CST:
15525 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15526 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15527 else
15528 t = arg0;
15529 break;
15531 default:
15532 gcc_unreachable ();
15535 return t;
15538 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15539 constant. TYPE is the type of the result. */
15541 static tree
15542 fold_not_const (const_tree arg0, tree type)
15544 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15546 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
15549 /* Given CODE, a relational operator, the target type, TYPE and two
15550 constant operands OP0 and OP1, return the result of the
15551 relational operation. If the result is not a compile time
15552 constant, then return NULL_TREE. */
15554 static tree
15555 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15557 int result, invert;
15559 /* From here on, the only cases we handle are when the result is
15560 known to be a constant. */
15562 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15564 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15565 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15567 /* Handle the cases where either operand is a NaN. */
15568 if (real_isnan (c0) || real_isnan (c1))
15570 switch (code)
15572 case EQ_EXPR:
15573 case ORDERED_EXPR:
15574 result = 0;
15575 break;
15577 case NE_EXPR:
15578 case UNORDERED_EXPR:
15579 case UNLT_EXPR:
15580 case UNLE_EXPR:
15581 case UNGT_EXPR:
15582 case UNGE_EXPR:
15583 case UNEQ_EXPR:
15584 result = 1;
15585 break;
15587 case LT_EXPR:
15588 case LE_EXPR:
15589 case GT_EXPR:
15590 case GE_EXPR:
15591 case LTGT_EXPR:
15592 if (flag_trapping_math)
15593 return NULL_TREE;
15594 result = 0;
15595 break;
15597 default:
15598 gcc_unreachable ();
15601 return constant_boolean_node (result, type);
15604 return constant_boolean_node (real_compare (code, c0, c1), type);
15607 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15609 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15610 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15611 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15614 /* Handle equality/inequality of complex constants. */
15615 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15617 tree rcond = fold_relational_const (code, type,
15618 TREE_REALPART (op0),
15619 TREE_REALPART (op1));
15620 tree icond = fold_relational_const (code, type,
15621 TREE_IMAGPART (op0),
15622 TREE_IMAGPART (op1));
15623 if (code == EQ_EXPR)
15624 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15625 else if (code == NE_EXPR)
15626 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15627 else
15628 return NULL_TREE;
15631 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15633 unsigned count = VECTOR_CST_NELTS (op0);
15634 tree *elts = XALLOCAVEC (tree, count);
15635 gcc_assert (VECTOR_CST_NELTS (op1) == count
15636 && TYPE_VECTOR_SUBPARTS (type) == count);
15638 for (unsigned i = 0; i < count; i++)
15640 tree elem_type = TREE_TYPE (type);
15641 tree elem0 = VECTOR_CST_ELT (op0, i);
15642 tree elem1 = VECTOR_CST_ELT (op1, i);
15644 tree tem = fold_relational_const (code, elem_type,
15645 elem0, elem1);
15647 if (tem == NULL_TREE)
15648 return NULL_TREE;
15650 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
15653 return build_vector (type, elts);
15656 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15658 To compute GT, swap the arguments and do LT.
15659 To compute GE, do LT and invert the result.
15660 To compute LE, swap the arguments, do LT and invert the result.
15661 To compute NE, do EQ and invert the result.
15663 Therefore, the code below must handle only EQ and LT. */
15665 if (code == LE_EXPR || code == GT_EXPR)
15667 std::swap (op0, op1);
15668 code = swap_tree_comparison (code);
15671 /* Note that it is safe to invert for real values here because we
15672 have already handled the one case that it matters. */
15674 invert = 0;
15675 if (code == NE_EXPR || code == GE_EXPR)
15677 invert = 1;
15678 code = invert_tree_comparison (code, false);
15681 /* Compute a result for LT or EQ if args permit;
15682 Otherwise return T. */
15683 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15685 if (code == EQ_EXPR)
15686 result = tree_int_cst_equal (op0, op1);
15687 else
15688 result = tree_int_cst_lt (op0, op1);
15690 else
15691 return NULL_TREE;
15693 if (invert)
15694 result ^= 1;
15695 return constant_boolean_node (result, type);
15698 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15699 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15700 itself. */
15702 tree
15703 fold_build_cleanup_point_expr (tree type, tree expr)
15705 /* If the expression does not have side effects then we don't have to wrap
15706 it with a cleanup point expression. */
15707 if (!TREE_SIDE_EFFECTS (expr))
15708 return expr;
15710 /* If the expression is a return, check to see if the expression inside the
15711 return has no side effects or the right hand side of the modify expression
15712 inside the return. If either don't have side effects set we don't need to
15713 wrap the expression in a cleanup point expression. Note we don't check the
15714 left hand side of the modify because it should always be a return decl. */
15715 if (TREE_CODE (expr) == RETURN_EXPR)
15717 tree op = TREE_OPERAND (expr, 0);
15718 if (!op || !TREE_SIDE_EFFECTS (op))
15719 return expr;
15720 op = TREE_OPERAND (op, 1);
15721 if (!TREE_SIDE_EFFECTS (op))
15722 return expr;
15725 return build1 (CLEANUP_POINT_EXPR, type, expr);
15728 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15729 of an indirection through OP0, or NULL_TREE if no simplification is
15730 possible. */
15732 tree
15733 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15735 tree sub = op0;
15736 tree subtype;
15738 STRIP_NOPS (sub);
15739 subtype = TREE_TYPE (sub);
15740 if (!POINTER_TYPE_P (subtype))
15741 return NULL_TREE;
15743 if (TREE_CODE (sub) == ADDR_EXPR)
15745 tree op = TREE_OPERAND (sub, 0);
15746 tree optype = TREE_TYPE (op);
15747 /* *&CONST_DECL -> to the value of the const decl. */
15748 if (TREE_CODE (op) == CONST_DECL)
15749 return DECL_INITIAL (op);
15750 /* *&p => p; make sure to handle *&"str"[cst] here. */
15751 if (type == optype)
15753 tree fop = fold_read_from_constant_string (op);
15754 if (fop)
15755 return fop;
15756 else
15757 return op;
15759 /* *(foo *)&fooarray => fooarray[0] */
15760 else if (TREE_CODE (optype) == ARRAY_TYPE
15761 && type == TREE_TYPE (optype)
15762 && (!in_gimple_form
15763 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15765 tree type_domain = TYPE_DOMAIN (optype);
15766 tree min_val = size_zero_node;
15767 if (type_domain && TYPE_MIN_VALUE (type_domain))
15768 min_val = TYPE_MIN_VALUE (type_domain);
15769 if (in_gimple_form
15770 && TREE_CODE (min_val) != INTEGER_CST)
15771 return NULL_TREE;
15772 return build4_loc (loc, ARRAY_REF, type, op, min_val,
15773 NULL_TREE, NULL_TREE);
15775 /* *(foo *)&complexfoo => __real__ complexfoo */
15776 else if (TREE_CODE (optype) == COMPLEX_TYPE
15777 && type == TREE_TYPE (optype))
15778 return fold_build1_loc (loc, REALPART_EXPR, type, op);
15779 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15780 else if (TREE_CODE (optype) == VECTOR_TYPE
15781 && type == TREE_TYPE (optype))
15783 tree part_width = TYPE_SIZE (type);
15784 tree index = bitsize_int (0);
15785 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15789 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15790 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15792 tree op00 = TREE_OPERAND (sub, 0);
15793 tree op01 = TREE_OPERAND (sub, 1);
15795 STRIP_NOPS (op00);
15796 if (TREE_CODE (op00) == ADDR_EXPR)
15798 tree op00type;
15799 op00 = TREE_OPERAND (op00, 0);
15800 op00type = TREE_TYPE (op00);
15802 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15803 if (TREE_CODE (op00type) == VECTOR_TYPE
15804 && type == TREE_TYPE (op00type))
15806 HOST_WIDE_INT offset = tree_to_shwi (op01);
15807 tree part_width = TYPE_SIZE (type);
15808 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
15809 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15810 tree index = bitsize_int (indexi);
15812 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
15813 return fold_build3_loc (loc,
15814 BIT_FIELD_REF, type, op00,
15815 part_width, index);
15818 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15819 else if (TREE_CODE (op00type) == COMPLEX_TYPE
15820 && type == TREE_TYPE (op00type))
15822 tree size = TYPE_SIZE_UNIT (type);
15823 if (tree_int_cst_equal (size, op01))
15824 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15826 /* ((foo *)&fooarray)[1] => fooarray[1] */
15827 else if (TREE_CODE (op00type) == ARRAY_TYPE
15828 && type == TREE_TYPE (op00type))
15830 tree type_domain = TYPE_DOMAIN (op00type);
15831 tree min_val = size_zero_node;
15832 if (type_domain && TYPE_MIN_VALUE (type_domain))
15833 min_val = TYPE_MIN_VALUE (type_domain);
15834 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
15835 TYPE_SIZE_UNIT (type));
15836 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
15837 return build4_loc (loc, ARRAY_REF, type, op00, op01,
15838 NULL_TREE, NULL_TREE);
15843 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15844 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15845 && type == TREE_TYPE (TREE_TYPE (subtype))
15846 && (!in_gimple_form
15847 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
15849 tree type_domain;
15850 tree min_val = size_zero_node;
15851 sub = build_fold_indirect_ref_loc (loc, sub);
15852 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15853 if (type_domain && TYPE_MIN_VALUE (type_domain))
15854 min_val = TYPE_MIN_VALUE (type_domain);
15855 if (in_gimple_form
15856 && TREE_CODE (min_val) != INTEGER_CST)
15857 return NULL_TREE;
15858 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
15859 NULL_TREE);
15862 return NULL_TREE;
15865 /* Builds an expression for an indirection through T, simplifying some
15866 cases. */
15868 tree
15869 build_fold_indirect_ref_loc (location_t loc, tree t)
15871 tree type = TREE_TYPE (TREE_TYPE (t));
15872 tree sub = fold_indirect_ref_1 (loc, type, t);
15874 if (sub)
15875 return sub;
15877 return build1_loc (loc, INDIRECT_REF, type, t);
15880 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15882 tree
15883 fold_indirect_ref_loc (location_t loc, tree t)
15885 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15887 if (sub)
15888 return sub;
15889 else
15890 return t;
15893 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15894 whose result is ignored. The type of the returned tree need not be
15895 the same as the original expression. */
15897 tree
15898 fold_ignored_result (tree t)
15900 if (!TREE_SIDE_EFFECTS (t))
15901 return integer_zero_node;
15903 for (;;)
15904 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15906 case tcc_unary:
15907 t = TREE_OPERAND (t, 0);
15908 break;
15910 case tcc_binary:
15911 case tcc_comparison:
15912 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15913 t = TREE_OPERAND (t, 0);
15914 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15915 t = TREE_OPERAND (t, 1);
15916 else
15917 return t;
15918 break;
15920 case tcc_expression:
15921 switch (TREE_CODE (t))
15923 case COMPOUND_EXPR:
15924 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15925 return t;
15926 t = TREE_OPERAND (t, 0);
15927 break;
15929 case COND_EXPR:
15930 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15931 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15932 return t;
15933 t = TREE_OPERAND (t, 0);
15934 break;
15936 default:
15937 return t;
15939 break;
15941 default:
15942 return t;
15946 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
15948 tree
15949 round_up_loc (location_t loc, tree value, unsigned int divisor)
15951 tree div = NULL_TREE;
15953 if (divisor == 1)
15954 return value;
15956 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15957 have to do anything. Only do this when we are not given a const,
15958 because in that case, this check is more expensive than just
15959 doing it. */
15960 if (TREE_CODE (value) != INTEGER_CST)
15962 div = build_int_cst (TREE_TYPE (value), divisor);
15964 if (multiple_of_p (TREE_TYPE (value), value, div))
15965 return value;
15968 /* If divisor is a power of two, simplify this to bit manipulation. */
15969 if (divisor == (divisor & -divisor))
15971 if (TREE_CODE (value) == INTEGER_CST)
15973 wide_int val = value;
15974 bool overflow_p;
15976 if ((val & (divisor - 1)) == 0)
15977 return value;
15979 overflow_p = TREE_OVERFLOW (value);
15980 val += divisor - 1;
15981 val &= - (int) divisor;
15982 if (val == 0)
15983 overflow_p = true;
15985 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
15987 else
15989 tree t;
15991 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15992 value = size_binop_loc (loc, PLUS_EXPR, value, t);
15993 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
15994 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15997 else
15999 if (!div)
16000 div = build_int_cst (TREE_TYPE (value), divisor);
16001 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16002 value = size_binop_loc (loc, MULT_EXPR, value, div);
16005 return value;
16008 /* Likewise, but round down. */
16010 tree
16011 round_down_loc (location_t loc, tree value, int divisor)
16013 tree div = NULL_TREE;
16015 gcc_assert (divisor > 0);
16016 if (divisor == 1)
16017 return value;
16019 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16020 have to do anything. Only do this when we are not given a const,
16021 because in that case, this check is more expensive than just
16022 doing it. */
16023 if (TREE_CODE (value) != INTEGER_CST)
16025 div = build_int_cst (TREE_TYPE (value), divisor);
16027 if (multiple_of_p (TREE_TYPE (value), value, div))
16028 return value;
16031 /* If divisor is a power of two, simplify this to bit manipulation. */
16032 if (divisor == (divisor & -divisor))
16034 tree t;
16036 t = build_int_cst (TREE_TYPE (value), -divisor);
16037 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16039 else
16041 if (!div)
16042 div = build_int_cst (TREE_TYPE (value), divisor);
16043 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16044 value = size_binop_loc (loc, MULT_EXPR, value, div);
16047 return value;
16050 /* Returns the pointer to the base of the object addressed by EXP and
16051 extracts the information about the offset of the access, storing it
16052 to PBITPOS and POFFSET. */
16054 static tree
16055 split_address_to_core_and_offset (tree exp,
16056 HOST_WIDE_INT *pbitpos, tree *poffset)
16058 tree core;
16059 machine_mode mode;
16060 int unsignedp, volatilep;
16061 HOST_WIDE_INT bitsize;
16062 location_t loc = EXPR_LOCATION (exp);
16064 if (TREE_CODE (exp) == ADDR_EXPR)
16066 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16067 poffset, &mode, &unsignedp, &volatilep,
16068 false);
16069 core = build_fold_addr_expr_loc (loc, core);
16071 else
16073 core = exp;
16074 *pbitpos = 0;
16075 *poffset = NULL_TREE;
16078 return core;
16081 /* Returns true if addresses of E1 and E2 differ by a constant, false
16082 otherwise. If they do, E1 - E2 is stored in *DIFF. */
16084 bool
16085 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
16087 tree core1, core2;
16088 HOST_WIDE_INT bitpos1, bitpos2;
16089 tree toffset1, toffset2, tdiff, type;
16091 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16092 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16094 if (bitpos1 % BITS_PER_UNIT != 0
16095 || bitpos2 % BITS_PER_UNIT != 0
16096 || !operand_equal_p (core1, core2, 0))
16097 return false;
16099 if (toffset1 && toffset2)
16101 type = TREE_TYPE (toffset1);
16102 if (type != TREE_TYPE (toffset2))
16103 toffset2 = fold_convert (type, toffset2);
16105 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16106 if (!cst_and_fits_in_hwi (tdiff))
16107 return false;
16109 *diff = int_cst_value (tdiff);
16111 else if (toffset1 || toffset2)
16113 /* If only one of the offsets is non-constant, the difference cannot
16114 be a constant. */
16115 return false;
16117 else
16118 *diff = 0;
16120 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
16121 return true;
16124 /* Simplify the floating point expression EXP when the sign of the
16125 result is not significant. Return NULL_TREE if no simplification
16126 is possible. */
16128 tree
16129 fold_strip_sign_ops (tree exp)
16131 tree arg0, arg1;
16132 location_t loc = EXPR_LOCATION (exp);
16134 switch (TREE_CODE (exp))
16136 case ABS_EXPR:
16137 case NEGATE_EXPR:
16138 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16139 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
16141 case MULT_EXPR:
16142 case RDIV_EXPR:
16143 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (exp)))
16144 return NULL_TREE;
16145 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16146 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16147 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
16148 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
16149 arg0 ? arg0 : TREE_OPERAND (exp, 0),
16150 arg1 ? arg1 : TREE_OPERAND (exp, 1));
16151 break;
16153 case COMPOUND_EXPR:
16154 arg0 = TREE_OPERAND (exp, 0);
16155 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16156 if (arg1)
16157 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16158 break;
16160 case COND_EXPR:
16161 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16162 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16163 if (arg0 || arg1)
16164 return fold_build3_loc (loc,
16165 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16166 arg0 ? arg0 : TREE_OPERAND (exp, 1),
16167 arg1 ? arg1 : TREE_OPERAND (exp, 2));
16168 break;
16170 case CALL_EXPR:
16172 const enum built_in_function fcode = builtin_mathfn_code (exp);
16173 switch (fcode)
16175 CASE_FLT_FN (BUILT_IN_COPYSIGN):
16176 /* Strip copysign function call, return the 1st argument. */
16177 arg0 = CALL_EXPR_ARG (exp, 0);
16178 arg1 = CALL_EXPR_ARG (exp, 1);
16179 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16181 default:
16182 /* Strip sign ops from the argument of "odd" math functions. */
16183 if (negate_mathfn_p (fcode))
16185 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16186 if (arg0)
16187 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16189 break;
16192 break;
16194 default:
16195 break;
16197 return NULL_TREE;
16200 /* Return OFF converted to a pointer offset type suitable as offset for
16201 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16202 tree
16203 convert_to_ptrofftype_loc (location_t loc, tree off)
16205 return fold_convert_loc (loc, sizetype, off);
16208 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16209 tree
16210 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16212 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16213 ptr, convert_to_ptrofftype_loc (loc, off));
16216 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16217 tree
16218 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16220 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16221 ptr, size_int (off));