Merge from trunk @215656.
[official-gcc.git] / gcc / fold-const.c
blob707a97f71b85c21803e1a6672e0685f1d1b4dd67
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2014 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 "tree.h"
49 #include "stor-layout.h"
50 #include "calls.h"
51 #include "tree-iterator.h"
52 #include "realmpfr.h"
53 #include "rtl.h"
54 #include "expr.h"
55 #include "tm_p.h"
56 #include "target.h"
57 #include "diagnostic-core.h"
58 #include "intl.h"
59 #include "langhooks.h"
60 #include "md5.h"
61 #include "basic-block.h"
62 #include "tree-ssa-alias.h"
63 #include "internal-fn.h"
64 #include "tree-eh.h"
65 #include "gimple-expr.h"
66 #include "is-a.h"
67 #include "gimple.h"
68 #include "gimplify.h"
69 #include "tree-dfa.h"
70 #include "hash-table.h" /* Required for ENABLE_FOLD_CHECKING. */
71 #include "builtins.h"
72 #include "cgraph.h"
74 /* Nonzero if we are folding constants inside an initializer; zero
75 otherwise. */
76 int folding_initializer = 0;
78 /* The following constants represent a bit based encoding of GCC's
79 comparison operators. This encoding simplifies transformations
80 on relational comparison operators, such as AND and OR. */
81 enum comparison_code {
82 COMPCODE_FALSE = 0,
83 COMPCODE_LT = 1,
84 COMPCODE_EQ = 2,
85 COMPCODE_LE = 3,
86 COMPCODE_GT = 4,
87 COMPCODE_LTGT = 5,
88 COMPCODE_GE = 6,
89 COMPCODE_ORD = 7,
90 COMPCODE_UNORD = 8,
91 COMPCODE_UNLT = 9,
92 COMPCODE_UNEQ = 10,
93 COMPCODE_UNLE = 11,
94 COMPCODE_UNGT = 12,
95 COMPCODE_NE = 13,
96 COMPCODE_UNGE = 14,
97 COMPCODE_TRUE = 15
100 static bool negate_mathfn_p (enum built_in_function);
101 static bool negate_expr_p (tree);
102 static tree negate_expr (tree);
103 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
104 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
105 static tree const_binop (enum tree_code, tree, tree);
106 static enum comparison_code comparison_to_compcode (enum tree_code);
107 static enum tree_code compcode_to_comparison (enum comparison_code);
108 static int operand_equal_for_comparison_p (tree, tree, tree);
109 static int twoval_comparison_p (tree, tree *, tree *, int *);
110 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
111 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
112 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
113 static tree make_bit_field_ref (location_t, tree, tree,
114 HOST_WIDE_INT, HOST_WIDE_INT, int, int);
115 static tree optimize_bit_field_compare (location_t, enum tree_code,
116 tree, tree, tree);
117 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
118 HOST_WIDE_INT *,
119 enum machine_mode *, int *, int *, int *,
120 tree *, tree *);
121 static tree sign_bit_p (tree, const_tree);
122 static int simple_operand_p (const_tree);
123 static bool simple_operand_p_2 (tree);
124 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
125 static tree range_predecessor (tree);
126 static tree range_successor (tree);
127 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
128 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
129 static tree unextend (tree, int, int, tree);
130 static tree optimize_minmax_comparison (location_t, enum tree_code,
131 tree, tree, tree);
132 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
133 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
134 static tree fold_binary_op_with_conditional_arg (location_t,
135 enum tree_code, tree,
136 tree, tree,
137 tree, tree, int);
138 static tree fold_mathfn_compare (location_t,
139 enum built_in_function, enum tree_code,
140 tree, tree, tree);
141 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
142 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
143 static bool reorder_operands_p (const_tree, const_tree);
144 static tree fold_negate_const (tree, tree);
145 static tree fold_not_const (const_tree, tree);
146 static tree fold_relational_const (enum tree_code, tree, tree, tree);
147 static tree fold_convert_const (enum tree_code, tree, tree);
149 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
150 Otherwise, return LOC. */
152 static location_t
153 expr_location_or (tree t, location_t loc)
155 location_t tloc = EXPR_LOCATION (t);
156 return tloc == UNKNOWN_LOCATION ? loc : tloc;
159 /* Similar to protected_set_expr_location, but never modify x in place,
160 if location can and needs to be set, unshare it. */
162 static inline tree
163 protected_set_expr_location_unshare (tree x, location_t loc)
165 if (CAN_HAVE_LOCATION_P (x)
166 && EXPR_LOCATION (x) != loc
167 && !(TREE_CODE (x) == SAVE_EXPR
168 || TREE_CODE (x) == TARGET_EXPR
169 || TREE_CODE (x) == BIND_EXPR))
171 x = copy_node (x);
172 SET_EXPR_LOCATION (x, loc);
174 return x;
177 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
178 division and returns the quotient. Otherwise returns
179 NULL_TREE. */
181 tree
182 div_if_zero_remainder (const_tree arg1, const_tree arg2)
184 widest_int quo;
186 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
187 SIGNED, &quo))
188 return wide_int_to_tree (TREE_TYPE (arg1), quo);
190 return NULL_TREE;
193 /* This is nonzero if we should defer warnings about undefined
194 overflow. This facility exists because these warnings are a
195 special case. The code to estimate loop iterations does not want
196 to issue any warnings, since it works with expressions which do not
197 occur in user code. Various bits of cleanup code call fold(), but
198 only use the result if it has certain characteristics (e.g., is a
199 constant); that code only wants to issue a warning if the result is
200 used. */
202 static int fold_deferring_overflow_warnings;
204 /* If a warning about undefined overflow is deferred, this is the
205 warning. Note that this may cause us to turn two warnings into
206 one, but that is fine since it is sufficient to only give one
207 warning per expression. */
209 static const char* fold_deferred_overflow_warning;
211 /* If a warning about undefined overflow is deferred, this is the
212 level at which the warning should be emitted. */
214 static enum warn_strict_overflow_code fold_deferred_overflow_code;
216 /* Start deferring overflow warnings. We could use a stack here to
217 permit nested calls, but at present it is not necessary. */
219 void
220 fold_defer_overflow_warnings (void)
222 ++fold_deferring_overflow_warnings;
225 /* Stop deferring overflow warnings. If there is a pending warning,
226 and ISSUE is true, then issue the warning if appropriate. STMT is
227 the statement with which the warning should be associated (used for
228 location information); STMT may be NULL. CODE is the level of the
229 warning--a warn_strict_overflow_code value. This function will use
230 the smaller of CODE and the deferred code when deciding whether to
231 issue the warning. CODE may be zero to mean to always use the
232 deferred code. */
234 void
235 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
237 const char *warnmsg;
238 location_t locus;
240 gcc_assert (fold_deferring_overflow_warnings > 0);
241 --fold_deferring_overflow_warnings;
242 if (fold_deferring_overflow_warnings > 0)
244 if (fold_deferred_overflow_warning != NULL
245 && code != 0
246 && code < (int) fold_deferred_overflow_code)
247 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
248 return;
251 warnmsg = fold_deferred_overflow_warning;
252 fold_deferred_overflow_warning = NULL;
254 if (!issue || warnmsg == NULL)
255 return;
257 if (gimple_no_warning_p (stmt))
258 return;
260 /* Use the smallest code level when deciding to issue the
261 warning. */
262 if (code == 0 || code > (int) fold_deferred_overflow_code)
263 code = fold_deferred_overflow_code;
265 if (!issue_strict_overflow_warning (code))
266 return;
268 if (stmt == NULL)
269 locus = input_location;
270 else
271 locus = gimple_location (stmt);
272 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
275 /* Stop deferring overflow warnings, ignoring any deferred
276 warnings. */
278 void
279 fold_undefer_and_ignore_overflow_warnings (void)
281 fold_undefer_overflow_warnings (false, NULL, 0);
284 /* Whether we are deferring overflow warnings. */
286 bool
287 fold_deferring_overflow_warnings_p (void)
289 return fold_deferring_overflow_warnings > 0;
292 /* This is called when we fold something based on the fact that signed
293 overflow is undefined. */
295 static void
296 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
298 if (fold_deferring_overflow_warnings > 0)
300 if (fold_deferred_overflow_warning == NULL
301 || wc < fold_deferred_overflow_code)
303 fold_deferred_overflow_warning = gmsgid;
304 fold_deferred_overflow_code = wc;
307 else if (issue_strict_overflow_warning (wc))
308 warning (OPT_Wstrict_overflow, gmsgid);
311 /* Return true if the built-in mathematical function specified by CODE
312 is odd, i.e. -f(x) == f(-x). */
314 static bool
315 negate_mathfn_p (enum built_in_function code)
317 switch (code)
319 CASE_FLT_FN (BUILT_IN_ASIN):
320 CASE_FLT_FN (BUILT_IN_ASINH):
321 CASE_FLT_FN (BUILT_IN_ATAN):
322 CASE_FLT_FN (BUILT_IN_ATANH):
323 CASE_FLT_FN (BUILT_IN_CASIN):
324 CASE_FLT_FN (BUILT_IN_CASINH):
325 CASE_FLT_FN (BUILT_IN_CATAN):
326 CASE_FLT_FN (BUILT_IN_CATANH):
327 CASE_FLT_FN (BUILT_IN_CBRT):
328 CASE_FLT_FN (BUILT_IN_CPROJ):
329 CASE_FLT_FN (BUILT_IN_CSIN):
330 CASE_FLT_FN (BUILT_IN_CSINH):
331 CASE_FLT_FN (BUILT_IN_CTAN):
332 CASE_FLT_FN (BUILT_IN_CTANH):
333 CASE_FLT_FN (BUILT_IN_ERF):
334 CASE_FLT_FN (BUILT_IN_LLROUND):
335 CASE_FLT_FN (BUILT_IN_LROUND):
336 CASE_FLT_FN (BUILT_IN_ROUND):
337 CASE_FLT_FN (BUILT_IN_SIN):
338 CASE_FLT_FN (BUILT_IN_SINH):
339 CASE_FLT_FN (BUILT_IN_TAN):
340 CASE_FLT_FN (BUILT_IN_TANH):
341 CASE_FLT_FN (BUILT_IN_TRUNC):
342 return true;
344 CASE_FLT_FN (BUILT_IN_LLRINT):
345 CASE_FLT_FN (BUILT_IN_LRINT):
346 CASE_FLT_FN (BUILT_IN_NEARBYINT):
347 CASE_FLT_FN (BUILT_IN_RINT):
348 return !flag_rounding_math;
350 default:
351 break;
353 return false;
356 /* Check whether we may negate an integer constant T without causing
357 overflow. */
359 bool
360 may_negate_without_overflow_p (const_tree t)
362 tree type;
364 gcc_assert (TREE_CODE (t) == INTEGER_CST);
366 type = TREE_TYPE (t);
367 if (TYPE_UNSIGNED (type))
368 return false;
370 return !wi::only_sign_bit_p (t);
373 /* Determine whether an expression T can be cheaply negated using
374 the function negate_expr without introducing undefined overflow. */
376 static bool
377 negate_expr_p (tree t)
379 tree type;
381 if (t == 0)
382 return false;
384 type = TREE_TYPE (t);
386 STRIP_SIGN_NOPS (t);
387 switch (TREE_CODE (t))
389 case INTEGER_CST:
390 if (TYPE_OVERFLOW_WRAPS (type))
391 return true;
393 /* Check that -CST will not overflow type. */
394 return may_negate_without_overflow_p (t);
395 case BIT_NOT_EXPR:
396 return (INTEGRAL_TYPE_P (type)
397 && TYPE_OVERFLOW_WRAPS (type));
399 case FIXED_CST:
400 case NEGATE_EXPR:
401 return true;
403 case REAL_CST:
404 /* We want to canonicalize to positive real constants. Pretend
405 that only negative ones can be easily negated. */
406 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
408 case COMPLEX_CST:
409 return negate_expr_p (TREE_REALPART (t))
410 && negate_expr_p (TREE_IMAGPART (t));
412 case VECTOR_CST:
414 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
415 return true;
417 int count = TYPE_VECTOR_SUBPARTS (type), i;
419 for (i = 0; i < count; i++)
420 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
421 return false;
423 return true;
426 case COMPLEX_EXPR:
427 return negate_expr_p (TREE_OPERAND (t, 0))
428 && negate_expr_p (TREE_OPERAND (t, 1));
430 case CONJ_EXPR:
431 return negate_expr_p (TREE_OPERAND (t, 0));
433 case PLUS_EXPR:
434 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
435 || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
436 return false;
437 /* -(A + B) -> (-B) - A. */
438 if (negate_expr_p (TREE_OPERAND (t, 1))
439 && reorder_operands_p (TREE_OPERAND (t, 0),
440 TREE_OPERAND (t, 1)))
441 return true;
442 /* -(A + B) -> (-A) - B. */
443 return negate_expr_p (TREE_OPERAND (t, 0));
445 case MINUS_EXPR:
446 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
447 return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
448 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
449 && reorder_operands_p (TREE_OPERAND (t, 0),
450 TREE_OPERAND (t, 1));
452 case MULT_EXPR:
453 if (TYPE_UNSIGNED (TREE_TYPE (t)))
454 break;
456 /* Fall through. */
458 case RDIV_EXPR:
459 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
460 return negate_expr_p (TREE_OPERAND (t, 1))
461 || negate_expr_p (TREE_OPERAND (t, 0));
462 break;
464 case TRUNC_DIV_EXPR:
465 case ROUND_DIV_EXPR:
466 case EXACT_DIV_EXPR:
467 /* In general we can't negate A / B, because if A is INT_MIN and
468 B is 1, we may turn this into INT_MIN / -1 which is undefined
469 and actually traps on some architectures. But if overflow is
470 undefined, we can negate, because - (INT_MIN / 1) is an
471 overflow. */
472 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
474 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
475 break;
476 /* If overflow is undefined then we have to be careful because
477 we ask whether it's ok to associate the negate with the
478 division which is not ok for example for
479 -((a - b) / c) where (-(a - b)) / c may invoke undefined
480 overflow because of negating INT_MIN. So do not use
481 negate_expr_p here but open-code the two important cases. */
482 if (TREE_CODE (TREE_OPERAND (t, 0)) == NEGATE_EXPR
483 || (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
484 && may_negate_without_overflow_p (TREE_OPERAND (t, 0))))
485 return true;
487 else if (negate_expr_p (TREE_OPERAND (t, 0)))
488 return true;
489 return negate_expr_p (TREE_OPERAND (t, 1));
491 case NOP_EXPR:
492 /* Negate -((double)float) as (double)(-float). */
493 if (TREE_CODE (type) == REAL_TYPE)
495 tree tem = strip_float_extensions (t);
496 if (tem != t)
497 return negate_expr_p (tem);
499 break;
501 case CALL_EXPR:
502 /* Negate -f(x) as f(-x). */
503 if (negate_mathfn_p (builtin_mathfn_code (t)))
504 return negate_expr_p (CALL_EXPR_ARG (t, 0));
505 break;
507 case RSHIFT_EXPR:
508 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
509 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
511 tree op1 = TREE_OPERAND (t, 1);
512 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
513 return true;
515 break;
517 default:
518 break;
520 return false;
523 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
524 simplification is possible.
525 If negate_expr_p would return true for T, NULL_TREE will never be
526 returned. */
528 static tree
529 fold_negate_expr (location_t loc, tree t)
531 tree type = TREE_TYPE (t);
532 tree tem;
534 switch (TREE_CODE (t))
536 /* Convert - (~A) to A + 1. */
537 case BIT_NOT_EXPR:
538 if (INTEGRAL_TYPE_P (type))
539 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
540 build_one_cst (type));
541 break;
543 case INTEGER_CST:
544 tem = fold_negate_const (t, type);
545 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
546 || !TYPE_OVERFLOW_TRAPS (type))
547 return tem;
548 break;
550 case REAL_CST:
551 tem = fold_negate_const (t, type);
552 /* Two's complement FP formats, such as c4x, may overflow. */
553 if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
554 return tem;
555 break;
557 case FIXED_CST:
558 tem = fold_negate_const (t, type);
559 return tem;
561 case COMPLEX_CST:
563 tree rpart = negate_expr (TREE_REALPART (t));
564 tree ipart = negate_expr (TREE_IMAGPART (t));
566 if ((TREE_CODE (rpart) == REAL_CST
567 && TREE_CODE (ipart) == REAL_CST)
568 || (TREE_CODE (rpart) == INTEGER_CST
569 && TREE_CODE (ipart) == INTEGER_CST))
570 return build_complex (type, rpart, ipart);
572 break;
574 case VECTOR_CST:
576 int count = TYPE_VECTOR_SUBPARTS (type), i;
577 tree *elts = XALLOCAVEC (tree, count);
579 for (i = 0; i < count; i++)
581 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
582 if (elts[i] == NULL_TREE)
583 return NULL_TREE;
586 return build_vector (type, elts);
589 case COMPLEX_EXPR:
590 if (negate_expr_p (t))
591 return fold_build2_loc (loc, COMPLEX_EXPR, type,
592 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
593 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
594 break;
596 case CONJ_EXPR:
597 if (negate_expr_p (t))
598 return fold_build1_loc (loc, CONJ_EXPR, type,
599 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
600 break;
602 case NEGATE_EXPR:
603 return TREE_OPERAND (t, 0);
605 case PLUS_EXPR:
606 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
607 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
609 /* -(A + B) -> (-B) - A. */
610 if (negate_expr_p (TREE_OPERAND (t, 1))
611 && reorder_operands_p (TREE_OPERAND (t, 0),
612 TREE_OPERAND (t, 1)))
614 tem = negate_expr (TREE_OPERAND (t, 1));
615 return fold_build2_loc (loc, MINUS_EXPR, type,
616 tem, TREE_OPERAND (t, 0));
619 /* -(A + B) -> (-A) - B. */
620 if (negate_expr_p (TREE_OPERAND (t, 0)))
622 tem = negate_expr (TREE_OPERAND (t, 0));
623 return fold_build2_loc (loc, MINUS_EXPR, type,
624 tem, TREE_OPERAND (t, 1));
627 break;
629 case MINUS_EXPR:
630 /* - (A - B) -> B - A */
631 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
632 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
633 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
634 return fold_build2_loc (loc, MINUS_EXPR, type,
635 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
636 break;
638 case MULT_EXPR:
639 if (TYPE_UNSIGNED (type))
640 break;
642 /* Fall through. */
644 case RDIV_EXPR:
645 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
647 tem = TREE_OPERAND (t, 1);
648 if (negate_expr_p (tem))
649 return fold_build2_loc (loc, TREE_CODE (t), type,
650 TREE_OPERAND (t, 0), negate_expr (tem));
651 tem = TREE_OPERAND (t, 0);
652 if (negate_expr_p (tem))
653 return fold_build2_loc (loc, TREE_CODE (t), type,
654 negate_expr (tem), TREE_OPERAND (t, 1));
656 break;
658 case TRUNC_DIV_EXPR:
659 case ROUND_DIV_EXPR:
660 case EXACT_DIV_EXPR:
661 /* In general we can't negate A / B, because if A is INT_MIN and
662 B is 1, we may turn this into INT_MIN / -1 which is undefined
663 and actually traps on some architectures. But if overflow is
664 undefined, we can negate, because - (INT_MIN / 1) is an
665 overflow. */
666 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
668 const char * const warnmsg = G_("assuming signed overflow does not "
669 "occur when negating a division");
670 tem = TREE_OPERAND (t, 1);
671 if (negate_expr_p (tem))
673 if (INTEGRAL_TYPE_P (type)
674 && (TREE_CODE (tem) != INTEGER_CST
675 || integer_onep (tem)))
676 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
677 return fold_build2_loc (loc, TREE_CODE (t), type,
678 TREE_OPERAND (t, 0), negate_expr (tem));
680 /* If overflow is undefined then we have to be careful because
681 we ask whether it's ok to associate the negate with the
682 division which is not ok for example for
683 -((a - b) / c) where (-(a - b)) / c may invoke undefined
684 overflow because of negating INT_MIN. So do not use
685 negate_expr_p here but open-code the two important cases. */
686 tem = TREE_OPERAND (t, 0);
687 if ((INTEGRAL_TYPE_P (type)
688 && (TREE_CODE (tem) == NEGATE_EXPR
689 || (TREE_CODE (tem) == INTEGER_CST
690 && may_negate_without_overflow_p (tem))))
691 || !INTEGRAL_TYPE_P (type))
692 return fold_build2_loc (loc, TREE_CODE (t), type,
693 negate_expr (tem), TREE_OPERAND (t, 1));
695 break;
697 case NOP_EXPR:
698 /* Convert -((double)float) into (double)(-float). */
699 if (TREE_CODE (type) == REAL_TYPE)
701 tem = strip_float_extensions (t);
702 if (tem != t && negate_expr_p (tem))
703 return fold_convert_loc (loc, type, negate_expr (tem));
705 break;
707 case CALL_EXPR:
708 /* Negate -f(x) as f(-x). */
709 if (negate_mathfn_p (builtin_mathfn_code (t))
710 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
712 tree fndecl, arg;
714 fndecl = get_callee_fndecl (t);
715 arg = negate_expr (CALL_EXPR_ARG (t, 0));
716 return build_call_expr_loc (loc, fndecl, 1, arg);
718 break;
720 case RSHIFT_EXPR:
721 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
722 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
724 tree op1 = TREE_OPERAND (t, 1);
725 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
727 tree ntype = TYPE_UNSIGNED (type)
728 ? signed_type_for (type)
729 : unsigned_type_for (type);
730 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
731 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
732 return fold_convert_loc (loc, type, temp);
735 break;
737 default:
738 break;
741 return NULL_TREE;
744 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
745 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
746 return NULL_TREE. */
748 static tree
749 negate_expr (tree t)
751 tree type, tem;
752 location_t loc;
754 if (t == NULL_TREE)
755 return NULL_TREE;
757 loc = EXPR_LOCATION (t);
758 type = TREE_TYPE (t);
759 STRIP_SIGN_NOPS (t);
761 tem = fold_negate_expr (loc, t);
762 if (!tem)
763 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
764 return fold_convert_loc (loc, type, tem);
767 /* Split a tree IN into a constant, literal and variable parts that could be
768 combined with CODE to make IN. "constant" means an expression with
769 TREE_CONSTANT but that isn't an actual constant. CODE must be a
770 commutative arithmetic operation. Store the constant part into *CONP,
771 the literal in *LITP and return the variable part. If a part isn't
772 present, set it to null. If the tree does not decompose in this way,
773 return the entire tree as the variable part and the other parts as null.
775 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
776 case, we negate an operand that was subtracted. Except if it is a
777 literal for which we use *MINUS_LITP instead.
779 If NEGATE_P is true, we are negating all of IN, again except a literal
780 for which we use *MINUS_LITP instead.
782 If IN is itself a literal or constant, return it as appropriate.
784 Note that we do not guarantee that any of the three values will be the
785 same type as IN, but they will have the same signedness and mode. */
787 static tree
788 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
789 tree *minus_litp, int negate_p)
791 tree var = 0;
793 *conp = 0;
794 *litp = 0;
795 *minus_litp = 0;
797 /* Strip any conversions that don't change the machine mode or signedness. */
798 STRIP_SIGN_NOPS (in);
800 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
801 || TREE_CODE (in) == FIXED_CST)
802 *litp = in;
803 else if (TREE_CODE (in) == code
804 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
805 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
806 /* We can associate addition and subtraction together (even
807 though the C standard doesn't say so) for integers because
808 the value is not affected. For reals, the value might be
809 affected, so we can't. */
810 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
811 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
813 tree op0 = TREE_OPERAND (in, 0);
814 tree op1 = TREE_OPERAND (in, 1);
815 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
816 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
818 /* First see if either of the operands is a literal, then a constant. */
819 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
820 || TREE_CODE (op0) == FIXED_CST)
821 *litp = op0, op0 = 0;
822 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
823 || TREE_CODE (op1) == FIXED_CST)
824 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
826 if (op0 != 0 && TREE_CONSTANT (op0))
827 *conp = op0, op0 = 0;
828 else if (op1 != 0 && TREE_CONSTANT (op1))
829 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
831 /* If we haven't dealt with either operand, this is not a case we can
832 decompose. Otherwise, VAR is either of the ones remaining, if any. */
833 if (op0 != 0 && op1 != 0)
834 var = in;
835 else if (op0 != 0)
836 var = op0;
837 else
838 var = op1, neg_var_p = neg1_p;
840 /* Now do any needed negations. */
841 if (neg_litp_p)
842 *minus_litp = *litp, *litp = 0;
843 if (neg_conp_p)
844 *conp = negate_expr (*conp);
845 if (neg_var_p)
846 var = negate_expr (var);
848 else if (TREE_CODE (in) == BIT_NOT_EXPR
849 && code == PLUS_EXPR)
851 /* -X - 1 is folded to ~X, undo that here. */
852 *minus_litp = build_one_cst (TREE_TYPE (in));
853 var = negate_expr (TREE_OPERAND (in, 0));
855 else if (TREE_CONSTANT (in))
856 *conp = in;
857 else
858 var = in;
860 if (negate_p)
862 if (*litp)
863 *minus_litp = *litp, *litp = 0;
864 else if (*minus_litp)
865 *litp = *minus_litp, *minus_litp = 0;
866 *conp = negate_expr (*conp);
867 var = negate_expr (var);
870 return var;
873 /* Re-associate trees split by the above function. T1 and T2 are
874 either expressions to associate or null. Return the new
875 expression, if any. LOC is the location of the new expression. If
876 we build an operation, do it in TYPE and with CODE. */
878 static tree
879 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
881 if (t1 == 0)
882 return t2;
883 else if (t2 == 0)
884 return t1;
886 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
887 try to fold this since we will have infinite recursion. But do
888 deal with any NEGATE_EXPRs. */
889 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
890 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
892 if (code == PLUS_EXPR)
894 if (TREE_CODE (t1) == NEGATE_EXPR)
895 return build2_loc (loc, MINUS_EXPR, type,
896 fold_convert_loc (loc, type, t2),
897 fold_convert_loc (loc, type,
898 TREE_OPERAND (t1, 0)));
899 else if (TREE_CODE (t2) == NEGATE_EXPR)
900 return build2_loc (loc, MINUS_EXPR, type,
901 fold_convert_loc (loc, type, t1),
902 fold_convert_loc (loc, type,
903 TREE_OPERAND (t2, 0)));
904 else if (integer_zerop (t2))
905 return fold_convert_loc (loc, type, t1);
907 else if (code == MINUS_EXPR)
909 if (integer_zerop (t2))
910 return fold_convert_loc (loc, type, t1);
913 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
914 fold_convert_loc (loc, type, t2));
917 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
918 fold_convert_loc (loc, type, t2));
921 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
922 for use in int_const_binop, size_binop and size_diffop. */
924 static bool
925 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
927 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
928 return false;
929 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
930 return false;
932 switch (code)
934 case LSHIFT_EXPR:
935 case RSHIFT_EXPR:
936 case LROTATE_EXPR:
937 case RROTATE_EXPR:
938 return true;
940 default:
941 break;
944 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
945 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
946 && TYPE_MODE (type1) == TYPE_MODE (type2);
950 /* Combine two integer constants ARG1 and ARG2 under operation CODE
951 to produce a new constant. Return NULL_TREE if we don't know how
952 to evaluate CODE at compile-time. */
954 static tree
955 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
956 int overflowable)
958 wide_int res;
959 tree t;
960 tree type = TREE_TYPE (arg1);
961 signop sign = TYPE_SIGN (type);
962 bool overflow = false;
964 wide_int arg2 = wide_int::from (parg2, TYPE_PRECISION (type),
965 TYPE_SIGN (TREE_TYPE (parg2)));
967 switch (code)
969 case BIT_IOR_EXPR:
970 res = wi::bit_or (arg1, arg2);
971 break;
973 case BIT_XOR_EXPR:
974 res = wi::bit_xor (arg1, arg2);
975 break;
977 case BIT_AND_EXPR:
978 res = wi::bit_and (arg1, arg2);
979 break;
981 case RSHIFT_EXPR:
982 case LSHIFT_EXPR:
983 if (wi::neg_p (arg2))
985 arg2 = -arg2;
986 if (code == RSHIFT_EXPR)
987 code = LSHIFT_EXPR;
988 else
989 code = RSHIFT_EXPR;
992 if (code == RSHIFT_EXPR)
993 /* It's unclear from the C standard whether shifts can overflow.
994 The following code ignores overflow; perhaps a C standard
995 interpretation ruling is needed. */
996 res = wi::rshift (arg1, arg2, sign);
997 else
998 res = wi::lshift (arg1, arg2);
999 break;
1001 case RROTATE_EXPR:
1002 case LROTATE_EXPR:
1003 if (wi::neg_p (arg2))
1005 arg2 = -arg2;
1006 if (code == RROTATE_EXPR)
1007 code = LROTATE_EXPR;
1008 else
1009 code = RROTATE_EXPR;
1012 if (code == RROTATE_EXPR)
1013 res = wi::rrotate (arg1, arg2);
1014 else
1015 res = wi::lrotate (arg1, arg2);
1016 break;
1018 case PLUS_EXPR:
1019 res = wi::add (arg1, arg2, sign, &overflow);
1020 break;
1022 case MINUS_EXPR:
1023 res = wi::sub (arg1, arg2, sign, &overflow);
1024 break;
1026 case MULT_EXPR:
1027 res = wi::mul (arg1, arg2, sign, &overflow);
1028 break;
1030 case MULT_HIGHPART_EXPR:
1031 res = wi::mul_high (arg1, arg2, sign);
1032 break;
1034 case TRUNC_DIV_EXPR:
1035 case EXACT_DIV_EXPR:
1036 if (arg2 == 0)
1037 return NULL_TREE;
1038 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1039 break;
1041 case FLOOR_DIV_EXPR:
1042 if (arg2 == 0)
1043 return NULL_TREE;
1044 res = wi::div_floor (arg1, arg2, sign, &overflow);
1045 break;
1047 case CEIL_DIV_EXPR:
1048 if (arg2 == 0)
1049 return NULL_TREE;
1050 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1051 break;
1053 case ROUND_DIV_EXPR:
1054 if (arg2 == 0)
1055 return NULL_TREE;
1056 res = wi::div_round (arg1, arg2, sign, &overflow);
1057 break;
1059 case TRUNC_MOD_EXPR:
1060 if (arg2 == 0)
1061 return NULL_TREE;
1062 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1063 break;
1065 case FLOOR_MOD_EXPR:
1066 if (arg2 == 0)
1067 return NULL_TREE;
1068 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1069 break;
1071 case CEIL_MOD_EXPR:
1072 if (arg2 == 0)
1073 return NULL_TREE;
1074 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1075 break;
1077 case ROUND_MOD_EXPR:
1078 if (arg2 == 0)
1079 return NULL_TREE;
1080 res = wi::mod_round (arg1, arg2, sign, &overflow);
1081 break;
1083 case MIN_EXPR:
1084 res = wi::min (arg1, arg2, sign);
1085 break;
1087 case MAX_EXPR:
1088 res = wi::max (arg1, arg2, sign);
1089 break;
1091 default:
1092 return NULL_TREE;
1095 t = force_fit_type (type, res, overflowable,
1096 (((sign == SIGNED || overflowable == -1)
1097 && overflow)
1098 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1100 return t;
1103 tree
1104 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1106 return int_const_binop_1 (code, arg1, arg2, 1);
1109 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1110 constant. We assume ARG1 and ARG2 have the same data type, or at least
1111 are the same kind of constant and the same machine mode. Return zero if
1112 combining the constants is not allowed in the current operating mode. */
1114 static tree
1115 const_binop (enum tree_code code, tree arg1, tree arg2)
1117 /* Sanity check for the recursive cases. */
1118 if (!arg1 || !arg2)
1119 return NULL_TREE;
1121 STRIP_NOPS (arg1);
1122 STRIP_NOPS (arg2);
1124 if (TREE_CODE (arg1) == INTEGER_CST)
1125 return int_const_binop (code, arg1, arg2);
1127 if (TREE_CODE (arg1) == REAL_CST)
1129 enum machine_mode mode;
1130 REAL_VALUE_TYPE d1;
1131 REAL_VALUE_TYPE d2;
1132 REAL_VALUE_TYPE value;
1133 REAL_VALUE_TYPE result;
1134 bool inexact;
1135 tree t, type;
1137 /* The following codes are handled by real_arithmetic. */
1138 switch (code)
1140 case PLUS_EXPR:
1141 case MINUS_EXPR:
1142 case MULT_EXPR:
1143 case RDIV_EXPR:
1144 case MIN_EXPR:
1145 case MAX_EXPR:
1146 break;
1148 default:
1149 return NULL_TREE;
1152 d1 = TREE_REAL_CST (arg1);
1153 d2 = TREE_REAL_CST (arg2);
1155 type = TREE_TYPE (arg1);
1156 mode = TYPE_MODE (type);
1158 /* Don't perform operation if we honor signaling NaNs and
1159 either operand is a NaN. */
1160 if (HONOR_SNANS (mode)
1161 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1162 return NULL_TREE;
1164 /* Don't perform operation if it would raise a division
1165 by zero exception. */
1166 if (code == RDIV_EXPR
1167 && REAL_VALUES_EQUAL (d2, dconst0)
1168 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1169 return NULL_TREE;
1171 /* If either operand is a NaN, just return it. Otherwise, set up
1172 for floating-point trap; we return an overflow. */
1173 if (REAL_VALUE_ISNAN (d1))
1174 return arg1;
1175 else if (REAL_VALUE_ISNAN (d2))
1176 return arg2;
1178 inexact = real_arithmetic (&value, code, &d1, &d2);
1179 real_convert (&result, mode, &value);
1181 /* Don't constant fold this floating point operation if
1182 the result has overflowed and flag_trapping_math. */
1183 if (flag_trapping_math
1184 && MODE_HAS_INFINITIES (mode)
1185 && REAL_VALUE_ISINF (result)
1186 && !REAL_VALUE_ISINF (d1)
1187 && !REAL_VALUE_ISINF (d2))
1188 return NULL_TREE;
1190 /* Don't constant fold this floating point operation if the
1191 result may dependent upon the run-time rounding mode and
1192 flag_rounding_math is set, or if GCC's software emulation
1193 is unable to accurately represent the result. */
1194 if ((flag_rounding_math
1195 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1196 && (inexact || !real_identical (&result, &value)))
1197 return NULL_TREE;
1199 t = build_real (type, result);
1201 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1202 return t;
1205 if (TREE_CODE (arg1) == FIXED_CST)
1207 FIXED_VALUE_TYPE f1;
1208 FIXED_VALUE_TYPE f2;
1209 FIXED_VALUE_TYPE result;
1210 tree t, type;
1211 int sat_p;
1212 bool overflow_p;
1214 /* The following codes are handled by fixed_arithmetic. */
1215 switch (code)
1217 case PLUS_EXPR:
1218 case MINUS_EXPR:
1219 case MULT_EXPR:
1220 case TRUNC_DIV_EXPR:
1221 f2 = TREE_FIXED_CST (arg2);
1222 break;
1224 case LSHIFT_EXPR:
1225 case RSHIFT_EXPR:
1227 wide_int w2 = arg2;
1228 f2.data.high = w2.elt (1);
1229 f2.data.low = w2.elt (0);
1230 f2.mode = SImode;
1232 break;
1234 default:
1235 return NULL_TREE;
1238 f1 = TREE_FIXED_CST (arg1);
1239 type = TREE_TYPE (arg1);
1240 sat_p = TYPE_SATURATING (type);
1241 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1242 t = build_fixed (type, result);
1243 /* Propagate overflow flags. */
1244 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1245 TREE_OVERFLOW (t) = 1;
1246 return t;
1249 if (TREE_CODE (arg1) == COMPLEX_CST)
1251 tree type = TREE_TYPE (arg1);
1252 tree r1 = TREE_REALPART (arg1);
1253 tree i1 = TREE_IMAGPART (arg1);
1254 tree r2 = TREE_REALPART (arg2);
1255 tree i2 = TREE_IMAGPART (arg2);
1256 tree real, imag;
1258 switch (code)
1260 case PLUS_EXPR:
1261 case MINUS_EXPR:
1262 real = const_binop (code, r1, r2);
1263 imag = const_binop (code, i1, i2);
1264 break;
1266 case MULT_EXPR:
1267 if (COMPLEX_FLOAT_TYPE_P (type))
1268 return do_mpc_arg2 (arg1, arg2, type,
1269 /* do_nonfinite= */ folding_initializer,
1270 mpc_mul);
1272 real = const_binop (MINUS_EXPR,
1273 const_binop (MULT_EXPR, r1, r2),
1274 const_binop (MULT_EXPR, i1, i2));
1275 imag = const_binop (PLUS_EXPR,
1276 const_binop (MULT_EXPR, r1, i2),
1277 const_binop (MULT_EXPR, i1, r2));
1278 break;
1280 case RDIV_EXPR:
1281 if (COMPLEX_FLOAT_TYPE_P (type))
1282 return do_mpc_arg2 (arg1, arg2, type,
1283 /* do_nonfinite= */ folding_initializer,
1284 mpc_div);
1285 /* Fallthru ... */
1286 case TRUNC_DIV_EXPR:
1287 case CEIL_DIV_EXPR:
1288 case FLOOR_DIV_EXPR:
1289 case ROUND_DIV_EXPR:
1290 if (flag_complex_method == 0)
1292 /* Keep this algorithm in sync with
1293 tree-complex.c:expand_complex_div_straight().
1295 Expand complex division to scalars, straightforward algorithm.
1296 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1297 t = br*br + bi*bi
1299 tree magsquared
1300 = const_binop (PLUS_EXPR,
1301 const_binop (MULT_EXPR, r2, r2),
1302 const_binop (MULT_EXPR, i2, i2));
1303 tree t1
1304 = const_binop (PLUS_EXPR,
1305 const_binop (MULT_EXPR, r1, r2),
1306 const_binop (MULT_EXPR, i1, i2));
1307 tree t2
1308 = const_binop (MINUS_EXPR,
1309 const_binop (MULT_EXPR, i1, r2),
1310 const_binop (MULT_EXPR, r1, i2));
1312 real = const_binop (code, t1, magsquared);
1313 imag = const_binop (code, t2, magsquared);
1315 else
1317 /* Keep this algorithm in sync with
1318 tree-complex.c:expand_complex_div_wide().
1320 Expand complex division to scalars, modified algorithm to minimize
1321 overflow with wide input ranges. */
1322 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1323 fold_abs_const (r2, TREE_TYPE (type)),
1324 fold_abs_const (i2, TREE_TYPE (type)));
1326 if (integer_nonzerop (compare))
1328 /* In the TRUE branch, we compute
1329 ratio = br/bi;
1330 div = (br * ratio) + bi;
1331 tr = (ar * ratio) + ai;
1332 ti = (ai * ratio) - ar;
1333 tr = tr / div;
1334 ti = ti / div; */
1335 tree ratio = const_binop (code, r2, i2);
1336 tree div = const_binop (PLUS_EXPR, i2,
1337 const_binop (MULT_EXPR, r2, ratio));
1338 real = const_binop (MULT_EXPR, r1, ratio);
1339 real = const_binop (PLUS_EXPR, real, i1);
1340 real = const_binop (code, real, div);
1342 imag = const_binop (MULT_EXPR, i1, ratio);
1343 imag = const_binop (MINUS_EXPR, imag, r1);
1344 imag = const_binop (code, imag, div);
1346 else
1348 /* In the FALSE branch, we compute
1349 ratio = d/c;
1350 divisor = (d * ratio) + c;
1351 tr = (b * ratio) + a;
1352 ti = b - (a * ratio);
1353 tr = tr / div;
1354 ti = ti / div; */
1355 tree ratio = const_binop (code, i2, r2);
1356 tree div = const_binop (PLUS_EXPR, r2,
1357 const_binop (MULT_EXPR, i2, ratio));
1359 real = const_binop (MULT_EXPR, i1, ratio);
1360 real = const_binop (PLUS_EXPR, real, r1);
1361 real = const_binop (code, real, div);
1363 imag = const_binop (MULT_EXPR, r1, ratio);
1364 imag = const_binop (MINUS_EXPR, i1, imag);
1365 imag = const_binop (code, imag, div);
1368 break;
1370 default:
1371 return NULL_TREE;
1374 if (real && imag)
1375 return build_complex (type, real, imag);
1378 if (TREE_CODE (arg1) == VECTOR_CST
1379 && TREE_CODE (arg2) == VECTOR_CST)
1381 tree type = TREE_TYPE (arg1);
1382 int count = TYPE_VECTOR_SUBPARTS (type), i;
1383 tree *elts = XALLOCAVEC (tree, count);
1385 for (i = 0; i < count; i++)
1387 tree elem1 = VECTOR_CST_ELT (arg1, i);
1388 tree elem2 = VECTOR_CST_ELT (arg2, i);
1390 elts[i] = const_binop (code, elem1, elem2);
1392 /* It is possible that const_binop cannot handle the given
1393 code and return NULL_TREE */
1394 if (elts[i] == NULL_TREE)
1395 return NULL_TREE;
1398 return build_vector (type, elts);
1401 /* Shifts allow a scalar offset for a vector. */
1402 if (TREE_CODE (arg1) == VECTOR_CST
1403 && TREE_CODE (arg2) == INTEGER_CST)
1405 tree type = TREE_TYPE (arg1);
1406 int count = TYPE_VECTOR_SUBPARTS (type), i;
1407 tree *elts = XALLOCAVEC (tree, count);
1409 if (code == VEC_LSHIFT_EXPR
1410 || code == VEC_RSHIFT_EXPR)
1412 if (!tree_fits_uhwi_p (arg2))
1413 return NULL_TREE;
1415 unsigned HOST_WIDE_INT shiftc = tree_to_uhwi (arg2);
1416 unsigned HOST_WIDE_INT outerc = tree_to_uhwi (TYPE_SIZE (type));
1417 unsigned HOST_WIDE_INT innerc
1418 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (type)));
1419 if (shiftc >= outerc || (shiftc % innerc) != 0)
1420 return NULL_TREE;
1421 int offset = shiftc / innerc;
1422 /* The direction of VEC_[LR]SHIFT_EXPR is endian dependent.
1423 For reductions, compiler emits VEC_RSHIFT_EXPR always,
1424 for !BYTES_BIG_ENDIAN picks first vector element, but
1425 for BYTES_BIG_ENDIAN last element from the vector. */
1426 if ((code == VEC_RSHIFT_EXPR) ^ (!BYTES_BIG_ENDIAN))
1427 offset = -offset;
1428 tree zero = build_zero_cst (TREE_TYPE (type));
1429 for (i = 0; i < count; i++)
1431 if (i + offset < 0 || i + offset >= count)
1432 elts[i] = zero;
1433 else
1434 elts[i] = VECTOR_CST_ELT (arg1, i + offset);
1437 else
1438 for (i = 0; i < count; i++)
1440 tree elem1 = VECTOR_CST_ELT (arg1, i);
1442 elts[i] = const_binop (code, elem1, arg2);
1444 /* It is possible that const_binop cannot handle the given
1445 code and return NULL_TREE */
1446 if (elts[i] == NULL_TREE)
1447 return NULL_TREE;
1450 return build_vector (type, elts);
1452 return NULL_TREE;
1455 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1456 indicates which particular sizetype to create. */
1458 tree
1459 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1461 return build_int_cst (sizetype_tab[(int) kind], number);
1464 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1465 is a tree code. The type of the result is taken from the operands.
1466 Both must be equivalent integer types, ala int_binop_types_match_p.
1467 If the operands are constant, so is the result. */
1469 tree
1470 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1472 tree type = TREE_TYPE (arg0);
1474 if (arg0 == error_mark_node || arg1 == error_mark_node)
1475 return error_mark_node;
1477 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1478 TREE_TYPE (arg1)));
1480 /* Handle the special case of two integer constants faster. */
1481 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1483 /* And some specific cases even faster than that. */
1484 if (code == PLUS_EXPR)
1486 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1487 return arg1;
1488 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1489 return arg0;
1491 else if (code == MINUS_EXPR)
1493 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1494 return arg0;
1496 else if (code == MULT_EXPR)
1498 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1499 return arg1;
1502 /* Handle general case of two integer constants. For sizetype
1503 constant calculations we always want to know about overflow,
1504 even in the unsigned case. */
1505 return int_const_binop_1 (code, arg0, arg1, -1);
1508 return fold_build2_loc (loc, code, type, arg0, arg1);
1511 /* Given two values, either both of sizetype or both of bitsizetype,
1512 compute the difference between the two values. Return the value
1513 in signed type corresponding to the type of the operands. */
1515 tree
1516 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1518 tree type = TREE_TYPE (arg0);
1519 tree ctype;
1521 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1522 TREE_TYPE (arg1)));
1524 /* If the type is already signed, just do the simple thing. */
1525 if (!TYPE_UNSIGNED (type))
1526 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1528 if (type == sizetype)
1529 ctype = ssizetype;
1530 else if (type == bitsizetype)
1531 ctype = sbitsizetype;
1532 else
1533 ctype = signed_type_for (type);
1535 /* If either operand is not a constant, do the conversions to the signed
1536 type and subtract. The hardware will do the right thing with any
1537 overflow in the subtraction. */
1538 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1539 return size_binop_loc (loc, MINUS_EXPR,
1540 fold_convert_loc (loc, ctype, arg0),
1541 fold_convert_loc (loc, ctype, arg1));
1543 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1544 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1545 overflow) and negate (which can't either). Special-case a result
1546 of zero while we're here. */
1547 if (tree_int_cst_equal (arg0, arg1))
1548 return build_int_cst (ctype, 0);
1549 else if (tree_int_cst_lt (arg1, arg0))
1550 return fold_convert_loc (loc, ctype,
1551 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1552 else
1553 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1554 fold_convert_loc (loc, ctype,
1555 size_binop_loc (loc,
1556 MINUS_EXPR,
1557 arg1, arg0)));
1560 /* A subroutine of fold_convert_const handling conversions of an
1561 INTEGER_CST to another integer type. */
1563 static tree
1564 fold_convert_const_int_from_int (tree type, const_tree arg1)
1566 /* Given an integer constant, make new constant with new type,
1567 appropriately sign-extended or truncated. Use widest_int
1568 so that any extension is done according ARG1's type. */
1569 return force_fit_type (type, wi::to_widest (arg1),
1570 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1571 TREE_OVERFLOW (arg1));
1574 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1575 to an integer type. */
1577 static tree
1578 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1580 bool overflow = false;
1581 tree t;
1583 /* The following code implements the floating point to integer
1584 conversion rules required by the Java Language Specification,
1585 that IEEE NaNs are mapped to zero and values that overflow
1586 the target precision saturate, i.e. values greater than
1587 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1588 are mapped to INT_MIN. These semantics are allowed by the
1589 C and C++ standards that simply state that the behavior of
1590 FP-to-integer conversion is unspecified upon overflow. */
1592 wide_int val;
1593 REAL_VALUE_TYPE r;
1594 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1596 switch (code)
1598 case FIX_TRUNC_EXPR:
1599 real_trunc (&r, VOIDmode, &x);
1600 break;
1602 default:
1603 gcc_unreachable ();
1606 /* If R is NaN, return zero and show we have an overflow. */
1607 if (REAL_VALUE_ISNAN (r))
1609 overflow = true;
1610 val = wi::zero (TYPE_PRECISION (type));
1613 /* See if R is less than the lower bound or greater than the
1614 upper bound. */
1616 if (! overflow)
1618 tree lt = TYPE_MIN_VALUE (type);
1619 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1620 if (REAL_VALUES_LESS (r, l))
1622 overflow = true;
1623 val = lt;
1627 if (! overflow)
1629 tree ut = TYPE_MAX_VALUE (type);
1630 if (ut)
1632 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1633 if (REAL_VALUES_LESS (u, r))
1635 overflow = true;
1636 val = ut;
1641 if (! overflow)
1642 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1644 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1645 return t;
1648 /* A subroutine of fold_convert_const handling conversions of a
1649 FIXED_CST to an integer type. */
1651 static tree
1652 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1654 tree t;
1655 double_int temp, temp_trunc;
1656 unsigned int mode;
1658 /* Right shift FIXED_CST to temp by fbit. */
1659 temp = TREE_FIXED_CST (arg1).data;
1660 mode = TREE_FIXED_CST (arg1).mode;
1661 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1663 temp = temp.rshift (GET_MODE_FBIT (mode),
1664 HOST_BITS_PER_DOUBLE_INT,
1665 SIGNED_FIXED_POINT_MODE_P (mode));
1667 /* Left shift temp to temp_trunc by fbit. */
1668 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1669 HOST_BITS_PER_DOUBLE_INT,
1670 SIGNED_FIXED_POINT_MODE_P (mode));
1672 else
1674 temp = double_int_zero;
1675 temp_trunc = double_int_zero;
1678 /* If FIXED_CST is negative, we need to round the value toward 0.
1679 By checking if the fractional bits are not zero to add 1 to temp. */
1680 if (SIGNED_FIXED_POINT_MODE_P (mode)
1681 && temp_trunc.is_negative ()
1682 && TREE_FIXED_CST (arg1).data != temp_trunc)
1683 temp += double_int_one;
1685 /* Given a fixed-point constant, make new constant with new type,
1686 appropriately sign-extended or truncated. */
1687 t = force_fit_type (type, temp, -1,
1688 (temp.is_negative ()
1689 && (TYPE_UNSIGNED (type)
1690 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1691 | TREE_OVERFLOW (arg1));
1693 return t;
1696 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1697 to another floating point type. */
1699 static tree
1700 fold_convert_const_real_from_real (tree type, const_tree arg1)
1702 REAL_VALUE_TYPE value;
1703 tree t;
1705 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1706 t = build_real (type, value);
1708 /* If converting an infinity or NAN to a representation that doesn't
1709 have one, set the overflow bit so that we can produce some kind of
1710 error message at the appropriate point if necessary. It's not the
1711 most user-friendly message, but it's better than nothing. */
1712 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1713 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1714 TREE_OVERFLOW (t) = 1;
1715 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1716 && !MODE_HAS_NANS (TYPE_MODE (type)))
1717 TREE_OVERFLOW (t) = 1;
1718 /* Regular overflow, conversion produced an infinity in a mode that
1719 can't represent them. */
1720 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1721 && REAL_VALUE_ISINF (value)
1722 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1723 TREE_OVERFLOW (t) = 1;
1724 else
1725 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1726 return t;
1729 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1730 to a floating point type. */
1732 static tree
1733 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1735 REAL_VALUE_TYPE value;
1736 tree t;
1738 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1739 t = build_real (type, value);
1741 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1742 return t;
1745 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1746 to another fixed-point type. */
1748 static tree
1749 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1751 FIXED_VALUE_TYPE value;
1752 tree t;
1753 bool overflow_p;
1755 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1756 TYPE_SATURATING (type));
1757 t = build_fixed (type, value);
1759 /* Propagate overflow flags. */
1760 if (overflow_p | TREE_OVERFLOW (arg1))
1761 TREE_OVERFLOW (t) = 1;
1762 return t;
1765 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1766 to a fixed-point type. */
1768 static tree
1769 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1771 FIXED_VALUE_TYPE value;
1772 tree t;
1773 bool overflow_p;
1774 double_int di;
1776 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
1778 di.low = TREE_INT_CST_ELT (arg1, 0);
1779 if (TREE_INT_CST_NUNITS (arg1) == 1)
1780 di.high = (HOST_WIDE_INT) di.low < 0 ? (HOST_WIDE_INT) -1 : 0;
1781 else
1782 di.high = TREE_INT_CST_ELT (arg1, 1);
1784 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
1785 TYPE_UNSIGNED (TREE_TYPE (arg1)),
1786 TYPE_SATURATING (type));
1787 t = build_fixed (type, value);
1789 /* Propagate overflow flags. */
1790 if (overflow_p | TREE_OVERFLOW (arg1))
1791 TREE_OVERFLOW (t) = 1;
1792 return t;
1795 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1796 to a fixed-point type. */
1798 static tree
1799 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1801 FIXED_VALUE_TYPE value;
1802 tree t;
1803 bool overflow_p;
1805 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1806 &TREE_REAL_CST (arg1),
1807 TYPE_SATURATING (type));
1808 t = build_fixed (type, value);
1810 /* Propagate overflow flags. */
1811 if (overflow_p | TREE_OVERFLOW (arg1))
1812 TREE_OVERFLOW (t) = 1;
1813 return t;
1816 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1817 type TYPE. If no simplification can be done return NULL_TREE. */
1819 static tree
1820 fold_convert_const (enum tree_code code, tree type, tree arg1)
1822 if (TREE_TYPE (arg1) == type)
1823 return arg1;
1825 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1826 || TREE_CODE (type) == OFFSET_TYPE)
1828 if (TREE_CODE (arg1) == INTEGER_CST)
1829 return fold_convert_const_int_from_int (type, arg1);
1830 else if (TREE_CODE (arg1) == REAL_CST)
1831 return fold_convert_const_int_from_real (code, type, arg1);
1832 else if (TREE_CODE (arg1) == FIXED_CST)
1833 return fold_convert_const_int_from_fixed (type, arg1);
1835 else if (TREE_CODE (type) == REAL_TYPE)
1837 if (TREE_CODE (arg1) == INTEGER_CST)
1838 return build_real_from_int_cst (type, arg1);
1839 else if (TREE_CODE (arg1) == REAL_CST)
1840 return fold_convert_const_real_from_real (type, arg1);
1841 else if (TREE_CODE (arg1) == FIXED_CST)
1842 return fold_convert_const_real_from_fixed (type, arg1);
1844 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1846 if (TREE_CODE (arg1) == FIXED_CST)
1847 return fold_convert_const_fixed_from_fixed (type, arg1);
1848 else if (TREE_CODE (arg1) == INTEGER_CST)
1849 return fold_convert_const_fixed_from_int (type, arg1);
1850 else if (TREE_CODE (arg1) == REAL_CST)
1851 return fold_convert_const_fixed_from_real (type, arg1);
1853 return NULL_TREE;
1856 /* Construct a vector of zero elements of vector type TYPE. */
1858 static tree
1859 build_zero_vector (tree type)
1861 tree t;
1863 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1864 return build_vector_from_val (type, t);
1867 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
1869 bool
1870 fold_convertible_p (const_tree type, const_tree arg)
1872 tree orig = TREE_TYPE (arg);
1874 if (type == orig)
1875 return true;
1877 if (TREE_CODE (arg) == ERROR_MARK
1878 || TREE_CODE (type) == ERROR_MARK
1879 || TREE_CODE (orig) == ERROR_MARK)
1880 return false;
1882 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1883 return true;
1885 switch (TREE_CODE (type))
1887 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1888 case POINTER_TYPE: case REFERENCE_TYPE:
1889 case OFFSET_TYPE:
1890 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1891 || TREE_CODE (orig) == OFFSET_TYPE)
1892 return true;
1893 return (TREE_CODE (orig) == VECTOR_TYPE
1894 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1896 case REAL_TYPE:
1897 case FIXED_POINT_TYPE:
1898 case COMPLEX_TYPE:
1899 case VECTOR_TYPE:
1900 case VOID_TYPE:
1901 return TREE_CODE (type) == TREE_CODE (orig);
1903 default:
1904 return false;
1908 /* Convert expression ARG to type TYPE. Used by the middle-end for
1909 simple conversions in preference to calling the front-end's convert. */
1911 tree
1912 fold_convert_loc (location_t loc, tree type, tree arg)
1914 tree orig = TREE_TYPE (arg);
1915 tree tem;
1917 if (type == orig)
1918 return arg;
1920 if (TREE_CODE (arg) == ERROR_MARK
1921 || TREE_CODE (type) == ERROR_MARK
1922 || TREE_CODE (orig) == ERROR_MARK)
1923 return error_mark_node;
1925 switch (TREE_CODE (type))
1927 case POINTER_TYPE:
1928 case REFERENCE_TYPE:
1929 /* Handle conversions between pointers to different address spaces. */
1930 if (POINTER_TYPE_P (orig)
1931 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1932 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1933 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1934 /* fall through */
1936 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1937 case OFFSET_TYPE:
1938 if (TREE_CODE (arg) == INTEGER_CST)
1940 tem = fold_convert_const (NOP_EXPR, type, arg);
1941 if (tem != NULL_TREE)
1942 return tem;
1944 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1945 || TREE_CODE (orig) == OFFSET_TYPE)
1946 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1947 if (TREE_CODE (orig) == COMPLEX_TYPE)
1948 return fold_convert_loc (loc, type,
1949 fold_build1_loc (loc, REALPART_EXPR,
1950 TREE_TYPE (orig), arg));
1951 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1952 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1953 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1955 case REAL_TYPE:
1956 if (TREE_CODE (arg) == INTEGER_CST)
1958 tem = fold_convert_const (FLOAT_EXPR, type, arg);
1959 if (tem != NULL_TREE)
1960 return tem;
1962 else if (TREE_CODE (arg) == REAL_CST)
1964 tem = fold_convert_const (NOP_EXPR, type, arg);
1965 if (tem != NULL_TREE)
1966 return tem;
1968 else if (TREE_CODE (arg) == FIXED_CST)
1970 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1971 if (tem != NULL_TREE)
1972 return tem;
1975 switch (TREE_CODE (orig))
1977 case INTEGER_TYPE:
1978 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1979 case POINTER_TYPE: case REFERENCE_TYPE:
1980 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1982 case REAL_TYPE:
1983 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1985 case FIXED_POINT_TYPE:
1986 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1988 case COMPLEX_TYPE:
1989 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1990 return fold_convert_loc (loc, type, tem);
1992 default:
1993 gcc_unreachable ();
1996 case FIXED_POINT_TYPE:
1997 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1998 || TREE_CODE (arg) == REAL_CST)
2000 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2001 if (tem != NULL_TREE)
2002 goto fold_convert_exit;
2005 switch (TREE_CODE (orig))
2007 case FIXED_POINT_TYPE:
2008 case INTEGER_TYPE:
2009 case ENUMERAL_TYPE:
2010 case BOOLEAN_TYPE:
2011 case REAL_TYPE:
2012 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2014 case COMPLEX_TYPE:
2015 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2016 return fold_convert_loc (loc, type, tem);
2018 default:
2019 gcc_unreachable ();
2022 case COMPLEX_TYPE:
2023 switch (TREE_CODE (orig))
2025 case INTEGER_TYPE:
2026 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2027 case POINTER_TYPE: case REFERENCE_TYPE:
2028 case REAL_TYPE:
2029 case FIXED_POINT_TYPE:
2030 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2031 fold_convert_loc (loc, TREE_TYPE (type), arg),
2032 fold_convert_loc (loc, TREE_TYPE (type),
2033 integer_zero_node));
2034 case COMPLEX_TYPE:
2036 tree rpart, ipart;
2038 if (TREE_CODE (arg) == COMPLEX_EXPR)
2040 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2041 TREE_OPERAND (arg, 0));
2042 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2043 TREE_OPERAND (arg, 1));
2044 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2047 arg = save_expr (arg);
2048 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2049 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2050 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2051 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2052 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2055 default:
2056 gcc_unreachable ();
2059 case VECTOR_TYPE:
2060 if (integer_zerop (arg))
2061 return build_zero_vector (type);
2062 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2063 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2064 || TREE_CODE (orig) == VECTOR_TYPE);
2065 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2067 case VOID_TYPE:
2068 tem = fold_ignored_result (arg);
2069 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2071 default:
2072 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2073 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2074 gcc_unreachable ();
2076 fold_convert_exit:
2077 protected_set_expr_location_unshare (tem, loc);
2078 return tem;
2081 /* Return false if expr can be assumed not to be an lvalue, true
2082 otherwise. */
2084 static bool
2085 maybe_lvalue_p (const_tree x)
2087 /* We only need to wrap lvalue tree codes. */
2088 switch (TREE_CODE (x))
2090 case VAR_DECL:
2091 case PARM_DECL:
2092 case RESULT_DECL:
2093 case LABEL_DECL:
2094 case FUNCTION_DECL:
2095 case SSA_NAME:
2097 case COMPONENT_REF:
2098 case MEM_REF:
2099 case INDIRECT_REF:
2100 case ARRAY_REF:
2101 case ARRAY_RANGE_REF:
2102 case BIT_FIELD_REF:
2103 case OBJ_TYPE_REF:
2105 case REALPART_EXPR:
2106 case IMAGPART_EXPR:
2107 case PREINCREMENT_EXPR:
2108 case PREDECREMENT_EXPR:
2109 case SAVE_EXPR:
2110 case TRY_CATCH_EXPR:
2111 case WITH_CLEANUP_EXPR:
2112 case COMPOUND_EXPR:
2113 case MODIFY_EXPR:
2114 case TARGET_EXPR:
2115 case COND_EXPR:
2116 case BIND_EXPR:
2117 break;
2119 default:
2120 /* Assume the worst for front-end tree codes. */
2121 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2122 break;
2123 return false;
2126 return true;
2129 /* Return an expr equal to X but certainly not valid as an lvalue. */
2131 tree
2132 non_lvalue_loc (location_t loc, tree x)
2134 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2135 us. */
2136 if (in_gimple_form)
2137 return x;
2139 if (! maybe_lvalue_p (x))
2140 return x;
2141 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2144 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2145 Zero means allow extended lvalues. */
2147 int pedantic_lvalues;
2149 /* When pedantic, return an expr equal to X but certainly not valid as a
2150 pedantic lvalue. Otherwise, return X. */
2152 static tree
2153 pedantic_non_lvalue_loc (location_t loc, tree x)
2155 if (pedantic_lvalues)
2156 return non_lvalue_loc (loc, x);
2158 return protected_set_expr_location_unshare (x, loc);
2161 /* Given a tree comparison code, return the code that is the logical inverse.
2162 It is generally not safe to do this for floating-point comparisons, except
2163 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2164 ERROR_MARK in this case. */
2166 enum tree_code
2167 invert_tree_comparison (enum tree_code code, bool honor_nans)
2169 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2170 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2171 return ERROR_MARK;
2173 switch (code)
2175 case EQ_EXPR:
2176 return NE_EXPR;
2177 case NE_EXPR:
2178 return EQ_EXPR;
2179 case GT_EXPR:
2180 return honor_nans ? UNLE_EXPR : LE_EXPR;
2181 case GE_EXPR:
2182 return honor_nans ? UNLT_EXPR : LT_EXPR;
2183 case LT_EXPR:
2184 return honor_nans ? UNGE_EXPR : GE_EXPR;
2185 case LE_EXPR:
2186 return honor_nans ? UNGT_EXPR : GT_EXPR;
2187 case LTGT_EXPR:
2188 return UNEQ_EXPR;
2189 case UNEQ_EXPR:
2190 return LTGT_EXPR;
2191 case UNGT_EXPR:
2192 return LE_EXPR;
2193 case UNGE_EXPR:
2194 return LT_EXPR;
2195 case UNLT_EXPR:
2196 return GE_EXPR;
2197 case UNLE_EXPR:
2198 return GT_EXPR;
2199 case ORDERED_EXPR:
2200 return UNORDERED_EXPR;
2201 case UNORDERED_EXPR:
2202 return ORDERED_EXPR;
2203 default:
2204 gcc_unreachable ();
2208 /* Similar, but return the comparison that results if the operands are
2209 swapped. This is safe for floating-point. */
2211 enum tree_code
2212 swap_tree_comparison (enum tree_code code)
2214 switch (code)
2216 case EQ_EXPR:
2217 case NE_EXPR:
2218 case ORDERED_EXPR:
2219 case UNORDERED_EXPR:
2220 case LTGT_EXPR:
2221 case UNEQ_EXPR:
2222 return code;
2223 case GT_EXPR:
2224 return LT_EXPR;
2225 case GE_EXPR:
2226 return LE_EXPR;
2227 case LT_EXPR:
2228 return GT_EXPR;
2229 case LE_EXPR:
2230 return GE_EXPR;
2231 case UNGT_EXPR:
2232 return UNLT_EXPR;
2233 case UNGE_EXPR:
2234 return UNLE_EXPR;
2235 case UNLT_EXPR:
2236 return UNGT_EXPR;
2237 case UNLE_EXPR:
2238 return UNGE_EXPR;
2239 default:
2240 gcc_unreachable ();
2245 /* Convert a comparison tree code from an enum tree_code representation
2246 into a compcode bit-based encoding. This function is the inverse of
2247 compcode_to_comparison. */
2249 static enum comparison_code
2250 comparison_to_compcode (enum tree_code code)
2252 switch (code)
2254 case LT_EXPR:
2255 return COMPCODE_LT;
2256 case EQ_EXPR:
2257 return COMPCODE_EQ;
2258 case LE_EXPR:
2259 return COMPCODE_LE;
2260 case GT_EXPR:
2261 return COMPCODE_GT;
2262 case NE_EXPR:
2263 return COMPCODE_NE;
2264 case GE_EXPR:
2265 return COMPCODE_GE;
2266 case ORDERED_EXPR:
2267 return COMPCODE_ORD;
2268 case UNORDERED_EXPR:
2269 return COMPCODE_UNORD;
2270 case UNLT_EXPR:
2271 return COMPCODE_UNLT;
2272 case UNEQ_EXPR:
2273 return COMPCODE_UNEQ;
2274 case UNLE_EXPR:
2275 return COMPCODE_UNLE;
2276 case UNGT_EXPR:
2277 return COMPCODE_UNGT;
2278 case LTGT_EXPR:
2279 return COMPCODE_LTGT;
2280 case UNGE_EXPR:
2281 return COMPCODE_UNGE;
2282 default:
2283 gcc_unreachable ();
2287 /* Convert a compcode bit-based encoding of a comparison operator back
2288 to GCC's enum tree_code representation. This function is the
2289 inverse of comparison_to_compcode. */
2291 static enum tree_code
2292 compcode_to_comparison (enum comparison_code code)
2294 switch (code)
2296 case COMPCODE_LT:
2297 return LT_EXPR;
2298 case COMPCODE_EQ:
2299 return EQ_EXPR;
2300 case COMPCODE_LE:
2301 return LE_EXPR;
2302 case COMPCODE_GT:
2303 return GT_EXPR;
2304 case COMPCODE_NE:
2305 return NE_EXPR;
2306 case COMPCODE_GE:
2307 return GE_EXPR;
2308 case COMPCODE_ORD:
2309 return ORDERED_EXPR;
2310 case COMPCODE_UNORD:
2311 return UNORDERED_EXPR;
2312 case COMPCODE_UNLT:
2313 return UNLT_EXPR;
2314 case COMPCODE_UNEQ:
2315 return UNEQ_EXPR;
2316 case COMPCODE_UNLE:
2317 return UNLE_EXPR;
2318 case COMPCODE_UNGT:
2319 return UNGT_EXPR;
2320 case COMPCODE_LTGT:
2321 return LTGT_EXPR;
2322 case COMPCODE_UNGE:
2323 return UNGE_EXPR;
2324 default:
2325 gcc_unreachable ();
2329 /* Return a tree for the comparison which is the combination of
2330 doing the AND or OR (depending on CODE) of the two operations LCODE
2331 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2332 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2333 if this makes the transformation invalid. */
2335 tree
2336 combine_comparisons (location_t loc,
2337 enum tree_code code, enum tree_code lcode,
2338 enum tree_code rcode, tree truth_type,
2339 tree ll_arg, tree lr_arg)
2341 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2342 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2343 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2344 int compcode;
2346 switch (code)
2348 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2349 compcode = lcompcode & rcompcode;
2350 break;
2352 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2353 compcode = lcompcode | rcompcode;
2354 break;
2356 default:
2357 return NULL_TREE;
2360 if (!honor_nans)
2362 /* Eliminate unordered comparisons, as well as LTGT and ORD
2363 which are not used unless the mode has NaNs. */
2364 compcode &= ~COMPCODE_UNORD;
2365 if (compcode == COMPCODE_LTGT)
2366 compcode = COMPCODE_NE;
2367 else if (compcode == COMPCODE_ORD)
2368 compcode = COMPCODE_TRUE;
2370 else if (flag_trapping_math)
2372 /* Check that the original operation and the optimized ones will trap
2373 under the same condition. */
2374 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2375 && (lcompcode != COMPCODE_EQ)
2376 && (lcompcode != COMPCODE_ORD);
2377 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2378 && (rcompcode != COMPCODE_EQ)
2379 && (rcompcode != COMPCODE_ORD);
2380 bool trap = (compcode & COMPCODE_UNORD) == 0
2381 && (compcode != COMPCODE_EQ)
2382 && (compcode != COMPCODE_ORD);
2384 /* In a short-circuited boolean expression the LHS might be
2385 such that the RHS, if evaluated, will never trap. For
2386 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2387 if neither x nor y is NaN. (This is a mixed blessing: for
2388 example, the expression above will never trap, hence
2389 optimizing it to x < y would be invalid). */
2390 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2391 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2392 rtrap = false;
2394 /* If the comparison was short-circuited, and only the RHS
2395 trapped, we may now generate a spurious trap. */
2396 if (rtrap && !ltrap
2397 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2398 return NULL_TREE;
2400 /* If we changed the conditions that cause a trap, we lose. */
2401 if ((ltrap || rtrap) != trap)
2402 return NULL_TREE;
2405 if (compcode == COMPCODE_TRUE)
2406 return constant_boolean_node (true, truth_type);
2407 else if (compcode == COMPCODE_FALSE)
2408 return constant_boolean_node (false, truth_type);
2409 else
2411 enum tree_code tcode;
2413 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2414 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2418 /* Return nonzero if two operands (typically of the same tree node)
2419 are necessarily equal. If either argument has side-effects this
2420 function returns zero. FLAGS modifies behavior as follows:
2422 If OEP_ONLY_CONST is set, only return nonzero for constants.
2423 This function tests whether the operands are indistinguishable;
2424 it does not test whether they are equal using C's == operation.
2425 The distinction is important for IEEE floating point, because
2426 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2427 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2429 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2430 even though it may hold multiple values during a function.
2431 This is because a GCC tree node guarantees that nothing else is
2432 executed between the evaluation of its "operands" (which may often
2433 be evaluated in arbitrary order). Hence if the operands themselves
2434 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2435 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2436 unset means assuming isochronic (or instantaneous) tree equivalence.
2437 Unless comparing arbitrary expression trees, such as from different
2438 statements, this flag can usually be left unset.
2440 If OEP_PURE_SAME is set, then pure functions with identical arguments
2441 are considered the same. It is used when the caller has other ways
2442 to ensure that global memory is unchanged in between. */
2445 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2447 /* If either is ERROR_MARK, they aren't equal. */
2448 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2449 || TREE_TYPE (arg0) == error_mark_node
2450 || TREE_TYPE (arg1) == error_mark_node)
2451 return 0;
2453 /* Similar, if either does not have a type (like a released SSA name),
2454 they aren't equal. */
2455 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2456 return 0;
2458 /* Check equality of integer constants before bailing out due to
2459 precision differences. */
2460 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2461 return tree_int_cst_equal (arg0, arg1);
2463 /* If both types don't have the same signedness, then we can't consider
2464 them equal. We must check this before the STRIP_NOPS calls
2465 because they may change the signedness of the arguments. As pointers
2466 strictly don't have a signedness, require either two pointers or
2467 two non-pointers as well. */
2468 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2469 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2470 return 0;
2472 /* We cannot consider pointers to different address space equal. */
2473 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2474 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2475 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2476 return 0;
2478 /* If both types don't have the same precision, then it is not safe
2479 to strip NOPs. */
2480 if (element_precision (TREE_TYPE (arg0))
2481 != element_precision (TREE_TYPE (arg1)))
2482 return 0;
2484 STRIP_NOPS (arg0);
2485 STRIP_NOPS (arg1);
2487 /* In case both args are comparisons but with different comparison
2488 code, try to swap the comparison operands of one arg to produce
2489 a match and compare that variant. */
2490 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2491 && COMPARISON_CLASS_P (arg0)
2492 && COMPARISON_CLASS_P (arg1))
2494 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2496 if (TREE_CODE (arg0) == swap_code)
2497 return operand_equal_p (TREE_OPERAND (arg0, 0),
2498 TREE_OPERAND (arg1, 1), flags)
2499 && operand_equal_p (TREE_OPERAND (arg0, 1),
2500 TREE_OPERAND (arg1, 0), flags);
2503 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2504 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2505 && !(CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1)))
2506 return 0;
2508 /* This is needed for conversions and for COMPONENT_REF.
2509 Might as well play it safe and always test this. */
2510 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2511 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2512 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2513 return 0;
2515 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2516 We don't care about side effects in that case because the SAVE_EXPR
2517 takes care of that for us. In all other cases, two expressions are
2518 equal if they have no side effects. If we have two identical
2519 expressions with side effects that should be treated the same due
2520 to the only side effects being identical SAVE_EXPR's, that will
2521 be detected in the recursive calls below.
2522 If we are taking an invariant address of two identical objects
2523 they are necessarily equal as well. */
2524 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2525 && (TREE_CODE (arg0) == SAVE_EXPR
2526 || (flags & OEP_CONSTANT_ADDRESS_OF)
2527 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2528 return 1;
2530 /* Next handle constant cases, those for which we can return 1 even
2531 if ONLY_CONST is set. */
2532 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2533 switch (TREE_CODE (arg0))
2535 case INTEGER_CST:
2536 return tree_int_cst_equal (arg0, arg1);
2538 case FIXED_CST:
2539 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2540 TREE_FIXED_CST (arg1));
2542 case REAL_CST:
2543 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2544 TREE_REAL_CST (arg1)))
2545 return 1;
2548 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2550 /* If we do not distinguish between signed and unsigned zero,
2551 consider them equal. */
2552 if (real_zerop (arg0) && real_zerop (arg1))
2553 return 1;
2555 return 0;
2557 case VECTOR_CST:
2559 unsigned i;
2561 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2562 return 0;
2564 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2566 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2567 VECTOR_CST_ELT (arg1, i), flags))
2568 return 0;
2570 return 1;
2573 case COMPLEX_CST:
2574 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2575 flags)
2576 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2577 flags));
2579 case STRING_CST:
2580 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2581 && ! memcmp (TREE_STRING_POINTER (arg0),
2582 TREE_STRING_POINTER (arg1),
2583 TREE_STRING_LENGTH (arg0)));
2585 case ADDR_EXPR:
2586 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2587 TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)
2588 ? OEP_CONSTANT_ADDRESS_OF : 0);
2589 default:
2590 break;
2593 if (flags & OEP_ONLY_CONST)
2594 return 0;
2596 /* Define macros to test an operand from arg0 and arg1 for equality and a
2597 variant that allows null and views null as being different from any
2598 non-null value. In the latter case, if either is null, the both
2599 must be; otherwise, do the normal comparison. */
2600 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2601 TREE_OPERAND (arg1, N), flags)
2603 #define OP_SAME_WITH_NULL(N) \
2604 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2605 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2607 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2609 case tcc_unary:
2610 /* Two conversions are equal only if signedness and modes match. */
2611 switch (TREE_CODE (arg0))
2613 CASE_CONVERT:
2614 case FIX_TRUNC_EXPR:
2615 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2616 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2617 return 0;
2618 break;
2619 default:
2620 break;
2623 return OP_SAME (0);
2626 case tcc_comparison:
2627 case tcc_binary:
2628 if (OP_SAME (0) && OP_SAME (1))
2629 return 1;
2631 /* For commutative ops, allow the other order. */
2632 return (commutative_tree_code (TREE_CODE (arg0))
2633 && operand_equal_p (TREE_OPERAND (arg0, 0),
2634 TREE_OPERAND (arg1, 1), flags)
2635 && operand_equal_p (TREE_OPERAND (arg0, 1),
2636 TREE_OPERAND (arg1, 0), flags));
2638 case tcc_reference:
2639 /* If either of the pointer (or reference) expressions we are
2640 dereferencing contain a side effect, these cannot be equal,
2641 but their addresses can be. */
2642 if ((flags & OEP_CONSTANT_ADDRESS_OF) == 0
2643 && (TREE_SIDE_EFFECTS (arg0)
2644 || TREE_SIDE_EFFECTS (arg1)))
2645 return 0;
2647 switch (TREE_CODE (arg0))
2649 case INDIRECT_REF:
2650 flags &= ~OEP_CONSTANT_ADDRESS_OF;
2651 return OP_SAME (0);
2653 case REALPART_EXPR:
2654 case IMAGPART_EXPR:
2655 return OP_SAME (0);
2657 case TARGET_MEM_REF:
2658 flags &= ~OEP_CONSTANT_ADDRESS_OF;
2659 /* Require equal extra operands and then fall through to MEM_REF
2660 handling of the two common operands. */
2661 if (!OP_SAME_WITH_NULL (2)
2662 || !OP_SAME_WITH_NULL (3)
2663 || !OP_SAME_WITH_NULL (4))
2664 return 0;
2665 /* Fallthru. */
2666 case MEM_REF:
2667 flags &= ~OEP_CONSTANT_ADDRESS_OF;
2668 /* Require equal access sizes, and similar pointer types.
2669 We can have incomplete types for array references of
2670 variable-sized arrays from the Fortran frontend
2671 though. Also verify the types are compatible. */
2672 return ((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2673 || (TYPE_SIZE (TREE_TYPE (arg0))
2674 && TYPE_SIZE (TREE_TYPE (arg1))
2675 && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2676 TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2677 && types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1))
2678 && alias_ptr_types_compatible_p
2679 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
2680 TREE_TYPE (TREE_OPERAND (arg1, 1)))
2681 && OP_SAME (0) && OP_SAME (1));
2683 case ARRAY_REF:
2684 case ARRAY_RANGE_REF:
2685 /* Operands 2 and 3 may be null.
2686 Compare the array index by value if it is constant first as we
2687 may have different types but same value here. */
2688 if (!OP_SAME (0))
2689 return 0;
2690 flags &= ~OEP_CONSTANT_ADDRESS_OF;
2691 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2692 TREE_OPERAND (arg1, 1))
2693 || OP_SAME (1))
2694 && OP_SAME_WITH_NULL (2)
2695 && OP_SAME_WITH_NULL (3));
2697 case COMPONENT_REF:
2698 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2699 may be NULL when we're called to compare MEM_EXPRs. */
2700 if (!OP_SAME_WITH_NULL (0)
2701 || !OP_SAME (1))
2702 return 0;
2703 flags &= ~OEP_CONSTANT_ADDRESS_OF;
2704 return OP_SAME_WITH_NULL (2);
2706 case BIT_FIELD_REF:
2707 if (!OP_SAME (0))
2708 return 0;
2709 flags &= ~OEP_CONSTANT_ADDRESS_OF;
2710 return OP_SAME (1) && OP_SAME (2);
2712 default:
2713 return 0;
2716 case tcc_expression:
2717 switch (TREE_CODE (arg0))
2719 case ADDR_EXPR:
2720 case TRUTH_NOT_EXPR:
2721 return OP_SAME (0);
2723 case TRUTH_ANDIF_EXPR:
2724 case TRUTH_ORIF_EXPR:
2725 return OP_SAME (0) && OP_SAME (1);
2727 case FMA_EXPR:
2728 case WIDEN_MULT_PLUS_EXPR:
2729 case WIDEN_MULT_MINUS_EXPR:
2730 if (!OP_SAME (2))
2731 return 0;
2732 /* The multiplcation operands are commutative. */
2733 /* FALLTHRU */
2735 case TRUTH_AND_EXPR:
2736 case TRUTH_OR_EXPR:
2737 case TRUTH_XOR_EXPR:
2738 if (OP_SAME (0) && OP_SAME (1))
2739 return 1;
2741 /* Otherwise take into account this is a commutative operation. */
2742 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2743 TREE_OPERAND (arg1, 1), flags)
2744 && operand_equal_p (TREE_OPERAND (arg0, 1),
2745 TREE_OPERAND (arg1, 0), flags));
2747 case COND_EXPR:
2748 case VEC_COND_EXPR:
2749 case DOT_PROD_EXPR:
2750 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2752 default:
2753 return 0;
2756 case tcc_vl_exp:
2757 switch (TREE_CODE (arg0))
2759 case CALL_EXPR:
2760 /* If the CALL_EXPRs call different functions, then they
2761 clearly can not be equal. */
2762 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2763 flags))
2764 return 0;
2767 unsigned int cef = call_expr_flags (arg0);
2768 if (flags & OEP_PURE_SAME)
2769 cef &= ECF_CONST | ECF_PURE;
2770 else
2771 cef &= ECF_CONST;
2772 if (!cef)
2773 return 0;
2776 /* Now see if all the arguments are the same. */
2778 const_call_expr_arg_iterator iter0, iter1;
2779 const_tree a0, a1;
2780 for (a0 = first_const_call_expr_arg (arg0, &iter0),
2781 a1 = first_const_call_expr_arg (arg1, &iter1);
2782 a0 && a1;
2783 a0 = next_const_call_expr_arg (&iter0),
2784 a1 = next_const_call_expr_arg (&iter1))
2785 if (! operand_equal_p (a0, a1, flags))
2786 return 0;
2788 /* If we get here and both argument lists are exhausted
2789 then the CALL_EXPRs are equal. */
2790 return ! (a0 || a1);
2792 default:
2793 return 0;
2796 case tcc_declaration:
2797 /* Consider __builtin_sqrt equal to sqrt. */
2798 return (TREE_CODE (arg0) == FUNCTION_DECL
2799 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2800 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2801 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2803 default:
2804 return 0;
2807 #undef OP_SAME
2808 #undef OP_SAME_WITH_NULL
2811 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2812 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2814 When in doubt, return 0. */
2816 static int
2817 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2819 int unsignedp1, unsignedpo;
2820 tree primarg0, primarg1, primother;
2821 unsigned int correct_width;
2823 if (operand_equal_p (arg0, arg1, 0))
2824 return 1;
2826 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2827 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2828 return 0;
2830 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2831 and see if the inner values are the same. This removes any
2832 signedness comparison, which doesn't matter here. */
2833 primarg0 = arg0, primarg1 = arg1;
2834 STRIP_NOPS (primarg0);
2835 STRIP_NOPS (primarg1);
2836 if (operand_equal_p (primarg0, primarg1, 0))
2837 return 1;
2839 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2840 actual comparison operand, ARG0.
2842 First throw away any conversions to wider types
2843 already present in the operands. */
2845 primarg1 = get_narrower (arg1, &unsignedp1);
2846 primother = get_narrower (other, &unsignedpo);
2848 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2849 if (unsignedp1 == unsignedpo
2850 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2851 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2853 tree type = TREE_TYPE (arg0);
2855 /* Make sure shorter operand is extended the right way
2856 to match the longer operand. */
2857 primarg1 = fold_convert (signed_or_unsigned_type_for
2858 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2860 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2861 return 1;
2864 return 0;
2867 /* See if ARG is an expression that is either a comparison or is performing
2868 arithmetic on comparisons. The comparisons must only be comparing
2869 two different values, which will be stored in *CVAL1 and *CVAL2; if
2870 they are nonzero it means that some operands have already been found.
2871 No variables may be used anywhere else in the expression except in the
2872 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2873 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2875 If this is true, return 1. Otherwise, return zero. */
2877 static int
2878 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2880 enum tree_code code = TREE_CODE (arg);
2881 enum tree_code_class tclass = TREE_CODE_CLASS (code);
2883 /* We can handle some of the tcc_expression cases here. */
2884 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2885 tclass = tcc_unary;
2886 else if (tclass == tcc_expression
2887 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2888 || code == COMPOUND_EXPR))
2889 tclass = tcc_binary;
2891 else if (tclass == tcc_expression && code == SAVE_EXPR
2892 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2894 /* If we've already found a CVAL1 or CVAL2, this expression is
2895 two complex to handle. */
2896 if (*cval1 || *cval2)
2897 return 0;
2899 tclass = tcc_unary;
2900 *save_p = 1;
2903 switch (tclass)
2905 case tcc_unary:
2906 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2908 case tcc_binary:
2909 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2910 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2911 cval1, cval2, save_p));
2913 case tcc_constant:
2914 return 1;
2916 case tcc_expression:
2917 if (code == COND_EXPR)
2918 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2919 cval1, cval2, save_p)
2920 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2921 cval1, cval2, save_p)
2922 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2923 cval1, cval2, save_p));
2924 return 0;
2926 case tcc_comparison:
2927 /* First see if we can handle the first operand, then the second. For
2928 the second operand, we know *CVAL1 can't be zero. It must be that
2929 one side of the comparison is each of the values; test for the
2930 case where this isn't true by failing if the two operands
2931 are the same. */
2933 if (operand_equal_p (TREE_OPERAND (arg, 0),
2934 TREE_OPERAND (arg, 1), 0))
2935 return 0;
2937 if (*cval1 == 0)
2938 *cval1 = TREE_OPERAND (arg, 0);
2939 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2941 else if (*cval2 == 0)
2942 *cval2 = TREE_OPERAND (arg, 0);
2943 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2945 else
2946 return 0;
2948 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2950 else if (*cval2 == 0)
2951 *cval2 = TREE_OPERAND (arg, 1);
2952 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2954 else
2955 return 0;
2957 return 1;
2959 default:
2960 return 0;
2964 /* ARG is a tree that is known to contain just arithmetic operations and
2965 comparisons. Evaluate the operations in the tree substituting NEW0 for
2966 any occurrence of OLD0 as an operand of a comparison and likewise for
2967 NEW1 and OLD1. */
2969 static tree
2970 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2971 tree old1, tree new1)
2973 tree type = TREE_TYPE (arg);
2974 enum tree_code code = TREE_CODE (arg);
2975 enum tree_code_class tclass = TREE_CODE_CLASS (code);
2977 /* We can handle some of the tcc_expression cases here. */
2978 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2979 tclass = tcc_unary;
2980 else if (tclass == tcc_expression
2981 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2982 tclass = tcc_binary;
2984 switch (tclass)
2986 case tcc_unary:
2987 return fold_build1_loc (loc, code, type,
2988 eval_subst (loc, TREE_OPERAND (arg, 0),
2989 old0, new0, old1, new1));
2991 case tcc_binary:
2992 return fold_build2_loc (loc, code, type,
2993 eval_subst (loc, TREE_OPERAND (arg, 0),
2994 old0, new0, old1, new1),
2995 eval_subst (loc, TREE_OPERAND (arg, 1),
2996 old0, new0, old1, new1));
2998 case tcc_expression:
2999 switch (code)
3001 case SAVE_EXPR:
3002 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3003 old1, new1);
3005 case COMPOUND_EXPR:
3006 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3007 old1, new1);
3009 case COND_EXPR:
3010 return fold_build3_loc (loc, code, type,
3011 eval_subst (loc, TREE_OPERAND (arg, 0),
3012 old0, new0, old1, new1),
3013 eval_subst (loc, TREE_OPERAND (arg, 1),
3014 old0, new0, old1, new1),
3015 eval_subst (loc, TREE_OPERAND (arg, 2),
3016 old0, new0, old1, new1));
3017 default:
3018 break;
3020 /* Fall through - ??? */
3022 case tcc_comparison:
3024 tree arg0 = TREE_OPERAND (arg, 0);
3025 tree arg1 = TREE_OPERAND (arg, 1);
3027 /* We need to check both for exact equality and tree equality. The
3028 former will be true if the operand has a side-effect. In that
3029 case, we know the operand occurred exactly once. */
3031 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3032 arg0 = new0;
3033 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3034 arg0 = new1;
3036 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3037 arg1 = new0;
3038 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3039 arg1 = new1;
3041 return fold_build2_loc (loc, code, type, arg0, arg1);
3044 default:
3045 return arg;
3049 /* Return a tree for the case when the result of an expression is RESULT
3050 converted to TYPE and OMITTED was previously an operand of the expression
3051 but is now not needed (e.g., we folded OMITTED * 0).
3053 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3054 the conversion of RESULT to TYPE. */
3056 tree
3057 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3059 tree t = fold_convert_loc (loc, type, result);
3061 /* If the resulting operand is an empty statement, just return the omitted
3062 statement casted to void. */
3063 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3064 return build1_loc (loc, NOP_EXPR, void_type_node,
3065 fold_ignored_result (omitted));
3067 if (TREE_SIDE_EFFECTS (omitted))
3068 return build2_loc (loc, COMPOUND_EXPR, type,
3069 fold_ignored_result (omitted), t);
3071 return non_lvalue_loc (loc, t);
3074 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3076 static tree
3077 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
3078 tree omitted)
3080 tree t = fold_convert_loc (loc, type, result);
3082 /* If the resulting operand is an empty statement, just return the omitted
3083 statement casted to void. */
3084 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3085 return build1_loc (loc, NOP_EXPR, void_type_node,
3086 fold_ignored_result (omitted));
3088 if (TREE_SIDE_EFFECTS (omitted))
3089 return build2_loc (loc, COMPOUND_EXPR, type,
3090 fold_ignored_result (omitted), t);
3092 return pedantic_non_lvalue_loc (loc, t);
3095 /* Return a tree for the case when the result of an expression is RESULT
3096 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3097 of the expression but are now not needed.
3099 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3100 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3101 evaluated before OMITTED2. Otherwise, if neither has side effects,
3102 just do the conversion of RESULT to TYPE. */
3104 tree
3105 omit_two_operands_loc (location_t loc, tree type, tree result,
3106 tree omitted1, tree omitted2)
3108 tree t = fold_convert_loc (loc, type, result);
3110 if (TREE_SIDE_EFFECTS (omitted2))
3111 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3112 if (TREE_SIDE_EFFECTS (omitted1))
3113 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3115 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3119 /* Return a simplified tree node for the truth-negation of ARG. This
3120 never alters ARG itself. We assume that ARG is an operation that
3121 returns a truth value (0 or 1).
3123 FIXME: one would think we would fold the result, but it causes
3124 problems with the dominator optimizer. */
3126 static tree
3127 fold_truth_not_expr (location_t loc, tree arg)
3129 tree type = TREE_TYPE (arg);
3130 enum tree_code code = TREE_CODE (arg);
3131 location_t loc1, loc2;
3133 /* If this is a comparison, we can simply invert it, except for
3134 floating-point non-equality comparisons, in which case we just
3135 enclose a TRUTH_NOT_EXPR around what we have. */
3137 if (TREE_CODE_CLASS (code) == tcc_comparison)
3139 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3140 if (FLOAT_TYPE_P (op_type)
3141 && flag_trapping_math
3142 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3143 && code != NE_EXPR && code != EQ_EXPR)
3144 return NULL_TREE;
3146 code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3147 if (code == ERROR_MARK)
3148 return NULL_TREE;
3150 return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3151 TREE_OPERAND (arg, 1));
3154 switch (code)
3156 case INTEGER_CST:
3157 return constant_boolean_node (integer_zerop (arg), type);
3159 case TRUTH_AND_EXPR:
3160 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3161 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3162 return build2_loc (loc, TRUTH_OR_EXPR, type,
3163 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3164 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3166 case TRUTH_OR_EXPR:
3167 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3168 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3169 return build2_loc (loc, TRUTH_AND_EXPR, type,
3170 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3171 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3173 case TRUTH_XOR_EXPR:
3174 /* Here we can invert either operand. We invert the first operand
3175 unless the second operand is a TRUTH_NOT_EXPR in which case our
3176 result is the XOR of the first operand with the inside of the
3177 negation of the second operand. */
3179 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3180 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3181 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3182 else
3183 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3184 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3185 TREE_OPERAND (arg, 1));
3187 case TRUTH_ANDIF_EXPR:
3188 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3189 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3190 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3191 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3192 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3194 case TRUTH_ORIF_EXPR:
3195 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3196 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3197 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3198 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3199 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3201 case TRUTH_NOT_EXPR:
3202 return TREE_OPERAND (arg, 0);
3204 case COND_EXPR:
3206 tree arg1 = TREE_OPERAND (arg, 1);
3207 tree arg2 = TREE_OPERAND (arg, 2);
3209 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3210 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3212 /* A COND_EXPR may have a throw as one operand, which
3213 then has void type. Just leave void operands
3214 as they are. */
3215 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3216 VOID_TYPE_P (TREE_TYPE (arg1))
3217 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3218 VOID_TYPE_P (TREE_TYPE (arg2))
3219 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3222 case COMPOUND_EXPR:
3223 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3224 return build2_loc (loc, COMPOUND_EXPR, type,
3225 TREE_OPERAND (arg, 0),
3226 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3228 case NON_LVALUE_EXPR:
3229 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3230 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3232 CASE_CONVERT:
3233 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3234 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3236 /* ... fall through ... */
3238 case FLOAT_EXPR:
3239 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3240 return build1_loc (loc, TREE_CODE (arg), type,
3241 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3243 case BIT_AND_EXPR:
3244 if (!integer_onep (TREE_OPERAND (arg, 1)))
3245 return NULL_TREE;
3246 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3248 case SAVE_EXPR:
3249 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3251 case CLEANUP_POINT_EXPR:
3252 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3253 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3254 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3256 default:
3257 return NULL_TREE;
3261 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3262 assume that ARG is an operation that returns a truth value (0 or 1
3263 for scalars, 0 or -1 for vectors). Return the folded expression if
3264 folding is successful. Otherwise, return NULL_TREE. */
3266 static tree
3267 fold_invert_truthvalue (location_t loc, tree arg)
3269 tree type = TREE_TYPE (arg);
3270 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3271 ? BIT_NOT_EXPR
3272 : TRUTH_NOT_EXPR,
3273 type, arg);
3276 /* Return a simplified tree node for the truth-negation of ARG. This
3277 never alters ARG itself. We assume that ARG is an operation that
3278 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3280 tree
3281 invert_truthvalue_loc (location_t loc, tree arg)
3283 if (TREE_CODE (arg) == ERROR_MARK)
3284 return arg;
3286 tree type = TREE_TYPE (arg);
3287 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3288 ? BIT_NOT_EXPR
3289 : TRUTH_NOT_EXPR,
3290 type, arg);
3293 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3294 operands are another bit-wise operation with a common input. If so,
3295 distribute the bit operations to save an operation and possibly two if
3296 constants are involved. For example, convert
3297 (A | B) & (A | C) into A | (B & C)
3298 Further simplification will occur if B and C are constants.
3300 If this optimization cannot be done, 0 will be returned. */
3302 static tree
3303 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3304 tree arg0, tree arg1)
3306 tree common;
3307 tree left, right;
3309 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3310 || TREE_CODE (arg0) == code
3311 || (TREE_CODE (arg0) != BIT_AND_EXPR
3312 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3313 return 0;
3315 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3317 common = TREE_OPERAND (arg0, 0);
3318 left = TREE_OPERAND (arg0, 1);
3319 right = TREE_OPERAND (arg1, 1);
3321 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3323 common = TREE_OPERAND (arg0, 0);
3324 left = TREE_OPERAND (arg0, 1);
3325 right = TREE_OPERAND (arg1, 0);
3327 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3329 common = TREE_OPERAND (arg0, 1);
3330 left = TREE_OPERAND (arg0, 0);
3331 right = TREE_OPERAND (arg1, 1);
3333 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3335 common = TREE_OPERAND (arg0, 1);
3336 left = TREE_OPERAND (arg0, 0);
3337 right = TREE_OPERAND (arg1, 0);
3339 else
3340 return 0;
3342 common = fold_convert_loc (loc, type, common);
3343 left = fold_convert_loc (loc, type, left);
3344 right = fold_convert_loc (loc, type, right);
3345 return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3346 fold_build2_loc (loc, code, type, left, right));
3349 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3350 with code CODE. This optimization is unsafe. */
3351 static tree
3352 distribute_real_division (location_t loc, enum tree_code code, tree type,
3353 tree arg0, tree arg1)
3355 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3356 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3358 /* (A / C) +- (B / C) -> (A +- B) / C. */
3359 if (mul0 == mul1
3360 && operand_equal_p (TREE_OPERAND (arg0, 1),
3361 TREE_OPERAND (arg1, 1), 0))
3362 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3363 fold_build2_loc (loc, code, type,
3364 TREE_OPERAND (arg0, 0),
3365 TREE_OPERAND (arg1, 0)),
3366 TREE_OPERAND (arg0, 1));
3368 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3369 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3370 TREE_OPERAND (arg1, 0), 0)
3371 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3372 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3374 REAL_VALUE_TYPE r0, r1;
3375 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3376 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3377 if (!mul0)
3378 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3379 if (!mul1)
3380 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3381 real_arithmetic (&r0, code, &r0, &r1);
3382 return fold_build2_loc (loc, MULT_EXPR, type,
3383 TREE_OPERAND (arg0, 0),
3384 build_real (type, r0));
3387 return NULL_TREE;
3390 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3391 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3392 and uses reverse storage order if REVERSEP is nonzero. */
3394 static tree
3395 make_bit_field_ref (location_t loc, tree inner, tree type,
3396 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3397 int unsignedp, int reversep)
3399 tree result, bftype;
3401 if (bitpos == 0 && !reversep)
3403 tree size = TYPE_SIZE (TREE_TYPE (inner));
3404 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3405 || POINTER_TYPE_P (TREE_TYPE (inner)))
3406 && tree_fits_shwi_p (size)
3407 && tree_to_shwi (size) == bitsize)
3408 return fold_convert_loc (loc, type, inner);
3411 bftype = type;
3412 if (TYPE_PRECISION (bftype) != bitsize
3413 || TYPE_UNSIGNED (bftype) == !unsignedp)
3414 bftype = build_nonstandard_integer_type (bitsize, 0);
3416 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3417 size_int (bitsize), bitsize_int (bitpos));
3418 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3420 if (bftype != type)
3421 result = fold_convert_loc (loc, type, result);
3423 return result;
3426 /* Optimize a bit-field compare.
3428 There are two cases: First is a compare against a constant and the
3429 second is a comparison of two items where the fields are at the same
3430 bit position relative to the start of a chunk (byte, halfword, word)
3431 large enough to contain it. In these cases we can avoid the shift
3432 implicit in bitfield extractions.
3434 For constants, we emit a compare of the shifted constant with the
3435 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3436 compared. For two fields at the same position, we do the ANDs with the
3437 similar mask and compare the result of the ANDs.
3439 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3440 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3441 are the left and right operands of the comparison, respectively.
3443 If the optimization described above can be done, we return the resulting
3444 tree. Otherwise we return zero. */
3446 static tree
3447 optimize_bit_field_compare (location_t loc, enum tree_code code,
3448 tree compare_type, tree lhs, tree rhs)
3450 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3451 tree type = TREE_TYPE (lhs);
3452 tree unsigned_type;
3453 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3454 enum machine_mode lmode, rmode, nmode;
3455 int lunsignedp, runsignedp;
3456 int lreversep, rreversep;
3457 int lvolatilep = 0, rvolatilep = 0;
3458 tree linner, rinner = NULL_TREE;
3459 tree mask;
3460 tree offset;
3462 /* Get all the information about the extractions being done. If the bit size
3463 if the same as the size of the underlying object, we aren't doing an
3464 extraction at all and so can do nothing. We also don't want to
3465 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3466 then will no longer be able to replace it. */
3467 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3468 &lunsignedp, &lreversep, &lvolatilep, false);
3469 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3470 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3471 return 0;
3473 if (const_p)
3474 rreversep = lreversep;
3475 else
3477 /* If this is not a constant, we can only do something if bit positions,
3478 sizes, signedness and storage order are the same. */
3479 rinner
3480 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3481 &runsignedp, &rreversep, &rvolatilep, false);
3483 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3484 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3485 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3486 return 0;
3489 /* See if we can find a mode to refer to this field. We should be able to,
3490 but fail if we can't. */
3491 nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3492 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3493 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3494 TYPE_ALIGN (TREE_TYPE (rinner))),
3495 word_mode, false);
3496 if (nmode == VOIDmode)
3497 return 0;
3499 /* Set signed and unsigned types of the precision of this mode for the
3500 shifts below. */
3501 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3503 /* Compute the bit position and size for the new reference and our offset
3504 within it. If the new reference is the same size as the original, we
3505 won't optimize anything, so return zero. */
3506 nbitsize = GET_MODE_BITSIZE (nmode);
3507 nbitpos = lbitpos & ~ (nbitsize - 1);
3508 lbitpos -= nbitpos;
3509 if (nbitsize == lbitsize)
3510 return 0;
3512 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
3513 lbitpos = nbitsize - lbitsize - lbitpos;
3515 /* Make the mask to be used against the extracted field. */
3516 mask = build_int_cst_type (unsigned_type, -1);
3517 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3518 mask = const_binop (RSHIFT_EXPR, mask,
3519 size_int (nbitsize - lbitsize - lbitpos));
3521 if (! const_p)
3522 /* If not comparing with constant, just rework the comparison
3523 and return. */
3524 return fold_build2_loc (loc, code, compare_type,
3525 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3526 make_bit_field_ref (loc, linner,
3527 unsigned_type,
3528 nbitsize, nbitpos,
3529 1, lreversep),
3530 mask),
3531 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3532 make_bit_field_ref (loc, rinner,
3533 unsigned_type,
3534 nbitsize, nbitpos,
3535 1, rreversep),
3536 mask));
3538 /* Otherwise, we are handling the constant case. See if the constant is too
3539 big for the field. Warn and return a tree for 0 (false) if so. We do
3540 this not only for its own sake, but to avoid having to test for this
3541 error case below. If we didn't, we might generate wrong code.
3543 For unsigned fields, the constant shifted right by the field length should
3544 be all zero. For signed fields, the high-order bits should agree with
3545 the sign bit. */
3547 if (lunsignedp)
3549 if (wi::lrshift (rhs, lbitsize) != 0)
3551 warning (0, "comparison is always %d due to width of bit-field",
3552 code == NE_EXPR);
3553 return constant_boolean_node (code == NE_EXPR, compare_type);
3556 else
3558 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3559 if (tem != 0 && tem != -1)
3561 warning (0, "comparison is always %d due to width of bit-field",
3562 code == NE_EXPR);
3563 return constant_boolean_node (code == NE_EXPR, compare_type);
3567 /* Single-bit compares should always be against zero. */
3568 if (lbitsize == 1 && ! integer_zerop (rhs))
3570 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3571 rhs = build_int_cst (type, 0);
3574 /* Make a new bitfield reference, shift the constant over the
3575 appropriate number of bits and mask it with the computed mask
3576 (in case this was a signed field). If we changed it, make a new one. */
3577 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1,
3578 lreversep);
3580 rhs = const_binop (BIT_AND_EXPR,
3581 const_binop (LSHIFT_EXPR,
3582 fold_convert_loc (loc, unsigned_type, rhs),
3583 size_int (lbitpos)),
3584 mask);
3586 lhs = build2_loc (loc, code, compare_type,
3587 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3588 return lhs;
3591 /* Subroutine for fold_truth_andor_1: decode a field reference.
3593 If EXP is a comparison reference, we return the innermost reference.
3595 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3596 set to the starting bit number.
3598 If the innermost field can be completely contained in a mode-sized
3599 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3601 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3602 otherwise it is not changed.
3604 *PUNSIGNEDP is set to the signedness of the field.
3606 *PREVERSEP is set to the storage order of the field.
3608 *PMASK is set to the mask used. This is either contained in a
3609 BIT_AND_EXPR or derived from the width of the field.
3611 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3613 Return 0 if this is not a component reference or is one that we can't
3614 do anything with. */
3616 static tree
3617 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3618 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3619 int *punsignedp, int *preversep, int *pvolatilep,
3620 tree *pmask, tree *pand_mask)
3622 tree outer_type = 0;
3623 tree and_mask = 0;
3624 tree mask, inner, offset;
3625 tree unsigned_type;
3626 unsigned int precision;
3628 /* All the optimizations using this function assume integer fields.
3629 There are problems with FP fields since the type_for_size call
3630 below can fail for, e.g., XFmode. */
3631 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3632 return 0;
3634 /* We are interested in the bare arrangement of bits, so strip everything
3635 that doesn't affect the machine mode. However, record the type of the
3636 outermost expression if it may matter below. */
3637 if (CONVERT_EXPR_P (exp)
3638 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3639 outer_type = TREE_TYPE (exp);
3640 STRIP_NOPS (exp);
3642 if (TREE_CODE (exp) == BIT_AND_EXPR)
3644 and_mask = TREE_OPERAND (exp, 1);
3645 exp = TREE_OPERAND (exp, 0);
3646 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3647 if (TREE_CODE (and_mask) != INTEGER_CST)
3648 return 0;
3651 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3652 punsignedp, preversep, pvolatilep, false);
3653 if ((inner == exp && and_mask == 0)
3654 || *pbitsize < 0 || offset != 0
3655 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3656 return 0;
3658 /* If the number of bits in the reference is the same as the bitsize of
3659 the outer type, then the outer type gives the signedness. Otherwise
3660 (in case of a small bitfield) the signedness is unchanged. */
3661 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3662 *punsignedp = TYPE_UNSIGNED (outer_type);
3664 /* Compute the mask to access the bitfield. */
3665 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3666 precision = TYPE_PRECISION (unsigned_type);
3668 mask = build_int_cst_type (unsigned_type, -1);
3670 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3671 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3673 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3674 if (and_mask != 0)
3675 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3676 fold_convert_loc (loc, unsigned_type, and_mask), mask);
3678 *pmask = mask;
3679 *pand_mask = and_mask;
3680 return inner;
3683 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3684 bit positions and MASK is SIGNED. */
3686 static int
3687 all_ones_mask_p (const_tree mask, unsigned int size)
3689 tree type = TREE_TYPE (mask);
3690 unsigned int precision = TYPE_PRECISION (type);
3692 /* If this function returns true when the type of the mask is
3693 UNSIGNED, then there will be errors. In particular see
3694 gcc.c-torture/execute/990326-1.c. There does not appear to be
3695 any documentation paper trail as to why this is so. But the pre
3696 wide-int worked with that restriction and it has been preserved
3697 here. */
3698 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
3699 return false;
3701 return wi::mask (size, false, precision) == mask;
3704 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3705 represents the sign bit of EXP's type. If EXP represents a sign
3706 or zero extension, also test VAL against the unextended type.
3707 The return value is the (sub)expression whose sign bit is VAL,
3708 or NULL_TREE otherwise. */
3710 static tree
3711 sign_bit_p (tree exp, const_tree val)
3713 int width;
3714 tree t;
3716 /* Tree EXP must have an integral type. */
3717 t = TREE_TYPE (exp);
3718 if (! INTEGRAL_TYPE_P (t))
3719 return NULL_TREE;
3721 /* Tree VAL must be an integer constant. */
3722 if (TREE_CODE (val) != INTEGER_CST
3723 || TREE_OVERFLOW (val))
3724 return NULL_TREE;
3726 width = TYPE_PRECISION (t);
3727 if (wi::only_sign_bit_p (val, width))
3728 return exp;
3730 /* Handle extension from a narrower type. */
3731 if (TREE_CODE (exp) == NOP_EXPR
3732 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3733 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3735 return NULL_TREE;
3738 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
3739 to be evaluated unconditionally. */
3741 static int
3742 simple_operand_p (const_tree exp)
3744 /* Strip any conversions that don't change the machine mode. */
3745 STRIP_NOPS (exp);
3747 return (CONSTANT_CLASS_P (exp)
3748 || TREE_CODE (exp) == SSA_NAME
3749 || (DECL_P (exp)
3750 && ! TREE_ADDRESSABLE (exp)
3751 && ! TREE_THIS_VOLATILE (exp)
3752 && ! DECL_NONLOCAL (exp)
3753 /* Don't regard global variables as simple. They may be
3754 allocated in ways unknown to the compiler (shared memory,
3755 #pragma weak, etc). */
3756 && ! TREE_PUBLIC (exp)
3757 && ! DECL_EXTERNAL (exp)
3758 /* Weakrefs are not safe to be read, since they can be NULL.
3759 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
3760 have DECL_WEAK flag set. */
3761 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
3762 /* Loading a static variable is unduly expensive, but global
3763 registers aren't expensive. */
3764 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3767 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
3768 to be evaluated unconditionally.
3769 I addition to simple_operand_p, we assume that comparisons, conversions,
3770 and logic-not operations are simple, if their operands are simple, too. */
3772 static bool
3773 simple_operand_p_2 (tree exp)
3775 enum tree_code code;
3777 if (TREE_SIDE_EFFECTS (exp)
3778 || tree_could_trap_p (exp))
3779 return false;
3781 while (CONVERT_EXPR_P (exp))
3782 exp = TREE_OPERAND (exp, 0);
3784 code = TREE_CODE (exp);
3786 if (TREE_CODE_CLASS (code) == tcc_comparison)
3787 return (simple_operand_p (TREE_OPERAND (exp, 0))
3788 && simple_operand_p (TREE_OPERAND (exp, 1)));
3790 if (code == TRUTH_NOT_EXPR)
3791 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
3793 return simple_operand_p (exp);
3797 /* The following functions are subroutines to fold_range_test and allow it to
3798 try to change a logical combination of comparisons into a range test.
3800 For example, both
3801 X == 2 || X == 3 || X == 4 || X == 5
3803 X >= 2 && X <= 5
3804 are converted to
3805 (unsigned) (X - 2) <= 3
3807 We describe each set of comparisons as being either inside or outside
3808 a range, using a variable named like IN_P, and then describe the
3809 range with a lower and upper bound. If one of the bounds is omitted,
3810 it represents either the highest or lowest value of the type.
3812 In the comments below, we represent a range by two numbers in brackets
3813 preceded by a "+" to designate being inside that range, or a "-" to
3814 designate being outside that range, so the condition can be inverted by
3815 flipping the prefix. An omitted bound is represented by a "-". For
3816 example, "- [-, 10]" means being outside the range starting at the lowest
3817 possible value and ending at 10, in other words, being greater than 10.
3818 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3819 always false.
3821 We set up things so that the missing bounds are handled in a consistent
3822 manner so neither a missing bound nor "true" and "false" need to be
3823 handled using a special case. */
3825 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3826 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3827 and UPPER1_P are nonzero if the respective argument is an upper bound
3828 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3829 must be specified for a comparison. ARG1 will be converted to ARG0's
3830 type if both are specified. */
3832 static tree
3833 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3834 tree arg1, int upper1_p)
3836 tree tem;
3837 int result;
3838 int sgn0, sgn1;
3840 /* If neither arg represents infinity, do the normal operation.
3841 Else, if not a comparison, return infinity. Else handle the special
3842 comparison rules. Note that most of the cases below won't occur, but
3843 are handled for consistency. */
3845 if (arg0 != 0 && arg1 != 0)
3847 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3848 arg0, fold_convert (TREE_TYPE (arg0), arg1));
3849 STRIP_NOPS (tem);
3850 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3853 if (TREE_CODE_CLASS (code) != tcc_comparison)
3854 return 0;
3856 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3857 for neither. In real maths, we cannot assume open ended ranges are
3858 the same. But, this is computer arithmetic, where numbers are finite.
3859 We can therefore make the transformation of any unbounded range with
3860 the value Z, Z being greater than any representable number. This permits
3861 us to treat unbounded ranges as equal. */
3862 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3863 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3864 switch (code)
3866 case EQ_EXPR:
3867 result = sgn0 == sgn1;
3868 break;
3869 case NE_EXPR:
3870 result = sgn0 != sgn1;
3871 break;
3872 case LT_EXPR:
3873 result = sgn0 < sgn1;
3874 break;
3875 case LE_EXPR:
3876 result = sgn0 <= sgn1;
3877 break;
3878 case GT_EXPR:
3879 result = sgn0 > sgn1;
3880 break;
3881 case GE_EXPR:
3882 result = sgn0 >= sgn1;
3883 break;
3884 default:
3885 gcc_unreachable ();
3888 return constant_boolean_node (result, type);
3891 /* Helper routine for make_range. Perform one step for it, return
3892 new expression if the loop should continue or NULL_TREE if it should
3893 stop. */
3895 tree
3896 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
3897 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
3898 bool *strict_overflow_p)
3900 tree arg0_type = TREE_TYPE (arg0);
3901 tree n_low, n_high, low = *p_low, high = *p_high;
3902 int in_p = *p_in_p, n_in_p;
3904 switch (code)
3906 case TRUTH_NOT_EXPR:
3907 /* We can only do something if the range is testing for zero. */
3908 if (low == NULL_TREE || high == NULL_TREE
3909 || ! integer_zerop (low) || ! integer_zerop (high))
3910 return NULL_TREE;
3911 *p_in_p = ! in_p;
3912 return arg0;
3914 case EQ_EXPR: case NE_EXPR:
3915 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3916 /* We can only do something if the range is testing for zero
3917 and if the second operand is an integer constant. Note that
3918 saying something is "in" the range we make is done by
3919 complementing IN_P since it will set in the initial case of
3920 being not equal to zero; "out" is leaving it alone. */
3921 if (low == NULL_TREE || high == NULL_TREE
3922 || ! integer_zerop (low) || ! integer_zerop (high)
3923 || TREE_CODE (arg1) != INTEGER_CST)
3924 return NULL_TREE;
3926 switch (code)
3928 case NE_EXPR: /* - [c, c] */
3929 low = high = arg1;
3930 break;
3931 case EQ_EXPR: /* + [c, c] */
3932 in_p = ! in_p, low = high = arg1;
3933 break;
3934 case GT_EXPR: /* - [-, c] */
3935 low = 0, high = arg1;
3936 break;
3937 case GE_EXPR: /* + [c, -] */
3938 in_p = ! in_p, low = arg1, high = 0;
3939 break;
3940 case LT_EXPR: /* - [c, -] */
3941 low = arg1, high = 0;
3942 break;
3943 case LE_EXPR: /* + [-, c] */
3944 in_p = ! in_p, low = 0, high = arg1;
3945 break;
3946 default:
3947 gcc_unreachable ();
3950 /* If this is an unsigned comparison, we also know that EXP is
3951 greater than or equal to zero. We base the range tests we make
3952 on that fact, so we record it here so we can parse existing
3953 range tests. We test arg0_type since often the return type
3954 of, e.g. EQ_EXPR, is boolean. */
3955 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3957 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3958 in_p, low, high, 1,
3959 build_int_cst (arg0_type, 0),
3960 NULL_TREE))
3961 return NULL_TREE;
3963 in_p = n_in_p, low = n_low, high = n_high;
3965 /* If the high bound is missing, but we have a nonzero low
3966 bound, reverse the range so it goes from zero to the low bound
3967 minus 1. */
3968 if (high == 0 && low && ! integer_zerop (low))
3970 in_p = ! in_p;
3971 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3972 build_int_cst (TREE_TYPE (low), 1), 0);
3973 low = build_int_cst (arg0_type, 0);
3977 *p_low = low;
3978 *p_high = high;
3979 *p_in_p = in_p;
3980 return arg0;
3982 case NEGATE_EXPR:
3983 /* If flag_wrapv and ARG0_TYPE is signed, make sure
3984 low and high are non-NULL, then normalize will DTRT. */
3985 if (!TYPE_UNSIGNED (arg0_type)
3986 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
3988 if (low == NULL_TREE)
3989 low = TYPE_MIN_VALUE (arg0_type);
3990 if (high == NULL_TREE)
3991 high = TYPE_MAX_VALUE (arg0_type);
3994 /* (-x) IN [a,b] -> x in [-b, -a] */
3995 n_low = range_binop (MINUS_EXPR, exp_type,
3996 build_int_cst (exp_type, 0),
3997 0, high, 1);
3998 n_high = range_binop (MINUS_EXPR, exp_type,
3999 build_int_cst (exp_type, 0),
4000 0, low, 0);
4001 if (n_high != 0 && TREE_OVERFLOW (n_high))
4002 return NULL_TREE;
4003 goto normalize;
4005 case BIT_NOT_EXPR:
4006 /* ~ X -> -X - 1 */
4007 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4008 build_int_cst (exp_type, 1));
4010 case PLUS_EXPR:
4011 case MINUS_EXPR:
4012 if (TREE_CODE (arg1) != INTEGER_CST)
4013 return NULL_TREE;
4015 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4016 move a constant to the other side. */
4017 if (!TYPE_UNSIGNED (arg0_type)
4018 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4019 return NULL_TREE;
4021 /* If EXP is signed, any overflow in the computation is undefined,
4022 so we don't worry about it so long as our computations on
4023 the bounds don't overflow. For unsigned, overflow is defined
4024 and this is exactly the right thing. */
4025 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4026 arg0_type, low, 0, arg1, 0);
4027 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4028 arg0_type, high, 1, arg1, 0);
4029 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4030 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4031 return NULL_TREE;
4033 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4034 *strict_overflow_p = true;
4036 normalize:
4037 /* Check for an unsigned range which has wrapped around the maximum
4038 value thus making n_high < n_low, and normalize it. */
4039 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4041 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4042 build_int_cst (TREE_TYPE (n_high), 1), 0);
4043 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4044 build_int_cst (TREE_TYPE (n_low), 1), 0);
4046 /* If the range is of the form +/- [ x+1, x ], we won't
4047 be able to normalize it. But then, it represents the
4048 whole range or the empty set, so make it
4049 +/- [ -, - ]. */
4050 if (tree_int_cst_equal (n_low, low)
4051 && tree_int_cst_equal (n_high, high))
4052 low = high = 0;
4053 else
4054 in_p = ! in_p;
4056 else
4057 low = n_low, high = n_high;
4059 *p_low = low;
4060 *p_high = high;
4061 *p_in_p = in_p;
4062 return arg0;
4064 CASE_CONVERT:
4065 case NON_LVALUE_EXPR:
4066 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4067 return NULL_TREE;
4069 if (! INTEGRAL_TYPE_P (arg0_type)
4070 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4071 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4072 return NULL_TREE;
4074 n_low = low, n_high = high;
4076 if (n_low != 0)
4077 n_low = fold_convert_loc (loc, arg0_type, n_low);
4079 if (n_high != 0)
4080 n_high = fold_convert_loc (loc, arg0_type, n_high);
4082 /* If we're converting arg0 from an unsigned type, to exp,
4083 a signed type, we will be doing the comparison as unsigned.
4084 The tests above have already verified that LOW and HIGH
4085 are both positive.
4087 So we have to ensure that we will handle large unsigned
4088 values the same way that the current signed bounds treat
4089 negative values. */
4091 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4093 tree high_positive;
4094 tree equiv_type;
4095 /* For fixed-point modes, we need to pass the saturating flag
4096 as the 2nd parameter. */
4097 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4098 equiv_type
4099 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4100 TYPE_SATURATING (arg0_type));
4101 else
4102 equiv_type
4103 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4105 /* A range without an upper bound is, naturally, unbounded.
4106 Since convert would have cropped a very large value, use
4107 the max value for the destination type. */
4108 high_positive
4109 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4110 : TYPE_MAX_VALUE (arg0_type);
4112 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4113 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4114 fold_convert_loc (loc, arg0_type,
4115 high_positive),
4116 build_int_cst (arg0_type, 1));
4118 /* If the low bound is specified, "and" the range with the
4119 range for which the original unsigned value will be
4120 positive. */
4121 if (low != 0)
4123 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4124 1, fold_convert_loc (loc, arg0_type,
4125 integer_zero_node),
4126 high_positive))
4127 return NULL_TREE;
4129 in_p = (n_in_p == in_p);
4131 else
4133 /* Otherwise, "or" the range with the range of the input
4134 that will be interpreted as negative. */
4135 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4136 1, fold_convert_loc (loc, arg0_type,
4137 integer_zero_node),
4138 high_positive))
4139 return NULL_TREE;
4141 in_p = (in_p != n_in_p);
4145 *p_low = n_low;
4146 *p_high = n_high;
4147 *p_in_p = in_p;
4148 return arg0;
4150 default:
4151 return NULL_TREE;
4155 /* Given EXP, a logical expression, set the range it is testing into
4156 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4157 actually being tested. *PLOW and *PHIGH will be made of the same
4158 type as the returned expression. If EXP is not a comparison, we
4159 will most likely not be returning a useful value and range. Set
4160 *STRICT_OVERFLOW_P to true if the return value is only valid
4161 because signed overflow is undefined; otherwise, do not change
4162 *STRICT_OVERFLOW_P. */
4164 tree
4165 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4166 bool *strict_overflow_p)
4168 enum tree_code code;
4169 tree arg0, arg1 = NULL_TREE;
4170 tree exp_type, nexp;
4171 int in_p;
4172 tree low, high;
4173 location_t loc = EXPR_LOCATION (exp);
4175 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4176 and see if we can refine the range. Some of the cases below may not
4177 happen, but it doesn't seem worth worrying about this. We "continue"
4178 the outer loop when we've changed something; otherwise we "break"
4179 the switch, which will "break" the while. */
4181 in_p = 0;
4182 low = high = build_int_cst (TREE_TYPE (exp), 0);
4184 while (1)
4186 code = TREE_CODE (exp);
4187 exp_type = TREE_TYPE (exp);
4188 arg0 = NULL_TREE;
4190 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4192 if (TREE_OPERAND_LENGTH (exp) > 0)
4193 arg0 = TREE_OPERAND (exp, 0);
4194 if (TREE_CODE_CLASS (code) == tcc_binary
4195 || TREE_CODE_CLASS (code) == tcc_comparison
4196 || (TREE_CODE_CLASS (code) == tcc_expression
4197 && TREE_OPERAND_LENGTH (exp) > 1))
4198 arg1 = TREE_OPERAND (exp, 1);
4200 if (arg0 == NULL_TREE)
4201 break;
4203 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4204 &high, &in_p, strict_overflow_p);
4205 if (nexp == NULL_TREE)
4206 break;
4207 exp = nexp;
4210 /* If EXP is a constant, we can evaluate whether this is true or false. */
4211 if (TREE_CODE (exp) == INTEGER_CST)
4213 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4214 exp, 0, low, 0))
4215 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4216 exp, 1, high, 1)));
4217 low = high = 0;
4218 exp = 0;
4221 *pin_p = in_p, *plow = low, *phigh = high;
4222 return exp;
4225 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4226 type, TYPE, return an expression to test if EXP is in (or out of, depending
4227 on IN_P) the range. Return 0 if the test couldn't be created. */
4229 tree
4230 build_range_check (location_t loc, tree type, tree exp, int in_p,
4231 tree low, tree high)
4233 tree etype = TREE_TYPE (exp), value;
4235 #ifdef HAVE_canonicalize_funcptr_for_compare
4236 /* Disable this optimization for function pointer expressions
4237 on targets that require function pointer canonicalization. */
4238 if (HAVE_canonicalize_funcptr_for_compare
4239 && TREE_CODE (etype) == POINTER_TYPE
4240 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4241 return NULL_TREE;
4242 #endif
4244 if (! in_p)
4246 value = build_range_check (loc, type, exp, 1, low, high);
4247 if (value != 0)
4248 return invert_truthvalue_loc (loc, value);
4250 return 0;
4253 if (low == 0 && high == 0)
4254 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4256 if (low == 0)
4257 return fold_build2_loc (loc, LE_EXPR, type, exp,
4258 fold_convert_loc (loc, etype, high));
4260 if (high == 0)
4261 return fold_build2_loc (loc, GE_EXPR, type, exp,
4262 fold_convert_loc (loc, etype, low));
4264 if (operand_equal_p (low, high, 0))
4265 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4266 fold_convert_loc (loc, etype, low));
4268 if (integer_zerop (low))
4270 if (! TYPE_UNSIGNED (etype))
4272 etype = unsigned_type_for (etype);
4273 high = fold_convert_loc (loc, etype, high);
4274 exp = fold_convert_loc (loc, etype, exp);
4276 return build_range_check (loc, type, exp, 1, 0, high);
4279 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4280 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4282 int prec = TYPE_PRECISION (etype);
4284 if (wi::mask (prec - 1, false, prec) == high)
4286 if (TYPE_UNSIGNED (etype))
4288 tree signed_etype = signed_type_for (etype);
4289 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4290 etype
4291 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4292 else
4293 etype = signed_etype;
4294 exp = fold_convert_loc (loc, etype, exp);
4296 return fold_build2_loc (loc, GT_EXPR, type, exp,
4297 build_int_cst (etype, 0));
4301 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4302 This requires wrap-around arithmetics for the type of the expression.
4303 First make sure that arithmetics in this type is valid, then make sure
4304 that it wraps around. */
4305 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4306 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4307 TYPE_UNSIGNED (etype));
4309 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4311 tree utype, minv, maxv;
4313 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4314 for the type in question, as we rely on this here. */
4315 utype = unsigned_type_for (etype);
4316 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4317 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4318 build_int_cst (TREE_TYPE (maxv), 1), 1);
4319 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4321 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4322 minv, 1, maxv, 1)))
4323 etype = utype;
4324 else
4325 return 0;
4328 high = fold_convert_loc (loc, etype, high);
4329 low = fold_convert_loc (loc, etype, low);
4330 exp = fold_convert_loc (loc, etype, exp);
4332 value = const_binop (MINUS_EXPR, high, low);
4335 if (POINTER_TYPE_P (etype))
4337 if (value != 0 && !TREE_OVERFLOW (value))
4339 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4340 return build_range_check (loc, type,
4341 fold_build_pointer_plus_loc (loc, exp, low),
4342 1, build_int_cst (etype, 0), value);
4344 return 0;
4347 if (value != 0 && !TREE_OVERFLOW (value))
4348 return build_range_check (loc, type,
4349 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4350 1, build_int_cst (etype, 0), value);
4352 return 0;
4355 /* Return the predecessor of VAL in its type, handling the infinite case. */
4357 static tree
4358 range_predecessor (tree val)
4360 tree type = TREE_TYPE (val);
4362 if (INTEGRAL_TYPE_P (type)
4363 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4364 return 0;
4365 else
4366 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4367 build_int_cst (TREE_TYPE (val), 1), 0);
4370 /* Return the successor of VAL in its type, handling the infinite case. */
4372 static tree
4373 range_successor (tree val)
4375 tree type = TREE_TYPE (val);
4377 if (INTEGRAL_TYPE_P (type)
4378 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4379 return 0;
4380 else
4381 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4382 build_int_cst (TREE_TYPE (val), 1), 0);
4385 /* Given two ranges, see if we can merge them into one. Return 1 if we
4386 can, 0 if we can't. Set the output range into the specified parameters. */
4388 bool
4389 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4390 tree high0, int in1_p, tree low1, tree high1)
4392 int no_overlap;
4393 int subset;
4394 int temp;
4395 tree tem;
4396 int in_p;
4397 tree low, high;
4398 int lowequal = ((low0 == 0 && low1 == 0)
4399 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4400 low0, 0, low1, 0)));
4401 int highequal = ((high0 == 0 && high1 == 0)
4402 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4403 high0, 1, high1, 1)));
4405 /* Make range 0 be the range that starts first, or ends last if they
4406 start at the same value. Swap them if it isn't. */
4407 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4408 low0, 0, low1, 0))
4409 || (lowequal
4410 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4411 high1, 1, high0, 1))))
4413 temp = in0_p, in0_p = in1_p, in1_p = temp;
4414 tem = low0, low0 = low1, low1 = tem;
4415 tem = high0, high0 = high1, high1 = tem;
4418 /* Now flag two cases, whether the ranges are disjoint or whether the
4419 second range is totally subsumed in the first. Note that the tests
4420 below are simplified by the ones above. */
4421 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4422 high0, 1, low1, 0));
4423 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4424 high1, 1, high0, 1));
4426 /* We now have four cases, depending on whether we are including or
4427 excluding the two ranges. */
4428 if (in0_p && in1_p)
4430 /* If they don't overlap, the result is false. If the second range
4431 is a subset it is the result. Otherwise, the range is from the start
4432 of the second to the end of the first. */
4433 if (no_overlap)
4434 in_p = 0, low = high = 0;
4435 else if (subset)
4436 in_p = 1, low = low1, high = high1;
4437 else
4438 in_p = 1, low = low1, high = high0;
4441 else if (in0_p && ! in1_p)
4443 /* If they don't overlap, the result is the first range. If they are
4444 equal, the result is false. If the second range is a subset of the
4445 first, and the ranges begin at the same place, we go from just after
4446 the end of the second range to the end of the first. If the second
4447 range is not a subset of the first, or if it is a subset and both
4448 ranges end at the same place, the range starts at the start of the
4449 first range and ends just before the second range.
4450 Otherwise, we can't describe this as a single range. */
4451 if (no_overlap)
4452 in_p = 1, low = low0, high = high0;
4453 else if (lowequal && highequal)
4454 in_p = 0, low = high = 0;
4455 else if (subset && lowequal)
4457 low = range_successor (high1);
4458 high = high0;
4459 in_p = 1;
4460 if (low == 0)
4462 /* We are in the weird situation where high0 > high1 but
4463 high1 has no successor. Punt. */
4464 return 0;
4467 else if (! subset || highequal)
4469 low = low0;
4470 high = range_predecessor (low1);
4471 in_p = 1;
4472 if (high == 0)
4474 /* low0 < low1 but low1 has no predecessor. Punt. */
4475 return 0;
4478 else
4479 return 0;
4482 else if (! in0_p && in1_p)
4484 /* If they don't overlap, the result is the second range. If the second
4485 is a subset of the first, the result is false. Otherwise,
4486 the range starts just after the first range and ends at the
4487 end of the second. */
4488 if (no_overlap)
4489 in_p = 1, low = low1, high = high1;
4490 else if (subset || highequal)
4491 in_p = 0, low = high = 0;
4492 else
4494 low = range_successor (high0);
4495 high = high1;
4496 in_p = 1;
4497 if (low == 0)
4499 /* high1 > high0 but high0 has no successor. Punt. */
4500 return 0;
4505 else
4507 /* The case where we are excluding both ranges. Here the complex case
4508 is if they don't overlap. In that case, the only time we have a
4509 range is if they are adjacent. If the second is a subset of the
4510 first, the result is the first. Otherwise, the range to exclude
4511 starts at the beginning of the first range and ends at the end of the
4512 second. */
4513 if (no_overlap)
4515 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4516 range_successor (high0),
4517 1, low1, 0)))
4518 in_p = 0, low = low0, high = high1;
4519 else
4521 /* Canonicalize - [min, x] into - [-, x]. */
4522 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4523 switch (TREE_CODE (TREE_TYPE (low0)))
4525 case ENUMERAL_TYPE:
4526 if (TYPE_PRECISION (TREE_TYPE (low0))
4527 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4528 break;
4529 /* FALLTHROUGH */
4530 case INTEGER_TYPE:
4531 if (tree_int_cst_equal (low0,
4532 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4533 low0 = 0;
4534 break;
4535 case POINTER_TYPE:
4536 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4537 && integer_zerop (low0))
4538 low0 = 0;
4539 break;
4540 default:
4541 break;
4544 /* Canonicalize - [x, max] into - [x, -]. */
4545 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4546 switch (TREE_CODE (TREE_TYPE (high1)))
4548 case ENUMERAL_TYPE:
4549 if (TYPE_PRECISION (TREE_TYPE (high1))
4550 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4551 break;
4552 /* FALLTHROUGH */
4553 case INTEGER_TYPE:
4554 if (tree_int_cst_equal (high1,
4555 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4556 high1 = 0;
4557 break;
4558 case POINTER_TYPE:
4559 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4560 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4561 high1, 1,
4562 build_int_cst (TREE_TYPE (high1), 1),
4563 1)))
4564 high1 = 0;
4565 break;
4566 default:
4567 break;
4570 /* The ranges might be also adjacent between the maximum and
4571 minimum values of the given type. For
4572 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4573 return + [x + 1, y - 1]. */
4574 if (low0 == 0 && high1 == 0)
4576 low = range_successor (high0);
4577 high = range_predecessor (low1);
4578 if (low == 0 || high == 0)
4579 return 0;
4581 in_p = 1;
4583 else
4584 return 0;
4587 else if (subset)
4588 in_p = 0, low = low0, high = high0;
4589 else
4590 in_p = 0, low = low0, high = high1;
4593 *pin_p = in_p, *plow = low, *phigh = high;
4594 return 1;
4598 /* Subroutine of fold, looking inside expressions of the form
4599 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4600 of the COND_EXPR. This function is being used also to optimize
4601 A op B ? C : A, by reversing the comparison first.
4603 Return a folded expression whose code is not a COND_EXPR
4604 anymore, or NULL_TREE if no folding opportunity is found. */
4606 static tree
4607 fold_cond_expr_with_comparison (location_t loc, tree type,
4608 tree arg0, tree arg1, tree arg2)
4610 enum tree_code comp_code = TREE_CODE (arg0);
4611 tree arg00 = TREE_OPERAND (arg0, 0);
4612 tree arg01 = TREE_OPERAND (arg0, 1);
4613 tree arg1_type = TREE_TYPE (arg1);
4614 tree tem;
4616 STRIP_NOPS (arg1);
4617 STRIP_NOPS (arg2);
4619 /* If we have A op 0 ? A : -A, consider applying the following
4620 transformations:
4622 A == 0? A : -A same as -A
4623 A != 0? A : -A same as A
4624 A >= 0? A : -A same as abs (A)
4625 A > 0? A : -A same as abs (A)
4626 A <= 0? A : -A same as -abs (A)
4627 A < 0? A : -A same as -abs (A)
4629 None of these transformations work for modes with signed
4630 zeros. If A is +/-0, the first two transformations will
4631 change the sign of the result (from +0 to -0, or vice
4632 versa). The last four will fix the sign of the result,
4633 even though the original expressions could be positive or
4634 negative, depending on the sign of A.
4636 Note that all these transformations are correct if A is
4637 NaN, since the two alternatives (A and -A) are also NaNs. */
4638 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4639 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4640 ? real_zerop (arg01)
4641 : integer_zerop (arg01))
4642 && ((TREE_CODE (arg2) == NEGATE_EXPR
4643 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4644 /* In the case that A is of the form X-Y, '-A' (arg2) may
4645 have already been folded to Y-X, check for that. */
4646 || (TREE_CODE (arg1) == MINUS_EXPR
4647 && TREE_CODE (arg2) == MINUS_EXPR
4648 && operand_equal_p (TREE_OPERAND (arg1, 0),
4649 TREE_OPERAND (arg2, 1), 0)
4650 && operand_equal_p (TREE_OPERAND (arg1, 1),
4651 TREE_OPERAND (arg2, 0), 0))))
4652 switch (comp_code)
4654 case EQ_EXPR:
4655 case UNEQ_EXPR:
4656 tem = fold_convert_loc (loc, arg1_type, arg1);
4657 return pedantic_non_lvalue_loc (loc,
4658 fold_convert_loc (loc, type,
4659 negate_expr (tem)));
4660 case NE_EXPR:
4661 case LTGT_EXPR:
4662 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4663 case UNGE_EXPR:
4664 case UNGT_EXPR:
4665 if (flag_trapping_math)
4666 break;
4667 /* Fall through. */
4668 case GE_EXPR:
4669 case GT_EXPR:
4670 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4671 arg1 = fold_convert_loc (loc, signed_type_for
4672 (TREE_TYPE (arg1)), arg1);
4673 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4674 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4675 case UNLE_EXPR:
4676 case UNLT_EXPR:
4677 if (flag_trapping_math)
4678 break;
4679 case LE_EXPR:
4680 case LT_EXPR:
4681 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4682 arg1 = fold_convert_loc (loc, signed_type_for
4683 (TREE_TYPE (arg1)), arg1);
4684 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4685 return negate_expr (fold_convert_loc (loc, type, tem));
4686 default:
4687 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4688 break;
4691 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4692 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4693 both transformations are correct when A is NaN: A != 0
4694 is then true, and A == 0 is false. */
4696 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4697 && integer_zerop (arg01) && integer_zerop (arg2))
4699 if (comp_code == NE_EXPR)
4700 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4701 else if (comp_code == EQ_EXPR)
4702 return build_zero_cst (type);
4705 /* Try some transformations of A op B ? A : B.
4707 A == B? A : B same as B
4708 A != B? A : B same as A
4709 A >= B? A : B same as max (A, B)
4710 A > B? A : B same as max (B, A)
4711 A <= B? A : B same as min (A, B)
4712 A < B? A : B same as min (B, A)
4714 As above, these transformations don't work in the presence
4715 of signed zeros. For example, if A and B are zeros of
4716 opposite sign, the first two transformations will change
4717 the sign of the result. In the last four, the original
4718 expressions give different results for (A=+0, B=-0) and
4719 (A=-0, B=+0), but the transformed expressions do not.
4721 The first two transformations are correct if either A or B
4722 is a NaN. In the first transformation, the condition will
4723 be false, and B will indeed be chosen. In the case of the
4724 second transformation, the condition A != B will be true,
4725 and A will be chosen.
4727 The conversions to max() and min() are not correct if B is
4728 a number and A is not. The conditions in the original
4729 expressions will be false, so all four give B. The min()
4730 and max() versions would give a NaN instead. */
4731 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4732 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4733 /* Avoid these transformations if the COND_EXPR may be used
4734 as an lvalue in the C++ front-end. PR c++/19199. */
4735 && (in_gimple_form
4736 || VECTOR_TYPE_P (type)
4737 || (strcmp (lang_hooks.name, "GNU C++") != 0
4738 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4739 || ! maybe_lvalue_p (arg1)
4740 || ! maybe_lvalue_p (arg2)))
4742 tree comp_op0 = arg00;
4743 tree comp_op1 = arg01;
4744 tree comp_type = TREE_TYPE (comp_op0);
4746 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4747 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4749 comp_type = type;
4750 comp_op0 = arg1;
4751 comp_op1 = arg2;
4754 switch (comp_code)
4756 case EQ_EXPR:
4757 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4758 case NE_EXPR:
4759 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4760 case LE_EXPR:
4761 case LT_EXPR:
4762 case UNLE_EXPR:
4763 case UNLT_EXPR:
4764 /* In C++ a ?: expression can be an lvalue, so put the
4765 operand which will be used if they are equal first
4766 so that we can convert this back to the
4767 corresponding COND_EXPR. */
4768 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4770 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4771 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4772 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4773 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4774 : fold_build2_loc (loc, MIN_EXPR, comp_type,
4775 comp_op1, comp_op0);
4776 return pedantic_non_lvalue_loc (loc,
4777 fold_convert_loc (loc, type, tem));
4779 break;
4780 case GE_EXPR:
4781 case GT_EXPR:
4782 case UNGE_EXPR:
4783 case UNGT_EXPR:
4784 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4786 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4787 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4788 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4789 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4790 : fold_build2_loc (loc, MAX_EXPR, comp_type,
4791 comp_op1, comp_op0);
4792 return pedantic_non_lvalue_loc (loc,
4793 fold_convert_loc (loc, type, tem));
4795 break;
4796 case UNEQ_EXPR:
4797 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4798 return pedantic_non_lvalue_loc (loc,
4799 fold_convert_loc (loc, type, arg2));
4800 break;
4801 case LTGT_EXPR:
4802 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4803 return pedantic_non_lvalue_loc (loc,
4804 fold_convert_loc (loc, type, arg1));
4805 break;
4806 default:
4807 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4808 break;
4812 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4813 we might still be able to simplify this. For example,
4814 if C1 is one less or one more than C2, this might have started
4815 out as a MIN or MAX and been transformed by this function.
4816 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
4818 if (INTEGRAL_TYPE_P (type)
4819 && TREE_CODE (arg01) == INTEGER_CST
4820 && TREE_CODE (arg2) == INTEGER_CST)
4821 switch (comp_code)
4823 case EQ_EXPR:
4824 if (TREE_CODE (arg1) == INTEGER_CST)
4825 break;
4826 /* We can replace A with C1 in this case. */
4827 arg1 = fold_convert_loc (loc, type, arg01);
4828 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4830 case LT_EXPR:
4831 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4832 MIN_EXPR, to preserve the signedness of the comparison. */
4833 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4834 OEP_ONLY_CONST)
4835 && operand_equal_p (arg01,
4836 const_binop (PLUS_EXPR, arg2,
4837 build_int_cst (type, 1)),
4838 OEP_ONLY_CONST))
4840 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4841 fold_convert_loc (loc, TREE_TYPE (arg00),
4842 arg2));
4843 return pedantic_non_lvalue_loc (loc,
4844 fold_convert_loc (loc, type, tem));
4846 break;
4848 case LE_EXPR:
4849 /* If C1 is C2 - 1, this is min(A, C2), with the same care
4850 as above. */
4851 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4852 OEP_ONLY_CONST)
4853 && operand_equal_p (arg01,
4854 const_binop (MINUS_EXPR, arg2,
4855 build_int_cst (type, 1)),
4856 OEP_ONLY_CONST))
4858 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4859 fold_convert_loc (loc, TREE_TYPE (arg00),
4860 arg2));
4861 return pedantic_non_lvalue_loc (loc,
4862 fold_convert_loc (loc, type, tem));
4864 break;
4866 case GT_EXPR:
4867 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4868 MAX_EXPR, to preserve the signedness of the comparison. */
4869 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4870 OEP_ONLY_CONST)
4871 && operand_equal_p (arg01,
4872 const_binop (MINUS_EXPR, arg2,
4873 build_int_cst (type, 1)),
4874 OEP_ONLY_CONST))
4876 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4877 fold_convert_loc (loc, TREE_TYPE (arg00),
4878 arg2));
4879 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4881 break;
4883 case GE_EXPR:
4884 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
4885 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4886 OEP_ONLY_CONST)
4887 && operand_equal_p (arg01,
4888 const_binop (PLUS_EXPR, arg2,
4889 build_int_cst (type, 1)),
4890 OEP_ONLY_CONST))
4892 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4893 fold_convert_loc (loc, TREE_TYPE (arg00),
4894 arg2));
4895 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4897 break;
4898 case NE_EXPR:
4899 break;
4900 default:
4901 gcc_unreachable ();
4904 return NULL_TREE;
4909 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4910 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4911 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4912 false) >= 2)
4913 #endif
4915 /* EXP is some logical combination of boolean tests. See if we can
4916 merge it into some range test. Return the new tree if so. */
4918 static tree
4919 fold_range_test (location_t loc, enum tree_code code, tree type,
4920 tree op0, tree op1)
4922 int or_op = (code == TRUTH_ORIF_EXPR
4923 || code == TRUTH_OR_EXPR);
4924 int in0_p, in1_p, in_p;
4925 tree low0, low1, low, high0, high1, high;
4926 bool strict_overflow_p = false;
4927 tree tem, lhs, rhs;
4928 const char * const warnmsg = G_("assuming signed overflow does not occur "
4929 "when simplifying range test");
4931 if (!INTEGRAL_TYPE_P (type))
4932 return 0;
4934 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4935 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4937 /* If this is an OR operation, invert both sides; we will invert
4938 again at the end. */
4939 if (or_op)
4940 in0_p = ! in0_p, in1_p = ! in1_p;
4942 /* If both expressions are the same, if we can merge the ranges, and we
4943 can build the range test, return it or it inverted. If one of the
4944 ranges is always true or always false, consider it to be the same
4945 expression as the other. */
4946 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4947 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4948 in1_p, low1, high1)
4949 && 0 != (tem = (build_range_check (loc, type,
4950 lhs != 0 ? lhs
4951 : rhs != 0 ? rhs : integer_zero_node,
4952 in_p, low, high))))
4954 if (strict_overflow_p)
4955 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
4956 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
4959 /* On machines where the branch cost is expensive, if this is a
4960 short-circuited branch and the underlying object on both sides
4961 is the same, make a non-short-circuit operation. */
4962 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4963 && lhs != 0 && rhs != 0
4964 && (code == TRUTH_ANDIF_EXPR
4965 || code == TRUTH_ORIF_EXPR)
4966 && operand_equal_p (lhs, rhs, 0))
4968 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
4969 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4970 which cases we can't do this. */
4971 if (simple_operand_p (lhs))
4972 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4973 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4974 type, op0, op1);
4976 else if (!lang_hooks.decls.global_bindings_p ()
4977 && !CONTAINS_PLACEHOLDER_P (lhs))
4979 tree common = save_expr (lhs);
4981 if (0 != (lhs = build_range_check (loc, type, common,
4982 or_op ? ! in0_p : in0_p,
4983 low0, high0))
4984 && (0 != (rhs = build_range_check (loc, type, common,
4985 or_op ? ! in1_p : in1_p,
4986 low1, high1))))
4988 if (strict_overflow_p)
4989 fold_overflow_warning (warnmsg,
4990 WARN_STRICT_OVERFLOW_COMPARISON);
4991 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4992 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4993 type, lhs, rhs);
4998 return 0;
5001 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5002 bit value. Arrange things so the extra bits will be set to zero if and
5003 only if C is signed-extended to its full width. If MASK is nonzero,
5004 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5006 static tree
5007 unextend (tree c, int p, int unsignedp, tree mask)
5009 tree type = TREE_TYPE (c);
5010 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5011 tree temp;
5013 if (p == modesize || unsignedp)
5014 return c;
5016 /* We work by getting just the sign bit into the low-order bit, then
5017 into the high-order bit, then sign-extend. We then XOR that value
5018 with C. */
5019 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5021 /* We must use a signed type in order to get an arithmetic right shift.
5022 However, we must also avoid introducing accidental overflows, so that
5023 a subsequent call to integer_zerop will work. Hence we must
5024 do the type conversion here. At this point, the constant is either
5025 zero or one, and the conversion to a signed type can never overflow.
5026 We could get an overflow if this conversion is done anywhere else. */
5027 if (TYPE_UNSIGNED (type))
5028 temp = fold_convert (signed_type_for (type), temp);
5030 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5031 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5032 if (mask != 0)
5033 temp = const_binop (BIT_AND_EXPR, temp,
5034 fold_convert (TREE_TYPE (c), mask));
5035 /* If necessary, convert the type back to match the type of C. */
5036 if (TYPE_UNSIGNED (type))
5037 temp = fold_convert (type, temp);
5039 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5042 /* For an expression that has the form
5043 (A && B) || ~B
5045 (A || B) && ~B,
5046 we can drop one of the inner expressions and simplify to
5047 A || ~B
5049 A && ~B
5050 LOC is the location of the resulting expression. OP is the inner
5051 logical operation; the left-hand side in the examples above, while CMPOP
5052 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5053 removing a condition that guards another, as in
5054 (A != NULL && A->...) || A == NULL
5055 which we must not transform. If RHS_ONLY is true, only eliminate the
5056 right-most operand of the inner logical operation. */
5058 static tree
5059 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5060 bool rhs_only)
5062 tree type = TREE_TYPE (cmpop);
5063 enum tree_code code = TREE_CODE (cmpop);
5064 enum tree_code truthop_code = TREE_CODE (op);
5065 tree lhs = TREE_OPERAND (op, 0);
5066 tree rhs = TREE_OPERAND (op, 1);
5067 tree orig_lhs = lhs, orig_rhs = rhs;
5068 enum tree_code rhs_code = TREE_CODE (rhs);
5069 enum tree_code lhs_code = TREE_CODE (lhs);
5070 enum tree_code inv_code;
5072 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5073 return NULL_TREE;
5075 if (TREE_CODE_CLASS (code) != tcc_comparison)
5076 return NULL_TREE;
5078 if (rhs_code == truthop_code)
5080 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5081 if (newrhs != NULL_TREE)
5083 rhs = newrhs;
5084 rhs_code = TREE_CODE (rhs);
5087 if (lhs_code == truthop_code && !rhs_only)
5089 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5090 if (newlhs != NULL_TREE)
5092 lhs = newlhs;
5093 lhs_code = TREE_CODE (lhs);
5097 inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
5098 if (inv_code == rhs_code
5099 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5100 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5101 return lhs;
5102 if (!rhs_only && inv_code == lhs_code
5103 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5104 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5105 return rhs;
5106 if (rhs != orig_rhs || lhs != orig_lhs)
5107 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5108 lhs, rhs);
5109 return NULL_TREE;
5112 /* Find ways of folding logical expressions of LHS and RHS:
5113 Try to merge two comparisons to the same innermost item.
5114 Look for range tests like "ch >= '0' && ch <= '9'".
5115 Look for combinations of simple terms on machines with expensive branches
5116 and evaluate the RHS unconditionally.
5118 For example, if we have p->a == 2 && p->b == 4 and we can make an
5119 object large enough to span both A and B, we can do this with a comparison
5120 against the object ANDed with the a mask.
5122 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5123 operations to do this with one comparison.
5125 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5126 function and the one above.
5128 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5129 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5131 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5132 two operands.
5134 We return the simplified tree or 0 if no optimization is possible. */
5136 static tree
5137 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5138 tree lhs, tree rhs)
5140 /* If this is the "or" of two comparisons, we can do something if
5141 the comparisons are NE_EXPR. If this is the "and", we can do something
5142 if the comparisons are EQ_EXPR. I.e.,
5143 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5145 WANTED_CODE is this operation code. For single bit fields, we can
5146 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5147 comparison for one-bit fields. */
5149 enum tree_code wanted_code;
5150 enum tree_code lcode, rcode;
5151 tree ll_arg, lr_arg, rl_arg, rr_arg;
5152 tree ll_inner, lr_inner, rl_inner, rr_inner;
5153 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5154 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5155 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5156 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5157 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5158 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5159 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5160 enum machine_mode lnmode, rnmode;
5161 tree ll_mask, lr_mask, rl_mask, rr_mask;
5162 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5163 tree l_const, r_const;
5164 tree lntype, rntype, result;
5165 HOST_WIDE_INT first_bit, end_bit;
5166 int volatilep;
5168 /* Start by getting the comparison codes. Fail if anything is volatile.
5169 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5170 it were surrounded with a NE_EXPR. */
5172 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5173 return 0;
5175 lcode = TREE_CODE (lhs);
5176 rcode = TREE_CODE (rhs);
5178 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5180 lhs = build2 (NE_EXPR, truth_type, lhs,
5181 build_int_cst (TREE_TYPE (lhs), 0));
5182 lcode = NE_EXPR;
5185 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5187 rhs = build2 (NE_EXPR, truth_type, rhs,
5188 build_int_cst (TREE_TYPE (rhs), 0));
5189 rcode = NE_EXPR;
5192 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5193 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5194 return 0;
5196 ll_arg = TREE_OPERAND (lhs, 0);
5197 lr_arg = TREE_OPERAND (lhs, 1);
5198 rl_arg = TREE_OPERAND (rhs, 0);
5199 rr_arg = TREE_OPERAND (rhs, 1);
5201 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5202 if (simple_operand_p (ll_arg)
5203 && simple_operand_p (lr_arg))
5205 if (operand_equal_p (ll_arg, rl_arg, 0)
5206 && operand_equal_p (lr_arg, rr_arg, 0))
5208 result = combine_comparisons (loc, code, lcode, rcode,
5209 truth_type, ll_arg, lr_arg);
5210 if (result)
5211 return result;
5213 else if (operand_equal_p (ll_arg, rr_arg, 0)
5214 && operand_equal_p (lr_arg, rl_arg, 0))
5216 result = combine_comparisons (loc, code, lcode,
5217 swap_tree_comparison (rcode),
5218 truth_type, ll_arg, lr_arg);
5219 if (result)
5220 return result;
5224 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5225 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5227 /* If the RHS can be evaluated unconditionally and its operands are
5228 simple, it wins to evaluate the RHS unconditionally on machines
5229 with expensive branches. In this case, this isn't a comparison
5230 that can be merged. */
5232 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5233 false) >= 2
5234 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5235 && simple_operand_p (rl_arg)
5236 && simple_operand_p (rr_arg))
5238 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5239 if (code == TRUTH_OR_EXPR
5240 && lcode == NE_EXPR && integer_zerop (lr_arg)
5241 && rcode == NE_EXPR && integer_zerop (rr_arg)
5242 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5243 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5244 return build2_loc (loc, NE_EXPR, truth_type,
5245 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5246 ll_arg, rl_arg),
5247 build_int_cst (TREE_TYPE (ll_arg), 0));
5249 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5250 if (code == TRUTH_AND_EXPR
5251 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5252 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5253 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5254 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5255 return build2_loc (loc, EQ_EXPR, truth_type,
5256 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5257 ll_arg, rl_arg),
5258 build_int_cst (TREE_TYPE (ll_arg), 0));
5261 /* See if the comparisons can be merged. Then get all the parameters for
5262 each side. */
5264 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5265 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5266 return 0;
5268 volatilep = 0;
5269 ll_inner = decode_field_reference (loc, ll_arg,
5270 &ll_bitsize, &ll_bitpos, &ll_mode,
5271 &ll_unsignedp, &ll_reversep, &volatilep,
5272 &ll_mask, &ll_and_mask);
5273 lr_inner = decode_field_reference (loc, lr_arg,
5274 &lr_bitsize, &lr_bitpos, &lr_mode,
5275 &lr_unsignedp, &lr_reversep, &volatilep,
5276 &lr_mask, &lr_and_mask);
5277 rl_inner = decode_field_reference (loc, rl_arg,
5278 &rl_bitsize, &rl_bitpos, &rl_mode,
5279 &rl_unsignedp, &rl_reversep, &volatilep,
5280 &rl_mask, &rl_and_mask);
5281 rr_inner = decode_field_reference (loc, rr_arg,
5282 &rr_bitsize, &rr_bitpos, &rr_mode,
5283 &rr_unsignedp, &rr_reversep, &volatilep,
5284 &rr_mask, &rr_and_mask);
5286 /* It must be true that the inner operation on the lhs of each
5287 comparison must be the same if we are to be able to do anything.
5288 Then see if we have constants. If not, the same must be true for
5289 the rhs's. */
5290 if (volatilep
5291 || ll_reversep != rl_reversep
5292 || ll_inner == 0 || rl_inner == 0
5293 || ! operand_equal_p (ll_inner, rl_inner, 0))
5294 return 0;
5296 if (TREE_CODE (lr_arg) == INTEGER_CST
5297 && TREE_CODE (rr_arg) == INTEGER_CST)
5299 l_const = lr_arg, r_const = rr_arg;
5300 lr_reversep = ll_reversep;
5302 else if (lr_reversep != rr_reversep
5303 || lr_inner == 0 || rr_inner == 0
5304 || ! operand_equal_p (lr_inner, rr_inner, 0))
5305 return 0;
5306 else
5307 l_const = r_const = 0;
5309 /* If either comparison code is not correct for our logical operation,
5310 fail. However, we can convert a one-bit comparison against zero into
5311 the opposite comparison against that bit being set in the field. */
5313 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5314 if (lcode != wanted_code)
5316 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5318 /* Make the left operand unsigned, since we are only interested
5319 in the value of one bit. Otherwise we are doing the wrong
5320 thing below. */
5321 ll_unsignedp = 1;
5322 l_const = ll_mask;
5324 else
5325 return 0;
5328 /* This is analogous to the code for l_const above. */
5329 if (rcode != wanted_code)
5331 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5333 rl_unsignedp = 1;
5334 r_const = rl_mask;
5336 else
5337 return 0;
5340 /* See if we can find a mode that contains both fields being compared on
5341 the left. If we can't, fail. Otherwise, update all constants and masks
5342 to be relative to a field of that size. */
5343 first_bit = MIN (ll_bitpos, rl_bitpos);
5344 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5345 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5346 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5347 volatilep);
5348 if (lnmode == VOIDmode)
5349 return 0;
5351 lnbitsize = GET_MODE_BITSIZE (lnmode);
5352 lnbitpos = first_bit & ~ (lnbitsize - 1);
5353 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5354 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5356 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5358 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5359 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5362 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5363 size_int (xll_bitpos));
5364 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5365 size_int (xrl_bitpos));
5367 if (l_const)
5369 l_const = fold_convert_loc (loc, lntype, l_const);
5370 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5371 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5372 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5373 fold_build1_loc (loc, BIT_NOT_EXPR,
5374 lntype, ll_mask))))
5376 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5378 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5381 if (r_const)
5383 r_const = fold_convert_loc (loc, lntype, r_const);
5384 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5385 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5386 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5387 fold_build1_loc (loc, BIT_NOT_EXPR,
5388 lntype, rl_mask))))
5390 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5392 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5396 /* If the right sides are not constant, do the same for it. Also,
5397 disallow this optimization if a size or signedness mismatch occurs
5398 between the left and right sides. */
5399 if (l_const == 0)
5401 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5402 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5403 /* Make sure the two fields on the right
5404 correspond to the left without being swapped. */
5405 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5406 return 0;
5408 first_bit = MIN (lr_bitpos, rr_bitpos);
5409 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5410 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5411 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5412 volatilep);
5413 if (rnmode == VOIDmode)
5414 return 0;
5416 rnbitsize = GET_MODE_BITSIZE (rnmode);
5417 rnbitpos = first_bit & ~ (rnbitsize - 1);
5418 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5419 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5421 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5423 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5424 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5427 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5428 rntype, lr_mask),
5429 size_int (xlr_bitpos));
5430 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5431 rntype, rr_mask),
5432 size_int (xrr_bitpos));
5434 /* Make a mask that corresponds to both fields being compared.
5435 Do this for both items being compared. If the operands are the
5436 same size and the bits being compared are in the same position
5437 then we can do this by masking both and comparing the masked
5438 results. */
5439 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5440 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5441 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5443 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5444 ll_unsignedp || rl_unsignedp, ll_reversep);
5445 if (! all_ones_mask_p (ll_mask, lnbitsize))
5446 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5448 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5449 lr_unsignedp || rr_unsignedp, lr_reversep);
5450 if (! all_ones_mask_p (lr_mask, rnbitsize))
5451 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5453 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5456 /* There is still another way we can do something: If both pairs of
5457 fields being compared are adjacent, we may be able to make a wider
5458 field containing them both.
5460 Note that we still must mask the lhs/rhs expressions. Furthermore,
5461 the mask must be shifted to account for the shift done by
5462 make_bit_field_ref. */
5463 if ((ll_bitsize + ll_bitpos == rl_bitpos
5464 && lr_bitsize + lr_bitpos == rr_bitpos)
5465 || (ll_bitpos == rl_bitpos + rl_bitsize
5466 && lr_bitpos == rr_bitpos + rr_bitsize))
5468 tree type;
5470 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5471 ll_bitsize + rl_bitsize,
5472 MIN (ll_bitpos, rl_bitpos),
5473 ll_unsignedp, ll_reversep);
5474 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5475 lr_bitsize + rr_bitsize,
5476 MIN (lr_bitpos, rr_bitpos),
5477 lr_unsignedp, lr_reversep);
5479 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5480 size_int (MIN (xll_bitpos, xrl_bitpos)));
5481 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5482 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5484 /* Convert to the smaller type before masking out unwanted bits. */
5485 type = lntype;
5486 if (lntype != rntype)
5488 if (lnbitsize > rnbitsize)
5490 lhs = fold_convert_loc (loc, rntype, lhs);
5491 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5492 type = rntype;
5494 else if (lnbitsize < rnbitsize)
5496 rhs = fold_convert_loc (loc, lntype, rhs);
5497 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5498 type = lntype;
5502 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5503 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5505 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5506 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5508 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5511 return 0;
5514 /* Handle the case of comparisons with constants. If there is something in
5515 common between the masks, those bits of the constants must be the same.
5516 If not, the condition is always false. Test for this to avoid generating
5517 incorrect code below. */
5518 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5519 if (! integer_zerop (result)
5520 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5521 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5523 if (wanted_code == NE_EXPR)
5525 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5526 return constant_boolean_node (true, truth_type);
5528 else
5530 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5531 return constant_boolean_node (false, truth_type);
5535 /* Construct the expression we will return. First get the component
5536 reference we will make. Unless the mask is all ones the width of
5537 that field, perform the mask operation. Then compare with the
5538 merged constant. */
5539 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5540 ll_unsignedp || rl_unsignedp, ll_reversep);
5542 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5543 if (! all_ones_mask_p (ll_mask, lnbitsize))
5544 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5546 return build2_loc (loc, wanted_code, truth_type, result,
5547 const_binop (BIT_IOR_EXPR, l_const, r_const));
5550 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5551 constant. */
5553 static tree
5554 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5555 tree op0, tree op1)
5557 tree arg0 = op0;
5558 enum tree_code op_code;
5559 tree comp_const;
5560 tree minmax_const;
5561 int consts_equal, consts_lt;
5562 tree inner;
5564 STRIP_SIGN_NOPS (arg0);
5566 op_code = TREE_CODE (arg0);
5567 minmax_const = TREE_OPERAND (arg0, 1);
5568 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5569 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5570 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5571 inner = TREE_OPERAND (arg0, 0);
5573 /* If something does not permit us to optimize, return the original tree. */
5574 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5575 || TREE_CODE (comp_const) != INTEGER_CST
5576 || TREE_OVERFLOW (comp_const)
5577 || TREE_CODE (minmax_const) != INTEGER_CST
5578 || TREE_OVERFLOW (minmax_const))
5579 return NULL_TREE;
5581 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5582 and GT_EXPR, doing the rest with recursive calls using logical
5583 simplifications. */
5584 switch (code)
5586 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5588 tree tem
5589 = optimize_minmax_comparison (loc,
5590 invert_tree_comparison (code, false),
5591 type, op0, op1);
5592 if (tem)
5593 return invert_truthvalue_loc (loc, tem);
5594 return NULL_TREE;
5597 case GE_EXPR:
5598 return
5599 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5600 optimize_minmax_comparison
5601 (loc, EQ_EXPR, type, arg0, comp_const),
5602 optimize_minmax_comparison
5603 (loc, GT_EXPR, type, arg0, comp_const));
5605 case EQ_EXPR:
5606 if (op_code == MAX_EXPR && consts_equal)
5607 /* MAX (X, 0) == 0 -> X <= 0 */
5608 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5610 else if (op_code == MAX_EXPR && consts_lt)
5611 /* MAX (X, 0) == 5 -> X == 5 */
5612 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5614 else if (op_code == MAX_EXPR)
5615 /* MAX (X, 0) == -1 -> false */
5616 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5618 else if (consts_equal)
5619 /* MIN (X, 0) == 0 -> X >= 0 */
5620 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5622 else if (consts_lt)
5623 /* MIN (X, 0) == 5 -> false */
5624 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5626 else
5627 /* MIN (X, 0) == -1 -> X == -1 */
5628 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5630 case GT_EXPR:
5631 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5632 /* MAX (X, 0) > 0 -> X > 0
5633 MAX (X, 0) > 5 -> X > 5 */
5634 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5636 else if (op_code == MAX_EXPR)
5637 /* MAX (X, 0) > -1 -> true */
5638 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5640 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5641 /* MIN (X, 0) > 0 -> false
5642 MIN (X, 0) > 5 -> false */
5643 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5645 else
5646 /* MIN (X, 0) > -1 -> X > -1 */
5647 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5649 default:
5650 return NULL_TREE;
5654 /* T is an integer expression that is being multiplied, divided, or taken a
5655 modulus (CODE says which and what kind of divide or modulus) by a
5656 constant C. See if we can eliminate that operation by folding it with
5657 other operations already in T. WIDE_TYPE, if non-null, is a type that
5658 should be used for the computation if wider than our type.
5660 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5661 (X * 2) + (Y * 4). We must, however, be assured that either the original
5662 expression would not overflow or that overflow is undefined for the type
5663 in the language in question.
5665 If we return a non-null expression, it is an equivalent form of the
5666 original computation, but need not be in the original type.
5668 We set *STRICT_OVERFLOW_P to true if the return values depends on
5669 signed overflow being undefined. Otherwise we do not change
5670 *STRICT_OVERFLOW_P. */
5672 static tree
5673 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5674 bool *strict_overflow_p)
5676 /* To avoid exponential search depth, refuse to allow recursion past
5677 three levels. Beyond that (1) it's highly unlikely that we'll find
5678 something interesting and (2) we've probably processed it before
5679 when we built the inner expression. */
5681 static int depth;
5682 tree ret;
5684 if (depth > 3)
5685 return NULL;
5687 depth++;
5688 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5689 depth--;
5691 return ret;
5694 static tree
5695 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5696 bool *strict_overflow_p)
5698 tree type = TREE_TYPE (t);
5699 enum tree_code tcode = TREE_CODE (t);
5700 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5701 > GET_MODE_SIZE (TYPE_MODE (type)))
5702 ? wide_type : type);
5703 tree t1, t2;
5704 int same_p = tcode == code;
5705 tree op0 = NULL_TREE, op1 = NULL_TREE;
5706 bool sub_strict_overflow_p;
5708 /* Don't deal with constants of zero here; they confuse the code below. */
5709 if (integer_zerop (c))
5710 return NULL_TREE;
5712 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5713 op0 = TREE_OPERAND (t, 0);
5715 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5716 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5718 /* Note that we need not handle conditional operations here since fold
5719 already handles those cases. So just do arithmetic here. */
5720 switch (tcode)
5722 case INTEGER_CST:
5723 /* For a constant, we can always simplify if we are a multiply
5724 or (for divide and modulus) if it is a multiple of our constant. */
5725 if (code == MULT_EXPR
5726 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
5727 return const_binop (code, fold_convert (ctype, t),
5728 fold_convert (ctype, c));
5729 break;
5731 CASE_CONVERT: case NON_LVALUE_EXPR:
5732 /* If op0 is an expression ... */
5733 if ((COMPARISON_CLASS_P (op0)
5734 || UNARY_CLASS_P (op0)
5735 || BINARY_CLASS_P (op0)
5736 || VL_EXP_CLASS_P (op0)
5737 || EXPRESSION_CLASS_P (op0))
5738 /* ... and has wrapping overflow, and its type is smaller
5739 than ctype, then we cannot pass through as widening. */
5740 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5741 && (TYPE_PRECISION (ctype)
5742 > TYPE_PRECISION (TREE_TYPE (op0))))
5743 /* ... or this is a truncation (t is narrower than op0),
5744 then we cannot pass through this narrowing. */
5745 || (TYPE_PRECISION (type)
5746 < TYPE_PRECISION (TREE_TYPE (op0)))
5747 /* ... or signedness changes for division or modulus,
5748 then we cannot pass through this conversion. */
5749 || (code != MULT_EXPR
5750 && (TYPE_UNSIGNED (ctype)
5751 != TYPE_UNSIGNED (TREE_TYPE (op0))))
5752 /* ... or has undefined overflow while the converted to
5753 type has not, we cannot do the operation in the inner type
5754 as that would introduce undefined overflow. */
5755 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5756 && !TYPE_OVERFLOW_UNDEFINED (type))))
5757 break;
5759 /* Pass the constant down and see if we can make a simplification. If
5760 we can, replace this expression with the inner simplification for
5761 possible later conversion to our or some other type. */
5762 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5763 && TREE_CODE (t2) == INTEGER_CST
5764 && !TREE_OVERFLOW (t2)
5765 && (0 != (t1 = extract_muldiv (op0, t2, code,
5766 code == MULT_EXPR
5767 ? ctype : NULL_TREE,
5768 strict_overflow_p))))
5769 return t1;
5770 break;
5772 case ABS_EXPR:
5773 /* If widening the type changes it from signed to unsigned, then we
5774 must avoid building ABS_EXPR itself as unsigned. */
5775 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5777 tree cstype = (*signed_type_for) (ctype);
5778 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5779 != 0)
5781 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5782 return fold_convert (ctype, t1);
5784 break;
5786 /* If the constant is negative, we cannot simplify this. */
5787 if (tree_int_cst_sgn (c) == -1)
5788 break;
5789 /* FALLTHROUGH */
5790 case NEGATE_EXPR:
5791 /* For division and modulus, type can't be unsigned, as e.g.
5792 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
5793 For signed types, even with wrapping overflow, this is fine. */
5794 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
5795 break;
5796 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5797 != 0)
5798 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5799 break;
5801 case MIN_EXPR: case MAX_EXPR:
5802 /* If widening the type changes the signedness, then we can't perform
5803 this optimization as that changes the result. */
5804 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5805 break;
5807 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5808 sub_strict_overflow_p = false;
5809 if ((t1 = extract_muldiv (op0, c, code, wide_type,
5810 &sub_strict_overflow_p)) != 0
5811 && (t2 = extract_muldiv (op1, c, code, wide_type,
5812 &sub_strict_overflow_p)) != 0)
5814 if (tree_int_cst_sgn (c) < 0)
5815 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5816 if (sub_strict_overflow_p)
5817 *strict_overflow_p = true;
5818 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5819 fold_convert (ctype, t2));
5821 break;
5823 case LSHIFT_EXPR: case RSHIFT_EXPR:
5824 /* If the second operand is constant, this is a multiplication
5825 or floor division, by a power of two, so we can treat it that
5826 way unless the multiplier or divisor overflows. Signed
5827 left-shift overflow is implementation-defined rather than
5828 undefined in C90, so do not convert signed left shift into
5829 multiplication. */
5830 if (TREE_CODE (op1) == INTEGER_CST
5831 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5832 /* const_binop may not detect overflow correctly,
5833 so check for it explicitly here. */
5834 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
5835 && 0 != (t1 = fold_convert (ctype,
5836 const_binop (LSHIFT_EXPR,
5837 size_one_node,
5838 op1)))
5839 && !TREE_OVERFLOW (t1))
5840 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5841 ? MULT_EXPR : FLOOR_DIV_EXPR,
5842 ctype,
5843 fold_convert (ctype, op0),
5844 t1),
5845 c, code, wide_type, strict_overflow_p);
5846 break;
5848 case PLUS_EXPR: case MINUS_EXPR:
5849 /* See if we can eliminate the operation on both sides. If we can, we
5850 can return a new PLUS or MINUS. If we can't, the only remaining
5851 cases where we can do anything are if the second operand is a
5852 constant. */
5853 sub_strict_overflow_p = false;
5854 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5855 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5856 if (t1 != 0 && t2 != 0
5857 && (code == MULT_EXPR
5858 /* If not multiplication, we can only do this if both operands
5859 are divisible by c. */
5860 || (multiple_of_p (ctype, op0, c)
5861 && multiple_of_p (ctype, op1, c))))
5863 if (sub_strict_overflow_p)
5864 *strict_overflow_p = true;
5865 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5866 fold_convert (ctype, t2));
5869 /* If this was a subtraction, negate OP1 and set it to be an addition.
5870 This simplifies the logic below. */
5871 if (tcode == MINUS_EXPR)
5873 tcode = PLUS_EXPR, op1 = negate_expr (op1);
5874 /* If OP1 was not easily negatable, the constant may be OP0. */
5875 if (TREE_CODE (op0) == INTEGER_CST)
5877 tree tem = op0;
5878 op0 = op1;
5879 op1 = tem;
5880 tem = t1;
5881 t1 = t2;
5882 t2 = tem;
5886 if (TREE_CODE (op1) != INTEGER_CST)
5887 break;
5889 /* If either OP1 or C are negative, this optimization is not safe for
5890 some of the division and remainder types while for others we need
5891 to change the code. */
5892 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5894 if (code == CEIL_DIV_EXPR)
5895 code = FLOOR_DIV_EXPR;
5896 else if (code == FLOOR_DIV_EXPR)
5897 code = CEIL_DIV_EXPR;
5898 else if (code != MULT_EXPR
5899 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5900 break;
5903 /* If it's a multiply or a division/modulus operation of a multiple
5904 of our constant, do the operation and verify it doesn't overflow. */
5905 if (code == MULT_EXPR
5906 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
5908 op1 = const_binop (code, fold_convert (ctype, op1),
5909 fold_convert (ctype, c));
5910 /* We allow the constant to overflow with wrapping semantics. */
5911 if (op1 == 0
5912 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5913 break;
5915 else
5916 break;
5918 /* If we have an unsigned type, we cannot widen the operation since it
5919 will change the result if the original computation overflowed. */
5920 if (TYPE_UNSIGNED (ctype) && ctype != type)
5921 break;
5923 /* If we were able to eliminate our operation from the first side,
5924 apply our operation to the second side and reform the PLUS. */
5925 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5926 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5928 /* The last case is if we are a multiply. In that case, we can
5929 apply the distributive law to commute the multiply and addition
5930 if the multiplication of the constants doesn't overflow
5931 and overflow is defined. With undefined overflow
5932 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
5933 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
5934 return fold_build2 (tcode, ctype,
5935 fold_build2 (code, ctype,
5936 fold_convert (ctype, op0),
5937 fold_convert (ctype, c)),
5938 op1);
5940 break;
5942 case MULT_EXPR:
5943 /* We have a special case here if we are doing something like
5944 (C * 8) % 4 since we know that's zero. */
5945 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5946 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5947 /* If the multiplication can overflow we cannot optimize this. */
5948 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5949 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5950 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
5952 *strict_overflow_p = true;
5953 return omit_one_operand (type, integer_zero_node, op0);
5956 /* ... fall through ... */
5958 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
5959 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
5960 /* If we can extract our operation from the LHS, do so and return a
5961 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
5962 do something only if the second operand is a constant. */
5963 if (same_p
5964 && (t1 = extract_muldiv (op0, c, code, wide_type,
5965 strict_overflow_p)) != 0)
5966 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5967 fold_convert (ctype, op1));
5968 else if (tcode == MULT_EXPR && code == MULT_EXPR
5969 && (t1 = extract_muldiv (op1, c, code, wide_type,
5970 strict_overflow_p)) != 0)
5971 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5972 fold_convert (ctype, t1));
5973 else if (TREE_CODE (op1) != INTEGER_CST)
5974 return 0;
5976 /* If these are the same operation types, we can associate them
5977 assuming no overflow. */
5978 if (tcode == code)
5980 bool overflow_p = false;
5981 bool overflow_mul_p;
5982 signop sign = TYPE_SIGN (ctype);
5983 wide_int mul = wi::mul (op1, c, sign, &overflow_mul_p);
5984 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
5985 if (overflow_mul_p
5986 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
5987 overflow_p = true;
5988 if (!overflow_p)
5989 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5990 wide_int_to_tree (ctype, mul));
5993 /* If these operations "cancel" each other, we have the main
5994 optimizations of this pass, which occur when either constant is a
5995 multiple of the other, in which case we replace this with either an
5996 operation or CODE or TCODE.
5998 If we have an unsigned type, we cannot do this since it will change
5999 the result if the original computation overflowed. */
6000 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6001 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6002 || (tcode == MULT_EXPR
6003 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6004 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6005 && code != MULT_EXPR)))
6007 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6009 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6010 *strict_overflow_p = true;
6011 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6012 fold_convert (ctype,
6013 const_binop (TRUNC_DIV_EXPR,
6014 op1, c)));
6016 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6018 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6019 *strict_overflow_p = true;
6020 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6021 fold_convert (ctype,
6022 const_binop (TRUNC_DIV_EXPR,
6023 c, op1)));
6026 break;
6028 default:
6029 break;
6032 return 0;
6035 /* Return a node which has the indicated constant VALUE (either 0 or
6036 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6037 and is of the indicated TYPE. */
6039 tree
6040 constant_boolean_node (bool value, tree type)
6042 if (type == integer_type_node)
6043 return value ? integer_one_node : integer_zero_node;
6044 else if (type == boolean_type_node)
6045 return value ? boolean_true_node : boolean_false_node;
6046 else if (TREE_CODE (type) == VECTOR_TYPE)
6047 return build_vector_from_val (type,
6048 build_int_cst (TREE_TYPE (type),
6049 value ? -1 : 0));
6050 else
6051 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6055 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6056 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6057 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6058 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6059 COND is the first argument to CODE; otherwise (as in the example
6060 given here), it is the second argument. TYPE is the type of the
6061 original expression. Return NULL_TREE if no simplification is
6062 possible. */
6064 static tree
6065 fold_binary_op_with_conditional_arg (location_t loc,
6066 enum tree_code code,
6067 tree type, tree op0, tree op1,
6068 tree cond, tree arg, int cond_first_p)
6070 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6071 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6072 tree test, true_value, false_value;
6073 tree lhs = NULL_TREE;
6074 tree rhs = NULL_TREE;
6075 enum tree_code cond_code = COND_EXPR;
6077 if (TREE_CODE (cond) == COND_EXPR
6078 || TREE_CODE (cond) == VEC_COND_EXPR)
6080 test = TREE_OPERAND (cond, 0);
6081 true_value = TREE_OPERAND (cond, 1);
6082 false_value = TREE_OPERAND (cond, 2);
6083 /* If this operand throws an expression, then it does not make
6084 sense to try to perform a logical or arithmetic operation
6085 involving it. */
6086 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6087 lhs = true_value;
6088 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6089 rhs = false_value;
6091 else
6093 tree testtype = TREE_TYPE (cond);
6094 test = cond;
6095 true_value = constant_boolean_node (true, testtype);
6096 false_value = constant_boolean_node (false, testtype);
6099 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6100 cond_code = VEC_COND_EXPR;
6102 /* This transformation is only worthwhile if we don't have to wrap ARG
6103 in a SAVE_EXPR and the operation can be simplified without recursing
6104 on at least one of the branches once its pushed inside the COND_EXPR. */
6105 if (!TREE_CONSTANT (arg)
6106 && (TREE_SIDE_EFFECTS (arg)
6107 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6108 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6109 return NULL_TREE;
6111 arg = fold_convert_loc (loc, arg_type, arg);
6112 if (lhs == 0)
6114 true_value = fold_convert_loc (loc, cond_type, true_value);
6115 if (cond_first_p)
6116 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6117 else
6118 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6120 if (rhs == 0)
6122 false_value = fold_convert_loc (loc, cond_type, false_value);
6123 if (cond_first_p)
6124 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6125 else
6126 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6129 /* Check that we have simplified at least one of the branches. */
6130 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6131 return NULL_TREE;
6133 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6137 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6139 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6140 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6141 ADDEND is the same as X.
6143 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6144 and finite. The problematic cases are when X is zero, and its mode
6145 has signed zeros. In the case of rounding towards -infinity,
6146 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6147 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6149 bool
6150 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6152 if (!real_zerop (addend))
6153 return false;
6155 /* Don't allow the fold with -fsignaling-nans. */
6156 if (HONOR_SNANS (TYPE_MODE (type)))
6157 return false;
6159 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6160 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6161 return true;
6163 /* In a vector or complex, we would need to check the sign of all zeros. */
6164 if (TREE_CODE (addend) != REAL_CST)
6165 return false;
6167 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6168 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6169 negate = !negate;
6171 /* The mode has signed zeros, and we have to honor their sign.
6172 In this situation, there is only one case we can return true for.
6173 X - 0 is the same as X unless rounding towards -infinity is
6174 supported. */
6175 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6178 /* Subroutine of fold() that checks comparisons of built-in math
6179 functions against real constants.
6181 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6182 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6183 is the type of the result and ARG0 and ARG1 are the operands of the
6184 comparison. ARG1 must be a TREE_REAL_CST.
6186 The function returns the constant folded tree if a simplification
6187 can be made, and NULL_TREE otherwise. */
6189 static tree
6190 fold_mathfn_compare (location_t loc,
6191 enum built_in_function fcode, enum tree_code code,
6192 tree type, tree arg0, tree arg1)
6194 REAL_VALUE_TYPE c;
6196 if (BUILTIN_SQRT_P (fcode))
6198 tree arg = CALL_EXPR_ARG (arg0, 0);
6199 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6201 c = TREE_REAL_CST (arg1);
6202 if (REAL_VALUE_NEGATIVE (c))
6204 /* sqrt(x) < y is always false, if y is negative. */
6205 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6206 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6208 /* sqrt(x) > y is always true, if y is negative and we
6209 don't care about NaNs, i.e. negative values of x. */
6210 if (code == NE_EXPR || !HONOR_NANS (mode))
6211 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6213 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6214 return fold_build2_loc (loc, GE_EXPR, type, arg,
6215 build_real (TREE_TYPE (arg), dconst0));
6217 else if (code == GT_EXPR || code == GE_EXPR)
6219 REAL_VALUE_TYPE c2;
6221 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6222 real_convert (&c2, mode, &c2);
6224 if (REAL_VALUE_ISINF (c2))
6226 /* sqrt(x) > y is x == +Inf, when y is very large. */
6227 if (HONOR_INFINITIES (mode))
6228 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6229 build_real (TREE_TYPE (arg), c2));
6231 /* sqrt(x) > y is always false, when y is very large
6232 and we don't care about infinities. */
6233 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6236 /* sqrt(x) > c is the same as x > c*c. */
6237 return fold_build2_loc (loc, code, type, arg,
6238 build_real (TREE_TYPE (arg), c2));
6240 else if (code == LT_EXPR || code == LE_EXPR)
6242 REAL_VALUE_TYPE c2;
6244 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6245 real_convert (&c2, mode, &c2);
6247 if (REAL_VALUE_ISINF (c2))
6249 /* sqrt(x) < y is always true, when y is a very large
6250 value and we don't care about NaNs or Infinities. */
6251 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6252 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6254 /* sqrt(x) < y is x != +Inf when y is very large and we
6255 don't care about NaNs. */
6256 if (! HONOR_NANS (mode))
6257 return fold_build2_loc (loc, NE_EXPR, type, arg,
6258 build_real (TREE_TYPE (arg), c2));
6260 /* sqrt(x) < y is x >= 0 when y is very large and we
6261 don't care about Infinities. */
6262 if (! HONOR_INFINITIES (mode))
6263 return fold_build2_loc (loc, GE_EXPR, type, arg,
6264 build_real (TREE_TYPE (arg), dconst0));
6266 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6267 arg = save_expr (arg);
6268 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6269 fold_build2_loc (loc, GE_EXPR, type, arg,
6270 build_real (TREE_TYPE (arg),
6271 dconst0)),
6272 fold_build2_loc (loc, NE_EXPR, type, arg,
6273 build_real (TREE_TYPE (arg),
6274 c2)));
6277 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6278 if (! HONOR_NANS (mode))
6279 return fold_build2_loc (loc, code, type, arg,
6280 build_real (TREE_TYPE (arg), c2));
6282 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6283 arg = save_expr (arg);
6284 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6285 fold_build2_loc (loc, GE_EXPR, type, arg,
6286 build_real (TREE_TYPE (arg),
6287 dconst0)),
6288 fold_build2_loc (loc, code, type, arg,
6289 build_real (TREE_TYPE (arg),
6290 c2)));
6294 return NULL_TREE;
6297 /* Subroutine of fold() that optimizes comparisons against Infinities,
6298 either +Inf or -Inf.
6300 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6301 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6302 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6304 The function returns the constant folded tree if a simplification
6305 can be made, and NULL_TREE otherwise. */
6307 static tree
6308 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6309 tree arg0, tree arg1)
6311 enum machine_mode mode;
6312 REAL_VALUE_TYPE max;
6313 tree temp;
6314 bool neg;
6316 mode = TYPE_MODE (TREE_TYPE (arg0));
6318 /* For negative infinity swap the sense of the comparison. */
6319 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6320 if (neg)
6321 code = swap_tree_comparison (code);
6323 switch (code)
6325 case GT_EXPR:
6326 /* x > +Inf is always false, if with ignore sNANs. */
6327 if (HONOR_SNANS (mode))
6328 return NULL_TREE;
6329 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6331 case LE_EXPR:
6332 /* x <= +Inf is always true, if we don't case about NaNs. */
6333 if (! HONOR_NANS (mode))
6334 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6336 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6337 arg0 = save_expr (arg0);
6338 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6340 case EQ_EXPR:
6341 case GE_EXPR:
6342 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6343 real_maxval (&max, neg, mode);
6344 return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6345 arg0, build_real (TREE_TYPE (arg0), max));
6347 case LT_EXPR:
6348 /* x < +Inf is always equal to x <= DBL_MAX. */
6349 real_maxval (&max, neg, mode);
6350 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6351 arg0, build_real (TREE_TYPE (arg0), max));
6353 case NE_EXPR:
6354 /* x != +Inf is always equal to !(x > DBL_MAX). */
6355 real_maxval (&max, neg, mode);
6356 if (! HONOR_NANS (mode))
6357 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6358 arg0, build_real (TREE_TYPE (arg0), max));
6360 temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6361 arg0, build_real (TREE_TYPE (arg0), max));
6362 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6364 default:
6365 break;
6368 return NULL_TREE;
6371 /* Subroutine of fold() that optimizes comparisons of a division by
6372 a nonzero integer constant against an integer constant, i.e.
6373 X/C1 op C2.
6375 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6376 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6377 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6379 The function returns the constant folded tree if a simplification
6380 can be made, and NULL_TREE otherwise. */
6382 static tree
6383 fold_div_compare (location_t loc,
6384 enum tree_code code, tree type, tree arg0, tree arg1)
6386 tree prod, tmp, hi, lo;
6387 tree arg00 = TREE_OPERAND (arg0, 0);
6388 tree arg01 = TREE_OPERAND (arg0, 1);
6389 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6390 bool neg_overflow = false;
6391 bool overflow;
6393 /* We have to do this the hard way to detect unsigned overflow.
6394 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6395 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6396 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6397 neg_overflow = false;
6399 if (sign == UNSIGNED)
6401 tmp = int_const_binop (MINUS_EXPR, arg01,
6402 build_int_cst (TREE_TYPE (arg01), 1));
6403 lo = prod;
6405 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6406 val = wi::add (prod, tmp, sign, &overflow);
6407 hi = force_fit_type (TREE_TYPE (arg00), val,
6408 -1, overflow | TREE_OVERFLOW (prod));
6410 else if (tree_int_cst_sgn (arg01) >= 0)
6412 tmp = int_const_binop (MINUS_EXPR, arg01,
6413 build_int_cst (TREE_TYPE (arg01), 1));
6414 switch (tree_int_cst_sgn (arg1))
6416 case -1:
6417 neg_overflow = true;
6418 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6419 hi = prod;
6420 break;
6422 case 0:
6423 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6424 hi = tmp;
6425 break;
6427 case 1:
6428 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6429 lo = prod;
6430 break;
6432 default:
6433 gcc_unreachable ();
6436 else
6438 /* A negative divisor reverses the relational operators. */
6439 code = swap_tree_comparison (code);
6441 tmp = int_const_binop (PLUS_EXPR, arg01,
6442 build_int_cst (TREE_TYPE (arg01), 1));
6443 switch (tree_int_cst_sgn (arg1))
6445 case -1:
6446 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6447 lo = prod;
6448 break;
6450 case 0:
6451 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6452 lo = tmp;
6453 break;
6455 case 1:
6456 neg_overflow = true;
6457 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6458 hi = prod;
6459 break;
6461 default:
6462 gcc_unreachable ();
6466 switch (code)
6468 case EQ_EXPR:
6469 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6470 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6471 if (TREE_OVERFLOW (hi))
6472 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6473 if (TREE_OVERFLOW (lo))
6474 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6475 return build_range_check (loc, type, arg00, 1, lo, hi);
6477 case NE_EXPR:
6478 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6479 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6480 if (TREE_OVERFLOW (hi))
6481 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6482 if (TREE_OVERFLOW (lo))
6483 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6484 return build_range_check (loc, type, arg00, 0, lo, hi);
6486 case LT_EXPR:
6487 if (TREE_OVERFLOW (lo))
6489 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6490 return omit_one_operand_loc (loc, type, tmp, arg00);
6492 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6494 case LE_EXPR:
6495 if (TREE_OVERFLOW (hi))
6497 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6498 return omit_one_operand_loc (loc, type, tmp, arg00);
6500 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6502 case GT_EXPR:
6503 if (TREE_OVERFLOW (hi))
6505 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6506 return omit_one_operand_loc (loc, type, tmp, arg00);
6508 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6510 case GE_EXPR:
6511 if (TREE_OVERFLOW (lo))
6513 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6514 return omit_one_operand_loc (loc, type, tmp, arg00);
6516 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6518 default:
6519 break;
6522 return NULL_TREE;
6526 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6527 equality/inequality test, then return a simplified form of the test
6528 using a sign testing. Otherwise return NULL. TYPE is the desired
6529 result type. */
6531 static tree
6532 fold_single_bit_test_into_sign_test (location_t loc,
6533 enum tree_code code, tree arg0, tree arg1,
6534 tree result_type)
6536 /* If this is testing a single bit, we can optimize the test. */
6537 if ((code == NE_EXPR || code == EQ_EXPR)
6538 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6539 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6541 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6542 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6543 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6545 if (arg00 != NULL_TREE
6546 /* This is only a win if casting to a signed type is cheap,
6547 i.e. when arg00's type is not a partial mode. */
6548 && TYPE_PRECISION (TREE_TYPE (arg00))
6549 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6551 tree stype = signed_type_for (TREE_TYPE (arg00));
6552 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6553 result_type,
6554 fold_convert_loc (loc, stype, arg00),
6555 build_int_cst (stype, 0));
6559 return NULL_TREE;
6562 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6563 equality/inequality test, then return a simplified form of
6564 the test using shifts and logical operations. Otherwise return
6565 NULL. TYPE is the desired result type. */
6567 tree
6568 fold_single_bit_test (location_t loc, enum tree_code code,
6569 tree arg0, tree arg1, tree result_type)
6571 /* If this is testing a single bit, we can optimize the test. */
6572 if ((code == NE_EXPR || code == EQ_EXPR)
6573 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6574 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6576 tree inner = TREE_OPERAND (arg0, 0);
6577 tree type = TREE_TYPE (arg0);
6578 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6579 enum machine_mode operand_mode = TYPE_MODE (type);
6580 int ops_unsigned;
6581 tree signed_type, unsigned_type, intermediate_type;
6582 tree tem, one;
6584 /* First, see if we can fold the single bit test into a sign-bit
6585 test. */
6586 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6587 result_type);
6588 if (tem)
6589 return tem;
6591 /* Otherwise we have (A & C) != 0 where C is a single bit,
6592 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6593 Similarly for (A & C) == 0. */
6595 /* If INNER is a right shift of a constant and it plus BITNUM does
6596 not overflow, adjust BITNUM and INNER. */
6597 if (TREE_CODE (inner) == RSHIFT_EXPR
6598 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6599 && bitnum < TYPE_PRECISION (type)
6600 && wi::ltu_p (TREE_OPERAND (inner, 1),
6601 TYPE_PRECISION (type) - bitnum))
6603 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6604 inner = TREE_OPERAND (inner, 0);
6607 /* If we are going to be able to omit the AND below, we must do our
6608 operations as unsigned. If we must use the AND, we have a choice.
6609 Normally unsigned is faster, but for some machines signed is. */
6610 #ifdef LOAD_EXTEND_OP
6611 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6612 && !flag_syntax_only) ? 0 : 1;
6613 #else
6614 ops_unsigned = 1;
6615 #endif
6617 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6618 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6619 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6620 inner = fold_convert_loc (loc, intermediate_type, inner);
6622 if (bitnum != 0)
6623 inner = build2 (RSHIFT_EXPR, intermediate_type,
6624 inner, size_int (bitnum));
6626 one = build_int_cst (intermediate_type, 1);
6628 if (code == EQ_EXPR)
6629 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6631 /* Put the AND last so it can combine with more things. */
6632 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6634 /* Make sure to return the proper type. */
6635 inner = fold_convert_loc (loc, result_type, inner);
6637 return inner;
6639 return NULL_TREE;
6642 /* Check whether we are allowed to reorder operands arg0 and arg1,
6643 such that the evaluation of arg1 occurs before arg0. */
6645 static bool
6646 reorder_operands_p (const_tree arg0, const_tree arg1)
6648 if (! flag_evaluation_order)
6649 return true;
6650 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6651 return true;
6652 return ! TREE_SIDE_EFFECTS (arg0)
6653 && ! TREE_SIDE_EFFECTS (arg1);
6656 /* Test whether it is preferable two swap two operands, ARG0 and
6657 ARG1, for example because ARG0 is an integer constant and ARG1
6658 isn't. If REORDER is true, only recommend swapping if we can
6659 evaluate the operands in reverse order. */
6661 bool
6662 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6664 if (CONSTANT_CLASS_P (arg1))
6665 return 0;
6666 if (CONSTANT_CLASS_P (arg0))
6667 return 1;
6669 STRIP_SIGN_NOPS (arg0);
6670 STRIP_SIGN_NOPS (arg1);
6672 if (TREE_CONSTANT (arg1))
6673 return 0;
6674 if (TREE_CONSTANT (arg0))
6675 return 1;
6677 if (reorder && flag_evaluation_order
6678 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6679 return 0;
6681 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6682 for commutative and comparison operators. Ensuring a canonical
6683 form allows the optimizers to find additional redundancies without
6684 having to explicitly check for both orderings. */
6685 if (TREE_CODE (arg0) == SSA_NAME
6686 && TREE_CODE (arg1) == SSA_NAME
6687 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6688 return 1;
6690 /* Put SSA_NAMEs last. */
6691 if (TREE_CODE (arg1) == SSA_NAME)
6692 return 0;
6693 if (TREE_CODE (arg0) == SSA_NAME)
6694 return 1;
6696 /* Put variables last. */
6697 if (DECL_P (arg1))
6698 return 0;
6699 if (DECL_P (arg0))
6700 return 1;
6702 return 0;
6705 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6706 ARG0 is extended to a wider type. */
6708 static tree
6709 fold_widened_comparison (location_t loc, enum tree_code code,
6710 tree type, tree arg0, tree arg1)
6712 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6713 tree arg1_unw;
6714 tree shorter_type, outer_type;
6715 tree min, max;
6716 bool above, below;
6718 if (arg0_unw == arg0)
6719 return NULL_TREE;
6720 shorter_type = TREE_TYPE (arg0_unw);
6722 #ifdef HAVE_canonicalize_funcptr_for_compare
6723 /* Disable this optimization if we're casting a function pointer
6724 type on targets that require function pointer canonicalization. */
6725 if (HAVE_canonicalize_funcptr_for_compare
6726 && TREE_CODE (shorter_type) == POINTER_TYPE
6727 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6728 return NULL_TREE;
6729 #endif
6731 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6732 return NULL_TREE;
6734 arg1_unw = get_unwidened (arg1, NULL_TREE);
6736 /* If possible, express the comparison in the shorter mode. */
6737 if ((code == EQ_EXPR || code == NE_EXPR
6738 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6739 && (TREE_TYPE (arg1_unw) == shorter_type
6740 || ((TYPE_PRECISION (shorter_type)
6741 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6742 && (TYPE_UNSIGNED (shorter_type)
6743 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6744 || (TREE_CODE (arg1_unw) == INTEGER_CST
6745 && (TREE_CODE (shorter_type) == INTEGER_TYPE
6746 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6747 && int_fits_type_p (arg1_unw, shorter_type))))
6748 return fold_build2_loc (loc, code, type, arg0_unw,
6749 fold_convert_loc (loc, shorter_type, arg1_unw));
6751 if (TREE_CODE (arg1_unw) != INTEGER_CST
6752 || TREE_CODE (shorter_type) != INTEGER_TYPE
6753 || !int_fits_type_p (arg1_unw, shorter_type))
6754 return NULL_TREE;
6756 /* If we are comparing with the integer that does not fit into the range
6757 of the shorter type, the result is known. */
6758 outer_type = TREE_TYPE (arg1_unw);
6759 min = lower_bound_in_type (outer_type, shorter_type);
6760 max = upper_bound_in_type (outer_type, shorter_type);
6762 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6763 max, arg1_unw));
6764 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6765 arg1_unw, min));
6767 switch (code)
6769 case EQ_EXPR:
6770 if (above || below)
6771 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6772 break;
6774 case NE_EXPR:
6775 if (above || below)
6776 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6777 break;
6779 case LT_EXPR:
6780 case LE_EXPR:
6781 if (above)
6782 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6783 else if (below)
6784 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6786 case GT_EXPR:
6787 case GE_EXPR:
6788 if (above)
6789 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6790 else if (below)
6791 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6793 default:
6794 break;
6797 return NULL_TREE;
6800 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6801 ARG0 just the signedness is changed. */
6803 static tree
6804 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6805 tree arg0, tree arg1)
6807 tree arg0_inner;
6808 tree inner_type, outer_type;
6810 if (!CONVERT_EXPR_P (arg0))
6811 return NULL_TREE;
6813 outer_type = TREE_TYPE (arg0);
6814 arg0_inner = TREE_OPERAND (arg0, 0);
6815 inner_type = TREE_TYPE (arg0_inner);
6817 #ifdef HAVE_canonicalize_funcptr_for_compare
6818 /* Disable this optimization if we're casting a function pointer
6819 type on targets that require function pointer canonicalization. */
6820 if (HAVE_canonicalize_funcptr_for_compare
6821 && TREE_CODE (inner_type) == POINTER_TYPE
6822 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6823 return NULL_TREE;
6824 #endif
6826 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6827 return NULL_TREE;
6829 if (TREE_CODE (arg1) != INTEGER_CST
6830 && !(CONVERT_EXPR_P (arg1)
6831 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6832 return NULL_TREE;
6834 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6835 && code != NE_EXPR
6836 && code != EQ_EXPR)
6837 return NULL_TREE;
6839 if (POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6840 return NULL_TREE;
6842 if (TREE_CODE (arg1) == INTEGER_CST)
6843 arg1 = force_fit_type (inner_type, wi::to_widest (arg1), 0,
6844 TREE_OVERFLOW (arg1));
6845 else
6846 arg1 = fold_convert_loc (loc, inner_type, arg1);
6848 return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6852 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6853 means A >= Y && A != MAX, but in this case we know that
6854 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6856 static tree
6857 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6859 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6861 if (TREE_CODE (bound) == LT_EXPR)
6862 a = TREE_OPERAND (bound, 0);
6863 else if (TREE_CODE (bound) == GT_EXPR)
6864 a = TREE_OPERAND (bound, 1);
6865 else
6866 return NULL_TREE;
6868 typea = TREE_TYPE (a);
6869 if (!INTEGRAL_TYPE_P (typea)
6870 && !POINTER_TYPE_P (typea))
6871 return NULL_TREE;
6873 if (TREE_CODE (ineq) == LT_EXPR)
6875 a1 = TREE_OPERAND (ineq, 1);
6876 y = TREE_OPERAND (ineq, 0);
6878 else if (TREE_CODE (ineq) == GT_EXPR)
6880 a1 = TREE_OPERAND (ineq, 0);
6881 y = TREE_OPERAND (ineq, 1);
6883 else
6884 return NULL_TREE;
6886 if (TREE_TYPE (a1) != typea)
6887 return NULL_TREE;
6889 if (POINTER_TYPE_P (typea))
6891 /* Convert the pointer types into integer before taking the difference. */
6892 tree ta = fold_convert_loc (loc, ssizetype, a);
6893 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6894 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6896 else
6897 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6899 if (!diff || !integer_onep (diff))
6900 return NULL_TREE;
6902 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6905 /* Fold a sum or difference of at least one multiplication.
6906 Returns the folded tree or NULL if no simplification could be made. */
6908 static tree
6909 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6910 tree arg0, tree arg1)
6912 tree arg00, arg01, arg10, arg11;
6913 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6915 /* (A * C) +- (B * C) -> (A+-B) * C.
6916 (A * C) +- A -> A * (C+-1).
6917 We are most concerned about the case where C is a constant,
6918 but other combinations show up during loop reduction. Since
6919 it is not difficult, try all four possibilities. */
6921 if (TREE_CODE (arg0) == MULT_EXPR)
6923 arg00 = TREE_OPERAND (arg0, 0);
6924 arg01 = TREE_OPERAND (arg0, 1);
6926 else if (TREE_CODE (arg0) == INTEGER_CST)
6928 arg00 = build_one_cst (type);
6929 arg01 = arg0;
6931 else
6933 /* We cannot generate constant 1 for fract. */
6934 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6935 return NULL_TREE;
6936 arg00 = arg0;
6937 arg01 = build_one_cst (type);
6939 if (TREE_CODE (arg1) == MULT_EXPR)
6941 arg10 = TREE_OPERAND (arg1, 0);
6942 arg11 = TREE_OPERAND (arg1, 1);
6944 else if (TREE_CODE (arg1) == INTEGER_CST)
6946 arg10 = build_one_cst (type);
6947 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6948 the purpose of this canonicalization. */
6949 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
6950 && negate_expr_p (arg1)
6951 && code == PLUS_EXPR)
6953 arg11 = negate_expr (arg1);
6954 code = MINUS_EXPR;
6956 else
6957 arg11 = arg1;
6959 else
6961 /* We cannot generate constant 1 for fract. */
6962 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6963 return NULL_TREE;
6964 arg10 = arg1;
6965 arg11 = build_one_cst (type);
6967 same = NULL_TREE;
6969 if (operand_equal_p (arg01, arg11, 0))
6970 same = arg01, alt0 = arg00, alt1 = arg10;
6971 else if (operand_equal_p (arg00, arg10, 0))
6972 same = arg00, alt0 = arg01, alt1 = arg11;
6973 else if (operand_equal_p (arg00, arg11, 0))
6974 same = arg00, alt0 = arg01, alt1 = arg10;
6975 else if (operand_equal_p (arg01, arg10, 0))
6976 same = arg01, alt0 = arg00, alt1 = arg11;
6978 /* No identical multiplicands; see if we can find a common
6979 power-of-two factor in non-power-of-two multiplies. This
6980 can help in multi-dimensional array access. */
6981 else if (tree_fits_shwi_p (arg01)
6982 && tree_fits_shwi_p (arg11))
6984 HOST_WIDE_INT int01, int11, tmp;
6985 bool swap = false;
6986 tree maybe_same;
6987 int01 = tree_to_shwi (arg01);
6988 int11 = tree_to_shwi (arg11);
6990 /* Move min of absolute values to int11. */
6991 if (absu_hwi (int01) < absu_hwi (int11))
6993 tmp = int01, int01 = int11, int11 = tmp;
6994 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6995 maybe_same = arg01;
6996 swap = true;
6998 else
6999 maybe_same = arg11;
7001 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7002 /* The remainder should not be a constant, otherwise we
7003 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7004 increased the number of multiplications necessary. */
7005 && TREE_CODE (arg10) != INTEGER_CST)
7007 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7008 build_int_cst (TREE_TYPE (arg00),
7009 int01 / int11));
7010 alt1 = arg10;
7011 same = maybe_same;
7012 if (swap)
7013 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7017 if (same)
7018 return fold_build2_loc (loc, MULT_EXPR, type,
7019 fold_build2_loc (loc, code, type,
7020 fold_convert_loc (loc, type, alt0),
7021 fold_convert_loc (loc, type, alt1)),
7022 fold_convert_loc (loc, type, same));
7024 return NULL_TREE;
7027 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7028 specified by EXPR into the buffer PTR of length LEN bytes.
7029 If REVERSE is true, the INTEGER_CST is interpreted in reverse
7030 storage order wrt the target order.
7031 Return the number of bytes placed in the buffer, or zero
7032 upon failure. */
7034 static int
7035 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off,
7036 bool reverse)
7038 tree type = TREE_TYPE (expr);
7039 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7040 int byte, offset, word, words;
7041 unsigned char value;
7043 if ((off == -1 && total_bytes > len)
7044 || off >= total_bytes)
7045 return 0;
7046 if (off == -1)
7047 off = 0;
7048 words = total_bytes / UNITS_PER_WORD;
7050 for (byte = 0; byte < total_bytes; byte++)
7052 int bitpos = byte * BITS_PER_UNIT;
7053 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7054 number of bytes. */
7055 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7057 if (total_bytes > UNITS_PER_WORD)
7059 word = byte / UNITS_PER_WORD;
7060 if (reverse ? !WORDS_BIG_ENDIAN : WORDS_BIG_ENDIAN)
7061 word = (words - 1) - word;
7062 offset = word * UNITS_PER_WORD;
7063 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7064 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7065 else
7066 offset += byte % UNITS_PER_WORD;
7068 else
7070 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7071 offset = (total_bytes - 1) - byte;
7072 else
7073 offset = byte;
7075 if (offset >= off
7076 && offset - off < len)
7077 ptr[offset - off] = value;
7079 return MIN (len, total_bytes - off);
7083 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7084 specified by EXPR into the buffer PTR of length LEN bytes.
7085 If REVERSE is true, the INTEGER_CST is interpreted in reverse
7086 storage order wrt the target order.
7087 Return the number of bytes placed in the buffer, or zero
7088 upon failure. */
7090 static int
7091 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off,
7092 bool reverse)
7094 tree type = TREE_TYPE (expr);
7095 enum machine_mode mode = TYPE_MODE (type);
7096 int total_bytes = GET_MODE_SIZE (mode);
7097 FIXED_VALUE_TYPE value;
7098 tree i_value, i_type;
7100 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7101 return 0;
7103 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7105 if (NULL_TREE == i_type
7106 || TYPE_PRECISION (i_type) != total_bytes)
7107 return 0;
7109 value = TREE_FIXED_CST (expr);
7110 i_value = double_int_to_tree (i_type, value.data);
7112 return native_encode_int (i_value, ptr, len, off, reverse);
7116 /* Subroutine of native_encode_expr. Encode the REAL_CST
7117 specified by EXPR into the buffer PTR of length LEN bytes.
7118 If REVERSE is true, the INTEGER_CST is interpreted in reverse
7119 storage order wrt the target order.
7120 Return the number of bytes placed in the buffer, or zero
7121 upon failure. */
7123 static int
7124 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off,
7125 bool reverse)
7127 tree type = TREE_TYPE (expr);
7128 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7129 int byte, offset, word, words, bitpos;
7130 unsigned char value;
7132 /* There are always 32 bits in each long, no matter the size of
7133 the hosts long. We handle floating point representations with
7134 up to 192 bits. */
7135 long tmp[6];
7137 if ((off == -1 && total_bytes > len)
7138 || off >= total_bytes)
7139 return 0;
7140 if (off == -1)
7141 off = 0;
7142 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7144 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7146 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7147 bitpos += BITS_PER_UNIT)
7149 byte = (bitpos / BITS_PER_UNIT) & 3;
7150 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7152 if (UNITS_PER_WORD < 4)
7154 word = byte / UNITS_PER_WORD;
7155 if (reverse ? !WORDS_BIG_ENDIAN : WORDS_BIG_ENDIAN)
7156 word = (words - 1) - word;
7157 offset = word * UNITS_PER_WORD;
7158 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7159 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7160 else
7161 offset += byte % UNITS_PER_WORD;
7163 else
7165 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
7166 offset = 3 - byte;
7167 else
7168 offset = byte;
7170 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7171 if (offset >= off
7172 && offset - off < len)
7173 ptr[offset - off] = value;
7175 return MIN (len, total_bytes - off);
7178 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7179 specified by EXPR into the buffer PTR of length LEN bytes.
7180 If REVERSE is true, the INTEGER_CST is interpreted in reverse
7181 storage order wrt the target order.
7182 Return the number of bytes placed in the buffer, or zero
7183 upon failure. */
7185 static int
7186 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off,
7187 bool reverse)
7189 int rsize, isize;
7190 tree part;
7192 part = TREE_REALPART (expr);
7193 rsize = native_encode_expr (part, ptr, len, off, reverse);
7194 if (off == -1
7195 && rsize == 0)
7196 return 0;
7197 part = TREE_IMAGPART (expr);
7198 if (off != -1)
7199 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7200 isize = native_encode_expr (part, ptr+rsize, len-rsize, off, reverse);
7201 if (off == -1
7202 && isize != rsize)
7203 return 0;
7204 return rsize + isize;
7208 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7209 specified by EXPR into the buffer PTR of length LEN bytes.
7210 If REVERSE is true, the INTEGER_CST is interpreted in reverse
7211 storage order wrt the target order.
7212 Return the number of bytes placed in the buffer, or zero
7213 upon failure. */
7215 static int
7216 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off,
7217 bool reverse)
7219 unsigned i, count;
7220 int size, offset;
7221 tree itype, elem;
7223 offset = 0;
7224 count = VECTOR_CST_NELTS (expr);
7225 itype = TREE_TYPE (TREE_TYPE (expr));
7226 size = GET_MODE_SIZE (TYPE_MODE (itype));
7227 for (i = 0; i < count; i++)
7229 if (off >= size)
7231 off -= size;
7232 continue;
7234 elem = VECTOR_CST_ELT (expr, i);
7235 int res = native_encode_expr (elem, ptr+offset, len-offset, off, reverse);
7236 if ((off == -1 && res != size)
7237 || res == 0)
7238 return 0;
7239 offset += res;
7240 if (offset >= len)
7241 return offset;
7242 if (off != -1)
7243 off = 0;
7245 return offset;
7249 /* Subroutine of native_encode_expr. Encode the STRING_CST
7250 specified by EXPR into the buffer PTR of length LEN bytes.
7251 Return the number of bytes placed in the buffer, or zero
7252 upon failure. */
7254 static int
7255 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7257 tree type = TREE_TYPE (expr);
7258 HOST_WIDE_INT total_bytes;
7260 if (TREE_CODE (type) != ARRAY_TYPE
7261 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7262 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7263 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7264 return 0;
7265 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7266 if ((off == -1 && total_bytes > len)
7267 || off >= total_bytes)
7268 return 0;
7269 if (off == -1)
7270 off = 0;
7271 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7273 int written = 0;
7274 if (off < TREE_STRING_LENGTH (expr))
7276 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7277 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7279 memset (ptr + written, 0,
7280 MIN (total_bytes - written, len - written));
7282 else
7283 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7284 return MIN (total_bytes - off, len);
7288 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7289 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7290 buffer PTR of length LEN bytes. If OFF is not -1 then start
7291 the encoding at byte offset OFF and encode at most LEN bytes.
7292 If REVERSE is true, the _CST object is interpreted in reverse
7293 storage order wrt the target order.
7294 Return the number of bytes placed in the buffer, or zero upon failure. */
7297 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off,
7298 bool reverse)
7300 switch (TREE_CODE (expr))
7302 case INTEGER_CST:
7303 return native_encode_int (expr, ptr, len, off, reverse);
7305 case REAL_CST:
7306 return native_encode_real (expr, ptr, len, off, reverse);
7308 case FIXED_CST:
7309 return native_encode_fixed (expr, ptr, len, off, reverse);
7311 case COMPLEX_CST:
7312 return native_encode_complex (expr, ptr, len, off, reverse);
7314 case VECTOR_CST:
7315 return native_encode_vector (expr, ptr, len, off, reverse);
7317 case STRING_CST:
7318 return native_encode_string (expr, ptr, len, off);
7320 default:
7321 return 0;
7326 /* Subroutine of native_interpret_expr. Interpret the contents of
7327 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7328 If the buffer cannot be interpreted, return NULL_TREE. */
7330 static tree
7331 native_interpret_int (tree type, const unsigned char *ptr, int len)
7333 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7335 if (total_bytes > len
7336 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7337 return NULL_TREE;
7339 wide_int result = wi::from_buffer (ptr, total_bytes);
7341 return wide_int_to_tree (type, result);
7345 /* Subroutine of native_interpret_expr. Interpret the contents of
7346 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7347 If the buffer cannot be interpreted, return NULL_TREE. */
7349 static tree
7350 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7352 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7353 double_int result;
7354 FIXED_VALUE_TYPE fixed_value;
7356 if (total_bytes > len
7357 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7358 return NULL_TREE;
7360 result = double_int::from_buffer (ptr, total_bytes);
7361 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7363 return build_fixed (type, fixed_value);
7367 /* Subroutine of native_interpret_expr. Interpret the contents of
7368 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7369 If the buffer cannot be interpreted, return NULL_TREE. */
7371 static tree
7372 native_interpret_real (tree type, const unsigned char *ptr, int len)
7374 enum machine_mode mode = TYPE_MODE (type);
7375 int total_bytes = GET_MODE_SIZE (mode);
7376 int byte, offset, word, words, bitpos;
7377 unsigned char value;
7378 /* There are always 32 bits in each long, no matter the size of
7379 the hosts long. We handle floating point representations with
7380 up to 192 bits. */
7381 REAL_VALUE_TYPE r;
7382 long tmp[6];
7384 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7385 if (total_bytes > len || total_bytes > 24)
7386 return NULL_TREE;
7387 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7389 memset (tmp, 0, sizeof (tmp));
7390 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7391 bitpos += BITS_PER_UNIT)
7393 byte = (bitpos / BITS_PER_UNIT) & 3;
7394 if (UNITS_PER_WORD < 4)
7396 word = byte / UNITS_PER_WORD;
7397 if (WORDS_BIG_ENDIAN)
7398 word = (words - 1) - word;
7399 offset = word * UNITS_PER_WORD;
7400 if (BYTES_BIG_ENDIAN)
7401 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7402 else
7403 offset += byte % UNITS_PER_WORD;
7405 else
7406 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7407 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7409 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7412 real_from_target (&r, tmp, mode);
7413 return build_real (type, r);
7417 /* Subroutine of native_interpret_expr. Interpret the contents of
7418 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7419 If the buffer cannot be interpreted, return NULL_TREE. */
7421 static tree
7422 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7424 tree etype, rpart, ipart;
7425 int size;
7427 etype = TREE_TYPE (type);
7428 size = GET_MODE_SIZE (TYPE_MODE (etype));
7429 if (size * 2 > len)
7430 return NULL_TREE;
7431 rpart = native_interpret_expr (etype, ptr, size);
7432 if (!rpart)
7433 return NULL_TREE;
7434 ipart = native_interpret_expr (etype, ptr+size, size);
7435 if (!ipart)
7436 return NULL_TREE;
7437 return build_complex (type, rpart, ipart);
7441 /* Subroutine of native_interpret_expr. Interpret the contents of
7442 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7443 If the buffer cannot be interpreted, return NULL_TREE. */
7445 static tree
7446 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7448 tree etype, elem;
7449 int i, size, count;
7450 tree *elements;
7452 etype = TREE_TYPE (type);
7453 size = GET_MODE_SIZE (TYPE_MODE (etype));
7454 count = TYPE_VECTOR_SUBPARTS (type);
7455 if (size * count > len)
7456 return NULL_TREE;
7458 elements = XALLOCAVEC (tree, count);
7459 for (i = count - 1; i >= 0; i--)
7461 elem = native_interpret_expr (etype, ptr+(i*size), size);
7462 if (!elem)
7463 return NULL_TREE;
7464 elements[i] = elem;
7466 return build_vector (type, elements);
7470 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7471 the buffer PTR of length LEN as a constant of type TYPE. For
7472 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7473 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7474 return NULL_TREE. */
7476 tree
7477 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7479 switch (TREE_CODE (type))
7481 case INTEGER_TYPE:
7482 case ENUMERAL_TYPE:
7483 case BOOLEAN_TYPE:
7484 case POINTER_TYPE:
7485 case REFERENCE_TYPE:
7486 return native_interpret_int (type, ptr, len);
7488 case REAL_TYPE:
7489 return native_interpret_real (type, ptr, len);
7491 case FIXED_POINT_TYPE:
7492 return native_interpret_fixed (type, ptr, len);
7494 case COMPLEX_TYPE:
7495 return native_interpret_complex (type, ptr, len);
7497 case VECTOR_TYPE:
7498 return native_interpret_vector (type, ptr, len);
7500 default:
7501 return NULL_TREE;
7505 /* Returns true if we can interpret the contents of a native encoding
7506 as TYPE. */
7508 static bool
7509 can_native_interpret_type_p (tree type)
7511 switch (TREE_CODE (type))
7513 case INTEGER_TYPE:
7514 case ENUMERAL_TYPE:
7515 case BOOLEAN_TYPE:
7516 case POINTER_TYPE:
7517 case REFERENCE_TYPE:
7518 case FIXED_POINT_TYPE:
7519 case REAL_TYPE:
7520 case COMPLEX_TYPE:
7521 case VECTOR_TYPE:
7522 return true;
7523 default:
7524 return false;
7528 /* Fold the flipping of storage order for constant expression EXPR
7529 at compile-time. If we're unable to perform the flipping, return
7530 NULL_TREE. */
7532 tree
7533 fold_flip_storage_order (tree expr)
7535 /* We support up to 512-bit values (for V8DFmode). */
7536 unsigned char buffer[64];
7537 int len;
7539 /* Check that the host and target are sane. */
7540 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7541 return NULL_TREE;
7543 len = native_encode_expr (expr, buffer, sizeof (buffer), -1, true);
7544 if (len == 0)
7545 return NULL_TREE;
7547 return native_interpret_expr (TREE_TYPE (expr), buffer, len);
7551 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7552 TYPE at compile-time. If we're unable to perform the conversion
7553 return NULL_TREE. */
7555 static tree
7556 fold_view_convert_expr (tree type, tree expr)
7558 /* We support up to 512-bit values (for V8DFmode). */
7559 unsigned char buffer[64];
7560 int len;
7562 /* Check that the host and target are sane. */
7563 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7564 return NULL_TREE;
7566 len = native_encode_expr (expr, buffer, sizeof (buffer));
7567 if (len == 0)
7568 return NULL_TREE;
7570 return native_interpret_expr (type, buffer, len);
7573 /* Build an expression for the address of T. Folds away INDIRECT_REF
7574 to avoid confusing the gimplify process. */
7576 tree
7577 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7579 /* The size of the object is not relevant when talking about its address. */
7580 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7581 t = TREE_OPERAND (t, 0);
7583 if (TREE_CODE (t) == INDIRECT_REF)
7585 t = TREE_OPERAND (t, 0);
7587 if (TREE_TYPE (t) != ptrtype)
7588 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7590 else if (TREE_CODE (t) == MEM_REF
7591 && integer_zerop (TREE_OPERAND (t, 1)))
7592 return TREE_OPERAND (t, 0);
7593 else if (TREE_CODE (t) == MEM_REF
7594 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7595 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7596 TREE_OPERAND (t, 0),
7597 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7598 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7600 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7602 if (TREE_TYPE (t) != ptrtype)
7603 t = fold_convert_loc (loc, ptrtype, t);
7605 else
7606 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7608 return t;
7611 /* Build an expression for the address of T. */
7613 tree
7614 build_fold_addr_expr_loc (location_t loc, tree t)
7616 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7618 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7621 static bool vec_cst_ctor_to_array (tree, tree *);
7623 /* Fold a unary expression of code CODE and type TYPE with operand
7624 OP0. Return the folded expression if folding is successful.
7625 Otherwise, return NULL_TREE. */
7627 tree
7628 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7630 tree tem;
7631 tree arg0;
7632 enum tree_code_class kind = TREE_CODE_CLASS (code);
7634 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7635 && TREE_CODE_LENGTH (code) == 1);
7637 arg0 = op0;
7638 if (arg0)
7640 if (CONVERT_EXPR_CODE_P (code)
7641 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7643 /* Don't use STRIP_NOPS, because signedness of argument type
7644 matters. */
7645 STRIP_SIGN_NOPS (arg0);
7647 else
7649 /* Strip any conversions that don't change the mode. This
7650 is safe for every expression, except for a comparison
7651 expression because its signedness is derived from its
7652 operands.
7654 Note that this is done as an internal manipulation within
7655 the constant folder, in order to find the simplest
7656 representation of the arguments so that their form can be
7657 studied. In any cases, the appropriate type conversions
7658 should be put back in the tree that will get out of the
7659 constant folder. */
7660 STRIP_NOPS (arg0);
7664 if (TREE_CODE_CLASS (code) == tcc_unary)
7666 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7667 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7668 fold_build1_loc (loc, code, type,
7669 fold_convert_loc (loc, TREE_TYPE (op0),
7670 TREE_OPERAND (arg0, 1))));
7671 else if (TREE_CODE (arg0) == COND_EXPR)
7673 tree arg01 = TREE_OPERAND (arg0, 1);
7674 tree arg02 = TREE_OPERAND (arg0, 2);
7675 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7676 arg01 = fold_build1_loc (loc, code, type,
7677 fold_convert_loc (loc,
7678 TREE_TYPE (op0), arg01));
7679 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7680 arg02 = fold_build1_loc (loc, code, type,
7681 fold_convert_loc (loc,
7682 TREE_TYPE (op0), arg02));
7683 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7684 arg01, arg02);
7686 /* If this was a conversion, and all we did was to move into
7687 inside the COND_EXPR, bring it back out. But leave it if
7688 it is a conversion from integer to integer and the
7689 result precision is no wider than a word since such a
7690 conversion is cheap and may be optimized away by combine,
7691 while it couldn't if it were outside the COND_EXPR. Then return
7692 so we don't get into an infinite recursion loop taking the
7693 conversion out and then back in. */
7695 if ((CONVERT_EXPR_CODE_P (code)
7696 || code == NON_LVALUE_EXPR)
7697 && TREE_CODE (tem) == COND_EXPR
7698 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7699 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7700 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7701 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7702 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7703 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7704 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7705 && (INTEGRAL_TYPE_P
7706 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7707 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7708 || flag_syntax_only))
7709 tem = build1_loc (loc, code, type,
7710 build3 (COND_EXPR,
7711 TREE_TYPE (TREE_OPERAND
7712 (TREE_OPERAND (tem, 1), 0)),
7713 TREE_OPERAND (tem, 0),
7714 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7715 TREE_OPERAND (TREE_OPERAND (tem, 2),
7716 0)));
7717 return tem;
7721 switch (code)
7723 case PAREN_EXPR:
7724 /* Re-association barriers around constants and other re-association
7725 barriers can be removed. */
7726 if (CONSTANT_CLASS_P (op0)
7727 || TREE_CODE (op0) == PAREN_EXPR)
7728 return fold_convert_loc (loc, type, op0);
7729 return NULL_TREE;
7731 case NON_LVALUE_EXPR:
7732 if (!maybe_lvalue_p (op0))
7733 return fold_convert_loc (loc, type, op0);
7734 return NULL_TREE;
7736 CASE_CONVERT:
7737 case FLOAT_EXPR:
7738 case FIX_TRUNC_EXPR:
7739 if (TREE_TYPE (op0) == type)
7740 return op0;
7742 if (COMPARISON_CLASS_P (op0))
7744 /* If we have (type) (a CMP b) and type is an integral type, return
7745 new expression involving the new type. Canonicalize
7746 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7747 non-integral type.
7748 Do not fold the result as that would not simplify further, also
7749 folding again results in recursions. */
7750 if (TREE_CODE (type) == BOOLEAN_TYPE)
7751 return build2_loc (loc, TREE_CODE (op0), type,
7752 TREE_OPERAND (op0, 0),
7753 TREE_OPERAND (op0, 1));
7754 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7755 && TREE_CODE (type) != VECTOR_TYPE)
7756 return build3_loc (loc, COND_EXPR, type, op0,
7757 constant_boolean_node (true, type),
7758 constant_boolean_node (false, type));
7761 /* Handle cases of two conversions in a row. */
7762 if (CONVERT_EXPR_P (op0))
7764 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7765 tree inter_type = TREE_TYPE (op0);
7766 int inside_int = INTEGRAL_TYPE_P (inside_type);
7767 int inside_ptr = POINTER_TYPE_P (inside_type);
7768 int inside_float = FLOAT_TYPE_P (inside_type);
7769 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7770 unsigned int inside_prec = TYPE_PRECISION (inside_type);
7771 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7772 int inter_int = INTEGRAL_TYPE_P (inter_type);
7773 int inter_ptr = POINTER_TYPE_P (inter_type);
7774 int inter_float = FLOAT_TYPE_P (inter_type);
7775 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7776 unsigned int inter_prec = TYPE_PRECISION (inter_type);
7777 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7778 int final_int = INTEGRAL_TYPE_P (type);
7779 int final_ptr = POINTER_TYPE_P (type);
7780 int final_float = FLOAT_TYPE_P (type);
7781 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7782 unsigned int final_prec = TYPE_PRECISION (type);
7783 int final_unsignedp = TYPE_UNSIGNED (type);
7785 /* In addition to the cases of two conversions in a row
7786 handled below, if we are converting something to its own
7787 type via an object of identical or wider precision, neither
7788 conversion is needed. */
7789 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7790 && (((inter_int || inter_ptr) && final_int)
7791 || (inter_float && final_float))
7792 && inter_prec >= final_prec)
7793 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7795 /* Likewise, if the intermediate and initial types are either both
7796 float or both integer, we don't need the middle conversion if the
7797 former is wider than the latter and doesn't change the signedness
7798 (for integers). Avoid this if the final type is a pointer since
7799 then we sometimes need the middle conversion. Likewise if the
7800 final type has a precision not equal to the size of its mode. */
7801 if (((inter_int && inside_int)
7802 || (inter_float && inside_float)
7803 || (inter_vec && inside_vec))
7804 && inter_prec >= inside_prec
7805 && (inter_float || inter_vec
7806 || inter_unsignedp == inside_unsignedp)
7807 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
7808 && TYPE_MODE (type) == TYPE_MODE (inter_type))
7809 && ! final_ptr
7810 && (! final_vec || inter_prec == inside_prec))
7811 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7813 /* If we have a sign-extension of a zero-extended value, we can
7814 replace that by a single zero-extension. Likewise if the
7815 final conversion does not change precision we can drop the
7816 intermediate conversion. */
7817 if (inside_int && inter_int && final_int
7818 && ((inside_prec < inter_prec && inter_prec < final_prec
7819 && inside_unsignedp && !inter_unsignedp)
7820 || final_prec == inter_prec))
7821 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7823 /* Two conversions in a row are not needed unless:
7824 - some conversion is floating-point (overstrict for now), or
7825 - some conversion is a vector (overstrict for now), or
7826 - the intermediate type is narrower than both initial and
7827 final, or
7828 - the intermediate type and innermost type differ in signedness,
7829 and the outermost type is wider than the intermediate, or
7830 - the initial type is a pointer type and the precisions of the
7831 intermediate and final types differ, or
7832 - the final type is a pointer type and the precisions of the
7833 initial and intermediate types differ. */
7834 if (! inside_float && ! inter_float && ! final_float
7835 && ! inside_vec && ! inter_vec && ! final_vec
7836 && (inter_prec >= inside_prec || inter_prec >= final_prec)
7837 && ! (inside_int && inter_int
7838 && inter_unsignedp != inside_unsignedp
7839 && inter_prec < final_prec)
7840 && ((inter_unsignedp && inter_prec > inside_prec)
7841 == (final_unsignedp && final_prec > inter_prec))
7842 && ! (inside_ptr && inter_prec != final_prec)
7843 && ! (final_ptr && inside_prec != inter_prec)
7844 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
7845 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7846 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7849 /* Handle (T *)&A.B.C for A being of type T and B and C
7850 living at offset zero. This occurs frequently in
7851 C++ upcasting and then accessing the base. */
7852 if (TREE_CODE (op0) == ADDR_EXPR
7853 && POINTER_TYPE_P (type)
7854 && handled_component_p (TREE_OPERAND (op0, 0)))
7856 HOST_WIDE_INT bitsize, bitpos;
7857 tree offset;
7858 enum machine_mode mode;
7859 int unsignedp, reversep, volatilep;
7860 tree base
7861 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7862 &offset, &mode, &unsignedp, &reversep,
7863 &volatilep, false);
7864 /* If the reference was to a (constant) zero offset, we can use
7865 the address of the base if it has the same base type
7866 as the result type and the pointer type is unqualified. */
7867 if (! offset && bitpos == 0
7868 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7869 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7870 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7871 return fold_convert_loc (loc, type,
7872 build_fold_addr_expr_loc (loc, base));
7875 if (TREE_CODE (op0) == MODIFY_EXPR
7876 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7877 /* Detect assigning a bitfield. */
7878 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7879 && DECL_BIT_FIELD
7880 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7882 /* Don't leave an assignment inside a conversion
7883 unless assigning a bitfield. */
7884 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7885 /* First do the assignment, then return converted constant. */
7886 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7887 TREE_NO_WARNING (tem) = 1;
7888 TREE_USED (tem) = 1;
7889 return tem;
7892 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7893 constants (if x has signed type, the sign bit cannot be set
7894 in c). This folds extension into the BIT_AND_EXPR.
7895 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7896 very likely don't have maximal range for their precision and this
7897 transformation effectively doesn't preserve non-maximal ranges. */
7898 if (TREE_CODE (type) == INTEGER_TYPE
7899 && TREE_CODE (op0) == BIT_AND_EXPR
7900 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7902 tree and_expr = op0;
7903 tree and0 = TREE_OPERAND (and_expr, 0);
7904 tree and1 = TREE_OPERAND (and_expr, 1);
7905 int change = 0;
7907 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7908 || (TYPE_PRECISION (type)
7909 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7910 change = 1;
7911 else if (TYPE_PRECISION (TREE_TYPE (and1))
7912 <= HOST_BITS_PER_WIDE_INT
7913 && tree_fits_uhwi_p (and1))
7915 unsigned HOST_WIDE_INT cst;
7917 cst = tree_to_uhwi (and1);
7918 cst &= HOST_WIDE_INT_M1U
7919 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7920 change = (cst == 0);
7921 #ifdef LOAD_EXTEND_OP
7922 if (change
7923 && !flag_syntax_only
7924 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7925 == ZERO_EXTEND))
7927 tree uns = unsigned_type_for (TREE_TYPE (and0));
7928 and0 = fold_convert_loc (loc, uns, and0);
7929 and1 = fold_convert_loc (loc, uns, and1);
7931 #endif
7933 if (change)
7935 tem = force_fit_type (type, wi::to_widest (and1), 0,
7936 TREE_OVERFLOW (and1));
7937 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7938 fold_convert_loc (loc, type, and0), tem);
7942 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7943 when one of the new casts will fold away. Conservatively we assume
7944 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7945 if (POINTER_TYPE_P (type)
7946 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7947 && (!TYPE_RESTRICT (type) || TYPE_RESTRICT (TREE_TYPE (arg0)))
7948 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7949 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7950 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7952 tree arg00 = TREE_OPERAND (arg0, 0);
7953 tree arg01 = TREE_OPERAND (arg0, 1);
7955 return fold_build_pointer_plus_loc
7956 (loc, fold_convert_loc (loc, type, arg00), arg01);
7959 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7960 of the same precision, and X is an integer type not narrower than
7961 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7962 if (INTEGRAL_TYPE_P (type)
7963 && TREE_CODE (op0) == BIT_NOT_EXPR
7964 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7965 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7966 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7968 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7969 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7970 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7971 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7972 fold_convert_loc (loc, type, tem));
7975 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7976 type of X and Y (integer types only). */
7977 if (INTEGRAL_TYPE_P (type)
7978 && TREE_CODE (op0) == MULT_EXPR
7979 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7980 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7982 /* Be careful not to introduce new overflows. */
7983 tree mult_type;
7984 if (TYPE_OVERFLOW_WRAPS (type))
7985 mult_type = type;
7986 else
7987 mult_type = unsigned_type_for (type);
7989 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7991 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7992 fold_convert_loc (loc, mult_type,
7993 TREE_OPERAND (op0, 0)),
7994 fold_convert_loc (loc, mult_type,
7995 TREE_OPERAND (op0, 1)));
7996 return fold_convert_loc (loc, type, tem);
8000 tem = fold_convert_const (code, type, arg0);
8001 return tem ? tem : NULL_TREE;
8003 case ADDR_SPACE_CONVERT_EXPR:
8004 if (integer_zerop (arg0))
8005 return fold_convert_const (code, type, arg0);
8006 return NULL_TREE;
8008 case FIXED_CONVERT_EXPR:
8009 tem = fold_convert_const (code, type, arg0);
8010 return tem ? tem : NULL_TREE;
8012 case VIEW_CONVERT_EXPR:
8013 if (TREE_TYPE (op0) == type)
8014 return op0;
8015 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8016 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8017 type, TREE_OPERAND (op0, 0));
8018 if (TREE_CODE (op0) == MEM_REF)
8020 tem = fold_build2_loc (loc, MEM_REF, type,
8021 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8022 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
8023 return tem;
8026 /* For integral conversions with the same precision or pointer
8027 conversions use a NOP_EXPR instead. */
8028 if ((INTEGRAL_TYPE_P (type)
8029 || POINTER_TYPE_P (type))
8030 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8031 || POINTER_TYPE_P (TREE_TYPE (op0)))
8032 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8033 return fold_convert_loc (loc, type, op0);
8035 /* Strip inner integral conversions that do not change the precision. */
8036 if (CONVERT_EXPR_P (op0)
8037 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8038 || POINTER_TYPE_P (TREE_TYPE (op0)))
8039 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8040 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8041 && (TYPE_PRECISION (TREE_TYPE (op0))
8042 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8043 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8044 type, TREE_OPERAND (op0, 0));
8046 return fold_view_convert_expr (type, op0);
8048 case NEGATE_EXPR:
8049 tem = fold_negate_expr (loc, arg0);
8050 if (tem)
8051 return fold_convert_loc (loc, type, tem);
8052 return NULL_TREE;
8054 case ABS_EXPR:
8055 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8056 return fold_abs_const (arg0, type);
8057 else if (TREE_CODE (arg0) == NEGATE_EXPR)
8058 return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8059 /* Convert fabs((double)float) into (double)fabsf(float). */
8060 else if (TREE_CODE (arg0) == NOP_EXPR
8061 && TREE_CODE (type) == REAL_TYPE)
8063 tree targ0 = strip_float_extensions (arg0);
8064 if (targ0 != arg0)
8065 return fold_convert_loc (loc, type,
8066 fold_build1_loc (loc, ABS_EXPR,
8067 TREE_TYPE (targ0),
8068 targ0));
8070 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8071 else if (TREE_CODE (arg0) == ABS_EXPR)
8072 return arg0;
8073 else if (tree_expr_nonnegative_p (arg0))
8074 return arg0;
8076 /* Strip sign ops from argument. */
8077 if (TREE_CODE (type) == REAL_TYPE)
8079 tem = fold_strip_sign_ops (arg0);
8080 if (tem)
8081 return fold_build1_loc (loc, ABS_EXPR, type,
8082 fold_convert_loc (loc, type, tem));
8084 return NULL_TREE;
8086 case CONJ_EXPR:
8087 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8088 return fold_convert_loc (loc, type, arg0);
8089 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8091 tree itype = TREE_TYPE (type);
8092 tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8093 tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8094 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8095 negate_expr (ipart));
8097 if (TREE_CODE (arg0) == COMPLEX_CST)
8099 tree itype = TREE_TYPE (type);
8100 tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8101 tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8102 return build_complex (type, rpart, negate_expr (ipart));
8104 if (TREE_CODE (arg0) == CONJ_EXPR)
8105 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8106 return NULL_TREE;
8108 case BIT_NOT_EXPR:
8109 if (TREE_CODE (arg0) == INTEGER_CST)
8110 return fold_not_const (arg0, type);
8111 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8112 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8113 /* Convert ~ (-A) to A - 1. */
8114 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8115 return fold_build2_loc (loc, MINUS_EXPR, type,
8116 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8117 build_int_cst (type, 1));
8118 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8119 else if (INTEGRAL_TYPE_P (type)
8120 && ((TREE_CODE (arg0) == MINUS_EXPR
8121 && integer_onep (TREE_OPERAND (arg0, 1)))
8122 || (TREE_CODE (arg0) == PLUS_EXPR
8123 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8124 return fold_build1_loc (loc, NEGATE_EXPR, type,
8125 fold_convert_loc (loc, type,
8126 TREE_OPERAND (arg0, 0)));
8127 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8128 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8129 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8130 fold_convert_loc (loc, type,
8131 TREE_OPERAND (arg0, 0)))))
8132 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8133 fold_convert_loc (loc, type,
8134 TREE_OPERAND (arg0, 1)));
8135 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8136 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8137 fold_convert_loc (loc, type,
8138 TREE_OPERAND (arg0, 1)))))
8139 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8140 fold_convert_loc (loc, type,
8141 TREE_OPERAND (arg0, 0)), tem);
8142 /* Perform BIT_NOT_EXPR on each element individually. */
8143 else if (TREE_CODE (arg0) == VECTOR_CST)
8145 tree *elements;
8146 tree elem;
8147 unsigned count = VECTOR_CST_NELTS (arg0), i;
8149 elements = XALLOCAVEC (tree, count);
8150 for (i = 0; i < count; i++)
8152 elem = VECTOR_CST_ELT (arg0, i);
8153 elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8154 if (elem == NULL_TREE)
8155 break;
8156 elements[i] = elem;
8158 if (i == count)
8159 return build_vector (type, elements);
8161 else if (COMPARISON_CLASS_P (arg0)
8162 && (VECTOR_TYPE_P (type)
8163 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
8165 tree op_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
8166 enum tree_code subcode = invert_tree_comparison (TREE_CODE (arg0),
8167 HONOR_NANS (TYPE_MODE (op_type)));
8168 if (subcode != ERROR_MARK)
8169 return build2_loc (loc, subcode, type, TREE_OPERAND (arg0, 0),
8170 TREE_OPERAND (arg0, 1));
8174 return NULL_TREE;
8176 case TRUTH_NOT_EXPR:
8177 /* Note that the operand of this must be an int
8178 and its values must be 0 or 1.
8179 ("true" is a fixed value perhaps depending on the language,
8180 but we don't handle values other than 1 correctly yet.) */
8181 tem = fold_truth_not_expr (loc, arg0);
8182 if (!tem)
8183 return NULL_TREE;
8184 return fold_convert_loc (loc, type, tem);
8186 case REALPART_EXPR:
8187 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8188 return fold_convert_loc (loc, type, arg0);
8189 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8190 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8191 TREE_OPERAND (arg0, 1));
8192 if (TREE_CODE (arg0) == COMPLEX_CST)
8193 return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8194 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8196 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8197 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8198 fold_build1_loc (loc, REALPART_EXPR, itype,
8199 TREE_OPERAND (arg0, 0)),
8200 fold_build1_loc (loc, REALPART_EXPR, itype,
8201 TREE_OPERAND (arg0, 1)));
8202 return fold_convert_loc (loc, type, tem);
8204 if (TREE_CODE (arg0) == CONJ_EXPR)
8206 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8207 tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8208 TREE_OPERAND (arg0, 0));
8209 return fold_convert_loc (loc, type, tem);
8211 if (TREE_CODE (arg0) == CALL_EXPR)
8213 tree fn = get_callee_fndecl (arg0);
8214 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8215 switch (DECL_FUNCTION_CODE (fn))
8217 CASE_FLT_FN (BUILT_IN_CEXPI):
8218 fn = mathfn_built_in (type, BUILT_IN_COS);
8219 if (fn)
8220 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8221 break;
8223 default:
8224 break;
8227 return NULL_TREE;
8229 case IMAGPART_EXPR:
8230 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8231 return build_zero_cst (type);
8232 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8233 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8234 TREE_OPERAND (arg0, 0));
8235 if (TREE_CODE (arg0) == COMPLEX_CST)
8236 return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8237 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8239 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8240 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8241 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8242 TREE_OPERAND (arg0, 0)),
8243 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8244 TREE_OPERAND (arg0, 1)));
8245 return fold_convert_loc (loc, type, tem);
8247 if (TREE_CODE (arg0) == CONJ_EXPR)
8249 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8250 tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8251 return fold_convert_loc (loc, type, negate_expr (tem));
8253 if (TREE_CODE (arg0) == CALL_EXPR)
8255 tree fn = get_callee_fndecl (arg0);
8256 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8257 switch (DECL_FUNCTION_CODE (fn))
8259 CASE_FLT_FN (BUILT_IN_CEXPI):
8260 fn = mathfn_built_in (type, BUILT_IN_SIN);
8261 if (fn)
8262 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8263 break;
8265 default:
8266 break;
8269 return NULL_TREE;
8271 case INDIRECT_REF:
8272 /* Fold *&X to X if X is an lvalue. */
8273 if (TREE_CODE (op0) == ADDR_EXPR)
8275 tree op00 = TREE_OPERAND (op0, 0);
8276 if ((TREE_CODE (op00) == VAR_DECL
8277 || TREE_CODE (op00) == PARM_DECL
8278 || TREE_CODE (op00) == RESULT_DECL)
8279 && !TREE_READONLY (op00))
8280 return op00;
8282 return NULL_TREE;
8284 case VEC_UNPACK_LO_EXPR:
8285 case VEC_UNPACK_HI_EXPR:
8286 case VEC_UNPACK_FLOAT_LO_EXPR:
8287 case VEC_UNPACK_FLOAT_HI_EXPR:
8289 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8290 tree *elts;
8291 enum tree_code subcode;
8293 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
8294 if (TREE_CODE (arg0) != VECTOR_CST)
8295 return NULL_TREE;
8297 elts = XALLOCAVEC (tree, nelts * 2);
8298 if (!vec_cst_ctor_to_array (arg0, elts))
8299 return NULL_TREE;
8301 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
8302 || code == VEC_UNPACK_FLOAT_LO_EXPR))
8303 elts += nelts;
8305 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
8306 subcode = NOP_EXPR;
8307 else
8308 subcode = FLOAT_EXPR;
8310 for (i = 0; i < nelts; i++)
8312 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
8313 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
8314 return NULL_TREE;
8317 return build_vector (type, elts);
8320 case REDUC_MIN_EXPR:
8321 case REDUC_MAX_EXPR:
8322 case REDUC_PLUS_EXPR:
8324 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8325 tree *elts;
8326 enum tree_code subcode;
8328 if (TREE_CODE (op0) != VECTOR_CST)
8329 return NULL_TREE;
8331 elts = XALLOCAVEC (tree, nelts);
8332 if (!vec_cst_ctor_to_array (op0, elts))
8333 return NULL_TREE;
8335 switch (code)
8337 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
8338 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
8339 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
8340 default: gcc_unreachable ();
8343 for (i = 1; i < nelts; i++)
8345 elts[0] = const_binop (subcode, elts[0], elts[i]);
8346 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
8347 return NULL_TREE;
8348 elts[i] = build_zero_cst (TREE_TYPE (type));
8351 return build_vector (type, elts);
8354 default:
8355 return NULL_TREE;
8356 } /* switch (code) */
8360 /* If the operation was a conversion do _not_ mark a resulting constant
8361 with TREE_OVERFLOW if the original constant was not. These conversions
8362 have implementation defined behavior and retaining the TREE_OVERFLOW
8363 flag here would confuse later passes such as VRP. */
8364 tree
8365 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8366 tree type, tree op0)
8368 tree res = fold_unary_loc (loc, code, type, op0);
8369 if (res
8370 && TREE_CODE (res) == INTEGER_CST
8371 && TREE_CODE (op0) == INTEGER_CST
8372 && CONVERT_EXPR_CODE_P (code))
8373 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8375 return res;
8378 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8379 operands OP0 and OP1. LOC is the location of the resulting expression.
8380 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8381 Return the folded expression if folding is successful. Otherwise,
8382 return NULL_TREE. */
8383 static tree
8384 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8385 tree arg0, tree arg1, tree op0, tree op1)
8387 tree tem;
8389 /* We only do these simplifications if we are optimizing. */
8390 if (!optimize)
8391 return NULL_TREE;
8393 /* Check for things like (A || B) && (A || C). We can convert this
8394 to A || (B && C). Note that either operator can be any of the four
8395 truth and/or operations and the transformation will still be
8396 valid. Also note that we only care about order for the
8397 ANDIF and ORIF operators. If B contains side effects, this
8398 might change the truth-value of A. */
8399 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8400 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8401 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8402 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8403 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8404 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8406 tree a00 = TREE_OPERAND (arg0, 0);
8407 tree a01 = TREE_OPERAND (arg0, 1);
8408 tree a10 = TREE_OPERAND (arg1, 0);
8409 tree a11 = TREE_OPERAND (arg1, 1);
8410 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8411 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8412 && (code == TRUTH_AND_EXPR
8413 || code == TRUTH_OR_EXPR));
8415 if (operand_equal_p (a00, a10, 0))
8416 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8417 fold_build2_loc (loc, code, type, a01, a11));
8418 else if (commutative && operand_equal_p (a00, a11, 0))
8419 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8420 fold_build2_loc (loc, code, type, a01, a10));
8421 else if (commutative && operand_equal_p (a01, a10, 0))
8422 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8423 fold_build2_loc (loc, code, type, a00, a11));
8425 /* This case if tricky because we must either have commutative
8426 operators or else A10 must not have side-effects. */
8428 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8429 && operand_equal_p (a01, a11, 0))
8430 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8431 fold_build2_loc (loc, code, type, a00, a10),
8432 a01);
8435 /* See if we can build a range comparison. */
8436 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8437 return tem;
8439 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8440 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8442 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8443 if (tem)
8444 return fold_build2_loc (loc, code, type, tem, arg1);
8447 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8448 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8450 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8451 if (tem)
8452 return fold_build2_loc (loc, code, type, arg0, tem);
8455 /* Check for the possibility of merging component references. If our
8456 lhs is another similar operation, try to merge its rhs with our
8457 rhs. Then try to merge our lhs and rhs. */
8458 if (TREE_CODE (arg0) == code
8459 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8460 TREE_OPERAND (arg0, 1), arg1)))
8461 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8463 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8464 return tem;
8466 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8467 && (code == TRUTH_AND_EXPR
8468 || code == TRUTH_ANDIF_EXPR
8469 || code == TRUTH_OR_EXPR
8470 || code == TRUTH_ORIF_EXPR))
8472 enum tree_code ncode, icode;
8474 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8475 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8476 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8478 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8479 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8480 We don't want to pack more than two leafs to a non-IF AND/OR
8481 expression.
8482 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8483 equal to IF-CODE, then we don't want to add right-hand operand.
8484 If the inner right-hand side of left-hand operand has
8485 side-effects, or isn't simple, then we can't add to it,
8486 as otherwise we might destroy if-sequence. */
8487 if (TREE_CODE (arg0) == icode
8488 && simple_operand_p_2 (arg1)
8489 /* Needed for sequence points to handle trappings, and
8490 side-effects. */
8491 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8493 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8494 arg1);
8495 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8496 tem);
8498 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8499 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8500 else if (TREE_CODE (arg1) == icode
8501 && simple_operand_p_2 (arg0)
8502 /* Needed for sequence points to handle trappings, and
8503 side-effects. */
8504 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8506 tem = fold_build2_loc (loc, ncode, type,
8507 arg0, TREE_OPERAND (arg1, 0));
8508 return fold_build2_loc (loc, icode, type, tem,
8509 TREE_OPERAND (arg1, 1));
8511 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8512 into (A OR B).
8513 For sequence point consistancy, we need to check for trapping,
8514 and side-effects. */
8515 else if (code == icode && simple_operand_p_2 (arg0)
8516 && simple_operand_p_2 (arg1))
8517 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8520 return NULL_TREE;
8523 /* Fold a binary expression of code CODE and type TYPE with operands
8524 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8525 Return the folded expression if folding is successful. Otherwise,
8526 return NULL_TREE. */
8528 static tree
8529 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8531 enum tree_code compl_code;
8533 if (code == MIN_EXPR)
8534 compl_code = MAX_EXPR;
8535 else if (code == MAX_EXPR)
8536 compl_code = MIN_EXPR;
8537 else
8538 gcc_unreachable ();
8540 /* MIN (MAX (a, b), b) == b. */
8541 if (TREE_CODE (op0) == compl_code
8542 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8543 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8545 /* MIN (MAX (b, a), b) == b. */
8546 if (TREE_CODE (op0) == compl_code
8547 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8548 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8549 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8551 /* MIN (a, MAX (a, b)) == a. */
8552 if (TREE_CODE (op1) == compl_code
8553 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8554 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8555 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8557 /* MIN (a, MAX (b, a)) == a. */
8558 if (TREE_CODE (op1) == compl_code
8559 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8560 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8561 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8563 return NULL_TREE;
8566 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8567 by changing CODE to reduce the magnitude of constants involved in
8568 ARG0 of the comparison.
8569 Returns a canonicalized comparison tree if a simplification was
8570 possible, otherwise returns NULL_TREE.
8571 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8572 valid if signed overflow is undefined. */
8574 static tree
8575 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8576 tree arg0, tree arg1,
8577 bool *strict_overflow_p)
8579 enum tree_code code0 = TREE_CODE (arg0);
8580 tree t, cst0 = NULL_TREE;
8581 int sgn0;
8582 bool swap = false;
8584 /* Match A +- CST code arg1 and CST code arg1. We can change the
8585 first form only if overflow is undefined. */
8586 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8587 /* In principle pointers also have undefined overflow behavior,
8588 but that causes problems elsewhere. */
8589 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8590 && (code0 == MINUS_EXPR
8591 || code0 == PLUS_EXPR)
8592 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8593 || code0 == INTEGER_CST))
8594 return NULL_TREE;
8596 /* Identify the constant in arg0 and its sign. */
8597 if (code0 == INTEGER_CST)
8598 cst0 = arg0;
8599 else
8600 cst0 = TREE_OPERAND (arg0, 1);
8601 sgn0 = tree_int_cst_sgn (cst0);
8603 /* Overflowed constants and zero will cause problems. */
8604 if (integer_zerop (cst0)
8605 || TREE_OVERFLOW (cst0))
8606 return NULL_TREE;
8608 /* See if we can reduce the magnitude of the constant in
8609 arg0 by changing the comparison code. */
8610 if (code0 == INTEGER_CST)
8612 /* CST <= arg1 -> CST-1 < arg1. */
8613 if (code == LE_EXPR && sgn0 == 1)
8614 code = LT_EXPR;
8615 /* -CST < arg1 -> -CST-1 <= arg1. */
8616 else if (code == LT_EXPR && sgn0 == -1)
8617 code = LE_EXPR;
8618 /* CST > arg1 -> CST-1 >= arg1. */
8619 else if (code == GT_EXPR && sgn0 == 1)
8620 code = GE_EXPR;
8621 /* -CST >= arg1 -> -CST-1 > arg1. */
8622 else if (code == GE_EXPR && sgn0 == -1)
8623 code = GT_EXPR;
8624 else
8625 return NULL_TREE;
8626 /* arg1 code' CST' might be more canonical. */
8627 swap = true;
8629 else
8631 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8632 if (code == LT_EXPR
8633 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8634 code = LE_EXPR;
8635 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8636 else if (code == GT_EXPR
8637 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8638 code = GE_EXPR;
8639 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8640 else if (code == LE_EXPR
8641 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8642 code = LT_EXPR;
8643 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8644 else if (code == GE_EXPR
8645 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8646 code = GT_EXPR;
8647 else
8648 return NULL_TREE;
8649 *strict_overflow_p = true;
8652 /* Now build the constant reduced in magnitude. But not if that
8653 would produce one outside of its types range. */
8654 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8655 && ((sgn0 == 1
8656 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8657 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8658 || (sgn0 == -1
8659 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8660 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8661 /* We cannot swap the comparison here as that would cause us to
8662 endlessly recurse. */
8663 return NULL_TREE;
8665 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8666 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8667 if (code0 != INTEGER_CST)
8668 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8669 t = fold_convert (TREE_TYPE (arg1), t);
8671 /* If swapping might yield to a more canonical form, do so. */
8672 if (swap)
8673 return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8674 else
8675 return fold_build2_loc (loc, code, type, t, arg1);
8678 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8679 overflow further. Try to decrease the magnitude of constants involved
8680 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8681 and put sole constants at the second argument position.
8682 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8684 static tree
8685 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8686 tree arg0, tree arg1)
8688 tree t;
8689 bool strict_overflow_p;
8690 const char * const warnmsg = G_("assuming signed overflow does not occur "
8691 "when reducing constant in comparison");
8693 /* Try canonicalization by simplifying arg0. */
8694 strict_overflow_p = false;
8695 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8696 &strict_overflow_p);
8697 if (t)
8699 if (strict_overflow_p)
8700 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8701 return t;
8704 /* Try canonicalization by simplifying arg1 using the swapped
8705 comparison. */
8706 code = swap_tree_comparison (code);
8707 strict_overflow_p = false;
8708 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8709 &strict_overflow_p);
8710 if (t && strict_overflow_p)
8711 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8712 return t;
8715 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8716 space. This is used to avoid issuing overflow warnings for
8717 expressions like &p->x which can not wrap. */
8719 static bool
8720 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8722 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8723 return true;
8725 if (bitpos < 0)
8726 return true;
8728 wide_int wi_offset;
8729 int precision = TYPE_PRECISION (TREE_TYPE (base));
8730 if (offset == NULL_TREE)
8731 wi_offset = wi::zero (precision);
8732 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8733 return true;
8734 else
8735 wi_offset = offset;
8737 bool overflow;
8738 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8739 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8740 if (overflow)
8741 return true;
8743 if (!wi::fits_uhwi_p (total))
8744 return true;
8746 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8747 if (size <= 0)
8748 return true;
8750 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8751 array. */
8752 if (TREE_CODE (base) == ADDR_EXPR)
8754 HOST_WIDE_INT base_size;
8756 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8757 if (base_size > 0 && size < base_size)
8758 size = base_size;
8761 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8764 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
8765 kind INTEGER_CST. This makes sure to properly sign-extend the
8766 constant. */
8768 static HOST_WIDE_INT
8769 size_low_cst (const_tree t)
8771 HOST_WIDE_INT w = TREE_INT_CST_ELT (t, 0);
8772 int prec = TYPE_PRECISION (TREE_TYPE (t));
8773 if (prec < HOST_BITS_PER_WIDE_INT)
8774 return sext_hwi (w, prec);
8775 return w;
8778 /* Subroutine of fold_binary. This routine performs all of the
8779 transformations that are common to the equality/inequality
8780 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8781 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8782 fold_binary should call fold_binary. Fold a comparison with
8783 tree code CODE and type TYPE with operands OP0 and OP1. Return
8784 the folded comparison or NULL_TREE. */
8786 static tree
8787 fold_comparison (location_t loc, enum tree_code code, tree type,
8788 tree op0, tree op1)
8790 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8791 tree arg0, arg1, tem;
8793 arg0 = op0;
8794 arg1 = op1;
8796 STRIP_SIGN_NOPS (arg0);
8797 STRIP_SIGN_NOPS (arg1);
8799 tem = fold_relational_const (code, type, arg0, arg1);
8800 if (tem != NULL_TREE)
8801 return tem;
8803 /* If one arg is a real or integer constant, put it last. */
8804 if (tree_swap_operands_p (arg0, arg1, true))
8805 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8807 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8808 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8809 && (equality_code || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8810 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8811 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8812 && TREE_CODE (arg1) == INTEGER_CST
8813 && !TREE_OVERFLOW (arg1))
8815 const enum tree_code
8816 reverse_op = TREE_CODE (arg0) == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
8817 tree const1 = TREE_OPERAND (arg0, 1);
8818 tree const2 = fold_convert_loc (loc, TREE_TYPE (const1), arg1);
8819 tree variable = TREE_OPERAND (arg0, 0);
8820 tree new_const = int_const_binop (reverse_op, const2, const1);
8822 /* If the constant operation overflowed this can be
8823 simplified as a comparison against INT_MAX/INT_MIN. */
8824 if (TREE_OVERFLOW (new_const))
8826 int const1_sgn = tree_int_cst_sgn (const1);
8827 enum tree_code code2 = code;
8829 /* Get the sign of the constant on the lhs if the
8830 operation were VARIABLE + CONST1. */
8831 if (TREE_CODE (arg0) == MINUS_EXPR)
8832 const1_sgn = -const1_sgn;
8834 /* The sign of the constant determines if we overflowed
8835 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8836 Canonicalize to the INT_MIN overflow by swapping the comparison
8837 if necessary. */
8838 if (const1_sgn == -1)
8839 code2 = swap_tree_comparison (code);
8841 /* We now can look at the canonicalized case
8842 VARIABLE + 1 CODE2 INT_MIN
8843 and decide on the result. */
8844 switch (code2)
8846 case EQ_EXPR:
8847 case LT_EXPR:
8848 case LE_EXPR:
8849 return
8850 omit_one_operand_loc (loc, type, boolean_false_node, variable);
8852 case NE_EXPR:
8853 case GE_EXPR:
8854 case GT_EXPR:
8855 return
8856 omit_one_operand_loc (loc, type, boolean_true_node, variable);
8858 default:
8859 gcc_unreachable ();
8862 else
8864 if (!equality_code)
8865 fold_overflow_warning ("assuming signed overflow does not occur "
8866 "when changing X +- C1 cmp C2 to "
8867 "X cmp C2 -+ C1",
8868 WARN_STRICT_OVERFLOW_COMPARISON);
8869 return fold_build2_loc (loc, code, type, variable, new_const);
8873 /* Transform comparisons of the form X - Y CMP 0 to X CMP Y. */
8874 if (TREE_CODE (arg0) == MINUS_EXPR
8875 && equality_code
8876 && integer_zerop (arg1))
8878 /* ??? The transformation is valid for the other operators if overflow
8879 is undefined for the type, but performing it here badly interacts
8880 with the transformation in fold_cond_expr_with_comparison which
8881 attempts to synthetize ABS_EXPR. */
8882 if (!equality_code)
8883 fold_overflow_warning ("assuming signed overflow does not occur "
8884 "when changing X - Y cmp 0 to X cmp Y",
8885 WARN_STRICT_OVERFLOW_COMPARISON);
8886 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
8887 TREE_OPERAND (arg0, 1));
8890 /* For comparisons of pointers we can decompose it to a compile time
8891 comparison of the base objects and the offsets into the object.
8892 This requires at least one operand being an ADDR_EXPR or a
8893 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8894 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8895 && (TREE_CODE (arg0) == ADDR_EXPR
8896 || TREE_CODE (arg1) == ADDR_EXPR
8897 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8898 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8900 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8901 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8902 enum machine_mode mode;
8903 int volatilep, reversep, unsignedp;
8904 bool indirect_base0 = false, indirect_base1 = false;
8906 /* Get base and offset for the access. Strip ADDR_EXPR for
8907 get_inner_reference, but put it back by stripping INDIRECT_REF
8908 off the base object if possible. indirect_baseN will be true
8909 if baseN is not an address but refers to the object itself. */
8910 base0 = arg0;
8911 if (TREE_CODE (arg0) == ADDR_EXPR)
8913 base0
8914 = get_inner_reference (TREE_OPERAND (arg0, 0),
8915 &bitsize, &bitpos0, &offset0, &mode,
8916 &unsignedp, &reversep, &volatilep, false);
8917 if (TREE_CODE (base0) == INDIRECT_REF)
8918 base0 = TREE_OPERAND (base0, 0);
8919 else
8920 indirect_base0 = true;
8922 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8924 base0 = TREE_OPERAND (arg0, 0);
8925 STRIP_SIGN_NOPS (base0);
8926 if (TREE_CODE (base0) == ADDR_EXPR)
8928 base0 = TREE_OPERAND (base0, 0);
8929 indirect_base0 = true;
8931 offset0 = TREE_OPERAND (arg0, 1);
8932 if (tree_fits_shwi_p (offset0))
8934 HOST_WIDE_INT off = size_low_cst (offset0);
8935 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8936 * BITS_PER_UNIT)
8937 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8939 bitpos0 = off * BITS_PER_UNIT;
8940 offset0 = NULL_TREE;
8945 base1 = arg1;
8946 if (TREE_CODE (arg1) == ADDR_EXPR)
8948 base1
8949 = get_inner_reference (TREE_OPERAND (arg1, 0),
8950 &bitsize, &bitpos1, &offset1, &mode,
8951 &unsignedp, &reversep, &volatilep, false);
8952 if (TREE_CODE (base1) == INDIRECT_REF)
8953 base1 = TREE_OPERAND (base1, 0);
8954 else
8955 indirect_base1 = true;
8957 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8959 base1 = TREE_OPERAND (arg1, 0);
8960 STRIP_SIGN_NOPS (base1);
8961 if (TREE_CODE (base1) == ADDR_EXPR)
8963 base1 = TREE_OPERAND (base1, 0);
8964 indirect_base1 = true;
8966 offset1 = TREE_OPERAND (arg1, 1);
8967 if (tree_fits_shwi_p (offset1))
8969 HOST_WIDE_INT off = size_low_cst (offset1);
8970 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8971 * BITS_PER_UNIT)
8972 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8974 bitpos1 = off * BITS_PER_UNIT;
8975 offset1 = NULL_TREE;
8980 /* A local variable can never be pointed to by
8981 the default SSA name of an incoming parameter. */
8982 if ((TREE_CODE (arg0) == ADDR_EXPR
8983 && indirect_base0
8984 && TREE_CODE (base0) == VAR_DECL
8985 && auto_var_in_fn_p (base0, current_function_decl)
8986 && !indirect_base1
8987 && TREE_CODE (base1) == SSA_NAME
8988 && SSA_NAME_IS_DEFAULT_DEF (base1)
8989 && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL)
8990 || (TREE_CODE (arg1) == ADDR_EXPR
8991 && indirect_base1
8992 && TREE_CODE (base1) == VAR_DECL
8993 && auto_var_in_fn_p (base1, current_function_decl)
8994 && !indirect_base0
8995 && TREE_CODE (base0) == SSA_NAME
8996 && SSA_NAME_IS_DEFAULT_DEF (base0)
8997 && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL))
8999 if (code == NE_EXPR)
9000 return constant_boolean_node (1, type);
9001 else if (code == EQ_EXPR)
9002 return constant_boolean_node (0, type);
9004 /* If we have equivalent bases we might be able to simplify. */
9005 else if (indirect_base0 == indirect_base1
9006 && operand_equal_p (base0, base1, 0))
9008 /* We can fold this expression to a constant if the non-constant
9009 offset parts are equal. */
9010 if ((offset0 == offset1
9011 || (offset0 && offset1
9012 && operand_equal_p (offset0, offset1, 0)))
9013 && (code == EQ_EXPR
9014 || code == NE_EXPR
9015 || (indirect_base0 && DECL_P (base0))
9016 || POINTER_TYPE_OVERFLOW_UNDEFINED))
9019 if (!equality_code
9020 && bitpos0 != bitpos1
9021 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9022 || pointer_may_wrap_p (base1, offset1, bitpos1)))
9023 fold_overflow_warning (("assuming pointer wraparound does not "
9024 "occur when comparing P +- C1 with "
9025 "P +- C2"),
9026 WARN_STRICT_OVERFLOW_CONDITIONAL);
9028 switch (code)
9030 case EQ_EXPR:
9031 return constant_boolean_node (bitpos0 == bitpos1, type);
9032 case NE_EXPR:
9033 return constant_boolean_node (bitpos0 != bitpos1, type);
9034 case LT_EXPR:
9035 return constant_boolean_node (bitpos0 < bitpos1, type);
9036 case LE_EXPR:
9037 return constant_boolean_node (bitpos0 <= bitpos1, type);
9038 case GE_EXPR:
9039 return constant_boolean_node (bitpos0 >= bitpos1, type);
9040 case GT_EXPR:
9041 return constant_boolean_node (bitpos0 > bitpos1, type);
9042 default:;
9045 /* We can simplify the comparison to a comparison of the variable
9046 offset parts if the constant offset parts are equal.
9047 Be careful to use signed sizetype here because otherwise we
9048 mess with array offsets in the wrong way. This is possible
9049 because pointer arithmetic is restricted to retain within an
9050 object and overflow on pointer differences is undefined as of
9051 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
9052 else if (bitpos0 == bitpos1
9053 && (equality_code
9054 || (indirect_base0 && DECL_P (base0))
9055 || POINTER_TYPE_OVERFLOW_UNDEFINED))
9057 /* By converting to signed sizetype we cover middle-end pointer
9058 arithmetic which operates on unsigned pointer types of size
9059 type size and ARRAY_REF offsets which are properly sign or
9060 zero extended from their type in case it is narrower than
9061 sizetype. */
9062 if (offset0 == NULL_TREE)
9063 offset0 = build_int_cst (ssizetype, 0);
9064 else
9065 offset0 = fold_convert_loc (loc, ssizetype, offset0);
9066 if (offset1 == NULL_TREE)
9067 offset1 = build_int_cst (ssizetype, 0);
9068 else
9069 offset1 = fold_convert_loc (loc, ssizetype, offset1);
9071 if (!equality_code
9072 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9073 || pointer_may_wrap_p (base1, offset1, bitpos1)))
9074 fold_overflow_warning (("assuming pointer wraparound does not "
9075 "occur when comparing P +- C1 with "
9076 "P +- C2"),
9077 WARN_STRICT_OVERFLOW_COMPARISON);
9079 return fold_build2_loc (loc, code, type, offset0, offset1);
9082 /* For non-equal bases we can simplify if they are addresses
9083 of local binding decls or constants. */
9084 else if (indirect_base0 && indirect_base1
9085 /* We know that !operand_equal_p (base0, base1, 0)
9086 because the if condition was false. But make
9087 sure two decls are not the same. */
9088 && base0 != base1
9089 && TREE_CODE (arg0) == ADDR_EXPR
9090 && TREE_CODE (arg1) == ADDR_EXPR
9091 && (((TREE_CODE (base0) == VAR_DECL
9092 || TREE_CODE (base0) == PARM_DECL)
9093 && (targetm.binds_local_p (base0)
9094 || CONSTANT_CLASS_P (base1)))
9095 || CONSTANT_CLASS_P (base0))
9096 && (((TREE_CODE (base1) == VAR_DECL
9097 || TREE_CODE (base1) == PARM_DECL)
9098 && (targetm.binds_local_p (base1)
9099 || CONSTANT_CLASS_P (base0)))
9100 || CONSTANT_CLASS_P (base1)))
9102 if (code == EQ_EXPR)
9103 return omit_two_operands_loc (loc, type, boolean_false_node,
9104 arg0, arg1);
9105 else if (code == NE_EXPR)
9106 return omit_two_operands_loc (loc, type, boolean_true_node,
9107 arg0, arg1);
9109 /* For equal offsets we can simplify to a comparison of the
9110 base addresses. */
9111 else if (bitpos0 == bitpos1
9112 && (indirect_base0
9113 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9114 && (indirect_base1
9115 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9116 && ((offset0 == offset1)
9117 || (offset0 && offset1
9118 && operand_equal_p (offset0, offset1, 0))))
9120 if (indirect_base0)
9121 base0 = build_fold_addr_expr_loc (loc, base0);
9122 if (indirect_base1)
9123 base1 = build_fold_addr_expr_loc (loc, base1);
9124 return fold_build2_loc (loc, code, type, base0, base1);
9128 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9129 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
9130 the resulting offset is smaller in absolute value than the
9131 original one and has the same sign. */
9132 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9133 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9134 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9135 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9136 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9137 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9138 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9140 tree const1 = TREE_OPERAND (arg0, 1);
9141 tree const2 = TREE_OPERAND (arg1, 1);
9142 tree variable1 = TREE_OPERAND (arg0, 0);
9143 tree variable2 = TREE_OPERAND (arg1, 0);
9144 tree cst;
9145 const char * const warnmsg = G_("assuming signed overflow does not "
9146 "occur when combining constants around "
9147 "a comparison");
9149 /* Put the constant on the side where it doesn't overflow and is
9150 of lower absolute value and of same sign than before. */
9151 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9152 ? MINUS_EXPR : PLUS_EXPR,
9153 const2, const1);
9154 if (!TREE_OVERFLOW (cst)
9155 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
9156 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
9158 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9159 return fold_build2_loc (loc, code, type,
9160 variable1,
9161 fold_build2_loc (loc, TREE_CODE (arg1),
9162 TREE_TYPE (arg1),
9163 variable2, cst));
9166 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9167 ? MINUS_EXPR : PLUS_EXPR,
9168 const1, const2);
9169 if (!TREE_OVERFLOW (cst)
9170 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
9171 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
9173 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9174 return fold_build2_loc (loc, code, type,
9175 fold_build2_loc (loc, TREE_CODE (arg0),
9176 TREE_TYPE (arg0),
9177 variable1, cst),
9178 variable2);
9182 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9183 signed arithmetic case. That form is created by the compiler
9184 often enough for folding it to be of value. One example is in
9185 computing loop trip counts after Operator Strength Reduction. */
9186 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9187 && TREE_CODE (arg0) == MULT_EXPR
9188 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9189 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9190 && integer_zerop (arg1))
9192 tree const1 = TREE_OPERAND (arg0, 1);
9193 tree const2 = arg1; /* zero */
9194 tree variable1 = TREE_OPERAND (arg0, 0);
9195 enum tree_code cmp_code = code;
9197 /* Handle unfolded multiplication by zero. */
9198 if (integer_zerop (const1))
9199 return fold_build2_loc (loc, cmp_code, type, const1, const2);
9201 fold_overflow_warning (("assuming signed overflow does not occur when "
9202 "eliminating multiplication in comparison "
9203 "with zero"),
9204 WARN_STRICT_OVERFLOW_COMPARISON);
9206 /* If const1 is negative we swap the sense of the comparison. */
9207 if (tree_int_cst_sgn (const1) < 0)
9208 cmp_code = swap_tree_comparison (cmp_code);
9210 return fold_build2_loc (loc, cmp_code, type, variable1, const2);
9213 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
9214 if (tem)
9215 return tem;
9217 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9219 tree targ0 = strip_float_extensions (arg0);
9220 tree targ1 = strip_float_extensions (arg1);
9221 tree newtype = TREE_TYPE (targ0);
9223 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9224 newtype = TREE_TYPE (targ1);
9226 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
9227 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9228 return fold_build2_loc (loc, code, type,
9229 fold_convert_loc (loc, newtype, targ0),
9230 fold_convert_loc (loc, newtype, targ1));
9232 /* (-a) CMP (-b) -> b CMP a */
9233 if (TREE_CODE (arg0) == NEGATE_EXPR
9234 && TREE_CODE (arg1) == NEGATE_EXPR)
9235 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
9236 TREE_OPERAND (arg0, 0));
9238 if (TREE_CODE (arg1) == REAL_CST)
9240 REAL_VALUE_TYPE cst;
9241 cst = TREE_REAL_CST (arg1);
9243 /* (-a) CMP CST -> a swap(CMP) (-CST) */
9244 if (TREE_CODE (arg0) == NEGATE_EXPR)
9245 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9246 TREE_OPERAND (arg0, 0),
9247 build_real (TREE_TYPE (arg1),
9248 real_value_negate (&cst)));
9250 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
9251 /* a CMP (-0) -> a CMP 0 */
9252 if (REAL_VALUE_MINUS_ZERO (cst))
9253 return fold_build2_loc (loc, code, type, arg0,
9254 build_real (TREE_TYPE (arg1), dconst0));
9256 /* x != NaN is always true, other ops are always false. */
9257 if (REAL_VALUE_ISNAN (cst)
9258 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9260 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9261 return omit_one_operand_loc (loc, type, tem, arg0);
9264 /* Fold comparisons against infinity. */
9265 if (REAL_VALUE_ISINF (cst)
9266 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9268 tem = fold_inf_compare (loc, code, type, arg0, arg1);
9269 if (tem != NULL_TREE)
9270 return tem;
9274 /* If this is a comparison of a real constant with a PLUS_EXPR
9275 or a MINUS_EXPR of a real constant, we can convert it into a
9276 comparison with a revised real constant as long as no overflow
9277 occurs when unsafe_math_optimizations are enabled. */
9278 if (flag_unsafe_math_optimizations
9279 && TREE_CODE (arg1) == REAL_CST
9280 && (TREE_CODE (arg0) == PLUS_EXPR
9281 || TREE_CODE (arg0) == MINUS_EXPR)
9282 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9283 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9284 ? MINUS_EXPR : PLUS_EXPR,
9285 arg1, TREE_OPERAND (arg0, 1)))
9286 && !TREE_OVERFLOW (tem))
9287 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9289 /* Likewise, we can simplify a comparison of a real constant with
9290 a MINUS_EXPR whose first operand is also a real constant, i.e.
9291 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
9292 floating-point types only if -fassociative-math is set. */
9293 if (flag_associative_math
9294 && TREE_CODE (arg1) == REAL_CST
9295 && TREE_CODE (arg0) == MINUS_EXPR
9296 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9297 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9298 arg1))
9299 && !TREE_OVERFLOW (tem))
9300 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9301 TREE_OPERAND (arg0, 1), tem);
9303 /* Fold comparisons against built-in math functions. */
9304 if (TREE_CODE (arg1) == REAL_CST
9305 && flag_unsafe_math_optimizations
9306 && ! flag_errno_math)
9308 enum built_in_function fcode = builtin_mathfn_code (arg0);
9310 if (fcode != END_BUILTINS)
9312 tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9313 if (tem != NULL_TREE)
9314 return tem;
9319 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9320 && CONVERT_EXPR_P (arg0))
9322 /* If we are widening one operand of an integer comparison,
9323 see if the other operand is similarly being widened. Perhaps we
9324 can do the comparison in the narrower type. */
9325 tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9326 if (tem)
9327 return tem;
9329 /* Or if we are changing signedness. */
9330 tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9331 if (tem)
9332 return tem;
9335 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9336 constant, we can simplify it. */
9337 if (TREE_CODE (arg1) == INTEGER_CST
9338 && (TREE_CODE (arg0) == MIN_EXPR
9339 || TREE_CODE (arg0) == MAX_EXPR)
9340 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9342 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9343 if (tem)
9344 return tem;
9347 /* Simplify comparison of something with itself. (For IEEE
9348 floating-point, we can only do some of these simplifications.) */
9349 if (operand_equal_p (arg0, arg1, 0))
9351 switch (code)
9353 case EQ_EXPR:
9354 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9355 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9356 return constant_boolean_node (1, type);
9357 break;
9359 case GE_EXPR:
9360 case LE_EXPR:
9361 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9362 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9363 return constant_boolean_node (1, type);
9364 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9366 case NE_EXPR:
9367 /* For NE, we can only do this simplification if integer
9368 or we don't honor IEEE floating point NaNs. */
9369 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9370 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9371 break;
9372 /* ... fall through ... */
9373 case GT_EXPR:
9374 case LT_EXPR:
9375 return constant_boolean_node (0, type);
9376 default:
9377 gcc_unreachable ();
9381 /* If we are comparing an expression that just has comparisons
9382 of two integer values, arithmetic expressions of those comparisons,
9383 and constants, we can simplify it. There are only three cases
9384 to check: the two values can either be equal, the first can be
9385 greater, or the second can be greater. Fold the expression for
9386 those three values. Since each value must be 0 or 1, we have
9387 eight possibilities, each of which corresponds to the constant 0
9388 or 1 or one of the six possible comparisons.
9390 This handles common cases like (a > b) == 0 but also handles
9391 expressions like ((x > y) - (y > x)) > 0, which supposedly
9392 occur in macroized code. */
9394 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9396 tree cval1 = 0, cval2 = 0;
9397 int save_p = 0;
9399 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9400 /* Don't handle degenerate cases here; they should already
9401 have been handled anyway. */
9402 && cval1 != 0 && cval2 != 0
9403 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9404 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9405 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9406 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9407 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9408 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9409 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9411 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9412 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9414 /* We can't just pass T to eval_subst in case cval1 or cval2
9415 was the same as ARG1. */
9417 tree high_result
9418 = fold_build2_loc (loc, code, type,
9419 eval_subst (loc, arg0, cval1, maxval,
9420 cval2, minval),
9421 arg1);
9422 tree equal_result
9423 = fold_build2_loc (loc, code, type,
9424 eval_subst (loc, arg0, cval1, maxval,
9425 cval2, maxval),
9426 arg1);
9427 tree low_result
9428 = fold_build2_loc (loc, code, type,
9429 eval_subst (loc, arg0, cval1, minval,
9430 cval2, maxval),
9431 arg1);
9433 /* All three of these results should be 0 or 1. Confirm they are.
9434 Then use those values to select the proper code to use. */
9436 if (TREE_CODE (high_result) == INTEGER_CST
9437 && TREE_CODE (equal_result) == INTEGER_CST
9438 && TREE_CODE (low_result) == INTEGER_CST)
9440 /* Make a 3-bit mask with the high-order bit being the
9441 value for `>', the next for '=', and the low for '<'. */
9442 switch ((integer_onep (high_result) * 4)
9443 + (integer_onep (equal_result) * 2)
9444 + integer_onep (low_result))
9446 case 0:
9447 /* Always false. */
9448 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9449 case 1:
9450 code = LT_EXPR;
9451 break;
9452 case 2:
9453 code = EQ_EXPR;
9454 break;
9455 case 3:
9456 code = LE_EXPR;
9457 break;
9458 case 4:
9459 code = GT_EXPR;
9460 break;
9461 case 5:
9462 code = NE_EXPR;
9463 break;
9464 case 6:
9465 code = GE_EXPR;
9466 break;
9467 case 7:
9468 /* Always true. */
9469 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9472 if (save_p)
9474 tem = save_expr (build2 (code, type, cval1, cval2));
9475 SET_EXPR_LOCATION (tem, loc);
9476 return tem;
9478 return fold_build2_loc (loc, code, type, cval1, cval2);
9483 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9484 into a single range test. */
9485 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9486 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9487 && TREE_CODE (arg1) == INTEGER_CST
9488 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9489 && !integer_zerop (TREE_OPERAND (arg0, 1))
9490 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9491 && !TREE_OVERFLOW (arg1))
9493 tem = fold_div_compare (loc, code, type, arg0, arg1);
9494 if (tem != NULL_TREE)
9495 return tem;
9498 /* Fold ~X op ~Y as Y op X. */
9499 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9500 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9502 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9503 return fold_build2_loc (loc, code, type,
9504 fold_convert_loc (loc, cmp_type,
9505 TREE_OPERAND (arg1, 0)),
9506 TREE_OPERAND (arg0, 0));
9509 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9510 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9511 && (TREE_CODE (arg1) == INTEGER_CST || TREE_CODE (arg1) == VECTOR_CST))
9513 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9514 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9515 TREE_OPERAND (arg0, 0),
9516 fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9517 fold_convert_loc (loc, cmp_type, arg1)));
9520 return NULL_TREE;
9524 /* Subroutine of fold_binary. Optimize complex multiplications of the
9525 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9526 argument EXPR represents the expression "z" of type TYPE. */
9528 static tree
9529 fold_mult_zconjz (location_t loc, tree type, tree expr)
9531 tree itype = TREE_TYPE (type);
9532 tree rpart, ipart, tem;
9534 if (TREE_CODE (expr) == COMPLEX_EXPR)
9536 rpart = TREE_OPERAND (expr, 0);
9537 ipart = TREE_OPERAND (expr, 1);
9539 else if (TREE_CODE (expr) == COMPLEX_CST)
9541 rpart = TREE_REALPART (expr);
9542 ipart = TREE_IMAGPART (expr);
9544 else
9546 expr = save_expr (expr);
9547 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9548 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9551 rpart = save_expr (rpart);
9552 ipart = save_expr (ipart);
9553 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9554 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9555 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9556 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9557 build_zero_cst (itype));
9561 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9562 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9563 guarantees that P and N have the same least significant log2(M) bits.
9564 N is not otherwise constrained. In particular, N is not normalized to
9565 0 <= N < M as is common. In general, the precise value of P is unknown.
9566 M is chosen as large as possible such that constant N can be determined.
9568 Returns M and sets *RESIDUE to N.
9570 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9571 account. This is not always possible due to PR 35705.
9574 static unsigned HOST_WIDE_INT
9575 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9576 bool allow_func_align)
9578 enum tree_code code;
9580 *residue = 0;
9582 code = TREE_CODE (expr);
9583 if (code == ADDR_EXPR)
9585 unsigned int bitalign;
9586 get_object_alignment_1 (TREE_OPERAND (expr, 0), &bitalign, residue);
9587 *residue /= BITS_PER_UNIT;
9588 return bitalign / BITS_PER_UNIT;
9590 else if (code == POINTER_PLUS_EXPR)
9592 tree op0, op1;
9593 unsigned HOST_WIDE_INT modulus;
9594 enum tree_code inner_code;
9596 op0 = TREE_OPERAND (expr, 0);
9597 STRIP_NOPS (op0);
9598 modulus = get_pointer_modulus_and_residue (op0, residue,
9599 allow_func_align);
9601 op1 = TREE_OPERAND (expr, 1);
9602 STRIP_NOPS (op1);
9603 inner_code = TREE_CODE (op1);
9604 if (inner_code == INTEGER_CST)
9606 *residue += TREE_INT_CST_LOW (op1);
9607 return modulus;
9609 else if (inner_code == MULT_EXPR)
9611 op1 = TREE_OPERAND (op1, 1);
9612 if (TREE_CODE (op1) == INTEGER_CST)
9614 unsigned HOST_WIDE_INT align;
9616 /* Compute the greatest power-of-2 divisor of op1. */
9617 align = TREE_INT_CST_LOW (op1);
9618 align &= -align;
9620 /* If align is non-zero and less than *modulus, replace
9621 *modulus with align., If align is 0, then either op1 is 0
9622 or the greatest power-of-2 divisor of op1 doesn't fit in an
9623 unsigned HOST_WIDE_INT. In either case, no additional
9624 constraint is imposed. */
9625 if (align)
9626 modulus = MIN (modulus, align);
9628 return modulus;
9633 /* If we get here, we were unable to determine anything useful about the
9634 expression. */
9635 return 1;
9638 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
9639 CONSTRUCTOR ARG into array ELTS and return true if successful. */
9641 static bool
9642 vec_cst_ctor_to_array (tree arg, tree *elts)
9644 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
9646 if (TREE_CODE (arg) == VECTOR_CST)
9648 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
9649 elts[i] = VECTOR_CST_ELT (arg, i);
9651 else if (TREE_CODE (arg) == CONSTRUCTOR)
9653 constructor_elt *elt;
9655 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
9656 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
9657 return false;
9658 else
9659 elts[i] = elt->value;
9661 else
9662 return false;
9663 for (; i < nelts; i++)
9664 elts[i]
9665 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
9666 return true;
9669 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
9670 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
9671 NULL_TREE otherwise. */
9673 static tree
9674 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
9676 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
9677 tree *elts;
9678 bool need_ctor = false;
9680 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
9681 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
9682 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
9683 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
9684 return NULL_TREE;
9686 elts = XALLOCAVEC (tree, nelts * 3);
9687 if (!vec_cst_ctor_to_array (arg0, elts)
9688 || !vec_cst_ctor_to_array (arg1, elts + nelts))
9689 return NULL_TREE;
9691 for (i = 0; i < nelts; i++)
9693 if (!CONSTANT_CLASS_P (elts[sel[i]]))
9694 need_ctor = true;
9695 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
9698 if (need_ctor)
9700 vec<constructor_elt, va_gc> *v;
9701 vec_alloc (v, nelts);
9702 for (i = 0; i < nelts; i++)
9703 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
9704 return build_constructor (type, v);
9706 else
9707 return build_vector (type, &elts[2 * nelts]);
9710 /* Try to fold a pointer difference of type TYPE two address expressions of
9711 array references AREF0 and AREF1 using location LOC. Return a
9712 simplified expression for the difference or NULL_TREE. */
9714 static tree
9715 fold_addr_of_array_ref_difference (location_t loc, tree type,
9716 tree aref0, tree aref1)
9718 tree base0 = TREE_OPERAND (aref0, 0);
9719 tree base1 = TREE_OPERAND (aref1, 0);
9720 tree base_offset = build_int_cst (type, 0);
9722 /* If the bases are array references as well, recurse. If the bases
9723 are pointer indirections compute the difference of the pointers.
9724 If the bases are equal, we are set. */
9725 if ((TREE_CODE (base0) == ARRAY_REF
9726 && TREE_CODE (base1) == ARRAY_REF
9727 && (base_offset
9728 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
9729 || (INDIRECT_REF_P (base0)
9730 && INDIRECT_REF_P (base1)
9731 && (base_offset = fold_binary_loc (loc, MINUS_EXPR, type,
9732 TREE_OPERAND (base0, 0),
9733 TREE_OPERAND (base1, 0))))
9734 || operand_equal_p (base0, base1, 0))
9736 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9737 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9738 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9739 tree diff = build2 (MINUS_EXPR, type, op0, op1);
9740 return fold_build2_loc (loc, PLUS_EXPR, type,
9741 base_offset,
9742 fold_build2_loc (loc, MULT_EXPR, type,
9743 diff, esz));
9745 return NULL_TREE;
9748 /* If the real or vector real constant CST of type TYPE has an exact
9749 inverse, return it, else return NULL. */
9751 static tree
9752 exact_inverse (tree type, tree cst)
9754 REAL_VALUE_TYPE r;
9755 tree unit_type, *elts;
9756 enum machine_mode mode;
9757 unsigned vec_nelts, i;
9759 switch (TREE_CODE (cst))
9761 case REAL_CST:
9762 r = TREE_REAL_CST (cst);
9764 if (exact_real_inverse (TYPE_MODE (type), &r))
9765 return build_real (type, r);
9767 return NULL_TREE;
9769 case VECTOR_CST:
9770 vec_nelts = VECTOR_CST_NELTS (cst);
9771 elts = XALLOCAVEC (tree, vec_nelts);
9772 unit_type = TREE_TYPE (type);
9773 mode = TYPE_MODE (unit_type);
9775 for (i = 0; i < vec_nelts; i++)
9777 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9778 if (!exact_real_inverse (mode, &r))
9779 return NULL_TREE;
9780 elts[i] = build_real (unit_type, r);
9783 return build_vector (type, elts);
9785 default:
9786 return NULL_TREE;
9790 /* Mask out the tz least significant bits of X of type TYPE where
9791 tz is the number of trailing zeroes in Y. */
9792 static wide_int
9793 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9795 int tz = wi::ctz (y);
9796 if (tz > 0)
9797 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9798 return x;
9801 /* Return true when T is an address and is known to be nonzero.
9802 For floating point we further ensure that T is not denormal.
9803 Similar logic is present in nonzero_address in rtlanal.h.
9805 If the return value is based on the assumption that signed overflow
9806 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9807 change *STRICT_OVERFLOW_P. */
9809 static bool
9810 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9812 tree type = TREE_TYPE (t);
9813 enum tree_code code;
9815 /* Doing something useful for floating point would need more work. */
9816 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9817 return false;
9819 code = TREE_CODE (t);
9820 switch (TREE_CODE_CLASS (code))
9822 case tcc_unary:
9823 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9824 strict_overflow_p);
9825 case tcc_binary:
9826 case tcc_comparison:
9827 return tree_binary_nonzero_warnv_p (code, type,
9828 TREE_OPERAND (t, 0),
9829 TREE_OPERAND (t, 1),
9830 strict_overflow_p);
9831 case tcc_constant:
9832 case tcc_declaration:
9833 case tcc_reference:
9834 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9836 default:
9837 break;
9840 switch (code)
9842 case TRUTH_NOT_EXPR:
9843 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9844 strict_overflow_p);
9846 case TRUTH_AND_EXPR:
9847 case TRUTH_OR_EXPR:
9848 case TRUTH_XOR_EXPR:
9849 return tree_binary_nonzero_warnv_p (code, type,
9850 TREE_OPERAND (t, 0),
9851 TREE_OPERAND (t, 1),
9852 strict_overflow_p);
9854 case COND_EXPR:
9855 case CONSTRUCTOR:
9856 case OBJ_TYPE_REF:
9857 case ASSERT_EXPR:
9858 case ADDR_EXPR:
9859 case WITH_SIZE_EXPR:
9860 case SSA_NAME:
9861 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9863 case COMPOUND_EXPR:
9864 case MODIFY_EXPR:
9865 case BIND_EXPR:
9866 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9867 strict_overflow_p);
9869 case SAVE_EXPR:
9870 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9871 strict_overflow_p);
9873 case CALL_EXPR:
9875 tree fndecl = get_callee_fndecl (t);
9876 if (!fndecl) return false;
9877 if (flag_delete_null_pointer_checks && !flag_check_new
9878 && DECL_IS_OPERATOR_NEW (fndecl)
9879 && !TREE_NOTHROW (fndecl))
9880 return true;
9881 if (flag_delete_null_pointer_checks
9882 && lookup_attribute ("returns_nonnull",
9883 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9884 return true;
9885 return alloca_call_p (t);
9888 default:
9889 break;
9891 return false;
9894 /* Return true when T is an address and is known to be nonzero.
9895 Handle warnings about undefined signed overflow. */
9897 static bool
9898 tree_expr_nonzero_p (tree t)
9900 bool ret, strict_overflow_p;
9902 strict_overflow_p = false;
9903 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9904 if (strict_overflow_p)
9905 fold_overflow_warning (("assuming signed overflow does not occur when "
9906 "determining that expression is always "
9907 "non-zero"),
9908 WARN_STRICT_OVERFLOW_MISC);
9909 return ret;
9912 /* Fold a binary expression of code CODE and type TYPE with operands
9913 OP0 and OP1. LOC is the location of the resulting expression.
9914 Return the folded expression if folding is successful. Otherwise,
9915 return NULL_TREE. */
9917 tree
9918 fold_binary_loc (location_t loc,
9919 enum tree_code code, tree type, tree op0, tree op1)
9921 enum tree_code_class kind = TREE_CODE_CLASS (code);
9922 tree arg0, arg1, tem;
9923 tree t1 = NULL_TREE;
9924 bool strict_overflow_p;
9925 unsigned int prec;
9927 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9928 && TREE_CODE_LENGTH (code) == 2
9929 && op0 != NULL_TREE
9930 && op1 != NULL_TREE);
9932 arg0 = op0;
9933 arg1 = op1;
9935 /* Strip any conversions that don't change the mode. This is
9936 safe for every expression, except for a comparison expression
9937 because its signedness is derived from its operands. So, in
9938 the latter case, only strip conversions that don't change the
9939 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9940 preserved.
9942 Note that this is done as an internal manipulation within the
9943 constant folder, in order to find the simplest representation
9944 of the arguments so that their form can be studied. In any
9945 cases, the appropriate type conversions should be put back in
9946 the tree that will get out of the constant folder. */
9948 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9950 STRIP_SIGN_NOPS (arg0);
9951 STRIP_SIGN_NOPS (arg1);
9953 else
9955 STRIP_NOPS (arg0);
9956 STRIP_NOPS (arg1);
9959 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9960 constant but we can't do arithmetic on them. */
9961 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9962 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9963 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9964 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9965 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9966 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST)
9967 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == INTEGER_CST))
9969 if (kind == tcc_binary)
9971 /* Make sure type and arg0 have the same saturating flag. */
9972 gcc_assert (TYPE_SATURATING (type)
9973 == TYPE_SATURATING (TREE_TYPE (arg0)));
9974 tem = const_binop (code, arg0, arg1);
9976 else if (kind == tcc_comparison)
9977 tem = fold_relational_const (code, type, arg0, arg1);
9978 else
9979 tem = NULL_TREE;
9981 if (tem != NULL_TREE)
9983 if (TREE_TYPE (tem) != type)
9984 tem = fold_convert_loc (loc, type, tem);
9985 return tem;
9989 /* If this is a commutative operation, and ARG0 is a constant, move it
9990 to ARG1 to reduce the number of tests below. */
9991 if (commutative_tree_code (code)
9992 && tree_swap_operands_p (arg0, arg1, true))
9993 return fold_build2_loc (loc, code, type, op1, op0);
9995 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9997 First check for cases where an arithmetic operation is applied to a
9998 compound, conditional, or comparison operation. Push the arithmetic
9999 operation inside the compound or conditional to see if any folding
10000 can then be done. Convert comparison to conditional for this purpose.
10001 The also optimizes non-constant cases that used to be done in
10002 expand_expr.
10004 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10005 one of the operands is a comparison and the other is a comparison, a
10006 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10007 code below would make the expression more complex. Change it to a
10008 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10009 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10011 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10012 || code == EQ_EXPR || code == NE_EXPR)
10013 && TREE_CODE (type) != VECTOR_TYPE
10014 && ((truth_value_p (TREE_CODE (arg0))
10015 && (truth_value_p (TREE_CODE (arg1))
10016 || (TREE_CODE (arg1) == BIT_AND_EXPR
10017 && integer_onep (TREE_OPERAND (arg1, 1)))))
10018 || (truth_value_p (TREE_CODE (arg1))
10019 && (truth_value_p (TREE_CODE (arg0))
10020 || (TREE_CODE (arg0) == BIT_AND_EXPR
10021 && integer_onep (TREE_OPERAND (arg0, 1)))))))
10023 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10024 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
10025 : TRUTH_XOR_EXPR,
10026 boolean_type_node,
10027 fold_convert_loc (loc, boolean_type_node, arg0),
10028 fold_convert_loc (loc, boolean_type_node, arg1));
10030 if (code == EQ_EXPR)
10031 tem = invert_truthvalue_loc (loc, tem);
10033 return fold_convert_loc (loc, type, tem);
10036 if (TREE_CODE_CLASS (code) == tcc_binary
10037 || TREE_CODE_CLASS (code) == tcc_comparison)
10039 if (TREE_CODE (arg0) == COMPOUND_EXPR)
10041 tem = fold_build2_loc (loc, code, type,
10042 fold_convert_loc (loc, TREE_TYPE (op0),
10043 TREE_OPERAND (arg0, 1)), op1);
10044 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
10045 tem);
10047 if (TREE_CODE (arg1) == COMPOUND_EXPR
10048 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10050 tem = fold_build2_loc (loc, code, type, op0,
10051 fold_convert_loc (loc, TREE_TYPE (op1),
10052 TREE_OPERAND (arg1, 1)));
10053 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
10054 tem);
10057 if (TREE_CODE (arg0) == COND_EXPR
10058 || TREE_CODE (arg0) == VEC_COND_EXPR
10059 || COMPARISON_CLASS_P (arg0))
10061 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10062 arg0, arg1,
10063 /*cond_first_p=*/1);
10064 if (tem != NULL_TREE)
10065 return tem;
10068 if (TREE_CODE (arg1) == COND_EXPR
10069 || TREE_CODE (arg1) == VEC_COND_EXPR
10070 || COMPARISON_CLASS_P (arg1))
10072 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10073 arg1, arg0,
10074 /*cond_first_p=*/0);
10075 if (tem != NULL_TREE)
10076 return tem;
10080 switch (code)
10082 case MEM_REF:
10083 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
10084 if (TREE_CODE (arg0) == ADDR_EXPR
10085 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
10087 tree iref = TREE_OPERAND (arg0, 0);
10088 return fold_build2 (MEM_REF, type,
10089 TREE_OPERAND (iref, 0),
10090 int_const_binop (PLUS_EXPR, arg1,
10091 TREE_OPERAND (iref, 1)));
10094 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
10095 if (TREE_CODE (arg0) == ADDR_EXPR
10096 && handled_component_p (TREE_OPERAND (arg0, 0)))
10098 tree base;
10099 HOST_WIDE_INT coffset;
10100 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
10101 &coffset);
10102 if (!base)
10103 return NULL_TREE;
10104 return fold_build2 (MEM_REF, type,
10105 build_fold_addr_expr (base),
10106 int_const_binop (PLUS_EXPR, arg1,
10107 size_int (coffset)));
10110 return NULL_TREE;
10112 case POINTER_PLUS_EXPR:
10113 /* 0 +p index -> (type)index */
10114 if (integer_zerop (arg0))
10115 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10117 /* PTR +p 0 -> PTR */
10118 if (integer_zerop (arg1))
10119 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10121 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
10122 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10123 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10124 return fold_convert_loc (loc, type,
10125 fold_build2_loc (loc, PLUS_EXPR, sizetype,
10126 fold_convert_loc (loc, sizetype,
10127 arg1),
10128 fold_convert_loc (loc, sizetype,
10129 arg0)));
10131 /* (PTR +p B) +p A -> PTR +p (B + A) */
10132 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10134 tree inner;
10135 tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
10136 tree arg00 = TREE_OPERAND (arg0, 0);
10137 inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
10138 arg01, fold_convert_loc (loc, sizetype, arg1));
10139 return fold_convert_loc (loc, type,
10140 fold_build_pointer_plus_loc (loc,
10141 arg00, inner));
10144 /* PTR_CST +p CST -> CST1 */
10145 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10146 return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
10147 fold_convert_loc (loc, type, arg1));
10149 return NULL_TREE;
10151 case PLUS_EXPR:
10152 /* A + (-B) -> A - B */
10153 if (TREE_CODE (arg1) == NEGATE_EXPR
10154 && (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
10155 return fold_build2_loc (loc, MINUS_EXPR, type,
10156 fold_convert_loc (loc, type, arg0),
10157 fold_convert_loc (loc, type,
10158 TREE_OPERAND (arg1, 0)));
10159 /* (-A) + B -> B - A */
10160 if (TREE_CODE (arg0) == NEGATE_EXPR
10161 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1)
10162 && (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
10163 return fold_build2_loc (loc, MINUS_EXPR, type,
10164 fold_convert_loc (loc, type, arg1),
10165 fold_convert_loc (loc, type,
10166 TREE_OPERAND (arg0, 0)));
10168 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
10170 /* Convert ~A + 1 to -A. */
10171 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10172 && integer_each_onep (arg1))
10173 return fold_build1_loc (loc, NEGATE_EXPR, type,
10174 fold_convert_loc (loc, type,
10175 TREE_OPERAND (arg0, 0)));
10177 /* ~X + X is -1. */
10178 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10179 && !TYPE_OVERFLOW_TRAPS (type))
10181 tree tem = TREE_OPERAND (arg0, 0);
10183 STRIP_NOPS (tem);
10184 if (operand_equal_p (tem, arg1, 0))
10186 t1 = build_all_ones_cst (type);
10187 return omit_one_operand_loc (loc, type, t1, arg1);
10191 /* X + ~X is -1. */
10192 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10193 && !TYPE_OVERFLOW_TRAPS (type))
10195 tree tem = TREE_OPERAND (arg1, 0);
10197 STRIP_NOPS (tem);
10198 if (operand_equal_p (arg0, tem, 0))
10200 t1 = build_all_ones_cst (type);
10201 return omit_one_operand_loc (loc, type, t1, arg0);
10205 /* X + (X / CST) * -CST is X % CST. */
10206 if (TREE_CODE (arg1) == MULT_EXPR
10207 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10208 && operand_equal_p (arg0,
10209 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
10211 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
10212 tree cst1 = TREE_OPERAND (arg1, 1);
10213 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
10214 cst1, cst0);
10215 if (sum && integer_zerop (sum))
10216 return fold_convert_loc (loc, type,
10217 fold_build2_loc (loc, TRUNC_MOD_EXPR,
10218 TREE_TYPE (arg0), arg0,
10219 cst0));
10223 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
10224 one. Make sure the type is not saturating and has the signedness of
10225 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10226 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10227 if ((TREE_CODE (arg0) == MULT_EXPR
10228 || TREE_CODE (arg1) == MULT_EXPR)
10229 && !TYPE_SATURATING (type)
10230 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10231 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10232 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10234 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10235 if (tem)
10236 return tem;
10239 if (! FLOAT_TYPE_P (type))
10241 if (integer_zerop (arg1))
10242 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10244 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
10245 with a constant, and the two constants have no bits in common,
10246 we should treat this as a BIT_IOR_EXPR since this may produce more
10247 simplifications. */
10248 if (TREE_CODE (arg0) == BIT_AND_EXPR
10249 && TREE_CODE (arg1) == BIT_AND_EXPR
10250 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10251 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10252 && wi::bit_and (TREE_OPERAND (arg0, 1),
10253 TREE_OPERAND (arg1, 1)) == 0)
10255 code = BIT_IOR_EXPR;
10256 goto bit_ior;
10259 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10260 (plus (plus (mult) (mult)) (foo)) so that we can
10261 take advantage of the factoring cases below. */
10262 if (TYPE_OVERFLOW_WRAPS (type)
10263 && (((TREE_CODE (arg0) == PLUS_EXPR
10264 || TREE_CODE (arg0) == MINUS_EXPR)
10265 && TREE_CODE (arg1) == MULT_EXPR)
10266 || ((TREE_CODE (arg1) == PLUS_EXPR
10267 || TREE_CODE (arg1) == MINUS_EXPR)
10268 && TREE_CODE (arg0) == MULT_EXPR)))
10270 tree parg0, parg1, parg, marg;
10271 enum tree_code pcode;
10273 if (TREE_CODE (arg1) == MULT_EXPR)
10274 parg = arg0, marg = arg1;
10275 else
10276 parg = arg1, marg = arg0;
10277 pcode = TREE_CODE (parg);
10278 parg0 = TREE_OPERAND (parg, 0);
10279 parg1 = TREE_OPERAND (parg, 1);
10280 STRIP_NOPS (parg0);
10281 STRIP_NOPS (parg1);
10283 if (TREE_CODE (parg0) == MULT_EXPR
10284 && TREE_CODE (parg1) != MULT_EXPR)
10285 return fold_build2_loc (loc, pcode, type,
10286 fold_build2_loc (loc, PLUS_EXPR, type,
10287 fold_convert_loc (loc, type,
10288 parg0),
10289 fold_convert_loc (loc, type,
10290 marg)),
10291 fold_convert_loc (loc, type, parg1));
10292 if (TREE_CODE (parg0) != MULT_EXPR
10293 && TREE_CODE (parg1) == MULT_EXPR)
10294 return
10295 fold_build2_loc (loc, PLUS_EXPR, type,
10296 fold_convert_loc (loc, type, parg0),
10297 fold_build2_loc (loc, pcode, type,
10298 fold_convert_loc (loc, type, marg),
10299 fold_convert_loc (loc, type,
10300 parg1)));
10303 else
10305 /* See if ARG1 is zero and X + ARG1 reduces to X. */
10306 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10307 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10309 /* Likewise if the operands are reversed. */
10310 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10311 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10313 /* Convert X + -C into X - C. */
10314 if (TREE_CODE (arg1) == REAL_CST
10315 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10317 tem = fold_negate_const (arg1, type);
10318 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10319 return fold_build2_loc (loc, MINUS_EXPR, type,
10320 fold_convert_loc (loc, type, arg0),
10321 fold_convert_loc (loc, type, tem));
10324 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10325 to __complex__ ( x, y ). This is not the same for SNaNs or
10326 if signed zeros are involved. */
10327 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10328 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10329 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10331 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10332 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10333 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10334 bool arg0rz = false, arg0iz = false;
10335 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10336 || (arg0i && (arg0iz = real_zerop (arg0i))))
10338 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10339 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10340 if (arg0rz && arg1i && real_zerop (arg1i))
10342 tree rp = arg1r ? arg1r
10343 : build1 (REALPART_EXPR, rtype, arg1);
10344 tree ip = arg0i ? arg0i
10345 : build1 (IMAGPART_EXPR, rtype, arg0);
10346 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10348 else if (arg0iz && arg1r && real_zerop (arg1r))
10350 tree rp = arg0r ? arg0r
10351 : build1 (REALPART_EXPR, rtype, arg0);
10352 tree ip = arg1i ? arg1i
10353 : build1 (IMAGPART_EXPR, rtype, arg1);
10354 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10359 if (flag_unsafe_math_optimizations
10360 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10361 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10362 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10363 return tem;
10365 /* Convert x+x into x*2.0. */
10366 if (operand_equal_p (arg0, arg1, 0)
10367 && SCALAR_FLOAT_TYPE_P (type))
10368 return fold_build2_loc (loc, MULT_EXPR, type, arg0,
10369 build_real (type, dconst2));
10371 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10372 We associate floats only if the user has specified
10373 -fassociative-math. */
10374 if (flag_associative_math
10375 && TREE_CODE (arg1) == PLUS_EXPR
10376 && TREE_CODE (arg0) != MULT_EXPR)
10378 tree tree10 = TREE_OPERAND (arg1, 0);
10379 tree tree11 = TREE_OPERAND (arg1, 1);
10380 if (TREE_CODE (tree11) == MULT_EXPR
10381 && TREE_CODE (tree10) == MULT_EXPR)
10383 tree tree0;
10384 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
10385 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
10388 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10389 We associate floats only if the user has specified
10390 -fassociative-math. */
10391 if (flag_associative_math
10392 && TREE_CODE (arg0) == PLUS_EXPR
10393 && TREE_CODE (arg1) != MULT_EXPR)
10395 tree tree00 = TREE_OPERAND (arg0, 0);
10396 tree tree01 = TREE_OPERAND (arg0, 1);
10397 if (TREE_CODE (tree01) == MULT_EXPR
10398 && TREE_CODE (tree00) == MULT_EXPR)
10400 tree tree0;
10401 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
10402 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
10407 bit_rotate:
10408 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10409 is a rotate of A by C1 bits. */
10410 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10411 is a rotate of A by B bits. */
10413 enum tree_code code0, code1;
10414 tree rtype;
10415 code0 = TREE_CODE (arg0);
10416 code1 = TREE_CODE (arg1);
10417 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10418 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10419 && operand_equal_p (TREE_OPERAND (arg0, 0),
10420 TREE_OPERAND (arg1, 0), 0)
10421 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10422 TYPE_UNSIGNED (rtype))
10423 /* Only create rotates in complete modes. Other cases are not
10424 expanded properly. */
10425 && (element_precision (rtype)
10426 == element_precision (TYPE_MODE (rtype))))
10428 tree tree01, tree11;
10429 enum tree_code code01, code11;
10431 tree01 = TREE_OPERAND (arg0, 1);
10432 tree11 = TREE_OPERAND (arg1, 1);
10433 STRIP_NOPS (tree01);
10434 STRIP_NOPS (tree11);
10435 code01 = TREE_CODE (tree01);
10436 code11 = TREE_CODE (tree11);
10437 if (code01 == INTEGER_CST
10438 && code11 == INTEGER_CST
10439 && (wi::to_widest (tree01) + wi::to_widest (tree11)
10440 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10442 tem = build2_loc (loc, LROTATE_EXPR,
10443 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10444 TREE_OPERAND (arg0, 0),
10445 code0 == LSHIFT_EXPR ? tree01 : tree11);
10446 return fold_convert_loc (loc, type, tem);
10448 else if (code11 == MINUS_EXPR)
10450 tree tree110, tree111;
10451 tree110 = TREE_OPERAND (tree11, 0);
10452 tree111 = TREE_OPERAND (tree11, 1);
10453 STRIP_NOPS (tree110);
10454 STRIP_NOPS (tree111);
10455 if (TREE_CODE (tree110) == INTEGER_CST
10456 && 0 == compare_tree_int (tree110,
10457 element_precision
10458 (TREE_TYPE (TREE_OPERAND
10459 (arg0, 0))))
10460 && operand_equal_p (tree01, tree111, 0))
10461 return
10462 fold_convert_loc (loc, type,
10463 build2 ((code0 == LSHIFT_EXPR
10464 ? LROTATE_EXPR
10465 : RROTATE_EXPR),
10466 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10467 TREE_OPERAND (arg0, 0), tree01));
10469 else if (code01 == MINUS_EXPR)
10471 tree tree010, tree011;
10472 tree010 = TREE_OPERAND (tree01, 0);
10473 tree011 = TREE_OPERAND (tree01, 1);
10474 STRIP_NOPS (tree010);
10475 STRIP_NOPS (tree011);
10476 if (TREE_CODE (tree010) == INTEGER_CST
10477 && 0 == compare_tree_int (tree010,
10478 element_precision
10479 (TREE_TYPE (TREE_OPERAND
10480 (arg0, 0))))
10481 && operand_equal_p (tree11, tree011, 0))
10482 return fold_convert_loc
10483 (loc, type,
10484 build2 ((code0 != LSHIFT_EXPR
10485 ? LROTATE_EXPR
10486 : RROTATE_EXPR),
10487 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10488 TREE_OPERAND (arg0, 0), tree11));
10493 associate:
10494 /* In most languages, can't associate operations on floats through
10495 parentheses. Rather than remember where the parentheses were, we
10496 don't associate floats at all, unless the user has specified
10497 -fassociative-math.
10498 And, we need to make sure type is not saturating. */
10500 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10501 && !TYPE_SATURATING (type))
10503 tree var0, con0, lit0, minus_lit0;
10504 tree var1, con1, lit1, minus_lit1;
10505 tree atype = type;
10506 bool ok = true;
10508 /* Split both trees into variables, constants, and literals. Then
10509 associate each group together, the constants with literals,
10510 then the result with variables. This increases the chances of
10511 literals being recombined later and of generating relocatable
10512 expressions for the sum of a constant and literal. */
10513 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10514 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10515 code == MINUS_EXPR);
10517 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
10518 if (code == MINUS_EXPR)
10519 code = PLUS_EXPR;
10521 /* With undefined overflow prefer doing association in a type
10522 which wraps on overflow, if that is one of the operand types. */
10523 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10524 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10526 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10527 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10528 atype = TREE_TYPE (arg0);
10529 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10530 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
10531 atype = TREE_TYPE (arg1);
10532 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
10535 /* With undefined overflow we can only associate constants with one
10536 variable, and constants whose association doesn't overflow. */
10537 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10538 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
10540 if (var0 && var1)
10542 tree tmp0 = var0;
10543 tree tmp1 = var1;
10545 if (TREE_CODE (tmp0) == NEGATE_EXPR)
10546 tmp0 = TREE_OPERAND (tmp0, 0);
10547 if (CONVERT_EXPR_P (tmp0)
10548 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
10549 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
10550 <= TYPE_PRECISION (atype)))
10551 tmp0 = TREE_OPERAND (tmp0, 0);
10552 if (TREE_CODE (tmp1) == NEGATE_EXPR)
10553 tmp1 = TREE_OPERAND (tmp1, 0);
10554 if (CONVERT_EXPR_P (tmp1)
10555 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
10556 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
10557 <= TYPE_PRECISION (atype)))
10558 tmp1 = TREE_OPERAND (tmp1, 0);
10559 /* The only case we can still associate with two variables
10560 is if they are the same, modulo negation and bit-pattern
10561 preserving conversions. */
10562 if (!operand_equal_p (tmp0, tmp1, 0))
10563 ok = false;
10567 /* Only do something if we found more than two objects. Otherwise,
10568 nothing has changed and we risk infinite recursion. */
10569 if (ok
10570 && (2 < ((var0 != 0) + (var1 != 0)
10571 + (con0 != 0) + (con1 != 0)
10572 + (lit0 != 0) + (lit1 != 0)
10573 + (minus_lit0 != 0) + (minus_lit1 != 0))))
10575 bool any_overflows = false;
10576 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
10577 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
10578 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
10579 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
10580 var0 = associate_trees (loc, var0, var1, code, atype);
10581 con0 = associate_trees (loc, con0, con1, code, atype);
10582 lit0 = associate_trees (loc, lit0, lit1, code, atype);
10583 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
10584 code, atype);
10586 /* Preserve the MINUS_EXPR if the negative part of the literal is
10587 greater than the positive part. Otherwise, the multiplicative
10588 folding code (i.e extract_muldiv) may be fooled in case
10589 unsigned constants are subtracted, like in the following
10590 example: ((X*2 + 4) - 8U)/2. */
10591 if (minus_lit0 && lit0)
10593 if (TREE_CODE (lit0) == INTEGER_CST
10594 && TREE_CODE (minus_lit0) == INTEGER_CST
10595 && tree_int_cst_lt (lit0, minus_lit0))
10597 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10598 MINUS_EXPR, atype);
10599 lit0 = 0;
10601 else
10603 lit0 = associate_trees (loc, lit0, minus_lit0,
10604 MINUS_EXPR, atype);
10605 minus_lit0 = 0;
10609 /* Don't introduce overflows through reassociation. */
10610 if (!any_overflows
10611 && ((lit0 && TREE_OVERFLOW (lit0))
10612 || (minus_lit0 && TREE_OVERFLOW (minus_lit0))))
10613 return NULL_TREE;
10615 if (minus_lit0)
10617 if (con0 == 0)
10618 return
10619 fold_convert_loc (loc, type,
10620 associate_trees (loc, var0, minus_lit0,
10621 MINUS_EXPR, atype));
10622 else
10624 con0 = associate_trees (loc, con0, minus_lit0,
10625 MINUS_EXPR, atype);
10626 return
10627 fold_convert_loc (loc, type,
10628 associate_trees (loc, var0, con0,
10629 PLUS_EXPR, atype));
10633 con0 = associate_trees (loc, con0, lit0, code, atype);
10634 return
10635 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10636 code, atype));
10640 return NULL_TREE;
10642 case MINUS_EXPR:
10643 /* Pointer simplifications for subtraction, simple reassociations. */
10644 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10646 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10647 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10648 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10650 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10651 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10652 tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10653 tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10654 return fold_build2_loc (loc, PLUS_EXPR, type,
10655 fold_build2_loc (loc, MINUS_EXPR, type,
10656 arg00, arg10),
10657 fold_build2_loc (loc, MINUS_EXPR, type,
10658 arg01, arg11));
10660 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10661 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10663 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10664 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10665 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10666 fold_convert_loc (loc, type, arg1));
10667 if (tmp)
10668 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10670 /* PTR0 - (PTR1 p+ A) -> (PTR0 - PTR1) - A, assuming PTR0 - PTR1
10671 simplifies. */
10672 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10674 tree arg10 = fold_convert_loc (loc, type,
10675 TREE_OPERAND (arg1, 0));
10676 tree arg11 = fold_convert_loc (loc, type,
10677 TREE_OPERAND (arg1, 1));
10678 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg0,
10679 fold_convert_loc (loc, type, arg10));
10680 if (tmp)
10681 return fold_build2_loc (loc, MINUS_EXPR, type, tmp, arg11);
10684 /* A - (-B) -> A + B */
10685 if (TREE_CODE (arg1) == NEGATE_EXPR)
10686 return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10687 fold_convert_loc (loc, type,
10688 TREE_OPERAND (arg1, 0)));
10689 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10690 if (TREE_CODE (arg0) == NEGATE_EXPR
10691 && negate_expr_p (arg1)
10692 && reorder_operands_p (arg0, arg1))
10693 return fold_build2_loc (loc, MINUS_EXPR, type,
10694 fold_convert_loc (loc, type,
10695 negate_expr (arg1)),
10696 fold_convert_loc (loc, type,
10697 TREE_OPERAND (arg0, 0)));
10698 /* Convert -A - 1 to ~A. */
10699 if (TREE_CODE (arg0) == NEGATE_EXPR
10700 && integer_each_onep (arg1)
10701 && !TYPE_OVERFLOW_TRAPS (type))
10702 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10703 fold_convert_loc (loc, type,
10704 TREE_OPERAND (arg0, 0)));
10706 /* Convert -1 - A to ~A. */
10707 if (TREE_CODE (type) != COMPLEX_TYPE
10708 && integer_all_onesp (arg0))
10709 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10712 /* X - (X / Y) * Y is X % Y. */
10713 if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
10714 && TREE_CODE (arg1) == MULT_EXPR
10715 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10716 && operand_equal_p (arg0,
10717 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10718 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10719 TREE_OPERAND (arg1, 1), 0))
10720 return
10721 fold_convert_loc (loc, type,
10722 fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10723 arg0, TREE_OPERAND (arg1, 1)));
10725 if (! FLOAT_TYPE_P (type))
10727 if (integer_zerop (arg0))
10728 return negate_expr (fold_convert_loc (loc, type, arg1));
10729 if (integer_zerop (arg1))
10730 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10732 /* Fold A - (A & B) into ~B & A. */
10733 if (!TREE_SIDE_EFFECTS (arg0)
10734 && TREE_CODE (arg1) == BIT_AND_EXPR)
10736 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10738 tree arg10 = fold_convert_loc (loc, type,
10739 TREE_OPERAND (arg1, 0));
10740 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10741 fold_build1_loc (loc, BIT_NOT_EXPR,
10742 type, arg10),
10743 fold_convert_loc (loc, type, arg0));
10745 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10747 tree arg11 = fold_convert_loc (loc,
10748 type, TREE_OPERAND (arg1, 1));
10749 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10750 fold_build1_loc (loc, BIT_NOT_EXPR,
10751 type, arg11),
10752 fold_convert_loc (loc, type, arg0));
10756 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10757 any power of 2 minus 1. */
10758 if (TREE_CODE (arg0) == BIT_AND_EXPR
10759 && TREE_CODE (arg1) == BIT_AND_EXPR
10760 && operand_equal_p (TREE_OPERAND (arg0, 0),
10761 TREE_OPERAND (arg1, 0), 0))
10763 tree mask0 = TREE_OPERAND (arg0, 1);
10764 tree mask1 = TREE_OPERAND (arg1, 1);
10765 tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10767 if (operand_equal_p (tem, mask1, 0))
10769 tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10770 TREE_OPERAND (arg0, 0), mask1);
10771 return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10776 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10777 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10778 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10780 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10781 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10782 (-ARG1 + ARG0) reduces to -ARG1. */
10783 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10784 return negate_expr (fold_convert_loc (loc, type, arg1));
10786 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10787 __complex__ ( x, -y ). This is not the same for SNaNs or if
10788 signed zeros are involved. */
10789 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10790 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10791 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10793 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10794 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10795 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10796 bool arg0rz = false, arg0iz = false;
10797 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10798 || (arg0i && (arg0iz = real_zerop (arg0i))))
10800 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10801 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10802 if (arg0rz && arg1i && real_zerop (arg1i))
10804 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10805 arg1r ? arg1r
10806 : build1 (REALPART_EXPR, rtype, arg1));
10807 tree ip = arg0i ? arg0i
10808 : build1 (IMAGPART_EXPR, rtype, arg0);
10809 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10811 else if (arg0iz && arg1r && real_zerop (arg1r))
10813 tree rp = arg0r ? arg0r
10814 : build1 (REALPART_EXPR, rtype, arg0);
10815 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10816 arg1i ? arg1i
10817 : build1 (IMAGPART_EXPR, rtype, arg1));
10818 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10823 /* Fold &x - &x. This can happen from &x.foo - &x.
10824 This is unsafe for certain floats even in non-IEEE formats.
10825 In IEEE, it is unsafe because it does wrong for NaNs.
10826 Also note that operand_equal_p is always false if an operand
10827 is volatile. */
10829 if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10830 && operand_equal_p (arg0, arg1, 0))
10831 return build_zero_cst (type);
10833 /* A - B -> A + (-B) if B is easily negatable. */
10834 if (negate_expr_p (arg1)
10835 && ((FLOAT_TYPE_P (type)
10836 /* Avoid this transformation if B is a positive REAL_CST. */
10837 && (TREE_CODE (arg1) != REAL_CST
10838 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10839 || INTEGRAL_TYPE_P (type)))
10840 return fold_build2_loc (loc, PLUS_EXPR, type,
10841 fold_convert_loc (loc, type, arg0),
10842 fold_convert_loc (loc, type,
10843 negate_expr (arg1)));
10845 /* Try folding difference of addresses. */
10847 HOST_WIDE_INT diff;
10849 if ((TREE_CODE (arg0) == ADDR_EXPR
10850 || TREE_CODE (arg1) == ADDR_EXPR)
10851 && ptr_difference_const (arg0, arg1, &diff))
10852 return build_int_cst_type (type, diff);
10855 /* Fold &a[i] - &a[j] to i-j. */
10856 if (TREE_CODE (arg0) == ADDR_EXPR
10857 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10858 && TREE_CODE (arg1) == ADDR_EXPR
10859 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10861 tree tem = fold_addr_of_array_ref_difference (loc, type,
10862 TREE_OPERAND (arg0, 0),
10863 TREE_OPERAND (arg1, 0));
10864 if (tem)
10865 return tem;
10868 if (FLOAT_TYPE_P (type)
10869 && flag_unsafe_math_optimizations
10870 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10871 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10872 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10873 return tem;
10875 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
10876 one. Make sure the type is not saturating and has the signedness of
10877 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10878 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10879 if ((TREE_CODE (arg0) == MULT_EXPR
10880 || TREE_CODE (arg1) == MULT_EXPR)
10881 && !TYPE_SATURATING (type)
10882 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10883 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10884 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10886 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10887 if (tem)
10888 return tem;
10891 goto associate;
10893 case MULT_EXPR:
10894 /* (-A) * (-B) -> A * B */
10895 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10896 return fold_build2_loc (loc, MULT_EXPR, type,
10897 fold_convert_loc (loc, type,
10898 TREE_OPERAND (arg0, 0)),
10899 fold_convert_loc (loc, type,
10900 negate_expr (arg1)));
10901 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10902 return fold_build2_loc (loc, MULT_EXPR, type,
10903 fold_convert_loc (loc, type,
10904 negate_expr (arg0)),
10905 fold_convert_loc (loc, type,
10906 TREE_OPERAND (arg1, 0)));
10908 if (! FLOAT_TYPE_P (type))
10910 if (integer_zerop (arg1))
10911 return omit_one_operand_loc (loc, type, arg1, arg0);
10912 if (integer_onep (arg1))
10913 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10914 /* Transform x * -1 into -x. Make sure to do the negation
10915 on the original operand with conversions not stripped
10916 because we can only strip non-sign-changing conversions. */
10917 if (integer_minus_onep (arg1))
10918 return fold_convert_loc (loc, type, negate_expr (op0));
10919 /* Transform x * -C into -x * C if x is easily negatable. */
10920 if (TREE_CODE (arg1) == INTEGER_CST
10921 && tree_int_cst_sgn (arg1) == -1
10922 && negate_expr_p (arg0)
10923 && (tem = negate_expr (arg1)) != arg1
10924 && !TREE_OVERFLOW (tem))
10925 return fold_build2_loc (loc, MULT_EXPR, type,
10926 fold_convert_loc (loc, type,
10927 negate_expr (arg0)),
10928 tem);
10930 /* (a * (1 << b)) is (a << b) */
10931 if (TREE_CODE (arg1) == LSHIFT_EXPR
10932 && integer_onep (TREE_OPERAND (arg1, 0)))
10933 return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10934 TREE_OPERAND (arg1, 1));
10935 if (TREE_CODE (arg0) == LSHIFT_EXPR
10936 && integer_onep (TREE_OPERAND (arg0, 0)))
10937 return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10938 TREE_OPERAND (arg0, 1));
10940 /* (A + A) * C -> A * 2 * C */
10941 if (TREE_CODE (arg0) == PLUS_EXPR
10942 && TREE_CODE (arg1) == INTEGER_CST
10943 && operand_equal_p (TREE_OPERAND (arg0, 0),
10944 TREE_OPERAND (arg0, 1), 0))
10945 return fold_build2_loc (loc, MULT_EXPR, type,
10946 omit_one_operand_loc (loc, type,
10947 TREE_OPERAND (arg0, 0),
10948 TREE_OPERAND (arg0, 1)),
10949 fold_build2_loc (loc, MULT_EXPR, type,
10950 build_int_cst (type, 2) , arg1));
10952 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
10953 sign-changing only. */
10954 if (TREE_CODE (arg1) == INTEGER_CST
10955 && TREE_CODE (arg0) == EXACT_DIV_EXPR
10956 && operand_equal_p (arg1, TREE_OPERAND (arg0, 1), 0))
10957 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10959 strict_overflow_p = false;
10960 if (TREE_CODE (arg1) == INTEGER_CST
10961 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10962 &strict_overflow_p)))
10964 if (strict_overflow_p)
10965 fold_overflow_warning (("assuming signed overflow does not "
10966 "occur when simplifying "
10967 "multiplication"),
10968 WARN_STRICT_OVERFLOW_MISC);
10969 return fold_convert_loc (loc, type, tem);
10972 /* Optimize z * conj(z) for integer complex numbers. */
10973 if (TREE_CODE (arg0) == CONJ_EXPR
10974 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10975 return fold_mult_zconjz (loc, type, arg1);
10976 if (TREE_CODE (arg1) == CONJ_EXPR
10977 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10978 return fold_mult_zconjz (loc, type, arg0);
10980 else
10982 /* Maybe fold x * 0 to 0. The expressions aren't the same
10983 when x is NaN, since x * 0 is also NaN. Nor are they the
10984 same in modes with signed zeros, since multiplying a
10985 negative value by 0 gives -0, not +0. */
10986 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10987 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10988 && real_zerop (arg1))
10989 return omit_one_operand_loc (loc, type, arg1, arg0);
10990 /* In IEEE floating point, x*1 is not equivalent to x for snans.
10991 Likewise for complex arithmetic with signed zeros. */
10992 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10993 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10994 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10995 && real_onep (arg1))
10996 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10998 /* Transform x * -1.0 into -x. */
10999 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11000 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11001 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11002 && real_minus_onep (arg1))
11003 return fold_convert_loc (loc, type, negate_expr (arg0));
11005 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
11006 the result for floating point types due to rounding so it is applied
11007 only if -fassociative-math was specify. */
11008 if (flag_associative_math
11009 && TREE_CODE (arg0) == RDIV_EXPR
11010 && TREE_CODE (arg1) == REAL_CST
11011 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
11013 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
11014 arg1);
11015 if (tem)
11016 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11017 TREE_OPERAND (arg0, 1));
11020 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
11021 if (operand_equal_p (arg0, arg1, 0))
11023 tree tem = fold_strip_sign_ops (arg0);
11024 if (tem != NULL_TREE)
11026 tem = fold_convert_loc (loc, type, tem);
11027 return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
11031 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11032 This is not the same for NaNs or if signed zeros are
11033 involved. */
11034 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11035 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11036 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11037 && TREE_CODE (arg1) == COMPLEX_CST
11038 && real_zerop (TREE_REALPART (arg1)))
11040 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11041 if (real_onep (TREE_IMAGPART (arg1)))
11042 return
11043 fold_build2_loc (loc, COMPLEX_EXPR, type,
11044 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
11045 rtype, arg0)),
11046 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
11047 else if (real_minus_onep (TREE_IMAGPART (arg1)))
11048 return
11049 fold_build2_loc (loc, COMPLEX_EXPR, type,
11050 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
11051 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
11052 rtype, arg0)));
11055 /* Optimize z * conj(z) for floating point complex numbers.
11056 Guarded by flag_unsafe_math_optimizations as non-finite
11057 imaginary components don't produce scalar results. */
11058 if (flag_unsafe_math_optimizations
11059 && TREE_CODE (arg0) == CONJ_EXPR
11060 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11061 return fold_mult_zconjz (loc, type, arg1);
11062 if (flag_unsafe_math_optimizations
11063 && TREE_CODE (arg1) == CONJ_EXPR
11064 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11065 return fold_mult_zconjz (loc, type, arg0);
11067 if (flag_unsafe_math_optimizations)
11069 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11070 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11072 /* Optimizations of root(...)*root(...). */
11073 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
11075 tree rootfn, arg;
11076 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11077 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11079 /* Optimize sqrt(x)*sqrt(x) as x. */
11080 if (BUILTIN_SQRT_P (fcode0)
11081 && operand_equal_p (arg00, arg10, 0)
11082 && ! HONOR_SNANS (TYPE_MODE (type)))
11083 return arg00;
11085 /* Optimize root(x)*root(y) as root(x*y). */
11086 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11087 arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
11088 return build_call_expr_loc (loc, rootfn, 1, arg);
11091 /* Optimize expN(x)*expN(y) as expN(x+y). */
11092 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
11094 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11095 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
11096 CALL_EXPR_ARG (arg0, 0),
11097 CALL_EXPR_ARG (arg1, 0));
11098 return build_call_expr_loc (loc, expfn, 1, arg);
11101 /* Optimizations of pow(...)*pow(...). */
11102 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
11103 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
11104 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
11106 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11107 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11108 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11109 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11111 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
11112 if (operand_equal_p (arg01, arg11, 0))
11114 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11115 tree arg = fold_build2_loc (loc, MULT_EXPR, type,
11116 arg00, arg10);
11117 return build_call_expr_loc (loc, powfn, 2, arg, arg01);
11120 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
11121 if (operand_equal_p (arg00, arg10, 0))
11123 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11124 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
11125 arg01, arg11);
11126 return build_call_expr_loc (loc, powfn, 2, arg00, arg);
11130 /* Optimize tan(x)*cos(x) as sin(x). */
11131 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
11132 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
11133 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
11134 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
11135 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
11136 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
11137 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11138 CALL_EXPR_ARG (arg1, 0), 0))
11140 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
11142 if (sinfn != NULL_TREE)
11143 return build_call_expr_loc (loc, sinfn, 1,
11144 CALL_EXPR_ARG (arg0, 0));
11147 /* Optimize x*pow(x,c) as pow(x,c+1). */
11148 if (fcode1 == BUILT_IN_POW
11149 || fcode1 == BUILT_IN_POWF
11150 || fcode1 == BUILT_IN_POWL)
11152 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11153 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11154 if (TREE_CODE (arg11) == REAL_CST
11155 && !TREE_OVERFLOW (arg11)
11156 && operand_equal_p (arg0, arg10, 0))
11158 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11159 REAL_VALUE_TYPE c;
11160 tree arg;
11162 c = TREE_REAL_CST (arg11);
11163 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
11164 arg = build_real (type, c);
11165 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11169 /* Optimize pow(x,c)*x as pow(x,c+1). */
11170 if (fcode0 == BUILT_IN_POW
11171 || fcode0 == BUILT_IN_POWF
11172 || fcode0 == BUILT_IN_POWL)
11174 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11175 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11176 if (TREE_CODE (arg01) == REAL_CST
11177 && !TREE_OVERFLOW (arg01)
11178 && operand_equal_p (arg1, arg00, 0))
11180 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11181 REAL_VALUE_TYPE c;
11182 tree arg;
11184 c = TREE_REAL_CST (arg01);
11185 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
11186 arg = build_real (type, c);
11187 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11191 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
11192 if (!in_gimple_form
11193 && optimize
11194 && operand_equal_p (arg0, arg1, 0))
11196 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
11198 if (powfn)
11200 tree arg = build_real (type, dconst2);
11201 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11206 goto associate;
11208 case BIT_IOR_EXPR:
11209 bit_ior:
11210 if (integer_all_onesp (arg1))
11211 return omit_one_operand_loc (loc, type, arg1, arg0);
11212 if (integer_zerop (arg1))
11213 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11214 if (operand_equal_p (arg0, arg1, 0))
11215 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11217 /* ~X | X is -1. */
11218 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11219 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11221 t1 = build_zero_cst (type);
11222 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11223 return omit_one_operand_loc (loc, type, t1, arg1);
11226 /* X | ~X is -1. */
11227 if (TREE_CODE (arg1) == BIT_NOT_EXPR
11228 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11230 t1 = build_zero_cst (type);
11231 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11232 return omit_one_operand_loc (loc, type, t1, arg0);
11235 /* Canonicalize (X & C1) | C2. */
11236 if (TREE_CODE (arg0) == BIT_AND_EXPR
11237 && TREE_CODE (arg1) == INTEGER_CST
11238 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11240 int width = TYPE_PRECISION (type), w;
11241 wide_int c1 = TREE_OPERAND (arg0, 1);
11242 wide_int c2 = arg1;
11244 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11245 if ((c1 & c2) == c1)
11246 return omit_one_operand_loc (loc, type, arg1,
11247 TREE_OPERAND (arg0, 0));
11249 wide_int msk = wi::mask (width, false,
11250 TYPE_PRECISION (TREE_TYPE (arg1)));
11252 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11253 if (msk.and_not (c1 | c2) == 0)
11254 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11255 TREE_OPERAND (arg0, 0), arg1);
11257 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11258 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11259 mode which allows further optimizations. */
11260 c1 &= msk;
11261 c2 &= msk;
11262 wide_int c3 = c1.and_not (c2);
11263 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11265 wide_int mask = wi::mask (w, false,
11266 TYPE_PRECISION (type));
11267 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
11269 c3 = mask;
11270 break;
11274 if (c3 != c1)
11275 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11276 fold_build2_loc (loc, BIT_AND_EXPR, type,
11277 TREE_OPERAND (arg0, 0),
11278 wide_int_to_tree (type,
11279 c3)),
11280 arg1);
11283 /* (X & Y) | Y is (X, Y). */
11284 if (TREE_CODE (arg0) == BIT_AND_EXPR
11285 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11286 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11287 /* (X & Y) | X is (Y, X). */
11288 if (TREE_CODE (arg0) == BIT_AND_EXPR
11289 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11290 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11291 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11292 /* X | (X & Y) is (Y, X). */
11293 if (TREE_CODE (arg1) == BIT_AND_EXPR
11294 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11295 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11296 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11297 /* X | (Y & X) is (Y, X). */
11298 if (TREE_CODE (arg1) == BIT_AND_EXPR
11299 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11300 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11301 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11303 /* (X & ~Y) | (~X & Y) is X ^ Y */
11304 if (TREE_CODE (arg0) == BIT_AND_EXPR
11305 && TREE_CODE (arg1) == BIT_AND_EXPR)
11307 tree a0, a1, l0, l1, n0, n1;
11309 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11310 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11312 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11313 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11315 n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
11316 n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
11318 if ((operand_equal_p (n0, a0, 0)
11319 && operand_equal_p (n1, a1, 0))
11320 || (operand_equal_p (n0, a1, 0)
11321 && operand_equal_p (n1, a0, 0)))
11322 return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
11325 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11326 if (t1 != NULL_TREE)
11327 return t1;
11329 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
11331 This results in more efficient code for machines without a NAND
11332 instruction. Combine will canonicalize to the first form
11333 which will allow use of NAND instructions provided by the
11334 backend if they exist. */
11335 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11336 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11338 return
11339 fold_build1_loc (loc, BIT_NOT_EXPR, type,
11340 build2 (BIT_AND_EXPR, type,
11341 fold_convert_loc (loc, type,
11342 TREE_OPERAND (arg0, 0)),
11343 fold_convert_loc (loc, type,
11344 TREE_OPERAND (arg1, 0))));
11347 /* See if this can be simplified into a rotate first. If that
11348 is unsuccessful continue in the association code. */
11349 goto bit_rotate;
11351 case BIT_XOR_EXPR:
11352 if (integer_zerop (arg1))
11353 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11354 if (integer_all_onesp (arg1))
11355 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
11356 if (operand_equal_p (arg0, arg1, 0))
11357 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11359 /* ~X ^ X is -1. */
11360 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11361 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11363 t1 = build_zero_cst (type);
11364 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11365 return omit_one_operand_loc (loc, type, t1, arg1);
11368 /* X ^ ~X is -1. */
11369 if (TREE_CODE (arg1) == BIT_NOT_EXPR
11370 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11372 t1 = build_zero_cst (type);
11373 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11374 return omit_one_operand_loc (loc, type, t1, arg0);
11377 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11378 with a constant, and the two constants have no bits in common,
11379 we should treat this as a BIT_IOR_EXPR since this may produce more
11380 simplifications. */
11381 if (TREE_CODE (arg0) == BIT_AND_EXPR
11382 && TREE_CODE (arg1) == BIT_AND_EXPR
11383 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11384 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11385 && wi::bit_and (TREE_OPERAND (arg0, 1),
11386 TREE_OPERAND (arg1, 1)) == 0)
11388 code = BIT_IOR_EXPR;
11389 goto bit_ior;
11392 /* (X | Y) ^ X -> Y & ~ X*/
11393 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11394 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11396 tree t2 = TREE_OPERAND (arg0, 1);
11397 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11398 arg1);
11399 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11400 fold_convert_loc (loc, type, t2),
11401 fold_convert_loc (loc, type, t1));
11402 return t1;
11405 /* (Y | X) ^ X -> Y & ~ X*/
11406 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11407 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11409 tree t2 = TREE_OPERAND (arg0, 0);
11410 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11411 arg1);
11412 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11413 fold_convert_loc (loc, type, t2),
11414 fold_convert_loc (loc, type, t1));
11415 return t1;
11418 /* X ^ (X | Y) -> Y & ~ X*/
11419 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11420 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11422 tree t2 = TREE_OPERAND (arg1, 1);
11423 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11424 arg0);
11425 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11426 fold_convert_loc (loc, type, t2),
11427 fold_convert_loc (loc, type, t1));
11428 return t1;
11431 /* X ^ (Y | X) -> Y & ~ X*/
11432 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11433 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11435 tree t2 = TREE_OPERAND (arg1, 0);
11436 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11437 arg0);
11438 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11439 fold_convert_loc (loc, type, t2),
11440 fold_convert_loc (loc, type, t1));
11441 return t1;
11444 /* Convert ~X ^ ~Y to X ^ Y. */
11445 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11446 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11447 return fold_build2_loc (loc, code, type,
11448 fold_convert_loc (loc, type,
11449 TREE_OPERAND (arg0, 0)),
11450 fold_convert_loc (loc, type,
11451 TREE_OPERAND (arg1, 0)));
11453 /* Convert ~X ^ C to X ^ ~C. */
11454 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11455 && TREE_CODE (arg1) == INTEGER_CST)
11456 return fold_build2_loc (loc, code, type,
11457 fold_convert_loc (loc, type,
11458 TREE_OPERAND (arg0, 0)),
11459 fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
11461 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11462 if (TREE_CODE (arg0) == BIT_AND_EXPR
11463 && INTEGRAL_TYPE_P (type)
11464 && integer_onep (TREE_OPERAND (arg0, 1))
11465 && integer_onep (arg1))
11466 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11467 build_zero_cst (TREE_TYPE (arg0)));
11469 /* Fold (X & Y) ^ Y as ~X & Y. */
11470 if (TREE_CODE (arg0) == BIT_AND_EXPR
11471 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11473 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11474 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11475 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11476 fold_convert_loc (loc, type, arg1));
11478 /* Fold (X & Y) ^ X as ~Y & X. */
11479 if (TREE_CODE (arg0) == BIT_AND_EXPR
11480 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11481 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11483 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11484 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11485 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11486 fold_convert_loc (loc, type, arg1));
11488 /* Fold X ^ (X & Y) as X & ~Y. */
11489 if (TREE_CODE (arg1) == BIT_AND_EXPR
11490 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11492 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11493 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11494 fold_convert_loc (loc, type, arg0),
11495 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11497 /* Fold X ^ (Y & X) as ~Y & X. */
11498 if (TREE_CODE (arg1) == BIT_AND_EXPR
11499 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11500 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11502 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11503 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11504 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11505 fold_convert_loc (loc, type, arg0));
11508 /* See if this can be simplified into a rotate first. If that
11509 is unsuccessful continue in the association code. */
11510 goto bit_rotate;
11512 case BIT_AND_EXPR:
11513 if (integer_all_onesp (arg1))
11514 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11515 if (integer_zerop (arg1))
11516 return omit_one_operand_loc (loc, type, arg1, arg0);
11517 if (operand_equal_p (arg0, arg1, 0))
11518 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11520 /* ~X & X, (X == 0) & X, and !X & X are always zero. */
11521 if ((TREE_CODE (arg0) == BIT_NOT_EXPR
11522 || TREE_CODE (arg0) == TRUTH_NOT_EXPR
11523 || (TREE_CODE (arg0) == EQ_EXPR
11524 && integer_zerop (TREE_OPERAND (arg0, 1))))
11525 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11526 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11528 /* X & ~X , X & (X == 0), and X & !X are always zero. */
11529 if ((TREE_CODE (arg1) == BIT_NOT_EXPR
11530 || TREE_CODE (arg1) == TRUTH_NOT_EXPR
11531 || (TREE_CODE (arg1) == EQ_EXPR
11532 && integer_zerop (TREE_OPERAND (arg1, 1))))
11533 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11534 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11536 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
11537 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11538 && TREE_CODE (arg1) == INTEGER_CST
11539 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11541 tree tmp1 = fold_convert_loc (loc, type, arg1);
11542 tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11543 tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11544 tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
11545 tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
11546 return
11547 fold_convert_loc (loc, type,
11548 fold_build2_loc (loc, BIT_IOR_EXPR,
11549 type, tmp2, tmp3));
11552 /* (X | Y) & Y is (X, Y). */
11553 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11554 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11555 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11556 /* (X | Y) & X is (Y, X). */
11557 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11558 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11559 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11560 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11561 /* X & (X | Y) is (Y, X). */
11562 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11563 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11564 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11565 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11566 /* X & (Y | X) is (Y, X). */
11567 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11568 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11569 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11570 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11572 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11573 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11574 && INTEGRAL_TYPE_P (type)
11575 && integer_onep (TREE_OPERAND (arg0, 1))
11576 && integer_onep (arg1))
11578 tree tem2;
11579 tem = TREE_OPERAND (arg0, 0);
11580 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
11581 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
11582 tem, tem2);
11583 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
11584 build_zero_cst (TREE_TYPE (tem)));
11586 /* Fold ~X & 1 as (X & 1) == 0. */
11587 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11588 && INTEGRAL_TYPE_P (type)
11589 && integer_onep (arg1))
11591 tree tem2;
11592 tem = TREE_OPERAND (arg0, 0);
11593 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
11594 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
11595 tem, tem2);
11596 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
11597 build_zero_cst (TREE_TYPE (tem)));
11599 /* Fold !X & 1 as X == 0. */
11600 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11601 && integer_onep (arg1))
11603 tem = TREE_OPERAND (arg0, 0);
11604 return fold_build2_loc (loc, EQ_EXPR, type, tem,
11605 build_zero_cst (TREE_TYPE (tem)));
11608 /* Fold (X ^ Y) & Y as ~X & Y. */
11609 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11610 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11612 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11613 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11614 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11615 fold_convert_loc (loc, type, arg1));
11617 /* Fold (X ^ Y) & X as ~Y & X. */
11618 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11619 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11620 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11622 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11623 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11624 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11625 fold_convert_loc (loc, type, arg1));
11627 /* Fold X & (X ^ Y) as X & ~Y. */
11628 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11629 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11631 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11632 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11633 fold_convert_loc (loc, type, arg0),
11634 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11636 /* Fold X & (Y ^ X) as ~Y & X. */
11637 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11638 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11639 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11641 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11642 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11643 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11644 fold_convert_loc (loc, type, arg0));
11647 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11648 multiple of 1 << CST. */
11649 if (TREE_CODE (arg1) == INTEGER_CST)
11651 wide_int cst1 = arg1;
11652 wide_int ncst1 = -cst1;
11653 if ((cst1 & ncst1) == ncst1
11654 && multiple_of_p (type, arg0,
11655 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11656 return fold_convert_loc (loc, type, arg0);
11659 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11660 bits from CST2. */
11661 if (TREE_CODE (arg1) == INTEGER_CST
11662 && TREE_CODE (arg0) == MULT_EXPR
11663 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11665 wide_int warg1 = arg1;
11666 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
11668 if (masked == 0)
11669 return omit_two_operands_loc (loc, type, build_zero_cst (type),
11670 arg0, arg1);
11671 else if (masked != warg1)
11673 /* Avoid the transform if arg1 is a mask of some
11674 mode which allows further optimizations. */
11675 int pop = wi::popcount (warg1);
11676 if (!(pop >= BITS_PER_UNIT
11677 && exact_log2 (pop) != -1
11678 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11679 return fold_build2_loc (loc, code, type, op0,
11680 wide_int_to_tree (type, masked));
11684 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11685 ((A & N) + B) & M -> (A + B) & M
11686 Similarly if (N & M) == 0,
11687 ((A | N) + B) & M -> (A + B) & M
11688 and for - instead of + (or unary - instead of +)
11689 and/or ^ instead of |.
11690 If B is constant and (B & M) == 0, fold into A & M. */
11691 if (TREE_CODE (arg1) == INTEGER_CST)
11693 wide_int cst1 = arg1;
11694 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
11695 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11696 && (TREE_CODE (arg0) == PLUS_EXPR
11697 || TREE_CODE (arg0) == MINUS_EXPR
11698 || TREE_CODE (arg0) == NEGATE_EXPR)
11699 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11700 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11702 tree pmop[2];
11703 int which = 0;
11704 wide_int cst0;
11706 /* Now we know that arg0 is (C + D) or (C - D) or
11707 -C and arg1 (M) is == (1LL << cst) - 1.
11708 Store C into PMOP[0] and D into PMOP[1]. */
11709 pmop[0] = TREE_OPERAND (arg0, 0);
11710 pmop[1] = NULL;
11711 if (TREE_CODE (arg0) != NEGATE_EXPR)
11713 pmop[1] = TREE_OPERAND (arg0, 1);
11714 which = 1;
11717 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
11718 which = -1;
11720 for (; which >= 0; which--)
11721 switch (TREE_CODE (pmop[which]))
11723 case BIT_AND_EXPR:
11724 case BIT_IOR_EXPR:
11725 case BIT_XOR_EXPR:
11726 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11727 != INTEGER_CST)
11728 break;
11729 cst0 = TREE_OPERAND (pmop[which], 1);
11730 cst0 &= cst1;
11731 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11733 if (cst0 != cst1)
11734 break;
11736 else if (cst0 != 0)
11737 break;
11738 /* If C or D is of the form (A & N) where
11739 (N & M) == M, or of the form (A | N) or
11740 (A ^ N) where (N & M) == 0, replace it with A. */
11741 pmop[which] = TREE_OPERAND (pmop[which], 0);
11742 break;
11743 case INTEGER_CST:
11744 /* If C or D is a N where (N & M) == 0, it can be
11745 omitted (assumed 0). */
11746 if ((TREE_CODE (arg0) == PLUS_EXPR
11747 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11748 && (cst1 & pmop[which]) == 0)
11749 pmop[which] = NULL;
11750 break;
11751 default:
11752 break;
11755 /* Only build anything new if we optimized one or both arguments
11756 above. */
11757 if (pmop[0] != TREE_OPERAND (arg0, 0)
11758 || (TREE_CODE (arg0) != NEGATE_EXPR
11759 && pmop[1] != TREE_OPERAND (arg0, 1)))
11761 tree utype = TREE_TYPE (arg0);
11762 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11764 /* Perform the operations in a type that has defined
11765 overflow behavior. */
11766 utype = unsigned_type_for (TREE_TYPE (arg0));
11767 if (pmop[0] != NULL)
11768 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11769 if (pmop[1] != NULL)
11770 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11773 if (TREE_CODE (arg0) == NEGATE_EXPR)
11774 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11775 else if (TREE_CODE (arg0) == PLUS_EXPR)
11777 if (pmop[0] != NULL && pmop[1] != NULL)
11778 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11779 pmop[0], pmop[1]);
11780 else if (pmop[0] != NULL)
11781 tem = pmop[0];
11782 else if (pmop[1] != NULL)
11783 tem = pmop[1];
11784 else
11785 return build_int_cst (type, 0);
11787 else if (pmop[0] == NULL)
11788 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11789 else
11790 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11791 pmop[0], pmop[1]);
11792 /* TEM is now the new binary +, - or unary - replacement. */
11793 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11794 fold_convert_loc (loc, utype, arg1));
11795 return fold_convert_loc (loc, type, tem);
11800 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11801 if (t1 != NULL_TREE)
11802 return t1;
11803 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11804 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11805 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11807 prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11809 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
11810 if (mask == -1)
11811 return
11812 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11815 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11817 This results in more efficient code for machines without a NOR
11818 instruction. Combine will canonicalize to the first form
11819 which will allow use of NOR instructions provided by the
11820 backend if they exist. */
11821 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11822 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11824 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11825 build2 (BIT_IOR_EXPR, type,
11826 fold_convert_loc (loc, type,
11827 TREE_OPERAND (arg0, 0)),
11828 fold_convert_loc (loc, type,
11829 TREE_OPERAND (arg1, 0))));
11832 /* If arg0 is derived from the address of an object or function, we may
11833 be able to fold this expression using the object or function's
11834 alignment. */
11835 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && tree_fits_uhwi_p (arg1))
11837 unsigned HOST_WIDE_INT modulus, residue;
11838 unsigned HOST_WIDE_INT low = tree_to_uhwi (arg1);
11840 modulus = get_pointer_modulus_and_residue (arg0, &residue,
11841 integer_onep (arg1));
11843 /* This works because modulus is a power of 2. If this weren't the
11844 case, we'd have to replace it by its greatest power-of-2
11845 divisor: modulus & -modulus. */
11846 if (low < modulus)
11847 return build_int_cst (type, residue & low);
11850 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11851 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11852 if the new mask might be further optimized. */
11853 if ((TREE_CODE (arg0) == LSHIFT_EXPR
11854 || TREE_CODE (arg0) == RSHIFT_EXPR)
11855 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11856 && TREE_CODE (arg1) == INTEGER_CST
11857 && tree_fits_uhwi_p (TREE_OPERAND (arg0, 1))
11858 && tree_to_uhwi (TREE_OPERAND (arg0, 1)) > 0
11859 && (tree_to_uhwi (TREE_OPERAND (arg0, 1))
11860 < TYPE_PRECISION (TREE_TYPE (arg0))))
11862 unsigned int shiftc = tree_to_uhwi (TREE_OPERAND (arg0, 1));
11863 unsigned HOST_WIDE_INT mask = TREE_INT_CST_LOW (arg1);
11864 unsigned HOST_WIDE_INT newmask, zerobits = 0;
11865 tree shift_type = TREE_TYPE (arg0);
11867 if (TREE_CODE (arg0) == LSHIFT_EXPR)
11868 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11869 else if (TREE_CODE (arg0) == RSHIFT_EXPR
11870 && TYPE_PRECISION (TREE_TYPE (arg0))
11871 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg0))))
11873 prec = TYPE_PRECISION (TREE_TYPE (arg0));
11874 tree arg00 = TREE_OPERAND (arg0, 0);
11875 /* See if more bits can be proven as zero because of
11876 zero extension. */
11877 if (TREE_CODE (arg00) == NOP_EXPR
11878 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11880 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11881 if (TYPE_PRECISION (inner_type)
11882 == GET_MODE_PRECISION (TYPE_MODE (inner_type))
11883 && TYPE_PRECISION (inner_type) < prec)
11885 prec = TYPE_PRECISION (inner_type);
11886 /* See if we can shorten the right shift. */
11887 if (shiftc < prec)
11888 shift_type = inner_type;
11889 /* Otherwise X >> C1 is all zeros, so we'll optimize
11890 it into (X, 0) later on by making sure zerobits
11891 is all ones. */
11894 zerobits = ~(unsigned HOST_WIDE_INT) 0;
11895 if (shiftc < prec)
11897 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11898 zerobits <<= prec - shiftc;
11900 /* For arithmetic shift if sign bit could be set, zerobits
11901 can contain actually sign bits, so no transformation is
11902 possible, unless MASK masks them all away. In that
11903 case the shift needs to be converted into logical shift. */
11904 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11905 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11907 if ((mask & zerobits) == 0)
11908 shift_type = unsigned_type_for (TREE_TYPE (arg0));
11909 else
11910 zerobits = 0;
11914 /* ((X << 16) & 0xff00) is (X, 0). */
11915 if ((mask & zerobits) == mask)
11916 return omit_one_operand_loc (loc, type,
11917 build_int_cst (type, 0), arg0);
11919 newmask = mask | zerobits;
11920 if (newmask != mask && (newmask & (newmask + 1)) == 0)
11922 /* Only do the transformation if NEWMASK is some integer
11923 mode's mask. */
11924 for (prec = BITS_PER_UNIT;
11925 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11926 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11927 break;
11928 if (prec < HOST_BITS_PER_WIDE_INT
11929 || newmask == ~(unsigned HOST_WIDE_INT) 0)
11931 tree newmaskt;
11933 if (shift_type != TREE_TYPE (arg0))
11935 tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11936 fold_convert_loc (loc, shift_type,
11937 TREE_OPERAND (arg0, 0)),
11938 TREE_OPERAND (arg0, 1));
11939 tem = fold_convert_loc (loc, type, tem);
11941 else
11942 tem = op0;
11943 newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11944 if (!tree_int_cst_equal (newmaskt, arg1))
11945 return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11950 goto associate;
11952 case RDIV_EXPR:
11953 /* Don't touch a floating-point divide by zero unless the mode
11954 of the constant can represent infinity. */
11955 if (TREE_CODE (arg1) == REAL_CST
11956 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11957 && real_zerop (arg1))
11958 return NULL_TREE;
11960 /* Optimize A / A to 1.0 if we don't care about
11961 NaNs or Infinities. Skip the transformation
11962 for non-real operands. */
11963 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11964 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11965 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11966 && operand_equal_p (arg0, arg1, 0))
11968 tree r = build_real (TREE_TYPE (arg0), dconst1);
11970 return omit_two_operands_loc (loc, type, r, arg0, arg1);
11973 /* The complex version of the above A / A optimization. */
11974 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11975 && operand_equal_p (arg0, arg1, 0))
11977 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11978 if (! HONOR_NANS (TYPE_MODE (elem_type))
11979 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11981 tree r = build_real (elem_type, dconst1);
11982 /* omit_two_operands will call fold_convert for us. */
11983 return omit_two_operands_loc (loc, type, r, arg0, arg1);
11987 /* (-A) / (-B) -> A / B */
11988 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11989 return fold_build2_loc (loc, RDIV_EXPR, type,
11990 TREE_OPERAND (arg0, 0),
11991 negate_expr (arg1));
11992 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11993 return fold_build2_loc (loc, RDIV_EXPR, type,
11994 negate_expr (arg0),
11995 TREE_OPERAND (arg1, 0));
11997 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
11998 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11999 && real_onep (arg1))
12000 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12002 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
12003 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
12004 && real_minus_onep (arg1))
12005 return non_lvalue_loc (loc, fold_convert_loc (loc, type,
12006 negate_expr (arg0)));
12008 /* If ARG1 is a constant, we can convert this to a multiply by the
12009 reciprocal. This does not have the same rounding properties,
12010 so only do this if -freciprocal-math. We can actually
12011 always safely do it if ARG1 is a power of two, but it's hard to
12012 tell if it is or not in a portable manner. */
12013 if (optimize
12014 && (TREE_CODE (arg1) == REAL_CST
12015 || (TREE_CODE (arg1) == COMPLEX_CST
12016 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg1)))
12017 || (TREE_CODE (arg1) == VECTOR_CST
12018 && VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg1)))))
12020 if (flag_reciprocal_math
12021 && 0 != (tem = const_binop (code, build_one_cst (type), arg1)))
12022 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
12023 /* Find the reciprocal if optimizing and the result is exact.
12024 TODO: Complex reciprocal not implemented. */
12025 if (TREE_CODE (arg1) != COMPLEX_CST)
12027 tree inverse = exact_inverse (TREE_TYPE (arg0), arg1);
12029 if (inverse)
12030 return fold_build2_loc (loc, MULT_EXPR, type, arg0, inverse);
12033 /* Convert A/B/C to A/(B*C). */
12034 if (flag_reciprocal_math
12035 && TREE_CODE (arg0) == RDIV_EXPR)
12036 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
12037 fold_build2_loc (loc, MULT_EXPR, type,
12038 TREE_OPERAND (arg0, 1), arg1));
12040 /* Convert A/(B/C) to (A/B)*C. */
12041 if (flag_reciprocal_math
12042 && TREE_CODE (arg1) == RDIV_EXPR)
12043 return fold_build2_loc (loc, MULT_EXPR, type,
12044 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
12045 TREE_OPERAND (arg1, 0)),
12046 TREE_OPERAND (arg1, 1));
12048 /* Convert C1/(X*C2) into (C1/C2)/X. */
12049 if (flag_reciprocal_math
12050 && TREE_CODE (arg1) == MULT_EXPR
12051 && TREE_CODE (arg0) == REAL_CST
12052 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
12054 tree tem = const_binop (RDIV_EXPR, arg0,
12055 TREE_OPERAND (arg1, 1));
12056 if (tem)
12057 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
12058 TREE_OPERAND (arg1, 0));
12061 if (flag_unsafe_math_optimizations)
12063 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
12064 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
12066 /* Optimize sin(x)/cos(x) as tan(x). */
12067 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
12068 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
12069 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
12070 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
12071 CALL_EXPR_ARG (arg1, 0), 0))
12073 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
12075 if (tanfn != NULL_TREE)
12076 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
12079 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
12080 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
12081 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
12082 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
12083 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
12084 CALL_EXPR_ARG (arg1, 0), 0))
12086 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
12088 if (tanfn != NULL_TREE)
12090 tree tmp = build_call_expr_loc (loc, tanfn, 1,
12091 CALL_EXPR_ARG (arg0, 0));
12092 return fold_build2_loc (loc, RDIV_EXPR, type,
12093 build_real (type, dconst1), tmp);
12097 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
12098 NaNs or Infinities. */
12099 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
12100 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
12101 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
12103 tree arg00 = CALL_EXPR_ARG (arg0, 0);
12104 tree arg01 = CALL_EXPR_ARG (arg1, 0);
12106 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
12107 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
12108 && operand_equal_p (arg00, arg01, 0))
12110 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
12112 if (cosfn != NULL_TREE)
12113 return build_call_expr_loc (loc, cosfn, 1, arg00);
12117 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
12118 NaNs or Infinities. */
12119 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
12120 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
12121 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
12123 tree arg00 = CALL_EXPR_ARG (arg0, 0);
12124 tree arg01 = CALL_EXPR_ARG (arg1, 0);
12126 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
12127 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
12128 && operand_equal_p (arg00, arg01, 0))
12130 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
12132 if (cosfn != NULL_TREE)
12134 tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
12135 return fold_build2_loc (loc, RDIV_EXPR, type,
12136 build_real (type, dconst1),
12137 tmp);
12142 /* Optimize pow(x,c)/x as pow(x,c-1). */
12143 if (fcode0 == BUILT_IN_POW
12144 || fcode0 == BUILT_IN_POWF
12145 || fcode0 == BUILT_IN_POWL)
12147 tree arg00 = CALL_EXPR_ARG (arg0, 0);
12148 tree arg01 = CALL_EXPR_ARG (arg0, 1);
12149 if (TREE_CODE (arg01) == REAL_CST
12150 && !TREE_OVERFLOW (arg01)
12151 && operand_equal_p (arg1, arg00, 0))
12153 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
12154 REAL_VALUE_TYPE c;
12155 tree arg;
12157 c = TREE_REAL_CST (arg01);
12158 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
12159 arg = build_real (type, c);
12160 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
12164 /* Optimize a/root(b/c) into a*root(c/b). */
12165 if (BUILTIN_ROOT_P (fcode1))
12167 tree rootarg = CALL_EXPR_ARG (arg1, 0);
12169 if (TREE_CODE (rootarg) == RDIV_EXPR)
12171 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12172 tree b = TREE_OPERAND (rootarg, 0);
12173 tree c = TREE_OPERAND (rootarg, 1);
12175 tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
12177 tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
12178 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
12182 /* Optimize x/expN(y) into x*expN(-y). */
12183 if (BUILTIN_EXPONENT_P (fcode1))
12185 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12186 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
12187 arg1 = build_call_expr_loc (loc,
12188 expfn, 1,
12189 fold_convert_loc (loc, type, arg));
12190 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12193 /* Optimize x/pow(y,z) into x*pow(y,-z). */
12194 if (fcode1 == BUILT_IN_POW
12195 || fcode1 == BUILT_IN_POWF
12196 || fcode1 == BUILT_IN_POWL)
12198 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12199 tree arg10 = CALL_EXPR_ARG (arg1, 0);
12200 tree arg11 = CALL_EXPR_ARG (arg1, 1);
12201 tree neg11 = fold_convert_loc (loc, type,
12202 negate_expr (arg11));
12203 arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
12204 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12207 return NULL_TREE;
12209 case TRUNC_DIV_EXPR:
12210 /* Optimize (X & (-A)) / A where A is a power of 2,
12211 to X >> log2(A) */
12212 if (TREE_CODE (arg0) == BIT_AND_EXPR
12213 && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
12214 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
12216 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
12217 arg1, TREE_OPERAND (arg0, 1));
12218 if (sum && integer_zerop (sum)) {
12219 tree pow2 = build_int_cst (integer_type_node,
12220 wi::exact_log2 (arg1));
12221 return fold_build2_loc (loc, RSHIFT_EXPR, type,
12222 TREE_OPERAND (arg0, 0), pow2);
12226 /* Fall through */
12228 case FLOOR_DIV_EXPR:
12229 /* Simplify A / (B << N) where A and B are positive and B is
12230 a power of 2, to A >> (N + log2(B)). */
12231 strict_overflow_p = false;
12232 if (TREE_CODE (arg1) == LSHIFT_EXPR
12233 && (TYPE_UNSIGNED (type)
12234 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12236 tree sval = TREE_OPERAND (arg1, 0);
12237 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
12239 tree sh_cnt = TREE_OPERAND (arg1, 1);
12240 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
12241 wi::exact_log2 (sval));
12243 if (strict_overflow_p)
12244 fold_overflow_warning (("assuming signed overflow does not "
12245 "occur when simplifying A / (B << N)"),
12246 WARN_STRICT_OVERFLOW_MISC);
12248 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
12249 sh_cnt, pow2);
12250 return fold_build2_loc (loc, RSHIFT_EXPR, type,
12251 fold_convert_loc (loc, type, arg0), sh_cnt);
12255 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
12256 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
12257 if (INTEGRAL_TYPE_P (type)
12258 && TYPE_UNSIGNED (type)
12259 && code == FLOOR_DIV_EXPR)
12260 return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
12262 /* Fall through */
12264 case ROUND_DIV_EXPR:
12265 case CEIL_DIV_EXPR:
12266 case EXACT_DIV_EXPR:
12267 if (integer_onep (arg1))
12268 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12269 if (integer_zerop (arg1))
12270 return NULL_TREE;
12271 /* X / -1 is -X. */
12272 if (!TYPE_UNSIGNED (type)
12273 && TREE_CODE (arg1) == INTEGER_CST
12274 && wi::eq_p (arg1, -1))
12275 return fold_convert_loc (loc, type, negate_expr (arg0));
12277 /* Convert -A / -B to A / B when the type is signed and overflow is
12278 undefined. */
12279 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12280 && TREE_CODE (arg0) == NEGATE_EXPR
12281 && negate_expr_p (arg1))
12283 if (INTEGRAL_TYPE_P (type))
12284 fold_overflow_warning (("assuming signed overflow does not occur "
12285 "when distributing negation across "
12286 "division"),
12287 WARN_STRICT_OVERFLOW_MISC);
12288 return fold_build2_loc (loc, code, type,
12289 fold_convert_loc (loc, type,
12290 TREE_OPERAND (arg0, 0)),
12291 fold_convert_loc (loc, type,
12292 negate_expr (arg1)));
12294 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12295 && TREE_CODE (arg1) == NEGATE_EXPR
12296 && negate_expr_p (arg0))
12298 if (INTEGRAL_TYPE_P (type))
12299 fold_overflow_warning (("assuming signed overflow does not occur "
12300 "when distributing negation across "
12301 "division"),
12302 WARN_STRICT_OVERFLOW_MISC);
12303 return fold_build2_loc (loc, code, type,
12304 fold_convert_loc (loc, type,
12305 negate_expr (arg0)),
12306 fold_convert_loc (loc, type,
12307 TREE_OPERAND (arg1, 0)));
12310 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12311 operation, EXACT_DIV_EXPR.
12313 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12314 At one time others generated faster code, it's not clear if they do
12315 after the last round to changes to the DIV code in expmed.c. */
12316 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12317 && multiple_of_p (type, arg0, arg1))
12318 return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
12320 strict_overflow_p = false;
12321 if (TREE_CODE (arg1) == INTEGER_CST
12322 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12323 &strict_overflow_p)))
12325 if (strict_overflow_p)
12326 fold_overflow_warning (("assuming signed overflow does not occur "
12327 "when simplifying division"),
12328 WARN_STRICT_OVERFLOW_MISC);
12329 return fold_convert_loc (loc, type, tem);
12332 return NULL_TREE;
12334 case CEIL_MOD_EXPR:
12335 case FLOOR_MOD_EXPR:
12336 case ROUND_MOD_EXPR:
12337 case TRUNC_MOD_EXPR:
12338 /* X % 1 is always zero, but be sure to preserve any side
12339 effects in X. */
12340 if (integer_onep (arg1))
12341 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12343 /* X % 0, return X % 0 unchanged so that we can get the
12344 proper warnings and errors. */
12345 if (integer_zerop (arg1))
12346 return NULL_TREE;
12348 /* 0 % X is always zero, but be sure to preserve any side
12349 effects in X. Place this after checking for X == 0. */
12350 if (integer_zerop (arg0))
12351 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12353 /* X % -1 is zero. */
12354 if (!TYPE_UNSIGNED (type)
12355 && TREE_CODE (arg1) == INTEGER_CST
12356 && wi::eq_p (arg1, -1))
12357 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12359 /* X % -C is the same as X % C. */
12360 if (code == TRUNC_MOD_EXPR
12361 && TYPE_SIGN (type) == SIGNED
12362 && TREE_CODE (arg1) == INTEGER_CST
12363 && !TREE_OVERFLOW (arg1)
12364 && wi::neg_p (arg1)
12365 && !TYPE_OVERFLOW_TRAPS (type)
12366 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
12367 && !sign_bit_p (arg1, arg1))
12368 return fold_build2_loc (loc, code, type,
12369 fold_convert_loc (loc, type, arg0),
12370 fold_convert_loc (loc, type,
12371 negate_expr (arg1)));
12373 /* X % -Y is the same as X % Y. */
12374 if (code == TRUNC_MOD_EXPR
12375 && !TYPE_UNSIGNED (type)
12376 && TREE_CODE (arg1) == NEGATE_EXPR
12377 && !TYPE_OVERFLOW_TRAPS (type))
12378 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
12379 fold_convert_loc (loc, type,
12380 TREE_OPERAND (arg1, 0)));
12382 strict_overflow_p = false;
12383 if (TREE_CODE (arg1) == INTEGER_CST
12384 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12385 &strict_overflow_p)))
12387 if (strict_overflow_p)
12388 fold_overflow_warning (("assuming signed overflow does not occur "
12389 "when simplifying modulus"),
12390 WARN_STRICT_OVERFLOW_MISC);
12391 return fold_convert_loc (loc, type, tem);
12394 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
12395 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
12396 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
12397 && (TYPE_UNSIGNED (type)
12398 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12400 tree c = arg1;
12401 /* Also optimize A % (C << N) where C is a power of 2,
12402 to A & ((C << N) - 1). */
12403 if (TREE_CODE (arg1) == LSHIFT_EXPR)
12404 c = TREE_OPERAND (arg1, 0);
12406 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
12408 tree mask
12409 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
12410 build_int_cst (TREE_TYPE (arg1), 1));
12411 if (strict_overflow_p)
12412 fold_overflow_warning (("assuming signed overflow does not "
12413 "occur when simplifying "
12414 "X % (power of two)"),
12415 WARN_STRICT_OVERFLOW_MISC);
12416 return fold_build2_loc (loc, BIT_AND_EXPR, type,
12417 fold_convert_loc (loc, type, arg0),
12418 fold_convert_loc (loc, type, mask));
12422 return NULL_TREE;
12424 case LROTATE_EXPR:
12425 case RROTATE_EXPR:
12426 if (integer_all_onesp (arg0))
12427 return omit_one_operand_loc (loc, type, arg0, arg1);
12428 goto shift;
12430 case RSHIFT_EXPR:
12431 /* Optimize -1 >> x for arithmetic right shifts. */
12432 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
12433 && tree_expr_nonnegative_p (arg1))
12434 return omit_one_operand_loc (loc, type, arg0, arg1);
12435 /* ... fall through ... */
12437 case LSHIFT_EXPR:
12438 shift:
12439 if (integer_zerop (arg1))
12440 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12441 if (integer_zerop (arg0))
12442 return omit_one_operand_loc (loc, type, arg0, arg1);
12444 /* Prefer vector1 << scalar to vector1 << vector2
12445 if vector2 is uniform. */
12446 if (VECTOR_TYPE_P (TREE_TYPE (arg1))
12447 && (tem = uniform_vector_p (arg1)) != NULL_TREE)
12448 return fold_build2_loc (loc, code, type, op0, tem);
12450 /* Since negative shift count is not well-defined,
12451 don't try to compute it in the compiler. */
12452 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12453 return NULL_TREE;
12455 prec = element_precision (type);
12457 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
12458 if (TREE_CODE (op0) == code && tree_fits_uhwi_p (arg1)
12459 && tree_to_uhwi (arg1) < prec
12460 && tree_fits_uhwi_p (TREE_OPERAND (arg0, 1))
12461 && tree_to_uhwi (TREE_OPERAND (arg0, 1)) < prec)
12463 unsigned int low = (tree_to_uhwi (TREE_OPERAND (arg0, 1))
12464 + tree_to_uhwi (arg1));
12466 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
12467 being well defined. */
12468 if (low >= prec)
12470 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
12471 low = low % prec;
12472 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
12473 return omit_one_operand_loc (loc, type, build_zero_cst (type),
12474 TREE_OPERAND (arg0, 0));
12475 else
12476 low = prec - 1;
12479 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12480 build_int_cst (TREE_TYPE (arg1), low));
12483 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
12484 into x & ((unsigned)-1 >> c) for unsigned types. */
12485 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
12486 || (TYPE_UNSIGNED (type)
12487 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
12488 && tree_fits_uhwi_p (arg1)
12489 && tree_to_uhwi (arg1) < prec
12490 && tree_fits_uhwi_p (TREE_OPERAND (arg0, 1))
12491 && tree_to_uhwi (TREE_OPERAND (arg0, 1)) < prec)
12493 HOST_WIDE_INT low0 = tree_to_uhwi (TREE_OPERAND (arg0, 1));
12494 HOST_WIDE_INT low1 = tree_to_uhwi (arg1);
12495 tree lshift;
12496 tree arg00;
12498 if (low0 == low1)
12500 arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12502 lshift = build_minus_one_cst (type);
12503 lshift = const_binop (code, lshift, arg1);
12505 return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
12509 /* Rewrite an LROTATE_EXPR by a constant into an
12510 RROTATE_EXPR by a new constant. */
12511 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
12513 tree tem = build_int_cst (TREE_TYPE (arg1), prec);
12514 tem = const_binop (MINUS_EXPR, tem, arg1);
12515 return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
12518 /* If we have a rotate of a bit operation with the rotate count and
12519 the second operand of the bit operation both constant,
12520 permute the two operations. */
12521 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12522 && (TREE_CODE (arg0) == BIT_AND_EXPR
12523 || TREE_CODE (arg0) == BIT_IOR_EXPR
12524 || TREE_CODE (arg0) == BIT_XOR_EXPR)
12525 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12526 return fold_build2_loc (loc, TREE_CODE (arg0), type,
12527 fold_build2_loc (loc, code, type,
12528 TREE_OPERAND (arg0, 0), arg1),
12529 fold_build2_loc (loc, code, type,
12530 TREE_OPERAND (arg0, 1), arg1));
12532 /* Two consecutive rotates adding up to the some integer
12533 multiple of the precision of the type can be ignored. */
12534 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12535 && TREE_CODE (arg0) == RROTATE_EXPR
12536 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12537 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
12538 prec) == 0)
12539 return TREE_OPERAND (arg0, 0);
12541 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
12542 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
12543 if the latter can be further optimized. */
12544 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
12545 && TREE_CODE (arg0) == BIT_AND_EXPR
12546 && TREE_CODE (arg1) == INTEGER_CST
12547 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12549 tree mask = fold_build2_loc (loc, code, type,
12550 fold_convert_loc (loc, type,
12551 TREE_OPERAND (arg0, 1)),
12552 arg1);
12553 tree shift = fold_build2_loc (loc, code, type,
12554 fold_convert_loc (loc, type,
12555 TREE_OPERAND (arg0, 0)),
12556 arg1);
12557 tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
12558 if (tem)
12559 return tem;
12562 return NULL_TREE;
12564 case MIN_EXPR:
12565 if (operand_equal_p (arg0, arg1, 0))
12566 return omit_one_operand_loc (loc, type, arg0, arg1);
12567 if (INTEGRAL_TYPE_P (type)
12568 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
12569 return omit_one_operand_loc (loc, type, arg1, arg0);
12570 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
12571 if (tem)
12572 return tem;
12573 goto associate;
12575 case MAX_EXPR:
12576 if (operand_equal_p (arg0, arg1, 0))
12577 return omit_one_operand_loc (loc, type, arg0, arg1);
12578 if (INTEGRAL_TYPE_P (type)
12579 && TYPE_MAX_VALUE (type)
12580 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
12581 return omit_one_operand_loc (loc, type, arg1, arg0);
12582 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
12583 if (tem)
12584 return tem;
12585 goto associate;
12587 case TRUTH_ANDIF_EXPR:
12588 /* Note that the operands of this must be ints
12589 and their values must be 0 or 1.
12590 ("true" is a fixed value perhaps depending on the language.) */
12591 /* If first arg is constant zero, return it. */
12592 if (integer_zerop (arg0))
12593 return fold_convert_loc (loc, type, arg0);
12594 case TRUTH_AND_EXPR:
12595 /* If either arg is constant true, drop it. */
12596 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12597 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12598 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12599 /* Preserve sequence points. */
12600 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12601 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12602 /* If second arg is constant zero, result is zero, but first arg
12603 must be evaluated. */
12604 if (integer_zerop (arg1))
12605 return omit_one_operand_loc (loc, type, arg1, arg0);
12606 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12607 case will be handled here. */
12608 if (integer_zerop (arg0))
12609 return omit_one_operand_loc (loc, type, arg0, arg1);
12611 /* !X && X is always false. */
12612 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12613 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12614 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12615 /* X && !X is always false. */
12616 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12617 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12618 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12620 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12621 means A >= Y && A != MAX, but in this case we know that
12622 A < X <= MAX. */
12624 if (!TREE_SIDE_EFFECTS (arg0)
12625 && !TREE_SIDE_EFFECTS (arg1))
12627 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12628 if (tem && !operand_equal_p (tem, arg0, 0))
12629 return fold_build2_loc (loc, code, type, tem, arg1);
12631 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12632 if (tem && !operand_equal_p (tem, arg1, 0))
12633 return fold_build2_loc (loc, code, type, arg0, tem);
12636 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12637 != NULL_TREE)
12638 return tem;
12640 return NULL_TREE;
12642 case TRUTH_ORIF_EXPR:
12643 /* Note that the operands of this must be ints
12644 and their values must be 0 or true.
12645 ("true" is a fixed value perhaps depending on the language.) */
12646 /* If first arg is constant true, return it. */
12647 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12648 return fold_convert_loc (loc, type, arg0);
12649 case TRUTH_OR_EXPR:
12650 /* If either arg is constant zero, drop it. */
12651 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12652 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12653 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12654 /* Preserve sequence points. */
12655 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12656 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12657 /* If second arg is constant true, result is true, but we must
12658 evaluate first arg. */
12659 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12660 return omit_one_operand_loc (loc, type, arg1, arg0);
12661 /* Likewise for first arg, but note this only occurs here for
12662 TRUTH_OR_EXPR. */
12663 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12664 return omit_one_operand_loc (loc, type, arg0, arg1);
12666 /* !X || X is always true. */
12667 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12668 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12669 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12670 /* X || !X is always true. */
12671 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12672 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12673 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12675 /* (X && !Y) || (!X && Y) is X ^ Y */
12676 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12677 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12679 tree a0, a1, l0, l1, n0, n1;
12681 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12682 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12684 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12685 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12687 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12688 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12690 if ((operand_equal_p (n0, a0, 0)
12691 && operand_equal_p (n1, a1, 0))
12692 || (operand_equal_p (n0, a1, 0)
12693 && operand_equal_p (n1, a0, 0)))
12694 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12697 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12698 != NULL_TREE)
12699 return tem;
12701 return NULL_TREE;
12703 case TRUTH_XOR_EXPR:
12704 /* If the second arg is constant zero, drop it. */
12705 if (integer_zerop (arg1))
12706 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12707 /* If the second arg is constant true, this is a logical inversion. */
12708 if (integer_onep (arg1))
12710 tem = invert_truthvalue_loc (loc, arg0);
12711 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12713 /* Identical arguments cancel to zero. */
12714 if (operand_equal_p (arg0, arg1, 0))
12715 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12717 /* !X ^ X is always true. */
12718 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12719 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12720 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12722 /* X ^ !X is always true. */
12723 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12724 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12725 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12727 return NULL_TREE;
12729 case EQ_EXPR:
12730 case NE_EXPR:
12731 STRIP_NOPS (arg0);
12732 STRIP_NOPS (arg1);
12734 tem = fold_comparison (loc, code, type, op0, op1);
12735 if (tem != NULL_TREE)
12736 return tem;
12738 /* bool_var != 0 becomes bool_var. */
12739 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12740 && code == NE_EXPR)
12741 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12743 /* bool_var == 1 becomes bool_var. */
12744 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12745 && code == EQ_EXPR)
12746 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12748 /* bool_var != 1 becomes !bool_var. */
12749 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12750 && code == NE_EXPR)
12751 return fold_convert_loc (loc, type,
12752 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12753 TREE_TYPE (arg0), arg0));
12755 /* bool_var == 0 becomes !bool_var. */
12756 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12757 && code == EQ_EXPR)
12758 return fold_convert_loc (loc, type,
12759 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12760 TREE_TYPE (arg0), arg0));
12762 /* !exp != 0 becomes !exp */
12763 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12764 && code == NE_EXPR)
12765 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12767 /* If this is an equality comparison of the address of two non-weak,
12768 unaliased symbols neither of which are extern (since we do not
12769 have access to attributes for externs), then we know the result. */
12770 if (TREE_CODE (arg0) == ADDR_EXPR
12771 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12772 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12773 && ! lookup_attribute ("alias",
12774 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12775 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12776 && TREE_CODE (arg1) == ADDR_EXPR
12777 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12778 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12779 && ! lookup_attribute ("alias",
12780 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12781 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12783 /* We know that we're looking at the address of two
12784 non-weak, unaliased, static _DECL nodes.
12786 It is both wasteful and incorrect to call operand_equal_p
12787 to compare the two ADDR_EXPR nodes. It is wasteful in that
12788 all we need to do is test pointer equality for the arguments
12789 to the two ADDR_EXPR nodes. It is incorrect to use
12790 operand_equal_p as that function is NOT equivalent to a
12791 C equality test. It can in fact return false for two
12792 objects which would test as equal using the C equality
12793 operator. */
12794 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12795 return constant_boolean_node (equal
12796 ? code == EQ_EXPR : code != EQ_EXPR,
12797 type);
12800 /* Similarly for a NEGATE_EXPR. */
12801 if (TREE_CODE (arg0) == NEGATE_EXPR
12802 && TREE_CODE (arg1) == INTEGER_CST
12803 && 0 != (tem = negate_expr (fold_convert_loc (loc, TREE_TYPE (arg0),
12804 arg1)))
12805 && TREE_CODE (tem) == INTEGER_CST
12806 && !TREE_OVERFLOW (tem))
12807 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12809 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12810 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12811 && TREE_CODE (arg1) == INTEGER_CST
12812 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12813 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12814 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12815 fold_convert_loc (loc,
12816 TREE_TYPE (arg0),
12817 arg1),
12818 TREE_OPERAND (arg0, 1)));
12820 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
12821 if ((TREE_CODE (arg0) == PLUS_EXPR
12822 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12823 || TREE_CODE (arg0) == MINUS_EXPR)
12824 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12825 0)),
12826 arg1, 0)
12827 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12828 || POINTER_TYPE_P (TREE_TYPE (arg0))))
12830 tree val = TREE_OPERAND (arg0, 1);
12831 return omit_two_operands_loc (loc, type,
12832 fold_build2_loc (loc, code, type,
12833 val,
12834 build_int_cst (TREE_TYPE (val),
12835 0)),
12836 TREE_OPERAND (arg0, 0), arg1);
12839 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
12840 if (TREE_CODE (arg0) == MINUS_EXPR
12841 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12842 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12843 1)),
12844 arg1, 0)
12845 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
12847 return omit_two_operands_loc (loc, type,
12848 code == NE_EXPR
12849 ? boolean_true_node : boolean_false_node,
12850 TREE_OPERAND (arg0, 1), arg1);
12853 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12854 if (TREE_CODE (arg0) == ABS_EXPR
12855 && (integer_zerop (arg1) || real_zerop (arg1)))
12856 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12858 /* If this is an EQ or NE comparison with zero and ARG0 is
12859 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12860 two operations, but the latter can be done in one less insn
12861 on machines that have only two-operand insns or on which a
12862 constant cannot be the first operand. */
12863 if (TREE_CODE (arg0) == BIT_AND_EXPR
12864 && integer_zerop (arg1))
12866 tree arg00 = TREE_OPERAND (arg0, 0);
12867 tree arg01 = TREE_OPERAND (arg0, 1);
12868 if (TREE_CODE (arg00) == LSHIFT_EXPR
12869 && integer_onep (TREE_OPERAND (arg00, 0)))
12871 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12872 arg01, TREE_OPERAND (arg00, 1));
12873 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12874 build_int_cst (TREE_TYPE (arg0), 1));
12875 return fold_build2_loc (loc, code, type,
12876 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12877 arg1);
12879 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12880 && integer_onep (TREE_OPERAND (arg01, 0)))
12882 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12883 arg00, TREE_OPERAND (arg01, 1));
12884 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12885 build_int_cst (TREE_TYPE (arg0), 1));
12886 return fold_build2_loc (loc, code, type,
12887 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12888 arg1);
12892 /* If this is an NE or EQ comparison of zero against the result of a
12893 signed MOD operation whose second operand is a power of 2, make
12894 the MOD operation unsigned since it is simpler and equivalent. */
12895 if (integer_zerop (arg1)
12896 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12897 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12898 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12899 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12900 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12901 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12903 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12904 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12905 fold_convert_loc (loc, newtype,
12906 TREE_OPERAND (arg0, 0)),
12907 fold_convert_loc (loc, newtype,
12908 TREE_OPERAND (arg0, 1)));
12910 return fold_build2_loc (loc, code, type, newmod,
12911 fold_convert_loc (loc, newtype, arg1));
12914 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12915 C1 is a valid shift constant, and C2 is a power of two, i.e.
12916 a single bit. */
12917 if (TREE_CODE (arg0) == BIT_AND_EXPR
12918 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12919 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12920 == INTEGER_CST
12921 && integer_pow2p (TREE_OPERAND (arg0, 1))
12922 && integer_zerop (arg1))
12924 tree itype = TREE_TYPE (arg0);
12925 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12926 prec = TYPE_PRECISION (itype);
12928 /* Check for a valid shift count. */
12929 if (wi::ltu_p (arg001, prec))
12931 tree arg01 = TREE_OPERAND (arg0, 1);
12932 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12933 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12934 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12935 can be rewritten as (X & (C2 << C1)) != 0. */
12936 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12938 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12939 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12940 return fold_build2_loc (loc, code, type, tem,
12941 fold_convert_loc (loc, itype, arg1));
12943 /* Otherwise, for signed (arithmetic) shifts,
12944 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12945 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12946 else if (!TYPE_UNSIGNED (itype))
12947 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12948 arg000, build_int_cst (itype, 0));
12949 /* Otherwise, of unsigned (logical) shifts,
12950 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12951 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12952 else
12953 return omit_one_operand_loc (loc, type,
12954 code == EQ_EXPR ? integer_one_node
12955 : integer_zero_node,
12956 arg000);
12960 /* If we have (A & C) == C where C is a power of 2, convert this into
12961 (A & C) != 0. Similarly for NE_EXPR. */
12962 if (TREE_CODE (arg0) == BIT_AND_EXPR
12963 && integer_pow2p (TREE_OPERAND (arg0, 1))
12964 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12965 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12966 arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12967 integer_zero_node));
12969 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12970 bit, then fold the expression into A < 0 or A >= 0. */
12971 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12972 if (tem)
12973 return tem;
12975 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12976 Similarly for NE_EXPR. */
12977 if (TREE_CODE (arg0) == BIT_AND_EXPR
12978 && TREE_CODE (arg1) == INTEGER_CST
12979 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12981 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12982 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12983 TREE_OPERAND (arg0, 1));
12984 tree dandnotc
12985 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12986 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
12987 notc);
12988 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12989 if (integer_nonzerop (dandnotc))
12990 return omit_one_operand_loc (loc, type, rslt, arg0);
12993 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12994 Similarly for NE_EXPR. */
12995 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12996 && TREE_CODE (arg1) == INTEGER_CST
12997 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12999 tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
13000 tree candnotd
13001 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
13002 TREE_OPERAND (arg0, 1),
13003 fold_convert_loc (loc, TREE_TYPE (arg0), notd));
13004 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
13005 if (integer_nonzerop (candnotd))
13006 return omit_one_operand_loc (loc, type, rslt, arg0);
13009 /* If this is a comparison of a field, we may be able to simplify it. */
13010 if ((TREE_CODE (arg0) == COMPONENT_REF
13011 || TREE_CODE (arg0) == BIT_FIELD_REF)
13012 /* Handle the constant case even without -O
13013 to make sure the warnings are given. */
13014 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
13016 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
13017 if (t1)
13018 return t1;
13021 /* Optimize comparisons of strlen vs zero to a compare of the
13022 first character of the string vs zero. To wit,
13023 strlen(ptr) == 0 => *ptr == 0
13024 strlen(ptr) != 0 => *ptr != 0
13025 Other cases should reduce to one of these two (or a constant)
13026 due to the return value of strlen being unsigned. */
13027 if (TREE_CODE (arg0) == CALL_EXPR
13028 && integer_zerop (arg1))
13030 tree fndecl = get_callee_fndecl (arg0);
13032 if (fndecl
13033 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
13034 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
13035 && call_expr_nargs (arg0) == 1
13036 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
13038 tree iref = build_fold_indirect_ref_loc (loc,
13039 CALL_EXPR_ARG (arg0, 0));
13040 return fold_build2_loc (loc, code, type, iref,
13041 build_int_cst (TREE_TYPE (iref), 0));
13045 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
13046 of X. Similarly fold (X >> C) == 0 into X >= 0. */
13047 if (TREE_CODE (arg0) == RSHIFT_EXPR
13048 && integer_zerop (arg1)
13049 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
13051 tree arg00 = TREE_OPERAND (arg0, 0);
13052 tree arg01 = TREE_OPERAND (arg0, 1);
13053 tree itype = TREE_TYPE (arg00);
13054 if (wi::eq_p (arg01, TYPE_PRECISION (itype) - 1))
13056 if (TYPE_UNSIGNED (itype))
13058 itype = signed_type_for (itype);
13059 arg00 = fold_convert_loc (loc, itype, arg00);
13061 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
13062 type, arg00, build_zero_cst (itype));
13066 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
13067 if (integer_zerop (arg1)
13068 && TREE_CODE (arg0) == BIT_XOR_EXPR)
13069 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
13070 TREE_OPERAND (arg0, 1));
13072 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
13073 if (TREE_CODE (arg0) == BIT_XOR_EXPR
13074 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
13075 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
13076 build_zero_cst (TREE_TYPE (arg0)));
13077 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
13078 if (TREE_CODE (arg0) == BIT_XOR_EXPR
13079 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
13080 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
13081 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
13082 build_zero_cst (TREE_TYPE (arg0)));
13084 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
13085 if (TREE_CODE (arg0) == BIT_XOR_EXPR
13086 && TREE_CODE (arg1) == INTEGER_CST
13087 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
13088 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
13089 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
13090 TREE_OPERAND (arg0, 1), arg1));
13092 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
13093 (X & C) == 0 when C is a single bit. */
13094 if (TREE_CODE (arg0) == BIT_AND_EXPR
13095 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
13096 && integer_zerop (arg1)
13097 && integer_pow2p (TREE_OPERAND (arg0, 1)))
13099 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
13100 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
13101 TREE_OPERAND (arg0, 1));
13102 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
13103 type, tem,
13104 fold_convert_loc (loc, TREE_TYPE (arg0),
13105 arg1));
13108 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
13109 constant C is a power of two, i.e. a single bit. */
13110 if (TREE_CODE (arg0) == BIT_XOR_EXPR
13111 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13112 && integer_zerop (arg1)
13113 && integer_pow2p (TREE_OPERAND (arg0, 1))
13114 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13115 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
13117 tree arg00 = TREE_OPERAND (arg0, 0);
13118 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
13119 arg00, build_int_cst (TREE_TYPE (arg00), 0));
13122 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
13123 when is C is a power of two, i.e. a single bit. */
13124 if (TREE_CODE (arg0) == BIT_AND_EXPR
13125 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
13126 && integer_zerop (arg1)
13127 && integer_pow2p (TREE_OPERAND (arg0, 1))
13128 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13129 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
13131 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
13132 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
13133 arg000, TREE_OPERAND (arg0, 1));
13134 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
13135 tem, build_int_cst (TREE_TYPE (tem), 0));
13138 if (integer_zerop (arg1)
13139 && tree_expr_nonzero_p (arg0))
13141 tree res = constant_boolean_node (code==NE_EXPR, type);
13142 return omit_one_operand_loc (loc, type, res, arg0);
13145 /* Fold -X op -Y as X op Y, where op is eq/ne. */
13146 if (TREE_CODE (arg0) == NEGATE_EXPR
13147 && TREE_CODE (arg1) == NEGATE_EXPR)
13148 return fold_build2_loc (loc, code, type,
13149 TREE_OPERAND (arg0, 0),
13150 fold_convert_loc (loc, TREE_TYPE (arg0),
13151 TREE_OPERAND (arg1, 0)));
13153 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
13154 if (TREE_CODE (arg0) == BIT_AND_EXPR
13155 && TREE_CODE (arg1) == BIT_AND_EXPR)
13157 tree arg00 = TREE_OPERAND (arg0, 0);
13158 tree arg01 = TREE_OPERAND (arg0, 1);
13159 tree arg10 = TREE_OPERAND (arg1, 0);
13160 tree arg11 = TREE_OPERAND (arg1, 1);
13161 tree itype = TREE_TYPE (arg0);
13163 if (operand_equal_p (arg01, arg11, 0))
13164 return fold_build2_loc (loc, code, type,
13165 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13166 fold_build2_loc (loc,
13167 BIT_XOR_EXPR, itype,
13168 arg00, arg10),
13169 arg01),
13170 build_zero_cst (itype));
13172 if (operand_equal_p (arg01, arg10, 0))
13173 return fold_build2_loc (loc, code, type,
13174 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13175 fold_build2_loc (loc,
13176 BIT_XOR_EXPR, itype,
13177 arg00, arg11),
13178 arg01),
13179 build_zero_cst (itype));
13181 if (operand_equal_p (arg00, arg11, 0))
13182 return fold_build2_loc (loc, code, type,
13183 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13184 fold_build2_loc (loc,
13185 BIT_XOR_EXPR, itype,
13186 arg01, arg10),
13187 arg00),
13188 build_zero_cst (itype));
13190 if (operand_equal_p (arg00, arg10, 0))
13191 return fold_build2_loc (loc, code, type,
13192 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13193 fold_build2_loc (loc,
13194 BIT_XOR_EXPR, itype,
13195 arg01, arg11),
13196 arg00),
13197 build_zero_cst (itype));
13200 if (TREE_CODE (arg0) == BIT_XOR_EXPR
13201 && TREE_CODE (arg1) == BIT_XOR_EXPR)
13203 tree arg00 = TREE_OPERAND (arg0, 0);
13204 tree arg01 = TREE_OPERAND (arg0, 1);
13205 tree arg10 = TREE_OPERAND (arg1, 0);
13206 tree arg11 = TREE_OPERAND (arg1, 1);
13207 tree itype = TREE_TYPE (arg0);
13209 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
13210 operand_equal_p guarantees no side-effects so we don't need
13211 to use omit_one_operand on Z. */
13212 if (operand_equal_p (arg01, arg11, 0))
13213 return fold_build2_loc (loc, code, type, arg00,
13214 fold_convert_loc (loc, TREE_TYPE (arg00),
13215 arg10));
13216 if (operand_equal_p (arg01, arg10, 0))
13217 return fold_build2_loc (loc, code, type, arg00,
13218 fold_convert_loc (loc, TREE_TYPE (arg00),
13219 arg11));
13220 if (operand_equal_p (arg00, arg11, 0))
13221 return fold_build2_loc (loc, code, type, arg01,
13222 fold_convert_loc (loc, TREE_TYPE (arg01),
13223 arg10));
13224 if (operand_equal_p (arg00, arg10, 0))
13225 return fold_build2_loc (loc, code, type, arg01,
13226 fold_convert_loc (loc, TREE_TYPE (arg01),
13227 arg11));
13229 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
13230 if (TREE_CODE (arg01) == INTEGER_CST
13231 && TREE_CODE (arg11) == INTEGER_CST)
13233 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
13234 fold_convert_loc (loc, itype, arg11));
13235 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
13236 return fold_build2_loc (loc, code, type, tem,
13237 fold_convert_loc (loc, itype, arg10));
13241 /* Attempt to simplify equality/inequality comparisons of complex
13242 values. Only lower the comparison if the result is known or
13243 can be simplified to a single scalar comparison. */
13244 if ((TREE_CODE (arg0) == COMPLEX_EXPR
13245 || TREE_CODE (arg0) == COMPLEX_CST)
13246 && (TREE_CODE (arg1) == COMPLEX_EXPR
13247 || TREE_CODE (arg1) == COMPLEX_CST))
13249 tree real0, imag0, real1, imag1;
13250 tree rcond, icond;
13252 if (TREE_CODE (arg0) == COMPLEX_EXPR)
13254 real0 = TREE_OPERAND (arg0, 0);
13255 imag0 = TREE_OPERAND (arg0, 1);
13257 else
13259 real0 = TREE_REALPART (arg0);
13260 imag0 = TREE_IMAGPART (arg0);
13263 if (TREE_CODE (arg1) == COMPLEX_EXPR)
13265 real1 = TREE_OPERAND (arg1, 0);
13266 imag1 = TREE_OPERAND (arg1, 1);
13268 else
13270 real1 = TREE_REALPART (arg1);
13271 imag1 = TREE_IMAGPART (arg1);
13274 rcond = fold_binary_loc (loc, code, type, real0, real1);
13275 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
13277 if (integer_zerop (rcond))
13279 if (code == EQ_EXPR)
13280 return omit_two_operands_loc (loc, type, boolean_false_node,
13281 imag0, imag1);
13282 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
13284 else
13286 if (code == NE_EXPR)
13287 return omit_two_operands_loc (loc, type, boolean_true_node,
13288 imag0, imag1);
13289 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
13293 icond = fold_binary_loc (loc, code, type, imag0, imag1);
13294 if (icond && TREE_CODE (icond) == INTEGER_CST)
13296 if (integer_zerop (icond))
13298 if (code == EQ_EXPR)
13299 return omit_two_operands_loc (loc, type, boolean_false_node,
13300 real0, real1);
13301 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
13303 else
13305 if (code == NE_EXPR)
13306 return omit_two_operands_loc (loc, type, boolean_true_node,
13307 real0, real1);
13308 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
13313 return NULL_TREE;
13315 case LT_EXPR:
13316 case GT_EXPR:
13317 case LE_EXPR:
13318 case GE_EXPR:
13319 tem = fold_comparison (loc, code, type, op0, op1);
13320 if (tem != NULL_TREE)
13321 return tem;
13323 /* Transform comparisons of the form X +- C CMP X. */
13324 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
13325 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
13326 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
13327 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
13328 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
13329 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
13331 tree arg01 = TREE_OPERAND (arg0, 1);
13332 enum tree_code code0 = TREE_CODE (arg0);
13333 int is_positive;
13335 if (TREE_CODE (arg01) == REAL_CST)
13336 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
13337 else
13338 is_positive = tree_int_cst_sgn (arg01);
13340 /* (X - c) > X becomes false. */
13341 if (code == GT_EXPR
13342 && ((code0 == MINUS_EXPR && is_positive >= 0)
13343 || (code0 == PLUS_EXPR && is_positive <= 0)))
13345 if (TREE_CODE (arg01) == INTEGER_CST
13346 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13347 fold_overflow_warning (("assuming signed overflow does not "
13348 "occur when assuming that (X - c) > X "
13349 "is always false"),
13350 WARN_STRICT_OVERFLOW_ALL);
13351 return constant_boolean_node (0, type);
13354 /* Likewise (X + c) < X becomes false. */
13355 if (code == LT_EXPR
13356 && ((code0 == PLUS_EXPR && is_positive >= 0)
13357 || (code0 == MINUS_EXPR && is_positive <= 0)))
13359 if (TREE_CODE (arg01) == INTEGER_CST
13360 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13361 fold_overflow_warning (("assuming signed overflow does not "
13362 "occur when assuming that "
13363 "(X + c) < X is always false"),
13364 WARN_STRICT_OVERFLOW_ALL);
13365 return constant_boolean_node (0, type);
13368 /* Convert (X - c) <= X to true. */
13369 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13370 && code == LE_EXPR
13371 && ((code0 == MINUS_EXPR && is_positive >= 0)
13372 || (code0 == PLUS_EXPR && is_positive <= 0)))
13374 if (TREE_CODE (arg01) == INTEGER_CST
13375 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13376 fold_overflow_warning (("assuming signed overflow does not "
13377 "occur when assuming that "
13378 "(X - c) <= X is always true"),
13379 WARN_STRICT_OVERFLOW_ALL);
13380 return constant_boolean_node (1, type);
13383 /* Convert (X + c) >= X to true. */
13384 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13385 && code == GE_EXPR
13386 && ((code0 == PLUS_EXPR && is_positive >= 0)
13387 || (code0 == MINUS_EXPR && is_positive <= 0)))
13389 if (TREE_CODE (arg01) == INTEGER_CST
13390 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13391 fold_overflow_warning (("assuming signed overflow does not "
13392 "occur when assuming that "
13393 "(X + c) >= X is always true"),
13394 WARN_STRICT_OVERFLOW_ALL);
13395 return constant_boolean_node (1, type);
13398 if (TREE_CODE (arg01) == INTEGER_CST)
13400 /* Convert X + c > X and X - c < X to true for integers. */
13401 if (code == GT_EXPR
13402 && ((code0 == PLUS_EXPR && is_positive > 0)
13403 || (code0 == MINUS_EXPR && is_positive < 0)))
13405 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13406 fold_overflow_warning (("assuming signed overflow does "
13407 "not occur when assuming that "
13408 "(X + c) > X is always true"),
13409 WARN_STRICT_OVERFLOW_ALL);
13410 return constant_boolean_node (1, type);
13413 if (code == LT_EXPR
13414 && ((code0 == MINUS_EXPR && is_positive > 0)
13415 || (code0 == PLUS_EXPR && is_positive < 0)))
13417 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13418 fold_overflow_warning (("assuming signed overflow does "
13419 "not occur when assuming that "
13420 "(X - c) < X is always true"),
13421 WARN_STRICT_OVERFLOW_ALL);
13422 return constant_boolean_node (1, type);
13425 /* Convert X + c <= X and X - c >= X to false for integers. */
13426 if (code == LE_EXPR
13427 && ((code0 == PLUS_EXPR && is_positive > 0)
13428 || (code0 == MINUS_EXPR && is_positive < 0)))
13430 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13431 fold_overflow_warning (("assuming signed overflow does "
13432 "not occur when assuming that "
13433 "(X + c) <= X is always false"),
13434 WARN_STRICT_OVERFLOW_ALL);
13435 return constant_boolean_node (0, type);
13438 if (code == GE_EXPR
13439 && ((code0 == MINUS_EXPR && is_positive > 0)
13440 || (code0 == PLUS_EXPR && is_positive < 0)))
13442 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13443 fold_overflow_warning (("assuming signed overflow does "
13444 "not occur when assuming that "
13445 "(X - c) >= X is always false"),
13446 WARN_STRICT_OVERFLOW_ALL);
13447 return constant_boolean_node (0, type);
13452 /* Comparisons with the highest or lowest possible integer of
13453 the specified precision will have known values. */
13455 tree arg1_type = TREE_TYPE (arg1);
13456 unsigned int prec = TYPE_PRECISION (arg1_type);
13458 if (TREE_CODE (arg1) == INTEGER_CST
13459 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
13461 wide_int max = wi::max_value (arg1_type);
13462 wide_int signed_max = wi::max_value (prec, SIGNED);
13463 wide_int min = wi::min_value (arg1_type);
13465 if (wi::eq_p (arg1, max))
13466 switch (code)
13468 case GT_EXPR:
13469 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13471 case GE_EXPR:
13472 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13474 case LE_EXPR:
13475 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13477 case LT_EXPR:
13478 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13480 /* The GE_EXPR and LT_EXPR cases above are not normally
13481 reached because of previous transformations. */
13483 default:
13484 break;
13486 else if (wi::eq_p (arg1, max - 1))
13487 switch (code)
13489 case GT_EXPR:
13490 arg1 = const_binop (PLUS_EXPR, arg1,
13491 build_int_cst (TREE_TYPE (arg1), 1));
13492 return fold_build2_loc (loc, EQ_EXPR, type,
13493 fold_convert_loc (loc,
13494 TREE_TYPE (arg1), arg0),
13495 arg1);
13496 case LE_EXPR:
13497 arg1 = const_binop (PLUS_EXPR, arg1,
13498 build_int_cst (TREE_TYPE (arg1), 1));
13499 return fold_build2_loc (loc, NE_EXPR, type,
13500 fold_convert_loc (loc, TREE_TYPE (arg1),
13501 arg0),
13502 arg1);
13503 default:
13504 break;
13506 else if (wi::eq_p (arg1, min))
13507 switch (code)
13509 case LT_EXPR:
13510 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13512 case LE_EXPR:
13513 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13515 case GE_EXPR:
13516 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13518 case GT_EXPR:
13519 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13521 default:
13522 break;
13524 else if (wi::eq_p (arg1, min + 1))
13525 switch (code)
13527 case GE_EXPR:
13528 arg1 = const_binop (MINUS_EXPR, arg1,
13529 build_int_cst (TREE_TYPE (arg1), 1));
13530 return fold_build2_loc (loc, NE_EXPR, type,
13531 fold_convert_loc (loc,
13532 TREE_TYPE (arg1), arg0),
13533 arg1);
13534 case LT_EXPR:
13535 arg1 = const_binop (MINUS_EXPR, arg1,
13536 build_int_cst (TREE_TYPE (arg1), 1));
13537 return fold_build2_loc (loc, EQ_EXPR, type,
13538 fold_convert_loc (loc, TREE_TYPE (arg1),
13539 arg0),
13540 arg1);
13541 default:
13542 break;
13545 else if (wi::eq_p (arg1, signed_max)
13546 && TYPE_UNSIGNED (arg1_type)
13547 /* We will flip the signedness of the comparison operator
13548 associated with the mode of arg1, so the sign bit is
13549 specified by this mode. Check that arg1 is the signed
13550 max associated with this sign bit. */
13551 && prec == GET_MODE_PRECISION (TYPE_MODE (arg1_type))
13552 /* signed_type does not work on pointer types. */
13553 && INTEGRAL_TYPE_P (arg1_type))
13555 /* The following case also applies to X < signed_max+1
13556 and X >= signed_max+1 because previous transformations. */
13557 if (code == LE_EXPR || code == GT_EXPR)
13559 tree st = signed_type_for (arg1_type);
13560 return fold_build2_loc (loc,
13561 code == LE_EXPR ? GE_EXPR : LT_EXPR,
13562 type, fold_convert_loc (loc, st, arg0),
13563 build_int_cst (st, 0));
13569 /* If we are comparing an ABS_EXPR with a constant, we can
13570 convert all the cases into explicit comparisons, but they may
13571 well not be faster than doing the ABS and one comparison.
13572 But ABS (X) <= C is a range comparison, which becomes a subtraction
13573 and a comparison, and is probably faster. */
13574 if (code == LE_EXPR
13575 && TREE_CODE (arg1) == INTEGER_CST
13576 && TREE_CODE (arg0) == ABS_EXPR
13577 && ! TREE_SIDE_EFFECTS (arg0)
13578 && (0 != (tem = negate_expr (arg1)))
13579 && TREE_CODE (tem) == INTEGER_CST
13580 && !TREE_OVERFLOW (tem))
13581 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13582 build2 (GE_EXPR, type,
13583 TREE_OPERAND (arg0, 0), tem),
13584 build2 (LE_EXPR, type,
13585 TREE_OPERAND (arg0, 0), arg1));
13587 /* Convert ABS_EXPR<x> >= 0 to true. */
13588 strict_overflow_p = false;
13589 if (code == GE_EXPR
13590 && (integer_zerop (arg1)
13591 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13592 && real_zerop (arg1)))
13593 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13595 if (strict_overflow_p)
13596 fold_overflow_warning (("assuming signed overflow does not occur "
13597 "when simplifying comparison of "
13598 "absolute value and zero"),
13599 WARN_STRICT_OVERFLOW_CONDITIONAL);
13600 return omit_one_operand_loc (loc, type,
13601 constant_boolean_node (true, type),
13602 arg0);
13605 /* Convert ABS_EXPR<x> < 0 to false. */
13606 strict_overflow_p = false;
13607 if (code == LT_EXPR
13608 && (integer_zerop (arg1) || real_zerop (arg1))
13609 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13611 if (strict_overflow_p)
13612 fold_overflow_warning (("assuming signed overflow does not occur "
13613 "when simplifying comparison of "
13614 "absolute value and zero"),
13615 WARN_STRICT_OVERFLOW_CONDITIONAL);
13616 return omit_one_operand_loc (loc, type,
13617 constant_boolean_node (false, type),
13618 arg0);
13621 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13622 and similarly for >= into !=. */
13623 if ((code == LT_EXPR || code == GE_EXPR)
13624 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13625 && TREE_CODE (arg1) == LSHIFT_EXPR
13626 && integer_onep (TREE_OPERAND (arg1, 0)))
13627 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13628 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13629 TREE_OPERAND (arg1, 1)),
13630 build_zero_cst (TREE_TYPE (arg0)));
13632 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
13633 otherwise Y might be >= # of bits in X's type and thus e.g.
13634 (unsigned char) (1 << Y) for Y 15 might be 0.
13635 If the cast is widening, then 1 << Y should have unsigned type,
13636 otherwise if Y is number of bits in the signed shift type minus 1,
13637 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
13638 31 might be 0xffffffff80000000. */
13639 if ((code == LT_EXPR || code == GE_EXPR)
13640 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13641 && CONVERT_EXPR_P (arg1)
13642 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13643 && (TYPE_PRECISION (TREE_TYPE (arg1))
13644 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0))))
13645 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
13646 || (TYPE_PRECISION (TREE_TYPE (arg1))
13647 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
13648 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13650 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13651 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
13652 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13653 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
13654 build_zero_cst (TREE_TYPE (arg0)));
13657 return NULL_TREE;
13659 case UNORDERED_EXPR:
13660 case ORDERED_EXPR:
13661 case UNLT_EXPR:
13662 case UNLE_EXPR:
13663 case UNGT_EXPR:
13664 case UNGE_EXPR:
13665 case UNEQ_EXPR:
13666 case LTGT_EXPR:
13667 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13669 t1 = fold_relational_const (code, type, arg0, arg1);
13670 if (t1 != NULL_TREE)
13671 return t1;
13674 /* If the first operand is NaN, the result is constant. */
13675 if (TREE_CODE (arg0) == REAL_CST
13676 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13677 && (code != LTGT_EXPR || ! flag_trapping_math))
13679 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13680 ? integer_zero_node
13681 : integer_one_node;
13682 return omit_one_operand_loc (loc, type, t1, arg1);
13685 /* If the second operand is NaN, the result is constant. */
13686 if (TREE_CODE (arg1) == REAL_CST
13687 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13688 && (code != LTGT_EXPR || ! flag_trapping_math))
13690 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13691 ? integer_zero_node
13692 : integer_one_node;
13693 return omit_one_operand_loc (loc, type, t1, arg0);
13696 /* Simplify unordered comparison of something with itself. */
13697 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13698 && operand_equal_p (arg0, arg1, 0))
13699 return constant_boolean_node (1, type);
13701 if (code == LTGT_EXPR
13702 && !flag_trapping_math
13703 && operand_equal_p (arg0, arg1, 0))
13704 return constant_boolean_node (0, type);
13706 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13708 tree targ0 = strip_float_extensions (arg0);
13709 tree targ1 = strip_float_extensions (arg1);
13710 tree newtype = TREE_TYPE (targ0);
13712 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13713 newtype = TREE_TYPE (targ1);
13715 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13716 return fold_build2_loc (loc, code, type,
13717 fold_convert_loc (loc, newtype, targ0),
13718 fold_convert_loc (loc, newtype, targ1));
13721 return NULL_TREE;
13723 case COMPOUND_EXPR:
13724 /* When pedantic, a compound expression can be neither an lvalue
13725 nor an integer constant expression. */
13726 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13727 return NULL_TREE;
13728 /* Don't let (0, 0) be null pointer constant. */
13729 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13730 : fold_convert_loc (loc, type, arg1);
13731 return pedantic_non_lvalue_loc (loc, tem);
13733 case COMPLEX_EXPR:
13734 if ((TREE_CODE (arg0) == REAL_CST
13735 && TREE_CODE (arg1) == REAL_CST)
13736 || (TREE_CODE (arg0) == INTEGER_CST
13737 && TREE_CODE (arg1) == INTEGER_CST))
13738 return build_complex (type, arg0, arg1);
13739 if (TREE_CODE (arg0) == REALPART_EXPR
13740 && TREE_CODE (arg1) == IMAGPART_EXPR
13741 && TREE_TYPE (TREE_OPERAND (arg0, 0)) == type
13742 && operand_equal_p (TREE_OPERAND (arg0, 0),
13743 TREE_OPERAND (arg1, 0), 0))
13744 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
13745 TREE_OPERAND (arg1, 0));
13746 return NULL_TREE;
13748 case ASSERT_EXPR:
13749 /* An ASSERT_EXPR should never be passed to fold_binary. */
13750 gcc_unreachable ();
13752 case VEC_PACK_TRUNC_EXPR:
13753 case VEC_PACK_FIX_TRUNC_EXPR:
13755 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
13756 tree *elts;
13758 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts / 2
13759 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2);
13760 if (TREE_CODE (arg0) != VECTOR_CST || TREE_CODE (arg1) != VECTOR_CST)
13761 return NULL_TREE;
13763 elts = XALLOCAVEC (tree, nelts);
13764 if (!vec_cst_ctor_to_array (arg0, elts)
13765 || !vec_cst_ctor_to_array (arg1, elts + nelts / 2))
13766 return NULL_TREE;
13768 for (i = 0; i < nelts; i++)
13770 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
13771 ? NOP_EXPR : FIX_TRUNC_EXPR,
13772 TREE_TYPE (type), elts[i]);
13773 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
13774 return NULL_TREE;
13777 return build_vector (type, elts);
13780 case VEC_WIDEN_MULT_LO_EXPR:
13781 case VEC_WIDEN_MULT_HI_EXPR:
13782 case VEC_WIDEN_MULT_EVEN_EXPR:
13783 case VEC_WIDEN_MULT_ODD_EXPR:
13785 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
13786 unsigned int out, ofs, scale;
13787 tree *elts;
13789 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2
13790 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2);
13791 if (TREE_CODE (arg0) != VECTOR_CST || TREE_CODE (arg1) != VECTOR_CST)
13792 return NULL_TREE;
13794 elts = XALLOCAVEC (tree, nelts * 4);
13795 if (!vec_cst_ctor_to_array (arg0, elts)
13796 || !vec_cst_ctor_to_array (arg1, elts + nelts * 2))
13797 return NULL_TREE;
13799 if (code == VEC_WIDEN_MULT_LO_EXPR)
13800 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
13801 else if (code == VEC_WIDEN_MULT_HI_EXPR)
13802 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
13803 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
13804 scale = 1, ofs = 0;
13805 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
13806 scale = 1, ofs = 1;
13808 for (out = 0; out < nelts; out++)
13810 unsigned int in1 = (out << scale) + ofs;
13811 unsigned int in2 = in1 + nelts * 2;
13812 tree t1, t2;
13814 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
13815 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
13817 if (t1 == NULL_TREE || t2 == NULL_TREE)
13818 return NULL_TREE;
13819 elts[out] = const_binop (MULT_EXPR, t1, t2);
13820 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
13821 return NULL_TREE;
13824 return build_vector (type, elts);
13827 default:
13828 return NULL_TREE;
13829 } /* switch (code) */
13832 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
13833 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
13834 of GOTO_EXPR. */
13836 static tree
13837 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13839 switch (TREE_CODE (*tp))
13841 case LABEL_EXPR:
13842 return *tp;
13844 case GOTO_EXPR:
13845 *walk_subtrees = 0;
13847 /* ... fall through ... */
13849 default:
13850 return NULL_TREE;
13854 /* Return whether the sub-tree ST contains a label which is accessible from
13855 outside the sub-tree. */
13857 static bool
13858 contains_label_p (tree st)
13860 return
13861 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13864 /* Fold a ternary expression of code CODE and type TYPE with operands
13865 OP0, OP1, and OP2. Return the folded expression if folding is
13866 successful. Otherwise, return NULL_TREE. */
13868 tree
13869 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13870 tree op0, tree op1, tree op2)
13872 tree tem;
13873 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
13874 enum tree_code_class kind = TREE_CODE_CLASS (code);
13876 gcc_assert (IS_EXPR_CODE_CLASS (kind)
13877 && TREE_CODE_LENGTH (code) == 3);
13879 /* Strip any conversions that don't change the mode. This is safe
13880 for every expression, except for a comparison expression because
13881 its signedness is derived from its operands. So, in the latter
13882 case, only strip conversions that don't change the signedness.
13884 Note that this is done as an internal manipulation within the
13885 constant folder, in order to find the simplest representation of
13886 the arguments so that their form can be studied. In any cases,
13887 the appropriate type conversions should be put back in the tree
13888 that will get out of the constant folder. */
13889 if (op0)
13891 arg0 = op0;
13892 STRIP_NOPS (arg0);
13895 if (op1)
13897 arg1 = op1;
13898 STRIP_NOPS (arg1);
13901 if (op2)
13903 arg2 = op2;
13904 STRIP_NOPS (arg2);
13907 switch (code)
13909 case COMPONENT_REF:
13910 if (TREE_CODE (arg0) == CONSTRUCTOR
13911 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13913 unsigned HOST_WIDE_INT idx;
13914 tree field, value;
13915 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13916 if (field == arg1)
13917 return value;
13919 return NULL_TREE;
13921 case COND_EXPR:
13922 case VEC_COND_EXPR:
13923 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13924 so all simple results must be passed through pedantic_non_lvalue. */
13925 if (TREE_CODE (arg0) == INTEGER_CST)
13927 tree unused_op = integer_zerop (arg0) ? op1 : op2;
13928 tem = integer_zerop (arg0) ? op2 : op1;
13929 /* Only optimize constant conditions when the selected branch
13930 has the same type as the COND_EXPR. This avoids optimizing
13931 away "c ? x : throw", where the throw has a void type.
13932 Avoid throwing away that operand which contains label. */
13933 if ((!TREE_SIDE_EFFECTS (unused_op)
13934 || !contains_label_p (unused_op))
13935 && (! VOID_TYPE_P (TREE_TYPE (tem))
13936 || VOID_TYPE_P (type)))
13937 return pedantic_non_lvalue_loc (loc, tem);
13938 return NULL_TREE;
13940 else if (TREE_CODE (arg0) == VECTOR_CST)
13942 if (integer_all_onesp (arg0))
13943 return pedantic_omit_one_operand_loc (loc, type, arg1, arg2);
13944 if (integer_zerop (arg0))
13945 return pedantic_omit_one_operand_loc (loc, type, arg2, arg1);
13947 if ((TREE_CODE (arg1) == VECTOR_CST
13948 || TREE_CODE (arg1) == CONSTRUCTOR)
13949 && (TREE_CODE (arg2) == VECTOR_CST
13950 || TREE_CODE (arg2) == CONSTRUCTOR))
13952 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
13953 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
13954 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
13955 for (i = 0; i < nelts; i++)
13957 tree val = VECTOR_CST_ELT (arg0, i);
13958 if (integer_all_onesp (val))
13959 sel[i] = i;
13960 else if (integer_zerop (val))
13961 sel[i] = nelts + i;
13962 else /* Currently unreachable. */
13963 return NULL_TREE;
13965 tree t = fold_vec_perm (type, arg1, arg2, sel);
13966 if (t != NULL_TREE)
13967 return t;
13971 if (operand_equal_p (arg1, op2, 0))
13972 return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13974 /* If we have A op B ? A : C, we may be able to convert this to a
13975 simpler expression, depending on the operation and the values
13976 of B and C. Signed zeros prevent all of these transformations,
13977 for reasons given above each one.
13979 Also try swapping the arguments and inverting the conditional. */
13980 if (COMPARISON_CLASS_P (arg0)
13981 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13982 arg1, TREE_OPERAND (arg0, 1))
13983 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13985 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13986 if (tem)
13987 return tem;
13990 if (COMPARISON_CLASS_P (arg0)
13991 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13992 op2,
13993 TREE_OPERAND (arg0, 1))
13994 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13996 location_t loc0 = expr_location_or (arg0, loc);
13997 tem = fold_invert_truthvalue (loc0, arg0);
13998 if (tem && COMPARISON_CLASS_P (tem))
14000 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
14001 if (tem)
14002 return tem;
14006 /* If the second operand is simpler than the third, swap them
14007 since that produces better jump optimization results. */
14008 if (truth_value_p (TREE_CODE (arg0))
14009 && tree_swap_operands_p (op1, op2, false))
14011 location_t loc0 = expr_location_or (arg0, loc);
14012 /* See if this can be inverted. If it can't, possibly because
14013 it was a floating-point inequality comparison, don't do
14014 anything. */
14015 tem = fold_invert_truthvalue (loc0, arg0);
14016 if (tem)
14017 return fold_build3_loc (loc, code, type, tem, op2, op1);
14020 /* Convert A ? 1 : 0 to simply A. */
14021 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
14022 : (integer_onep (op1)
14023 && !VECTOR_TYPE_P (type)))
14024 && integer_zerop (op2)
14025 /* If we try to convert OP0 to our type, the
14026 call to fold will try to move the conversion inside
14027 a COND, which will recurse. In that case, the COND_EXPR
14028 is probably the best choice, so leave it alone. */
14029 && type == TREE_TYPE (arg0))
14030 return pedantic_non_lvalue_loc (loc, arg0);
14032 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
14033 over COND_EXPR in cases such as floating point comparisons. */
14034 if (integer_zerop (op1)
14035 && (code == VEC_COND_EXPR ? integer_all_onesp (op2)
14036 : (integer_onep (op2)
14037 && !VECTOR_TYPE_P (type)))
14038 && truth_value_p (TREE_CODE (arg0)))
14039 return pedantic_non_lvalue_loc (loc,
14040 fold_convert_loc (loc, type,
14041 invert_truthvalue_loc (loc,
14042 arg0)));
14044 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
14045 if (TREE_CODE (arg0) == LT_EXPR
14046 && integer_zerop (TREE_OPERAND (arg0, 1))
14047 && integer_zerop (op2)
14048 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
14050 /* sign_bit_p looks through both zero and sign extensions,
14051 but for this optimization only sign extensions are
14052 usable. */
14053 tree tem2 = TREE_OPERAND (arg0, 0);
14054 while (tem != tem2)
14056 if (TREE_CODE (tem2) != NOP_EXPR
14057 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
14059 tem = NULL_TREE;
14060 break;
14062 tem2 = TREE_OPERAND (tem2, 0);
14064 /* sign_bit_p only checks ARG1 bits within A's precision.
14065 If <sign bit of A> has wider type than A, bits outside
14066 of A's precision in <sign bit of A> need to be checked.
14067 If they are all 0, this optimization needs to be done
14068 in unsigned A's type, if they are all 1 in signed A's type,
14069 otherwise this can't be done. */
14070 if (tem
14071 && TYPE_PRECISION (TREE_TYPE (tem))
14072 < TYPE_PRECISION (TREE_TYPE (arg1))
14073 && TYPE_PRECISION (TREE_TYPE (tem))
14074 < TYPE_PRECISION (type))
14076 int inner_width, outer_width;
14077 tree tem_type;
14079 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
14080 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
14081 if (outer_width > TYPE_PRECISION (type))
14082 outer_width = TYPE_PRECISION (type);
14084 wide_int mask = wi::shifted_mask
14085 (inner_width, outer_width - inner_width, false,
14086 TYPE_PRECISION (TREE_TYPE (arg1)));
14088 wide_int common = mask & arg1;
14089 if (common == mask)
14091 tem_type = signed_type_for (TREE_TYPE (tem));
14092 tem = fold_convert_loc (loc, tem_type, tem);
14094 else if (common == 0)
14096 tem_type = unsigned_type_for (TREE_TYPE (tem));
14097 tem = fold_convert_loc (loc, tem_type, tem);
14099 else
14100 tem = NULL;
14103 if (tem)
14104 return
14105 fold_convert_loc (loc, type,
14106 fold_build2_loc (loc, BIT_AND_EXPR,
14107 TREE_TYPE (tem), tem,
14108 fold_convert_loc (loc,
14109 TREE_TYPE (tem),
14110 arg1)));
14113 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
14114 already handled above. */
14115 if (TREE_CODE (arg0) == BIT_AND_EXPR
14116 && integer_onep (TREE_OPERAND (arg0, 1))
14117 && integer_zerop (op2)
14118 && integer_pow2p (arg1))
14120 tree tem = TREE_OPERAND (arg0, 0);
14121 STRIP_NOPS (tem);
14122 if (TREE_CODE (tem) == RSHIFT_EXPR
14123 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
14124 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
14125 tree_to_uhwi (TREE_OPERAND (tem, 1)))
14126 return fold_build2_loc (loc, BIT_AND_EXPR, type,
14127 TREE_OPERAND (tem, 0), arg1);
14130 /* A & N ? N : 0 is simply A & N if N is a power of two. This
14131 is probably obsolete because the first operand should be a
14132 truth value (that's why we have the two cases above), but let's
14133 leave it in until we can confirm this for all front-ends. */
14134 if (integer_zerop (op2)
14135 && TREE_CODE (arg0) == NE_EXPR
14136 && integer_zerop (TREE_OPERAND (arg0, 1))
14137 && integer_pow2p (arg1)
14138 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
14139 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
14140 arg1, OEP_ONLY_CONST))
14141 return pedantic_non_lvalue_loc (loc,
14142 fold_convert_loc (loc, type,
14143 TREE_OPERAND (arg0, 0)));
14145 /* Disable the transformations below for vectors, since
14146 fold_binary_op_with_conditional_arg may undo them immediately,
14147 yielding an infinite loop. */
14148 if (code == VEC_COND_EXPR)
14149 return NULL_TREE;
14151 /* Convert A ? B : 0 into A && B if A and B are truth values. */
14152 if (integer_zerop (op2)
14153 && truth_value_p (TREE_CODE (arg0))
14154 && truth_value_p (TREE_CODE (arg1))
14155 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
14156 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
14157 : TRUTH_ANDIF_EXPR,
14158 type, fold_convert_loc (loc, type, arg0), arg1);
14160 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
14161 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
14162 && truth_value_p (TREE_CODE (arg0))
14163 && truth_value_p (TREE_CODE (arg1))
14164 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
14166 location_t loc0 = expr_location_or (arg0, loc);
14167 /* Only perform transformation if ARG0 is easily inverted. */
14168 tem = fold_invert_truthvalue (loc0, arg0);
14169 if (tem)
14170 return fold_build2_loc (loc, code == VEC_COND_EXPR
14171 ? BIT_IOR_EXPR
14172 : TRUTH_ORIF_EXPR,
14173 type, fold_convert_loc (loc, type, tem),
14174 arg1);
14177 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
14178 if (integer_zerop (arg1)
14179 && truth_value_p (TREE_CODE (arg0))
14180 && truth_value_p (TREE_CODE (op2))
14181 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
14183 location_t loc0 = expr_location_or (arg0, loc);
14184 /* Only perform transformation if ARG0 is easily inverted. */
14185 tem = fold_invert_truthvalue (loc0, arg0);
14186 if (tem)
14187 return fold_build2_loc (loc, code == VEC_COND_EXPR
14188 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
14189 type, fold_convert_loc (loc, type, tem),
14190 op2);
14193 /* Convert A ? 1 : B into A || B if A and B are truth values. */
14194 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
14195 && truth_value_p (TREE_CODE (arg0))
14196 && truth_value_p (TREE_CODE (op2))
14197 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
14198 return fold_build2_loc (loc, code == VEC_COND_EXPR
14199 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
14200 type, fold_convert_loc (loc, type, arg0), op2);
14202 return NULL_TREE;
14204 case CALL_EXPR:
14205 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
14206 of fold_ternary on them. */
14207 gcc_unreachable ();
14209 case BIT_FIELD_REF:
14210 if ((TREE_CODE (arg0) == VECTOR_CST
14211 || (TREE_CODE (arg0) == CONSTRUCTOR
14212 && TREE_CODE (TREE_TYPE (arg0)) == VECTOR_TYPE))
14213 && (type == TREE_TYPE (TREE_TYPE (arg0))
14214 || (TREE_CODE (type) == VECTOR_TYPE
14215 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
14217 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
14218 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
14219 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
14220 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
14222 if (n != 0
14223 && (idx % width) == 0
14224 && (n % width) == 0
14225 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
14227 idx = idx / width;
14228 n = n / width;
14230 if (TREE_CODE (arg0) == VECTOR_CST)
14232 if (n == 1)
14233 return VECTOR_CST_ELT (arg0, idx);
14235 tree *vals = XALLOCAVEC (tree, n);
14236 for (unsigned i = 0; i < n; ++i)
14237 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
14238 return build_vector (type, vals);
14241 /* Constructor elements can be subvectors. */
14242 unsigned HOST_WIDE_INT k = 1;
14243 if (CONSTRUCTOR_NELTS (arg0) != 0)
14245 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (arg0, 0)->value);
14246 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
14247 k = TYPE_VECTOR_SUBPARTS (cons_elem);
14250 /* We keep an exact subset of the constructor elements. */
14251 if ((idx % k) == 0 && (n % k) == 0)
14253 if (CONSTRUCTOR_NELTS (arg0) == 0)
14254 return build_constructor (type, NULL);
14255 idx /= k;
14256 n /= k;
14257 if (n == 1)
14259 if (idx < CONSTRUCTOR_NELTS (arg0))
14260 return CONSTRUCTOR_ELT (arg0, idx)->value;
14261 return build_zero_cst (type);
14264 vec<constructor_elt, va_gc> *vals;
14265 vec_alloc (vals, n);
14266 for (unsigned i = 0;
14267 i < n && idx + i < CONSTRUCTOR_NELTS (arg0);
14268 ++i)
14269 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
14270 CONSTRUCTOR_ELT
14271 (arg0, idx + i)->value);
14272 return build_constructor (type, vals);
14274 /* The bitfield references a single constructor element. */
14275 else if (idx + n <= (idx / k + 1) * k)
14277 if (CONSTRUCTOR_NELTS (arg0) <= idx / k)
14278 return build_zero_cst (type);
14279 else if (n == k)
14280 return CONSTRUCTOR_ELT (arg0, idx / k)->value;
14281 else
14282 return fold_build3_loc (loc, code, type,
14283 CONSTRUCTOR_ELT (arg0, idx / k)->value, op1,
14284 build_int_cst (TREE_TYPE (op2), (idx % k) * width));
14289 /* A bit-field-ref that referenced the full argument can be stripped. */
14290 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
14291 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_to_uhwi (arg1)
14292 && integer_zerop (op2))
14293 return fold_convert_loc (loc, type, arg0);
14295 /* On constants we can use native encode/interpret to constant
14296 fold (nearly) all BIT_FIELD_REFs. */
14297 if (CONSTANT_CLASS_P (arg0)
14298 && can_native_interpret_type_p (type)
14299 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0)))
14300 /* This limitation should not be necessary, we just need to
14301 round this up to mode size. */
14302 && tree_to_uhwi (op1) % BITS_PER_UNIT == 0
14303 /* Need bit-shifting of the buffer to relax the following. */
14304 && tree_to_uhwi (op2) % BITS_PER_UNIT == 0)
14306 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
14307 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
14308 unsigned HOST_WIDE_INT clen;
14309 clen = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0)));
14310 /* ??? We cannot tell native_encode_expr to start at
14311 some random byte only. So limit us to a reasonable amount
14312 of work. */
14313 if (clen <= 4096)
14315 unsigned char *b = XALLOCAVEC (unsigned char, clen);
14316 unsigned HOST_WIDE_INT len
14317 = native_encode_expr (arg0, b, clen, false);
14318 if (len > 0 && len * BITS_PER_UNIT >= bitpos + bitsize)
14320 tree v = native_interpret_expr (type,
14321 b + bitpos / BITS_PER_UNIT,
14322 bitsize / BITS_PER_UNIT);
14323 if (v)
14324 return v;
14329 return NULL_TREE;
14331 case FMA_EXPR:
14332 /* For integers we can decompose the FMA if possible. */
14333 if (TREE_CODE (arg0) == INTEGER_CST
14334 && TREE_CODE (arg1) == INTEGER_CST)
14335 return fold_build2_loc (loc, PLUS_EXPR, type,
14336 const_binop (MULT_EXPR, arg0, arg1), arg2);
14337 if (integer_zerop (arg2))
14338 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
14340 return fold_fma (loc, type, arg0, arg1, arg2);
14342 case VEC_PERM_EXPR:
14343 if (TREE_CODE (arg2) == VECTOR_CST)
14345 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask;
14346 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
14347 bool need_mask_canon = false;
14348 bool all_in_vec0 = true;
14349 bool all_in_vec1 = true;
14350 bool maybe_identity = true;
14351 bool single_arg = (op0 == op1);
14352 bool changed = false;
14354 mask = single_arg ? (nelts - 1) : (2 * nelts - 1);
14355 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
14356 for (i = 0; i < nelts; i++)
14358 tree val = VECTOR_CST_ELT (arg2, i);
14359 if (TREE_CODE (val) != INTEGER_CST)
14360 return NULL_TREE;
14362 /* Make sure that the perm value is in an acceptable
14363 range. */
14364 wide_int t = val;
14365 if (wi::gtu_p (t, mask))
14367 need_mask_canon = true;
14368 sel[i] = t.to_uhwi () & mask;
14370 else
14371 sel[i] = t.to_uhwi ();
14373 if (sel[i] < nelts)
14374 all_in_vec1 = false;
14375 else
14376 all_in_vec0 = false;
14378 if ((sel[i] & (nelts-1)) != i)
14379 maybe_identity = false;
14382 if (maybe_identity)
14384 if (all_in_vec0)
14385 return op0;
14386 if (all_in_vec1)
14387 return op1;
14390 if (all_in_vec0)
14391 op1 = op0;
14392 else if (all_in_vec1)
14394 op0 = op1;
14395 for (i = 0; i < nelts; i++)
14396 sel[i] -= nelts;
14397 need_mask_canon = true;
14400 if ((TREE_CODE (op0) == VECTOR_CST
14401 || TREE_CODE (op0) == CONSTRUCTOR)
14402 && (TREE_CODE (op1) == VECTOR_CST
14403 || TREE_CODE (op1) == CONSTRUCTOR))
14405 tree t = fold_vec_perm (type, op0, op1, sel);
14406 if (t != NULL_TREE)
14407 return t;
14410 if (op0 == op1 && !single_arg)
14411 changed = true;
14413 if (need_mask_canon && arg2 == op2)
14415 tree *tsel = XALLOCAVEC (tree, nelts);
14416 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
14417 for (i = 0; i < nelts; i++)
14418 tsel[i] = build_int_cst (eltype, sel[i]);
14419 op2 = build_vector (TREE_TYPE (arg2), tsel);
14420 changed = true;
14423 if (changed)
14424 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
14426 return NULL_TREE;
14428 default:
14429 return NULL_TREE;
14430 } /* switch (code) */
14433 /* Perform constant folding and related simplification of EXPR.
14434 The related simplifications include x*1 => x, x*0 => 0, etc.,
14435 and application of the associative law.
14436 NOP_EXPR conversions may be removed freely (as long as we
14437 are careful not to change the type of the overall expression).
14438 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
14439 but we can constant-fold them if they have constant operands. */
14441 #ifdef ENABLE_FOLD_CHECKING
14442 # define fold(x) fold_1 (x)
14443 static tree fold_1 (tree);
14444 static
14445 #endif
14446 tree
14447 fold (tree expr)
14449 const tree t = expr;
14450 enum tree_code code = TREE_CODE (t);
14451 enum tree_code_class kind = TREE_CODE_CLASS (code);
14452 tree tem;
14453 location_t loc = EXPR_LOCATION (expr);
14455 /* Return right away if a constant. */
14456 if (kind == tcc_constant)
14457 return t;
14459 /* CALL_EXPR-like objects with variable numbers of operands are
14460 treated specially. */
14461 if (kind == tcc_vl_exp)
14463 if (code == CALL_EXPR)
14465 tem = fold_call_expr (loc, expr, false);
14466 return tem ? tem : expr;
14468 return expr;
14471 if (IS_EXPR_CODE_CLASS (kind))
14473 tree type = TREE_TYPE (t);
14474 tree op0, op1, op2;
14476 switch (TREE_CODE_LENGTH (code))
14478 case 1:
14479 op0 = TREE_OPERAND (t, 0);
14480 tem = fold_unary_loc (loc, code, type, op0);
14481 return tem ? tem : expr;
14482 case 2:
14483 op0 = TREE_OPERAND (t, 0);
14484 op1 = TREE_OPERAND (t, 1);
14485 tem = fold_binary_loc (loc, code, type, op0, op1);
14486 return tem ? tem : expr;
14487 case 3:
14488 op0 = TREE_OPERAND (t, 0);
14489 op1 = TREE_OPERAND (t, 1);
14490 op2 = TREE_OPERAND (t, 2);
14491 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14492 return tem ? tem : expr;
14493 default:
14494 break;
14498 switch (code)
14500 case ARRAY_REF:
14502 tree op0 = TREE_OPERAND (t, 0);
14503 tree op1 = TREE_OPERAND (t, 1);
14505 if (TREE_CODE (op1) == INTEGER_CST
14506 && TREE_CODE (op0) == CONSTRUCTOR
14507 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
14509 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (op0);
14510 unsigned HOST_WIDE_INT end = vec_safe_length (elts);
14511 unsigned HOST_WIDE_INT begin = 0;
14513 /* Find a matching index by means of a binary search. */
14514 while (begin != end)
14516 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
14517 tree index = (*elts)[middle].index;
14519 if (TREE_CODE (index) == INTEGER_CST
14520 && tree_int_cst_lt (index, op1))
14521 begin = middle + 1;
14522 else if (TREE_CODE (index) == INTEGER_CST
14523 && tree_int_cst_lt (op1, index))
14524 end = middle;
14525 else if (TREE_CODE (index) == RANGE_EXPR
14526 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
14527 begin = middle + 1;
14528 else if (TREE_CODE (index) == RANGE_EXPR
14529 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
14530 end = middle;
14531 else
14532 return (*elts)[middle].value;
14536 return t;
14539 /* Return a VECTOR_CST if possible. */
14540 case CONSTRUCTOR:
14542 tree type = TREE_TYPE (t);
14543 if (TREE_CODE (type) != VECTOR_TYPE)
14544 return t;
14546 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
14547 unsigned HOST_WIDE_INT idx, pos = 0;
14548 tree value;
14550 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, value)
14552 if (!CONSTANT_CLASS_P (value))
14553 return t;
14554 if (TREE_CODE (value) == VECTOR_CST)
14556 for (unsigned i = 0; i < VECTOR_CST_NELTS (value); ++i)
14557 vec[pos++] = VECTOR_CST_ELT (value, i);
14559 else
14560 vec[pos++] = value;
14562 for (; pos < TYPE_VECTOR_SUBPARTS (type); ++pos)
14563 vec[pos] = build_zero_cst (TREE_TYPE (type));
14565 return build_vector (type, vec);
14568 case CONST_DECL:
14569 return fold (DECL_INITIAL (t));
14571 default:
14572 return t;
14573 } /* switch (code) */
14576 #ifdef ENABLE_FOLD_CHECKING
14577 #undef fold
14579 static void fold_checksum_tree (const_tree, struct md5_ctx *,
14580 hash_table<pointer_hash<const tree_node> > *);
14581 static void fold_check_failed (const_tree, const_tree);
14582 void print_fold_checksum (const_tree);
14584 /* When --enable-checking=fold, compute a digest of expr before
14585 and after actual fold call to see if fold did not accidentally
14586 change original expr. */
14588 tree
14589 fold (tree expr)
14591 tree ret;
14592 struct md5_ctx ctx;
14593 unsigned char checksum_before[16], checksum_after[16];
14594 hash_table<pointer_hash<const tree_node> > ht (32);
14596 md5_init_ctx (&ctx);
14597 fold_checksum_tree (expr, &ctx, &ht);
14598 md5_finish_ctx (&ctx, checksum_before);
14599 ht.empty ();
14601 ret = fold_1 (expr);
14603 md5_init_ctx (&ctx);
14604 fold_checksum_tree (expr, &ctx, &ht);
14605 md5_finish_ctx (&ctx, checksum_after);
14607 if (memcmp (checksum_before, checksum_after, 16))
14608 fold_check_failed (expr, ret);
14610 return ret;
14613 void
14614 print_fold_checksum (const_tree expr)
14616 struct md5_ctx ctx;
14617 unsigned char checksum[16], cnt;
14618 hash_table<pointer_hash<const tree_node> > ht (32);
14620 md5_init_ctx (&ctx);
14621 fold_checksum_tree (expr, &ctx, &ht);
14622 md5_finish_ctx (&ctx, checksum);
14623 for (cnt = 0; cnt < 16; ++cnt)
14624 fprintf (stderr, "%02x", checksum[cnt]);
14625 putc ('\n', stderr);
14628 static void
14629 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
14631 internal_error ("fold check: original tree changed by fold");
14634 static void
14635 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
14636 hash_table<pointer_hash <const tree_node> > *ht)
14638 const tree_node **slot;
14639 enum tree_code code;
14640 union tree_node buf;
14641 int i, len;
14643 recursive_label:
14644 if (expr == NULL)
14645 return;
14646 slot = ht->find_slot (expr, INSERT);
14647 if (*slot != NULL)
14648 return;
14649 *slot = expr;
14650 code = TREE_CODE (expr);
14651 if (TREE_CODE_CLASS (code) == tcc_declaration
14652 && DECL_ASSEMBLER_NAME_SET_P (expr))
14654 /* Allow DECL_ASSEMBLER_NAME to be modified. */
14655 memcpy ((char *) &buf, expr, tree_size (expr));
14656 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
14657 expr = (tree) &buf;
14659 else if (TREE_CODE_CLASS (code) == tcc_type
14660 && (TYPE_POINTER_TO (expr)
14661 || TYPE_REFERENCE_TO (expr)
14662 || TYPE_CACHED_VALUES_P (expr)
14663 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
14664 || TYPE_NEXT_VARIANT (expr)))
14666 /* Allow these fields to be modified. */
14667 tree tmp;
14668 memcpy ((char *) &buf, expr, tree_size (expr));
14669 expr = tmp = (tree) &buf;
14670 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
14671 TYPE_POINTER_TO (tmp) = NULL;
14672 TYPE_REFERENCE_TO (tmp) = NULL;
14673 TYPE_NEXT_VARIANT (tmp) = NULL;
14674 if (TYPE_CACHED_VALUES_P (tmp))
14676 TYPE_CACHED_VALUES_P (tmp) = 0;
14677 TYPE_CACHED_VALUES (tmp) = NULL;
14680 md5_process_bytes (expr, tree_size (expr), ctx);
14681 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
14682 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
14683 if (TREE_CODE_CLASS (code) != tcc_type
14684 && TREE_CODE_CLASS (code) != tcc_declaration
14685 && code != TREE_LIST
14686 && code != SSA_NAME
14687 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
14688 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
14689 switch (TREE_CODE_CLASS (code))
14691 case tcc_constant:
14692 switch (code)
14694 case STRING_CST:
14695 md5_process_bytes (TREE_STRING_POINTER (expr),
14696 TREE_STRING_LENGTH (expr), ctx);
14697 break;
14698 case COMPLEX_CST:
14699 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
14700 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
14701 break;
14702 case VECTOR_CST:
14703 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
14704 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
14705 break;
14706 default:
14707 break;
14709 break;
14710 case tcc_exceptional:
14711 switch (code)
14713 case TREE_LIST:
14714 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
14715 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
14716 expr = TREE_CHAIN (expr);
14717 goto recursive_label;
14718 break;
14719 case TREE_VEC:
14720 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
14721 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
14722 break;
14723 default:
14724 break;
14726 break;
14727 case tcc_expression:
14728 case tcc_reference:
14729 case tcc_comparison:
14730 case tcc_unary:
14731 case tcc_binary:
14732 case tcc_statement:
14733 case tcc_vl_exp:
14734 len = TREE_OPERAND_LENGTH (expr);
14735 for (i = 0; i < len; ++i)
14736 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
14737 break;
14738 case tcc_declaration:
14739 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
14740 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
14741 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
14743 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
14744 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
14745 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
14746 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
14747 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
14750 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
14752 if (TREE_CODE (expr) == FUNCTION_DECL)
14754 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
14755 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
14757 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
14759 break;
14760 case tcc_type:
14761 if (TREE_CODE (expr) == ENUMERAL_TYPE)
14762 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
14763 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
14764 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
14765 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
14766 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
14767 if (INTEGRAL_TYPE_P (expr)
14768 || SCALAR_FLOAT_TYPE_P (expr))
14770 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
14771 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
14773 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
14774 if (TREE_CODE (expr) == RECORD_TYPE
14775 || TREE_CODE (expr) == UNION_TYPE
14776 || TREE_CODE (expr) == QUAL_UNION_TYPE)
14777 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
14778 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
14779 break;
14780 default:
14781 break;
14785 /* Helper function for outputting the checksum of a tree T. When
14786 debugging with gdb, you can "define mynext" to be "next" followed
14787 by "call debug_fold_checksum (op0)", then just trace down till the
14788 outputs differ. */
14790 DEBUG_FUNCTION void
14791 debug_fold_checksum (const_tree t)
14793 int i;
14794 unsigned char checksum[16];
14795 struct md5_ctx ctx;
14796 hash_table<pointer_hash<const tree_node> > ht (32);
14798 md5_init_ctx (&ctx);
14799 fold_checksum_tree (t, &ctx, &ht);
14800 md5_finish_ctx (&ctx, checksum);
14801 ht.empty ();
14803 for (i = 0; i < 16; i++)
14804 fprintf (stderr, "%d ", checksum[i]);
14806 fprintf (stderr, "\n");
14809 #endif
14811 /* Fold a unary tree expression with code CODE of type TYPE with an
14812 operand OP0. LOC is the location of the resulting expression.
14813 Return a folded expression if successful. Otherwise, return a tree
14814 expression with code CODE of type TYPE with an operand OP0. */
14816 tree
14817 fold_build1_stat_loc (location_t loc,
14818 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
14820 tree tem;
14821 #ifdef ENABLE_FOLD_CHECKING
14822 unsigned char checksum_before[16], checksum_after[16];
14823 struct md5_ctx ctx;
14824 hash_table<pointer_hash<const tree_node> > ht (32);
14826 md5_init_ctx (&ctx);
14827 fold_checksum_tree (op0, &ctx, &ht);
14828 md5_finish_ctx (&ctx, checksum_before);
14829 ht.empty ();
14830 #endif
14832 tem = fold_unary_loc (loc, code, type, op0);
14833 if (!tem)
14834 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
14836 #ifdef ENABLE_FOLD_CHECKING
14837 md5_init_ctx (&ctx);
14838 fold_checksum_tree (op0, &ctx, &ht);
14839 md5_finish_ctx (&ctx, checksum_after);
14841 if (memcmp (checksum_before, checksum_after, 16))
14842 fold_check_failed (op0, tem);
14843 #endif
14844 return tem;
14847 /* Fold a binary tree expression with code CODE of type TYPE with
14848 operands OP0 and OP1. LOC is the location of the resulting
14849 expression. Return a folded expression if successful. Otherwise,
14850 return a tree expression with code CODE of type TYPE with operands
14851 OP0 and OP1. */
14853 tree
14854 fold_build2_stat_loc (location_t loc,
14855 enum tree_code code, tree type, tree op0, tree op1
14856 MEM_STAT_DECL)
14858 tree tem;
14859 #ifdef ENABLE_FOLD_CHECKING
14860 unsigned char checksum_before_op0[16],
14861 checksum_before_op1[16],
14862 checksum_after_op0[16],
14863 checksum_after_op1[16];
14864 struct md5_ctx ctx;
14865 hash_table<pointer_hash<const tree_node> > ht (32);
14867 md5_init_ctx (&ctx);
14868 fold_checksum_tree (op0, &ctx, &ht);
14869 md5_finish_ctx (&ctx, checksum_before_op0);
14870 ht.empty ();
14872 md5_init_ctx (&ctx);
14873 fold_checksum_tree (op1, &ctx, &ht);
14874 md5_finish_ctx (&ctx, checksum_before_op1);
14875 ht.empty ();
14876 #endif
14878 tem = fold_binary_loc (loc, code, type, op0, op1);
14879 if (!tem)
14880 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
14882 #ifdef ENABLE_FOLD_CHECKING
14883 md5_init_ctx (&ctx);
14884 fold_checksum_tree (op0, &ctx, &ht);
14885 md5_finish_ctx (&ctx, checksum_after_op0);
14886 ht.empty ();
14888 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14889 fold_check_failed (op0, tem);
14891 md5_init_ctx (&ctx);
14892 fold_checksum_tree (op1, &ctx, &ht);
14893 md5_finish_ctx (&ctx, checksum_after_op1);
14895 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14896 fold_check_failed (op1, tem);
14897 #endif
14898 return tem;
14901 /* Fold a ternary tree expression with code CODE of type TYPE with
14902 operands OP0, OP1, and OP2. Return a folded expression if
14903 successful. Otherwise, return a tree expression with code CODE of
14904 type TYPE with operands OP0, OP1, and OP2. */
14906 tree
14907 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14908 tree op0, tree op1, tree op2 MEM_STAT_DECL)
14910 tree tem;
14911 #ifdef ENABLE_FOLD_CHECKING
14912 unsigned char checksum_before_op0[16],
14913 checksum_before_op1[16],
14914 checksum_before_op2[16],
14915 checksum_after_op0[16],
14916 checksum_after_op1[16],
14917 checksum_after_op2[16];
14918 struct md5_ctx ctx;
14919 hash_table<pointer_hash<const tree_node> > ht (32);
14921 md5_init_ctx (&ctx);
14922 fold_checksum_tree (op0, &ctx, &ht);
14923 md5_finish_ctx (&ctx, checksum_before_op0);
14924 ht.empty ();
14926 md5_init_ctx (&ctx);
14927 fold_checksum_tree (op1, &ctx, &ht);
14928 md5_finish_ctx (&ctx, checksum_before_op1);
14929 ht.empty ();
14931 md5_init_ctx (&ctx);
14932 fold_checksum_tree (op2, &ctx, &ht);
14933 md5_finish_ctx (&ctx, checksum_before_op2);
14934 ht.empty ();
14935 #endif
14937 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14938 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14939 if (!tem)
14940 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14942 #ifdef ENABLE_FOLD_CHECKING
14943 md5_init_ctx (&ctx);
14944 fold_checksum_tree (op0, &ctx, &ht);
14945 md5_finish_ctx (&ctx, checksum_after_op0);
14946 ht.empty ();
14948 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14949 fold_check_failed (op0, tem);
14951 md5_init_ctx (&ctx);
14952 fold_checksum_tree (op1, &ctx, &ht);
14953 md5_finish_ctx (&ctx, checksum_after_op1);
14954 ht.empty ();
14956 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14957 fold_check_failed (op1, tem);
14959 md5_init_ctx (&ctx);
14960 fold_checksum_tree (op2, &ctx, &ht);
14961 md5_finish_ctx (&ctx, checksum_after_op2);
14963 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14964 fold_check_failed (op2, tem);
14965 #endif
14966 return tem;
14969 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14970 arguments in ARGARRAY, and a null static chain.
14971 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14972 of type TYPE from the given operands as constructed by build_call_array. */
14974 tree
14975 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14976 int nargs, tree *argarray)
14978 tree tem;
14979 #ifdef ENABLE_FOLD_CHECKING
14980 unsigned char checksum_before_fn[16],
14981 checksum_before_arglist[16],
14982 checksum_after_fn[16],
14983 checksum_after_arglist[16];
14984 struct md5_ctx ctx;
14985 hash_table<pointer_hash<const tree_node> > ht (32);
14986 int i;
14988 md5_init_ctx (&ctx);
14989 fold_checksum_tree (fn, &ctx, &ht);
14990 md5_finish_ctx (&ctx, checksum_before_fn);
14991 ht.empty ();
14993 md5_init_ctx (&ctx);
14994 for (i = 0; i < nargs; i++)
14995 fold_checksum_tree (argarray[i], &ctx, &ht);
14996 md5_finish_ctx (&ctx, checksum_before_arglist);
14997 ht.empty ();
14998 #endif
15000 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
15002 #ifdef ENABLE_FOLD_CHECKING
15003 md5_init_ctx (&ctx);
15004 fold_checksum_tree (fn, &ctx, &ht);
15005 md5_finish_ctx (&ctx, checksum_after_fn);
15006 ht.empty ();
15008 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
15009 fold_check_failed (fn, tem);
15011 md5_init_ctx (&ctx);
15012 for (i = 0; i < nargs; i++)
15013 fold_checksum_tree (argarray[i], &ctx, &ht);
15014 md5_finish_ctx (&ctx, checksum_after_arglist);
15016 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
15017 fold_check_failed (NULL_TREE, tem);
15018 #endif
15019 return tem;
15022 /* Perform constant folding and related simplification of initializer
15023 expression EXPR. These behave identically to "fold_buildN" but ignore
15024 potential run-time traps and exceptions that fold must preserve. */
15026 #define START_FOLD_INIT \
15027 int saved_signaling_nans = flag_signaling_nans;\
15028 int saved_trapping_math = flag_trapping_math;\
15029 int saved_rounding_math = flag_rounding_math;\
15030 int saved_trapv = flag_trapv;\
15031 int saved_folding_initializer = folding_initializer;\
15032 flag_signaling_nans = 0;\
15033 flag_trapping_math = 0;\
15034 flag_rounding_math = 0;\
15035 flag_trapv = 0;\
15036 folding_initializer = 1;
15038 #define END_FOLD_INIT \
15039 flag_signaling_nans = saved_signaling_nans;\
15040 flag_trapping_math = saved_trapping_math;\
15041 flag_rounding_math = saved_rounding_math;\
15042 flag_trapv = saved_trapv;\
15043 folding_initializer = saved_folding_initializer;
15045 tree
15046 fold_build1_initializer_loc (location_t loc, enum tree_code code,
15047 tree type, tree op)
15049 tree result;
15050 START_FOLD_INIT;
15052 result = fold_build1_loc (loc, code, type, op);
15054 END_FOLD_INIT;
15055 return result;
15058 tree
15059 fold_build2_initializer_loc (location_t loc, enum tree_code code,
15060 tree type, tree op0, tree op1)
15062 tree result;
15063 START_FOLD_INIT;
15065 result = fold_build2_loc (loc, code, type, op0, op1);
15067 END_FOLD_INIT;
15068 return result;
15071 tree
15072 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
15073 int nargs, tree *argarray)
15075 tree result;
15076 START_FOLD_INIT;
15078 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
15080 END_FOLD_INIT;
15081 return result;
15084 #undef START_FOLD_INIT
15085 #undef END_FOLD_INIT
15087 /* Determine if first argument is a multiple of second argument. Return 0 if
15088 it is not, or we cannot easily determined it to be.
15090 An example of the sort of thing we care about (at this point; this routine
15091 could surely be made more general, and expanded to do what the *_DIV_EXPR's
15092 fold cases do now) is discovering that
15094 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
15096 is a multiple of
15098 SAVE_EXPR (J * 8)
15100 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
15102 This code also handles discovering that
15104 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
15106 is a multiple of 8 so we don't have to worry about dealing with a
15107 possible remainder.
15109 Note that we *look* inside a SAVE_EXPR only to determine how it was
15110 calculated; it is not safe for fold to do much of anything else with the
15111 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
15112 at run time. For example, the latter example above *cannot* be implemented
15113 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
15114 evaluation time of the original SAVE_EXPR is not necessarily the same at
15115 the time the new expression is evaluated. The only optimization of this
15116 sort that would be valid is changing
15118 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
15120 divided by 8 to
15122 SAVE_EXPR (I) * SAVE_EXPR (J)
15124 (where the same SAVE_EXPR (J) is used in the original and the
15125 transformed version). */
15128 multiple_of_p (tree type, const_tree top, const_tree bottom)
15130 if (operand_equal_p (top, bottom, 0))
15131 return 1;
15133 if (TREE_CODE (type) != INTEGER_TYPE)
15134 return 0;
15136 switch (TREE_CODE (top))
15138 case BIT_AND_EXPR:
15139 /* Bitwise and provides a power of two multiple. If the mask is
15140 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
15141 if (!integer_pow2p (bottom))
15142 return 0;
15143 /* FALLTHRU */
15145 case MULT_EXPR:
15146 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
15147 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
15149 case PLUS_EXPR:
15150 case MINUS_EXPR:
15151 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
15152 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
15154 case LSHIFT_EXPR:
15155 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
15157 tree op1, t1;
15159 op1 = TREE_OPERAND (top, 1);
15160 /* const_binop may not detect overflow correctly,
15161 so check for it explicitly here. */
15162 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
15163 && 0 != (t1 = fold_convert (type,
15164 const_binop (LSHIFT_EXPR,
15165 size_one_node,
15166 op1)))
15167 && !TREE_OVERFLOW (t1))
15168 return multiple_of_p (type, t1, bottom);
15170 return 0;
15172 case NOP_EXPR:
15173 /* Can't handle conversions from non-integral or wider integral type. */
15174 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
15175 || (TYPE_PRECISION (type)
15176 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
15177 return 0;
15179 /* .. fall through ... */
15181 case SAVE_EXPR:
15182 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
15184 case COND_EXPR:
15185 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
15186 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
15188 case INTEGER_CST:
15189 if (TREE_CODE (bottom) != INTEGER_CST
15190 || integer_zerop (bottom)
15191 || (TYPE_UNSIGNED (type)
15192 && (tree_int_cst_sgn (top) < 0
15193 || tree_int_cst_sgn (bottom) < 0)))
15194 return 0;
15195 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
15196 SIGNED);
15198 default:
15199 return 0;
15203 /* Return true if CODE or TYPE is known to be non-negative. */
15205 static bool
15206 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
15208 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
15209 && truth_value_p (code))
15210 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
15211 have a signed:1 type (where the value is -1 and 0). */
15212 return true;
15213 return false;
15216 /* Return true if (CODE OP0) is known to be non-negative. If the return
15217 value is based on the assumption that signed overflow is undefined,
15218 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15219 *STRICT_OVERFLOW_P. */
15221 bool
15222 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
15223 bool *strict_overflow_p)
15225 if (TYPE_UNSIGNED (type))
15226 return true;
15228 switch (code)
15230 case ABS_EXPR:
15231 /* We can't return 1 if flag_wrapv is set because
15232 ABS_EXPR<INT_MIN> = INT_MIN. */
15233 if (!INTEGRAL_TYPE_P (type))
15234 return true;
15235 if (TYPE_OVERFLOW_UNDEFINED (type))
15237 *strict_overflow_p = true;
15238 return true;
15240 break;
15242 case NON_LVALUE_EXPR:
15243 case FLOAT_EXPR:
15244 case FIX_TRUNC_EXPR:
15245 return tree_expr_nonnegative_warnv_p (op0,
15246 strict_overflow_p);
15248 case NOP_EXPR:
15250 tree inner_type = TREE_TYPE (op0);
15251 tree outer_type = type;
15253 if (TREE_CODE (outer_type) == REAL_TYPE)
15255 if (TREE_CODE (inner_type) == REAL_TYPE)
15256 return tree_expr_nonnegative_warnv_p (op0,
15257 strict_overflow_p);
15258 if (INTEGRAL_TYPE_P (inner_type))
15260 if (TYPE_UNSIGNED (inner_type))
15261 return true;
15262 return tree_expr_nonnegative_warnv_p (op0,
15263 strict_overflow_p);
15266 else if (INTEGRAL_TYPE_P (outer_type))
15268 if (TREE_CODE (inner_type) == REAL_TYPE)
15269 return tree_expr_nonnegative_warnv_p (op0,
15270 strict_overflow_p);
15271 if (INTEGRAL_TYPE_P (inner_type))
15272 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
15273 && TYPE_UNSIGNED (inner_type);
15276 break;
15278 default:
15279 return tree_simple_nonnegative_warnv_p (code, type);
15282 /* We don't know sign of `t', so be conservative and return false. */
15283 return false;
15286 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
15287 value is based on the assumption that signed overflow is undefined,
15288 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15289 *STRICT_OVERFLOW_P. */
15291 bool
15292 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
15293 tree op1, bool *strict_overflow_p)
15295 if (TYPE_UNSIGNED (type))
15296 return true;
15298 switch (code)
15300 case POINTER_PLUS_EXPR:
15301 case PLUS_EXPR:
15302 if (FLOAT_TYPE_P (type))
15303 return (tree_expr_nonnegative_warnv_p (op0,
15304 strict_overflow_p)
15305 && tree_expr_nonnegative_warnv_p (op1,
15306 strict_overflow_p));
15308 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
15309 both unsigned and at least 2 bits shorter than the result. */
15310 if (TREE_CODE (type) == INTEGER_TYPE
15311 && TREE_CODE (op0) == NOP_EXPR
15312 && TREE_CODE (op1) == NOP_EXPR)
15314 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
15315 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
15316 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
15317 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
15319 unsigned int prec = MAX (TYPE_PRECISION (inner1),
15320 TYPE_PRECISION (inner2)) + 1;
15321 return prec < TYPE_PRECISION (type);
15324 break;
15326 case MULT_EXPR:
15327 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
15329 /* x * x is always non-negative for floating point x
15330 or without overflow. */
15331 if (operand_equal_p (op0, op1, 0)
15332 || (tree_expr_nonnegative_warnv_p (op0, strict_overflow_p)
15333 && tree_expr_nonnegative_warnv_p (op1, strict_overflow_p)))
15335 if (TYPE_OVERFLOW_UNDEFINED (type))
15336 *strict_overflow_p = true;
15337 return true;
15341 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
15342 both unsigned and their total bits is shorter than the result. */
15343 if (TREE_CODE (type) == INTEGER_TYPE
15344 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
15345 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
15347 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
15348 ? TREE_TYPE (TREE_OPERAND (op0, 0))
15349 : TREE_TYPE (op0);
15350 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
15351 ? TREE_TYPE (TREE_OPERAND (op1, 0))
15352 : TREE_TYPE (op1);
15354 bool unsigned0 = TYPE_UNSIGNED (inner0);
15355 bool unsigned1 = TYPE_UNSIGNED (inner1);
15357 if (TREE_CODE (op0) == INTEGER_CST)
15358 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
15360 if (TREE_CODE (op1) == INTEGER_CST)
15361 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
15363 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
15364 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
15366 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
15367 ? tree_int_cst_min_precision (op0, UNSIGNED)
15368 : TYPE_PRECISION (inner0);
15370 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
15371 ? tree_int_cst_min_precision (op1, UNSIGNED)
15372 : TYPE_PRECISION (inner1);
15374 return precision0 + precision1 < TYPE_PRECISION (type);
15377 return false;
15379 case BIT_AND_EXPR:
15380 case MAX_EXPR:
15381 return (tree_expr_nonnegative_warnv_p (op0,
15382 strict_overflow_p)
15383 || tree_expr_nonnegative_warnv_p (op1,
15384 strict_overflow_p));
15386 case BIT_IOR_EXPR:
15387 case BIT_XOR_EXPR:
15388 case MIN_EXPR:
15389 case RDIV_EXPR:
15390 case TRUNC_DIV_EXPR:
15391 case CEIL_DIV_EXPR:
15392 case FLOOR_DIV_EXPR:
15393 case ROUND_DIV_EXPR:
15394 return (tree_expr_nonnegative_warnv_p (op0,
15395 strict_overflow_p)
15396 && tree_expr_nonnegative_warnv_p (op1,
15397 strict_overflow_p));
15399 case TRUNC_MOD_EXPR:
15400 case CEIL_MOD_EXPR:
15401 case FLOOR_MOD_EXPR:
15402 case ROUND_MOD_EXPR:
15403 return tree_expr_nonnegative_warnv_p (op0,
15404 strict_overflow_p);
15405 default:
15406 return tree_simple_nonnegative_warnv_p (code, type);
15409 /* We don't know sign of `t', so be conservative and return false. */
15410 return false;
15413 /* Return true if T is known to be non-negative. If the return
15414 value is based on the assumption that signed overflow is undefined,
15415 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15416 *STRICT_OVERFLOW_P. */
15418 bool
15419 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15421 if (TYPE_UNSIGNED (TREE_TYPE (t)))
15422 return true;
15424 switch (TREE_CODE (t))
15426 case INTEGER_CST:
15427 return tree_int_cst_sgn (t) >= 0;
15429 case REAL_CST:
15430 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
15432 case FIXED_CST:
15433 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
15435 case COND_EXPR:
15436 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15437 strict_overflow_p)
15438 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
15439 strict_overflow_p));
15440 default:
15441 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15442 TREE_TYPE (t));
15444 /* We don't know sign of `t', so be conservative and return false. */
15445 return false;
15448 /* Return true if T is known to be non-negative. If the return
15449 value is based on the assumption that signed overflow is undefined,
15450 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15451 *STRICT_OVERFLOW_P. */
15453 bool
15454 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
15455 tree arg0, tree arg1, bool *strict_overflow_p)
15457 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
15458 switch (DECL_FUNCTION_CODE (fndecl))
15460 CASE_FLT_FN (BUILT_IN_ACOS):
15461 CASE_FLT_FN (BUILT_IN_ACOSH):
15462 CASE_FLT_FN (BUILT_IN_CABS):
15463 CASE_FLT_FN (BUILT_IN_COSH):
15464 CASE_FLT_FN (BUILT_IN_ERFC):
15465 CASE_FLT_FN (BUILT_IN_EXP):
15466 CASE_FLT_FN (BUILT_IN_EXP10):
15467 CASE_FLT_FN (BUILT_IN_EXP2):
15468 CASE_FLT_FN (BUILT_IN_FABS):
15469 CASE_FLT_FN (BUILT_IN_FDIM):
15470 CASE_FLT_FN (BUILT_IN_HYPOT):
15471 CASE_FLT_FN (BUILT_IN_POW10):
15472 CASE_INT_FN (BUILT_IN_FFS):
15473 CASE_INT_FN (BUILT_IN_PARITY):
15474 CASE_INT_FN (BUILT_IN_POPCOUNT):
15475 CASE_INT_FN (BUILT_IN_CLZ):
15476 CASE_INT_FN (BUILT_IN_CLRSB):
15477 case BUILT_IN_BSWAP32:
15478 case BUILT_IN_BSWAP64:
15479 /* Always true. */
15480 return true;
15482 CASE_FLT_FN (BUILT_IN_SQRT):
15483 /* sqrt(-0.0) is -0.0. */
15484 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
15485 return true;
15486 return tree_expr_nonnegative_warnv_p (arg0,
15487 strict_overflow_p);
15489 CASE_FLT_FN (BUILT_IN_ASINH):
15490 CASE_FLT_FN (BUILT_IN_ATAN):
15491 CASE_FLT_FN (BUILT_IN_ATANH):
15492 CASE_FLT_FN (BUILT_IN_CBRT):
15493 CASE_FLT_FN (BUILT_IN_CEIL):
15494 CASE_FLT_FN (BUILT_IN_ERF):
15495 CASE_FLT_FN (BUILT_IN_EXPM1):
15496 CASE_FLT_FN (BUILT_IN_FLOOR):
15497 CASE_FLT_FN (BUILT_IN_FMOD):
15498 CASE_FLT_FN (BUILT_IN_FREXP):
15499 CASE_FLT_FN (BUILT_IN_ICEIL):
15500 CASE_FLT_FN (BUILT_IN_IFLOOR):
15501 CASE_FLT_FN (BUILT_IN_IRINT):
15502 CASE_FLT_FN (BUILT_IN_IROUND):
15503 CASE_FLT_FN (BUILT_IN_LCEIL):
15504 CASE_FLT_FN (BUILT_IN_LDEXP):
15505 CASE_FLT_FN (BUILT_IN_LFLOOR):
15506 CASE_FLT_FN (BUILT_IN_LLCEIL):
15507 CASE_FLT_FN (BUILT_IN_LLFLOOR):
15508 CASE_FLT_FN (BUILT_IN_LLRINT):
15509 CASE_FLT_FN (BUILT_IN_LLROUND):
15510 CASE_FLT_FN (BUILT_IN_LRINT):
15511 CASE_FLT_FN (BUILT_IN_LROUND):
15512 CASE_FLT_FN (BUILT_IN_MODF):
15513 CASE_FLT_FN (BUILT_IN_NEARBYINT):
15514 CASE_FLT_FN (BUILT_IN_RINT):
15515 CASE_FLT_FN (BUILT_IN_ROUND):
15516 CASE_FLT_FN (BUILT_IN_SCALB):
15517 CASE_FLT_FN (BUILT_IN_SCALBLN):
15518 CASE_FLT_FN (BUILT_IN_SCALBN):
15519 CASE_FLT_FN (BUILT_IN_SIGNBIT):
15520 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
15521 CASE_FLT_FN (BUILT_IN_SINH):
15522 CASE_FLT_FN (BUILT_IN_TANH):
15523 CASE_FLT_FN (BUILT_IN_TRUNC):
15524 /* True if the 1st argument is nonnegative. */
15525 return tree_expr_nonnegative_warnv_p (arg0,
15526 strict_overflow_p);
15528 CASE_FLT_FN (BUILT_IN_FMAX):
15529 /* True if the 1st OR 2nd arguments are nonnegative. */
15530 return (tree_expr_nonnegative_warnv_p (arg0,
15531 strict_overflow_p)
15532 || (tree_expr_nonnegative_warnv_p (arg1,
15533 strict_overflow_p)));
15535 CASE_FLT_FN (BUILT_IN_FMIN):
15536 /* True if the 1st AND 2nd arguments are nonnegative. */
15537 return (tree_expr_nonnegative_warnv_p (arg0,
15538 strict_overflow_p)
15539 && (tree_expr_nonnegative_warnv_p (arg1,
15540 strict_overflow_p)));
15542 CASE_FLT_FN (BUILT_IN_COPYSIGN):
15543 /* True if the 2nd argument is nonnegative. */
15544 return tree_expr_nonnegative_warnv_p (arg1,
15545 strict_overflow_p);
15547 CASE_FLT_FN (BUILT_IN_POWI):
15548 /* True if the 1st argument is nonnegative or the second
15549 argument is an even integer. */
15550 if (TREE_CODE (arg1) == INTEGER_CST
15551 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15552 return true;
15553 return tree_expr_nonnegative_warnv_p (arg0,
15554 strict_overflow_p);
15556 CASE_FLT_FN (BUILT_IN_POW):
15557 /* True if the 1st argument is nonnegative or the second
15558 argument is an even integer valued real. */
15559 if (TREE_CODE (arg1) == REAL_CST)
15561 REAL_VALUE_TYPE c;
15562 HOST_WIDE_INT n;
15564 c = TREE_REAL_CST (arg1);
15565 n = real_to_integer (&c);
15566 if ((n & 1) == 0)
15568 REAL_VALUE_TYPE cint;
15569 real_from_integer (&cint, VOIDmode, n, SIGNED);
15570 if (real_identical (&c, &cint))
15571 return true;
15574 return tree_expr_nonnegative_warnv_p (arg0,
15575 strict_overflow_p);
15577 default:
15578 break;
15580 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
15581 type);
15584 /* Return true if T is known to be non-negative. If the return
15585 value is based on the assumption that signed overflow is undefined,
15586 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15587 *STRICT_OVERFLOW_P. */
15589 static bool
15590 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15592 enum tree_code code = TREE_CODE (t);
15593 if (TYPE_UNSIGNED (TREE_TYPE (t)))
15594 return true;
15596 switch (code)
15598 case TARGET_EXPR:
15600 tree temp = TARGET_EXPR_SLOT (t);
15601 t = TARGET_EXPR_INITIAL (t);
15603 /* If the initializer is non-void, then it's a normal expression
15604 that will be assigned to the slot. */
15605 if (!VOID_TYPE_P (t))
15606 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
15608 /* Otherwise, the initializer sets the slot in some way. One common
15609 way is an assignment statement at the end of the initializer. */
15610 while (1)
15612 if (TREE_CODE (t) == BIND_EXPR)
15613 t = expr_last (BIND_EXPR_BODY (t));
15614 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15615 || TREE_CODE (t) == TRY_CATCH_EXPR)
15616 t = expr_last (TREE_OPERAND (t, 0));
15617 else if (TREE_CODE (t) == STATEMENT_LIST)
15618 t = expr_last (t);
15619 else
15620 break;
15622 if (TREE_CODE (t) == MODIFY_EXPR
15623 && TREE_OPERAND (t, 0) == temp)
15624 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15625 strict_overflow_p);
15627 return false;
15630 case CALL_EXPR:
15632 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15633 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15635 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15636 get_callee_fndecl (t),
15637 arg0,
15638 arg1,
15639 strict_overflow_p);
15641 case COMPOUND_EXPR:
15642 case MODIFY_EXPR:
15643 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15644 strict_overflow_p);
15645 case BIND_EXPR:
15646 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
15647 strict_overflow_p);
15648 case SAVE_EXPR:
15649 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
15650 strict_overflow_p);
15652 default:
15653 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15654 TREE_TYPE (t));
15657 /* We don't know sign of `t', so be conservative and return false. */
15658 return false;
15661 /* Return true if T is known to be non-negative. If the return
15662 value is based on the assumption that signed overflow is undefined,
15663 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15664 *STRICT_OVERFLOW_P. */
15666 bool
15667 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15669 enum tree_code code;
15670 if (t == error_mark_node)
15671 return false;
15673 code = TREE_CODE (t);
15674 switch (TREE_CODE_CLASS (code))
15676 case tcc_binary:
15677 case tcc_comparison:
15678 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15679 TREE_TYPE (t),
15680 TREE_OPERAND (t, 0),
15681 TREE_OPERAND (t, 1),
15682 strict_overflow_p);
15684 case tcc_unary:
15685 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15686 TREE_TYPE (t),
15687 TREE_OPERAND (t, 0),
15688 strict_overflow_p);
15690 case tcc_constant:
15691 case tcc_declaration:
15692 case tcc_reference:
15693 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15695 default:
15696 break;
15699 switch (code)
15701 case TRUTH_AND_EXPR:
15702 case TRUTH_OR_EXPR:
15703 case TRUTH_XOR_EXPR:
15704 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15705 TREE_TYPE (t),
15706 TREE_OPERAND (t, 0),
15707 TREE_OPERAND (t, 1),
15708 strict_overflow_p);
15709 case TRUTH_NOT_EXPR:
15710 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15711 TREE_TYPE (t),
15712 TREE_OPERAND (t, 0),
15713 strict_overflow_p);
15715 case COND_EXPR:
15716 case CONSTRUCTOR:
15717 case OBJ_TYPE_REF:
15718 case ASSERT_EXPR:
15719 case ADDR_EXPR:
15720 case WITH_SIZE_EXPR:
15721 case SSA_NAME:
15722 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15724 default:
15725 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
15729 /* Return true if `t' is known to be non-negative. Handle warnings
15730 about undefined signed overflow. */
15732 bool
15733 tree_expr_nonnegative_p (tree t)
15735 bool ret, strict_overflow_p;
15737 strict_overflow_p = false;
15738 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15739 if (strict_overflow_p)
15740 fold_overflow_warning (("assuming signed overflow does not occur when "
15741 "determining that expression is always "
15742 "non-negative"),
15743 WARN_STRICT_OVERFLOW_MISC);
15744 return ret;
15748 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15749 For floating point we further ensure that T is not denormal.
15750 Similar logic is present in nonzero_address in rtlanal.h.
15752 If the return value is based on the assumption that signed overflow
15753 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15754 change *STRICT_OVERFLOW_P. */
15756 bool
15757 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15758 bool *strict_overflow_p)
15760 switch (code)
15762 case ABS_EXPR:
15763 return tree_expr_nonzero_warnv_p (op0,
15764 strict_overflow_p);
15766 case NOP_EXPR:
15768 tree inner_type = TREE_TYPE (op0);
15769 tree outer_type = type;
15771 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15772 && tree_expr_nonzero_warnv_p (op0,
15773 strict_overflow_p));
15775 break;
15777 case NON_LVALUE_EXPR:
15778 return tree_expr_nonzero_warnv_p (op0,
15779 strict_overflow_p);
15781 default:
15782 break;
15785 return false;
15788 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15789 For floating point we further ensure that T is not denormal.
15790 Similar logic is present in nonzero_address in rtlanal.h.
15792 If the return value is based on the assumption that signed overflow
15793 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15794 change *STRICT_OVERFLOW_P. */
15796 bool
15797 tree_binary_nonzero_warnv_p (enum tree_code code,
15798 tree type,
15799 tree op0,
15800 tree op1, bool *strict_overflow_p)
15802 bool sub_strict_overflow_p;
15803 switch (code)
15805 case POINTER_PLUS_EXPR:
15806 case PLUS_EXPR:
15807 if (TYPE_OVERFLOW_UNDEFINED (type))
15809 /* With the presence of negative values it is hard
15810 to say something. */
15811 sub_strict_overflow_p = false;
15812 if (!tree_expr_nonnegative_warnv_p (op0,
15813 &sub_strict_overflow_p)
15814 || !tree_expr_nonnegative_warnv_p (op1,
15815 &sub_strict_overflow_p))
15816 return false;
15817 /* One of operands must be positive and the other non-negative. */
15818 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15819 overflows, on a twos-complement machine the sum of two
15820 nonnegative numbers can never be zero. */
15821 return (tree_expr_nonzero_warnv_p (op0,
15822 strict_overflow_p)
15823 || tree_expr_nonzero_warnv_p (op1,
15824 strict_overflow_p));
15826 break;
15828 case MULT_EXPR:
15829 if (TYPE_OVERFLOW_UNDEFINED (type))
15831 if (tree_expr_nonzero_warnv_p (op0,
15832 strict_overflow_p)
15833 && tree_expr_nonzero_warnv_p (op1,
15834 strict_overflow_p))
15836 *strict_overflow_p = true;
15837 return true;
15840 break;
15842 case MIN_EXPR:
15843 sub_strict_overflow_p = false;
15844 if (tree_expr_nonzero_warnv_p (op0,
15845 &sub_strict_overflow_p)
15846 && tree_expr_nonzero_warnv_p (op1,
15847 &sub_strict_overflow_p))
15849 if (sub_strict_overflow_p)
15850 *strict_overflow_p = true;
15852 break;
15854 case MAX_EXPR:
15855 sub_strict_overflow_p = false;
15856 if (tree_expr_nonzero_warnv_p (op0,
15857 &sub_strict_overflow_p))
15859 if (sub_strict_overflow_p)
15860 *strict_overflow_p = true;
15862 /* When both operands are nonzero, then MAX must be too. */
15863 if (tree_expr_nonzero_warnv_p (op1,
15864 strict_overflow_p))
15865 return true;
15867 /* MAX where operand 0 is positive is positive. */
15868 return tree_expr_nonnegative_warnv_p (op0,
15869 strict_overflow_p);
15871 /* MAX where operand 1 is positive is positive. */
15872 else if (tree_expr_nonzero_warnv_p (op1,
15873 &sub_strict_overflow_p)
15874 && tree_expr_nonnegative_warnv_p (op1,
15875 &sub_strict_overflow_p))
15877 if (sub_strict_overflow_p)
15878 *strict_overflow_p = true;
15879 return true;
15881 break;
15883 case BIT_IOR_EXPR:
15884 return (tree_expr_nonzero_warnv_p (op1,
15885 strict_overflow_p)
15886 || tree_expr_nonzero_warnv_p (op0,
15887 strict_overflow_p));
15889 default:
15890 break;
15893 return false;
15896 /* Return true when T is an address and is known to be nonzero.
15897 For floating point we further ensure that T is not denormal.
15898 Similar logic is present in nonzero_address in rtlanal.h.
15900 If the return value is based on the assumption that signed overflow
15901 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15902 change *STRICT_OVERFLOW_P. */
15904 bool
15905 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15907 bool sub_strict_overflow_p;
15908 switch (TREE_CODE (t))
15910 case INTEGER_CST:
15911 return !integer_zerop (t);
15913 case ADDR_EXPR:
15915 tree base = TREE_OPERAND (t, 0);
15917 if (!DECL_P (base))
15918 base = get_base_address (base);
15920 if (!base)
15921 return false;
15923 /* For objects in symbol table check if we know they are non-zero.
15924 Don't do anything for variables and functions before symtab is built;
15925 it is quite possible that they will be declared weak later. */
15926 if (DECL_P (base) && decl_in_symtab_p (base))
15928 struct symtab_node *symbol;
15930 symbol = symtab_node::get_create (base);
15931 if (symbol)
15932 return symbol->nonzero_address ();
15933 else
15934 return false;
15937 /* Function local objects are never NULL. */
15938 if (DECL_P (base)
15939 && (DECL_CONTEXT (base)
15940 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
15941 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
15942 return true;
15944 /* Constants are never weak. */
15945 if (CONSTANT_CLASS_P (base))
15946 return true;
15948 return false;
15951 case COND_EXPR:
15952 sub_strict_overflow_p = false;
15953 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15954 &sub_strict_overflow_p)
15955 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15956 &sub_strict_overflow_p))
15958 if (sub_strict_overflow_p)
15959 *strict_overflow_p = true;
15960 return true;
15962 break;
15964 default:
15965 break;
15967 return false;
15970 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15971 attempt to fold the expression to a constant without modifying TYPE,
15972 OP0 or OP1.
15974 If the expression could be simplified to a constant, then return
15975 the constant. If the expression would not be simplified to a
15976 constant, then return NULL_TREE. */
15978 tree
15979 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15981 tree tem = fold_binary (code, type, op0, op1);
15982 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15985 /* Given the components of a unary expression CODE, TYPE and OP0,
15986 attempt to fold the expression to a constant without modifying
15987 TYPE or OP0.
15989 If the expression could be simplified to a constant, then return
15990 the constant. If the expression would not be simplified to a
15991 constant, then return NULL_TREE. */
15993 tree
15994 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15996 tree tem = fold_unary (code, type, op0);
15997 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
16000 /* If EXP represents referencing an element in a constant string
16001 (either via pointer arithmetic or array indexing), return the
16002 tree representing the value accessed, otherwise return NULL. */
16004 tree
16005 fold_read_from_constant_string (tree exp)
16007 if ((TREE_CODE (exp) == INDIRECT_REF
16008 || TREE_CODE (exp) == ARRAY_REF)
16009 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
16011 tree exp1 = TREE_OPERAND (exp, 0);
16012 tree index;
16013 tree string;
16014 location_t loc = EXPR_LOCATION (exp);
16016 if (TREE_CODE (exp) == INDIRECT_REF)
16017 string = string_constant (exp1, &index);
16018 else
16020 tree low_bound = array_ref_low_bound (exp);
16021 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
16023 /* Optimize the special-case of a zero lower bound.
16025 We convert the low_bound to sizetype to avoid some problems
16026 with constant folding. (E.g. suppose the lower bound is 1,
16027 and its mode is QI. Without the conversion,l (ARRAY
16028 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
16029 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
16030 if (! integer_zerop (low_bound))
16031 index = size_diffop_loc (loc, index,
16032 fold_convert_loc (loc, sizetype, low_bound));
16034 string = exp1;
16037 if (string
16038 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
16039 && TREE_CODE (string) == STRING_CST
16040 && TREE_CODE (index) == INTEGER_CST
16041 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
16042 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
16043 == MODE_INT)
16044 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
16045 return build_int_cst_type (TREE_TYPE (exp),
16046 (TREE_STRING_POINTER (string)
16047 [TREE_INT_CST_LOW (index)]));
16049 return NULL;
16052 /* Return the tree for neg (ARG0) when ARG0 is known to be either
16053 an integer constant, real, or fixed-point constant.
16055 TYPE is the type of the result. */
16057 static tree
16058 fold_negate_const (tree arg0, tree type)
16060 tree t = NULL_TREE;
16062 switch (TREE_CODE (arg0))
16064 case INTEGER_CST:
16066 bool overflow;
16067 wide_int val = wi::neg (arg0, &overflow);
16068 t = force_fit_type (type, val, 1,
16069 (overflow | TREE_OVERFLOW (arg0))
16070 && !TYPE_UNSIGNED (type));
16071 break;
16074 case REAL_CST:
16075 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
16076 break;
16078 case FIXED_CST:
16080 FIXED_VALUE_TYPE f;
16081 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
16082 &(TREE_FIXED_CST (arg0)), NULL,
16083 TYPE_SATURATING (type));
16084 t = build_fixed (type, f);
16085 /* Propagate overflow flags. */
16086 if (overflow_p | TREE_OVERFLOW (arg0))
16087 TREE_OVERFLOW (t) = 1;
16088 break;
16091 default:
16092 gcc_unreachable ();
16095 return t;
16098 /* Return the tree for abs (ARG0) when ARG0 is known to be either
16099 an integer constant or real constant.
16101 TYPE is the type of the result. */
16103 tree
16104 fold_abs_const (tree arg0, tree type)
16106 tree t = NULL_TREE;
16108 switch (TREE_CODE (arg0))
16110 case INTEGER_CST:
16112 /* If the value is unsigned or non-negative, then the absolute value
16113 is the same as the ordinary value. */
16114 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
16115 t = arg0;
16117 /* If the value is negative, then the absolute value is
16118 its negation. */
16119 else
16121 bool overflow;
16122 wide_int val = wi::neg (arg0, &overflow);
16123 t = force_fit_type (type, val, -1,
16124 overflow | TREE_OVERFLOW (arg0));
16127 break;
16129 case REAL_CST:
16130 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
16131 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
16132 else
16133 t = arg0;
16134 break;
16136 default:
16137 gcc_unreachable ();
16140 return t;
16143 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
16144 constant. TYPE is the type of the result. */
16146 static tree
16147 fold_not_const (const_tree arg0, tree type)
16149 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
16151 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
16154 /* Given CODE, a relational operator, the target type, TYPE and two
16155 constant operands OP0 and OP1, return the result of the
16156 relational operation. If the result is not a compile time
16157 constant, then return NULL_TREE. */
16159 static tree
16160 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
16162 int result, invert;
16164 /* From here on, the only cases we handle are when the result is
16165 known to be a constant. */
16167 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
16169 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
16170 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
16172 /* Handle the cases where either operand is a NaN. */
16173 if (real_isnan (c0) || real_isnan (c1))
16175 switch (code)
16177 case EQ_EXPR:
16178 case ORDERED_EXPR:
16179 result = 0;
16180 break;
16182 case NE_EXPR:
16183 case UNORDERED_EXPR:
16184 case UNLT_EXPR:
16185 case UNLE_EXPR:
16186 case UNGT_EXPR:
16187 case UNGE_EXPR:
16188 case UNEQ_EXPR:
16189 result = 1;
16190 break;
16192 case LT_EXPR:
16193 case LE_EXPR:
16194 case GT_EXPR:
16195 case GE_EXPR:
16196 case LTGT_EXPR:
16197 if (flag_trapping_math)
16198 return NULL_TREE;
16199 result = 0;
16200 break;
16202 default:
16203 gcc_unreachable ();
16206 return constant_boolean_node (result, type);
16209 return constant_boolean_node (real_compare (code, c0, c1), type);
16212 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
16214 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
16215 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
16216 return constant_boolean_node (fixed_compare (code, c0, c1), type);
16219 /* Handle equality/inequality of complex constants. */
16220 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
16222 tree rcond = fold_relational_const (code, type,
16223 TREE_REALPART (op0),
16224 TREE_REALPART (op1));
16225 tree icond = fold_relational_const (code, type,
16226 TREE_IMAGPART (op0),
16227 TREE_IMAGPART (op1));
16228 if (code == EQ_EXPR)
16229 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
16230 else if (code == NE_EXPR)
16231 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
16232 else
16233 return NULL_TREE;
16236 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
16238 unsigned count = VECTOR_CST_NELTS (op0);
16239 tree *elts = XALLOCAVEC (tree, count);
16240 gcc_assert (VECTOR_CST_NELTS (op1) == count
16241 && TYPE_VECTOR_SUBPARTS (type) == count);
16243 for (unsigned i = 0; i < count; i++)
16245 tree elem_type = TREE_TYPE (type);
16246 tree elem0 = VECTOR_CST_ELT (op0, i);
16247 tree elem1 = VECTOR_CST_ELT (op1, i);
16249 tree tem = fold_relational_const (code, elem_type,
16250 elem0, elem1);
16252 if (tem == NULL_TREE)
16253 return NULL_TREE;
16255 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
16258 return build_vector (type, elts);
16261 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
16263 To compute GT, swap the arguments and do LT.
16264 To compute GE, do LT and invert the result.
16265 To compute LE, swap the arguments, do LT and invert the result.
16266 To compute NE, do EQ and invert the result.
16268 Therefore, the code below must handle only EQ and LT. */
16270 if (code == LE_EXPR || code == GT_EXPR)
16272 tree tem = op0;
16273 op0 = op1;
16274 op1 = tem;
16275 code = swap_tree_comparison (code);
16278 /* Note that it is safe to invert for real values here because we
16279 have already handled the one case that it matters. */
16281 invert = 0;
16282 if (code == NE_EXPR || code == GE_EXPR)
16284 invert = 1;
16285 code = invert_tree_comparison (code, false);
16288 /* Compute a result for LT or EQ if args permit;
16289 Otherwise return T. */
16290 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
16292 if (code == EQ_EXPR)
16293 result = tree_int_cst_equal (op0, op1);
16294 else
16295 result = tree_int_cst_lt (op0, op1);
16297 else
16298 return NULL_TREE;
16300 if (invert)
16301 result ^= 1;
16302 return constant_boolean_node (result, type);
16305 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
16306 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
16307 itself. */
16309 tree
16310 fold_build_cleanup_point_expr (tree type, tree expr)
16312 /* If the expression does not have side effects then we don't have to wrap
16313 it with a cleanup point expression. */
16314 if (!TREE_SIDE_EFFECTS (expr))
16315 return expr;
16317 /* If the expression is a return, check to see if the expression inside the
16318 return has no side effects or the right hand side of the modify expression
16319 inside the return. If either don't have side effects set we don't need to
16320 wrap the expression in a cleanup point expression. Note we don't check the
16321 left hand side of the modify because it should always be a return decl. */
16322 if (TREE_CODE (expr) == RETURN_EXPR)
16324 tree op = TREE_OPERAND (expr, 0);
16325 if (!op || !TREE_SIDE_EFFECTS (op))
16326 return expr;
16327 op = TREE_OPERAND (op, 1);
16328 if (!TREE_SIDE_EFFECTS (op))
16329 return expr;
16332 return build1 (CLEANUP_POINT_EXPR, type, expr);
16335 /* Given a pointer value OP0 and a type TYPE, return a simplified version
16336 of an indirection through OP0, or NULL_TREE if no simplification is
16337 possible. */
16339 tree
16340 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16342 tree sub = op0;
16343 tree subtype;
16345 STRIP_NOPS (sub);
16346 subtype = TREE_TYPE (sub);
16347 if (!POINTER_TYPE_P (subtype))
16348 return NULL_TREE;
16350 if (TREE_CODE (sub) == ADDR_EXPR)
16352 tree op = TREE_OPERAND (sub, 0);
16353 tree optype = TREE_TYPE (op);
16354 /* *&CONST_DECL -> to the value of the const decl. */
16355 if (TREE_CODE (op) == CONST_DECL)
16356 return DECL_INITIAL (op);
16357 /* *&p => p; make sure to handle *&"str"[cst] here. */
16358 if (type == optype)
16360 tree fop = fold_read_from_constant_string (op);
16361 if (fop)
16362 return fop;
16363 else
16364 return op;
16366 /* *(foo *)&fooarray => fooarray[0] */
16367 else if (TREE_CODE (optype) == ARRAY_TYPE
16368 && type == TREE_TYPE (optype)
16369 && (!in_gimple_form
16370 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16372 tree type_domain = TYPE_DOMAIN (optype);
16373 tree min_val = size_zero_node;
16374 if (type_domain && TYPE_MIN_VALUE (type_domain))
16375 min_val = TYPE_MIN_VALUE (type_domain);
16376 if (in_gimple_form
16377 && TREE_CODE (min_val) != INTEGER_CST)
16378 return NULL_TREE;
16379 return build4_loc (loc, ARRAY_REF, type, op, min_val,
16380 NULL_TREE, NULL_TREE);
16382 /* *(foo *)&complexfoo => __real__ complexfoo */
16383 else if (TREE_CODE (optype) == COMPLEX_TYPE
16384 && type == TREE_TYPE (optype))
16385 return fold_build1_loc (loc, REALPART_EXPR, type, op);
16386 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16387 else if (TREE_CODE (optype) == VECTOR_TYPE
16388 && type == TREE_TYPE (optype))
16390 tree part_width = TYPE_SIZE (type);
16391 tree index = bitsize_int (0);
16392 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
16396 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16397 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
16399 tree op00 = TREE_OPERAND (sub, 0);
16400 tree op01 = TREE_OPERAND (sub, 1);
16402 STRIP_NOPS (op00);
16403 if (TREE_CODE (op00) == ADDR_EXPR)
16405 tree op00type;
16406 op00 = TREE_OPERAND (op00, 0);
16407 op00type = TREE_TYPE (op00);
16409 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16410 if (TREE_CODE (op00type) == VECTOR_TYPE
16411 && type == TREE_TYPE (op00type))
16413 HOST_WIDE_INT offset = tree_to_shwi (op01);
16414 tree part_width = TYPE_SIZE (type);
16415 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
16416 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
16417 tree index = bitsize_int (indexi);
16419 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
16420 return fold_build3_loc (loc,
16421 BIT_FIELD_REF, type, op00,
16422 part_width, index);
16425 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16426 else if (TREE_CODE (op00type) == COMPLEX_TYPE
16427 && type == TREE_TYPE (op00type))
16429 tree size = TYPE_SIZE_UNIT (type);
16430 if (tree_int_cst_equal (size, op01))
16431 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16433 /* ((foo *)&fooarray)[1] => fooarray[1] */
16434 else if (TREE_CODE (op00type) == ARRAY_TYPE
16435 && type == TREE_TYPE (op00type))
16437 tree type_domain = TYPE_DOMAIN (op00type);
16438 tree min_val = size_zero_node;
16439 if (type_domain && TYPE_MIN_VALUE (type_domain))
16440 min_val = TYPE_MIN_VALUE (type_domain);
16441 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
16442 TYPE_SIZE_UNIT (type));
16443 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
16444 return build4_loc (loc, ARRAY_REF, type, op00, op01,
16445 NULL_TREE, NULL_TREE);
16450 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16451 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16452 && type == TREE_TYPE (TREE_TYPE (subtype))
16453 && (!in_gimple_form
16454 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16456 tree type_domain;
16457 tree min_val = size_zero_node;
16458 sub = build_fold_indirect_ref_loc (loc, sub);
16459 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16460 if (type_domain && TYPE_MIN_VALUE (type_domain))
16461 min_val = TYPE_MIN_VALUE (type_domain);
16462 if (in_gimple_form
16463 && TREE_CODE (min_val) != INTEGER_CST)
16464 return NULL_TREE;
16465 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16466 NULL_TREE);
16469 return NULL_TREE;
16472 /* Builds an expression for an indirection through T, simplifying some
16473 cases. */
16475 tree
16476 build_fold_indirect_ref_loc (location_t loc, tree t)
16478 tree type = TREE_TYPE (TREE_TYPE (t));
16479 tree sub = fold_indirect_ref_1 (loc, type, t);
16481 if (sub)
16482 return sub;
16484 return build1_loc (loc, INDIRECT_REF, type, t);
16487 /* Given an INDIRECT_REF T, return either T or a simplified version. */
16489 tree
16490 fold_indirect_ref_loc (location_t loc, tree t)
16492 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16494 if (sub)
16495 return sub;
16496 else
16497 return t;
16500 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16501 whose result is ignored. The type of the returned tree need not be
16502 the same as the original expression. */
16504 tree
16505 fold_ignored_result (tree t)
16507 if (!TREE_SIDE_EFFECTS (t))
16508 return integer_zero_node;
16510 for (;;)
16511 switch (TREE_CODE_CLASS (TREE_CODE (t)))
16513 case tcc_unary:
16514 t = TREE_OPERAND (t, 0);
16515 break;
16517 case tcc_binary:
16518 case tcc_comparison:
16519 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16520 t = TREE_OPERAND (t, 0);
16521 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16522 t = TREE_OPERAND (t, 1);
16523 else
16524 return t;
16525 break;
16527 case tcc_expression:
16528 switch (TREE_CODE (t))
16530 case COMPOUND_EXPR:
16531 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16532 return t;
16533 t = TREE_OPERAND (t, 0);
16534 break;
16536 case COND_EXPR:
16537 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16538 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16539 return t;
16540 t = TREE_OPERAND (t, 0);
16541 break;
16543 default:
16544 return t;
16546 break;
16548 default:
16549 return t;
16553 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16555 tree
16556 round_up_loc (location_t loc, tree value, unsigned int divisor)
16558 tree div = NULL_TREE;
16560 if (divisor == 1)
16561 return value;
16563 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16564 have to do anything. Only do this when we are not given a const,
16565 because in that case, this check is more expensive than just
16566 doing it. */
16567 if (TREE_CODE (value) != INTEGER_CST)
16569 div = build_int_cst (TREE_TYPE (value), divisor);
16571 if (multiple_of_p (TREE_TYPE (value), value, div))
16572 return value;
16575 /* If divisor is a power of two, simplify this to bit manipulation. */
16576 if (divisor == (divisor & -divisor))
16578 if (TREE_CODE (value) == INTEGER_CST)
16580 wide_int val = value;
16581 bool overflow_p;
16583 if ((val & (divisor - 1)) == 0)
16584 return value;
16586 overflow_p = TREE_OVERFLOW (value);
16587 val &= ~(divisor - 1);
16588 val += divisor;
16589 if (val == 0)
16590 overflow_p = true;
16592 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16594 else
16596 tree t;
16598 t = build_int_cst (TREE_TYPE (value), divisor - 1);
16599 value = size_binop_loc (loc, PLUS_EXPR, value, t);
16600 t = build_int_cst (TREE_TYPE (value), -divisor);
16601 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16604 else
16606 if (!div)
16607 div = build_int_cst (TREE_TYPE (value), divisor);
16608 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16609 value = size_binop_loc (loc, MULT_EXPR, value, div);
16612 return value;
16615 /* Likewise, but round down. */
16617 tree
16618 round_down_loc (location_t loc, tree value, int divisor)
16620 tree div = NULL_TREE;
16622 gcc_assert (divisor > 0);
16623 if (divisor == 1)
16624 return value;
16626 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16627 have to do anything. Only do this when we are not given a const,
16628 because in that case, this check is more expensive than just
16629 doing it. */
16630 if (TREE_CODE (value) != INTEGER_CST)
16632 div = build_int_cst (TREE_TYPE (value), divisor);
16634 if (multiple_of_p (TREE_TYPE (value), value, div))
16635 return value;
16638 /* If divisor is a power of two, simplify this to bit manipulation. */
16639 if (divisor == (divisor & -divisor))
16641 tree t;
16643 t = build_int_cst (TREE_TYPE (value), -divisor);
16644 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16646 else
16648 if (!div)
16649 div = build_int_cst (TREE_TYPE (value), divisor);
16650 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16651 value = size_binop_loc (loc, MULT_EXPR, value, div);
16654 return value;
16657 /* Returns the pointer to the base of the object addressed by EXP and
16658 extracts the information about the offset of the access, storing it
16659 to PBITPOS and POFFSET. */
16661 static tree
16662 split_address_to_core_and_offset (tree exp,
16663 HOST_WIDE_INT *pbitpos, tree *poffset)
16665 tree core;
16666 enum machine_mode mode;
16667 int unsignedp, reversep, volatilep;
16668 HOST_WIDE_INT bitsize;
16669 location_t loc = EXPR_LOCATION (exp);
16671 if (TREE_CODE (exp) == ADDR_EXPR)
16673 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16674 poffset, &mode, &unsignedp, &reversep,
16675 &volatilep, false);
16676 core = build_fold_addr_expr_loc (loc, core);
16678 else
16680 core = exp;
16681 *pbitpos = 0;
16682 *poffset = NULL_TREE;
16685 return core;
16688 /* Returns true if addresses of E1 and E2 differ by a constant, false
16689 otherwise. If they do, E1 - E2 is stored in *DIFF. */
16691 bool
16692 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
16694 tree core1, core2;
16695 HOST_WIDE_INT bitpos1, bitpos2;
16696 tree toffset1, toffset2, tdiff, type;
16698 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16699 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16701 if (bitpos1 % BITS_PER_UNIT != 0
16702 || bitpos2 % BITS_PER_UNIT != 0
16703 || !operand_equal_p (core1, core2, 0))
16704 return false;
16706 if (toffset1 && toffset2)
16708 type = TREE_TYPE (toffset1);
16709 if (type != TREE_TYPE (toffset2))
16710 toffset2 = fold_convert (type, toffset2);
16712 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16713 if (!cst_and_fits_in_hwi (tdiff))
16714 return false;
16716 *diff = int_cst_value (tdiff);
16718 else if (toffset1 || toffset2)
16720 /* If only one of the offsets is non-constant, the difference cannot
16721 be a constant. */
16722 return false;
16724 else
16725 *diff = 0;
16727 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
16728 return true;
16731 /* Simplify the floating point expression EXP when the sign of the
16732 result is not significant. Return NULL_TREE if no simplification
16733 is possible. */
16735 tree
16736 fold_strip_sign_ops (tree exp)
16738 tree arg0, arg1;
16739 location_t loc = EXPR_LOCATION (exp);
16741 switch (TREE_CODE (exp))
16743 case ABS_EXPR:
16744 case NEGATE_EXPR:
16745 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16746 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
16748 case MULT_EXPR:
16749 case RDIV_EXPR:
16750 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
16751 return NULL_TREE;
16752 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16753 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16754 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
16755 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
16756 arg0 ? arg0 : TREE_OPERAND (exp, 0),
16757 arg1 ? arg1 : TREE_OPERAND (exp, 1));
16758 break;
16760 case COMPOUND_EXPR:
16761 arg0 = TREE_OPERAND (exp, 0);
16762 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16763 if (arg1)
16764 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16765 break;
16767 case COND_EXPR:
16768 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16769 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16770 if (arg0 || arg1)
16771 return fold_build3_loc (loc,
16772 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16773 arg0 ? arg0 : TREE_OPERAND (exp, 1),
16774 arg1 ? arg1 : TREE_OPERAND (exp, 2));
16775 break;
16777 case CALL_EXPR:
16779 const enum built_in_function fcode = builtin_mathfn_code (exp);
16780 switch (fcode)
16782 CASE_FLT_FN (BUILT_IN_COPYSIGN):
16783 /* Strip copysign function call, return the 1st argument. */
16784 arg0 = CALL_EXPR_ARG (exp, 0);
16785 arg1 = CALL_EXPR_ARG (exp, 1);
16786 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16788 default:
16789 /* Strip sign ops from the argument of "odd" math functions. */
16790 if (negate_mathfn_p (fcode))
16792 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16793 if (arg0)
16794 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
16796 break;
16799 break;
16801 default:
16802 break;
16804 return NULL_TREE;