2013-10-22 Jan-Benedict Glaw <jbglaw@lug-owl.de>
[official-gcc.git] / gcc / fold-const.c
blob021d670acf0744359418effcfa093f626bcd812c
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2013 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 "realmpfr.h"
50 #include "rtl.h"
51 #include "expr.h"
52 #include "tm_p.h"
53 #include "target.h"
54 #include "diagnostic-core.h"
55 #include "intl.h"
56 #include "ggc.h"
57 #include "hash-table.h"
58 #include "langhooks.h"
59 #include "md5.h"
60 #include "gimple.h"
61 #include "tree-ssa.h"
63 /* Nonzero if we are folding constants inside an initializer; zero
64 otherwise. */
65 int folding_initializer = 0;
67 /* The following constants represent a bit based encoding of GCC's
68 comparison operators. This encoding simplifies transformations
69 on relational comparison operators, such as AND and OR. */
70 enum comparison_code {
71 COMPCODE_FALSE = 0,
72 COMPCODE_LT = 1,
73 COMPCODE_EQ = 2,
74 COMPCODE_LE = 3,
75 COMPCODE_GT = 4,
76 COMPCODE_LTGT = 5,
77 COMPCODE_GE = 6,
78 COMPCODE_ORD = 7,
79 COMPCODE_UNORD = 8,
80 COMPCODE_UNLT = 9,
81 COMPCODE_UNEQ = 10,
82 COMPCODE_UNLE = 11,
83 COMPCODE_UNGT = 12,
84 COMPCODE_NE = 13,
85 COMPCODE_UNGE = 14,
86 COMPCODE_TRUE = 15
89 static bool negate_mathfn_p (enum built_in_function);
90 static bool negate_expr_p (tree);
91 static tree negate_expr (tree);
92 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
93 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
94 static tree const_binop (enum tree_code, tree, tree);
95 static enum comparison_code comparison_to_compcode (enum tree_code);
96 static enum tree_code compcode_to_comparison (enum comparison_code);
97 static int operand_equal_for_comparison_p (tree, tree, tree);
98 static int twoval_comparison_p (tree, tree *, tree *, int *);
99 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
100 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
101 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
102 static tree make_bit_field_ref (location_t, tree, tree,
103 HOST_WIDE_INT, HOST_WIDE_INT, int);
104 static tree optimize_bit_field_compare (location_t, enum tree_code,
105 tree, tree, tree);
106 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
107 HOST_WIDE_INT *,
108 enum machine_mode *, int *, int *,
109 tree *, tree *);
110 static int all_ones_mask_p (const_tree, int);
111 static tree sign_bit_p (tree, const_tree);
112 static int simple_operand_p (const_tree);
113 static bool simple_operand_p_2 (tree);
114 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
115 static tree range_predecessor (tree);
116 static tree range_successor (tree);
117 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
118 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
119 static tree unextend (tree, int, int, tree);
120 static tree optimize_minmax_comparison (location_t, enum tree_code,
121 tree, tree, tree);
122 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
123 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
124 static tree fold_binary_op_with_conditional_arg (location_t,
125 enum tree_code, tree,
126 tree, tree,
127 tree, tree, int);
128 static tree fold_mathfn_compare (location_t,
129 enum built_in_function, enum tree_code,
130 tree, tree, tree);
131 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
132 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
133 static bool reorder_operands_p (const_tree, const_tree);
134 static tree fold_negate_const (tree, tree);
135 static tree fold_not_const (const_tree, tree);
136 static tree fold_relational_const (enum tree_code, tree, tree, tree);
137 static tree fold_convert_const (enum tree_code, tree, tree);
139 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
140 Otherwise, return LOC. */
142 static location_t
143 expr_location_or (tree t, location_t loc)
145 location_t tloc = EXPR_LOCATION (t);
146 return tloc == UNKNOWN_LOCATION ? loc : tloc;
149 /* Similar to protected_set_expr_location, but never modify x in place,
150 if location can and needs to be set, unshare it. */
152 static inline tree
153 protected_set_expr_location_unshare (tree x, location_t loc)
155 if (CAN_HAVE_LOCATION_P (x)
156 && EXPR_LOCATION (x) != loc
157 && !(TREE_CODE (x) == SAVE_EXPR
158 || TREE_CODE (x) == TARGET_EXPR
159 || TREE_CODE (x) == BIND_EXPR))
161 x = copy_node (x);
162 SET_EXPR_LOCATION (x, loc);
164 return x;
167 /* If ARG2 divides ARG1 with zero remainder, carries out the division
168 of type CODE and returns the quotient.
169 Otherwise returns NULL_TREE. */
171 tree
172 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
174 double_int quo, rem;
175 int uns;
177 /* The sign of the division is according to operand two, that
178 does the correct thing for POINTER_PLUS_EXPR where we want
179 a signed division. */
180 uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
182 quo = tree_to_double_int (arg1).divmod (tree_to_double_int (arg2),
183 uns, code, &rem);
185 if (rem.is_zero ())
186 return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high);
188 return NULL_TREE;
191 /* This is nonzero if we should defer warnings about undefined
192 overflow. This facility exists because these warnings are a
193 special case. The code to estimate loop iterations does not want
194 to issue any warnings, since it works with expressions which do not
195 occur in user code. Various bits of cleanup code call fold(), but
196 only use the result if it has certain characteristics (e.g., is a
197 constant); that code only wants to issue a warning if the result is
198 used. */
200 static int fold_deferring_overflow_warnings;
202 /* If a warning about undefined overflow is deferred, this is the
203 warning. Note that this may cause us to turn two warnings into
204 one, but that is fine since it is sufficient to only give one
205 warning per expression. */
207 static const char* fold_deferred_overflow_warning;
209 /* If a warning about undefined overflow is deferred, this is the
210 level at which the warning should be emitted. */
212 static enum warn_strict_overflow_code fold_deferred_overflow_code;
214 /* Start deferring overflow warnings. We could use a stack here to
215 permit nested calls, but at present it is not necessary. */
217 void
218 fold_defer_overflow_warnings (void)
220 ++fold_deferring_overflow_warnings;
223 /* Stop deferring overflow warnings. If there is a pending warning,
224 and ISSUE is true, then issue the warning if appropriate. STMT is
225 the statement with which the warning should be associated (used for
226 location information); STMT may be NULL. CODE is the level of the
227 warning--a warn_strict_overflow_code value. This function will use
228 the smaller of CODE and the deferred code when deciding whether to
229 issue the warning. CODE may be zero to mean to always use the
230 deferred code. */
232 void
233 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
235 const char *warnmsg;
236 location_t locus;
238 gcc_assert (fold_deferring_overflow_warnings > 0);
239 --fold_deferring_overflow_warnings;
240 if (fold_deferring_overflow_warnings > 0)
242 if (fold_deferred_overflow_warning != NULL
243 && code != 0
244 && code < (int) fold_deferred_overflow_code)
245 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
246 return;
249 warnmsg = fold_deferred_overflow_warning;
250 fold_deferred_overflow_warning = NULL;
252 if (!issue || warnmsg == NULL)
253 return;
255 if (gimple_no_warning_p (stmt))
256 return;
258 /* Use the smallest code level when deciding to issue the
259 warning. */
260 if (code == 0 || code > (int) fold_deferred_overflow_code)
261 code = fold_deferred_overflow_code;
263 if (!issue_strict_overflow_warning (code))
264 return;
266 if (stmt == NULL)
267 locus = input_location;
268 else
269 locus = gimple_location (stmt);
270 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
273 /* Stop deferring overflow warnings, ignoring any deferred
274 warnings. */
276 void
277 fold_undefer_and_ignore_overflow_warnings (void)
279 fold_undefer_overflow_warnings (false, NULL, 0);
282 /* Whether we are deferring overflow warnings. */
284 bool
285 fold_deferring_overflow_warnings_p (void)
287 return fold_deferring_overflow_warnings > 0;
290 /* This is called when we fold something based on the fact that signed
291 overflow is undefined. */
293 static void
294 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
296 if (fold_deferring_overflow_warnings > 0)
298 if (fold_deferred_overflow_warning == NULL
299 || wc < fold_deferred_overflow_code)
301 fold_deferred_overflow_warning = gmsgid;
302 fold_deferred_overflow_code = wc;
305 else if (issue_strict_overflow_warning (wc))
306 warning (OPT_Wstrict_overflow, gmsgid);
309 /* Return true if the built-in mathematical function specified by CODE
310 is odd, i.e. -f(x) == f(-x). */
312 static bool
313 negate_mathfn_p (enum built_in_function code)
315 switch (code)
317 CASE_FLT_FN (BUILT_IN_ASIN):
318 CASE_FLT_FN (BUILT_IN_ASINH):
319 CASE_FLT_FN (BUILT_IN_ATAN):
320 CASE_FLT_FN (BUILT_IN_ATANH):
321 CASE_FLT_FN (BUILT_IN_CASIN):
322 CASE_FLT_FN (BUILT_IN_CASINH):
323 CASE_FLT_FN (BUILT_IN_CATAN):
324 CASE_FLT_FN (BUILT_IN_CATANH):
325 CASE_FLT_FN (BUILT_IN_CBRT):
326 CASE_FLT_FN (BUILT_IN_CPROJ):
327 CASE_FLT_FN (BUILT_IN_CSIN):
328 CASE_FLT_FN (BUILT_IN_CSINH):
329 CASE_FLT_FN (BUILT_IN_CTAN):
330 CASE_FLT_FN (BUILT_IN_CTANH):
331 CASE_FLT_FN (BUILT_IN_ERF):
332 CASE_FLT_FN (BUILT_IN_LLROUND):
333 CASE_FLT_FN (BUILT_IN_LROUND):
334 CASE_FLT_FN (BUILT_IN_ROUND):
335 CASE_FLT_FN (BUILT_IN_SIN):
336 CASE_FLT_FN (BUILT_IN_SINH):
337 CASE_FLT_FN (BUILT_IN_TAN):
338 CASE_FLT_FN (BUILT_IN_TANH):
339 CASE_FLT_FN (BUILT_IN_TRUNC):
340 return true;
342 CASE_FLT_FN (BUILT_IN_LLRINT):
343 CASE_FLT_FN (BUILT_IN_LRINT):
344 CASE_FLT_FN (BUILT_IN_NEARBYINT):
345 CASE_FLT_FN (BUILT_IN_RINT):
346 return !flag_rounding_math;
348 default:
349 break;
351 return false;
354 /* Check whether we may negate an integer constant T without causing
355 overflow. */
357 bool
358 may_negate_without_overflow_p (const_tree t)
360 unsigned HOST_WIDE_INT val;
361 unsigned int prec;
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 prec = TYPE_PRECISION (type);
371 if (prec > HOST_BITS_PER_WIDE_INT)
373 if (TREE_INT_CST_LOW (t) != 0)
374 return true;
375 prec -= HOST_BITS_PER_WIDE_INT;
376 val = TREE_INT_CST_HIGH (t);
378 else
379 val = TREE_INT_CST_LOW (t);
380 if (prec < HOST_BITS_PER_WIDE_INT)
381 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
382 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
385 /* Determine whether an expression T can be cheaply negated using
386 the function negate_expr without introducing undefined overflow. */
388 static bool
389 negate_expr_p (tree t)
391 tree type;
393 if (t == 0)
394 return false;
396 type = TREE_TYPE (t);
398 STRIP_SIGN_NOPS (t);
399 switch (TREE_CODE (t))
401 case INTEGER_CST:
402 if (TYPE_OVERFLOW_WRAPS (type))
403 return true;
405 /* Check that -CST will not overflow type. */
406 return may_negate_without_overflow_p (t);
407 case BIT_NOT_EXPR:
408 return (INTEGRAL_TYPE_P (type)
409 && TYPE_OVERFLOW_WRAPS (type));
411 case FIXED_CST:
412 case NEGATE_EXPR:
413 return true;
415 case REAL_CST:
416 /* We want to canonicalize to positive real constants. Pretend
417 that only negative ones can be easily negated. */
418 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
420 case COMPLEX_CST:
421 return negate_expr_p (TREE_REALPART (t))
422 && negate_expr_p (TREE_IMAGPART (t));
424 case VECTOR_CST:
426 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
427 return true;
429 int count = TYPE_VECTOR_SUBPARTS (type), i;
431 for (i = 0; i < count; i++)
432 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
433 return false;
435 return true;
438 case COMPLEX_EXPR:
439 return negate_expr_p (TREE_OPERAND (t, 0))
440 && negate_expr_p (TREE_OPERAND (t, 1));
442 case CONJ_EXPR:
443 return negate_expr_p (TREE_OPERAND (t, 0));
445 case PLUS_EXPR:
446 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
447 || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
448 return false;
449 /* -(A + B) -> (-B) - A. */
450 if (negate_expr_p (TREE_OPERAND (t, 1))
451 && reorder_operands_p (TREE_OPERAND (t, 0),
452 TREE_OPERAND (t, 1)))
453 return true;
454 /* -(A + B) -> (-A) - B. */
455 return negate_expr_p (TREE_OPERAND (t, 0));
457 case MINUS_EXPR:
458 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
459 return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
460 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
461 && reorder_operands_p (TREE_OPERAND (t, 0),
462 TREE_OPERAND (t, 1));
464 case MULT_EXPR:
465 if (TYPE_UNSIGNED (TREE_TYPE (t)))
466 break;
468 /* Fall through. */
470 case RDIV_EXPR:
471 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
472 return negate_expr_p (TREE_OPERAND (t, 1))
473 || negate_expr_p (TREE_OPERAND (t, 0));
474 break;
476 case TRUNC_DIV_EXPR:
477 case ROUND_DIV_EXPR:
478 case FLOOR_DIV_EXPR:
479 case CEIL_DIV_EXPR:
480 case EXACT_DIV_EXPR:
481 /* In general we can't negate A / B, because if A is INT_MIN and
482 B is 1, we may turn this into INT_MIN / -1 which is undefined
483 and actually traps on some architectures. But if overflow is
484 undefined, we can negate, because - (INT_MIN / 1) is an
485 overflow. */
486 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
488 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
489 break;
490 /* If overflow is undefined then we have to be careful because
491 we ask whether it's ok to associate the negate with the
492 division which is not ok for example for
493 -((a - b) / c) where (-(a - b)) / c may invoke undefined
494 overflow because of negating INT_MIN. So do not use
495 negate_expr_p here but open-code the two important cases. */
496 if (TREE_CODE (TREE_OPERAND (t, 0)) == NEGATE_EXPR
497 || (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
498 && may_negate_without_overflow_p (TREE_OPERAND (t, 0))))
499 return true;
501 else if (negate_expr_p (TREE_OPERAND (t, 0)))
502 return true;
503 return negate_expr_p (TREE_OPERAND (t, 1));
505 case NOP_EXPR:
506 /* Negate -((double)float) as (double)(-float). */
507 if (TREE_CODE (type) == REAL_TYPE)
509 tree tem = strip_float_extensions (t);
510 if (tem != t)
511 return negate_expr_p (tem);
513 break;
515 case CALL_EXPR:
516 /* Negate -f(x) as f(-x). */
517 if (negate_mathfn_p (builtin_mathfn_code (t)))
518 return negate_expr_p (CALL_EXPR_ARG (t, 0));
519 break;
521 case RSHIFT_EXPR:
522 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
523 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
525 tree op1 = TREE_OPERAND (t, 1);
526 if (TREE_INT_CST_HIGH (op1) == 0
527 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
528 == TREE_INT_CST_LOW (op1))
529 return true;
531 break;
533 default:
534 break;
536 return false;
539 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
540 simplification is possible.
541 If negate_expr_p would return true for T, NULL_TREE will never be
542 returned. */
544 static tree
545 fold_negate_expr (location_t loc, tree t)
547 tree type = TREE_TYPE (t);
548 tree tem;
550 switch (TREE_CODE (t))
552 /* Convert - (~A) to A + 1. */
553 case BIT_NOT_EXPR:
554 if (INTEGRAL_TYPE_P (type))
555 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
556 build_one_cst (type));
557 break;
559 case INTEGER_CST:
560 tem = fold_negate_const (t, type);
561 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
562 || !TYPE_OVERFLOW_TRAPS (type))
563 return tem;
564 break;
566 case REAL_CST:
567 tem = fold_negate_const (t, type);
568 /* Two's complement FP formats, such as c4x, may overflow. */
569 if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
570 return tem;
571 break;
573 case FIXED_CST:
574 tem = fold_negate_const (t, type);
575 return tem;
577 case COMPLEX_CST:
579 tree rpart = negate_expr (TREE_REALPART (t));
580 tree ipart = negate_expr (TREE_IMAGPART (t));
582 if ((TREE_CODE (rpart) == REAL_CST
583 && TREE_CODE (ipart) == REAL_CST)
584 || (TREE_CODE (rpart) == INTEGER_CST
585 && TREE_CODE (ipart) == INTEGER_CST))
586 return build_complex (type, rpart, ipart);
588 break;
590 case VECTOR_CST:
592 int count = TYPE_VECTOR_SUBPARTS (type), i;
593 tree *elts = XALLOCAVEC (tree, count);
595 for (i = 0; i < count; i++)
597 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
598 if (elts[i] == NULL_TREE)
599 return NULL_TREE;
602 return build_vector (type, elts);
605 case COMPLEX_EXPR:
606 if (negate_expr_p (t))
607 return fold_build2_loc (loc, COMPLEX_EXPR, type,
608 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
609 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
610 break;
612 case CONJ_EXPR:
613 if (negate_expr_p (t))
614 return fold_build1_loc (loc, CONJ_EXPR, type,
615 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
616 break;
618 case NEGATE_EXPR:
619 return TREE_OPERAND (t, 0);
621 case PLUS_EXPR:
622 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
623 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
625 /* -(A + B) -> (-B) - A. */
626 if (negate_expr_p (TREE_OPERAND (t, 1))
627 && reorder_operands_p (TREE_OPERAND (t, 0),
628 TREE_OPERAND (t, 1)))
630 tem = negate_expr (TREE_OPERAND (t, 1));
631 return fold_build2_loc (loc, MINUS_EXPR, type,
632 tem, TREE_OPERAND (t, 0));
635 /* -(A + B) -> (-A) - B. */
636 if (negate_expr_p (TREE_OPERAND (t, 0)))
638 tem = negate_expr (TREE_OPERAND (t, 0));
639 return fold_build2_loc (loc, MINUS_EXPR, type,
640 tem, TREE_OPERAND (t, 1));
643 break;
645 case MINUS_EXPR:
646 /* - (A - B) -> B - A */
647 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
648 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
649 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
650 return fold_build2_loc (loc, MINUS_EXPR, type,
651 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
652 break;
654 case MULT_EXPR:
655 if (TYPE_UNSIGNED (type))
656 break;
658 /* Fall through. */
660 case RDIV_EXPR:
661 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
663 tem = TREE_OPERAND (t, 1);
664 if (negate_expr_p (tem))
665 return fold_build2_loc (loc, TREE_CODE (t), type,
666 TREE_OPERAND (t, 0), negate_expr (tem));
667 tem = TREE_OPERAND (t, 0);
668 if (negate_expr_p (tem))
669 return fold_build2_loc (loc, TREE_CODE (t), type,
670 negate_expr (tem), TREE_OPERAND (t, 1));
672 break;
674 case TRUNC_DIV_EXPR:
675 case ROUND_DIV_EXPR:
676 case FLOOR_DIV_EXPR:
677 case CEIL_DIV_EXPR:
678 case EXACT_DIV_EXPR:
679 /* In general we can't negate A / B, because if A is INT_MIN and
680 B is 1, we may turn this into INT_MIN / -1 which is undefined
681 and actually traps on some architectures. But if overflow is
682 undefined, we can negate, because - (INT_MIN / 1) is an
683 overflow. */
684 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
686 const char * const warnmsg = G_("assuming signed overflow does not "
687 "occur when negating a division");
688 tem = TREE_OPERAND (t, 1);
689 if (negate_expr_p (tem))
691 if (INTEGRAL_TYPE_P (type)
692 && (TREE_CODE (tem) != INTEGER_CST
693 || integer_onep (tem)))
694 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
695 return fold_build2_loc (loc, TREE_CODE (t), type,
696 TREE_OPERAND (t, 0), negate_expr (tem));
698 /* If overflow is undefined then we have to be careful because
699 we ask whether it's ok to associate the negate with the
700 division which is not ok for example for
701 -((a - b) / c) where (-(a - b)) / c may invoke undefined
702 overflow because of negating INT_MIN. So do not use
703 negate_expr_p here but open-code the two important cases. */
704 tem = TREE_OPERAND (t, 0);
705 if ((INTEGRAL_TYPE_P (type)
706 && (TREE_CODE (tem) == NEGATE_EXPR
707 || (TREE_CODE (tem) == INTEGER_CST
708 && may_negate_without_overflow_p (tem))))
709 || !INTEGRAL_TYPE_P (type))
710 return fold_build2_loc (loc, TREE_CODE (t), type,
711 negate_expr (tem), TREE_OPERAND (t, 1));
713 break;
715 case NOP_EXPR:
716 /* Convert -((double)float) into (double)(-float). */
717 if (TREE_CODE (type) == REAL_TYPE)
719 tem = strip_float_extensions (t);
720 if (tem != t && negate_expr_p (tem))
721 return fold_convert_loc (loc, type, negate_expr (tem));
723 break;
725 case CALL_EXPR:
726 /* Negate -f(x) as f(-x). */
727 if (negate_mathfn_p (builtin_mathfn_code (t))
728 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
730 tree fndecl, arg;
732 fndecl = get_callee_fndecl (t);
733 arg = negate_expr (CALL_EXPR_ARG (t, 0));
734 return build_call_expr_loc (loc, fndecl, 1, arg);
736 break;
738 case RSHIFT_EXPR:
739 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
740 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
742 tree op1 = TREE_OPERAND (t, 1);
743 if (TREE_INT_CST_HIGH (op1) == 0
744 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
745 == TREE_INT_CST_LOW (op1))
747 tree ntype = TYPE_UNSIGNED (type)
748 ? signed_type_for (type)
749 : unsigned_type_for (type);
750 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
751 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
752 return fold_convert_loc (loc, type, temp);
755 break;
757 default:
758 break;
761 return NULL_TREE;
764 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
765 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
766 return NULL_TREE. */
768 static tree
769 negate_expr (tree t)
771 tree type, tem;
772 location_t loc;
774 if (t == NULL_TREE)
775 return NULL_TREE;
777 loc = EXPR_LOCATION (t);
778 type = TREE_TYPE (t);
779 STRIP_SIGN_NOPS (t);
781 tem = fold_negate_expr (loc, t);
782 if (!tem)
783 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
784 return fold_convert_loc (loc, type, tem);
787 /* Split a tree IN into a constant, literal and variable parts that could be
788 combined with CODE to make IN. "constant" means an expression with
789 TREE_CONSTANT but that isn't an actual constant. CODE must be a
790 commutative arithmetic operation. Store the constant part into *CONP,
791 the literal in *LITP and return the variable part. If a part isn't
792 present, set it to null. If the tree does not decompose in this way,
793 return the entire tree as the variable part and the other parts as null.
795 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
796 case, we negate an operand that was subtracted. Except if it is a
797 literal for which we use *MINUS_LITP instead.
799 If NEGATE_P is true, we are negating all of IN, again except a literal
800 for which we use *MINUS_LITP instead.
802 If IN is itself a literal or constant, return it as appropriate.
804 Note that we do not guarantee that any of the three values will be the
805 same type as IN, but they will have the same signedness and mode. */
807 static tree
808 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
809 tree *minus_litp, int negate_p)
811 tree var = 0;
813 *conp = 0;
814 *litp = 0;
815 *minus_litp = 0;
817 /* Strip any conversions that don't change the machine mode or signedness. */
818 STRIP_SIGN_NOPS (in);
820 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
821 || TREE_CODE (in) == FIXED_CST)
822 *litp = in;
823 else if (TREE_CODE (in) == code
824 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
825 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
826 /* We can associate addition and subtraction together (even
827 though the C standard doesn't say so) for integers because
828 the value is not affected. For reals, the value might be
829 affected, so we can't. */
830 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
831 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
833 tree op0 = TREE_OPERAND (in, 0);
834 tree op1 = TREE_OPERAND (in, 1);
835 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
836 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
838 /* First see if either of the operands is a literal, then a constant. */
839 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
840 || TREE_CODE (op0) == FIXED_CST)
841 *litp = op0, op0 = 0;
842 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
843 || TREE_CODE (op1) == FIXED_CST)
844 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
846 if (op0 != 0 && TREE_CONSTANT (op0))
847 *conp = op0, op0 = 0;
848 else if (op1 != 0 && TREE_CONSTANT (op1))
849 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
851 /* If we haven't dealt with either operand, this is not a case we can
852 decompose. Otherwise, VAR is either of the ones remaining, if any. */
853 if (op0 != 0 && op1 != 0)
854 var = in;
855 else if (op0 != 0)
856 var = op0;
857 else
858 var = op1, neg_var_p = neg1_p;
860 /* Now do any needed negations. */
861 if (neg_litp_p)
862 *minus_litp = *litp, *litp = 0;
863 if (neg_conp_p)
864 *conp = negate_expr (*conp);
865 if (neg_var_p)
866 var = negate_expr (var);
868 else if (TREE_CODE (in) == BIT_NOT_EXPR
869 && code == PLUS_EXPR)
871 /* -X - 1 is folded to ~X, undo that here. */
872 *minus_litp = build_one_cst (TREE_TYPE (in));
873 var = negate_expr (TREE_OPERAND (in, 0));
875 else if (TREE_CONSTANT (in))
876 *conp = in;
877 else
878 var = in;
880 if (negate_p)
882 if (*litp)
883 *minus_litp = *litp, *litp = 0;
884 else if (*minus_litp)
885 *litp = *minus_litp, *minus_litp = 0;
886 *conp = negate_expr (*conp);
887 var = negate_expr (var);
890 return var;
893 /* Re-associate trees split by the above function. T1 and T2 are
894 either expressions to associate or null. Return the new
895 expression, if any. LOC is the location of the new expression. If
896 we build an operation, do it in TYPE and with CODE. */
898 static tree
899 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
901 if (t1 == 0)
902 return t2;
903 else if (t2 == 0)
904 return t1;
906 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
907 try to fold this since we will have infinite recursion. But do
908 deal with any NEGATE_EXPRs. */
909 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
910 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
912 if (code == PLUS_EXPR)
914 if (TREE_CODE (t1) == NEGATE_EXPR)
915 return build2_loc (loc, MINUS_EXPR, type,
916 fold_convert_loc (loc, type, t2),
917 fold_convert_loc (loc, type,
918 TREE_OPERAND (t1, 0)));
919 else if (TREE_CODE (t2) == NEGATE_EXPR)
920 return build2_loc (loc, MINUS_EXPR, type,
921 fold_convert_loc (loc, type, t1),
922 fold_convert_loc (loc, type,
923 TREE_OPERAND (t2, 0)));
924 else if (integer_zerop (t2))
925 return fold_convert_loc (loc, type, t1);
927 else if (code == MINUS_EXPR)
929 if (integer_zerop (t2))
930 return fold_convert_loc (loc, type, t1);
933 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
934 fold_convert_loc (loc, type, t2));
937 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
938 fold_convert_loc (loc, type, t2));
941 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
942 for use in int_const_binop, size_binop and size_diffop. */
944 static bool
945 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
947 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
948 return false;
949 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
950 return false;
952 switch (code)
954 case LSHIFT_EXPR:
955 case RSHIFT_EXPR:
956 case LROTATE_EXPR:
957 case RROTATE_EXPR:
958 return true;
960 default:
961 break;
964 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
965 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
966 && TYPE_MODE (type1) == TYPE_MODE (type2);
970 /* Combine two integer constants ARG1 and ARG2 under operation CODE
971 to produce a new constant. Return NULL_TREE if we don't know how
972 to evaluate CODE at compile-time. */
974 static tree
975 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree arg2,
976 int overflowable)
978 double_int op1, op2, res, tmp;
979 tree t;
980 tree type = TREE_TYPE (arg1);
981 bool uns = TYPE_UNSIGNED (type);
982 bool overflow = false;
984 op1 = tree_to_double_int (arg1);
985 op2 = tree_to_double_int (arg2);
987 switch (code)
989 case BIT_IOR_EXPR:
990 res = op1 | op2;
991 break;
993 case BIT_XOR_EXPR:
994 res = op1 ^ op2;
995 break;
997 case BIT_AND_EXPR:
998 res = op1 & op2;
999 break;
1001 case RSHIFT_EXPR:
1002 res = op1.rshift (op2.to_shwi (), TYPE_PRECISION (type), !uns);
1003 break;
1005 case LSHIFT_EXPR:
1006 /* It's unclear from the C standard whether shifts can overflow.
1007 The following code ignores overflow; perhaps a C standard
1008 interpretation ruling is needed. */
1009 res = op1.lshift (op2.to_shwi (), TYPE_PRECISION (type), !uns);
1010 break;
1012 case RROTATE_EXPR:
1013 res = op1.rrotate (op2.to_shwi (), TYPE_PRECISION (type));
1014 break;
1016 case LROTATE_EXPR:
1017 res = op1.lrotate (op2.to_shwi (), TYPE_PRECISION (type));
1018 break;
1020 case PLUS_EXPR:
1021 res = op1.add_with_sign (op2, false, &overflow);
1022 break;
1024 case MINUS_EXPR:
1025 res = op1.sub_with_overflow (op2, &overflow);
1026 break;
1028 case MULT_EXPR:
1029 res = op1.mul_with_sign (op2, false, &overflow);
1030 break;
1032 case MULT_HIGHPART_EXPR:
1033 if (TYPE_PRECISION (type) > HOST_BITS_PER_WIDE_INT)
1035 bool dummy_overflow;
1036 if (TYPE_PRECISION (type) != 2 * HOST_BITS_PER_WIDE_INT)
1037 return NULL_TREE;
1038 op1.wide_mul_with_sign (op2, uns, &res, &dummy_overflow);
1040 else
1042 bool dummy_overflow;
1043 /* MULT_HIGHPART_EXPR can't ever oveflow, as the multiplication
1044 is performed in twice the precision of arguments. */
1045 tmp = op1.mul_with_sign (op2, false, &dummy_overflow);
1046 res = tmp.rshift (TYPE_PRECISION (type),
1047 2 * TYPE_PRECISION (type), !uns);
1049 break;
1051 case TRUNC_DIV_EXPR:
1052 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1053 case EXACT_DIV_EXPR:
1054 /* This is a shortcut for a common special case. */
1055 if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1056 && !TREE_OVERFLOW (arg1)
1057 && !TREE_OVERFLOW (arg2)
1058 && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1060 if (code == CEIL_DIV_EXPR)
1061 op1.low += op2.low - 1;
1063 res.low = op1.low / op2.low, res.high = 0;
1064 break;
1067 /* ... fall through ... */
1069 case ROUND_DIV_EXPR:
1070 if (op2.is_zero ())
1071 return NULL_TREE;
1072 if (op2.is_one ())
1074 res = op1;
1075 break;
1077 if (op1 == op2 && !op1.is_zero ())
1079 res = double_int_one;
1080 break;
1082 res = op1.divmod_with_overflow (op2, uns, code, &tmp, &overflow);
1083 break;
1085 case TRUNC_MOD_EXPR:
1086 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1087 /* This is a shortcut for a common special case. */
1088 if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1089 && !TREE_OVERFLOW (arg1)
1090 && !TREE_OVERFLOW (arg2)
1091 && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1093 if (code == CEIL_MOD_EXPR)
1094 op1.low += op2.low - 1;
1095 res.low = op1.low % op2.low, res.high = 0;
1096 break;
1099 /* ... fall through ... */
1101 case ROUND_MOD_EXPR:
1102 if (op2.is_zero ())
1103 return NULL_TREE;
1104 tmp = op1.divmod_with_overflow (op2, uns, code, &res, &overflow);
1105 break;
1107 case MIN_EXPR:
1108 res = op1.min (op2, uns);
1109 break;
1111 case MAX_EXPR:
1112 res = op1.max (op2, uns);
1113 break;
1115 default:
1116 return NULL_TREE;
1119 t = force_fit_type_double (TREE_TYPE (arg1), res, overflowable,
1120 (!uns && overflow)
1121 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1123 return t;
1126 tree
1127 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1129 return int_const_binop_1 (code, arg1, arg2, 1);
1132 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1133 constant. We assume ARG1 and ARG2 have the same data type, or at least
1134 are the same kind of constant and the same machine mode. Return zero if
1135 combining the constants is not allowed in the current operating mode. */
1137 static tree
1138 const_binop (enum tree_code code, tree arg1, tree arg2)
1140 /* Sanity check for the recursive cases. */
1141 if (!arg1 || !arg2)
1142 return NULL_TREE;
1144 STRIP_NOPS (arg1);
1145 STRIP_NOPS (arg2);
1147 if (TREE_CODE (arg1) == INTEGER_CST)
1148 return int_const_binop (code, arg1, arg2);
1150 if (TREE_CODE (arg1) == REAL_CST)
1152 enum machine_mode mode;
1153 REAL_VALUE_TYPE d1;
1154 REAL_VALUE_TYPE d2;
1155 REAL_VALUE_TYPE value;
1156 REAL_VALUE_TYPE result;
1157 bool inexact;
1158 tree t, type;
1160 /* The following codes are handled by real_arithmetic. */
1161 switch (code)
1163 case PLUS_EXPR:
1164 case MINUS_EXPR:
1165 case MULT_EXPR:
1166 case RDIV_EXPR:
1167 case MIN_EXPR:
1168 case MAX_EXPR:
1169 break;
1171 default:
1172 return NULL_TREE;
1175 d1 = TREE_REAL_CST (arg1);
1176 d2 = TREE_REAL_CST (arg2);
1178 type = TREE_TYPE (arg1);
1179 mode = TYPE_MODE (type);
1181 /* Don't perform operation if we honor signaling NaNs and
1182 either operand is a NaN. */
1183 if (HONOR_SNANS (mode)
1184 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1185 return NULL_TREE;
1187 /* Don't perform operation if it would raise a division
1188 by zero exception. */
1189 if (code == RDIV_EXPR
1190 && REAL_VALUES_EQUAL (d2, dconst0)
1191 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1192 return NULL_TREE;
1194 /* If either operand is a NaN, just return it. Otherwise, set up
1195 for floating-point trap; we return an overflow. */
1196 if (REAL_VALUE_ISNAN (d1))
1197 return arg1;
1198 else if (REAL_VALUE_ISNAN (d2))
1199 return arg2;
1201 inexact = real_arithmetic (&value, code, &d1, &d2);
1202 real_convert (&result, mode, &value);
1204 /* Don't constant fold this floating point operation if
1205 the result has overflowed and flag_trapping_math. */
1206 if (flag_trapping_math
1207 && MODE_HAS_INFINITIES (mode)
1208 && REAL_VALUE_ISINF (result)
1209 && !REAL_VALUE_ISINF (d1)
1210 && !REAL_VALUE_ISINF (d2))
1211 return NULL_TREE;
1213 /* Don't constant fold this floating point operation if the
1214 result may dependent upon the run-time rounding mode and
1215 flag_rounding_math is set, or if GCC's software emulation
1216 is unable to accurately represent the result. */
1217 if ((flag_rounding_math
1218 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1219 && (inexact || !real_identical (&result, &value)))
1220 return NULL_TREE;
1222 t = build_real (type, result);
1224 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1225 return t;
1228 if (TREE_CODE (arg1) == FIXED_CST)
1230 FIXED_VALUE_TYPE f1;
1231 FIXED_VALUE_TYPE f2;
1232 FIXED_VALUE_TYPE result;
1233 tree t, type;
1234 int sat_p;
1235 bool overflow_p;
1237 /* The following codes are handled by fixed_arithmetic. */
1238 switch (code)
1240 case PLUS_EXPR:
1241 case MINUS_EXPR:
1242 case MULT_EXPR:
1243 case TRUNC_DIV_EXPR:
1244 f2 = TREE_FIXED_CST (arg2);
1245 break;
1247 case LSHIFT_EXPR:
1248 case RSHIFT_EXPR:
1249 f2.data.high = TREE_INT_CST_HIGH (arg2);
1250 f2.data.low = TREE_INT_CST_LOW (arg2);
1251 f2.mode = SImode;
1252 break;
1254 default:
1255 return NULL_TREE;
1258 f1 = TREE_FIXED_CST (arg1);
1259 type = TREE_TYPE (arg1);
1260 sat_p = TYPE_SATURATING (type);
1261 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1262 t = build_fixed (type, result);
1263 /* Propagate overflow flags. */
1264 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1265 TREE_OVERFLOW (t) = 1;
1266 return t;
1269 if (TREE_CODE (arg1) == COMPLEX_CST)
1271 tree type = TREE_TYPE (arg1);
1272 tree r1 = TREE_REALPART (arg1);
1273 tree i1 = TREE_IMAGPART (arg1);
1274 tree r2 = TREE_REALPART (arg2);
1275 tree i2 = TREE_IMAGPART (arg2);
1276 tree real, imag;
1278 switch (code)
1280 case PLUS_EXPR:
1281 case MINUS_EXPR:
1282 real = const_binop (code, r1, r2);
1283 imag = const_binop (code, i1, i2);
1284 break;
1286 case MULT_EXPR:
1287 if (COMPLEX_FLOAT_TYPE_P (type))
1288 return do_mpc_arg2 (arg1, arg2, type,
1289 /* do_nonfinite= */ folding_initializer,
1290 mpc_mul);
1292 real = const_binop (MINUS_EXPR,
1293 const_binop (MULT_EXPR, r1, r2),
1294 const_binop (MULT_EXPR, i1, i2));
1295 imag = const_binop (PLUS_EXPR,
1296 const_binop (MULT_EXPR, r1, i2),
1297 const_binop (MULT_EXPR, i1, r2));
1298 break;
1300 case RDIV_EXPR:
1301 if (COMPLEX_FLOAT_TYPE_P (type))
1302 return do_mpc_arg2 (arg1, arg2, type,
1303 /* do_nonfinite= */ folding_initializer,
1304 mpc_div);
1305 /* Fallthru ... */
1306 case TRUNC_DIV_EXPR:
1307 case CEIL_DIV_EXPR:
1308 case FLOOR_DIV_EXPR:
1309 case ROUND_DIV_EXPR:
1310 if (flag_complex_method == 0)
1312 /* Keep this algorithm in sync with
1313 tree-complex.c:expand_complex_div_straight().
1315 Expand complex division to scalars, straightforward algorithm.
1316 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1317 t = br*br + bi*bi
1319 tree magsquared
1320 = const_binop (PLUS_EXPR,
1321 const_binop (MULT_EXPR, r2, r2),
1322 const_binop (MULT_EXPR, i2, i2));
1323 tree t1
1324 = const_binop (PLUS_EXPR,
1325 const_binop (MULT_EXPR, r1, r2),
1326 const_binop (MULT_EXPR, i1, i2));
1327 tree t2
1328 = const_binop (MINUS_EXPR,
1329 const_binop (MULT_EXPR, i1, r2),
1330 const_binop (MULT_EXPR, r1, i2));
1332 real = const_binop (code, t1, magsquared);
1333 imag = const_binop (code, t2, magsquared);
1335 else
1337 /* Keep this algorithm in sync with
1338 tree-complex.c:expand_complex_div_wide().
1340 Expand complex division to scalars, modified algorithm to minimize
1341 overflow with wide input ranges. */
1342 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1343 fold_abs_const (r2, TREE_TYPE (type)),
1344 fold_abs_const (i2, TREE_TYPE (type)));
1346 if (integer_nonzerop (compare))
1348 /* In the TRUE branch, we compute
1349 ratio = br/bi;
1350 div = (br * ratio) + bi;
1351 tr = (ar * ratio) + ai;
1352 ti = (ai * ratio) - ar;
1353 tr = tr / div;
1354 ti = ti / div; */
1355 tree ratio = const_binop (code, r2, i2);
1356 tree div = const_binop (PLUS_EXPR, i2,
1357 const_binop (MULT_EXPR, r2, ratio));
1358 real = const_binop (MULT_EXPR, r1, ratio);
1359 real = const_binop (PLUS_EXPR, real, i1);
1360 real = const_binop (code, real, div);
1362 imag = const_binop (MULT_EXPR, i1, ratio);
1363 imag = const_binop (MINUS_EXPR, imag, r1);
1364 imag = const_binop (code, imag, div);
1366 else
1368 /* In the FALSE branch, we compute
1369 ratio = d/c;
1370 divisor = (d * ratio) + c;
1371 tr = (b * ratio) + a;
1372 ti = b - (a * ratio);
1373 tr = tr / div;
1374 ti = ti / div; */
1375 tree ratio = const_binop (code, i2, r2);
1376 tree div = const_binop (PLUS_EXPR, r2,
1377 const_binop (MULT_EXPR, i2, ratio));
1379 real = const_binop (MULT_EXPR, i1, ratio);
1380 real = const_binop (PLUS_EXPR, real, r1);
1381 real = const_binop (code, real, div);
1383 imag = const_binop (MULT_EXPR, r1, ratio);
1384 imag = const_binop (MINUS_EXPR, i1, imag);
1385 imag = const_binop (code, imag, div);
1388 break;
1390 default:
1391 return NULL_TREE;
1394 if (real && imag)
1395 return build_complex (type, real, imag);
1398 if (TREE_CODE (arg1) == VECTOR_CST
1399 && TREE_CODE (arg2) == VECTOR_CST)
1401 tree type = TREE_TYPE (arg1);
1402 int count = TYPE_VECTOR_SUBPARTS (type), i;
1403 tree *elts = XALLOCAVEC (tree, count);
1405 for (i = 0; i < count; i++)
1407 tree elem1 = VECTOR_CST_ELT (arg1, i);
1408 tree elem2 = VECTOR_CST_ELT (arg2, i);
1410 elts[i] = const_binop (code, elem1, elem2);
1412 /* It is possible that const_binop cannot handle the given
1413 code and return NULL_TREE */
1414 if (elts[i] == NULL_TREE)
1415 return NULL_TREE;
1418 return build_vector (type, elts);
1421 /* Shifts allow a scalar offset for a vector. */
1422 if (TREE_CODE (arg1) == VECTOR_CST
1423 && TREE_CODE (arg2) == INTEGER_CST)
1425 tree type = TREE_TYPE (arg1);
1426 int count = TYPE_VECTOR_SUBPARTS (type), i;
1427 tree *elts = XALLOCAVEC (tree, count);
1429 if (code == VEC_LSHIFT_EXPR
1430 || code == VEC_RSHIFT_EXPR)
1432 if (!host_integerp (arg2, 1))
1433 return NULL_TREE;
1435 unsigned HOST_WIDE_INT shiftc = tree_low_cst (arg2, 1);
1436 unsigned HOST_WIDE_INT outerc = tree_low_cst (TYPE_SIZE (type), 1);
1437 unsigned HOST_WIDE_INT innerc
1438 = tree_low_cst (TYPE_SIZE (TREE_TYPE (type)), 1);
1439 if (shiftc >= outerc || (shiftc % innerc) != 0)
1440 return NULL_TREE;
1441 int offset = shiftc / innerc;
1442 /* The direction of VEC_[LR]SHIFT_EXPR is endian dependent.
1443 For reductions, compiler emits VEC_RSHIFT_EXPR always,
1444 for !BYTES_BIG_ENDIAN picks first vector element, but
1445 for BYTES_BIG_ENDIAN last element from the vector. */
1446 if ((code == VEC_RSHIFT_EXPR) ^ (!BYTES_BIG_ENDIAN))
1447 offset = -offset;
1448 tree zero = build_zero_cst (TREE_TYPE (type));
1449 for (i = 0; i < count; i++)
1451 if (i + offset < 0 || i + offset >= count)
1452 elts[i] = zero;
1453 else
1454 elts[i] = VECTOR_CST_ELT (arg1, i + offset);
1457 else
1458 for (i = 0; i < count; i++)
1460 tree elem1 = VECTOR_CST_ELT (arg1, i);
1462 elts[i] = const_binop (code, elem1, arg2);
1464 /* It is possible that const_binop cannot handle the given
1465 code and return NULL_TREE */
1466 if (elts[i] == NULL_TREE)
1467 return NULL_TREE;
1470 return build_vector (type, elts);
1472 return NULL_TREE;
1475 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1476 indicates which particular sizetype to create. */
1478 tree
1479 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1481 return build_int_cst (sizetype_tab[(int) kind], number);
1484 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1485 is a tree code. The type of the result is taken from the operands.
1486 Both must be equivalent integer types, ala int_binop_types_match_p.
1487 If the operands are constant, so is the result. */
1489 tree
1490 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1492 tree type = TREE_TYPE (arg0);
1494 if (arg0 == error_mark_node || arg1 == error_mark_node)
1495 return error_mark_node;
1497 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1498 TREE_TYPE (arg1)));
1500 /* Handle the special case of two integer constants faster. */
1501 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1503 /* And some specific cases even faster than that. */
1504 if (code == PLUS_EXPR)
1506 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1507 return arg1;
1508 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1509 return arg0;
1511 else if (code == MINUS_EXPR)
1513 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1514 return arg0;
1516 else if (code == MULT_EXPR)
1518 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1519 return arg1;
1522 /* Handle general case of two integer constants. For sizetype
1523 constant calculations we always want to know about overflow,
1524 even in the unsigned case. */
1525 return int_const_binop_1 (code, arg0, arg1, -1);
1528 return fold_build2_loc (loc, code, type, arg0, arg1);
1531 /* Given two values, either both of sizetype or both of bitsizetype,
1532 compute the difference between the two values. Return the value
1533 in signed type corresponding to the type of the operands. */
1535 tree
1536 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1538 tree type = TREE_TYPE (arg0);
1539 tree ctype;
1541 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1542 TREE_TYPE (arg1)));
1544 /* If the type is already signed, just do the simple thing. */
1545 if (!TYPE_UNSIGNED (type))
1546 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1548 if (type == sizetype)
1549 ctype = ssizetype;
1550 else if (type == bitsizetype)
1551 ctype = sbitsizetype;
1552 else
1553 ctype = signed_type_for (type);
1555 /* If either operand is not a constant, do the conversions to the signed
1556 type and subtract. The hardware will do the right thing with any
1557 overflow in the subtraction. */
1558 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1559 return size_binop_loc (loc, MINUS_EXPR,
1560 fold_convert_loc (loc, ctype, arg0),
1561 fold_convert_loc (loc, ctype, arg1));
1563 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1564 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1565 overflow) and negate (which can't either). Special-case a result
1566 of zero while we're here. */
1567 if (tree_int_cst_equal (arg0, arg1))
1568 return build_int_cst (ctype, 0);
1569 else if (tree_int_cst_lt (arg1, arg0))
1570 return fold_convert_loc (loc, ctype,
1571 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1572 else
1573 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1574 fold_convert_loc (loc, ctype,
1575 size_binop_loc (loc,
1576 MINUS_EXPR,
1577 arg1, arg0)));
1580 /* A subroutine of fold_convert_const handling conversions of an
1581 INTEGER_CST to another integer type. */
1583 static tree
1584 fold_convert_const_int_from_int (tree type, const_tree arg1)
1586 tree t;
1588 /* Given an integer constant, make new constant with new type,
1589 appropriately sign-extended or truncated. */
1590 t = force_fit_type_double (type, tree_to_double_int (arg1),
1591 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1592 (TREE_INT_CST_HIGH (arg1) < 0
1593 && (TYPE_UNSIGNED (type)
1594 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1595 | TREE_OVERFLOW (arg1));
1597 return t;
1600 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1601 to an integer type. */
1603 static tree
1604 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1606 int overflow = 0;
1607 tree t;
1609 /* The following code implements the floating point to integer
1610 conversion rules required by the Java Language Specification,
1611 that IEEE NaNs are mapped to zero and values that overflow
1612 the target precision saturate, i.e. values greater than
1613 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1614 are mapped to INT_MIN. These semantics are allowed by the
1615 C and C++ standards that simply state that the behavior of
1616 FP-to-integer conversion is unspecified upon overflow. */
1618 double_int val;
1619 REAL_VALUE_TYPE r;
1620 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1622 switch (code)
1624 case FIX_TRUNC_EXPR:
1625 real_trunc (&r, VOIDmode, &x);
1626 break;
1628 default:
1629 gcc_unreachable ();
1632 /* If R is NaN, return zero and show we have an overflow. */
1633 if (REAL_VALUE_ISNAN (r))
1635 overflow = 1;
1636 val = double_int_zero;
1639 /* See if R is less than the lower bound or greater than the
1640 upper bound. */
1642 if (! overflow)
1644 tree lt = TYPE_MIN_VALUE (type);
1645 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1646 if (REAL_VALUES_LESS (r, l))
1648 overflow = 1;
1649 val = tree_to_double_int (lt);
1653 if (! overflow)
1655 tree ut = TYPE_MAX_VALUE (type);
1656 if (ut)
1658 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1659 if (REAL_VALUES_LESS (u, r))
1661 overflow = 1;
1662 val = tree_to_double_int (ut);
1667 if (! overflow)
1668 real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r);
1670 t = force_fit_type_double (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1671 return t;
1674 /* A subroutine of fold_convert_const handling conversions of a
1675 FIXED_CST to an integer type. */
1677 static tree
1678 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1680 tree t;
1681 double_int temp, temp_trunc;
1682 unsigned int mode;
1684 /* Right shift FIXED_CST to temp by fbit. */
1685 temp = TREE_FIXED_CST (arg1).data;
1686 mode = TREE_FIXED_CST (arg1).mode;
1687 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1689 temp = temp.rshift (GET_MODE_FBIT (mode),
1690 HOST_BITS_PER_DOUBLE_INT,
1691 SIGNED_FIXED_POINT_MODE_P (mode));
1693 /* Left shift temp to temp_trunc by fbit. */
1694 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1695 HOST_BITS_PER_DOUBLE_INT,
1696 SIGNED_FIXED_POINT_MODE_P (mode));
1698 else
1700 temp = double_int_zero;
1701 temp_trunc = double_int_zero;
1704 /* If FIXED_CST is negative, we need to round the value toward 0.
1705 By checking if the fractional bits are not zero to add 1 to temp. */
1706 if (SIGNED_FIXED_POINT_MODE_P (mode)
1707 && temp_trunc.is_negative ()
1708 && TREE_FIXED_CST (arg1).data != temp_trunc)
1709 temp += double_int_one;
1711 /* Given a fixed-point constant, make new constant with new type,
1712 appropriately sign-extended or truncated. */
1713 t = force_fit_type_double (type, temp, -1,
1714 (temp.is_negative ()
1715 && (TYPE_UNSIGNED (type)
1716 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1717 | TREE_OVERFLOW (arg1));
1719 return t;
1722 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1723 to another floating point type. */
1725 static tree
1726 fold_convert_const_real_from_real (tree type, const_tree arg1)
1728 REAL_VALUE_TYPE value;
1729 tree t;
1731 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1732 t = build_real (type, value);
1734 /* If converting an infinity or NAN to a representation that doesn't
1735 have one, set the overflow bit so that we can produce some kind of
1736 error message at the appropriate point if necessary. It's not the
1737 most user-friendly message, but it's better than nothing. */
1738 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1739 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1740 TREE_OVERFLOW (t) = 1;
1741 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1742 && !MODE_HAS_NANS (TYPE_MODE (type)))
1743 TREE_OVERFLOW (t) = 1;
1744 /* Regular overflow, conversion produced an infinity in a mode that
1745 can't represent them. */
1746 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1747 && REAL_VALUE_ISINF (value)
1748 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1749 TREE_OVERFLOW (t) = 1;
1750 else
1751 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1752 return t;
1755 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1756 to a floating point type. */
1758 static tree
1759 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1761 REAL_VALUE_TYPE value;
1762 tree t;
1764 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1765 t = build_real (type, value);
1767 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1768 return t;
1771 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1772 to another fixed-point type. */
1774 static tree
1775 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1777 FIXED_VALUE_TYPE value;
1778 tree t;
1779 bool overflow_p;
1781 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1782 TYPE_SATURATING (type));
1783 t = build_fixed (type, value);
1785 /* Propagate overflow flags. */
1786 if (overflow_p | TREE_OVERFLOW (arg1))
1787 TREE_OVERFLOW (t) = 1;
1788 return t;
1791 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1792 to a fixed-point type. */
1794 static tree
1795 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1797 FIXED_VALUE_TYPE value;
1798 tree t;
1799 bool overflow_p;
1801 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
1802 TREE_INT_CST (arg1),
1803 TYPE_UNSIGNED (TREE_TYPE (arg1)),
1804 TYPE_SATURATING (type));
1805 t = build_fixed (type, value);
1807 /* Propagate overflow flags. */
1808 if (overflow_p | TREE_OVERFLOW (arg1))
1809 TREE_OVERFLOW (t) = 1;
1810 return t;
1813 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1814 to a fixed-point type. */
1816 static tree
1817 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1819 FIXED_VALUE_TYPE value;
1820 tree t;
1821 bool overflow_p;
1823 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1824 &TREE_REAL_CST (arg1),
1825 TYPE_SATURATING (type));
1826 t = build_fixed (type, value);
1828 /* Propagate overflow flags. */
1829 if (overflow_p | TREE_OVERFLOW (arg1))
1830 TREE_OVERFLOW (t) = 1;
1831 return t;
1834 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1835 type TYPE. If no simplification can be done return NULL_TREE. */
1837 static tree
1838 fold_convert_const (enum tree_code code, tree type, tree arg1)
1840 if (TREE_TYPE (arg1) == type)
1841 return arg1;
1843 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1844 || TREE_CODE (type) == OFFSET_TYPE)
1846 if (TREE_CODE (arg1) == INTEGER_CST)
1847 return fold_convert_const_int_from_int (type, arg1);
1848 else if (TREE_CODE (arg1) == REAL_CST)
1849 return fold_convert_const_int_from_real (code, type, arg1);
1850 else if (TREE_CODE (arg1) == FIXED_CST)
1851 return fold_convert_const_int_from_fixed (type, arg1);
1853 else if (TREE_CODE (type) == REAL_TYPE)
1855 if (TREE_CODE (arg1) == INTEGER_CST)
1856 return build_real_from_int_cst (type, arg1);
1857 else if (TREE_CODE (arg1) == REAL_CST)
1858 return fold_convert_const_real_from_real (type, arg1);
1859 else if (TREE_CODE (arg1) == FIXED_CST)
1860 return fold_convert_const_real_from_fixed (type, arg1);
1862 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1864 if (TREE_CODE (arg1) == FIXED_CST)
1865 return fold_convert_const_fixed_from_fixed (type, arg1);
1866 else if (TREE_CODE (arg1) == INTEGER_CST)
1867 return fold_convert_const_fixed_from_int (type, arg1);
1868 else if (TREE_CODE (arg1) == REAL_CST)
1869 return fold_convert_const_fixed_from_real (type, arg1);
1871 return NULL_TREE;
1874 /* Construct a vector of zero elements of vector type TYPE. */
1876 static tree
1877 build_zero_vector (tree type)
1879 tree t;
1881 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1882 return build_vector_from_val (type, t);
1885 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
1887 bool
1888 fold_convertible_p (const_tree type, const_tree arg)
1890 tree orig = TREE_TYPE (arg);
1892 if (type == orig)
1893 return true;
1895 if (TREE_CODE (arg) == ERROR_MARK
1896 || TREE_CODE (type) == ERROR_MARK
1897 || TREE_CODE (orig) == ERROR_MARK)
1898 return false;
1900 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1901 return true;
1903 switch (TREE_CODE (type))
1905 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1906 case POINTER_TYPE: case REFERENCE_TYPE:
1907 case OFFSET_TYPE:
1908 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1909 || TREE_CODE (orig) == OFFSET_TYPE)
1910 return true;
1911 return (TREE_CODE (orig) == VECTOR_TYPE
1912 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1914 case REAL_TYPE:
1915 case FIXED_POINT_TYPE:
1916 case COMPLEX_TYPE:
1917 case VECTOR_TYPE:
1918 case VOID_TYPE:
1919 return TREE_CODE (type) == TREE_CODE (orig);
1921 default:
1922 return false;
1926 /* Convert expression ARG to type TYPE. Used by the middle-end for
1927 simple conversions in preference to calling the front-end's convert. */
1929 tree
1930 fold_convert_loc (location_t loc, tree type, tree arg)
1932 tree orig = TREE_TYPE (arg);
1933 tree tem;
1935 if (type == orig)
1936 return arg;
1938 if (TREE_CODE (arg) == ERROR_MARK
1939 || TREE_CODE (type) == ERROR_MARK
1940 || TREE_CODE (orig) == ERROR_MARK)
1941 return error_mark_node;
1943 switch (TREE_CODE (type))
1945 case POINTER_TYPE:
1946 case REFERENCE_TYPE:
1947 /* Handle conversions between pointers to different address spaces. */
1948 if (POINTER_TYPE_P (orig)
1949 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1950 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1951 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1952 /* fall through */
1954 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1955 case OFFSET_TYPE:
1956 if (TREE_CODE (arg) == INTEGER_CST)
1958 tem = fold_convert_const (NOP_EXPR, type, arg);
1959 if (tem != NULL_TREE)
1960 return tem;
1962 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1963 || TREE_CODE (orig) == OFFSET_TYPE)
1964 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1965 if (TREE_CODE (orig) == COMPLEX_TYPE)
1966 return fold_convert_loc (loc, type,
1967 fold_build1_loc (loc, REALPART_EXPR,
1968 TREE_TYPE (orig), arg));
1969 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1970 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1971 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1973 case REAL_TYPE:
1974 if (TREE_CODE (arg) == INTEGER_CST)
1976 tem = fold_convert_const (FLOAT_EXPR, type, arg);
1977 if (tem != NULL_TREE)
1978 return tem;
1980 else if (TREE_CODE (arg) == REAL_CST)
1982 tem = fold_convert_const (NOP_EXPR, type, arg);
1983 if (tem != NULL_TREE)
1984 return tem;
1986 else if (TREE_CODE (arg) == FIXED_CST)
1988 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1989 if (tem != NULL_TREE)
1990 return tem;
1993 switch (TREE_CODE (orig))
1995 case INTEGER_TYPE:
1996 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1997 case POINTER_TYPE: case REFERENCE_TYPE:
1998 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2000 case REAL_TYPE:
2001 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2003 case FIXED_POINT_TYPE:
2004 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2006 case COMPLEX_TYPE:
2007 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2008 return fold_convert_loc (loc, type, tem);
2010 default:
2011 gcc_unreachable ();
2014 case FIXED_POINT_TYPE:
2015 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2016 || TREE_CODE (arg) == REAL_CST)
2018 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2019 if (tem != NULL_TREE)
2020 goto fold_convert_exit;
2023 switch (TREE_CODE (orig))
2025 case FIXED_POINT_TYPE:
2026 case INTEGER_TYPE:
2027 case ENUMERAL_TYPE:
2028 case BOOLEAN_TYPE:
2029 case REAL_TYPE:
2030 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2032 case COMPLEX_TYPE:
2033 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2034 return fold_convert_loc (loc, type, tem);
2036 default:
2037 gcc_unreachable ();
2040 case COMPLEX_TYPE:
2041 switch (TREE_CODE (orig))
2043 case INTEGER_TYPE:
2044 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2045 case POINTER_TYPE: case REFERENCE_TYPE:
2046 case REAL_TYPE:
2047 case FIXED_POINT_TYPE:
2048 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2049 fold_convert_loc (loc, TREE_TYPE (type), arg),
2050 fold_convert_loc (loc, TREE_TYPE (type),
2051 integer_zero_node));
2052 case COMPLEX_TYPE:
2054 tree rpart, ipart;
2056 if (TREE_CODE (arg) == COMPLEX_EXPR)
2058 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2059 TREE_OPERAND (arg, 0));
2060 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2061 TREE_OPERAND (arg, 1));
2062 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2065 arg = save_expr (arg);
2066 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2067 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2068 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2069 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2070 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2073 default:
2074 gcc_unreachable ();
2077 case VECTOR_TYPE:
2078 if (integer_zerop (arg))
2079 return build_zero_vector (type);
2080 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2081 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2082 || TREE_CODE (orig) == VECTOR_TYPE);
2083 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2085 case VOID_TYPE:
2086 tem = fold_ignored_result (arg);
2087 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2089 default:
2090 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2091 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2092 gcc_unreachable ();
2094 fold_convert_exit:
2095 protected_set_expr_location_unshare (tem, loc);
2096 return tem;
2099 /* Return false if expr can be assumed not to be an lvalue, true
2100 otherwise. */
2102 static bool
2103 maybe_lvalue_p (const_tree x)
2105 /* We only need to wrap lvalue tree codes. */
2106 switch (TREE_CODE (x))
2108 case VAR_DECL:
2109 case PARM_DECL:
2110 case RESULT_DECL:
2111 case LABEL_DECL:
2112 case FUNCTION_DECL:
2113 case SSA_NAME:
2115 case COMPONENT_REF:
2116 case MEM_REF:
2117 case INDIRECT_REF:
2118 case ARRAY_REF:
2119 case ARRAY_RANGE_REF:
2120 case BIT_FIELD_REF:
2121 case OBJ_TYPE_REF:
2123 case REALPART_EXPR:
2124 case IMAGPART_EXPR:
2125 case PREINCREMENT_EXPR:
2126 case PREDECREMENT_EXPR:
2127 case SAVE_EXPR:
2128 case TRY_CATCH_EXPR:
2129 case WITH_CLEANUP_EXPR:
2130 case COMPOUND_EXPR:
2131 case MODIFY_EXPR:
2132 case TARGET_EXPR:
2133 case COND_EXPR:
2134 case BIND_EXPR:
2135 break;
2137 default:
2138 /* Assume the worst for front-end tree codes. */
2139 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2140 break;
2141 return false;
2144 return true;
2147 /* Return an expr equal to X but certainly not valid as an lvalue. */
2149 tree
2150 non_lvalue_loc (location_t loc, tree x)
2152 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2153 us. */
2154 if (in_gimple_form)
2155 return x;
2157 if (! maybe_lvalue_p (x))
2158 return x;
2159 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2162 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2163 Zero means allow extended lvalues. */
2165 int pedantic_lvalues;
2167 /* When pedantic, return an expr equal to X but certainly not valid as a
2168 pedantic lvalue. Otherwise, return X. */
2170 static tree
2171 pedantic_non_lvalue_loc (location_t loc, tree x)
2173 if (pedantic_lvalues)
2174 return non_lvalue_loc (loc, x);
2176 return protected_set_expr_location_unshare (x, loc);
2179 /* Given a tree comparison code, return the code that is the logical inverse.
2180 It is generally not safe to do this for floating-point comparisons, except
2181 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2182 ERROR_MARK in this case. */
2184 enum tree_code
2185 invert_tree_comparison (enum tree_code code, bool honor_nans)
2187 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2188 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2189 return ERROR_MARK;
2191 switch (code)
2193 case EQ_EXPR:
2194 return NE_EXPR;
2195 case NE_EXPR:
2196 return EQ_EXPR;
2197 case GT_EXPR:
2198 return honor_nans ? UNLE_EXPR : LE_EXPR;
2199 case GE_EXPR:
2200 return honor_nans ? UNLT_EXPR : LT_EXPR;
2201 case LT_EXPR:
2202 return honor_nans ? UNGE_EXPR : GE_EXPR;
2203 case LE_EXPR:
2204 return honor_nans ? UNGT_EXPR : GT_EXPR;
2205 case LTGT_EXPR:
2206 return UNEQ_EXPR;
2207 case UNEQ_EXPR:
2208 return LTGT_EXPR;
2209 case UNGT_EXPR:
2210 return LE_EXPR;
2211 case UNGE_EXPR:
2212 return LT_EXPR;
2213 case UNLT_EXPR:
2214 return GE_EXPR;
2215 case UNLE_EXPR:
2216 return GT_EXPR;
2217 case ORDERED_EXPR:
2218 return UNORDERED_EXPR;
2219 case UNORDERED_EXPR:
2220 return ORDERED_EXPR;
2221 default:
2222 gcc_unreachable ();
2226 /* Similar, but return the comparison that results if the operands are
2227 swapped. This is safe for floating-point. */
2229 enum tree_code
2230 swap_tree_comparison (enum tree_code code)
2232 switch (code)
2234 case EQ_EXPR:
2235 case NE_EXPR:
2236 case ORDERED_EXPR:
2237 case UNORDERED_EXPR:
2238 case LTGT_EXPR:
2239 case UNEQ_EXPR:
2240 return code;
2241 case GT_EXPR:
2242 return LT_EXPR;
2243 case GE_EXPR:
2244 return LE_EXPR;
2245 case LT_EXPR:
2246 return GT_EXPR;
2247 case LE_EXPR:
2248 return GE_EXPR;
2249 case UNGT_EXPR:
2250 return UNLT_EXPR;
2251 case UNGE_EXPR:
2252 return UNLE_EXPR;
2253 case UNLT_EXPR:
2254 return UNGT_EXPR;
2255 case UNLE_EXPR:
2256 return UNGE_EXPR;
2257 default:
2258 gcc_unreachable ();
2263 /* Convert a comparison tree code from an enum tree_code representation
2264 into a compcode bit-based encoding. This function is the inverse of
2265 compcode_to_comparison. */
2267 static enum comparison_code
2268 comparison_to_compcode (enum tree_code code)
2270 switch (code)
2272 case LT_EXPR:
2273 return COMPCODE_LT;
2274 case EQ_EXPR:
2275 return COMPCODE_EQ;
2276 case LE_EXPR:
2277 return COMPCODE_LE;
2278 case GT_EXPR:
2279 return COMPCODE_GT;
2280 case NE_EXPR:
2281 return COMPCODE_NE;
2282 case GE_EXPR:
2283 return COMPCODE_GE;
2284 case ORDERED_EXPR:
2285 return COMPCODE_ORD;
2286 case UNORDERED_EXPR:
2287 return COMPCODE_UNORD;
2288 case UNLT_EXPR:
2289 return COMPCODE_UNLT;
2290 case UNEQ_EXPR:
2291 return COMPCODE_UNEQ;
2292 case UNLE_EXPR:
2293 return COMPCODE_UNLE;
2294 case UNGT_EXPR:
2295 return COMPCODE_UNGT;
2296 case LTGT_EXPR:
2297 return COMPCODE_LTGT;
2298 case UNGE_EXPR:
2299 return COMPCODE_UNGE;
2300 default:
2301 gcc_unreachable ();
2305 /* Convert a compcode bit-based encoding of a comparison operator back
2306 to GCC's enum tree_code representation. This function is the
2307 inverse of comparison_to_compcode. */
2309 static enum tree_code
2310 compcode_to_comparison (enum comparison_code code)
2312 switch (code)
2314 case COMPCODE_LT:
2315 return LT_EXPR;
2316 case COMPCODE_EQ:
2317 return EQ_EXPR;
2318 case COMPCODE_LE:
2319 return LE_EXPR;
2320 case COMPCODE_GT:
2321 return GT_EXPR;
2322 case COMPCODE_NE:
2323 return NE_EXPR;
2324 case COMPCODE_GE:
2325 return GE_EXPR;
2326 case COMPCODE_ORD:
2327 return ORDERED_EXPR;
2328 case COMPCODE_UNORD:
2329 return UNORDERED_EXPR;
2330 case COMPCODE_UNLT:
2331 return UNLT_EXPR;
2332 case COMPCODE_UNEQ:
2333 return UNEQ_EXPR;
2334 case COMPCODE_UNLE:
2335 return UNLE_EXPR;
2336 case COMPCODE_UNGT:
2337 return UNGT_EXPR;
2338 case COMPCODE_LTGT:
2339 return LTGT_EXPR;
2340 case COMPCODE_UNGE:
2341 return UNGE_EXPR;
2342 default:
2343 gcc_unreachable ();
2347 /* Return a tree for the comparison which is the combination of
2348 doing the AND or OR (depending on CODE) of the two operations LCODE
2349 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2350 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2351 if this makes the transformation invalid. */
2353 tree
2354 combine_comparisons (location_t loc,
2355 enum tree_code code, enum tree_code lcode,
2356 enum tree_code rcode, tree truth_type,
2357 tree ll_arg, tree lr_arg)
2359 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2360 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2361 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2362 int compcode;
2364 switch (code)
2366 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2367 compcode = lcompcode & rcompcode;
2368 break;
2370 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2371 compcode = lcompcode | rcompcode;
2372 break;
2374 default:
2375 return NULL_TREE;
2378 if (!honor_nans)
2380 /* Eliminate unordered comparisons, as well as LTGT and ORD
2381 which are not used unless the mode has NaNs. */
2382 compcode &= ~COMPCODE_UNORD;
2383 if (compcode == COMPCODE_LTGT)
2384 compcode = COMPCODE_NE;
2385 else if (compcode == COMPCODE_ORD)
2386 compcode = COMPCODE_TRUE;
2388 else if (flag_trapping_math)
2390 /* Check that the original operation and the optimized ones will trap
2391 under the same condition. */
2392 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2393 && (lcompcode != COMPCODE_EQ)
2394 && (lcompcode != COMPCODE_ORD);
2395 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2396 && (rcompcode != COMPCODE_EQ)
2397 && (rcompcode != COMPCODE_ORD);
2398 bool trap = (compcode & COMPCODE_UNORD) == 0
2399 && (compcode != COMPCODE_EQ)
2400 && (compcode != COMPCODE_ORD);
2402 /* In a short-circuited boolean expression the LHS might be
2403 such that the RHS, if evaluated, will never trap. For
2404 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2405 if neither x nor y is NaN. (This is a mixed blessing: for
2406 example, the expression above will never trap, hence
2407 optimizing it to x < y would be invalid). */
2408 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2409 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2410 rtrap = false;
2412 /* If the comparison was short-circuited, and only the RHS
2413 trapped, we may now generate a spurious trap. */
2414 if (rtrap && !ltrap
2415 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2416 return NULL_TREE;
2418 /* If we changed the conditions that cause a trap, we lose. */
2419 if ((ltrap || rtrap) != trap)
2420 return NULL_TREE;
2423 if (compcode == COMPCODE_TRUE)
2424 return constant_boolean_node (true, truth_type);
2425 else if (compcode == COMPCODE_FALSE)
2426 return constant_boolean_node (false, truth_type);
2427 else
2429 enum tree_code tcode;
2431 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2432 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2436 /* Return nonzero if two operands (typically of the same tree node)
2437 are necessarily equal. If either argument has side-effects this
2438 function returns zero. FLAGS modifies behavior as follows:
2440 If OEP_ONLY_CONST is set, only return nonzero for constants.
2441 This function tests whether the operands are indistinguishable;
2442 it does not test whether they are equal using C's == operation.
2443 The distinction is important for IEEE floating point, because
2444 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2445 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2447 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2448 even though it may hold multiple values during a function.
2449 This is because a GCC tree node guarantees that nothing else is
2450 executed between the evaluation of its "operands" (which may often
2451 be evaluated in arbitrary order). Hence if the operands themselves
2452 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2453 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2454 unset means assuming isochronic (or instantaneous) tree equivalence.
2455 Unless comparing arbitrary expression trees, such as from different
2456 statements, this flag can usually be left unset.
2458 If OEP_PURE_SAME is set, then pure functions with identical arguments
2459 are considered the same. It is used when the caller has other ways
2460 to ensure that global memory is unchanged in between. */
2463 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2465 /* If either is ERROR_MARK, they aren't equal. */
2466 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2467 || TREE_TYPE (arg0) == error_mark_node
2468 || TREE_TYPE (arg1) == error_mark_node)
2469 return 0;
2471 /* Similar, if either does not have a type (like a released SSA name),
2472 they aren't equal. */
2473 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2474 return 0;
2476 /* Check equality of integer constants before bailing out due to
2477 precision differences. */
2478 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2479 return tree_int_cst_equal (arg0, arg1);
2481 /* If both types don't have the same signedness, then we can't consider
2482 them equal. We must check this before the STRIP_NOPS calls
2483 because they may change the signedness of the arguments. As pointers
2484 strictly don't have a signedness, require either two pointers or
2485 two non-pointers as well. */
2486 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2487 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2488 return 0;
2490 /* We cannot consider pointers to different address space equal. */
2491 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2492 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2493 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2494 return 0;
2496 /* If both types don't have the same precision, then it is not safe
2497 to strip NOPs. */
2498 if (element_precision (TREE_TYPE (arg0))
2499 != element_precision (TREE_TYPE (arg1)))
2500 return 0;
2502 STRIP_NOPS (arg0);
2503 STRIP_NOPS (arg1);
2505 /* In case both args are comparisons but with different comparison
2506 code, try to swap the comparison operands of one arg to produce
2507 a match and compare that variant. */
2508 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2509 && COMPARISON_CLASS_P (arg0)
2510 && COMPARISON_CLASS_P (arg1))
2512 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2514 if (TREE_CODE (arg0) == swap_code)
2515 return operand_equal_p (TREE_OPERAND (arg0, 0),
2516 TREE_OPERAND (arg1, 1), flags)
2517 && operand_equal_p (TREE_OPERAND (arg0, 1),
2518 TREE_OPERAND (arg1, 0), flags);
2521 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2522 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2523 && !(CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1)))
2524 return 0;
2526 /* This is needed for conversions and for COMPONENT_REF.
2527 Might as well play it safe and always test this. */
2528 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2529 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2530 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2531 return 0;
2533 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2534 We don't care about side effects in that case because the SAVE_EXPR
2535 takes care of that for us. In all other cases, two expressions are
2536 equal if they have no side effects. If we have two identical
2537 expressions with side effects that should be treated the same due
2538 to the only side effects being identical SAVE_EXPR's, that will
2539 be detected in the recursive calls below.
2540 If we are taking an invariant address of two identical objects
2541 they are necessarily equal as well. */
2542 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2543 && (TREE_CODE (arg0) == SAVE_EXPR
2544 || (flags & OEP_CONSTANT_ADDRESS_OF)
2545 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2546 return 1;
2548 /* Next handle constant cases, those for which we can return 1 even
2549 if ONLY_CONST is set. */
2550 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2551 switch (TREE_CODE (arg0))
2553 case INTEGER_CST:
2554 return tree_int_cst_equal (arg0, arg1);
2556 case FIXED_CST:
2557 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2558 TREE_FIXED_CST (arg1));
2560 case REAL_CST:
2561 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2562 TREE_REAL_CST (arg1)))
2563 return 1;
2566 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2568 /* If we do not distinguish between signed and unsigned zero,
2569 consider them equal. */
2570 if (real_zerop (arg0) && real_zerop (arg1))
2571 return 1;
2573 return 0;
2575 case VECTOR_CST:
2577 unsigned i;
2579 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2580 return 0;
2582 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2584 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2585 VECTOR_CST_ELT (arg1, i), flags))
2586 return 0;
2588 return 1;
2591 case COMPLEX_CST:
2592 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2593 flags)
2594 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2595 flags));
2597 case STRING_CST:
2598 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2599 && ! memcmp (TREE_STRING_POINTER (arg0),
2600 TREE_STRING_POINTER (arg1),
2601 TREE_STRING_LENGTH (arg0)));
2603 case ADDR_EXPR:
2604 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2605 TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)
2606 ? OEP_CONSTANT_ADDRESS_OF : 0);
2607 default:
2608 break;
2611 if (flags & OEP_ONLY_CONST)
2612 return 0;
2614 /* Define macros to test an operand from arg0 and arg1 for equality and a
2615 variant that allows null and views null as being different from any
2616 non-null value. In the latter case, if either is null, the both
2617 must be; otherwise, do the normal comparison. */
2618 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2619 TREE_OPERAND (arg1, N), flags)
2621 #define OP_SAME_WITH_NULL(N) \
2622 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2623 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2625 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2627 case tcc_unary:
2628 /* Two conversions are equal only if signedness and modes match. */
2629 switch (TREE_CODE (arg0))
2631 CASE_CONVERT:
2632 case FIX_TRUNC_EXPR:
2633 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2634 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2635 return 0;
2636 break;
2637 default:
2638 break;
2641 return OP_SAME (0);
2644 case tcc_comparison:
2645 case tcc_binary:
2646 if (OP_SAME (0) && OP_SAME (1))
2647 return 1;
2649 /* For commutative ops, allow the other order. */
2650 return (commutative_tree_code (TREE_CODE (arg0))
2651 && operand_equal_p (TREE_OPERAND (arg0, 0),
2652 TREE_OPERAND (arg1, 1), flags)
2653 && operand_equal_p (TREE_OPERAND (arg0, 1),
2654 TREE_OPERAND (arg1, 0), flags));
2656 case tcc_reference:
2657 /* If either of the pointer (or reference) expressions we are
2658 dereferencing contain a side effect, these cannot be equal,
2659 but their addresses can be. */
2660 if ((flags & OEP_CONSTANT_ADDRESS_OF) == 0
2661 && (TREE_SIDE_EFFECTS (arg0)
2662 || TREE_SIDE_EFFECTS (arg1)))
2663 return 0;
2665 switch (TREE_CODE (arg0))
2667 case INDIRECT_REF:
2668 flags &= ~OEP_CONSTANT_ADDRESS_OF;
2669 return OP_SAME (0);
2671 case REALPART_EXPR:
2672 case IMAGPART_EXPR:
2673 return OP_SAME (0);
2675 case TARGET_MEM_REF:
2676 flags &= ~OEP_CONSTANT_ADDRESS_OF;
2677 /* Require equal extra operands and then fall through to MEM_REF
2678 handling of the two common operands. */
2679 if (!OP_SAME_WITH_NULL (2)
2680 || !OP_SAME_WITH_NULL (3)
2681 || !OP_SAME_WITH_NULL (4))
2682 return 0;
2683 /* Fallthru. */
2684 case MEM_REF:
2685 flags &= ~OEP_CONSTANT_ADDRESS_OF;
2686 /* Require equal access sizes, and similar pointer types.
2687 We can have incomplete types for array references of
2688 variable-sized arrays from the Fortran frontend
2689 though. Also verify the types are compatible. */
2690 return ((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2691 || (TYPE_SIZE (TREE_TYPE (arg0))
2692 && TYPE_SIZE (TREE_TYPE (arg1))
2693 && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2694 TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2695 && types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1))
2696 && alias_ptr_types_compatible_p
2697 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
2698 TREE_TYPE (TREE_OPERAND (arg1, 1)))
2699 && OP_SAME (0) && OP_SAME (1));
2701 case ARRAY_REF:
2702 case ARRAY_RANGE_REF:
2703 /* Operands 2 and 3 may be null.
2704 Compare the array index by value if it is constant first as we
2705 may have different types but same value here. */
2706 if (!OP_SAME (0))
2707 return 0;
2708 flags &= ~OEP_CONSTANT_ADDRESS_OF;
2709 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2710 TREE_OPERAND (arg1, 1))
2711 || OP_SAME (1))
2712 && OP_SAME_WITH_NULL (2)
2713 && OP_SAME_WITH_NULL (3));
2715 case COMPONENT_REF:
2716 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2717 may be NULL when we're called to compare MEM_EXPRs. */
2718 if (!OP_SAME_WITH_NULL (0)
2719 || !OP_SAME (1))
2720 return 0;
2721 flags &= ~OEP_CONSTANT_ADDRESS_OF;
2722 return OP_SAME_WITH_NULL (2);
2724 case BIT_FIELD_REF:
2725 if (!OP_SAME (0))
2726 return 0;
2727 flags &= ~OEP_CONSTANT_ADDRESS_OF;
2728 return OP_SAME (1) && OP_SAME (2);
2730 default:
2731 return 0;
2734 case tcc_expression:
2735 switch (TREE_CODE (arg0))
2737 case ADDR_EXPR:
2738 case TRUTH_NOT_EXPR:
2739 return OP_SAME (0);
2741 case TRUTH_ANDIF_EXPR:
2742 case TRUTH_ORIF_EXPR:
2743 return OP_SAME (0) && OP_SAME (1);
2745 case FMA_EXPR:
2746 case WIDEN_MULT_PLUS_EXPR:
2747 case WIDEN_MULT_MINUS_EXPR:
2748 if (!OP_SAME (2))
2749 return 0;
2750 /* The multiplcation operands are commutative. */
2751 /* FALLTHRU */
2753 case TRUTH_AND_EXPR:
2754 case TRUTH_OR_EXPR:
2755 case TRUTH_XOR_EXPR:
2756 if (OP_SAME (0) && OP_SAME (1))
2757 return 1;
2759 /* Otherwise take into account this is a commutative operation. */
2760 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2761 TREE_OPERAND (arg1, 1), flags)
2762 && operand_equal_p (TREE_OPERAND (arg0, 1),
2763 TREE_OPERAND (arg1, 0), flags));
2765 case COND_EXPR:
2766 case VEC_COND_EXPR:
2767 case DOT_PROD_EXPR:
2768 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2770 default:
2771 return 0;
2774 case tcc_vl_exp:
2775 switch (TREE_CODE (arg0))
2777 case CALL_EXPR:
2778 /* If the CALL_EXPRs call different functions, then they
2779 clearly can not be equal. */
2780 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2781 flags))
2782 return 0;
2785 unsigned int cef = call_expr_flags (arg0);
2786 if (flags & OEP_PURE_SAME)
2787 cef &= ECF_CONST | ECF_PURE;
2788 else
2789 cef &= ECF_CONST;
2790 if (!cef)
2791 return 0;
2794 /* Now see if all the arguments are the same. */
2796 const_call_expr_arg_iterator iter0, iter1;
2797 const_tree a0, a1;
2798 for (a0 = first_const_call_expr_arg (arg0, &iter0),
2799 a1 = first_const_call_expr_arg (arg1, &iter1);
2800 a0 && a1;
2801 a0 = next_const_call_expr_arg (&iter0),
2802 a1 = next_const_call_expr_arg (&iter1))
2803 if (! operand_equal_p (a0, a1, flags))
2804 return 0;
2806 /* If we get here and both argument lists are exhausted
2807 then the CALL_EXPRs are equal. */
2808 return ! (a0 || a1);
2810 default:
2811 return 0;
2814 case tcc_declaration:
2815 /* Consider __builtin_sqrt equal to sqrt. */
2816 return (TREE_CODE (arg0) == FUNCTION_DECL
2817 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2818 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2819 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2821 default:
2822 return 0;
2825 #undef OP_SAME
2826 #undef OP_SAME_WITH_NULL
2829 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2830 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2832 When in doubt, return 0. */
2834 static int
2835 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2837 int unsignedp1, unsignedpo;
2838 tree primarg0, primarg1, primother;
2839 unsigned int correct_width;
2841 if (operand_equal_p (arg0, arg1, 0))
2842 return 1;
2844 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2845 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2846 return 0;
2848 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2849 and see if the inner values are the same. This removes any
2850 signedness comparison, which doesn't matter here. */
2851 primarg0 = arg0, primarg1 = arg1;
2852 STRIP_NOPS (primarg0);
2853 STRIP_NOPS (primarg1);
2854 if (operand_equal_p (primarg0, primarg1, 0))
2855 return 1;
2857 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2858 actual comparison operand, ARG0.
2860 First throw away any conversions to wider types
2861 already present in the operands. */
2863 primarg1 = get_narrower (arg1, &unsignedp1);
2864 primother = get_narrower (other, &unsignedpo);
2866 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2867 if (unsignedp1 == unsignedpo
2868 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2869 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2871 tree type = TREE_TYPE (arg0);
2873 /* Make sure shorter operand is extended the right way
2874 to match the longer operand. */
2875 primarg1 = fold_convert (signed_or_unsigned_type_for
2876 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2878 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2879 return 1;
2882 return 0;
2885 /* See if ARG is an expression that is either a comparison or is performing
2886 arithmetic on comparisons. The comparisons must only be comparing
2887 two different values, which will be stored in *CVAL1 and *CVAL2; if
2888 they are nonzero it means that some operands have already been found.
2889 No variables may be used anywhere else in the expression except in the
2890 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2891 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2893 If this is true, return 1. Otherwise, return zero. */
2895 static int
2896 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2898 enum tree_code code = TREE_CODE (arg);
2899 enum tree_code_class tclass = TREE_CODE_CLASS (code);
2901 /* We can handle some of the tcc_expression cases here. */
2902 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2903 tclass = tcc_unary;
2904 else if (tclass == tcc_expression
2905 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2906 || code == COMPOUND_EXPR))
2907 tclass = tcc_binary;
2909 else if (tclass == tcc_expression && code == SAVE_EXPR
2910 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2912 /* If we've already found a CVAL1 or CVAL2, this expression is
2913 two complex to handle. */
2914 if (*cval1 || *cval2)
2915 return 0;
2917 tclass = tcc_unary;
2918 *save_p = 1;
2921 switch (tclass)
2923 case tcc_unary:
2924 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2926 case tcc_binary:
2927 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2928 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2929 cval1, cval2, save_p));
2931 case tcc_constant:
2932 return 1;
2934 case tcc_expression:
2935 if (code == COND_EXPR)
2936 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2937 cval1, cval2, save_p)
2938 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2939 cval1, cval2, save_p)
2940 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2941 cval1, cval2, save_p));
2942 return 0;
2944 case tcc_comparison:
2945 /* First see if we can handle the first operand, then the second. For
2946 the second operand, we know *CVAL1 can't be zero. It must be that
2947 one side of the comparison is each of the values; test for the
2948 case where this isn't true by failing if the two operands
2949 are the same. */
2951 if (operand_equal_p (TREE_OPERAND (arg, 0),
2952 TREE_OPERAND (arg, 1), 0))
2953 return 0;
2955 if (*cval1 == 0)
2956 *cval1 = TREE_OPERAND (arg, 0);
2957 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2959 else if (*cval2 == 0)
2960 *cval2 = TREE_OPERAND (arg, 0);
2961 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2963 else
2964 return 0;
2966 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2968 else if (*cval2 == 0)
2969 *cval2 = TREE_OPERAND (arg, 1);
2970 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2972 else
2973 return 0;
2975 return 1;
2977 default:
2978 return 0;
2982 /* ARG is a tree that is known to contain just arithmetic operations and
2983 comparisons. Evaluate the operations in the tree substituting NEW0 for
2984 any occurrence of OLD0 as an operand of a comparison and likewise for
2985 NEW1 and OLD1. */
2987 static tree
2988 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2989 tree old1, tree new1)
2991 tree type = TREE_TYPE (arg);
2992 enum tree_code code = TREE_CODE (arg);
2993 enum tree_code_class tclass = TREE_CODE_CLASS (code);
2995 /* We can handle some of the tcc_expression cases here. */
2996 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2997 tclass = tcc_unary;
2998 else if (tclass == tcc_expression
2999 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3000 tclass = tcc_binary;
3002 switch (tclass)
3004 case tcc_unary:
3005 return fold_build1_loc (loc, code, type,
3006 eval_subst (loc, TREE_OPERAND (arg, 0),
3007 old0, new0, old1, new1));
3009 case tcc_binary:
3010 return fold_build2_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));
3016 case tcc_expression:
3017 switch (code)
3019 case SAVE_EXPR:
3020 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3021 old1, new1);
3023 case COMPOUND_EXPR:
3024 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3025 old1, new1);
3027 case COND_EXPR:
3028 return fold_build3_loc (loc, code, type,
3029 eval_subst (loc, TREE_OPERAND (arg, 0),
3030 old0, new0, old1, new1),
3031 eval_subst (loc, TREE_OPERAND (arg, 1),
3032 old0, new0, old1, new1),
3033 eval_subst (loc, TREE_OPERAND (arg, 2),
3034 old0, new0, old1, new1));
3035 default:
3036 break;
3038 /* Fall through - ??? */
3040 case tcc_comparison:
3042 tree arg0 = TREE_OPERAND (arg, 0);
3043 tree arg1 = TREE_OPERAND (arg, 1);
3045 /* We need to check both for exact equality and tree equality. The
3046 former will be true if the operand has a side-effect. In that
3047 case, we know the operand occurred exactly once. */
3049 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3050 arg0 = new0;
3051 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3052 arg0 = new1;
3054 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3055 arg1 = new0;
3056 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3057 arg1 = new1;
3059 return fold_build2_loc (loc, code, type, arg0, arg1);
3062 default:
3063 return arg;
3067 /* Return a tree for the case when the result of an expression is RESULT
3068 converted to TYPE and OMITTED was previously an operand of the expression
3069 but is now not needed (e.g., we folded OMITTED * 0).
3071 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3072 the conversion of RESULT to TYPE. */
3074 tree
3075 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3077 tree t = fold_convert_loc (loc, type, result);
3079 /* If the resulting operand is an empty statement, just return the omitted
3080 statement casted to void. */
3081 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3082 return build1_loc (loc, NOP_EXPR, void_type_node,
3083 fold_ignored_result (omitted));
3085 if (TREE_SIDE_EFFECTS (omitted))
3086 return build2_loc (loc, COMPOUND_EXPR, type,
3087 fold_ignored_result (omitted), t);
3089 return non_lvalue_loc (loc, t);
3092 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3094 static tree
3095 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
3096 tree omitted)
3098 tree t = fold_convert_loc (loc, type, result);
3100 /* If the resulting operand is an empty statement, just return the omitted
3101 statement casted to void. */
3102 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3103 return build1_loc (loc, NOP_EXPR, void_type_node,
3104 fold_ignored_result (omitted));
3106 if (TREE_SIDE_EFFECTS (omitted))
3107 return build2_loc (loc, COMPOUND_EXPR, type,
3108 fold_ignored_result (omitted), t);
3110 return pedantic_non_lvalue_loc (loc, t);
3113 /* Return a tree for the case when the result of an expression is RESULT
3114 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3115 of the expression but are now not needed.
3117 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3118 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3119 evaluated before OMITTED2. Otherwise, if neither has side effects,
3120 just do the conversion of RESULT to TYPE. */
3122 tree
3123 omit_two_operands_loc (location_t loc, tree type, tree result,
3124 tree omitted1, tree omitted2)
3126 tree t = fold_convert_loc (loc, type, result);
3128 if (TREE_SIDE_EFFECTS (omitted2))
3129 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3130 if (TREE_SIDE_EFFECTS (omitted1))
3131 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3133 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3137 /* Return a simplified tree node for the truth-negation of ARG. This
3138 never alters ARG itself. We assume that ARG is an operation that
3139 returns a truth value (0 or 1).
3141 FIXME: one would think we would fold the result, but it causes
3142 problems with the dominator optimizer. */
3144 static tree
3145 fold_truth_not_expr (location_t loc, tree arg)
3147 tree type = TREE_TYPE (arg);
3148 enum tree_code code = TREE_CODE (arg);
3149 location_t loc1, loc2;
3151 /* If this is a comparison, we can simply invert it, except for
3152 floating-point non-equality comparisons, in which case we just
3153 enclose a TRUTH_NOT_EXPR around what we have. */
3155 if (TREE_CODE_CLASS (code) == tcc_comparison)
3157 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3158 if (FLOAT_TYPE_P (op_type)
3159 && flag_trapping_math
3160 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3161 && code != NE_EXPR && code != EQ_EXPR)
3162 return NULL_TREE;
3164 code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3165 if (code == ERROR_MARK)
3166 return NULL_TREE;
3168 return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3169 TREE_OPERAND (arg, 1));
3172 switch (code)
3174 case INTEGER_CST:
3175 return constant_boolean_node (integer_zerop (arg), type);
3177 case TRUTH_AND_EXPR:
3178 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3179 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3180 return build2_loc (loc, TRUTH_OR_EXPR, type,
3181 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3182 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3184 case TRUTH_OR_EXPR:
3185 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3186 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3187 return build2_loc (loc, TRUTH_AND_EXPR, type,
3188 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3189 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3191 case TRUTH_XOR_EXPR:
3192 /* Here we can invert either operand. We invert the first operand
3193 unless the second operand is a TRUTH_NOT_EXPR in which case our
3194 result is the XOR of the first operand with the inside of the
3195 negation of the second operand. */
3197 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3198 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3199 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3200 else
3201 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3202 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3203 TREE_OPERAND (arg, 1));
3205 case TRUTH_ANDIF_EXPR:
3206 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3207 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3208 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3209 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3210 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3212 case TRUTH_ORIF_EXPR:
3213 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3214 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3215 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3216 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3217 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3219 case TRUTH_NOT_EXPR:
3220 return TREE_OPERAND (arg, 0);
3222 case COND_EXPR:
3224 tree arg1 = TREE_OPERAND (arg, 1);
3225 tree arg2 = TREE_OPERAND (arg, 2);
3227 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3228 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3230 /* A COND_EXPR may have a throw as one operand, which
3231 then has void type. Just leave void operands
3232 as they are. */
3233 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3234 VOID_TYPE_P (TREE_TYPE (arg1))
3235 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3236 VOID_TYPE_P (TREE_TYPE (arg2))
3237 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3240 case COMPOUND_EXPR:
3241 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3242 return build2_loc (loc, COMPOUND_EXPR, type,
3243 TREE_OPERAND (arg, 0),
3244 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3246 case NON_LVALUE_EXPR:
3247 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3248 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3250 CASE_CONVERT:
3251 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3252 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3254 /* ... fall through ... */
3256 case FLOAT_EXPR:
3257 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3258 return build1_loc (loc, TREE_CODE (arg), type,
3259 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3261 case BIT_AND_EXPR:
3262 if (!integer_onep (TREE_OPERAND (arg, 1)))
3263 return NULL_TREE;
3264 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3266 case SAVE_EXPR:
3267 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3269 case CLEANUP_POINT_EXPR:
3270 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3271 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3272 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3274 default:
3275 return NULL_TREE;
3279 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3280 assume that ARG is an operation that returns a truth value (0 or 1
3281 for scalars, 0 or -1 for vectors). Return the folded expression if
3282 folding is successful. Otherwise, return NULL_TREE. */
3284 static tree
3285 fold_invert_truthvalue (location_t loc, tree arg)
3287 tree type = TREE_TYPE (arg);
3288 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3289 ? BIT_NOT_EXPR
3290 : TRUTH_NOT_EXPR,
3291 type, arg);
3294 /* Return a simplified tree node for the truth-negation of ARG. This
3295 never alters ARG itself. We assume that ARG is an operation that
3296 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3298 tree
3299 invert_truthvalue_loc (location_t loc, tree arg)
3301 if (TREE_CODE (arg) == ERROR_MARK)
3302 return arg;
3304 tree type = TREE_TYPE (arg);
3305 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3306 ? BIT_NOT_EXPR
3307 : TRUTH_NOT_EXPR,
3308 type, arg);
3311 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3312 operands are another bit-wise operation with a common input. If so,
3313 distribute the bit operations to save an operation and possibly two if
3314 constants are involved. For example, convert
3315 (A | B) & (A | C) into A | (B & C)
3316 Further simplification will occur if B and C are constants.
3318 If this optimization cannot be done, 0 will be returned. */
3320 static tree
3321 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3322 tree arg0, tree arg1)
3324 tree common;
3325 tree left, right;
3327 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3328 || TREE_CODE (arg0) == code
3329 || (TREE_CODE (arg0) != BIT_AND_EXPR
3330 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3331 return 0;
3333 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3335 common = TREE_OPERAND (arg0, 0);
3336 left = TREE_OPERAND (arg0, 1);
3337 right = TREE_OPERAND (arg1, 1);
3339 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3341 common = TREE_OPERAND (arg0, 0);
3342 left = TREE_OPERAND (arg0, 1);
3343 right = TREE_OPERAND (arg1, 0);
3345 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3347 common = TREE_OPERAND (arg0, 1);
3348 left = TREE_OPERAND (arg0, 0);
3349 right = TREE_OPERAND (arg1, 1);
3351 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3353 common = TREE_OPERAND (arg0, 1);
3354 left = TREE_OPERAND (arg0, 0);
3355 right = TREE_OPERAND (arg1, 0);
3357 else
3358 return 0;
3360 common = fold_convert_loc (loc, type, common);
3361 left = fold_convert_loc (loc, type, left);
3362 right = fold_convert_loc (loc, type, right);
3363 return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3364 fold_build2_loc (loc, code, type, left, right));
3367 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3368 with code CODE. This optimization is unsafe. */
3369 static tree
3370 distribute_real_division (location_t loc, enum tree_code code, tree type,
3371 tree arg0, tree arg1)
3373 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3374 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3376 /* (A / C) +- (B / C) -> (A +- B) / C. */
3377 if (mul0 == mul1
3378 && operand_equal_p (TREE_OPERAND (arg0, 1),
3379 TREE_OPERAND (arg1, 1), 0))
3380 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3381 fold_build2_loc (loc, code, type,
3382 TREE_OPERAND (arg0, 0),
3383 TREE_OPERAND (arg1, 0)),
3384 TREE_OPERAND (arg0, 1));
3386 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3387 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3388 TREE_OPERAND (arg1, 0), 0)
3389 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3390 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3392 REAL_VALUE_TYPE r0, r1;
3393 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3394 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3395 if (!mul0)
3396 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3397 if (!mul1)
3398 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3399 real_arithmetic (&r0, code, &r0, &r1);
3400 return fold_build2_loc (loc, MULT_EXPR, type,
3401 TREE_OPERAND (arg0, 0),
3402 build_real (type, r0));
3405 return NULL_TREE;
3408 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3409 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3411 static tree
3412 make_bit_field_ref (location_t loc, tree inner, tree type,
3413 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3415 tree result, bftype;
3417 if (bitpos == 0)
3419 tree size = TYPE_SIZE (TREE_TYPE (inner));
3420 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3421 || POINTER_TYPE_P (TREE_TYPE (inner)))
3422 && host_integerp (size, 0)
3423 && tree_low_cst (size, 0) == bitsize)
3424 return fold_convert_loc (loc, type, inner);
3427 bftype = type;
3428 if (TYPE_PRECISION (bftype) != bitsize
3429 || TYPE_UNSIGNED (bftype) == !unsignedp)
3430 bftype = build_nonstandard_integer_type (bitsize, 0);
3432 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3433 size_int (bitsize), bitsize_int (bitpos));
3435 if (bftype != type)
3436 result = fold_convert_loc (loc, type, result);
3438 return result;
3441 /* Optimize a bit-field compare.
3443 There are two cases: First is a compare against a constant and the
3444 second is a comparison of two items where the fields are at the same
3445 bit position relative to the start of a chunk (byte, halfword, word)
3446 large enough to contain it. In these cases we can avoid the shift
3447 implicit in bitfield extractions.
3449 For constants, we emit a compare of the shifted constant with the
3450 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3451 compared. For two fields at the same position, we do the ANDs with the
3452 similar mask and compare the result of the ANDs.
3454 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3455 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3456 are the left and right operands of the comparison, respectively.
3458 If the optimization described above can be done, we return the resulting
3459 tree. Otherwise we return zero. */
3461 static tree
3462 optimize_bit_field_compare (location_t loc, enum tree_code code,
3463 tree compare_type, tree lhs, tree rhs)
3465 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3466 tree type = TREE_TYPE (lhs);
3467 tree signed_type, unsigned_type;
3468 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3469 enum machine_mode lmode, rmode, nmode;
3470 int lunsignedp, runsignedp;
3471 int lvolatilep = 0, rvolatilep = 0;
3472 tree linner, rinner = NULL_TREE;
3473 tree mask;
3474 tree offset;
3476 /* Get all the information about the extractions being done. If the bit size
3477 if the same as the size of the underlying object, we aren't doing an
3478 extraction at all and so can do nothing. We also don't want to
3479 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3480 then will no longer be able to replace it. */
3481 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3482 &lunsignedp, &lvolatilep, false);
3483 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3484 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3485 return 0;
3487 if (!const_p)
3489 /* If this is not a constant, we can only do something if bit positions,
3490 sizes, and signedness are the same. */
3491 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3492 &runsignedp, &rvolatilep, false);
3494 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3495 || lunsignedp != runsignedp || offset != 0
3496 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3497 return 0;
3500 /* See if we can find a mode to refer to this field. We should be able to,
3501 but fail if we can't. */
3502 nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3503 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3504 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3505 TYPE_ALIGN (TREE_TYPE (rinner))),
3506 word_mode, false);
3507 if (nmode == VOIDmode)
3508 return 0;
3510 /* Set signed and unsigned types of the precision of this mode for the
3511 shifts below. */
3512 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3513 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3515 /* Compute the bit position and size for the new reference and our offset
3516 within it. If the new reference is the same size as the original, we
3517 won't optimize anything, so return zero. */
3518 nbitsize = GET_MODE_BITSIZE (nmode);
3519 nbitpos = lbitpos & ~ (nbitsize - 1);
3520 lbitpos -= nbitpos;
3521 if (nbitsize == lbitsize)
3522 return 0;
3524 if (BYTES_BIG_ENDIAN)
3525 lbitpos = nbitsize - lbitsize - lbitpos;
3527 /* Make the mask to be used against the extracted field. */
3528 mask = build_int_cst_type (unsigned_type, -1);
3529 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3530 mask = const_binop (RSHIFT_EXPR, mask,
3531 size_int (nbitsize - lbitsize - lbitpos));
3533 if (! const_p)
3534 /* If not comparing with constant, just rework the comparison
3535 and return. */
3536 return fold_build2_loc (loc, code, compare_type,
3537 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3538 make_bit_field_ref (loc, linner,
3539 unsigned_type,
3540 nbitsize, nbitpos,
3542 mask),
3543 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3544 make_bit_field_ref (loc, rinner,
3545 unsigned_type,
3546 nbitsize, nbitpos,
3548 mask));
3550 /* Otherwise, we are handling the constant case. See if the constant is too
3551 big for the field. Warn and return a tree of for 0 (false) if so. We do
3552 this not only for its own sake, but to avoid having to test for this
3553 error case below. If we didn't, we might generate wrong code.
3555 For unsigned fields, the constant shifted right by the field length should
3556 be all zero. For signed fields, the high-order bits should agree with
3557 the sign bit. */
3559 if (lunsignedp)
3561 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3562 fold_convert_loc (loc,
3563 unsigned_type, rhs),
3564 size_int (lbitsize))))
3566 warning (0, "comparison is always %d due to width of bit-field",
3567 code == NE_EXPR);
3568 return constant_boolean_node (code == NE_EXPR, compare_type);
3571 else
3573 tree tem = const_binop (RSHIFT_EXPR,
3574 fold_convert_loc (loc, signed_type, rhs),
3575 size_int (lbitsize - 1));
3576 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3578 warning (0, "comparison is always %d due to width of bit-field",
3579 code == NE_EXPR);
3580 return constant_boolean_node (code == NE_EXPR, compare_type);
3584 /* Single-bit compares should always be against zero. */
3585 if (lbitsize == 1 && ! integer_zerop (rhs))
3587 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3588 rhs = build_int_cst (type, 0);
3591 /* Make a new bitfield reference, shift the constant over the
3592 appropriate number of bits and mask it with the computed mask
3593 (in case this was a signed field). If we changed it, make a new one. */
3594 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3596 rhs = const_binop (BIT_AND_EXPR,
3597 const_binop (LSHIFT_EXPR,
3598 fold_convert_loc (loc, unsigned_type, rhs),
3599 size_int (lbitpos)),
3600 mask);
3602 lhs = build2_loc (loc, code, compare_type,
3603 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3604 return lhs;
3607 /* Subroutine for fold_truth_andor_1: decode a field reference.
3609 If EXP is a comparison reference, we return the innermost reference.
3611 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3612 set to the starting bit number.
3614 If the innermost field can be completely contained in a mode-sized
3615 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3617 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3618 otherwise it is not changed.
3620 *PUNSIGNEDP is set to the signedness of the field.
3622 *PMASK is set to the mask used. This is either contained in a
3623 BIT_AND_EXPR or derived from the width of the field.
3625 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3627 Return 0 if this is not a component reference or is one that we can't
3628 do anything with. */
3630 static tree
3631 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3632 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3633 int *punsignedp, int *pvolatilep,
3634 tree *pmask, tree *pand_mask)
3636 tree outer_type = 0;
3637 tree and_mask = 0;
3638 tree mask, inner, offset;
3639 tree unsigned_type;
3640 unsigned int precision;
3642 /* All the optimizations using this function assume integer fields.
3643 There are problems with FP fields since the type_for_size call
3644 below can fail for, e.g., XFmode. */
3645 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3646 return 0;
3648 /* We are interested in the bare arrangement of bits, so strip everything
3649 that doesn't affect the machine mode. However, record the type of the
3650 outermost expression if it may matter below. */
3651 if (CONVERT_EXPR_P (exp)
3652 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3653 outer_type = TREE_TYPE (exp);
3654 STRIP_NOPS (exp);
3656 if (TREE_CODE (exp) == BIT_AND_EXPR)
3658 and_mask = TREE_OPERAND (exp, 1);
3659 exp = TREE_OPERAND (exp, 0);
3660 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3661 if (TREE_CODE (and_mask) != INTEGER_CST)
3662 return 0;
3665 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3666 punsignedp, pvolatilep, false);
3667 if ((inner == exp && and_mask == 0)
3668 || *pbitsize < 0 || offset != 0
3669 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3670 return 0;
3672 /* If the number of bits in the reference is the same as the bitsize of
3673 the outer type, then the outer type gives the signedness. Otherwise
3674 (in case of a small bitfield) the signedness is unchanged. */
3675 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3676 *punsignedp = TYPE_UNSIGNED (outer_type);
3678 /* Compute the mask to access the bitfield. */
3679 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3680 precision = TYPE_PRECISION (unsigned_type);
3682 mask = build_int_cst_type (unsigned_type, -1);
3684 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3685 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3687 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3688 if (and_mask != 0)
3689 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3690 fold_convert_loc (loc, unsigned_type, and_mask), mask);
3692 *pmask = mask;
3693 *pand_mask = and_mask;
3694 return inner;
3697 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3698 bit positions. */
3700 static int
3701 all_ones_mask_p (const_tree mask, int size)
3703 tree type = TREE_TYPE (mask);
3704 unsigned int precision = TYPE_PRECISION (type);
3705 tree tmask;
3707 tmask = build_int_cst_type (signed_type_for (type), -1);
3709 return
3710 tree_int_cst_equal (mask,
3711 const_binop (RSHIFT_EXPR,
3712 const_binop (LSHIFT_EXPR, tmask,
3713 size_int (precision - size)),
3714 size_int (precision - size)));
3717 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3718 represents the sign bit of EXP's type. If EXP represents a sign
3719 or zero extension, also test VAL against the unextended type.
3720 The return value is the (sub)expression whose sign bit is VAL,
3721 or NULL_TREE otherwise. */
3723 static tree
3724 sign_bit_p (tree exp, const_tree val)
3726 unsigned HOST_WIDE_INT mask_lo, lo;
3727 HOST_WIDE_INT mask_hi, hi;
3728 int width;
3729 tree t;
3731 /* Tree EXP must have an integral type. */
3732 t = TREE_TYPE (exp);
3733 if (! INTEGRAL_TYPE_P (t))
3734 return NULL_TREE;
3736 /* Tree VAL must be an integer constant. */
3737 if (TREE_CODE (val) != INTEGER_CST
3738 || TREE_OVERFLOW (val))
3739 return NULL_TREE;
3741 width = TYPE_PRECISION (t);
3742 if (width > HOST_BITS_PER_WIDE_INT)
3744 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3745 lo = 0;
3747 mask_hi = (HOST_WIDE_INT_M1U >> (HOST_BITS_PER_DOUBLE_INT - width));
3748 mask_lo = -1;
3750 else
3752 hi = 0;
3753 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3755 mask_hi = 0;
3756 mask_lo = (HOST_WIDE_INT_M1U >> (HOST_BITS_PER_WIDE_INT - width));
3759 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3760 treat VAL as if it were unsigned. */
3761 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3762 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3763 return exp;
3765 /* Handle extension from a narrower type. */
3766 if (TREE_CODE (exp) == NOP_EXPR
3767 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3768 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3770 return NULL_TREE;
3773 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
3774 to be evaluated unconditionally. */
3776 static int
3777 simple_operand_p (const_tree exp)
3779 /* Strip any conversions that don't change the machine mode. */
3780 STRIP_NOPS (exp);
3782 return (CONSTANT_CLASS_P (exp)
3783 || TREE_CODE (exp) == SSA_NAME
3784 || (DECL_P (exp)
3785 && ! TREE_ADDRESSABLE (exp)
3786 && ! TREE_THIS_VOLATILE (exp)
3787 && ! DECL_NONLOCAL (exp)
3788 /* Don't regard global variables as simple. They may be
3789 allocated in ways unknown to the compiler (shared memory,
3790 #pragma weak, etc). */
3791 && ! TREE_PUBLIC (exp)
3792 && ! DECL_EXTERNAL (exp)
3793 /* Weakrefs are not safe to be read, since they can be NULL.
3794 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
3795 have DECL_WEAK flag set. */
3796 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
3797 /* Loading a static variable is unduly expensive, but global
3798 registers aren't expensive. */
3799 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3802 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
3803 to be evaluated unconditionally.
3804 I addition to simple_operand_p, we assume that comparisons, conversions,
3805 and logic-not operations are simple, if their operands are simple, too. */
3807 static bool
3808 simple_operand_p_2 (tree exp)
3810 enum tree_code code;
3812 if (TREE_SIDE_EFFECTS (exp)
3813 || tree_could_trap_p (exp))
3814 return false;
3816 while (CONVERT_EXPR_P (exp))
3817 exp = TREE_OPERAND (exp, 0);
3819 code = TREE_CODE (exp);
3821 if (TREE_CODE_CLASS (code) == tcc_comparison)
3822 return (simple_operand_p (TREE_OPERAND (exp, 0))
3823 && simple_operand_p (TREE_OPERAND (exp, 1)));
3825 if (code == TRUTH_NOT_EXPR)
3826 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
3828 return simple_operand_p (exp);
3832 /* The following functions are subroutines to fold_range_test and allow it to
3833 try to change a logical combination of comparisons into a range test.
3835 For example, both
3836 X == 2 || X == 3 || X == 4 || X == 5
3838 X >= 2 && X <= 5
3839 are converted to
3840 (unsigned) (X - 2) <= 3
3842 We describe each set of comparisons as being either inside or outside
3843 a range, using a variable named like IN_P, and then describe the
3844 range with a lower and upper bound. If one of the bounds is omitted,
3845 it represents either the highest or lowest value of the type.
3847 In the comments below, we represent a range by two numbers in brackets
3848 preceded by a "+" to designate being inside that range, or a "-" to
3849 designate being outside that range, so the condition can be inverted by
3850 flipping the prefix. An omitted bound is represented by a "-". For
3851 example, "- [-, 10]" means being outside the range starting at the lowest
3852 possible value and ending at 10, in other words, being greater than 10.
3853 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3854 always false.
3856 We set up things so that the missing bounds are handled in a consistent
3857 manner so neither a missing bound nor "true" and "false" need to be
3858 handled using a special case. */
3860 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3861 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3862 and UPPER1_P are nonzero if the respective argument is an upper bound
3863 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3864 must be specified for a comparison. ARG1 will be converted to ARG0's
3865 type if both are specified. */
3867 static tree
3868 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3869 tree arg1, int upper1_p)
3871 tree tem;
3872 int result;
3873 int sgn0, sgn1;
3875 /* If neither arg represents infinity, do the normal operation.
3876 Else, if not a comparison, return infinity. Else handle the special
3877 comparison rules. Note that most of the cases below won't occur, but
3878 are handled for consistency. */
3880 if (arg0 != 0 && arg1 != 0)
3882 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3883 arg0, fold_convert (TREE_TYPE (arg0), arg1));
3884 STRIP_NOPS (tem);
3885 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3888 if (TREE_CODE_CLASS (code) != tcc_comparison)
3889 return 0;
3891 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3892 for neither. In real maths, we cannot assume open ended ranges are
3893 the same. But, this is computer arithmetic, where numbers are finite.
3894 We can therefore make the transformation of any unbounded range with
3895 the value Z, Z being greater than any representable number. This permits
3896 us to treat unbounded ranges as equal. */
3897 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3898 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3899 switch (code)
3901 case EQ_EXPR:
3902 result = sgn0 == sgn1;
3903 break;
3904 case NE_EXPR:
3905 result = sgn0 != sgn1;
3906 break;
3907 case LT_EXPR:
3908 result = sgn0 < sgn1;
3909 break;
3910 case LE_EXPR:
3911 result = sgn0 <= sgn1;
3912 break;
3913 case GT_EXPR:
3914 result = sgn0 > sgn1;
3915 break;
3916 case GE_EXPR:
3917 result = sgn0 >= sgn1;
3918 break;
3919 default:
3920 gcc_unreachable ();
3923 return constant_boolean_node (result, type);
3926 /* Helper routine for make_range. Perform one step for it, return
3927 new expression if the loop should continue or NULL_TREE if it should
3928 stop. */
3930 tree
3931 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
3932 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
3933 bool *strict_overflow_p)
3935 tree arg0_type = TREE_TYPE (arg0);
3936 tree n_low, n_high, low = *p_low, high = *p_high;
3937 int in_p = *p_in_p, n_in_p;
3939 switch (code)
3941 case TRUTH_NOT_EXPR:
3942 /* We can only do something if the range is testing for zero. */
3943 if (low == NULL_TREE || high == NULL_TREE
3944 || ! integer_zerop (low) || ! integer_zerop (high))
3945 return NULL_TREE;
3946 *p_in_p = ! in_p;
3947 return arg0;
3949 case EQ_EXPR: case NE_EXPR:
3950 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3951 /* We can only do something if the range is testing for zero
3952 and if the second operand is an integer constant. Note that
3953 saying something is "in" the range we make is done by
3954 complementing IN_P since it will set in the initial case of
3955 being not equal to zero; "out" is leaving it alone. */
3956 if (low == NULL_TREE || high == NULL_TREE
3957 || ! integer_zerop (low) || ! integer_zerop (high)
3958 || TREE_CODE (arg1) != INTEGER_CST)
3959 return NULL_TREE;
3961 switch (code)
3963 case NE_EXPR: /* - [c, c] */
3964 low = high = arg1;
3965 break;
3966 case EQ_EXPR: /* + [c, c] */
3967 in_p = ! in_p, low = high = arg1;
3968 break;
3969 case GT_EXPR: /* - [-, c] */
3970 low = 0, high = arg1;
3971 break;
3972 case GE_EXPR: /* + [c, -] */
3973 in_p = ! in_p, low = arg1, high = 0;
3974 break;
3975 case LT_EXPR: /* - [c, -] */
3976 low = arg1, high = 0;
3977 break;
3978 case LE_EXPR: /* + [-, c] */
3979 in_p = ! in_p, low = 0, high = arg1;
3980 break;
3981 default:
3982 gcc_unreachable ();
3985 /* If this is an unsigned comparison, we also know that EXP is
3986 greater than or equal to zero. We base the range tests we make
3987 on that fact, so we record it here so we can parse existing
3988 range tests. We test arg0_type since often the return type
3989 of, e.g. EQ_EXPR, is boolean. */
3990 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3992 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3993 in_p, low, high, 1,
3994 build_int_cst (arg0_type, 0),
3995 NULL_TREE))
3996 return NULL_TREE;
3998 in_p = n_in_p, low = n_low, high = n_high;
4000 /* If the high bound is missing, but we have a nonzero low
4001 bound, reverse the range so it goes from zero to the low bound
4002 minus 1. */
4003 if (high == 0 && low && ! integer_zerop (low))
4005 in_p = ! in_p;
4006 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4007 integer_one_node, 0);
4008 low = build_int_cst (arg0_type, 0);
4012 *p_low = low;
4013 *p_high = high;
4014 *p_in_p = in_p;
4015 return arg0;
4017 case NEGATE_EXPR:
4018 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4019 low and high are non-NULL, then normalize will DTRT. */
4020 if (!TYPE_UNSIGNED (arg0_type)
4021 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4023 if (low == NULL_TREE)
4024 low = TYPE_MIN_VALUE (arg0_type);
4025 if (high == NULL_TREE)
4026 high = TYPE_MAX_VALUE (arg0_type);
4029 /* (-x) IN [a,b] -> x in [-b, -a] */
4030 n_low = range_binop (MINUS_EXPR, exp_type,
4031 build_int_cst (exp_type, 0),
4032 0, high, 1);
4033 n_high = range_binop (MINUS_EXPR, exp_type,
4034 build_int_cst (exp_type, 0),
4035 0, low, 0);
4036 if (n_high != 0 && TREE_OVERFLOW (n_high))
4037 return NULL_TREE;
4038 goto normalize;
4040 case BIT_NOT_EXPR:
4041 /* ~ X -> -X - 1 */
4042 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4043 build_int_cst (exp_type, 1));
4045 case PLUS_EXPR:
4046 case MINUS_EXPR:
4047 if (TREE_CODE (arg1) != INTEGER_CST)
4048 return NULL_TREE;
4050 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4051 move a constant to the other side. */
4052 if (!TYPE_UNSIGNED (arg0_type)
4053 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4054 return NULL_TREE;
4056 /* If EXP is signed, any overflow in the computation is undefined,
4057 so we don't worry about it so long as our computations on
4058 the bounds don't overflow. For unsigned, overflow is defined
4059 and this is exactly the right thing. */
4060 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4061 arg0_type, low, 0, arg1, 0);
4062 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4063 arg0_type, high, 1, arg1, 0);
4064 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4065 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4066 return NULL_TREE;
4068 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4069 *strict_overflow_p = true;
4071 normalize:
4072 /* Check for an unsigned range which has wrapped around the maximum
4073 value thus making n_high < n_low, and normalize it. */
4074 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4076 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4077 integer_one_node, 0);
4078 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4079 integer_one_node, 0);
4081 /* If the range is of the form +/- [ x+1, x ], we won't
4082 be able to normalize it. But then, it represents the
4083 whole range or the empty set, so make it
4084 +/- [ -, - ]. */
4085 if (tree_int_cst_equal (n_low, low)
4086 && tree_int_cst_equal (n_high, high))
4087 low = high = 0;
4088 else
4089 in_p = ! in_p;
4091 else
4092 low = n_low, high = n_high;
4094 *p_low = low;
4095 *p_high = high;
4096 *p_in_p = in_p;
4097 return arg0;
4099 CASE_CONVERT:
4100 case NON_LVALUE_EXPR:
4101 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4102 return NULL_TREE;
4104 if (! INTEGRAL_TYPE_P (arg0_type)
4105 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4106 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4107 return NULL_TREE;
4109 n_low = low, n_high = high;
4111 if (n_low != 0)
4112 n_low = fold_convert_loc (loc, arg0_type, n_low);
4114 if (n_high != 0)
4115 n_high = fold_convert_loc (loc, arg0_type, n_high);
4117 /* If we're converting arg0 from an unsigned type, to exp,
4118 a signed type, we will be doing the comparison as unsigned.
4119 The tests above have already verified that LOW and HIGH
4120 are both positive.
4122 So we have to ensure that we will handle large unsigned
4123 values the same way that the current signed bounds treat
4124 negative values. */
4126 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4128 tree high_positive;
4129 tree equiv_type;
4130 /* For fixed-point modes, we need to pass the saturating flag
4131 as the 2nd parameter. */
4132 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4133 equiv_type
4134 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4135 TYPE_SATURATING (arg0_type));
4136 else
4137 equiv_type
4138 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4140 /* A range without an upper bound is, naturally, unbounded.
4141 Since convert would have cropped a very large value, use
4142 the max value for the destination type. */
4143 high_positive
4144 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4145 : TYPE_MAX_VALUE (arg0_type);
4147 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4148 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4149 fold_convert_loc (loc, arg0_type,
4150 high_positive),
4151 build_int_cst (arg0_type, 1));
4153 /* If the low bound is specified, "and" the range with the
4154 range for which the original unsigned value will be
4155 positive. */
4156 if (low != 0)
4158 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4159 1, fold_convert_loc (loc, arg0_type,
4160 integer_zero_node),
4161 high_positive))
4162 return NULL_TREE;
4164 in_p = (n_in_p == in_p);
4166 else
4168 /* Otherwise, "or" the range with the range of the input
4169 that will be interpreted as negative. */
4170 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4171 1, fold_convert_loc (loc, arg0_type,
4172 integer_zero_node),
4173 high_positive))
4174 return NULL_TREE;
4176 in_p = (in_p != n_in_p);
4180 *p_low = n_low;
4181 *p_high = n_high;
4182 *p_in_p = in_p;
4183 return arg0;
4185 default:
4186 return NULL_TREE;
4190 /* Given EXP, a logical expression, set the range it is testing into
4191 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4192 actually being tested. *PLOW and *PHIGH will be made of the same
4193 type as the returned expression. If EXP is not a comparison, we
4194 will most likely not be returning a useful value and range. Set
4195 *STRICT_OVERFLOW_P to true if the return value is only valid
4196 because signed overflow is undefined; otherwise, do not change
4197 *STRICT_OVERFLOW_P. */
4199 tree
4200 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4201 bool *strict_overflow_p)
4203 enum tree_code code;
4204 tree arg0, arg1 = NULL_TREE;
4205 tree exp_type, nexp;
4206 int in_p;
4207 tree low, high;
4208 location_t loc = EXPR_LOCATION (exp);
4210 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4211 and see if we can refine the range. Some of the cases below may not
4212 happen, but it doesn't seem worth worrying about this. We "continue"
4213 the outer loop when we've changed something; otherwise we "break"
4214 the switch, which will "break" the while. */
4216 in_p = 0;
4217 low = high = build_int_cst (TREE_TYPE (exp), 0);
4219 while (1)
4221 code = TREE_CODE (exp);
4222 exp_type = TREE_TYPE (exp);
4223 arg0 = NULL_TREE;
4225 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4227 if (TREE_OPERAND_LENGTH (exp) > 0)
4228 arg0 = TREE_OPERAND (exp, 0);
4229 if (TREE_CODE_CLASS (code) == tcc_binary
4230 || TREE_CODE_CLASS (code) == tcc_comparison
4231 || (TREE_CODE_CLASS (code) == tcc_expression
4232 && TREE_OPERAND_LENGTH (exp) > 1))
4233 arg1 = TREE_OPERAND (exp, 1);
4235 if (arg0 == NULL_TREE)
4236 break;
4238 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4239 &high, &in_p, strict_overflow_p);
4240 if (nexp == NULL_TREE)
4241 break;
4242 exp = nexp;
4245 /* If EXP is a constant, we can evaluate whether this is true or false. */
4246 if (TREE_CODE (exp) == INTEGER_CST)
4248 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4249 exp, 0, low, 0))
4250 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4251 exp, 1, high, 1)));
4252 low = high = 0;
4253 exp = 0;
4256 *pin_p = in_p, *plow = low, *phigh = high;
4257 return exp;
4260 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4261 type, TYPE, return an expression to test if EXP is in (or out of, depending
4262 on IN_P) the range. Return 0 if the test couldn't be created. */
4264 tree
4265 build_range_check (location_t loc, tree type, tree exp, int in_p,
4266 tree low, tree high)
4268 tree etype = TREE_TYPE (exp), value;
4270 #ifdef HAVE_canonicalize_funcptr_for_compare
4271 /* Disable this optimization for function pointer expressions
4272 on targets that require function pointer canonicalization. */
4273 if (HAVE_canonicalize_funcptr_for_compare
4274 && TREE_CODE (etype) == POINTER_TYPE
4275 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4276 return NULL_TREE;
4277 #endif
4279 if (! in_p)
4281 value = build_range_check (loc, type, exp, 1, low, high);
4282 if (value != 0)
4283 return invert_truthvalue_loc (loc, value);
4285 return 0;
4288 if (low == 0 && high == 0)
4289 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4291 if (low == 0)
4292 return fold_build2_loc (loc, LE_EXPR, type, exp,
4293 fold_convert_loc (loc, etype, high));
4295 if (high == 0)
4296 return fold_build2_loc (loc, GE_EXPR, type, exp,
4297 fold_convert_loc (loc, etype, low));
4299 if (operand_equal_p (low, high, 0))
4300 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4301 fold_convert_loc (loc, etype, low));
4303 if (integer_zerop (low))
4305 if (! TYPE_UNSIGNED (etype))
4307 etype = unsigned_type_for (etype);
4308 high = fold_convert_loc (loc, etype, high);
4309 exp = fold_convert_loc (loc, etype, exp);
4311 return build_range_check (loc, type, exp, 1, 0, high);
4314 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4315 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4317 unsigned HOST_WIDE_INT lo;
4318 HOST_WIDE_INT hi;
4319 int prec;
4321 prec = TYPE_PRECISION (etype);
4322 if (prec <= HOST_BITS_PER_WIDE_INT)
4324 hi = 0;
4325 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4327 else
4329 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4330 lo = HOST_WIDE_INT_M1U;
4333 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4335 if (TYPE_UNSIGNED (etype))
4337 tree signed_etype = signed_type_for (etype);
4338 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4339 etype
4340 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4341 else
4342 etype = signed_etype;
4343 exp = fold_convert_loc (loc, etype, exp);
4345 return fold_build2_loc (loc, GT_EXPR, type, exp,
4346 build_int_cst (etype, 0));
4350 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4351 This requires wrap-around arithmetics for the type of the expression.
4352 First make sure that arithmetics in this type is valid, then make sure
4353 that it wraps around. */
4354 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4355 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4356 TYPE_UNSIGNED (etype));
4358 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4360 tree utype, minv, maxv;
4362 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4363 for the type in question, as we rely on this here. */
4364 utype = unsigned_type_for (etype);
4365 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4366 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4367 integer_one_node, 1);
4368 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4370 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4371 minv, 1, maxv, 1)))
4372 etype = utype;
4373 else
4374 return 0;
4377 high = fold_convert_loc (loc, etype, high);
4378 low = fold_convert_loc (loc, etype, low);
4379 exp = fold_convert_loc (loc, etype, exp);
4381 value = const_binop (MINUS_EXPR, high, low);
4384 if (POINTER_TYPE_P (etype))
4386 if (value != 0 && !TREE_OVERFLOW (value))
4388 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4389 return build_range_check (loc, type,
4390 fold_build_pointer_plus_loc (loc, exp, low),
4391 1, build_int_cst (etype, 0), value);
4393 return 0;
4396 if (value != 0 && !TREE_OVERFLOW (value))
4397 return build_range_check (loc, type,
4398 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4399 1, build_int_cst (etype, 0), value);
4401 return 0;
4404 /* Return the predecessor of VAL in its type, handling the infinite case. */
4406 static tree
4407 range_predecessor (tree val)
4409 tree type = TREE_TYPE (val);
4411 if (INTEGRAL_TYPE_P (type)
4412 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4413 return 0;
4414 else
4415 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4418 /* Return the successor of VAL in its type, handling the infinite case. */
4420 static tree
4421 range_successor (tree val)
4423 tree type = TREE_TYPE (val);
4425 if (INTEGRAL_TYPE_P (type)
4426 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4427 return 0;
4428 else
4429 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4432 /* Given two ranges, see if we can merge them into one. Return 1 if we
4433 can, 0 if we can't. Set the output range into the specified parameters. */
4435 bool
4436 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4437 tree high0, int in1_p, tree low1, tree high1)
4439 int no_overlap;
4440 int subset;
4441 int temp;
4442 tree tem;
4443 int in_p;
4444 tree low, high;
4445 int lowequal = ((low0 == 0 && low1 == 0)
4446 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4447 low0, 0, low1, 0)));
4448 int highequal = ((high0 == 0 && high1 == 0)
4449 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4450 high0, 1, high1, 1)));
4452 /* Make range 0 be the range that starts first, or ends last if they
4453 start at the same value. Swap them if it isn't. */
4454 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4455 low0, 0, low1, 0))
4456 || (lowequal
4457 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4458 high1, 1, high0, 1))))
4460 temp = in0_p, in0_p = in1_p, in1_p = temp;
4461 tem = low0, low0 = low1, low1 = tem;
4462 tem = high0, high0 = high1, high1 = tem;
4465 /* Now flag two cases, whether the ranges are disjoint or whether the
4466 second range is totally subsumed in the first. Note that the tests
4467 below are simplified by the ones above. */
4468 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4469 high0, 1, low1, 0));
4470 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4471 high1, 1, high0, 1));
4473 /* We now have four cases, depending on whether we are including or
4474 excluding the two ranges. */
4475 if (in0_p && in1_p)
4477 /* If they don't overlap, the result is false. If the second range
4478 is a subset it is the result. Otherwise, the range is from the start
4479 of the second to the end of the first. */
4480 if (no_overlap)
4481 in_p = 0, low = high = 0;
4482 else if (subset)
4483 in_p = 1, low = low1, high = high1;
4484 else
4485 in_p = 1, low = low1, high = high0;
4488 else if (in0_p && ! in1_p)
4490 /* If they don't overlap, the result is the first range. If they are
4491 equal, the result is false. If the second range is a subset of the
4492 first, and the ranges begin at the same place, we go from just after
4493 the end of the second range to the end of the first. If the second
4494 range is not a subset of the first, or if it is a subset and both
4495 ranges end at the same place, the range starts at the start of the
4496 first range and ends just before the second range.
4497 Otherwise, we can't describe this as a single range. */
4498 if (no_overlap)
4499 in_p = 1, low = low0, high = high0;
4500 else if (lowequal && highequal)
4501 in_p = 0, low = high = 0;
4502 else if (subset && lowequal)
4504 low = range_successor (high1);
4505 high = high0;
4506 in_p = 1;
4507 if (low == 0)
4509 /* We are in the weird situation where high0 > high1 but
4510 high1 has no successor. Punt. */
4511 return 0;
4514 else if (! subset || highequal)
4516 low = low0;
4517 high = range_predecessor (low1);
4518 in_p = 1;
4519 if (high == 0)
4521 /* low0 < low1 but low1 has no predecessor. Punt. */
4522 return 0;
4525 else
4526 return 0;
4529 else if (! in0_p && in1_p)
4531 /* If they don't overlap, the result is the second range. If the second
4532 is a subset of the first, the result is false. Otherwise,
4533 the range starts just after the first range and ends at the
4534 end of the second. */
4535 if (no_overlap)
4536 in_p = 1, low = low1, high = high1;
4537 else if (subset || highequal)
4538 in_p = 0, low = high = 0;
4539 else
4541 low = range_successor (high0);
4542 high = high1;
4543 in_p = 1;
4544 if (low == 0)
4546 /* high1 > high0 but high0 has no successor. Punt. */
4547 return 0;
4552 else
4554 /* The case where we are excluding both ranges. Here the complex case
4555 is if they don't overlap. In that case, the only time we have a
4556 range is if they are adjacent. If the second is a subset of the
4557 first, the result is the first. Otherwise, the range to exclude
4558 starts at the beginning of the first range and ends at the end of the
4559 second. */
4560 if (no_overlap)
4562 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4563 range_successor (high0),
4564 1, low1, 0)))
4565 in_p = 0, low = low0, high = high1;
4566 else
4568 /* Canonicalize - [min, x] into - [-, x]. */
4569 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4570 switch (TREE_CODE (TREE_TYPE (low0)))
4572 case ENUMERAL_TYPE:
4573 if (TYPE_PRECISION (TREE_TYPE (low0))
4574 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4575 break;
4576 /* FALLTHROUGH */
4577 case INTEGER_TYPE:
4578 if (tree_int_cst_equal (low0,
4579 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4580 low0 = 0;
4581 break;
4582 case POINTER_TYPE:
4583 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4584 && integer_zerop (low0))
4585 low0 = 0;
4586 break;
4587 default:
4588 break;
4591 /* Canonicalize - [x, max] into - [x, -]. */
4592 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4593 switch (TREE_CODE (TREE_TYPE (high1)))
4595 case ENUMERAL_TYPE:
4596 if (TYPE_PRECISION (TREE_TYPE (high1))
4597 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4598 break;
4599 /* FALLTHROUGH */
4600 case INTEGER_TYPE:
4601 if (tree_int_cst_equal (high1,
4602 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4603 high1 = 0;
4604 break;
4605 case POINTER_TYPE:
4606 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4607 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4608 high1, 1,
4609 integer_one_node, 1)))
4610 high1 = 0;
4611 break;
4612 default:
4613 break;
4616 /* The ranges might be also adjacent between the maximum and
4617 minimum values of the given type. For
4618 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4619 return + [x + 1, y - 1]. */
4620 if (low0 == 0 && high1 == 0)
4622 low = range_successor (high0);
4623 high = range_predecessor (low1);
4624 if (low == 0 || high == 0)
4625 return 0;
4627 in_p = 1;
4629 else
4630 return 0;
4633 else if (subset)
4634 in_p = 0, low = low0, high = high0;
4635 else
4636 in_p = 0, low = low0, high = high1;
4639 *pin_p = in_p, *plow = low, *phigh = high;
4640 return 1;
4644 /* Subroutine of fold, looking inside expressions of the form
4645 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4646 of the COND_EXPR. This function is being used also to optimize
4647 A op B ? C : A, by reversing the comparison first.
4649 Return a folded expression whose code is not a COND_EXPR
4650 anymore, or NULL_TREE if no folding opportunity is found. */
4652 static tree
4653 fold_cond_expr_with_comparison (location_t loc, tree type,
4654 tree arg0, tree arg1, tree arg2)
4656 enum tree_code comp_code = TREE_CODE (arg0);
4657 tree arg00 = TREE_OPERAND (arg0, 0);
4658 tree arg01 = TREE_OPERAND (arg0, 1);
4659 tree arg1_type = TREE_TYPE (arg1);
4660 tree tem;
4662 STRIP_NOPS (arg1);
4663 STRIP_NOPS (arg2);
4665 /* If we have A op 0 ? A : -A, consider applying the following
4666 transformations:
4668 A == 0? A : -A same as -A
4669 A != 0? A : -A same as A
4670 A >= 0? A : -A same as abs (A)
4671 A > 0? A : -A same as abs (A)
4672 A <= 0? A : -A same as -abs (A)
4673 A < 0? A : -A same as -abs (A)
4675 None of these transformations work for modes with signed
4676 zeros. If A is +/-0, the first two transformations will
4677 change the sign of the result (from +0 to -0, or vice
4678 versa). The last four will fix the sign of the result,
4679 even though the original expressions could be positive or
4680 negative, depending on the sign of A.
4682 Note that all these transformations are correct if A is
4683 NaN, since the two alternatives (A and -A) are also NaNs. */
4684 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4685 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4686 ? real_zerop (arg01)
4687 : integer_zerop (arg01))
4688 && ((TREE_CODE (arg2) == NEGATE_EXPR
4689 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4690 /* In the case that A is of the form X-Y, '-A' (arg2) may
4691 have already been folded to Y-X, check for that. */
4692 || (TREE_CODE (arg1) == MINUS_EXPR
4693 && TREE_CODE (arg2) == MINUS_EXPR
4694 && operand_equal_p (TREE_OPERAND (arg1, 0),
4695 TREE_OPERAND (arg2, 1), 0)
4696 && operand_equal_p (TREE_OPERAND (arg1, 1),
4697 TREE_OPERAND (arg2, 0), 0))))
4698 switch (comp_code)
4700 case EQ_EXPR:
4701 case UNEQ_EXPR:
4702 tem = fold_convert_loc (loc, arg1_type, arg1);
4703 return pedantic_non_lvalue_loc (loc,
4704 fold_convert_loc (loc, type,
4705 negate_expr (tem)));
4706 case NE_EXPR:
4707 case LTGT_EXPR:
4708 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4709 case UNGE_EXPR:
4710 case UNGT_EXPR:
4711 if (flag_trapping_math)
4712 break;
4713 /* Fall through. */
4714 case GE_EXPR:
4715 case GT_EXPR:
4716 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4717 arg1 = fold_convert_loc (loc, signed_type_for
4718 (TREE_TYPE (arg1)), arg1);
4719 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4720 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4721 case UNLE_EXPR:
4722 case UNLT_EXPR:
4723 if (flag_trapping_math)
4724 break;
4725 case LE_EXPR:
4726 case LT_EXPR:
4727 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4728 arg1 = fold_convert_loc (loc, signed_type_for
4729 (TREE_TYPE (arg1)), arg1);
4730 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4731 return negate_expr (fold_convert_loc (loc, type, tem));
4732 default:
4733 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4734 break;
4737 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4738 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4739 both transformations are correct when A is NaN: A != 0
4740 is then true, and A == 0 is false. */
4742 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4743 && integer_zerop (arg01) && integer_zerop (arg2))
4745 if (comp_code == NE_EXPR)
4746 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4747 else if (comp_code == EQ_EXPR)
4748 return build_zero_cst (type);
4751 /* Try some transformations of A op B ? A : B.
4753 A == B? A : B same as B
4754 A != B? A : B same as A
4755 A >= B? A : B same as max (A, B)
4756 A > B? A : B same as max (B, A)
4757 A <= B? A : B same as min (A, B)
4758 A < B? A : B same as min (B, A)
4760 As above, these transformations don't work in the presence
4761 of signed zeros. For example, if A and B are zeros of
4762 opposite sign, the first two transformations will change
4763 the sign of the result. In the last four, the original
4764 expressions give different results for (A=+0, B=-0) and
4765 (A=-0, B=+0), but the transformed expressions do not.
4767 The first two transformations are correct if either A or B
4768 is a NaN. In the first transformation, the condition will
4769 be false, and B will indeed be chosen. In the case of the
4770 second transformation, the condition A != B will be true,
4771 and A will be chosen.
4773 The conversions to max() and min() are not correct if B is
4774 a number and A is not. The conditions in the original
4775 expressions will be false, so all four give B. The min()
4776 and max() versions would give a NaN instead. */
4777 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4778 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4779 /* Avoid these transformations if the COND_EXPR may be used
4780 as an lvalue in the C++ front-end. PR c++/19199. */
4781 && (in_gimple_form
4782 || VECTOR_TYPE_P (type)
4783 || (strcmp (lang_hooks.name, "GNU C++") != 0
4784 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4785 || ! maybe_lvalue_p (arg1)
4786 || ! maybe_lvalue_p (arg2)))
4788 tree comp_op0 = arg00;
4789 tree comp_op1 = arg01;
4790 tree comp_type = TREE_TYPE (comp_op0);
4792 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4793 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4795 comp_type = type;
4796 comp_op0 = arg1;
4797 comp_op1 = arg2;
4800 switch (comp_code)
4802 case EQ_EXPR:
4803 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4804 case NE_EXPR:
4805 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4806 case LE_EXPR:
4807 case LT_EXPR:
4808 case UNLE_EXPR:
4809 case UNLT_EXPR:
4810 /* In C++ a ?: expression can be an lvalue, so put the
4811 operand which will be used if they are equal first
4812 so that we can convert this back to the
4813 corresponding COND_EXPR. */
4814 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4816 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4817 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4818 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4819 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4820 : fold_build2_loc (loc, MIN_EXPR, comp_type,
4821 comp_op1, comp_op0);
4822 return pedantic_non_lvalue_loc (loc,
4823 fold_convert_loc (loc, type, tem));
4825 break;
4826 case GE_EXPR:
4827 case GT_EXPR:
4828 case UNGE_EXPR:
4829 case UNGT_EXPR:
4830 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4832 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4833 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4834 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4835 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4836 : fold_build2_loc (loc, MAX_EXPR, comp_type,
4837 comp_op1, comp_op0);
4838 return pedantic_non_lvalue_loc (loc,
4839 fold_convert_loc (loc, type, tem));
4841 break;
4842 case UNEQ_EXPR:
4843 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4844 return pedantic_non_lvalue_loc (loc,
4845 fold_convert_loc (loc, type, arg2));
4846 break;
4847 case LTGT_EXPR:
4848 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4849 return pedantic_non_lvalue_loc (loc,
4850 fold_convert_loc (loc, type, arg1));
4851 break;
4852 default:
4853 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4854 break;
4858 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4859 we might still be able to simplify this. For example,
4860 if C1 is one less or one more than C2, this might have started
4861 out as a MIN or MAX and been transformed by this function.
4862 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
4864 if (INTEGRAL_TYPE_P (type)
4865 && TREE_CODE (arg01) == INTEGER_CST
4866 && TREE_CODE (arg2) == INTEGER_CST)
4867 switch (comp_code)
4869 case EQ_EXPR:
4870 if (TREE_CODE (arg1) == INTEGER_CST)
4871 break;
4872 /* We can replace A with C1 in this case. */
4873 arg1 = fold_convert_loc (loc, type, arg01);
4874 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4876 case LT_EXPR:
4877 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4878 MIN_EXPR, to preserve the signedness of the comparison. */
4879 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4880 OEP_ONLY_CONST)
4881 && operand_equal_p (arg01,
4882 const_binop (PLUS_EXPR, arg2,
4883 build_int_cst (type, 1)),
4884 OEP_ONLY_CONST))
4886 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4887 fold_convert_loc (loc, TREE_TYPE (arg00),
4888 arg2));
4889 return pedantic_non_lvalue_loc (loc,
4890 fold_convert_loc (loc, type, tem));
4892 break;
4894 case LE_EXPR:
4895 /* If C1 is C2 - 1, this is min(A, C2), with the same care
4896 as above. */
4897 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4898 OEP_ONLY_CONST)
4899 && operand_equal_p (arg01,
4900 const_binop (MINUS_EXPR, arg2,
4901 build_int_cst (type, 1)),
4902 OEP_ONLY_CONST))
4904 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4905 fold_convert_loc (loc, TREE_TYPE (arg00),
4906 arg2));
4907 return pedantic_non_lvalue_loc (loc,
4908 fold_convert_loc (loc, type, tem));
4910 break;
4912 case GT_EXPR:
4913 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4914 MAX_EXPR, to preserve the signedness of the comparison. */
4915 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4916 OEP_ONLY_CONST)
4917 && operand_equal_p (arg01,
4918 const_binop (MINUS_EXPR, arg2,
4919 build_int_cst (type, 1)),
4920 OEP_ONLY_CONST))
4922 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4923 fold_convert_loc (loc, TREE_TYPE (arg00),
4924 arg2));
4925 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4927 break;
4929 case GE_EXPR:
4930 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
4931 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4932 OEP_ONLY_CONST)
4933 && operand_equal_p (arg01,
4934 const_binop (PLUS_EXPR, arg2,
4935 build_int_cst (type, 1)),
4936 OEP_ONLY_CONST))
4938 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4939 fold_convert_loc (loc, TREE_TYPE (arg00),
4940 arg2));
4941 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4943 break;
4944 case NE_EXPR:
4945 break;
4946 default:
4947 gcc_unreachable ();
4950 return NULL_TREE;
4955 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4956 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4957 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4958 false) >= 2)
4959 #endif
4961 /* EXP is some logical combination of boolean tests. See if we can
4962 merge it into some range test. Return the new tree if so. */
4964 static tree
4965 fold_range_test (location_t loc, enum tree_code code, tree type,
4966 tree op0, tree op1)
4968 int or_op = (code == TRUTH_ORIF_EXPR
4969 || code == TRUTH_OR_EXPR);
4970 int in0_p, in1_p, in_p;
4971 tree low0, low1, low, high0, high1, high;
4972 bool strict_overflow_p = false;
4973 tree tem, lhs, rhs;
4974 const char * const warnmsg = G_("assuming signed overflow does not occur "
4975 "when simplifying range test");
4977 if (!INTEGRAL_TYPE_P (type))
4978 return 0;
4980 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4981 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4983 /* If this is an OR operation, invert both sides; we will invert
4984 again at the end. */
4985 if (or_op)
4986 in0_p = ! in0_p, in1_p = ! in1_p;
4988 /* If both expressions are the same, if we can merge the ranges, and we
4989 can build the range test, return it or it inverted. If one of the
4990 ranges is always true or always false, consider it to be the same
4991 expression as the other. */
4992 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4993 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4994 in1_p, low1, high1)
4995 && 0 != (tem = (build_range_check (loc, type,
4996 lhs != 0 ? lhs
4997 : rhs != 0 ? rhs : integer_zero_node,
4998 in_p, low, high))))
5000 if (strict_overflow_p)
5001 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5002 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5005 /* On machines where the branch cost is expensive, if this is a
5006 short-circuited branch and the underlying object on both sides
5007 is the same, make a non-short-circuit operation. */
5008 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5009 && lhs != 0 && rhs != 0
5010 && (code == TRUTH_ANDIF_EXPR
5011 || code == TRUTH_ORIF_EXPR)
5012 && operand_equal_p (lhs, rhs, 0))
5014 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5015 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5016 which cases we can't do this. */
5017 if (simple_operand_p (lhs))
5018 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5019 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5020 type, op0, op1);
5022 else if (!lang_hooks.decls.global_bindings_p ()
5023 && !CONTAINS_PLACEHOLDER_P (lhs))
5025 tree common = save_expr (lhs);
5027 if (0 != (lhs = build_range_check (loc, type, common,
5028 or_op ? ! in0_p : in0_p,
5029 low0, high0))
5030 && (0 != (rhs = build_range_check (loc, type, common,
5031 or_op ? ! in1_p : in1_p,
5032 low1, high1))))
5034 if (strict_overflow_p)
5035 fold_overflow_warning (warnmsg,
5036 WARN_STRICT_OVERFLOW_COMPARISON);
5037 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5038 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5039 type, lhs, rhs);
5044 return 0;
5047 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5048 bit value. Arrange things so the extra bits will be set to zero if and
5049 only if C is signed-extended to its full width. If MASK is nonzero,
5050 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5052 static tree
5053 unextend (tree c, int p, int unsignedp, tree mask)
5055 tree type = TREE_TYPE (c);
5056 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5057 tree temp;
5059 if (p == modesize || unsignedp)
5060 return c;
5062 /* We work by getting just the sign bit into the low-order bit, then
5063 into the high-order bit, then sign-extend. We then XOR that value
5064 with C. */
5065 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1));
5066 temp = const_binop (BIT_AND_EXPR, temp, size_int (1));
5068 /* We must use a signed type in order to get an arithmetic right shift.
5069 However, we must also avoid introducing accidental overflows, so that
5070 a subsequent call to integer_zerop will work. Hence we must
5071 do the type conversion here. At this point, the constant is either
5072 zero or one, and the conversion to a signed type can never overflow.
5073 We could get an overflow if this conversion is done anywhere else. */
5074 if (TYPE_UNSIGNED (type))
5075 temp = fold_convert (signed_type_for (type), temp);
5077 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5078 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5079 if (mask != 0)
5080 temp = const_binop (BIT_AND_EXPR, temp,
5081 fold_convert (TREE_TYPE (c), mask));
5082 /* If necessary, convert the type back to match the type of C. */
5083 if (TYPE_UNSIGNED (type))
5084 temp = fold_convert (type, temp);
5086 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5089 /* For an expression that has the form
5090 (A && B) || ~B
5092 (A || B) && ~B,
5093 we can drop one of the inner expressions and simplify to
5094 A || ~B
5096 A && ~B
5097 LOC is the location of the resulting expression. OP is the inner
5098 logical operation; the left-hand side in the examples above, while CMPOP
5099 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5100 removing a condition that guards another, as in
5101 (A != NULL && A->...) || A == NULL
5102 which we must not transform. If RHS_ONLY is true, only eliminate the
5103 right-most operand of the inner logical operation. */
5105 static tree
5106 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5107 bool rhs_only)
5109 tree type = TREE_TYPE (cmpop);
5110 enum tree_code code = TREE_CODE (cmpop);
5111 enum tree_code truthop_code = TREE_CODE (op);
5112 tree lhs = TREE_OPERAND (op, 0);
5113 tree rhs = TREE_OPERAND (op, 1);
5114 tree orig_lhs = lhs, orig_rhs = rhs;
5115 enum tree_code rhs_code = TREE_CODE (rhs);
5116 enum tree_code lhs_code = TREE_CODE (lhs);
5117 enum tree_code inv_code;
5119 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5120 return NULL_TREE;
5122 if (TREE_CODE_CLASS (code) != tcc_comparison)
5123 return NULL_TREE;
5125 if (rhs_code == truthop_code)
5127 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5128 if (newrhs != NULL_TREE)
5130 rhs = newrhs;
5131 rhs_code = TREE_CODE (rhs);
5134 if (lhs_code == truthop_code && !rhs_only)
5136 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5137 if (newlhs != NULL_TREE)
5139 lhs = newlhs;
5140 lhs_code = TREE_CODE (lhs);
5144 inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
5145 if (inv_code == rhs_code
5146 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5147 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5148 return lhs;
5149 if (!rhs_only && inv_code == lhs_code
5150 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5151 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5152 return rhs;
5153 if (rhs != orig_rhs || lhs != orig_lhs)
5154 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5155 lhs, rhs);
5156 return NULL_TREE;
5159 /* Find ways of folding logical expressions of LHS and RHS:
5160 Try to merge two comparisons to the same innermost item.
5161 Look for range tests like "ch >= '0' && ch <= '9'".
5162 Look for combinations of simple terms on machines with expensive branches
5163 and evaluate the RHS unconditionally.
5165 For example, if we have p->a == 2 && p->b == 4 and we can make an
5166 object large enough to span both A and B, we can do this with a comparison
5167 against the object ANDed with the a mask.
5169 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5170 operations to do this with one comparison.
5172 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5173 function and the one above.
5175 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5176 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5178 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5179 two operands.
5181 We return the simplified tree or 0 if no optimization is possible. */
5183 static tree
5184 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5185 tree lhs, tree rhs)
5187 /* If this is the "or" of two comparisons, we can do something if
5188 the comparisons are NE_EXPR. If this is the "and", we can do something
5189 if the comparisons are EQ_EXPR. I.e.,
5190 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5192 WANTED_CODE is this operation code. For single bit fields, we can
5193 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5194 comparison for one-bit fields. */
5196 enum tree_code wanted_code;
5197 enum tree_code lcode, rcode;
5198 tree ll_arg, lr_arg, rl_arg, rr_arg;
5199 tree ll_inner, lr_inner, rl_inner, rr_inner;
5200 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5201 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5202 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5203 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5204 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5205 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5206 enum machine_mode lnmode, rnmode;
5207 tree ll_mask, lr_mask, rl_mask, rr_mask;
5208 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5209 tree l_const, r_const;
5210 tree lntype, rntype, result;
5211 HOST_WIDE_INT first_bit, end_bit;
5212 int volatilep;
5214 /* Start by getting the comparison codes. Fail if anything is volatile.
5215 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5216 it were surrounded with a NE_EXPR. */
5218 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5219 return 0;
5221 lcode = TREE_CODE (lhs);
5222 rcode = TREE_CODE (rhs);
5224 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5226 lhs = build2 (NE_EXPR, truth_type, lhs,
5227 build_int_cst (TREE_TYPE (lhs), 0));
5228 lcode = NE_EXPR;
5231 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5233 rhs = build2 (NE_EXPR, truth_type, rhs,
5234 build_int_cst (TREE_TYPE (rhs), 0));
5235 rcode = NE_EXPR;
5238 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5239 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5240 return 0;
5242 ll_arg = TREE_OPERAND (lhs, 0);
5243 lr_arg = TREE_OPERAND (lhs, 1);
5244 rl_arg = TREE_OPERAND (rhs, 0);
5245 rr_arg = TREE_OPERAND (rhs, 1);
5247 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5248 if (simple_operand_p (ll_arg)
5249 && simple_operand_p (lr_arg))
5251 if (operand_equal_p (ll_arg, rl_arg, 0)
5252 && operand_equal_p (lr_arg, rr_arg, 0))
5254 result = combine_comparisons (loc, code, lcode, rcode,
5255 truth_type, ll_arg, lr_arg);
5256 if (result)
5257 return result;
5259 else if (operand_equal_p (ll_arg, rr_arg, 0)
5260 && operand_equal_p (lr_arg, rl_arg, 0))
5262 result = combine_comparisons (loc, code, lcode,
5263 swap_tree_comparison (rcode),
5264 truth_type, ll_arg, lr_arg);
5265 if (result)
5266 return result;
5270 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5271 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5273 /* If the RHS can be evaluated unconditionally and its operands are
5274 simple, it wins to evaluate the RHS unconditionally on machines
5275 with expensive branches. In this case, this isn't a comparison
5276 that can be merged. */
5278 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5279 false) >= 2
5280 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5281 && simple_operand_p (rl_arg)
5282 && simple_operand_p (rr_arg))
5284 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5285 if (code == TRUTH_OR_EXPR
5286 && lcode == NE_EXPR && integer_zerop (lr_arg)
5287 && rcode == NE_EXPR && integer_zerop (rr_arg)
5288 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5289 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5290 return build2_loc (loc, NE_EXPR, truth_type,
5291 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5292 ll_arg, rl_arg),
5293 build_int_cst (TREE_TYPE (ll_arg), 0));
5295 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5296 if (code == TRUTH_AND_EXPR
5297 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5298 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5299 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5300 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5301 return build2_loc (loc, EQ_EXPR, truth_type,
5302 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5303 ll_arg, rl_arg),
5304 build_int_cst (TREE_TYPE (ll_arg), 0));
5307 /* See if the comparisons can be merged. Then get all the parameters for
5308 each side. */
5310 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5311 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5312 return 0;
5314 volatilep = 0;
5315 ll_inner = decode_field_reference (loc, ll_arg,
5316 &ll_bitsize, &ll_bitpos, &ll_mode,
5317 &ll_unsignedp, &volatilep, &ll_mask,
5318 &ll_and_mask);
5319 lr_inner = decode_field_reference (loc, lr_arg,
5320 &lr_bitsize, &lr_bitpos, &lr_mode,
5321 &lr_unsignedp, &volatilep, &lr_mask,
5322 &lr_and_mask);
5323 rl_inner = decode_field_reference (loc, rl_arg,
5324 &rl_bitsize, &rl_bitpos, &rl_mode,
5325 &rl_unsignedp, &volatilep, &rl_mask,
5326 &rl_and_mask);
5327 rr_inner = decode_field_reference (loc, rr_arg,
5328 &rr_bitsize, &rr_bitpos, &rr_mode,
5329 &rr_unsignedp, &volatilep, &rr_mask,
5330 &rr_and_mask);
5332 /* It must be true that the inner operation on the lhs of each
5333 comparison must be the same if we are to be able to do anything.
5334 Then see if we have constants. If not, the same must be true for
5335 the rhs's. */
5336 if (volatilep || ll_inner == 0 || rl_inner == 0
5337 || ! operand_equal_p (ll_inner, rl_inner, 0))
5338 return 0;
5340 if (TREE_CODE (lr_arg) == INTEGER_CST
5341 && TREE_CODE (rr_arg) == INTEGER_CST)
5342 l_const = lr_arg, r_const = rr_arg;
5343 else if (lr_inner == 0 || rr_inner == 0
5344 || ! operand_equal_p (lr_inner, rr_inner, 0))
5345 return 0;
5346 else
5347 l_const = r_const = 0;
5349 /* If either comparison code is not correct for our logical operation,
5350 fail. However, we can convert a one-bit comparison against zero into
5351 the opposite comparison against that bit being set in the field. */
5353 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5354 if (lcode != wanted_code)
5356 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5358 /* Make the left operand unsigned, since we are only interested
5359 in the value of one bit. Otherwise we are doing the wrong
5360 thing below. */
5361 ll_unsignedp = 1;
5362 l_const = ll_mask;
5364 else
5365 return 0;
5368 /* This is analogous to the code for l_const above. */
5369 if (rcode != wanted_code)
5371 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5373 rl_unsignedp = 1;
5374 r_const = rl_mask;
5376 else
5377 return 0;
5380 /* See if we can find a mode that contains both fields being compared on
5381 the left. If we can't, fail. Otherwise, update all constants and masks
5382 to be relative to a field of that size. */
5383 first_bit = MIN (ll_bitpos, rl_bitpos);
5384 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5385 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5386 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5387 volatilep);
5388 if (lnmode == VOIDmode)
5389 return 0;
5391 lnbitsize = GET_MODE_BITSIZE (lnmode);
5392 lnbitpos = first_bit & ~ (lnbitsize - 1);
5393 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5394 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5396 if (BYTES_BIG_ENDIAN)
5398 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5399 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5402 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5403 size_int (xll_bitpos));
5404 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5405 size_int (xrl_bitpos));
5407 if (l_const)
5409 l_const = fold_convert_loc (loc, lntype, l_const);
5410 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5411 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5412 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5413 fold_build1_loc (loc, BIT_NOT_EXPR,
5414 lntype, ll_mask))))
5416 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5418 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5421 if (r_const)
5423 r_const = fold_convert_loc (loc, lntype, r_const);
5424 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5425 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5426 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5427 fold_build1_loc (loc, BIT_NOT_EXPR,
5428 lntype, rl_mask))))
5430 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5432 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5436 /* If the right sides are not constant, do the same for it. Also,
5437 disallow this optimization if a size or signedness mismatch occurs
5438 between the left and right sides. */
5439 if (l_const == 0)
5441 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5442 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5443 /* Make sure the two fields on the right
5444 correspond to the left without being swapped. */
5445 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5446 return 0;
5448 first_bit = MIN (lr_bitpos, rr_bitpos);
5449 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5450 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5451 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5452 volatilep);
5453 if (rnmode == VOIDmode)
5454 return 0;
5456 rnbitsize = GET_MODE_BITSIZE (rnmode);
5457 rnbitpos = first_bit & ~ (rnbitsize - 1);
5458 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5459 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5461 if (BYTES_BIG_ENDIAN)
5463 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5464 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5467 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5468 rntype, lr_mask),
5469 size_int (xlr_bitpos));
5470 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5471 rntype, rr_mask),
5472 size_int (xrr_bitpos));
5474 /* Make a mask that corresponds to both fields being compared.
5475 Do this for both items being compared. If the operands are the
5476 same size and the bits being compared are in the same position
5477 then we can do this by masking both and comparing the masked
5478 results. */
5479 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5480 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5481 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5483 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5484 ll_unsignedp || rl_unsignedp);
5485 if (! all_ones_mask_p (ll_mask, lnbitsize))
5486 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5488 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5489 lr_unsignedp || rr_unsignedp);
5490 if (! all_ones_mask_p (lr_mask, rnbitsize))
5491 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5493 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5496 /* There is still another way we can do something: If both pairs of
5497 fields being compared are adjacent, we may be able to make a wider
5498 field containing them both.
5500 Note that we still must mask the lhs/rhs expressions. Furthermore,
5501 the mask must be shifted to account for the shift done by
5502 make_bit_field_ref. */
5503 if ((ll_bitsize + ll_bitpos == rl_bitpos
5504 && lr_bitsize + lr_bitpos == rr_bitpos)
5505 || (ll_bitpos == rl_bitpos + rl_bitsize
5506 && lr_bitpos == rr_bitpos + rr_bitsize))
5508 tree type;
5510 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5511 ll_bitsize + rl_bitsize,
5512 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5513 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5514 lr_bitsize + rr_bitsize,
5515 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5517 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5518 size_int (MIN (xll_bitpos, xrl_bitpos)));
5519 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5520 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5522 /* Convert to the smaller type before masking out unwanted bits. */
5523 type = lntype;
5524 if (lntype != rntype)
5526 if (lnbitsize > rnbitsize)
5528 lhs = fold_convert_loc (loc, rntype, lhs);
5529 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5530 type = rntype;
5532 else if (lnbitsize < rnbitsize)
5534 rhs = fold_convert_loc (loc, lntype, rhs);
5535 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5536 type = lntype;
5540 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5541 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5543 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5544 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5546 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5549 return 0;
5552 /* Handle the case of comparisons with constants. If there is something in
5553 common between the masks, those bits of the constants must be the same.
5554 If not, the condition is always false. Test for this to avoid generating
5555 incorrect code below. */
5556 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5557 if (! integer_zerop (result)
5558 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5559 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5561 if (wanted_code == NE_EXPR)
5563 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5564 return constant_boolean_node (true, truth_type);
5566 else
5568 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5569 return constant_boolean_node (false, truth_type);
5573 /* Construct the expression we will return. First get the component
5574 reference we will make. Unless the mask is all ones the width of
5575 that field, perform the mask operation. Then compare with the
5576 merged constant. */
5577 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5578 ll_unsignedp || rl_unsignedp);
5580 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5581 if (! all_ones_mask_p (ll_mask, lnbitsize))
5582 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5584 return build2_loc (loc, wanted_code, truth_type, result,
5585 const_binop (BIT_IOR_EXPR, l_const, r_const));
5588 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5589 constant. */
5591 static tree
5592 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5593 tree op0, tree op1)
5595 tree arg0 = op0;
5596 enum tree_code op_code;
5597 tree comp_const;
5598 tree minmax_const;
5599 int consts_equal, consts_lt;
5600 tree inner;
5602 STRIP_SIGN_NOPS (arg0);
5604 op_code = TREE_CODE (arg0);
5605 minmax_const = TREE_OPERAND (arg0, 1);
5606 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5607 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5608 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5609 inner = TREE_OPERAND (arg0, 0);
5611 /* If something does not permit us to optimize, return the original tree. */
5612 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5613 || TREE_CODE (comp_const) != INTEGER_CST
5614 || TREE_OVERFLOW (comp_const)
5615 || TREE_CODE (minmax_const) != INTEGER_CST
5616 || TREE_OVERFLOW (minmax_const))
5617 return NULL_TREE;
5619 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5620 and GT_EXPR, doing the rest with recursive calls using logical
5621 simplifications. */
5622 switch (code)
5624 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5626 tree tem
5627 = optimize_minmax_comparison (loc,
5628 invert_tree_comparison (code, false),
5629 type, op0, op1);
5630 if (tem)
5631 return invert_truthvalue_loc (loc, tem);
5632 return NULL_TREE;
5635 case GE_EXPR:
5636 return
5637 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5638 optimize_minmax_comparison
5639 (loc, EQ_EXPR, type, arg0, comp_const),
5640 optimize_minmax_comparison
5641 (loc, GT_EXPR, type, arg0, comp_const));
5643 case EQ_EXPR:
5644 if (op_code == MAX_EXPR && consts_equal)
5645 /* MAX (X, 0) == 0 -> X <= 0 */
5646 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5648 else if (op_code == MAX_EXPR && consts_lt)
5649 /* MAX (X, 0) == 5 -> X == 5 */
5650 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5652 else if (op_code == MAX_EXPR)
5653 /* MAX (X, 0) == -1 -> false */
5654 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5656 else if (consts_equal)
5657 /* MIN (X, 0) == 0 -> X >= 0 */
5658 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5660 else if (consts_lt)
5661 /* MIN (X, 0) == 5 -> false */
5662 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5664 else
5665 /* MIN (X, 0) == -1 -> X == -1 */
5666 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5668 case GT_EXPR:
5669 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5670 /* MAX (X, 0) > 0 -> X > 0
5671 MAX (X, 0) > 5 -> X > 5 */
5672 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5674 else if (op_code == MAX_EXPR)
5675 /* MAX (X, 0) > -1 -> true */
5676 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5678 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5679 /* MIN (X, 0) > 0 -> false
5680 MIN (X, 0) > 5 -> false */
5681 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5683 else
5684 /* MIN (X, 0) > -1 -> X > -1 */
5685 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5687 default:
5688 return NULL_TREE;
5692 /* T is an integer expression that is being multiplied, divided, or taken a
5693 modulus (CODE says which and what kind of divide or modulus) by a
5694 constant C. See if we can eliminate that operation by folding it with
5695 other operations already in T. WIDE_TYPE, if non-null, is a type that
5696 should be used for the computation if wider than our type.
5698 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5699 (X * 2) + (Y * 4). We must, however, be assured that either the original
5700 expression would not overflow or that overflow is undefined for the type
5701 in the language in question.
5703 If we return a non-null expression, it is an equivalent form of the
5704 original computation, but need not be in the original type.
5706 We set *STRICT_OVERFLOW_P to true if the return values depends on
5707 signed overflow being undefined. Otherwise we do not change
5708 *STRICT_OVERFLOW_P. */
5710 static tree
5711 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5712 bool *strict_overflow_p)
5714 /* To avoid exponential search depth, refuse to allow recursion past
5715 three levels. Beyond that (1) it's highly unlikely that we'll find
5716 something interesting and (2) we've probably processed it before
5717 when we built the inner expression. */
5719 static int depth;
5720 tree ret;
5722 if (depth > 3)
5723 return NULL;
5725 depth++;
5726 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5727 depth--;
5729 return ret;
5732 static tree
5733 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5734 bool *strict_overflow_p)
5736 tree type = TREE_TYPE (t);
5737 enum tree_code tcode = TREE_CODE (t);
5738 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5739 > GET_MODE_SIZE (TYPE_MODE (type)))
5740 ? wide_type : type);
5741 tree t1, t2;
5742 int same_p = tcode == code;
5743 tree op0 = NULL_TREE, op1 = NULL_TREE;
5744 bool sub_strict_overflow_p;
5746 /* Don't deal with constants of zero here; they confuse the code below. */
5747 if (integer_zerop (c))
5748 return NULL_TREE;
5750 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5751 op0 = TREE_OPERAND (t, 0);
5753 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5754 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5756 /* Note that we need not handle conditional operations here since fold
5757 already handles those cases. So just do arithmetic here. */
5758 switch (tcode)
5760 case INTEGER_CST:
5761 /* For a constant, we can always simplify if we are a multiply
5762 or (for divide and modulus) if it is a multiple of our constant. */
5763 if (code == MULT_EXPR
5764 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c)))
5765 return const_binop (code, fold_convert (ctype, t),
5766 fold_convert (ctype, c));
5767 break;
5769 CASE_CONVERT: case NON_LVALUE_EXPR:
5770 /* If op0 is an expression ... */
5771 if ((COMPARISON_CLASS_P (op0)
5772 || UNARY_CLASS_P (op0)
5773 || BINARY_CLASS_P (op0)
5774 || VL_EXP_CLASS_P (op0)
5775 || EXPRESSION_CLASS_P (op0))
5776 /* ... and has wrapping overflow, and its type is smaller
5777 than ctype, then we cannot pass through as widening. */
5778 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5779 && (TYPE_PRECISION (ctype)
5780 > TYPE_PRECISION (TREE_TYPE (op0))))
5781 /* ... or this is a truncation (t is narrower than op0),
5782 then we cannot pass through this narrowing. */
5783 || (TYPE_PRECISION (type)
5784 < TYPE_PRECISION (TREE_TYPE (op0)))
5785 /* ... or signedness changes for division or modulus,
5786 then we cannot pass through this conversion. */
5787 || (code != MULT_EXPR
5788 && (TYPE_UNSIGNED (ctype)
5789 != TYPE_UNSIGNED (TREE_TYPE (op0))))
5790 /* ... or has undefined overflow while the converted to
5791 type has not, we cannot do the operation in the inner type
5792 as that would introduce undefined overflow. */
5793 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5794 && !TYPE_OVERFLOW_UNDEFINED (type))))
5795 break;
5797 /* Pass the constant down and see if we can make a simplification. If
5798 we can, replace this expression with the inner simplification for
5799 possible later conversion to our or some other type. */
5800 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5801 && TREE_CODE (t2) == INTEGER_CST
5802 && !TREE_OVERFLOW (t2)
5803 && (0 != (t1 = extract_muldiv (op0, t2, code,
5804 code == MULT_EXPR
5805 ? ctype : NULL_TREE,
5806 strict_overflow_p))))
5807 return t1;
5808 break;
5810 case ABS_EXPR:
5811 /* If widening the type changes it from signed to unsigned, then we
5812 must avoid building ABS_EXPR itself as unsigned. */
5813 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5815 tree cstype = (*signed_type_for) (ctype);
5816 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5817 != 0)
5819 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5820 return fold_convert (ctype, t1);
5822 break;
5824 /* If the constant is negative, we cannot simplify this. */
5825 if (tree_int_cst_sgn (c) == -1)
5826 break;
5827 /* FALLTHROUGH */
5828 case NEGATE_EXPR:
5829 /* For division and modulus, type can't be unsigned, as e.g.
5830 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
5831 For signed types, even with wrapping overflow, this is fine. */
5832 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
5833 break;
5834 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5835 != 0)
5836 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5837 break;
5839 case MIN_EXPR: case MAX_EXPR:
5840 /* If widening the type changes the signedness, then we can't perform
5841 this optimization as that changes the result. */
5842 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5843 break;
5845 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5846 sub_strict_overflow_p = false;
5847 if ((t1 = extract_muldiv (op0, c, code, wide_type,
5848 &sub_strict_overflow_p)) != 0
5849 && (t2 = extract_muldiv (op1, c, code, wide_type,
5850 &sub_strict_overflow_p)) != 0)
5852 if (tree_int_cst_sgn (c) < 0)
5853 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5854 if (sub_strict_overflow_p)
5855 *strict_overflow_p = true;
5856 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5857 fold_convert (ctype, t2));
5859 break;
5861 case LSHIFT_EXPR: case RSHIFT_EXPR:
5862 /* If the second operand is constant, this is a multiplication
5863 or floor division, by a power of two, so we can treat it that
5864 way unless the multiplier or divisor overflows. Signed
5865 left-shift overflow is implementation-defined rather than
5866 undefined in C90, so do not convert signed left shift into
5867 multiplication. */
5868 if (TREE_CODE (op1) == INTEGER_CST
5869 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5870 /* const_binop may not detect overflow correctly,
5871 so check for it explicitly here. */
5872 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5873 && TREE_INT_CST_HIGH (op1) == 0
5874 && 0 != (t1 = fold_convert (ctype,
5875 const_binop (LSHIFT_EXPR,
5876 size_one_node,
5877 op1)))
5878 && !TREE_OVERFLOW (t1))
5879 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5880 ? MULT_EXPR : FLOOR_DIV_EXPR,
5881 ctype,
5882 fold_convert (ctype, op0),
5883 t1),
5884 c, code, wide_type, strict_overflow_p);
5885 break;
5887 case PLUS_EXPR: case MINUS_EXPR:
5888 /* See if we can eliminate the operation on both sides. If we can, we
5889 can return a new PLUS or MINUS. If we can't, the only remaining
5890 cases where we can do anything are if the second operand is a
5891 constant. */
5892 sub_strict_overflow_p = false;
5893 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5894 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5895 if (t1 != 0 && t2 != 0
5896 && (code == MULT_EXPR
5897 /* If not multiplication, we can only do this if both operands
5898 are divisible by c. */
5899 || (multiple_of_p (ctype, op0, c)
5900 && multiple_of_p (ctype, op1, c))))
5902 if (sub_strict_overflow_p)
5903 *strict_overflow_p = true;
5904 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5905 fold_convert (ctype, t2));
5908 /* If this was a subtraction, negate OP1 and set it to be an addition.
5909 This simplifies the logic below. */
5910 if (tcode == MINUS_EXPR)
5912 tcode = PLUS_EXPR, op1 = negate_expr (op1);
5913 /* If OP1 was not easily negatable, the constant may be OP0. */
5914 if (TREE_CODE (op0) == INTEGER_CST)
5916 tree tem = op0;
5917 op0 = op1;
5918 op1 = tem;
5919 tem = t1;
5920 t1 = t2;
5921 t2 = tem;
5925 if (TREE_CODE (op1) != INTEGER_CST)
5926 break;
5928 /* If either OP1 or C are negative, this optimization is not safe for
5929 some of the division and remainder types while for others we need
5930 to change the code. */
5931 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5933 if (code == CEIL_DIV_EXPR)
5934 code = FLOOR_DIV_EXPR;
5935 else if (code == FLOOR_DIV_EXPR)
5936 code = CEIL_DIV_EXPR;
5937 else if (code != MULT_EXPR
5938 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5939 break;
5942 /* If it's a multiply or a division/modulus operation of a multiple
5943 of our constant, do the operation and verify it doesn't overflow. */
5944 if (code == MULT_EXPR
5945 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5947 op1 = const_binop (code, fold_convert (ctype, op1),
5948 fold_convert (ctype, c));
5949 /* We allow the constant to overflow with wrapping semantics. */
5950 if (op1 == 0
5951 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5952 break;
5954 else
5955 break;
5957 /* If we have an unsigned type, we cannot widen the operation since it
5958 will change the result if the original computation overflowed. */
5959 if (TYPE_UNSIGNED (ctype) && ctype != type)
5960 break;
5962 /* If we were able to eliminate our operation from the first side,
5963 apply our operation to the second side and reform the PLUS. */
5964 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5965 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5967 /* The last case is if we are a multiply. In that case, we can
5968 apply the distributive law to commute the multiply and addition
5969 if the multiplication of the constants doesn't overflow
5970 and overflow is defined. With undefined overflow
5971 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
5972 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
5973 return fold_build2 (tcode, ctype,
5974 fold_build2 (code, ctype,
5975 fold_convert (ctype, op0),
5976 fold_convert (ctype, c)),
5977 op1);
5979 break;
5981 case MULT_EXPR:
5982 /* We have a special case here if we are doing something like
5983 (C * 8) % 4 since we know that's zero. */
5984 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5985 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5986 /* If the multiplication can overflow we cannot optimize this. */
5987 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5988 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5989 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5991 *strict_overflow_p = true;
5992 return omit_one_operand (type, integer_zero_node, op0);
5995 /* ... fall through ... */
5997 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
5998 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
5999 /* If we can extract our operation from the LHS, do so and return a
6000 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6001 do something only if the second operand is a constant. */
6002 if (same_p
6003 && (t1 = extract_muldiv (op0, c, code, wide_type,
6004 strict_overflow_p)) != 0)
6005 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6006 fold_convert (ctype, op1));
6007 else if (tcode == MULT_EXPR && code == MULT_EXPR
6008 && (t1 = extract_muldiv (op1, c, code, wide_type,
6009 strict_overflow_p)) != 0)
6010 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6011 fold_convert (ctype, t1));
6012 else if (TREE_CODE (op1) != INTEGER_CST)
6013 return 0;
6015 /* If these are the same operation types, we can associate them
6016 assuming no overflow. */
6017 if (tcode == code)
6019 double_int mul;
6020 bool overflow_p;
6021 unsigned prec = TYPE_PRECISION (ctype);
6022 bool uns = TYPE_UNSIGNED (ctype);
6023 double_int diop1 = tree_to_double_int (op1).ext (prec, uns);
6024 double_int dic = tree_to_double_int (c).ext (prec, uns);
6025 mul = diop1.mul_with_sign (dic, false, &overflow_p);
6026 overflow_p = ((!uns && overflow_p)
6027 | TREE_OVERFLOW (c) | TREE_OVERFLOW (op1));
6028 if (!double_int_fits_to_tree_p (ctype, mul)
6029 && ((uns && tcode != MULT_EXPR) || !uns))
6030 overflow_p = 1;
6031 if (!overflow_p)
6032 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6033 double_int_to_tree (ctype, mul));
6036 /* If these operations "cancel" each other, we have the main
6037 optimizations of this pass, which occur when either constant is a
6038 multiple of the other, in which case we replace this with either an
6039 operation or CODE or TCODE.
6041 If we have an unsigned type, we cannot do this since it will change
6042 the result if the original computation overflowed. */
6043 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6044 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6045 || (tcode == MULT_EXPR
6046 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6047 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6048 && code != MULT_EXPR)))
6050 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
6052 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6053 *strict_overflow_p = true;
6054 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6055 fold_convert (ctype,
6056 const_binop (TRUNC_DIV_EXPR,
6057 op1, c)));
6059 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1)))
6061 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6062 *strict_overflow_p = true;
6063 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6064 fold_convert (ctype,
6065 const_binop (TRUNC_DIV_EXPR,
6066 c, op1)));
6069 break;
6071 default:
6072 break;
6075 return 0;
6078 /* Return a node which has the indicated constant VALUE (either 0 or
6079 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6080 and is of the indicated TYPE. */
6082 tree
6083 constant_boolean_node (bool value, tree type)
6085 if (type == integer_type_node)
6086 return value ? integer_one_node : integer_zero_node;
6087 else if (type == boolean_type_node)
6088 return value ? boolean_true_node : boolean_false_node;
6089 else if (TREE_CODE (type) == VECTOR_TYPE)
6090 return build_vector_from_val (type,
6091 build_int_cst (TREE_TYPE (type),
6092 value ? -1 : 0));
6093 else
6094 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6098 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6099 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6100 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6101 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6102 COND is the first argument to CODE; otherwise (as in the example
6103 given here), it is the second argument. TYPE is the type of the
6104 original expression. Return NULL_TREE if no simplification is
6105 possible. */
6107 static tree
6108 fold_binary_op_with_conditional_arg (location_t loc,
6109 enum tree_code code,
6110 tree type, tree op0, tree op1,
6111 tree cond, tree arg, int cond_first_p)
6113 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6114 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6115 tree test, true_value, false_value;
6116 tree lhs = NULL_TREE;
6117 tree rhs = NULL_TREE;
6118 enum tree_code cond_code = COND_EXPR;
6120 if (TREE_CODE (cond) == COND_EXPR
6121 || TREE_CODE (cond) == VEC_COND_EXPR)
6123 test = TREE_OPERAND (cond, 0);
6124 true_value = TREE_OPERAND (cond, 1);
6125 false_value = TREE_OPERAND (cond, 2);
6126 /* If this operand throws an expression, then it does not make
6127 sense to try to perform a logical or arithmetic operation
6128 involving it. */
6129 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6130 lhs = true_value;
6131 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6132 rhs = false_value;
6134 else
6136 tree testtype = TREE_TYPE (cond);
6137 test = cond;
6138 true_value = constant_boolean_node (true, testtype);
6139 false_value = constant_boolean_node (false, testtype);
6142 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6143 cond_code = VEC_COND_EXPR;
6145 /* This transformation is only worthwhile if we don't have to wrap ARG
6146 in a SAVE_EXPR and the operation can be simplified without recursing
6147 on at least one of the branches once its pushed inside the COND_EXPR. */
6148 if (!TREE_CONSTANT (arg)
6149 && (TREE_SIDE_EFFECTS (arg)
6150 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6151 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6152 return NULL_TREE;
6154 arg = fold_convert_loc (loc, arg_type, arg);
6155 if (lhs == 0)
6157 true_value = fold_convert_loc (loc, cond_type, true_value);
6158 if (cond_first_p)
6159 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6160 else
6161 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6163 if (rhs == 0)
6165 false_value = fold_convert_loc (loc, cond_type, false_value);
6166 if (cond_first_p)
6167 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6168 else
6169 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6172 /* Check that we have simplified at least one of the branches. */
6173 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6174 return NULL_TREE;
6176 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6180 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6182 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6183 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6184 ADDEND is the same as X.
6186 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6187 and finite. The problematic cases are when X is zero, and its mode
6188 has signed zeros. In the case of rounding towards -infinity,
6189 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6190 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6192 bool
6193 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6195 if (!real_zerop (addend))
6196 return false;
6198 /* Don't allow the fold with -fsignaling-nans. */
6199 if (HONOR_SNANS (TYPE_MODE (type)))
6200 return false;
6202 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6203 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6204 return true;
6206 /* In a vector or complex, we would need to check the sign of all zeros. */
6207 if (TREE_CODE (addend) != REAL_CST)
6208 return false;
6210 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6211 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6212 negate = !negate;
6214 /* The mode has signed zeros, and we have to honor their sign.
6215 In this situation, there is only one case we can return true for.
6216 X - 0 is the same as X unless rounding towards -infinity is
6217 supported. */
6218 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6221 /* Subroutine of fold() that checks comparisons of built-in math
6222 functions against real constants.
6224 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6225 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6226 is the type of the result and ARG0 and ARG1 are the operands of the
6227 comparison. ARG1 must be a TREE_REAL_CST.
6229 The function returns the constant folded tree if a simplification
6230 can be made, and NULL_TREE otherwise. */
6232 static tree
6233 fold_mathfn_compare (location_t loc,
6234 enum built_in_function fcode, enum tree_code code,
6235 tree type, tree arg0, tree arg1)
6237 REAL_VALUE_TYPE c;
6239 if (BUILTIN_SQRT_P (fcode))
6241 tree arg = CALL_EXPR_ARG (arg0, 0);
6242 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6244 c = TREE_REAL_CST (arg1);
6245 if (REAL_VALUE_NEGATIVE (c))
6247 /* sqrt(x) < y is always false, if y is negative. */
6248 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6249 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6251 /* sqrt(x) > y is always true, if y is negative and we
6252 don't care about NaNs, i.e. negative values of x. */
6253 if (code == NE_EXPR || !HONOR_NANS (mode))
6254 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6256 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6257 return fold_build2_loc (loc, GE_EXPR, type, arg,
6258 build_real (TREE_TYPE (arg), dconst0));
6260 else if (code == GT_EXPR || code == GE_EXPR)
6262 REAL_VALUE_TYPE c2;
6264 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6265 real_convert (&c2, mode, &c2);
6267 if (REAL_VALUE_ISINF (c2))
6269 /* sqrt(x) > y is x == +Inf, when y is very large. */
6270 if (HONOR_INFINITIES (mode))
6271 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6272 build_real (TREE_TYPE (arg), c2));
6274 /* sqrt(x) > y is always false, when y is very large
6275 and we don't care about infinities. */
6276 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6279 /* sqrt(x) > c is the same as x > c*c. */
6280 return fold_build2_loc (loc, code, type, arg,
6281 build_real (TREE_TYPE (arg), c2));
6283 else if (code == LT_EXPR || code == LE_EXPR)
6285 REAL_VALUE_TYPE c2;
6287 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6288 real_convert (&c2, mode, &c2);
6290 if (REAL_VALUE_ISINF (c2))
6292 /* sqrt(x) < y is always true, when y is a very large
6293 value and we don't care about NaNs or Infinities. */
6294 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6295 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6297 /* sqrt(x) < y is x != +Inf when y is very large and we
6298 don't care about NaNs. */
6299 if (! HONOR_NANS (mode))
6300 return fold_build2_loc (loc, NE_EXPR, type, arg,
6301 build_real (TREE_TYPE (arg), c2));
6303 /* sqrt(x) < y is x >= 0 when y is very large and we
6304 don't care about Infinities. */
6305 if (! HONOR_INFINITIES (mode))
6306 return fold_build2_loc (loc, GE_EXPR, type, arg,
6307 build_real (TREE_TYPE (arg), dconst0));
6309 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6310 arg = save_expr (arg);
6311 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6312 fold_build2_loc (loc, GE_EXPR, type, arg,
6313 build_real (TREE_TYPE (arg),
6314 dconst0)),
6315 fold_build2_loc (loc, NE_EXPR, type, arg,
6316 build_real (TREE_TYPE (arg),
6317 c2)));
6320 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6321 if (! HONOR_NANS (mode))
6322 return fold_build2_loc (loc, code, type, arg,
6323 build_real (TREE_TYPE (arg), c2));
6325 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6326 arg = save_expr (arg);
6327 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6328 fold_build2_loc (loc, GE_EXPR, type, arg,
6329 build_real (TREE_TYPE (arg),
6330 dconst0)),
6331 fold_build2_loc (loc, code, type, arg,
6332 build_real (TREE_TYPE (arg),
6333 c2)));
6337 return NULL_TREE;
6340 /* Subroutine of fold() that optimizes comparisons against Infinities,
6341 either +Inf or -Inf.
6343 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6344 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6345 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6347 The function returns the constant folded tree if a simplification
6348 can be made, and NULL_TREE otherwise. */
6350 static tree
6351 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6352 tree arg0, tree arg1)
6354 enum machine_mode mode;
6355 REAL_VALUE_TYPE max;
6356 tree temp;
6357 bool neg;
6359 mode = TYPE_MODE (TREE_TYPE (arg0));
6361 /* For negative infinity swap the sense of the comparison. */
6362 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6363 if (neg)
6364 code = swap_tree_comparison (code);
6366 switch (code)
6368 case GT_EXPR:
6369 /* x > +Inf is always false, if with ignore sNANs. */
6370 if (HONOR_SNANS (mode))
6371 return NULL_TREE;
6372 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6374 case LE_EXPR:
6375 /* x <= +Inf is always true, if we don't case about NaNs. */
6376 if (! HONOR_NANS (mode))
6377 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6379 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6380 arg0 = save_expr (arg0);
6381 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6383 case EQ_EXPR:
6384 case GE_EXPR:
6385 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6386 real_maxval (&max, neg, mode);
6387 return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6388 arg0, build_real (TREE_TYPE (arg0), max));
6390 case LT_EXPR:
6391 /* x < +Inf is always equal to x <= DBL_MAX. */
6392 real_maxval (&max, neg, mode);
6393 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6394 arg0, build_real (TREE_TYPE (arg0), max));
6396 case NE_EXPR:
6397 /* x != +Inf is always equal to !(x > DBL_MAX). */
6398 real_maxval (&max, neg, mode);
6399 if (! HONOR_NANS (mode))
6400 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6401 arg0, build_real (TREE_TYPE (arg0), max));
6403 temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6404 arg0, build_real (TREE_TYPE (arg0), max));
6405 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6407 default:
6408 break;
6411 return NULL_TREE;
6414 /* Subroutine of fold() that optimizes comparisons of a division by
6415 a nonzero integer constant against an integer constant, i.e.
6416 X/C1 op C2.
6418 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6419 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6420 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6422 The function returns the constant folded tree if a simplification
6423 can be made, and NULL_TREE otherwise. */
6425 static tree
6426 fold_div_compare (location_t loc,
6427 enum tree_code code, tree type, tree arg0, tree arg1)
6429 tree prod, tmp, hi, lo;
6430 tree arg00 = TREE_OPERAND (arg0, 0);
6431 tree arg01 = TREE_OPERAND (arg0, 1);
6432 double_int val;
6433 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6434 bool neg_overflow;
6435 bool overflow;
6437 /* We have to do this the hard way to detect unsigned overflow.
6438 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6439 val = TREE_INT_CST (arg01)
6440 .mul_with_sign (TREE_INT_CST (arg1), unsigned_p, &overflow);
6441 prod = force_fit_type_double (TREE_TYPE (arg00), val, -1, overflow);
6442 neg_overflow = false;
6444 if (unsigned_p)
6446 tmp = int_const_binop (MINUS_EXPR, arg01,
6447 build_int_cst (TREE_TYPE (arg01), 1));
6448 lo = prod;
6450 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6451 val = TREE_INT_CST (prod)
6452 .add_with_sign (TREE_INT_CST (tmp), unsigned_p, &overflow);
6453 hi = force_fit_type_double (TREE_TYPE (arg00), val,
6454 -1, overflow | TREE_OVERFLOW (prod));
6456 else if (tree_int_cst_sgn (arg01) >= 0)
6458 tmp = int_const_binop (MINUS_EXPR, arg01,
6459 build_int_cst (TREE_TYPE (arg01), 1));
6460 switch (tree_int_cst_sgn (arg1))
6462 case -1:
6463 neg_overflow = true;
6464 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6465 hi = prod;
6466 break;
6468 case 0:
6469 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6470 hi = tmp;
6471 break;
6473 case 1:
6474 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6475 lo = prod;
6476 break;
6478 default:
6479 gcc_unreachable ();
6482 else
6484 /* A negative divisor reverses the relational operators. */
6485 code = swap_tree_comparison (code);
6487 tmp = int_const_binop (PLUS_EXPR, arg01,
6488 build_int_cst (TREE_TYPE (arg01), 1));
6489 switch (tree_int_cst_sgn (arg1))
6491 case -1:
6492 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6493 lo = prod;
6494 break;
6496 case 0:
6497 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6498 lo = tmp;
6499 break;
6501 case 1:
6502 neg_overflow = true;
6503 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6504 hi = prod;
6505 break;
6507 default:
6508 gcc_unreachable ();
6512 switch (code)
6514 case EQ_EXPR:
6515 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6516 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6517 if (TREE_OVERFLOW (hi))
6518 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6519 if (TREE_OVERFLOW (lo))
6520 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6521 return build_range_check (loc, type, arg00, 1, lo, hi);
6523 case NE_EXPR:
6524 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6525 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6526 if (TREE_OVERFLOW (hi))
6527 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6528 if (TREE_OVERFLOW (lo))
6529 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6530 return build_range_check (loc, type, arg00, 0, lo, hi);
6532 case LT_EXPR:
6533 if (TREE_OVERFLOW (lo))
6535 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6536 return omit_one_operand_loc (loc, type, tmp, arg00);
6538 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6540 case LE_EXPR:
6541 if (TREE_OVERFLOW (hi))
6543 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6544 return omit_one_operand_loc (loc, type, tmp, arg00);
6546 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6548 case GT_EXPR:
6549 if (TREE_OVERFLOW (hi))
6551 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6552 return omit_one_operand_loc (loc, type, tmp, arg00);
6554 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6556 case GE_EXPR:
6557 if (TREE_OVERFLOW (lo))
6559 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6560 return omit_one_operand_loc (loc, type, tmp, arg00);
6562 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6564 default:
6565 break;
6568 return NULL_TREE;
6572 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6573 equality/inequality test, then return a simplified form of the test
6574 using a sign testing. Otherwise return NULL. TYPE is the desired
6575 result type. */
6577 static tree
6578 fold_single_bit_test_into_sign_test (location_t loc,
6579 enum tree_code code, tree arg0, tree arg1,
6580 tree result_type)
6582 /* If this is testing a single bit, we can optimize the test. */
6583 if ((code == NE_EXPR || code == EQ_EXPR)
6584 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6585 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6587 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6588 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6589 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6591 if (arg00 != NULL_TREE
6592 /* This is only a win if casting to a signed type is cheap,
6593 i.e. when arg00's type is not a partial mode. */
6594 && TYPE_PRECISION (TREE_TYPE (arg00))
6595 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6597 tree stype = signed_type_for (TREE_TYPE (arg00));
6598 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6599 result_type,
6600 fold_convert_loc (loc, stype, arg00),
6601 build_int_cst (stype, 0));
6605 return NULL_TREE;
6608 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6609 equality/inequality test, then return a simplified form of
6610 the test using shifts and logical operations. Otherwise return
6611 NULL. TYPE is the desired result type. */
6613 tree
6614 fold_single_bit_test (location_t loc, enum tree_code code,
6615 tree arg0, tree arg1, tree result_type)
6617 /* If this is testing a single bit, we can optimize the test. */
6618 if ((code == NE_EXPR || code == EQ_EXPR)
6619 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6620 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6622 tree inner = TREE_OPERAND (arg0, 0);
6623 tree type = TREE_TYPE (arg0);
6624 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6625 enum machine_mode operand_mode = TYPE_MODE (type);
6626 int ops_unsigned;
6627 tree signed_type, unsigned_type, intermediate_type;
6628 tree tem, one;
6630 /* First, see if we can fold the single bit test into a sign-bit
6631 test. */
6632 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6633 result_type);
6634 if (tem)
6635 return tem;
6637 /* Otherwise we have (A & C) != 0 where C is a single bit,
6638 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6639 Similarly for (A & C) == 0. */
6641 /* If INNER is a right shift of a constant and it plus BITNUM does
6642 not overflow, adjust BITNUM and INNER. */
6643 if (TREE_CODE (inner) == RSHIFT_EXPR
6644 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6645 && host_integerp (TREE_OPERAND (inner, 1), 1)
6646 && bitnum < TYPE_PRECISION (type)
6647 && (TREE_INT_CST_LOW (TREE_OPERAND (inner, 1))
6648 < (unsigned) (TYPE_PRECISION (type) - bitnum)))
6650 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6651 inner = TREE_OPERAND (inner, 0);
6654 /* If we are going to be able to omit the AND below, we must do our
6655 operations as unsigned. If we must use the AND, we have a choice.
6656 Normally unsigned is faster, but for some machines signed is. */
6657 #ifdef LOAD_EXTEND_OP
6658 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6659 && !flag_syntax_only) ? 0 : 1;
6660 #else
6661 ops_unsigned = 1;
6662 #endif
6664 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6665 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6666 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6667 inner = fold_convert_loc (loc, intermediate_type, inner);
6669 if (bitnum != 0)
6670 inner = build2 (RSHIFT_EXPR, intermediate_type,
6671 inner, size_int (bitnum));
6673 one = build_int_cst (intermediate_type, 1);
6675 if (code == EQ_EXPR)
6676 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6678 /* Put the AND last so it can combine with more things. */
6679 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6681 /* Make sure to return the proper type. */
6682 inner = fold_convert_loc (loc, result_type, inner);
6684 return inner;
6686 return NULL_TREE;
6689 /* Check whether we are allowed to reorder operands arg0 and arg1,
6690 such that the evaluation of arg1 occurs before arg0. */
6692 static bool
6693 reorder_operands_p (const_tree arg0, const_tree arg1)
6695 if (! flag_evaluation_order)
6696 return true;
6697 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6698 return true;
6699 return ! TREE_SIDE_EFFECTS (arg0)
6700 && ! TREE_SIDE_EFFECTS (arg1);
6703 /* Test whether it is preferable two swap two operands, ARG0 and
6704 ARG1, for example because ARG0 is an integer constant and ARG1
6705 isn't. If REORDER is true, only recommend swapping if we can
6706 evaluate the operands in reverse order. */
6708 bool
6709 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6711 STRIP_SIGN_NOPS (arg0);
6712 STRIP_SIGN_NOPS (arg1);
6714 if (TREE_CODE (arg1) == INTEGER_CST)
6715 return 0;
6716 if (TREE_CODE (arg0) == INTEGER_CST)
6717 return 1;
6719 if (TREE_CODE (arg1) == REAL_CST)
6720 return 0;
6721 if (TREE_CODE (arg0) == REAL_CST)
6722 return 1;
6724 if (TREE_CODE (arg1) == FIXED_CST)
6725 return 0;
6726 if (TREE_CODE (arg0) == FIXED_CST)
6727 return 1;
6729 if (TREE_CODE (arg1) == COMPLEX_CST)
6730 return 0;
6731 if (TREE_CODE (arg0) == COMPLEX_CST)
6732 return 1;
6734 if (TREE_CONSTANT (arg1))
6735 return 0;
6736 if (TREE_CONSTANT (arg0))
6737 return 1;
6739 if (optimize_function_for_size_p (cfun))
6740 return 0;
6742 if (reorder && flag_evaluation_order
6743 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6744 return 0;
6746 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6747 for commutative and comparison operators. Ensuring a canonical
6748 form allows the optimizers to find additional redundancies without
6749 having to explicitly check for both orderings. */
6750 if (TREE_CODE (arg0) == SSA_NAME
6751 && TREE_CODE (arg1) == SSA_NAME
6752 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6753 return 1;
6755 /* Put SSA_NAMEs last. */
6756 if (TREE_CODE (arg1) == SSA_NAME)
6757 return 0;
6758 if (TREE_CODE (arg0) == SSA_NAME)
6759 return 1;
6761 /* Put variables last. */
6762 if (DECL_P (arg1))
6763 return 0;
6764 if (DECL_P (arg0))
6765 return 1;
6767 return 0;
6770 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6771 ARG0 is extended to a wider type. */
6773 static tree
6774 fold_widened_comparison (location_t loc, enum tree_code code,
6775 tree type, tree arg0, tree arg1)
6777 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6778 tree arg1_unw;
6779 tree shorter_type, outer_type;
6780 tree min, max;
6781 bool above, below;
6783 if (arg0_unw == arg0)
6784 return NULL_TREE;
6785 shorter_type = TREE_TYPE (arg0_unw);
6787 #ifdef HAVE_canonicalize_funcptr_for_compare
6788 /* Disable this optimization if we're casting a function pointer
6789 type on targets that require function pointer canonicalization. */
6790 if (HAVE_canonicalize_funcptr_for_compare
6791 && TREE_CODE (shorter_type) == POINTER_TYPE
6792 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6793 return NULL_TREE;
6794 #endif
6796 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6797 return NULL_TREE;
6799 arg1_unw = get_unwidened (arg1, NULL_TREE);
6801 /* If possible, express the comparison in the shorter mode. */
6802 if ((code == EQ_EXPR || code == NE_EXPR
6803 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6804 && (TREE_TYPE (arg1_unw) == shorter_type
6805 || ((TYPE_PRECISION (shorter_type)
6806 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6807 && (TYPE_UNSIGNED (shorter_type)
6808 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6809 || (TREE_CODE (arg1_unw) == INTEGER_CST
6810 && (TREE_CODE (shorter_type) == INTEGER_TYPE
6811 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6812 && int_fits_type_p (arg1_unw, shorter_type))))
6813 return fold_build2_loc (loc, code, type, arg0_unw,
6814 fold_convert_loc (loc, shorter_type, arg1_unw));
6816 if (TREE_CODE (arg1_unw) != INTEGER_CST
6817 || TREE_CODE (shorter_type) != INTEGER_TYPE
6818 || !int_fits_type_p (arg1_unw, shorter_type))
6819 return NULL_TREE;
6821 /* If we are comparing with the integer that does not fit into the range
6822 of the shorter type, the result is known. */
6823 outer_type = TREE_TYPE (arg1_unw);
6824 min = lower_bound_in_type (outer_type, shorter_type);
6825 max = upper_bound_in_type (outer_type, shorter_type);
6827 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6828 max, arg1_unw));
6829 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6830 arg1_unw, min));
6832 switch (code)
6834 case EQ_EXPR:
6835 if (above || below)
6836 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6837 break;
6839 case NE_EXPR:
6840 if (above || below)
6841 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6842 break;
6844 case LT_EXPR:
6845 case LE_EXPR:
6846 if (above)
6847 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6848 else if (below)
6849 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6851 case GT_EXPR:
6852 case GE_EXPR:
6853 if (above)
6854 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6855 else if (below)
6856 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6858 default:
6859 break;
6862 return NULL_TREE;
6865 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6866 ARG0 just the signedness is changed. */
6868 static tree
6869 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6870 tree arg0, tree arg1)
6872 tree arg0_inner;
6873 tree inner_type, outer_type;
6875 if (!CONVERT_EXPR_P (arg0))
6876 return NULL_TREE;
6878 outer_type = TREE_TYPE (arg0);
6879 arg0_inner = TREE_OPERAND (arg0, 0);
6880 inner_type = TREE_TYPE (arg0_inner);
6882 #ifdef HAVE_canonicalize_funcptr_for_compare
6883 /* Disable this optimization if we're casting a function pointer
6884 type on targets that require function pointer canonicalization. */
6885 if (HAVE_canonicalize_funcptr_for_compare
6886 && TREE_CODE (inner_type) == POINTER_TYPE
6887 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6888 return NULL_TREE;
6889 #endif
6891 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6892 return NULL_TREE;
6894 if (TREE_CODE (arg1) != INTEGER_CST
6895 && !(CONVERT_EXPR_P (arg1)
6896 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6897 return NULL_TREE;
6899 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6900 && code != NE_EXPR
6901 && code != EQ_EXPR)
6902 return NULL_TREE;
6904 if (POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6905 return NULL_TREE;
6907 if (TREE_CODE (arg1) == INTEGER_CST)
6908 arg1 = force_fit_type_double (inner_type, tree_to_double_int (arg1),
6909 0, TREE_OVERFLOW (arg1));
6910 else
6911 arg1 = fold_convert_loc (loc, inner_type, arg1);
6913 return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6916 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6917 step of the array. Reconstructs s and delta in the case of s *
6918 delta being an integer constant (and thus already folded). ADDR is
6919 the address. MULT is the multiplicative expression. If the
6920 function succeeds, the new address expression is returned.
6921 Otherwise NULL_TREE is returned. LOC is the location of the
6922 resulting expression. */
6924 static tree
6925 try_move_mult_to_index (location_t loc, tree addr, tree op1)
6927 tree s, delta, step;
6928 tree ref = TREE_OPERAND (addr, 0), pref;
6929 tree ret, pos;
6930 tree itype;
6931 bool mdim = false;
6933 /* Strip the nops that might be added when converting op1 to sizetype. */
6934 STRIP_NOPS (op1);
6936 /* Canonicalize op1 into a possibly non-constant delta
6937 and an INTEGER_CST s. */
6938 if (TREE_CODE (op1) == MULT_EXPR)
6940 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6942 STRIP_NOPS (arg0);
6943 STRIP_NOPS (arg1);
6945 if (TREE_CODE (arg0) == INTEGER_CST)
6947 s = arg0;
6948 delta = arg1;
6950 else if (TREE_CODE (arg1) == INTEGER_CST)
6952 s = arg1;
6953 delta = arg0;
6955 else
6956 return NULL_TREE;
6958 else if (TREE_CODE (op1) == INTEGER_CST)
6960 delta = op1;
6961 s = NULL_TREE;
6963 else
6965 /* Simulate we are delta * 1. */
6966 delta = op1;
6967 s = integer_one_node;
6970 /* Handle &x.array the same as we would handle &x.array[0]. */
6971 if (TREE_CODE (ref) == COMPONENT_REF
6972 && TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE)
6974 tree domain;
6976 /* Remember if this was a multi-dimensional array. */
6977 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6978 mdim = true;
6980 domain = TYPE_DOMAIN (TREE_TYPE (ref));
6981 if (! domain)
6982 goto cont;
6983 itype = TREE_TYPE (domain);
6985 step = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (ref)));
6986 if (TREE_CODE (step) != INTEGER_CST)
6987 goto cont;
6989 if (s)
6991 if (! tree_int_cst_equal (step, s))
6992 goto cont;
6994 else
6996 /* Try if delta is a multiple of step. */
6997 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6998 if (! tmp)
6999 goto cont;
7000 delta = tmp;
7003 /* Only fold here if we can verify we do not overflow one
7004 dimension of a multi-dimensional array. */
7005 if (mdim)
7007 tree tmp;
7009 if (!TYPE_MIN_VALUE (domain)
7010 || !TYPE_MAX_VALUE (domain)
7011 || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
7012 goto cont;
7014 tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
7015 fold_convert_loc (loc, itype,
7016 TYPE_MIN_VALUE (domain)),
7017 fold_convert_loc (loc, itype, delta));
7018 if (TREE_CODE (tmp) != INTEGER_CST
7019 || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
7020 goto cont;
7023 /* We found a suitable component reference. */
7025 pref = TREE_OPERAND (addr, 0);
7026 ret = copy_node (pref);
7027 SET_EXPR_LOCATION (ret, loc);
7029 ret = build4_loc (loc, ARRAY_REF, TREE_TYPE (TREE_TYPE (ref)), ret,
7030 fold_build2_loc
7031 (loc, PLUS_EXPR, itype,
7032 fold_convert_loc (loc, itype,
7033 TYPE_MIN_VALUE
7034 (TYPE_DOMAIN (TREE_TYPE (ref)))),
7035 fold_convert_loc (loc, itype, delta)),
7036 NULL_TREE, NULL_TREE);
7037 return build_fold_addr_expr_loc (loc, ret);
7040 cont:
7042 for (;; ref = TREE_OPERAND (ref, 0))
7044 if (TREE_CODE (ref) == ARRAY_REF)
7046 tree domain;
7048 /* Remember if this was a multi-dimensional array. */
7049 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7050 mdim = true;
7052 domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7053 if (! domain)
7054 continue;
7055 itype = TREE_TYPE (domain);
7057 step = array_ref_element_size (ref);
7058 if (TREE_CODE (step) != INTEGER_CST)
7059 continue;
7061 if (s)
7063 if (! tree_int_cst_equal (step, s))
7064 continue;
7066 else
7068 /* Try if delta is a multiple of step. */
7069 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7070 if (! tmp)
7071 continue;
7072 delta = tmp;
7075 /* Only fold here if we can verify we do not overflow one
7076 dimension of a multi-dimensional array. */
7077 if (mdim)
7079 tree tmp;
7081 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7082 || !TYPE_MAX_VALUE (domain)
7083 || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
7084 continue;
7086 tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
7087 fold_convert_loc (loc, itype,
7088 TREE_OPERAND (ref, 1)),
7089 fold_convert_loc (loc, itype, delta));
7090 if (!tmp
7091 || TREE_CODE (tmp) != INTEGER_CST
7092 || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
7093 continue;
7096 break;
7098 else
7099 mdim = false;
7101 if (!handled_component_p (ref))
7102 return NULL_TREE;
7105 /* We found the suitable array reference. So copy everything up to it,
7106 and replace the index. */
7108 pref = TREE_OPERAND (addr, 0);
7109 ret = copy_node (pref);
7110 SET_EXPR_LOCATION (ret, loc);
7111 pos = ret;
7113 while (pref != ref)
7115 pref = TREE_OPERAND (pref, 0);
7116 TREE_OPERAND (pos, 0) = copy_node (pref);
7117 pos = TREE_OPERAND (pos, 0);
7120 TREE_OPERAND (pos, 1)
7121 = fold_build2_loc (loc, PLUS_EXPR, itype,
7122 fold_convert_loc (loc, itype, TREE_OPERAND (pos, 1)),
7123 fold_convert_loc (loc, itype, delta));
7124 return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
7128 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7129 means A >= Y && A != MAX, but in this case we know that
7130 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7132 static tree
7133 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7135 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7137 if (TREE_CODE (bound) == LT_EXPR)
7138 a = TREE_OPERAND (bound, 0);
7139 else if (TREE_CODE (bound) == GT_EXPR)
7140 a = TREE_OPERAND (bound, 1);
7141 else
7142 return NULL_TREE;
7144 typea = TREE_TYPE (a);
7145 if (!INTEGRAL_TYPE_P (typea)
7146 && !POINTER_TYPE_P (typea))
7147 return NULL_TREE;
7149 if (TREE_CODE (ineq) == LT_EXPR)
7151 a1 = TREE_OPERAND (ineq, 1);
7152 y = TREE_OPERAND (ineq, 0);
7154 else if (TREE_CODE (ineq) == GT_EXPR)
7156 a1 = TREE_OPERAND (ineq, 0);
7157 y = TREE_OPERAND (ineq, 1);
7159 else
7160 return NULL_TREE;
7162 if (TREE_TYPE (a1) != typea)
7163 return NULL_TREE;
7165 if (POINTER_TYPE_P (typea))
7167 /* Convert the pointer types into integer before taking the difference. */
7168 tree ta = fold_convert_loc (loc, ssizetype, a);
7169 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7170 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7172 else
7173 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7175 if (!diff || !integer_onep (diff))
7176 return NULL_TREE;
7178 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7181 /* Fold a sum or difference of at least one multiplication.
7182 Returns the folded tree or NULL if no simplification could be made. */
7184 static tree
7185 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7186 tree arg0, tree arg1)
7188 tree arg00, arg01, arg10, arg11;
7189 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7191 /* (A * C) +- (B * C) -> (A+-B) * C.
7192 (A * C) +- A -> A * (C+-1).
7193 We are most concerned about the case where C is a constant,
7194 but other combinations show up during loop reduction. Since
7195 it is not difficult, try all four possibilities. */
7197 if (TREE_CODE (arg0) == MULT_EXPR)
7199 arg00 = TREE_OPERAND (arg0, 0);
7200 arg01 = TREE_OPERAND (arg0, 1);
7202 else if (TREE_CODE (arg0) == INTEGER_CST)
7204 arg00 = build_one_cst (type);
7205 arg01 = arg0;
7207 else
7209 /* We cannot generate constant 1 for fract. */
7210 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7211 return NULL_TREE;
7212 arg00 = arg0;
7213 arg01 = build_one_cst (type);
7215 if (TREE_CODE (arg1) == MULT_EXPR)
7217 arg10 = TREE_OPERAND (arg1, 0);
7218 arg11 = TREE_OPERAND (arg1, 1);
7220 else if (TREE_CODE (arg1) == INTEGER_CST)
7222 arg10 = build_one_cst (type);
7223 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7224 the purpose of this canonicalization. */
7225 if (TREE_INT_CST_HIGH (arg1) == -1
7226 && negate_expr_p (arg1)
7227 && code == PLUS_EXPR)
7229 arg11 = negate_expr (arg1);
7230 code = MINUS_EXPR;
7232 else
7233 arg11 = arg1;
7235 else
7237 /* We cannot generate constant 1 for fract. */
7238 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7239 return NULL_TREE;
7240 arg10 = arg1;
7241 arg11 = build_one_cst (type);
7243 same = NULL_TREE;
7245 if (operand_equal_p (arg01, arg11, 0))
7246 same = arg01, alt0 = arg00, alt1 = arg10;
7247 else if (operand_equal_p (arg00, arg10, 0))
7248 same = arg00, alt0 = arg01, alt1 = arg11;
7249 else if (operand_equal_p (arg00, arg11, 0))
7250 same = arg00, alt0 = arg01, alt1 = arg10;
7251 else if (operand_equal_p (arg01, arg10, 0))
7252 same = arg01, alt0 = arg00, alt1 = arg11;
7254 /* No identical multiplicands; see if we can find a common
7255 power-of-two factor in non-power-of-two multiplies. This
7256 can help in multi-dimensional array access. */
7257 else if (host_integerp (arg01, 0)
7258 && host_integerp (arg11, 0))
7260 HOST_WIDE_INT int01, int11, tmp;
7261 bool swap = false;
7262 tree maybe_same;
7263 int01 = TREE_INT_CST_LOW (arg01);
7264 int11 = TREE_INT_CST_LOW (arg11);
7266 /* Move min of absolute values to int11. */
7267 if (absu_hwi (int01) < absu_hwi (int11))
7269 tmp = int01, int01 = int11, int11 = tmp;
7270 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7271 maybe_same = arg01;
7272 swap = true;
7274 else
7275 maybe_same = arg11;
7277 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7278 /* The remainder should not be a constant, otherwise we
7279 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7280 increased the number of multiplications necessary. */
7281 && TREE_CODE (arg10) != INTEGER_CST)
7283 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7284 build_int_cst (TREE_TYPE (arg00),
7285 int01 / int11));
7286 alt1 = arg10;
7287 same = maybe_same;
7288 if (swap)
7289 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7293 if (same)
7294 return fold_build2_loc (loc, MULT_EXPR, type,
7295 fold_build2_loc (loc, code, type,
7296 fold_convert_loc (loc, type, alt0),
7297 fold_convert_loc (loc, type, alt1)),
7298 fold_convert_loc (loc, type, same));
7300 return NULL_TREE;
7303 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7304 specified by EXPR into the buffer PTR of length LEN bytes.
7305 Return the number of bytes placed in the buffer, or zero
7306 upon failure. */
7308 static int
7309 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7311 tree type = TREE_TYPE (expr);
7312 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7313 int byte, offset, word, words;
7314 unsigned char value;
7316 if (total_bytes > len)
7317 return 0;
7318 words = total_bytes / UNITS_PER_WORD;
7320 for (byte = 0; byte < total_bytes; byte++)
7322 int bitpos = byte * BITS_PER_UNIT;
7323 if (bitpos < HOST_BITS_PER_WIDE_INT)
7324 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7325 else
7326 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7327 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7329 if (total_bytes > UNITS_PER_WORD)
7331 word = byte / UNITS_PER_WORD;
7332 if (WORDS_BIG_ENDIAN)
7333 word = (words - 1) - word;
7334 offset = word * UNITS_PER_WORD;
7335 if (BYTES_BIG_ENDIAN)
7336 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7337 else
7338 offset += byte % UNITS_PER_WORD;
7340 else
7341 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7342 ptr[offset] = value;
7344 return total_bytes;
7348 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7349 specified by EXPR into the buffer PTR of length LEN bytes.
7350 Return the number of bytes placed in the buffer, or zero
7351 upon failure. */
7353 static int
7354 native_encode_fixed (const_tree expr, unsigned char *ptr, int len)
7356 tree type = TREE_TYPE (expr);
7357 enum machine_mode mode = TYPE_MODE (type);
7358 int total_bytes = GET_MODE_SIZE (mode);
7359 FIXED_VALUE_TYPE value;
7360 tree i_value, i_type;
7362 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7363 return 0;
7365 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7367 if (NULL_TREE == i_type
7368 || TYPE_PRECISION (i_type) != total_bytes)
7369 return 0;
7371 value = TREE_FIXED_CST (expr);
7372 i_value = double_int_to_tree (i_type, value.data);
7374 return native_encode_int (i_value, ptr, len);
7378 /* Subroutine of native_encode_expr. Encode the REAL_CST
7379 specified by EXPR into the buffer PTR of length LEN bytes.
7380 Return the number of bytes placed in the buffer, or zero
7381 upon failure. */
7383 static int
7384 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7386 tree type = TREE_TYPE (expr);
7387 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7388 int byte, offset, word, words, bitpos;
7389 unsigned char value;
7391 /* There are always 32 bits in each long, no matter the size of
7392 the hosts long. We handle floating point representations with
7393 up to 192 bits. */
7394 long tmp[6];
7396 if (total_bytes > len)
7397 return 0;
7398 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7400 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7402 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7403 bitpos += BITS_PER_UNIT)
7405 byte = (bitpos / BITS_PER_UNIT) & 3;
7406 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7408 if (UNITS_PER_WORD < 4)
7410 word = byte / UNITS_PER_WORD;
7411 if (WORDS_BIG_ENDIAN)
7412 word = (words - 1) - word;
7413 offset = word * UNITS_PER_WORD;
7414 if (BYTES_BIG_ENDIAN)
7415 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7416 else
7417 offset += byte % UNITS_PER_WORD;
7419 else
7420 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7421 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7423 return total_bytes;
7426 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7427 specified by EXPR into the buffer PTR of length LEN bytes.
7428 Return the number of bytes placed in the buffer, or zero
7429 upon failure. */
7431 static int
7432 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7434 int rsize, isize;
7435 tree part;
7437 part = TREE_REALPART (expr);
7438 rsize = native_encode_expr (part, ptr, len);
7439 if (rsize == 0)
7440 return 0;
7441 part = TREE_IMAGPART (expr);
7442 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7443 if (isize != rsize)
7444 return 0;
7445 return rsize + isize;
7449 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7450 specified by EXPR into the buffer PTR of length LEN bytes.
7451 Return the number of bytes placed in the buffer, or zero
7452 upon failure. */
7454 static int
7455 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7457 unsigned i, count;
7458 int size, offset;
7459 tree itype, elem;
7461 offset = 0;
7462 count = VECTOR_CST_NELTS (expr);
7463 itype = TREE_TYPE (TREE_TYPE (expr));
7464 size = GET_MODE_SIZE (TYPE_MODE (itype));
7465 for (i = 0; i < count; i++)
7467 elem = VECTOR_CST_ELT (expr, i);
7468 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7469 return 0;
7470 offset += size;
7472 return offset;
7476 /* Subroutine of native_encode_expr. Encode the STRING_CST
7477 specified by EXPR into the buffer PTR of length LEN bytes.
7478 Return the number of bytes placed in the buffer, or zero
7479 upon failure. */
7481 static int
7482 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7484 tree type = TREE_TYPE (expr);
7485 HOST_WIDE_INT total_bytes;
7487 if (TREE_CODE (type) != ARRAY_TYPE
7488 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7489 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7490 || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7491 return 0;
7492 total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7493 if (total_bytes > len)
7494 return 0;
7495 if (TREE_STRING_LENGTH (expr) < total_bytes)
7497 memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7498 memset (ptr + TREE_STRING_LENGTH (expr), 0,
7499 total_bytes - TREE_STRING_LENGTH (expr));
7501 else
7502 memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7503 return total_bytes;
7507 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7508 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7509 buffer PTR of length LEN bytes. Return the number of bytes
7510 placed in the buffer, or zero upon failure. */
7513 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7515 switch (TREE_CODE (expr))
7517 case INTEGER_CST:
7518 return native_encode_int (expr, ptr, len);
7520 case REAL_CST:
7521 return native_encode_real (expr, ptr, len);
7523 case FIXED_CST:
7524 return native_encode_fixed (expr, ptr, len);
7526 case COMPLEX_CST:
7527 return native_encode_complex (expr, ptr, len);
7529 case VECTOR_CST:
7530 return native_encode_vector (expr, ptr, len);
7532 case STRING_CST:
7533 return native_encode_string (expr, ptr, len);
7535 default:
7536 return 0;
7541 /* Subroutine of native_interpret_expr. Interpret the contents of
7542 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7543 If the buffer cannot be interpreted, return NULL_TREE. */
7545 static tree
7546 native_interpret_int (tree type, const unsigned char *ptr, int len)
7548 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7549 double_int result;
7551 if (total_bytes > len
7552 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7553 return NULL_TREE;
7555 result = double_int::from_buffer (ptr, total_bytes);
7557 return double_int_to_tree (type, result);
7561 /* Subroutine of native_interpret_expr. Interpret the contents of
7562 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7563 If the buffer cannot be interpreted, return NULL_TREE. */
7565 static tree
7566 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7568 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7569 double_int result;
7570 FIXED_VALUE_TYPE fixed_value;
7572 if (total_bytes > len
7573 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7574 return NULL_TREE;
7576 result = double_int::from_buffer (ptr, total_bytes);
7577 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7579 return build_fixed (type, fixed_value);
7583 /* Subroutine of native_interpret_expr. Interpret the contents of
7584 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7585 If the buffer cannot be interpreted, return NULL_TREE. */
7587 static tree
7588 native_interpret_real (tree type, const unsigned char *ptr, int len)
7590 enum machine_mode mode = TYPE_MODE (type);
7591 int total_bytes = GET_MODE_SIZE (mode);
7592 int byte, offset, word, words, bitpos;
7593 unsigned char value;
7594 /* There are always 32 bits in each long, no matter the size of
7595 the hosts long. We handle floating point representations with
7596 up to 192 bits. */
7597 REAL_VALUE_TYPE r;
7598 long tmp[6];
7600 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7601 if (total_bytes > len || total_bytes > 24)
7602 return NULL_TREE;
7603 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7605 memset (tmp, 0, sizeof (tmp));
7606 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7607 bitpos += BITS_PER_UNIT)
7609 byte = (bitpos / BITS_PER_UNIT) & 3;
7610 if (UNITS_PER_WORD < 4)
7612 word = byte / UNITS_PER_WORD;
7613 if (WORDS_BIG_ENDIAN)
7614 word = (words - 1) - word;
7615 offset = word * UNITS_PER_WORD;
7616 if (BYTES_BIG_ENDIAN)
7617 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7618 else
7619 offset += byte % UNITS_PER_WORD;
7621 else
7622 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7623 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7625 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7628 real_from_target (&r, tmp, mode);
7629 return build_real (type, r);
7633 /* Subroutine of native_interpret_expr. Interpret the contents of
7634 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7635 If the buffer cannot be interpreted, return NULL_TREE. */
7637 static tree
7638 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7640 tree etype, rpart, ipart;
7641 int size;
7643 etype = TREE_TYPE (type);
7644 size = GET_MODE_SIZE (TYPE_MODE (etype));
7645 if (size * 2 > len)
7646 return NULL_TREE;
7647 rpart = native_interpret_expr (etype, ptr, size);
7648 if (!rpart)
7649 return NULL_TREE;
7650 ipart = native_interpret_expr (etype, ptr+size, size);
7651 if (!ipart)
7652 return NULL_TREE;
7653 return build_complex (type, rpart, ipart);
7657 /* Subroutine of native_interpret_expr. Interpret the contents of
7658 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7659 If the buffer cannot be interpreted, return NULL_TREE. */
7661 static tree
7662 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7664 tree etype, elem;
7665 int i, size, count;
7666 tree *elements;
7668 etype = TREE_TYPE (type);
7669 size = GET_MODE_SIZE (TYPE_MODE (etype));
7670 count = TYPE_VECTOR_SUBPARTS (type);
7671 if (size * count > len)
7672 return NULL_TREE;
7674 elements = XALLOCAVEC (tree, count);
7675 for (i = count - 1; i >= 0; i--)
7677 elem = native_interpret_expr (etype, ptr+(i*size), size);
7678 if (!elem)
7679 return NULL_TREE;
7680 elements[i] = elem;
7682 return build_vector (type, elements);
7686 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7687 the buffer PTR of length LEN as a constant of type TYPE. For
7688 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7689 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7690 return NULL_TREE. */
7692 tree
7693 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7695 switch (TREE_CODE (type))
7697 case INTEGER_TYPE:
7698 case ENUMERAL_TYPE:
7699 case BOOLEAN_TYPE:
7700 case POINTER_TYPE:
7701 case REFERENCE_TYPE:
7702 return native_interpret_int (type, ptr, len);
7704 case REAL_TYPE:
7705 return native_interpret_real (type, ptr, len);
7707 case FIXED_POINT_TYPE:
7708 return native_interpret_fixed (type, ptr, len);
7710 case COMPLEX_TYPE:
7711 return native_interpret_complex (type, ptr, len);
7713 case VECTOR_TYPE:
7714 return native_interpret_vector (type, ptr, len);
7716 default:
7717 return NULL_TREE;
7721 /* Returns true if we can interpret the contents of a native encoding
7722 as TYPE. */
7724 static bool
7725 can_native_interpret_type_p (tree type)
7727 switch (TREE_CODE (type))
7729 case INTEGER_TYPE:
7730 case ENUMERAL_TYPE:
7731 case BOOLEAN_TYPE:
7732 case POINTER_TYPE:
7733 case REFERENCE_TYPE:
7734 case FIXED_POINT_TYPE:
7735 case REAL_TYPE:
7736 case COMPLEX_TYPE:
7737 case VECTOR_TYPE:
7738 return true;
7739 default:
7740 return false;
7744 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7745 TYPE at compile-time. If we're unable to perform the conversion
7746 return NULL_TREE. */
7748 static tree
7749 fold_view_convert_expr (tree type, tree expr)
7751 /* We support up to 512-bit values (for V8DFmode). */
7752 unsigned char buffer[64];
7753 int len;
7755 /* Check that the host and target are sane. */
7756 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7757 return NULL_TREE;
7759 len = native_encode_expr (expr, buffer, sizeof (buffer));
7760 if (len == 0)
7761 return NULL_TREE;
7763 return native_interpret_expr (type, buffer, len);
7766 /* Build an expression for the address of T. Folds away INDIRECT_REF
7767 to avoid confusing the gimplify process. */
7769 tree
7770 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7772 /* The size of the object is not relevant when talking about its address. */
7773 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7774 t = TREE_OPERAND (t, 0);
7776 if (TREE_CODE (t) == INDIRECT_REF)
7778 t = TREE_OPERAND (t, 0);
7780 if (TREE_TYPE (t) != ptrtype)
7781 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7783 else if (TREE_CODE (t) == MEM_REF
7784 && integer_zerop (TREE_OPERAND (t, 1)))
7785 return TREE_OPERAND (t, 0);
7786 else if (TREE_CODE (t) == MEM_REF
7787 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7788 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7789 TREE_OPERAND (t, 0),
7790 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7791 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7793 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7795 if (TREE_TYPE (t) != ptrtype)
7796 t = fold_convert_loc (loc, ptrtype, t);
7798 else
7799 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7801 return t;
7804 /* Build an expression for the address of T. */
7806 tree
7807 build_fold_addr_expr_loc (location_t loc, tree t)
7809 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7811 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7814 static bool vec_cst_ctor_to_array (tree, tree *);
7816 /* Fold a unary expression of code CODE and type TYPE with operand
7817 OP0. Return the folded expression if folding is successful.
7818 Otherwise, return NULL_TREE. */
7820 tree
7821 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7823 tree tem;
7824 tree arg0;
7825 enum tree_code_class kind = TREE_CODE_CLASS (code);
7827 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7828 && TREE_CODE_LENGTH (code) == 1);
7830 arg0 = op0;
7831 if (arg0)
7833 if (CONVERT_EXPR_CODE_P (code)
7834 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7836 /* Don't use STRIP_NOPS, because signedness of argument type
7837 matters. */
7838 STRIP_SIGN_NOPS (arg0);
7840 else
7842 /* Strip any conversions that don't change the mode. This
7843 is safe for every expression, except for a comparison
7844 expression because its signedness is derived from its
7845 operands.
7847 Note that this is done as an internal manipulation within
7848 the constant folder, in order to find the simplest
7849 representation of the arguments so that their form can be
7850 studied. In any cases, the appropriate type conversions
7851 should be put back in the tree that will get out of the
7852 constant folder. */
7853 STRIP_NOPS (arg0);
7857 if (TREE_CODE_CLASS (code) == tcc_unary)
7859 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7860 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7861 fold_build1_loc (loc, code, type,
7862 fold_convert_loc (loc, TREE_TYPE (op0),
7863 TREE_OPERAND (arg0, 1))));
7864 else if (TREE_CODE (arg0) == COND_EXPR)
7866 tree arg01 = TREE_OPERAND (arg0, 1);
7867 tree arg02 = TREE_OPERAND (arg0, 2);
7868 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7869 arg01 = fold_build1_loc (loc, code, type,
7870 fold_convert_loc (loc,
7871 TREE_TYPE (op0), arg01));
7872 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7873 arg02 = fold_build1_loc (loc, code, type,
7874 fold_convert_loc (loc,
7875 TREE_TYPE (op0), arg02));
7876 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7877 arg01, arg02);
7879 /* If this was a conversion, and all we did was to move into
7880 inside the COND_EXPR, bring it back out. But leave it if
7881 it is a conversion from integer to integer and the
7882 result precision is no wider than a word since such a
7883 conversion is cheap and may be optimized away by combine,
7884 while it couldn't if it were outside the COND_EXPR. Then return
7885 so we don't get into an infinite recursion loop taking the
7886 conversion out and then back in. */
7888 if ((CONVERT_EXPR_CODE_P (code)
7889 || code == NON_LVALUE_EXPR)
7890 && TREE_CODE (tem) == COND_EXPR
7891 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7892 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7893 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7894 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7895 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7896 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7897 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7898 && (INTEGRAL_TYPE_P
7899 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7900 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7901 || flag_syntax_only))
7902 tem = build1_loc (loc, code, type,
7903 build3 (COND_EXPR,
7904 TREE_TYPE (TREE_OPERAND
7905 (TREE_OPERAND (tem, 1), 0)),
7906 TREE_OPERAND (tem, 0),
7907 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7908 TREE_OPERAND (TREE_OPERAND (tem, 2),
7909 0)));
7910 return tem;
7914 switch (code)
7916 case PAREN_EXPR:
7917 /* Re-association barriers around constants and other re-association
7918 barriers can be removed. */
7919 if (CONSTANT_CLASS_P (op0)
7920 || TREE_CODE (op0) == PAREN_EXPR)
7921 return fold_convert_loc (loc, type, op0);
7922 return NULL_TREE;
7924 CASE_CONVERT:
7925 case FLOAT_EXPR:
7926 case FIX_TRUNC_EXPR:
7927 if (TREE_TYPE (op0) == type)
7928 return op0;
7930 if (COMPARISON_CLASS_P (op0))
7932 /* If we have (type) (a CMP b) and type is an integral type, return
7933 new expression involving the new type. Canonicalize
7934 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7935 non-integral type.
7936 Do not fold the result as that would not simplify further, also
7937 folding again results in recursions. */
7938 if (TREE_CODE (type) == BOOLEAN_TYPE)
7939 return build2_loc (loc, TREE_CODE (op0), type,
7940 TREE_OPERAND (op0, 0),
7941 TREE_OPERAND (op0, 1));
7942 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7943 && TREE_CODE (type) != VECTOR_TYPE)
7944 return build3_loc (loc, COND_EXPR, type, op0,
7945 constant_boolean_node (true, type),
7946 constant_boolean_node (false, type));
7949 /* Handle cases of two conversions in a row. */
7950 if (CONVERT_EXPR_P (op0))
7952 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7953 tree inter_type = TREE_TYPE (op0);
7954 int inside_int = INTEGRAL_TYPE_P (inside_type);
7955 int inside_ptr = POINTER_TYPE_P (inside_type);
7956 int inside_float = FLOAT_TYPE_P (inside_type);
7957 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7958 unsigned int inside_prec = TYPE_PRECISION (inside_type);
7959 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7960 int inter_int = INTEGRAL_TYPE_P (inter_type);
7961 int inter_ptr = POINTER_TYPE_P (inter_type);
7962 int inter_float = FLOAT_TYPE_P (inter_type);
7963 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7964 unsigned int inter_prec = TYPE_PRECISION (inter_type);
7965 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7966 int final_int = INTEGRAL_TYPE_P (type);
7967 int final_ptr = POINTER_TYPE_P (type);
7968 int final_float = FLOAT_TYPE_P (type);
7969 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7970 unsigned int final_prec = TYPE_PRECISION (type);
7971 int final_unsignedp = TYPE_UNSIGNED (type);
7973 /* In addition to the cases of two conversions in a row
7974 handled below, if we are converting something to its own
7975 type via an object of identical or wider precision, neither
7976 conversion is needed. */
7977 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7978 && (((inter_int || inter_ptr) && final_int)
7979 || (inter_float && final_float))
7980 && inter_prec >= final_prec)
7981 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7983 /* Likewise, if the intermediate and initial types are either both
7984 float or both integer, we don't need the middle conversion if the
7985 former is wider than the latter and doesn't change the signedness
7986 (for integers). Avoid this if the final type is a pointer since
7987 then we sometimes need the middle conversion. Likewise if the
7988 final type has a precision not equal to the size of its mode. */
7989 if (((inter_int && inside_int)
7990 || (inter_float && inside_float)
7991 || (inter_vec && inside_vec))
7992 && inter_prec >= inside_prec
7993 && (inter_float || inter_vec
7994 || inter_unsignedp == inside_unsignedp)
7995 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
7996 && TYPE_MODE (type) == TYPE_MODE (inter_type))
7997 && ! final_ptr
7998 && (! final_vec || inter_prec == inside_prec))
7999 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8001 /* If we have a sign-extension of a zero-extended value, we can
8002 replace that by a single zero-extension. Likewise if the
8003 final conversion does not change precision we can drop the
8004 intermediate conversion. */
8005 if (inside_int && inter_int && final_int
8006 && ((inside_prec < inter_prec && inter_prec < final_prec
8007 && inside_unsignedp && !inter_unsignedp)
8008 || final_prec == inter_prec))
8009 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8011 /* Two conversions in a row are not needed unless:
8012 - some conversion is floating-point (overstrict for now), or
8013 - some conversion is a vector (overstrict for now), or
8014 - the intermediate type is narrower than both initial and
8015 final, or
8016 - the intermediate type and innermost type differ in signedness,
8017 and the outermost type is wider than the intermediate, or
8018 - the initial type is a pointer type and the precisions of the
8019 intermediate and final types differ, or
8020 - the final type is a pointer type and the precisions of the
8021 initial and intermediate types differ. */
8022 if (! inside_float && ! inter_float && ! final_float
8023 && ! inside_vec && ! inter_vec && ! final_vec
8024 && (inter_prec >= inside_prec || inter_prec >= final_prec)
8025 && ! (inside_int && inter_int
8026 && inter_unsignedp != inside_unsignedp
8027 && inter_prec < final_prec)
8028 && ((inter_unsignedp && inter_prec > inside_prec)
8029 == (final_unsignedp && final_prec > inter_prec))
8030 && ! (inside_ptr && inter_prec != final_prec)
8031 && ! (final_ptr && inside_prec != inter_prec)
8032 && ! (final_prec != GET_MODE_PRECISION (TYPE_MODE (type))
8033 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8034 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8037 /* Handle (T *)&A.B.C for A being of type T and B and C
8038 living at offset zero. This occurs frequently in
8039 C++ upcasting and then accessing the base. */
8040 if (TREE_CODE (op0) == ADDR_EXPR
8041 && POINTER_TYPE_P (type)
8042 && handled_component_p (TREE_OPERAND (op0, 0)))
8044 HOST_WIDE_INT bitsize, bitpos;
8045 tree offset;
8046 enum machine_mode mode;
8047 int unsignedp, volatilep;
8048 tree base = TREE_OPERAND (op0, 0);
8049 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8050 &mode, &unsignedp, &volatilep, false);
8051 /* If the reference was to a (constant) zero offset, we can use
8052 the address of the base if it has the same base type
8053 as the result type and the pointer type is unqualified. */
8054 if (! offset && bitpos == 0
8055 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
8056 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8057 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
8058 return fold_convert_loc (loc, type,
8059 build_fold_addr_expr_loc (loc, base));
8062 if (TREE_CODE (op0) == MODIFY_EXPR
8063 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8064 /* Detect assigning a bitfield. */
8065 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8066 && DECL_BIT_FIELD
8067 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8069 /* Don't leave an assignment inside a conversion
8070 unless assigning a bitfield. */
8071 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
8072 /* First do the assignment, then return converted constant. */
8073 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8074 TREE_NO_WARNING (tem) = 1;
8075 TREE_USED (tem) = 1;
8076 return tem;
8079 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8080 constants (if x has signed type, the sign bit cannot be set
8081 in c). This folds extension into the BIT_AND_EXPR.
8082 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8083 very likely don't have maximal range for their precision and this
8084 transformation effectively doesn't preserve non-maximal ranges. */
8085 if (TREE_CODE (type) == INTEGER_TYPE
8086 && TREE_CODE (op0) == BIT_AND_EXPR
8087 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
8089 tree and_expr = op0;
8090 tree and0 = TREE_OPERAND (and_expr, 0);
8091 tree and1 = TREE_OPERAND (and_expr, 1);
8092 int change = 0;
8094 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
8095 || (TYPE_PRECISION (type)
8096 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
8097 change = 1;
8098 else if (TYPE_PRECISION (TREE_TYPE (and1))
8099 <= HOST_BITS_PER_WIDE_INT
8100 && host_integerp (and1, 1))
8102 unsigned HOST_WIDE_INT cst;
8104 cst = tree_low_cst (and1, 1);
8105 cst &= HOST_WIDE_INT_M1U
8106 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8107 change = (cst == 0);
8108 #ifdef LOAD_EXTEND_OP
8109 if (change
8110 && !flag_syntax_only
8111 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8112 == ZERO_EXTEND))
8114 tree uns = unsigned_type_for (TREE_TYPE (and0));
8115 and0 = fold_convert_loc (loc, uns, and0);
8116 and1 = fold_convert_loc (loc, uns, and1);
8118 #endif
8120 if (change)
8122 tem = force_fit_type_double (type, tree_to_double_int (and1),
8123 0, TREE_OVERFLOW (and1));
8124 return fold_build2_loc (loc, BIT_AND_EXPR, type,
8125 fold_convert_loc (loc, type, and0), tem);
8129 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8130 when one of the new casts will fold away. Conservatively we assume
8131 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8132 if (POINTER_TYPE_P (type)
8133 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8134 && (!TYPE_RESTRICT (type) || TYPE_RESTRICT (TREE_TYPE (arg0)))
8135 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8136 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8137 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8139 tree arg00 = TREE_OPERAND (arg0, 0);
8140 tree arg01 = TREE_OPERAND (arg0, 1);
8142 return fold_build_pointer_plus_loc
8143 (loc, fold_convert_loc (loc, type, arg00), arg01);
8146 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8147 of the same precision, and X is an integer type not narrower than
8148 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8149 if (INTEGRAL_TYPE_P (type)
8150 && TREE_CODE (op0) == BIT_NOT_EXPR
8151 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8152 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8153 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8155 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8156 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8157 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8158 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
8159 fold_convert_loc (loc, type, tem));
8162 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8163 type of X and Y (integer types only). */
8164 if (INTEGRAL_TYPE_P (type)
8165 && TREE_CODE (op0) == MULT_EXPR
8166 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8167 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8169 /* Be careful not to introduce new overflows. */
8170 tree mult_type;
8171 if (TYPE_OVERFLOW_WRAPS (type))
8172 mult_type = type;
8173 else
8174 mult_type = unsigned_type_for (type);
8176 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8178 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8179 fold_convert_loc (loc, mult_type,
8180 TREE_OPERAND (op0, 0)),
8181 fold_convert_loc (loc, mult_type,
8182 TREE_OPERAND (op0, 1)));
8183 return fold_convert_loc (loc, type, tem);
8187 tem = fold_convert_const (code, type, op0);
8188 return tem ? tem : NULL_TREE;
8190 case ADDR_SPACE_CONVERT_EXPR:
8191 if (integer_zerop (arg0))
8192 return fold_convert_const (code, type, arg0);
8193 return NULL_TREE;
8195 case FIXED_CONVERT_EXPR:
8196 tem = fold_convert_const (code, type, arg0);
8197 return tem ? tem : NULL_TREE;
8199 case VIEW_CONVERT_EXPR:
8200 if (TREE_TYPE (op0) == type)
8201 return op0;
8202 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8203 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8204 type, TREE_OPERAND (op0, 0));
8205 if (TREE_CODE (op0) == MEM_REF)
8206 return fold_build2_loc (loc, MEM_REF, type,
8207 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8209 /* For integral conversions with the same precision or pointer
8210 conversions use a NOP_EXPR instead. */
8211 if ((INTEGRAL_TYPE_P (type)
8212 || POINTER_TYPE_P (type))
8213 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8214 || POINTER_TYPE_P (TREE_TYPE (op0)))
8215 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8216 return fold_convert_loc (loc, type, op0);
8218 /* Strip inner integral conversions that do not change the precision. */
8219 if (CONVERT_EXPR_P (op0)
8220 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8221 || POINTER_TYPE_P (TREE_TYPE (op0)))
8222 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8223 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8224 && (TYPE_PRECISION (TREE_TYPE (op0))
8225 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8226 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8227 type, TREE_OPERAND (op0, 0));
8229 return fold_view_convert_expr (type, op0);
8231 case NEGATE_EXPR:
8232 tem = fold_negate_expr (loc, arg0);
8233 if (tem)
8234 return fold_convert_loc (loc, type, tem);
8235 return NULL_TREE;
8237 case ABS_EXPR:
8238 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8239 return fold_abs_const (arg0, type);
8240 else if (TREE_CODE (arg0) == NEGATE_EXPR)
8241 return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8242 /* Convert fabs((double)float) into (double)fabsf(float). */
8243 else if (TREE_CODE (arg0) == NOP_EXPR
8244 && TREE_CODE (type) == REAL_TYPE)
8246 tree targ0 = strip_float_extensions (arg0);
8247 if (targ0 != arg0)
8248 return fold_convert_loc (loc, type,
8249 fold_build1_loc (loc, ABS_EXPR,
8250 TREE_TYPE (targ0),
8251 targ0));
8253 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8254 else if (TREE_CODE (arg0) == ABS_EXPR)
8255 return arg0;
8256 else if (tree_expr_nonnegative_p (arg0))
8257 return arg0;
8259 /* Strip sign ops from argument. */
8260 if (TREE_CODE (type) == REAL_TYPE)
8262 tem = fold_strip_sign_ops (arg0);
8263 if (tem)
8264 return fold_build1_loc (loc, ABS_EXPR, type,
8265 fold_convert_loc (loc, type, tem));
8267 return NULL_TREE;
8269 case CONJ_EXPR:
8270 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8271 return fold_convert_loc (loc, type, arg0);
8272 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8274 tree itype = TREE_TYPE (type);
8275 tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8276 tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8277 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8278 negate_expr (ipart));
8280 if (TREE_CODE (arg0) == COMPLEX_CST)
8282 tree itype = TREE_TYPE (type);
8283 tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8284 tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8285 return build_complex (type, rpart, negate_expr (ipart));
8287 if (TREE_CODE (arg0) == CONJ_EXPR)
8288 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8289 return NULL_TREE;
8291 case BIT_NOT_EXPR:
8292 if (TREE_CODE (arg0) == INTEGER_CST)
8293 return fold_not_const (arg0, type);
8294 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8295 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8296 /* Convert ~ (-A) to A - 1. */
8297 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8298 return fold_build2_loc (loc, MINUS_EXPR, type,
8299 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8300 build_int_cst (type, 1));
8301 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8302 else if (INTEGRAL_TYPE_P (type)
8303 && ((TREE_CODE (arg0) == MINUS_EXPR
8304 && integer_onep (TREE_OPERAND (arg0, 1)))
8305 || (TREE_CODE (arg0) == PLUS_EXPR
8306 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8307 return fold_build1_loc (loc, NEGATE_EXPR, type,
8308 fold_convert_loc (loc, type,
8309 TREE_OPERAND (arg0, 0)));
8310 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8311 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8312 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8313 fold_convert_loc (loc, type,
8314 TREE_OPERAND (arg0, 0)))))
8315 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8316 fold_convert_loc (loc, type,
8317 TREE_OPERAND (arg0, 1)));
8318 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8319 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8320 fold_convert_loc (loc, type,
8321 TREE_OPERAND (arg0, 1)))))
8322 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8323 fold_convert_loc (loc, type,
8324 TREE_OPERAND (arg0, 0)), tem);
8325 /* Perform BIT_NOT_EXPR on each element individually. */
8326 else if (TREE_CODE (arg0) == VECTOR_CST)
8328 tree *elements;
8329 tree elem;
8330 unsigned count = VECTOR_CST_NELTS (arg0), i;
8332 elements = XALLOCAVEC (tree, count);
8333 for (i = 0; i < count; i++)
8335 elem = VECTOR_CST_ELT (arg0, i);
8336 elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8337 if (elem == NULL_TREE)
8338 break;
8339 elements[i] = elem;
8341 if (i == count)
8342 return build_vector (type, elements);
8344 else if (COMPARISON_CLASS_P (arg0)
8345 && (VECTOR_TYPE_P (type)
8346 || (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) == 1)))
8348 tree op_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
8349 enum tree_code subcode = invert_tree_comparison (TREE_CODE (arg0),
8350 HONOR_NANS (TYPE_MODE (op_type)));
8351 if (subcode != ERROR_MARK)
8352 return build2_loc (loc, subcode, type, TREE_OPERAND (arg0, 0),
8353 TREE_OPERAND (arg0, 1));
8357 return NULL_TREE;
8359 case TRUTH_NOT_EXPR:
8360 /* Note that the operand of this must be an int
8361 and its values must be 0 or 1.
8362 ("true" is a fixed value perhaps depending on the language,
8363 but we don't handle values other than 1 correctly yet.) */
8364 tem = fold_truth_not_expr (loc, arg0);
8365 if (!tem)
8366 return NULL_TREE;
8367 return fold_convert_loc (loc, type, tem);
8369 case REALPART_EXPR:
8370 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8371 return fold_convert_loc (loc, type, arg0);
8372 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8373 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8374 TREE_OPERAND (arg0, 1));
8375 if (TREE_CODE (arg0) == COMPLEX_CST)
8376 return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8377 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8379 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8380 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8381 fold_build1_loc (loc, REALPART_EXPR, itype,
8382 TREE_OPERAND (arg0, 0)),
8383 fold_build1_loc (loc, REALPART_EXPR, itype,
8384 TREE_OPERAND (arg0, 1)));
8385 return fold_convert_loc (loc, type, tem);
8387 if (TREE_CODE (arg0) == CONJ_EXPR)
8389 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8390 tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8391 TREE_OPERAND (arg0, 0));
8392 return fold_convert_loc (loc, type, tem);
8394 if (TREE_CODE (arg0) == CALL_EXPR)
8396 tree fn = get_callee_fndecl (arg0);
8397 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8398 switch (DECL_FUNCTION_CODE (fn))
8400 CASE_FLT_FN (BUILT_IN_CEXPI):
8401 fn = mathfn_built_in (type, BUILT_IN_COS);
8402 if (fn)
8403 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8404 break;
8406 default:
8407 break;
8410 return NULL_TREE;
8412 case IMAGPART_EXPR:
8413 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8414 return build_zero_cst (type);
8415 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8416 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8417 TREE_OPERAND (arg0, 0));
8418 if (TREE_CODE (arg0) == COMPLEX_CST)
8419 return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8420 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8422 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8423 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8424 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8425 TREE_OPERAND (arg0, 0)),
8426 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8427 TREE_OPERAND (arg0, 1)));
8428 return fold_convert_loc (loc, type, tem);
8430 if (TREE_CODE (arg0) == CONJ_EXPR)
8432 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8433 tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8434 return fold_convert_loc (loc, type, negate_expr (tem));
8436 if (TREE_CODE (arg0) == CALL_EXPR)
8438 tree fn = get_callee_fndecl (arg0);
8439 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8440 switch (DECL_FUNCTION_CODE (fn))
8442 CASE_FLT_FN (BUILT_IN_CEXPI):
8443 fn = mathfn_built_in (type, BUILT_IN_SIN);
8444 if (fn)
8445 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8446 break;
8448 default:
8449 break;
8452 return NULL_TREE;
8454 case INDIRECT_REF:
8455 /* Fold *&X to X if X is an lvalue. */
8456 if (TREE_CODE (op0) == ADDR_EXPR)
8458 tree op00 = TREE_OPERAND (op0, 0);
8459 if ((TREE_CODE (op00) == VAR_DECL
8460 || TREE_CODE (op00) == PARM_DECL
8461 || TREE_CODE (op00) == RESULT_DECL)
8462 && !TREE_READONLY (op00))
8463 return op00;
8465 return NULL_TREE;
8467 case VEC_UNPACK_LO_EXPR:
8468 case VEC_UNPACK_HI_EXPR:
8469 case VEC_UNPACK_FLOAT_LO_EXPR:
8470 case VEC_UNPACK_FLOAT_HI_EXPR:
8472 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8473 tree *elts;
8474 enum tree_code subcode;
8476 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
8477 if (TREE_CODE (arg0) != VECTOR_CST)
8478 return NULL_TREE;
8480 elts = XALLOCAVEC (tree, nelts * 2);
8481 if (!vec_cst_ctor_to_array (arg0, elts))
8482 return NULL_TREE;
8484 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
8485 || code == VEC_UNPACK_FLOAT_LO_EXPR))
8486 elts += nelts;
8488 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
8489 subcode = NOP_EXPR;
8490 else
8491 subcode = FLOAT_EXPR;
8493 for (i = 0; i < nelts; i++)
8495 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
8496 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
8497 return NULL_TREE;
8500 return build_vector (type, elts);
8503 case REDUC_MIN_EXPR:
8504 case REDUC_MAX_EXPR:
8505 case REDUC_PLUS_EXPR:
8507 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8508 tree *elts;
8509 enum tree_code subcode;
8511 if (TREE_CODE (op0) != VECTOR_CST)
8512 return NULL_TREE;
8514 elts = XALLOCAVEC (tree, nelts);
8515 if (!vec_cst_ctor_to_array (op0, elts))
8516 return NULL_TREE;
8518 switch (code)
8520 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
8521 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
8522 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
8523 default: gcc_unreachable ();
8526 for (i = 1; i < nelts; i++)
8528 elts[0] = const_binop (subcode, elts[0], elts[i]);
8529 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
8530 return NULL_TREE;
8531 elts[i] = build_zero_cst (TREE_TYPE (type));
8534 return build_vector (type, elts);
8537 default:
8538 return NULL_TREE;
8539 } /* switch (code) */
8543 /* If the operation was a conversion do _not_ mark a resulting constant
8544 with TREE_OVERFLOW if the original constant was not. These conversions
8545 have implementation defined behavior and retaining the TREE_OVERFLOW
8546 flag here would confuse later passes such as VRP. */
8547 tree
8548 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8549 tree type, tree op0)
8551 tree res = fold_unary_loc (loc, code, type, op0);
8552 if (res
8553 && TREE_CODE (res) == INTEGER_CST
8554 && TREE_CODE (op0) == INTEGER_CST
8555 && CONVERT_EXPR_CODE_P (code))
8556 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8558 return res;
8561 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8562 operands OP0 and OP1. LOC is the location of the resulting expression.
8563 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8564 Return the folded expression if folding is successful. Otherwise,
8565 return NULL_TREE. */
8566 static tree
8567 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8568 tree arg0, tree arg1, tree op0, tree op1)
8570 tree tem;
8572 /* We only do these simplifications if we are optimizing. */
8573 if (!optimize)
8574 return NULL_TREE;
8576 /* Check for things like (A || B) && (A || C). We can convert this
8577 to A || (B && C). Note that either operator can be any of the four
8578 truth and/or operations and the transformation will still be
8579 valid. Also note that we only care about order for the
8580 ANDIF and ORIF operators. If B contains side effects, this
8581 might change the truth-value of A. */
8582 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8583 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8584 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8585 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8586 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8587 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8589 tree a00 = TREE_OPERAND (arg0, 0);
8590 tree a01 = TREE_OPERAND (arg0, 1);
8591 tree a10 = TREE_OPERAND (arg1, 0);
8592 tree a11 = TREE_OPERAND (arg1, 1);
8593 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8594 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8595 && (code == TRUTH_AND_EXPR
8596 || code == TRUTH_OR_EXPR));
8598 if (operand_equal_p (a00, a10, 0))
8599 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8600 fold_build2_loc (loc, code, type, a01, a11));
8601 else if (commutative && operand_equal_p (a00, a11, 0))
8602 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8603 fold_build2_loc (loc, code, type, a01, a10));
8604 else if (commutative && operand_equal_p (a01, a10, 0))
8605 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8606 fold_build2_loc (loc, code, type, a00, a11));
8608 /* This case if tricky because we must either have commutative
8609 operators or else A10 must not have side-effects. */
8611 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8612 && operand_equal_p (a01, a11, 0))
8613 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8614 fold_build2_loc (loc, code, type, a00, a10),
8615 a01);
8618 /* See if we can build a range comparison. */
8619 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8620 return tem;
8622 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8623 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8625 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8626 if (tem)
8627 return fold_build2_loc (loc, code, type, tem, arg1);
8630 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8631 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8633 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8634 if (tem)
8635 return fold_build2_loc (loc, code, type, arg0, tem);
8638 /* Check for the possibility of merging component references. If our
8639 lhs is another similar operation, try to merge its rhs with our
8640 rhs. Then try to merge our lhs and rhs. */
8641 if (TREE_CODE (arg0) == code
8642 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8643 TREE_OPERAND (arg0, 1), arg1)))
8644 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8646 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8647 return tem;
8649 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8650 && (code == TRUTH_AND_EXPR
8651 || code == TRUTH_ANDIF_EXPR
8652 || code == TRUTH_OR_EXPR
8653 || code == TRUTH_ORIF_EXPR))
8655 enum tree_code ncode, icode;
8657 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8658 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8659 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8661 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8662 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8663 We don't want to pack more than two leafs to a non-IF AND/OR
8664 expression.
8665 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8666 equal to IF-CODE, then we don't want to add right-hand operand.
8667 If the inner right-hand side of left-hand operand has
8668 side-effects, or isn't simple, then we can't add to it,
8669 as otherwise we might destroy if-sequence. */
8670 if (TREE_CODE (arg0) == icode
8671 && simple_operand_p_2 (arg1)
8672 /* Needed for sequence points to handle trappings, and
8673 side-effects. */
8674 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8676 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8677 arg1);
8678 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8679 tem);
8681 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8682 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8683 else if (TREE_CODE (arg1) == icode
8684 && simple_operand_p_2 (arg0)
8685 /* Needed for sequence points to handle trappings, and
8686 side-effects. */
8687 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8689 tem = fold_build2_loc (loc, ncode, type,
8690 arg0, TREE_OPERAND (arg1, 0));
8691 return fold_build2_loc (loc, icode, type, tem,
8692 TREE_OPERAND (arg1, 1));
8694 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8695 into (A OR B).
8696 For sequence point consistancy, we need to check for trapping,
8697 and side-effects. */
8698 else if (code == icode && simple_operand_p_2 (arg0)
8699 && simple_operand_p_2 (arg1))
8700 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8703 return NULL_TREE;
8706 /* Fold a binary expression of code CODE and type TYPE with operands
8707 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8708 Return the folded expression if folding is successful. Otherwise,
8709 return NULL_TREE. */
8711 static tree
8712 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8714 enum tree_code compl_code;
8716 if (code == MIN_EXPR)
8717 compl_code = MAX_EXPR;
8718 else if (code == MAX_EXPR)
8719 compl_code = MIN_EXPR;
8720 else
8721 gcc_unreachable ();
8723 /* MIN (MAX (a, b), b) == b. */
8724 if (TREE_CODE (op0) == compl_code
8725 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8726 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8728 /* MIN (MAX (b, a), b) == b. */
8729 if (TREE_CODE (op0) == compl_code
8730 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8731 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8732 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8734 /* MIN (a, MAX (a, b)) == a. */
8735 if (TREE_CODE (op1) == compl_code
8736 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8737 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8738 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8740 /* MIN (a, MAX (b, a)) == a. */
8741 if (TREE_CODE (op1) == compl_code
8742 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8743 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8744 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8746 return NULL_TREE;
8749 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8750 by changing CODE to reduce the magnitude of constants involved in
8751 ARG0 of the comparison.
8752 Returns a canonicalized comparison tree if a simplification was
8753 possible, otherwise returns NULL_TREE.
8754 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8755 valid if signed overflow is undefined. */
8757 static tree
8758 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8759 tree arg0, tree arg1,
8760 bool *strict_overflow_p)
8762 enum tree_code code0 = TREE_CODE (arg0);
8763 tree t, cst0 = NULL_TREE;
8764 int sgn0;
8765 bool swap = false;
8767 /* Match A +- CST code arg1 and CST code arg1. We can change the
8768 first form only if overflow is undefined. */
8769 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8770 /* In principle pointers also have undefined overflow behavior,
8771 but that causes problems elsewhere. */
8772 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8773 && (code0 == MINUS_EXPR
8774 || code0 == PLUS_EXPR)
8775 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8776 || code0 == INTEGER_CST))
8777 return NULL_TREE;
8779 /* Identify the constant in arg0 and its sign. */
8780 if (code0 == INTEGER_CST)
8781 cst0 = arg0;
8782 else
8783 cst0 = TREE_OPERAND (arg0, 1);
8784 sgn0 = tree_int_cst_sgn (cst0);
8786 /* Overflowed constants and zero will cause problems. */
8787 if (integer_zerop (cst0)
8788 || TREE_OVERFLOW (cst0))
8789 return NULL_TREE;
8791 /* See if we can reduce the magnitude of the constant in
8792 arg0 by changing the comparison code. */
8793 if (code0 == INTEGER_CST)
8795 /* CST <= arg1 -> CST-1 < arg1. */
8796 if (code == LE_EXPR && sgn0 == 1)
8797 code = LT_EXPR;
8798 /* -CST < arg1 -> -CST-1 <= arg1. */
8799 else if (code == LT_EXPR && sgn0 == -1)
8800 code = LE_EXPR;
8801 /* CST > arg1 -> CST-1 >= arg1. */
8802 else if (code == GT_EXPR && sgn0 == 1)
8803 code = GE_EXPR;
8804 /* -CST >= arg1 -> -CST-1 > arg1. */
8805 else if (code == GE_EXPR && sgn0 == -1)
8806 code = GT_EXPR;
8807 else
8808 return NULL_TREE;
8809 /* arg1 code' CST' might be more canonical. */
8810 swap = true;
8812 else
8814 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8815 if (code == LT_EXPR
8816 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8817 code = LE_EXPR;
8818 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8819 else if (code == GT_EXPR
8820 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8821 code = GE_EXPR;
8822 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8823 else if (code == LE_EXPR
8824 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8825 code = LT_EXPR;
8826 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8827 else if (code == GE_EXPR
8828 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8829 code = GT_EXPR;
8830 else
8831 return NULL_TREE;
8832 *strict_overflow_p = true;
8835 /* Now build the constant reduced in magnitude. But not if that
8836 would produce one outside of its types range. */
8837 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8838 && ((sgn0 == 1
8839 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8840 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8841 || (sgn0 == -1
8842 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8843 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8844 /* We cannot swap the comparison here as that would cause us to
8845 endlessly recurse. */
8846 return NULL_TREE;
8848 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8849 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8850 if (code0 != INTEGER_CST)
8851 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8852 t = fold_convert (TREE_TYPE (arg1), t);
8854 /* If swapping might yield to a more canonical form, do so. */
8855 if (swap)
8856 return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8857 else
8858 return fold_build2_loc (loc, code, type, t, arg1);
8861 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8862 overflow further. Try to decrease the magnitude of constants involved
8863 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8864 and put sole constants at the second argument position.
8865 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8867 static tree
8868 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8869 tree arg0, tree arg1)
8871 tree t;
8872 bool strict_overflow_p;
8873 const char * const warnmsg = G_("assuming signed overflow does not occur "
8874 "when reducing constant in comparison");
8876 /* Try canonicalization by simplifying arg0. */
8877 strict_overflow_p = false;
8878 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8879 &strict_overflow_p);
8880 if (t)
8882 if (strict_overflow_p)
8883 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8884 return t;
8887 /* Try canonicalization by simplifying arg1 using the swapped
8888 comparison. */
8889 code = swap_tree_comparison (code);
8890 strict_overflow_p = false;
8891 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8892 &strict_overflow_p);
8893 if (t && strict_overflow_p)
8894 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8895 return t;
8898 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8899 space. This is used to avoid issuing overflow warnings for
8900 expressions like &p->x which can not wrap. */
8902 static bool
8903 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8905 double_int di_offset, total;
8907 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8908 return true;
8910 if (bitpos < 0)
8911 return true;
8913 if (offset == NULL_TREE)
8914 di_offset = double_int_zero;
8915 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8916 return true;
8917 else
8918 di_offset = TREE_INT_CST (offset);
8920 bool overflow;
8921 double_int units = double_int::from_uhwi (bitpos / BITS_PER_UNIT);
8922 total = di_offset.add_with_sign (units, true, &overflow);
8923 if (overflow)
8924 return true;
8926 if (total.high != 0)
8927 return true;
8929 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8930 if (size <= 0)
8931 return true;
8933 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8934 array. */
8935 if (TREE_CODE (base) == ADDR_EXPR)
8937 HOST_WIDE_INT base_size;
8939 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8940 if (base_size > 0 && size < base_size)
8941 size = base_size;
8944 return total.low > (unsigned HOST_WIDE_INT) size;
8947 /* Subroutine of fold_binary. This routine performs all of the
8948 transformations that are common to the equality/inequality
8949 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8950 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8951 fold_binary should call fold_binary. Fold a comparison with
8952 tree code CODE and type TYPE with operands OP0 and OP1. Return
8953 the folded comparison or NULL_TREE. */
8955 static tree
8956 fold_comparison (location_t loc, enum tree_code code, tree type,
8957 tree op0, tree op1)
8959 tree arg0, arg1, tem;
8961 arg0 = op0;
8962 arg1 = op1;
8964 STRIP_SIGN_NOPS (arg0);
8965 STRIP_SIGN_NOPS (arg1);
8967 tem = fold_relational_const (code, type, arg0, arg1);
8968 if (tem != NULL_TREE)
8969 return tem;
8971 /* If one arg is a real or integer constant, put it last. */
8972 if (tree_swap_operands_p (arg0, arg1, true))
8973 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8975 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8976 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8977 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8978 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8979 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8980 && (TREE_CODE (arg1) == INTEGER_CST
8981 && !TREE_OVERFLOW (arg1)))
8983 tree const1 = TREE_OPERAND (arg0, 1);
8984 tree const2 = arg1;
8985 tree variable = TREE_OPERAND (arg0, 0);
8986 tree lhs;
8987 int lhs_add;
8988 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8990 lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
8991 TREE_TYPE (arg1), const2, const1);
8993 /* If the constant operation overflowed this can be
8994 simplified as a comparison against INT_MAX/INT_MIN. */
8995 if (TREE_CODE (lhs) == INTEGER_CST
8996 && TREE_OVERFLOW (lhs))
8998 int const1_sgn = tree_int_cst_sgn (const1);
8999 enum tree_code code2 = code;
9001 /* Get the sign of the constant on the lhs if the
9002 operation were VARIABLE + CONST1. */
9003 if (TREE_CODE (arg0) == MINUS_EXPR)
9004 const1_sgn = -const1_sgn;
9006 /* The sign of the constant determines if we overflowed
9007 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
9008 Canonicalize to the INT_MIN overflow by swapping the comparison
9009 if necessary. */
9010 if (const1_sgn == -1)
9011 code2 = swap_tree_comparison (code);
9013 /* We now can look at the canonicalized case
9014 VARIABLE + 1 CODE2 INT_MIN
9015 and decide on the result. */
9016 if (code2 == LT_EXPR
9017 || code2 == LE_EXPR
9018 || code2 == EQ_EXPR)
9019 return omit_one_operand_loc (loc, type, boolean_false_node, variable);
9020 else if (code2 == NE_EXPR
9021 || code2 == GE_EXPR
9022 || code2 == GT_EXPR)
9023 return omit_one_operand_loc (loc, type, boolean_true_node, variable);
9026 if (TREE_CODE (lhs) == TREE_CODE (arg1)
9027 && (TREE_CODE (lhs) != INTEGER_CST
9028 || !TREE_OVERFLOW (lhs)))
9030 if (code != EQ_EXPR && code != NE_EXPR)
9031 fold_overflow_warning ("assuming signed overflow does not occur "
9032 "when changing X +- C1 cmp C2 to "
9033 "X cmp C1 +- C2",
9034 WARN_STRICT_OVERFLOW_COMPARISON);
9035 return fold_build2_loc (loc, code, type, variable, lhs);
9039 /* For comparisons of pointers we can decompose it to a compile time
9040 comparison of the base objects and the offsets into the object.
9041 This requires at least one operand being an ADDR_EXPR or a
9042 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
9043 if (POINTER_TYPE_P (TREE_TYPE (arg0))
9044 && (TREE_CODE (arg0) == ADDR_EXPR
9045 || TREE_CODE (arg1) == ADDR_EXPR
9046 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9047 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9049 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9050 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
9051 enum machine_mode mode;
9052 int volatilep, unsignedp;
9053 bool indirect_base0 = false, indirect_base1 = false;
9055 /* Get base and offset for the access. Strip ADDR_EXPR for
9056 get_inner_reference, but put it back by stripping INDIRECT_REF
9057 off the base object if possible. indirect_baseN will be true
9058 if baseN is not an address but refers to the object itself. */
9059 base0 = arg0;
9060 if (TREE_CODE (arg0) == ADDR_EXPR)
9062 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
9063 &bitsize, &bitpos0, &offset0, &mode,
9064 &unsignedp, &volatilep, false);
9065 if (TREE_CODE (base0) == INDIRECT_REF)
9066 base0 = TREE_OPERAND (base0, 0);
9067 else
9068 indirect_base0 = true;
9070 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9072 base0 = TREE_OPERAND (arg0, 0);
9073 STRIP_SIGN_NOPS (base0);
9074 if (TREE_CODE (base0) == ADDR_EXPR)
9076 base0 = TREE_OPERAND (base0, 0);
9077 indirect_base0 = true;
9079 offset0 = TREE_OPERAND (arg0, 1);
9080 if (host_integerp (offset0, 0))
9082 HOST_WIDE_INT off = size_low_cst (offset0);
9083 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
9084 * BITS_PER_UNIT)
9085 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
9087 bitpos0 = off * BITS_PER_UNIT;
9088 offset0 = NULL_TREE;
9093 base1 = arg1;
9094 if (TREE_CODE (arg1) == ADDR_EXPR)
9096 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
9097 &bitsize, &bitpos1, &offset1, &mode,
9098 &unsignedp, &volatilep, false);
9099 if (TREE_CODE (base1) == INDIRECT_REF)
9100 base1 = TREE_OPERAND (base1, 0);
9101 else
9102 indirect_base1 = true;
9104 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9106 base1 = TREE_OPERAND (arg1, 0);
9107 STRIP_SIGN_NOPS (base1);
9108 if (TREE_CODE (base1) == ADDR_EXPR)
9110 base1 = TREE_OPERAND (base1, 0);
9111 indirect_base1 = true;
9113 offset1 = TREE_OPERAND (arg1, 1);
9114 if (host_integerp (offset1, 0))
9116 HOST_WIDE_INT off = size_low_cst (offset1);
9117 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
9118 * BITS_PER_UNIT)
9119 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
9121 bitpos1 = off * BITS_PER_UNIT;
9122 offset1 = NULL_TREE;
9127 /* A local variable can never be pointed to by
9128 the default SSA name of an incoming parameter. */
9129 if ((TREE_CODE (arg0) == ADDR_EXPR
9130 && indirect_base0
9131 && TREE_CODE (base0) == VAR_DECL
9132 && auto_var_in_fn_p (base0, current_function_decl)
9133 && !indirect_base1
9134 && TREE_CODE (base1) == SSA_NAME
9135 && SSA_NAME_IS_DEFAULT_DEF (base1)
9136 && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL)
9137 || (TREE_CODE (arg1) == ADDR_EXPR
9138 && indirect_base1
9139 && TREE_CODE (base1) == VAR_DECL
9140 && auto_var_in_fn_p (base1, current_function_decl)
9141 && !indirect_base0
9142 && TREE_CODE (base0) == SSA_NAME
9143 && SSA_NAME_IS_DEFAULT_DEF (base0)
9144 && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL))
9146 if (code == NE_EXPR)
9147 return constant_boolean_node (1, type);
9148 else if (code == EQ_EXPR)
9149 return constant_boolean_node (0, type);
9151 /* If we have equivalent bases we might be able to simplify. */
9152 else if (indirect_base0 == indirect_base1
9153 && operand_equal_p (base0, base1, 0))
9155 /* We can fold this expression to a constant if the non-constant
9156 offset parts are equal. */
9157 if ((offset0 == offset1
9158 || (offset0 && offset1
9159 && operand_equal_p (offset0, offset1, 0)))
9160 && (code == EQ_EXPR
9161 || code == NE_EXPR
9162 || (indirect_base0 && DECL_P (base0))
9163 || POINTER_TYPE_OVERFLOW_UNDEFINED))
9166 if (code != EQ_EXPR
9167 && code != NE_EXPR
9168 && bitpos0 != bitpos1
9169 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9170 || pointer_may_wrap_p (base1, offset1, bitpos1)))
9171 fold_overflow_warning (("assuming pointer wraparound does not "
9172 "occur when comparing P +- C1 with "
9173 "P +- C2"),
9174 WARN_STRICT_OVERFLOW_CONDITIONAL);
9176 switch (code)
9178 case EQ_EXPR:
9179 return constant_boolean_node (bitpos0 == bitpos1, type);
9180 case NE_EXPR:
9181 return constant_boolean_node (bitpos0 != bitpos1, type);
9182 case LT_EXPR:
9183 return constant_boolean_node (bitpos0 < bitpos1, type);
9184 case LE_EXPR:
9185 return constant_boolean_node (bitpos0 <= bitpos1, type);
9186 case GE_EXPR:
9187 return constant_boolean_node (bitpos0 >= bitpos1, type);
9188 case GT_EXPR:
9189 return constant_boolean_node (bitpos0 > bitpos1, type);
9190 default:;
9193 /* We can simplify the comparison to a comparison of the variable
9194 offset parts if the constant offset parts are equal.
9195 Be careful to use signed sizetype here because otherwise we
9196 mess with array offsets in the wrong way. This is possible
9197 because pointer arithmetic is restricted to retain within an
9198 object and overflow on pointer differences is undefined as of
9199 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
9200 else if (bitpos0 == bitpos1
9201 && ((code == EQ_EXPR || code == NE_EXPR)
9202 || (indirect_base0 && DECL_P (base0))
9203 || POINTER_TYPE_OVERFLOW_UNDEFINED))
9205 /* By converting to signed sizetype we cover middle-end pointer
9206 arithmetic which operates on unsigned pointer types of size
9207 type size and ARRAY_REF offsets which are properly sign or
9208 zero extended from their type in case it is narrower than
9209 sizetype. */
9210 if (offset0 == NULL_TREE)
9211 offset0 = build_int_cst (ssizetype, 0);
9212 else
9213 offset0 = fold_convert_loc (loc, ssizetype, offset0);
9214 if (offset1 == NULL_TREE)
9215 offset1 = build_int_cst (ssizetype, 0);
9216 else
9217 offset1 = fold_convert_loc (loc, ssizetype, offset1);
9219 if (code != EQ_EXPR
9220 && code != NE_EXPR
9221 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9222 || pointer_may_wrap_p (base1, offset1, bitpos1)))
9223 fold_overflow_warning (("assuming pointer wraparound does not "
9224 "occur when comparing P +- C1 with "
9225 "P +- C2"),
9226 WARN_STRICT_OVERFLOW_COMPARISON);
9228 return fold_build2_loc (loc, code, type, offset0, offset1);
9231 /* For non-equal bases we can simplify if they are addresses
9232 of local binding decls or constants. */
9233 else if (indirect_base0 && indirect_base1
9234 /* We know that !operand_equal_p (base0, base1, 0)
9235 because the if condition was false. But make
9236 sure two decls are not the same. */
9237 && base0 != base1
9238 && TREE_CODE (arg0) == ADDR_EXPR
9239 && TREE_CODE (arg1) == ADDR_EXPR
9240 && (((TREE_CODE (base0) == VAR_DECL
9241 || TREE_CODE (base0) == PARM_DECL)
9242 && (targetm.binds_local_p (base0)
9243 || CONSTANT_CLASS_P (base1)))
9244 || CONSTANT_CLASS_P (base0))
9245 && (((TREE_CODE (base1) == VAR_DECL
9246 || TREE_CODE (base1) == PARM_DECL)
9247 && (targetm.binds_local_p (base1)
9248 || CONSTANT_CLASS_P (base0)))
9249 || CONSTANT_CLASS_P (base1)))
9251 if (code == EQ_EXPR)
9252 return omit_two_operands_loc (loc, type, boolean_false_node,
9253 arg0, arg1);
9254 else if (code == NE_EXPR)
9255 return omit_two_operands_loc (loc, type, boolean_true_node,
9256 arg0, arg1);
9258 /* For equal offsets we can simplify to a comparison of the
9259 base addresses. */
9260 else if (bitpos0 == bitpos1
9261 && (indirect_base0
9262 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9263 && (indirect_base1
9264 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9265 && ((offset0 == offset1)
9266 || (offset0 && offset1
9267 && operand_equal_p (offset0, offset1, 0))))
9269 if (indirect_base0)
9270 base0 = build_fold_addr_expr_loc (loc, base0);
9271 if (indirect_base1)
9272 base1 = build_fold_addr_expr_loc (loc, base1);
9273 return fold_build2_loc (loc, code, type, base0, base1);
9277 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9278 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
9279 the resulting offset is smaller in absolute value than the
9280 original one. */
9281 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9282 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9283 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9284 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9285 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9286 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9287 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9289 tree const1 = TREE_OPERAND (arg0, 1);
9290 tree const2 = TREE_OPERAND (arg1, 1);
9291 tree variable1 = TREE_OPERAND (arg0, 0);
9292 tree variable2 = TREE_OPERAND (arg1, 0);
9293 tree cst;
9294 const char * const warnmsg = G_("assuming signed overflow does not "
9295 "occur when combining constants around "
9296 "a comparison");
9298 /* Put the constant on the side where it doesn't overflow and is
9299 of lower absolute value than before. */
9300 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9301 ? MINUS_EXPR : PLUS_EXPR,
9302 const2, const1);
9303 if (!TREE_OVERFLOW (cst)
9304 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9306 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9307 return fold_build2_loc (loc, code, type,
9308 variable1,
9309 fold_build2_loc (loc,
9310 TREE_CODE (arg1), TREE_TYPE (arg1),
9311 variable2, cst));
9314 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9315 ? MINUS_EXPR : PLUS_EXPR,
9316 const1, const2);
9317 if (!TREE_OVERFLOW (cst)
9318 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9320 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9321 return fold_build2_loc (loc, code, type,
9322 fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
9323 variable1, cst),
9324 variable2);
9328 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9329 signed arithmetic case. That form is created by the compiler
9330 often enough for folding it to be of value. One example is in
9331 computing loop trip counts after Operator Strength Reduction. */
9332 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9333 && TREE_CODE (arg0) == MULT_EXPR
9334 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9335 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9336 && integer_zerop (arg1))
9338 tree const1 = TREE_OPERAND (arg0, 1);
9339 tree const2 = arg1; /* zero */
9340 tree variable1 = TREE_OPERAND (arg0, 0);
9341 enum tree_code cmp_code = code;
9343 /* Handle unfolded multiplication by zero. */
9344 if (integer_zerop (const1))
9345 return fold_build2_loc (loc, cmp_code, type, const1, const2);
9347 fold_overflow_warning (("assuming signed overflow does not occur when "
9348 "eliminating multiplication in comparison "
9349 "with zero"),
9350 WARN_STRICT_OVERFLOW_COMPARISON);
9352 /* If const1 is negative we swap the sense of the comparison. */
9353 if (tree_int_cst_sgn (const1) < 0)
9354 cmp_code = swap_tree_comparison (cmp_code);
9356 return fold_build2_loc (loc, cmp_code, type, variable1, const2);
9359 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
9360 if (tem)
9361 return tem;
9363 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9365 tree targ0 = strip_float_extensions (arg0);
9366 tree targ1 = strip_float_extensions (arg1);
9367 tree newtype = TREE_TYPE (targ0);
9369 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9370 newtype = TREE_TYPE (targ1);
9372 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
9373 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9374 return fold_build2_loc (loc, code, type,
9375 fold_convert_loc (loc, newtype, targ0),
9376 fold_convert_loc (loc, newtype, targ1));
9378 /* (-a) CMP (-b) -> b CMP a */
9379 if (TREE_CODE (arg0) == NEGATE_EXPR
9380 && TREE_CODE (arg1) == NEGATE_EXPR)
9381 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
9382 TREE_OPERAND (arg0, 0));
9384 if (TREE_CODE (arg1) == REAL_CST)
9386 REAL_VALUE_TYPE cst;
9387 cst = TREE_REAL_CST (arg1);
9389 /* (-a) CMP CST -> a swap(CMP) (-CST) */
9390 if (TREE_CODE (arg0) == NEGATE_EXPR)
9391 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9392 TREE_OPERAND (arg0, 0),
9393 build_real (TREE_TYPE (arg1),
9394 real_value_negate (&cst)));
9396 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
9397 /* a CMP (-0) -> a CMP 0 */
9398 if (REAL_VALUE_MINUS_ZERO (cst))
9399 return fold_build2_loc (loc, code, type, arg0,
9400 build_real (TREE_TYPE (arg1), dconst0));
9402 /* x != NaN is always true, other ops are always false. */
9403 if (REAL_VALUE_ISNAN (cst)
9404 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9406 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9407 return omit_one_operand_loc (loc, type, tem, arg0);
9410 /* Fold comparisons against infinity. */
9411 if (REAL_VALUE_ISINF (cst)
9412 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9414 tem = fold_inf_compare (loc, code, type, arg0, arg1);
9415 if (tem != NULL_TREE)
9416 return tem;
9420 /* If this is a comparison of a real constant with a PLUS_EXPR
9421 or a MINUS_EXPR of a real constant, we can convert it into a
9422 comparison with a revised real constant as long as no overflow
9423 occurs when unsafe_math_optimizations are enabled. */
9424 if (flag_unsafe_math_optimizations
9425 && TREE_CODE (arg1) == REAL_CST
9426 && (TREE_CODE (arg0) == PLUS_EXPR
9427 || TREE_CODE (arg0) == MINUS_EXPR)
9428 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9429 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9430 ? MINUS_EXPR : PLUS_EXPR,
9431 arg1, TREE_OPERAND (arg0, 1)))
9432 && !TREE_OVERFLOW (tem))
9433 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9435 /* Likewise, we can simplify a comparison of a real constant with
9436 a MINUS_EXPR whose first operand is also a real constant, i.e.
9437 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
9438 floating-point types only if -fassociative-math is set. */
9439 if (flag_associative_math
9440 && TREE_CODE (arg1) == REAL_CST
9441 && TREE_CODE (arg0) == MINUS_EXPR
9442 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9443 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9444 arg1))
9445 && !TREE_OVERFLOW (tem))
9446 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9447 TREE_OPERAND (arg0, 1), tem);
9449 /* Fold comparisons against built-in math functions. */
9450 if (TREE_CODE (arg1) == REAL_CST
9451 && flag_unsafe_math_optimizations
9452 && ! flag_errno_math)
9454 enum built_in_function fcode = builtin_mathfn_code (arg0);
9456 if (fcode != END_BUILTINS)
9458 tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9459 if (tem != NULL_TREE)
9460 return tem;
9465 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9466 && CONVERT_EXPR_P (arg0))
9468 /* If we are widening one operand of an integer comparison,
9469 see if the other operand is similarly being widened. Perhaps we
9470 can do the comparison in the narrower type. */
9471 tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9472 if (tem)
9473 return tem;
9475 /* Or if we are changing signedness. */
9476 tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9477 if (tem)
9478 return tem;
9481 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9482 constant, we can simplify it. */
9483 if (TREE_CODE (arg1) == INTEGER_CST
9484 && (TREE_CODE (arg0) == MIN_EXPR
9485 || TREE_CODE (arg0) == MAX_EXPR)
9486 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9488 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9489 if (tem)
9490 return tem;
9493 /* Simplify comparison of something with itself. (For IEEE
9494 floating-point, we can only do some of these simplifications.) */
9495 if (operand_equal_p (arg0, arg1, 0))
9497 switch (code)
9499 case EQ_EXPR:
9500 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9501 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9502 return constant_boolean_node (1, type);
9503 break;
9505 case GE_EXPR:
9506 case LE_EXPR:
9507 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9508 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9509 return constant_boolean_node (1, type);
9510 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9512 case NE_EXPR:
9513 /* For NE, we can only do this simplification if integer
9514 or we don't honor IEEE floating point NaNs. */
9515 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9516 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9517 break;
9518 /* ... fall through ... */
9519 case GT_EXPR:
9520 case LT_EXPR:
9521 return constant_boolean_node (0, type);
9522 default:
9523 gcc_unreachable ();
9527 /* If we are comparing an expression that just has comparisons
9528 of two integer values, arithmetic expressions of those comparisons,
9529 and constants, we can simplify it. There are only three cases
9530 to check: the two values can either be equal, the first can be
9531 greater, or the second can be greater. Fold the expression for
9532 those three values. Since each value must be 0 or 1, we have
9533 eight possibilities, each of which corresponds to the constant 0
9534 or 1 or one of the six possible comparisons.
9536 This handles common cases like (a > b) == 0 but also handles
9537 expressions like ((x > y) - (y > x)) > 0, which supposedly
9538 occur in macroized code. */
9540 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9542 tree cval1 = 0, cval2 = 0;
9543 int save_p = 0;
9545 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9546 /* Don't handle degenerate cases here; they should already
9547 have been handled anyway. */
9548 && cval1 != 0 && cval2 != 0
9549 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9550 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9551 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9552 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9553 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9554 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9555 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9557 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9558 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9560 /* We can't just pass T to eval_subst in case cval1 or cval2
9561 was the same as ARG1. */
9563 tree high_result
9564 = fold_build2_loc (loc, code, type,
9565 eval_subst (loc, arg0, cval1, maxval,
9566 cval2, minval),
9567 arg1);
9568 tree equal_result
9569 = fold_build2_loc (loc, code, type,
9570 eval_subst (loc, arg0, cval1, maxval,
9571 cval2, maxval),
9572 arg1);
9573 tree low_result
9574 = fold_build2_loc (loc, code, type,
9575 eval_subst (loc, arg0, cval1, minval,
9576 cval2, maxval),
9577 arg1);
9579 /* All three of these results should be 0 or 1. Confirm they are.
9580 Then use those values to select the proper code to use. */
9582 if (TREE_CODE (high_result) == INTEGER_CST
9583 && TREE_CODE (equal_result) == INTEGER_CST
9584 && TREE_CODE (low_result) == INTEGER_CST)
9586 /* Make a 3-bit mask with the high-order bit being the
9587 value for `>', the next for '=', and the low for '<'. */
9588 switch ((integer_onep (high_result) * 4)
9589 + (integer_onep (equal_result) * 2)
9590 + integer_onep (low_result))
9592 case 0:
9593 /* Always false. */
9594 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9595 case 1:
9596 code = LT_EXPR;
9597 break;
9598 case 2:
9599 code = EQ_EXPR;
9600 break;
9601 case 3:
9602 code = LE_EXPR;
9603 break;
9604 case 4:
9605 code = GT_EXPR;
9606 break;
9607 case 5:
9608 code = NE_EXPR;
9609 break;
9610 case 6:
9611 code = GE_EXPR;
9612 break;
9613 case 7:
9614 /* Always true. */
9615 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9618 if (save_p)
9620 tem = save_expr (build2 (code, type, cval1, cval2));
9621 SET_EXPR_LOCATION (tem, loc);
9622 return tem;
9624 return fold_build2_loc (loc, code, type, cval1, cval2);
9629 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9630 into a single range test. */
9631 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9632 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9633 && TREE_CODE (arg1) == INTEGER_CST
9634 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9635 && !integer_zerop (TREE_OPERAND (arg0, 1))
9636 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9637 && !TREE_OVERFLOW (arg1))
9639 tem = fold_div_compare (loc, code, type, arg0, arg1);
9640 if (tem != NULL_TREE)
9641 return tem;
9644 /* Fold ~X op ~Y as Y op X. */
9645 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9646 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9648 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9649 return fold_build2_loc (loc, code, type,
9650 fold_convert_loc (loc, cmp_type,
9651 TREE_OPERAND (arg1, 0)),
9652 TREE_OPERAND (arg0, 0));
9655 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9656 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9657 && (TREE_CODE (arg1) == INTEGER_CST || TREE_CODE (arg1) == VECTOR_CST))
9659 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9660 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9661 TREE_OPERAND (arg0, 0),
9662 fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9663 fold_convert_loc (loc, cmp_type, arg1)));
9666 return NULL_TREE;
9670 /* Subroutine of fold_binary. Optimize complex multiplications of the
9671 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9672 argument EXPR represents the expression "z" of type TYPE. */
9674 static tree
9675 fold_mult_zconjz (location_t loc, tree type, tree expr)
9677 tree itype = TREE_TYPE (type);
9678 tree rpart, ipart, tem;
9680 if (TREE_CODE (expr) == COMPLEX_EXPR)
9682 rpart = TREE_OPERAND (expr, 0);
9683 ipart = TREE_OPERAND (expr, 1);
9685 else if (TREE_CODE (expr) == COMPLEX_CST)
9687 rpart = TREE_REALPART (expr);
9688 ipart = TREE_IMAGPART (expr);
9690 else
9692 expr = save_expr (expr);
9693 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9694 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9697 rpart = save_expr (rpart);
9698 ipart = save_expr (ipart);
9699 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9700 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9701 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9702 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9703 build_zero_cst (itype));
9707 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9708 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9709 guarantees that P and N have the same least significant log2(M) bits.
9710 N is not otherwise constrained. In particular, N is not normalized to
9711 0 <= N < M as is common. In general, the precise value of P is unknown.
9712 M is chosen as large as possible such that constant N can be determined.
9714 Returns M and sets *RESIDUE to N.
9716 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9717 account. This is not always possible due to PR 35705.
9720 static unsigned HOST_WIDE_INT
9721 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9722 bool allow_func_align)
9724 enum tree_code code;
9726 *residue = 0;
9728 code = TREE_CODE (expr);
9729 if (code == ADDR_EXPR)
9731 unsigned int bitalign;
9732 get_object_alignment_1 (TREE_OPERAND (expr, 0), &bitalign, residue);
9733 *residue /= BITS_PER_UNIT;
9734 return bitalign / BITS_PER_UNIT;
9736 else if (code == POINTER_PLUS_EXPR)
9738 tree op0, op1;
9739 unsigned HOST_WIDE_INT modulus;
9740 enum tree_code inner_code;
9742 op0 = TREE_OPERAND (expr, 0);
9743 STRIP_NOPS (op0);
9744 modulus = get_pointer_modulus_and_residue (op0, residue,
9745 allow_func_align);
9747 op1 = TREE_OPERAND (expr, 1);
9748 STRIP_NOPS (op1);
9749 inner_code = TREE_CODE (op1);
9750 if (inner_code == INTEGER_CST)
9752 *residue += TREE_INT_CST_LOW (op1);
9753 return modulus;
9755 else if (inner_code == MULT_EXPR)
9757 op1 = TREE_OPERAND (op1, 1);
9758 if (TREE_CODE (op1) == INTEGER_CST)
9760 unsigned HOST_WIDE_INT align;
9762 /* Compute the greatest power-of-2 divisor of op1. */
9763 align = TREE_INT_CST_LOW (op1);
9764 align &= -align;
9766 /* If align is non-zero and less than *modulus, replace
9767 *modulus with align., If align is 0, then either op1 is 0
9768 or the greatest power-of-2 divisor of op1 doesn't fit in an
9769 unsigned HOST_WIDE_INT. In either case, no additional
9770 constraint is imposed. */
9771 if (align)
9772 modulus = MIN (modulus, align);
9774 return modulus;
9779 /* If we get here, we were unable to determine anything useful about the
9780 expression. */
9781 return 1;
9784 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
9785 CONSTRUCTOR ARG into array ELTS and return true if successful. */
9787 static bool
9788 vec_cst_ctor_to_array (tree arg, tree *elts)
9790 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
9792 if (TREE_CODE (arg) == VECTOR_CST)
9794 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
9795 elts[i] = VECTOR_CST_ELT (arg, i);
9797 else if (TREE_CODE (arg) == CONSTRUCTOR)
9799 constructor_elt *elt;
9801 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
9802 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
9803 return false;
9804 else
9805 elts[i] = elt->value;
9807 else
9808 return false;
9809 for (; i < nelts; i++)
9810 elts[i]
9811 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
9812 return true;
9815 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
9816 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
9817 NULL_TREE otherwise. */
9819 static tree
9820 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
9822 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
9823 tree *elts;
9824 bool need_ctor = false;
9826 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
9827 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
9828 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
9829 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
9830 return NULL_TREE;
9832 elts = XALLOCAVEC (tree, nelts * 3);
9833 if (!vec_cst_ctor_to_array (arg0, elts)
9834 || !vec_cst_ctor_to_array (arg1, elts + nelts))
9835 return NULL_TREE;
9837 for (i = 0; i < nelts; i++)
9839 if (!CONSTANT_CLASS_P (elts[sel[i]]))
9840 need_ctor = true;
9841 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
9844 if (need_ctor)
9846 vec<constructor_elt, va_gc> *v;
9847 vec_alloc (v, nelts);
9848 for (i = 0; i < nelts; i++)
9849 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
9850 return build_constructor (type, v);
9852 else
9853 return build_vector (type, &elts[2 * nelts]);
9856 /* Try to fold a pointer difference of type TYPE two address expressions of
9857 array references AREF0 and AREF1 using location LOC. Return a
9858 simplified expression for the difference or NULL_TREE. */
9860 static tree
9861 fold_addr_of_array_ref_difference (location_t loc, tree type,
9862 tree aref0, tree aref1)
9864 tree base0 = TREE_OPERAND (aref0, 0);
9865 tree base1 = TREE_OPERAND (aref1, 0);
9866 tree base_offset = build_int_cst (type, 0);
9868 /* If the bases are array references as well, recurse. If the bases
9869 are pointer indirections compute the difference of the pointers.
9870 If the bases are equal, we are set. */
9871 if ((TREE_CODE (base0) == ARRAY_REF
9872 && TREE_CODE (base1) == ARRAY_REF
9873 && (base_offset
9874 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
9875 || (INDIRECT_REF_P (base0)
9876 && INDIRECT_REF_P (base1)
9877 && (base_offset = fold_binary_loc (loc, MINUS_EXPR, type,
9878 TREE_OPERAND (base0, 0),
9879 TREE_OPERAND (base1, 0))))
9880 || operand_equal_p (base0, base1, 0))
9882 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9883 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9884 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9885 tree diff = build2 (MINUS_EXPR, type, op0, op1);
9886 return fold_build2_loc (loc, PLUS_EXPR, type,
9887 base_offset,
9888 fold_build2_loc (loc, MULT_EXPR, type,
9889 diff, esz));
9891 return NULL_TREE;
9894 /* If the real or vector real constant CST of type TYPE has an exact
9895 inverse, return it, else return NULL. */
9897 static tree
9898 exact_inverse (tree type, tree cst)
9900 REAL_VALUE_TYPE r;
9901 tree unit_type, *elts;
9902 enum machine_mode mode;
9903 unsigned vec_nelts, i;
9905 switch (TREE_CODE (cst))
9907 case REAL_CST:
9908 r = TREE_REAL_CST (cst);
9910 if (exact_real_inverse (TYPE_MODE (type), &r))
9911 return build_real (type, r);
9913 return NULL_TREE;
9915 case VECTOR_CST:
9916 vec_nelts = VECTOR_CST_NELTS (cst);
9917 elts = XALLOCAVEC (tree, vec_nelts);
9918 unit_type = TREE_TYPE (type);
9919 mode = TYPE_MODE (unit_type);
9921 for (i = 0; i < vec_nelts; i++)
9923 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9924 if (!exact_real_inverse (mode, &r))
9925 return NULL_TREE;
9926 elts[i] = build_real (unit_type, r);
9929 return build_vector (type, elts);
9931 default:
9932 return NULL_TREE;
9936 /* Mask out the tz least significant bits of X of type TYPE where
9937 tz is the number of trailing zeroes in Y. */
9938 static double_int
9939 mask_with_tz (tree type, double_int x, double_int y)
9941 int tz = y.trailing_zeros ();
9943 if (tz > 0)
9945 double_int mask;
9947 mask = ~double_int::mask (tz);
9948 mask = mask.ext (TYPE_PRECISION (type), TYPE_UNSIGNED (type));
9949 return mask & x;
9951 return x;
9954 /* Fold a binary expression of code CODE and type TYPE with operands
9955 OP0 and OP1. LOC is the location of the resulting expression.
9956 Return the folded expression if folding is successful. Otherwise,
9957 return NULL_TREE. */
9959 tree
9960 fold_binary_loc (location_t loc,
9961 enum tree_code code, tree type, tree op0, tree op1)
9963 enum tree_code_class kind = TREE_CODE_CLASS (code);
9964 tree arg0, arg1, tem;
9965 tree t1 = NULL_TREE;
9966 bool strict_overflow_p;
9967 unsigned int prec;
9969 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9970 && TREE_CODE_LENGTH (code) == 2
9971 && op0 != NULL_TREE
9972 && op1 != NULL_TREE);
9974 arg0 = op0;
9975 arg1 = op1;
9977 /* Strip any conversions that don't change the mode. This is
9978 safe for every expression, except for a comparison expression
9979 because its signedness is derived from its operands. So, in
9980 the latter case, only strip conversions that don't change the
9981 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9982 preserved.
9984 Note that this is done as an internal manipulation within the
9985 constant folder, in order to find the simplest representation
9986 of the arguments so that their form can be studied. In any
9987 cases, the appropriate type conversions should be put back in
9988 the tree that will get out of the constant folder. */
9990 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9992 STRIP_SIGN_NOPS (arg0);
9993 STRIP_SIGN_NOPS (arg1);
9995 else
9997 STRIP_NOPS (arg0);
9998 STRIP_NOPS (arg1);
10001 /* Note that TREE_CONSTANT isn't enough: static var addresses are
10002 constant but we can't do arithmetic on them. */
10003 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10004 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
10005 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
10006 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
10007 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
10008 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST)
10009 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == INTEGER_CST))
10011 if (kind == tcc_binary)
10013 /* Make sure type and arg0 have the same saturating flag. */
10014 gcc_assert (TYPE_SATURATING (type)
10015 == TYPE_SATURATING (TREE_TYPE (arg0)));
10016 tem = const_binop (code, arg0, arg1);
10018 else if (kind == tcc_comparison)
10019 tem = fold_relational_const (code, type, arg0, arg1);
10020 else
10021 tem = NULL_TREE;
10023 if (tem != NULL_TREE)
10025 if (TREE_TYPE (tem) != type)
10026 tem = fold_convert_loc (loc, type, tem);
10027 return tem;
10031 /* If this is a commutative operation, and ARG0 is a constant, move it
10032 to ARG1 to reduce the number of tests below. */
10033 if (commutative_tree_code (code)
10034 && tree_swap_operands_p (arg0, arg1, true))
10035 return fold_build2_loc (loc, code, type, op1, op0);
10037 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10039 First check for cases where an arithmetic operation is applied to a
10040 compound, conditional, or comparison operation. Push the arithmetic
10041 operation inside the compound or conditional to see if any folding
10042 can then be done. Convert comparison to conditional for this purpose.
10043 The also optimizes non-constant cases that used to be done in
10044 expand_expr.
10046 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10047 one of the operands is a comparison and the other is a comparison, a
10048 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10049 code below would make the expression more complex. Change it to a
10050 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10051 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10053 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10054 || code == EQ_EXPR || code == NE_EXPR)
10055 && TREE_CODE (type) != VECTOR_TYPE
10056 && ((truth_value_p (TREE_CODE (arg0))
10057 && (truth_value_p (TREE_CODE (arg1))
10058 || (TREE_CODE (arg1) == BIT_AND_EXPR
10059 && integer_onep (TREE_OPERAND (arg1, 1)))))
10060 || (truth_value_p (TREE_CODE (arg1))
10061 && (truth_value_p (TREE_CODE (arg0))
10062 || (TREE_CODE (arg0) == BIT_AND_EXPR
10063 && integer_onep (TREE_OPERAND (arg0, 1)))))))
10065 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10066 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
10067 : TRUTH_XOR_EXPR,
10068 boolean_type_node,
10069 fold_convert_loc (loc, boolean_type_node, arg0),
10070 fold_convert_loc (loc, boolean_type_node, arg1));
10072 if (code == EQ_EXPR)
10073 tem = invert_truthvalue_loc (loc, tem);
10075 return fold_convert_loc (loc, type, tem);
10078 if (TREE_CODE_CLASS (code) == tcc_binary
10079 || TREE_CODE_CLASS (code) == tcc_comparison)
10081 if (TREE_CODE (arg0) == COMPOUND_EXPR)
10083 tem = fold_build2_loc (loc, code, type,
10084 fold_convert_loc (loc, TREE_TYPE (op0),
10085 TREE_OPERAND (arg0, 1)), op1);
10086 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
10087 tem);
10089 if (TREE_CODE (arg1) == COMPOUND_EXPR
10090 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10092 tem = fold_build2_loc (loc, code, type, op0,
10093 fold_convert_loc (loc, TREE_TYPE (op1),
10094 TREE_OPERAND (arg1, 1)));
10095 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
10096 tem);
10099 if (TREE_CODE (arg0) == COND_EXPR
10100 || TREE_CODE (arg0) == VEC_COND_EXPR
10101 || COMPARISON_CLASS_P (arg0))
10103 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10104 arg0, arg1,
10105 /*cond_first_p=*/1);
10106 if (tem != NULL_TREE)
10107 return tem;
10110 if (TREE_CODE (arg1) == COND_EXPR
10111 || TREE_CODE (arg1) == VEC_COND_EXPR
10112 || COMPARISON_CLASS_P (arg1))
10114 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10115 arg1, arg0,
10116 /*cond_first_p=*/0);
10117 if (tem != NULL_TREE)
10118 return tem;
10122 switch (code)
10124 case MEM_REF:
10125 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
10126 if (TREE_CODE (arg0) == ADDR_EXPR
10127 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
10129 tree iref = TREE_OPERAND (arg0, 0);
10130 return fold_build2 (MEM_REF, type,
10131 TREE_OPERAND (iref, 0),
10132 int_const_binop (PLUS_EXPR, arg1,
10133 TREE_OPERAND (iref, 1)));
10136 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
10137 if (TREE_CODE (arg0) == ADDR_EXPR
10138 && handled_component_p (TREE_OPERAND (arg0, 0)))
10140 tree base;
10141 HOST_WIDE_INT coffset;
10142 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
10143 &coffset);
10144 if (!base)
10145 return NULL_TREE;
10146 return fold_build2 (MEM_REF, type,
10147 build_fold_addr_expr (base),
10148 int_const_binop (PLUS_EXPR, arg1,
10149 size_int (coffset)));
10152 return NULL_TREE;
10154 case POINTER_PLUS_EXPR:
10155 /* 0 +p index -> (type)index */
10156 if (integer_zerop (arg0))
10157 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10159 /* PTR +p 0 -> PTR */
10160 if (integer_zerop (arg1))
10161 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10163 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
10164 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10165 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10166 return fold_convert_loc (loc, type,
10167 fold_build2_loc (loc, PLUS_EXPR, sizetype,
10168 fold_convert_loc (loc, sizetype,
10169 arg1),
10170 fold_convert_loc (loc, sizetype,
10171 arg0)));
10173 /* (PTR +p B) +p A -> PTR +p (B + A) */
10174 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10176 tree inner;
10177 tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
10178 tree arg00 = TREE_OPERAND (arg0, 0);
10179 inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
10180 arg01, fold_convert_loc (loc, sizetype, arg1));
10181 return fold_convert_loc (loc, type,
10182 fold_build_pointer_plus_loc (loc,
10183 arg00, inner));
10186 /* PTR_CST +p CST -> CST1 */
10187 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10188 return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
10189 fold_convert_loc (loc, type, arg1));
10191 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
10192 of the array. Loop optimizer sometimes produce this type of
10193 expressions. */
10194 if (TREE_CODE (arg0) == ADDR_EXPR)
10196 tem = try_move_mult_to_index (loc, arg0,
10197 fold_convert_loc (loc,
10198 ssizetype, arg1));
10199 if (tem)
10200 return fold_convert_loc (loc, type, tem);
10203 return NULL_TREE;
10205 case PLUS_EXPR:
10206 /* A + (-B) -> A - B */
10207 if (TREE_CODE (arg1) == NEGATE_EXPR)
10208 return fold_build2_loc (loc, MINUS_EXPR, type,
10209 fold_convert_loc (loc, type, arg0),
10210 fold_convert_loc (loc, type,
10211 TREE_OPERAND (arg1, 0)));
10212 /* (-A) + B -> B - A */
10213 if (TREE_CODE (arg0) == NEGATE_EXPR
10214 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
10215 return fold_build2_loc (loc, MINUS_EXPR, type,
10216 fold_convert_loc (loc, type, arg1),
10217 fold_convert_loc (loc, type,
10218 TREE_OPERAND (arg0, 0)));
10220 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
10222 /* Convert ~A + 1 to -A. */
10223 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10224 && integer_onep (arg1))
10225 return fold_build1_loc (loc, NEGATE_EXPR, type,
10226 fold_convert_loc (loc, type,
10227 TREE_OPERAND (arg0, 0)));
10229 /* ~X + X is -1. */
10230 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10231 && !TYPE_OVERFLOW_TRAPS (type))
10233 tree tem = TREE_OPERAND (arg0, 0);
10235 STRIP_NOPS (tem);
10236 if (operand_equal_p (tem, arg1, 0))
10238 t1 = build_all_ones_cst (type);
10239 return omit_one_operand_loc (loc, type, t1, arg1);
10243 /* X + ~X is -1. */
10244 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10245 && !TYPE_OVERFLOW_TRAPS (type))
10247 tree tem = TREE_OPERAND (arg1, 0);
10249 STRIP_NOPS (tem);
10250 if (operand_equal_p (arg0, tem, 0))
10252 t1 = build_all_ones_cst (type);
10253 return omit_one_operand_loc (loc, type, t1, arg0);
10257 /* X + (X / CST) * -CST is X % CST. */
10258 if (TREE_CODE (arg1) == MULT_EXPR
10259 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10260 && operand_equal_p (arg0,
10261 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
10263 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
10264 tree cst1 = TREE_OPERAND (arg1, 1);
10265 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
10266 cst1, cst0);
10267 if (sum && integer_zerop (sum))
10268 return fold_convert_loc (loc, type,
10269 fold_build2_loc (loc, TRUNC_MOD_EXPR,
10270 TREE_TYPE (arg0), arg0,
10271 cst0));
10275 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
10276 one. Make sure the type is not saturating and has the signedness of
10277 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10278 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10279 if ((TREE_CODE (arg0) == MULT_EXPR
10280 || TREE_CODE (arg1) == MULT_EXPR)
10281 && !TYPE_SATURATING (type)
10282 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10283 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10284 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10286 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10287 if (tem)
10288 return tem;
10291 if (! FLOAT_TYPE_P (type))
10293 if (integer_zerop (arg1))
10294 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10296 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
10297 with a constant, and the two constants have no bits in common,
10298 we should treat this as a BIT_IOR_EXPR since this may produce more
10299 simplifications. */
10300 if (TREE_CODE (arg0) == BIT_AND_EXPR
10301 && TREE_CODE (arg1) == BIT_AND_EXPR
10302 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10303 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10304 && integer_zerop (const_binop (BIT_AND_EXPR,
10305 TREE_OPERAND (arg0, 1),
10306 TREE_OPERAND (arg1, 1))))
10308 code = BIT_IOR_EXPR;
10309 goto bit_ior;
10312 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10313 (plus (plus (mult) (mult)) (foo)) so that we can
10314 take advantage of the factoring cases below. */
10315 if (TYPE_OVERFLOW_WRAPS (type)
10316 && (((TREE_CODE (arg0) == PLUS_EXPR
10317 || TREE_CODE (arg0) == MINUS_EXPR)
10318 && TREE_CODE (arg1) == MULT_EXPR)
10319 || ((TREE_CODE (arg1) == PLUS_EXPR
10320 || TREE_CODE (arg1) == MINUS_EXPR)
10321 && TREE_CODE (arg0) == MULT_EXPR)))
10323 tree parg0, parg1, parg, marg;
10324 enum tree_code pcode;
10326 if (TREE_CODE (arg1) == MULT_EXPR)
10327 parg = arg0, marg = arg1;
10328 else
10329 parg = arg1, marg = arg0;
10330 pcode = TREE_CODE (parg);
10331 parg0 = TREE_OPERAND (parg, 0);
10332 parg1 = TREE_OPERAND (parg, 1);
10333 STRIP_NOPS (parg0);
10334 STRIP_NOPS (parg1);
10336 if (TREE_CODE (parg0) == MULT_EXPR
10337 && TREE_CODE (parg1) != MULT_EXPR)
10338 return fold_build2_loc (loc, pcode, type,
10339 fold_build2_loc (loc, PLUS_EXPR, type,
10340 fold_convert_loc (loc, type,
10341 parg0),
10342 fold_convert_loc (loc, type,
10343 marg)),
10344 fold_convert_loc (loc, type, parg1));
10345 if (TREE_CODE (parg0) != MULT_EXPR
10346 && TREE_CODE (parg1) == MULT_EXPR)
10347 return
10348 fold_build2_loc (loc, PLUS_EXPR, type,
10349 fold_convert_loc (loc, type, parg0),
10350 fold_build2_loc (loc, pcode, type,
10351 fold_convert_loc (loc, type, marg),
10352 fold_convert_loc (loc, type,
10353 parg1)));
10356 else
10358 /* See if ARG1 is zero and X + ARG1 reduces to X. */
10359 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10360 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10362 /* Likewise if the operands are reversed. */
10363 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10364 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10366 /* Convert X + -C into X - C. */
10367 if (TREE_CODE (arg1) == REAL_CST
10368 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10370 tem = fold_negate_const (arg1, type);
10371 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10372 return fold_build2_loc (loc, MINUS_EXPR, type,
10373 fold_convert_loc (loc, type, arg0),
10374 fold_convert_loc (loc, type, tem));
10377 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10378 to __complex__ ( x, y ). This is not the same for SNaNs or
10379 if signed zeros are involved. */
10380 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10381 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10382 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10384 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10385 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10386 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10387 bool arg0rz = false, arg0iz = false;
10388 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10389 || (arg0i && (arg0iz = real_zerop (arg0i))))
10391 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10392 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10393 if (arg0rz && arg1i && real_zerop (arg1i))
10395 tree rp = arg1r ? arg1r
10396 : build1 (REALPART_EXPR, rtype, arg1);
10397 tree ip = arg0i ? arg0i
10398 : build1 (IMAGPART_EXPR, rtype, arg0);
10399 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10401 else if (arg0iz && arg1r && real_zerop (arg1r))
10403 tree rp = arg0r ? arg0r
10404 : build1 (REALPART_EXPR, rtype, arg0);
10405 tree ip = arg1i ? arg1i
10406 : build1 (IMAGPART_EXPR, rtype, arg1);
10407 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10412 if (flag_unsafe_math_optimizations
10413 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10414 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10415 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10416 return tem;
10418 /* Convert x+x into x*2.0. */
10419 if (operand_equal_p (arg0, arg1, 0)
10420 && SCALAR_FLOAT_TYPE_P (type))
10421 return fold_build2_loc (loc, MULT_EXPR, type, arg0,
10422 build_real (type, dconst2));
10424 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10425 We associate floats only if the user has specified
10426 -fassociative-math. */
10427 if (flag_associative_math
10428 && TREE_CODE (arg1) == PLUS_EXPR
10429 && TREE_CODE (arg0) != MULT_EXPR)
10431 tree tree10 = TREE_OPERAND (arg1, 0);
10432 tree tree11 = TREE_OPERAND (arg1, 1);
10433 if (TREE_CODE (tree11) == MULT_EXPR
10434 && TREE_CODE (tree10) == MULT_EXPR)
10436 tree tree0;
10437 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
10438 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
10441 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10442 We associate floats only if the user has specified
10443 -fassociative-math. */
10444 if (flag_associative_math
10445 && TREE_CODE (arg0) == PLUS_EXPR
10446 && TREE_CODE (arg1) != MULT_EXPR)
10448 tree tree00 = TREE_OPERAND (arg0, 0);
10449 tree tree01 = TREE_OPERAND (arg0, 1);
10450 if (TREE_CODE (tree01) == MULT_EXPR
10451 && TREE_CODE (tree00) == MULT_EXPR)
10453 tree tree0;
10454 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
10455 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
10460 bit_rotate:
10461 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10462 is a rotate of A by C1 bits. */
10463 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10464 is a rotate of A by B bits. */
10466 enum tree_code code0, code1;
10467 tree rtype;
10468 code0 = TREE_CODE (arg0);
10469 code1 = TREE_CODE (arg1);
10470 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10471 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10472 && operand_equal_p (TREE_OPERAND (arg0, 0),
10473 TREE_OPERAND (arg1, 0), 0)
10474 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10475 TYPE_UNSIGNED (rtype))
10476 /* Only create rotates in complete modes. Other cases are not
10477 expanded properly. */
10478 && (element_precision (rtype)
10479 == element_precision (TYPE_MODE (rtype))))
10481 tree tree01, tree11;
10482 enum tree_code code01, code11;
10484 tree01 = TREE_OPERAND (arg0, 1);
10485 tree11 = TREE_OPERAND (arg1, 1);
10486 STRIP_NOPS (tree01);
10487 STRIP_NOPS (tree11);
10488 code01 = TREE_CODE (tree01);
10489 code11 = TREE_CODE (tree11);
10490 if (code01 == INTEGER_CST
10491 && code11 == INTEGER_CST
10492 && TREE_INT_CST_HIGH (tree01) == 0
10493 && TREE_INT_CST_HIGH (tree11) == 0
10494 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10495 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10497 tem = build2_loc (loc, LROTATE_EXPR,
10498 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10499 TREE_OPERAND (arg0, 0),
10500 code0 == LSHIFT_EXPR ? tree01 : tree11);
10501 return fold_convert_loc (loc, type, tem);
10503 else if (code11 == MINUS_EXPR)
10505 tree tree110, tree111;
10506 tree110 = TREE_OPERAND (tree11, 0);
10507 tree111 = TREE_OPERAND (tree11, 1);
10508 STRIP_NOPS (tree110);
10509 STRIP_NOPS (tree111);
10510 if (TREE_CODE (tree110) == INTEGER_CST
10511 && 0 == compare_tree_int (tree110,
10512 element_precision
10513 (TREE_TYPE (TREE_OPERAND
10514 (arg0, 0))))
10515 && operand_equal_p (tree01, tree111, 0))
10516 return
10517 fold_convert_loc (loc, type,
10518 build2 ((code0 == LSHIFT_EXPR
10519 ? LROTATE_EXPR
10520 : RROTATE_EXPR),
10521 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10522 TREE_OPERAND (arg0, 0), tree01));
10524 else if (code01 == MINUS_EXPR)
10526 tree tree010, tree011;
10527 tree010 = TREE_OPERAND (tree01, 0);
10528 tree011 = TREE_OPERAND (tree01, 1);
10529 STRIP_NOPS (tree010);
10530 STRIP_NOPS (tree011);
10531 if (TREE_CODE (tree010) == INTEGER_CST
10532 && 0 == compare_tree_int (tree010,
10533 element_precision
10534 (TREE_TYPE (TREE_OPERAND
10535 (arg0, 0))))
10536 && operand_equal_p (tree11, tree011, 0))
10537 return fold_convert_loc
10538 (loc, type,
10539 build2 ((code0 != LSHIFT_EXPR
10540 ? LROTATE_EXPR
10541 : RROTATE_EXPR),
10542 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10543 TREE_OPERAND (arg0, 0), tree11));
10548 associate:
10549 /* In most languages, can't associate operations on floats through
10550 parentheses. Rather than remember where the parentheses were, we
10551 don't associate floats at all, unless the user has specified
10552 -fassociative-math.
10553 And, we need to make sure type is not saturating. */
10555 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10556 && !TYPE_SATURATING (type))
10558 tree var0, con0, lit0, minus_lit0;
10559 tree var1, con1, lit1, minus_lit1;
10560 tree atype = type;
10561 bool ok = true;
10563 /* Split both trees into variables, constants, and literals. Then
10564 associate each group together, the constants with literals,
10565 then the result with variables. This increases the chances of
10566 literals being recombined later and of generating relocatable
10567 expressions for the sum of a constant and literal. */
10568 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10569 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10570 code == MINUS_EXPR);
10572 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
10573 if (code == MINUS_EXPR)
10574 code = PLUS_EXPR;
10576 /* With undefined overflow prefer doing association in a type
10577 which wraps on overflow, if that is one of the operand types. */
10578 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10579 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10581 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10582 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10583 atype = TREE_TYPE (arg0);
10584 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10585 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
10586 atype = TREE_TYPE (arg1);
10587 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
10590 /* With undefined overflow we can only associate constants with one
10591 variable, and constants whose association doesn't overflow. */
10592 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10593 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
10595 if (var0 && var1)
10597 tree tmp0 = var0;
10598 tree tmp1 = var1;
10600 if (TREE_CODE (tmp0) == NEGATE_EXPR)
10601 tmp0 = TREE_OPERAND (tmp0, 0);
10602 if (CONVERT_EXPR_P (tmp0)
10603 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
10604 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
10605 <= TYPE_PRECISION (atype)))
10606 tmp0 = TREE_OPERAND (tmp0, 0);
10607 if (TREE_CODE (tmp1) == NEGATE_EXPR)
10608 tmp1 = TREE_OPERAND (tmp1, 0);
10609 if (CONVERT_EXPR_P (tmp1)
10610 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
10611 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
10612 <= TYPE_PRECISION (atype)))
10613 tmp1 = TREE_OPERAND (tmp1, 0);
10614 /* The only case we can still associate with two variables
10615 is if they are the same, modulo negation and bit-pattern
10616 preserving conversions. */
10617 if (!operand_equal_p (tmp0, tmp1, 0))
10618 ok = false;
10622 /* Only do something if we found more than two objects. Otherwise,
10623 nothing has changed and we risk infinite recursion. */
10624 if (ok
10625 && (2 < ((var0 != 0) + (var1 != 0)
10626 + (con0 != 0) + (con1 != 0)
10627 + (lit0 != 0) + (lit1 != 0)
10628 + (minus_lit0 != 0) + (minus_lit1 != 0))))
10630 bool any_overflows = false;
10631 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
10632 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
10633 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
10634 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
10635 var0 = associate_trees (loc, var0, var1, code, atype);
10636 con0 = associate_trees (loc, con0, con1, code, atype);
10637 lit0 = associate_trees (loc, lit0, lit1, code, atype);
10638 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
10639 code, atype);
10641 /* Preserve the MINUS_EXPR if the negative part of the literal is
10642 greater than the positive part. Otherwise, the multiplicative
10643 folding code (i.e extract_muldiv) may be fooled in case
10644 unsigned constants are subtracted, like in the following
10645 example: ((X*2 + 4) - 8U)/2. */
10646 if (minus_lit0 && lit0)
10648 if (TREE_CODE (lit0) == INTEGER_CST
10649 && TREE_CODE (minus_lit0) == INTEGER_CST
10650 && tree_int_cst_lt (lit0, minus_lit0))
10652 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10653 MINUS_EXPR, atype);
10654 lit0 = 0;
10656 else
10658 lit0 = associate_trees (loc, lit0, minus_lit0,
10659 MINUS_EXPR, atype);
10660 minus_lit0 = 0;
10664 /* Don't introduce overflows through reassociation. */
10665 if (!any_overflows
10666 && ((lit0 && TREE_OVERFLOW (lit0))
10667 || (minus_lit0 && TREE_OVERFLOW (minus_lit0))))
10668 return NULL_TREE;
10670 if (minus_lit0)
10672 if (con0 == 0)
10673 return
10674 fold_convert_loc (loc, type,
10675 associate_trees (loc, var0, minus_lit0,
10676 MINUS_EXPR, atype));
10677 else
10679 con0 = associate_trees (loc, con0, minus_lit0,
10680 MINUS_EXPR, atype);
10681 return
10682 fold_convert_loc (loc, type,
10683 associate_trees (loc, var0, con0,
10684 PLUS_EXPR, atype));
10688 con0 = associate_trees (loc, con0, lit0, code, atype);
10689 return
10690 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10691 code, atype));
10695 return NULL_TREE;
10697 case MINUS_EXPR:
10698 /* Pointer simplifications for subtraction, simple reassociations. */
10699 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10701 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10702 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10703 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10705 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10706 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10707 tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10708 tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10709 return fold_build2_loc (loc, PLUS_EXPR, type,
10710 fold_build2_loc (loc, MINUS_EXPR, type,
10711 arg00, arg10),
10712 fold_build2_loc (loc, MINUS_EXPR, type,
10713 arg01, arg11));
10715 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10716 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10718 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10719 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10720 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10721 fold_convert_loc (loc, type, arg1));
10722 if (tmp)
10723 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10726 /* A - (-B) -> A + B */
10727 if (TREE_CODE (arg1) == NEGATE_EXPR)
10728 return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10729 fold_convert_loc (loc, type,
10730 TREE_OPERAND (arg1, 0)));
10731 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10732 if (TREE_CODE (arg0) == NEGATE_EXPR
10733 && negate_expr_p (arg1)
10734 && reorder_operands_p (arg0, arg1))
10735 return fold_build2_loc (loc, MINUS_EXPR, type,
10736 fold_convert_loc (loc, type,
10737 negate_expr (arg1)),
10738 fold_convert_loc (loc, type,
10739 TREE_OPERAND (arg0, 0)));
10740 /* Convert -A - 1 to ~A. */
10741 if (TREE_CODE (type) != COMPLEX_TYPE
10742 && TREE_CODE (arg0) == NEGATE_EXPR
10743 && integer_onep (arg1)
10744 && !TYPE_OVERFLOW_TRAPS (type))
10745 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10746 fold_convert_loc (loc, type,
10747 TREE_OPERAND (arg0, 0)));
10749 /* Convert -1 - A to ~A. */
10750 if (TREE_CODE (type) != COMPLEX_TYPE
10751 && integer_all_onesp (arg0))
10752 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10755 /* X - (X / Y) * Y is X % Y. */
10756 if ((INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
10757 && TREE_CODE (arg1) == MULT_EXPR
10758 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10759 && operand_equal_p (arg0,
10760 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10761 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10762 TREE_OPERAND (arg1, 1), 0))
10763 return
10764 fold_convert_loc (loc, type,
10765 fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10766 arg0, TREE_OPERAND (arg1, 1)));
10768 if (! FLOAT_TYPE_P (type))
10770 if (integer_zerop (arg0))
10771 return negate_expr (fold_convert_loc (loc, type, arg1));
10772 if (integer_zerop (arg1))
10773 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10775 /* Fold A - (A & B) into ~B & A. */
10776 if (!TREE_SIDE_EFFECTS (arg0)
10777 && TREE_CODE (arg1) == BIT_AND_EXPR)
10779 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10781 tree arg10 = fold_convert_loc (loc, type,
10782 TREE_OPERAND (arg1, 0));
10783 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10784 fold_build1_loc (loc, BIT_NOT_EXPR,
10785 type, arg10),
10786 fold_convert_loc (loc, type, arg0));
10788 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10790 tree arg11 = fold_convert_loc (loc,
10791 type, TREE_OPERAND (arg1, 1));
10792 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10793 fold_build1_loc (loc, BIT_NOT_EXPR,
10794 type, arg11),
10795 fold_convert_loc (loc, type, arg0));
10799 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10800 any power of 2 minus 1. */
10801 if (TREE_CODE (arg0) == BIT_AND_EXPR
10802 && TREE_CODE (arg1) == BIT_AND_EXPR
10803 && operand_equal_p (TREE_OPERAND (arg0, 0),
10804 TREE_OPERAND (arg1, 0), 0))
10806 tree mask0 = TREE_OPERAND (arg0, 1);
10807 tree mask1 = TREE_OPERAND (arg1, 1);
10808 tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10810 if (operand_equal_p (tem, mask1, 0))
10812 tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10813 TREE_OPERAND (arg0, 0), mask1);
10814 return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10819 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10820 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10821 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10823 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10824 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10825 (-ARG1 + ARG0) reduces to -ARG1. */
10826 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10827 return negate_expr (fold_convert_loc (loc, type, arg1));
10829 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10830 __complex__ ( x, -y ). This is not the same for SNaNs or if
10831 signed zeros are involved. */
10832 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10833 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10834 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10836 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10837 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10838 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10839 bool arg0rz = false, arg0iz = false;
10840 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10841 || (arg0i && (arg0iz = real_zerop (arg0i))))
10843 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10844 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10845 if (arg0rz && arg1i && real_zerop (arg1i))
10847 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10848 arg1r ? arg1r
10849 : build1 (REALPART_EXPR, rtype, arg1));
10850 tree ip = arg0i ? arg0i
10851 : build1 (IMAGPART_EXPR, rtype, arg0);
10852 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10854 else if (arg0iz && arg1r && real_zerop (arg1r))
10856 tree rp = arg0r ? arg0r
10857 : build1 (REALPART_EXPR, rtype, arg0);
10858 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10859 arg1i ? arg1i
10860 : build1 (IMAGPART_EXPR, rtype, arg1));
10861 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10866 /* Fold &x - &x. This can happen from &x.foo - &x.
10867 This is unsafe for certain floats even in non-IEEE formats.
10868 In IEEE, it is unsafe because it does wrong for NaNs.
10869 Also note that operand_equal_p is always false if an operand
10870 is volatile. */
10872 if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10873 && operand_equal_p (arg0, arg1, 0))
10874 return build_zero_cst (type);
10876 /* A - B -> A + (-B) if B is easily negatable. */
10877 if (negate_expr_p (arg1)
10878 && ((FLOAT_TYPE_P (type)
10879 /* Avoid this transformation if B is a positive REAL_CST. */
10880 && (TREE_CODE (arg1) != REAL_CST
10881 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10882 || INTEGRAL_TYPE_P (type)))
10883 return fold_build2_loc (loc, PLUS_EXPR, type,
10884 fold_convert_loc (loc, type, arg0),
10885 fold_convert_loc (loc, type,
10886 negate_expr (arg1)));
10888 /* Try folding difference of addresses. */
10890 HOST_WIDE_INT diff;
10892 if ((TREE_CODE (arg0) == ADDR_EXPR
10893 || TREE_CODE (arg1) == ADDR_EXPR)
10894 && ptr_difference_const (arg0, arg1, &diff))
10895 return build_int_cst_type (type, diff);
10898 /* Fold &a[i] - &a[j] to i-j. */
10899 if (TREE_CODE (arg0) == ADDR_EXPR
10900 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10901 && TREE_CODE (arg1) == ADDR_EXPR
10902 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10904 tree tem = fold_addr_of_array_ref_difference (loc, type,
10905 TREE_OPERAND (arg0, 0),
10906 TREE_OPERAND (arg1, 0));
10907 if (tem)
10908 return tem;
10911 if (FLOAT_TYPE_P (type)
10912 && flag_unsafe_math_optimizations
10913 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10914 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10915 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10916 return tem;
10918 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
10919 one. Make sure the type is not saturating and has the signedness of
10920 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10921 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10922 if ((TREE_CODE (arg0) == MULT_EXPR
10923 || TREE_CODE (arg1) == MULT_EXPR)
10924 && !TYPE_SATURATING (type)
10925 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10926 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10927 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10929 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10930 if (tem)
10931 return tem;
10934 goto associate;
10936 case MULT_EXPR:
10937 /* (-A) * (-B) -> A * B */
10938 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10939 return fold_build2_loc (loc, MULT_EXPR, type,
10940 fold_convert_loc (loc, type,
10941 TREE_OPERAND (arg0, 0)),
10942 fold_convert_loc (loc, type,
10943 negate_expr (arg1)));
10944 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10945 return fold_build2_loc (loc, MULT_EXPR, type,
10946 fold_convert_loc (loc, type,
10947 negate_expr (arg0)),
10948 fold_convert_loc (loc, type,
10949 TREE_OPERAND (arg1, 0)));
10951 if (! FLOAT_TYPE_P (type))
10953 if (integer_zerop (arg1))
10954 return omit_one_operand_loc (loc, type, arg1, arg0);
10955 if (integer_onep (arg1))
10956 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10957 /* Transform x * -1 into -x. Make sure to do the negation
10958 on the original operand with conversions not stripped
10959 because we can only strip non-sign-changing conversions. */
10960 if (integer_minus_onep (arg1))
10961 return fold_convert_loc (loc, type, negate_expr (op0));
10962 /* Transform x * -C into -x * C if x is easily negatable. */
10963 if (TREE_CODE (arg1) == INTEGER_CST
10964 && tree_int_cst_sgn (arg1) == -1
10965 && negate_expr_p (arg0)
10966 && (tem = negate_expr (arg1)) != arg1
10967 && !TREE_OVERFLOW (tem))
10968 return fold_build2_loc (loc, MULT_EXPR, type,
10969 fold_convert_loc (loc, type,
10970 negate_expr (arg0)),
10971 tem);
10973 /* (a * (1 << b)) is (a << b) */
10974 if (TREE_CODE (arg1) == LSHIFT_EXPR
10975 && integer_onep (TREE_OPERAND (arg1, 0)))
10976 return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10977 TREE_OPERAND (arg1, 1));
10978 if (TREE_CODE (arg0) == LSHIFT_EXPR
10979 && integer_onep (TREE_OPERAND (arg0, 0)))
10980 return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10981 TREE_OPERAND (arg0, 1));
10983 /* (A + A) * C -> A * 2 * C */
10984 if (TREE_CODE (arg0) == PLUS_EXPR
10985 && TREE_CODE (arg1) == INTEGER_CST
10986 && operand_equal_p (TREE_OPERAND (arg0, 0),
10987 TREE_OPERAND (arg0, 1), 0))
10988 return fold_build2_loc (loc, MULT_EXPR, type,
10989 omit_one_operand_loc (loc, type,
10990 TREE_OPERAND (arg0, 0),
10991 TREE_OPERAND (arg0, 1)),
10992 fold_build2_loc (loc, MULT_EXPR, type,
10993 build_int_cst (type, 2) , arg1));
10995 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
10996 sign-changing only. */
10997 if (TREE_CODE (arg1) == INTEGER_CST
10998 && TREE_CODE (arg0) == EXACT_DIV_EXPR
10999 && operand_equal_p (arg1, TREE_OPERAND (arg0, 1), 0))
11000 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11002 strict_overflow_p = false;
11003 if (TREE_CODE (arg1) == INTEGER_CST
11004 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11005 &strict_overflow_p)))
11007 if (strict_overflow_p)
11008 fold_overflow_warning (("assuming signed overflow does not "
11009 "occur when simplifying "
11010 "multiplication"),
11011 WARN_STRICT_OVERFLOW_MISC);
11012 return fold_convert_loc (loc, type, tem);
11015 /* Optimize z * conj(z) for integer complex numbers. */
11016 if (TREE_CODE (arg0) == CONJ_EXPR
11017 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11018 return fold_mult_zconjz (loc, type, arg1);
11019 if (TREE_CODE (arg1) == CONJ_EXPR
11020 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11021 return fold_mult_zconjz (loc, type, arg0);
11023 else
11025 /* Maybe fold x * 0 to 0. The expressions aren't the same
11026 when x is NaN, since x * 0 is also NaN. Nor are they the
11027 same in modes with signed zeros, since multiplying a
11028 negative value by 0 gives -0, not +0. */
11029 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11030 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11031 && real_zerop (arg1))
11032 return omit_one_operand_loc (loc, type, arg1, arg0);
11033 /* In IEEE floating point, x*1 is not equivalent to x for snans.
11034 Likewise for complex arithmetic with signed zeros. */
11035 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11036 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11037 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11038 && real_onep (arg1))
11039 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11041 /* Transform x * -1.0 into -x. */
11042 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11043 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11044 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11045 && real_minus_onep (arg1))
11046 return fold_convert_loc (loc, type, negate_expr (arg0));
11048 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
11049 the result for floating point types due to rounding so it is applied
11050 only if -fassociative-math was specify. */
11051 if (flag_associative_math
11052 && TREE_CODE (arg0) == RDIV_EXPR
11053 && TREE_CODE (arg1) == REAL_CST
11054 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
11056 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
11057 arg1);
11058 if (tem)
11059 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11060 TREE_OPERAND (arg0, 1));
11063 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
11064 if (operand_equal_p (arg0, arg1, 0))
11066 tree tem = fold_strip_sign_ops (arg0);
11067 if (tem != NULL_TREE)
11069 tem = fold_convert_loc (loc, type, tem);
11070 return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
11074 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11075 This is not the same for NaNs or if signed zeros are
11076 involved. */
11077 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11078 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11079 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11080 && TREE_CODE (arg1) == COMPLEX_CST
11081 && real_zerop (TREE_REALPART (arg1)))
11083 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11084 if (real_onep (TREE_IMAGPART (arg1)))
11085 return
11086 fold_build2_loc (loc, COMPLEX_EXPR, type,
11087 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
11088 rtype, arg0)),
11089 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
11090 else if (real_minus_onep (TREE_IMAGPART (arg1)))
11091 return
11092 fold_build2_loc (loc, COMPLEX_EXPR, type,
11093 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
11094 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
11095 rtype, arg0)));
11098 /* Optimize z * conj(z) for floating point complex numbers.
11099 Guarded by flag_unsafe_math_optimizations as non-finite
11100 imaginary components don't produce scalar results. */
11101 if (flag_unsafe_math_optimizations
11102 && TREE_CODE (arg0) == CONJ_EXPR
11103 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11104 return fold_mult_zconjz (loc, type, arg1);
11105 if (flag_unsafe_math_optimizations
11106 && TREE_CODE (arg1) == CONJ_EXPR
11107 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11108 return fold_mult_zconjz (loc, type, arg0);
11110 if (flag_unsafe_math_optimizations)
11112 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11113 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11115 /* Optimizations of root(...)*root(...). */
11116 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
11118 tree rootfn, arg;
11119 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11120 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11122 /* Optimize sqrt(x)*sqrt(x) as x. */
11123 if (BUILTIN_SQRT_P (fcode0)
11124 && operand_equal_p (arg00, arg10, 0)
11125 && ! HONOR_SNANS (TYPE_MODE (type)))
11126 return arg00;
11128 /* Optimize root(x)*root(y) as root(x*y). */
11129 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11130 arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
11131 return build_call_expr_loc (loc, rootfn, 1, arg);
11134 /* Optimize expN(x)*expN(y) as expN(x+y). */
11135 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
11137 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11138 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
11139 CALL_EXPR_ARG (arg0, 0),
11140 CALL_EXPR_ARG (arg1, 0));
11141 return build_call_expr_loc (loc, expfn, 1, arg);
11144 /* Optimizations of pow(...)*pow(...). */
11145 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
11146 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
11147 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
11149 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11150 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11151 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11152 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11154 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
11155 if (operand_equal_p (arg01, arg11, 0))
11157 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11158 tree arg = fold_build2_loc (loc, MULT_EXPR, type,
11159 arg00, arg10);
11160 return build_call_expr_loc (loc, powfn, 2, arg, arg01);
11163 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
11164 if (operand_equal_p (arg00, arg10, 0))
11166 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11167 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
11168 arg01, arg11);
11169 return build_call_expr_loc (loc, powfn, 2, arg00, arg);
11173 /* Optimize tan(x)*cos(x) as sin(x). */
11174 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
11175 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
11176 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
11177 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
11178 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
11179 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
11180 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11181 CALL_EXPR_ARG (arg1, 0), 0))
11183 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
11185 if (sinfn != NULL_TREE)
11186 return build_call_expr_loc (loc, sinfn, 1,
11187 CALL_EXPR_ARG (arg0, 0));
11190 /* Optimize x*pow(x,c) as pow(x,c+1). */
11191 if (fcode1 == BUILT_IN_POW
11192 || fcode1 == BUILT_IN_POWF
11193 || fcode1 == BUILT_IN_POWL)
11195 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11196 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11197 if (TREE_CODE (arg11) == REAL_CST
11198 && !TREE_OVERFLOW (arg11)
11199 && operand_equal_p (arg0, arg10, 0))
11201 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11202 REAL_VALUE_TYPE c;
11203 tree arg;
11205 c = TREE_REAL_CST (arg11);
11206 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
11207 arg = build_real (type, c);
11208 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11212 /* Optimize pow(x,c)*x as pow(x,c+1). */
11213 if (fcode0 == BUILT_IN_POW
11214 || fcode0 == BUILT_IN_POWF
11215 || fcode0 == BUILT_IN_POWL)
11217 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11218 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11219 if (TREE_CODE (arg01) == REAL_CST
11220 && !TREE_OVERFLOW (arg01)
11221 && operand_equal_p (arg1, arg00, 0))
11223 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11224 REAL_VALUE_TYPE c;
11225 tree arg;
11227 c = TREE_REAL_CST (arg01);
11228 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
11229 arg = build_real (type, c);
11230 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11234 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
11235 if (!in_gimple_form
11236 && optimize
11237 && operand_equal_p (arg0, arg1, 0))
11239 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
11241 if (powfn)
11243 tree arg = build_real (type, dconst2);
11244 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11249 goto associate;
11251 case BIT_IOR_EXPR:
11252 bit_ior:
11253 if (integer_all_onesp (arg1))
11254 return omit_one_operand_loc (loc, type, arg1, arg0);
11255 if (integer_zerop (arg1))
11256 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11257 if (operand_equal_p (arg0, arg1, 0))
11258 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11260 /* ~X | X is -1. */
11261 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11262 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11264 t1 = build_zero_cst (type);
11265 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11266 return omit_one_operand_loc (loc, type, t1, arg1);
11269 /* X | ~X is -1. */
11270 if (TREE_CODE (arg1) == BIT_NOT_EXPR
11271 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11273 t1 = build_zero_cst (type);
11274 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11275 return omit_one_operand_loc (loc, type, t1, arg0);
11278 /* Canonicalize (X & C1) | C2. */
11279 if (TREE_CODE (arg0) == BIT_AND_EXPR
11280 && TREE_CODE (arg1) == INTEGER_CST
11281 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11283 double_int c1, c2, c3, msk;
11284 int width = TYPE_PRECISION (type), w;
11285 bool try_simplify = true;
11287 c1 = tree_to_double_int (TREE_OPERAND (arg0, 1));
11288 c2 = tree_to_double_int (arg1);
11290 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11291 if ((c1 & c2) == c1)
11292 return omit_one_operand_loc (loc, type, arg1,
11293 TREE_OPERAND (arg0, 0));
11295 msk = double_int::mask (width);
11297 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11298 if (msk.and_not (c1 | c2).is_zero ())
11299 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11300 TREE_OPERAND (arg0, 0), arg1);
11302 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11303 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11304 mode which allows further optimizations. */
11305 c1 &= msk;
11306 c2 &= msk;
11307 c3 = c1.and_not (c2);
11308 for (w = BITS_PER_UNIT;
11309 w <= width && w <= HOST_BITS_PER_WIDE_INT;
11310 w <<= 1)
11312 unsigned HOST_WIDE_INT mask
11313 = HOST_WIDE_INT_M1U >> (HOST_BITS_PER_WIDE_INT - w);
11314 if (((c1.low | c2.low) & mask) == mask
11315 && (c1.low & ~mask) == 0 && c1.high == 0)
11317 c3 = double_int::from_uhwi (mask);
11318 break;
11322 /* If X is a tree of the form (Y * K1) & K2, this might conflict
11323 with that optimization from the BIT_AND_EXPR optimizations.
11324 This could end up in an infinite recursion. */
11325 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == MULT_EXPR
11326 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
11327 == INTEGER_CST)
11329 tree t = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
11330 double_int masked = mask_with_tz (type, c3, tree_to_double_int (t));
11332 try_simplify = (masked != c1);
11335 if (try_simplify && c3 != c1)
11336 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11337 fold_build2_loc (loc, BIT_AND_EXPR, type,
11338 TREE_OPERAND (arg0, 0),
11339 double_int_to_tree (type,
11340 c3)),
11341 arg1);
11344 /* (X & Y) | Y is (X, Y). */
11345 if (TREE_CODE (arg0) == BIT_AND_EXPR
11346 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11347 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11348 /* (X & Y) | X is (Y, X). */
11349 if (TREE_CODE (arg0) == BIT_AND_EXPR
11350 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11351 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11352 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11353 /* X | (X & Y) is (Y, X). */
11354 if (TREE_CODE (arg1) == BIT_AND_EXPR
11355 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11356 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11357 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11358 /* X | (Y & X) is (Y, X). */
11359 if (TREE_CODE (arg1) == BIT_AND_EXPR
11360 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11361 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11362 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11364 /* (X & ~Y) | (~X & Y) is X ^ Y */
11365 if (TREE_CODE (arg0) == BIT_AND_EXPR
11366 && TREE_CODE (arg1) == BIT_AND_EXPR)
11368 tree a0, a1, l0, l1, n0, n1;
11370 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11371 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11373 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11374 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11376 n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
11377 n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
11379 if ((operand_equal_p (n0, a0, 0)
11380 && operand_equal_p (n1, a1, 0))
11381 || (operand_equal_p (n0, a1, 0)
11382 && operand_equal_p (n1, a0, 0)))
11383 return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
11386 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11387 if (t1 != NULL_TREE)
11388 return t1;
11390 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
11392 This results in more efficient code for machines without a NAND
11393 instruction. Combine will canonicalize to the first form
11394 which will allow use of NAND instructions provided by the
11395 backend if they exist. */
11396 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11397 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11399 return
11400 fold_build1_loc (loc, BIT_NOT_EXPR, type,
11401 build2 (BIT_AND_EXPR, type,
11402 fold_convert_loc (loc, type,
11403 TREE_OPERAND (arg0, 0)),
11404 fold_convert_loc (loc, type,
11405 TREE_OPERAND (arg1, 0))));
11408 /* See if this can be simplified into a rotate first. If that
11409 is unsuccessful continue in the association code. */
11410 goto bit_rotate;
11412 case BIT_XOR_EXPR:
11413 if (integer_zerop (arg1))
11414 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11415 if (integer_all_onesp (arg1))
11416 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
11417 if (operand_equal_p (arg0, arg1, 0))
11418 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11420 /* ~X ^ X is -1. */
11421 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11422 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11424 t1 = build_zero_cst (type);
11425 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11426 return omit_one_operand_loc (loc, type, t1, arg1);
11429 /* X ^ ~X is -1. */
11430 if (TREE_CODE (arg1) == BIT_NOT_EXPR
11431 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11433 t1 = build_zero_cst (type);
11434 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11435 return omit_one_operand_loc (loc, type, t1, arg0);
11438 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11439 with a constant, and the two constants have no bits in common,
11440 we should treat this as a BIT_IOR_EXPR since this may produce more
11441 simplifications. */
11442 if (TREE_CODE (arg0) == BIT_AND_EXPR
11443 && TREE_CODE (arg1) == BIT_AND_EXPR
11444 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11445 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11446 && integer_zerop (const_binop (BIT_AND_EXPR,
11447 TREE_OPERAND (arg0, 1),
11448 TREE_OPERAND (arg1, 1))))
11450 code = BIT_IOR_EXPR;
11451 goto bit_ior;
11454 /* (X | Y) ^ X -> Y & ~ X*/
11455 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11456 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11458 tree t2 = TREE_OPERAND (arg0, 1);
11459 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11460 arg1);
11461 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11462 fold_convert_loc (loc, type, t2),
11463 fold_convert_loc (loc, type, t1));
11464 return t1;
11467 /* (Y | X) ^ X -> Y & ~ X*/
11468 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11469 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11471 tree t2 = TREE_OPERAND (arg0, 0);
11472 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11473 arg1);
11474 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11475 fold_convert_loc (loc, type, t2),
11476 fold_convert_loc (loc, type, t1));
11477 return t1;
11480 /* X ^ (X | Y) -> Y & ~ X*/
11481 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11482 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11484 tree t2 = TREE_OPERAND (arg1, 1);
11485 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11486 arg0);
11487 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11488 fold_convert_loc (loc, type, t2),
11489 fold_convert_loc (loc, type, t1));
11490 return t1;
11493 /* X ^ (Y | X) -> Y & ~ X*/
11494 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11495 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11497 tree t2 = TREE_OPERAND (arg1, 0);
11498 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11499 arg0);
11500 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11501 fold_convert_loc (loc, type, t2),
11502 fold_convert_loc (loc, type, t1));
11503 return t1;
11506 /* Convert ~X ^ ~Y to X ^ Y. */
11507 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11508 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11509 return fold_build2_loc (loc, code, type,
11510 fold_convert_loc (loc, type,
11511 TREE_OPERAND (arg0, 0)),
11512 fold_convert_loc (loc, type,
11513 TREE_OPERAND (arg1, 0)));
11515 /* Convert ~X ^ C to X ^ ~C. */
11516 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11517 && TREE_CODE (arg1) == INTEGER_CST)
11518 return fold_build2_loc (loc, code, type,
11519 fold_convert_loc (loc, type,
11520 TREE_OPERAND (arg0, 0)),
11521 fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
11523 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11524 if (TREE_CODE (arg0) == BIT_AND_EXPR
11525 && integer_onep (TREE_OPERAND (arg0, 1))
11526 && integer_onep (arg1))
11527 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11528 build_zero_cst (TREE_TYPE (arg0)));
11530 /* Fold (X & Y) ^ Y as ~X & Y. */
11531 if (TREE_CODE (arg0) == BIT_AND_EXPR
11532 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11534 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11535 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11536 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11537 fold_convert_loc (loc, type, arg1));
11539 /* Fold (X & Y) ^ X as ~Y & X. */
11540 if (TREE_CODE (arg0) == BIT_AND_EXPR
11541 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11542 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11544 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11545 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11546 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11547 fold_convert_loc (loc, type, arg1));
11549 /* Fold X ^ (X & Y) as X & ~Y. */
11550 if (TREE_CODE (arg1) == BIT_AND_EXPR
11551 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11553 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11554 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11555 fold_convert_loc (loc, type, arg0),
11556 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11558 /* Fold X ^ (Y & X) as ~Y & X. */
11559 if (TREE_CODE (arg1) == BIT_AND_EXPR
11560 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11561 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11563 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11564 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11565 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11566 fold_convert_loc (loc, type, arg0));
11569 /* See if this can be simplified into a rotate first. If that
11570 is unsuccessful continue in the association code. */
11571 goto bit_rotate;
11573 case BIT_AND_EXPR:
11574 if (integer_all_onesp (arg1))
11575 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11576 if (integer_zerop (arg1))
11577 return omit_one_operand_loc (loc, type, arg1, arg0);
11578 if (operand_equal_p (arg0, arg1, 0))
11579 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11581 /* ~X & X, (X == 0) & X, and !X & X are always zero. */
11582 if ((TREE_CODE (arg0) == BIT_NOT_EXPR
11583 || TREE_CODE (arg0) == TRUTH_NOT_EXPR
11584 || (TREE_CODE (arg0) == EQ_EXPR
11585 && integer_zerop (TREE_OPERAND (arg0, 1))))
11586 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11587 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11589 /* X & ~X , X & (X == 0), and X & !X are always zero. */
11590 if ((TREE_CODE (arg1) == BIT_NOT_EXPR
11591 || TREE_CODE (arg1) == TRUTH_NOT_EXPR
11592 || (TREE_CODE (arg1) == EQ_EXPR
11593 && integer_zerop (TREE_OPERAND (arg1, 1))))
11594 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11595 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11597 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
11598 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11599 && TREE_CODE (arg1) == INTEGER_CST
11600 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11602 tree tmp1 = fold_convert_loc (loc, type, arg1);
11603 tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11604 tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11605 tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
11606 tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
11607 return
11608 fold_convert_loc (loc, type,
11609 fold_build2_loc (loc, BIT_IOR_EXPR,
11610 type, tmp2, tmp3));
11613 /* (X | Y) & Y is (X, Y). */
11614 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11615 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11616 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11617 /* (X | Y) & X is (Y, X). */
11618 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11619 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11620 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11621 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11622 /* X & (X | Y) is (Y, X). */
11623 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11624 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11625 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11626 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11627 /* X & (Y | X) is (Y, X). */
11628 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11629 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11630 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11631 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11633 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11634 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11635 && integer_onep (TREE_OPERAND (arg0, 1))
11636 && integer_onep (arg1))
11638 tree tem2;
11639 tem = TREE_OPERAND (arg0, 0);
11640 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
11641 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
11642 tem, tem2);
11643 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
11644 build_zero_cst (TREE_TYPE (tem)));
11646 /* Fold ~X & 1 as (X & 1) == 0. */
11647 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11648 && integer_onep (arg1))
11650 tree tem2;
11651 tem = TREE_OPERAND (arg0, 0);
11652 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
11653 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
11654 tem, tem2);
11655 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
11656 build_zero_cst (TREE_TYPE (tem)));
11658 /* Fold !X & 1 as X == 0. */
11659 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11660 && integer_onep (arg1))
11662 tem = TREE_OPERAND (arg0, 0);
11663 return fold_build2_loc (loc, EQ_EXPR, type, tem,
11664 build_zero_cst (TREE_TYPE (tem)));
11667 /* Fold (X ^ Y) & Y as ~X & Y. */
11668 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11669 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11671 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11672 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11673 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11674 fold_convert_loc (loc, type, arg1));
11676 /* Fold (X ^ Y) & X as ~Y & X. */
11677 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11678 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11679 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11681 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11682 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11683 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11684 fold_convert_loc (loc, type, arg1));
11686 /* Fold X & (X ^ Y) as X & ~Y. */
11687 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11688 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11690 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11691 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11692 fold_convert_loc (loc, type, arg0),
11693 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11695 /* Fold X & (Y ^ X) as ~Y & X. */
11696 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11697 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11698 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11700 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11701 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11702 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11703 fold_convert_loc (loc, type, arg0));
11706 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11707 multiple of 1 << CST. */
11708 if (TREE_CODE (arg1) == INTEGER_CST)
11710 double_int cst1 = tree_to_double_int (arg1);
11711 double_int ncst1 = (-cst1).ext (TYPE_PRECISION (TREE_TYPE (arg1)),
11712 TYPE_UNSIGNED (TREE_TYPE (arg1)));
11713 if ((cst1 & ncst1) == ncst1
11714 && multiple_of_p (type, arg0,
11715 double_int_to_tree (TREE_TYPE (arg1), ncst1)))
11716 return fold_convert_loc (loc, type, arg0);
11719 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11720 bits from CST2. */
11721 if (TREE_CODE (arg1) == INTEGER_CST
11722 && TREE_CODE (arg0) == MULT_EXPR
11723 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11725 double_int masked
11726 = mask_with_tz (type, tree_to_double_int (arg1),
11727 tree_to_double_int (TREE_OPERAND (arg0, 1)));
11729 if (masked.is_zero ())
11730 return omit_two_operands_loc (loc, type, build_zero_cst (type),
11731 arg0, arg1);
11732 else if (masked != tree_to_double_int (arg1))
11733 return fold_build2_loc (loc, code, type, op0,
11734 double_int_to_tree (type, masked));
11737 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11738 ((A & N) + B) & M -> (A + B) & M
11739 Similarly if (N & M) == 0,
11740 ((A | N) + B) & M -> (A + B) & M
11741 and for - instead of + (or unary - instead of +)
11742 and/or ^ instead of |.
11743 If B is constant and (B & M) == 0, fold into A & M. */
11744 if (host_integerp (arg1, 1))
11746 unsigned HOST_WIDE_INT cst1 = tree_low_cst (arg1, 1);
11747 if (~cst1 && (cst1 & (cst1 + 1)) == 0
11748 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11749 && (TREE_CODE (arg0) == PLUS_EXPR
11750 || TREE_CODE (arg0) == MINUS_EXPR
11751 || TREE_CODE (arg0) == NEGATE_EXPR)
11752 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11753 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11755 tree pmop[2];
11756 int which = 0;
11757 unsigned HOST_WIDE_INT cst0;
11759 /* Now we know that arg0 is (C + D) or (C - D) or
11760 -C and arg1 (M) is == (1LL << cst) - 1.
11761 Store C into PMOP[0] and D into PMOP[1]. */
11762 pmop[0] = TREE_OPERAND (arg0, 0);
11763 pmop[1] = NULL;
11764 if (TREE_CODE (arg0) != NEGATE_EXPR)
11766 pmop[1] = TREE_OPERAND (arg0, 1);
11767 which = 1;
11770 if (!host_integerp (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11771 || (tree_low_cst (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11772 & cst1) != cst1)
11773 which = -1;
11775 for (; which >= 0; which--)
11776 switch (TREE_CODE (pmop[which]))
11778 case BIT_AND_EXPR:
11779 case BIT_IOR_EXPR:
11780 case BIT_XOR_EXPR:
11781 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11782 != INTEGER_CST)
11783 break;
11784 /* tree_low_cst not used, because we don't care about
11785 the upper bits. */
11786 cst0 = TREE_INT_CST_LOW (TREE_OPERAND (pmop[which], 1));
11787 cst0 &= cst1;
11788 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11790 if (cst0 != cst1)
11791 break;
11793 else if (cst0 != 0)
11794 break;
11795 /* If C or D is of the form (A & N) where
11796 (N & M) == M, or of the form (A | N) or
11797 (A ^ N) where (N & M) == 0, replace it with A. */
11798 pmop[which] = TREE_OPERAND (pmop[which], 0);
11799 break;
11800 case INTEGER_CST:
11801 /* If C or D is a N where (N & M) == 0, it can be
11802 omitted (assumed 0). */
11803 if ((TREE_CODE (arg0) == PLUS_EXPR
11804 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11805 && (TREE_INT_CST_LOW (pmop[which]) & cst1) == 0)
11806 pmop[which] = NULL;
11807 break;
11808 default:
11809 break;
11812 /* Only build anything new if we optimized one or both arguments
11813 above. */
11814 if (pmop[0] != TREE_OPERAND (arg0, 0)
11815 || (TREE_CODE (arg0) != NEGATE_EXPR
11816 && pmop[1] != TREE_OPERAND (arg0, 1)))
11818 tree utype = TREE_TYPE (arg0);
11819 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11821 /* Perform the operations in a type that has defined
11822 overflow behavior. */
11823 utype = unsigned_type_for (TREE_TYPE (arg0));
11824 if (pmop[0] != NULL)
11825 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11826 if (pmop[1] != NULL)
11827 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11830 if (TREE_CODE (arg0) == NEGATE_EXPR)
11831 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11832 else if (TREE_CODE (arg0) == PLUS_EXPR)
11834 if (pmop[0] != NULL && pmop[1] != NULL)
11835 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11836 pmop[0], pmop[1]);
11837 else if (pmop[0] != NULL)
11838 tem = pmop[0];
11839 else if (pmop[1] != NULL)
11840 tem = pmop[1];
11841 else
11842 return build_int_cst (type, 0);
11844 else if (pmop[0] == NULL)
11845 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11846 else
11847 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11848 pmop[0], pmop[1]);
11849 /* TEM is now the new binary +, - or unary - replacement. */
11850 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11851 fold_convert_loc (loc, utype, arg1));
11852 return fold_convert_loc (loc, type, tem);
11857 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11858 if (t1 != NULL_TREE)
11859 return t1;
11860 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11861 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11862 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11864 prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11866 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11867 && (~TREE_INT_CST_LOW (arg1)
11868 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11869 return
11870 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11873 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11875 This results in more efficient code for machines without a NOR
11876 instruction. Combine will canonicalize to the first form
11877 which will allow use of NOR instructions provided by the
11878 backend if they exist. */
11879 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11880 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11882 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11883 build2 (BIT_IOR_EXPR, type,
11884 fold_convert_loc (loc, type,
11885 TREE_OPERAND (arg0, 0)),
11886 fold_convert_loc (loc, type,
11887 TREE_OPERAND (arg1, 0))));
11890 /* If arg0 is derived from the address of an object or function, we may
11891 be able to fold this expression using the object or function's
11892 alignment. */
11893 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11895 unsigned HOST_WIDE_INT modulus, residue;
11896 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11898 modulus = get_pointer_modulus_and_residue (arg0, &residue,
11899 integer_onep (arg1));
11901 /* This works because modulus is a power of 2. If this weren't the
11902 case, we'd have to replace it by its greatest power-of-2
11903 divisor: modulus & -modulus. */
11904 if (low < modulus)
11905 return build_int_cst (type, residue & low);
11908 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11909 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11910 if the new mask might be further optimized. */
11911 if ((TREE_CODE (arg0) == LSHIFT_EXPR
11912 || TREE_CODE (arg0) == RSHIFT_EXPR)
11913 && host_integerp (TREE_OPERAND (arg0, 1), 1)
11914 && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11915 && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11916 < TYPE_PRECISION (TREE_TYPE (arg0))
11917 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11918 && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11920 unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11921 unsigned HOST_WIDE_INT mask
11922 = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11923 unsigned HOST_WIDE_INT newmask, zerobits = 0;
11924 tree shift_type = TREE_TYPE (arg0);
11926 if (TREE_CODE (arg0) == LSHIFT_EXPR)
11927 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11928 else if (TREE_CODE (arg0) == RSHIFT_EXPR
11929 && TYPE_PRECISION (TREE_TYPE (arg0))
11930 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11932 prec = TYPE_PRECISION (TREE_TYPE (arg0));
11933 tree arg00 = TREE_OPERAND (arg0, 0);
11934 /* See if more bits can be proven as zero because of
11935 zero extension. */
11936 if (TREE_CODE (arg00) == NOP_EXPR
11937 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11939 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11940 if (TYPE_PRECISION (inner_type)
11941 == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11942 && TYPE_PRECISION (inner_type) < prec)
11944 prec = TYPE_PRECISION (inner_type);
11945 /* See if we can shorten the right shift. */
11946 if (shiftc < prec)
11947 shift_type = inner_type;
11950 zerobits = ~(unsigned HOST_WIDE_INT) 0;
11951 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11952 zerobits <<= prec - shiftc;
11953 /* For arithmetic shift if sign bit could be set, zerobits
11954 can contain actually sign bits, so no transformation is
11955 possible, unless MASK masks them all away. In that
11956 case the shift needs to be converted into logical shift. */
11957 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11958 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11960 if ((mask & zerobits) == 0)
11961 shift_type = unsigned_type_for (TREE_TYPE (arg0));
11962 else
11963 zerobits = 0;
11967 /* ((X << 16) & 0xff00) is (X, 0). */
11968 if ((mask & zerobits) == mask)
11969 return omit_one_operand_loc (loc, type,
11970 build_int_cst (type, 0), arg0);
11972 newmask = mask | zerobits;
11973 if (newmask != mask && (newmask & (newmask + 1)) == 0)
11975 /* Only do the transformation if NEWMASK is some integer
11976 mode's mask. */
11977 for (prec = BITS_PER_UNIT;
11978 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11979 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11980 break;
11981 if (prec < HOST_BITS_PER_WIDE_INT
11982 || newmask == ~(unsigned HOST_WIDE_INT) 0)
11984 tree newmaskt;
11986 if (shift_type != TREE_TYPE (arg0))
11988 tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11989 fold_convert_loc (loc, shift_type,
11990 TREE_OPERAND (arg0, 0)),
11991 TREE_OPERAND (arg0, 1));
11992 tem = fold_convert_loc (loc, type, tem);
11994 else
11995 tem = op0;
11996 newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11997 if (!tree_int_cst_equal (newmaskt, arg1))
11998 return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
12003 goto associate;
12005 case RDIV_EXPR:
12006 /* Don't touch a floating-point divide by zero unless the mode
12007 of the constant can represent infinity. */
12008 if (TREE_CODE (arg1) == REAL_CST
12009 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
12010 && real_zerop (arg1))
12011 return NULL_TREE;
12013 /* Optimize A / A to 1.0 if we don't care about
12014 NaNs or Infinities. Skip the transformation
12015 for non-real operands. */
12016 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
12017 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
12018 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
12019 && operand_equal_p (arg0, arg1, 0))
12021 tree r = build_real (TREE_TYPE (arg0), dconst1);
12023 return omit_two_operands_loc (loc, type, r, arg0, arg1);
12026 /* The complex version of the above A / A optimization. */
12027 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
12028 && operand_equal_p (arg0, arg1, 0))
12030 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
12031 if (! HONOR_NANS (TYPE_MODE (elem_type))
12032 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
12034 tree r = build_real (elem_type, dconst1);
12035 /* omit_two_operands will call fold_convert for us. */
12036 return omit_two_operands_loc (loc, type, r, arg0, arg1);
12040 /* (-A) / (-B) -> A / B */
12041 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
12042 return fold_build2_loc (loc, RDIV_EXPR, type,
12043 TREE_OPERAND (arg0, 0),
12044 negate_expr (arg1));
12045 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
12046 return fold_build2_loc (loc, RDIV_EXPR, type,
12047 negate_expr (arg0),
12048 TREE_OPERAND (arg1, 0));
12050 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
12051 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
12052 && real_onep (arg1))
12053 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12055 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
12056 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
12057 && real_minus_onep (arg1))
12058 return non_lvalue_loc (loc, fold_convert_loc (loc, type,
12059 negate_expr (arg0)));
12061 /* If ARG1 is a constant, we can convert this to a multiply by the
12062 reciprocal. This does not have the same rounding properties,
12063 so only do this if -freciprocal-math. We can actually
12064 always safely do it if ARG1 is a power of two, but it's hard to
12065 tell if it is or not in a portable manner. */
12066 if (optimize
12067 && (TREE_CODE (arg1) == REAL_CST
12068 || (TREE_CODE (arg1) == COMPLEX_CST
12069 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg1)))
12070 || (TREE_CODE (arg1) == VECTOR_CST
12071 && VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg1)))))
12073 if (flag_reciprocal_math
12074 && 0 != (tem = const_binop (code, build_one_cst (type), arg1)))
12075 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
12076 /* Find the reciprocal if optimizing and the result is exact.
12077 TODO: Complex reciprocal not implemented. */
12078 if (TREE_CODE (arg1) != COMPLEX_CST)
12080 tree inverse = exact_inverse (TREE_TYPE (arg0), arg1);
12082 if (inverse)
12083 return fold_build2_loc (loc, MULT_EXPR, type, arg0, inverse);
12086 /* Convert A/B/C to A/(B*C). */
12087 if (flag_reciprocal_math
12088 && TREE_CODE (arg0) == RDIV_EXPR)
12089 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
12090 fold_build2_loc (loc, MULT_EXPR, type,
12091 TREE_OPERAND (arg0, 1), arg1));
12093 /* Convert A/(B/C) to (A/B)*C. */
12094 if (flag_reciprocal_math
12095 && TREE_CODE (arg1) == RDIV_EXPR)
12096 return fold_build2_loc (loc, MULT_EXPR, type,
12097 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
12098 TREE_OPERAND (arg1, 0)),
12099 TREE_OPERAND (arg1, 1));
12101 /* Convert C1/(X*C2) into (C1/C2)/X. */
12102 if (flag_reciprocal_math
12103 && TREE_CODE (arg1) == MULT_EXPR
12104 && TREE_CODE (arg0) == REAL_CST
12105 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
12107 tree tem = const_binop (RDIV_EXPR, arg0,
12108 TREE_OPERAND (arg1, 1));
12109 if (tem)
12110 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
12111 TREE_OPERAND (arg1, 0));
12114 if (flag_unsafe_math_optimizations)
12116 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
12117 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
12119 /* Optimize sin(x)/cos(x) as tan(x). */
12120 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
12121 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
12122 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
12123 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
12124 CALL_EXPR_ARG (arg1, 0), 0))
12126 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
12128 if (tanfn != NULL_TREE)
12129 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
12132 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
12133 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
12134 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
12135 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
12136 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
12137 CALL_EXPR_ARG (arg1, 0), 0))
12139 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
12141 if (tanfn != NULL_TREE)
12143 tree tmp = build_call_expr_loc (loc, tanfn, 1,
12144 CALL_EXPR_ARG (arg0, 0));
12145 return fold_build2_loc (loc, RDIV_EXPR, type,
12146 build_real (type, dconst1), tmp);
12150 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
12151 NaNs or Infinities. */
12152 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
12153 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
12154 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
12156 tree arg00 = CALL_EXPR_ARG (arg0, 0);
12157 tree arg01 = CALL_EXPR_ARG (arg1, 0);
12159 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
12160 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
12161 && operand_equal_p (arg00, arg01, 0))
12163 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
12165 if (cosfn != NULL_TREE)
12166 return build_call_expr_loc (loc, cosfn, 1, arg00);
12170 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
12171 NaNs or Infinities. */
12172 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
12173 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
12174 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
12176 tree arg00 = CALL_EXPR_ARG (arg0, 0);
12177 tree arg01 = CALL_EXPR_ARG (arg1, 0);
12179 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
12180 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
12181 && operand_equal_p (arg00, arg01, 0))
12183 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
12185 if (cosfn != NULL_TREE)
12187 tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
12188 return fold_build2_loc (loc, RDIV_EXPR, type,
12189 build_real (type, dconst1),
12190 tmp);
12195 /* Optimize pow(x,c)/x as pow(x,c-1). */
12196 if (fcode0 == BUILT_IN_POW
12197 || fcode0 == BUILT_IN_POWF
12198 || fcode0 == BUILT_IN_POWL)
12200 tree arg00 = CALL_EXPR_ARG (arg0, 0);
12201 tree arg01 = CALL_EXPR_ARG (arg0, 1);
12202 if (TREE_CODE (arg01) == REAL_CST
12203 && !TREE_OVERFLOW (arg01)
12204 && operand_equal_p (arg1, arg00, 0))
12206 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
12207 REAL_VALUE_TYPE c;
12208 tree arg;
12210 c = TREE_REAL_CST (arg01);
12211 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
12212 arg = build_real (type, c);
12213 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
12217 /* Optimize a/root(b/c) into a*root(c/b). */
12218 if (BUILTIN_ROOT_P (fcode1))
12220 tree rootarg = CALL_EXPR_ARG (arg1, 0);
12222 if (TREE_CODE (rootarg) == RDIV_EXPR)
12224 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12225 tree b = TREE_OPERAND (rootarg, 0);
12226 tree c = TREE_OPERAND (rootarg, 1);
12228 tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
12230 tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
12231 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
12235 /* Optimize x/expN(y) into x*expN(-y). */
12236 if (BUILTIN_EXPONENT_P (fcode1))
12238 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12239 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
12240 arg1 = build_call_expr_loc (loc,
12241 expfn, 1,
12242 fold_convert_loc (loc, type, arg));
12243 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12246 /* Optimize x/pow(y,z) into x*pow(y,-z). */
12247 if (fcode1 == BUILT_IN_POW
12248 || fcode1 == BUILT_IN_POWF
12249 || fcode1 == BUILT_IN_POWL)
12251 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12252 tree arg10 = CALL_EXPR_ARG (arg1, 0);
12253 tree arg11 = CALL_EXPR_ARG (arg1, 1);
12254 tree neg11 = fold_convert_loc (loc, type,
12255 negate_expr (arg11));
12256 arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
12257 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12260 return NULL_TREE;
12262 case TRUNC_DIV_EXPR:
12263 /* Optimize (X & (-A)) / A where A is a power of 2,
12264 to X >> log2(A) */
12265 if (TREE_CODE (arg0) == BIT_AND_EXPR
12266 && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
12267 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
12269 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
12270 arg1, TREE_OPERAND (arg0, 1));
12271 if (sum && integer_zerop (sum)) {
12272 unsigned long pow2;
12274 if (TREE_INT_CST_LOW (arg1))
12275 pow2 = exact_log2 (TREE_INT_CST_LOW (arg1));
12276 else
12277 pow2 = exact_log2 (TREE_INT_CST_HIGH (arg1))
12278 + HOST_BITS_PER_WIDE_INT;
12280 return fold_build2_loc (loc, RSHIFT_EXPR, type,
12281 TREE_OPERAND (arg0, 0),
12282 build_int_cst (integer_type_node, pow2));
12286 /* Fall through */
12288 case FLOOR_DIV_EXPR:
12289 /* Simplify A / (B << N) where A and B are positive and B is
12290 a power of 2, to A >> (N + log2(B)). */
12291 strict_overflow_p = false;
12292 if (TREE_CODE (arg1) == LSHIFT_EXPR
12293 && (TYPE_UNSIGNED (type)
12294 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12296 tree sval = TREE_OPERAND (arg1, 0);
12297 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
12299 tree sh_cnt = TREE_OPERAND (arg1, 1);
12300 unsigned long pow2;
12302 if (TREE_INT_CST_LOW (sval))
12303 pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
12304 else
12305 pow2 = exact_log2 (TREE_INT_CST_HIGH (sval))
12306 + HOST_BITS_PER_WIDE_INT;
12308 if (strict_overflow_p)
12309 fold_overflow_warning (("assuming signed overflow does not "
12310 "occur when simplifying A / (B << N)"),
12311 WARN_STRICT_OVERFLOW_MISC);
12313 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
12314 sh_cnt,
12315 build_int_cst (TREE_TYPE (sh_cnt),
12316 pow2));
12317 return fold_build2_loc (loc, RSHIFT_EXPR, type,
12318 fold_convert_loc (loc, type, arg0), sh_cnt);
12322 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
12323 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
12324 if (INTEGRAL_TYPE_P (type)
12325 && TYPE_UNSIGNED (type)
12326 && code == FLOOR_DIV_EXPR)
12327 return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
12329 /* Fall through */
12331 case ROUND_DIV_EXPR:
12332 case CEIL_DIV_EXPR:
12333 case EXACT_DIV_EXPR:
12334 if (integer_onep (arg1))
12335 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12336 if (integer_zerop (arg1))
12337 return NULL_TREE;
12338 /* X / -1 is -X. */
12339 if (!TYPE_UNSIGNED (type)
12340 && TREE_CODE (arg1) == INTEGER_CST
12341 && TREE_INT_CST_LOW (arg1) == HOST_WIDE_INT_M1U
12342 && TREE_INT_CST_HIGH (arg1) == -1)
12343 return fold_convert_loc (loc, type, negate_expr (arg0));
12345 /* Convert -A / -B to A / B when the type is signed and overflow is
12346 undefined. */
12347 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12348 && TREE_CODE (arg0) == NEGATE_EXPR
12349 && negate_expr_p (arg1))
12351 if (INTEGRAL_TYPE_P (type))
12352 fold_overflow_warning (("assuming signed overflow does not occur "
12353 "when distributing negation across "
12354 "division"),
12355 WARN_STRICT_OVERFLOW_MISC);
12356 return fold_build2_loc (loc, code, type,
12357 fold_convert_loc (loc, type,
12358 TREE_OPERAND (arg0, 0)),
12359 fold_convert_loc (loc, type,
12360 negate_expr (arg1)));
12362 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12363 && TREE_CODE (arg1) == NEGATE_EXPR
12364 && negate_expr_p (arg0))
12366 if (INTEGRAL_TYPE_P (type))
12367 fold_overflow_warning (("assuming signed overflow does not occur "
12368 "when distributing negation across "
12369 "division"),
12370 WARN_STRICT_OVERFLOW_MISC);
12371 return fold_build2_loc (loc, code, type,
12372 fold_convert_loc (loc, type,
12373 negate_expr (arg0)),
12374 fold_convert_loc (loc, type,
12375 TREE_OPERAND (arg1, 0)));
12378 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12379 operation, EXACT_DIV_EXPR.
12381 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12382 At one time others generated faster code, it's not clear if they do
12383 after the last round to changes to the DIV code in expmed.c. */
12384 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12385 && multiple_of_p (type, arg0, arg1))
12386 return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
12388 strict_overflow_p = false;
12389 if (TREE_CODE (arg1) == INTEGER_CST
12390 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12391 &strict_overflow_p)))
12393 if (strict_overflow_p)
12394 fold_overflow_warning (("assuming signed overflow does not occur "
12395 "when simplifying division"),
12396 WARN_STRICT_OVERFLOW_MISC);
12397 return fold_convert_loc (loc, type, tem);
12400 return NULL_TREE;
12402 case CEIL_MOD_EXPR:
12403 case FLOOR_MOD_EXPR:
12404 case ROUND_MOD_EXPR:
12405 case TRUNC_MOD_EXPR:
12406 /* X % 1 is always zero, but be sure to preserve any side
12407 effects in X. */
12408 if (integer_onep (arg1))
12409 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12411 /* X % 0, return X % 0 unchanged so that we can get the
12412 proper warnings and errors. */
12413 if (integer_zerop (arg1))
12414 return NULL_TREE;
12416 /* 0 % X is always zero, but be sure to preserve any side
12417 effects in X. Place this after checking for X == 0. */
12418 if (integer_zerop (arg0))
12419 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12421 /* X % -1 is zero. */
12422 if (!TYPE_UNSIGNED (type)
12423 && TREE_CODE (arg1) == INTEGER_CST
12424 && TREE_INT_CST_LOW (arg1) == HOST_WIDE_INT_M1U
12425 && TREE_INT_CST_HIGH (arg1) == -1)
12426 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12428 /* X % -C is the same as X % C. */
12429 if (code == TRUNC_MOD_EXPR
12430 && !TYPE_UNSIGNED (type)
12431 && TREE_CODE (arg1) == INTEGER_CST
12432 && !TREE_OVERFLOW (arg1)
12433 && TREE_INT_CST_HIGH (arg1) < 0
12434 && !TYPE_OVERFLOW_TRAPS (type)
12435 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
12436 && !sign_bit_p (arg1, arg1))
12437 return fold_build2_loc (loc, code, type,
12438 fold_convert_loc (loc, type, arg0),
12439 fold_convert_loc (loc, type,
12440 negate_expr (arg1)));
12442 /* X % -Y is the same as X % Y. */
12443 if (code == TRUNC_MOD_EXPR
12444 && !TYPE_UNSIGNED (type)
12445 && TREE_CODE (arg1) == NEGATE_EXPR
12446 && !TYPE_OVERFLOW_TRAPS (type))
12447 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
12448 fold_convert_loc (loc, type,
12449 TREE_OPERAND (arg1, 0)));
12451 strict_overflow_p = false;
12452 if (TREE_CODE (arg1) == INTEGER_CST
12453 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12454 &strict_overflow_p)))
12456 if (strict_overflow_p)
12457 fold_overflow_warning (("assuming signed overflow does not occur "
12458 "when simplifying modulus"),
12459 WARN_STRICT_OVERFLOW_MISC);
12460 return fold_convert_loc (loc, type, tem);
12463 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
12464 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
12465 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
12466 && (TYPE_UNSIGNED (type)
12467 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12469 tree c = arg1;
12470 /* Also optimize A % (C << N) where C is a power of 2,
12471 to A & ((C << N) - 1). */
12472 if (TREE_CODE (arg1) == LSHIFT_EXPR)
12473 c = TREE_OPERAND (arg1, 0);
12475 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
12477 tree mask
12478 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
12479 build_int_cst (TREE_TYPE (arg1), 1));
12480 if (strict_overflow_p)
12481 fold_overflow_warning (("assuming signed overflow does not "
12482 "occur when simplifying "
12483 "X % (power of two)"),
12484 WARN_STRICT_OVERFLOW_MISC);
12485 return fold_build2_loc (loc, BIT_AND_EXPR, type,
12486 fold_convert_loc (loc, type, arg0),
12487 fold_convert_loc (loc, type, mask));
12491 return NULL_TREE;
12493 case LROTATE_EXPR:
12494 case RROTATE_EXPR:
12495 if (integer_all_onesp (arg0))
12496 return omit_one_operand_loc (loc, type, arg0, arg1);
12497 goto shift;
12499 case RSHIFT_EXPR:
12500 /* Optimize -1 >> x for arithmetic right shifts. */
12501 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
12502 && tree_expr_nonnegative_p (arg1))
12503 return omit_one_operand_loc (loc, type, arg0, arg1);
12504 /* ... fall through ... */
12506 case LSHIFT_EXPR:
12507 shift:
12508 if (integer_zerop (arg1))
12509 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12510 if (integer_zerop (arg0))
12511 return omit_one_operand_loc (loc, type, arg0, arg1);
12513 /* Prefer vector1 << scalar to vector1 << vector2
12514 if vector2 is uniform. */
12515 if (VECTOR_TYPE_P (TREE_TYPE (arg1))
12516 && (tem = uniform_vector_p (arg1)) != NULL_TREE)
12517 return fold_build2_loc (loc, code, type, op0, tem);
12519 /* Since negative shift count is not well-defined,
12520 don't try to compute it in the compiler. */
12521 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12522 return NULL_TREE;
12524 prec = element_precision (type);
12526 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
12527 if (TREE_CODE (op0) == code && host_integerp (arg1, true)
12528 && TREE_INT_CST_LOW (arg1) < prec
12529 && host_integerp (TREE_OPERAND (arg0, 1), true)
12530 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < prec)
12532 unsigned int low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
12533 + TREE_INT_CST_LOW (arg1));
12535 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
12536 being well defined. */
12537 if (low >= prec)
12539 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
12540 low = low % prec;
12541 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
12542 return omit_one_operand_loc (loc, type, build_zero_cst (type),
12543 TREE_OPERAND (arg0, 0));
12544 else
12545 low = prec - 1;
12548 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12549 build_int_cst (TREE_TYPE (arg1), low));
12552 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
12553 into x & ((unsigned)-1 >> c) for unsigned types. */
12554 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
12555 || (TYPE_UNSIGNED (type)
12556 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
12557 && host_integerp (arg1, false)
12558 && TREE_INT_CST_LOW (arg1) < prec
12559 && host_integerp (TREE_OPERAND (arg0, 1), false)
12560 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < prec)
12562 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
12563 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
12564 tree lshift;
12565 tree arg00;
12567 if (low0 == low1)
12569 arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12571 lshift = build_minus_one_cst (type);
12572 lshift = const_binop (code, lshift, arg1);
12574 return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
12578 /* Rewrite an LROTATE_EXPR by a constant into an
12579 RROTATE_EXPR by a new constant. */
12580 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
12582 tree tem = build_int_cst (TREE_TYPE (arg1), prec);
12583 tem = const_binop (MINUS_EXPR, tem, arg1);
12584 return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
12587 /* If we have a rotate of a bit operation with the rotate count and
12588 the second operand of the bit operation both constant,
12589 permute the two operations. */
12590 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12591 && (TREE_CODE (arg0) == BIT_AND_EXPR
12592 || TREE_CODE (arg0) == BIT_IOR_EXPR
12593 || TREE_CODE (arg0) == BIT_XOR_EXPR)
12594 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12595 return fold_build2_loc (loc, TREE_CODE (arg0), type,
12596 fold_build2_loc (loc, code, type,
12597 TREE_OPERAND (arg0, 0), arg1),
12598 fold_build2_loc (loc, code, type,
12599 TREE_OPERAND (arg0, 1), arg1));
12601 /* Two consecutive rotates adding up to the precision of the
12602 type can be ignored. */
12603 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12604 && TREE_CODE (arg0) == RROTATE_EXPR
12605 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12606 && TREE_INT_CST_HIGH (arg1) == 0
12607 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
12608 && ((TREE_INT_CST_LOW (arg1)
12609 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
12610 == prec))
12611 return TREE_OPERAND (arg0, 0);
12613 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
12614 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
12615 if the latter can be further optimized. */
12616 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
12617 && TREE_CODE (arg0) == BIT_AND_EXPR
12618 && TREE_CODE (arg1) == INTEGER_CST
12619 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12621 tree mask = fold_build2_loc (loc, code, type,
12622 fold_convert_loc (loc, type,
12623 TREE_OPERAND (arg0, 1)),
12624 arg1);
12625 tree shift = fold_build2_loc (loc, code, type,
12626 fold_convert_loc (loc, type,
12627 TREE_OPERAND (arg0, 0)),
12628 arg1);
12629 tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
12630 if (tem)
12631 return tem;
12634 return NULL_TREE;
12636 case MIN_EXPR:
12637 if (operand_equal_p (arg0, arg1, 0))
12638 return omit_one_operand_loc (loc, type, arg0, arg1);
12639 if (INTEGRAL_TYPE_P (type)
12640 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
12641 return omit_one_operand_loc (loc, type, arg1, arg0);
12642 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
12643 if (tem)
12644 return tem;
12645 goto associate;
12647 case MAX_EXPR:
12648 if (operand_equal_p (arg0, arg1, 0))
12649 return omit_one_operand_loc (loc, type, arg0, arg1);
12650 if (INTEGRAL_TYPE_P (type)
12651 && TYPE_MAX_VALUE (type)
12652 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
12653 return omit_one_operand_loc (loc, type, arg1, arg0);
12654 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
12655 if (tem)
12656 return tem;
12657 goto associate;
12659 case TRUTH_ANDIF_EXPR:
12660 /* Note that the operands of this must be ints
12661 and their values must be 0 or 1.
12662 ("true" is a fixed value perhaps depending on the language.) */
12663 /* If first arg is constant zero, return it. */
12664 if (integer_zerop (arg0))
12665 return fold_convert_loc (loc, type, arg0);
12666 case TRUTH_AND_EXPR:
12667 /* If either arg is constant true, drop it. */
12668 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12669 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12670 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12671 /* Preserve sequence points. */
12672 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12673 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12674 /* If second arg is constant zero, result is zero, but first arg
12675 must be evaluated. */
12676 if (integer_zerop (arg1))
12677 return omit_one_operand_loc (loc, type, arg1, arg0);
12678 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12679 case will be handled here. */
12680 if (integer_zerop (arg0))
12681 return omit_one_operand_loc (loc, type, arg0, arg1);
12683 /* !X && X is always false. */
12684 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12685 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12686 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12687 /* X && !X is always false. */
12688 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12689 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12690 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12692 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12693 means A >= Y && A != MAX, but in this case we know that
12694 A < X <= MAX. */
12696 if (!TREE_SIDE_EFFECTS (arg0)
12697 && !TREE_SIDE_EFFECTS (arg1))
12699 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12700 if (tem && !operand_equal_p (tem, arg0, 0))
12701 return fold_build2_loc (loc, code, type, tem, arg1);
12703 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12704 if (tem && !operand_equal_p (tem, arg1, 0))
12705 return fold_build2_loc (loc, code, type, arg0, tem);
12708 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12709 != NULL_TREE)
12710 return tem;
12712 return NULL_TREE;
12714 case TRUTH_ORIF_EXPR:
12715 /* Note that the operands of this must be ints
12716 and their values must be 0 or true.
12717 ("true" is a fixed value perhaps depending on the language.) */
12718 /* If first arg is constant true, return it. */
12719 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12720 return fold_convert_loc (loc, type, arg0);
12721 case TRUTH_OR_EXPR:
12722 /* If either arg is constant zero, drop it. */
12723 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12724 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12725 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12726 /* Preserve sequence points. */
12727 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12728 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12729 /* If second arg is constant true, result is true, but we must
12730 evaluate first arg. */
12731 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12732 return omit_one_operand_loc (loc, type, arg1, arg0);
12733 /* Likewise for first arg, but note this only occurs here for
12734 TRUTH_OR_EXPR. */
12735 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12736 return omit_one_operand_loc (loc, type, arg0, arg1);
12738 /* !X || X is always true. */
12739 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12740 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12741 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12742 /* X || !X is always true. */
12743 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12744 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12745 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12747 /* (X && !Y) || (!X && Y) is X ^ Y */
12748 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12749 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12751 tree a0, a1, l0, l1, n0, n1;
12753 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12754 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12756 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12757 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12759 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12760 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12762 if ((operand_equal_p (n0, a0, 0)
12763 && operand_equal_p (n1, a1, 0))
12764 || (operand_equal_p (n0, a1, 0)
12765 && operand_equal_p (n1, a0, 0)))
12766 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12769 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12770 != NULL_TREE)
12771 return tem;
12773 return NULL_TREE;
12775 case TRUTH_XOR_EXPR:
12776 /* If the second arg is constant zero, drop it. */
12777 if (integer_zerop (arg1))
12778 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12779 /* If the second arg is constant true, this is a logical inversion. */
12780 if (integer_onep (arg1))
12782 tem = invert_truthvalue_loc (loc, arg0);
12783 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12785 /* Identical arguments cancel to zero. */
12786 if (operand_equal_p (arg0, arg1, 0))
12787 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12789 /* !X ^ X is always true. */
12790 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12791 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12792 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12794 /* X ^ !X is always true. */
12795 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12796 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12797 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12799 return NULL_TREE;
12801 case EQ_EXPR:
12802 case NE_EXPR:
12803 STRIP_NOPS (arg0);
12804 STRIP_NOPS (arg1);
12806 tem = fold_comparison (loc, code, type, op0, op1);
12807 if (tem != NULL_TREE)
12808 return tem;
12810 /* bool_var != 0 becomes bool_var. */
12811 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12812 && code == NE_EXPR)
12813 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12815 /* bool_var == 1 becomes bool_var. */
12816 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12817 && code == EQ_EXPR)
12818 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12820 /* bool_var != 1 becomes !bool_var. */
12821 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12822 && code == NE_EXPR)
12823 return fold_convert_loc (loc, type,
12824 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12825 TREE_TYPE (arg0), arg0));
12827 /* bool_var == 0 becomes !bool_var. */
12828 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12829 && code == EQ_EXPR)
12830 return fold_convert_loc (loc, type,
12831 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12832 TREE_TYPE (arg0), arg0));
12834 /* !exp != 0 becomes !exp */
12835 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12836 && code == NE_EXPR)
12837 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12839 /* If this is an equality comparison of the address of two non-weak,
12840 unaliased symbols neither of which are extern (since we do not
12841 have access to attributes for externs), then we know the result. */
12842 if (TREE_CODE (arg0) == ADDR_EXPR
12843 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12844 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12845 && ! lookup_attribute ("alias",
12846 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12847 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12848 && TREE_CODE (arg1) == ADDR_EXPR
12849 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12850 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12851 && ! lookup_attribute ("alias",
12852 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12853 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12855 /* We know that we're looking at the address of two
12856 non-weak, unaliased, static _DECL nodes.
12858 It is both wasteful and incorrect to call operand_equal_p
12859 to compare the two ADDR_EXPR nodes. It is wasteful in that
12860 all we need to do is test pointer equality for the arguments
12861 to the two ADDR_EXPR nodes. It is incorrect to use
12862 operand_equal_p as that function is NOT equivalent to a
12863 C equality test. It can in fact return false for two
12864 objects which would test as equal using the C equality
12865 operator. */
12866 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12867 return constant_boolean_node (equal
12868 ? code == EQ_EXPR : code != EQ_EXPR,
12869 type);
12872 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12873 a MINUS_EXPR of a constant, we can convert it into a comparison with
12874 a revised constant as long as no overflow occurs. */
12875 if (TREE_CODE (arg1) == INTEGER_CST
12876 && (TREE_CODE (arg0) == PLUS_EXPR
12877 || TREE_CODE (arg0) == MINUS_EXPR)
12878 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12879 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12880 ? MINUS_EXPR : PLUS_EXPR,
12881 fold_convert_loc (loc, TREE_TYPE (arg0),
12882 arg1),
12883 TREE_OPERAND (arg0, 1)))
12884 && !TREE_OVERFLOW (tem))
12885 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12887 /* Similarly for a NEGATE_EXPR. */
12888 if (TREE_CODE (arg0) == NEGATE_EXPR
12889 && TREE_CODE (arg1) == INTEGER_CST
12890 && 0 != (tem = negate_expr (fold_convert_loc (loc, TREE_TYPE (arg0),
12891 arg1)))
12892 && TREE_CODE (tem) == INTEGER_CST
12893 && !TREE_OVERFLOW (tem))
12894 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12896 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12897 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12898 && TREE_CODE (arg1) == INTEGER_CST
12899 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12900 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12901 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12902 fold_convert_loc (loc,
12903 TREE_TYPE (arg0),
12904 arg1),
12905 TREE_OPERAND (arg0, 1)));
12907 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
12908 if ((TREE_CODE (arg0) == PLUS_EXPR
12909 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12910 || TREE_CODE (arg0) == MINUS_EXPR)
12911 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12912 0)),
12913 arg1, 0)
12914 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12915 || POINTER_TYPE_P (TREE_TYPE (arg0))))
12917 tree val = TREE_OPERAND (arg0, 1);
12918 return omit_two_operands_loc (loc, type,
12919 fold_build2_loc (loc, code, type,
12920 val,
12921 build_int_cst (TREE_TYPE (val),
12922 0)),
12923 TREE_OPERAND (arg0, 0), arg1);
12926 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
12927 if (TREE_CODE (arg0) == MINUS_EXPR
12928 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12929 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
12930 1)),
12931 arg1, 0)
12932 && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12934 return omit_two_operands_loc (loc, type,
12935 code == NE_EXPR
12936 ? boolean_true_node : boolean_false_node,
12937 TREE_OPERAND (arg0, 1), arg1);
12940 /* If we have X - Y == 0, we can convert that to X == Y and similarly
12941 for !=. Don't do this for ordered comparisons due to overflow. */
12942 if (TREE_CODE (arg0) == MINUS_EXPR
12943 && integer_zerop (arg1))
12944 return fold_build2_loc (loc, code, type,
12945 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12947 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12948 if (TREE_CODE (arg0) == ABS_EXPR
12949 && (integer_zerop (arg1) || real_zerop (arg1)))
12950 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12952 /* If this is an EQ or NE comparison with zero and ARG0 is
12953 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12954 two operations, but the latter can be done in one less insn
12955 on machines that have only two-operand insns or on which a
12956 constant cannot be the first operand. */
12957 if (TREE_CODE (arg0) == BIT_AND_EXPR
12958 && integer_zerop (arg1))
12960 tree arg00 = TREE_OPERAND (arg0, 0);
12961 tree arg01 = TREE_OPERAND (arg0, 1);
12962 if (TREE_CODE (arg00) == LSHIFT_EXPR
12963 && integer_onep (TREE_OPERAND (arg00, 0)))
12965 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12966 arg01, TREE_OPERAND (arg00, 1));
12967 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12968 build_int_cst (TREE_TYPE (arg0), 1));
12969 return fold_build2_loc (loc, code, type,
12970 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12971 arg1);
12973 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12974 && integer_onep (TREE_OPERAND (arg01, 0)))
12976 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12977 arg00, TREE_OPERAND (arg01, 1));
12978 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12979 build_int_cst (TREE_TYPE (arg0), 1));
12980 return fold_build2_loc (loc, code, type,
12981 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12982 arg1);
12986 /* If this is an NE or EQ comparison of zero against the result of a
12987 signed MOD operation whose second operand is a power of 2, make
12988 the MOD operation unsigned since it is simpler and equivalent. */
12989 if (integer_zerop (arg1)
12990 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12991 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12992 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12993 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12994 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12995 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12997 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12998 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12999 fold_convert_loc (loc, newtype,
13000 TREE_OPERAND (arg0, 0)),
13001 fold_convert_loc (loc, newtype,
13002 TREE_OPERAND (arg0, 1)));
13004 return fold_build2_loc (loc, code, type, newmod,
13005 fold_convert_loc (loc, newtype, arg1));
13008 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
13009 C1 is a valid shift constant, and C2 is a power of two, i.e.
13010 a single bit. */
13011 if (TREE_CODE (arg0) == BIT_AND_EXPR
13012 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
13013 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
13014 == INTEGER_CST
13015 && integer_pow2p (TREE_OPERAND (arg0, 1))
13016 && integer_zerop (arg1))
13018 tree itype = TREE_TYPE (arg0);
13019 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
13020 prec = TYPE_PRECISION (itype);
13022 /* Check for a valid shift count. */
13023 if (TREE_INT_CST_HIGH (arg001) == 0
13024 && TREE_INT_CST_LOW (arg001) < prec)
13026 tree arg01 = TREE_OPERAND (arg0, 1);
13027 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
13028 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
13029 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
13030 can be rewritten as (X & (C2 << C1)) != 0. */
13031 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
13033 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
13034 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
13035 return fold_build2_loc (loc, code, type, tem,
13036 fold_convert_loc (loc, itype, arg1));
13038 /* Otherwise, for signed (arithmetic) shifts,
13039 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
13040 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
13041 else if (!TYPE_UNSIGNED (itype))
13042 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
13043 arg000, build_int_cst (itype, 0));
13044 /* Otherwise, of unsigned (logical) shifts,
13045 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
13046 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
13047 else
13048 return omit_one_operand_loc (loc, type,
13049 code == EQ_EXPR ? integer_one_node
13050 : integer_zero_node,
13051 arg000);
13055 /* If we have (A & C) == C where C is a power of 2, convert this into
13056 (A & C) != 0. Similarly for NE_EXPR. */
13057 if (TREE_CODE (arg0) == BIT_AND_EXPR
13058 && integer_pow2p (TREE_OPERAND (arg0, 1))
13059 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
13060 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
13061 arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
13062 integer_zero_node));
13064 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
13065 bit, then fold the expression into A < 0 or A >= 0. */
13066 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
13067 if (tem)
13068 return tem;
13070 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
13071 Similarly for NE_EXPR. */
13072 if (TREE_CODE (arg0) == BIT_AND_EXPR
13073 && TREE_CODE (arg1) == INTEGER_CST
13074 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
13076 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
13077 TREE_TYPE (TREE_OPERAND (arg0, 1)),
13078 TREE_OPERAND (arg0, 1));
13079 tree dandnotc
13080 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
13081 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
13082 notc);
13083 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
13084 if (integer_nonzerop (dandnotc))
13085 return omit_one_operand_loc (loc, type, rslt, arg0);
13088 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
13089 Similarly for NE_EXPR. */
13090 if (TREE_CODE (arg0) == BIT_IOR_EXPR
13091 && TREE_CODE (arg1) == INTEGER_CST
13092 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
13094 tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
13095 tree candnotd
13096 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
13097 TREE_OPERAND (arg0, 1),
13098 fold_convert_loc (loc, TREE_TYPE (arg0), notd));
13099 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
13100 if (integer_nonzerop (candnotd))
13101 return omit_one_operand_loc (loc, type, rslt, arg0);
13104 /* If this is a comparison of a field, we may be able to simplify it. */
13105 if ((TREE_CODE (arg0) == COMPONENT_REF
13106 || TREE_CODE (arg0) == BIT_FIELD_REF)
13107 /* Handle the constant case even without -O
13108 to make sure the warnings are given. */
13109 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
13111 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
13112 if (t1)
13113 return t1;
13116 /* Optimize comparisons of strlen vs zero to a compare of the
13117 first character of the string vs zero. To wit,
13118 strlen(ptr) == 0 => *ptr == 0
13119 strlen(ptr) != 0 => *ptr != 0
13120 Other cases should reduce to one of these two (or a constant)
13121 due to the return value of strlen being unsigned. */
13122 if (TREE_CODE (arg0) == CALL_EXPR
13123 && integer_zerop (arg1))
13125 tree fndecl = get_callee_fndecl (arg0);
13127 if (fndecl
13128 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
13129 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
13130 && call_expr_nargs (arg0) == 1
13131 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
13133 tree iref = build_fold_indirect_ref_loc (loc,
13134 CALL_EXPR_ARG (arg0, 0));
13135 return fold_build2_loc (loc, code, type, iref,
13136 build_int_cst (TREE_TYPE (iref), 0));
13140 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
13141 of X. Similarly fold (X >> C) == 0 into X >= 0. */
13142 if (TREE_CODE (arg0) == RSHIFT_EXPR
13143 && integer_zerop (arg1)
13144 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
13146 tree arg00 = TREE_OPERAND (arg0, 0);
13147 tree arg01 = TREE_OPERAND (arg0, 1);
13148 tree itype = TREE_TYPE (arg00);
13149 if (TREE_INT_CST_HIGH (arg01) == 0
13150 && TREE_INT_CST_LOW (arg01)
13151 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
13153 if (TYPE_UNSIGNED (itype))
13155 itype = signed_type_for (itype);
13156 arg00 = fold_convert_loc (loc, itype, arg00);
13158 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
13159 type, arg00, build_zero_cst (itype));
13163 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
13164 if (integer_zerop (arg1)
13165 && TREE_CODE (arg0) == BIT_XOR_EXPR)
13166 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
13167 TREE_OPERAND (arg0, 1));
13169 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
13170 if (TREE_CODE (arg0) == BIT_XOR_EXPR
13171 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
13172 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
13173 build_zero_cst (TREE_TYPE (arg0)));
13174 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
13175 if (TREE_CODE (arg0) == BIT_XOR_EXPR
13176 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
13177 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
13178 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
13179 build_zero_cst (TREE_TYPE (arg0)));
13181 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
13182 if (TREE_CODE (arg0) == BIT_XOR_EXPR
13183 && TREE_CODE (arg1) == INTEGER_CST
13184 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
13185 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
13186 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
13187 TREE_OPERAND (arg0, 1), arg1));
13189 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
13190 (X & C) == 0 when C is a single bit. */
13191 if (TREE_CODE (arg0) == BIT_AND_EXPR
13192 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
13193 && integer_zerop (arg1)
13194 && integer_pow2p (TREE_OPERAND (arg0, 1)))
13196 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
13197 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
13198 TREE_OPERAND (arg0, 1));
13199 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
13200 type, tem,
13201 fold_convert_loc (loc, TREE_TYPE (arg0),
13202 arg1));
13205 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
13206 constant C is a power of two, i.e. a single bit. */
13207 if (TREE_CODE (arg0) == BIT_XOR_EXPR
13208 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13209 && integer_zerop (arg1)
13210 && integer_pow2p (TREE_OPERAND (arg0, 1))
13211 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13212 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
13214 tree arg00 = TREE_OPERAND (arg0, 0);
13215 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
13216 arg00, build_int_cst (TREE_TYPE (arg00), 0));
13219 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
13220 when is C is a power of two, i.e. a single bit. */
13221 if (TREE_CODE (arg0) == BIT_AND_EXPR
13222 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
13223 && integer_zerop (arg1)
13224 && integer_pow2p (TREE_OPERAND (arg0, 1))
13225 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13226 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
13228 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
13229 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
13230 arg000, TREE_OPERAND (arg0, 1));
13231 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
13232 tem, build_int_cst (TREE_TYPE (tem), 0));
13235 if (integer_zerop (arg1)
13236 && tree_expr_nonzero_p (arg0))
13238 tree res = constant_boolean_node (code==NE_EXPR, type);
13239 return omit_one_operand_loc (loc, type, res, arg0);
13242 /* Fold -X op -Y as X op Y, where op is eq/ne. */
13243 if (TREE_CODE (arg0) == NEGATE_EXPR
13244 && TREE_CODE (arg1) == NEGATE_EXPR)
13245 return fold_build2_loc (loc, code, type,
13246 TREE_OPERAND (arg0, 0),
13247 fold_convert_loc (loc, TREE_TYPE (arg0),
13248 TREE_OPERAND (arg1, 0)));
13250 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
13251 if (TREE_CODE (arg0) == BIT_AND_EXPR
13252 && TREE_CODE (arg1) == BIT_AND_EXPR)
13254 tree arg00 = TREE_OPERAND (arg0, 0);
13255 tree arg01 = TREE_OPERAND (arg0, 1);
13256 tree arg10 = TREE_OPERAND (arg1, 0);
13257 tree arg11 = TREE_OPERAND (arg1, 1);
13258 tree itype = TREE_TYPE (arg0);
13260 if (operand_equal_p (arg01, arg11, 0))
13261 return fold_build2_loc (loc, code, type,
13262 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13263 fold_build2_loc (loc,
13264 BIT_XOR_EXPR, itype,
13265 arg00, arg10),
13266 arg01),
13267 build_zero_cst (itype));
13269 if (operand_equal_p (arg01, arg10, 0))
13270 return fold_build2_loc (loc, code, type,
13271 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13272 fold_build2_loc (loc,
13273 BIT_XOR_EXPR, itype,
13274 arg00, arg11),
13275 arg01),
13276 build_zero_cst (itype));
13278 if (operand_equal_p (arg00, arg11, 0))
13279 return fold_build2_loc (loc, code, type,
13280 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13281 fold_build2_loc (loc,
13282 BIT_XOR_EXPR, itype,
13283 arg01, arg10),
13284 arg00),
13285 build_zero_cst (itype));
13287 if (operand_equal_p (arg00, arg10, 0))
13288 return fold_build2_loc (loc, code, type,
13289 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13290 fold_build2_loc (loc,
13291 BIT_XOR_EXPR, itype,
13292 arg01, arg11),
13293 arg00),
13294 build_zero_cst (itype));
13297 if (TREE_CODE (arg0) == BIT_XOR_EXPR
13298 && TREE_CODE (arg1) == BIT_XOR_EXPR)
13300 tree arg00 = TREE_OPERAND (arg0, 0);
13301 tree arg01 = TREE_OPERAND (arg0, 1);
13302 tree arg10 = TREE_OPERAND (arg1, 0);
13303 tree arg11 = TREE_OPERAND (arg1, 1);
13304 tree itype = TREE_TYPE (arg0);
13306 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
13307 operand_equal_p guarantees no side-effects so we don't need
13308 to use omit_one_operand on Z. */
13309 if (operand_equal_p (arg01, arg11, 0))
13310 return fold_build2_loc (loc, code, type, arg00,
13311 fold_convert_loc (loc, TREE_TYPE (arg00),
13312 arg10));
13313 if (operand_equal_p (arg01, arg10, 0))
13314 return fold_build2_loc (loc, code, type, arg00,
13315 fold_convert_loc (loc, TREE_TYPE (arg00),
13316 arg11));
13317 if (operand_equal_p (arg00, arg11, 0))
13318 return fold_build2_loc (loc, code, type, arg01,
13319 fold_convert_loc (loc, TREE_TYPE (arg01),
13320 arg10));
13321 if (operand_equal_p (arg00, arg10, 0))
13322 return fold_build2_loc (loc, code, type, arg01,
13323 fold_convert_loc (loc, TREE_TYPE (arg01),
13324 arg11));
13326 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
13327 if (TREE_CODE (arg01) == INTEGER_CST
13328 && TREE_CODE (arg11) == INTEGER_CST)
13330 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
13331 fold_convert_loc (loc, itype, arg11));
13332 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
13333 return fold_build2_loc (loc, code, type, tem,
13334 fold_convert_loc (loc, itype, arg10));
13338 /* Attempt to simplify equality/inequality comparisons of complex
13339 values. Only lower the comparison if the result is known or
13340 can be simplified to a single scalar comparison. */
13341 if ((TREE_CODE (arg0) == COMPLEX_EXPR
13342 || TREE_CODE (arg0) == COMPLEX_CST)
13343 && (TREE_CODE (arg1) == COMPLEX_EXPR
13344 || TREE_CODE (arg1) == COMPLEX_CST))
13346 tree real0, imag0, real1, imag1;
13347 tree rcond, icond;
13349 if (TREE_CODE (arg0) == COMPLEX_EXPR)
13351 real0 = TREE_OPERAND (arg0, 0);
13352 imag0 = TREE_OPERAND (arg0, 1);
13354 else
13356 real0 = TREE_REALPART (arg0);
13357 imag0 = TREE_IMAGPART (arg0);
13360 if (TREE_CODE (arg1) == COMPLEX_EXPR)
13362 real1 = TREE_OPERAND (arg1, 0);
13363 imag1 = TREE_OPERAND (arg1, 1);
13365 else
13367 real1 = TREE_REALPART (arg1);
13368 imag1 = TREE_IMAGPART (arg1);
13371 rcond = fold_binary_loc (loc, code, type, real0, real1);
13372 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
13374 if (integer_zerop (rcond))
13376 if (code == EQ_EXPR)
13377 return omit_two_operands_loc (loc, type, boolean_false_node,
13378 imag0, imag1);
13379 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
13381 else
13383 if (code == NE_EXPR)
13384 return omit_two_operands_loc (loc, type, boolean_true_node,
13385 imag0, imag1);
13386 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
13390 icond = fold_binary_loc (loc, code, type, imag0, imag1);
13391 if (icond && TREE_CODE (icond) == INTEGER_CST)
13393 if (integer_zerop (icond))
13395 if (code == EQ_EXPR)
13396 return omit_two_operands_loc (loc, type, boolean_false_node,
13397 real0, real1);
13398 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
13400 else
13402 if (code == NE_EXPR)
13403 return omit_two_operands_loc (loc, type, boolean_true_node,
13404 real0, real1);
13405 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
13410 return NULL_TREE;
13412 case LT_EXPR:
13413 case GT_EXPR:
13414 case LE_EXPR:
13415 case GE_EXPR:
13416 tem = fold_comparison (loc, code, type, op0, op1);
13417 if (tem != NULL_TREE)
13418 return tem;
13420 /* Transform comparisons of the form X +- C CMP X. */
13421 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
13422 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
13423 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
13424 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
13425 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
13426 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
13428 tree arg01 = TREE_OPERAND (arg0, 1);
13429 enum tree_code code0 = TREE_CODE (arg0);
13430 int is_positive;
13432 if (TREE_CODE (arg01) == REAL_CST)
13433 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
13434 else
13435 is_positive = tree_int_cst_sgn (arg01);
13437 /* (X - c) > X becomes false. */
13438 if (code == GT_EXPR
13439 && ((code0 == MINUS_EXPR && is_positive >= 0)
13440 || (code0 == PLUS_EXPR && is_positive <= 0)))
13442 if (TREE_CODE (arg01) == INTEGER_CST
13443 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13444 fold_overflow_warning (("assuming signed overflow does not "
13445 "occur when assuming that (X - c) > X "
13446 "is always false"),
13447 WARN_STRICT_OVERFLOW_ALL);
13448 return constant_boolean_node (0, type);
13451 /* Likewise (X + c) < X becomes false. */
13452 if (code == LT_EXPR
13453 && ((code0 == PLUS_EXPR && is_positive >= 0)
13454 || (code0 == MINUS_EXPR && is_positive <= 0)))
13456 if (TREE_CODE (arg01) == INTEGER_CST
13457 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13458 fold_overflow_warning (("assuming signed overflow does not "
13459 "occur when assuming that "
13460 "(X + c) < X is always false"),
13461 WARN_STRICT_OVERFLOW_ALL);
13462 return constant_boolean_node (0, type);
13465 /* Convert (X - c) <= X to true. */
13466 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13467 && code == LE_EXPR
13468 && ((code0 == MINUS_EXPR && is_positive >= 0)
13469 || (code0 == PLUS_EXPR && is_positive <= 0)))
13471 if (TREE_CODE (arg01) == INTEGER_CST
13472 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13473 fold_overflow_warning (("assuming signed overflow does not "
13474 "occur when assuming that "
13475 "(X - c) <= X is always true"),
13476 WARN_STRICT_OVERFLOW_ALL);
13477 return constant_boolean_node (1, type);
13480 /* Convert (X + c) >= X to true. */
13481 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13482 && code == GE_EXPR
13483 && ((code0 == PLUS_EXPR && is_positive >= 0)
13484 || (code0 == MINUS_EXPR && is_positive <= 0)))
13486 if (TREE_CODE (arg01) == INTEGER_CST
13487 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13488 fold_overflow_warning (("assuming signed overflow does not "
13489 "occur when assuming that "
13490 "(X + c) >= X is always true"),
13491 WARN_STRICT_OVERFLOW_ALL);
13492 return constant_boolean_node (1, type);
13495 if (TREE_CODE (arg01) == INTEGER_CST)
13497 /* Convert X + c > X and X - c < X to true for integers. */
13498 if (code == GT_EXPR
13499 && ((code0 == PLUS_EXPR && is_positive > 0)
13500 || (code0 == MINUS_EXPR && is_positive < 0)))
13502 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13503 fold_overflow_warning (("assuming signed overflow does "
13504 "not occur when assuming that "
13505 "(X + c) > X is always true"),
13506 WARN_STRICT_OVERFLOW_ALL);
13507 return constant_boolean_node (1, type);
13510 if (code == LT_EXPR
13511 && ((code0 == MINUS_EXPR && is_positive > 0)
13512 || (code0 == PLUS_EXPR && is_positive < 0)))
13514 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13515 fold_overflow_warning (("assuming signed overflow does "
13516 "not occur when assuming that "
13517 "(X - c) < X is always true"),
13518 WARN_STRICT_OVERFLOW_ALL);
13519 return constant_boolean_node (1, type);
13522 /* Convert X + c <= X and X - c >= X to false for integers. */
13523 if (code == LE_EXPR
13524 && ((code0 == PLUS_EXPR && is_positive > 0)
13525 || (code0 == MINUS_EXPR && is_positive < 0)))
13527 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13528 fold_overflow_warning (("assuming signed overflow does "
13529 "not occur when assuming that "
13530 "(X + c) <= X is always false"),
13531 WARN_STRICT_OVERFLOW_ALL);
13532 return constant_boolean_node (0, type);
13535 if (code == GE_EXPR
13536 && ((code0 == MINUS_EXPR && is_positive > 0)
13537 || (code0 == PLUS_EXPR && is_positive < 0)))
13539 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13540 fold_overflow_warning (("assuming signed overflow does "
13541 "not occur when assuming that "
13542 "(X - c) >= X is always false"),
13543 WARN_STRICT_OVERFLOW_ALL);
13544 return constant_boolean_node (0, type);
13549 /* Comparisons with the highest or lowest possible integer of
13550 the specified precision will have known values. */
13552 tree arg1_type = TREE_TYPE (arg1);
13553 unsigned int width = TYPE_PRECISION (arg1_type);
13555 if (TREE_CODE (arg1) == INTEGER_CST
13556 && width <= HOST_BITS_PER_DOUBLE_INT
13557 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
13559 HOST_WIDE_INT signed_max_hi;
13560 unsigned HOST_WIDE_INT signed_max_lo;
13561 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
13563 if (width <= HOST_BITS_PER_WIDE_INT)
13565 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13566 - 1;
13567 signed_max_hi = 0;
13568 max_hi = 0;
13570 if (TYPE_UNSIGNED (arg1_type))
13572 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13573 min_lo = 0;
13574 min_hi = 0;
13576 else
13578 max_lo = signed_max_lo;
13579 min_lo = (HOST_WIDE_INT_M1U << (width - 1));
13580 min_hi = -1;
13583 else
13585 width -= HOST_BITS_PER_WIDE_INT;
13586 signed_max_lo = -1;
13587 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13588 - 1;
13589 max_lo = -1;
13590 min_lo = 0;
13592 if (TYPE_UNSIGNED (arg1_type))
13594 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13595 min_hi = 0;
13597 else
13599 max_hi = signed_max_hi;
13600 min_hi = (HOST_WIDE_INT_M1U << (width - 1));
13604 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
13605 && TREE_INT_CST_LOW (arg1) == max_lo)
13606 switch (code)
13608 case GT_EXPR:
13609 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13611 case GE_EXPR:
13612 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13614 case LE_EXPR:
13615 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13617 case LT_EXPR:
13618 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13620 /* The GE_EXPR and LT_EXPR cases above are not normally
13621 reached because of previous transformations. */
13623 default:
13624 break;
13626 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13627 == max_hi
13628 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
13629 switch (code)
13631 case GT_EXPR:
13632 arg1 = const_binop (PLUS_EXPR, arg1,
13633 build_int_cst (TREE_TYPE (arg1), 1));
13634 return fold_build2_loc (loc, EQ_EXPR, type,
13635 fold_convert_loc (loc,
13636 TREE_TYPE (arg1), arg0),
13637 arg1);
13638 case LE_EXPR:
13639 arg1 = const_binop (PLUS_EXPR, arg1,
13640 build_int_cst (TREE_TYPE (arg1), 1));
13641 return fold_build2_loc (loc, NE_EXPR, type,
13642 fold_convert_loc (loc, TREE_TYPE (arg1),
13643 arg0),
13644 arg1);
13645 default:
13646 break;
13648 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13649 == min_hi
13650 && TREE_INT_CST_LOW (arg1) == min_lo)
13651 switch (code)
13653 case LT_EXPR:
13654 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13656 case LE_EXPR:
13657 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13659 case GE_EXPR:
13660 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13662 case GT_EXPR:
13663 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13665 default:
13666 break;
13668 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13669 == min_hi
13670 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13671 switch (code)
13673 case GE_EXPR:
13674 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13675 return fold_build2_loc (loc, NE_EXPR, type,
13676 fold_convert_loc (loc,
13677 TREE_TYPE (arg1), arg0),
13678 arg1);
13679 case LT_EXPR:
13680 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
13681 return fold_build2_loc (loc, EQ_EXPR, type,
13682 fold_convert_loc (loc, TREE_TYPE (arg1),
13683 arg0),
13684 arg1);
13685 default:
13686 break;
13689 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13690 && TREE_INT_CST_LOW (arg1) == signed_max_lo
13691 && TYPE_UNSIGNED (arg1_type)
13692 /* We will flip the signedness of the comparison operator
13693 associated with the mode of arg1, so the sign bit is
13694 specified by this mode. Check that arg1 is the signed
13695 max associated with this sign bit. */
13696 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13697 /* signed_type does not work on pointer types. */
13698 && INTEGRAL_TYPE_P (arg1_type))
13700 /* The following case also applies to X < signed_max+1
13701 and X >= signed_max+1 because previous transformations. */
13702 if (code == LE_EXPR || code == GT_EXPR)
13704 tree st;
13705 st = signed_type_for (TREE_TYPE (arg1));
13706 return fold_build2_loc (loc,
13707 code == LE_EXPR ? GE_EXPR : LT_EXPR,
13708 type, fold_convert_loc (loc, st, arg0),
13709 build_int_cst (st, 0));
13715 /* If we are comparing an ABS_EXPR with a constant, we can
13716 convert all the cases into explicit comparisons, but they may
13717 well not be faster than doing the ABS and one comparison.
13718 But ABS (X) <= C is a range comparison, which becomes a subtraction
13719 and a comparison, and is probably faster. */
13720 if (code == LE_EXPR
13721 && TREE_CODE (arg1) == INTEGER_CST
13722 && TREE_CODE (arg0) == ABS_EXPR
13723 && ! TREE_SIDE_EFFECTS (arg0)
13724 && (0 != (tem = negate_expr (arg1)))
13725 && TREE_CODE (tem) == INTEGER_CST
13726 && !TREE_OVERFLOW (tem))
13727 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13728 build2 (GE_EXPR, type,
13729 TREE_OPERAND (arg0, 0), tem),
13730 build2 (LE_EXPR, type,
13731 TREE_OPERAND (arg0, 0), arg1));
13733 /* Convert ABS_EXPR<x> >= 0 to true. */
13734 strict_overflow_p = false;
13735 if (code == GE_EXPR
13736 && (integer_zerop (arg1)
13737 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13738 && real_zerop (arg1)))
13739 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13741 if (strict_overflow_p)
13742 fold_overflow_warning (("assuming signed overflow does not occur "
13743 "when simplifying comparison of "
13744 "absolute value and zero"),
13745 WARN_STRICT_OVERFLOW_CONDITIONAL);
13746 return omit_one_operand_loc (loc, type,
13747 constant_boolean_node (true, type),
13748 arg0);
13751 /* Convert ABS_EXPR<x> < 0 to false. */
13752 strict_overflow_p = false;
13753 if (code == LT_EXPR
13754 && (integer_zerop (arg1) || real_zerop (arg1))
13755 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13757 if (strict_overflow_p)
13758 fold_overflow_warning (("assuming signed overflow does not occur "
13759 "when simplifying comparison of "
13760 "absolute value and zero"),
13761 WARN_STRICT_OVERFLOW_CONDITIONAL);
13762 return omit_one_operand_loc (loc, type,
13763 constant_boolean_node (false, type),
13764 arg0);
13767 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13768 and similarly for >= into !=. */
13769 if ((code == LT_EXPR || code == GE_EXPR)
13770 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13771 && TREE_CODE (arg1) == LSHIFT_EXPR
13772 && integer_onep (TREE_OPERAND (arg1, 0)))
13773 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13774 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13775 TREE_OPERAND (arg1, 1)),
13776 build_zero_cst (TREE_TYPE (arg0)));
13778 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
13779 otherwise Y might be >= # of bits in X's type and thus e.g.
13780 (unsigned char) (1 << Y) for Y 15 might be 0.
13781 If the cast is widening, then 1 << Y should have unsigned type,
13782 otherwise if Y is number of bits in the signed shift type minus 1,
13783 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
13784 31 might be 0xffffffff80000000. */
13785 if ((code == LT_EXPR || code == GE_EXPR)
13786 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13787 && CONVERT_EXPR_P (arg1)
13788 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13789 && (TYPE_PRECISION (TREE_TYPE (arg1))
13790 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0))))
13791 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
13792 || (TYPE_PRECISION (TREE_TYPE (arg1))
13793 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
13794 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13796 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13797 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
13798 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13799 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
13800 build_zero_cst (TREE_TYPE (arg0)));
13803 return NULL_TREE;
13805 case UNORDERED_EXPR:
13806 case ORDERED_EXPR:
13807 case UNLT_EXPR:
13808 case UNLE_EXPR:
13809 case UNGT_EXPR:
13810 case UNGE_EXPR:
13811 case UNEQ_EXPR:
13812 case LTGT_EXPR:
13813 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13815 t1 = fold_relational_const (code, type, arg0, arg1);
13816 if (t1 != NULL_TREE)
13817 return t1;
13820 /* If the first operand is NaN, the result is constant. */
13821 if (TREE_CODE (arg0) == REAL_CST
13822 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13823 && (code != LTGT_EXPR || ! flag_trapping_math))
13825 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13826 ? integer_zero_node
13827 : integer_one_node;
13828 return omit_one_operand_loc (loc, type, t1, arg1);
13831 /* If the second operand is NaN, the result is constant. */
13832 if (TREE_CODE (arg1) == REAL_CST
13833 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13834 && (code != LTGT_EXPR || ! flag_trapping_math))
13836 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13837 ? integer_zero_node
13838 : integer_one_node;
13839 return omit_one_operand_loc (loc, type, t1, arg0);
13842 /* Simplify unordered comparison of something with itself. */
13843 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13844 && operand_equal_p (arg0, arg1, 0))
13845 return constant_boolean_node (1, type);
13847 if (code == LTGT_EXPR
13848 && !flag_trapping_math
13849 && operand_equal_p (arg0, arg1, 0))
13850 return constant_boolean_node (0, type);
13852 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13854 tree targ0 = strip_float_extensions (arg0);
13855 tree targ1 = strip_float_extensions (arg1);
13856 tree newtype = TREE_TYPE (targ0);
13858 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13859 newtype = TREE_TYPE (targ1);
13861 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13862 return fold_build2_loc (loc, code, type,
13863 fold_convert_loc (loc, newtype, targ0),
13864 fold_convert_loc (loc, newtype, targ1));
13867 return NULL_TREE;
13869 case COMPOUND_EXPR:
13870 /* When pedantic, a compound expression can be neither an lvalue
13871 nor an integer constant expression. */
13872 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13873 return NULL_TREE;
13874 /* Don't let (0, 0) be null pointer constant. */
13875 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13876 : fold_convert_loc (loc, type, arg1);
13877 return pedantic_non_lvalue_loc (loc, tem);
13879 case COMPLEX_EXPR:
13880 if ((TREE_CODE (arg0) == REAL_CST
13881 && TREE_CODE (arg1) == REAL_CST)
13882 || (TREE_CODE (arg0) == INTEGER_CST
13883 && TREE_CODE (arg1) == INTEGER_CST))
13884 return build_complex (type, arg0, arg1);
13885 if (TREE_CODE (arg0) == REALPART_EXPR
13886 && TREE_CODE (arg1) == IMAGPART_EXPR
13887 && TREE_TYPE (TREE_OPERAND (arg0, 0)) == type
13888 && operand_equal_p (TREE_OPERAND (arg0, 0),
13889 TREE_OPERAND (arg1, 0), 0))
13890 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
13891 TREE_OPERAND (arg1, 0));
13892 return NULL_TREE;
13894 case ASSERT_EXPR:
13895 /* An ASSERT_EXPR should never be passed to fold_binary. */
13896 gcc_unreachable ();
13898 case VEC_PACK_TRUNC_EXPR:
13899 case VEC_PACK_FIX_TRUNC_EXPR:
13901 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
13902 tree *elts;
13904 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts / 2
13905 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2);
13906 if (TREE_CODE (arg0) != VECTOR_CST || TREE_CODE (arg1) != VECTOR_CST)
13907 return NULL_TREE;
13909 elts = XALLOCAVEC (tree, nelts);
13910 if (!vec_cst_ctor_to_array (arg0, elts)
13911 || !vec_cst_ctor_to_array (arg1, elts + nelts / 2))
13912 return NULL_TREE;
13914 for (i = 0; i < nelts; i++)
13916 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
13917 ? NOP_EXPR : FIX_TRUNC_EXPR,
13918 TREE_TYPE (type), elts[i]);
13919 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
13920 return NULL_TREE;
13923 return build_vector (type, elts);
13926 case VEC_WIDEN_MULT_LO_EXPR:
13927 case VEC_WIDEN_MULT_HI_EXPR:
13928 case VEC_WIDEN_MULT_EVEN_EXPR:
13929 case VEC_WIDEN_MULT_ODD_EXPR:
13931 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
13932 unsigned int out, ofs, scale;
13933 tree *elts;
13935 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2
13936 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2);
13937 if (TREE_CODE (arg0) != VECTOR_CST || TREE_CODE (arg1) != VECTOR_CST)
13938 return NULL_TREE;
13940 elts = XALLOCAVEC (tree, nelts * 4);
13941 if (!vec_cst_ctor_to_array (arg0, elts)
13942 || !vec_cst_ctor_to_array (arg1, elts + nelts * 2))
13943 return NULL_TREE;
13945 if (code == VEC_WIDEN_MULT_LO_EXPR)
13946 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
13947 else if (code == VEC_WIDEN_MULT_HI_EXPR)
13948 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
13949 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
13950 scale = 1, ofs = 0;
13951 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
13952 scale = 1, ofs = 1;
13954 for (out = 0; out < nelts; out++)
13956 unsigned int in1 = (out << scale) + ofs;
13957 unsigned int in2 = in1 + nelts * 2;
13958 tree t1, t2;
13960 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
13961 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
13963 if (t1 == NULL_TREE || t2 == NULL_TREE)
13964 return NULL_TREE;
13965 elts[out] = const_binop (MULT_EXPR, t1, t2);
13966 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
13967 return NULL_TREE;
13970 return build_vector (type, elts);
13973 default:
13974 return NULL_TREE;
13975 } /* switch (code) */
13978 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
13979 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
13980 of GOTO_EXPR. */
13982 static tree
13983 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13985 switch (TREE_CODE (*tp))
13987 case LABEL_EXPR:
13988 return *tp;
13990 case GOTO_EXPR:
13991 *walk_subtrees = 0;
13993 /* ... fall through ... */
13995 default:
13996 return NULL_TREE;
14000 /* Return whether the sub-tree ST contains a label which is accessible from
14001 outside the sub-tree. */
14003 static bool
14004 contains_label_p (tree st)
14006 return
14007 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
14010 /* Fold a ternary expression of code CODE and type TYPE with operands
14011 OP0, OP1, and OP2. Return the folded expression if folding is
14012 successful. Otherwise, return NULL_TREE. */
14014 tree
14015 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
14016 tree op0, tree op1, tree op2)
14018 tree tem;
14019 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
14020 enum tree_code_class kind = TREE_CODE_CLASS (code);
14022 gcc_assert (IS_EXPR_CODE_CLASS (kind)
14023 && TREE_CODE_LENGTH (code) == 3);
14025 /* Strip any conversions that don't change the mode. This is safe
14026 for every expression, except for a comparison expression because
14027 its signedness is derived from its operands. So, in the latter
14028 case, only strip conversions that don't change the signedness.
14030 Note that this is done as an internal manipulation within the
14031 constant folder, in order to find the simplest representation of
14032 the arguments so that their form can be studied. In any cases,
14033 the appropriate type conversions should be put back in the tree
14034 that will get out of the constant folder. */
14035 if (op0)
14037 arg0 = op0;
14038 STRIP_NOPS (arg0);
14041 if (op1)
14043 arg1 = op1;
14044 STRIP_NOPS (arg1);
14047 if (op2)
14049 arg2 = op2;
14050 STRIP_NOPS (arg2);
14053 switch (code)
14055 case COMPONENT_REF:
14056 if (TREE_CODE (arg0) == CONSTRUCTOR
14057 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
14059 unsigned HOST_WIDE_INT idx;
14060 tree field, value;
14061 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
14062 if (field == arg1)
14063 return value;
14065 return NULL_TREE;
14067 case COND_EXPR:
14068 case VEC_COND_EXPR:
14069 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
14070 so all simple results must be passed through pedantic_non_lvalue. */
14071 if (TREE_CODE (arg0) == INTEGER_CST)
14073 tree unused_op = integer_zerop (arg0) ? op1 : op2;
14074 tem = integer_zerop (arg0) ? op2 : op1;
14075 /* Only optimize constant conditions when the selected branch
14076 has the same type as the COND_EXPR. This avoids optimizing
14077 away "c ? x : throw", where the throw has a void type.
14078 Avoid throwing away that operand which contains label. */
14079 if ((!TREE_SIDE_EFFECTS (unused_op)
14080 || !contains_label_p (unused_op))
14081 && (! VOID_TYPE_P (TREE_TYPE (tem))
14082 || VOID_TYPE_P (type)))
14083 return pedantic_non_lvalue_loc (loc, tem);
14084 return NULL_TREE;
14086 else if (TREE_CODE (arg0) == VECTOR_CST)
14088 if (integer_all_onesp (arg0))
14089 return pedantic_omit_one_operand_loc (loc, type, arg1, arg2);
14090 if (integer_zerop (arg0))
14091 return pedantic_omit_one_operand_loc (loc, type, arg2, arg1);
14093 if ((TREE_CODE (arg1) == VECTOR_CST
14094 || TREE_CODE (arg1) == CONSTRUCTOR)
14095 && (TREE_CODE (arg2) == VECTOR_CST
14096 || TREE_CODE (arg2) == CONSTRUCTOR))
14098 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
14099 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
14100 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
14101 for (i = 0; i < nelts; i++)
14103 tree val = VECTOR_CST_ELT (arg0, i);
14104 if (integer_all_onesp (val))
14105 sel[i] = i;
14106 else if (integer_zerop (val))
14107 sel[i] = nelts + i;
14108 else /* Currently unreachable. */
14109 return NULL_TREE;
14111 tree t = fold_vec_perm (type, arg1, arg2, sel);
14112 if (t != NULL_TREE)
14113 return t;
14117 if (operand_equal_p (arg1, op2, 0))
14118 return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
14120 /* If we have A op B ? A : C, we may be able to convert this to a
14121 simpler expression, depending on the operation and the values
14122 of B and C. Signed zeros prevent all of these transformations,
14123 for reasons given above each one.
14125 Also try swapping the arguments and inverting the conditional. */
14126 if (COMPARISON_CLASS_P (arg0)
14127 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
14128 arg1, TREE_OPERAND (arg0, 1))
14129 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
14131 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
14132 if (tem)
14133 return tem;
14136 if (COMPARISON_CLASS_P (arg0)
14137 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
14138 op2,
14139 TREE_OPERAND (arg0, 1))
14140 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
14142 location_t loc0 = expr_location_or (arg0, loc);
14143 tem = fold_invert_truthvalue (loc0, arg0);
14144 if (tem && COMPARISON_CLASS_P (tem))
14146 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
14147 if (tem)
14148 return tem;
14152 /* If the second operand is simpler than the third, swap them
14153 since that produces better jump optimization results. */
14154 if (truth_value_p (TREE_CODE (arg0))
14155 && tree_swap_operands_p (op1, op2, false))
14157 location_t loc0 = expr_location_or (arg0, loc);
14158 /* See if this can be inverted. If it can't, possibly because
14159 it was a floating-point inequality comparison, don't do
14160 anything. */
14161 tem = fold_invert_truthvalue (loc0, arg0);
14162 if (tem)
14163 return fold_build3_loc (loc, code, type, tem, op2, op1);
14166 /* Convert A ? 1 : 0 to simply A. */
14167 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
14168 : (integer_onep (op1)
14169 && !VECTOR_TYPE_P (type)))
14170 && integer_zerop (op2)
14171 /* If we try to convert OP0 to our type, the
14172 call to fold will try to move the conversion inside
14173 a COND, which will recurse. In that case, the COND_EXPR
14174 is probably the best choice, so leave it alone. */
14175 && type == TREE_TYPE (arg0))
14176 return pedantic_non_lvalue_loc (loc, arg0);
14178 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
14179 over COND_EXPR in cases such as floating point comparisons. */
14180 if (integer_zerop (op1)
14181 && (code == VEC_COND_EXPR ? integer_all_onesp (op2)
14182 : (integer_onep (op2)
14183 && !VECTOR_TYPE_P (type)))
14184 && truth_value_p (TREE_CODE (arg0)))
14185 return pedantic_non_lvalue_loc (loc,
14186 fold_convert_loc (loc, type,
14187 invert_truthvalue_loc (loc,
14188 arg0)));
14190 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
14191 if (TREE_CODE (arg0) == LT_EXPR
14192 && integer_zerop (TREE_OPERAND (arg0, 1))
14193 && integer_zerop (op2)
14194 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
14196 /* sign_bit_p looks through both zero and sign extensions,
14197 but for this optimization only sign extensions are
14198 usable. */
14199 tree tem2 = TREE_OPERAND (arg0, 0);
14200 while (tem != tem2)
14202 if (TREE_CODE (tem2) != NOP_EXPR
14203 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
14205 tem = NULL_TREE;
14206 break;
14208 tem2 = TREE_OPERAND (tem2, 0);
14210 /* sign_bit_p only checks ARG1 bits within A's precision.
14211 If <sign bit of A> has wider type than A, bits outside
14212 of A's precision in <sign bit of A> need to be checked.
14213 If they are all 0, this optimization needs to be done
14214 in unsigned A's type, if they are all 1 in signed A's type,
14215 otherwise this can't be done. */
14216 if (tem
14217 && TYPE_PRECISION (TREE_TYPE (tem))
14218 < TYPE_PRECISION (TREE_TYPE (arg1))
14219 && TYPE_PRECISION (TREE_TYPE (tem))
14220 < TYPE_PRECISION (type))
14222 unsigned HOST_WIDE_INT mask_lo;
14223 HOST_WIDE_INT mask_hi;
14224 int inner_width, outer_width;
14225 tree tem_type;
14227 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
14228 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
14229 if (outer_width > TYPE_PRECISION (type))
14230 outer_width = TYPE_PRECISION (type);
14232 if (outer_width > HOST_BITS_PER_WIDE_INT)
14234 mask_hi = (HOST_WIDE_INT_M1U
14235 >> (HOST_BITS_PER_DOUBLE_INT - outer_width));
14236 mask_lo = -1;
14238 else
14240 mask_hi = 0;
14241 mask_lo = (HOST_WIDE_INT_M1U
14242 >> (HOST_BITS_PER_WIDE_INT - outer_width));
14244 if (inner_width > HOST_BITS_PER_WIDE_INT)
14246 mask_hi &= ~(HOST_WIDE_INT_M1U
14247 >> (HOST_BITS_PER_WIDE_INT - inner_width));
14248 mask_lo = 0;
14250 else
14251 mask_lo &= ~(HOST_WIDE_INT_M1U
14252 >> (HOST_BITS_PER_WIDE_INT - inner_width));
14254 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
14255 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
14257 tem_type = signed_type_for (TREE_TYPE (tem));
14258 tem = fold_convert_loc (loc, tem_type, tem);
14260 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
14261 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
14263 tem_type = unsigned_type_for (TREE_TYPE (tem));
14264 tem = fold_convert_loc (loc, tem_type, tem);
14266 else
14267 tem = NULL;
14270 if (tem)
14271 return
14272 fold_convert_loc (loc, type,
14273 fold_build2_loc (loc, BIT_AND_EXPR,
14274 TREE_TYPE (tem), tem,
14275 fold_convert_loc (loc,
14276 TREE_TYPE (tem),
14277 arg1)));
14280 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
14281 already handled above. */
14282 if (TREE_CODE (arg0) == BIT_AND_EXPR
14283 && integer_onep (TREE_OPERAND (arg0, 1))
14284 && integer_zerop (op2)
14285 && integer_pow2p (arg1))
14287 tree tem = TREE_OPERAND (arg0, 0);
14288 STRIP_NOPS (tem);
14289 if (TREE_CODE (tem) == RSHIFT_EXPR
14290 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
14291 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
14292 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
14293 return fold_build2_loc (loc, BIT_AND_EXPR, type,
14294 TREE_OPERAND (tem, 0), arg1);
14297 /* A & N ? N : 0 is simply A & N if N is a power of two. This
14298 is probably obsolete because the first operand should be a
14299 truth value (that's why we have the two cases above), but let's
14300 leave it in until we can confirm this for all front-ends. */
14301 if (integer_zerop (op2)
14302 && TREE_CODE (arg0) == NE_EXPR
14303 && integer_zerop (TREE_OPERAND (arg0, 1))
14304 && integer_pow2p (arg1)
14305 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
14306 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
14307 arg1, OEP_ONLY_CONST))
14308 return pedantic_non_lvalue_loc (loc,
14309 fold_convert_loc (loc, type,
14310 TREE_OPERAND (arg0, 0)));
14312 /* Disable the transformations below for vectors, since
14313 fold_binary_op_with_conditional_arg may undo them immediately,
14314 yielding an infinite loop. */
14315 if (code == VEC_COND_EXPR)
14316 return NULL_TREE;
14318 /* Convert A ? B : 0 into A && B if A and B are truth values. */
14319 if (integer_zerop (op2)
14320 && truth_value_p (TREE_CODE (arg0))
14321 && truth_value_p (TREE_CODE (arg1))
14322 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
14323 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
14324 : TRUTH_ANDIF_EXPR,
14325 type, fold_convert_loc (loc, type, arg0), arg1);
14327 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
14328 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
14329 && truth_value_p (TREE_CODE (arg0))
14330 && truth_value_p (TREE_CODE (arg1))
14331 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
14333 location_t loc0 = expr_location_or (arg0, loc);
14334 /* Only perform transformation if ARG0 is easily inverted. */
14335 tem = fold_invert_truthvalue (loc0, arg0);
14336 if (tem)
14337 return fold_build2_loc (loc, code == VEC_COND_EXPR
14338 ? BIT_IOR_EXPR
14339 : TRUTH_ORIF_EXPR,
14340 type, fold_convert_loc (loc, type, tem),
14341 arg1);
14344 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
14345 if (integer_zerop (arg1)
14346 && truth_value_p (TREE_CODE (arg0))
14347 && truth_value_p (TREE_CODE (op2))
14348 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
14350 location_t loc0 = expr_location_or (arg0, loc);
14351 /* Only perform transformation if ARG0 is easily inverted. */
14352 tem = fold_invert_truthvalue (loc0, arg0);
14353 if (tem)
14354 return fold_build2_loc (loc, code == VEC_COND_EXPR
14355 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
14356 type, fold_convert_loc (loc, type, tem),
14357 op2);
14360 /* Convert A ? 1 : B into A || B if A and B are truth values. */
14361 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
14362 && truth_value_p (TREE_CODE (arg0))
14363 && truth_value_p (TREE_CODE (op2))
14364 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
14365 return fold_build2_loc (loc, code == VEC_COND_EXPR
14366 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
14367 type, fold_convert_loc (loc, type, arg0), op2);
14369 return NULL_TREE;
14371 case CALL_EXPR:
14372 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
14373 of fold_ternary on them. */
14374 gcc_unreachable ();
14376 case BIT_FIELD_REF:
14377 if ((TREE_CODE (arg0) == VECTOR_CST
14378 || (TREE_CODE (arg0) == CONSTRUCTOR
14379 && TREE_CODE (TREE_TYPE (arg0)) == VECTOR_TYPE))
14380 && (type == TREE_TYPE (TREE_TYPE (arg0))
14381 || (TREE_CODE (type) == VECTOR_TYPE
14382 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
14384 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
14385 unsigned HOST_WIDE_INT width = tree_low_cst (TYPE_SIZE (eltype), 1);
14386 unsigned HOST_WIDE_INT n = tree_low_cst (arg1, 1);
14387 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
14389 if (n != 0
14390 && (idx % width) == 0
14391 && (n % width) == 0
14392 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
14394 idx = idx / width;
14395 n = n / width;
14397 if (TREE_CODE (arg0) == VECTOR_CST)
14399 if (n == 1)
14400 return VECTOR_CST_ELT (arg0, idx);
14402 tree *vals = XALLOCAVEC (tree, n);
14403 for (unsigned i = 0; i < n; ++i)
14404 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
14405 return build_vector (type, vals);
14408 /* Constructor elements can be subvectors. */
14409 unsigned HOST_WIDE_INT k = 1;
14410 if (CONSTRUCTOR_NELTS (arg0) != 0)
14412 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (arg0, 0)->value);
14413 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
14414 k = TYPE_VECTOR_SUBPARTS (cons_elem);
14417 /* We keep an exact subset of the constructor elements. */
14418 if ((idx % k) == 0 && (n % k) == 0)
14420 if (CONSTRUCTOR_NELTS (arg0) == 0)
14421 return build_constructor (type, NULL);
14422 idx /= k;
14423 n /= k;
14424 if (n == 1)
14426 if (idx < CONSTRUCTOR_NELTS (arg0))
14427 return CONSTRUCTOR_ELT (arg0, idx)->value;
14428 return build_zero_cst (type);
14431 vec<constructor_elt, va_gc> *vals;
14432 vec_alloc (vals, n);
14433 for (unsigned i = 0;
14434 i < n && idx + i < CONSTRUCTOR_NELTS (arg0);
14435 ++i)
14436 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
14437 CONSTRUCTOR_ELT
14438 (arg0, idx + i)->value);
14439 return build_constructor (type, vals);
14441 /* The bitfield references a single constructor element. */
14442 else if (idx + n <= (idx / k + 1) * k)
14444 if (CONSTRUCTOR_NELTS (arg0) <= idx / k)
14445 return build_zero_cst (type);
14446 else if (n == k)
14447 return CONSTRUCTOR_ELT (arg0, idx / k)->value;
14448 else
14449 return fold_build3_loc (loc, code, type,
14450 CONSTRUCTOR_ELT (arg0, idx / k)->value, op1,
14451 build_int_cst (TREE_TYPE (op2), (idx % k) * width));
14456 /* A bit-field-ref that referenced the full argument can be stripped. */
14457 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
14458 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
14459 && integer_zerop (op2))
14460 return fold_convert_loc (loc, type, arg0);
14462 /* On constants we can use native encode/interpret to constant
14463 fold (nearly) all BIT_FIELD_REFs. */
14464 if (CONSTANT_CLASS_P (arg0)
14465 && can_native_interpret_type_p (type)
14466 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (arg0)), 1)
14467 /* This limitation should not be necessary, we just need to
14468 round this up to mode size. */
14469 && tree_low_cst (op1, 1) % BITS_PER_UNIT == 0
14470 /* Need bit-shifting of the buffer to relax the following. */
14471 && tree_low_cst (op2, 1) % BITS_PER_UNIT == 0)
14473 unsigned HOST_WIDE_INT bitpos = tree_low_cst (op2, 1);
14474 unsigned HOST_WIDE_INT bitsize = tree_low_cst (op1, 1);
14475 unsigned HOST_WIDE_INT clen;
14476 clen = tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (arg0)), 1);
14477 /* ??? We cannot tell native_encode_expr to start at
14478 some random byte only. So limit us to a reasonable amount
14479 of work. */
14480 if (clen <= 4096)
14482 unsigned char *b = XALLOCAVEC (unsigned char, clen);
14483 unsigned HOST_WIDE_INT len = native_encode_expr (arg0, b, clen);
14484 if (len > 0
14485 && len * BITS_PER_UNIT >= bitpos + bitsize)
14487 tree v = native_interpret_expr (type,
14488 b + bitpos / BITS_PER_UNIT,
14489 bitsize / BITS_PER_UNIT);
14490 if (v)
14491 return v;
14496 return NULL_TREE;
14498 case FMA_EXPR:
14499 /* For integers we can decompose the FMA if possible. */
14500 if (TREE_CODE (arg0) == INTEGER_CST
14501 && TREE_CODE (arg1) == INTEGER_CST)
14502 return fold_build2_loc (loc, PLUS_EXPR, type,
14503 const_binop (MULT_EXPR, arg0, arg1), arg2);
14504 if (integer_zerop (arg2))
14505 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
14507 return fold_fma (loc, type, arg0, arg1, arg2);
14509 case VEC_PERM_EXPR:
14510 if (TREE_CODE (arg2) == VECTOR_CST)
14512 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask;
14513 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
14514 tree t;
14515 bool need_mask_canon = false;
14516 bool all_in_vec0 = true;
14517 bool all_in_vec1 = true;
14518 bool maybe_identity = true;
14519 bool single_arg = (op0 == op1);
14520 bool changed = false;
14522 mask = single_arg ? (nelts - 1) : (2 * nelts - 1);
14523 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
14524 for (i = 0; i < nelts; i++)
14526 tree val = VECTOR_CST_ELT (arg2, i);
14527 if (TREE_CODE (val) != INTEGER_CST)
14528 return NULL_TREE;
14530 sel[i] = TREE_INT_CST_LOW (val) & mask;
14531 if (TREE_INT_CST_HIGH (val)
14532 || ((unsigned HOST_WIDE_INT)
14533 TREE_INT_CST_LOW (val) != sel[i]))
14534 need_mask_canon = true;
14536 if (sel[i] < nelts)
14537 all_in_vec1 = false;
14538 else
14539 all_in_vec0 = false;
14541 if ((sel[i] & (nelts-1)) != i)
14542 maybe_identity = false;
14545 if (maybe_identity)
14547 if (all_in_vec0)
14548 return op0;
14549 if (all_in_vec1)
14550 return op1;
14553 if (all_in_vec0)
14554 op1 = op0;
14555 else if (all_in_vec1)
14557 op0 = op1;
14558 for (i = 0; i < nelts; i++)
14559 sel[i] -= nelts;
14560 need_mask_canon = true;
14563 if ((TREE_CODE (op0) == VECTOR_CST
14564 || TREE_CODE (op0) == CONSTRUCTOR)
14565 && (TREE_CODE (op1) == VECTOR_CST
14566 || TREE_CODE (op1) == CONSTRUCTOR))
14568 t = fold_vec_perm (type, op0, op1, sel);
14569 if (t != NULL_TREE)
14570 return t;
14573 if (op0 == op1 && !single_arg)
14574 changed = true;
14576 if (need_mask_canon && arg2 == op2)
14578 tree *tsel = XALLOCAVEC (tree, nelts);
14579 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
14580 for (i = 0; i < nelts; i++)
14581 tsel[i] = build_int_cst (eltype, sel[i]);
14582 op2 = build_vector (TREE_TYPE (arg2), tsel);
14583 changed = true;
14586 if (changed)
14587 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
14589 return NULL_TREE;
14591 default:
14592 return NULL_TREE;
14593 } /* switch (code) */
14596 /* Perform constant folding and related simplification of EXPR.
14597 The related simplifications include x*1 => x, x*0 => 0, etc.,
14598 and application of the associative law.
14599 NOP_EXPR conversions may be removed freely (as long as we
14600 are careful not to change the type of the overall expression).
14601 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
14602 but we can constant-fold them if they have constant operands. */
14604 #ifdef ENABLE_FOLD_CHECKING
14605 # define fold(x) fold_1 (x)
14606 static tree fold_1 (tree);
14607 static
14608 #endif
14609 tree
14610 fold (tree expr)
14612 const tree t = expr;
14613 enum tree_code code = TREE_CODE (t);
14614 enum tree_code_class kind = TREE_CODE_CLASS (code);
14615 tree tem;
14616 location_t loc = EXPR_LOCATION (expr);
14618 /* Return right away if a constant. */
14619 if (kind == tcc_constant)
14620 return t;
14622 /* CALL_EXPR-like objects with variable numbers of operands are
14623 treated specially. */
14624 if (kind == tcc_vl_exp)
14626 if (code == CALL_EXPR)
14628 tem = fold_call_expr (loc, expr, false);
14629 return tem ? tem : expr;
14631 return expr;
14634 if (IS_EXPR_CODE_CLASS (kind))
14636 tree type = TREE_TYPE (t);
14637 tree op0, op1, op2;
14639 switch (TREE_CODE_LENGTH (code))
14641 case 1:
14642 op0 = TREE_OPERAND (t, 0);
14643 tem = fold_unary_loc (loc, code, type, op0);
14644 return tem ? tem : expr;
14645 case 2:
14646 op0 = TREE_OPERAND (t, 0);
14647 op1 = TREE_OPERAND (t, 1);
14648 tem = fold_binary_loc (loc, code, type, op0, op1);
14649 return tem ? tem : expr;
14650 case 3:
14651 op0 = TREE_OPERAND (t, 0);
14652 op1 = TREE_OPERAND (t, 1);
14653 op2 = TREE_OPERAND (t, 2);
14654 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14655 return tem ? tem : expr;
14656 default:
14657 break;
14661 switch (code)
14663 case ARRAY_REF:
14665 tree op0 = TREE_OPERAND (t, 0);
14666 tree op1 = TREE_OPERAND (t, 1);
14668 if (TREE_CODE (op1) == INTEGER_CST
14669 && TREE_CODE (op0) == CONSTRUCTOR
14670 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
14672 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (op0);
14673 unsigned HOST_WIDE_INT end = vec_safe_length (elts);
14674 unsigned HOST_WIDE_INT begin = 0;
14676 /* Find a matching index by means of a binary search. */
14677 while (begin != end)
14679 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
14680 tree index = (*elts)[middle].index;
14682 if (TREE_CODE (index) == INTEGER_CST
14683 && tree_int_cst_lt (index, op1))
14684 begin = middle + 1;
14685 else if (TREE_CODE (index) == INTEGER_CST
14686 && tree_int_cst_lt (op1, index))
14687 end = middle;
14688 else if (TREE_CODE (index) == RANGE_EXPR
14689 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
14690 begin = middle + 1;
14691 else if (TREE_CODE (index) == RANGE_EXPR
14692 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
14693 end = middle;
14694 else
14695 return (*elts)[middle].value;
14699 return t;
14702 /* Return a VECTOR_CST if possible. */
14703 case CONSTRUCTOR:
14705 tree type = TREE_TYPE (t);
14706 if (TREE_CODE (type) != VECTOR_TYPE)
14707 return t;
14709 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
14710 unsigned HOST_WIDE_INT idx, pos = 0;
14711 tree value;
14713 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, value)
14715 if (!CONSTANT_CLASS_P (value))
14716 return t;
14717 if (TREE_CODE (value) == VECTOR_CST)
14719 for (unsigned i = 0; i < VECTOR_CST_NELTS (value); ++i)
14720 vec[pos++] = VECTOR_CST_ELT (value, i);
14722 else
14723 vec[pos++] = value;
14725 for (; pos < TYPE_VECTOR_SUBPARTS (type); ++pos)
14726 vec[pos] = build_zero_cst (TREE_TYPE (type));
14728 return build_vector (type, vec);
14731 case CONST_DECL:
14732 return fold (DECL_INITIAL (t));
14734 default:
14735 return t;
14736 } /* switch (code) */
14739 #ifdef ENABLE_FOLD_CHECKING
14740 #undef fold
14742 static void fold_checksum_tree (const_tree, struct md5_ctx *,
14743 hash_table <pointer_hash <tree_node> >);
14744 static void fold_check_failed (const_tree, const_tree);
14745 void print_fold_checksum (const_tree);
14747 /* When --enable-checking=fold, compute a digest of expr before
14748 and after actual fold call to see if fold did not accidentally
14749 change original expr. */
14751 tree
14752 fold (tree expr)
14754 tree ret;
14755 struct md5_ctx ctx;
14756 unsigned char checksum_before[16], checksum_after[16];
14757 hash_table <pointer_hash <tree_node> > ht;
14759 ht.create (32);
14760 md5_init_ctx (&ctx);
14761 fold_checksum_tree (expr, &ctx, ht);
14762 md5_finish_ctx (&ctx, checksum_before);
14763 ht.empty ();
14765 ret = fold_1 (expr);
14767 md5_init_ctx (&ctx);
14768 fold_checksum_tree (expr, &ctx, ht);
14769 md5_finish_ctx (&ctx, checksum_after);
14770 ht.dispose ();
14772 if (memcmp (checksum_before, checksum_after, 16))
14773 fold_check_failed (expr, ret);
14775 return ret;
14778 void
14779 print_fold_checksum (const_tree expr)
14781 struct md5_ctx ctx;
14782 unsigned char checksum[16], cnt;
14783 hash_table <pointer_hash <tree_node> > ht;
14785 ht.create (32);
14786 md5_init_ctx (&ctx);
14787 fold_checksum_tree (expr, &ctx, ht);
14788 md5_finish_ctx (&ctx, checksum);
14789 ht.dispose ();
14790 for (cnt = 0; cnt < 16; ++cnt)
14791 fprintf (stderr, "%02x", checksum[cnt]);
14792 putc ('\n', stderr);
14795 static void
14796 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
14798 internal_error ("fold check: original tree changed by fold");
14801 static void
14802 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
14803 hash_table <pointer_hash <tree_node> > ht)
14805 tree_node **slot;
14806 enum tree_code code;
14807 union tree_node buf;
14808 int i, len;
14810 recursive_label:
14811 if (expr == NULL)
14812 return;
14813 slot = ht.find_slot (expr, INSERT);
14814 if (*slot != NULL)
14815 return;
14816 *slot = CONST_CAST_TREE (expr);
14817 code = TREE_CODE (expr);
14818 if (TREE_CODE_CLASS (code) == tcc_declaration
14819 && DECL_ASSEMBLER_NAME_SET_P (expr))
14821 /* Allow DECL_ASSEMBLER_NAME to be modified. */
14822 memcpy ((char *) &buf, expr, tree_size (expr));
14823 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
14824 expr = (tree) &buf;
14826 else if (TREE_CODE_CLASS (code) == tcc_type
14827 && (TYPE_POINTER_TO (expr)
14828 || TYPE_REFERENCE_TO (expr)
14829 || TYPE_CACHED_VALUES_P (expr)
14830 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
14831 || TYPE_NEXT_VARIANT (expr)))
14833 /* Allow these fields to be modified. */
14834 tree tmp;
14835 memcpy ((char *) &buf, expr, tree_size (expr));
14836 expr = tmp = (tree) &buf;
14837 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
14838 TYPE_POINTER_TO (tmp) = NULL;
14839 TYPE_REFERENCE_TO (tmp) = NULL;
14840 TYPE_NEXT_VARIANT (tmp) = NULL;
14841 if (TYPE_CACHED_VALUES_P (tmp))
14843 TYPE_CACHED_VALUES_P (tmp) = 0;
14844 TYPE_CACHED_VALUES (tmp) = NULL;
14847 md5_process_bytes (expr, tree_size (expr), ctx);
14848 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
14849 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
14850 if (TREE_CODE_CLASS (code) != tcc_type
14851 && TREE_CODE_CLASS (code) != tcc_declaration
14852 && code != TREE_LIST
14853 && code != SSA_NAME
14854 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
14855 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
14856 switch (TREE_CODE_CLASS (code))
14858 case tcc_constant:
14859 switch (code)
14861 case STRING_CST:
14862 md5_process_bytes (TREE_STRING_POINTER (expr),
14863 TREE_STRING_LENGTH (expr), ctx);
14864 break;
14865 case COMPLEX_CST:
14866 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
14867 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
14868 break;
14869 case VECTOR_CST:
14870 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
14871 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
14872 break;
14873 default:
14874 break;
14876 break;
14877 case tcc_exceptional:
14878 switch (code)
14880 case TREE_LIST:
14881 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
14882 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
14883 expr = TREE_CHAIN (expr);
14884 goto recursive_label;
14885 break;
14886 case TREE_VEC:
14887 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
14888 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
14889 break;
14890 default:
14891 break;
14893 break;
14894 case tcc_expression:
14895 case tcc_reference:
14896 case tcc_comparison:
14897 case tcc_unary:
14898 case tcc_binary:
14899 case tcc_statement:
14900 case tcc_vl_exp:
14901 len = TREE_OPERAND_LENGTH (expr);
14902 for (i = 0; i < len; ++i)
14903 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
14904 break;
14905 case tcc_declaration:
14906 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
14907 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
14908 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
14910 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
14911 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
14912 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
14913 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
14914 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
14916 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
14917 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
14919 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
14921 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
14922 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
14923 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
14925 break;
14926 case tcc_type:
14927 if (TREE_CODE (expr) == ENUMERAL_TYPE)
14928 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
14929 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
14930 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
14931 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
14932 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
14933 if (INTEGRAL_TYPE_P (expr)
14934 || SCALAR_FLOAT_TYPE_P (expr))
14936 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
14937 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
14939 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
14940 if (TREE_CODE (expr) == RECORD_TYPE
14941 || TREE_CODE (expr) == UNION_TYPE
14942 || TREE_CODE (expr) == QUAL_UNION_TYPE)
14943 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
14944 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
14945 break;
14946 default:
14947 break;
14951 /* Helper function for outputting the checksum of a tree T. When
14952 debugging with gdb, you can "define mynext" to be "next" followed
14953 by "call debug_fold_checksum (op0)", then just trace down till the
14954 outputs differ. */
14956 DEBUG_FUNCTION void
14957 debug_fold_checksum (const_tree t)
14959 int i;
14960 unsigned char checksum[16];
14961 struct md5_ctx ctx;
14962 hash_table <pointer_hash <tree_node> > ht;
14963 ht.create (32);
14965 md5_init_ctx (&ctx);
14966 fold_checksum_tree (t, &ctx, ht);
14967 md5_finish_ctx (&ctx, checksum);
14968 ht.empty ();
14970 for (i = 0; i < 16; i++)
14971 fprintf (stderr, "%d ", checksum[i]);
14973 fprintf (stderr, "\n");
14976 #endif
14978 /* Fold a unary tree expression with code CODE of type TYPE with an
14979 operand OP0. LOC is the location of the resulting expression.
14980 Return a folded expression if successful. Otherwise, return a tree
14981 expression with code CODE of type TYPE with an operand OP0. */
14983 tree
14984 fold_build1_stat_loc (location_t loc,
14985 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
14987 tree tem;
14988 #ifdef ENABLE_FOLD_CHECKING
14989 unsigned char checksum_before[16], checksum_after[16];
14990 struct md5_ctx ctx;
14991 hash_table <pointer_hash <tree_node> > ht;
14993 ht.create (32);
14994 md5_init_ctx (&ctx);
14995 fold_checksum_tree (op0, &ctx, ht);
14996 md5_finish_ctx (&ctx, checksum_before);
14997 ht.empty ();
14998 #endif
15000 tem = fold_unary_loc (loc, code, type, op0);
15001 if (!tem)
15002 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
15004 #ifdef ENABLE_FOLD_CHECKING
15005 md5_init_ctx (&ctx);
15006 fold_checksum_tree (op0, &ctx, ht);
15007 md5_finish_ctx (&ctx, checksum_after);
15008 ht.dispose ();
15010 if (memcmp (checksum_before, checksum_after, 16))
15011 fold_check_failed (op0, tem);
15012 #endif
15013 return tem;
15016 /* Fold a binary tree expression with code CODE of type TYPE with
15017 operands OP0 and OP1. LOC is the location of the resulting
15018 expression. Return a folded expression if successful. Otherwise,
15019 return a tree expression with code CODE of type TYPE with operands
15020 OP0 and OP1. */
15022 tree
15023 fold_build2_stat_loc (location_t loc,
15024 enum tree_code code, tree type, tree op0, tree op1
15025 MEM_STAT_DECL)
15027 tree tem;
15028 #ifdef ENABLE_FOLD_CHECKING
15029 unsigned char checksum_before_op0[16],
15030 checksum_before_op1[16],
15031 checksum_after_op0[16],
15032 checksum_after_op1[16];
15033 struct md5_ctx ctx;
15034 hash_table <pointer_hash <tree_node> > ht;
15036 ht.create (32);
15037 md5_init_ctx (&ctx);
15038 fold_checksum_tree (op0, &ctx, ht);
15039 md5_finish_ctx (&ctx, checksum_before_op0);
15040 ht.empty ();
15042 md5_init_ctx (&ctx);
15043 fold_checksum_tree (op1, &ctx, ht);
15044 md5_finish_ctx (&ctx, checksum_before_op1);
15045 ht.empty ();
15046 #endif
15048 tem = fold_binary_loc (loc, code, type, op0, op1);
15049 if (!tem)
15050 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
15052 #ifdef ENABLE_FOLD_CHECKING
15053 md5_init_ctx (&ctx);
15054 fold_checksum_tree (op0, &ctx, ht);
15055 md5_finish_ctx (&ctx, checksum_after_op0);
15056 ht.empty ();
15058 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
15059 fold_check_failed (op0, tem);
15061 md5_init_ctx (&ctx);
15062 fold_checksum_tree (op1, &ctx, ht);
15063 md5_finish_ctx (&ctx, checksum_after_op1);
15064 ht.dispose ();
15066 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
15067 fold_check_failed (op1, tem);
15068 #endif
15069 return tem;
15072 /* Fold a ternary tree expression with code CODE of type TYPE with
15073 operands OP0, OP1, and OP2. Return a folded expression if
15074 successful. Otherwise, return a tree expression with code CODE of
15075 type TYPE with operands OP0, OP1, and OP2. */
15077 tree
15078 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
15079 tree op0, tree op1, tree op2 MEM_STAT_DECL)
15081 tree tem;
15082 #ifdef ENABLE_FOLD_CHECKING
15083 unsigned char checksum_before_op0[16],
15084 checksum_before_op1[16],
15085 checksum_before_op2[16],
15086 checksum_after_op0[16],
15087 checksum_after_op1[16],
15088 checksum_after_op2[16];
15089 struct md5_ctx ctx;
15090 hash_table <pointer_hash <tree_node> > ht;
15092 ht.create (32);
15093 md5_init_ctx (&ctx);
15094 fold_checksum_tree (op0, &ctx, ht);
15095 md5_finish_ctx (&ctx, checksum_before_op0);
15096 ht.empty ();
15098 md5_init_ctx (&ctx);
15099 fold_checksum_tree (op1, &ctx, ht);
15100 md5_finish_ctx (&ctx, checksum_before_op1);
15101 ht.empty ();
15103 md5_init_ctx (&ctx);
15104 fold_checksum_tree (op2, &ctx, ht);
15105 md5_finish_ctx (&ctx, checksum_before_op2);
15106 ht.empty ();
15107 #endif
15109 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
15110 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
15111 if (!tem)
15112 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
15114 #ifdef ENABLE_FOLD_CHECKING
15115 md5_init_ctx (&ctx);
15116 fold_checksum_tree (op0, &ctx, ht);
15117 md5_finish_ctx (&ctx, checksum_after_op0);
15118 ht.empty ();
15120 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
15121 fold_check_failed (op0, tem);
15123 md5_init_ctx (&ctx);
15124 fold_checksum_tree (op1, &ctx, ht);
15125 md5_finish_ctx (&ctx, checksum_after_op1);
15126 ht.empty ();
15128 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
15129 fold_check_failed (op1, tem);
15131 md5_init_ctx (&ctx);
15132 fold_checksum_tree (op2, &ctx, ht);
15133 md5_finish_ctx (&ctx, checksum_after_op2);
15134 ht.dispose ();
15136 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
15137 fold_check_failed (op2, tem);
15138 #endif
15139 return tem;
15142 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
15143 arguments in ARGARRAY, and a null static chain.
15144 Return a folded expression if successful. Otherwise, return a CALL_EXPR
15145 of type TYPE from the given operands as constructed by build_call_array. */
15147 tree
15148 fold_build_call_array_loc (location_t loc, tree type, tree fn,
15149 int nargs, tree *argarray)
15151 tree tem;
15152 #ifdef ENABLE_FOLD_CHECKING
15153 unsigned char checksum_before_fn[16],
15154 checksum_before_arglist[16],
15155 checksum_after_fn[16],
15156 checksum_after_arglist[16];
15157 struct md5_ctx ctx;
15158 hash_table <pointer_hash <tree_node> > ht;
15159 int i;
15161 ht.create (32);
15162 md5_init_ctx (&ctx);
15163 fold_checksum_tree (fn, &ctx, ht);
15164 md5_finish_ctx (&ctx, checksum_before_fn);
15165 ht.empty ();
15167 md5_init_ctx (&ctx);
15168 for (i = 0; i < nargs; i++)
15169 fold_checksum_tree (argarray[i], &ctx, ht);
15170 md5_finish_ctx (&ctx, checksum_before_arglist);
15171 ht.empty ();
15172 #endif
15174 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
15176 #ifdef ENABLE_FOLD_CHECKING
15177 md5_init_ctx (&ctx);
15178 fold_checksum_tree (fn, &ctx, ht);
15179 md5_finish_ctx (&ctx, checksum_after_fn);
15180 ht.empty ();
15182 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
15183 fold_check_failed (fn, tem);
15185 md5_init_ctx (&ctx);
15186 for (i = 0; i < nargs; i++)
15187 fold_checksum_tree (argarray[i], &ctx, ht);
15188 md5_finish_ctx (&ctx, checksum_after_arglist);
15189 ht.dispose ();
15191 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
15192 fold_check_failed (NULL_TREE, tem);
15193 #endif
15194 return tem;
15197 /* Perform constant folding and related simplification of initializer
15198 expression EXPR. These behave identically to "fold_buildN" but ignore
15199 potential run-time traps and exceptions that fold must preserve. */
15201 #define START_FOLD_INIT \
15202 int saved_signaling_nans = flag_signaling_nans;\
15203 int saved_trapping_math = flag_trapping_math;\
15204 int saved_rounding_math = flag_rounding_math;\
15205 int saved_trapv = flag_trapv;\
15206 int saved_folding_initializer = folding_initializer;\
15207 flag_signaling_nans = 0;\
15208 flag_trapping_math = 0;\
15209 flag_rounding_math = 0;\
15210 flag_trapv = 0;\
15211 folding_initializer = 1;
15213 #define END_FOLD_INIT \
15214 flag_signaling_nans = saved_signaling_nans;\
15215 flag_trapping_math = saved_trapping_math;\
15216 flag_rounding_math = saved_rounding_math;\
15217 flag_trapv = saved_trapv;\
15218 folding_initializer = saved_folding_initializer;
15220 tree
15221 fold_build1_initializer_loc (location_t loc, enum tree_code code,
15222 tree type, tree op)
15224 tree result;
15225 START_FOLD_INIT;
15227 result = fold_build1_loc (loc, code, type, op);
15229 END_FOLD_INIT;
15230 return result;
15233 tree
15234 fold_build2_initializer_loc (location_t loc, enum tree_code code,
15235 tree type, tree op0, tree op1)
15237 tree result;
15238 START_FOLD_INIT;
15240 result = fold_build2_loc (loc, code, type, op0, op1);
15242 END_FOLD_INIT;
15243 return result;
15246 tree
15247 fold_build3_initializer_loc (location_t loc, enum tree_code code,
15248 tree type, tree op0, tree op1, tree op2)
15250 tree result;
15251 START_FOLD_INIT;
15253 result = fold_build3_loc (loc, code, type, op0, op1, op2);
15255 END_FOLD_INIT;
15256 return result;
15259 tree
15260 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
15261 int nargs, tree *argarray)
15263 tree result;
15264 START_FOLD_INIT;
15266 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
15268 END_FOLD_INIT;
15269 return result;
15272 #undef START_FOLD_INIT
15273 #undef END_FOLD_INIT
15275 /* Determine if first argument is a multiple of second argument. Return 0 if
15276 it is not, or we cannot easily determined it to be.
15278 An example of the sort of thing we care about (at this point; this routine
15279 could surely be made more general, and expanded to do what the *_DIV_EXPR's
15280 fold cases do now) is discovering that
15282 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
15284 is a multiple of
15286 SAVE_EXPR (J * 8)
15288 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
15290 This code also handles discovering that
15292 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
15294 is a multiple of 8 so we don't have to worry about dealing with a
15295 possible remainder.
15297 Note that we *look* inside a SAVE_EXPR only to determine how it was
15298 calculated; it is not safe for fold to do much of anything else with the
15299 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
15300 at run time. For example, the latter example above *cannot* be implemented
15301 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
15302 evaluation time of the original SAVE_EXPR is not necessarily the same at
15303 the time the new expression is evaluated. The only optimization of this
15304 sort that would be valid is changing
15306 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
15308 divided by 8 to
15310 SAVE_EXPR (I) * SAVE_EXPR (J)
15312 (where the same SAVE_EXPR (J) is used in the original and the
15313 transformed version). */
15316 multiple_of_p (tree type, const_tree top, const_tree bottom)
15318 if (operand_equal_p (top, bottom, 0))
15319 return 1;
15321 if (TREE_CODE (type) != INTEGER_TYPE)
15322 return 0;
15324 switch (TREE_CODE (top))
15326 case BIT_AND_EXPR:
15327 /* Bitwise and provides a power of two multiple. If the mask is
15328 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
15329 if (!integer_pow2p (bottom))
15330 return 0;
15331 /* FALLTHRU */
15333 case MULT_EXPR:
15334 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
15335 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
15337 case PLUS_EXPR:
15338 case MINUS_EXPR:
15339 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
15340 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
15342 case LSHIFT_EXPR:
15343 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
15345 tree op1, t1;
15347 op1 = TREE_OPERAND (top, 1);
15348 /* const_binop may not detect overflow correctly,
15349 so check for it explicitly here. */
15350 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
15351 > TREE_INT_CST_LOW (op1)
15352 && TREE_INT_CST_HIGH (op1) == 0
15353 && 0 != (t1 = fold_convert (type,
15354 const_binop (LSHIFT_EXPR,
15355 size_one_node,
15356 op1)))
15357 && !TREE_OVERFLOW (t1))
15358 return multiple_of_p (type, t1, bottom);
15360 return 0;
15362 case NOP_EXPR:
15363 /* Can't handle conversions from non-integral or wider integral type. */
15364 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
15365 || (TYPE_PRECISION (type)
15366 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
15367 return 0;
15369 /* .. fall through ... */
15371 case SAVE_EXPR:
15372 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
15374 case COND_EXPR:
15375 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
15376 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
15378 case INTEGER_CST:
15379 if (TREE_CODE (bottom) != INTEGER_CST
15380 || integer_zerop (bottom)
15381 || (TYPE_UNSIGNED (type)
15382 && (tree_int_cst_sgn (top) < 0
15383 || tree_int_cst_sgn (bottom) < 0)))
15384 return 0;
15385 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
15386 top, bottom));
15388 default:
15389 return 0;
15393 /* Return true if CODE or TYPE is known to be non-negative. */
15395 static bool
15396 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
15398 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
15399 && truth_value_p (code))
15400 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
15401 have a signed:1 type (where the value is -1 and 0). */
15402 return true;
15403 return false;
15406 /* Return true if (CODE OP0) is known to be non-negative. If the return
15407 value is based on the assumption that signed overflow is undefined,
15408 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15409 *STRICT_OVERFLOW_P. */
15411 bool
15412 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
15413 bool *strict_overflow_p)
15415 if (TYPE_UNSIGNED (type))
15416 return true;
15418 switch (code)
15420 case ABS_EXPR:
15421 /* We can't return 1 if flag_wrapv is set because
15422 ABS_EXPR<INT_MIN> = INT_MIN. */
15423 if (!INTEGRAL_TYPE_P (type))
15424 return true;
15425 if (TYPE_OVERFLOW_UNDEFINED (type))
15427 *strict_overflow_p = true;
15428 return true;
15430 break;
15432 case NON_LVALUE_EXPR:
15433 case FLOAT_EXPR:
15434 case FIX_TRUNC_EXPR:
15435 return tree_expr_nonnegative_warnv_p (op0,
15436 strict_overflow_p);
15438 case NOP_EXPR:
15440 tree inner_type = TREE_TYPE (op0);
15441 tree outer_type = type;
15443 if (TREE_CODE (outer_type) == REAL_TYPE)
15445 if (TREE_CODE (inner_type) == REAL_TYPE)
15446 return tree_expr_nonnegative_warnv_p (op0,
15447 strict_overflow_p);
15448 if (INTEGRAL_TYPE_P (inner_type))
15450 if (TYPE_UNSIGNED (inner_type))
15451 return true;
15452 return tree_expr_nonnegative_warnv_p (op0,
15453 strict_overflow_p);
15456 else if (INTEGRAL_TYPE_P (outer_type))
15458 if (TREE_CODE (inner_type) == REAL_TYPE)
15459 return tree_expr_nonnegative_warnv_p (op0,
15460 strict_overflow_p);
15461 if (INTEGRAL_TYPE_P (inner_type))
15462 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
15463 && TYPE_UNSIGNED (inner_type);
15466 break;
15468 default:
15469 return tree_simple_nonnegative_warnv_p (code, type);
15472 /* We don't know sign of `t', so be conservative and return false. */
15473 return false;
15476 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
15477 value is based on the assumption that signed overflow is undefined,
15478 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15479 *STRICT_OVERFLOW_P. */
15481 bool
15482 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
15483 tree op1, bool *strict_overflow_p)
15485 if (TYPE_UNSIGNED (type))
15486 return true;
15488 switch (code)
15490 case POINTER_PLUS_EXPR:
15491 case PLUS_EXPR:
15492 if (FLOAT_TYPE_P (type))
15493 return (tree_expr_nonnegative_warnv_p (op0,
15494 strict_overflow_p)
15495 && tree_expr_nonnegative_warnv_p (op1,
15496 strict_overflow_p));
15498 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
15499 both unsigned and at least 2 bits shorter than the result. */
15500 if (TREE_CODE (type) == INTEGER_TYPE
15501 && TREE_CODE (op0) == NOP_EXPR
15502 && TREE_CODE (op1) == NOP_EXPR)
15504 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
15505 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
15506 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
15507 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
15509 unsigned int prec = MAX (TYPE_PRECISION (inner1),
15510 TYPE_PRECISION (inner2)) + 1;
15511 return prec < TYPE_PRECISION (type);
15514 break;
15516 case MULT_EXPR:
15517 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
15519 /* x * x is always non-negative for floating point x
15520 or without overflow. */
15521 if (operand_equal_p (op0, op1, 0)
15522 || (tree_expr_nonnegative_warnv_p (op0, strict_overflow_p)
15523 && tree_expr_nonnegative_warnv_p (op1, strict_overflow_p)))
15525 if (TYPE_OVERFLOW_UNDEFINED (type))
15526 *strict_overflow_p = true;
15527 return true;
15531 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
15532 both unsigned and their total bits is shorter than the result. */
15533 if (TREE_CODE (type) == INTEGER_TYPE
15534 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
15535 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
15537 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
15538 ? TREE_TYPE (TREE_OPERAND (op0, 0))
15539 : TREE_TYPE (op0);
15540 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
15541 ? TREE_TYPE (TREE_OPERAND (op1, 0))
15542 : TREE_TYPE (op1);
15544 bool unsigned0 = TYPE_UNSIGNED (inner0);
15545 bool unsigned1 = TYPE_UNSIGNED (inner1);
15547 if (TREE_CODE (op0) == INTEGER_CST)
15548 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
15550 if (TREE_CODE (op1) == INTEGER_CST)
15551 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
15553 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
15554 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
15556 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
15557 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
15558 : TYPE_PRECISION (inner0);
15560 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
15561 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
15562 : TYPE_PRECISION (inner1);
15564 return precision0 + precision1 < TYPE_PRECISION (type);
15567 return false;
15569 case BIT_AND_EXPR:
15570 case MAX_EXPR:
15571 return (tree_expr_nonnegative_warnv_p (op0,
15572 strict_overflow_p)
15573 || tree_expr_nonnegative_warnv_p (op1,
15574 strict_overflow_p));
15576 case BIT_IOR_EXPR:
15577 case BIT_XOR_EXPR:
15578 case MIN_EXPR:
15579 case RDIV_EXPR:
15580 case TRUNC_DIV_EXPR:
15581 case CEIL_DIV_EXPR:
15582 case FLOOR_DIV_EXPR:
15583 case ROUND_DIV_EXPR:
15584 return (tree_expr_nonnegative_warnv_p (op0,
15585 strict_overflow_p)
15586 && tree_expr_nonnegative_warnv_p (op1,
15587 strict_overflow_p));
15589 case TRUNC_MOD_EXPR:
15590 case CEIL_MOD_EXPR:
15591 case FLOOR_MOD_EXPR:
15592 case ROUND_MOD_EXPR:
15593 return tree_expr_nonnegative_warnv_p (op0,
15594 strict_overflow_p);
15595 default:
15596 return tree_simple_nonnegative_warnv_p (code, type);
15599 /* We don't know sign of `t', so be conservative and return false. */
15600 return false;
15603 /* Return true if T is known to be non-negative. If the return
15604 value is based on the assumption that signed overflow is undefined,
15605 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15606 *STRICT_OVERFLOW_P. */
15608 bool
15609 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15611 if (TYPE_UNSIGNED (TREE_TYPE (t)))
15612 return true;
15614 switch (TREE_CODE (t))
15616 case INTEGER_CST:
15617 return tree_int_cst_sgn (t) >= 0;
15619 case REAL_CST:
15620 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
15622 case FIXED_CST:
15623 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
15625 case COND_EXPR:
15626 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15627 strict_overflow_p)
15628 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
15629 strict_overflow_p));
15630 default:
15631 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15632 TREE_TYPE (t));
15634 /* We don't know sign of `t', so be conservative and return false. */
15635 return false;
15638 /* Return true if T is known to be non-negative. If the return
15639 value is based on the assumption that signed overflow is undefined,
15640 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15641 *STRICT_OVERFLOW_P. */
15643 bool
15644 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
15645 tree arg0, tree arg1, bool *strict_overflow_p)
15647 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
15648 switch (DECL_FUNCTION_CODE (fndecl))
15650 CASE_FLT_FN (BUILT_IN_ACOS):
15651 CASE_FLT_FN (BUILT_IN_ACOSH):
15652 CASE_FLT_FN (BUILT_IN_CABS):
15653 CASE_FLT_FN (BUILT_IN_COSH):
15654 CASE_FLT_FN (BUILT_IN_ERFC):
15655 CASE_FLT_FN (BUILT_IN_EXP):
15656 CASE_FLT_FN (BUILT_IN_EXP10):
15657 CASE_FLT_FN (BUILT_IN_EXP2):
15658 CASE_FLT_FN (BUILT_IN_FABS):
15659 CASE_FLT_FN (BUILT_IN_FDIM):
15660 CASE_FLT_FN (BUILT_IN_HYPOT):
15661 CASE_FLT_FN (BUILT_IN_POW10):
15662 CASE_INT_FN (BUILT_IN_FFS):
15663 CASE_INT_FN (BUILT_IN_PARITY):
15664 CASE_INT_FN (BUILT_IN_POPCOUNT):
15665 CASE_INT_FN (BUILT_IN_CLZ):
15666 CASE_INT_FN (BUILT_IN_CLRSB):
15667 case BUILT_IN_BSWAP32:
15668 case BUILT_IN_BSWAP64:
15669 /* Always true. */
15670 return true;
15672 CASE_FLT_FN (BUILT_IN_SQRT):
15673 /* sqrt(-0.0) is -0.0. */
15674 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
15675 return true;
15676 return tree_expr_nonnegative_warnv_p (arg0,
15677 strict_overflow_p);
15679 CASE_FLT_FN (BUILT_IN_ASINH):
15680 CASE_FLT_FN (BUILT_IN_ATAN):
15681 CASE_FLT_FN (BUILT_IN_ATANH):
15682 CASE_FLT_FN (BUILT_IN_CBRT):
15683 CASE_FLT_FN (BUILT_IN_CEIL):
15684 CASE_FLT_FN (BUILT_IN_ERF):
15685 CASE_FLT_FN (BUILT_IN_EXPM1):
15686 CASE_FLT_FN (BUILT_IN_FLOOR):
15687 CASE_FLT_FN (BUILT_IN_FMOD):
15688 CASE_FLT_FN (BUILT_IN_FREXP):
15689 CASE_FLT_FN (BUILT_IN_ICEIL):
15690 CASE_FLT_FN (BUILT_IN_IFLOOR):
15691 CASE_FLT_FN (BUILT_IN_IRINT):
15692 CASE_FLT_FN (BUILT_IN_IROUND):
15693 CASE_FLT_FN (BUILT_IN_LCEIL):
15694 CASE_FLT_FN (BUILT_IN_LDEXP):
15695 CASE_FLT_FN (BUILT_IN_LFLOOR):
15696 CASE_FLT_FN (BUILT_IN_LLCEIL):
15697 CASE_FLT_FN (BUILT_IN_LLFLOOR):
15698 CASE_FLT_FN (BUILT_IN_LLRINT):
15699 CASE_FLT_FN (BUILT_IN_LLROUND):
15700 CASE_FLT_FN (BUILT_IN_LRINT):
15701 CASE_FLT_FN (BUILT_IN_LROUND):
15702 CASE_FLT_FN (BUILT_IN_MODF):
15703 CASE_FLT_FN (BUILT_IN_NEARBYINT):
15704 CASE_FLT_FN (BUILT_IN_RINT):
15705 CASE_FLT_FN (BUILT_IN_ROUND):
15706 CASE_FLT_FN (BUILT_IN_SCALB):
15707 CASE_FLT_FN (BUILT_IN_SCALBLN):
15708 CASE_FLT_FN (BUILT_IN_SCALBN):
15709 CASE_FLT_FN (BUILT_IN_SIGNBIT):
15710 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
15711 CASE_FLT_FN (BUILT_IN_SINH):
15712 CASE_FLT_FN (BUILT_IN_TANH):
15713 CASE_FLT_FN (BUILT_IN_TRUNC):
15714 /* True if the 1st argument is nonnegative. */
15715 return tree_expr_nonnegative_warnv_p (arg0,
15716 strict_overflow_p);
15718 CASE_FLT_FN (BUILT_IN_FMAX):
15719 /* True if the 1st OR 2nd arguments are nonnegative. */
15720 return (tree_expr_nonnegative_warnv_p (arg0,
15721 strict_overflow_p)
15722 || (tree_expr_nonnegative_warnv_p (arg1,
15723 strict_overflow_p)));
15725 CASE_FLT_FN (BUILT_IN_FMIN):
15726 /* True if the 1st AND 2nd arguments are nonnegative. */
15727 return (tree_expr_nonnegative_warnv_p (arg0,
15728 strict_overflow_p)
15729 && (tree_expr_nonnegative_warnv_p (arg1,
15730 strict_overflow_p)));
15732 CASE_FLT_FN (BUILT_IN_COPYSIGN):
15733 /* True if the 2nd argument is nonnegative. */
15734 return tree_expr_nonnegative_warnv_p (arg1,
15735 strict_overflow_p);
15737 CASE_FLT_FN (BUILT_IN_POWI):
15738 /* True if the 1st argument is nonnegative or the second
15739 argument is an even integer. */
15740 if (TREE_CODE (arg1) == INTEGER_CST
15741 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15742 return true;
15743 return tree_expr_nonnegative_warnv_p (arg0,
15744 strict_overflow_p);
15746 CASE_FLT_FN (BUILT_IN_POW):
15747 /* True if the 1st argument is nonnegative or the second
15748 argument is an even integer valued real. */
15749 if (TREE_CODE (arg1) == REAL_CST)
15751 REAL_VALUE_TYPE c;
15752 HOST_WIDE_INT n;
15754 c = TREE_REAL_CST (arg1);
15755 n = real_to_integer (&c);
15756 if ((n & 1) == 0)
15758 REAL_VALUE_TYPE cint;
15759 real_from_integer (&cint, VOIDmode, n,
15760 n < 0 ? -1 : 0, 0);
15761 if (real_identical (&c, &cint))
15762 return true;
15765 return tree_expr_nonnegative_warnv_p (arg0,
15766 strict_overflow_p);
15768 default:
15769 break;
15771 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
15772 type);
15775 /* Return true if T is known to be non-negative. If the return
15776 value is based on the assumption that signed overflow is undefined,
15777 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15778 *STRICT_OVERFLOW_P. */
15780 bool
15781 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15783 enum tree_code code = TREE_CODE (t);
15784 if (TYPE_UNSIGNED (TREE_TYPE (t)))
15785 return true;
15787 switch (code)
15789 case TARGET_EXPR:
15791 tree temp = TARGET_EXPR_SLOT (t);
15792 t = TARGET_EXPR_INITIAL (t);
15794 /* If the initializer is non-void, then it's a normal expression
15795 that will be assigned to the slot. */
15796 if (!VOID_TYPE_P (t))
15797 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
15799 /* Otherwise, the initializer sets the slot in some way. One common
15800 way is an assignment statement at the end of the initializer. */
15801 while (1)
15803 if (TREE_CODE (t) == BIND_EXPR)
15804 t = expr_last (BIND_EXPR_BODY (t));
15805 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15806 || TREE_CODE (t) == TRY_CATCH_EXPR)
15807 t = expr_last (TREE_OPERAND (t, 0));
15808 else if (TREE_CODE (t) == STATEMENT_LIST)
15809 t = expr_last (t);
15810 else
15811 break;
15813 if (TREE_CODE (t) == MODIFY_EXPR
15814 && TREE_OPERAND (t, 0) == temp)
15815 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15816 strict_overflow_p);
15818 return false;
15821 case CALL_EXPR:
15823 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15824 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15826 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15827 get_callee_fndecl (t),
15828 arg0,
15829 arg1,
15830 strict_overflow_p);
15832 case COMPOUND_EXPR:
15833 case MODIFY_EXPR:
15834 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15835 strict_overflow_p);
15836 case BIND_EXPR:
15837 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
15838 strict_overflow_p);
15839 case SAVE_EXPR:
15840 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
15841 strict_overflow_p);
15843 default:
15844 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15845 TREE_TYPE (t));
15848 /* We don't know sign of `t', so be conservative and return false. */
15849 return false;
15852 /* Return true if T is known to be non-negative. If the return
15853 value is based on the assumption that signed overflow is undefined,
15854 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15855 *STRICT_OVERFLOW_P. */
15857 bool
15858 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15860 enum tree_code code;
15861 if (t == error_mark_node)
15862 return false;
15864 code = TREE_CODE (t);
15865 switch (TREE_CODE_CLASS (code))
15867 case tcc_binary:
15868 case tcc_comparison:
15869 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15870 TREE_TYPE (t),
15871 TREE_OPERAND (t, 0),
15872 TREE_OPERAND (t, 1),
15873 strict_overflow_p);
15875 case tcc_unary:
15876 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15877 TREE_TYPE (t),
15878 TREE_OPERAND (t, 0),
15879 strict_overflow_p);
15881 case tcc_constant:
15882 case tcc_declaration:
15883 case tcc_reference:
15884 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15886 default:
15887 break;
15890 switch (code)
15892 case TRUTH_AND_EXPR:
15893 case TRUTH_OR_EXPR:
15894 case TRUTH_XOR_EXPR:
15895 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15896 TREE_TYPE (t),
15897 TREE_OPERAND (t, 0),
15898 TREE_OPERAND (t, 1),
15899 strict_overflow_p);
15900 case TRUTH_NOT_EXPR:
15901 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15902 TREE_TYPE (t),
15903 TREE_OPERAND (t, 0),
15904 strict_overflow_p);
15906 case COND_EXPR:
15907 case CONSTRUCTOR:
15908 case OBJ_TYPE_REF:
15909 case ASSERT_EXPR:
15910 case ADDR_EXPR:
15911 case WITH_SIZE_EXPR:
15912 case SSA_NAME:
15913 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15915 default:
15916 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
15920 /* Return true if `t' is known to be non-negative. Handle warnings
15921 about undefined signed overflow. */
15923 bool
15924 tree_expr_nonnegative_p (tree t)
15926 bool ret, strict_overflow_p;
15928 strict_overflow_p = false;
15929 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15930 if (strict_overflow_p)
15931 fold_overflow_warning (("assuming signed overflow does not occur when "
15932 "determining that expression is always "
15933 "non-negative"),
15934 WARN_STRICT_OVERFLOW_MISC);
15935 return ret;
15939 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15940 For floating point we further ensure that T is not denormal.
15941 Similar logic is present in nonzero_address in rtlanal.h.
15943 If the return value is based on the assumption that signed overflow
15944 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15945 change *STRICT_OVERFLOW_P. */
15947 bool
15948 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15949 bool *strict_overflow_p)
15951 switch (code)
15953 case ABS_EXPR:
15954 return tree_expr_nonzero_warnv_p (op0,
15955 strict_overflow_p);
15957 case NOP_EXPR:
15959 tree inner_type = TREE_TYPE (op0);
15960 tree outer_type = type;
15962 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15963 && tree_expr_nonzero_warnv_p (op0,
15964 strict_overflow_p));
15966 break;
15968 case NON_LVALUE_EXPR:
15969 return tree_expr_nonzero_warnv_p (op0,
15970 strict_overflow_p);
15972 default:
15973 break;
15976 return false;
15979 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15980 For floating point we further ensure that T is not denormal.
15981 Similar logic is present in nonzero_address in rtlanal.h.
15983 If the return value is based on the assumption that signed overflow
15984 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15985 change *STRICT_OVERFLOW_P. */
15987 bool
15988 tree_binary_nonzero_warnv_p (enum tree_code code,
15989 tree type,
15990 tree op0,
15991 tree op1, bool *strict_overflow_p)
15993 bool sub_strict_overflow_p;
15994 switch (code)
15996 case POINTER_PLUS_EXPR:
15997 case PLUS_EXPR:
15998 if (TYPE_OVERFLOW_UNDEFINED (type))
16000 /* With the presence of negative values it is hard
16001 to say something. */
16002 sub_strict_overflow_p = false;
16003 if (!tree_expr_nonnegative_warnv_p (op0,
16004 &sub_strict_overflow_p)
16005 || !tree_expr_nonnegative_warnv_p (op1,
16006 &sub_strict_overflow_p))
16007 return false;
16008 /* One of operands must be positive and the other non-negative. */
16009 /* We don't set *STRICT_OVERFLOW_P here: even if this value
16010 overflows, on a twos-complement machine the sum of two
16011 nonnegative numbers can never be zero. */
16012 return (tree_expr_nonzero_warnv_p (op0,
16013 strict_overflow_p)
16014 || tree_expr_nonzero_warnv_p (op1,
16015 strict_overflow_p));
16017 break;
16019 case MULT_EXPR:
16020 if (TYPE_OVERFLOW_UNDEFINED (type))
16022 if (tree_expr_nonzero_warnv_p (op0,
16023 strict_overflow_p)
16024 && tree_expr_nonzero_warnv_p (op1,
16025 strict_overflow_p))
16027 *strict_overflow_p = true;
16028 return true;
16031 break;
16033 case MIN_EXPR:
16034 sub_strict_overflow_p = false;
16035 if (tree_expr_nonzero_warnv_p (op0,
16036 &sub_strict_overflow_p)
16037 && tree_expr_nonzero_warnv_p (op1,
16038 &sub_strict_overflow_p))
16040 if (sub_strict_overflow_p)
16041 *strict_overflow_p = true;
16043 break;
16045 case MAX_EXPR:
16046 sub_strict_overflow_p = false;
16047 if (tree_expr_nonzero_warnv_p (op0,
16048 &sub_strict_overflow_p))
16050 if (sub_strict_overflow_p)
16051 *strict_overflow_p = true;
16053 /* When both operands are nonzero, then MAX must be too. */
16054 if (tree_expr_nonzero_warnv_p (op1,
16055 strict_overflow_p))
16056 return true;
16058 /* MAX where operand 0 is positive is positive. */
16059 return tree_expr_nonnegative_warnv_p (op0,
16060 strict_overflow_p);
16062 /* MAX where operand 1 is positive is positive. */
16063 else if (tree_expr_nonzero_warnv_p (op1,
16064 &sub_strict_overflow_p)
16065 && tree_expr_nonnegative_warnv_p (op1,
16066 &sub_strict_overflow_p))
16068 if (sub_strict_overflow_p)
16069 *strict_overflow_p = true;
16070 return true;
16072 break;
16074 case BIT_IOR_EXPR:
16075 return (tree_expr_nonzero_warnv_p (op1,
16076 strict_overflow_p)
16077 || tree_expr_nonzero_warnv_p (op0,
16078 strict_overflow_p));
16080 default:
16081 break;
16084 return false;
16087 /* Return true when T is an address and is known to be nonzero.
16088 For floating point we further ensure that T is not denormal.
16089 Similar logic is present in nonzero_address in rtlanal.h.
16091 If the return value is based on the assumption that signed overflow
16092 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
16093 change *STRICT_OVERFLOW_P. */
16095 bool
16096 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
16098 bool sub_strict_overflow_p;
16099 switch (TREE_CODE (t))
16101 case INTEGER_CST:
16102 return !integer_zerop (t);
16104 case ADDR_EXPR:
16106 tree base = TREE_OPERAND (t, 0);
16107 if (!DECL_P (base))
16108 base = get_base_address (base);
16110 if (!base)
16111 return false;
16113 /* Weak declarations may link to NULL. Other things may also be NULL
16114 so protect with -fdelete-null-pointer-checks; but not variables
16115 allocated on the stack. */
16116 if (DECL_P (base)
16117 && (flag_delete_null_pointer_checks
16118 || (DECL_CONTEXT (base)
16119 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
16120 && auto_var_in_fn_p (base, DECL_CONTEXT (base)))))
16121 return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
16123 /* Constants are never weak. */
16124 if (CONSTANT_CLASS_P (base))
16125 return true;
16127 return false;
16130 case COND_EXPR:
16131 sub_strict_overflow_p = false;
16132 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
16133 &sub_strict_overflow_p)
16134 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
16135 &sub_strict_overflow_p))
16137 if (sub_strict_overflow_p)
16138 *strict_overflow_p = true;
16139 return true;
16141 break;
16143 default:
16144 break;
16146 return false;
16149 /* Return true when T is an address and is known to be nonzero.
16150 For floating point we further ensure that T is not denormal.
16151 Similar logic is present in nonzero_address in rtlanal.h.
16153 If the return value is based on the assumption that signed overflow
16154 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
16155 change *STRICT_OVERFLOW_P. */
16157 bool
16158 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
16160 tree type = TREE_TYPE (t);
16161 enum tree_code code;
16163 /* Doing something useful for floating point would need more work. */
16164 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
16165 return false;
16167 code = TREE_CODE (t);
16168 switch (TREE_CODE_CLASS (code))
16170 case tcc_unary:
16171 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
16172 strict_overflow_p);
16173 case tcc_binary:
16174 case tcc_comparison:
16175 return tree_binary_nonzero_warnv_p (code, type,
16176 TREE_OPERAND (t, 0),
16177 TREE_OPERAND (t, 1),
16178 strict_overflow_p);
16179 case tcc_constant:
16180 case tcc_declaration:
16181 case tcc_reference:
16182 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
16184 default:
16185 break;
16188 switch (code)
16190 case TRUTH_NOT_EXPR:
16191 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
16192 strict_overflow_p);
16194 case TRUTH_AND_EXPR:
16195 case TRUTH_OR_EXPR:
16196 case TRUTH_XOR_EXPR:
16197 return tree_binary_nonzero_warnv_p (code, type,
16198 TREE_OPERAND (t, 0),
16199 TREE_OPERAND (t, 1),
16200 strict_overflow_p);
16202 case COND_EXPR:
16203 case CONSTRUCTOR:
16204 case OBJ_TYPE_REF:
16205 case ASSERT_EXPR:
16206 case ADDR_EXPR:
16207 case WITH_SIZE_EXPR:
16208 case SSA_NAME:
16209 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
16211 case COMPOUND_EXPR:
16212 case MODIFY_EXPR:
16213 case BIND_EXPR:
16214 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
16215 strict_overflow_p);
16217 case SAVE_EXPR:
16218 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
16219 strict_overflow_p);
16221 case CALL_EXPR:
16223 tree fndecl = get_callee_fndecl (t);
16224 if (!fndecl) return false;
16225 if (flag_delete_null_pointer_checks && !flag_check_new
16226 && DECL_IS_OPERATOR_NEW (fndecl)
16227 && !TREE_NOTHROW (fndecl))
16228 return true;
16229 if (flag_delete_null_pointer_checks
16230 && lookup_attribute ("returns_nonnull",
16231 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
16232 return true;
16233 return alloca_call_p (t);
16236 default:
16237 break;
16239 return false;
16242 /* Return true when T is an address and is known to be nonzero.
16243 Handle warnings about undefined signed overflow. */
16245 bool
16246 tree_expr_nonzero_p (tree t)
16248 bool ret, strict_overflow_p;
16250 strict_overflow_p = false;
16251 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
16252 if (strict_overflow_p)
16253 fold_overflow_warning (("assuming signed overflow does not occur when "
16254 "determining that expression is always "
16255 "non-zero"),
16256 WARN_STRICT_OVERFLOW_MISC);
16257 return ret;
16260 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
16261 attempt to fold the expression to a constant without modifying TYPE,
16262 OP0 or OP1.
16264 If the expression could be simplified to a constant, then return
16265 the constant. If the expression would not be simplified to a
16266 constant, then return NULL_TREE. */
16268 tree
16269 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
16271 tree tem = fold_binary (code, type, op0, op1);
16272 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
16275 /* Given the components of a unary expression CODE, TYPE and OP0,
16276 attempt to fold the expression to a constant without modifying
16277 TYPE or OP0.
16279 If the expression could be simplified to a constant, then return
16280 the constant. If the expression would not be simplified to a
16281 constant, then return NULL_TREE. */
16283 tree
16284 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
16286 tree tem = fold_unary (code, type, op0);
16287 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
16290 /* If EXP represents referencing an element in a constant string
16291 (either via pointer arithmetic or array indexing), return the
16292 tree representing the value accessed, otherwise return NULL. */
16294 tree
16295 fold_read_from_constant_string (tree exp)
16297 if ((TREE_CODE (exp) == INDIRECT_REF
16298 || TREE_CODE (exp) == ARRAY_REF)
16299 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
16301 tree exp1 = TREE_OPERAND (exp, 0);
16302 tree index;
16303 tree string;
16304 location_t loc = EXPR_LOCATION (exp);
16306 if (TREE_CODE (exp) == INDIRECT_REF)
16307 string = string_constant (exp1, &index);
16308 else
16310 tree low_bound = array_ref_low_bound (exp);
16311 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
16313 /* Optimize the special-case of a zero lower bound.
16315 We convert the low_bound to sizetype to avoid some problems
16316 with constant folding. (E.g. suppose the lower bound is 1,
16317 and its mode is QI. Without the conversion,l (ARRAY
16318 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
16319 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
16320 if (! integer_zerop (low_bound))
16321 index = size_diffop_loc (loc, index,
16322 fold_convert_loc (loc, sizetype, low_bound));
16324 string = exp1;
16327 if (string
16328 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
16329 && TREE_CODE (string) == STRING_CST
16330 && TREE_CODE (index) == INTEGER_CST
16331 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
16332 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
16333 == MODE_INT)
16334 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
16335 return build_int_cst_type (TREE_TYPE (exp),
16336 (TREE_STRING_POINTER (string)
16337 [TREE_INT_CST_LOW (index)]));
16339 return NULL;
16342 /* Return the tree for neg (ARG0) when ARG0 is known to be either
16343 an integer constant, real, or fixed-point constant.
16345 TYPE is the type of the result. */
16347 static tree
16348 fold_negate_const (tree arg0, tree type)
16350 tree t = NULL_TREE;
16352 switch (TREE_CODE (arg0))
16354 case INTEGER_CST:
16356 double_int val = tree_to_double_int (arg0);
16357 bool overflow;
16358 val = val.neg_with_overflow (&overflow);
16359 t = force_fit_type_double (type, val, 1,
16360 (overflow | TREE_OVERFLOW (arg0))
16361 && !TYPE_UNSIGNED (type));
16362 break;
16365 case REAL_CST:
16366 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
16367 break;
16369 case FIXED_CST:
16371 FIXED_VALUE_TYPE f;
16372 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
16373 &(TREE_FIXED_CST (arg0)), NULL,
16374 TYPE_SATURATING (type));
16375 t = build_fixed (type, f);
16376 /* Propagate overflow flags. */
16377 if (overflow_p | TREE_OVERFLOW (arg0))
16378 TREE_OVERFLOW (t) = 1;
16379 break;
16382 default:
16383 gcc_unreachable ();
16386 return t;
16389 /* Return the tree for abs (ARG0) when ARG0 is known to be either
16390 an integer constant or real constant.
16392 TYPE is the type of the result. */
16394 tree
16395 fold_abs_const (tree arg0, tree type)
16397 tree t = NULL_TREE;
16399 switch (TREE_CODE (arg0))
16401 case INTEGER_CST:
16403 double_int val = tree_to_double_int (arg0);
16405 /* If the value is unsigned or non-negative, then the absolute value
16406 is the same as the ordinary value. */
16407 if (TYPE_UNSIGNED (type)
16408 || !val.is_negative ())
16409 t = arg0;
16411 /* If the value is negative, then the absolute value is
16412 its negation. */
16413 else
16415 bool overflow;
16416 val = val.neg_with_overflow (&overflow);
16417 t = force_fit_type_double (type, val, -1,
16418 overflow | TREE_OVERFLOW (arg0));
16421 break;
16423 case REAL_CST:
16424 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
16425 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
16426 else
16427 t = arg0;
16428 break;
16430 default:
16431 gcc_unreachable ();
16434 return t;
16437 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
16438 constant. TYPE is the type of the result. */
16440 static tree
16441 fold_not_const (const_tree arg0, tree type)
16443 double_int val;
16445 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
16447 val = ~tree_to_double_int (arg0);
16448 return force_fit_type_double (type, val, 0, TREE_OVERFLOW (arg0));
16451 /* Given CODE, a relational operator, the target type, TYPE and two
16452 constant operands OP0 and OP1, return the result of the
16453 relational operation. If the result is not a compile time
16454 constant, then return NULL_TREE. */
16456 static tree
16457 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
16459 int result, invert;
16461 /* From here on, the only cases we handle are when the result is
16462 known to be a constant. */
16464 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
16466 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
16467 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
16469 /* Handle the cases where either operand is a NaN. */
16470 if (real_isnan (c0) || real_isnan (c1))
16472 switch (code)
16474 case EQ_EXPR:
16475 case ORDERED_EXPR:
16476 result = 0;
16477 break;
16479 case NE_EXPR:
16480 case UNORDERED_EXPR:
16481 case UNLT_EXPR:
16482 case UNLE_EXPR:
16483 case UNGT_EXPR:
16484 case UNGE_EXPR:
16485 case UNEQ_EXPR:
16486 result = 1;
16487 break;
16489 case LT_EXPR:
16490 case LE_EXPR:
16491 case GT_EXPR:
16492 case GE_EXPR:
16493 case LTGT_EXPR:
16494 if (flag_trapping_math)
16495 return NULL_TREE;
16496 result = 0;
16497 break;
16499 default:
16500 gcc_unreachable ();
16503 return constant_boolean_node (result, type);
16506 return constant_boolean_node (real_compare (code, c0, c1), type);
16509 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
16511 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
16512 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
16513 return constant_boolean_node (fixed_compare (code, c0, c1), type);
16516 /* Handle equality/inequality of complex constants. */
16517 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
16519 tree rcond = fold_relational_const (code, type,
16520 TREE_REALPART (op0),
16521 TREE_REALPART (op1));
16522 tree icond = fold_relational_const (code, type,
16523 TREE_IMAGPART (op0),
16524 TREE_IMAGPART (op1));
16525 if (code == EQ_EXPR)
16526 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
16527 else if (code == NE_EXPR)
16528 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
16529 else
16530 return NULL_TREE;
16533 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
16535 unsigned count = VECTOR_CST_NELTS (op0);
16536 tree *elts = XALLOCAVEC (tree, count);
16537 gcc_assert (VECTOR_CST_NELTS (op1) == count
16538 && TYPE_VECTOR_SUBPARTS (type) == count);
16540 for (unsigned i = 0; i < count; i++)
16542 tree elem_type = TREE_TYPE (type);
16543 tree elem0 = VECTOR_CST_ELT (op0, i);
16544 tree elem1 = VECTOR_CST_ELT (op1, i);
16546 tree tem = fold_relational_const (code, elem_type,
16547 elem0, elem1);
16549 if (tem == NULL_TREE)
16550 return NULL_TREE;
16552 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
16555 return build_vector (type, elts);
16558 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
16560 To compute GT, swap the arguments and do LT.
16561 To compute GE, do LT and invert the result.
16562 To compute LE, swap the arguments, do LT and invert the result.
16563 To compute NE, do EQ and invert the result.
16565 Therefore, the code below must handle only EQ and LT. */
16567 if (code == LE_EXPR || code == GT_EXPR)
16569 tree tem = op0;
16570 op0 = op1;
16571 op1 = tem;
16572 code = swap_tree_comparison (code);
16575 /* Note that it is safe to invert for real values here because we
16576 have already handled the one case that it matters. */
16578 invert = 0;
16579 if (code == NE_EXPR || code == GE_EXPR)
16581 invert = 1;
16582 code = invert_tree_comparison (code, false);
16585 /* Compute a result for LT or EQ if args permit;
16586 Otherwise return T. */
16587 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
16589 if (code == EQ_EXPR)
16590 result = tree_int_cst_equal (op0, op1);
16591 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
16592 result = INT_CST_LT_UNSIGNED (op0, op1);
16593 else
16594 result = INT_CST_LT (op0, op1);
16596 else
16597 return NULL_TREE;
16599 if (invert)
16600 result ^= 1;
16601 return constant_boolean_node (result, type);
16604 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
16605 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
16606 itself. */
16608 tree
16609 fold_build_cleanup_point_expr (tree type, tree expr)
16611 /* If the expression does not have side effects then we don't have to wrap
16612 it with a cleanup point expression. */
16613 if (!TREE_SIDE_EFFECTS (expr))
16614 return expr;
16616 /* If the expression is a return, check to see if the expression inside the
16617 return has no side effects or the right hand side of the modify expression
16618 inside the return. If either don't have side effects set we don't need to
16619 wrap the expression in a cleanup point expression. Note we don't check the
16620 left hand side of the modify because it should always be a return decl. */
16621 if (TREE_CODE (expr) == RETURN_EXPR)
16623 tree op = TREE_OPERAND (expr, 0);
16624 if (!op || !TREE_SIDE_EFFECTS (op))
16625 return expr;
16626 op = TREE_OPERAND (op, 1);
16627 if (!TREE_SIDE_EFFECTS (op))
16628 return expr;
16631 return build1 (CLEANUP_POINT_EXPR, type, expr);
16634 /* Given a pointer value OP0 and a type TYPE, return a simplified version
16635 of an indirection through OP0, or NULL_TREE if no simplification is
16636 possible. */
16638 tree
16639 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16641 tree sub = op0;
16642 tree subtype;
16644 STRIP_NOPS (sub);
16645 subtype = TREE_TYPE (sub);
16646 if (!POINTER_TYPE_P (subtype))
16647 return NULL_TREE;
16649 if (TREE_CODE (sub) == ADDR_EXPR)
16651 tree op = TREE_OPERAND (sub, 0);
16652 tree optype = TREE_TYPE (op);
16653 /* *&CONST_DECL -> to the value of the const decl. */
16654 if (TREE_CODE (op) == CONST_DECL)
16655 return DECL_INITIAL (op);
16656 /* *&p => p; make sure to handle *&"str"[cst] here. */
16657 if (type == optype)
16659 tree fop = fold_read_from_constant_string (op);
16660 if (fop)
16661 return fop;
16662 else
16663 return op;
16665 /* *(foo *)&fooarray => fooarray[0] */
16666 else if (TREE_CODE (optype) == ARRAY_TYPE
16667 && type == TREE_TYPE (optype)
16668 && (!in_gimple_form
16669 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16671 tree type_domain = TYPE_DOMAIN (optype);
16672 tree min_val = size_zero_node;
16673 if (type_domain && TYPE_MIN_VALUE (type_domain))
16674 min_val = TYPE_MIN_VALUE (type_domain);
16675 if (in_gimple_form
16676 && TREE_CODE (min_val) != INTEGER_CST)
16677 return NULL_TREE;
16678 return build4_loc (loc, ARRAY_REF, type, op, min_val,
16679 NULL_TREE, NULL_TREE);
16681 /* *(foo *)&complexfoo => __real__ complexfoo */
16682 else if (TREE_CODE (optype) == COMPLEX_TYPE
16683 && type == TREE_TYPE (optype))
16684 return fold_build1_loc (loc, REALPART_EXPR, type, op);
16685 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16686 else if (TREE_CODE (optype) == VECTOR_TYPE
16687 && type == TREE_TYPE (optype))
16689 tree part_width = TYPE_SIZE (type);
16690 tree index = bitsize_int (0);
16691 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
16695 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16696 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
16698 tree op00 = TREE_OPERAND (sub, 0);
16699 tree op01 = TREE_OPERAND (sub, 1);
16701 STRIP_NOPS (op00);
16702 if (TREE_CODE (op00) == ADDR_EXPR)
16704 tree op00type;
16705 op00 = TREE_OPERAND (op00, 0);
16706 op00type = TREE_TYPE (op00);
16708 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16709 if (TREE_CODE (op00type) == VECTOR_TYPE
16710 && type == TREE_TYPE (op00type))
16712 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
16713 tree part_width = TYPE_SIZE (type);
16714 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
16715 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
16716 tree index = bitsize_int (indexi);
16718 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
16719 return fold_build3_loc (loc,
16720 BIT_FIELD_REF, type, op00,
16721 part_width, index);
16724 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16725 else if (TREE_CODE (op00type) == COMPLEX_TYPE
16726 && type == TREE_TYPE (op00type))
16728 tree size = TYPE_SIZE_UNIT (type);
16729 if (tree_int_cst_equal (size, op01))
16730 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16732 /* ((foo *)&fooarray)[1] => fooarray[1] */
16733 else if (TREE_CODE (op00type) == ARRAY_TYPE
16734 && type == TREE_TYPE (op00type))
16736 tree type_domain = TYPE_DOMAIN (op00type);
16737 tree min_val = size_zero_node;
16738 if (type_domain && TYPE_MIN_VALUE (type_domain))
16739 min_val = TYPE_MIN_VALUE (type_domain);
16740 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
16741 TYPE_SIZE_UNIT (type));
16742 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
16743 return build4_loc (loc, ARRAY_REF, type, op00, op01,
16744 NULL_TREE, NULL_TREE);
16749 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16750 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16751 && type == TREE_TYPE (TREE_TYPE (subtype))
16752 && (!in_gimple_form
16753 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16755 tree type_domain;
16756 tree min_val = size_zero_node;
16757 sub = build_fold_indirect_ref_loc (loc, sub);
16758 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16759 if (type_domain && TYPE_MIN_VALUE (type_domain))
16760 min_val = TYPE_MIN_VALUE (type_domain);
16761 if (in_gimple_form
16762 && TREE_CODE (min_val) != INTEGER_CST)
16763 return NULL_TREE;
16764 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16765 NULL_TREE);
16768 return NULL_TREE;
16771 /* Builds an expression for an indirection through T, simplifying some
16772 cases. */
16774 tree
16775 build_fold_indirect_ref_loc (location_t loc, tree t)
16777 tree type = TREE_TYPE (TREE_TYPE (t));
16778 tree sub = fold_indirect_ref_1 (loc, type, t);
16780 if (sub)
16781 return sub;
16783 return build1_loc (loc, INDIRECT_REF, type, t);
16786 /* Given an INDIRECT_REF T, return either T or a simplified version. */
16788 tree
16789 fold_indirect_ref_loc (location_t loc, tree t)
16791 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16793 if (sub)
16794 return sub;
16795 else
16796 return t;
16799 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16800 whose result is ignored. The type of the returned tree need not be
16801 the same as the original expression. */
16803 tree
16804 fold_ignored_result (tree t)
16806 if (!TREE_SIDE_EFFECTS (t))
16807 return integer_zero_node;
16809 for (;;)
16810 switch (TREE_CODE_CLASS (TREE_CODE (t)))
16812 case tcc_unary:
16813 t = TREE_OPERAND (t, 0);
16814 break;
16816 case tcc_binary:
16817 case tcc_comparison:
16818 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16819 t = TREE_OPERAND (t, 0);
16820 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16821 t = TREE_OPERAND (t, 1);
16822 else
16823 return t;
16824 break;
16826 case tcc_expression:
16827 switch (TREE_CODE (t))
16829 case COMPOUND_EXPR:
16830 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16831 return t;
16832 t = TREE_OPERAND (t, 0);
16833 break;
16835 case COND_EXPR:
16836 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16837 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16838 return t;
16839 t = TREE_OPERAND (t, 0);
16840 break;
16842 default:
16843 return t;
16845 break;
16847 default:
16848 return t;
16852 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
16853 This can only be applied to objects of a sizetype. */
16855 tree
16856 round_up_loc (location_t loc, tree value, int divisor)
16858 tree div = NULL_TREE;
16860 gcc_assert (divisor > 0);
16861 if (divisor == 1)
16862 return value;
16864 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16865 have to do anything. Only do this when we are not given a const,
16866 because in that case, this check is more expensive than just
16867 doing it. */
16868 if (TREE_CODE (value) != INTEGER_CST)
16870 div = build_int_cst (TREE_TYPE (value), divisor);
16872 if (multiple_of_p (TREE_TYPE (value), value, div))
16873 return value;
16876 /* If divisor is a power of two, simplify this to bit manipulation. */
16877 if (divisor == (divisor & -divisor))
16879 if (TREE_CODE (value) == INTEGER_CST)
16881 double_int val = tree_to_double_int (value);
16882 bool overflow_p;
16884 if ((val.low & (divisor - 1)) == 0)
16885 return value;
16887 overflow_p = TREE_OVERFLOW (value);
16888 val.low &= ~(divisor - 1);
16889 val.low += divisor;
16890 if (val.low == 0)
16892 val.high++;
16893 if (val.high == 0)
16894 overflow_p = true;
16897 return force_fit_type_double (TREE_TYPE (value), val,
16898 -1, overflow_p);
16900 else
16902 tree t;
16904 t = build_int_cst (TREE_TYPE (value), divisor - 1);
16905 value = size_binop_loc (loc, PLUS_EXPR, value, t);
16906 t = build_int_cst (TREE_TYPE (value), -divisor);
16907 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16910 else
16912 if (!div)
16913 div = build_int_cst (TREE_TYPE (value), divisor);
16914 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16915 value = size_binop_loc (loc, MULT_EXPR, value, div);
16918 return value;
16921 /* Likewise, but round down. */
16923 tree
16924 round_down_loc (location_t loc, tree value, int divisor)
16926 tree div = NULL_TREE;
16928 gcc_assert (divisor > 0);
16929 if (divisor == 1)
16930 return value;
16932 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16933 have to do anything. Only do this when we are not given a const,
16934 because in that case, this check is more expensive than just
16935 doing it. */
16936 if (TREE_CODE (value) != INTEGER_CST)
16938 div = build_int_cst (TREE_TYPE (value), divisor);
16940 if (multiple_of_p (TREE_TYPE (value), value, div))
16941 return value;
16944 /* If divisor is a power of two, simplify this to bit manipulation. */
16945 if (divisor == (divisor & -divisor))
16947 tree t;
16949 t = build_int_cst (TREE_TYPE (value), -divisor);
16950 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16952 else
16954 if (!div)
16955 div = build_int_cst (TREE_TYPE (value), divisor);
16956 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16957 value = size_binop_loc (loc, MULT_EXPR, value, div);
16960 return value;
16963 /* Returns the pointer to the base of the object addressed by EXP and
16964 extracts the information about the offset of the access, storing it
16965 to PBITPOS and POFFSET. */
16967 static tree
16968 split_address_to_core_and_offset (tree exp,
16969 HOST_WIDE_INT *pbitpos, tree *poffset)
16971 tree core;
16972 enum machine_mode mode;
16973 int unsignedp, volatilep;
16974 HOST_WIDE_INT bitsize;
16975 location_t loc = EXPR_LOCATION (exp);
16977 if (TREE_CODE (exp) == ADDR_EXPR)
16979 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16980 poffset, &mode, &unsignedp, &volatilep,
16981 false);
16982 core = build_fold_addr_expr_loc (loc, core);
16984 else
16986 core = exp;
16987 *pbitpos = 0;
16988 *poffset = NULL_TREE;
16991 return core;
16994 /* Returns true if addresses of E1 and E2 differ by a constant, false
16995 otherwise. If they do, E1 - E2 is stored in *DIFF. */
16997 bool
16998 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
17000 tree core1, core2;
17001 HOST_WIDE_INT bitpos1, bitpos2;
17002 tree toffset1, toffset2, tdiff, type;
17004 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
17005 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
17007 if (bitpos1 % BITS_PER_UNIT != 0
17008 || bitpos2 % BITS_PER_UNIT != 0
17009 || !operand_equal_p (core1, core2, 0))
17010 return false;
17012 if (toffset1 && toffset2)
17014 type = TREE_TYPE (toffset1);
17015 if (type != TREE_TYPE (toffset2))
17016 toffset2 = fold_convert (type, toffset2);
17018 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
17019 if (!cst_and_fits_in_hwi (tdiff))
17020 return false;
17022 *diff = int_cst_value (tdiff);
17024 else if (toffset1 || toffset2)
17026 /* If only one of the offsets is non-constant, the difference cannot
17027 be a constant. */
17028 return false;
17030 else
17031 *diff = 0;
17033 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
17034 return true;
17037 /* Simplify the floating point expression EXP when the sign of the
17038 result is not significant. Return NULL_TREE if no simplification
17039 is possible. */
17041 tree
17042 fold_strip_sign_ops (tree exp)
17044 tree arg0, arg1;
17045 location_t loc = EXPR_LOCATION (exp);
17047 switch (TREE_CODE (exp))
17049 case ABS_EXPR:
17050 case NEGATE_EXPR:
17051 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
17052 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
17054 case MULT_EXPR:
17055 case RDIV_EXPR:
17056 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
17057 return NULL_TREE;
17058 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
17059 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
17060 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
17061 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
17062 arg0 ? arg0 : TREE_OPERAND (exp, 0),
17063 arg1 ? arg1 : TREE_OPERAND (exp, 1));
17064 break;
17066 case COMPOUND_EXPR:
17067 arg0 = TREE_OPERAND (exp, 0);
17068 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
17069 if (arg1)
17070 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
17071 break;
17073 case COND_EXPR:
17074 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
17075 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
17076 if (arg0 || arg1)
17077 return fold_build3_loc (loc,
17078 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
17079 arg0 ? arg0 : TREE_OPERAND (exp, 1),
17080 arg1 ? arg1 : TREE_OPERAND (exp, 2));
17081 break;
17083 case CALL_EXPR:
17085 const enum built_in_function fcode = builtin_mathfn_code (exp);
17086 switch (fcode)
17088 CASE_FLT_FN (BUILT_IN_COPYSIGN):
17089 /* Strip copysign function call, return the 1st argument. */
17090 arg0 = CALL_EXPR_ARG (exp, 0);
17091 arg1 = CALL_EXPR_ARG (exp, 1);
17092 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
17094 default:
17095 /* Strip sign ops from the argument of "odd" math functions. */
17096 if (negate_mathfn_p (fcode))
17098 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
17099 if (arg0)
17100 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
17102 break;
17105 break;
17107 default:
17108 break;
17110 return NULL_TREE;