2016-01-26 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / fold-const.c
blobbece8d74b46b64afad8ee9465b4b50f80a87ccad
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
43 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "backend.h"
47 #include "target.h"
48 #include "rtl.h"
49 #include "tree.h"
50 #include "gimple.h"
51 #include "predict.h"
52 #include "tm_p.h"
53 #include "tree-ssa-operands.h"
54 #include "optabs-query.h"
55 #include "cgraph.h"
56 #include "diagnostic-core.h"
57 #include "flags.h"
58 #include "alias.h"
59 #include "fold-const.h"
60 #include "fold-const-call.h"
61 #include "stor-layout.h"
62 #include "calls.h"
63 #include "tree-iterator.h"
64 #include "expr.h"
65 #include "intl.h"
66 #include "langhooks.h"
67 #include "tree-eh.h"
68 #include "gimplify.h"
69 #include "tree-dfa.h"
70 #include "builtins.h"
71 #include "generic-match.h"
72 #include "gimple-fold.h"
73 #include "params.h"
74 #include "tree-into-ssa.h"
75 #include "md5.h"
76 #include "case-cfn-macros.h"
77 #include "stringpool.h"
78 #include "tree-ssanames.h"
80 #ifndef LOAD_EXTEND_OP
81 #define LOAD_EXTEND_OP(M) UNKNOWN
82 #endif
84 /* Nonzero if we are folding constants inside an initializer; zero
85 otherwise. */
86 int folding_initializer = 0;
88 /* The following constants represent a bit based encoding of GCC's
89 comparison operators. This encoding simplifies transformations
90 on relational comparison operators, such as AND and OR. */
91 enum comparison_code {
92 COMPCODE_FALSE = 0,
93 COMPCODE_LT = 1,
94 COMPCODE_EQ = 2,
95 COMPCODE_LE = 3,
96 COMPCODE_GT = 4,
97 COMPCODE_LTGT = 5,
98 COMPCODE_GE = 6,
99 COMPCODE_ORD = 7,
100 COMPCODE_UNORD = 8,
101 COMPCODE_UNLT = 9,
102 COMPCODE_UNEQ = 10,
103 COMPCODE_UNLE = 11,
104 COMPCODE_UNGT = 12,
105 COMPCODE_NE = 13,
106 COMPCODE_UNGE = 14,
107 COMPCODE_TRUE = 15
110 static bool negate_expr_p (tree);
111 static tree negate_expr (tree);
112 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
113 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
114 static enum comparison_code comparison_to_compcode (enum tree_code);
115 static enum tree_code compcode_to_comparison (enum comparison_code);
116 static int operand_equal_for_comparison_p (tree, tree, tree);
117 static int twoval_comparison_p (tree, tree *, tree *, int *);
118 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
119 static tree make_bit_field_ref (location_t, tree, tree,
120 HOST_WIDE_INT, HOST_WIDE_INT, int, int);
121 static tree optimize_bit_field_compare (location_t, enum tree_code,
122 tree, tree, tree);
123 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
124 HOST_WIDE_INT *,
125 machine_mode *, int *, int *, int *,
126 tree *, tree *);
127 static int simple_operand_p (const_tree);
128 static bool simple_operand_p_2 (tree);
129 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
130 static tree range_predecessor (tree);
131 static tree range_successor (tree);
132 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
133 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
134 static tree unextend (tree, int, int, tree);
135 static tree optimize_minmax_comparison (location_t, enum tree_code,
136 tree, tree, tree);
137 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
138 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
139 static tree fold_binary_op_with_conditional_arg (location_t,
140 enum tree_code, tree,
141 tree, tree,
142 tree, tree, int);
143 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
144 static bool reorder_operands_p (const_tree, const_tree);
145 static tree fold_negate_const (tree, tree);
146 static tree fold_not_const (const_tree, tree);
147 static tree fold_relational_const (enum tree_code, tree, tree, tree);
148 static tree fold_convert_const (enum tree_code, tree, tree);
149 static tree fold_view_convert_expr (tree, tree);
150 static bool vec_cst_ctor_to_array (tree, tree *);
153 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
154 Otherwise, return LOC. */
156 static location_t
157 expr_location_or (tree t, location_t loc)
159 location_t tloc = EXPR_LOCATION (t);
160 return tloc == UNKNOWN_LOCATION ? loc : tloc;
163 /* Similar to protected_set_expr_location, but never modify x in place,
164 if location can and needs to be set, unshare it. */
166 static inline tree
167 protected_set_expr_location_unshare (tree x, location_t loc)
169 if (CAN_HAVE_LOCATION_P (x)
170 && EXPR_LOCATION (x) != loc
171 && !(TREE_CODE (x) == SAVE_EXPR
172 || TREE_CODE (x) == TARGET_EXPR
173 || TREE_CODE (x) == BIND_EXPR))
175 x = copy_node (x);
176 SET_EXPR_LOCATION (x, loc);
178 return x;
181 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
182 division and returns the quotient. Otherwise returns
183 NULL_TREE. */
185 tree
186 div_if_zero_remainder (const_tree arg1, const_tree arg2)
188 widest_int quo;
190 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
191 SIGNED, &quo))
192 return wide_int_to_tree (TREE_TYPE (arg1), quo);
194 return NULL_TREE;
197 /* This is nonzero if we should defer warnings about undefined
198 overflow. This facility exists because these warnings are a
199 special case. The code to estimate loop iterations does not want
200 to issue any warnings, since it works with expressions which do not
201 occur in user code. Various bits of cleanup code call fold(), but
202 only use the result if it has certain characteristics (e.g., is a
203 constant); that code only wants to issue a warning if the result is
204 used. */
206 static int fold_deferring_overflow_warnings;
208 /* If a warning about undefined overflow is deferred, this is the
209 warning. Note that this may cause us to turn two warnings into
210 one, but that is fine since it is sufficient to only give one
211 warning per expression. */
213 static const char* fold_deferred_overflow_warning;
215 /* If a warning about undefined overflow is deferred, this is the
216 level at which the warning should be emitted. */
218 static enum warn_strict_overflow_code fold_deferred_overflow_code;
220 /* Start deferring overflow warnings. We could use a stack here to
221 permit nested calls, but at present it is not necessary. */
223 void
224 fold_defer_overflow_warnings (void)
226 ++fold_deferring_overflow_warnings;
229 /* Stop deferring overflow warnings. If there is a pending warning,
230 and ISSUE is true, then issue the warning if appropriate. STMT is
231 the statement with which the warning should be associated (used for
232 location information); STMT may be NULL. CODE is the level of the
233 warning--a warn_strict_overflow_code value. This function will use
234 the smaller of CODE and the deferred code when deciding whether to
235 issue the warning. CODE may be zero to mean to always use the
236 deferred code. */
238 void
239 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
241 const char *warnmsg;
242 location_t locus;
244 gcc_assert (fold_deferring_overflow_warnings > 0);
245 --fold_deferring_overflow_warnings;
246 if (fold_deferring_overflow_warnings > 0)
248 if (fold_deferred_overflow_warning != NULL
249 && code != 0
250 && code < (int) fold_deferred_overflow_code)
251 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
252 return;
255 warnmsg = fold_deferred_overflow_warning;
256 fold_deferred_overflow_warning = NULL;
258 if (!issue || warnmsg == NULL)
259 return;
261 if (gimple_no_warning_p (stmt))
262 return;
264 /* Use the smallest code level when deciding to issue the
265 warning. */
266 if (code == 0 || code > (int) fold_deferred_overflow_code)
267 code = fold_deferred_overflow_code;
269 if (!issue_strict_overflow_warning (code))
270 return;
272 if (stmt == NULL)
273 locus = input_location;
274 else
275 locus = gimple_location (stmt);
276 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
279 /* Stop deferring overflow warnings, ignoring any deferred
280 warnings. */
282 void
283 fold_undefer_and_ignore_overflow_warnings (void)
285 fold_undefer_overflow_warnings (false, NULL, 0);
288 /* Whether we are deferring overflow warnings. */
290 bool
291 fold_deferring_overflow_warnings_p (void)
293 return fold_deferring_overflow_warnings > 0;
296 /* This is called when we fold something based on the fact that signed
297 overflow is undefined. */
299 static void
300 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
302 if (fold_deferring_overflow_warnings > 0)
304 if (fold_deferred_overflow_warning == NULL
305 || wc < fold_deferred_overflow_code)
307 fold_deferred_overflow_warning = gmsgid;
308 fold_deferred_overflow_code = wc;
311 else if (issue_strict_overflow_warning (wc))
312 warning (OPT_Wstrict_overflow, gmsgid);
315 /* Return true if the built-in mathematical function specified by CODE
316 is odd, i.e. -f(x) == f(-x). */
318 bool
319 negate_mathfn_p (combined_fn fn)
321 switch (fn)
323 CASE_CFN_ASIN:
324 CASE_CFN_ASINH:
325 CASE_CFN_ATAN:
326 CASE_CFN_ATANH:
327 CASE_CFN_CASIN:
328 CASE_CFN_CASINH:
329 CASE_CFN_CATAN:
330 CASE_CFN_CATANH:
331 CASE_CFN_CBRT:
332 CASE_CFN_CPROJ:
333 CASE_CFN_CSIN:
334 CASE_CFN_CSINH:
335 CASE_CFN_CTAN:
336 CASE_CFN_CTANH:
337 CASE_CFN_ERF:
338 CASE_CFN_LLROUND:
339 CASE_CFN_LROUND:
340 CASE_CFN_ROUND:
341 CASE_CFN_SIN:
342 CASE_CFN_SINH:
343 CASE_CFN_TAN:
344 CASE_CFN_TANH:
345 CASE_CFN_TRUNC:
346 return true;
348 CASE_CFN_LLRINT:
349 CASE_CFN_LRINT:
350 CASE_CFN_NEARBYINT:
351 CASE_CFN_RINT:
352 return !flag_rounding_math;
354 default:
355 break;
357 return false;
360 /* Check whether we may negate an integer constant T without causing
361 overflow. */
363 bool
364 may_negate_without_overflow_p (const_tree t)
366 tree type;
368 gcc_assert (TREE_CODE (t) == INTEGER_CST);
370 type = TREE_TYPE (t);
371 if (TYPE_UNSIGNED (type))
372 return false;
374 return !wi::only_sign_bit_p (t);
377 /* Determine whether an expression T can be cheaply negated using
378 the function negate_expr without introducing undefined overflow. */
380 static bool
381 negate_expr_p (tree t)
383 tree type;
385 if (t == 0)
386 return false;
388 type = TREE_TYPE (t);
390 STRIP_SIGN_NOPS (t);
391 switch (TREE_CODE (t))
393 case INTEGER_CST:
394 if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
395 return true;
397 /* Check that -CST will not overflow type. */
398 return may_negate_without_overflow_p (t);
399 case BIT_NOT_EXPR:
400 return (INTEGRAL_TYPE_P (type)
401 && TYPE_OVERFLOW_WRAPS (type));
403 case FIXED_CST:
404 return true;
406 case NEGATE_EXPR:
407 return !TYPE_OVERFLOW_SANITIZED (type);
409 case REAL_CST:
410 /* We want to canonicalize to positive real constants. Pretend
411 that only negative ones can be easily negated. */
412 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
414 case COMPLEX_CST:
415 return negate_expr_p (TREE_REALPART (t))
416 && negate_expr_p (TREE_IMAGPART (t));
418 case VECTOR_CST:
420 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
421 return true;
423 int count = TYPE_VECTOR_SUBPARTS (type), i;
425 for (i = 0; i < count; i++)
426 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
427 return false;
429 return true;
432 case COMPLEX_EXPR:
433 return negate_expr_p (TREE_OPERAND (t, 0))
434 && negate_expr_p (TREE_OPERAND (t, 1));
436 case CONJ_EXPR:
437 return negate_expr_p (TREE_OPERAND (t, 0));
439 case PLUS_EXPR:
440 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
441 || HONOR_SIGNED_ZEROS (element_mode (type))
442 || (INTEGRAL_TYPE_P (type)
443 && ! TYPE_OVERFLOW_WRAPS (type)))
444 return false;
445 /* -(A + B) -> (-B) - A. */
446 if (negate_expr_p (TREE_OPERAND (t, 1))
447 && reorder_operands_p (TREE_OPERAND (t, 0),
448 TREE_OPERAND (t, 1)))
449 return true;
450 /* -(A + B) -> (-A) - B. */
451 return negate_expr_p (TREE_OPERAND (t, 0));
453 case MINUS_EXPR:
454 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
455 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
456 && !HONOR_SIGNED_ZEROS (element_mode (type))
457 && (! INTEGRAL_TYPE_P (type)
458 || TYPE_OVERFLOW_WRAPS (type))
459 && reorder_operands_p (TREE_OPERAND (t, 0),
460 TREE_OPERAND (t, 1));
462 case MULT_EXPR:
463 if (TYPE_UNSIGNED (type))
464 break;
465 /* INT_MIN/n * n doesn't overflow while negating one operand it does
466 if n is a power of two. */
467 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
468 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
469 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
470 && ! integer_pow2p (TREE_OPERAND (t, 0)))
471 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
472 && ! integer_pow2p (TREE_OPERAND (t, 1)))))
473 break;
475 /* Fall through. */
477 case RDIV_EXPR:
478 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
479 return negate_expr_p (TREE_OPERAND (t, 1))
480 || negate_expr_p (TREE_OPERAND (t, 0));
481 break;
483 case TRUNC_DIV_EXPR:
484 case ROUND_DIV_EXPR:
485 case EXACT_DIV_EXPR:
486 if (TYPE_UNSIGNED (type))
487 break;
488 if (negate_expr_p (TREE_OPERAND (t, 0)))
489 return true;
490 /* In general we can't negate B in A / B, because if A is INT_MIN and
491 B is 1, we may turn this into INT_MIN / -1 which is undefined
492 and actually traps on some architectures. */
493 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
494 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
495 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
496 && ! integer_onep (TREE_OPERAND (t, 1))))
497 return negate_expr_p (TREE_OPERAND (t, 1));
498 break;
500 case NOP_EXPR:
501 /* Negate -((double)float) as (double)(-float). */
502 if (TREE_CODE (type) == REAL_TYPE)
504 tree tem = strip_float_extensions (t);
505 if (tem != t)
506 return negate_expr_p (tem);
508 break;
510 case CALL_EXPR:
511 /* Negate -f(x) as f(-x). */
512 if (negate_mathfn_p (get_call_combined_fn (t)))
513 return negate_expr_p (CALL_EXPR_ARG (t, 0));
514 break;
516 case RSHIFT_EXPR:
517 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
518 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
520 tree op1 = TREE_OPERAND (t, 1);
521 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
522 return true;
524 break;
526 default:
527 break;
529 return false;
532 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
533 simplification is possible.
534 If negate_expr_p would return true for T, NULL_TREE will never be
535 returned. */
537 static tree
538 fold_negate_expr (location_t loc, tree t)
540 tree type = TREE_TYPE (t);
541 tree tem;
543 switch (TREE_CODE (t))
545 /* Convert - (~A) to A + 1. */
546 case BIT_NOT_EXPR:
547 if (INTEGRAL_TYPE_P (type))
548 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
549 build_one_cst (type));
550 break;
552 case INTEGER_CST:
553 tem = fold_negate_const (t, type);
554 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
555 || (ANY_INTEGRAL_TYPE_P (type)
556 && !TYPE_OVERFLOW_TRAPS (type)
557 && TYPE_OVERFLOW_WRAPS (type))
558 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
559 return tem;
560 break;
562 case REAL_CST:
563 tem = fold_negate_const (t, type);
564 return tem;
566 case FIXED_CST:
567 tem = fold_negate_const (t, type);
568 return tem;
570 case COMPLEX_CST:
572 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
573 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
574 if (rpart && ipart)
575 return build_complex (type, rpart, ipart);
577 break;
579 case VECTOR_CST:
581 int count = TYPE_VECTOR_SUBPARTS (type), i;
582 tree *elts = XALLOCAVEC (tree, count);
584 for (i = 0; i < count; i++)
586 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
587 if (elts[i] == NULL_TREE)
588 return NULL_TREE;
591 return build_vector (type, elts);
594 case COMPLEX_EXPR:
595 if (negate_expr_p (t))
596 return fold_build2_loc (loc, COMPLEX_EXPR, type,
597 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
598 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
599 break;
601 case CONJ_EXPR:
602 if (negate_expr_p (t))
603 return fold_build1_loc (loc, CONJ_EXPR, type,
604 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
605 break;
607 case NEGATE_EXPR:
608 if (!TYPE_OVERFLOW_SANITIZED (type))
609 return TREE_OPERAND (t, 0);
610 break;
612 case PLUS_EXPR:
613 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
614 && !HONOR_SIGNED_ZEROS (element_mode (type)))
616 /* -(A + B) -> (-B) - A. */
617 if (negate_expr_p (TREE_OPERAND (t, 1))
618 && reorder_operands_p (TREE_OPERAND (t, 0),
619 TREE_OPERAND (t, 1)))
621 tem = negate_expr (TREE_OPERAND (t, 1));
622 return fold_build2_loc (loc, MINUS_EXPR, type,
623 tem, TREE_OPERAND (t, 0));
626 /* -(A + B) -> (-A) - B. */
627 if (negate_expr_p (TREE_OPERAND (t, 0)))
629 tem = negate_expr (TREE_OPERAND (t, 0));
630 return fold_build2_loc (loc, MINUS_EXPR, type,
631 tem, TREE_OPERAND (t, 1));
634 break;
636 case MINUS_EXPR:
637 /* - (A - B) -> B - A */
638 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
639 && !HONOR_SIGNED_ZEROS (element_mode (type))
640 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
641 return fold_build2_loc (loc, MINUS_EXPR, type,
642 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
643 break;
645 case MULT_EXPR:
646 if (TYPE_UNSIGNED (type))
647 break;
649 /* Fall through. */
651 case RDIV_EXPR:
652 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
654 tem = TREE_OPERAND (t, 1);
655 if (negate_expr_p (tem))
656 return fold_build2_loc (loc, TREE_CODE (t), type,
657 TREE_OPERAND (t, 0), negate_expr (tem));
658 tem = TREE_OPERAND (t, 0);
659 if (negate_expr_p (tem))
660 return fold_build2_loc (loc, TREE_CODE (t), type,
661 negate_expr (tem), TREE_OPERAND (t, 1));
663 break;
665 case TRUNC_DIV_EXPR:
666 case ROUND_DIV_EXPR:
667 case EXACT_DIV_EXPR:
668 if (TYPE_UNSIGNED (type))
669 break;
670 if (negate_expr_p (TREE_OPERAND (t, 0)))
671 return fold_build2_loc (loc, TREE_CODE (t), type,
672 negate_expr (TREE_OPERAND (t, 0)),
673 TREE_OPERAND (t, 1));
674 /* In general we can't negate B in A / B, because if A is INT_MIN and
675 B is 1, we may turn this into INT_MIN / -1 which is undefined
676 and actually traps on some architectures. */
677 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
678 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
679 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
680 && ! integer_onep (TREE_OPERAND (t, 1))))
681 && negate_expr_p (TREE_OPERAND (t, 1)))
682 return fold_build2_loc (loc, TREE_CODE (t), type,
683 TREE_OPERAND (t, 0),
684 negate_expr (TREE_OPERAND (t, 1)));
685 break;
687 case NOP_EXPR:
688 /* Convert -((double)float) into (double)(-float). */
689 if (TREE_CODE (type) == REAL_TYPE)
691 tem = strip_float_extensions (t);
692 if (tem != t && negate_expr_p (tem))
693 return fold_convert_loc (loc, type, negate_expr (tem));
695 break;
697 case CALL_EXPR:
698 /* Negate -f(x) as f(-x). */
699 if (negate_mathfn_p (get_call_combined_fn (t))
700 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
702 tree fndecl, arg;
704 fndecl = get_callee_fndecl (t);
705 arg = negate_expr (CALL_EXPR_ARG (t, 0));
706 return build_call_expr_loc (loc, fndecl, 1, arg);
708 break;
710 case RSHIFT_EXPR:
711 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
712 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
714 tree op1 = TREE_OPERAND (t, 1);
715 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
717 tree ntype = TYPE_UNSIGNED (type)
718 ? signed_type_for (type)
719 : unsigned_type_for (type);
720 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
721 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
722 return fold_convert_loc (loc, type, temp);
725 break;
727 default:
728 break;
731 return NULL_TREE;
734 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
735 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
736 return NULL_TREE. */
738 static tree
739 negate_expr (tree t)
741 tree type, tem;
742 location_t loc;
744 if (t == NULL_TREE)
745 return NULL_TREE;
747 loc = EXPR_LOCATION (t);
748 type = TREE_TYPE (t);
749 STRIP_SIGN_NOPS (t);
751 tem = fold_negate_expr (loc, t);
752 if (!tem)
753 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
754 return fold_convert_loc (loc, type, tem);
757 /* Split a tree IN into a constant, literal and variable parts that could be
758 combined with CODE to make IN. "constant" means an expression with
759 TREE_CONSTANT but that isn't an actual constant. CODE must be a
760 commutative arithmetic operation. Store the constant part into *CONP,
761 the literal in *LITP and return the variable part. If a part isn't
762 present, set it to null. If the tree does not decompose in this way,
763 return the entire tree as the variable part and the other parts as null.
765 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
766 case, we negate an operand that was subtracted. Except if it is a
767 literal for which we use *MINUS_LITP instead.
769 If NEGATE_P is true, we are negating all of IN, again except a literal
770 for which we use *MINUS_LITP instead.
772 If IN is itself a literal or constant, return it as appropriate.
774 Note that we do not guarantee that any of the three values will be the
775 same type as IN, but they will have the same signedness and mode. */
777 static tree
778 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
779 tree *minus_litp, int negate_p)
781 tree var = 0;
783 *conp = 0;
784 *litp = 0;
785 *minus_litp = 0;
787 /* Strip any conversions that don't change the machine mode or signedness. */
788 STRIP_SIGN_NOPS (in);
790 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
791 || TREE_CODE (in) == FIXED_CST)
792 *litp = in;
793 else if (TREE_CODE (in) == code
794 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
795 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
796 /* We can associate addition and subtraction together (even
797 though the C standard doesn't say so) for integers because
798 the value is not affected. For reals, the value might be
799 affected, so we can't. */
800 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
801 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
803 tree op0 = TREE_OPERAND (in, 0);
804 tree op1 = TREE_OPERAND (in, 1);
805 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
806 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
808 /* First see if either of the operands is a literal, then a constant. */
809 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
810 || TREE_CODE (op0) == FIXED_CST)
811 *litp = op0, op0 = 0;
812 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
813 || TREE_CODE (op1) == FIXED_CST)
814 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
816 if (op0 != 0 && TREE_CONSTANT (op0))
817 *conp = op0, op0 = 0;
818 else if (op1 != 0 && TREE_CONSTANT (op1))
819 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
821 /* If we haven't dealt with either operand, this is not a case we can
822 decompose. Otherwise, VAR is either of the ones remaining, if any. */
823 if (op0 != 0 && op1 != 0)
824 var = in;
825 else if (op0 != 0)
826 var = op0;
827 else
828 var = op1, neg_var_p = neg1_p;
830 /* Now do any needed negations. */
831 if (neg_litp_p)
832 *minus_litp = *litp, *litp = 0;
833 if (neg_conp_p)
834 *conp = negate_expr (*conp);
835 if (neg_var_p)
836 var = negate_expr (var);
838 else if (TREE_CODE (in) == BIT_NOT_EXPR
839 && code == PLUS_EXPR)
841 /* -X - 1 is folded to ~X, undo that here. */
842 *minus_litp = build_one_cst (TREE_TYPE (in));
843 var = negate_expr (TREE_OPERAND (in, 0));
845 else if (TREE_CONSTANT (in))
846 *conp = in;
847 else
848 var = in;
850 if (negate_p)
852 if (*litp)
853 *minus_litp = *litp, *litp = 0;
854 else if (*minus_litp)
855 *litp = *minus_litp, *minus_litp = 0;
856 *conp = negate_expr (*conp);
857 var = negate_expr (var);
860 return var;
863 /* Re-associate trees split by the above function. T1 and T2 are
864 either expressions to associate or null. Return the new
865 expression, if any. LOC is the location of the new expression. If
866 we build an operation, do it in TYPE and with CODE. */
868 static tree
869 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
871 if (t1 == 0)
872 return t2;
873 else if (t2 == 0)
874 return t1;
876 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
877 try to fold this since we will have infinite recursion. But do
878 deal with any NEGATE_EXPRs. */
879 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
880 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
882 if (code == PLUS_EXPR)
884 if (TREE_CODE (t1) == NEGATE_EXPR)
885 return build2_loc (loc, MINUS_EXPR, type,
886 fold_convert_loc (loc, type, t2),
887 fold_convert_loc (loc, type,
888 TREE_OPERAND (t1, 0)));
889 else if (TREE_CODE (t2) == NEGATE_EXPR)
890 return build2_loc (loc, MINUS_EXPR, type,
891 fold_convert_loc (loc, type, t1),
892 fold_convert_loc (loc, type,
893 TREE_OPERAND (t2, 0)));
894 else if (integer_zerop (t2))
895 return fold_convert_loc (loc, type, t1);
897 else if (code == MINUS_EXPR)
899 if (integer_zerop (t2))
900 return fold_convert_loc (loc, type, t1);
903 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
904 fold_convert_loc (loc, type, t2));
907 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
908 fold_convert_loc (loc, type, t2));
911 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
912 for use in int_const_binop, size_binop and size_diffop. */
914 static bool
915 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
917 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
918 return false;
919 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
920 return false;
922 switch (code)
924 case LSHIFT_EXPR:
925 case RSHIFT_EXPR:
926 case LROTATE_EXPR:
927 case RROTATE_EXPR:
928 return true;
930 default:
931 break;
934 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
935 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
936 && TYPE_MODE (type1) == TYPE_MODE (type2);
940 /* Combine two integer constants ARG1 and ARG2 under operation CODE
941 to produce a new constant. Return NULL_TREE if we don't know how
942 to evaluate CODE at compile-time. */
944 static tree
945 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
946 int overflowable)
948 wide_int res;
949 tree t;
950 tree type = TREE_TYPE (arg1);
951 signop sign = TYPE_SIGN (type);
952 bool overflow = false;
954 wide_int arg2 = wide_int::from (parg2, TYPE_PRECISION (type),
955 TYPE_SIGN (TREE_TYPE (parg2)));
957 switch (code)
959 case BIT_IOR_EXPR:
960 res = wi::bit_or (arg1, arg2);
961 break;
963 case BIT_XOR_EXPR:
964 res = wi::bit_xor (arg1, arg2);
965 break;
967 case BIT_AND_EXPR:
968 res = wi::bit_and (arg1, arg2);
969 break;
971 case RSHIFT_EXPR:
972 case LSHIFT_EXPR:
973 if (wi::neg_p (arg2))
975 arg2 = -arg2;
976 if (code == RSHIFT_EXPR)
977 code = LSHIFT_EXPR;
978 else
979 code = RSHIFT_EXPR;
982 if (code == RSHIFT_EXPR)
983 /* It's unclear from the C standard whether shifts can overflow.
984 The following code ignores overflow; perhaps a C standard
985 interpretation ruling is needed. */
986 res = wi::rshift (arg1, arg2, sign);
987 else
988 res = wi::lshift (arg1, arg2);
989 break;
991 case RROTATE_EXPR:
992 case LROTATE_EXPR:
993 if (wi::neg_p (arg2))
995 arg2 = -arg2;
996 if (code == RROTATE_EXPR)
997 code = LROTATE_EXPR;
998 else
999 code = RROTATE_EXPR;
1002 if (code == RROTATE_EXPR)
1003 res = wi::rrotate (arg1, arg2);
1004 else
1005 res = wi::lrotate (arg1, arg2);
1006 break;
1008 case PLUS_EXPR:
1009 res = wi::add (arg1, arg2, sign, &overflow);
1010 break;
1012 case MINUS_EXPR:
1013 res = wi::sub (arg1, arg2, sign, &overflow);
1014 break;
1016 case MULT_EXPR:
1017 res = wi::mul (arg1, arg2, sign, &overflow);
1018 break;
1020 case MULT_HIGHPART_EXPR:
1021 res = wi::mul_high (arg1, arg2, sign);
1022 break;
1024 case TRUNC_DIV_EXPR:
1025 case EXACT_DIV_EXPR:
1026 if (arg2 == 0)
1027 return NULL_TREE;
1028 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1029 break;
1031 case FLOOR_DIV_EXPR:
1032 if (arg2 == 0)
1033 return NULL_TREE;
1034 res = wi::div_floor (arg1, arg2, sign, &overflow);
1035 break;
1037 case CEIL_DIV_EXPR:
1038 if (arg2 == 0)
1039 return NULL_TREE;
1040 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1041 break;
1043 case ROUND_DIV_EXPR:
1044 if (arg2 == 0)
1045 return NULL_TREE;
1046 res = wi::div_round (arg1, arg2, sign, &overflow);
1047 break;
1049 case TRUNC_MOD_EXPR:
1050 if (arg2 == 0)
1051 return NULL_TREE;
1052 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1053 break;
1055 case FLOOR_MOD_EXPR:
1056 if (arg2 == 0)
1057 return NULL_TREE;
1058 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1059 break;
1061 case CEIL_MOD_EXPR:
1062 if (arg2 == 0)
1063 return NULL_TREE;
1064 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1065 break;
1067 case ROUND_MOD_EXPR:
1068 if (arg2 == 0)
1069 return NULL_TREE;
1070 res = wi::mod_round (arg1, arg2, sign, &overflow);
1071 break;
1073 case MIN_EXPR:
1074 res = wi::min (arg1, arg2, sign);
1075 break;
1077 case MAX_EXPR:
1078 res = wi::max (arg1, arg2, sign);
1079 break;
1081 default:
1082 return NULL_TREE;
1085 t = force_fit_type (type, res, overflowable,
1086 (((sign == SIGNED || overflowable == -1)
1087 && overflow)
1088 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1090 return t;
1093 tree
1094 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1096 return int_const_binop_1 (code, arg1, arg2, 1);
1099 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1100 constant. We assume ARG1 and ARG2 have the same data type, or at least
1101 are the same kind of constant and the same machine mode. Return zero if
1102 combining the constants is not allowed in the current operating mode. */
1104 static tree
1105 const_binop (enum tree_code code, tree arg1, tree arg2)
1107 /* Sanity check for the recursive cases. */
1108 if (!arg1 || !arg2)
1109 return NULL_TREE;
1111 STRIP_NOPS (arg1);
1112 STRIP_NOPS (arg2);
1114 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1116 if (code == POINTER_PLUS_EXPR)
1117 return int_const_binop (PLUS_EXPR,
1118 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1120 return int_const_binop (code, arg1, arg2);
1123 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1125 machine_mode mode;
1126 REAL_VALUE_TYPE d1;
1127 REAL_VALUE_TYPE d2;
1128 REAL_VALUE_TYPE value;
1129 REAL_VALUE_TYPE result;
1130 bool inexact;
1131 tree t, type;
1133 /* The following codes are handled by real_arithmetic. */
1134 switch (code)
1136 case PLUS_EXPR:
1137 case MINUS_EXPR:
1138 case MULT_EXPR:
1139 case RDIV_EXPR:
1140 case MIN_EXPR:
1141 case MAX_EXPR:
1142 break;
1144 default:
1145 return NULL_TREE;
1148 d1 = TREE_REAL_CST (arg1);
1149 d2 = TREE_REAL_CST (arg2);
1151 type = TREE_TYPE (arg1);
1152 mode = TYPE_MODE (type);
1154 /* Don't perform operation if we honor signaling NaNs and
1155 either operand is a signaling NaN. */
1156 if (HONOR_SNANS (mode)
1157 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1158 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1159 return NULL_TREE;
1161 /* Don't perform operation if it would raise a division
1162 by zero exception. */
1163 if (code == RDIV_EXPR
1164 && real_equal (&d2, &dconst0)
1165 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1166 return NULL_TREE;
1168 /* If either operand is a NaN, just return it. Otherwise, set up
1169 for floating-point trap; we return an overflow. */
1170 if (REAL_VALUE_ISNAN (d1))
1172 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1173 is off. */
1174 d1.signalling = 0;
1175 t = build_real (type, d1);
1176 return t;
1178 else if (REAL_VALUE_ISNAN (d2))
1180 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1181 is off. */
1182 d2.signalling = 0;
1183 t = build_real (type, d2);
1184 return t;
1187 inexact = real_arithmetic (&value, code, &d1, &d2);
1188 real_convert (&result, mode, &value);
1190 /* Don't constant fold this floating point operation if
1191 the result has overflowed and flag_trapping_math. */
1192 if (flag_trapping_math
1193 && MODE_HAS_INFINITIES (mode)
1194 && REAL_VALUE_ISINF (result)
1195 && !REAL_VALUE_ISINF (d1)
1196 && !REAL_VALUE_ISINF (d2))
1197 return NULL_TREE;
1199 /* Don't constant fold this floating point operation if the
1200 result may dependent upon the run-time rounding mode and
1201 flag_rounding_math is set, or if GCC's software emulation
1202 is unable to accurately represent the result. */
1203 if ((flag_rounding_math
1204 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1205 && (inexact || !real_identical (&result, &value)))
1206 return NULL_TREE;
1208 t = build_real (type, result);
1210 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1211 return t;
1214 if (TREE_CODE (arg1) == FIXED_CST)
1216 FIXED_VALUE_TYPE f1;
1217 FIXED_VALUE_TYPE f2;
1218 FIXED_VALUE_TYPE result;
1219 tree t, type;
1220 int sat_p;
1221 bool overflow_p;
1223 /* The following codes are handled by fixed_arithmetic. */
1224 switch (code)
1226 case PLUS_EXPR:
1227 case MINUS_EXPR:
1228 case MULT_EXPR:
1229 case TRUNC_DIV_EXPR:
1230 if (TREE_CODE (arg2) != FIXED_CST)
1231 return NULL_TREE;
1232 f2 = TREE_FIXED_CST (arg2);
1233 break;
1235 case LSHIFT_EXPR:
1236 case RSHIFT_EXPR:
1238 if (TREE_CODE (arg2) != INTEGER_CST)
1239 return NULL_TREE;
1240 wide_int w2 = arg2;
1241 f2.data.high = w2.elt (1);
1242 f2.data.low = w2.elt (0);
1243 f2.mode = SImode;
1245 break;
1247 default:
1248 return NULL_TREE;
1251 f1 = TREE_FIXED_CST (arg1);
1252 type = TREE_TYPE (arg1);
1253 sat_p = TYPE_SATURATING (type);
1254 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1255 t = build_fixed (type, result);
1256 /* Propagate overflow flags. */
1257 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1258 TREE_OVERFLOW (t) = 1;
1259 return t;
1262 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1264 tree type = TREE_TYPE (arg1);
1265 tree r1 = TREE_REALPART (arg1);
1266 tree i1 = TREE_IMAGPART (arg1);
1267 tree r2 = TREE_REALPART (arg2);
1268 tree i2 = TREE_IMAGPART (arg2);
1269 tree real, imag;
1271 switch (code)
1273 case PLUS_EXPR:
1274 case MINUS_EXPR:
1275 real = const_binop (code, r1, r2);
1276 imag = const_binop (code, i1, i2);
1277 break;
1279 case MULT_EXPR:
1280 if (COMPLEX_FLOAT_TYPE_P (type))
1281 return do_mpc_arg2 (arg1, arg2, type,
1282 /* do_nonfinite= */ folding_initializer,
1283 mpc_mul);
1285 real = const_binop (MINUS_EXPR,
1286 const_binop (MULT_EXPR, r1, r2),
1287 const_binop (MULT_EXPR, i1, i2));
1288 imag = const_binop (PLUS_EXPR,
1289 const_binop (MULT_EXPR, r1, i2),
1290 const_binop (MULT_EXPR, i1, r2));
1291 break;
1293 case RDIV_EXPR:
1294 if (COMPLEX_FLOAT_TYPE_P (type))
1295 return do_mpc_arg2 (arg1, arg2, type,
1296 /* do_nonfinite= */ folding_initializer,
1297 mpc_div);
1298 /* Fallthru ... */
1299 case TRUNC_DIV_EXPR:
1300 case CEIL_DIV_EXPR:
1301 case FLOOR_DIV_EXPR:
1302 case ROUND_DIV_EXPR:
1303 if (flag_complex_method == 0)
1305 /* Keep this algorithm in sync with
1306 tree-complex.c:expand_complex_div_straight().
1308 Expand complex division to scalars, straightforward algorithm.
1309 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1310 t = br*br + bi*bi
1312 tree magsquared
1313 = const_binop (PLUS_EXPR,
1314 const_binop (MULT_EXPR, r2, r2),
1315 const_binop (MULT_EXPR, i2, i2));
1316 tree t1
1317 = const_binop (PLUS_EXPR,
1318 const_binop (MULT_EXPR, r1, r2),
1319 const_binop (MULT_EXPR, i1, i2));
1320 tree t2
1321 = const_binop (MINUS_EXPR,
1322 const_binop (MULT_EXPR, i1, r2),
1323 const_binop (MULT_EXPR, r1, i2));
1325 real = const_binop (code, t1, magsquared);
1326 imag = const_binop (code, t2, magsquared);
1328 else
1330 /* Keep this algorithm in sync with
1331 tree-complex.c:expand_complex_div_wide().
1333 Expand complex division to scalars, modified algorithm to minimize
1334 overflow with wide input ranges. */
1335 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1336 fold_abs_const (r2, TREE_TYPE (type)),
1337 fold_abs_const (i2, TREE_TYPE (type)));
1339 if (integer_nonzerop (compare))
1341 /* In the TRUE branch, we compute
1342 ratio = br/bi;
1343 div = (br * ratio) + bi;
1344 tr = (ar * ratio) + ai;
1345 ti = (ai * ratio) - ar;
1346 tr = tr / div;
1347 ti = ti / div; */
1348 tree ratio = const_binop (code, r2, i2);
1349 tree div = const_binop (PLUS_EXPR, i2,
1350 const_binop (MULT_EXPR, r2, ratio));
1351 real = const_binop (MULT_EXPR, r1, ratio);
1352 real = const_binop (PLUS_EXPR, real, i1);
1353 real = const_binop (code, real, div);
1355 imag = const_binop (MULT_EXPR, i1, ratio);
1356 imag = const_binop (MINUS_EXPR, imag, r1);
1357 imag = const_binop (code, imag, div);
1359 else
1361 /* In the FALSE branch, we compute
1362 ratio = d/c;
1363 divisor = (d * ratio) + c;
1364 tr = (b * ratio) + a;
1365 ti = b - (a * ratio);
1366 tr = tr / div;
1367 ti = ti / div; */
1368 tree ratio = const_binop (code, i2, r2);
1369 tree div = const_binop (PLUS_EXPR, r2,
1370 const_binop (MULT_EXPR, i2, ratio));
1372 real = const_binop (MULT_EXPR, i1, ratio);
1373 real = const_binop (PLUS_EXPR, real, r1);
1374 real = const_binop (code, real, div);
1376 imag = const_binop (MULT_EXPR, r1, ratio);
1377 imag = const_binop (MINUS_EXPR, i1, imag);
1378 imag = const_binop (code, imag, div);
1381 break;
1383 default:
1384 return NULL_TREE;
1387 if (real && imag)
1388 return build_complex (type, real, imag);
1391 if (TREE_CODE (arg1) == VECTOR_CST
1392 && TREE_CODE (arg2) == VECTOR_CST)
1394 tree type = TREE_TYPE (arg1);
1395 int count = TYPE_VECTOR_SUBPARTS (type), i;
1396 tree *elts = XALLOCAVEC (tree, count);
1398 for (i = 0; i < count; i++)
1400 tree elem1 = VECTOR_CST_ELT (arg1, i);
1401 tree elem2 = VECTOR_CST_ELT (arg2, i);
1403 elts[i] = const_binop (code, elem1, elem2);
1405 /* It is possible that const_binop cannot handle the given
1406 code and return NULL_TREE */
1407 if (elts[i] == NULL_TREE)
1408 return NULL_TREE;
1411 return build_vector (type, elts);
1414 /* Shifts allow a scalar offset for a vector. */
1415 if (TREE_CODE (arg1) == VECTOR_CST
1416 && TREE_CODE (arg2) == INTEGER_CST)
1418 tree type = TREE_TYPE (arg1);
1419 int count = TYPE_VECTOR_SUBPARTS (type), i;
1420 tree *elts = XALLOCAVEC (tree, count);
1422 for (i = 0; i < count; i++)
1424 tree elem1 = VECTOR_CST_ELT (arg1, i);
1426 elts[i] = const_binop (code, elem1, arg2);
1428 /* It is possible that const_binop cannot handle the given
1429 code and return NULL_TREE. */
1430 if (elts[i] == NULL_TREE)
1431 return NULL_TREE;
1434 return build_vector (type, elts);
1436 return NULL_TREE;
1439 /* Overload that adds a TYPE parameter to be able to dispatch
1440 to fold_relational_const. */
1442 tree
1443 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1445 if (TREE_CODE_CLASS (code) == tcc_comparison)
1446 return fold_relational_const (code, type, arg1, arg2);
1448 /* ??? Until we make the const_binop worker take the type of the
1449 result as argument put those cases that need it here. */
1450 switch (code)
1452 case COMPLEX_EXPR:
1453 if ((TREE_CODE (arg1) == REAL_CST
1454 && TREE_CODE (arg2) == REAL_CST)
1455 || (TREE_CODE (arg1) == INTEGER_CST
1456 && TREE_CODE (arg2) == INTEGER_CST))
1457 return build_complex (type, arg1, arg2);
1458 return NULL_TREE;
1460 case VEC_PACK_TRUNC_EXPR:
1461 case VEC_PACK_FIX_TRUNC_EXPR:
1463 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1464 tree *elts;
1466 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1467 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1468 if (TREE_CODE (arg1) != VECTOR_CST
1469 || TREE_CODE (arg2) != VECTOR_CST)
1470 return NULL_TREE;
1472 elts = XALLOCAVEC (tree, nelts);
1473 if (!vec_cst_ctor_to_array (arg1, elts)
1474 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1475 return NULL_TREE;
1477 for (i = 0; i < nelts; i++)
1479 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1480 ? NOP_EXPR : FIX_TRUNC_EXPR,
1481 TREE_TYPE (type), elts[i]);
1482 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1483 return NULL_TREE;
1486 return build_vector (type, elts);
1489 case VEC_WIDEN_MULT_LO_EXPR:
1490 case VEC_WIDEN_MULT_HI_EXPR:
1491 case VEC_WIDEN_MULT_EVEN_EXPR:
1492 case VEC_WIDEN_MULT_ODD_EXPR:
1494 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1495 unsigned int out, ofs, scale;
1496 tree *elts;
1498 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1499 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1500 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1501 return NULL_TREE;
1503 elts = XALLOCAVEC (tree, nelts * 4);
1504 if (!vec_cst_ctor_to_array (arg1, elts)
1505 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1506 return NULL_TREE;
1508 if (code == VEC_WIDEN_MULT_LO_EXPR)
1509 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1510 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1511 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1512 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1513 scale = 1, ofs = 0;
1514 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1515 scale = 1, ofs = 1;
1517 for (out = 0; out < nelts; out++)
1519 unsigned int in1 = (out << scale) + ofs;
1520 unsigned int in2 = in1 + nelts * 2;
1521 tree t1, t2;
1523 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1524 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1526 if (t1 == NULL_TREE || t2 == NULL_TREE)
1527 return NULL_TREE;
1528 elts[out] = const_binop (MULT_EXPR, t1, t2);
1529 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1530 return NULL_TREE;
1533 return build_vector (type, elts);
1536 default:;
1539 if (TREE_CODE_CLASS (code) != tcc_binary)
1540 return NULL_TREE;
1542 /* Make sure type and arg0 have the same saturating flag. */
1543 gcc_checking_assert (TYPE_SATURATING (type)
1544 == TYPE_SATURATING (TREE_TYPE (arg1)));
1546 return const_binop (code, arg1, arg2);
1549 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1550 Return zero if computing the constants is not possible. */
1552 tree
1553 const_unop (enum tree_code code, tree type, tree arg0)
1555 /* Don't perform the operation, other than NEGATE and ABS, if
1556 flag_signaling_nans is on and the operand is a signaling NaN. */
1557 if (TREE_CODE (arg0) == REAL_CST
1558 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1559 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1560 && code != NEGATE_EXPR
1561 && code != ABS_EXPR)
1562 return NULL_TREE;
1564 switch (code)
1566 CASE_CONVERT:
1567 case FLOAT_EXPR:
1568 case FIX_TRUNC_EXPR:
1569 case FIXED_CONVERT_EXPR:
1570 return fold_convert_const (code, type, arg0);
1572 case ADDR_SPACE_CONVERT_EXPR:
1573 /* If the source address is 0, and the source address space
1574 cannot have a valid object at 0, fold to dest type null. */
1575 if (integer_zerop (arg0)
1576 && !(targetm.addr_space.zero_address_valid
1577 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1578 return fold_convert_const (code, type, arg0);
1579 break;
1581 case VIEW_CONVERT_EXPR:
1582 return fold_view_convert_expr (type, arg0);
1584 case NEGATE_EXPR:
1586 /* Can't call fold_negate_const directly here as that doesn't
1587 handle all cases and we might not be able to negate some
1588 constants. */
1589 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1590 if (tem && CONSTANT_CLASS_P (tem))
1591 return tem;
1592 break;
1595 case ABS_EXPR:
1596 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1597 return fold_abs_const (arg0, type);
1598 break;
1600 case CONJ_EXPR:
1601 if (TREE_CODE (arg0) == COMPLEX_CST)
1603 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1604 TREE_TYPE (type));
1605 return build_complex (type, TREE_REALPART (arg0), ipart);
1607 break;
1609 case BIT_NOT_EXPR:
1610 if (TREE_CODE (arg0) == INTEGER_CST)
1611 return fold_not_const (arg0, type);
1612 /* Perform BIT_NOT_EXPR on each element individually. */
1613 else if (TREE_CODE (arg0) == VECTOR_CST)
1615 tree *elements;
1616 tree elem;
1617 unsigned count = VECTOR_CST_NELTS (arg0), i;
1619 elements = XALLOCAVEC (tree, count);
1620 for (i = 0; i < count; i++)
1622 elem = VECTOR_CST_ELT (arg0, i);
1623 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1624 if (elem == NULL_TREE)
1625 break;
1626 elements[i] = elem;
1628 if (i == count)
1629 return build_vector (type, elements);
1631 break;
1633 case TRUTH_NOT_EXPR:
1634 if (TREE_CODE (arg0) == INTEGER_CST)
1635 return constant_boolean_node (integer_zerop (arg0), type);
1636 break;
1638 case REALPART_EXPR:
1639 if (TREE_CODE (arg0) == COMPLEX_CST)
1640 return fold_convert (type, TREE_REALPART (arg0));
1641 break;
1643 case IMAGPART_EXPR:
1644 if (TREE_CODE (arg0) == COMPLEX_CST)
1645 return fold_convert (type, TREE_IMAGPART (arg0));
1646 break;
1648 case VEC_UNPACK_LO_EXPR:
1649 case VEC_UNPACK_HI_EXPR:
1650 case VEC_UNPACK_FLOAT_LO_EXPR:
1651 case VEC_UNPACK_FLOAT_HI_EXPR:
1653 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1654 tree *elts;
1655 enum tree_code subcode;
1657 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1658 if (TREE_CODE (arg0) != VECTOR_CST)
1659 return NULL_TREE;
1661 elts = XALLOCAVEC (tree, nelts * 2);
1662 if (!vec_cst_ctor_to_array (arg0, elts))
1663 return NULL_TREE;
1665 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1666 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1667 elts += nelts;
1669 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1670 subcode = NOP_EXPR;
1671 else
1672 subcode = FLOAT_EXPR;
1674 for (i = 0; i < nelts; i++)
1676 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1677 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1678 return NULL_TREE;
1681 return build_vector (type, elts);
1684 case REDUC_MIN_EXPR:
1685 case REDUC_MAX_EXPR:
1686 case REDUC_PLUS_EXPR:
1688 unsigned int nelts, i;
1689 tree *elts;
1690 enum tree_code subcode;
1692 if (TREE_CODE (arg0) != VECTOR_CST)
1693 return NULL_TREE;
1694 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1696 elts = XALLOCAVEC (tree, nelts);
1697 if (!vec_cst_ctor_to_array (arg0, elts))
1698 return NULL_TREE;
1700 switch (code)
1702 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1703 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1704 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1705 default: gcc_unreachable ();
1708 for (i = 1; i < nelts; i++)
1710 elts[0] = const_binop (subcode, elts[0], elts[i]);
1711 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1712 return NULL_TREE;
1715 return elts[0];
1718 default:
1719 break;
1722 return NULL_TREE;
1725 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1726 indicates which particular sizetype to create. */
1728 tree
1729 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1731 return build_int_cst (sizetype_tab[(int) kind], number);
1734 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1735 is a tree code. The type of the result is taken from the operands.
1736 Both must be equivalent integer types, ala int_binop_types_match_p.
1737 If the operands are constant, so is the result. */
1739 tree
1740 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1742 tree type = TREE_TYPE (arg0);
1744 if (arg0 == error_mark_node || arg1 == error_mark_node)
1745 return error_mark_node;
1747 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1748 TREE_TYPE (arg1)));
1750 /* Handle the special case of two integer constants faster. */
1751 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1753 /* And some specific cases even faster than that. */
1754 if (code == PLUS_EXPR)
1756 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1757 return arg1;
1758 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1759 return arg0;
1761 else if (code == MINUS_EXPR)
1763 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1764 return arg0;
1766 else if (code == MULT_EXPR)
1768 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1769 return arg1;
1772 /* Handle general case of two integer constants. For sizetype
1773 constant calculations we always want to know about overflow,
1774 even in the unsigned case. */
1775 return int_const_binop_1 (code, arg0, arg1, -1);
1778 return fold_build2_loc (loc, code, type, arg0, arg1);
1781 /* Given two values, either both of sizetype or both of bitsizetype,
1782 compute the difference between the two values. Return the value
1783 in signed type corresponding to the type of the operands. */
1785 tree
1786 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1788 tree type = TREE_TYPE (arg0);
1789 tree ctype;
1791 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1792 TREE_TYPE (arg1)));
1794 /* If the type is already signed, just do the simple thing. */
1795 if (!TYPE_UNSIGNED (type))
1796 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1798 if (type == sizetype)
1799 ctype = ssizetype;
1800 else if (type == bitsizetype)
1801 ctype = sbitsizetype;
1802 else
1803 ctype = signed_type_for (type);
1805 /* If either operand is not a constant, do the conversions to the signed
1806 type and subtract. The hardware will do the right thing with any
1807 overflow in the subtraction. */
1808 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1809 return size_binop_loc (loc, MINUS_EXPR,
1810 fold_convert_loc (loc, ctype, arg0),
1811 fold_convert_loc (loc, ctype, arg1));
1813 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1814 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1815 overflow) and negate (which can't either). Special-case a result
1816 of zero while we're here. */
1817 if (tree_int_cst_equal (arg0, arg1))
1818 return build_int_cst (ctype, 0);
1819 else if (tree_int_cst_lt (arg1, arg0))
1820 return fold_convert_loc (loc, ctype,
1821 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1822 else
1823 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1824 fold_convert_loc (loc, ctype,
1825 size_binop_loc (loc,
1826 MINUS_EXPR,
1827 arg1, arg0)));
1830 /* A subroutine of fold_convert_const handling conversions of an
1831 INTEGER_CST to another integer type. */
1833 static tree
1834 fold_convert_const_int_from_int (tree type, const_tree arg1)
1836 /* Given an integer constant, make new constant with new type,
1837 appropriately sign-extended or truncated. Use widest_int
1838 so that any extension is done according ARG1's type. */
1839 return force_fit_type (type, wi::to_widest (arg1),
1840 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1841 TREE_OVERFLOW (arg1));
1844 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1845 to an integer type. */
1847 static tree
1848 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1850 bool overflow = false;
1851 tree t;
1853 /* The following code implements the floating point to integer
1854 conversion rules required by the Java Language Specification,
1855 that IEEE NaNs are mapped to zero and values that overflow
1856 the target precision saturate, i.e. values greater than
1857 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1858 are mapped to INT_MIN. These semantics are allowed by the
1859 C and C++ standards that simply state that the behavior of
1860 FP-to-integer conversion is unspecified upon overflow. */
1862 wide_int val;
1863 REAL_VALUE_TYPE r;
1864 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1866 switch (code)
1868 case FIX_TRUNC_EXPR:
1869 real_trunc (&r, VOIDmode, &x);
1870 break;
1872 default:
1873 gcc_unreachable ();
1876 /* If R is NaN, return zero and show we have an overflow. */
1877 if (REAL_VALUE_ISNAN (r))
1879 overflow = true;
1880 val = wi::zero (TYPE_PRECISION (type));
1883 /* See if R is less than the lower bound or greater than the
1884 upper bound. */
1886 if (! overflow)
1888 tree lt = TYPE_MIN_VALUE (type);
1889 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1890 if (real_less (&r, &l))
1892 overflow = true;
1893 val = lt;
1897 if (! overflow)
1899 tree ut = TYPE_MAX_VALUE (type);
1900 if (ut)
1902 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1903 if (real_less (&u, &r))
1905 overflow = true;
1906 val = ut;
1911 if (! overflow)
1912 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1914 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1915 return t;
1918 /* A subroutine of fold_convert_const handling conversions of a
1919 FIXED_CST to an integer type. */
1921 static tree
1922 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1924 tree t;
1925 double_int temp, temp_trunc;
1926 unsigned int mode;
1928 /* Right shift FIXED_CST to temp by fbit. */
1929 temp = TREE_FIXED_CST (arg1).data;
1930 mode = TREE_FIXED_CST (arg1).mode;
1931 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1933 temp = temp.rshift (GET_MODE_FBIT (mode),
1934 HOST_BITS_PER_DOUBLE_INT,
1935 SIGNED_FIXED_POINT_MODE_P (mode));
1937 /* Left shift temp to temp_trunc by fbit. */
1938 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1939 HOST_BITS_PER_DOUBLE_INT,
1940 SIGNED_FIXED_POINT_MODE_P (mode));
1942 else
1944 temp = double_int_zero;
1945 temp_trunc = double_int_zero;
1948 /* If FIXED_CST is negative, we need to round the value toward 0.
1949 By checking if the fractional bits are not zero to add 1 to temp. */
1950 if (SIGNED_FIXED_POINT_MODE_P (mode)
1951 && temp_trunc.is_negative ()
1952 && TREE_FIXED_CST (arg1).data != temp_trunc)
1953 temp += double_int_one;
1955 /* Given a fixed-point constant, make new constant with new type,
1956 appropriately sign-extended or truncated. */
1957 t = force_fit_type (type, temp, -1,
1958 (temp.is_negative ()
1959 && (TYPE_UNSIGNED (type)
1960 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1961 | TREE_OVERFLOW (arg1));
1963 return t;
1966 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1967 to another floating point type. */
1969 static tree
1970 fold_convert_const_real_from_real (tree type, const_tree arg1)
1972 REAL_VALUE_TYPE value;
1973 tree t;
1975 /* Don't perform the operation if flag_signaling_nans is on
1976 and the operand is a signaling NaN. */
1977 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
1978 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
1979 return NULL_TREE;
1981 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1982 t = build_real (type, value);
1984 /* If converting an infinity or NAN to a representation that doesn't
1985 have one, set the overflow bit so that we can produce some kind of
1986 error message at the appropriate point if necessary. It's not the
1987 most user-friendly message, but it's better than nothing. */
1988 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1989 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1990 TREE_OVERFLOW (t) = 1;
1991 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1992 && !MODE_HAS_NANS (TYPE_MODE (type)))
1993 TREE_OVERFLOW (t) = 1;
1994 /* Regular overflow, conversion produced an infinity in a mode that
1995 can't represent them. */
1996 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1997 && REAL_VALUE_ISINF (value)
1998 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1999 TREE_OVERFLOW (t) = 1;
2000 else
2001 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2002 return t;
2005 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2006 to a floating point type. */
2008 static tree
2009 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2011 REAL_VALUE_TYPE value;
2012 tree t;
2014 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2015 t = build_real (type, value);
2017 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2018 return t;
2021 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2022 to another fixed-point type. */
2024 static tree
2025 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2027 FIXED_VALUE_TYPE value;
2028 tree t;
2029 bool overflow_p;
2031 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2032 TYPE_SATURATING (type));
2033 t = build_fixed (type, value);
2035 /* Propagate overflow flags. */
2036 if (overflow_p | TREE_OVERFLOW (arg1))
2037 TREE_OVERFLOW (t) = 1;
2038 return t;
2041 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2042 to a fixed-point type. */
2044 static tree
2045 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2047 FIXED_VALUE_TYPE value;
2048 tree t;
2049 bool overflow_p;
2050 double_int di;
2052 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2054 di.low = TREE_INT_CST_ELT (arg1, 0);
2055 if (TREE_INT_CST_NUNITS (arg1) == 1)
2056 di.high = (HOST_WIDE_INT) di.low < 0 ? (HOST_WIDE_INT) -1 : 0;
2057 else
2058 di.high = TREE_INT_CST_ELT (arg1, 1);
2060 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2061 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2062 TYPE_SATURATING (type));
2063 t = build_fixed (type, value);
2065 /* Propagate overflow flags. */
2066 if (overflow_p | TREE_OVERFLOW (arg1))
2067 TREE_OVERFLOW (t) = 1;
2068 return t;
2071 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2072 to a fixed-point type. */
2074 static tree
2075 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2077 FIXED_VALUE_TYPE value;
2078 tree t;
2079 bool overflow_p;
2081 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2082 &TREE_REAL_CST (arg1),
2083 TYPE_SATURATING (type));
2084 t = build_fixed (type, value);
2086 /* Propagate overflow flags. */
2087 if (overflow_p | TREE_OVERFLOW (arg1))
2088 TREE_OVERFLOW (t) = 1;
2089 return t;
2092 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2093 type TYPE. If no simplification can be done return NULL_TREE. */
2095 static tree
2096 fold_convert_const (enum tree_code code, tree type, tree arg1)
2098 if (TREE_TYPE (arg1) == type)
2099 return arg1;
2101 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2102 || TREE_CODE (type) == OFFSET_TYPE)
2104 if (TREE_CODE (arg1) == INTEGER_CST)
2105 return fold_convert_const_int_from_int (type, arg1);
2106 else if (TREE_CODE (arg1) == REAL_CST)
2107 return fold_convert_const_int_from_real (code, type, arg1);
2108 else if (TREE_CODE (arg1) == FIXED_CST)
2109 return fold_convert_const_int_from_fixed (type, arg1);
2111 else if (TREE_CODE (type) == REAL_TYPE)
2113 if (TREE_CODE (arg1) == INTEGER_CST)
2114 return build_real_from_int_cst (type, arg1);
2115 else if (TREE_CODE (arg1) == REAL_CST)
2116 return fold_convert_const_real_from_real (type, arg1);
2117 else if (TREE_CODE (arg1) == FIXED_CST)
2118 return fold_convert_const_real_from_fixed (type, arg1);
2120 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2122 if (TREE_CODE (arg1) == FIXED_CST)
2123 return fold_convert_const_fixed_from_fixed (type, arg1);
2124 else if (TREE_CODE (arg1) == INTEGER_CST)
2125 return fold_convert_const_fixed_from_int (type, arg1);
2126 else if (TREE_CODE (arg1) == REAL_CST)
2127 return fold_convert_const_fixed_from_real (type, arg1);
2129 else if (TREE_CODE (type) == VECTOR_TYPE)
2131 if (TREE_CODE (arg1) == VECTOR_CST
2132 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2134 int len = TYPE_VECTOR_SUBPARTS (type);
2135 tree elttype = TREE_TYPE (type);
2136 tree *v = XALLOCAVEC (tree, len);
2137 for (int i = 0; i < len; ++i)
2139 tree elt = VECTOR_CST_ELT (arg1, i);
2140 tree cvt = fold_convert_const (code, elttype, elt);
2141 if (cvt == NULL_TREE)
2142 return NULL_TREE;
2143 v[i] = cvt;
2145 return build_vector (type, v);
2148 return NULL_TREE;
2151 /* Construct a vector of zero elements of vector type TYPE. */
2153 static tree
2154 build_zero_vector (tree type)
2156 tree t;
2158 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2159 return build_vector_from_val (type, t);
2162 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2164 bool
2165 fold_convertible_p (const_tree type, const_tree arg)
2167 tree orig = TREE_TYPE (arg);
2169 if (type == orig)
2170 return true;
2172 if (TREE_CODE (arg) == ERROR_MARK
2173 || TREE_CODE (type) == ERROR_MARK
2174 || TREE_CODE (orig) == ERROR_MARK)
2175 return false;
2177 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2178 return true;
2180 switch (TREE_CODE (type))
2182 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2183 case POINTER_TYPE: case REFERENCE_TYPE:
2184 case OFFSET_TYPE:
2185 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2186 || TREE_CODE (orig) == OFFSET_TYPE);
2188 case REAL_TYPE:
2189 case FIXED_POINT_TYPE:
2190 case COMPLEX_TYPE:
2191 case VECTOR_TYPE:
2192 case VOID_TYPE:
2193 return TREE_CODE (type) == TREE_CODE (orig);
2195 default:
2196 return false;
2200 /* Convert expression ARG to type TYPE. Used by the middle-end for
2201 simple conversions in preference to calling the front-end's convert. */
2203 tree
2204 fold_convert_loc (location_t loc, tree type, tree arg)
2206 tree orig = TREE_TYPE (arg);
2207 tree tem;
2209 if (type == orig)
2210 return arg;
2212 if (TREE_CODE (arg) == ERROR_MARK
2213 || TREE_CODE (type) == ERROR_MARK
2214 || TREE_CODE (orig) == ERROR_MARK)
2215 return error_mark_node;
2217 switch (TREE_CODE (type))
2219 case POINTER_TYPE:
2220 case REFERENCE_TYPE:
2221 /* Handle conversions between pointers to different address spaces. */
2222 if (POINTER_TYPE_P (orig)
2223 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2224 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2225 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2226 /* fall through */
2228 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2229 case OFFSET_TYPE:
2230 if (TREE_CODE (arg) == INTEGER_CST)
2232 tem = fold_convert_const (NOP_EXPR, type, arg);
2233 if (tem != NULL_TREE)
2234 return tem;
2236 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2237 || TREE_CODE (orig) == OFFSET_TYPE)
2238 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2239 if (TREE_CODE (orig) == COMPLEX_TYPE)
2240 return fold_convert_loc (loc, type,
2241 fold_build1_loc (loc, REALPART_EXPR,
2242 TREE_TYPE (orig), arg));
2243 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2244 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2245 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2247 case REAL_TYPE:
2248 if (TREE_CODE (arg) == INTEGER_CST)
2250 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2251 if (tem != NULL_TREE)
2252 return tem;
2254 else if (TREE_CODE (arg) == REAL_CST)
2256 tem = fold_convert_const (NOP_EXPR, type, arg);
2257 if (tem != NULL_TREE)
2258 return tem;
2260 else if (TREE_CODE (arg) == FIXED_CST)
2262 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2263 if (tem != NULL_TREE)
2264 return tem;
2267 switch (TREE_CODE (orig))
2269 case INTEGER_TYPE:
2270 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2271 case POINTER_TYPE: case REFERENCE_TYPE:
2272 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2274 case REAL_TYPE:
2275 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2277 case FIXED_POINT_TYPE:
2278 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2280 case COMPLEX_TYPE:
2281 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2282 return fold_convert_loc (loc, type, tem);
2284 default:
2285 gcc_unreachable ();
2288 case FIXED_POINT_TYPE:
2289 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2290 || TREE_CODE (arg) == REAL_CST)
2292 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2293 if (tem != NULL_TREE)
2294 goto fold_convert_exit;
2297 switch (TREE_CODE (orig))
2299 case FIXED_POINT_TYPE:
2300 case INTEGER_TYPE:
2301 case ENUMERAL_TYPE:
2302 case BOOLEAN_TYPE:
2303 case REAL_TYPE:
2304 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2306 case COMPLEX_TYPE:
2307 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2308 return fold_convert_loc (loc, type, tem);
2310 default:
2311 gcc_unreachable ();
2314 case COMPLEX_TYPE:
2315 switch (TREE_CODE (orig))
2317 case INTEGER_TYPE:
2318 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2319 case POINTER_TYPE: case REFERENCE_TYPE:
2320 case REAL_TYPE:
2321 case FIXED_POINT_TYPE:
2322 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2323 fold_convert_loc (loc, TREE_TYPE (type), arg),
2324 fold_convert_loc (loc, TREE_TYPE (type),
2325 integer_zero_node));
2326 case COMPLEX_TYPE:
2328 tree rpart, ipart;
2330 if (TREE_CODE (arg) == COMPLEX_EXPR)
2332 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2333 TREE_OPERAND (arg, 0));
2334 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2335 TREE_OPERAND (arg, 1));
2336 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2339 arg = save_expr (arg);
2340 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2341 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2342 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2343 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2344 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2347 default:
2348 gcc_unreachable ();
2351 case VECTOR_TYPE:
2352 if (integer_zerop (arg))
2353 return build_zero_vector (type);
2354 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2355 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2356 || TREE_CODE (orig) == VECTOR_TYPE);
2357 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2359 case VOID_TYPE:
2360 tem = fold_ignored_result (arg);
2361 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2363 default:
2364 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2365 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2366 gcc_unreachable ();
2368 fold_convert_exit:
2369 protected_set_expr_location_unshare (tem, loc);
2370 return tem;
2373 /* Return false if expr can be assumed not to be an lvalue, true
2374 otherwise. */
2376 static bool
2377 maybe_lvalue_p (const_tree x)
2379 /* We only need to wrap lvalue tree codes. */
2380 switch (TREE_CODE (x))
2382 case VAR_DECL:
2383 case PARM_DECL:
2384 case RESULT_DECL:
2385 case LABEL_DECL:
2386 case FUNCTION_DECL:
2387 case SSA_NAME:
2389 case COMPONENT_REF:
2390 case MEM_REF:
2391 case INDIRECT_REF:
2392 case ARRAY_REF:
2393 case ARRAY_RANGE_REF:
2394 case BIT_FIELD_REF:
2395 case OBJ_TYPE_REF:
2397 case REALPART_EXPR:
2398 case IMAGPART_EXPR:
2399 case PREINCREMENT_EXPR:
2400 case PREDECREMENT_EXPR:
2401 case SAVE_EXPR:
2402 case TRY_CATCH_EXPR:
2403 case WITH_CLEANUP_EXPR:
2404 case COMPOUND_EXPR:
2405 case MODIFY_EXPR:
2406 case TARGET_EXPR:
2407 case COND_EXPR:
2408 case BIND_EXPR:
2409 break;
2411 default:
2412 /* Assume the worst for front-end tree codes. */
2413 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2414 break;
2415 return false;
2418 return true;
2421 /* Return an expr equal to X but certainly not valid as an lvalue. */
2423 tree
2424 non_lvalue_loc (location_t loc, tree x)
2426 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2427 us. */
2428 if (in_gimple_form)
2429 return x;
2431 if (! maybe_lvalue_p (x))
2432 return x;
2433 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2436 /* When pedantic, return an expr equal to X but certainly not valid as a
2437 pedantic lvalue. Otherwise, return X. */
2439 static tree
2440 pedantic_non_lvalue_loc (location_t loc, tree x)
2442 return protected_set_expr_location_unshare (x, loc);
2445 /* Given a tree comparison code, return the code that is the logical inverse.
2446 It is generally not safe to do this for floating-point comparisons, except
2447 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2448 ERROR_MARK in this case. */
2450 enum tree_code
2451 invert_tree_comparison (enum tree_code code, bool honor_nans)
2453 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2454 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2455 return ERROR_MARK;
2457 switch (code)
2459 case EQ_EXPR:
2460 return NE_EXPR;
2461 case NE_EXPR:
2462 return EQ_EXPR;
2463 case GT_EXPR:
2464 return honor_nans ? UNLE_EXPR : LE_EXPR;
2465 case GE_EXPR:
2466 return honor_nans ? UNLT_EXPR : LT_EXPR;
2467 case LT_EXPR:
2468 return honor_nans ? UNGE_EXPR : GE_EXPR;
2469 case LE_EXPR:
2470 return honor_nans ? UNGT_EXPR : GT_EXPR;
2471 case LTGT_EXPR:
2472 return UNEQ_EXPR;
2473 case UNEQ_EXPR:
2474 return LTGT_EXPR;
2475 case UNGT_EXPR:
2476 return LE_EXPR;
2477 case UNGE_EXPR:
2478 return LT_EXPR;
2479 case UNLT_EXPR:
2480 return GE_EXPR;
2481 case UNLE_EXPR:
2482 return GT_EXPR;
2483 case ORDERED_EXPR:
2484 return UNORDERED_EXPR;
2485 case UNORDERED_EXPR:
2486 return ORDERED_EXPR;
2487 default:
2488 gcc_unreachable ();
2492 /* Similar, but return the comparison that results if the operands are
2493 swapped. This is safe for floating-point. */
2495 enum tree_code
2496 swap_tree_comparison (enum tree_code code)
2498 switch (code)
2500 case EQ_EXPR:
2501 case NE_EXPR:
2502 case ORDERED_EXPR:
2503 case UNORDERED_EXPR:
2504 case LTGT_EXPR:
2505 case UNEQ_EXPR:
2506 return code;
2507 case GT_EXPR:
2508 return LT_EXPR;
2509 case GE_EXPR:
2510 return LE_EXPR;
2511 case LT_EXPR:
2512 return GT_EXPR;
2513 case LE_EXPR:
2514 return GE_EXPR;
2515 case UNGT_EXPR:
2516 return UNLT_EXPR;
2517 case UNGE_EXPR:
2518 return UNLE_EXPR;
2519 case UNLT_EXPR:
2520 return UNGT_EXPR;
2521 case UNLE_EXPR:
2522 return UNGE_EXPR;
2523 default:
2524 gcc_unreachable ();
2529 /* Convert a comparison tree code from an enum tree_code representation
2530 into a compcode bit-based encoding. This function is the inverse of
2531 compcode_to_comparison. */
2533 static enum comparison_code
2534 comparison_to_compcode (enum tree_code code)
2536 switch (code)
2538 case LT_EXPR:
2539 return COMPCODE_LT;
2540 case EQ_EXPR:
2541 return COMPCODE_EQ;
2542 case LE_EXPR:
2543 return COMPCODE_LE;
2544 case GT_EXPR:
2545 return COMPCODE_GT;
2546 case NE_EXPR:
2547 return COMPCODE_NE;
2548 case GE_EXPR:
2549 return COMPCODE_GE;
2550 case ORDERED_EXPR:
2551 return COMPCODE_ORD;
2552 case UNORDERED_EXPR:
2553 return COMPCODE_UNORD;
2554 case UNLT_EXPR:
2555 return COMPCODE_UNLT;
2556 case UNEQ_EXPR:
2557 return COMPCODE_UNEQ;
2558 case UNLE_EXPR:
2559 return COMPCODE_UNLE;
2560 case UNGT_EXPR:
2561 return COMPCODE_UNGT;
2562 case LTGT_EXPR:
2563 return COMPCODE_LTGT;
2564 case UNGE_EXPR:
2565 return COMPCODE_UNGE;
2566 default:
2567 gcc_unreachable ();
2571 /* Convert a compcode bit-based encoding of a comparison operator back
2572 to GCC's enum tree_code representation. This function is the
2573 inverse of comparison_to_compcode. */
2575 static enum tree_code
2576 compcode_to_comparison (enum comparison_code code)
2578 switch (code)
2580 case COMPCODE_LT:
2581 return LT_EXPR;
2582 case COMPCODE_EQ:
2583 return EQ_EXPR;
2584 case COMPCODE_LE:
2585 return LE_EXPR;
2586 case COMPCODE_GT:
2587 return GT_EXPR;
2588 case COMPCODE_NE:
2589 return NE_EXPR;
2590 case COMPCODE_GE:
2591 return GE_EXPR;
2592 case COMPCODE_ORD:
2593 return ORDERED_EXPR;
2594 case COMPCODE_UNORD:
2595 return UNORDERED_EXPR;
2596 case COMPCODE_UNLT:
2597 return UNLT_EXPR;
2598 case COMPCODE_UNEQ:
2599 return UNEQ_EXPR;
2600 case COMPCODE_UNLE:
2601 return UNLE_EXPR;
2602 case COMPCODE_UNGT:
2603 return UNGT_EXPR;
2604 case COMPCODE_LTGT:
2605 return LTGT_EXPR;
2606 case COMPCODE_UNGE:
2607 return UNGE_EXPR;
2608 default:
2609 gcc_unreachable ();
2613 /* Return a tree for the comparison which is the combination of
2614 doing the AND or OR (depending on CODE) of the two operations LCODE
2615 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2616 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2617 if this makes the transformation invalid. */
2619 tree
2620 combine_comparisons (location_t loc,
2621 enum tree_code code, enum tree_code lcode,
2622 enum tree_code rcode, tree truth_type,
2623 tree ll_arg, tree lr_arg)
2625 bool honor_nans = HONOR_NANS (ll_arg);
2626 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2627 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2628 int compcode;
2630 switch (code)
2632 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2633 compcode = lcompcode & rcompcode;
2634 break;
2636 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2637 compcode = lcompcode | rcompcode;
2638 break;
2640 default:
2641 return NULL_TREE;
2644 if (!honor_nans)
2646 /* Eliminate unordered comparisons, as well as LTGT and ORD
2647 which are not used unless the mode has NaNs. */
2648 compcode &= ~COMPCODE_UNORD;
2649 if (compcode == COMPCODE_LTGT)
2650 compcode = COMPCODE_NE;
2651 else if (compcode == COMPCODE_ORD)
2652 compcode = COMPCODE_TRUE;
2654 else if (flag_trapping_math)
2656 /* Check that the original operation and the optimized ones will trap
2657 under the same condition. */
2658 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2659 && (lcompcode != COMPCODE_EQ)
2660 && (lcompcode != COMPCODE_ORD);
2661 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2662 && (rcompcode != COMPCODE_EQ)
2663 && (rcompcode != COMPCODE_ORD);
2664 bool trap = (compcode & COMPCODE_UNORD) == 0
2665 && (compcode != COMPCODE_EQ)
2666 && (compcode != COMPCODE_ORD);
2668 /* In a short-circuited boolean expression the LHS might be
2669 such that the RHS, if evaluated, will never trap. For
2670 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2671 if neither x nor y is NaN. (This is a mixed blessing: for
2672 example, the expression above will never trap, hence
2673 optimizing it to x < y would be invalid). */
2674 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2675 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2676 rtrap = false;
2678 /* If the comparison was short-circuited, and only the RHS
2679 trapped, we may now generate a spurious trap. */
2680 if (rtrap && !ltrap
2681 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2682 return NULL_TREE;
2684 /* If we changed the conditions that cause a trap, we lose. */
2685 if ((ltrap || rtrap) != trap)
2686 return NULL_TREE;
2689 if (compcode == COMPCODE_TRUE)
2690 return constant_boolean_node (true, truth_type);
2691 else if (compcode == COMPCODE_FALSE)
2692 return constant_boolean_node (false, truth_type);
2693 else
2695 enum tree_code tcode;
2697 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2698 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2702 /* Return nonzero if two operands (typically of the same tree node)
2703 are necessarily equal. FLAGS modifies behavior as follows:
2705 If OEP_ONLY_CONST is set, only return nonzero for constants.
2706 This function tests whether the operands are indistinguishable;
2707 it does not test whether they are equal using C's == operation.
2708 The distinction is important for IEEE floating point, because
2709 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2710 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2712 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2713 even though it may hold multiple values during a function.
2714 This is because a GCC tree node guarantees that nothing else is
2715 executed between the evaluation of its "operands" (which may often
2716 be evaluated in arbitrary order). Hence if the operands themselves
2717 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2718 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2719 unset means assuming isochronic (or instantaneous) tree equivalence.
2720 Unless comparing arbitrary expression trees, such as from different
2721 statements, this flag can usually be left unset.
2723 If OEP_PURE_SAME is set, then pure functions with identical arguments
2724 are considered the same. It is used when the caller has other ways
2725 to ensure that global memory is unchanged in between.
2727 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2728 not values of expressions.
2730 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2731 any operand with side effect. This is unnecesarily conservative in the
2732 case we know that arg0 and arg1 are in disjoint code paths (such as in
2733 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2734 addresses with TREE_CONSTANT flag set so we know that &var == &var
2735 even if var is volatile. */
2738 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2740 /* If either is ERROR_MARK, they aren't equal. */
2741 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2742 || TREE_TYPE (arg0) == error_mark_node
2743 || TREE_TYPE (arg1) == error_mark_node)
2744 return 0;
2746 /* Similar, if either does not have a type (like a released SSA name),
2747 they aren't equal. */
2748 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2749 return 0;
2751 /* We cannot consider pointers to different address space equal. */
2752 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2753 && POINTER_TYPE_P (TREE_TYPE (arg1))
2754 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2755 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2756 return 0;
2758 /* Check equality of integer constants before bailing out due to
2759 precision differences. */
2760 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2762 /* Address of INTEGER_CST is not defined; check that we did not forget
2763 to drop the OEP_ADDRESS_OF flags. */
2764 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2765 return tree_int_cst_equal (arg0, arg1);
2768 if (!(flags & OEP_ADDRESS_OF))
2770 /* If both types don't have the same signedness, then we can't consider
2771 them equal. We must check this before the STRIP_NOPS calls
2772 because they may change the signedness of the arguments. As pointers
2773 strictly don't have a signedness, require either two pointers or
2774 two non-pointers as well. */
2775 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2776 || POINTER_TYPE_P (TREE_TYPE (arg0))
2777 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2778 return 0;
2780 /* If both types don't have the same precision, then it is not safe
2781 to strip NOPs. */
2782 if (element_precision (TREE_TYPE (arg0))
2783 != element_precision (TREE_TYPE (arg1)))
2784 return 0;
2786 STRIP_NOPS (arg0);
2787 STRIP_NOPS (arg1);
2789 #if 0
2790 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2791 sanity check once the issue is solved. */
2792 else
2793 /* Addresses of conversions and SSA_NAMEs (and many other things)
2794 are not defined. Check that we did not forget to drop the
2795 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2796 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2797 && TREE_CODE (arg0) != SSA_NAME);
2798 #endif
2800 /* In case both args are comparisons but with different comparison
2801 code, try to swap the comparison operands of one arg to produce
2802 a match and compare that variant. */
2803 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2804 && COMPARISON_CLASS_P (arg0)
2805 && COMPARISON_CLASS_P (arg1))
2807 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2809 if (TREE_CODE (arg0) == swap_code)
2810 return operand_equal_p (TREE_OPERAND (arg0, 0),
2811 TREE_OPERAND (arg1, 1), flags)
2812 && operand_equal_p (TREE_OPERAND (arg0, 1),
2813 TREE_OPERAND (arg1, 0), flags);
2816 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2818 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2819 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2821 else if (flags & OEP_ADDRESS_OF)
2823 /* If we are interested in comparing addresses ignore
2824 MEM_REF wrappings of the base that can appear just for
2825 TBAA reasons. */
2826 if (TREE_CODE (arg0) == MEM_REF
2827 && DECL_P (arg1)
2828 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2829 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2830 && integer_zerop (TREE_OPERAND (arg0, 1)))
2831 return 1;
2832 else if (TREE_CODE (arg1) == MEM_REF
2833 && DECL_P (arg0)
2834 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2835 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2836 && integer_zerop (TREE_OPERAND (arg1, 1)))
2837 return 1;
2838 return 0;
2840 else
2841 return 0;
2844 /* When not checking adddresses, this is needed for conversions and for
2845 COMPONENT_REF. Might as well play it safe and always test this. */
2846 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2847 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2848 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
2849 && !(flags & OEP_ADDRESS_OF)))
2850 return 0;
2852 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2853 We don't care about side effects in that case because the SAVE_EXPR
2854 takes care of that for us. In all other cases, two expressions are
2855 equal if they have no side effects. If we have two identical
2856 expressions with side effects that should be treated the same due
2857 to the only side effects being identical SAVE_EXPR's, that will
2858 be detected in the recursive calls below.
2859 If we are taking an invariant address of two identical objects
2860 they are necessarily equal as well. */
2861 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2862 && (TREE_CODE (arg0) == SAVE_EXPR
2863 || (flags & OEP_MATCH_SIDE_EFFECTS)
2864 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2865 return 1;
2867 /* Next handle constant cases, those for which we can return 1 even
2868 if ONLY_CONST is set. */
2869 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2870 switch (TREE_CODE (arg0))
2872 case INTEGER_CST:
2873 return tree_int_cst_equal (arg0, arg1);
2875 case FIXED_CST:
2876 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2877 TREE_FIXED_CST (arg1));
2879 case REAL_CST:
2880 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
2881 return 1;
2884 if (!HONOR_SIGNED_ZEROS (arg0))
2886 /* If we do not distinguish between signed and unsigned zero,
2887 consider them equal. */
2888 if (real_zerop (arg0) && real_zerop (arg1))
2889 return 1;
2891 return 0;
2893 case VECTOR_CST:
2895 unsigned i;
2897 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2898 return 0;
2900 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2902 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2903 VECTOR_CST_ELT (arg1, i), flags))
2904 return 0;
2906 return 1;
2909 case COMPLEX_CST:
2910 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2911 flags)
2912 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2913 flags));
2915 case STRING_CST:
2916 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2917 && ! memcmp (TREE_STRING_POINTER (arg0),
2918 TREE_STRING_POINTER (arg1),
2919 TREE_STRING_LENGTH (arg0)));
2921 case ADDR_EXPR:
2922 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2923 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2924 flags | OEP_ADDRESS_OF
2925 | OEP_MATCH_SIDE_EFFECTS);
2926 case CONSTRUCTOR:
2927 /* In GIMPLE empty constructors are allowed in initializers of
2928 aggregates. */
2929 return (!vec_safe_length (CONSTRUCTOR_ELTS (arg0))
2930 && !vec_safe_length (CONSTRUCTOR_ELTS (arg1)));
2931 default:
2932 break;
2935 if (flags & OEP_ONLY_CONST)
2936 return 0;
2938 /* Define macros to test an operand from arg0 and arg1 for equality and a
2939 variant that allows null and views null as being different from any
2940 non-null value. In the latter case, if either is null, the both
2941 must be; otherwise, do the normal comparison. */
2942 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2943 TREE_OPERAND (arg1, N), flags)
2945 #define OP_SAME_WITH_NULL(N) \
2946 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2947 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2949 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2951 case tcc_unary:
2952 /* Two conversions are equal only if signedness and modes match. */
2953 switch (TREE_CODE (arg0))
2955 CASE_CONVERT:
2956 case FIX_TRUNC_EXPR:
2957 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2958 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2959 return 0;
2960 break;
2961 default:
2962 break;
2965 return OP_SAME (0);
2968 case tcc_comparison:
2969 case tcc_binary:
2970 if (OP_SAME (0) && OP_SAME (1))
2971 return 1;
2973 /* For commutative ops, allow the other order. */
2974 return (commutative_tree_code (TREE_CODE (arg0))
2975 && operand_equal_p (TREE_OPERAND (arg0, 0),
2976 TREE_OPERAND (arg1, 1), flags)
2977 && operand_equal_p (TREE_OPERAND (arg0, 1),
2978 TREE_OPERAND (arg1, 0), flags));
2980 case tcc_reference:
2981 /* If either of the pointer (or reference) expressions we are
2982 dereferencing contain a side effect, these cannot be equal,
2983 but their addresses can be. */
2984 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
2985 && (TREE_SIDE_EFFECTS (arg0)
2986 || TREE_SIDE_EFFECTS (arg1)))
2987 return 0;
2989 switch (TREE_CODE (arg0))
2991 case INDIRECT_REF:
2992 if (!(flags & OEP_ADDRESS_OF)
2993 && (TYPE_ALIGN (TREE_TYPE (arg0))
2994 != TYPE_ALIGN (TREE_TYPE (arg1))))
2995 return 0;
2996 flags &= ~OEP_ADDRESS_OF;
2997 return OP_SAME (0);
2999 case REALPART_EXPR:
3000 case IMAGPART_EXPR:
3001 case VIEW_CONVERT_EXPR:
3002 return OP_SAME (0);
3004 case TARGET_MEM_REF:
3005 case MEM_REF:
3006 if (!(flags & OEP_ADDRESS_OF))
3008 /* Require equal access sizes */
3009 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3010 && (!TYPE_SIZE (TREE_TYPE (arg0))
3011 || !TYPE_SIZE (TREE_TYPE (arg1))
3012 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3013 TYPE_SIZE (TREE_TYPE (arg1)),
3014 flags)))
3015 return 0;
3016 /* Verify that accesses are TBAA compatible. */
3017 if (!alias_ptr_types_compatible_p
3018 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3019 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3020 || (MR_DEPENDENCE_CLIQUE (arg0)
3021 != MR_DEPENDENCE_CLIQUE (arg1))
3022 || (MR_DEPENDENCE_BASE (arg0)
3023 != MR_DEPENDENCE_BASE (arg1)))
3024 return 0;
3025 /* Verify that alignment is compatible. */
3026 if (TYPE_ALIGN (TREE_TYPE (arg0))
3027 != TYPE_ALIGN (TREE_TYPE (arg1)))
3028 return 0;
3030 flags &= ~OEP_ADDRESS_OF;
3031 return (OP_SAME (0) && OP_SAME (1)
3032 /* TARGET_MEM_REF require equal extra operands. */
3033 && (TREE_CODE (arg0) != TARGET_MEM_REF
3034 || (OP_SAME_WITH_NULL (2)
3035 && OP_SAME_WITH_NULL (3)
3036 && OP_SAME_WITH_NULL (4))));
3038 case ARRAY_REF:
3039 case ARRAY_RANGE_REF:
3040 /* Operands 2 and 3 may be null.
3041 Compare the array index by value if it is constant first as we
3042 may have different types but same value here. */
3043 if (!OP_SAME (0))
3044 return 0;
3045 flags &= ~OEP_ADDRESS_OF;
3046 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3047 TREE_OPERAND (arg1, 1))
3048 || OP_SAME (1))
3049 && OP_SAME_WITH_NULL (2)
3050 && OP_SAME_WITH_NULL (3));
3052 case COMPONENT_REF:
3053 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3054 may be NULL when we're called to compare MEM_EXPRs. */
3055 if (!OP_SAME_WITH_NULL (0)
3056 || !OP_SAME (1))
3057 return 0;
3058 flags &= ~OEP_ADDRESS_OF;
3059 return OP_SAME_WITH_NULL (2);
3061 case BIT_FIELD_REF:
3062 if (!OP_SAME (0))
3063 return 0;
3064 flags &= ~OEP_ADDRESS_OF;
3065 return OP_SAME (1) && OP_SAME (2);
3067 default:
3068 return 0;
3071 case tcc_expression:
3072 switch (TREE_CODE (arg0))
3074 case ADDR_EXPR:
3075 /* Be sure we pass right ADDRESS_OF flag. */
3076 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3077 return operand_equal_p (TREE_OPERAND (arg0, 0),
3078 TREE_OPERAND (arg1, 0),
3079 flags | OEP_ADDRESS_OF);
3081 case TRUTH_NOT_EXPR:
3082 return OP_SAME (0);
3084 case TRUTH_ANDIF_EXPR:
3085 case TRUTH_ORIF_EXPR:
3086 return OP_SAME (0) && OP_SAME (1);
3088 case FMA_EXPR:
3089 case WIDEN_MULT_PLUS_EXPR:
3090 case WIDEN_MULT_MINUS_EXPR:
3091 if (!OP_SAME (2))
3092 return 0;
3093 /* The multiplcation operands are commutative. */
3094 /* FALLTHRU */
3096 case TRUTH_AND_EXPR:
3097 case TRUTH_OR_EXPR:
3098 case TRUTH_XOR_EXPR:
3099 if (OP_SAME (0) && OP_SAME (1))
3100 return 1;
3102 /* Otherwise take into account this is a commutative operation. */
3103 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3104 TREE_OPERAND (arg1, 1), flags)
3105 && operand_equal_p (TREE_OPERAND (arg0, 1),
3106 TREE_OPERAND (arg1, 0), flags));
3108 case COND_EXPR:
3109 case VEC_COND_EXPR:
3110 case DOT_PROD_EXPR:
3111 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3113 default:
3114 return 0;
3117 case tcc_vl_exp:
3118 switch (TREE_CODE (arg0))
3120 case CALL_EXPR:
3121 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3122 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3123 /* If not both CALL_EXPRs are either internal or normal function
3124 functions, then they are not equal. */
3125 return 0;
3126 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3128 /* If the CALL_EXPRs call different internal functions, then they
3129 are not equal. */
3130 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3131 return 0;
3133 else
3135 /* If the CALL_EXPRs call different functions, then they are not
3136 equal. */
3137 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3138 flags))
3139 return 0;
3142 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3144 unsigned int cef = call_expr_flags (arg0);
3145 if (flags & OEP_PURE_SAME)
3146 cef &= ECF_CONST | ECF_PURE;
3147 else
3148 cef &= ECF_CONST;
3149 if (!cef)
3150 return 0;
3153 /* Now see if all the arguments are the same. */
3155 const_call_expr_arg_iterator iter0, iter1;
3156 const_tree a0, a1;
3157 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3158 a1 = first_const_call_expr_arg (arg1, &iter1);
3159 a0 && a1;
3160 a0 = next_const_call_expr_arg (&iter0),
3161 a1 = next_const_call_expr_arg (&iter1))
3162 if (! operand_equal_p (a0, a1, flags))
3163 return 0;
3165 /* If we get here and both argument lists are exhausted
3166 then the CALL_EXPRs are equal. */
3167 return ! (a0 || a1);
3169 default:
3170 return 0;
3173 case tcc_declaration:
3174 /* Consider __builtin_sqrt equal to sqrt. */
3175 return (TREE_CODE (arg0) == FUNCTION_DECL
3176 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3177 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3178 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3180 case tcc_exceptional:
3181 if (TREE_CODE (arg0) == CONSTRUCTOR)
3183 /* In GIMPLE constructors are used only to build vectors from
3184 elements. Individual elements in the constructor must be
3185 indexed in increasing order and form an initial sequence.
3187 We make no effort to compare constructors in generic.
3188 (see sem_variable::equals in ipa-icf which can do so for
3189 constants). */
3190 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3191 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3192 return 0;
3194 /* Be sure that vectors constructed have the same representation.
3195 We only tested element precision and modes to match.
3196 Vectors may be BLKmode and thus also check that the number of
3197 parts match. */
3198 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3199 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3200 return 0;
3202 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3203 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3204 unsigned int len = vec_safe_length (v0);
3206 if (len != vec_safe_length (v1))
3207 return 0;
3209 for (unsigned int i = 0; i < len; i++)
3211 constructor_elt *c0 = &(*v0)[i];
3212 constructor_elt *c1 = &(*v1)[i];
3214 if (!operand_equal_p (c0->value, c1->value, flags)
3215 /* In GIMPLE the indexes can be either NULL or matching i.
3216 Double check this so we won't get false
3217 positives for GENERIC. */
3218 || (c0->index
3219 && (TREE_CODE (c0->index) != INTEGER_CST
3220 || !compare_tree_int (c0->index, i)))
3221 || (c1->index
3222 && (TREE_CODE (c1->index) != INTEGER_CST
3223 || !compare_tree_int (c1->index, i))))
3224 return 0;
3226 return 1;
3228 return 0;
3230 default:
3231 return 0;
3234 #undef OP_SAME
3235 #undef OP_SAME_WITH_NULL
3238 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3239 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3241 When in doubt, return 0. */
3243 static int
3244 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3246 int unsignedp1, unsignedpo;
3247 tree primarg0, primarg1, primother;
3248 unsigned int correct_width;
3250 if (operand_equal_p (arg0, arg1, 0))
3251 return 1;
3253 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3254 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3255 return 0;
3257 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3258 and see if the inner values are the same. This removes any
3259 signedness comparison, which doesn't matter here. */
3260 primarg0 = arg0, primarg1 = arg1;
3261 STRIP_NOPS (primarg0);
3262 STRIP_NOPS (primarg1);
3263 if (operand_equal_p (primarg0, primarg1, 0))
3264 return 1;
3266 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3267 actual comparison operand, ARG0.
3269 First throw away any conversions to wider types
3270 already present in the operands. */
3272 primarg1 = get_narrower (arg1, &unsignedp1);
3273 primother = get_narrower (other, &unsignedpo);
3275 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3276 if (unsignedp1 == unsignedpo
3277 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3278 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3280 tree type = TREE_TYPE (arg0);
3282 /* Make sure shorter operand is extended the right way
3283 to match the longer operand. */
3284 primarg1 = fold_convert (signed_or_unsigned_type_for
3285 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3287 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3288 return 1;
3291 return 0;
3294 /* See if ARG is an expression that is either a comparison or is performing
3295 arithmetic on comparisons. The comparisons must only be comparing
3296 two different values, which will be stored in *CVAL1 and *CVAL2; if
3297 they are nonzero it means that some operands have already been found.
3298 No variables may be used anywhere else in the expression except in the
3299 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3300 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3302 If this is true, return 1. Otherwise, return zero. */
3304 static int
3305 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3307 enum tree_code code = TREE_CODE (arg);
3308 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3310 /* We can handle some of the tcc_expression cases here. */
3311 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3312 tclass = tcc_unary;
3313 else if (tclass == tcc_expression
3314 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3315 || code == COMPOUND_EXPR))
3316 tclass = tcc_binary;
3318 else if (tclass == tcc_expression && code == SAVE_EXPR
3319 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3321 /* If we've already found a CVAL1 or CVAL2, this expression is
3322 two complex to handle. */
3323 if (*cval1 || *cval2)
3324 return 0;
3326 tclass = tcc_unary;
3327 *save_p = 1;
3330 switch (tclass)
3332 case tcc_unary:
3333 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3335 case tcc_binary:
3336 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3337 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3338 cval1, cval2, save_p));
3340 case tcc_constant:
3341 return 1;
3343 case tcc_expression:
3344 if (code == COND_EXPR)
3345 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3346 cval1, cval2, save_p)
3347 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3348 cval1, cval2, save_p)
3349 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3350 cval1, cval2, save_p));
3351 return 0;
3353 case tcc_comparison:
3354 /* First see if we can handle the first operand, then the second. For
3355 the second operand, we know *CVAL1 can't be zero. It must be that
3356 one side of the comparison is each of the values; test for the
3357 case where this isn't true by failing if the two operands
3358 are the same. */
3360 if (operand_equal_p (TREE_OPERAND (arg, 0),
3361 TREE_OPERAND (arg, 1), 0))
3362 return 0;
3364 if (*cval1 == 0)
3365 *cval1 = TREE_OPERAND (arg, 0);
3366 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3368 else if (*cval2 == 0)
3369 *cval2 = TREE_OPERAND (arg, 0);
3370 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3372 else
3373 return 0;
3375 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3377 else if (*cval2 == 0)
3378 *cval2 = TREE_OPERAND (arg, 1);
3379 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3381 else
3382 return 0;
3384 return 1;
3386 default:
3387 return 0;
3391 /* ARG is a tree that is known to contain just arithmetic operations and
3392 comparisons. Evaluate the operations in the tree substituting NEW0 for
3393 any occurrence of OLD0 as an operand of a comparison and likewise for
3394 NEW1 and OLD1. */
3396 static tree
3397 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3398 tree old1, tree new1)
3400 tree type = TREE_TYPE (arg);
3401 enum tree_code code = TREE_CODE (arg);
3402 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3404 /* We can handle some of the tcc_expression cases here. */
3405 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3406 tclass = tcc_unary;
3407 else if (tclass == tcc_expression
3408 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3409 tclass = tcc_binary;
3411 switch (tclass)
3413 case tcc_unary:
3414 return fold_build1_loc (loc, code, type,
3415 eval_subst (loc, TREE_OPERAND (arg, 0),
3416 old0, new0, old1, new1));
3418 case tcc_binary:
3419 return fold_build2_loc (loc, code, type,
3420 eval_subst (loc, TREE_OPERAND (arg, 0),
3421 old0, new0, old1, new1),
3422 eval_subst (loc, TREE_OPERAND (arg, 1),
3423 old0, new0, old1, new1));
3425 case tcc_expression:
3426 switch (code)
3428 case SAVE_EXPR:
3429 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3430 old1, new1);
3432 case COMPOUND_EXPR:
3433 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3434 old1, new1);
3436 case COND_EXPR:
3437 return fold_build3_loc (loc, code, type,
3438 eval_subst (loc, TREE_OPERAND (arg, 0),
3439 old0, new0, old1, new1),
3440 eval_subst (loc, TREE_OPERAND (arg, 1),
3441 old0, new0, old1, new1),
3442 eval_subst (loc, TREE_OPERAND (arg, 2),
3443 old0, new0, old1, new1));
3444 default:
3445 break;
3447 /* Fall through - ??? */
3449 case tcc_comparison:
3451 tree arg0 = TREE_OPERAND (arg, 0);
3452 tree arg1 = TREE_OPERAND (arg, 1);
3454 /* We need to check both for exact equality and tree equality. The
3455 former will be true if the operand has a side-effect. In that
3456 case, we know the operand occurred exactly once. */
3458 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3459 arg0 = new0;
3460 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3461 arg0 = new1;
3463 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3464 arg1 = new0;
3465 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3466 arg1 = new1;
3468 return fold_build2_loc (loc, code, type, arg0, arg1);
3471 default:
3472 return arg;
3476 /* Return a tree for the case when the result of an expression is RESULT
3477 converted to TYPE and OMITTED was previously an operand of the expression
3478 but is now not needed (e.g., we folded OMITTED * 0).
3480 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3481 the conversion of RESULT to TYPE. */
3483 tree
3484 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3486 tree t = fold_convert_loc (loc, type, result);
3488 /* If the resulting operand is an empty statement, just return the omitted
3489 statement casted to void. */
3490 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3491 return build1_loc (loc, NOP_EXPR, void_type_node,
3492 fold_ignored_result (omitted));
3494 if (TREE_SIDE_EFFECTS (omitted))
3495 return build2_loc (loc, COMPOUND_EXPR, type,
3496 fold_ignored_result (omitted), t);
3498 return non_lvalue_loc (loc, t);
3501 /* Return a tree for the case when the result of an expression is RESULT
3502 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3503 of the expression but are now not needed.
3505 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3506 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3507 evaluated before OMITTED2. Otherwise, if neither has side effects,
3508 just do the conversion of RESULT to TYPE. */
3510 tree
3511 omit_two_operands_loc (location_t loc, tree type, tree result,
3512 tree omitted1, tree omitted2)
3514 tree t = fold_convert_loc (loc, type, result);
3516 if (TREE_SIDE_EFFECTS (omitted2))
3517 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3518 if (TREE_SIDE_EFFECTS (omitted1))
3519 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3521 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3525 /* Return a simplified tree node for the truth-negation of ARG. This
3526 never alters ARG itself. We assume that ARG is an operation that
3527 returns a truth value (0 or 1).
3529 FIXME: one would think we would fold the result, but it causes
3530 problems with the dominator optimizer. */
3532 static tree
3533 fold_truth_not_expr (location_t loc, tree arg)
3535 tree type = TREE_TYPE (arg);
3536 enum tree_code code = TREE_CODE (arg);
3537 location_t loc1, loc2;
3539 /* If this is a comparison, we can simply invert it, except for
3540 floating-point non-equality comparisons, in which case we just
3541 enclose a TRUTH_NOT_EXPR around what we have. */
3543 if (TREE_CODE_CLASS (code) == tcc_comparison)
3545 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3546 if (FLOAT_TYPE_P (op_type)
3547 && flag_trapping_math
3548 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3549 && code != NE_EXPR && code != EQ_EXPR)
3550 return NULL_TREE;
3552 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3553 if (code == ERROR_MARK)
3554 return NULL_TREE;
3556 return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3557 TREE_OPERAND (arg, 1));
3560 switch (code)
3562 case INTEGER_CST:
3563 return constant_boolean_node (integer_zerop (arg), type);
3565 case TRUTH_AND_EXPR:
3566 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3567 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3568 return build2_loc (loc, TRUTH_OR_EXPR, type,
3569 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3570 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3572 case TRUTH_OR_EXPR:
3573 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3574 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3575 return build2_loc (loc, TRUTH_AND_EXPR, type,
3576 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3577 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3579 case TRUTH_XOR_EXPR:
3580 /* Here we can invert either operand. We invert the first operand
3581 unless the second operand is a TRUTH_NOT_EXPR in which case our
3582 result is the XOR of the first operand with the inside of the
3583 negation of the second operand. */
3585 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3586 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3587 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3588 else
3589 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3590 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3591 TREE_OPERAND (arg, 1));
3593 case TRUTH_ANDIF_EXPR:
3594 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3595 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3596 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3597 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3598 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3600 case TRUTH_ORIF_EXPR:
3601 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3602 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3603 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3604 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3605 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3607 case TRUTH_NOT_EXPR:
3608 return TREE_OPERAND (arg, 0);
3610 case COND_EXPR:
3612 tree arg1 = TREE_OPERAND (arg, 1);
3613 tree arg2 = TREE_OPERAND (arg, 2);
3615 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3616 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3618 /* A COND_EXPR may have a throw as one operand, which
3619 then has void type. Just leave void operands
3620 as they are. */
3621 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3622 VOID_TYPE_P (TREE_TYPE (arg1))
3623 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3624 VOID_TYPE_P (TREE_TYPE (arg2))
3625 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3628 case COMPOUND_EXPR:
3629 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3630 return build2_loc (loc, COMPOUND_EXPR, type,
3631 TREE_OPERAND (arg, 0),
3632 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3634 case NON_LVALUE_EXPR:
3635 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3636 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3638 CASE_CONVERT:
3639 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3640 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3642 /* ... fall through ... */
3644 case FLOAT_EXPR:
3645 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3646 return build1_loc (loc, TREE_CODE (arg), type,
3647 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3649 case BIT_AND_EXPR:
3650 if (!integer_onep (TREE_OPERAND (arg, 1)))
3651 return NULL_TREE;
3652 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3654 case SAVE_EXPR:
3655 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3657 case CLEANUP_POINT_EXPR:
3658 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3659 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3660 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3662 default:
3663 return NULL_TREE;
3667 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3668 assume that ARG is an operation that returns a truth value (0 or 1
3669 for scalars, 0 or -1 for vectors). Return the folded expression if
3670 folding is successful. Otherwise, return NULL_TREE. */
3672 static tree
3673 fold_invert_truthvalue (location_t loc, tree arg)
3675 tree type = TREE_TYPE (arg);
3676 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3677 ? BIT_NOT_EXPR
3678 : TRUTH_NOT_EXPR,
3679 type, arg);
3682 /* Return a simplified tree node for the truth-negation of ARG. This
3683 never alters ARG itself. We assume that ARG is an operation that
3684 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3686 tree
3687 invert_truthvalue_loc (location_t loc, tree arg)
3689 if (TREE_CODE (arg) == ERROR_MARK)
3690 return arg;
3692 tree type = TREE_TYPE (arg);
3693 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3694 ? BIT_NOT_EXPR
3695 : TRUTH_NOT_EXPR,
3696 type, arg);
3699 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3700 with code CODE. This optimization is unsafe. */
3701 static tree
3702 distribute_real_division (location_t loc, enum tree_code code, tree type,
3703 tree arg0, tree arg1)
3705 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3706 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3708 /* (A / C) +- (B / C) -> (A +- B) / C. */
3709 if (mul0 == mul1
3710 && operand_equal_p (TREE_OPERAND (arg0, 1),
3711 TREE_OPERAND (arg1, 1), 0))
3712 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3713 fold_build2_loc (loc, code, type,
3714 TREE_OPERAND (arg0, 0),
3715 TREE_OPERAND (arg1, 0)),
3716 TREE_OPERAND (arg0, 1));
3718 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3719 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3720 TREE_OPERAND (arg1, 0), 0)
3721 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3722 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3724 REAL_VALUE_TYPE r0, r1;
3725 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3726 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3727 if (!mul0)
3728 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3729 if (!mul1)
3730 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3731 real_arithmetic (&r0, code, &r0, &r1);
3732 return fold_build2_loc (loc, MULT_EXPR, type,
3733 TREE_OPERAND (arg0, 0),
3734 build_real (type, r0));
3737 return NULL_TREE;
3740 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3741 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3742 and uses reverse storage order if REVERSEP is nonzero. */
3744 static tree
3745 make_bit_field_ref (location_t loc, tree inner, tree type,
3746 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
3747 int unsignedp, int reversep)
3749 tree result, bftype;
3751 if (bitpos == 0 && !reversep)
3753 tree size = TYPE_SIZE (TREE_TYPE (inner));
3754 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3755 || POINTER_TYPE_P (TREE_TYPE (inner)))
3756 && tree_fits_shwi_p (size)
3757 && tree_to_shwi (size) == bitsize)
3758 return fold_convert_loc (loc, type, inner);
3761 bftype = type;
3762 if (TYPE_PRECISION (bftype) != bitsize
3763 || TYPE_UNSIGNED (bftype) == !unsignedp)
3764 bftype = build_nonstandard_integer_type (bitsize, 0);
3766 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3767 size_int (bitsize), bitsize_int (bitpos));
3768 REF_REVERSE_STORAGE_ORDER (result) = reversep;
3770 if (bftype != type)
3771 result = fold_convert_loc (loc, type, result);
3773 return result;
3776 /* Optimize a bit-field compare.
3778 There are two cases: First is a compare against a constant and the
3779 second is a comparison of two items where the fields are at the same
3780 bit position relative to the start of a chunk (byte, halfword, word)
3781 large enough to contain it. In these cases we can avoid the shift
3782 implicit in bitfield extractions.
3784 For constants, we emit a compare of the shifted constant with the
3785 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3786 compared. For two fields at the same position, we do the ANDs with the
3787 similar mask and compare the result of the ANDs.
3789 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3790 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3791 are the left and right operands of the comparison, respectively.
3793 If the optimization described above can be done, we return the resulting
3794 tree. Otherwise we return zero. */
3796 static tree
3797 optimize_bit_field_compare (location_t loc, enum tree_code code,
3798 tree compare_type, tree lhs, tree rhs)
3800 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3801 tree type = TREE_TYPE (lhs);
3802 tree unsigned_type;
3803 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3804 machine_mode lmode, rmode, nmode;
3805 int lunsignedp, runsignedp;
3806 int lreversep, rreversep;
3807 int lvolatilep = 0, rvolatilep = 0;
3808 tree linner, rinner = NULL_TREE;
3809 tree mask;
3810 tree offset;
3812 /* Get all the information about the extractions being done. If the bit size
3813 if the same as the size of the underlying object, we aren't doing an
3814 extraction at all and so can do nothing. We also don't want to
3815 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3816 then will no longer be able to replace it. */
3817 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3818 &lunsignedp, &lreversep, &lvolatilep, false);
3819 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3820 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3821 return 0;
3823 if (const_p)
3824 rreversep = lreversep;
3825 else
3827 /* If this is not a constant, we can only do something if bit positions,
3828 sizes, signedness and storage order are the same. */
3829 rinner
3830 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3831 &runsignedp, &rreversep, &rvolatilep, false);
3833 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3834 || lunsignedp != runsignedp || lreversep != rreversep || offset != 0
3835 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3836 return 0;
3839 /* See if we can find a mode to refer to this field. We should be able to,
3840 but fail if we can't. */
3841 nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3842 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3843 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3844 TYPE_ALIGN (TREE_TYPE (rinner))),
3845 word_mode, false);
3846 if (nmode == VOIDmode)
3847 return 0;
3849 /* Set signed and unsigned types of the precision of this mode for the
3850 shifts below. */
3851 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3853 /* Compute the bit position and size for the new reference and our offset
3854 within it. If the new reference is the same size as the original, we
3855 won't optimize anything, so return zero. */
3856 nbitsize = GET_MODE_BITSIZE (nmode);
3857 nbitpos = lbitpos & ~ (nbitsize - 1);
3858 lbitpos -= nbitpos;
3859 if (nbitsize == lbitsize)
3860 return 0;
3862 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
3863 lbitpos = nbitsize - lbitsize - lbitpos;
3865 /* Make the mask to be used against the extracted field. */
3866 mask = build_int_cst_type (unsigned_type, -1);
3867 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3868 mask = const_binop (RSHIFT_EXPR, mask,
3869 size_int (nbitsize - lbitsize - lbitpos));
3871 if (! const_p)
3872 /* If not comparing with constant, just rework the comparison
3873 and return. */
3874 return fold_build2_loc (loc, code, compare_type,
3875 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3876 make_bit_field_ref (loc, linner,
3877 unsigned_type,
3878 nbitsize, nbitpos,
3879 1, lreversep),
3880 mask),
3881 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3882 make_bit_field_ref (loc, rinner,
3883 unsigned_type,
3884 nbitsize, nbitpos,
3885 1, rreversep),
3886 mask));
3888 /* Otherwise, we are handling the constant case. See if the constant is too
3889 big for the field. Warn and return a tree for 0 (false) if so. We do
3890 this not only for its own sake, but to avoid having to test for this
3891 error case below. If we didn't, we might generate wrong code.
3893 For unsigned fields, the constant shifted right by the field length should
3894 be all zero. For signed fields, the high-order bits should agree with
3895 the sign bit. */
3897 if (lunsignedp)
3899 if (wi::lrshift (rhs, lbitsize) != 0)
3901 warning (0, "comparison is always %d due to width of bit-field",
3902 code == NE_EXPR);
3903 return constant_boolean_node (code == NE_EXPR, compare_type);
3906 else
3908 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3909 if (tem != 0 && tem != -1)
3911 warning (0, "comparison is always %d due to width of bit-field",
3912 code == NE_EXPR);
3913 return constant_boolean_node (code == NE_EXPR, compare_type);
3917 /* Single-bit compares should always be against zero. */
3918 if (lbitsize == 1 && ! integer_zerop (rhs))
3920 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3921 rhs = build_int_cst (type, 0);
3924 /* Make a new bitfield reference, shift the constant over the
3925 appropriate number of bits and mask it with the computed mask
3926 (in case this was a signed field). If we changed it, make a new one. */
3927 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1,
3928 lreversep);
3930 rhs = const_binop (BIT_AND_EXPR,
3931 const_binop (LSHIFT_EXPR,
3932 fold_convert_loc (loc, unsigned_type, rhs),
3933 size_int (lbitpos)),
3934 mask);
3936 lhs = build2_loc (loc, code, compare_type,
3937 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3938 return lhs;
3941 /* Subroutine for fold_truth_andor_1: decode a field reference.
3943 If EXP is a comparison reference, we return the innermost reference.
3945 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3946 set to the starting bit number.
3948 If the innermost field can be completely contained in a mode-sized
3949 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3951 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3952 otherwise it is not changed.
3954 *PUNSIGNEDP is set to the signedness of the field.
3956 *PREVERSEP is set to the storage order of the field.
3958 *PMASK is set to the mask used. This is either contained in a
3959 BIT_AND_EXPR or derived from the width of the field.
3961 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3963 Return 0 if this is not a component reference or is one that we can't
3964 do anything with. */
3966 static tree
3967 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3968 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
3969 int *punsignedp, int *preversep, int *pvolatilep,
3970 tree *pmask, tree *pand_mask)
3972 tree outer_type = 0;
3973 tree and_mask = 0;
3974 tree mask, inner, offset;
3975 tree unsigned_type;
3976 unsigned int precision;
3978 /* All the optimizations using this function assume integer fields.
3979 There are problems with FP fields since the type_for_size call
3980 below can fail for, e.g., XFmode. */
3981 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3982 return 0;
3984 /* We are interested in the bare arrangement of bits, so strip everything
3985 that doesn't affect the machine mode. However, record the type of the
3986 outermost expression if it may matter below. */
3987 if (CONVERT_EXPR_P (exp)
3988 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3989 outer_type = TREE_TYPE (exp);
3990 STRIP_NOPS (exp);
3992 if (TREE_CODE (exp) == BIT_AND_EXPR)
3994 and_mask = TREE_OPERAND (exp, 1);
3995 exp = TREE_OPERAND (exp, 0);
3996 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3997 if (TREE_CODE (and_mask) != INTEGER_CST)
3998 return 0;
4001 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4002 punsignedp, preversep, pvolatilep, false);
4003 if ((inner == exp && and_mask == 0)
4004 || *pbitsize < 0 || offset != 0
4005 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4006 return 0;
4008 /* If the number of bits in the reference is the same as the bitsize of
4009 the outer type, then the outer type gives the signedness. Otherwise
4010 (in case of a small bitfield) the signedness is unchanged. */
4011 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4012 *punsignedp = TYPE_UNSIGNED (outer_type);
4014 /* Compute the mask to access the bitfield. */
4015 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4016 precision = TYPE_PRECISION (unsigned_type);
4018 mask = build_int_cst_type (unsigned_type, -1);
4020 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4021 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4023 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4024 if (and_mask != 0)
4025 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4026 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4028 *pmask = mask;
4029 *pand_mask = and_mask;
4030 return inner;
4033 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4034 bit positions and MASK is SIGNED. */
4036 static int
4037 all_ones_mask_p (const_tree mask, unsigned int size)
4039 tree type = TREE_TYPE (mask);
4040 unsigned int precision = TYPE_PRECISION (type);
4042 /* If this function returns true when the type of the mask is
4043 UNSIGNED, then there will be errors. In particular see
4044 gcc.c-torture/execute/990326-1.c. There does not appear to be
4045 any documentation paper trail as to why this is so. But the pre
4046 wide-int worked with that restriction and it has been preserved
4047 here. */
4048 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4049 return false;
4051 return wi::mask (size, false, precision) == mask;
4054 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4055 represents the sign bit of EXP's type. If EXP represents a sign
4056 or zero extension, also test VAL against the unextended type.
4057 The return value is the (sub)expression whose sign bit is VAL,
4058 or NULL_TREE otherwise. */
4060 tree
4061 sign_bit_p (tree exp, const_tree val)
4063 int width;
4064 tree t;
4066 /* Tree EXP must have an integral type. */
4067 t = TREE_TYPE (exp);
4068 if (! INTEGRAL_TYPE_P (t))
4069 return NULL_TREE;
4071 /* Tree VAL must be an integer constant. */
4072 if (TREE_CODE (val) != INTEGER_CST
4073 || TREE_OVERFLOW (val))
4074 return NULL_TREE;
4076 width = TYPE_PRECISION (t);
4077 if (wi::only_sign_bit_p (val, width))
4078 return exp;
4080 /* Handle extension from a narrower type. */
4081 if (TREE_CODE (exp) == NOP_EXPR
4082 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4083 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4085 return NULL_TREE;
4088 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4089 to be evaluated unconditionally. */
4091 static int
4092 simple_operand_p (const_tree exp)
4094 /* Strip any conversions that don't change the machine mode. */
4095 STRIP_NOPS (exp);
4097 return (CONSTANT_CLASS_P (exp)
4098 || TREE_CODE (exp) == SSA_NAME
4099 || (DECL_P (exp)
4100 && ! TREE_ADDRESSABLE (exp)
4101 && ! TREE_THIS_VOLATILE (exp)
4102 && ! DECL_NONLOCAL (exp)
4103 /* Don't regard global variables as simple. They may be
4104 allocated in ways unknown to the compiler (shared memory,
4105 #pragma weak, etc). */
4106 && ! TREE_PUBLIC (exp)
4107 && ! DECL_EXTERNAL (exp)
4108 /* Weakrefs are not safe to be read, since they can be NULL.
4109 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4110 have DECL_WEAK flag set. */
4111 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4112 /* Loading a static variable is unduly expensive, but global
4113 registers aren't expensive. */
4114 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4117 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4118 to be evaluated unconditionally.
4119 I addition to simple_operand_p, we assume that comparisons, conversions,
4120 and logic-not operations are simple, if their operands are simple, too. */
4122 static bool
4123 simple_operand_p_2 (tree exp)
4125 enum tree_code code;
4127 if (TREE_SIDE_EFFECTS (exp)
4128 || tree_could_trap_p (exp))
4129 return false;
4131 while (CONVERT_EXPR_P (exp))
4132 exp = TREE_OPERAND (exp, 0);
4134 code = TREE_CODE (exp);
4136 if (TREE_CODE_CLASS (code) == tcc_comparison)
4137 return (simple_operand_p (TREE_OPERAND (exp, 0))
4138 && simple_operand_p (TREE_OPERAND (exp, 1)));
4140 if (code == TRUTH_NOT_EXPR)
4141 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4143 return simple_operand_p (exp);
4147 /* The following functions are subroutines to fold_range_test and allow it to
4148 try to change a logical combination of comparisons into a range test.
4150 For example, both
4151 X == 2 || X == 3 || X == 4 || X == 5
4153 X >= 2 && X <= 5
4154 are converted to
4155 (unsigned) (X - 2) <= 3
4157 We describe each set of comparisons as being either inside or outside
4158 a range, using a variable named like IN_P, and then describe the
4159 range with a lower and upper bound. If one of the bounds is omitted,
4160 it represents either the highest or lowest value of the type.
4162 In the comments below, we represent a range by two numbers in brackets
4163 preceded by a "+" to designate being inside that range, or a "-" to
4164 designate being outside that range, so the condition can be inverted by
4165 flipping the prefix. An omitted bound is represented by a "-". For
4166 example, "- [-, 10]" means being outside the range starting at the lowest
4167 possible value and ending at 10, in other words, being greater than 10.
4168 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4169 always false.
4171 We set up things so that the missing bounds are handled in a consistent
4172 manner so neither a missing bound nor "true" and "false" need to be
4173 handled using a special case. */
4175 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4176 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4177 and UPPER1_P are nonzero if the respective argument is an upper bound
4178 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4179 must be specified for a comparison. ARG1 will be converted to ARG0's
4180 type if both are specified. */
4182 static tree
4183 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4184 tree arg1, int upper1_p)
4186 tree tem;
4187 int result;
4188 int sgn0, sgn1;
4190 /* If neither arg represents infinity, do the normal operation.
4191 Else, if not a comparison, return infinity. Else handle the special
4192 comparison rules. Note that most of the cases below won't occur, but
4193 are handled for consistency. */
4195 if (arg0 != 0 && arg1 != 0)
4197 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4198 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4199 STRIP_NOPS (tem);
4200 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4203 if (TREE_CODE_CLASS (code) != tcc_comparison)
4204 return 0;
4206 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4207 for neither. In real maths, we cannot assume open ended ranges are
4208 the same. But, this is computer arithmetic, where numbers are finite.
4209 We can therefore make the transformation of any unbounded range with
4210 the value Z, Z being greater than any representable number. This permits
4211 us to treat unbounded ranges as equal. */
4212 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4213 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4214 switch (code)
4216 case EQ_EXPR:
4217 result = sgn0 == sgn1;
4218 break;
4219 case NE_EXPR:
4220 result = sgn0 != sgn1;
4221 break;
4222 case LT_EXPR:
4223 result = sgn0 < sgn1;
4224 break;
4225 case LE_EXPR:
4226 result = sgn0 <= sgn1;
4227 break;
4228 case GT_EXPR:
4229 result = sgn0 > sgn1;
4230 break;
4231 case GE_EXPR:
4232 result = sgn0 >= sgn1;
4233 break;
4234 default:
4235 gcc_unreachable ();
4238 return constant_boolean_node (result, type);
4241 /* Helper routine for make_range. Perform one step for it, return
4242 new expression if the loop should continue or NULL_TREE if it should
4243 stop. */
4245 tree
4246 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4247 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4248 bool *strict_overflow_p)
4250 tree arg0_type = TREE_TYPE (arg0);
4251 tree n_low, n_high, low = *p_low, high = *p_high;
4252 int in_p = *p_in_p, n_in_p;
4254 switch (code)
4256 case TRUTH_NOT_EXPR:
4257 /* We can only do something if the range is testing for zero. */
4258 if (low == NULL_TREE || high == NULL_TREE
4259 || ! integer_zerop (low) || ! integer_zerop (high))
4260 return NULL_TREE;
4261 *p_in_p = ! in_p;
4262 return arg0;
4264 case EQ_EXPR: case NE_EXPR:
4265 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4266 /* We can only do something if the range is testing for zero
4267 and if the second operand is an integer constant. Note that
4268 saying something is "in" the range we make is done by
4269 complementing IN_P since it will set in the initial case of
4270 being not equal to zero; "out" is leaving it alone. */
4271 if (low == NULL_TREE || high == NULL_TREE
4272 || ! integer_zerop (low) || ! integer_zerop (high)
4273 || TREE_CODE (arg1) != INTEGER_CST)
4274 return NULL_TREE;
4276 switch (code)
4278 case NE_EXPR: /* - [c, c] */
4279 low = high = arg1;
4280 break;
4281 case EQ_EXPR: /* + [c, c] */
4282 in_p = ! in_p, low = high = arg1;
4283 break;
4284 case GT_EXPR: /* - [-, c] */
4285 low = 0, high = arg1;
4286 break;
4287 case GE_EXPR: /* + [c, -] */
4288 in_p = ! in_p, low = arg1, high = 0;
4289 break;
4290 case LT_EXPR: /* - [c, -] */
4291 low = arg1, high = 0;
4292 break;
4293 case LE_EXPR: /* + [-, c] */
4294 in_p = ! in_p, low = 0, high = arg1;
4295 break;
4296 default:
4297 gcc_unreachable ();
4300 /* If this is an unsigned comparison, we also know that EXP is
4301 greater than or equal to zero. We base the range tests we make
4302 on that fact, so we record it here so we can parse existing
4303 range tests. We test arg0_type since often the return type
4304 of, e.g. EQ_EXPR, is boolean. */
4305 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4307 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4308 in_p, low, high, 1,
4309 build_int_cst (arg0_type, 0),
4310 NULL_TREE))
4311 return NULL_TREE;
4313 in_p = n_in_p, low = n_low, high = n_high;
4315 /* If the high bound is missing, but we have a nonzero low
4316 bound, reverse the range so it goes from zero to the low bound
4317 minus 1. */
4318 if (high == 0 && low && ! integer_zerop (low))
4320 in_p = ! in_p;
4321 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4322 build_int_cst (TREE_TYPE (low), 1), 0);
4323 low = build_int_cst (arg0_type, 0);
4327 *p_low = low;
4328 *p_high = high;
4329 *p_in_p = in_p;
4330 return arg0;
4332 case NEGATE_EXPR:
4333 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4334 low and high are non-NULL, then normalize will DTRT. */
4335 if (!TYPE_UNSIGNED (arg0_type)
4336 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4338 if (low == NULL_TREE)
4339 low = TYPE_MIN_VALUE (arg0_type);
4340 if (high == NULL_TREE)
4341 high = TYPE_MAX_VALUE (arg0_type);
4344 /* (-x) IN [a,b] -> x in [-b, -a] */
4345 n_low = range_binop (MINUS_EXPR, exp_type,
4346 build_int_cst (exp_type, 0),
4347 0, high, 1);
4348 n_high = range_binop (MINUS_EXPR, exp_type,
4349 build_int_cst (exp_type, 0),
4350 0, low, 0);
4351 if (n_high != 0 && TREE_OVERFLOW (n_high))
4352 return NULL_TREE;
4353 goto normalize;
4355 case BIT_NOT_EXPR:
4356 /* ~ X -> -X - 1 */
4357 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4358 build_int_cst (exp_type, 1));
4360 case PLUS_EXPR:
4361 case MINUS_EXPR:
4362 if (TREE_CODE (arg1) != INTEGER_CST)
4363 return NULL_TREE;
4365 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4366 move a constant to the other side. */
4367 if (!TYPE_UNSIGNED (arg0_type)
4368 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4369 return NULL_TREE;
4371 /* If EXP is signed, any overflow in the computation is undefined,
4372 so we don't worry about it so long as our computations on
4373 the bounds don't overflow. For unsigned, overflow is defined
4374 and this is exactly the right thing. */
4375 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4376 arg0_type, low, 0, arg1, 0);
4377 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4378 arg0_type, high, 1, arg1, 0);
4379 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4380 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4381 return NULL_TREE;
4383 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4384 *strict_overflow_p = true;
4386 normalize:
4387 /* Check for an unsigned range which has wrapped around the maximum
4388 value thus making n_high < n_low, and normalize it. */
4389 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4391 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4392 build_int_cst (TREE_TYPE (n_high), 1), 0);
4393 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4394 build_int_cst (TREE_TYPE (n_low), 1), 0);
4396 /* If the range is of the form +/- [ x+1, x ], we won't
4397 be able to normalize it. But then, it represents the
4398 whole range or the empty set, so make it
4399 +/- [ -, - ]. */
4400 if (tree_int_cst_equal (n_low, low)
4401 && tree_int_cst_equal (n_high, high))
4402 low = high = 0;
4403 else
4404 in_p = ! in_p;
4406 else
4407 low = n_low, high = n_high;
4409 *p_low = low;
4410 *p_high = high;
4411 *p_in_p = in_p;
4412 return arg0;
4414 CASE_CONVERT:
4415 case NON_LVALUE_EXPR:
4416 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4417 return NULL_TREE;
4419 if (! INTEGRAL_TYPE_P (arg0_type)
4420 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4421 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4422 return NULL_TREE;
4424 n_low = low, n_high = high;
4426 if (n_low != 0)
4427 n_low = fold_convert_loc (loc, arg0_type, n_low);
4429 if (n_high != 0)
4430 n_high = fold_convert_loc (loc, arg0_type, n_high);
4432 /* If we're converting arg0 from an unsigned type, to exp,
4433 a signed type, we will be doing the comparison as unsigned.
4434 The tests above have already verified that LOW and HIGH
4435 are both positive.
4437 So we have to ensure that we will handle large unsigned
4438 values the same way that the current signed bounds treat
4439 negative values. */
4441 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4443 tree high_positive;
4444 tree equiv_type;
4445 /* For fixed-point modes, we need to pass the saturating flag
4446 as the 2nd parameter. */
4447 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4448 equiv_type
4449 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4450 TYPE_SATURATING (arg0_type));
4451 else
4452 equiv_type
4453 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4455 /* A range without an upper bound is, naturally, unbounded.
4456 Since convert would have cropped a very large value, use
4457 the max value for the destination type. */
4458 high_positive
4459 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4460 : TYPE_MAX_VALUE (arg0_type);
4462 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4463 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4464 fold_convert_loc (loc, arg0_type,
4465 high_positive),
4466 build_int_cst (arg0_type, 1));
4468 /* If the low bound is specified, "and" the range with the
4469 range for which the original unsigned value will be
4470 positive. */
4471 if (low != 0)
4473 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4474 1, fold_convert_loc (loc, arg0_type,
4475 integer_zero_node),
4476 high_positive))
4477 return NULL_TREE;
4479 in_p = (n_in_p == in_p);
4481 else
4483 /* Otherwise, "or" the range with the range of the input
4484 that will be interpreted as negative. */
4485 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4486 1, fold_convert_loc (loc, arg0_type,
4487 integer_zero_node),
4488 high_positive))
4489 return NULL_TREE;
4491 in_p = (in_p != n_in_p);
4495 *p_low = n_low;
4496 *p_high = n_high;
4497 *p_in_p = in_p;
4498 return arg0;
4500 default:
4501 return NULL_TREE;
4505 /* Given EXP, a logical expression, set the range it is testing into
4506 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4507 actually being tested. *PLOW and *PHIGH will be made of the same
4508 type as the returned expression. If EXP is not a comparison, we
4509 will most likely not be returning a useful value and range. Set
4510 *STRICT_OVERFLOW_P to true if the return value is only valid
4511 because signed overflow is undefined; otherwise, do not change
4512 *STRICT_OVERFLOW_P. */
4514 tree
4515 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4516 bool *strict_overflow_p)
4518 enum tree_code code;
4519 tree arg0, arg1 = NULL_TREE;
4520 tree exp_type, nexp;
4521 int in_p;
4522 tree low, high;
4523 location_t loc = EXPR_LOCATION (exp);
4525 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4526 and see if we can refine the range. Some of the cases below may not
4527 happen, but it doesn't seem worth worrying about this. We "continue"
4528 the outer loop when we've changed something; otherwise we "break"
4529 the switch, which will "break" the while. */
4531 in_p = 0;
4532 low = high = build_int_cst (TREE_TYPE (exp), 0);
4534 while (1)
4536 code = TREE_CODE (exp);
4537 exp_type = TREE_TYPE (exp);
4538 arg0 = NULL_TREE;
4540 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4542 if (TREE_OPERAND_LENGTH (exp) > 0)
4543 arg0 = TREE_OPERAND (exp, 0);
4544 if (TREE_CODE_CLASS (code) == tcc_binary
4545 || TREE_CODE_CLASS (code) == tcc_comparison
4546 || (TREE_CODE_CLASS (code) == tcc_expression
4547 && TREE_OPERAND_LENGTH (exp) > 1))
4548 arg1 = TREE_OPERAND (exp, 1);
4550 if (arg0 == NULL_TREE)
4551 break;
4553 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4554 &high, &in_p, strict_overflow_p);
4555 if (nexp == NULL_TREE)
4556 break;
4557 exp = nexp;
4560 /* If EXP is a constant, we can evaluate whether this is true or false. */
4561 if (TREE_CODE (exp) == INTEGER_CST)
4563 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4564 exp, 0, low, 0))
4565 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4566 exp, 1, high, 1)));
4567 low = high = 0;
4568 exp = 0;
4571 *pin_p = in_p, *plow = low, *phigh = high;
4572 return exp;
4575 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4576 type, TYPE, return an expression to test if EXP is in (or out of, depending
4577 on IN_P) the range. Return 0 if the test couldn't be created. */
4579 tree
4580 build_range_check (location_t loc, tree type, tree exp, int in_p,
4581 tree low, tree high)
4583 tree etype = TREE_TYPE (exp), value;
4585 /* Disable this optimization for function pointer expressions
4586 on targets that require function pointer canonicalization. */
4587 if (targetm.have_canonicalize_funcptr_for_compare ()
4588 && TREE_CODE (etype) == POINTER_TYPE
4589 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4590 return NULL_TREE;
4592 if (! in_p)
4594 value = build_range_check (loc, type, exp, 1, low, high);
4595 if (value != 0)
4596 return invert_truthvalue_loc (loc, value);
4598 return 0;
4601 if (low == 0 && high == 0)
4602 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4604 if (low == 0)
4605 return fold_build2_loc (loc, LE_EXPR, type, exp,
4606 fold_convert_loc (loc, etype, high));
4608 if (high == 0)
4609 return fold_build2_loc (loc, GE_EXPR, type, exp,
4610 fold_convert_loc (loc, etype, low));
4612 if (operand_equal_p (low, high, 0))
4613 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4614 fold_convert_loc (loc, etype, low));
4616 if (integer_zerop (low))
4618 if (! TYPE_UNSIGNED (etype))
4620 etype = unsigned_type_for (etype);
4621 high = fold_convert_loc (loc, etype, high);
4622 exp = fold_convert_loc (loc, etype, exp);
4624 return build_range_check (loc, type, exp, 1, 0, high);
4627 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4628 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4630 int prec = TYPE_PRECISION (etype);
4632 if (wi::mask (prec - 1, false, prec) == high)
4634 if (TYPE_UNSIGNED (etype))
4636 tree signed_etype = signed_type_for (etype);
4637 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4638 etype
4639 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4640 else
4641 etype = signed_etype;
4642 exp = fold_convert_loc (loc, etype, exp);
4644 return fold_build2_loc (loc, GT_EXPR, type, exp,
4645 build_int_cst (etype, 0));
4649 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4650 This requires wrap-around arithmetics for the type of the expression.
4651 First make sure that arithmetics in this type is valid, then make sure
4652 that it wraps around. */
4653 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4654 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4655 TYPE_UNSIGNED (etype));
4657 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4659 tree utype, minv, maxv;
4661 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4662 for the type in question, as we rely on this here. */
4663 utype = unsigned_type_for (etype);
4664 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4665 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4666 build_int_cst (TREE_TYPE (maxv), 1), 1);
4667 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4669 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4670 minv, 1, maxv, 1)))
4671 etype = utype;
4672 else
4673 return 0;
4676 high = fold_convert_loc (loc, etype, high);
4677 low = fold_convert_loc (loc, etype, low);
4678 exp = fold_convert_loc (loc, etype, exp);
4680 value = const_binop (MINUS_EXPR, high, low);
4683 if (POINTER_TYPE_P (etype))
4685 if (value != 0 && !TREE_OVERFLOW (value))
4687 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4688 return build_range_check (loc, type,
4689 fold_build_pointer_plus_loc (loc, exp, low),
4690 1, build_int_cst (etype, 0), value);
4692 return 0;
4695 if (value != 0 && !TREE_OVERFLOW (value))
4696 return build_range_check (loc, type,
4697 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4698 1, build_int_cst (etype, 0), value);
4700 return 0;
4703 /* Return the predecessor of VAL in its type, handling the infinite case. */
4705 static tree
4706 range_predecessor (tree val)
4708 tree type = TREE_TYPE (val);
4710 if (INTEGRAL_TYPE_P (type)
4711 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4712 return 0;
4713 else
4714 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4715 build_int_cst (TREE_TYPE (val), 1), 0);
4718 /* Return the successor of VAL in its type, handling the infinite case. */
4720 static tree
4721 range_successor (tree val)
4723 tree type = TREE_TYPE (val);
4725 if (INTEGRAL_TYPE_P (type)
4726 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4727 return 0;
4728 else
4729 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4730 build_int_cst (TREE_TYPE (val), 1), 0);
4733 /* Given two ranges, see if we can merge them into one. Return 1 if we
4734 can, 0 if we can't. Set the output range into the specified parameters. */
4736 bool
4737 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4738 tree high0, int in1_p, tree low1, tree high1)
4740 int no_overlap;
4741 int subset;
4742 int temp;
4743 tree tem;
4744 int in_p;
4745 tree low, high;
4746 int lowequal = ((low0 == 0 && low1 == 0)
4747 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4748 low0, 0, low1, 0)));
4749 int highequal = ((high0 == 0 && high1 == 0)
4750 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4751 high0, 1, high1, 1)));
4753 /* Make range 0 be the range that starts first, or ends last if they
4754 start at the same value. Swap them if it isn't. */
4755 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4756 low0, 0, low1, 0))
4757 || (lowequal
4758 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4759 high1, 1, high0, 1))))
4761 temp = in0_p, in0_p = in1_p, in1_p = temp;
4762 tem = low0, low0 = low1, low1 = tem;
4763 tem = high0, high0 = high1, high1 = tem;
4766 /* Now flag two cases, whether the ranges are disjoint or whether the
4767 second range is totally subsumed in the first. Note that the tests
4768 below are simplified by the ones above. */
4769 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4770 high0, 1, low1, 0));
4771 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4772 high1, 1, high0, 1));
4774 /* We now have four cases, depending on whether we are including or
4775 excluding the two ranges. */
4776 if (in0_p && in1_p)
4778 /* If they don't overlap, the result is false. If the second range
4779 is a subset it is the result. Otherwise, the range is from the start
4780 of the second to the end of the first. */
4781 if (no_overlap)
4782 in_p = 0, low = high = 0;
4783 else if (subset)
4784 in_p = 1, low = low1, high = high1;
4785 else
4786 in_p = 1, low = low1, high = high0;
4789 else if (in0_p && ! in1_p)
4791 /* If they don't overlap, the result is the first range. If they are
4792 equal, the result is false. If the second range is a subset of the
4793 first, and the ranges begin at the same place, we go from just after
4794 the end of the second range to the end of the first. If the second
4795 range is not a subset of the first, or if it is a subset and both
4796 ranges end at the same place, the range starts at the start of the
4797 first range and ends just before the second range.
4798 Otherwise, we can't describe this as a single range. */
4799 if (no_overlap)
4800 in_p = 1, low = low0, high = high0;
4801 else if (lowequal && highequal)
4802 in_p = 0, low = high = 0;
4803 else if (subset && lowequal)
4805 low = range_successor (high1);
4806 high = high0;
4807 in_p = 1;
4808 if (low == 0)
4810 /* We are in the weird situation where high0 > high1 but
4811 high1 has no successor. Punt. */
4812 return 0;
4815 else if (! subset || highequal)
4817 low = low0;
4818 high = range_predecessor (low1);
4819 in_p = 1;
4820 if (high == 0)
4822 /* low0 < low1 but low1 has no predecessor. Punt. */
4823 return 0;
4826 else
4827 return 0;
4830 else if (! in0_p && in1_p)
4832 /* If they don't overlap, the result is the second range. If the second
4833 is a subset of the first, the result is false. Otherwise,
4834 the range starts just after the first range and ends at the
4835 end of the second. */
4836 if (no_overlap)
4837 in_p = 1, low = low1, high = high1;
4838 else if (subset || highequal)
4839 in_p = 0, low = high = 0;
4840 else
4842 low = range_successor (high0);
4843 high = high1;
4844 in_p = 1;
4845 if (low == 0)
4847 /* high1 > high0 but high0 has no successor. Punt. */
4848 return 0;
4853 else
4855 /* The case where we are excluding both ranges. Here the complex case
4856 is if they don't overlap. In that case, the only time we have a
4857 range is if they are adjacent. If the second is a subset of the
4858 first, the result is the first. Otherwise, the range to exclude
4859 starts at the beginning of the first range and ends at the end of the
4860 second. */
4861 if (no_overlap)
4863 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4864 range_successor (high0),
4865 1, low1, 0)))
4866 in_p = 0, low = low0, high = high1;
4867 else
4869 /* Canonicalize - [min, x] into - [-, x]. */
4870 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4871 switch (TREE_CODE (TREE_TYPE (low0)))
4873 case ENUMERAL_TYPE:
4874 if (TYPE_PRECISION (TREE_TYPE (low0))
4875 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4876 break;
4877 /* FALLTHROUGH */
4878 case INTEGER_TYPE:
4879 if (tree_int_cst_equal (low0,
4880 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4881 low0 = 0;
4882 break;
4883 case POINTER_TYPE:
4884 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4885 && integer_zerop (low0))
4886 low0 = 0;
4887 break;
4888 default:
4889 break;
4892 /* Canonicalize - [x, max] into - [x, -]. */
4893 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4894 switch (TREE_CODE (TREE_TYPE (high1)))
4896 case ENUMERAL_TYPE:
4897 if (TYPE_PRECISION (TREE_TYPE (high1))
4898 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4899 break;
4900 /* FALLTHROUGH */
4901 case INTEGER_TYPE:
4902 if (tree_int_cst_equal (high1,
4903 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4904 high1 = 0;
4905 break;
4906 case POINTER_TYPE:
4907 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4908 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4909 high1, 1,
4910 build_int_cst (TREE_TYPE (high1), 1),
4911 1)))
4912 high1 = 0;
4913 break;
4914 default:
4915 break;
4918 /* The ranges might be also adjacent between the maximum and
4919 minimum values of the given type. For
4920 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4921 return + [x + 1, y - 1]. */
4922 if (low0 == 0 && high1 == 0)
4924 low = range_successor (high0);
4925 high = range_predecessor (low1);
4926 if (low == 0 || high == 0)
4927 return 0;
4929 in_p = 1;
4931 else
4932 return 0;
4935 else if (subset)
4936 in_p = 0, low = low0, high = high0;
4937 else
4938 in_p = 0, low = low0, high = high1;
4941 *pin_p = in_p, *plow = low, *phigh = high;
4942 return 1;
4946 /* Subroutine of fold, looking inside expressions of the form
4947 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4948 of the COND_EXPR. This function is being used also to optimize
4949 A op B ? C : A, by reversing the comparison first.
4951 Return a folded expression whose code is not a COND_EXPR
4952 anymore, or NULL_TREE if no folding opportunity is found. */
4954 static tree
4955 fold_cond_expr_with_comparison (location_t loc, tree type,
4956 tree arg0, tree arg1, tree arg2)
4958 enum tree_code comp_code = TREE_CODE (arg0);
4959 tree arg00 = TREE_OPERAND (arg0, 0);
4960 tree arg01 = TREE_OPERAND (arg0, 1);
4961 tree arg1_type = TREE_TYPE (arg1);
4962 tree tem;
4964 STRIP_NOPS (arg1);
4965 STRIP_NOPS (arg2);
4967 /* If we have A op 0 ? A : -A, consider applying the following
4968 transformations:
4970 A == 0? A : -A same as -A
4971 A != 0? A : -A same as A
4972 A >= 0? A : -A same as abs (A)
4973 A > 0? A : -A same as abs (A)
4974 A <= 0? A : -A same as -abs (A)
4975 A < 0? A : -A same as -abs (A)
4977 None of these transformations work for modes with signed
4978 zeros. If A is +/-0, the first two transformations will
4979 change the sign of the result (from +0 to -0, or vice
4980 versa). The last four will fix the sign of the result,
4981 even though the original expressions could be positive or
4982 negative, depending on the sign of A.
4984 Note that all these transformations are correct if A is
4985 NaN, since the two alternatives (A and -A) are also NaNs. */
4986 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4987 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4988 ? real_zerop (arg01)
4989 : integer_zerop (arg01))
4990 && ((TREE_CODE (arg2) == NEGATE_EXPR
4991 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4992 /* In the case that A is of the form X-Y, '-A' (arg2) may
4993 have already been folded to Y-X, check for that. */
4994 || (TREE_CODE (arg1) == MINUS_EXPR
4995 && TREE_CODE (arg2) == MINUS_EXPR
4996 && operand_equal_p (TREE_OPERAND (arg1, 0),
4997 TREE_OPERAND (arg2, 1), 0)
4998 && operand_equal_p (TREE_OPERAND (arg1, 1),
4999 TREE_OPERAND (arg2, 0), 0))))
5000 switch (comp_code)
5002 case EQ_EXPR:
5003 case UNEQ_EXPR:
5004 tem = fold_convert_loc (loc, arg1_type, arg1);
5005 return pedantic_non_lvalue_loc (loc,
5006 fold_convert_loc (loc, type,
5007 negate_expr (tem)));
5008 case NE_EXPR:
5009 case LTGT_EXPR:
5010 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5011 case UNGE_EXPR:
5012 case UNGT_EXPR:
5013 if (flag_trapping_math)
5014 break;
5015 /* Fall through. */
5016 case GE_EXPR:
5017 case GT_EXPR:
5018 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5019 break;
5020 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5021 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5022 case UNLE_EXPR:
5023 case UNLT_EXPR:
5024 if (flag_trapping_math)
5025 break;
5026 case LE_EXPR:
5027 case LT_EXPR:
5028 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5029 break;
5030 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5031 return negate_expr (fold_convert_loc (loc, type, tem));
5032 default:
5033 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5034 break;
5037 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5038 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5039 both transformations are correct when A is NaN: A != 0
5040 is then true, and A == 0 is false. */
5042 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5043 && integer_zerop (arg01) && integer_zerop (arg2))
5045 if (comp_code == NE_EXPR)
5046 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5047 else if (comp_code == EQ_EXPR)
5048 return build_zero_cst (type);
5051 /* Try some transformations of A op B ? A : B.
5053 A == B? A : B same as B
5054 A != B? A : B same as A
5055 A >= B? A : B same as max (A, B)
5056 A > B? A : B same as max (B, A)
5057 A <= B? A : B same as min (A, B)
5058 A < B? A : B same as min (B, A)
5060 As above, these transformations don't work in the presence
5061 of signed zeros. For example, if A and B are zeros of
5062 opposite sign, the first two transformations will change
5063 the sign of the result. In the last four, the original
5064 expressions give different results for (A=+0, B=-0) and
5065 (A=-0, B=+0), but the transformed expressions do not.
5067 The first two transformations are correct if either A or B
5068 is a NaN. In the first transformation, the condition will
5069 be false, and B will indeed be chosen. In the case of the
5070 second transformation, the condition A != B will be true,
5071 and A will be chosen.
5073 The conversions to max() and min() are not correct if B is
5074 a number and A is not. The conditions in the original
5075 expressions will be false, so all four give B. The min()
5076 and max() versions would give a NaN instead. */
5077 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5078 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5079 /* Avoid these transformations if the COND_EXPR may be used
5080 as an lvalue in the C++ front-end. PR c++/19199. */
5081 && (in_gimple_form
5082 || VECTOR_TYPE_P (type)
5083 || (! lang_GNU_CXX ()
5084 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5085 || ! maybe_lvalue_p (arg1)
5086 || ! maybe_lvalue_p (arg2)))
5088 tree comp_op0 = arg00;
5089 tree comp_op1 = arg01;
5090 tree comp_type = TREE_TYPE (comp_op0);
5092 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5093 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5095 comp_type = type;
5096 comp_op0 = arg1;
5097 comp_op1 = arg2;
5100 switch (comp_code)
5102 case EQ_EXPR:
5103 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5104 case NE_EXPR:
5105 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5106 case LE_EXPR:
5107 case LT_EXPR:
5108 case UNLE_EXPR:
5109 case UNLT_EXPR:
5110 /* In C++ a ?: expression can be an lvalue, so put the
5111 operand which will be used if they are equal first
5112 so that we can convert this back to the
5113 corresponding COND_EXPR. */
5114 if (!HONOR_NANS (arg1))
5116 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5117 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5118 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5119 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5120 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5121 comp_op1, comp_op0);
5122 return pedantic_non_lvalue_loc (loc,
5123 fold_convert_loc (loc, type, tem));
5125 break;
5126 case GE_EXPR:
5127 case GT_EXPR:
5128 case UNGE_EXPR:
5129 case UNGT_EXPR:
5130 if (!HONOR_NANS (arg1))
5132 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5133 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5134 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5135 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5136 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5137 comp_op1, comp_op0);
5138 return pedantic_non_lvalue_loc (loc,
5139 fold_convert_loc (loc, type, tem));
5141 break;
5142 case UNEQ_EXPR:
5143 if (!HONOR_NANS (arg1))
5144 return pedantic_non_lvalue_loc (loc,
5145 fold_convert_loc (loc, type, arg2));
5146 break;
5147 case LTGT_EXPR:
5148 if (!HONOR_NANS (arg1))
5149 return pedantic_non_lvalue_loc (loc,
5150 fold_convert_loc (loc, type, arg1));
5151 break;
5152 default:
5153 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5154 break;
5158 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5159 we might still be able to simplify this. For example,
5160 if C1 is one less or one more than C2, this might have started
5161 out as a MIN or MAX and been transformed by this function.
5162 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5164 if (INTEGRAL_TYPE_P (type)
5165 && TREE_CODE (arg01) == INTEGER_CST
5166 && TREE_CODE (arg2) == INTEGER_CST)
5167 switch (comp_code)
5169 case EQ_EXPR:
5170 if (TREE_CODE (arg1) == INTEGER_CST)
5171 break;
5172 /* We can replace A with C1 in this case. */
5173 arg1 = fold_convert_loc (loc, type, arg01);
5174 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5176 case LT_EXPR:
5177 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5178 MIN_EXPR, to preserve the signedness of the comparison. */
5179 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5180 OEP_ONLY_CONST)
5181 && operand_equal_p (arg01,
5182 const_binop (PLUS_EXPR, arg2,
5183 build_int_cst (type, 1)),
5184 OEP_ONLY_CONST))
5186 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5187 fold_convert_loc (loc, TREE_TYPE (arg00),
5188 arg2));
5189 return pedantic_non_lvalue_loc (loc,
5190 fold_convert_loc (loc, type, tem));
5192 break;
5194 case LE_EXPR:
5195 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5196 as above. */
5197 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5198 OEP_ONLY_CONST)
5199 && operand_equal_p (arg01,
5200 const_binop (MINUS_EXPR, arg2,
5201 build_int_cst (type, 1)),
5202 OEP_ONLY_CONST))
5204 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5205 fold_convert_loc (loc, TREE_TYPE (arg00),
5206 arg2));
5207 return pedantic_non_lvalue_loc (loc,
5208 fold_convert_loc (loc, type, tem));
5210 break;
5212 case GT_EXPR:
5213 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5214 MAX_EXPR, to preserve the signedness of the comparison. */
5215 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5216 OEP_ONLY_CONST)
5217 && operand_equal_p (arg01,
5218 const_binop (MINUS_EXPR, arg2,
5219 build_int_cst (type, 1)),
5220 OEP_ONLY_CONST))
5222 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5223 fold_convert_loc (loc, TREE_TYPE (arg00),
5224 arg2));
5225 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5227 break;
5229 case GE_EXPR:
5230 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5231 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5232 OEP_ONLY_CONST)
5233 && operand_equal_p (arg01,
5234 const_binop (PLUS_EXPR, arg2,
5235 build_int_cst (type, 1)),
5236 OEP_ONLY_CONST))
5238 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5239 fold_convert_loc (loc, TREE_TYPE (arg00),
5240 arg2));
5241 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5243 break;
5244 case NE_EXPR:
5245 break;
5246 default:
5247 gcc_unreachable ();
5250 return NULL_TREE;
5255 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5256 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5257 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5258 false) >= 2)
5259 #endif
5261 /* EXP is some logical combination of boolean tests. See if we can
5262 merge it into some range test. Return the new tree if so. */
5264 static tree
5265 fold_range_test (location_t loc, enum tree_code code, tree type,
5266 tree op0, tree op1)
5268 int or_op = (code == TRUTH_ORIF_EXPR
5269 || code == TRUTH_OR_EXPR);
5270 int in0_p, in1_p, in_p;
5271 tree low0, low1, low, high0, high1, high;
5272 bool strict_overflow_p = false;
5273 tree tem, lhs, rhs;
5274 const char * const warnmsg = G_("assuming signed overflow does not occur "
5275 "when simplifying range test");
5277 if (!INTEGRAL_TYPE_P (type))
5278 return 0;
5280 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5281 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5283 /* If this is an OR operation, invert both sides; we will invert
5284 again at the end. */
5285 if (or_op)
5286 in0_p = ! in0_p, in1_p = ! in1_p;
5288 /* If both expressions are the same, if we can merge the ranges, and we
5289 can build the range test, return it or it inverted. If one of the
5290 ranges is always true or always false, consider it to be the same
5291 expression as the other. */
5292 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5293 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5294 in1_p, low1, high1)
5295 && 0 != (tem = (build_range_check (loc, type,
5296 lhs != 0 ? lhs
5297 : rhs != 0 ? rhs : integer_zero_node,
5298 in_p, low, high))))
5300 if (strict_overflow_p)
5301 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5302 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5305 /* On machines where the branch cost is expensive, if this is a
5306 short-circuited branch and the underlying object on both sides
5307 is the same, make a non-short-circuit operation. */
5308 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5309 && lhs != 0 && rhs != 0
5310 && (code == TRUTH_ANDIF_EXPR
5311 || code == TRUTH_ORIF_EXPR)
5312 && operand_equal_p (lhs, rhs, 0))
5314 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5315 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5316 which cases we can't do this. */
5317 if (simple_operand_p (lhs))
5318 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5319 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5320 type, op0, op1);
5322 else if (!lang_hooks.decls.global_bindings_p ()
5323 && !CONTAINS_PLACEHOLDER_P (lhs))
5325 tree common = save_expr (lhs);
5327 if (0 != (lhs = build_range_check (loc, type, common,
5328 or_op ? ! in0_p : in0_p,
5329 low0, high0))
5330 && (0 != (rhs = build_range_check (loc, type, common,
5331 or_op ? ! in1_p : in1_p,
5332 low1, high1))))
5334 if (strict_overflow_p)
5335 fold_overflow_warning (warnmsg,
5336 WARN_STRICT_OVERFLOW_COMPARISON);
5337 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5338 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5339 type, lhs, rhs);
5344 return 0;
5347 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5348 bit value. Arrange things so the extra bits will be set to zero if and
5349 only if C is signed-extended to its full width. If MASK is nonzero,
5350 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5352 static tree
5353 unextend (tree c, int p, int unsignedp, tree mask)
5355 tree type = TREE_TYPE (c);
5356 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5357 tree temp;
5359 if (p == modesize || unsignedp)
5360 return c;
5362 /* We work by getting just the sign bit into the low-order bit, then
5363 into the high-order bit, then sign-extend. We then XOR that value
5364 with C. */
5365 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5367 /* We must use a signed type in order to get an arithmetic right shift.
5368 However, we must also avoid introducing accidental overflows, so that
5369 a subsequent call to integer_zerop will work. Hence we must
5370 do the type conversion here. At this point, the constant is either
5371 zero or one, and the conversion to a signed type can never overflow.
5372 We could get an overflow if this conversion is done anywhere else. */
5373 if (TYPE_UNSIGNED (type))
5374 temp = fold_convert (signed_type_for (type), temp);
5376 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5377 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5378 if (mask != 0)
5379 temp = const_binop (BIT_AND_EXPR, temp,
5380 fold_convert (TREE_TYPE (c), mask));
5381 /* If necessary, convert the type back to match the type of C. */
5382 if (TYPE_UNSIGNED (type))
5383 temp = fold_convert (type, temp);
5385 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5388 /* For an expression that has the form
5389 (A && B) || ~B
5391 (A || B) && ~B,
5392 we can drop one of the inner expressions and simplify to
5393 A || ~B
5395 A && ~B
5396 LOC is the location of the resulting expression. OP is the inner
5397 logical operation; the left-hand side in the examples above, while CMPOP
5398 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5399 removing a condition that guards another, as in
5400 (A != NULL && A->...) || A == NULL
5401 which we must not transform. If RHS_ONLY is true, only eliminate the
5402 right-most operand of the inner logical operation. */
5404 static tree
5405 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5406 bool rhs_only)
5408 tree type = TREE_TYPE (cmpop);
5409 enum tree_code code = TREE_CODE (cmpop);
5410 enum tree_code truthop_code = TREE_CODE (op);
5411 tree lhs = TREE_OPERAND (op, 0);
5412 tree rhs = TREE_OPERAND (op, 1);
5413 tree orig_lhs = lhs, orig_rhs = rhs;
5414 enum tree_code rhs_code = TREE_CODE (rhs);
5415 enum tree_code lhs_code = TREE_CODE (lhs);
5416 enum tree_code inv_code;
5418 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5419 return NULL_TREE;
5421 if (TREE_CODE_CLASS (code) != tcc_comparison)
5422 return NULL_TREE;
5424 if (rhs_code == truthop_code)
5426 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5427 if (newrhs != NULL_TREE)
5429 rhs = newrhs;
5430 rhs_code = TREE_CODE (rhs);
5433 if (lhs_code == truthop_code && !rhs_only)
5435 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5436 if (newlhs != NULL_TREE)
5438 lhs = newlhs;
5439 lhs_code = TREE_CODE (lhs);
5443 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5444 if (inv_code == rhs_code
5445 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5446 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5447 return lhs;
5448 if (!rhs_only && inv_code == lhs_code
5449 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5450 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5451 return rhs;
5452 if (rhs != orig_rhs || lhs != orig_lhs)
5453 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5454 lhs, rhs);
5455 return NULL_TREE;
5458 /* Find ways of folding logical expressions of LHS and RHS:
5459 Try to merge two comparisons to the same innermost item.
5460 Look for range tests like "ch >= '0' && ch <= '9'".
5461 Look for combinations of simple terms on machines with expensive branches
5462 and evaluate the RHS unconditionally.
5464 For example, if we have p->a == 2 && p->b == 4 and we can make an
5465 object large enough to span both A and B, we can do this with a comparison
5466 against the object ANDed with the a mask.
5468 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5469 operations to do this with one comparison.
5471 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5472 function and the one above.
5474 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5475 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5477 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5478 two operands.
5480 We return the simplified tree or 0 if no optimization is possible. */
5482 static tree
5483 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5484 tree lhs, tree rhs)
5486 /* If this is the "or" of two comparisons, we can do something if
5487 the comparisons are NE_EXPR. If this is the "and", we can do something
5488 if the comparisons are EQ_EXPR. I.e.,
5489 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5491 WANTED_CODE is this operation code. For single bit fields, we can
5492 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5493 comparison for one-bit fields. */
5495 enum tree_code wanted_code;
5496 enum tree_code lcode, rcode;
5497 tree ll_arg, lr_arg, rl_arg, rr_arg;
5498 tree ll_inner, lr_inner, rl_inner, rr_inner;
5499 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5500 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5501 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5502 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5503 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5504 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5505 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5506 machine_mode lnmode, rnmode;
5507 tree ll_mask, lr_mask, rl_mask, rr_mask;
5508 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5509 tree l_const, r_const;
5510 tree lntype, rntype, result;
5511 HOST_WIDE_INT first_bit, end_bit;
5512 int volatilep;
5514 /* Start by getting the comparison codes. Fail if anything is volatile.
5515 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5516 it were surrounded with a NE_EXPR. */
5518 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5519 return 0;
5521 lcode = TREE_CODE (lhs);
5522 rcode = TREE_CODE (rhs);
5524 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5526 lhs = build2 (NE_EXPR, truth_type, lhs,
5527 build_int_cst (TREE_TYPE (lhs), 0));
5528 lcode = NE_EXPR;
5531 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5533 rhs = build2 (NE_EXPR, truth_type, rhs,
5534 build_int_cst (TREE_TYPE (rhs), 0));
5535 rcode = NE_EXPR;
5538 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5539 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5540 return 0;
5542 ll_arg = TREE_OPERAND (lhs, 0);
5543 lr_arg = TREE_OPERAND (lhs, 1);
5544 rl_arg = TREE_OPERAND (rhs, 0);
5545 rr_arg = TREE_OPERAND (rhs, 1);
5547 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5548 if (simple_operand_p (ll_arg)
5549 && simple_operand_p (lr_arg))
5551 if (operand_equal_p (ll_arg, rl_arg, 0)
5552 && operand_equal_p (lr_arg, rr_arg, 0))
5554 result = combine_comparisons (loc, code, lcode, rcode,
5555 truth_type, ll_arg, lr_arg);
5556 if (result)
5557 return result;
5559 else if (operand_equal_p (ll_arg, rr_arg, 0)
5560 && operand_equal_p (lr_arg, rl_arg, 0))
5562 result = combine_comparisons (loc, code, lcode,
5563 swap_tree_comparison (rcode),
5564 truth_type, ll_arg, lr_arg);
5565 if (result)
5566 return result;
5570 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5571 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5573 /* If the RHS can be evaluated unconditionally and its operands are
5574 simple, it wins to evaluate the RHS unconditionally on machines
5575 with expensive branches. In this case, this isn't a comparison
5576 that can be merged. */
5578 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5579 false) >= 2
5580 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5581 && simple_operand_p (rl_arg)
5582 && simple_operand_p (rr_arg))
5584 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5585 if (code == TRUTH_OR_EXPR
5586 && lcode == NE_EXPR && integer_zerop (lr_arg)
5587 && rcode == NE_EXPR && integer_zerop (rr_arg)
5588 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5589 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5590 return build2_loc (loc, NE_EXPR, truth_type,
5591 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5592 ll_arg, rl_arg),
5593 build_int_cst (TREE_TYPE (ll_arg), 0));
5595 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5596 if (code == TRUTH_AND_EXPR
5597 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5598 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5599 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5600 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5601 return build2_loc (loc, EQ_EXPR, truth_type,
5602 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5603 ll_arg, rl_arg),
5604 build_int_cst (TREE_TYPE (ll_arg), 0));
5607 /* See if the comparisons can be merged. Then get all the parameters for
5608 each side. */
5610 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5611 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5612 return 0;
5614 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5615 volatilep = 0;
5616 ll_inner = decode_field_reference (loc, ll_arg,
5617 &ll_bitsize, &ll_bitpos, &ll_mode,
5618 &ll_unsignedp, &ll_reversep, &volatilep,
5619 &ll_mask, &ll_and_mask);
5620 lr_inner = decode_field_reference (loc, lr_arg,
5621 &lr_bitsize, &lr_bitpos, &lr_mode,
5622 &lr_unsignedp, &lr_reversep, &volatilep,
5623 &lr_mask, &lr_and_mask);
5624 rl_inner = decode_field_reference (loc, rl_arg,
5625 &rl_bitsize, &rl_bitpos, &rl_mode,
5626 &rl_unsignedp, &rl_reversep, &volatilep,
5627 &rl_mask, &rl_and_mask);
5628 rr_inner = decode_field_reference (loc, rr_arg,
5629 &rr_bitsize, &rr_bitpos, &rr_mode,
5630 &rr_unsignedp, &rr_reversep, &volatilep,
5631 &rr_mask, &rr_and_mask);
5633 /* It must be true that the inner operation on the lhs of each
5634 comparison must be the same if we are to be able to do anything.
5635 Then see if we have constants. If not, the same must be true for
5636 the rhs's. */
5637 if (volatilep
5638 || ll_reversep != rl_reversep
5639 || ll_inner == 0 || rl_inner == 0
5640 || ! operand_equal_p (ll_inner, rl_inner, 0))
5641 return 0;
5643 if (TREE_CODE (lr_arg) == INTEGER_CST
5644 && TREE_CODE (rr_arg) == INTEGER_CST)
5646 l_const = lr_arg, r_const = rr_arg;
5647 lr_reversep = ll_reversep;
5649 else if (lr_reversep != rr_reversep
5650 || lr_inner == 0 || rr_inner == 0
5651 || ! operand_equal_p (lr_inner, rr_inner, 0))
5652 return 0;
5653 else
5654 l_const = r_const = 0;
5656 /* If either comparison code is not correct for our logical operation,
5657 fail. However, we can convert a one-bit comparison against zero into
5658 the opposite comparison against that bit being set in the field. */
5660 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5661 if (lcode != wanted_code)
5663 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5665 /* Make the left operand unsigned, since we are only interested
5666 in the value of one bit. Otherwise we are doing the wrong
5667 thing below. */
5668 ll_unsignedp = 1;
5669 l_const = ll_mask;
5671 else
5672 return 0;
5675 /* This is analogous to the code for l_const above. */
5676 if (rcode != wanted_code)
5678 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5680 rl_unsignedp = 1;
5681 r_const = rl_mask;
5683 else
5684 return 0;
5687 /* See if we can find a mode that contains both fields being compared on
5688 the left. If we can't, fail. Otherwise, update all constants and masks
5689 to be relative to a field of that size. */
5690 first_bit = MIN (ll_bitpos, rl_bitpos);
5691 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5692 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5693 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5694 volatilep);
5695 if (lnmode == VOIDmode)
5696 return 0;
5698 lnbitsize = GET_MODE_BITSIZE (lnmode);
5699 lnbitpos = first_bit & ~ (lnbitsize - 1);
5700 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5701 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5703 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5705 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5706 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5709 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5710 size_int (xll_bitpos));
5711 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5712 size_int (xrl_bitpos));
5714 if (l_const)
5716 l_const = fold_convert_loc (loc, lntype, l_const);
5717 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5718 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5719 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5720 fold_build1_loc (loc, BIT_NOT_EXPR,
5721 lntype, ll_mask))))
5723 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5725 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5728 if (r_const)
5730 r_const = fold_convert_loc (loc, lntype, r_const);
5731 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5732 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5733 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5734 fold_build1_loc (loc, BIT_NOT_EXPR,
5735 lntype, rl_mask))))
5737 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5739 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5743 /* If the right sides are not constant, do the same for it. Also,
5744 disallow this optimization if a size or signedness mismatch occurs
5745 between the left and right sides. */
5746 if (l_const == 0)
5748 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5749 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5750 /* Make sure the two fields on the right
5751 correspond to the left without being swapped. */
5752 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5753 return 0;
5755 first_bit = MIN (lr_bitpos, rr_bitpos);
5756 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5757 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5758 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5759 volatilep);
5760 if (rnmode == VOIDmode)
5761 return 0;
5763 rnbitsize = GET_MODE_BITSIZE (rnmode);
5764 rnbitpos = first_bit & ~ (rnbitsize - 1);
5765 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5766 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5768 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5770 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5771 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5774 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5775 rntype, lr_mask),
5776 size_int (xlr_bitpos));
5777 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5778 rntype, rr_mask),
5779 size_int (xrr_bitpos));
5781 /* Make a mask that corresponds to both fields being compared.
5782 Do this for both items being compared. If the operands are the
5783 same size and the bits being compared are in the same position
5784 then we can do this by masking both and comparing the masked
5785 results. */
5786 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5787 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5788 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5790 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5791 ll_unsignedp || rl_unsignedp, ll_reversep);
5792 if (! all_ones_mask_p (ll_mask, lnbitsize))
5793 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5795 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5796 lr_unsignedp || rr_unsignedp, lr_reversep);
5797 if (! all_ones_mask_p (lr_mask, rnbitsize))
5798 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5800 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5803 /* There is still another way we can do something: If both pairs of
5804 fields being compared are adjacent, we may be able to make a wider
5805 field containing them both.
5807 Note that we still must mask the lhs/rhs expressions. Furthermore,
5808 the mask must be shifted to account for the shift done by
5809 make_bit_field_ref. */
5810 if ((ll_bitsize + ll_bitpos == rl_bitpos
5811 && lr_bitsize + lr_bitpos == rr_bitpos)
5812 || (ll_bitpos == rl_bitpos + rl_bitsize
5813 && lr_bitpos == rr_bitpos + rr_bitsize))
5815 tree type;
5817 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5818 ll_bitsize + rl_bitsize,
5819 MIN (ll_bitpos, rl_bitpos),
5820 ll_unsignedp, ll_reversep);
5821 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5822 lr_bitsize + rr_bitsize,
5823 MIN (lr_bitpos, rr_bitpos),
5824 lr_unsignedp, lr_reversep);
5826 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5827 size_int (MIN (xll_bitpos, xrl_bitpos)));
5828 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5829 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5831 /* Convert to the smaller type before masking out unwanted bits. */
5832 type = lntype;
5833 if (lntype != rntype)
5835 if (lnbitsize > rnbitsize)
5837 lhs = fold_convert_loc (loc, rntype, lhs);
5838 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5839 type = rntype;
5841 else if (lnbitsize < rnbitsize)
5843 rhs = fold_convert_loc (loc, lntype, rhs);
5844 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5845 type = lntype;
5849 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5850 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5852 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5853 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5855 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5858 return 0;
5861 /* Handle the case of comparisons with constants. If there is something in
5862 common between the masks, those bits of the constants must be the same.
5863 If not, the condition is always false. Test for this to avoid generating
5864 incorrect code below. */
5865 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5866 if (! integer_zerop (result)
5867 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5868 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5870 if (wanted_code == NE_EXPR)
5872 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5873 return constant_boolean_node (true, truth_type);
5875 else
5877 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5878 return constant_boolean_node (false, truth_type);
5882 /* Construct the expression we will return. First get the component
5883 reference we will make. Unless the mask is all ones the width of
5884 that field, perform the mask operation. Then compare with the
5885 merged constant. */
5886 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5887 ll_unsignedp || rl_unsignedp, ll_reversep);
5889 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5890 if (! all_ones_mask_p (ll_mask, lnbitsize))
5891 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5893 return build2_loc (loc, wanted_code, truth_type, result,
5894 const_binop (BIT_IOR_EXPR, l_const, r_const));
5897 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5898 constant. */
5900 static tree
5901 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5902 tree op0, tree op1)
5904 tree arg0 = op0;
5905 enum tree_code op_code;
5906 tree comp_const;
5907 tree minmax_const;
5908 int consts_equal, consts_lt;
5909 tree inner;
5911 STRIP_SIGN_NOPS (arg0);
5913 op_code = TREE_CODE (arg0);
5914 minmax_const = TREE_OPERAND (arg0, 1);
5915 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5916 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5917 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5918 inner = TREE_OPERAND (arg0, 0);
5920 /* If something does not permit us to optimize, return the original tree. */
5921 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5922 || TREE_CODE (comp_const) != INTEGER_CST
5923 || TREE_OVERFLOW (comp_const)
5924 || TREE_CODE (minmax_const) != INTEGER_CST
5925 || TREE_OVERFLOW (minmax_const))
5926 return NULL_TREE;
5928 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5929 and GT_EXPR, doing the rest with recursive calls using logical
5930 simplifications. */
5931 switch (code)
5933 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5935 tree tem
5936 = optimize_minmax_comparison (loc,
5937 invert_tree_comparison (code, false),
5938 type, op0, op1);
5939 if (tem)
5940 return invert_truthvalue_loc (loc, tem);
5941 return NULL_TREE;
5944 case GE_EXPR:
5945 return
5946 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5947 optimize_minmax_comparison
5948 (loc, EQ_EXPR, type, arg0, comp_const),
5949 optimize_minmax_comparison
5950 (loc, GT_EXPR, type, arg0, comp_const));
5952 case EQ_EXPR:
5953 if (op_code == MAX_EXPR && consts_equal)
5954 /* MAX (X, 0) == 0 -> X <= 0 */
5955 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5957 else if (op_code == MAX_EXPR && consts_lt)
5958 /* MAX (X, 0) == 5 -> X == 5 */
5959 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5961 else if (op_code == MAX_EXPR)
5962 /* MAX (X, 0) == -1 -> false */
5963 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5965 else if (consts_equal)
5966 /* MIN (X, 0) == 0 -> X >= 0 */
5967 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5969 else if (consts_lt)
5970 /* MIN (X, 0) == 5 -> false */
5971 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5973 else
5974 /* MIN (X, 0) == -1 -> X == -1 */
5975 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5977 case GT_EXPR:
5978 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5979 /* MAX (X, 0) > 0 -> X > 0
5980 MAX (X, 0) > 5 -> X > 5 */
5981 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5983 else if (op_code == MAX_EXPR)
5984 /* MAX (X, 0) > -1 -> true */
5985 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5987 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5988 /* MIN (X, 0) > 0 -> false
5989 MIN (X, 0) > 5 -> false */
5990 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5992 else
5993 /* MIN (X, 0) > -1 -> X > -1 */
5994 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5996 default:
5997 return NULL_TREE;
6001 /* T is an integer expression that is being multiplied, divided, or taken a
6002 modulus (CODE says which and what kind of divide or modulus) by a
6003 constant C. See if we can eliminate that operation by folding it with
6004 other operations already in T. WIDE_TYPE, if non-null, is a type that
6005 should be used for the computation if wider than our type.
6007 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6008 (X * 2) + (Y * 4). We must, however, be assured that either the original
6009 expression would not overflow or that overflow is undefined for the type
6010 in the language in question.
6012 If we return a non-null expression, it is an equivalent form of the
6013 original computation, but need not be in the original type.
6015 We set *STRICT_OVERFLOW_P to true if the return values depends on
6016 signed overflow being undefined. Otherwise we do not change
6017 *STRICT_OVERFLOW_P. */
6019 static tree
6020 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6021 bool *strict_overflow_p)
6023 /* To avoid exponential search depth, refuse to allow recursion past
6024 three levels. Beyond that (1) it's highly unlikely that we'll find
6025 something interesting and (2) we've probably processed it before
6026 when we built the inner expression. */
6028 static int depth;
6029 tree ret;
6031 if (depth > 3)
6032 return NULL;
6034 depth++;
6035 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6036 depth--;
6038 return ret;
6041 static tree
6042 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6043 bool *strict_overflow_p)
6045 tree type = TREE_TYPE (t);
6046 enum tree_code tcode = TREE_CODE (t);
6047 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6048 > GET_MODE_SIZE (TYPE_MODE (type)))
6049 ? wide_type : type);
6050 tree t1, t2;
6051 int same_p = tcode == code;
6052 tree op0 = NULL_TREE, op1 = NULL_TREE;
6053 bool sub_strict_overflow_p;
6055 /* Don't deal with constants of zero here; they confuse the code below. */
6056 if (integer_zerop (c))
6057 return NULL_TREE;
6059 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6060 op0 = TREE_OPERAND (t, 0);
6062 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6063 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6065 /* Note that we need not handle conditional operations here since fold
6066 already handles those cases. So just do arithmetic here. */
6067 switch (tcode)
6069 case INTEGER_CST:
6070 /* For a constant, we can always simplify if we are a multiply
6071 or (for divide and modulus) if it is a multiple of our constant. */
6072 if (code == MULT_EXPR
6073 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
6075 tree tem = const_binop (code, fold_convert (ctype, t),
6076 fold_convert (ctype, c));
6077 /* If the multiplication overflowed to INT_MIN then we lost sign
6078 information on it and a subsequent multiplication might
6079 spuriously overflow. See PR68142. */
6080 if (TREE_OVERFLOW (tem)
6081 && wi::eq_p (tem, wi::min_value (TYPE_PRECISION (ctype), SIGNED)))
6082 return NULL_TREE;
6083 return tem;
6085 break;
6087 CASE_CONVERT: case NON_LVALUE_EXPR:
6088 /* If op0 is an expression ... */
6089 if ((COMPARISON_CLASS_P (op0)
6090 || UNARY_CLASS_P (op0)
6091 || BINARY_CLASS_P (op0)
6092 || VL_EXP_CLASS_P (op0)
6093 || EXPRESSION_CLASS_P (op0))
6094 /* ... and has wrapping overflow, and its type is smaller
6095 than ctype, then we cannot pass through as widening. */
6096 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6097 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6098 && (TYPE_PRECISION (ctype)
6099 > TYPE_PRECISION (TREE_TYPE (op0))))
6100 /* ... or this is a truncation (t is narrower than op0),
6101 then we cannot pass through this narrowing. */
6102 || (TYPE_PRECISION (type)
6103 < TYPE_PRECISION (TREE_TYPE (op0)))
6104 /* ... or signedness changes for division or modulus,
6105 then we cannot pass through this conversion. */
6106 || (code != MULT_EXPR
6107 && (TYPE_UNSIGNED (ctype)
6108 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6109 /* ... or has undefined overflow while the converted to
6110 type has not, we cannot do the operation in the inner type
6111 as that would introduce undefined overflow. */
6112 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6113 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6114 && !TYPE_OVERFLOW_UNDEFINED (type))))
6115 break;
6117 /* Pass the constant down and see if we can make a simplification. If
6118 we can, replace this expression with the inner simplification for
6119 possible later conversion to our or some other type. */
6120 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6121 && TREE_CODE (t2) == INTEGER_CST
6122 && !TREE_OVERFLOW (t2)
6123 && (0 != (t1 = extract_muldiv (op0, t2, code,
6124 code == MULT_EXPR
6125 ? ctype : NULL_TREE,
6126 strict_overflow_p))))
6127 return t1;
6128 break;
6130 case ABS_EXPR:
6131 /* If widening the type changes it from signed to unsigned, then we
6132 must avoid building ABS_EXPR itself as unsigned. */
6133 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6135 tree cstype = (*signed_type_for) (ctype);
6136 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6137 != 0)
6139 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6140 return fold_convert (ctype, t1);
6142 break;
6144 /* If the constant is negative, we cannot simplify this. */
6145 if (tree_int_cst_sgn (c) == -1)
6146 break;
6147 /* FALLTHROUGH */
6148 case NEGATE_EXPR:
6149 /* For division and modulus, type can't be unsigned, as e.g.
6150 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6151 For signed types, even with wrapping overflow, this is fine. */
6152 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6153 break;
6154 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6155 != 0)
6156 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6157 break;
6159 case MIN_EXPR: case MAX_EXPR:
6160 /* If widening the type changes the signedness, then we can't perform
6161 this optimization as that changes the result. */
6162 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6163 break;
6165 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6166 sub_strict_overflow_p = false;
6167 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6168 &sub_strict_overflow_p)) != 0
6169 && (t2 = extract_muldiv (op1, c, code, wide_type,
6170 &sub_strict_overflow_p)) != 0)
6172 if (tree_int_cst_sgn (c) < 0)
6173 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6174 if (sub_strict_overflow_p)
6175 *strict_overflow_p = true;
6176 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6177 fold_convert (ctype, t2));
6179 break;
6181 case LSHIFT_EXPR: case RSHIFT_EXPR:
6182 /* If the second operand is constant, this is a multiplication
6183 or floor division, by a power of two, so we can treat it that
6184 way unless the multiplier or divisor overflows. Signed
6185 left-shift overflow is implementation-defined rather than
6186 undefined in C90, so do not convert signed left shift into
6187 multiplication. */
6188 if (TREE_CODE (op1) == INTEGER_CST
6189 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6190 /* const_binop may not detect overflow correctly,
6191 so check for it explicitly here. */
6192 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6193 && 0 != (t1 = fold_convert (ctype,
6194 const_binop (LSHIFT_EXPR,
6195 size_one_node,
6196 op1)))
6197 && !TREE_OVERFLOW (t1))
6198 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6199 ? MULT_EXPR : FLOOR_DIV_EXPR,
6200 ctype,
6201 fold_convert (ctype, op0),
6202 t1),
6203 c, code, wide_type, strict_overflow_p);
6204 break;
6206 case PLUS_EXPR: case MINUS_EXPR:
6207 /* See if we can eliminate the operation on both sides. If we can, we
6208 can return a new PLUS or MINUS. If we can't, the only remaining
6209 cases where we can do anything are if the second operand is a
6210 constant. */
6211 sub_strict_overflow_p = false;
6212 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6213 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6214 if (t1 != 0 && t2 != 0
6215 && (code == MULT_EXPR
6216 /* If not multiplication, we can only do this if both operands
6217 are divisible by c. */
6218 || (multiple_of_p (ctype, op0, c)
6219 && multiple_of_p (ctype, op1, c))))
6221 if (sub_strict_overflow_p)
6222 *strict_overflow_p = true;
6223 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6224 fold_convert (ctype, t2));
6227 /* If this was a subtraction, negate OP1 and set it to be an addition.
6228 This simplifies the logic below. */
6229 if (tcode == MINUS_EXPR)
6231 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6232 /* If OP1 was not easily negatable, the constant may be OP0. */
6233 if (TREE_CODE (op0) == INTEGER_CST)
6235 std::swap (op0, op1);
6236 std::swap (t1, t2);
6240 if (TREE_CODE (op1) != INTEGER_CST)
6241 break;
6243 /* If either OP1 or C are negative, this optimization is not safe for
6244 some of the division and remainder types while for others we need
6245 to change the code. */
6246 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6248 if (code == CEIL_DIV_EXPR)
6249 code = FLOOR_DIV_EXPR;
6250 else if (code == FLOOR_DIV_EXPR)
6251 code = CEIL_DIV_EXPR;
6252 else if (code != MULT_EXPR
6253 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6254 break;
6257 /* If it's a multiply or a division/modulus operation of a multiple
6258 of our constant, do the operation and verify it doesn't overflow. */
6259 if (code == MULT_EXPR
6260 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6262 op1 = const_binop (code, fold_convert (ctype, op1),
6263 fold_convert (ctype, c));
6264 /* We allow the constant to overflow with wrapping semantics. */
6265 if (op1 == 0
6266 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6267 break;
6269 else
6270 break;
6272 /* If we have an unsigned type, we cannot widen the operation since it
6273 will change the result if the original computation overflowed. */
6274 if (TYPE_UNSIGNED (ctype) && ctype != type)
6275 break;
6277 /* If we were able to eliminate our operation from the first side,
6278 apply our operation to the second side and reform the PLUS. */
6279 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6280 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6282 /* The last case is if we are a multiply. In that case, we can
6283 apply the distributive law to commute the multiply and addition
6284 if the multiplication of the constants doesn't overflow
6285 and overflow is defined. With undefined overflow
6286 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6287 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6288 return fold_build2 (tcode, ctype,
6289 fold_build2 (code, ctype,
6290 fold_convert (ctype, op0),
6291 fold_convert (ctype, c)),
6292 op1);
6294 break;
6296 case MULT_EXPR:
6297 /* We have a special case here if we are doing something like
6298 (C * 8) % 4 since we know that's zero. */
6299 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6300 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6301 /* If the multiplication can overflow we cannot optimize this. */
6302 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6303 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6304 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6306 *strict_overflow_p = true;
6307 return omit_one_operand (type, integer_zero_node, op0);
6310 /* ... fall through ... */
6312 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6313 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6314 /* If we can extract our operation from the LHS, do so and return a
6315 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6316 do something only if the second operand is a constant. */
6317 if (same_p
6318 && (t1 = extract_muldiv (op0, c, code, wide_type,
6319 strict_overflow_p)) != 0)
6320 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6321 fold_convert (ctype, op1));
6322 else if (tcode == MULT_EXPR && code == MULT_EXPR
6323 && (t1 = extract_muldiv (op1, c, code, wide_type,
6324 strict_overflow_p)) != 0)
6325 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6326 fold_convert (ctype, t1));
6327 else if (TREE_CODE (op1) != INTEGER_CST)
6328 return 0;
6330 /* If these are the same operation types, we can associate them
6331 assuming no overflow. */
6332 if (tcode == code)
6334 bool overflow_p = false;
6335 bool overflow_mul_p;
6336 signop sign = TYPE_SIGN (ctype);
6337 wide_int mul = wi::mul (op1, c, sign, &overflow_mul_p);
6338 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6339 if (overflow_mul_p
6340 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6341 overflow_p = true;
6342 if (!overflow_p)
6344 mul = wide_int::from (mul, TYPE_PRECISION (ctype),
6345 TYPE_SIGN (TREE_TYPE (op1)));
6346 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6347 wide_int_to_tree (ctype, mul));
6351 /* If these operations "cancel" each other, we have the main
6352 optimizations of this pass, which occur when either constant is a
6353 multiple of the other, in which case we replace this with either an
6354 operation or CODE or TCODE.
6356 If we have an unsigned type, we cannot do this since it will change
6357 the result if the original computation overflowed. */
6358 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6359 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6360 || (tcode == MULT_EXPR
6361 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6362 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6363 && code != MULT_EXPR)))
6365 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6367 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6368 *strict_overflow_p = true;
6369 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6370 fold_convert (ctype,
6371 const_binop (TRUNC_DIV_EXPR,
6372 op1, c)));
6374 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6376 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6377 *strict_overflow_p = true;
6378 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6379 fold_convert (ctype,
6380 const_binop (TRUNC_DIV_EXPR,
6381 c, op1)));
6384 break;
6386 default:
6387 break;
6390 return 0;
6393 /* Return a node which has the indicated constant VALUE (either 0 or
6394 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6395 and is of the indicated TYPE. */
6397 tree
6398 constant_boolean_node (bool value, tree type)
6400 if (type == integer_type_node)
6401 return value ? integer_one_node : integer_zero_node;
6402 else if (type == boolean_type_node)
6403 return value ? boolean_true_node : boolean_false_node;
6404 else if (TREE_CODE (type) == VECTOR_TYPE)
6405 return build_vector_from_val (type,
6406 build_int_cst (TREE_TYPE (type),
6407 value ? -1 : 0));
6408 else
6409 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6413 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6414 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6415 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6416 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6417 COND is the first argument to CODE; otherwise (as in the example
6418 given here), it is the second argument. TYPE is the type of the
6419 original expression. Return NULL_TREE if no simplification is
6420 possible. */
6422 static tree
6423 fold_binary_op_with_conditional_arg (location_t loc,
6424 enum tree_code code,
6425 tree type, tree op0, tree op1,
6426 tree cond, tree arg, int cond_first_p)
6428 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6429 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6430 tree test, true_value, false_value;
6431 tree lhs = NULL_TREE;
6432 tree rhs = NULL_TREE;
6433 enum tree_code cond_code = COND_EXPR;
6435 if (TREE_CODE (cond) == COND_EXPR
6436 || TREE_CODE (cond) == VEC_COND_EXPR)
6438 test = TREE_OPERAND (cond, 0);
6439 true_value = TREE_OPERAND (cond, 1);
6440 false_value = TREE_OPERAND (cond, 2);
6441 /* If this operand throws an expression, then it does not make
6442 sense to try to perform a logical or arithmetic operation
6443 involving it. */
6444 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6445 lhs = true_value;
6446 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6447 rhs = false_value;
6449 else if (!(TREE_CODE (type) != VECTOR_TYPE
6450 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6452 tree testtype = TREE_TYPE (cond);
6453 test = cond;
6454 true_value = constant_boolean_node (true, testtype);
6455 false_value = constant_boolean_node (false, testtype);
6457 else
6458 /* Detect the case of mixing vector and scalar types - bail out. */
6459 return NULL_TREE;
6461 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6462 cond_code = VEC_COND_EXPR;
6464 /* This transformation is only worthwhile if we don't have to wrap ARG
6465 in a SAVE_EXPR and the operation can be simplified without recursing
6466 on at least one of the branches once its pushed inside the COND_EXPR. */
6467 if (!TREE_CONSTANT (arg)
6468 && (TREE_SIDE_EFFECTS (arg)
6469 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6470 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6471 return NULL_TREE;
6473 arg = fold_convert_loc (loc, arg_type, arg);
6474 if (lhs == 0)
6476 true_value = fold_convert_loc (loc, cond_type, true_value);
6477 if (cond_first_p)
6478 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6479 else
6480 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6482 if (rhs == 0)
6484 false_value = fold_convert_loc (loc, cond_type, false_value);
6485 if (cond_first_p)
6486 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6487 else
6488 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6491 /* Check that we have simplified at least one of the branches. */
6492 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6493 return NULL_TREE;
6495 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6499 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6501 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6502 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6503 ADDEND is the same as X.
6505 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6506 and finite. The problematic cases are when X is zero, and its mode
6507 has signed zeros. In the case of rounding towards -infinity,
6508 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6509 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6511 bool
6512 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6514 if (!real_zerop (addend))
6515 return false;
6517 /* Don't allow the fold with -fsignaling-nans. */
6518 if (HONOR_SNANS (element_mode (type)))
6519 return false;
6521 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6522 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6523 return true;
6525 /* In a vector or complex, we would need to check the sign of all zeros. */
6526 if (TREE_CODE (addend) != REAL_CST)
6527 return false;
6529 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6530 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6531 negate = !negate;
6533 /* The mode has signed zeros, and we have to honor their sign.
6534 In this situation, there is only one case we can return true for.
6535 X - 0 is the same as X unless rounding towards -infinity is
6536 supported. */
6537 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6540 /* Subroutine of fold() that optimizes comparisons of a division by
6541 a nonzero integer constant against an integer constant, i.e.
6542 X/C1 op C2.
6544 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6545 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6546 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6548 The function returns the constant folded tree if a simplification
6549 can be made, and NULL_TREE otherwise. */
6551 static tree
6552 fold_div_compare (location_t loc,
6553 enum tree_code code, tree type, tree arg0, tree arg1)
6555 tree prod, tmp, hi, lo;
6556 tree arg00 = TREE_OPERAND (arg0, 0);
6557 tree arg01 = TREE_OPERAND (arg0, 1);
6558 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6559 bool neg_overflow = false;
6560 bool overflow;
6562 /* We have to do this the hard way to detect unsigned overflow.
6563 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6564 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6565 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6566 neg_overflow = false;
6568 if (sign == UNSIGNED)
6570 tmp = int_const_binop (MINUS_EXPR, arg01,
6571 build_int_cst (TREE_TYPE (arg01), 1));
6572 lo = prod;
6574 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6575 val = wi::add (prod, tmp, sign, &overflow);
6576 hi = force_fit_type (TREE_TYPE (arg00), val,
6577 -1, overflow | TREE_OVERFLOW (prod));
6579 else if (tree_int_cst_sgn (arg01) >= 0)
6581 tmp = int_const_binop (MINUS_EXPR, arg01,
6582 build_int_cst (TREE_TYPE (arg01), 1));
6583 switch (tree_int_cst_sgn (arg1))
6585 case -1:
6586 neg_overflow = true;
6587 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6588 hi = prod;
6589 break;
6591 case 0:
6592 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6593 hi = tmp;
6594 break;
6596 case 1:
6597 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6598 lo = prod;
6599 break;
6601 default:
6602 gcc_unreachable ();
6605 else
6607 /* A negative divisor reverses the relational operators. */
6608 code = swap_tree_comparison (code);
6610 tmp = int_const_binop (PLUS_EXPR, arg01,
6611 build_int_cst (TREE_TYPE (arg01), 1));
6612 switch (tree_int_cst_sgn (arg1))
6614 case -1:
6615 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6616 lo = prod;
6617 break;
6619 case 0:
6620 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6621 lo = tmp;
6622 break;
6624 case 1:
6625 neg_overflow = true;
6626 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6627 hi = prod;
6628 break;
6630 default:
6631 gcc_unreachable ();
6635 switch (code)
6637 case EQ_EXPR:
6638 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6639 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6640 if (TREE_OVERFLOW (hi))
6641 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6642 if (TREE_OVERFLOW (lo))
6643 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6644 return build_range_check (loc, type, arg00, 1, lo, hi);
6646 case NE_EXPR:
6647 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6648 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6649 if (TREE_OVERFLOW (hi))
6650 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6651 if (TREE_OVERFLOW (lo))
6652 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6653 return build_range_check (loc, type, arg00, 0, lo, hi);
6655 case LT_EXPR:
6656 if (TREE_OVERFLOW (lo))
6658 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6659 return omit_one_operand_loc (loc, type, tmp, arg00);
6661 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6663 case LE_EXPR:
6664 if (TREE_OVERFLOW (hi))
6666 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6667 return omit_one_operand_loc (loc, type, tmp, arg00);
6669 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6671 case GT_EXPR:
6672 if (TREE_OVERFLOW (hi))
6674 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6675 return omit_one_operand_loc (loc, type, tmp, arg00);
6677 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6679 case GE_EXPR:
6680 if (TREE_OVERFLOW (lo))
6682 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6683 return omit_one_operand_loc (loc, type, tmp, arg00);
6685 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6687 default:
6688 break;
6691 return NULL_TREE;
6695 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6696 equality/inequality test, then return a simplified form of the test
6697 using a sign testing. Otherwise return NULL. TYPE is the desired
6698 result type. */
6700 static tree
6701 fold_single_bit_test_into_sign_test (location_t loc,
6702 enum tree_code code, tree arg0, tree arg1,
6703 tree result_type)
6705 /* If this is testing a single bit, we can optimize the test. */
6706 if ((code == NE_EXPR || code == EQ_EXPR)
6707 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6708 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6710 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6711 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6712 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6714 if (arg00 != NULL_TREE
6715 /* This is only a win if casting to a signed type is cheap,
6716 i.e. when arg00's type is not a partial mode. */
6717 && TYPE_PRECISION (TREE_TYPE (arg00))
6718 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6720 tree stype = signed_type_for (TREE_TYPE (arg00));
6721 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6722 result_type,
6723 fold_convert_loc (loc, stype, arg00),
6724 build_int_cst (stype, 0));
6728 return NULL_TREE;
6731 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6732 equality/inequality test, then return a simplified form of
6733 the test using shifts and logical operations. Otherwise return
6734 NULL. TYPE is the desired result type. */
6736 tree
6737 fold_single_bit_test (location_t loc, enum tree_code code,
6738 tree arg0, tree arg1, tree result_type)
6740 /* If this is testing a single bit, we can optimize the test. */
6741 if ((code == NE_EXPR || code == EQ_EXPR)
6742 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6743 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6745 tree inner = TREE_OPERAND (arg0, 0);
6746 tree type = TREE_TYPE (arg0);
6747 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6748 machine_mode operand_mode = TYPE_MODE (type);
6749 int ops_unsigned;
6750 tree signed_type, unsigned_type, intermediate_type;
6751 tree tem, one;
6753 /* First, see if we can fold the single bit test into a sign-bit
6754 test. */
6755 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6756 result_type);
6757 if (tem)
6758 return tem;
6760 /* Otherwise we have (A & C) != 0 where C is a single bit,
6761 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6762 Similarly for (A & C) == 0. */
6764 /* If INNER is a right shift of a constant and it plus BITNUM does
6765 not overflow, adjust BITNUM and INNER. */
6766 if (TREE_CODE (inner) == RSHIFT_EXPR
6767 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6768 && bitnum < TYPE_PRECISION (type)
6769 && wi::ltu_p (TREE_OPERAND (inner, 1),
6770 TYPE_PRECISION (type) - bitnum))
6772 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6773 inner = TREE_OPERAND (inner, 0);
6776 /* If we are going to be able to omit the AND below, we must do our
6777 operations as unsigned. If we must use the AND, we have a choice.
6778 Normally unsigned is faster, but for some machines signed is. */
6779 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6780 && !flag_syntax_only) ? 0 : 1;
6782 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6783 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6784 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6785 inner = fold_convert_loc (loc, intermediate_type, inner);
6787 if (bitnum != 0)
6788 inner = build2 (RSHIFT_EXPR, intermediate_type,
6789 inner, size_int (bitnum));
6791 one = build_int_cst (intermediate_type, 1);
6793 if (code == EQ_EXPR)
6794 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6796 /* Put the AND last so it can combine with more things. */
6797 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6799 /* Make sure to return the proper type. */
6800 inner = fold_convert_loc (loc, result_type, inner);
6802 return inner;
6804 return NULL_TREE;
6807 /* Check whether we are allowed to reorder operands arg0 and arg1,
6808 such that the evaluation of arg1 occurs before arg0. */
6810 static bool
6811 reorder_operands_p (const_tree arg0, const_tree arg1)
6813 if (! flag_evaluation_order)
6814 return true;
6815 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6816 return true;
6817 return ! TREE_SIDE_EFFECTS (arg0)
6818 && ! TREE_SIDE_EFFECTS (arg1);
6821 /* Test whether it is preferable two swap two operands, ARG0 and
6822 ARG1, for example because ARG0 is an integer constant and ARG1
6823 isn't. If REORDER is true, only recommend swapping if we can
6824 evaluate the operands in reverse order. */
6826 bool
6827 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6829 if (CONSTANT_CLASS_P (arg1))
6830 return 0;
6831 if (CONSTANT_CLASS_P (arg0))
6832 return 1;
6834 STRIP_NOPS (arg0);
6835 STRIP_NOPS (arg1);
6837 if (TREE_CONSTANT (arg1))
6838 return 0;
6839 if (TREE_CONSTANT (arg0))
6840 return 1;
6842 if (reorder && flag_evaluation_order
6843 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6844 return 0;
6846 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6847 for commutative and comparison operators. Ensuring a canonical
6848 form allows the optimizers to find additional redundancies without
6849 having to explicitly check for both orderings. */
6850 if (TREE_CODE (arg0) == SSA_NAME
6851 && TREE_CODE (arg1) == SSA_NAME
6852 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6853 return 1;
6855 /* Put SSA_NAMEs last. */
6856 if (TREE_CODE (arg1) == SSA_NAME)
6857 return 0;
6858 if (TREE_CODE (arg0) == SSA_NAME)
6859 return 1;
6861 /* Put variables last. */
6862 if (DECL_P (arg1))
6863 return 0;
6864 if (DECL_P (arg0))
6865 return 1;
6867 return 0;
6871 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6872 means A >= Y && A != MAX, but in this case we know that
6873 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6875 static tree
6876 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6878 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6880 if (TREE_CODE (bound) == LT_EXPR)
6881 a = TREE_OPERAND (bound, 0);
6882 else if (TREE_CODE (bound) == GT_EXPR)
6883 a = TREE_OPERAND (bound, 1);
6884 else
6885 return NULL_TREE;
6887 typea = TREE_TYPE (a);
6888 if (!INTEGRAL_TYPE_P (typea)
6889 && !POINTER_TYPE_P (typea))
6890 return NULL_TREE;
6892 if (TREE_CODE (ineq) == LT_EXPR)
6894 a1 = TREE_OPERAND (ineq, 1);
6895 y = TREE_OPERAND (ineq, 0);
6897 else if (TREE_CODE (ineq) == GT_EXPR)
6899 a1 = TREE_OPERAND (ineq, 0);
6900 y = TREE_OPERAND (ineq, 1);
6902 else
6903 return NULL_TREE;
6905 if (TREE_TYPE (a1) != typea)
6906 return NULL_TREE;
6908 if (POINTER_TYPE_P (typea))
6910 /* Convert the pointer types into integer before taking the difference. */
6911 tree ta = fold_convert_loc (loc, ssizetype, a);
6912 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6913 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6915 else
6916 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6918 if (!diff || !integer_onep (diff))
6919 return NULL_TREE;
6921 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6924 /* Fold a sum or difference of at least one multiplication.
6925 Returns the folded tree or NULL if no simplification could be made. */
6927 static tree
6928 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6929 tree arg0, tree arg1)
6931 tree arg00, arg01, arg10, arg11;
6932 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6934 /* (A * C) +- (B * C) -> (A+-B) * C.
6935 (A * C) +- A -> A * (C+-1).
6936 We are most concerned about the case where C is a constant,
6937 but other combinations show up during loop reduction. Since
6938 it is not difficult, try all four possibilities. */
6940 if (TREE_CODE (arg0) == MULT_EXPR)
6942 arg00 = TREE_OPERAND (arg0, 0);
6943 arg01 = TREE_OPERAND (arg0, 1);
6945 else if (TREE_CODE (arg0) == INTEGER_CST)
6947 arg00 = build_one_cst (type);
6948 arg01 = arg0;
6950 else
6952 /* We cannot generate constant 1 for fract. */
6953 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6954 return NULL_TREE;
6955 arg00 = arg0;
6956 arg01 = build_one_cst (type);
6958 if (TREE_CODE (arg1) == MULT_EXPR)
6960 arg10 = TREE_OPERAND (arg1, 0);
6961 arg11 = TREE_OPERAND (arg1, 1);
6963 else if (TREE_CODE (arg1) == INTEGER_CST)
6965 arg10 = build_one_cst (type);
6966 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6967 the purpose of this canonicalization. */
6968 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
6969 && negate_expr_p (arg1)
6970 && code == PLUS_EXPR)
6972 arg11 = negate_expr (arg1);
6973 code = MINUS_EXPR;
6975 else
6976 arg11 = arg1;
6978 else
6980 /* We cannot generate constant 1 for fract. */
6981 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6982 return NULL_TREE;
6983 arg10 = arg1;
6984 arg11 = build_one_cst (type);
6986 same = NULL_TREE;
6988 if (operand_equal_p (arg01, arg11, 0))
6989 same = arg01, alt0 = arg00, alt1 = arg10;
6990 else if (operand_equal_p (arg00, arg10, 0))
6991 same = arg00, alt0 = arg01, alt1 = arg11;
6992 else if (operand_equal_p (arg00, arg11, 0))
6993 same = arg00, alt0 = arg01, alt1 = arg10;
6994 else if (operand_equal_p (arg01, arg10, 0))
6995 same = arg01, alt0 = arg00, alt1 = arg11;
6997 /* No identical multiplicands; see if we can find a common
6998 power-of-two factor in non-power-of-two multiplies. This
6999 can help in multi-dimensional array access. */
7000 else if (tree_fits_shwi_p (arg01)
7001 && tree_fits_shwi_p (arg11))
7003 HOST_WIDE_INT int01, int11, tmp;
7004 bool swap = false;
7005 tree maybe_same;
7006 int01 = tree_to_shwi (arg01);
7007 int11 = tree_to_shwi (arg11);
7009 /* Move min of absolute values to int11. */
7010 if (absu_hwi (int01) < absu_hwi (int11))
7012 tmp = int01, int01 = int11, int11 = tmp;
7013 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7014 maybe_same = arg01;
7015 swap = true;
7017 else
7018 maybe_same = arg11;
7020 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7021 /* The remainder should not be a constant, otherwise we
7022 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7023 increased the number of multiplications necessary. */
7024 && TREE_CODE (arg10) != INTEGER_CST)
7026 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7027 build_int_cst (TREE_TYPE (arg00),
7028 int01 / int11));
7029 alt1 = arg10;
7030 same = maybe_same;
7031 if (swap)
7032 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7036 if (same)
7037 return fold_build2_loc (loc, MULT_EXPR, type,
7038 fold_build2_loc (loc, code, type,
7039 fold_convert_loc (loc, type, alt0),
7040 fold_convert_loc (loc, type, alt1)),
7041 fold_convert_loc (loc, type, same));
7043 return NULL_TREE;
7046 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7047 specified by EXPR into the buffer PTR of length LEN bytes.
7048 Return the number of bytes placed in the buffer, or zero
7049 upon failure. */
7051 static int
7052 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7054 tree type = TREE_TYPE (expr);
7055 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7056 int byte, offset, word, words;
7057 unsigned char value;
7059 if ((off == -1 && total_bytes > len)
7060 || off >= total_bytes)
7061 return 0;
7062 if (off == -1)
7063 off = 0;
7064 words = total_bytes / UNITS_PER_WORD;
7066 for (byte = 0; byte < total_bytes; byte++)
7068 int bitpos = byte * BITS_PER_UNIT;
7069 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7070 number of bytes. */
7071 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7073 if (total_bytes > UNITS_PER_WORD)
7075 word = byte / UNITS_PER_WORD;
7076 if (WORDS_BIG_ENDIAN)
7077 word = (words - 1) - word;
7078 offset = word * UNITS_PER_WORD;
7079 if (BYTES_BIG_ENDIAN)
7080 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7081 else
7082 offset += byte % UNITS_PER_WORD;
7084 else
7085 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7086 if (offset >= off
7087 && offset - off < len)
7088 ptr[offset - off] = value;
7090 return MIN (len, total_bytes - off);
7094 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7095 specified by EXPR into the buffer PTR of length LEN bytes.
7096 Return the number of bytes placed in the buffer, or zero
7097 upon failure. */
7099 static int
7100 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7102 tree type = TREE_TYPE (expr);
7103 machine_mode mode = TYPE_MODE (type);
7104 int total_bytes = GET_MODE_SIZE (mode);
7105 FIXED_VALUE_TYPE value;
7106 tree i_value, i_type;
7108 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7109 return 0;
7111 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7113 if (NULL_TREE == i_type
7114 || TYPE_PRECISION (i_type) != total_bytes)
7115 return 0;
7117 value = TREE_FIXED_CST (expr);
7118 i_value = double_int_to_tree (i_type, value.data);
7120 return native_encode_int (i_value, ptr, len, off);
7124 /* Subroutine of native_encode_expr. Encode the REAL_CST
7125 specified by EXPR into the buffer PTR of length LEN bytes.
7126 Return the number of bytes placed in the buffer, or zero
7127 upon failure. */
7129 static int
7130 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7132 tree type = TREE_TYPE (expr);
7133 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7134 int byte, offset, word, words, bitpos;
7135 unsigned char value;
7137 /* There are always 32 bits in each long, no matter the size of
7138 the hosts long. We handle floating point representations with
7139 up to 192 bits. */
7140 long tmp[6];
7142 if ((off == -1 && total_bytes > len)
7143 || off >= total_bytes)
7144 return 0;
7145 if (off == -1)
7146 off = 0;
7147 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7149 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7151 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7152 bitpos += BITS_PER_UNIT)
7154 byte = (bitpos / BITS_PER_UNIT) & 3;
7155 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7157 if (UNITS_PER_WORD < 4)
7159 word = byte / UNITS_PER_WORD;
7160 if (WORDS_BIG_ENDIAN)
7161 word = (words - 1) - word;
7162 offset = word * UNITS_PER_WORD;
7163 if (BYTES_BIG_ENDIAN)
7164 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7165 else
7166 offset += byte % UNITS_PER_WORD;
7168 else
7169 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7170 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7171 if (offset >= off
7172 && offset - off < len)
7173 ptr[offset - off] = value;
7175 return MIN (len, total_bytes - off);
7178 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7179 specified by EXPR into the buffer PTR of length LEN bytes.
7180 Return the number of bytes placed in the buffer, or zero
7181 upon failure. */
7183 static int
7184 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7186 int rsize, isize;
7187 tree part;
7189 part = TREE_REALPART (expr);
7190 rsize = native_encode_expr (part, ptr, len, off);
7191 if (off == -1
7192 && rsize == 0)
7193 return 0;
7194 part = TREE_IMAGPART (expr);
7195 if (off != -1)
7196 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7197 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7198 if (off == -1
7199 && isize != rsize)
7200 return 0;
7201 return rsize + isize;
7205 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7206 specified by EXPR into the buffer PTR of length LEN bytes.
7207 Return the number of bytes placed in the buffer, or zero
7208 upon failure. */
7210 static int
7211 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7213 unsigned i, count;
7214 int size, offset;
7215 tree itype, elem;
7217 offset = 0;
7218 count = VECTOR_CST_NELTS (expr);
7219 itype = TREE_TYPE (TREE_TYPE (expr));
7220 size = GET_MODE_SIZE (TYPE_MODE (itype));
7221 for (i = 0; i < count; i++)
7223 if (off >= size)
7225 off -= size;
7226 continue;
7228 elem = VECTOR_CST_ELT (expr, i);
7229 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7230 if ((off == -1 && res != size)
7231 || res == 0)
7232 return 0;
7233 offset += res;
7234 if (offset >= len)
7235 return offset;
7236 if (off != -1)
7237 off = 0;
7239 return offset;
7243 /* Subroutine of native_encode_expr. Encode the STRING_CST
7244 specified by EXPR into the buffer PTR of length LEN bytes.
7245 Return the number of bytes placed in the buffer, or zero
7246 upon failure. */
7248 static int
7249 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7251 tree type = TREE_TYPE (expr);
7252 HOST_WIDE_INT total_bytes;
7254 if (TREE_CODE (type) != ARRAY_TYPE
7255 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7256 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7257 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7258 return 0;
7259 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7260 if ((off == -1 && total_bytes > len)
7261 || off >= total_bytes)
7262 return 0;
7263 if (off == -1)
7264 off = 0;
7265 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7267 int written = 0;
7268 if (off < TREE_STRING_LENGTH (expr))
7270 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7271 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7273 memset (ptr + written, 0,
7274 MIN (total_bytes - written, len - written));
7276 else
7277 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7278 return MIN (total_bytes - off, len);
7282 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7283 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7284 buffer PTR of length LEN bytes. If OFF is not -1 then start
7285 the encoding at byte offset OFF and encode at most LEN bytes.
7286 Return the number of bytes placed in the buffer, or zero upon failure. */
7289 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7291 /* We don't support starting at negative offset and -1 is special. */
7292 if (off < -1)
7293 return 0;
7295 switch (TREE_CODE (expr))
7297 case INTEGER_CST:
7298 return native_encode_int (expr, ptr, len, off);
7300 case REAL_CST:
7301 return native_encode_real (expr, ptr, len, off);
7303 case FIXED_CST:
7304 return native_encode_fixed (expr, ptr, len, off);
7306 case COMPLEX_CST:
7307 return native_encode_complex (expr, ptr, len, off);
7309 case VECTOR_CST:
7310 return native_encode_vector (expr, ptr, len, off);
7312 case STRING_CST:
7313 return native_encode_string (expr, ptr, len, off);
7315 default:
7316 return 0;
7321 /* Subroutine of native_interpret_expr. Interpret the contents of
7322 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7323 If the buffer cannot be interpreted, return NULL_TREE. */
7325 static tree
7326 native_interpret_int (tree type, const unsigned char *ptr, int len)
7328 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7330 if (total_bytes > len
7331 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7332 return NULL_TREE;
7334 wide_int result = wi::from_buffer (ptr, total_bytes);
7336 return wide_int_to_tree (type, result);
7340 /* Subroutine of native_interpret_expr. Interpret the contents of
7341 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7342 If the buffer cannot be interpreted, return NULL_TREE. */
7344 static tree
7345 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7347 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7348 double_int result;
7349 FIXED_VALUE_TYPE fixed_value;
7351 if (total_bytes > len
7352 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7353 return NULL_TREE;
7355 result = double_int::from_buffer (ptr, total_bytes);
7356 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7358 return build_fixed (type, fixed_value);
7362 /* Subroutine of native_interpret_expr. Interpret the contents of
7363 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7364 If the buffer cannot be interpreted, return NULL_TREE. */
7366 static tree
7367 native_interpret_real (tree type, const unsigned char *ptr, int len)
7369 machine_mode mode = TYPE_MODE (type);
7370 int total_bytes = GET_MODE_SIZE (mode);
7371 unsigned char value;
7372 /* There are always 32 bits in each long, no matter the size of
7373 the hosts long. We handle floating point representations with
7374 up to 192 bits. */
7375 REAL_VALUE_TYPE r;
7376 long tmp[6];
7378 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7379 if (total_bytes > len || total_bytes > 24)
7380 return NULL_TREE;
7381 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7383 memset (tmp, 0, sizeof (tmp));
7384 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7385 bitpos += BITS_PER_UNIT)
7387 /* Both OFFSET and BYTE index within a long;
7388 bitpos indexes the whole float. */
7389 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7390 if (UNITS_PER_WORD < 4)
7392 int word = byte / UNITS_PER_WORD;
7393 if (WORDS_BIG_ENDIAN)
7394 word = (words - 1) - word;
7395 offset = word * UNITS_PER_WORD;
7396 if (BYTES_BIG_ENDIAN)
7397 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7398 else
7399 offset += byte % UNITS_PER_WORD;
7401 else
7403 offset = byte;
7404 if (BYTES_BIG_ENDIAN)
7406 /* Reverse bytes within each long, or within the entire float
7407 if it's smaller than a long (for HFmode). */
7408 offset = MIN (3, total_bytes - 1) - offset;
7409 gcc_assert (offset >= 0);
7412 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7414 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7417 real_from_target (&r, tmp, mode);
7418 return build_real (type, r);
7422 /* Subroutine of native_interpret_expr. Interpret the contents of
7423 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7424 If the buffer cannot be interpreted, return NULL_TREE. */
7426 static tree
7427 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7429 tree etype, rpart, ipart;
7430 int size;
7432 etype = TREE_TYPE (type);
7433 size = GET_MODE_SIZE (TYPE_MODE (etype));
7434 if (size * 2 > len)
7435 return NULL_TREE;
7436 rpart = native_interpret_expr (etype, ptr, size);
7437 if (!rpart)
7438 return NULL_TREE;
7439 ipart = native_interpret_expr (etype, ptr+size, size);
7440 if (!ipart)
7441 return NULL_TREE;
7442 return build_complex (type, rpart, ipart);
7446 /* Subroutine of native_interpret_expr. Interpret the contents of
7447 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7448 If the buffer cannot be interpreted, return NULL_TREE. */
7450 static tree
7451 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7453 tree etype, elem;
7454 int i, size, count;
7455 tree *elements;
7457 etype = TREE_TYPE (type);
7458 size = GET_MODE_SIZE (TYPE_MODE (etype));
7459 count = TYPE_VECTOR_SUBPARTS (type);
7460 if (size * count > len)
7461 return NULL_TREE;
7463 elements = XALLOCAVEC (tree, count);
7464 for (i = count - 1; i >= 0; i--)
7466 elem = native_interpret_expr (etype, ptr+(i*size), size);
7467 if (!elem)
7468 return NULL_TREE;
7469 elements[i] = elem;
7471 return build_vector (type, elements);
7475 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7476 the buffer PTR of length LEN as a constant of type TYPE. For
7477 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7478 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7479 return NULL_TREE. */
7481 tree
7482 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7484 switch (TREE_CODE (type))
7486 case INTEGER_TYPE:
7487 case ENUMERAL_TYPE:
7488 case BOOLEAN_TYPE:
7489 case POINTER_TYPE:
7490 case REFERENCE_TYPE:
7491 return native_interpret_int (type, ptr, len);
7493 case REAL_TYPE:
7494 return native_interpret_real (type, ptr, len);
7496 case FIXED_POINT_TYPE:
7497 return native_interpret_fixed (type, ptr, len);
7499 case COMPLEX_TYPE:
7500 return native_interpret_complex (type, ptr, len);
7502 case VECTOR_TYPE:
7503 return native_interpret_vector (type, ptr, len);
7505 default:
7506 return NULL_TREE;
7510 /* Returns true if we can interpret the contents of a native encoding
7511 as TYPE. */
7513 static bool
7514 can_native_interpret_type_p (tree type)
7516 switch (TREE_CODE (type))
7518 case INTEGER_TYPE:
7519 case ENUMERAL_TYPE:
7520 case BOOLEAN_TYPE:
7521 case POINTER_TYPE:
7522 case REFERENCE_TYPE:
7523 case FIXED_POINT_TYPE:
7524 case REAL_TYPE:
7525 case COMPLEX_TYPE:
7526 case VECTOR_TYPE:
7527 return true;
7528 default:
7529 return false;
7533 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7534 TYPE at compile-time. If we're unable to perform the conversion
7535 return NULL_TREE. */
7537 static tree
7538 fold_view_convert_expr (tree type, tree expr)
7540 /* We support up to 512-bit values (for V8DFmode). */
7541 unsigned char buffer[64];
7542 int len;
7544 /* Check that the host and target are sane. */
7545 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7546 return NULL_TREE;
7548 len = native_encode_expr (expr, buffer, sizeof (buffer));
7549 if (len == 0)
7550 return NULL_TREE;
7552 return native_interpret_expr (type, buffer, len);
7555 /* Build an expression for the address of T. Folds away INDIRECT_REF
7556 to avoid confusing the gimplify process. */
7558 tree
7559 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7561 /* The size of the object is not relevant when talking about its address. */
7562 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7563 t = TREE_OPERAND (t, 0);
7565 if (TREE_CODE (t) == INDIRECT_REF)
7567 t = TREE_OPERAND (t, 0);
7569 if (TREE_TYPE (t) != ptrtype)
7570 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7572 else if (TREE_CODE (t) == MEM_REF
7573 && integer_zerop (TREE_OPERAND (t, 1)))
7574 return TREE_OPERAND (t, 0);
7575 else if (TREE_CODE (t) == MEM_REF
7576 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7577 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7578 TREE_OPERAND (t, 0),
7579 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7580 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7582 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7584 if (TREE_TYPE (t) != ptrtype)
7585 t = fold_convert_loc (loc, ptrtype, t);
7587 else
7588 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7590 return t;
7593 /* Build an expression for the address of T. */
7595 tree
7596 build_fold_addr_expr_loc (location_t loc, tree t)
7598 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7600 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7603 /* Fold a unary expression of code CODE and type TYPE with operand
7604 OP0. Return the folded expression if folding is successful.
7605 Otherwise, return NULL_TREE. */
7607 tree
7608 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7610 tree tem;
7611 tree arg0;
7612 enum tree_code_class kind = TREE_CODE_CLASS (code);
7614 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7615 && TREE_CODE_LENGTH (code) == 1);
7617 arg0 = op0;
7618 if (arg0)
7620 if (CONVERT_EXPR_CODE_P (code)
7621 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7623 /* Don't use STRIP_NOPS, because signedness of argument type
7624 matters. */
7625 STRIP_SIGN_NOPS (arg0);
7627 else
7629 /* Strip any conversions that don't change the mode. This
7630 is safe for every expression, except for a comparison
7631 expression because its signedness is derived from its
7632 operands.
7634 Note that this is done as an internal manipulation within
7635 the constant folder, in order to find the simplest
7636 representation of the arguments so that their form can be
7637 studied. In any cases, the appropriate type conversions
7638 should be put back in the tree that will get out of the
7639 constant folder. */
7640 STRIP_NOPS (arg0);
7643 if (CONSTANT_CLASS_P (arg0))
7645 tree tem = const_unop (code, type, arg0);
7646 if (tem)
7648 if (TREE_TYPE (tem) != type)
7649 tem = fold_convert_loc (loc, type, tem);
7650 return tem;
7655 tem = generic_simplify (loc, code, type, op0);
7656 if (tem)
7657 return tem;
7659 if (TREE_CODE_CLASS (code) == tcc_unary)
7661 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7662 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7663 fold_build1_loc (loc, code, type,
7664 fold_convert_loc (loc, TREE_TYPE (op0),
7665 TREE_OPERAND (arg0, 1))));
7666 else if (TREE_CODE (arg0) == COND_EXPR)
7668 tree arg01 = TREE_OPERAND (arg0, 1);
7669 tree arg02 = TREE_OPERAND (arg0, 2);
7670 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7671 arg01 = fold_build1_loc (loc, code, type,
7672 fold_convert_loc (loc,
7673 TREE_TYPE (op0), arg01));
7674 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7675 arg02 = fold_build1_loc (loc, code, type,
7676 fold_convert_loc (loc,
7677 TREE_TYPE (op0), arg02));
7678 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7679 arg01, arg02);
7681 /* If this was a conversion, and all we did was to move into
7682 inside the COND_EXPR, bring it back out. But leave it if
7683 it is a conversion from integer to integer and the
7684 result precision is no wider than a word since such a
7685 conversion is cheap and may be optimized away by combine,
7686 while it couldn't if it were outside the COND_EXPR. Then return
7687 so we don't get into an infinite recursion loop taking the
7688 conversion out and then back in. */
7690 if ((CONVERT_EXPR_CODE_P (code)
7691 || code == NON_LVALUE_EXPR)
7692 && TREE_CODE (tem) == COND_EXPR
7693 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7694 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7695 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7696 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7697 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7698 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7699 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7700 && (INTEGRAL_TYPE_P
7701 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7702 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7703 || flag_syntax_only))
7704 tem = build1_loc (loc, code, type,
7705 build3 (COND_EXPR,
7706 TREE_TYPE (TREE_OPERAND
7707 (TREE_OPERAND (tem, 1), 0)),
7708 TREE_OPERAND (tem, 0),
7709 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7710 TREE_OPERAND (TREE_OPERAND (tem, 2),
7711 0)));
7712 return tem;
7716 switch (code)
7718 case NON_LVALUE_EXPR:
7719 if (!maybe_lvalue_p (op0))
7720 return fold_convert_loc (loc, type, op0);
7721 return NULL_TREE;
7723 CASE_CONVERT:
7724 case FLOAT_EXPR:
7725 case FIX_TRUNC_EXPR:
7726 if (COMPARISON_CLASS_P (op0))
7728 /* If we have (type) (a CMP b) and type is an integral type, return
7729 new expression involving the new type. Canonicalize
7730 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7731 non-integral type.
7732 Do not fold the result as that would not simplify further, also
7733 folding again results in recursions. */
7734 if (TREE_CODE (type) == BOOLEAN_TYPE)
7735 return build2_loc (loc, TREE_CODE (op0), type,
7736 TREE_OPERAND (op0, 0),
7737 TREE_OPERAND (op0, 1));
7738 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7739 && TREE_CODE (type) != VECTOR_TYPE)
7740 return build3_loc (loc, COND_EXPR, type, op0,
7741 constant_boolean_node (true, type),
7742 constant_boolean_node (false, type));
7745 /* Handle (T *)&A.B.C for A being of type T and B and C
7746 living at offset zero. This occurs frequently in
7747 C++ upcasting and then accessing the base. */
7748 if (TREE_CODE (op0) == ADDR_EXPR
7749 && POINTER_TYPE_P (type)
7750 && handled_component_p (TREE_OPERAND (op0, 0)))
7752 HOST_WIDE_INT bitsize, bitpos;
7753 tree offset;
7754 machine_mode mode;
7755 int unsignedp, reversep, volatilep;
7756 tree base
7757 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7758 &offset, &mode, &unsignedp, &reversep,
7759 &volatilep, false);
7760 /* If the reference was to a (constant) zero offset, we can use
7761 the address of the base if it has the same base type
7762 as the result type and the pointer type is unqualified. */
7763 if (! offset && bitpos == 0
7764 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7765 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7766 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7767 return fold_convert_loc (loc, type,
7768 build_fold_addr_expr_loc (loc, base));
7771 if (TREE_CODE (op0) == MODIFY_EXPR
7772 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7773 /* Detect assigning a bitfield. */
7774 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7775 && DECL_BIT_FIELD
7776 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7778 /* Don't leave an assignment inside a conversion
7779 unless assigning a bitfield. */
7780 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7781 /* First do the assignment, then return converted constant. */
7782 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7783 TREE_NO_WARNING (tem) = 1;
7784 TREE_USED (tem) = 1;
7785 return tem;
7788 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7789 constants (if x has signed type, the sign bit cannot be set
7790 in c). This folds extension into the BIT_AND_EXPR.
7791 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7792 very likely don't have maximal range for their precision and this
7793 transformation effectively doesn't preserve non-maximal ranges. */
7794 if (TREE_CODE (type) == INTEGER_TYPE
7795 && TREE_CODE (op0) == BIT_AND_EXPR
7796 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7798 tree and_expr = op0;
7799 tree and0 = TREE_OPERAND (and_expr, 0);
7800 tree and1 = TREE_OPERAND (and_expr, 1);
7801 int change = 0;
7803 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7804 || (TYPE_PRECISION (type)
7805 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7806 change = 1;
7807 else if (TYPE_PRECISION (TREE_TYPE (and1))
7808 <= HOST_BITS_PER_WIDE_INT
7809 && tree_fits_uhwi_p (and1))
7811 unsigned HOST_WIDE_INT cst;
7813 cst = tree_to_uhwi (and1);
7814 cst &= HOST_WIDE_INT_M1U
7815 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7816 change = (cst == 0);
7817 if (change
7818 && !flag_syntax_only
7819 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7820 == ZERO_EXTEND))
7822 tree uns = unsigned_type_for (TREE_TYPE (and0));
7823 and0 = fold_convert_loc (loc, uns, and0);
7824 and1 = fold_convert_loc (loc, uns, and1);
7827 if (change)
7829 tem = force_fit_type (type, wi::to_widest (and1), 0,
7830 TREE_OVERFLOW (and1));
7831 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7832 fold_convert_loc (loc, type, and0), tem);
7836 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7837 cast (T1)X will fold away. We assume that this happens when X itself
7838 is a cast. */
7839 if (POINTER_TYPE_P (type)
7840 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7841 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7843 tree arg00 = TREE_OPERAND (arg0, 0);
7844 tree arg01 = TREE_OPERAND (arg0, 1);
7846 return fold_build_pointer_plus_loc
7847 (loc, fold_convert_loc (loc, type, arg00), arg01);
7850 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7851 of the same precision, and X is an integer type not narrower than
7852 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7853 if (INTEGRAL_TYPE_P (type)
7854 && TREE_CODE (op0) == BIT_NOT_EXPR
7855 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7856 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7857 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7859 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7860 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7861 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7862 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7863 fold_convert_loc (loc, type, tem));
7866 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7867 type of X and Y (integer types only). */
7868 if (INTEGRAL_TYPE_P (type)
7869 && TREE_CODE (op0) == MULT_EXPR
7870 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7871 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7873 /* Be careful not to introduce new overflows. */
7874 tree mult_type;
7875 if (TYPE_OVERFLOW_WRAPS (type))
7876 mult_type = type;
7877 else
7878 mult_type = unsigned_type_for (type);
7880 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7882 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7883 fold_convert_loc (loc, mult_type,
7884 TREE_OPERAND (op0, 0)),
7885 fold_convert_loc (loc, mult_type,
7886 TREE_OPERAND (op0, 1)));
7887 return fold_convert_loc (loc, type, tem);
7891 return NULL_TREE;
7893 case VIEW_CONVERT_EXPR:
7894 if (TREE_CODE (op0) == MEM_REF)
7896 tem = fold_build2_loc (loc, MEM_REF, type,
7897 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7898 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7899 return tem;
7902 return NULL_TREE;
7904 case NEGATE_EXPR:
7905 tem = fold_negate_expr (loc, arg0);
7906 if (tem)
7907 return fold_convert_loc (loc, type, tem);
7908 return NULL_TREE;
7910 case ABS_EXPR:
7911 /* Convert fabs((double)float) into (double)fabsf(float). */
7912 if (TREE_CODE (arg0) == NOP_EXPR
7913 && TREE_CODE (type) == REAL_TYPE)
7915 tree targ0 = strip_float_extensions (arg0);
7916 if (targ0 != arg0)
7917 return fold_convert_loc (loc, type,
7918 fold_build1_loc (loc, ABS_EXPR,
7919 TREE_TYPE (targ0),
7920 targ0));
7922 return NULL_TREE;
7924 case BIT_NOT_EXPR:
7925 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7926 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7927 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7928 fold_convert_loc (loc, type,
7929 TREE_OPERAND (arg0, 0)))))
7930 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7931 fold_convert_loc (loc, type,
7932 TREE_OPERAND (arg0, 1)));
7933 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7934 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7935 fold_convert_loc (loc, type,
7936 TREE_OPERAND (arg0, 1)))))
7937 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7938 fold_convert_loc (loc, type,
7939 TREE_OPERAND (arg0, 0)), tem);
7941 return NULL_TREE;
7943 case TRUTH_NOT_EXPR:
7944 /* Note that the operand of this must be an int
7945 and its values must be 0 or 1.
7946 ("true" is a fixed value perhaps depending on the language,
7947 but we don't handle values other than 1 correctly yet.) */
7948 tem = fold_truth_not_expr (loc, arg0);
7949 if (!tem)
7950 return NULL_TREE;
7951 return fold_convert_loc (loc, type, tem);
7953 case INDIRECT_REF:
7954 /* Fold *&X to X if X is an lvalue. */
7955 if (TREE_CODE (op0) == ADDR_EXPR)
7957 tree op00 = TREE_OPERAND (op0, 0);
7958 if ((TREE_CODE (op00) == VAR_DECL
7959 || TREE_CODE (op00) == PARM_DECL
7960 || TREE_CODE (op00) == RESULT_DECL)
7961 && !TREE_READONLY (op00))
7962 return op00;
7964 return NULL_TREE;
7966 default:
7967 return NULL_TREE;
7968 } /* switch (code) */
7972 /* If the operation was a conversion do _not_ mark a resulting constant
7973 with TREE_OVERFLOW if the original constant was not. These conversions
7974 have implementation defined behavior and retaining the TREE_OVERFLOW
7975 flag here would confuse later passes such as VRP. */
7976 tree
7977 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
7978 tree type, tree op0)
7980 tree res = fold_unary_loc (loc, code, type, op0);
7981 if (res
7982 && TREE_CODE (res) == INTEGER_CST
7983 && TREE_CODE (op0) == INTEGER_CST
7984 && CONVERT_EXPR_CODE_P (code))
7985 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
7987 return res;
7990 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7991 operands OP0 and OP1. LOC is the location of the resulting expression.
7992 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7993 Return the folded expression if folding is successful. Otherwise,
7994 return NULL_TREE. */
7995 static tree
7996 fold_truth_andor (location_t loc, enum tree_code code, tree type,
7997 tree arg0, tree arg1, tree op0, tree op1)
7999 tree tem;
8001 /* We only do these simplifications if we are optimizing. */
8002 if (!optimize)
8003 return NULL_TREE;
8005 /* Check for things like (A || B) && (A || C). We can convert this
8006 to A || (B && C). Note that either operator can be any of the four
8007 truth and/or operations and the transformation will still be
8008 valid. Also note that we only care about order for the
8009 ANDIF and ORIF operators. If B contains side effects, this
8010 might change the truth-value of A. */
8011 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8012 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8013 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8014 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8015 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8016 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8018 tree a00 = TREE_OPERAND (arg0, 0);
8019 tree a01 = TREE_OPERAND (arg0, 1);
8020 tree a10 = TREE_OPERAND (arg1, 0);
8021 tree a11 = TREE_OPERAND (arg1, 1);
8022 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8023 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8024 && (code == TRUTH_AND_EXPR
8025 || code == TRUTH_OR_EXPR));
8027 if (operand_equal_p (a00, a10, 0))
8028 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8029 fold_build2_loc (loc, code, type, a01, a11));
8030 else if (commutative && operand_equal_p (a00, a11, 0))
8031 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8032 fold_build2_loc (loc, code, type, a01, a10));
8033 else if (commutative && operand_equal_p (a01, a10, 0))
8034 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8035 fold_build2_loc (loc, code, type, a00, a11));
8037 /* This case if tricky because we must either have commutative
8038 operators or else A10 must not have side-effects. */
8040 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8041 && operand_equal_p (a01, a11, 0))
8042 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8043 fold_build2_loc (loc, code, type, a00, a10),
8044 a01);
8047 /* See if we can build a range comparison. */
8048 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
8049 return tem;
8051 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8052 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8054 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8055 if (tem)
8056 return fold_build2_loc (loc, code, type, tem, arg1);
8059 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8060 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8062 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8063 if (tem)
8064 return fold_build2_loc (loc, code, type, arg0, tem);
8067 /* Check for the possibility of merging component references. If our
8068 lhs is another similar operation, try to merge its rhs with our
8069 rhs. Then try to merge our lhs and rhs. */
8070 if (TREE_CODE (arg0) == code
8071 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
8072 TREE_OPERAND (arg0, 1), arg1)))
8073 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8075 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8076 return tem;
8078 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8079 && (code == TRUTH_AND_EXPR
8080 || code == TRUTH_ANDIF_EXPR
8081 || code == TRUTH_OR_EXPR
8082 || code == TRUTH_ORIF_EXPR))
8084 enum tree_code ncode, icode;
8086 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8087 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8088 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8090 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8091 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8092 We don't want to pack more than two leafs to a non-IF AND/OR
8093 expression.
8094 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8095 equal to IF-CODE, then we don't want to add right-hand operand.
8096 If the inner right-hand side of left-hand operand has
8097 side-effects, or isn't simple, then we can't add to it,
8098 as otherwise we might destroy if-sequence. */
8099 if (TREE_CODE (arg0) == icode
8100 && simple_operand_p_2 (arg1)
8101 /* Needed for sequence points to handle trappings, and
8102 side-effects. */
8103 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8105 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8106 arg1);
8107 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8108 tem);
8110 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8111 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8112 else if (TREE_CODE (arg1) == icode
8113 && simple_operand_p_2 (arg0)
8114 /* Needed for sequence points to handle trappings, and
8115 side-effects. */
8116 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8118 tem = fold_build2_loc (loc, ncode, type,
8119 arg0, TREE_OPERAND (arg1, 0));
8120 return fold_build2_loc (loc, icode, type, tem,
8121 TREE_OPERAND (arg1, 1));
8123 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8124 into (A OR B).
8125 For sequence point consistancy, we need to check for trapping,
8126 and side-effects. */
8127 else if (code == icode && simple_operand_p_2 (arg0)
8128 && simple_operand_p_2 (arg1))
8129 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8132 return NULL_TREE;
8135 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8136 by changing CODE to reduce the magnitude of constants involved in
8137 ARG0 of the comparison.
8138 Returns a canonicalized comparison tree if a simplification was
8139 possible, otherwise returns NULL_TREE.
8140 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8141 valid if signed overflow is undefined. */
8143 static tree
8144 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8145 tree arg0, tree arg1,
8146 bool *strict_overflow_p)
8148 enum tree_code code0 = TREE_CODE (arg0);
8149 tree t, cst0 = NULL_TREE;
8150 int sgn0;
8152 /* Match A +- CST code arg1. We can change this only if overflow
8153 is undefined. */
8154 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8155 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8156 /* In principle pointers also have undefined overflow behavior,
8157 but that causes problems elsewhere. */
8158 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8159 && (code0 == MINUS_EXPR
8160 || code0 == PLUS_EXPR)
8161 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8162 return NULL_TREE;
8164 /* Identify the constant in arg0 and its sign. */
8165 cst0 = TREE_OPERAND (arg0, 1);
8166 sgn0 = tree_int_cst_sgn (cst0);
8168 /* Overflowed constants and zero will cause problems. */
8169 if (integer_zerop (cst0)
8170 || TREE_OVERFLOW (cst0))
8171 return NULL_TREE;
8173 /* See if we can reduce the magnitude of the constant in
8174 arg0 by changing the comparison code. */
8175 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8176 if (code == LT_EXPR
8177 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8178 code = LE_EXPR;
8179 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8180 else if (code == GT_EXPR
8181 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8182 code = GE_EXPR;
8183 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8184 else if (code == LE_EXPR
8185 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8186 code = LT_EXPR;
8187 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8188 else if (code == GE_EXPR
8189 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8190 code = GT_EXPR;
8191 else
8192 return NULL_TREE;
8193 *strict_overflow_p = true;
8195 /* Now build the constant reduced in magnitude. But not if that
8196 would produce one outside of its types range. */
8197 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8198 && ((sgn0 == 1
8199 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8200 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8201 || (sgn0 == -1
8202 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8203 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8204 return NULL_TREE;
8206 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8207 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8208 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8209 t = fold_convert (TREE_TYPE (arg1), t);
8211 return fold_build2_loc (loc, code, type, t, arg1);
8214 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8215 overflow further. Try to decrease the magnitude of constants involved
8216 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8217 and put sole constants at the second argument position.
8218 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8220 static tree
8221 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8222 tree arg0, tree arg1)
8224 tree t;
8225 bool strict_overflow_p;
8226 const char * const warnmsg = G_("assuming signed overflow does not occur "
8227 "when reducing constant in comparison");
8229 /* Try canonicalization by simplifying arg0. */
8230 strict_overflow_p = false;
8231 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8232 &strict_overflow_p);
8233 if (t)
8235 if (strict_overflow_p)
8236 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8237 return t;
8240 /* Try canonicalization by simplifying arg1 using the swapped
8241 comparison. */
8242 code = swap_tree_comparison (code);
8243 strict_overflow_p = false;
8244 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8245 &strict_overflow_p);
8246 if (t && strict_overflow_p)
8247 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8248 return t;
8251 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8252 space. This is used to avoid issuing overflow warnings for
8253 expressions like &p->x which can not wrap. */
8255 static bool
8256 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8258 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8259 return true;
8261 if (bitpos < 0)
8262 return true;
8264 wide_int wi_offset;
8265 int precision = TYPE_PRECISION (TREE_TYPE (base));
8266 if (offset == NULL_TREE)
8267 wi_offset = wi::zero (precision);
8268 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8269 return true;
8270 else
8271 wi_offset = offset;
8273 bool overflow;
8274 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8275 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8276 if (overflow)
8277 return true;
8279 if (!wi::fits_uhwi_p (total))
8280 return true;
8282 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8283 if (size <= 0)
8284 return true;
8286 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8287 array. */
8288 if (TREE_CODE (base) == ADDR_EXPR)
8290 HOST_WIDE_INT base_size;
8292 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8293 if (base_size > 0 && size < base_size)
8294 size = base_size;
8297 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8300 /* Subroutine of fold_binary. This routine performs all of the
8301 transformations that are common to the equality/inequality
8302 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8303 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8304 fold_binary should call fold_binary. Fold a comparison with
8305 tree code CODE and type TYPE with operands OP0 and OP1. Return
8306 the folded comparison or NULL_TREE. */
8308 static tree
8309 fold_comparison (location_t loc, enum tree_code code, tree type,
8310 tree op0, tree op1)
8312 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8313 tree arg0, arg1, tem;
8315 arg0 = op0;
8316 arg1 = op1;
8318 STRIP_SIGN_NOPS (arg0);
8319 STRIP_SIGN_NOPS (arg1);
8321 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8322 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8323 && (equality_code
8324 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8325 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8326 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8327 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8328 && TREE_CODE (arg1) == INTEGER_CST
8329 && !TREE_OVERFLOW (arg1))
8331 const enum tree_code
8332 reverse_op = TREE_CODE (arg0) == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
8333 tree const1 = TREE_OPERAND (arg0, 1);
8334 tree const2 = fold_convert_loc (loc, TREE_TYPE (const1), arg1);
8335 tree variable = TREE_OPERAND (arg0, 0);
8336 tree new_const = int_const_binop (reverse_op, const2, const1);
8338 /* If the constant operation overflowed this can be
8339 simplified as a comparison against INT_MAX/INT_MIN. */
8340 if (TREE_OVERFLOW (new_const)
8341 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
8343 int const1_sgn = tree_int_cst_sgn (const1);
8344 enum tree_code code2 = code;
8346 /* Get the sign of the constant on the lhs if the
8347 operation were VARIABLE + CONST1. */
8348 if (TREE_CODE (arg0) == MINUS_EXPR)
8349 const1_sgn = -const1_sgn;
8351 /* The sign of the constant determines if we overflowed
8352 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8353 Canonicalize to the INT_MIN overflow by swapping the comparison
8354 if necessary. */
8355 if (const1_sgn == -1)
8356 code2 = swap_tree_comparison (code);
8358 /* We now can look at the canonicalized case
8359 VARIABLE + 1 CODE2 INT_MIN
8360 and decide on the result. */
8361 switch (code2)
8363 case EQ_EXPR:
8364 case LT_EXPR:
8365 case LE_EXPR:
8366 return
8367 omit_one_operand_loc (loc, type, boolean_false_node, variable);
8369 case NE_EXPR:
8370 case GE_EXPR:
8371 case GT_EXPR:
8372 return
8373 omit_one_operand_loc (loc, type, boolean_true_node, variable);
8375 default:
8376 gcc_unreachable ();
8379 else
8381 if (!equality_code)
8382 fold_overflow_warning ("assuming signed overflow does not occur "
8383 "when changing X +- C1 cmp C2 to "
8384 "X cmp C2 -+ C1",
8385 WARN_STRICT_OVERFLOW_COMPARISON);
8386 return fold_build2_loc (loc, code, type, variable, new_const);
8390 /* For comparisons of pointers we can decompose it to a compile time
8391 comparison of the base objects and the offsets into the object.
8392 This requires at least one operand being an ADDR_EXPR or a
8393 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8394 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8395 && (TREE_CODE (arg0) == ADDR_EXPR
8396 || TREE_CODE (arg1) == ADDR_EXPR
8397 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8398 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8400 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8401 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8402 machine_mode mode;
8403 int volatilep, reversep, unsignedp;
8404 bool indirect_base0 = false, indirect_base1 = false;
8406 /* Get base and offset for the access. Strip ADDR_EXPR for
8407 get_inner_reference, but put it back by stripping INDIRECT_REF
8408 off the base object if possible. indirect_baseN will be true
8409 if baseN is not an address but refers to the object itself. */
8410 base0 = arg0;
8411 if (TREE_CODE (arg0) == ADDR_EXPR)
8413 base0
8414 = get_inner_reference (TREE_OPERAND (arg0, 0),
8415 &bitsize, &bitpos0, &offset0, &mode,
8416 &unsignedp, &reversep, &volatilep, false);
8417 if (TREE_CODE (base0) == INDIRECT_REF)
8418 base0 = TREE_OPERAND (base0, 0);
8419 else
8420 indirect_base0 = true;
8422 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8424 base0 = TREE_OPERAND (arg0, 0);
8425 STRIP_SIGN_NOPS (base0);
8426 if (TREE_CODE (base0) == ADDR_EXPR)
8428 base0
8429 = get_inner_reference (TREE_OPERAND (base0, 0),
8430 &bitsize, &bitpos0, &offset0, &mode,
8431 &unsignedp, &reversep, &volatilep,
8432 false);
8433 if (TREE_CODE (base0) == INDIRECT_REF)
8434 base0 = TREE_OPERAND (base0, 0);
8435 else
8436 indirect_base0 = true;
8438 if (offset0 == NULL_TREE || integer_zerop (offset0))
8439 offset0 = TREE_OPERAND (arg0, 1);
8440 else
8441 offset0 = size_binop (PLUS_EXPR, offset0,
8442 TREE_OPERAND (arg0, 1));
8443 if (TREE_CODE (offset0) == INTEGER_CST)
8445 offset_int tem = wi::sext (wi::to_offset (offset0),
8446 TYPE_PRECISION (sizetype));
8447 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
8448 tem += bitpos0;
8449 if (wi::fits_shwi_p (tem))
8451 bitpos0 = tem.to_shwi ();
8452 offset0 = NULL_TREE;
8457 base1 = arg1;
8458 if (TREE_CODE (arg1) == ADDR_EXPR)
8460 base1
8461 = get_inner_reference (TREE_OPERAND (arg1, 0),
8462 &bitsize, &bitpos1, &offset1, &mode,
8463 &unsignedp, &reversep, &volatilep, false);
8464 if (TREE_CODE (base1) == INDIRECT_REF)
8465 base1 = TREE_OPERAND (base1, 0);
8466 else
8467 indirect_base1 = true;
8469 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8471 base1 = TREE_OPERAND (arg1, 0);
8472 STRIP_SIGN_NOPS (base1);
8473 if (TREE_CODE (base1) == ADDR_EXPR)
8475 base1
8476 = get_inner_reference (TREE_OPERAND (base1, 0),
8477 &bitsize, &bitpos1, &offset1, &mode,
8478 &unsignedp, &reversep, &volatilep,
8479 false);
8480 if (TREE_CODE (base1) == INDIRECT_REF)
8481 base1 = TREE_OPERAND (base1, 0);
8482 else
8483 indirect_base1 = true;
8485 if (offset1 == NULL_TREE || integer_zerop (offset1))
8486 offset1 = TREE_OPERAND (arg1, 1);
8487 else
8488 offset1 = size_binop (PLUS_EXPR, offset1,
8489 TREE_OPERAND (arg1, 1));
8490 if (TREE_CODE (offset1) == INTEGER_CST)
8492 offset_int tem = wi::sext (wi::to_offset (offset1),
8493 TYPE_PRECISION (sizetype));
8494 tem = wi::lshift (tem, LOG2_BITS_PER_UNIT);
8495 tem += bitpos1;
8496 if (wi::fits_shwi_p (tem))
8498 bitpos1 = tem.to_shwi ();
8499 offset1 = NULL_TREE;
8504 /* If we have equivalent bases we might be able to simplify. */
8505 if (indirect_base0 == indirect_base1
8506 && operand_equal_p (base0, base1,
8507 indirect_base0 ? OEP_ADDRESS_OF : 0))
8509 /* We can fold this expression to a constant if the non-constant
8510 offset parts are equal. */
8511 if ((offset0 == offset1
8512 || (offset0 && offset1
8513 && operand_equal_p (offset0, offset1, 0)))
8514 && (code == EQ_EXPR
8515 || code == NE_EXPR
8516 || (indirect_base0 && DECL_P (base0))
8517 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8520 if (!equality_code
8521 && bitpos0 != bitpos1
8522 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8523 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8524 fold_overflow_warning (("assuming pointer wraparound does not "
8525 "occur when comparing P +- C1 with "
8526 "P +- C2"),
8527 WARN_STRICT_OVERFLOW_CONDITIONAL);
8529 switch (code)
8531 case EQ_EXPR:
8532 return constant_boolean_node (bitpos0 == bitpos1, type);
8533 case NE_EXPR:
8534 return constant_boolean_node (bitpos0 != bitpos1, type);
8535 case LT_EXPR:
8536 return constant_boolean_node (bitpos0 < bitpos1, type);
8537 case LE_EXPR:
8538 return constant_boolean_node (bitpos0 <= bitpos1, type);
8539 case GE_EXPR:
8540 return constant_boolean_node (bitpos0 >= bitpos1, type);
8541 case GT_EXPR:
8542 return constant_boolean_node (bitpos0 > bitpos1, type);
8543 default:;
8546 /* We can simplify the comparison to a comparison of the variable
8547 offset parts if the constant offset parts are equal.
8548 Be careful to use signed sizetype here because otherwise we
8549 mess with array offsets in the wrong way. This is possible
8550 because pointer arithmetic is restricted to retain within an
8551 object and overflow on pointer differences is undefined as of
8552 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8553 else if (bitpos0 == bitpos1
8554 && (equality_code
8555 || (indirect_base0 && DECL_P (base0))
8556 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8558 /* By converting to signed sizetype we cover middle-end pointer
8559 arithmetic which operates on unsigned pointer types of size
8560 type size and ARRAY_REF offsets which are properly sign or
8561 zero extended from their type in case it is narrower than
8562 sizetype. */
8563 if (offset0 == NULL_TREE)
8564 offset0 = build_int_cst (ssizetype, 0);
8565 else
8566 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8567 if (offset1 == NULL_TREE)
8568 offset1 = build_int_cst (ssizetype, 0);
8569 else
8570 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8572 if (!equality_code
8573 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8574 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8575 fold_overflow_warning (("assuming pointer wraparound does not "
8576 "occur when comparing P +- C1 with "
8577 "P +- C2"),
8578 WARN_STRICT_OVERFLOW_COMPARISON);
8580 return fold_build2_loc (loc, code, type, offset0, offset1);
8583 /* For equal offsets we can simplify to a comparison of the
8584 base addresses. */
8585 else if (bitpos0 == bitpos1
8586 && (indirect_base0
8587 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8588 && (indirect_base1
8589 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8590 && ((offset0 == offset1)
8591 || (offset0 && offset1
8592 && operand_equal_p (offset0, offset1, 0))))
8594 if (indirect_base0)
8595 base0 = build_fold_addr_expr_loc (loc, base0);
8596 if (indirect_base1)
8597 base1 = build_fold_addr_expr_loc (loc, base1);
8598 return fold_build2_loc (loc, code, type, base0, base1);
8602 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8603 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8604 the resulting offset is smaller in absolute value than the
8605 original one and has the same sign. */
8606 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8607 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8608 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8609 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8610 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8611 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8612 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8613 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8615 tree const1 = TREE_OPERAND (arg0, 1);
8616 tree const2 = TREE_OPERAND (arg1, 1);
8617 tree variable1 = TREE_OPERAND (arg0, 0);
8618 tree variable2 = TREE_OPERAND (arg1, 0);
8619 tree cst;
8620 const char * const warnmsg = G_("assuming signed overflow does not "
8621 "occur when combining constants around "
8622 "a comparison");
8624 /* Put the constant on the side where it doesn't overflow and is
8625 of lower absolute value and of same sign than before. */
8626 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8627 ? MINUS_EXPR : PLUS_EXPR,
8628 const2, const1);
8629 if (!TREE_OVERFLOW (cst)
8630 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8631 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8633 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8634 return fold_build2_loc (loc, code, type,
8635 variable1,
8636 fold_build2_loc (loc, TREE_CODE (arg1),
8637 TREE_TYPE (arg1),
8638 variable2, cst));
8641 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8642 ? MINUS_EXPR : PLUS_EXPR,
8643 const1, const2);
8644 if (!TREE_OVERFLOW (cst)
8645 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8646 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8648 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8649 return fold_build2_loc (loc, code, type,
8650 fold_build2_loc (loc, TREE_CODE (arg0),
8651 TREE_TYPE (arg0),
8652 variable1, cst),
8653 variable2);
8657 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8658 if (tem)
8659 return tem;
8661 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8662 constant, we can simplify it. */
8663 if (TREE_CODE (arg1) == INTEGER_CST
8664 && (TREE_CODE (arg0) == MIN_EXPR
8665 || TREE_CODE (arg0) == MAX_EXPR)
8666 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8668 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
8669 if (tem)
8670 return tem;
8673 /* If we are comparing an expression that just has comparisons
8674 of two integer values, arithmetic expressions of those comparisons,
8675 and constants, we can simplify it. There are only three cases
8676 to check: the two values can either be equal, the first can be
8677 greater, or the second can be greater. Fold the expression for
8678 those three values. Since each value must be 0 or 1, we have
8679 eight possibilities, each of which corresponds to the constant 0
8680 or 1 or one of the six possible comparisons.
8682 This handles common cases like (a > b) == 0 but also handles
8683 expressions like ((x > y) - (y > x)) > 0, which supposedly
8684 occur in macroized code. */
8686 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8688 tree cval1 = 0, cval2 = 0;
8689 int save_p = 0;
8691 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8692 /* Don't handle degenerate cases here; they should already
8693 have been handled anyway. */
8694 && cval1 != 0 && cval2 != 0
8695 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8696 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8697 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8698 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8699 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8700 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8701 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8703 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8704 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8706 /* We can't just pass T to eval_subst in case cval1 or cval2
8707 was the same as ARG1. */
8709 tree high_result
8710 = fold_build2_loc (loc, code, type,
8711 eval_subst (loc, arg0, cval1, maxval,
8712 cval2, minval),
8713 arg1);
8714 tree equal_result
8715 = fold_build2_loc (loc, code, type,
8716 eval_subst (loc, arg0, cval1, maxval,
8717 cval2, maxval),
8718 arg1);
8719 tree low_result
8720 = fold_build2_loc (loc, code, type,
8721 eval_subst (loc, arg0, cval1, minval,
8722 cval2, maxval),
8723 arg1);
8725 /* All three of these results should be 0 or 1. Confirm they are.
8726 Then use those values to select the proper code to use. */
8728 if (TREE_CODE (high_result) == INTEGER_CST
8729 && TREE_CODE (equal_result) == INTEGER_CST
8730 && TREE_CODE (low_result) == INTEGER_CST)
8732 /* Make a 3-bit mask with the high-order bit being the
8733 value for `>', the next for '=', and the low for '<'. */
8734 switch ((integer_onep (high_result) * 4)
8735 + (integer_onep (equal_result) * 2)
8736 + integer_onep (low_result))
8738 case 0:
8739 /* Always false. */
8740 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8741 case 1:
8742 code = LT_EXPR;
8743 break;
8744 case 2:
8745 code = EQ_EXPR;
8746 break;
8747 case 3:
8748 code = LE_EXPR;
8749 break;
8750 case 4:
8751 code = GT_EXPR;
8752 break;
8753 case 5:
8754 code = NE_EXPR;
8755 break;
8756 case 6:
8757 code = GE_EXPR;
8758 break;
8759 case 7:
8760 /* Always true. */
8761 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8764 if (save_p)
8766 tem = save_expr (build2 (code, type, cval1, cval2));
8767 SET_EXPR_LOCATION (tem, loc);
8768 return tem;
8770 return fold_build2_loc (loc, code, type, cval1, cval2);
8775 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8776 into a single range test. */
8777 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8778 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8779 && TREE_CODE (arg1) == INTEGER_CST
8780 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8781 && !integer_zerop (TREE_OPERAND (arg0, 1))
8782 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8783 && !TREE_OVERFLOW (arg1))
8785 tem = fold_div_compare (loc, code, type, arg0, arg1);
8786 if (tem != NULL_TREE)
8787 return tem;
8790 return NULL_TREE;
8794 /* Subroutine of fold_binary. Optimize complex multiplications of the
8795 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8796 argument EXPR represents the expression "z" of type TYPE. */
8798 static tree
8799 fold_mult_zconjz (location_t loc, tree type, tree expr)
8801 tree itype = TREE_TYPE (type);
8802 tree rpart, ipart, tem;
8804 if (TREE_CODE (expr) == COMPLEX_EXPR)
8806 rpart = TREE_OPERAND (expr, 0);
8807 ipart = TREE_OPERAND (expr, 1);
8809 else if (TREE_CODE (expr) == COMPLEX_CST)
8811 rpart = TREE_REALPART (expr);
8812 ipart = TREE_IMAGPART (expr);
8814 else
8816 expr = save_expr (expr);
8817 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8818 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8821 rpart = save_expr (rpart);
8822 ipart = save_expr (ipart);
8823 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8824 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8825 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8826 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8827 build_zero_cst (itype));
8831 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8832 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8834 static bool
8835 vec_cst_ctor_to_array (tree arg, tree *elts)
8837 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8839 if (TREE_CODE (arg) == VECTOR_CST)
8841 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8842 elts[i] = VECTOR_CST_ELT (arg, i);
8844 else if (TREE_CODE (arg) == CONSTRUCTOR)
8846 constructor_elt *elt;
8848 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8849 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8850 return false;
8851 else
8852 elts[i] = elt->value;
8854 else
8855 return false;
8856 for (; i < nelts; i++)
8857 elts[i]
8858 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8859 return true;
8862 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8863 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8864 NULL_TREE otherwise. */
8866 static tree
8867 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8869 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8870 tree *elts;
8871 bool need_ctor = false;
8873 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8874 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8875 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8876 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8877 return NULL_TREE;
8879 elts = XALLOCAVEC (tree, nelts * 3);
8880 if (!vec_cst_ctor_to_array (arg0, elts)
8881 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8882 return NULL_TREE;
8884 for (i = 0; i < nelts; i++)
8886 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8887 need_ctor = true;
8888 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8891 if (need_ctor)
8893 vec<constructor_elt, va_gc> *v;
8894 vec_alloc (v, nelts);
8895 for (i = 0; i < nelts; i++)
8896 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8897 return build_constructor (type, v);
8899 else
8900 return build_vector (type, &elts[2 * nelts]);
8903 /* Try to fold a pointer difference of type TYPE two address expressions of
8904 array references AREF0 and AREF1 using location LOC. Return a
8905 simplified expression for the difference or NULL_TREE. */
8907 static tree
8908 fold_addr_of_array_ref_difference (location_t loc, tree type,
8909 tree aref0, tree aref1)
8911 tree base0 = TREE_OPERAND (aref0, 0);
8912 tree base1 = TREE_OPERAND (aref1, 0);
8913 tree base_offset = build_int_cst (type, 0);
8915 /* If the bases are array references as well, recurse. If the bases
8916 are pointer indirections compute the difference of the pointers.
8917 If the bases are equal, we are set. */
8918 if ((TREE_CODE (base0) == ARRAY_REF
8919 && TREE_CODE (base1) == ARRAY_REF
8920 && (base_offset
8921 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8922 || (INDIRECT_REF_P (base0)
8923 && INDIRECT_REF_P (base1)
8924 && (base_offset
8925 = fold_binary_loc (loc, MINUS_EXPR, type,
8926 fold_convert (type, TREE_OPERAND (base0, 0)),
8927 fold_convert (type,
8928 TREE_OPERAND (base1, 0)))))
8929 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
8931 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8932 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8933 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8934 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8935 return fold_build2_loc (loc, PLUS_EXPR, type,
8936 base_offset,
8937 fold_build2_loc (loc, MULT_EXPR, type,
8938 diff, esz));
8940 return NULL_TREE;
8943 /* If the real or vector real constant CST of type TYPE has an exact
8944 inverse, return it, else return NULL. */
8946 tree
8947 exact_inverse (tree type, tree cst)
8949 REAL_VALUE_TYPE r;
8950 tree unit_type, *elts;
8951 machine_mode mode;
8952 unsigned vec_nelts, i;
8954 switch (TREE_CODE (cst))
8956 case REAL_CST:
8957 r = TREE_REAL_CST (cst);
8959 if (exact_real_inverse (TYPE_MODE (type), &r))
8960 return build_real (type, r);
8962 return NULL_TREE;
8964 case VECTOR_CST:
8965 vec_nelts = VECTOR_CST_NELTS (cst);
8966 elts = XALLOCAVEC (tree, vec_nelts);
8967 unit_type = TREE_TYPE (type);
8968 mode = TYPE_MODE (unit_type);
8970 for (i = 0; i < vec_nelts; i++)
8972 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8973 if (!exact_real_inverse (mode, &r))
8974 return NULL_TREE;
8975 elts[i] = build_real (unit_type, r);
8978 return build_vector (type, elts);
8980 default:
8981 return NULL_TREE;
8985 /* Mask out the tz least significant bits of X of type TYPE where
8986 tz is the number of trailing zeroes in Y. */
8987 static wide_int
8988 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
8990 int tz = wi::ctz (y);
8991 if (tz > 0)
8992 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
8993 return x;
8996 /* Return true when T is an address and is known to be nonzero.
8997 For floating point we further ensure that T is not denormal.
8998 Similar logic is present in nonzero_address in rtlanal.h.
9000 If the return value is based on the assumption that signed overflow
9001 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9002 change *STRICT_OVERFLOW_P. */
9004 static bool
9005 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9007 tree type = TREE_TYPE (t);
9008 enum tree_code code;
9010 /* Doing something useful for floating point would need more work. */
9011 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9012 return false;
9014 code = TREE_CODE (t);
9015 switch (TREE_CODE_CLASS (code))
9017 case tcc_unary:
9018 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9019 strict_overflow_p);
9020 case tcc_binary:
9021 case tcc_comparison:
9022 return tree_binary_nonzero_warnv_p (code, type,
9023 TREE_OPERAND (t, 0),
9024 TREE_OPERAND (t, 1),
9025 strict_overflow_p);
9026 case tcc_constant:
9027 case tcc_declaration:
9028 case tcc_reference:
9029 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9031 default:
9032 break;
9035 switch (code)
9037 case TRUTH_NOT_EXPR:
9038 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9039 strict_overflow_p);
9041 case TRUTH_AND_EXPR:
9042 case TRUTH_OR_EXPR:
9043 case TRUTH_XOR_EXPR:
9044 return tree_binary_nonzero_warnv_p (code, type,
9045 TREE_OPERAND (t, 0),
9046 TREE_OPERAND (t, 1),
9047 strict_overflow_p);
9049 case COND_EXPR:
9050 case CONSTRUCTOR:
9051 case OBJ_TYPE_REF:
9052 case ASSERT_EXPR:
9053 case ADDR_EXPR:
9054 case WITH_SIZE_EXPR:
9055 case SSA_NAME:
9056 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9058 case COMPOUND_EXPR:
9059 case MODIFY_EXPR:
9060 case BIND_EXPR:
9061 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9062 strict_overflow_p);
9064 case SAVE_EXPR:
9065 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9066 strict_overflow_p);
9068 case CALL_EXPR:
9070 tree fndecl = get_callee_fndecl (t);
9071 if (!fndecl) return false;
9072 if (flag_delete_null_pointer_checks && !flag_check_new
9073 && DECL_IS_OPERATOR_NEW (fndecl)
9074 && !TREE_NOTHROW (fndecl))
9075 return true;
9076 if (flag_delete_null_pointer_checks
9077 && lookup_attribute ("returns_nonnull",
9078 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9079 return true;
9080 return alloca_call_p (t);
9083 default:
9084 break;
9086 return false;
9089 /* Return true when T is an address and is known to be nonzero.
9090 Handle warnings about undefined signed overflow. */
9092 static bool
9093 tree_expr_nonzero_p (tree t)
9095 bool ret, strict_overflow_p;
9097 strict_overflow_p = false;
9098 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9099 if (strict_overflow_p)
9100 fold_overflow_warning (("assuming signed overflow does not occur when "
9101 "determining that expression is always "
9102 "non-zero"),
9103 WARN_STRICT_OVERFLOW_MISC);
9104 return ret;
9107 /* Return true if T is known not to be equal to an integer W. */
9109 bool
9110 expr_not_equal_to (tree t, const wide_int &w)
9112 wide_int min, max, nz;
9113 value_range_type rtype;
9114 switch (TREE_CODE (t))
9116 case INTEGER_CST:
9117 return wi::ne_p (t, w);
9119 case SSA_NAME:
9120 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9121 return false;
9122 rtype = get_range_info (t, &min, &max);
9123 if (rtype == VR_RANGE)
9125 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9126 return true;
9127 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9128 return true;
9130 else if (rtype == VR_ANTI_RANGE
9131 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9132 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9133 return true;
9134 /* If T has some known zero bits and W has any of those bits set,
9135 then T is known not to be equal to W. */
9136 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9137 TYPE_PRECISION (TREE_TYPE (t))), 0))
9138 return true;
9139 return false;
9141 default:
9142 return false;
9146 /* Fold a binary expression of code CODE and type TYPE with operands
9147 OP0 and OP1. LOC is the location of the resulting expression.
9148 Return the folded expression if folding is successful. Otherwise,
9149 return NULL_TREE. */
9151 tree
9152 fold_binary_loc (location_t loc,
9153 enum tree_code code, tree type, tree op0, tree op1)
9155 enum tree_code_class kind = TREE_CODE_CLASS (code);
9156 tree arg0, arg1, tem;
9157 tree t1 = NULL_TREE;
9158 bool strict_overflow_p;
9159 unsigned int prec;
9161 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9162 && TREE_CODE_LENGTH (code) == 2
9163 && op0 != NULL_TREE
9164 && op1 != NULL_TREE);
9166 arg0 = op0;
9167 arg1 = op1;
9169 /* Strip any conversions that don't change the mode. This is
9170 safe for every expression, except for a comparison expression
9171 because its signedness is derived from its operands. So, in
9172 the latter case, only strip conversions that don't change the
9173 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9174 preserved.
9176 Note that this is done as an internal manipulation within the
9177 constant folder, in order to find the simplest representation
9178 of the arguments so that their form can be studied. In any
9179 cases, the appropriate type conversions should be put back in
9180 the tree that will get out of the constant folder. */
9182 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9184 STRIP_SIGN_NOPS (arg0);
9185 STRIP_SIGN_NOPS (arg1);
9187 else
9189 STRIP_NOPS (arg0);
9190 STRIP_NOPS (arg1);
9193 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9194 constant but we can't do arithmetic on them. */
9195 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9197 tem = const_binop (code, type, arg0, arg1);
9198 if (tem != NULL_TREE)
9200 if (TREE_TYPE (tem) != type)
9201 tem = fold_convert_loc (loc, type, tem);
9202 return tem;
9206 /* If this is a commutative operation, and ARG0 is a constant, move it
9207 to ARG1 to reduce the number of tests below. */
9208 if (commutative_tree_code (code)
9209 && tree_swap_operands_p (arg0, arg1, true))
9210 return fold_build2_loc (loc, code, type, op1, op0);
9212 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9213 to ARG1 to reduce the number of tests below. */
9214 if (kind == tcc_comparison
9215 && tree_swap_operands_p (arg0, arg1, true))
9216 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9218 tem = generic_simplify (loc, code, type, op0, op1);
9219 if (tem)
9220 return tem;
9222 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9224 First check for cases where an arithmetic operation is applied to a
9225 compound, conditional, or comparison operation. Push the arithmetic
9226 operation inside the compound or conditional to see if any folding
9227 can then be done. Convert comparison to conditional for this purpose.
9228 The also optimizes non-constant cases that used to be done in
9229 expand_expr.
9231 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9232 one of the operands is a comparison and the other is a comparison, a
9233 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9234 code below would make the expression more complex. Change it to a
9235 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9236 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9238 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9239 || code == EQ_EXPR || code == NE_EXPR)
9240 && TREE_CODE (type) != VECTOR_TYPE
9241 && ((truth_value_p (TREE_CODE (arg0))
9242 && (truth_value_p (TREE_CODE (arg1))
9243 || (TREE_CODE (arg1) == BIT_AND_EXPR
9244 && integer_onep (TREE_OPERAND (arg1, 1)))))
9245 || (truth_value_p (TREE_CODE (arg1))
9246 && (truth_value_p (TREE_CODE (arg0))
9247 || (TREE_CODE (arg0) == BIT_AND_EXPR
9248 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9250 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9251 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9252 : TRUTH_XOR_EXPR,
9253 boolean_type_node,
9254 fold_convert_loc (loc, boolean_type_node, arg0),
9255 fold_convert_loc (loc, boolean_type_node, arg1));
9257 if (code == EQ_EXPR)
9258 tem = invert_truthvalue_loc (loc, tem);
9260 return fold_convert_loc (loc, type, tem);
9263 if (TREE_CODE_CLASS (code) == tcc_binary
9264 || TREE_CODE_CLASS (code) == tcc_comparison)
9266 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9268 tem = fold_build2_loc (loc, code, type,
9269 fold_convert_loc (loc, TREE_TYPE (op0),
9270 TREE_OPERAND (arg0, 1)), op1);
9271 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9272 tem);
9274 if (TREE_CODE (arg1) == COMPOUND_EXPR
9275 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9277 tem = fold_build2_loc (loc, code, type, op0,
9278 fold_convert_loc (loc, TREE_TYPE (op1),
9279 TREE_OPERAND (arg1, 1)));
9280 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9281 tem);
9284 if (TREE_CODE (arg0) == COND_EXPR
9285 || TREE_CODE (arg0) == VEC_COND_EXPR
9286 || COMPARISON_CLASS_P (arg0))
9288 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9289 arg0, arg1,
9290 /*cond_first_p=*/1);
9291 if (tem != NULL_TREE)
9292 return tem;
9295 if (TREE_CODE (arg1) == COND_EXPR
9296 || TREE_CODE (arg1) == VEC_COND_EXPR
9297 || COMPARISON_CLASS_P (arg1))
9299 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9300 arg1, arg0,
9301 /*cond_first_p=*/0);
9302 if (tem != NULL_TREE)
9303 return tem;
9307 switch (code)
9309 case MEM_REF:
9310 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9311 if (TREE_CODE (arg0) == ADDR_EXPR
9312 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9314 tree iref = TREE_OPERAND (arg0, 0);
9315 return fold_build2 (MEM_REF, type,
9316 TREE_OPERAND (iref, 0),
9317 int_const_binop (PLUS_EXPR, arg1,
9318 TREE_OPERAND (iref, 1)));
9321 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9322 if (TREE_CODE (arg0) == ADDR_EXPR
9323 && handled_component_p (TREE_OPERAND (arg0, 0)))
9325 tree base;
9326 HOST_WIDE_INT coffset;
9327 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9328 &coffset);
9329 if (!base)
9330 return NULL_TREE;
9331 return fold_build2 (MEM_REF, type,
9332 build_fold_addr_expr (base),
9333 int_const_binop (PLUS_EXPR, arg1,
9334 size_int (coffset)));
9337 return NULL_TREE;
9339 case POINTER_PLUS_EXPR:
9340 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9341 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9342 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9343 return fold_convert_loc (loc, type,
9344 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9345 fold_convert_loc (loc, sizetype,
9346 arg1),
9347 fold_convert_loc (loc, sizetype,
9348 arg0)));
9350 return NULL_TREE;
9352 case PLUS_EXPR:
9353 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9355 /* X + (X / CST) * -CST is X % CST. */
9356 if (TREE_CODE (arg1) == MULT_EXPR
9357 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9358 && operand_equal_p (arg0,
9359 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9361 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9362 tree cst1 = TREE_OPERAND (arg1, 1);
9363 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9364 cst1, cst0);
9365 if (sum && integer_zerop (sum))
9366 return fold_convert_loc (loc, type,
9367 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9368 TREE_TYPE (arg0), arg0,
9369 cst0));
9373 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9374 one. Make sure the type is not saturating and has the signedness of
9375 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9376 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9377 if ((TREE_CODE (arg0) == MULT_EXPR
9378 || TREE_CODE (arg1) == MULT_EXPR)
9379 && !TYPE_SATURATING (type)
9380 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9381 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9382 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9384 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9385 if (tem)
9386 return tem;
9389 if (! FLOAT_TYPE_P (type))
9391 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9392 (plus (plus (mult) (mult)) (foo)) so that we can
9393 take advantage of the factoring cases below. */
9394 if (ANY_INTEGRAL_TYPE_P (type)
9395 && TYPE_OVERFLOW_WRAPS (type)
9396 && (((TREE_CODE (arg0) == PLUS_EXPR
9397 || TREE_CODE (arg0) == MINUS_EXPR)
9398 && TREE_CODE (arg1) == MULT_EXPR)
9399 || ((TREE_CODE (arg1) == PLUS_EXPR
9400 || TREE_CODE (arg1) == MINUS_EXPR)
9401 && TREE_CODE (arg0) == MULT_EXPR)))
9403 tree parg0, parg1, parg, marg;
9404 enum tree_code pcode;
9406 if (TREE_CODE (arg1) == MULT_EXPR)
9407 parg = arg0, marg = arg1;
9408 else
9409 parg = arg1, marg = arg0;
9410 pcode = TREE_CODE (parg);
9411 parg0 = TREE_OPERAND (parg, 0);
9412 parg1 = TREE_OPERAND (parg, 1);
9413 STRIP_NOPS (parg0);
9414 STRIP_NOPS (parg1);
9416 if (TREE_CODE (parg0) == MULT_EXPR
9417 && TREE_CODE (parg1) != MULT_EXPR)
9418 return fold_build2_loc (loc, pcode, type,
9419 fold_build2_loc (loc, PLUS_EXPR, type,
9420 fold_convert_loc (loc, type,
9421 parg0),
9422 fold_convert_loc (loc, type,
9423 marg)),
9424 fold_convert_loc (loc, type, parg1));
9425 if (TREE_CODE (parg0) != MULT_EXPR
9426 && TREE_CODE (parg1) == MULT_EXPR)
9427 return
9428 fold_build2_loc (loc, PLUS_EXPR, type,
9429 fold_convert_loc (loc, type, parg0),
9430 fold_build2_loc (loc, pcode, type,
9431 fold_convert_loc (loc, type, marg),
9432 fold_convert_loc (loc, type,
9433 parg1)));
9436 else
9438 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9439 to __complex__ ( x, y ). This is not the same for SNaNs or
9440 if signed zeros are involved. */
9441 if (!HONOR_SNANS (element_mode (arg0))
9442 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9443 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9445 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9446 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9447 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9448 bool arg0rz = false, arg0iz = false;
9449 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9450 || (arg0i && (arg0iz = real_zerop (arg0i))))
9452 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9453 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9454 if (arg0rz && arg1i && real_zerop (arg1i))
9456 tree rp = arg1r ? arg1r
9457 : build1 (REALPART_EXPR, rtype, arg1);
9458 tree ip = arg0i ? arg0i
9459 : build1 (IMAGPART_EXPR, rtype, arg0);
9460 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9462 else if (arg0iz && arg1r && real_zerop (arg1r))
9464 tree rp = arg0r ? arg0r
9465 : build1 (REALPART_EXPR, rtype, arg0);
9466 tree ip = arg1i ? arg1i
9467 : build1 (IMAGPART_EXPR, rtype, arg1);
9468 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9473 if (flag_unsafe_math_optimizations
9474 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9475 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9476 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9477 return tem;
9479 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9480 We associate floats only if the user has specified
9481 -fassociative-math. */
9482 if (flag_associative_math
9483 && TREE_CODE (arg1) == PLUS_EXPR
9484 && TREE_CODE (arg0) != MULT_EXPR)
9486 tree tree10 = TREE_OPERAND (arg1, 0);
9487 tree tree11 = TREE_OPERAND (arg1, 1);
9488 if (TREE_CODE (tree11) == MULT_EXPR
9489 && TREE_CODE (tree10) == MULT_EXPR)
9491 tree tree0;
9492 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9493 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9496 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9497 We associate floats only if the user has specified
9498 -fassociative-math. */
9499 if (flag_associative_math
9500 && TREE_CODE (arg0) == PLUS_EXPR
9501 && TREE_CODE (arg1) != MULT_EXPR)
9503 tree tree00 = TREE_OPERAND (arg0, 0);
9504 tree tree01 = TREE_OPERAND (arg0, 1);
9505 if (TREE_CODE (tree01) == MULT_EXPR
9506 && TREE_CODE (tree00) == MULT_EXPR)
9508 tree tree0;
9509 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9510 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9515 bit_rotate:
9516 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9517 is a rotate of A by C1 bits. */
9518 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9519 is a rotate of A by B bits. */
9521 enum tree_code code0, code1;
9522 tree rtype;
9523 code0 = TREE_CODE (arg0);
9524 code1 = TREE_CODE (arg1);
9525 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9526 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9527 && operand_equal_p (TREE_OPERAND (arg0, 0),
9528 TREE_OPERAND (arg1, 0), 0)
9529 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9530 TYPE_UNSIGNED (rtype))
9531 /* Only create rotates in complete modes. Other cases are not
9532 expanded properly. */
9533 && (element_precision (rtype)
9534 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9536 tree tree01, tree11;
9537 enum tree_code code01, code11;
9539 tree01 = TREE_OPERAND (arg0, 1);
9540 tree11 = TREE_OPERAND (arg1, 1);
9541 STRIP_NOPS (tree01);
9542 STRIP_NOPS (tree11);
9543 code01 = TREE_CODE (tree01);
9544 code11 = TREE_CODE (tree11);
9545 if (code01 == INTEGER_CST
9546 && code11 == INTEGER_CST
9547 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9548 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9550 tem = build2_loc (loc, LROTATE_EXPR,
9551 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9552 TREE_OPERAND (arg0, 0),
9553 code0 == LSHIFT_EXPR
9554 ? TREE_OPERAND (arg0, 1)
9555 : TREE_OPERAND (arg1, 1));
9556 return fold_convert_loc (loc, type, tem);
9558 else if (code11 == MINUS_EXPR)
9560 tree tree110, tree111;
9561 tree110 = TREE_OPERAND (tree11, 0);
9562 tree111 = TREE_OPERAND (tree11, 1);
9563 STRIP_NOPS (tree110);
9564 STRIP_NOPS (tree111);
9565 if (TREE_CODE (tree110) == INTEGER_CST
9566 && 0 == compare_tree_int (tree110,
9567 element_precision
9568 (TREE_TYPE (TREE_OPERAND
9569 (arg0, 0))))
9570 && operand_equal_p (tree01, tree111, 0))
9571 return
9572 fold_convert_loc (loc, type,
9573 build2 ((code0 == LSHIFT_EXPR
9574 ? LROTATE_EXPR
9575 : RROTATE_EXPR),
9576 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9577 TREE_OPERAND (arg0, 0),
9578 TREE_OPERAND (arg0, 1)));
9580 else if (code01 == MINUS_EXPR)
9582 tree tree010, tree011;
9583 tree010 = TREE_OPERAND (tree01, 0);
9584 tree011 = TREE_OPERAND (tree01, 1);
9585 STRIP_NOPS (tree010);
9586 STRIP_NOPS (tree011);
9587 if (TREE_CODE (tree010) == INTEGER_CST
9588 && 0 == compare_tree_int (tree010,
9589 element_precision
9590 (TREE_TYPE (TREE_OPERAND
9591 (arg0, 0))))
9592 && operand_equal_p (tree11, tree011, 0))
9593 return fold_convert_loc
9594 (loc, type,
9595 build2 ((code0 != LSHIFT_EXPR
9596 ? LROTATE_EXPR
9597 : RROTATE_EXPR),
9598 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9599 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9604 associate:
9605 /* In most languages, can't associate operations on floats through
9606 parentheses. Rather than remember where the parentheses were, we
9607 don't associate floats at all, unless the user has specified
9608 -fassociative-math.
9609 And, we need to make sure type is not saturating. */
9611 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9612 && !TYPE_SATURATING (type))
9614 tree var0, con0, lit0, minus_lit0;
9615 tree var1, con1, lit1, minus_lit1;
9616 tree atype = type;
9617 bool ok = true;
9619 /* Split both trees into variables, constants, and literals. Then
9620 associate each group together, the constants with literals,
9621 then the result with variables. This increases the chances of
9622 literals being recombined later and of generating relocatable
9623 expressions for the sum of a constant and literal. */
9624 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9625 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9626 code == MINUS_EXPR);
9628 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9629 if (code == MINUS_EXPR)
9630 code = PLUS_EXPR;
9632 /* With undefined overflow prefer doing association in a type
9633 which wraps on overflow, if that is one of the operand types. */
9634 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9635 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9637 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9638 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9639 atype = TREE_TYPE (arg0);
9640 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9641 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9642 atype = TREE_TYPE (arg1);
9643 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9646 /* With undefined overflow we can only associate constants with one
9647 variable, and constants whose association doesn't overflow. */
9648 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9649 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9651 if (var0 && var1)
9653 tree tmp0 = var0;
9654 tree tmp1 = var1;
9655 bool one_neg = false;
9657 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9659 tmp0 = TREE_OPERAND (tmp0, 0);
9660 one_neg = !one_neg;
9662 if (CONVERT_EXPR_P (tmp0)
9663 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9664 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9665 <= TYPE_PRECISION (atype)))
9666 tmp0 = TREE_OPERAND (tmp0, 0);
9667 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9669 tmp1 = TREE_OPERAND (tmp1, 0);
9670 one_neg = !one_neg;
9672 if (CONVERT_EXPR_P (tmp1)
9673 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9674 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9675 <= TYPE_PRECISION (atype)))
9676 tmp1 = TREE_OPERAND (tmp1, 0);
9677 /* The only case we can still associate with two variables
9678 is if they cancel out. */
9679 if (!one_neg
9680 || !operand_equal_p (tmp0, tmp1, 0))
9681 ok = false;
9685 /* Only do something if we found more than two objects. Otherwise,
9686 nothing has changed and we risk infinite recursion. */
9687 if (ok
9688 && (2 < ((var0 != 0) + (var1 != 0)
9689 + (con0 != 0) + (con1 != 0)
9690 + (lit0 != 0) + (lit1 != 0)
9691 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9693 bool any_overflows = false;
9694 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
9695 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
9696 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
9697 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
9698 var0 = associate_trees (loc, var0, var1, code, atype);
9699 con0 = associate_trees (loc, con0, con1, code, atype);
9700 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9701 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9702 code, atype);
9704 /* Preserve the MINUS_EXPR if the negative part of the literal is
9705 greater than the positive part. Otherwise, the multiplicative
9706 folding code (i.e extract_muldiv) may be fooled in case
9707 unsigned constants are subtracted, like in the following
9708 example: ((X*2 + 4) - 8U)/2. */
9709 if (minus_lit0 && lit0)
9711 if (TREE_CODE (lit0) == INTEGER_CST
9712 && TREE_CODE (minus_lit0) == INTEGER_CST
9713 && tree_int_cst_lt (lit0, minus_lit0))
9715 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9716 MINUS_EXPR, atype);
9717 lit0 = 0;
9719 else
9721 lit0 = associate_trees (loc, lit0, minus_lit0,
9722 MINUS_EXPR, atype);
9723 minus_lit0 = 0;
9727 /* Don't introduce overflows through reassociation. */
9728 if (!any_overflows
9729 && ((lit0 && TREE_OVERFLOW_P (lit0))
9730 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
9731 return NULL_TREE;
9733 if (minus_lit0)
9735 if (con0 == 0)
9736 return
9737 fold_convert_loc (loc, type,
9738 associate_trees (loc, var0, minus_lit0,
9739 MINUS_EXPR, atype));
9740 else
9742 con0 = associate_trees (loc, con0, minus_lit0,
9743 MINUS_EXPR, atype);
9744 return
9745 fold_convert_loc (loc, type,
9746 associate_trees (loc, var0, con0,
9747 PLUS_EXPR, atype));
9751 con0 = associate_trees (loc, con0, lit0, code, atype);
9752 return
9753 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9754 code, atype));
9758 return NULL_TREE;
9760 case MINUS_EXPR:
9761 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9762 if (TREE_CODE (arg0) == NEGATE_EXPR
9763 && negate_expr_p (op1)
9764 && reorder_operands_p (arg0, arg1))
9765 return fold_build2_loc (loc, MINUS_EXPR, type,
9766 negate_expr (op1),
9767 fold_convert_loc (loc, type,
9768 TREE_OPERAND (arg0, 0)));
9770 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9771 __complex__ ( x, -y ). This is not the same for SNaNs or if
9772 signed zeros are involved. */
9773 if (!HONOR_SNANS (element_mode (arg0))
9774 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9775 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9777 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9778 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9779 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9780 bool arg0rz = false, arg0iz = false;
9781 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9782 || (arg0i && (arg0iz = real_zerop (arg0i))))
9784 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9785 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9786 if (arg0rz && arg1i && real_zerop (arg1i))
9788 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9789 arg1r ? arg1r
9790 : build1 (REALPART_EXPR, rtype, arg1));
9791 tree ip = arg0i ? arg0i
9792 : build1 (IMAGPART_EXPR, rtype, arg0);
9793 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9795 else if (arg0iz && arg1r && real_zerop (arg1r))
9797 tree rp = arg0r ? arg0r
9798 : build1 (REALPART_EXPR, rtype, arg0);
9799 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9800 arg1i ? arg1i
9801 : build1 (IMAGPART_EXPR, rtype, arg1));
9802 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9807 /* A - B -> A + (-B) if B is easily negatable. */
9808 if (negate_expr_p (op1)
9809 && ! TYPE_OVERFLOW_SANITIZED (type)
9810 && ((FLOAT_TYPE_P (type)
9811 /* Avoid this transformation if B is a positive REAL_CST. */
9812 && (TREE_CODE (op1) != REAL_CST
9813 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9814 || INTEGRAL_TYPE_P (type)))
9815 return fold_build2_loc (loc, PLUS_EXPR, type,
9816 fold_convert_loc (loc, type, arg0),
9817 negate_expr (op1));
9819 /* Fold &a[i] - &a[j] to i-j. */
9820 if (TREE_CODE (arg0) == ADDR_EXPR
9821 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9822 && TREE_CODE (arg1) == ADDR_EXPR
9823 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9825 tree tem = fold_addr_of_array_ref_difference (loc, type,
9826 TREE_OPERAND (arg0, 0),
9827 TREE_OPERAND (arg1, 0));
9828 if (tem)
9829 return tem;
9832 if (FLOAT_TYPE_P (type)
9833 && flag_unsafe_math_optimizations
9834 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9835 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9836 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9837 return tem;
9839 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9840 one. Make sure the type is not saturating and has the signedness of
9841 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9842 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9843 if ((TREE_CODE (arg0) == MULT_EXPR
9844 || TREE_CODE (arg1) == MULT_EXPR)
9845 && !TYPE_SATURATING (type)
9846 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9847 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9848 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9850 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9851 if (tem)
9852 return tem;
9855 goto associate;
9857 case MULT_EXPR:
9858 if (! FLOAT_TYPE_P (type))
9860 /* Transform x * -C into -x * C if x is easily negatable. */
9861 if (TREE_CODE (op1) == INTEGER_CST
9862 && tree_int_cst_sgn (op1) == -1
9863 && negate_expr_p (op0)
9864 && (tem = negate_expr (op1)) != op1
9865 && ! TREE_OVERFLOW (tem))
9866 return fold_build2_loc (loc, MULT_EXPR, type,
9867 fold_convert_loc (loc, type,
9868 negate_expr (op0)), tem);
9870 /* (A + A) * C -> A * 2 * C */
9871 if (TREE_CODE (arg0) == PLUS_EXPR
9872 && TREE_CODE (arg1) == INTEGER_CST
9873 && operand_equal_p (TREE_OPERAND (arg0, 0),
9874 TREE_OPERAND (arg0, 1), 0))
9875 return fold_build2_loc (loc, MULT_EXPR, type,
9876 omit_one_operand_loc (loc, type,
9877 TREE_OPERAND (arg0, 0),
9878 TREE_OPERAND (arg0, 1)),
9879 fold_build2_loc (loc, MULT_EXPR, type,
9880 build_int_cst (type, 2) , arg1));
9882 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
9883 sign-changing only. */
9884 if (TREE_CODE (arg1) == INTEGER_CST
9885 && TREE_CODE (arg0) == EXACT_DIV_EXPR
9886 && operand_equal_p (arg1, TREE_OPERAND (arg0, 1), 0))
9887 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9889 strict_overflow_p = false;
9890 if (TREE_CODE (arg1) == INTEGER_CST
9891 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9892 &strict_overflow_p)))
9894 if (strict_overflow_p)
9895 fold_overflow_warning (("assuming signed overflow does not "
9896 "occur when simplifying "
9897 "multiplication"),
9898 WARN_STRICT_OVERFLOW_MISC);
9899 return fold_convert_loc (loc, type, tem);
9902 /* Optimize z * conj(z) for integer complex numbers. */
9903 if (TREE_CODE (arg0) == CONJ_EXPR
9904 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9905 return fold_mult_zconjz (loc, type, arg1);
9906 if (TREE_CODE (arg1) == CONJ_EXPR
9907 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9908 return fold_mult_zconjz (loc, type, arg0);
9910 else
9912 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9913 This is not the same for NaNs or if signed zeros are
9914 involved. */
9915 if (!HONOR_NANS (arg0)
9916 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9917 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9918 && TREE_CODE (arg1) == COMPLEX_CST
9919 && real_zerop (TREE_REALPART (arg1)))
9921 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9922 if (real_onep (TREE_IMAGPART (arg1)))
9923 return
9924 fold_build2_loc (loc, COMPLEX_EXPR, type,
9925 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9926 rtype, arg0)),
9927 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9928 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9929 return
9930 fold_build2_loc (loc, COMPLEX_EXPR, type,
9931 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9932 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9933 rtype, arg0)));
9936 /* Optimize z * conj(z) for floating point complex numbers.
9937 Guarded by flag_unsafe_math_optimizations as non-finite
9938 imaginary components don't produce scalar results. */
9939 if (flag_unsafe_math_optimizations
9940 && TREE_CODE (arg0) == CONJ_EXPR
9941 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9942 return fold_mult_zconjz (loc, type, arg1);
9943 if (flag_unsafe_math_optimizations
9944 && TREE_CODE (arg1) == CONJ_EXPR
9945 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9946 return fold_mult_zconjz (loc, type, arg0);
9948 if (flag_unsafe_math_optimizations)
9951 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
9952 if (!in_gimple_form
9953 && optimize
9954 && operand_equal_p (arg0, arg1, 0))
9956 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
9958 if (powfn)
9960 tree arg = build_real (type, dconst2);
9961 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
9966 goto associate;
9968 case BIT_IOR_EXPR:
9969 /* Canonicalize (X & C1) | C2. */
9970 if (TREE_CODE (arg0) == BIT_AND_EXPR
9971 && TREE_CODE (arg1) == INTEGER_CST
9972 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9974 int width = TYPE_PRECISION (type), w;
9975 wide_int c1 = TREE_OPERAND (arg0, 1);
9976 wide_int c2 = arg1;
9978 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9979 if ((c1 & c2) == c1)
9980 return omit_one_operand_loc (loc, type, arg1,
9981 TREE_OPERAND (arg0, 0));
9983 wide_int msk = wi::mask (width, false,
9984 TYPE_PRECISION (TREE_TYPE (arg1)));
9986 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9987 if (msk.and_not (c1 | c2) == 0)
9988 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9989 TREE_OPERAND (arg0, 0), arg1);
9991 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9992 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9993 mode which allows further optimizations. */
9994 c1 &= msk;
9995 c2 &= msk;
9996 wide_int c3 = c1.and_not (c2);
9997 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
9999 wide_int mask = wi::mask (w, false,
10000 TYPE_PRECISION (type));
10001 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
10003 c3 = mask;
10004 break;
10008 if (c3 != c1)
10009 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
10010 fold_build2_loc (loc, BIT_AND_EXPR, type,
10011 TREE_OPERAND (arg0, 0),
10012 wide_int_to_tree (type,
10013 c3)),
10014 arg1);
10017 /* See if this can be simplified into a rotate first. If that
10018 is unsuccessful continue in the association code. */
10019 goto bit_rotate;
10021 case BIT_XOR_EXPR:
10022 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10023 if (TREE_CODE (arg0) == BIT_AND_EXPR
10024 && INTEGRAL_TYPE_P (type)
10025 && integer_onep (TREE_OPERAND (arg0, 1))
10026 && integer_onep (arg1))
10027 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10028 build_zero_cst (TREE_TYPE (arg0)));
10030 /* See if this can be simplified into a rotate first. If that
10031 is unsuccessful continue in the association code. */
10032 goto bit_rotate;
10034 case BIT_AND_EXPR:
10035 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10036 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10037 && INTEGRAL_TYPE_P (type)
10038 && integer_onep (TREE_OPERAND (arg0, 1))
10039 && integer_onep (arg1))
10041 tree tem2;
10042 tem = TREE_OPERAND (arg0, 0);
10043 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10044 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10045 tem, tem2);
10046 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10047 build_zero_cst (TREE_TYPE (tem)));
10049 /* Fold ~X & 1 as (X & 1) == 0. */
10050 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10051 && INTEGRAL_TYPE_P (type)
10052 && integer_onep (arg1))
10054 tree tem2;
10055 tem = TREE_OPERAND (arg0, 0);
10056 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10057 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10058 tem, tem2);
10059 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10060 build_zero_cst (TREE_TYPE (tem)));
10062 /* Fold !X & 1 as X == 0. */
10063 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10064 && integer_onep (arg1))
10066 tem = TREE_OPERAND (arg0, 0);
10067 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10068 build_zero_cst (TREE_TYPE (tem)));
10071 /* Fold (X ^ Y) & Y as ~X & Y. */
10072 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10073 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10075 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10076 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10077 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10078 fold_convert_loc (loc, type, arg1));
10080 /* Fold (X ^ Y) & X as ~Y & X. */
10081 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10082 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10083 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10085 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10086 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10087 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10088 fold_convert_loc (loc, type, arg1));
10090 /* Fold X & (X ^ Y) as X & ~Y. */
10091 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10092 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10094 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10095 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10096 fold_convert_loc (loc, type, arg0),
10097 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10099 /* Fold X & (Y ^ X) as ~Y & X. */
10100 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10101 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10102 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10104 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10105 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10106 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10107 fold_convert_loc (loc, type, arg0));
10110 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10111 multiple of 1 << CST. */
10112 if (TREE_CODE (arg1) == INTEGER_CST)
10114 wide_int cst1 = arg1;
10115 wide_int ncst1 = -cst1;
10116 if ((cst1 & ncst1) == ncst1
10117 && multiple_of_p (type, arg0,
10118 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10119 return fold_convert_loc (loc, type, arg0);
10122 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10123 bits from CST2. */
10124 if (TREE_CODE (arg1) == INTEGER_CST
10125 && TREE_CODE (arg0) == MULT_EXPR
10126 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10128 wide_int warg1 = arg1;
10129 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
10131 if (masked == 0)
10132 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10133 arg0, arg1);
10134 else if (masked != warg1)
10136 /* Avoid the transform if arg1 is a mask of some
10137 mode which allows further optimizations. */
10138 int pop = wi::popcount (warg1);
10139 if (!(pop >= BITS_PER_UNIT
10140 && exact_log2 (pop) != -1
10141 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10142 return fold_build2_loc (loc, code, type, op0,
10143 wide_int_to_tree (type, masked));
10147 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10148 ((A & N) + B) & M -> (A + B) & M
10149 Similarly if (N & M) == 0,
10150 ((A | N) + B) & M -> (A + B) & M
10151 and for - instead of + (or unary - instead of +)
10152 and/or ^ instead of |.
10153 If B is constant and (B & M) == 0, fold into A & M. */
10154 if (TREE_CODE (arg1) == INTEGER_CST)
10156 wide_int cst1 = arg1;
10157 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10158 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10159 && (TREE_CODE (arg0) == PLUS_EXPR
10160 || TREE_CODE (arg0) == MINUS_EXPR
10161 || TREE_CODE (arg0) == NEGATE_EXPR)
10162 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10163 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10165 tree pmop[2];
10166 int which = 0;
10167 wide_int cst0;
10169 /* Now we know that arg0 is (C + D) or (C - D) or
10170 -C and arg1 (M) is == (1LL << cst) - 1.
10171 Store C into PMOP[0] and D into PMOP[1]. */
10172 pmop[0] = TREE_OPERAND (arg0, 0);
10173 pmop[1] = NULL;
10174 if (TREE_CODE (arg0) != NEGATE_EXPR)
10176 pmop[1] = TREE_OPERAND (arg0, 1);
10177 which = 1;
10180 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10181 which = -1;
10183 for (; which >= 0; which--)
10184 switch (TREE_CODE (pmop[which]))
10186 case BIT_AND_EXPR:
10187 case BIT_IOR_EXPR:
10188 case BIT_XOR_EXPR:
10189 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10190 != INTEGER_CST)
10191 break;
10192 cst0 = TREE_OPERAND (pmop[which], 1);
10193 cst0 &= cst1;
10194 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10196 if (cst0 != cst1)
10197 break;
10199 else if (cst0 != 0)
10200 break;
10201 /* If C or D is of the form (A & N) where
10202 (N & M) == M, or of the form (A | N) or
10203 (A ^ N) where (N & M) == 0, replace it with A. */
10204 pmop[which] = TREE_OPERAND (pmop[which], 0);
10205 break;
10206 case INTEGER_CST:
10207 /* If C or D is a N where (N & M) == 0, it can be
10208 omitted (assumed 0). */
10209 if ((TREE_CODE (arg0) == PLUS_EXPR
10210 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10211 && (cst1 & pmop[which]) == 0)
10212 pmop[which] = NULL;
10213 break;
10214 default:
10215 break;
10218 /* Only build anything new if we optimized one or both arguments
10219 above. */
10220 if (pmop[0] != TREE_OPERAND (arg0, 0)
10221 || (TREE_CODE (arg0) != NEGATE_EXPR
10222 && pmop[1] != TREE_OPERAND (arg0, 1)))
10224 tree utype = TREE_TYPE (arg0);
10225 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10227 /* Perform the operations in a type that has defined
10228 overflow behavior. */
10229 utype = unsigned_type_for (TREE_TYPE (arg0));
10230 if (pmop[0] != NULL)
10231 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10232 if (pmop[1] != NULL)
10233 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10236 if (TREE_CODE (arg0) == NEGATE_EXPR)
10237 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10238 else if (TREE_CODE (arg0) == PLUS_EXPR)
10240 if (pmop[0] != NULL && pmop[1] != NULL)
10241 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10242 pmop[0], pmop[1]);
10243 else if (pmop[0] != NULL)
10244 tem = pmop[0];
10245 else if (pmop[1] != NULL)
10246 tem = pmop[1];
10247 else
10248 return build_int_cst (type, 0);
10250 else if (pmop[0] == NULL)
10251 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10252 else
10253 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10254 pmop[0], pmop[1]);
10255 /* TEM is now the new binary +, - or unary - replacement. */
10256 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10257 fold_convert_loc (loc, utype, arg1));
10258 return fold_convert_loc (loc, type, tem);
10263 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10264 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10265 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10267 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10269 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10270 if (mask == -1)
10271 return
10272 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10275 goto associate;
10277 case RDIV_EXPR:
10278 /* Don't touch a floating-point divide by zero unless the mode
10279 of the constant can represent infinity. */
10280 if (TREE_CODE (arg1) == REAL_CST
10281 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10282 && real_zerop (arg1))
10283 return NULL_TREE;
10285 /* (-A) / (-B) -> A / B */
10286 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10287 return fold_build2_loc (loc, RDIV_EXPR, type,
10288 TREE_OPERAND (arg0, 0),
10289 negate_expr (arg1));
10290 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10291 return fold_build2_loc (loc, RDIV_EXPR, type,
10292 negate_expr (arg0),
10293 TREE_OPERAND (arg1, 0));
10294 return NULL_TREE;
10296 case TRUNC_DIV_EXPR:
10297 /* Fall through */
10299 case FLOOR_DIV_EXPR:
10300 /* Simplify A / (B << N) where A and B are positive and B is
10301 a power of 2, to A >> (N + log2(B)). */
10302 strict_overflow_p = false;
10303 if (TREE_CODE (arg1) == LSHIFT_EXPR
10304 && (TYPE_UNSIGNED (type)
10305 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10307 tree sval = TREE_OPERAND (arg1, 0);
10308 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10310 tree sh_cnt = TREE_OPERAND (arg1, 1);
10311 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10312 wi::exact_log2 (sval));
10314 if (strict_overflow_p)
10315 fold_overflow_warning (("assuming signed overflow does not "
10316 "occur when simplifying A / (B << N)"),
10317 WARN_STRICT_OVERFLOW_MISC);
10319 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10320 sh_cnt, pow2);
10321 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10322 fold_convert_loc (loc, type, arg0), sh_cnt);
10326 /* Fall through */
10328 case ROUND_DIV_EXPR:
10329 case CEIL_DIV_EXPR:
10330 case EXACT_DIV_EXPR:
10331 if (integer_zerop (arg1))
10332 return NULL_TREE;
10334 /* Convert -A / -B to A / B when the type is signed and overflow is
10335 undefined. */
10336 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10337 && TREE_CODE (arg0) == NEGATE_EXPR
10338 && negate_expr_p (op1))
10340 if (INTEGRAL_TYPE_P (type))
10341 fold_overflow_warning (("assuming signed overflow does not occur "
10342 "when distributing negation across "
10343 "division"),
10344 WARN_STRICT_OVERFLOW_MISC);
10345 return fold_build2_loc (loc, code, type,
10346 fold_convert_loc (loc, type,
10347 TREE_OPERAND (arg0, 0)),
10348 negate_expr (op1));
10350 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10351 && TREE_CODE (arg1) == NEGATE_EXPR
10352 && negate_expr_p (op0))
10354 if (INTEGRAL_TYPE_P (type))
10355 fold_overflow_warning (("assuming signed overflow does not occur "
10356 "when distributing negation across "
10357 "division"),
10358 WARN_STRICT_OVERFLOW_MISC);
10359 return fold_build2_loc (loc, code, type,
10360 negate_expr (op0),
10361 fold_convert_loc (loc, type,
10362 TREE_OPERAND (arg1, 0)));
10365 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10366 operation, EXACT_DIV_EXPR.
10368 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10369 At one time others generated faster code, it's not clear if they do
10370 after the last round to changes to the DIV code in expmed.c. */
10371 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10372 && multiple_of_p (type, arg0, arg1))
10373 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10374 fold_convert (type, arg0),
10375 fold_convert (type, arg1));
10377 strict_overflow_p = false;
10378 if (TREE_CODE (arg1) == INTEGER_CST
10379 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10380 &strict_overflow_p)))
10382 if (strict_overflow_p)
10383 fold_overflow_warning (("assuming signed overflow does not occur "
10384 "when simplifying division"),
10385 WARN_STRICT_OVERFLOW_MISC);
10386 return fold_convert_loc (loc, type, tem);
10389 return NULL_TREE;
10391 case CEIL_MOD_EXPR:
10392 case FLOOR_MOD_EXPR:
10393 case ROUND_MOD_EXPR:
10394 case TRUNC_MOD_EXPR:
10395 strict_overflow_p = false;
10396 if (TREE_CODE (arg1) == INTEGER_CST
10397 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10398 &strict_overflow_p)))
10400 if (strict_overflow_p)
10401 fold_overflow_warning (("assuming signed overflow does not occur "
10402 "when simplifying modulus"),
10403 WARN_STRICT_OVERFLOW_MISC);
10404 return fold_convert_loc (loc, type, tem);
10407 return NULL_TREE;
10409 case LROTATE_EXPR:
10410 case RROTATE_EXPR:
10411 case RSHIFT_EXPR:
10412 case LSHIFT_EXPR:
10413 /* Since negative shift count is not well-defined,
10414 don't try to compute it in the compiler. */
10415 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10416 return NULL_TREE;
10418 prec = element_precision (type);
10420 /* If we have a rotate of a bit operation with the rotate count and
10421 the second operand of the bit operation both constant,
10422 permute the two operations. */
10423 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10424 && (TREE_CODE (arg0) == BIT_AND_EXPR
10425 || TREE_CODE (arg0) == BIT_IOR_EXPR
10426 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10427 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10428 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10429 fold_build2_loc (loc, code, type,
10430 TREE_OPERAND (arg0, 0), arg1),
10431 fold_build2_loc (loc, code, type,
10432 TREE_OPERAND (arg0, 1), arg1));
10434 /* Two consecutive rotates adding up to the some integer
10435 multiple of the precision of the type can be ignored. */
10436 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10437 && TREE_CODE (arg0) == RROTATE_EXPR
10438 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10439 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10440 prec) == 0)
10441 return TREE_OPERAND (arg0, 0);
10443 return NULL_TREE;
10445 case MIN_EXPR:
10446 case MAX_EXPR:
10447 goto associate;
10449 case TRUTH_ANDIF_EXPR:
10450 /* Note that the operands of this must be ints
10451 and their values must be 0 or 1.
10452 ("true" is a fixed value perhaps depending on the language.) */
10453 /* If first arg is constant zero, return it. */
10454 if (integer_zerop (arg0))
10455 return fold_convert_loc (loc, type, arg0);
10456 case TRUTH_AND_EXPR:
10457 /* If either arg is constant true, drop it. */
10458 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10459 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10460 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10461 /* Preserve sequence points. */
10462 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10463 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10464 /* If second arg is constant zero, result is zero, but first arg
10465 must be evaluated. */
10466 if (integer_zerop (arg1))
10467 return omit_one_operand_loc (loc, type, arg1, arg0);
10468 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10469 case will be handled here. */
10470 if (integer_zerop (arg0))
10471 return omit_one_operand_loc (loc, type, arg0, arg1);
10473 /* !X && X is always false. */
10474 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10475 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10476 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10477 /* X && !X is always false. */
10478 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10479 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10480 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10482 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10483 means A >= Y && A != MAX, but in this case we know that
10484 A < X <= MAX. */
10486 if (!TREE_SIDE_EFFECTS (arg0)
10487 && !TREE_SIDE_EFFECTS (arg1))
10489 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10490 if (tem && !operand_equal_p (tem, arg0, 0))
10491 return fold_build2_loc (loc, code, type, tem, arg1);
10493 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10494 if (tem && !operand_equal_p (tem, arg1, 0))
10495 return fold_build2_loc (loc, code, type, arg0, tem);
10498 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10499 != NULL_TREE)
10500 return tem;
10502 return NULL_TREE;
10504 case TRUTH_ORIF_EXPR:
10505 /* Note that the operands of this must be ints
10506 and their values must be 0 or true.
10507 ("true" is a fixed value perhaps depending on the language.) */
10508 /* If first arg is constant true, return it. */
10509 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10510 return fold_convert_loc (loc, type, arg0);
10511 case TRUTH_OR_EXPR:
10512 /* If either arg is constant zero, drop it. */
10513 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10514 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10515 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10516 /* Preserve sequence points. */
10517 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10518 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10519 /* If second arg is constant true, result is true, but we must
10520 evaluate first arg. */
10521 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10522 return omit_one_operand_loc (loc, type, arg1, arg0);
10523 /* Likewise for first arg, but note this only occurs here for
10524 TRUTH_OR_EXPR. */
10525 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10526 return omit_one_operand_loc (loc, type, arg0, arg1);
10528 /* !X || X is always true. */
10529 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10530 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10531 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10532 /* X || !X is always true. */
10533 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10534 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10535 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10537 /* (X && !Y) || (!X && Y) is X ^ Y */
10538 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10539 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10541 tree a0, a1, l0, l1, n0, n1;
10543 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10544 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10546 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10547 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10549 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10550 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10552 if ((operand_equal_p (n0, a0, 0)
10553 && operand_equal_p (n1, a1, 0))
10554 || (operand_equal_p (n0, a1, 0)
10555 && operand_equal_p (n1, a0, 0)))
10556 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10559 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10560 != NULL_TREE)
10561 return tem;
10563 return NULL_TREE;
10565 case TRUTH_XOR_EXPR:
10566 /* If the second arg is constant zero, drop it. */
10567 if (integer_zerop (arg1))
10568 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10569 /* If the second arg is constant true, this is a logical inversion. */
10570 if (integer_onep (arg1))
10572 tem = invert_truthvalue_loc (loc, arg0);
10573 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10575 /* Identical arguments cancel to zero. */
10576 if (operand_equal_p (arg0, arg1, 0))
10577 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10579 /* !X ^ X is always true. */
10580 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10581 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10582 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10584 /* X ^ !X is always true. */
10585 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10586 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10587 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10589 return NULL_TREE;
10591 case EQ_EXPR:
10592 case NE_EXPR:
10593 STRIP_NOPS (arg0);
10594 STRIP_NOPS (arg1);
10596 tem = fold_comparison (loc, code, type, op0, op1);
10597 if (tem != NULL_TREE)
10598 return tem;
10600 /* bool_var != 1 becomes !bool_var. */
10601 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10602 && code == NE_EXPR)
10603 return fold_convert_loc (loc, type,
10604 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10605 TREE_TYPE (arg0), arg0));
10607 /* bool_var == 0 becomes !bool_var. */
10608 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10609 && code == EQ_EXPR)
10610 return fold_convert_loc (loc, type,
10611 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10612 TREE_TYPE (arg0), arg0));
10614 /* !exp != 0 becomes !exp */
10615 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10616 && code == NE_EXPR)
10617 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10619 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10620 if ((TREE_CODE (arg0) == PLUS_EXPR
10621 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10622 || TREE_CODE (arg0) == MINUS_EXPR)
10623 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10624 0)),
10625 arg1, 0)
10626 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10627 || POINTER_TYPE_P (TREE_TYPE (arg0))))
10629 tree val = TREE_OPERAND (arg0, 1);
10630 val = fold_build2_loc (loc, code, type, val,
10631 build_int_cst (TREE_TYPE (val), 0));
10632 return omit_two_operands_loc (loc, type, val,
10633 TREE_OPERAND (arg0, 0), arg1);
10636 /* Transform comparisons of the form X CMP X +- Y to Y CMP 0. */
10637 if ((TREE_CODE (arg1) == PLUS_EXPR
10638 || TREE_CODE (arg1) == POINTER_PLUS_EXPR
10639 || TREE_CODE (arg1) == MINUS_EXPR)
10640 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10641 0)),
10642 arg0, 0)
10643 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10644 || POINTER_TYPE_P (TREE_TYPE (arg1))))
10646 tree val = TREE_OPERAND (arg1, 1);
10647 val = fold_build2_loc (loc, code, type, val,
10648 build_int_cst (TREE_TYPE (val), 0));
10649 return omit_two_operands_loc (loc, type, val,
10650 TREE_OPERAND (arg1, 0), arg0);
10653 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10654 if (TREE_CODE (arg0) == MINUS_EXPR
10655 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
10656 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10657 1)),
10658 arg1, 0)
10659 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
10660 return omit_two_operands_loc (loc, type,
10661 code == NE_EXPR
10662 ? boolean_true_node : boolean_false_node,
10663 TREE_OPERAND (arg0, 1), arg1);
10665 /* Transform comparisons of the form X CMP C - X if C % 2 == 1. */
10666 if (TREE_CODE (arg1) == MINUS_EXPR
10667 && TREE_CODE (TREE_OPERAND (arg1, 0)) == INTEGER_CST
10668 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg1,
10669 1)),
10670 arg0, 0)
10671 && wi::extract_uhwi (TREE_OPERAND (arg1, 0), 0, 1) == 1)
10672 return omit_two_operands_loc (loc, type,
10673 code == NE_EXPR
10674 ? boolean_true_node : boolean_false_node,
10675 TREE_OPERAND (arg1, 1), arg0);
10677 /* If this is an EQ or NE comparison with zero and ARG0 is
10678 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10679 two operations, but the latter can be done in one less insn
10680 on machines that have only two-operand insns or on which a
10681 constant cannot be the first operand. */
10682 if (TREE_CODE (arg0) == BIT_AND_EXPR
10683 && integer_zerop (arg1))
10685 tree arg00 = TREE_OPERAND (arg0, 0);
10686 tree arg01 = TREE_OPERAND (arg0, 1);
10687 if (TREE_CODE (arg00) == LSHIFT_EXPR
10688 && integer_onep (TREE_OPERAND (arg00, 0)))
10690 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10691 arg01, TREE_OPERAND (arg00, 1));
10692 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10693 build_int_cst (TREE_TYPE (arg0), 1));
10694 return fold_build2_loc (loc, code, type,
10695 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10696 arg1);
10698 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10699 && integer_onep (TREE_OPERAND (arg01, 0)))
10701 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10702 arg00, TREE_OPERAND (arg01, 1));
10703 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10704 build_int_cst (TREE_TYPE (arg0), 1));
10705 return fold_build2_loc (loc, code, type,
10706 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10707 arg1);
10711 /* If this is an NE or EQ comparison of zero against the result of a
10712 signed MOD operation whose second operand is a power of 2, make
10713 the MOD operation unsigned since it is simpler and equivalent. */
10714 if (integer_zerop (arg1)
10715 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10716 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10717 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10718 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10719 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10720 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10722 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10723 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10724 fold_convert_loc (loc, newtype,
10725 TREE_OPERAND (arg0, 0)),
10726 fold_convert_loc (loc, newtype,
10727 TREE_OPERAND (arg0, 1)));
10729 return fold_build2_loc (loc, code, type, newmod,
10730 fold_convert_loc (loc, newtype, arg1));
10733 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10734 C1 is a valid shift constant, and C2 is a power of two, i.e.
10735 a single bit. */
10736 if (TREE_CODE (arg0) == BIT_AND_EXPR
10737 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10738 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10739 == INTEGER_CST
10740 && integer_pow2p (TREE_OPERAND (arg0, 1))
10741 && integer_zerop (arg1))
10743 tree itype = TREE_TYPE (arg0);
10744 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10745 prec = TYPE_PRECISION (itype);
10747 /* Check for a valid shift count. */
10748 if (wi::ltu_p (arg001, prec))
10750 tree arg01 = TREE_OPERAND (arg0, 1);
10751 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10752 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10753 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10754 can be rewritten as (X & (C2 << C1)) != 0. */
10755 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10757 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10758 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10759 return fold_build2_loc (loc, code, type, tem,
10760 fold_convert_loc (loc, itype, arg1));
10762 /* Otherwise, for signed (arithmetic) shifts,
10763 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10764 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10765 else if (!TYPE_UNSIGNED (itype))
10766 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10767 arg000, build_int_cst (itype, 0));
10768 /* Otherwise, of unsigned (logical) shifts,
10769 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10770 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10771 else
10772 return omit_one_operand_loc (loc, type,
10773 code == EQ_EXPR ? integer_one_node
10774 : integer_zero_node,
10775 arg000);
10779 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10780 Similarly for NE_EXPR. */
10781 if (TREE_CODE (arg0) == BIT_AND_EXPR
10782 && TREE_CODE (arg1) == INTEGER_CST
10783 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10785 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
10786 TREE_TYPE (TREE_OPERAND (arg0, 1)),
10787 TREE_OPERAND (arg0, 1));
10788 tree dandnotc
10789 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10790 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
10791 notc);
10792 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
10793 if (integer_nonzerop (dandnotc))
10794 return omit_one_operand_loc (loc, type, rslt, arg0);
10797 /* If this is a comparison of a field, we may be able to simplify it. */
10798 if ((TREE_CODE (arg0) == COMPONENT_REF
10799 || TREE_CODE (arg0) == BIT_FIELD_REF)
10800 /* Handle the constant case even without -O
10801 to make sure the warnings are given. */
10802 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10804 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10805 if (t1)
10806 return t1;
10809 /* Optimize comparisons of strlen vs zero to a compare of the
10810 first character of the string vs zero. To wit,
10811 strlen(ptr) == 0 => *ptr == 0
10812 strlen(ptr) != 0 => *ptr != 0
10813 Other cases should reduce to one of these two (or a constant)
10814 due to the return value of strlen being unsigned. */
10815 if (TREE_CODE (arg0) == CALL_EXPR
10816 && integer_zerop (arg1))
10818 tree fndecl = get_callee_fndecl (arg0);
10820 if (fndecl
10821 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10822 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10823 && call_expr_nargs (arg0) == 1
10824 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10826 tree iref = build_fold_indirect_ref_loc (loc,
10827 CALL_EXPR_ARG (arg0, 0));
10828 return fold_build2_loc (loc, code, type, iref,
10829 build_int_cst (TREE_TYPE (iref), 0));
10833 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10834 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10835 if (TREE_CODE (arg0) == RSHIFT_EXPR
10836 && integer_zerop (arg1)
10837 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10839 tree arg00 = TREE_OPERAND (arg0, 0);
10840 tree arg01 = TREE_OPERAND (arg0, 1);
10841 tree itype = TREE_TYPE (arg00);
10842 if (wi::eq_p (arg01, element_precision (itype) - 1))
10844 if (TYPE_UNSIGNED (itype))
10846 itype = signed_type_for (itype);
10847 arg00 = fold_convert_loc (loc, itype, arg00);
10849 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10850 type, arg00, build_zero_cst (itype));
10854 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10855 (X & C) == 0 when C is a single bit. */
10856 if (TREE_CODE (arg0) == BIT_AND_EXPR
10857 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10858 && integer_zerop (arg1)
10859 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10861 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10862 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10863 TREE_OPERAND (arg0, 1));
10864 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10865 type, tem,
10866 fold_convert_loc (loc, TREE_TYPE (arg0),
10867 arg1));
10870 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10871 constant C is a power of two, i.e. a single bit. */
10872 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10873 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10874 && integer_zerop (arg1)
10875 && integer_pow2p (TREE_OPERAND (arg0, 1))
10876 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10877 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10879 tree arg00 = TREE_OPERAND (arg0, 0);
10880 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10881 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10884 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10885 when is C is a power of two, i.e. a single bit. */
10886 if (TREE_CODE (arg0) == BIT_AND_EXPR
10887 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10888 && integer_zerop (arg1)
10889 && integer_pow2p (TREE_OPERAND (arg0, 1))
10890 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10891 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10893 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10894 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10895 arg000, TREE_OPERAND (arg0, 1));
10896 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10897 tem, build_int_cst (TREE_TYPE (tem), 0));
10900 if (integer_zerop (arg1)
10901 && tree_expr_nonzero_p (arg0))
10903 tree res = constant_boolean_node (code==NE_EXPR, type);
10904 return omit_one_operand_loc (loc, type, res, arg0);
10907 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10908 if (TREE_CODE (arg0) == BIT_AND_EXPR
10909 && TREE_CODE (arg1) == BIT_AND_EXPR)
10911 tree arg00 = TREE_OPERAND (arg0, 0);
10912 tree arg01 = TREE_OPERAND (arg0, 1);
10913 tree arg10 = TREE_OPERAND (arg1, 0);
10914 tree arg11 = TREE_OPERAND (arg1, 1);
10915 tree itype = TREE_TYPE (arg0);
10917 if (operand_equal_p (arg01, arg11, 0))
10918 return fold_build2_loc (loc, code, type,
10919 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10920 fold_build2_loc (loc,
10921 BIT_XOR_EXPR, itype,
10922 arg00, arg10),
10923 arg01),
10924 build_zero_cst (itype));
10926 if (operand_equal_p (arg01, arg10, 0))
10927 return fold_build2_loc (loc, code, type,
10928 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10929 fold_build2_loc (loc,
10930 BIT_XOR_EXPR, itype,
10931 arg00, arg11),
10932 arg01),
10933 build_zero_cst (itype));
10935 if (operand_equal_p (arg00, arg11, 0))
10936 return fold_build2_loc (loc, code, type,
10937 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10938 fold_build2_loc (loc,
10939 BIT_XOR_EXPR, itype,
10940 arg01, arg10),
10941 arg00),
10942 build_zero_cst (itype));
10944 if (operand_equal_p (arg00, arg10, 0))
10945 return fold_build2_loc (loc, code, type,
10946 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10947 fold_build2_loc (loc,
10948 BIT_XOR_EXPR, itype,
10949 arg01, arg11),
10950 arg00),
10951 build_zero_cst (itype));
10954 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10955 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10957 tree arg00 = TREE_OPERAND (arg0, 0);
10958 tree arg01 = TREE_OPERAND (arg0, 1);
10959 tree arg10 = TREE_OPERAND (arg1, 0);
10960 tree arg11 = TREE_OPERAND (arg1, 1);
10961 tree itype = TREE_TYPE (arg0);
10963 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10964 operand_equal_p guarantees no side-effects so we don't need
10965 to use omit_one_operand on Z. */
10966 if (operand_equal_p (arg01, arg11, 0))
10967 return fold_build2_loc (loc, code, type, arg00,
10968 fold_convert_loc (loc, TREE_TYPE (arg00),
10969 arg10));
10970 if (operand_equal_p (arg01, arg10, 0))
10971 return fold_build2_loc (loc, code, type, arg00,
10972 fold_convert_loc (loc, TREE_TYPE (arg00),
10973 arg11));
10974 if (operand_equal_p (arg00, arg11, 0))
10975 return fold_build2_loc (loc, code, type, arg01,
10976 fold_convert_loc (loc, TREE_TYPE (arg01),
10977 arg10));
10978 if (operand_equal_p (arg00, arg10, 0))
10979 return fold_build2_loc (loc, code, type, arg01,
10980 fold_convert_loc (loc, TREE_TYPE (arg01),
10981 arg11));
10983 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10984 if (TREE_CODE (arg01) == INTEGER_CST
10985 && TREE_CODE (arg11) == INTEGER_CST)
10987 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10988 fold_convert_loc (loc, itype, arg11));
10989 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10990 return fold_build2_loc (loc, code, type, tem,
10991 fold_convert_loc (loc, itype, arg10));
10995 /* Attempt to simplify equality/inequality comparisons of complex
10996 values. Only lower the comparison if the result is known or
10997 can be simplified to a single scalar comparison. */
10998 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10999 || TREE_CODE (arg0) == COMPLEX_CST)
11000 && (TREE_CODE (arg1) == COMPLEX_EXPR
11001 || TREE_CODE (arg1) == COMPLEX_CST))
11003 tree real0, imag0, real1, imag1;
11004 tree rcond, icond;
11006 if (TREE_CODE (arg0) == COMPLEX_EXPR)
11008 real0 = TREE_OPERAND (arg0, 0);
11009 imag0 = TREE_OPERAND (arg0, 1);
11011 else
11013 real0 = TREE_REALPART (arg0);
11014 imag0 = TREE_IMAGPART (arg0);
11017 if (TREE_CODE (arg1) == COMPLEX_EXPR)
11019 real1 = TREE_OPERAND (arg1, 0);
11020 imag1 = TREE_OPERAND (arg1, 1);
11022 else
11024 real1 = TREE_REALPART (arg1);
11025 imag1 = TREE_IMAGPART (arg1);
11028 rcond = fold_binary_loc (loc, code, type, real0, real1);
11029 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11031 if (integer_zerop (rcond))
11033 if (code == EQ_EXPR)
11034 return omit_two_operands_loc (loc, type, boolean_false_node,
11035 imag0, imag1);
11036 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
11038 else
11040 if (code == NE_EXPR)
11041 return omit_two_operands_loc (loc, type, boolean_true_node,
11042 imag0, imag1);
11043 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
11047 icond = fold_binary_loc (loc, code, type, imag0, imag1);
11048 if (icond && TREE_CODE (icond) == INTEGER_CST)
11050 if (integer_zerop (icond))
11052 if (code == EQ_EXPR)
11053 return omit_two_operands_loc (loc, type, boolean_false_node,
11054 real0, real1);
11055 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11057 else
11059 if (code == NE_EXPR)
11060 return omit_two_operands_loc (loc, type, boolean_true_node,
11061 real0, real1);
11062 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11067 return NULL_TREE;
11069 case LT_EXPR:
11070 case GT_EXPR:
11071 case LE_EXPR:
11072 case GE_EXPR:
11073 tem = fold_comparison (loc, code, type, op0, op1);
11074 if (tem != NULL_TREE)
11075 return tem;
11077 /* Transform comparisons of the form X +- C CMP X. */
11078 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11079 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11080 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11081 && !HONOR_SNANS (arg0))
11082 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11083 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
11085 tree arg01 = TREE_OPERAND (arg0, 1);
11086 enum tree_code code0 = TREE_CODE (arg0);
11087 int is_positive;
11089 if (TREE_CODE (arg01) == REAL_CST)
11090 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11091 else
11092 is_positive = tree_int_cst_sgn (arg01);
11094 /* (X - c) > X becomes false. */
11095 if (code == GT_EXPR
11096 && ((code0 == MINUS_EXPR && is_positive >= 0)
11097 || (code0 == PLUS_EXPR && is_positive <= 0)))
11099 if (TREE_CODE (arg01) == INTEGER_CST
11100 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11101 fold_overflow_warning (("assuming signed overflow does not "
11102 "occur when assuming that (X - c) > X "
11103 "is always false"),
11104 WARN_STRICT_OVERFLOW_ALL);
11105 return constant_boolean_node (0, type);
11108 /* Likewise (X + c) < X becomes false. */
11109 if (code == LT_EXPR
11110 && ((code0 == PLUS_EXPR && is_positive >= 0)
11111 || (code0 == MINUS_EXPR && is_positive <= 0)))
11113 if (TREE_CODE (arg01) == INTEGER_CST
11114 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11115 fold_overflow_warning (("assuming signed overflow does not "
11116 "occur when assuming that "
11117 "(X + c) < X is always false"),
11118 WARN_STRICT_OVERFLOW_ALL);
11119 return constant_boolean_node (0, type);
11122 /* Convert (X - c) <= X to true. */
11123 if (!HONOR_NANS (arg1)
11124 && code == LE_EXPR
11125 && ((code0 == MINUS_EXPR && is_positive >= 0)
11126 || (code0 == PLUS_EXPR && is_positive <= 0)))
11128 if (TREE_CODE (arg01) == INTEGER_CST
11129 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11130 fold_overflow_warning (("assuming signed overflow does not "
11131 "occur when assuming that "
11132 "(X - c) <= X is always true"),
11133 WARN_STRICT_OVERFLOW_ALL);
11134 return constant_boolean_node (1, type);
11137 /* Convert (X + c) >= X to true. */
11138 if (!HONOR_NANS (arg1)
11139 && code == GE_EXPR
11140 && ((code0 == PLUS_EXPR && is_positive >= 0)
11141 || (code0 == MINUS_EXPR && is_positive <= 0)))
11143 if (TREE_CODE (arg01) == INTEGER_CST
11144 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11145 fold_overflow_warning (("assuming signed overflow does not "
11146 "occur when assuming that "
11147 "(X + c) >= X is always true"),
11148 WARN_STRICT_OVERFLOW_ALL);
11149 return constant_boolean_node (1, type);
11152 if (TREE_CODE (arg01) == INTEGER_CST)
11154 /* Convert X + c > X and X - c < X to true for integers. */
11155 if (code == GT_EXPR
11156 && ((code0 == PLUS_EXPR && is_positive > 0)
11157 || (code0 == MINUS_EXPR && is_positive < 0)))
11159 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11160 fold_overflow_warning (("assuming signed overflow does "
11161 "not occur when assuming that "
11162 "(X + c) > X is always true"),
11163 WARN_STRICT_OVERFLOW_ALL);
11164 return constant_boolean_node (1, type);
11167 if (code == LT_EXPR
11168 && ((code0 == MINUS_EXPR && is_positive > 0)
11169 || (code0 == PLUS_EXPR && is_positive < 0)))
11171 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11172 fold_overflow_warning (("assuming signed overflow does "
11173 "not occur when assuming that "
11174 "(X - c) < X is always true"),
11175 WARN_STRICT_OVERFLOW_ALL);
11176 return constant_boolean_node (1, type);
11179 /* Convert X + c <= X and X - c >= X to false for integers. */
11180 if (code == LE_EXPR
11181 && ((code0 == PLUS_EXPR && is_positive > 0)
11182 || (code0 == MINUS_EXPR && is_positive < 0)))
11184 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11185 fold_overflow_warning (("assuming signed overflow does "
11186 "not occur when assuming that "
11187 "(X + c) <= X is always false"),
11188 WARN_STRICT_OVERFLOW_ALL);
11189 return constant_boolean_node (0, type);
11192 if (code == GE_EXPR
11193 && ((code0 == MINUS_EXPR && is_positive > 0)
11194 || (code0 == PLUS_EXPR && is_positive < 0)))
11196 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11197 fold_overflow_warning (("assuming signed overflow does "
11198 "not occur when assuming that "
11199 "(X - c) >= X is always false"),
11200 WARN_STRICT_OVERFLOW_ALL);
11201 return constant_boolean_node (0, type);
11206 /* If we are comparing an ABS_EXPR with a constant, we can
11207 convert all the cases into explicit comparisons, but they may
11208 well not be faster than doing the ABS and one comparison.
11209 But ABS (X) <= C is a range comparison, which becomes a subtraction
11210 and a comparison, and is probably faster. */
11211 if (code == LE_EXPR
11212 && TREE_CODE (arg1) == INTEGER_CST
11213 && TREE_CODE (arg0) == ABS_EXPR
11214 && ! TREE_SIDE_EFFECTS (arg0)
11215 && (0 != (tem = negate_expr (arg1)))
11216 && TREE_CODE (tem) == INTEGER_CST
11217 && !TREE_OVERFLOW (tem))
11218 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11219 build2 (GE_EXPR, type,
11220 TREE_OPERAND (arg0, 0), tem),
11221 build2 (LE_EXPR, type,
11222 TREE_OPERAND (arg0, 0), arg1));
11224 /* Convert ABS_EXPR<x> >= 0 to true. */
11225 strict_overflow_p = false;
11226 if (code == GE_EXPR
11227 && (integer_zerop (arg1)
11228 || (! HONOR_NANS (arg0)
11229 && real_zerop (arg1)))
11230 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11232 if (strict_overflow_p)
11233 fold_overflow_warning (("assuming signed overflow does not occur "
11234 "when simplifying comparison of "
11235 "absolute value and zero"),
11236 WARN_STRICT_OVERFLOW_CONDITIONAL);
11237 return omit_one_operand_loc (loc, type,
11238 constant_boolean_node (true, type),
11239 arg0);
11242 /* Convert ABS_EXPR<x> < 0 to false. */
11243 strict_overflow_p = false;
11244 if (code == LT_EXPR
11245 && (integer_zerop (arg1) || real_zerop (arg1))
11246 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11248 if (strict_overflow_p)
11249 fold_overflow_warning (("assuming signed overflow does not occur "
11250 "when simplifying comparison of "
11251 "absolute value and zero"),
11252 WARN_STRICT_OVERFLOW_CONDITIONAL);
11253 return omit_one_operand_loc (loc, type,
11254 constant_boolean_node (false, type),
11255 arg0);
11258 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11259 and similarly for >= into !=. */
11260 if ((code == LT_EXPR || code == GE_EXPR)
11261 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11262 && TREE_CODE (arg1) == LSHIFT_EXPR
11263 && integer_onep (TREE_OPERAND (arg1, 0)))
11264 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11265 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11266 TREE_OPERAND (arg1, 1)),
11267 build_zero_cst (TREE_TYPE (arg0)));
11269 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11270 otherwise Y might be >= # of bits in X's type and thus e.g.
11271 (unsigned char) (1 << Y) for Y 15 might be 0.
11272 If the cast is widening, then 1 << Y should have unsigned type,
11273 otherwise if Y is number of bits in the signed shift type minus 1,
11274 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11275 31 might be 0xffffffff80000000. */
11276 if ((code == LT_EXPR || code == GE_EXPR)
11277 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11278 && CONVERT_EXPR_P (arg1)
11279 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11280 && (element_precision (TREE_TYPE (arg1))
11281 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11282 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11283 || (element_precision (TREE_TYPE (arg1))
11284 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11285 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11287 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11288 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11289 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11290 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11291 build_zero_cst (TREE_TYPE (arg0)));
11294 return NULL_TREE;
11296 case UNORDERED_EXPR:
11297 case ORDERED_EXPR:
11298 case UNLT_EXPR:
11299 case UNLE_EXPR:
11300 case UNGT_EXPR:
11301 case UNGE_EXPR:
11302 case UNEQ_EXPR:
11303 case LTGT_EXPR:
11304 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11306 tree targ0 = strip_float_extensions (arg0);
11307 tree targ1 = strip_float_extensions (arg1);
11308 tree newtype = TREE_TYPE (targ0);
11310 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11311 newtype = TREE_TYPE (targ1);
11313 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11314 return fold_build2_loc (loc, code, type,
11315 fold_convert_loc (loc, newtype, targ0),
11316 fold_convert_loc (loc, newtype, targ1));
11319 return NULL_TREE;
11321 case COMPOUND_EXPR:
11322 /* When pedantic, a compound expression can be neither an lvalue
11323 nor an integer constant expression. */
11324 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11325 return NULL_TREE;
11326 /* Don't let (0, 0) be null pointer constant. */
11327 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11328 : fold_convert_loc (loc, type, arg1);
11329 return pedantic_non_lvalue_loc (loc, tem);
11331 case ASSERT_EXPR:
11332 /* An ASSERT_EXPR should never be passed to fold_binary. */
11333 gcc_unreachable ();
11335 default:
11336 return NULL_TREE;
11337 } /* switch (code) */
11340 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11341 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11342 of GOTO_EXPR. */
11344 static tree
11345 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11347 switch (TREE_CODE (*tp))
11349 case LABEL_EXPR:
11350 return *tp;
11352 case GOTO_EXPR:
11353 *walk_subtrees = 0;
11355 /* ... fall through ... */
11357 default:
11358 return NULL_TREE;
11362 /* Return whether the sub-tree ST contains a label which is accessible from
11363 outside the sub-tree. */
11365 static bool
11366 contains_label_p (tree st)
11368 return
11369 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11372 /* Fold a ternary expression of code CODE and type TYPE with operands
11373 OP0, OP1, and OP2. Return the folded expression if folding is
11374 successful. Otherwise, return NULL_TREE. */
11376 tree
11377 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11378 tree op0, tree op1, tree op2)
11380 tree tem;
11381 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11382 enum tree_code_class kind = TREE_CODE_CLASS (code);
11384 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11385 && TREE_CODE_LENGTH (code) == 3);
11387 /* If this is a commutative operation, and OP0 is a constant, move it
11388 to OP1 to reduce the number of tests below. */
11389 if (commutative_ternary_tree_code (code)
11390 && tree_swap_operands_p (op0, op1, true))
11391 return fold_build3_loc (loc, code, type, op1, op0, op2);
11393 tem = generic_simplify (loc, code, type, op0, op1, op2);
11394 if (tem)
11395 return tem;
11397 /* Strip any conversions that don't change the mode. This is safe
11398 for every expression, except for a comparison expression because
11399 its signedness is derived from its operands. So, in the latter
11400 case, only strip conversions that don't change the signedness.
11402 Note that this is done as an internal manipulation within the
11403 constant folder, in order to find the simplest representation of
11404 the arguments so that their form can be studied. In any cases,
11405 the appropriate type conversions should be put back in the tree
11406 that will get out of the constant folder. */
11407 if (op0)
11409 arg0 = op0;
11410 STRIP_NOPS (arg0);
11413 if (op1)
11415 arg1 = op1;
11416 STRIP_NOPS (arg1);
11419 if (op2)
11421 arg2 = op2;
11422 STRIP_NOPS (arg2);
11425 switch (code)
11427 case COMPONENT_REF:
11428 if (TREE_CODE (arg0) == CONSTRUCTOR
11429 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11431 unsigned HOST_WIDE_INT idx;
11432 tree field, value;
11433 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11434 if (field == arg1)
11435 return value;
11437 return NULL_TREE;
11439 case COND_EXPR:
11440 case VEC_COND_EXPR:
11441 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11442 so all simple results must be passed through pedantic_non_lvalue. */
11443 if (TREE_CODE (arg0) == INTEGER_CST)
11445 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11446 tem = integer_zerop (arg0) ? op2 : op1;
11447 /* Only optimize constant conditions when the selected branch
11448 has the same type as the COND_EXPR. This avoids optimizing
11449 away "c ? x : throw", where the throw has a void type.
11450 Avoid throwing away that operand which contains label. */
11451 if ((!TREE_SIDE_EFFECTS (unused_op)
11452 || !contains_label_p (unused_op))
11453 && (! VOID_TYPE_P (TREE_TYPE (tem))
11454 || VOID_TYPE_P (type)))
11455 return pedantic_non_lvalue_loc (loc, tem);
11456 return NULL_TREE;
11458 else if (TREE_CODE (arg0) == VECTOR_CST)
11460 if ((TREE_CODE (arg1) == VECTOR_CST
11461 || TREE_CODE (arg1) == CONSTRUCTOR)
11462 && (TREE_CODE (arg2) == VECTOR_CST
11463 || TREE_CODE (arg2) == CONSTRUCTOR))
11465 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
11466 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
11467 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
11468 for (i = 0; i < nelts; i++)
11470 tree val = VECTOR_CST_ELT (arg0, i);
11471 if (integer_all_onesp (val))
11472 sel[i] = i;
11473 else if (integer_zerop (val))
11474 sel[i] = nelts + i;
11475 else /* Currently unreachable. */
11476 return NULL_TREE;
11478 tree t = fold_vec_perm (type, arg1, arg2, sel);
11479 if (t != NULL_TREE)
11480 return t;
11484 /* If we have A op B ? A : C, we may be able to convert this to a
11485 simpler expression, depending on the operation and the values
11486 of B and C. Signed zeros prevent all of these transformations,
11487 for reasons given above each one.
11489 Also try swapping the arguments and inverting the conditional. */
11490 if (COMPARISON_CLASS_P (arg0)
11491 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11492 arg1, TREE_OPERAND (arg0, 1))
11493 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11495 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11496 if (tem)
11497 return tem;
11500 if (COMPARISON_CLASS_P (arg0)
11501 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11502 op2,
11503 TREE_OPERAND (arg0, 1))
11504 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11506 location_t loc0 = expr_location_or (arg0, loc);
11507 tem = fold_invert_truthvalue (loc0, arg0);
11508 if (tem && COMPARISON_CLASS_P (tem))
11510 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11511 if (tem)
11512 return tem;
11516 /* If the second operand is simpler than the third, swap them
11517 since that produces better jump optimization results. */
11518 if (truth_value_p (TREE_CODE (arg0))
11519 && tree_swap_operands_p (op1, op2, false))
11521 location_t loc0 = expr_location_or (arg0, loc);
11522 /* See if this can be inverted. If it can't, possibly because
11523 it was a floating-point inequality comparison, don't do
11524 anything. */
11525 tem = fold_invert_truthvalue (loc0, arg0);
11526 if (tem)
11527 return fold_build3_loc (loc, code, type, tem, op2, op1);
11530 /* Convert A ? 1 : 0 to simply A. */
11531 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11532 : (integer_onep (op1)
11533 && !VECTOR_TYPE_P (type)))
11534 && integer_zerop (op2)
11535 /* If we try to convert OP0 to our type, the
11536 call to fold will try to move the conversion inside
11537 a COND, which will recurse. In that case, the COND_EXPR
11538 is probably the best choice, so leave it alone. */
11539 && type == TREE_TYPE (arg0))
11540 return pedantic_non_lvalue_loc (loc, arg0);
11542 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11543 over COND_EXPR in cases such as floating point comparisons. */
11544 if (integer_zerop (op1)
11545 && (code == VEC_COND_EXPR ? integer_all_onesp (op2)
11546 : (integer_onep (op2)
11547 && !VECTOR_TYPE_P (type)))
11548 && truth_value_p (TREE_CODE (arg0)))
11549 return pedantic_non_lvalue_loc (loc,
11550 fold_convert_loc (loc, type,
11551 invert_truthvalue_loc (loc,
11552 arg0)));
11554 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11555 if (TREE_CODE (arg0) == LT_EXPR
11556 && integer_zerop (TREE_OPERAND (arg0, 1))
11557 && integer_zerop (op2)
11558 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11560 /* sign_bit_p looks through both zero and sign extensions,
11561 but for this optimization only sign extensions are
11562 usable. */
11563 tree tem2 = TREE_OPERAND (arg0, 0);
11564 while (tem != tem2)
11566 if (TREE_CODE (tem2) != NOP_EXPR
11567 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11569 tem = NULL_TREE;
11570 break;
11572 tem2 = TREE_OPERAND (tem2, 0);
11574 /* sign_bit_p only checks ARG1 bits within A's precision.
11575 If <sign bit of A> has wider type than A, bits outside
11576 of A's precision in <sign bit of A> need to be checked.
11577 If they are all 0, this optimization needs to be done
11578 in unsigned A's type, if they are all 1 in signed A's type,
11579 otherwise this can't be done. */
11580 if (tem
11581 && TYPE_PRECISION (TREE_TYPE (tem))
11582 < TYPE_PRECISION (TREE_TYPE (arg1))
11583 && TYPE_PRECISION (TREE_TYPE (tem))
11584 < TYPE_PRECISION (type))
11586 int inner_width, outer_width;
11587 tree tem_type;
11589 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11590 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11591 if (outer_width > TYPE_PRECISION (type))
11592 outer_width = TYPE_PRECISION (type);
11594 wide_int mask = wi::shifted_mask
11595 (inner_width, outer_width - inner_width, false,
11596 TYPE_PRECISION (TREE_TYPE (arg1)));
11598 wide_int common = mask & arg1;
11599 if (common == mask)
11601 tem_type = signed_type_for (TREE_TYPE (tem));
11602 tem = fold_convert_loc (loc, tem_type, tem);
11604 else if (common == 0)
11606 tem_type = unsigned_type_for (TREE_TYPE (tem));
11607 tem = fold_convert_loc (loc, tem_type, tem);
11609 else
11610 tem = NULL;
11613 if (tem)
11614 return
11615 fold_convert_loc (loc, type,
11616 fold_build2_loc (loc, BIT_AND_EXPR,
11617 TREE_TYPE (tem), tem,
11618 fold_convert_loc (loc,
11619 TREE_TYPE (tem),
11620 arg1)));
11623 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11624 already handled above. */
11625 if (TREE_CODE (arg0) == BIT_AND_EXPR
11626 && integer_onep (TREE_OPERAND (arg0, 1))
11627 && integer_zerop (op2)
11628 && integer_pow2p (arg1))
11630 tree tem = TREE_OPERAND (arg0, 0);
11631 STRIP_NOPS (tem);
11632 if (TREE_CODE (tem) == RSHIFT_EXPR
11633 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11634 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
11635 tree_to_uhwi (TREE_OPERAND (tem, 1)))
11636 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11637 TREE_OPERAND (tem, 0), arg1);
11640 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11641 is probably obsolete because the first operand should be a
11642 truth value (that's why we have the two cases above), but let's
11643 leave it in until we can confirm this for all front-ends. */
11644 if (integer_zerop (op2)
11645 && TREE_CODE (arg0) == NE_EXPR
11646 && integer_zerop (TREE_OPERAND (arg0, 1))
11647 && integer_pow2p (arg1)
11648 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11649 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11650 arg1, OEP_ONLY_CONST))
11651 return pedantic_non_lvalue_loc (loc,
11652 fold_convert_loc (loc, type,
11653 TREE_OPERAND (arg0, 0)));
11655 /* Disable the transformations below for vectors, since
11656 fold_binary_op_with_conditional_arg may undo them immediately,
11657 yielding an infinite loop. */
11658 if (code == VEC_COND_EXPR)
11659 return NULL_TREE;
11661 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11662 if (integer_zerop (op2)
11663 && truth_value_p (TREE_CODE (arg0))
11664 && truth_value_p (TREE_CODE (arg1))
11665 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11666 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11667 : TRUTH_ANDIF_EXPR,
11668 type, fold_convert_loc (loc, type, arg0), arg1);
11670 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11671 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11672 && truth_value_p (TREE_CODE (arg0))
11673 && truth_value_p (TREE_CODE (arg1))
11674 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11676 location_t loc0 = expr_location_or (arg0, loc);
11677 /* Only perform transformation if ARG0 is easily inverted. */
11678 tem = fold_invert_truthvalue (loc0, arg0);
11679 if (tem)
11680 return fold_build2_loc (loc, code == VEC_COND_EXPR
11681 ? BIT_IOR_EXPR
11682 : TRUTH_ORIF_EXPR,
11683 type, fold_convert_loc (loc, type, tem),
11684 arg1);
11687 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11688 if (integer_zerop (arg1)
11689 && truth_value_p (TREE_CODE (arg0))
11690 && truth_value_p (TREE_CODE (op2))
11691 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11693 location_t loc0 = expr_location_or (arg0, loc);
11694 /* Only perform transformation if ARG0 is easily inverted. */
11695 tem = fold_invert_truthvalue (loc0, arg0);
11696 if (tem)
11697 return fold_build2_loc (loc, code == VEC_COND_EXPR
11698 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11699 type, fold_convert_loc (loc, type, tem),
11700 op2);
11703 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11704 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11705 && truth_value_p (TREE_CODE (arg0))
11706 && truth_value_p (TREE_CODE (op2))
11707 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11708 return fold_build2_loc (loc, code == VEC_COND_EXPR
11709 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11710 type, fold_convert_loc (loc, type, arg0), op2);
11712 return NULL_TREE;
11714 case CALL_EXPR:
11715 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11716 of fold_ternary on them. */
11717 gcc_unreachable ();
11719 case BIT_FIELD_REF:
11720 if ((TREE_CODE (arg0) == VECTOR_CST
11721 || (TREE_CODE (arg0) == CONSTRUCTOR
11722 && TREE_CODE (TREE_TYPE (arg0)) == VECTOR_TYPE))
11723 && (type == TREE_TYPE (TREE_TYPE (arg0))
11724 || (TREE_CODE (type) == VECTOR_TYPE
11725 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11727 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11728 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11729 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11730 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11732 if (n != 0
11733 && (idx % width) == 0
11734 && (n % width) == 0
11735 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11737 idx = idx / width;
11738 n = n / width;
11740 if (TREE_CODE (arg0) == VECTOR_CST)
11742 if (n == 1)
11743 return VECTOR_CST_ELT (arg0, idx);
11745 tree *vals = XALLOCAVEC (tree, n);
11746 for (unsigned i = 0; i < n; ++i)
11747 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
11748 return build_vector (type, vals);
11751 /* Constructor elements can be subvectors. */
11752 unsigned HOST_WIDE_INT k = 1;
11753 if (CONSTRUCTOR_NELTS (arg0) != 0)
11755 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (arg0, 0)->value);
11756 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
11757 k = TYPE_VECTOR_SUBPARTS (cons_elem);
11760 /* We keep an exact subset of the constructor elements. */
11761 if ((idx % k) == 0 && (n % k) == 0)
11763 if (CONSTRUCTOR_NELTS (arg0) == 0)
11764 return build_constructor (type, NULL);
11765 idx /= k;
11766 n /= k;
11767 if (n == 1)
11769 if (idx < CONSTRUCTOR_NELTS (arg0))
11770 return CONSTRUCTOR_ELT (arg0, idx)->value;
11771 return build_zero_cst (type);
11774 vec<constructor_elt, va_gc> *vals;
11775 vec_alloc (vals, n);
11776 for (unsigned i = 0;
11777 i < n && idx + i < CONSTRUCTOR_NELTS (arg0);
11778 ++i)
11779 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
11780 CONSTRUCTOR_ELT
11781 (arg0, idx + i)->value);
11782 return build_constructor (type, vals);
11784 /* The bitfield references a single constructor element. */
11785 else if (idx + n <= (idx / k + 1) * k)
11787 if (CONSTRUCTOR_NELTS (arg0) <= idx / k)
11788 return build_zero_cst (type);
11789 else if (n == k)
11790 return CONSTRUCTOR_ELT (arg0, idx / k)->value;
11791 else
11792 return fold_build3_loc (loc, code, type,
11793 CONSTRUCTOR_ELT (arg0, idx / k)->value, op1,
11794 build_int_cst (TREE_TYPE (op2), (idx % k) * width));
11799 /* A bit-field-ref that referenced the full argument can be stripped. */
11800 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11801 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_to_uhwi (arg1)
11802 && integer_zerop (op2))
11803 return fold_convert_loc (loc, type, arg0);
11805 /* On constants we can use native encode/interpret to constant
11806 fold (nearly) all BIT_FIELD_REFs. */
11807 if (CONSTANT_CLASS_P (arg0)
11808 && can_native_interpret_type_p (type)
11809 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0)))
11810 /* This limitation should not be necessary, we just need to
11811 round this up to mode size. */
11812 && tree_to_uhwi (op1) % BITS_PER_UNIT == 0
11813 /* Need bit-shifting of the buffer to relax the following. */
11814 && tree_to_uhwi (op2) % BITS_PER_UNIT == 0)
11816 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11817 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11818 unsigned HOST_WIDE_INT clen;
11819 clen = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0)));
11820 /* ??? We cannot tell native_encode_expr to start at
11821 some random byte only. So limit us to a reasonable amount
11822 of work. */
11823 if (clen <= 4096)
11825 unsigned char *b = XALLOCAVEC (unsigned char, clen);
11826 unsigned HOST_WIDE_INT len = native_encode_expr (arg0, b, clen);
11827 if (len > 0
11828 && len * BITS_PER_UNIT >= bitpos + bitsize)
11830 tree v = native_interpret_expr (type,
11831 b + bitpos / BITS_PER_UNIT,
11832 bitsize / BITS_PER_UNIT);
11833 if (v)
11834 return v;
11839 return NULL_TREE;
11841 case FMA_EXPR:
11842 /* For integers we can decompose the FMA if possible. */
11843 if (TREE_CODE (arg0) == INTEGER_CST
11844 && TREE_CODE (arg1) == INTEGER_CST)
11845 return fold_build2_loc (loc, PLUS_EXPR, type,
11846 const_binop (MULT_EXPR, arg0, arg1), arg2);
11847 if (integer_zerop (arg2))
11848 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11850 return fold_fma (loc, type, arg0, arg1, arg2);
11852 case VEC_PERM_EXPR:
11853 if (TREE_CODE (arg2) == VECTOR_CST)
11855 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
11856 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
11857 unsigned char *sel2 = sel + nelts;
11858 bool need_mask_canon = false;
11859 bool need_mask_canon2 = false;
11860 bool all_in_vec0 = true;
11861 bool all_in_vec1 = true;
11862 bool maybe_identity = true;
11863 bool single_arg = (op0 == op1);
11864 bool changed = false;
11866 mask2 = 2 * nelts - 1;
11867 mask = single_arg ? (nelts - 1) : mask2;
11868 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
11869 for (i = 0; i < nelts; i++)
11871 tree val = VECTOR_CST_ELT (arg2, i);
11872 if (TREE_CODE (val) != INTEGER_CST)
11873 return NULL_TREE;
11875 /* Make sure that the perm value is in an acceptable
11876 range. */
11877 wide_int t = val;
11878 need_mask_canon |= wi::gtu_p (t, mask);
11879 need_mask_canon2 |= wi::gtu_p (t, mask2);
11880 sel[i] = t.to_uhwi () & mask;
11881 sel2[i] = t.to_uhwi () & mask2;
11883 if (sel[i] < nelts)
11884 all_in_vec1 = false;
11885 else
11886 all_in_vec0 = false;
11888 if ((sel[i] & (nelts-1)) != i)
11889 maybe_identity = false;
11892 if (maybe_identity)
11894 if (all_in_vec0)
11895 return op0;
11896 if (all_in_vec1)
11897 return op1;
11900 if (all_in_vec0)
11901 op1 = op0;
11902 else if (all_in_vec1)
11904 op0 = op1;
11905 for (i = 0; i < nelts; i++)
11906 sel[i] -= nelts;
11907 need_mask_canon = true;
11910 if ((TREE_CODE (op0) == VECTOR_CST
11911 || TREE_CODE (op0) == CONSTRUCTOR)
11912 && (TREE_CODE (op1) == VECTOR_CST
11913 || TREE_CODE (op1) == CONSTRUCTOR))
11915 tree t = fold_vec_perm (type, op0, op1, sel);
11916 if (t != NULL_TREE)
11917 return t;
11920 if (op0 == op1 && !single_arg)
11921 changed = true;
11923 /* Some targets are deficient and fail to expand a single
11924 argument permutation while still allowing an equivalent
11925 2-argument version. */
11926 if (need_mask_canon && arg2 == op2
11927 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
11928 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
11930 need_mask_canon = need_mask_canon2;
11931 sel = sel2;
11934 if (need_mask_canon && arg2 == op2)
11936 tree *tsel = XALLOCAVEC (tree, nelts);
11937 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11938 for (i = 0; i < nelts; i++)
11939 tsel[i] = build_int_cst (eltype, sel[i]);
11940 op2 = build_vector (TREE_TYPE (arg2), tsel);
11941 changed = true;
11944 if (changed)
11945 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11947 return NULL_TREE;
11949 default:
11950 return NULL_TREE;
11951 } /* switch (code) */
11954 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11955 of an array (or vector). */
11957 tree
11958 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11960 tree index_type = NULL_TREE;
11961 offset_int low_bound = 0;
11963 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11965 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11966 if (domain_type && TYPE_MIN_VALUE (domain_type))
11968 /* Static constructors for variably sized objects makes no sense. */
11969 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11970 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11971 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11975 if (index_type)
11976 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11977 TYPE_SIGN (index_type));
11979 offset_int index = low_bound - 1;
11980 if (index_type)
11981 index = wi::ext (index, TYPE_PRECISION (index_type),
11982 TYPE_SIGN (index_type));
11984 offset_int max_index;
11985 unsigned HOST_WIDE_INT cnt;
11986 tree cfield, cval;
11988 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11990 /* Array constructor might explicitly set index, or specify a range,
11991 or leave index NULL meaning that it is next index after previous
11992 one. */
11993 if (cfield)
11995 if (TREE_CODE (cfield) == INTEGER_CST)
11996 max_index = index = wi::to_offset (cfield);
11997 else
11999 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
12000 index = wi::to_offset (TREE_OPERAND (cfield, 0));
12001 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
12004 else
12006 index += 1;
12007 if (index_type)
12008 index = wi::ext (index, TYPE_PRECISION (index_type),
12009 TYPE_SIGN (index_type));
12010 max_index = index;
12013 /* Do we have match? */
12014 if (wi::cmpu (access_index, index) >= 0
12015 && wi::cmpu (access_index, max_index) <= 0)
12016 return cval;
12018 return NULL_TREE;
12021 /* Perform constant folding and related simplification of EXPR.
12022 The related simplifications include x*1 => x, x*0 => 0, etc.,
12023 and application of the associative law.
12024 NOP_EXPR conversions may be removed freely (as long as we
12025 are careful not to change the type of the overall expression).
12026 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
12027 but we can constant-fold them if they have constant operands. */
12029 #ifdef ENABLE_FOLD_CHECKING
12030 # define fold(x) fold_1 (x)
12031 static tree fold_1 (tree);
12032 static
12033 #endif
12034 tree
12035 fold (tree expr)
12037 const tree t = expr;
12038 enum tree_code code = TREE_CODE (t);
12039 enum tree_code_class kind = TREE_CODE_CLASS (code);
12040 tree tem;
12041 location_t loc = EXPR_LOCATION (expr);
12043 /* Return right away if a constant. */
12044 if (kind == tcc_constant)
12045 return t;
12047 /* CALL_EXPR-like objects with variable numbers of operands are
12048 treated specially. */
12049 if (kind == tcc_vl_exp)
12051 if (code == CALL_EXPR)
12053 tem = fold_call_expr (loc, expr, false);
12054 return tem ? tem : expr;
12056 return expr;
12059 if (IS_EXPR_CODE_CLASS (kind))
12061 tree type = TREE_TYPE (t);
12062 tree op0, op1, op2;
12064 switch (TREE_CODE_LENGTH (code))
12066 case 1:
12067 op0 = TREE_OPERAND (t, 0);
12068 tem = fold_unary_loc (loc, code, type, op0);
12069 return tem ? tem : expr;
12070 case 2:
12071 op0 = TREE_OPERAND (t, 0);
12072 op1 = TREE_OPERAND (t, 1);
12073 tem = fold_binary_loc (loc, code, type, op0, op1);
12074 return tem ? tem : expr;
12075 case 3:
12076 op0 = TREE_OPERAND (t, 0);
12077 op1 = TREE_OPERAND (t, 1);
12078 op2 = TREE_OPERAND (t, 2);
12079 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12080 return tem ? tem : expr;
12081 default:
12082 break;
12086 switch (code)
12088 case ARRAY_REF:
12090 tree op0 = TREE_OPERAND (t, 0);
12091 tree op1 = TREE_OPERAND (t, 1);
12093 if (TREE_CODE (op1) == INTEGER_CST
12094 && TREE_CODE (op0) == CONSTRUCTOR
12095 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
12097 tree val = get_array_ctor_element_at_index (op0,
12098 wi::to_offset (op1));
12099 if (val)
12100 return val;
12103 return t;
12106 /* Return a VECTOR_CST if possible. */
12107 case CONSTRUCTOR:
12109 tree type = TREE_TYPE (t);
12110 if (TREE_CODE (type) != VECTOR_TYPE)
12111 return t;
12113 unsigned i;
12114 tree val;
12115 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
12116 if (! CONSTANT_CLASS_P (val))
12117 return t;
12119 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12122 case CONST_DECL:
12123 return fold (DECL_INITIAL (t));
12125 default:
12126 return t;
12127 } /* switch (code) */
12130 #ifdef ENABLE_FOLD_CHECKING
12131 #undef fold
12133 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12134 hash_table<nofree_ptr_hash<const tree_node> > *);
12135 static void fold_check_failed (const_tree, const_tree);
12136 void print_fold_checksum (const_tree);
12138 /* When --enable-checking=fold, compute a digest of expr before
12139 and after actual fold call to see if fold did not accidentally
12140 change original expr. */
12142 tree
12143 fold (tree expr)
12145 tree ret;
12146 struct md5_ctx ctx;
12147 unsigned char checksum_before[16], checksum_after[16];
12148 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12150 md5_init_ctx (&ctx);
12151 fold_checksum_tree (expr, &ctx, &ht);
12152 md5_finish_ctx (&ctx, checksum_before);
12153 ht.empty ();
12155 ret = fold_1 (expr);
12157 md5_init_ctx (&ctx);
12158 fold_checksum_tree (expr, &ctx, &ht);
12159 md5_finish_ctx (&ctx, checksum_after);
12161 if (memcmp (checksum_before, checksum_after, 16))
12162 fold_check_failed (expr, ret);
12164 return ret;
12167 void
12168 print_fold_checksum (const_tree expr)
12170 struct md5_ctx ctx;
12171 unsigned char checksum[16], cnt;
12172 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12174 md5_init_ctx (&ctx);
12175 fold_checksum_tree (expr, &ctx, &ht);
12176 md5_finish_ctx (&ctx, checksum);
12177 for (cnt = 0; cnt < 16; ++cnt)
12178 fprintf (stderr, "%02x", checksum[cnt]);
12179 putc ('\n', stderr);
12182 static void
12183 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12185 internal_error ("fold check: original tree changed by fold");
12188 static void
12189 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12190 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12192 const tree_node **slot;
12193 enum tree_code code;
12194 union tree_node buf;
12195 int i, len;
12197 recursive_label:
12198 if (expr == NULL)
12199 return;
12200 slot = ht->find_slot (expr, INSERT);
12201 if (*slot != NULL)
12202 return;
12203 *slot = expr;
12204 code = TREE_CODE (expr);
12205 if (TREE_CODE_CLASS (code) == tcc_declaration
12206 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12208 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12209 memcpy ((char *) &buf, expr, tree_size (expr));
12210 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12211 buf.decl_with_vis.symtab_node = NULL;
12212 expr = (tree) &buf;
12214 else if (TREE_CODE_CLASS (code) == tcc_type
12215 && (TYPE_POINTER_TO (expr)
12216 || TYPE_REFERENCE_TO (expr)
12217 || TYPE_CACHED_VALUES_P (expr)
12218 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12219 || TYPE_NEXT_VARIANT (expr)))
12221 /* Allow these fields to be modified. */
12222 tree tmp;
12223 memcpy ((char *) &buf, expr, tree_size (expr));
12224 expr = tmp = (tree) &buf;
12225 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12226 TYPE_POINTER_TO (tmp) = NULL;
12227 TYPE_REFERENCE_TO (tmp) = NULL;
12228 TYPE_NEXT_VARIANT (tmp) = NULL;
12229 if (TYPE_CACHED_VALUES_P (tmp))
12231 TYPE_CACHED_VALUES_P (tmp) = 0;
12232 TYPE_CACHED_VALUES (tmp) = NULL;
12235 md5_process_bytes (expr, tree_size (expr), ctx);
12236 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12237 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12238 if (TREE_CODE_CLASS (code) != tcc_type
12239 && TREE_CODE_CLASS (code) != tcc_declaration
12240 && code != TREE_LIST
12241 && code != SSA_NAME
12242 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12243 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12244 switch (TREE_CODE_CLASS (code))
12246 case tcc_constant:
12247 switch (code)
12249 case STRING_CST:
12250 md5_process_bytes (TREE_STRING_POINTER (expr),
12251 TREE_STRING_LENGTH (expr), ctx);
12252 break;
12253 case COMPLEX_CST:
12254 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12255 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12256 break;
12257 case VECTOR_CST:
12258 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12259 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12260 break;
12261 default:
12262 break;
12264 break;
12265 case tcc_exceptional:
12266 switch (code)
12268 case TREE_LIST:
12269 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12270 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12271 expr = TREE_CHAIN (expr);
12272 goto recursive_label;
12273 break;
12274 case TREE_VEC:
12275 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12276 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12277 break;
12278 default:
12279 break;
12281 break;
12282 case tcc_expression:
12283 case tcc_reference:
12284 case tcc_comparison:
12285 case tcc_unary:
12286 case tcc_binary:
12287 case tcc_statement:
12288 case tcc_vl_exp:
12289 len = TREE_OPERAND_LENGTH (expr);
12290 for (i = 0; i < len; ++i)
12291 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12292 break;
12293 case tcc_declaration:
12294 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12295 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12296 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12298 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12299 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12300 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12301 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12302 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12305 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12307 if (TREE_CODE (expr) == FUNCTION_DECL)
12309 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12310 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12312 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12314 break;
12315 case tcc_type:
12316 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12317 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12318 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12319 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12320 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12321 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12322 if (INTEGRAL_TYPE_P (expr)
12323 || SCALAR_FLOAT_TYPE_P (expr))
12325 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12326 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12328 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12329 if (TREE_CODE (expr) == RECORD_TYPE
12330 || TREE_CODE (expr) == UNION_TYPE
12331 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12332 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12333 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12334 break;
12335 default:
12336 break;
12340 /* Helper function for outputting the checksum of a tree T. When
12341 debugging with gdb, you can "define mynext" to be "next" followed
12342 by "call debug_fold_checksum (op0)", then just trace down till the
12343 outputs differ. */
12345 DEBUG_FUNCTION void
12346 debug_fold_checksum (const_tree t)
12348 int i;
12349 unsigned char checksum[16];
12350 struct md5_ctx ctx;
12351 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12353 md5_init_ctx (&ctx);
12354 fold_checksum_tree (t, &ctx, &ht);
12355 md5_finish_ctx (&ctx, checksum);
12356 ht.empty ();
12358 for (i = 0; i < 16; i++)
12359 fprintf (stderr, "%d ", checksum[i]);
12361 fprintf (stderr, "\n");
12364 #endif
12366 /* Fold a unary tree expression with code CODE of type TYPE with an
12367 operand OP0. LOC is the location of the resulting expression.
12368 Return a folded expression if successful. Otherwise, return a tree
12369 expression with code CODE of type TYPE with an operand OP0. */
12371 tree
12372 fold_build1_stat_loc (location_t loc,
12373 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12375 tree tem;
12376 #ifdef ENABLE_FOLD_CHECKING
12377 unsigned char checksum_before[16], checksum_after[16];
12378 struct md5_ctx ctx;
12379 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12381 md5_init_ctx (&ctx);
12382 fold_checksum_tree (op0, &ctx, &ht);
12383 md5_finish_ctx (&ctx, checksum_before);
12384 ht.empty ();
12385 #endif
12387 tem = fold_unary_loc (loc, code, type, op0);
12388 if (!tem)
12389 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
12391 #ifdef ENABLE_FOLD_CHECKING
12392 md5_init_ctx (&ctx);
12393 fold_checksum_tree (op0, &ctx, &ht);
12394 md5_finish_ctx (&ctx, checksum_after);
12396 if (memcmp (checksum_before, checksum_after, 16))
12397 fold_check_failed (op0, tem);
12398 #endif
12399 return tem;
12402 /* Fold a binary tree expression with code CODE of type TYPE with
12403 operands OP0 and OP1. LOC is the location of the resulting
12404 expression. Return a folded expression if successful. Otherwise,
12405 return a tree expression with code CODE of type TYPE with operands
12406 OP0 and OP1. */
12408 tree
12409 fold_build2_stat_loc (location_t loc,
12410 enum tree_code code, tree type, tree op0, tree op1
12411 MEM_STAT_DECL)
12413 tree tem;
12414 #ifdef ENABLE_FOLD_CHECKING
12415 unsigned char checksum_before_op0[16],
12416 checksum_before_op1[16],
12417 checksum_after_op0[16],
12418 checksum_after_op1[16];
12419 struct md5_ctx ctx;
12420 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12422 md5_init_ctx (&ctx);
12423 fold_checksum_tree (op0, &ctx, &ht);
12424 md5_finish_ctx (&ctx, checksum_before_op0);
12425 ht.empty ();
12427 md5_init_ctx (&ctx);
12428 fold_checksum_tree (op1, &ctx, &ht);
12429 md5_finish_ctx (&ctx, checksum_before_op1);
12430 ht.empty ();
12431 #endif
12433 tem = fold_binary_loc (loc, code, type, op0, op1);
12434 if (!tem)
12435 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12437 #ifdef ENABLE_FOLD_CHECKING
12438 md5_init_ctx (&ctx);
12439 fold_checksum_tree (op0, &ctx, &ht);
12440 md5_finish_ctx (&ctx, checksum_after_op0);
12441 ht.empty ();
12443 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12444 fold_check_failed (op0, tem);
12446 md5_init_ctx (&ctx);
12447 fold_checksum_tree (op1, &ctx, &ht);
12448 md5_finish_ctx (&ctx, checksum_after_op1);
12450 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12451 fold_check_failed (op1, tem);
12452 #endif
12453 return tem;
12456 /* Fold a ternary tree expression with code CODE of type TYPE with
12457 operands OP0, OP1, and OP2. Return a folded expression if
12458 successful. Otherwise, return a tree expression with code CODE of
12459 type TYPE with operands OP0, OP1, and OP2. */
12461 tree
12462 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
12463 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12465 tree tem;
12466 #ifdef ENABLE_FOLD_CHECKING
12467 unsigned char checksum_before_op0[16],
12468 checksum_before_op1[16],
12469 checksum_before_op2[16],
12470 checksum_after_op0[16],
12471 checksum_after_op1[16],
12472 checksum_after_op2[16];
12473 struct md5_ctx ctx;
12474 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12476 md5_init_ctx (&ctx);
12477 fold_checksum_tree (op0, &ctx, &ht);
12478 md5_finish_ctx (&ctx, checksum_before_op0);
12479 ht.empty ();
12481 md5_init_ctx (&ctx);
12482 fold_checksum_tree (op1, &ctx, &ht);
12483 md5_finish_ctx (&ctx, checksum_before_op1);
12484 ht.empty ();
12486 md5_init_ctx (&ctx);
12487 fold_checksum_tree (op2, &ctx, &ht);
12488 md5_finish_ctx (&ctx, checksum_before_op2);
12489 ht.empty ();
12490 #endif
12492 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12493 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12494 if (!tem)
12495 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12497 #ifdef ENABLE_FOLD_CHECKING
12498 md5_init_ctx (&ctx);
12499 fold_checksum_tree (op0, &ctx, &ht);
12500 md5_finish_ctx (&ctx, checksum_after_op0);
12501 ht.empty ();
12503 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12504 fold_check_failed (op0, tem);
12506 md5_init_ctx (&ctx);
12507 fold_checksum_tree (op1, &ctx, &ht);
12508 md5_finish_ctx (&ctx, checksum_after_op1);
12509 ht.empty ();
12511 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12512 fold_check_failed (op1, tem);
12514 md5_init_ctx (&ctx);
12515 fold_checksum_tree (op2, &ctx, &ht);
12516 md5_finish_ctx (&ctx, checksum_after_op2);
12518 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12519 fold_check_failed (op2, tem);
12520 #endif
12521 return tem;
12524 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12525 arguments in ARGARRAY, and a null static chain.
12526 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12527 of type TYPE from the given operands as constructed by build_call_array. */
12529 tree
12530 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12531 int nargs, tree *argarray)
12533 tree tem;
12534 #ifdef ENABLE_FOLD_CHECKING
12535 unsigned char checksum_before_fn[16],
12536 checksum_before_arglist[16],
12537 checksum_after_fn[16],
12538 checksum_after_arglist[16];
12539 struct md5_ctx ctx;
12540 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12541 int i;
12543 md5_init_ctx (&ctx);
12544 fold_checksum_tree (fn, &ctx, &ht);
12545 md5_finish_ctx (&ctx, checksum_before_fn);
12546 ht.empty ();
12548 md5_init_ctx (&ctx);
12549 for (i = 0; i < nargs; i++)
12550 fold_checksum_tree (argarray[i], &ctx, &ht);
12551 md5_finish_ctx (&ctx, checksum_before_arglist);
12552 ht.empty ();
12553 #endif
12555 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12556 if (!tem)
12557 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12559 #ifdef ENABLE_FOLD_CHECKING
12560 md5_init_ctx (&ctx);
12561 fold_checksum_tree (fn, &ctx, &ht);
12562 md5_finish_ctx (&ctx, checksum_after_fn);
12563 ht.empty ();
12565 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12566 fold_check_failed (fn, tem);
12568 md5_init_ctx (&ctx);
12569 for (i = 0; i < nargs; i++)
12570 fold_checksum_tree (argarray[i], &ctx, &ht);
12571 md5_finish_ctx (&ctx, checksum_after_arglist);
12573 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12574 fold_check_failed (NULL_TREE, tem);
12575 #endif
12576 return tem;
12579 /* Perform constant folding and related simplification of initializer
12580 expression EXPR. These behave identically to "fold_buildN" but ignore
12581 potential run-time traps and exceptions that fold must preserve. */
12583 #define START_FOLD_INIT \
12584 int saved_signaling_nans = flag_signaling_nans;\
12585 int saved_trapping_math = flag_trapping_math;\
12586 int saved_rounding_math = flag_rounding_math;\
12587 int saved_trapv = flag_trapv;\
12588 int saved_folding_initializer = folding_initializer;\
12589 flag_signaling_nans = 0;\
12590 flag_trapping_math = 0;\
12591 flag_rounding_math = 0;\
12592 flag_trapv = 0;\
12593 folding_initializer = 1;
12595 #define END_FOLD_INIT \
12596 flag_signaling_nans = saved_signaling_nans;\
12597 flag_trapping_math = saved_trapping_math;\
12598 flag_rounding_math = saved_rounding_math;\
12599 flag_trapv = saved_trapv;\
12600 folding_initializer = saved_folding_initializer;
12602 tree
12603 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12604 tree type, tree op)
12606 tree result;
12607 START_FOLD_INIT;
12609 result = fold_build1_loc (loc, code, type, op);
12611 END_FOLD_INIT;
12612 return result;
12615 tree
12616 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12617 tree type, tree op0, tree op1)
12619 tree result;
12620 START_FOLD_INIT;
12622 result = fold_build2_loc (loc, code, type, op0, op1);
12624 END_FOLD_INIT;
12625 return result;
12628 tree
12629 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12630 int nargs, tree *argarray)
12632 tree result;
12633 START_FOLD_INIT;
12635 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12637 END_FOLD_INIT;
12638 return result;
12641 #undef START_FOLD_INIT
12642 #undef END_FOLD_INIT
12644 /* Determine if first argument is a multiple of second argument. Return 0 if
12645 it is not, or we cannot easily determined it to be.
12647 An example of the sort of thing we care about (at this point; this routine
12648 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12649 fold cases do now) is discovering that
12651 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12653 is a multiple of
12655 SAVE_EXPR (J * 8)
12657 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12659 This code also handles discovering that
12661 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12663 is a multiple of 8 so we don't have to worry about dealing with a
12664 possible remainder.
12666 Note that we *look* inside a SAVE_EXPR only to determine how it was
12667 calculated; it is not safe for fold to do much of anything else with the
12668 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12669 at run time. For example, the latter example above *cannot* be implemented
12670 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12671 evaluation time of the original SAVE_EXPR is not necessarily the same at
12672 the time the new expression is evaluated. The only optimization of this
12673 sort that would be valid is changing
12675 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12677 divided by 8 to
12679 SAVE_EXPR (I) * SAVE_EXPR (J)
12681 (where the same SAVE_EXPR (J) is used in the original and the
12682 transformed version). */
12685 multiple_of_p (tree type, const_tree top, const_tree bottom)
12687 if (operand_equal_p (top, bottom, 0))
12688 return 1;
12690 if (TREE_CODE (type) != INTEGER_TYPE)
12691 return 0;
12693 switch (TREE_CODE (top))
12695 case BIT_AND_EXPR:
12696 /* Bitwise and provides a power of two multiple. If the mask is
12697 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12698 if (!integer_pow2p (bottom))
12699 return 0;
12700 /* FALLTHRU */
12702 case MULT_EXPR:
12703 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12704 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12706 case PLUS_EXPR:
12707 case MINUS_EXPR:
12708 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12709 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12711 case LSHIFT_EXPR:
12712 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12714 tree op1, t1;
12716 op1 = TREE_OPERAND (top, 1);
12717 /* const_binop may not detect overflow correctly,
12718 so check for it explicitly here. */
12719 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
12720 && 0 != (t1 = fold_convert (type,
12721 const_binop (LSHIFT_EXPR,
12722 size_one_node,
12723 op1)))
12724 && !TREE_OVERFLOW (t1))
12725 return multiple_of_p (type, t1, bottom);
12727 return 0;
12729 case NOP_EXPR:
12730 /* Can't handle conversions from non-integral or wider integral type. */
12731 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12732 || (TYPE_PRECISION (type)
12733 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12734 return 0;
12736 /* .. fall through ... */
12738 case SAVE_EXPR:
12739 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12741 case COND_EXPR:
12742 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12743 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12745 case INTEGER_CST:
12746 if (TREE_CODE (bottom) != INTEGER_CST
12747 || integer_zerop (bottom)
12748 || (TYPE_UNSIGNED (type)
12749 && (tree_int_cst_sgn (top) < 0
12750 || tree_int_cst_sgn (bottom) < 0)))
12751 return 0;
12752 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12753 SIGNED);
12755 default:
12756 return 0;
12760 #define tree_expr_nonnegative_warnv_p(X, Y) \
12761 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12763 #define RECURSE(X) \
12764 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12766 /* Return true if CODE or TYPE is known to be non-negative. */
12768 static bool
12769 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12771 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12772 && truth_value_p (code))
12773 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12774 have a signed:1 type (where the value is -1 and 0). */
12775 return true;
12776 return false;
12779 /* Return true if (CODE OP0) is known to be non-negative. If the return
12780 value is based on the assumption that signed overflow is undefined,
12781 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12782 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12784 bool
12785 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12786 bool *strict_overflow_p, int depth)
12788 if (TYPE_UNSIGNED (type))
12789 return true;
12791 switch (code)
12793 case ABS_EXPR:
12794 /* We can't return 1 if flag_wrapv is set because
12795 ABS_EXPR<INT_MIN> = INT_MIN. */
12796 if (!ANY_INTEGRAL_TYPE_P (type))
12797 return true;
12798 if (TYPE_OVERFLOW_UNDEFINED (type))
12800 *strict_overflow_p = true;
12801 return true;
12803 break;
12805 case NON_LVALUE_EXPR:
12806 case FLOAT_EXPR:
12807 case FIX_TRUNC_EXPR:
12808 return RECURSE (op0);
12810 CASE_CONVERT:
12812 tree inner_type = TREE_TYPE (op0);
12813 tree outer_type = type;
12815 if (TREE_CODE (outer_type) == REAL_TYPE)
12817 if (TREE_CODE (inner_type) == REAL_TYPE)
12818 return RECURSE (op0);
12819 if (INTEGRAL_TYPE_P (inner_type))
12821 if (TYPE_UNSIGNED (inner_type))
12822 return true;
12823 return RECURSE (op0);
12826 else if (INTEGRAL_TYPE_P (outer_type))
12828 if (TREE_CODE (inner_type) == REAL_TYPE)
12829 return RECURSE (op0);
12830 if (INTEGRAL_TYPE_P (inner_type))
12831 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12832 && TYPE_UNSIGNED (inner_type);
12835 break;
12837 default:
12838 return tree_simple_nonnegative_warnv_p (code, type);
12841 /* We don't know sign of `t', so be conservative and return false. */
12842 return false;
12845 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12846 value is based on the assumption that signed overflow is undefined,
12847 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12848 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12850 bool
12851 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12852 tree op1, bool *strict_overflow_p,
12853 int depth)
12855 if (TYPE_UNSIGNED (type))
12856 return true;
12858 switch (code)
12860 case POINTER_PLUS_EXPR:
12861 case PLUS_EXPR:
12862 if (FLOAT_TYPE_P (type))
12863 return RECURSE (op0) && RECURSE (op1);
12865 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12866 both unsigned and at least 2 bits shorter than the result. */
12867 if (TREE_CODE (type) == INTEGER_TYPE
12868 && TREE_CODE (op0) == NOP_EXPR
12869 && TREE_CODE (op1) == NOP_EXPR)
12871 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12872 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12873 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12874 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12876 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12877 TYPE_PRECISION (inner2)) + 1;
12878 return prec < TYPE_PRECISION (type);
12881 break;
12883 case MULT_EXPR:
12884 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12886 /* x * x is always non-negative for floating point x
12887 or without overflow. */
12888 if (operand_equal_p (op0, op1, 0)
12889 || (RECURSE (op0) && RECURSE (op1)))
12891 if (ANY_INTEGRAL_TYPE_P (type)
12892 && TYPE_OVERFLOW_UNDEFINED (type))
12893 *strict_overflow_p = true;
12894 return true;
12898 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12899 both unsigned and their total bits is shorter than the result. */
12900 if (TREE_CODE (type) == INTEGER_TYPE
12901 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12902 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12904 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12905 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12906 : TREE_TYPE (op0);
12907 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12908 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12909 : TREE_TYPE (op1);
12911 bool unsigned0 = TYPE_UNSIGNED (inner0);
12912 bool unsigned1 = TYPE_UNSIGNED (inner1);
12914 if (TREE_CODE (op0) == INTEGER_CST)
12915 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12917 if (TREE_CODE (op1) == INTEGER_CST)
12918 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12920 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12921 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12923 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12924 ? tree_int_cst_min_precision (op0, UNSIGNED)
12925 : TYPE_PRECISION (inner0);
12927 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12928 ? tree_int_cst_min_precision (op1, UNSIGNED)
12929 : TYPE_PRECISION (inner1);
12931 return precision0 + precision1 < TYPE_PRECISION (type);
12934 return false;
12936 case BIT_AND_EXPR:
12937 case MAX_EXPR:
12938 return RECURSE (op0) || RECURSE (op1);
12940 case BIT_IOR_EXPR:
12941 case BIT_XOR_EXPR:
12942 case MIN_EXPR:
12943 case RDIV_EXPR:
12944 case TRUNC_DIV_EXPR:
12945 case CEIL_DIV_EXPR:
12946 case FLOOR_DIV_EXPR:
12947 case ROUND_DIV_EXPR:
12948 return RECURSE (op0) && RECURSE (op1);
12950 case TRUNC_MOD_EXPR:
12951 return RECURSE (op0);
12953 case FLOOR_MOD_EXPR:
12954 return RECURSE (op1);
12956 case CEIL_MOD_EXPR:
12957 case ROUND_MOD_EXPR:
12958 default:
12959 return tree_simple_nonnegative_warnv_p (code, type);
12962 /* We don't know sign of `t', so be conservative and return false. */
12963 return false;
12966 /* Return true if T is known to be non-negative. If the return
12967 value is based on the assumption that signed overflow is undefined,
12968 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12969 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12971 bool
12972 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12974 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12975 return true;
12977 switch (TREE_CODE (t))
12979 case INTEGER_CST:
12980 return tree_int_cst_sgn (t) >= 0;
12982 case REAL_CST:
12983 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12985 case FIXED_CST:
12986 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12988 case COND_EXPR:
12989 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12991 case SSA_NAME:
12992 /* Limit the depth of recursion to avoid quadratic behavior.
12993 This is expected to catch almost all occurrences in practice.
12994 If this code misses important cases that unbounded recursion
12995 would not, passes that need this information could be revised
12996 to provide it through dataflow propagation. */
12997 return (!name_registered_for_update_p (t)
12998 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12999 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
13000 strict_overflow_p, depth));
13002 default:
13003 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13007 /* Return true if T is known to be non-negative. If the return
13008 value is based on the assumption that signed overflow is undefined,
13009 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13010 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13012 bool
13013 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
13014 bool *strict_overflow_p, int depth)
13016 switch (fn)
13018 CASE_CFN_ACOS:
13019 CASE_CFN_ACOSH:
13020 CASE_CFN_CABS:
13021 CASE_CFN_COSH:
13022 CASE_CFN_ERFC:
13023 CASE_CFN_EXP:
13024 CASE_CFN_EXP10:
13025 CASE_CFN_EXP2:
13026 CASE_CFN_FABS:
13027 CASE_CFN_FDIM:
13028 CASE_CFN_HYPOT:
13029 CASE_CFN_POW10:
13030 CASE_CFN_FFS:
13031 CASE_CFN_PARITY:
13032 CASE_CFN_POPCOUNT:
13033 CASE_CFN_CLZ:
13034 CASE_CFN_CLRSB:
13035 case CFN_BUILT_IN_BSWAP32:
13036 case CFN_BUILT_IN_BSWAP64:
13037 /* Always true. */
13038 return true;
13040 CASE_CFN_SQRT:
13041 /* sqrt(-0.0) is -0.0. */
13042 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13043 return true;
13044 return RECURSE (arg0);
13046 CASE_CFN_ASINH:
13047 CASE_CFN_ATAN:
13048 CASE_CFN_ATANH:
13049 CASE_CFN_CBRT:
13050 CASE_CFN_CEIL:
13051 CASE_CFN_ERF:
13052 CASE_CFN_EXPM1:
13053 CASE_CFN_FLOOR:
13054 CASE_CFN_FMOD:
13055 CASE_CFN_FREXP:
13056 CASE_CFN_ICEIL:
13057 CASE_CFN_IFLOOR:
13058 CASE_CFN_IRINT:
13059 CASE_CFN_IROUND:
13060 CASE_CFN_LCEIL:
13061 CASE_CFN_LDEXP:
13062 CASE_CFN_LFLOOR:
13063 CASE_CFN_LLCEIL:
13064 CASE_CFN_LLFLOOR:
13065 CASE_CFN_LLRINT:
13066 CASE_CFN_LLROUND:
13067 CASE_CFN_LRINT:
13068 CASE_CFN_LROUND:
13069 CASE_CFN_MODF:
13070 CASE_CFN_NEARBYINT:
13071 CASE_CFN_RINT:
13072 CASE_CFN_ROUND:
13073 CASE_CFN_SCALB:
13074 CASE_CFN_SCALBLN:
13075 CASE_CFN_SCALBN:
13076 CASE_CFN_SIGNBIT:
13077 CASE_CFN_SIGNIFICAND:
13078 CASE_CFN_SINH:
13079 CASE_CFN_TANH:
13080 CASE_CFN_TRUNC:
13081 /* True if the 1st argument is nonnegative. */
13082 return RECURSE (arg0);
13084 CASE_CFN_FMAX:
13085 /* True if the 1st OR 2nd arguments are nonnegative. */
13086 return RECURSE (arg0) || RECURSE (arg1);
13088 CASE_CFN_FMIN:
13089 /* True if the 1st AND 2nd arguments are nonnegative. */
13090 return RECURSE (arg0) && RECURSE (arg1);
13092 CASE_CFN_COPYSIGN:
13093 /* True if the 2nd argument is nonnegative. */
13094 return RECURSE (arg1);
13096 CASE_CFN_POWI:
13097 /* True if the 1st argument is nonnegative or the second
13098 argument is an even integer. */
13099 if (TREE_CODE (arg1) == INTEGER_CST
13100 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13101 return true;
13102 return RECURSE (arg0);
13104 CASE_CFN_POW:
13105 /* True if the 1st argument is nonnegative or the second
13106 argument is an even integer valued real. */
13107 if (TREE_CODE (arg1) == REAL_CST)
13109 REAL_VALUE_TYPE c;
13110 HOST_WIDE_INT n;
13112 c = TREE_REAL_CST (arg1);
13113 n = real_to_integer (&c);
13114 if ((n & 1) == 0)
13116 REAL_VALUE_TYPE cint;
13117 real_from_integer (&cint, VOIDmode, n, SIGNED);
13118 if (real_identical (&c, &cint))
13119 return true;
13122 return RECURSE (arg0);
13124 default:
13125 break;
13127 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13130 /* Return true if T is known to be non-negative. If the return
13131 value is based on the assumption that signed overflow is undefined,
13132 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13133 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13135 static bool
13136 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13138 enum tree_code code = TREE_CODE (t);
13139 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13140 return true;
13142 switch (code)
13144 case TARGET_EXPR:
13146 tree temp = TARGET_EXPR_SLOT (t);
13147 t = TARGET_EXPR_INITIAL (t);
13149 /* If the initializer is non-void, then it's a normal expression
13150 that will be assigned to the slot. */
13151 if (!VOID_TYPE_P (t))
13152 return RECURSE (t);
13154 /* Otherwise, the initializer sets the slot in some way. One common
13155 way is an assignment statement at the end of the initializer. */
13156 while (1)
13158 if (TREE_CODE (t) == BIND_EXPR)
13159 t = expr_last (BIND_EXPR_BODY (t));
13160 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13161 || TREE_CODE (t) == TRY_CATCH_EXPR)
13162 t = expr_last (TREE_OPERAND (t, 0));
13163 else if (TREE_CODE (t) == STATEMENT_LIST)
13164 t = expr_last (t);
13165 else
13166 break;
13168 if (TREE_CODE (t) == MODIFY_EXPR
13169 && TREE_OPERAND (t, 0) == temp)
13170 return RECURSE (TREE_OPERAND (t, 1));
13172 return false;
13175 case CALL_EXPR:
13177 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13178 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13180 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13181 get_call_combined_fn (t),
13182 arg0,
13183 arg1,
13184 strict_overflow_p, depth);
13186 case COMPOUND_EXPR:
13187 case MODIFY_EXPR:
13188 return RECURSE (TREE_OPERAND (t, 1));
13190 case BIND_EXPR:
13191 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13193 case SAVE_EXPR:
13194 return RECURSE (TREE_OPERAND (t, 0));
13196 default:
13197 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13201 #undef RECURSE
13202 #undef tree_expr_nonnegative_warnv_p
13204 /* Return true if T is known to be non-negative. If the return
13205 value is based on the assumption that signed overflow is undefined,
13206 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13207 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13209 bool
13210 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13212 enum tree_code code;
13213 if (t == error_mark_node)
13214 return false;
13216 code = TREE_CODE (t);
13217 switch (TREE_CODE_CLASS (code))
13219 case tcc_binary:
13220 case tcc_comparison:
13221 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13222 TREE_TYPE (t),
13223 TREE_OPERAND (t, 0),
13224 TREE_OPERAND (t, 1),
13225 strict_overflow_p, depth);
13227 case tcc_unary:
13228 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13229 TREE_TYPE (t),
13230 TREE_OPERAND (t, 0),
13231 strict_overflow_p, depth);
13233 case tcc_constant:
13234 case tcc_declaration:
13235 case tcc_reference:
13236 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13238 default:
13239 break;
13242 switch (code)
13244 case TRUTH_AND_EXPR:
13245 case TRUTH_OR_EXPR:
13246 case TRUTH_XOR_EXPR:
13247 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13248 TREE_TYPE (t),
13249 TREE_OPERAND (t, 0),
13250 TREE_OPERAND (t, 1),
13251 strict_overflow_p, depth);
13252 case TRUTH_NOT_EXPR:
13253 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13254 TREE_TYPE (t),
13255 TREE_OPERAND (t, 0),
13256 strict_overflow_p, depth);
13258 case COND_EXPR:
13259 case CONSTRUCTOR:
13260 case OBJ_TYPE_REF:
13261 case ASSERT_EXPR:
13262 case ADDR_EXPR:
13263 case WITH_SIZE_EXPR:
13264 case SSA_NAME:
13265 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13267 default:
13268 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13272 /* Return true if `t' is known to be non-negative. Handle warnings
13273 about undefined signed overflow. */
13275 bool
13276 tree_expr_nonnegative_p (tree t)
13278 bool ret, strict_overflow_p;
13280 strict_overflow_p = false;
13281 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13282 if (strict_overflow_p)
13283 fold_overflow_warning (("assuming signed overflow does not occur when "
13284 "determining that expression is always "
13285 "non-negative"),
13286 WARN_STRICT_OVERFLOW_MISC);
13287 return ret;
13291 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13292 For floating point we further ensure that T is not denormal.
13293 Similar logic is present in nonzero_address in rtlanal.h.
13295 If the return value is based on the assumption that signed overflow
13296 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13297 change *STRICT_OVERFLOW_P. */
13299 bool
13300 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13301 bool *strict_overflow_p)
13303 switch (code)
13305 case ABS_EXPR:
13306 return tree_expr_nonzero_warnv_p (op0,
13307 strict_overflow_p);
13309 case NOP_EXPR:
13311 tree inner_type = TREE_TYPE (op0);
13312 tree outer_type = type;
13314 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13315 && tree_expr_nonzero_warnv_p (op0,
13316 strict_overflow_p));
13318 break;
13320 case NON_LVALUE_EXPR:
13321 return tree_expr_nonzero_warnv_p (op0,
13322 strict_overflow_p);
13324 default:
13325 break;
13328 return false;
13331 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13332 For floating point we further ensure that T is not denormal.
13333 Similar logic is present in nonzero_address in rtlanal.h.
13335 If the return value is based on the assumption that signed overflow
13336 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13337 change *STRICT_OVERFLOW_P. */
13339 bool
13340 tree_binary_nonzero_warnv_p (enum tree_code code,
13341 tree type,
13342 tree op0,
13343 tree op1, bool *strict_overflow_p)
13345 bool sub_strict_overflow_p;
13346 switch (code)
13348 case POINTER_PLUS_EXPR:
13349 case PLUS_EXPR:
13350 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13352 /* With the presence of negative values it is hard
13353 to say something. */
13354 sub_strict_overflow_p = false;
13355 if (!tree_expr_nonnegative_warnv_p (op0,
13356 &sub_strict_overflow_p)
13357 || !tree_expr_nonnegative_warnv_p (op1,
13358 &sub_strict_overflow_p))
13359 return false;
13360 /* One of operands must be positive and the other non-negative. */
13361 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13362 overflows, on a twos-complement machine the sum of two
13363 nonnegative numbers can never be zero. */
13364 return (tree_expr_nonzero_warnv_p (op0,
13365 strict_overflow_p)
13366 || tree_expr_nonzero_warnv_p (op1,
13367 strict_overflow_p));
13369 break;
13371 case MULT_EXPR:
13372 if (TYPE_OVERFLOW_UNDEFINED (type))
13374 if (tree_expr_nonzero_warnv_p (op0,
13375 strict_overflow_p)
13376 && tree_expr_nonzero_warnv_p (op1,
13377 strict_overflow_p))
13379 *strict_overflow_p = true;
13380 return true;
13383 break;
13385 case MIN_EXPR:
13386 sub_strict_overflow_p = false;
13387 if (tree_expr_nonzero_warnv_p (op0,
13388 &sub_strict_overflow_p)
13389 && tree_expr_nonzero_warnv_p (op1,
13390 &sub_strict_overflow_p))
13392 if (sub_strict_overflow_p)
13393 *strict_overflow_p = true;
13395 break;
13397 case MAX_EXPR:
13398 sub_strict_overflow_p = false;
13399 if (tree_expr_nonzero_warnv_p (op0,
13400 &sub_strict_overflow_p))
13402 if (sub_strict_overflow_p)
13403 *strict_overflow_p = true;
13405 /* When both operands are nonzero, then MAX must be too. */
13406 if (tree_expr_nonzero_warnv_p (op1,
13407 strict_overflow_p))
13408 return true;
13410 /* MAX where operand 0 is positive is positive. */
13411 return tree_expr_nonnegative_warnv_p (op0,
13412 strict_overflow_p);
13414 /* MAX where operand 1 is positive is positive. */
13415 else if (tree_expr_nonzero_warnv_p (op1,
13416 &sub_strict_overflow_p)
13417 && tree_expr_nonnegative_warnv_p (op1,
13418 &sub_strict_overflow_p))
13420 if (sub_strict_overflow_p)
13421 *strict_overflow_p = true;
13422 return true;
13424 break;
13426 case BIT_IOR_EXPR:
13427 return (tree_expr_nonzero_warnv_p (op1,
13428 strict_overflow_p)
13429 || tree_expr_nonzero_warnv_p (op0,
13430 strict_overflow_p));
13432 default:
13433 break;
13436 return false;
13439 /* Return true when T is an address and is known to be nonzero.
13440 For floating point we further ensure that T is not denormal.
13441 Similar logic is present in nonzero_address in rtlanal.h.
13443 If the return value is based on the assumption that signed overflow
13444 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13445 change *STRICT_OVERFLOW_P. */
13447 bool
13448 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13450 bool sub_strict_overflow_p;
13451 switch (TREE_CODE (t))
13453 case INTEGER_CST:
13454 return !integer_zerop (t);
13456 case ADDR_EXPR:
13458 tree base = TREE_OPERAND (t, 0);
13460 if (!DECL_P (base))
13461 base = get_base_address (base);
13463 if (!base)
13464 return false;
13466 /* For objects in symbol table check if we know they are non-zero.
13467 Don't do anything for variables and functions before symtab is built;
13468 it is quite possible that they will be declared weak later. */
13469 if (DECL_P (base) && decl_in_symtab_p (base))
13471 struct symtab_node *symbol;
13473 symbol = symtab_node::get_create (base);
13474 if (symbol)
13475 return symbol->nonzero_address ();
13476 else
13477 return false;
13480 /* Function local objects are never NULL. */
13481 if (DECL_P (base)
13482 && (DECL_CONTEXT (base)
13483 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
13484 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
13485 return true;
13487 /* Constants are never weak. */
13488 if (CONSTANT_CLASS_P (base))
13489 return true;
13491 return false;
13494 case COND_EXPR:
13495 sub_strict_overflow_p = false;
13496 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13497 &sub_strict_overflow_p)
13498 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13499 &sub_strict_overflow_p))
13501 if (sub_strict_overflow_p)
13502 *strict_overflow_p = true;
13503 return true;
13505 break;
13507 default:
13508 break;
13510 return false;
13513 #define integer_valued_real_p(X) \
13514 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13516 #define RECURSE(X) \
13517 ((integer_valued_real_p) (X, depth + 1))
13519 /* Return true if the floating point result of (CODE OP0) has an
13520 integer value. We also allow +Inf, -Inf and NaN to be considered
13521 integer values. Return false for signaling NaN.
13523 DEPTH is the current nesting depth of the query. */
13525 bool
13526 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13528 switch (code)
13530 case FLOAT_EXPR:
13531 return true;
13533 case ABS_EXPR:
13534 return RECURSE (op0);
13536 CASE_CONVERT:
13538 tree type = TREE_TYPE (op0);
13539 if (TREE_CODE (type) == INTEGER_TYPE)
13540 return true;
13541 if (TREE_CODE (type) == REAL_TYPE)
13542 return RECURSE (op0);
13543 break;
13546 default:
13547 break;
13549 return false;
13552 /* Return true if the floating point result of (CODE OP0 OP1) has an
13553 integer value. We also allow +Inf, -Inf and NaN to be considered
13554 integer values. Return false for signaling NaN.
13556 DEPTH is the current nesting depth of the query. */
13558 bool
13559 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13561 switch (code)
13563 case PLUS_EXPR:
13564 case MINUS_EXPR:
13565 case MULT_EXPR:
13566 case MIN_EXPR:
13567 case MAX_EXPR:
13568 return RECURSE (op0) && RECURSE (op1);
13570 default:
13571 break;
13573 return false;
13576 /* Return true if the floating point result of calling FNDECL with arguments
13577 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13578 considered integer values. Return false for signaling NaN. If FNDECL
13579 takes fewer than 2 arguments, the remaining ARGn are null.
13581 DEPTH is the current nesting depth of the query. */
13583 bool
13584 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13586 switch (fn)
13588 CASE_CFN_CEIL:
13589 CASE_CFN_FLOOR:
13590 CASE_CFN_NEARBYINT:
13591 CASE_CFN_RINT:
13592 CASE_CFN_ROUND:
13593 CASE_CFN_TRUNC:
13594 return true;
13596 CASE_CFN_FMIN:
13597 CASE_CFN_FMAX:
13598 return RECURSE (arg0) && RECURSE (arg1);
13600 default:
13601 break;
13603 return false;
13606 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13607 has an integer value. We also allow +Inf, -Inf and NaN to be
13608 considered integer values. Return false for signaling NaN.
13610 DEPTH is the current nesting depth of the query. */
13612 bool
13613 integer_valued_real_single_p (tree t, int depth)
13615 switch (TREE_CODE (t))
13617 case REAL_CST:
13618 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13620 case COND_EXPR:
13621 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13623 case SSA_NAME:
13624 /* Limit the depth of recursion to avoid quadratic behavior.
13625 This is expected to catch almost all occurrences in practice.
13626 If this code misses important cases that unbounded recursion
13627 would not, passes that need this information could be revised
13628 to provide it through dataflow propagation. */
13629 return (!name_registered_for_update_p (t)
13630 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13631 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13632 depth));
13634 default:
13635 break;
13637 return false;
13640 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13641 has an integer value. We also allow +Inf, -Inf and NaN to be
13642 considered integer values. Return false for signaling NaN.
13644 DEPTH is the current nesting depth of the query. */
13646 static bool
13647 integer_valued_real_invalid_p (tree t, int depth)
13649 switch (TREE_CODE (t))
13651 case COMPOUND_EXPR:
13652 case MODIFY_EXPR:
13653 case BIND_EXPR:
13654 return RECURSE (TREE_OPERAND (t, 1));
13656 case SAVE_EXPR:
13657 return RECURSE (TREE_OPERAND (t, 0));
13659 default:
13660 break;
13662 return false;
13665 #undef RECURSE
13666 #undef integer_valued_real_p
13668 /* Return true if the floating point expression T has an integer value.
13669 We also allow +Inf, -Inf and NaN to be considered integer values.
13670 Return false for signaling NaN.
13672 DEPTH is the current nesting depth of the query. */
13674 bool
13675 integer_valued_real_p (tree t, int depth)
13677 if (t == error_mark_node)
13678 return false;
13680 tree_code code = TREE_CODE (t);
13681 switch (TREE_CODE_CLASS (code))
13683 case tcc_binary:
13684 case tcc_comparison:
13685 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13686 TREE_OPERAND (t, 1), depth);
13688 case tcc_unary:
13689 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13691 case tcc_constant:
13692 case tcc_declaration:
13693 case tcc_reference:
13694 return integer_valued_real_single_p (t, depth);
13696 default:
13697 break;
13700 switch (code)
13702 case COND_EXPR:
13703 case SSA_NAME:
13704 return integer_valued_real_single_p (t, depth);
13706 case CALL_EXPR:
13708 tree arg0 = (call_expr_nargs (t) > 0
13709 ? CALL_EXPR_ARG (t, 0)
13710 : NULL_TREE);
13711 tree arg1 = (call_expr_nargs (t) > 1
13712 ? CALL_EXPR_ARG (t, 1)
13713 : NULL_TREE);
13714 return integer_valued_real_call_p (get_call_combined_fn (t),
13715 arg0, arg1, depth);
13718 default:
13719 return integer_valued_real_invalid_p (t, depth);
13723 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13724 attempt to fold the expression to a constant without modifying TYPE,
13725 OP0 or OP1.
13727 If the expression could be simplified to a constant, then return
13728 the constant. If the expression would not be simplified to a
13729 constant, then return NULL_TREE. */
13731 tree
13732 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13734 tree tem = fold_binary (code, type, op0, op1);
13735 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13738 /* Given the components of a unary expression CODE, TYPE and OP0,
13739 attempt to fold the expression to a constant without modifying
13740 TYPE or OP0.
13742 If the expression could be simplified to a constant, then return
13743 the constant. If the expression would not be simplified to a
13744 constant, then return NULL_TREE. */
13746 tree
13747 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13749 tree tem = fold_unary (code, type, op0);
13750 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13753 /* If EXP represents referencing an element in a constant string
13754 (either via pointer arithmetic or array indexing), return the
13755 tree representing the value accessed, otherwise return NULL. */
13757 tree
13758 fold_read_from_constant_string (tree exp)
13760 if ((TREE_CODE (exp) == INDIRECT_REF
13761 || TREE_CODE (exp) == ARRAY_REF)
13762 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13764 tree exp1 = TREE_OPERAND (exp, 0);
13765 tree index;
13766 tree string;
13767 location_t loc = EXPR_LOCATION (exp);
13769 if (TREE_CODE (exp) == INDIRECT_REF)
13770 string = string_constant (exp1, &index);
13771 else
13773 tree low_bound = array_ref_low_bound (exp);
13774 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13776 /* Optimize the special-case of a zero lower bound.
13778 We convert the low_bound to sizetype to avoid some problems
13779 with constant folding. (E.g. suppose the lower bound is 1,
13780 and its mode is QI. Without the conversion,l (ARRAY
13781 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13782 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13783 if (! integer_zerop (low_bound))
13784 index = size_diffop_loc (loc, index,
13785 fold_convert_loc (loc, sizetype, low_bound));
13787 string = exp1;
13790 if (string
13791 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13792 && TREE_CODE (string) == STRING_CST
13793 && TREE_CODE (index) == INTEGER_CST
13794 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13795 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
13796 == MODE_INT)
13797 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
13798 return build_int_cst_type (TREE_TYPE (exp),
13799 (TREE_STRING_POINTER (string)
13800 [TREE_INT_CST_LOW (index)]));
13802 return NULL;
13805 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13806 an integer constant, real, or fixed-point constant.
13808 TYPE is the type of the result. */
13810 static tree
13811 fold_negate_const (tree arg0, tree type)
13813 tree t = NULL_TREE;
13815 switch (TREE_CODE (arg0))
13817 case INTEGER_CST:
13819 bool overflow;
13820 wide_int val = wi::neg (arg0, &overflow);
13821 t = force_fit_type (type, val, 1,
13822 (overflow | TREE_OVERFLOW (arg0))
13823 && !TYPE_UNSIGNED (type));
13824 break;
13827 case REAL_CST:
13828 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13829 break;
13831 case FIXED_CST:
13833 FIXED_VALUE_TYPE f;
13834 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13835 &(TREE_FIXED_CST (arg0)), NULL,
13836 TYPE_SATURATING (type));
13837 t = build_fixed (type, f);
13838 /* Propagate overflow flags. */
13839 if (overflow_p | TREE_OVERFLOW (arg0))
13840 TREE_OVERFLOW (t) = 1;
13841 break;
13844 default:
13845 gcc_unreachable ();
13848 return t;
13851 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13852 an integer constant or real constant.
13854 TYPE is the type of the result. */
13856 tree
13857 fold_abs_const (tree arg0, tree type)
13859 tree t = NULL_TREE;
13861 switch (TREE_CODE (arg0))
13863 case INTEGER_CST:
13865 /* If the value is unsigned or non-negative, then the absolute value
13866 is the same as the ordinary value. */
13867 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
13868 t = arg0;
13870 /* If the value is negative, then the absolute value is
13871 its negation. */
13872 else
13874 bool overflow;
13875 wide_int val = wi::neg (arg0, &overflow);
13876 t = force_fit_type (type, val, -1,
13877 overflow | TREE_OVERFLOW (arg0));
13880 break;
13882 case REAL_CST:
13883 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13884 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13885 else
13886 t = arg0;
13887 break;
13889 default:
13890 gcc_unreachable ();
13893 return t;
13896 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13897 constant. TYPE is the type of the result. */
13899 static tree
13900 fold_not_const (const_tree arg0, tree type)
13902 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13904 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
13907 /* Given CODE, a relational operator, the target type, TYPE and two
13908 constant operands OP0 and OP1, return the result of the
13909 relational operation. If the result is not a compile time
13910 constant, then return NULL_TREE. */
13912 static tree
13913 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13915 int result, invert;
13917 /* From here on, the only cases we handle are when the result is
13918 known to be a constant. */
13920 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13922 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13923 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13925 /* Handle the cases where either operand is a NaN. */
13926 if (real_isnan (c0) || real_isnan (c1))
13928 switch (code)
13930 case EQ_EXPR:
13931 case ORDERED_EXPR:
13932 result = 0;
13933 break;
13935 case NE_EXPR:
13936 case UNORDERED_EXPR:
13937 case UNLT_EXPR:
13938 case UNLE_EXPR:
13939 case UNGT_EXPR:
13940 case UNGE_EXPR:
13941 case UNEQ_EXPR:
13942 result = 1;
13943 break;
13945 case LT_EXPR:
13946 case LE_EXPR:
13947 case GT_EXPR:
13948 case GE_EXPR:
13949 case LTGT_EXPR:
13950 if (flag_trapping_math)
13951 return NULL_TREE;
13952 result = 0;
13953 break;
13955 default:
13956 gcc_unreachable ();
13959 return constant_boolean_node (result, type);
13962 return constant_boolean_node (real_compare (code, c0, c1), type);
13965 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13967 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13968 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13969 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13972 /* Handle equality/inequality of complex constants. */
13973 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13975 tree rcond = fold_relational_const (code, type,
13976 TREE_REALPART (op0),
13977 TREE_REALPART (op1));
13978 tree icond = fold_relational_const (code, type,
13979 TREE_IMAGPART (op0),
13980 TREE_IMAGPART (op1));
13981 if (code == EQ_EXPR)
13982 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13983 else if (code == NE_EXPR)
13984 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13985 else
13986 return NULL_TREE;
13989 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13991 if (!VECTOR_TYPE_P (type))
13993 /* Have vector comparison with scalar boolean result. */
13994 bool result = true;
13995 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13996 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13997 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13999 tree elem0 = VECTOR_CST_ELT (op0, i);
14000 tree elem1 = VECTOR_CST_ELT (op1, i);
14001 tree tmp = fold_relational_const (code, type, elem0, elem1);
14002 result &= integer_onep (tmp);
14004 if (code == NE_EXPR)
14005 result = !result;
14006 return constant_boolean_node (result, type);
14008 unsigned count = VECTOR_CST_NELTS (op0);
14009 tree *elts = XALLOCAVEC (tree, count);
14010 gcc_assert (VECTOR_CST_NELTS (op1) == count
14011 && TYPE_VECTOR_SUBPARTS (type) == count);
14013 for (unsigned i = 0; i < count; i++)
14015 tree elem_type = TREE_TYPE (type);
14016 tree elem0 = VECTOR_CST_ELT (op0, i);
14017 tree elem1 = VECTOR_CST_ELT (op1, i);
14019 tree tem = fold_relational_const (code, elem_type,
14020 elem0, elem1);
14022 if (tem == NULL_TREE)
14023 return NULL_TREE;
14025 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
14028 return build_vector (type, elts);
14031 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14033 To compute GT, swap the arguments and do LT.
14034 To compute GE, do LT and invert the result.
14035 To compute LE, swap the arguments, do LT and invert the result.
14036 To compute NE, do EQ and invert the result.
14038 Therefore, the code below must handle only EQ and LT. */
14040 if (code == LE_EXPR || code == GT_EXPR)
14042 std::swap (op0, op1);
14043 code = swap_tree_comparison (code);
14046 /* Note that it is safe to invert for real values here because we
14047 have already handled the one case that it matters. */
14049 invert = 0;
14050 if (code == NE_EXPR || code == GE_EXPR)
14052 invert = 1;
14053 code = invert_tree_comparison (code, false);
14056 /* Compute a result for LT or EQ if args permit;
14057 Otherwise return T. */
14058 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14060 if (code == EQ_EXPR)
14061 result = tree_int_cst_equal (op0, op1);
14062 else
14063 result = tree_int_cst_lt (op0, op1);
14065 else
14066 return NULL_TREE;
14068 if (invert)
14069 result ^= 1;
14070 return constant_boolean_node (result, type);
14073 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14074 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14075 itself. */
14077 tree
14078 fold_build_cleanup_point_expr (tree type, tree expr)
14080 /* If the expression does not have side effects then we don't have to wrap
14081 it with a cleanup point expression. */
14082 if (!TREE_SIDE_EFFECTS (expr))
14083 return expr;
14085 /* If the expression is a return, check to see if the expression inside the
14086 return has no side effects or the right hand side of the modify expression
14087 inside the return. If either don't have side effects set we don't need to
14088 wrap the expression in a cleanup point expression. Note we don't check the
14089 left hand side of the modify because it should always be a return decl. */
14090 if (TREE_CODE (expr) == RETURN_EXPR)
14092 tree op = TREE_OPERAND (expr, 0);
14093 if (!op || !TREE_SIDE_EFFECTS (op))
14094 return expr;
14095 op = TREE_OPERAND (op, 1);
14096 if (!TREE_SIDE_EFFECTS (op))
14097 return expr;
14100 return build1 (CLEANUP_POINT_EXPR, type, expr);
14103 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14104 of an indirection through OP0, or NULL_TREE if no simplification is
14105 possible. */
14107 tree
14108 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14110 tree sub = op0;
14111 tree subtype;
14113 STRIP_NOPS (sub);
14114 subtype = TREE_TYPE (sub);
14115 if (!POINTER_TYPE_P (subtype))
14116 return NULL_TREE;
14118 if (TREE_CODE (sub) == ADDR_EXPR)
14120 tree op = TREE_OPERAND (sub, 0);
14121 tree optype = TREE_TYPE (op);
14122 /* *&CONST_DECL -> to the value of the const decl. */
14123 if (TREE_CODE (op) == CONST_DECL)
14124 return DECL_INITIAL (op);
14125 /* *&p => p; make sure to handle *&"str"[cst] here. */
14126 if (type == optype)
14128 tree fop = fold_read_from_constant_string (op);
14129 if (fop)
14130 return fop;
14131 else
14132 return op;
14134 /* *(foo *)&fooarray => fooarray[0] */
14135 else if (TREE_CODE (optype) == ARRAY_TYPE
14136 && type == TREE_TYPE (optype)
14137 && (!in_gimple_form
14138 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14140 tree type_domain = TYPE_DOMAIN (optype);
14141 tree min_val = size_zero_node;
14142 if (type_domain && TYPE_MIN_VALUE (type_domain))
14143 min_val = TYPE_MIN_VALUE (type_domain);
14144 if (in_gimple_form
14145 && TREE_CODE (min_val) != INTEGER_CST)
14146 return NULL_TREE;
14147 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14148 NULL_TREE, NULL_TREE);
14150 /* *(foo *)&complexfoo => __real__ complexfoo */
14151 else if (TREE_CODE (optype) == COMPLEX_TYPE
14152 && type == TREE_TYPE (optype))
14153 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14154 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14155 else if (TREE_CODE (optype) == VECTOR_TYPE
14156 && type == TREE_TYPE (optype))
14158 tree part_width = TYPE_SIZE (type);
14159 tree index = bitsize_int (0);
14160 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14164 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14165 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14167 tree op00 = TREE_OPERAND (sub, 0);
14168 tree op01 = TREE_OPERAND (sub, 1);
14170 STRIP_NOPS (op00);
14171 if (TREE_CODE (op00) == ADDR_EXPR)
14173 tree op00type;
14174 op00 = TREE_OPERAND (op00, 0);
14175 op00type = TREE_TYPE (op00);
14177 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14178 if (TREE_CODE (op00type) == VECTOR_TYPE
14179 && type == TREE_TYPE (op00type))
14181 HOST_WIDE_INT offset = tree_to_shwi (op01);
14182 tree part_width = TYPE_SIZE (type);
14183 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
14184 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
14185 tree index = bitsize_int (indexi);
14187 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
14188 return fold_build3_loc (loc,
14189 BIT_FIELD_REF, type, op00,
14190 part_width, index);
14193 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14194 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14195 && type == TREE_TYPE (op00type))
14197 tree size = TYPE_SIZE_UNIT (type);
14198 if (tree_int_cst_equal (size, op01))
14199 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14201 /* ((foo *)&fooarray)[1] => fooarray[1] */
14202 else if (TREE_CODE (op00type) == ARRAY_TYPE
14203 && type == TREE_TYPE (op00type))
14205 tree type_domain = TYPE_DOMAIN (op00type);
14206 tree min_val = size_zero_node;
14207 if (type_domain && TYPE_MIN_VALUE (type_domain))
14208 min_val = TYPE_MIN_VALUE (type_domain);
14209 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
14210 TYPE_SIZE_UNIT (type));
14211 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
14212 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14213 NULL_TREE, NULL_TREE);
14218 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14219 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14220 && type == TREE_TYPE (TREE_TYPE (subtype))
14221 && (!in_gimple_form
14222 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14224 tree type_domain;
14225 tree min_val = size_zero_node;
14226 sub = build_fold_indirect_ref_loc (loc, sub);
14227 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14228 if (type_domain && TYPE_MIN_VALUE (type_domain))
14229 min_val = TYPE_MIN_VALUE (type_domain);
14230 if (in_gimple_form
14231 && TREE_CODE (min_val) != INTEGER_CST)
14232 return NULL_TREE;
14233 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14234 NULL_TREE);
14237 return NULL_TREE;
14240 /* Builds an expression for an indirection through T, simplifying some
14241 cases. */
14243 tree
14244 build_fold_indirect_ref_loc (location_t loc, tree t)
14246 tree type = TREE_TYPE (TREE_TYPE (t));
14247 tree sub = fold_indirect_ref_1 (loc, type, t);
14249 if (sub)
14250 return sub;
14252 return build1_loc (loc, INDIRECT_REF, type, t);
14255 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14257 tree
14258 fold_indirect_ref_loc (location_t loc, tree t)
14260 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14262 if (sub)
14263 return sub;
14264 else
14265 return t;
14268 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14269 whose result is ignored. The type of the returned tree need not be
14270 the same as the original expression. */
14272 tree
14273 fold_ignored_result (tree t)
14275 if (!TREE_SIDE_EFFECTS (t))
14276 return integer_zero_node;
14278 for (;;)
14279 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14281 case tcc_unary:
14282 t = TREE_OPERAND (t, 0);
14283 break;
14285 case tcc_binary:
14286 case tcc_comparison:
14287 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14288 t = TREE_OPERAND (t, 0);
14289 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14290 t = TREE_OPERAND (t, 1);
14291 else
14292 return t;
14293 break;
14295 case tcc_expression:
14296 switch (TREE_CODE (t))
14298 case COMPOUND_EXPR:
14299 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14300 return t;
14301 t = TREE_OPERAND (t, 0);
14302 break;
14304 case COND_EXPR:
14305 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14306 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14307 return t;
14308 t = TREE_OPERAND (t, 0);
14309 break;
14311 default:
14312 return t;
14314 break;
14316 default:
14317 return t;
14321 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14323 tree
14324 round_up_loc (location_t loc, tree value, unsigned int divisor)
14326 tree div = NULL_TREE;
14328 if (divisor == 1)
14329 return value;
14331 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14332 have to do anything. Only do this when we are not given a const,
14333 because in that case, this check is more expensive than just
14334 doing it. */
14335 if (TREE_CODE (value) != INTEGER_CST)
14337 div = build_int_cst (TREE_TYPE (value), divisor);
14339 if (multiple_of_p (TREE_TYPE (value), value, div))
14340 return value;
14343 /* If divisor is a power of two, simplify this to bit manipulation. */
14344 if (divisor == (divisor & -divisor))
14346 if (TREE_CODE (value) == INTEGER_CST)
14348 wide_int val = value;
14349 bool overflow_p;
14351 if ((val & (divisor - 1)) == 0)
14352 return value;
14354 overflow_p = TREE_OVERFLOW (value);
14355 val += divisor - 1;
14356 val &= - (int) divisor;
14357 if (val == 0)
14358 overflow_p = true;
14360 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14362 else
14364 tree t;
14366 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14367 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14368 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14369 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14372 else
14374 if (!div)
14375 div = build_int_cst (TREE_TYPE (value), divisor);
14376 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14377 value = size_binop_loc (loc, MULT_EXPR, value, div);
14380 return value;
14383 /* Likewise, but round down. */
14385 tree
14386 round_down_loc (location_t loc, tree value, int divisor)
14388 tree div = NULL_TREE;
14390 gcc_assert (divisor > 0);
14391 if (divisor == 1)
14392 return value;
14394 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14395 have to do anything. Only do this when we are not given a const,
14396 because in that case, this check is more expensive than just
14397 doing it. */
14398 if (TREE_CODE (value) != INTEGER_CST)
14400 div = build_int_cst (TREE_TYPE (value), divisor);
14402 if (multiple_of_p (TREE_TYPE (value), value, div))
14403 return value;
14406 /* If divisor is a power of two, simplify this to bit manipulation. */
14407 if (divisor == (divisor & -divisor))
14409 tree t;
14411 t = build_int_cst (TREE_TYPE (value), -divisor);
14412 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14414 else
14416 if (!div)
14417 div = build_int_cst (TREE_TYPE (value), divisor);
14418 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14419 value = size_binop_loc (loc, MULT_EXPR, value, div);
14422 return value;
14425 /* Returns the pointer to the base of the object addressed by EXP and
14426 extracts the information about the offset of the access, storing it
14427 to PBITPOS and POFFSET. */
14429 static tree
14430 split_address_to_core_and_offset (tree exp,
14431 HOST_WIDE_INT *pbitpos, tree *poffset)
14433 tree core;
14434 machine_mode mode;
14435 int unsignedp, reversep, volatilep;
14436 HOST_WIDE_INT bitsize;
14437 location_t loc = EXPR_LOCATION (exp);
14439 if (TREE_CODE (exp) == ADDR_EXPR)
14441 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14442 poffset, &mode, &unsignedp, &reversep,
14443 &volatilep, false);
14444 core = build_fold_addr_expr_loc (loc, core);
14446 else
14448 core = exp;
14449 *pbitpos = 0;
14450 *poffset = NULL_TREE;
14453 return core;
14456 /* Returns true if addresses of E1 and E2 differ by a constant, false
14457 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14459 bool
14460 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14462 tree core1, core2;
14463 HOST_WIDE_INT bitpos1, bitpos2;
14464 tree toffset1, toffset2, tdiff, type;
14466 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14467 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14469 if (bitpos1 % BITS_PER_UNIT != 0
14470 || bitpos2 % BITS_PER_UNIT != 0
14471 || !operand_equal_p (core1, core2, 0))
14472 return false;
14474 if (toffset1 && toffset2)
14476 type = TREE_TYPE (toffset1);
14477 if (type != TREE_TYPE (toffset2))
14478 toffset2 = fold_convert (type, toffset2);
14480 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14481 if (!cst_and_fits_in_hwi (tdiff))
14482 return false;
14484 *diff = int_cst_value (tdiff);
14486 else if (toffset1 || toffset2)
14488 /* If only one of the offsets is non-constant, the difference cannot
14489 be a constant. */
14490 return false;
14492 else
14493 *diff = 0;
14495 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14496 return true;
14499 /* Return OFF converted to a pointer offset type suitable as offset for
14500 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14501 tree
14502 convert_to_ptrofftype_loc (location_t loc, tree off)
14504 return fold_convert_loc (loc, sizetype, off);
14507 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14508 tree
14509 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14511 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14512 ptr, convert_to_ptrofftype_loc (loc, off));
14515 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14516 tree
14517 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14519 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14520 ptr, size_int (off));
14523 /* Return a char pointer for a C string if it is a string constant
14524 or sum of string constant and integer constant. */
14526 const char *
14527 c_getstr (tree src)
14529 tree offset_node;
14531 src = string_constant (src, &offset_node);
14532 if (src == 0)
14533 return 0;
14535 if (offset_node == 0)
14536 return TREE_STRING_POINTER (src);
14537 else if (!tree_fits_uhwi_p (offset_node)
14538 || compare_tree_int (offset_node, TREE_STRING_LENGTH (src) - 1) > 0)
14539 return 0;
14541 return TREE_STRING_POINTER (src) + tree_to_uhwi (offset_node);