Add missing ChangeLog entries for revision 160327.
[official-gcc.git] / gcc / fold-const.c
blob9f2c250e23578020a0026512618d2866b94c8597
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /*@@ This file should be rewritten to use an arbitrary precision
23 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25 @@ The routines that translate from the ap rep should
26 @@ warn if precision et. al. is lost.
27 @@ This would also make life easier when this technology is used
28 @@ for cross-compilers. */
30 /* The entry points in this file are fold, size_int_wide, size_binop
31 and force_fit_type_double.
33 fold takes a tree as argument and returns a simplified tree.
35 size_binop takes a tree code for an arithmetic operation
36 and two operands that are trees, and produces a tree for the
37 result, assuming the type comes from `sizetype'.
39 size_int takes an integer value, and creates a tree constant
40 with type from `sizetype'.
42 force_fit_type_double takes a constant, an overflowable flag and a
43 prior overflow indicator. It forces the value to fit the type and
44 sets TREE_OVERFLOW.
46 Note: Since the folders get called on non-gimple code as well as
47 gimple code, we need to handle GIMPLE tuples as well as their
48 corresponding tree equivalents. */
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "flags.h"
55 #include "tree.h"
56 #include "realmpfr.h"
57 #include "rtl.h"
58 #include "expr.h"
59 #include "tm_p.h"
60 #include "target.h"
61 #include "toplev.h"
62 #include "intl.h"
63 #include "ggc.h"
64 #include "hashtab.h"
65 #include "langhooks.h"
66 #include "md5.h"
67 #include "gimple.h"
69 /* Nonzero if we are folding constants inside an initializer; zero
70 otherwise. */
71 int folding_initializer = 0;
73 /* The following constants represent a bit based encoding of GCC's
74 comparison operators. This encoding simplifies transformations
75 on relational comparison operators, such as AND and OR. */
76 enum comparison_code {
77 COMPCODE_FALSE = 0,
78 COMPCODE_LT = 1,
79 COMPCODE_EQ = 2,
80 COMPCODE_LE = 3,
81 COMPCODE_GT = 4,
82 COMPCODE_LTGT = 5,
83 COMPCODE_GE = 6,
84 COMPCODE_ORD = 7,
85 COMPCODE_UNORD = 8,
86 COMPCODE_UNLT = 9,
87 COMPCODE_UNEQ = 10,
88 COMPCODE_UNLE = 11,
89 COMPCODE_UNGT = 12,
90 COMPCODE_NE = 13,
91 COMPCODE_UNGE = 14,
92 COMPCODE_TRUE = 15
95 static bool negate_mathfn_p (enum built_in_function);
96 static bool negate_expr_p (tree);
97 static tree negate_expr (tree);
98 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
99 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
100 static tree const_binop (enum tree_code, tree, tree, int);
101 static enum comparison_code comparison_to_compcode (enum tree_code);
102 static enum tree_code compcode_to_comparison (enum comparison_code);
103 static int operand_equal_for_comparison_p (tree, tree, tree);
104 static int twoval_comparison_p (tree, tree *, tree *, int *);
105 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
106 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
107 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
108 static tree make_bit_field_ref (location_t, tree, tree,
109 HOST_WIDE_INT, HOST_WIDE_INT, int);
110 static tree optimize_bit_field_compare (location_t, enum tree_code,
111 tree, tree, tree);
112 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
113 HOST_WIDE_INT *,
114 enum machine_mode *, int *, int *,
115 tree *, tree *);
116 static int all_ones_mask_p (const_tree, int);
117 static tree sign_bit_p (tree, const_tree);
118 static int simple_operand_p (const_tree);
119 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
120 static tree range_predecessor (tree);
121 static tree range_successor (tree);
122 extern tree make_range (tree, int *, tree *, tree *, bool *);
123 extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int,
124 tree, tree);
125 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
126 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
127 static tree unextend (tree, int, int, tree);
128 static tree fold_truthop (location_t, enum tree_code, tree, tree, tree);
129 static tree optimize_minmax_comparison (location_t, enum tree_code,
130 tree, tree, tree);
131 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
132 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
133 static tree fold_binary_op_with_conditional_arg (location_t,
134 enum tree_code, tree,
135 tree, tree,
136 tree, tree, int);
137 static tree fold_mathfn_compare (location_t,
138 enum built_in_function, enum tree_code,
139 tree, tree, tree);
140 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
141 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
142 static bool reorder_operands_p (const_tree, const_tree);
143 static tree fold_negate_const (tree, tree);
144 static tree fold_not_const (tree, tree);
145 static tree fold_relational_const (enum tree_code, tree, tree, tree);
146 static tree fold_convert_const (enum tree_code, tree, tree);
149 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
150 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
151 and SUM1. Then this yields nonzero if overflow occurred during the
152 addition.
154 Overflow occurs if A and B have the same sign, but A and SUM differ in
155 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
156 sign. */
157 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
159 /* If ARG2 divides ARG1 with zero remainder, carries out the division
160 of type CODE and returns the quotient.
161 Otherwise returns NULL_TREE. */
163 tree
164 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
166 double_int quo, rem;
167 int uns;
169 /* The sign of the division is according to operand two, that
170 does the correct thing for POINTER_PLUS_EXPR where we want
171 a signed division. */
172 uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
173 if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
174 && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
175 uns = false;
177 quo = double_int_divmod (tree_to_double_int (arg1),
178 tree_to_double_int (arg2),
179 uns, code, &rem);
181 if (double_int_zero_p (rem))
182 return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high);
184 return NULL_TREE;
187 /* This is nonzero if we should defer warnings about undefined
188 overflow. This facility exists because these warnings are a
189 special case. The code to estimate loop iterations does not want
190 to issue any warnings, since it works with expressions which do not
191 occur in user code. Various bits of cleanup code call fold(), but
192 only use the result if it has certain characteristics (e.g., is a
193 constant); that code only wants to issue a warning if the result is
194 used. */
196 static int fold_deferring_overflow_warnings;
198 /* If a warning about undefined overflow is deferred, this is the
199 warning. Note that this may cause us to turn two warnings into
200 one, but that is fine since it is sufficient to only give one
201 warning per expression. */
203 static const char* fold_deferred_overflow_warning;
205 /* If a warning about undefined overflow is deferred, this is the
206 level at which the warning should be emitted. */
208 static enum warn_strict_overflow_code fold_deferred_overflow_code;
210 /* Start deferring overflow warnings. We could use a stack here to
211 permit nested calls, but at present it is not necessary. */
213 void
214 fold_defer_overflow_warnings (void)
216 ++fold_deferring_overflow_warnings;
219 /* Stop deferring overflow warnings. If there is a pending warning,
220 and ISSUE is true, then issue the warning if appropriate. STMT is
221 the statement with which the warning should be associated (used for
222 location information); STMT may be NULL. CODE is the level of the
223 warning--a warn_strict_overflow_code value. This function will use
224 the smaller of CODE and the deferred code when deciding whether to
225 issue the warning. CODE may be zero to mean to always use the
226 deferred code. */
228 void
229 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
231 const char *warnmsg;
232 location_t locus;
234 gcc_assert (fold_deferring_overflow_warnings > 0);
235 --fold_deferring_overflow_warnings;
236 if (fold_deferring_overflow_warnings > 0)
238 if (fold_deferred_overflow_warning != NULL
239 && code != 0
240 && code < (int) fold_deferred_overflow_code)
241 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
242 return;
245 warnmsg = fold_deferred_overflow_warning;
246 fold_deferred_overflow_warning = NULL;
248 if (!issue || warnmsg == NULL)
249 return;
251 if (gimple_no_warning_p (stmt))
252 return;
254 /* Use the smallest code level when deciding to issue the
255 warning. */
256 if (code == 0 || code > (int) fold_deferred_overflow_code)
257 code = fold_deferred_overflow_code;
259 if (!issue_strict_overflow_warning (code))
260 return;
262 if (stmt == NULL)
263 locus = input_location;
264 else
265 locus = gimple_location (stmt);
266 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
269 /* Stop deferring overflow warnings, ignoring any deferred
270 warnings. */
272 void
273 fold_undefer_and_ignore_overflow_warnings (void)
275 fold_undefer_overflow_warnings (false, NULL, 0);
278 /* Whether we are deferring overflow warnings. */
280 bool
281 fold_deferring_overflow_warnings_p (void)
283 return fold_deferring_overflow_warnings > 0;
286 /* This is called when we fold something based on the fact that signed
287 overflow is undefined. */
289 static void
290 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
292 if (fold_deferring_overflow_warnings > 0)
294 if (fold_deferred_overflow_warning == NULL
295 || wc < fold_deferred_overflow_code)
297 fold_deferred_overflow_warning = gmsgid;
298 fold_deferred_overflow_code = wc;
301 else if (issue_strict_overflow_warning (wc))
302 warning (OPT_Wstrict_overflow, gmsgid);
305 /* Return true if the built-in mathematical function specified by CODE
306 is odd, i.e. -f(x) == f(-x). */
308 static bool
309 negate_mathfn_p (enum built_in_function code)
311 switch (code)
313 CASE_FLT_FN (BUILT_IN_ASIN):
314 CASE_FLT_FN (BUILT_IN_ASINH):
315 CASE_FLT_FN (BUILT_IN_ATAN):
316 CASE_FLT_FN (BUILT_IN_ATANH):
317 CASE_FLT_FN (BUILT_IN_CASIN):
318 CASE_FLT_FN (BUILT_IN_CASINH):
319 CASE_FLT_FN (BUILT_IN_CATAN):
320 CASE_FLT_FN (BUILT_IN_CATANH):
321 CASE_FLT_FN (BUILT_IN_CBRT):
322 CASE_FLT_FN (BUILT_IN_CPROJ):
323 CASE_FLT_FN (BUILT_IN_CSIN):
324 CASE_FLT_FN (BUILT_IN_CSINH):
325 CASE_FLT_FN (BUILT_IN_CTAN):
326 CASE_FLT_FN (BUILT_IN_CTANH):
327 CASE_FLT_FN (BUILT_IN_ERF):
328 CASE_FLT_FN (BUILT_IN_LLROUND):
329 CASE_FLT_FN (BUILT_IN_LROUND):
330 CASE_FLT_FN (BUILT_IN_ROUND):
331 CASE_FLT_FN (BUILT_IN_SIN):
332 CASE_FLT_FN (BUILT_IN_SINH):
333 CASE_FLT_FN (BUILT_IN_TAN):
334 CASE_FLT_FN (BUILT_IN_TANH):
335 CASE_FLT_FN (BUILT_IN_TRUNC):
336 return true;
338 CASE_FLT_FN (BUILT_IN_LLRINT):
339 CASE_FLT_FN (BUILT_IN_LRINT):
340 CASE_FLT_FN (BUILT_IN_NEARBYINT):
341 CASE_FLT_FN (BUILT_IN_RINT):
342 return !flag_rounding_math;
344 default:
345 break;
347 return false;
350 /* Check whether we may negate an integer constant T without causing
351 overflow. */
353 bool
354 may_negate_without_overflow_p (const_tree t)
356 unsigned HOST_WIDE_INT val;
357 unsigned int prec;
358 tree type;
360 gcc_assert (TREE_CODE (t) == INTEGER_CST);
362 type = TREE_TYPE (t);
363 if (TYPE_UNSIGNED (type))
364 return false;
366 prec = TYPE_PRECISION (type);
367 if (prec > HOST_BITS_PER_WIDE_INT)
369 if (TREE_INT_CST_LOW (t) != 0)
370 return true;
371 prec -= HOST_BITS_PER_WIDE_INT;
372 val = TREE_INT_CST_HIGH (t);
374 else
375 val = TREE_INT_CST_LOW (t);
376 if (prec < HOST_BITS_PER_WIDE_INT)
377 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
378 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
381 /* Determine whether an expression T can be cheaply negated using
382 the function negate_expr without introducing undefined overflow. */
384 static bool
385 negate_expr_p (tree t)
387 tree type;
389 if (t == 0)
390 return false;
392 type = TREE_TYPE (t);
394 STRIP_SIGN_NOPS (t);
395 switch (TREE_CODE (t))
397 case INTEGER_CST:
398 if (TYPE_OVERFLOW_WRAPS (type))
399 return true;
401 /* Check that -CST will not overflow type. */
402 return may_negate_without_overflow_p (t);
403 case BIT_NOT_EXPR:
404 return (INTEGRAL_TYPE_P (type)
405 && TYPE_OVERFLOW_WRAPS (type));
407 case FIXED_CST:
408 case NEGATE_EXPR:
409 return true;
411 case REAL_CST:
412 /* We want to canonicalize to positive real constants. Pretend
413 that only negative ones can be easily negated. */
414 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
416 case COMPLEX_CST:
417 return negate_expr_p (TREE_REALPART (t))
418 && negate_expr_p (TREE_IMAGPART (t));
420 case COMPLEX_EXPR:
421 return negate_expr_p (TREE_OPERAND (t, 0))
422 && negate_expr_p (TREE_OPERAND (t, 1));
424 case CONJ_EXPR:
425 return negate_expr_p (TREE_OPERAND (t, 0));
427 case PLUS_EXPR:
428 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
429 || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
430 return false;
431 /* -(A + B) -> (-B) - A. */
432 if (negate_expr_p (TREE_OPERAND (t, 1))
433 && reorder_operands_p (TREE_OPERAND (t, 0),
434 TREE_OPERAND (t, 1)))
435 return true;
436 /* -(A + B) -> (-A) - B. */
437 return negate_expr_p (TREE_OPERAND (t, 0));
439 case MINUS_EXPR:
440 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
441 return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
442 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
443 && reorder_operands_p (TREE_OPERAND (t, 0),
444 TREE_OPERAND (t, 1));
446 case MULT_EXPR:
447 if (TYPE_UNSIGNED (TREE_TYPE (t)))
448 break;
450 /* Fall through. */
452 case RDIV_EXPR:
453 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
454 return negate_expr_p (TREE_OPERAND (t, 1))
455 || negate_expr_p (TREE_OPERAND (t, 0));
456 break;
458 case TRUNC_DIV_EXPR:
459 case ROUND_DIV_EXPR:
460 case FLOOR_DIV_EXPR:
461 case CEIL_DIV_EXPR:
462 case EXACT_DIV_EXPR:
463 /* In general we can't negate A / B, because if A is INT_MIN and
464 B is 1, we may turn this into INT_MIN / -1 which is undefined
465 and actually traps on some architectures. But if overflow is
466 undefined, we can negate, because - (INT_MIN / 1) is an
467 overflow. */
468 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
469 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
470 break;
471 return negate_expr_p (TREE_OPERAND (t, 1))
472 || negate_expr_p (TREE_OPERAND (t, 0));
474 case NOP_EXPR:
475 /* Negate -((double)float) as (double)(-float). */
476 if (TREE_CODE (type) == REAL_TYPE)
478 tree tem = strip_float_extensions (t);
479 if (tem != t)
480 return negate_expr_p (tem);
482 break;
484 case CALL_EXPR:
485 /* Negate -f(x) as f(-x). */
486 if (negate_mathfn_p (builtin_mathfn_code (t)))
487 return negate_expr_p (CALL_EXPR_ARG (t, 0));
488 break;
490 case RSHIFT_EXPR:
491 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
492 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
494 tree op1 = TREE_OPERAND (t, 1);
495 if (TREE_INT_CST_HIGH (op1) == 0
496 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
497 == TREE_INT_CST_LOW (op1))
498 return true;
500 break;
502 default:
503 break;
505 return false;
508 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
509 simplification is possible.
510 If negate_expr_p would return true for T, NULL_TREE will never be
511 returned. */
513 static tree
514 fold_negate_expr (location_t loc, tree t)
516 tree type = TREE_TYPE (t);
517 tree tem;
519 switch (TREE_CODE (t))
521 /* Convert - (~A) to A + 1. */
522 case BIT_NOT_EXPR:
523 if (INTEGRAL_TYPE_P (type))
524 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
525 build_int_cst (type, 1));
526 break;
528 case INTEGER_CST:
529 tem = fold_negate_const (t, type);
530 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
531 || !TYPE_OVERFLOW_TRAPS (type))
532 return tem;
533 break;
535 case REAL_CST:
536 tem = fold_negate_const (t, type);
537 /* Two's complement FP formats, such as c4x, may overflow. */
538 if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
539 return tem;
540 break;
542 case FIXED_CST:
543 tem = fold_negate_const (t, type);
544 return tem;
546 case COMPLEX_CST:
548 tree rpart = negate_expr (TREE_REALPART (t));
549 tree ipart = negate_expr (TREE_IMAGPART (t));
551 if ((TREE_CODE (rpart) == REAL_CST
552 && TREE_CODE (ipart) == REAL_CST)
553 || (TREE_CODE (rpart) == INTEGER_CST
554 && TREE_CODE (ipart) == INTEGER_CST))
555 return build_complex (type, rpart, ipart);
557 break;
559 case COMPLEX_EXPR:
560 if (negate_expr_p (t))
561 return fold_build2_loc (loc, COMPLEX_EXPR, type,
562 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
563 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
564 break;
566 case CONJ_EXPR:
567 if (negate_expr_p (t))
568 return fold_build1_loc (loc, CONJ_EXPR, type,
569 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
570 break;
572 case NEGATE_EXPR:
573 return TREE_OPERAND (t, 0);
575 case PLUS_EXPR:
576 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
577 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
579 /* -(A + B) -> (-B) - A. */
580 if (negate_expr_p (TREE_OPERAND (t, 1))
581 && reorder_operands_p (TREE_OPERAND (t, 0),
582 TREE_OPERAND (t, 1)))
584 tem = negate_expr (TREE_OPERAND (t, 1));
585 return fold_build2_loc (loc, MINUS_EXPR, type,
586 tem, TREE_OPERAND (t, 0));
589 /* -(A + B) -> (-A) - B. */
590 if (negate_expr_p (TREE_OPERAND (t, 0)))
592 tem = negate_expr (TREE_OPERAND (t, 0));
593 return fold_build2_loc (loc, MINUS_EXPR, type,
594 tem, TREE_OPERAND (t, 1));
597 break;
599 case MINUS_EXPR:
600 /* - (A - B) -> B - A */
601 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
602 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
603 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
604 return fold_build2_loc (loc, MINUS_EXPR, type,
605 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
606 break;
608 case MULT_EXPR:
609 if (TYPE_UNSIGNED (type))
610 break;
612 /* Fall through. */
614 case RDIV_EXPR:
615 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
617 tem = TREE_OPERAND (t, 1);
618 if (negate_expr_p (tem))
619 return fold_build2_loc (loc, TREE_CODE (t), type,
620 TREE_OPERAND (t, 0), negate_expr (tem));
621 tem = TREE_OPERAND (t, 0);
622 if (negate_expr_p (tem))
623 return fold_build2_loc (loc, TREE_CODE (t), type,
624 negate_expr (tem), TREE_OPERAND (t, 1));
626 break;
628 case TRUNC_DIV_EXPR:
629 case ROUND_DIV_EXPR:
630 case FLOOR_DIV_EXPR:
631 case CEIL_DIV_EXPR:
632 case EXACT_DIV_EXPR:
633 /* In general we can't negate A / B, because if A is INT_MIN and
634 B is 1, we may turn this into INT_MIN / -1 which is undefined
635 and actually traps on some architectures. But if overflow is
636 undefined, we can negate, because - (INT_MIN / 1) is an
637 overflow. */
638 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
640 const char * const warnmsg = G_("assuming signed overflow does not "
641 "occur when negating a division");
642 tem = TREE_OPERAND (t, 1);
643 if (negate_expr_p (tem))
645 if (INTEGRAL_TYPE_P (type)
646 && (TREE_CODE (tem) != INTEGER_CST
647 || integer_onep (tem)))
648 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
649 return fold_build2_loc (loc, TREE_CODE (t), type,
650 TREE_OPERAND (t, 0), negate_expr (tem));
652 tem = TREE_OPERAND (t, 0);
653 if (negate_expr_p (tem))
655 if (INTEGRAL_TYPE_P (type)
656 && (TREE_CODE (tem) != INTEGER_CST
657 || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
658 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
659 return fold_build2_loc (loc, TREE_CODE (t), type,
660 negate_expr (tem), TREE_OPERAND (t, 1));
663 break;
665 case NOP_EXPR:
666 /* Convert -((double)float) into (double)(-float). */
667 if (TREE_CODE (type) == REAL_TYPE)
669 tem = strip_float_extensions (t);
670 if (tem != t && negate_expr_p (tem))
671 return fold_convert_loc (loc, type, negate_expr (tem));
673 break;
675 case CALL_EXPR:
676 /* Negate -f(x) as f(-x). */
677 if (negate_mathfn_p (builtin_mathfn_code (t))
678 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
680 tree fndecl, arg;
682 fndecl = get_callee_fndecl (t);
683 arg = negate_expr (CALL_EXPR_ARG (t, 0));
684 return build_call_expr_loc (loc, fndecl, 1, arg);
686 break;
688 case RSHIFT_EXPR:
689 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
690 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
692 tree op1 = TREE_OPERAND (t, 1);
693 if (TREE_INT_CST_HIGH (op1) == 0
694 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
695 == TREE_INT_CST_LOW (op1))
697 tree ntype = TYPE_UNSIGNED (type)
698 ? signed_type_for (type)
699 : unsigned_type_for (type);
700 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
701 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
702 return fold_convert_loc (loc, type, temp);
705 break;
707 default:
708 break;
711 return NULL_TREE;
714 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
715 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
716 return NULL_TREE. */
718 static tree
719 negate_expr (tree t)
721 tree type, tem;
722 location_t loc;
724 if (t == NULL_TREE)
725 return NULL_TREE;
727 loc = EXPR_LOCATION (t);
728 type = TREE_TYPE (t);
729 STRIP_SIGN_NOPS (t);
731 tem = fold_negate_expr (loc, t);
732 if (!tem)
734 tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
735 SET_EXPR_LOCATION (tem, loc);
737 return fold_convert_loc (loc, type, tem);
740 /* Split a tree IN into a constant, literal and variable parts that could be
741 combined with CODE to make IN. "constant" means an expression with
742 TREE_CONSTANT but that isn't an actual constant. CODE must be a
743 commutative arithmetic operation. Store the constant part into *CONP,
744 the literal in *LITP and return the variable part. If a part isn't
745 present, set it to null. If the tree does not decompose in this way,
746 return the entire tree as the variable part and the other parts as null.
748 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
749 case, we negate an operand that was subtracted. Except if it is a
750 literal for which we use *MINUS_LITP instead.
752 If NEGATE_P is true, we are negating all of IN, again except a literal
753 for which we use *MINUS_LITP instead.
755 If IN is itself a literal or constant, return it as appropriate.
757 Note that we do not guarantee that any of the three values will be the
758 same type as IN, but they will have the same signedness and mode. */
760 static tree
761 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
762 tree *minus_litp, int negate_p)
764 tree var = 0;
766 *conp = 0;
767 *litp = 0;
768 *minus_litp = 0;
770 /* Strip any conversions that don't change the machine mode or signedness. */
771 STRIP_SIGN_NOPS (in);
773 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
774 || TREE_CODE (in) == FIXED_CST)
775 *litp = in;
776 else if (TREE_CODE (in) == code
777 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
778 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
779 /* We can associate addition and subtraction together (even
780 though the C standard doesn't say so) for integers because
781 the value is not affected. For reals, the value might be
782 affected, so we can't. */
783 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
784 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
786 tree op0 = TREE_OPERAND (in, 0);
787 tree op1 = TREE_OPERAND (in, 1);
788 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
789 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
791 /* First see if either of the operands is a literal, then a constant. */
792 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
793 || TREE_CODE (op0) == FIXED_CST)
794 *litp = op0, op0 = 0;
795 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
796 || TREE_CODE (op1) == FIXED_CST)
797 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
799 if (op0 != 0 && TREE_CONSTANT (op0))
800 *conp = op0, op0 = 0;
801 else if (op1 != 0 && TREE_CONSTANT (op1))
802 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
804 /* If we haven't dealt with either operand, this is not a case we can
805 decompose. Otherwise, VAR is either of the ones remaining, if any. */
806 if (op0 != 0 && op1 != 0)
807 var = in;
808 else if (op0 != 0)
809 var = op0;
810 else
811 var = op1, neg_var_p = neg1_p;
813 /* Now do any needed negations. */
814 if (neg_litp_p)
815 *minus_litp = *litp, *litp = 0;
816 if (neg_conp_p)
817 *conp = negate_expr (*conp);
818 if (neg_var_p)
819 var = negate_expr (var);
821 else if (TREE_CONSTANT (in))
822 *conp = in;
823 else
824 var = in;
826 if (negate_p)
828 if (*litp)
829 *minus_litp = *litp, *litp = 0;
830 else if (*minus_litp)
831 *litp = *minus_litp, *minus_litp = 0;
832 *conp = negate_expr (*conp);
833 var = negate_expr (var);
836 return var;
839 /* Re-associate trees split by the above function. T1 and T2 are
840 either expressions to associate or null. Return the new
841 expression, if any. LOC is the location of the new expression. If
842 we build an operation, do it in TYPE and with CODE. */
844 static tree
845 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
847 tree tem;
849 if (t1 == 0)
850 return t2;
851 else if (t2 == 0)
852 return t1;
854 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
855 try to fold this since we will have infinite recursion. But do
856 deal with any NEGATE_EXPRs. */
857 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
858 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
860 if (code == PLUS_EXPR)
862 if (TREE_CODE (t1) == NEGATE_EXPR)
863 tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t2),
864 fold_convert_loc (loc, type, TREE_OPERAND (t1, 0)));
865 else if (TREE_CODE (t2) == NEGATE_EXPR)
866 tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t1),
867 fold_convert_loc (loc, type, TREE_OPERAND (t2, 0)));
868 else if (integer_zerop (t2))
869 return fold_convert_loc (loc, type, t1);
871 else if (code == MINUS_EXPR)
873 if (integer_zerop (t2))
874 return fold_convert_loc (loc, type, t1);
877 tem = build2 (code, type, fold_convert_loc (loc, type, t1),
878 fold_convert_loc (loc, type, t2));
879 goto associate_trees_exit;
882 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
883 fold_convert_loc (loc, type, t2));
884 associate_trees_exit:
885 protected_set_expr_location (tem, loc);
886 return tem;
889 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
890 for use in int_const_binop, size_binop and size_diffop. */
892 static bool
893 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
895 if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
896 return false;
897 if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
898 return false;
900 switch (code)
902 case LSHIFT_EXPR:
903 case RSHIFT_EXPR:
904 case LROTATE_EXPR:
905 case RROTATE_EXPR:
906 return true;
908 default:
909 break;
912 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
913 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
914 && TYPE_MODE (type1) == TYPE_MODE (type2);
918 /* Combine two integer constants ARG1 and ARG2 under operation CODE
919 to produce a new constant. Return NULL_TREE if we don't know how
920 to evaluate CODE at compile-time.
922 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
924 tree
925 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
927 unsigned HOST_WIDE_INT int1l, int2l;
928 HOST_WIDE_INT int1h, int2h;
929 unsigned HOST_WIDE_INT low;
930 HOST_WIDE_INT hi;
931 unsigned HOST_WIDE_INT garbagel;
932 HOST_WIDE_INT garbageh;
933 tree t;
934 tree type = TREE_TYPE (arg1);
935 int uns = TYPE_UNSIGNED (type);
936 int is_sizetype
937 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
938 int overflow = 0;
940 int1l = TREE_INT_CST_LOW (arg1);
941 int1h = TREE_INT_CST_HIGH (arg1);
942 int2l = TREE_INT_CST_LOW (arg2);
943 int2h = TREE_INT_CST_HIGH (arg2);
945 switch (code)
947 case BIT_IOR_EXPR:
948 low = int1l | int2l, hi = int1h | int2h;
949 break;
951 case BIT_XOR_EXPR:
952 low = int1l ^ int2l, hi = int1h ^ int2h;
953 break;
955 case BIT_AND_EXPR:
956 low = int1l & int2l, hi = int1h & int2h;
957 break;
959 case RSHIFT_EXPR:
960 int2l = -int2l;
961 case LSHIFT_EXPR:
962 /* It's unclear from the C standard whether shifts can overflow.
963 The following code ignores overflow; perhaps a C standard
964 interpretation ruling is needed. */
965 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
966 &low, &hi, !uns);
967 break;
969 case RROTATE_EXPR:
970 int2l = - int2l;
971 case LROTATE_EXPR:
972 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
973 &low, &hi);
974 break;
976 case PLUS_EXPR:
977 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
978 break;
980 case MINUS_EXPR:
981 neg_double (int2l, int2h, &low, &hi);
982 add_double (int1l, int1h, low, hi, &low, &hi);
983 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
984 break;
986 case MULT_EXPR:
987 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
988 break;
990 case TRUNC_DIV_EXPR:
991 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
992 case EXACT_DIV_EXPR:
993 /* This is a shortcut for a common special case. */
994 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
995 && !TREE_OVERFLOW (arg1)
996 && !TREE_OVERFLOW (arg2)
997 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
999 if (code == CEIL_DIV_EXPR)
1000 int1l += int2l - 1;
1002 low = int1l / int2l, hi = 0;
1003 break;
1006 /* ... fall through ... */
1008 case ROUND_DIV_EXPR:
1009 if (int2h == 0 && int2l == 0)
1010 return NULL_TREE;
1011 if (int2h == 0 && int2l == 1)
1013 low = int1l, hi = int1h;
1014 break;
1016 if (int1l == int2l && int1h == int2h
1017 && ! (int1l == 0 && int1h == 0))
1019 low = 1, hi = 0;
1020 break;
1022 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1023 &low, &hi, &garbagel, &garbageh);
1024 break;
1026 case TRUNC_MOD_EXPR:
1027 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1028 /* This is a shortcut for a common special case. */
1029 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1030 && !TREE_OVERFLOW (arg1)
1031 && !TREE_OVERFLOW (arg2)
1032 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1034 if (code == CEIL_MOD_EXPR)
1035 int1l += int2l - 1;
1036 low = int1l % int2l, hi = 0;
1037 break;
1040 /* ... fall through ... */
1042 case ROUND_MOD_EXPR:
1043 if (int2h == 0 && int2l == 0)
1044 return NULL_TREE;
1045 overflow = div_and_round_double (code, uns,
1046 int1l, int1h, int2l, int2h,
1047 &garbagel, &garbageh, &low, &hi);
1048 break;
1050 case MIN_EXPR:
1051 case MAX_EXPR:
1052 if (uns)
1053 low = (((unsigned HOST_WIDE_INT) int1h
1054 < (unsigned HOST_WIDE_INT) int2h)
1055 || (((unsigned HOST_WIDE_INT) int1h
1056 == (unsigned HOST_WIDE_INT) int2h)
1057 && int1l < int2l));
1058 else
1059 low = (int1h < int2h
1060 || (int1h == int2h && int1l < int2l));
1062 if (low == (code == MIN_EXPR))
1063 low = int1l, hi = int1h;
1064 else
1065 low = int2l, hi = int2h;
1066 break;
1068 default:
1069 return NULL_TREE;
1072 if (notrunc)
1074 t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1076 /* Propagate overflow flags ourselves. */
1077 if (((!uns || is_sizetype) && overflow)
1078 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1080 t = copy_node (t);
1081 TREE_OVERFLOW (t) = 1;
1084 else
1085 t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1086 ((!uns || is_sizetype) && overflow)
1087 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1089 return t;
1092 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1093 constant. We assume ARG1 and ARG2 have the same data type, or at least
1094 are the same kind of constant and the same machine mode. Return zero if
1095 combining the constants is not allowed in the current operating mode.
1097 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1099 static tree
1100 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1102 /* Sanity check for the recursive cases. */
1103 if (!arg1 || !arg2)
1104 return NULL_TREE;
1106 STRIP_NOPS (arg1);
1107 STRIP_NOPS (arg2);
1109 if (TREE_CODE (arg1) == INTEGER_CST)
1110 return int_const_binop (code, arg1, arg2, notrunc);
1112 if (TREE_CODE (arg1) == REAL_CST)
1114 enum machine_mode mode;
1115 REAL_VALUE_TYPE d1;
1116 REAL_VALUE_TYPE d2;
1117 REAL_VALUE_TYPE value;
1118 REAL_VALUE_TYPE result;
1119 bool inexact;
1120 tree t, type;
1122 /* The following codes are handled by real_arithmetic. */
1123 switch (code)
1125 case PLUS_EXPR:
1126 case MINUS_EXPR:
1127 case MULT_EXPR:
1128 case RDIV_EXPR:
1129 case MIN_EXPR:
1130 case MAX_EXPR:
1131 break;
1133 default:
1134 return NULL_TREE;
1137 d1 = TREE_REAL_CST (arg1);
1138 d2 = TREE_REAL_CST (arg2);
1140 type = TREE_TYPE (arg1);
1141 mode = TYPE_MODE (type);
1143 /* Don't perform operation if we honor signaling NaNs and
1144 either operand is a NaN. */
1145 if (HONOR_SNANS (mode)
1146 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1147 return NULL_TREE;
1149 /* Don't perform operation if it would raise a division
1150 by zero exception. */
1151 if (code == RDIV_EXPR
1152 && REAL_VALUES_EQUAL (d2, dconst0)
1153 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1154 return NULL_TREE;
1156 /* If either operand is a NaN, just return it. Otherwise, set up
1157 for floating-point trap; we return an overflow. */
1158 if (REAL_VALUE_ISNAN (d1))
1159 return arg1;
1160 else if (REAL_VALUE_ISNAN (d2))
1161 return arg2;
1163 inexact = real_arithmetic (&value, code, &d1, &d2);
1164 real_convert (&result, mode, &value);
1166 /* Don't constant fold this floating point operation if
1167 the result has overflowed and flag_trapping_math. */
1168 if (flag_trapping_math
1169 && MODE_HAS_INFINITIES (mode)
1170 && REAL_VALUE_ISINF (result)
1171 && !REAL_VALUE_ISINF (d1)
1172 && !REAL_VALUE_ISINF (d2))
1173 return NULL_TREE;
1175 /* Don't constant fold this floating point operation if the
1176 result may dependent upon the run-time rounding mode and
1177 flag_rounding_math is set, or if GCC's software emulation
1178 is unable to accurately represent the result. */
1179 if ((flag_rounding_math
1180 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1181 && (inexact || !real_identical (&result, &value)))
1182 return NULL_TREE;
1184 t = build_real (type, result);
1186 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1187 return t;
1190 if (TREE_CODE (arg1) == FIXED_CST)
1192 FIXED_VALUE_TYPE f1;
1193 FIXED_VALUE_TYPE f2;
1194 FIXED_VALUE_TYPE result;
1195 tree t, type;
1196 int sat_p;
1197 bool overflow_p;
1199 /* The following codes are handled by fixed_arithmetic. */
1200 switch (code)
1202 case PLUS_EXPR:
1203 case MINUS_EXPR:
1204 case MULT_EXPR:
1205 case TRUNC_DIV_EXPR:
1206 f2 = TREE_FIXED_CST (arg2);
1207 break;
1209 case LSHIFT_EXPR:
1210 case RSHIFT_EXPR:
1211 f2.data.high = TREE_INT_CST_HIGH (arg2);
1212 f2.data.low = TREE_INT_CST_LOW (arg2);
1213 f2.mode = SImode;
1214 break;
1216 default:
1217 return NULL_TREE;
1220 f1 = TREE_FIXED_CST (arg1);
1221 type = TREE_TYPE (arg1);
1222 sat_p = TYPE_SATURATING (type);
1223 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1224 t = build_fixed (type, result);
1225 /* Propagate overflow flags. */
1226 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1227 TREE_OVERFLOW (t) = 1;
1228 return t;
1231 if (TREE_CODE (arg1) == COMPLEX_CST)
1233 tree type = TREE_TYPE (arg1);
1234 tree r1 = TREE_REALPART (arg1);
1235 tree i1 = TREE_IMAGPART (arg1);
1236 tree r2 = TREE_REALPART (arg2);
1237 tree i2 = TREE_IMAGPART (arg2);
1238 tree real, imag;
1240 switch (code)
1242 case PLUS_EXPR:
1243 case MINUS_EXPR:
1244 real = const_binop (code, r1, r2, notrunc);
1245 imag = const_binop (code, i1, i2, notrunc);
1246 break;
1248 case MULT_EXPR:
1249 if (COMPLEX_FLOAT_TYPE_P (type))
1250 return do_mpc_arg2 (arg1, arg2, type,
1251 /* do_nonfinite= */ folding_initializer,
1252 mpc_mul);
1254 real = const_binop (MINUS_EXPR,
1255 const_binop (MULT_EXPR, r1, r2, notrunc),
1256 const_binop (MULT_EXPR, i1, i2, notrunc),
1257 notrunc);
1258 imag = const_binop (PLUS_EXPR,
1259 const_binop (MULT_EXPR, r1, i2, notrunc),
1260 const_binop (MULT_EXPR, i1, r2, notrunc),
1261 notrunc);
1262 break;
1264 case RDIV_EXPR:
1265 if (COMPLEX_FLOAT_TYPE_P (type))
1266 return do_mpc_arg2 (arg1, arg2, type,
1267 /* do_nonfinite= */ folding_initializer,
1268 mpc_div);
1269 /* Fallthru ... */
1270 case TRUNC_DIV_EXPR:
1271 case CEIL_DIV_EXPR:
1272 case FLOOR_DIV_EXPR:
1273 case ROUND_DIV_EXPR:
1274 if (flag_complex_method == 0)
1276 /* Keep this algorithm in sync with
1277 tree-complex.c:expand_complex_div_straight().
1279 Expand complex division to scalars, straightforward algorithm.
1280 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1281 t = br*br + bi*bi
1283 tree magsquared
1284 = const_binop (PLUS_EXPR,
1285 const_binop (MULT_EXPR, r2, r2, notrunc),
1286 const_binop (MULT_EXPR, i2, i2, notrunc),
1287 notrunc);
1288 tree t1
1289 = const_binop (PLUS_EXPR,
1290 const_binop (MULT_EXPR, r1, r2, notrunc),
1291 const_binop (MULT_EXPR, i1, i2, notrunc),
1292 notrunc);
1293 tree t2
1294 = const_binop (MINUS_EXPR,
1295 const_binop (MULT_EXPR, i1, r2, notrunc),
1296 const_binop (MULT_EXPR, r1, i2, notrunc),
1297 notrunc);
1299 real = const_binop (code, t1, magsquared, notrunc);
1300 imag = const_binop (code, t2, magsquared, notrunc);
1302 else
1304 /* Keep this algorithm in sync with
1305 tree-complex.c:expand_complex_div_wide().
1307 Expand complex division to scalars, modified algorithm to minimize
1308 overflow with wide input ranges. */
1309 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1310 fold_abs_const (r2, TREE_TYPE (type)),
1311 fold_abs_const (i2, TREE_TYPE (type)));
1313 if (integer_nonzerop (compare))
1315 /* In the TRUE branch, we compute
1316 ratio = br/bi;
1317 div = (br * ratio) + bi;
1318 tr = (ar * ratio) + ai;
1319 ti = (ai * ratio) - ar;
1320 tr = tr / div;
1321 ti = ti / div; */
1322 tree ratio = const_binop (code, r2, i2, notrunc);
1323 tree div = const_binop (PLUS_EXPR, i2,
1324 const_binop (MULT_EXPR, r2, ratio,
1325 notrunc),
1326 notrunc);
1327 real = const_binop (MULT_EXPR, r1, ratio, notrunc);
1328 real = const_binop (PLUS_EXPR, real, i1, notrunc);
1329 real = const_binop (code, real, div, notrunc);
1331 imag = const_binop (MULT_EXPR, i1, ratio, notrunc);
1332 imag = const_binop (MINUS_EXPR, imag, r1, notrunc);
1333 imag = const_binop (code, imag, div, notrunc);
1335 else
1337 /* In the FALSE branch, we compute
1338 ratio = d/c;
1339 divisor = (d * ratio) + c;
1340 tr = (b * ratio) + a;
1341 ti = b - (a * ratio);
1342 tr = tr / div;
1343 ti = ti / div; */
1344 tree ratio = const_binop (code, i2, r2, notrunc);
1345 tree div = const_binop (PLUS_EXPR, r2,
1346 const_binop (MULT_EXPR, i2, ratio,
1347 notrunc),
1348 notrunc);
1350 real = const_binop (MULT_EXPR, i1, ratio, notrunc);
1351 real = const_binop (PLUS_EXPR, real, r1, notrunc);
1352 real = const_binop (code, real, div, notrunc);
1354 imag = const_binop (MULT_EXPR, r1, ratio, notrunc);
1355 imag = const_binop (MINUS_EXPR, i1, imag, notrunc);
1356 imag = const_binop (code, imag, div, notrunc);
1359 break;
1361 default:
1362 return NULL_TREE;
1365 if (real && imag)
1366 return build_complex (type, real, imag);
1369 if (TREE_CODE (arg1) == VECTOR_CST)
1371 tree type = TREE_TYPE(arg1);
1372 int count = TYPE_VECTOR_SUBPARTS (type), i;
1373 tree elements1, elements2, list = NULL_TREE;
1375 if(TREE_CODE(arg2) != VECTOR_CST)
1376 return NULL_TREE;
1378 elements1 = TREE_VECTOR_CST_ELTS (arg1);
1379 elements2 = TREE_VECTOR_CST_ELTS (arg2);
1381 for (i = 0; i < count; i++)
1383 tree elem1, elem2, elem;
1385 /* The trailing elements can be empty and should be treated as 0 */
1386 if(!elements1)
1387 elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1388 else
1390 elem1 = TREE_VALUE(elements1);
1391 elements1 = TREE_CHAIN (elements1);
1394 if(!elements2)
1395 elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1396 else
1398 elem2 = TREE_VALUE(elements2);
1399 elements2 = TREE_CHAIN (elements2);
1402 elem = const_binop (code, elem1, elem2, notrunc);
1404 /* It is possible that const_binop cannot handle the given
1405 code and return NULL_TREE */
1406 if(elem == NULL_TREE)
1407 return NULL_TREE;
1409 list = tree_cons (NULL_TREE, elem, list);
1411 return build_vector(type, nreverse(list));
1413 return NULL_TREE;
1416 /* Create a size type INT_CST node with NUMBER sign extended. KIND
1417 indicates which particular sizetype to create. */
1419 tree
1420 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1422 return build_int_cst (sizetype_tab[(int) kind], number);
1425 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1426 is a tree code. The type of the result is taken from the operands.
1427 Both must be equivalent integer types, ala int_binop_types_match_p.
1428 If the operands are constant, so is the result. */
1430 tree
1431 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1433 tree type = TREE_TYPE (arg0);
1435 if (arg0 == error_mark_node || arg1 == error_mark_node)
1436 return error_mark_node;
1438 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1439 TREE_TYPE (arg1)));
1441 /* Handle the special case of two integer constants faster. */
1442 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1444 /* And some specific cases even faster than that. */
1445 if (code == PLUS_EXPR)
1447 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1448 return arg1;
1449 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1450 return arg0;
1452 else if (code == MINUS_EXPR)
1454 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1455 return arg0;
1457 else if (code == MULT_EXPR)
1459 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1460 return arg1;
1463 /* Handle general case of two integer constants. */
1464 return int_const_binop (code, arg0, arg1, 0);
1467 return fold_build2_loc (loc, code, type, arg0, arg1);
1470 /* Given two values, either both of sizetype or both of bitsizetype,
1471 compute the difference between the two values. Return the value
1472 in signed type corresponding to the type of the operands. */
1474 tree
1475 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1477 tree type = TREE_TYPE (arg0);
1478 tree ctype;
1480 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1481 TREE_TYPE (arg1)));
1483 /* If the type is already signed, just do the simple thing. */
1484 if (!TYPE_UNSIGNED (type))
1485 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1487 if (type == sizetype)
1488 ctype = ssizetype;
1489 else if (type == bitsizetype)
1490 ctype = sbitsizetype;
1491 else
1492 ctype = signed_type_for (type);
1494 /* If either operand is not a constant, do the conversions to the signed
1495 type and subtract. The hardware will do the right thing with any
1496 overflow in the subtraction. */
1497 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1498 return size_binop_loc (loc, MINUS_EXPR,
1499 fold_convert_loc (loc, ctype, arg0),
1500 fold_convert_loc (loc, ctype, arg1));
1502 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1503 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1504 overflow) and negate (which can't either). Special-case a result
1505 of zero while we're here. */
1506 if (tree_int_cst_equal (arg0, arg1))
1507 return build_int_cst (ctype, 0);
1508 else if (tree_int_cst_lt (arg1, arg0))
1509 return fold_convert_loc (loc, ctype,
1510 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1511 else
1512 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1513 fold_convert_loc (loc, ctype,
1514 size_binop_loc (loc,
1515 MINUS_EXPR,
1516 arg1, arg0)));
1519 /* A subroutine of fold_convert_const handling conversions of an
1520 INTEGER_CST to another integer type. */
1522 static tree
1523 fold_convert_const_int_from_int (tree type, const_tree arg1)
1525 tree t;
1527 /* Given an integer constant, make new constant with new type,
1528 appropriately sign-extended or truncated. */
1529 t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
1530 TREE_INT_CST_HIGH (arg1),
1531 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1532 (TREE_INT_CST_HIGH (arg1) < 0
1533 && (TYPE_UNSIGNED (type)
1534 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1535 | TREE_OVERFLOW (arg1));
1537 return t;
1540 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1541 to an integer type. */
1543 static tree
1544 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1546 int overflow = 0;
1547 tree t;
1549 /* The following code implements the floating point to integer
1550 conversion rules required by the Java Language Specification,
1551 that IEEE NaNs are mapped to zero and values that overflow
1552 the target precision saturate, i.e. values greater than
1553 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1554 are mapped to INT_MIN. These semantics are allowed by the
1555 C and C++ standards that simply state that the behavior of
1556 FP-to-integer conversion is unspecified upon overflow. */
1558 double_int val;
1559 REAL_VALUE_TYPE r;
1560 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1562 switch (code)
1564 case FIX_TRUNC_EXPR:
1565 real_trunc (&r, VOIDmode, &x);
1566 break;
1568 default:
1569 gcc_unreachable ();
1572 /* If R is NaN, return zero and show we have an overflow. */
1573 if (REAL_VALUE_ISNAN (r))
1575 overflow = 1;
1576 val = double_int_zero;
1579 /* See if R is less than the lower bound or greater than the
1580 upper bound. */
1582 if (! overflow)
1584 tree lt = TYPE_MIN_VALUE (type);
1585 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1586 if (REAL_VALUES_LESS (r, l))
1588 overflow = 1;
1589 val = tree_to_double_int (lt);
1593 if (! overflow)
1595 tree ut = TYPE_MAX_VALUE (type);
1596 if (ut)
1598 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1599 if (REAL_VALUES_LESS (u, r))
1601 overflow = 1;
1602 val = tree_to_double_int (ut);
1607 if (! overflow)
1608 real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r);
1610 t = force_fit_type_double (type, val.low, val.high, -1,
1611 overflow | TREE_OVERFLOW (arg1));
1612 return t;
1615 /* A subroutine of fold_convert_const handling conversions of a
1616 FIXED_CST to an integer type. */
1618 static tree
1619 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1621 tree t;
1622 double_int temp, temp_trunc;
1623 unsigned int mode;
1625 /* Right shift FIXED_CST to temp by fbit. */
1626 temp = TREE_FIXED_CST (arg1).data;
1627 mode = TREE_FIXED_CST (arg1).mode;
1628 if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
1630 temp = double_int_rshift (temp, GET_MODE_FBIT (mode),
1631 HOST_BITS_PER_DOUBLE_INT,
1632 SIGNED_FIXED_POINT_MODE_P (mode));
1634 /* Left shift temp to temp_trunc by fbit. */
1635 temp_trunc = double_int_lshift (temp, GET_MODE_FBIT (mode),
1636 HOST_BITS_PER_DOUBLE_INT,
1637 SIGNED_FIXED_POINT_MODE_P (mode));
1639 else
1641 temp = double_int_zero;
1642 temp_trunc = double_int_zero;
1645 /* If FIXED_CST is negative, we need to round the value toward 0.
1646 By checking if the fractional bits are not zero to add 1 to temp. */
1647 if (SIGNED_FIXED_POINT_MODE_P (mode)
1648 && double_int_negative_p (temp_trunc)
1649 && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
1650 temp = double_int_add (temp, double_int_one);
1652 /* Given a fixed-point constant, make new constant with new type,
1653 appropriately sign-extended or truncated. */
1654 t = force_fit_type_double (type, temp.low, temp.high, -1,
1655 (double_int_negative_p (temp)
1656 && (TYPE_UNSIGNED (type)
1657 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1658 | TREE_OVERFLOW (arg1));
1660 return t;
1663 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1664 to another floating point type. */
1666 static tree
1667 fold_convert_const_real_from_real (tree type, const_tree arg1)
1669 REAL_VALUE_TYPE value;
1670 tree t;
1672 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1673 t = build_real (type, value);
1675 /* If converting an infinity or NAN to a representation that doesn't
1676 have one, set the overflow bit so that we can produce some kind of
1677 error message at the appropriate point if necessary. It's not the
1678 most user-friendly message, but it's better than nothing. */
1679 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1680 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1681 TREE_OVERFLOW (t) = 1;
1682 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1683 && !MODE_HAS_NANS (TYPE_MODE (type)))
1684 TREE_OVERFLOW (t) = 1;
1685 /* Regular overflow, conversion produced an infinity in a mode that
1686 can't represent them. */
1687 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1688 && REAL_VALUE_ISINF (value)
1689 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1690 TREE_OVERFLOW (t) = 1;
1691 else
1692 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1693 return t;
1696 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1697 to a floating point type. */
1699 static tree
1700 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1702 REAL_VALUE_TYPE value;
1703 tree t;
1705 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1706 t = build_real (type, value);
1708 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1709 return t;
1712 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1713 to another fixed-point type. */
1715 static tree
1716 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1718 FIXED_VALUE_TYPE value;
1719 tree t;
1720 bool overflow_p;
1722 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1723 TYPE_SATURATING (type));
1724 t = build_fixed (type, value);
1726 /* Propagate overflow flags. */
1727 if (overflow_p | TREE_OVERFLOW (arg1))
1728 TREE_OVERFLOW (t) = 1;
1729 return t;
1732 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1733 to a fixed-point type. */
1735 static tree
1736 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1738 FIXED_VALUE_TYPE value;
1739 tree t;
1740 bool overflow_p;
1742 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
1743 TREE_INT_CST (arg1),
1744 TYPE_UNSIGNED (TREE_TYPE (arg1)),
1745 TYPE_SATURATING (type));
1746 t = build_fixed (type, value);
1748 /* Propagate overflow flags. */
1749 if (overflow_p | TREE_OVERFLOW (arg1))
1750 TREE_OVERFLOW (t) = 1;
1751 return t;
1754 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1755 to a fixed-point type. */
1757 static tree
1758 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1760 FIXED_VALUE_TYPE value;
1761 tree t;
1762 bool overflow_p;
1764 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1765 &TREE_REAL_CST (arg1),
1766 TYPE_SATURATING (type));
1767 t = build_fixed (type, value);
1769 /* Propagate overflow flags. */
1770 if (overflow_p | TREE_OVERFLOW (arg1))
1771 TREE_OVERFLOW (t) = 1;
1772 return t;
1775 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1776 type TYPE. If no simplification can be done return NULL_TREE. */
1778 static tree
1779 fold_convert_const (enum tree_code code, tree type, tree arg1)
1781 if (TREE_TYPE (arg1) == type)
1782 return arg1;
1784 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1785 || TREE_CODE (type) == OFFSET_TYPE)
1787 if (TREE_CODE (arg1) == INTEGER_CST)
1788 return fold_convert_const_int_from_int (type, arg1);
1789 else if (TREE_CODE (arg1) == REAL_CST)
1790 return fold_convert_const_int_from_real (code, type, arg1);
1791 else if (TREE_CODE (arg1) == FIXED_CST)
1792 return fold_convert_const_int_from_fixed (type, arg1);
1794 else if (TREE_CODE (type) == REAL_TYPE)
1796 if (TREE_CODE (arg1) == INTEGER_CST)
1797 return build_real_from_int_cst (type, arg1);
1798 else if (TREE_CODE (arg1) == REAL_CST)
1799 return fold_convert_const_real_from_real (type, arg1);
1800 else if (TREE_CODE (arg1) == FIXED_CST)
1801 return fold_convert_const_real_from_fixed (type, arg1);
1803 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1805 if (TREE_CODE (arg1) == FIXED_CST)
1806 return fold_convert_const_fixed_from_fixed (type, arg1);
1807 else if (TREE_CODE (arg1) == INTEGER_CST)
1808 return fold_convert_const_fixed_from_int (type, arg1);
1809 else if (TREE_CODE (arg1) == REAL_CST)
1810 return fold_convert_const_fixed_from_real (type, arg1);
1812 return NULL_TREE;
1815 /* Construct a vector of zero elements of vector type TYPE. */
1817 static tree
1818 build_zero_vector (tree type)
1820 tree elem, list;
1821 int i, units;
1823 elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1824 units = TYPE_VECTOR_SUBPARTS (type);
1826 list = NULL_TREE;
1827 for (i = 0; i < units; i++)
1828 list = tree_cons (NULL_TREE, elem, list);
1829 return build_vector (type, list);
1832 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
1834 bool
1835 fold_convertible_p (const_tree type, const_tree arg)
1837 tree orig = TREE_TYPE (arg);
1839 if (type == orig)
1840 return true;
1842 if (TREE_CODE (arg) == ERROR_MARK
1843 || TREE_CODE (type) == ERROR_MARK
1844 || TREE_CODE (orig) == ERROR_MARK)
1845 return false;
1847 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1848 return true;
1850 switch (TREE_CODE (type))
1852 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1853 case POINTER_TYPE: case REFERENCE_TYPE:
1854 case OFFSET_TYPE:
1855 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1856 || TREE_CODE (orig) == OFFSET_TYPE)
1857 return true;
1858 return (TREE_CODE (orig) == VECTOR_TYPE
1859 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1861 case REAL_TYPE:
1862 case FIXED_POINT_TYPE:
1863 case COMPLEX_TYPE:
1864 case VECTOR_TYPE:
1865 case VOID_TYPE:
1866 return TREE_CODE (type) == TREE_CODE (orig);
1868 default:
1869 return false;
1873 /* Convert expression ARG to type TYPE. Used by the middle-end for
1874 simple conversions in preference to calling the front-end's convert. */
1876 tree
1877 fold_convert_loc (location_t loc, tree type, tree arg)
1879 tree orig = TREE_TYPE (arg);
1880 tree tem;
1882 if (type == orig)
1883 return arg;
1885 if (TREE_CODE (arg) == ERROR_MARK
1886 || TREE_CODE (type) == ERROR_MARK
1887 || TREE_CODE (orig) == ERROR_MARK)
1888 return error_mark_node;
1890 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1891 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1893 switch (TREE_CODE (type))
1895 case POINTER_TYPE:
1896 case REFERENCE_TYPE:
1897 /* Handle conversions between pointers to different address spaces. */
1898 if (POINTER_TYPE_P (orig)
1899 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1900 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1901 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1902 /* fall through */
1904 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1905 case OFFSET_TYPE:
1906 if (TREE_CODE (arg) == INTEGER_CST)
1908 tem = fold_convert_const (NOP_EXPR, type, arg);
1909 if (tem != NULL_TREE)
1910 return tem;
1912 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1913 || TREE_CODE (orig) == OFFSET_TYPE)
1914 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1915 if (TREE_CODE (orig) == COMPLEX_TYPE)
1916 return fold_convert_loc (loc, type,
1917 fold_build1_loc (loc, REALPART_EXPR,
1918 TREE_TYPE (orig), arg));
1919 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1920 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1921 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1923 case REAL_TYPE:
1924 if (TREE_CODE (arg) == INTEGER_CST)
1926 tem = fold_convert_const (FLOAT_EXPR, type, arg);
1927 if (tem != NULL_TREE)
1928 return tem;
1930 else if (TREE_CODE (arg) == REAL_CST)
1932 tem = fold_convert_const (NOP_EXPR, type, arg);
1933 if (tem != NULL_TREE)
1934 return tem;
1936 else if (TREE_CODE (arg) == FIXED_CST)
1938 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1939 if (tem != NULL_TREE)
1940 return tem;
1943 switch (TREE_CODE (orig))
1945 case INTEGER_TYPE:
1946 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1947 case POINTER_TYPE: case REFERENCE_TYPE:
1948 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1950 case REAL_TYPE:
1951 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1953 case FIXED_POINT_TYPE:
1954 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1956 case COMPLEX_TYPE:
1957 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1958 return fold_convert_loc (loc, type, tem);
1960 default:
1961 gcc_unreachable ();
1964 case FIXED_POINT_TYPE:
1965 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1966 || TREE_CODE (arg) == REAL_CST)
1968 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1969 if (tem != NULL_TREE)
1970 goto fold_convert_exit;
1973 switch (TREE_CODE (orig))
1975 case FIXED_POINT_TYPE:
1976 case INTEGER_TYPE:
1977 case ENUMERAL_TYPE:
1978 case BOOLEAN_TYPE:
1979 case REAL_TYPE:
1980 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1982 case COMPLEX_TYPE:
1983 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1984 return fold_convert_loc (loc, type, tem);
1986 default:
1987 gcc_unreachable ();
1990 case COMPLEX_TYPE:
1991 switch (TREE_CODE (orig))
1993 case INTEGER_TYPE:
1994 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1995 case POINTER_TYPE: case REFERENCE_TYPE:
1996 case REAL_TYPE:
1997 case FIXED_POINT_TYPE:
1998 return fold_build2_loc (loc, COMPLEX_EXPR, type,
1999 fold_convert_loc (loc, TREE_TYPE (type), arg),
2000 fold_convert_loc (loc, TREE_TYPE (type),
2001 integer_zero_node));
2002 case COMPLEX_TYPE:
2004 tree rpart, ipart;
2006 if (TREE_CODE (arg) == COMPLEX_EXPR)
2008 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2009 TREE_OPERAND (arg, 0));
2010 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2011 TREE_OPERAND (arg, 1));
2012 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2015 arg = save_expr (arg);
2016 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2017 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2018 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2019 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2020 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2023 default:
2024 gcc_unreachable ();
2027 case VECTOR_TYPE:
2028 if (integer_zerop (arg))
2029 return build_zero_vector (type);
2030 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2031 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2032 || TREE_CODE (orig) == VECTOR_TYPE);
2033 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2035 case VOID_TYPE:
2036 tem = fold_ignored_result (arg);
2037 if (TREE_CODE (tem) == MODIFY_EXPR)
2038 goto fold_convert_exit;
2039 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2041 default:
2042 gcc_unreachable ();
2044 fold_convert_exit:
2045 protected_set_expr_location (tem, loc);
2046 return tem;
2049 /* Return false if expr can be assumed not to be an lvalue, true
2050 otherwise. */
2052 static bool
2053 maybe_lvalue_p (const_tree x)
2055 /* We only need to wrap lvalue tree codes. */
2056 switch (TREE_CODE (x))
2058 case VAR_DECL:
2059 case PARM_DECL:
2060 case RESULT_DECL:
2061 case LABEL_DECL:
2062 case FUNCTION_DECL:
2063 case SSA_NAME:
2065 case COMPONENT_REF:
2066 case INDIRECT_REF:
2067 case ALIGN_INDIRECT_REF:
2068 case MISALIGNED_INDIRECT_REF:
2069 case ARRAY_REF:
2070 case ARRAY_RANGE_REF:
2071 case BIT_FIELD_REF:
2072 case OBJ_TYPE_REF:
2074 case REALPART_EXPR:
2075 case IMAGPART_EXPR:
2076 case PREINCREMENT_EXPR:
2077 case PREDECREMENT_EXPR:
2078 case SAVE_EXPR:
2079 case TRY_CATCH_EXPR:
2080 case WITH_CLEANUP_EXPR:
2081 case COMPOUND_EXPR:
2082 case MODIFY_EXPR:
2083 case TARGET_EXPR:
2084 case COND_EXPR:
2085 case BIND_EXPR:
2086 break;
2088 default:
2089 /* Assume the worst for front-end tree codes. */
2090 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2091 break;
2092 return false;
2095 return true;
2098 /* Return an expr equal to X but certainly not valid as an lvalue. */
2100 tree
2101 non_lvalue_loc (location_t loc, tree x)
2103 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2104 us. */
2105 if (in_gimple_form)
2106 return x;
2108 if (! maybe_lvalue_p (x))
2109 return x;
2110 x = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2111 SET_EXPR_LOCATION (x, loc);
2112 return x;
2115 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2116 Zero means allow extended lvalues. */
2118 int pedantic_lvalues;
2120 /* When pedantic, return an expr equal to X but certainly not valid as a
2121 pedantic lvalue. Otherwise, return X. */
2123 static tree
2124 pedantic_non_lvalue_loc (location_t loc, tree x)
2126 if (pedantic_lvalues)
2127 return non_lvalue_loc (loc, x);
2128 protected_set_expr_location (x, loc);
2129 return x;
2132 /* Given a tree comparison code, return the code that is the logical inverse
2133 of the given code. It is not safe to do this for floating-point
2134 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2135 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2137 enum tree_code
2138 invert_tree_comparison (enum tree_code code, bool honor_nans)
2140 if (honor_nans && flag_trapping_math)
2141 return ERROR_MARK;
2143 switch (code)
2145 case EQ_EXPR:
2146 return NE_EXPR;
2147 case NE_EXPR:
2148 return EQ_EXPR;
2149 case GT_EXPR:
2150 return honor_nans ? UNLE_EXPR : LE_EXPR;
2151 case GE_EXPR:
2152 return honor_nans ? UNLT_EXPR : LT_EXPR;
2153 case LT_EXPR:
2154 return honor_nans ? UNGE_EXPR : GE_EXPR;
2155 case LE_EXPR:
2156 return honor_nans ? UNGT_EXPR : GT_EXPR;
2157 case LTGT_EXPR:
2158 return UNEQ_EXPR;
2159 case UNEQ_EXPR:
2160 return LTGT_EXPR;
2161 case UNGT_EXPR:
2162 return LE_EXPR;
2163 case UNGE_EXPR:
2164 return LT_EXPR;
2165 case UNLT_EXPR:
2166 return GE_EXPR;
2167 case UNLE_EXPR:
2168 return GT_EXPR;
2169 case ORDERED_EXPR:
2170 return UNORDERED_EXPR;
2171 case UNORDERED_EXPR:
2172 return ORDERED_EXPR;
2173 default:
2174 gcc_unreachable ();
2178 /* Similar, but return the comparison that results if the operands are
2179 swapped. This is safe for floating-point. */
2181 enum tree_code
2182 swap_tree_comparison (enum tree_code code)
2184 switch (code)
2186 case EQ_EXPR:
2187 case NE_EXPR:
2188 case ORDERED_EXPR:
2189 case UNORDERED_EXPR:
2190 case LTGT_EXPR:
2191 case UNEQ_EXPR:
2192 return code;
2193 case GT_EXPR:
2194 return LT_EXPR;
2195 case GE_EXPR:
2196 return LE_EXPR;
2197 case LT_EXPR:
2198 return GT_EXPR;
2199 case LE_EXPR:
2200 return GE_EXPR;
2201 case UNGT_EXPR:
2202 return UNLT_EXPR;
2203 case UNGE_EXPR:
2204 return UNLE_EXPR;
2205 case UNLT_EXPR:
2206 return UNGT_EXPR;
2207 case UNLE_EXPR:
2208 return UNGE_EXPR;
2209 default:
2210 gcc_unreachable ();
2215 /* Convert a comparison tree code from an enum tree_code representation
2216 into a compcode bit-based encoding. This function is the inverse of
2217 compcode_to_comparison. */
2219 static enum comparison_code
2220 comparison_to_compcode (enum tree_code code)
2222 switch (code)
2224 case LT_EXPR:
2225 return COMPCODE_LT;
2226 case EQ_EXPR:
2227 return COMPCODE_EQ;
2228 case LE_EXPR:
2229 return COMPCODE_LE;
2230 case GT_EXPR:
2231 return COMPCODE_GT;
2232 case NE_EXPR:
2233 return COMPCODE_NE;
2234 case GE_EXPR:
2235 return COMPCODE_GE;
2236 case ORDERED_EXPR:
2237 return COMPCODE_ORD;
2238 case UNORDERED_EXPR:
2239 return COMPCODE_UNORD;
2240 case UNLT_EXPR:
2241 return COMPCODE_UNLT;
2242 case UNEQ_EXPR:
2243 return COMPCODE_UNEQ;
2244 case UNLE_EXPR:
2245 return COMPCODE_UNLE;
2246 case UNGT_EXPR:
2247 return COMPCODE_UNGT;
2248 case LTGT_EXPR:
2249 return COMPCODE_LTGT;
2250 case UNGE_EXPR:
2251 return COMPCODE_UNGE;
2252 default:
2253 gcc_unreachable ();
2257 /* Convert a compcode bit-based encoding of a comparison operator back
2258 to GCC's enum tree_code representation. This function is the
2259 inverse of comparison_to_compcode. */
2261 static enum tree_code
2262 compcode_to_comparison (enum comparison_code code)
2264 switch (code)
2266 case COMPCODE_LT:
2267 return LT_EXPR;
2268 case COMPCODE_EQ:
2269 return EQ_EXPR;
2270 case COMPCODE_LE:
2271 return LE_EXPR;
2272 case COMPCODE_GT:
2273 return GT_EXPR;
2274 case COMPCODE_NE:
2275 return NE_EXPR;
2276 case COMPCODE_GE:
2277 return GE_EXPR;
2278 case COMPCODE_ORD:
2279 return ORDERED_EXPR;
2280 case COMPCODE_UNORD:
2281 return UNORDERED_EXPR;
2282 case COMPCODE_UNLT:
2283 return UNLT_EXPR;
2284 case COMPCODE_UNEQ:
2285 return UNEQ_EXPR;
2286 case COMPCODE_UNLE:
2287 return UNLE_EXPR;
2288 case COMPCODE_UNGT:
2289 return UNGT_EXPR;
2290 case COMPCODE_LTGT:
2291 return LTGT_EXPR;
2292 case COMPCODE_UNGE:
2293 return UNGE_EXPR;
2294 default:
2295 gcc_unreachable ();
2299 /* Return a tree for the comparison which is the combination of
2300 doing the AND or OR (depending on CODE) of the two operations LCODE
2301 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2302 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2303 if this makes the transformation invalid. */
2305 tree
2306 combine_comparisons (location_t loc,
2307 enum tree_code code, enum tree_code lcode,
2308 enum tree_code rcode, tree truth_type,
2309 tree ll_arg, tree lr_arg)
2311 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2312 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2313 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2314 int compcode;
2316 switch (code)
2318 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2319 compcode = lcompcode & rcompcode;
2320 break;
2322 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2323 compcode = lcompcode | rcompcode;
2324 break;
2326 default:
2327 return NULL_TREE;
2330 if (!honor_nans)
2332 /* Eliminate unordered comparisons, as well as LTGT and ORD
2333 which are not used unless the mode has NaNs. */
2334 compcode &= ~COMPCODE_UNORD;
2335 if (compcode == COMPCODE_LTGT)
2336 compcode = COMPCODE_NE;
2337 else if (compcode == COMPCODE_ORD)
2338 compcode = COMPCODE_TRUE;
2340 else if (flag_trapping_math)
2342 /* Check that the original operation and the optimized ones will trap
2343 under the same condition. */
2344 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2345 && (lcompcode != COMPCODE_EQ)
2346 && (lcompcode != COMPCODE_ORD);
2347 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2348 && (rcompcode != COMPCODE_EQ)
2349 && (rcompcode != COMPCODE_ORD);
2350 bool trap = (compcode & COMPCODE_UNORD) == 0
2351 && (compcode != COMPCODE_EQ)
2352 && (compcode != COMPCODE_ORD);
2354 /* In a short-circuited boolean expression the LHS might be
2355 such that the RHS, if evaluated, will never trap. For
2356 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2357 if neither x nor y is NaN. (This is a mixed blessing: for
2358 example, the expression above will never trap, hence
2359 optimizing it to x < y would be invalid). */
2360 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2361 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2362 rtrap = false;
2364 /* If the comparison was short-circuited, and only the RHS
2365 trapped, we may now generate a spurious trap. */
2366 if (rtrap && !ltrap
2367 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2368 return NULL_TREE;
2370 /* If we changed the conditions that cause a trap, we lose. */
2371 if ((ltrap || rtrap) != trap)
2372 return NULL_TREE;
2375 if (compcode == COMPCODE_TRUE)
2376 return constant_boolean_node (true, truth_type);
2377 else if (compcode == COMPCODE_FALSE)
2378 return constant_boolean_node (false, truth_type);
2379 else
2381 enum tree_code tcode;
2383 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2384 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2388 /* Return nonzero if two operands (typically of the same tree node)
2389 are necessarily equal. If either argument has side-effects this
2390 function returns zero. FLAGS modifies behavior as follows:
2392 If OEP_ONLY_CONST is set, only return nonzero for constants.
2393 This function tests whether the operands are indistinguishable;
2394 it does not test whether they are equal using C's == operation.
2395 The distinction is important for IEEE floating point, because
2396 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2397 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2399 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2400 even though it may hold multiple values during a function.
2401 This is because a GCC tree node guarantees that nothing else is
2402 executed between the evaluation of its "operands" (which may often
2403 be evaluated in arbitrary order). Hence if the operands themselves
2404 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2405 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2406 unset means assuming isochronic (or instantaneous) tree equivalence.
2407 Unless comparing arbitrary expression trees, such as from different
2408 statements, this flag can usually be left unset.
2410 If OEP_PURE_SAME is set, then pure functions with identical arguments
2411 are considered the same. It is used when the caller has other ways
2412 to ensure that global memory is unchanged in between. */
2415 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2417 /* If either is ERROR_MARK, they aren't equal. */
2418 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2419 || TREE_TYPE (arg0) == error_mark_node
2420 || TREE_TYPE (arg1) == error_mark_node)
2421 return 0;
2423 /* Similar, if either does not have a type (like a released SSA name),
2424 they aren't equal. */
2425 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2426 return 0;
2428 /* Check equality of integer constants before bailing out due to
2429 precision differences. */
2430 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2431 return tree_int_cst_equal (arg0, arg1);
2433 /* If both types don't have the same signedness, then we can't consider
2434 them equal. We must check this before the STRIP_NOPS calls
2435 because they may change the signedness of the arguments. As pointers
2436 strictly don't have a signedness, require either two pointers or
2437 two non-pointers as well. */
2438 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2439 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2440 return 0;
2442 /* We cannot consider pointers to different address space equal. */
2443 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2444 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2445 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2446 return 0;
2448 /* If both types don't have the same precision, then it is not safe
2449 to strip NOPs. */
2450 if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2451 return 0;
2453 STRIP_NOPS (arg0);
2454 STRIP_NOPS (arg1);
2456 /* In case both args are comparisons but with different comparison
2457 code, try to swap the comparison operands of one arg to produce
2458 a match and compare that variant. */
2459 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2460 && COMPARISON_CLASS_P (arg0)
2461 && COMPARISON_CLASS_P (arg1))
2463 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2465 if (TREE_CODE (arg0) == swap_code)
2466 return operand_equal_p (TREE_OPERAND (arg0, 0),
2467 TREE_OPERAND (arg1, 1), flags)
2468 && operand_equal_p (TREE_OPERAND (arg0, 1),
2469 TREE_OPERAND (arg1, 0), flags);
2472 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2473 /* This is needed for conversions and for COMPONENT_REF.
2474 Might as well play it safe and always test this. */
2475 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2476 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2477 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2478 return 0;
2480 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2481 We don't care about side effects in that case because the SAVE_EXPR
2482 takes care of that for us. In all other cases, two expressions are
2483 equal if they have no side effects. If we have two identical
2484 expressions with side effects that should be treated the same due
2485 to the only side effects being identical SAVE_EXPR's, that will
2486 be detected in the recursive calls below. */
2487 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2488 && (TREE_CODE (arg0) == SAVE_EXPR
2489 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2490 return 1;
2492 /* Next handle constant cases, those for which we can return 1 even
2493 if ONLY_CONST is set. */
2494 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2495 switch (TREE_CODE (arg0))
2497 case INTEGER_CST:
2498 return tree_int_cst_equal (arg0, arg1);
2500 case FIXED_CST:
2501 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2502 TREE_FIXED_CST (arg1));
2504 case REAL_CST:
2505 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2506 TREE_REAL_CST (arg1)))
2507 return 1;
2510 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2512 /* If we do not distinguish between signed and unsigned zero,
2513 consider them equal. */
2514 if (real_zerop (arg0) && real_zerop (arg1))
2515 return 1;
2517 return 0;
2519 case VECTOR_CST:
2521 tree v1, v2;
2523 v1 = TREE_VECTOR_CST_ELTS (arg0);
2524 v2 = TREE_VECTOR_CST_ELTS (arg1);
2525 while (v1 && v2)
2527 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2528 flags))
2529 return 0;
2530 v1 = TREE_CHAIN (v1);
2531 v2 = TREE_CHAIN (v2);
2534 return v1 == v2;
2537 case COMPLEX_CST:
2538 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2539 flags)
2540 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2541 flags));
2543 case STRING_CST:
2544 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2545 && ! memcmp (TREE_STRING_POINTER (arg0),
2546 TREE_STRING_POINTER (arg1),
2547 TREE_STRING_LENGTH (arg0)));
2549 case ADDR_EXPR:
2550 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2552 default:
2553 break;
2556 if (flags & OEP_ONLY_CONST)
2557 return 0;
2559 /* Define macros to test an operand from arg0 and arg1 for equality and a
2560 variant that allows null and views null as being different from any
2561 non-null value. In the latter case, if either is null, the both
2562 must be; otherwise, do the normal comparison. */
2563 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2564 TREE_OPERAND (arg1, N), flags)
2566 #define OP_SAME_WITH_NULL(N) \
2567 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2568 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2570 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2572 case tcc_unary:
2573 /* Two conversions are equal only if signedness and modes match. */
2574 switch (TREE_CODE (arg0))
2576 CASE_CONVERT:
2577 case FIX_TRUNC_EXPR:
2578 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2579 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2580 return 0;
2581 break;
2582 default:
2583 break;
2586 return OP_SAME (0);
2589 case tcc_comparison:
2590 case tcc_binary:
2591 if (OP_SAME (0) && OP_SAME (1))
2592 return 1;
2594 /* For commutative ops, allow the other order. */
2595 return (commutative_tree_code (TREE_CODE (arg0))
2596 && operand_equal_p (TREE_OPERAND (arg0, 0),
2597 TREE_OPERAND (arg1, 1), flags)
2598 && operand_equal_p (TREE_OPERAND (arg0, 1),
2599 TREE_OPERAND (arg1, 0), flags));
2601 case tcc_reference:
2602 /* If either of the pointer (or reference) expressions we are
2603 dereferencing contain a side effect, these cannot be equal. */
2604 if (TREE_SIDE_EFFECTS (arg0)
2605 || TREE_SIDE_EFFECTS (arg1))
2606 return 0;
2608 switch (TREE_CODE (arg0))
2610 case INDIRECT_REF:
2611 case ALIGN_INDIRECT_REF:
2612 case MISALIGNED_INDIRECT_REF:
2613 case REALPART_EXPR:
2614 case IMAGPART_EXPR:
2615 return OP_SAME (0);
2617 case ARRAY_REF:
2618 case ARRAY_RANGE_REF:
2619 /* Operands 2 and 3 may be null.
2620 Compare the array index by value if it is constant first as we
2621 may have different types but same value here. */
2622 return (OP_SAME (0)
2623 && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2624 TREE_OPERAND (arg1, 1))
2625 || OP_SAME (1))
2626 && OP_SAME_WITH_NULL (2)
2627 && OP_SAME_WITH_NULL (3));
2629 case COMPONENT_REF:
2630 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2631 may be NULL when we're called to compare MEM_EXPRs. */
2632 return OP_SAME_WITH_NULL (0)
2633 && OP_SAME (1)
2634 && OP_SAME_WITH_NULL (2);
2636 case BIT_FIELD_REF:
2637 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2639 default:
2640 return 0;
2643 case tcc_expression:
2644 switch (TREE_CODE (arg0))
2646 case ADDR_EXPR:
2647 case TRUTH_NOT_EXPR:
2648 return OP_SAME (0);
2650 case TRUTH_ANDIF_EXPR:
2651 case TRUTH_ORIF_EXPR:
2652 return OP_SAME (0) && OP_SAME (1);
2654 case TRUTH_AND_EXPR:
2655 case TRUTH_OR_EXPR:
2656 case TRUTH_XOR_EXPR:
2657 if (OP_SAME (0) && OP_SAME (1))
2658 return 1;
2660 /* Otherwise take into account this is a commutative operation. */
2661 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2662 TREE_OPERAND (arg1, 1), flags)
2663 && operand_equal_p (TREE_OPERAND (arg0, 1),
2664 TREE_OPERAND (arg1, 0), flags));
2666 case COND_EXPR:
2667 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2669 default:
2670 return 0;
2673 case tcc_vl_exp:
2674 switch (TREE_CODE (arg0))
2676 case CALL_EXPR:
2677 /* If the CALL_EXPRs call different functions, then they
2678 clearly can not be equal. */
2679 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2680 flags))
2681 return 0;
2684 unsigned int cef = call_expr_flags (arg0);
2685 if (flags & OEP_PURE_SAME)
2686 cef &= ECF_CONST | ECF_PURE;
2687 else
2688 cef &= ECF_CONST;
2689 if (!cef)
2690 return 0;
2693 /* Now see if all the arguments are the same. */
2695 const_call_expr_arg_iterator iter0, iter1;
2696 const_tree a0, a1;
2697 for (a0 = first_const_call_expr_arg (arg0, &iter0),
2698 a1 = first_const_call_expr_arg (arg1, &iter1);
2699 a0 && a1;
2700 a0 = next_const_call_expr_arg (&iter0),
2701 a1 = next_const_call_expr_arg (&iter1))
2702 if (! operand_equal_p (a0, a1, flags))
2703 return 0;
2705 /* If we get here and both argument lists are exhausted
2706 then the CALL_EXPRs are equal. */
2707 return ! (a0 || a1);
2709 default:
2710 return 0;
2713 case tcc_declaration:
2714 /* Consider __builtin_sqrt equal to sqrt. */
2715 return (TREE_CODE (arg0) == FUNCTION_DECL
2716 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2717 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2718 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2720 default:
2721 return 0;
2724 #undef OP_SAME
2725 #undef OP_SAME_WITH_NULL
2728 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2729 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2731 When in doubt, return 0. */
2733 static int
2734 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2736 int unsignedp1, unsignedpo;
2737 tree primarg0, primarg1, primother;
2738 unsigned int correct_width;
2740 if (operand_equal_p (arg0, arg1, 0))
2741 return 1;
2743 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2744 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2745 return 0;
2747 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2748 and see if the inner values are the same. This removes any
2749 signedness comparison, which doesn't matter here. */
2750 primarg0 = arg0, primarg1 = arg1;
2751 STRIP_NOPS (primarg0);
2752 STRIP_NOPS (primarg1);
2753 if (operand_equal_p (primarg0, primarg1, 0))
2754 return 1;
2756 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2757 actual comparison operand, ARG0.
2759 First throw away any conversions to wider types
2760 already present in the operands. */
2762 primarg1 = get_narrower (arg1, &unsignedp1);
2763 primother = get_narrower (other, &unsignedpo);
2765 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2766 if (unsignedp1 == unsignedpo
2767 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2768 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2770 tree type = TREE_TYPE (arg0);
2772 /* Make sure shorter operand is extended the right way
2773 to match the longer operand. */
2774 primarg1 = fold_convert (signed_or_unsigned_type_for
2775 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2777 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2778 return 1;
2781 return 0;
2784 /* See if ARG is an expression that is either a comparison or is performing
2785 arithmetic on comparisons. The comparisons must only be comparing
2786 two different values, which will be stored in *CVAL1 and *CVAL2; if
2787 they are nonzero it means that some operands have already been found.
2788 No variables may be used anywhere else in the expression except in the
2789 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2790 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2792 If this is true, return 1. Otherwise, return zero. */
2794 static int
2795 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2797 enum tree_code code = TREE_CODE (arg);
2798 enum tree_code_class tclass = TREE_CODE_CLASS (code);
2800 /* We can handle some of the tcc_expression cases here. */
2801 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2802 tclass = tcc_unary;
2803 else if (tclass == tcc_expression
2804 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2805 || code == COMPOUND_EXPR))
2806 tclass = tcc_binary;
2808 else if (tclass == tcc_expression && code == SAVE_EXPR
2809 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2811 /* If we've already found a CVAL1 or CVAL2, this expression is
2812 two complex to handle. */
2813 if (*cval1 || *cval2)
2814 return 0;
2816 tclass = tcc_unary;
2817 *save_p = 1;
2820 switch (tclass)
2822 case tcc_unary:
2823 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2825 case tcc_binary:
2826 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2827 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2828 cval1, cval2, save_p));
2830 case tcc_constant:
2831 return 1;
2833 case tcc_expression:
2834 if (code == COND_EXPR)
2835 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2836 cval1, cval2, save_p)
2837 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2838 cval1, cval2, save_p)
2839 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2840 cval1, cval2, save_p));
2841 return 0;
2843 case tcc_comparison:
2844 /* First see if we can handle the first operand, then the second. For
2845 the second operand, we know *CVAL1 can't be zero. It must be that
2846 one side of the comparison is each of the values; test for the
2847 case where this isn't true by failing if the two operands
2848 are the same. */
2850 if (operand_equal_p (TREE_OPERAND (arg, 0),
2851 TREE_OPERAND (arg, 1), 0))
2852 return 0;
2854 if (*cval1 == 0)
2855 *cval1 = TREE_OPERAND (arg, 0);
2856 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2858 else if (*cval2 == 0)
2859 *cval2 = TREE_OPERAND (arg, 0);
2860 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2862 else
2863 return 0;
2865 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2867 else if (*cval2 == 0)
2868 *cval2 = TREE_OPERAND (arg, 1);
2869 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2871 else
2872 return 0;
2874 return 1;
2876 default:
2877 return 0;
2881 /* ARG is a tree that is known to contain just arithmetic operations and
2882 comparisons. Evaluate the operations in the tree substituting NEW0 for
2883 any occurrence of OLD0 as an operand of a comparison and likewise for
2884 NEW1 and OLD1. */
2886 static tree
2887 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2888 tree old1, tree new1)
2890 tree type = TREE_TYPE (arg);
2891 enum tree_code code = TREE_CODE (arg);
2892 enum tree_code_class tclass = TREE_CODE_CLASS (code);
2894 /* We can handle some of the tcc_expression cases here. */
2895 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2896 tclass = tcc_unary;
2897 else if (tclass == tcc_expression
2898 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2899 tclass = tcc_binary;
2901 switch (tclass)
2903 case tcc_unary:
2904 return fold_build1_loc (loc, code, type,
2905 eval_subst (loc, TREE_OPERAND (arg, 0),
2906 old0, new0, old1, new1));
2908 case tcc_binary:
2909 return fold_build2_loc (loc, code, type,
2910 eval_subst (loc, TREE_OPERAND (arg, 0),
2911 old0, new0, old1, new1),
2912 eval_subst (loc, TREE_OPERAND (arg, 1),
2913 old0, new0, old1, new1));
2915 case tcc_expression:
2916 switch (code)
2918 case SAVE_EXPR:
2919 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
2920 old1, new1);
2922 case COMPOUND_EXPR:
2923 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
2924 old1, new1);
2926 case COND_EXPR:
2927 return fold_build3_loc (loc, code, type,
2928 eval_subst (loc, TREE_OPERAND (arg, 0),
2929 old0, new0, old1, new1),
2930 eval_subst (loc, TREE_OPERAND (arg, 1),
2931 old0, new0, old1, new1),
2932 eval_subst (loc, TREE_OPERAND (arg, 2),
2933 old0, new0, old1, new1));
2934 default:
2935 break;
2937 /* Fall through - ??? */
2939 case tcc_comparison:
2941 tree arg0 = TREE_OPERAND (arg, 0);
2942 tree arg1 = TREE_OPERAND (arg, 1);
2944 /* We need to check both for exact equality and tree equality. The
2945 former will be true if the operand has a side-effect. In that
2946 case, we know the operand occurred exactly once. */
2948 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2949 arg0 = new0;
2950 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2951 arg0 = new1;
2953 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2954 arg1 = new0;
2955 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2956 arg1 = new1;
2958 return fold_build2_loc (loc, code, type, arg0, arg1);
2961 default:
2962 return arg;
2966 /* Return a tree for the case when the result of an expression is RESULT
2967 converted to TYPE and OMITTED was previously an operand of the expression
2968 but is now not needed (e.g., we folded OMITTED * 0).
2970 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2971 the conversion of RESULT to TYPE. */
2973 tree
2974 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
2976 tree t = fold_convert_loc (loc, type, result);
2978 /* If the resulting operand is an empty statement, just return the omitted
2979 statement casted to void. */
2980 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2982 t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
2983 goto omit_one_operand_exit;
2986 if (TREE_SIDE_EFFECTS (omitted))
2988 t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2989 goto omit_one_operand_exit;
2992 return non_lvalue_loc (loc, t);
2994 omit_one_operand_exit:
2995 protected_set_expr_location (t, loc);
2996 return t;
2999 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3001 static tree
3002 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
3003 tree omitted)
3005 tree t = fold_convert_loc (loc, type, result);
3007 /* If the resulting operand is an empty statement, just return the omitted
3008 statement casted to void. */
3009 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3011 t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3012 goto pedantic_omit_one_operand_exit;
3015 if (TREE_SIDE_EFFECTS (omitted))
3017 t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3018 goto pedantic_omit_one_operand_exit;
3021 return pedantic_non_lvalue_loc (loc, t);
3023 pedantic_omit_one_operand_exit:
3024 protected_set_expr_location (t, loc);
3025 return t;
3028 /* Return a tree for the case when the result of an expression is RESULT
3029 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3030 of the expression but are now not needed.
3032 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3033 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3034 evaluated before OMITTED2. Otherwise, if neither has side effects,
3035 just do the conversion of RESULT to TYPE. */
3037 tree
3038 omit_two_operands_loc (location_t loc, tree type, tree result,
3039 tree omitted1, tree omitted2)
3041 tree t = fold_convert_loc (loc, type, result);
3043 if (TREE_SIDE_EFFECTS (omitted2))
3045 t = build2 (COMPOUND_EXPR, type, omitted2, t);
3046 SET_EXPR_LOCATION (t, loc);
3048 if (TREE_SIDE_EFFECTS (omitted1))
3050 t = build2 (COMPOUND_EXPR, type, omitted1, t);
3051 SET_EXPR_LOCATION (t, loc);
3054 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3058 /* Return a simplified tree node for the truth-negation of ARG. This
3059 never alters ARG itself. We assume that ARG is an operation that
3060 returns a truth value (0 or 1).
3062 FIXME: one would think we would fold the result, but it causes
3063 problems with the dominator optimizer. */
3065 tree
3066 fold_truth_not_expr (location_t loc, tree arg)
3068 tree t, type = TREE_TYPE (arg);
3069 enum tree_code code = TREE_CODE (arg);
3070 location_t loc1, loc2;
3072 /* If this is a comparison, we can simply invert it, except for
3073 floating-point non-equality comparisons, in which case we just
3074 enclose a TRUTH_NOT_EXPR around what we have. */
3076 if (TREE_CODE_CLASS (code) == tcc_comparison)
3078 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3079 if (FLOAT_TYPE_P (op_type)
3080 && flag_trapping_math
3081 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3082 && code != NE_EXPR && code != EQ_EXPR)
3083 return NULL_TREE;
3085 code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3086 if (code == ERROR_MARK)
3087 return NULL_TREE;
3089 t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3090 SET_EXPR_LOCATION (t, loc);
3091 return t;
3094 switch (code)
3096 case INTEGER_CST:
3097 return constant_boolean_node (integer_zerop (arg), type);
3099 case TRUTH_AND_EXPR:
3100 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3101 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3102 if (loc1 == UNKNOWN_LOCATION)
3103 loc1 = loc;
3104 if (loc2 == UNKNOWN_LOCATION)
3105 loc2 = loc;
3106 t = build2 (TRUTH_OR_EXPR, type,
3107 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3108 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3109 break;
3111 case TRUTH_OR_EXPR:
3112 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3113 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3114 if (loc1 == UNKNOWN_LOCATION)
3115 loc1 = loc;
3116 if (loc2 == UNKNOWN_LOCATION)
3117 loc2 = loc;
3118 t = build2 (TRUTH_AND_EXPR, type,
3119 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3120 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3121 break;
3123 case TRUTH_XOR_EXPR:
3124 /* Here we can invert either operand. We invert the first operand
3125 unless the second operand is a TRUTH_NOT_EXPR in which case our
3126 result is the XOR of the first operand with the inside of the
3127 negation of the second operand. */
3129 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3130 t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3131 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3132 else
3133 t = build2 (TRUTH_XOR_EXPR, type,
3134 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3135 TREE_OPERAND (arg, 1));
3136 break;
3138 case TRUTH_ANDIF_EXPR:
3139 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3140 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3141 if (loc1 == UNKNOWN_LOCATION)
3142 loc1 = loc;
3143 if (loc2 == UNKNOWN_LOCATION)
3144 loc2 = loc;
3145 t = build2 (TRUTH_ORIF_EXPR, type,
3146 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3147 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3148 break;
3150 case TRUTH_ORIF_EXPR:
3151 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3152 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3153 if (loc1 == UNKNOWN_LOCATION)
3154 loc1 = loc;
3155 if (loc2 == UNKNOWN_LOCATION)
3156 loc2 = loc;
3157 t = build2 (TRUTH_ANDIF_EXPR, type,
3158 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3159 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3160 break;
3162 case TRUTH_NOT_EXPR:
3163 return TREE_OPERAND (arg, 0);
3165 case COND_EXPR:
3167 tree arg1 = TREE_OPERAND (arg, 1);
3168 tree arg2 = TREE_OPERAND (arg, 2);
3170 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3171 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 2));
3172 if (loc1 == UNKNOWN_LOCATION)
3173 loc1 = loc;
3174 if (loc2 == UNKNOWN_LOCATION)
3175 loc2 = loc;
3177 /* A COND_EXPR may have a throw as one operand, which
3178 then has void type. Just leave void operands
3179 as they are. */
3180 t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3181 VOID_TYPE_P (TREE_TYPE (arg1))
3182 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3183 VOID_TYPE_P (TREE_TYPE (arg2))
3184 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3185 break;
3188 case COMPOUND_EXPR:
3189 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3190 if (loc1 == UNKNOWN_LOCATION)
3191 loc1 = loc;
3192 t = build2 (COMPOUND_EXPR, type,
3193 TREE_OPERAND (arg, 0),
3194 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3195 break;
3197 case NON_LVALUE_EXPR:
3198 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3199 if (loc1 == UNKNOWN_LOCATION)
3200 loc1 = loc;
3201 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3203 CASE_CONVERT:
3204 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3206 t = build1 (TRUTH_NOT_EXPR, type, arg);
3207 break;
3210 /* ... fall through ... */
3212 case FLOAT_EXPR:
3213 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3214 if (loc1 == UNKNOWN_LOCATION)
3215 loc1 = loc;
3216 t = build1 (TREE_CODE (arg), type,
3217 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3218 break;
3220 case BIT_AND_EXPR:
3221 if (!integer_onep (TREE_OPERAND (arg, 1)))
3222 return NULL_TREE;
3223 t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0));
3224 break;
3226 case SAVE_EXPR:
3227 t = build1 (TRUTH_NOT_EXPR, type, arg);
3228 break;
3230 case CLEANUP_POINT_EXPR:
3231 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3232 if (loc1 == UNKNOWN_LOCATION)
3233 loc1 = loc;
3234 t = build1 (CLEANUP_POINT_EXPR, type,
3235 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3236 break;
3238 default:
3239 t = NULL_TREE;
3240 break;
3243 if (t)
3244 SET_EXPR_LOCATION (t, loc);
3246 return t;
3249 /* Return a simplified tree node for the truth-negation of ARG. This
3250 never alters ARG itself. We assume that ARG is an operation that
3251 returns a truth value (0 or 1).
3253 FIXME: one would think we would fold the result, but it causes
3254 problems with the dominator optimizer. */
3256 tree
3257 invert_truthvalue_loc (location_t loc, tree arg)
3259 tree tem;
3261 if (TREE_CODE (arg) == ERROR_MARK)
3262 return arg;
3264 tem = fold_truth_not_expr (loc, arg);
3265 if (!tem)
3267 tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3268 SET_EXPR_LOCATION (tem, loc);
3271 return tem;
3274 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3275 operands are another bit-wise operation with a common input. If so,
3276 distribute the bit operations to save an operation and possibly two if
3277 constants are involved. For example, convert
3278 (A | B) & (A | C) into A | (B & C)
3279 Further simplification will occur if B and C are constants.
3281 If this optimization cannot be done, 0 will be returned. */
3283 static tree
3284 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3285 tree arg0, tree arg1)
3287 tree common;
3288 tree left, right;
3290 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3291 || TREE_CODE (arg0) == code
3292 || (TREE_CODE (arg0) != BIT_AND_EXPR
3293 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3294 return 0;
3296 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3298 common = TREE_OPERAND (arg0, 0);
3299 left = TREE_OPERAND (arg0, 1);
3300 right = TREE_OPERAND (arg1, 1);
3302 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3304 common = TREE_OPERAND (arg0, 0);
3305 left = TREE_OPERAND (arg0, 1);
3306 right = TREE_OPERAND (arg1, 0);
3308 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3310 common = TREE_OPERAND (arg0, 1);
3311 left = TREE_OPERAND (arg0, 0);
3312 right = TREE_OPERAND (arg1, 1);
3314 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3316 common = TREE_OPERAND (arg0, 1);
3317 left = TREE_OPERAND (arg0, 0);
3318 right = TREE_OPERAND (arg1, 0);
3320 else
3321 return 0;
3323 common = fold_convert_loc (loc, type, common);
3324 left = fold_convert_loc (loc, type, left);
3325 right = fold_convert_loc (loc, type, right);
3326 return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3327 fold_build2_loc (loc, code, type, left, right));
3330 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3331 with code CODE. This optimization is unsafe. */
3332 static tree
3333 distribute_real_division (location_t loc, enum tree_code code, tree type,
3334 tree arg0, tree arg1)
3336 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3337 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3339 /* (A / C) +- (B / C) -> (A +- B) / C. */
3340 if (mul0 == mul1
3341 && operand_equal_p (TREE_OPERAND (arg0, 1),
3342 TREE_OPERAND (arg1, 1), 0))
3343 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3344 fold_build2_loc (loc, code, type,
3345 TREE_OPERAND (arg0, 0),
3346 TREE_OPERAND (arg1, 0)),
3347 TREE_OPERAND (arg0, 1));
3349 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3350 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3351 TREE_OPERAND (arg1, 0), 0)
3352 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3353 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3355 REAL_VALUE_TYPE r0, r1;
3356 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3357 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3358 if (!mul0)
3359 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3360 if (!mul1)
3361 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3362 real_arithmetic (&r0, code, &r0, &r1);
3363 return fold_build2_loc (loc, MULT_EXPR, type,
3364 TREE_OPERAND (arg0, 0),
3365 build_real (type, r0));
3368 return NULL_TREE;
3371 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3372 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3374 static tree
3375 make_bit_field_ref (location_t loc, tree inner, tree type,
3376 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3378 tree result, bftype;
3380 if (bitpos == 0)
3382 tree size = TYPE_SIZE (TREE_TYPE (inner));
3383 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3384 || POINTER_TYPE_P (TREE_TYPE (inner)))
3385 && host_integerp (size, 0)
3386 && tree_low_cst (size, 0) == bitsize)
3387 return fold_convert_loc (loc, type, inner);
3390 bftype = type;
3391 if (TYPE_PRECISION (bftype) != bitsize
3392 || TYPE_UNSIGNED (bftype) == !unsignedp)
3393 bftype = build_nonstandard_integer_type (bitsize, 0);
3395 result = build3 (BIT_FIELD_REF, bftype, inner,
3396 size_int (bitsize), bitsize_int (bitpos));
3397 SET_EXPR_LOCATION (result, loc);
3399 if (bftype != type)
3400 result = fold_convert_loc (loc, type, result);
3402 return result;
3405 /* Optimize a bit-field compare.
3407 There are two cases: First is a compare against a constant and the
3408 second is a comparison of two items where the fields are at the same
3409 bit position relative to the start of a chunk (byte, halfword, word)
3410 large enough to contain it. In these cases we can avoid the shift
3411 implicit in bitfield extractions.
3413 For constants, we emit a compare of the shifted constant with the
3414 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3415 compared. For two fields at the same position, we do the ANDs with the
3416 similar mask and compare the result of the ANDs.
3418 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3419 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3420 are the left and right operands of the comparison, respectively.
3422 If the optimization described above can be done, we return the resulting
3423 tree. Otherwise we return zero. */
3425 static tree
3426 optimize_bit_field_compare (location_t loc, enum tree_code code,
3427 tree compare_type, tree lhs, tree rhs)
3429 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3430 tree type = TREE_TYPE (lhs);
3431 tree signed_type, unsigned_type;
3432 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3433 enum machine_mode lmode, rmode, nmode;
3434 int lunsignedp, runsignedp;
3435 int lvolatilep = 0, rvolatilep = 0;
3436 tree linner, rinner = NULL_TREE;
3437 tree mask;
3438 tree offset;
3440 /* Get all the information about the extractions being done. If the bit size
3441 if the same as the size of the underlying object, we aren't doing an
3442 extraction at all and so can do nothing. We also don't want to
3443 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3444 then will no longer be able to replace it. */
3445 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3446 &lunsignedp, &lvolatilep, false);
3447 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3448 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3449 return 0;
3451 if (!const_p)
3453 /* If this is not a constant, we can only do something if bit positions,
3454 sizes, and signedness are the same. */
3455 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3456 &runsignedp, &rvolatilep, false);
3458 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3459 || lunsignedp != runsignedp || offset != 0
3460 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3461 return 0;
3464 /* See if we can find a mode to refer to this field. We should be able to,
3465 but fail if we can't. */
3466 nmode = get_best_mode (lbitsize, lbitpos,
3467 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3468 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3469 TYPE_ALIGN (TREE_TYPE (rinner))),
3470 word_mode, lvolatilep || rvolatilep);
3471 if (nmode == VOIDmode)
3472 return 0;
3474 /* Set signed and unsigned types of the precision of this mode for the
3475 shifts below. */
3476 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3477 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3479 /* Compute the bit position and size for the new reference and our offset
3480 within it. If the new reference is the same size as the original, we
3481 won't optimize anything, so return zero. */
3482 nbitsize = GET_MODE_BITSIZE (nmode);
3483 nbitpos = lbitpos & ~ (nbitsize - 1);
3484 lbitpos -= nbitpos;
3485 if (nbitsize == lbitsize)
3486 return 0;
3488 if (BYTES_BIG_ENDIAN)
3489 lbitpos = nbitsize - lbitsize - lbitpos;
3491 /* Make the mask to be used against the extracted field. */
3492 mask = build_int_cst_type (unsigned_type, -1);
3493 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3494 mask = const_binop (RSHIFT_EXPR, mask,
3495 size_int (nbitsize - lbitsize - lbitpos), 0);
3497 if (! const_p)
3498 /* If not comparing with constant, just rework the comparison
3499 and return. */
3500 return fold_build2_loc (loc, code, compare_type,
3501 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3502 make_bit_field_ref (loc, linner,
3503 unsigned_type,
3504 nbitsize, nbitpos,
3506 mask),
3507 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3508 make_bit_field_ref (loc, rinner,
3509 unsigned_type,
3510 nbitsize, nbitpos,
3512 mask));
3514 /* Otherwise, we are handling the constant case. See if the constant is too
3515 big for the field. Warn and return a tree of for 0 (false) if so. We do
3516 this not only for its own sake, but to avoid having to test for this
3517 error case below. If we didn't, we might generate wrong code.
3519 For unsigned fields, the constant shifted right by the field length should
3520 be all zero. For signed fields, the high-order bits should agree with
3521 the sign bit. */
3523 if (lunsignedp)
3525 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3526 fold_convert_loc (loc,
3527 unsigned_type, rhs),
3528 size_int (lbitsize), 0)))
3530 warning (0, "comparison is always %d due to width of bit-field",
3531 code == NE_EXPR);
3532 return constant_boolean_node (code == NE_EXPR, compare_type);
3535 else
3537 tree tem = const_binop (RSHIFT_EXPR,
3538 fold_convert_loc (loc, signed_type, rhs),
3539 size_int (lbitsize - 1), 0);
3540 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3542 warning (0, "comparison is always %d due to width of bit-field",
3543 code == NE_EXPR);
3544 return constant_boolean_node (code == NE_EXPR, compare_type);
3548 /* Single-bit compares should always be against zero. */
3549 if (lbitsize == 1 && ! integer_zerop (rhs))
3551 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3552 rhs = build_int_cst (type, 0);
3555 /* Make a new bitfield reference, shift the constant over the
3556 appropriate number of bits and mask it with the computed mask
3557 (in case this was a signed field). If we changed it, make a new one. */
3558 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3559 if (lvolatilep)
3561 TREE_SIDE_EFFECTS (lhs) = 1;
3562 TREE_THIS_VOLATILE (lhs) = 1;
3565 rhs = const_binop (BIT_AND_EXPR,
3566 const_binop (LSHIFT_EXPR,
3567 fold_convert_loc (loc, unsigned_type, rhs),
3568 size_int (lbitpos), 0),
3569 mask, 0);
3571 lhs = build2 (code, compare_type,
3572 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3573 rhs);
3574 SET_EXPR_LOCATION (lhs, loc);
3575 return lhs;
3578 /* Subroutine for fold_truthop: decode a field reference.
3580 If EXP is a comparison reference, we return the innermost reference.
3582 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3583 set to the starting bit number.
3585 If the innermost field can be completely contained in a mode-sized
3586 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3588 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3589 otherwise it is not changed.
3591 *PUNSIGNEDP is set to the signedness of the field.
3593 *PMASK is set to the mask used. This is either contained in a
3594 BIT_AND_EXPR or derived from the width of the field.
3596 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3598 Return 0 if this is not a component reference or is one that we can't
3599 do anything with. */
3601 static tree
3602 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3603 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3604 int *punsignedp, int *pvolatilep,
3605 tree *pmask, tree *pand_mask)
3607 tree outer_type = 0;
3608 tree and_mask = 0;
3609 tree mask, inner, offset;
3610 tree unsigned_type;
3611 unsigned int precision;
3613 /* All the optimizations using this function assume integer fields.
3614 There are problems with FP fields since the type_for_size call
3615 below can fail for, e.g., XFmode. */
3616 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3617 return 0;
3619 /* We are interested in the bare arrangement of bits, so strip everything
3620 that doesn't affect the machine mode. However, record the type of the
3621 outermost expression if it may matter below. */
3622 if (CONVERT_EXPR_P (exp)
3623 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3624 outer_type = TREE_TYPE (exp);
3625 STRIP_NOPS (exp);
3627 if (TREE_CODE (exp) == BIT_AND_EXPR)
3629 and_mask = TREE_OPERAND (exp, 1);
3630 exp = TREE_OPERAND (exp, 0);
3631 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3632 if (TREE_CODE (and_mask) != INTEGER_CST)
3633 return 0;
3636 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3637 punsignedp, pvolatilep, false);
3638 if ((inner == exp && and_mask == 0)
3639 || *pbitsize < 0 || offset != 0
3640 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3641 return 0;
3643 /* If the number of bits in the reference is the same as the bitsize of
3644 the outer type, then the outer type gives the signedness. Otherwise
3645 (in case of a small bitfield) the signedness is unchanged. */
3646 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3647 *punsignedp = TYPE_UNSIGNED (outer_type);
3649 /* Compute the mask to access the bitfield. */
3650 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3651 precision = TYPE_PRECISION (unsigned_type);
3653 mask = build_int_cst_type (unsigned_type, -1);
3655 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3656 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3658 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3659 if (and_mask != 0)
3660 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3661 fold_convert_loc (loc, unsigned_type, and_mask), mask);
3663 *pmask = mask;
3664 *pand_mask = and_mask;
3665 return inner;
3668 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3669 bit positions. */
3671 static int
3672 all_ones_mask_p (const_tree mask, int size)
3674 tree type = TREE_TYPE (mask);
3675 unsigned int precision = TYPE_PRECISION (type);
3676 tree tmask;
3678 tmask = build_int_cst_type (signed_type_for (type), -1);
3680 return
3681 tree_int_cst_equal (mask,
3682 const_binop (RSHIFT_EXPR,
3683 const_binop (LSHIFT_EXPR, tmask,
3684 size_int (precision - size),
3686 size_int (precision - size), 0));
3689 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3690 represents the sign bit of EXP's type. If EXP represents a sign
3691 or zero extension, also test VAL against the unextended type.
3692 The return value is the (sub)expression whose sign bit is VAL,
3693 or NULL_TREE otherwise. */
3695 static tree
3696 sign_bit_p (tree exp, const_tree val)
3698 unsigned HOST_WIDE_INT mask_lo, lo;
3699 HOST_WIDE_INT mask_hi, hi;
3700 int width;
3701 tree t;
3703 /* Tree EXP must have an integral type. */
3704 t = TREE_TYPE (exp);
3705 if (! INTEGRAL_TYPE_P (t))
3706 return NULL_TREE;
3708 /* Tree VAL must be an integer constant. */
3709 if (TREE_CODE (val) != INTEGER_CST
3710 || TREE_OVERFLOW (val))
3711 return NULL_TREE;
3713 width = TYPE_PRECISION (t);
3714 if (width > HOST_BITS_PER_WIDE_INT)
3716 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3717 lo = 0;
3719 mask_hi = ((unsigned HOST_WIDE_INT) -1
3720 >> (2 * HOST_BITS_PER_WIDE_INT - width));
3721 mask_lo = -1;
3723 else
3725 hi = 0;
3726 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3728 mask_hi = 0;
3729 mask_lo = ((unsigned HOST_WIDE_INT) -1
3730 >> (HOST_BITS_PER_WIDE_INT - width));
3733 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3734 treat VAL as if it were unsigned. */
3735 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3736 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3737 return exp;
3739 /* Handle extension from a narrower type. */
3740 if (TREE_CODE (exp) == NOP_EXPR
3741 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3742 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3744 return NULL_TREE;
3747 /* Subroutine for fold_truthop: determine if an operand is simple enough
3748 to be evaluated unconditionally. */
3750 static int
3751 simple_operand_p (const_tree exp)
3753 /* Strip any conversions that don't change the machine mode. */
3754 STRIP_NOPS (exp);
3756 return (CONSTANT_CLASS_P (exp)
3757 || TREE_CODE (exp) == SSA_NAME
3758 || (DECL_P (exp)
3759 && ! TREE_ADDRESSABLE (exp)
3760 && ! TREE_THIS_VOLATILE (exp)
3761 && ! DECL_NONLOCAL (exp)
3762 /* Don't regard global variables as simple. They may be
3763 allocated in ways unknown to the compiler (shared memory,
3764 #pragma weak, etc). */
3765 && ! TREE_PUBLIC (exp)
3766 && ! DECL_EXTERNAL (exp)
3767 /* Loading a static variable is unduly expensive, but global
3768 registers aren't expensive. */
3769 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3772 /* The following functions are subroutines to fold_range_test and allow it to
3773 try to change a logical combination of comparisons into a range test.
3775 For example, both
3776 X == 2 || X == 3 || X == 4 || X == 5
3778 X >= 2 && X <= 5
3779 are converted to
3780 (unsigned) (X - 2) <= 3
3782 We describe each set of comparisons as being either inside or outside
3783 a range, using a variable named like IN_P, and then describe the
3784 range with a lower and upper bound. If one of the bounds is omitted,
3785 it represents either the highest or lowest value of the type.
3787 In the comments below, we represent a range by two numbers in brackets
3788 preceded by a "+" to designate being inside that range, or a "-" to
3789 designate being outside that range, so the condition can be inverted by
3790 flipping the prefix. An omitted bound is represented by a "-". For
3791 example, "- [-, 10]" means being outside the range starting at the lowest
3792 possible value and ending at 10, in other words, being greater than 10.
3793 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3794 always false.
3796 We set up things so that the missing bounds are handled in a consistent
3797 manner so neither a missing bound nor "true" and "false" need to be
3798 handled using a special case. */
3800 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3801 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3802 and UPPER1_P are nonzero if the respective argument is an upper bound
3803 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3804 must be specified for a comparison. ARG1 will be converted to ARG0's
3805 type if both are specified. */
3807 static tree
3808 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3809 tree arg1, int upper1_p)
3811 tree tem;
3812 int result;
3813 int sgn0, sgn1;
3815 /* If neither arg represents infinity, do the normal operation.
3816 Else, if not a comparison, return infinity. Else handle the special
3817 comparison rules. Note that most of the cases below won't occur, but
3818 are handled for consistency. */
3820 if (arg0 != 0 && arg1 != 0)
3822 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3823 arg0, fold_convert (TREE_TYPE (arg0), arg1));
3824 STRIP_NOPS (tem);
3825 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3828 if (TREE_CODE_CLASS (code) != tcc_comparison)
3829 return 0;
3831 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3832 for neither. In real maths, we cannot assume open ended ranges are
3833 the same. But, this is computer arithmetic, where numbers are finite.
3834 We can therefore make the transformation of any unbounded range with
3835 the value Z, Z being greater than any representable number. This permits
3836 us to treat unbounded ranges as equal. */
3837 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3838 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3839 switch (code)
3841 case EQ_EXPR:
3842 result = sgn0 == sgn1;
3843 break;
3844 case NE_EXPR:
3845 result = sgn0 != sgn1;
3846 break;
3847 case LT_EXPR:
3848 result = sgn0 < sgn1;
3849 break;
3850 case LE_EXPR:
3851 result = sgn0 <= sgn1;
3852 break;
3853 case GT_EXPR:
3854 result = sgn0 > sgn1;
3855 break;
3856 case GE_EXPR:
3857 result = sgn0 >= sgn1;
3858 break;
3859 default:
3860 gcc_unreachable ();
3863 return constant_boolean_node (result, type);
3866 /* Given EXP, a logical expression, set the range it is testing into
3867 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3868 actually being tested. *PLOW and *PHIGH will be made of the same
3869 type as the returned expression. If EXP is not a comparison, we
3870 will most likely not be returning a useful value and range. Set
3871 *STRICT_OVERFLOW_P to true if the return value is only valid
3872 because signed overflow is undefined; otherwise, do not change
3873 *STRICT_OVERFLOW_P. */
3875 tree
3876 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
3877 bool *strict_overflow_p)
3879 enum tree_code code;
3880 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3881 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3882 int in_p, n_in_p;
3883 tree low, high, n_low, n_high;
3884 location_t loc = EXPR_LOCATION (exp);
3886 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3887 and see if we can refine the range. Some of the cases below may not
3888 happen, but it doesn't seem worth worrying about this. We "continue"
3889 the outer loop when we've changed something; otherwise we "break"
3890 the switch, which will "break" the while. */
3892 in_p = 0;
3893 low = high = build_int_cst (TREE_TYPE (exp), 0);
3895 while (1)
3897 code = TREE_CODE (exp);
3898 exp_type = TREE_TYPE (exp);
3900 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3902 if (TREE_OPERAND_LENGTH (exp) > 0)
3903 arg0 = TREE_OPERAND (exp, 0);
3904 if (TREE_CODE_CLASS (code) == tcc_comparison
3905 || TREE_CODE_CLASS (code) == tcc_unary
3906 || TREE_CODE_CLASS (code) == tcc_binary)
3907 arg0_type = TREE_TYPE (arg0);
3908 if (TREE_CODE_CLASS (code) == tcc_binary
3909 || TREE_CODE_CLASS (code) == tcc_comparison
3910 || (TREE_CODE_CLASS (code) == tcc_expression
3911 && TREE_OPERAND_LENGTH (exp) > 1))
3912 arg1 = TREE_OPERAND (exp, 1);
3915 switch (code)
3917 case TRUTH_NOT_EXPR:
3918 in_p = ! in_p, exp = arg0;
3919 continue;
3921 case EQ_EXPR: case NE_EXPR:
3922 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3923 /* We can only do something if the range is testing for zero
3924 and if the second operand is an integer constant. Note that
3925 saying something is "in" the range we make is done by
3926 complementing IN_P since it will set in the initial case of
3927 being not equal to zero; "out" is leaving it alone. */
3928 if (low == 0 || high == 0
3929 || ! integer_zerop (low) || ! integer_zerop (high)
3930 || TREE_CODE (arg1) != INTEGER_CST)
3931 break;
3933 switch (code)
3935 case NE_EXPR: /* - [c, c] */
3936 low = high = arg1;
3937 break;
3938 case EQ_EXPR: /* + [c, c] */
3939 in_p = ! in_p, low = high = arg1;
3940 break;
3941 case GT_EXPR: /* - [-, c] */
3942 low = 0, high = arg1;
3943 break;
3944 case GE_EXPR: /* + [c, -] */
3945 in_p = ! in_p, low = arg1, high = 0;
3946 break;
3947 case LT_EXPR: /* - [c, -] */
3948 low = arg1, high = 0;
3949 break;
3950 case LE_EXPR: /* + [-, c] */
3951 in_p = ! in_p, low = 0, high = arg1;
3952 break;
3953 default:
3954 gcc_unreachable ();
3957 /* If this is an unsigned comparison, we also know that EXP is
3958 greater than or equal to zero. We base the range tests we make
3959 on that fact, so we record it here so we can parse existing
3960 range tests. We test arg0_type since often the return type
3961 of, e.g. EQ_EXPR, is boolean. */
3962 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3964 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3965 in_p, low, high, 1,
3966 build_int_cst (arg0_type, 0),
3967 NULL_TREE))
3968 break;
3970 in_p = n_in_p, low = n_low, high = n_high;
3972 /* If the high bound is missing, but we have a nonzero low
3973 bound, reverse the range so it goes from zero to the low bound
3974 minus 1. */
3975 if (high == 0 && low && ! integer_zerop (low))
3977 in_p = ! in_p;
3978 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3979 integer_one_node, 0);
3980 low = build_int_cst (arg0_type, 0);
3984 exp = arg0;
3985 continue;
3987 case NEGATE_EXPR:
3988 /* (-x) IN [a,b] -> x in [-b, -a] */
3989 n_low = range_binop (MINUS_EXPR, exp_type,
3990 build_int_cst (exp_type, 0),
3991 0, high, 1);
3992 n_high = range_binop (MINUS_EXPR, exp_type,
3993 build_int_cst (exp_type, 0),
3994 0, low, 0);
3995 low = n_low, high = n_high;
3996 exp = arg0;
3997 continue;
3999 case BIT_NOT_EXPR:
4000 /* ~ X -> -X - 1 */
4001 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4002 build_int_cst (exp_type, 1));
4003 SET_EXPR_LOCATION (exp, loc);
4004 continue;
4006 case PLUS_EXPR: case MINUS_EXPR:
4007 if (TREE_CODE (arg1) != INTEGER_CST)
4008 break;
4010 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4011 move a constant to the other side. */
4012 if (!TYPE_UNSIGNED (arg0_type)
4013 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4014 break;
4016 /* If EXP is signed, any overflow in the computation is undefined,
4017 so we don't worry about it so long as our computations on
4018 the bounds don't overflow. For unsigned, overflow is defined
4019 and this is exactly the right thing. */
4020 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4021 arg0_type, low, 0, arg1, 0);
4022 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4023 arg0_type, high, 1, arg1, 0);
4024 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4025 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4026 break;
4028 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4029 *strict_overflow_p = true;
4031 /* Check for an unsigned range which has wrapped around the maximum
4032 value thus making n_high < n_low, and normalize it. */
4033 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4035 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4036 integer_one_node, 0);
4037 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4038 integer_one_node, 0);
4040 /* If the range is of the form +/- [ x+1, x ], we won't
4041 be able to normalize it. But then, it represents the
4042 whole range or the empty set, so make it
4043 +/- [ -, - ]. */
4044 if (tree_int_cst_equal (n_low, low)
4045 && tree_int_cst_equal (n_high, high))
4046 low = high = 0;
4047 else
4048 in_p = ! in_p;
4050 else
4051 low = n_low, high = n_high;
4053 exp = arg0;
4054 continue;
4056 CASE_CONVERT: case NON_LVALUE_EXPR:
4057 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4058 break;
4060 if (! INTEGRAL_TYPE_P (arg0_type)
4061 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4062 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4063 break;
4065 n_low = low, n_high = high;
4067 if (n_low != 0)
4068 n_low = fold_convert_loc (loc, arg0_type, n_low);
4070 if (n_high != 0)
4071 n_high = fold_convert_loc (loc, arg0_type, n_high);
4074 /* If we're converting arg0 from an unsigned type, to exp,
4075 a signed type, we will be doing the comparison as unsigned.
4076 The tests above have already verified that LOW and HIGH
4077 are both positive.
4079 So we have to ensure that we will handle large unsigned
4080 values the same way that the current signed bounds treat
4081 negative values. */
4083 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4085 tree high_positive;
4086 tree equiv_type;
4087 /* For fixed-point modes, we need to pass the saturating flag
4088 as the 2nd parameter. */
4089 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4090 equiv_type = lang_hooks.types.type_for_mode
4091 (TYPE_MODE (arg0_type),
4092 TYPE_SATURATING (arg0_type));
4093 else
4094 equiv_type = lang_hooks.types.type_for_mode
4095 (TYPE_MODE (arg0_type), 1);
4097 /* A range without an upper bound is, naturally, unbounded.
4098 Since convert would have cropped a very large value, use
4099 the max value for the destination type. */
4100 high_positive
4101 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4102 : TYPE_MAX_VALUE (arg0_type);
4104 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4105 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4106 fold_convert_loc (loc, arg0_type,
4107 high_positive),
4108 build_int_cst (arg0_type, 1));
4110 /* If the low bound is specified, "and" the range with the
4111 range for which the original unsigned value will be
4112 positive. */
4113 if (low != 0)
4115 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4116 1, n_low, n_high, 1,
4117 fold_convert_loc (loc, arg0_type,
4118 integer_zero_node),
4119 high_positive))
4120 break;
4122 in_p = (n_in_p == in_p);
4124 else
4126 /* Otherwise, "or" the range with the range of the input
4127 that will be interpreted as negative. */
4128 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4129 0, n_low, n_high, 1,
4130 fold_convert_loc (loc, arg0_type,
4131 integer_zero_node),
4132 high_positive))
4133 break;
4135 in_p = (in_p != n_in_p);
4139 exp = arg0;
4140 low = n_low, high = n_high;
4141 continue;
4143 default:
4144 break;
4147 break;
4150 /* If EXP is a constant, we can evaluate whether this is true or false. */
4151 if (TREE_CODE (exp) == INTEGER_CST)
4153 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4154 exp, 0, low, 0))
4155 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4156 exp, 1, high, 1)));
4157 low = high = 0;
4158 exp = 0;
4161 *pin_p = in_p, *plow = low, *phigh = high;
4162 return exp;
4165 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4166 type, TYPE, return an expression to test if EXP is in (or out of, depending
4167 on IN_P) the range. Return 0 if the test couldn't be created. */
4169 tree
4170 build_range_check (location_t loc, tree type, tree exp, int in_p,
4171 tree low, tree high)
4173 tree etype = TREE_TYPE (exp), value;
4175 #ifdef HAVE_canonicalize_funcptr_for_compare
4176 /* Disable this optimization for function pointer expressions
4177 on targets that require function pointer canonicalization. */
4178 if (HAVE_canonicalize_funcptr_for_compare
4179 && TREE_CODE (etype) == POINTER_TYPE
4180 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4181 return NULL_TREE;
4182 #endif
4184 if (! in_p)
4186 value = build_range_check (loc, type, exp, 1, low, high);
4187 if (value != 0)
4188 return invert_truthvalue_loc (loc, value);
4190 return 0;
4193 if (low == 0 && high == 0)
4194 return build_int_cst (type, 1);
4196 if (low == 0)
4197 return fold_build2_loc (loc, LE_EXPR, type, exp,
4198 fold_convert_loc (loc, etype, high));
4200 if (high == 0)
4201 return fold_build2_loc (loc, GE_EXPR, type, exp,
4202 fold_convert_loc (loc, etype, low));
4204 if (operand_equal_p (low, high, 0))
4205 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4206 fold_convert_loc (loc, etype, low));
4208 if (integer_zerop (low))
4210 if (! TYPE_UNSIGNED (etype))
4212 etype = unsigned_type_for (etype);
4213 high = fold_convert_loc (loc, etype, high);
4214 exp = fold_convert_loc (loc, etype, exp);
4216 return build_range_check (loc, type, exp, 1, 0, high);
4219 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4220 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4222 unsigned HOST_WIDE_INT lo;
4223 HOST_WIDE_INT hi;
4224 int prec;
4226 prec = TYPE_PRECISION (etype);
4227 if (prec <= HOST_BITS_PER_WIDE_INT)
4229 hi = 0;
4230 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4232 else
4234 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4235 lo = (unsigned HOST_WIDE_INT) -1;
4238 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4240 if (TYPE_UNSIGNED (etype))
4242 tree signed_etype = signed_type_for (etype);
4243 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4244 etype
4245 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4246 else
4247 etype = signed_etype;
4248 exp = fold_convert_loc (loc, etype, exp);
4250 return fold_build2_loc (loc, GT_EXPR, type, exp,
4251 build_int_cst (etype, 0));
4255 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4256 This requires wrap-around arithmetics for the type of the expression.
4257 First make sure that arithmetics in this type is valid, then make sure
4258 that it wraps around. */
4259 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4260 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4261 TYPE_UNSIGNED (etype));
4263 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4265 tree utype, minv, maxv;
4267 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4268 for the type in question, as we rely on this here. */
4269 utype = unsigned_type_for (etype);
4270 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4271 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4272 integer_one_node, 1);
4273 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4275 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4276 minv, 1, maxv, 1)))
4277 etype = utype;
4278 else
4279 return 0;
4282 high = fold_convert_loc (loc, etype, high);
4283 low = fold_convert_loc (loc, etype, low);
4284 exp = fold_convert_loc (loc, etype, exp);
4286 value = const_binop (MINUS_EXPR, high, low, 0);
4289 if (POINTER_TYPE_P (etype))
4291 if (value != 0 && !TREE_OVERFLOW (value))
4293 low = fold_convert_loc (loc, sizetype, low);
4294 low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
4295 return build_range_check (loc, type,
4296 fold_build2_loc (loc, POINTER_PLUS_EXPR,
4297 etype, exp, low),
4298 1, build_int_cst (etype, 0), value);
4300 return 0;
4303 if (value != 0 && !TREE_OVERFLOW (value))
4304 return build_range_check (loc, type,
4305 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4306 1, build_int_cst (etype, 0), value);
4308 return 0;
4311 /* Return the predecessor of VAL in its type, handling the infinite case. */
4313 static tree
4314 range_predecessor (tree val)
4316 tree type = TREE_TYPE (val);
4318 if (INTEGRAL_TYPE_P (type)
4319 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4320 return 0;
4321 else
4322 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4325 /* Return the successor of VAL in its type, handling the infinite case. */
4327 static tree
4328 range_successor (tree val)
4330 tree type = TREE_TYPE (val);
4332 if (INTEGRAL_TYPE_P (type)
4333 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4334 return 0;
4335 else
4336 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4339 /* Given two ranges, see if we can merge them into one. Return 1 if we
4340 can, 0 if we can't. Set the output range into the specified parameters. */
4342 bool
4343 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4344 tree high0, int in1_p, tree low1, tree high1)
4346 int no_overlap;
4347 int subset;
4348 int temp;
4349 tree tem;
4350 int in_p;
4351 tree low, high;
4352 int lowequal = ((low0 == 0 && low1 == 0)
4353 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4354 low0, 0, low1, 0)));
4355 int highequal = ((high0 == 0 && high1 == 0)
4356 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4357 high0, 1, high1, 1)));
4359 /* Make range 0 be the range that starts first, or ends last if they
4360 start at the same value. Swap them if it isn't. */
4361 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4362 low0, 0, low1, 0))
4363 || (lowequal
4364 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4365 high1, 1, high0, 1))))
4367 temp = in0_p, in0_p = in1_p, in1_p = temp;
4368 tem = low0, low0 = low1, low1 = tem;
4369 tem = high0, high0 = high1, high1 = tem;
4372 /* Now flag two cases, whether the ranges are disjoint or whether the
4373 second range is totally subsumed in the first. Note that the tests
4374 below are simplified by the ones above. */
4375 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4376 high0, 1, low1, 0));
4377 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4378 high1, 1, high0, 1));
4380 /* We now have four cases, depending on whether we are including or
4381 excluding the two ranges. */
4382 if (in0_p && in1_p)
4384 /* If they don't overlap, the result is false. If the second range
4385 is a subset it is the result. Otherwise, the range is from the start
4386 of the second to the end of the first. */
4387 if (no_overlap)
4388 in_p = 0, low = high = 0;
4389 else if (subset)
4390 in_p = 1, low = low1, high = high1;
4391 else
4392 in_p = 1, low = low1, high = high0;
4395 else if (in0_p && ! in1_p)
4397 /* If they don't overlap, the result is the first range. If they are
4398 equal, the result is false. If the second range is a subset of the
4399 first, and the ranges begin at the same place, we go from just after
4400 the end of the second range to the end of the first. If the second
4401 range is not a subset of the first, or if it is a subset and both
4402 ranges end at the same place, the range starts at the start of the
4403 first range and ends just before the second range.
4404 Otherwise, we can't describe this as a single range. */
4405 if (no_overlap)
4406 in_p = 1, low = low0, high = high0;
4407 else if (lowequal && highequal)
4408 in_p = 0, low = high = 0;
4409 else if (subset && lowequal)
4411 low = range_successor (high1);
4412 high = high0;
4413 in_p = 1;
4414 if (low == 0)
4416 /* We are in the weird situation where high0 > high1 but
4417 high1 has no successor. Punt. */
4418 return 0;
4421 else if (! subset || highequal)
4423 low = low0;
4424 high = range_predecessor (low1);
4425 in_p = 1;
4426 if (high == 0)
4428 /* low0 < low1 but low1 has no predecessor. Punt. */
4429 return 0;
4432 else
4433 return 0;
4436 else if (! in0_p && in1_p)
4438 /* If they don't overlap, the result is the second range. If the second
4439 is a subset of the first, the result is false. Otherwise,
4440 the range starts just after the first range and ends at the
4441 end of the second. */
4442 if (no_overlap)
4443 in_p = 1, low = low1, high = high1;
4444 else if (subset || highequal)
4445 in_p = 0, low = high = 0;
4446 else
4448 low = range_successor (high0);
4449 high = high1;
4450 in_p = 1;
4451 if (low == 0)
4453 /* high1 > high0 but high0 has no successor. Punt. */
4454 return 0;
4459 else
4461 /* The case where we are excluding both ranges. Here the complex case
4462 is if they don't overlap. In that case, the only time we have a
4463 range is if they are adjacent. If the second is a subset of the
4464 first, the result is the first. Otherwise, the range to exclude
4465 starts at the beginning of the first range and ends at the end of the
4466 second. */
4467 if (no_overlap)
4469 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4470 range_successor (high0),
4471 1, low1, 0)))
4472 in_p = 0, low = low0, high = high1;
4473 else
4475 /* Canonicalize - [min, x] into - [-, x]. */
4476 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4477 switch (TREE_CODE (TREE_TYPE (low0)))
4479 case ENUMERAL_TYPE:
4480 if (TYPE_PRECISION (TREE_TYPE (low0))
4481 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4482 break;
4483 /* FALLTHROUGH */
4484 case INTEGER_TYPE:
4485 if (tree_int_cst_equal (low0,
4486 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4487 low0 = 0;
4488 break;
4489 case POINTER_TYPE:
4490 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4491 && integer_zerop (low0))
4492 low0 = 0;
4493 break;
4494 default:
4495 break;
4498 /* Canonicalize - [x, max] into - [x, -]. */
4499 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4500 switch (TREE_CODE (TREE_TYPE (high1)))
4502 case ENUMERAL_TYPE:
4503 if (TYPE_PRECISION (TREE_TYPE (high1))
4504 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4505 break;
4506 /* FALLTHROUGH */
4507 case INTEGER_TYPE:
4508 if (tree_int_cst_equal (high1,
4509 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4510 high1 = 0;
4511 break;
4512 case POINTER_TYPE:
4513 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4514 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4515 high1, 1,
4516 integer_one_node, 1)))
4517 high1 = 0;
4518 break;
4519 default:
4520 break;
4523 /* The ranges might be also adjacent between the maximum and
4524 minimum values of the given type. For
4525 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4526 return + [x + 1, y - 1]. */
4527 if (low0 == 0 && high1 == 0)
4529 low = range_successor (high0);
4530 high = range_predecessor (low1);
4531 if (low == 0 || high == 0)
4532 return 0;
4534 in_p = 1;
4536 else
4537 return 0;
4540 else if (subset)
4541 in_p = 0, low = low0, high = high0;
4542 else
4543 in_p = 0, low = low0, high = high1;
4546 *pin_p = in_p, *plow = low, *phigh = high;
4547 return 1;
4551 /* Subroutine of fold, looking inside expressions of the form
4552 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4553 of the COND_EXPR. This function is being used also to optimize
4554 A op B ? C : A, by reversing the comparison first.
4556 Return a folded expression whose code is not a COND_EXPR
4557 anymore, or NULL_TREE if no folding opportunity is found. */
4559 static tree
4560 fold_cond_expr_with_comparison (location_t loc, tree type,
4561 tree arg0, tree arg1, tree arg2)
4563 enum tree_code comp_code = TREE_CODE (arg0);
4564 tree arg00 = TREE_OPERAND (arg0, 0);
4565 tree arg01 = TREE_OPERAND (arg0, 1);
4566 tree arg1_type = TREE_TYPE (arg1);
4567 tree tem;
4569 STRIP_NOPS (arg1);
4570 STRIP_NOPS (arg2);
4572 /* If we have A op 0 ? A : -A, consider applying the following
4573 transformations:
4575 A == 0? A : -A same as -A
4576 A != 0? A : -A same as A
4577 A >= 0? A : -A same as abs (A)
4578 A > 0? A : -A same as abs (A)
4579 A <= 0? A : -A same as -abs (A)
4580 A < 0? A : -A same as -abs (A)
4582 None of these transformations work for modes with signed
4583 zeros. If A is +/-0, the first two transformations will
4584 change the sign of the result (from +0 to -0, or vice
4585 versa). The last four will fix the sign of the result,
4586 even though the original expressions could be positive or
4587 negative, depending on the sign of A.
4589 Note that all these transformations are correct if A is
4590 NaN, since the two alternatives (A and -A) are also NaNs. */
4591 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4592 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4593 ? real_zerop (arg01)
4594 : integer_zerop (arg01))
4595 && ((TREE_CODE (arg2) == NEGATE_EXPR
4596 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4597 /* In the case that A is of the form X-Y, '-A' (arg2) may
4598 have already been folded to Y-X, check for that. */
4599 || (TREE_CODE (arg1) == MINUS_EXPR
4600 && TREE_CODE (arg2) == MINUS_EXPR
4601 && operand_equal_p (TREE_OPERAND (arg1, 0),
4602 TREE_OPERAND (arg2, 1), 0)
4603 && operand_equal_p (TREE_OPERAND (arg1, 1),
4604 TREE_OPERAND (arg2, 0), 0))))
4605 switch (comp_code)
4607 case EQ_EXPR:
4608 case UNEQ_EXPR:
4609 tem = fold_convert_loc (loc, arg1_type, arg1);
4610 return pedantic_non_lvalue_loc (loc,
4611 fold_convert_loc (loc, type,
4612 negate_expr (tem)));
4613 case NE_EXPR:
4614 case LTGT_EXPR:
4615 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4616 case UNGE_EXPR:
4617 case UNGT_EXPR:
4618 if (flag_trapping_math)
4619 break;
4620 /* Fall through. */
4621 case GE_EXPR:
4622 case GT_EXPR:
4623 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4624 arg1 = fold_convert_loc (loc, signed_type_for
4625 (TREE_TYPE (arg1)), arg1);
4626 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4627 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4628 case UNLE_EXPR:
4629 case UNLT_EXPR:
4630 if (flag_trapping_math)
4631 break;
4632 case LE_EXPR:
4633 case LT_EXPR:
4634 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4635 arg1 = fold_convert_loc (loc, signed_type_for
4636 (TREE_TYPE (arg1)), arg1);
4637 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4638 return negate_expr (fold_convert_loc (loc, type, tem));
4639 default:
4640 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4641 break;
4644 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4645 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4646 both transformations are correct when A is NaN: A != 0
4647 is then true, and A == 0 is false. */
4649 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4650 && integer_zerop (arg01) && integer_zerop (arg2))
4652 if (comp_code == NE_EXPR)
4653 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4654 else if (comp_code == EQ_EXPR)
4655 return build_int_cst (type, 0);
4658 /* Try some transformations of A op B ? A : B.
4660 A == B? A : B same as B
4661 A != B? A : B same as A
4662 A >= B? A : B same as max (A, B)
4663 A > B? A : B same as max (B, A)
4664 A <= B? A : B same as min (A, B)
4665 A < B? A : B same as min (B, A)
4667 As above, these transformations don't work in the presence
4668 of signed zeros. For example, if A and B are zeros of
4669 opposite sign, the first two transformations will change
4670 the sign of the result. In the last four, the original
4671 expressions give different results for (A=+0, B=-0) and
4672 (A=-0, B=+0), but the transformed expressions do not.
4674 The first two transformations are correct if either A or B
4675 is a NaN. In the first transformation, the condition will
4676 be false, and B will indeed be chosen. In the case of the
4677 second transformation, the condition A != B will be true,
4678 and A will be chosen.
4680 The conversions to max() and min() are not correct if B is
4681 a number and A is not. The conditions in the original
4682 expressions will be false, so all four give B. The min()
4683 and max() versions would give a NaN instead. */
4684 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4685 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4686 /* Avoid these transformations if the COND_EXPR may be used
4687 as an lvalue in the C++ front-end. PR c++/19199. */
4688 && (in_gimple_form
4689 || (strcmp (lang_hooks.name, "GNU C++") != 0
4690 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4691 || ! maybe_lvalue_p (arg1)
4692 || ! maybe_lvalue_p (arg2)))
4694 tree comp_op0 = arg00;
4695 tree comp_op1 = arg01;
4696 tree comp_type = TREE_TYPE (comp_op0);
4698 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4699 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4701 comp_type = type;
4702 comp_op0 = arg1;
4703 comp_op1 = arg2;
4706 switch (comp_code)
4708 case EQ_EXPR:
4709 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4710 case NE_EXPR:
4711 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4712 case LE_EXPR:
4713 case LT_EXPR:
4714 case UNLE_EXPR:
4715 case UNLT_EXPR:
4716 /* In C++ a ?: expression can be an lvalue, so put the
4717 operand which will be used if they are equal first
4718 so that we can convert this back to the
4719 corresponding COND_EXPR. */
4720 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4722 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4723 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4724 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4725 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4726 : fold_build2_loc (loc, MIN_EXPR, comp_type,
4727 comp_op1, comp_op0);
4728 return pedantic_non_lvalue_loc (loc,
4729 fold_convert_loc (loc, type, tem));
4731 break;
4732 case GE_EXPR:
4733 case GT_EXPR:
4734 case UNGE_EXPR:
4735 case UNGT_EXPR:
4736 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4738 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4739 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4740 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4741 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4742 : fold_build2_loc (loc, MAX_EXPR, comp_type,
4743 comp_op1, comp_op0);
4744 return pedantic_non_lvalue_loc (loc,
4745 fold_convert_loc (loc, type, tem));
4747 break;
4748 case UNEQ_EXPR:
4749 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4750 return pedantic_non_lvalue_loc (loc,
4751 fold_convert_loc (loc, type, arg2));
4752 break;
4753 case LTGT_EXPR:
4754 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4755 return pedantic_non_lvalue_loc (loc,
4756 fold_convert_loc (loc, type, arg1));
4757 break;
4758 default:
4759 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4760 break;
4764 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4765 we might still be able to simplify this. For example,
4766 if C1 is one less or one more than C2, this might have started
4767 out as a MIN or MAX and been transformed by this function.
4768 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
4770 if (INTEGRAL_TYPE_P (type)
4771 && TREE_CODE (arg01) == INTEGER_CST
4772 && TREE_CODE (arg2) == INTEGER_CST)
4773 switch (comp_code)
4775 case EQ_EXPR:
4776 if (TREE_CODE (arg1) == INTEGER_CST)
4777 break;
4778 /* We can replace A with C1 in this case. */
4779 arg1 = fold_convert_loc (loc, type, arg01);
4780 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4782 case LT_EXPR:
4783 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4784 MIN_EXPR, to preserve the signedness of the comparison. */
4785 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4786 OEP_ONLY_CONST)
4787 && operand_equal_p (arg01,
4788 const_binop (PLUS_EXPR, arg2,
4789 build_int_cst (type, 1), 0),
4790 OEP_ONLY_CONST))
4792 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4793 fold_convert_loc (loc, TREE_TYPE (arg00),
4794 arg2));
4795 return pedantic_non_lvalue_loc (loc,
4796 fold_convert_loc (loc, type, tem));
4798 break;
4800 case LE_EXPR:
4801 /* If C1 is C2 - 1, this is min(A, C2), with the same care
4802 as above. */
4803 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4804 OEP_ONLY_CONST)
4805 && operand_equal_p (arg01,
4806 const_binop (MINUS_EXPR, arg2,
4807 build_int_cst (type, 1), 0),
4808 OEP_ONLY_CONST))
4810 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4811 fold_convert_loc (loc, TREE_TYPE (arg00),
4812 arg2));
4813 return pedantic_non_lvalue_loc (loc,
4814 fold_convert_loc (loc, type, tem));
4816 break;
4818 case GT_EXPR:
4819 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4820 MAX_EXPR, to preserve the signedness of the comparison. */
4821 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4822 OEP_ONLY_CONST)
4823 && operand_equal_p (arg01,
4824 const_binop (MINUS_EXPR, arg2,
4825 build_int_cst (type, 1), 0),
4826 OEP_ONLY_CONST))
4828 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4829 fold_convert_loc (loc, TREE_TYPE (arg00),
4830 arg2));
4831 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4833 break;
4835 case GE_EXPR:
4836 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
4837 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4838 OEP_ONLY_CONST)
4839 && operand_equal_p (arg01,
4840 const_binop (PLUS_EXPR, arg2,
4841 build_int_cst (type, 1), 0),
4842 OEP_ONLY_CONST))
4844 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4845 fold_convert_loc (loc, TREE_TYPE (arg00),
4846 arg2));
4847 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4849 break;
4850 case NE_EXPR:
4851 break;
4852 default:
4853 gcc_unreachable ();
4856 return NULL_TREE;
4861 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4862 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4863 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4864 false) >= 2)
4865 #endif
4867 /* EXP is some logical combination of boolean tests. See if we can
4868 merge it into some range test. Return the new tree if so. */
4870 static tree
4871 fold_range_test (location_t loc, enum tree_code code, tree type,
4872 tree op0, tree op1)
4874 int or_op = (code == TRUTH_ORIF_EXPR
4875 || code == TRUTH_OR_EXPR);
4876 int in0_p, in1_p, in_p;
4877 tree low0, low1, low, high0, high1, high;
4878 bool strict_overflow_p = false;
4879 tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4880 tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4881 tree tem;
4882 const char * const warnmsg = G_("assuming signed overflow does not occur "
4883 "when simplifying range test");
4885 /* If this is an OR operation, invert both sides; we will invert
4886 again at the end. */
4887 if (or_op)
4888 in0_p = ! in0_p, in1_p = ! in1_p;
4890 /* If both expressions are the same, if we can merge the ranges, and we
4891 can build the range test, return it or it inverted. If one of the
4892 ranges is always true or always false, consider it to be the same
4893 expression as the other. */
4894 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4895 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4896 in1_p, low1, high1)
4897 && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type,
4898 lhs != 0 ? lhs
4899 : rhs != 0 ? rhs : integer_zero_node,
4900 in_p, low, high))))
4902 if (strict_overflow_p)
4903 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
4904 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
4907 /* On machines where the branch cost is expensive, if this is a
4908 short-circuited branch and the underlying object on both sides
4909 is the same, make a non-short-circuit operation. */
4910 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4911 && lhs != 0 && rhs != 0
4912 && (code == TRUTH_ANDIF_EXPR
4913 || code == TRUTH_ORIF_EXPR)
4914 && operand_equal_p (lhs, rhs, 0))
4916 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
4917 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4918 which cases we can't do this. */
4919 if (simple_operand_p (lhs))
4921 tem = build2 (code == TRUTH_ANDIF_EXPR
4922 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4923 type, op0, op1);
4924 SET_EXPR_LOCATION (tem, loc);
4925 return tem;
4928 else if (lang_hooks.decls.global_bindings_p () == 0
4929 && ! CONTAINS_PLACEHOLDER_P (lhs))
4931 tree common = save_expr (lhs);
4933 if (0 != (lhs = build_range_check (loc, type, common,
4934 or_op ? ! in0_p : in0_p,
4935 low0, high0))
4936 && (0 != (rhs = build_range_check (loc, type, common,
4937 or_op ? ! in1_p : in1_p,
4938 low1, high1))))
4940 if (strict_overflow_p)
4941 fold_overflow_warning (warnmsg,
4942 WARN_STRICT_OVERFLOW_COMPARISON);
4943 tem = build2 (code == TRUTH_ANDIF_EXPR
4944 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4945 type, lhs, rhs);
4946 SET_EXPR_LOCATION (tem, loc);
4947 return tem;
4952 return 0;
4955 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4956 bit value. Arrange things so the extra bits will be set to zero if and
4957 only if C is signed-extended to its full width. If MASK is nonzero,
4958 it is an INTEGER_CST that should be AND'ed with the extra bits. */
4960 static tree
4961 unextend (tree c, int p, int unsignedp, tree mask)
4963 tree type = TREE_TYPE (c);
4964 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4965 tree temp;
4967 if (p == modesize || unsignedp)
4968 return c;
4970 /* We work by getting just the sign bit into the low-order bit, then
4971 into the high-order bit, then sign-extend. We then XOR that value
4972 with C. */
4973 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
4974 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
4976 /* We must use a signed type in order to get an arithmetic right shift.
4977 However, we must also avoid introducing accidental overflows, so that
4978 a subsequent call to integer_zerop will work. Hence we must
4979 do the type conversion here. At this point, the constant is either
4980 zero or one, and the conversion to a signed type can never overflow.
4981 We could get an overflow if this conversion is done anywhere else. */
4982 if (TYPE_UNSIGNED (type))
4983 temp = fold_convert (signed_type_for (type), temp);
4985 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
4986 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
4987 if (mask != 0)
4988 temp = const_binop (BIT_AND_EXPR, temp,
4989 fold_convert (TREE_TYPE (c), mask),
4991 /* If necessary, convert the type back to match the type of C. */
4992 if (TYPE_UNSIGNED (type))
4993 temp = fold_convert (type, temp);
4995 return fold_convert (type,
4996 const_binop (BIT_XOR_EXPR, c, temp, 0));
4999 /* For an expression that has the form
5000 (A && B) || ~B
5002 (A || B) && ~B,
5003 we can drop one of the inner expressions and simplify to
5004 A || ~B
5006 A && ~B
5007 LOC is the location of the resulting expression. OP is the inner
5008 logical operation; the left-hand side in the examples above, while CMPOP
5009 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5010 removing a condition that guards another, as in
5011 (A != NULL && A->...) || A == NULL
5012 which we must not transform. If RHS_ONLY is true, only eliminate the
5013 right-most operand of the inner logical operation. */
5015 static tree
5016 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5017 bool rhs_only)
5019 tree type = TREE_TYPE (cmpop);
5020 enum tree_code code = TREE_CODE (cmpop);
5021 enum tree_code truthop_code = TREE_CODE (op);
5022 tree lhs = TREE_OPERAND (op, 0);
5023 tree rhs = TREE_OPERAND (op, 1);
5024 tree orig_lhs = lhs, orig_rhs = rhs;
5025 enum tree_code rhs_code = TREE_CODE (rhs);
5026 enum tree_code lhs_code = TREE_CODE (lhs);
5027 enum tree_code inv_code;
5029 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5030 return NULL_TREE;
5032 if (TREE_CODE_CLASS (code) != tcc_comparison)
5033 return NULL_TREE;
5035 if (rhs_code == truthop_code)
5037 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5038 if (newrhs != NULL_TREE)
5040 rhs = newrhs;
5041 rhs_code = TREE_CODE (rhs);
5044 if (lhs_code == truthop_code && !rhs_only)
5046 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5047 if (newlhs != NULL_TREE)
5049 lhs = newlhs;
5050 lhs_code = TREE_CODE (lhs);
5054 inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
5055 if (inv_code == rhs_code
5056 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5057 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5058 return lhs;
5059 if (!rhs_only && inv_code == lhs_code
5060 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5061 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5062 return rhs;
5063 if (rhs != orig_rhs || lhs != orig_lhs)
5064 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5065 lhs, rhs);
5066 return NULL_TREE;
5069 /* Find ways of folding logical expressions of LHS and RHS:
5070 Try to merge two comparisons to the same innermost item.
5071 Look for range tests like "ch >= '0' && ch <= '9'".
5072 Look for combinations of simple terms on machines with expensive branches
5073 and evaluate the RHS unconditionally.
5075 For example, if we have p->a == 2 && p->b == 4 and we can make an
5076 object large enough to span both A and B, we can do this with a comparison
5077 against the object ANDed with the a mask.
5079 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5080 operations to do this with one comparison.
5082 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5083 function and the one above.
5085 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5086 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5088 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5089 two operands.
5091 We return the simplified tree or 0 if no optimization is possible. */
5093 static tree
5094 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5095 tree lhs, tree rhs)
5097 /* If this is the "or" of two comparisons, we can do something if
5098 the comparisons are NE_EXPR. If this is the "and", we can do something
5099 if the comparisons are EQ_EXPR. I.e.,
5100 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5102 WANTED_CODE is this operation code. For single bit fields, we can
5103 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5104 comparison for one-bit fields. */
5106 enum tree_code wanted_code;
5107 enum tree_code lcode, rcode;
5108 tree ll_arg, lr_arg, rl_arg, rr_arg;
5109 tree ll_inner, lr_inner, rl_inner, rr_inner;
5110 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5111 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5112 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5113 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5114 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5115 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5116 enum machine_mode lnmode, rnmode;
5117 tree ll_mask, lr_mask, rl_mask, rr_mask;
5118 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5119 tree l_const, r_const;
5120 tree lntype, rntype, result;
5121 HOST_WIDE_INT first_bit, end_bit;
5122 int volatilep;
5123 tree orig_lhs = lhs, orig_rhs = rhs;
5124 enum tree_code orig_code = code;
5126 /* Start by getting the comparison codes. Fail if anything is volatile.
5127 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5128 it were surrounded with a NE_EXPR. */
5130 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5131 return 0;
5133 lcode = TREE_CODE (lhs);
5134 rcode = TREE_CODE (rhs);
5136 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5138 lhs = build2 (NE_EXPR, truth_type, lhs,
5139 build_int_cst (TREE_TYPE (lhs), 0));
5140 lcode = NE_EXPR;
5143 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5145 rhs = build2 (NE_EXPR, truth_type, rhs,
5146 build_int_cst (TREE_TYPE (rhs), 0));
5147 rcode = NE_EXPR;
5150 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5151 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5152 return 0;
5154 ll_arg = TREE_OPERAND (lhs, 0);
5155 lr_arg = TREE_OPERAND (lhs, 1);
5156 rl_arg = TREE_OPERAND (rhs, 0);
5157 rr_arg = TREE_OPERAND (rhs, 1);
5159 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5160 if (simple_operand_p (ll_arg)
5161 && simple_operand_p (lr_arg))
5163 tree result;
5164 if (operand_equal_p (ll_arg, rl_arg, 0)
5165 && operand_equal_p (lr_arg, rr_arg, 0))
5167 result = combine_comparisons (loc, code, lcode, rcode,
5168 truth_type, ll_arg, lr_arg);
5169 if (result)
5170 return result;
5172 else if (operand_equal_p (ll_arg, rr_arg, 0)
5173 && operand_equal_p (lr_arg, rl_arg, 0))
5175 result = combine_comparisons (loc, code, lcode,
5176 swap_tree_comparison (rcode),
5177 truth_type, ll_arg, lr_arg);
5178 if (result)
5179 return result;
5183 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5184 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5186 /* If the RHS can be evaluated unconditionally and its operands are
5187 simple, it wins to evaluate the RHS unconditionally on machines
5188 with expensive branches. In this case, this isn't a comparison
5189 that can be merged. Avoid doing this if the RHS is a floating-point
5190 comparison since those can trap. */
5192 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5193 false) >= 2
5194 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5195 && simple_operand_p (rl_arg)
5196 && simple_operand_p (rr_arg))
5198 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5199 if (code == TRUTH_OR_EXPR
5200 && lcode == NE_EXPR && integer_zerop (lr_arg)
5201 && rcode == NE_EXPR && integer_zerop (rr_arg)
5202 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5203 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5205 result = build2 (NE_EXPR, truth_type,
5206 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5207 ll_arg, rl_arg),
5208 build_int_cst (TREE_TYPE (ll_arg), 0));
5209 goto fold_truthop_exit;
5212 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5213 if (code == TRUTH_AND_EXPR
5214 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5215 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5216 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5217 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5219 result = build2 (EQ_EXPR, truth_type,
5220 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5221 ll_arg, rl_arg),
5222 build_int_cst (TREE_TYPE (ll_arg), 0));
5223 goto fold_truthop_exit;
5226 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5228 if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5230 result = build2 (code, truth_type, lhs, rhs);
5231 goto fold_truthop_exit;
5233 return NULL_TREE;
5237 /* See if the comparisons can be merged. Then get all the parameters for
5238 each side. */
5240 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5241 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5242 return 0;
5244 volatilep = 0;
5245 ll_inner = decode_field_reference (loc, ll_arg,
5246 &ll_bitsize, &ll_bitpos, &ll_mode,
5247 &ll_unsignedp, &volatilep, &ll_mask,
5248 &ll_and_mask);
5249 lr_inner = decode_field_reference (loc, lr_arg,
5250 &lr_bitsize, &lr_bitpos, &lr_mode,
5251 &lr_unsignedp, &volatilep, &lr_mask,
5252 &lr_and_mask);
5253 rl_inner = decode_field_reference (loc, rl_arg,
5254 &rl_bitsize, &rl_bitpos, &rl_mode,
5255 &rl_unsignedp, &volatilep, &rl_mask,
5256 &rl_and_mask);
5257 rr_inner = decode_field_reference (loc, rr_arg,
5258 &rr_bitsize, &rr_bitpos, &rr_mode,
5259 &rr_unsignedp, &volatilep, &rr_mask,
5260 &rr_and_mask);
5262 /* It must be true that the inner operation on the lhs of each
5263 comparison must be the same if we are to be able to do anything.
5264 Then see if we have constants. If not, the same must be true for
5265 the rhs's. */
5266 if (volatilep || ll_inner == 0 || rl_inner == 0
5267 || ! operand_equal_p (ll_inner, rl_inner, 0))
5268 return 0;
5270 if (TREE_CODE (lr_arg) == INTEGER_CST
5271 && TREE_CODE (rr_arg) == INTEGER_CST)
5272 l_const = lr_arg, r_const = rr_arg;
5273 else if (lr_inner == 0 || rr_inner == 0
5274 || ! operand_equal_p (lr_inner, rr_inner, 0))
5275 return 0;
5276 else
5277 l_const = r_const = 0;
5279 /* If either comparison code is not correct for our logical operation,
5280 fail. However, we can convert a one-bit comparison against zero into
5281 the opposite comparison against that bit being set in the field. */
5283 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5284 if (lcode != wanted_code)
5286 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5288 /* Make the left operand unsigned, since we are only interested
5289 in the value of one bit. Otherwise we are doing the wrong
5290 thing below. */
5291 ll_unsignedp = 1;
5292 l_const = ll_mask;
5294 else
5295 return 0;
5298 /* This is analogous to the code for l_const above. */
5299 if (rcode != wanted_code)
5301 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5303 rl_unsignedp = 1;
5304 r_const = rl_mask;
5306 else
5307 return 0;
5310 /* See if we can find a mode that contains both fields being compared on
5311 the left. If we can't, fail. Otherwise, update all constants and masks
5312 to be relative to a field of that size. */
5313 first_bit = MIN (ll_bitpos, rl_bitpos);
5314 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5315 lnmode = get_best_mode (end_bit - first_bit, first_bit,
5316 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5317 volatilep);
5318 if (lnmode == VOIDmode)
5319 return 0;
5321 lnbitsize = GET_MODE_BITSIZE (lnmode);
5322 lnbitpos = first_bit & ~ (lnbitsize - 1);
5323 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5324 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5326 if (BYTES_BIG_ENDIAN)
5328 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5329 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5332 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5333 size_int (xll_bitpos), 0);
5334 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5335 size_int (xrl_bitpos), 0);
5337 if (l_const)
5339 l_const = fold_convert_loc (loc, lntype, l_const);
5340 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5341 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5342 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5343 fold_build1_loc (loc, BIT_NOT_EXPR,
5344 lntype, ll_mask),
5345 0)))
5347 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5349 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5352 if (r_const)
5354 r_const = fold_convert_loc (loc, lntype, r_const);
5355 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5356 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5357 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5358 fold_build1_loc (loc, BIT_NOT_EXPR,
5359 lntype, rl_mask),
5360 0)))
5362 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5364 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5368 /* If the right sides are not constant, do the same for it. Also,
5369 disallow this optimization if a size or signedness mismatch occurs
5370 between the left and right sides. */
5371 if (l_const == 0)
5373 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5374 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5375 /* Make sure the two fields on the right
5376 correspond to the left without being swapped. */
5377 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5378 return 0;
5380 first_bit = MIN (lr_bitpos, rr_bitpos);
5381 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5382 rnmode = get_best_mode (end_bit - first_bit, first_bit,
5383 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5384 volatilep);
5385 if (rnmode == VOIDmode)
5386 return 0;
5388 rnbitsize = GET_MODE_BITSIZE (rnmode);
5389 rnbitpos = first_bit & ~ (rnbitsize - 1);
5390 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5391 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5393 if (BYTES_BIG_ENDIAN)
5395 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5396 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5399 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5400 rntype, lr_mask),
5401 size_int (xlr_bitpos), 0);
5402 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5403 rntype, rr_mask),
5404 size_int (xrr_bitpos), 0);
5406 /* Make a mask that corresponds to both fields being compared.
5407 Do this for both items being compared. If the operands are the
5408 same size and the bits being compared are in the same position
5409 then we can do this by masking both and comparing the masked
5410 results. */
5411 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5412 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5413 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5415 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5416 ll_unsignedp || rl_unsignedp);
5417 if (! all_ones_mask_p (ll_mask, lnbitsize))
5418 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5420 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5421 lr_unsignedp || rr_unsignedp);
5422 if (! all_ones_mask_p (lr_mask, rnbitsize))
5423 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5425 result = build2 (wanted_code, truth_type, lhs, rhs);
5426 goto fold_truthop_exit;
5429 /* There is still another way we can do something: If both pairs of
5430 fields being compared are adjacent, we may be able to make a wider
5431 field containing them both.
5433 Note that we still must mask the lhs/rhs expressions. Furthermore,
5434 the mask must be shifted to account for the shift done by
5435 make_bit_field_ref. */
5436 if ((ll_bitsize + ll_bitpos == rl_bitpos
5437 && lr_bitsize + lr_bitpos == rr_bitpos)
5438 || (ll_bitpos == rl_bitpos + rl_bitsize
5439 && lr_bitpos == rr_bitpos + rr_bitsize))
5441 tree type;
5443 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5444 ll_bitsize + rl_bitsize,
5445 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5446 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5447 lr_bitsize + rr_bitsize,
5448 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5450 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5451 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5452 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5453 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5455 /* Convert to the smaller type before masking out unwanted bits. */
5456 type = lntype;
5457 if (lntype != rntype)
5459 if (lnbitsize > rnbitsize)
5461 lhs = fold_convert_loc (loc, rntype, lhs);
5462 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5463 type = rntype;
5465 else if (lnbitsize < rnbitsize)
5467 rhs = fold_convert_loc (loc, lntype, rhs);
5468 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5469 type = lntype;
5473 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5474 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5476 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5477 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5479 result = build2 (wanted_code, truth_type, lhs, rhs);
5480 goto fold_truthop_exit;
5483 return 0;
5486 /* Handle the case of comparisons with constants. If there is something in
5487 common between the masks, those bits of the constants must be the same.
5488 If not, the condition is always false. Test for this to avoid generating
5489 incorrect code below. */
5490 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5491 if (! integer_zerop (result)
5492 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5493 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5495 if (wanted_code == NE_EXPR)
5497 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5498 return constant_boolean_node (true, truth_type);
5500 else
5502 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5503 return constant_boolean_node (false, truth_type);
5507 /* Construct the expression we will return. First get the component
5508 reference we will make. Unless the mask is all ones the width of
5509 that field, perform the mask operation. Then compare with the
5510 merged constant. */
5511 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5512 ll_unsignedp || rl_unsignedp);
5514 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5515 if (! all_ones_mask_p (ll_mask, lnbitsize))
5517 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5518 SET_EXPR_LOCATION (result, loc);
5521 result = build2 (wanted_code, truth_type, result,
5522 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5524 fold_truthop_exit:
5525 SET_EXPR_LOCATION (result, loc);
5526 return result;
5529 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5530 constant. */
5532 static tree
5533 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5534 tree op0, tree op1)
5536 tree arg0 = op0;
5537 enum tree_code op_code;
5538 tree comp_const;
5539 tree minmax_const;
5540 int consts_equal, consts_lt;
5541 tree inner;
5543 STRIP_SIGN_NOPS (arg0);
5545 op_code = TREE_CODE (arg0);
5546 minmax_const = TREE_OPERAND (arg0, 1);
5547 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5548 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5549 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5550 inner = TREE_OPERAND (arg0, 0);
5552 /* If something does not permit us to optimize, return the original tree. */
5553 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5554 || TREE_CODE (comp_const) != INTEGER_CST
5555 || TREE_OVERFLOW (comp_const)
5556 || TREE_CODE (minmax_const) != INTEGER_CST
5557 || TREE_OVERFLOW (minmax_const))
5558 return NULL_TREE;
5560 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5561 and GT_EXPR, doing the rest with recursive calls using logical
5562 simplifications. */
5563 switch (code)
5565 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5567 tree tem
5568 = optimize_minmax_comparison (loc,
5569 invert_tree_comparison (code, false),
5570 type, op0, op1);
5571 if (tem)
5572 return invert_truthvalue_loc (loc, tem);
5573 return NULL_TREE;
5576 case GE_EXPR:
5577 return
5578 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5579 optimize_minmax_comparison
5580 (loc, EQ_EXPR, type, arg0, comp_const),
5581 optimize_minmax_comparison
5582 (loc, GT_EXPR, type, arg0, comp_const));
5584 case EQ_EXPR:
5585 if (op_code == MAX_EXPR && consts_equal)
5586 /* MAX (X, 0) == 0 -> X <= 0 */
5587 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5589 else if (op_code == MAX_EXPR && consts_lt)
5590 /* MAX (X, 0) == 5 -> X == 5 */
5591 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5593 else if (op_code == MAX_EXPR)
5594 /* MAX (X, 0) == -1 -> false */
5595 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5597 else if (consts_equal)
5598 /* MIN (X, 0) == 0 -> X >= 0 */
5599 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5601 else if (consts_lt)
5602 /* MIN (X, 0) == 5 -> false */
5603 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5605 else
5606 /* MIN (X, 0) == -1 -> X == -1 */
5607 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5609 case GT_EXPR:
5610 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5611 /* MAX (X, 0) > 0 -> X > 0
5612 MAX (X, 0) > 5 -> X > 5 */
5613 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5615 else if (op_code == MAX_EXPR)
5616 /* MAX (X, 0) > -1 -> true */
5617 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5619 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5620 /* MIN (X, 0) > 0 -> false
5621 MIN (X, 0) > 5 -> false */
5622 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5624 else
5625 /* MIN (X, 0) > -1 -> X > -1 */
5626 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5628 default:
5629 return NULL_TREE;
5633 /* T is an integer expression that is being multiplied, divided, or taken a
5634 modulus (CODE says which and what kind of divide or modulus) by a
5635 constant C. See if we can eliminate that operation by folding it with
5636 other operations already in T. WIDE_TYPE, if non-null, is a type that
5637 should be used for the computation if wider than our type.
5639 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5640 (X * 2) + (Y * 4). We must, however, be assured that either the original
5641 expression would not overflow or that overflow is undefined for the type
5642 in the language in question.
5644 If we return a non-null expression, it is an equivalent form of the
5645 original computation, but need not be in the original type.
5647 We set *STRICT_OVERFLOW_P to true if the return values depends on
5648 signed overflow being undefined. Otherwise we do not change
5649 *STRICT_OVERFLOW_P. */
5651 static tree
5652 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5653 bool *strict_overflow_p)
5655 /* To avoid exponential search depth, refuse to allow recursion past
5656 three levels. Beyond that (1) it's highly unlikely that we'll find
5657 something interesting and (2) we've probably processed it before
5658 when we built the inner expression. */
5660 static int depth;
5661 tree ret;
5663 if (depth > 3)
5664 return NULL;
5666 depth++;
5667 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5668 depth--;
5670 return ret;
5673 static tree
5674 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5675 bool *strict_overflow_p)
5677 tree type = TREE_TYPE (t);
5678 enum tree_code tcode = TREE_CODE (t);
5679 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5680 > GET_MODE_SIZE (TYPE_MODE (type)))
5681 ? wide_type : type);
5682 tree t1, t2;
5683 int same_p = tcode == code;
5684 tree op0 = NULL_TREE, op1 = NULL_TREE;
5685 bool sub_strict_overflow_p;
5687 /* Don't deal with constants of zero here; they confuse the code below. */
5688 if (integer_zerop (c))
5689 return NULL_TREE;
5691 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5692 op0 = TREE_OPERAND (t, 0);
5694 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5695 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5697 /* Note that we need not handle conditional operations here since fold
5698 already handles those cases. So just do arithmetic here. */
5699 switch (tcode)
5701 case INTEGER_CST:
5702 /* For a constant, we can always simplify if we are a multiply
5703 or (for divide and modulus) if it is a multiple of our constant. */
5704 if (code == MULT_EXPR
5705 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
5706 return const_binop (code, fold_convert (ctype, t),
5707 fold_convert (ctype, c), 0);
5708 break;
5710 CASE_CONVERT: case NON_LVALUE_EXPR:
5711 /* If op0 is an expression ... */
5712 if ((COMPARISON_CLASS_P (op0)
5713 || UNARY_CLASS_P (op0)
5714 || BINARY_CLASS_P (op0)
5715 || VL_EXP_CLASS_P (op0)
5716 || EXPRESSION_CLASS_P (op0))
5717 /* ... and has wrapping overflow, and its type is smaller
5718 than ctype, then we cannot pass through as widening. */
5719 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5720 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5721 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5722 && (TYPE_PRECISION (ctype)
5723 > TYPE_PRECISION (TREE_TYPE (op0))))
5724 /* ... or this is a truncation (t is narrower than op0),
5725 then we cannot pass through this narrowing. */
5726 || (TYPE_PRECISION (type)
5727 < TYPE_PRECISION (TREE_TYPE (op0)))
5728 /* ... or signedness changes for division or modulus,
5729 then we cannot pass through this conversion. */
5730 || (code != MULT_EXPR
5731 && (TYPE_UNSIGNED (ctype)
5732 != TYPE_UNSIGNED (TREE_TYPE (op0))))
5733 /* ... or has undefined overflow while the converted to
5734 type has not, we cannot do the operation in the inner type
5735 as that would introduce undefined overflow. */
5736 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5737 && !TYPE_OVERFLOW_UNDEFINED (type))))
5738 break;
5740 /* Pass the constant down and see if we can make a simplification. If
5741 we can, replace this expression with the inner simplification for
5742 possible later conversion to our or some other type. */
5743 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5744 && TREE_CODE (t2) == INTEGER_CST
5745 && !TREE_OVERFLOW (t2)
5746 && (0 != (t1 = extract_muldiv (op0, t2, code,
5747 code == MULT_EXPR
5748 ? ctype : NULL_TREE,
5749 strict_overflow_p))))
5750 return t1;
5751 break;
5753 case ABS_EXPR:
5754 /* If widening the type changes it from signed to unsigned, then we
5755 must avoid building ABS_EXPR itself as unsigned. */
5756 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5758 tree cstype = (*signed_type_for) (ctype);
5759 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5760 != 0)
5762 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5763 return fold_convert (ctype, t1);
5765 break;
5767 /* If the constant is negative, we cannot simplify this. */
5768 if (tree_int_cst_sgn (c) == -1)
5769 break;
5770 /* FALLTHROUGH */
5771 case NEGATE_EXPR:
5772 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5773 != 0)
5774 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5775 break;
5777 case MIN_EXPR: case MAX_EXPR:
5778 /* If widening the type changes the signedness, then we can't perform
5779 this optimization as that changes the result. */
5780 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5781 break;
5783 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5784 sub_strict_overflow_p = false;
5785 if ((t1 = extract_muldiv (op0, c, code, wide_type,
5786 &sub_strict_overflow_p)) != 0
5787 && (t2 = extract_muldiv (op1, c, code, wide_type,
5788 &sub_strict_overflow_p)) != 0)
5790 if (tree_int_cst_sgn (c) < 0)
5791 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5792 if (sub_strict_overflow_p)
5793 *strict_overflow_p = true;
5794 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5795 fold_convert (ctype, t2));
5797 break;
5799 case LSHIFT_EXPR: case RSHIFT_EXPR:
5800 /* If the second operand is constant, this is a multiplication
5801 or floor division, by a power of two, so we can treat it that
5802 way unless the multiplier or divisor overflows. Signed
5803 left-shift overflow is implementation-defined rather than
5804 undefined in C90, so do not convert signed left shift into
5805 multiplication. */
5806 if (TREE_CODE (op1) == INTEGER_CST
5807 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5808 /* const_binop may not detect overflow correctly,
5809 so check for it explicitly here. */
5810 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5811 && TREE_INT_CST_HIGH (op1) == 0
5812 && 0 != (t1 = fold_convert (ctype,
5813 const_binop (LSHIFT_EXPR,
5814 size_one_node,
5815 op1, 0)))
5816 && !TREE_OVERFLOW (t1))
5817 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5818 ? MULT_EXPR : FLOOR_DIV_EXPR,
5819 ctype,
5820 fold_convert (ctype, op0),
5821 t1),
5822 c, code, wide_type, strict_overflow_p);
5823 break;
5825 case PLUS_EXPR: case MINUS_EXPR:
5826 /* See if we can eliminate the operation on both sides. If we can, we
5827 can return a new PLUS or MINUS. If we can't, the only remaining
5828 cases where we can do anything are if the second operand is a
5829 constant. */
5830 sub_strict_overflow_p = false;
5831 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5832 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5833 if (t1 != 0 && t2 != 0
5834 && (code == MULT_EXPR
5835 /* If not multiplication, we can only do this if both operands
5836 are divisible by c. */
5837 || (multiple_of_p (ctype, op0, c)
5838 && multiple_of_p (ctype, op1, c))))
5840 if (sub_strict_overflow_p)
5841 *strict_overflow_p = true;
5842 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5843 fold_convert (ctype, t2));
5846 /* If this was a subtraction, negate OP1 and set it to be an addition.
5847 This simplifies the logic below. */
5848 if (tcode == MINUS_EXPR)
5850 tcode = PLUS_EXPR, op1 = negate_expr (op1);
5851 /* If OP1 was not easily negatable, the constant may be OP0. */
5852 if (TREE_CODE (op0) == INTEGER_CST)
5854 tree tem = op0;
5855 op0 = op1;
5856 op1 = tem;
5857 tem = t1;
5858 t1 = t2;
5859 t2 = tem;
5863 if (TREE_CODE (op1) != INTEGER_CST)
5864 break;
5866 /* If either OP1 or C are negative, this optimization is not safe for
5867 some of the division and remainder types while for others we need
5868 to change the code. */
5869 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5871 if (code == CEIL_DIV_EXPR)
5872 code = FLOOR_DIV_EXPR;
5873 else if (code == FLOOR_DIV_EXPR)
5874 code = CEIL_DIV_EXPR;
5875 else if (code != MULT_EXPR
5876 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5877 break;
5880 /* If it's a multiply or a division/modulus operation of a multiple
5881 of our constant, do the operation and verify it doesn't overflow. */
5882 if (code == MULT_EXPR
5883 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5885 op1 = const_binop (code, fold_convert (ctype, op1),
5886 fold_convert (ctype, c), 0);
5887 /* We allow the constant to overflow with wrapping semantics. */
5888 if (op1 == 0
5889 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5890 break;
5892 else
5893 break;
5895 /* If we have an unsigned type is not a sizetype, we cannot widen
5896 the operation since it will change the result if the original
5897 computation overflowed. */
5898 if (TYPE_UNSIGNED (ctype)
5899 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5900 && ctype != type)
5901 break;
5903 /* If we were able to eliminate our operation from the first side,
5904 apply our operation to the second side and reform the PLUS. */
5905 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5906 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5908 /* The last case is if we are a multiply. In that case, we can
5909 apply the distributive law to commute the multiply and addition
5910 if the multiplication of the constants doesn't overflow. */
5911 if (code == MULT_EXPR)
5912 return fold_build2 (tcode, ctype,
5913 fold_build2 (code, ctype,
5914 fold_convert (ctype, op0),
5915 fold_convert (ctype, c)),
5916 op1);
5918 break;
5920 case MULT_EXPR:
5921 /* We have a special case here if we are doing something like
5922 (C * 8) % 4 since we know that's zero. */
5923 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5924 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5925 /* If the multiplication can overflow we cannot optimize this.
5926 ??? Until we can properly mark individual operations as
5927 not overflowing we need to treat sizetype special here as
5928 stor-layout relies on this opimization to make
5929 DECL_FIELD_BIT_OFFSET always a constant. */
5930 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5931 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5932 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5933 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5934 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5936 *strict_overflow_p = true;
5937 return omit_one_operand (type, integer_zero_node, op0);
5940 /* ... fall through ... */
5942 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
5943 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
5944 /* If we can extract our operation from the LHS, do so and return a
5945 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
5946 do something only if the second operand is a constant. */
5947 if (same_p
5948 && (t1 = extract_muldiv (op0, c, code, wide_type,
5949 strict_overflow_p)) != 0)
5950 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5951 fold_convert (ctype, op1));
5952 else if (tcode == MULT_EXPR && code == MULT_EXPR
5953 && (t1 = extract_muldiv (op1, c, code, wide_type,
5954 strict_overflow_p)) != 0)
5955 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5956 fold_convert (ctype, t1));
5957 else if (TREE_CODE (op1) != INTEGER_CST)
5958 return 0;
5960 /* If these are the same operation types, we can associate them
5961 assuming no overflow. */
5962 if (tcode == code
5963 && 0 != (t1 = int_const_binop (MULT_EXPR,
5964 fold_convert (ctype, op1),
5965 fold_convert (ctype, c), 1))
5966 && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
5967 TREE_INT_CST_HIGH (t1),
5968 (TYPE_UNSIGNED (ctype)
5969 && tcode != MULT_EXPR) ? -1 : 1,
5970 TREE_OVERFLOW (t1)))
5971 && !TREE_OVERFLOW (t1))
5972 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5974 /* If these operations "cancel" each other, we have the main
5975 optimizations of this pass, which occur when either constant is a
5976 multiple of the other, in which case we replace this with either an
5977 operation or CODE or TCODE.
5979 If we have an unsigned type that is not a sizetype, we cannot do
5980 this since it will change the result if the original computation
5981 overflowed. */
5982 if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5983 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5984 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5985 || (tcode == MULT_EXPR
5986 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5987 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5988 && code != MULT_EXPR)))
5990 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5992 if (TYPE_OVERFLOW_UNDEFINED (ctype))
5993 *strict_overflow_p = true;
5994 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5995 fold_convert (ctype,
5996 const_binop (TRUNC_DIV_EXPR,
5997 op1, c, 0)));
5999 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6001 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6002 *strict_overflow_p = true;
6003 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6004 fold_convert (ctype,
6005 const_binop (TRUNC_DIV_EXPR,
6006 c, op1, 0)));
6009 break;
6011 default:
6012 break;
6015 return 0;
6018 /* Return a node which has the indicated constant VALUE (either 0 or
6019 1), and is of the indicated TYPE. */
6021 tree
6022 constant_boolean_node (int value, tree type)
6024 if (type == integer_type_node)
6025 return value ? integer_one_node : integer_zero_node;
6026 else if (type == boolean_type_node)
6027 return value ? boolean_true_node : boolean_false_node;
6028 else
6029 return build_int_cst (type, value);
6033 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6034 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6035 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6036 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6037 COND is the first argument to CODE; otherwise (as in the example
6038 given here), it is the second argument. TYPE is the type of the
6039 original expression. Return NULL_TREE if no simplification is
6040 possible. */
6042 static tree
6043 fold_binary_op_with_conditional_arg (location_t loc,
6044 enum tree_code code,
6045 tree type, tree op0, tree op1,
6046 tree cond, tree arg, int cond_first_p)
6048 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6049 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6050 tree test, true_value, false_value;
6051 tree lhs = NULL_TREE;
6052 tree rhs = NULL_TREE;
6054 if (TREE_CODE (cond) == COND_EXPR)
6056 test = TREE_OPERAND (cond, 0);
6057 true_value = TREE_OPERAND (cond, 1);
6058 false_value = TREE_OPERAND (cond, 2);
6059 /* If this operand throws an expression, then it does not make
6060 sense to try to perform a logical or arithmetic operation
6061 involving it. */
6062 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6063 lhs = true_value;
6064 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6065 rhs = false_value;
6067 else
6069 tree testtype = TREE_TYPE (cond);
6070 test = cond;
6071 true_value = constant_boolean_node (true, testtype);
6072 false_value = constant_boolean_node (false, testtype);
6075 /* This transformation is only worthwhile if we don't have to wrap ARG
6076 in a SAVE_EXPR and the operation can be simplified on at least one
6077 of the branches once its pushed inside the COND_EXPR. */
6078 if (!TREE_CONSTANT (arg)
6079 && (TREE_SIDE_EFFECTS (arg)
6080 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6081 return NULL_TREE;
6083 arg = fold_convert_loc (loc, arg_type, arg);
6084 if (lhs == 0)
6086 true_value = fold_convert_loc (loc, cond_type, true_value);
6087 if (cond_first_p)
6088 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6089 else
6090 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6092 if (rhs == 0)
6094 false_value = fold_convert_loc (loc, cond_type, false_value);
6095 if (cond_first_p)
6096 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6097 else
6098 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6101 /* Check that we have simplified at least one of the branches. */
6102 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6103 return NULL_TREE;
6105 return fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6109 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6111 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6112 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6113 ADDEND is the same as X.
6115 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6116 and finite. The problematic cases are when X is zero, and its mode
6117 has signed zeros. In the case of rounding towards -infinity,
6118 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6119 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6121 bool
6122 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6124 if (!real_zerop (addend))
6125 return false;
6127 /* Don't allow the fold with -fsignaling-nans. */
6128 if (HONOR_SNANS (TYPE_MODE (type)))
6129 return false;
6131 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6132 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6133 return true;
6135 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6136 if (TREE_CODE (addend) == REAL_CST
6137 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6138 negate = !negate;
6140 /* The mode has signed zeros, and we have to honor their sign.
6141 In this situation, there is only one case we can return true for.
6142 X - 0 is the same as X unless rounding towards -infinity is
6143 supported. */
6144 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6147 /* Subroutine of fold() that checks comparisons of built-in math
6148 functions against real constants.
6150 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6151 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6152 is the type of the result and ARG0 and ARG1 are the operands of the
6153 comparison. ARG1 must be a TREE_REAL_CST.
6155 The function returns the constant folded tree if a simplification
6156 can be made, and NULL_TREE otherwise. */
6158 static tree
6159 fold_mathfn_compare (location_t loc,
6160 enum built_in_function fcode, enum tree_code code,
6161 tree type, tree arg0, tree arg1)
6163 REAL_VALUE_TYPE c;
6165 if (BUILTIN_SQRT_P (fcode))
6167 tree arg = CALL_EXPR_ARG (arg0, 0);
6168 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6170 c = TREE_REAL_CST (arg1);
6171 if (REAL_VALUE_NEGATIVE (c))
6173 /* sqrt(x) < y is always false, if y is negative. */
6174 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6175 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6177 /* sqrt(x) > y is always true, if y is negative and we
6178 don't care about NaNs, i.e. negative values of x. */
6179 if (code == NE_EXPR || !HONOR_NANS (mode))
6180 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6182 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6183 return fold_build2_loc (loc, GE_EXPR, type, arg,
6184 build_real (TREE_TYPE (arg), dconst0));
6186 else if (code == GT_EXPR || code == GE_EXPR)
6188 REAL_VALUE_TYPE c2;
6190 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6191 real_convert (&c2, mode, &c2);
6193 if (REAL_VALUE_ISINF (c2))
6195 /* sqrt(x) > y is x == +Inf, when y is very large. */
6196 if (HONOR_INFINITIES (mode))
6197 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6198 build_real (TREE_TYPE (arg), c2));
6200 /* sqrt(x) > y is always false, when y is very large
6201 and we don't care about infinities. */
6202 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6205 /* sqrt(x) > c is the same as x > c*c. */
6206 return fold_build2_loc (loc, code, type, arg,
6207 build_real (TREE_TYPE (arg), c2));
6209 else if (code == LT_EXPR || code == LE_EXPR)
6211 REAL_VALUE_TYPE c2;
6213 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6214 real_convert (&c2, mode, &c2);
6216 if (REAL_VALUE_ISINF (c2))
6218 /* sqrt(x) < y is always true, when y is a very large
6219 value and we don't care about NaNs or Infinities. */
6220 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6221 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6223 /* sqrt(x) < y is x != +Inf when y is very large and we
6224 don't care about NaNs. */
6225 if (! HONOR_NANS (mode))
6226 return fold_build2_loc (loc, NE_EXPR, type, arg,
6227 build_real (TREE_TYPE (arg), c2));
6229 /* sqrt(x) < y is x >= 0 when y is very large and we
6230 don't care about Infinities. */
6231 if (! HONOR_INFINITIES (mode))
6232 return fold_build2_loc (loc, GE_EXPR, type, arg,
6233 build_real (TREE_TYPE (arg), dconst0));
6235 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6236 if (lang_hooks.decls.global_bindings_p () != 0
6237 || CONTAINS_PLACEHOLDER_P (arg))
6238 return NULL_TREE;
6240 arg = save_expr (arg);
6241 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6242 fold_build2_loc (loc, GE_EXPR, type, arg,
6243 build_real (TREE_TYPE (arg),
6244 dconst0)),
6245 fold_build2_loc (loc, NE_EXPR, type, arg,
6246 build_real (TREE_TYPE (arg),
6247 c2)));
6250 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6251 if (! HONOR_NANS (mode))
6252 return fold_build2_loc (loc, code, type, arg,
6253 build_real (TREE_TYPE (arg), c2));
6255 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6256 if (lang_hooks.decls.global_bindings_p () == 0
6257 && ! CONTAINS_PLACEHOLDER_P (arg))
6259 arg = save_expr (arg);
6260 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6261 fold_build2_loc (loc, GE_EXPR, type, arg,
6262 build_real (TREE_TYPE (arg),
6263 dconst0)),
6264 fold_build2_loc (loc, code, type, arg,
6265 build_real (TREE_TYPE (arg),
6266 c2)));
6271 return NULL_TREE;
6274 /* Subroutine of fold() that optimizes comparisons against Infinities,
6275 either +Inf or -Inf.
6277 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6278 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6279 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6281 The function returns the constant folded tree if a simplification
6282 can be made, and NULL_TREE otherwise. */
6284 static tree
6285 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6286 tree arg0, tree arg1)
6288 enum machine_mode mode;
6289 REAL_VALUE_TYPE max;
6290 tree temp;
6291 bool neg;
6293 mode = TYPE_MODE (TREE_TYPE (arg0));
6295 /* For negative infinity swap the sense of the comparison. */
6296 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6297 if (neg)
6298 code = swap_tree_comparison (code);
6300 switch (code)
6302 case GT_EXPR:
6303 /* x > +Inf is always false, if with ignore sNANs. */
6304 if (HONOR_SNANS (mode))
6305 return NULL_TREE;
6306 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6308 case LE_EXPR:
6309 /* x <= +Inf is always true, if we don't case about NaNs. */
6310 if (! HONOR_NANS (mode))
6311 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6313 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6314 if (lang_hooks.decls.global_bindings_p () == 0
6315 && ! CONTAINS_PLACEHOLDER_P (arg0))
6317 arg0 = save_expr (arg0);
6318 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6320 break;
6322 case EQ_EXPR:
6323 case GE_EXPR:
6324 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6325 real_maxval (&max, neg, mode);
6326 return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6327 arg0, build_real (TREE_TYPE (arg0), max));
6329 case LT_EXPR:
6330 /* x < +Inf is always equal to x <= DBL_MAX. */
6331 real_maxval (&max, neg, mode);
6332 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6333 arg0, build_real (TREE_TYPE (arg0), max));
6335 case NE_EXPR:
6336 /* x != +Inf is always equal to !(x > DBL_MAX). */
6337 real_maxval (&max, neg, mode);
6338 if (! HONOR_NANS (mode))
6339 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6340 arg0, build_real (TREE_TYPE (arg0), max));
6342 temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6343 arg0, build_real (TREE_TYPE (arg0), max));
6344 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6346 default:
6347 break;
6350 return NULL_TREE;
6353 /* Subroutine of fold() that optimizes comparisons of a division by
6354 a nonzero integer constant against an integer constant, i.e.
6355 X/C1 op C2.
6357 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6358 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6359 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6361 The function returns the constant folded tree if a simplification
6362 can be made, and NULL_TREE otherwise. */
6364 static tree
6365 fold_div_compare (location_t loc,
6366 enum tree_code code, tree type, tree arg0, tree arg1)
6368 tree prod, tmp, hi, lo;
6369 tree arg00 = TREE_OPERAND (arg0, 0);
6370 tree arg01 = TREE_OPERAND (arg0, 1);
6371 unsigned HOST_WIDE_INT lpart;
6372 HOST_WIDE_INT hpart;
6373 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6374 bool neg_overflow;
6375 int overflow;
6377 /* We have to do this the hard way to detect unsigned overflow.
6378 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
6379 overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6380 TREE_INT_CST_HIGH (arg01),
6381 TREE_INT_CST_LOW (arg1),
6382 TREE_INT_CST_HIGH (arg1),
6383 &lpart, &hpart, unsigned_p);
6384 prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6385 -1, overflow);
6386 neg_overflow = false;
6388 if (unsigned_p)
6390 tmp = int_const_binop (MINUS_EXPR, arg01,
6391 build_int_cst (TREE_TYPE (arg01), 1), 0);
6392 lo = prod;
6394 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
6395 overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6396 TREE_INT_CST_HIGH (prod),
6397 TREE_INT_CST_LOW (tmp),
6398 TREE_INT_CST_HIGH (tmp),
6399 &lpart, &hpart, unsigned_p);
6400 hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6401 -1, overflow | TREE_OVERFLOW (prod));
6403 else if (tree_int_cst_sgn (arg01) >= 0)
6405 tmp = int_const_binop (MINUS_EXPR, arg01,
6406 build_int_cst (TREE_TYPE (arg01), 1), 0);
6407 switch (tree_int_cst_sgn (arg1))
6409 case -1:
6410 neg_overflow = true;
6411 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6412 hi = prod;
6413 break;
6415 case 0:
6416 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6417 hi = tmp;
6418 break;
6420 case 1:
6421 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6422 lo = prod;
6423 break;
6425 default:
6426 gcc_unreachable ();
6429 else
6431 /* A negative divisor reverses the relational operators. */
6432 code = swap_tree_comparison (code);
6434 tmp = int_const_binop (PLUS_EXPR, arg01,
6435 build_int_cst (TREE_TYPE (arg01), 1), 0);
6436 switch (tree_int_cst_sgn (arg1))
6438 case -1:
6439 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6440 lo = prod;
6441 break;
6443 case 0:
6444 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6445 lo = tmp;
6446 break;
6448 case 1:
6449 neg_overflow = true;
6450 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6451 hi = prod;
6452 break;
6454 default:
6455 gcc_unreachable ();
6459 switch (code)
6461 case EQ_EXPR:
6462 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6463 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6464 if (TREE_OVERFLOW (hi))
6465 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6466 if (TREE_OVERFLOW (lo))
6467 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6468 return build_range_check (loc, type, arg00, 1, lo, hi);
6470 case NE_EXPR:
6471 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6472 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6473 if (TREE_OVERFLOW (hi))
6474 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6475 if (TREE_OVERFLOW (lo))
6476 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6477 return build_range_check (loc, type, arg00, 0, lo, hi);
6479 case LT_EXPR:
6480 if (TREE_OVERFLOW (lo))
6482 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6483 return omit_one_operand_loc (loc, type, tmp, arg00);
6485 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6487 case LE_EXPR:
6488 if (TREE_OVERFLOW (hi))
6490 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6491 return omit_one_operand_loc (loc, type, tmp, arg00);
6493 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6495 case GT_EXPR:
6496 if (TREE_OVERFLOW (hi))
6498 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6499 return omit_one_operand_loc (loc, type, tmp, arg00);
6501 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6503 case GE_EXPR:
6504 if (TREE_OVERFLOW (lo))
6506 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6507 return omit_one_operand_loc (loc, type, tmp, arg00);
6509 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6511 default:
6512 break;
6515 return NULL_TREE;
6519 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6520 equality/inequality test, then return a simplified form of the test
6521 using a sign testing. Otherwise return NULL. TYPE is the desired
6522 result type. */
6524 static tree
6525 fold_single_bit_test_into_sign_test (location_t loc,
6526 enum tree_code code, tree arg0, tree arg1,
6527 tree result_type)
6529 /* If this is testing a single bit, we can optimize the test. */
6530 if ((code == NE_EXPR || code == EQ_EXPR)
6531 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6532 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6534 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6535 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6536 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6538 if (arg00 != NULL_TREE
6539 /* This is only a win if casting to a signed type is cheap,
6540 i.e. when arg00's type is not a partial mode. */
6541 && TYPE_PRECISION (TREE_TYPE (arg00))
6542 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6544 tree stype = signed_type_for (TREE_TYPE (arg00));
6545 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6546 result_type,
6547 fold_convert_loc (loc, stype, arg00),
6548 build_int_cst (stype, 0));
6552 return NULL_TREE;
6555 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6556 equality/inequality test, then return a simplified form of
6557 the test using shifts and logical operations. Otherwise return
6558 NULL. TYPE is the desired result type. */
6560 tree
6561 fold_single_bit_test (location_t loc, enum tree_code code,
6562 tree arg0, tree arg1, tree result_type)
6564 /* If this is testing a single bit, we can optimize the test. */
6565 if ((code == NE_EXPR || code == EQ_EXPR)
6566 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6567 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6569 tree inner = TREE_OPERAND (arg0, 0);
6570 tree type = TREE_TYPE (arg0);
6571 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6572 enum machine_mode operand_mode = TYPE_MODE (type);
6573 int ops_unsigned;
6574 tree signed_type, unsigned_type, intermediate_type;
6575 tree tem, one;
6577 /* First, see if we can fold the single bit test into a sign-bit
6578 test. */
6579 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6580 result_type);
6581 if (tem)
6582 return tem;
6584 /* Otherwise we have (A & C) != 0 where C is a single bit,
6585 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6586 Similarly for (A & C) == 0. */
6588 /* If INNER is a right shift of a constant and it plus BITNUM does
6589 not overflow, adjust BITNUM and INNER. */
6590 if (TREE_CODE (inner) == RSHIFT_EXPR
6591 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6592 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6593 && bitnum < TYPE_PRECISION (type)
6594 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6595 bitnum - TYPE_PRECISION (type)))
6597 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6598 inner = TREE_OPERAND (inner, 0);
6601 /* If we are going to be able to omit the AND below, we must do our
6602 operations as unsigned. If we must use the AND, we have a choice.
6603 Normally unsigned is faster, but for some machines signed is. */
6604 #ifdef LOAD_EXTEND_OP
6605 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6606 && !flag_syntax_only) ? 0 : 1;
6607 #else
6608 ops_unsigned = 1;
6609 #endif
6611 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6612 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6613 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6614 inner = fold_convert_loc (loc, intermediate_type, inner);
6616 if (bitnum != 0)
6617 inner = build2 (RSHIFT_EXPR, intermediate_type,
6618 inner, size_int (bitnum));
6620 one = build_int_cst (intermediate_type, 1);
6622 if (code == EQ_EXPR)
6623 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6625 /* Put the AND last so it can combine with more things. */
6626 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6628 /* Make sure to return the proper type. */
6629 inner = fold_convert_loc (loc, result_type, inner);
6631 return inner;
6633 return NULL_TREE;
6636 /* Check whether we are allowed to reorder operands arg0 and arg1,
6637 such that the evaluation of arg1 occurs before arg0. */
6639 static bool
6640 reorder_operands_p (const_tree arg0, const_tree arg1)
6642 if (! flag_evaluation_order)
6643 return true;
6644 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6645 return true;
6646 return ! TREE_SIDE_EFFECTS (arg0)
6647 && ! TREE_SIDE_EFFECTS (arg1);
6650 /* Test whether it is preferable two swap two operands, ARG0 and
6651 ARG1, for example because ARG0 is an integer constant and ARG1
6652 isn't. If REORDER is true, only recommend swapping if we can
6653 evaluate the operands in reverse order. */
6655 bool
6656 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6658 STRIP_SIGN_NOPS (arg0);
6659 STRIP_SIGN_NOPS (arg1);
6661 if (TREE_CODE (arg1) == INTEGER_CST)
6662 return 0;
6663 if (TREE_CODE (arg0) == INTEGER_CST)
6664 return 1;
6666 if (TREE_CODE (arg1) == REAL_CST)
6667 return 0;
6668 if (TREE_CODE (arg0) == REAL_CST)
6669 return 1;
6671 if (TREE_CODE (arg1) == FIXED_CST)
6672 return 0;
6673 if (TREE_CODE (arg0) == FIXED_CST)
6674 return 1;
6676 if (TREE_CODE (arg1) == COMPLEX_CST)
6677 return 0;
6678 if (TREE_CODE (arg0) == COMPLEX_CST)
6679 return 1;
6681 if (TREE_CONSTANT (arg1))
6682 return 0;
6683 if (TREE_CONSTANT (arg0))
6684 return 1;
6686 if (optimize_function_for_size_p (cfun))
6687 return 0;
6689 if (reorder && flag_evaluation_order
6690 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6691 return 0;
6693 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6694 for commutative and comparison operators. Ensuring a canonical
6695 form allows the optimizers to find additional redundancies without
6696 having to explicitly check for both orderings. */
6697 if (TREE_CODE (arg0) == SSA_NAME
6698 && TREE_CODE (arg1) == SSA_NAME
6699 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6700 return 1;
6702 /* Put SSA_NAMEs last. */
6703 if (TREE_CODE (arg1) == SSA_NAME)
6704 return 0;
6705 if (TREE_CODE (arg0) == SSA_NAME)
6706 return 1;
6708 /* Put variables last. */
6709 if (DECL_P (arg1))
6710 return 0;
6711 if (DECL_P (arg0))
6712 return 1;
6714 return 0;
6717 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6718 ARG0 is extended to a wider type. */
6720 static tree
6721 fold_widened_comparison (location_t loc, enum tree_code code,
6722 tree type, tree arg0, tree arg1)
6724 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6725 tree arg1_unw;
6726 tree shorter_type, outer_type;
6727 tree min, max;
6728 bool above, below;
6730 if (arg0_unw == arg0)
6731 return NULL_TREE;
6732 shorter_type = TREE_TYPE (arg0_unw);
6734 #ifdef HAVE_canonicalize_funcptr_for_compare
6735 /* Disable this optimization if we're casting a function pointer
6736 type on targets that require function pointer canonicalization. */
6737 if (HAVE_canonicalize_funcptr_for_compare
6738 && TREE_CODE (shorter_type) == POINTER_TYPE
6739 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6740 return NULL_TREE;
6741 #endif
6743 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6744 return NULL_TREE;
6746 arg1_unw = get_unwidened (arg1, NULL_TREE);
6748 /* If possible, express the comparison in the shorter mode. */
6749 if ((code == EQ_EXPR || code == NE_EXPR
6750 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6751 && (TREE_TYPE (arg1_unw) == shorter_type
6752 || ((TYPE_PRECISION (shorter_type)
6753 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6754 && (TYPE_UNSIGNED (shorter_type)
6755 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6756 || (TREE_CODE (arg1_unw) == INTEGER_CST
6757 && (TREE_CODE (shorter_type) == INTEGER_TYPE
6758 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6759 && int_fits_type_p (arg1_unw, shorter_type))))
6760 return fold_build2_loc (loc, code, type, arg0_unw,
6761 fold_convert_loc (loc, shorter_type, arg1_unw));
6763 if (TREE_CODE (arg1_unw) != INTEGER_CST
6764 || TREE_CODE (shorter_type) != INTEGER_TYPE
6765 || !int_fits_type_p (arg1_unw, shorter_type))
6766 return NULL_TREE;
6768 /* If we are comparing with the integer that does not fit into the range
6769 of the shorter type, the result is known. */
6770 outer_type = TREE_TYPE (arg1_unw);
6771 min = lower_bound_in_type (outer_type, shorter_type);
6772 max = upper_bound_in_type (outer_type, shorter_type);
6774 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6775 max, arg1_unw));
6776 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6777 arg1_unw, min));
6779 switch (code)
6781 case EQ_EXPR:
6782 if (above || below)
6783 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6784 break;
6786 case NE_EXPR:
6787 if (above || below)
6788 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6789 break;
6791 case LT_EXPR:
6792 case LE_EXPR:
6793 if (above)
6794 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6795 else if (below)
6796 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6798 case GT_EXPR:
6799 case GE_EXPR:
6800 if (above)
6801 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6802 else if (below)
6803 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6805 default:
6806 break;
6809 return NULL_TREE;
6812 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6813 ARG0 just the signedness is changed. */
6815 static tree
6816 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6817 tree arg0, tree arg1)
6819 tree arg0_inner;
6820 tree inner_type, outer_type;
6822 if (!CONVERT_EXPR_P (arg0))
6823 return NULL_TREE;
6825 outer_type = TREE_TYPE (arg0);
6826 arg0_inner = TREE_OPERAND (arg0, 0);
6827 inner_type = TREE_TYPE (arg0_inner);
6829 #ifdef HAVE_canonicalize_funcptr_for_compare
6830 /* Disable this optimization if we're casting a function pointer
6831 type on targets that require function pointer canonicalization. */
6832 if (HAVE_canonicalize_funcptr_for_compare
6833 && TREE_CODE (inner_type) == POINTER_TYPE
6834 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6835 return NULL_TREE;
6836 #endif
6838 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6839 return NULL_TREE;
6841 if (TREE_CODE (arg1) != INTEGER_CST
6842 && !(CONVERT_EXPR_P (arg1)
6843 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6844 return NULL_TREE;
6846 if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6847 || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6848 && code != NE_EXPR
6849 && code != EQ_EXPR)
6850 return NULL_TREE;
6852 if (TREE_CODE (arg1) == INTEGER_CST)
6853 arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
6854 TREE_INT_CST_HIGH (arg1), 0,
6855 TREE_OVERFLOW (arg1));
6856 else
6857 arg1 = fold_convert_loc (loc, inner_type, arg1);
6859 return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6862 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6863 step of the array. Reconstructs s and delta in the case of s *
6864 delta being an integer constant (and thus already folded). ADDR is
6865 the address. MULT is the multiplicative expression. If the
6866 function succeeds, the new address expression is returned.
6867 Otherwise NULL_TREE is returned. LOC is the location of the
6868 resulting expression. */
6870 static tree
6871 try_move_mult_to_index (location_t loc, tree addr, tree op1)
6873 tree s, delta, step;
6874 tree ref = TREE_OPERAND (addr, 0), pref;
6875 tree ret, pos;
6876 tree itype;
6877 bool mdim = false;
6879 /* Strip the nops that might be added when converting op1 to sizetype. */
6880 STRIP_NOPS (op1);
6882 /* Canonicalize op1 into a possibly non-constant delta
6883 and an INTEGER_CST s. */
6884 if (TREE_CODE (op1) == MULT_EXPR)
6886 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6888 STRIP_NOPS (arg0);
6889 STRIP_NOPS (arg1);
6891 if (TREE_CODE (arg0) == INTEGER_CST)
6893 s = arg0;
6894 delta = arg1;
6896 else if (TREE_CODE (arg1) == INTEGER_CST)
6898 s = arg1;
6899 delta = arg0;
6901 else
6902 return NULL_TREE;
6904 else if (TREE_CODE (op1) == INTEGER_CST)
6906 delta = op1;
6907 s = NULL_TREE;
6909 else
6911 /* Simulate we are delta * 1. */
6912 delta = op1;
6913 s = integer_one_node;
6916 for (;; ref = TREE_OPERAND (ref, 0))
6918 if (TREE_CODE (ref) == ARRAY_REF)
6920 tree domain;
6922 /* Remember if this was a multi-dimensional array. */
6923 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6924 mdim = true;
6926 domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6927 if (! domain)
6928 continue;
6929 itype = TREE_TYPE (domain);
6931 step = array_ref_element_size (ref);
6932 if (TREE_CODE (step) != INTEGER_CST)
6933 continue;
6935 if (s)
6937 if (! tree_int_cst_equal (step, s))
6938 continue;
6940 else
6942 /* Try if delta is a multiple of step. */
6943 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6944 if (! tmp)
6945 continue;
6946 delta = tmp;
6949 /* Only fold here if we can verify we do not overflow one
6950 dimension of a multi-dimensional array. */
6951 if (mdim)
6953 tree tmp;
6955 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6956 || !TYPE_MAX_VALUE (domain)
6957 || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6958 continue;
6960 tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6961 fold_convert_loc (loc, itype,
6962 TREE_OPERAND (ref, 1)),
6963 fold_convert_loc (loc, itype, delta));
6964 if (!tmp
6965 || TREE_CODE (tmp) != INTEGER_CST
6966 || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6967 continue;
6970 break;
6972 else
6973 mdim = false;
6975 if (!handled_component_p (ref))
6976 return NULL_TREE;
6979 /* We found the suitable array reference. So copy everything up to it,
6980 and replace the index. */
6982 pref = TREE_OPERAND (addr, 0);
6983 ret = copy_node (pref);
6984 SET_EXPR_LOCATION (ret, loc);
6985 pos = ret;
6987 while (pref != ref)
6989 pref = TREE_OPERAND (pref, 0);
6990 TREE_OPERAND (pos, 0) = copy_node (pref);
6991 pos = TREE_OPERAND (pos, 0);
6994 TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
6995 fold_convert_loc (loc, itype,
6996 TREE_OPERAND (pos, 1)),
6997 fold_convert_loc (loc, itype, delta));
6999 return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
7003 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7004 means A >= Y && A != MAX, but in this case we know that
7005 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7007 static tree
7008 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7010 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7012 if (TREE_CODE (bound) == LT_EXPR)
7013 a = TREE_OPERAND (bound, 0);
7014 else if (TREE_CODE (bound) == GT_EXPR)
7015 a = TREE_OPERAND (bound, 1);
7016 else
7017 return NULL_TREE;
7019 typea = TREE_TYPE (a);
7020 if (!INTEGRAL_TYPE_P (typea)
7021 && !POINTER_TYPE_P (typea))
7022 return NULL_TREE;
7024 if (TREE_CODE (ineq) == LT_EXPR)
7026 a1 = TREE_OPERAND (ineq, 1);
7027 y = TREE_OPERAND (ineq, 0);
7029 else if (TREE_CODE (ineq) == GT_EXPR)
7031 a1 = TREE_OPERAND (ineq, 0);
7032 y = TREE_OPERAND (ineq, 1);
7034 else
7035 return NULL_TREE;
7037 if (TREE_TYPE (a1) != typea)
7038 return NULL_TREE;
7040 if (POINTER_TYPE_P (typea))
7042 /* Convert the pointer types into integer before taking the difference. */
7043 tree ta = fold_convert_loc (loc, ssizetype, a);
7044 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7045 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7047 else
7048 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7050 if (!diff || !integer_onep (diff))
7051 return NULL_TREE;
7053 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7056 /* Fold a sum or difference of at least one multiplication.
7057 Returns the folded tree or NULL if no simplification could be made. */
7059 static tree
7060 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7061 tree arg0, tree arg1)
7063 tree arg00, arg01, arg10, arg11;
7064 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7066 /* (A * C) +- (B * C) -> (A+-B) * C.
7067 (A * C) +- A -> A * (C+-1).
7068 We are most concerned about the case where C is a constant,
7069 but other combinations show up during loop reduction. Since
7070 it is not difficult, try all four possibilities. */
7072 if (TREE_CODE (arg0) == MULT_EXPR)
7074 arg00 = TREE_OPERAND (arg0, 0);
7075 arg01 = TREE_OPERAND (arg0, 1);
7077 else if (TREE_CODE (arg0) == INTEGER_CST)
7079 arg00 = build_one_cst (type);
7080 arg01 = arg0;
7082 else
7084 /* We cannot generate constant 1 for fract. */
7085 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7086 return NULL_TREE;
7087 arg00 = arg0;
7088 arg01 = build_one_cst (type);
7090 if (TREE_CODE (arg1) == MULT_EXPR)
7092 arg10 = TREE_OPERAND (arg1, 0);
7093 arg11 = TREE_OPERAND (arg1, 1);
7095 else if (TREE_CODE (arg1) == INTEGER_CST)
7097 arg10 = build_one_cst (type);
7098 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7099 the purpose of this canonicalization. */
7100 if (TREE_INT_CST_HIGH (arg1) == -1
7101 && negate_expr_p (arg1)
7102 && code == PLUS_EXPR)
7104 arg11 = negate_expr (arg1);
7105 code = MINUS_EXPR;
7107 else
7108 arg11 = arg1;
7110 else
7112 /* We cannot generate constant 1 for fract. */
7113 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7114 return NULL_TREE;
7115 arg10 = arg1;
7116 arg11 = build_one_cst (type);
7118 same = NULL_TREE;
7120 if (operand_equal_p (arg01, arg11, 0))
7121 same = arg01, alt0 = arg00, alt1 = arg10;
7122 else if (operand_equal_p (arg00, arg10, 0))
7123 same = arg00, alt0 = arg01, alt1 = arg11;
7124 else if (operand_equal_p (arg00, arg11, 0))
7125 same = arg00, alt0 = arg01, alt1 = arg10;
7126 else if (operand_equal_p (arg01, arg10, 0))
7127 same = arg01, alt0 = arg00, alt1 = arg11;
7129 /* No identical multiplicands; see if we can find a common
7130 power-of-two factor in non-power-of-two multiplies. This
7131 can help in multi-dimensional array access. */
7132 else if (host_integerp (arg01, 0)
7133 && host_integerp (arg11, 0))
7135 HOST_WIDE_INT int01, int11, tmp;
7136 bool swap = false;
7137 tree maybe_same;
7138 int01 = TREE_INT_CST_LOW (arg01);
7139 int11 = TREE_INT_CST_LOW (arg11);
7141 /* Move min of absolute values to int11. */
7142 if ((int01 >= 0 ? int01 : -int01)
7143 < (int11 >= 0 ? int11 : -int11))
7145 tmp = int01, int01 = int11, int11 = tmp;
7146 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7147 maybe_same = arg01;
7148 swap = true;
7150 else
7151 maybe_same = arg11;
7153 if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7154 /* The remainder should not be a constant, otherwise we
7155 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7156 increased the number of multiplications necessary. */
7157 && TREE_CODE (arg10) != INTEGER_CST)
7159 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7160 build_int_cst (TREE_TYPE (arg00),
7161 int01 / int11));
7162 alt1 = arg10;
7163 same = maybe_same;
7164 if (swap)
7165 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7169 if (same)
7170 return fold_build2_loc (loc, MULT_EXPR, type,
7171 fold_build2_loc (loc, code, type,
7172 fold_convert_loc (loc, type, alt0),
7173 fold_convert_loc (loc, type, alt1)),
7174 fold_convert_loc (loc, type, same));
7176 return NULL_TREE;
7179 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7180 specified by EXPR into the buffer PTR of length LEN bytes.
7181 Return the number of bytes placed in the buffer, or zero
7182 upon failure. */
7184 static int
7185 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7187 tree type = TREE_TYPE (expr);
7188 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7189 int byte, offset, word, words;
7190 unsigned char value;
7192 if (total_bytes > len)
7193 return 0;
7194 words = total_bytes / UNITS_PER_WORD;
7196 for (byte = 0; byte < total_bytes; byte++)
7198 int bitpos = byte * BITS_PER_UNIT;
7199 if (bitpos < HOST_BITS_PER_WIDE_INT)
7200 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7201 else
7202 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7203 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7205 if (total_bytes > UNITS_PER_WORD)
7207 word = byte / UNITS_PER_WORD;
7208 if (WORDS_BIG_ENDIAN)
7209 word = (words - 1) - word;
7210 offset = word * UNITS_PER_WORD;
7211 if (BYTES_BIG_ENDIAN)
7212 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7213 else
7214 offset += byte % UNITS_PER_WORD;
7216 else
7217 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7218 ptr[offset] = value;
7220 return total_bytes;
7224 /* Subroutine of native_encode_expr. Encode the REAL_CST
7225 specified by EXPR into the buffer PTR of length LEN bytes.
7226 Return the number of bytes placed in the buffer, or zero
7227 upon failure. */
7229 static int
7230 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7232 tree type = TREE_TYPE (expr);
7233 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7234 int byte, offset, word, words, bitpos;
7235 unsigned char value;
7237 /* There are always 32 bits in each long, no matter the size of
7238 the hosts long. We handle floating point representations with
7239 up to 192 bits. */
7240 long tmp[6];
7242 if (total_bytes > len)
7243 return 0;
7244 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7246 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7248 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7249 bitpos += BITS_PER_UNIT)
7251 byte = (bitpos / BITS_PER_UNIT) & 3;
7252 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7254 if (UNITS_PER_WORD < 4)
7256 word = byte / UNITS_PER_WORD;
7257 if (WORDS_BIG_ENDIAN)
7258 word = (words - 1) - word;
7259 offset = word * UNITS_PER_WORD;
7260 if (BYTES_BIG_ENDIAN)
7261 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7262 else
7263 offset += byte % UNITS_PER_WORD;
7265 else
7266 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7267 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7269 return total_bytes;
7272 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7273 specified by EXPR into the buffer PTR of length LEN bytes.
7274 Return the number of bytes placed in the buffer, or zero
7275 upon failure. */
7277 static int
7278 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7280 int rsize, isize;
7281 tree part;
7283 part = TREE_REALPART (expr);
7284 rsize = native_encode_expr (part, ptr, len);
7285 if (rsize == 0)
7286 return 0;
7287 part = TREE_IMAGPART (expr);
7288 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7289 if (isize != rsize)
7290 return 0;
7291 return rsize + isize;
7295 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7296 specified by EXPR into the buffer PTR of length LEN bytes.
7297 Return the number of bytes placed in the buffer, or zero
7298 upon failure. */
7300 static int
7301 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7303 int i, size, offset, count;
7304 tree itype, elem, elements;
7306 offset = 0;
7307 elements = TREE_VECTOR_CST_ELTS (expr);
7308 count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7309 itype = TREE_TYPE (TREE_TYPE (expr));
7310 size = GET_MODE_SIZE (TYPE_MODE (itype));
7311 for (i = 0; i < count; i++)
7313 if (elements)
7315 elem = TREE_VALUE (elements);
7316 elements = TREE_CHAIN (elements);
7318 else
7319 elem = NULL_TREE;
7321 if (elem)
7323 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7324 return 0;
7326 else
7328 if (offset + size > len)
7329 return 0;
7330 memset (ptr+offset, 0, size);
7332 offset += size;
7334 return offset;
7338 /* Subroutine of native_encode_expr. Encode the STRING_CST
7339 specified by EXPR into the buffer PTR of length LEN bytes.
7340 Return the number of bytes placed in the buffer, or zero
7341 upon failure. */
7343 static int
7344 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7346 tree type = TREE_TYPE (expr);
7347 HOST_WIDE_INT total_bytes;
7349 if (TREE_CODE (type) != ARRAY_TYPE
7350 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7351 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7352 || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7353 return 0;
7354 total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7355 if (total_bytes > len)
7356 return 0;
7357 if (TREE_STRING_LENGTH (expr) < total_bytes)
7359 memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7360 memset (ptr + TREE_STRING_LENGTH (expr), 0,
7361 total_bytes - TREE_STRING_LENGTH (expr));
7363 else
7364 memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7365 return total_bytes;
7369 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7370 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7371 buffer PTR of length LEN bytes. Return the number of bytes
7372 placed in the buffer, or zero upon failure. */
7375 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7377 switch (TREE_CODE (expr))
7379 case INTEGER_CST:
7380 return native_encode_int (expr, ptr, len);
7382 case REAL_CST:
7383 return native_encode_real (expr, ptr, len);
7385 case COMPLEX_CST:
7386 return native_encode_complex (expr, ptr, len);
7388 case VECTOR_CST:
7389 return native_encode_vector (expr, ptr, len);
7391 case STRING_CST:
7392 return native_encode_string (expr, ptr, len);
7394 default:
7395 return 0;
7400 /* Subroutine of native_interpret_expr. Interpret the contents of
7401 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7402 If the buffer cannot be interpreted, return NULL_TREE. */
7404 static tree
7405 native_interpret_int (tree type, const unsigned char *ptr, int len)
7407 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7408 int byte, offset, word, words;
7409 unsigned char value;
7410 double_int result;
7412 if (total_bytes > len)
7413 return NULL_TREE;
7414 if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7415 return NULL_TREE;
7417 result = double_int_zero;
7418 words = total_bytes / UNITS_PER_WORD;
7420 for (byte = 0; byte < total_bytes; byte++)
7422 int bitpos = byte * BITS_PER_UNIT;
7423 if (total_bytes > UNITS_PER_WORD)
7425 word = byte / UNITS_PER_WORD;
7426 if (WORDS_BIG_ENDIAN)
7427 word = (words - 1) - word;
7428 offset = word * UNITS_PER_WORD;
7429 if (BYTES_BIG_ENDIAN)
7430 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7431 else
7432 offset += byte % UNITS_PER_WORD;
7434 else
7435 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7436 value = ptr[offset];
7438 if (bitpos < HOST_BITS_PER_WIDE_INT)
7439 result.low |= (unsigned HOST_WIDE_INT) value << bitpos;
7440 else
7441 result.high |= (unsigned HOST_WIDE_INT) value
7442 << (bitpos - HOST_BITS_PER_WIDE_INT);
7445 return double_int_to_tree (type, result);
7449 /* Subroutine of native_interpret_expr. Interpret the contents of
7450 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7451 If the buffer cannot be interpreted, return NULL_TREE. */
7453 static tree
7454 native_interpret_real (tree type, const unsigned char *ptr, int len)
7456 enum machine_mode mode = TYPE_MODE (type);
7457 int total_bytes = GET_MODE_SIZE (mode);
7458 int byte, offset, word, words, bitpos;
7459 unsigned char value;
7460 /* There are always 32 bits in each long, no matter the size of
7461 the hosts long. We handle floating point representations with
7462 up to 192 bits. */
7463 REAL_VALUE_TYPE r;
7464 long tmp[6];
7466 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7467 if (total_bytes > len || total_bytes > 24)
7468 return NULL_TREE;
7469 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7471 memset (tmp, 0, sizeof (tmp));
7472 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7473 bitpos += BITS_PER_UNIT)
7475 byte = (bitpos / BITS_PER_UNIT) & 3;
7476 if (UNITS_PER_WORD < 4)
7478 word = byte / UNITS_PER_WORD;
7479 if (WORDS_BIG_ENDIAN)
7480 word = (words - 1) - word;
7481 offset = word * UNITS_PER_WORD;
7482 if (BYTES_BIG_ENDIAN)
7483 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7484 else
7485 offset += byte % UNITS_PER_WORD;
7487 else
7488 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7489 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7491 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7494 real_from_target (&r, tmp, mode);
7495 return build_real (type, r);
7499 /* Subroutine of native_interpret_expr. Interpret the contents of
7500 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7501 If the buffer cannot be interpreted, return NULL_TREE. */
7503 static tree
7504 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7506 tree etype, rpart, ipart;
7507 int size;
7509 etype = TREE_TYPE (type);
7510 size = GET_MODE_SIZE (TYPE_MODE (etype));
7511 if (size * 2 > len)
7512 return NULL_TREE;
7513 rpart = native_interpret_expr (etype, ptr, size);
7514 if (!rpart)
7515 return NULL_TREE;
7516 ipart = native_interpret_expr (etype, ptr+size, size);
7517 if (!ipart)
7518 return NULL_TREE;
7519 return build_complex (type, rpart, ipart);
7523 /* Subroutine of native_interpret_expr. Interpret the contents of
7524 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7525 If the buffer cannot be interpreted, return NULL_TREE. */
7527 static tree
7528 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7530 tree etype, elem, elements;
7531 int i, size, count;
7533 etype = TREE_TYPE (type);
7534 size = GET_MODE_SIZE (TYPE_MODE (etype));
7535 count = TYPE_VECTOR_SUBPARTS (type);
7536 if (size * count > len)
7537 return NULL_TREE;
7539 elements = NULL_TREE;
7540 for (i = count - 1; i >= 0; i--)
7542 elem = native_interpret_expr (etype, ptr+(i*size), size);
7543 if (!elem)
7544 return NULL_TREE;
7545 elements = tree_cons (NULL_TREE, elem, elements);
7547 return build_vector (type, elements);
7551 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7552 the buffer PTR of length LEN as a constant of type TYPE. For
7553 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7554 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7555 return NULL_TREE. */
7557 tree
7558 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7560 switch (TREE_CODE (type))
7562 case INTEGER_TYPE:
7563 case ENUMERAL_TYPE:
7564 case BOOLEAN_TYPE:
7565 return native_interpret_int (type, ptr, len);
7567 case REAL_TYPE:
7568 return native_interpret_real (type, ptr, len);
7570 case COMPLEX_TYPE:
7571 return native_interpret_complex (type, ptr, len);
7573 case VECTOR_TYPE:
7574 return native_interpret_vector (type, ptr, len);
7576 default:
7577 return NULL_TREE;
7582 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7583 TYPE at compile-time. If we're unable to perform the conversion
7584 return NULL_TREE. */
7586 static tree
7587 fold_view_convert_expr (tree type, tree expr)
7589 /* We support up to 512-bit values (for V8DFmode). */
7590 unsigned char buffer[64];
7591 int len;
7593 /* Check that the host and target are sane. */
7594 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7595 return NULL_TREE;
7597 len = native_encode_expr (expr, buffer, sizeof (buffer));
7598 if (len == 0)
7599 return NULL_TREE;
7601 return native_interpret_expr (type, buffer, len);
7604 /* Build an expression for the address of T. Folds away INDIRECT_REF
7605 to avoid confusing the gimplify process. */
7607 tree
7608 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7610 /* The size of the object is not relevant when talking about its address. */
7611 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7612 t = TREE_OPERAND (t, 0);
7614 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7615 if (TREE_CODE (t) == INDIRECT_REF
7616 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7618 t = TREE_OPERAND (t, 0);
7620 if (TREE_TYPE (t) != ptrtype)
7622 t = build1 (NOP_EXPR, ptrtype, t);
7623 SET_EXPR_LOCATION (t, loc);
7626 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7628 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7630 if (TREE_TYPE (t) != ptrtype)
7631 t = fold_convert_loc (loc, ptrtype, t);
7633 else
7635 t = build1 (ADDR_EXPR, ptrtype, t);
7636 SET_EXPR_LOCATION (t, loc);
7639 return t;
7642 /* Build an expression for the address of T. */
7644 tree
7645 build_fold_addr_expr_loc (location_t loc, tree t)
7647 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7649 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7652 /* Fold a unary expression of code CODE and type TYPE with operand
7653 OP0. Return the folded expression if folding is successful.
7654 Otherwise, return NULL_TREE. */
7656 tree
7657 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7659 tree tem;
7660 tree arg0;
7661 enum tree_code_class kind = TREE_CODE_CLASS (code);
7663 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7664 && TREE_CODE_LENGTH (code) == 1);
7666 arg0 = op0;
7667 if (arg0)
7669 if (CONVERT_EXPR_CODE_P (code)
7670 || code == FLOAT_EXPR || code == ABS_EXPR)
7672 /* Don't use STRIP_NOPS, because signedness of argument type
7673 matters. */
7674 STRIP_SIGN_NOPS (arg0);
7676 else
7678 /* Strip any conversions that don't change the mode. This
7679 is safe for every expression, except for a comparison
7680 expression because its signedness is derived from its
7681 operands.
7683 Note that this is done as an internal manipulation within
7684 the constant folder, in order to find the simplest
7685 representation of the arguments so that their form can be
7686 studied. In any cases, the appropriate type conversions
7687 should be put back in the tree that will get out of the
7688 constant folder. */
7689 STRIP_NOPS (arg0);
7693 if (TREE_CODE_CLASS (code) == tcc_unary)
7695 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7696 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7697 fold_build1_loc (loc, code, type,
7698 fold_convert_loc (loc, TREE_TYPE (op0),
7699 TREE_OPERAND (arg0, 1))));
7700 else if (TREE_CODE (arg0) == COND_EXPR)
7702 tree arg01 = TREE_OPERAND (arg0, 1);
7703 tree arg02 = TREE_OPERAND (arg0, 2);
7704 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7705 arg01 = fold_build1_loc (loc, code, type,
7706 fold_convert_loc (loc,
7707 TREE_TYPE (op0), arg01));
7708 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7709 arg02 = fold_build1_loc (loc, code, type,
7710 fold_convert_loc (loc,
7711 TREE_TYPE (op0), arg02));
7712 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7713 arg01, arg02);
7715 /* If this was a conversion, and all we did was to move into
7716 inside the COND_EXPR, bring it back out. But leave it if
7717 it is a conversion from integer to integer and the
7718 result precision is no wider than a word since such a
7719 conversion is cheap and may be optimized away by combine,
7720 while it couldn't if it were outside the COND_EXPR. Then return
7721 so we don't get into an infinite recursion loop taking the
7722 conversion out and then back in. */
7724 if ((CONVERT_EXPR_CODE_P (code)
7725 || code == NON_LVALUE_EXPR)
7726 && TREE_CODE (tem) == COND_EXPR
7727 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7728 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7729 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7730 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7731 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7732 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7733 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7734 && (INTEGRAL_TYPE_P
7735 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7736 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7737 || flag_syntax_only))
7739 tem = build1 (code, type,
7740 build3 (COND_EXPR,
7741 TREE_TYPE (TREE_OPERAND
7742 (TREE_OPERAND (tem, 1), 0)),
7743 TREE_OPERAND (tem, 0),
7744 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7745 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
7746 SET_EXPR_LOCATION (tem, loc);
7748 return tem;
7750 else if (COMPARISON_CLASS_P (arg0))
7752 if (TREE_CODE (type) == BOOLEAN_TYPE)
7754 arg0 = copy_node (arg0);
7755 TREE_TYPE (arg0) = type;
7756 return arg0;
7758 else if (TREE_CODE (type) != INTEGER_TYPE)
7759 return fold_build3_loc (loc, COND_EXPR, type, arg0,
7760 fold_build1_loc (loc, code, type,
7761 integer_one_node),
7762 fold_build1_loc (loc, code, type,
7763 integer_zero_node));
7767 switch (code)
7769 case PAREN_EXPR:
7770 /* Re-association barriers around constants and other re-association
7771 barriers can be removed. */
7772 if (CONSTANT_CLASS_P (op0)
7773 || TREE_CODE (op0) == PAREN_EXPR)
7774 return fold_convert_loc (loc, type, op0);
7775 return NULL_TREE;
7777 CASE_CONVERT:
7778 case FLOAT_EXPR:
7779 case FIX_TRUNC_EXPR:
7780 if (TREE_TYPE (op0) == type)
7781 return op0;
7783 /* If we have (type) (a CMP b) and type is an integral type, return
7784 new expression involving the new type. */
7785 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7786 return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7787 TREE_OPERAND (op0, 1));
7789 /* Handle cases of two conversions in a row. */
7790 if (CONVERT_EXPR_P (op0))
7792 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7793 tree inter_type = TREE_TYPE (op0);
7794 int inside_int = INTEGRAL_TYPE_P (inside_type);
7795 int inside_ptr = POINTER_TYPE_P (inside_type);
7796 int inside_float = FLOAT_TYPE_P (inside_type);
7797 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7798 unsigned int inside_prec = TYPE_PRECISION (inside_type);
7799 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7800 int inter_int = INTEGRAL_TYPE_P (inter_type);
7801 int inter_ptr = POINTER_TYPE_P (inter_type);
7802 int inter_float = FLOAT_TYPE_P (inter_type);
7803 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7804 unsigned int inter_prec = TYPE_PRECISION (inter_type);
7805 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7806 int final_int = INTEGRAL_TYPE_P (type);
7807 int final_ptr = POINTER_TYPE_P (type);
7808 int final_float = FLOAT_TYPE_P (type);
7809 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7810 unsigned int final_prec = TYPE_PRECISION (type);
7811 int final_unsignedp = TYPE_UNSIGNED (type);
7813 /* In addition to the cases of two conversions in a row
7814 handled below, if we are converting something to its own
7815 type via an object of identical or wider precision, neither
7816 conversion is needed. */
7817 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7818 && (((inter_int || inter_ptr) && final_int)
7819 || (inter_float && final_float))
7820 && inter_prec >= final_prec)
7821 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7823 /* Likewise, if the intermediate and initial types are either both
7824 float or both integer, we don't need the middle conversion if the
7825 former is wider than the latter and doesn't change the signedness
7826 (for integers). Avoid this if the final type is a pointer since
7827 then we sometimes need the middle conversion. Likewise if the
7828 final type has a precision not equal to the size of its mode. */
7829 if (((inter_int && inside_int)
7830 || (inter_float && inside_float)
7831 || (inter_vec && inside_vec))
7832 && inter_prec >= inside_prec
7833 && (inter_float || inter_vec
7834 || inter_unsignedp == inside_unsignedp)
7835 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7836 && TYPE_MODE (type) == TYPE_MODE (inter_type))
7837 && ! final_ptr
7838 && (! final_vec || inter_prec == inside_prec))
7839 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7841 /* If we have a sign-extension of a zero-extended value, we can
7842 replace that by a single zero-extension. */
7843 if (inside_int && inter_int && final_int
7844 && inside_prec < inter_prec && inter_prec < final_prec
7845 && inside_unsignedp && !inter_unsignedp)
7846 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7848 /* Two conversions in a row are not needed unless:
7849 - some conversion is floating-point (overstrict for now), or
7850 - some conversion is a vector (overstrict for now), or
7851 - the intermediate type is narrower than both initial and
7852 final, or
7853 - the intermediate type and innermost type differ in signedness,
7854 and the outermost type is wider than the intermediate, or
7855 - the initial type is a pointer type and the precisions of the
7856 intermediate and final types differ, or
7857 - the final type is a pointer type and the precisions of the
7858 initial and intermediate types differ. */
7859 if (! inside_float && ! inter_float && ! final_float
7860 && ! inside_vec && ! inter_vec && ! final_vec
7861 && (inter_prec >= inside_prec || inter_prec >= final_prec)
7862 && ! (inside_int && inter_int
7863 && inter_unsignedp != inside_unsignedp
7864 && inter_prec < final_prec)
7865 && ((inter_unsignedp && inter_prec > inside_prec)
7866 == (final_unsignedp && final_prec > inter_prec))
7867 && ! (inside_ptr && inter_prec != final_prec)
7868 && ! (final_ptr && inside_prec != inter_prec)
7869 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7870 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7871 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7874 /* Handle (T *)&A.B.C for A being of type T and B and C
7875 living at offset zero. This occurs frequently in
7876 C++ upcasting and then accessing the base. */
7877 if (TREE_CODE (op0) == ADDR_EXPR
7878 && POINTER_TYPE_P (type)
7879 && handled_component_p (TREE_OPERAND (op0, 0)))
7881 HOST_WIDE_INT bitsize, bitpos;
7882 tree offset;
7883 enum machine_mode mode;
7884 int unsignedp, volatilep;
7885 tree base = TREE_OPERAND (op0, 0);
7886 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7887 &mode, &unsignedp, &volatilep, false);
7888 /* If the reference was to a (constant) zero offset, we can use
7889 the address of the base if it has the same base type
7890 as the result type and the pointer type is unqualified. */
7891 if (! offset && bitpos == 0
7892 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7893 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7894 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7895 return fold_convert_loc (loc, type,
7896 build_fold_addr_expr_loc (loc, base));
7899 if (TREE_CODE (op0) == MODIFY_EXPR
7900 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7901 /* Detect assigning a bitfield. */
7902 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7903 && DECL_BIT_FIELD
7904 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7906 /* Don't leave an assignment inside a conversion
7907 unless assigning a bitfield. */
7908 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7909 /* First do the assignment, then return converted constant. */
7910 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7911 TREE_NO_WARNING (tem) = 1;
7912 TREE_USED (tem) = 1;
7913 SET_EXPR_LOCATION (tem, loc);
7914 return tem;
7917 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7918 constants (if x has signed type, the sign bit cannot be set
7919 in c). This folds extension into the BIT_AND_EXPR.
7920 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7921 very likely don't have maximal range for their precision and this
7922 transformation effectively doesn't preserve non-maximal ranges. */
7923 if (TREE_CODE (type) == INTEGER_TYPE
7924 && TREE_CODE (op0) == BIT_AND_EXPR
7925 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7927 tree and_expr = op0;
7928 tree and0 = TREE_OPERAND (and_expr, 0);
7929 tree and1 = TREE_OPERAND (and_expr, 1);
7930 int change = 0;
7932 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7933 || (TYPE_PRECISION (type)
7934 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7935 change = 1;
7936 else if (TYPE_PRECISION (TREE_TYPE (and1))
7937 <= HOST_BITS_PER_WIDE_INT
7938 && host_integerp (and1, 1))
7940 unsigned HOST_WIDE_INT cst;
7942 cst = tree_low_cst (and1, 1);
7943 cst &= (HOST_WIDE_INT) -1
7944 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7945 change = (cst == 0);
7946 #ifdef LOAD_EXTEND_OP
7947 if (change
7948 && !flag_syntax_only
7949 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7950 == ZERO_EXTEND))
7952 tree uns = unsigned_type_for (TREE_TYPE (and0));
7953 and0 = fold_convert_loc (loc, uns, and0);
7954 and1 = fold_convert_loc (loc, uns, and1);
7956 #endif
7958 if (change)
7960 tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
7961 TREE_INT_CST_HIGH (and1), 0,
7962 TREE_OVERFLOW (and1));
7963 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7964 fold_convert_loc (loc, type, and0), tem);
7968 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7969 when one of the new casts will fold away. Conservatively we assume
7970 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7971 if (POINTER_TYPE_P (type)
7972 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7973 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7974 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7975 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7977 tree arg00 = TREE_OPERAND (arg0, 0);
7978 tree arg01 = TREE_OPERAND (arg0, 1);
7980 return fold_build2_loc (loc,
7981 TREE_CODE (arg0), type,
7982 fold_convert_loc (loc, type, arg00),
7983 fold_convert_loc (loc, sizetype, arg01));
7986 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7987 of the same precision, and X is an integer type not narrower than
7988 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7989 if (INTEGRAL_TYPE_P (type)
7990 && TREE_CODE (op0) == BIT_NOT_EXPR
7991 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7992 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7993 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7995 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7996 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7997 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7998 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7999 fold_convert_loc (loc, type, tem));
8002 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8003 type of X and Y (integer types only). */
8004 if (INTEGRAL_TYPE_P (type)
8005 && TREE_CODE (op0) == MULT_EXPR
8006 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8007 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8009 /* Be careful not to introduce new overflows. */
8010 tree mult_type;
8011 if (TYPE_OVERFLOW_WRAPS (type))
8012 mult_type = type;
8013 else
8014 mult_type = unsigned_type_for (type);
8016 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8018 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8019 fold_convert_loc (loc, mult_type,
8020 TREE_OPERAND (op0, 0)),
8021 fold_convert_loc (loc, mult_type,
8022 TREE_OPERAND (op0, 1)));
8023 return fold_convert_loc (loc, type, tem);
8027 tem = fold_convert_const (code, type, op0);
8028 return tem ? tem : NULL_TREE;
8030 case ADDR_SPACE_CONVERT_EXPR:
8031 if (integer_zerop (arg0))
8032 return fold_convert_const (code, type, arg0);
8033 return NULL_TREE;
8035 case FIXED_CONVERT_EXPR:
8036 tem = fold_convert_const (code, type, arg0);
8037 return tem ? tem : NULL_TREE;
8039 case VIEW_CONVERT_EXPR:
8040 if (TREE_TYPE (op0) == type)
8041 return op0;
8042 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8043 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8044 type, TREE_OPERAND (op0, 0));
8046 /* For integral conversions with the same precision or pointer
8047 conversions use a NOP_EXPR instead. */
8048 if ((INTEGRAL_TYPE_P (type)
8049 || POINTER_TYPE_P (type))
8050 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8051 || POINTER_TYPE_P (TREE_TYPE (op0)))
8052 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8053 return fold_convert_loc (loc, type, op0);
8055 /* Strip inner integral conversions that do not change the precision. */
8056 if (CONVERT_EXPR_P (op0)
8057 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8058 || POINTER_TYPE_P (TREE_TYPE (op0)))
8059 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8060 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8061 && (TYPE_PRECISION (TREE_TYPE (op0))
8062 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8063 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8064 type, TREE_OPERAND (op0, 0));
8066 return fold_view_convert_expr (type, op0);
8068 case NEGATE_EXPR:
8069 tem = fold_negate_expr (loc, arg0);
8070 if (tem)
8071 return fold_convert_loc (loc, type, tem);
8072 return NULL_TREE;
8074 case ABS_EXPR:
8075 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8076 return fold_abs_const (arg0, type);
8077 else if (TREE_CODE (arg0) == NEGATE_EXPR)
8078 return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8079 /* Convert fabs((double)float) into (double)fabsf(float). */
8080 else if (TREE_CODE (arg0) == NOP_EXPR
8081 && TREE_CODE (type) == REAL_TYPE)
8083 tree targ0 = strip_float_extensions (arg0);
8084 if (targ0 != arg0)
8085 return fold_convert_loc (loc, type,
8086 fold_build1_loc (loc, ABS_EXPR,
8087 TREE_TYPE (targ0),
8088 targ0));
8090 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8091 else if (TREE_CODE (arg0) == ABS_EXPR)
8092 return arg0;
8093 else if (tree_expr_nonnegative_p (arg0))
8094 return arg0;
8096 /* Strip sign ops from argument. */
8097 if (TREE_CODE (type) == REAL_TYPE)
8099 tem = fold_strip_sign_ops (arg0);
8100 if (tem)
8101 return fold_build1_loc (loc, ABS_EXPR, type,
8102 fold_convert_loc (loc, type, tem));
8104 return NULL_TREE;
8106 case CONJ_EXPR:
8107 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8108 return fold_convert_loc (loc, type, arg0);
8109 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8111 tree itype = TREE_TYPE (type);
8112 tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8113 tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8114 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8115 negate_expr (ipart));
8117 if (TREE_CODE (arg0) == COMPLEX_CST)
8119 tree itype = TREE_TYPE (type);
8120 tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8121 tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8122 return build_complex (type, rpart, negate_expr (ipart));
8124 if (TREE_CODE (arg0) == CONJ_EXPR)
8125 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8126 return NULL_TREE;
8128 case BIT_NOT_EXPR:
8129 if (TREE_CODE (arg0) == INTEGER_CST)
8130 return fold_not_const (arg0, type);
8131 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8132 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8133 /* Convert ~ (-A) to A - 1. */
8134 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8135 return fold_build2_loc (loc, MINUS_EXPR, type,
8136 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8137 build_int_cst (type, 1));
8138 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8139 else if (INTEGRAL_TYPE_P (type)
8140 && ((TREE_CODE (arg0) == MINUS_EXPR
8141 && integer_onep (TREE_OPERAND (arg0, 1)))
8142 || (TREE_CODE (arg0) == PLUS_EXPR
8143 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8144 return fold_build1_loc (loc, NEGATE_EXPR, type,
8145 fold_convert_loc (loc, type,
8146 TREE_OPERAND (arg0, 0)));
8147 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8148 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8149 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8150 fold_convert_loc (loc, type,
8151 TREE_OPERAND (arg0, 0)))))
8152 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8153 fold_convert_loc (loc, type,
8154 TREE_OPERAND (arg0, 1)));
8155 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8156 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8157 fold_convert_loc (loc, type,
8158 TREE_OPERAND (arg0, 1)))))
8159 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8160 fold_convert_loc (loc, type,
8161 TREE_OPERAND (arg0, 0)), tem);
8162 /* Perform BIT_NOT_EXPR on each element individually. */
8163 else if (TREE_CODE (arg0) == VECTOR_CST)
8165 tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8166 int count = TYPE_VECTOR_SUBPARTS (type), i;
8168 for (i = 0; i < count; i++)
8170 if (elements)
8172 elem = TREE_VALUE (elements);
8173 elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8174 if (elem == NULL_TREE)
8175 break;
8176 elements = TREE_CHAIN (elements);
8178 else
8179 elem = build_int_cst (TREE_TYPE (type), -1);
8180 list = tree_cons (NULL_TREE, elem, list);
8182 if (i == count)
8183 return build_vector (type, nreverse (list));
8186 return NULL_TREE;
8188 case TRUTH_NOT_EXPR:
8189 /* The argument to invert_truthvalue must have Boolean type. */
8190 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8191 arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8193 /* Note that the operand of this must be an int
8194 and its values must be 0 or 1.
8195 ("true" is a fixed value perhaps depending on the language,
8196 but we don't handle values other than 1 correctly yet.) */
8197 tem = fold_truth_not_expr (loc, arg0);
8198 if (!tem)
8199 return NULL_TREE;
8200 return fold_convert_loc (loc, type, tem);
8202 case REALPART_EXPR:
8203 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8204 return fold_convert_loc (loc, type, arg0);
8205 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8206 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8207 TREE_OPERAND (arg0, 1));
8208 if (TREE_CODE (arg0) == COMPLEX_CST)
8209 return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8210 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8212 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8213 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8214 fold_build1_loc (loc, REALPART_EXPR, itype,
8215 TREE_OPERAND (arg0, 0)),
8216 fold_build1_loc (loc, REALPART_EXPR, itype,
8217 TREE_OPERAND (arg0, 1)));
8218 return fold_convert_loc (loc, type, tem);
8220 if (TREE_CODE (arg0) == CONJ_EXPR)
8222 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8223 tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8224 TREE_OPERAND (arg0, 0));
8225 return fold_convert_loc (loc, type, tem);
8227 if (TREE_CODE (arg0) == CALL_EXPR)
8229 tree fn = get_callee_fndecl (arg0);
8230 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8231 switch (DECL_FUNCTION_CODE (fn))
8233 CASE_FLT_FN (BUILT_IN_CEXPI):
8234 fn = mathfn_built_in (type, BUILT_IN_COS);
8235 if (fn)
8236 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8237 break;
8239 default:
8240 break;
8243 return NULL_TREE;
8245 case IMAGPART_EXPR:
8246 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8247 return fold_convert_loc (loc, type, integer_zero_node);
8248 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8249 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8250 TREE_OPERAND (arg0, 0));
8251 if (TREE_CODE (arg0) == COMPLEX_CST)
8252 return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8253 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8255 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8256 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8257 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8258 TREE_OPERAND (arg0, 0)),
8259 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8260 TREE_OPERAND (arg0, 1)));
8261 return fold_convert_loc (loc, type, tem);
8263 if (TREE_CODE (arg0) == CONJ_EXPR)
8265 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8266 tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8267 return fold_convert_loc (loc, type, negate_expr (tem));
8269 if (TREE_CODE (arg0) == CALL_EXPR)
8271 tree fn = get_callee_fndecl (arg0);
8272 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8273 switch (DECL_FUNCTION_CODE (fn))
8275 CASE_FLT_FN (BUILT_IN_CEXPI):
8276 fn = mathfn_built_in (type, BUILT_IN_SIN);
8277 if (fn)
8278 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8279 break;
8281 default:
8282 break;
8285 return NULL_TREE;
8287 case INDIRECT_REF:
8288 /* Fold *&X to X if X is an lvalue. */
8289 if (TREE_CODE (op0) == ADDR_EXPR)
8291 tree op00 = TREE_OPERAND (op0, 0);
8292 if ((TREE_CODE (op00) == VAR_DECL
8293 || TREE_CODE (op00) == PARM_DECL
8294 || TREE_CODE (op00) == RESULT_DECL)
8295 && !TREE_READONLY (op00))
8296 return op00;
8298 return NULL_TREE;
8300 default:
8301 return NULL_TREE;
8302 } /* switch (code) */
8306 /* If the operation was a conversion do _not_ mark a resulting constant
8307 with TREE_OVERFLOW if the original constant was not. These conversions
8308 have implementation defined behavior and retaining the TREE_OVERFLOW
8309 flag here would confuse later passes such as VRP. */
8310 tree
8311 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8312 tree type, tree op0)
8314 tree res = fold_unary_loc (loc, code, type, op0);
8315 if (res
8316 && TREE_CODE (res) == INTEGER_CST
8317 && TREE_CODE (op0) == INTEGER_CST
8318 && CONVERT_EXPR_CODE_P (code))
8319 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8321 return res;
8324 /* Fold a binary expression of code CODE and type TYPE with operands
8325 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8326 Return the folded expression if folding is successful. Otherwise,
8327 return NULL_TREE. */
8329 static tree
8330 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8332 enum tree_code compl_code;
8334 if (code == MIN_EXPR)
8335 compl_code = MAX_EXPR;
8336 else if (code == MAX_EXPR)
8337 compl_code = MIN_EXPR;
8338 else
8339 gcc_unreachable ();
8341 /* MIN (MAX (a, b), b) == b. */
8342 if (TREE_CODE (op0) == compl_code
8343 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8344 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8346 /* MIN (MAX (b, a), b) == b. */
8347 if (TREE_CODE (op0) == compl_code
8348 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8349 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8350 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8352 /* MIN (a, MAX (a, b)) == a. */
8353 if (TREE_CODE (op1) == compl_code
8354 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8355 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8356 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8358 /* MIN (a, MAX (b, a)) == a. */
8359 if (TREE_CODE (op1) == compl_code
8360 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8361 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8362 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8364 return NULL_TREE;
8367 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8368 by changing CODE to reduce the magnitude of constants involved in
8369 ARG0 of the comparison.
8370 Returns a canonicalized comparison tree if a simplification was
8371 possible, otherwise returns NULL_TREE.
8372 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8373 valid if signed overflow is undefined. */
8375 static tree
8376 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8377 tree arg0, tree arg1,
8378 bool *strict_overflow_p)
8380 enum tree_code code0 = TREE_CODE (arg0);
8381 tree t, cst0 = NULL_TREE;
8382 int sgn0;
8383 bool swap = false;
8385 /* Match A +- CST code arg1 and CST code arg1. We can change the
8386 first form only if overflow is undefined. */
8387 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8388 /* In principle pointers also have undefined overflow behavior,
8389 but that causes problems elsewhere. */
8390 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8391 && (code0 == MINUS_EXPR
8392 || code0 == PLUS_EXPR)
8393 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8394 || code0 == INTEGER_CST))
8395 return NULL_TREE;
8397 /* Identify the constant in arg0 and its sign. */
8398 if (code0 == INTEGER_CST)
8399 cst0 = arg0;
8400 else
8401 cst0 = TREE_OPERAND (arg0, 1);
8402 sgn0 = tree_int_cst_sgn (cst0);
8404 /* Overflowed constants and zero will cause problems. */
8405 if (integer_zerop (cst0)
8406 || TREE_OVERFLOW (cst0))
8407 return NULL_TREE;
8409 /* See if we can reduce the magnitude of the constant in
8410 arg0 by changing the comparison code. */
8411 if (code0 == INTEGER_CST)
8413 /* CST <= arg1 -> CST-1 < arg1. */
8414 if (code == LE_EXPR && sgn0 == 1)
8415 code = LT_EXPR;
8416 /* -CST < arg1 -> -CST-1 <= arg1. */
8417 else if (code == LT_EXPR && sgn0 == -1)
8418 code = LE_EXPR;
8419 /* CST > arg1 -> CST-1 >= arg1. */
8420 else if (code == GT_EXPR && sgn0 == 1)
8421 code = GE_EXPR;
8422 /* -CST >= arg1 -> -CST-1 > arg1. */
8423 else if (code == GE_EXPR && sgn0 == -1)
8424 code = GT_EXPR;
8425 else
8426 return NULL_TREE;
8427 /* arg1 code' CST' might be more canonical. */
8428 swap = true;
8430 else
8432 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8433 if (code == LT_EXPR
8434 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8435 code = LE_EXPR;
8436 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8437 else if (code == GT_EXPR
8438 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8439 code = GE_EXPR;
8440 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8441 else if (code == LE_EXPR
8442 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8443 code = LT_EXPR;
8444 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8445 else if (code == GE_EXPR
8446 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8447 code = GT_EXPR;
8448 else
8449 return NULL_TREE;
8450 *strict_overflow_p = true;
8453 /* Now build the constant reduced in magnitude. But not if that
8454 would produce one outside of its types range. */
8455 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8456 && ((sgn0 == 1
8457 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8458 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8459 || (sgn0 == -1
8460 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8461 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8462 /* We cannot swap the comparison here as that would cause us to
8463 endlessly recurse. */
8464 return NULL_TREE;
8466 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8467 cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8468 if (code0 != INTEGER_CST)
8469 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8471 /* If swapping might yield to a more canonical form, do so. */
8472 if (swap)
8473 return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8474 else
8475 return fold_build2_loc (loc, code, type, t, arg1);
8478 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8479 overflow further. Try to decrease the magnitude of constants involved
8480 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8481 and put sole constants at the second argument position.
8482 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8484 static tree
8485 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8486 tree arg0, tree arg1)
8488 tree t;
8489 bool strict_overflow_p;
8490 const char * const warnmsg = G_("assuming signed overflow does not occur "
8491 "when reducing constant in comparison");
8493 /* Try canonicalization by simplifying arg0. */
8494 strict_overflow_p = false;
8495 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8496 &strict_overflow_p);
8497 if (t)
8499 if (strict_overflow_p)
8500 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8501 return t;
8504 /* Try canonicalization by simplifying arg1 using the swapped
8505 comparison. */
8506 code = swap_tree_comparison (code);
8507 strict_overflow_p = false;
8508 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8509 &strict_overflow_p);
8510 if (t && strict_overflow_p)
8511 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8512 return t;
8515 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8516 space. This is used to avoid issuing overflow warnings for
8517 expressions like &p->x which can not wrap. */
8519 static bool
8520 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8522 unsigned HOST_WIDE_INT offset_low, total_low;
8523 HOST_WIDE_INT size, offset_high, total_high;
8525 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8526 return true;
8528 if (bitpos < 0)
8529 return true;
8531 if (offset == NULL_TREE)
8533 offset_low = 0;
8534 offset_high = 0;
8536 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8537 return true;
8538 else
8540 offset_low = TREE_INT_CST_LOW (offset);
8541 offset_high = TREE_INT_CST_HIGH (offset);
8544 if (add_double_with_sign (offset_low, offset_high,
8545 bitpos / BITS_PER_UNIT, 0,
8546 &total_low, &total_high,
8547 true))
8548 return true;
8550 if (total_high != 0)
8551 return true;
8553 size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8554 if (size <= 0)
8555 return true;
8557 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8558 array. */
8559 if (TREE_CODE (base) == ADDR_EXPR)
8561 HOST_WIDE_INT base_size;
8563 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8564 if (base_size > 0 && size < base_size)
8565 size = base_size;
8568 return total_low > (unsigned HOST_WIDE_INT) size;
8571 /* Subroutine of fold_binary. This routine performs all of the
8572 transformations that are common to the equality/inequality
8573 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8574 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8575 fold_binary should call fold_binary. Fold a comparison with
8576 tree code CODE and type TYPE with operands OP0 and OP1. Return
8577 the folded comparison or NULL_TREE. */
8579 static tree
8580 fold_comparison (location_t loc, enum tree_code code, tree type,
8581 tree op0, tree op1)
8583 tree arg0, arg1, tem;
8585 arg0 = op0;
8586 arg1 = op1;
8588 STRIP_SIGN_NOPS (arg0);
8589 STRIP_SIGN_NOPS (arg1);
8591 tem = fold_relational_const (code, type, arg0, arg1);
8592 if (tem != NULL_TREE)
8593 return tem;
8595 /* If one arg is a real or integer constant, put it last. */
8596 if (tree_swap_operands_p (arg0, arg1, true))
8597 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8599 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8600 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8601 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8602 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8603 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8604 && (TREE_CODE (arg1) == INTEGER_CST
8605 && !TREE_OVERFLOW (arg1)))
8607 tree const1 = TREE_OPERAND (arg0, 1);
8608 tree const2 = arg1;
8609 tree variable = TREE_OPERAND (arg0, 0);
8610 tree lhs;
8611 int lhs_add;
8612 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8614 lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
8615 TREE_TYPE (arg1), const2, const1);
8617 /* If the constant operation overflowed this can be
8618 simplified as a comparison against INT_MAX/INT_MIN. */
8619 if (TREE_CODE (lhs) == INTEGER_CST
8620 && TREE_OVERFLOW (lhs))
8622 int const1_sgn = tree_int_cst_sgn (const1);
8623 enum tree_code code2 = code;
8625 /* Get the sign of the constant on the lhs if the
8626 operation were VARIABLE + CONST1. */
8627 if (TREE_CODE (arg0) == MINUS_EXPR)
8628 const1_sgn = -const1_sgn;
8630 /* The sign of the constant determines if we overflowed
8631 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8632 Canonicalize to the INT_MIN overflow by swapping the comparison
8633 if necessary. */
8634 if (const1_sgn == -1)
8635 code2 = swap_tree_comparison (code);
8637 /* We now can look at the canonicalized case
8638 VARIABLE + 1 CODE2 INT_MIN
8639 and decide on the result. */
8640 if (code2 == LT_EXPR
8641 || code2 == LE_EXPR
8642 || code2 == EQ_EXPR)
8643 return omit_one_operand_loc (loc, type, boolean_false_node, variable);
8644 else if (code2 == NE_EXPR
8645 || code2 == GE_EXPR
8646 || code2 == GT_EXPR)
8647 return omit_one_operand_loc (loc, type, boolean_true_node, variable);
8650 if (TREE_CODE (lhs) == TREE_CODE (arg1)
8651 && (TREE_CODE (lhs) != INTEGER_CST
8652 || !TREE_OVERFLOW (lhs)))
8654 fold_overflow_warning ("assuming signed overflow does not occur "
8655 "when changing X +- C1 cmp C2 to "
8656 "X cmp C1 +- C2",
8657 WARN_STRICT_OVERFLOW_COMPARISON);
8658 return fold_build2_loc (loc, code, type, variable, lhs);
8662 /* For comparisons of pointers we can decompose it to a compile time
8663 comparison of the base objects and the offsets into the object.
8664 This requires at least one operand being an ADDR_EXPR or a
8665 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8666 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8667 && (TREE_CODE (arg0) == ADDR_EXPR
8668 || TREE_CODE (arg1) == ADDR_EXPR
8669 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8670 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8672 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8673 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8674 enum machine_mode mode;
8675 int volatilep, unsignedp;
8676 bool indirect_base0 = false, indirect_base1 = false;
8678 /* Get base and offset for the access. Strip ADDR_EXPR for
8679 get_inner_reference, but put it back by stripping INDIRECT_REF
8680 off the base object if possible. indirect_baseN will be true
8681 if baseN is not an address but refers to the object itself. */
8682 base0 = arg0;
8683 if (TREE_CODE (arg0) == ADDR_EXPR)
8685 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8686 &bitsize, &bitpos0, &offset0, &mode,
8687 &unsignedp, &volatilep, false);
8688 if (TREE_CODE (base0) == INDIRECT_REF)
8689 base0 = TREE_OPERAND (base0, 0);
8690 else
8691 indirect_base0 = true;
8693 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8695 base0 = TREE_OPERAND (arg0, 0);
8696 offset0 = TREE_OPERAND (arg0, 1);
8699 base1 = arg1;
8700 if (TREE_CODE (arg1) == ADDR_EXPR)
8702 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8703 &bitsize, &bitpos1, &offset1, &mode,
8704 &unsignedp, &volatilep, false);
8705 if (TREE_CODE (base1) == INDIRECT_REF)
8706 base1 = TREE_OPERAND (base1, 0);
8707 else
8708 indirect_base1 = true;
8710 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8712 base1 = TREE_OPERAND (arg1, 0);
8713 offset1 = TREE_OPERAND (arg1, 1);
8716 /* A local variable can never be pointed to by
8717 the default SSA name of an incoming parameter. */
8718 if ((TREE_CODE (arg0) == ADDR_EXPR
8719 && indirect_base0
8720 && TREE_CODE (base0) == VAR_DECL
8721 && auto_var_in_fn_p (base0, current_function_decl)
8722 && !indirect_base1
8723 && TREE_CODE (base1) == SSA_NAME
8724 && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL
8725 && SSA_NAME_IS_DEFAULT_DEF (base1))
8726 || (TREE_CODE (arg1) == ADDR_EXPR
8727 && indirect_base1
8728 && TREE_CODE (base1) == VAR_DECL
8729 && auto_var_in_fn_p (base1, current_function_decl)
8730 && !indirect_base0
8731 && TREE_CODE (base0) == SSA_NAME
8732 && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL
8733 && SSA_NAME_IS_DEFAULT_DEF (base0)))
8735 if (code == NE_EXPR)
8736 return constant_boolean_node (1, type);
8737 else if (code == EQ_EXPR)
8738 return constant_boolean_node (0, type);
8740 /* If we have equivalent bases we might be able to simplify. */
8741 else if (indirect_base0 == indirect_base1
8742 && operand_equal_p (base0, base1, 0))
8744 /* We can fold this expression to a constant if the non-constant
8745 offset parts are equal. */
8746 if ((offset0 == offset1
8747 || (offset0 && offset1
8748 && operand_equal_p (offset0, offset1, 0)))
8749 && (code == EQ_EXPR
8750 || code == NE_EXPR
8751 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8754 if (code != EQ_EXPR
8755 && code != NE_EXPR
8756 && bitpos0 != bitpos1
8757 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8758 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8759 fold_overflow_warning (("assuming pointer wraparound does not "
8760 "occur when comparing P +- C1 with "
8761 "P +- C2"),
8762 WARN_STRICT_OVERFLOW_CONDITIONAL);
8764 switch (code)
8766 case EQ_EXPR:
8767 return constant_boolean_node (bitpos0 == bitpos1, type);
8768 case NE_EXPR:
8769 return constant_boolean_node (bitpos0 != bitpos1, type);
8770 case LT_EXPR:
8771 return constant_boolean_node (bitpos0 < bitpos1, type);
8772 case LE_EXPR:
8773 return constant_boolean_node (bitpos0 <= bitpos1, type);
8774 case GE_EXPR:
8775 return constant_boolean_node (bitpos0 >= bitpos1, type);
8776 case GT_EXPR:
8777 return constant_boolean_node (bitpos0 > bitpos1, type);
8778 default:;
8781 /* We can simplify the comparison to a comparison of the variable
8782 offset parts if the constant offset parts are equal.
8783 Be careful to use signed size type here because otherwise we
8784 mess with array offsets in the wrong way. This is possible
8785 because pointer arithmetic is restricted to retain within an
8786 object and overflow on pointer differences is undefined as of
8787 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8788 else if (bitpos0 == bitpos1
8789 && ((code == EQ_EXPR || code == NE_EXPR)
8790 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8792 /* By converting to signed size type we cover middle-end pointer
8793 arithmetic which operates on unsigned pointer types of size
8794 type size and ARRAY_REF offsets which are properly sign or
8795 zero extended from their type in case it is narrower than
8796 size type. */
8797 if (offset0 == NULL_TREE)
8798 offset0 = build_int_cst (ssizetype, 0);
8799 else
8800 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8801 if (offset1 == NULL_TREE)
8802 offset1 = build_int_cst (ssizetype, 0);
8803 else
8804 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8806 if (code != EQ_EXPR
8807 && code != NE_EXPR
8808 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8809 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8810 fold_overflow_warning (("assuming pointer wraparound does not "
8811 "occur when comparing P +- C1 with "
8812 "P +- C2"),
8813 WARN_STRICT_OVERFLOW_COMPARISON);
8815 return fold_build2_loc (loc, code, type, offset0, offset1);
8818 /* For non-equal bases we can simplify if they are addresses
8819 of local binding decls or constants. */
8820 else if (indirect_base0 && indirect_base1
8821 /* We know that !operand_equal_p (base0, base1, 0)
8822 because the if condition was false. But make
8823 sure two decls are not the same. */
8824 && base0 != base1
8825 && TREE_CODE (arg0) == ADDR_EXPR
8826 && TREE_CODE (arg1) == ADDR_EXPR
8827 && (((TREE_CODE (base0) == VAR_DECL
8828 || TREE_CODE (base0) == PARM_DECL)
8829 && (targetm.binds_local_p (base0)
8830 || CONSTANT_CLASS_P (base1)))
8831 || CONSTANT_CLASS_P (base0))
8832 && (((TREE_CODE (base1) == VAR_DECL
8833 || TREE_CODE (base1) == PARM_DECL)
8834 && (targetm.binds_local_p (base1)
8835 || CONSTANT_CLASS_P (base0)))
8836 || CONSTANT_CLASS_P (base1)))
8838 if (code == EQ_EXPR)
8839 return omit_two_operands_loc (loc, type, boolean_false_node,
8840 arg0, arg1);
8841 else if (code == NE_EXPR)
8842 return omit_two_operands_loc (loc, type, boolean_true_node,
8843 arg0, arg1);
8845 /* For equal offsets we can simplify to a comparison of the
8846 base addresses. */
8847 else if (bitpos0 == bitpos1
8848 && (indirect_base0
8849 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8850 && (indirect_base1
8851 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8852 && ((offset0 == offset1)
8853 || (offset0 && offset1
8854 && operand_equal_p (offset0, offset1, 0))))
8856 if (indirect_base0)
8857 base0 = build_fold_addr_expr_loc (loc, base0);
8858 if (indirect_base1)
8859 base1 = build_fold_addr_expr_loc (loc, base1);
8860 return fold_build2_loc (loc, code, type, base0, base1);
8864 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8865 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8866 the resulting offset is smaller in absolute value than the
8867 original one. */
8868 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8869 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8870 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8871 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8872 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8873 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8874 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8876 tree const1 = TREE_OPERAND (arg0, 1);
8877 tree const2 = TREE_OPERAND (arg1, 1);
8878 tree variable1 = TREE_OPERAND (arg0, 0);
8879 tree variable2 = TREE_OPERAND (arg1, 0);
8880 tree cst;
8881 const char * const warnmsg = G_("assuming signed overflow does not "
8882 "occur when combining constants around "
8883 "a comparison");
8885 /* Put the constant on the side where it doesn't overflow and is
8886 of lower absolute value than before. */
8887 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8888 ? MINUS_EXPR : PLUS_EXPR,
8889 const2, const1, 0);
8890 if (!TREE_OVERFLOW (cst)
8891 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8893 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8894 return fold_build2_loc (loc, code, type,
8895 variable1,
8896 fold_build2_loc (loc,
8897 TREE_CODE (arg1), TREE_TYPE (arg1),
8898 variable2, cst));
8901 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8902 ? MINUS_EXPR : PLUS_EXPR,
8903 const1, const2, 0);
8904 if (!TREE_OVERFLOW (cst)
8905 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8907 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8908 return fold_build2_loc (loc, code, type,
8909 fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
8910 variable1, cst),
8911 variable2);
8915 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8916 signed arithmetic case. That form is created by the compiler
8917 often enough for folding it to be of value. One example is in
8918 computing loop trip counts after Operator Strength Reduction. */
8919 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8920 && TREE_CODE (arg0) == MULT_EXPR
8921 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8922 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8923 && integer_zerop (arg1))
8925 tree const1 = TREE_OPERAND (arg0, 1);
8926 tree const2 = arg1; /* zero */
8927 tree variable1 = TREE_OPERAND (arg0, 0);
8928 enum tree_code cmp_code = code;
8930 /* Handle unfolded multiplication by zero. */
8931 if (integer_zerop (const1))
8932 return fold_build2_loc (loc, cmp_code, type, const1, const2);
8934 fold_overflow_warning (("assuming signed overflow does not occur when "
8935 "eliminating multiplication in comparison "
8936 "with zero"),
8937 WARN_STRICT_OVERFLOW_COMPARISON);
8939 /* If const1 is negative we swap the sense of the comparison. */
8940 if (tree_int_cst_sgn (const1) < 0)
8941 cmp_code = swap_tree_comparison (cmp_code);
8943 return fold_build2_loc (loc, cmp_code, type, variable1, const2);
8946 tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
8947 if (tem)
8948 return tem;
8950 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8952 tree targ0 = strip_float_extensions (arg0);
8953 tree targ1 = strip_float_extensions (arg1);
8954 tree newtype = TREE_TYPE (targ0);
8956 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8957 newtype = TREE_TYPE (targ1);
8959 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
8960 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8961 return fold_build2_loc (loc, code, type,
8962 fold_convert_loc (loc, newtype, targ0),
8963 fold_convert_loc (loc, newtype, targ1));
8965 /* (-a) CMP (-b) -> b CMP a */
8966 if (TREE_CODE (arg0) == NEGATE_EXPR
8967 && TREE_CODE (arg1) == NEGATE_EXPR)
8968 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
8969 TREE_OPERAND (arg0, 0));
8971 if (TREE_CODE (arg1) == REAL_CST)
8973 REAL_VALUE_TYPE cst;
8974 cst = TREE_REAL_CST (arg1);
8976 /* (-a) CMP CST -> a swap(CMP) (-CST) */
8977 if (TREE_CODE (arg0) == NEGATE_EXPR)
8978 return fold_build2_loc (loc, swap_tree_comparison (code), type,
8979 TREE_OPERAND (arg0, 0),
8980 build_real (TREE_TYPE (arg1),
8981 real_value_negate (&cst)));
8983 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
8984 /* a CMP (-0) -> a CMP 0 */
8985 if (REAL_VALUE_MINUS_ZERO (cst))
8986 return fold_build2_loc (loc, code, type, arg0,
8987 build_real (TREE_TYPE (arg1), dconst0));
8989 /* x != NaN is always true, other ops are always false. */
8990 if (REAL_VALUE_ISNAN (cst)
8991 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8993 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8994 return omit_one_operand_loc (loc, type, tem, arg0);
8997 /* Fold comparisons against infinity. */
8998 if (REAL_VALUE_ISINF (cst)
8999 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9001 tem = fold_inf_compare (loc, code, type, arg0, arg1);
9002 if (tem != NULL_TREE)
9003 return tem;
9007 /* If this is a comparison of a real constant with a PLUS_EXPR
9008 or a MINUS_EXPR of a real constant, we can convert it into a
9009 comparison with a revised real constant as long as no overflow
9010 occurs when unsafe_math_optimizations are enabled. */
9011 if (flag_unsafe_math_optimizations
9012 && TREE_CODE (arg1) == REAL_CST
9013 && (TREE_CODE (arg0) == PLUS_EXPR
9014 || TREE_CODE (arg0) == MINUS_EXPR)
9015 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9016 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9017 ? MINUS_EXPR : PLUS_EXPR,
9018 arg1, TREE_OPERAND (arg0, 1), 0))
9019 && !TREE_OVERFLOW (tem))
9020 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9022 /* Likewise, we can simplify a comparison of a real constant with
9023 a MINUS_EXPR whose first operand is also a real constant, i.e.
9024 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
9025 floating-point types only if -fassociative-math is set. */
9026 if (flag_associative_math
9027 && TREE_CODE (arg1) == REAL_CST
9028 && TREE_CODE (arg0) == MINUS_EXPR
9029 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9030 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9031 arg1, 0))
9032 && !TREE_OVERFLOW (tem))
9033 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9034 TREE_OPERAND (arg0, 1), tem);
9036 /* Fold comparisons against built-in math functions. */
9037 if (TREE_CODE (arg1) == REAL_CST
9038 && flag_unsafe_math_optimizations
9039 && ! flag_errno_math)
9041 enum built_in_function fcode = builtin_mathfn_code (arg0);
9043 if (fcode != END_BUILTINS)
9045 tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9046 if (tem != NULL_TREE)
9047 return tem;
9052 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9053 && CONVERT_EXPR_P (arg0))
9055 /* If we are widening one operand of an integer comparison,
9056 see if the other operand is similarly being widened. Perhaps we
9057 can do the comparison in the narrower type. */
9058 tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9059 if (tem)
9060 return tem;
9062 /* Or if we are changing signedness. */
9063 tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9064 if (tem)
9065 return tem;
9068 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9069 constant, we can simplify it. */
9070 if (TREE_CODE (arg1) == INTEGER_CST
9071 && (TREE_CODE (arg0) == MIN_EXPR
9072 || TREE_CODE (arg0) == MAX_EXPR)
9073 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9075 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9076 if (tem)
9077 return tem;
9080 /* Simplify comparison of something with itself. (For IEEE
9081 floating-point, we can only do some of these simplifications.) */
9082 if (operand_equal_p (arg0, arg1, 0))
9084 switch (code)
9086 case EQ_EXPR:
9087 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9088 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9089 return constant_boolean_node (1, type);
9090 break;
9092 case GE_EXPR:
9093 case LE_EXPR:
9094 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9095 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9096 return constant_boolean_node (1, type);
9097 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9099 case NE_EXPR:
9100 /* For NE, we can only do this simplification if integer
9101 or we don't honor IEEE floating point NaNs. */
9102 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9103 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9104 break;
9105 /* ... fall through ... */
9106 case GT_EXPR:
9107 case LT_EXPR:
9108 return constant_boolean_node (0, type);
9109 default:
9110 gcc_unreachable ();
9114 /* If we are comparing an expression that just has comparisons
9115 of two integer values, arithmetic expressions of those comparisons,
9116 and constants, we can simplify it. There are only three cases
9117 to check: the two values can either be equal, the first can be
9118 greater, or the second can be greater. Fold the expression for
9119 those three values. Since each value must be 0 or 1, we have
9120 eight possibilities, each of which corresponds to the constant 0
9121 or 1 or one of the six possible comparisons.
9123 This handles common cases like (a > b) == 0 but also handles
9124 expressions like ((x > y) - (y > x)) > 0, which supposedly
9125 occur in macroized code. */
9127 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9129 tree cval1 = 0, cval2 = 0;
9130 int save_p = 0;
9132 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9133 /* Don't handle degenerate cases here; they should already
9134 have been handled anyway. */
9135 && cval1 != 0 && cval2 != 0
9136 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9137 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9138 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9139 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9140 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9141 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9142 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9144 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9145 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9147 /* We can't just pass T to eval_subst in case cval1 or cval2
9148 was the same as ARG1. */
9150 tree high_result
9151 = fold_build2_loc (loc, code, type,
9152 eval_subst (loc, arg0, cval1, maxval,
9153 cval2, minval),
9154 arg1);
9155 tree equal_result
9156 = fold_build2_loc (loc, code, type,
9157 eval_subst (loc, arg0, cval1, maxval,
9158 cval2, maxval),
9159 arg1);
9160 tree low_result
9161 = fold_build2_loc (loc, code, type,
9162 eval_subst (loc, arg0, cval1, minval,
9163 cval2, maxval),
9164 arg1);
9166 /* All three of these results should be 0 or 1. Confirm they are.
9167 Then use those values to select the proper code to use. */
9169 if (TREE_CODE (high_result) == INTEGER_CST
9170 && TREE_CODE (equal_result) == INTEGER_CST
9171 && TREE_CODE (low_result) == INTEGER_CST)
9173 /* Make a 3-bit mask with the high-order bit being the
9174 value for `>', the next for '=', and the low for '<'. */
9175 switch ((integer_onep (high_result) * 4)
9176 + (integer_onep (equal_result) * 2)
9177 + integer_onep (low_result))
9179 case 0:
9180 /* Always false. */
9181 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9182 case 1:
9183 code = LT_EXPR;
9184 break;
9185 case 2:
9186 code = EQ_EXPR;
9187 break;
9188 case 3:
9189 code = LE_EXPR;
9190 break;
9191 case 4:
9192 code = GT_EXPR;
9193 break;
9194 case 5:
9195 code = NE_EXPR;
9196 break;
9197 case 6:
9198 code = GE_EXPR;
9199 break;
9200 case 7:
9201 /* Always true. */
9202 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9205 if (save_p)
9207 tem = save_expr (build2 (code, type, cval1, cval2));
9208 SET_EXPR_LOCATION (tem, loc);
9209 return tem;
9211 return fold_build2_loc (loc, code, type, cval1, cval2);
9216 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9217 into a single range test. */
9218 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9219 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9220 && TREE_CODE (arg1) == INTEGER_CST
9221 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9222 && !integer_zerop (TREE_OPERAND (arg0, 1))
9223 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9224 && !TREE_OVERFLOW (arg1))
9226 tem = fold_div_compare (loc, code, type, arg0, arg1);
9227 if (tem != NULL_TREE)
9228 return tem;
9231 /* Fold ~X op ~Y as Y op X. */
9232 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9233 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9235 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9236 return fold_build2_loc (loc, code, type,
9237 fold_convert_loc (loc, cmp_type,
9238 TREE_OPERAND (arg1, 0)),
9239 TREE_OPERAND (arg0, 0));
9242 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9243 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9244 && TREE_CODE (arg1) == INTEGER_CST)
9246 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9247 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9248 TREE_OPERAND (arg0, 0),
9249 fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9250 fold_convert_loc (loc, cmp_type, arg1)));
9253 return NULL_TREE;
9257 /* Subroutine of fold_binary. Optimize complex multiplications of the
9258 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9259 argument EXPR represents the expression "z" of type TYPE. */
9261 static tree
9262 fold_mult_zconjz (location_t loc, tree type, tree expr)
9264 tree itype = TREE_TYPE (type);
9265 tree rpart, ipart, tem;
9267 if (TREE_CODE (expr) == COMPLEX_EXPR)
9269 rpart = TREE_OPERAND (expr, 0);
9270 ipart = TREE_OPERAND (expr, 1);
9272 else if (TREE_CODE (expr) == COMPLEX_CST)
9274 rpart = TREE_REALPART (expr);
9275 ipart = TREE_IMAGPART (expr);
9277 else
9279 expr = save_expr (expr);
9280 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9281 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9284 rpart = save_expr (rpart);
9285 ipart = save_expr (ipart);
9286 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9287 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9288 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9289 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9290 fold_convert_loc (loc, itype, integer_zero_node));
9294 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9295 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9296 guarantees that P and N have the same least significant log2(M) bits.
9297 N is not otherwise constrained. In particular, N is not normalized to
9298 0 <= N < M as is common. In general, the precise value of P is unknown.
9299 M is chosen as large as possible such that constant N can be determined.
9301 Returns M and sets *RESIDUE to N.
9303 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9304 account. This is not always possible due to PR 35705.
9307 static unsigned HOST_WIDE_INT
9308 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9309 bool allow_func_align)
9311 enum tree_code code;
9313 *residue = 0;
9315 code = TREE_CODE (expr);
9316 if (code == ADDR_EXPR)
9318 expr = TREE_OPERAND (expr, 0);
9319 if (handled_component_p (expr))
9321 HOST_WIDE_INT bitsize, bitpos;
9322 tree offset;
9323 enum machine_mode mode;
9324 int unsignedp, volatilep;
9326 expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9327 &mode, &unsignedp, &volatilep, false);
9328 *residue = bitpos / BITS_PER_UNIT;
9329 if (offset)
9331 if (TREE_CODE (offset) == INTEGER_CST)
9332 *residue += TREE_INT_CST_LOW (offset);
9333 else
9334 /* We don't handle more complicated offset expressions. */
9335 return 1;
9339 if (DECL_P (expr)
9340 && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9341 return DECL_ALIGN_UNIT (expr);
9343 else if (code == POINTER_PLUS_EXPR)
9345 tree op0, op1;
9346 unsigned HOST_WIDE_INT modulus;
9347 enum tree_code inner_code;
9349 op0 = TREE_OPERAND (expr, 0);
9350 STRIP_NOPS (op0);
9351 modulus = get_pointer_modulus_and_residue (op0, residue,
9352 allow_func_align);
9354 op1 = TREE_OPERAND (expr, 1);
9355 STRIP_NOPS (op1);
9356 inner_code = TREE_CODE (op1);
9357 if (inner_code == INTEGER_CST)
9359 *residue += TREE_INT_CST_LOW (op1);
9360 return modulus;
9362 else if (inner_code == MULT_EXPR)
9364 op1 = TREE_OPERAND (op1, 1);
9365 if (TREE_CODE (op1) == INTEGER_CST)
9367 unsigned HOST_WIDE_INT align;
9369 /* Compute the greatest power-of-2 divisor of op1. */
9370 align = TREE_INT_CST_LOW (op1);
9371 align &= -align;
9373 /* If align is non-zero and less than *modulus, replace
9374 *modulus with align., If align is 0, then either op1 is 0
9375 or the greatest power-of-2 divisor of op1 doesn't fit in an
9376 unsigned HOST_WIDE_INT. In either case, no additional
9377 constraint is imposed. */
9378 if (align)
9379 modulus = MIN (modulus, align);
9381 return modulus;
9386 /* If we get here, we were unable to determine anything useful about the
9387 expression. */
9388 return 1;
9392 /* Fold a binary expression of code CODE and type TYPE with operands
9393 OP0 and OP1. LOC is the location of the resulting expression.
9394 Return the folded expression if folding is successful. Otherwise,
9395 return NULL_TREE. */
9397 tree
9398 fold_binary_loc (location_t loc,
9399 enum tree_code code, tree type, tree op0, tree op1)
9401 enum tree_code_class kind = TREE_CODE_CLASS (code);
9402 tree arg0, arg1, tem;
9403 tree t1 = NULL_TREE;
9404 bool strict_overflow_p;
9406 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9407 && TREE_CODE_LENGTH (code) == 2
9408 && op0 != NULL_TREE
9409 && op1 != NULL_TREE);
9411 arg0 = op0;
9412 arg1 = op1;
9414 /* Strip any conversions that don't change the mode. This is
9415 safe for every expression, except for a comparison expression
9416 because its signedness is derived from its operands. So, in
9417 the latter case, only strip conversions that don't change the
9418 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9419 preserved.
9421 Note that this is done as an internal manipulation within the
9422 constant folder, in order to find the simplest representation
9423 of the arguments so that their form can be studied. In any
9424 cases, the appropriate type conversions should be put back in
9425 the tree that will get out of the constant folder. */
9427 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9429 STRIP_SIGN_NOPS (arg0);
9430 STRIP_SIGN_NOPS (arg1);
9432 else
9434 STRIP_NOPS (arg0);
9435 STRIP_NOPS (arg1);
9438 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9439 constant but we can't do arithmetic on them. */
9440 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9441 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9442 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9443 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9444 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9445 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9447 if (kind == tcc_binary)
9449 /* Make sure type and arg0 have the same saturating flag. */
9450 gcc_assert (TYPE_SATURATING (type)
9451 == TYPE_SATURATING (TREE_TYPE (arg0)));
9452 tem = const_binop (code, arg0, arg1, 0);
9454 else if (kind == tcc_comparison)
9455 tem = fold_relational_const (code, type, arg0, arg1);
9456 else
9457 tem = NULL_TREE;
9459 if (tem != NULL_TREE)
9461 if (TREE_TYPE (tem) != type)
9462 tem = fold_convert_loc (loc, type, tem);
9463 return tem;
9467 /* If this is a commutative operation, and ARG0 is a constant, move it
9468 to ARG1 to reduce the number of tests below. */
9469 if (commutative_tree_code (code)
9470 && tree_swap_operands_p (arg0, arg1, true))
9471 return fold_build2_loc (loc, code, type, op1, op0);
9473 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9475 First check for cases where an arithmetic operation is applied to a
9476 compound, conditional, or comparison operation. Push the arithmetic
9477 operation inside the compound or conditional to see if any folding
9478 can then be done. Convert comparison to conditional for this purpose.
9479 The also optimizes non-constant cases that used to be done in
9480 expand_expr.
9482 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9483 one of the operands is a comparison and the other is a comparison, a
9484 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9485 code below would make the expression more complex. Change it to a
9486 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9487 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9489 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9490 || code == EQ_EXPR || code == NE_EXPR)
9491 && ((truth_value_p (TREE_CODE (arg0))
9492 && (truth_value_p (TREE_CODE (arg1))
9493 || (TREE_CODE (arg1) == BIT_AND_EXPR
9494 && integer_onep (TREE_OPERAND (arg1, 1)))))
9495 || (truth_value_p (TREE_CODE (arg1))
9496 && (truth_value_p (TREE_CODE (arg0))
9497 || (TREE_CODE (arg0) == BIT_AND_EXPR
9498 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9500 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9501 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9502 : TRUTH_XOR_EXPR,
9503 boolean_type_node,
9504 fold_convert_loc (loc, boolean_type_node, arg0),
9505 fold_convert_loc (loc, boolean_type_node, arg1));
9507 if (code == EQ_EXPR)
9508 tem = invert_truthvalue_loc (loc, tem);
9510 return fold_convert_loc (loc, type, tem);
9513 if (TREE_CODE_CLASS (code) == tcc_binary
9514 || TREE_CODE_CLASS (code) == tcc_comparison)
9516 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9518 tem = fold_build2_loc (loc, code, type,
9519 fold_convert_loc (loc, TREE_TYPE (op0),
9520 TREE_OPERAND (arg0, 1)), op1);
9521 tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), tem);
9522 goto fold_binary_exit;
9524 if (TREE_CODE (arg1) == COMPOUND_EXPR
9525 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9527 tem = fold_build2_loc (loc, code, type, op0,
9528 fold_convert_loc (loc, TREE_TYPE (op1),
9529 TREE_OPERAND (arg1, 1)));
9530 tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), tem);
9531 goto fold_binary_exit;
9534 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9536 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9537 arg0, arg1,
9538 /*cond_first_p=*/1);
9539 if (tem != NULL_TREE)
9540 return tem;
9543 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9545 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9546 arg1, arg0,
9547 /*cond_first_p=*/0);
9548 if (tem != NULL_TREE)
9549 return tem;
9553 switch (code)
9555 case POINTER_PLUS_EXPR:
9556 /* 0 +p index -> (type)index */
9557 if (integer_zerop (arg0))
9558 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9560 /* PTR +p 0 -> PTR */
9561 if (integer_zerop (arg1))
9562 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9564 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9565 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9566 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9567 return fold_convert_loc (loc, type,
9568 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9569 fold_convert_loc (loc, sizetype,
9570 arg1),
9571 fold_convert_loc (loc, sizetype,
9572 arg0)));
9574 /* index +p PTR -> PTR +p index */
9575 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9576 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9577 return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
9578 fold_convert_loc (loc, type, arg1),
9579 fold_convert_loc (loc, sizetype, arg0));
9581 /* (PTR +p B) +p A -> PTR +p (B + A) */
9582 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9584 tree inner;
9585 tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
9586 tree arg00 = TREE_OPERAND (arg0, 0);
9587 inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
9588 arg01, fold_convert_loc (loc, sizetype, arg1));
9589 return fold_convert_loc (loc, type,
9590 fold_build2_loc (loc, POINTER_PLUS_EXPR,
9591 TREE_TYPE (arg00),
9592 arg00, inner));
9595 /* PTR_CST +p CST -> CST1 */
9596 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9597 return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
9598 fold_convert_loc (loc, type, arg1));
9600 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9601 of the array. Loop optimizer sometimes produce this type of
9602 expressions. */
9603 if (TREE_CODE (arg0) == ADDR_EXPR)
9605 tem = try_move_mult_to_index (loc, arg0,
9606 fold_convert_loc (loc, sizetype, arg1));
9607 if (tem)
9608 return fold_convert_loc (loc, type, tem);
9611 return NULL_TREE;
9613 case PLUS_EXPR:
9614 /* A + (-B) -> A - B */
9615 if (TREE_CODE (arg1) == NEGATE_EXPR)
9616 return fold_build2_loc (loc, MINUS_EXPR, type,
9617 fold_convert_loc (loc, type, arg0),
9618 fold_convert_loc (loc, type,
9619 TREE_OPERAND (arg1, 0)));
9620 /* (-A) + B -> B - A */
9621 if (TREE_CODE (arg0) == NEGATE_EXPR
9622 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9623 return fold_build2_loc (loc, MINUS_EXPR, type,
9624 fold_convert_loc (loc, type, arg1),
9625 fold_convert_loc (loc, type,
9626 TREE_OPERAND (arg0, 0)));
9628 if (INTEGRAL_TYPE_P (type))
9630 /* Convert ~A + 1 to -A. */
9631 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9632 && integer_onep (arg1))
9633 return fold_build1_loc (loc, NEGATE_EXPR, type,
9634 fold_convert_loc (loc, type,
9635 TREE_OPERAND (arg0, 0)));
9637 /* ~X + X is -1. */
9638 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9639 && !TYPE_OVERFLOW_TRAPS (type))
9641 tree tem = TREE_OPERAND (arg0, 0);
9643 STRIP_NOPS (tem);
9644 if (operand_equal_p (tem, arg1, 0))
9646 t1 = build_int_cst_type (type, -1);
9647 return omit_one_operand_loc (loc, type, t1, arg1);
9651 /* X + ~X is -1. */
9652 if (TREE_CODE (arg1) == BIT_NOT_EXPR
9653 && !TYPE_OVERFLOW_TRAPS (type))
9655 tree tem = TREE_OPERAND (arg1, 0);
9657 STRIP_NOPS (tem);
9658 if (operand_equal_p (arg0, tem, 0))
9660 t1 = build_int_cst_type (type, -1);
9661 return omit_one_operand_loc (loc, type, t1, arg0);
9665 /* X + (X / CST) * -CST is X % CST. */
9666 if (TREE_CODE (arg1) == MULT_EXPR
9667 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9668 && operand_equal_p (arg0,
9669 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9671 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9672 tree cst1 = TREE_OPERAND (arg1, 1);
9673 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9674 cst1, cst0);
9675 if (sum && integer_zerop (sum))
9676 return fold_convert_loc (loc, type,
9677 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9678 TREE_TYPE (arg0), arg0,
9679 cst0));
9683 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9684 same or one. Make sure type is not saturating.
9685 fold_plusminus_mult_expr will re-associate. */
9686 if ((TREE_CODE (arg0) == MULT_EXPR
9687 || TREE_CODE (arg1) == MULT_EXPR)
9688 && !TYPE_SATURATING (type)
9689 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9691 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9692 if (tem)
9693 return tem;
9696 if (! FLOAT_TYPE_P (type))
9698 if (integer_zerop (arg1))
9699 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9701 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9702 with a constant, and the two constants have no bits in common,
9703 we should treat this as a BIT_IOR_EXPR since this may produce more
9704 simplifications. */
9705 if (TREE_CODE (arg0) == BIT_AND_EXPR
9706 && TREE_CODE (arg1) == BIT_AND_EXPR
9707 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9708 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9709 && integer_zerop (const_binop (BIT_AND_EXPR,
9710 TREE_OPERAND (arg0, 1),
9711 TREE_OPERAND (arg1, 1), 0)))
9713 code = BIT_IOR_EXPR;
9714 goto bit_ior;
9717 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9718 (plus (plus (mult) (mult)) (foo)) so that we can
9719 take advantage of the factoring cases below. */
9720 if (((TREE_CODE (arg0) == PLUS_EXPR
9721 || TREE_CODE (arg0) == MINUS_EXPR)
9722 && TREE_CODE (arg1) == MULT_EXPR)
9723 || ((TREE_CODE (arg1) == PLUS_EXPR
9724 || TREE_CODE (arg1) == MINUS_EXPR)
9725 && TREE_CODE (arg0) == MULT_EXPR))
9727 tree parg0, parg1, parg, marg;
9728 enum tree_code pcode;
9730 if (TREE_CODE (arg1) == MULT_EXPR)
9731 parg = arg0, marg = arg1;
9732 else
9733 parg = arg1, marg = arg0;
9734 pcode = TREE_CODE (parg);
9735 parg0 = TREE_OPERAND (parg, 0);
9736 parg1 = TREE_OPERAND (parg, 1);
9737 STRIP_NOPS (parg0);
9738 STRIP_NOPS (parg1);
9740 if (TREE_CODE (parg0) == MULT_EXPR
9741 && TREE_CODE (parg1) != MULT_EXPR)
9742 return fold_build2_loc (loc, pcode, type,
9743 fold_build2_loc (loc, PLUS_EXPR, type,
9744 fold_convert_loc (loc, type,
9745 parg0),
9746 fold_convert_loc (loc, type,
9747 marg)),
9748 fold_convert_loc (loc, type, parg1));
9749 if (TREE_CODE (parg0) != MULT_EXPR
9750 && TREE_CODE (parg1) == MULT_EXPR)
9751 return
9752 fold_build2_loc (loc, PLUS_EXPR, type,
9753 fold_convert_loc (loc, type, parg0),
9754 fold_build2_loc (loc, pcode, type,
9755 fold_convert_loc (loc, type, marg),
9756 fold_convert_loc (loc, type,
9757 parg1)));
9760 else
9762 /* See if ARG1 is zero and X + ARG1 reduces to X. */
9763 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9764 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9766 /* Likewise if the operands are reversed. */
9767 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9768 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9770 /* Convert X + -C into X - C. */
9771 if (TREE_CODE (arg1) == REAL_CST
9772 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9774 tem = fold_negate_const (arg1, type);
9775 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9776 return fold_build2_loc (loc, MINUS_EXPR, type,
9777 fold_convert_loc (loc, type, arg0),
9778 fold_convert_loc (loc, type, tem));
9781 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9782 to __complex__ ( x, y ). This is not the same for SNaNs or
9783 if signed zeros are involved. */
9784 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9785 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9786 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9788 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9789 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9790 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9791 bool arg0rz = false, arg0iz = false;
9792 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9793 || (arg0i && (arg0iz = real_zerop (arg0i))))
9795 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9796 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9797 if (arg0rz && arg1i && real_zerop (arg1i))
9799 tree rp = arg1r ? arg1r
9800 : build1 (REALPART_EXPR, rtype, arg1);
9801 tree ip = arg0i ? arg0i
9802 : build1 (IMAGPART_EXPR, rtype, arg0);
9803 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9805 else if (arg0iz && arg1r && real_zerop (arg1r))
9807 tree rp = arg0r ? arg0r
9808 : build1 (REALPART_EXPR, rtype, arg0);
9809 tree ip = arg1i ? arg1i
9810 : build1 (IMAGPART_EXPR, rtype, arg1);
9811 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9816 if (flag_unsafe_math_optimizations
9817 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9818 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9819 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9820 return tem;
9822 /* Convert x+x into x*2.0. */
9823 if (operand_equal_p (arg0, arg1, 0)
9824 && SCALAR_FLOAT_TYPE_P (type))
9825 return fold_build2_loc (loc, MULT_EXPR, type, arg0,
9826 build_real (type, dconst2));
9828 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9829 We associate floats only if the user has specified
9830 -fassociative-math. */
9831 if (flag_associative_math
9832 && TREE_CODE (arg1) == PLUS_EXPR
9833 && TREE_CODE (arg0) != MULT_EXPR)
9835 tree tree10 = TREE_OPERAND (arg1, 0);
9836 tree tree11 = TREE_OPERAND (arg1, 1);
9837 if (TREE_CODE (tree11) == MULT_EXPR
9838 && TREE_CODE (tree10) == MULT_EXPR)
9840 tree tree0;
9841 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9842 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9845 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9846 We associate floats only if the user has specified
9847 -fassociative-math. */
9848 if (flag_associative_math
9849 && TREE_CODE (arg0) == PLUS_EXPR
9850 && TREE_CODE (arg1) != MULT_EXPR)
9852 tree tree00 = TREE_OPERAND (arg0, 0);
9853 tree tree01 = TREE_OPERAND (arg0, 1);
9854 if (TREE_CODE (tree01) == MULT_EXPR
9855 && TREE_CODE (tree00) == MULT_EXPR)
9857 tree tree0;
9858 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9859 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9864 bit_rotate:
9865 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9866 is a rotate of A by C1 bits. */
9867 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9868 is a rotate of A by B bits. */
9870 enum tree_code code0, code1;
9871 tree rtype;
9872 code0 = TREE_CODE (arg0);
9873 code1 = TREE_CODE (arg1);
9874 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9875 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9876 && operand_equal_p (TREE_OPERAND (arg0, 0),
9877 TREE_OPERAND (arg1, 0), 0)
9878 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9879 TYPE_UNSIGNED (rtype))
9880 /* Only create rotates in complete modes. Other cases are not
9881 expanded properly. */
9882 && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9884 tree tree01, tree11;
9885 enum tree_code code01, code11;
9887 tree01 = TREE_OPERAND (arg0, 1);
9888 tree11 = TREE_OPERAND (arg1, 1);
9889 STRIP_NOPS (tree01);
9890 STRIP_NOPS (tree11);
9891 code01 = TREE_CODE (tree01);
9892 code11 = TREE_CODE (tree11);
9893 if (code01 == INTEGER_CST
9894 && code11 == INTEGER_CST
9895 && TREE_INT_CST_HIGH (tree01) == 0
9896 && TREE_INT_CST_HIGH (tree11) == 0
9897 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9898 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9900 tem = build2 (LROTATE_EXPR,
9901 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9902 TREE_OPERAND (arg0, 0),
9903 code0 == LSHIFT_EXPR
9904 ? tree01 : tree11);
9905 SET_EXPR_LOCATION (tem, loc);
9906 return fold_convert_loc (loc, type, tem);
9908 else if (code11 == MINUS_EXPR)
9910 tree tree110, tree111;
9911 tree110 = TREE_OPERAND (tree11, 0);
9912 tree111 = TREE_OPERAND (tree11, 1);
9913 STRIP_NOPS (tree110);
9914 STRIP_NOPS (tree111);
9915 if (TREE_CODE (tree110) == INTEGER_CST
9916 && 0 == compare_tree_int (tree110,
9917 TYPE_PRECISION
9918 (TREE_TYPE (TREE_OPERAND
9919 (arg0, 0))))
9920 && operand_equal_p (tree01, tree111, 0))
9921 return
9922 fold_convert_loc (loc, type,
9923 build2 ((code0 == LSHIFT_EXPR
9924 ? LROTATE_EXPR
9925 : RROTATE_EXPR),
9926 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9927 TREE_OPERAND (arg0, 0), tree01));
9929 else if (code01 == MINUS_EXPR)
9931 tree tree010, tree011;
9932 tree010 = TREE_OPERAND (tree01, 0);
9933 tree011 = TREE_OPERAND (tree01, 1);
9934 STRIP_NOPS (tree010);
9935 STRIP_NOPS (tree011);
9936 if (TREE_CODE (tree010) == INTEGER_CST
9937 && 0 == compare_tree_int (tree010,
9938 TYPE_PRECISION
9939 (TREE_TYPE (TREE_OPERAND
9940 (arg0, 0))))
9941 && operand_equal_p (tree11, tree011, 0))
9942 return fold_convert_loc
9943 (loc, type,
9944 build2 ((code0 != LSHIFT_EXPR
9945 ? LROTATE_EXPR
9946 : RROTATE_EXPR),
9947 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9948 TREE_OPERAND (arg0, 0), tree11));
9953 associate:
9954 /* In most languages, can't associate operations on floats through
9955 parentheses. Rather than remember where the parentheses were, we
9956 don't associate floats at all, unless the user has specified
9957 -fassociative-math.
9958 And, we need to make sure type is not saturating. */
9960 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9961 && !TYPE_SATURATING (type))
9963 tree var0, con0, lit0, minus_lit0;
9964 tree var1, con1, lit1, minus_lit1;
9965 bool ok = true;
9967 /* Split both trees into variables, constants, and literals. Then
9968 associate each group together, the constants with literals,
9969 then the result with variables. This increases the chances of
9970 literals being recombined later and of generating relocatable
9971 expressions for the sum of a constant and literal. */
9972 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9973 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9974 code == MINUS_EXPR);
9976 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9977 if (code == MINUS_EXPR)
9978 code = PLUS_EXPR;
9980 /* With undefined overflow we can only associate constants with one
9981 variable, and constants whose association doesn't overflow. */
9982 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9983 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9985 if (var0 && var1)
9987 tree tmp0 = var0;
9988 tree tmp1 = var1;
9990 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9991 tmp0 = TREE_OPERAND (tmp0, 0);
9992 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9993 tmp1 = TREE_OPERAND (tmp1, 0);
9994 /* The only case we can still associate with two variables
9995 is if they are the same, modulo negation. */
9996 if (!operand_equal_p (tmp0, tmp1, 0))
9997 ok = false;
10000 if (ok && lit0 && lit1)
10002 tree tmp0 = fold_convert (type, lit0);
10003 tree tmp1 = fold_convert (type, lit1);
10005 if (!TREE_OVERFLOW (tmp0) && !TREE_OVERFLOW (tmp1)
10006 && TREE_OVERFLOW (fold_build2 (code, type, tmp0, tmp1)))
10007 ok = false;
10011 /* Only do something if we found more than two objects. Otherwise,
10012 nothing has changed and we risk infinite recursion. */
10013 if (ok
10014 && (2 < ((var0 != 0) + (var1 != 0)
10015 + (con0 != 0) + (con1 != 0)
10016 + (lit0 != 0) + (lit1 != 0)
10017 + (minus_lit0 != 0) + (minus_lit1 != 0))))
10019 var0 = associate_trees (loc, var0, var1, code, type);
10020 con0 = associate_trees (loc, con0, con1, code, type);
10021 lit0 = associate_trees (loc, lit0, lit1, code, type);
10022 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
10024 /* Preserve the MINUS_EXPR if the negative part of the literal is
10025 greater than the positive part. Otherwise, the multiplicative
10026 folding code (i.e extract_muldiv) may be fooled in case
10027 unsigned constants are subtracted, like in the following
10028 example: ((X*2 + 4) - 8U)/2. */
10029 if (minus_lit0 && lit0)
10031 if (TREE_CODE (lit0) == INTEGER_CST
10032 && TREE_CODE (minus_lit0) == INTEGER_CST
10033 && tree_int_cst_lt (lit0, minus_lit0))
10035 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10036 MINUS_EXPR, type);
10037 lit0 = 0;
10039 else
10041 lit0 = associate_trees (loc, lit0, minus_lit0,
10042 MINUS_EXPR, type);
10043 minus_lit0 = 0;
10046 if (minus_lit0)
10048 if (con0 == 0)
10049 return
10050 fold_convert_loc (loc, type,
10051 associate_trees (loc, var0, minus_lit0,
10052 MINUS_EXPR, type));
10053 else
10055 con0 = associate_trees (loc, con0, minus_lit0,
10056 MINUS_EXPR, type);
10057 return
10058 fold_convert_loc (loc, type,
10059 associate_trees (loc, var0, con0,
10060 PLUS_EXPR, type));
10064 con0 = associate_trees (loc, con0, lit0, code, type);
10065 return
10066 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10067 code, type));
10071 return NULL_TREE;
10073 case MINUS_EXPR:
10074 /* Pointer simplifications for subtraction, simple reassociations. */
10075 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10077 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10078 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10079 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10081 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10082 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10083 tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10084 tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10085 return fold_build2_loc (loc, PLUS_EXPR, type,
10086 fold_build2_loc (loc, MINUS_EXPR, type,
10087 arg00, arg10),
10088 fold_build2_loc (loc, MINUS_EXPR, type,
10089 arg01, arg11));
10091 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10092 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10094 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10095 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10096 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10097 fold_convert_loc (loc, type, arg1));
10098 if (tmp)
10099 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10102 /* A - (-B) -> A + B */
10103 if (TREE_CODE (arg1) == NEGATE_EXPR)
10104 return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10105 fold_convert_loc (loc, type,
10106 TREE_OPERAND (arg1, 0)));
10107 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10108 if (TREE_CODE (arg0) == NEGATE_EXPR
10109 && (FLOAT_TYPE_P (type)
10110 || INTEGRAL_TYPE_P (type))
10111 && negate_expr_p (arg1)
10112 && reorder_operands_p (arg0, arg1))
10113 return fold_build2_loc (loc, MINUS_EXPR, type,
10114 fold_convert_loc (loc, type,
10115 negate_expr (arg1)),
10116 fold_convert_loc (loc, type,
10117 TREE_OPERAND (arg0, 0)));
10118 /* Convert -A - 1 to ~A. */
10119 if (INTEGRAL_TYPE_P (type)
10120 && TREE_CODE (arg0) == NEGATE_EXPR
10121 && integer_onep (arg1)
10122 && !TYPE_OVERFLOW_TRAPS (type))
10123 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10124 fold_convert_loc (loc, type,
10125 TREE_OPERAND (arg0, 0)));
10127 /* Convert -1 - A to ~A. */
10128 if (INTEGRAL_TYPE_P (type)
10129 && integer_all_onesp (arg0))
10130 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10133 /* X - (X / CST) * CST is X % CST. */
10134 if (INTEGRAL_TYPE_P (type)
10135 && TREE_CODE (arg1) == MULT_EXPR
10136 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10137 && operand_equal_p (arg0,
10138 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10139 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10140 TREE_OPERAND (arg1, 1), 0))
10141 return
10142 fold_convert_loc (loc, type,
10143 fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10144 arg0, TREE_OPERAND (arg1, 1)));
10146 if (! FLOAT_TYPE_P (type))
10148 if (integer_zerop (arg0))
10149 return negate_expr (fold_convert_loc (loc, type, arg1));
10150 if (integer_zerop (arg1))
10151 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10153 /* Fold A - (A & B) into ~B & A. */
10154 if (!TREE_SIDE_EFFECTS (arg0)
10155 && TREE_CODE (arg1) == BIT_AND_EXPR)
10157 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10159 tree arg10 = fold_convert_loc (loc, type,
10160 TREE_OPERAND (arg1, 0));
10161 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10162 fold_build1_loc (loc, BIT_NOT_EXPR,
10163 type, arg10),
10164 fold_convert_loc (loc, type, arg0));
10166 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10168 tree arg11 = fold_convert_loc (loc,
10169 type, TREE_OPERAND (arg1, 1));
10170 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10171 fold_build1_loc (loc, BIT_NOT_EXPR,
10172 type, arg11),
10173 fold_convert_loc (loc, type, arg0));
10177 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10178 any power of 2 minus 1. */
10179 if (TREE_CODE (arg0) == BIT_AND_EXPR
10180 && TREE_CODE (arg1) == BIT_AND_EXPR
10181 && operand_equal_p (TREE_OPERAND (arg0, 0),
10182 TREE_OPERAND (arg1, 0), 0))
10184 tree mask0 = TREE_OPERAND (arg0, 1);
10185 tree mask1 = TREE_OPERAND (arg1, 1);
10186 tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10188 if (operand_equal_p (tem, mask1, 0))
10190 tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10191 TREE_OPERAND (arg0, 0), mask1);
10192 return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10197 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10198 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10199 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10201 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10202 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10203 (-ARG1 + ARG0) reduces to -ARG1. */
10204 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10205 return negate_expr (fold_convert_loc (loc, type, arg1));
10207 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10208 __complex__ ( x, -y ). This is not the same for SNaNs or if
10209 signed zeros are involved. */
10210 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10211 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10212 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10214 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10215 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10216 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10217 bool arg0rz = false, arg0iz = false;
10218 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10219 || (arg0i && (arg0iz = real_zerop (arg0i))))
10221 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10222 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10223 if (arg0rz && arg1i && real_zerop (arg1i))
10225 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10226 arg1r ? arg1r
10227 : build1 (REALPART_EXPR, rtype, arg1));
10228 tree ip = arg0i ? arg0i
10229 : build1 (IMAGPART_EXPR, rtype, arg0);
10230 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10232 else if (arg0iz && arg1r && real_zerop (arg1r))
10234 tree rp = arg0r ? arg0r
10235 : build1 (REALPART_EXPR, rtype, arg0);
10236 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10237 arg1i ? arg1i
10238 : build1 (IMAGPART_EXPR, rtype, arg1));
10239 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10244 /* Fold &x - &x. This can happen from &x.foo - &x.
10245 This is unsafe for certain floats even in non-IEEE formats.
10246 In IEEE, it is unsafe because it does wrong for NaNs.
10247 Also note that operand_equal_p is always false if an operand
10248 is volatile. */
10250 if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10251 && operand_equal_p (arg0, arg1, 0))
10252 return fold_convert_loc (loc, type, integer_zero_node);
10254 /* A - B -> A + (-B) if B is easily negatable. */
10255 if (negate_expr_p (arg1)
10256 && ((FLOAT_TYPE_P (type)
10257 /* Avoid this transformation if B is a positive REAL_CST. */
10258 && (TREE_CODE (arg1) != REAL_CST
10259 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10260 || INTEGRAL_TYPE_P (type)))
10261 return fold_build2_loc (loc, PLUS_EXPR, type,
10262 fold_convert_loc (loc, type, arg0),
10263 fold_convert_loc (loc, type,
10264 negate_expr (arg1)));
10266 /* Try folding difference of addresses. */
10268 HOST_WIDE_INT diff;
10270 if ((TREE_CODE (arg0) == ADDR_EXPR
10271 || TREE_CODE (arg1) == ADDR_EXPR)
10272 && ptr_difference_const (arg0, arg1, &diff))
10273 return build_int_cst_type (type, diff);
10276 /* Fold &a[i] - &a[j] to i-j. */
10277 if (TREE_CODE (arg0) == ADDR_EXPR
10278 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10279 && TREE_CODE (arg1) == ADDR_EXPR
10280 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10282 tree aref0 = TREE_OPERAND (arg0, 0);
10283 tree aref1 = TREE_OPERAND (arg1, 0);
10284 if (operand_equal_p (TREE_OPERAND (aref0, 0),
10285 TREE_OPERAND (aref1, 0), 0))
10287 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10288 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10289 tree esz = array_ref_element_size (aref0);
10290 tree diff = build2 (MINUS_EXPR, type, op0, op1);
10291 return fold_build2_loc (loc, MULT_EXPR, type, diff,
10292 fold_convert_loc (loc, type, esz));
10297 if (FLOAT_TYPE_P (type)
10298 && flag_unsafe_math_optimizations
10299 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10300 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10301 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10302 return tem;
10304 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10305 same or one. Make sure type is not saturating.
10306 fold_plusminus_mult_expr will re-associate. */
10307 if ((TREE_CODE (arg0) == MULT_EXPR
10308 || TREE_CODE (arg1) == MULT_EXPR)
10309 && !TYPE_SATURATING (type)
10310 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10312 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10313 if (tem)
10314 return tem;
10317 goto associate;
10319 case MULT_EXPR:
10320 /* (-A) * (-B) -> A * B */
10321 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10322 return fold_build2_loc (loc, MULT_EXPR, type,
10323 fold_convert_loc (loc, type,
10324 TREE_OPERAND (arg0, 0)),
10325 fold_convert_loc (loc, type,
10326 negate_expr (arg1)));
10327 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10328 return fold_build2_loc (loc, MULT_EXPR, type,
10329 fold_convert_loc (loc, type,
10330 negate_expr (arg0)),
10331 fold_convert_loc (loc, type,
10332 TREE_OPERAND (arg1, 0)));
10334 if (! FLOAT_TYPE_P (type))
10336 if (integer_zerop (arg1))
10337 return omit_one_operand_loc (loc, type, arg1, arg0);
10338 if (integer_onep (arg1))
10339 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10340 /* Transform x * -1 into -x. Make sure to do the negation
10341 on the original operand with conversions not stripped
10342 because we can only strip non-sign-changing conversions. */
10343 if (integer_all_onesp (arg1))
10344 return fold_convert_loc (loc, type, negate_expr (op0));
10345 /* Transform x * -C into -x * C if x is easily negatable. */
10346 if (TREE_CODE (arg1) == INTEGER_CST
10347 && tree_int_cst_sgn (arg1) == -1
10348 && negate_expr_p (arg0)
10349 && (tem = negate_expr (arg1)) != arg1
10350 && !TREE_OVERFLOW (tem))
10351 return fold_build2_loc (loc, MULT_EXPR, type,
10352 fold_convert_loc (loc, type,
10353 negate_expr (arg0)),
10354 tem);
10356 /* (a * (1 << b)) is (a << b) */
10357 if (TREE_CODE (arg1) == LSHIFT_EXPR
10358 && integer_onep (TREE_OPERAND (arg1, 0)))
10359 return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10360 TREE_OPERAND (arg1, 1));
10361 if (TREE_CODE (arg0) == LSHIFT_EXPR
10362 && integer_onep (TREE_OPERAND (arg0, 0)))
10363 return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10364 TREE_OPERAND (arg0, 1));
10366 /* (A + A) * C -> A * 2 * C */
10367 if (TREE_CODE (arg0) == PLUS_EXPR
10368 && TREE_CODE (arg1) == INTEGER_CST
10369 && operand_equal_p (TREE_OPERAND (arg0, 0),
10370 TREE_OPERAND (arg0, 1), 0))
10371 return fold_build2_loc (loc, MULT_EXPR, type,
10372 omit_one_operand_loc (loc, type,
10373 TREE_OPERAND (arg0, 0),
10374 TREE_OPERAND (arg0, 1)),
10375 fold_build2_loc (loc, MULT_EXPR, type,
10376 build_int_cst (type, 2) , arg1));
10378 strict_overflow_p = false;
10379 if (TREE_CODE (arg1) == INTEGER_CST
10380 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10381 &strict_overflow_p)))
10383 if (strict_overflow_p)
10384 fold_overflow_warning (("assuming signed overflow does not "
10385 "occur when simplifying "
10386 "multiplication"),
10387 WARN_STRICT_OVERFLOW_MISC);
10388 return fold_convert_loc (loc, type, tem);
10391 /* Optimize z * conj(z) for integer complex numbers. */
10392 if (TREE_CODE (arg0) == CONJ_EXPR
10393 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10394 return fold_mult_zconjz (loc, type, arg1);
10395 if (TREE_CODE (arg1) == CONJ_EXPR
10396 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10397 return fold_mult_zconjz (loc, type, arg0);
10399 else
10401 /* Maybe fold x * 0 to 0. The expressions aren't the same
10402 when x is NaN, since x * 0 is also NaN. Nor are they the
10403 same in modes with signed zeros, since multiplying a
10404 negative value by 0 gives -0, not +0. */
10405 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10406 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10407 && real_zerop (arg1))
10408 return omit_one_operand_loc (loc, type, arg1, arg0);
10409 /* In IEEE floating point, x*1 is not equivalent to x for snans.
10410 Likewise for complex arithmetic with signed zeros. */
10411 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10412 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10413 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10414 && real_onep (arg1))
10415 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10417 /* Transform x * -1.0 into -x. */
10418 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10419 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10420 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10421 && real_minus_onep (arg1))
10422 return fold_convert_loc (loc, type, negate_expr (arg0));
10424 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10425 the result for floating point types due to rounding so it is applied
10426 only if -fassociative-math was specify. */
10427 if (flag_associative_math
10428 && TREE_CODE (arg0) == RDIV_EXPR
10429 && TREE_CODE (arg1) == REAL_CST
10430 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10432 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10433 arg1, 0);
10434 if (tem)
10435 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10436 TREE_OPERAND (arg0, 1));
10439 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10440 if (operand_equal_p (arg0, arg1, 0))
10442 tree tem = fold_strip_sign_ops (arg0);
10443 if (tem != NULL_TREE)
10445 tem = fold_convert_loc (loc, type, tem);
10446 return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10450 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10451 This is not the same for NaNs or if signed zeros are
10452 involved. */
10453 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10454 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10455 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10456 && TREE_CODE (arg1) == COMPLEX_CST
10457 && real_zerop (TREE_REALPART (arg1)))
10459 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10460 if (real_onep (TREE_IMAGPART (arg1)))
10461 return
10462 fold_build2_loc (loc, COMPLEX_EXPR, type,
10463 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10464 rtype, arg0)),
10465 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10466 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10467 return
10468 fold_build2_loc (loc, COMPLEX_EXPR, type,
10469 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10470 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10471 rtype, arg0)));
10474 /* Optimize z * conj(z) for floating point complex numbers.
10475 Guarded by flag_unsafe_math_optimizations as non-finite
10476 imaginary components don't produce scalar results. */
10477 if (flag_unsafe_math_optimizations
10478 && TREE_CODE (arg0) == CONJ_EXPR
10479 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10480 return fold_mult_zconjz (loc, type, arg1);
10481 if (flag_unsafe_math_optimizations
10482 && TREE_CODE (arg1) == CONJ_EXPR
10483 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10484 return fold_mult_zconjz (loc, type, arg0);
10486 if (flag_unsafe_math_optimizations)
10488 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10489 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10491 /* Optimizations of root(...)*root(...). */
10492 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10494 tree rootfn, arg;
10495 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10496 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10498 /* Optimize sqrt(x)*sqrt(x) as x. */
10499 if (BUILTIN_SQRT_P (fcode0)
10500 && operand_equal_p (arg00, arg10, 0)
10501 && ! HONOR_SNANS (TYPE_MODE (type)))
10502 return arg00;
10504 /* Optimize root(x)*root(y) as root(x*y). */
10505 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10506 arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10507 return build_call_expr_loc (loc, rootfn, 1, arg);
10510 /* Optimize expN(x)*expN(y) as expN(x+y). */
10511 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10513 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10514 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10515 CALL_EXPR_ARG (arg0, 0),
10516 CALL_EXPR_ARG (arg1, 0));
10517 return build_call_expr_loc (loc, expfn, 1, arg);
10520 /* Optimizations of pow(...)*pow(...). */
10521 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10522 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10523 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10525 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10526 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10527 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10528 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10530 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10531 if (operand_equal_p (arg01, arg11, 0))
10533 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10534 tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10535 arg00, arg10);
10536 return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10539 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10540 if (operand_equal_p (arg00, arg10, 0))
10542 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10543 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10544 arg01, arg11);
10545 return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10549 /* Optimize tan(x)*cos(x) as sin(x). */
10550 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10551 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10552 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10553 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10554 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10555 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10556 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10557 CALL_EXPR_ARG (arg1, 0), 0))
10559 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10561 if (sinfn != NULL_TREE)
10562 return build_call_expr_loc (loc, sinfn, 1,
10563 CALL_EXPR_ARG (arg0, 0));
10566 /* Optimize x*pow(x,c) as pow(x,c+1). */
10567 if (fcode1 == BUILT_IN_POW
10568 || fcode1 == BUILT_IN_POWF
10569 || fcode1 == BUILT_IN_POWL)
10571 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10572 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10573 if (TREE_CODE (arg11) == REAL_CST
10574 && !TREE_OVERFLOW (arg11)
10575 && operand_equal_p (arg0, arg10, 0))
10577 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10578 REAL_VALUE_TYPE c;
10579 tree arg;
10581 c = TREE_REAL_CST (arg11);
10582 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10583 arg = build_real (type, c);
10584 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10588 /* Optimize pow(x,c)*x as pow(x,c+1). */
10589 if (fcode0 == BUILT_IN_POW
10590 || fcode0 == BUILT_IN_POWF
10591 || fcode0 == BUILT_IN_POWL)
10593 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10594 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10595 if (TREE_CODE (arg01) == REAL_CST
10596 && !TREE_OVERFLOW (arg01)
10597 && operand_equal_p (arg1, arg00, 0))
10599 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10600 REAL_VALUE_TYPE c;
10601 tree arg;
10603 c = TREE_REAL_CST (arg01);
10604 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10605 arg = build_real (type, c);
10606 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10610 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
10611 if (optimize_function_for_speed_p (cfun)
10612 && operand_equal_p (arg0, arg1, 0))
10614 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10616 if (powfn)
10618 tree arg = build_real (type, dconst2);
10619 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10624 goto associate;
10626 case BIT_IOR_EXPR:
10627 bit_ior:
10628 if (integer_all_onesp (arg1))
10629 return omit_one_operand_loc (loc, type, arg1, arg0);
10630 if (integer_zerop (arg1))
10631 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10632 if (operand_equal_p (arg0, arg1, 0))
10633 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10635 /* ~X | X is -1. */
10636 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10637 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10639 t1 = fold_convert_loc (loc, type, integer_zero_node);
10640 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10641 return omit_one_operand_loc (loc, type, t1, arg1);
10644 /* X | ~X is -1. */
10645 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10646 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10648 t1 = fold_convert_loc (loc, type, integer_zero_node);
10649 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10650 return omit_one_operand_loc (loc, type, t1, arg0);
10653 /* Canonicalize (X & C1) | C2. */
10654 if (TREE_CODE (arg0) == BIT_AND_EXPR
10655 && TREE_CODE (arg1) == INTEGER_CST
10656 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10658 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10659 int width = TYPE_PRECISION (type), w;
10660 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10661 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10662 hi2 = TREE_INT_CST_HIGH (arg1);
10663 lo2 = TREE_INT_CST_LOW (arg1);
10665 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10666 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10667 return omit_one_operand_loc (loc, type, arg1,
10668 TREE_OPERAND (arg0, 0));
10670 if (width > HOST_BITS_PER_WIDE_INT)
10672 mhi = (unsigned HOST_WIDE_INT) -1
10673 >> (2 * HOST_BITS_PER_WIDE_INT - width);
10674 mlo = -1;
10676 else
10678 mhi = 0;
10679 mlo = (unsigned HOST_WIDE_INT) -1
10680 >> (HOST_BITS_PER_WIDE_INT - width);
10683 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10684 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10685 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10686 TREE_OPERAND (arg0, 0), arg1);
10688 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10689 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10690 mode which allows further optimizations. */
10691 hi1 &= mhi;
10692 lo1 &= mlo;
10693 hi2 &= mhi;
10694 lo2 &= mlo;
10695 hi3 = hi1 & ~hi2;
10696 lo3 = lo1 & ~lo2;
10697 for (w = BITS_PER_UNIT;
10698 w <= width && w <= HOST_BITS_PER_WIDE_INT;
10699 w <<= 1)
10701 unsigned HOST_WIDE_INT mask
10702 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10703 if (((lo1 | lo2) & mask) == mask
10704 && (lo1 & ~mask) == 0 && hi1 == 0)
10706 hi3 = 0;
10707 lo3 = mask;
10708 break;
10711 if (hi3 != hi1 || lo3 != lo1)
10712 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10713 fold_build2_loc (loc, BIT_AND_EXPR, type,
10714 TREE_OPERAND (arg0, 0),
10715 build_int_cst_wide (type,
10716 lo3, hi3)),
10717 arg1);
10720 /* (X & Y) | Y is (X, Y). */
10721 if (TREE_CODE (arg0) == BIT_AND_EXPR
10722 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10723 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10724 /* (X & Y) | X is (Y, X). */
10725 if (TREE_CODE (arg0) == BIT_AND_EXPR
10726 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10727 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10728 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10729 /* X | (X & Y) is (Y, X). */
10730 if (TREE_CODE (arg1) == BIT_AND_EXPR
10731 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10732 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10733 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10734 /* X | (Y & X) is (Y, X). */
10735 if (TREE_CODE (arg1) == BIT_AND_EXPR
10736 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10737 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10738 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10740 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
10741 if (t1 != NULL_TREE)
10742 return t1;
10744 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10746 This results in more efficient code for machines without a NAND
10747 instruction. Combine will canonicalize to the first form
10748 which will allow use of NAND instructions provided by the
10749 backend if they exist. */
10750 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10751 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10753 return
10754 fold_build1_loc (loc, BIT_NOT_EXPR, type,
10755 build2 (BIT_AND_EXPR, type,
10756 fold_convert_loc (loc, type,
10757 TREE_OPERAND (arg0, 0)),
10758 fold_convert_loc (loc, type,
10759 TREE_OPERAND (arg1, 0))));
10762 /* See if this can be simplified into a rotate first. If that
10763 is unsuccessful continue in the association code. */
10764 goto bit_rotate;
10766 case BIT_XOR_EXPR:
10767 if (integer_zerop (arg1))
10768 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10769 if (integer_all_onesp (arg1))
10770 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
10771 if (operand_equal_p (arg0, arg1, 0))
10772 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10774 /* ~X ^ X is -1. */
10775 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10776 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10778 t1 = fold_convert_loc (loc, type, integer_zero_node);
10779 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10780 return omit_one_operand_loc (loc, type, t1, arg1);
10783 /* X ^ ~X is -1. */
10784 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10785 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10787 t1 = fold_convert_loc (loc, type, integer_zero_node);
10788 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10789 return omit_one_operand_loc (loc, type, t1, arg0);
10792 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10793 with a constant, and the two constants have no bits in common,
10794 we should treat this as a BIT_IOR_EXPR since this may produce more
10795 simplifications. */
10796 if (TREE_CODE (arg0) == BIT_AND_EXPR
10797 && TREE_CODE (arg1) == BIT_AND_EXPR
10798 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10799 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10800 && integer_zerop (const_binop (BIT_AND_EXPR,
10801 TREE_OPERAND (arg0, 1),
10802 TREE_OPERAND (arg1, 1), 0)))
10804 code = BIT_IOR_EXPR;
10805 goto bit_ior;
10808 /* (X | Y) ^ X -> Y & ~ X*/
10809 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10810 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10812 tree t2 = TREE_OPERAND (arg0, 1);
10813 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10814 arg1);
10815 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10816 fold_convert_loc (loc, type, t2),
10817 fold_convert_loc (loc, type, t1));
10818 return t1;
10821 /* (Y | X) ^ X -> Y & ~ X*/
10822 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10823 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10825 tree t2 = TREE_OPERAND (arg0, 0);
10826 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10827 arg1);
10828 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10829 fold_convert_loc (loc, type, t2),
10830 fold_convert_loc (loc, type, t1));
10831 return t1;
10834 /* X ^ (X | Y) -> Y & ~ X*/
10835 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10836 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10838 tree t2 = TREE_OPERAND (arg1, 1);
10839 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10840 arg0);
10841 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10842 fold_convert_loc (loc, type, t2),
10843 fold_convert_loc (loc, type, t1));
10844 return t1;
10847 /* X ^ (Y | X) -> Y & ~ X*/
10848 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10849 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10851 tree t2 = TREE_OPERAND (arg1, 0);
10852 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10853 arg0);
10854 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10855 fold_convert_loc (loc, type, t2),
10856 fold_convert_loc (loc, type, t1));
10857 return t1;
10860 /* Convert ~X ^ ~Y to X ^ Y. */
10861 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10862 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10863 return fold_build2_loc (loc, code, type,
10864 fold_convert_loc (loc, type,
10865 TREE_OPERAND (arg0, 0)),
10866 fold_convert_loc (loc, type,
10867 TREE_OPERAND (arg1, 0)));
10869 /* Convert ~X ^ C to X ^ ~C. */
10870 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10871 && TREE_CODE (arg1) == INTEGER_CST)
10872 return fold_build2_loc (loc, code, type,
10873 fold_convert_loc (loc, type,
10874 TREE_OPERAND (arg0, 0)),
10875 fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
10877 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10878 if (TREE_CODE (arg0) == BIT_AND_EXPR
10879 && integer_onep (TREE_OPERAND (arg0, 1))
10880 && integer_onep (arg1))
10881 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10882 build_int_cst (TREE_TYPE (arg0), 0));
10884 /* Fold (X & Y) ^ Y as ~X & Y. */
10885 if (TREE_CODE (arg0) == BIT_AND_EXPR
10886 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10888 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10889 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10890 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10891 fold_convert_loc (loc, type, arg1));
10893 /* Fold (X & Y) ^ X as ~Y & X. */
10894 if (TREE_CODE (arg0) == BIT_AND_EXPR
10895 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10896 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10898 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10899 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10900 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10901 fold_convert_loc (loc, type, arg1));
10903 /* Fold X ^ (X & Y) as X & ~Y. */
10904 if (TREE_CODE (arg1) == BIT_AND_EXPR
10905 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10907 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10908 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10909 fold_convert_loc (loc, type, arg0),
10910 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10912 /* Fold X ^ (Y & X) as ~Y & X. */
10913 if (TREE_CODE (arg1) == BIT_AND_EXPR
10914 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10915 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10917 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10918 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10919 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10920 fold_convert_loc (loc, type, arg0));
10923 /* See if this can be simplified into a rotate first. If that
10924 is unsuccessful continue in the association code. */
10925 goto bit_rotate;
10927 case BIT_AND_EXPR:
10928 if (integer_all_onesp (arg1))
10929 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10930 if (integer_zerop (arg1))
10931 return omit_one_operand_loc (loc, type, arg1, arg0);
10932 if (operand_equal_p (arg0, arg1, 0))
10933 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10935 /* ~X & X is always zero. */
10936 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10937 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10938 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10940 /* X & ~X is always zero. */
10941 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10942 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10943 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10945 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
10946 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10947 && TREE_CODE (arg1) == INTEGER_CST
10948 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10950 tree tmp1 = fold_convert_loc (loc, type, arg1);
10951 tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10952 tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10953 tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
10954 tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
10955 return
10956 fold_convert_loc (loc, type,
10957 fold_build2_loc (loc, BIT_IOR_EXPR,
10958 type, tmp2, tmp3));
10961 /* (X | Y) & Y is (X, Y). */
10962 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10963 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10964 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10965 /* (X | Y) & X is (Y, X). */
10966 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10967 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10968 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10969 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10970 /* X & (X | Y) is (Y, X). */
10971 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10972 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10973 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10974 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10975 /* X & (Y | X) is (Y, X). */
10976 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10977 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10978 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10979 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10981 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10982 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10983 && integer_onep (TREE_OPERAND (arg0, 1))
10984 && integer_onep (arg1))
10986 tem = TREE_OPERAND (arg0, 0);
10987 return fold_build2_loc (loc, EQ_EXPR, type,
10988 fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
10989 build_int_cst (TREE_TYPE (tem), 1)),
10990 build_int_cst (TREE_TYPE (tem), 0));
10992 /* Fold ~X & 1 as (X & 1) == 0. */
10993 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10994 && integer_onep (arg1))
10996 tem = TREE_OPERAND (arg0, 0);
10997 return fold_build2_loc (loc, EQ_EXPR, type,
10998 fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
10999 build_int_cst (TREE_TYPE (tem), 1)),
11000 build_int_cst (TREE_TYPE (tem), 0));
11003 /* Fold (X ^ Y) & Y as ~X & Y. */
11004 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11005 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11007 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11008 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11009 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11010 fold_convert_loc (loc, type, arg1));
11012 /* Fold (X ^ Y) & X as ~Y & X. */
11013 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11014 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11015 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11017 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11018 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11019 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11020 fold_convert_loc (loc, type, arg1));
11022 /* Fold X & (X ^ Y) as X & ~Y. */
11023 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11024 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11026 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11027 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11028 fold_convert_loc (loc, type, arg0),
11029 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11031 /* Fold X & (Y ^ X) as ~Y & X. */
11032 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11033 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11034 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11036 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11037 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11038 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11039 fold_convert_loc (loc, type, arg0));
11042 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11043 if (t1 != NULL_TREE)
11044 return t1;
11045 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11046 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11047 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11049 unsigned int prec
11050 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11052 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11053 && (~TREE_INT_CST_LOW (arg1)
11054 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11055 return
11056 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11059 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11061 This results in more efficient code for machines without a NOR
11062 instruction. Combine will canonicalize to the first form
11063 which will allow use of NOR instructions provided by the
11064 backend if they exist. */
11065 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11066 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11068 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11069 build2 (BIT_IOR_EXPR, type,
11070 fold_convert_loc (loc, type,
11071 TREE_OPERAND (arg0, 0)),
11072 fold_convert_loc (loc, type,
11073 TREE_OPERAND (arg1, 0))));
11076 /* If arg0 is derived from the address of an object or function, we may
11077 be able to fold this expression using the object or function's
11078 alignment. */
11079 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11081 unsigned HOST_WIDE_INT modulus, residue;
11082 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11084 modulus = get_pointer_modulus_and_residue (arg0, &residue,
11085 integer_onep (arg1));
11087 /* This works because modulus is a power of 2. If this weren't the
11088 case, we'd have to replace it by its greatest power-of-2
11089 divisor: modulus & -modulus. */
11090 if (low < modulus)
11091 return build_int_cst (type, residue & low);
11094 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11095 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11096 if the new mask might be further optimized. */
11097 if ((TREE_CODE (arg0) == LSHIFT_EXPR
11098 || TREE_CODE (arg0) == RSHIFT_EXPR)
11099 && host_integerp (TREE_OPERAND (arg0, 1), 1)
11100 && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11101 && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11102 < TYPE_PRECISION (TREE_TYPE (arg0))
11103 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11104 && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11106 unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11107 unsigned HOST_WIDE_INT mask
11108 = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11109 unsigned HOST_WIDE_INT newmask, zerobits = 0;
11110 tree shift_type = TREE_TYPE (arg0);
11112 if (TREE_CODE (arg0) == LSHIFT_EXPR)
11113 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11114 else if (TREE_CODE (arg0) == RSHIFT_EXPR
11115 && TYPE_PRECISION (TREE_TYPE (arg0))
11116 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11118 unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11119 tree arg00 = TREE_OPERAND (arg0, 0);
11120 /* See if more bits can be proven as zero because of
11121 zero extension. */
11122 if (TREE_CODE (arg00) == NOP_EXPR
11123 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11125 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11126 if (TYPE_PRECISION (inner_type)
11127 == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11128 && TYPE_PRECISION (inner_type) < prec)
11130 prec = TYPE_PRECISION (inner_type);
11131 /* See if we can shorten the right shift. */
11132 if (shiftc < prec)
11133 shift_type = inner_type;
11136 zerobits = ~(unsigned HOST_WIDE_INT) 0;
11137 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11138 zerobits <<= prec - shiftc;
11139 /* For arithmetic shift if sign bit could be set, zerobits
11140 can contain actually sign bits, so no transformation is
11141 possible, unless MASK masks them all away. In that
11142 case the shift needs to be converted into logical shift. */
11143 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11144 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11146 if ((mask & zerobits) == 0)
11147 shift_type = unsigned_type_for (TREE_TYPE (arg0));
11148 else
11149 zerobits = 0;
11153 /* ((X << 16) & 0xff00) is (X, 0). */
11154 if ((mask & zerobits) == mask)
11155 return omit_one_operand_loc (loc, type,
11156 build_int_cst (type, 0), arg0);
11158 newmask = mask | zerobits;
11159 if (newmask != mask && (newmask & (newmask + 1)) == 0)
11161 unsigned int prec;
11163 /* Only do the transformation if NEWMASK is some integer
11164 mode's mask. */
11165 for (prec = BITS_PER_UNIT;
11166 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11167 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11168 break;
11169 if (prec < HOST_BITS_PER_WIDE_INT
11170 || newmask == ~(unsigned HOST_WIDE_INT) 0)
11172 tree newmaskt;
11174 if (shift_type != TREE_TYPE (arg0))
11176 tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11177 fold_convert_loc (loc, shift_type,
11178 TREE_OPERAND (arg0, 0)),
11179 TREE_OPERAND (arg0, 1));
11180 tem = fold_convert_loc (loc, type, tem);
11182 else
11183 tem = op0;
11184 newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11185 if (!tree_int_cst_equal (newmaskt, arg1))
11186 return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11191 goto associate;
11193 case RDIV_EXPR:
11194 /* Don't touch a floating-point divide by zero unless the mode
11195 of the constant can represent infinity. */
11196 if (TREE_CODE (arg1) == REAL_CST
11197 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11198 && real_zerop (arg1))
11199 return NULL_TREE;
11201 /* Optimize A / A to 1.0 if we don't care about
11202 NaNs or Infinities. Skip the transformation
11203 for non-real operands. */
11204 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11205 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11206 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11207 && operand_equal_p (arg0, arg1, 0))
11209 tree r = build_real (TREE_TYPE (arg0), dconst1);
11211 return omit_two_operands_loc (loc, type, r, arg0, arg1);
11214 /* The complex version of the above A / A optimization. */
11215 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11216 && operand_equal_p (arg0, arg1, 0))
11218 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11219 if (! HONOR_NANS (TYPE_MODE (elem_type))
11220 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11222 tree r = build_real (elem_type, dconst1);
11223 /* omit_two_operands will call fold_convert for us. */
11224 return omit_two_operands_loc (loc, type, r, arg0, arg1);
11228 /* (-A) / (-B) -> A / B */
11229 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11230 return fold_build2_loc (loc, RDIV_EXPR, type,
11231 TREE_OPERAND (arg0, 0),
11232 negate_expr (arg1));
11233 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11234 return fold_build2_loc (loc, RDIV_EXPR, type,
11235 negate_expr (arg0),
11236 TREE_OPERAND (arg1, 0));
11238 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
11239 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11240 && real_onep (arg1))
11241 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11243 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
11244 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11245 && real_minus_onep (arg1))
11246 return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11247 negate_expr (arg0)));
11249 /* If ARG1 is a constant, we can convert this to a multiply by the
11250 reciprocal. This does not have the same rounding properties,
11251 so only do this if -freciprocal-math. We can actually
11252 always safely do it if ARG1 is a power of two, but it's hard to
11253 tell if it is or not in a portable manner. */
11254 if (TREE_CODE (arg1) == REAL_CST)
11256 if (flag_reciprocal_math
11257 && 0 != (tem = const_binop (code, build_real (type, dconst1),
11258 arg1, 0)))
11259 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11260 /* Find the reciprocal if optimizing and the result is exact. */
11261 if (optimize)
11263 REAL_VALUE_TYPE r;
11264 r = TREE_REAL_CST (arg1);
11265 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11267 tem = build_real (type, r);
11268 return fold_build2_loc (loc, MULT_EXPR, type,
11269 fold_convert_loc (loc, type, arg0), tem);
11273 /* Convert A/B/C to A/(B*C). */
11274 if (flag_reciprocal_math
11275 && TREE_CODE (arg0) == RDIV_EXPR)
11276 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11277 fold_build2_loc (loc, MULT_EXPR, type,
11278 TREE_OPERAND (arg0, 1), arg1));
11280 /* Convert A/(B/C) to (A/B)*C. */
11281 if (flag_reciprocal_math
11282 && TREE_CODE (arg1) == RDIV_EXPR)
11283 return fold_build2_loc (loc, MULT_EXPR, type,
11284 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11285 TREE_OPERAND (arg1, 0)),
11286 TREE_OPERAND (arg1, 1));
11288 /* Convert C1/(X*C2) into (C1/C2)/X. */
11289 if (flag_reciprocal_math
11290 && TREE_CODE (arg1) == MULT_EXPR
11291 && TREE_CODE (arg0) == REAL_CST
11292 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11294 tree tem = const_binop (RDIV_EXPR, arg0,
11295 TREE_OPERAND (arg1, 1), 0);
11296 if (tem)
11297 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11298 TREE_OPERAND (arg1, 0));
11301 if (flag_unsafe_math_optimizations)
11303 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11304 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11306 /* Optimize sin(x)/cos(x) as tan(x). */
11307 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11308 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11309 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11310 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11311 CALL_EXPR_ARG (arg1, 0), 0))
11313 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11315 if (tanfn != NULL_TREE)
11316 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11319 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11320 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11321 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11322 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11323 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11324 CALL_EXPR_ARG (arg1, 0), 0))
11326 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11328 if (tanfn != NULL_TREE)
11330 tree tmp = build_call_expr_loc (loc, tanfn, 1,
11331 CALL_EXPR_ARG (arg0, 0));
11332 return fold_build2_loc (loc, RDIV_EXPR, type,
11333 build_real (type, dconst1), tmp);
11337 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11338 NaNs or Infinities. */
11339 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11340 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11341 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11343 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11344 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11346 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11347 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11348 && operand_equal_p (arg00, arg01, 0))
11350 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11352 if (cosfn != NULL_TREE)
11353 return build_call_expr_loc (loc, cosfn, 1, arg00);
11357 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11358 NaNs or Infinities. */
11359 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11360 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11361 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11363 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11364 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11366 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11367 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11368 && operand_equal_p (arg00, arg01, 0))
11370 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11372 if (cosfn != NULL_TREE)
11374 tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11375 return fold_build2_loc (loc, RDIV_EXPR, type,
11376 build_real (type, dconst1),
11377 tmp);
11382 /* Optimize pow(x,c)/x as pow(x,c-1). */
11383 if (fcode0 == BUILT_IN_POW
11384 || fcode0 == BUILT_IN_POWF
11385 || fcode0 == BUILT_IN_POWL)
11387 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11388 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11389 if (TREE_CODE (arg01) == REAL_CST
11390 && !TREE_OVERFLOW (arg01)
11391 && operand_equal_p (arg1, arg00, 0))
11393 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11394 REAL_VALUE_TYPE c;
11395 tree arg;
11397 c = TREE_REAL_CST (arg01);
11398 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11399 arg = build_real (type, c);
11400 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11404 /* Optimize a/root(b/c) into a*root(c/b). */
11405 if (BUILTIN_ROOT_P (fcode1))
11407 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11409 if (TREE_CODE (rootarg) == RDIV_EXPR)
11411 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11412 tree b = TREE_OPERAND (rootarg, 0);
11413 tree c = TREE_OPERAND (rootarg, 1);
11415 tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11417 tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11418 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11422 /* Optimize x/expN(y) into x*expN(-y). */
11423 if (BUILTIN_EXPONENT_P (fcode1))
11425 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11426 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11427 arg1 = build_call_expr_loc (loc,
11428 expfn, 1,
11429 fold_convert_loc (loc, type, arg));
11430 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11433 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11434 if (fcode1 == BUILT_IN_POW
11435 || fcode1 == BUILT_IN_POWF
11436 || fcode1 == BUILT_IN_POWL)
11438 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11439 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11440 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11441 tree neg11 = fold_convert_loc (loc, type,
11442 negate_expr (arg11));
11443 arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11444 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11447 return NULL_TREE;
11449 case TRUNC_DIV_EXPR:
11450 case FLOOR_DIV_EXPR:
11451 /* Simplify A / (B << N) where A and B are positive and B is
11452 a power of 2, to A >> (N + log2(B)). */
11453 strict_overflow_p = false;
11454 if (TREE_CODE (arg1) == LSHIFT_EXPR
11455 && (TYPE_UNSIGNED (type)
11456 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11458 tree sval = TREE_OPERAND (arg1, 0);
11459 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11461 tree sh_cnt = TREE_OPERAND (arg1, 1);
11462 unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11464 if (strict_overflow_p)
11465 fold_overflow_warning (("assuming signed overflow does not "
11466 "occur when simplifying A / (B << N)"),
11467 WARN_STRICT_OVERFLOW_MISC);
11469 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11470 sh_cnt, build_int_cst (NULL_TREE, pow2));
11471 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11472 fold_convert_loc (loc, type, arg0), sh_cnt);
11476 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11477 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
11478 if (INTEGRAL_TYPE_P (type)
11479 && TYPE_UNSIGNED (type)
11480 && code == FLOOR_DIV_EXPR)
11481 return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
11483 /* Fall thru */
11485 case ROUND_DIV_EXPR:
11486 case CEIL_DIV_EXPR:
11487 case EXACT_DIV_EXPR:
11488 if (integer_onep (arg1))
11489 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11490 if (integer_zerop (arg1))
11491 return NULL_TREE;
11492 /* X / -1 is -X. */
11493 if (!TYPE_UNSIGNED (type)
11494 && TREE_CODE (arg1) == INTEGER_CST
11495 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11496 && TREE_INT_CST_HIGH (arg1) == -1)
11497 return fold_convert_loc (loc, type, negate_expr (arg0));
11499 /* Convert -A / -B to A / B when the type is signed and overflow is
11500 undefined. */
11501 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11502 && TREE_CODE (arg0) == NEGATE_EXPR
11503 && negate_expr_p (arg1))
11505 if (INTEGRAL_TYPE_P (type))
11506 fold_overflow_warning (("assuming signed overflow does not occur "
11507 "when distributing negation across "
11508 "division"),
11509 WARN_STRICT_OVERFLOW_MISC);
11510 return fold_build2_loc (loc, code, type,
11511 fold_convert_loc (loc, type,
11512 TREE_OPERAND (arg0, 0)),
11513 fold_convert_loc (loc, type,
11514 negate_expr (arg1)));
11516 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11517 && TREE_CODE (arg1) == NEGATE_EXPR
11518 && negate_expr_p (arg0))
11520 if (INTEGRAL_TYPE_P (type))
11521 fold_overflow_warning (("assuming signed overflow does not occur "
11522 "when distributing negation across "
11523 "division"),
11524 WARN_STRICT_OVERFLOW_MISC);
11525 return fold_build2_loc (loc, code, type,
11526 fold_convert_loc (loc, type,
11527 negate_expr (arg0)),
11528 fold_convert_loc (loc, type,
11529 TREE_OPERAND (arg1, 0)));
11532 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11533 operation, EXACT_DIV_EXPR.
11535 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11536 At one time others generated faster code, it's not clear if they do
11537 after the last round to changes to the DIV code in expmed.c. */
11538 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11539 && multiple_of_p (type, arg0, arg1))
11540 return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11542 strict_overflow_p = false;
11543 if (TREE_CODE (arg1) == INTEGER_CST
11544 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11545 &strict_overflow_p)))
11547 if (strict_overflow_p)
11548 fold_overflow_warning (("assuming signed overflow does not occur "
11549 "when simplifying division"),
11550 WARN_STRICT_OVERFLOW_MISC);
11551 return fold_convert_loc (loc, type, tem);
11554 return NULL_TREE;
11556 case CEIL_MOD_EXPR:
11557 case FLOOR_MOD_EXPR:
11558 case ROUND_MOD_EXPR:
11559 case TRUNC_MOD_EXPR:
11560 /* X % 1 is always zero, but be sure to preserve any side
11561 effects in X. */
11562 if (integer_onep (arg1))
11563 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11565 /* X % 0, return X % 0 unchanged so that we can get the
11566 proper warnings and errors. */
11567 if (integer_zerop (arg1))
11568 return NULL_TREE;
11570 /* 0 % X is always zero, but be sure to preserve any side
11571 effects in X. Place this after checking for X == 0. */
11572 if (integer_zerop (arg0))
11573 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11575 /* X % -1 is zero. */
11576 if (!TYPE_UNSIGNED (type)
11577 && TREE_CODE (arg1) == INTEGER_CST
11578 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11579 && TREE_INT_CST_HIGH (arg1) == -1)
11580 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11582 /* X % -C is the same as X % C. */
11583 if (code == TRUNC_MOD_EXPR
11584 && !TYPE_UNSIGNED (type)
11585 && TREE_CODE (arg1) == INTEGER_CST
11586 && !TREE_OVERFLOW (arg1)
11587 && TREE_INT_CST_HIGH (arg1) < 0
11588 && !TYPE_OVERFLOW_TRAPS (type)
11589 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
11590 && !sign_bit_p (arg1, arg1))
11591 return fold_build2_loc (loc, code, type,
11592 fold_convert_loc (loc, type, arg0),
11593 fold_convert_loc (loc, type,
11594 negate_expr (arg1)));
11596 /* X % -Y is the same as X % Y. */
11597 if (code == TRUNC_MOD_EXPR
11598 && !TYPE_UNSIGNED (type)
11599 && TREE_CODE (arg1) == NEGATE_EXPR
11600 && !TYPE_OVERFLOW_TRAPS (type))
11601 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
11602 fold_convert_loc (loc, type,
11603 TREE_OPERAND (arg1, 0)));
11605 strict_overflow_p = false;
11606 if (TREE_CODE (arg1) == INTEGER_CST
11607 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11608 &strict_overflow_p)))
11610 if (strict_overflow_p)
11611 fold_overflow_warning (("assuming signed overflow does not occur "
11612 "when simplifying modulus"),
11613 WARN_STRICT_OVERFLOW_MISC);
11614 return fold_convert_loc (loc, type, tem);
11617 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11618 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
11619 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11620 && (TYPE_UNSIGNED (type)
11621 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11623 tree c = arg1;
11624 /* Also optimize A % (C << N) where C is a power of 2,
11625 to A & ((C << N) - 1). */
11626 if (TREE_CODE (arg1) == LSHIFT_EXPR)
11627 c = TREE_OPERAND (arg1, 0);
11629 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11631 tree mask
11632 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
11633 build_int_cst (TREE_TYPE (arg1), 1));
11634 if (strict_overflow_p)
11635 fold_overflow_warning (("assuming signed overflow does not "
11636 "occur when simplifying "
11637 "X % (power of two)"),
11638 WARN_STRICT_OVERFLOW_MISC);
11639 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11640 fold_convert_loc (loc, type, arg0),
11641 fold_convert_loc (loc, type, mask));
11645 return NULL_TREE;
11647 case LROTATE_EXPR:
11648 case RROTATE_EXPR:
11649 if (integer_all_onesp (arg0))
11650 return omit_one_operand_loc (loc, type, arg0, arg1);
11651 goto shift;
11653 case RSHIFT_EXPR:
11654 /* Optimize -1 >> x for arithmetic right shifts. */
11655 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11656 && tree_expr_nonnegative_p (arg1))
11657 return omit_one_operand_loc (loc, type, arg0, arg1);
11658 /* ... fall through ... */
11660 case LSHIFT_EXPR:
11661 shift:
11662 if (integer_zerop (arg1))
11663 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11664 if (integer_zerop (arg0))
11665 return omit_one_operand_loc (loc, type, arg0, arg1);
11667 /* Since negative shift count is not well-defined,
11668 don't try to compute it in the compiler. */
11669 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11670 return NULL_TREE;
11672 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11673 if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11674 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11675 && host_integerp (TREE_OPERAND (arg0, 1), false)
11676 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11678 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11679 + TREE_INT_CST_LOW (arg1));
11681 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11682 being well defined. */
11683 if (low >= TYPE_PRECISION (type))
11685 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11686 low = low % TYPE_PRECISION (type);
11687 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11688 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
11689 TREE_OPERAND (arg0, 0));
11690 else
11691 low = TYPE_PRECISION (type) - 1;
11694 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11695 build_int_cst (type, low));
11698 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11699 into x & ((unsigned)-1 >> c) for unsigned types. */
11700 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11701 || (TYPE_UNSIGNED (type)
11702 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11703 && host_integerp (arg1, false)
11704 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11705 && host_integerp (TREE_OPERAND (arg0, 1), false)
11706 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11708 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11709 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11710 tree lshift;
11711 tree arg00;
11713 if (low0 == low1)
11715 arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11717 lshift = build_int_cst (type, -1);
11718 lshift = int_const_binop (code, lshift, arg1, 0);
11720 return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
11724 /* Rewrite an LROTATE_EXPR by a constant into an
11725 RROTATE_EXPR by a new constant. */
11726 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11728 tree tem = build_int_cst (TREE_TYPE (arg1),
11729 TYPE_PRECISION (type));
11730 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11731 return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
11734 /* If we have a rotate of a bit operation with the rotate count and
11735 the second operand of the bit operation both constant,
11736 permute the two operations. */
11737 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11738 && (TREE_CODE (arg0) == BIT_AND_EXPR
11739 || TREE_CODE (arg0) == BIT_IOR_EXPR
11740 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11741 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11742 return fold_build2_loc (loc, TREE_CODE (arg0), type,
11743 fold_build2_loc (loc, code, type,
11744 TREE_OPERAND (arg0, 0), arg1),
11745 fold_build2_loc (loc, code, type,
11746 TREE_OPERAND (arg0, 1), arg1));
11748 /* Two consecutive rotates adding up to the precision of the
11749 type can be ignored. */
11750 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11751 && TREE_CODE (arg0) == RROTATE_EXPR
11752 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11753 && TREE_INT_CST_HIGH (arg1) == 0
11754 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11755 && ((TREE_INT_CST_LOW (arg1)
11756 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11757 == (unsigned int) TYPE_PRECISION (type)))
11758 return TREE_OPERAND (arg0, 0);
11760 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11761 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11762 if the latter can be further optimized. */
11763 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11764 && TREE_CODE (arg0) == BIT_AND_EXPR
11765 && TREE_CODE (arg1) == INTEGER_CST
11766 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11768 tree mask = fold_build2_loc (loc, code, type,
11769 fold_convert_loc (loc, type,
11770 TREE_OPERAND (arg0, 1)),
11771 arg1);
11772 tree shift = fold_build2_loc (loc, code, type,
11773 fold_convert_loc (loc, type,
11774 TREE_OPERAND (arg0, 0)),
11775 arg1);
11776 tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
11777 if (tem)
11778 return tem;
11781 return NULL_TREE;
11783 case MIN_EXPR:
11784 if (operand_equal_p (arg0, arg1, 0))
11785 return omit_one_operand_loc (loc, type, arg0, arg1);
11786 if (INTEGRAL_TYPE_P (type)
11787 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11788 return omit_one_operand_loc (loc, type, arg1, arg0);
11789 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
11790 if (tem)
11791 return tem;
11792 goto associate;
11794 case MAX_EXPR:
11795 if (operand_equal_p (arg0, arg1, 0))
11796 return omit_one_operand_loc (loc, type, arg0, arg1);
11797 if (INTEGRAL_TYPE_P (type)
11798 && TYPE_MAX_VALUE (type)
11799 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11800 return omit_one_operand_loc (loc, type, arg1, arg0);
11801 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
11802 if (tem)
11803 return tem;
11804 goto associate;
11806 case TRUTH_ANDIF_EXPR:
11807 /* Note that the operands of this must be ints
11808 and their values must be 0 or 1.
11809 ("true" is a fixed value perhaps depending on the language.) */
11810 /* If first arg is constant zero, return it. */
11811 if (integer_zerop (arg0))
11812 return fold_convert_loc (loc, type, arg0);
11813 case TRUTH_AND_EXPR:
11814 /* If either arg is constant true, drop it. */
11815 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11816 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11817 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11818 /* Preserve sequence points. */
11819 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11820 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11821 /* If second arg is constant zero, result is zero, but first arg
11822 must be evaluated. */
11823 if (integer_zerop (arg1))
11824 return omit_one_operand_loc (loc, type, arg1, arg0);
11825 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11826 case will be handled here. */
11827 if (integer_zerop (arg0))
11828 return omit_one_operand_loc (loc, type, arg0, arg1);
11830 /* !X && X is always false. */
11831 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11832 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11833 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11834 /* X && !X is always false. */
11835 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11836 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11837 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11839 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11840 means A >= Y && A != MAX, but in this case we know that
11841 A < X <= MAX. */
11843 if (!TREE_SIDE_EFFECTS (arg0)
11844 && !TREE_SIDE_EFFECTS (arg1))
11846 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
11847 if (tem && !operand_equal_p (tem, arg0, 0))
11848 return fold_build2_loc (loc, code, type, tem, arg1);
11850 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
11851 if (tem && !operand_equal_p (tem, arg1, 0))
11852 return fold_build2_loc (loc, code, type, arg0, tem);
11855 truth_andor:
11856 /* We only do these simplifications if we are optimizing. */
11857 if (!optimize)
11858 return NULL_TREE;
11860 /* Check for things like (A || B) && (A || C). We can convert this
11861 to A || (B && C). Note that either operator can be any of the four
11862 truth and/or operations and the transformation will still be
11863 valid. Also note that we only care about order for the
11864 ANDIF and ORIF operators. If B contains side effects, this
11865 might change the truth-value of A. */
11866 if (TREE_CODE (arg0) == TREE_CODE (arg1)
11867 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11868 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11869 || TREE_CODE (arg0) == TRUTH_AND_EXPR
11870 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11871 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11873 tree a00 = TREE_OPERAND (arg0, 0);
11874 tree a01 = TREE_OPERAND (arg0, 1);
11875 tree a10 = TREE_OPERAND (arg1, 0);
11876 tree a11 = TREE_OPERAND (arg1, 1);
11877 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11878 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11879 && (code == TRUTH_AND_EXPR
11880 || code == TRUTH_OR_EXPR));
11882 if (operand_equal_p (a00, a10, 0))
11883 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
11884 fold_build2_loc (loc, code, type, a01, a11));
11885 else if (commutative && operand_equal_p (a00, a11, 0))
11886 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
11887 fold_build2_loc (loc, code, type, a01, a10));
11888 else if (commutative && operand_equal_p (a01, a10, 0))
11889 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
11890 fold_build2_loc (loc, code, type, a00, a11));
11892 /* This case if tricky because we must either have commutative
11893 operators or else A10 must not have side-effects. */
11895 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
11896 && operand_equal_p (a01, a11, 0))
11897 return fold_build2_loc (loc, TREE_CODE (arg0), type,
11898 fold_build2_loc (loc, code, type, a00, a10),
11899 a01);
11902 /* See if we can build a range comparison. */
11903 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
11904 return tem;
11906 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
11907 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
11909 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
11910 if (tem)
11911 return fold_build2_loc (loc, code, type, tem, arg1);
11914 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
11915 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
11917 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
11918 if (tem)
11919 return fold_build2_loc (loc, code, type, arg0, tem);
11922 /* Check for the possibility of merging component references. If our
11923 lhs is another similar operation, try to merge its rhs with our
11924 rhs. Then try to merge our lhs and rhs. */
11925 if (TREE_CODE (arg0) == code
11926 && 0 != (tem = fold_truthop (loc, code, type,
11927 TREE_OPERAND (arg0, 1), arg1)))
11928 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
11930 if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
11931 return tem;
11933 return NULL_TREE;
11935 case TRUTH_ORIF_EXPR:
11936 /* Note that the operands of this must be ints
11937 and their values must be 0 or true.
11938 ("true" is a fixed value perhaps depending on the language.) */
11939 /* If first arg is constant true, return it. */
11940 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11941 return fold_convert_loc (loc, type, arg0);
11942 case TRUTH_OR_EXPR:
11943 /* If either arg is constant zero, drop it. */
11944 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
11945 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11946 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
11947 /* Preserve sequence points. */
11948 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11949 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11950 /* If second arg is constant true, result is true, but we must
11951 evaluate first arg. */
11952 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
11953 return omit_one_operand_loc (loc, type, arg1, arg0);
11954 /* Likewise for first arg, but note this only occurs here for
11955 TRUTH_OR_EXPR. */
11956 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11957 return omit_one_operand_loc (loc, type, arg0, arg1);
11959 /* !X || X is always true. */
11960 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11961 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11962 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
11963 /* X || !X is always true. */
11964 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11965 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11966 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
11968 goto truth_andor;
11970 case TRUTH_XOR_EXPR:
11971 /* If the second arg is constant zero, drop it. */
11972 if (integer_zerop (arg1))
11973 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11974 /* If the second arg is constant true, this is a logical inversion. */
11975 if (integer_onep (arg1))
11977 /* Only call invert_truthvalue if operand is a truth value. */
11978 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
11979 tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
11980 else
11981 tem = invert_truthvalue_loc (loc, arg0);
11982 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
11984 /* Identical arguments cancel to zero. */
11985 if (operand_equal_p (arg0, arg1, 0))
11986 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11988 /* !X ^ X is always true. */
11989 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11990 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11991 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
11993 /* X ^ !X is always true. */
11994 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11995 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11996 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
11998 return NULL_TREE;
12000 case EQ_EXPR:
12001 case NE_EXPR:
12002 tem = fold_comparison (loc, code, type, op0, op1);
12003 if (tem != NULL_TREE)
12004 return tem;
12006 /* bool_var != 0 becomes bool_var. */
12007 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12008 && code == NE_EXPR)
12009 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12011 /* bool_var == 1 becomes bool_var. */
12012 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12013 && code == EQ_EXPR)
12014 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12016 /* bool_var != 1 becomes !bool_var. */
12017 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12018 && code == NE_EXPR)
12019 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12020 fold_convert_loc (loc, type, arg0));
12022 /* bool_var == 0 becomes !bool_var. */
12023 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12024 && code == EQ_EXPR)
12025 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12026 fold_convert_loc (loc, type, arg0));
12028 /* !exp != 0 becomes !exp */
12029 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12030 && code == NE_EXPR)
12031 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12033 /* If this is an equality comparison of the address of two non-weak,
12034 unaliased symbols neither of which are extern (since we do not
12035 have access to attributes for externs), then we know the result. */
12036 if (TREE_CODE (arg0) == ADDR_EXPR
12037 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12038 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12039 && ! lookup_attribute ("alias",
12040 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12041 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12042 && TREE_CODE (arg1) == ADDR_EXPR
12043 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12044 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12045 && ! lookup_attribute ("alias",
12046 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12047 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12049 /* We know that we're looking at the address of two
12050 non-weak, unaliased, static _DECL nodes.
12052 It is both wasteful and incorrect to call operand_equal_p
12053 to compare the two ADDR_EXPR nodes. It is wasteful in that
12054 all we need to do is test pointer equality for the arguments
12055 to the two ADDR_EXPR nodes. It is incorrect to use
12056 operand_equal_p as that function is NOT equivalent to a
12057 C equality test. It can in fact return false for two
12058 objects which would test as equal using the C equality
12059 operator. */
12060 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12061 return constant_boolean_node (equal
12062 ? code == EQ_EXPR : code != EQ_EXPR,
12063 type);
12066 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12067 a MINUS_EXPR of a constant, we can convert it into a comparison with
12068 a revised constant as long as no overflow occurs. */
12069 if (TREE_CODE (arg1) == INTEGER_CST
12070 && (TREE_CODE (arg0) == PLUS_EXPR
12071 || TREE_CODE (arg0) == MINUS_EXPR)
12072 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12073 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12074 ? MINUS_EXPR : PLUS_EXPR,
12075 fold_convert_loc (loc, TREE_TYPE (arg0),
12076 arg1),
12077 TREE_OPERAND (arg0, 1), 0))
12078 && !TREE_OVERFLOW (tem))
12079 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12081 /* Similarly for a NEGATE_EXPR. */
12082 if (TREE_CODE (arg0) == NEGATE_EXPR
12083 && TREE_CODE (arg1) == INTEGER_CST
12084 && 0 != (tem = negate_expr (arg1))
12085 && TREE_CODE (tem) == INTEGER_CST
12086 && !TREE_OVERFLOW (tem))
12087 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12089 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12090 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12091 && TREE_CODE (arg1) == INTEGER_CST
12092 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12093 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12094 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12095 fold_convert_loc (loc,
12096 TREE_TYPE (arg0),
12097 arg1),
12098 TREE_OPERAND (arg0, 1)));
12100 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
12101 if ((TREE_CODE (arg0) == PLUS_EXPR
12102 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12103 || TREE_CODE (arg0) == MINUS_EXPR)
12104 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12105 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12106 || POINTER_TYPE_P (TREE_TYPE (arg0))))
12108 tree val = TREE_OPERAND (arg0, 1);
12109 return omit_two_operands_loc (loc, type,
12110 fold_build2_loc (loc, code, type,
12111 val,
12112 build_int_cst (TREE_TYPE (val),
12113 0)),
12114 TREE_OPERAND (arg0, 0), arg1);
12117 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
12118 if (TREE_CODE (arg0) == MINUS_EXPR
12119 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12120 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12121 && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12123 return omit_two_operands_loc (loc, type,
12124 code == NE_EXPR
12125 ? boolean_true_node : boolean_false_node,
12126 TREE_OPERAND (arg0, 1), arg1);
12129 /* If we have X - Y == 0, we can convert that to X == Y and similarly
12130 for !=. Don't do this for ordered comparisons due to overflow. */
12131 if (TREE_CODE (arg0) == MINUS_EXPR
12132 && integer_zerop (arg1))
12133 return fold_build2_loc (loc, code, type,
12134 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12136 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12137 if (TREE_CODE (arg0) == ABS_EXPR
12138 && (integer_zerop (arg1) || real_zerop (arg1)))
12139 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12141 /* If this is an EQ or NE comparison with zero and ARG0 is
12142 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12143 two operations, but the latter can be done in one less insn
12144 on machines that have only two-operand insns or on which a
12145 constant cannot be the first operand. */
12146 if (TREE_CODE (arg0) == BIT_AND_EXPR
12147 && integer_zerop (arg1))
12149 tree arg00 = TREE_OPERAND (arg0, 0);
12150 tree arg01 = TREE_OPERAND (arg0, 1);
12151 if (TREE_CODE (arg00) == LSHIFT_EXPR
12152 && integer_onep (TREE_OPERAND (arg00, 0)))
12154 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12155 arg01, TREE_OPERAND (arg00, 1));
12156 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12157 build_int_cst (TREE_TYPE (arg0), 1));
12158 return fold_build2_loc (loc, code, type,
12159 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12160 arg1);
12162 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12163 && integer_onep (TREE_OPERAND (arg01, 0)))
12165 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12166 arg00, TREE_OPERAND (arg01, 1));
12167 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12168 build_int_cst (TREE_TYPE (arg0), 1));
12169 return fold_build2_loc (loc, code, type,
12170 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12171 arg1);
12175 /* If this is an NE or EQ comparison of zero against the result of a
12176 signed MOD operation whose second operand is a power of 2, make
12177 the MOD operation unsigned since it is simpler and equivalent. */
12178 if (integer_zerop (arg1)
12179 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12180 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12181 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12182 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12183 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12184 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12186 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12187 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12188 fold_convert_loc (loc, newtype,
12189 TREE_OPERAND (arg0, 0)),
12190 fold_convert_loc (loc, newtype,
12191 TREE_OPERAND (arg0, 1)));
12193 return fold_build2_loc (loc, code, type, newmod,
12194 fold_convert_loc (loc, newtype, arg1));
12197 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12198 C1 is a valid shift constant, and C2 is a power of two, i.e.
12199 a single bit. */
12200 if (TREE_CODE (arg0) == BIT_AND_EXPR
12201 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12202 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12203 == INTEGER_CST
12204 && integer_pow2p (TREE_OPERAND (arg0, 1))
12205 && integer_zerop (arg1))
12207 tree itype = TREE_TYPE (arg0);
12208 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12209 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12211 /* Check for a valid shift count. */
12212 if (TREE_INT_CST_HIGH (arg001) == 0
12213 && TREE_INT_CST_LOW (arg001) < prec)
12215 tree arg01 = TREE_OPERAND (arg0, 1);
12216 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12217 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12218 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12219 can be rewritten as (X & (C2 << C1)) != 0. */
12220 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12222 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12223 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12224 return fold_build2_loc (loc, code, type, tem, arg1);
12226 /* Otherwise, for signed (arithmetic) shifts,
12227 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12228 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12229 else if (!TYPE_UNSIGNED (itype))
12230 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12231 arg000, build_int_cst (itype, 0));
12232 /* Otherwise, of unsigned (logical) shifts,
12233 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12234 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12235 else
12236 return omit_one_operand_loc (loc, type,
12237 code == EQ_EXPR ? integer_one_node
12238 : integer_zero_node,
12239 arg000);
12243 /* If this is an NE comparison of zero with an AND of one, remove the
12244 comparison since the AND will give the correct value. */
12245 if (code == NE_EXPR
12246 && integer_zerop (arg1)
12247 && TREE_CODE (arg0) == BIT_AND_EXPR
12248 && integer_onep (TREE_OPERAND (arg0, 1)))
12249 return fold_convert_loc (loc, type, arg0);
12251 /* If we have (A & C) == C where C is a power of 2, convert this into
12252 (A & C) != 0. Similarly for NE_EXPR. */
12253 if (TREE_CODE (arg0) == BIT_AND_EXPR
12254 && integer_pow2p (TREE_OPERAND (arg0, 1))
12255 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12256 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12257 arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12258 integer_zero_node));
12260 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12261 bit, then fold the expression into A < 0 or A >= 0. */
12262 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12263 if (tem)
12264 return tem;
12266 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12267 Similarly for NE_EXPR. */
12268 if (TREE_CODE (arg0) == BIT_AND_EXPR
12269 && TREE_CODE (arg1) == INTEGER_CST
12270 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12272 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12273 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12274 TREE_OPERAND (arg0, 1));
12275 tree dandnotc = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12276 arg1, notc);
12277 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12278 if (integer_nonzerop (dandnotc))
12279 return omit_one_operand_loc (loc, type, rslt, arg0);
12282 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12283 Similarly for NE_EXPR. */
12284 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12285 && TREE_CODE (arg1) == INTEGER_CST
12286 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12288 tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12289 tree candnotd = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12290 TREE_OPERAND (arg0, 1), notd);
12291 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12292 if (integer_nonzerop (candnotd))
12293 return omit_one_operand_loc (loc, type, rslt, arg0);
12296 /* If this is a comparison of a field, we may be able to simplify it. */
12297 if ((TREE_CODE (arg0) == COMPONENT_REF
12298 || TREE_CODE (arg0) == BIT_FIELD_REF)
12299 /* Handle the constant case even without -O
12300 to make sure the warnings are given. */
12301 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12303 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12304 if (t1)
12305 return t1;
12308 /* Optimize comparisons of strlen vs zero to a compare of the
12309 first character of the string vs zero. To wit,
12310 strlen(ptr) == 0 => *ptr == 0
12311 strlen(ptr) != 0 => *ptr != 0
12312 Other cases should reduce to one of these two (or a constant)
12313 due to the return value of strlen being unsigned. */
12314 if (TREE_CODE (arg0) == CALL_EXPR
12315 && integer_zerop (arg1))
12317 tree fndecl = get_callee_fndecl (arg0);
12319 if (fndecl
12320 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12321 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12322 && call_expr_nargs (arg0) == 1
12323 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12325 tree iref = build_fold_indirect_ref_loc (loc,
12326 CALL_EXPR_ARG (arg0, 0));
12327 return fold_build2_loc (loc, code, type, iref,
12328 build_int_cst (TREE_TYPE (iref), 0));
12332 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12333 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12334 if (TREE_CODE (arg0) == RSHIFT_EXPR
12335 && integer_zerop (arg1)
12336 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12338 tree arg00 = TREE_OPERAND (arg0, 0);
12339 tree arg01 = TREE_OPERAND (arg0, 1);
12340 tree itype = TREE_TYPE (arg00);
12341 if (TREE_INT_CST_HIGH (arg01) == 0
12342 && TREE_INT_CST_LOW (arg01)
12343 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12345 if (TYPE_UNSIGNED (itype))
12347 itype = signed_type_for (itype);
12348 arg00 = fold_convert_loc (loc, itype, arg00);
12350 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12351 type, arg00, build_int_cst (itype, 0));
12355 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
12356 if (integer_zerop (arg1)
12357 && TREE_CODE (arg0) == BIT_XOR_EXPR)
12358 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12359 TREE_OPERAND (arg0, 1));
12361 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
12362 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12363 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12364 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12365 build_int_cst (TREE_TYPE (arg1), 0));
12366 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
12367 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12368 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12369 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12370 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12371 build_int_cst (TREE_TYPE (arg1), 0));
12373 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
12374 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12375 && TREE_CODE (arg1) == INTEGER_CST
12376 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12377 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12378 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12379 TREE_OPERAND (arg0, 1), arg1));
12381 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12382 (X & C) == 0 when C is a single bit. */
12383 if (TREE_CODE (arg0) == BIT_AND_EXPR
12384 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12385 && integer_zerop (arg1)
12386 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12388 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12389 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12390 TREE_OPERAND (arg0, 1));
12391 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12392 type, tem, arg1);
12395 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12396 constant C is a power of two, i.e. a single bit. */
12397 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12398 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12399 && integer_zerop (arg1)
12400 && integer_pow2p (TREE_OPERAND (arg0, 1))
12401 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12402 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12404 tree arg00 = TREE_OPERAND (arg0, 0);
12405 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12406 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12409 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12410 when is C is a power of two, i.e. a single bit. */
12411 if (TREE_CODE (arg0) == BIT_AND_EXPR
12412 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12413 && integer_zerop (arg1)
12414 && integer_pow2p (TREE_OPERAND (arg0, 1))
12415 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12416 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12418 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12419 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12420 arg000, TREE_OPERAND (arg0, 1));
12421 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12422 tem, build_int_cst (TREE_TYPE (tem), 0));
12425 if (integer_zerop (arg1)
12426 && tree_expr_nonzero_p (arg0))
12428 tree res = constant_boolean_node (code==NE_EXPR, type);
12429 return omit_one_operand_loc (loc, type, res, arg0);
12432 /* Fold -X op -Y as X op Y, where op is eq/ne. */
12433 if (TREE_CODE (arg0) == NEGATE_EXPR
12434 && TREE_CODE (arg1) == NEGATE_EXPR)
12435 return fold_build2_loc (loc, code, type,
12436 TREE_OPERAND (arg0, 0),
12437 TREE_OPERAND (arg1, 0));
12439 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12440 if (TREE_CODE (arg0) == BIT_AND_EXPR
12441 && TREE_CODE (arg1) == BIT_AND_EXPR)
12443 tree arg00 = TREE_OPERAND (arg0, 0);
12444 tree arg01 = TREE_OPERAND (arg0, 1);
12445 tree arg10 = TREE_OPERAND (arg1, 0);
12446 tree arg11 = TREE_OPERAND (arg1, 1);
12447 tree itype = TREE_TYPE (arg0);
12449 if (operand_equal_p (arg01, arg11, 0))
12450 return fold_build2_loc (loc, code, type,
12451 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12452 fold_build2_loc (loc,
12453 BIT_XOR_EXPR, itype,
12454 arg00, arg10),
12455 arg01),
12456 build_int_cst (itype, 0));
12458 if (operand_equal_p (arg01, arg10, 0))
12459 return fold_build2_loc (loc, code, type,
12460 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12461 fold_build2_loc (loc,
12462 BIT_XOR_EXPR, itype,
12463 arg00, arg11),
12464 arg01),
12465 build_int_cst (itype, 0));
12467 if (operand_equal_p (arg00, arg11, 0))
12468 return fold_build2_loc (loc, code, type,
12469 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12470 fold_build2_loc (loc,
12471 BIT_XOR_EXPR, itype,
12472 arg01, arg10),
12473 arg00),
12474 build_int_cst (itype, 0));
12476 if (operand_equal_p (arg00, arg10, 0))
12477 return fold_build2_loc (loc, code, type,
12478 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12479 fold_build2_loc (loc,
12480 BIT_XOR_EXPR, itype,
12481 arg01, arg11),
12482 arg00),
12483 build_int_cst (itype, 0));
12486 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12487 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12489 tree arg00 = TREE_OPERAND (arg0, 0);
12490 tree arg01 = TREE_OPERAND (arg0, 1);
12491 tree arg10 = TREE_OPERAND (arg1, 0);
12492 tree arg11 = TREE_OPERAND (arg1, 1);
12493 tree itype = TREE_TYPE (arg0);
12495 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12496 operand_equal_p guarantees no side-effects so we don't need
12497 to use omit_one_operand on Z. */
12498 if (operand_equal_p (arg01, arg11, 0))
12499 return fold_build2_loc (loc, code, type, arg00, arg10);
12500 if (operand_equal_p (arg01, arg10, 0))
12501 return fold_build2_loc (loc, code, type, arg00, arg11);
12502 if (operand_equal_p (arg00, arg11, 0))
12503 return fold_build2_loc (loc, code, type, arg01, arg10);
12504 if (operand_equal_p (arg00, arg10, 0))
12505 return fold_build2_loc (loc, code, type, arg01, arg11);
12507 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12508 if (TREE_CODE (arg01) == INTEGER_CST
12509 && TREE_CODE (arg11) == INTEGER_CST)
12510 return fold_build2_loc (loc, code, type,
12511 fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00,
12512 fold_build2_loc (loc,
12513 BIT_XOR_EXPR, itype,
12514 arg01, arg11)),
12515 arg10);
12518 /* Attempt to simplify equality/inequality comparisons of complex
12519 values. Only lower the comparison if the result is known or
12520 can be simplified to a single scalar comparison. */
12521 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12522 || TREE_CODE (arg0) == COMPLEX_CST)
12523 && (TREE_CODE (arg1) == COMPLEX_EXPR
12524 || TREE_CODE (arg1) == COMPLEX_CST))
12526 tree real0, imag0, real1, imag1;
12527 tree rcond, icond;
12529 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12531 real0 = TREE_OPERAND (arg0, 0);
12532 imag0 = TREE_OPERAND (arg0, 1);
12534 else
12536 real0 = TREE_REALPART (arg0);
12537 imag0 = TREE_IMAGPART (arg0);
12540 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12542 real1 = TREE_OPERAND (arg1, 0);
12543 imag1 = TREE_OPERAND (arg1, 1);
12545 else
12547 real1 = TREE_REALPART (arg1);
12548 imag1 = TREE_IMAGPART (arg1);
12551 rcond = fold_binary_loc (loc, code, type, real0, real1);
12552 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12554 if (integer_zerop (rcond))
12556 if (code == EQ_EXPR)
12557 return omit_two_operands_loc (loc, type, boolean_false_node,
12558 imag0, imag1);
12559 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12561 else
12563 if (code == NE_EXPR)
12564 return omit_two_operands_loc (loc, type, boolean_true_node,
12565 imag0, imag1);
12566 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12570 icond = fold_binary_loc (loc, code, type, imag0, imag1);
12571 if (icond && TREE_CODE (icond) == INTEGER_CST)
12573 if (integer_zerop (icond))
12575 if (code == EQ_EXPR)
12576 return omit_two_operands_loc (loc, type, boolean_false_node,
12577 real0, real1);
12578 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12580 else
12582 if (code == NE_EXPR)
12583 return omit_two_operands_loc (loc, type, boolean_true_node,
12584 real0, real1);
12585 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12590 return NULL_TREE;
12592 case LT_EXPR:
12593 case GT_EXPR:
12594 case LE_EXPR:
12595 case GE_EXPR:
12596 tem = fold_comparison (loc, code, type, op0, op1);
12597 if (tem != NULL_TREE)
12598 return tem;
12600 /* Transform comparisons of the form X +- C CMP X. */
12601 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12602 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12603 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12604 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12605 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12606 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12608 tree arg01 = TREE_OPERAND (arg0, 1);
12609 enum tree_code code0 = TREE_CODE (arg0);
12610 int is_positive;
12612 if (TREE_CODE (arg01) == REAL_CST)
12613 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12614 else
12615 is_positive = tree_int_cst_sgn (arg01);
12617 /* (X - c) > X becomes false. */
12618 if (code == GT_EXPR
12619 && ((code0 == MINUS_EXPR && is_positive >= 0)
12620 || (code0 == PLUS_EXPR && is_positive <= 0)))
12622 if (TREE_CODE (arg01) == INTEGER_CST
12623 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12624 fold_overflow_warning (("assuming signed overflow does not "
12625 "occur when assuming that (X - c) > X "
12626 "is always false"),
12627 WARN_STRICT_OVERFLOW_ALL);
12628 return constant_boolean_node (0, type);
12631 /* Likewise (X + c) < X becomes false. */
12632 if (code == LT_EXPR
12633 && ((code0 == PLUS_EXPR && is_positive >= 0)
12634 || (code0 == MINUS_EXPR && is_positive <= 0)))
12636 if (TREE_CODE (arg01) == INTEGER_CST
12637 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12638 fold_overflow_warning (("assuming signed overflow does not "
12639 "occur when assuming that "
12640 "(X + c) < X is always false"),
12641 WARN_STRICT_OVERFLOW_ALL);
12642 return constant_boolean_node (0, type);
12645 /* Convert (X - c) <= X to true. */
12646 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12647 && code == LE_EXPR
12648 && ((code0 == MINUS_EXPR && is_positive >= 0)
12649 || (code0 == PLUS_EXPR && is_positive <= 0)))
12651 if (TREE_CODE (arg01) == INTEGER_CST
12652 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12653 fold_overflow_warning (("assuming signed overflow does not "
12654 "occur when assuming that "
12655 "(X - c) <= X is always true"),
12656 WARN_STRICT_OVERFLOW_ALL);
12657 return constant_boolean_node (1, type);
12660 /* Convert (X + c) >= X to true. */
12661 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12662 && code == GE_EXPR
12663 && ((code0 == PLUS_EXPR && is_positive >= 0)
12664 || (code0 == MINUS_EXPR && is_positive <= 0)))
12666 if (TREE_CODE (arg01) == INTEGER_CST
12667 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12668 fold_overflow_warning (("assuming signed overflow does not "
12669 "occur when assuming that "
12670 "(X + c) >= X is always true"),
12671 WARN_STRICT_OVERFLOW_ALL);
12672 return constant_boolean_node (1, type);
12675 if (TREE_CODE (arg01) == INTEGER_CST)
12677 /* Convert X + c > X and X - c < X to true for integers. */
12678 if (code == GT_EXPR
12679 && ((code0 == PLUS_EXPR && is_positive > 0)
12680 || (code0 == MINUS_EXPR && is_positive < 0)))
12682 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12683 fold_overflow_warning (("assuming signed overflow does "
12684 "not occur when assuming that "
12685 "(X + c) > X is always true"),
12686 WARN_STRICT_OVERFLOW_ALL);
12687 return constant_boolean_node (1, type);
12690 if (code == LT_EXPR
12691 && ((code0 == MINUS_EXPR && is_positive > 0)
12692 || (code0 == PLUS_EXPR && is_positive < 0)))
12694 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12695 fold_overflow_warning (("assuming signed overflow does "
12696 "not occur when assuming that "
12697 "(X - c) < X is always true"),
12698 WARN_STRICT_OVERFLOW_ALL);
12699 return constant_boolean_node (1, type);
12702 /* Convert X + c <= X and X - c >= X to false for integers. */
12703 if (code == LE_EXPR
12704 && ((code0 == PLUS_EXPR && is_positive > 0)
12705 || (code0 == MINUS_EXPR && is_positive < 0)))
12707 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12708 fold_overflow_warning (("assuming signed overflow does "
12709 "not occur when assuming that "
12710 "(X + c) <= X is always false"),
12711 WARN_STRICT_OVERFLOW_ALL);
12712 return constant_boolean_node (0, type);
12715 if (code == GE_EXPR
12716 && ((code0 == MINUS_EXPR && is_positive > 0)
12717 || (code0 == PLUS_EXPR && is_positive < 0)))
12719 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12720 fold_overflow_warning (("assuming signed overflow does "
12721 "not occur when assuming that "
12722 "(X - c) >= X is always false"),
12723 WARN_STRICT_OVERFLOW_ALL);
12724 return constant_boolean_node (0, type);
12729 /* Comparisons with the highest or lowest possible integer of
12730 the specified precision will have known values. */
12732 tree arg1_type = TREE_TYPE (arg1);
12733 unsigned int width = TYPE_PRECISION (arg1_type);
12735 if (TREE_CODE (arg1) == INTEGER_CST
12736 && width <= 2 * HOST_BITS_PER_WIDE_INT
12737 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12739 HOST_WIDE_INT signed_max_hi;
12740 unsigned HOST_WIDE_INT signed_max_lo;
12741 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12743 if (width <= HOST_BITS_PER_WIDE_INT)
12745 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12746 - 1;
12747 signed_max_hi = 0;
12748 max_hi = 0;
12750 if (TYPE_UNSIGNED (arg1_type))
12752 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12753 min_lo = 0;
12754 min_hi = 0;
12756 else
12758 max_lo = signed_max_lo;
12759 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12760 min_hi = -1;
12763 else
12765 width -= HOST_BITS_PER_WIDE_INT;
12766 signed_max_lo = -1;
12767 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12768 - 1;
12769 max_lo = -1;
12770 min_lo = 0;
12772 if (TYPE_UNSIGNED (arg1_type))
12774 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12775 min_hi = 0;
12777 else
12779 max_hi = signed_max_hi;
12780 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12784 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12785 && TREE_INT_CST_LOW (arg1) == max_lo)
12786 switch (code)
12788 case GT_EXPR:
12789 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12791 case GE_EXPR:
12792 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12794 case LE_EXPR:
12795 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12797 case LT_EXPR:
12798 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12800 /* The GE_EXPR and LT_EXPR cases above are not normally
12801 reached because of previous transformations. */
12803 default:
12804 break;
12806 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12807 == max_hi
12808 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12809 switch (code)
12811 case GT_EXPR:
12812 arg1 = const_binop (PLUS_EXPR, arg1,
12813 build_int_cst (TREE_TYPE (arg1), 1), 0);
12814 return fold_build2_loc (loc, EQ_EXPR, type,
12815 fold_convert_loc (loc,
12816 TREE_TYPE (arg1), arg0),
12817 arg1);
12818 case LE_EXPR:
12819 arg1 = const_binop (PLUS_EXPR, arg1,
12820 build_int_cst (TREE_TYPE (arg1), 1), 0);
12821 return fold_build2_loc (loc, NE_EXPR, type,
12822 fold_convert_loc (loc, TREE_TYPE (arg1),
12823 arg0),
12824 arg1);
12825 default:
12826 break;
12828 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12829 == min_hi
12830 && TREE_INT_CST_LOW (arg1) == min_lo)
12831 switch (code)
12833 case LT_EXPR:
12834 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12836 case LE_EXPR:
12837 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12839 case GE_EXPR:
12840 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12842 case GT_EXPR:
12843 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12845 default:
12846 break;
12848 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12849 == min_hi
12850 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12851 switch (code)
12853 case GE_EXPR:
12854 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12855 return fold_build2_loc (loc, NE_EXPR, type,
12856 fold_convert_loc (loc,
12857 TREE_TYPE (arg1), arg0),
12858 arg1);
12859 case LT_EXPR:
12860 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12861 return fold_build2_loc (loc, EQ_EXPR, type,
12862 fold_convert_loc (loc, TREE_TYPE (arg1),
12863 arg0),
12864 arg1);
12865 default:
12866 break;
12869 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12870 && TREE_INT_CST_LOW (arg1) == signed_max_lo
12871 && TYPE_UNSIGNED (arg1_type)
12872 /* We will flip the signedness of the comparison operator
12873 associated with the mode of arg1, so the sign bit is
12874 specified by this mode. Check that arg1 is the signed
12875 max associated with this sign bit. */
12876 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12877 /* signed_type does not work on pointer types. */
12878 && INTEGRAL_TYPE_P (arg1_type))
12880 /* The following case also applies to X < signed_max+1
12881 and X >= signed_max+1 because previous transformations. */
12882 if (code == LE_EXPR || code == GT_EXPR)
12884 tree st;
12885 st = signed_type_for (TREE_TYPE (arg1));
12886 return fold_build2_loc (loc,
12887 code == LE_EXPR ? GE_EXPR : LT_EXPR,
12888 type, fold_convert_loc (loc, st, arg0),
12889 build_int_cst (st, 0));
12895 /* If we are comparing an ABS_EXPR with a constant, we can
12896 convert all the cases into explicit comparisons, but they may
12897 well not be faster than doing the ABS and one comparison.
12898 But ABS (X) <= C is a range comparison, which becomes a subtraction
12899 and a comparison, and is probably faster. */
12900 if (code == LE_EXPR
12901 && TREE_CODE (arg1) == INTEGER_CST
12902 && TREE_CODE (arg0) == ABS_EXPR
12903 && ! TREE_SIDE_EFFECTS (arg0)
12904 && (0 != (tem = negate_expr (arg1)))
12905 && TREE_CODE (tem) == INTEGER_CST
12906 && !TREE_OVERFLOW (tem))
12907 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12908 build2 (GE_EXPR, type,
12909 TREE_OPERAND (arg0, 0), tem),
12910 build2 (LE_EXPR, type,
12911 TREE_OPERAND (arg0, 0), arg1));
12913 /* Convert ABS_EXPR<x> >= 0 to true. */
12914 strict_overflow_p = false;
12915 if (code == GE_EXPR
12916 && (integer_zerop (arg1)
12917 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
12918 && real_zerop (arg1)))
12919 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12921 if (strict_overflow_p)
12922 fold_overflow_warning (("assuming signed overflow does not occur "
12923 "when simplifying comparison of "
12924 "absolute value and zero"),
12925 WARN_STRICT_OVERFLOW_CONDITIONAL);
12926 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12929 /* Convert ABS_EXPR<x> < 0 to false. */
12930 strict_overflow_p = false;
12931 if (code == LT_EXPR
12932 && (integer_zerop (arg1) || real_zerop (arg1))
12933 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12935 if (strict_overflow_p)
12936 fold_overflow_warning (("assuming signed overflow does not occur "
12937 "when simplifying comparison of "
12938 "absolute value and zero"),
12939 WARN_STRICT_OVERFLOW_CONDITIONAL);
12940 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12943 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12944 and similarly for >= into !=. */
12945 if ((code == LT_EXPR || code == GE_EXPR)
12946 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12947 && TREE_CODE (arg1) == LSHIFT_EXPR
12948 && integer_onep (TREE_OPERAND (arg1, 0)))
12950 tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12951 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12952 TREE_OPERAND (arg1, 1)),
12953 build_int_cst (TREE_TYPE (arg0), 0));
12954 goto fold_binary_exit;
12957 if ((code == LT_EXPR || code == GE_EXPR)
12958 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12959 && CONVERT_EXPR_P (arg1)
12960 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12961 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12963 tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12964 fold_convert_loc (loc, TREE_TYPE (arg0),
12965 build2 (RSHIFT_EXPR,
12966 TREE_TYPE (arg0), arg0,
12967 TREE_OPERAND (TREE_OPERAND (arg1, 0),
12968 1))),
12969 build_int_cst (TREE_TYPE (arg0), 0));
12970 goto fold_binary_exit;
12973 return NULL_TREE;
12975 case UNORDERED_EXPR:
12976 case ORDERED_EXPR:
12977 case UNLT_EXPR:
12978 case UNLE_EXPR:
12979 case UNGT_EXPR:
12980 case UNGE_EXPR:
12981 case UNEQ_EXPR:
12982 case LTGT_EXPR:
12983 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
12985 t1 = fold_relational_const (code, type, arg0, arg1);
12986 if (t1 != NULL_TREE)
12987 return t1;
12990 /* If the first operand is NaN, the result is constant. */
12991 if (TREE_CODE (arg0) == REAL_CST
12992 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
12993 && (code != LTGT_EXPR || ! flag_trapping_math))
12995 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12996 ? integer_zero_node
12997 : integer_one_node;
12998 return omit_one_operand_loc (loc, type, t1, arg1);
13001 /* If the second operand is NaN, the result is constant. */
13002 if (TREE_CODE (arg1) == REAL_CST
13003 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13004 && (code != LTGT_EXPR || ! flag_trapping_math))
13006 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13007 ? integer_zero_node
13008 : integer_one_node;
13009 return omit_one_operand_loc (loc, type, t1, arg0);
13012 /* Simplify unordered comparison of something with itself. */
13013 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13014 && operand_equal_p (arg0, arg1, 0))
13015 return constant_boolean_node (1, type);
13017 if (code == LTGT_EXPR
13018 && !flag_trapping_math
13019 && operand_equal_p (arg0, arg1, 0))
13020 return constant_boolean_node (0, type);
13022 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13024 tree targ0 = strip_float_extensions (arg0);
13025 tree targ1 = strip_float_extensions (arg1);
13026 tree newtype = TREE_TYPE (targ0);
13028 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13029 newtype = TREE_TYPE (targ1);
13031 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13032 return fold_build2_loc (loc, code, type,
13033 fold_convert_loc (loc, newtype, targ0),
13034 fold_convert_loc (loc, newtype, targ1));
13037 return NULL_TREE;
13039 case COMPOUND_EXPR:
13040 /* When pedantic, a compound expression can be neither an lvalue
13041 nor an integer constant expression. */
13042 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13043 return NULL_TREE;
13044 /* Don't let (0, 0) be null pointer constant. */
13045 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13046 : fold_convert_loc (loc, type, arg1);
13047 return pedantic_non_lvalue_loc (loc, tem);
13049 case COMPLEX_EXPR:
13050 if ((TREE_CODE (arg0) == REAL_CST
13051 && TREE_CODE (arg1) == REAL_CST)
13052 || (TREE_CODE (arg0) == INTEGER_CST
13053 && TREE_CODE (arg1) == INTEGER_CST))
13054 return build_complex (type, arg0, arg1);
13055 return NULL_TREE;
13057 case ASSERT_EXPR:
13058 /* An ASSERT_EXPR should never be passed to fold_binary. */
13059 gcc_unreachable ();
13061 default:
13062 return NULL_TREE;
13063 } /* switch (code) */
13064 fold_binary_exit:
13065 protected_set_expr_location (tem, loc);
13066 return tem;
13069 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
13070 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
13071 of GOTO_EXPR. */
13073 static tree
13074 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13076 switch (TREE_CODE (*tp))
13078 case LABEL_EXPR:
13079 return *tp;
13081 case GOTO_EXPR:
13082 *walk_subtrees = 0;
13084 /* ... fall through ... */
13086 default:
13087 return NULL_TREE;
13091 /* Return whether the sub-tree ST contains a label which is accessible from
13092 outside the sub-tree. */
13094 static bool
13095 contains_label_p (tree st)
13097 return
13098 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13101 /* Fold a ternary expression of code CODE and type TYPE with operands
13102 OP0, OP1, and OP2. Return the folded expression if folding is
13103 successful. Otherwise, return NULL_TREE. */
13105 tree
13106 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13107 tree op0, tree op1, tree op2)
13109 tree tem;
13110 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13111 enum tree_code_class kind = TREE_CODE_CLASS (code);
13113 gcc_assert (IS_EXPR_CODE_CLASS (kind)
13114 && TREE_CODE_LENGTH (code) == 3);
13116 /* Strip any conversions that don't change the mode. This is safe
13117 for every expression, except for a comparison expression because
13118 its signedness is derived from its operands. So, in the latter
13119 case, only strip conversions that don't change the signedness.
13121 Note that this is done as an internal manipulation within the
13122 constant folder, in order to find the simplest representation of
13123 the arguments so that their form can be studied. In any cases,
13124 the appropriate type conversions should be put back in the tree
13125 that will get out of the constant folder. */
13126 if (op0)
13128 arg0 = op0;
13129 STRIP_NOPS (arg0);
13132 if (op1)
13134 arg1 = op1;
13135 STRIP_NOPS (arg1);
13138 switch (code)
13140 case COMPONENT_REF:
13141 if (TREE_CODE (arg0) == CONSTRUCTOR
13142 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13144 unsigned HOST_WIDE_INT idx;
13145 tree field, value;
13146 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13147 if (field == arg1)
13148 return value;
13150 return NULL_TREE;
13152 case COND_EXPR:
13153 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13154 so all simple results must be passed through pedantic_non_lvalue. */
13155 if (TREE_CODE (arg0) == INTEGER_CST)
13157 tree unused_op = integer_zerop (arg0) ? op1 : op2;
13158 tem = integer_zerop (arg0) ? op2 : op1;
13159 /* Only optimize constant conditions when the selected branch
13160 has the same type as the COND_EXPR. This avoids optimizing
13161 away "c ? x : throw", where the throw has a void type.
13162 Avoid throwing away that operand which contains label. */
13163 if ((!TREE_SIDE_EFFECTS (unused_op)
13164 || !contains_label_p (unused_op))
13165 && (! VOID_TYPE_P (TREE_TYPE (tem))
13166 || VOID_TYPE_P (type)))
13167 return pedantic_non_lvalue_loc (loc, tem);
13168 return NULL_TREE;
13170 if (operand_equal_p (arg1, op2, 0))
13171 return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13173 /* If we have A op B ? A : C, we may be able to convert this to a
13174 simpler expression, depending on the operation and the values
13175 of B and C. Signed zeros prevent all of these transformations,
13176 for reasons given above each one.
13178 Also try swapping the arguments and inverting the conditional. */
13179 if (COMPARISON_CLASS_P (arg0)
13180 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13181 arg1, TREE_OPERAND (arg0, 1))
13182 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13184 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13185 if (tem)
13186 return tem;
13189 if (COMPARISON_CLASS_P (arg0)
13190 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13191 op2,
13192 TREE_OPERAND (arg0, 1))
13193 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13195 tem = fold_truth_not_expr (loc, arg0);
13196 if (tem && COMPARISON_CLASS_P (tem))
13198 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13199 if (tem)
13200 return tem;
13204 /* If the second operand is simpler than the third, swap them
13205 since that produces better jump optimization results. */
13206 if (truth_value_p (TREE_CODE (arg0))
13207 && tree_swap_operands_p (op1, op2, false))
13209 /* See if this can be inverted. If it can't, possibly because
13210 it was a floating-point inequality comparison, don't do
13211 anything. */
13212 tem = fold_truth_not_expr (loc, arg0);
13213 if (tem)
13214 return fold_build3_loc (loc, code, type, tem, op2, op1);
13217 /* Convert A ? 1 : 0 to simply A. */
13218 if (integer_onep (op1)
13219 && integer_zerop (op2)
13220 /* If we try to convert OP0 to our type, the
13221 call to fold will try to move the conversion inside
13222 a COND, which will recurse. In that case, the COND_EXPR
13223 is probably the best choice, so leave it alone. */
13224 && type == TREE_TYPE (arg0))
13225 return pedantic_non_lvalue_loc (loc, arg0);
13227 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13228 over COND_EXPR in cases such as floating point comparisons. */
13229 if (integer_zerop (op1)
13230 && integer_onep (op2)
13231 && truth_value_p (TREE_CODE (arg0)))
13232 return pedantic_non_lvalue_loc (loc,
13233 fold_convert_loc (loc, type,
13234 invert_truthvalue_loc (loc,
13235 arg0)));
13237 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13238 if (TREE_CODE (arg0) == LT_EXPR
13239 && integer_zerop (TREE_OPERAND (arg0, 1))
13240 && integer_zerop (op2)
13241 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13243 /* sign_bit_p only checks ARG1 bits within A's precision.
13244 If <sign bit of A> has wider type than A, bits outside
13245 of A's precision in <sign bit of A> need to be checked.
13246 If they are all 0, this optimization needs to be done
13247 in unsigned A's type, if they are all 1 in signed A's type,
13248 otherwise this can't be done. */
13249 if (TYPE_PRECISION (TREE_TYPE (tem))
13250 < TYPE_PRECISION (TREE_TYPE (arg1))
13251 && TYPE_PRECISION (TREE_TYPE (tem))
13252 < TYPE_PRECISION (type))
13254 unsigned HOST_WIDE_INT mask_lo;
13255 HOST_WIDE_INT mask_hi;
13256 int inner_width, outer_width;
13257 tree tem_type;
13259 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13260 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13261 if (outer_width > TYPE_PRECISION (type))
13262 outer_width = TYPE_PRECISION (type);
13264 if (outer_width > HOST_BITS_PER_WIDE_INT)
13266 mask_hi = ((unsigned HOST_WIDE_INT) -1
13267 >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13268 mask_lo = -1;
13270 else
13272 mask_hi = 0;
13273 mask_lo = ((unsigned HOST_WIDE_INT) -1
13274 >> (HOST_BITS_PER_WIDE_INT - outer_width));
13276 if (inner_width > HOST_BITS_PER_WIDE_INT)
13278 mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13279 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13280 mask_lo = 0;
13282 else
13283 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13284 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13286 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13287 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13289 tem_type = signed_type_for (TREE_TYPE (tem));
13290 tem = fold_convert_loc (loc, tem_type, tem);
13292 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13293 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13295 tem_type = unsigned_type_for (TREE_TYPE (tem));
13296 tem = fold_convert_loc (loc, tem_type, tem);
13298 else
13299 tem = NULL;
13302 if (tem)
13303 return
13304 fold_convert_loc (loc, type,
13305 fold_build2_loc (loc, BIT_AND_EXPR,
13306 TREE_TYPE (tem), tem,
13307 fold_convert_loc (loc,
13308 TREE_TYPE (tem),
13309 arg1)));
13312 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13313 already handled above. */
13314 if (TREE_CODE (arg0) == BIT_AND_EXPR
13315 && integer_onep (TREE_OPERAND (arg0, 1))
13316 && integer_zerop (op2)
13317 && integer_pow2p (arg1))
13319 tree tem = TREE_OPERAND (arg0, 0);
13320 STRIP_NOPS (tem);
13321 if (TREE_CODE (tem) == RSHIFT_EXPR
13322 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13323 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13324 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13325 return fold_build2_loc (loc, BIT_AND_EXPR, type,
13326 TREE_OPERAND (tem, 0), arg1);
13329 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13330 is probably obsolete because the first operand should be a
13331 truth value (that's why we have the two cases above), but let's
13332 leave it in until we can confirm this for all front-ends. */
13333 if (integer_zerop (op2)
13334 && TREE_CODE (arg0) == NE_EXPR
13335 && integer_zerop (TREE_OPERAND (arg0, 1))
13336 && integer_pow2p (arg1)
13337 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13338 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13339 arg1, OEP_ONLY_CONST))
13340 return pedantic_non_lvalue_loc (loc,
13341 fold_convert_loc (loc, type,
13342 TREE_OPERAND (arg0, 0)));
13344 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13345 if (integer_zerop (op2)
13346 && truth_value_p (TREE_CODE (arg0))
13347 && truth_value_p (TREE_CODE (arg1)))
13348 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13349 fold_convert_loc (loc, type, arg0),
13350 arg1);
13352 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13353 if (integer_onep (op2)
13354 && truth_value_p (TREE_CODE (arg0))
13355 && truth_value_p (TREE_CODE (arg1)))
13357 /* Only perform transformation if ARG0 is easily inverted. */
13358 tem = fold_truth_not_expr (loc, arg0);
13359 if (tem)
13360 return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13361 fold_convert_loc (loc, type, tem),
13362 arg1);
13365 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13366 if (integer_zerop (arg1)
13367 && truth_value_p (TREE_CODE (arg0))
13368 && truth_value_p (TREE_CODE (op2)))
13370 /* Only perform transformation if ARG0 is easily inverted. */
13371 tem = fold_truth_not_expr (loc, arg0);
13372 if (tem)
13373 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13374 fold_convert_loc (loc, type, tem),
13375 op2);
13378 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13379 if (integer_onep (arg1)
13380 && truth_value_p (TREE_CODE (arg0))
13381 && truth_value_p (TREE_CODE (op2)))
13382 return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13383 fold_convert_loc (loc, type, arg0),
13384 op2);
13386 return NULL_TREE;
13388 case CALL_EXPR:
13389 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13390 of fold_ternary on them. */
13391 gcc_unreachable ();
13393 case BIT_FIELD_REF:
13394 if ((TREE_CODE (arg0) == VECTOR_CST
13395 || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13396 && type == TREE_TYPE (TREE_TYPE (arg0)))
13398 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13399 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13401 if (width != 0
13402 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13403 && (idx % width) == 0
13404 && (idx = idx / width)
13405 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13407 tree elements = NULL_TREE;
13409 if (TREE_CODE (arg0) == VECTOR_CST)
13410 elements = TREE_VECTOR_CST_ELTS (arg0);
13411 else
13413 unsigned HOST_WIDE_INT idx;
13414 tree value;
13416 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13417 elements = tree_cons (NULL_TREE, value, elements);
13419 while (idx-- > 0 && elements)
13420 elements = TREE_CHAIN (elements);
13421 if (elements)
13422 return TREE_VALUE (elements);
13423 else
13424 return fold_convert_loc (loc, type, integer_zero_node);
13428 /* A bit-field-ref that referenced the full argument can be stripped. */
13429 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13430 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13431 && integer_zerop (op2))
13432 return fold_convert_loc (loc, type, arg0);
13434 return NULL_TREE;
13436 default:
13437 return NULL_TREE;
13438 } /* switch (code) */
13441 /* Perform constant folding and related simplification of EXPR.
13442 The related simplifications include x*1 => x, x*0 => 0, etc.,
13443 and application of the associative law.
13444 NOP_EXPR conversions may be removed freely (as long as we
13445 are careful not to change the type of the overall expression).
13446 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13447 but we can constant-fold them if they have constant operands. */
13449 #ifdef ENABLE_FOLD_CHECKING
13450 # define fold(x) fold_1 (x)
13451 static tree fold_1 (tree);
13452 static
13453 #endif
13454 tree
13455 fold (tree expr)
13457 const tree t = expr;
13458 enum tree_code code = TREE_CODE (t);
13459 enum tree_code_class kind = TREE_CODE_CLASS (code);
13460 tree tem;
13461 location_t loc = EXPR_LOCATION (expr);
13463 /* Return right away if a constant. */
13464 if (kind == tcc_constant)
13465 return t;
13467 /* CALL_EXPR-like objects with variable numbers of operands are
13468 treated specially. */
13469 if (kind == tcc_vl_exp)
13471 if (code == CALL_EXPR)
13473 tem = fold_call_expr (loc, expr, false);
13474 return tem ? tem : expr;
13476 return expr;
13479 if (IS_EXPR_CODE_CLASS (kind))
13481 tree type = TREE_TYPE (t);
13482 tree op0, op1, op2;
13484 switch (TREE_CODE_LENGTH (code))
13486 case 1:
13487 op0 = TREE_OPERAND (t, 0);
13488 tem = fold_unary_loc (loc, code, type, op0);
13489 return tem ? tem : expr;
13490 case 2:
13491 op0 = TREE_OPERAND (t, 0);
13492 op1 = TREE_OPERAND (t, 1);
13493 tem = fold_binary_loc (loc, code, type, op0, op1);
13494 return tem ? tem : expr;
13495 case 3:
13496 op0 = TREE_OPERAND (t, 0);
13497 op1 = TREE_OPERAND (t, 1);
13498 op2 = TREE_OPERAND (t, 2);
13499 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13500 return tem ? tem : expr;
13501 default:
13502 break;
13506 switch (code)
13508 case ARRAY_REF:
13510 tree op0 = TREE_OPERAND (t, 0);
13511 tree op1 = TREE_OPERAND (t, 1);
13513 if (TREE_CODE (op1) == INTEGER_CST
13514 && TREE_CODE (op0) == CONSTRUCTOR
13515 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13517 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13518 unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13519 unsigned HOST_WIDE_INT begin = 0;
13521 /* Find a matching index by means of a binary search. */
13522 while (begin != end)
13524 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13525 tree index = VEC_index (constructor_elt, elts, middle)->index;
13527 if (TREE_CODE (index) == INTEGER_CST
13528 && tree_int_cst_lt (index, op1))
13529 begin = middle + 1;
13530 else if (TREE_CODE (index) == INTEGER_CST
13531 && tree_int_cst_lt (op1, index))
13532 end = middle;
13533 else if (TREE_CODE (index) == RANGE_EXPR
13534 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13535 begin = middle + 1;
13536 else if (TREE_CODE (index) == RANGE_EXPR
13537 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13538 end = middle;
13539 else
13540 return VEC_index (constructor_elt, elts, middle)->value;
13544 return t;
13547 case CONST_DECL:
13548 return fold (DECL_INITIAL (t));
13550 default:
13551 return t;
13552 } /* switch (code) */
13555 #ifdef ENABLE_FOLD_CHECKING
13556 #undef fold
13558 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13559 static void fold_check_failed (const_tree, const_tree);
13560 void print_fold_checksum (const_tree);
13562 /* When --enable-checking=fold, compute a digest of expr before
13563 and after actual fold call to see if fold did not accidentally
13564 change original expr. */
13566 tree
13567 fold (tree expr)
13569 tree ret;
13570 struct md5_ctx ctx;
13571 unsigned char checksum_before[16], checksum_after[16];
13572 htab_t ht;
13574 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13575 md5_init_ctx (&ctx);
13576 fold_checksum_tree (expr, &ctx, ht);
13577 md5_finish_ctx (&ctx, checksum_before);
13578 htab_empty (ht);
13580 ret = fold_1 (expr);
13582 md5_init_ctx (&ctx);
13583 fold_checksum_tree (expr, &ctx, ht);
13584 md5_finish_ctx (&ctx, checksum_after);
13585 htab_delete (ht);
13587 if (memcmp (checksum_before, checksum_after, 16))
13588 fold_check_failed (expr, ret);
13590 return ret;
13593 void
13594 print_fold_checksum (const_tree expr)
13596 struct md5_ctx ctx;
13597 unsigned char checksum[16], cnt;
13598 htab_t ht;
13600 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13601 md5_init_ctx (&ctx);
13602 fold_checksum_tree (expr, &ctx, ht);
13603 md5_finish_ctx (&ctx, checksum);
13604 htab_delete (ht);
13605 for (cnt = 0; cnt < 16; ++cnt)
13606 fprintf (stderr, "%02x", checksum[cnt]);
13607 putc ('\n', stderr);
13610 static void
13611 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13613 internal_error ("fold check: original tree changed by fold");
13616 static void
13617 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13619 const void **slot;
13620 enum tree_code code;
13621 union tree_node buf;
13622 int i, len;
13624 recursive_label:
13626 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13627 <= sizeof (struct tree_function_decl))
13628 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13629 if (expr == NULL)
13630 return;
13631 slot = (const void **) htab_find_slot (ht, expr, INSERT);
13632 if (*slot != NULL)
13633 return;
13634 *slot = expr;
13635 code = TREE_CODE (expr);
13636 if (TREE_CODE_CLASS (code) == tcc_declaration
13637 && DECL_ASSEMBLER_NAME_SET_P (expr))
13639 /* Allow DECL_ASSEMBLER_NAME to be modified. */
13640 memcpy ((char *) &buf, expr, tree_size (expr));
13641 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13642 expr = (tree) &buf;
13644 else if (TREE_CODE_CLASS (code) == tcc_type
13645 && (TYPE_POINTER_TO (expr)
13646 || TYPE_REFERENCE_TO (expr)
13647 || TYPE_CACHED_VALUES_P (expr)
13648 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13649 || TYPE_NEXT_VARIANT (expr)))
13651 /* Allow these fields to be modified. */
13652 tree tmp;
13653 memcpy ((char *) &buf, expr, tree_size (expr));
13654 expr = tmp = (tree) &buf;
13655 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13656 TYPE_POINTER_TO (tmp) = NULL;
13657 TYPE_REFERENCE_TO (tmp) = NULL;
13658 TYPE_NEXT_VARIANT (tmp) = NULL;
13659 if (TYPE_CACHED_VALUES_P (tmp))
13661 TYPE_CACHED_VALUES_P (tmp) = 0;
13662 TYPE_CACHED_VALUES (tmp) = NULL;
13665 md5_process_bytes (expr, tree_size (expr), ctx);
13666 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13667 if (TREE_CODE_CLASS (code) != tcc_type
13668 && TREE_CODE_CLASS (code) != tcc_declaration
13669 && code != TREE_LIST
13670 && code != SSA_NAME)
13671 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13672 switch (TREE_CODE_CLASS (code))
13674 case tcc_constant:
13675 switch (code)
13677 case STRING_CST:
13678 md5_process_bytes (TREE_STRING_POINTER (expr),
13679 TREE_STRING_LENGTH (expr), ctx);
13680 break;
13681 case COMPLEX_CST:
13682 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13683 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13684 break;
13685 case VECTOR_CST:
13686 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13687 break;
13688 default:
13689 break;
13691 break;
13692 case tcc_exceptional:
13693 switch (code)
13695 case TREE_LIST:
13696 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13697 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13698 expr = TREE_CHAIN (expr);
13699 goto recursive_label;
13700 break;
13701 case TREE_VEC:
13702 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13703 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13704 break;
13705 default:
13706 break;
13708 break;
13709 case tcc_expression:
13710 case tcc_reference:
13711 case tcc_comparison:
13712 case tcc_unary:
13713 case tcc_binary:
13714 case tcc_statement:
13715 case tcc_vl_exp:
13716 len = TREE_OPERAND_LENGTH (expr);
13717 for (i = 0; i < len; ++i)
13718 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13719 break;
13720 case tcc_declaration:
13721 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13722 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13723 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13725 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13726 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13727 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13728 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13729 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13731 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13732 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13734 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13736 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13737 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13738 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13740 break;
13741 case tcc_type:
13742 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13743 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13744 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13745 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13746 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13747 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13748 if (INTEGRAL_TYPE_P (expr)
13749 || SCALAR_FLOAT_TYPE_P (expr))
13751 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13752 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13754 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13755 if (TREE_CODE (expr) == RECORD_TYPE
13756 || TREE_CODE (expr) == UNION_TYPE
13757 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13758 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13759 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13760 break;
13761 default:
13762 break;
13766 /* Helper function for outputting the checksum of a tree T. When
13767 debugging with gdb, you can "define mynext" to be "next" followed
13768 by "call debug_fold_checksum (op0)", then just trace down till the
13769 outputs differ. */
13771 DEBUG_FUNCTION void
13772 debug_fold_checksum (const_tree t)
13774 int i;
13775 unsigned char checksum[16];
13776 struct md5_ctx ctx;
13777 htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13779 md5_init_ctx (&ctx);
13780 fold_checksum_tree (t, &ctx, ht);
13781 md5_finish_ctx (&ctx, checksum);
13782 htab_empty (ht);
13784 for (i = 0; i < 16; i++)
13785 fprintf (stderr, "%d ", checksum[i]);
13787 fprintf (stderr, "\n");
13790 #endif
13792 /* Fold a unary tree expression with code CODE of type TYPE with an
13793 operand OP0. LOC is the location of the resulting expression.
13794 Return a folded expression if successful. Otherwise, return a tree
13795 expression with code CODE of type TYPE with an operand OP0. */
13797 tree
13798 fold_build1_stat_loc (location_t loc,
13799 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13801 tree tem;
13802 #ifdef ENABLE_FOLD_CHECKING
13803 unsigned char checksum_before[16], checksum_after[16];
13804 struct md5_ctx ctx;
13805 htab_t ht;
13807 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13808 md5_init_ctx (&ctx);
13809 fold_checksum_tree (op0, &ctx, ht);
13810 md5_finish_ctx (&ctx, checksum_before);
13811 htab_empty (ht);
13812 #endif
13814 tem = fold_unary_loc (loc, code, type, op0);
13815 if (!tem)
13817 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13818 SET_EXPR_LOCATION (tem, loc);
13821 #ifdef ENABLE_FOLD_CHECKING
13822 md5_init_ctx (&ctx);
13823 fold_checksum_tree (op0, &ctx, ht);
13824 md5_finish_ctx (&ctx, checksum_after);
13825 htab_delete (ht);
13827 if (memcmp (checksum_before, checksum_after, 16))
13828 fold_check_failed (op0, tem);
13829 #endif
13830 return tem;
13833 /* Fold a binary tree expression with code CODE of type TYPE with
13834 operands OP0 and OP1. LOC is the location of the resulting
13835 expression. Return a folded expression if successful. Otherwise,
13836 return a tree expression with code CODE of type TYPE with operands
13837 OP0 and OP1. */
13839 tree
13840 fold_build2_stat_loc (location_t loc,
13841 enum tree_code code, tree type, tree op0, tree op1
13842 MEM_STAT_DECL)
13844 tree tem;
13845 #ifdef ENABLE_FOLD_CHECKING
13846 unsigned char checksum_before_op0[16],
13847 checksum_before_op1[16],
13848 checksum_after_op0[16],
13849 checksum_after_op1[16];
13850 struct md5_ctx ctx;
13851 htab_t ht;
13853 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13854 md5_init_ctx (&ctx);
13855 fold_checksum_tree (op0, &ctx, ht);
13856 md5_finish_ctx (&ctx, checksum_before_op0);
13857 htab_empty (ht);
13859 md5_init_ctx (&ctx);
13860 fold_checksum_tree (op1, &ctx, ht);
13861 md5_finish_ctx (&ctx, checksum_before_op1);
13862 htab_empty (ht);
13863 #endif
13865 tem = fold_binary_loc (loc, code, type, op0, op1);
13866 if (!tem)
13868 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13869 SET_EXPR_LOCATION (tem, loc);
13872 #ifdef ENABLE_FOLD_CHECKING
13873 md5_init_ctx (&ctx);
13874 fold_checksum_tree (op0, &ctx, ht);
13875 md5_finish_ctx (&ctx, checksum_after_op0);
13876 htab_empty (ht);
13878 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13879 fold_check_failed (op0, tem);
13881 md5_init_ctx (&ctx);
13882 fold_checksum_tree (op1, &ctx, ht);
13883 md5_finish_ctx (&ctx, checksum_after_op1);
13884 htab_delete (ht);
13886 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13887 fold_check_failed (op1, tem);
13888 #endif
13889 return tem;
13892 /* Fold a ternary tree expression with code CODE of type TYPE with
13893 operands OP0, OP1, and OP2. Return a folded expression if
13894 successful. Otherwise, return a tree expression with code CODE of
13895 type TYPE with operands OP0, OP1, and OP2. */
13897 tree
13898 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
13899 tree op0, tree op1, tree op2 MEM_STAT_DECL)
13901 tree tem;
13902 #ifdef ENABLE_FOLD_CHECKING
13903 unsigned char checksum_before_op0[16],
13904 checksum_before_op1[16],
13905 checksum_before_op2[16],
13906 checksum_after_op0[16],
13907 checksum_after_op1[16],
13908 checksum_after_op2[16];
13909 struct md5_ctx ctx;
13910 htab_t ht;
13912 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13913 md5_init_ctx (&ctx);
13914 fold_checksum_tree (op0, &ctx, ht);
13915 md5_finish_ctx (&ctx, checksum_before_op0);
13916 htab_empty (ht);
13918 md5_init_ctx (&ctx);
13919 fold_checksum_tree (op1, &ctx, ht);
13920 md5_finish_ctx (&ctx, checksum_before_op1);
13921 htab_empty (ht);
13923 md5_init_ctx (&ctx);
13924 fold_checksum_tree (op2, &ctx, ht);
13925 md5_finish_ctx (&ctx, checksum_before_op2);
13926 htab_empty (ht);
13927 #endif
13929 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13930 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13931 if (!tem)
13933 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
13934 SET_EXPR_LOCATION (tem, loc);
13937 #ifdef ENABLE_FOLD_CHECKING
13938 md5_init_ctx (&ctx);
13939 fold_checksum_tree (op0, &ctx, ht);
13940 md5_finish_ctx (&ctx, checksum_after_op0);
13941 htab_empty (ht);
13943 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13944 fold_check_failed (op0, tem);
13946 md5_init_ctx (&ctx);
13947 fold_checksum_tree (op1, &ctx, ht);
13948 md5_finish_ctx (&ctx, checksum_after_op1);
13949 htab_empty (ht);
13951 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13952 fold_check_failed (op1, tem);
13954 md5_init_ctx (&ctx);
13955 fold_checksum_tree (op2, &ctx, ht);
13956 md5_finish_ctx (&ctx, checksum_after_op2);
13957 htab_delete (ht);
13959 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13960 fold_check_failed (op2, tem);
13961 #endif
13962 return tem;
13965 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13966 arguments in ARGARRAY, and a null static chain.
13967 Return a folded expression if successful. Otherwise, return a CALL_EXPR
13968 of type TYPE from the given operands as constructed by build_call_array. */
13970 tree
13971 fold_build_call_array_loc (location_t loc, tree type, tree fn,
13972 int nargs, tree *argarray)
13974 tree tem;
13975 #ifdef ENABLE_FOLD_CHECKING
13976 unsigned char checksum_before_fn[16],
13977 checksum_before_arglist[16],
13978 checksum_after_fn[16],
13979 checksum_after_arglist[16];
13980 struct md5_ctx ctx;
13981 htab_t ht;
13982 int i;
13984 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13985 md5_init_ctx (&ctx);
13986 fold_checksum_tree (fn, &ctx, ht);
13987 md5_finish_ctx (&ctx, checksum_before_fn);
13988 htab_empty (ht);
13990 md5_init_ctx (&ctx);
13991 for (i = 0; i < nargs; i++)
13992 fold_checksum_tree (argarray[i], &ctx, ht);
13993 md5_finish_ctx (&ctx, checksum_before_arglist);
13994 htab_empty (ht);
13995 #endif
13997 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13999 #ifdef ENABLE_FOLD_CHECKING
14000 md5_init_ctx (&ctx);
14001 fold_checksum_tree (fn, &ctx, ht);
14002 md5_finish_ctx (&ctx, checksum_after_fn);
14003 htab_empty (ht);
14005 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14006 fold_check_failed (fn, tem);
14008 md5_init_ctx (&ctx);
14009 for (i = 0; i < nargs; i++)
14010 fold_checksum_tree (argarray[i], &ctx, ht);
14011 md5_finish_ctx (&ctx, checksum_after_arglist);
14012 htab_delete (ht);
14014 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14015 fold_check_failed (NULL_TREE, tem);
14016 #endif
14017 return tem;
14020 /* Perform constant folding and related simplification of initializer
14021 expression EXPR. These behave identically to "fold_buildN" but ignore
14022 potential run-time traps and exceptions that fold must preserve. */
14024 #define START_FOLD_INIT \
14025 int saved_signaling_nans = flag_signaling_nans;\
14026 int saved_trapping_math = flag_trapping_math;\
14027 int saved_rounding_math = flag_rounding_math;\
14028 int saved_trapv = flag_trapv;\
14029 int saved_folding_initializer = folding_initializer;\
14030 flag_signaling_nans = 0;\
14031 flag_trapping_math = 0;\
14032 flag_rounding_math = 0;\
14033 flag_trapv = 0;\
14034 folding_initializer = 1;
14036 #define END_FOLD_INIT \
14037 flag_signaling_nans = saved_signaling_nans;\
14038 flag_trapping_math = saved_trapping_math;\
14039 flag_rounding_math = saved_rounding_math;\
14040 flag_trapv = saved_trapv;\
14041 folding_initializer = saved_folding_initializer;
14043 tree
14044 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14045 tree type, tree op)
14047 tree result;
14048 START_FOLD_INIT;
14050 result = fold_build1_loc (loc, code, type, op);
14052 END_FOLD_INIT;
14053 return result;
14056 tree
14057 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14058 tree type, tree op0, tree op1)
14060 tree result;
14061 START_FOLD_INIT;
14063 result = fold_build2_loc (loc, code, type, op0, op1);
14065 END_FOLD_INIT;
14066 return result;
14069 tree
14070 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14071 tree type, tree op0, tree op1, tree op2)
14073 tree result;
14074 START_FOLD_INIT;
14076 result = fold_build3_loc (loc, code, type, op0, op1, op2);
14078 END_FOLD_INIT;
14079 return result;
14082 tree
14083 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14084 int nargs, tree *argarray)
14086 tree result;
14087 START_FOLD_INIT;
14089 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14091 END_FOLD_INIT;
14092 return result;
14095 #undef START_FOLD_INIT
14096 #undef END_FOLD_INIT
14098 /* Determine if first argument is a multiple of second argument. Return 0 if
14099 it is not, or we cannot easily determined it to be.
14101 An example of the sort of thing we care about (at this point; this routine
14102 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14103 fold cases do now) is discovering that
14105 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14107 is a multiple of
14109 SAVE_EXPR (J * 8)
14111 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14113 This code also handles discovering that
14115 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14117 is a multiple of 8 so we don't have to worry about dealing with a
14118 possible remainder.
14120 Note that we *look* inside a SAVE_EXPR only to determine how it was
14121 calculated; it is not safe for fold to do much of anything else with the
14122 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14123 at run time. For example, the latter example above *cannot* be implemented
14124 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14125 evaluation time of the original SAVE_EXPR is not necessarily the same at
14126 the time the new expression is evaluated. The only optimization of this
14127 sort that would be valid is changing
14129 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14131 divided by 8 to
14133 SAVE_EXPR (I) * SAVE_EXPR (J)
14135 (where the same SAVE_EXPR (J) is used in the original and the
14136 transformed version). */
14139 multiple_of_p (tree type, const_tree top, const_tree bottom)
14141 if (operand_equal_p (top, bottom, 0))
14142 return 1;
14144 if (TREE_CODE (type) != INTEGER_TYPE)
14145 return 0;
14147 switch (TREE_CODE (top))
14149 case BIT_AND_EXPR:
14150 /* Bitwise and provides a power of two multiple. If the mask is
14151 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14152 if (!integer_pow2p (bottom))
14153 return 0;
14154 /* FALLTHRU */
14156 case MULT_EXPR:
14157 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14158 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14160 case PLUS_EXPR:
14161 case MINUS_EXPR:
14162 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14163 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14165 case LSHIFT_EXPR:
14166 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14168 tree op1, t1;
14170 op1 = TREE_OPERAND (top, 1);
14171 /* const_binop may not detect overflow correctly,
14172 so check for it explicitly here. */
14173 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14174 > TREE_INT_CST_LOW (op1)
14175 && TREE_INT_CST_HIGH (op1) == 0
14176 && 0 != (t1 = fold_convert (type,
14177 const_binop (LSHIFT_EXPR,
14178 size_one_node,
14179 op1, 0)))
14180 && !TREE_OVERFLOW (t1))
14181 return multiple_of_p (type, t1, bottom);
14183 return 0;
14185 case NOP_EXPR:
14186 /* Can't handle conversions from non-integral or wider integral type. */
14187 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14188 || (TYPE_PRECISION (type)
14189 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14190 return 0;
14192 /* .. fall through ... */
14194 case SAVE_EXPR:
14195 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14197 case COND_EXPR:
14198 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14199 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14201 case INTEGER_CST:
14202 if (TREE_CODE (bottom) != INTEGER_CST
14203 || integer_zerop (bottom)
14204 || (TYPE_UNSIGNED (type)
14205 && (tree_int_cst_sgn (top) < 0
14206 || tree_int_cst_sgn (bottom) < 0)))
14207 return 0;
14208 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14209 top, bottom, 0));
14211 default:
14212 return 0;
14216 /* Return true if CODE or TYPE is known to be non-negative. */
14218 static bool
14219 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14221 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14222 && truth_value_p (code))
14223 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14224 have a signed:1 type (where the value is -1 and 0). */
14225 return true;
14226 return false;
14229 /* Return true if (CODE OP0) is known to be non-negative. If the return
14230 value is based on the assumption that signed overflow is undefined,
14231 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14232 *STRICT_OVERFLOW_P. */
14234 bool
14235 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14236 bool *strict_overflow_p)
14238 if (TYPE_UNSIGNED (type))
14239 return true;
14241 switch (code)
14243 case ABS_EXPR:
14244 /* We can't return 1 if flag_wrapv is set because
14245 ABS_EXPR<INT_MIN> = INT_MIN. */
14246 if (!INTEGRAL_TYPE_P (type))
14247 return true;
14248 if (TYPE_OVERFLOW_UNDEFINED (type))
14250 *strict_overflow_p = true;
14251 return true;
14253 break;
14255 case NON_LVALUE_EXPR:
14256 case FLOAT_EXPR:
14257 case FIX_TRUNC_EXPR:
14258 return tree_expr_nonnegative_warnv_p (op0,
14259 strict_overflow_p);
14261 case NOP_EXPR:
14263 tree inner_type = TREE_TYPE (op0);
14264 tree outer_type = type;
14266 if (TREE_CODE (outer_type) == REAL_TYPE)
14268 if (TREE_CODE (inner_type) == REAL_TYPE)
14269 return tree_expr_nonnegative_warnv_p (op0,
14270 strict_overflow_p);
14271 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14273 if (TYPE_UNSIGNED (inner_type))
14274 return true;
14275 return tree_expr_nonnegative_warnv_p (op0,
14276 strict_overflow_p);
14279 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14281 if (TREE_CODE (inner_type) == REAL_TYPE)
14282 return tree_expr_nonnegative_warnv_p (op0,
14283 strict_overflow_p);
14284 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14285 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14286 && TYPE_UNSIGNED (inner_type);
14289 break;
14291 default:
14292 return tree_simple_nonnegative_warnv_p (code, type);
14295 /* We don't know sign of `t', so be conservative and return false. */
14296 return false;
14299 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14300 value is based on the assumption that signed overflow is undefined,
14301 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14302 *STRICT_OVERFLOW_P. */
14304 bool
14305 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14306 tree op1, bool *strict_overflow_p)
14308 if (TYPE_UNSIGNED (type))
14309 return true;
14311 switch (code)
14313 case POINTER_PLUS_EXPR:
14314 case PLUS_EXPR:
14315 if (FLOAT_TYPE_P (type))
14316 return (tree_expr_nonnegative_warnv_p (op0,
14317 strict_overflow_p)
14318 && tree_expr_nonnegative_warnv_p (op1,
14319 strict_overflow_p));
14321 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14322 both unsigned and at least 2 bits shorter than the result. */
14323 if (TREE_CODE (type) == INTEGER_TYPE
14324 && TREE_CODE (op0) == NOP_EXPR
14325 && TREE_CODE (op1) == NOP_EXPR)
14327 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14328 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14329 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14330 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14332 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14333 TYPE_PRECISION (inner2)) + 1;
14334 return prec < TYPE_PRECISION (type);
14337 break;
14339 case MULT_EXPR:
14340 if (FLOAT_TYPE_P (type))
14342 /* x * x for floating point x is always non-negative. */
14343 if (operand_equal_p (op0, op1, 0))
14344 return true;
14345 return (tree_expr_nonnegative_warnv_p (op0,
14346 strict_overflow_p)
14347 && tree_expr_nonnegative_warnv_p (op1,
14348 strict_overflow_p));
14351 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14352 both unsigned and their total bits is shorter than the result. */
14353 if (TREE_CODE (type) == INTEGER_TYPE
14354 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14355 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14357 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14358 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14359 : TREE_TYPE (op0);
14360 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14361 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14362 : TREE_TYPE (op1);
14364 bool unsigned0 = TYPE_UNSIGNED (inner0);
14365 bool unsigned1 = TYPE_UNSIGNED (inner1);
14367 if (TREE_CODE (op0) == INTEGER_CST)
14368 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14370 if (TREE_CODE (op1) == INTEGER_CST)
14371 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14373 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14374 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14376 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14377 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14378 : TYPE_PRECISION (inner0);
14380 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14381 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14382 : TYPE_PRECISION (inner1);
14384 return precision0 + precision1 < TYPE_PRECISION (type);
14387 return false;
14389 case BIT_AND_EXPR:
14390 case MAX_EXPR:
14391 return (tree_expr_nonnegative_warnv_p (op0,
14392 strict_overflow_p)
14393 || tree_expr_nonnegative_warnv_p (op1,
14394 strict_overflow_p));
14396 case BIT_IOR_EXPR:
14397 case BIT_XOR_EXPR:
14398 case MIN_EXPR:
14399 case RDIV_EXPR:
14400 case TRUNC_DIV_EXPR:
14401 case CEIL_DIV_EXPR:
14402 case FLOOR_DIV_EXPR:
14403 case ROUND_DIV_EXPR:
14404 return (tree_expr_nonnegative_warnv_p (op0,
14405 strict_overflow_p)
14406 && tree_expr_nonnegative_warnv_p (op1,
14407 strict_overflow_p));
14409 case TRUNC_MOD_EXPR:
14410 case CEIL_MOD_EXPR:
14411 case FLOOR_MOD_EXPR:
14412 case ROUND_MOD_EXPR:
14413 return tree_expr_nonnegative_warnv_p (op0,
14414 strict_overflow_p);
14415 default:
14416 return tree_simple_nonnegative_warnv_p (code, type);
14419 /* We don't know sign of `t', so be conservative and return false. */
14420 return false;
14423 /* Return true if T is known to be non-negative. If the return
14424 value is based on the assumption that signed overflow is undefined,
14425 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14426 *STRICT_OVERFLOW_P. */
14428 bool
14429 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14431 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14432 return true;
14434 switch (TREE_CODE (t))
14436 case INTEGER_CST:
14437 return tree_int_cst_sgn (t) >= 0;
14439 case REAL_CST:
14440 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14442 case FIXED_CST:
14443 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14445 case COND_EXPR:
14446 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14447 strict_overflow_p)
14448 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14449 strict_overflow_p));
14450 default:
14451 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14452 TREE_TYPE (t));
14454 /* We don't know sign of `t', so be conservative and return false. */
14455 return false;
14458 /* Return true if T is known to be non-negative. If the return
14459 value is based on the assumption that signed overflow is undefined,
14460 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14461 *STRICT_OVERFLOW_P. */
14463 bool
14464 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14465 tree arg0, tree arg1, bool *strict_overflow_p)
14467 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14468 switch (DECL_FUNCTION_CODE (fndecl))
14470 CASE_FLT_FN (BUILT_IN_ACOS):
14471 CASE_FLT_FN (BUILT_IN_ACOSH):
14472 CASE_FLT_FN (BUILT_IN_CABS):
14473 CASE_FLT_FN (BUILT_IN_COSH):
14474 CASE_FLT_FN (BUILT_IN_ERFC):
14475 CASE_FLT_FN (BUILT_IN_EXP):
14476 CASE_FLT_FN (BUILT_IN_EXP10):
14477 CASE_FLT_FN (BUILT_IN_EXP2):
14478 CASE_FLT_FN (BUILT_IN_FABS):
14479 CASE_FLT_FN (BUILT_IN_FDIM):
14480 CASE_FLT_FN (BUILT_IN_HYPOT):
14481 CASE_FLT_FN (BUILT_IN_POW10):
14482 CASE_INT_FN (BUILT_IN_FFS):
14483 CASE_INT_FN (BUILT_IN_PARITY):
14484 CASE_INT_FN (BUILT_IN_POPCOUNT):
14485 case BUILT_IN_BSWAP32:
14486 case BUILT_IN_BSWAP64:
14487 /* Always true. */
14488 return true;
14490 CASE_FLT_FN (BUILT_IN_SQRT):
14491 /* sqrt(-0.0) is -0.0. */
14492 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14493 return true;
14494 return tree_expr_nonnegative_warnv_p (arg0,
14495 strict_overflow_p);
14497 CASE_FLT_FN (BUILT_IN_ASINH):
14498 CASE_FLT_FN (BUILT_IN_ATAN):
14499 CASE_FLT_FN (BUILT_IN_ATANH):
14500 CASE_FLT_FN (BUILT_IN_CBRT):
14501 CASE_FLT_FN (BUILT_IN_CEIL):
14502 CASE_FLT_FN (BUILT_IN_ERF):
14503 CASE_FLT_FN (BUILT_IN_EXPM1):
14504 CASE_FLT_FN (BUILT_IN_FLOOR):
14505 CASE_FLT_FN (BUILT_IN_FMOD):
14506 CASE_FLT_FN (BUILT_IN_FREXP):
14507 CASE_FLT_FN (BUILT_IN_LCEIL):
14508 CASE_FLT_FN (BUILT_IN_LDEXP):
14509 CASE_FLT_FN (BUILT_IN_LFLOOR):
14510 CASE_FLT_FN (BUILT_IN_LLCEIL):
14511 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14512 CASE_FLT_FN (BUILT_IN_LLRINT):
14513 CASE_FLT_FN (BUILT_IN_LLROUND):
14514 CASE_FLT_FN (BUILT_IN_LRINT):
14515 CASE_FLT_FN (BUILT_IN_LROUND):
14516 CASE_FLT_FN (BUILT_IN_MODF):
14517 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14518 CASE_FLT_FN (BUILT_IN_RINT):
14519 CASE_FLT_FN (BUILT_IN_ROUND):
14520 CASE_FLT_FN (BUILT_IN_SCALB):
14521 CASE_FLT_FN (BUILT_IN_SCALBLN):
14522 CASE_FLT_FN (BUILT_IN_SCALBN):
14523 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14524 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14525 CASE_FLT_FN (BUILT_IN_SINH):
14526 CASE_FLT_FN (BUILT_IN_TANH):
14527 CASE_FLT_FN (BUILT_IN_TRUNC):
14528 /* True if the 1st argument is nonnegative. */
14529 return tree_expr_nonnegative_warnv_p (arg0,
14530 strict_overflow_p);
14532 CASE_FLT_FN (BUILT_IN_FMAX):
14533 /* True if the 1st OR 2nd arguments are nonnegative. */
14534 return (tree_expr_nonnegative_warnv_p (arg0,
14535 strict_overflow_p)
14536 || (tree_expr_nonnegative_warnv_p (arg1,
14537 strict_overflow_p)));
14539 CASE_FLT_FN (BUILT_IN_FMIN):
14540 /* True if the 1st AND 2nd arguments are nonnegative. */
14541 return (tree_expr_nonnegative_warnv_p (arg0,
14542 strict_overflow_p)
14543 && (tree_expr_nonnegative_warnv_p (arg1,
14544 strict_overflow_p)));
14546 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14547 /* True if the 2nd argument is nonnegative. */
14548 return tree_expr_nonnegative_warnv_p (arg1,
14549 strict_overflow_p);
14551 CASE_FLT_FN (BUILT_IN_POWI):
14552 /* True if the 1st argument is nonnegative or the second
14553 argument is an even integer. */
14554 if (TREE_CODE (arg1) == INTEGER_CST
14555 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14556 return true;
14557 return tree_expr_nonnegative_warnv_p (arg0,
14558 strict_overflow_p);
14560 CASE_FLT_FN (BUILT_IN_POW):
14561 /* True if the 1st argument is nonnegative or the second
14562 argument is an even integer valued real. */
14563 if (TREE_CODE (arg1) == REAL_CST)
14565 REAL_VALUE_TYPE c;
14566 HOST_WIDE_INT n;
14568 c = TREE_REAL_CST (arg1);
14569 n = real_to_integer (&c);
14570 if ((n & 1) == 0)
14572 REAL_VALUE_TYPE cint;
14573 real_from_integer (&cint, VOIDmode, n,
14574 n < 0 ? -1 : 0, 0);
14575 if (real_identical (&c, &cint))
14576 return true;
14579 return tree_expr_nonnegative_warnv_p (arg0,
14580 strict_overflow_p);
14582 default:
14583 break;
14585 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14586 type);
14589 /* Return true if T is known to be non-negative. If the return
14590 value is based on the assumption that signed overflow is undefined,
14591 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14592 *STRICT_OVERFLOW_P. */
14594 bool
14595 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14597 enum tree_code code = TREE_CODE (t);
14598 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14599 return true;
14601 switch (code)
14603 case TARGET_EXPR:
14605 tree temp = TARGET_EXPR_SLOT (t);
14606 t = TARGET_EXPR_INITIAL (t);
14608 /* If the initializer is non-void, then it's a normal expression
14609 that will be assigned to the slot. */
14610 if (!VOID_TYPE_P (t))
14611 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14613 /* Otherwise, the initializer sets the slot in some way. One common
14614 way is an assignment statement at the end of the initializer. */
14615 while (1)
14617 if (TREE_CODE (t) == BIND_EXPR)
14618 t = expr_last (BIND_EXPR_BODY (t));
14619 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14620 || TREE_CODE (t) == TRY_CATCH_EXPR)
14621 t = expr_last (TREE_OPERAND (t, 0));
14622 else if (TREE_CODE (t) == STATEMENT_LIST)
14623 t = expr_last (t);
14624 else
14625 break;
14627 if (TREE_CODE (t) == MODIFY_EXPR
14628 && TREE_OPERAND (t, 0) == temp)
14629 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14630 strict_overflow_p);
14632 return false;
14635 case CALL_EXPR:
14637 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14638 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14640 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14641 get_callee_fndecl (t),
14642 arg0,
14643 arg1,
14644 strict_overflow_p);
14646 case COMPOUND_EXPR:
14647 case MODIFY_EXPR:
14648 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14649 strict_overflow_p);
14650 case BIND_EXPR:
14651 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14652 strict_overflow_p);
14653 case SAVE_EXPR:
14654 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14655 strict_overflow_p);
14657 default:
14658 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14659 TREE_TYPE (t));
14662 /* We don't know sign of `t', so be conservative and return false. */
14663 return false;
14666 /* Return true if T is known to be non-negative. If the return
14667 value is based on the assumption that signed overflow is undefined,
14668 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14669 *STRICT_OVERFLOW_P. */
14671 bool
14672 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14674 enum tree_code code;
14675 if (t == error_mark_node)
14676 return false;
14678 code = TREE_CODE (t);
14679 switch (TREE_CODE_CLASS (code))
14681 case tcc_binary:
14682 case tcc_comparison:
14683 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14684 TREE_TYPE (t),
14685 TREE_OPERAND (t, 0),
14686 TREE_OPERAND (t, 1),
14687 strict_overflow_p);
14689 case tcc_unary:
14690 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14691 TREE_TYPE (t),
14692 TREE_OPERAND (t, 0),
14693 strict_overflow_p);
14695 case tcc_constant:
14696 case tcc_declaration:
14697 case tcc_reference:
14698 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14700 default:
14701 break;
14704 switch (code)
14706 case TRUTH_AND_EXPR:
14707 case TRUTH_OR_EXPR:
14708 case TRUTH_XOR_EXPR:
14709 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14710 TREE_TYPE (t),
14711 TREE_OPERAND (t, 0),
14712 TREE_OPERAND (t, 1),
14713 strict_overflow_p);
14714 case TRUTH_NOT_EXPR:
14715 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14716 TREE_TYPE (t),
14717 TREE_OPERAND (t, 0),
14718 strict_overflow_p);
14720 case COND_EXPR:
14721 case CONSTRUCTOR:
14722 case OBJ_TYPE_REF:
14723 case ASSERT_EXPR:
14724 case ADDR_EXPR:
14725 case WITH_SIZE_EXPR:
14726 case SSA_NAME:
14727 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14729 default:
14730 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14734 /* Return true if `t' is known to be non-negative. Handle warnings
14735 about undefined signed overflow. */
14737 bool
14738 tree_expr_nonnegative_p (tree t)
14740 bool ret, strict_overflow_p;
14742 strict_overflow_p = false;
14743 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14744 if (strict_overflow_p)
14745 fold_overflow_warning (("assuming signed overflow does not occur when "
14746 "determining that expression is always "
14747 "non-negative"),
14748 WARN_STRICT_OVERFLOW_MISC);
14749 return ret;
14753 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14754 For floating point we further ensure that T is not denormal.
14755 Similar logic is present in nonzero_address in rtlanal.h.
14757 If the return value is based on the assumption that signed overflow
14758 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14759 change *STRICT_OVERFLOW_P. */
14761 bool
14762 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14763 bool *strict_overflow_p)
14765 switch (code)
14767 case ABS_EXPR:
14768 return tree_expr_nonzero_warnv_p (op0,
14769 strict_overflow_p);
14771 case NOP_EXPR:
14773 tree inner_type = TREE_TYPE (op0);
14774 tree outer_type = type;
14776 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14777 && tree_expr_nonzero_warnv_p (op0,
14778 strict_overflow_p));
14780 break;
14782 case NON_LVALUE_EXPR:
14783 return tree_expr_nonzero_warnv_p (op0,
14784 strict_overflow_p);
14786 default:
14787 break;
14790 return false;
14793 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14794 For floating point we further ensure that T is not denormal.
14795 Similar logic is present in nonzero_address in rtlanal.h.
14797 If the return value is based on the assumption that signed overflow
14798 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14799 change *STRICT_OVERFLOW_P. */
14801 bool
14802 tree_binary_nonzero_warnv_p (enum tree_code code,
14803 tree type,
14804 tree op0,
14805 tree op1, bool *strict_overflow_p)
14807 bool sub_strict_overflow_p;
14808 switch (code)
14810 case POINTER_PLUS_EXPR:
14811 case PLUS_EXPR:
14812 if (TYPE_OVERFLOW_UNDEFINED (type))
14814 /* With the presence of negative values it is hard
14815 to say something. */
14816 sub_strict_overflow_p = false;
14817 if (!tree_expr_nonnegative_warnv_p (op0,
14818 &sub_strict_overflow_p)
14819 || !tree_expr_nonnegative_warnv_p (op1,
14820 &sub_strict_overflow_p))
14821 return false;
14822 /* One of operands must be positive and the other non-negative. */
14823 /* We don't set *STRICT_OVERFLOW_P here: even if this value
14824 overflows, on a twos-complement machine the sum of two
14825 nonnegative numbers can never be zero. */
14826 return (tree_expr_nonzero_warnv_p (op0,
14827 strict_overflow_p)
14828 || tree_expr_nonzero_warnv_p (op1,
14829 strict_overflow_p));
14831 break;
14833 case MULT_EXPR:
14834 if (TYPE_OVERFLOW_UNDEFINED (type))
14836 if (tree_expr_nonzero_warnv_p (op0,
14837 strict_overflow_p)
14838 && tree_expr_nonzero_warnv_p (op1,
14839 strict_overflow_p))
14841 *strict_overflow_p = true;
14842 return true;
14845 break;
14847 case MIN_EXPR:
14848 sub_strict_overflow_p = false;
14849 if (tree_expr_nonzero_warnv_p (op0,
14850 &sub_strict_overflow_p)
14851 && tree_expr_nonzero_warnv_p (op1,
14852 &sub_strict_overflow_p))
14854 if (sub_strict_overflow_p)
14855 *strict_overflow_p = true;
14857 break;
14859 case MAX_EXPR:
14860 sub_strict_overflow_p = false;
14861 if (tree_expr_nonzero_warnv_p (op0,
14862 &sub_strict_overflow_p))
14864 if (sub_strict_overflow_p)
14865 *strict_overflow_p = true;
14867 /* When both operands are nonzero, then MAX must be too. */
14868 if (tree_expr_nonzero_warnv_p (op1,
14869 strict_overflow_p))
14870 return true;
14872 /* MAX where operand 0 is positive is positive. */
14873 return tree_expr_nonnegative_warnv_p (op0,
14874 strict_overflow_p);
14876 /* MAX where operand 1 is positive is positive. */
14877 else if (tree_expr_nonzero_warnv_p (op1,
14878 &sub_strict_overflow_p)
14879 && tree_expr_nonnegative_warnv_p (op1,
14880 &sub_strict_overflow_p))
14882 if (sub_strict_overflow_p)
14883 *strict_overflow_p = true;
14884 return true;
14886 break;
14888 case BIT_IOR_EXPR:
14889 return (tree_expr_nonzero_warnv_p (op1,
14890 strict_overflow_p)
14891 || tree_expr_nonzero_warnv_p (op0,
14892 strict_overflow_p));
14894 default:
14895 break;
14898 return false;
14901 /* Return true when T is an address and is known to be nonzero.
14902 For floating point we further ensure that T is not denormal.
14903 Similar logic is present in nonzero_address in rtlanal.h.
14905 If the return value is based on the assumption that signed overflow
14906 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14907 change *STRICT_OVERFLOW_P. */
14909 bool
14910 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14912 bool sub_strict_overflow_p;
14913 switch (TREE_CODE (t))
14915 case INTEGER_CST:
14916 return !integer_zerop (t);
14918 case ADDR_EXPR:
14920 tree base = TREE_OPERAND (t, 0);
14921 if (!DECL_P (base))
14922 base = get_base_address (base);
14924 if (!base)
14925 return false;
14927 /* Weak declarations may link to NULL. Other things may also be NULL
14928 so protect with -fdelete-null-pointer-checks; but not variables
14929 allocated on the stack. */
14930 if (DECL_P (base)
14931 && (flag_delete_null_pointer_checks
14932 || (DECL_CONTEXT (base)
14933 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
14934 && auto_var_in_fn_p (base, DECL_CONTEXT (base)))))
14935 return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
14937 /* Constants are never weak. */
14938 if (CONSTANT_CLASS_P (base))
14939 return true;
14941 return false;
14944 case COND_EXPR:
14945 sub_strict_overflow_p = false;
14946 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14947 &sub_strict_overflow_p)
14948 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14949 &sub_strict_overflow_p))
14951 if (sub_strict_overflow_p)
14952 *strict_overflow_p = true;
14953 return true;
14955 break;
14957 default:
14958 break;
14960 return false;
14963 /* Return true when T is an address and is known to be nonzero.
14964 For floating point we further ensure that T is not denormal.
14965 Similar logic is present in nonzero_address in rtlanal.h.
14967 If the return value is based on the assumption that signed overflow
14968 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14969 change *STRICT_OVERFLOW_P. */
14971 bool
14972 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14974 tree type = TREE_TYPE (t);
14975 enum tree_code code;
14977 /* Doing something useful for floating point would need more work. */
14978 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14979 return false;
14981 code = TREE_CODE (t);
14982 switch (TREE_CODE_CLASS (code))
14984 case tcc_unary:
14985 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14986 strict_overflow_p);
14987 case tcc_binary:
14988 case tcc_comparison:
14989 return tree_binary_nonzero_warnv_p (code, type,
14990 TREE_OPERAND (t, 0),
14991 TREE_OPERAND (t, 1),
14992 strict_overflow_p);
14993 case tcc_constant:
14994 case tcc_declaration:
14995 case tcc_reference:
14996 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14998 default:
14999 break;
15002 switch (code)
15004 case TRUTH_NOT_EXPR:
15005 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15006 strict_overflow_p);
15008 case TRUTH_AND_EXPR:
15009 case TRUTH_OR_EXPR:
15010 case TRUTH_XOR_EXPR:
15011 return tree_binary_nonzero_warnv_p (code, type,
15012 TREE_OPERAND (t, 0),
15013 TREE_OPERAND (t, 1),
15014 strict_overflow_p);
15016 case COND_EXPR:
15017 case CONSTRUCTOR:
15018 case OBJ_TYPE_REF:
15019 case ASSERT_EXPR:
15020 case ADDR_EXPR:
15021 case WITH_SIZE_EXPR:
15022 case SSA_NAME:
15023 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15025 case COMPOUND_EXPR:
15026 case MODIFY_EXPR:
15027 case BIND_EXPR:
15028 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15029 strict_overflow_p);
15031 case SAVE_EXPR:
15032 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15033 strict_overflow_p);
15035 case CALL_EXPR:
15036 return alloca_call_p (t);
15038 default:
15039 break;
15041 return false;
15044 /* Return true when T is an address and is known to be nonzero.
15045 Handle warnings about undefined signed overflow. */
15047 bool
15048 tree_expr_nonzero_p (tree t)
15050 bool ret, strict_overflow_p;
15052 strict_overflow_p = false;
15053 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15054 if (strict_overflow_p)
15055 fold_overflow_warning (("assuming signed overflow does not occur when "
15056 "determining that expression is always "
15057 "non-zero"),
15058 WARN_STRICT_OVERFLOW_MISC);
15059 return ret;
15062 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15063 attempt to fold the expression to a constant without modifying TYPE,
15064 OP0 or OP1.
15066 If the expression could be simplified to a constant, then return
15067 the constant. If the expression would not be simplified to a
15068 constant, then return NULL_TREE. */
15070 tree
15071 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15073 tree tem = fold_binary (code, type, op0, op1);
15074 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15077 /* Given the components of a unary expression CODE, TYPE and OP0,
15078 attempt to fold the expression to a constant without modifying
15079 TYPE or OP0.
15081 If the expression could be simplified to a constant, then return
15082 the constant. If the expression would not be simplified to a
15083 constant, then return NULL_TREE. */
15085 tree
15086 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15088 tree tem = fold_unary (code, type, op0);
15089 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15092 /* If EXP represents referencing an element in a constant string
15093 (either via pointer arithmetic or array indexing), return the
15094 tree representing the value accessed, otherwise return NULL. */
15096 tree
15097 fold_read_from_constant_string (tree exp)
15099 if ((TREE_CODE (exp) == INDIRECT_REF
15100 || TREE_CODE (exp) == ARRAY_REF)
15101 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15103 tree exp1 = TREE_OPERAND (exp, 0);
15104 tree index;
15105 tree string;
15106 location_t loc = EXPR_LOCATION (exp);
15108 if (TREE_CODE (exp) == INDIRECT_REF)
15109 string = string_constant (exp1, &index);
15110 else
15112 tree low_bound = array_ref_low_bound (exp);
15113 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15115 /* Optimize the special-case of a zero lower bound.
15117 We convert the low_bound to sizetype to avoid some problems
15118 with constant folding. (E.g. suppose the lower bound is 1,
15119 and its mode is QI. Without the conversion,l (ARRAY
15120 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15121 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15122 if (! integer_zerop (low_bound))
15123 index = size_diffop_loc (loc, index,
15124 fold_convert_loc (loc, sizetype, low_bound));
15126 string = exp1;
15129 if (string
15130 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15131 && TREE_CODE (string) == STRING_CST
15132 && TREE_CODE (index) == INTEGER_CST
15133 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15134 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15135 == MODE_INT)
15136 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15137 return build_int_cst_type (TREE_TYPE (exp),
15138 (TREE_STRING_POINTER (string)
15139 [TREE_INT_CST_LOW (index)]));
15141 return NULL;
15144 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15145 an integer constant, real, or fixed-point constant.
15147 TYPE is the type of the result. */
15149 static tree
15150 fold_negate_const (tree arg0, tree type)
15152 tree t = NULL_TREE;
15154 switch (TREE_CODE (arg0))
15156 case INTEGER_CST:
15158 unsigned HOST_WIDE_INT low;
15159 HOST_WIDE_INT high;
15160 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15161 TREE_INT_CST_HIGH (arg0),
15162 &low, &high);
15163 t = force_fit_type_double (type, low, high, 1,
15164 (overflow | TREE_OVERFLOW (arg0))
15165 && !TYPE_UNSIGNED (type));
15166 break;
15169 case REAL_CST:
15170 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15171 break;
15173 case FIXED_CST:
15175 FIXED_VALUE_TYPE f;
15176 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15177 &(TREE_FIXED_CST (arg0)), NULL,
15178 TYPE_SATURATING (type));
15179 t = build_fixed (type, f);
15180 /* Propagate overflow flags. */
15181 if (overflow_p | TREE_OVERFLOW (arg0))
15182 TREE_OVERFLOW (t) = 1;
15183 break;
15186 default:
15187 gcc_unreachable ();
15190 return t;
15193 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15194 an integer constant or real constant.
15196 TYPE is the type of the result. */
15198 tree
15199 fold_abs_const (tree arg0, tree type)
15201 tree t = NULL_TREE;
15203 switch (TREE_CODE (arg0))
15205 case INTEGER_CST:
15206 /* If the value is unsigned, then the absolute value is
15207 the same as the ordinary value. */
15208 if (TYPE_UNSIGNED (type))
15209 t = arg0;
15210 /* Similarly, if the value is non-negative. */
15211 else if (INT_CST_LT (integer_minus_one_node, arg0))
15212 t = arg0;
15213 /* If the value is negative, then the absolute value is
15214 its negation. */
15215 else
15217 unsigned HOST_WIDE_INT low;
15218 HOST_WIDE_INT high;
15219 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15220 TREE_INT_CST_HIGH (arg0),
15221 &low, &high);
15222 t = force_fit_type_double (type, low, high, -1,
15223 overflow | TREE_OVERFLOW (arg0));
15225 break;
15227 case REAL_CST:
15228 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15229 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15230 else
15231 t = arg0;
15232 break;
15234 default:
15235 gcc_unreachable ();
15238 return t;
15241 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15242 constant. TYPE is the type of the result. */
15244 static tree
15245 fold_not_const (tree arg0, tree type)
15247 tree t = NULL_TREE;
15249 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15251 t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15252 ~TREE_INT_CST_HIGH (arg0), 0,
15253 TREE_OVERFLOW (arg0));
15255 return t;
15258 /* Given CODE, a relational operator, the target type, TYPE and two
15259 constant operands OP0 and OP1, return the result of the
15260 relational operation. If the result is not a compile time
15261 constant, then return NULL_TREE. */
15263 static tree
15264 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15266 int result, invert;
15268 /* From here on, the only cases we handle are when the result is
15269 known to be a constant. */
15271 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15273 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15274 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15276 /* Handle the cases where either operand is a NaN. */
15277 if (real_isnan (c0) || real_isnan (c1))
15279 switch (code)
15281 case EQ_EXPR:
15282 case ORDERED_EXPR:
15283 result = 0;
15284 break;
15286 case NE_EXPR:
15287 case UNORDERED_EXPR:
15288 case UNLT_EXPR:
15289 case UNLE_EXPR:
15290 case UNGT_EXPR:
15291 case UNGE_EXPR:
15292 case UNEQ_EXPR:
15293 result = 1;
15294 break;
15296 case LT_EXPR:
15297 case LE_EXPR:
15298 case GT_EXPR:
15299 case GE_EXPR:
15300 case LTGT_EXPR:
15301 if (flag_trapping_math)
15302 return NULL_TREE;
15303 result = 0;
15304 break;
15306 default:
15307 gcc_unreachable ();
15310 return constant_boolean_node (result, type);
15313 return constant_boolean_node (real_compare (code, c0, c1), type);
15316 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15318 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15319 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15320 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15323 /* Handle equality/inequality of complex constants. */
15324 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15326 tree rcond = fold_relational_const (code, type,
15327 TREE_REALPART (op0),
15328 TREE_REALPART (op1));
15329 tree icond = fold_relational_const (code, type,
15330 TREE_IMAGPART (op0),
15331 TREE_IMAGPART (op1));
15332 if (code == EQ_EXPR)
15333 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15334 else if (code == NE_EXPR)
15335 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15336 else
15337 return NULL_TREE;
15340 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15342 To compute GT, swap the arguments and do LT.
15343 To compute GE, do LT and invert the result.
15344 To compute LE, swap the arguments, do LT and invert the result.
15345 To compute NE, do EQ and invert the result.
15347 Therefore, the code below must handle only EQ and LT. */
15349 if (code == LE_EXPR || code == GT_EXPR)
15351 tree tem = op0;
15352 op0 = op1;
15353 op1 = tem;
15354 code = swap_tree_comparison (code);
15357 /* Note that it is safe to invert for real values here because we
15358 have already handled the one case that it matters. */
15360 invert = 0;
15361 if (code == NE_EXPR || code == GE_EXPR)
15363 invert = 1;
15364 code = invert_tree_comparison (code, false);
15367 /* Compute a result for LT or EQ if args permit;
15368 Otherwise return T. */
15369 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15371 if (code == EQ_EXPR)
15372 result = tree_int_cst_equal (op0, op1);
15373 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15374 result = INT_CST_LT_UNSIGNED (op0, op1);
15375 else
15376 result = INT_CST_LT (op0, op1);
15378 else
15379 return NULL_TREE;
15381 if (invert)
15382 result ^= 1;
15383 return constant_boolean_node (result, type);
15386 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15387 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15388 itself. */
15390 tree
15391 fold_build_cleanup_point_expr (tree type, tree expr)
15393 /* If the expression does not have side effects then we don't have to wrap
15394 it with a cleanup point expression. */
15395 if (!TREE_SIDE_EFFECTS (expr))
15396 return expr;
15398 /* If the expression is a return, check to see if the expression inside the
15399 return has no side effects or the right hand side of the modify expression
15400 inside the return. If either don't have side effects set we don't need to
15401 wrap the expression in a cleanup point expression. Note we don't check the
15402 left hand side of the modify because it should always be a return decl. */
15403 if (TREE_CODE (expr) == RETURN_EXPR)
15405 tree op = TREE_OPERAND (expr, 0);
15406 if (!op || !TREE_SIDE_EFFECTS (op))
15407 return expr;
15408 op = TREE_OPERAND (op, 1);
15409 if (!TREE_SIDE_EFFECTS (op))
15410 return expr;
15413 return build1 (CLEANUP_POINT_EXPR, type, expr);
15416 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15417 of an indirection through OP0, or NULL_TREE if no simplification is
15418 possible. */
15420 tree
15421 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15423 tree sub = op0;
15424 tree subtype;
15426 STRIP_NOPS (sub);
15427 subtype = TREE_TYPE (sub);
15428 if (!POINTER_TYPE_P (subtype))
15429 return NULL_TREE;
15431 if (TREE_CODE (sub) == ADDR_EXPR)
15433 tree op = TREE_OPERAND (sub, 0);
15434 tree optype = TREE_TYPE (op);
15435 /* *&CONST_DECL -> to the value of the const decl. */
15436 if (TREE_CODE (op) == CONST_DECL)
15437 return DECL_INITIAL (op);
15438 /* *&p => p; make sure to handle *&"str"[cst] here. */
15439 if (type == optype)
15441 tree fop = fold_read_from_constant_string (op);
15442 if (fop)
15443 return fop;
15444 else
15445 return op;
15447 /* *(foo *)&fooarray => fooarray[0] */
15448 else if (TREE_CODE (optype) == ARRAY_TYPE
15449 && type == TREE_TYPE (optype))
15451 tree type_domain = TYPE_DOMAIN (optype);
15452 tree min_val = size_zero_node;
15453 if (type_domain && TYPE_MIN_VALUE (type_domain))
15454 min_val = TYPE_MIN_VALUE (type_domain);
15455 op0 = build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15456 SET_EXPR_LOCATION (op0, loc);
15457 return op0;
15459 /* *(foo *)&complexfoo => __real__ complexfoo */
15460 else if (TREE_CODE (optype) == COMPLEX_TYPE
15461 && type == TREE_TYPE (optype))
15462 return fold_build1_loc (loc, REALPART_EXPR, type, op);
15463 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15464 else if (TREE_CODE (optype) == VECTOR_TYPE
15465 && type == TREE_TYPE (optype))
15467 tree part_width = TYPE_SIZE (type);
15468 tree index = bitsize_int (0);
15469 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15473 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15474 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15475 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15477 tree op00 = TREE_OPERAND (sub, 0);
15478 tree op01 = TREE_OPERAND (sub, 1);
15479 tree op00type;
15481 STRIP_NOPS (op00);
15482 op00type = TREE_TYPE (op00);
15483 if (TREE_CODE (op00) == ADDR_EXPR
15484 && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15485 && type == TREE_TYPE (TREE_TYPE (op00type)))
15487 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15488 tree part_width = TYPE_SIZE (type);
15489 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15490 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15491 tree index = bitsize_int (indexi);
15493 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15494 return fold_build3_loc (loc,
15495 BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15496 part_width, index);
15502 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15503 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15504 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15506 tree op00 = TREE_OPERAND (sub, 0);
15507 tree op01 = TREE_OPERAND (sub, 1);
15508 tree op00type;
15510 STRIP_NOPS (op00);
15511 op00type = TREE_TYPE (op00);
15512 if (TREE_CODE (op00) == ADDR_EXPR
15513 && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15514 && type == TREE_TYPE (TREE_TYPE (op00type)))
15516 tree size = TYPE_SIZE_UNIT (type);
15517 if (tree_int_cst_equal (size, op01))
15518 return fold_build1_loc (loc, IMAGPART_EXPR, type,
15519 TREE_OPERAND (op00, 0));
15523 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15524 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15525 && type == TREE_TYPE (TREE_TYPE (subtype)))
15527 tree type_domain;
15528 tree min_val = size_zero_node;
15529 sub = build_fold_indirect_ref_loc (loc, sub);
15530 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15531 if (type_domain && TYPE_MIN_VALUE (type_domain))
15532 min_val = TYPE_MIN_VALUE (type_domain);
15533 op0 = build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15534 SET_EXPR_LOCATION (op0, loc);
15535 return op0;
15538 return NULL_TREE;
15541 /* Builds an expression for an indirection through T, simplifying some
15542 cases. */
15544 tree
15545 build_fold_indirect_ref_loc (location_t loc, tree t)
15547 tree type = TREE_TYPE (TREE_TYPE (t));
15548 tree sub = fold_indirect_ref_1 (loc, type, t);
15550 if (sub)
15551 return sub;
15553 t = build1 (INDIRECT_REF, type, t);
15554 SET_EXPR_LOCATION (t, loc);
15555 return t;
15558 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15560 tree
15561 fold_indirect_ref_loc (location_t loc, tree t)
15563 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15565 if (sub)
15566 return sub;
15567 else
15568 return t;
15571 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15572 whose result is ignored. The type of the returned tree need not be
15573 the same as the original expression. */
15575 tree
15576 fold_ignored_result (tree t)
15578 if (!TREE_SIDE_EFFECTS (t))
15579 return integer_zero_node;
15581 for (;;)
15582 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15584 case tcc_unary:
15585 t = TREE_OPERAND (t, 0);
15586 break;
15588 case tcc_binary:
15589 case tcc_comparison:
15590 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15591 t = TREE_OPERAND (t, 0);
15592 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15593 t = TREE_OPERAND (t, 1);
15594 else
15595 return t;
15596 break;
15598 case tcc_expression:
15599 switch (TREE_CODE (t))
15601 case COMPOUND_EXPR:
15602 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15603 return t;
15604 t = TREE_OPERAND (t, 0);
15605 break;
15607 case COND_EXPR:
15608 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15609 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15610 return t;
15611 t = TREE_OPERAND (t, 0);
15612 break;
15614 default:
15615 return t;
15617 break;
15619 default:
15620 return t;
15624 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15625 This can only be applied to objects of a sizetype. */
15627 tree
15628 round_up_loc (location_t loc, tree value, int divisor)
15630 tree div = NULL_TREE;
15632 gcc_assert (divisor > 0);
15633 if (divisor == 1)
15634 return value;
15636 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15637 have to do anything. Only do this when we are not given a const,
15638 because in that case, this check is more expensive than just
15639 doing it. */
15640 if (TREE_CODE (value) != INTEGER_CST)
15642 div = build_int_cst (TREE_TYPE (value), divisor);
15644 if (multiple_of_p (TREE_TYPE (value), value, div))
15645 return value;
15648 /* If divisor is a power of two, simplify this to bit manipulation. */
15649 if (divisor == (divisor & -divisor))
15651 if (TREE_CODE (value) == INTEGER_CST)
15653 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15654 unsigned HOST_WIDE_INT high;
15655 bool overflow_p;
15657 if ((low & (divisor - 1)) == 0)
15658 return value;
15660 overflow_p = TREE_OVERFLOW (value);
15661 high = TREE_INT_CST_HIGH (value);
15662 low &= ~(divisor - 1);
15663 low += divisor;
15664 if (low == 0)
15666 high++;
15667 if (high == 0)
15668 overflow_p = true;
15671 return force_fit_type_double (TREE_TYPE (value), low, high,
15672 -1, overflow_p);
15674 else
15676 tree t;
15678 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15679 value = size_binop_loc (loc, PLUS_EXPR, value, t);
15680 t = build_int_cst (TREE_TYPE (value), -divisor);
15681 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15684 else
15686 if (!div)
15687 div = build_int_cst (TREE_TYPE (value), divisor);
15688 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15689 value = size_binop_loc (loc, MULT_EXPR, value, div);
15692 return value;
15695 /* Likewise, but round down. */
15697 tree
15698 round_down_loc (location_t loc, tree value, int divisor)
15700 tree div = NULL_TREE;
15702 gcc_assert (divisor > 0);
15703 if (divisor == 1)
15704 return value;
15706 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15707 have to do anything. Only do this when we are not given a const,
15708 because in that case, this check is more expensive than just
15709 doing it. */
15710 if (TREE_CODE (value) != INTEGER_CST)
15712 div = build_int_cst (TREE_TYPE (value), divisor);
15714 if (multiple_of_p (TREE_TYPE (value), value, div))
15715 return value;
15718 /* If divisor is a power of two, simplify this to bit manipulation. */
15719 if (divisor == (divisor & -divisor))
15721 tree t;
15723 t = build_int_cst (TREE_TYPE (value), -divisor);
15724 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15726 else
15728 if (!div)
15729 div = build_int_cst (TREE_TYPE (value), divisor);
15730 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
15731 value = size_binop_loc (loc, MULT_EXPR, value, div);
15734 return value;
15737 /* Returns the pointer to the base of the object addressed by EXP and
15738 extracts the information about the offset of the access, storing it
15739 to PBITPOS and POFFSET. */
15741 static tree
15742 split_address_to_core_and_offset (tree exp,
15743 HOST_WIDE_INT *pbitpos, tree *poffset)
15745 tree core;
15746 enum machine_mode mode;
15747 int unsignedp, volatilep;
15748 HOST_WIDE_INT bitsize;
15749 location_t loc = EXPR_LOCATION (exp);
15751 if (TREE_CODE (exp) == ADDR_EXPR)
15753 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15754 poffset, &mode, &unsignedp, &volatilep,
15755 false);
15756 core = build_fold_addr_expr_loc (loc, core);
15758 else
15760 core = exp;
15761 *pbitpos = 0;
15762 *poffset = NULL_TREE;
15765 return core;
15768 /* Returns true if addresses of E1 and E2 differ by a constant, false
15769 otherwise. If they do, E1 - E2 is stored in *DIFF. */
15771 bool
15772 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15774 tree core1, core2;
15775 HOST_WIDE_INT bitpos1, bitpos2;
15776 tree toffset1, toffset2, tdiff, type;
15778 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15779 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15781 if (bitpos1 % BITS_PER_UNIT != 0
15782 || bitpos2 % BITS_PER_UNIT != 0
15783 || !operand_equal_p (core1, core2, 0))
15784 return false;
15786 if (toffset1 && toffset2)
15788 type = TREE_TYPE (toffset1);
15789 if (type != TREE_TYPE (toffset2))
15790 toffset2 = fold_convert (type, toffset2);
15792 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15793 if (!cst_and_fits_in_hwi (tdiff))
15794 return false;
15796 *diff = int_cst_value (tdiff);
15798 else if (toffset1 || toffset2)
15800 /* If only one of the offsets is non-constant, the difference cannot
15801 be a constant. */
15802 return false;
15804 else
15805 *diff = 0;
15807 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15808 return true;
15811 /* Simplify the floating point expression EXP when the sign of the
15812 result is not significant. Return NULL_TREE if no simplification
15813 is possible. */
15815 tree
15816 fold_strip_sign_ops (tree exp)
15818 tree arg0, arg1;
15819 location_t loc = EXPR_LOCATION (exp);
15821 switch (TREE_CODE (exp))
15823 case ABS_EXPR:
15824 case NEGATE_EXPR:
15825 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15826 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15828 case MULT_EXPR:
15829 case RDIV_EXPR:
15830 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15831 return NULL_TREE;
15832 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15833 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15834 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15835 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
15836 arg0 ? arg0 : TREE_OPERAND (exp, 0),
15837 arg1 ? arg1 : TREE_OPERAND (exp, 1));
15838 break;
15840 case COMPOUND_EXPR:
15841 arg0 = TREE_OPERAND (exp, 0);
15842 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15843 if (arg1)
15844 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15845 break;
15847 case COND_EXPR:
15848 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15849 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15850 if (arg0 || arg1)
15851 return fold_build3_loc (loc,
15852 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15853 arg0 ? arg0 : TREE_OPERAND (exp, 1),
15854 arg1 ? arg1 : TREE_OPERAND (exp, 2));
15855 break;
15857 case CALL_EXPR:
15859 const enum built_in_function fcode = builtin_mathfn_code (exp);
15860 switch (fcode)
15862 CASE_FLT_FN (BUILT_IN_COPYSIGN):
15863 /* Strip copysign function call, return the 1st argument. */
15864 arg0 = CALL_EXPR_ARG (exp, 0);
15865 arg1 = CALL_EXPR_ARG (exp, 1);
15866 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
15868 default:
15869 /* Strip sign ops from the argument of "odd" math functions. */
15870 if (negate_mathfn_p (fcode))
15872 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15873 if (arg0)
15874 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
15876 break;
15879 break;
15881 default:
15882 break;
15884 return NULL_TREE;