Merge from mainline (165734:167278).
[official-gcc/graphite-test-results.git] / gcc / fold-const.c
blobc1950733b55a6ec6f40ee772d00ece491c0ed1af
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 and size_binop.
32 fold takes a tree as argument and returns a simplified tree.
34 size_binop takes a tree code for an arithmetic operation
35 and two operands that are trees, and produces a tree for the
36 result, assuming the type comes from `sizetype'.
38 size_int takes an integer value, and creates a tree constant
39 with type from `sizetype'.
41 Note: Since the folders get called on non-gimple code as well as
42 gimple code, we need to handle GIMPLE tuples as well as their
43 corresponding tree equivalents. */
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "tm.h"
49 #include "flags.h"
50 #include "tree.h"
51 #include "realmpfr.h"
52 #include "rtl.h"
53 #include "expr.h"
54 #include "tm_p.h"
55 #include "target.h"
56 #include "diagnostic-core.h"
57 #include "toplev.h"
58 #include "intl.h"
59 #include "ggc.h"
60 #include "hashtab.h"
61 #include "langhooks.h"
62 #include "md5.h"
63 #include "gimple.h"
64 #include "tree-flow.h"
66 /* Nonzero if we are folding constants inside an initializer; zero
67 otherwise. */
68 int folding_initializer = 0;
70 /* The following constants represent a bit based encoding of GCC's
71 comparison operators. This encoding simplifies transformations
72 on relational comparison operators, such as AND and OR. */
73 enum comparison_code {
74 COMPCODE_FALSE = 0,
75 COMPCODE_LT = 1,
76 COMPCODE_EQ = 2,
77 COMPCODE_LE = 3,
78 COMPCODE_GT = 4,
79 COMPCODE_LTGT = 5,
80 COMPCODE_GE = 6,
81 COMPCODE_ORD = 7,
82 COMPCODE_UNORD = 8,
83 COMPCODE_UNLT = 9,
84 COMPCODE_UNEQ = 10,
85 COMPCODE_UNLE = 11,
86 COMPCODE_UNGT = 12,
87 COMPCODE_NE = 13,
88 COMPCODE_UNGE = 14,
89 COMPCODE_TRUE = 15
92 static bool negate_mathfn_p (enum built_in_function);
93 static bool negate_expr_p (tree);
94 static tree negate_expr (tree);
95 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
96 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
97 static tree const_binop (enum tree_code, tree, tree);
98 static enum comparison_code comparison_to_compcode (enum tree_code);
99 static enum tree_code compcode_to_comparison (enum comparison_code);
100 static int operand_equal_for_comparison_p (tree, tree, tree);
101 static int twoval_comparison_p (tree, tree *, tree *, int *);
102 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
103 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
104 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
105 static tree make_bit_field_ref (location_t, tree, tree,
106 HOST_WIDE_INT, HOST_WIDE_INT, int);
107 static tree optimize_bit_field_compare (location_t, enum tree_code,
108 tree, tree, tree);
109 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
110 HOST_WIDE_INT *,
111 enum machine_mode *, int *, int *,
112 tree *, tree *);
113 static int all_ones_mask_p (const_tree, int);
114 static tree sign_bit_p (tree, const_tree);
115 static int simple_operand_p (const_tree);
116 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
117 static tree range_predecessor (tree);
118 static tree range_successor (tree);
119 extern tree make_range (tree, int *, tree *, tree *, bool *);
120 extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int,
121 tree, tree);
122 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
123 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
124 static tree unextend (tree, int, int, tree);
125 static tree fold_truthop (location_t, enum tree_code, tree, tree, tree);
126 static tree optimize_minmax_comparison (location_t, enum tree_code,
127 tree, tree, tree);
128 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
129 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
130 static tree fold_binary_op_with_conditional_arg (location_t,
131 enum tree_code, tree,
132 tree, tree,
133 tree, tree, int);
134 static tree fold_mathfn_compare (location_t,
135 enum built_in_function, enum tree_code,
136 tree, tree, tree);
137 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
138 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
139 static bool reorder_operands_p (const_tree, const_tree);
140 static tree fold_negate_const (tree, tree);
141 static tree fold_not_const (const_tree, tree);
142 static tree fold_relational_const (enum tree_code, tree, tree, tree);
143 static tree fold_convert_const (enum tree_code, tree, tree);
146 /* Similar to protected_set_expr_location, but never modify x in place,
147 if location can and needs to be set, unshare it. */
149 static inline tree
150 protected_set_expr_location_unshare (tree x, location_t loc)
152 if (CAN_HAVE_LOCATION_P (x)
153 && EXPR_LOCATION (x) != loc
154 && !(TREE_CODE (x) == SAVE_EXPR
155 || TREE_CODE (x) == TARGET_EXPR
156 || TREE_CODE (x) == BIND_EXPR))
158 x = copy_node (x);
159 SET_EXPR_LOCATION (x, loc);
161 return x;
165 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
166 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
167 and SUM1. Then this yields nonzero if overflow occurred during the
168 addition.
170 Overflow occurs if A and B have the same sign, but A and SUM differ in
171 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
172 sign. */
173 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
175 /* If ARG2 divides ARG1 with zero remainder, carries out the division
176 of type CODE and returns the quotient.
177 Otherwise returns NULL_TREE. */
179 tree
180 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
182 double_int quo, rem;
183 int uns;
185 /* The sign of the division is according to operand two, that
186 does the correct thing for POINTER_PLUS_EXPR where we want
187 a signed division. */
188 uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
189 if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
190 && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
191 uns = false;
193 quo = double_int_divmod (tree_to_double_int (arg1),
194 tree_to_double_int (arg2),
195 uns, code, &rem);
197 if (double_int_zero_p (rem))
198 return build_int_cst_wide (TREE_TYPE (arg1), quo.low, quo.high);
200 return NULL_TREE;
203 /* This is nonzero if we should defer warnings about undefined
204 overflow. This facility exists because these warnings are a
205 special case. The code to estimate loop iterations does not want
206 to issue any warnings, since it works with expressions which do not
207 occur in user code. Various bits of cleanup code call fold(), but
208 only use the result if it has certain characteristics (e.g., is a
209 constant); that code only wants to issue a warning if the result is
210 used. */
212 static int fold_deferring_overflow_warnings;
214 /* If a warning about undefined overflow is deferred, this is the
215 warning. Note that this may cause us to turn two warnings into
216 one, but that is fine since it is sufficient to only give one
217 warning per expression. */
219 static const char* fold_deferred_overflow_warning;
221 /* If a warning about undefined overflow is deferred, this is the
222 level at which the warning should be emitted. */
224 static enum warn_strict_overflow_code fold_deferred_overflow_code;
226 /* Start deferring overflow warnings. We could use a stack here to
227 permit nested calls, but at present it is not necessary. */
229 void
230 fold_defer_overflow_warnings (void)
232 ++fold_deferring_overflow_warnings;
235 /* Stop deferring overflow warnings. If there is a pending warning,
236 and ISSUE is true, then issue the warning if appropriate. STMT is
237 the statement with which the warning should be associated (used for
238 location information); STMT may be NULL. CODE is the level of the
239 warning--a warn_strict_overflow_code value. This function will use
240 the smaller of CODE and the deferred code when deciding whether to
241 issue the warning. CODE may be zero to mean to always use the
242 deferred code. */
244 void
245 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
247 const char *warnmsg;
248 location_t locus;
250 gcc_assert (fold_deferring_overflow_warnings > 0);
251 --fold_deferring_overflow_warnings;
252 if (fold_deferring_overflow_warnings > 0)
254 if (fold_deferred_overflow_warning != NULL
255 && code != 0
256 && code < (int) fold_deferred_overflow_code)
257 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
258 return;
261 warnmsg = fold_deferred_overflow_warning;
262 fold_deferred_overflow_warning = NULL;
264 if (!issue || warnmsg == NULL)
265 return;
267 if (gimple_no_warning_p (stmt))
268 return;
270 /* Use the smallest code level when deciding to issue the
271 warning. */
272 if (code == 0 || code > (int) fold_deferred_overflow_code)
273 code = fold_deferred_overflow_code;
275 if (!issue_strict_overflow_warning (code))
276 return;
278 if (stmt == NULL)
279 locus = input_location;
280 else
281 locus = gimple_location (stmt);
282 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
285 /* Stop deferring overflow warnings, ignoring any deferred
286 warnings. */
288 void
289 fold_undefer_and_ignore_overflow_warnings (void)
291 fold_undefer_overflow_warnings (false, NULL, 0);
294 /* Whether we are deferring overflow warnings. */
296 bool
297 fold_deferring_overflow_warnings_p (void)
299 return fold_deferring_overflow_warnings > 0;
302 /* This is called when we fold something based on the fact that signed
303 overflow is undefined. */
305 static void
306 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
308 if (fold_deferring_overflow_warnings > 0)
310 if (fold_deferred_overflow_warning == NULL
311 || wc < fold_deferred_overflow_code)
313 fold_deferred_overflow_warning = gmsgid;
314 fold_deferred_overflow_code = wc;
317 else if (issue_strict_overflow_warning (wc))
318 warning (OPT_Wstrict_overflow, gmsgid);
321 /* Return true if the built-in mathematical function specified by CODE
322 is odd, i.e. -f(x) == f(-x). */
324 static bool
325 negate_mathfn_p (enum built_in_function code)
327 switch (code)
329 CASE_FLT_FN (BUILT_IN_ASIN):
330 CASE_FLT_FN (BUILT_IN_ASINH):
331 CASE_FLT_FN (BUILT_IN_ATAN):
332 CASE_FLT_FN (BUILT_IN_ATANH):
333 CASE_FLT_FN (BUILT_IN_CASIN):
334 CASE_FLT_FN (BUILT_IN_CASINH):
335 CASE_FLT_FN (BUILT_IN_CATAN):
336 CASE_FLT_FN (BUILT_IN_CATANH):
337 CASE_FLT_FN (BUILT_IN_CBRT):
338 CASE_FLT_FN (BUILT_IN_CPROJ):
339 CASE_FLT_FN (BUILT_IN_CSIN):
340 CASE_FLT_FN (BUILT_IN_CSINH):
341 CASE_FLT_FN (BUILT_IN_CTAN):
342 CASE_FLT_FN (BUILT_IN_CTANH):
343 CASE_FLT_FN (BUILT_IN_ERF):
344 CASE_FLT_FN (BUILT_IN_LLROUND):
345 CASE_FLT_FN (BUILT_IN_LROUND):
346 CASE_FLT_FN (BUILT_IN_ROUND):
347 CASE_FLT_FN (BUILT_IN_SIN):
348 CASE_FLT_FN (BUILT_IN_SINH):
349 CASE_FLT_FN (BUILT_IN_TAN):
350 CASE_FLT_FN (BUILT_IN_TANH):
351 CASE_FLT_FN (BUILT_IN_TRUNC):
352 return true;
354 CASE_FLT_FN (BUILT_IN_LLRINT):
355 CASE_FLT_FN (BUILT_IN_LRINT):
356 CASE_FLT_FN (BUILT_IN_NEARBYINT):
357 CASE_FLT_FN (BUILT_IN_RINT):
358 return !flag_rounding_math;
360 default:
361 break;
363 return false;
366 /* Check whether we may negate an integer constant T without causing
367 overflow. */
369 bool
370 may_negate_without_overflow_p (const_tree t)
372 unsigned HOST_WIDE_INT val;
373 unsigned int prec;
374 tree type;
376 gcc_assert (TREE_CODE (t) == INTEGER_CST);
378 type = TREE_TYPE (t);
379 if (TYPE_UNSIGNED (type))
380 return false;
382 prec = TYPE_PRECISION (type);
383 if (prec > HOST_BITS_PER_WIDE_INT)
385 if (TREE_INT_CST_LOW (t) != 0)
386 return true;
387 prec -= HOST_BITS_PER_WIDE_INT;
388 val = TREE_INT_CST_HIGH (t);
390 else
391 val = TREE_INT_CST_LOW (t);
392 if (prec < HOST_BITS_PER_WIDE_INT)
393 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
394 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
397 /* Determine whether an expression T can be cheaply negated using
398 the function negate_expr without introducing undefined overflow. */
400 static bool
401 negate_expr_p (tree t)
403 tree type;
405 if (t == 0)
406 return false;
408 type = TREE_TYPE (t);
410 STRIP_SIGN_NOPS (t);
411 switch (TREE_CODE (t))
413 case INTEGER_CST:
414 if (TYPE_OVERFLOW_WRAPS (type))
415 return true;
417 /* Check that -CST will not overflow type. */
418 return may_negate_without_overflow_p (t);
419 case BIT_NOT_EXPR:
420 return (INTEGRAL_TYPE_P (type)
421 && TYPE_OVERFLOW_WRAPS (type));
423 case FIXED_CST:
424 case NEGATE_EXPR:
425 return true;
427 case REAL_CST:
428 /* We want to canonicalize to positive real constants. Pretend
429 that only negative ones can be easily negated. */
430 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
432 case COMPLEX_CST:
433 return negate_expr_p (TREE_REALPART (t))
434 && negate_expr_p (TREE_IMAGPART (t));
436 case COMPLEX_EXPR:
437 return negate_expr_p (TREE_OPERAND (t, 0))
438 && negate_expr_p (TREE_OPERAND (t, 1));
440 case CONJ_EXPR:
441 return negate_expr_p (TREE_OPERAND (t, 0));
443 case PLUS_EXPR:
444 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
445 || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
446 return false;
447 /* -(A + B) -> (-B) - A. */
448 if (negate_expr_p (TREE_OPERAND (t, 1))
449 && reorder_operands_p (TREE_OPERAND (t, 0),
450 TREE_OPERAND (t, 1)))
451 return true;
452 /* -(A + B) -> (-A) - B. */
453 return negate_expr_p (TREE_OPERAND (t, 0));
455 case MINUS_EXPR:
456 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
457 return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
458 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
459 && reorder_operands_p (TREE_OPERAND (t, 0),
460 TREE_OPERAND (t, 1));
462 case MULT_EXPR:
463 if (TYPE_UNSIGNED (TREE_TYPE (t)))
464 break;
466 /* Fall through. */
468 case RDIV_EXPR:
469 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
470 return negate_expr_p (TREE_OPERAND (t, 1))
471 || negate_expr_p (TREE_OPERAND (t, 0));
472 break;
474 case TRUNC_DIV_EXPR:
475 case ROUND_DIV_EXPR:
476 case FLOOR_DIV_EXPR:
477 case CEIL_DIV_EXPR:
478 case EXACT_DIV_EXPR:
479 /* In general we can't negate A / B, because if A is INT_MIN and
480 B is 1, we may turn this into INT_MIN / -1 which is undefined
481 and actually traps on some architectures. But if overflow is
482 undefined, we can negate, because - (INT_MIN / 1) is an
483 overflow. */
484 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
485 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
486 break;
487 return negate_expr_p (TREE_OPERAND (t, 1))
488 || negate_expr_p (TREE_OPERAND (t, 0));
490 case NOP_EXPR:
491 /* Negate -((double)float) as (double)(-float). */
492 if (TREE_CODE (type) == REAL_TYPE)
494 tree tem = strip_float_extensions (t);
495 if (tem != t)
496 return negate_expr_p (tem);
498 break;
500 case CALL_EXPR:
501 /* Negate -f(x) as f(-x). */
502 if (negate_mathfn_p (builtin_mathfn_code (t)))
503 return negate_expr_p (CALL_EXPR_ARG (t, 0));
504 break;
506 case RSHIFT_EXPR:
507 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
508 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
510 tree op1 = TREE_OPERAND (t, 1);
511 if (TREE_INT_CST_HIGH (op1) == 0
512 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
513 == TREE_INT_CST_LOW (op1))
514 return true;
516 break;
518 default:
519 break;
521 return false;
524 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
525 simplification is possible.
526 If negate_expr_p would return true for T, NULL_TREE will never be
527 returned. */
529 static tree
530 fold_negate_expr (location_t loc, tree t)
532 tree type = TREE_TYPE (t);
533 tree tem;
535 switch (TREE_CODE (t))
537 /* Convert - (~A) to A + 1. */
538 case BIT_NOT_EXPR:
539 if (INTEGRAL_TYPE_P (type))
540 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
541 build_int_cst (type, 1));
542 break;
544 case INTEGER_CST:
545 tem = fold_negate_const (t, type);
546 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
547 || !TYPE_OVERFLOW_TRAPS (type))
548 return tem;
549 break;
551 case REAL_CST:
552 tem = fold_negate_const (t, type);
553 /* Two's complement FP formats, such as c4x, may overflow. */
554 if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
555 return tem;
556 break;
558 case FIXED_CST:
559 tem = fold_negate_const (t, type);
560 return tem;
562 case COMPLEX_CST:
564 tree rpart = negate_expr (TREE_REALPART (t));
565 tree ipart = negate_expr (TREE_IMAGPART (t));
567 if ((TREE_CODE (rpart) == REAL_CST
568 && TREE_CODE (ipart) == REAL_CST)
569 || (TREE_CODE (rpart) == INTEGER_CST
570 && TREE_CODE (ipart) == INTEGER_CST))
571 return build_complex (type, rpart, ipart);
573 break;
575 case COMPLEX_EXPR:
576 if (negate_expr_p (t))
577 return fold_build2_loc (loc, COMPLEX_EXPR, type,
578 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
579 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
580 break;
582 case CONJ_EXPR:
583 if (negate_expr_p (t))
584 return fold_build1_loc (loc, CONJ_EXPR, type,
585 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
586 break;
588 case NEGATE_EXPR:
589 return TREE_OPERAND (t, 0);
591 case PLUS_EXPR:
592 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
593 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
595 /* -(A + B) -> (-B) - A. */
596 if (negate_expr_p (TREE_OPERAND (t, 1))
597 && reorder_operands_p (TREE_OPERAND (t, 0),
598 TREE_OPERAND (t, 1)))
600 tem = negate_expr (TREE_OPERAND (t, 1));
601 return fold_build2_loc (loc, MINUS_EXPR, type,
602 tem, TREE_OPERAND (t, 0));
605 /* -(A + B) -> (-A) - B. */
606 if (negate_expr_p (TREE_OPERAND (t, 0)))
608 tem = negate_expr (TREE_OPERAND (t, 0));
609 return fold_build2_loc (loc, MINUS_EXPR, type,
610 tem, TREE_OPERAND (t, 1));
613 break;
615 case MINUS_EXPR:
616 /* - (A - B) -> B - A */
617 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
618 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
619 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
620 return fold_build2_loc (loc, MINUS_EXPR, type,
621 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
622 break;
624 case MULT_EXPR:
625 if (TYPE_UNSIGNED (type))
626 break;
628 /* Fall through. */
630 case RDIV_EXPR:
631 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
633 tem = TREE_OPERAND (t, 1);
634 if (negate_expr_p (tem))
635 return fold_build2_loc (loc, TREE_CODE (t), type,
636 TREE_OPERAND (t, 0), negate_expr (tem));
637 tem = TREE_OPERAND (t, 0);
638 if (negate_expr_p (tem))
639 return fold_build2_loc (loc, TREE_CODE (t), type,
640 negate_expr (tem), TREE_OPERAND (t, 1));
642 break;
644 case TRUNC_DIV_EXPR:
645 case ROUND_DIV_EXPR:
646 case FLOOR_DIV_EXPR:
647 case CEIL_DIV_EXPR:
648 case EXACT_DIV_EXPR:
649 /* In general we can't negate A / B, because if A is INT_MIN and
650 B is 1, we may turn this into INT_MIN / -1 which is undefined
651 and actually traps on some architectures. But if overflow is
652 undefined, we can negate, because - (INT_MIN / 1) is an
653 overflow. */
654 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
656 const char * const warnmsg = G_("assuming signed overflow does not "
657 "occur when negating a division");
658 tem = TREE_OPERAND (t, 1);
659 if (negate_expr_p (tem))
661 if (INTEGRAL_TYPE_P (type)
662 && (TREE_CODE (tem) != INTEGER_CST
663 || integer_onep (tem)))
664 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
665 return fold_build2_loc (loc, TREE_CODE (t), type,
666 TREE_OPERAND (t, 0), negate_expr (tem));
668 tem = TREE_OPERAND (t, 0);
669 if (negate_expr_p (tem))
671 if (INTEGRAL_TYPE_P (type)
672 && (TREE_CODE (tem) != INTEGER_CST
673 || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
674 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
675 return fold_build2_loc (loc, TREE_CODE (t), type,
676 negate_expr (tem), TREE_OPERAND (t, 1));
679 break;
681 case NOP_EXPR:
682 /* Convert -((double)float) into (double)(-float). */
683 if (TREE_CODE (type) == REAL_TYPE)
685 tem = strip_float_extensions (t);
686 if (tem != t && negate_expr_p (tem))
687 return fold_convert_loc (loc, type, negate_expr (tem));
689 break;
691 case CALL_EXPR:
692 /* Negate -f(x) as f(-x). */
693 if (negate_mathfn_p (builtin_mathfn_code (t))
694 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
696 tree fndecl, arg;
698 fndecl = get_callee_fndecl (t);
699 arg = negate_expr (CALL_EXPR_ARG (t, 0));
700 return build_call_expr_loc (loc, fndecl, 1, arg);
702 break;
704 case RSHIFT_EXPR:
705 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
706 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
708 tree op1 = TREE_OPERAND (t, 1);
709 if (TREE_INT_CST_HIGH (op1) == 0
710 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
711 == TREE_INT_CST_LOW (op1))
713 tree ntype = TYPE_UNSIGNED (type)
714 ? signed_type_for (type)
715 : unsigned_type_for (type);
716 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
717 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
718 return fold_convert_loc (loc, type, temp);
721 break;
723 default:
724 break;
727 return NULL_TREE;
730 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
731 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
732 return NULL_TREE. */
734 static tree
735 negate_expr (tree t)
737 tree type, tem;
738 location_t loc;
740 if (t == NULL_TREE)
741 return NULL_TREE;
743 loc = EXPR_LOCATION (t);
744 type = TREE_TYPE (t);
745 STRIP_SIGN_NOPS (t);
747 tem = fold_negate_expr (loc, t);
748 if (!tem)
749 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
750 return fold_convert_loc (loc, type, tem);
753 /* Split a tree IN into a constant, literal and variable parts that could be
754 combined with CODE to make IN. "constant" means an expression with
755 TREE_CONSTANT but that isn't an actual constant. CODE must be a
756 commutative arithmetic operation. Store the constant part into *CONP,
757 the literal in *LITP and return the variable part. If a part isn't
758 present, set it to null. If the tree does not decompose in this way,
759 return the entire tree as the variable part and the other parts as null.
761 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
762 case, we negate an operand that was subtracted. Except if it is a
763 literal for which we use *MINUS_LITP instead.
765 If NEGATE_P is true, we are negating all of IN, again except a literal
766 for which we use *MINUS_LITP instead.
768 If IN is itself a literal or constant, return it as appropriate.
770 Note that we do not guarantee that any of the three values will be the
771 same type as IN, but they will have the same signedness and mode. */
773 static tree
774 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
775 tree *minus_litp, int negate_p)
777 tree var = 0;
779 *conp = 0;
780 *litp = 0;
781 *minus_litp = 0;
783 /* Strip any conversions that don't change the machine mode or signedness. */
784 STRIP_SIGN_NOPS (in);
786 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
787 || TREE_CODE (in) == FIXED_CST)
788 *litp = in;
789 else if (TREE_CODE (in) == code
790 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
791 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
792 /* We can associate addition and subtraction together (even
793 though the C standard doesn't say so) for integers because
794 the value is not affected. For reals, the value might be
795 affected, so we can't. */
796 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
797 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
799 tree op0 = TREE_OPERAND (in, 0);
800 tree op1 = TREE_OPERAND (in, 1);
801 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
802 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
804 /* First see if either of the operands is a literal, then a constant. */
805 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
806 || TREE_CODE (op0) == FIXED_CST)
807 *litp = op0, op0 = 0;
808 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
809 || TREE_CODE (op1) == FIXED_CST)
810 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
812 if (op0 != 0 && TREE_CONSTANT (op0))
813 *conp = op0, op0 = 0;
814 else if (op1 != 0 && TREE_CONSTANT (op1))
815 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
817 /* If we haven't dealt with either operand, this is not a case we can
818 decompose. Otherwise, VAR is either of the ones remaining, if any. */
819 if (op0 != 0 && op1 != 0)
820 var = in;
821 else if (op0 != 0)
822 var = op0;
823 else
824 var = op1, neg_var_p = neg1_p;
826 /* Now do any needed negations. */
827 if (neg_litp_p)
828 *minus_litp = *litp, *litp = 0;
829 if (neg_conp_p)
830 *conp = negate_expr (*conp);
831 if (neg_var_p)
832 var = negate_expr (var);
834 else if (TREE_CONSTANT (in))
835 *conp = in;
836 else
837 var = in;
839 if (negate_p)
841 if (*litp)
842 *minus_litp = *litp, *litp = 0;
843 else if (*minus_litp)
844 *litp = *minus_litp, *minus_litp = 0;
845 *conp = negate_expr (*conp);
846 var = negate_expr (var);
849 return var;
852 /* Re-associate trees split by the above function. T1 and T2 are
853 either expressions to associate or null. Return the new
854 expression, if any. LOC is the location of the new expression. If
855 we build an operation, do it in TYPE and with CODE. */
857 static tree
858 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
860 if (t1 == 0)
861 return t2;
862 else if (t2 == 0)
863 return t1;
865 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
866 try to fold this since we will have infinite recursion. But do
867 deal with any NEGATE_EXPRs. */
868 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
869 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
871 if (code == PLUS_EXPR)
873 if (TREE_CODE (t1) == NEGATE_EXPR)
874 return build2_loc (loc, MINUS_EXPR, type,
875 fold_convert_loc (loc, type, t2),
876 fold_convert_loc (loc, type,
877 TREE_OPERAND (t1, 0)));
878 else if (TREE_CODE (t2) == NEGATE_EXPR)
879 return build2_loc (loc, MINUS_EXPR, type,
880 fold_convert_loc (loc, type, t1),
881 fold_convert_loc (loc, type,
882 TREE_OPERAND (t2, 0)));
883 else if (integer_zerop (t2))
884 return fold_convert_loc (loc, type, t1);
886 else if (code == MINUS_EXPR)
888 if (integer_zerop (t2))
889 return fold_convert_loc (loc, type, t1);
892 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
893 fold_convert_loc (loc, type, t2));
896 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
897 fold_convert_loc (loc, type, t2));
900 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
901 for use in int_const_binop, size_binop and size_diffop. */
903 static bool
904 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
906 if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
907 return false;
908 if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
909 return false;
911 switch (code)
913 case LSHIFT_EXPR:
914 case RSHIFT_EXPR:
915 case LROTATE_EXPR:
916 case RROTATE_EXPR:
917 return true;
919 default:
920 break;
923 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
924 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
925 && TYPE_MODE (type1) == TYPE_MODE (type2);
929 /* Combine two integer constants ARG1 and ARG2 under operation CODE
930 to produce a new constant. Return NULL_TREE if we don't know how
931 to evaluate CODE at compile-time.
933 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
935 tree
936 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
938 double_int op1, op2, res, tmp;
939 tree t;
940 tree type = TREE_TYPE (arg1);
941 bool uns = TYPE_UNSIGNED (type);
942 bool is_sizetype
943 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
944 bool overflow = false;
946 op1 = tree_to_double_int (arg1);
947 op2 = tree_to_double_int (arg2);
949 switch (code)
951 case BIT_IOR_EXPR:
952 res = double_int_ior (op1, op2);
953 break;
955 case BIT_XOR_EXPR:
956 res = double_int_xor (op1, op2);
957 break;
959 case BIT_AND_EXPR:
960 res = double_int_and (op1, op2);
961 break;
963 case RSHIFT_EXPR:
964 res = double_int_rshift (op1, double_int_to_shwi (op2),
965 TYPE_PRECISION (type), !uns);
966 break;
968 case LSHIFT_EXPR:
969 /* It's unclear from the C standard whether shifts can overflow.
970 The following code ignores overflow; perhaps a C standard
971 interpretation ruling is needed. */
972 res = double_int_lshift (op1, double_int_to_shwi (op2),
973 TYPE_PRECISION (type), !uns);
974 break;
976 case RROTATE_EXPR:
977 res = double_int_rrotate (op1, double_int_to_shwi (op2),
978 TYPE_PRECISION (type));
979 break;
981 case LROTATE_EXPR:
982 res = double_int_lrotate (op1, double_int_to_shwi (op2),
983 TYPE_PRECISION (type));
984 break;
986 case PLUS_EXPR:
987 overflow = add_double (op1.low, op1.high, op2.low, op2.high,
988 &res.low, &res.high);
989 break;
991 case MINUS_EXPR:
992 neg_double (op2.low, op2.high, &res.low, &res.high);
993 add_double (op1.low, op1.high, res.low, res.high,
994 &res.low, &res.high);
995 overflow = OVERFLOW_SUM_SIGN (res.high, op2.high, op1.high);
996 break;
998 case MULT_EXPR:
999 overflow = mul_double (op1.low, op1.high, op2.low, op2.high,
1000 &res.low, &res.high);
1001 break;
1003 case TRUNC_DIV_EXPR:
1004 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1005 case EXACT_DIV_EXPR:
1006 /* This is a shortcut for a common special case. */
1007 if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1008 && !TREE_OVERFLOW (arg1)
1009 && !TREE_OVERFLOW (arg2)
1010 && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1012 if (code == CEIL_DIV_EXPR)
1013 op1.low += op2.low - 1;
1015 res.low = op1.low / op2.low, res.high = 0;
1016 break;
1019 /* ... fall through ... */
1021 case ROUND_DIV_EXPR:
1022 if (double_int_zero_p (op2))
1023 return NULL_TREE;
1024 if (double_int_one_p (op2))
1026 res = op1;
1027 break;
1029 if (double_int_equal_p (op1, op2)
1030 && ! double_int_zero_p (op1))
1032 res = double_int_one;
1033 break;
1035 overflow = div_and_round_double (code, uns,
1036 op1.low, op1.high, op2.low, op2.high,
1037 &res.low, &res.high,
1038 &tmp.low, &tmp.high);
1039 break;
1041 case TRUNC_MOD_EXPR:
1042 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1043 /* This is a shortcut for a common special case. */
1044 if (op2.high == 0 && (HOST_WIDE_INT) op2.low > 0
1045 && !TREE_OVERFLOW (arg1)
1046 && !TREE_OVERFLOW (arg2)
1047 && op1.high == 0 && (HOST_WIDE_INT) op1.low >= 0)
1049 if (code == CEIL_MOD_EXPR)
1050 op1.low += op2.low - 1;
1051 res.low = op1.low % op2.low, res.high = 0;
1052 break;
1055 /* ... fall through ... */
1057 case ROUND_MOD_EXPR:
1058 if (double_int_zero_p (op2))
1059 return NULL_TREE;
1060 overflow = div_and_round_double (code, uns,
1061 op1.low, op1.high, op2.low, op2.high,
1062 &tmp.low, &tmp.high,
1063 &res.low, &res.high);
1064 break;
1066 case MIN_EXPR:
1067 res = double_int_min (op1, op2, uns);
1068 break;
1070 case MAX_EXPR:
1071 res = double_int_max (op1, op2, uns);
1072 break;
1074 default:
1075 return NULL_TREE;
1078 if (notrunc)
1080 t = build_int_cst_wide (TREE_TYPE (arg1), res.low, res.high);
1082 /* Propagate overflow flags ourselves. */
1083 if (((!uns || is_sizetype) && overflow)
1084 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1086 t = copy_node (t);
1087 TREE_OVERFLOW (t) = 1;
1090 else
1091 t = force_fit_type_double (TREE_TYPE (arg1), res, 1,
1092 ((!uns || is_sizetype) && overflow)
1093 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1095 return t;
1098 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1099 constant. We assume ARG1 and ARG2 have the same data type, or at least
1100 are the same kind of constant and the same machine mode. Return zero if
1101 combining the constants is not allowed in the current operating mode. */
1103 static tree
1104 const_binop (enum tree_code code, tree arg1, tree arg2)
1106 /* Sanity check for the recursive cases. */
1107 if (!arg1 || !arg2)
1108 return NULL_TREE;
1110 STRIP_NOPS (arg1);
1111 STRIP_NOPS (arg2);
1113 if (TREE_CODE (arg1) == INTEGER_CST)
1114 return int_const_binop (code, arg1, arg2, 0);
1116 if (TREE_CODE (arg1) == REAL_CST)
1118 enum machine_mode mode;
1119 REAL_VALUE_TYPE d1;
1120 REAL_VALUE_TYPE d2;
1121 REAL_VALUE_TYPE value;
1122 REAL_VALUE_TYPE result;
1123 bool inexact;
1124 tree t, type;
1126 /* The following codes are handled by real_arithmetic. */
1127 switch (code)
1129 case PLUS_EXPR:
1130 case MINUS_EXPR:
1131 case MULT_EXPR:
1132 case RDIV_EXPR:
1133 case MIN_EXPR:
1134 case MAX_EXPR:
1135 break;
1137 default:
1138 return NULL_TREE;
1141 d1 = TREE_REAL_CST (arg1);
1142 d2 = TREE_REAL_CST (arg2);
1144 type = TREE_TYPE (arg1);
1145 mode = TYPE_MODE (type);
1147 /* Don't perform operation if we honor signaling NaNs and
1148 either operand is a NaN. */
1149 if (HONOR_SNANS (mode)
1150 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1151 return NULL_TREE;
1153 /* Don't perform operation if it would raise a division
1154 by zero exception. */
1155 if (code == RDIV_EXPR
1156 && REAL_VALUES_EQUAL (d2, dconst0)
1157 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1158 return NULL_TREE;
1160 /* If either operand is a NaN, just return it. Otherwise, set up
1161 for floating-point trap; we return an overflow. */
1162 if (REAL_VALUE_ISNAN (d1))
1163 return arg1;
1164 else if (REAL_VALUE_ISNAN (d2))
1165 return arg2;
1167 inexact = real_arithmetic (&value, code, &d1, &d2);
1168 real_convert (&result, mode, &value);
1170 /* Don't constant fold this floating point operation if
1171 the result has overflowed and flag_trapping_math. */
1172 if (flag_trapping_math
1173 && MODE_HAS_INFINITIES (mode)
1174 && REAL_VALUE_ISINF (result)
1175 && !REAL_VALUE_ISINF (d1)
1176 && !REAL_VALUE_ISINF (d2))
1177 return NULL_TREE;
1179 /* Don't constant fold this floating point operation if the
1180 result may dependent upon the run-time rounding mode and
1181 flag_rounding_math is set, or if GCC's software emulation
1182 is unable to accurately represent the result. */
1183 if ((flag_rounding_math
1184 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1185 && (inexact || !real_identical (&result, &value)))
1186 return NULL_TREE;
1188 t = build_real (type, result);
1190 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1191 return t;
1194 if (TREE_CODE (arg1) == FIXED_CST)
1196 FIXED_VALUE_TYPE f1;
1197 FIXED_VALUE_TYPE f2;
1198 FIXED_VALUE_TYPE result;
1199 tree t, type;
1200 int sat_p;
1201 bool overflow_p;
1203 /* The following codes are handled by fixed_arithmetic. */
1204 switch (code)
1206 case PLUS_EXPR:
1207 case MINUS_EXPR:
1208 case MULT_EXPR:
1209 case TRUNC_DIV_EXPR:
1210 f2 = TREE_FIXED_CST (arg2);
1211 break;
1213 case LSHIFT_EXPR:
1214 case RSHIFT_EXPR:
1215 f2.data.high = TREE_INT_CST_HIGH (arg2);
1216 f2.data.low = TREE_INT_CST_LOW (arg2);
1217 f2.mode = SImode;
1218 break;
1220 default:
1221 return NULL_TREE;
1224 f1 = TREE_FIXED_CST (arg1);
1225 type = TREE_TYPE (arg1);
1226 sat_p = TYPE_SATURATING (type);
1227 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1228 t = build_fixed (type, result);
1229 /* Propagate overflow flags. */
1230 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1231 TREE_OVERFLOW (t) = 1;
1232 return t;
1235 if (TREE_CODE (arg1) == COMPLEX_CST)
1237 tree type = TREE_TYPE (arg1);
1238 tree r1 = TREE_REALPART (arg1);
1239 tree i1 = TREE_IMAGPART (arg1);
1240 tree r2 = TREE_REALPART (arg2);
1241 tree i2 = TREE_IMAGPART (arg2);
1242 tree real, imag;
1244 switch (code)
1246 case PLUS_EXPR:
1247 case MINUS_EXPR:
1248 real = const_binop (code, r1, r2);
1249 imag = const_binop (code, i1, i2);
1250 break;
1252 case MULT_EXPR:
1253 if (COMPLEX_FLOAT_TYPE_P (type))
1254 return do_mpc_arg2 (arg1, arg2, type,
1255 /* do_nonfinite= */ folding_initializer,
1256 mpc_mul);
1258 real = const_binop (MINUS_EXPR,
1259 const_binop (MULT_EXPR, r1, r2),
1260 const_binop (MULT_EXPR, i1, i2));
1261 imag = const_binop (PLUS_EXPR,
1262 const_binop (MULT_EXPR, r1, i2),
1263 const_binop (MULT_EXPR, i1, r2));
1264 break;
1266 case RDIV_EXPR:
1267 if (COMPLEX_FLOAT_TYPE_P (type))
1268 return do_mpc_arg2 (arg1, arg2, type,
1269 /* do_nonfinite= */ folding_initializer,
1270 mpc_div);
1271 /* Fallthru ... */
1272 case TRUNC_DIV_EXPR:
1273 case CEIL_DIV_EXPR:
1274 case FLOOR_DIV_EXPR:
1275 case ROUND_DIV_EXPR:
1276 if (flag_complex_method == 0)
1278 /* Keep this algorithm in sync with
1279 tree-complex.c:expand_complex_div_straight().
1281 Expand complex division to scalars, straightforward algorithm.
1282 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1283 t = br*br + bi*bi
1285 tree magsquared
1286 = const_binop (PLUS_EXPR,
1287 const_binop (MULT_EXPR, r2, r2),
1288 const_binop (MULT_EXPR, i2, i2));
1289 tree t1
1290 = const_binop (PLUS_EXPR,
1291 const_binop (MULT_EXPR, r1, r2),
1292 const_binop (MULT_EXPR, i1, i2));
1293 tree t2
1294 = const_binop (MINUS_EXPR,
1295 const_binop (MULT_EXPR, i1, r2),
1296 const_binop (MULT_EXPR, r1, i2));
1298 real = const_binop (code, t1, magsquared);
1299 imag = const_binop (code, t2, magsquared);
1301 else
1303 /* Keep this algorithm in sync with
1304 tree-complex.c:expand_complex_div_wide().
1306 Expand complex division to scalars, modified algorithm to minimize
1307 overflow with wide input ranges. */
1308 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1309 fold_abs_const (r2, TREE_TYPE (type)),
1310 fold_abs_const (i2, TREE_TYPE (type)));
1312 if (integer_nonzerop (compare))
1314 /* In the TRUE branch, we compute
1315 ratio = br/bi;
1316 div = (br * ratio) + bi;
1317 tr = (ar * ratio) + ai;
1318 ti = (ai * ratio) - ar;
1319 tr = tr / div;
1320 ti = ti / div; */
1321 tree ratio = const_binop (code, r2, i2);
1322 tree div = const_binop (PLUS_EXPR, i2,
1323 const_binop (MULT_EXPR, r2, ratio));
1324 real = const_binop (MULT_EXPR, r1, ratio);
1325 real = const_binop (PLUS_EXPR, real, i1);
1326 real = const_binop (code, real, div);
1328 imag = const_binop (MULT_EXPR, i1, ratio);
1329 imag = const_binop (MINUS_EXPR, imag, r1);
1330 imag = const_binop (code, imag, div);
1332 else
1334 /* In the FALSE branch, we compute
1335 ratio = d/c;
1336 divisor = (d * ratio) + c;
1337 tr = (b * ratio) + a;
1338 ti = b - (a * ratio);
1339 tr = tr / div;
1340 ti = ti / div; */
1341 tree ratio = const_binop (code, i2, r2);
1342 tree div = const_binop (PLUS_EXPR, r2,
1343 const_binop (MULT_EXPR, i2, ratio));
1345 real = const_binop (MULT_EXPR, i1, ratio);
1346 real = const_binop (PLUS_EXPR, real, r1);
1347 real = const_binop (code, real, div);
1349 imag = const_binop (MULT_EXPR, r1, ratio);
1350 imag = const_binop (MINUS_EXPR, i1, imag);
1351 imag = const_binop (code, imag, div);
1354 break;
1356 default:
1357 return NULL_TREE;
1360 if (real && imag)
1361 return build_complex (type, real, imag);
1364 if (TREE_CODE (arg1) == VECTOR_CST)
1366 tree type = TREE_TYPE(arg1);
1367 int count = TYPE_VECTOR_SUBPARTS (type), i;
1368 tree elements1, elements2, list = NULL_TREE;
1370 if(TREE_CODE(arg2) != VECTOR_CST)
1371 return NULL_TREE;
1373 elements1 = TREE_VECTOR_CST_ELTS (arg1);
1374 elements2 = TREE_VECTOR_CST_ELTS (arg2);
1376 for (i = 0; i < count; i++)
1378 tree elem1, elem2, elem;
1380 /* The trailing elements can be empty and should be treated as 0 */
1381 if(!elements1)
1382 elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1383 else
1385 elem1 = TREE_VALUE(elements1);
1386 elements1 = TREE_CHAIN (elements1);
1389 if(!elements2)
1390 elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1391 else
1393 elem2 = TREE_VALUE(elements2);
1394 elements2 = TREE_CHAIN (elements2);
1397 elem = const_binop (code, elem1, elem2);
1399 /* It is possible that const_binop cannot handle the given
1400 code and return NULL_TREE */
1401 if(elem == NULL_TREE)
1402 return NULL_TREE;
1404 list = tree_cons (NULL_TREE, elem, list);
1406 return build_vector(type, nreverse(list));
1408 return NULL_TREE;
1411 /* Create a size type INT_CST node with NUMBER sign extended. KIND
1412 indicates which particular sizetype to create. */
1414 tree
1415 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1417 return build_int_cst (sizetype_tab[(int) kind], number);
1420 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1421 is a tree code. The type of the result is taken from the operands.
1422 Both must be equivalent integer types, ala int_binop_types_match_p.
1423 If the operands are constant, so is the result. */
1425 tree
1426 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1428 tree type = TREE_TYPE (arg0);
1430 if (arg0 == error_mark_node || arg1 == error_mark_node)
1431 return error_mark_node;
1433 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1434 TREE_TYPE (arg1)));
1436 /* Handle the special case of two integer constants faster. */
1437 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1439 /* And some specific cases even faster than that. */
1440 if (code == PLUS_EXPR)
1442 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1443 return arg1;
1444 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1445 return arg0;
1447 else if (code == MINUS_EXPR)
1449 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1450 return arg0;
1452 else if (code == MULT_EXPR)
1454 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1455 return arg1;
1458 /* Handle general case of two integer constants. */
1459 return int_const_binop (code, arg0, arg1, 0);
1462 return fold_build2_loc (loc, code, type, arg0, arg1);
1465 /* Given two values, either both of sizetype or both of bitsizetype,
1466 compute the difference between the two values. Return the value
1467 in signed type corresponding to the type of the operands. */
1469 tree
1470 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1472 tree type = TREE_TYPE (arg0);
1473 tree ctype;
1475 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1476 TREE_TYPE (arg1)));
1478 /* If the type is already signed, just do the simple thing. */
1479 if (!TYPE_UNSIGNED (type))
1480 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1482 if (type == sizetype)
1483 ctype = ssizetype;
1484 else if (type == bitsizetype)
1485 ctype = sbitsizetype;
1486 else
1487 ctype = signed_type_for (type);
1489 /* If either operand is not a constant, do the conversions to the signed
1490 type and subtract. The hardware will do the right thing with any
1491 overflow in the subtraction. */
1492 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1493 return size_binop_loc (loc, MINUS_EXPR,
1494 fold_convert_loc (loc, ctype, arg0),
1495 fold_convert_loc (loc, ctype, arg1));
1497 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1498 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1499 overflow) and negate (which can't either). Special-case a result
1500 of zero while we're here. */
1501 if (tree_int_cst_equal (arg0, arg1))
1502 return build_int_cst (ctype, 0);
1503 else if (tree_int_cst_lt (arg1, arg0))
1504 return fold_convert_loc (loc, ctype,
1505 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1506 else
1507 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1508 fold_convert_loc (loc, ctype,
1509 size_binop_loc (loc,
1510 MINUS_EXPR,
1511 arg1, arg0)));
1514 /* A subroutine of fold_convert_const handling conversions of an
1515 INTEGER_CST to another integer type. */
1517 static tree
1518 fold_convert_const_int_from_int (tree type, const_tree arg1)
1520 tree t;
1522 /* Given an integer constant, make new constant with new type,
1523 appropriately sign-extended or truncated. */
1524 t = force_fit_type_double (type, tree_to_double_int (arg1),
1525 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1526 (TREE_INT_CST_HIGH (arg1) < 0
1527 && (TYPE_UNSIGNED (type)
1528 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1529 | TREE_OVERFLOW (arg1));
1531 return t;
1534 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1535 to an integer type. */
1537 static tree
1538 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1540 int overflow = 0;
1541 tree t;
1543 /* The following code implements the floating point to integer
1544 conversion rules required by the Java Language Specification,
1545 that IEEE NaNs are mapped to zero and values that overflow
1546 the target precision saturate, i.e. values greater than
1547 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1548 are mapped to INT_MIN. These semantics are allowed by the
1549 C and C++ standards that simply state that the behavior of
1550 FP-to-integer conversion is unspecified upon overflow. */
1552 double_int val;
1553 REAL_VALUE_TYPE r;
1554 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1556 switch (code)
1558 case FIX_TRUNC_EXPR:
1559 real_trunc (&r, VOIDmode, &x);
1560 break;
1562 default:
1563 gcc_unreachable ();
1566 /* If R is NaN, return zero and show we have an overflow. */
1567 if (REAL_VALUE_ISNAN (r))
1569 overflow = 1;
1570 val = double_int_zero;
1573 /* See if R is less than the lower bound or greater than the
1574 upper bound. */
1576 if (! overflow)
1578 tree lt = TYPE_MIN_VALUE (type);
1579 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1580 if (REAL_VALUES_LESS (r, l))
1582 overflow = 1;
1583 val = tree_to_double_int (lt);
1587 if (! overflow)
1589 tree ut = TYPE_MAX_VALUE (type);
1590 if (ut)
1592 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1593 if (REAL_VALUES_LESS (u, r))
1595 overflow = 1;
1596 val = tree_to_double_int (ut);
1601 if (! overflow)
1602 real_to_integer2 ((HOST_WIDE_INT *) &val.low, &val.high, &r);
1604 t = force_fit_type_double (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1605 return t;
1608 /* A subroutine of fold_convert_const handling conversions of a
1609 FIXED_CST to an integer type. */
1611 static tree
1612 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1614 tree t;
1615 double_int temp, temp_trunc;
1616 unsigned int mode;
1618 /* Right shift FIXED_CST to temp by fbit. */
1619 temp = TREE_FIXED_CST (arg1).data;
1620 mode = TREE_FIXED_CST (arg1).mode;
1621 if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
1623 temp = double_int_rshift (temp, GET_MODE_FBIT (mode),
1624 HOST_BITS_PER_DOUBLE_INT,
1625 SIGNED_FIXED_POINT_MODE_P (mode));
1627 /* Left shift temp to temp_trunc by fbit. */
1628 temp_trunc = double_int_lshift (temp, GET_MODE_FBIT (mode),
1629 HOST_BITS_PER_DOUBLE_INT,
1630 SIGNED_FIXED_POINT_MODE_P (mode));
1632 else
1634 temp = double_int_zero;
1635 temp_trunc = double_int_zero;
1638 /* If FIXED_CST is negative, we need to round the value toward 0.
1639 By checking if the fractional bits are not zero to add 1 to temp. */
1640 if (SIGNED_FIXED_POINT_MODE_P (mode)
1641 && double_int_negative_p (temp_trunc)
1642 && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
1643 temp = double_int_add (temp, double_int_one);
1645 /* Given a fixed-point constant, make new constant with new type,
1646 appropriately sign-extended or truncated. */
1647 t = force_fit_type_double (type, temp, -1,
1648 (double_int_negative_p (temp)
1649 && (TYPE_UNSIGNED (type)
1650 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1651 | TREE_OVERFLOW (arg1));
1653 return t;
1656 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1657 to another floating point type. */
1659 static tree
1660 fold_convert_const_real_from_real (tree type, const_tree arg1)
1662 REAL_VALUE_TYPE value;
1663 tree t;
1665 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1666 t = build_real (type, value);
1668 /* If converting an infinity or NAN to a representation that doesn't
1669 have one, set the overflow bit so that we can produce some kind of
1670 error message at the appropriate point if necessary. It's not the
1671 most user-friendly message, but it's better than nothing. */
1672 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1673 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1674 TREE_OVERFLOW (t) = 1;
1675 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1676 && !MODE_HAS_NANS (TYPE_MODE (type)))
1677 TREE_OVERFLOW (t) = 1;
1678 /* Regular overflow, conversion produced an infinity in a mode that
1679 can't represent them. */
1680 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1681 && REAL_VALUE_ISINF (value)
1682 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1683 TREE_OVERFLOW (t) = 1;
1684 else
1685 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1686 return t;
1689 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1690 to a floating point type. */
1692 static tree
1693 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1695 REAL_VALUE_TYPE value;
1696 tree t;
1698 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1699 t = build_real (type, value);
1701 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1702 return t;
1705 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1706 to another fixed-point type. */
1708 static tree
1709 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
1711 FIXED_VALUE_TYPE value;
1712 tree t;
1713 bool overflow_p;
1715 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
1716 TYPE_SATURATING (type));
1717 t = build_fixed (type, value);
1719 /* Propagate overflow flags. */
1720 if (overflow_p | TREE_OVERFLOW (arg1))
1721 TREE_OVERFLOW (t) = 1;
1722 return t;
1725 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
1726 to a fixed-point type. */
1728 static tree
1729 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
1731 FIXED_VALUE_TYPE value;
1732 tree t;
1733 bool overflow_p;
1735 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
1736 TREE_INT_CST (arg1),
1737 TYPE_UNSIGNED (TREE_TYPE (arg1)),
1738 TYPE_SATURATING (type));
1739 t = build_fixed (type, value);
1741 /* Propagate overflow flags. */
1742 if (overflow_p | TREE_OVERFLOW (arg1))
1743 TREE_OVERFLOW (t) = 1;
1744 return t;
1747 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1748 to a fixed-point type. */
1750 static tree
1751 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
1753 FIXED_VALUE_TYPE value;
1754 tree t;
1755 bool overflow_p;
1757 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
1758 &TREE_REAL_CST (arg1),
1759 TYPE_SATURATING (type));
1760 t = build_fixed (type, value);
1762 /* Propagate overflow flags. */
1763 if (overflow_p | TREE_OVERFLOW (arg1))
1764 TREE_OVERFLOW (t) = 1;
1765 return t;
1768 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1769 type TYPE. If no simplification can be done return NULL_TREE. */
1771 static tree
1772 fold_convert_const (enum tree_code code, tree type, tree arg1)
1774 if (TREE_TYPE (arg1) == type)
1775 return arg1;
1777 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
1778 || TREE_CODE (type) == OFFSET_TYPE)
1780 if (TREE_CODE (arg1) == INTEGER_CST)
1781 return fold_convert_const_int_from_int (type, arg1);
1782 else if (TREE_CODE (arg1) == REAL_CST)
1783 return fold_convert_const_int_from_real (code, type, arg1);
1784 else if (TREE_CODE (arg1) == FIXED_CST)
1785 return fold_convert_const_int_from_fixed (type, arg1);
1787 else if (TREE_CODE (type) == REAL_TYPE)
1789 if (TREE_CODE (arg1) == INTEGER_CST)
1790 return build_real_from_int_cst (type, arg1);
1791 else if (TREE_CODE (arg1) == REAL_CST)
1792 return fold_convert_const_real_from_real (type, arg1);
1793 else if (TREE_CODE (arg1) == FIXED_CST)
1794 return fold_convert_const_real_from_fixed (type, arg1);
1796 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
1798 if (TREE_CODE (arg1) == FIXED_CST)
1799 return fold_convert_const_fixed_from_fixed (type, arg1);
1800 else if (TREE_CODE (arg1) == INTEGER_CST)
1801 return fold_convert_const_fixed_from_int (type, arg1);
1802 else if (TREE_CODE (arg1) == REAL_CST)
1803 return fold_convert_const_fixed_from_real (type, arg1);
1805 return NULL_TREE;
1808 /* Construct a vector of zero elements of vector type TYPE. */
1810 static tree
1811 build_zero_vector (tree type)
1813 tree t;
1815 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1816 return build_vector_from_val (type, t);
1819 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
1821 bool
1822 fold_convertible_p (const_tree type, const_tree arg)
1824 tree orig = TREE_TYPE (arg);
1826 if (type == orig)
1827 return true;
1829 if (TREE_CODE (arg) == ERROR_MARK
1830 || TREE_CODE (type) == ERROR_MARK
1831 || TREE_CODE (orig) == ERROR_MARK)
1832 return false;
1834 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1835 return true;
1837 switch (TREE_CODE (type))
1839 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1840 case POINTER_TYPE: case REFERENCE_TYPE:
1841 case OFFSET_TYPE:
1842 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1843 || TREE_CODE (orig) == OFFSET_TYPE)
1844 return true;
1845 return (TREE_CODE (orig) == VECTOR_TYPE
1846 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1848 case REAL_TYPE:
1849 case FIXED_POINT_TYPE:
1850 case COMPLEX_TYPE:
1851 case VECTOR_TYPE:
1852 case VOID_TYPE:
1853 return TREE_CODE (type) == TREE_CODE (orig);
1855 default:
1856 return false;
1860 /* Convert expression ARG to type TYPE. Used by the middle-end for
1861 simple conversions in preference to calling the front-end's convert. */
1863 tree
1864 fold_convert_loc (location_t loc, tree type, tree arg)
1866 tree orig = TREE_TYPE (arg);
1867 tree tem;
1869 if (type == orig)
1870 return arg;
1872 if (TREE_CODE (arg) == ERROR_MARK
1873 || TREE_CODE (type) == ERROR_MARK
1874 || TREE_CODE (orig) == ERROR_MARK)
1875 return error_mark_node;
1877 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1878 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1880 switch (TREE_CODE (type))
1882 case POINTER_TYPE:
1883 case REFERENCE_TYPE:
1884 /* Handle conversions between pointers to different address spaces. */
1885 if (POINTER_TYPE_P (orig)
1886 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
1887 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
1888 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
1889 /* fall through */
1891 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1892 case OFFSET_TYPE:
1893 if (TREE_CODE (arg) == INTEGER_CST)
1895 tem = fold_convert_const (NOP_EXPR, type, arg);
1896 if (tem != NULL_TREE)
1897 return tem;
1899 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1900 || TREE_CODE (orig) == OFFSET_TYPE)
1901 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1902 if (TREE_CODE (orig) == COMPLEX_TYPE)
1903 return fold_convert_loc (loc, type,
1904 fold_build1_loc (loc, REALPART_EXPR,
1905 TREE_TYPE (orig), arg));
1906 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1907 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1908 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1910 case REAL_TYPE:
1911 if (TREE_CODE (arg) == INTEGER_CST)
1913 tem = fold_convert_const (FLOAT_EXPR, type, arg);
1914 if (tem != NULL_TREE)
1915 return tem;
1917 else if (TREE_CODE (arg) == REAL_CST)
1919 tem = fold_convert_const (NOP_EXPR, type, arg);
1920 if (tem != NULL_TREE)
1921 return tem;
1923 else if (TREE_CODE (arg) == FIXED_CST)
1925 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1926 if (tem != NULL_TREE)
1927 return tem;
1930 switch (TREE_CODE (orig))
1932 case INTEGER_TYPE:
1933 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1934 case POINTER_TYPE: case REFERENCE_TYPE:
1935 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
1937 case REAL_TYPE:
1938 return fold_build1_loc (loc, NOP_EXPR, type, arg);
1940 case FIXED_POINT_TYPE:
1941 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1943 case COMPLEX_TYPE:
1944 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1945 return fold_convert_loc (loc, type, tem);
1947 default:
1948 gcc_unreachable ();
1951 case FIXED_POINT_TYPE:
1952 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
1953 || TREE_CODE (arg) == REAL_CST)
1955 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
1956 if (tem != NULL_TREE)
1957 goto fold_convert_exit;
1960 switch (TREE_CODE (orig))
1962 case FIXED_POINT_TYPE:
1963 case INTEGER_TYPE:
1964 case ENUMERAL_TYPE:
1965 case BOOLEAN_TYPE:
1966 case REAL_TYPE:
1967 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
1969 case COMPLEX_TYPE:
1970 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
1971 return fold_convert_loc (loc, type, tem);
1973 default:
1974 gcc_unreachable ();
1977 case COMPLEX_TYPE:
1978 switch (TREE_CODE (orig))
1980 case INTEGER_TYPE:
1981 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1982 case POINTER_TYPE: case REFERENCE_TYPE:
1983 case REAL_TYPE:
1984 case FIXED_POINT_TYPE:
1985 return fold_build2_loc (loc, COMPLEX_EXPR, type,
1986 fold_convert_loc (loc, TREE_TYPE (type), arg),
1987 fold_convert_loc (loc, TREE_TYPE (type),
1988 integer_zero_node));
1989 case COMPLEX_TYPE:
1991 tree rpart, ipart;
1993 if (TREE_CODE (arg) == COMPLEX_EXPR)
1995 rpart = fold_convert_loc (loc, TREE_TYPE (type),
1996 TREE_OPERAND (arg, 0));
1997 ipart = fold_convert_loc (loc, TREE_TYPE (type),
1998 TREE_OPERAND (arg, 1));
1999 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2002 arg = save_expr (arg);
2003 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2004 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2005 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2006 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2007 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2010 default:
2011 gcc_unreachable ();
2014 case VECTOR_TYPE:
2015 if (integer_zerop (arg))
2016 return build_zero_vector (type);
2017 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2018 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2019 || TREE_CODE (orig) == VECTOR_TYPE);
2020 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2022 case VOID_TYPE:
2023 tem = fold_ignored_result (arg);
2024 if (TREE_CODE (tem) == MODIFY_EXPR)
2025 goto fold_convert_exit;
2026 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2028 default:
2029 gcc_unreachable ();
2031 fold_convert_exit:
2032 protected_set_expr_location_unshare (tem, loc);
2033 return tem;
2036 /* Return false if expr can be assumed not to be an lvalue, true
2037 otherwise. */
2039 static bool
2040 maybe_lvalue_p (const_tree x)
2042 /* We only need to wrap lvalue tree codes. */
2043 switch (TREE_CODE (x))
2045 case VAR_DECL:
2046 case PARM_DECL:
2047 case RESULT_DECL:
2048 case LABEL_DECL:
2049 case FUNCTION_DECL:
2050 case SSA_NAME:
2052 case COMPONENT_REF:
2053 case MEM_REF:
2054 case INDIRECT_REF:
2055 case ARRAY_REF:
2056 case ARRAY_RANGE_REF:
2057 case BIT_FIELD_REF:
2058 case OBJ_TYPE_REF:
2060 case REALPART_EXPR:
2061 case IMAGPART_EXPR:
2062 case PREINCREMENT_EXPR:
2063 case PREDECREMENT_EXPR:
2064 case SAVE_EXPR:
2065 case TRY_CATCH_EXPR:
2066 case WITH_CLEANUP_EXPR:
2067 case COMPOUND_EXPR:
2068 case MODIFY_EXPR:
2069 case TARGET_EXPR:
2070 case COND_EXPR:
2071 case BIND_EXPR:
2072 break;
2074 default:
2075 /* Assume the worst for front-end tree codes. */
2076 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2077 break;
2078 return false;
2081 return true;
2084 /* Return an expr equal to X but certainly not valid as an lvalue. */
2086 tree
2087 non_lvalue_loc (location_t loc, tree x)
2089 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2090 us. */
2091 if (in_gimple_form)
2092 return x;
2094 if (! maybe_lvalue_p (x))
2095 return x;
2096 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2099 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2100 Zero means allow extended lvalues. */
2102 int pedantic_lvalues;
2104 /* When pedantic, return an expr equal to X but certainly not valid as a
2105 pedantic lvalue. Otherwise, return X. */
2107 static tree
2108 pedantic_non_lvalue_loc (location_t loc, tree x)
2110 if (pedantic_lvalues)
2111 return non_lvalue_loc (loc, x);
2113 return protected_set_expr_location_unshare (x, loc);
2116 /* Given a tree comparison code, return the code that is the logical inverse
2117 of the given code. It is not safe to do this for floating-point
2118 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2119 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2121 enum tree_code
2122 invert_tree_comparison (enum tree_code code, bool honor_nans)
2124 if (honor_nans && flag_trapping_math)
2125 return ERROR_MARK;
2127 switch (code)
2129 case EQ_EXPR:
2130 return NE_EXPR;
2131 case NE_EXPR:
2132 return EQ_EXPR;
2133 case GT_EXPR:
2134 return honor_nans ? UNLE_EXPR : LE_EXPR;
2135 case GE_EXPR:
2136 return honor_nans ? UNLT_EXPR : LT_EXPR;
2137 case LT_EXPR:
2138 return honor_nans ? UNGE_EXPR : GE_EXPR;
2139 case LE_EXPR:
2140 return honor_nans ? UNGT_EXPR : GT_EXPR;
2141 case LTGT_EXPR:
2142 return UNEQ_EXPR;
2143 case UNEQ_EXPR:
2144 return LTGT_EXPR;
2145 case UNGT_EXPR:
2146 return LE_EXPR;
2147 case UNGE_EXPR:
2148 return LT_EXPR;
2149 case UNLT_EXPR:
2150 return GE_EXPR;
2151 case UNLE_EXPR:
2152 return GT_EXPR;
2153 case ORDERED_EXPR:
2154 return UNORDERED_EXPR;
2155 case UNORDERED_EXPR:
2156 return ORDERED_EXPR;
2157 default:
2158 gcc_unreachable ();
2162 /* Similar, but return the comparison that results if the operands are
2163 swapped. This is safe for floating-point. */
2165 enum tree_code
2166 swap_tree_comparison (enum tree_code code)
2168 switch (code)
2170 case EQ_EXPR:
2171 case NE_EXPR:
2172 case ORDERED_EXPR:
2173 case UNORDERED_EXPR:
2174 case LTGT_EXPR:
2175 case UNEQ_EXPR:
2176 return code;
2177 case GT_EXPR:
2178 return LT_EXPR;
2179 case GE_EXPR:
2180 return LE_EXPR;
2181 case LT_EXPR:
2182 return GT_EXPR;
2183 case LE_EXPR:
2184 return GE_EXPR;
2185 case UNGT_EXPR:
2186 return UNLT_EXPR;
2187 case UNGE_EXPR:
2188 return UNLE_EXPR;
2189 case UNLT_EXPR:
2190 return UNGT_EXPR;
2191 case UNLE_EXPR:
2192 return UNGE_EXPR;
2193 default:
2194 gcc_unreachable ();
2199 /* Convert a comparison tree code from an enum tree_code representation
2200 into a compcode bit-based encoding. This function is the inverse of
2201 compcode_to_comparison. */
2203 static enum comparison_code
2204 comparison_to_compcode (enum tree_code code)
2206 switch (code)
2208 case LT_EXPR:
2209 return COMPCODE_LT;
2210 case EQ_EXPR:
2211 return COMPCODE_EQ;
2212 case LE_EXPR:
2213 return COMPCODE_LE;
2214 case GT_EXPR:
2215 return COMPCODE_GT;
2216 case NE_EXPR:
2217 return COMPCODE_NE;
2218 case GE_EXPR:
2219 return COMPCODE_GE;
2220 case ORDERED_EXPR:
2221 return COMPCODE_ORD;
2222 case UNORDERED_EXPR:
2223 return COMPCODE_UNORD;
2224 case UNLT_EXPR:
2225 return COMPCODE_UNLT;
2226 case UNEQ_EXPR:
2227 return COMPCODE_UNEQ;
2228 case UNLE_EXPR:
2229 return COMPCODE_UNLE;
2230 case UNGT_EXPR:
2231 return COMPCODE_UNGT;
2232 case LTGT_EXPR:
2233 return COMPCODE_LTGT;
2234 case UNGE_EXPR:
2235 return COMPCODE_UNGE;
2236 default:
2237 gcc_unreachable ();
2241 /* Convert a compcode bit-based encoding of a comparison operator back
2242 to GCC's enum tree_code representation. This function is the
2243 inverse of comparison_to_compcode. */
2245 static enum tree_code
2246 compcode_to_comparison (enum comparison_code code)
2248 switch (code)
2250 case COMPCODE_LT:
2251 return LT_EXPR;
2252 case COMPCODE_EQ:
2253 return EQ_EXPR;
2254 case COMPCODE_LE:
2255 return LE_EXPR;
2256 case COMPCODE_GT:
2257 return GT_EXPR;
2258 case COMPCODE_NE:
2259 return NE_EXPR;
2260 case COMPCODE_GE:
2261 return GE_EXPR;
2262 case COMPCODE_ORD:
2263 return ORDERED_EXPR;
2264 case COMPCODE_UNORD:
2265 return UNORDERED_EXPR;
2266 case COMPCODE_UNLT:
2267 return UNLT_EXPR;
2268 case COMPCODE_UNEQ:
2269 return UNEQ_EXPR;
2270 case COMPCODE_UNLE:
2271 return UNLE_EXPR;
2272 case COMPCODE_UNGT:
2273 return UNGT_EXPR;
2274 case COMPCODE_LTGT:
2275 return LTGT_EXPR;
2276 case COMPCODE_UNGE:
2277 return UNGE_EXPR;
2278 default:
2279 gcc_unreachable ();
2283 /* Return a tree for the comparison which is the combination of
2284 doing the AND or OR (depending on CODE) of the two operations LCODE
2285 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2286 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2287 if this makes the transformation invalid. */
2289 tree
2290 combine_comparisons (location_t loc,
2291 enum tree_code code, enum tree_code lcode,
2292 enum tree_code rcode, tree truth_type,
2293 tree ll_arg, tree lr_arg)
2295 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2296 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2297 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2298 int compcode;
2300 switch (code)
2302 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2303 compcode = lcompcode & rcompcode;
2304 break;
2306 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2307 compcode = lcompcode | rcompcode;
2308 break;
2310 default:
2311 return NULL_TREE;
2314 if (!honor_nans)
2316 /* Eliminate unordered comparisons, as well as LTGT and ORD
2317 which are not used unless the mode has NaNs. */
2318 compcode &= ~COMPCODE_UNORD;
2319 if (compcode == COMPCODE_LTGT)
2320 compcode = COMPCODE_NE;
2321 else if (compcode == COMPCODE_ORD)
2322 compcode = COMPCODE_TRUE;
2324 else if (flag_trapping_math)
2326 /* Check that the original operation and the optimized ones will trap
2327 under the same condition. */
2328 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2329 && (lcompcode != COMPCODE_EQ)
2330 && (lcompcode != COMPCODE_ORD);
2331 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2332 && (rcompcode != COMPCODE_EQ)
2333 && (rcompcode != COMPCODE_ORD);
2334 bool trap = (compcode & COMPCODE_UNORD) == 0
2335 && (compcode != COMPCODE_EQ)
2336 && (compcode != COMPCODE_ORD);
2338 /* In a short-circuited boolean expression the LHS might be
2339 such that the RHS, if evaluated, will never trap. For
2340 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2341 if neither x nor y is NaN. (This is a mixed blessing: for
2342 example, the expression above will never trap, hence
2343 optimizing it to x < y would be invalid). */
2344 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2345 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2346 rtrap = false;
2348 /* If the comparison was short-circuited, and only the RHS
2349 trapped, we may now generate a spurious trap. */
2350 if (rtrap && !ltrap
2351 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2352 return NULL_TREE;
2354 /* If we changed the conditions that cause a trap, we lose. */
2355 if ((ltrap || rtrap) != trap)
2356 return NULL_TREE;
2359 if (compcode == COMPCODE_TRUE)
2360 return constant_boolean_node (true, truth_type);
2361 else if (compcode == COMPCODE_FALSE)
2362 return constant_boolean_node (false, truth_type);
2363 else
2365 enum tree_code tcode;
2367 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2368 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2372 /* Return nonzero if two operands (typically of the same tree node)
2373 are necessarily equal. If either argument has side-effects this
2374 function returns zero. FLAGS modifies behavior as follows:
2376 If OEP_ONLY_CONST is set, only return nonzero for constants.
2377 This function tests whether the operands are indistinguishable;
2378 it does not test whether they are equal using C's == operation.
2379 The distinction is important for IEEE floating point, because
2380 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2381 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2383 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2384 even though it may hold multiple values during a function.
2385 This is because a GCC tree node guarantees that nothing else is
2386 executed between the evaluation of its "operands" (which may often
2387 be evaluated in arbitrary order). Hence if the operands themselves
2388 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2389 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2390 unset means assuming isochronic (or instantaneous) tree equivalence.
2391 Unless comparing arbitrary expression trees, such as from different
2392 statements, this flag can usually be left unset.
2394 If OEP_PURE_SAME is set, then pure functions with identical arguments
2395 are considered the same. It is used when the caller has other ways
2396 to ensure that global memory is unchanged in between. */
2399 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2401 /* If either is ERROR_MARK, they aren't equal. */
2402 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2403 || TREE_TYPE (arg0) == error_mark_node
2404 || TREE_TYPE (arg1) == error_mark_node)
2405 return 0;
2407 /* Similar, if either does not have a type (like a released SSA name),
2408 they aren't equal. */
2409 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2410 return 0;
2412 /* Check equality of integer constants before bailing out due to
2413 precision differences. */
2414 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2415 return tree_int_cst_equal (arg0, arg1);
2417 /* If both types don't have the same signedness, then we can't consider
2418 them equal. We must check this before the STRIP_NOPS calls
2419 because they may change the signedness of the arguments. As pointers
2420 strictly don't have a signedness, require either two pointers or
2421 two non-pointers as well. */
2422 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2423 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2424 return 0;
2426 /* We cannot consider pointers to different address space equal. */
2427 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2428 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2429 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2430 return 0;
2432 /* If both types don't have the same precision, then it is not safe
2433 to strip NOPs. */
2434 if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
2435 return 0;
2437 STRIP_NOPS (arg0);
2438 STRIP_NOPS (arg1);
2440 /* In case both args are comparisons but with different comparison
2441 code, try to swap the comparison operands of one arg to produce
2442 a match and compare that variant. */
2443 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2444 && COMPARISON_CLASS_P (arg0)
2445 && COMPARISON_CLASS_P (arg1))
2447 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2449 if (TREE_CODE (arg0) == swap_code)
2450 return operand_equal_p (TREE_OPERAND (arg0, 0),
2451 TREE_OPERAND (arg1, 1), flags)
2452 && operand_equal_p (TREE_OPERAND (arg0, 1),
2453 TREE_OPERAND (arg1, 0), flags);
2456 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2457 /* This is needed for conversions and for COMPONENT_REF.
2458 Might as well play it safe and always test this. */
2459 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2460 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2461 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2462 return 0;
2464 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2465 We don't care about side effects in that case because the SAVE_EXPR
2466 takes care of that for us. In all other cases, two expressions are
2467 equal if they have no side effects. If we have two identical
2468 expressions with side effects that should be treated the same due
2469 to the only side effects being identical SAVE_EXPR's, that will
2470 be detected in the recursive calls below. */
2471 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2472 && (TREE_CODE (arg0) == SAVE_EXPR
2473 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2474 return 1;
2476 /* Next handle constant cases, those for which we can return 1 even
2477 if ONLY_CONST is set. */
2478 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2479 switch (TREE_CODE (arg0))
2481 case INTEGER_CST:
2482 return tree_int_cst_equal (arg0, arg1);
2484 case FIXED_CST:
2485 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2486 TREE_FIXED_CST (arg1));
2488 case REAL_CST:
2489 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2490 TREE_REAL_CST (arg1)))
2491 return 1;
2494 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
2496 /* If we do not distinguish between signed and unsigned zero,
2497 consider them equal. */
2498 if (real_zerop (arg0) && real_zerop (arg1))
2499 return 1;
2501 return 0;
2503 case VECTOR_CST:
2505 tree v1, v2;
2507 v1 = TREE_VECTOR_CST_ELTS (arg0);
2508 v2 = TREE_VECTOR_CST_ELTS (arg1);
2509 while (v1 && v2)
2511 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2512 flags))
2513 return 0;
2514 v1 = TREE_CHAIN (v1);
2515 v2 = TREE_CHAIN (v2);
2518 return v1 == v2;
2521 case COMPLEX_CST:
2522 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2523 flags)
2524 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2525 flags));
2527 case STRING_CST:
2528 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2529 && ! memcmp (TREE_STRING_POINTER (arg0),
2530 TREE_STRING_POINTER (arg1),
2531 TREE_STRING_LENGTH (arg0)));
2533 case ADDR_EXPR:
2534 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2536 default:
2537 break;
2540 if (flags & OEP_ONLY_CONST)
2541 return 0;
2543 /* Define macros to test an operand from arg0 and arg1 for equality and a
2544 variant that allows null and views null as being different from any
2545 non-null value. In the latter case, if either is null, the both
2546 must be; otherwise, do the normal comparison. */
2547 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2548 TREE_OPERAND (arg1, N), flags)
2550 #define OP_SAME_WITH_NULL(N) \
2551 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2552 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2554 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2556 case tcc_unary:
2557 /* Two conversions are equal only if signedness and modes match. */
2558 switch (TREE_CODE (arg0))
2560 CASE_CONVERT:
2561 case FIX_TRUNC_EXPR:
2562 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2563 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2564 return 0;
2565 break;
2566 default:
2567 break;
2570 return OP_SAME (0);
2573 case tcc_comparison:
2574 case tcc_binary:
2575 if (OP_SAME (0) && OP_SAME (1))
2576 return 1;
2578 /* For commutative ops, allow the other order. */
2579 return (commutative_tree_code (TREE_CODE (arg0))
2580 && operand_equal_p (TREE_OPERAND (arg0, 0),
2581 TREE_OPERAND (arg1, 1), flags)
2582 && operand_equal_p (TREE_OPERAND (arg0, 1),
2583 TREE_OPERAND (arg1, 0), flags));
2585 case tcc_reference:
2586 /* If either of the pointer (or reference) expressions we are
2587 dereferencing contain a side effect, these cannot be equal. */
2588 if (TREE_SIDE_EFFECTS (arg0)
2589 || TREE_SIDE_EFFECTS (arg1))
2590 return 0;
2592 switch (TREE_CODE (arg0))
2594 case INDIRECT_REF:
2595 case REALPART_EXPR:
2596 case IMAGPART_EXPR:
2597 return OP_SAME (0);
2599 case MEM_REF:
2600 /* Require equal access sizes, and similar pointer types.
2601 We can have incomplete types for array references of
2602 variable-sized arrays from the Fortran frontent
2603 though. */
2604 return ((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2605 || (TYPE_SIZE (TREE_TYPE (arg0))
2606 && TYPE_SIZE (TREE_TYPE (arg1))
2607 && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2608 TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2609 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg0, 1)))
2610 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg1, 1))))
2611 && OP_SAME (0) && OP_SAME (1));
2613 case ARRAY_REF:
2614 case ARRAY_RANGE_REF:
2615 /* Operands 2 and 3 may be null.
2616 Compare the array index by value if it is constant first as we
2617 may have different types but same value here. */
2618 return (OP_SAME (0)
2619 && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2620 TREE_OPERAND (arg1, 1))
2621 || OP_SAME (1))
2622 && OP_SAME_WITH_NULL (2)
2623 && OP_SAME_WITH_NULL (3));
2625 case COMPONENT_REF:
2626 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2627 may be NULL when we're called to compare MEM_EXPRs. */
2628 return OP_SAME_WITH_NULL (0)
2629 && OP_SAME (1)
2630 && OP_SAME_WITH_NULL (2);
2632 case BIT_FIELD_REF:
2633 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2635 default:
2636 return 0;
2639 case tcc_expression:
2640 switch (TREE_CODE (arg0))
2642 case ADDR_EXPR:
2643 case TRUTH_NOT_EXPR:
2644 return OP_SAME (0);
2646 case TRUTH_ANDIF_EXPR:
2647 case TRUTH_ORIF_EXPR:
2648 return OP_SAME (0) && OP_SAME (1);
2650 case FMA_EXPR:
2651 case WIDEN_MULT_PLUS_EXPR:
2652 case WIDEN_MULT_MINUS_EXPR:
2653 if (!OP_SAME (2))
2654 return 0;
2655 /* The multiplcation operands are commutative. */
2656 /* FALLTHRU */
2658 case TRUTH_AND_EXPR:
2659 case TRUTH_OR_EXPR:
2660 case TRUTH_XOR_EXPR:
2661 if (OP_SAME (0) && OP_SAME (1))
2662 return 1;
2664 /* Otherwise take into account this is a commutative operation. */
2665 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2666 TREE_OPERAND (arg1, 1), flags)
2667 && operand_equal_p (TREE_OPERAND (arg0, 1),
2668 TREE_OPERAND (arg1, 0), flags));
2670 case COND_EXPR:
2671 case VEC_COND_EXPR:
2672 case DOT_PROD_EXPR:
2673 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2675 default:
2676 return 0;
2679 case tcc_vl_exp:
2680 switch (TREE_CODE (arg0))
2682 case CALL_EXPR:
2683 /* If the CALL_EXPRs call different functions, then they
2684 clearly can not be equal. */
2685 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
2686 flags))
2687 return 0;
2690 unsigned int cef = call_expr_flags (arg0);
2691 if (flags & OEP_PURE_SAME)
2692 cef &= ECF_CONST | ECF_PURE;
2693 else
2694 cef &= ECF_CONST;
2695 if (!cef)
2696 return 0;
2699 /* Now see if all the arguments are the same. */
2701 const_call_expr_arg_iterator iter0, iter1;
2702 const_tree a0, a1;
2703 for (a0 = first_const_call_expr_arg (arg0, &iter0),
2704 a1 = first_const_call_expr_arg (arg1, &iter1);
2705 a0 && a1;
2706 a0 = next_const_call_expr_arg (&iter0),
2707 a1 = next_const_call_expr_arg (&iter1))
2708 if (! operand_equal_p (a0, a1, flags))
2709 return 0;
2711 /* If we get here and both argument lists are exhausted
2712 then the CALL_EXPRs are equal. */
2713 return ! (a0 || a1);
2715 default:
2716 return 0;
2719 case tcc_declaration:
2720 /* Consider __builtin_sqrt equal to sqrt. */
2721 return (TREE_CODE (arg0) == FUNCTION_DECL
2722 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2723 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2724 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2726 default:
2727 return 0;
2730 #undef OP_SAME
2731 #undef OP_SAME_WITH_NULL
2734 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2735 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2737 When in doubt, return 0. */
2739 static int
2740 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2742 int unsignedp1, unsignedpo;
2743 tree primarg0, primarg1, primother;
2744 unsigned int correct_width;
2746 if (operand_equal_p (arg0, arg1, 0))
2747 return 1;
2749 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2750 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2751 return 0;
2753 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2754 and see if the inner values are the same. This removes any
2755 signedness comparison, which doesn't matter here. */
2756 primarg0 = arg0, primarg1 = arg1;
2757 STRIP_NOPS (primarg0);
2758 STRIP_NOPS (primarg1);
2759 if (operand_equal_p (primarg0, primarg1, 0))
2760 return 1;
2762 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2763 actual comparison operand, ARG0.
2765 First throw away any conversions to wider types
2766 already present in the operands. */
2768 primarg1 = get_narrower (arg1, &unsignedp1);
2769 primother = get_narrower (other, &unsignedpo);
2771 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2772 if (unsignedp1 == unsignedpo
2773 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2774 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2776 tree type = TREE_TYPE (arg0);
2778 /* Make sure shorter operand is extended the right way
2779 to match the longer operand. */
2780 primarg1 = fold_convert (signed_or_unsigned_type_for
2781 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2783 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2784 return 1;
2787 return 0;
2790 /* See if ARG is an expression that is either a comparison or is performing
2791 arithmetic on comparisons. The comparisons must only be comparing
2792 two different values, which will be stored in *CVAL1 and *CVAL2; if
2793 they are nonzero it means that some operands have already been found.
2794 No variables may be used anywhere else in the expression except in the
2795 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2796 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2798 If this is true, return 1. Otherwise, return zero. */
2800 static int
2801 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2803 enum tree_code code = TREE_CODE (arg);
2804 enum tree_code_class tclass = TREE_CODE_CLASS (code);
2806 /* We can handle some of the tcc_expression cases here. */
2807 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2808 tclass = tcc_unary;
2809 else if (tclass == tcc_expression
2810 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2811 || code == COMPOUND_EXPR))
2812 tclass = tcc_binary;
2814 else if (tclass == tcc_expression && code == SAVE_EXPR
2815 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2817 /* If we've already found a CVAL1 or CVAL2, this expression is
2818 two complex to handle. */
2819 if (*cval1 || *cval2)
2820 return 0;
2822 tclass = tcc_unary;
2823 *save_p = 1;
2826 switch (tclass)
2828 case tcc_unary:
2829 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2831 case tcc_binary:
2832 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2833 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2834 cval1, cval2, save_p));
2836 case tcc_constant:
2837 return 1;
2839 case tcc_expression:
2840 if (code == COND_EXPR)
2841 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2842 cval1, cval2, save_p)
2843 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2844 cval1, cval2, save_p)
2845 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2846 cval1, cval2, save_p));
2847 return 0;
2849 case tcc_comparison:
2850 /* First see if we can handle the first operand, then the second. For
2851 the second operand, we know *CVAL1 can't be zero. It must be that
2852 one side of the comparison is each of the values; test for the
2853 case where this isn't true by failing if the two operands
2854 are the same. */
2856 if (operand_equal_p (TREE_OPERAND (arg, 0),
2857 TREE_OPERAND (arg, 1), 0))
2858 return 0;
2860 if (*cval1 == 0)
2861 *cval1 = TREE_OPERAND (arg, 0);
2862 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2864 else if (*cval2 == 0)
2865 *cval2 = TREE_OPERAND (arg, 0);
2866 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2868 else
2869 return 0;
2871 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2873 else if (*cval2 == 0)
2874 *cval2 = TREE_OPERAND (arg, 1);
2875 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2877 else
2878 return 0;
2880 return 1;
2882 default:
2883 return 0;
2887 /* ARG is a tree that is known to contain just arithmetic operations and
2888 comparisons. Evaluate the operations in the tree substituting NEW0 for
2889 any occurrence of OLD0 as an operand of a comparison and likewise for
2890 NEW1 and OLD1. */
2892 static tree
2893 eval_subst (location_t loc, tree arg, tree old0, tree new0,
2894 tree old1, tree new1)
2896 tree type = TREE_TYPE (arg);
2897 enum tree_code code = TREE_CODE (arg);
2898 enum tree_code_class tclass = TREE_CODE_CLASS (code);
2900 /* We can handle some of the tcc_expression cases here. */
2901 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
2902 tclass = tcc_unary;
2903 else if (tclass == tcc_expression
2904 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2905 tclass = tcc_binary;
2907 switch (tclass)
2909 case tcc_unary:
2910 return fold_build1_loc (loc, code, type,
2911 eval_subst (loc, TREE_OPERAND (arg, 0),
2912 old0, new0, old1, new1));
2914 case tcc_binary:
2915 return fold_build2_loc (loc, code, type,
2916 eval_subst (loc, TREE_OPERAND (arg, 0),
2917 old0, new0, old1, new1),
2918 eval_subst (loc, TREE_OPERAND (arg, 1),
2919 old0, new0, old1, new1));
2921 case tcc_expression:
2922 switch (code)
2924 case SAVE_EXPR:
2925 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
2926 old1, new1);
2928 case COMPOUND_EXPR:
2929 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
2930 old1, new1);
2932 case COND_EXPR:
2933 return fold_build3_loc (loc, code, type,
2934 eval_subst (loc, TREE_OPERAND (arg, 0),
2935 old0, new0, old1, new1),
2936 eval_subst (loc, TREE_OPERAND (arg, 1),
2937 old0, new0, old1, new1),
2938 eval_subst (loc, TREE_OPERAND (arg, 2),
2939 old0, new0, old1, new1));
2940 default:
2941 break;
2943 /* Fall through - ??? */
2945 case tcc_comparison:
2947 tree arg0 = TREE_OPERAND (arg, 0);
2948 tree arg1 = TREE_OPERAND (arg, 1);
2950 /* We need to check both for exact equality and tree equality. The
2951 former will be true if the operand has a side-effect. In that
2952 case, we know the operand occurred exactly once. */
2954 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2955 arg0 = new0;
2956 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2957 arg0 = new1;
2959 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2960 arg1 = new0;
2961 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2962 arg1 = new1;
2964 return fold_build2_loc (loc, code, type, arg0, arg1);
2967 default:
2968 return arg;
2972 /* Return a tree for the case when the result of an expression is RESULT
2973 converted to TYPE and OMITTED was previously an operand of the expression
2974 but is now not needed (e.g., we folded OMITTED * 0).
2976 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2977 the conversion of RESULT to TYPE. */
2979 tree
2980 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
2982 tree t = fold_convert_loc (loc, type, result);
2984 /* If the resulting operand is an empty statement, just return the omitted
2985 statement casted to void. */
2986 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
2987 return build1_loc (loc, NOP_EXPR, void_type_node,
2988 fold_ignored_result (omitted));
2990 if (TREE_SIDE_EFFECTS (omitted))
2991 return build2_loc (loc, COMPOUND_EXPR, type,
2992 fold_ignored_result (omitted), t);
2994 return non_lvalue_loc (loc, t);
2997 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2999 static tree
3000 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
3001 tree omitted)
3003 tree t = fold_convert_loc (loc, type, result);
3005 /* If the resulting operand is an empty statement, just return the omitted
3006 statement casted to void. */
3007 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3008 return build1_loc (loc, NOP_EXPR, void_type_node,
3009 fold_ignored_result (omitted));
3011 if (TREE_SIDE_EFFECTS (omitted))
3012 return build2_loc (loc, COMPOUND_EXPR, type,
3013 fold_ignored_result (omitted), t);
3015 return pedantic_non_lvalue_loc (loc, t);
3018 /* Return a tree for the case when the result of an expression is RESULT
3019 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3020 of the expression but are now not needed.
3022 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3023 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3024 evaluated before OMITTED2. Otherwise, if neither has side effects,
3025 just do the conversion of RESULT to TYPE. */
3027 tree
3028 omit_two_operands_loc (location_t loc, tree type, tree result,
3029 tree omitted1, tree omitted2)
3031 tree t = fold_convert_loc (loc, type, result);
3033 if (TREE_SIDE_EFFECTS (omitted2))
3034 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3035 if (TREE_SIDE_EFFECTS (omitted1))
3036 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3038 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3042 /* Return a simplified tree node for the truth-negation of ARG. This
3043 never alters ARG itself. We assume that ARG is an operation that
3044 returns a truth value (0 or 1).
3046 FIXME: one would think we would fold the result, but it causes
3047 problems with the dominator optimizer. */
3049 tree
3050 fold_truth_not_expr (location_t loc, tree arg)
3052 tree type = TREE_TYPE (arg);
3053 enum tree_code code = TREE_CODE (arg);
3054 location_t loc1, loc2;
3056 /* If this is a comparison, we can simply invert it, except for
3057 floating-point non-equality comparisons, in which case we just
3058 enclose a TRUTH_NOT_EXPR around what we have. */
3060 if (TREE_CODE_CLASS (code) == tcc_comparison)
3062 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3063 if (FLOAT_TYPE_P (op_type)
3064 && flag_trapping_math
3065 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3066 && code != NE_EXPR && code != EQ_EXPR)
3067 return NULL_TREE;
3069 code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3070 if (code == ERROR_MARK)
3071 return NULL_TREE;
3073 return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3074 TREE_OPERAND (arg, 1));
3077 switch (code)
3079 case INTEGER_CST:
3080 return constant_boolean_node (integer_zerop (arg), type);
3082 case TRUTH_AND_EXPR:
3083 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3084 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3085 if (loc1 == UNKNOWN_LOCATION)
3086 loc1 = loc;
3087 if (loc2 == UNKNOWN_LOCATION)
3088 loc2 = loc;
3089 return build2_loc (loc, TRUTH_OR_EXPR, type,
3090 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3091 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3093 case TRUTH_OR_EXPR:
3094 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3095 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3096 if (loc1 == UNKNOWN_LOCATION)
3097 loc1 = loc;
3098 if (loc2 == UNKNOWN_LOCATION)
3099 loc2 = loc;
3100 return build2_loc (loc, TRUTH_AND_EXPR, type,
3101 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3102 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3104 case TRUTH_XOR_EXPR:
3105 /* Here we can invert either operand. We invert the first operand
3106 unless the second operand is a TRUTH_NOT_EXPR in which case our
3107 result is the XOR of the first operand with the inside of the
3108 negation of the second operand. */
3110 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3111 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3112 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3113 else
3114 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3115 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3116 TREE_OPERAND (arg, 1));
3118 case TRUTH_ANDIF_EXPR:
3119 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3120 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3121 if (loc1 == UNKNOWN_LOCATION)
3122 loc1 = loc;
3123 if (loc2 == UNKNOWN_LOCATION)
3124 loc2 = loc;
3125 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3126 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3127 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3129 case TRUTH_ORIF_EXPR:
3130 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3131 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3132 if (loc1 == UNKNOWN_LOCATION)
3133 loc1 = loc;
3134 if (loc2 == UNKNOWN_LOCATION)
3135 loc2 = loc;
3136 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3137 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3138 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3140 case TRUTH_NOT_EXPR:
3141 return TREE_OPERAND (arg, 0);
3143 case COND_EXPR:
3145 tree arg1 = TREE_OPERAND (arg, 1);
3146 tree arg2 = TREE_OPERAND (arg, 2);
3148 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3149 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 2));
3150 if (loc1 == UNKNOWN_LOCATION)
3151 loc1 = loc;
3152 if (loc2 == UNKNOWN_LOCATION)
3153 loc2 = loc;
3155 /* A COND_EXPR may have a throw as one operand, which
3156 then has void type. Just leave void operands
3157 as they are. */
3158 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3159 VOID_TYPE_P (TREE_TYPE (arg1))
3160 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3161 VOID_TYPE_P (TREE_TYPE (arg2))
3162 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3165 case COMPOUND_EXPR:
3166 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3167 if (loc1 == UNKNOWN_LOCATION)
3168 loc1 = loc;
3169 return build2_loc (loc, COMPOUND_EXPR, type,
3170 TREE_OPERAND (arg, 0),
3171 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3173 case NON_LVALUE_EXPR:
3174 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3175 if (loc1 == UNKNOWN_LOCATION)
3176 loc1 = loc;
3177 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3179 CASE_CONVERT:
3180 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3181 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3183 /* ... fall through ... */
3185 case FLOAT_EXPR:
3186 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3187 if (loc1 == UNKNOWN_LOCATION)
3188 loc1 = loc;
3189 return build1_loc (loc, TREE_CODE (arg), type,
3190 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3192 case BIT_AND_EXPR:
3193 if (!integer_onep (TREE_OPERAND (arg, 1)))
3194 return NULL_TREE;
3195 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3197 case SAVE_EXPR:
3198 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3200 case CLEANUP_POINT_EXPR:
3201 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3202 if (loc1 == UNKNOWN_LOCATION)
3203 loc1 = loc;
3204 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3205 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3207 default:
3208 return NULL_TREE;
3212 /* Return a simplified tree node for the truth-negation of ARG. This
3213 never alters ARG itself. We assume that ARG is an operation that
3214 returns a truth value (0 or 1).
3216 FIXME: one would think we would fold the result, but it causes
3217 problems with the dominator optimizer. */
3219 tree
3220 invert_truthvalue_loc (location_t loc, tree arg)
3222 tree tem;
3224 if (TREE_CODE (arg) == ERROR_MARK)
3225 return arg;
3227 tem = fold_truth_not_expr (loc, arg);
3228 if (!tem)
3229 tem = build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3231 return tem;
3234 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3235 operands are another bit-wise operation with a common input. If so,
3236 distribute the bit operations to save an operation and possibly two if
3237 constants are involved. For example, convert
3238 (A | B) & (A | C) into A | (B & C)
3239 Further simplification will occur if B and C are constants.
3241 If this optimization cannot be done, 0 will be returned. */
3243 static tree
3244 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
3245 tree arg0, tree arg1)
3247 tree common;
3248 tree left, right;
3250 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3251 || TREE_CODE (arg0) == code
3252 || (TREE_CODE (arg0) != BIT_AND_EXPR
3253 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3254 return 0;
3256 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3258 common = TREE_OPERAND (arg0, 0);
3259 left = TREE_OPERAND (arg0, 1);
3260 right = TREE_OPERAND (arg1, 1);
3262 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3264 common = TREE_OPERAND (arg0, 0);
3265 left = TREE_OPERAND (arg0, 1);
3266 right = TREE_OPERAND (arg1, 0);
3268 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3270 common = TREE_OPERAND (arg0, 1);
3271 left = TREE_OPERAND (arg0, 0);
3272 right = TREE_OPERAND (arg1, 1);
3274 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3276 common = TREE_OPERAND (arg0, 1);
3277 left = TREE_OPERAND (arg0, 0);
3278 right = TREE_OPERAND (arg1, 0);
3280 else
3281 return 0;
3283 common = fold_convert_loc (loc, type, common);
3284 left = fold_convert_loc (loc, type, left);
3285 right = fold_convert_loc (loc, type, right);
3286 return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
3287 fold_build2_loc (loc, code, type, left, right));
3290 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3291 with code CODE. This optimization is unsafe. */
3292 static tree
3293 distribute_real_division (location_t loc, enum tree_code code, tree type,
3294 tree arg0, tree arg1)
3296 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3297 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3299 /* (A / C) +- (B / C) -> (A +- B) / C. */
3300 if (mul0 == mul1
3301 && operand_equal_p (TREE_OPERAND (arg0, 1),
3302 TREE_OPERAND (arg1, 1), 0))
3303 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3304 fold_build2_loc (loc, code, type,
3305 TREE_OPERAND (arg0, 0),
3306 TREE_OPERAND (arg1, 0)),
3307 TREE_OPERAND (arg0, 1));
3309 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3310 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3311 TREE_OPERAND (arg1, 0), 0)
3312 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3313 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3315 REAL_VALUE_TYPE r0, r1;
3316 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3317 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3318 if (!mul0)
3319 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3320 if (!mul1)
3321 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3322 real_arithmetic (&r0, code, &r0, &r1);
3323 return fold_build2_loc (loc, MULT_EXPR, type,
3324 TREE_OPERAND (arg0, 0),
3325 build_real (type, r0));
3328 return NULL_TREE;
3331 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3332 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3334 static tree
3335 make_bit_field_ref (location_t loc, tree inner, tree type,
3336 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3338 tree result, bftype;
3340 if (bitpos == 0)
3342 tree size = TYPE_SIZE (TREE_TYPE (inner));
3343 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3344 || POINTER_TYPE_P (TREE_TYPE (inner)))
3345 && host_integerp (size, 0)
3346 && tree_low_cst (size, 0) == bitsize)
3347 return fold_convert_loc (loc, type, inner);
3350 bftype = type;
3351 if (TYPE_PRECISION (bftype) != bitsize
3352 || TYPE_UNSIGNED (bftype) == !unsignedp)
3353 bftype = build_nonstandard_integer_type (bitsize, 0);
3355 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3356 size_int (bitsize), bitsize_int (bitpos));
3358 if (bftype != type)
3359 result = fold_convert_loc (loc, type, result);
3361 return result;
3364 /* Optimize a bit-field compare.
3366 There are two cases: First is a compare against a constant and the
3367 second is a comparison of two items where the fields are at the same
3368 bit position relative to the start of a chunk (byte, halfword, word)
3369 large enough to contain it. In these cases we can avoid the shift
3370 implicit in bitfield extractions.
3372 For constants, we emit a compare of the shifted constant with the
3373 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3374 compared. For two fields at the same position, we do the ANDs with the
3375 similar mask and compare the result of the ANDs.
3377 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3378 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3379 are the left and right operands of the comparison, respectively.
3381 If the optimization described above can be done, we return the resulting
3382 tree. Otherwise we return zero. */
3384 static tree
3385 optimize_bit_field_compare (location_t loc, enum tree_code code,
3386 tree compare_type, tree lhs, tree rhs)
3388 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3389 tree type = TREE_TYPE (lhs);
3390 tree signed_type, unsigned_type;
3391 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3392 enum machine_mode lmode, rmode, nmode;
3393 int lunsignedp, runsignedp;
3394 int lvolatilep = 0, rvolatilep = 0;
3395 tree linner, rinner = NULL_TREE;
3396 tree mask;
3397 tree offset;
3399 /* Get all the information about the extractions being done. If the bit size
3400 if the same as the size of the underlying object, we aren't doing an
3401 extraction at all and so can do nothing. We also don't want to
3402 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3403 then will no longer be able to replace it. */
3404 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3405 &lunsignedp, &lvolatilep, false);
3406 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3407 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3408 return 0;
3410 if (!const_p)
3412 /* If this is not a constant, we can only do something if bit positions,
3413 sizes, and signedness are the same. */
3414 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3415 &runsignedp, &rvolatilep, false);
3417 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3418 || lunsignedp != runsignedp || offset != 0
3419 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3420 return 0;
3423 /* See if we can find a mode to refer to this field. We should be able to,
3424 but fail if we can't. */
3425 if (lvolatilep
3426 && GET_MODE_BITSIZE (lmode) > 0
3427 && flag_strict_volatile_bitfields > 0)
3428 nmode = lmode;
3429 else
3430 nmode = get_best_mode (lbitsize, lbitpos,
3431 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3432 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3433 TYPE_ALIGN (TREE_TYPE (rinner))),
3434 word_mode, lvolatilep || rvolatilep);
3435 if (nmode == VOIDmode)
3436 return 0;
3438 /* Set signed and unsigned types of the precision of this mode for the
3439 shifts below. */
3440 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3441 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3443 /* Compute the bit position and size for the new reference and our offset
3444 within it. If the new reference is the same size as the original, we
3445 won't optimize anything, so return zero. */
3446 nbitsize = GET_MODE_BITSIZE (nmode);
3447 nbitpos = lbitpos & ~ (nbitsize - 1);
3448 lbitpos -= nbitpos;
3449 if (nbitsize == lbitsize)
3450 return 0;
3452 if (BYTES_BIG_ENDIAN)
3453 lbitpos = nbitsize - lbitsize - lbitpos;
3455 /* Make the mask to be used against the extracted field. */
3456 mask = build_int_cst_type (unsigned_type, -1);
3457 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3458 mask = const_binop (RSHIFT_EXPR, mask,
3459 size_int (nbitsize - lbitsize - lbitpos));
3461 if (! const_p)
3462 /* If not comparing with constant, just rework the comparison
3463 and return. */
3464 return fold_build2_loc (loc, code, compare_type,
3465 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3466 make_bit_field_ref (loc, linner,
3467 unsigned_type,
3468 nbitsize, nbitpos,
3470 mask),
3471 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3472 make_bit_field_ref (loc, rinner,
3473 unsigned_type,
3474 nbitsize, nbitpos,
3476 mask));
3478 /* Otherwise, we are handling the constant case. See if the constant is too
3479 big for the field. Warn and return a tree of for 0 (false) if so. We do
3480 this not only for its own sake, but to avoid having to test for this
3481 error case below. If we didn't, we might generate wrong code.
3483 For unsigned fields, the constant shifted right by the field length should
3484 be all zero. For signed fields, the high-order bits should agree with
3485 the sign bit. */
3487 if (lunsignedp)
3489 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3490 fold_convert_loc (loc,
3491 unsigned_type, rhs),
3492 size_int (lbitsize))))
3494 warning (0, "comparison is always %d due to width of bit-field",
3495 code == NE_EXPR);
3496 return constant_boolean_node (code == NE_EXPR, compare_type);
3499 else
3501 tree tem = const_binop (RSHIFT_EXPR,
3502 fold_convert_loc (loc, signed_type, rhs),
3503 size_int (lbitsize - 1));
3504 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3506 warning (0, "comparison is always %d due to width of bit-field",
3507 code == NE_EXPR);
3508 return constant_boolean_node (code == NE_EXPR, compare_type);
3512 /* Single-bit compares should always be against zero. */
3513 if (lbitsize == 1 && ! integer_zerop (rhs))
3515 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3516 rhs = build_int_cst (type, 0);
3519 /* Make a new bitfield reference, shift the constant over the
3520 appropriate number of bits and mask it with the computed mask
3521 (in case this was a signed field). If we changed it, make a new one. */
3522 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3523 if (lvolatilep)
3525 TREE_SIDE_EFFECTS (lhs) = 1;
3526 TREE_THIS_VOLATILE (lhs) = 1;
3529 rhs = const_binop (BIT_AND_EXPR,
3530 const_binop (LSHIFT_EXPR,
3531 fold_convert_loc (loc, unsigned_type, rhs),
3532 size_int (lbitpos)),
3533 mask);
3535 lhs = build2_loc (loc, code, compare_type,
3536 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3537 return lhs;
3540 /* Subroutine for fold_truthop: decode a field reference.
3542 If EXP is a comparison reference, we return the innermost reference.
3544 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3545 set to the starting bit number.
3547 If the innermost field can be completely contained in a mode-sized
3548 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3550 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3551 otherwise it is not changed.
3553 *PUNSIGNEDP is set to the signedness of the field.
3555 *PMASK is set to the mask used. This is either contained in a
3556 BIT_AND_EXPR or derived from the width of the field.
3558 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3560 Return 0 if this is not a component reference or is one that we can't
3561 do anything with. */
3563 static tree
3564 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3565 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3566 int *punsignedp, int *pvolatilep,
3567 tree *pmask, tree *pand_mask)
3569 tree outer_type = 0;
3570 tree and_mask = 0;
3571 tree mask, inner, offset;
3572 tree unsigned_type;
3573 unsigned int precision;
3575 /* All the optimizations using this function assume integer fields.
3576 There are problems with FP fields since the type_for_size call
3577 below can fail for, e.g., XFmode. */
3578 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3579 return 0;
3581 /* We are interested in the bare arrangement of bits, so strip everything
3582 that doesn't affect the machine mode. However, record the type of the
3583 outermost expression if it may matter below. */
3584 if (CONVERT_EXPR_P (exp)
3585 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3586 outer_type = TREE_TYPE (exp);
3587 STRIP_NOPS (exp);
3589 if (TREE_CODE (exp) == BIT_AND_EXPR)
3591 and_mask = TREE_OPERAND (exp, 1);
3592 exp = TREE_OPERAND (exp, 0);
3593 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3594 if (TREE_CODE (and_mask) != INTEGER_CST)
3595 return 0;
3598 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3599 punsignedp, pvolatilep, false);
3600 if ((inner == exp && and_mask == 0)
3601 || *pbitsize < 0 || offset != 0
3602 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3603 return 0;
3605 /* If the number of bits in the reference is the same as the bitsize of
3606 the outer type, then the outer type gives the signedness. Otherwise
3607 (in case of a small bitfield) the signedness is unchanged. */
3608 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3609 *punsignedp = TYPE_UNSIGNED (outer_type);
3611 /* Compute the mask to access the bitfield. */
3612 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3613 precision = TYPE_PRECISION (unsigned_type);
3615 mask = build_int_cst_type (unsigned_type, -1);
3617 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3618 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3620 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3621 if (and_mask != 0)
3622 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3623 fold_convert_loc (loc, unsigned_type, and_mask), mask);
3625 *pmask = mask;
3626 *pand_mask = and_mask;
3627 return inner;
3630 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3631 bit positions. */
3633 static int
3634 all_ones_mask_p (const_tree mask, int size)
3636 tree type = TREE_TYPE (mask);
3637 unsigned int precision = TYPE_PRECISION (type);
3638 tree tmask;
3640 tmask = build_int_cst_type (signed_type_for (type), -1);
3642 return
3643 tree_int_cst_equal (mask,
3644 const_binop (RSHIFT_EXPR,
3645 const_binop (LSHIFT_EXPR, tmask,
3646 size_int (precision - size)),
3647 size_int (precision - size)));
3650 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3651 represents the sign bit of EXP's type. If EXP represents a sign
3652 or zero extension, also test VAL against the unextended type.
3653 The return value is the (sub)expression whose sign bit is VAL,
3654 or NULL_TREE otherwise. */
3656 static tree
3657 sign_bit_p (tree exp, const_tree val)
3659 unsigned HOST_WIDE_INT mask_lo, lo;
3660 HOST_WIDE_INT mask_hi, hi;
3661 int width;
3662 tree t;
3664 /* Tree EXP must have an integral type. */
3665 t = TREE_TYPE (exp);
3666 if (! INTEGRAL_TYPE_P (t))
3667 return NULL_TREE;
3669 /* Tree VAL must be an integer constant. */
3670 if (TREE_CODE (val) != INTEGER_CST
3671 || TREE_OVERFLOW (val))
3672 return NULL_TREE;
3674 width = TYPE_PRECISION (t);
3675 if (width > HOST_BITS_PER_WIDE_INT)
3677 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3678 lo = 0;
3680 mask_hi = ((unsigned HOST_WIDE_INT) -1
3681 >> (2 * HOST_BITS_PER_WIDE_INT - width));
3682 mask_lo = -1;
3684 else
3686 hi = 0;
3687 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3689 mask_hi = 0;
3690 mask_lo = ((unsigned HOST_WIDE_INT) -1
3691 >> (HOST_BITS_PER_WIDE_INT - width));
3694 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3695 treat VAL as if it were unsigned. */
3696 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3697 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3698 return exp;
3700 /* Handle extension from a narrower type. */
3701 if (TREE_CODE (exp) == NOP_EXPR
3702 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3703 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3705 return NULL_TREE;
3708 /* Subroutine for fold_truthop: determine if an operand is simple enough
3709 to be evaluated unconditionally. */
3711 static int
3712 simple_operand_p (const_tree exp)
3714 /* Strip any conversions that don't change the machine mode. */
3715 STRIP_NOPS (exp);
3717 return (CONSTANT_CLASS_P (exp)
3718 || TREE_CODE (exp) == SSA_NAME
3719 || (DECL_P (exp)
3720 && ! TREE_ADDRESSABLE (exp)
3721 && ! TREE_THIS_VOLATILE (exp)
3722 && ! DECL_NONLOCAL (exp)
3723 /* Don't regard global variables as simple. They may be
3724 allocated in ways unknown to the compiler (shared memory,
3725 #pragma weak, etc). */
3726 && ! TREE_PUBLIC (exp)
3727 && ! DECL_EXTERNAL (exp)
3728 /* Loading a static variable is unduly expensive, but global
3729 registers aren't expensive. */
3730 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3733 /* The following functions are subroutines to fold_range_test and allow it to
3734 try to change a logical combination of comparisons into a range test.
3736 For example, both
3737 X == 2 || X == 3 || X == 4 || X == 5
3739 X >= 2 && X <= 5
3740 are converted to
3741 (unsigned) (X - 2) <= 3
3743 We describe each set of comparisons as being either inside or outside
3744 a range, using a variable named like IN_P, and then describe the
3745 range with a lower and upper bound. If one of the bounds is omitted,
3746 it represents either the highest or lowest value of the type.
3748 In the comments below, we represent a range by two numbers in brackets
3749 preceded by a "+" to designate being inside that range, or a "-" to
3750 designate being outside that range, so the condition can be inverted by
3751 flipping the prefix. An omitted bound is represented by a "-". For
3752 example, "- [-, 10]" means being outside the range starting at the lowest
3753 possible value and ending at 10, in other words, being greater than 10.
3754 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3755 always false.
3757 We set up things so that the missing bounds are handled in a consistent
3758 manner so neither a missing bound nor "true" and "false" need to be
3759 handled using a special case. */
3761 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3762 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3763 and UPPER1_P are nonzero if the respective argument is an upper bound
3764 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3765 must be specified for a comparison. ARG1 will be converted to ARG0's
3766 type if both are specified. */
3768 static tree
3769 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3770 tree arg1, int upper1_p)
3772 tree tem;
3773 int result;
3774 int sgn0, sgn1;
3776 /* If neither arg represents infinity, do the normal operation.
3777 Else, if not a comparison, return infinity. Else handle the special
3778 comparison rules. Note that most of the cases below won't occur, but
3779 are handled for consistency. */
3781 if (arg0 != 0 && arg1 != 0)
3783 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3784 arg0, fold_convert (TREE_TYPE (arg0), arg1));
3785 STRIP_NOPS (tem);
3786 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3789 if (TREE_CODE_CLASS (code) != tcc_comparison)
3790 return 0;
3792 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3793 for neither. In real maths, we cannot assume open ended ranges are
3794 the same. But, this is computer arithmetic, where numbers are finite.
3795 We can therefore make the transformation of any unbounded range with
3796 the value Z, Z being greater than any representable number. This permits
3797 us to treat unbounded ranges as equal. */
3798 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3799 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3800 switch (code)
3802 case EQ_EXPR:
3803 result = sgn0 == sgn1;
3804 break;
3805 case NE_EXPR:
3806 result = sgn0 != sgn1;
3807 break;
3808 case LT_EXPR:
3809 result = sgn0 < sgn1;
3810 break;
3811 case LE_EXPR:
3812 result = sgn0 <= sgn1;
3813 break;
3814 case GT_EXPR:
3815 result = sgn0 > sgn1;
3816 break;
3817 case GE_EXPR:
3818 result = sgn0 >= sgn1;
3819 break;
3820 default:
3821 gcc_unreachable ();
3824 return constant_boolean_node (result, type);
3827 /* Given EXP, a logical expression, set the range it is testing into
3828 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3829 actually being tested. *PLOW and *PHIGH will be made of the same
3830 type as the returned expression. If EXP is not a comparison, we
3831 will most likely not be returning a useful value and range. Set
3832 *STRICT_OVERFLOW_P to true if the return value is only valid
3833 because signed overflow is undefined; otherwise, do not change
3834 *STRICT_OVERFLOW_P. */
3836 tree
3837 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
3838 bool *strict_overflow_p)
3840 enum tree_code code;
3841 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3842 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3843 int in_p, n_in_p;
3844 tree low, high, n_low, n_high;
3845 location_t loc = EXPR_LOCATION (exp);
3847 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3848 and see if we can refine the range. Some of the cases below may not
3849 happen, but it doesn't seem worth worrying about this. We "continue"
3850 the outer loop when we've changed something; otherwise we "break"
3851 the switch, which will "break" the while. */
3853 in_p = 0;
3854 low = high = build_int_cst (TREE_TYPE (exp), 0);
3856 while (1)
3858 code = TREE_CODE (exp);
3859 exp_type = TREE_TYPE (exp);
3861 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3863 if (TREE_OPERAND_LENGTH (exp) > 0)
3864 arg0 = TREE_OPERAND (exp, 0);
3865 if (TREE_CODE_CLASS (code) == tcc_comparison
3866 || TREE_CODE_CLASS (code) == tcc_unary
3867 || TREE_CODE_CLASS (code) == tcc_binary)
3868 arg0_type = TREE_TYPE (arg0);
3869 if (TREE_CODE_CLASS (code) == tcc_binary
3870 || TREE_CODE_CLASS (code) == tcc_comparison
3871 || (TREE_CODE_CLASS (code) == tcc_expression
3872 && TREE_OPERAND_LENGTH (exp) > 1))
3873 arg1 = TREE_OPERAND (exp, 1);
3876 switch (code)
3878 case TRUTH_NOT_EXPR:
3879 in_p = ! in_p, exp = arg0;
3880 continue;
3882 case EQ_EXPR: case NE_EXPR:
3883 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3884 /* We can only do something if the range is testing for zero
3885 and if the second operand is an integer constant. Note that
3886 saying something is "in" the range we make is done by
3887 complementing IN_P since it will set in the initial case of
3888 being not equal to zero; "out" is leaving it alone. */
3889 if (low == 0 || high == 0
3890 || ! integer_zerop (low) || ! integer_zerop (high)
3891 || TREE_CODE (arg1) != INTEGER_CST)
3892 break;
3894 switch (code)
3896 case NE_EXPR: /* - [c, c] */
3897 low = high = arg1;
3898 break;
3899 case EQ_EXPR: /* + [c, c] */
3900 in_p = ! in_p, low = high = arg1;
3901 break;
3902 case GT_EXPR: /* - [-, c] */
3903 low = 0, high = arg1;
3904 break;
3905 case GE_EXPR: /* + [c, -] */
3906 in_p = ! in_p, low = arg1, high = 0;
3907 break;
3908 case LT_EXPR: /* - [c, -] */
3909 low = arg1, high = 0;
3910 break;
3911 case LE_EXPR: /* + [-, c] */
3912 in_p = ! in_p, low = 0, high = arg1;
3913 break;
3914 default:
3915 gcc_unreachable ();
3918 /* If this is an unsigned comparison, we also know that EXP is
3919 greater than or equal to zero. We base the range tests we make
3920 on that fact, so we record it here so we can parse existing
3921 range tests. We test arg0_type since often the return type
3922 of, e.g. EQ_EXPR, is boolean. */
3923 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3925 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3926 in_p, low, high, 1,
3927 build_int_cst (arg0_type, 0),
3928 NULL_TREE))
3929 break;
3931 in_p = n_in_p, low = n_low, high = n_high;
3933 /* If the high bound is missing, but we have a nonzero low
3934 bound, reverse the range so it goes from zero to the low bound
3935 minus 1. */
3936 if (high == 0 && low && ! integer_zerop (low))
3938 in_p = ! in_p;
3939 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3940 integer_one_node, 0);
3941 low = build_int_cst (arg0_type, 0);
3945 exp = arg0;
3946 continue;
3948 case NEGATE_EXPR:
3949 /* (-x) IN [a,b] -> x in [-b, -a] */
3950 n_low = range_binop (MINUS_EXPR, exp_type,
3951 build_int_cst (exp_type, 0),
3952 0, high, 1);
3953 n_high = range_binop (MINUS_EXPR, exp_type,
3954 build_int_cst (exp_type, 0),
3955 0, low, 0);
3956 if (n_high != 0 && TREE_OVERFLOW (n_high))
3957 break;
3958 goto normalize;
3960 case BIT_NOT_EXPR:
3961 /* ~ X -> -X - 1 */
3962 exp = build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
3963 build_int_cst (exp_type, 1));
3964 continue;
3966 case PLUS_EXPR: case MINUS_EXPR:
3967 if (TREE_CODE (arg1) != INTEGER_CST)
3968 break;
3970 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
3971 move a constant to the other side. */
3972 if (!TYPE_UNSIGNED (arg0_type)
3973 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
3974 break;
3976 /* If EXP is signed, any overflow in the computation is undefined,
3977 so we don't worry about it so long as our computations on
3978 the bounds don't overflow. For unsigned, overflow is defined
3979 and this is exactly the right thing. */
3980 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3981 arg0_type, low, 0, arg1, 0);
3982 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3983 arg0_type, high, 1, arg1, 0);
3984 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3985 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3986 break;
3988 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
3989 *strict_overflow_p = true;
3991 normalize:
3992 /* Check for an unsigned range which has wrapped around the maximum
3993 value thus making n_high < n_low, and normalize it. */
3994 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3996 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
3997 integer_one_node, 0);
3998 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
3999 integer_one_node, 0);
4001 /* If the range is of the form +/- [ x+1, x ], we won't
4002 be able to normalize it. But then, it represents the
4003 whole range or the empty set, so make it
4004 +/- [ -, - ]. */
4005 if (tree_int_cst_equal (n_low, low)
4006 && tree_int_cst_equal (n_high, high))
4007 low = high = 0;
4008 else
4009 in_p = ! in_p;
4011 else
4012 low = n_low, high = n_high;
4014 exp = arg0;
4015 continue;
4017 CASE_CONVERT: case NON_LVALUE_EXPR:
4018 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4019 break;
4021 if (! INTEGRAL_TYPE_P (arg0_type)
4022 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4023 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4024 break;
4026 n_low = low, n_high = high;
4028 if (n_low != 0)
4029 n_low = fold_convert_loc (loc, arg0_type, n_low);
4031 if (n_high != 0)
4032 n_high = fold_convert_loc (loc, arg0_type, n_high);
4035 /* If we're converting arg0 from an unsigned type, to exp,
4036 a signed type, we will be doing the comparison as unsigned.
4037 The tests above have already verified that LOW and HIGH
4038 are both positive.
4040 So we have to ensure that we will handle large unsigned
4041 values the same way that the current signed bounds treat
4042 negative values. */
4044 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4046 tree high_positive;
4047 tree equiv_type;
4048 /* For fixed-point modes, we need to pass the saturating flag
4049 as the 2nd parameter. */
4050 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4051 equiv_type = lang_hooks.types.type_for_mode
4052 (TYPE_MODE (arg0_type),
4053 TYPE_SATURATING (arg0_type));
4054 else
4055 equiv_type = lang_hooks.types.type_for_mode
4056 (TYPE_MODE (arg0_type), 1);
4058 /* A range without an upper bound is, naturally, unbounded.
4059 Since convert would have cropped a very large value, use
4060 the max value for the destination type. */
4061 high_positive
4062 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4063 : TYPE_MAX_VALUE (arg0_type);
4065 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4066 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4067 fold_convert_loc (loc, arg0_type,
4068 high_positive),
4069 build_int_cst (arg0_type, 1));
4071 /* If the low bound is specified, "and" the range with the
4072 range for which the original unsigned value will be
4073 positive. */
4074 if (low != 0)
4076 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4077 1, n_low, n_high, 1,
4078 fold_convert_loc (loc, arg0_type,
4079 integer_zero_node),
4080 high_positive))
4081 break;
4083 in_p = (n_in_p == in_p);
4085 else
4087 /* Otherwise, "or" the range with the range of the input
4088 that will be interpreted as negative. */
4089 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4090 0, n_low, n_high, 1,
4091 fold_convert_loc (loc, arg0_type,
4092 integer_zero_node),
4093 high_positive))
4094 break;
4096 in_p = (in_p != n_in_p);
4100 exp = arg0;
4101 low = n_low, high = n_high;
4102 continue;
4104 default:
4105 break;
4108 break;
4111 /* If EXP is a constant, we can evaluate whether this is true or false. */
4112 if (TREE_CODE (exp) == INTEGER_CST)
4114 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4115 exp, 0, low, 0))
4116 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4117 exp, 1, high, 1)));
4118 low = high = 0;
4119 exp = 0;
4122 *pin_p = in_p, *plow = low, *phigh = high;
4123 return exp;
4126 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4127 type, TYPE, return an expression to test if EXP is in (or out of, depending
4128 on IN_P) the range. Return 0 if the test couldn't be created. */
4130 tree
4131 build_range_check (location_t loc, tree type, tree exp, int in_p,
4132 tree low, tree high)
4134 tree etype = TREE_TYPE (exp), value;
4136 #ifdef HAVE_canonicalize_funcptr_for_compare
4137 /* Disable this optimization for function pointer expressions
4138 on targets that require function pointer canonicalization. */
4139 if (HAVE_canonicalize_funcptr_for_compare
4140 && TREE_CODE (etype) == POINTER_TYPE
4141 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4142 return NULL_TREE;
4143 #endif
4145 if (! in_p)
4147 value = build_range_check (loc, type, exp, 1, low, high);
4148 if (value != 0)
4149 return invert_truthvalue_loc (loc, value);
4151 return 0;
4154 if (low == 0 && high == 0)
4155 return build_int_cst (type, 1);
4157 if (low == 0)
4158 return fold_build2_loc (loc, LE_EXPR, type, exp,
4159 fold_convert_loc (loc, etype, high));
4161 if (high == 0)
4162 return fold_build2_loc (loc, GE_EXPR, type, exp,
4163 fold_convert_loc (loc, etype, low));
4165 if (operand_equal_p (low, high, 0))
4166 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4167 fold_convert_loc (loc, etype, low));
4169 if (integer_zerop (low))
4171 if (! TYPE_UNSIGNED (etype))
4173 etype = unsigned_type_for (etype);
4174 high = fold_convert_loc (loc, etype, high);
4175 exp = fold_convert_loc (loc, etype, exp);
4177 return build_range_check (loc, type, exp, 1, 0, high);
4180 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4181 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4183 unsigned HOST_WIDE_INT lo;
4184 HOST_WIDE_INT hi;
4185 int prec;
4187 prec = TYPE_PRECISION (etype);
4188 if (prec <= HOST_BITS_PER_WIDE_INT)
4190 hi = 0;
4191 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4193 else
4195 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4196 lo = (unsigned HOST_WIDE_INT) -1;
4199 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4201 if (TYPE_UNSIGNED (etype))
4203 tree signed_etype = signed_type_for (etype);
4204 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4205 etype
4206 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4207 else
4208 etype = signed_etype;
4209 exp = fold_convert_loc (loc, etype, exp);
4211 return fold_build2_loc (loc, GT_EXPR, type, exp,
4212 build_int_cst (etype, 0));
4216 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4217 This requires wrap-around arithmetics for the type of the expression.
4218 First make sure that arithmetics in this type is valid, then make sure
4219 that it wraps around. */
4220 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4221 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4222 TYPE_UNSIGNED (etype));
4224 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4226 tree utype, minv, maxv;
4228 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4229 for the type in question, as we rely on this here. */
4230 utype = unsigned_type_for (etype);
4231 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4232 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4233 integer_one_node, 1);
4234 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4236 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4237 minv, 1, maxv, 1)))
4238 etype = utype;
4239 else
4240 return 0;
4243 high = fold_convert_loc (loc, etype, high);
4244 low = fold_convert_loc (loc, etype, low);
4245 exp = fold_convert_loc (loc, etype, exp);
4247 value = const_binop (MINUS_EXPR, high, low);
4250 if (POINTER_TYPE_P (etype))
4252 if (value != 0 && !TREE_OVERFLOW (value))
4254 low = fold_convert_loc (loc, sizetype, low);
4255 low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
4256 return build_range_check (loc, type,
4257 fold_build2_loc (loc, POINTER_PLUS_EXPR,
4258 etype, exp, low),
4259 1, build_int_cst (etype, 0), value);
4261 return 0;
4264 if (value != 0 && !TREE_OVERFLOW (value))
4265 return build_range_check (loc, type,
4266 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4267 1, build_int_cst (etype, 0), value);
4269 return 0;
4272 /* Return the predecessor of VAL in its type, handling the infinite case. */
4274 static tree
4275 range_predecessor (tree val)
4277 tree type = TREE_TYPE (val);
4279 if (INTEGRAL_TYPE_P (type)
4280 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4281 return 0;
4282 else
4283 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4286 /* Return the successor of VAL in its type, handling the infinite case. */
4288 static tree
4289 range_successor (tree val)
4291 tree type = TREE_TYPE (val);
4293 if (INTEGRAL_TYPE_P (type)
4294 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4295 return 0;
4296 else
4297 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4300 /* Given two ranges, see if we can merge them into one. Return 1 if we
4301 can, 0 if we can't. Set the output range into the specified parameters. */
4303 bool
4304 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4305 tree high0, int in1_p, tree low1, tree high1)
4307 int no_overlap;
4308 int subset;
4309 int temp;
4310 tree tem;
4311 int in_p;
4312 tree low, high;
4313 int lowequal = ((low0 == 0 && low1 == 0)
4314 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4315 low0, 0, low1, 0)));
4316 int highequal = ((high0 == 0 && high1 == 0)
4317 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4318 high0, 1, high1, 1)));
4320 /* Make range 0 be the range that starts first, or ends last if they
4321 start at the same value. Swap them if it isn't. */
4322 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4323 low0, 0, low1, 0))
4324 || (lowequal
4325 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4326 high1, 1, high0, 1))))
4328 temp = in0_p, in0_p = in1_p, in1_p = temp;
4329 tem = low0, low0 = low1, low1 = tem;
4330 tem = high0, high0 = high1, high1 = tem;
4333 /* Now flag two cases, whether the ranges are disjoint or whether the
4334 second range is totally subsumed in the first. Note that the tests
4335 below are simplified by the ones above. */
4336 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4337 high0, 1, low1, 0));
4338 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4339 high1, 1, high0, 1));
4341 /* We now have four cases, depending on whether we are including or
4342 excluding the two ranges. */
4343 if (in0_p && in1_p)
4345 /* If they don't overlap, the result is false. If the second range
4346 is a subset it is the result. Otherwise, the range is from the start
4347 of the second to the end of the first. */
4348 if (no_overlap)
4349 in_p = 0, low = high = 0;
4350 else if (subset)
4351 in_p = 1, low = low1, high = high1;
4352 else
4353 in_p = 1, low = low1, high = high0;
4356 else if (in0_p && ! in1_p)
4358 /* If they don't overlap, the result is the first range. If they are
4359 equal, the result is false. If the second range is a subset of the
4360 first, and the ranges begin at the same place, we go from just after
4361 the end of the second range to the end of the first. If the second
4362 range is not a subset of the first, or if it is a subset and both
4363 ranges end at the same place, the range starts at the start of the
4364 first range and ends just before the second range.
4365 Otherwise, we can't describe this as a single range. */
4366 if (no_overlap)
4367 in_p = 1, low = low0, high = high0;
4368 else if (lowequal && highequal)
4369 in_p = 0, low = high = 0;
4370 else if (subset && lowequal)
4372 low = range_successor (high1);
4373 high = high0;
4374 in_p = 1;
4375 if (low == 0)
4377 /* We are in the weird situation where high0 > high1 but
4378 high1 has no successor. Punt. */
4379 return 0;
4382 else if (! subset || highequal)
4384 low = low0;
4385 high = range_predecessor (low1);
4386 in_p = 1;
4387 if (high == 0)
4389 /* low0 < low1 but low1 has no predecessor. Punt. */
4390 return 0;
4393 else
4394 return 0;
4397 else if (! in0_p && in1_p)
4399 /* If they don't overlap, the result is the second range. If the second
4400 is a subset of the first, the result is false. Otherwise,
4401 the range starts just after the first range and ends at the
4402 end of the second. */
4403 if (no_overlap)
4404 in_p = 1, low = low1, high = high1;
4405 else if (subset || highequal)
4406 in_p = 0, low = high = 0;
4407 else
4409 low = range_successor (high0);
4410 high = high1;
4411 in_p = 1;
4412 if (low == 0)
4414 /* high1 > high0 but high0 has no successor. Punt. */
4415 return 0;
4420 else
4422 /* The case where we are excluding both ranges. Here the complex case
4423 is if they don't overlap. In that case, the only time we have a
4424 range is if they are adjacent. If the second is a subset of the
4425 first, the result is the first. Otherwise, the range to exclude
4426 starts at the beginning of the first range and ends at the end of the
4427 second. */
4428 if (no_overlap)
4430 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4431 range_successor (high0),
4432 1, low1, 0)))
4433 in_p = 0, low = low0, high = high1;
4434 else
4436 /* Canonicalize - [min, x] into - [-, x]. */
4437 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4438 switch (TREE_CODE (TREE_TYPE (low0)))
4440 case ENUMERAL_TYPE:
4441 if (TYPE_PRECISION (TREE_TYPE (low0))
4442 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4443 break;
4444 /* FALLTHROUGH */
4445 case INTEGER_TYPE:
4446 if (tree_int_cst_equal (low0,
4447 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4448 low0 = 0;
4449 break;
4450 case POINTER_TYPE:
4451 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4452 && integer_zerop (low0))
4453 low0 = 0;
4454 break;
4455 default:
4456 break;
4459 /* Canonicalize - [x, max] into - [x, -]. */
4460 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4461 switch (TREE_CODE (TREE_TYPE (high1)))
4463 case ENUMERAL_TYPE:
4464 if (TYPE_PRECISION (TREE_TYPE (high1))
4465 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4466 break;
4467 /* FALLTHROUGH */
4468 case INTEGER_TYPE:
4469 if (tree_int_cst_equal (high1,
4470 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4471 high1 = 0;
4472 break;
4473 case POINTER_TYPE:
4474 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4475 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4476 high1, 1,
4477 integer_one_node, 1)))
4478 high1 = 0;
4479 break;
4480 default:
4481 break;
4484 /* The ranges might be also adjacent between the maximum and
4485 minimum values of the given type. For
4486 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4487 return + [x + 1, y - 1]. */
4488 if (low0 == 0 && high1 == 0)
4490 low = range_successor (high0);
4491 high = range_predecessor (low1);
4492 if (low == 0 || high == 0)
4493 return 0;
4495 in_p = 1;
4497 else
4498 return 0;
4501 else if (subset)
4502 in_p = 0, low = low0, high = high0;
4503 else
4504 in_p = 0, low = low0, high = high1;
4507 *pin_p = in_p, *plow = low, *phigh = high;
4508 return 1;
4512 /* Subroutine of fold, looking inside expressions of the form
4513 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4514 of the COND_EXPR. This function is being used also to optimize
4515 A op B ? C : A, by reversing the comparison first.
4517 Return a folded expression whose code is not a COND_EXPR
4518 anymore, or NULL_TREE if no folding opportunity is found. */
4520 static tree
4521 fold_cond_expr_with_comparison (location_t loc, tree type,
4522 tree arg0, tree arg1, tree arg2)
4524 enum tree_code comp_code = TREE_CODE (arg0);
4525 tree arg00 = TREE_OPERAND (arg0, 0);
4526 tree arg01 = TREE_OPERAND (arg0, 1);
4527 tree arg1_type = TREE_TYPE (arg1);
4528 tree tem;
4530 STRIP_NOPS (arg1);
4531 STRIP_NOPS (arg2);
4533 /* If we have A op 0 ? A : -A, consider applying the following
4534 transformations:
4536 A == 0? A : -A same as -A
4537 A != 0? A : -A same as A
4538 A >= 0? A : -A same as abs (A)
4539 A > 0? A : -A same as abs (A)
4540 A <= 0? A : -A same as -abs (A)
4541 A < 0? A : -A same as -abs (A)
4543 None of these transformations work for modes with signed
4544 zeros. If A is +/-0, the first two transformations will
4545 change the sign of the result (from +0 to -0, or vice
4546 versa). The last four will fix the sign of the result,
4547 even though the original expressions could be positive or
4548 negative, depending on the sign of A.
4550 Note that all these transformations are correct if A is
4551 NaN, since the two alternatives (A and -A) are also NaNs. */
4552 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4553 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4554 ? real_zerop (arg01)
4555 : integer_zerop (arg01))
4556 && ((TREE_CODE (arg2) == NEGATE_EXPR
4557 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4558 /* In the case that A is of the form X-Y, '-A' (arg2) may
4559 have already been folded to Y-X, check for that. */
4560 || (TREE_CODE (arg1) == MINUS_EXPR
4561 && TREE_CODE (arg2) == MINUS_EXPR
4562 && operand_equal_p (TREE_OPERAND (arg1, 0),
4563 TREE_OPERAND (arg2, 1), 0)
4564 && operand_equal_p (TREE_OPERAND (arg1, 1),
4565 TREE_OPERAND (arg2, 0), 0))))
4566 switch (comp_code)
4568 case EQ_EXPR:
4569 case UNEQ_EXPR:
4570 tem = fold_convert_loc (loc, arg1_type, arg1);
4571 return pedantic_non_lvalue_loc (loc,
4572 fold_convert_loc (loc, type,
4573 negate_expr (tem)));
4574 case NE_EXPR:
4575 case LTGT_EXPR:
4576 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4577 case UNGE_EXPR:
4578 case UNGT_EXPR:
4579 if (flag_trapping_math)
4580 break;
4581 /* Fall through. */
4582 case GE_EXPR:
4583 case GT_EXPR:
4584 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4585 arg1 = fold_convert_loc (loc, signed_type_for
4586 (TREE_TYPE (arg1)), arg1);
4587 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4588 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4589 case UNLE_EXPR:
4590 case UNLT_EXPR:
4591 if (flag_trapping_math)
4592 break;
4593 case LE_EXPR:
4594 case LT_EXPR:
4595 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4596 arg1 = fold_convert_loc (loc, signed_type_for
4597 (TREE_TYPE (arg1)), arg1);
4598 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4599 return negate_expr (fold_convert_loc (loc, type, tem));
4600 default:
4601 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4602 break;
4605 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4606 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4607 both transformations are correct when A is NaN: A != 0
4608 is then true, and A == 0 is false. */
4610 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4611 && integer_zerop (arg01) && integer_zerop (arg2))
4613 if (comp_code == NE_EXPR)
4614 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4615 else if (comp_code == EQ_EXPR)
4616 return build_int_cst (type, 0);
4619 /* Try some transformations of A op B ? A : B.
4621 A == B? A : B same as B
4622 A != B? A : B same as A
4623 A >= B? A : B same as max (A, B)
4624 A > B? A : B same as max (B, A)
4625 A <= B? A : B same as min (A, B)
4626 A < B? A : B same as min (B, A)
4628 As above, these transformations don't work in the presence
4629 of signed zeros. For example, if A and B are zeros of
4630 opposite sign, the first two transformations will change
4631 the sign of the result. In the last four, the original
4632 expressions give different results for (A=+0, B=-0) and
4633 (A=-0, B=+0), but the transformed expressions do not.
4635 The first two transformations are correct if either A or B
4636 is a NaN. In the first transformation, the condition will
4637 be false, and B will indeed be chosen. In the case of the
4638 second transformation, the condition A != B will be true,
4639 and A will be chosen.
4641 The conversions to max() and min() are not correct if B is
4642 a number and A is not. The conditions in the original
4643 expressions will be false, so all four give B. The min()
4644 and max() versions would give a NaN instead. */
4645 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4646 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4647 /* Avoid these transformations if the COND_EXPR may be used
4648 as an lvalue in the C++ front-end. PR c++/19199. */
4649 && (in_gimple_form
4650 || (strcmp (lang_hooks.name, "GNU C++") != 0
4651 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4652 || ! maybe_lvalue_p (arg1)
4653 || ! maybe_lvalue_p (arg2)))
4655 tree comp_op0 = arg00;
4656 tree comp_op1 = arg01;
4657 tree comp_type = TREE_TYPE (comp_op0);
4659 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4660 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4662 comp_type = type;
4663 comp_op0 = arg1;
4664 comp_op1 = arg2;
4667 switch (comp_code)
4669 case EQ_EXPR:
4670 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4671 case NE_EXPR:
4672 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4673 case LE_EXPR:
4674 case LT_EXPR:
4675 case UNLE_EXPR:
4676 case UNLT_EXPR:
4677 /* In C++ a ?: expression can be an lvalue, so put the
4678 operand which will be used if they are equal first
4679 so that we can convert this back to the
4680 corresponding COND_EXPR. */
4681 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4683 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4684 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4685 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4686 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4687 : fold_build2_loc (loc, MIN_EXPR, comp_type,
4688 comp_op1, comp_op0);
4689 return pedantic_non_lvalue_loc (loc,
4690 fold_convert_loc (loc, type, tem));
4692 break;
4693 case GE_EXPR:
4694 case GT_EXPR:
4695 case UNGE_EXPR:
4696 case UNGT_EXPR:
4697 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4699 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4700 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4701 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4702 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
4703 : fold_build2_loc (loc, MAX_EXPR, comp_type,
4704 comp_op1, comp_op0);
4705 return pedantic_non_lvalue_loc (loc,
4706 fold_convert_loc (loc, type, tem));
4708 break;
4709 case UNEQ_EXPR:
4710 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4711 return pedantic_non_lvalue_loc (loc,
4712 fold_convert_loc (loc, type, arg2));
4713 break;
4714 case LTGT_EXPR:
4715 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4716 return pedantic_non_lvalue_loc (loc,
4717 fold_convert_loc (loc, type, arg1));
4718 break;
4719 default:
4720 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4721 break;
4725 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4726 we might still be able to simplify this. For example,
4727 if C1 is one less or one more than C2, this might have started
4728 out as a MIN or MAX and been transformed by this function.
4729 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
4731 if (INTEGRAL_TYPE_P (type)
4732 && TREE_CODE (arg01) == INTEGER_CST
4733 && TREE_CODE (arg2) == INTEGER_CST)
4734 switch (comp_code)
4736 case EQ_EXPR:
4737 if (TREE_CODE (arg1) == INTEGER_CST)
4738 break;
4739 /* We can replace A with C1 in this case. */
4740 arg1 = fold_convert_loc (loc, type, arg01);
4741 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
4743 case LT_EXPR:
4744 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
4745 MIN_EXPR, to preserve the signedness of the comparison. */
4746 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4747 OEP_ONLY_CONST)
4748 && operand_equal_p (arg01,
4749 const_binop (PLUS_EXPR, arg2,
4750 build_int_cst (type, 1)),
4751 OEP_ONLY_CONST))
4753 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4754 fold_convert_loc (loc, TREE_TYPE (arg00),
4755 arg2));
4756 return pedantic_non_lvalue_loc (loc,
4757 fold_convert_loc (loc, type, tem));
4759 break;
4761 case LE_EXPR:
4762 /* If C1 is C2 - 1, this is min(A, C2), with the same care
4763 as above. */
4764 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4765 OEP_ONLY_CONST)
4766 && operand_equal_p (arg01,
4767 const_binop (MINUS_EXPR, arg2,
4768 build_int_cst (type, 1)),
4769 OEP_ONLY_CONST))
4771 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
4772 fold_convert_loc (loc, TREE_TYPE (arg00),
4773 arg2));
4774 return pedantic_non_lvalue_loc (loc,
4775 fold_convert_loc (loc, type, tem));
4777 break;
4779 case GT_EXPR:
4780 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
4781 MAX_EXPR, to preserve the signedness of the comparison. */
4782 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4783 OEP_ONLY_CONST)
4784 && operand_equal_p (arg01,
4785 const_binop (MINUS_EXPR, arg2,
4786 build_int_cst (type, 1)),
4787 OEP_ONLY_CONST))
4789 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4790 fold_convert_loc (loc, TREE_TYPE (arg00),
4791 arg2));
4792 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4794 break;
4796 case GE_EXPR:
4797 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
4798 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4799 OEP_ONLY_CONST)
4800 && operand_equal_p (arg01,
4801 const_binop (PLUS_EXPR, arg2,
4802 build_int_cst (type, 1)),
4803 OEP_ONLY_CONST))
4805 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
4806 fold_convert_loc (loc, TREE_TYPE (arg00),
4807 arg2));
4808 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4810 break;
4811 case NE_EXPR:
4812 break;
4813 default:
4814 gcc_unreachable ();
4817 return NULL_TREE;
4822 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4823 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
4824 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
4825 false) >= 2)
4826 #endif
4828 /* EXP is some logical combination of boolean tests. See if we can
4829 merge it into some range test. Return the new tree if so. */
4831 static tree
4832 fold_range_test (location_t loc, enum tree_code code, tree type,
4833 tree op0, tree op1)
4835 int or_op = (code == TRUTH_ORIF_EXPR
4836 || code == TRUTH_OR_EXPR);
4837 int in0_p, in1_p, in_p;
4838 tree low0, low1, low, high0, high1, high;
4839 bool strict_overflow_p = false;
4840 tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
4841 tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
4842 tree tem;
4843 const char * const warnmsg = G_("assuming signed overflow does not occur "
4844 "when simplifying range test");
4846 /* If this is an OR operation, invert both sides; we will invert
4847 again at the end. */
4848 if (or_op)
4849 in0_p = ! in0_p, in1_p = ! in1_p;
4851 /* If both expressions are the same, if we can merge the ranges, and we
4852 can build the range test, return it or it inverted. If one of the
4853 ranges is always true or always false, consider it to be the same
4854 expression as the other. */
4855 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4856 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4857 in1_p, low1, high1)
4858 && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type,
4859 lhs != 0 ? lhs
4860 : rhs != 0 ? rhs : integer_zero_node,
4861 in_p, low, high))))
4863 if (strict_overflow_p)
4864 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
4865 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
4868 /* On machines where the branch cost is expensive, if this is a
4869 short-circuited branch and the underlying object on both sides
4870 is the same, make a non-short-circuit operation. */
4871 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4872 && lhs != 0 && rhs != 0
4873 && (code == TRUTH_ANDIF_EXPR
4874 || code == TRUTH_ORIF_EXPR)
4875 && operand_equal_p (lhs, rhs, 0))
4877 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
4878 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4879 which cases we can't do this. */
4880 if (simple_operand_p (lhs))
4881 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4882 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4883 type, op0, op1);
4885 else if (lang_hooks.decls.global_bindings_p () == 0
4886 && ! CONTAINS_PLACEHOLDER_P (lhs))
4888 tree common = save_expr (lhs);
4890 if (0 != (lhs = build_range_check (loc, type, common,
4891 or_op ? ! in0_p : in0_p,
4892 low0, high0))
4893 && (0 != (rhs = build_range_check (loc, type, common,
4894 or_op ? ! in1_p : in1_p,
4895 low1, high1))))
4897 if (strict_overflow_p)
4898 fold_overflow_warning (warnmsg,
4899 WARN_STRICT_OVERFLOW_COMPARISON);
4900 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
4901 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4902 type, lhs, rhs);
4907 return 0;
4910 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4911 bit value. Arrange things so the extra bits will be set to zero if and
4912 only if C is signed-extended to its full width. If MASK is nonzero,
4913 it is an INTEGER_CST that should be AND'ed with the extra bits. */
4915 static tree
4916 unextend (tree c, int p, int unsignedp, tree mask)
4918 tree type = TREE_TYPE (c);
4919 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4920 tree temp;
4922 if (p == modesize || unsignedp)
4923 return c;
4925 /* We work by getting just the sign bit into the low-order bit, then
4926 into the high-order bit, then sign-extend. We then XOR that value
4927 with C. */
4928 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1));
4929 temp = const_binop (BIT_AND_EXPR, temp, size_int (1));
4931 /* We must use a signed type in order to get an arithmetic right shift.
4932 However, we must also avoid introducing accidental overflows, so that
4933 a subsequent call to integer_zerop will work. Hence we must
4934 do the type conversion here. At this point, the constant is either
4935 zero or one, and the conversion to a signed type can never overflow.
4936 We could get an overflow if this conversion is done anywhere else. */
4937 if (TYPE_UNSIGNED (type))
4938 temp = fold_convert (signed_type_for (type), temp);
4940 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
4941 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
4942 if (mask != 0)
4943 temp = const_binop (BIT_AND_EXPR, temp,
4944 fold_convert (TREE_TYPE (c), mask));
4945 /* If necessary, convert the type back to match the type of C. */
4946 if (TYPE_UNSIGNED (type))
4947 temp = fold_convert (type, temp);
4949 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
4952 /* For an expression that has the form
4953 (A && B) || ~B
4955 (A || B) && ~B,
4956 we can drop one of the inner expressions and simplify to
4957 A || ~B
4959 A && ~B
4960 LOC is the location of the resulting expression. OP is the inner
4961 logical operation; the left-hand side in the examples above, while CMPOP
4962 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
4963 removing a condition that guards another, as in
4964 (A != NULL && A->...) || A == NULL
4965 which we must not transform. If RHS_ONLY is true, only eliminate the
4966 right-most operand of the inner logical operation. */
4968 static tree
4969 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
4970 bool rhs_only)
4972 tree type = TREE_TYPE (cmpop);
4973 enum tree_code code = TREE_CODE (cmpop);
4974 enum tree_code truthop_code = TREE_CODE (op);
4975 tree lhs = TREE_OPERAND (op, 0);
4976 tree rhs = TREE_OPERAND (op, 1);
4977 tree orig_lhs = lhs, orig_rhs = rhs;
4978 enum tree_code rhs_code = TREE_CODE (rhs);
4979 enum tree_code lhs_code = TREE_CODE (lhs);
4980 enum tree_code inv_code;
4982 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
4983 return NULL_TREE;
4985 if (TREE_CODE_CLASS (code) != tcc_comparison)
4986 return NULL_TREE;
4988 if (rhs_code == truthop_code)
4990 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
4991 if (newrhs != NULL_TREE)
4993 rhs = newrhs;
4994 rhs_code = TREE_CODE (rhs);
4997 if (lhs_code == truthop_code && !rhs_only)
4999 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5000 if (newlhs != NULL_TREE)
5002 lhs = newlhs;
5003 lhs_code = TREE_CODE (lhs);
5007 inv_code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
5008 if (inv_code == rhs_code
5009 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5010 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5011 return lhs;
5012 if (!rhs_only && inv_code == lhs_code
5013 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5014 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5015 return rhs;
5016 if (rhs != orig_rhs || lhs != orig_lhs)
5017 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5018 lhs, rhs);
5019 return NULL_TREE;
5022 /* Find ways of folding logical expressions of LHS and RHS:
5023 Try to merge two comparisons to the same innermost item.
5024 Look for range tests like "ch >= '0' && ch <= '9'".
5025 Look for combinations of simple terms on machines with expensive branches
5026 and evaluate the RHS unconditionally.
5028 For example, if we have p->a == 2 && p->b == 4 and we can make an
5029 object large enough to span both A and B, we can do this with a comparison
5030 against the object ANDed with the a mask.
5032 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5033 operations to do this with one comparison.
5035 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5036 function and the one above.
5038 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5039 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5041 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5042 two operands.
5044 We return the simplified tree or 0 if no optimization is possible. */
5046 static tree
5047 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5048 tree lhs, tree rhs)
5050 /* If this is the "or" of two comparisons, we can do something if
5051 the comparisons are NE_EXPR. If this is the "and", we can do something
5052 if the comparisons are EQ_EXPR. I.e.,
5053 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5055 WANTED_CODE is this operation code. For single bit fields, we can
5056 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5057 comparison for one-bit fields. */
5059 enum tree_code wanted_code;
5060 enum tree_code lcode, rcode;
5061 tree ll_arg, lr_arg, rl_arg, rr_arg;
5062 tree ll_inner, lr_inner, rl_inner, rr_inner;
5063 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5064 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5065 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5066 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5067 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5068 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5069 enum machine_mode lnmode, rnmode;
5070 tree ll_mask, lr_mask, rl_mask, rr_mask;
5071 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5072 tree l_const, r_const;
5073 tree lntype, rntype, result;
5074 HOST_WIDE_INT first_bit, end_bit;
5075 int volatilep;
5076 tree orig_lhs = lhs, orig_rhs = rhs;
5077 enum tree_code orig_code = code;
5079 /* Start by getting the comparison codes. Fail if anything is volatile.
5080 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5081 it were surrounded with a NE_EXPR. */
5083 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5084 return 0;
5086 lcode = TREE_CODE (lhs);
5087 rcode = TREE_CODE (rhs);
5089 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5091 lhs = build2 (NE_EXPR, truth_type, lhs,
5092 build_int_cst (TREE_TYPE (lhs), 0));
5093 lcode = NE_EXPR;
5096 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5098 rhs = build2 (NE_EXPR, truth_type, rhs,
5099 build_int_cst (TREE_TYPE (rhs), 0));
5100 rcode = NE_EXPR;
5103 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5104 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5105 return 0;
5107 ll_arg = TREE_OPERAND (lhs, 0);
5108 lr_arg = TREE_OPERAND (lhs, 1);
5109 rl_arg = TREE_OPERAND (rhs, 0);
5110 rr_arg = TREE_OPERAND (rhs, 1);
5112 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5113 if (simple_operand_p (ll_arg)
5114 && simple_operand_p (lr_arg))
5116 if (operand_equal_p (ll_arg, rl_arg, 0)
5117 && operand_equal_p (lr_arg, rr_arg, 0))
5119 result = combine_comparisons (loc, code, lcode, rcode,
5120 truth_type, ll_arg, lr_arg);
5121 if (result)
5122 return result;
5124 else if (operand_equal_p (ll_arg, rr_arg, 0)
5125 && operand_equal_p (lr_arg, rl_arg, 0))
5127 result = combine_comparisons (loc, code, lcode,
5128 swap_tree_comparison (rcode),
5129 truth_type, ll_arg, lr_arg);
5130 if (result)
5131 return result;
5135 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5136 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5138 /* If the RHS can be evaluated unconditionally and its operands are
5139 simple, it wins to evaluate the RHS unconditionally on machines
5140 with expensive branches. In this case, this isn't a comparison
5141 that can be merged. Avoid doing this if the RHS is a floating-point
5142 comparison since those can trap. */
5144 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5145 false) >= 2
5146 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5147 && simple_operand_p (rl_arg)
5148 && simple_operand_p (rr_arg))
5150 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5151 if (code == TRUTH_OR_EXPR
5152 && lcode == NE_EXPR && integer_zerop (lr_arg)
5153 && rcode == NE_EXPR && integer_zerop (rr_arg)
5154 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5155 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5156 return build2_loc (loc, NE_EXPR, truth_type,
5157 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5158 ll_arg, rl_arg),
5159 build_int_cst (TREE_TYPE (ll_arg), 0));
5161 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5162 if (code == TRUTH_AND_EXPR
5163 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5164 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5165 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5166 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5167 return build2_loc (loc, EQ_EXPR, truth_type,
5168 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5169 ll_arg, rl_arg),
5170 build_int_cst (TREE_TYPE (ll_arg), 0));
5172 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5174 if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5175 return build2_loc (loc, code, truth_type, lhs, rhs);
5176 return NULL_TREE;
5180 /* See if the comparisons can be merged. Then get all the parameters for
5181 each side. */
5183 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5184 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5185 return 0;
5187 volatilep = 0;
5188 ll_inner = decode_field_reference (loc, ll_arg,
5189 &ll_bitsize, &ll_bitpos, &ll_mode,
5190 &ll_unsignedp, &volatilep, &ll_mask,
5191 &ll_and_mask);
5192 lr_inner = decode_field_reference (loc, lr_arg,
5193 &lr_bitsize, &lr_bitpos, &lr_mode,
5194 &lr_unsignedp, &volatilep, &lr_mask,
5195 &lr_and_mask);
5196 rl_inner = decode_field_reference (loc, rl_arg,
5197 &rl_bitsize, &rl_bitpos, &rl_mode,
5198 &rl_unsignedp, &volatilep, &rl_mask,
5199 &rl_and_mask);
5200 rr_inner = decode_field_reference (loc, rr_arg,
5201 &rr_bitsize, &rr_bitpos, &rr_mode,
5202 &rr_unsignedp, &volatilep, &rr_mask,
5203 &rr_and_mask);
5205 /* It must be true that the inner operation on the lhs of each
5206 comparison must be the same if we are to be able to do anything.
5207 Then see if we have constants. If not, the same must be true for
5208 the rhs's. */
5209 if (volatilep || ll_inner == 0 || rl_inner == 0
5210 || ! operand_equal_p (ll_inner, rl_inner, 0))
5211 return 0;
5213 if (TREE_CODE (lr_arg) == INTEGER_CST
5214 && TREE_CODE (rr_arg) == INTEGER_CST)
5215 l_const = lr_arg, r_const = rr_arg;
5216 else if (lr_inner == 0 || rr_inner == 0
5217 || ! operand_equal_p (lr_inner, rr_inner, 0))
5218 return 0;
5219 else
5220 l_const = r_const = 0;
5222 /* If either comparison code is not correct for our logical operation,
5223 fail. However, we can convert a one-bit comparison against zero into
5224 the opposite comparison against that bit being set in the field. */
5226 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5227 if (lcode != wanted_code)
5229 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5231 /* Make the left operand unsigned, since we are only interested
5232 in the value of one bit. Otherwise we are doing the wrong
5233 thing below. */
5234 ll_unsignedp = 1;
5235 l_const = ll_mask;
5237 else
5238 return 0;
5241 /* This is analogous to the code for l_const above. */
5242 if (rcode != wanted_code)
5244 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5246 rl_unsignedp = 1;
5247 r_const = rl_mask;
5249 else
5250 return 0;
5253 /* See if we can find a mode that contains both fields being compared on
5254 the left. If we can't, fail. Otherwise, update all constants and masks
5255 to be relative to a field of that size. */
5256 first_bit = MIN (ll_bitpos, rl_bitpos);
5257 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5258 lnmode = get_best_mode (end_bit - first_bit, first_bit,
5259 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5260 volatilep);
5261 if (lnmode == VOIDmode)
5262 return 0;
5264 lnbitsize = GET_MODE_BITSIZE (lnmode);
5265 lnbitpos = first_bit & ~ (lnbitsize - 1);
5266 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5267 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5269 if (BYTES_BIG_ENDIAN)
5271 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5272 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5275 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5276 size_int (xll_bitpos));
5277 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5278 size_int (xrl_bitpos));
5280 if (l_const)
5282 l_const = fold_convert_loc (loc, lntype, l_const);
5283 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5284 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5285 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5286 fold_build1_loc (loc, BIT_NOT_EXPR,
5287 lntype, ll_mask))))
5289 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5291 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5294 if (r_const)
5296 r_const = fold_convert_loc (loc, lntype, r_const);
5297 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5298 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5299 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5300 fold_build1_loc (loc, BIT_NOT_EXPR,
5301 lntype, rl_mask))))
5303 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5305 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5309 /* If the right sides are not constant, do the same for it. Also,
5310 disallow this optimization if a size or signedness mismatch occurs
5311 between the left and right sides. */
5312 if (l_const == 0)
5314 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5315 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5316 /* Make sure the two fields on the right
5317 correspond to the left without being swapped. */
5318 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5319 return 0;
5321 first_bit = MIN (lr_bitpos, rr_bitpos);
5322 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5323 rnmode = get_best_mode (end_bit - first_bit, first_bit,
5324 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5325 volatilep);
5326 if (rnmode == VOIDmode)
5327 return 0;
5329 rnbitsize = GET_MODE_BITSIZE (rnmode);
5330 rnbitpos = first_bit & ~ (rnbitsize - 1);
5331 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5332 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5334 if (BYTES_BIG_ENDIAN)
5336 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5337 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5340 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5341 rntype, lr_mask),
5342 size_int (xlr_bitpos));
5343 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5344 rntype, rr_mask),
5345 size_int (xrr_bitpos));
5347 /* Make a mask that corresponds to both fields being compared.
5348 Do this for both items being compared. If the operands are the
5349 same size and the bits being compared are in the same position
5350 then we can do this by masking both and comparing the masked
5351 results. */
5352 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5353 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5354 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5356 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5357 ll_unsignedp || rl_unsignedp);
5358 if (! all_ones_mask_p (ll_mask, lnbitsize))
5359 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5361 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5362 lr_unsignedp || rr_unsignedp);
5363 if (! all_ones_mask_p (lr_mask, rnbitsize))
5364 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5366 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5369 /* There is still another way we can do something: If both pairs of
5370 fields being compared are adjacent, we may be able to make a wider
5371 field containing them both.
5373 Note that we still must mask the lhs/rhs expressions. Furthermore,
5374 the mask must be shifted to account for the shift done by
5375 make_bit_field_ref. */
5376 if ((ll_bitsize + ll_bitpos == rl_bitpos
5377 && lr_bitsize + lr_bitpos == rr_bitpos)
5378 || (ll_bitpos == rl_bitpos + rl_bitsize
5379 && lr_bitpos == rr_bitpos + rr_bitsize))
5381 tree type;
5383 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5384 ll_bitsize + rl_bitsize,
5385 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5386 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5387 lr_bitsize + rr_bitsize,
5388 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5390 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5391 size_int (MIN (xll_bitpos, xrl_bitpos)));
5392 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5393 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5395 /* Convert to the smaller type before masking out unwanted bits. */
5396 type = lntype;
5397 if (lntype != rntype)
5399 if (lnbitsize > rnbitsize)
5401 lhs = fold_convert_loc (loc, rntype, lhs);
5402 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5403 type = rntype;
5405 else if (lnbitsize < rnbitsize)
5407 rhs = fold_convert_loc (loc, lntype, rhs);
5408 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5409 type = lntype;
5413 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5414 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5416 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5417 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5419 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5422 return 0;
5425 /* Handle the case of comparisons with constants. If there is something in
5426 common between the masks, those bits of the constants must be the same.
5427 If not, the condition is always false. Test for this to avoid generating
5428 incorrect code below. */
5429 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5430 if (! integer_zerop (result)
5431 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5432 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5434 if (wanted_code == NE_EXPR)
5436 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5437 return constant_boolean_node (true, truth_type);
5439 else
5441 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5442 return constant_boolean_node (false, truth_type);
5446 /* Construct the expression we will return. First get the component
5447 reference we will make. Unless the mask is all ones the width of
5448 that field, perform the mask operation. Then compare with the
5449 merged constant. */
5450 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5451 ll_unsignedp || rl_unsignedp);
5453 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5454 if (! all_ones_mask_p (ll_mask, lnbitsize))
5455 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5457 return build2_loc (loc, wanted_code, truth_type, result,
5458 const_binop (BIT_IOR_EXPR, l_const, r_const));
5461 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5462 constant. */
5464 static tree
5465 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5466 tree op0, tree op1)
5468 tree arg0 = op0;
5469 enum tree_code op_code;
5470 tree comp_const;
5471 tree minmax_const;
5472 int consts_equal, consts_lt;
5473 tree inner;
5475 STRIP_SIGN_NOPS (arg0);
5477 op_code = TREE_CODE (arg0);
5478 minmax_const = TREE_OPERAND (arg0, 1);
5479 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5480 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5481 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5482 inner = TREE_OPERAND (arg0, 0);
5484 /* If something does not permit us to optimize, return the original tree. */
5485 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5486 || TREE_CODE (comp_const) != INTEGER_CST
5487 || TREE_OVERFLOW (comp_const)
5488 || TREE_CODE (minmax_const) != INTEGER_CST
5489 || TREE_OVERFLOW (minmax_const))
5490 return NULL_TREE;
5492 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5493 and GT_EXPR, doing the rest with recursive calls using logical
5494 simplifications. */
5495 switch (code)
5497 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5499 tree tem
5500 = optimize_minmax_comparison (loc,
5501 invert_tree_comparison (code, false),
5502 type, op0, op1);
5503 if (tem)
5504 return invert_truthvalue_loc (loc, tem);
5505 return NULL_TREE;
5508 case GE_EXPR:
5509 return
5510 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5511 optimize_minmax_comparison
5512 (loc, EQ_EXPR, type, arg0, comp_const),
5513 optimize_minmax_comparison
5514 (loc, GT_EXPR, type, arg0, comp_const));
5516 case EQ_EXPR:
5517 if (op_code == MAX_EXPR && consts_equal)
5518 /* MAX (X, 0) == 0 -> X <= 0 */
5519 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5521 else if (op_code == MAX_EXPR && consts_lt)
5522 /* MAX (X, 0) == 5 -> X == 5 */
5523 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5525 else if (op_code == MAX_EXPR)
5526 /* MAX (X, 0) == -1 -> false */
5527 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5529 else if (consts_equal)
5530 /* MIN (X, 0) == 0 -> X >= 0 */
5531 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5533 else if (consts_lt)
5534 /* MIN (X, 0) == 5 -> false */
5535 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5537 else
5538 /* MIN (X, 0) == -1 -> X == -1 */
5539 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5541 case GT_EXPR:
5542 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5543 /* MAX (X, 0) > 0 -> X > 0
5544 MAX (X, 0) > 5 -> X > 5 */
5545 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5547 else if (op_code == MAX_EXPR)
5548 /* MAX (X, 0) > -1 -> true */
5549 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5551 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5552 /* MIN (X, 0) > 0 -> false
5553 MIN (X, 0) > 5 -> false */
5554 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5556 else
5557 /* MIN (X, 0) > -1 -> X > -1 */
5558 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5560 default:
5561 return NULL_TREE;
5565 /* T is an integer expression that is being multiplied, divided, or taken a
5566 modulus (CODE says which and what kind of divide or modulus) by a
5567 constant C. See if we can eliminate that operation by folding it with
5568 other operations already in T. WIDE_TYPE, if non-null, is a type that
5569 should be used for the computation if wider than our type.
5571 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5572 (X * 2) + (Y * 4). We must, however, be assured that either the original
5573 expression would not overflow or that overflow is undefined for the type
5574 in the language in question.
5576 If we return a non-null expression, it is an equivalent form of the
5577 original computation, but need not be in the original type.
5579 We set *STRICT_OVERFLOW_P to true if the return values depends on
5580 signed overflow being undefined. Otherwise we do not change
5581 *STRICT_OVERFLOW_P. */
5583 static tree
5584 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5585 bool *strict_overflow_p)
5587 /* To avoid exponential search depth, refuse to allow recursion past
5588 three levels. Beyond that (1) it's highly unlikely that we'll find
5589 something interesting and (2) we've probably processed it before
5590 when we built the inner expression. */
5592 static int depth;
5593 tree ret;
5595 if (depth > 3)
5596 return NULL;
5598 depth++;
5599 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5600 depth--;
5602 return ret;
5605 static tree
5606 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5607 bool *strict_overflow_p)
5609 tree type = TREE_TYPE (t);
5610 enum tree_code tcode = TREE_CODE (t);
5611 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5612 > GET_MODE_SIZE (TYPE_MODE (type)))
5613 ? wide_type : type);
5614 tree t1, t2;
5615 int same_p = tcode == code;
5616 tree op0 = NULL_TREE, op1 = NULL_TREE;
5617 bool sub_strict_overflow_p;
5619 /* Don't deal with constants of zero here; they confuse the code below. */
5620 if (integer_zerop (c))
5621 return NULL_TREE;
5623 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5624 op0 = TREE_OPERAND (t, 0);
5626 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5627 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5629 /* Note that we need not handle conditional operations here since fold
5630 already handles those cases. So just do arithmetic here. */
5631 switch (tcode)
5633 case INTEGER_CST:
5634 /* For a constant, we can always simplify if we are a multiply
5635 or (for divide and modulus) if it is a multiple of our constant. */
5636 if (code == MULT_EXPR
5637 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c)))
5638 return const_binop (code, fold_convert (ctype, t),
5639 fold_convert (ctype, c));
5640 break;
5642 CASE_CONVERT: case NON_LVALUE_EXPR:
5643 /* If op0 is an expression ... */
5644 if ((COMPARISON_CLASS_P (op0)
5645 || UNARY_CLASS_P (op0)
5646 || BINARY_CLASS_P (op0)
5647 || VL_EXP_CLASS_P (op0)
5648 || EXPRESSION_CLASS_P (op0))
5649 /* ... and has wrapping overflow, and its type is smaller
5650 than ctype, then we cannot pass through as widening. */
5651 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5652 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5653 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5654 && (TYPE_PRECISION (ctype)
5655 > TYPE_PRECISION (TREE_TYPE (op0))))
5656 /* ... or this is a truncation (t is narrower than op0),
5657 then we cannot pass through this narrowing. */
5658 || (TYPE_PRECISION (type)
5659 < TYPE_PRECISION (TREE_TYPE (op0)))
5660 /* ... or signedness changes for division or modulus,
5661 then we cannot pass through this conversion. */
5662 || (code != MULT_EXPR
5663 && (TYPE_UNSIGNED (ctype)
5664 != TYPE_UNSIGNED (TREE_TYPE (op0))))
5665 /* ... or has undefined overflow while the converted to
5666 type has not, we cannot do the operation in the inner type
5667 as that would introduce undefined overflow. */
5668 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5669 && !TYPE_OVERFLOW_UNDEFINED (type))))
5670 break;
5672 /* Pass the constant down and see if we can make a simplification. If
5673 we can, replace this expression with the inner simplification for
5674 possible later conversion to our or some other type. */
5675 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5676 && TREE_CODE (t2) == INTEGER_CST
5677 && !TREE_OVERFLOW (t2)
5678 && (0 != (t1 = extract_muldiv (op0, t2, code,
5679 code == MULT_EXPR
5680 ? ctype : NULL_TREE,
5681 strict_overflow_p))))
5682 return t1;
5683 break;
5685 case ABS_EXPR:
5686 /* If widening the type changes it from signed to unsigned, then we
5687 must avoid building ABS_EXPR itself as unsigned. */
5688 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5690 tree cstype = (*signed_type_for) (ctype);
5691 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5692 != 0)
5694 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5695 return fold_convert (ctype, t1);
5697 break;
5699 /* If the constant is negative, we cannot simplify this. */
5700 if (tree_int_cst_sgn (c) == -1)
5701 break;
5702 /* FALLTHROUGH */
5703 case NEGATE_EXPR:
5704 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5705 != 0)
5706 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5707 break;
5709 case MIN_EXPR: case MAX_EXPR:
5710 /* If widening the type changes the signedness, then we can't perform
5711 this optimization as that changes the result. */
5712 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5713 break;
5715 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5716 sub_strict_overflow_p = false;
5717 if ((t1 = extract_muldiv (op0, c, code, wide_type,
5718 &sub_strict_overflow_p)) != 0
5719 && (t2 = extract_muldiv (op1, c, code, wide_type,
5720 &sub_strict_overflow_p)) != 0)
5722 if (tree_int_cst_sgn (c) < 0)
5723 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5724 if (sub_strict_overflow_p)
5725 *strict_overflow_p = true;
5726 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5727 fold_convert (ctype, t2));
5729 break;
5731 case LSHIFT_EXPR: case RSHIFT_EXPR:
5732 /* If the second operand is constant, this is a multiplication
5733 or floor division, by a power of two, so we can treat it that
5734 way unless the multiplier or divisor overflows. Signed
5735 left-shift overflow is implementation-defined rather than
5736 undefined in C90, so do not convert signed left shift into
5737 multiplication. */
5738 if (TREE_CODE (op1) == INTEGER_CST
5739 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5740 /* const_binop may not detect overflow correctly,
5741 so check for it explicitly here. */
5742 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5743 && TREE_INT_CST_HIGH (op1) == 0
5744 && 0 != (t1 = fold_convert (ctype,
5745 const_binop (LSHIFT_EXPR,
5746 size_one_node,
5747 op1)))
5748 && !TREE_OVERFLOW (t1))
5749 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5750 ? MULT_EXPR : FLOOR_DIV_EXPR,
5751 ctype,
5752 fold_convert (ctype, op0),
5753 t1),
5754 c, code, wide_type, strict_overflow_p);
5755 break;
5757 case PLUS_EXPR: case MINUS_EXPR:
5758 /* See if we can eliminate the operation on both sides. If we can, we
5759 can return a new PLUS or MINUS. If we can't, the only remaining
5760 cases where we can do anything are if the second operand is a
5761 constant. */
5762 sub_strict_overflow_p = false;
5763 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5764 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5765 if (t1 != 0 && t2 != 0
5766 && (code == MULT_EXPR
5767 /* If not multiplication, we can only do this if both operands
5768 are divisible by c. */
5769 || (multiple_of_p (ctype, op0, c)
5770 && multiple_of_p (ctype, op1, c))))
5772 if (sub_strict_overflow_p)
5773 *strict_overflow_p = true;
5774 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5775 fold_convert (ctype, t2));
5778 /* If this was a subtraction, negate OP1 and set it to be an addition.
5779 This simplifies the logic below. */
5780 if (tcode == MINUS_EXPR)
5782 tcode = PLUS_EXPR, op1 = negate_expr (op1);
5783 /* If OP1 was not easily negatable, the constant may be OP0. */
5784 if (TREE_CODE (op0) == INTEGER_CST)
5786 tree tem = op0;
5787 op0 = op1;
5788 op1 = tem;
5789 tem = t1;
5790 t1 = t2;
5791 t2 = tem;
5795 if (TREE_CODE (op1) != INTEGER_CST)
5796 break;
5798 /* If either OP1 or C are negative, this optimization is not safe for
5799 some of the division and remainder types while for others we need
5800 to change the code. */
5801 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5803 if (code == CEIL_DIV_EXPR)
5804 code = FLOOR_DIV_EXPR;
5805 else if (code == FLOOR_DIV_EXPR)
5806 code = CEIL_DIV_EXPR;
5807 else if (code != MULT_EXPR
5808 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5809 break;
5812 /* If it's a multiply or a division/modulus operation of a multiple
5813 of our constant, do the operation and verify it doesn't overflow. */
5814 if (code == MULT_EXPR
5815 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5817 op1 = const_binop (code, fold_convert (ctype, op1),
5818 fold_convert (ctype, c));
5819 /* We allow the constant to overflow with wrapping semantics. */
5820 if (op1 == 0
5821 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5822 break;
5824 else
5825 break;
5827 /* If we have an unsigned type is not a sizetype, we cannot widen
5828 the operation since it will change the result if the original
5829 computation overflowed. */
5830 if (TYPE_UNSIGNED (ctype)
5831 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5832 && ctype != type)
5833 break;
5835 /* If we were able to eliminate our operation from the first side,
5836 apply our operation to the second side and reform the PLUS. */
5837 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5838 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5840 /* The last case is if we are a multiply. In that case, we can
5841 apply the distributive law to commute the multiply and addition
5842 if the multiplication of the constants doesn't overflow. */
5843 if (code == MULT_EXPR)
5844 return fold_build2 (tcode, ctype,
5845 fold_build2 (code, ctype,
5846 fold_convert (ctype, op0),
5847 fold_convert (ctype, c)),
5848 op1);
5850 break;
5852 case MULT_EXPR:
5853 /* We have a special case here if we are doing something like
5854 (C * 8) % 4 since we know that's zero. */
5855 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5856 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5857 /* If the multiplication can overflow we cannot optimize this.
5858 ??? Until we can properly mark individual operations as
5859 not overflowing we need to treat sizetype special here as
5860 stor-layout relies on this opimization to make
5861 DECL_FIELD_BIT_OFFSET always a constant. */
5862 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5863 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5864 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5865 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5866 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5868 *strict_overflow_p = true;
5869 return omit_one_operand (type, integer_zero_node, op0);
5872 /* ... fall through ... */
5874 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
5875 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
5876 /* If we can extract our operation from the LHS, do so and return a
5877 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
5878 do something only if the second operand is a constant. */
5879 if (same_p
5880 && (t1 = extract_muldiv (op0, c, code, wide_type,
5881 strict_overflow_p)) != 0)
5882 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5883 fold_convert (ctype, op1));
5884 else if (tcode == MULT_EXPR && code == MULT_EXPR
5885 && (t1 = extract_muldiv (op1, c, code, wide_type,
5886 strict_overflow_p)) != 0)
5887 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5888 fold_convert (ctype, t1));
5889 else if (TREE_CODE (op1) != INTEGER_CST)
5890 return 0;
5892 /* If these are the same operation types, we can associate them
5893 assuming no overflow. */
5894 if (tcode == code
5895 && 0 != (t1 = int_const_binop (MULT_EXPR,
5896 fold_convert (ctype, op1),
5897 fold_convert (ctype, c), 1))
5898 && 0 != (t1 = force_fit_type_double (ctype, tree_to_double_int (t1),
5899 (TYPE_UNSIGNED (ctype)
5900 && tcode != MULT_EXPR) ? -1 : 1,
5901 TREE_OVERFLOW (t1)))
5902 && !TREE_OVERFLOW (t1))
5903 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5905 /* If these operations "cancel" each other, we have the main
5906 optimizations of this pass, which occur when either constant is a
5907 multiple of the other, in which case we replace this with either an
5908 operation or CODE or TCODE.
5910 If we have an unsigned type that is not a sizetype, we cannot do
5911 this since it will change the result if the original computation
5912 overflowed. */
5913 if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5914 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5915 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5916 || (tcode == MULT_EXPR
5917 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5918 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5919 && code != MULT_EXPR)))
5921 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c)))
5923 if (TYPE_OVERFLOW_UNDEFINED (ctype))
5924 *strict_overflow_p = true;
5925 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5926 fold_convert (ctype,
5927 const_binop (TRUNC_DIV_EXPR,
5928 op1, c)));
5930 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1)))
5932 if (TYPE_OVERFLOW_UNDEFINED (ctype))
5933 *strict_overflow_p = true;
5934 return fold_build2 (code, ctype, fold_convert (ctype, op0),
5935 fold_convert (ctype,
5936 const_binop (TRUNC_DIV_EXPR,
5937 c, op1)));
5940 break;
5942 default:
5943 break;
5946 return 0;
5949 /* Return a node which has the indicated constant VALUE (either 0 or
5950 1), and is of the indicated TYPE. */
5952 tree
5953 constant_boolean_node (int value, tree type)
5955 if (type == integer_type_node)
5956 return value ? integer_one_node : integer_zero_node;
5957 else if (type == boolean_type_node)
5958 return value ? boolean_true_node : boolean_false_node;
5959 else
5960 return build_int_cst (type, value);
5964 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
5965 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
5966 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
5967 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
5968 COND is the first argument to CODE; otherwise (as in the example
5969 given here), it is the second argument. TYPE is the type of the
5970 original expression. Return NULL_TREE if no simplification is
5971 possible. */
5973 static tree
5974 fold_binary_op_with_conditional_arg (location_t loc,
5975 enum tree_code code,
5976 tree type, tree op0, tree op1,
5977 tree cond, tree arg, int cond_first_p)
5979 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
5980 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
5981 tree test, true_value, false_value;
5982 tree lhs = NULL_TREE;
5983 tree rhs = NULL_TREE;
5985 if (TREE_CODE (cond) == COND_EXPR)
5987 test = TREE_OPERAND (cond, 0);
5988 true_value = TREE_OPERAND (cond, 1);
5989 false_value = TREE_OPERAND (cond, 2);
5990 /* If this operand throws an expression, then it does not make
5991 sense to try to perform a logical or arithmetic operation
5992 involving it. */
5993 if (VOID_TYPE_P (TREE_TYPE (true_value)))
5994 lhs = true_value;
5995 if (VOID_TYPE_P (TREE_TYPE (false_value)))
5996 rhs = false_value;
5998 else
6000 tree testtype = TREE_TYPE (cond);
6001 test = cond;
6002 true_value = constant_boolean_node (true, testtype);
6003 false_value = constant_boolean_node (false, testtype);
6006 /* This transformation is only worthwhile if we don't have to wrap ARG
6007 in a SAVE_EXPR and the operation can be simplified on at least one
6008 of the branches once its pushed inside the COND_EXPR. */
6009 if (!TREE_CONSTANT (arg)
6010 && (TREE_SIDE_EFFECTS (arg)
6011 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6012 return NULL_TREE;
6014 arg = fold_convert_loc (loc, arg_type, arg);
6015 if (lhs == 0)
6017 true_value = fold_convert_loc (loc, cond_type, true_value);
6018 if (cond_first_p)
6019 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6020 else
6021 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6023 if (rhs == 0)
6025 false_value = fold_convert_loc (loc, cond_type, false_value);
6026 if (cond_first_p)
6027 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6028 else
6029 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6032 /* Check that we have simplified at least one of the branches. */
6033 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6034 return NULL_TREE;
6036 return fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6040 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6042 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6043 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6044 ADDEND is the same as X.
6046 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6047 and finite. The problematic cases are when X is zero, and its mode
6048 has signed zeros. In the case of rounding towards -infinity,
6049 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6050 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6052 bool
6053 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6055 if (!real_zerop (addend))
6056 return false;
6058 /* Don't allow the fold with -fsignaling-nans. */
6059 if (HONOR_SNANS (TYPE_MODE (type)))
6060 return false;
6062 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6063 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6064 return true;
6066 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6067 if (TREE_CODE (addend) == REAL_CST
6068 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6069 negate = !negate;
6071 /* The mode has signed zeros, and we have to honor their sign.
6072 In this situation, there is only one case we can return true for.
6073 X - 0 is the same as X unless rounding towards -infinity is
6074 supported. */
6075 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6078 /* Subroutine of fold() that checks comparisons of built-in math
6079 functions against real constants.
6081 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6082 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6083 is the type of the result and ARG0 and ARG1 are the operands of the
6084 comparison. ARG1 must be a TREE_REAL_CST.
6086 The function returns the constant folded tree if a simplification
6087 can be made, and NULL_TREE otherwise. */
6089 static tree
6090 fold_mathfn_compare (location_t loc,
6091 enum built_in_function fcode, enum tree_code code,
6092 tree type, tree arg0, tree arg1)
6094 REAL_VALUE_TYPE c;
6096 if (BUILTIN_SQRT_P (fcode))
6098 tree arg = CALL_EXPR_ARG (arg0, 0);
6099 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6101 c = TREE_REAL_CST (arg1);
6102 if (REAL_VALUE_NEGATIVE (c))
6104 /* sqrt(x) < y is always false, if y is negative. */
6105 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6106 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6108 /* sqrt(x) > y is always true, if y is negative and we
6109 don't care about NaNs, i.e. negative values of x. */
6110 if (code == NE_EXPR || !HONOR_NANS (mode))
6111 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6113 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6114 return fold_build2_loc (loc, GE_EXPR, type, arg,
6115 build_real (TREE_TYPE (arg), dconst0));
6117 else if (code == GT_EXPR || code == GE_EXPR)
6119 REAL_VALUE_TYPE c2;
6121 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6122 real_convert (&c2, mode, &c2);
6124 if (REAL_VALUE_ISINF (c2))
6126 /* sqrt(x) > y is x == +Inf, when y is very large. */
6127 if (HONOR_INFINITIES (mode))
6128 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6129 build_real (TREE_TYPE (arg), c2));
6131 /* sqrt(x) > y is always false, when y is very large
6132 and we don't care about infinities. */
6133 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6136 /* sqrt(x) > c is the same as x > c*c. */
6137 return fold_build2_loc (loc, code, type, arg,
6138 build_real (TREE_TYPE (arg), c2));
6140 else if (code == LT_EXPR || code == LE_EXPR)
6142 REAL_VALUE_TYPE c2;
6144 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6145 real_convert (&c2, mode, &c2);
6147 if (REAL_VALUE_ISINF (c2))
6149 /* sqrt(x) < y is always true, when y is a very large
6150 value and we don't care about NaNs or Infinities. */
6151 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6152 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6154 /* sqrt(x) < y is x != +Inf when y is very large and we
6155 don't care about NaNs. */
6156 if (! HONOR_NANS (mode))
6157 return fold_build2_loc (loc, NE_EXPR, type, arg,
6158 build_real (TREE_TYPE (arg), c2));
6160 /* sqrt(x) < y is x >= 0 when y is very large and we
6161 don't care about Infinities. */
6162 if (! HONOR_INFINITIES (mode))
6163 return fold_build2_loc (loc, GE_EXPR, type, arg,
6164 build_real (TREE_TYPE (arg), dconst0));
6166 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6167 if (lang_hooks.decls.global_bindings_p () != 0
6168 || CONTAINS_PLACEHOLDER_P (arg))
6169 return NULL_TREE;
6171 arg = save_expr (arg);
6172 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6173 fold_build2_loc (loc, GE_EXPR, type, arg,
6174 build_real (TREE_TYPE (arg),
6175 dconst0)),
6176 fold_build2_loc (loc, NE_EXPR, type, arg,
6177 build_real (TREE_TYPE (arg),
6178 c2)));
6181 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6182 if (! HONOR_NANS (mode))
6183 return fold_build2_loc (loc, code, type, arg,
6184 build_real (TREE_TYPE (arg), c2));
6186 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6187 if (lang_hooks.decls.global_bindings_p () == 0
6188 && ! CONTAINS_PLACEHOLDER_P (arg))
6190 arg = save_expr (arg);
6191 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6192 fold_build2_loc (loc, GE_EXPR, type, arg,
6193 build_real (TREE_TYPE (arg),
6194 dconst0)),
6195 fold_build2_loc (loc, code, type, arg,
6196 build_real (TREE_TYPE (arg),
6197 c2)));
6202 return NULL_TREE;
6205 /* Subroutine of fold() that optimizes comparisons against Infinities,
6206 either +Inf or -Inf.
6208 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6209 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6210 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6212 The function returns the constant folded tree if a simplification
6213 can be made, and NULL_TREE otherwise. */
6215 static tree
6216 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6217 tree arg0, tree arg1)
6219 enum machine_mode mode;
6220 REAL_VALUE_TYPE max;
6221 tree temp;
6222 bool neg;
6224 mode = TYPE_MODE (TREE_TYPE (arg0));
6226 /* For negative infinity swap the sense of the comparison. */
6227 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6228 if (neg)
6229 code = swap_tree_comparison (code);
6231 switch (code)
6233 case GT_EXPR:
6234 /* x > +Inf is always false, if with ignore sNANs. */
6235 if (HONOR_SNANS (mode))
6236 return NULL_TREE;
6237 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6239 case LE_EXPR:
6240 /* x <= +Inf is always true, if we don't case about NaNs. */
6241 if (! HONOR_NANS (mode))
6242 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6244 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6245 if (lang_hooks.decls.global_bindings_p () == 0
6246 && ! CONTAINS_PLACEHOLDER_P (arg0))
6248 arg0 = save_expr (arg0);
6249 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6251 break;
6253 case EQ_EXPR:
6254 case GE_EXPR:
6255 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6256 real_maxval (&max, neg, mode);
6257 return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6258 arg0, build_real (TREE_TYPE (arg0), max));
6260 case LT_EXPR:
6261 /* x < +Inf is always equal to x <= DBL_MAX. */
6262 real_maxval (&max, neg, mode);
6263 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6264 arg0, build_real (TREE_TYPE (arg0), max));
6266 case NE_EXPR:
6267 /* x != +Inf is always equal to !(x > DBL_MAX). */
6268 real_maxval (&max, neg, mode);
6269 if (! HONOR_NANS (mode))
6270 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6271 arg0, build_real (TREE_TYPE (arg0), max));
6273 temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6274 arg0, build_real (TREE_TYPE (arg0), max));
6275 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
6277 default:
6278 break;
6281 return NULL_TREE;
6284 /* Subroutine of fold() that optimizes comparisons of a division by
6285 a nonzero integer constant against an integer constant, i.e.
6286 X/C1 op C2.
6288 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6289 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6290 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6292 The function returns the constant folded tree if a simplification
6293 can be made, and NULL_TREE otherwise. */
6295 static tree
6296 fold_div_compare (location_t loc,
6297 enum tree_code code, tree type, tree arg0, tree arg1)
6299 tree prod, tmp, hi, lo;
6300 tree arg00 = TREE_OPERAND (arg0, 0);
6301 tree arg01 = TREE_OPERAND (arg0, 1);
6302 double_int val;
6303 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6304 bool neg_overflow;
6305 int overflow;
6307 /* We have to do this the hard way to detect unsigned overflow.
6308 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
6309 overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6310 TREE_INT_CST_HIGH (arg01),
6311 TREE_INT_CST_LOW (arg1),
6312 TREE_INT_CST_HIGH (arg1),
6313 &val.low, &val.high, unsigned_p);
6314 prod = force_fit_type_double (TREE_TYPE (arg00), val, -1, overflow);
6315 neg_overflow = false;
6317 if (unsigned_p)
6319 tmp = int_const_binop (MINUS_EXPR, arg01,
6320 build_int_cst (TREE_TYPE (arg01), 1), 0);
6321 lo = prod;
6323 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
6324 overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6325 TREE_INT_CST_HIGH (prod),
6326 TREE_INT_CST_LOW (tmp),
6327 TREE_INT_CST_HIGH (tmp),
6328 &val.low, &val.high, unsigned_p);
6329 hi = force_fit_type_double (TREE_TYPE (arg00), val,
6330 -1, overflow | TREE_OVERFLOW (prod));
6332 else if (tree_int_cst_sgn (arg01) >= 0)
6334 tmp = int_const_binop (MINUS_EXPR, arg01,
6335 build_int_cst (TREE_TYPE (arg01), 1), 0);
6336 switch (tree_int_cst_sgn (arg1))
6338 case -1:
6339 neg_overflow = true;
6340 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6341 hi = prod;
6342 break;
6344 case 0:
6345 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6346 hi = tmp;
6347 break;
6349 case 1:
6350 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6351 lo = prod;
6352 break;
6354 default:
6355 gcc_unreachable ();
6358 else
6360 /* A negative divisor reverses the relational operators. */
6361 code = swap_tree_comparison (code);
6363 tmp = int_const_binop (PLUS_EXPR, arg01,
6364 build_int_cst (TREE_TYPE (arg01), 1), 0);
6365 switch (tree_int_cst_sgn (arg1))
6367 case -1:
6368 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6369 lo = prod;
6370 break;
6372 case 0:
6373 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6374 lo = tmp;
6375 break;
6377 case 1:
6378 neg_overflow = true;
6379 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6380 hi = prod;
6381 break;
6383 default:
6384 gcc_unreachable ();
6388 switch (code)
6390 case EQ_EXPR:
6391 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6392 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6393 if (TREE_OVERFLOW (hi))
6394 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6395 if (TREE_OVERFLOW (lo))
6396 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6397 return build_range_check (loc, type, arg00, 1, lo, hi);
6399 case NE_EXPR:
6400 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6401 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6402 if (TREE_OVERFLOW (hi))
6403 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6404 if (TREE_OVERFLOW (lo))
6405 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6406 return build_range_check (loc, type, arg00, 0, lo, hi);
6408 case LT_EXPR:
6409 if (TREE_OVERFLOW (lo))
6411 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6412 return omit_one_operand_loc (loc, type, tmp, arg00);
6414 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6416 case LE_EXPR:
6417 if (TREE_OVERFLOW (hi))
6419 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6420 return omit_one_operand_loc (loc, type, tmp, arg00);
6422 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6424 case GT_EXPR:
6425 if (TREE_OVERFLOW (hi))
6427 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6428 return omit_one_operand_loc (loc, type, tmp, arg00);
6430 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6432 case GE_EXPR:
6433 if (TREE_OVERFLOW (lo))
6435 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6436 return omit_one_operand_loc (loc, type, tmp, arg00);
6438 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6440 default:
6441 break;
6444 return NULL_TREE;
6448 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6449 equality/inequality test, then return a simplified form of the test
6450 using a sign testing. Otherwise return NULL. TYPE is the desired
6451 result type. */
6453 static tree
6454 fold_single_bit_test_into_sign_test (location_t loc,
6455 enum tree_code code, tree arg0, tree arg1,
6456 tree result_type)
6458 /* If this is testing a single bit, we can optimize the test. */
6459 if ((code == NE_EXPR || code == EQ_EXPR)
6460 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6461 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6463 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6464 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6465 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6467 if (arg00 != NULL_TREE
6468 /* This is only a win if casting to a signed type is cheap,
6469 i.e. when arg00's type is not a partial mode. */
6470 && TYPE_PRECISION (TREE_TYPE (arg00))
6471 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6473 tree stype = signed_type_for (TREE_TYPE (arg00));
6474 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6475 result_type,
6476 fold_convert_loc (loc, stype, arg00),
6477 build_int_cst (stype, 0));
6481 return NULL_TREE;
6484 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6485 equality/inequality test, then return a simplified form of
6486 the test using shifts and logical operations. Otherwise return
6487 NULL. TYPE is the desired result type. */
6489 tree
6490 fold_single_bit_test (location_t loc, enum tree_code code,
6491 tree arg0, tree arg1, tree result_type)
6493 /* If this is testing a single bit, we can optimize the test. */
6494 if ((code == NE_EXPR || code == EQ_EXPR)
6495 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6496 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6498 tree inner = TREE_OPERAND (arg0, 0);
6499 tree type = TREE_TYPE (arg0);
6500 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6501 enum machine_mode operand_mode = TYPE_MODE (type);
6502 int ops_unsigned;
6503 tree signed_type, unsigned_type, intermediate_type;
6504 tree tem, one;
6506 /* First, see if we can fold the single bit test into a sign-bit
6507 test. */
6508 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6509 result_type);
6510 if (tem)
6511 return tem;
6513 /* Otherwise we have (A & C) != 0 where C is a single bit,
6514 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6515 Similarly for (A & C) == 0. */
6517 /* If INNER is a right shift of a constant and it plus BITNUM does
6518 not overflow, adjust BITNUM and INNER. */
6519 if (TREE_CODE (inner) == RSHIFT_EXPR
6520 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6521 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6522 && bitnum < TYPE_PRECISION (type)
6523 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6524 bitnum - TYPE_PRECISION (type)))
6526 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6527 inner = TREE_OPERAND (inner, 0);
6530 /* If we are going to be able to omit the AND below, we must do our
6531 operations as unsigned. If we must use the AND, we have a choice.
6532 Normally unsigned is faster, but for some machines signed is. */
6533 #ifdef LOAD_EXTEND_OP
6534 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6535 && !flag_syntax_only) ? 0 : 1;
6536 #else
6537 ops_unsigned = 1;
6538 #endif
6540 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6541 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6542 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6543 inner = fold_convert_loc (loc, intermediate_type, inner);
6545 if (bitnum != 0)
6546 inner = build2 (RSHIFT_EXPR, intermediate_type,
6547 inner, size_int (bitnum));
6549 one = build_int_cst (intermediate_type, 1);
6551 if (code == EQ_EXPR)
6552 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6554 /* Put the AND last so it can combine with more things. */
6555 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6557 /* Make sure to return the proper type. */
6558 inner = fold_convert_loc (loc, result_type, inner);
6560 return inner;
6562 return NULL_TREE;
6565 /* Check whether we are allowed to reorder operands arg0 and arg1,
6566 such that the evaluation of arg1 occurs before arg0. */
6568 static bool
6569 reorder_operands_p (const_tree arg0, const_tree arg1)
6571 if (! flag_evaluation_order)
6572 return true;
6573 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6574 return true;
6575 return ! TREE_SIDE_EFFECTS (arg0)
6576 && ! TREE_SIDE_EFFECTS (arg1);
6579 /* Test whether it is preferable two swap two operands, ARG0 and
6580 ARG1, for example because ARG0 is an integer constant and ARG1
6581 isn't. If REORDER is true, only recommend swapping if we can
6582 evaluate the operands in reverse order. */
6584 bool
6585 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6587 STRIP_SIGN_NOPS (arg0);
6588 STRIP_SIGN_NOPS (arg1);
6590 if (TREE_CODE (arg1) == INTEGER_CST)
6591 return 0;
6592 if (TREE_CODE (arg0) == INTEGER_CST)
6593 return 1;
6595 if (TREE_CODE (arg1) == REAL_CST)
6596 return 0;
6597 if (TREE_CODE (arg0) == REAL_CST)
6598 return 1;
6600 if (TREE_CODE (arg1) == FIXED_CST)
6601 return 0;
6602 if (TREE_CODE (arg0) == FIXED_CST)
6603 return 1;
6605 if (TREE_CODE (arg1) == COMPLEX_CST)
6606 return 0;
6607 if (TREE_CODE (arg0) == COMPLEX_CST)
6608 return 1;
6610 if (TREE_CONSTANT (arg1))
6611 return 0;
6612 if (TREE_CONSTANT (arg0))
6613 return 1;
6615 if (optimize_function_for_size_p (cfun))
6616 return 0;
6618 if (reorder && flag_evaluation_order
6619 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6620 return 0;
6622 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6623 for commutative and comparison operators. Ensuring a canonical
6624 form allows the optimizers to find additional redundancies without
6625 having to explicitly check for both orderings. */
6626 if (TREE_CODE (arg0) == SSA_NAME
6627 && TREE_CODE (arg1) == SSA_NAME
6628 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6629 return 1;
6631 /* Put SSA_NAMEs last. */
6632 if (TREE_CODE (arg1) == SSA_NAME)
6633 return 0;
6634 if (TREE_CODE (arg0) == SSA_NAME)
6635 return 1;
6637 /* Put variables last. */
6638 if (DECL_P (arg1))
6639 return 0;
6640 if (DECL_P (arg0))
6641 return 1;
6643 return 0;
6646 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6647 ARG0 is extended to a wider type. */
6649 static tree
6650 fold_widened_comparison (location_t loc, enum tree_code code,
6651 tree type, tree arg0, tree arg1)
6653 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6654 tree arg1_unw;
6655 tree shorter_type, outer_type;
6656 tree min, max;
6657 bool above, below;
6659 if (arg0_unw == arg0)
6660 return NULL_TREE;
6661 shorter_type = TREE_TYPE (arg0_unw);
6663 #ifdef HAVE_canonicalize_funcptr_for_compare
6664 /* Disable this optimization if we're casting a function pointer
6665 type on targets that require function pointer canonicalization. */
6666 if (HAVE_canonicalize_funcptr_for_compare
6667 && TREE_CODE (shorter_type) == POINTER_TYPE
6668 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6669 return NULL_TREE;
6670 #endif
6672 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6673 return NULL_TREE;
6675 arg1_unw = get_unwidened (arg1, NULL_TREE);
6677 /* If possible, express the comparison in the shorter mode. */
6678 if ((code == EQ_EXPR || code == NE_EXPR
6679 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6680 && (TREE_TYPE (arg1_unw) == shorter_type
6681 || ((TYPE_PRECISION (shorter_type)
6682 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6683 && (TYPE_UNSIGNED (shorter_type)
6684 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6685 || (TREE_CODE (arg1_unw) == INTEGER_CST
6686 && (TREE_CODE (shorter_type) == INTEGER_TYPE
6687 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6688 && int_fits_type_p (arg1_unw, shorter_type))))
6689 return fold_build2_loc (loc, code, type, arg0_unw,
6690 fold_convert_loc (loc, shorter_type, arg1_unw));
6692 if (TREE_CODE (arg1_unw) != INTEGER_CST
6693 || TREE_CODE (shorter_type) != INTEGER_TYPE
6694 || !int_fits_type_p (arg1_unw, shorter_type))
6695 return NULL_TREE;
6697 /* If we are comparing with the integer that does not fit into the range
6698 of the shorter type, the result is known. */
6699 outer_type = TREE_TYPE (arg1_unw);
6700 min = lower_bound_in_type (outer_type, shorter_type);
6701 max = upper_bound_in_type (outer_type, shorter_type);
6703 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6704 max, arg1_unw));
6705 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6706 arg1_unw, min));
6708 switch (code)
6710 case EQ_EXPR:
6711 if (above || below)
6712 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6713 break;
6715 case NE_EXPR:
6716 if (above || below)
6717 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6718 break;
6720 case LT_EXPR:
6721 case LE_EXPR:
6722 if (above)
6723 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6724 else if (below)
6725 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6727 case GT_EXPR:
6728 case GE_EXPR:
6729 if (above)
6730 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6731 else if (below)
6732 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6734 default:
6735 break;
6738 return NULL_TREE;
6741 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6742 ARG0 just the signedness is changed. */
6744 static tree
6745 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
6746 tree arg0, tree arg1)
6748 tree arg0_inner;
6749 tree inner_type, outer_type;
6751 if (!CONVERT_EXPR_P (arg0))
6752 return NULL_TREE;
6754 outer_type = TREE_TYPE (arg0);
6755 arg0_inner = TREE_OPERAND (arg0, 0);
6756 inner_type = TREE_TYPE (arg0_inner);
6758 #ifdef HAVE_canonicalize_funcptr_for_compare
6759 /* Disable this optimization if we're casting a function pointer
6760 type on targets that require function pointer canonicalization. */
6761 if (HAVE_canonicalize_funcptr_for_compare
6762 && TREE_CODE (inner_type) == POINTER_TYPE
6763 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6764 return NULL_TREE;
6765 #endif
6767 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6768 return NULL_TREE;
6770 if (TREE_CODE (arg1) != INTEGER_CST
6771 && !(CONVERT_EXPR_P (arg1)
6772 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6773 return NULL_TREE;
6775 if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6776 || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6777 && code != NE_EXPR
6778 && code != EQ_EXPR)
6779 return NULL_TREE;
6781 if (TREE_CODE (arg1) == INTEGER_CST)
6782 arg1 = force_fit_type_double (inner_type, tree_to_double_int (arg1),
6783 0, TREE_OVERFLOW (arg1));
6784 else
6785 arg1 = fold_convert_loc (loc, inner_type, arg1);
6787 return fold_build2_loc (loc, code, type, arg0_inner, arg1);
6790 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6791 step of the array. Reconstructs s and delta in the case of s *
6792 delta being an integer constant (and thus already folded). ADDR is
6793 the address. MULT is the multiplicative expression. If the
6794 function succeeds, the new address expression is returned.
6795 Otherwise NULL_TREE is returned. LOC is the location of the
6796 resulting expression. */
6798 static tree
6799 try_move_mult_to_index (location_t loc, tree addr, tree op1)
6801 tree s, delta, step;
6802 tree ref = TREE_OPERAND (addr, 0), pref;
6803 tree ret, pos;
6804 tree itype;
6805 bool mdim = false;
6807 /* Strip the nops that might be added when converting op1 to sizetype. */
6808 STRIP_NOPS (op1);
6810 /* Canonicalize op1 into a possibly non-constant delta
6811 and an INTEGER_CST s. */
6812 if (TREE_CODE (op1) == MULT_EXPR)
6814 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6816 STRIP_NOPS (arg0);
6817 STRIP_NOPS (arg1);
6819 if (TREE_CODE (arg0) == INTEGER_CST)
6821 s = arg0;
6822 delta = arg1;
6824 else if (TREE_CODE (arg1) == INTEGER_CST)
6826 s = arg1;
6827 delta = arg0;
6829 else
6830 return NULL_TREE;
6832 else if (TREE_CODE (op1) == INTEGER_CST)
6834 delta = op1;
6835 s = NULL_TREE;
6837 else
6839 /* Simulate we are delta * 1. */
6840 delta = op1;
6841 s = integer_one_node;
6844 for (;; ref = TREE_OPERAND (ref, 0))
6846 if (TREE_CODE (ref) == ARRAY_REF)
6848 tree domain;
6850 /* Remember if this was a multi-dimensional array. */
6851 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6852 mdim = true;
6854 domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6855 if (! domain)
6856 continue;
6857 itype = TREE_TYPE (domain);
6859 step = array_ref_element_size (ref);
6860 if (TREE_CODE (step) != INTEGER_CST)
6861 continue;
6863 if (s)
6865 if (! tree_int_cst_equal (step, s))
6866 continue;
6868 else
6870 /* Try if delta is a multiple of step. */
6871 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6872 if (! tmp)
6873 continue;
6874 delta = tmp;
6877 /* Only fold here if we can verify we do not overflow one
6878 dimension of a multi-dimensional array. */
6879 if (mdim)
6881 tree tmp;
6883 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6884 || !TYPE_MAX_VALUE (domain)
6885 || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
6886 continue;
6888 tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
6889 fold_convert_loc (loc, itype,
6890 TREE_OPERAND (ref, 1)),
6891 fold_convert_loc (loc, itype, delta));
6892 if (!tmp
6893 || TREE_CODE (tmp) != INTEGER_CST
6894 || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
6895 continue;
6898 break;
6900 else
6901 mdim = false;
6903 if (!handled_component_p (ref))
6904 return NULL_TREE;
6907 /* We found the suitable array reference. So copy everything up to it,
6908 and replace the index. */
6910 pref = TREE_OPERAND (addr, 0);
6911 ret = copy_node (pref);
6912 SET_EXPR_LOCATION (ret, loc);
6913 pos = ret;
6915 while (pref != ref)
6917 pref = TREE_OPERAND (pref, 0);
6918 TREE_OPERAND (pos, 0) = copy_node (pref);
6919 pos = TREE_OPERAND (pos, 0);
6922 TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
6923 fold_convert_loc (loc, itype,
6924 TREE_OPERAND (pos, 1)),
6925 fold_convert_loc (loc, itype, delta));
6927 return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
6931 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6932 means A >= Y && A != MAX, but in this case we know that
6933 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6935 static tree
6936 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6938 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6940 if (TREE_CODE (bound) == LT_EXPR)
6941 a = TREE_OPERAND (bound, 0);
6942 else if (TREE_CODE (bound) == GT_EXPR)
6943 a = TREE_OPERAND (bound, 1);
6944 else
6945 return NULL_TREE;
6947 typea = TREE_TYPE (a);
6948 if (!INTEGRAL_TYPE_P (typea)
6949 && !POINTER_TYPE_P (typea))
6950 return NULL_TREE;
6952 if (TREE_CODE (ineq) == LT_EXPR)
6954 a1 = TREE_OPERAND (ineq, 1);
6955 y = TREE_OPERAND (ineq, 0);
6957 else if (TREE_CODE (ineq) == GT_EXPR)
6959 a1 = TREE_OPERAND (ineq, 0);
6960 y = TREE_OPERAND (ineq, 1);
6962 else
6963 return NULL_TREE;
6965 if (TREE_TYPE (a1) != typea)
6966 return NULL_TREE;
6968 if (POINTER_TYPE_P (typea))
6970 /* Convert the pointer types into integer before taking the difference. */
6971 tree ta = fold_convert_loc (loc, ssizetype, a);
6972 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6973 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6975 else
6976 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6978 if (!diff || !integer_onep (diff))
6979 return NULL_TREE;
6981 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6984 /* Fold a sum or difference of at least one multiplication.
6985 Returns the folded tree or NULL if no simplification could be made. */
6987 static tree
6988 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6989 tree arg0, tree arg1)
6991 tree arg00, arg01, arg10, arg11;
6992 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6994 /* (A * C) +- (B * C) -> (A+-B) * C.
6995 (A * C) +- A -> A * (C+-1).
6996 We are most concerned about the case where C is a constant,
6997 but other combinations show up during loop reduction. Since
6998 it is not difficult, try all four possibilities. */
7000 if (TREE_CODE (arg0) == MULT_EXPR)
7002 arg00 = TREE_OPERAND (arg0, 0);
7003 arg01 = TREE_OPERAND (arg0, 1);
7005 else if (TREE_CODE (arg0) == INTEGER_CST)
7007 arg00 = build_one_cst (type);
7008 arg01 = arg0;
7010 else
7012 /* We cannot generate constant 1 for fract. */
7013 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7014 return NULL_TREE;
7015 arg00 = arg0;
7016 arg01 = build_one_cst (type);
7018 if (TREE_CODE (arg1) == MULT_EXPR)
7020 arg10 = TREE_OPERAND (arg1, 0);
7021 arg11 = TREE_OPERAND (arg1, 1);
7023 else if (TREE_CODE (arg1) == INTEGER_CST)
7025 arg10 = build_one_cst (type);
7026 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7027 the purpose of this canonicalization. */
7028 if (TREE_INT_CST_HIGH (arg1) == -1
7029 && negate_expr_p (arg1)
7030 && code == PLUS_EXPR)
7032 arg11 = negate_expr (arg1);
7033 code = MINUS_EXPR;
7035 else
7036 arg11 = arg1;
7038 else
7040 /* We cannot generate constant 1 for fract. */
7041 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7042 return NULL_TREE;
7043 arg10 = arg1;
7044 arg11 = build_one_cst (type);
7046 same = NULL_TREE;
7048 if (operand_equal_p (arg01, arg11, 0))
7049 same = arg01, alt0 = arg00, alt1 = arg10;
7050 else if (operand_equal_p (arg00, arg10, 0))
7051 same = arg00, alt0 = arg01, alt1 = arg11;
7052 else if (operand_equal_p (arg00, arg11, 0))
7053 same = arg00, alt0 = arg01, alt1 = arg10;
7054 else if (operand_equal_p (arg01, arg10, 0))
7055 same = arg01, alt0 = arg00, alt1 = arg11;
7057 /* No identical multiplicands; see if we can find a common
7058 power-of-two factor in non-power-of-two multiplies. This
7059 can help in multi-dimensional array access. */
7060 else if (host_integerp (arg01, 0)
7061 && host_integerp (arg11, 0))
7063 HOST_WIDE_INT int01, int11, tmp;
7064 bool swap = false;
7065 tree maybe_same;
7066 int01 = TREE_INT_CST_LOW (arg01);
7067 int11 = TREE_INT_CST_LOW (arg11);
7069 /* Move min of absolute values to int11. */
7070 if ((int01 >= 0 ? int01 : -int01)
7071 < (int11 >= 0 ? int11 : -int11))
7073 tmp = int01, int01 = int11, int11 = tmp;
7074 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7075 maybe_same = arg01;
7076 swap = true;
7078 else
7079 maybe_same = arg11;
7081 if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7082 /* The remainder should not be a constant, otherwise we
7083 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7084 increased the number of multiplications necessary. */
7085 && TREE_CODE (arg10) != INTEGER_CST)
7087 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7088 build_int_cst (TREE_TYPE (arg00),
7089 int01 / int11));
7090 alt1 = arg10;
7091 same = maybe_same;
7092 if (swap)
7093 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7097 if (same)
7098 return fold_build2_loc (loc, MULT_EXPR, type,
7099 fold_build2_loc (loc, code, type,
7100 fold_convert_loc (loc, type, alt0),
7101 fold_convert_loc (loc, type, alt1)),
7102 fold_convert_loc (loc, type, same));
7104 return NULL_TREE;
7107 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7108 specified by EXPR into the buffer PTR of length LEN bytes.
7109 Return the number of bytes placed in the buffer, or zero
7110 upon failure. */
7112 static int
7113 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7115 tree type = TREE_TYPE (expr);
7116 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7117 int byte, offset, word, words;
7118 unsigned char value;
7120 if (total_bytes > len)
7121 return 0;
7122 words = total_bytes / UNITS_PER_WORD;
7124 for (byte = 0; byte < total_bytes; byte++)
7126 int bitpos = byte * BITS_PER_UNIT;
7127 if (bitpos < HOST_BITS_PER_WIDE_INT)
7128 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7129 else
7130 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7131 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7133 if (total_bytes > UNITS_PER_WORD)
7135 word = byte / UNITS_PER_WORD;
7136 if (WORDS_BIG_ENDIAN)
7137 word = (words - 1) - word;
7138 offset = word * UNITS_PER_WORD;
7139 if (BYTES_BIG_ENDIAN)
7140 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7141 else
7142 offset += byte % UNITS_PER_WORD;
7144 else
7145 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7146 ptr[offset] = value;
7148 return total_bytes;
7152 /* Subroutine of native_encode_expr. Encode the REAL_CST
7153 specified by EXPR into the buffer PTR of length LEN bytes.
7154 Return the number of bytes placed in the buffer, or zero
7155 upon failure. */
7157 static int
7158 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7160 tree type = TREE_TYPE (expr);
7161 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7162 int byte, offset, word, words, bitpos;
7163 unsigned char value;
7165 /* There are always 32 bits in each long, no matter the size of
7166 the hosts long. We handle floating point representations with
7167 up to 192 bits. */
7168 long tmp[6];
7170 if (total_bytes > len)
7171 return 0;
7172 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7174 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7176 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7177 bitpos += BITS_PER_UNIT)
7179 byte = (bitpos / BITS_PER_UNIT) & 3;
7180 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7182 if (UNITS_PER_WORD < 4)
7184 word = byte / UNITS_PER_WORD;
7185 if (WORDS_BIG_ENDIAN)
7186 word = (words - 1) - word;
7187 offset = word * UNITS_PER_WORD;
7188 if (BYTES_BIG_ENDIAN)
7189 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7190 else
7191 offset += byte % UNITS_PER_WORD;
7193 else
7194 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7195 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7197 return total_bytes;
7200 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7201 specified by EXPR into the buffer PTR of length LEN bytes.
7202 Return the number of bytes placed in the buffer, or zero
7203 upon failure. */
7205 static int
7206 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7208 int rsize, isize;
7209 tree part;
7211 part = TREE_REALPART (expr);
7212 rsize = native_encode_expr (part, ptr, len);
7213 if (rsize == 0)
7214 return 0;
7215 part = TREE_IMAGPART (expr);
7216 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7217 if (isize != rsize)
7218 return 0;
7219 return rsize + isize;
7223 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7224 specified by EXPR into the buffer PTR of length LEN bytes.
7225 Return the number of bytes placed in the buffer, or zero
7226 upon failure. */
7228 static int
7229 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7231 int i, size, offset, count;
7232 tree itype, elem, elements;
7234 offset = 0;
7235 elements = TREE_VECTOR_CST_ELTS (expr);
7236 count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7237 itype = TREE_TYPE (TREE_TYPE (expr));
7238 size = GET_MODE_SIZE (TYPE_MODE (itype));
7239 for (i = 0; i < count; i++)
7241 if (elements)
7243 elem = TREE_VALUE (elements);
7244 elements = TREE_CHAIN (elements);
7246 else
7247 elem = NULL_TREE;
7249 if (elem)
7251 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7252 return 0;
7254 else
7256 if (offset + size > len)
7257 return 0;
7258 memset (ptr+offset, 0, size);
7260 offset += size;
7262 return offset;
7266 /* Subroutine of native_encode_expr. Encode the STRING_CST
7267 specified by EXPR into the buffer PTR of length LEN bytes.
7268 Return the number of bytes placed in the buffer, or zero
7269 upon failure. */
7271 static int
7272 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7274 tree type = TREE_TYPE (expr);
7275 HOST_WIDE_INT total_bytes;
7277 if (TREE_CODE (type) != ARRAY_TYPE
7278 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7279 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7280 || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7281 return 0;
7282 total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7283 if (total_bytes > len)
7284 return 0;
7285 if (TREE_STRING_LENGTH (expr) < total_bytes)
7287 memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7288 memset (ptr + TREE_STRING_LENGTH (expr), 0,
7289 total_bytes - TREE_STRING_LENGTH (expr));
7291 else
7292 memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7293 return total_bytes;
7297 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7298 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7299 buffer PTR of length LEN bytes. Return the number of bytes
7300 placed in the buffer, or zero upon failure. */
7303 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7305 switch (TREE_CODE (expr))
7307 case INTEGER_CST:
7308 return native_encode_int (expr, ptr, len);
7310 case REAL_CST:
7311 return native_encode_real (expr, ptr, len);
7313 case COMPLEX_CST:
7314 return native_encode_complex (expr, ptr, len);
7316 case VECTOR_CST:
7317 return native_encode_vector (expr, ptr, len);
7319 case STRING_CST:
7320 return native_encode_string (expr, ptr, len);
7322 default:
7323 return 0;
7328 /* Subroutine of native_interpret_expr. Interpret the contents of
7329 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7330 If the buffer cannot be interpreted, return NULL_TREE. */
7332 static tree
7333 native_interpret_int (tree type, const unsigned char *ptr, int len)
7335 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7336 int byte, offset, word, words;
7337 unsigned char value;
7338 double_int result;
7340 if (total_bytes > len)
7341 return NULL_TREE;
7342 if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7343 return NULL_TREE;
7345 result = double_int_zero;
7346 words = total_bytes / UNITS_PER_WORD;
7348 for (byte = 0; byte < total_bytes; byte++)
7350 int bitpos = byte * BITS_PER_UNIT;
7351 if (total_bytes > UNITS_PER_WORD)
7353 word = byte / UNITS_PER_WORD;
7354 if (WORDS_BIG_ENDIAN)
7355 word = (words - 1) - word;
7356 offset = word * UNITS_PER_WORD;
7357 if (BYTES_BIG_ENDIAN)
7358 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7359 else
7360 offset += byte % UNITS_PER_WORD;
7362 else
7363 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7364 value = ptr[offset];
7366 if (bitpos < HOST_BITS_PER_WIDE_INT)
7367 result.low |= (unsigned HOST_WIDE_INT) value << bitpos;
7368 else
7369 result.high |= (unsigned HOST_WIDE_INT) value
7370 << (bitpos - HOST_BITS_PER_WIDE_INT);
7373 return double_int_to_tree (type, result);
7377 /* Subroutine of native_interpret_expr. Interpret the contents of
7378 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7379 If the buffer cannot be interpreted, return NULL_TREE. */
7381 static tree
7382 native_interpret_real (tree type, const unsigned char *ptr, int len)
7384 enum machine_mode mode = TYPE_MODE (type);
7385 int total_bytes = GET_MODE_SIZE (mode);
7386 int byte, offset, word, words, bitpos;
7387 unsigned char value;
7388 /* There are always 32 bits in each long, no matter the size of
7389 the hosts long. We handle floating point representations with
7390 up to 192 bits. */
7391 REAL_VALUE_TYPE r;
7392 long tmp[6];
7394 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7395 if (total_bytes > len || total_bytes > 24)
7396 return NULL_TREE;
7397 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7399 memset (tmp, 0, sizeof (tmp));
7400 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7401 bitpos += BITS_PER_UNIT)
7403 byte = (bitpos / BITS_PER_UNIT) & 3;
7404 if (UNITS_PER_WORD < 4)
7406 word = byte / UNITS_PER_WORD;
7407 if (WORDS_BIG_ENDIAN)
7408 word = (words - 1) - word;
7409 offset = word * UNITS_PER_WORD;
7410 if (BYTES_BIG_ENDIAN)
7411 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7412 else
7413 offset += byte % UNITS_PER_WORD;
7415 else
7416 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7417 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7419 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7422 real_from_target (&r, tmp, mode);
7423 return build_real (type, r);
7427 /* Subroutine of native_interpret_expr. Interpret the contents of
7428 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7429 If the buffer cannot be interpreted, return NULL_TREE. */
7431 static tree
7432 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7434 tree etype, rpart, ipart;
7435 int size;
7437 etype = TREE_TYPE (type);
7438 size = GET_MODE_SIZE (TYPE_MODE (etype));
7439 if (size * 2 > len)
7440 return NULL_TREE;
7441 rpart = native_interpret_expr (etype, ptr, size);
7442 if (!rpart)
7443 return NULL_TREE;
7444 ipart = native_interpret_expr (etype, ptr+size, size);
7445 if (!ipart)
7446 return NULL_TREE;
7447 return build_complex (type, rpart, ipart);
7451 /* Subroutine of native_interpret_expr. Interpret the contents of
7452 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7453 If the buffer cannot be interpreted, return NULL_TREE. */
7455 static tree
7456 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7458 tree etype, elem, elements;
7459 int i, size, count;
7461 etype = TREE_TYPE (type);
7462 size = GET_MODE_SIZE (TYPE_MODE (etype));
7463 count = TYPE_VECTOR_SUBPARTS (type);
7464 if (size * count > len)
7465 return NULL_TREE;
7467 elements = NULL_TREE;
7468 for (i = count - 1; i >= 0; i--)
7470 elem = native_interpret_expr (etype, ptr+(i*size), size);
7471 if (!elem)
7472 return NULL_TREE;
7473 elements = tree_cons (NULL_TREE, elem, elements);
7475 return build_vector (type, elements);
7479 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7480 the buffer PTR of length LEN as a constant of type TYPE. For
7481 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7482 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7483 return NULL_TREE. */
7485 tree
7486 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7488 switch (TREE_CODE (type))
7490 case INTEGER_TYPE:
7491 case ENUMERAL_TYPE:
7492 case BOOLEAN_TYPE:
7493 return native_interpret_int (type, ptr, len);
7495 case REAL_TYPE:
7496 return native_interpret_real (type, ptr, len);
7498 case COMPLEX_TYPE:
7499 return native_interpret_complex (type, ptr, len);
7501 case VECTOR_TYPE:
7502 return native_interpret_vector (type, ptr, len);
7504 default:
7505 return NULL_TREE;
7510 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7511 TYPE at compile-time. If we're unable to perform the conversion
7512 return NULL_TREE. */
7514 static tree
7515 fold_view_convert_expr (tree type, tree expr)
7517 /* We support up to 512-bit values (for V8DFmode). */
7518 unsigned char buffer[64];
7519 int len;
7521 /* Check that the host and target are sane. */
7522 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7523 return NULL_TREE;
7525 len = native_encode_expr (expr, buffer, sizeof (buffer));
7526 if (len == 0)
7527 return NULL_TREE;
7529 return native_interpret_expr (type, buffer, len);
7532 /* Build an expression for the address of T. Folds away INDIRECT_REF
7533 to avoid confusing the gimplify process. */
7535 tree
7536 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7538 /* The size of the object is not relevant when talking about its address. */
7539 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7540 t = TREE_OPERAND (t, 0);
7542 if (TREE_CODE (t) == INDIRECT_REF)
7544 t = TREE_OPERAND (t, 0);
7546 if (TREE_TYPE (t) != ptrtype)
7547 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7549 else if (TREE_CODE (t) == MEM_REF
7550 && integer_zerop (TREE_OPERAND (t, 1)))
7551 return TREE_OPERAND (t, 0);
7552 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7554 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7556 if (TREE_TYPE (t) != ptrtype)
7557 t = fold_convert_loc (loc, ptrtype, t);
7559 else
7560 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7562 return t;
7565 /* Build an expression for the address of T. */
7567 tree
7568 build_fold_addr_expr_loc (location_t loc, tree t)
7570 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7572 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7575 /* Fold a unary expression of code CODE and type TYPE with operand
7576 OP0. Return the folded expression if folding is successful.
7577 Otherwise, return NULL_TREE. */
7579 tree
7580 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7582 tree tem;
7583 tree arg0;
7584 enum tree_code_class kind = TREE_CODE_CLASS (code);
7586 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7587 && TREE_CODE_LENGTH (code) == 1);
7589 arg0 = op0;
7590 if (arg0)
7592 if (CONVERT_EXPR_CODE_P (code)
7593 || code == FLOAT_EXPR || code == ABS_EXPR)
7595 /* Don't use STRIP_NOPS, because signedness of argument type
7596 matters. */
7597 STRIP_SIGN_NOPS (arg0);
7599 else
7601 /* Strip any conversions that don't change the mode. This
7602 is safe for every expression, except for a comparison
7603 expression because its signedness is derived from its
7604 operands.
7606 Note that this is done as an internal manipulation within
7607 the constant folder, in order to find the simplest
7608 representation of the arguments so that their form can be
7609 studied. In any cases, the appropriate type conversions
7610 should be put back in the tree that will get out of the
7611 constant folder. */
7612 STRIP_NOPS (arg0);
7616 if (TREE_CODE_CLASS (code) == tcc_unary)
7618 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7619 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7620 fold_build1_loc (loc, code, type,
7621 fold_convert_loc (loc, TREE_TYPE (op0),
7622 TREE_OPERAND (arg0, 1))));
7623 else if (TREE_CODE (arg0) == COND_EXPR)
7625 tree arg01 = TREE_OPERAND (arg0, 1);
7626 tree arg02 = TREE_OPERAND (arg0, 2);
7627 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7628 arg01 = fold_build1_loc (loc, code, type,
7629 fold_convert_loc (loc,
7630 TREE_TYPE (op0), arg01));
7631 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7632 arg02 = fold_build1_loc (loc, code, type,
7633 fold_convert_loc (loc,
7634 TREE_TYPE (op0), arg02));
7635 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7636 arg01, arg02);
7638 /* If this was a conversion, and all we did was to move into
7639 inside the COND_EXPR, bring it back out. But leave it if
7640 it is a conversion from integer to integer and the
7641 result precision is no wider than a word since such a
7642 conversion is cheap and may be optimized away by combine,
7643 while it couldn't if it were outside the COND_EXPR. Then return
7644 so we don't get into an infinite recursion loop taking the
7645 conversion out and then back in. */
7647 if ((CONVERT_EXPR_CODE_P (code)
7648 || code == NON_LVALUE_EXPR)
7649 && TREE_CODE (tem) == COND_EXPR
7650 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7651 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7652 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7653 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7654 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7655 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7656 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7657 && (INTEGRAL_TYPE_P
7658 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7659 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7660 || flag_syntax_only))
7661 tem = build1_loc (loc, code, type,
7662 build3 (COND_EXPR,
7663 TREE_TYPE (TREE_OPERAND
7664 (TREE_OPERAND (tem, 1), 0)),
7665 TREE_OPERAND (tem, 0),
7666 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7667 TREE_OPERAND (TREE_OPERAND (tem, 2),
7668 0)));
7669 return tem;
7671 else if (COMPARISON_CLASS_P (arg0))
7673 if (TREE_CODE (type) == BOOLEAN_TYPE)
7675 arg0 = copy_node (arg0);
7676 TREE_TYPE (arg0) = type;
7677 return arg0;
7679 else if (TREE_CODE (type) != INTEGER_TYPE)
7680 return fold_build3_loc (loc, COND_EXPR, type, arg0,
7681 fold_build1_loc (loc, code, type,
7682 integer_one_node),
7683 fold_build1_loc (loc, code, type,
7684 integer_zero_node));
7688 switch (code)
7690 case PAREN_EXPR:
7691 /* Re-association barriers around constants and other re-association
7692 barriers can be removed. */
7693 if (CONSTANT_CLASS_P (op0)
7694 || TREE_CODE (op0) == PAREN_EXPR)
7695 return fold_convert_loc (loc, type, op0);
7696 return NULL_TREE;
7698 CASE_CONVERT:
7699 case FLOAT_EXPR:
7700 case FIX_TRUNC_EXPR:
7701 if (TREE_TYPE (op0) == type)
7702 return op0;
7704 /* If we have (type) (a CMP b) and type is an integral type, return
7705 new expression involving the new type. */
7706 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7707 return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7708 TREE_OPERAND (op0, 1));
7710 /* Handle cases of two conversions in a row. */
7711 if (CONVERT_EXPR_P (op0))
7713 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7714 tree inter_type = TREE_TYPE (op0);
7715 int inside_int = INTEGRAL_TYPE_P (inside_type);
7716 int inside_ptr = POINTER_TYPE_P (inside_type);
7717 int inside_float = FLOAT_TYPE_P (inside_type);
7718 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7719 unsigned int inside_prec = TYPE_PRECISION (inside_type);
7720 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7721 int inter_int = INTEGRAL_TYPE_P (inter_type);
7722 int inter_ptr = POINTER_TYPE_P (inter_type);
7723 int inter_float = FLOAT_TYPE_P (inter_type);
7724 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7725 unsigned int inter_prec = TYPE_PRECISION (inter_type);
7726 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7727 int final_int = INTEGRAL_TYPE_P (type);
7728 int final_ptr = POINTER_TYPE_P (type);
7729 int final_float = FLOAT_TYPE_P (type);
7730 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7731 unsigned int final_prec = TYPE_PRECISION (type);
7732 int final_unsignedp = TYPE_UNSIGNED (type);
7734 /* In addition to the cases of two conversions in a row
7735 handled below, if we are converting something to its own
7736 type via an object of identical or wider precision, neither
7737 conversion is needed. */
7738 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7739 && (((inter_int || inter_ptr) && final_int)
7740 || (inter_float && final_float))
7741 && inter_prec >= final_prec)
7742 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7744 /* Likewise, if the intermediate and initial types are either both
7745 float or both integer, we don't need the middle conversion if the
7746 former is wider than the latter and doesn't change the signedness
7747 (for integers). Avoid this if the final type is a pointer since
7748 then we sometimes need the middle conversion. Likewise if the
7749 final type has a precision not equal to the size of its mode. */
7750 if (((inter_int && inside_int)
7751 || (inter_float && inside_float)
7752 || (inter_vec && inside_vec))
7753 && inter_prec >= inside_prec
7754 && (inter_float || inter_vec
7755 || inter_unsignedp == inside_unsignedp)
7756 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7757 && TYPE_MODE (type) == TYPE_MODE (inter_type))
7758 && ! final_ptr
7759 && (! final_vec || inter_prec == inside_prec))
7760 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7762 /* If we have a sign-extension of a zero-extended value, we can
7763 replace that by a single zero-extension. */
7764 if (inside_int && inter_int && final_int
7765 && inside_prec < inter_prec && inter_prec < final_prec
7766 && inside_unsignedp && !inter_unsignedp)
7767 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7769 /* Two conversions in a row are not needed unless:
7770 - some conversion is floating-point (overstrict for now), or
7771 - some conversion is a vector (overstrict for now), or
7772 - the intermediate type is narrower than both initial and
7773 final, or
7774 - the intermediate type and innermost type differ in signedness,
7775 and the outermost type is wider than the intermediate, or
7776 - the initial type is a pointer type and the precisions of the
7777 intermediate and final types differ, or
7778 - the final type is a pointer type and the precisions of the
7779 initial and intermediate types differ. */
7780 if (! inside_float && ! inter_float && ! final_float
7781 && ! inside_vec && ! inter_vec && ! final_vec
7782 && (inter_prec >= inside_prec || inter_prec >= final_prec)
7783 && ! (inside_int && inter_int
7784 && inter_unsignedp != inside_unsignedp
7785 && inter_prec < final_prec)
7786 && ((inter_unsignedp && inter_prec > inside_prec)
7787 == (final_unsignedp && final_prec > inter_prec))
7788 && ! (inside_ptr && inter_prec != final_prec)
7789 && ! (final_ptr && inside_prec != inter_prec)
7790 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7791 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7792 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
7795 /* Handle (T *)&A.B.C for A being of type T and B and C
7796 living at offset zero. This occurs frequently in
7797 C++ upcasting and then accessing the base. */
7798 if (TREE_CODE (op0) == ADDR_EXPR
7799 && POINTER_TYPE_P (type)
7800 && handled_component_p (TREE_OPERAND (op0, 0)))
7802 HOST_WIDE_INT bitsize, bitpos;
7803 tree offset;
7804 enum machine_mode mode;
7805 int unsignedp, volatilep;
7806 tree base = TREE_OPERAND (op0, 0);
7807 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7808 &mode, &unsignedp, &volatilep, false);
7809 /* If the reference was to a (constant) zero offset, we can use
7810 the address of the base if it has the same base type
7811 as the result type and the pointer type is unqualified. */
7812 if (! offset && bitpos == 0
7813 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7814 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7815 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7816 return fold_convert_loc (loc, type,
7817 build_fold_addr_expr_loc (loc, base));
7820 if (TREE_CODE (op0) == MODIFY_EXPR
7821 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7822 /* Detect assigning a bitfield. */
7823 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7824 && DECL_BIT_FIELD
7825 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7827 /* Don't leave an assignment inside a conversion
7828 unless assigning a bitfield. */
7829 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7830 /* First do the assignment, then return converted constant. */
7831 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7832 TREE_NO_WARNING (tem) = 1;
7833 TREE_USED (tem) = 1;
7834 return tem;
7837 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7838 constants (if x has signed type, the sign bit cannot be set
7839 in c). This folds extension into the BIT_AND_EXPR.
7840 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7841 very likely don't have maximal range for their precision and this
7842 transformation effectively doesn't preserve non-maximal ranges. */
7843 if (TREE_CODE (type) == INTEGER_TYPE
7844 && TREE_CODE (op0) == BIT_AND_EXPR
7845 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7847 tree and_expr = op0;
7848 tree and0 = TREE_OPERAND (and_expr, 0);
7849 tree and1 = TREE_OPERAND (and_expr, 1);
7850 int change = 0;
7852 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7853 || (TYPE_PRECISION (type)
7854 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7855 change = 1;
7856 else if (TYPE_PRECISION (TREE_TYPE (and1))
7857 <= HOST_BITS_PER_WIDE_INT
7858 && host_integerp (and1, 1))
7860 unsigned HOST_WIDE_INT cst;
7862 cst = tree_low_cst (and1, 1);
7863 cst &= (HOST_WIDE_INT) -1
7864 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7865 change = (cst == 0);
7866 #ifdef LOAD_EXTEND_OP
7867 if (change
7868 && !flag_syntax_only
7869 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7870 == ZERO_EXTEND))
7872 tree uns = unsigned_type_for (TREE_TYPE (and0));
7873 and0 = fold_convert_loc (loc, uns, and0);
7874 and1 = fold_convert_loc (loc, uns, and1);
7876 #endif
7878 if (change)
7880 tem = force_fit_type_double (type, tree_to_double_int (and1),
7881 0, TREE_OVERFLOW (and1));
7882 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7883 fold_convert_loc (loc, type, and0), tem);
7887 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7888 when one of the new casts will fold away. Conservatively we assume
7889 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7890 if (POINTER_TYPE_P (type)
7891 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7892 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7893 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7894 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7896 tree arg00 = TREE_OPERAND (arg0, 0);
7897 tree arg01 = TREE_OPERAND (arg0, 1);
7899 return fold_build2_loc (loc,
7900 TREE_CODE (arg0), type,
7901 fold_convert_loc (loc, type, arg00),
7902 fold_convert_loc (loc, sizetype, arg01));
7905 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7906 of the same precision, and X is an integer type not narrower than
7907 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7908 if (INTEGRAL_TYPE_P (type)
7909 && TREE_CODE (op0) == BIT_NOT_EXPR
7910 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7911 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7912 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7914 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7915 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7916 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7917 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7918 fold_convert_loc (loc, type, tem));
7921 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7922 type of X and Y (integer types only). */
7923 if (INTEGRAL_TYPE_P (type)
7924 && TREE_CODE (op0) == MULT_EXPR
7925 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7926 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7928 /* Be careful not to introduce new overflows. */
7929 tree mult_type;
7930 if (TYPE_OVERFLOW_WRAPS (type))
7931 mult_type = type;
7932 else
7933 mult_type = unsigned_type_for (type);
7935 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7937 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7938 fold_convert_loc (loc, mult_type,
7939 TREE_OPERAND (op0, 0)),
7940 fold_convert_loc (loc, mult_type,
7941 TREE_OPERAND (op0, 1)));
7942 return fold_convert_loc (loc, type, tem);
7946 tem = fold_convert_const (code, type, op0);
7947 return tem ? tem : NULL_TREE;
7949 case ADDR_SPACE_CONVERT_EXPR:
7950 if (integer_zerop (arg0))
7951 return fold_convert_const (code, type, arg0);
7952 return NULL_TREE;
7954 case FIXED_CONVERT_EXPR:
7955 tem = fold_convert_const (code, type, arg0);
7956 return tem ? tem : NULL_TREE;
7958 case VIEW_CONVERT_EXPR:
7959 if (TREE_TYPE (op0) == type)
7960 return op0;
7961 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7962 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
7963 type, TREE_OPERAND (op0, 0));
7964 if (TREE_CODE (op0) == MEM_REF)
7965 return fold_build2_loc (loc, MEM_REF, type,
7966 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7968 /* For integral conversions with the same precision or pointer
7969 conversions use a NOP_EXPR instead. */
7970 if ((INTEGRAL_TYPE_P (type)
7971 || POINTER_TYPE_P (type))
7972 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7973 || POINTER_TYPE_P (TREE_TYPE (op0)))
7974 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7975 return fold_convert_loc (loc, type, op0);
7977 /* Strip inner integral conversions that do not change the precision. */
7978 if (CONVERT_EXPR_P (op0)
7979 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7980 || POINTER_TYPE_P (TREE_TYPE (op0)))
7981 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
7982 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
7983 && (TYPE_PRECISION (TREE_TYPE (op0))
7984 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
7985 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
7986 type, TREE_OPERAND (op0, 0));
7988 return fold_view_convert_expr (type, op0);
7990 case NEGATE_EXPR:
7991 tem = fold_negate_expr (loc, arg0);
7992 if (tem)
7993 return fold_convert_loc (loc, type, tem);
7994 return NULL_TREE;
7996 case ABS_EXPR:
7997 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
7998 return fold_abs_const (arg0, type);
7999 else if (TREE_CODE (arg0) == NEGATE_EXPR)
8000 return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8001 /* Convert fabs((double)float) into (double)fabsf(float). */
8002 else if (TREE_CODE (arg0) == NOP_EXPR
8003 && TREE_CODE (type) == REAL_TYPE)
8005 tree targ0 = strip_float_extensions (arg0);
8006 if (targ0 != arg0)
8007 return fold_convert_loc (loc, type,
8008 fold_build1_loc (loc, ABS_EXPR,
8009 TREE_TYPE (targ0),
8010 targ0));
8012 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8013 else if (TREE_CODE (arg0) == ABS_EXPR)
8014 return arg0;
8015 else if (tree_expr_nonnegative_p (arg0))
8016 return arg0;
8018 /* Strip sign ops from argument. */
8019 if (TREE_CODE (type) == REAL_TYPE)
8021 tem = fold_strip_sign_ops (arg0);
8022 if (tem)
8023 return fold_build1_loc (loc, ABS_EXPR, type,
8024 fold_convert_loc (loc, type, tem));
8026 return NULL_TREE;
8028 case CONJ_EXPR:
8029 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8030 return fold_convert_loc (loc, type, arg0);
8031 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8033 tree itype = TREE_TYPE (type);
8034 tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8035 tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8036 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8037 negate_expr (ipart));
8039 if (TREE_CODE (arg0) == COMPLEX_CST)
8041 tree itype = TREE_TYPE (type);
8042 tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8043 tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8044 return build_complex (type, rpart, negate_expr (ipart));
8046 if (TREE_CODE (arg0) == CONJ_EXPR)
8047 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8048 return NULL_TREE;
8050 case BIT_NOT_EXPR:
8051 if (TREE_CODE (arg0) == INTEGER_CST)
8052 return fold_not_const (arg0, type);
8053 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8054 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8055 /* Convert ~ (-A) to A - 1. */
8056 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8057 return fold_build2_loc (loc, MINUS_EXPR, type,
8058 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8059 build_int_cst (type, 1));
8060 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8061 else if (INTEGRAL_TYPE_P (type)
8062 && ((TREE_CODE (arg0) == MINUS_EXPR
8063 && integer_onep (TREE_OPERAND (arg0, 1)))
8064 || (TREE_CODE (arg0) == PLUS_EXPR
8065 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8066 return fold_build1_loc (loc, NEGATE_EXPR, type,
8067 fold_convert_loc (loc, type,
8068 TREE_OPERAND (arg0, 0)));
8069 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8070 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8071 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8072 fold_convert_loc (loc, type,
8073 TREE_OPERAND (arg0, 0)))))
8074 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8075 fold_convert_loc (loc, type,
8076 TREE_OPERAND (arg0, 1)));
8077 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8078 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8079 fold_convert_loc (loc, type,
8080 TREE_OPERAND (arg0, 1)))))
8081 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8082 fold_convert_loc (loc, type,
8083 TREE_OPERAND (arg0, 0)), tem);
8084 /* Perform BIT_NOT_EXPR on each element individually. */
8085 else if (TREE_CODE (arg0) == VECTOR_CST)
8087 tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8088 int count = TYPE_VECTOR_SUBPARTS (type), i;
8090 for (i = 0; i < count; i++)
8092 if (elements)
8094 elem = TREE_VALUE (elements);
8095 elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8096 if (elem == NULL_TREE)
8097 break;
8098 elements = TREE_CHAIN (elements);
8100 else
8101 elem = build_int_cst (TREE_TYPE (type), -1);
8102 list = tree_cons (NULL_TREE, elem, list);
8104 if (i == count)
8105 return build_vector (type, nreverse (list));
8108 return NULL_TREE;
8110 case TRUTH_NOT_EXPR:
8111 /* The argument to invert_truthvalue must have Boolean type. */
8112 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8113 arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8115 /* Note that the operand of this must be an int
8116 and its values must be 0 or 1.
8117 ("true" is a fixed value perhaps depending on the language,
8118 but we don't handle values other than 1 correctly yet.) */
8119 tem = fold_truth_not_expr (loc, arg0);
8120 if (!tem)
8121 return NULL_TREE;
8122 return fold_convert_loc (loc, type, tem);
8124 case REALPART_EXPR:
8125 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8126 return fold_convert_loc (loc, type, arg0);
8127 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8128 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8129 TREE_OPERAND (arg0, 1));
8130 if (TREE_CODE (arg0) == COMPLEX_CST)
8131 return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8132 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8134 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8135 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8136 fold_build1_loc (loc, REALPART_EXPR, itype,
8137 TREE_OPERAND (arg0, 0)),
8138 fold_build1_loc (loc, REALPART_EXPR, itype,
8139 TREE_OPERAND (arg0, 1)));
8140 return fold_convert_loc (loc, type, tem);
8142 if (TREE_CODE (arg0) == CONJ_EXPR)
8144 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8145 tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8146 TREE_OPERAND (arg0, 0));
8147 return fold_convert_loc (loc, type, tem);
8149 if (TREE_CODE (arg0) == CALL_EXPR)
8151 tree fn = get_callee_fndecl (arg0);
8152 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8153 switch (DECL_FUNCTION_CODE (fn))
8155 CASE_FLT_FN (BUILT_IN_CEXPI):
8156 fn = mathfn_built_in (type, BUILT_IN_COS);
8157 if (fn)
8158 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8159 break;
8161 default:
8162 break;
8165 return NULL_TREE;
8167 case IMAGPART_EXPR:
8168 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8169 return build_zero_cst (type);
8170 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8171 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8172 TREE_OPERAND (arg0, 0));
8173 if (TREE_CODE (arg0) == COMPLEX_CST)
8174 return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8175 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8177 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8178 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8179 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8180 TREE_OPERAND (arg0, 0)),
8181 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8182 TREE_OPERAND (arg0, 1)));
8183 return fold_convert_loc (loc, type, tem);
8185 if (TREE_CODE (arg0) == CONJ_EXPR)
8187 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8188 tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8189 return fold_convert_loc (loc, type, negate_expr (tem));
8191 if (TREE_CODE (arg0) == CALL_EXPR)
8193 tree fn = get_callee_fndecl (arg0);
8194 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8195 switch (DECL_FUNCTION_CODE (fn))
8197 CASE_FLT_FN (BUILT_IN_CEXPI):
8198 fn = mathfn_built_in (type, BUILT_IN_SIN);
8199 if (fn)
8200 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8201 break;
8203 default:
8204 break;
8207 return NULL_TREE;
8209 case INDIRECT_REF:
8210 /* Fold *&X to X if X is an lvalue. */
8211 if (TREE_CODE (op0) == ADDR_EXPR)
8213 tree op00 = TREE_OPERAND (op0, 0);
8214 if ((TREE_CODE (op00) == VAR_DECL
8215 || TREE_CODE (op00) == PARM_DECL
8216 || TREE_CODE (op00) == RESULT_DECL)
8217 && !TREE_READONLY (op00))
8218 return op00;
8220 return NULL_TREE;
8222 default:
8223 return NULL_TREE;
8224 } /* switch (code) */
8228 /* If the operation was a conversion do _not_ mark a resulting constant
8229 with TREE_OVERFLOW if the original constant was not. These conversions
8230 have implementation defined behavior and retaining the TREE_OVERFLOW
8231 flag here would confuse later passes such as VRP. */
8232 tree
8233 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8234 tree type, tree op0)
8236 tree res = fold_unary_loc (loc, code, type, op0);
8237 if (res
8238 && TREE_CODE (res) == INTEGER_CST
8239 && TREE_CODE (op0) == INTEGER_CST
8240 && CONVERT_EXPR_CODE_P (code))
8241 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8243 return res;
8246 /* Fold a binary expression of code CODE and type TYPE with operands
8247 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8248 Return the folded expression if folding is successful. Otherwise,
8249 return NULL_TREE. */
8251 static tree
8252 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8254 enum tree_code compl_code;
8256 if (code == MIN_EXPR)
8257 compl_code = MAX_EXPR;
8258 else if (code == MAX_EXPR)
8259 compl_code = MIN_EXPR;
8260 else
8261 gcc_unreachable ();
8263 /* MIN (MAX (a, b), b) == b. */
8264 if (TREE_CODE (op0) == compl_code
8265 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8266 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8268 /* MIN (MAX (b, a), b) == b. */
8269 if (TREE_CODE (op0) == compl_code
8270 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8271 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8272 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8274 /* MIN (a, MAX (a, b)) == a. */
8275 if (TREE_CODE (op1) == compl_code
8276 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8277 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8278 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8280 /* MIN (a, MAX (b, a)) == a. */
8281 if (TREE_CODE (op1) == compl_code
8282 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8283 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8284 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8286 return NULL_TREE;
8289 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8290 by changing CODE to reduce the magnitude of constants involved in
8291 ARG0 of the comparison.
8292 Returns a canonicalized comparison tree if a simplification was
8293 possible, otherwise returns NULL_TREE.
8294 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8295 valid if signed overflow is undefined. */
8297 static tree
8298 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8299 tree arg0, tree arg1,
8300 bool *strict_overflow_p)
8302 enum tree_code code0 = TREE_CODE (arg0);
8303 tree t, cst0 = NULL_TREE;
8304 int sgn0;
8305 bool swap = false;
8307 /* Match A +- CST code arg1 and CST code arg1. We can change the
8308 first form only if overflow is undefined. */
8309 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8310 /* In principle pointers also have undefined overflow behavior,
8311 but that causes problems elsewhere. */
8312 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8313 && (code0 == MINUS_EXPR
8314 || code0 == PLUS_EXPR)
8315 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8316 || code0 == INTEGER_CST))
8317 return NULL_TREE;
8319 /* Identify the constant in arg0 and its sign. */
8320 if (code0 == INTEGER_CST)
8321 cst0 = arg0;
8322 else
8323 cst0 = TREE_OPERAND (arg0, 1);
8324 sgn0 = tree_int_cst_sgn (cst0);
8326 /* Overflowed constants and zero will cause problems. */
8327 if (integer_zerop (cst0)
8328 || TREE_OVERFLOW (cst0))
8329 return NULL_TREE;
8331 /* See if we can reduce the magnitude of the constant in
8332 arg0 by changing the comparison code. */
8333 if (code0 == INTEGER_CST)
8335 /* CST <= arg1 -> CST-1 < arg1. */
8336 if (code == LE_EXPR && sgn0 == 1)
8337 code = LT_EXPR;
8338 /* -CST < arg1 -> -CST-1 <= arg1. */
8339 else if (code == LT_EXPR && sgn0 == -1)
8340 code = LE_EXPR;
8341 /* CST > arg1 -> CST-1 >= arg1. */
8342 else if (code == GT_EXPR && sgn0 == 1)
8343 code = GE_EXPR;
8344 /* -CST >= arg1 -> -CST-1 > arg1. */
8345 else if (code == GE_EXPR && sgn0 == -1)
8346 code = GT_EXPR;
8347 else
8348 return NULL_TREE;
8349 /* arg1 code' CST' might be more canonical. */
8350 swap = true;
8352 else
8354 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8355 if (code == LT_EXPR
8356 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8357 code = LE_EXPR;
8358 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8359 else if (code == GT_EXPR
8360 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8361 code = GE_EXPR;
8362 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8363 else if (code == LE_EXPR
8364 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8365 code = LT_EXPR;
8366 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8367 else if (code == GE_EXPR
8368 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8369 code = GT_EXPR;
8370 else
8371 return NULL_TREE;
8372 *strict_overflow_p = true;
8375 /* Now build the constant reduced in magnitude. But not if that
8376 would produce one outside of its types range. */
8377 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8378 && ((sgn0 == 1
8379 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8380 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8381 || (sgn0 == -1
8382 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8383 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8384 /* We cannot swap the comparison here as that would cause us to
8385 endlessly recurse. */
8386 return NULL_TREE;
8388 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8389 cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8390 if (code0 != INTEGER_CST)
8391 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8393 /* If swapping might yield to a more canonical form, do so. */
8394 if (swap)
8395 return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
8396 else
8397 return fold_build2_loc (loc, code, type, t, arg1);
8400 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8401 overflow further. Try to decrease the magnitude of constants involved
8402 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8403 and put sole constants at the second argument position.
8404 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8406 static tree
8407 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8408 tree arg0, tree arg1)
8410 tree t;
8411 bool strict_overflow_p;
8412 const char * const warnmsg = G_("assuming signed overflow does not occur "
8413 "when reducing constant in comparison");
8415 /* Try canonicalization by simplifying arg0. */
8416 strict_overflow_p = false;
8417 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8418 &strict_overflow_p);
8419 if (t)
8421 if (strict_overflow_p)
8422 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8423 return t;
8426 /* Try canonicalization by simplifying arg1 using the swapped
8427 comparison. */
8428 code = swap_tree_comparison (code);
8429 strict_overflow_p = false;
8430 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8431 &strict_overflow_p);
8432 if (t && strict_overflow_p)
8433 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8434 return t;
8437 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8438 space. This is used to avoid issuing overflow warnings for
8439 expressions like &p->x which can not wrap. */
8441 static bool
8442 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8444 unsigned HOST_WIDE_INT offset_low, total_low;
8445 HOST_WIDE_INT size, offset_high, total_high;
8447 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8448 return true;
8450 if (bitpos < 0)
8451 return true;
8453 if (offset == NULL_TREE)
8455 offset_low = 0;
8456 offset_high = 0;
8458 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8459 return true;
8460 else
8462 offset_low = TREE_INT_CST_LOW (offset);
8463 offset_high = TREE_INT_CST_HIGH (offset);
8466 if (add_double_with_sign (offset_low, offset_high,
8467 bitpos / BITS_PER_UNIT, 0,
8468 &total_low, &total_high,
8469 true))
8470 return true;
8472 if (total_high != 0)
8473 return true;
8475 size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8476 if (size <= 0)
8477 return true;
8479 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8480 array. */
8481 if (TREE_CODE (base) == ADDR_EXPR)
8483 HOST_WIDE_INT base_size;
8485 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8486 if (base_size > 0 && size < base_size)
8487 size = base_size;
8490 return total_low > (unsigned HOST_WIDE_INT) size;
8493 /* Subroutine of fold_binary. This routine performs all of the
8494 transformations that are common to the equality/inequality
8495 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8496 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8497 fold_binary should call fold_binary. Fold a comparison with
8498 tree code CODE and type TYPE with operands OP0 and OP1. Return
8499 the folded comparison or NULL_TREE. */
8501 static tree
8502 fold_comparison (location_t loc, enum tree_code code, tree type,
8503 tree op0, tree op1)
8505 tree arg0, arg1, tem;
8507 arg0 = op0;
8508 arg1 = op1;
8510 STRIP_SIGN_NOPS (arg0);
8511 STRIP_SIGN_NOPS (arg1);
8513 tem = fold_relational_const (code, type, arg0, arg1);
8514 if (tem != NULL_TREE)
8515 return tem;
8517 /* If one arg is a real or integer constant, put it last. */
8518 if (tree_swap_operands_p (arg0, arg1, true))
8519 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
8521 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8522 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8523 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8524 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8525 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8526 && (TREE_CODE (arg1) == INTEGER_CST
8527 && !TREE_OVERFLOW (arg1)))
8529 tree const1 = TREE_OPERAND (arg0, 1);
8530 tree const2 = arg1;
8531 tree variable = TREE_OPERAND (arg0, 0);
8532 tree lhs;
8533 int lhs_add;
8534 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8536 lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
8537 TREE_TYPE (arg1), const2, const1);
8539 /* If the constant operation overflowed this can be
8540 simplified as a comparison against INT_MAX/INT_MIN. */
8541 if (TREE_CODE (lhs) == INTEGER_CST
8542 && TREE_OVERFLOW (lhs))
8544 int const1_sgn = tree_int_cst_sgn (const1);
8545 enum tree_code code2 = code;
8547 /* Get the sign of the constant on the lhs if the
8548 operation were VARIABLE + CONST1. */
8549 if (TREE_CODE (arg0) == MINUS_EXPR)
8550 const1_sgn = -const1_sgn;
8552 /* The sign of the constant determines if we overflowed
8553 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8554 Canonicalize to the INT_MIN overflow by swapping the comparison
8555 if necessary. */
8556 if (const1_sgn == -1)
8557 code2 = swap_tree_comparison (code);
8559 /* We now can look at the canonicalized case
8560 VARIABLE + 1 CODE2 INT_MIN
8561 and decide on the result. */
8562 if (code2 == LT_EXPR
8563 || code2 == LE_EXPR
8564 || code2 == EQ_EXPR)
8565 return omit_one_operand_loc (loc, type, boolean_false_node, variable);
8566 else if (code2 == NE_EXPR
8567 || code2 == GE_EXPR
8568 || code2 == GT_EXPR)
8569 return omit_one_operand_loc (loc, type, boolean_true_node, variable);
8572 if (TREE_CODE (lhs) == TREE_CODE (arg1)
8573 && (TREE_CODE (lhs) != INTEGER_CST
8574 || !TREE_OVERFLOW (lhs)))
8576 fold_overflow_warning ("assuming signed overflow does not occur "
8577 "when changing X +- C1 cmp C2 to "
8578 "X cmp C1 +- C2",
8579 WARN_STRICT_OVERFLOW_COMPARISON);
8580 return fold_build2_loc (loc, code, type, variable, lhs);
8584 /* For comparisons of pointers we can decompose it to a compile time
8585 comparison of the base objects and the offsets into the object.
8586 This requires at least one operand being an ADDR_EXPR or a
8587 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8588 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8589 && (TREE_CODE (arg0) == ADDR_EXPR
8590 || TREE_CODE (arg1) == ADDR_EXPR
8591 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8592 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8594 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8595 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8596 enum machine_mode mode;
8597 int volatilep, unsignedp;
8598 bool indirect_base0 = false, indirect_base1 = false;
8600 /* Get base and offset for the access. Strip ADDR_EXPR for
8601 get_inner_reference, but put it back by stripping INDIRECT_REF
8602 off the base object if possible. indirect_baseN will be true
8603 if baseN is not an address but refers to the object itself. */
8604 base0 = arg0;
8605 if (TREE_CODE (arg0) == ADDR_EXPR)
8607 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8608 &bitsize, &bitpos0, &offset0, &mode,
8609 &unsignedp, &volatilep, false);
8610 if (TREE_CODE (base0) == INDIRECT_REF)
8611 base0 = TREE_OPERAND (base0, 0);
8612 else
8613 indirect_base0 = true;
8615 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8617 base0 = TREE_OPERAND (arg0, 0);
8618 STRIP_SIGN_NOPS (base0);
8619 if (TREE_CODE (base0) == ADDR_EXPR)
8621 base0 = TREE_OPERAND (base0, 0);
8622 indirect_base0 = true;
8624 offset0 = TREE_OPERAND (arg0, 1);
8627 base1 = arg1;
8628 if (TREE_CODE (arg1) == ADDR_EXPR)
8630 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8631 &bitsize, &bitpos1, &offset1, &mode,
8632 &unsignedp, &volatilep, false);
8633 if (TREE_CODE (base1) == INDIRECT_REF)
8634 base1 = TREE_OPERAND (base1, 0);
8635 else
8636 indirect_base1 = true;
8638 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8640 base1 = TREE_OPERAND (arg1, 0);
8641 STRIP_SIGN_NOPS (base1);
8642 if (TREE_CODE (base1) == ADDR_EXPR)
8644 base1 = TREE_OPERAND (base1, 0);
8645 indirect_base1 = true;
8647 offset1 = TREE_OPERAND (arg1, 1);
8650 /* A local variable can never be pointed to by
8651 the default SSA name of an incoming parameter. */
8652 if ((TREE_CODE (arg0) == ADDR_EXPR
8653 && indirect_base0
8654 && TREE_CODE (base0) == VAR_DECL
8655 && auto_var_in_fn_p (base0, current_function_decl)
8656 && !indirect_base1
8657 && TREE_CODE (base1) == SSA_NAME
8658 && TREE_CODE (SSA_NAME_VAR (base1)) == PARM_DECL
8659 && SSA_NAME_IS_DEFAULT_DEF (base1))
8660 || (TREE_CODE (arg1) == ADDR_EXPR
8661 && indirect_base1
8662 && TREE_CODE (base1) == VAR_DECL
8663 && auto_var_in_fn_p (base1, current_function_decl)
8664 && !indirect_base0
8665 && TREE_CODE (base0) == SSA_NAME
8666 && TREE_CODE (SSA_NAME_VAR (base0)) == PARM_DECL
8667 && SSA_NAME_IS_DEFAULT_DEF (base0)))
8669 if (code == NE_EXPR)
8670 return constant_boolean_node (1, type);
8671 else if (code == EQ_EXPR)
8672 return constant_boolean_node (0, type);
8674 /* If we have equivalent bases we might be able to simplify. */
8675 else if (indirect_base0 == indirect_base1
8676 && operand_equal_p (base0, base1, 0))
8678 /* We can fold this expression to a constant if the non-constant
8679 offset parts are equal. */
8680 if ((offset0 == offset1
8681 || (offset0 && offset1
8682 && operand_equal_p (offset0, offset1, 0)))
8683 && (code == EQ_EXPR
8684 || code == NE_EXPR
8685 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8688 if (code != EQ_EXPR
8689 && code != NE_EXPR
8690 && bitpos0 != bitpos1
8691 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8692 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8693 fold_overflow_warning (("assuming pointer wraparound does not "
8694 "occur when comparing P +- C1 with "
8695 "P +- C2"),
8696 WARN_STRICT_OVERFLOW_CONDITIONAL);
8698 switch (code)
8700 case EQ_EXPR:
8701 return constant_boolean_node (bitpos0 == bitpos1, type);
8702 case NE_EXPR:
8703 return constant_boolean_node (bitpos0 != bitpos1, type);
8704 case LT_EXPR:
8705 return constant_boolean_node (bitpos0 < bitpos1, type);
8706 case LE_EXPR:
8707 return constant_boolean_node (bitpos0 <= bitpos1, type);
8708 case GE_EXPR:
8709 return constant_boolean_node (bitpos0 >= bitpos1, type);
8710 case GT_EXPR:
8711 return constant_boolean_node (bitpos0 > bitpos1, type);
8712 default:;
8715 /* We can simplify the comparison to a comparison of the variable
8716 offset parts if the constant offset parts are equal.
8717 Be careful to use signed size type here because otherwise we
8718 mess with array offsets in the wrong way. This is possible
8719 because pointer arithmetic is restricted to retain within an
8720 object and overflow on pointer differences is undefined as of
8721 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8722 else if (bitpos0 == bitpos1
8723 && ((code == EQ_EXPR || code == NE_EXPR)
8724 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8726 /* By converting to signed size type we cover middle-end pointer
8727 arithmetic which operates on unsigned pointer types of size
8728 type size and ARRAY_REF offsets which are properly sign or
8729 zero extended from their type in case it is narrower than
8730 size type. */
8731 if (offset0 == NULL_TREE)
8732 offset0 = build_int_cst (ssizetype, 0);
8733 else
8734 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8735 if (offset1 == NULL_TREE)
8736 offset1 = build_int_cst (ssizetype, 0);
8737 else
8738 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8740 if (code != EQ_EXPR
8741 && code != NE_EXPR
8742 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8743 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8744 fold_overflow_warning (("assuming pointer wraparound does not "
8745 "occur when comparing P +- C1 with "
8746 "P +- C2"),
8747 WARN_STRICT_OVERFLOW_COMPARISON);
8749 return fold_build2_loc (loc, code, type, offset0, offset1);
8752 /* For non-equal bases we can simplify if they are addresses
8753 of local binding decls or constants. */
8754 else if (indirect_base0 && indirect_base1
8755 /* We know that !operand_equal_p (base0, base1, 0)
8756 because the if condition was false. But make
8757 sure two decls are not the same. */
8758 && base0 != base1
8759 && TREE_CODE (arg0) == ADDR_EXPR
8760 && TREE_CODE (arg1) == ADDR_EXPR
8761 && (((TREE_CODE (base0) == VAR_DECL
8762 || TREE_CODE (base0) == PARM_DECL)
8763 && (targetm.binds_local_p (base0)
8764 || CONSTANT_CLASS_P (base1)))
8765 || CONSTANT_CLASS_P (base0))
8766 && (((TREE_CODE (base1) == VAR_DECL
8767 || TREE_CODE (base1) == PARM_DECL)
8768 && (targetm.binds_local_p (base1)
8769 || CONSTANT_CLASS_P (base0)))
8770 || CONSTANT_CLASS_P (base1)))
8772 if (code == EQ_EXPR)
8773 return omit_two_operands_loc (loc, type, boolean_false_node,
8774 arg0, arg1);
8775 else if (code == NE_EXPR)
8776 return omit_two_operands_loc (loc, type, boolean_true_node,
8777 arg0, arg1);
8779 /* For equal offsets we can simplify to a comparison of the
8780 base addresses. */
8781 else if (bitpos0 == bitpos1
8782 && (indirect_base0
8783 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8784 && (indirect_base1
8785 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8786 && ((offset0 == offset1)
8787 || (offset0 && offset1
8788 && operand_equal_p (offset0, offset1, 0))))
8790 if (indirect_base0)
8791 base0 = build_fold_addr_expr_loc (loc, base0);
8792 if (indirect_base1)
8793 base1 = build_fold_addr_expr_loc (loc, base1);
8794 return fold_build2_loc (loc, code, type, base0, base1);
8798 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8799 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8800 the resulting offset is smaller in absolute value than the
8801 original one. */
8802 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8803 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8804 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8805 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8806 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8807 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8808 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8810 tree const1 = TREE_OPERAND (arg0, 1);
8811 tree const2 = TREE_OPERAND (arg1, 1);
8812 tree variable1 = TREE_OPERAND (arg0, 0);
8813 tree variable2 = TREE_OPERAND (arg1, 0);
8814 tree cst;
8815 const char * const warnmsg = G_("assuming signed overflow does not "
8816 "occur when combining constants around "
8817 "a comparison");
8819 /* Put the constant on the side where it doesn't overflow and is
8820 of lower absolute value than before. */
8821 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8822 ? MINUS_EXPR : PLUS_EXPR,
8823 const2, const1, 0);
8824 if (!TREE_OVERFLOW (cst)
8825 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8827 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8828 return fold_build2_loc (loc, code, type,
8829 variable1,
8830 fold_build2_loc (loc,
8831 TREE_CODE (arg1), TREE_TYPE (arg1),
8832 variable2, cst));
8835 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8836 ? MINUS_EXPR : PLUS_EXPR,
8837 const1, const2, 0);
8838 if (!TREE_OVERFLOW (cst)
8839 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8841 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8842 return fold_build2_loc (loc, code, type,
8843 fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
8844 variable1, cst),
8845 variable2);
8849 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8850 signed arithmetic case. That form is created by the compiler
8851 often enough for folding it to be of value. One example is in
8852 computing loop trip counts after Operator Strength Reduction. */
8853 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8854 && TREE_CODE (arg0) == MULT_EXPR
8855 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8856 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8857 && integer_zerop (arg1))
8859 tree const1 = TREE_OPERAND (arg0, 1);
8860 tree const2 = arg1; /* zero */
8861 tree variable1 = TREE_OPERAND (arg0, 0);
8862 enum tree_code cmp_code = code;
8864 /* Handle unfolded multiplication by zero. */
8865 if (integer_zerop (const1))
8866 return fold_build2_loc (loc, cmp_code, type, const1, const2);
8868 fold_overflow_warning (("assuming signed overflow does not occur when "
8869 "eliminating multiplication in comparison "
8870 "with zero"),
8871 WARN_STRICT_OVERFLOW_COMPARISON);
8873 /* If const1 is negative we swap the sense of the comparison. */
8874 if (tree_int_cst_sgn (const1) < 0)
8875 cmp_code = swap_tree_comparison (cmp_code);
8877 return fold_build2_loc (loc, cmp_code, type, variable1, const2);
8880 tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
8881 if (tem)
8882 return tem;
8884 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8886 tree targ0 = strip_float_extensions (arg0);
8887 tree targ1 = strip_float_extensions (arg1);
8888 tree newtype = TREE_TYPE (targ0);
8890 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8891 newtype = TREE_TYPE (targ1);
8893 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
8894 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8895 return fold_build2_loc (loc, code, type,
8896 fold_convert_loc (loc, newtype, targ0),
8897 fold_convert_loc (loc, newtype, targ1));
8899 /* (-a) CMP (-b) -> b CMP a */
8900 if (TREE_CODE (arg0) == NEGATE_EXPR
8901 && TREE_CODE (arg1) == NEGATE_EXPR)
8902 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
8903 TREE_OPERAND (arg0, 0));
8905 if (TREE_CODE (arg1) == REAL_CST)
8907 REAL_VALUE_TYPE cst;
8908 cst = TREE_REAL_CST (arg1);
8910 /* (-a) CMP CST -> a swap(CMP) (-CST) */
8911 if (TREE_CODE (arg0) == NEGATE_EXPR)
8912 return fold_build2_loc (loc, swap_tree_comparison (code), type,
8913 TREE_OPERAND (arg0, 0),
8914 build_real (TREE_TYPE (arg1),
8915 real_value_negate (&cst)));
8917 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
8918 /* a CMP (-0) -> a CMP 0 */
8919 if (REAL_VALUE_MINUS_ZERO (cst))
8920 return fold_build2_loc (loc, code, type, arg0,
8921 build_real (TREE_TYPE (arg1), dconst0));
8923 /* x != NaN is always true, other ops are always false. */
8924 if (REAL_VALUE_ISNAN (cst)
8925 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8927 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8928 return omit_one_operand_loc (loc, type, tem, arg0);
8931 /* Fold comparisons against infinity. */
8932 if (REAL_VALUE_ISINF (cst)
8933 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
8935 tem = fold_inf_compare (loc, code, type, arg0, arg1);
8936 if (tem != NULL_TREE)
8937 return tem;
8941 /* If this is a comparison of a real constant with a PLUS_EXPR
8942 or a MINUS_EXPR of a real constant, we can convert it into a
8943 comparison with a revised real constant as long as no overflow
8944 occurs when unsafe_math_optimizations are enabled. */
8945 if (flag_unsafe_math_optimizations
8946 && TREE_CODE (arg1) == REAL_CST
8947 && (TREE_CODE (arg0) == PLUS_EXPR
8948 || TREE_CODE (arg0) == MINUS_EXPR)
8949 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8950 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8951 ? MINUS_EXPR : PLUS_EXPR,
8952 arg1, TREE_OPERAND (arg0, 1)))
8953 && !TREE_OVERFLOW (tem))
8954 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8956 /* Likewise, we can simplify a comparison of a real constant with
8957 a MINUS_EXPR whose first operand is also a real constant, i.e.
8958 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
8959 floating-point types only if -fassociative-math is set. */
8960 if (flag_associative_math
8961 && TREE_CODE (arg1) == REAL_CST
8962 && TREE_CODE (arg0) == MINUS_EXPR
8963 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8964 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8965 arg1))
8966 && !TREE_OVERFLOW (tem))
8967 return fold_build2_loc (loc, swap_tree_comparison (code), type,
8968 TREE_OPERAND (arg0, 1), tem);
8970 /* Fold comparisons against built-in math functions. */
8971 if (TREE_CODE (arg1) == REAL_CST
8972 && flag_unsafe_math_optimizations
8973 && ! flag_errno_math)
8975 enum built_in_function fcode = builtin_mathfn_code (arg0);
8977 if (fcode != END_BUILTINS)
8979 tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
8980 if (tem != NULL_TREE)
8981 return tem;
8986 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
8987 && CONVERT_EXPR_P (arg0))
8989 /* If we are widening one operand of an integer comparison,
8990 see if the other operand is similarly being widened. Perhaps we
8991 can do the comparison in the narrower type. */
8992 tem = fold_widened_comparison (loc, code, type, arg0, arg1);
8993 if (tem)
8994 return tem;
8996 /* Or if we are changing signedness. */
8997 tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
8998 if (tem)
8999 return tem;
9002 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9003 constant, we can simplify it. */
9004 if (TREE_CODE (arg1) == INTEGER_CST
9005 && (TREE_CODE (arg0) == MIN_EXPR
9006 || TREE_CODE (arg0) == MAX_EXPR)
9007 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9009 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9010 if (tem)
9011 return tem;
9014 /* Simplify comparison of something with itself. (For IEEE
9015 floating-point, we can only do some of these simplifications.) */
9016 if (operand_equal_p (arg0, arg1, 0))
9018 switch (code)
9020 case EQ_EXPR:
9021 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9022 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9023 return constant_boolean_node (1, type);
9024 break;
9026 case GE_EXPR:
9027 case LE_EXPR:
9028 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9029 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9030 return constant_boolean_node (1, type);
9031 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9033 case NE_EXPR:
9034 /* For NE, we can only do this simplification if integer
9035 or we don't honor IEEE floating point NaNs. */
9036 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9037 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9038 break;
9039 /* ... fall through ... */
9040 case GT_EXPR:
9041 case LT_EXPR:
9042 return constant_boolean_node (0, type);
9043 default:
9044 gcc_unreachable ();
9048 /* If we are comparing an expression that just has comparisons
9049 of two integer values, arithmetic expressions of those comparisons,
9050 and constants, we can simplify it. There are only three cases
9051 to check: the two values can either be equal, the first can be
9052 greater, or the second can be greater. Fold the expression for
9053 those three values. Since each value must be 0 or 1, we have
9054 eight possibilities, each of which corresponds to the constant 0
9055 or 1 or one of the six possible comparisons.
9057 This handles common cases like (a > b) == 0 but also handles
9058 expressions like ((x > y) - (y > x)) > 0, which supposedly
9059 occur in macroized code. */
9061 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9063 tree cval1 = 0, cval2 = 0;
9064 int save_p = 0;
9066 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9067 /* Don't handle degenerate cases here; they should already
9068 have been handled anyway. */
9069 && cval1 != 0 && cval2 != 0
9070 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9071 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9072 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9073 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9074 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9075 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9076 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9078 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9079 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9081 /* We can't just pass T to eval_subst in case cval1 or cval2
9082 was the same as ARG1. */
9084 tree high_result
9085 = fold_build2_loc (loc, code, type,
9086 eval_subst (loc, arg0, cval1, maxval,
9087 cval2, minval),
9088 arg1);
9089 tree equal_result
9090 = fold_build2_loc (loc, code, type,
9091 eval_subst (loc, arg0, cval1, maxval,
9092 cval2, maxval),
9093 arg1);
9094 tree low_result
9095 = fold_build2_loc (loc, code, type,
9096 eval_subst (loc, arg0, cval1, minval,
9097 cval2, maxval),
9098 arg1);
9100 /* All three of these results should be 0 or 1. Confirm they are.
9101 Then use those values to select the proper code to use. */
9103 if (TREE_CODE (high_result) == INTEGER_CST
9104 && TREE_CODE (equal_result) == INTEGER_CST
9105 && TREE_CODE (low_result) == INTEGER_CST)
9107 /* Make a 3-bit mask with the high-order bit being the
9108 value for `>', the next for '=', and the low for '<'. */
9109 switch ((integer_onep (high_result) * 4)
9110 + (integer_onep (equal_result) * 2)
9111 + integer_onep (low_result))
9113 case 0:
9114 /* Always false. */
9115 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9116 case 1:
9117 code = LT_EXPR;
9118 break;
9119 case 2:
9120 code = EQ_EXPR;
9121 break;
9122 case 3:
9123 code = LE_EXPR;
9124 break;
9125 case 4:
9126 code = GT_EXPR;
9127 break;
9128 case 5:
9129 code = NE_EXPR;
9130 break;
9131 case 6:
9132 code = GE_EXPR;
9133 break;
9134 case 7:
9135 /* Always true. */
9136 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9139 if (save_p)
9141 tem = save_expr (build2 (code, type, cval1, cval2));
9142 SET_EXPR_LOCATION (tem, loc);
9143 return tem;
9145 return fold_build2_loc (loc, code, type, cval1, cval2);
9150 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9151 into a single range test. */
9152 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9153 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9154 && TREE_CODE (arg1) == INTEGER_CST
9155 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9156 && !integer_zerop (TREE_OPERAND (arg0, 1))
9157 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9158 && !TREE_OVERFLOW (arg1))
9160 tem = fold_div_compare (loc, code, type, arg0, arg1);
9161 if (tem != NULL_TREE)
9162 return tem;
9165 /* Fold ~X op ~Y as Y op X. */
9166 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9167 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9169 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9170 return fold_build2_loc (loc, code, type,
9171 fold_convert_loc (loc, cmp_type,
9172 TREE_OPERAND (arg1, 0)),
9173 TREE_OPERAND (arg0, 0));
9176 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9177 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9178 && TREE_CODE (arg1) == INTEGER_CST)
9180 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9181 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9182 TREE_OPERAND (arg0, 0),
9183 fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9184 fold_convert_loc (loc, cmp_type, arg1)));
9187 return NULL_TREE;
9191 /* Subroutine of fold_binary. Optimize complex multiplications of the
9192 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9193 argument EXPR represents the expression "z" of type TYPE. */
9195 static tree
9196 fold_mult_zconjz (location_t loc, tree type, tree expr)
9198 tree itype = TREE_TYPE (type);
9199 tree rpart, ipart, tem;
9201 if (TREE_CODE (expr) == COMPLEX_EXPR)
9203 rpart = TREE_OPERAND (expr, 0);
9204 ipart = TREE_OPERAND (expr, 1);
9206 else if (TREE_CODE (expr) == COMPLEX_CST)
9208 rpart = TREE_REALPART (expr);
9209 ipart = TREE_IMAGPART (expr);
9211 else
9213 expr = save_expr (expr);
9214 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9215 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9218 rpart = save_expr (rpart);
9219 ipart = save_expr (ipart);
9220 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9221 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9222 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9223 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9224 build_zero_cst (itype));
9228 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9229 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9230 guarantees that P and N have the same least significant log2(M) bits.
9231 N is not otherwise constrained. In particular, N is not normalized to
9232 0 <= N < M as is common. In general, the precise value of P is unknown.
9233 M is chosen as large as possible such that constant N can be determined.
9235 Returns M and sets *RESIDUE to N.
9237 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9238 account. This is not always possible due to PR 35705.
9241 static unsigned HOST_WIDE_INT
9242 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9243 bool allow_func_align)
9245 enum tree_code code;
9247 *residue = 0;
9249 code = TREE_CODE (expr);
9250 if (code == ADDR_EXPR)
9252 expr = TREE_OPERAND (expr, 0);
9253 if (handled_component_p (expr))
9255 HOST_WIDE_INT bitsize, bitpos;
9256 tree offset;
9257 enum machine_mode mode;
9258 int unsignedp, volatilep;
9260 expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9261 &mode, &unsignedp, &volatilep, false);
9262 *residue = bitpos / BITS_PER_UNIT;
9263 if (offset)
9265 if (TREE_CODE (offset) == INTEGER_CST)
9266 *residue += TREE_INT_CST_LOW (offset);
9267 else
9268 /* We don't handle more complicated offset expressions. */
9269 return 1;
9273 if (DECL_P (expr)
9274 && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9275 return DECL_ALIGN_UNIT (expr);
9277 else if (code == POINTER_PLUS_EXPR)
9279 tree op0, op1;
9280 unsigned HOST_WIDE_INT modulus;
9281 enum tree_code inner_code;
9283 op0 = TREE_OPERAND (expr, 0);
9284 STRIP_NOPS (op0);
9285 modulus = get_pointer_modulus_and_residue (op0, residue,
9286 allow_func_align);
9288 op1 = TREE_OPERAND (expr, 1);
9289 STRIP_NOPS (op1);
9290 inner_code = TREE_CODE (op1);
9291 if (inner_code == INTEGER_CST)
9293 *residue += TREE_INT_CST_LOW (op1);
9294 return modulus;
9296 else if (inner_code == MULT_EXPR)
9298 op1 = TREE_OPERAND (op1, 1);
9299 if (TREE_CODE (op1) == INTEGER_CST)
9301 unsigned HOST_WIDE_INT align;
9303 /* Compute the greatest power-of-2 divisor of op1. */
9304 align = TREE_INT_CST_LOW (op1);
9305 align &= -align;
9307 /* If align is non-zero and less than *modulus, replace
9308 *modulus with align., If align is 0, then either op1 is 0
9309 or the greatest power-of-2 divisor of op1 doesn't fit in an
9310 unsigned HOST_WIDE_INT. In either case, no additional
9311 constraint is imposed. */
9312 if (align)
9313 modulus = MIN (modulus, align);
9315 return modulus;
9320 /* If we get here, we were unable to determine anything useful about the
9321 expression. */
9322 return 1;
9326 /* Fold a binary expression of code CODE and type TYPE with operands
9327 OP0 and OP1. LOC is the location of the resulting expression.
9328 Return the folded expression if folding is successful. Otherwise,
9329 return NULL_TREE. */
9331 tree
9332 fold_binary_loc (location_t loc,
9333 enum tree_code code, tree type, tree op0, tree op1)
9335 enum tree_code_class kind = TREE_CODE_CLASS (code);
9336 tree arg0, arg1, tem;
9337 tree t1 = NULL_TREE;
9338 bool strict_overflow_p;
9340 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9341 && TREE_CODE_LENGTH (code) == 2
9342 && op0 != NULL_TREE
9343 && op1 != NULL_TREE);
9345 arg0 = op0;
9346 arg1 = op1;
9348 /* Strip any conversions that don't change the mode. This is
9349 safe for every expression, except for a comparison expression
9350 because its signedness is derived from its operands. So, in
9351 the latter case, only strip conversions that don't change the
9352 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9353 preserved.
9355 Note that this is done as an internal manipulation within the
9356 constant folder, in order to find the simplest representation
9357 of the arguments so that their form can be studied. In any
9358 cases, the appropriate type conversions should be put back in
9359 the tree that will get out of the constant folder. */
9361 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9363 STRIP_SIGN_NOPS (arg0);
9364 STRIP_SIGN_NOPS (arg1);
9366 else
9368 STRIP_NOPS (arg0);
9369 STRIP_NOPS (arg1);
9372 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9373 constant but we can't do arithmetic on them. */
9374 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9375 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9376 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9377 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9378 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9379 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9381 if (kind == tcc_binary)
9383 /* Make sure type and arg0 have the same saturating flag. */
9384 gcc_assert (TYPE_SATURATING (type)
9385 == TYPE_SATURATING (TREE_TYPE (arg0)));
9386 tem = const_binop (code, arg0, arg1);
9388 else if (kind == tcc_comparison)
9389 tem = fold_relational_const (code, type, arg0, arg1);
9390 else
9391 tem = NULL_TREE;
9393 if (tem != NULL_TREE)
9395 if (TREE_TYPE (tem) != type)
9396 tem = fold_convert_loc (loc, type, tem);
9397 return tem;
9401 /* If this is a commutative operation, and ARG0 is a constant, move it
9402 to ARG1 to reduce the number of tests below. */
9403 if (commutative_tree_code (code)
9404 && tree_swap_operands_p (arg0, arg1, true))
9405 return fold_build2_loc (loc, code, type, op1, op0);
9407 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9409 First check for cases where an arithmetic operation is applied to a
9410 compound, conditional, or comparison operation. Push the arithmetic
9411 operation inside the compound or conditional to see if any folding
9412 can then be done. Convert comparison to conditional for this purpose.
9413 The also optimizes non-constant cases that used to be done in
9414 expand_expr.
9416 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9417 one of the operands is a comparison and the other is a comparison, a
9418 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9419 code below would make the expression more complex. Change it to a
9420 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9421 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9423 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9424 || code == EQ_EXPR || code == NE_EXPR)
9425 && ((truth_value_p (TREE_CODE (arg0))
9426 && (truth_value_p (TREE_CODE (arg1))
9427 || (TREE_CODE (arg1) == BIT_AND_EXPR
9428 && integer_onep (TREE_OPERAND (arg1, 1)))))
9429 || (truth_value_p (TREE_CODE (arg1))
9430 && (truth_value_p (TREE_CODE (arg0))
9431 || (TREE_CODE (arg0) == BIT_AND_EXPR
9432 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9434 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9435 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9436 : TRUTH_XOR_EXPR,
9437 boolean_type_node,
9438 fold_convert_loc (loc, boolean_type_node, arg0),
9439 fold_convert_loc (loc, boolean_type_node, arg1));
9441 if (code == EQ_EXPR)
9442 tem = invert_truthvalue_loc (loc, tem);
9444 return fold_convert_loc (loc, type, tem);
9447 if (TREE_CODE_CLASS (code) == tcc_binary
9448 || TREE_CODE_CLASS (code) == tcc_comparison)
9450 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9452 tem = fold_build2_loc (loc, code, type,
9453 fold_convert_loc (loc, TREE_TYPE (op0),
9454 TREE_OPERAND (arg0, 1)), op1);
9455 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9456 tem);
9458 if (TREE_CODE (arg1) == COMPOUND_EXPR
9459 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9461 tem = fold_build2_loc (loc, code, type, op0,
9462 fold_convert_loc (loc, TREE_TYPE (op1),
9463 TREE_OPERAND (arg1, 1)));
9464 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9465 tem);
9468 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9470 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9471 arg0, arg1,
9472 /*cond_first_p=*/1);
9473 if (tem != NULL_TREE)
9474 return tem;
9477 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9479 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9480 arg1, arg0,
9481 /*cond_first_p=*/0);
9482 if (tem != NULL_TREE)
9483 return tem;
9487 switch (code)
9489 case MEM_REF:
9490 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9491 if (TREE_CODE (arg0) == ADDR_EXPR
9492 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9494 tree iref = TREE_OPERAND (arg0, 0);
9495 return fold_build2 (MEM_REF, type,
9496 TREE_OPERAND (iref, 0),
9497 int_const_binop (PLUS_EXPR, arg1,
9498 TREE_OPERAND (iref, 1), 0));
9501 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9502 if (TREE_CODE (arg0) == ADDR_EXPR
9503 && handled_component_p (TREE_OPERAND (arg0, 0)))
9505 tree base;
9506 HOST_WIDE_INT coffset;
9507 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9508 &coffset);
9509 if (!base)
9510 return NULL_TREE;
9511 return fold_build2 (MEM_REF, type,
9512 build_fold_addr_expr (base),
9513 int_const_binop (PLUS_EXPR, arg1,
9514 size_int (coffset), 0));
9517 return NULL_TREE;
9519 case POINTER_PLUS_EXPR:
9520 /* 0 +p index -> (type)index */
9521 if (integer_zerop (arg0))
9522 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9524 /* PTR +p 0 -> PTR */
9525 if (integer_zerop (arg1))
9526 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9528 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9529 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9530 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9531 return fold_convert_loc (loc, type,
9532 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9533 fold_convert_loc (loc, sizetype,
9534 arg1),
9535 fold_convert_loc (loc, sizetype,
9536 arg0)));
9538 /* index +p PTR -> PTR +p index */
9539 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9540 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9541 return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
9542 fold_convert_loc (loc, type, arg1),
9543 fold_convert_loc (loc, sizetype, arg0));
9545 /* (PTR +p B) +p A -> PTR +p (B + A) */
9546 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9548 tree inner;
9549 tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
9550 tree arg00 = TREE_OPERAND (arg0, 0);
9551 inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
9552 arg01, fold_convert_loc (loc, sizetype, arg1));
9553 return fold_convert_loc (loc, type,
9554 fold_build2_loc (loc, POINTER_PLUS_EXPR,
9555 TREE_TYPE (arg00),
9556 arg00, inner));
9559 /* PTR_CST +p CST -> CST1 */
9560 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9561 return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
9562 fold_convert_loc (loc, type, arg1));
9564 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9565 of the array. Loop optimizer sometimes produce this type of
9566 expressions. */
9567 if (TREE_CODE (arg0) == ADDR_EXPR)
9569 tem = try_move_mult_to_index (loc, arg0,
9570 fold_convert_loc (loc, sizetype, arg1));
9571 if (tem)
9572 return fold_convert_loc (loc, type, tem);
9575 return NULL_TREE;
9577 case PLUS_EXPR:
9578 /* A + (-B) -> A - B */
9579 if (TREE_CODE (arg1) == NEGATE_EXPR)
9580 return fold_build2_loc (loc, MINUS_EXPR, type,
9581 fold_convert_loc (loc, type, arg0),
9582 fold_convert_loc (loc, type,
9583 TREE_OPERAND (arg1, 0)));
9584 /* (-A) + B -> B - A */
9585 if (TREE_CODE (arg0) == NEGATE_EXPR
9586 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9587 return fold_build2_loc (loc, MINUS_EXPR, type,
9588 fold_convert_loc (loc, type, arg1),
9589 fold_convert_loc (loc, type,
9590 TREE_OPERAND (arg0, 0)));
9592 if (INTEGRAL_TYPE_P (type))
9594 /* Convert ~A + 1 to -A. */
9595 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9596 && integer_onep (arg1))
9597 return fold_build1_loc (loc, NEGATE_EXPR, type,
9598 fold_convert_loc (loc, type,
9599 TREE_OPERAND (arg0, 0)));
9601 /* ~X + X is -1. */
9602 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9603 && !TYPE_OVERFLOW_TRAPS (type))
9605 tree tem = TREE_OPERAND (arg0, 0);
9607 STRIP_NOPS (tem);
9608 if (operand_equal_p (tem, arg1, 0))
9610 t1 = build_int_cst_type (type, -1);
9611 return omit_one_operand_loc (loc, type, t1, arg1);
9615 /* X + ~X is -1. */
9616 if (TREE_CODE (arg1) == BIT_NOT_EXPR
9617 && !TYPE_OVERFLOW_TRAPS (type))
9619 tree tem = TREE_OPERAND (arg1, 0);
9621 STRIP_NOPS (tem);
9622 if (operand_equal_p (arg0, tem, 0))
9624 t1 = build_int_cst_type (type, -1);
9625 return omit_one_operand_loc (loc, type, t1, arg0);
9629 /* X + (X / CST) * -CST is X % CST. */
9630 if (TREE_CODE (arg1) == MULT_EXPR
9631 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9632 && operand_equal_p (arg0,
9633 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9635 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9636 tree cst1 = TREE_OPERAND (arg1, 1);
9637 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9638 cst1, cst0);
9639 if (sum && integer_zerop (sum))
9640 return fold_convert_loc (loc, type,
9641 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9642 TREE_TYPE (arg0), arg0,
9643 cst0));
9647 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9648 same or one. Make sure type is not saturating.
9649 fold_plusminus_mult_expr will re-associate. */
9650 if ((TREE_CODE (arg0) == MULT_EXPR
9651 || TREE_CODE (arg1) == MULT_EXPR)
9652 && !TYPE_SATURATING (type)
9653 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9655 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9656 if (tem)
9657 return tem;
9660 if (! FLOAT_TYPE_P (type))
9662 if (integer_zerop (arg1))
9663 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9665 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9666 with a constant, and the two constants have no bits in common,
9667 we should treat this as a BIT_IOR_EXPR since this may produce more
9668 simplifications. */
9669 if (TREE_CODE (arg0) == BIT_AND_EXPR
9670 && TREE_CODE (arg1) == BIT_AND_EXPR
9671 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9672 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9673 && integer_zerop (const_binop (BIT_AND_EXPR,
9674 TREE_OPERAND (arg0, 1),
9675 TREE_OPERAND (arg1, 1))))
9677 code = BIT_IOR_EXPR;
9678 goto bit_ior;
9681 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9682 (plus (plus (mult) (mult)) (foo)) so that we can
9683 take advantage of the factoring cases below. */
9684 if (((TREE_CODE (arg0) == PLUS_EXPR
9685 || TREE_CODE (arg0) == MINUS_EXPR)
9686 && TREE_CODE (arg1) == MULT_EXPR)
9687 || ((TREE_CODE (arg1) == PLUS_EXPR
9688 || TREE_CODE (arg1) == MINUS_EXPR)
9689 && TREE_CODE (arg0) == MULT_EXPR))
9691 tree parg0, parg1, parg, marg;
9692 enum tree_code pcode;
9694 if (TREE_CODE (arg1) == MULT_EXPR)
9695 parg = arg0, marg = arg1;
9696 else
9697 parg = arg1, marg = arg0;
9698 pcode = TREE_CODE (parg);
9699 parg0 = TREE_OPERAND (parg, 0);
9700 parg1 = TREE_OPERAND (parg, 1);
9701 STRIP_NOPS (parg0);
9702 STRIP_NOPS (parg1);
9704 if (TREE_CODE (parg0) == MULT_EXPR
9705 && TREE_CODE (parg1) != MULT_EXPR)
9706 return fold_build2_loc (loc, pcode, type,
9707 fold_build2_loc (loc, PLUS_EXPR, type,
9708 fold_convert_loc (loc, type,
9709 parg0),
9710 fold_convert_loc (loc, type,
9711 marg)),
9712 fold_convert_loc (loc, type, parg1));
9713 if (TREE_CODE (parg0) != MULT_EXPR
9714 && TREE_CODE (parg1) == MULT_EXPR)
9715 return
9716 fold_build2_loc (loc, PLUS_EXPR, type,
9717 fold_convert_loc (loc, type, parg0),
9718 fold_build2_loc (loc, pcode, type,
9719 fold_convert_loc (loc, type, marg),
9720 fold_convert_loc (loc, type,
9721 parg1)));
9724 else
9726 /* See if ARG1 is zero and X + ARG1 reduces to X. */
9727 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9728 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
9730 /* Likewise if the operands are reversed. */
9731 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9732 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
9734 /* Convert X + -C into X - C. */
9735 if (TREE_CODE (arg1) == REAL_CST
9736 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9738 tem = fold_negate_const (arg1, type);
9739 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9740 return fold_build2_loc (loc, MINUS_EXPR, type,
9741 fold_convert_loc (loc, type, arg0),
9742 fold_convert_loc (loc, type, tem));
9745 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9746 to __complex__ ( x, y ). This is not the same for SNaNs or
9747 if signed zeros are involved. */
9748 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9749 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9750 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9752 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9753 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9754 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9755 bool arg0rz = false, arg0iz = false;
9756 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9757 || (arg0i && (arg0iz = real_zerop (arg0i))))
9759 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9760 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9761 if (arg0rz && arg1i && real_zerop (arg1i))
9763 tree rp = arg1r ? arg1r
9764 : build1 (REALPART_EXPR, rtype, arg1);
9765 tree ip = arg0i ? arg0i
9766 : build1 (IMAGPART_EXPR, rtype, arg0);
9767 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9769 else if (arg0iz && arg1r && real_zerop (arg1r))
9771 tree rp = arg0r ? arg0r
9772 : build1 (REALPART_EXPR, rtype, arg0);
9773 tree ip = arg1i ? arg1i
9774 : build1 (IMAGPART_EXPR, rtype, arg1);
9775 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9780 if (flag_unsafe_math_optimizations
9781 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9782 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9783 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9784 return tem;
9786 /* Convert x+x into x*2.0. */
9787 if (operand_equal_p (arg0, arg1, 0)
9788 && SCALAR_FLOAT_TYPE_P (type))
9789 return fold_build2_loc (loc, MULT_EXPR, type, arg0,
9790 build_real (type, dconst2));
9792 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9793 We associate floats only if the user has specified
9794 -fassociative-math. */
9795 if (flag_associative_math
9796 && TREE_CODE (arg1) == PLUS_EXPR
9797 && TREE_CODE (arg0) != MULT_EXPR)
9799 tree tree10 = TREE_OPERAND (arg1, 0);
9800 tree tree11 = TREE_OPERAND (arg1, 1);
9801 if (TREE_CODE (tree11) == MULT_EXPR
9802 && TREE_CODE (tree10) == MULT_EXPR)
9804 tree tree0;
9805 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9806 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9809 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9810 We associate floats only if the user has specified
9811 -fassociative-math. */
9812 if (flag_associative_math
9813 && TREE_CODE (arg0) == PLUS_EXPR
9814 && TREE_CODE (arg1) != MULT_EXPR)
9816 tree tree00 = TREE_OPERAND (arg0, 0);
9817 tree tree01 = TREE_OPERAND (arg0, 1);
9818 if (TREE_CODE (tree01) == MULT_EXPR
9819 && TREE_CODE (tree00) == MULT_EXPR)
9821 tree tree0;
9822 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9823 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9828 bit_rotate:
9829 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9830 is a rotate of A by C1 bits. */
9831 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9832 is a rotate of A by B bits. */
9834 enum tree_code code0, code1;
9835 tree rtype;
9836 code0 = TREE_CODE (arg0);
9837 code1 = TREE_CODE (arg1);
9838 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9839 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9840 && operand_equal_p (TREE_OPERAND (arg0, 0),
9841 TREE_OPERAND (arg1, 0), 0)
9842 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9843 TYPE_UNSIGNED (rtype))
9844 /* Only create rotates in complete modes. Other cases are not
9845 expanded properly. */
9846 && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9848 tree tree01, tree11;
9849 enum tree_code code01, code11;
9851 tree01 = TREE_OPERAND (arg0, 1);
9852 tree11 = TREE_OPERAND (arg1, 1);
9853 STRIP_NOPS (tree01);
9854 STRIP_NOPS (tree11);
9855 code01 = TREE_CODE (tree01);
9856 code11 = TREE_CODE (tree11);
9857 if (code01 == INTEGER_CST
9858 && code11 == INTEGER_CST
9859 && TREE_INT_CST_HIGH (tree01) == 0
9860 && TREE_INT_CST_HIGH (tree11) == 0
9861 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9862 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9864 tem = build2_loc (loc, LROTATE_EXPR,
9865 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9866 TREE_OPERAND (arg0, 0),
9867 code0 == LSHIFT_EXPR ? tree01 : tree11);
9868 return fold_convert_loc (loc, type, tem);
9870 else if (code11 == MINUS_EXPR)
9872 tree tree110, tree111;
9873 tree110 = TREE_OPERAND (tree11, 0);
9874 tree111 = TREE_OPERAND (tree11, 1);
9875 STRIP_NOPS (tree110);
9876 STRIP_NOPS (tree111);
9877 if (TREE_CODE (tree110) == INTEGER_CST
9878 && 0 == compare_tree_int (tree110,
9879 TYPE_PRECISION
9880 (TREE_TYPE (TREE_OPERAND
9881 (arg0, 0))))
9882 && operand_equal_p (tree01, tree111, 0))
9883 return
9884 fold_convert_loc (loc, type,
9885 build2 ((code0 == LSHIFT_EXPR
9886 ? LROTATE_EXPR
9887 : RROTATE_EXPR),
9888 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9889 TREE_OPERAND (arg0, 0), tree01));
9891 else if (code01 == MINUS_EXPR)
9893 tree tree010, tree011;
9894 tree010 = TREE_OPERAND (tree01, 0);
9895 tree011 = TREE_OPERAND (tree01, 1);
9896 STRIP_NOPS (tree010);
9897 STRIP_NOPS (tree011);
9898 if (TREE_CODE (tree010) == INTEGER_CST
9899 && 0 == compare_tree_int (tree010,
9900 TYPE_PRECISION
9901 (TREE_TYPE (TREE_OPERAND
9902 (arg0, 0))))
9903 && operand_equal_p (tree11, tree011, 0))
9904 return fold_convert_loc
9905 (loc, type,
9906 build2 ((code0 != LSHIFT_EXPR
9907 ? LROTATE_EXPR
9908 : RROTATE_EXPR),
9909 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9910 TREE_OPERAND (arg0, 0), tree11));
9915 associate:
9916 /* In most languages, can't associate operations on floats through
9917 parentheses. Rather than remember where the parentheses were, we
9918 don't associate floats at all, unless the user has specified
9919 -fassociative-math.
9920 And, we need to make sure type is not saturating. */
9922 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9923 && !TYPE_SATURATING (type))
9925 tree var0, con0, lit0, minus_lit0;
9926 tree var1, con1, lit1, minus_lit1;
9927 bool ok = true;
9929 /* Split both trees into variables, constants, and literals. Then
9930 associate each group together, the constants with literals,
9931 then the result with variables. This increases the chances of
9932 literals being recombined later and of generating relocatable
9933 expressions for the sum of a constant and literal. */
9934 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9935 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9936 code == MINUS_EXPR);
9938 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9939 if (code == MINUS_EXPR)
9940 code = PLUS_EXPR;
9942 /* With undefined overflow we can only associate constants with one
9943 variable, and constants whose association doesn't overflow. */
9944 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9945 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9947 if (var0 && var1)
9949 tree tmp0 = var0;
9950 tree tmp1 = var1;
9952 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9953 tmp0 = TREE_OPERAND (tmp0, 0);
9954 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9955 tmp1 = TREE_OPERAND (tmp1, 0);
9956 /* The only case we can still associate with two variables
9957 is if they are the same, modulo negation. */
9958 if (!operand_equal_p (tmp0, tmp1, 0))
9959 ok = false;
9962 if (ok && lit0 && lit1)
9964 tree tmp0 = fold_convert (type, lit0);
9965 tree tmp1 = fold_convert (type, lit1);
9967 if (!TREE_OVERFLOW (tmp0) && !TREE_OVERFLOW (tmp1)
9968 && TREE_OVERFLOW (fold_build2 (code, type, tmp0, tmp1)))
9969 ok = false;
9973 /* Only do something if we found more than two objects. Otherwise,
9974 nothing has changed and we risk infinite recursion. */
9975 if (ok
9976 && (2 < ((var0 != 0) + (var1 != 0)
9977 + (con0 != 0) + (con1 != 0)
9978 + (lit0 != 0) + (lit1 != 0)
9979 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9981 var0 = associate_trees (loc, var0, var1, code, type);
9982 con0 = associate_trees (loc, con0, con1, code, type);
9983 lit0 = associate_trees (loc, lit0, lit1, code, type);
9984 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
9986 /* Preserve the MINUS_EXPR if the negative part of the literal is
9987 greater than the positive part. Otherwise, the multiplicative
9988 folding code (i.e extract_muldiv) may be fooled in case
9989 unsigned constants are subtracted, like in the following
9990 example: ((X*2 + 4) - 8U)/2. */
9991 if (minus_lit0 && lit0)
9993 if (TREE_CODE (lit0) == INTEGER_CST
9994 && TREE_CODE (minus_lit0) == INTEGER_CST
9995 && tree_int_cst_lt (lit0, minus_lit0))
9997 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9998 MINUS_EXPR, type);
9999 lit0 = 0;
10001 else
10003 lit0 = associate_trees (loc, lit0, minus_lit0,
10004 MINUS_EXPR, type);
10005 minus_lit0 = 0;
10008 if (minus_lit0)
10010 if (con0 == 0)
10011 return
10012 fold_convert_loc (loc, type,
10013 associate_trees (loc, var0, minus_lit0,
10014 MINUS_EXPR, type));
10015 else
10017 con0 = associate_trees (loc, con0, minus_lit0,
10018 MINUS_EXPR, type);
10019 return
10020 fold_convert_loc (loc, type,
10021 associate_trees (loc, var0, con0,
10022 PLUS_EXPR, type));
10026 con0 = associate_trees (loc, con0, lit0, code, type);
10027 return
10028 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10029 code, type));
10033 return NULL_TREE;
10035 case MINUS_EXPR:
10036 /* Pointer simplifications for subtraction, simple reassociations. */
10037 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10039 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10040 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10041 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10043 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10044 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10045 tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10046 tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10047 return fold_build2_loc (loc, PLUS_EXPR, type,
10048 fold_build2_loc (loc, MINUS_EXPR, type,
10049 arg00, arg10),
10050 fold_build2_loc (loc, MINUS_EXPR, type,
10051 arg01, arg11));
10053 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10054 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10056 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10057 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10058 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10059 fold_convert_loc (loc, type, arg1));
10060 if (tmp)
10061 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10064 /* A - (-B) -> A + B */
10065 if (TREE_CODE (arg1) == NEGATE_EXPR)
10066 return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10067 fold_convert_loc (loc, type,
10068 TREE_OPERAND (arg1, 0)));
10069 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10070 if (TREE_CODE (arg0) == NEGATE_EXPR
10071 && (FLOAT_TYPE_P (type)
10072 || INTEGRAL_TYPE_P (type))
10073 && negate_expr_p (arg1)
10074 && reorder_operands_p (arg0, arg1))
10075 return fold_build2_loc (loc, MINUS_EXPR, type,
10076 fold_convert_loc (loc, type,
10077 negate_expr (arg1)),
10078 fold_convert_loc (loc, type,
10079 TREE_OPERAND (arg0, 0)));
10080 /* Convert -A - 1 to ~A. */
10081 if (INTEGRAL_TYPE_P (type)
10082 && TREE_CODE (arg0) == NEGATE_EXPR
10083 && integer_onep (arg1)
10084 && !TYPE_OVERFLOW_TRAPS (type))
10085 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10086 fold_convert_loc (loc, type,
10087 TREE_OPERAND (arg0, 0)));
10089 /* Convert -1 - A to ~A. */
10090 if (INTEGRAL_TYPE_P (type)
10091 && integer_all_onesp (arg0))
10092 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10095 /* X - (X / CST) * CST is X % CST. */
10096 if (INTEGRAL_TYPE_P (type)
10097 && TREE_CODE (arg1) == MULT_EXPR
10098 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10099 && operand_equal_p (arg0,
10100 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10101 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10102 TREE_OPERAND (arg1, 1), 0))
10103 return
10104 fold_convert_loc (loc, type,
10105 fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10106 arg0, TREE_OPERAND (arg1, 1)));
10108 if (! FLOAT_TYPE_P (type))
10110 if (integer_zerop (arg0))
10111 return negate_expr (fold_convert_loc (loc, type, arg1));
10112 if (integer_zerop (arg1))
10113 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10115 /* Fold A - (A & B) into ~B & A. */
10116 if (!TREE_SIDE_EFFECTS (arg0)
10117 && TREE_CODE (arg1) == BIT_AND_EXPR)
10119 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10121 tree arg10 = fold_convert_loc (loc, type,
10122 TREE_OPERAND (arg1, 0));
10123 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10124 fold_build1_loc (loc, BIT_NOT_EXPR,
10125 type, arg10),
10126 fold_convert_loc (loc, type, arg0));
10128 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10130 tree arg11 = fold_convert_loc (loc,
10131 type, TREE_OPERAND (arg1, 1));
10132 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10133 fold_build1_loc (loc, BIT_NOT_EXPR,
10134 type, arg11),
10135 fold_convert_loc (loc, type, arg0));
10139 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10140 any power of 2 minus 1. */
10141 if (TREE_CODE (arg0) == BIT_AND_EXPR
10142 && TREE_CODE (arg1) == BIT_AND_EXPR
10143 && operand_equal_p (TREE_OPERAND (arg0, 0),
10144 TREE_OPERAND (arg1, 0), 0))
10146 tree mask0 = TREE_OPERAND (arg0, 1);
10147 tree mask1 = TREE_OPERAND (arg1, 1);
10148 tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10150 if (operand_equal_p (tem, mask1, 0))
10152 tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10153 TREE_OPERAND (arg0, 0), mask1);
10154 return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10159 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10160 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10161 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10163 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10164 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10165 (-ARG1 + ARG0) reduces to -ARG1. */
10166 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10167 return negate_expr (fold_convert_loc (loc, type, arg1));
10169 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10170 __complex__ ( x, -y ). This is not the same for SNaNs or if
10171 signed zeros are involved. */
10172 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10173 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10174 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10176 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10177 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10178 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10179 bool arg0rz = false, arg0iz = false;
10180 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10181 || (arg0i && (arg0iz = real_zerop (arg0i))))
10183 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10184 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10185 if (arg0rz && arg1i && real_zerop (arg1i))
10187 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10188 arg1r ? arg1r
10189 : build1 (REALPART_EXPR, rtype, arg1));
10190 tree ip = arg0i ? arg0i
10191 : build1 (IMAGPART_EXPR, rtype, arg0);
10192 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10194 else if (arg0iz && arg1r && real_zerop (arg1r))
10196 tree rp = arg0r ? arg0r
10197 : build1 (REALPART_EXPR, rtype, arg0);
10198 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10199 arg1i ? arg1i
10200 : build1 (IMAGPART_EXPR, rtype, arg1));
10201 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10206 /* Fold &x - &x. This can happen from &x.foo - &x.
10207 This is unsafe for certain floats even in non-IEEE formats.
10208 In IEEE, it is unsafe because it does wrong for NaNs.
10209 Also note that operand_equal_p is always false if an operand
10210 is volatile. */
10212 if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10213 && operand_equal_p (arg0, arg1, 0))
10214 return build_zero_cst (type);
10216 /* A - B -> A + (-B) if B is easily negatable. */
10217 if (negate_expr_p (arg1)
10218 && ((FLOAT_TYPE_P (type)
10219 /* Avoid this transformation if B is a positive REAL_CST. */
10220 && (TREE_CODE (arg1) != REAL_CST
10221 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10222 || INTEGRAL_TYPE_P (type)))
10223 return fold_build2_loc (loc, PLUS_EXPR, type,
10224 fold_convert_loc (loc, type, arg0),
10225 fold_convert_loc (loc, type,
10226 negate_expr (arg1)));
10228 /* Try folding difference of addresses. */
10230 HOST_WIDE_INT diff;
10232 if ((TREE_CODE (arg0) == ADDR_EXPR
10233 || TREE_CODE (arg1) == ADDR_EXPR)
10234 && ptr_difference_const (arg0, arg1, &diff))
10235 return build_int_cst_type (type, diff);
10238 /* Fold &a[i] - &a[j] to i-j. */
10239 if (TREE_CODE (arg0) == ADDR_EXPR
10240 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10241 && TREE_CODE (arg1) == ADDR_EXPR
10242 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10244 tree aref0 = TREE_OPERAND (arg0, 0);
10245 tree aref1 = TREE_OPERAND (arg1, 0);
10246 if (operand_equal_p (TREE_OPERAND (aref0, 0),
10247 TREE_OPERAND (aref1, 0), 0))
10249 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10250 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10251 tree esz = array_ref_element_size (aref0);
10252 tree diff = build2 (MINUS_EXPR, type, op0, op1);
10253 return fold_build2_loc (loc, MULT_EXPR, type, diff,
10254 fold_convert_loc (loc, type, esz));
10259 if (FLOAT_TYPE_P (type)
10260 && flag_unsafe_math_optimizations
10261 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10262 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10263 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10264 return tem;
10266 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10267 same or one. Make sure type is not saturating.
10268 fold_plusminus_mult_expr will re-associate. */
10269 if ((TREE_CODE (arg0) == MULT_EXPR
10270 || TREE_CODE (arg1) == MULT_EXPR)
10271 && !TYPE_SATURATING (type)
10272 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10274 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10275 if (tem)
10276 return tem;
10279 goto associate;
10281 case MULT_EXPR:
10282 /* (-A) * (-B) -> A * B */
10283 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10284 return fold_build2_loc (loc, MULT_EXPR, type,
10285 fold_convert_loc (loc, type,
10286 TREE_OPERAND (arg0, 0)),
10287 fold_convert_loc (loc, type,
10288 negate_expr (arg1)));
10289 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10290 return fold_build2_loc (loc, MULT_EXPR, type,
10291 fold_convert_loc (loc, type,
10292 negate_expr (arg0)),
10293 fold_convert_loc (loc, type,
10294 TREE_OPERAND (arg1, 0)));
10296 if (! FLOAT_TYPE_P (type))
10298 if (integer_zerop (arg1))
10299 return omit_one_operand_loc (loc, type, arg1, arg0);
10300 if (integer_onep (arg1))
10301 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10302 /* Transform x * -1 into -x. Make sure to do the negation
10303 on the original operand with conversions not stripped
10304 because we can only strip non-sign-changing conversions. */
10305 if (integer_all_onesp (arg1))
10306 return fold_convert_loc (loc, type, negate_expr (op0));
10307 /* Transform x * -C into -x * C if x is easily negatable. */
10308 if (TREE_CODE (arg1) == INTEGER_CST
10309 && tree_int_cst_sgn (arg1) == -1
10310 && negate_expr_p (arg0)
10311 && (tem = negate_expr (arg1)) != arg1
10312 && !TREE_OVERFLOW (tem))
10313 return fold_build2_loc (loc, MULT_EXPR, type,
10314 fold_convert_loc (loc, type,
10315 negate_expr (arg0)),
10316 tem);
10318 /* (a * (1 << b)) is (a << b) */
10319 if (TREE_CODE (arg1) == LSHIFT_EXPR
10320 && integer_onep (TREE_OPERAND (arg1, 0)))
10321 return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10322 TREE_OPERAND (arg1, 1));
10323 if (TREE_CODE (arg0) == LSHIFT_EXPR
10324 && integer_onep (TREE_OPERAND (arg0, 0)))
10325 return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10326 TREE_OPERAND (arg0, 1));
10328 /* (A + A) * C -> A * 2 * C */
10329 if (TREE_CODE (arg0) == PLUS_EXPR
10330 && TREE_CODE (arg1) == INTEGER_CST
10331 && operand_equal_p (TREE_OPERAND (arg0, 0),
10332 TREE_OPERAND (arg0, 1), 0))
10333 return fold_build2_loc (loc, MULT_EXPR, type,
10334 omit_one_operand_loc (loc, type,
10335 TREE_OPERAND (arg0, 0),
10336 TREE_OPERAND (arg0, 1)),
10337 fold_build2_loc (loc, MULT_EXPR, type,
10338 build_int_cst (type, 2) , arg1));
10340 strict_overflow_p = false;
10341 if (TREE_CODE (arg1) == INTEGER_CST
10342 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10343 &strict_overflow_p)))
10345 if (strict_overflow_p)
10346 fold_overflow_warning (("assuming signed overflow does not "
10347 "occur when simplifying "
10348 "multiplication"),
10349 WARN_STRICT_OVERFLOW_MISC);
10350 return fold_convert_loc (loc, type, tem);
10353 /* Optimize z * conj(z) for integer complex numbers. */
10354 if (TREE_CODE (arg0) == CONJ_EXPR
10355 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10356 return fold_mult_zconjz (loc, type, arg1);
10357 if (TREE_CODE (arg1) == CONJ_EXPR
10358 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10359 return fold_mult_zconjz (loc, type, arg0);
10361 else
10363 /* Maybe fold x * 0 to 0. The expressions aren't the same
10364 when x is NaN, since x * 0 is also NaN. Nor are they the
10365 same in modes with signed zeros, since multiplying a
10366 negative value by 0 gives -0, not +0. */
10367 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10368 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10369 && real_zerop (arg1))
10370 return omit_one_operand_loc (loc, type, arg1, arg0);
10371 /* In IEEE floating point, x*1 is not equivalent to x for snans.
10372 Likewise for complex arithmetic with signed zeros. */
10373 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10374 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10375 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10376 && real_onep (arg1))
10377 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10379 /* Transform x * -1.0 into -x. */
10380 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10381 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10382 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10383 && real_minus_onep (arg1))
10384 return fold_convert_loc (loc, type, negate_expr (arg0));
10386 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10387 the result for floating point types due to rounding so it is applied
10388 only if -fassociative-math was specify. */
10389 if (flag_associative_math
10390 && TREE_CODE (arg0) == RDIV_EXPR
10391 && TREE_CODE (arg1) == REAL_CST
10392 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10394 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10395 arg1);
10396 if (tem)
10397 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10398 TREE_OPERAND (arg0, 1));
10401 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10402 if (operand_equal_p (arg0, arg1, 0))
10404 tree tem = fold_strip_sign_ops (arg0);
10405 if (tem != NULL_TREE)
10407 tem = fold_convert_loc (loc, type, tem);
10408 return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
10412 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10413 This is not the same for NaNs or if signed zeros are
10414 involved. */
10415 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10416 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10417 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10418 && TREE_CODE (arg1) == COMPLEX_CST
10419 && real_zerop (TREE_REALPART (arg1)))
10421 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10422 if (real_onep (TREE_IMAGPART (arg1)))
10423 return
10424 fold_build2_loc (loc, COMPLEX_EXPR, type,
10425 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10426 rtype, arg0)),
10427 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10428 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10429 return
10430 fold_build2_loc (loc, COMPLEX_EXPR, type,
10431 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10432 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10433 rtype, arg0)));
10436 /* Optimize z * conj(z) for floating point complex numbers.
10437 Guarded by flag_unsafe_math_optimizations as non-finite
10438 imaginary components don't produce scalar results. */
10439 if (flag_unsafe_math_optimizations
10440 && TREE_CODE (arg0) == CONJ_EXPR
10441 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10442 return fold_mult_zconjz (loc, type, arg1);
10443 if (flag_unsafe_math_optimizations
10444 && TREE_CODE (arg1) == CONJ_EXPR
10445 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10446 return fold_mult_zconjz (loc, type, arg0);
10448 if (flag_unsafe_math_optimizations)
10450 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10451 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10453 /* Optimizations of root(...)*root(...). */
10454 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10456 tree rootfn, arg;
10457 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10458 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10460 /* Optimize sqrt(x)*sqrt(x) as x. */
10461 if (BUILTIN_SQRT_P (fcode0)
10462 && operand_equal_p (arg00, arg10, 0)
10463 && ! HONOR_SNANS (TYPE_MODE (type)))
10464 return arg00;
10466 /* Optimize root(x)*root(y) as root(x*y). */
10467 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10468 arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
10469 return build_call_expr_loc (loc, rootfn, 1, arg);
10472 /* Optimize expN(x)*expN(y) as expN(x+y). */
10473 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10475 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10476 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10477 CALL_EXPR_ARG (arg0, 0),
10478 CALL_EXPR_ARG (arg1, 0));
10479 return build_call_expr_loc (loc, expfn, 1, arg);
10482 /* Optimizations of pow(...)*pow(...). */
10483 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10484 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10485 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10487 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10488 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10489 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10490 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10492 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10493 if (operand_equal_p (arg01, arg11, 0))
10495 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10496 tree arg = fold_build2_loc (loc, MULT_EXPR, type,
10497 arg00, arg10);
10498 return build_call_expr_loc (loc, powfn, 2, arg, arg01);
10501 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10502 if (operand_equal_p (arg00, arg10, 0))
10504 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10505 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
10506 arg01, arg11);
10507 return build_call_expr_loc (loc, powfn, 2, arg00, arg);
10511 /* Optimize tan(x)*cos(x) as sin(x). */
10512 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10513 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10514 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10515 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10516 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10517 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10518 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10519 CALL_EXPR_ARG (arg1, 0), 0))
10521 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10523 if (sinfn != NULL_TREE)
10524 return build_call_expr_loc (loc, sinfn, 1,
10525 CALL_EXPR_ARG (arg0, 0));
10528 /* Optimize x*pow(x,c) as pow(x,c+1). */
10529 if (fcode1 == BUILT_IN_POW
10530 || fcode1 == BUILT_IN_POWF
10531 || fcode1 == BUILT_IN_POWL)
10533 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10534 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10535 if (TREE_CODE (arg11) == REAL_CST
10536 && !TREE_OVERFLOW (arg11)
10537 && operand_equal_p (arg0, arg10, 0))
10539 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10540 REAL_VALUE_TYPE c;
10541 tree arg;
10543 c = TREE_REAL_CST (arg11);
10544 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10545 arg = build_real (type, c);
10546 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10550 /* Optimize pow(x,c)*x as pow(x,c+1). */
10551 if (fcode0 == BUILT_IN_POW
10552 || fcode0 == BUILT_IN_POWF
10553 || fcode0 == BUILT_IN_POWL)
10555 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10556 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10557 if (TREE_CODE (arg01) == REAL_CST
10558 && !TREE_OVERFLOW (arg01)
10559 && operand_equal_p (arg1, arg00, 0))
10561 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10562 REAL_VALUE_TYPE c;
10563 tree arg;
10565 c = TREE_REAL_CST (arg01);
10566 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10567 arg = build_real (type, c);
10568 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
10572 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
10573 if (optimize_function_for_speed_p (cfun)
10574 && operand_equal_p (arg0, arg1, 0))
10576 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10578 if (powfn)
10580 tree arg = build_real (type, dconst2);
10581 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
10586 goto associate;
10588 case BIT_IOR_EXPR:
10589 bit_ior:
10590 if (integer_all_onesp (arg1))
10591 return omit_one_operand_loc (loc, type, arg1, arg0);
10592 if (integer_zerop (arg1))
10593 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10594 if (operand_equal_p (arg0, arg1, 0))
10595 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10597 /* ~X | X is -1. */
10598 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10599 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10601 t1 = build_zero_cst (type);
10602 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10603 return omit_one_operand_loc (loc, type, t1, arg1);
10606 /* X | ~X is -1. */
10607 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10608 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10610 t1 = build_zero_cst (type);
10611 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10612 return omit_one_operand_loc (loc, type, t1, arg0);
10615 /* Canonicalize (X & C1) | C2. */
10616 if (TREE_CODE (arg0) == BIT_AND_EXPR
10617 && TREE_CODE (arg1) == INTEGER_CST
10618 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10620 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10621 int width = TYPE_PRECISION (type), w;
10622 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10623 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10624 hi2 = TREE_INT_CST_HIGH (arg1);
10625 lo2 = TREE_INT_CST_LOW (arg1);
10627 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10628 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10629 return omit_one_operand_loc (loc, type, arg1,
10630 TREE_OPERAND (arg0, 0));
10632 if (width > HOST_BITS_PER_WIDE_INT)
10634 mhi = (unsigned HOST_WIDE_INT) -1
10635 >> (2 * HOST_BITS_PER_WIDE_INT - width);
10636 mlo = -1;
10638 else
10640 mhi = 0;
10641 mlo = (unsigned HOST_WIDE_INT) -1
10642 >> (HOST_BITS_PER_WIDE_INT - width);
10645 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10646 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10647 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10648 TREE_OPERAND (arg0, 0), arg1);
10650 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10651 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10652 mode which allows further optimizations. */
10653 hi1 &= mhi;
10654 lo1 &= mlo;
10655 hi2 &= mhi;
10656 lo2 &= mlo;
10657 hi3 = hi1 & ~hi2;
10658 lo3 = lo1 & ~lo2;
10659 for (w = BITS_PER_UNIT;
10660 w <= width && w <= HOST_BITS_PER_WIDE_INT;
10661 w <<= 1)
10663 unsigned HOST_WIDE_INT mask
10664 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10665 if (((lo1 | lo2) & mask) == mask
10666 && (lo1 & ~mask) == 0 && hi1 == 0)
10668 hi3 = 0;
10669 lo3 = mask;
10670 break;
10673 if (hi3 != hi1 || lo3 != lo1)
10674 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10675 fold_build2_loc (loc, BIT_AND_EXPR, type,
10676 TREE_OPERAND (arg0, 0),
10677 build_int_cst_wide (type,
10678 lo3, hi3)),
10679 arg1);
10682 /* (X & Y) | Y is (X, Y). */
10683 if (TREE_CODE (arg0) == BIT_AND_EXPR
10684 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10685 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10686 /* (X & Y) | X is (Y, X). */
10687 if (TREE_CODE (arg0) == BIT_AND_EXPR
10688 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10689 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10690 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10691 /* X | (X & Y) is (Y, X). */
10692 if (TREE_CODE (arg1) == BIT_AND_EXPR
10693 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10694 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10695 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10696 /* X | (Y & X) is (Y, X). */
10697 if (TREE_CODE (arg1) == BIT_AND_EXPR
10698 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10699 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10700 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10702 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
10703 if (t1 != NULL_TREE)
10704 return t1;
10706 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10708 This results in more efficient code for machines without a NAND
10709 instruction. Combine will canonicalize to the first form
10710 which will allow use of NAND instructions provided by the
10711 backend if they exist. */
10712 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10713 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10715 return
10716 fold_build1_loc (loc, BIT_NOT_EXPR, type,
10717 build2 (BIT_AND_EXPR, type,
10718 fold_convert_loc (loc, type,
10719 TREE_OPERAND (arg0, 0)),
10720 fold_convert_loc (loc, type,
10721 TREE_OPERAND (arg1, 0))));
10724 /* See if this can be simplified into a rotate first. If that
10725 is unsuccessful continue in the association code. */
10726 goto bit_rotate;
10728 case BIT_XOR_EXPR:
10729 if (integer_zerop (arg1))
10730 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10731 if (integer_all_onesp (arg1))
10732 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
10733 if (operand_equal_p (arg0, arg1, 0))
10734 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10736 /* ~X ^ X is -1. */
10737 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10738 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10740 t1 = build_zero_cst (type);
10741 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10742 return omit_one_operand_loc (loc, type, t1, arg1);
10745 /* X ^ ~X is -1. */
10746 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10747 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10749 t1 = build_zero_cst (type);
10750 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
10751 return omit_one_operand_loc (loc, type, t1, arg0);
10754 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10755 with a constant, and the two constants have no bits in common,
10756 we should treat this as a BIT_IOR_EXPR since this may produce more
10757 simplifications. */
10758 if (TREE_CODE (arg0) == BIT_AND_EXPR
10759 && TREE_CODE (arg1) == BIT_AND_EXPR
10760 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10761 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10762 && integer_zerop (const_binop (BIT_AND_EXPR,
10763 TREE_OPERAND (arg0, 1),
10764 TREE_OPERAND (arg1, 1))))
10766 code = BIT_IOR_EXPR;
10767 goto bit_ior;
10770 /* (X | Y) ^ X -> Y & ~ X*/
10771 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10772 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10774 tree t2 = TREE_OPERAND (arg0, 1);
10775 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10776 arg1);
10777 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10778 fold_convert_loc (loc, type, t2),
10779 fold_convert_loc (loc, type, t1));
10780 return t1;
10783 /* (Y | X) ^ X -> Y & ~ X*/
10784 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10785 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10787 tree t2 = TREE_OPERAND (arg0, 0);
10788 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
10789 arg1);
10790 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10791 fold_convert_loc (loc, type, t2),
10792 fold_convert_loc (loc, type, t1));
10793 return t1;
10796 /* X ^ (X | Y) -> Y & ~ X*/
10797 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10798 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10800 tree t2 = TREE_OPERAND (arg1, 1);
10801 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10802 arg0);
10803 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10804 fold_convert_loc (loc, type, t2),
10805 fold_convert_loc (loc, type, t1));
10806 return t1;
10809 /* X ^ (Y | X) -> Y & ~ X*/
10810 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10811 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10813 tree t2 = TREE_OPERAND (arg1, 0);
10814 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
10815 arg0);
10816 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
10817 fold_convert_loc (loc, type, t2),
10818 fold_convert_loc (loc, type, t1));
10819 return t1;
10822 /* Convert ~X ^ ~Y to X ^ Y. */
10823 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10824 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10825 return fold_build2_loc (loc, code, type,
10826 fold_convert_loc (loc, type,
10827 TREE_OPERAND (arg0, 0)),
10828 fold_convert_loc (loc, type,
10829 TREE_OPERAND (arg1, 0)));
10831 /* Convert ~X ^ C to X ^ ~C. */
10832 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10833 && TREE_CODE (arg1) == INTEGER_CST)
10834 return fold_build2_loc (loc, code, type,
10835 fold_convert_loc (loc, type,
10836 TREE_OPERAND (arg0, 0)),
10837 fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
10839 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10840 if (TREE_CODE (arg0) == BIT_AND_EXPR
10841 && integer_onep (TREE_OPERAND (arg0, 1))
10842 && integer_onep (arg1))
10843 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10844 build_int_cst (TREE_TYPE (arg0), 0));
10846 /* Fold (X & Y) ^ Y as ~X & Y. */
10847 if (TREE_CODE (arg0) == BIT_AND_EXPR
10848 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10850 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10851 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10852 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10853 fold_convert_loc (loc, type, arg1));
10855 /* Fold (X & Y) ^ X as ~Y & X. */
10856 if (TREE_CODE (arg0) == BIT_AND_EXPR
10857 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10858 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10860 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10861 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10862 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10863 fold_convert_loc (loc, type, arg1));
10865 /* Fold X ^ (X & Y) as X & ~Y. */
10866 if (TREE_CODE (arg1) == BIT_AND_EXPR
10867 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10869 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10870 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10871 fold_convert_loc (loc, type, arg0),
10872 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10874 /* Fold X ^ (Y & X) as ~Y & X. */
10875 if (TREE_CODE (arg1) == BIT_AND_EXPR
10876 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10877 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10879 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10880 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10881 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10882 fold_convert_loc (loc, type, arg0));
10885 /* See if this can be simplified into a rotate first. If that
10886 is unsuccessful continue in the association code. */
10887 goto bit_rotate;
10889 case BIT_AND_EXPR:
10890 if (integer_all_onesp (arg1))
10891 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10892 if (integer_zerop (arg1))
10893 return omit_one_operand_loc (loc, type, arg1, arg0);
10894 if (operand_equal_p (arg0, arg1, 0))
10895 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10897 /* ~X & X is always zero. */
10898 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10899 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10900 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10902 /* X & ~X is always zero. */
10903 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10904 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10905 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10907 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
10908 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10909 && TREE_CODE (arg1) == INTEGER_CST
10910 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10912 tree tmp1 = fold_convert_loc (loc, type, arg1);
10913 tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10914 tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10915 tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
10916 tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
10917 return
10918 fold_convert_loc (loc, type,
10919 fold_build2_loc (loc, BIT_IOR_EXPR,
10920 type, tmp2, tmp3));
10923 /* (X | Y) & Y is (X, Y). */
10924 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10925 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10926 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
10927 /* (X | Y) & X is (Y, X). */
10928 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10929 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10930 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10931 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
10932 /* X & (X | Y) is (Y, X). */
10933 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10934 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10935 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10936 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
10937 /* X & (Y | X) is (Y, X). */
10938 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10939 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10940 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10941 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
10943 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10944 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10945 && integer_onep (TREE_OPERAND (arg0, 1))
10946 && integer_onep (arg1))
10948 tem = TREE_OPERAND (arg0, 0);
10949 return fold_build2_loc (loc, EQ_EXPR, type,
10950 fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
10951 build_int_cst (TREE_TYPE (tem), 1)),
10952 build_int_cst (TREE_TYPE (tem), 0));
10954 /* Fold ~X & 1 as (X & 1) == 0. */
10955 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10956 && integer_onep (arg1))
10958 tem = TREE_OPERAND (arg0, 0);
10959 return fold_build2_loc (loc, EQ_EXPR, type,
10960 fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
10961 build_int_cst (TREE_TYPE (tem), 1)),
10962 build_int_cst (TREE_TYPE (tem), 0));
10965 /* Fold (X ^ Y) & Y as ~X & Y. */
10966 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10967 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10969 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10970 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10971 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10972 fold_convert_loc (loc, type, arg1));
10974 /* Fold (X ^ Y) & X as ~Y & X. */
10975 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10976 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10977 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10979 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10980 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10981 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10982 fold_convert_loc (loc, type, arg1));
10984 /* Fold X & (X ^ Y) as X & ~Y. */
10985 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10986 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10988 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10989 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10990 fold_convert_loc (loc, type, arg0),
10991 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10993 /* Fold X & (Y ^ X) as ~Y & X. */
10994 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10995 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10996 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10998 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10999 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11000 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11001 fold_convert_loc (loc, type, arg0));
11004 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11005 ((A & N) + B) & M -> (A + B) & M
11006 Similarly if (N & M) == 0,
11007 ((A | N) + B) & M -> (A + B) & M
11008 and for - instead of + (or unary - instead of +)
11009 and/or ^ instead of |.
11010 If B is constant and (B & M) == 0, fold into A & M. */
11011 if (host_integerp (arg1, 1))
11013 unsigned HOST_WIDE_INT cst1 = tree_low_cst (arg1, 1);
11014 if (~cst1 && (cst1 & (cst1 + 1)) == 0
11015 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11016 && (TREE_CODE (arg0) == PLUS_EXPR
11017 || TREE_CODE (arg0) == MINUS_EXPR
11018 || TREE_CODE (arg0) == NEGATE_EXPR)
11019 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
11020 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
11022 tree pmop[2];
11023 int which = 0;
11024 unsigned HOST_WIDE_INT cst0;
11026 /* Now we know that arg0 is (C + D) or (C - D) or
11027 -C and arg1 (M) is == (1LL << cst) - 1.
11028 Store C into PMOP[0] and D into PMOP[1]. */
11029 pmop[0] = TREE_OPERAND (arg0, 0);
11030 pmop[1] = NULL;
11031 if (TREE_CODE (arg0) != NEGATE_EXPR)
11033 pmop[1] = TREE_OPERAND (arg0, 1);
11034 which = 1;
11037 if (!host_integerp (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11038 || (tree_low_cst (TYPE_MAX_VALUE (TREE_TYPE (arg0)), 1)
11039 & cst1) != cst1)
11040 which = -1;
11042 for (; which >= 0; which--)
11043 switch (TREE_CODE (pmop[which]))
11045 case BIT_AND_EXPR:
11046 case BIT_IOR_EXPR:
11047 case BIT_XOR_EXPR:
11048 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
11049 != INTEGER_CST)
11050 break;
11051 /* tree_low_cst not used, because we don't care about
11052 the upper bits. */
11053 cst0 = TREE_INT_CST_LOW (TREE_OPERAND (pmop[which], 1));
11054 cst0 &= cst1;
11055 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
11057 if (cst0 != cst1)
11058 break;
11060 else if (cst0 != 0)
11061 break;
11062 /* If C or D is of the form (A & N) where
11063 (N & M) == M, or of the form (A | N) or
11064 (A ^ N) where (N & M) == 0, replace it with A. */
11065 pmop[which] = TREE_OPERAND (pmop[which], 0);
11066 break;
11067 case INTEGER_CST:
11068 /* If C or D is a N where (N & M) == 0, it can be
11069 omitted (assumed 0). */
11070 if ((TREE_CODE (arg0) == PLUS_EXPR
11071 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
11072 && (TREE_INT_CST_LOW (pmop[which]) & cst1) == 0)
11073 pmop[which] = NULL;
11074 break;
11075 default:
11076 break;
11079 /* Only build anything new if we optimized one or both arguments
11080 above. */
11081 if (pmop[0] != TREE_OPERAND (arg0, 0)
11082 || (TREE_CODE (arg0) != NEGATE_EXPR
11083 && pmop[1] != TREE_OPERAND (arg0, 1)))
11085 tree utype = TREE_TYPE (arg0);
11086 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11088 /* Perform the operations in a type that has defined
11089 overflow behavior. */
11090 utype = unsigned_type_for (TREE_TYPE (arg0));
11091 if (pmop[0] != NULL)
11092 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
11093 if (pmop[1] != NULL)
11094 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
11097 if (TREE_CODE (arg0) == NEGATE_EXPR)
11098 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
11099 else if (TREE_CODE (arg0) == PLUS_EXPR)
11101 if (pmop[0] != NULL && pmop[1] != NULL)
11102 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
11103 pmop[0], pmop[1]);
11104 else if (pmop[0] != NULL)
11105 tem = pmop[0];
11106 else if (pmop[1] != NULL)
11107 tem = pmop[1];
11108 else
11109 return build_int_cst (type, 0);
11111 else if (pmop[0] == NULL)
11112 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
11113 else
11114 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
11115 pmop[0], pmop[1]);
11116 /* TEM is now the new binary +, - or unary - replacement. */
11117 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
11118 fold_convert_loc (loc, utype, arg1));
11119 return fold_convert_loc (loc, type, tem);
11124 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11125 if (t1 != NULL_TREE)
11126 return t1;
11127 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11128 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11129 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11131 unsigned int prec
11132 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11134 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11135 && (~TREE_INT_CST_LOW (arg1)
11136 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11137 return
11138 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11141 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11143 This results in more efficient code for machines without a NOR
11144 instruction. Combine will canonicalize to the first form
11145 which will allow use of NOR instructions provided by the
11146 backend if they exist. */
11147 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11148 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11150 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11151 build2 (BIT_IOR_EXPR, type,
11152 fold_convert_loc (loc, type,
11153 TREE_OPERAND (arg0, 0)),
11154 fold_convert_loc (loc, type,
11155 TREE_OPERAND (arg1, 0))));
11158 /* If arg0 is derived from the address of an object or function, we may
11159 be able to fold this expression using the object or function's
11160 alignment. */
11161 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11163 unsigned HOST_WIDE_INT modulus, residue;
11164 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11166 modulus = get_pointer_modulus_and_residue (arg0, &residue,
11167 integer_onep (arg1));
11169 /* This works because modulus is a power of 2. If this weren't the
11170 case, we'd have to replace it by its greatest power-of-2
11171 divisor: modulus & -modulus. */
11172 if (low < modulus)
11173 return build_int_cst (type, residue & low);
11176 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11177 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11178 if the new mask might be further optimized. */
11179 if ((TREE_CODE (arg0) == LSHIFT_EXPR
11180 || TREE_CODE (arg0) == RSHIFT_EXPR)
11181 && host_integerp (TREE_OPERAND (arg0, 1), 1)
11182 && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11183 && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11184 < TYPE_PRECISION (TREE_TYPE (arg0))
11185 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11186 && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11188 unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11189 unsigned HOST_WIDE_INT mask
11190 = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11191 unsigned HOST_WIDE_INT newmask, zerobits = 0;
11192 tree shift_type = TREE_TYPE (arg0);
11194 if (TREE_CODE (arg0) == LSHIFT_EXPR)
11195 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11196 else if (TREE_CODE (arg0) == RSHIFT_EXPR
11197 && TYPE_PRECISION (TREE_TYPE (arg0))
11198 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11200 unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11201 tree arg00 = TREE_OPERAND (arg0, 0);
11202 /* See if more bits can be proven as zero because of
11203 zero extension. */
11204 if (TREE_CODE (arg00) == NOP_EXPR
11205 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11207 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11208 if (TYPE_PRECISION (inner_type)
11209 == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11210 && TYPE_PRECISION (inner_type) < prec)
11212 prec = TYPE_PRECISION (inner_type);
11213 /* See if we can shorten the right shift. */
11214 if (shiftc < prec)
11215 shift_type = inner_type;
11218 zerobits = ~(unsigned HOST_WIDE_INT) 0;
11219 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11220 zerobits <<= prec - shiftc;
11221 /* For arithmetic shift if sign bit could be set, zerobits
11222 can contain actually sign bits, so no transformation is
11223 possible, unless MASK masks them all away. In that
11224 case the shift needs to be converted into logical shift. */
11225 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11226 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11228 if ((mask & zerobits) == 0)
11229 shift_type = unsigned_type_for (TREE_TYPE (arg0));
11230 else
11231 zerobits = 0;
11235 /* ((X << 16) & 0xff00) is (X, 0). */
11236 if ((mask & zerobits) == mask)
11237 return omit_one_operand_loc (loc, type,
11238 build_int_cst (type, 0), arg0);
11240 newmask = mask | zerobits;
11241 if (newmask != mask && (newmask & (newmask + 1)) == 0)
11243 unsigned int prec;
11245 /* Only do the transformation if NEWMASK is some integer
11246 mode's mask. */
11247 for (prec = BITS_PER_UNIT;
11248 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11249 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11250 break;
11251 if (prec < HOST_BITS_PER_WIDE_INT
11252 || newmask == ~(unsigned HOST_WIDE_INT) 0)
11254 tree newmaskt;
11256 if (shift_type != TREE_TYPE (arg0))
11258 tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11259 fold_convert_loc (loc, shift_type,
11260 TREE_OPERAND (arg0, 0)),
11261 TREE_OPERAND (arg0, 1));
11262 tem = fold_convert_loc (loc, type, tem);
11264 else
11265 tem = op0;
11266 newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11267 if (!tree_int_cst_equal (newmaskt, arg1))
11268 return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11273 goto associate;
11275 case RDIV_EXPR:
11276 /* Don't touch a floating-point divide by zero unless the mode
11277 of the constant can represent infinity. */
11278 if (TREE_CODE (arg1) == REAL_CST
11279 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11280 && real_zerop (arg1))
11281 return NULL_TREE;
11283 /* Optimize A / A to 1.0 if we don't care about
11284 NaNs or Infinities. Skip the transformation
11285 for non-real operands. */
11286 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11287 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11288 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11289 && operand_equal_p (arg0, arg1, 0))
11291 tree r = build_real (TREE_TYPE (arg0), dconst1);
11293 return omit_two_operands_loc (loc, type, r, arg0, arg1);
11296 /* The complex version of the above A / A optimization. */
11297 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11298 && operand_equal_p (arg0, arg1, 0))
11300 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11301 if (! HONOR_NANS (TYPE_MODE (elem_type))
11302 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11304 tree r = build_real (elem_type, dconst1);
11305 /* omit_two_operands will call fold_convert for us. */
11306 return omit_two_operands_loc (loc, type, r, arg0, arg1);
11310 /* (-A) / (-B) -> A / B */
11311 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11312 return fold_build2_loc (loc, RDIV_EXPR, type,
11313 TREE_OPERAND (arg0, 0),
11314 negate_expr (arg1));
11315 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11316 return fold_build2_loc (loc, RDIV_EXPR, type,
11317 negate_expr (arg0),
11318 TREE_OPERAND (arg1, 0));
11320 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
11321 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11322 && real_onep (arg1))
11323 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11325 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
11326 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11327 && real_minus_onep (arg1))
11328 return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11329 negate_expr (arg0)));
11331 /* If ARG1 is a constant, we can convert this to a multiply by the
11332 reciprocal. This does not have the same rounding properties,
11333 so only do this if -freciprocal-math. We can actually
11334 always safely do it if ARG1 is a power of two, but it's hard to
11335 tell if it is or not in a portable manner. */
11336 if (TREE_CODE (arg1) == REAL_CST)
11338 if (flag_reciprocal_math
11339 && 0 != (tem = const_binop (code, build_real (type, dconst1),
11340 arg1)))
11341 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11342 /* Find the reciprocal if optimizing and the result is exact. */
11343 if (optimize)
11345 REAL_VALUE_TYPE r;
11346 r = TREE_REAL_CST (arg1);
11347 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11349 tem = build_real (type, r);
11350 return fold_build2_loc (loc, MULT_EXPR, type,
11351 fold_convert_loc (loc, type, arg0), tem);
11355 /* Convert A/B/C to A/(B*C). */
11356 if (flag_reciprocal_math
11357 && TREE_CODE (arg0) == RDIV_EXPR)
11358 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11359 fold_build2_loc (loc, MULT_EXPR, type,
11360 TREE_OPERAND (arg0, 1), arg1));
11362 /* Convert A/(B/C) to (A/B)*C. */
11363 if (flag_reciprocal_math
11364 && TREE_CODE (arg1) == RDIV_EXPR)
11365 return fold_build2_loc (loc, MULT_EXPR, type,
11366 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11367 TREE_OPERAND (arg1, 0)),
11368 TREE_OPERAND (arg1, 1));
11370 /* Convert C1/(X*C2) into (C1/C2)/X. */
11371 if (flag_reciprocal_math
11372 && TREE_CODE (arg1) == MULT_EXPR
11373 && TREE_CODE (arg0) == REAL_CST
11374 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11376 tree tem = const_binop (RDIV_EXPR, arg0,
11377 TREE_OPERAND (arg1, 1));
11378 if (tem)
11379 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11380 TREE_OPERAND (arg1, 0));
11383 if (flag_unsafe_math_optimizations)
11385 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11386 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11388 /* Optimize sin(x)/cos(x) as tan(x). */
11389 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11390 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11391 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11392 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11393 CALL_EXPR_ARG (arg1, 0), 0))
11395 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11397 if (tanfn != NULL_TREE)
11398 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11401 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11402 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11403 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11404 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11405 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11406 CALL_EXPR_ARG (arg1, 0), 0))
11408 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11410 if (tanfn != NULL_TREE)
11412 tree tmp = build_call_expr_loc (loc, tanfn, 1,
11413 CALL_EXPR_ARG (arg0, 0));
11414 return fold_build2_loc (loc, RDIV_EXPR, type,
11415 build_real (type, dconst1), tmp);
11419 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11420 NaNs or Infinities. */
11421 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11422 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11423 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11425 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11426 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11428 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11429 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11430 && operand_equal_p (arg00, arg01, 0))
11432 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11434 if (cosfn != NULL_TREE)
11435 return build_call_expr_loc (loc, cosfn, 1, arg00);
11439 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11440 NaNs or Infinities. */
11441 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11442 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11443 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11445 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11446 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11448 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11449 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11450 && operand_equal_p (arg00, arg01, 0))
11452 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11454 if (cosfn != NULL_TREE)
11456 tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
11457 return fold_build2_loc (loc, RDIV_EXPR, type,
11458 build_real (type, dconst1),
11459 tmp);
11464 /* Optimize pow(x,c)/x as pow(x,c-1). */
11465 if (fcode0 == BUILT_IN_POW
11466 || fcode0 == BUILT_IN_POWF
11467 || fcode0 == BUILT_IN_POWL)
11469 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11470 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11471 if (TREE_CODE (arg01) == REAL_CST
11472 && !TREE_OVERFLOW (arg01)
11473 && operand_equal_p (arg1, arg00, 0))
11475 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11476 REAL_VALUE_TYPE c;
11477 tree arg;
11479 c = TREE_REAL_CST (arg01);
11480 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11481 arg = build_real (type, c);
11482 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11486 /* Optimize a/root(b/c) into a*root(c/b). */
11487 if (BUILTIN_ROOT_P (fcode1))
11489 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11491 if (TREE_CODE (rootarg) == RDIV_EXPR)
11493 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11494 tree b = TREE_OPERAND (rootarg, 0);
11495 tree c = TREE_OPERAND (rootarg, 1);
11497 tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
11499 tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
11500 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
11504 /* Optimize x/expN(y) into x*expN(-y). */
11505 if (BUILTIN_EXPONENT_P (fcode1))
11507 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11508 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11509 arg1 = build_call_expr_loc (loc,
11510 expfn, 1,
11511 fold_convert_loc (loc, type, arg));
11512 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11515 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11516 if (fcode1 == BUILT_IN_POW
11517 || fcode1 == BUILT_IN_POWF
11518 || fcode1 == BUILT_IN_POWL)
11520 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11521 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11522 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11523 tree neg11 = fold_convert_loc (loc, type,
11524 negate_expr (arg11));
11525 arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
11526 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11529 return NULL_TREE;
11531 case TRUNC_DIV_EXPR:
11532 /* Optimize (X & (-A)) / A where A is a power of 2,
11533 to X >> log2(A) */
11534 if (TREE_CODE (arg0) == BIT_AND_EXPR
11535 && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
11536 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
11538 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
11539 arg1, TREE_OPERAND (arg0, 1));
11540 if (sum && integer_zerop (sum)) {
11541 unsigned long pow2;
11543 if (TREE_INT_CST_LOW (arg1))
11544 pow2 = exact_log2 (TREE_INT_CST_LOW (arg1));
11545 else
11546 pow2 = exact_log2 (TREE_INT_CST_HIGH (arg1))
11547 + HOST_BITS_PER_WIDE_INT;
11549 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11550 TREE_OPERAND (arg0, 0),
11551 build_int_cst (NULL_TREE, pow2));
11555 /* Fall thru */
11557 case FLOOR_DIV_EXPR:
11558 /* Simplify A / (B << N) where A and B are positive and B is
11559 a power of 2, to A >> (N + log2(B)). */
11560 strict_overflow_p = false;
11561 if (TREE_CODE (arg1) == LSHIFT_EXPR
11562 && (TYPE_UNSIGNED (type)
11563 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11565 tree sval = TREE_OPERAND (arg1, 0);
11566 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11568 tree sh_cnt = TREE_OPERAND (arg1, 1);
11569 unsigned long pow2;
11571 if (TREE_INT_CST_LOW (sval))
11572 pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11573 else
11574 pow2 = exact_log2 (TREE_INT_CST_HIGH (sval))
11575 + HOST_BITS_PER_WIDE_INT;
11577 if (strict_overflow_p)
11578 fold_overflow_warning (("assuming signed overflow does not "
11579 "occur when simplifying A / (B << N)"),
11580 WARN_STRICT_OVERFLOW_MISC);
11582 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11583 sh_cnt, build_int_cst (NULL_TREE, pow2));
11584 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11585 fold_convert_loc (loc, type, arg0), sh_cnt);
11589 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11590 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
11591 if (INTEGRAL_TYPE_P (type)
11592 && TYPE_UNSIGNED (type)
11593 && code == FLOOR_DIV_EXPR)
11594 return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
11596 /* Fall thru */
11598 case ROUND_DIV_EXPR:
11599 case CEIL_DIV_EXPR:
11600 case EXACT_DIV_EXPR:
11601 if (integer_onep (arg1))
11602 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11603 if (integer_zerop (arg1))
11604 return NULL_TREE;
11605 /* X / -1 is -X. */
11606 if (!TYPE_UNSIGNED (type)
11607 && TREE_CODE (arg1) == INTEGER_CST
11608 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11609 && TREE_INT_CST_HIGH (arg1) == -1)
11610 return fold_convert_loc (loc, type, negate_expr (arg0));
11612 /* Convert -A / -B to A / B when the type is signed and overflow is
11613 undefined. */
11614 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11615 && TREE_CODE (arg0) == NEGATE_EXPR
11616 && negate_expr_p (arg1))
11618 if (INTEGRAL_TYPE_P (type))
11619 fold_overflow_warning (("assuming signed overflow does not occur "
11620 "when distributing negation across "
11621 "division"),
11622 WARN_STRICT_OVERFLOW_MISC);
11623 return fold_build2_loc (loc, code, type,
11624 fold_convert_loc (loc, type,
11625 TREE_OPERAND (arg0, 0)),
11626 fold_convert_loc (loc, type,
11627 negate_expr (arg1)));
11629 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11630 && TREE_CODE (arg1) == NEGATE_EXPR
11631 && negate_expr_p (arg0))
11633 if (INTEGRAL_TYPE_P (type))
11634 fold_overflow_warning (("assuming signed overflow does not occur "
11635 "when distributing negation across "
11636 "division"),
11637 WARN_STRICT_OVERFLOW_MISC);
11638 return fold_build2_loc (loc, code, type,
11639 fold_convert_loc (loc, type,
11640 negate_expr (arg0)),
11641 fold_convert_loc (loc, type,
11642 TREE_OPERAND (arg1, 0)));
11645 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11646 operation, EXACT_DIV_EXPR.
11648 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11649 At one time others generated faster code, it's not clear if they do
11650 after the last round to changes to the DIV code in expmed.c. */
11651 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11652 && multiple_of_p (type, arg0, arg1))
11653 return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
11655 strict_overflow_p = false;
11656 if (TREE_CODE (arg1) == INTEGER_CST
11657 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11658 &strict_overflow_p)))
11660 if (strict_overflow_p)
11661 fold_overflow_warning (("assuming signed overflow does not occur "
11662 "when simplifying division"),
11663 WARN_STRICT_OVERFLOW_MISC);
11664 return fold_convert_loc (loc, type, tem);
11667 return NULL_TREE;
11669 case CEIL_MOD_EXPR:
11670 case FLOOR_MOD_EXPR:
11671 case ROUND_MOD_EXPR:
11672 case TRUNC_MOD_EXPR:
11673 /* X % 1 is always zero, but be sure to preserve any side
11674 effects in X. */
11675 if (integer_onep (arg1))
11676 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11678 /* X % 0, return X % 0 unchanged so that we can get the
11679 proper warnings and errors. */
11680 if (integer_zerop (arg1))
11681 return NULL_TREE;
11683 /* 0 % X is always zero, but be sure to preserve any side
11684 effects in X. Place this after checking for X == 0. */
11685 if (integer_zerop (arg0))
11686 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11688 /* X % -1 is zero. */
11689 if (!TYPE_UNSIGNED (type)
11690 && TREE_CODE (arg1) == INTEGER_CST
11691 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11692 && TREE_INT_CST_HIGH (arg1) == -1)
11693 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11695 /* X % -C is the same as X % C. */
11696 if (code == TRUNC_MOD_EXPR
11697 && !TYPE_UNSIGNED (type)
11698 && TREE_CODE (arg1) == INTEGER_CST
11699 && !TREE_OVERFLOW (arg1)
11700 && TREE_INT_CST_HIGH (arg1) < 0
11701 && !TYPE_OVERFLOW_TRAPS (type)
11702 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
11703 && !sign_bit_p (arg1, arg1))
11704 return fold_build2_loc (loc, code, type,
11705 fold_convert_loc (loc, type, arg0),
11706 fold_convert_loc (loc, type,
11707 negate_expr (arg1)));
11709 /* X % -Y is the same as X % Y. */
11710 if (code == TRUNC_MOD_EXPR
11711 && !TYPE_UNSIGNED (type)
11712 && TREE_CODE (arg1) == NEGATE_EXPR
11713 && !TYPE_OVERFLOW_TRAPS (type))
11714 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
11715 fold_convert_loc (loc, type,
11716 TREE_OPERAND (arg1, 0)));
11718 strict_overflow_p = false;
11719 if (TREE_CODE (arg1) == INTEGER_CST
11720 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11721 &strict_overflow_p)))
11723 if (strict_overflow_p)
11724 fold_overflow_warning (("assuming signed overflow does not occur "
11725 "when simplifying modulus"),
11726 WARN_STRICT_OVERFLOW_MISC);
11727 return fold_convert_loc (loc, type, tem);
11730 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11731 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
11732 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11733 && (TYPE_UNSIGNED (type)
11734 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11736 tree c = arg1;
11737 /* Also optimize A % (C << N) where C is a power of 2,
11738 to A & ((C << N) - 1). */
11739 if (TREE_CODE (arg1) == LSHIFT_EXPR)
11740 c = TREE_OPERAND (arg1, 0);
11742 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11744 tree mask
11745 = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
11746 build_int_cst (TREE_TYPE (arg1), 1));
11747 if (strict_overflow_p)
11748 fold_overflow_warning (("assuming signed overflow does not "
11749 "occur when simplifying "
11750 "X % (power of two)"),
11751 WARN_STRICT_OVERFLOW_MISC);
11752 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11753 fold_convert_loc (loc, type, arg0),
11754 fold_convert_loc (loc, type, mask));
11758 return NULL_TREE;
11760 case LROTATE_EXPR:
11761 case RROTATE_EXPR:
11762 if (integer_all_onesp (arg0))
11763 return omit_one_operand_loc (loc, type, arg0, arg1);
11764 goto shift;
11766 case RSHIFT_EXPR:
11767 /* Optimize -1 >> x for arithmetic right shifts. */
11768 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11769 && tree_expr_nonnegative_p (arg1))
11770 return omit_one_operand_loc (loc, type, arg0, arg1);
11771 /* ... fall through ... */
11773 case LSHIFT_EXPR:
11774 shift:
11775 if (integer_zerop (arg1))
11776 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11777 if (integer_zerop (arg0))
11778 return omit_one_operand_loc (loc, type, arg0, arg1);
11780 /* Since negative shift count is not well-defined,
11781 don't try to compute it in the compiler. */
11782 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11783 return NULL_TREE;
11785 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11786 if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11787 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11788 && host_integerp (TREE_OPERAND (arg0, 1), false)
11789 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11791 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11792 + TREE_INT_CST_LOW (arg1));
11794 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11795 being well defined. */
11796 if (low >= TYPE_PRECISION (type))
11798 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11799 low = low % TYPE_PRECISION (type);
11800 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11801 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
11802 TREE_OPERAND (arg0, 0));
11803 else
11804 low = TYPE_PRECISION (type) - 1;
11807 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
11808 build_int_cst (type, low));
11811 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11812 into x & ((unsigned)-1 >> c) for unsigned types. */
11813 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11814 || (TYPE_UNSIGNED (type)
11815 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11816 && host_integerp (arg1, false)
11817 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11818 && host_integerp (TREE_OPERAND (arg0, 1), false)
11819 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11821 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11822 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11823 tree lshift;
11824 tree arg00;
11826 if (low0 == low1)
11828 arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11830 lshift = build_int_cst (type, -1);
11831 lshift = int_const_binop (code, lshift, arg1, 0);
11833 return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
11837 /* Rewrite an LROTATE_EXPR by a constant into an
11838 RROTATE_EXPR by a new constant. */
11839 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11841 tree tem = build_int_cst (TREE_TYPE (arg1),
11842 TYPE_PRECISION (type));
11843 tem = const_binop (MINUS_EXPR, tem, arg1);
11844 return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
11847 /* If we have a rotate of a bit operation with the rotate count and
11848 the second operand of the bit operation both constant,
11849 permute the two operations. */
11850 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11851 && (TREE_CODE (arg0) == BIT_AND_EXPR
11852 || TREE_CODE (arg0) == BIT_IOR_EXPR
11853 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11854 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11855 return fold_build2_loc (loc, TREE_CODE (arg0), type,
11856 fold_build2_loc (loc, code, type,
11857 TREE_OPERAND (arg0, 0), arg1),
11858 fold_build2_loc (loc, code, type,
11859 TREE_OPERAND (arg0, 1), arg1));
11861 /* Two consecutive rotates adding up to the precision of the
11862 type can be ignored. */
11863 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11864 && TREE_CODE (arg0) == RROTATE_EXPR
11865 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11866 && TREE_INT_CST_HIGH (arg1) == 0
11867 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11868 && ((TREE_INT_CST_LOW (arg1)
11869 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11870 == (unsigned int) TYPE_PRECISION (type)))
11871 return TREE_OPERAND (arg0, 0);
11873 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11874 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11875 if the latter can be further optimized. */
11876 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11877 && TREE_CODE (arg0) == BIT_AND_EXPR
11878 && TREE_CODE (arg1) == INTEGER_CST
11879 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11881 tree mask = fold_build2_loc (loc, code, type,
11882 fold_convert_loc (loc, type,
11883 TREE_OPERAND (arg0, 1)),
11884 arg1);
11885 tree shift = fold_build2_loc (loc, code, type,
11886 fold_convert_loc (loc, type,
11887 TREE_OPERAND (arg0, 0)),
11888 arg1);
11889 tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
11890 if (tem)
11891 return tem;
11894 return NULL_TREE;
11896 case MIN_EXPR:
11897 if (operand_equal_p (arg0, arg1, 0))
11898 return omit_one_operand_loc (loc, type, arg0, arg1);
11899 if (INTEGRAL_TYPE_P (type)
11900 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11901 return omit_one_operand_loc (loc, type, arg1, arg0);
11902 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
11903 if (tem)
11904 return tem;
11905 goto associate;
11907 case MAX_EXPR:
11908 if (operand_equal_p (arg0, arg1, 0))
11909 return omit_one_operand_loc (loc, type, arg0, arg1);
11910 if (INTEGRAL_TYPE_P (type)
11911 && TYPE_MAX_VALUE (type)
11912 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11913 return omit_one_operand_loc (loc, type, arg1, arg0);
11914 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
11915 if (tem)
11916 return tem;
11917 goto associate;
11919 case TRUTH_ANDIF_EXPR:
11920 /* Note that the operands of this must be ints
11921 and their values must be 0 or 1.
11922 ("true" is a fixed value perhaps depending on the language.) */
11923 /* If first arg is constant zero, return it. */
11924 if (integer_zerop (arg0))
11925 return fold_convert_loc (loc, type, arg0);
11926 case TRUTH_AND_EXPR:
11927 /* If either arg is constant true, drop it. */
11928 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11929 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
11930 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11931 /* Preserve sequence points. */
11932 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11933 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11934 /* If second arg is constant zero, result is zero, but first arg
11935 must be evaluated. */
11936 if (integer_zerop (arg1))
11937 return omit_one_operand_loc (loc, type, arg1, arg0);
11938 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11939 case will be handled here. */
11940 if (integer_zerop (arg0))
11941 return omit_one_operand_loc (loc, type, arg0, arg1);
11943 /* !X && X is always false. */
11944 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11945 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11946 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11947 /* X && !X is always false. */
11948 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11949 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11950 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11952 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11953 means A >= Y && A != MAX, but in this case we know that
11954 A < X <= MAX. */
11956 if (!TREE_SIDE_EFFECTS (arg0)
11957 && !TREE_SIDE_EFFECTS (arg1))
11959 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
11960 if (tem && !operand_equal_p (tem, arg0, 0))
11961 return fold_build2_loc (loc, code, type, tem, arg1);
11963 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
11964 if (tem && !operand_equal_p (tem, arg1, 0))
11965 return fold_build2_loc (loc, code, type, arg0, tem);
11968 truth_andor:
11969 /* We only do these simplifications if we are optimizing. */
11970 if (!optimize)
11971 return NULL_TREE;
11973 /* Check for things like (A || B) && (A || C). We can convert this
11974 to A || (B && C). Note that either operator can be any of the four
11975 truth and/or operations and the transformation will still be
11976 valid. Also note that we only care about order for the
11977 ANDIF and ORIF operators. If B contains side effects, this
11978 might change the truth-value of A. */
11979 if (TREE_CODE (arg0) == TREE_CODE (arg1)
11980 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11981 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11982 || TREE_CODE (arg0) == TRUTH_AND_EXPR
11983 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11984 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11986 tree a00 = TREE_OPERAND (arg0, 0);
11987 tree a01 = TREE_OPERAND (arg0, 1);
11988 tree a10 = TREE_OPERAND (arg1, 0);
11989 tree a11 = TREE_OPERAND (arg1, 1);
11990 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11991 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11992 && (code == TRUTH_AND_EXPR
11993 || code == TRUTH_OR_EXPR));
11995 if (operand_equal_p (a00, a10, 0))
11996 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
11997 fold_build2_loc (loc, code, type, a01, a11));
11998 else if (commutative && operand_equal_p (a00, a11, 0))
11999 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12000 fold_build2_loc (loc, code, type, a01, a10));
12001 else if (commutative && operand_equal_p (a01, a10, 0))
12002 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
12003 fold_build2_loc (loc, code, type, a00, a11));
12005 /* This case if tricky because we must either have commutative
12006 operators or else A10 must not have side-effects. */
12008 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12009 && operand_equal_p (a01, a11, 0))
12010 return fold_build2_loc (loc, TREE_CODE (arg0), type,
12011 fold_build2_loc (loc, code, type, a00, a10),
12012 a01);
12015 /* See if we can build a range comparison. */
12016 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
12017 return tem;
12019 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
12020 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
12022 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
12023 if (tem)
12024 return fold_build2_loc (loc, code, type, tem, arg1);
12027 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
12028 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
12030 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
12031 if (tem)
12032 return fold_build2_loc (loc, code, type, arg0, tem);
12035 /* Check for the possibility of merging component references. If our
12036 lhs is another similar operation, try to merge its rhs with our
12037 rhs. Then try to merge our lhs and rhs. */
12038 if (TREE_CODE (arg0) == code
12039 && 0 != (tem = fold_truthop (loc, code, type,
12040 TREE_OPERAND (arg0, 1), arg1)))
12041 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12043 if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
12044 return tem;
12046 return NULL_TREE;
12048 case TRUTH_ORIF_EXPR:
12049 /* Note that the operands of this must be ints
12050 and their values must be 0 or true.
12051 ("true" is a fixed value perhaps depending on the language.) */
12052 /* If first arg is constant true, return it. */
12053 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12054 return fold_convert_loc (loc, type, arg0);
12055 case TRUTH_OR_EXPR:
12056 /* If either arg is constant zero, drop it. */
12057 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12058 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12059 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12060 /* Preserve sequence points. */
12061 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12062 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12063 /* If second arg is constant true, result is true, but we must
12064 evaluate first arg. */
12065 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12066 return omit_one_operand_loc (loc, type, arg1, arg0);
12067 /* Likewise for first arg, but note this only occurs here for
12068 TRUTH_OR_EXPR. */
12069 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12070 return omit_one_operand_loc (loc, type, arg0, arg1);
12072 /* !X || X is always true. */
12073 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12074 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12075 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12076 /* X || !X is always true. */
12077 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12078 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12079 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12081 goto truth_andor;
12083 case TRUTH_XOR_EXPR:
12084 /* If the second arg is constant zero, drop it. */
12085 if (integer_zerop (arg1))
12086 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12087 /* If the second arg is constant true, this is a logical inversion. */
12088 if (integer_onep (arg1))
12090 /* Only call invert_truthvalue if operand is a truth value. */
12091 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12092 tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12093 else
12094 tem = invert_truthvalue_loc (loc, arg0);
12095 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12097 /* Identical arguments cancel to zero. */
12098 if (operand_equal_p (arg0, arg1, 0))
12099 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12101 /* !X ^ X is always true. */
12102 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12103 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12104 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12106 /* X ^ !X is always true. */
12107 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12108 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12109 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12111 return NULL_TREE;
12113 case EQ_EXPR:
12114 case NE_EXPR:
12115 tem = fold_comparison (loc, code, type, op0, op1);
12116 if (tem != NULL_TREE)
12117 return tem;
12119 /* bool_var != 0 becomes bool_var. */
12120 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12121 && code == NE_EXPR)
12122 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12124 /* bool_var == 1 becomes bool_var. */
12125 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12126 && code == EQ_EXPR)
12127 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12129 /* bool_var != 1 becomes !bool_var. */
12130 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12131 && code == NE_EXPR)
12132 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12133 fold_convert_loc (loc, type, arg0));
12135 /* bool_var == 0 becomes !bool_var. */
12136 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12137 && code == EQ_EXPR)
12138 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12139 fold_convert_loc (loc, type, arg0));
12141 /* !exp != 0 becomes !exp */
12142 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12143 && code == NE_EXPR)
12144 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12146 /* If this is an equality comparison of the address of two non-weak,
12147 unaliased symbols neither of which are extern (since we do not
12148 have access to attributes for externs), then we know the result. */
12149 if (TREE_CODE (arg0) == ADDR_EXPR
12150 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12151 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12152 && ! lookup_attribute ("alias",
12153 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12154 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12155 && TREE_CODE (arg1) == ADDR_EXPR
12156 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12157 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12158 && ! lookup_attribute ("alias",
12159 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12160 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12162 /* We know that we're looking at the address of two
12163 non-weak, unaliased, static _DECL nodes.
12165 It is both wasteful and incorrect to call operand_equal_p
12166 to compare the two ADDR_EXPR nodes. It is wasteful in that
12167 all we need to do is test pointer equality for the arguments
12168 to the two ADDR_EXPR nodes. It is incorrect to use
12169 operand_equal_p as that function is NOT equivalent to a
12170 C equality test. It can in fact return false for two
12171 objects which would test as equal using the C equality
12172 operator. */
12173 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12174 return constant_boolean_node (equal
12175 ? code == EQ_EXPR : code != EQ_EXPR,
12176 type);
12179 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12180 a MINUS_EXPR of a constant, we can convert it into a comparison with
12181 a revised constant as long as no overflow occurs. */
12182 if (TREE_CODE (arg1) == INTEGER_CST
12183 && (TREE_CODE (arg0) == PLUS_EXPR
12184 || TREE_CODE (arg0) == MINUS_EXPR)
12185 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12186 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12187 ? MINUS_EXPR : PLUS_EXPR,
12188 fold_convert_loc (loc, TREE_TYPE (arg0),
12189 arg1),
12190 TREE_OPERAND (arg0, 1)))
12191 && !TREE_OVERFLOW (tem))
12192 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12194 /* Similarly for a NEGATE_EXPR. */
12195 if (TREE_CODE (arg0) == NEGATE_EXPR
12196 && TREE_CODE (arg1) == INTEGER_CST
12197 && 0 != (tem = negate_expr (arg1))
12198 && TREE_CODE (tem) == INTEGER_CST
12199 && !TREE_OVERFLOW (tem))
12200 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12202 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12203 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12204 && TREE_CODE (arg1) == INTEGER_CST
12205 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12206 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12207 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12208 fold_convert_loc (loc,
12209 TREE_TYPE (arg0),
12210 arg1),
12211 TREE_OPERAND (arg0, 1)));
12213 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
12214 if ((TREE_CODE (arg0) == PLUS_EXPR
12215 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12216 || TREE_CODE (arg0) == MINUS_EXPR)
12217 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12218 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12219 || POINTER_TYPE_P (TREE_TYPE (arg0))))
12221 tree val = TREE_OPERAND (arg0, 1);
12222 return omit_two_operands_loc (loc, type,
12223 fold_build2_loc (loc, code, type,
12224 val,
12225 build_int_cst (TREE_TYPE (val),
12226 0)),
12227 TREE_OPERAND (arg0, 0), arg1);
12230 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
12231 if (TREE_CODE (arg0) == MINUS_EXPR
12232 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12233 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12234 && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12236 return omit_two_operands_loc (loc, type,
12237 code == NE_EXPR
12238 ? boolean_true_node : boolean_false_node,
12239 TREE_OPERAND (arg0, 1), arg1);
12242 /* If we have X - Y == 0, we can convert that to X == Y and similarly
12243 for !=. Don't do this for ordered comparisons due to overflow. */
12244 if (TREE_CODE (arg0) == MINUS_EXPR
12245 && integer_zerop (arg1))
12246 return fold_build2_loc (loc, code, type,
12247 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12249 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12250 if (TREE_CODE (arg0) == ABS_EXPR
12251 && (integer_zerop (arg1) || real_zerop (arg1)))
12252 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12254 /* If this is an EQ or NE comparison with zero and ARG0 is
12255 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12256 two operations, but the latter can be done in one less insn
12257 on machines that have only two-operand insns or on which a
12258 constant cannot be the first operand. */
12259 if (TREE_CODE (arg0) == BIT_AND_EXPR
12260 && integer_zerop (arg1))
12262 tree arg00 = TREE_OPERAND (arg0, 0);
12263 tree arg01 = TREE_OPERAND (arg0, 1);
12264 if (TREE_CODE (arg00) == LSHIFT_EXPR
12265 && integer_onep (TREE_OPERAND (arg00, 0)))
12267 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12268 arg01, TREE_OPERAND (arg00, 1));
12269 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12270 build_int_cst (TREE_TYPE (arg0), 1));
12271 return fold_build2_loc (loc, code, type,
12272 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12273 arg1);
12275 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12276 && integer_onep (TREE_OPERAND (arg01, 0)))
12278 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12279 arg00, TREE_OPERAND (arg01, 1));
12280 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12281 build_int_cst (TREE_TYPE (arg0), 1));
12282 return fold_build2_loc (loc, code, type,
12283 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12284 arg1);
12288 /* If this is an NE or EQ comparison of zero against the result of a
12289 signed MOD operation whose second operand is a power of 2, make
12290 the MOD operation unsigned since it is simpler and equivalent. */
12291 if (integer_zerop (arg1)
12292 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12293 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12294 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12295 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12296 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12297 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12299 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12300 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12301 fold_convert_loc (loc, newtype,
12302 TREE_OPERAND (arg0, 0)),
12303 fold_convert_loc (loc, newtype,
12304 TREE_OPERAND (arg0, 1)));
12306 return fold_build2_loc (loc, code, type, newmod,
12307 fold_convert_loc (loc, newtype, arg1));
12310 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12311 C1 is a valid shift constant, and C2 is a power of two, i.e.
12312 a single bit. */
12313 if (TREE_CODE (arg0) == BIT_AND_EXPR
12314 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12315 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12316 == INTEGER_CST
12317 && integer_pow2p (TREE_OPERAND (arg0, 1))
12318 && integer_zerop (arg1))
12320 tree itype = TREE_TYPE (arg0);
12321 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12322 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12324 /* Check for a valid shift count. */
12325 if (TREE_INT_CST_HIGH (arg001) == 0
12326 && TREE_INT_CST_LOW (arg001) < prec)
12328 tree arg01 = TREE_OPERAND (arg0, 1);
12329 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12330 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12331 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12332 can be rewritten as (X & (C2 << C1)) != 0. */
12333 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12335 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12336 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12337 return fold_build2_loc (loc, code, type, tem, arg1);
12339 /* Otherwise, for signed (arithmetic) shifts,
12340 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12341 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12342 else if (!TYPE_UNSIGNED (itype))
12343 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12344 arg000, build_int_cst (itype, 0));
12345 /* Otherwise, of unsigned (logical) shifts,
12346 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12347 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12348 else
12349 return omit_one_operand_loc (loc, type,
12350 code == EQ_EXPR ? integer_one_node
12351 : integer_zero_node,
12352 arg000);
12356 /* If this is an NE comparison of zero with an AND of one, remove the
12357 comparison since the AND will give the correct value. */
12358 if (code == NE_EXPR
12359 && integer_zerop (arg1)
12360 && TREE_CODE (arg0) == BIT_AND_EXPR
12361 && integer_onep (TREE_OPERAND (arg0, 1)))
12362 return fold_convert_loc (loc, type, arg0);
12364 /* If we have (A & C) == C where C is a power of 2, convert this into
12365 (A & C) != 0. Similarly for NE_EXPR. */
12366 if (TREE_CODE (arg0) == BIT_AND_EXPR
12367 && integer_pow2p (TREE_OPERAND (arg0, 1))
12368 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12369 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12370 arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12371 integer_zero_node));
12373 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12374 bit, then fold the expression into A < 0 or A >= 0. */
12375 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12376 if (tem)
12377 return tem;
12379 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12380 Similarly for NE_EXPR. */
12381 if (TREE_CODE (arg0) == BIT_AND_EXPR
12382 && TREE_CODE (arg1) == INTEGER_CST
12383 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12385 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12386 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12387 TREE_OPERAND (arg0, 1));
12388 tree dandnotc = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12389 arg1, notc);
12390 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12391 if (integer_nonzerop (dandnotc))
12392 return omit_one_operand_loc (loc, type, rslt, arg0);
12395 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12396 Similarly for NE_EXPR. */
12397 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12398 && TREE_CODE (arg1) == INTEGER_CST
12399 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12401 tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12402 tree candnotd = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12403 TREE_OPERAND (arg0, 1), notd);
12404 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12405 if (integer_nonzerop (candnotd))
12406 return omit_one_operand_loc (loc, type, rslt, arg0);
12409 /* If this is a comparison of a field, we may be able to simplify it. */
12410 if ((TREE_CODE (arg0) == COMPONENT_REF
12411 || TREE_CODE (arg0) == BIT_FIELD_REF)
12412 /* Handle the constant case even without -O
12413 to make sure the warnings are given. */
12414 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12416 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12417 if (t1)
12418 return t1;
12421 /* Optimize comparisons of strlen vs zero to a compare of the
12422 first character of the string vs zero. To wit,
12423 strlen(ptr) == 0 => *ptr == 0
12424 strlen(ptr) != 0 => *ptr != 0
12425 Other cases should reduce to one of these two (or a constant)
12426 due to the return value of strlen being unsigned. */
12427 if (TREE_CODE (arg0) == CALL_EXPR
12428 && integer_zerop (arg1))
12430 tree fndecl = get_callee_fndecl (arg0);
12432 if (fndecl
12433 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12434 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12435 && call_expr_nargs (arg0) == 1
12436 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12438 tree iref = build_fold_indirect_ref_loc (loc,
12439 CALL_EXPR_ARG (arg0, 0));
12440 return fold_build2_loc (loc, code, type, iref,
12441 build_int_cst (TREE_TYPE (iref), 0));
12445 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12446 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12447 if (TREE_CODE (arg0) == RSHIFT_EXPR
12448 && integer_zerop (arg1)
12449 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12451 tree arg00 = TREE_OPERAND (arg0, 0);
12452 tree arg01 = TREE_OPERAND (arg0, 1);
12453 tree itype = TREE_TYPE (arg00);
12454 if (TREE_INT_CST_HIGH (arg01) == 0
12455 && TREE_INT_CST_LOW (arg01)
12456 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12458 if (TYPE_UNSIGNED (itype))
12460 itype = signed_type_for (itype);
12461 arg00 = fold_convert_loc (loc, itype, arg00);
12463 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12464 type, arg00, build_int_cst (itype, 0));
12468 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
12469 if (integer_zerop (arg1)
12470 && TREE_CODE (arg0) == BIT_XOR_EXPR)
12471 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12472 TREE_OPERAND (arg0, 1));
12474 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
12475 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12476 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12477 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12478 build_int_cst (TREE_TYPE (arg1), 0));
12479 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
12480 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12481 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12482 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12483 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12484 build_int_cst (TREE_TYPE (arg1), 0));
12486 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
12487 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12488 && TREE_CODE (arg1) == INTEGER_CST
12489 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12490 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12491 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12492 TREE_OPERAND (arg0, 1), arg1));
12494 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12495 (X & C) == 0 when C is a single bit. */
12496 if (TREE_CODE (arg0) == BIT_AND_EXPR
12497 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12498 && integer_zerop (arg1)
12499 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12501 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12502 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12503 TREE_OPERAND (arg0, 1));
12504 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12505 type, tem, arg1);
12508 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12509 constant C is a power of two, i.e. a single bit. */
12510 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12511 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12512 && integer_zerop (arg1)
12513 && integer_pow2p (TREE_OPERAND (arg0, 1))
12514 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12515 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12517 tree arg00 = TREE_OPERAND (arg0, 0);
12518 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12519 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12522 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12523 when is C is a power of two, i.e. a single bit. */
12524 if (TREE_CODE (arg0) == BIT_AND_EXPR
12525 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12526 && integer_zerop (arg1)
12527 && integer_pow2p (TREE_OPERAND (arg0, 1))
12528 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12529 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12531 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12532 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12533 arg000, TREE_OPERAND (arg0, 1));
12534 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12535 tem, build_int_cst (TREE_TYPE (tem), 0));
12538 if (integer_zerop (arg1)
12539 && tree_expr_nonzero_p (arg0))
12541 tree res = constant_boolean_node (code==NE_EXPR, type);
12542 return omit_one_operand_loc (loc, type, res, arg0);
12545 /* Fold -X op -Y as X op Y, where op is eq/ne. */
12546 if (TREE_CODE (arg0) == NEGATE_EXPR
12547 && TREE_CODE (arg1) == NEGATE_EXPR)
12548 return fold_build2_loc (loc, code, type,
12549 TREE_OPERAND (arg0, 0),
12550 TREE_OPERAND (arg1, 0));
12552 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12553 if (TREE_CODE (arg0) == BIT_AND_EXPR
12554 && TREE_CODE (arg1) == BIT_AND_EXPR)
12556 tree arg00 = TREE_OPERAND (arg0, 0);
12557 tree arg01 = TREE_OPERAND (arg0, 1);
12558 tree arg10 = TREE_OPERAND (arg1, 0);
12559 tree arg11 = TREE_OPERAND (arg1, 1);
12560 tree itype = TREE_TYPE (arg0);
12562 if (operand_equal_p (arg01, arg11, 0))
12563 return fold_build2_loc (loc, code, type,
12564 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12565 fold_build2_loc (loc,
12566 BIT_XOR_EXPR, itype,
12567 arg00, arg10),
12568 arg01),
12569 build_int_cst (itype, 0));
12571 if (operand_equal_p (arg01, arg10, 0))
12572 return fold_build2_loc (loc, code, type,
12573 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12574 fold_build2_loc (loc,
12575 BIT_XOR_EXPR, itype,
12576 arg00, arg11),
12577 arg01),
12578 build_int_cst (itype, 0));
12580 if (operand_equal_p (arg00, arg11, 0))
12581 return fold_build2_loc (loc, code, type,
12582 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12583 fold_build2_loc (loc,
12584 BIT_XOR_EXPR, itype,
12585 arg01, arg10),
12586 arg00),
12587 build_int_cst (itype, 0));
12589 if (operand_equal_p (arg00, arg10, 0))
12590 return fold_build2_loc (loc, code, type,
12591 fold_build2_loc (loc, BIT_AND_EXPR, itype,
12592 fold_build2_loc (loc,
12593 BIT_XOR_EXPR, itype,
12594 arg01, arg11),
12595 arg00),
12596 build_int_cst (itype, 0));
12599 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12600 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12602 tree arg00 = TREE_OPERAND (arg0, 0);
12603 tree arg01 = TREE_OPERAND (arg0, 1);
12604 tree arg10 = TREE_OPERAND (arg1, 0);
12605 tree arg11 = TREE_OPERAND (arg1, 1);
12606 tree itype = TREE_TYPE (arg0);
12608 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12609 operand_equal_p guarantees no side-effects so we don't need
12610 to use omit_one_operand on Z. */
12611 if (operand_equal_p (arg01, arg11, 0))
12612 return fold_build2_loc (loc, code, type, arg00, arg10);
12613 if (operand_equal_p (arg01, arg10, 0))
12614 return fold_build2_loc (loc, code, type, arg00, arg11);
12615 if (operand_equal_p (arg00, arg11, 0))
12616 return fold_build2_loc (loc, code, type, arg01, arg10);
12617 if (operand_equal_p (arg00, arg10, 0))
12618 return fold_build2_loc (loc, code, type, arg01, arg11);
12620 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12621 if (TREE_CODE (arg01) == INTEGER_CST
12622 && TREE_CODE (arg11) == INTEGER_CST)
12623 return fold_build2_loc (loc, code, type,
12624 fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00,
12625 fold_build2_loc (loc,
12626 BIT_XOR_EXPR, itype,
12627 arg01, arg11)),
12628 arg10);
12631 /* Attempt to simplify equality/inequality comparisons of complex
12632 values. Only lower the comparison if the result is known or
12633 can be simplified to a single scalar comparison. */
12634 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12635 || TREE_CODE (arg0) == COMPLEX_CST)
12636 && (TREE_CODE (arg1) == COMPLEX_EXPR
12637 || TREE_CODE (arg1) == COMPLEX_CST))
12639 tree real0, imag0, real1, imag1;
12640 tree rcond, icond;
12642 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12644 real0 = TREE_OPERAND (arg0, 0);
12645 imag0 = TREE_OPERAND (arg0, 1);
12647 else
12649 real0 = TREE_REALPART (arg0);
12650 imag0 = TREE_IMAGPART (arg0);
12653 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12655 real1 = TREE_OPERAND (arg1, 0);
12656 imag1 = TREE_OPERAND (arg1, 1);
12658 else
12660 real1 = TREE_REALPART (arg1);
12661 imag1 = TREE_IMAGPART (arg1);
12664 rcond = fold_binary_loc (loc, code, type, real0, real1);
12665 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12667 if (integer_zerop (rcond))
12669 if (code == EQ_EXPR)
12670 return omit_two_operands_loc (loc, type, boolean_false_node,
12671 imag0, imag1);
12672 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12674 else
12676 if (code == NE_EXPR)
12677 return omit_two_operands_loc (loc, type, boolean_true_node,
12678 imag0, imag1);
12679 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12683 icond = fold_binary_loc (loc, code, type, imag0, imag1);
12684 if (icond && TREE_CODE (icond) == INTEGER_CST)
12686 if (integer_zerop (icond))
12688 if (code == EQ_EXPR)
12689 return omit_two_operands_loc (loc, type, boolean_false_node,
12690 real0, real1);
12691 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12693 else
12695 if (code == NE_EXPR)
12696 return omit_two_operands_loc (loc, type, boolean_true_node,
12697 real0, real1);
12698 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12703 return NULL_TREE;
12705 case LT_EXPR:
12706 case GT_EXPR:
12707 case LE_EXPR:
12708 case GE_EXPR:
12709 tem = fold_comparison (loc, code, type, op0, op1);
12710 if (tem != NULL_TREE)
12711 return tem;
12713 /* Transform comparisons of the form X +- C CMP X. */
12714 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12715 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12716 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12717 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12718 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12719 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12721 tree arg01 = TREE_OPERAND (arg0, 1);
12722 enum tree_code code0 = TREE_CODE (arg0);
12723 int is_positive;
12725 if (TREE_CODE (arg01) == REAL_CST)
12726 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12727 else
12728 is_positive = tree_int_cst_sgn (arg01);
12730 /* (X - c) > X becomes false. */
12731 if (code == GT_EXPR
12732 && ((code0 == MINUS_EXPR && is_positive >= 0)
12733 || (code0 == PLUS_EXPR && is_positive <= 0)))
12735 if (TREE_CODE (arg01) == INTEGER_CST
12736 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12737 fold_overflow_warning (("assuming signed overflow does not "
12738 "occur when assuming that (X - c) > X "
12739 "is always false"),
12740 WARN_STRICT_OVERFLOW_ALL);
12741 return constant_boolean_node (0, type);
12744 /* Likewise (X + c) < X becomes false. */
12745 if (code == LT_EXPR
12746 && ((code0 == PLUS_EXPR && is_positive >= 0)
12747 || (code0 == MINUS_EXPR && is_positive <= 0)))
12749 if (TREE_CODE (arg01) == INTEGER_CST
12750 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12751 fold_overflow_warning (("assuming signed overflow does not "
12752 "occur when assuming that "
12753 "(X + c) < X is always false"),
12754 WARN_STRICT_OVERFLOW_ALL);
12755 return constant_boolean_node (0, type);
12758 /* Convert (X - c) <= X to true. */
12759 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12760 && code == LE_EXPR
12761 && ((code0 == MINUS_EXPR && is_positive >= 0)
12762 || (code0 == PLUS_EXPR && is_positive <= 0)))
12764 if (TREE_CODE (arg01) == INTEGER_CST
12765 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12766 fold_overflow_warning (("assuming signed overflow does not "
12767 "occur when assuming that "
12768 "(X - c) <= X is always true"),
12769 WARN_STRICT_OVERFLOW_ALL);
12770 return constant_boolean_node (1, type);
12773 /* Convert (X + c) >= X to true. */
12774 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12775 && code == GE_EXPR
12776 && ((code0 == PLUS_EXPR && is_positive >= 0)
12777 || (code0 == MINUS_EXPR && is_positive <= 0)))
12779 if (TREE_CODE (arg01) == INTEGER_CST
12780 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12781 fold_overflow_warning (("assuming signed overflow does not "
12782 "occur when assuming that "
12783 "(X + c) >= X is always true"),
12784 WARN_STRICT_OVERFLOW_ALL);
12785 return constant_boolean_node (1, type);
12788 if (TREE_CODE (arg01) == INTEGER_CST)
12790 /* Convert X + c > X and X - c < X to true for integers. */
12791 if (code == GT_EXPR
12792 && ((code0 == PLUS_EXPR && is_positive > 0)
12793 || (code0 == MINUS_EXPR && is_positive < 0)))
12795 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12796 fold_overflow_warning (("assuming signed overflow does "
12797 "not occur when assuming that "
12798 "(X + c) > X is always true"),
12799 WARN_STRICT_OVERFLOW_ALL);
12800 return constant_boolean_node (1, type);
12803 if (code == LT_EXPR
12804 && ((code0 == MINUS_EXPR && is_positive > 0)
12805 || (code0 == PLUS_EXPR && is_positive < 0)))
12807 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12808 fold_overflow_warning (("assuming signed overflow does "
12809 "not occur when assuming that "
12810 "(X - c) < X is always true"),
12811 WARN_STRICT_OVERFLOW_ALL);
12812 return constant_boolean_node (1, type);
12815 /* Convert X + c <= X and X - c >= X to false for integers. */
12816 if (code == LE_EXPR
12817 && ((code0 == PLUS_EXPR && is_positive > 0)
12818 || (code0 == MINUS_EXPR && is_positive < 0)))
12820 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12821 fold_overflow_warning (("assuming signed overflow does "
12822 "not occur when assuming that "
12823 "(X + c) <= X is always false"),
12824 WARN_STRICT_OVERFLOW_ALL);
12825 return constant_boolean_node (0, type);
12828 if (code == GE_EXPR
12829 && ((code0 == MINUS_EXPR && is_positive > 0)
12830 || (code0 == PLUS_EXPR && is_positive < 0)))
12832 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12833 fold_overflow_warning (("assuming signed overflow does "
12834 "not occur when assuming that "
12835 "(X - c) >= X is always false"),
12836 WARN_STRICT_OVERFLOW_ALL);
12837 return constant_boolean_node (0, type);
12842 /* Comparisons with the highest or lowest possible integer of
12843 the specified precision will have known values. */
12845 tree arg1_type = TREE_TYPE (arg1);
12846 unsigned int width = TYPE_PRECISION (arg1_type);
12848 if (TREE_CODE (arg1) == INTEGER_CST
12849 && width <= 2 * HOST_BITS_PER_WIDE_INT
12850 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12852 HOST_WIDE_INT signed_max_hi;
12853 unsigned HOST_WIDE_INT signed_max_lo;
12854 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12856 if (width <= HOST_BITS_PER_WIDE_INT)
12858 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12859 - 1;
12860 signed_max_hi = 0;
12861 max_hi = 0;
12863 if (TYPE_UNSIGNED (arg1_type))
12865 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12866 min_lo = 0;
12867 min_hi = 0;
12869 else
12871 max_lo = signed_max_lo;
12872 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12873 min_hi = -1;
12876 else
12878 width -= HOST_BITS_PER_WIDE_INT;
12879 signed_max_lo = -1;
12880 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12881 - 1;
12882 max_lo = -1;
12883 min_lo = 0;
12885 if (TYPE_UNSIGNED (arg1_type))
12887 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12888 min_hi = 0;
12890 else
12892 max_hi = signed_max_hi;
12893 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12897 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12898 && TREE_INT_CST_LOW (arg1) == max_lo)
12899 switch (code)
12901 case GT_EXPR:
12902 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12904 case GE_EXPR:
12905 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12907 case LE_EXPR:
12908 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12910 case LT_EXPR:
12911 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12913 /* The GE_EXPR and LT_EXPR cases above are not normally
12914 reached because of previous transformations. */
12916 default:
12917 break;
12919 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12920 == max_hi
12921 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12922 switch (code)
12924 case GT_EXPR:
12925 arg1 = const_binop (PLUS_EXPR, arg1,
12926 build_int_cst (TREE_TYPE (arg1), 1));
12927 return fold_build2_loc (loc, EQ_EXPR, type,
12928 fold_convert_loc (loc,
12929 TREE_TYPE (arg1), arg0),
12930 arg1);
12931 case LE_EXPR:
12932 arg1 = const_binop (PLUS_EXPR, arg1,
12933 build_int_cst (TREE_TYPE (arg1), 1));
12934 return fold_build2_loc (loc, NE_EXPR, type,
12935 fold_convert_loc (loc, TREE_TYPE (arg1),
12936 arg0),
12937 arg1);
12938 default:
12939 break;
12941 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12942 == min_hi
12943 && TREE_INT_CST_LOW (arg1) == min_lo)
12944 switch (code)
12946 case LT_EXPR:
12947 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12949 case LE_EXPR:
12950 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
12952 case GE_EXPR:
12953 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12955 case GT_EXPR:
12956 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
12958 default:
12959 break;
12961 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12962 == min_hi
12963 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12964 switch (code)
12966 case GE_EXPR:
12967 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
12968 return fold_build2_loc (loc, NE_EXPR, type,
12969 fold_convert_loc (loc,
12970 TREE_TYPE (arg1), arg0),
12971 arg1);
12972 case LT_EXPR:
12973 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node);
12974 return fold_build2_loc (loc, EQ_EXPR, type,
12975 fold_convert_loc (loc, TREE_TYPE (arg1),
12976 arg0),
12977 arg1);
12978 default:
12979 break;
12982 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12983 && TREE_INT_CST_LOW (arg1) == signed_max_lo
12984 && TYPE_UNSIGNED (arg1_type)
12985 /* We will flip the signedness of the comparison operator
12986 associated with the mode of arg1, so the sign bit is
12987 specified by this mode. Check that arg1 is the signed
12988 max associated with this sign bit. */
12989 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12990 /* signed_type does not work on pointer types. */
12991 && INTEGRAL_TYPE_P (arg1_type))
12993 /* The following case also applies to X < signed_max+1
12994 and X >= signed_max+1 because previous transformations. */
12995 if (code == LE_EXPR || code == GT_EXPR)
12997 tree st;
12998 st = signed_type_for (TREE_TYPE (arg1));
12999 return fold_build2_loc (loc,
13000 code == LE_EXPR ? GE_EXPR : LT_EXPR,
13001 type, fold_convert_loc (loc, st, arg0),
13002 build_int_cst (st, 0));
13008 /* If we are comparing an ABS_EXPR with a constant, we can
13009 convert all the cases into explicit comparisons, but they may
13010 well not be faster than doing the ABS and one comparison.
13011 But ABS (X) <= C is a range comparison, which becomes a subtraction
13012 and a comparison, and is probably faster. */
13013 if (code == LE_EXPR
13014 && TREE_CODE (arg1) == INTEGER_CST
13015 && TREE_CODE (arg0) == ABS_EXPR
13016 && ! TREE_SIDE_EFFECTS (arg0)
13017 && (0 != (tem = negate_expr (arg1)))
13018 && TREE_CODE (tem) == INTEGER_CST
13019 && !TREE_OVERFLOW (tem))
13020 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13021 build2 (GE_EXPR, type,
13022 TREE_OPERAND (arg0, 0), tem),
13023 build2 (LE_EXPR, type,
13024 TREE_OPERAND (arg0, 0), arg1));
13026 /* Convert ABS_EXPR<x> >= 0 to true. */
13027 strict_overflow_p = false;
13028 if (code == GE_EXPR
13029 && (integer_zerop (arg1)
13030 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13031 && real_zerop (arg1)))
13032 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13034 if (strict_overflow_p)
13035 fold_overflow_warning (("assuming signed overflow does not occur "
13036 "when simplifying comparison of "
13037 "absolute value and zero"),
13038 WARN_STRICT_OVERFLOW_CONDITIONAL);
13039 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13042 /* Convert ABS_EXPR<x> < 0 to false. */
13043 strict_overflow_p = false;
13044 if (code == LT_EXPR
13045 && (integer_zerop (arg1) || real_zerop (arg1))
13046 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13048 if (strict_overflow_p)
13049 fold_overflow_warning (("assuming signed overflow does not occur "
13050 "when simplifying comparison of "
13051 "absolute value and zero"),
13052 WARN_STRICT_OVERFLOW_CONDITIONAL);
13053 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13056 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13057 and similarly for >= into !=. */
13058 if ((code == LT_EXPR || code == GE_EXPR)
13059 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13060 && TREE_CODE (arg1) == LSHIFT_EXPR
13061 && integer_onep (TREE_OPERAND (arg1, 0)))
13062 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13063 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13064 TREE_OPERAND (arg1, 1)),
13065 build_int_cst (TREE_TYPE (arg0), 0));
13067 if ((code == LT_EXPR || code == GE_EXPR)
13068 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13069 && CONVERT_EXPR_P (arg1)
13070 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13071 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13073 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13074 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
13075 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13076 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
13077 build_int_cst (TREE_TYPE (arg0), 0));
13080 return NULL_TREE;
13082 case UNORDERED_EXPR:
13083 case ORDERED_EXPR:
13084 case UNLT_EXPR:
13085 case UNLE_EXPR:
13086 case UNGT_EXPR:
13087 case UNGE_EXPR:
13088 case UNEQ_EXPR:
13089 case LTGT_EXPR:
13090 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13092 t1 = fold_relational_const (code, type, arg0, arg1);
13093 if (t1 != NULL_TREE)
13094 return t1;
13097 /* If the first operand is NaN, the result is constant. */
13098 if (TREE_CODE (arg0) == REAL_CST
13099 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13100 && (code != LTGT_EXPR || ! flag_trapping_math))
13102 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13103 ? integer_zero_node
13104 : integer_one_node;
13105 return omit_one_operand_loc (loc, type, t1, arg1);
13108 /* If the second operand is NaN, the result is constant. */
13109 if (TREE_CODE (arg1) == REAL_CST
13110 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13111 && (code != LTGT_EXPR || ! flag_trapping_math))
13113 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13114 ? integer_zero_node
13115 : integer_one_node;
13116 return omit_one_operand_loc (loc, type, t1, arg0);
13119 /* Simplify unordered comparison of something with itself. */
13120 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13121 && operand_equal_p (arg0, arg1, 0))
13122 return constant_boolean_node (1, type);
13124 if (code == LTGT_EXPR
13125 && !flag_trapping_math
13126 && operand_equal_p (arg0, arg1, 0))
13127 return constant_boolean_node (0, type);
13129 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13131 tree targ0 = strip_float_extensions (arg0);
13132 tree targ1 = strip_float_extensions (arg1);
13133 tree newtype = TREE_TYPE (targ0);
13135 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13136 newtype = TREE_TYPE (targ1);
13138 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13139 return fold_build2_loc (loc, code, type,
13140 fold_convert_loc (loc, newtype, targ0),
13141 fold_convert_loc (loc, newtype, targ1));
13144 return NULL_TREE;
13146 case COMPOUND_EXPR:
13147 /* When pedantic, a compound expression can be neither an lvalue
13148 nor an integer constant expression. */
13149 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13150 return NULL_TREE;
13151 /* Don't let (0, 0) be null pointer constant. */
13152 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13153 : fold_convert_loc (loc, type, arg1);
13154 return pedantic_non_lvalue_loc (loc, tem);
13156 case COMPLEX_EXPR:
13157 if ((TREE_CODE (arg0) == REAL_CST
13158 && TREE_CODE (arg1) == REAL_CST)
13159 || (TREE_CODE (arg0) == INTEGER_CST
13160 && TREE_CODE (arg1) == INTEGER_CST))
13161 return build_complex (type, arg0, arg1);
13162 return NULL_TREE;
13164 case ASSERT_EXPR:
13165 /* An ASSERT_EXPR should never be passed to fold_binary. */
13166 gcc_unreachable ();
13168 default:
13169 return NULL_TREE;
13170 } /* switch (code) */
13173 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
13174 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
13175 of GOTO_EXPR. */
13177 static tree
13178 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13180 switch (TREE_CODE (*tp))
13182 case LABEL_EXPR:
13183 return *tp;
13185 case GOTO_EXPR:
13186 *walk_subtrees = 0;
13188 /* ... fall through ... */
13190 default:
13191 return NULL_TREE;
13195 /* Return whether the sub-tree ST contains a label which is accessible from
13196 outside the sub-tree. */
13198 static bool
13199 contains_label_p (tree st)
13201 return
13202 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13205 /* Fold a ternary expression of code CODE and type TYPE with operands
13206 OP0, OP1, and OP2. Return the folded expression if folding is
13207 successful. Otherwise, return NULL_TREE. */
13209 tree
13210 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13211 tree op0, tree op1, tree op2)
13213 tree tem;
13214 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
13215 enum tree_code_class kind = TREE_CODE_CLASS (code);
13217 gcc_assert (IS_EXPR_CODE_CLASS (kind)
13218 && TREE_CODE_LENGTH (code) == 3);
13220 /* Strip any conversions that don't change the mode. This is safe
13221 for every expression, except for a comparison expression because
13222 its signedness is derived from its operands. So, in the latter
13223 case, only strip conversions that don't change the signedness.
13225 Note that this is done as an internal manipulation within the
13226 constant folder, in order to find the simplest representation of
13227 the arguments so that their form can be studied. In any cases,
13228 the appropriate type conversions should be put back in the tree
13229 that will get out of the constant folder. */
13230 if (op0)
13232 arg0 = op0;
13233 STRIP_NOPS (arg0);
13236 if (op1)
13238 arg1 = op1;
13239 STRIP_NOPS (arg1);
13242 if (op2)
13244 arg2 = op2;
13245 STRIP_NOPS (arg2);
13248 switch (code)
13250 case COMPONENT_REF:
13251 if (TREE_CODE (arg0) == CONSTRUCTOR
13252 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13254 unsigned HOST_WIDE_INT idx;
13255 tree field, value;
13256 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13257 if (field == arg1)
13258 return value;
13260 return NULL_TREE;
13262 case COND_EXPR:
13263 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13264 so all simple results must be passed through pedantic_non_lvalue. */
13265 if (TREE_CODE (arg0) == INTEGER_CST)
13267 tree unused_op = integer_zerop (arg0) ? op1 : op2;
13268 tem = integer_zerop (arg0) ? op2 : op1;
13269 /* Only optimize constant conditions when the selected branch
13270 has the same type as the COND_EXPR. This avoids optimizing
13271 away "c ? x : throw", where the throw has a void type.
13272 Avoid throwing away that operand which contains label. */
13273 if ((!TREE_SIDE_EFFECTS (unused_op)
13274 || !contains_label_p (unused_op))
13275 && (! VOID_TYPE_P (TREE_TYPE (tem))
13276 || VOID_TYPE_P (type)))
13277 return pedantic_non_lvalue_loc (loc, tem);
13278 return NULL_TREE;
13280 if (operand_equal_p (arg1, op2, 0))
13281 return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13283 /* If we have A op B ? A : C, we may be able to convert this to a
13284 simpler expression, depending on the operation and the values
13285 of B and C. Signed zeros prevent all of these transformations,
13286 for reasons given above each one.
13288 Also try swapping the arguments and inverting the conditional. */
13289 if (COMPARISON_CLASS_P (arg0)
13290 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13291 arg1, TREE_OPERAND (arg0, 1))
13292 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13294 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13295 if (tem)
13296 return tem;
13299 if (COMPARISON_CLASS_P (arg0)
13300 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13301 op2,
13302 TREE_OPERAND (arg0, 1))
13303 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13305 tem = fold_truth_not_expr (loc, arg0);
13306 if (tem && COMPARISON_CLASS_P (tem))
13308 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13309 if (tem)
13310 return tem;
13314 /* If the second operand is simpler than the third, swap them
13315 since that produces better jump optimization results. */
13316 if (truth_value_p (TREE_CODE (arg0))
13317 && tree_swap_operands_p (op1, op2, false))
13319 /* See if this can be inverted. If it can't, possibly because
13320 it was a floating-point inequality comparison, don't do
13321 anything. */
13322 tem = fold_truth_not_expr (loc, arg0);
13323 if (tem)
13324 return fold_build3_loc (loc, code, type, tem, op2, op1);
13327 /* Convert A ? 1 : 0 to simply A. */
13328 if (integer_onep (op1)
13329 && integer_zerop (op2)
13330 /* If we try to convert OP0 to our type, the
13331 call to fold will try to move the conversion inside
13332 a COND, which will recurse. In that case, the COND_EXPR
13333 is probably the best choice, so leave it alone. */
13334 && type == TREE_TYPE (arg0))
13335 return pedantic_non_lvalue_loc (loc, arg0);
13337 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13338 over COND_EXPR in cases such as floating point comparisons. */
13339 if (integer_zerop (op1)
13340 && integer_onep (op2)
13341 && truth_value_p (TREE_CODE (arg0)))
13342 return pedantic_non_lvalue_loc (loc,
13343 fold_convert_loc (loc, type,
13344 invert_truthvalue_loc (loc,
13345 arg0)));
13347 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13348 if (TREE_CODE (arg0) == LT_EXPR
13349 && integer_zerop (TREE_OPERAND (arg0, 1))
13350 && integer_zerop (op2)
13351 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13353 /* sign_bit_p only checks ARG1 bits within A's precision.
13354 If <sign bit of A> has wider type than A, bits outside
13355 of A's precision in <sign bit of A> need to be checked.
13356 If they are all 0, this optimization needs to be done
13357 in unsigned A's type, if they are all 1 in signed A's type,
13358 otherwise this can't be done. */
13359 if (TYPE_PRECISION (TREE_TYPE (tem))
13360 < TYPE_PRECISION (TREE_TYPE (arg1))
13361 && TYPE_PRECISION (TREE_TYPE (tem))
13362 < TYPE_PRECISION (type))
13364 unsigned HOST_WIDE_INT mask_lo;
13365 HOST_WIDE_INT mask_hi;
13366 int inner_width, outer_width;
13367 tree tem_type;
13369 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13370 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13371 if (outer_width > TYPE_PRECISION (type))
13372 outer_width = TYPE_PRECISION (type);
13374 if (outer_width > HOST_BITS_PER_WIDE_INT)
13376 mask_hi = ((unsigned HOST_WIDE_INT) -1
13377 >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13378 mask_lo = -1;
13380 else
13382 mask_hi = 0;
13383 mask_lo = ((unsigned HOST_WIDE_INT) -1
13384 >> (HOST_BITS_PER_WIDE_INT - outer_width));
13386 if (inner_width > HOST_BITS_PER_WIDE_INT)
13388 mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13389 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13390 mask_lo = 0;
13392 else
13393 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13394 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13396 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13397 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13399 tem_type = signed_type_for (TREE_TYPE (tem));
13400 tem = fold_convert_loc (loc, tem_type, tem);
13402 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13403 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13405 tem_type = unsigned_type_for (TREE_TYPE (tem));
13406 tem = fold_convert_loc (loc, tem_type, tem);
13408 else
13409 tem = NULL;
13412 if (tem)
13413 return
13414 fold_convert_loc (loc, type,
13415 fold_build2_loc (loc, BIT_AND_EXPR,
13416 TREE_TYPE (tem), tem,
13417 fold_convert_loc (loc,
13418 TREE_TYPE (tem),
13419 arg1)));
13422 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13423 already handled above. */
13424 if (TREE_CODE (arg0) == BIT_AND_EXPR
13425 && integer_onep (TREE_OPERAND (arg0, 1))
13426 && integer_zerop (op2)
13427 && integer_pow2p (arg1))
13429 tree tem = TREE_OPERAND (arg0, 0);
13430 STRIP_NOPS (tem);
13431 if (TREE_CODE (tem) == RSHIFT_EXPR
13432 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13433 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13434 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13435 return fold_build2_loc (loc, BIT_AND_EXPR, type,
13436 TREE_OPERAND (tem, 0), arg1);
13439 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13440 is probably obsolete because the first operand should be a
13441 truth value (that's why we have the two cases above), but let's
13442 leave it in until we can confirm this for all front-ends. */
13443 if (integer_zerop (op2)
13444 && TREE_CODE (arg0) == NE_EXPR
13445 && integer_zerop (TREE_OPERAND (arg0, 1))
13446 && integer_pow2p (arg1)
13447 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13448 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13449 arg1, OEP_ONLY_CONST))
13450 return pedantic_non_lvalue_loc (loc,
13451 fold_convert_loc (loc, type,
13452 TREE_OPERAND (arg0, 0)));
13454 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13455 if (integer_zerop (op2)
13456 && truth_value_p (TREE_CODE (arg0))
13457 && truth_value_p (TREE_CODE (arg1)))
13458 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13459 fold_convert_loc (loc, type, arg0),
13460 arg1);
13462 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13463 if (integer_onep (op2)
13464 && truth_value_p (TREE_CODE (arg0))
13465 && truth_value_p (TREE_CODE (arg1)))
13467 /* Only perform transformation if ARG0 is easily inverted. */
13468 tem = fold_truth_not_expr (loc, arg0);
13469 if (tem)
13470 return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13471 fold_convert_loc (loc, type, tem),
13472 arg1);
13475 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13476 if (integer_zerop (arg1)
13477 && truth_value_p (TREE_CODE (arg0))
13478 && truth_value_p (TREE_CODE (op2)))
13480 /* Only perform transformation if ARG0 is easily inverted. */
13481 tem = fold_truth_not_expr (loc, arg0);
13482 if (tem)
13483 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13484 fold_convert_loc (loc, type, tem),
13485 op2);
13488 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13489 if (integer_onep (arg1)
13490 && truth_value_p (TREE_CODE (arg0))
13491 && truth_value_p (TREE_CODE (op2)))
13492 return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13493 fold_convert_loc (loc, type, arg0),
13494 op2);
13496 return NULL_TREE;
13498 case CALL_EXPR:
13499 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13500 of fold_ternary on them. */
13501 gcc_unreachable ();
13503 case BIT_FIELD_REF:
13504 if ((TREE_CODE (arg0) == VECTOR_CST
13505 || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13506 && type == TREE_TYPE (TREE_TYPE (arg0)))
13508 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13509 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13511 if (width != 0
13512 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13513 && (idx % width) == 0
13514 && (idx = idx / width)
13515 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13517 tree elements = NULL_TREE;
13519 if (TREE_CODE (arg0) == VECTOR_CST)
13520 elements = TREE_VECTOR_CST_ELTS (arg0);
13521 else
13523 unsigned HOST_WIDE_INT idx;
13524 tree value;
13526 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13527 elements = tree_cons (NULL_TREE, value, elements);
13529 while (idx-- > 0 && elements)
13530 elements = TREE_CHAIN (elements);
13531 if (elements)
13532 return TREE_VALUE (elements);
13533 else
13534 return build_zero_cst (type);
13538 /* A bit-field-ref that referenced the full argument can be stripped. */
13539 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13540 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13541 && integer_zerop (op2))
13542 return fold_convert_loc (loc, type, arg0);
13544 return NULL_TREE;
13546 case FMA_EXPR:
13547 /* For integers we can decompose the FMA if possible. */
13548 if (TREE_CODE (arg0) == INTEGER_CST
13549 && TREE_CODE (arg1) == INTEGER_CST)
13550 return fold_build2_loc (loc, PLUS_EXPR, type,
13551 const_binop (MULT_EXPR, arg0, arg1), arg2);
13552 if (integer_zerop (arg2))
13553 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
13555 return fold_fma (loc, type, arg0, arg1, arg2);
13557 default:
13558 return NULL_TREE;
13559 } /* switch (code) */
13562 /* Perform constant folding and related simplification of EXPR.
13563 The related simplifications include x*1 => x, x*0 => 0, etc.,
13564 and application of the associative law.
13565 NOP_EXPR conversions may be removed freely (as long as we
13566 are careful not to change the type of the overall expression).
13567 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13568 but we can constant-fold them if they have constant operands. */
13570 #ifdef ENABLE_FOLD_CHECKING
13571 # define fold(x) fold_1 (x)
13572 static tree fold_1 (tree);
13573 static
13574 #endif
13575 tree
13576 fold (tree expr)
13578 const tree t = expr;
13579 enum tree_code code = TREE_CODE (t);
13580 enum tree_code_class kind = TREE_CODE_CLASS (code);
13581 tree tem;
13582 location_t loc = EXPR_LOCATION (expr);
13584 /* Return right away if a constant. */
13585 if (kind == tcc_constant)
13586 return t;
13588 /* CALL_EXPR-like objects with variable numbers of operands are
13589 treated specially. */
13590 if (kind == tcc_vl_exp)
13592 if (code == CALL_EXPR)
13594 tem = fold_call_expr (loc, expr, false);
13595 return tem ? tem : expr;
13597 return expr;
13600 if (IS_EXPR_CODE_CLASS (kind))
13602 tree type = TREE_TYPE (t);
13603 tree op0, op1, op2;
13605 switch (TREE_CODE_LENGTH (code))
13607 case 1:
13608 op0 = TREE_OPERAND (t, 0);
13609 tem = fold_unary_loc (loc, code, type, op0);
13610 return tem ? tem : expr;
13611 case 2:
13612 op0 = TREE_OPERAND (t, 0);
13613 op1 = TREE_OPERAND (t, 1);
13614 tem = fold_binary_loc (loc, code, type, op0, op1);
13615 return tem ? tem : expr;
13616 case 3:
13617 op0 = TREE_OPERAND (t, 0);
13618 op1 = TREE_OPERAND (t, 1);
13619 op2 = TREE_OPERAND (t, 2);
13620 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13621 return tem ? tem : expr;
13622 default:
13623 break;
13627 switch (code)
13629 case ARRAY_REF:
13631 tree op0 = TREE_OPERAND (t, 0);
13632 tree op1 = TREE_OPERAND (t, 1);
13634 if (TREE_CODE (op1) == INTEGER_CST
13635 && TREE_CODE (op0) == CONSTRUCTOR
13636 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13638 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13639 unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13640 unsigned HOST_WIDE_INT begin = 0;
13642 /* Find a matching index by means of a binary search. */
13643 while (begin != end)
13645 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13646 tree index = VEC_index (constructor_elt, elts, middle)->index;
13648 if (TREE_CODE (index) == INTEGER_CST
13649 && tree_int_cst_lt (index, op1))
13650 begin = middle + 1;
13651 else if (TREE_CODE (index) == INTEGER_CST
13652 && tree_int_cst_lt (op1, index))
13653 end = middle;
13654 else if (TREE_CODE (index) == RANGE_EXPR
13655 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13656 begin = middle + 1;
13657 else if (TREE_CODE (index) == RANGE_EXPR
13658 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13659 end = middle;
13660 else
13661 return VEC_index (constructor_elt, elts, middle)->value;
13665 return t;
13668 case CONST_DECL:
13669 return fold (DECL_INITIAL (t));
13671 default:
13672 return t;
13673 } /* switch (code) */
13676 #ifdef ENABLE_FOLD_CHECKING
13677 #undef fold
13679 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13680 static void fold_check_failed (const_tree, const_tree);
13681 void print_fold_checksum (const_tree);
13683 /* When --enable-checking=fold, compute a digest of expr before
13684 and after actual fold call to see if fold did not accidentally
13685 change original expr. */
13687 tree
13688 fold (tree expr)
13690 tree ret;
13691 struct md5_ctx ctx;
13692 unsigned char checksum_before[16], checksum_after[16];
13693 htab_t ht;
13695 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13696 md5_init_ctx (&ctx);
13697 fold_checksum_tree (expr, &ctx, ht);
13698 md5_finish_ctx (&ctx, checksum_before);
13699 htab_empty (ht);
13701 ret = fold_1 (expr);
13703 md5_init_ctx (&ctx);
13704 fold_checksum_tree (expr, &ctx, ht);
13705 md5_finish_ctx (&ctx, checksum_after);
13706 htab_delete (ht);
13708 if (memcmp (checksum_before, checksum_after, 16))
13709 fold_check_failed (expr, ret);
13711 return ret;
13714 void
13715 print_fold_checksum (const_tree expr)
13717 struct md5_ctx ctx;
13718 unsigned char checksum[16], cnt;
13719 htab_t ht;
13721 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13722 md5_init_ctx (&ctx);
13723 fold_checksum_tree (expr, &ctx, ht);
13724 md5_finish_ctx (&ctx, checksum);
13725 htab_delete (ht);
13726 for (cnt = 0; cnt < 16; ++cnt)
13727 fprintf (stderr, "%02x", checksum[cnt]);
13728 putc ('\n', stderr);
13731 static void
13732 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13734 internal_error ("fold check: original tree changed by fold");
13737 static void
13738 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13740 void **slot;
13741 enum tree_code code;
13742 union tree_node buf;
13743 int i, len;
13745 recursive_label:
13747 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13748 <= sizeof (struct tree_function_decl))
13749 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13750 if (expr == NULL)
13751 return;
13752 slot = (void **) htab_find_slot (ht, expr, INSERT);
13753 if (*slot != NULL)
13754 return;
13755 *slot = CONST_CAST_TREE (expr);
13756 code = TREE_CODE (expr);
13757 if (TREE_CODE_CLASS (code) == tcc_declaration
13758 && DECL_ASSEMBLER_NAME_SET_P (expr))
13760 /* Allow DECL_ASSEMBLER_NAME to be modified. */
13761 memcpy ((char *) &buf, expr, tree_size (expr));
13762 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13763 expr = (tree) &buf;
13765 else if (TREE_CODE_CLASS (code) == tcc_type
13766 && (TYPE_POINTER_TO (expr)
13767 || TYPE_REFERENCE_TO (expr)
13768 || TYPE_CACHED_VALUES_P (expr)
13769 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13770 || TYPE_NEXT_VARIANT (expr)))
13772 /* Allow these fields to be modified. */
13773 tree tmp;
13774 memcpy ((char *) &buf, expr, tree_size (expr));
13775 expr = tmp = (tree) &buf;
13776 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13777 TYPE_POINTER_TO (tmp) = NULL;
13778 TYPE_REFERENCE_TO (tmp) = NULL;
13779 TYPE_NEXT_VARIANT (tmp) = NULL;
13780 if (TYPE_CACHED_VALUES_P (tmp))
13782 TYPE_CACHED_VALUES_P (tmp) = 0;
13783 TYPE_CACHED_VALUES (tmp) = NULL;
13786 md5_process_bytes (expr, tree_size (expr), ctx);
13787 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13788 if (TREE_CODE_CLASS (code) != tcc_type
13789 && TREE_CODE_CLASS (code) != tcc_declaration
13790 && code != TREE_LIST
13791 && code != SSA_NAME)
13792 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13793 switch (TREE_CODE_CLASS (code))
13795 case tcc_constant:
13796 switch (code)
13798 case STRING_CST:
13799 md5_process_bytes (TREE_STRING_POINTER (expr),
13800 TREE_STRING_LENGTH (expr), ctx);
13801 break;
13802 case COMPLEX_CST:
13803 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13804 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13805 break;
13806 case VECTOR_CST:
13807 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13808 break;
13809 default:
13810 break;
13812 break;
13813 case tcc_exceptional:
13814 switch (code)
13816 case TREE_LIST:
13817 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13818 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13819 expr = TREE_CHAIN (expr);
13820 goto recursive_label;
13821 break;
13822 case TREE_VEC:
13823 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13824 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13825 break;
13826 default:
13827 break;
13829 break;
13830 case tcc_expression:
13831 case tcc_reference:
13832 case tcc_comparison:
13833 case tcc_unary:
13834 case tcc_binary:
13835 case tcc_statement:
13836 case tcc_vl_exp:
13837 len = TREE_OPERAND_LENGTH (expr);
13838 for (i = 0; i < len; ++i)
13839 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13840 break;
13841 case tcc_declaration:
13842 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13843 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13844 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13846 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13847 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13848 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13849 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13850 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13852 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13853 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13855 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13857 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13858 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13859 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13861 break;
13862 case tcc_type:
13863 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13864 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13865 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13866 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13867 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13868 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13869 if (INTEGRAL_TYPE_P (expr)
13870 || SCALAR_FLOAT_TYPE_P (expr))
13872 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13873 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13875 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13876 if (TREE_CODE (expr) == RECORD_TYPE
13877 || TREE_CODE (expr) == UNION_TYPE
13878 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13879 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13880 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13881 break;
13882 default:
13883 break;
13887 /* Helper function for outputting the checksum of a tree T. When
13888 debugging with gdb, you can "define mynext" to be "next" followed
13889 by "call debug_fold_checksum (op0)", then just trace down till the
13890 outputs differ. */
13892 DEBUG_FUNCTION void
13893 debug_fold_checksum (const_tree t)
13895 int i;
13896 unsigned char checksum[16];
13897 struct md5_ctx ctx;
13898 htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13900 md5_init_ctx (&ctx);
13901 fold_checksum_tree (t, &ctx, ht);
13902 md5_finish_ctx (&ctx, checksum);
13903 htab_empty (ht);
13905 for (i = 0; i < 16; i++)
13906 fprintf (stderr, "%d ", checksum[i]);
13908 fprintf (stderr, "\n");
13911 #endif
13913 /* Fold a unary tree expression with code CODE of type TYPE with an
13914 operand OP0. LOC is the location of the resulting expression.
13915 Return a folded expression if successful. Otherwise, return a tree
13916 expression with code CODE of type TYPE with an operand OP0. */
13918 tree
13919 fold_build1_stat_loc (location_t loc,
13920 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13922 tree tem;
13923 #ifdef ENABLE_FOLD_CHECKING
13924 unsigned char checksum_before[16], checksum_after[16];
13925 struct md5_ctx ctx;
13926 htab_t ht;
13928 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13929 md5_init_ctx (&ctx);
13930 fold_checksum_tree (op0, &ctx, ht);
13931 md5_finish_ctx (&ctx, checksum_before);
13932 htab_empty (ht);
13933 #endif
13935 tem = fold_unary_loc (loc, code, type, op0);
13936 if (!tem)
13937 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
13939 #ifdef ENABLE_FOLD_CHECKING
13940 md5_init_ctx (&ctx);
13941 fold_checksum_tree (op0, &ctx, ht);
13942 md5_finish_ctx (&ctx, checksum_after);
13943 htab_delete (ht);
13945 if (memcmp (checksum_before, checksum_after, 16))
13946 fold_check_failed (op0, tem);
13947 #endif
13948 return tem;
13951 /* Fold a binary tree expression with code CODE of type TYPE with
13952 operands OP0 and OP1. LOC is the location of the resulting
13953 expression. Return a folded expression if successful. Otherwise,
13954 return a tree expression with code CODE of type TYPE with operands
13955 OP0 and OP1. */
13957 tree
13958 fold_build2_stat_loc (location_t loc,
13959 enum tree_code code, tree type, tree op0, tree op1
13960 MEM_STAT_DECL)
13962 tree tem;
13963 #ifdef ENABLE_FOLD_CHECKING
13964 unsigned char checksum_before_op0[16],
13965 checksum_before_op1[16],
13966 checksum_after_op0[16],
13967 checksum_after_op1[16];
13968 struct md5_ctx ctx;
13969 htab_t ht;
13971 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13972 md5_init_ctx (&ctx);
13973 fold_checksum_tree (op0, &ctx, ht);
13974 md5_finish_ctx (&ctx, checksum_before_op0);
13975 htab_empty (ht);
13977 md5_init_ctx (&ctx);
13978 fold_checksum_tree (op1, &ctx, ht);
13979 md5_finish_ctx (&ctx, checksum_before_op1);
13980 htab_empty (ht);
13981 #endif
13983 tem = fold_binary_loc (loc, code, type, op0, op1);
13984 if (!tem)
13985 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13987 #ifdef ENABLE_FOLD_CHECKING
13988 md5_init_ctx (&ctx);
13989 fold_checksum_tree (op0, &ctx, ht);
13990 md5_finish_ctx (&ctx, checksum_after_op0);
13991 htab_empty (ht);
13993 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13994 fold_check_failed (op0, tem);
13996 md5_init_ctx (&ctx);
13997 fold_checksum_tree (op1, &ctx, ht);
13998 md5_finish_ctx (&ctx, checksum_after_op1);
13999 htab_delete (ht);
14001 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14002 fold_check_failed (op1, tem);
14003 #endif
14004 return tem;
14007 /* Fold a ternary tree expression with code CODE of type TYPE with
14008 operands OP0, OP1, and OP2. Return a folded expression if
14009 successful. Otherwise, return a tree expression with code CODE of
14010 type TYPE with operands OP0, OP1, and OP2. */
14012 tree
14013 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14014 tree op0, tree op1, tree op2 MEM_STAT_DECL)
14016 tree tem;
14017 #ifdef ENABLE_FOLD_CHECKING
14018 unsigned char checksum_before_op0[16],
14019 checksum_before_op1[16],
14020 checksum_before_op2[16],
14021 checksum_after_op0[16],
14022 checksum_after_op1[16],
14023 checksum_after_op2[16];
14024 struct md5_ctx ctx;
14025 htab_t ht;
14027 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14028 md5_init_ctx (&ctx);
14029 fold_checksum_tree (op0, &ctx, ht);
14030 md5_finish_ctx (&ctx, checksum_before_op0);
14031 htab_empty (ht);
14033 md5_init_ctx (&ctx);
14034 fold_checksum_tree (op1, &ctx, ht);
14035 md5_finish_ctx (&ctx, checksum_before_op1);
14036 htab_empty (ht);
14038 md5_init_ctx (&ctx);
14039 fold_checksum_tree (op2, &ctx, ht);
14040 md5_finish_ctx (&ctx, checksum_before_op2);
14041 htab_empty (ht);
14042 #endif
14044 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14045 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14046 if (!tem)
14047 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
14049 #ifdef ENABLE_FOLD_CHECKING
14050 md5_init_ctx (&ctx);
14051 fold_checksum_tree (op0, &ctx, ht);
14052 md5_finish_ctx (&ctx, checksum_after_op0);
14053 htab_empty (ht);
14055 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14056 fold_check_failed (op0, tem);
14058 md5_init_ctx (&ctx);
14059 fold_checksum_tree (op1, &ctx, ht);
14060 md5_finish_ctx (&ctx, checksum_after_op1);
14061 htab_empty (ht);
14063 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14064 fold_check_failed (op1, tem);
14066 md5_init_ctx (&ctx);
14067 fold_checksum_tree (op2, &ctx, ht);
14068 md5_finish_ctx (&ctx, checksum_after_op2);
14069 htab_delete (ht);
14071 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14072 fold_check_failed (op2, tem);
14073 #endif
14074 return tem;
14077 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14078 arguments in ARGARRAY, and a null static chain.
14079 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14080 of type TYPE from the given operands as constructed by build_call_array. */
14082 tree
14083 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14084 int nargs, tree *argarray)
14086 tree tem;
14087 #ifdef ENABLE_FOLD_CHECKING
14088 unsigned char checksum_before_fn[16],
14089 checksum_before_arglist[16],
14090 checksum_after_fn[16],
14091 checksum_after_arglist[16];
14092 struct md5_ctx ctx;
14093 htab_t ht;
14094 int i;
14096 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14097 md5_init_ctx (&ctx);
14098 fold_checksum_tree (fn, &ctx, ht);
14099 md5_finish_ctx (&ctx, checksum_before_fn);
14100 htab_empty (ht);
14102 md5_init_ctx (&ctx);
14103 for (i = 0; i < nargs; i++)
14104 fold_checksum_tree (argarray[i], &ctx, ht);
14105 md5_finish_ctx (&ctx, checksum_before_arglist);
14106 htab_empty (ht);
14107 #endif
14109 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14111 #ifdef ENABLE_FOLD_CHECKING
14112 md5_init_ctx (&ctx);
14113 fold_checksum_tree (fn, &ctx, ht);
14114 md5_finish_ctx (&ctx, checksum_after_fn);
14115 htab_empty (ht);
14117 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14118 fold_check_failed (fn, tem);
14120 md5_init_ctx (&ctx);
14121 for (i = 0; i < nargs; i++)
14122 fold_checksum_tree (argarray[i], &ctx, ht);
14123 md5_finish_ctx (&ctx, checksum_after_arglist);
14124 htab_delete (ht);
14126 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14127 fold_check_failed (NULL_TREE, tem);
14128 #endif
14129 return tem;
14132 /* Perform constant folding and related simplification of initializer
14133 expression EXPR. These behave identically to "fold_buildN" but ignore
14134 potential run-time traps and exceptions that fold must preserve. */
14136 #define START_FOLD_INIT \
14137 int saved_signaling_nans = flag_signaling_nans;\
14138 int saved_trapping_math = flag_trapping_math;\
14139 int saved_rounding_math = flag_rounding_math;\
14140 int saved_trapv = flag_trapv;\
14141 int saved_folding_initializer = folding_initializer;\
14142 flag_signaling_nans = 0;\
14143 flag_trapping_math = 0;\
14144 flag_rounding_math = 0;\
14145 flag_trapv = 0;\
14146 folding_initializer = 1;
14148 #define END_FOLD_INIT \
14149 flag_signaling_nans = saved_signaling_nans;\
14150 flag_trapping_math = saved_trapping_math;\
14151 flag_rounding_math = saved_rounding_math;\
14152 flag_trapv = saved_trapv;\
14153 folding_initializer = saved_folding_initializer;
14155 tree
14156 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14157 tree type, tree op)
14159 tree result;
14160 START_FOLD_INIT;
14162 result = fold_build1_loc (loc, code, type, op);
14164 END_FOLD_INIT;
14165 return result;
14168 tree
14169 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14170 tree type, tree op0, tree op1)
14172 tree result;
14173 START_FOLD_INIT;
14175 result = fold_build2_loc (loc, code, type, op0, op1);
14177 END_FOLD_INIT;
14178 return result;
14181 tree
14182 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14183 tree type, tree op0, tree op1, tree op2)
14185 tree result;
14186 START_FOLD_INIT;
14188 result = fold_build3_loc (loc, code, type, op0, op1, op2);
14190 END_FOLD_INIT;
14191 return result;
14194 tree
14195 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14196 int nargs, tree *argarray)
14198 tree result;
14199 START_FOLD_INIT;
14201 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14203 END_FOLD_INIT;
14204 return result;
14207 #undef START_FOLD_INIT
14208 #undef END_FOLD_INIT
14210 /* Determine if first argument is a multiple of second argument. Return 0 if
14211 it is not, or we cannot easily determined it to be.
14213 An example of the sort of thing we care about (at this point; this routine
14214 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14215 fold cases do now) is discovering that
14217 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14219 is a multiple of
14221 SAVE_EXPR (J * 8)
14223 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14225 This code also handles discovering that
14227 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14229 is a multiple of 8 so we don't have to worry about dealing with a
14230 possible remainder.
14232 Note that we *look* inside a SAVE_EXPR only to determine how it was
14233 calculated; it is not safe for fold to do much of anything else with the
14234 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14235 at run time. For example, the latter example above *cannot* be implemented
14236 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14237 evaluation time of the original SAVE_EXPR is not necessarily the same at
14238 the time the new expression is evaluated. The only optimization of this
14239 sort that would be valid is changing
14241 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14243 divided by 8 to
14245 SAVE_EXPR (I) * SAVE_EXPR (J)
14247 (where the same SAVE_EXPR (J) is used in the original and the
14248 transformed version). */
14251 multiple_of_p (tree type, const_tree top, const_tree bottom)
14253 if (operand_equal_p (top, bottom, 0))
14254 return 1;
14256 if (TREE_CODE (type) != INTEGER_TYPE)
14257 return 0;
14259 switch (TREE_CODE (top))
14261 case BIT_AND_EXPR:
14262 /* Bitwise and provides a power of two multiple. If the mask is
14263 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14264 if (!integer_pow2p (bottom))
14265 return 0;
14266 /* FALLTHRU */
14268 case MULT_EXPR:
14269 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14270 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14272 case PLUS_EXPR:
14273 case MINUS_EXPR:
14274 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14275 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14277 case LSHIFT_EXPR:
14278 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14280 tree op1, t1;
14282 op1 = TREE_OPERAND (top, 1);
14283 /* const_binop may not detect overflow correctly,
14284 so check for it explicitly here. */
14285 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14286 > TREE_INT_CST_LOW (op1)
14287 && TREE_INT_CST_HIGH (op1) == 0
14288 && 0 != (t1 = fold_convert (type,
14289 const_binop (LSHIFT_EXPR,
14290 size_one_node,
14291 op1)))
14292 && !TREE_OVERFLOW (t1))
14293 return multiple_of_p (type, t1, bottom);
14295 return 0;
14297 case NOP_EXPR:
14298 /* Can't handle conversions from non-integral or wider integral type. */
14299 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14300 || (TYPE_PRECISION (type)
14301 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14302 return 0;
14304 /* .. fall through ... */
14306 case SAVE_EXPR:
14307 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14309 case COND_EXPR:
14310 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
14311 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
14313 case INTEGER_CST:
14314 if (TREE_CODE (bottom) != INTEGER_CST
14315 || integer_zerop (bottom)
14316 || (TYPE_UNSIGNED (type)
14317 && (tree_int_cst_sgn (top) < 0
14318 || tree_int_cst_sgn (bottom) < 0)))
14319 return 0;
14320 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14321 top, bottom, 0));
14323 default:
14324 return 0;
14328 /* Return true if CODE or TYPE is known to be non-negative. */
14330 static bool
14331 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14333 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14334 && truth_value_p (code))
14335 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14336 have a signed:1 type (where the value is -1 and 0). */
14337 return true;
14338 return false;
14341 /* Return true if (CODE OP0) is known to be non-negative. If the return
14342 value is based on the assumption that signed overflow is undefined,
14343 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14344 *STRICT_OVERFLOW_P. */
14346 bool
14347 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14348 bool *strict_overflow_p)
14350 if (TYPE_UNSIGNED (type))
14351 return true;
14353 switch (code)
14355 case ABS_EXPR:
14356 /* We can't return 1 if flag_wrapv is set because
14357 ABS_EXPR<INT_MIN> = INT_MIN. */
14358 if (!INTEGRAL_TYPE_P (type))
14359 return true;
14360 if (TYPE_OVERFLOW_UNDEFINED (type))
14362 *strict_overflow_p = true;
14363 return true;
14365 break;
14367 case NON_LVALUE_EXPR:
14368 case FLOAT_EXPR:
14369 case FIX_TRUNC_EXPR:
14370 return tree_expr_nonnegative_warnv_p (op0,
14371 strict_overflow_p);
14373 case NOP_EXPR:
14375 tree inner_type = TREE_TYPE (op0);
14376 tree outer_type = type;
14378 if (TREE_CODE (outer_type) == REAL_TYPE)
14380 if (TREE_CODE (inner_type) == REAL_TYPE)
14381 return tree_expr_nonnegative_warnv_p (op0,
14382 strict_overflow_p);
14383 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14385 if (TYPE_UNSIGNED (inner_type))
14386 return true;
14387 return tree_expr_nonnegative_warnv_p (op0,
14388 strict_overflow_p);
14391 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14393 if (TREE_CODE (inner_type) == REAL_TYPE)
14394 return tree_expr_nonnegative_warnv_p (op0,
14395 strict_overflow_p);
14396 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14397 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14398 && TYPE_UNSIGNED (inner_type);
14401 break;
14403 default:
14404 return tree_simple_nonnegative_warnv_p (code, type);
14407 /* We don't know sign of `t', so be conservative and return false. */
14408 return false;
14411 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14412 value is based on the assumption that signed overflow is undefined,
14413 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14414 *STRICT_OVERFLOW_P. */
14416 bool
14417 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14418 tree op1, bool *strict_overflow_p)
14420 if (TYPE_UNSIGNED (type))
14421 return true;
14423 switch (code)
14425 case POINTER_PLUS_EXPR:
14426 case PLUS_EXPR:
14427 if (FLOAT_TYPE_P (type))
14428 return (tree_expr_nonnegative_warnv_p (op0,
14429 strict_overflow_p)
14430 && tree_expr_nonnegative_warnv_p (op1,
14431 strict_overflow_p));
14433 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14434 both unsigned and at least 2 bits shorter than the result. */
14435 if (TREE_CODE (type) == INTEGER_TYPE
14436 && TREE_CODE (op0) == NOP_EXPR
14437 && TREE_CODE (op1) == NOP_EXPR)
14439 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14440 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14441 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14442 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14444 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14445 TYPE_PRECISION (inner2)) + 1;
14446 return prec < TYPE_PRECISION (type);
14449 break;
14451 case MULT_EXPR:
14452 if (FLOAT_TYPE_P (type))
14454 /* x * x for floating point x is always non-negative. */
14455 if (operand_equal_p (op0, op1, 0))
14456 return true;
14457 return (tree_expr_nonnegative_warnv_p (op0,
14458 strict_overflow_p)
14459 && tree_expr_nonnegative_warnv_p (op1,
14460 strict_overflow_p));
14463 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14464 both unsigned and their total bits is shorter than the result. */
14465 if (TREE_CODE (type) == INTEGER_TYPE
14466 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14467 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14469 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14470 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14471 : TREE_TYPE (op0);
14472 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14473 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14474 : TREE_TYPE (op1);
14476 bool unsigned0 = TYPE_UNSIGNED (inner0);
14477 bool unsigned1 = TYPE_UNSIGNED (inner1);
14479 if (TREE_CODE (op0) == INTEGER_CST)
14480 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14482 if (TREE_CODE (op1) == INTEGER_CST)
14483 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14485 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14486 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14488 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14489 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14490 : TYPE_PRECISION (inner0);
14492 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14493 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14494 : TYPE_PRECISION (inner1);
14496 return precision0 + precision1 < TYPE_PRECISION (type);
14499 return false;
14501 case BIT_AND_EXPR:
14502 case MAX_EXPR:
14503 return (tree_expr_nonnegative_warnv_p (op0,
14504 strict_overflow_p)
14505 || tree_expr_nonnegative_warnv_p (op1,
14506 strict_overflow_p));
14508 case BIT_IOR_EXPR:
14509 case BIT_XOR_EXPR:
14510 case MIN_EXPR:
14511 case RDIV_EXPR:
14512 case TRUNC_DIV_EXPR:
14513 case CEIL_DIV_EXPR:
14514 case FLOOR_DIV_EXPR:
14515 case ROUND_DIV_EXPR:
14516 return (tree_expr_nonnegative_warnv_p (op0,
14517 strict_overflow_p)
14518 && tree_expr_nonnegative_warnv_p (op1,
14519 strict_overflow_p));
14521 case TRUNC_MOD_EXPR:
14522 case CEIL_MOD_EXPR:
14523 case FLOOR_MOD_EXPR:
14524 case ROUND_MOD_EXPR:
14525 return tree_expr_nonnegative_warnv_p (op0,
14526 strict_overflow_p);
14527 default:
14528 return tree_simple_nonnegative_warnv_p (code, type);
14531 /* We don't know sign of `t', so be conservative and return false. */
14532 return false;
14535 /* Return true if T is known to be non-negative. If the return
14536 value is based on the assumption that signed overflow is undefined,
14537 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14538 *STRICT_OVERFLOW_P. */
14540 bool
14541 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14543 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14544 return true;
14546 switch (TREE_CODE (t))
14548 case INTEGER_CST:
14549 return tree_int_cst_sgn (t) >= 0;
14551 case REAL_CST:
14552 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14554 case FIXED_CST:
14555 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14557 case COND_EXPR:
14558 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14559 strict_overflow_p)
14560 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14561 strict_overflow_p));
14562 default:
14563 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14564 TREE_TYPE (t));
14566 /* We don't know sign of `t', so be conservative and return false. */
14567 return false;
14570 /* Return true if T is known to be non-negative. If the return
14571 value is based on the assumption that signed overflow is undefined,
14572 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14573 *STRICT_OVERFLOW_P. */
14575 bool
14576 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14577 tree arg0, tree arg1, bool *strict_overflow_p)
14579 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14580 switch (DECL_FUNCTION_CODE (fndecl))
14582 CASE_FLT_FN (BUILT_IN_ACOS):
14583 CASE_FLT_FN (BUILT_IN_ACOSH):
14584 CASE_FLT_FN (BUILT_IN_CABS):
14585 CASE_FLT_FN (BUILT_IN_COSH):
14586 CASE_FLT_FN (BUILT_IN_ERFC):
14587 CASE_FLT_FN (BUILT_IN_EXP):
14588 CASE_FLT_FN (BUILT_IN_EXP10):
14589 CASE_FLT_FN (BUILT_IN_EXP2):
14590 CASE_FLT_FN (BUILT_IN_FABS):
14591 CASE_FLT_FN (BUILT_IN_FDIM):
14592 CASE_FLT_FN (BUILT_IN_HYPOT):
14593 CASE_FLT_FN (BUILT_IN_POW10):
14594 CASE_INT_FN (BUILT_IN_FFS):
14595 CASE_INT_FN (BUILT_IN_PARITY):
14596 CASE_INT_FN (BUILT_IN_POPCOUNT):
14597 case BUILT_IN_BSWAP32:
14598 case BUILT_IN_BSWAP64:
14599 /* Always true. */
14600 return true;
14602 CASE_FLT_FN (BUILT_IN_SQRT):
14603 /* sqrt(-0.0) is -0.0. */
14604 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14605 return true;
14606 return tree_expr_nonnegative_warnv_p (arg0,
14607 strict_overflow_p);
14609 CASE_FLT_FN (BUILT_IN_ASINH):
14610 CASE_FLT_FN (BUILT_IN_ATAN):
14611 CASE_FLT_FN (BUILT_IN_ATANH):
14612 CASE_FLT_FN (BUILT_IN_CBRT):
14613 CASE_FLT_FN (BUILT_IN_CEIL):
14614 CASE_FLT_FN (BUILT_IN_ERF):
14615 CASE_FLT_FN (BUILT_IN_EXPM1):
14616 CASE_FLT_FN (BUILT_IN_FLOOR):
14617 CASE_FLT_FN (BUILT_IN_FMOD):
14618 CASE_FLT_FN (BUILT_IN_FREXP):
14619 CASE_FLT_FN (BUILT_IN_LCEIL):
14620 CASE_FLT_FN (BUILT_IN_LDEXP):
14621 CASE_FLT_FN (BUILT_IN_LFLOOR):
14622 CASE_FLT_FN (BUILT_IN_LLCEIL):
14623 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14624 CASE_FLT_FN (BUILT_IN_LLRINT):
14625 CASE_FLT_FN (BUILT_IN_LLROUND):
14626 CASE_FLT_FN (BUILT_IN_LRINT):
14627 CASE_FLT_FN (BUILT_IN_LROUND):
14628 CASE_FLT_FN (BUILT_IN_MODF):
14629 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14630 CASE_FLT_FN (BUILT_IN_RINT):
14631 CASE_FLT_FN (BUILT_IN_ROUND):
14632 CASE_FLT_FN (BUILT_IN_SCALB):
14633 CASE_FLT_FN (BUILT_IN_SCALBLN):
14634 CASE_FLT_FN (BUILT_IN_SCALBN):
14635 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14636 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14637 CASE_FLT_FN (BUILT_IN_SINH):
14638 CASE_FLT_FN (BUILT_IN_TANH):
14639 CASE_FLT_FN (BUILT_IN_TRUNC):
14640 /* True if the 1st argument is nonnegative. */
14641 return tree_expr_nonnegative_warnv_p (arg0,
14642 strict_overflow_p);
14644 CASE_FLT_FN (BUILT_IN_FMAX):
14645 /* True if the 1st OR 2nd arguments are nonnegative. */
14646 return (tree_expr_nonnegative_warnv_p (arg0,
14647 strict_overflow_p)
14648 || (tree_expr_nonnegative_warnv_p (arg1,
14649 strict_overflow_p)));
14651 CASE_FLT_FN (BUILT_IN_FMIN):
14652 /* True if the 1st AND 2nd arguments are nonnegative. */
14653 return (tree_expr_nonnegative_warnv_p (arg0,
14654 strict_overflow_p)
14655 && (tree_expr_nonnegative_warnv_p (arg1,
14656 strict_overflow_p)));
14658 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14659 /* True if the 2nd argument is nonnegative. */
14660 return tree_expr_nonnegative_warnv_p (arg1,
14661 strict_overflow_p);
14663 CASE_FLT_FN (BUILT_IN_POWI):
14664 /* True if the 1st argument is nonnegative or the second
14665 argument is an even integer. */
14666 if (TREE_CODE (arg1) == INTEGER_CST
14667 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14668 return true;
14669 return tree_expr_nonnegative_warnv_p (arg0,
14670 strict_overflow_p);
14672 CASE_FLT_FN (BUILT_IN_POW):
14673 /* True if the 1st argument is nonnegative or the second
14674 argument is an even integer valued real. */
14675 if (TREE_CODE (arg1) == REAL_CST)
14677 REAL_VALUE_TYPE c;
14678 HOST_WIDE_INT n;
14680 c = TREE_REAL_CST (arg1);
14681 n = real_to_integer (&c);
14682 if ((n & 1) == 0)
14684 REAL_VALUE_TYPE cint;
14685 real_from_integer (&cint, VOIDmode, n,
14686 n < 0 ? -1 : 0, 0);
14687 if (real_identical (&c, &cint))
14688 return true;
14691 return tree_expr_nonnegative_warnv_p (arg0,
14692 strict_overflow_p);
14694 default:
14695 break;
14697 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14698 type);
14701 /* Return true if T is known to be non-negative. If the return
14702 value is based on the assumption that signed overflow is undefined,
14703 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14704 *STRICT_OVERFLOW_P. */
14706 bool
14707 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14709 enum tree_code code = TREE_CODE (t);
14710 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14711 return true;
14713 switch (code)
14715 case TARGET_EXPR:
14717 tree temp = TARGET_EXPR_SLOT (t);
14718 t = TARGET_EXPR_INITIAL (t);
14720 /* If the initializer is non-void, then it's a normal expression
14721 that will be assigned to the slot. */
14722 if (!VOID_TYPE_P (t))
14723 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14725 /* Otherwise, the initializer sets the slot in some way. One common
14726 way is an assignment statement at the end of the initializer. */
14727 while (1)
14729 if (TREE_CODE (t) == BIND_EXPR)
14730 t = expr_last (BIND_EXPR_BODY (t));
14731 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14732 || TREE_CODE (t) == TRY_CATCH_EXPR)
14733 t = expr_last (TREE_OPERAND (t, 0));
14734 else if (TREE_CODE (t) == STATEMENT_LIST)
14735 t = expr_last (t);
14736 else
14737 break;
14739 if (TREE_CODE (t) == MODIFY_EXPR
14740 && TREE_OPERAND (t, 0) == temp)
14741 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14742 strict_overflow_p);
14744 return false;
14747 case CALL_EXPR:
14749 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14750 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14752 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14753 get_callee_fndecl (t),
14754 arg0,
14755 arg1,
14756 strict_overflow_p);
14758 case COMPOUND_EXPR:
14759 case MODIFY_EXPR:
14760 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14761 strict_overflow_p);
14762 case BIND_EXPR:
14763 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14764 strict_overflow_p);
14765 case SAVE_EXPR:
14766 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14767 strict_overflow_p);
14769 default:
14770 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14771 TREE_TYPE (t));
14774 /* We don't know sign of `t', so be conservative and return false. */
14775 return false;
14778 /* Return true if T is known to be non-negative. If the return
14779 value is based on the assumption that signed overflow is undefined,
14780 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14781 *STRICT_OVERFLOW_P. */
14783 bool
14784 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14786 enum tree_code code;
14787 if (t == error_mark_node)
14788 return false;
14790 code = TREE_CODE (t);
14791 switch (TREE_CODE_CLASS (code))
14793 case tcc_binary:
14794 case tcc_comparison:
14795 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14796 TREE_TYPE (t),
14797 TREE_OPERAND (t, 0),
14798 TREE_OPERAND (t, 1),
14799 strict_overflow_p);
14801 case tcc_unary:
14802 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14803 TREE_TYPE (t),
14804 TREE_OPERAND (t, 0),
14805 strict_overflow_p);
14807 case tcc_constant:
14808 case tcc_declaration:
14809 case tcc_reference:
14810 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14812 default:
14813 break;
14816 switch (code)
14818 case TRUTH_AND_EXPR:
14819 case TRUTH_OR_EXPR:
14820 case TRUTH_XOR_EXPR:
14821 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14822 TREE_TYPE (t),
14823 TREE_OPERAND (t, 0),
14824 TREE_OPERAND (t, 1),
14825 strict_overflow_p);
14826 case TRUTH_NOT_EXPR:
14827 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14828 TREE_TYPE (t),
14829 TREE_OPERAND (t, 0),
14830 strict_overflow_p);
14832 case COND_EXPR:
14833 case CONSTRUCTOR:
14834 case OBJ_TYPE_REF:
14835 case ASSERT_EXPR:
14836 case ADDR_EXPR:
14837 case WITH_SIZE_EXPR:
14838 case SSA_NAME:
14839 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14841 default:
14842 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14846 /* Return true if `t' is known to be non-negative. Handle warnings
14847 about undefined signed overflow. */
14849 bool
14850 tree_expr_nonnegative_p (tree t)
14852 bool ret, strict_overflow_p;
14854 strict_overflow_p = false;
14855 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14856 if (strict_overflow_p)
14857 fold_overflow_warning (("assuming signed overflow does not occur when "
14858 "determining that expression is always "
14859 "non-negative"),
14860 WARN_STRICT_OVERFLOW_MISC);
14861 return ret;
14865 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14866 For floating point we further ensure that T is not denormal.
14867 Similar logic is present in nonzero_address in rtlanal.h.
14869 If the return value is based on the assumption that signed overflow
14870 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14871 change *STRICT_OVERFLOW_P. */
14873 bool
14874 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14875 bool *strict_overflow_p)
14877 switch (code)
14879 case ABS_EXPR:
14880 return tree_expr_nonzero_warnv_p (op0,
14881 strict_overflow_p);
14883 case NOP_EXPR:
14885 tree inner_type = TREE_TYPE (op0);
14886 tree outer_type = type;
14888 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14889 && tree_expr_nonzero_warnv_p (op0,
14890 strict_overflow_p));
14892 break;
14894 case NON_LVALUE_EXPR:
14895 return tree_expr_nonzero_warnv_p (op0,
14896 strict_overflow_p);
14898 default:
14899 break;
14902 return false;
14905 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14906 For floating point we further ensure that T is not denormal.
14907 Similar logic is present in nonzero_address in rtlanal.h.
14909 If the return value is based on the assumption that signed overflow
14910 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14911 change *STRICT_OVERFLOW_P. */
14913 bool
14914 tree_binary_nonzero_warnv_p (enum tree_code code,
14915 tree type,
14916 tree op0,
14917 tree op1, bool *strict_overflow_p)
14919 bool sub_strict_overflow_p;
14920 switch (code)
14922 case POINTER_PLUS_EXPR:
14923 case PLUS_EXPR:
14924 if (TYPE_OVERFLOW_UNDEFINED (type))
14926 /* With the presence of negative values it is hard
14927 to say something. */
14928 sub_strict_overflow_p = false;
14929 if (!tree_expr_nonnegative_warnv_p (op0,
14930 &sub_strict_overflow_p)
14931 || !tree_expr_nonnegative_warnv_p (op1,
14932 &sub_strict_overflow_p))
14933 return false;
14934 /* One of operands must be positive and the other non-negative. */
14935 /* We don't set *STRICT_OVERFLOW_P here: even if this value
14936 overflows, on a twos-complement machine the sum of two
14937 nonnegative numbers can never be zero. */
14938 return (tree_expr_nonzero_warnv_p (op0,
14939 strict_overflow_p)
14940 || tree_expr_nonzero_warnv_p (op1,
14941 strict_overflow_p));
14943 break;
14945 case MULT_EXPR:
14946 if (TYPE_OVERFLOW_UNDEFINED (type))
14948 if (tree_expr_nonzero_warnv_p (op0,
14949 strict_overflow_p)
14950 && tree_expr_nonzero_warnv_p (op1,
14951 strict_overflow_p))
14953 *strict_overflow_p = true;
14954 return true;
14957 break;
14959 case MIN_EXPR:
14960 sub_strict_overflow_p = false;
14961 if (tree_expr_nonzero_warnv_p (op0,
14962 &sub_strict_overflow_p)
14963 && tree_expr_nonzero_warnv_p (op1,
14964 &sub_strict_overflow_p))
14966 if (sub_strict_overflow_p)
14967 *strict_overflow_p = true;
14969 break;
14971 case MAX_EXPR:
14972 sub_strict_overflow_p = false;
14973 if (tree_expr_nonzero_warnv_p (op0,
14974 &sub_strict_overflow_p))
14976 if (sub_strict_overflow_p)
14977 *strict_overflow_p = true;
14979 /* When both operands are nonzero, then MAX must be too. */
14980 if (tree_expr_nonzero_warnv_p (op1,
14981 strict_overflow_p))
14982 return true;
14984 /* MAX where operand 0 is positive is positive. */
14985 return tree_expr_nonnegative_warnv_p (op0,
14986 strict_overflow_p);
14988 /* MAX where operand 1 is positive is positive. */
14989 else if (tree_expr_nonzero_warnv_p (op1,
14990 &sub_strict_overflow_p)
14991 && tree_expr_nonnegative_warnv_p (op1,
14992 &sub_strict_overflow_p))
14994 if (sub_strict_overflow_p)
14995 *strict_overflow_p = true;
14996 return true;
14998 break;
15000 case BIT_IOR_EXPR:
15001 return (tree_expr_nonzero_warnv_p (op1,
15002 strict_overflow_p)
15003 || tree_expr_nonzero_warnv_p (op0,
15004 strict_overflow_p));
15006 default:
15007 break;
15010 return false;
15013 /* Return true when T is an address and is known to be nonzero.
15014 For floating point we further ensure that T is not denormal.
15015 Similar logic is present in nonzero_address in rtlanal.h.
15017 If the return value is based on the assumption that signed overflow
15018 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15019 change *STRICT_OVERFLOW_P. */
15021 bool
15022 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15024 bool sub_strict_overflow_p;
15025 switch (TREE_CODE (t))
15027 case INTEGER_CST:
15028 return !integer_zerop (t);
15030 case ADDR_EXPR:
15032 tree base = TREE_OPERAND (t, 0);
15033 if (!DECL_P (base))
15034 base = get_base_address (base);
15036 if (!base)
15037 return false;
15039 /* Weak declarations may link to NULL. Other things may also be NULL
15040 so protect with -fdelete-null-pointer-checks; but not variables
15041 allocated on the stack. */
15042 if (DECL_P (base)
15043 && (flag_delete_null_pointer_checks
15044 || (DECL_CONTEXT (base)
15045 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
15046 && auto_var_in_fn_p (base, DECL_CONTEXT (base)))))
15047 return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15049 /* Constants are never weak. */
15050 if (CONSTANT_CLASS_P (base))
15051 return true;
15053 return false;
15056 case COND_EXPR:
15057 sub_strict_overflow_p = false;
15058 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15059 &sub_strict_overflow_p)
15060 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15061 &sub_strict_overflow_p))
15063 if (sub_strict_overflow_p)
15064 *strict_overflow_p = true;
15065 return true;
15067 break;
15069 default:
15070 break;
15072 return false;
15075 /* Return true when T is an address and is known to be nonzero.
15076 For floating point we further ensure that T is not denormal.
15077 Similar logic is present in nonzero_address in rtlanal.h.
15079 If the return value is based on the assumption that signed overflow
15080 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15081 change *STRICT_OVERFLOW_P. */
15083 bool
15084 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15086 tree type = TREE_TYPE (t);
15087 enum tree_code code;
15089 /* Doing something useful for floating point would need more work. */
15090 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15091 return false;
15093 code = TREE_CODE (t);
15094 switch (TREE_CODE_CLASS (code))
15096 case tcc_unary:
15097 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15098 strict_overflow_p);
15099 case tcc_binary:
15100 case tcc_comparison:
15101 return tree_binary_nonzero_warnv_p (code, type,
15102 TREE_OPERAND (t, 0),
15103 TREE_OPERAND (t, 1),
15104 strict_overflow_p);
15105 case tcc_constant:
15106 case tcc_declaration:
15107 case tcc_reference:
15108 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15110 default:
15111 break;
15114 switch (code)
15116 case TRUTH_NOT_EXPR:
15117 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15118 strict_overflow_p);
15120 case TRUTH_AND_EXPR:
15121 case TRUTH_OR_EXPR:
15122 case TRUTH_XOR_EXPR:
15123 return tree_binary_nonzero_warnv_p (code, type,
15124 TREE_OPERAND (t, 0),
15125 TREE_OPERAND (t, 1),
15126 strict_overflow_p);
15128 case COND_EXPR:
15129 case CONSTRUCTOR:
15130 case OBJ_TYPE_REF:
15131 case ASSERT_EXPR:
15132 case ADDR_EXPR:
15133 case WITH_SIZE_EXPR:
15134 case SSA_NAME:
15135 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15137 case COMPOUND_EXPR:
15138 case MODIFY_EXPR:
15139 case BIND_EXPR:
15140 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15141 strict_overflow_p);
15143 case SAVE_EXPR:
15144 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15145 strict_overflow_p);
15147 case CALL_EXPR:
15148 return alloca_call_p (t);
15150 default:
15151 break;
15153 return false;
15156 /* Return true when T is an address and is known to be nonzero.
15157 Handle warnings about undefined signed overflow. */
15159 bool
15160 tree_expr_nonzero_p (tree t)
15162 bool ret, strict_overflow_p;
15164 strict_overflow_p = false;
15165 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15166 if (strict_overflow_p)
15167 fold_overflow_warning (("assuming signed overflow does not occur when "
15168 "determining that expression is always "
15169 "non-zero"),
15170 WARN_STRICT_OVERFLOW_MISC);
15171 return ret;
15174 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15175 attempt to fold the expression to a constant without modifying TYPE,
15176 OP0 or OP1.
15178 If the expression could be simplified to a constant, then return
15179 the constant. If the expression would not be simplified to a
15180 constant, then return NULL_TREE. */
15182 tree
15183 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15185 tree tem = fold_binary (code, type, op0, op1);
15186 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15189 /* Given the components of a unary expression CODE, TYPE and OP0,
15190 attempt to fold the expression to a constant without modifying
15191 TYPE or OP0.
15193 If the expression could be simplified to a constant, then return
15194 the constant. If the expression would not be simplified to a
15195 constant, then return NULL_TREE. */
15197 tree
15198 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15200 tree tem = fold_unary (code, type, op0);
15201 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15204 /* If EXP represents referencing an element in a constant string
15205 (either via pointer arithmetic or array indexing), return the
15206 tree representing the value accessed, otherwise return NULL. */
15208 tree
15209 fold_read_from_constant_string (tree exp)
15211 if ((TREE_CODE (exp) == INDIRECT_REF
15212 || TREE_CODE (exp) == ARRAY_REF)
15213 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15215 tree exp1 = TREE_OPERAND (exp, 0);
15216 tree index;
15217 tree string;
15218 location_t loc = EXPR_LOCATION (exp);
15220 if (TREE_CODE (exp) == INDIRECT_REF)
15221 string = string_constant (exp1, &index);
15222 else
15224 tree low_bound = array_ref_low_bound (exp);
15225 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15227 /* Optimize the special-case of a zero lower bound.
15229 We convert the low_bound to sizetype to avoid some problems
15230 with constant folding. (E.g. suppose the lower bound is 1,
15231 and its mode is QI. Without the conversion,l (ARRAY
15232 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15233 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15234 if (! integer_zerop (low_bound))
15235 index = size_diffop_loc (loc, index,
15236 fold_convert_loc (loc, sizetype, low_bound));
15238 string = exp1;
15241 if (string
15242 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15243 && TREE_CODE (string) == STRING_CST
15244 && TREE_CODE (index) == INTEGER_CST
15245 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15246 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15247 == MODE_INT)
15248 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15249 return build_int_cst_type (TREE_TYPE (exp),
15250 (TREE_STRING_POINTER (string)
15251 [TREE_INT_CST_LOW (index)]));
15253 return NULL;
15256 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15257 an integer constant, real, or fixed-point constant.
15259 TYPE is the type of the result. */
15261 static tree
15262 fold_negate_const (tree arg0, tree type)
15264 tree t = NULL_TREE;
15266 switch (TREE_CODE (arg0))
15268 case INTEGER_CST:
15270 double_int val = tree_to_double_int (arg0);
15271 int overflow = neg_double (val.low, val.high, &val.low, &val.high);
15273 t = force_fit_type_double (type, val, 1,
15274 (overflow | TREE_OVERFLOW (arg0))
15275 && !TYPE_UNSIGNED (type));
15276 break;
15279 case REAL_CST:
15280 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15281 break;
15283 case FIXED_CST:
15285 FIXED_VALUE_TYPE f;
15286 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15287 &(TREE_FIXED_CST (arg0)), NULL,
15288 TYPE_SATURATING (type));
15289 t = build_fixed (type, f);
15290 /* Propagate overflow flags. */
15291 if (overflow_p | TREE_OVERFLOW (arg0))
15292 TREE_OVERFLOW (t) = 1;
15293 break;
15296 default:
15297 gcc_unreachable ();
15300 return t;
15303 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15304 an integer constant or real constant.
15306 TYPE is the type of the result. */
15308 tree
15309 fold_abs_const (tree arg0, tree type)
15311 tree t = NULL_TREE;
15313 switch (TREE_CODE (arg0))
15315 case INTEGER_CST:
15317 double_int val = tree_to_double_int (arg0);
15319 /* If the value is unsigned or non-negative, then the absolute value
15320 is the same as the ordinary value. */
15321 if (TYPE_UNSIGNED (type)
15322 || !double_int_negative_p (val))
15323 t = arg0;
15325 /* If the value is negative, then the absolute value is
15326 its negation. */
15327 else
15329 int overflow;
15331 overflow = neg_double (val.low, val.high, &val.low, &val.high);
15332 t = force_fit_type_double (type, val, -1,
15333 overflow | TREE_OVERFLOW (arg0));
15336 break;
15338 case REAL_CST:
15339 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15340 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15341 else
15342 t = arg0;
15343 break;
15345 default:
15346 gcc_unreachable ();
15349 return t;
15352 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15353 constant. TYPE is the type of the result. */
15355 static tree
15356 fold_not_const (const_tree arg0, tree type)
15358 double_int val;
15360 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15362 val = double_int_not (tree_to_double_int (arg0));
15363 return force_fit_type_double (type, val, 0, TREE_OVERFLOW (arg0));
15366 /* Given CODE, a relational operator, the target type, TYPE and two
15367 constant operands OP0 and OP1, return the result of the
15368 relational operation. If the result is not a compile time
15369 constant, then return NULL_TREE. */
15371 static tree
15372 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15374 int result, invert;
15376 /* From here on, the only cases we handle are when the result is
15377 known to be a constant. */
15379 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15381 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15382 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15384 /* Handle the cases where either operand is a NaN. */
15385 if (real_isnan (c0) || real_isnan (c1))
15387 switch (code)
15389 case EQ_EXPR:
15390 case ORDERED_EXPR:
15391 result = 0;
15392 break;
15394 case NE_EXPR:
15395 case UNORDERED_EXPR:
15396 case UNLT_EXPR:
15397 case UNLE_EXPR:
15398 case UNGT_EXPR:
15399 case UNGE_EXPR:
15400 case UNEQ_EXPR:
15401 result = 1;
15402 break;
15404 case LT_EXPR:
15405 case LE_EXPR:
15406 case GT_EXPR:
15407 case GE_EXPR:
15408 case LTGT_EXPR:
15409 if (flag_trapping_math)
15410 return NULL_TREE;
15411 result = 0;
15412 break;
15414 default:
15415 gcc_unreachable ();
15418 return constant_boolean_node (result, type);
15421 return constant_boolean_node (real_compare (code, c0, c1), type);
15424 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15426 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15427 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15428 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15431 /* Handle equality/inequality of complex constants. */
15432 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15434 tree rcond = fold_relational_const (code, type,
15435 TREE_REALPART (op0),
15436 TREE_REALPART (op1));
15437 tree icond = fold_relational_const (code, type,
15438 TREE_IMAGPART (op0),
15439 TREE_IMAGPART (op1));
15440 if (code == EQ_EXPR)
15441 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15442 else if (code == NE_EXPR)
15443 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15444 else
15445 return NULL_TREE;
15448 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15450 To compute GT, swap the arguments and do LT.
15451 To compute GE, do LT and invert the result.
15452 To compute LE, swap the arguments, do LT and invert the result.
15453 To compute NE, do EQ and invert the result.
15455 Therefore, the code below must handle only EQ and LT. */
15457 if (code == LE_EXPR || code == GT_EXPR)
15459 tree tem = op0;
15460 op0 = op1;
15461 op1 = tem;
15462 code = swap_tree_comparison (code);
15465 /* Note that it is safe to invert for real values here because we
15466 have already handled the one case that it matters. */
15468 invert = 0;
15469 if (code == NE_EXPR || code == GE_EXPR)
15471 invert = 1;
15472 code = invert_tree_comparison (code, false);
15475 /* Compute a result for LT or EQ if args permit;
15476 Otherwise return T. */
15477 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15479 if (code == EQ_EXPR)
15480 result = tree_int_cst_equal (op0, op1);
15481 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15482 result = INT_CST_LT_UNSIGNED (op0, op1);
15483 else
15484 result = INT_CST_LT (op0, op1);
15486 else
15487 return NULL_TREE;
15489 if (invert)
15490 result ^= 1;
15491 return constant_boolean_node (result, type);
15494 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15495 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15496 itself. */
15498 tree
15499 fold_build_cleanup_point_expr (tree type, tree expr)
15501 /* If the expression does not have side effects then we don't have to wrap
15502 it with a cleanup point expression. */
15503 if (!TREE_SIDE_EFFECTS (expr))
15504 return expr;
15506 /* If the expression is a return, check to see if the expression inside the
15507 return has no side effects or the right hand side of the modify expression
15508 inside the return. If either don't have side effects set we don't need to
15509 wrap the expression in a cleanup point expression. Note we don't check the
15510 left hand side of the modify because it should always be a return decl. */
15511 if (TREE_CODE (expr) == RETURN_EXPR)
15513 tree op = TREE_OPERAND (expr, 0);
15514 if (!op || !TREE_SIDE_EFFECTS (op))
15515 return expr;
15516 op = TREE_OPERAND (op, 1);
15517 if (!TREE_SIDE_EFFECTS (op))
15518 return expr;
15521 return build1 (CLEANUP_POINT_EXPR, type, expr);
15524 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15525 of an indirection through OP0, or NULL_TREE if no simplification is
15526 possible. */
15528 tree
15529 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
15531 tree sub = op0;
15532 tree subtype;
15534 STRIP_NOPS (sub);
15535 subtype = TREE_TYPE (sub);
15536 if (!POINTER_TYPE_P (subtype))
15537 return NULL_TREE;
15539 if (TREE_CODE (sub) == ADDR_EXPR)
15541 tree op = TREE_OPERAND (sub, 0);
15542 tree optype = TREE_TYPE (op);
15543 /* *&CONST_DECL -> to the value of the const decl. */
15544 if (TREE_CODE (op) == CONST_DECL)
15545 return DECL_INITIAL (op);
15546 /* *&p => p; make sure to handle *&"str"[cst] here. */
15547 if (type == optype)
15549 tree fop = fold_read_from_constant_string (op);
15550 if (fop)
15551 return fop;
15552 else
15553 return op;
15555 /* *(foo *)&fooarray => fooarray[0] */
15556 else if (TREE_CODE (optype) == ARRAY_TYPE
15557 && type == TREE_TYPE (optype))
15559 tree type_domain = TYPE_DOMAIN (optype);
15560 tree min_val = size_zero_node;
15561 if (type_domain && TYPE_MIN_VALUE (type_domain))
15562 min_val = TYPE_MIN_VALUE (type_domain);
15563 return build4_loc (loc, ARRAY_REF, type, op, min_val,
15564 NULL_TREE, NULL_TREE);
15566 /* *(foo *)&complexfoo => __real__ complexfoo */
15567 else if (TREE_CODE (optype) == COMPLEX_TYPE
15568 && type == TREE_TYPE (optype))
15569 return fold_build1_loc (loc, REALPART_EXPR, type, op);
15570 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15571 else if (TREE_CODE (optype) == VECTOR_TYPE
15572 && type == TREE_TYPE (optype))
15574 tree part_width = TYPE_SIZE (type);
15575 tree index = bitsize_int (0);
15576 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
15580 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15581 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15583 tree op00 = TREE_OPERAND (sub, 0);
15584 tree op01 = TREE_OPERAND (sub, 1);
15586 STRIP_NOPS (op00);
15587 if (TREE_CODE (op00) == ADDR_EXPR)
15589 tree op00type;
15590 op00 = TREE_OPERAND (op00, 0);
15591 op00type = TREE_TYPE (op00);
15593 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15594 if (TREE_CODE (op00type) == VECTOR_TYPE
15595 && type == TREE_TYPE (op00type))
15597 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15598 tree part_width = TYPE_SIZE (type);
15599 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15600 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15601 tree index = bitsize_int (indexi);
15603 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (op00type))
15604 return fold_build3_loc (loc,
15605 BIT_FIELD_REF, type, op00,
15606 part_width, index);
15609 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15610 else if (TREE_CODE (op00type) == COMPLEX_TYPE
15611 && type == TREE_TYPE (op00type))
15613 tree size = TYPE_SIZE_UNIT (type);
15614 if (tree_int_cst_equal (size, op01))
15615 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
15617 /* ((foo *)&fooarray)[1] => fooarray[1] */
15618 else if (TREE_CODE (op00type) == ARRAY_TYPE
15619 && type == TREE_TYPE (op00type))
15621 tree type_domain = TYPE_DOMAIN (op00type);
15622 tree min_val = size_zero_node;
15623 if (type_domain && TYPE_MIN_VALUE (type_domain))
15624 min_val = TYPE_MIN_VALUE (type_domain);
15625 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
15626 TYPE_SIZE_UNIT (type));
15627 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
15628 return build4_loc (loc, ARRAY_REF, type, op00, op01,
15629 NULL_TREE, NULL_TREE);
15634 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15635 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15636 && type == TREE_TYPE (TREE_TYPE (subtype)))
15638 tree type_domain;
15639 tree min_val = size_zero_node;
15640 sub = build_fold_indirect_ref_loc (loc, sub);
15641 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15642 if (type_domain && TYPE_MIN_VALUE (type_domain))
15643 min_val = TYPE_MIN_VALUE (type_domain);
15644 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
15645 NULL_TREE);
15648 return NULL_TREE;
15651 /* Builds an expression for an indirection through T, simplifying some
15652 cases. */
15654 tree
15655 build_fold_indirect_ref_loc (location_t loc, tree t)
15657 tree type = TREE_TYPE (TREE_TYPE (t));
15658 tree sub = fold_indirect_ref_1 (loc, type, t);
15660 if (sub)
15661 return sub;
15663 return build1_loc (loc, INDIRECT_REF, type, t);
15666 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15668 tree
15669 fold_indirect_ref_loc (location_t loc, tree t)
15671 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
15673 if (sub)
15674 return sub;
15675 else
15676 return t;
15679 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15680 whose result is ignored. The type of the returned tree need not be
15681 the same as the original expression. */
15683 tree
15684 fold_ignored_result (tree t)
15686 if (!TREE_SIDE_EFFECTS (t))
15687 return integer_zero_node;
15689 for (;;)
15690 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15692 case tcc_unary:
15693 t = TREE_OPERAND (t, 0);
15694 break;
15696 case tcc_binary:
15697 case tcc_comparison:
15698 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15699 t = TREE_OPERAND (t, 0);
15700 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15701 t = TREE_OPERAND (t, 1);
15702 else
15703 return t;
15704 break;
15706 case tcc_expression:
15707 switch (TREE_CODE (t))
15709 case COMPOUND_EXPR:
15710 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15711 return t;
15712 t = TREE_OPERAND (t, 0);
15713 break;
15715 case COND_EXPR:
15716 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15717 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15718 return t;
15719 t = TREE_OPERAND (t, 0);
15720 break;
15722 default:
15723 return t;
15725 break;
15727 default:
15728 return t;
15732 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15733 This can only be applied to objects of a sizetype. */
15735 tree
15736 round_up_loc (location_t loc, tree value, int divisor)
15738 tree div = NULL_TREE;
15740 gcc_assert (divisor > 0);
15741 if (divisor == 1)
15742 return value;
15744 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15745 have to do anything. Only do this when we are not given a const,
15746 because in that case, this check is more expensive than just
15747 doing it. */
15748 if (TREE_CODE (value) != INTEGER_CST)
15750 div = build_int_cst (TREE_TYPE (value), divisor);
15752 if (multiple_of_p (TREE_TYPE (value), value, div))
15753 return value;
15756 /* If divisor is a power of two, simplify this to bit manipulation. */
15757 if (divisor == (divisor & -divisor))
15759 if (TREE_CODE (value) == INTEGER_CST)
15761 double_int val = tree_to_double_int (value);
15762 bool overflow_p;
15764 if ((val.low & (divisor - 1)) == 0)
15765 return value;
15767 overflow_p = TREE_OVERFLOW (value);
15768 val.low &= ~(divisor - 1);
15769 val.low += divisor;
15770 if (val.low == 0)
15772 val.high++;
15773 if (val.high == 0)
15774 overflow_p = true;
15777 return force_fit_type_double (TREE_TYPE (value), val,
15778 -1, overflow_p);
15780 else
15782 tree t;
15784 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15785 value = size_binop_loc (loc, PLUS_EXPR, value, t);
15786 t = build_int_cst (TREE_TYPE (value), -divisor);
15787 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15790 else
15792 if (!div)
15793 div = build_int_cst (TREE_TYPE (value), divisor);
15794 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
15795 value = size_binop_loc (loc, MULT_EXPR, value, div);
15798 return value;
15801 /* Likewise, but round down. */
15803 tree
15804 round_down_loc (location_t loc, tree value, int divisor)
15806 tree div = NULL_TREE;
15808 gcc_assert (divisor > 0);
15809 if (divisor == 1)
15810 return value;
15812 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15813 have to do anything. Only do this when we are not given a const,
15814 because in that case, this check is more expensive than just
15815 doing it. */
15816 if (TREE_CODE (value) != INTEGER_CST)
15818 div = build_int_cst (TREE_TYPE (value), divisor);
15820 if (multiple_of_p (TREE_TYPE (value), value, div))
15821 return value;
15824 /* If divisor is a power of two, simplify this to bit manipulation. */
15825 if (divisor == (divisor & -divisor))
15827 tree t;
15829 t = build_int_cst (TREE_TYPE (value), -divisor);
15830 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
15832 else
15834 if (!div)
15835 div = build_int_cst (TREE_TYPE (value), divisor);
15836 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
15837 value = size_binop_loc (loc, MULT_EXPR, value, div);
15840 return value;
15843 /* Returns the pointer to the base of the object addressed by EXP and
15844 extracts the information about the offset of the access, storing it
15845 to PBITPOS and POFFSET. */
15847 static tree
15848 split_address_to_core_and_offset (tree exp,
15849 HOST_WIDE_INT *pbitpos, tree *poffset)
15851 tree core;
15852 enum machine_mode mode;
15853 int unsignedp, volatilep;
15854 HOST_WIDE_INT bitsize;
15855 location_t loc = EXPR_LOCATION (exp);
15857 if (TREE_CODE (exp) == ADDR_EXPR)
15859 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15860 poffset, &mode, &unsignedp, &volatilep,
15861 false);
15862 core = build_fold_addr_expr_loc (loc, core);
15864 else
15866 core = exp;
15867 *pbitpos = 0;
15868 *poffset = NULL_TREE;
15871 return core;
15874 /* Returns true if addresses of E1 and E2 differ by a constant, false
15875 otherwise. If they do, E1 - E2 is stored in *DIFF. */
15877 bool
15878 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15880 tree core1, core2;
15881 HOST_WIDE_INT bitpos1, bitpos2;
15882 tree toffset1, toffset2, tdiff, type;
15884 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15885 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15887 if (bitpos1 % BITS_PER_UNIT != 0
15888 || bitpos2 % BITS_PER_UNIT != 0
15889 || !operand_equal_p (core1, core2, 0))
15890 return false;
15892 if (toffset1 && toffset2)
15894 type = TREE_TYPE (toffset1);
15895 if (type != TREE_TYPE (toffset2))
15896 toffset2 = fold_convert (type, toffset2);
15898 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15899 if (!cst_and_fits_in_hwi (tdiff))
15900 return false;
15902 *diff = int_cst_value (tdiff);
15904 else if (toffset1 || toffset2)
15906 /* If only one of the offsets is non-constant, the difference cannot
15907 be a constant. */
15908 return false;
15910 else
15911 *diff = 0;
15913 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15914 return true;
15917 /* Simplify the floating point expression EXP when the sign of the
15918 result is not significant. Return NULL_TREE if no simplification
15919 is possible. */
15921 tree
15922 fold_strip_sign_ops (tree exp)
15924 tree arg0, arg1;
15925 location_t loc = EXPR_LOCATION (exp);
15927 switch (TREE_CODE (exp))
15929 case ABS_EXPR:
15930 case NEGATE_EXPR:
15931 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15932 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15934 case MULT_EXPR:
15935 case RDIV_EXPR:
15936 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15937 return NULL_TREE;
15938 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15939 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15940 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15941 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
15942 arg0 ? arg0 : TREE_OPERAND (exp, 0),
15943 arg1 ? arg1 : TREE_OPERAND (exp, 1));
15944 break;
15946 case COMPOUND_EXPR:
15947 arg0 = TREE_OPERAND (exp, 0);
15948 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15949 if (arg1)
15950 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15951 break;
15953 case COND_EXPR:
15954 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15955 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15956 if (arg0 || arg1)
15957 return fold_build3_loc (loc,
15958 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15959 arg0 ? arg0 : TREE_OPERAND (exp, 1),
15960 arg1 ? arg1 : TREE_OPERAND (exp, 2));
15961 break;
15963 case CALL_EXPR:
15965 const enum built_in_function fcode = builtin_mathfn_code (exp);
15966 switch (fcode)
15968 CASE_FLT_FN (BUILT_IN_COPYSIGN):
15969 /* Strip copysign function call, return the 1st argument. */
15970 arg0 = CALL_EXPR_ARG (exp, 0);
15971 arg1 = CALL_EXPR_ARG (exp, 1);
15972 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
15974 default:
15975 /* Strip sign ops from the argument of "odd" math functions. */
15976 if (negate_mathfn_p (fcode))
15978 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15979 if (arg0)
15980 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
15982 break;
15985 break;
15987 default:
15988 break;
15990 return NULL_TREE;