Fix type in the changelog entry,
[official-gcc.git] / gcc / fold-const.c
blob7231fd65d50fac7e9efbd6c1969cb06f77d785cd
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2015 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 "predict.h"
48 #include "tree.h"
49 #include "gimple.h"
50 #include "rtl.h"
51 #include "flags.h"
52 #include "alias.h"
53 #include "fold-const.h"
54 #include "stor-layout.h"
55 #include "calls.h"
56 #include "tree-iterator.h"
57 #include "realmpfr.h"
58 #include "insn-config.h"
59 #include "expmed.h"
60 #include "dojump.h"
61 #include "explow.h"
62 #include "emit-rtl.h"
63 #include "varasm.h"
64 #include "stmt.h"
65 #include "expr.h"
66 #include "tm_p.h"
67 #include "target.h"
68 #include "diagnostic-core.h"
69 #include "intl.h"
70 #include "langhooks.h"
71 #include "md5.h"
72 #include "internal-fn.h"
73 #include "tree-eh.h"
74 #include "gimplify.h"
75 #include "tree-dfa.h"
76 #include "builtins.h"
77 #include "cgraph.h"
78 #include "generic-match.h"
79 #include "optabs-query.h"
81 #ifndef LOAD_EXTEND_OP
82 #define LOAD_EXTEND_OP(M) UNKNOWN
83 #endif
85 /* Nonzero if we are folding constants inside an initializer; zero
86 otherwise. */
87 int folding_initializer = 0;
89 /* The following constants represent a bit based encoding of GCC's
90 comparison operators. This encoding simplifies transformations
91 on relational comparison operators, such as AND and OR. */
92 enum comparison_code {
93 COMPCODE_FALSE = 0,
94 COMPCODE_LT = 1,
95 COMPCODE_EQ = 2,
96 COMPCODE_LE = 3,
97 COMPCODE_GT = 4,
98 COMPCODE_LTGT = 5,
99 COMPCODE_GE = 6,
100 COMPCODE_ORD = 7,
101 COMPCODE_UNORD = 8,
102 COMPCODE_UNLT = 9,
103 COMPCODE_UNEQ = 10,
104 COMPCODE_UNLE = 11,
105 COMPCODE_UNGT = 12,
106 COMPCODE_NE = 13,
107 COMPCODE_UNGE = 14,
108 COMPCODE_TRUE = 15
111 static bool negate_mathfn_p (enum built_in_function);
112 static bool negate_expr_p (tree);
113 static tree negate_expr (tree);
114 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
115 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
116 static enum comparison_code comparison_to_compcode (enum tree_code);
117 static enum tree_code compcode_to_comparison (enum comparison_code);
118 static int operand_equal_for_comparison_p (tree, tree, tree);
119 static int twoval_comparison_p (tree, tree *, tree *, int *);
120 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
121 static tree make_bit_field_ref (location_t, tree, tree,
122 HOST_WIDE_INT, HOST_WIDE_INT, int);
123 static tree optimize_bit_field_compare (location_t, enum tree_code,
124 tree, tree, tree);
125 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
126 HOST_WIDE_INT *,
127 machine_mode *, int *, int *,
128 tree *, tree *);
129 static int simple_operand_p (const_tree);
130 static bool simple_operand_p_2 (tree);
131 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
132 static tree range_predecessor (tree);
133 static tree range_successor (tree);
134 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
135 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
136 static tree unextend (tree, int, int, tree);
137 static tree optimize_minmax_comparison (location_t, enum tree_code,
138 tree, tree, tree);
139 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
140 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
141 static tree fold_binary_op_with_conditional_arg (location_t,
142 enum tree_code, tree,
143 tree, tree,
144 tree, tree, int);
145 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
146 static bool reorder_operands_p (const_tree, const_tree);
147 static tree fold_negate_const (tree, tree);
148 static tree fold_not_const (const_tree, tree);
149 static tree fold_relational_const (enum tree_code, tree, tree, tree);
150 static tree fold_convert_const (enum tree_code, tree, tree);
151 static tree fold_view_convert_expr (tree, tree);
152 static bool vec_cst_ctor_to_array (tree, tree *);
155 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
156 Otherwise, return LOC. */
158 static location_t
159 expr_location_or (tree t, location_t loc)
161 location_t tloc = EXPR_LOCATION (t);
162 return tloc == UNKNOWN_LOCATION ? loc : tloc;
165 /* Similar to protected_set_expr_location, but never modify x in place,
166 if location can and needs to be set, unshare it. */
168 static inline tree
169 protected_set_expr_location_unshare (tree x, location_t loc)
171 if (CAN_HAVE_LOCATION_P (x)
172 && EXPR_LOCATION (x) != loc
173 && !(TREE_CODE (x) == SAVE_EXPR
174 || TREE_CODE (x) == TARGET_EXPR
175 || TREE_CODE (x) == BIND_EXPR))
177 x = copy_node (x);
178 SET_EXPR_LOCATION (x, loc);
180 return x;
183 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
184 division and returns the quotient. Otherwise returns
185 NULL_TREE. */
187 tree
188 div_if_zero_remainder (const_tree arg1, const_tree arg2)
190 widest_int quo;
192 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
193 SIGNED, &quo))
194 return wide_int_to_tree (TREE_TYPE (arg1), quo);
196 return NULL_TREE;
199 /* This is nonzero if we should defer warnings about undefined
200 overflow. This facility exists because these warnings are a
201 special case. The code to estimate loop iterations does not want
202 to issue any warnings, since it works with expressions which do not
203 occur in user code. Various bits of cleanup code call fold(), but
204 only use the result if it has certain characteristics (e.g., is a
205 constant); that code only wants to issue a warning if the result is
206 used. */
208 static int fold_deferring_overflow_warnings;
210 /* If a warning about undefined overflow is deferred, this is the
211 warning. Note that this may cause us to turn two warnings into
212 one, but that is fine since it is sufficient to only give one
213 warning per expression. */
215 static const char* fold_deferred_overflow_warning;
217 /* If a warning about undefined overflow is deferred, this is the
218 level at which the warning should be emitted. */
220 static enum warn_strict_overflow_code fold_deferred_overflow_code;
222 /* Start deferring overflow warnings. We could use a stack here to
223 permit nested calls, but at present it is not necessary. */
225 void
226 fold_defer_overflow_warnings (void)
228 ++fold_deferring_overflow_warnings;
231 /* Stop deferring overflow warnings. If there is a pending warning,
232 and ISSUE is true, then issue the warning if appropriate. STMT is
233 the statement with which the warning should be associated (used for
234 location information); STMT may be NULL. CODE is the level of the
235 warning--a warn_strict_overflow_code value. This function will use
236 the smaller of CODE and the deferred code when deciding whether to
237 issue the warning. CODE may be zero to mean to always use the
238 deferred code. */
240 void
241 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
243 const char *warnmsg;
244 location_t locus;
246 gcc_assert (fold_deferring_overflow_warnings > 0);
247 --fold_deferring_overflow_warnings;
248 if (fold_deferring_overflow_warnings > 0)
250 if (fold_deferred_overflow_warning != NULL
251 && code != 0
252 && code < (int) fold_deferred_overflow_code)
253 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
254 return;
257 warnmsg = fold_deferred_overflow_warning;
258 fold_deferred_overflow_warning = NULL;
260 if (!issue || warnmsg == NULL)
261 return;
263 if (gimple_no_warning_p (stmt))
264 return;
266 /* Use the smallest code level when deciding to issue the
267 warning. */
268 if (code == 0 || code > (int) fold_deferred_overflow_code)
269 code = fold_deferred_overflow_code;
271 if (!issue_strict_overflow_warning (code))
272 return;
274 if (stmt == NULL)
275 locus = input_location;
276 else
277 locus = gimple_location (stmt);
278 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
281 /* Stop deferring overflow warnings, ignoring any deferred
282 warnings. */
284 void
285 fold_undefer_and_ignore_overflow_warnings (void)
287 fold_undefer_overflow_warnings (false, NULL, 0);
290 /* Whether we are deferring overflow warnings. */
292 bool
293 fold_deferring_overflow_warnings_p (void)
295 return fold_deferring_overflow_warnings > 0;
298 /* This is called when we fold something based on the fact that signed
299 overflow is undefined. */
301 static void
302 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
304 if (fold_deferring_overflow_warnings > 0)
306 if (fold_deferred_overflow_warning == NULL
307 || wc < fold_deferred_overflow_code)
309 fold_deferred_overflow_warning = gmsgid;
310 fold_deferred_overflow_code = wc;
313 else if (issue_strict_overflow_warning (wc))
314 warning (OPT_Wstrict_overflow, gmsgid);
317 /* Return true if the built-in mathematical function specified by CODE
318 is odd, i.e. -f(x) == f(-x). */
320 static bool
321 negate_mathfn_p (enum built_in_function code)
323 switch (code)
325 CASE_FLT_FN (BUILT_IN_ASIN):
326 CASE_FLT_FN (BUILT_IN_ASINH):
327 CASE_FLT_FN (BUILT_IN_ATAN):
328 CASE_FLT_FN (BUILT_IN_ATANH):
329 CASE_FLT_FN (BUILT_IN_CASIN):
330 CASE_FLT_FN (BUILT_IN_CASINH):
331 CASE_FLT_FN (BUILT_IN_CATAN):
332 CASE_FLT_FN (BUILT_IN_CATANH):
333 CASE_FLT_FN (BUILT_IN_CBRT):
334 CASE_FLT_FN (BUILT_IN_CPROJ):
335 CASE_FLT_FN (BUILT_IN_CSIN):
336 CASE_FLT_FN (BUILT_IN_CSINH):
337 CASE_FLT_FN (BUILT_IN_CTAN):
338 CASE_FLT_FN (BUILT_IN_CTANH):
339 CASE_FLT_FN (BUILT_IN_ERF):
340 CASE_FLT_FN (BUILT_IN_LLROUND):
341 CASE_FLT_FN (BUILT_IN_LROUND):
342 CASE_FLT_FN (BUILT_IN_ROUND):
343 CASE_FLT_FN (BUILT_IN_SIN):
344 CASE_FLT_FN (BUILT_IN_SINH):
345 CASE_FLT_FN (BUILT_IN_TAN):
346 CASE_FLT_FN (BUILT_IN_TANH):
347 CASE_FLT_FN (BUILT_IN_TRUNC):
348 return true;
350 CASE_FLT_FN (BUILT_IN_LLRINT):
351 CASE_FLT_FN (BUILT_IN_LRINT):
352 CASE_FLT_FN (BUILT_IN_NEARBYINT):
353 CASE_FLT_FN (BUILT_IN_RINT):
354 return !flag_rounding_math;
356 default:
357 break;
359 return false;
362 /* Check whether we may negate an integer constant T without causing
363 overflow. */
365 bool
366 may_negate_without_overflow_p (const_tree t)
368 tree type;
370 gcc_assert (TREE_CODE (t) == INTEGER_CST);
372 type = TREE_TYPE (t);
373 if (TYPE_UNSIGNED (type))
374 return false;
376 return !wi::only_sign_bit_p (t);
379 /* Determine whether an expression T can be cheaply negated using
380 the function negate_expr without introducing undefined overflow. */
382 static bool
383 negate_expr_p (tree t)
385 tree type;
387 if (t == 0)
388 return false;
390 type = TREE_TYPE (t);
392 STRIP_SIGN_NOPS (t);
393 switch (TREE_CODE (t))
395 case INTEGER_CST:
396 if (INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
397 return true;
399 /* Check that -CST will not overflow type. */
400 return may_negate_without_overflow_p (t);
401 case BIT_NOT_EXPR:
402 return (INTEGRAL_TYPE_P (type)
403 && TYPE_OVERFLOW_WRAPS (type));
405 case FIXED_CST:
406 return true;
408 case NEGATE_EXPR:
409 return !TYPE_OVERFLOW_SANITIZED (type);
411 case REAL_CST:
412 /* We want to canonicalize to positive real constants. Pretend
413 that only negative ones can be easily negated. */
414 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
416 case COMPLEX_CST:
417 return negate_expr_p (TREE_REALPART (t))
418 && negate_expr_p (TREE_IMAGPART (t));
420 case VECTOR_CST:
422 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
423 return true;
425 int count = TYPE_VECTOR_SUBPARTS (type), i;
427 for (i = 0; i < count; i++)
428 if (!negate_expr_p (VECTOR_CST_ELT (t, i)))
429 return false;
431 return true;
434 case COMPLEX_EXPR:
435 return negate_expr_p (TREE_OPERAND (t, 0))
436 && negate_expr_p (TREE_OPERAND (t, 1));
438 case CONJ_EXPR:
439 return negate_expr_p (TREE_OPERAND (t, 0));
441 case PLUS_EXPR:
442 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
443 || HONOR_SIGNED_ZEROS (element_mode (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 && reorder_operands_p (TREE_OPERAND (t, 0),
458 TREE_OPERAND (t, 1));
460 case MULT_EXPR:
461 if (TYPE_UNSIGNED (TREE_TYPE (t)))
462 break;
464 /* Fall through. */
466 case RDIV_EXPR:
467 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
468 return negate_expr_p (TREE_OPERAND (t, 1))
469 || negate_expr_p (TREE_OPERAND (t, 0));
470 break;
472 case TRUNC_DIV_EXPR:
473 case ROUND_DIV_EXPR:
474 case EXACT_DIV_EXPR:
475 /* In general we can't negate A / B, because if A is INT_MIN and
476 B is 1, we may turn this into INT_MIN / -1 which is undefined
477 and actually traps on some architectures. But if overflow is
478 undefined, we can negate, because - (INT_MIN / 1) is an
479 overflow. */
480 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
482 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
483 break;
484 /* If overflow is undefined then we have to be careful because
485 we ask whether it's ok to associate the negate with the
486 division which is not ok for example for
487 -((a - b) / c) where (-(a - b)) / c may invoke undefined
488 overflow because of negating INT_MIN. So do not use
489 negate_expr_p here but open-code the two important cases. */
490 if (TREE_CODE (TREE_OPERAND (t, 0)) == NEGATE_EXPR
491 || (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
492 && may_negate_without_overflow_p (TREE_OPERAND (t, 0))))
493 return true;
495 else if (negate_expr_p (TREE_OPERAND (t, 0)))
496 return true;
497 return negate_expr_p (TREE_OPERAND (t, 1));
499 case NOP_EXPR:
500 /* Negate -((double)float) as (double)(-float). */
501 if (TREE_CODE (type) == REAL_TYPE)
503 tree tem = strip_float_extensions (t);
504 if (tem != t)
505 return negate_expr_p (tem);
507 break;
509 case CALL_EXPR:
510 /* Negate -f(x) as f(-x). */
511 if (negate_mathfn_p (builtin_mathfn_code (t)))
512 return negate_expr_p (CALL_EXPR_ARG (t, 0));
513 break;
515 case RSHIFT_EXPR:
516 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
517 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
519 tree op1 = TREE_OPERAND (t, 1);
520 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
521 return true;
523 break;
525 default:
526 break;
528 return false;
531 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
532 simplification is possible.
533 If negate_expr_p would return true for T, NULL_TREE will never be
534 returned. */
536 static tree
537 fold_negate_expr (location_t loc, tree t)
539 tree type = TREE_TYPE (t);
540 tree tem;
542 switch (TREE_CODE (t))
544 /* Convert - (~A) to A + 1. */
545 case BIT_NOT_EXPR:
546 if (INTEGRAL_TYPE_P (type))
547 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
548 build_one_cst (type));
549 break;
551 case INTEGER_CST:
552 tem = fold_negate_const (t, type);
553 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
554 || (ANY_INTEGRAL_TYPE_P (type)
555 && !TYPE_OVERFLOW_TRAPS (type)
556 && TYPE_OVERFLOW_WRAPS (type))
557 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
558 return tem;
559 break;
561 case REAL_CST:
562 tem = fold_negate_const (t, type);
563 return tem;
565 case FIXED_CST:
566 tem = fold_negate_const (t, type);
567 return tem;
569 case COMPLEX_CST:
571 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
572 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
573 if (rpart && ipart)
574 return build_complex (type, rpart, ipart);
576 break;
578 case VECTOR_CST:
580 int count = TYPE_VECTOR_SUBPARTS (type), i;
581 tree *elts = XALLOCAVEC (tree, count);
583 for (i = 0; i < count; i++)
585 elts[i] = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
586 if (elts[i] == NULL_TREE)
587 return NULL_TREE;
590 return build_vector (type, elts);
593 case COMPLEX_EXPR:
594 if (negate_expr_p (t))
595 return fold_build2_loc (loc, COMPLEX_EXPR, type,
596 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
597 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
598 break;
600 case CONJ_EXPR:
601 if (negate_expr_p (t))
602 return fold_build1_loc (loc, CONJ_EXPR, type,
603 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
604 break;
606 case NEGATE_EXPR:
607 if (!TYPE_OVERFLOW_SANITIZED (type))
608 return TREE_OPERAND (t, 0);
609 break;
611 case PLUS_EXPR:
612 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
613 && !HONOR_SIGNED_ZEROS (element_mode (type)))
615 /* -(A + B) -> (-B) - A. */
616 if (negate_expr_p (TREE_OPERAND (t, 1))
617 && reorder_operands_p (TREE_OPERAND (t, 0),
618 TREE_OPERAND (t, 1)))
620 tem = negate_expr (TREE_OPERAND (t, 1));
621 return fold_build2_loc (loc, MINUS_EXPR, type,
622 tem, TREE_OPERAND (t, 0));
625 /* -(A + B) -> (-A) - B. */
626 if (negate_expr_p (TREE_OPERAND (t, 0)))
628 tem = negate_expr (TREE_OPERAND (t, 0));
629 return fold_build2_loc (loc, MINUS_EXPR, type,
630 tem, TREE_OPERAND (t, 1));
633 break;
635 case MINUS_EXPR:
636 /* - (A - B) -> B - A */
637 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
638 && !HONOR_SIGNED_ZEROS (element_mode (type))
639 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
640 return fold_build2_loc (loc, MINUS_EXPR, type,
641 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
642 break;
644 case MULT_EXPR:
645 if (TYPE_UNSIGNED (type))
646 break;
648 /* Fall through. */
650 case RDIV_EXPR:
651 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
653 tem = TREE_OPERAND (t, 1);
654 if (negate_expr_p (tem))
655 return fold_build2_loc (loc, TREE_CODE (t), type,
656 TREE_OPERAND (t, 0), negate_expr (tem));
657 tem = TREE_OPERAND (t, 0);
658 if (negate_expr_p (tem))
659 return fold_build2_loc (loc, TREE_CODE (t), type,
660 negate_expr (tem), TREE_OPERAND (t, 1));
662 break;
664 case TRUNC_DIV_EXPR:
665 case ROUND_DIV_EXPR:
666 case EXACT_DIV_EXPR:
667 /* In general we can't negate A / B, because if A is INT_MIN and
668 B is 1, we may turn this into INT_MIN / -1 which is undefined
669 and actually traps on some architectures. But if overflow is
670 undefined, we can negate, because - (INT_MIN / 1) is an
671 overflow. */
672 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
674 const char * const warnmsg = G_("assuming signed overflow does not "
675 "occur when negating a division");
676 tem = TREE_OPERAND (t, 1);
677 if (negate_expr_p (tem))
679 if (INTEGRAL_TYPE_P (type)
680 && (TREE_CODE (tem) != INTEGER_CST
681 || integer_onep (tem)))
682 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
683 return fold_build2_loc (loc, TREE_CODE (t), type,
684 TREE_OPERAND (t, 0), negate_expr (tem));
686 /* If overflow is undefined then we have to be careful because
687 we ask whether it's ok to associate the negate with the
688 division which is not ok for example for
689 -((a - b) / c) where (-(a - b)) / c may invoke undefined
690 overflow because of negating INT_MIN. So do not use
691 negate_expr_p here but open-code the two important cases. */
692 tem = TREE_OPERAND (t, 0);
693 if ((INTEGRAL_TYPE_P (type)
694 && (TREE_CODE (tem) == NEGATE_EXPR
695 || (TREE_CODE (tem) == INTEGER_CST
696 && may_negate_without_overflow_p (tem))))
697 || !INTEGRAL_TYPE_P (type))
698 return fold_build2_loc (loc, TREE_CODE (t), type,
699 negate_expr (tem), TREE_OPERAND (t, 1));
701 break;
703 case NOP_EXPR:
704 /* Convert -((double)float) into (double)(-float). */
705 if (TREE_CODE (type) == REAL_TYPE)
707 tem = strip_float_extensions (t);
708 if (tem != t && negate_expr_p (tem))
709 return fold_convert_loc (loc, type, negate_expr (tem));
711 break;
713 case CALL_EXPR:
714 /* Negate -f(x) as f(-x). */
715 if (negate_mathfn_p (builtin_mathfn_code (t))
716 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
718 tree fndecl, arg;
720 fndecl = get_callee_fndecl (t);
721 arg = negate_expr (CALL_EXPR_ARG (t, 0));
722 return build_call_expr_loc (loc, fndecl, 1, arg);
724 break;
726 case RSHIFT_EXPR:
727 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
728 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
730 tree op1 = TREE_OPERAND (t, 1);
731 if (wi::eq_p (op1, TYPE_PRECISION (type) - 1))
733 tree ntype = TYPE_UNSIGNED (type)
734 ? signed_type_for (type)
735 : unsigned_type_for (type);
736 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
737 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
738 return fold_convert_loc (loc, type, temp);
741 break;
743 default:
744 break;
747 return NULL_TREE;
750 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
751 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
752 return NULL_TREE. */
754 static tree
755 negate_expr (tree t)
757 tree type, tem;
758 location_t loc;
760 if (t == NULL_TREE)
761 return NULL_TREE;
763 loc = EXPR_LOCATION (t);
764 type = TREE_TYPE (t);
765 STRIP_SIGN_NOPS (t);
767 tem = fold_negate_expr (loc, t);
768 if (!tem)
769 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
770 return fold_convert_loc (loc, type, tem);
773 /* Split a tree IN into a constant, literal and variable parts that could be
774 combined with CODE to make IN. "constant" means an expression with
775 TREE_CONSTANT but that isn't an actual constant. CODE must be a
776 commutative arithmetic operation. Store the constant part into *CONP,
777 the literal in *LITP and return the variable part. If a part isn't
778 present, set it to null. If the tree does not decompose in this way,
779 return the entire tree as the variable part and the other parts as null.
781 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
782 case, we negate an operand that was subtracted. Except if it is a
783 literal for which we use *MINUS_LITP instead.
785 If NEGATE_P is true, we are negating all of IN, again except a literal
786 for which we use *MINUS_LITP instead.
788 If IN is itself a literal or constant, return it as appropriate.
790 Note that we do not guarantee that any of the three values will be the
791 same type as IN, but they will have the same signedness and mode. */
793 static tree
794 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
795 tree *minus_litp, int negate_p)
797 tree var = 0;
799 *conp = 0;
800 *litp = 0;
801 *minus_litp = 0;
803 /* Strip any conversions that don't change the machine mode or signedness. */
804 STRIP_SIGN_NOPS (in);
806 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
807 || TREE_CODE (in) == FIXED_CST)
808 *litp = in;
809 else if (TREE_CODE (in) == code
810 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
811 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
812 /* We can associate addition and subtraction together (even
813 though the C standard doesn't say so) for integers because
814 the value is not affected. For reals, the value might be
815 affected, so we can't. */
816 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
817 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
819 tree op0 = TREE_OPERAND (in, 0);
820 tree op1 = TREE_OPERAND (in, 1);
821 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
822 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
824 /* First see if either of the operands is a literal, then a constant. */
825 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
826 || TREE_CODE (op0) == FIXED_CST)
827 *litp = op0, op0 = 0;
828 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
829 || TREE_CODE (op1) == FIXED_CST)
830 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
832 if (op0 != 0 && TREE_CONSTANT (op0))
833 *conp = op0, op0 = 0;
834 else if (op1 != 0 && TREE_CONSTANT (op1))
835 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
837 /* If we haven't dealt with either operand, this is not a case we can
838 decompose. Otherwise, VAR is either of the ones remaining, if any. */
839 if (op0 != 0 && op1 != 0)
840 var = in;
841 else if (op0 != 0)
842 var = op0;
843 else
844 var = op1, neg_var_p = neg1_p;
846 /* Now do any needed negations. */
847 if (neg_litp_p)
848 *minus_litp = *litp, *litp = 0;
849 if (neg_conp_p)
850 *conp = negate_expr (*conp);
851 if (neg_var_p)
852 var = negate_expr (var);
854 else if (TREE_CODE (in) == BIT_NOT_EXPR
855 && code == PLUS_EXPR)
857 /* -X - 1 is folded to ~X, undo that here. */
858 *minus_litp = build_one_cst (TREE_TYPE (in));
859 var = negate_expr (TREE_OPERAND (in, 0));
861 else if (TREE_CONSTANT (in))
862 *conp = in;
863 else
864 var = in;
866 if (negate_p)
868 if (*litp)
869 *minus_litp = *litp, *litp = 0;
870 else if (*minus_litp)
871 *litp = *minus_litp, *minus_litp = 0;
872 *conp = negate_expr (*conp);
873 var = negate_expr (var);
876 return var;
879 /* Re-associate trees split by the above function. T1 and T2 are
880 either expressions to associate or null. Return the new
881 expression, if any. LOC is the location of the new expression. If
882 we build an operation, do it in TYPE and with CODE. */
884 static tree
885 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
887 if (t1 == 0)
888 return t2;
889 else if (t2 == 0)
890 return t1;
892 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
893 try to fold this since we will have infinite recursion. But do
894 deal with any NEGATE_EXPRs. */
895 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
896 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
898 if (code == PLUS_EXPR)
900 if (TREE_CODE (t1) == NEGATE_EXPR)
901 return build2_loc (loc, MINUS_EXPR, type,
902 fold_convert_loc (loc, type, t2),
903 fold_convert_loc (loc, type,
904 TREE_OPERAND (t1, 0)));
905 else if (TREE_CODE (t2) == NEGATE_EXPR)
906 return build2_loc (loc, MINUS_EXPR, type,
907 fold_convert_loc (loc, type, t1),
908 fold_convert_loc (loc, type,
909 TREE_OPERAND (t2, 0)));
910 else if (integer_zerop (t2))
911 return fold_convert_loc (loc, type, t1);
913 else if (code == MINUS_EXPR)
915 if (integer_zerop (t2))
916 return fold_convert_loc (loc, type, t1);
919 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
920 fold_convert_loc (loc, type, t2));
923 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
924 fold_convert_loc (loc, type, t2));
927 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
928 for use in int_const_binop, size_binop and size_diffop. */
930 static bool
931 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
933 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
934 return false;
935 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
936 return false;
938 switch (code)
940 case LSHIFT_EXPR:
941 case RSHIFT_EXPR:
942 case LROTATE_EXPR:
943 case RROTATE_EXPR:
944 return true;
946 default:
947 break;
950 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
951 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
952 && TYPE_MODE (type1) == TYPE_MODE (type2);
956 /* Combine two integer constants ARG1 and ARG2 under operation CODE
957 to produce a new constant. Return NULL_TREE if we don't know how
958 to evaluate CODE at compile-time. */
960 static tree
961 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree parg2,
962 int overflowable)
964 wide_int res;
965 tree t;
966 tree type = TREE_TYPE (arg1);
967 signop sign = TYPE_SIGN (type);
968 bool overflow = false;
970 wide_int arg2 = wide_int::from (parg2, TYPE_PRECISION (type),
971 TYPE_SIGN (TREE_TYPE (parg2)));
973 switch (code)
975 case BIT_IOR_EXPR:
976 res = wi::bit_or (arg1, arg2);
977 break;
979 case BIT_XOR_EXPR:
980 res = wi::bit_xor (arg1, arg2);
981 break;
983 case BIT_AND_EXPR:
984 res = wi::bit_and (arg1, arg2);
985 break;
987 case RSHIFT_EXPR:
988 case LSHIFT_EXPR:
989 if (wi::neg_p (arg2))
991 arg2 = -arg2;
992 if (code == RSHIFT_EXPR)
993 code = LSHIFT_EXPR;
994 else
995 code = RSHIFT_EXPR;
998 if (code == RSHIFT_EXPR)
999 /* It's unclear from the C standard whether shifts can overflow.
1000 The following code ignores overflow; perhaps a C standard
1001 interpretation ruling is needed. */
1002 res = wi::rshift (arg1, arg2, sign);
1003 else
1004 res = wi::lshift (arg1, arg2);
1005 break;
1007 case RROTATE_EXPR:
1008 case LROTATE_EXPR:
1009 if (wi::neg_p (arg2))
1011 arg2 = -arg2;
1012 if (code == RROTATE_EXPR)
1013 code = LROTATE_EXPR;
1014 else
1015 code = RROTATE_EXPR;
1018 if (code == RROTATE_EXPR)
1019 res = wi::rrotate (arg1, arg2);
1020 else
1021 res = wi::lrotate (arg1, arg2);
1022 break;
1024 case PLUS_EXPR:
1025 res = wi::add (arg1, arg2, sign, &overflow);
1026 break;
1028 case MINUS_EXPR:
1029 res = wi::sub (arg1, arg2, sign, &overflow);
1030 break;
1032 case MULT_EXPR:
1033 res = wi::mul (arg1, arg2, sign, &overflow);
1034 break;
1036 case MULT_HIGHPART_EXPR:
1037 res = wi::mul_high (arg1, arg2, sign);
1038 break;
1040 case TRUNC_DIV_EXPR:
1041 case EXACT_DIV_EXPR:
1042 if (arg2 == 0)
1043 return NULL_TREE;
1044 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1045 break;
1047 case FLOOR_DIV_EXPR:
1048 if (arg2 == 0)
1049 return NULL_TREE;
1050 res = wi::div_floor (arg1, arg2, sign, &overflow);
1051 break;
1053 case CEIL_DIV_EXPR:
1054 if (arg2 == 0)
1055 return NULL_TREE;
1056 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1057 break;
1059 case ROUND_DIV_EXPR:
1060 if (arg2 == 0)
1061 return NULL_TREE;
1062 res = wi::div_round (arg1, arg2, sign, &overflow);
1063 break;
1065 case TRUNC_MOD_EXPR:
1066 if (arg2 == 0)
1067 return NULL_TREE;
1068 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1069 break;
1071 case FLOOR_MOD_EXPR:
1072 if (arg2 == 0)
1073 return NULL_TREE;
1074 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1075 break;
1077 case CEIL_MOD_EXPR:
1078 if (arg2 == 0)
1079 return NULL_TREE;
1080 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1081 break;
1083 case ROUND_MOD_EXPR:
1084 if (arg2 == 0)
1085 return NULL_TREE;
1086 res = wi::mod_round (arg1, arg2, sign, &overflow);
1087 break;
1089 case MIN_EXPR:
1090 res = wi::min (arg1, arg2, sign);
1091 break;
1093 case MAX_EXPR:
1094 res = wi::max (arg1, arg2, sign);
1095 break;
1097 default:
1098 return NULL_TREE;
1101 t = force_fit_type (type, res, overflowable,
1102 (((sign == SIGNED || overflowable == -1)
1103 && overflow)
1104 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (parg2)));
1106 return t;
1109 tree
1110 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1112 return int_const_binop_1 (code, arg1, arg2, 1);
1115 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1116 constant. We assume ARG1 and ARG2 have the same data type, or at least
1117 are the same kind of constant and the same machine mode. Return zero if
1118 combining the constants is not allowed in the current operating mode. */
1120 static tree
1121 const_binop (enum tree_code code, tree arg1, tree arg2)
1123 /* Sanity check for the recursive cases. */
1124 if (!arg1 || !arg2)
1125 return NULL_TREE;
1127 STRIP_NOPS (arg1);
1128 STRIP_NOPS (arg2);
1130 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1132 if (code == POINTER_PLUS_EXPR)
1133 return int_const_binop (PLUS_EXPR,
1134 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1136 return int_const_binop (code, arg1, arg2);
1139 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1141 machine_mode mode;
1142 REAL_VALUE_TYPE d1;
1143 REAL_VALUE_TYPE d2;
1144 REAL_VALUE_TYPE value;
1145 REAL_VALUE_TYPE result;
1146 bool inexact;
1147 tree t, type;
1149 /* The following codes are handled by real_arithmetic. */
1150 switch (code)
1152 case PLUS_EXPR:
1153 case MINUS_EXPR:
1154 case MULT_EXPR:
1155 case RDIV_EXPR:
1156 case MIN_EXPR:
1157 case MAX_EXPR:
1158 break;
1160 default:
1161 return NULL_TREE;
1164 d1 = TREE_REAL_CST (arg1);
1165 d2 = TREE_REAL_CST (arg2);
1167 type = TREE_TYPE (arg1);
1168 mode = TYPE_MODE (type);
1170 /* Don't perform operation if we honor signaling NaNs and
1171 either operand is a NaN. */
1172 if (HONOR_SNANS (mode)
1173 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1174 return NULL_TREE;
1176 /* Don't perform operation if it would raise a division
1177 by zero exception. */
1178 if (code == RDIV_EXPR
1179 && REAL_VALUES_EQUAL (d2, dconst0)
1180 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1181 return NULL_TREE;
1183 /* If either operand is a NaN, just return it. Otherwise, set up
1184 for floating-point trap; we return an overflow. */
1185 if (REAL_VALUE_ISNAN (d1))
1186 return arg1;
1187 else if (REAL_VALUE_ISNAN (d2))
1188 return arg2;
1190 inexact = real_arithmetic (&value, code, &d1, &d2);
1191 real_convert (&result, mode, &value);
1193 /* Don't constant fold this floating point operation if
1194 the result has overflowed and flag_trapping_math. */
1195 if (flag_trapping_math
1196 && MODE_HAS_INFINITIES (mode)
1197 && REAL_VALUE_ISINF (result)
1198 && !REAL_VALUE_ISINF (d1)
1199 && !REAL_VALUE_ISINF (d2))
1200 return NULL_TREE;
1202 /* Don't constant fold this floating point operation if the
1203 result may dependent upon the run-time rounding mode and
1204 flag_rounding_math is set, or if GCC's software emulation
1205 is unable to accurately represent the result. */
1206 if ((flag_rounding_math
1207 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1208 && (inexact || !real_identical (&result, &value)))
1209 return NULL_TREE;
1211 t = build_real (type, result);
1213 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1214 return t;
1217 if (TREE_CODE (arg1) == FIXED_CST)
1219 FIXED_VALUE_TYPE f1;
1220 FIXED_VALUE_TYPE f2;
1221 FIXED_VALUE_TYPE result;
1222 tree t, type;
1223 int sat_p;
1224 bool overflow_p;
1226 /* The following codes are handled by fixed_arithmetic. */
1227 switch (code)
1229 case PLUS_EXPR:
1230 case MINUS_EXPR:
1231 case MULT_EXPR:
1232 case TRUNC_DIV_EXPR:
1233 if (TREE_CODE (arg2) != FIXED_CST)
1234 return NULL_TREE;
1235 f2 = TREE_FIXED_CST (arg2);
1236 break;
1238 case LSHIFT_EXPR:
1239 case RSHIFT_EXPR:
1241 if (TREE_CODE (arg2) != INTEGER_CST)
1242 return NULL_TREE;
1243 wide_int w2 = arg2;
1244 f2.data.high = w2.elt (1);
1245 f2.data.low = w2.elt (0);
1246 f2.mode = SImode;
1248 break;
1250 default:
1251 return NULL_TREE;
1254 f1 = TREE_FIXED_CST (arg1);
1255 type = TREE_TYPE (arg1);
1256 sat_p = TYPE_SATURATING (type);
1257 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1258 t = build_fixed (type, result);
1259 /* Propagate overflow flags. */
1260 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1261 TREE_OVERFLOW (t) = 1;
1262 return t;
1265 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1267 tree type = TREE_TYPE (arg1);
1268 tree r1 = TREE_REALPART (arg1);
1269 tree i1 = TREE_IMAGPART (arg1);
1270 tree r2 = TREE_REALPART (arg2);
1271 tree i2 = TREE_IMAGPART (arg2);
1272 tree real, imag;
1274 switch (code)
1276 case PLUS_EXPR:
1277 case MINUS_EXPR:
1278 real = const_binop (code, r1, r2);
1279 imag = const_binop (code, i1, i2);
1280 break;
1282 case MULT_EXPR:
1283 if (COMPLEX_FLOAT_TYPE_P (type))
1284 return do_mpc_arg2 (arg1, arg2, type,
1285 /* do_nonfinite= */ folding_initializer,
1286 mpc_mul);
1288 real = const_binop (MINUS_EXPR,
1289 const_binop (MULT_EXPR, r1, r2),
1290 const_binop (MULT_EXPR, i1, i2));
1291 imag = const_binop (PLUS_EXPR,
1292 const_binop (MULT_EXPR, r1, i2),
1293 const_binop (MULT_EXPR, i1, r2));
1294 break;
1296 case RDIV_EXPR:
1297 if (COMPLEX_FLOAT_TYPE_P (type))
1298 return do_mpc_arg2 (arg1, arg2, type,
1299 /* do_nonfinite= */ folding_initializer,
1300 mpc_div);
1301 /* Fallthru ... */
1302 case TRUNC_DIV_EXPR:
1303 case CEIL_DIV_EXPR:
1304 case FLOOR_DIV_EXPR:
1305 case ROUND_DIV_EXPR:
1306 if (flag_complex_method == 0)
1308 /* Keep this algorithm in sync with
1309 tree-complex.c:expand_complex_div_straight().
1311 Expand complex division to scalars, straightforward algorithm.
1312 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1313 t = br*br + bi*bi
1315 tree magsquared
1316 = const_binop (PLUS_EXPR,
1317 const_binop (MULT_EXPR, r2, r2),
1318 const_binop (MULT_EXPR, i2, i2));
1319 tree t1
1320 = const_binop (PLUS_EXPR,
1321 const_binop (MULT_EXPR, r1, r2),
1322 const_binop (MULT_EXPR, i1, i2));
1323 tree t2
1324 = const_binop (MINUS_EXPR,
1325 const_binop (MULT_EXPR, i1, r2),
1326 const_binop (MULT_EXPR, r1, i2));
1328 real = const_binop (code, t1, magsquared);
1329 imag = const_binop (code, t2, magsquared);
1331 else
1333 /* Keep this algorithm in sync with
1334 tree-complex.c:expand_complex_div_wide().
1336 Expand complex division to scalars, modified algorithm to minimize
1337 overflow with wide input ranges. */
1338 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1339 fold_abs_const (r2, TREE_TYPE (type)),
1340 fold_abs_const (i2, TREE_TYPE (type)));
1342 if (integer_nonzerop (compare))
1344 /* In the TRUE branch, we compute
1345 ratio = br/bi;
1346 div = (br * ratio) + bi;
1347 tr = (ar * ratio) + ai;
1348 ti = (ai * ratio) - ar;
1349 tr = tr / div;
1350 ti = ti / div; */
1351 tree ratio = const_binop (code, r2, i2);
1352 tree div = const_binop (PLUS_EXPR, i2,
1353 const_binop (MULT_EXPR, r2, ratio));
1354 real = const_binop (MULT_EXPR, r1, ratio);
1355 real = const_binop (PLUS_EXPR, real, i1);
1356 real = const_binop (code, real, div);
1358 imag = const_binop (MULT_EXPR, i1, ratio);
1359 imag = const_binop (MINUS_EXPR, imag, r1);
1360 imag = const_binop (code, imag, div);
1362 else
1364 /* In the FALSE branch, we compute
1365 ratio = d/c;
1366 divisor = (d * ratio) + c;
1367 tr = (b * ratio) + a;
1368 ti = b - (a * ratio);
1369 tr = tr / div;
1370 ti = ti / div; */
1371 tree ratio = const_binop (code, i2, r2);
1372 tree div = const_binop (PLUS_EXPR, r2,
1373 const_binop (MULT_EXPR, i2, ratio));
1375 real = const_binop (MULT_EXPR, i1, ratio);
1376 real = const_binop (PLUS_EXPR, real, r1);
1377 real = const_binop (code, real, div);
1379 imag = const_binop (MULT_EXPR, r1, ratio);
1380 imag = const_binop (MINUS_EXPR, i1, imag);
1381 imag = const_binop (code, imag, div);
1384 break;
1386 default:
1387 return NULL_TREE;
1390 if (real && imag)
1391 return build_complex (type, real, imag);
1394 if (TREE_CODE (arg1) == VECTOR_CST
1395 && TREE_CODE (arg2) == VECTOR_CST)
1397 tree type = TREE_TYPE (arg1);
1398 int count = TYPE_VECTOR_SUBPARTS (type), i;
1399 tree *elts = XALLOCAVEC (tree, count);
1401 for (i = 0; i < count; i++)
1403 tree elem1 = VECTOR_CST_ELT (arg1, i);
1404 tree elem2 = VECTOR_CST_ELT (arg2, i);
1406 elts[i] = const_binop (code, elem1, elem2);
1408 /* It is possible that const_binop cannot handle the given
1409 code and return NULL_TREE */
1410 if (elts[i] == NULL_TREE)
1411 return NULL_TREE;
1414 return build_vector (type, elts);
1417 /* Shifts allow a scalar offset for a vector. */
1418 if (TREE_CODE (arg1) == VECTOR_CST
1419 && TREE_CODE (arg2) == INTEGER_CST)
1421 tree type = TREE_TYPE (arg1);
1422 int count = TYPE_VECTOR_SUBPARTS (type), i;
1423 tree *elts = XALLOCAVEC (tree, count);
1425 for (i = 0; i < count; i++)
1427 tree elem1 = VECTOR_CST_ELT (arg1, i);
1429 elts[i] = const_binop (code, elem1, arg2);
1431 /* It is possible that const_binop cannot handle the given
1432 code and return NULL_TREE. */
1433 if (elts[i] == NULL_TREE)
1434 return NULL_TREE;
1437 return build_vector (type, elts);
1439 return NULL_TREE;
1442 /* Overload that adds a TYPE parameter to be able to dispatch
1443 to fold_relational_const. */
1445 tree
1446 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1448 if (TREE_CODE_CLASS (code) == tcc_comparison)
1449 return fold_relational_const (code, type, arg1, arg2);
1451 /* ??? Until we make the const_binop worker take the type of the
1452 result as argument put those cases that need it here. */
1453 switch (code)
1455 case COMPLEX_EXPR:
1456 if ((TREE_CODE (arg1) == REAL_CST
1457 && TREE_CODE (arg2) == REAL_CST)
1458 || (TREE_CODE (arg1) == INTEGER_CST
1459 && TREE_CODE (arg2) == INTEGER_CST))
1460 return build_complex (type, arg1, arg2);
1461 return NULL_TREE;
1463 case VEC_PACK_TRUNC_EXPR:
1464 case VEC_PACK_FIX_TRUNC_EXPR:
1466 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1467 tree *elts;
1469 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts / 2
1470 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts / 2);
1471 if (TREE_CODE (arg1) != VECTOR_CST
1472 || TREE_CODE (arg2) != VECTOR_CST)
1473 return NULL_TREE;
1475 elts = XALLOCAVEC (tree, nelts);
1476 if (!vec_cst_ctor_to_array (arg1, elts)
1477 || !vec_cst_ctor_to_array (arg2, elts + nelts / 2))
1478 return NULL_TREE;
1480 for (i = 0; i < nelts; i++)
1482 elts[i] = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1483 ? NOP_EXPR : FIX_TRUNC_EXPR,
1484 TREE_TYPE (type), elts[i]);
1485 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1486 return NULL_TREE;
1489 return build_vector (type, elts);
1492 case VEC_WIDEN_MULT_LO_EXPR:
1493 case VEC_WIDEN_MULT_HI_EXPR:
1494 case VEC_WIDEN_MULT_EVEN_EXPR:
1495 case VEC_WIDEN_MULT_ODD_EXPR:
1497 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
1498 unsigned int out, ofs, scale;
1499 tree *elts;
1501 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts * 2
1502 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2)) == nelts * 2);
1503 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1504 return NULL_TREE;
1506 elts = XALLOCAVEC (tree, nelts * 4);
1507 if (!vec_cst_ctor_to_array (arg1, elts)
1508 || !vec_cst_ctor_to_array (arg2, elts + nelts * 2))
1509 return NULL_TREE;
1511 if (code == VEC_WIDEN_MULT_LO_EXPR)
1512 scale = 0, ofs = BYTES_BIG_ENDIAN ? nelts : 0;
1513 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1514 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : nelts;
1515 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1516 scale = 1, ofs = 0;
1517 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1518 scale = 1, ofs = 1;
1520 for (out = 0; out < nelts; out++)
1522 unsigned int in1 = (out << scale) + ofs;
1523 unsigned int in2 = in1 + nelts * 2;
1524 tree t1, t2;
1526 t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in1]);
1527 t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), elts[in2]);
1529 if (t1 == NULL_TREE || t2 == NULL_TREE)
1530 return NULL_TREE;
1531 elts[out] = const_binop (MULT_EXPR, t1, t2);
1532 if (elts[out] == NULL_TREE || !CONSTANT_CLASS_P (elts[out]))
1533 return NULL_TREE;
1536 return build_vector (type, elts);
1539 default:;
1542 if (TREE_CODE_CLASS (code) != tcc_binary)
1543 return NULL_TREE;
1545 /* Make sure type and arg0 have the same saturating flag. */
1546 gcc_checking_assert (TYPE_SATURATING (type)
1547 == TYPE_SATURATING (TREE_TYPE (arg1)));
1549 return const_binop (code, arg1, arg2);
1552 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1553 Return zero if computing the constants is not possible. */
1555 tree
1556 const_unop (enum tree_code code, tree type, tree arg0)
1558 switch (code)
1560 CASE_CONVERT:
1561 case FLOAT_EXPR:
1562 case FIX_TRUNC_EXPR:
1563 case FIXED_CONVERT_EXPR:
1564 return fold_convert_const (code, type, arg0);
1566 case ADDR_SPACE_CONVERT_EXPR:
1567 if (integer_zerop (arg0))
1568 return fold_convert_const (code, type, arg0);
1569 break;
1571 case VIEW_CONVERT_EXPR:
1572 return fold_view_convert_expr (type, arg0);
1574 case NEGATE_EXPR:
1576 /* Can't call fold_negate_const directly here as that doesn't
1577 handle all cases and we might not be able to negate some
1578 constants. */
1579 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1580 if (tem && CONSTANT_CLASS_P (tem))
1581 return tem;
1582 break;
1585 case ABS_EXPR:
1586 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1587 return fold_abs_const (arg0, type);
1588 break;
1590 case CONJ_EXPR:
1591 if (TREE_CODE (arg0) == COMPLEX_CST)
1593 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1594 TREE_TYPE (type));
1595 return build_complex (type, TREE_REALPART (arg0), ipart);
1597 break;
1599 case BIT_NOT_EXPR:
1600 if (TREE_CODE (arg0) == INTEGER_CST)
1601 return fold_not_const (arg0, type);
1602 /* Perform BIT_NOT_EXPR on each element individually. */
1603 else if (TREE_CODE (arg0) == VECTOR_CST)
1605 tree *elements;
1606 tree elem;
1607 unsigned count = VECTOR_CST_NELTS (arg0), i;
1609 elements = XALLOCAVEC (tree, count);
1610 for (i = 0; i < count; i++)
1612 elem = VECTOR_CST_ELT (arg0, i);
1613 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1614 if (elem == NULL_TREE)
1615 break;
1616 elements[i] = elem;
1618 if (i == count)
1619 return build_vector (type, elements);
1621 break;
1623 case TRUTH_NOT_EXPR:
1624 if (TREE_CODE (arg0) == INTEGER_CST)
1625 return constant_boolean_node (integer_zerop (arg0), type);
1626 break;
1628 case REALPART_EXPR:
1629 if (TREE_CODE (arg0) == COMPLEX_CST)
1630 return fold_convert (type, TREE_REALPART (arg0));
1631 break;
1633 case IMAGPART_EXPR:
1634 if (TREE_CODE (arg0) == COMPLEX_CST)
1635 return fold_convert (type, TREE_IMAGPART (arg0));
1636 break;
1638 case VEC_UNPACK_LO_EXPR:
1639 case VEC_UNPACK_HI_EXPR:
1640 case VEC_UNPACK_FLOAT_LO_EXPR:
1641 case VEC_UNPACK_FLOAT_HI_EXPR:
1643 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
1644 tree *elts;
1645 enum tree_code subcode;
1647 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts * 2);
1648 if (TREE_CODE (arg0) != VECTOR_CST)
1649 return NULL_TREE;
1651 elts = XALLOCAVEC (tree, nelts * 2);
1652 if (!vec_cst_ctor_to_array (arg0, elts))
1653 return NULL_TREE;
1655 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1656 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1657 elts += nelts;
1659 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1660 subcode = NOP_EXPR;
1661 else
1662 subcode = FLOAT_EXPR;
1664 for (i = 0; i < nelts; i++)
1666 elts[i] = fold_convert_const (subcode, TREE_TYPE (type), elts[i]);
1667 if (elts[i] == NULL_TREE || !CONSTANT_CLASS_P (elts[i]))
1668 return NULL_TREE;
1671 return build_vector (type, elts);
1674 case REDUC_MIN_EXPR:
1675 case REDUC_MAX_EXPR:
1676 case REDUC_PLUS_EXPR:
1678 unsigned int nelts, i;
1679 tree *elts;
1680 enum tree_code subcode;
1682 if (TREE_CODE (arg0) != VECTOR_CST)
1683 return NULL_TREE;
1684 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
1686 elts = XALLOCAVEC (tree, nelts);
1687 if (!vec_cst_ctor_to_array (arg0, elts))
1688 return NULL_TREE;
1690 switch (code)
1692 case REDUC_MIN_EXPR: subcode = MIN_EXPR; break;
1693 case REDUC_MAX_EXPR: subcode = MAX_EXPR; break;
1694 case REDUC_PLUS_EXPR: subcode = PLUS_EXPR; break;
1695 default: gcc_unreachable ();
1698 for (i = 1; i < nelts; i++)
1700 elts[0] = const_binop (subcode, elts[0], elts[i]);
1701 if (elts[0] == NULL_TREE || !CONSTANT_CLASS_P (elts[0]))
1702 return NULL_TREE;
1705 return elts[0];
1708 default:
1709 break;
1712 return NULL_TREE;
1715 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1716 indicates which particular sizetype to create. */
1718 tree
1719 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1721 return build_int_cst (sizetype_tab[(int) kind], number);
1724 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1725 is a tree code. The type of the result is taken from the operands.
1726 Both must be equivalent integer types, ala int_binop_types_match_p.
1727 If the operands are constant, so is the result. */
1729 tree
1730 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1732 tree type = TREE_TYPE (arg0);
1734 if (arg0 == error_mark_node || arg1 == error_mark_node)
1735 return error_mark_node;
1737 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1738 TREE_TYPE (arg1)));
1740 /* Handle the special case of two integer constants faster. */
1741 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1743 /* And some specific cases even faster than that. */
1744 if (code == PLUS_EXPR)
1746 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1747 return arg1;
1748 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1749 return arg0;
1751 else if (code == MINUS_EXPR)
1753 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1754 return arg0;
1756 else if (code == MULT_EXPR)
1758 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1759 return arg1;
1762 /* Handle general case of two integer constants. For sizetype
1763 constant calculations we always want to know about overflow,
1764 even in the unsigned case. */
1765 return int_const_binop_1 (code, arg0, arg1, -1);
1768 return fold_build2_loc (loc, code, type, arg0, arg1);
1771 /* Given two values, either both of sizetype or both of bitsizetype,
1772 compute the difference between the two values. Return the value
1773 in signed type corresponding to the type of the operands. */
1775 tree
1776 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1778 tree type = TREE_TYPE (arg0);
1779 tree ctype;
1781 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1782 TREE_TYPE (arg1)));
1784 /* If the type is already signed, just do the simple thing. */
1785 if (!TYPE_UNSIGNED (type))
1786 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1788 if (type == sizetype)
1789 ctype = ssizetype;
1790 else if (type == bitsizetype)
1791 ctype = sbitsizetype;
1792 else
1793 ctype = signed_type_for (type);
1795 /* If either operand is not a constant, do the conversions to the signed
1796 type and subtract. The hardware will do the right thing with any
1797 overflow in the subtraction. */
1798 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1799 return size_binop_loc (loc, MINUS_EXPR,
1800 fold_convert_loc (loc, ctype, arg0),
1801 fold_convert_loc (loc, ctype, arg1));
1803 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1804 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1805 overflow) and negate (which can't either). Special-case a result
1806 of zero while we're here. */
1807 if (tree_int_cst_equal (arg0, arg1))
1808 return build_int_cst (ctype, 0);
1809 else if (tree_int_cst_lt (arg1, arg0))
1810 return fold_convert_loc (loc, ctype,
1811 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1812 else
1813 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1814 fold_convert_loc (loc, ctype,
1815 size_binop_loc (loc,
1816 MINUS_EXPR,
1817 arg1, arg0)));
1820 /* A subroutine of fold_convert_const handling conversions of an
1821 INTEGER_CST to another integer type. */
1823 static tree
1824 fold_convert_const_int_from_int (tree type, const_tree arg1)
1826 /* Given an integer constant, make new constant with new type,
1827 appropriately sign-extended or truncated. Use widest_int
1828 so that any extension is done according ARG1's type. */
1829 return force_fit_type (type, wi::to_widest (arg1),
1830 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1831 TREE_OVERFLOW (arg1));
1834 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1835 to an integer type. */
1837 static tree
1838 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1840 bool overflow = false;
1841 tree t;
1843 /* The following code implements the floating point to integer
1844 conversion rules required by the Java Language Specification,
1845 that IEEE NaNs are mapped to zero and values that overflow
1846 the target precision saturate, i.e. values greater than
1847 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1848 are mapped to INT_MIN. These semantics are allowed by the
1849 C and C++ standards that simply state that the behavior of
1850 FP-to-integer conversion is unspecified upon overflow. */
1852 wide_int val;
1853 REAL_VALUE_TYPE r;
1854 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1856 switch (code)
1858 case FIX_TRUNC_EXPR:
1859 real_trunc (&r, VOIDmode, &x);
1860 break;
1862 default:
1863 gcc_unreachable ();
1866 /* If R is NaN, return zero and show we have an overflow. */
1867 if (REAL_VALUE_ISNAN (r))
1869 overflow = true;
1870 val = wi::zero (TYPE_PRECISION (type));
1873 /* See if R is less than the lower bound or greater than the
1874 upper bound. */
1876 if (! overflow)
1878 tree lt = TYPE_MIN_VALUE (type);
1879 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1880 if (REAL_VALUES_LESS (r, l))
1882 overflow = true;
1883 val = lt;
1887 if (! overflow)
1889 tree ut = TYPE_MAX_VALUE (type);
1890 if (ut)
1892 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1893 if (REAL_VALUES_LESS (u, r))
1895 overflow = true;
1896 val = ut;
1901 if (! overflow)
1902 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
1904 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
1905 return t;
1908 /* A subroutine of fold_convert_const handling conversions of a
1909 FIXED_CST to an integer type. */
1911 static tree
1912 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
1914 tree t;
1915 double_int temp, temp_trunc;
1916 unsigned int mode;
1918 /* Right shift FIXED_CST to temp by fbit. */
1919 temp = TREE_FIXED_CST (arg1).data;
1920 mode = TREE_FIXED_CST (arg1).mode;
1921 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
1923 temp = temp.rshift (GET_MODE_FBIT (mode),
1924 HOST_BITS_PER_DOUBLE_INT,
1925 SIGNED_FIXED_POINT_MODE_P (mode));
1927 /* Left shift temp to temp_trunc by fbit. */
1928 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
1929 HOST_BITS_PER_DOUBLE_INT,
1930 SIGNED_FIXED_POINT_MODE_P (mode));
1932 else
1934 temp = double_int_zero;
1935 temp_trunc = double_int_zero;
1938 /* If FIXED_CST is negative, we need to round the value toward 0.
1939 By checking if the fractional bits are not zero to add 1 to temp. */
1940 if (SIGNED_FIXED_POINT_MODE_P (mode)
1941 && temp_trunc.is_negative ()
1942 && TREE_FIXED_CST (arg1).data != temp_trunc)
1943 temp += double_int_one;
1945 /* Given a fixed-point constant, make new constant with new type,
1946 appropriately sign-extended or truncated. */
1947 t = force_fit_type (type, temp, -1,
1948 (temp.is_negative ()
1949 && (TYPE_UNSIGNED (type)
1950 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1951 | TREE_OVERFLOW (arg1));
1953 return t;
1956 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1957 to another floating point type. */
1959 static tree
1960 fold_convert_const_real_from_real (tree type, const_tree arg1)
1962 REAL_VALUE_TYPE value;
1963 tree t;
1965 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1966 t = build_real (type, value);
1968 /* If converting an infinity or NAN to a representation that doesn't
1969 have one, set the overflow bit so that we can produce some kind of
1970 error message at the appropriate point if necessary. It's not the
1971 most user-friendly message, but it's better than nothing. */
1972 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
1973 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
1974 TREE_OVERFLOW (t) = 1;
1975 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
1976 && !MODE_HAS_NANS (TYPE_MODE (type)))
1977 TREE_OVERFLOW (t) = 1;
1978 /* Regular overflow, conversion produced an infinity in a mode that
1979 can't represent them. */
1980 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
1981 && REAL_VALUE_ISINF (value)
1982 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
1983 TREE_OVERFLOW (t) = 1;
1984 else
1985 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1986 return t;
1989 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
1990 to a floating point type. */
1992 static tree
1993 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
1995 REAL_VALUE_TYPE value;
1996 tree t;
1998 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
1999 t = build_real (type, value);
2001 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2002 return t;
2005 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2006 to another fixed-point type. */
2008 static tree
2009 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2011 FIXED_VALUE_TYPE value;
2012 tree t;
2013 bool overflow_p;
2015 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2016 TYPE_SATURATING (type));
2017 t = build_fixed (type, value);
2019 /* Propagate overflow flags. */
2020 if (overflow_p | TREE_OVERFLOW (arg1))
2021 TREE_OVERFLOW (t) = 1;
2022 return t;
2025 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2026 to a fixed-point type. */
2028 static tree
2029 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2031 FIXED_VALUE_TYPE value;
2032 tree t;
2033 bool overflow_p;
2034 double_int di;
2036 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2038 di.low = TREE_INT_CST_ELT (arg1, 0);
2039 if (TREE_INT_CST_NUNITS (arg1) == 1)
2040 di.high = (HOST_WIDE_INT) di.low < 0 ? (HOST_WIDE_INT) -1 : 0;
2041 else
2042 di.high = TREE_INT_CST_ELT (arg1, 1);
2044 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type), di,
2045 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2046 TYPE_SATURATING (type));
2047 t = build_fixed (type, value);
2049 /* Propagate overflow flags. */
2050 if (overflow_p | TREE_OVERFLOW (arg1))
2051 TREE_OVERFLOW (t) = 1;
2052 return t;
2055 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2056 to a fixed-point type. */
2058 static tree
2059 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2061 FIXED_VALUE_TYPE value;
2062 tree t;
2063 bool overflow_p;
2065 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2066 &TREE_REAL_CST (arg1),
2067 TYPE_SATURATING (type));
2068 t = build_fixed (type, value);
2070 /* Propagate overflow flags. */
2071 if (overflow_p | TREE_OVERFLOW (arg1))
2072 TREE_OVERFLOW (t) = 1;
2073 return t;
2076 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2077 type TYPE. If no simplification can be done return NULL_TREE. */
2079 static tree
2080 fold_convert_const (enum tree_code code, tree type, tree arg1)
2082 if (TREE_TYPE (arg1) == type)
2083 return arg1;
2085 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2086 || TREE_CODE (type) == OFFSET_TYPE)
2088 if (TREE_CODE (arg1) == INTEGER_CST)
2089 return fold_convert_const_int_from_int (type, arg1);
2090 else if (TREE_CODE (arg1) == REAL_CST)
2091 return fold_convert_const_int_from_real (code, type, arg1);
2092 else if (TREE_CODE (arg1) == FIXED_CST)
2093 return fold_convert_const_int_from_fixed (type, arg1);
2095 else if (TREE_CODE (type) == REAL_TYPE)
2097 if (TREE_CODE (arg1) == INTEGER_CST)
2098 return build_real_from_int_cst (type, arg1);
2099 else if (TREE_CODE (arg1) == REAL_CST)
2100 return fold_convert_const_real_from_real (type, arg1);
2101 else if (TREE_CODE (arg1) == FIXED_CST)
2102 return fold_convert_const_real_from_fixed (type, arg1);
2104 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2106 if (TREE_CODE (arg1) == FIXED_CST)
2107 return fold_convert_const_fixed_from_fixed (type, arg1);
2108 else if (TREE_CODE (arg1) == INTEGER_CST)
2109 return fold_convert_const_fixed_from_int (type, arg1);
2110 else if (TREE_CODE (arg1) == REAL_CST)
2111 return fold_convert_const_fixed_from_real (type, arg1);
2113 return NULL_TREE;
2116 /* Construct a vector of zero elements of vector type TYPE. */
2118 static tree
2119 build_zero_vector (tree type)
2121 tree t;
2123 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2124 return build_vector_from_val (type, t);
2127 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2129 bool
2130 fold_convertible_p (const_tree type, const_tree arg)
2132 tree orig = TREE_TYPE (arg);
2134 if (type == orig)
2135 return true;
2137 if (TREE_CODE (arg) == ERROR_MARK
2138 || TREE_CODE (type) == ERROR_MARK
2139 || TREE_CODE (orig) == ERROR_MARK)
2140 return false;
2142 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2143 return true;
2145 switch (TREE_CODE (type))
2147 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2148 case POINTER_TYPE: case REFERENCE_TYPE:
2149 case OFFSET_TYPE:
2150 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2151 || TREE_CODE (orig) == OFFSET_TYPE)
2152 return true;
2153 return (TREE_CODE (orig) == VECTOR_TYPE
2154 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2156 case REAL_TYPE:
2157 case FIXED_POINT_TYPE:
2158 case COMPLEX_TYPE:
2159 case VECTOR_TYPE:
2160 case VOID_TYPE:
2161 return TREE_CODE (type) == TREE_CODE (orig);
2163 default:
2164 return false;
2168 /* Convert expression ARG to type TYPE. Used by the middle-end for
2169 simple conversions in preference to calling the front-end's convert. */
2171 tree
2172 fold_convert_loc (location_t loc, tree type, tree arg)
2174 tree orig = TREE_TYPE (arg);
2175 tree tem;
2177 if (type == orig)
2178 return arg;
2180 if (TREE_CODE (arg) == ERROR_MARK
2181 || TREE_CODE (type) == ERROR_MARK
2182 || TREE_CODE (orig) == ERROR_MARK)
2183 return error_mark_node;
2185 switch (TREE_CODE (type))
2187 case POINTER_TYPE:
2188 case REFERENCE_TYPE:
2189 /* Handle conversions between pointers to different address spaces. */
2190 if (POINTER_TYPE_P (orig)
2191 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2192 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2193 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2194 /* fall through */
2196 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2197 case OFFSET_TYPE:
2198 if (TREE_CODE (arg) == INTEGER_CST)
2200 tem = fold_convert_const (NOP_EXPR, type, arg);
2201 if (tem != NULL_TREE)
2202 return tem;
2204 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2205 || TREE_CODE (orig) == OFFSET_TYPE)
2206 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2207 if (TREE_CODE (orig) == COMPLEX_TYPE)
2208 return fold_convert_loc (loc, type,
2209 fold_build1_loc (loc, REALPART_EXPR,
2210 TREE_TYPE (orig), arg));
2211 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2212 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2213 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2215 case REAL_TYPE:
2216 if (TREE_CODE (arg) == INTEGER_CST)
2218 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2219 if (tem != NULL_TREE)
2220 return tem;
2222 else if (TREE_CODE (arg) == REAL_CST)
2224 tem = fold_convert_const (NOP_EXPR, type, arg);
2225 if (tem != NULL_TREE)
2226 return tem;
2228 else if (TREE_CODE (arg) == FIXED_CST)
2230 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2231 if (tem != NULL_TREE)
2232 return tem;
2235 switch (TREE_CODE (orig))
2237 case INTEGER_TYPE:
2238 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2239 case POINTER_TYPE: case REFERENCE_TYPE:
2240 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2242 case REAL_TYPE:
2243 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2245 case FIXED_POINT_TYPE:
2246 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2248 case COMPLEX_TYPE:
2249 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2250 return fold_convert_loc (loc, type, tem);
2252 default:
2253 gcc_unreachable ();
2256 case FIXED_POINT_TYPE:
2257 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2258 || TREE_CODE (arg) == REAL_CST)
2260 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2261 if (tem != NULL_TREE)
2262 goto fold_convert_exit;
2265 switch (TREE_CODE (orig))
2267 case FIXED_POINT_TYPE:
2268 case INTEGER_TYPE:
2269 case ENUMERAL_TYPE:
2270 case BOOLEAN_TYPE:
2271 case REAL_TYPE:
2272 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2274 case COMPLEX_TYPE:
2275 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2276 return fold_convert_loc (loc, type, tem);
2278 default:
2279 gcc_unreachable ();
2282 case COMPLEX_TYPE:
2283 switch (TREE_CODE (orig))
2285 case INTEGER_TYPE:
2286 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2287 case POINTER_TYPE: case REFERENCE_TYPE:
2288 case REAL_TYPE:
2289 case FIXED_POINT_TYPE:
2290 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2291 fold_convert_loc (loc, TREE_TYPE (type), arg),
2292 fold_convert_loc (loc, TREE_TYPE (type),
2293 integer_zero_node));
2294 case COMPLEX_TYPE:
2296 tree rpart, ipart;
2298 if (TREE_CODE (arg) == COMPLEX_EXPR)
2300 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2301 TREE_OPERAND (arg, 0));
2302 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2303 TREE_OPERAND (arg, 1));
2304 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2307 arg = save_expr (arg);
2308 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2309 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2310 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2311 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2312 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2315 default:
2316 gcc_unreachable ();
2319 case VECTOR_TYPE:
2320 if (integer_zerop (arg))
2321 return build_zero_vector (type);
2322 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2323 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2324 || TREE_CODE (orig) == VECTOR_TYPE);
2325 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2327 case VOID_TYPE:
2328 tem = fold_ignored_result (arg);
2329 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2331 default:
2332 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2333 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2334 gcc_unreachable ();
2336 fold_convert_exit:
2337 protected_set_expr_location_unshare (tem, loc);
2338 return tem;
2341 /* Return false if expr can be assumed not to be an lvalue, true
2342 otherwise. */
2344 static bool
2345 maybe_lvalue_p (const_tree x)
2347 /* We only need to wrap lvalue tree codes. */
2348 switch (TREE_CODE (x))
2350 case VAR_DECL:
2351 case PARM_DECL:
2352 case RESULT_DECL:
2353 case LABEL_DECL:
2354 case FUNCTION_DECL:
2355 case SSA_NAME:
2357 case COMPONENT_REF:
2358 case MEM_REF:
2359 case INDIRECT_REF:
2360 case ARRAY_REF:
2361 case ARRAY_RANGE_REF:
2362 case BIT_FIELD_REF:
2363 case OBJ_TYPE_REF:
2365 case REALPART_EXPR:
2366 case IMAGPART_EXPR:
2367 case PREINCREMENT_EXPR:
2368 case PREDECREMENT_EXPR:
2369 case SAVE_EXPR:
2370 case TRY_CATCH_EXPR:
2371 case WITH_CLEANUP_EXPR:
2372 case COMPOUND_EXPR:
2373 case MODIFY_EXPR:
2374 case TARGET_EXPR:
2375 case COND_EXPR:
2376 case BIND_EXPR:
2377 break;
2379 default:
2380 /* Assume the worst for front-end tree codes. */
2381 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2382 break;
2383 return false;
2386 return true;
2389 /* Return an expr equal to X but certainly not valid as an lvalue. */
2391 tree
2392 non_lvalue_loc (location_t loc, tree x)
2394 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2395 us. */
2396 if (in_gimple_form)
2397 return x;
2399 if (! maybe_lvalue_p (x))
2400 return x;
2401 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2404 /* When pedantic, return an expr equal to X but certainly not valid as a
2405 pedantic lvalue. Otherwise, return X. */
2407 static tree
2408 pedantic_non_lvalue_loc (location_t loc, tree x)
2410 return protected_set_expr_location_unshare (x, loc);
2413 /* Given a tree comparison code, return the code that is the logical inverse.
2414 It is generally not safe to do this for floating-point comparisons, except
2415 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2416 ERROR_MARK in this case. */
2418 enum tree_code
2419 invert_tree_comparison (enum tree_code code, bool honor_nans)
2421 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2422 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2423 return ERROR_MARK;
2425 switch (code)
2427 case EQ_EXPR:
2428 return NE_EXPR;
2429 case NE_EXPR:
2430 return EQ_EXPR;
2431 case GT_EXPR:
2432 return honor_nans ? UNLE_EXPR : LE_EXPR;
2433 case GE_EXPR:
2434 return honor_nans ? UNLT_EXPR : LT_EXPR;
2435 case LT_EXPR:
2436 return honor_nans ? UNGE_EXPR : GE_EXPR;
2437 case LE_EXPR:
2438 return honor_nans ? UNGT_EXPR : GT_EXPR;
2439 case LTGT_EXPR:
2440 return UNEQ_EXPR;
2441 case UNEQ_EXPR:
2442 return LTGT_EXPR;
2443 case UNGT_EXPR:
2444 return LE_EXPR;
2445 case UNGE_EXPR:
2446 return LT_EXPR;
2447 case UNLT_EXPR:
2448 return GE_EXPR;
2449 case UNLE_EXPR:
2450 return GT_EXPR;
2451 case ORDERED_EXPR:
2452 return UNORDERED_EXPR;
2453 case UNORDERED_EXPR:
2454 return ORDERED_EXPR;
2455 default:
2456 gcc_unreachable ();
2460 /* Similar, but return the comparison that results if the operands are
2461 swapped. This is safe for floating-point. */
2463 enum tree_code
2464 swap_tree_comparison (enum tree_code code)
2466 switch (code)
2468 case EQ_EXPR:
2469 case NE_EXPR:
2470 case ORDERED_EXPR:
2471 case UNORDERED_EXPR:
2472 case LTGT_EXPR:
2473 case UNEQ_EXPR:
2474 return code;
2475 case GT_EXPR:
2476 return LT_EXPR;
2477 case GE_EXPR:
2478 return LE_EXPR;
2479 case LT_EXPR:
2480 return GT_EXPR;
2481 case LE_EXPR:
2482 return GE_EXPR;
2483 case UNGT_EXPR:
2484 return UNLT_EXPR;
2485 case UNGE_EXPR:
2486 return UNLE_EXPR;
2487 case UNLT_EXPR:
2488 return UNGT_EXPR;
2489 case UNLE_EXPR:
2490 return UNGE_EXPR;
2491 default:
2492 gcc_unreachable ();
2497 /* Convert a comparison tree code from an enum tree_code representation
2498 into a compcode bit-based encoding. This function is the inverse of
2499 compcode_to_comparison. */
2501 static enum comparison_code
2502 comparison_to_compcode (enum tree_code code)
2504 switch (code)
2506 case LT_EXPR:
2507 return COMPCODE_LT;
2508 case EQ_EXPR:
2509 return COMPCODE_EQ;
2510 case LE_EXPR:
2511 return COMPCODE_LE;
2512 case GT_EXPR:
2513 return COMPCODE_GT;
2514 case NE_EXPR:
2515 return COMPCODE_NE;
2516 case GE_EXPR:
2517 return COMPCODE_GE;
2518 case ORDERED_EXPR:
2519 return COMPCODE_ORD;
2520 case UNORDERED_EXPR:
2521 return COMPCODE_UNORD;
2522 case UNLT_EXPR:
2523 return COMPCODE_UNLT;
2524 case UNEQ_EXPR:
2525 return COMPCODE_UNEQ;
2526 case UNLE_EXPR:
2527 return COMPCODE_UNLE;
2528 case UNGT_EXPR:
2529 return COMPCODE_UNGT;
2530 case LTGT_EXPR:
2531 return COMPCODE_LTGT;
2532 case UNGE_EXPR:
2533 return COMPCODE_UNGE;
2534 default:
2535 gcc_unreachable ();
2539 /* Convert a compcode bit-based encoding of a comparison operator back
2540 to GCC's enum tree_code representation. This function is the
2541 inverse of comparison_to_compcode. */
2543 static enum tree_code
2544 compcode_to_comparison (enum comparison_code code)
2546 switch (code)
2548 case COMPCODE_LT:
2549 return LT_EXPR;
2550 case COMPCODE_EQ:
2551 return EQ_EXPR;
2552 case COMPCODE_LE:
2553 return LE_EXPR;
2554 case COMPCODE_GT:
2555 return GT_EXPR;
2556 case COMPCODE_NE:
2557 return NE_EXPR;
2558 case COMPCODE_GE:
2559 return GE_EXPR;
2560 case COMPCODE_ORD:
2561 return ORDERED_EXPR;
2562 case COMPCODE_UNORD:
2563 return UNORDERED_EXPR;
2564 case COMPCODE_UNLT:
2565 return UNLT_EXPR;
2566 case COMPCODE_UNEQ:
2567 return UNEQ_EXPR;
2568 case COMPCODE_UNLE:
2569 return UNLE_EXPR;
2570 case COMPCODE_UNGT:
2571 return UNGT_EXPR;
2572 case COMPCODE_LTGT:
2573 return LTGT_EXPR;
2574 case COMPCODE_UNGE:
2575 return UNGE_EXPR;
2576 default:
2577 gcc_unreachable ();
2581 /* Return a tree for the comparison which is the combination of
2582 doing the AND or OR (depending on CODE) of the two operations LCODE
2583 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2584 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2585 if this makes the transformation invalid. */
2587 tree
2588 combine_comparisons (location_t loc,
2589 enum tree_code code, enum tree_code lcode,
2590 enum tree_code rcode, tree truth_type,
2591 tree ll_arg, tree lr_arg)
2593 bool honor_nans = HONOR_NANS (ll_arg);
2594 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2595 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2596 int compcode;
2598 switch (code)
2600 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2601 compcode = lcompcode & rcompcode;
2602 break;
2604 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2605 compcode = lcompcode | rcompcode;
2606 break;
2608 default:
2609 return NULL_TREE;
2612 if (!honor_nans)
2614 /* Eliminate unordered comparisons, as well as LTGT and ORD
2615 which are not used unless the mode has NaNs. */
2616 compcode &= ~COMPCODE_UNORD;
2617 if (compcode == COMPCODE_LTGT)
2618 compcode = COMPCODE_NE;
2619 else if (compcode == COMPCODE_ORD)
2620 compcode = COMPCODE_TRUE;
2622 else if (flag_trapping_math)
2624 /* Check that the original operation and the optimized ones will trap
2625 under the same condition. */
2626 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2627 && (lcompcode != COMPCODE_EQ)
2628 && (lcompcode != COMPCODE_ORD);
2629 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2630 && (rcompcode != COMPCODE_EQ)
2631 && (rcompcode != COMPCODE_ORD);
2632 bool trap = (compcode & COMPCODE_UNORD) == 0
2633 && (compcode != COMPCODE_EQ)
2634 && (compcode != COMPCODE_ORD);
2636 /* In a short-circuited boolean expression the LHS might be
2637 such that the RHS, if evaluated, will never trap. For
2638 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2639 if neither x nor y is NaN. (This is a mixed blessing: for
2640 example, the expression above will never trap, hence
2641 optimizing it to x < y would be invalid). */
2642 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2643 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2644 rtrap = false;
2646 /* If the comparison was short-circuited, and only the RHS
2647 trapped, we may now generate a spurious trap. */
2648 if (rtrap && !ltrap
2649 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2650 return NULL_TREE;
2652 /* If we changed the conditions that cause a trap, we lose. */
2653 if ((ltrap || rtrap) != trap)
2654 return NULL_TREE;
2657 if (compcode == COMPCODE_TRUE)
2658 return constant_boolean_node (true, truth_type);
2659 else if (compcode == COMPCODE_FALSE)
2660 return constant_boolean_node (false, truth_type);
2661 else
2663 enum tree_code tcode;
2665 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2666 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2670 /* Return nonzero if two operands (typically of the same tree node)
2671 are necessarily equal. If either argument has side-effects this
2672 function returns zero. FLAGS modifies behavior as follows:
2674 If OEP_ONLY_CONST is set, only return nonzero for constants.
2675 This function tests whether the operands are indistinguishable;
2676 it does not test whether they are equal using C's == operation.
2677 The distinction is important for IEEE floating point, because
2678 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2679 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2681 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2682 even though it may hold multiple values during a function.
2683 This is because a GCC tree node guarantees that nothing else is
2684 executed between the evaluation of its "operands" (which may often
2685 be evaluated in arbitrary order). Hence if the operands themselves
2686 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2687 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2688 unset means assuming isochronic (or instantaneous) tree equivalence.
2689 Unless comparing arbitrary expression trees, such as from different
2690 statements, this flag can usually be left unset.
2692 If OEP_PURE_SAME is set, then pure functions with identical arguments
2693 are considered the same. It is used when the caller has other ways
2694 to ensure that global memory is unchanged in between. */
2697 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2699 /* If either is ERROR_MARK, they aren't equal. */
2700 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2701 || TREE_TYPE (arg0) == error_mark_node
2702 || TREE_TYPE (arg1) == error_mark_node)
2703 return 0;
2705 /* Similar, if either does not have a type (like a released SSA name),
2706 they aren't equal. */
2707 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2708 return 0;
2710 /* Check equality of integer constants before bailing out due to
2711 precision differences. */
2712 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2713 return tree_int_cst_equal (arg0, arg1);
2715 /* If both types don't have the same signedness, then we can't consider
2716 them equal. We must check this before the STRIP_NOPS calls
2717 because they may change the signedness of the arguments. As pointers
2718 strictly don't have a signedness, require either two pointers or
2719 two non-pointers as well. */
2720 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2721 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
2722 return 0;
2724 /* We cannot consider pointers to different address space equal. */
2725 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
2726 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2727 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2728 return 0;
2730 /* If both types don't have the same precision, then it is not safe
2731 to strip NOPs. */
2732 if (element_precision (TREE_TYPE (arg0))
2733 != element_precision (TREE_TYPE (arg1)))
2734 return 0;
2736 STRIP_NOPS (arg0);
2737 STRIP_NOPS (arg1);
2739 /* In case both args are comparisons but with different comparison
2740 code, try to swap the comparison operands of one arg to produce
2741 a match and compare that variant. */
2742 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2743 && COMPARISON_CLASS_P (arg0)
2744 && COMPARISON_CLASS_P (arg1))
2746 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2748 if (TREE_CODE (arg0) == swap_code)
2749 return operand_equal_p (TREE_OPERAND (arg0, 0),
2750 TREE_OPERAND (arg1, 1), flags)
2751 && operand_equal_p (TREE_OPERAND (arg0, 1),
2752 TREE_OPERAND (arg1, 0), flags);
2755 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2757 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2758 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2760 else if (flags & OEP_ADDRESS_OF)
2762 /* If we are interested in comparing addresses ignore
2763 MEM_REF wrappings of the base that can appear just for
2764 TBAA reasons. */
2765 if (TREE_CODE (arg0) == MEM_REF
2766 && DECL_P (arg1)
2767 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
2768 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
2769 && integer_zerop (TREE_OPERAND (arg0, 1)))
2770 return 1;
2771 else if (TREE_CODE (arg1) == MEM_REF
2772 && DECL_P (arg0)
2773 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
2774 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
2775 && integer_zerop (TREE_OPERAND (arg1, 1)))
2776 return 1;
2777 return 0;
2779 else
2780 return 0;
2783 /* This is needed for conversions and for COMPONENT_REF.
2784 Might as well play it safe and always test this. */
2785 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2786 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2787 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2788 return 0;
2790 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2791 We don't care about side effects in that case because the SAVE_EXPR
2792 takes care of that for us. In all other cases, two expressions are
2793 equal if they have no side effects. If we have two identical
2794 expressions with side effects that should be treated the same due
2795 to the only side effects being identical SAVE_EXPR's, that will
2796 be detected in the recursive calls below.
2797 If we are taking an invariant address of two identical objects
2798 they are necessarily equal as well. */
2799 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2800 && (TREE_CODE (arg0) == SAVE_EXPR
2801 || (flags & OEP_CONSTANT_ADDRESS_OF)
2802 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2803 return 1;
2805 /* Next handle constant cases, those for which we can return 1 even
2806 if ONLY_CONST is set. */
2807 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2808 switch (TREE_CODE (arg0))
2810 case INTEGER_CST:
2811 return tree_int_cst_equal (arg0, arg1);
2813 case FIXED_CST:
2814 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
2815 TREE_FIXED_CST (arg1));
2817 case REAL_CST:
2818 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2819 TREE_REAL_CST (arg1)))
2820 return 1;
2823 if (!HONOR_SIGNED_ZEROS (arg0))
2825 /* If we do not distinguish between signed and unsigned zero,
2826 consider them equal. */
2827 if (real_zerop (arg0) && real_zerop (arg1))
2828 return 1;
2830 return 0;
2832 case VECTOR_CST:
2834 unsigned i;
2836 if (VECTOR_CST_NELTS (arg0) != VECTOR_CST_NELTS (arg1))
2837 return 0;
2839 for (i = 0; i < VECTOR_CST_NELTS (arg0); ++i)
2841 if (!operand_equal_p (VECTOR_CST_ELT (arg0, i),
2842 VECTOR_CST_ELT (arg1, i), flags))
2843 return 0;
2845 return 1;
2848 case COMPLEX_CST:
2849 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2850 flags)
2851 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2852 flags));
2854 case STRING_CST:
2855 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2856 && ! memcmp (TREE_STRING_POINTER (arg0),
2857 TREE_STRING_POINTER (arg1),
2858 TREE_STRING_LENGTH (arg0)));
2860 case ADDR_EXPR:
2861 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2862 TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1)
2863 ? OEP_CONSTANT_ADDRESS_OF | OEP_ADDRESS_OF : 0);
2864 default:
2865 break;
2868 if (flags & OEP_ONLY_CONST)
2869 return 0;
2871 /* Define macros to test an operand from arg0 and arg1 for equality and a
2872 variant that allows null and views null as being different from any
2873 non-null value. In the latter case, if either is null, the both
2874 must be; otherwise, do the normal comparison. */
2875 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2876 TREE_OPERAND (arg1, N), flags)
2878 #define OP_SAME_WITH_NULL(N) \
2879 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2880 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2882 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2884 case tcc_unary:
2885 /* Two conversions are equal only if signedness and modes match. */
2886 switch (TREE_CODE (arg0))
2888 CASE_CONVERT:
2889 case FIX_TRUNC_EXPR:
2890 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2891 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2892 return 0;
2893 break;
2894 default:
2895 break;
2898 return OP_SAME (0);
2901 case tcc_comparison:
2902 case tcc_binary:
2903 if (OP_SAME (0) && OP_SAME (1))
2904 return 1;
2906 /* For commutative ops, allow the other order. */
2907 return (commutative_tree_code (TREE_CODE (arg0))
2908 && operand_equal_p (TREE_OPERAND (arg0, 0),
2909 TREE_OPERAND (arg1, 1), flags)
2910 && operand_equal_p (TREE_OPERAND (arg0, 1),
2911 TREE_OPERAND (arg1, 0), flags));
2913 case tcc_reference:
2914 /* If either of the pointer (or reference) expressions we are
2915 dereferencing contain a side effect, these cannot be equal,
2916 but their addresses can be. */
2917 if ((flags & OEP_CONSTANT_ADDRESS_OF) == 0
2918 && (TREE_SIDE_EFFECTS (arg0)
2919 || TREE_SIDE_EFFECTS (arg1)))
2920 return 0;
2922 switch (TREE_CODE (arg0))
2924 case INDIRECT_REF:
2925 if (!(flags & OEP_ADDRESS_OF)
2926 && (TYPE_ALIGN (TREE_TYPE (arg0))
2927 != TYPE_ALIGN (TREE_TYPE (arg1))))
2928 return 0;
2929 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2930 return OP_SAME (0);
2932 case REALPART_EXPR:
2933 case IMAGPART_EXPR:
2934 return OP_SAME (0);
2936 case TARGET_MEM_REF:
2937 case MEM_REF:
2938 /* Require equal access sizes, and similar pointer types.
2939 We can have incomplete types for array references of
2940 variable-sized arrays from the Fortran frontend
2941 though. Also verify the types are compatible. */
2942 if (!((TYPE_SIZE (TREE_TYPE (arg0)) == TYPE_SIZE (TREE_TYPE (arg1))
2943 || (TYPE_SIZE (TREE_TYPE (arg0))
2944 && TYPE_SIZE (TREE_TYPE (arg1))
2945 && operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
2946 TYPE_SIZE (TREE_TYPE (arg1)), flags)))
2947 && types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1))
2948 && ((flags & OEP_ADDRESS_OF)
2949 || (alias_ptr_types_compatible_p
2950 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
2951 TREE_TYPE (TREE_OPERAND (arg1, 1)))
2952 && (MR_DEPENDENCE_CLIQUE (arg0)
2953 == MR_DEPENDENCE_CLIQUE (arg1))
2954 && (MR_DEPENDENCE_BASE (arg0)
2955 == MR_DEPENDENCE_BASE (arg1))
2956 && (TYPE_ALIGN (TREE_TYPE (arg0))
2957 == TYPE_ALIGN (TREE_TYPE (arg1)))))))
2958 return 0;
2959 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2960 return (OP_SAME (0) && OP_SAME (1)
2961 /* TARGET_MEM_REF require equal extra operands. */
2962 && (TREE_CODE (arg0) != TARGET_MEM_REF
2963 || (OP_SAME_WITH_NULL (2)
2964 && OP_SAME_WITH_NULL (3)
2965 && OP_SAME_WITH_NULL (4))));
2967 case ARRAY_REF:
2968 case ARRAY_RANGE_REF:
2969 /* Operands 2 and 3 may be null.
2970 Compare the array index by value if it is constant first as we
2971 may have different types but same value here. */
2972 if (!OP_SAME (0))
2973 return 0;
2974 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2975 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
2976 TREE_OPERAND (arg1, 1))
2977 || OP_SAME (1))
2978 && OP_SAME_WITH_NULL (2)
2979 && OP_SAME_WITH_NULL (3));
2981 case COMPONENT_REF:
2982 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
2983 may be NULL when we're called to compare MEM_EXPRs. */
2984 if (!OP_SAME_WITH_NULL (0)
2985 || !OP_SAME (1))
2986 return 0;
2987 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2988 return OP_SAME_WITH_NULL (2);
2990 case BIT_FIELD_REF:
2991 if (!OP_SAME (0))
2992 return 0;
2993 flags &= ~(OEP_CONSTANT_ADDRESS_OF|OEP_ADDRESS_OF);
2994 return OP_SAME (1) && OP_SAME (2);
2996 default:
2997 return 0;
3000 case tcc_expression:
3001 switch (TREE_CODE (arg0))
3003 case ADDR_EXPR:
3004 return operand_equal_p (TREE_OPERAND (arg0, 0),
3005 TREE_OPERAND (arg1, 0),
3006 flags | OEP_ADDRESS_OF);
3008 case TRUTH_NOT_EXPR:
3009 return OP_SAME (0);
3011 case TRUTH_ANDIF_EXPR:
3012 case TRUTH_ORIF_EXPR:
3013 return OP_SAME (0) && OP_SAME (1);
3015 case FMA_EXPR:
3016 case WIDEN_MULT_PLUS_EXPR:
3017 case WIDEN_MULT_MINUS_EXPR:
3018 if (!OP_SAME (2))
3019 return 0;
3020 /* The multiplcation operands are commutative. */
3021 /* FALLTHRU */
3023 case TRUTH_AND_EXPR:
3024 case TRUTH_OR_EXPR:
3025 case TRUTH_XOR_EXPR:
3026 if (OP_SAME (0) && OP_SAME (1))
3027 return 1;
3029 /* Otherwise take into account this is a commutative operation. */
3030 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3031 TREE_OPERAND (arg1, 1), flags)
3032 && operand_equal_p (TREE_OPERAND (arg0, 1),
3033 TREE_OPERAND (arg1, 0), flags));
3035 case COND_EXPR:
3036 case VEC_COND_EXPR:
3037 case DOT_PROD_EXPR:
3038 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3040 default:
3041 return 0;
3044 case tcc_vl_exp:
3045 switch (TREE_CODE (arg0))
3047 case CALL_EXPR:
3048 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3049 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3050 /* If not both CALL_EXPRs are either internal or normal function
3051 functions, then they are not equal. */
3052 return 0;
3053 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3055 /* If the CALL_EXPRs call different internal functions, then they
3056 are not equal. */
3057 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3058 return 0;
3060 else
3062 /* If the CALL_EXPRs call different functions, then they are not
3063 equal. */
3064 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3065 flags))
3066 return 0;
3070 unsigned int cef = call_expr_flags (arg0);
3071 if (flags & OEP_PURE_SAME)
3072 cef &= ECF_CONST | ECF_PURE;
3073 else
3074 cef &= ECF_CONST;
3075 if (!cef)
3076 return 0;
3079 /* Now see if all the arguments are the same. */
3081 const_call_expr_arg_iterator iter0, iter1;
3082 const_tree a0, a1;
3083 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3084 a1 = first_const_call_expr_arg (arg1, &iter1);
3085 a0 && a1;
3086 a0 = next_const_call_expr_arg (&iter0),
3087 a1 = next_const_call_expr_arg (&iter1))
3088 if (! operand_equal_p (a0, a1, flags))
3089 return 0;
3091 /* If we get here and both argument lists are exhausted
3092 then the CALL_EXPRs are equal. */
3093 return ! (a0 || a1);
3095 default:
3096 return 0;
3099 case tcc_declaration:
3100 /* Consider __builtin_sqrt equal to sqrt. */
3101 return (TREE_CODE (arg0) == FUNCTION_DECL
3102 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3103 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3104 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3106 default:
3107 return 0;
3110 #undef OP_SAME
3111 #undef OP_SAME_WITH_NULL
3114 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3115 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3117 When in doubt, return 0. */
3119 static int
3120 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3122 int unsignedp1, unsignedpo;
3123 tree primarg0, primarg1, primother;
3124 unsigned int correct_width;
3126 if (operand_equal_p (arg0, arg1, 0))
3127 return 1;
3129 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3130 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3131 return 0;
3133 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3134 and see if the inner values are the same. This removes any
3135 signedness comparison, which doesn't matter here. */
3136 primarg0 = arg0, primarg1 = arg1;
3137 STRIP_NOPS (primarg0);
3138 STRIP_NOPS (primarg1);
3139 if (operand_equal_p (primarg0, primarg1, 0))
3140 return 1;
3142 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3143 actual comparison operand, ARG0.
3145 First throw away any conversions to wider types
3146 already present in the operands. */
3148 primarg1 = get_narrower (arg1, &unsignedp1);
3149 primother = get_narrower (other, &unsignedpo);
3151 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3152 if (unsignedp1 == unsignedpo
3153 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3154 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3156 tree type = TREE_TYPE (arg0);
3158 /* Make sure shorter operand is extended the right way
3159 to match the longer operand. */
3160 primarg1 = fold_convert (signed_or_unsigned_type_for
3161 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3163 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3164 return 1;
3167 return 0;
3170 /* See if ARG is an expression that is either a comparison or is performing
3171 arithmetic on comparisons. The comparisons must only be comparing
3172 two different values, which will be stored in *CVAL1 and *CVAL2; if
3173 they are nonzero it means that some operands have already been found.
3174 No variables may be used anywhere else in the expression except in the
3175 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3176 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3178 If this is true, return 1. Otherwise, return zero. */
3180 static int
3181 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3183 enum tree_code code = TREE_CODE (arg);
3184 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3186 /* We can handle some of the tcc_expression cases here. */
3187 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3188 tclass = tcc_unary;
3189 else if (tclass == tcc_expression
3190 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3191 || code == COMPOUND_EXPR))
3192 tclass = tcc_binary;
3194 else if (tclass == tcc_expression && code == SAVE_EXPR
3195 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3197 /* If we've already found a CVAL1 or CVAL2, this expression is
3198 two complex to handle. */
3199 if (*cval1 || *cval2)
3200 return 0;
3202 tclass = tcc_unary;
3203 *save_p = 1;
3206 switch (tclass)
3208 case tcc_unary:
3209 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3211 case tcc_binary:
3212 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3213 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3214 cval1, cval2, save_p));
3216 case tcc_constant:
3217 return 1;
3219 case tcc_expression:
3220 if (code == COND_EXPR)
3221 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3222 cval1, cval2, save_p)
3223 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3224 cval1, cval2, save_p)
3225 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3226 cval1, cval2, save_p));
3227 return 0;
3229 case tcc_comparison:
3230 /* First see if we can handle the first operand, then the second. For
3231 the second operand, we know *CVAL1 can't be zero. It must be that
3232 one side of the comparison is each of the values; test for the
3233 case where this isn't true by failing if the two operands
3234 are the same. */
3236 if (operand_equal_p (TREE_OPERAND (arg, 0),
3237 TREE_OPERAND (arg, 1), 0))
3238 return 0;
3240 if (*cval1 == 0)
3241 *cval1 = TREE_OPERAND (arg, 0);
3242 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3244 else if (*cval2 == 0)
3245 *cval2 = TREE_OPERAND (arg, 0);
3246 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3248 else
3249 return 0;
3251 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3253 else if (*cval2 == 0)
3254 *cval2 = TREE_OPERAND (arg, 1);
3255 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3257 else
3258 return 0;
3260 return 1;
3262 default:
3263 return 0;
3267 /* ARG is a tree that is known to contain just arithmetic operations and
3268 comparisons. Evaluate the operations in the tree substituting NEW0 for
3269 any occurrence of OLD0 as an operand of a comparison and likewise for
3270 NEW1 and OLD1. */
3272 static tree
3273 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3274 tree old1, tree new1)
3276 tree type = TREE_TYPE (arg);
3277 enum tree_code code = TREE_CODE (arg);
3278 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3280 /* We can handle some of the tcc_expression cases here. */
3281 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3282 tclass = tcc_unary;
3283 else if (tclass == tcc_expression
3284 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3285 tclass = tcc_binary;
3287 switch (tclass)
3289 case tcc_unary:
3290 return fold_build1_loc (loc, code, type,
3291 eval_subst (loc, TREE_OPERAND (arg, 0),
3292 old0, new0, old1, new1));
3294 case tcc_binary:
3295 return fold_build2_loc (loc, code, type,
3296 eval_subst (loc, TREE_OPERAND (arg, 0),
3297 old0, new0, old1, new1),
3298 eval_subst (loc, TREE_OPERAND (arg, 1),
3299 old0, new0, old1, new1));
3301 case tcc_expression:
3302 switch (code)
3304 case SAVE_EXPR:
3305 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3306 old1, new1);
3308 case COMPOUND_EXPR:
3309 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3310 old1, new1);
3312 case COND_EXPR:
3313 return fold_build3_loc (loc, code, type,
3314 eval_subst (loc, TREE_OPERAND (arg, 0),
3315 old0, new0, old1, new1),
3316 eval_subst (loc, TREE_OPERAND (arg, 1),
3317 old0, new0, old1, new1),
3318 eval_subst (loc, TREE_OPERAND (arg, 2),
3319 old0, new0, old1, new1));
3320 default:
3321 break;
3323 /* Fall through - ??? */
3325 case tcc_comparison:
3327 tree arg0 = TREE_OPERAND (arg, 0);
3328 tree arg1 = TREE_OPERAND (arg, 1);
3330 /* We need to check both for exact equality and tree equality. The
3331 former will be true if the operand has a side-effect. In that
3332 case, we know the operand occurred exactly once. */
3334 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3335 arg0 = new0;
3336 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3337 arg0 = new1;
3339 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3340 arg1 = new0;
3341 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3342 arg1 = new1;
3344 return fold_build2_loc (loc, code, type, arg0, arg1);
3347 default:
3348 return arg;
3352 /* Return a tree for the case when the result of an expression is RESULT
3353 converted to TYPE and OMITTED was previously an operand of the expression
3354 but is now not needed (e.g., we folded OMITTED * 0).
3356 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3357 the conversion of RESULT to TYPE. */
3359 tree
3360 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3362 tree t = fold_convert_loc (loc, type, result);
3364 /* If the resulting operand is an empty statement, just return the omitted
3365 statement casted to void. */
3366 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3367 return build1_loc (loc, NOP_EXPR, void_type_node,
3368 fold_ignored_result (omitted));
3370 if (TREE_SIDE_EFFECTS (omitted))
3371 return build2_loc (loc, COMPOUND_EXPR, type,
3372 fold_ignored_result (omitted), t);
3374 return non_lvalue_loc (loc, t);
3377 /* Return a tree for the case when the result of an expression is RESULT
3378 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3379 of the expression but are now not needed.
3381 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3382 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3383 evaluated before OMITTED2. Otherwise, if neither has side effects,
3384 just do the conversion of RESULT to TYPE. */
3386 tree
3387 omit_two_operands_loc (location_t loc, tree type, tree result,
3388 tree omitted1, tree omitted2)
3390 tree t = fold_convert_loc (loc, type, result);
3392 if (TREE_SIDE_EFFECTS (omitted2))
3393 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3394 if (TREE_SIDE_EFFECTS (omitted1))
3395 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3397 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3401 /* Return a simplified tree node for the truth-negation of ARG. This
3402 never alters ARG itself. We assume that ARG is an operation that
3403 returns a truth value (0 or 1).
3405 FIXME: one would think we would fold the result, but it causes
3406 problems with the dominator optimizer. */
3408 static tree
3409 fold_truth_not_expr (location_t loc, tree arg)
3411 tree type = TREE_TYPE (arg);
3412 enum tree_code code = TREE_CODE (arg);
3413 location_t loc1, loc2;
3415 /* If this is a comparison, we can simply invert it, except for
3416 floating-point non-equality comparisons, in which case we just
3417 enclose a TRUTH_NOT_EXPR around what we have. */
3419 if (TREE_CODE_CLASS (code) == tcc_comparison)
3421 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3422 if (FLOAT_TYPE_P (op_type)
3423 && flag_trapping_math
3424 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3425 && code != NE_EXPR && code != EQ_EXPR)
3426 return NULL_TREE;
3428 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3429 if (code == ERROR_MARK)
3430 return NULL_TREE;
3432 return build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3433 TREE_OPERAND (arg, 1));
3436 switch (code)
3438 case INTEGER_CST:
3439 return constant_boolean_node (integer_zerop (arg), type);
3441 case TRUTH_AND_EXPR:
3442 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3443 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3444 return build2_loc (loc, TRUTH_OR_EXPR, type,
3445 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3446 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3448 case TRUTH_OR_EXPR:
3449 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3450 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3451 return build2_loc (loc, TRUTH_AND_EXPR, type,
3452 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3453 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3455 case TRUTH_XOR_EXPR:
3456 /* Here we can invert either operand. We invert the first operand
3457 unless the second operand is a TRUTH_NOT_EXPR in which case our
3458 result is the XOR of the first operand with the inside of the
3459 negation of the second operand. */
3461 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3462 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3463 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3464 else
3465 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3466 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3467 TREE_OPERAND (arg, 1));
3469 case TRUTH_ANDIF_EXPR:
3470 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3471 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3472 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3473 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3474 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3476 case TRUTH_ORIF_EXPR:
3477 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3478 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3479 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3480 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3481 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3483 case TRUTH_NOT_EXPR:
3484 return TREE_OPERAND (arg, 0);
3486 case COND_EXPR:
3488 tree arg1 = TREE_OPERAND (arg, 1);
3489 tree arg2 = TREE_OPERAND (arg, 2);
3491 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3492 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3494 /* A COND_EXPR may have a throw as one operand, which
3495 then has void type. Just leave void operands
3496 as they are. */
3497 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3498 VOID_TYPE_P (TREE_TYPE (arg1))
3499 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3500 VOID_TYPE_P (TREE_TYPE (arg2))
3501 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3504 case COMPOUND_EXPR:
3505 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3506 return build2_loc (loc, COMPOUND_EXPR, type,
3507 TREE_OPERAND (arg, 0),
3508 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3510 case NON_LVALUE_EXPR:
3511 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3512 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3514 CASE_CONVERT:
3515 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3516 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3518 /* ... fall through ... */
3520 case FLOAT_EXPR:
3521 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3522 return build1_loc (loc, TREE_CODE (arg), type,
3523 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3525 case BIT_AND_EXPR:
3526 if (!integer_onep (TREE_OPERAND (arg, 1)))
3527 return NULL_TREE;
3528 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3530 case SAVE_EXPR:
3531 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3533 case CLEANUP_POINT_EXPR:
3534 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3535 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3536 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3538 default:
3539 return NULL_TREE;
3543 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3544 assume that ARG is an operation that returns a truth value (0 or 1
3545 for scalars, 0 or -1 for vectors). Return the folded expression if
3546 folding is successful. Otherwise, return NULL_TREE. */
3548 static tree
3549 fold_invert_truthvalue (location_t loc, tree arg)
3551 tree type = TREE_TYPE (arg);
3552 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3553 ? BIT_NOT_EXPR
3554 : TRUTH_NOT_EXPR,
3555 type, arg);
3558 /* Return a simplified tree node for the truth-negation of ARG. This
3559 never alters ARG itself. We assume that ARG is an operation that
3560 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3562 tree
3563 invert_truthvalue_loc (location_t loc, tree arg)
3565 if (TREE_CODE (arg) == ERROR_MARK)
3566 return arg;
3568 tree type = TREE_TYPE (arg);
3569 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3570 ? BIT_NOT_EXPR
3571 : TRUTH_NOT_EXPR,
3572 type, arg);
3575 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3576 with code CODE. This optimization is unsafe. */
3577 static tree
3578 distribute_real_division (location_t loc, enum tree_code code, tree type,
3579 tree arg0, tree arg1)
3581 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3582 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3584 /* (A / C) +- (B / C) -> (A +- B) / C. */
3585 if (mul0 == mul1
3586 && operand_equal_p (TREE_OPERAND (arg0, 1),
3587 TREE_OPERAND (arg1, 1), 0))
3588 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
3589 fold_build2_loc (loc, code, type,
3590 TREE_OPERAND (arg0, 0),
3591 TREE_OPERAND (arg1, 0)),
3592 TREE_OPERAND (arg0, 1));
3594 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3595 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3596 TREE_OPERAND (arg1, 0), 0)
3597 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3598 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3600 REAL_VALUE_TYPE r0, r1;
3601 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3602 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3603 if (!mul0)
3604 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3605 if (!mul1)
3606 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3607 real_arithmetic (&r0, code, &r0, &r1);
3608 return fold_build2_loc (loc, MULT_EXPR, type,
3609 TREE_OPERAND (arg0, 0),
3610 build_real (type, r0));
3613 return NULL_TREE;
3616 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3617 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3619 static tree
3620 make_bit_field_ref (location_t loc, tree inner, tree type,
3621 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
3623 tree result, bftype;
3625 if (bitpos == 0)
3627 tree size = TYPE_SIZE (TREE_TYPE (inner));
3628 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3629 || POINTER_TYPE_P (TREE_TYPE (inner)))
3630 && tree_fits_shwi_p (size)
3631 && tree_to_shwi (size) == bitsize)
3632 return fold_convert_loc (loc, type, inner);
3635 bftype = type;
3636 if (TYPE_PRECISION (bftype) != bitsize
3637 || TYPE_UNSIGNED (bftype) == !unsignedp)
3638 bftype = build_nonstandard_integer_type (bitsize, 0);
3640 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
3641 size_int (bitsize), bitsize_int (bitpos));
3643 if (bftype != type)
3644 result = fold_convert_loc (loc, type, result);
3646 return result;
3649 /* Optimize a bit-field compare.
3651 There are two cases: First is a compare against a constant and the
3652 second is a comparison of two items where the fields are at the same
3653 bit position relative to the start of a chunk (byte, halfword, word)
3654 large enough to contain it. In these cases we can avoid the shift
3655 implicit in bitfield extractions.
3657 For constants, we emit a compare of the shifted constant with the
3658 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3659 compared. For two fields at the same position, we do the ANDs with the
3660 similar mask and compare the result of the ANDs.
3662 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3663 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3664 are the left and right operands of the comparison, respectively.
3666 If the optimization described above can be done, we return the resulting
3667 tree. Otherwise we return zero. */
3669 static tree
3670 optimize_bit_field_compare (location_t loc, enum tree_code code,
3671 tree compare_type, tree lhs, tree rhs)
3673 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3674 tree type = TREE_TYPE (lhs);
3675 tree unsigned_type;
3676 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3677 machine_mode lmode, rmode, nmode;
3678 int lunsignedp, runsignedp;
3679 int lvolatilep = 0, rvolatilep = 0;
3680 tree linner, rinner = NULL_TREE;
3681 tree mask;
3682 tree offset;
3684 /* Get all the information about the extractions being done. If the bit size
3685 if the same as the size of the underlying object, we aren't doing an
3686 extraction at all and so can do nothing. We also don't want to
3687 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3688 then will no longer be able to replace it. */
3689 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3690 &lunsignedp, &lvolatilep, false);
3691 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3692 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR || lvolatilep)
3693 return 0;
3695 if (!const_p)
3697 /* If this is not a constant, we can only do something if bit positions,
3698 sizes, and signedness are the same. */
3699 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3700 &runsignedp, &rvolatilep, false);
3702 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3703 || lunsignedp != runsignedp || offset != 0
3704 || TREE_CODE (rinner) == PLACEHOLDER_EXPR || rvolatilep)
3705 return 0;
3708 /* See if we can find a mode to refer to this field. We should be able to,
3709 but fail if we can't. */
3710 nmode = get_best_mode (lbitsize, lbitpos, 0, 0,
3711 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3712 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3713 TYPE_ALIGN (TREE_TYPE (rinner))),
3714 word_mode, false);
3715 if (nmode == VOIDmode)
3716 return 0;
3718 /* Set signed and unsigned types of the precision of this mode for the
3719 shifts below. */
3720 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3722 /* Compute the bit position and size for the new reference and our offset
3723 within it. If the new reference is the same size as the original, we
3724 won't optimize anything, so return zero. */
3725 nbitsize = GET_MODE_BITSIZE (nmode);
3726 nbitpos = lbitpos & ~ (nbitsize - 1);
3727 lbitpos -= nbitpos;
3728 if (nbitsize == lbitsize)
3729 return 0;
3731 if (BYTES_BIG_ENDIAN)
3732 lbitpos = nbitsize - lbitsize - lbitpos;
3734 /* Make the mask to be used against the extracted field. */
3735 mask = build_int_cst_type (unsigned_type, -1);
3736 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
3737 mask = const_binop (RSHIFT_EXPR, mask,
3738 size_int (nbitsize - lbitsize - lbitpos));
3740 if (! const_p)
3741 /* If not comparing with constant, just rework the comparison
3742 and return. */
3743 return fold_build2_loc (loc, code, compare_type,
3744 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3745 make_bit_field_ref (loc, linner,
3746 unsigned_type,
3747 nbitsize, nbitpos,
3749 mask),
3750 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3751 make_bit_field_ref (loc, rinner,
3752 unsigned_type,
3753 nbitsize, nbitpos,
3755 mask));
3757 /* Otherwise, we are handling the constant case. See if the constant is too
3758 big for the field. Warn and return a tree of for 0 (false) if so. We do
3759 this not only for its own sake, but to avoid having to test for this
3760 error case below. If we didn't, we might generate wrong code.
3762 For unsigned fields, the constant shifted right by the field length should
3763 be all zero. For signed fields, the high-order bits should agree with
3764 the sign bit. */
3766 if (lunsignedp)
3768 if (wi::lrshift (rhs, lbitsize) != 0)
3770 warning (0, "comparison is always %d due to width of bit-field",
3771 code == NE_EXPR);
3772 return constant_boolean_node (code == NE_EXPR, compare_type);
3775 else
3777 wide_int tem = wi::arshift (rhs, lbitsize - 1);
3778 if (tem != 0 && tem != -1)
3780 warning (0, "comparison is always %d due to width of bit-field",
3781 code == NE_EXPR);
3782 return constant_boolean_node (code == NE_EXPR, compare_type);
3786 /* Single-bit compares should always be against zero. */
3787 if (lbitsize == 1 && ! integer_zerop (rhs))
3789 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3790 rhs = build_int_cst (type, 0);
3793 /* Make a new bitfield reference, shift the constant over the
3794 appropriate number of bits and mask it with the computed mask
3795 (in case this was a signed field). If we changed it, make a new one. */
3796 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
3798 rhs = const_binop (BIT_AND_EXPR,
3799 const_binop (LSHIFT_EXPR,
3800 fold_convert_loc (loc, unsigned_type, rhs),
3801 size_int (lbitpos)),
3802 mask);
3804 lhs = build2_loc (loc, code, compare_type,
3805 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
3806 return lhs;
3809 /* Subroutine for fold_truth_andor_1: decode a field reference.
3811 If EXP is a comparison reference, we return the innermost reference.
3813 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3814 set to the starting bit number.
3816 If the innermost field can be completely contained in a mode-sized
3817 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3819 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3820 otherwise it is not changed.
3822 *PUNSIGNEDP is set to the signedness of the field.
3824 *PMASK is set to the mask used. This is either contained in a
3825 BIT_AND_EXPR or derived from the width of the field.
3827 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3829 Return 0 if this is not a component reference or is one that we can't
3830 do anything with. */
3832 static tree
3833 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
3834 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
3835 int *punsignedp, int *pvolatilep,
3836 tree *pmask, tree *pand_mask)
3838 tree outer_type = 0;
3839 tree and_mask = 0;
3840 tree mask, inner, offset;
3841 tree unsigned_type;
3842 unsigned int precision;
3844 /* All the optimizations using this function assume integer fields.
3845 There are problems with FP fields since the type_for_size call
3846 below can fail for, e.g., XFmode. */
3847 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3848 return 0;
3850 /* We are interested in the bare arrangement of bits, so strip everything
3851 that doesn't affect the machine mode. However, record the type of the
3852 outermost expression if it may matter below. */
3853 if (CONVERT_EXPR_P (exp)
3854 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3855 outer_type = TREE_TYPE (exp);
3856 STRIP_NOPS (exp);
3858 if (TREE_CODE (exp) == BIT_AND_EXPR)
3860 and_mask = TREE_OPERAND (exp, 1);
3861 exp = TREE_OPERAND (exp, 0);
3862 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3863 if (TREE_CODE (and_mask) != INTEGER_CST)
3864 return 0;
3867 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3868 punsignedp, pvolatilep, false);
3869 if ((inner == exp && and_mask == 0)
3870 || *pbitsize < 0 || offset != 0
3871 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3872 return 0;
3874 /* If the number of bits in the reference is the same as the bitsize of
3875 the outer type, then the outer type gives the signedness. Otherwise
3876 (in case of a small bitfield) the signedness is unchanged. */
3877 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3878 *punsignedp = TYPE_UNSIGNED (outer_type);
3880 /* Compute the mask to access the bitfield. */
3881 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3882 precision = TYPE_PRECISION (unsigned_type);
3884 mask = build_int_cst_type (unsigned_type, -1);
3886 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3887 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
3889 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3890 if (and_mask != 0)
3891 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
3892 fold_convert_loc (loc, unsigned_type, and_mask), mask);
3894 *pmask = mask;
3895 *pand_mask = and_mask;
3896 return inner;
3899 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3900 bit positions and MASK is SIGNED. */
3902 static int
3903 all_ones_mask_p (const_tree mask, unsigned int size)
3905 tree type = TREE_TYPE (mask);
3906 unsigned int precision = TYPE_PRECISION (type);
3908 /* If this function returns true when the type of the mask is
3909 UNSIGNED, then there will be errors. In particular see
3910 gcc.c-torture/execute/990326-1.c. There does not appear to be
3911 any documentation paper trail as to why this is so. But the pre
3912 wide-int worked with that restriction and it has been preserved
3913 here. */
3914 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
3915 return false;
3917 return wi::mask (size, false, precision) == mask;
3920 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3921 represents the sign bit of EXP's type. If EXP represents a sign
3922 or zero extension, also test VAL against the unextended type.
3923 The return value is the (sub)expression whose sign bit is VAL,
3924 or NULL_TREE otherwise. */
3926 tree
3927 sign_bit_p (tree exp, const_tree val)
3929 int width;
3930 tree t;
3932 /* Tree EXP must have an integral type. */
3933 t = TREE_TYPE (exp);
3934 if (! INTEGRAL_TYPE_P (t))
3935 return NULL_TREE;
3937 /* Tree VAL must be an integer constant. */
3938 if (TREE_CODE (val) != INTEGER_CST
3939 || TREE_OVERFLOW (val))
3940 return NULL_TREE;
3942 width = TYPE_PRECISION (t);
3943 if (wi::only_sign_bit_p (val, width))
3944 return exp;
3946 /* Handle extension from a narrower type. */
3947 if (TREE_CODE (exp) == NOP_EXPR
3948 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3949 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3951 return NULL_TREE;
3954 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
3955 to be evaluated unconditionally. */
3957 static int
3958 simple_operand_p (const_tree exp)
3960 /* Strip any conversions that don't change the machine mode. */
3961 STRIP_NOPS (exp);
3963 return (CONSTANT_CLASS_P (exp)
3964 || TREE_CODE (exp) == SSA_NAME
3965 || (DECL_P (exp)
3966 && ! TREE_ADDRESSABLE (exp)
3967 && ! TREE_THIS_VOLATILE (exp)
3968 && ! DECL_NONLOCAL (exp)
3969 /* Don't regard global variables as simple. They may be
3970 allocated in ways unknown to the compiler (shared memory,
3971 #pragma weak, etc). */
3972 && ! TREE_PUBLIC (exp)
3973 && ! DECL_EXTERNAL (exp)
3974 /* Weakrefs are not safe to be read, since they can be NULL.
3975 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
3976 have DECL_WEAK flag set. */
3977 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
3978 /* Loading a static variable is unduly expensive, but global
3979 registers aren't expensive. */
3980 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3983 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
3984 to be evaluated unconditionally.
3985 I addition to simple_operand_p, we assume that comparisons, conversions,
3986 and logic-not operations are simple, if their operands are simple, too. */
3988 static bool
3989 simple_operand_p_2 (tree exp)
3991 enum tree_code code;
3993 if (TREE_SIDE_EFFECTS (exp)
3994 || tree_could_trap_p (exp))
3995 return false;
3997 while (CONVERT_EXPR_P (exp))
3998 exp = TREE_OPERAND (exp, 0);
4000 code = TREE_CODE (exp);
4002 if (TREE_CODE_CLASS (code) == tcc_comparison)
4003 return (simple_operand_p (TREE_OPERAND (exp, 0))
4004 && simple_operand_p (TREE_OPERAND (exp, 1)));
4006 if (code == TRUTH_NOT_EXPR)
4007 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4009 return simple_operand_p (exp);
4013 /* The following functions are subroutines to fold_range_test and allow it to
4014 try to change a logical combination of comparisons into a range test.
4016 For example, both
4017 X == 2 || X == 3 || X == 4 || X == 5
4019 X >= 2 && X <= 5
4020 are converted to
4021 (unsigned) (X - 2) <= 3
4023 We describe each set of comparisons as being either inside or outside
4024 a range, using a variable named like IN_P, and then describe the
4025 range with a lower and upper bound. If one of the bounds is omitted,
4026 it represents either the highest or lowest value of the type.
4028 In the comments below, we represent a range by two numbers in brackets
4029 preceded by a "+" to designate being inside that range, or a "-" to
4030 designate being outside that range, so the condition can be inverted by
4031 flipping the prefix. An omitted bound is represented by a "-". For
4032 example, "- [-, 10]" means being outside the range starting at the lowest
4033 possible value and ending at 10, in other words, being greater than 10.
4034 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4035 always false.
4037 We set up things so that the missing bounds are handled in a consistent
4038 manner so neither a missing bound nor "true" and "false" need to be
4039 handled using a special case. */
4041 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4042 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4043 and UPPER1_P are nonzero if the respective argument is an upper bound
4044 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4045 must be specified for a comparison. ARG1 will be converted to ARG0's
4046 type if both are specified. */
4048 static tree
4049 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4050 tree arg1, int upper1_p)
4052 tree tem;
4053 int result;
4054 int sgn0, sgn1;
4056 /* If neither arg represents infinity, do the normal operation.
4057 Else, if not a comparison, return infinity. Else handle the special
4058 comparison rules. Note that most of the cases below won't occur, but
4059 are handled for consistency. */
4061 if (arg0 != 0 && arg1 != 0)
4063 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4064 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4065 STRIP_NOPS (tem);
4066 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4069 if (TREE_CODE_CLASS (code) != tcc_comparison)
4070 return 0;
4072 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4073 for neither. In real maths, we cannot assume open ended ranges are
4074 the same. But, this is computer arithmetic, where numbers are finite.
4075 We can therefore make the transformation of any unbounded range with
4076 the value Z, Z being greater than any representable number. This permits
4077 us to treat unbounded ranges as equal. */
4078 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4079 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4080 switch (code)
4082 case EQ_EXPR:
4083 result = sgn0 == sgn1;
4084 break;
4085 case NE_EXPR:
4086 result = sgn0 != sgn1;
4087 break;
4088 case LT_EXPR:
4089 result = sgn0 < sgn1;
4090 break;
4091 case LE_EXPR:
4092 result = sgn0 <= sgn1;
4093 break;
4094 case GT_EXPR:
4095 result = sgn0 > sgn1;
4096 break;
4097 case GE_EXPR:
4098 result = sgn0 >= sgn1;
4099 break;
4100 default:
4101 gcc_unreachable ();
4104 return constant_boolean_node (result, type);
4107 /* Helper routine for make_range. Perform one step for it, return
4108 new expression if the loop should continue or NULL_TREE if it should
4109 stop. */
4111 tree
4112 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4113 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4114 bool *strict_overflow_p)
4116 tree arg0_type = TREE_TYPE (arg0);
4117 tree n_low, n_high, low = *p_low, high = *p_high;
4118 int in_p = *p_in_p, n_in_p;
4120 switch (code)
4122 case TRUTH_NOT_EXPR:
4123 /* We can only do something if the range is testing for zero. */
4124 if (low == NULL_TREE || high == NULL_TREE
4125 || ! integer_zerop (low) || ! integer_zerop (high))
4126 return NULL_TREE;
4127 *p_in_p = ! in_p;
4128 return arg0;
4130 case EQ_EXPR: case NE_EXPR:
4131 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4132 /* We can only do something if the range is testing for zero
4133 and if the second operand is an integer constant. Note that
4134 saying something is "in" the range we make is done by
4135 complementing IN_P since it will set in the initial case of
4136 being not equal to zero; "out" is leaving it alone. */
4137 if (low == NULL_TREE || high == NULL_TREE
4138 || ! integer_zerop (low) || ! integer_zerop (high)
4139 || TREE_CODE (arg1) != INTEGER_CST)
4140 return NULL_TREE;
4142 switch (code)
4144 case NE_EXPR: /* - [c, c] */
4145 low = high = arg1;
4146 break;
4147 case EQ_EXPR: /* + [c, c] */
4148 in_p = ! in_p, low = high = arg1;
4149 break;
4150 case GT_EXPR: /* - [-, c] */
4151 low = 0, high = arg1;
4152 break;
4153 case GE_EXPR: /* + [c, -] */
4154 in_p = ! in_p, low = arg1, high = 0;
4155 break;
4156 case LT_EXPR: /* - [c, -] */
4157 low = arg1, high = 0;
4158 break;
4159 case LE_EXPR: /* + [-, c] */
4160 in_p = ! in_p, low = 0, high = arg1;
4161 break;
4162 default:
4163 gcc_unreachable ();
4166 /* If this is an unsigned comparison, we also know that EXP is
4167 greater than or equal to zero. We base the range tests we make
4168 on that fact, so we record it here so we can parse existing
4169 range tests. We test arg0_type since often the return type
4170 of, e.g. EQ_EXPR, is boolean. */
4171 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4173 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4174 in_p, low, high, 1,
4175 build_int_cst (arg0_type, 0),
4176 NULL_TREE))
4177 return NULL_TREE;
4179 in_p = n_in_p, low = n_low, high = n_high;
4181 /* If the high bound is missing, but we have a nonzero low
4182 bound, reverse the range so it goes from zero to the low bound
4183 minus 1. */
4184 if (high == 0 && low && ! integer_zerop (low))
4186 in_p = ! in_p;
4187 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4188 build_int_cst (TREE_TYPE (low), 1), 0);
4189 low = build_int_cst (arg0_type, 0);
4193 *p_low = low;
4194 *p_high = high;
4195 *p_in_p = in_p;
4196 return arg0;
4198 case NEGATE_EXPR:
4199 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4200 low and high are non-NULL, then normalize will DTRT. */
4201 if (!TYPE_UNSIGNED (arg0_type)
4202 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4204 if (low == NULL_TREE)
4205 low = TYPE_MIN_VALUE (arg0_type);
4206 if (high == NULL_TREE)
4207 high = TYPE_MAX_VALUE (arg0_type);
4210 /* (-x) IN [a,b] -> x in [-b, -a] */
4211 n_low = range_binop (MINUS_EXPR, exp_type,
4212 build_int_cst (exp_type, 0),
4213 0, high, 1);
4214 n_high = range_binop (MINUS_EXPR, exp_type,
4215 build_int_cst (exp_type, 0),
4216 0, low, 0);
4217 if (n_high != 0 && TREE_OVERFLOW (n_high))
4218 return NULL_TREE;
4219 goto normalize;
4221 case BIT_NOT_EXPR:
4222 /* ~ X -> -X - 1 */
4223 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4224 build_int_cst (exp_type, 1));
4226 case PLUS_EXPR:
4227 case MINUS_EXPR:
4228 if (TREE_CODE (arg1) != INTEGER_CST)
4229 return NULL_TREE;
4231 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4232 move a constant to the other side. */
4233 if (!TYPE_UNSIGNED (arg0_type)
4234 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4235 return NULL_TREE;
4237 /* If EXP is signed, any overflow in the computation is undefined,
4238 so we don't worry about it so long as our computations on
4239 the bounds don't overflow. For unsigned, overflow is defined
4240 and this is exactly the right thing. */
4241 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4242 arg0_type, low, 0, arg1, 0);
4243 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4244 arg0_type, high, 1, arg1, 0);
4245 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4246 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4247 return NULL_TREE;
4249 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4250 *strict_overflow_p = true;
4252 normalize:
4253 /* Check for an unsigned range which has wrapped around the maximum
4254 value thus making n_high < n_low, and normalize it. */
4255 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4257 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4258 build_int_cst (TREE_TYPE (n_high), 1), 0);
4259 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4260 build_int_cst (TREE_TYPE (n_low), 1), 0);
4262 /* If the range is of the form +/- [ x+1, x ], we won't
4263 be able to normalize it. But then, it represents the
4264 whole range or the empty set, so make it
4265 +/- [ -, - ]. */
4266 if (tree_int_cst_equal (n_low, low)
4267 && tree_int_cst_equal (n_high, high))
4268 low = high = 0;
4269 else
4270 in_p = ! in_p;
4272 else
4273 low = n_low, high = n_high;
4275 *p_low = low;
4276 *p_high = high;
4277 *p_in_p = in_p;
4278 return arg0;
4280 CASE_CONVERT:
4281 case NON_LVALUE_EXPR:
4282 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4283 return NULL_TREE;
4285 if (! INTEGRAL_TYPE_P (arg0_type)
4286 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4287 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4288 return NULL_TREE;
4290 n_low = low, n_high = high;
4292 if (n_low != 0)
4293 n_low = fold_convert_loc (loc, arg0_type, n_low);
4295 if (n_high != 0)
4296 n_high = fold_convert_loc (loc, arg0_type, n_high);
4298 /* If we're converting arg0 from an unsigned type, to exp,
4299 a signed type, we will be doing the comparison as unsigned.
4300 The tests above have already verified that LOW and HIGH
4301 are both positive.
4303 So we have to ensure that we will handle large unsigned
4304 values the same way that the current signed bounds treat
4305 negative values. */
4307 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4309 tree high_positive;
4310 tree equiv_type;
4311 /* For fixed-point modes, we need to pass the saturating flag
4312 as the 2nd parameter. */
4313 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4314 equiv_type
4315 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4316 TYPE_SATURATING (arg0_type));
4317 else
4318 equiv_type
4319 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4321 /* A range without an upper bound is, naturally, unbounded.
4322 Since convert would have cropped a very large value, use
4323 the max value for the destination type. */
4324 high_positive
4325 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4326 : TYPE_MAX_VALUE (arg0_type);
4328 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4329 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4330 fold_convert_loc (loc, arg0_type,
4331 high_positive),
4332 build_int_cst (arg0_type, 1));
4334 /* If the low bound is specified, "and" the range with the
4335 range for which the original unsigned value will be
4336 positive. */
4337 if (low != 0)
4339 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4340 1, fold_convert_loc (loc, arg0_type,
4341 integer_zero_node),
4342 high_positive))
4343 return NULL_TREE;
4345 in_p = (n_in_p == in_p);
4347 else
4349 /* Otherwise, "or" the range with the range of the input
4350 that will be interpreted as negative. */
4351 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4352 1, fold_convert_loc (loc, arg0_type,
4353 integer_zero_node),
4354 high_positive))
4355 return NULL_TREE;
4357 in_p = (in_p != n_in_p);
4361 *p_low = n_low;
4362 *p_high = n_high;
4363 *p_in_p = in_p;
4364 return arg0;
4366 default:
4367 return NULL_TREE;
4371 /* Given EXP, a logical expression, set the range it is testing into
4372 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4373 actually being tested. *PLOW and *PHIGH will be made of the same
4374 type as the returned expression. If EXP is not a comparison, we
4375 will most likely not be returning a useful value and range. Set
4376 *STRICT_OVERFLOW_P to true if the return value is only valid
4377 because signed overflow is undefined; otherwise, do not change
4378 *STRICT_OVERFLOW_P. */
4380 tree
4381 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4382 bool *strict_overflow_p)
4384 enum tree_code code;
4385 tree arg0, arg1 = NULL_TREE;
4386 tree exp_type, nexp;
4387 int in_p;
4388 tree low, high;
4389 location_t loc = EXPR_LOCATION (exp);
4391 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4392 and see if we can refine the range. Some of the cases below may not
4393 happen, but it doesn't seem worth worrying about this. We "continue"
4394 the outer loop when we've changed something; otherwise we "break"
4395 the switch, which will "break" the while. */
4397 in_p = 0;
4398 low = high = build_int_cst (TREE_TYPE (exp), 0);
4400 while (1)
4402 code = TREE_CODE (exp);
4403 exp_type = TREE_TYPE (exp);
4404 arg0 = NULL_TREE;
4406 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4408 if (TREE_OPERAND_LENGTH (exp) > 0)
4409 arg0 = TREE_OPERAND (exp, 0);
4410 if (TREE_CODE_CLASS (code) == tcc_binary
4411 || TREE_CODE_CLASS (code) == tcc_comparison
4412 || (TREE_CODE_CLASS (code) == tcc_expression
4413 && TREE_OPERAND_LENGTH (exp) > 1))
4414 arg1 = TREE_OPERAND (exp, 1);
4416 if (arg0 == NULL_TREE)
4417 break;
4419 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4420 &high, &in_p, strict_overflow_p);
4421 if (nexp == NULL_TREE)
4422 break;
4423 exp = nexp;
4426 /* If EXP is a constant, we can evaluate whether this is true or false. */
4427 if (TREE_CODE (exp) == INTEGER_CST)
4429 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4430 exp, 0, low, 0))
4431 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4432 exp, 1, high, 1)));
4433 low = high = 0;
4434 exp = 0;
4437 *pin_p = in_p, *plow = low, *phigh = high;
4438 return exp;
4441 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4442 type, TYPE, return an expression to test if EXP is in (or out of, depending
4443 on IN_P) the range. Return 0 if the test couldn't be created. */
4445 tree
4446 build_range_check (location_t loc, tree type, tree exp, int in_p,
4447 tree low, tree high)
4449 tree etype = TREE_TYPE (exp), value;
4451 /* Disable this optimization for function pointer expressions
4452 on targets that require function pointer canonicalization. */
4453 if (targetm.have_canonicalize_funcptr_for_compare ()
4454 && TREE_CODE (etype) == POINTER_TYPE
4455 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4456 return NULL_TREE;
4458 if (! in_p)
4460 value = build_range_check (loc, type, exp, 1, low, high);
4461 if (value != 0)
4462 return invert_truthvalue_loc (loc, value);
4464 return 0;
4467 if (low == 0 && high == 0)
4468 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4470 if (low == 0)
4471 return fold_build2_loc (loc, LE_EXPR, type, exp,
4472 fold_convert_loc (loc, etype, high));
4474 if (high == 0)
4475 return fold_build2_loc (loc, GE_EXPR, type, exp,
4476 fold_convert_loc (loc, etype, low));
4478 if (operand_equal_p (low, high, 0))
4479 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4480 fold_convert_loc (loc, etype, low));
4482 if (integer_zerop (low))
4484 if (! TYPE_UNSIGNED (etype))
4486 etype = unsigned_type_for (etype);
4487 high = fold_convert_loc (loc, etype, high);
4488 exp = fold_convert_loc (loc, etype, exp);
4490 return build_range_check (loc, type, exp, 1, 0, high);
4493 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4494 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4496 int prec = TYPE_PRECISION (etype);
4498 if (wi::mask (prec - 1, false, prec) == high)
4500 if (TYPE_UNSIGNED (etype))
4502 tree signed_etype = signed_type_for (etype);
4503 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4504 etype
4505 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4506 else
4507 etype = signed_etype;
4508 exp = fold_convert_loc (loc, etype, exp);
4510 return fold_build2_loc (loc, GT_EXPR, type, exp,
4511 build_int_cst (etype, 0));
4515 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4516 This requires wrap-around arithmetics for the type of the expression.
4517 First make sure that arithmetics in this type is valid, then make sure
4518 that it wraps around. */
4519 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4520 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4521 TYPE_UNSIGNED (etype));
4523 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4525 tree utype, minv, maxv;
4527 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4528 for the type in question, as we rely on this here. */
4529 utype = unsigned_type_for (etype);
4530 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
4531 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4532 build_int_cst (TREE_TYPE (maxv), 1), 1);
4533 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
4535 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4536 minv, 1, maxv, 1)))
4537 etype = utype;
4538 else
4539 return 0;
4542 high = fold_convert_loc (loc, etype, high);
4543 low = fold_convert_loc (loc, etype, low);
4544 exp = fold_convert_loc (loc, etype, exp);
4546 value = const_binop (MINUS_EXPR, high, low);
4549 if (POINTER_TYPE_P (etype))
4551 if (value != 0 && !TREE_OVERFLOW (value))
4553 low = fold_build1_loc (loc, NEGATE_EXPR, TREE_TYPE (low), low);
4554 return build_range_check (loc, type,
4555 fold_build_pointer_plus_loc (loc, exp, low),
4556 1, build_int_cst (etype, 0), value);
4558 return 0;
4561 if (value != 0 && !TREE_OVERFLOW (value))
4562 return build_range_check (loc, type,
4563 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
4564 1, build_int_cst (etype, 0), value);
4566 return 0;
4569 /* Return the predecessor of VAL in its type, handling the infinite case. */
4571 static tree
4572 range_predecessor (tree val)
4574 tree type = TREE_TYPE (val);
4576 if (INTEGRAL_TYPE_P (type)
4577 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4578 return 0;
4579 else
4580 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
4581 build_int_cst (TREE_TYPE (val), 1), 0);
4584 /* Return the successor of VAL in its type, handling the infinite case. */
4586 static tree
4587 range_successor (tree val)
4589 tree type = TREE_TYPE (val);
4591 if (INTEGRAL_TYPE_P (type)
4592 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4593 return 0;
4594 else
4595 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
4596 build_int_cst (TREE_TYPE (val), 1), 0);
4599 /* Given two ranges, see if we can merge them into one. Return 1 if we
4600 can, 0 if we can't. Set the output range into the specified parameters. */
4602 bool
4603 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4604 tree high0, int in1_p, tree low1, tree high1)
4606 int no_overlap;
4607 int subset;
4608 int temp;
4609 tree tem;
4610 int in_p;
4611 tree low, high;
4612 int lowequal = ((low0 == 0 && low1 == 0)
4613 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4614 low0, 0, low1, 0)));
4615 int highequal = ((high0 == 0 && high1 == 0)
4616 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4617 high0, 1, high1, 1)));
4619 /* Make range 0 be the range that starts first, or ends last if they
4620 start at the same value. Swap them if it isn't. */
4621 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4622 low0, 0, low1, 0))
4623 || (lowequal
4624 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4625 high1, 1, high0, 1))))
4627 temp = in0_p, in0_p = in1_p, in1_p = temp;
4628 tem = low0, low0 = low1, low1 = tem;
4629 tem = high0, high0 = high1, high1 = tem;
4632 /* Now flag two cases, whether the ranges are disjoint or whether the
4633 second range is totally subsumed in the first. Note that the tests
4634 below are simplified by the ones above. */
4635 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4636 high0, 1, low1, 0));
4637 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4638 high1, 1, high0, 1));
4640 /* We now have four cases, depending on whether we are including or
4641 excluding the two ranges. */
4642 if (in0_p && in1_p)
4644 /* If they don't overlap, the result is false. If the second range
4645 is a subset it is the result. Otherwise, the range is from the start
4646 of the second to the end of the first. */
4647 if (no_overlap)
4648 in_p = 0, low = high = 0;
4649 else if (subset)
4650 in_p = 1, low = low1, high = high1;
4651 else
4652 in_p = 1, low = low1, high = high0;
4655 else if (in0_p && ! in1_p)
4657 /* If they don't overlap, the result is the first range. If they are
4658 equal, the result is false. If the second range is a subset of the
4659 first, and the ranges begin at the same place, we go from just after
4660 the end of the second range to the end of the first. If the second
4661 range is not a subset of the first, or if it is a subset and both
4662 ranges end at the same place, the range starts at the start of the
4663 first range and ends just before the second range.
4664 Otherwise, we can't describe this as a single range. */
4665 if (no_overlap)
4666 in_p = 1, low = low0, high = high0;
4667 else if (lowequal && highequal)
4668 in_p = 0, low = high = 0;
4669 else if (subset && lowequal)
4671 low = range_successor (high1);
4672 high = high0;
4673 in_p = 1;
4674 if (low == 0)
4676 /* We are in the weird situation where high0 > high1 but
4677 high1 has no successor. Punt. */
4678 return 0;
4681 else if (! subset || highequal)
4683 low = low0;
4684 high = range_predecessor (low1);
4685 in_p = 1;
4686 if (high == 0)
4688 /* low0 < low1 but low1 has no predecessor. Punt. */
4689 return 0;
4692 else
4693 return 0;
4696 else if (! in0_p && in1_p)
4698 /* If they don't overlap, the result is the second range. If the second
4699 is a subset of the first, the result is false. Otherwise,
4700 the range starts just after the first range and ends at the
4701 end of the second. */
4702 if (no_overlap)
4703 in_p = 1, low = low1, high = high1;
4704 else if (subset || highequal)
4705 in_p = 0, low = high = 0;
4706 else
4708 low = range_successor (high0);
4709 high = high1;
4710 in_p = 1;
4711 if (low == 0)
4713 /* high1 > high0 but high0 has no successor. Punt. */
4714 return 0;
4719 else
4721 /* The case where we are excluding both ranges. Here the complex case
4722 is if they don't overlap. In that case, the only time we have a
4723 range is if they are adjacent. If the second is a subset of the
4724 first, the result is the first. Otherwise, the range to exclude
4725 starts at the beginning of the first range and ends at the end of the
4726 second. */
4727 if (no_overlap)
4729 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4730 range_successor (high0),
4731 1, low1, 0)))
4732 in_p = 0, low = low0, high = high1;
4733 else
4735 /* Canonicalize - [min, x] into - [-, x]. */
4736 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4737 switch (TREE_CODE (TREE_TYPE (low0)))
4739 case ENUMERAL_TYPE:
4740 if (TYPE_PRECISION (TREE_TYPE (low0))
4741 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4742 break;
4743 /* FALLTHROUGH */
4744 case INTEGER_TYPE:
4745 if (tree_int_cst_equal (low0,
4746 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4747 low0 = 0;
4748 break;
4749 case POINTER_TYPE:
4750 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4751 && integer_zerop (low0))
4752 low0 = 0;
4753 break;
4754 default:
4755 break;
4758 /* Canonicalize - [x, max] into - [x, -]. */
4759 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4760 switch (TREE_CODE (TREE_TYPE (high1)))
4762 case ENUMERAL_TYPE:
4763 if (TYPE_PRECISION (TREE_TYPE (high1))
4764 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4765 break;
4766 /* FALLTHROUGH */
4767 case INTEGER_TYPE:
4768 if (tree_int_cst_equal (high1,
4769 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4770 high1 = 0;
4771 break;
4772 case POINTER_TYPE:
4773 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4774 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4775 high1, 1,
4776 build_int_cst (TREE_TYPE (high1), 1),
4777 1)))
4778 high1 = 0;
4779 break;
4780 default:
4781 break;
4784 /* The ranges might be also adjacent between the maximum and
4785 minimum values of the given type. For
4786 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4787 return + [x + 1, y - 1]. */
4788 if (low0 == 0 && high1 == 0)
4790 low = range_successor (high0);
4791 high = range_predecessor (low1);
4792 if (low == 0 || high == 0)
4793 return 0;
4795 in_p = 1;
4797 else
4798 return 0;
4801 else if (subset)
4802 in_p = 0, low = low0, high = high0;
4803 else
4804 in_p = 0, low = low0, high = high1;
4807 *pin_p = in_p, *plow = low, *phigh = high;
4808 return 1;
4812 /* Subroutine of fold, looking inside expressions of the form
4813 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4814 of the COND_EXPR. This function is being used also to optimize
4815 A op B ? C : A, by reversing the comparison first.
4817 Return a folded expression whose code is not a COND_EXPR
4818 anymore, or NULL_TREE if no folding opportunity is found. */
4820 static tree
4821 fold_cond_expr_with_comparison (location_t loc, tree type,
4822 tree arg0, tree arg1, tree arg2)
4824 enum tree_code comp_code = TREE_CODE (arg0);
4825 tree arg00 = TREE_OPERAND (arg0, 0);
4826 tree arg01 = TREE_OPERAND (arg0, 1);
4827 tree arg1_type = TREE_TYPE (arg1);
4828 tree tem;
4830 STRIP_NOPS (arg1);
4831 STRIP_NOPS (arg2);
4833 /* If we have A op 0 ? A : -A, consider applying the following
4834 transformations:
4836 A == 0? A : -A same as -A
4837 A != 0? A : -A same as A
4838 A >= 0? A : -A same as abs (A)
4839 A > 0? A : -A same as abs (A)
4840 A <= 0? A : -A same as -abs (A)
4841 A < 0? A : -A same as -abs (A)
4843 None of these transformations work for modes with signed
4844 zeros. If A is +/-0, the first two transformations will
4845 change the sign of the result (from +0 to -0, or vice
4846 versa). The last four will fix the sign of the result,
4847 even though the original expressions could be positive or
4848 negative, depending on the sign of A.
4850 Note that all these transformations are correct if A is
4851 NaN, since the two alternatives (A and -A) are also NaNs. */
4852 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4853 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4854 ? real_zerop (arg01)
4855 : integer_zerop (arg01))
4856 && ((TREE_CODE (arg2) == NEGATE_EXPR
4857 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4858 /* In the case that A is of the form X-Y, '-A' (arg2) may
4859 have already been folded to Y-X, check for that. */
4860 || (TREE_CODE (arg1) == MINUS_EXPR
4861 && TREE_CODE (arg2) == MINUS_EXPR
4862 && operand_equal_p (TREE_OPERAND (arg1, 0),
4863 TREE_OPERAND (arg2, 1), 0)
4864 && operand_equal_p (TREE_OPERAND (arg1, 1),
4865 TREE_OPERAND (arg2, 0), 0))))
4866 switch (comp_code)
4868 case EQ_EXPR:
4869 case UNEQ_EXPR:
4870 tem = fold_convert_loc (loc, arg1_type, arg1);
4871 return pedantic_non_lvalue_loc (loc,
4872 fold_convert_loc (loc, type,
4873 negate_expr (tem)));
4874 case NE_EXPR:
4875 case LTGT_EXPR:
4876 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4877 case UNGE_EXPR:
4878 case UNGT_EXPR:
4879 if (flag_trapping_math)
4880 break;
4881 /* Fall through. */
4882 case GE_EXPR:
4883 case GT_EXPR:
4884 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4885 arg1 = fold_convert_loc (loc, signed_type_for
4886 (TREE_TYPE (arg1)), arg1);
4887 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4888 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
4889 case UNLE_EXPR:
4890 case UNLT_EXPR:
4891 if (flag_trapping_math)
4892 break;
4893 case LE_EXPR:
4894 case LT_EXPR:
4895 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4896 arg1 = fold_convert_loc (loc, signed_type_for
4897 (TREE_TYPE (arg1)), arg1);
4898 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
4899 return negate_expr (fold_convert_loc (loc, type, tem));
4900 default:
4901 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4902 break;
4905 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4906 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4907 both transformations are correct when A is NaN: A != 0
4908 is then true, and A == 0 is false. */
4910 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4911 && integer_zerop (arg01) && integer_zerop (arg2))
4913 if (comp_code == NE_EXPR)
4914 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4915 else if (comp_code == EQ_EXPR)
4916 return build_zero_cst (type);
4919 /* Try some transformations of A op B ? A : B.
4921 A == B? A : B same as B
4922 A != B? A : B same as A
4923 A >= B? A : B same as max (A, B)
4924 A > B? A : B same as max (B, A)
4925 A <= B? A : B same as min (A, B)
4926 A < B? A : B same as min (B, A)
4928 As above, these transformations don't work in the presence
4929 of signed zeros. For example, if A and B are zeros of
4930 opposite sign, the first two transformations will change
4931 the sign of the result. In the last four, the original
4932 expressions give different results for (A=+0, B=-0) and
4933 (A=-0, B=+0), but the transformed expressions do not.
4935 The first two transformations are correct if either A or B
4936 is a NaN. In the first transformation, the condition will
4937 be false, and B will indeed be chosen. In the case of the
4938 second transformation, the condition A != B will be true,
4939 and A will be chosen.
4941 The conversions to max() and min() are not correct if B is
4942 a number and A is not. The conditions in the original
4943 expressions will be false, so all four give B. The min()
4944 and max() versions would give a NaN instead. */
4945 if (!HONOR_SIGNED_ZEROS (element_mode (type))
4946 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4947 /* Avoid these transformations if the COND_EXPR may be used
4948 as an lvalue in the C++ front-end. PR c++/19199. */
4949 && (in_gimple_form
4950 || VECTOR_TYPE_P (type)
4951 || (! lang_GNU_CXX ()
4952 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4953 || ! maybe_lvalue_p (arg1)
4954 || ! maybe_lvalue_p (arg2)))
4956 tree comp_op0 = arg00;
4957 tree comp_op1 = arg01;
4958 tree comp_type = TREE_TYPE (comp_op0);
4960 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4961 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4963 comp_type = type;
4964 comp_op0 = arg1;
4965 comp_op1 = arg2;
4968 switch (comp_code)
4970 case EQ_EXPR:
4971 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
4972 case NE_EXPR:
4973 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
4974 case LE_EXPR:
4975 case LT_EXPR:
4976 case UNLE_EXPR:
4977 case UNLT_EXPR:
4978 /* In C++ a ?: expression can be an lvalue, so put the
4979 operand which will be used if they are equal first
4980 so that we can convert this back to the
4981 corresponding COND_EXPR. */
4982 if (!HONOR_NANS (arg1))
4984 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
4985 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
4986 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4987 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
4988 : fold_build2_loc (loc, MIN_EXPR, comp_type,
4989 comp_op1, comp_op0);
4990 return pedantic_non_lvalue_loc (loc,
4991 fold_convert_loc (loc, type, tem));
4993 break;
4994 case GE_EXPR:
4995 case GT_EXPR:
4996 case UNGE_EXPR:
4997 case UNGT_EXPR:
4998 if (!HONOR_NANS (arg1))
5000 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5001 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5002 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5003 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5004 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5005 comp_op1, comp_op0);
5006 return pedantic_non_lvalue_loc (loc,
5007 fold_convert_loc (loc, type, tem));
5009 break;
5010 case UNEQ_EXPR:
5011 if (!HONOR_NANS (arg1))
5012 return pedantic_non_lvalue_loc (loc,
5013 fold_convert_loc (loc, type, arg2));
5014 break;
5015 case LTGT_EXPR:
5016 if (!HONOR_NANS (arg1))
5017 return pedantic_non_lvalue_loc (loc,
5018 fold_convert_loc (loc, type, arg1));
5019 break;
5020 default:
5021 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5022 break;
5026 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5027 we might still be able to simplify this. For example,
5028 if C1 is one less or one more than C2, this might have started
5029 out as a MIN or MAX and been transformed by this function.
5030 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5032 if (INTEGRAL_TYPE_P (type)
5033 && TREE_CODE (arg01) == INTEGER_CST
5034 && TREE_CODE (arg2) == INTEGER_CST)
5035 switch (comp_code)
5037 case EQ_EXPR:
5038 if (TREE_CODE (arg1) == INTEGER_CST)
5039 break;
5040 /* We can replace A with C1 in this case. */
5041 arg1 = fold_convert_loc (loc, type, arg01);
5042 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5044 case LT_EXPR:
5045 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5046 MIN_EXPR, to preserve the signedness of the comparison. */
5047 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5048 OEP_ONLY_CONST)
5049 && operand_equal_p (arg01,
5050 const_binop (PLUS_EXPR, arg2,
5051 build_int_cst (type, 1)),
5052 OEP_ONLY_CONST))
5054 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5055 fold_convert_loc (loc, TREE_TYPE (arg00),
5056 arg2));
5057 return pedantic_non_lvalue_loc (loc,
5058 fold_convert_loc (loc, type, tem));
5060 break;
5062 case LE_EXPR:
5063 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5064 as above. */
5065 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5066 OEP_ONLY_CONST)
5067 && operand_equal_p (arg01,
5068 const_binop (MINUS_EXPR, arg2,
5069 build_int_cst (type, 1)),
5070 OEP_ONLY_CONST))
5072 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5073 fold_convert_loc (loc, TREE_TYPE (arg00),
5074 arg2));
5075 return pedantic_non_lvalue_loc (loc,
5076 fold_convert_loc (loc, type, tem));
5078 break;
5080 case GT_EXPR:
5081 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5082 MAX_EXPR, to preserve the signedness of the comparison. */
5083 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5084 OEP_ONLY_CONST)
5085 && operand_equal_p (arg01,
5086 const_binop (MINUS_EXPR, arg2,
5087 build_int_cst (type, 1)),
5088 OEP_ONLY_CONST))
5090 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5091 fold_convert_loc (loc, TREE_TYPE (arg00),
5092 arg2));
5093 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5095 break;
5097 case GE_EXPR:
5098 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5099 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5100 OEP_ONLY_CONST)
5101 && operand_equal_p (arg01,
5102 const_binop (PLUS_EXPR, arg2,
5103 build_int_cst (type, 1)),
5104 OEP_ONLY_CONST))
5106 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5107 fold_convert_loc (loc, TREE_TYPE (arg00),
5108 arg2));
5109 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5111 break;
5112 case NE_EXPR:
5113 break;
5114 default:
5115 gcc_unreachable ();
5118 return NULL_TREE;
5123 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5124 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5125 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5126 false) >= 2)
5127 #endif
5129 /* EXP is some logical combination of boolean tests. See if we can
5130 merge it into some range test. Return the new tree if so. */
5132 static tree
5133 fold_range_test (location_t loc, enum tree_code code, tree type,
5134 tree op0, tree op1)
5136 int or_op = (code == TRUTH_ORIF_EXPR
5137 || code == TRUTH_OR_EXPR);
5138 int in0_p, in1_p, in_p;
5139 tree low0, low1, low, high0, high1, high;
5140 bool strict_overflow_p = false;
5141 tree tem, lhs, rhs;
5142 const char * const warnmsg = G_("assuming signed overflow does not occur "
5143 "when simplifying range test");
5145 if (!INTEGRAL_TYPE_P (type))
5146 return 0;
5148 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5149 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5151 /* If this is an OR operation, invert both sides; we will invert
5152 again at the end. */
5153 if (or_op)
5154 in0_p = ! in0_p, in1_p = ! in1_p;
5156 /* If both expressions are the same, if we can merge the ranges, and we
5157 can build the range test, return it or it inverted. If one of the
5158 ranges is always true or always false, consider it to be the same
5159 expression as the other. */
5160 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5161 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5162 in1_p, low1, high1)
5163 && 0 != (tem = (build_range_check (loc, type,
5164 lhs != 0 ? lhs
5165 : rhs != 0 ? rhs : integer_zero_node,
5166 in_p, low, high))))
5168 if (strict_overflow_p)
5169 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5170 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5173 /* On machines where the branch cost is expensive, if this is a
5174 short-circuited branch and the underlying object on both sides
5175 is the same, make a non-short-circuit operation. */
5176 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5177 && lhs != 0 && rhs != 0
5178 && (code == TRUTH_ANDIF_EXPR
5179 || code == TRUTH_ORIF_EXPR)
5180 && operand_equal_p (lhs, rhs, 0))
5182 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5183 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5184 which cases we can't do this. */
5185 if (simple_operand_p (lhs))
5186 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5187 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5188 type, op0, op1);
5190 else if (!lang_hooks.decls.global_bindings_p ()
5191 && !CONTAINS_PLACEHOLDER_P (lhs))
5193 tree common = save_expr (lhs);
5195 if (0 != (lhs = build_range_check (loc, type, common,
5196 or_op ? ! in0_p : in0_p,
5197 low0, high0))
5198 && (0 != (rhs = build_range_check (loc, type, common,
5199 or_op ? ! in1_p : in1_p,
5200 low1, high1))))
5202 if (strict_overflow_p)
5203 fold_overflow_warning (warnmsg,
5204 WARN_STRICT_OVERFLOW_COMPARISON);
5205 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5206 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5207 type, lhs, rhs);
5212 return 0;
5215 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5216 bit value. Arrange things so the extra bits will be set to zero if and
5217 only if C is signed-extended to its full width. If MASK is nonzero,
5218 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5220 static tree
5221 unextend (tree c, int p, int unsignedp, tree mask)
5223 tree type = TREE_TYPE (c);
5224 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5225 tree temp;
5227 if (p == modesize || unsignedp)
5228 return c;
5230 /* We work by getting just the sign bit into the low-order bit, then
5231 into the high-order bit, then sign-extend. We then XOR that value
5232 with C. */
5233 temp = build_int_cst (TREE_TYPE (c), wi::extract_uhwi (c, p - 1, 1));
5235 /* We must use a signed type in order to get an arithmetic right shift.
5236 However, we must also avoid introducing accidental overflows, so that
5237 a subsequent call to integer_zerop will work. Hence we must
5238 do the type conversion here. At this point, the constant is either
5239 zero or one, and the conversion to a signed type can never overflow.
5240 We could get an overflow if this conversion is done anywhere else. */
5241 if (TYPE_UNSIGNED (type))
5242 temp = fold_convert (signed_type_for (type), temp);
5244 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5245 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5246 if (mask != 0)
5247 temp = const_binop (BIT_AND_EXPR, temp,
5248 fold_convert (TREE_TYPE (c), mask));
5249 /* If necessary, convert the type back to match the type of C. */
5250 if (TYPE_UNSIGNED (type))
5251 temp = fold_convert (type, temp);
5253 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5256 /* For an expression that has the form
5257 (A && B) || ~B
5259 (A || B) && ~B,
5260 we can drop one of the inner expressions and simplify to
5261 A || ~B
5263 A && ~B
5264 LOC is the location of the resulting expression. OP is the inner
5265 logical operation; the left-hand side in the examples above, while CMPOP
5266 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5267 removing a condition that guards another, as in
5268 (A != NULL && A->...) || A == NULL
5269 which we must not transform. If RHS_ONLY is true, only eliminate the
5270 right-most operand of the inner logical operation. */
5272 static tree
5273 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5274 bool rhs_only)
5276 tree type = TREE_TYPE (cmpop);
5277 enum tree_code code = TREE_CODE (cmpop);
5278 enum tree_code truthop_code = TREE_CODE (op);
5279 tree lhs = TREE_OPERAND (op, 0);
5280 tree rhs = TREE_OPERAND (op, 1);
5281 tree orig_lhs = lhs, orig_rhs = rhs;
5282 enum tree_code rhs_code = TREE_CODE (rhs);
5283 enum tree_code lhs_code = TREE_CODE (lhs);
5284 enum tree_code inv_code;
5286 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5287 return NULL_TREE;
5289 if (TREE_CODE_CLASS (code) != tcc_comparison)
5290 return NULL_TREE;
5292 if (rhs_code == truthop_code)
5294 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5295 if (newrhs != NULL_TREE)
5297 rhs = newrhs;
5298 rhs_code = TREE_CODE (rhs);
5301 if (lhs_code == truthop_code && !rhs_only)
5303 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5304 if (newlhs != NULL_TREE)
5306 lhs = newlhs;
5307 lhs_code = TREE_CODE (lhs);
5311 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5312 if (inv_code == rhs_code
5313 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5314 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5315 return lhs;
5316 if (!rhs_only && inv_code == lhs_code
5317 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5318 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5319 return rhs;
5320 if (rhs != orig_rhs || lhs != orig_lhs)
5321 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5322 lhs, rhs);
5323 return NULL_TREE;
5326 /* Find ways of folding logical expressions of LHS and RHS:
5327 Try to merge two comparisons to the same innermost item.
5328 Look for range tests like "ch >= '0' && ch <= '9'".
5329 Look for combinations of simple terms on machines with expensive branches
5330 and evaluate the RHS unconditionally.
5332 For example, if we have p->a == 2 && p->b == 4 and we can make an
5333 object large enough to span both A and B, we can do this with a comparison
5334 against the object ANDed with the a mask.
5336 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5337 operations to do this with one comparison.
5339 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5340 function and the one above.
5342 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5343 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5345 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5346 two operands.
5348 We return the simplified tree or 0 if no optimization is possible. */
5350 static tree
5351 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5352 tree lhs, tree rhs)
5354 /* If this is the "or" of two comparisons, we can do something if
5355 the comparisons are NE_EXPR. If this is the "and", we can do something
5356 if the comparisons are EQ_EXPR. I.e.,
5357 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5359 WANTED_CODE is this operation code. For single bit fields, we can
5360 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5361 comparison for one-bit fields. */
5363 enum tree_code wanted_code;
5364 enum tree_code lcode, rcode;
5365 tree ll_arg, lr_arg, rl_arg, rr_arg;
5366 tree ll_inner, lr_inner, rl_inner, rr_inner;
5367 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5368 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5369 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5370 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5371 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5372 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5373 machine_mode lnmode, rnmode;
5374 tree ll_mask, lr_mask, rl_mask, rr_mask;
5375 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5376 tree l_const, r_const;
5377 tree lntype, rntype, result;
5378 HOST_WIDE_INT first_bit, end_bit;
5379 int volatilep;
5381 /* Start by getting the comparison codes. Fail if anything is volatile.
5382 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5383 it were surrounded with a NE_EXPR. */
5385 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5386 return 0;
5388 lcode = TREE_CODE (lhs);
5389 rcode = TREE_CODE (rhs);
5391 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5393 lhs = build2 (NE_EXPR, truth_type, lhs,
5394 build_int_cst (TREE_TYPE (lhs), 0));
5395 lcode = NE_EXPR;
5398 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5400 rhs = build2 (NE_EXPR, truth_type, rhs,
5401 build_int_cst (TREE_TYPE (rhs), 0));
5402 rcode = NE_EXPR;
5405 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5406 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5407 return 0;
5409 ll_arg = TREE_OPERAND (lhs, 0);
5410 lr_arg = TREE_OPERAND (lhs, 1);
5411 rl_arg = TREE_OPERAND (rhs, 0);
5412 rr_arg = TREE_OPERAND (rhs, 1);
5414 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5415 if (simple_operand_p (ll_arg)
5416 && simple_operand_p (lr_arg))
5418 if (operand_equal_p (ll_arg, rl_arg, 0)
5419 && operand_equal_p (lr_arg, rr_arg, 0))
5421 result = combine_comparisons (loc, code, lcode, rcode,
5422 truth_type, ll_arg, lr_arg);
5423 if (result)
5424 return result;
5426 else if (operand_equal_p (ll_arg, rr_arg, 0)
5427 && operand_equal_p (lr_arg, rl_arg, 0))
5429 result = combine_comparisons (loc, code, lcode,
5430 swap_tree_comparison (rcode),
5431 truth_type, ll_arg, lr_arg);
5432 if (result)
5433 return result;
5437 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5438 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5440 /* If the RHS can be evaluated unconditionally and its operands are
5441 simple, it wins to evaluate the RHS unconditionally on machines
5442 with expensive branches. In this case, this isn't a comparison
5443 that can be merged. */
5445 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5446 false) >= 2
5447 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5448 && simple_operand_p (rl_arg)
5449 && simple_operand_p (rr_arg))
5451 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5452 if (code == TRUTH_OR_EXPR
5453 && lcode == NE_EXPR && integer_zerop (lr_arg)
5454 && rcode == NE_EXPR && integer_zerop (rr_arg)
5455 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5456 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5457 return build2_loc (loc, NE_EXPR, truth_type,
5458 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5459 ll_arg, rl_arg),
5460 build_int_cst (TREE_TYPE (ll_arg), 0));
5462 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5463 if (code == TRUTH_AND_EXPR
5464 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5465 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5466 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5467 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5468 return build2_loc (loc, EQ_EXPR, truth_type,
5469 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5470 ll_arg, rl_arg),
5471 build_int_cst (TREE_TYPE (ll_arg), 0));
5474 /* See if the comparisons can be merged. Then get all the parameters for
5475 each side. */
5477 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5478 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5479 return 0;
5481 volatilep = 0;
5482 ll_inner = decode_field_reference (loc, ll_arg,
5483 &ll_bitsize, &ll_bitpos, &ll_mode,
5484 &ll_unsignedp, &volatilep, &ll_mask,
5485 &ll_and_mask);
5486 lr_inner = decode_field_reference (loc, lr_arg,
5487 &lr_bitsize, &lr_bitpos, &lr_mode,
5488 &lr_unsignedp, &volatilep, &lr_mask,
5489 &lr_and_mask);
5490 rl_inner = decode_field_reference (loc, rl_arg,
5491 &rl_bitsize, &rl_bitpos, &rl_mode,
5492 &rl_unsignedp, &volatilep, &rl_mask,
5493 &rl_and_mask);
5494 rr_inner = decode_field_reference (loc, rr_arg,
5495 &rr_bitsize, &rr_bitpos, &rr_mode,
5496 &rr_unsignedp, &volatilep, &rr_mask,
5497 &rr_and_mask);
5499 /* It must be true that the inner operation on the lhs of each
5500 comparison must be the same if we are to be able to do anything.
5501 Then see if we have constants. If not, the same must be true for
5502 the rhs's. */
5503 if (volatilep || ll_inner == 0 || rl_inner == 0
5504 || ! operand_equal_p (ll_inner, rl_inner, 0))
5505 return 0;
5507 if (TREE_CODE (lr_arg) == INTEGER_CST
5508 && TREE_CODE (rr_arg) == INTEGER_CST)
5509 l_const = lr_arg, r_const = rr_arg;
5510 else if (lr_inner == 0 || rr_inner == 0
5511 || ! operand_equal_p (lr_inner, rr_inner, 0))
5512 return 0;
5513 else
5514 l_const = r_const = 0;
5516 /* If either comparison code is not correct for our logical operation,
5517 fail. However, we can convert a one-bit comparison against zero into
5518 the opposite comparison against that bit being set in the field. */
5520 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5521 if (lcode != wanted_code)
5523 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5525 /* Make the left operand unsigned, since we are only interested
5526 in the value of one bit. Otherwise we are doing the wrong
5527 thing below. */
5528 ll_unsignedp = 1;
5529 l_const = ll_mask;
5531 else
5532 return 0;
5535 /* This is analogous to the code for l_const above. */
5536 if (rcode != wanted_code)
5538 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5540 rl_unsignedp = 1;
5541 r_const = rl_mask;
5543 else
5544 return 0;
5547 /* See if we can find a mode that contains both fields being compared on
5548 the left. If we can't, fail. Otherwise, update all constants and masks
5549 to be relative to a field of that size. */
5550 first_bit = MIN (ll_bitpos, rl_bitpos);
5551 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5552 lnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5553 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5554 volatilep);
5555 if (lnmode == VOIDmode)
5556 return 0;
5558 lnbitsize = GET_MODE_BITSIZE (lnmode);
5559 lnbitpos = first_bit & ~ (lnbitsize - 1);
5560 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5561 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5563 if (BYTES_BIG_ENDIAN)
5565 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5566 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5569 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5570 size_int (xll_bitpos));
5571 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5572 size_int (xrl_bitpos));
5574 if (l_const)
5576 l_const = fold_convert_loc (loc, lntype, l_const);
5577 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5578 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5579 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5580 fold_build1_loc (loc, BIT_NOT_EXPR,
5581 lntype, ll_mask))))
5583 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5585 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5588 if (r_const)
5590 r_const = fold_convert_loc (loc, lntype, r_const);
5591 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5592 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5593 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5594 fold_build1_loc (loc, BIT_NOT_EXPR,
5595 lntype, rl_mask))))
5597 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5599 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5603 /* If the right sides are not constant, do the same for it. Also,
5604 disallow this optimization if a size or signedness mismatch occurs
5605 between the left and right sides. */
5606 if (l_const == 0)
5608 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5609 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5610 /* Make sure the two fields on the right
5611 correspond to the left without being swapped. */
5612 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5613 return 0;
5615 first_bit = MIN (lr_bitpos, rr_bitpos);
5616 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5617 rnmode = get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5618 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5619 volatilep);
5620 if (rnmode == VOIDmode)
5621 return 0;
5623 rnbitsize = GET_MODE_BITSIZE (rnmode);
5624 rnbitpos = first_bit & ~ (rnbitsize - 1);
5625 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5626 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5628 if (BYTES_BIG_ENDIAN)
5630 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5631 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5634 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5635 rntype, lr_mask),
5636 size_int (xlr_bitpos));
5637 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5638 rntype, rr_mask),
5639 size_int (xrr_bitpos));
5641 /* Make a mask that corresponds to both fields being compared.
5642 Do this for both items being compared. If the operands are the
5643 same size and the bits being compared are in the same position
5644 then we can do this by masking both and comparing the masked
5645 results. */
5646 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5647 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5648 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5650 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5651 ll_unsignedp || rl_unsignedp);
5652 if (! all_ones_mask_p (ll_mask, lnbitsize))
5653 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5655 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
5656 lr_unsignedp || rr_unsignedp);
5657 if (! all_ones_mask_p (lr_mask, rnbitsize))
5658 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5660 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5663 /* There is still another way we can do something: If both pairs of
5664 fields being compared are adjacent, we may be able to make a wider
5665 field containing them both.
5667 Note that we still must mask the lhs/rhs expressions. Furthermore,
5668 the mask must be shifted to account for the shift done by
5669 make_bit_field_ref. */
5670 if ((ll_bitsize + ll_bitpos == rl_bitpos
5671 && lr_bitsize + lr_bitpos == rr_bitpos)
5672 || (ll_bitpos == rl_bitpos + rl_bitsize
5673 && lr_bitpos == rr_bitpos + rr_bitsize))
5675 tree type;
5677 lhs = make_bit_field_ref (loc, ll_inner, lntype,
5678 ll_bitsize + rl_bitsize,
5679 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5680 rhs = make_bit_field_ref (loc, lr_inner, rntype,
5681 lr_bitsize + rr_bitsize,
5682 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5684 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5685 size_int (MIN (xll_bitpos, xrl_bitpos)));
5686 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5687 size_int (MIN (xlr_bitpos, xrr_bitpos)));
5689 /* Convert to the smaller type before masking out unwanted bits. */
5690 type = lntype;
5691 if (lntype != rntype)
5693 if (lnbitsize > rnbitsize)
5695 lhs = fold_convert_loc (loc, rntype, lhs);
5696 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
5697 type = rntype;
5699 else if (lnbitsize < rnbitsize)
5701 rhs = fold_convert_loc (loc, lntype, rhs);
5702 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
5703 type = lntype;
5707 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5708 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5710 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5711 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5713 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
5716 return 0;
5719 /* Handle the case of comparisons with constants. If there is something in
5720 common between the masks, those bits of the constants must be the same.
5721 If not, the condition is always false. Test for this to avoid generating
5722 incorrect code below. */
5723 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
5724 if (! integer_zerop (result)
5725 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
5726 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
5728 if (wanted_code == NE_EXPR)
5730 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5731 return constant_boolean_node (true, truth_type);
5733 else
5735 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5736 return constant_boolean_node (false, truth_type);
5740 /* Construct the expression we will return. First get the component
5741 reference we will make. Unless the mask is all ones the width of
5742 that field, perform the mask operation. Then compare with the
5743 merged constant. */
5744 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
5745 ll_unsignedp || rl_unsignedp);
5747 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5748 if (! all_ones_mask_p (ll_mask, lnbitsize))
5749 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
5751 return build2_loc (loc, wanted_code, truth_type, result,
5752 const_binop (BIT_IOR_EXPR, l_const, r_const));
5755 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5756 constant. */
5758 static tree
5759 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
5760 tree op0, tree op1)
5762 tree arg0 = op0;
5763 enum tree_code op_code;
5764 tree comp_const;
5765 tree minmax_const;
5766 int consts_equal, consts_lt;
5767 tree inner;
5769 STRIP_SIGN_NOPS (arg0);
5771 op_code = TREE_CODE (arg0);
5772 minmax_const = TREE_OPERAND (arg0, 1);
5773 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
5774 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5775 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5776 inner = TREE_OPERAND (arg0, 0);
5778 /* If something does not permit us to optimize, return the original tree. */
5779 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5780 || TREE_CODE (comp_const) != INTEGER_CST
5781 || TREE_OVERFLOW (comp_const)
5782 || TREE_CODE (minmax_const) != INTEGER_CST
5783 || TREE_OVERFLOW (minmax_const))
5784 return NULL_TREE;
5786 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5787 and GT_EXPR, doing the rest with recursive calls using logical
5788 simplifications. */
5789 switch (code)
5791 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5793 tree tem
5794 = optimize_minmax_comparison (loc,
5795 invert_tree_comparison (code, false),
5796 type, op0, op1);
5797 if (tem)
5798 return invert_truthvalue_loc (loc, tem);
5799 return NULL_TREE;
5802 case GE_EXPR:
5803 return
5804 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
5805 optimize_minmax_comparison
5806 (loc, EQ_EXPR, type, arg0, comp_const),
5807 optimize_minmax_comparison
5808 (loc, GT_EXPR, type, arg0, comp_const));
5810 case EQ_EXPR:
5811 if (op_code == MAX_EXPR && consts_equal)
5812 /* MAX (X, 0) == 0 -> X <= 0 */
5813 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
5815 else if (op_code == MAX_EXPR && consts_lt)
5816 /* MAX (X, 0) == 5 -> X == 5 */
5817 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5819 else if (op_code == MAX_EXPR)
5820 /* MAX (X, 0) == -1 -> false */
5821 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5823 else if (consts_equal)
5824 /* MIN (X, 0) == 0 -> X >= 0 */
5825 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
5827 else if (consts_lt)
5828 /* MIN (X, 0) == 5 -> false */
5829 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5831 else
5832 /* MIN (X, 0) == -1 -> X == -1 */
5833 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
5835 case GT_EXPR:
5836 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5837 /* MAX (X, 0) > 0 -> X > 0
5838 MAX (X, 0) > 5 -> X > 5 */
5839 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5841 else if (op_code == MAX_EXPR)
5842 /* MAX (X, 0) > -1 -> true */
5843 return omit_one_operand_loc (loc, type, integer_one_node, inner);
5845 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5846 /* MIN (X, 0) > 0 -> false
5847 MIN (X, 0) > 5 -> false */
5848 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
5850 else
5851 /* MIN (X, 0) > -1 -> X > -1 */
5852 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
5854 default:
5855 return NULL_TREE;
5859 /* T is an integer expression that is being multiplied, divided, or taken a
5860 modulus (CODE says which and what kind of divide or modulus) by a
5861 constant C. See if we can eliminate that operation by folding it with
5862 other operations already in T. WIDE_TYPE, if non-null, is a type that
5863 should be used for the computation if wider than our type.
5865 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5866 (X * 2) + (Y * 4). We must, however, be assured that either the original
5867 expression would not overflow or that overflow is undefined for the type
5868 in the language in question.
5870 If we return a non-null expression, it is an equivalent form of the
5871 original computation, but need not be in the original type.
5873 We set *STRICT_OVERFLOW_P to true if the return values depends on
5874 signed overflow being undefined. Otherwise we do not change
5875 *STRICT_OVERFLOW_P. */
5877 static tree
5878 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5879 bool *strict_overflow_p)
5881 /* To avoid exponential search depth, refuse to allow recursion past
5882 three levels. Beyond that (1) it's highly unlikely that we'll find
5883 something interesting and (2) we've probably processed it before
5884 when we built the inner expression. */
5886 static int depth;
5887 tree ret;
5889 if (depth > 3)
5890 return NULL;
5892 depth++;
5893 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5894 depth--;
5896 return ret;
5899 static tree
5900 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5901 bool *strict_overflow_p)
5903 tree type = TREE_TYPE (t);
5904 enum tree_code tcode = TREE_CODE (t);
5905 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5906 > GET_MODE_SIZE (TYPE_MODE (type)))
5907 ? wide_type : type);
5908 tree t1, t2;
5909 int same_p = tcode == code;
5910 tree op0 = NULL_TREE, op1 = NULL_TREE;
5911 bool sub_strict_overflow_p;
5913 /* Don't deal with constants of zero here; they confuse the code below. */
5914 if (integer_zerop (c))
5915 return NULL_TREE;
5917 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5918 op0 = TREE_OPERAND (t, 0);
5920 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5921 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5923 /* Note that we need not handle conditional operations here since fold
5924 already handles those cases. So just do arithmetic here. */
5925 switch (tcode)
5927 case INTEGER_CST:
5928 /* For a constant, we can always simplify if we are a multiply
5929 or (for divide and modulus) if it is a multiple of our constant. */
5930 if (code == MULT_EXPR
5931 || wi::multiple_of_p (t, c, TYPE_SIGN (type)))
5932 return const_binop (code, fold_convert (ctype, t),
5933 fold_convert (ctype, c));
5934 break;
5936 CASE_CONVERT: case NON_LVALUE_EXPR:
5937 /* If op0 is an expression ... */
5938 if ((COMPARISON_CLASS_P (op0)
5939 || UNARY_CLASS_P (op0)
5940 || BINARY_CLASS_P (op0)
5941 || VL_EXP_CLASS_P (op0)
5942 || EXPRESSION_CLASS_P (op0))
5943 /* ... and has wrapping overflow, and its type is smaller
5944 than ctype, then we cannot pass through as widening. */
5945 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
5946 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
5947 && (TYPE_PRECISION (ctype)
5948 > TYPE_PRECISION (TREE_TYPE (op0))))
5949 /* ... or this is a truncation (t is narrower than op0),
5950 then we cannot pass through this narrowing. */
5951 || (TYPE_PRECISION (type)
5952 < TYPE_PRECISION (TREE_TYPE (op0)))
5953 /* ... or signedness changes for division or modulus,
5954 then we cannot pass through this conversion. */
5955 || (code != MULT_EXPR
5956 && (TYPE_UNSIGNED (ctype)
5957 != TYPE_UNSIGNED (TREE_TYPE (op0))))
5958 /* ... or has undefined overflow while the converted to
5959 type has not, we cannot do the operation in the inner type
5960 as that would introduce undefined overflow. */
5961 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
5962 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
5963 && !TYPE_OVERFLOW_UNDEFINED (type))))
5964 break;
5966 /* Pass the constant down and see if we can make a simplification. If
5967 we can, replace this expression with the inner simplification for
5968 possible later conversion to our or some other type. */
5969 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5970 && TREE_CODE (t2) == INTEGER_CST
5971 && !TREE_OVERFLOW (t2)
5972 && (0 != (t1 = extract_muldiv (op0, t2, code,
5973 code == MULT_EXPR
5974 ? ctype : NULL_TREE,
5975 strict_overflow_p))))
5976 return t1;
5977 break;
5979 case ABS_EXPR:
5980 /* If widening the type changes it from signed to unsigned, then we
5981 must avoid building ABS_EXPR itself as unsigned. */
5982 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5984 tree cstype = (*signed_type_for) (ctype);
5985 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5986 != 0)
5988 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5989 return fold_convert (ctype, t1);
5991 break;
5993 /* If the constant is negative, we cannot simplify this. */
5994 if (tree_int_cst_sgn (c) == -1)
5995 break;
5996 /* FALLTHROUGH */
5997 case NEGATE_EXPR:
5998 /* For division and modulus, type can't be unsigned, as e.g.
5999 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6000 For signed types, even with wrapping overflow, this is fine. */
6001 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6002 break;
6003 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6004 != 0)
6005 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6006 break;
6008 case MIN_EXPR: case MAX_EXPR:
6009 /* If widening the type changes the signedness, then we can't perform
6010 this optimization as that changes the result. */
6011 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6012 break;
6014 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6015 sub_strict_overflow_p = false;
6016 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6017 &sub_strict_overflow_p)) != 0
6018 && (t2 = extract_muldiv (op1, c, code, wide_type,
6019 &sub_strict_overflow_p)) != 0)
6021 if (tree_int_cst_sgn (c) < 0)
6022 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6023 if (sub_strict_overflow_p)
6024 *strict_overflow_p = true;
6025 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6026 fold_convert (ctype, t2));
6028 break;
6030 case LSHIFT_EXPR: case RSHIFT_EXPR:
6031 /* If the second operand is constant, this is a multiplication
6032 or floor division, by a power of two, so we can treat it that
6033 way unless the multiplier or divisor overflows. Signed
6034 left-shift overflow is implementation-defined rather than
6035 undefined in C90, so do not convert signed left shift into
6036 multiplication. */
6037 if (TREE_CODE (op1) == INTEGER_CST
6038 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6039 /* const_binop may not detect overflow correctly,
6040 so check for it explicitly here. */
6041 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
6042 && 0 != (t1 = fold_convert (ctype,
6043 const_binop (LSHIFT_EXPR,
6044 size_one_node,
6045 op1)))
6046 && !TREE_OVERFLOW (t1))
6047 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6048 ? MULT_EXPR : FLOOR_DIV_EXPR,
6049 ctype,
6050 fold_convert (ctype, op0),
6051 t1),
6052 c, code, wide_type, strict_overflow_p);
6053 break;
6055 case PLUS_EXPR: case MINUS_EXPR:
6056 /* See if we can eliminate the operation on both sides. If we can, we
6057 can return a new PLUS or MINUS. If we can't, the only remaining
6058 cases where we can do anything are if the second operand is a
6059 constant. */
6060 sub_strict_overflow_p = false;
6061 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6062 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6063 if (t1 != 0 && t2 != 0
6064 && (code == MULT_EXPR
6065 /* If not multiplication, we can only do this if both operands
6066 are divisible by c. */
6067 || (multiple_of_p (ctype, op0, c)
6068 && multiple_of_p (ctype, op1, c))))
6070 if (sub_strict_overflow_p)
6071 *strict_overflow_p = true;
6072 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6073 fold_convert (ctype, t2));
6076 /* If this was a subtraction, negate OP1 and set it to be an addition.
6077 This simplifies the logic below. */
6078 if (tcode == MINUS_EXPR)
6080 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6081 /* If OP1 was not easily negatable, the constant may be OP0. */
6082 if (TREE_CODE (op0) == INTEGER_CST)
6084 std::swap (op0, op1);
6085 std::swap (t1, t2);
6089 if (TREE_CODE (op1) != INTEGER_CST)
6090 break;
6092 /* If either OP1 or C are negative, this optimization is not safe for
6093 some of the division and remainder types while for others we need
6094 to change the code. */
6095 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6097 if (code == CEIL_DIV_EXPR)
6098 code = FLOOR_DIV_EXPR;
6099 else if (code == FLOOR_DIV_EXPR)
6100 code = CEIL_DIV_EXPR;
6101 else if (code != MULT_EXPR
6102 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6103 break;
6106 /* If it's a multiply or a division/modulus operation of a multiple
6107 of our constant, do the operation and verify it doesn't overflow. */
6108 if (code == MULT_EXPR
6109 || wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6111 op1 = const_binop (code, fold_convert (ctype, op1),
6112 fold_convert (ctype, c));
6113 /* We allow the constant to overflow with wrapping semantics. */
6114 if (op1 == 0
6115 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6116 break;
6118 else
6119 break;
6121 /* If we have an unsigned type, we cannot widen the operation since it
6122 will change the result if the original computation overflowed. */
6123 if (TYPE_UNSIGNED (ctype) && ctype != type)
6124 break;
6126 /* If we were able to eliminate our operation from the first side,
6127 apply our operation to the second side and reform the PLUS. */
6128 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6129 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6131 /* The last case is if we are a multiply. In that case, we can
6132 apply the distributive law to commute the multiply and addition
6133 if the multiplication of the constants doesn't overflow
6134 and overflow is defined. With undefined overflow
6135 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6136 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6137 return fold_build2 (tcode, ctype,
6138 fold_build2 (code, ctype,
6139 fold_convert (ctype, op0),
6140 fold_convert (ctype, c)),
6141 op1);
6143 break;
6145 case MULT_EXPR:
6146 /* We have a special case here if we are doing something like
6147 (C * 8) % 4 since we know that's zero. */
6148 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6149 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6150 /* If the multiplication can overflow we cannot optimize this. */
6151 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6152 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6153 && wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6155 *strict_overflow_p = true;
6156 return omit_one_operand (type, integer_zero_node, op0);
6159 /* ... fall through ... */
6161 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6162 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6163 /* If we can extract our operation from the LHS, do so and return a
6164 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6165 do something only if the second operand is a constant. */
6166 if (same_p
6167 && (t1 = extract_muldiv (op0, c, code, wide_type,
6168 strict_overflow_p)) != 0)
6169 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6170 fold_convert (ctype, op1));
6171 else if (tcode == MULT_EXPR && code == MULT_EXPR
6172 && (t1 = extract_muldiv (op1, c, code, wide_type,
6173 strict_overflow_p)) != 0)
6174 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6175 fold_convert (ctype, t1));
6176 else if (TREE_CODE (op1) != INTEGER_CST)
6177 return 0;
6179 /* If these are the same operation types, we can associate them
6180 assuming no overflow. */
6181 if (tcode == code)
6183 bool overflow_p = false;
6184 bool overflow_mul_p;
6185 signop sign = TYPE_SIGN (ctype);
6186 wide_int mul = wi::mul (op1, c, sign, &overflow_mul_p);
6187 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6188 if (overflow_mul_p
6189 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6190 overflow_p = true;
6191 if (!overflow_p)
6193 mul = wide_int::from (mul, TYPE_PRECISION (ctype),
6194 TYPE_SIGN (TREE_TYPE (op1)));
6195 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6196 wide_int_to_tree (ctype, mul));
6200 /* If these operations "cancel" each other, we have the main
6201 optimizations of this pass, which occur when either constant is a
6202 multiple of the other, in which case we replace this with either an
6203 operation or CODE or TCODE.
6205 If we have an unsigned type, we cannot do this since it will change
6206 the result if the original computation overflowed. */
6207 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6208 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6209 || (tcode == MULT_EXPR
6210 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6211 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6212 && code != MULT_EXPR)))
6214 if (wi::multiple_of_p (op1, c, TYPE_SIGN (type)))
6216 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6217 *strict_overflow_p = true;
6218 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6219 fold_convert (ctype,
6220 const_binop (TRUNC_DIV_EXPR,
6221 op1, c)));
6223 else if (wi::multiple_of_p (c, op1, TYPE_SIGN (type)))
6225 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6226 *strict_overflow_p = true;
6227 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6228 fold_convert (ctype,
6229 const_binop (TRUNC_DIV_EXPR,
6230 c, op1)));
6233 break;
6235 default:
6236 break;
6239 return 0;
6242 /* Return a node which has the indicated constant VALUE (either 0 or
6243 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6244 and is of the indicated TYPE. */
6246 tree
6247 constant_boolean_node (bool value, tree type)
6249 if (type == integer_type_node)
6250 return value ? integer_one_node : integer_zero_node;
6251 else if (type == boolean_type_node)
6252 return value ? boolean_true_node : boolean_false_node;
6253 else if (TREE_CODE (type) == VECTOR_TYPE)
6254 return build_vector_from_val (type,
6255 build_int_cst (TREE_TYPE (type),
6256 value ? -1 : 0));
6257 else
6258 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6262 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6263 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6264 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6265 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6266 COND is the first argument to CODE; otherwise (as in the example
6267 given here), it is the second argument. TYPE is the type of the
6268 original expression. Return NULL_TREE if no simplification is
6269 possible. */
6271 static tree
6272 fold_binary_op_with_conditional_arg (location_t loc,
6273 enum tree_code code,
6274 tree type, tree op0, tree op1,
6275 tree cond, tree arg, int cond_first_p)
6277 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6278 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6279 tree test, true_value, false_value;
6280 tree lhs = NULL_TREE;
6281 tree rhs = NULL_TREE;
6282 enum tree_code cond_code = COND_EXPR;
6284 if (TREE_CODE (cond) == COND_EXPR
6285 || TREE_CODE (cond) == VEC_COND_EXPR)
6287 test = TREE_OPERAND (cond, 0);
6288 true_value = TREE_OPERAND (cond, 1);
6289 false_value = TREE_OPERAND (cond, 2);
6290 /* If this operand throws an expression, then it does not make
6291 sense to try to perform a logical or arithmetic operation
6292 involving it. */
6293 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6294 lhs = true_value;
6295 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6296 rhs = false_value;
6298 else
6300 tree testtype = TREE_TYPE (cond);
6301 test = cond;
6302 true_value = constant_boolean_node (true, testtype);
6303 false_value = constant_boolean_node (false, testtype);
6306 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6307 cond_code = VEC_COND_EXPR;
6309 /* This transformation is only worthwhile if we don't have to wrap ARG
6310 in a SAVE_EXPR and the operation can be simplified without recursing
6311 on at least one of the branches once its pushed inside the COND_EXPR. */
6312 if (!TREE_CONSTANT (arg)
6313 && (TREE_SIDE_EFFECTS (arg)
6314 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6315 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6316 return NULL_TREE;
6318 arg = fold_convert_loc (loc, arg_type, arg);
6319 if (lhs == 0)
6321 true_value = fold_convert_loc (loc, cond_type, true_value);
6322 if (cond_first_p)
6323 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6324 else
6325 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6327 if (rhs == 0)
6329 false_value = fold_convert_loc (loc, cond_type, false_value);
6330 if (cond_first_p)
6331 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6332 else
6333 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6336 /* Check that we have simplified at least one of the branches. */
6337 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6338 return NULL_TREE;
6340 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6344 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6346 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6347 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6348 ADDEND is the same as X.
6350 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6351 and finite. The problematic cases are when X is zero, and its mode
6352 has signed zeros. In the case of rounding towards -infinity,
6353 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6354 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6356 bool
6357 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6359 if (!real_zerop (addend))
6360 return false;
6362 /* Don't allow the fold with -fsignaling-nans. */
6363 if (HONOR_SNANS (element_mode (type)))
6364 return false;
6366 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6367 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6368 return true;
6370 /* In a vector or complex, we would need to check the sign of all zeros. */
6371 if (TREE_CODE (addend) != REAL_CST)
6372 return false;
6374 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6375 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6376 negate = !negate;
6378 /* The mode has signed zeros, and we have to honor their sign.
6379 In this situation, there is only one case we can return true for.
6380 X - 0 is the same as X unless rounding towards -infinity is
6381 supported. */
6382 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6385 /* Subroutine of fold() that optimizes comparisons of a division by
6386 a nonzero integer constant against an integer constant, i.e.
6387 X/C1 op C2.
6389 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6390 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6391 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6393 The function returns the constant folded tree if a simplification
6394 can be made, and NULL_TREE otherwise. */
6396 static tree
6397 fold_div_compare (location_t loc,
6398 enum tree_code code, tree type, tree arg0, tree arg1)
6400 tree prod, tmp, hi, lo;
6401 tree arg00 = TREE_OPERAND (arg0, 0);
6402 tree arg01 = TREE_OPERAND (arg0, 1);
6403 signop sign = TYPE_SIGN (TREE_TYPE (arg0));
6404 bool neg_overflow = false;
6405 bool overflow;
6407 /* We have to do this the hard way to detect unsigned overflow.
6408 prod = int_const_binop (MULT_EXPR, arg01, arg1); */
6409 wide_int val = wi::mul (arg01, arg1, sign, &overflow);
6410 prod = force_fit_type (TREE_TYPE (arg00), val, -1, overflow);
6411 neg_overflow = false;
6413 if (sign == UNSIGNED)
6415 tmp = int_const_binop (MINUS_EXPR, arg01,
6416 build_int_cst (TREE_TYPE (arg01), 1));
6417 lo = prod;
6419 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6420 val = wi::add (prod, tmp, sign, &overflow);
6421 hi = force_fit_type (TREE_TYPE (arg00), val,
6422 -1, overflow | TREE_OVERFLOW (prod));
6424 else if (tree_int_cst_sgn (arg01) >= 0)
6426 tmp = int_const_binop (MINUS_EXPR, arg01,
6427 build_int_cst (TREE_TYPE (arg01), 1));
6428 switch (tree_int_cst_sgn (arg1))
6430 case -1:
6431 neg_overflow = true;
6432 lo = int_const_binop (MINUS_EXPR, prod, tmp);
6433 hi = prod;
6434 break;
6436 case 0:
6437 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6438 hi = tmp;
6439 break;
6441 case 1:
6442 hi = int_const_binop (PLUS_EXPR, prod, tmp);
6443 lo = prod;
6444 break;
6446 default:
6447 gcc_unreachable ();
6450 else
6452 /* A negative divisor reverses the relational operators. */
6453 code = swap_tree_comparison (code);
6455 tmp = int_const_binop (PLUS_EXPR, arg01,
6456 build_int_cst (TREE_TYPE (arg01), 1));
6457 switch (tree_int_cst_sgn (arg1))
6459 case -1:
6460 hi = int_const_binop (MINUS_EXPR, prod, tmp);
6461 lo = prod;
6462 break;
6464 case 0:
6465 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6466 lo = tmp;
6467 break;
6469 case 1:
6470 neg_overflow = true;
6471 lo = int_const_binop (PLUS_EXPR, prod, tmp);
6472 hi = prod;
6473 break;
6475 default:
6476 gcc_unreachable ();
6480 switch (code)
6482 case EQ_EXPR:
6483 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6484 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
6485 if (TREE_OVERFLOW (hi))
6486 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6487 if (TREE_OVERFLOW (lo))
6488 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6489 return build_range_check (loc, type, arg00, 1, lo, hi);
6491 case NE_EXPR:
6492 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6493 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
6494 if (TREE_OVERFLOW (hi))
6495 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6496 if (TREE_OVERFLOW (lo))
6497 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6498 return build_range_check (loc, type, arg00, 0, lo, hi);
6500 case LT_EXPR:
6501 if (TREE_OVERFLOW (lo))
6503 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6504 return omit_one_operand_loc (loc, type, tmp, arg00);
6506 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
6508 case LE_EXPR:
6509 if (TREE_OVERFLOW (hi))
6511 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6512 return omit_one_operand_loc (loc, type, tmp, arg00);
6514 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
6516 case GT_EXPR:
6517 if (TREE_OVERFLOW (hi))
6519 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6520 return omit_one_operand_loc (loc, type, tmp, arg00);
6522 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
6524 case GE_EXPR:
6525 if (TREE_OVERFLOW (lo))
6527 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6528 return omit_one_operand_loc (loc, type, tmp, arg00);
6530 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
6532 default:
6533 break;
6536 return NULL_TREE;
6540 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6541 equality/inequality test, then return a simplified form of the test
6542 using a sign testing. Otherwise return NULL. TYPE is the desired
6543 result type. */
6545 static tree
6546 fold_single_bit_test_into_sign_test (location_t loc,
6547 enum tree_code code, tree arg0, tree arg1,
6548 tree result_type)
6550 /* If this is testing a single bit, we can optimize the test. */
6551 if ((code == NE_EXPR || code == EQ_EXPR)
6552 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6553 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6555 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6556 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6557 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6559 if (arg00 != NULL_TREE
6560 /* This is only a win if casting to a signed type is cheap,
6561 i.e. when arg00's type is not a partial mode. */
6562 && TYPE_PRECISION (TREE_TYPE (arg00))
6563 == GET_MODE_PRECISION (TYPE_MODE (TREE_TYPE (arg00))))
6565 tree stype = signed_type_for (TREE_TYPE (arg00));
6566 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6567 result_type,
6568 fold_convert_loc (loc, stype, arg00),
6569 build_int_cst (stype, 0));
6573 return NULL_TREE;
6576 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6577 equality/inequality test, then return a simplified form of
6578 the test using shifts and logical operations. Otherwise return
6579 NULL. TYPE is the desired result type. */
6581 tree
6582 fold_single_bit_test (location_t loc, enum tree_code code,
6583 tree arg0, tree arg1, tree result_type)
6585 /* If this is testing a single bit, we can optimize the test. */
6586 if ((code == NE_EXPR || code == EQ_EXPR)
6587 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6588 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6590 tree inner = TREE_OPERAND (arg0, 0);
6591 tree type = TREE_TYPE (arg0);
6592 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6593 machine_mode operand_mode = TYPE_MODE (type);
6594 int ops_unsigned;
6595 tree signed_type, unsigned_type, intermediate_type;
6596 tree tem, one;
6598 /* First, see if we can fold the single bit test into a sign-bit
6599 test. */
6600 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6601 result_type);
6602 if (tem)
6603 return tem;
6605 /* Otherwise we have (A & C) != 0 where C is a single bit,
6606 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6607 Similarly for (A & C) == 0. */
6609 /* If INNER is a right shift of a constant and it plus BITNUM does
6610 not overflow, adjust BITNUM and INNER. */
6611 if (TREE_CODE (inner) == RSHIFT_EXPR
6612 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6613 && bitnum < TYPE_PRECISION (type)
6614 && wi::ltu_p (TREE_OPERAND (inner, 1),
6615 TYPE_PRECISION (type) - bitnum))
6617 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6618 inner = TREE_OPERAND (inner, 0);
6621 /* If we are going to be able to omit the AND below, we must do our
6622 operations as unsigned. If we must use the AND, we have a choice.
6623 Normally unsigned is faster, but for some machines signed is. */
6624 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6625 && !flag_syntax_only) ? 0 : 1;
6627 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6628 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6629 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6630 inner = fold_convert_loc (loc, intermediate_type, inner);
6632 if (bitnum != 0)
6633 inner = build2 (RSHIFT_EXPR, intermediate_type,
6634 inner, size_int (bitnum));
6636 one = build_int_cst (intermediate_type, 1);
6638 if (code == EQ_EXPR)
6639 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6641 /* Put the AND last so it can combine with more things. */
6642 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6644 /* Make sure to return the proper type. */
6645 inner = fold_convert_loc (loc, result_type, inner);
6647 return inner;
6649 return NULL_TREE;
6652 /* Check whether we are allowed to reorder operands arg0 and arg1,
6653 such that the evaluation of arg1 occurs before arg0. */
6655 static bool
6656 reorder_operands_p (const_tree arg0, const_tree arg1)
6658 if (! flag_evaluation_order)
6659 return true;
6660 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6661 return true;
6662 return ! TREE_SIDE_EFFECTS (arg0)
6663 && ! TREE_SIDE_EFFECTS (arg1);
6666 /* Test whether it is preferable two swap two operands, ARG0 and
6667 ARG1, for example because ARG0 is an integer constant and ARG1
6668 isn't. If REORDER is true, only recommend swapping if we can
6669 evaluate the operands in reverse order. */
6671 bool
6672 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6674 if (CONSTANT_CLASS_P (arg1))
6675 return 0;
6676 if (CONSTANT_CLASS_P (arg0))
6677 return 1;
6679 STRIP_NOPS (arg0);
6680 STRIP_NOPS (arg1);
6682 if (TREE_CONSTANT (arg1))
6683 return 0;
6684 if (TREE_CONSTANT (arg0))
6685 return 1;
6687 if (reorder && flag_evaluation_order
6688 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6689 return 0;
6691 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6692 for commutative and comparison operators. Ensuring a canonical
6693 form allows the optimizers to find additional redundancies without
6694 having to explicitly check for both orderings. */
6695 if (TREE_CODE (arg0) == SSA_NAME
6696 && TREE_CODE (arg1) == SSA_NAME
6697 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6698 return 1;
6700 /* Put SSA_NAMEs last. */
6701 if (TREE_CODE (arg1) == SSA_NAME)
6702 return 0;
6703 if (TREE_CODE (arg0) == SSA_NAME)
6704 return 1;
6706 /* Put variables last. */
6707 if (DECL_P (arg1))
6708 return 0;
6709 if (DECL_P (arg0))
6710 return 1;
6712 return 0;
6716 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6717 means A >= Y && A != MAX, but in this case we know that
6718 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6720 static tree
6721 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6723 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6725 if (TREE_CODE (bound) == LT_EXPR)
6726 a = TREE_OPERAND (bound, 0);
6727 else if (TREE_CODE (bound) == GT_EXPR)
6728 a = TREE_OPERAND (bound, 1);
6729 else
6730 return NULL_TREE;
6732 typea = TREE_TYPE (a);
6733 if (!INTEGRAL_TYPE_P (typea)
6734 && !POINTER_TYPE_P (typea))
6735 return NULL_TREE;
6737 if (TREE_CODE (ineq) == LT_EXPR)
6739 a1 = TREE_OPERAND (ineq, 1);
6740 y = TREE_OPERAND (ineq, 0);
6742 else if (TREE_CODE (ineq) == GT_EXPR)
6744 a1 = TREE_OPERAND (ineq, 0);
6745 y = TREE_OPERAND (ineq, 1);
6747 else
6748 return NULL_TREE;
6750 if (TREE_TYPE (a1) != typea)
6751 return NULL_TREE;
6753 if (POINTER_TYPE_P (typea))
6755 /* Convert the pointer types into integer before taking the difference. */
6756 tree ta = fold_convert_loc (loc, ssizetype, a);
6757 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6758 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6760 else
6761 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6763 if (!diff || !integer_onep (diff))
6764 return NULL_TREE;
6766 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6769 /* Fold a sum or difference of at least one multiplication.
6770 Returns the folded tree or NULL if no simplification could be made. */
6772 static tree
6773 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6774 tree arg0, tree arg1)
6776 tree arg00, arg01, arg10, arg11;
6777 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6779 /* (A * C) +- (B * C) -> (A+-B) * C.
6780 (A * C) +- A -> A * (C+-1).
6781 We are most concerned about the case where C is a constant,
6782 but other combinations show up during loop reduction. Since
6783 it is not difficult, try all four possibilities. */
6785 if (TREE_CODE (arg0) == MULT_EXPR)
6787 arg00 = TREE_OPERAND (arg0, 0);
6788 arg01 = TREE_OPERAND (arg0, 1);
6790 else if (TREE_CODE (arg0) == INTEGER_CST)
6792 arg00 = build_one_cst (type);
6793 arg01 = arg0;
6795 else
6797 /* We cannot generate constant 1 for fract. */
6798 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6799 return NULL_TREE;
6800 arg00 = arg0;
6801 arg01 = build_one_cst (type);
6803 if (TREE_CODE (arg1) == MULT_EXPR)
6805 arg10 = TREE_OPERAND (arg1, 0);
6806 arg11 = TREE_OPERAND (arg1, 1);
6808 else if (TREE_CODE (arg1) == INTEGER_CST)
6810 arg10 = build_one_cst (type);
6811 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
6812 the purpose of this canonicalization. */
6813 if (wi::neg_p (arg1, TYPE_SIGN (TREE_TYPE (arg1)))
6814 && negate_expr_p (arg1)
6815 && code == PLUS_EXPR)
6817 arg11 = negate_expr (arg1);
6818 code = MINUS_EXPR;
6820 else
6821 arg11 = arg1;
6823 else
6825 /* We cannot generate constant 1 for fract. */
6826 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6827 return NULL_TREE;
6828 arg10 = arg1;
6829 arg11 = build_one_cst (type);
6831 same = NULL_TREE;
6833 if (operand_equal_p (arg01, arg11, 0))
6834 same = arg01, alt0 = arg00, alt1 = arg10;
6835 else if (operand_equal_p (arg00, arg10, 0))
6836 same = arg00, alt0 = arg01, alt1 = arg11;
6837 else if (operand_equal_p (arg00, arg11, 0))
6838 same = arg00, alt0 = arg01, alt1 = arg10;
6839 else if (operand_equal_p (arg01, arg10, 0))
6840 same = arg01, alt0 = arg00, alt1 = arg11;
6842 /* No identical multiplicands; see if we can find a common
6843 power-of-two factor in non-power-of-two multiplies. This
6844 can help in multi-dimensional array access. */
6845 else if (tree_fits_shwi_p (arg01)
6846 && tree_fits_shwi_p (arg11))
6848 HOST_WIDE_INT int01, int11, tmp;
6849 bool swap = false;
6850 tree maybe_same;
6851 int01 = tree_to_shwi (arg01);
6852 int11 = tree_to_shwi (arg11);
6854 /* Move min of absolute values to int11. */
6855 if (absu_hwi (int01) < absu_hwi (int11))
6857 tmp = int01, int01 = int11, int11 = tmp;
6858 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6859 maybe_same = arg01;
6860 swap = true;
6862 else
6863 maybe_same = arg11;
6865 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
6866 /* The remainder should not be a constant, otherwise we
6867 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
6868 increased the number of multiplications necessary. */
6869 && TREE_CODE (arg10) != INTEGER_CST)
6871 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
6872 build_int_cst (TREE_TYPE (arg00),
6873 int01 / int11));
6874 alt1 = arg10;
6875 same = maybe_same;
6876 if (swap)
6877 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
6881 if (same)
6882 return fold_build2_loc (loc, MULT_EXPR, type,
6883 fold_build2_loc (loc, code, type,
6884 fold_convert_loc (loc, type, alt0),
6885 fold_convert_loc (loc, type, alt1)),
6886 fold_convert_loc (loc, type, same));
6888 return NULL_TREE;
6891 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
6892 specified by EXPR into the buffer PTR of length LEN bytes.
6893 Return the number of bytes placed in the buffer, or zero
6894 upon failure. */
6896 static int
6897 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
6899 tree type = TREE_TYPE (expr);
6900 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
6901 int byte, offset, word, words;
6902 unsigned char value;
6904 if ((off == -1 && total_bytes > len)
6905 || off >= total_bytes)
6906 return 0;
6907 if (off == -1)
6908 off = 0;
6909 words = total_bytes / UNITS_PER_WORD;
6911 for (byte = 0; byte < total_bytes; byte++)
6913 int bitpos = byte * BITS_PER_UNIT;
6914 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
6915 number of bytes. */
6916 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
6918 if (total_bytes > UNITS_PER_WORD)
6920 word = byte / UNITS_PER_WORD;
6921 if (WORDS_BIG_ENDIAN)
6922 word = (words - 1) - word;
6923 offset = word * UNITS_PER_WORD;
6924 if (BYTES_BIG_ENDIAN)
6925 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
6926 else
6927 offset += byte % UNITS_PER_WORD;
6929 else
6930 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
6931 if (offset >= off
6932 && offset - off < len)
6933 ptr[offset - off] = value;
6935 return MIN (len, total_bytes - off);
6939 /* Subroutine of native_encode_expr. Encode the FIXED_CST
6940 specified by EXPR into the buffer PTR of length LEN bytes.
6941 Return the number of bytes placed in the buffer, or zero
6942 upon failure. */
6944 static int
6945 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
6947 tree type = TREE_TYPE (expr);
6948 machine_mode mode = TYPE_MODE (type);
6949 int total_bytes = GET_MODE_SIZE (mode);
6950 FIXED_VALUE_TYPE value;
6951 tree i_value, i_type;
6953 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
6954 return 0;
6956 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
6958 if (NULL_TREE == i_type
6959 || TYPE_PRECISION (i_type) != total_bytes)
6960 return 0;
6962 value = TREE_FIXED_CST (expr);
6963 i_value = double_int_to_tree (i_type, value.data);
6965 return native_encode_int (i_value, ptr, len, off);
6969 /* Subroutine of native_encode_expr. Encode the REAL_CST
6970 specified by EXPR into the buffer PTR of length LEN bytes.
6971 Return the number of bytes placed in the buffer, or zero
6972 upon failure. */
6974 static int
6975 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
6977 tree type = TREE_TYPE (expr);
6978 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
6979 int byte, offset, word, words, bitpos;
6980 unsigned char value;
6982 /* There are always 32 bits in each long, no matter the size of
6983 the hosts long. We handle floating point representations with
6984 up to 192 bits. */
6985 long tmp[6];
6987 if ((off == -1 && total_bytes > len)
6988 || off >= total_bytes)
6989 return 0;
6990 if (off == -1)
6991 off = 0;
6992 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
6994 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
6996 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
6997 bitpos += BITS_PER_UNIT)
6999 byte = (bitpos / BITS_PER_UNIT) & 3;
7000 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7002 if (UNITS_PER_WORD < 4)
7004 word = byte / UNITS_PER_WORD;
7005 if (WORDS_BIG_ENDIAN)
7006 word = (words - 1) - word;
7007 offset = word * UNITS_PER_WORD;
7008 if (BYTES_BIG_ENDIAN)
7009 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7010 else
7011 offset += byte % UNITS_PER_WORD;
7013 else
7014 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7015 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7016 if (offset >= off
7017 && offset - off < len)
7018 ptr[offset - off] = value;
7020 return MIN (len, total_bytes - off);
7023 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7024 specified by EXPR into the buffer PTR of length LEN bytes.
7025 Return the number of bytes placed in the buffer, or zero
7026 upon failure. */
7028 static int
7029 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7031 int rsize, isize;
7032 tree part;
7034 part = TREE_REALPART (expr);
7035 rsize = native_encode_expr (part, ptr, len, off);
7036 if (off == -1
7037 && rsize == 0)
7038 return 0;
7039 part = TREE_IMAGPART (expr);
7040 if (off != -1)
7041 off = MAX (0, off - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (part))));
7042 isize = native_encode_expr (part, ptr+rsize, len-rsize, off);
7043 if (off == -1
7044 && isize != rsize)
7045 return 0;
7046 return rsize + isize;
7050 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7051 specified by EXPR into the buffer PTR of length LEN bytes.
7052 Return the number of bytes placed in the buffer, or zero
7053 upon failure. */
7055 static int
7056 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7058 unsigned i, count;
7059 int size, offset;
7060 tree itype, elem;
7062 offset = 0;
7063 count = VECTOR_CST_NELTS (expr);
7064 itype = TREE_TYPE (TREE_TYPE (expr));
7065 size = GET_MODE_SIZE (TYPE_MODE (itype));
7066 for (i = 0; i < count; i++)
7068 if (off >= size)
7070 off -= size;
7071 continue;
7073 elem = VECTOR_CST_ELT (expr, i);
7074 int res = native_encode_expr (elem, ptr+offset, len-offset, off);
7075 if ((off == -1 && res != size)
7076 || res == 0)
7077 return 0;
7078 offset += res;
7079 if (offset >= len)
7080 return offset;
7081 if (off != -1)
7082 off = 0;
7084 return offset;
7088 /* Subroutine of native_encode_expr. Encode the STRING_CST
7089 specified by EXPR into the buffer PTR of length LEN bytes.
7090 Return the number of bytes placed in the buffer, or zero
7091 upon failure. */
7093 static int
7094 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7096 tree type = TREE_TYPE (expr);
7097 HOST_WIDE_INT total_bytes;
7099 if (TREE_CODE (type) != ARRAY_TYPE
7100 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7101 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7102 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7103 return 0;
7104 total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (type));
7105 if ((off == -1 && total_bytes > len)
7106 || off >= total_bytes)
7107 return 0;
7108 if (off == -1)
7109 off = 0;
7110 if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7112 int written = 0;
7113 if (off < TREE_STRING_LENGTH (expr))
7115 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7116 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7118 memset (ptr + written, 0,
7119 MIN (total_bytes - written, len - written));
7121 else
7122 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7123 return MIN (total_bytes - off, len);
7127 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7128 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7129 buffer PTR of length LEN bytes. If OFF is not -1 then start
7130 the encoding at byte offset OFF and encode at most LEN bytes.
7131 Return the number of bytes placed in the buffer, or zero upon failure. */
7134 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7136 /* We don't support starting at negative offset and -1 is special. */
7137 if (off < -1)
7138 return 0;
7140 switch (TREE_CODE (expr))
7142 case INTEGER_CST:
7143 return native_encode_int (expr, ptr, len, off);
7145 case REAL_CST:
7146 return native_encode_real (expr, ptr, len, off);
7148 case FIXED_CST:
7149 return native_encode_fixed (expr, ptr, len, off);
7151 case COMPLEX_CST:
7152 return native_encode_complex (expr, ptr, len, off);
7154 case VECTOR_CST:
7155 return native_encode_vector (expr, ptr, len, off);
7157 case STRING_CST:
7158 return native_encode_string (expr, ptr, len, off);
7160 default:
7161 return 0;
7166 /* Subroutine of native_interpret_expr. Interpret the contents of
7167 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7168 If the buffer cannot be interpreted, return NULL_TREE. */
7170 static tree
7171 native_interpret_int (tree type, const unsigned char *ptr, int len)
7173 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7175 if (total_bytes > len
7176 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7177 return NULL_TREE;
7179 wide_int result = wi::from_buffer (ptr, total_bytes);
7181 return wide_int_to_tree (type, result);
7185 /* Subroutine of native_interpret_expr. Interpret the contents of
7186 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7187 If the buffer cannot be interpreted, return NULL_TREE. */
7189 static tree
7190 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7192 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7193 double_int result;
7194 FIXED_VALUE_TYPE fixed_value;
7196 if (total_bytes > len
7197 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7198 return NULL_TREE;
7200 result = double_int::from_buffer (ptr, total_bytes);
7201 fixed_value = fixed_from_double_int (result, TYPE_MODE (type));
7203 return build_fixed (type, fixed_value);
7207 /* Subroutine of native_interpret_expr. Interpret the contents of
7208 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7209 If the buffer cannot be interpreted, return NULL_TREE. */
7211 static tree
7212 native_interpret_real (tree type, const unsigned char *ptr, int len)
7214 machine_mode mode = TYPE_MODE (type);
7215 int total_bytes = GET_MODE_SIZE (mode);
7216 unsigned char value;
7217 /* There are always 32 bits in each long, no matter the size of
7218 the hosts long. We handle floating point representations with
7219 up to 192 bits. */
7220 REAL_VALUE_TYPE r;
7221 long tmp[6];
7223 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7224 if (total_bytes > len || total_bytes > 24)
7225 return NULL_TREE;
7226 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7228 memset (tmp, 0, sizeof (tmp));
7229 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7230 bitpos += BITS_PER_UNIT)
7232 /* Both OFFSET and BYTE index within a long;
7233 bitpos indexes the whole float. */
7234 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7235 if (UNITS_PER_WORD < 4)
7237 int word = byte / UNITS_PER_WORD;
7238 if (WORDS_BIG_ENDIAN)
7239 word = (words - 1) - word;
7240 offset = word * UNITS_PER_WORD;
7241 if (BYTES_BIG_ENDIAN)
7242 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7243 else
7244 offset += byte % UNITS_PER_WORD;
7246 else
7248 offset = byte;
7249 if (BYTES_BIG_ENDIAN)
7251 /* Reverse bytes within each long, or within the entire float
7252 if it's smaller than a long (for HFmode). */
7253 offset = MIN (3, total_bytes - 1) - offset;
7254 gcc_assert (offset >= 0);
7257 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7259 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7262 real_from_target (&r, tmp, mode);
7263 return build_real (type, r);
7267 /* Subroutine of native_interpret_expr. Interpret the contents of
7268 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7269 If the buffer cannot be interpreted, return NULL_TREE. */
7271 static tree
7272 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7274 tree etype, rpart, ipart;
7275 int size;
7277 etype = TREE_TYPE (type);
7278 size = GET_MODE_SIZE (TYPE_MODE (etype));
7279 if (size * 2 > len)
7280 return NULL_TREE;
7281 rpart = native_interpret_expr (etype, ptr, size);
7282 if (!rpart)
7283 return NULL_TREE;
7284 ipart = native_interpret_expr (etype, ptr+size, size);
7285 if (!ipart)
7286 return NULL_TREE;
7287 return build_complex (type, rpart, ipart);
7291 /* Subroutine of native_interpret_expr. Interpret the contents of
7292 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7293 If the buffer cannot be interpreted, return NULL_TREE. */
7295 static tree
7296 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7298 tree etype, elem;
7299 int i, size, count;
7300 tree *elements;
7302 etype = TREE_TYPE (type);
7303 size = GET_MODE_SIZE (TYPE_MODE (etype));
7304 count = TYPE_VECTOR_SUBPARTS (type);
7305 if (size * count > len)
7306 return NULL_TREE;
7308 elements = XALLOCAVEC (tree, count);
7309 for (i = count - 1; i >= 0; i--)
7311 elem = native_interpret_expr (etype, ptr+(i*size), size);
7312 if (!elem)
7313 return NULL_TREE;
7314 elements[i] = elem;
7316 return build_vector (type, elements);
7320 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7321 the buffer PTR of length LEN as a constant of type TYPE. For
7322 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7323 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7324 return NULL_TREE. */
7326 tree
7327 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7329 switch (TREE_CODE (type))
7331 case INTEGER_TYPE:
7332 case ENUMERAL_TYPE:
7333 case BOOLEAN_TYPE:
7334 case POINTER_TYPE:
7335 case REFERENCE_TYPE:
7336 return native_interpret_int (type, ptr, len);
7338 case REAL_TYPE:
7339 return native_interpret_real (type, ptr, len);
7341 case FIXED_POINT_TYPE:
7342 return native_interpret_fixed (type, ptr, len);
7344 case COMPLEX_TYPE:
7345 return native_interpret_complex (type, ptr, len);
7347 case VECTOR_TYPE:
7348 return native_interpret_vector (type, ptr, len);
7350 default:
7351 return NULL_TREE;
7355 /* Returns true if we can interpret the contents of a native encoding
7356 as TYPE. */
7358 static bool
7359 can_native_interpret_type_p (tree type)
7361 switch (TREE_CODE (type))
7363 case INTEGER_TYPE:
7364 case ENUMERAL_TYPE:
7365 case BOOLEAN_TYPE:
7366 case POINTER_TYPE:
7367 case REFERENCE_TYPE:
7368 case FIXED_POINT_TYPE:
7369 case REAL_TYPE:
7370 case COMPLEX_TYPE:
7371 case VECTOR_TYPE:
7372 return true;
7373 default:
7374 return false;
7378 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7379 TYPE at compile-time. If we're unable to perform the conversion
7380 return NULL_TREE. */
7382 static tree
7383 fold_view_convert_expr (tree type, tree expr)
7385 /* We support up to 512-bit values (for V8DFmode). */
7386 unsigned char buffer[64];
7387 int len;
7389 /* Check that the host and target are sane. */
7390 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7391 return NULL_TREE;
7393 len = native_encode_expr (expr, buffer, sizeof (buffer));
7394 if (len == 0)
7395 return NULL_TREE;
7397 return native_interpret_expr (type, buffer, len);
7400 /* Build an expression for the address of T. Folds away INDIRECT_REF
7401 to avoid confusing the gimplify process. */
7403 tree
7404 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7406 /* The size of the object is not relevant when talking about its address. */
7407 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7408 t = TREE_OPERAND (t, 0);
7410 if (TREE_CODE (t) == INDIRECT_REF)
7412 t = TREE_OPERAND (t, 0);
7414 if (TREE_TYPE (t) != ptrtype)
7415 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7417 else if (TREE_CODE (t) == MEM_REF
7418 && integer_zerop (TREE_OPERAND (t, 1)))
7419 return TREE_OPERAND (t, 0);
7420 else if (TREE_CODE (t) == MEM_REF
7421 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7422 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7423 TREE_OPERAND (t, 0),
7424 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7425 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7427 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7429 if (TREE_TYPE (t) != ptrtype)
7430 t = fold_convert_loc (loc, ptrtype, t);
7432 else
7433 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7435 return t;
7438 /* Build an expression for the address of T. */
7440 tree
7441 build_fold_addr_expr_loc (location_t loc, tree t)
7443 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7445 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7448 /* Fold a unary expression of code CODE and type TYPE with operand
7449 OP0. Return the folded expression if folding is successful.
7450 Otherwise, return NULL_TREE. */
7452 tree
7453 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7455 tree tem;
7456 tree arg0;
7457 enum tree_code_class kind = TREE_CODE_CLASS (code);
7459 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7460 && TREE_CODE_LENGTH (code) == 1);
7462 arg0 = op0;
7463 if (arg0)
7465 if (CONVERT_EXPR_CODE_P (code)
7466 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7468 /* Don't use STRIP_NOPS, because signedness of argument type
7469 matters. */
7470 STRIP_SIGN_NOPS (arg0);
7472 else
7474 /* Strip any conversions that don't change the mode. This
7475 is safe for every expression, except for a comparison
7476 expression because its signedness is derived from its
7477 operands.
7479 Note that this is done as an internal manipulation within
7480 the constant folder, in order to find the simplest
7481 representation of the arguments so that their form can be
7482 studied. In any cases, the appropriate type conversions
7483 should be put back in the tree that will get out of the
7484 constant folder. */
7485 STRIP_NOPS (arg0);
7488 if (CONSTANT_CLASS_P (arg0))
7490 tree tem = const_unop (code, type, arg0);
7491 if (tem)
7493 if (TREE_TYPE (tem) != type)
7494 tem = fold_convert_loc (loc, type, tem);
7495 return tem;
7500 tem = generic_simplify (loc, code, type, op0);
7501 if (tem)
7502 return tem;
7504 if (TREE_CODE_CLASS (code) == tcc_unary)
7506 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7507 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7508 fold_build1_loc (loc, code, type,
7509 fold_convert_loc (loc, TREE_TYPE (op0),
7510 TREE_OPERAND (arg0, 1))));
7511 else if (TREE_CODE (arg0) == COND_EXPR)
7513 tree arg01 = TREE_OPERAND (arg0, 1);
7514 tree arg02 = TREE_OPERAND (arg0, 2);
7515 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7516 arg01 = fold_build1_loc (loc, code, type,
7517 fold_convert_loc (loc,
7518 TREE_TYPE (op0), arg01));
7519 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7520 arg02 = fold_build1_loc (loc, code, type,
7521 fold_convert_loc (loc,
7522 TREE_TYPE (op0), arg02));
7523 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7524 arg01, arg02);
7526 /* If this was a conversion, and all we did was to move into
7527 inside the COND_EXPR, bring it back out. But leave it if
7528 it is a conversion from integer to integer and the
7529 result precision is no wider than a word since such a
7530 conversion is cheap and may be optimized away by combine,
7531 while it couldn't if it were outside the COND_EXPR. Then return
7532 so we don't get into an infinite recursion loop taking the
7533 conversion out and then back in. */
7535 if ((CONVERT_EXPR_CODE_P (code)
7536 || code == NON_LVALUE_EXPR)
7537 && TREE_CODE (tem) == COND_EXPR
7538 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7539 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7540 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7541 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7542 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7543 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7544 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7545 && (INTEGRAL_TYPE_P
7546 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7547 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7548 || flag_syntax_only))
7549 tem = build1_loc (loc, code, type,
7550 build3 (COND_EXPR,
7551 TREE_TYPE (TREE_OPERAND
7552 (TREE_OPERAND (tem, 1), 0)),
7553 TREE_OPERAND (tem, 0),
7554 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7555 TREE_OPERAND (TREE_OPERAND (tem, 2),
7556 0)));
7557 return tem;
7561 switch (code)
7563 case NON_LVALUE_EXPR:
7564 if (!maybe_lvalue_p (op0))
7565 return fold_convert_loc (loc, type, op0);
7566 return NULL_TREE;
7568 CASE_CONVERT:
7569 case FLOAT_EXPR:
7570 case FIX_TRUNC_EXPR:
7571 if (COMPARISON_CLASS_P (op0))
7573 /* If we have (type) (a CMP b) and type is an integral type, return
7574 new expression involving the new type. Canonicalize
7575 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7576 non-integral type.
7577 Do not fold the result as that would not simplify further, also
7578 folding again results in recursions. */
7579 if (TREE_CODE (type) == BOOLEAN_TYPE)
7580 return build2_loc (loc, TREE_CODE (op0), type,
7581 TREE_OPERAND (op0, 0),
7582 TREE_OPERAND (op0, 1));
7583 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7584 && TREE_CODE (type) != VECTOR_TYPE)
7585 return build3_loc (loc, COND_EXPR, type, op0,
7586 constant_boolean_node (true, type),
7587 constant_boolean_node (false, type));
7590 /* Handle (T *)&A.B.C for A being of type T and B and C
7591 living at offset zero. This occurs frequently in
7592 C++ upcasting and then accessing the base. */
7593 if (TREE_CODE (op0) == ADDR_EXPR
7594 && POINTER_TYPE_P (type)
7595 && handled_component_p (TREE_OPERAND (op0, 0)))
7597 HOST_WIDE_INT bitsize, bitpos;
7598 tree offset;
7599 machine_mode mode;
7600 int unsignedp, volatilep;
7601 tree base = TREE_OPERAND (op0, 0);
7602 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7603 &mode, &unsignedp, &volatilep, false);
7604 /* If the reference was to a (constant) zero offset, we can use
7605 the address of the base if it has the same base type
7606 as the result type and the pointer type is unqualified. */
7607 if (! offset && bitpos == 0
7608 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7609 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7610 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7611 return fold_convert_loc (loc, type,
7612 build_fold_addr_expr_loc (loc, base));
7615 if (TREE_CODE (op0) == MODIFY_EXPR
7616 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7617 /* Detect assigning a bitfield. */
7618 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7619 && DECL_BIT_FIELD
7620 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7622 /* Don't leave an assignment inside a conversion
7623 unless assigning a bitfield. */
7624 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7625 /* First do the assignment, then return converted constant. */
7626 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7627 TREE_NO_WARNING (tem) = 1;
7628 TREE_USED (tem) = 1;
7629 return tem;
7632 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7633 constants (if x has signed type, the sign bit cannot be set
7634 in c). This folds extension into the BIT_AND_EXPR.
7635 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7636 very likely don't have maximal range for their precision and this
7637 transformation effectively doesn't preserve non-maximal ranges. */
7638 if (TREE_CODE (type) == INTEGER_TYPE
7639 && TREE_CODE (op0) == BIT_AND_EXPR
7640 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7642 tree and_expr = op0;
7643 tree and0 = TREE_OPERAND (and_expr, 0);
7644 tree and1 = TREE_OPERAND (and_expr, 1);
7645 int change = 0;
7647 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7648 || (TYPE_PRECISION (type)
7649 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7650 change = 1;
7651 else if (TYPE_PRECISION (TREE_TYPE (and1))
7652 <= HOST_BITS_PER_WIDE_INT
7653 && tree_fits_uhwi_p (and1))
7655 unsigned HOST_WIDE_INT cst;
7657 cst = tree_to_uhwi (and1);
7658 cst &= HOST_WIDE_INT_M1U
7659 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7660 change = (cst == 0);
7661 if (change
7662 && !flag_syntax_only
7663 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7664 == ZERO_EXTEND))
7666 tree uns = unsigned_type_for (TREE_TYPE (and0));
7667 and0 = fold_convert_loc (loc, uns, and0);
7668 and1 = fold_convert_loc (loc, uns, and1);
7671 if (change)
7673 tem = force_fit_type (type, wi::to_widest (and1), 0,
7674 TREE_OVERFLOW (and1));
7675 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7676 fold_convert_loc (loc, type, and0), tem);
7680 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7681 when one of the new casts will fold away. Conservatively we assume
7682 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7683 if (POINTER_TYPE_P (type)
7684 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7685 && (!TYPE_RESTRICT (type) || TYPE_RESTRICT (TREE_TYPE (arg0)))
7686 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7687 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7688 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7690 tree arg00 = TREE_OPERAND (arg0, 0);
7691 tree arg01 = TREE_OPERAND (arg0, 1);
7693 return fold_build_pointer_plus_loc
7694 (loc, fold_convert_loc (loc, type, arg00), arg01);
7697 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7698 of the same precision, and X is an integer type not narrower than
7699 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7700 if (INTEGRAL_TYPE_P (type)
7701 && TREE_CODE (op0) == BIT_NOT_EXPR
7702 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7703 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7704 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7706 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7707 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7708 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7709 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7710 fold_convert_loc (loc, type, tem));
7713 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7714 type of X and Y (integer types only). */
7715 if (INTEGRAL_TYPE_P (type)
7716 && TREE_CODE (op0) == MULT_EXPR
7717 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7718 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7720 /* Be careful not to introduce new overflows. */
7721 tree mult_type;
7722 if (TYPE_OVERFLOW_WRAPS (type))
7723 mult_type = type;
7724 else
7725 mult_type = unsigned_type_for (type);
7727 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7729 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7730 fold_convert_loc (loc, mult_type,
7731 TREE_OPERAND (op0, 0)),
7732 fold_convert_loc (loc, mult_type,
7733 TREE_OPERAND (op0, 1)));
7734 return fold_convert_loc (loc, type, tem);
7738 return NULL_TREE;
7740 case VIEW_CONVERT_EXPR:
7741 if (TREE_CODE (op0) == MEM_REF)
7742 return fold_build2_loc (loc, MEM_REF, type,
7743 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7745 return NULL_TREE;
7747 case NEGATE_EXPR:
7748 tem = fold_negate_expr (loc, arg0);
7749 if (tem)
7750 return fold_convert_loc (loc, type, tem);
7751 return NULL_TREE;
7753 case ABS_EXPR:
7754 /* Convert fabs((double)float) into (double)fabsf(float). */
7755 if (TREE_CODE (arg0) == NOP_EXPR
7756 && TREE_CODE (type) == REAL_TYPE)
7758 tree targ0 = strip_float_extensions (arg0);
7759 if (targ0 != arg0)
7760 return fold_convert_loc (loc, type,
7761 fold_build1_loc (loc, ABS_EXPR,
7762 TREE_TYPE (targ0),
7763 targ0));
7766 /* Strip sign ops from argument. */
7767 if (TREE_CODE (type) == REAL_TYPE)
7769 tem = fold_strip_sign_ops (arg0);
7770 if (tem)
7771 return fold_build1_loc (loc, ABS_EXPR, type,
7772 fold_convert_loc (loc, type, tem));
7774 return NULL_TREE;
7776 case BIT_NOT_EXPR:
7777 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
7778 if (TREE_CODE (arg0) == BIT_XOR_EXPR
7779 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7780 fold_convert_loc (loc, type,
7781 TREE_OPERAND (arg0, 0)))))
7782 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
7783 fold_convert_loc (loc, type,
7784 TREE_OPERAND (arg0, 1)));
7785 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
7786 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
7787 fold_convert_loc (loc, type,
7788 TREE_OPERAND (arg0, 1)))))
7789 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
7790 fold_convert_loc (loc, type,
7791 TREE_OPERAND (arg0, 0)), tem);
7793 return NULL_TREE;
7795 case TRUTH_NOT_EXPR:
7796 /* Note that the operand of this must be an int
7797 and its values must be 0 or 1.
7798 ("true" is a fixed value perhaps depending on the language,
7799 but we don't handle values other than 1 correctly yet.) */
7800 tem = fold_truth_not_expr (loc, arg0);
7801 if (!tem)
7802 return NULL_TREE;
7803 return fold_convert_loc (loc, type, tem);
7805 case INDIRECT_REF:
7806 /* Fold *&X to X if X is an lvalue. */
7807 if (TREE_CODE (op0) == ADDR_EXPR)
7809 tree op00 = TREE_OPERAND (op0, 0);
7810 if ((TREE_CODE (op00) == VAR_DECL
7811 || TREE_CODE (op00) == PARM_DECL
7812 || TREE_CODE (op00) == RESULT_DECL)
7813 && !TREE_READONLY (op00))
7814 return op00;
7816 return NULL_TREE;
7818 default:
7819 return NULL_TREE;
7820 } /* switch (code) */
7824 /* If the operation was a conversion do _not_ mark a resulting constant
7825 with TREE_OVERFLOW if the original constant was not. These conversions
7826 have implementation defined behavior and retaining the TREE_OVERFLOW
7827 flag here would confuse later passes such as VRP. */
7828 tree
7829 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
7830 tree type, tree op0)
7832 tree res = fold_unary_loc (loc, code, type, op0);
7833 if (res
7834 && TREE_CODE (res) == INTEGER_CST
7835 && TREE_CODE (op0) == INTEGER_CST
7836 && CONVERT_EXPR_CODE_P (code))
7837 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
7839 return res;
7842 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
7843 operands OP0 and OP1. LOC is the location of the resulting expression.
7844 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
7845 Return the folded expression if folding is successful. Otherwise,
7846 return NULL_TREE. */
7847 static tree
7848 fold_truth_andor (location_t loc, enum tree_code code, tree type,
7849 tree arg0, tree arg1, tree op0, tree op1)
7851 tree tem;
7853 /* We only do these simplifications if we are optimizing. */
7854 if (!optimize)
7855 return NULL_TREE;
7857 /* Check for things like (A || B) && (A || C). We can convert this
7858 to A || (B && C). Note that either operator can be any of the four
7859 truth and/or operations and the transformation will still be
7860 valid. Also note that we only care about order for the
7861 ANDIF and ORIF operators. If B contains side effects, this
7862 might change the truth-value of A. */
7863 if (TREE_CODE (arg0) == TREE_CODE (arg1)
7864 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7865 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7866 || TREE_CODE (arg0) == TRUTH_AND_EXPR
7867 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
7868 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
7870 tree a00 = TREE_OPERAND (arg0, 0);
7871 tree a01 = TREE_OPERAND (arg0, 1);
7872 tree a10 = TREE_OPERAND (arg1, 0);
7873 tree a11 = TREE_OPERAND (arg1, 1);
7874 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
7875 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
7876 && (code == TRUTH_AND_EXPR
7877 || code == TRUTH_OR_EXPR));
7879 if (operand_equal_p (a00, a10, 0))
7880 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
7881 fold_build2_loc (loc, code, type, a01, a11));
7882 else if (commutative && operand_equal_p (a00, a11, 0))
7883 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
7884 fold_build2_loc (loc, code, type, a01, a10));
7885 else if (commutative && operand_equal_p (a01, a10, 0))
7886 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
7887 fold_build2_loc (loc, code, type, a00, a11));
7889 /* This case if tricky because we must either have commutative
7890 operators or else A10 must not have side-effects. */
7892 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
7893 && operand_equal_p (a01, a11, 0))
7894 return fold_build2_loc (loc, TREE_CODE (arg0), type,
7895 fold_build2_loc (loc, code, type, a00, a10),
7896 a01);
7899 /* See if we can build a range comparison. */
7900 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
7901 return tem;
7903 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
7904 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
7906 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
7907 if (tem)
7908 return fold_build2_loc (loc, code, type, tem, arg1);
7911 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
7912 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
7914 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
7915 if (tem)
7916 return fold_build2_loc (loc, code, type, arg0, tem);
7919 /* Check for the possibility of merging component references. If our
7920 lhs is another similar operation, try to merge its rhs with our
7921 rhs. Then try to merge our lhs and rhs. */
7922 if (TREE_CODE (arg0) == code
7923 && 0 != (tem = fold_truth_andor_1 (loc, code, type,
7924 TREE_OPERAND (arg0, 1), arg1)))
7925 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
7927 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
7928 return tem;
7930 if (LOGICAL_OP_NON_SHORT_CIRCUIT
7931 && (code == TRUTH_AND_EXPR
7932 || code == TRUTH_ANDIF_EXPR
7933 || code == TRUTH_OR_EXPR
7934 || code == TRUTH_ORIF_EXPR))
7936 enum tree_code ncode, icode;
7938 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
7939 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
7940 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
7942 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
7943 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
7944 We don't want to pack more than two leafs to a non-IF AND/OR
7945 expression.
7946 If tree-code of left-hand operand isn't an AND/OR-IF code and not
7947 equal to IF-CODE, then we don't want to add right-hand operand.
7948 If the inner right-hand side of left-hand operand has
7949 side-effects, or isn't simple, then we can't add to it,
7950 as otherwise we might destroy if-sequence. */
7951 if (TREE_CODE (arg0) == icode
7952 && simple_operand_p_2 (arg1)
7953 /* Needed for sequence points to handle trappings, and
7954 side-effects. */
7955 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
7957 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
7958 arg1);
7959 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
7960 tem);
7962 /* Same as abouve but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
7963 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
7964 else if (TREE_CODE (arg1) == icode
7965 && simple_operand_p_2 (arg0)
7966 /* Needed for sequence points to handle trappings, and
7967 side-effects. */
7968 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
7970 tem = fold_build2_loc (loc, ncode, type,
7971 arg0, TREE_OPERAND (arg1, 0));
7972 return fold_build2_loc (loc, icode, type, tem,
7973 TREE_OPERAND (arg1, 1));
7975 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
7976 into (A OR B).
7977 For sequence point consistancy, we need to check for trapping,
7978 and side-effects. */
7979 else if (code == icode && simple_operand_p_2 (arg0)
7980 && simple_operand_p_2 (arg1))
7981 return fold_build2_loc (loc, ncode, type, arg0, arg1);
7984 return NULL_TREE;
7987 /* Fold a binary expression of code CODE and type TYPE with operands
7988 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
7989 Return the folded expression if folding is successful. Otherwise,
7990 return NULL_TREE. */
7992 static tree
7993 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
7995 enum tree_code compl_code;
7997 if (code == MIN_EXPR)
7998 compl_code = MAX_EXPR;
7999 else if (code == MAX_EXPR)
8000 compl_code = MIN_EXPR;
8001 else
8002 gcc_unreachable ();
8004 /* MIN (MAX (a, b), b) == b. */
8005 if (TREE_CODE (op0) == compl_code
8006 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8007 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
8009 /* MIN (MAX (b, a), b) == b. */
8010 if (TREE_CODE (op0) == compl_code
8011 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8012 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8013 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
8015 /* MIN (a, MAX (a, b)) == a. */
8016 if (TREE_CODE (op1) == compl_code
8017 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8018 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8019 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
8021 /* MIN (a, MAX (b, a)) == a. */
8022 if (TREE_CODE (op1) == compl_code
8023 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8024 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8025 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
8027 return NULL_TREE;
8030 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8031 by changing CODE to reduce the magnitude of constants involved in
8032 ARG0 of the comparison.
8033 Returns a canonicalized comparison tree if a simplification was
8034 possible, otherwise returns NULL_TREE.
8035 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8036 valid if signed overflow is undefined. */
8038 static tree
8039 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8040 tree arg0, tree arg1,
8041 bool *strict_overflow_p)
8043 enum tree_code code0 = TREE_CODE (arg0);
8044 tree t, cst0 = NULL_TREE;
8045 int sgn0;
8047 /* Match A +- CST code arg1. We can change this only if overflow
8048 is undefined. */
8049 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8050 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8051 /* In principle pointers also have undefined overflow behavior,
8052 but that causes problems elsewhere. */
8053 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8054 && (code0 == MINUS_EXPR
8055 || code0 == PLUS_EXPR)
8056 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8057 return NULL_TREE;
8059 /* Identify the constant in arg0 and its sign. */
8060 cst0 = TREE_OPERAND (arg0, 1);
8061 sgn0 = tree_int_cst_sgn (cst0);
8063 /* Overflowed constants and zero will cause problems. */
8064 if (integer_zerop (cst0)
8065 || TREE_OVERFLOW (cst0))
8066 return NULL_TREE;
8068 /* See if we can reduce the magnitude of the constant in
8069 arg0 by changing the comparison code. */
8070 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8071 if (code == LT_EXPR
8072 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8073 code = LE_EXPR;
8074 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8075 else if (code == GT_EXPR
8076 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8077 code = GE_EXPR;
8078 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8079 else if (code == LE_EXPR
8080 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8081 code = LT_EXPR;
8082 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8083 else if (code == GE_EXPR
8084 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8085 code = GT_EXPR;
8086 else
8087 return NULL_TREE;
8088 *strict_overflow_p = true;
8090 /* Now build the constant reduced in magnitude. But not if that
8091 would produce one outside of its types range. */
8092 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8093 && ((sgn0 == 1
8094 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8095 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8096 || (sgn0 == -1
8097 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8098 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8099 return NULL_TREE;
8101 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8102 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8103 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8104 t = fold_convert (TREE_TYPE (arg1), t);
8106 return fold_build2_loc (loc, code, type, t, arg1);
8109 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8110 overflow further. Try to decrease the magnitude of constants involved
8111 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8112 and put sole constants at the second argument position.
8113 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8115 static tree
8116 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8117 tree arg0, tree arg1)
8119 tree t;
8120 bool strict_overflow_p;
8121 const char * const warnmsg = G_("assuming signed overflow does not occur "
8122 "when reducing constant in comparison");
8124 /* Try canonicalization by simplifying arg0. */
8125 strict_overflow_p = false;
8126 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8127 &strict_overflow_p);
8128 if (t)
8130 if (strict_overflow_p)
8131 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8132 return t;
8135 /* Try canonicalization by simplifying arg1 using the swapped
8136 comparison. */
8137 code = swap_tree_comparison (code);
8138 strict_overflow_p = false;
8139 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8140 &strict_overflow_p);
8141 if (t && strict_overflow_p)
8142 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8143 return t;
8146 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8147 space. This is used to avoid issuing overflow warnings for
8148 expressions like &p->x which can not wrap. */
8150 static bool
8151 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8153 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8154 return true;
8156 if (bitpos < 0)
8157 return true;
8159 wide_int wi_offset;
8160 int precision = TYPE_PRECISION (TREE_TYPE (base));
8161 if (offset == NULL_TREE)
8162 wi_offset = wi::zero (precision);
8163 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8164 return true;
8165 else
8166 wi_offset = offset;
8168 bool overflow;
8169 wide_int units = wi::shwi (bitpos / BITS_PER_UNIT, precision);
8170 wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8171 if (overflow)
8172 return true;
8174 if (!wi::fits_uhwi_p (total))
8175 return true;
8177 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8178 if (size <= 0)
8179 return true;
8181 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8182 array. */
8183 if (TREE_CODE (base) == ADDR_EXPR)
8185 HOST_WIDE_INT base_size;
8187 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8188 if (base_size > 0 && size < base_size)
8189 size = base_size;
8192 return total.to_uhwi () > (unsigned HOST_WIDE_INT) size;
8195 /* Return the HOST_WIDE_INT least significant bits of T, a sizetype
8196 kind INTEGER_CST. This makes sure to properly sign-extend the
8197 constant. */
8199 static HOST_WIDE_INT
8200 size_low_cst (const_tree t)
8202 HOST_WIDE_INT w = TREE_INT_CST_ELT (t, 0);
8203 int prec = TYPE_PRECISION (TREE_TYPE (t));
8204 if (prec < HOST_BITS_PER_WIDE_INT)
8205 return sext_hwi (w, prec);
8206 return w;
8209 /* Subroutine of fold_binary. This routine performs all of the
8210 transformations that are common to the equality/inequality
8211 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8212 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8213 fold_binary should call fold_binary. Fold a comparison with
8214 tree code CODE and type TYPE with operands OP0 and OP1. Return
8215 the folded comparison or NULL_TREE. */
8217 static tree
8218 fold_comparison (location_t loc, enum tree_code code, tree type,
8219 tree op0, tree op1)
8221 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8222 tree arg0, arg1, tem;
8224 arg0 = op0;
8225 arg1 = op1;
8227 STRIP_SIGN_NOPS (arg0);
8228 STRIP_SIGN_NOPS (arg1);
8230 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 -+ C1. */
8231 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8232 && (equality_code
8233 || (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8234 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8235 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8236 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8237 && TREE_CODE (arg1) == INTEGER_CST
8238 && !TREE_OVERFLOW (arg1))
8240 const enum tree_code
8241 reverse_op = TREE_CODE (arg0) == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR;
8242 tree const1 = TREE_OPERAND (arg0, 1);
8243 tree const2 = fold_convert_loc (loc, TREE_TYPE (const1), arg1);
8244 tree variable = TREE_OPERAND (arg0, 0);
8245 tree new_const = int_const_binop (reverse_op, const2, const1);
8247 /* If the constant operation overflowed this can be
8248 simplified as a comparison against INT_MAX/INT_MIN. */
8249 if (TREE_OVERFLOW (new_const)
8250 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
8252 int const1_sgn = tree_int_cst_sgn (const1);
8253 enum tree_code code2 = code;
8255 /* Get the sign of the constant on the lhs if the
8256 operation were VARIABLE + CONST1. */
8257 if (TREE_CODE (arg0) == MINUS_EXPR)
8258 const1_sgn = -const1_sgn;
8260 /* The sign of the constant determines if we overflowed
8261 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8262 Canonicalize to the INT_MIN overflow by swapping the comparison
8263 if necessary. */
8264 if (const1_sgn == -1)
8265 code2 = swap_tree_comparison (code);
8267 /* We now can look at the canonicalized case
8268 VARIABLE + 1 CODE2 INT_MIN
8269 and decide on the result. */
8270 switch (code2)
8272 case EQ_EXPR:
8273 case LT_EXPR:
8274 case LE_EXPR:
8275 return
8276 omit_one_operand_loc (loc, type, boolean_false_node, variable);
8278 case NE_EXPR:
8279 case GE_EXPR:
8280 case GT_EXPR:
8281 return
8282 omit_one_operand_loc (loc, type, boolean_true_node, variable);
8284 default:
8285 gcc_unreachable ();
8288 else
8290 if (!equality_code)
8291 fold_overflow_warning ("assuming signed overflow does not occur "
8292 "when changing X +- C1 cmp C2 to "
8293 "X cmp C2 -+ C1",
8294 WARN_STRICT_OVERFLOW_COMPARISON);
8295 return fold_build2_loc (loc, code, type, variable, new_const);
8299 /* For comparisons of pointers we can decompose it to a compile time
8300 comparison of the base objects and the offsets into the object.
8301 This requires at least one operand being an ADDR_EXPR or a
8302 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8303 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8304 && (TREE_CODE (arg0) == ADDR_EXPR
8305 || TREE_CODE (arg1) == ADDR_EXPR
8306 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8307 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8309 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8310 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8311 machine_mode mode;
8312 int volatilep, unsignedp;
8313 bool indirect_base0 = false, indirect_base1 = false;
8315 /* Get base and offset for the access. Strip ADDR_EXPR for
8316 get_inner_reference, but put it back by stripping INDIRECT_REF
8317 off the base object if possible. indirect_baseN will be true
8318 if baseN is not an address but refers to the object itself. */
8319 base0 = arg0;
8320 if (TREE_CODE (arg0) == ADDR_EXPR)
8322 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8323 &bitsize, &bitpos0, &offset0, &mode,
8324 &unsignedp, &volatilep, false);
8325 if (TREE_CODE (base0) == INDIRECT_REF)
8326 base0 = TREE_OPERAND (base0, 0);
8327 else
8328 indirect_base0 = true;
8330 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8332 base0 = TREE_OPERAND (arg0, 0);
8333 STRIP_SIGN_NOPS (base0);
8334 if (TREE_CODE (base0) == ADDR_EXPR)
8336 base0 = TREE_OPERAND (base0, 0);
8337 indirect_base0 = true;
8339 offset0 = TREE_OPERAND (arg0, 1);
8340 if (tree_fits_shwi_p (offset0))
8342 HOST_WIDE_INT off = size_low_cst (offset0);
8343 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8344 * BITS_PER_UNIT)
8345 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8347 bitpos0 = off * BITS_PER_UNIT;
8348 offset0 = NULL_TREE;
8353 base1 = arg1;
8354 if (TREE_CODE (arg1) == ADDR_EXPR)
8356 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8357 &bitsize, &bitpos1, &offset1, &mode,
8358 &unsignedp, &volatilep, false);
8359 if (TREE_CODE (base1) == INDIRECT_REF)
8360 base1 = TREE_OPERAND (base1, 0);
8361 else
8362 indirect_base1 = true;
8364 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8366 base1 = TREE_OPERAND (arg1, 0);
8367 STRIP_SIGN_NOPS (base1);
8368 if (TREE_CODE (base1) == ADDR_EXPR)
8370 base1 = TREE_OPERAND (base1, 0);
8371 indirect_base1 = true;
8373 offset1 = TREE_OPERAND (arg1, 1);
8374 if (tree_fits_shwi_p (offset1))
8376 HOST_WIDE_INT off = size_low_cst (offset1);
8377 if ((HOST_WIDE_INT) (((unsigned HOST_WIDE_INT) off)
8378 * BITS_PER_UNIT)
8379 / BITS_PER_UNIT == (HOST_WIDE_INT) off)
8381 bitpos1 = off * BITS_PER_UNIT;
8382 offset1 = NULL_TREE;
8387 /* If we have equivalent bases we might be able to simplify. */
8388 if (indirect_base0 == indirect_base1
8389 && operand_equal_p (base0, base1, 0))
8391 /* We can fold this expression to a constant if the non-constant
8392 offset parts are equal. */
8393 if ((offset0 == offset1
8394 || (offset0 && offset1
8395 && operand_equal_p (offset0, offset1, 0)))
8396 && (code == EQ_EXPR
8397 || code == NE_EXPR
8398 || (indirect_base0 && DECL_P (base0))
8399 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8402 if (!equality_code
8403 && bitpos0 != bitpos1
8404 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8405 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8406 fold_overflow_warning (("assuming pointer wraparound does not "
8407 "occur when comparing P +- C1 with "
8408 "P +- C2"),
8409 WARN_STRICT_OVERFLOW_CONDITIONAL);
8411 switch (code)
8413 case EQ_EXPR:
8414 return constant_boolean_node (bitpos0 == bitpos1, type);
8415 case NE_EXPR:
8416 return constant_boolean_node (bitpos0 != bitpos1, type);
8417 case LT_EXPR:
8418 return constant_boolean_node (bitpos0 < bitpos1, type);
8419 case LE_EXPR:
8420 return constant_boolean_node (bitpos0 <= bitpos1, type);
8421 case GE_EXPR:
8422 return constant_boolean_node (bitpos0 >= bitpos1, type);
8423 case GT_EXPR:
8424 return constant_boolean_node (bitpos0 > bitpos1, type);
8425 default:;
8428 /* We can simplify the comparison to a comparison of the variable
8429 offset parts if the constant offset parts are equal.
8430 Be careful to use signed sizetype here because otherwise we
8431 mess with array offsets in the wrong way. This is possible
8432 because pointer arithmetic is restricted to retain within an
8433 object and overflow on pointer differences is undefined as of
8434 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8435 else if (bitpos0 == bitpos1
8436 && (equality_code
8437 || (indirect_base0 && DECL_P (base0))
8438 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8440 /* By converting to signed sizetype we cover middle-end pointer
8441 arithmetic which operates on unsigned pointer types of size
8442 type size and ARRAY_REF offsets which are properly sign or
8443 zero extended from their type in case it is narrower than
8444 sizetype. */
8445 if (offset0 == NULL_TREE)
8446 offset0 = build_int_cst (ssizetype, 0);
8447 else
8448 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8449 if (offset1 == NULL_TREE)
8450 offset1 = build_int_cst (ssizetype, 0);
8451 else
8452 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8454 if (!equality_code
8455 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8456 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8457 fold_overflow_warning (("assuming pointer wraparound does not "
8458 "occur when comparing P +- C1 with "
8459 "P +- C2"),
8460 WARN_STRICT_OVERFLOW_COMPARISON);
8462 return fold_build2_loc (loc, code, type, offset0, offset1);
8465 /* For equal offsets we can simplify to a comparison of the
8466 base addresses. */
8467 else if (bitpos0 == bitpos1
8468 && (indirect_base0
8469 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8470 && (indirect_base1
8471 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8472 && ((offset0 == offset1)
8473 || (offset0 && offset1
8474 && operand_equal_p (offset0, offset1, 0))))
8476 if (indirect_base0)
8477 base0 = build_fold_addr_expr_loc (loc, base0);
8478 if (indirect_base1)
8479 base1 = build_fold_addr_expr_loc (loc, base1);
8480 return fold_build2_loc (loc, code, type, base0, base1);
8484 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8485 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8486 the resulting offset is smaller in absolute value than the
8487 original one and has the same sign. */
8488 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8489 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8490 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8491 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8492 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8493 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8494 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8495 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8497 tree const1 = TREE_OPERAND (arg0, 1);
8498 tree const2 = TREE_OPERAND (arg1, 1);
8499 tree variable1 = TREE_OPERAND (arg0, 0);
8500 tree variable2 = TREE_OPERAND (arg1, 0);
8501 tree cst;
8502 const char * const warnmsg = G_("assuming signed overflow does not "
8503 "occur when combining constants around "
8504 "a comparison");
8506 /* Put the constant on the side where it doesn't overflow and is
8507 of lower absolute value and of same sign than before. */
8508 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8509 ? MINUS_EXPR : PLUS_EXPR,
8510 const2, const1);
8511 if (!TREE_OVERFLOW (cst)
8512 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8513 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8515 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8516 return fold_build2_loc (loc, code, type,
8517 variable1,
8518 fold_build2_loc (loc, TREE_CODE (arg1),
8519 TREE_TYPE (arg1),
8520 variable2, cst));
8523 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8524 ? MINUS_EXPR : PLUS_EXPR,
8525 const1, const2);
8526 if (!TREE_OVERFLOW (cst)
8527 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8528 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8530 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8531 return fold_build2_loc (loc, code, type,
8532 fold_build2_loc (loc, TREE_CODE (arg0),
8533 TREE_TYPE (arg0),
8534 variable1, cst),
8535 variable2);
8539 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8540 if (tem)
8541 return tem;
8543 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8544 constant, we can simplify it. */
8545 if (TREE_CODE (arg1) == INTEGER_CST
8546 && (TREE_CODE (arg0) == MIN_EXPR
8547 || TREE_CODE (arg0) == MAX_EXPR)
8548 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8550 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
8551 if (tem)
8552 return tem;
8555 /* If we are comparing an expression that just has comparisons
8556 of two integer values, arithmetic expressions of those comparisons,
8557 and constants, we can simplify it. There are only three cases
8558 to check: the two values can either be equal, the first can be
8559 greater, or the second can be greater. Fold the expression for
8560 those three values. Since each value must be 0 or 1, we have
8561 eight possibilities, each of which corresponds to the constant 0
8562 or 1 or one of the six possible comparisons.
8564 This handles common cases like (a > b) == 0 but also handles
8565 expressions like ((x > y) - (y > x)) > 0, which supposedly
8566 occur in macroized code. */
8568 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8570 tree cval1 = 0, cval2 = 0;
8571 int save_p = 0;
8573 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8574 /* Don't handle degenerate cases here; they should already
8575 have been handled anyway. */
8576 && cval1 != 0 && cval2 != 0
8577 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8578 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8579 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8580 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8581 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8582 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8583 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8585 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8586 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8588 /* We can't just pass T to eval_subst in case cval1 or cval2
8589 was the same as ARG1. */
8591 tree high_result
8592 = fold_build2_loc (loc, code, type,
8593 eval_subst (loc, arg0, cval1, maxval,
8594 cval2, minval),
8595 arg1);
8596 tree equal_result
8597 = fold_build2_loc (loc, code, type,
8598 eval_subst (loc, arg0, cval1, maxval,
8599 cval2, maxval),
8600 arg1);
8601 tree low_result
8602 = fold_build2_loc (loc, code, type,
8603 eval_subst (loc, arg0, cval1, minval,
8604 cval2, maxval),
8605 arg1);
8607 /* All three of these results should be 0 or 1. Confirm they are.
8608 Then use those values to select the proper code to use. */
8610 if (TREE_CODE (high_result) == INTEGER_CST
8611 && TREE_CODE (equal_result) == INTEGER_CST
8612 && TREE_CODE (low_result) == INTEGER_CST)
8614 /* Make a 3-bit mask with the high-order bit being the
8615 value for `>', the next for '=', and the low for '<'. */
8616 switch ((integer_onep (high_result) * 4)
8617 + (integer_onep (equal_result) * 2)
8618 + integer_onep (low_result))
8620 case 0:
8621 /* Always false. */
8622 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8623 case 1:
8624 code = LT_EXPR;
8625 break;
8626 case 2:
8627 code = EQ_EXPR;
8628 break;
8629 case 3:
8630 code = LE_EXPR;
8631 break;
8632 case 4:
8633 code = GT_EXPR;
8634 break;
8635 case 5:
8636 code = NE_EXPR;
8637 break;
8638 case 6:
8639 code = GE_EXPR;
8640 break;
8641 case 7:
8642 /* Always true. */
8643 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8646 if (save_p)
8648 tem = save_expr (build2 (code, type, cval1, cval2));
8649 SET_EXPR_LOCATION (tem, loc);
8650 return tem;
8652 return fold_build2_loc (loc, code, type, cval1, cval2);
8657 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8658 into a single range test. */
8659 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
8660 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
8661 && TREE_CODE (arg1) == INTEGER_CST
8662 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8663 && !integer_zerop (TREE_OPERAND (arg0, 1))
8664 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8665 && !TREE_OVERFLOW (arg1))
8667 tem = fold_div_compare (loc, code, type, arg0, arg1);
8668 if (tem != NULL_TREE)
8669 return tem;
8672 return NULL_TREE;
8676 /* Subroutine of fold_binary. Optimize complex multiplications of the
8677 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8678 argument EXPR represents the expression "z" of type TYPE. */
8680 static tree
8681 fold_mult_zconjz (location_t loc, tree type, tree expr)
8683 tree itype = TREE_TYPE (type);
8684 tree rpart, ipart, tem;
8686 if (TREE_CODE (expr) == COMPLEX_EXPR)
8688 rpart = TREE_OPERAND (expr, 0);
8689 ipart = TREE_OPERAND (expr, 1);
8691 else if (TREE_CODE (expr) == COMPLEX_CST)
8693 rpart = TREE_REALPART (expr);
8694 ipart = TREE_IMAGPART (expr);
8696 else
8698 expr = save_expr (expr);
8699 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8700 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8703 rpart = save_expr (rpart);
8704 ipart = save_expr (ipart);
8705 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8706 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8707 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8708 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8709 build_zero_cst (itype));
8713 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8714 CONSTRUCTOR ARG into array ELTS and return true if successful. */
8716 static bool
8717 vec_cst_ctor_to_array (tree arg, tree *elts)
8719 unsigned int nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)), i;
8721 if (TREE_CODE (arg) == VECTOR_CST)
8723 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8724 elts[i] = VECTOR_CST_ELT (arg, i);
8726 else if (TREE_CODE (arg) == CONSTRUCTOR)
8728 constructor_elt *elt;
8730 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8731 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8732 return false;
8733 else
8734 elts[i] = elt->value;
8736 else
8737 return false;
8738 for (; i < nelts; i++)
8739 elts[i]
8740 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8741 return true;
8744 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8745 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8746 NULL_TREE otherwise. */
8748 static tree
8749 fold_vec_perm (tree type, tree arg0, tree arg1, const unsigned char *sel)
8751 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
8752 tree *elts;
8753 bool need_ctor = false;
8755 gcc_assert (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8756 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8757 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8758 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8759 return NULL_TREE;
8761 elts = XALLOCAVEC (tree, nelts * 3);
8762 if (!vec_cst_ctor_to_array (arg0, elts)
8763 || !vec_cst_ctor_to_array (arg1, elts + nelts))
8764 return NULL_TREE;
8766 for (i = 0; i < nelts; i++)
8768 if (!CONSTANT_CLASS_P (elts[sel[i]]))
8769 need_ctor = true;
8770 elts[i + 2 * nelts] = unshare_expr (elts[sel[i]]);
8773 if (need_ctor)
8775 vec<constructor_elt, va_gc> *v;
8776 vec_alloc (v, nelts);
8777 for (i = 0; i < nelts; i++)
8778 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, elts[2 * nelts + i]);
8779 return build_constructor (type, v);
8781 else
8782 return build_vector (type, &elts[2 * nelts]);
8785 /* Try to fold a pointer difference of type TYPE two address expressions of
8786 array references AREF0 and AREF1 using location LOC. Return a
8787 simplified expression for the difference or NULL_TREE. */
8789 static tree
8790 fold_addr_of_array_ref_difference (location_t loc, tree type,
8791 tree aref0, tree aref1)
8793 tree base0 = TREE_OPERAND (aref0, 0);
8794 tree base1 = TREE_OPERAND (aref1, 0);
8795 tree base_offset = build_int_cst (type, 0);
8797 /* If the bases are array references as well, recurse. If the bases
8798 are pointer indirections compute the difference of the pointers.
8799 If the bases are equal, we are set. */
8800 if ((TREE_CODE (base0) == ARRAY_REF
8801 && TREE_CODE (base1) == ARRAY_REF
8802 && (base_offset
8803 = fold_addr_of_array_ref_difference (loc, type, base0, base1)))
8804 || (INDIRECT_REF_P (base0)
8805 && INDIRECT_REF_P (base1)
8806 && (base_offset = fold_binary_loc (loc, MINUS_EXPR, type,
8807 TREE_OPERAND (base0, 0),
8808 TREE_OPERAND (base1, 0))))
8809 || operand_equal_p (base0, base1, 0))
8811 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
8812 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
8813 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
8814 tree diff = build2 (MINUS_EXPR, type, op0, op1);
8815 return fold_build2_loc (loc, PLUS_EXPR, type,
8816 base_offset,
8817 fold_build2_loc (loc, MULT_EXPR, type,
8818 diff, esz));
8820 return NULL_TREE;
8823 /* If the real or vector real constant CST of type TYPE has an exact
8824 inverse, return it, else return NULL. */
8826 tree
8827 exact_inverse (tree type, tree cst)
8829 REAL_VALUE_TYPE r;
8830 tree unit_type, *elts;
8831 machine_mode mode;
8832 unsigned vec_nelts, i;
8834 switch (TREE_CODE (cst))
8836 case REAL_CST:
8837 r = TREE_REAL_CST (cst);
8839 if (exact_real_inverse (TYPE_MODE (type), &r))
8840 return build_real (type, r);
8842 return NULL_TREE;
8844 case VECTOR_CST:
8845 vec_nelts = VECTOR_CST_NELTS (cst);
8846 elts = XALLOCAVEC (tree, vec_nelts);
8847 unit_type = TREE_TYPE (type);
8848 mode = TYPE_MODE (unit_type);
8850 for (i = 0; i < vec_nelts; i++)
8852 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
8853 if (!exact_real_inverse (mode, &r))
8854 return NULL_TREE;
8855 elts[i] = build_real (unit_type, r);
8858 return build_vector (type, elts);
8860 default:
8861 return NULL_TREE;
8865 /* Mask out the tz least significant bits of X of type TYPE where
8866 tz is the number of trailing zeroes in Y. */
8867 static wide_int
8868 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
8870 int tz = wi::ctz (y);
8871 if (tz > 0)
8872 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
8873 return x;
8876 /* Return true when T is an address and is known to be nonzero.
8877 For floating point we further ensure that T is not denormal.
8878 Similar logic is present in nonzero_address in rtlanal.h.
8880 If the return value is based on the assumption that signed overflow
8881 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
8882 change *STRICT_OVERFLOW_P. */
8884 static bool
8885 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
8887 tree type = TREE_TYPE (t);
8888 enum tree_code code;
8890 /* Doing something useful for floating point would need more work. */
8891 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
8892 return false;
8894 code = TREE_CODE (t);
8895 switch (TREE_CODE_CLASS (code))
8897 case tcc_unary:
8898 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8899 strict_overflow_p);
8900 case tcc_binary:
8901 case tcc_comparison:
8902 return tree_binary_nonzero_warnv_p (code, type,
8903 TREE_OPERAND (t, 0),
8904 TREE_OPERAND (t, 1),
8905 strict_overflow_p);
8906 case tcc_constant:
8907 case tcc_declaration:
8908 case tcc_reference:
8909 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8911 default:
8912 break;
8915 switch (code)
8917 case TRUTH_NOT_EXPR:
8918 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
8919 strict_overflow_p);
8921 case TRUTH_AND_EXPR:
8922 case TRUTH_OR_EXPR:
8923 case TRUTH_XOR_EXPR:
8924 return tree_binary_nonzero_warnv_p (code, type,
8925 TREE_OPERAND (t, 0),
8926 TREE_OPERAND (t, 1),
8927 strict_overflow_p);
8929 case COND_EXPR:
8930 case CONSTRUCTOR:
8931 case OBJ_TYPE_REF:
8932 case ASSERT_EXPR:
8933 case ADDR_EXPR:
8934 case WITH_SIZE_EXPR:
8935 case SSA_NAME:
8936 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
8938 case COMPOUND_EXPR:
8939 case MODIFY_EXPR:
8940 case BIND_EXPR:
8941 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
8942 strict_overflow_p);
8944 case SAVE_EXPR:
8945 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
8946 strict_overflow_p);
8948 case CALL_EXPR:
8950 tree fndecl = get_callee_fndecl (t);
8951 if (!fndecl) return false;
8952 if (flag_delete_null_pointer_checks && !flag_check_new
8953 && DECL_IS_OPERATOR_NEW (fndecl)
8954 && !TREE_NOTHROW (fndecl))
8955 return true;
8956 if (flag_delete_null_pointer_checks
8957 && lookup_attribute ("returns_nonnull",
8958 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
8959 return true;
8960 return alloca_call_p (t);
8963 default:
8964 break;
8966 return false;
8969 /* Return true when T is an address and is known to be nonzero.
8970 Handle warnings about undefined signed overflow. */
8972 static bool
8973 tree_expr_nonzero_p (tree t)
8975 bool ret, strict_overflow_p;
8977 strict_overflow_p = false;
8978 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
8979 if (strict_overflow_p)
8980 fold_overflow_warning (("assuming signed overflow does not occur when "
8981 "determining that expression is always "
8982 "non-zero"),
8983 WARN_STRICT_OVERFLOW_MISC);
8984 return ret;
8987 /* Fold a binary expression of code CODE and type TYPE with operands
8988 OP0 and OP1. LOC is the location of the resulting expression.
8989 Return the folded expression if folding is successful. Otherwise,
8990 return NULL_TREE. */
8992 tree
8993 fold_binary_loc (location_t loc,
8994 enum tree_code code, tree type, tree op0, tree op1)
8996 enum tree_code_class kind = TREE_CODE_CLASS (code);
8997 tree arg0, arg1, tem;
8998 tree t1 = NULL_TREE;
8999 bool strict_overflow_p;
9000 unsigned int prec;
9002 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9003 && TREE_CODE_LENGTH (code) == 2
9004 && op0 != NULL_TREE
9005 && op1 != NULL_TREE);
9007 arg0 = op0;
9008 arg1 = op1;
9010 /* Strip any conversions that don't change the mode. This is
9011 safe for every expression, except for a comparison expression
9012 because its signedness is derived from its operands. So, in
9013 the latter case, only strip conversions that don't change the
9014 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9015 preserved.
9017 Note that this is done as an internal manipulation within the
9018 constant folder, in order to find the simplest representation
9019 of the arguments so that their form can be studied. In any
9020 cases, the appropriate type conversions should be put back in
9021 the tree that will get out of the constant folder. */
9023 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9025 STRIP_SIGN_NOPS (arg0);
9026 STRIP_SIGN_NOPS (arg1);
9028 else
9030 STRIP_NOPS (arg0);
9031 STRIP_NOPS (arg1);
9034 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9035 constant but we can't do arithmetic on them. */
9036 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9038 tem = const_binop (code, type, arg0, arg1);
9039 if (tem != NULL_TREE)
9041 if (TREE_TYPE (tem) != type)
9042 tem = fold_convert_loc (loc, type, tem);
9043 return tem;
9047 /* If this is a commutative operation, and ARG0 is a constant, move it
9048 to ARG1 to reduce the number of tests below. */
9049 if (commutative_tree_code (code)
9050 && tree_swap_operands_p (arg0, arg1, true))
9051 return fold_build2_loc (loc, code, type, op1, op0);
9053 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9054 to ARG1 to reduce the number of tests below. */
9055 if (kind == tcc_comparison
9056 && tree_swap_operands_p (arg0, arg1, true))
9057 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9059 tem = generic_simplify (loc, code, type, op0, op1);
9060 if (tem)
9061 return tem;
9063 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9065 First check for cases where an arithmetic operation is applied to a
9066 compound, conditional, or comparison operation. Push the arithmetic
9067 operation inside the compound or conditional to see if any folding
9068 can then be done. Convert comparison to conditional for this purpose.
9069 The also optimizes non-constant cases that used to be done in
9070 expand_expr.
9072 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9073 one of the operands is a comparison and the other is a comparison, a
9074 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9075 code below would make the expression more complex. Change it to a
9076 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9077 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9079 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9080 || code == EQ_EXPR || code == NE_EXPR)
9081 && TREE_CODE (type) != VECTOR_TYPE
9082 && ((truth_value_p (TREE_CODE (arg0))
9083 && (truth_value_p (TREE_CODE (arg1))
9084 || (TREE_CODE (arg1) == BIT_AND_EXPR
9085 && integer_onep (TREE_OPERAND (arg1, 1)))))
9086 || (truth_value_p (TREE_CODE (arg1))
9087 && (truth_value_p (TREE_CODE (arg0))
9088 || (TREE_CODE (arg0) == BIT_AND_EXPR
9089 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9091 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9092 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9093 : TRUTH_XOR_EXPR,
9094 boolean_type_node,
9095 fold_convert_loc (loc, boolean_type_node, arg0),
9096 fold_convert_loc (loc, boolean_type_node, arg1));
9098 if (code == EQ_EXPR)
9099 tem = invert_truthvalue_loc (loc, tem);
9101 return fold_convert_loc (loc, type, tem);
9104 if (TREE_CODE_CLASS (code) == tcc_binary
9105 || TREE_CODE_CLASS (code) == tcc_comparison)
9107 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9109 tem = fold_build2_loc (loc, code, type,
9110 fold_convert_loc (loc, TREE_TYPE (op0),
9111 TREE_OPERAND (arg0, 1)), op1);
9112 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9113 tem);
9115 if (TREE_CODE (arg1) == COMPOUND_EXPR
9116 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9118 tem = fold_build2_loc (loc, code, type, op0,
9119 fold_convert_loc (loc, TREE_TYPE (op1),
9120 TREE_OPERAND (arg1, 1)));
9121 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9122 tem);
9125 if (TREE_CODE (arg0) == COND_EXPR
9126 || TREE_CODE (arg0) == VEC_COND_EXPR
9127 || COMPARISON_CLASS_P (arg0))
9129 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9130 arg0, arg1,
9131 /*cond_first_p=*/1);
9132 if (tem != NULL_TREE)
9133 return tem;
9136 if (TREE_CODE (arg1) == COND_EXPR
9137 || TREE_CODE (arg1) == VEC_COND_EXPR
9138 || COMPARISON_CLASS_P (arg1))
9140 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9141 arg1, arg0,
9142 /*cond_first_p=*/0);
9143 if (tem != NULL_TREE)
9144 return tem;
9148 switch (code)
9150 case MEM_REF:
9151 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9152 if (TREE_CODE (arg0) == ADDR_EXPR
9153 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9155 tree iref = TREE_OPERAND (arg0, 0);
9156 return fold_build2 (MEM_REF, type,
9157 TREE_OPERAND (iref, 0),
9158 int_const_binop (PLUS_EXPR, arg1,
9159 TREE_OPERAND (iref, 1)));
9162 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9163 if (TREE_CODE (arg0) == ADDR_EXPR
9164 && handled_component_p (TREE_OPERAND (arg0, 0)))
9166 tree base;
9167 HOST_WIDE_INT coffset;
9168 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9169 &coffset);
9170 if (!base)
9171 return NULL_TREE;
9172 return fold_build2 (MEM_REF, type,
9173 build_fold_addr_expr (base),
9174 int_const_binop (PLUS_EXPR, arg1,
9175 size_int (coffset)));
9178 return NULL_TREE;
9180 case POINTER_PLUS_EXPR:
9181 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9182 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9183 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9184 return fold_convert_loc (loc, type,
9185 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9186 fold_convert_loc (loc, sizetype,
9187 arg1),
9188 fold_convert_loc (loc, sizetype,
9189 arg0)));
9191 return NULL_TREE;
9193 case PLUS_EXPR:
9194 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9196 /* X + (X / CST) * -CST is X % CST. */
9197 if (TREE_CODE (arg1) == MULT_EXPR
9198 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9199 && operand_equal_p (arg0,
9200 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9202 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9203 tree cst1 = TREE_OPERAND (arg1, 1);
9204 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9205 cst1, cst0);
9206 if (sum && integer_zerop (sum))
9207 return fold_convert_loc (loc, type,
9208 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9209 TREE_TYPE (arg0), arg0,
9210 cst0));
9214 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9215 one. Make sure the type is not saturating and has the signedness of
9216 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9217 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9218 if ((TREE_CODE (arg0) == MULT_EXPR
9219 || TREE_CODE (arg1) == MULT_EXPR)
9220 && !TYPE_SATURATING (type)
9221 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9222 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9223 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9225 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9226 if (tem)
9227 return tem;
9230 if (! FLOAT_TYPE_P (type))
9232 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9233 (plus (plus (mult) (mult)) (foo)) so that we can
9234 take advantage of the factoring cases below. */
9235 if (ANY_INTEGRAL_TYPE_P (type)
9236 && TYPE_OVERFLOW_WRAPS (type)
9237 && (((TREE_CODE (arg0) == PLUS_EXPR
9238 || TREE_CODE (arg0) == MINUS_EXPR)
9239 && TREE_CODE (arg1) == MULT_EXPR)
9240 || ((TREE_CODE (arg1) == PLUS_EXPR
9241 || TREE_CODE (arg1) == MINUS_EXPR)
9242 && TREE_CODE (arg0) == MULT_EXPR)))
9244 tree parg0, parg1, parg, marg;
9245 enum tree_code pcode;
9247 if (TREE_CODE (arg1) == MULT_EXPR)
9248 parg = arg0, marg = arg1;
9249 else
9250 parg = arg1, marg = arg0;
9251 pcode = TREE_CODE (parg);
9252 parg0 = TREE_OPERAND (parg, 0);
9253 parg1 = TREE_OPERAND (parg, 1);
9254 STRIP_NOPS (parg0);
9255 STRIP_NOPS (parg1);
9257 if (TREE_CODE (parg0) == MULT_EXPR
9258 && TREE_CODE (parg1) != MULT_EXPR)
9259 return fold_build2_loc (loc, pcode, type,
9260 fold_build2_loc (loc, PLUS_EXPR, type,
9261 fold_convert_loc (loc, type,
9262 parg0),
9263 fold_convert_loc (loc, type,
9264 marg)),
9265 fold_convert_loc (loc, type, parg1));
9266 if (TREE_CODE (parg0) != MULT_EXPR
9267 && TREE_CODE (parg1) == MULT_EXPR)
9268 return
9269 fold_build2_loc (loc, PLUS_EXPR, type,
9270 fold_convert_loc (loc, type, parg0),
9271 fold_build2_loc (loc, pcode, type,
9272 fold_convert_loc (loc, type, marg),
9273 fold_convert_loc (loc, type,
9274 parg1)));
9277 else
9279 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9280 to __complex__ ( x, y ). This is not the same for SNaNs or
9281 if signed zeros are involved. */
9282 if (!HONOR_SNANS (element_mode (arg0))
9283 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9284 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9286 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9287 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9288 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9289 bool arg0rz = false, arg0iz = false;
9290 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9291 || (arg0i && (arg0iz = real_zerop (arg0i))))
9293 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9294 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9295 if (arg0rz && arg1i && real_zerop (arg1i))
9297 tree rp = arg1r ? arg1r
9298 : build1 (REALPART_EXPR, rtype, arg1);
9299 tree ip = arg0i ? arg0i
9300 : build1 (IMAGPART_EXPR, rtype, arg0);
9301 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9303 else if (arg0iz && arg1r && real_zerop (arg1r))
9305 tree rp = arg0r ? arg0r
9306 : build1 (REALPART_EXPR, rtype, arg0);
9307 tree ip = arg1i ? arg1i
9308 : build1 (IMAGPART_EXPR, rtype, arg1);
9309 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9314 if (flag_unsafe_math_optimizations
9315 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9316 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9317 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9318 return tem;
9320 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9321 We associate floats only if the user has specified
9322 -fassociative-math. */
9323 if (flag_associative_math
9324 && TREE_CODE (arg1) == PLUS_EXPR
9325 && TREE_CODE (arg0) != MULT_EXPR)
9327 tree tree10 = TREE_OPERAND (arg1, 0);
9328 tree tree11 = TREE_OPERAND (arg1, 1);
9329 if (TREE_CODE (tree11) == MULT_EXPR
9330 && TREE_CODE (tree10) == MULT_EXPR)
9332 tree tree0;
9333 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9334 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9337 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9338 We associate floats only if the user has specified
9339 -fassociative-math. */
9340 if (flag_associative_math
9341 && TREE_CODE (arg0) == PLUS_EXPR
9342 && TREE_CODE (arg1) != MULT_EXPR)
9344 tree tree00 = TREE_OPERAND (arg0, 0);
9345 tree tree01 = TREE_OPERAND (arg0, 1);
9346 if (TREE_CODE (tree01) == MULT_EXPR
9347 && TREE_CODE (tree00) == MULT_EXPR)
9349 tree tree0;
9350 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9351 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9356 bit_rotate:
9357 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9358 is a rotate of A by C1 bits. */
9359 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9360 is a rotate of A by B bits. */
9362 enum tree_code code0, code1;
9363 tree rtype;
9364 code0 = TREE_CODE (arg0);
9365 code1 = TREE_CODE (arg1);
9366 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9367 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9368 && operand_equal_p (TREE_OPERAND (arg0, 0),
9369 TREE_OPERAND (arg1, 0), 0)
9370 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9371 TYPE_UNSIGNED (rtype))
9372 /* Only create rotates in complete modes. Other cases are not
9373 expanded properly. */
9374 && (element_precision (rtype)
9375 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9377 tree tree01, tree11;
9378 enum tree_code code01, code11;
9380 tree01 = TREE_OPERAND (arg0, 1);
9381 tree11 = TREE_OPERAND (arg1, 1);
9382 STRIP_NOPS (tree01);
9383 STRIP_NOPS (tree11);
9384 code01 = TREE_CODE (tree01);
9385 code11 = TREE_CODE (tree11);
9386 if (code01 == INTEGER_CST
9387 && code11 == INTEGER_CST
9388 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9389 == element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9391 tem = build2_loc (loc, LROTATE_EXPR,
9392 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9393 TREE_OPERAND (arg0, 0),
9394 code0 == LSHIFT_EXPR
9395 ? TREE_OPERAND (arg0, 1)
9396 : TREE_OPERAND (arg1, 1));
9397 return fold_convert_loc (loc, type, tem);
9399 else if (code11 == MINUS_EXPR)
9401 tree tree110, tree111;
9402 tree110 = TREE_OPERAND (tree11, 0);
9403 tree111 = TREE_OPERAND (tree11, 1);
9404 STRIP_NOPS (tree110);
9405 STRIP_NOPS (tree111);
9406 if (TREE_CODE (tree110) == INTEGER_CST
9407 && 0 == compare_tree_int (tree110,
9408 element_precision
9409 (TREE_TYPE (TREE_OPERAND
9410 (arg0, 0))))
9411 && operand_equal_p (tree01, tree111, 0))
9412 return
9413 fold_convert_loc (loc, type,
9414 build2 ((code0 == LSHIFT_EXPR
9415 ? LROTATE_EXPR
9416 : RROTATE_EXPR),
9417 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9418 TREE_OPERAND (arg0, 0),
9419 TREE_OPERAND (arg0, 1)));
9421 else if (code01 == MINUS_EXPR)
9423 tree tree010, tree011;
9424 tree010 = TREE_OPERAND (tree01, 0);
9425 tree011 = TREE_OPERAND (tree01, 1);
9426 STRIP_NOPS (tree010);
9427 STRIP_NOPS (tree011);
9428 if (TREE_CODE (tree010) == INTEGER_CST
9429 && 0 == compare_tree_int (tree010,
9430 element_precision
9431 (TREE_TYPE (TREE_OPERAND
9432 (arg0, 0))))
9433 && operand_equal_p (tree11, tree011, 0))
9434 return fold_convert_loc
9435 (loc, type,
9436 build2 ((code0 != LSHIFT_EXPR
9437 ? LROTATE_EXPR
9438 : RROTATE_EXPR),
9439 TREE_TYPE (TREE_OPERAND (arg0, 0)),
9440 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1)));
9445 associate:
9446 /* In most languages, can't associate operations on floats through
9447 parentheses. Rather than remember where the parentheses were, we
9448 don't associate floats at all, unless the user has specified
9449 -fassociative-math.
9450 And, we need to make sure type is not saturating. */
9452 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9453 && !TYPE_SATURATING (type))
9455 tree var0, con0, lit0, minus_lit0;
9456 tree var1, con1, lit1, minus_lit1;
9457 tree atype = type;
9458 bool ok = true;
9460 /* Split both trees into variables, constants, and literals. Then
9461 associate each group together, the constants with literals,
9462 then the result with variables. This increases the chances of
9463 literals being recombined later and of generating relocatable
9464 expressions for the sum of a constant and literal. */
9465 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9466 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9467 code == MINUS_EXPR);
9469 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9470 if (code == MINUS_EXPR)
9471 code = PLUS_EXPR;
9473 /* With undefined overflow prefer doing association in a type
9474 which wraps on overflow, if that is one of the operand types. */
9475 if ((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9476 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9478 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9479 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9480 atype = TREE_TYPE (arg0);
9481 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9482 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9483 atype = TREE_TYPE (arg1);
9484 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9487 /* With undefined overflow we can only associate constants with one
9488 variable, and constants whose association doesn't overflow. */
9489 if ((POINTER_TYPE_P (atype) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9490 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9492 if (var0 && var1)
9494 tree tmp0 = var0;
9495 tree tmp1 = var1;
9496 bool one_neg = false;
9498 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9500 tmp0 = TREE_OPERAND (tmp0, 0);
9501 one_neg = !one_neg;
9503 if (CONVERT_EXPR_P (tmp0)
9504 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9505 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9506 <= TYPE_PRECISION (atype)))
9507 tmp0 = TREE_OPERAND (tmp0, 0);
9508 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9510 tmp1 = TREE_OPERAND (tmp1, 0);
9511 one_neg = !one_neg;
9513 if (CONVERT_EXPR_P (tmp1)
9514 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9515 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9516 <= TYPE_PRECISION (atype)))
9517 tmp1 = TREE_OPERAND (tmp1, 0);
9518 /* The only case we can still associate with two variables
9519 is if they cancel out. */
9520 if (!one_neg
9521 || !operand_equal_p (tmp0, tmp1, 0))
9522 ok = false;
9526 /* Only do something if we found more than two objects. Otherwise,
9527 nothing has changed and we risk infinite recursion. */
9528 if (ok
9529 && (2 < ((var0 != 0) + (var1 != 0)
9530 + (con0 != 0) + (con1 != 0)
9531 + (lit0 != 0) + (lit1 != 0)
9532 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9534 bool any_overflows = false;
9535 if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
9536 if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
9537 if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
9538 if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
9539 var0 = associate_trees (loc, var0, var1, code, atype);
9540 con0 = associate_trees (loc, con0, con1, code, atype);
9541 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9542 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9543 code, atype);
9545 /* Preserve the MINUS_EXPR if the negative part of the literal is
9546 greater than the positive part. Otherwise, the multiplicative
9547 folding code (i.e extract_muldiv) may be fooled in case
9548 unsigned constants are subtracted, like in the following
9549 example: ((X*2 + 4) - 8U)/2. */
9550 if (minus_lit0 && lit0)
9552 if (TREE_CODE (lit0) == INTEGER_CST
9553 && TREE_CODE (minus_lit0) == INTEGER_CST
9554 && tree_int_cst_lt (lit0, minus_lit0))
9556 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9557 MINUS_EXPR, atype);
9558 lit0 = 0;
9560 else
9562 lit0 = associate_trees (loc, lit0, minus_lit0,
9563 MINUS_EXPR, atype);
9564 minus_lit0 = 0;
9568 /* Don't introduce overflows through reassociation. */
9569 if (!any_overflows
9570 && ((lit0 && TREE_OVERFLOW_P (lit0))
9571 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
9572 return NULL_TREE;
9574 if (minus_lit0)
9576 if (con0 == 0)
9577 return
9578 fold_convert_loc (loc, type,
9579 associate_trees (loc, var0, minus_lit0,
9580 MINUS_EXPR, atype));
9581 else
9583 con0 = associate_trees (loc, con0, minus_lit0,
9584 MINUS_EXPR, atype);
9585 return
9586 fold_convert_loc (loc, type,
9587 associate_trees (loc, var0, con0,
9588 PLUS_EXPR, atype));
9592 con0 = associate_trees (loc, con0, lit0, code, atype);
9593 return
9594 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9595 code, atype));
9599 return NULL_TREE;
9601 case MINUS_EXPR:
9602 /* Pointer simplifications for subtraction, simple reassociations. */
9603 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
9605 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
9606 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
9607 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9609 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9610 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
9611 tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
9612 tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
9613 return fold_build2_loc (loc, PLUS_EXPR, type,
9614 fold_build2_loc (loc, MINUS_EXPR, type,
9615 arg00, arg10),
9616 fold_build2_loc (loc, MINUS_EXPR, type,
9617 arg01, arg11));
9619 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
9620 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9622 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9623 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
9624 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
9625 fold_convert_loc (loc, type, arg1));
9626 if (tmp)
9627 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
9629 /* PTR0 - (PTR1 p+ A) -> (PTR0 - PTR1) - A, assuming PTR0 - PTR1
9630 simplifies. */
9631 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9633 tree arg10 = fold_convert_loc (loc, type,
9634 TREE_OPERAND (arg1, 0));
9635 tree arg11 = fold_convert_loc (loc, type,
9636 TREE_OPERAND (arg1, 1));
9637 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type,
9638 fold_convert_loc (loc, type, arg0),
9639 arg10);
9640 if (tmp)
9641 return fold_build2_loc (loc, MINUS_EXPR, type, tmp, arg11);
9644 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9645 if (TREE_CODE (arg0) == NEGATE_EXPR
9646 && negate_expr_p (arg1)
9647 && reorder_operands_p (arg0, arg1))
9648 return fold_build2_loc (loc, MINUS_EXPR, type,
9649 fold_convert_loc (loc, type,
9650 negate_expr (arg1)),
9651 fold_convert_loc (loc, type,
9652 TREE_OPERAND (arg0, 0)));
9654 if (! FLOAT_TYPE_P (type))
9656 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
9657 any power of 2 minus 1. */
9658 if (TREE_CODE (arg0) == BIT_AND_EXPR
9659 && TREE_CODE (arg1) == BIT_AND_EXPR
9660 && operand_equal_p (TREE_OPERAND (arg0, 0),
9661 TREE_OPERAND (arg1, 0), 0))
9663 tree mask0 = TREE_OPERAND (arg0, 1);
9664 tree mask1 = TREE_OPERAND (arg1, 1);
9665 tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
9667 if (operand_equal_p (tem, mask1, 0))
9669 tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
9670 TREE_OPERAND (arg0, 0), mask1);
9671 return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
9676 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9677 __complex__ ( x, -y ). This is not the same for SNaNs or if
9678 signed zeros are involved. */
9679 if (!HONOR_SNANS (element_mode (arg0))
9680 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9681 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9683 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9684 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9685 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9686 bool arg0rz = false, arg0iz = false;
9687 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9688 || (arg0i && (arg0iz = real_zerop (arg0i))))
9690 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9691 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9692 if (arg0rz && arg1i && real_zerop (arg1i))
9694 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9695 arg1r ? arg1r
9696 : build1 (REALPART_EXPR, rtype, arg1));
9697 tree ip = arg0i ? arg0i
9698 : build1 (IMAGPART_EXPR, rtype, arg0);
9699 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9701 else if (arg0iz && arg1r && real_zerop (arg1r))
9703 tree rp = arg0r ? arg0r
9704 : build1 (REALPART_EXPR, rtype, arg0);
9705 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9706 arg1i ? arg1i
9707 : build1 (IMAGPART_EXPR, rtype, arg1));
9708 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9713 /* A - B -> A + (-B) if B is easily negatable. */
9714 if (negate_expr_p (arg1)
9715 && !TYPE_OVERFLOW_SANITIZED (type)
9716 && ((FLOAT_TYPE_P (type)
9717 /* Avoid this transformation if B is a positive REAL_CST. */
9718 && (TREE_CODE (arg1) != REAL_CST
9719 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
9720 || INTEGRAL_TYPE_P (type)))
9721 return fold_build2_loc (loc, PLUS_EXPR, type,
9722 fold_convert_loc (loc, type, arg0),
9723 fold_convert_loc (loc, type,
9724 negate_expr (arg1)));
9726 /* Fold &a[i] - &a[j] to i-j. */
9727 if (TREE_CODE (arg0) == ADDR_EXPR
9728 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9729 && TREE_CODE (arg1) == ADDR_EXPR
9730 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9732 tree tem = fold_addr_of_array_ref_difference (loc, type,
9733 TREE_OPERAND (arg0, 0),
9734 TREE_OPERAND (arg1, 0));
9735 if (tem)
9736 return tem;
9739 if (FLOAT_TYPE_P (type)
9740 && flag_unsafe_math_optimizations
9741 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9742 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9743 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
9744 return tem;
9746 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9747 one. Make sure the type is not saturating and has the signedness of
9748 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9749 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9750 if ((TREE_CODE (arg0) == MULT_EXPR
9751 || TREE_CODE (arg1) == MULT_EXPR)
9752 && !TYPE_SATURATING (type)
9753 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9754 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9755 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9757 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9758 if (tem)
9759 return tem;
9762 goto associate;
9764 case MULT_EXPR:
9765 /* (-A) * (-B) -> A * B */
9766 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
9767 return fold_build2_loc (loc, MULT_EXPR, type,
9768 fold_convert_loc (loc, type,
9769 TREE_OPERAND (arg0, 0)),
9770 fold_convert_loc (loc, type,
9771 negate_expr (arg1)));
9772 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
9773 return fold_build2_loc (loc, MULT_EXPR, type,
9774 fold_convert_loc (loc, type,
9775 negate_expr (arg0)),
9776 fold_convert_loc (loc, type,
9777 TREE_OPERAND (arg1, 0)));
9779 if (! FLOAT_TYPE_P (type))
9781 /* Transform x * -C into -x * C if x is easily negatable. */
9782 if (TREE_CODE (arg1) == INTEGER_CST
9783 && tree_int_cst_sgn (arg1) == -1
9784 && negate_expr_p (arg0)
9785 && (tem = negate_expr (arg1)) != arg1
9786 && !TREE_OVERFLOW (tem))
9787 return fold_build2_loc (loc, MULT_EXPR, type,
9788 fold_convert_loc (loc, type,
9789 negate_expr (arg0)),
9790 tem);
9792 /* (a * (1 << b)) is (a << b) */
9793 if (TREE_CODE (arg1) == LSHIFT_EXPR
9794 && integer_onep (TREE_OPERAND (arg1, 0)))
9795 return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
9796 TREE_OPERAND (arg1, 1));
9797 if (TREE_CODE (arg0) == LSHIFT_EXPR
9798 && integer_onep (TREE_OPERAND (arg0, 0)))
9799 return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
9800 TREE_OPERAND (arg0, 1));
9802 /* (A + A) * C -> A * 2 * C */
9803 if (TREE_CODE (arg0) == PLUS_EXPR
9804 && TREE_CODE (arg1) == INTEGER_CST
9805 && operand_equal_p (TREE_OPERAND (arg0, 0),
9806 TREE_OPERAND (arg0, 1), 0))
9807 return fold_build2_loc (loc, MULT_EXPR, type,
9808 omit_one_operand_loc (loc, type,
9809 TREE_OPERAND (arg0, 0),
9810 TREE_OPERAND (arg0, 1)),
9811 fold_build2_loc (loc, MULT_EXPR, type,
9812 build_int_cst (type, 2) , arg1));
9814 /* ((T) (X /[ex] C)) * C cancels out if the conversion is
9815 sign-changing only. */
9816 if (TREE_CODE (arg1) == INTEGER_CST
9817 && TREE_CODE (arg0) == EXACT_DIV_EXPR
9818 && operand_equal_p (arg1, TREE_OPERAND (arg0, 1), 0))
9819 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9821 strict_overflow_p = false;
9822 if (TREE_CODE (arg1) == INTEGER_CST
9823 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9824 &strict_overflow_p)))
9826 if (strict_overflow_p)
9827 fold_overflow_warning (("assuming signed overflow does not "
9828 "occur when simplifying "
9829 "multiplication"),
9830 WARN_STRICT_OVERFLOW_MISC);
9831 return fold_convert_loc (loc, type, tem);
9834 /* Optimize z * conj(z) for integer complex numbers. */
9835 if (TREE_CODE (arg0) == CONJ_EXPR
9836 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9837 return fold_mult_zconjz (loc, type, arg1);
9838 if (TREE_CODE (arg1) == CONJ_EXPR
9839 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9840 return fold_mult_zconjz (loc, type, arg0);
9842 else
9844 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
9845 the result for floating point types due to rounding so it is applied
9846 only if -fassociative-math was specify. */
9847 if (flag_associative_math
9848 && TREE_CODE (arg0) == RDIV_EXPR
9849 && TREE_CODE (arg1) == REAL_CST
9850 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
9852 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
9853 arg1);
9854 if (tem)
9855 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
9856 TREE_OPERAND (arg0, 1));
9859 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
9860 if (operand_equal_p (arg0, arg1, 0))
9862 tree tem = fold_strip_sign_ops (arg0);
9863 if (tem != NULL_TREE)
9865 tem = fold_convert_loc (loc, type, tem);
9866 return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
9870 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
9871 This is not the same for NaNs or if signed zeros are
9872 involved. */
9873 if (!HONOR_NANS (arg0)
9874 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9875 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
9876 && TREE_CODE (arg1) == COMPLEX_CST
9877 && real_zerop (TREE_REALPART (arg1)))
9879 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9880 if (real_onep (TREE_IMAGPART (arg1)))
9881 return
9882 fold_build2_loc (loc, COMPLEX_EXPR, type,
9883 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
9884 rtype, arg0)),
9885 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
9886 else if (real_minus_onep (TREE_IMAGPART (arg1)))
9887 return
9888 fold_build2_loc (loc, COMPLEX_EXPR, type,
9889 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
9890 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
9891 rtype, arg0)));
9894 /* Optimize z * conj(z) for floating point complex numbers.
9895 Guarded by flag_unsafe_math_optimizations as non-finite
9896 imaginary components don't produce scalar results. */
9897 if (flag_unsafe_math_optimizations
9898 && TREE_CODE (arg0) == CONJ_EXPR
9899 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
9900 return fold_mult_zconjz (loc, type, arg1);
9901 if (flag_unsafe_math_optimizations
9902 && TREE_CODE (arg1) == CONJ_EXPR
9903 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9904 return fold_mult_zconjz (loc, type, arg0);
9906 if (flag_unsafe_math_optimizations)
9909 /* Canonicalize x*x as pow(x,2.0), which is expanded as x*x. */
9910 if (!in_gimple_form
9911 && optimize
9912 && operand_equal_p (arg0, arg1, 0))
9914 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
9916 if (powfn)
9918 tree arg = build_real (type, dconst2);
9919 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
9924 goto associate;
9926 case BIT_IOR_EXPR:
9927 /* Canonicalize (X & C1) | C2. */
9928 if (TREE_CODE (arg0) == BIT_AND_EXPR
9929 && TREE_CODE (arg1) == INTEGER_CST
9930 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9932 int width = TYPE_PRECISION (type), w;
9933 wide_int c1 = TREE_OPERAND (arg0, 1);
9934 wide_int c2 = arg1;
9936 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
9937 if ((c1 & c2) == c1)
9938 return omit_one_operand_loc (loc, type, arg1,
9939 TREE_OPERAND (arg0, 0));
9941 wide_int msk = wi::mask (width, false,
9942 TYPE_PRECISION (TREE_TYPE (arg1)));
9944 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
9945 if (msk.and_not (c1 | c2) == 0)
9946 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9947 TREE_OPERAND (arg0, 0), arg1);
9949 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
9950 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
9951 mode which allows further optimizations. */
9952 c1 &= msk;
9953 c2 &= msk;
9954 wide_int c3 = c1.and_not (c2);
9955 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
9957 wide_int mask = wi::mask (w, false,
9958 TYPE_PRECISION (type));
9959 if (((c1 | c2) & mask) == mask && c1.and_not (mask) == 0)
9961 c3 = mask;
9962 break;
9966 if (c3 != c1)
9967 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
9968 fold_build2_loc (loc, BIT_AND_EXPR, type,
9969 TREE_OPERAND (arg0, 0),
9970 wide_int_to_tree (type,
9971 c3)),
9972 arg1);
9975 /* (X & ~Y) | (~X & Y) is X ^ Y */
9976 if (TREE_CODE (arg0) == BIT_AND_EXPR
9977 && TREE_CODE (arg1) == BIT_AND_EXPR)
9979 tree a0, a1, l0, l1, n0, n1;
9981 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
9982 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
9984 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
9985 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
9987 n0 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l0);
9988 n1 = fold_build1_loc (loc, BIT_NOT_EXPR, type, l1);
9990 if ((operand_equal_p (n0, a0, 0)
9991 && operand_equal_p (n1, a1, 0))
9992 || (operand_equal_p (n0, a1, 0)
9993 && operand_equal_p (n1, a0, 0)))
9994 return fold_build2_loc (loc, BIT_XOR_EXPR, type, l0, n1);
9997 /* See if this can be simplified into a rotate first. If that
9998 is unsuccessful continue in the association code. */
9999 goto bit_rotate;
10001 case BIT_XOR_EXPR:
10002 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10003 if (TREE_CODE (arg0) == BIT_AND_EXPR
10004 && INTEGRAL_TYPE_P (type)
10005 && integer_onep (TREE_OPERAND (arg0, 1))
10006 && integer_onep (arg1))
10007 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10008 build_zero_cst (TREE_TYPE (arg0)));
10010 /* See if this can be simplified into a rotate first. If that
10011 is unsuccessful continue in the association code. */
10012 goto bit_rotate;
10014 case BIT_AND_EXPR:
10015 /* ~X & X, (X == 0) & X, and !X & X are always zero. */
10016 if ((TREE_CODE (arg0) == BIT_NOT_EXPR
10017 || TREE_CODE (arg0) == TRUTH_NOT_EXPR
10018 || (TREE_CODE (arg0) == EQ_EXPR
10019 && integer_zerop (TREE_OPERAND (arg0, 1))))
10020 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10021 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10023 /* X & ~X , X & (X == 0), and X & !X are always zero. */
10024 if ((TREE_CODE (arg1) == BIT_NOT_EXPR
10025 || TREE_CODE (arg1) == TRUTH_NOT_EXPR
10026 || (TREE_CODE (arg1) == EQ_EXPR
10027 && integer_zerop (TREE_OPERAND (arg1, 1))))
10028 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10029 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10031 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10032 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10033 && INTEGRAL_TYPE_P (type)
10034 && integer_onep (TREE_OPERAND (arg0, 1))
10035 && integer_onep (arg1))
10037 tree tem2;
10038 tem = TREE_OPERAND (arg0, 0);
10039 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10040 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10041 tem, tem2);
10042 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10043 build_zero_cst (TREE_TYPE (tem)));
10045 /* Fold ~X & 1 as (X & 1) == 0. */
10046 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10047 && INTEGRAL_TYPE_P (type)
10048 && integer_onep (arg1))
10050 tree tem2;
10051 tem = TREE_OPERAND (arg0, 0);
10052 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10053 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10054 tem, tem2);
10055 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10056 build_zero_cst (TREE_TYPE (tem)));
10058 /* Fold !X & 1 as X == 0. */
10059 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10060 && integer_onep (arg1))
10062 tem = TREE_OPERAND (arg0, 0);
10063 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10064 build_zero_cst (TREE_TYPE (tem)));
10067 /* Fold (X ^ Y) & Y as ~X & Y. */
10068 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10069 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10071 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10072 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10073 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10074 fold_convert_loc (loc, type, arg1));
10076 /* Fold (X ^ Y) & X as ~Y & X. */
10077 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10078 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10079 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10081 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10082 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10083 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10084 fold_convert_loc (loc, type, arg1));
10086 /* Fold X & (X ^ Y) as X & ~Y. */
10087 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10088 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10090 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10091 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10092 fold_convert_loc (loc, type, arg0),
10093 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
10095 /* Fold X & (Y ^ X) as ~Y & X. */
10096 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10097 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10098 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10100 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10101 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10102 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
10103 fold_convert_loc (loc, type, arg0));
10106 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10107 multiple of 1 << CST. */
10108 if (TREE_CODE (arg1) == INTEGER_CST)
10110 wide_int cst1 = arg1;
10111 wide_int ncst1 = -cst1;
10112 if ((cst1 & ncst1) == ncst1
10113 && multiple_of_p (type, arg0,
10114 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10115 return fold_convert_loc (loc, type, arg0);
10118 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10119 bits from CST2. */
10120 if (TREE_CODE (arg1) == INTEGER_CST
10121 && TREE_CODE (arg0) == MULT_EXPR
10122 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10124 wide_int warg1 = arg1;
10125 wide_int masked = mask_with_tz (type, warg1, TREE_OPERAND (arg0, 1));
10127 if (masked == 0)
10128 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10129 arg0, arg1);
10130 else if (masked != warg1)
10132 /* Avoid the transform if arg1 is a mask of some
10133 mode which allows further optimizations. */
10134 int pop = wi::popcount (warg1);
10135 if (!(pop >= BITS_PER_UNIT
10136 && exact_log2 (pop) != -1
10137 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10138 return fold_build2_loc (loc, code, type, op0,
10139 wide_int_to_tree (type, masked));
10143 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10144 ((A & N) + B) & M -> (A + B) & M
10145 Similarly if (N & M) == 0,
10146 ((A | N) + B) & M -> (A + B) & M
10147 and for - instead of + (or unary - instead of +)
10148 and/or ^ instead of |.
10149 If B is constant and (B & M) == 0, fold into A & M. */
10150 if (TREE_CODE (arg1) == INTEGER_CST)
10152 wide_int cst1 = arg1;
10153 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10154 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10155 && (TREE_CODE (arg0) == PLUS_EXPR
10156 || TREE_CODE (arg0) == MINUS_EXPR
10157 || TREE_CODE (arg0) == NEGATE_EXPR)
10158 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10159 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10161 tree pmop[2];
10162 int which = 0;
10163 wide_int cst0;
10165 /* Now we know that arg0 is (C + D) or (C - D) or
10166 -C and arg1 (M) is == (1LL << cst) - 1.
10167 Store C into PMOP[0] and D into PMOP[1]. */
10168 pmop[0] = TREE_OPERAND (arg0, 0);
10169 pmop[1] = NULL;
10170 if (TREE_CODE (arg0) != NEGATE_EXPR)
10172 pmop[1] = TREE_OPERAND (arg0, 1);
10173 which = 1;
10176 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10177 which = -1;
10179 for (; which >= 0; which--)
10180 switch (TREE_CODE (pmop[which]))
10182 case BIT_AND_EXPR:
10183 case BIT_IOR_EXPR:
10184 case BIT_XOR_EXPR:
10185 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10186 != INTEGER_CST)
10187 break;
10188 cst0 = TREE_OPERAND (pmop[which], 1);
10189 cst0 &= cst1;
10190 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10192 if (cst0 != cst1)
10193 break;
10195 else if (cst0 != 0)
10196 break;
10197 /* If C or D is of the form (A & N) where
10198 (N & M) == M, or of the form (A | N) or
10199 (A ^ N) where (N & M) == 0, replace it with A. */
10200 pmop[which] = TREE_OPERAND (pmop[which], 0);
10201 break;
10202 case INTEGER_CST:
10203 /* If C or D is a N where (N & M) == 0, it can be
10204 omitted (assumed 0). */
10205 if ((TREE_CODE (arg0) == PLUS_EXPR
10206 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10207 && (cst1 & pmop[which]) == 0)
10208 pmop[which] = NULL;
10209 break;
10210 default:
10211 break;
10214 /* Only build anything new if we optimized one or both arguments
10215 above. */
10216 if (pmop[0] != TREE_OPERAND (arg0, 0)
10217 || (TREE_CODE (arg0) != NEGATE_EXPR
10218 && pmop[1] != TREE_OPERAND (arg0, 1)))
10220 tree utype = TREE_TYPE (arg0);
10221 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10223 /* Perform the operations in a type that has defined
10224 overflow behavior. */
10225 utype = unsigned_type_for (TREE_TYPE (arg0));
10226 if (pmop[0] != NULL)
10227 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10228 if (pmop[1] != NULL)
10229 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10232 if (TREE_CODE (arg0) == NEGATE_EXPR)
10233 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10234 else if (TREE_CODE (arg0) == PLUS_EXPR)
10236 if (pmop[0] != NULL && pmop[1] != NULL)
10237 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10238 pmop[0], pmop[1]);
10239 else if (pmop[0] != NULL)
10240 tem = pmop[0];
10241 else if (pmop[1] != NULL)
10242 tem = pmop[1];
10243 else
10244 return build_int_cst (type, 0);
10246 else if (pmop[0] == NULL)
10247 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10248 else
10249 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10250 pmop[0], pmop[1]);
10251 /* TEM is now the new binary +, - or unary - replacement. */
10252 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10253 fold_convert_loc (loc, utype, arg1));
10254 return fold_convert_loc (loc, type, tem);
10259 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10260 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10261 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10263 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10265 wide_int mask = wide_int::from (arg1, prec, UNSIGNED);
10266 if (mask == -1)
10267 return
10268 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10271 goto associate;
10273 case RDIV_EXPR:
10274 /* Don't touch a floating-point divide by zero unless the mode
10275 of the constant can represent infinity. */
10276 if (TREE_CODE (arg1) == REAL_CST
10277 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10278 && real_zerop (arg1))
10279 return NULL_TREE;
10281 /* (-A) / (-B) -> A / B */
10282 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10283 return fold_build2_loc (loc, RDIV_EXPR, type,
10284 TREE_OPERAND (arg0, 0),
10285 negate_expr (arg1));
10286 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10287 return fold_build2_loc (loc, RDIV_EXPR, type,
10288 negate_expr (arg0),
10289 TREE_OPERAND (arg1, 0));
10291 /* Convert A/B/C to A/(B*C). */
10292 if (flag_reciprocal_math
10293 && TREE_CODE (arg0) == RDIV_EXPR)
10294 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
10295 fold_build2_loc (loc, MULT_EXPR, type,
10296 TREE_OPERAND (arg0, 1), arg1));
10298 /* Convert A/(B/C) to (A/B)*C. */
10299 if (flag_reciprocal_math
10300 && TREE_CODE (arg1) == RDIV_EXPR)
10301 return fold_build2_loc (loc, MULT_EXPR, type,
10302 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
10303 TREE_OPERAND (arg1, 0)),
10304 TREE_OPERAND (arg1, 1));
10306 /* Convert C1/(X*C2) into (C1/C2)/X. */
10307 if (flag_reciprocal_math
10308 && TREE_CODE (arg1) == MULT_EXPR
10309 && TREE_CODE (arg0) == REAL_CST
10310 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
10312 tree tem = const_binop (RDIV_EXPR, arg0,
10313 TREE_OPERAND (arg1, 1));
10314 if (tem)
10315 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
10316 TREE_OPERAND (arg1, 0));
10319 return NULL_TREE;
10321 case TRUNC_DIV_EXPR:
10322 /* Optimize (X & (-A)) / A where A is a power of 2,
10323 to X >> log2(A) */
10324 if (TREE_CODE (arg0) == BIT_AND_EXPR
10325 && !TYPE_UNSIGNED (type) && TREE_CODE (arg1) == INTEGER_CST
10326 && integer_pow2p (arg1) && tree_int_cst_sgn (arg1) > 0)
10328 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (arg1),
10329 arg1, TREE_OPERAND (arg0, 1));
10330 if (sum && integer_zerop (sum)) {
10331 tree pow2 = build_int_cst (integer_type_node,
10332 wi::exact_log2 (arg1));
10333 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10334 TREE_OPERAND (arg0, 0), pow2);
10338 /* Fall through */
10340 case FLOOR_DIV_EXPR:
10341 /* Simplify A / (B << N) where A and B are positive and B is
10342 a power of 2, to A >> (N + log2(B)). */
10343 strict_overflow_p = false;
10344 if (TREE_CODE (arg1) == LSHIFT_EXPR
10345 && (TYPE_UNSIGNED (type)
10346 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10348 tree sval = TREE_OPERAND (arg1, 0);
10349 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10351 tree sh_cnt = TREE_OPERAND (arg1, 1);
10352 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10353 wi::exact_log2 (sval));
10355 if (strict_overflow_p)
10356 fold_overflow_warning (("assuming signed overflow does not "
10357 "occur when simplifying A / (B << N)"),
10358 WARN_STRICT_OVERFLOW_MISC);
10360 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10361 sh_cnt, pow2);
10362 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10363 fold_convert_loc (loc, type, arg0), sh_cnt);
10367 /* Fall through */
10369 case ROUND_DIV_EXPR:
10370 case CEIL_DIV_EXPR:
10371 case EXACT_DIV_EXPR:
10372 if (integer_zerop (arg1))
10373 return NULL_TREE;
10375 /* Convert -A / -B to A / B when the type is signed and overflow is
10376 undefined. */
10377 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10378 && TREE_CODE (arg0) == NEGATE_EXPR
10379 && negate_expr_p (arg1))
10381 if (INTEGRAL_TYPE_P (type))
10382 fold_overflow_warning (("assuming signed overflow does not occur "
10383 "when distributing negation across "
10384 "division"),
10385 WARN_STRICT_OVERFLOW_MISC);
10386 return fold_build2_loc (loc, code, type,
10387 fold_convert_loc (loc, type,
10388 TREE_OPERAND (arg0, 0)),
10389 fold_convert_loc (loc, type,
10390 negate_expr (arg1)));
10392 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10393 && TREE_CODE (arg1) == NEGATE_EXPR
10394 && negate_expr_p (arg0))
10396 if (INTEGRAL_TYPE_P (type))
10397 fold_overflow_warning (("assuming signed overflow does not occur "
10398 "when distributing negation across "
10399 "division"),
10400 WARN_STRICT_OVERFLOW_MISC);
10401 return fold_build2_loc (loc, code, type,
10402 fold_convert_loc (loc, type,
10403 negate_expr (arg0)),
10404 fold_convert_loc (loc, type,
10405 TREE_OPERAND (arg1, 0)));
10408 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10409 operation, EXACT_DIV_EXPR.
10411 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10412 At one time others generated faster code, it's not clear if they do
10413 after the last round to changes to the DIV code in expmed.c. */
10414 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10415 && multiple_of_p (type, arg0, arg1))
10416 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10417 fold_convert (type, arg0),
10418 fold_convert (type, arg1));
10420 strict_overflow_p = false;
10421 if (TREE_CODE (arg1) == INTEGER_CST
10422 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10423 &strict_overflow_p)))
10425 if (strict_overflow_p)
10426 fold_overflow_warning (("assuming signed overflow does not occur "
10427 "when simplifying division"),
10428 WARN_STRICT_OVERFLOW_MISC);
10429 return fold_convert_loc (loc, type, tem);
10432 return NULL_TREE;
10434 case CEIL_MOD_EXPR:
10435 case FLOOR_MOD_EXPR:
10436 case ROUND_MOD_EXPR:
10437 case TRUNC_MOD_EXPR:
10438 strict_overflow_p = false;
10439 if (TREE_CODE (arg1) == INTEGER_CST
10440 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10441 &strict_overflow_p)))
10443 if (strict_overflow_p)
10444 fold_overflow_warning (("assuming signed overflow does not occur "
10445 "when simplifying modulus"),
10446 WARN_STRICT_OVERFLOW_MISC);
10447 return fold_convert_loc (loc, type, tem);
10450 return NULL_TREE;
10452 case LROTATE_EXPR:
10453 case RROTATE_EXPR:
10454 case RSHIFT_EXPR:
10455 case LSHIFT_EXPR:
10456 /* Since negative shift count is not well-defined,
10457 don't try to compute it in the compiler. */
10458 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10459 return NULL_TREE;
10461 prec = element_precision (type);
10463 /* If we have a rotate of a bit operation with the rotate count and
10464 the second operand of the bit operation both constant,
10465 permute the two operations. */
10466 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10467 && (TREE_CODE (arg0) == BIT_AND_EXPR
10468 || TREE_CODE (arg0) == BIT_IOR_EXPR
10469 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10470 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10471 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10472 fold_build2_loc (loc, code, type,
10473 TREE_OPERAND (arg0, 0), arg1),
10474 fold_build2_loc (loc, code, type,
10475 TREE_OPERAND (arg0, 1), arg1));
10477 /* Two consecutive rotates adding up to the some integer
10478 multiple of the precision of the type can be ignored. */
10479 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10480 && TREE_CODE (arg0) == RROTATE_EXPR
10481 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10482 && wi::umod_trunc (wi::add (arg1, TREE_OPERAND (arg0, 1)),
10483 prec) == 0)
10484 return TREE_OPERAND (arg0, 0);
10486 return NULL_TREE;
10488 case MIN_EXPR:
10489 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
10490 if (tem)
10491 return tem;
10492 goto associate;
10494 case MAX_EXPR:
10495 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
10496 if (tem)
10497 return tem;
10498 goto associate;
10500 case TRUTH_ANDIF_EXPR:
10501 /* Note that the operands of this must be ints
10502 and their values must be 0 or 1.
10503 ("true" is a fixed value perhaps depending on the language.) */
10504 /* If first arg is constant zero, return it. */
10505 if (integer_zerop (arg0))
10506 return fold_convert_loc (loc, type, arg0);
10507 case TRUTH_AND_EXPR:
10508 /* If either arg is constant true, drop it. */
10509 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10510 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10511 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10512 /* Preserve sequence points. */
10513 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10514 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10515 /* If second arg is constant zero, result is zero, but first arg
10516 must be evaluated. */
10517 if (integer_zerop (arg1))
10518 return omit_one_operand_loc (loc, type, arg1, arg0);
10519 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10520 case will be handled here. */
10521 if (integer_zerop (arg0))
10522 return omit_one_operand_loc (loc, type, arg0, arg1);
10524 /* !X && X is always false. */
10525 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10526 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10527 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10528 /* X && !X is always false. */
10529 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10530 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10531 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10533 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10534 means A >= Y && A != MAX, but in this case we know that
10535 A < X <= MAX. */
10537 if (!TREE_SIDE_EFFECTS (arg0)
10538 && !TREE_SIDE_EFFECTS (arg1))
10540 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10541 if (tem && !operand_equal_p (tem, arg0, 0))
10542 return fold_build2_loc (loc, code, type, tem, arg1);
10544 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10545 if (tem && !operand_equal_p (tem, arg1, 0))
10546 return fold_build2_loc (loc, code, type, arg0, tem);
10549 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10550 != NULL_TREE)
10551 return tem;
10553 return NULL_TREE;
10555 case TRUTH_ORIF_EXPR:
10556 /* Note that the operands of this must be ints
10557 and their values must be 0 or true.
10558 ("true" is a fixed value perhaps depending on the language.) */
10559 /* If first arg is constant true, return it. */
10560 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10561 return fold_convert_loc (loc, type, arg0);
10562 case TRUTH_OR_EXPR:
10563 /* If either arg is constant zero, drop it. */
10564 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10565 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10566 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10567 /* Preserve sequence points. */
10568 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10569 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10570 /* If second arg is constant true, result is true, but we must
10571 evaluate first arg. */
10572 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10573 return omit_one_operand_loc (loc, type, arg1, arg0);
10574 /* Likewise for first arg, but note this only occurs here for
10575 TRUTH_OR_EXPR. */
10576 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10577 return omit_one_operand_loc (loc, type, arg0, arg1);
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);
10583 /* X || !X is always true. */
10584 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10585 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10586 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10588 /* (X && !Y) || (!X && Y) is X ^ Y */
10589 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10590 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10592 tree a0, a1, l0, l1, n0, n1;
10594 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10595 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10597 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10598 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10600 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10601 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10603 if ((operand_equal_p (n0, a0, 0)
10604 && operand_equal_p (n1, a1, 0))
10605 || (operand_equal_p (n0, a1, 0)
10606 && operand_equal_p (n1, a0, 0)))
10607 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10610 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10611 != NULL_TREE)
10612 return tem;
10614 return NULL_TREE;
10616 case TRUTH_XOR_EXPR:
10617 /* If the second arg is constant zero, drop it. */
10618 if (integer_zerop (arg1))
10619 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10620 /* If the second arg is constant true, this is a logical inversion. */
10621 if (integer_onep (arg1))
10623 tem = invert_truthvalue_loc (loc, arg0);
10624 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10626 /* Identical arguments cancel to zero. */
10627 if (operand_equal_p (arg0, arg1, 0))
10628 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10630 /* !X ^ X is always true. */
10631 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10632 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10633 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10635 /* X ^ !X is always true. */
10636 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10637 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10638 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10640 return NULL_TREE;
10642 case EQ_EXPR:
10643 case NE_EXPR:
10644 STRIP_NOPS (arg0);
10645 STRIP_NOPS (arg1);
10647 tem = fold_comparison (loc, code, type, op0, op1);
10648 if (tem != NULL_TREE)
10649 return tem;
10651 /* bool_var != 1 becomes !bool_var. */
10652 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10653 && code == NE_EXPR)
10654 return fold_convert_loc (loc, type,
10655 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10656 TREE_TYPE (arg0), arg0));
10658 /* bool_var == 0 becomes !bool_var. */
10659 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10660 && code == EQ_EXPR)
10661 return fold_convert_loc (loc, type,
10662 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10663 TREE_TYPE (arg0), arg0));
10665 /* !exp != 0 becomes !exp */
10666 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10667 && code == NE_EXPR)
10668 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10670 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
10671 if ((TREE_CODE (arg0) == PLUS_EXPR
10672 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10673 || TREE_CODE (arg0) == MINUS_EXPR)
10674 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10675 0)),
10676 arg1, 0)
10677 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10678 || POINTER_TYPE_P (TREE_TYPE (arg0))))
10680 tree val = TREE_OPERAND (arg0, 1);
10681 return omit_two_operands_loc (loc, type,
10682 fold_build2_loc (loc, code, type,
10683 val,
10684 build_int_cst (TREE_TYPE (val),
10685 0)),
10686 TREE_OPERAND (arg0, 0), arg1);
10689 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
10690 if (TREE_CODE (arg0) == MINUS_EXPR
10691 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
10692 && operand_equal_p (tree_strip_nop_conversions (TREE_OPERAND (arg0,
10693 1)),
10694 arg1, 0)
10695 && wi::extract_uhwi (TREE_OPERAND (arg0, 0), 0, 1) == 1)
10697 return omit_two_operands_loc (loc, type,
10698 code == NE_EXPR
10699 ? boolean_true_node : boolean_false_node,
10700 TREE_OPERAND (arg0, 1), arg1);
10703 /* If this is an EQ or NE comparison with zero and ARG0 is
10704 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10705 two operations, but the latter can be done in one less insn
10706 on machines that have only two-operand insns or on which a
10707 constant cannot be the first operand. */
10708 if (TREE_CODE (arg0) == BIT_AND_EXPR
10709 && integer_zerop (arg1))
10711 tree arg00 = TREE_OPERAND (arg0, 0);
10712 tree arg01 = TREE_OPERAND (arg0, 1);
10713 if (TREE_CODE (arg00) == LSHIFT_EXPR
10714 && integer_onep (TREE_OPERAND (arg00, 0)))
10716 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10717 arg01, TREE_OPERAND (arg00, 1));
10718 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10719 build_int_cst (TREE_TYPE (arg0), 1));
10720 return fold_build2_loc (loc, code, type,
10721 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10722 arg1);
10724 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10725 && integer_onep (TREE_OPERAND (arg01, 0)))
10727 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10728 arg00, TREE_OPERAND (arg01, 1));
10729 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10730 build_int_cst (TREE_TYPE (arg0), 1));
10731 return fold_build2_loc (loc, code, type,
10732 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10733 arg1);
10737 /* If this is an NE or EQ comparison of zero against the result of a
10738 signed MOD operation whose second operand is a power of 2, make
10739 the MOD operation unsigned since it is simpler and equivalent. */
10740 if (integer_zerop (arg1)
10741 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10742 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10743 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10744 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10745 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10746 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10748 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10749 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10750 fold_convert_loc (loc, newtype,
10751 TREE_OPERAND (arg0, 0)),
10752 fold_convert_loc (loc, newtype,
10753 TREE_OPERAND (arg0, 1)));
10755 return fold_build2_loc (loc, code, type, newmod,
10756 fold_convert_loc (loc, newtype, arg1));
10759 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10760 C1 is a valid shift constant, and C2 is a power of two, i.e.
10761 a single bit. */
10762 if (TREE_CODE (arg0) == BIT_AND_EXPR
10763 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10764 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10765 == INTEGER_CST
10766 && integer_pow2p (TREE_OPERAND (arg0, 1))
10767 && integer_zerop (arg1))
10769 tree itype = TREE_TYPE (arg0);
10770 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10771 prec = TYPE_PRECISION (itype);
10773 /* Check for a valid shift count. */
10774 if (wi::ltu_p (arg001, prec))
10776 tree arg01 = TREE_OPERAND (arg0, 1);
10777 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10778 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10779 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10780 can be rewritten as (X & (C2 << C1)) != 0. */
10781 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10783 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10784 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10785 return fold_build2_loc (loc, code, type, tem,
10786 fold_convert_loc (loc, itype, arg1));
10788 /* Otherwise, for signed (arithmetic) shifts,
10789 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10790 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10791 else if (!TYPE_UNSIGNED (itype))
10792 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10793 arg000, build_int_cst (itype, 0));
10794 /* Otherwise, of unsigned (logical) shifts,
10795 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10796 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10797 else
10798 return omit_one_operand_loc (loc, type,
10799 code == EQ_EXPR ? integer_one_node
10800 : integer_zero_node,
10801 arg000);
10805 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
10806 Similarly for NE_EXPR. */
10807 if (TREE_CODE (arg0) == BIT_AND_EXPR
10808 && TREE_CODE (arg1) == INTEGER_CST
10809 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10811 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
10812 TREE_TYPE (TREE_OPERAND (arg0, 1)),
10813 TREE_OPERAND (arg0, 1));
10814 tree dandnotc
10815 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10816 fold_convert_loc (loc, TREE_TYPE (arg0), arg1),
10817 notc);
10818 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
10819 if (integer_nonzerop (dandnotc))
10820 return omit_one_operand_loc (loc, type, rslt, arg0);
10823 /* If this is a comparison of a field, we may be able to simplify it. */
10824 if ((TREE_CODE (arg0) == COMPONENT_REF
10825 || TREE_CODE (arg0) == BIT_FIELD_REF)
10826 /* Handle the constant case even without -O
10827 to make sure the warnings are given. */
10828 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10830 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10831 if (t1)
10832 return t1;
10835 /* Optimize comparisons of strlen vs zero to a compare of the
10836 first character of the string vs zero. To wit,
10837 strlen(ptr) == 0 => *ptr == 0
10838 strlen(ptr) != 0 => *ptr != 0
10839 Other cases should reduce to one of these two (or a constant)
10840 due to the return value of strlen being unsigned. */
10841 if (TREE_CODE (arg0) == CALL_EXPR
10842 && integer_zerop (arg1))
10844 tree fndecl = get_callee_fndecl (arg0);
10846 if (fndecl
10847 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10848 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10849 && call_expr_nargs (arg0) == 1
10850 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10852 tree iref = build_fold_indirect_ref_loc (loc,
10853 CALL_EXPR_ARG (arg0, 0));
10854 return fold_build2_loc (loc, code, type, iref,
10855 build_int_cst (TREE_TYPE (iref), 0));
10859 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10860 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10861 if (TREE_CODE (arg0) == RSHIFT_EXPR
10862 && integer_zerop (arg1)
10863 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10865 tree arg00 = TREE_OPERAND (arg0, 0);
10866 tree arg01 = TREE_OPERAND (arg0, 1);
10867 tree itype = TREE_TYPE (arg00);
10868 if (wi::eq_p (arg01, element_precision (itype) - 1))
10870 if (TYPE_UNSIGNED (itype))
10872 itype = signed_type_for (itype);
10873 arg00 = fold_convert_loc (loc, itype, arg00);
10875 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10876 type, arg00, build_zero_cst (itype));
10880 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10881 (X & C) == 0 when C is a single bit. */
10882 if (TREE_CODE (arg0) == BIT_AND_EXPR
10883 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10884 && integer_zerop (arg1)
10885 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10887 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10888 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10889 TREE_OPERAND (arg0, 1));
10890 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10891 type, tem,
10892 fold_convert_loc (loc, TREE_TYPE (arg0),
10893 arg1));
10896 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10897 constant C is a power of two, i.e. a single bit. */
10898 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10899 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10900 && integer_zerop (arg1)
10901 && integer_pow2p (TREE_OPERAND (arg0, 1))
10902 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10903 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10905 tree arg00 = TREE_OPERAND (arg0, 0);
10906 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10907 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10910 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10911 when is C is a power of two, i.e. a single bit. */
10912 if (TREE_CODE (arg0) == BIT_AND_EXPR
10913 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10914 && integer_zerop (arg1)
10915 && integer_pow2p (TREE_OPERAND (arg0, 1))
10916 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10917 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10919 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10920 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10921 arg000, TREE_OPERAND (arg0, 1));
10922 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10923 tem, build_int_cst (TREE_TYPE (tem), 0));
10926 if (integer_zerop (arg1)
10927 && tree_expr_nonzero_p (arg0))
10929 tree res = constant_boolean_node (code==NE_EXPR, type);
10930 return omit_one_operand_loc (loc, type, res, arg0);
10933 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10934 if (TREE_CODE (arg0) == BIT_AND_EXPR
10935 && TREE_CODE (arg1) == BIT_AND_EXPR)
10937 tree arg00 = TREE_OPERAND (arg0, 0);
10938 tree arg01 = TREE_OPERAND (arg0, 1);
10939 tree arg10 = TREE_OPERAND (arg1, 0);
10940 tree arg11 = TREE_OPERAND (arg1, 1);
10941 tree itype = TREE_TYPE (arg0);
10943 if (operand_equal_p (arg01, arg11, 0))
10944 return fold_build2_loc (loc, code, type,
10945 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10946 fold_build2_loc (loc,
10947 BIT_XOR_EXPR, itype,
10948 arg00, arg10),
10949 arg01),
10950 build_zero_cst (itype));
10952 if (operand_equal_p (arg01, arg10, 0))
10953 return fold_build2_loc (loc, code, type,
10954 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10955 fold_build2_loc (loc,
10956 BIT_XOR_EXPR, itype,
10957 arg00, arg11),
10958 arg01),
10959 build_zero_cst (itype));
10961 if (operand_equal_p (arg00, arg11, 0))
10962 return fold_build2_loc (loc, code, type,
10963 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10964 fold_build2_loc (loc,
10965 BIT_XOR_EXPR, itype,
10966 arg01, arg10),
10967 arg00),
10968 build_zero_cst (itype));
10970 if (operand_equal_p (arg00, arg10, 0))
10971 return fold_build2_loc (loc, code, type,
10972 fold_build2_loc (loc, BIT_AND_EXPR, itype,
10973 fold_build2_loc (loc,
10974 BIT_XOR_EXPR, itype,
10975 arg01, arg11),
10976 arg00),
10977 build_zero_cst (itype));
10980 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10981 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10983 tree arg00 = TREE_OPERAND (arg0, 0);
10984 tree arg01 = TREE_OPERAND (arg0, 1);
10985 tree arg10 = TREE_OPERAND (arg1, 0);
10986 tree arg11 = TREE_OPERAND (arg1, 1);
10987 tree itype = TREE_TYPE (arg0);
10989 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10990 operand_equal_p guarantees no side-effects so we don't need
10991 to use omit_one_operand on Z. */
10992 if (operand_equal_p (arg01, arg11, 0))
10993 return fold_build2_loc (loc, code, type, arg00,
10994 fold_convert_loc (loc, TREE_TYPE (arg00),
10995 arg10));
10996 if (operand_equal_p (arg01, arg10, 0))
10997 return fold_build2_loc (loc, code, type, arg00,
10998 fold_convert_loc (loc, TREE_TYPE (arg00),
10999 arg11));
11000 if (operand_equal_p (arg00, arg11, 0))
11001 return fold_build2_loc (loc, code, type, arg01,
11002 fold_convert_loc (loc, TREE_TYPE (arg01),
11003 arg10));
11004 if (operand_equal_p (arg00, arg10, 0))
11005 return fold_build2_loc (loc, code, type, arg01,
11006 fold_convert_loc (loc, TREE_TYPE (arg01),
11007 arg11));
11009 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
11010 if (TREE_CODE (arg01) == INTEGER_CST
11011 && TREE_CODE (arg11) == INTEGER_CST)
11013 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
11014 fold_convert_loc (loc, itype, arg11));
11015 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
11016 return fold_build2_loc (loc, code, type, tem,
11017 fold_convert_loc (loc, itype, arg10));
11021 /* Attempt to simplify equality/inequality comparisons of complex
11022 values. Only lower the comparison if the result is known or
11023 can be simplified to a single scalar comparison. */
11024 if ((TREE_CODE (arg0) == COMPLEX_EXPR
11025 || TREE_CODE (arg0) == COMPLEX_CST)
11026 && (TREE_CODE (arg1) == COMPLEX_EXPR
11027 || TREE_CODE (arg1) == COMPLEX_CST))
11029 tree real0, imag0, real1, imag1;
11030 tree rcond, icond;
11032 if (TREE_CODE (arg0) == COMPLEX_EXPR)
11034 real0 = TREE_OPERAND (arg0, 0);
11035 imag0 = TREE_OPERAND (arg0, 1);
11037 else
11039 real0 = TREE_REALPART (arg0);
11040 imag0 = TREE_IMAGPART (arg0);
11043 if (TREE_CODE (arg1) == COMPLEX_EXPR)
11045 real1 = TREE_OPERAND (arg1, 0);
11046 imag1 = TREE_OPERAND (arg1, 1);
11048 else
11050 real1 = TREE_REALPART (arg1);
11051 imag1 = TREE_IMAGPART (arg1);
11054 rcond = fold_binary_loc (loc, code, type, real0, real1);
11055 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11057 if (integer_zerop (rcond))
11059 if (code == EQ_EXPR)
11060 return omit_two_operands_loc (loc, type, boolean_false_node,
11061 imag0, imag1);
11062 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
11064 else
11066 if (code == NE_EXPR)
11067 return omit_two_operands_loc (loc, type, boolean_true_node,
11068 imag0, imag1);
11069 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
11073 icond = fold_binary_loc (loc, code, type, imag0, imag1);
11074 if (icond && TREE_CODE (icond) == INTEGER_CST)
11076 if (integer_zerop (icond))
11078 if (code == EQ_EXPR)
11079 return omit_two_operands_loc (loc, type, boolean_false_node,
11080 real0, real1);
11081 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11083 else
11085 if (code == NE_EXPR)
11086 return omit_two_operands_loc (loc, type, boolean_true_node,
11087 real0, real1);
11088 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11093 return NULL_TREE;
11095 case LT_EXPR:
11096 case GT_EXPR:
11097 case LE_EXPR:
11098 case GE_EXPR:
11099 tem = fold_comparison (loc, code, type, op0, op1);
11100 if (tem != NULL_TREE)
11101 return tem;
11103 /* Transform comparisons of the form X +- C CMP X. */
11104 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11105 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11106 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11107 && !HONOR_SNANS (arg0))
11108 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11109 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
11111 tree arg01 = TREE_OPERAND (arg0, 1);
11112 enum tree_code code0 = TREE_CODE (arg0);
11113 int is_positive;
11115 if (TREE_CODE (arg01) == REAL_CST)
11116 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11117 else
11118 is_positive = tree_int_cst_sgn (arg01);
11120 /* (X - c) > X becomes false. */
11121 if (code == GT_EXPR
11122 && ((code0 == MINUS_EXPR && is_positive >= 0)
11123 || (code0 == PLUS_EXPR && is_positive <= 0)))
11125 if (TREE_CODE (arg01) == INTEGER_CST
11126 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11127 fold_overflow_warning (("assuming signed overflow does not "
11128 "occur when assuming that (X - c) > X "
11129 "is always false"),
11130 WARN_STRICT_OVERFLOW_ALL);
11131 return constant_boolean_node (0, type);
11134 /* Likewise (X + c) < X becomes false. */
11135 if (code == LT_EXPR
11136 && ((code0 == PLUS_EXPR && is_positive >= 0)
11137 || (code0 == MINUS_EXPR && is_positive <= 0)))
11139 if (TREE_CODE (arg01) == INTEGER_CST
11140 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11141 fold_overflow_warning (("assuming signed overflow does not "
11142 "occur when assuming that "
11143 "(X + c) < X is always false"),
11144 WARN_STRICT_OVERFLOW_ALL);
11145 return constant_boolean_node (0, type);
11148 /* Convert (X - c) <= X to true. */
11149 if (!HONOR_NANS (arg1)
11150 && code == LE_EXPR
11151 && ((code0 == MINUS_EXPR && is_positive >= 0)
11152 || (code0 == PLUS_EXPR && is_positive <= 0)))
11154 if (TREE_CODE (arg01) == INTEGER_CST
11155 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11156 fold_overflow_warning (("assuming signed overflow does not "
11157 "occur when assuming that "
11158 "(X - c) <= X is always true"),
11159 WARN_STRICT_OVERFLOW_ALL);
11160 return constant_boolean_node (1, type);
11163 /* Convert (X + c) >= X to true. */
11164 if (!HONOR_NANS (arg1)
11165 && code == GE_EXPR
11166 && ((code0 == PLUS_EXPR && is_positive >= 0)
11167 || (code0 == MINUS_EXPR && is_positive <= 0)))
11169 if (TREE_CODE (arg01) == INTEGER_CST
11170 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11171 fold_overflow_warning (("assuming signed overflow does not "
11172 "occur when assuming that "
11173 "(X + c) >= X is always true"),
11174 WARN_STRICT_OVERFLOW_ALL);
11175 return constant_boolean_node (1, type);
11178 if (TREE_CODE (arg01) == INTEGER_CST)
11180 /* Convert X + c > X and X - c < X to true for integers. */
11181 if (code == GT_EXPR
11182 && ((code0 == PLUS_EXPR && is_positive > 0)
11183 || (code0 == MINUS_EXPR && is_positive < 0)))
11185 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11186 fold_overflow_warning (("assuming signed overflow does "
11187 "not occur when assuming that "
11188 "(X + c) > X is always true"),
11189 WARN_STRICT_OVERFLOW_ALL);
11190 return constant_boolean_node (1, type);
11193 if (code == LT_EXPR
11194 && ((code0 == MINUS_EXPR && is_positive > 0)
11195 || (code0 == PLUS_EXPR && is_positive < 0)))
11197 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11198 fold_overflow_warning (("assuming signed overflow does "
11199 "not occur when assuming that "
11200 "(X - c) < X is always true"),
11201 WARN_STRICT_OVERFLOW_ALL);
11202 return constant_boolean_node (1, type);
11205 /* Convert X + c <= X and X - c >= X to false for integers. */
11206 if (code == LE_EXPR
11207 && ((code0 == PLUS_EXPR && is_positive > 0)
11208 || (code0 == MINUS_EXPR && is_positive < 0)))
11210 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11211 fold_overflow_warning (("assuming signed overflow does "
11212 "not occur when assuming that "
11213 "(X + c) <= X is always false"),
11214 WARN_STRICT_OVERFLOW_ALL);
11215 return constant_boolean_node (0, type);
11218 if (code == GE_EXPR
11219 && ((code0 == MINUS_EXPR && is_positive > 0)
11220 || (code0 == PLUS_EXPR && is_positive < 0)))
11222 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
11223 fold_overflow_warning (("assuming signed overflow does "
11224 "not occur when assuming that "
11225 "(X - c) >= X is always false"),
11226 WARN_STRICT_OVERFLOW_ALL);
11227 return constant_boolean_node (0, type);
11232 /* If we are comparing an ABS_EXPR with a constant, we can
11233 convert all the cases into explicit comparisons, but they may
11234 well not be faster than doing the ABS and one comparison.
11235 But ABS (X) <= C is a range comparison, which becomes a subtraction
11236 and a comparison, and is probably faster. */
11237 if (code == LE_EXPR
11238 && TREE_CODE (arg1) == INTEGER_CST
11239 && TREE_CODE (arg0) == ABS_EXPR
11240 && ! TREE_SIDE_EFFECTS (arg0)
11241 && (0 != (tem = negate_expr (arg1)))
11242 && TREE_CODE (tem) == INTEGER_CST
11243 && !TREE_OVERFLOW (tem))
11244 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11245 build2 (GE_EXPR, type,
11246 TREE_OPERAND (arg0, 0), tem),
11247 build2 (LE_EXPR, type,
11248 TREE_OPERAND (arg0, 0), arg1));
11250 /* Convert ABS_EXPR<x> >= 0 to true. */
11251 strict_overflow_p = false;
11252 if (code == GE_EXPR
11253 && (integer_zerop (arg1)
11254 || (! HONOR_NANS (arg0)
11255 && real_zerop (arg1)))
11256 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11258 if (strict_overflow_p)
11259 fold_overflow_warning (("assuming signed overflow does not occur "
11260 "when simplifying comparison of "
11261 "absolute value and zero"),
11262 WARN_STRICT_OVERFLOW_CONDITIONAL);
11263 return omit_one_operand_loc (loc, type,
11264 constant_boolean_node (true, type),
11265 arg0);
11268 /* Convert ABS_EXPR<x> < 0 to false. */
11269 strict_overflow_p = false;
11270 if (code == LT_EXPR
11271 && (integer_zerop (arg1) || real_zerop (arg1))
11272 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11274 if (strict_overflow_p)
11275 fold_overflow_warning (("assuming signed overflow does not occur "
11276 "when simplifying comparison of "
11277 "absolute value and zero"),
11278 WARN_STRICT_OVERFLOW_CONDITIONAL);
11279 return omit_one_operand_loc (loc, type,
11280 constant_boolean_node (false, type),
11281 arg0);
11284 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11285 and similarly for >= into !=. */
11286 if ((code == LT_EXPR || code == GE_EXPR)
11287 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11288 && TREE_CODE (arg1) == LSHIFT_EXPR
11289 && integer_onep (TREE_OPERAND (arg1, 0)))
11290 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11291 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11292 TREE_OPERAND (arg1, 1)),
11293 build_zero_cst (TREE_TYPE (arg0)));
11295 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11296 otherwise Y might be >= # of bits in X's type and thus e.g.
11297 (unsigned char) (1 << Y) for Y 15 might be 0.
11298 If the cast is widening, then 1 << Y should have unsigned type,
11299 otherwise if Y is number of bits in the signed shift type minus 1,
11300 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11301 31 might be 0xffffffff80000000. */
11302 if ((code == LT_EXPR || code == GE_EXPR)
11303 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11304 && CONVERT_EXPR_P (arg1)
11305 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11306 && (element_precision (TREE_TYPE (arg1))
11307 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11308 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11309 || (element_precision (TREE_TYPE (arg1))
11310 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11311 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11313 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11314 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11315 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11316 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11317 build_zero_cst (TREE_TYPE (arg0)));
11320 return NULL_TREE;
11322 case UNORDERED_EXPR:
11323 case ORDERED_EXPR:
11324 case UNLT_EXPR:
11325 case UNLE_EXPR:
11326 case UNGT_EXPR:
11327 case UNGE_EXPR:
11328 case UNEQ_EXPR:
11329 case LTGT_EXPR:
11330 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11332 tree targ0 = strip_float_extensions (arg0);
11333 tree targ1 = strip_float_extensions (arg1);
11334 tree newtype = TREE_TYPE (targ0);
11336 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11337 newtype = TREE_TYPE (targ1);
11339 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11340 return fold_build2_loc (loc, code, type,
11341 fold_convert_loc (loc, newtype, targ0),
11342 fold_convert_loc (loc, newtype, targ1));
11345 return NULL_TREE;
11347 case COMPOUND_EXPR:
11348 /* When pedantic, a compound expression can be neither an lvalue
11349 nor an integer constant expression. */
11350 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11351 return NULL_TREE;
11352 /* Don't let (0, 0) be null pointer constant. */
11353 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11354 : fold_convert_loc (loc, type, arg1);
11355 return pedantic_non_lvalue_loc (loc, tem);
11357 case ASSERT_EXPR:
11358 /* An ASSERT_EXPR should never be passed to fold_binary. */
11359 gcc_unreachable ();
11361 default:
11362 return NULL_TREE;
11363 } /* switch (code) */
11366 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11367 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11368 of GOTO_EXPR. */
11370 static tree
11371 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11373 switch (TREE_CODE (*tp))
11375 case LABEL_EXPR:
11376 return *tp;
11378 case GOTO_EXPR:
11379 *walk_subtrees = 0;
11381 /* ... fall through ... */
11383 default:
11384 return NULL_TREE;
11388 /* Return whether the sub-tree ST contains a label which is accessible from
11389 outside the sub-tree. */
11391 static bool
11392 contains_label_p (tree st)
11394 return
11395 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11398 /* Fold a ternary expression of code CODE and type TYPE with operands
11399 OP0, OP1, and OP2. Return the folded expression if folding is
11400 successful. Otherwise, return NULL_TREE. */
11402 tree
11403 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11404 tree op0, tree op1, tree op2)
11406 tree tem;
11407 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11408 enum tree_code_class kind = TREE_CODE_CLASS (code);
11410 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11411 && TREE_CODE_LENGTH (code) == 3);
11413 /* If this is a commutative operation, and OP0 is a constant, move it
11414 to OP1 to reduce the number of tests below. */
11415 if (commutative_ternary_tree_code (code)
11416 && tree_swap_operands_p (op0, op1, true))
11417 return fold_build3_loc (loc, code, type, op1, op0, op2);
11419 tem = generic_simplify (loc, code, type, op0, op1, op2);
11420 if (tem)
11421 return tem;
11423 /* Strip any conversions that don't change the mode. This is safe
11424 for every expression, except for a comparison expression because
11425 its signedness is derived from its operands. So, in the latter
11426 case, only strip conversions that don't change the signedness.
11428 Note that this is done as an internal manipulation within the
11429 constant folder, in order to find the simplest representation of
11430 the arguments so that their form can be studied. In any cases,
11431 the appropriate type conversions should be put back in the tree
11432 that will get out of the constant folder. */
11433 if (op0)
11435 arg0 = op0;
11436 STRIP_NOPS (arg0);
11439 if (op1)
11441 arg1 = op1;
11442 STRIP_NOPS (arg1);
11445 if (op2)
11447 arg2 = op2;
11448 STRIP_NOPS (arg2);
11451 switch (code)
11453 case COMPONENT_REF:
11454 if (TREE_CODE (arg0) == CONSTRUCTOR
11455 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11457 unsigned HOST_WIDE_INT idx;
11458 tree field, value;
11459 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11460 if (field == arg1)
11461 return value;
11463 return NULL_TREE;
11465 case COND_EXPR:
11466 case VEC_COND_EXPR:
11467 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11468 so all simple results must be passed through pedantic_non_lvalue. */
11469 if (TREE_CODE (arg0) == INTEGER_CST)
11471 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11472 tem = integer_zerop (arg0) ? op2 : op1;
11473 /* Only optimize constant conditions when the selected branch
11474 has the same type as the COND_EXPR. This avoids optimizing
11475 away "c ? x : throw", where the throw has a void type.
11476 Avoid throwing away that operand which contains label. */
11477 if ((!TREE_SIDE_EFFECTS (unused_op)
11478 || !contains_label_p (unused_op))
11479 && (! VOID_TYPE_P (TREE_TYPE (tem))
11480 || VOID_TYPE_P (type)))
11481 return pedantic_non_lvalue_loc (loc, tem);
11482 return NULL_TREE;
11484 else if (TREE_CODE (arg0) == VECTOR_CST)
11486 if ((TREE_CODE (arg1) == VECTOR_CST
11487 || TREE_CODE (arg1) == CONSTRUCTOR)
11488 && (TREE_CODE (arg2) == VECTOR_CST
11489 || TREE_CODE (arg2) == CONSTRUCTOR))
11491 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i;
11492 unsigned char *sel = XALLOCAVEC (unsigned char, nelts);
11493 gcc_assert (nelts == VECTOR_CST_NELTS (arg0));
11494 for (i = 0; i < nelts; i++)
11496 tree val = VECTOR_CST_ELT (arg0, i);
11497 if (integer_all_onesp (val))
11498 sel[i] = i;
11499 else if (integer_zerop (val))
11500 sel[i] = nelts + i;
11501 else /* Currently unreachable. */
11502 return NULL_TREE;
11504 tree t = fold_vec_perm (type, arg1, arg2, sel);
11505 if (t != NULL_TREE)
11506 return t;
11510 /* If we have A op B ? A : C, we may be able to convert this to a
11511 simpler expression, depending on the operation and the values
11512 of B and C. Signed zeros prevent all of these transformations,
11513 for reasons given above each one.
11515 Also try swapping the arguments and inverting the conditional. */
11516 if (COMPARISON_CLASS_P (arg0)
11517 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11518 arg1, TREE_OPERAND (arg0, 1))
11519 && !HONOR_SIGNED_ZEROS (element_mode (arg1)))
11521 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11522 if (tem)
11523 return tem;
11526 if (COMPARISON_CLASS_P (arg0)
11527 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
11528 op2,
11529 TREE_OPERAND (arg0, 1))
11530 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11532 location_t loc0 = expr_location_or (arg0, loc);
11533 tem = fold_invert_truthvalue (loc0, arg0);
11534 if (tem && COMPARISON_CLASS_P (tem))
11536 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11537 if (tem)
11538 return tem;
11542 /* If the second operand is simpler than the third, swap them
11543 since that produces better jump optimization results. */
11544 if (truth_value_p (TREE_CODE (arg0))
11545 && tree_swap_operands_p (op1, op2, false))
11547 location_t loc0 = expr_location_or (arg0, loc);
11548 /* See if this can be inverted. If it can't, possibly because
11549 it was a floating-point inequality comparison, don't do
11550 anything. */
11551 tem = fold_invert_truthvalue (loc0, arg0);
11552 if (tem)
11553 return fold_build3_loc (loc, code, type, tem, op2, op1);
11556 /* Convert A ? 1 : 0 to simply A. */
11557 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11558 : (integer_onep (op1)
11559 && !VECTOR_TYPE_P (type)))
11560 && integer_zerop (op2)
11561 /* If we try to convert OP0 to our type, the
11562 call to fold will try to move the conversion inside
11563 a COND, which will recurse. In that case, the COND_EXPR
11564 is probably the best choice, so leave it alone. */
11565 && type == TREE_TYPE (arg0))
11566 return pedantic_non_lvalue_loc (loc, arg0);
11568 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11569 over COND_EXPR in cases such as floating point comparisons. */
11570 if (integer_zerop (op1)
11571 && (code == VEC_COND_EXPR ? integer_all_onesp (op2)
11572 : (integer_onep (op2)
11573 && !VECTOR_TYPE_P (type)))
11574 && truth_value_p (TREE_CODE (arg0)))
11575 return pedantic_non_lvalue_loc (loc,
11576 fold_convert_loc (loc, type,
11577 invert_truthvalue_loc (loc,
11578 arg0)));
11580 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11581 if (TREE_CODE (arg0) == LT_EXPR
11582 && integer_zerop (TREE_OPERAND (arg0, 1))
11583 && integer_zerop (op2)
11584 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11586 /* sign_bit_p looks through both zero and sign extensions,
11587 but for this optimization only sign extensions are
11588 usable. */
11589 tree tem2 = TREE_OPERAND (arg0, 0);
11590 while (tem != tem2)
11592 if (TREE_CODE (tem2) != NOP_EXPR
11593 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11595 tem = NULL_TREE;
11596 break;
11598 tem2 = TREE_OPERAND (tem2, 0);
11600 /* sign_bit_p only checks ARG1 bits within A's precision.
11601 If <sign bit of A> has wider type than A, bits outside
11602 of A's precision in <sign bit of A> need to be checked.
11603 If they are all 0, this optimization needs to be done
11604 in unsigned A's type, if they are all 1 in signed A's type,
11605 otherwise this can't be done. */
11606 if (tem
11607 && TYPE_PRECISION (TREE_TYPE (tem))
11608 < TYPE_PRECISION (TREE_TYPE (arg1))
11609 && TYPE_PRECISION (TREE_TYPE (tem))
11610 < TYPE_PRECISION (type))
11612 int inner_width, outer_width;
11613 tree tem_type;
11615 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11616 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11617 if (outer_width > TYPE_PRECISION (type))
11618 outer_width = TYPE_PRECISION (type);
11620 wide_int mask = wi::shifted_mask
11621 (inner_width, outer_width - inner_width, false,
11622 TYPE_PRECISION (TREE_TYPE (arg1)));
11624 wide_int common = mask & arg1;
11625 if (common == mask)
11627 tem_type = signed_type_for (TREE_TYPE (tem));
11628 tem = fold_convert_loc (loc, tem_type, tem);
11630 else if (common == 0)
11632 tem_type = unsigned_type_for (TREE_TYPE (tem));
11633 tem = fold_convert_loc (loc, tem_type, tem);
11635 else
11636 tem = NULL;
11639 if (tem)
11640 return
11641 fold_convert_loc (loc, type,
11642 fold_build2_loc (loc, BIT_AND_EXPR,
11643 TREE_TYPE (tem), tem,
11644 fold_convert_loc (loc,
11645 TREE_TYPE (tem),
11646 arg1)));
11649 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11650 already handled above. */
11651 if (TREE_CODE (arg0) == BIT_AND_EXPR
11652 && integer_onep (TREE_OPERAND (arg0, 1))
11653 && integer_zerop (op2)
11654 && integer_pow2p (arg1))
11656 tree tem = TREE_OPERAND (arg0, 0);
11657 STRIP_NOPS (tem);
11658 if (TREE_CODE (tem) == RSHIFT_EXPR
11659 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11660 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
11661 tree_to_uhwi (TREE_OPERAND (tem, 1)))
11662 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11663 TREE_OPERAND (tem, 0), arg1);
11666 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11667 is probably obsolete because the first operand should be a
11668 truth value (that's why we have the two cases above), but let's
11669 leave it in until we can confirm this for all front-ends. */
11670 if (integer_zerop (op2)
11671 && TREE_CODE (arg0) == NE_EXPR
11672 && integer_zerop (TREE_OPERAND (arg0, 1))
11673 && integer_pow2p (arg1)
11674 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11675 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11676 arg1, OEP_ONLY_CONST))
11677 return pedantic_non_lvalue_loc (loc,
11678 fold_convert_loc (loc, type,
11679 TREE_OPERAND (arg0, 0)));
11681 /* Disable the transformations below for vectors, since
11682 fold_binary_op_with_conditional_arg may undo them immediately,
11683 yielding an infinite loop. */
11684 if (code == VEC_COND_EXPR)
11685 return NULL_TREE;
11687 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11688 if (integer_zerop (op2)
11689 && truth_value_p (TREE_CODE (arg0))
11690 && truth_value_p (TREE_CODE (arg1))
11691 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11692 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11693 : TRUTH_ANDIF_EXPR,
11694 type, fold_convert_loc (loc, type, arg0), arg1);
11696 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11697 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11698 && truth_value_p (TREE_CODE (arg0))
11699 && truth_value_p (TREE_CODE (arg1))
11700 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11702 location_t loc0 = expr_location_or (arg0, loc);
11703 /* Only perform transformation if ARG0 is easily inverted. */
11704 tem = fold_invert_truthvalue (loc0, arg0);
11705 if (tem)
11706 return fold_build2_loc (loc, code == VEC_COND_EXPR
11707 ? BIT_IOR_EXPR
11708 : TRUTH_ORIF_EXPR,
11709 type, fold_convert_loc (loc, type, tem),
11710 arg1);
11713 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11714 if (integer_zerop (arg1)
11715 && truth_value_p (TREE_CODE (arg0))
11716 && truth_value_p (TREE_CODE (op2))
11717 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11719 location_t loc0 = expr_location_or (arg0, loc);
11720 /* Only perform transformation if ARG0 is easily inverted. */
11721 tem = fold_invert_truthvalue (loc0, arg0);
11722 if (tem)
11723 return fold_build2_loc (loc, code == VEC_COND_EXPR
11724 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11725 type, fold_convert_loc (loc, type, tem),
11726 op2);
11729 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11730 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11731 && truth_value_p (TREE_CODE (arg0))
11732 && truth_value_p (TREE_CODE (op2))
11733 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11734 return fold_build2_loc (loc, code == VEC_COND_EXPR
11735 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11736 type, fold_convert_loc (loc, type, arg0), op2);
11738 return NULL_TREE;
11740 case CALL_EXPR:
11741 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11742 of fold_ternary on them. */
11743 gcc_unreachable ();
11745 case BIT_FIELD_REF:
11746 if ((TREE_CODE (arg0) == VECTOR_CST
11747 || (TREE_CODE (arg0) == CONSTRUCTOR
11748 && TREE_CODE (TREE_TYPE (arg0)) == VECTOR_TYPE))
11749 && (type == TREE_TYPE (TREE_TYPE (arg0))
11750 || (TREE_CODE (type) == VECTOR_TYPE
11751 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11753 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11754 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11755 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11756 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11758 if (n != 0
11759 && (idx % width) == 0
11760 && (n % width) == 0
11761 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11763 idx = idx / width;
11764 n = n / width;
11766 if (TREE_CODE (arg0) == VECTOR_CST)
11768 if (n == 1)
11769 return VECTOR_CST_ELT (arg0, idx);
11771 tree *vals = XALLOCAVEC (tree, n);
11772 for (unsigned i = 0; i < n; ++i)
11773 vals[i] = VECTOR_CST_ELT (arg0, idx + i);
11774 return build_vector (type, vals);
11777 /* Constructor elements can be subvectors. */
11778 unsigned HOST_WIDE_INT k = 1;
11779 if (CONSTRUCTOR_NELTS (arg0) != 0)
11781 tree cons_elem = TREE_TYPE (CONSTRUCTOR_ELT (arg0, 0)->value);
11782 if (TREE_CODE (cons_elem) == VECTOR_TYPE)
11783 k = TYPE_VECTOR_SUBPARTS (cons_elem);
11786 /* We keep an exact subset of the constructor elements. */
11787 if ((idx % k) == 0 && (n % k) == 0)
11789 if (CONSTRUCTOR_NELTS (arg0) == 0)
11790 return build_constructor (type, NULL);
11791 idx /= k;
11792 n /= k;
11793 if (n == 1)
11795 if (idx < CONSTRUCTOR_NELTS (arg0))
11796 return CONSTRUCTOR_ELT (arg0, idx)->value;
11797 return build_zero_cst (type);
11800 vec<constructor_elt, va_gc> *vals;
11801 vec_alloc (vals, n);
11802 for (unsigned i = 0;
11803 i < n && idx + i < CONSTRUCTOR_NELTS (arg0);
11804 ++i)
11805 CONSTRUCTOR_APPEND_ELT (vals, NULL_TREE,
11806 CONSTRUCTOR_ELT
11807 (arg0, idx + i)->value);
11808 return build_constructor (type, vals);
11810 /* The bitfield references a single constructor element. */
11811 else if (idx + n <= (idx / k + 1) * k)
11813 if (CONSTRUCTOR_NELTS (arg0) <= idx / k)
11814 return build_zero_cst (type);
11815 else if (n == k)
11816 return CONSTRUCTOR_ELT (arg0, idx / k)->value;
11817 else
11818 return fold_build3_loc (loc, code, type,
11819 CONSTRUCTOR_ELT (arg0, idx / k)->value, op1,
11820 build_int_cst (TREE_TYPE (op2), (idx % k) * width));
11825 /* A bit-field-ref that referenced the full argument can be stripped. */
11826 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11827 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_to_uhwi (arg1)
11828 && integer_zerop (op2))
11829 return fold_convert_loc (loc, type, arg0);
11831 /* On constants we can use native encode/interpret to constant
11832 fold (nearly) all BIT_FIELD_REFs. */
11833 if (CONSTANT_CLASS_P (arg0)
11834 && can_native_interpret_type_p (type)
11835 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (arg0)))
11836 /* This limitation should not be necessary, we just need to
11837 round this up to mode size. */
11838 && tree_to_uhwi (op1) % BITS_PER_UNIT == 0
11839 /* Need bit-shifting of the buffer to relax the following. */
11840 && tree_to_uhwi (op2) % BITS_PER_UNIT == 0)
11842 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11843 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11844 unsigned HOST_WIDE_INT clen;
11845 clen = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (arg0)));
11846 /* ??? We cannot tell native_encode_expr to start at
11847 some random byte only. So limit us to a reasonable amount
11848 of work. */
11849 if (clen <= 4096)
11851 unsigned char *b = XALLOCAVEC (unsigned char, clen);
11852 unsigned HOST_WIDE_INT len = native_encode_expr (arg0, b, clen);
11853 if (len > 0
11854 && len * BITS_PER_UNIT >= bitpos + bitsize)
11856 tree v = native_interpret_expr (type,
11857 b + bitpos / BITS_PER_UNIT,
11858 bitsize / BITS_PER_UNIT);
11859 if (v)
11860 return v;
11865 return NULL_TREE;
11867 case FMA_EXPR:
11868 /* For integers we can decompose the FMA if possible. */
11869 if (TREE_CODE (arg0) == INTEGER_CST
11870 && TREE_CODE (arg1) == INTEGER_CST)
11871 return fold_build2_loc (loc, PLUS_EXPR, type,
11872 const_binop (MULT_EXPR, arg0, arg1), arg2);
11873 if (integer_zerop (arg2))
11874 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11876 return fold_fma (loc, type, arg0, arg1, arg2);
11878 case VEC_PERM_EXPR:
11879 if (TREE_CODE (arg2) == VECTOR_CST)
11881 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type), i, mask, mask2;
11882 unsigned char *sel = XALLOCAVEC (unsigned char, 2 * nelts);
11883 unsigned char *sel2 = sel + nelts;
11884 bool need_mask_canon = false;
11885 bool need_mask_canon2 = false;
11886 bool all_in_vec0 = true;
11887 bool all_in_vec1 = true;
11888 bool maybe_identity = true;
11889 bool single_arg = (op0 == op1);
11890 bool changed = false;
11892 mask2 = 2 * nelts - 1;
11893 mask = single_arg ? (nelts - 1) : mask2;
11894 gcc_assert (nelts == VECTOR_CST_NELTS (arg2));
11895 for (i = 0; i < nelts; i++)
11897 tree val = VECTOR_CST_ELT (arg2, i);
11898 if (TREE_CODE (val) != INTEGER_CST)
11899 return NULL_TREE;
11901 /* Make sure that the perm value is in an acceptable
11902 range. */
11903 wide_int t = val;
11904 need_mask_canon |= wi::gtu_p (t, mask);
11905 need_mask_canon2 |= wi::gtu_p (t, mask2);
11906 sel[i] = t.to_uhwi () & mask;
11907 sel2[i] = t.to_uhwi () & mask2;
11909 if (sel[i] < nelts)
11910 all_in_vec1 = false;
11911 else
11912 all_in_vec0 = false;
11914 if ((sel[i] & (nelts-1)) != i)
11915 maybe_identity = false;
11918 if (maybe_identity)
11920 if (all_in_vec0)
11921 return op0;
11922 if (all_in_vec1)
11923 return op1;
11926 if (all_in_vec0)
11927 op1 = op0;
11928 else if (all_in_vec1)
11930 op0 = op1;
11931 for (i = 0; i < nelts; i++)
11932 sel[i] -= nelts;
11933 need_mask_canon = true;
11936 if ((TREE_CODE (op0) == VECTOR_CST
11937 || TREE_CODE (op0) == CONSTRUCTOR)
11938 && (TREE_CODE (op1) == VECTOR_CST
11939 || TREE_CODE (op1) == CONSTRUCTOR))
11941 tree t = fold_vec_perm (type, op0, op1, sel);
11942 if (t != NULL_TREE)
11943 return t;
11946 if (op0 == op1 && !single_arg)
11947 changed = true;
11949 /* Some targets are deficient and fail to expand a single
11950 argument permutation while still allowing an equivalent
11951 2-argument version. */
11952 if (need_mask_canon && arg2 == op2
11953 && !can_vec_perm_p (TYPE_MODE (type), false, sel)
11954 && can_vec_perm_p (TYPE_MODE (type), false, sel2))
11956 need_mask_canon = need_mask_canon2;
11957 sel = sel2;
11960 if (need_mask_canon && arg2 == op2)
11962 tree *tsel = XALLOCAVEC (tree, nelts);
11963 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11964 for (i = 0; i < nelts; i++)
11965 tsel[i] = build_int_cst (eltype, sel[i]);
11966 op2 = build_vector (TREE_TYPE (arg2), tsel);
11967 changed = true;
11970 if (changed)
11971 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11973 return NULL_TREE;
11975 default:
11976 return NULL_TREE;
11977 } /* switch (code) */
11980 /* Perform constant folding and related simplification of EXPR.
11981 The related simplifications include x*1 => x, x*0 => 0, etc.,
11982 and application of the associative law.
11983 NOP_EXPR conversions may be removed freely (as long as we
11984 are careful not to change the type of the overall expression).
11985 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11986 but we can constant-fold them if they have constant operands. */
11988 #ifdef ENABLE_FOLD_CHECKING
11989 # define fold(x) fold_1 (x)
11990 static tree fold_1 (tree);
11991 static
11992 #endif
11993 tree
11994 fold (tree expr)
11996 const tree t = expr;
11997 enum tree_code code = TREE_CODE (t);
11998 enum tree_code_class kind = TREE_CODE_CLASS (code);
11999 tree tem;
12000 location_t loc = EXPR_LOCATION (expr);
12002 /* Return right away if a constant. */
12003 if (kind == tcc_constant)
12004 return t;
12006 /* CALL_EXPR-like objects with variable numbers of operands are
12007 treated specially. */
12008 if (kind == tcc_vl_exp)
12010 if (code == CALL_EXPR)
12012 tem = fold_call_expr (loc, expr, false);
12013 return tem ? tem : expr;
12015 return expr;
12018 if (IS_EXPR_CODE_CLASS (kind))
12020 tree type = TREE_TYPE (t);
12021 tree op0, op1, op2;
12023 switch (TREE_CODE_LENGTH (code))
12025 case 1:
12026 op0 = TREE_OPERAND (t, 0);
12027 tem = fold_unary_loc (loc, code, type, op0);
12028 return tem ? tem : expr;
12029 case 2:
12030 op0 = TREE_OPERAND (t, 0);
12031 op1 = TREE_OPERAND (t, 1);
12032 tem = fold_binary_loc (loc, code, type, op0, op1);
12033 return tem ? tem : expr;
12034 case 3:
12035 op0 = TREE_OPERAND (t, 0);
12036 op1 = TREE_OPERAND (t, 1);
12037 op2 = TREE_OPERAND (t, 2);
12038 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12039 return tem ? tem : expr;
12040 default:
12041 break;
12045 switch (code)
12047 case ARRAY_REF:
12049 tree op0 = TREE_OPERAND (t, 0);
12050 tree op1 = TREE_OPERAND (t, 1);
12052 if (TREE_CODE (op1) == INTEGER_CST
12053 && TREE_CODE (op0) == CONSTRUCTOR
12054 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
12056 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (op0);
12057 unsigned HOST_WIDE_INT end = vec_safe_length (elts);
12058 unsigned HOST_WIDE_INT begin = 0;
12060 /* Find a matching index by means of a binary search. */
12061 while (begin != end)
12063 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
12064 tree index = (*elts)[middle].index;
12066 if (TREE_CODE (index) == INTEGER_CST
12067 && tree_int_cst_lt (index, op1))
12068 begin = middle + 1;
12069 else if (TREE_CODE (index) == INTEGER_CST
12070 && tree_int_cst_lt (op1, index))
12071 end = middle;
12072 else if (TREE_CODE (index) == RANGE_EXPR
12073 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
12074 begin = middle + 1;
12075 else if (TREE_CODE (index) == RANGE_EXPR
12076 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
12077 end = middle;
12078 else
12079 return (*elts)[middle].value;
12083 return t;
12086 /* Return a VECTOR_CST if possible. */
12087 case CONSTRUCTOR:
12089 tree type = TREE_TYPE (t);
12090 if (TREE_CODE (type) != VECTOR_TYPE)
12091 return t;
12093 tree *vec = XALLOCAVEC (tree, TYPE_VECTOR_SUBPARTS (type));
12094 unsigned HOST_WIDE_INT idx, pos = 0;
12095 tree value;
12097 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), idx, value)
12099 if (!CONSTANT_CLASS_P (value))
12100 return t;
12101 if (TREE_CODE (value) == VECTOR_CST)
12103 for (unsigned i = 0; i < VECTOR_CST_NELTS (value); ++i)
12104 vec[pos++] = VECTOR_CST_ELT (value, i);
12106 else
12107 vec[pos++] = value;
12109 for (; pos < TYPE_VECTOR_SUBPARTS (type); ++pos)
12110 vec[pos] = build_zero_cst (TREE_TYPE (type));
12112 return build_vector (type, vec);
12115 case CONST_DECL:
12116 return fold (DECL_INITIAL (t));
12118 default:
12119 return t;
12120 } /* switch (code) */
12123 #ifdef ENABLE_FOLD_CHECKING
12124 #undef fold
12126 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12127 hash_table<nofree_ptr_hash<const tree_node> > *);
12128 static void fold_check_failed (const_tree, const_tree);
12129 void print_fold_checksum (const_tree);
12131 /* When --enable-checking=fold, compute a digest of expr before
12132 and after actual fold call to see if fold did not accidentally
12133 change original expr. */
12135 tree
12136 fold (tree expr)
12138 tree ret;
12139 struct md5_ctx ctx;
12140 unsigned char checksum_before[16], checksum_after[16];
12141 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12143 md5_init_ctx (&ctx);
12144 fold_checksum_tree (expr, &ctx, &ht);
12145 md5_finish_ctx (&ctx, checksum_before);
12146 ht.empty ();
12148 ret = fold_1 (expr);
12150 md5_init_ctx (&ctx);
12151 fold_checksum_tree (expr, &ctx, &ht);
12152 md5_finish_ctx (&ctx, checksum_after);
12154 if (memcmp (checksum_before, checksum_after, 16))
12155 fold_check_failed (expr, ret);
12157 return ret;
12160 void
12161 print_fold_checksum (const_tree expr)
12163 struct md5_ctx ctx;
12164 unsigned char checksum[16], cnt;
12165 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12167 md5_init_ctx (&ctx);
12168 fold_checksum_tree (expr, &ctx, &ht);
12169 md5_finish_ctx (&ctx, checksum);
12170 for (cnt = 0; cnt < 16; ++cnt)
12171 fprintf (stderr, "%02x", checksum[cnt]);
12172 putc ('\n', stderr);
12175 static void
12176 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12178 internal_error ("fold check: original tree changed by fold");
12181 static void
12182 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12183 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12185 const tree_node **slot;
12186 enum tree_code code;
12187 union tree_node buf;
12188 int i, len;
12190 recursive_label:
12191 if (expr == NULL)
12192 return;
12193 slot = ht->find_slot (expr, INSERT);
12194 if (*slot != NULL)
12195 return;
12196 *slot = expr;
12197 code = TREE_CODE (expr);
12198 if (TREE_CODE_CLASS (code) == tcc_declaration
12199 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12201 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12202 memcpy ((char *) &buf, expr, tree_size (expr));
12203 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12204 buf.decl_with_vis.symtab_node = NULL;
12205 expr = (tree) &buf;
12207 else if (TREE_CODE_CLASS (code) == tcc_type
12208 && (TYPE_POINTER_TO (expr)
12209 || TYPE_REFERENCE_TO (expr)
12210 || TYPE_CACHED_VALUES_P (expr)
12211 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12212 || TYPE_NEXT_VARIANT (expr)))
12214 /* Allow these fields to be modified. */
12215 tree tmp;
12216 memcpy ((char *) &buf, expr, tree_size (expr));
12217 expr = tmp = (tree) &buf;
12218 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12219 TYPE_POINTER_TO (tmp) = NULL;
12220 TYPE_REFERENCE_TO (tmp) = NULL;
12221 TYPE_NEXT_VARIANT (tmp) = NULL;
12222 if (TYPE_CACHED_VALUES_P (tmp))
12224 TYPE_CACHED_VALUES_P (tmp) = 0;
12225 TYPE_CACHED_VALUES (tmp) = NULL;
12228 md5_process_bytes (expr, tree_size (expr), ctx);
12229 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12230 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12231 if (TREE_CODE_CLASS (code) != tcc_type
12232 && TREE_CODE_CLASS (code) != tcc_declaration
12233 && code != TREE_LIST
12234 && code != SSA_NAME
12235 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12236 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12237 switch (TREE_CODE_CLASS (code))
12239 case tcc_constant:
12240 switch (code)
12242 case STRING_CST:
12243 md5_process_bytes (TREE_STRING_POINTER (expr),
12244 TREE_STRING_LENGTH (expr), ctx);
12245 break;
12246 case COMPLEX_CST:
12247 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12248 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12249 break;
12250 case VECTOR_CST:
12251 for (i = 0; i < (int) VECTOR_CST_NELTS (expr); ++i)
12252 fold_checksum_tree (VECTOR_CST_ELT (expr, i), ctx, ht);
12253 break;
12254 default:
12255 break;
12257 break;
12258 case tcc_exceptional:
12259 switch (code)
12261 case TREE_LIST:
12262 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12263 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12264 expr = TREE_CHAIN (expr);
12265 goto recursive_label;
12266 break;
12267 case TREE_VEC:
12268 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12269 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12270 break;
12271 default:
12272 break;
12274 break;
12275 case tcc_expression:
12276 case tcc_reference:
12277 case tcc_comparison:
12278 case tcc_unary:
12279 case tcc_binary:
12280 case tcc_statement:
12281 case tcc_vl_exp:
12282 len = TREE_OPERAND_LENGTH (expr);
12283 for (i = 0; i < len; ++i)
12284 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12285 break;
12286 case tcc_declaration:
12287 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12288 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12289 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12291 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12292 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12293 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12294 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12295 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12298 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12300 if (TREE_CODE (expr) == FUNCTION_DECL)
12302 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12303 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12305 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12307 break;
12308 case tcc_type:
12309 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12310 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12311 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12312 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12313 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12314 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12315 if (INTEGRAL_TYPE_P (expr)
12316 || SCALAR_FLOAT_TYPE_P (expr))
12318 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12319 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12321 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12322 if (TREE_CODE (expr) == RECORD_TYPE
12323 || TREE_CODE (expr) == UNION_TYPE
12324 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12325 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12326 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12327 break;
12328 default:
12329 break;
12333 /* Helper function for outputting the checksum of a tree T. When
12334 debugging with gdb, you can "define mynext" to be "next" followed
12335 by "call debug_fold_checksum (op0)", then just trace down till the
12336 outputs differ. */
12338 DEBUG_FUNCTION void
12339 debug_fold_checksum (const_tree t)
12341 int i;
12342 unsigned char checksum[16];
12343 struct md5_ctx ctx;
12344 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12346 md5_init_ctx (&ctx);
12347 fold_checksum_tree (t, &ctx, &ht);
12348 md5_finish_ctx (&ctx, checksum);
12349 ht.empty ();
12351 for (i = 0; i < 16; i++)
12352 fprintf (stderr, "%d ", checksum[i]);
12354 fprintf (stderr, "\n");
12357 #endif
12359 /* Fold a unary tree expression with code CODE of type TYPE with an
12360 operand OP0. LOC is the location of the resulting expression.
12361 Return a folded expression if successful. Otherwise, return a tree
12362 expression with code CODE of type TYPE with an operand OP0. */
12364 tree
12365 fold_build1_stat_loc (location_t loc,
12366 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12368 tree tem;
12369 #ifdef ENABLE_FOLD_CHECKING
12370 unsigned char checksum_before[16], checksum_after[16];
12371 struct md5_ctx ctx;
12372 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12374 md5_init_ctx (&ctx);
12375 fold_checksum_tree (op0, &ctx, &ht);
12376 md5_finish_ctx (&ctx, checksum_before);
12377 ht.empty ();
12378 #endif
12380 tem = fold_unary_loc (loc, code, type, op0);
12381 if (!tem)
12382 tem = build1_stat_loc (loc, code, type, op0 PASS_MEM_STAT);
12384 #ifdef ENABLE_FOLD_CHECKING
12385 md5_init_ctx (&ctx);
12386 fold_checksum_tree (op0, &ctx, &ht);
12387 md5_finish_ctx (&ctx, checksum_after);
12389 if (memcmp (checksum_before, checksum_after, 16))
12390 fold_check_failed (op0, tem);
12391 #endif
12392 return tem;
12395 /* Fold a binary tree expression with code CODE of type TYPE with
12396 operands OP0 and OP1. LOC is the location of the resulting
12397 expression. Return a folded expression if successful. Otherwise,
12398 return a tree expression with code CODE of type TYPE with operands
12399 OP0 and OP1. */
12401 tree
12402 fold_build2_stat_loc (location_t loc,
12403 enum tree_code code, tree type, tree op0, tree op1
12404 MEM_STAT_DECL)
12406 tree tem;
12407 #ifdef ENABLE_FOLD_CHECKING
12408 unsigned char checksum_before_op0[16],
12409 checksum_before_op1[16],
12410 checksum_after_op0[16],
12411 checksum_after_op1[16];
12412 struct md5_ctx ctx;
12413 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12415 md5_init_ctx (&ctx);
12416 fold_checksum_tree (op0, &ctx, &ht);
12417 md5_finish_ctx (&ctx, checksum_before_op0);
12418 ht.empty ();
12420 md5_init_ctx (&ctx);
12421 fold_checksum_tree (op1, &ctx, &ht);
12422 md5_finish_ctx (&ctx, checksum_before_op1);
12423 ht.empty ();
12424 #endif
12426 tem = fold_binary_loc (loc, code, type, op0, op1);
12427 if (!tem)
12428 tem = build2_stat_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12430 #ifdef ENABLE_FOLD_CHECKING
12431 md5_init_ctx (&ctx);
12432 fold_checksum_tree (op0, &ctx, &ht);
12433 md5_finish_ctx (&ctx, checksum_after_op0);
12434 ht.empty ();
12436 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12437 fold_check_failed (op0, tem);
12439 md5_init_ctx (&ctx);
12440 fold_checksum_tree (op1, &ctx, &ht);
12441 md5_finish_ctx (&ctx, checksum_after_op1);
12443 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12444 fold_check_failed (op1, tem);
12445 #endif
12446 return tem;
12449 /* Fold a ternary tree expression with code CODE of type TYPE with
12450 operands OP0, OP1, and OP2. Return a folded expression if
12451 successful. Otherwise, return a tree expression with code CODE of
12452 type TYPE with operands OP0, OP1, and OP2. */
12454 tree
12455 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
12456 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12458 tree tem;
12459 #ifdef ENABLE_FOLD_CHECKING
12460 unsigned char checksum_before_op0[16],
12461 checksum_before_op1[16],
12462 checksum_before_op2[16],
12463 checksum_after_op0[16],
12464 checksum_after_op1[16],
12465 checksum_after_op2[16];
12466 struct md5_ctx ctx;
12467 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12469 md5_init_ctx (&ctx);
12470 fold_checksum_tree (op0, &ctx, &ht);
12471 md5_finish_ctx (&ctx, checksum_before_op0);
12472 ht.empty ();
12474 md5_init_ctx (&ctx);
12475 fold_checksum_tree (op1, &ctx, &ht);
12476 md5_finish_ctx (&ctx, checksum_before_op1);
12477 ht.empty ();
12479 md5_init_ctx (&ctx);
12480 fold_checksum_tree (op2, &ctx, &ht);
12481 md5_finish_ctx (&ctx, checksum_before_op2);
12482 ht.empty ();
12483 #endif
12485 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12486 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12487 if (!tem)
12488 tem = build3_stat_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12490 #ifdef ENABLE_FOLD_CHECKING
12491 md5_init_ctx (&ctx);
12492 fold_checksum_tree (op0, &ctx, &ht);
12493 md5_finish_ctx (&ctx, checksum_after_op0);
12494 ht.empty ();
12496 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12497 fold_check_failed (op0, tem);
12499 md5_init_ctx (&ctx);
12500 fold_checksum_tree (op1, &ctx, &ht);
12501 md5_finish_ctx (&ctx, checksum_after_op1);
12502 ht.empty ();
12504 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12505 fold_check_failed (op1, tem);
12507 md5_init_ctx (&ctx);
12508 fold_checksum_tree (op2, &ctx, &ht);
12509 md5_finish_ctx (&ctx, checksum_after_op2);
12511 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12512 fold_check_failed (op2, tem);
12513 #endif
12514 return tem;
12517 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12518 arguments in ARGARRAY, and a null static chain.
12519 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12520 of type TYPE from the given operands as constructed by build_call_array. */
12522 tree
12523 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12524 int nargs, tree *argarray)
12526 tree tem;
12527 #ifdef ENABLE_FOLD_CHECKING
12528 unsigned char checksum_before_fn[16],
12529 checksum_before_arglist[16],
12530 checksum_after_fn[16],
12531 checksum_after_arglist[16];
12532 struct md5_ctx ctx;
12533 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12534 int i;
12536 md5_init_ctx (&ctx);
12537 fold_checksum_tree (fn, &ctx, &ht);
12538 md5_finish_ctx (&ctx, checksum_before_fn);
12539 ht.empty ();
12541 md5_init_ctx (&ctx);
12542 for (i = 0; i < nargs; i++)
12543 fold_checksum_tree (argarray[i], &ctx, &ht);
12544 md5_finish_ctx (&ctx, checksum_before_arglist);
12545 ht.empty ();
12546 #endif
12548 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12549 if (!tem)
12550 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12552 #ifdef ENABLE_FOLD_CHECKING
12553 md5_init_ctx (&ctx);
12554 fold_checksum_tree (fn, &ctx, &ht);
12555 md5_finish_ctx (&ctx, checksum_after_fn);
12556 ht.empty ();
12558 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12559 fold_check_failed (fn, tem);
12561 md5_init_ctx (&ctx);
12562 for (i = 0; i < nargs; i++)
12563 fold_checksum_tree (argarray[i], &ctx, &ht);
12564 md5_finish_ctx (&ctx, checksum_after_arglist);
12566 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12567 fold_check_failed (NULL_TREE, tem);
12568 #endif
12569 return tem;
12572 /* Perform constant folding and related simplification of initializer
12573 expression EXPR. These behave identically to "fold_buildN" but ignore
12574 potential run-time traps and exceptions that fold must preserve. */
12576 #define START_FOLD_INIT \
12577 int saved_signaling_nans = flag_signaling_nans;\
12578 int saved_trapping_math = flag_trapping_math;\
12579 int saved_rounding_math = flag_rounding_math;\
12580 int saved_trapv = flag_trapv;\
12581 int saved_folding_initializer = folding_initializer;\
12582 flag_signaling_nans = 0;\
12583 flag_trapping_math = 0;\
12584 flag_rounding_math = 0;\
12585 flag_trapv = 0;\
12586 folding_initializer = 1;
12588 #define END_FOLD_INIT \
12589 flag_signaling_nans = saved_signaling_nans;\
12590 flag_trapping_math = saved_trapping_math;\
12591 flag_rounding_math = saved_rounding_math;\
12592 flag_trapv = saved_trapv;\
12593 folding_initializer = saved_folding_initializer;
12595 tree
12596 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12597 tree type, tree op)
12599 tree result;
12600 START_FOLD_INIT;
12602 result = fold_build1_loc (loc, code, type, op);
12604 END_FOLD_INIT;
12605 return result;
12608 tree
12609 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12610 tree type, tree op0, tree op1)
12612 tree result;
12613 START_FOLD_INIT;
12615 result = fold_build2_loc (loc, code, type, op0, op1);
12617 END_FOLD_INIT;
12618 return result;
12621 tree
12622 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12623 int nargs, tree *argarray)
12625 tree result;
12626 START_FOLD_INIT;
12628 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12630 END_FOLD_INIT;
12631 return result;
12634 #undef START_FOLD_INIT
12635 #undef END_FOLD_INIT
12637 /* Determine if first argument is a multiple of second argument. Return 0 if
12638 it is not, or we cannot easily determined it to be.
12640 An example of the sort of thing we care about (at this point; this routine
12641 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12642 fold cases do now) is discovering that
12644 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12646 is a multiple of
12648 SAVE_EXPR (J * 8)
12650 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12652 This code also handles discovering that
12654 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12656 is a multiple of 8 so we don't have to worry about dealing with a
12657 possible remainder.
12659 Note that we *look* inside a SAVE_EXPR only to determine how it was
12660 calculated; it is not safe for fold to do much of anything else with the
12661 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12662 at run time. For example, the latter example above *cannot* be implemented
12663 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12664 evaluation time of the original SAVE_EXPR is not necessarily the same at
12665 the time the new expression is evaluated. The only optimization of this
12666 sort that would be valid is changing
12668 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12670 divided by 8 to
12672 SAVE_EXPR (I) * SAVE_EXPR (J)
12674 (where the same SAVE_EXPR (J) is used in the original and the
12675 transformed version). */
12678 multiple_of_p (tree type, const_tree top, const_tree bottom)
12680 if (operand_equal_p (top, bottom, 0))
12681 return 1;
12683 if (TREE_CODE (type) != INTEGER_TYPE)
12684 return 0;
12686 switch (TREE_CODE (top))
12688 case BIT_AND_EXPR:
12689 /* Bitwise and provides a power of two multiple. If the mask is
12690 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12691 if (!integer_pow2p (bottom))
12692 return 0;
12693 /* FALLTHRU */
12695 case MULT_EXPR:
12696 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12697 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12699 case PLUS_EXPR:
12700 case MINUS_EXPR:
12701 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
12702 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
12704 case LSHIFT_EXPR:
12705 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12707 tree op1, t1;
12709 op1 = TREE_OPERAND (top, 1);
12710 /* const_binop may not detect overflow correctly,
12711 so check for it explicitly here. */
12712 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)), op1)
12713 && 0 != (t1 = fold_convert (type,
12714 const_binop (LSHIFT_EXPR,
12715 size_one_node,
12716 op1)))
12717 && !TREE_OVERFLOW (t1))
12718 return multiple_of_p (type, t1, bottom);
12720 return 0;
12722 case NOP_EXPR:
12723 /* Can't handle conversions from non-integral or wider integral type. */
12724 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12725 || (TYPE_PRECISION (type)
12726 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12727 return 0;
12729 /* .. fall through ... */
12731 case SAVE_EXPR:
12732 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12734 case COND_EXPR:
12735 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12736 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12738 case INTEGER_CST:
12739 if (TREE_CODE (bottom) != INTEGER_CST
12740 || integer_zerop (bottom)
12741 || (TYPE_UNSIGNED (type)
12742 && (tree_int_cst_sgn (top) < 0
12743 || tree_int_cst_sgn (bottom) < 0)))
12744 return 0;
12745 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12746 SIGNED);
12748 default:
12749 return 0;
12753 /* Return true if CODE or TYPE is known to be non-negative. */
12755 static bool
12756 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12758 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12759 && truth_value_p (code))
12760 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12761 have a signed:1 type (where the value is -1 and 0). */
12762 return true;
12763 return false;
12766 /* Return true if (CODE OP0) is known to be non-negative. If the return
12767 value is based on the assumption that signed overflow is undefined,
12768 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12769 *STRICT_OVERFLOW_P. */
12771 bool
12772 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12773 bool *strict_overflow_p)
12775 if (TYPE_UNSIGNED (type))
12776 return true;
12778 switch (code)
12780 case ABS_EXPR:
12781 /* We can't return 1 if flag_wrapv is set because
12782 ABS_EXPR<INT_MIN> = INT_MIN. */
12783 if (!ANY_INTEGRAL_TYPE_P (type))
12784 return true;
12785 if (TYPE_OVERFLOW_UNDEFINED (type))
12787 *strict_overflow_p = true;
12788 return true;
12790 break;
12792 case NON_LVALUE_EXPR:
12793 case FLOAT_EXPR:
12794 case FIX_TRUNC_EXPR:
12795 return tree_expr_nonnegative_warnv_p (op0,
12796 strict_overflow_p);
12798 CASE_CONVERT:
12800 tree inner_type = TREE_TYPE (op0);
12801 tree outer_type = type;
12803 if (TREE_CODE (outer_type) == REAL_TYPE)
12805 if (TREE_CODE (inner_type) == REAL_TYPE)
12806 return tree_expr_nonnegative_warnv_p (op0,
12807 strict_overflow_p);
12808 if (INTEGRAL_TYPE_P (inner_type))
12810 if (TYPE_UNSIGNED (inner_type))
12811 return true;
12812 return tree_expr_nonnegative_warnv_p (op0,
12813 strict_overflow_p);
12816 else if (INTEGRAL_TYPE_P (outer_type))
12818 if (TREE_CODE (inner_type) == REAL_TYPE)
12819 return tree_expr_nonnegative_warnv_p (op0,
12820 strict_overflow_p);
12821 if (INTEGRAL_TYPE_P (inner_type))
12822 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12823 && TYPE_UNSIGNED (inner_type);
12826 break;
12828 default:
12829 return tree_simple_nonnegative_warnv_p (code, type);
12832 /* We don't know sign of `t', so be conservative and return false. */
12833 return false;
12836 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12837 value is based on the assumption that signed overflow is undefined,
12838 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12839 *STRICT_OVERFLOW_P. */
12841 bool
12842 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12843 tree op1, bool *strict_overflow_p)
12845 if (TYPE_UNSIGNED (type))
12846 return true;
12848 switch (code)
12850 case POINTER_PLUS_EXPR:
12851 case PLUS_EXPR:
12852 if (FLOAT_TYPE_P (type))
12853 return (tree_expr_nonnegative_warnv_p (op0,
12854 strict_overflow_p)
12855 && tree_expr_nonnegative_warnv_p (op1,
12856 strict_overflow_p));
12858 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12859 both unsigned and at least 2 bits shorter than the result. */
12860 if (TREE_CODE (type) == INTEGER_TYPE
12861 && TREE_CODE (op0) == NOP_EXPR
12862 && TREE_CODE (op1) == NOP_EXPR)
12864 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12865 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12866 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12867 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12869 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12870 TYPE_PRECISION (inner2)) + 1;
12871 return prec < TYPE_PRECISION (type);
12874 break;
12876 case MULT_EXPR:
12877 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12879 /* x * x is always non-negative for floating point x
12880 or without overflow. */
12881 if (operand_equal_p (op0, op1, 0)
12882 || (tree_expr_nonnegative_warnv_p (op0, strict_overflow_p)
12883 && tree_expr_nonnegative_warnv_p (op1, strict_overflow_p)))
12885 if (ANY_INTEGRAL_TYPE_P (type)
12886 && TYPE_OVERFLOW_UNDEFINED (type))
12887 *strict_overflow_p = true;
12888 return true;
12892 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12893 both unsigned and their total bits is shorter than the result. */
12894 if (TREE_CODE (type) == INTEGER_TYPE
12895 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12896 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12898 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12899 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12900 : TREE_TYPE (op0);
12901 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12902 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12903 : TREE_TYPE (op1);
12905 bool unsigned0 = TYPE_UNSIGNED (inner0);
12906 bool unsigned1 = TYPE_UNSIGNED (inner1);
12908 if (TREE_CODE (op0) == INTEGER_CST)
12909 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12911 if (TREE_CODE (op1) == INTEGER_CST)
12912 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12914 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12915 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12917 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12918 ? tree_int_cst_min_precision (op0, UNSIGNED)
12919 : TYPE_PRECISION (inner0);
12921 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12922 ? tree_int_cst_min_precision (op1, UNSIGNED)
12923 : TYPE_PRECISION (inner1);
12925 return precision0 + precision1 < TYPE_PRECISION (type);
12928 return false;
12930 case BIT_AND_EXPR:
12931 case MAX_EXPR:
12932 return (tree_expr_nonnegative_warnv_p (op0,
12933 strict_overflow_p)
12934 || tree_expr_nonnegative_warnv_p (op1,
12935 strict_overflow_p));
12937 case BIT_IOR_EXPR:
12938 case BIT_XOR_EXPR:
12939 case MIN_EXPR:
12940 case RDIV_EXPR:
12941 case TRUNC_DIV_EXPR:
12942 case CEIL_DIV_EXPR:
12943 case FLOOR_DIV_EXPR:
12944 case ROUND_DIV_EXPR:
12945 return (tree_expr_nonnegative_warnv_p (op0,
12946 strict_overflow_p)
12947 && tree_expr_nonnegative_warnv_p (op1,
12948 strict_overflow_p));
12950 case TRUNC_MOD_EXPR:
12951 case CEIL_MOD_EXPR:
12952 case FLOOR_MOD_EXPR:
12953 case ROUND_MOD_EXPR:
12954 return tree_expr_nonnegative_warnv_p (op0,
12955 strict_overflow_p);
12956 default:
12957 return tree_simple_nonnegative_warnv_p (code, type);
12960 /* We don't know sign of `t', so be conservative and return false. */
12961 return false;
12964 /* Return true if T is known to be non-negative. If the return
12965 value is based on the assumption that signed overflow is undefined,
12966 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12967 *STRICT_OVERFLOW_P. */
12969 bool
12970 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
12972 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12973 return true;
12975 switch (TREE_CODE (t))
12977 case INTEGER_CST:
12978 return tree_int_cst_sgn (t) >= 0;
12980 case REAL_CST:
12981 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12983 case FIXED_CST:
12984 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12986 case COND_EXPR:
12987 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
12988 strict_overflow_p)
12989 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
12990 strict_overflow_p));
12991 default:
12992 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
12993 TREE_TYPE (t));
12995 /* We don't know sign of `t', so be conservative and return false. */
12996 return false;
12999 /* Return true if T is known to be non-negative. If the return
13000 value is based on the assumption that signed overflow is undefined,
13001 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13002 *STRICT_OVERFLOW_P. */
13004 bool
13005 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
13006 tree arg0, tree arg1, bool *strict_overflow_p)
13008 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
13009 switch (DECL_FUNCTION_CODE (fndecl))
13011 CASE_FLT_FN (BUILT_IN_ACOS):
13012 CASE_FLT_FN (BUILT_IN_ACOSH):
13013 CASE_FLT_FN (BUILT_IN_CABS):
13014 CASE_FLT_FN (BUILT_IN_COSH):
13015 CASE_FLT_FN (BUILT_IN_ERFC):
13016 CASE_FLT_FN (BUILT_IN_EXP):
13017 CASE_FLT_FN (BUILT_IN_EXP10):
13018 CASE_FLT_FN (BUILT_IN_EXP2):
13019 CASE_FLT_FN (BUILT_IN_FABS):
13020 CASE_FLT_FN (BUILT_IN_FDIM):
13021 CASE_FLT_FN (BUILT_IN_HYPOT):
13022 CASE_FLT_FN (BUILT_IN_POW10):
13023 CASE_INT_FN (BUILT_IN_FFS):
13024 CASE_INT_FN (BUILT_IN_PARITY):
13025 CASE_INT_FN (BUILT_IN_POPCOUNT):
13026 CASE_INT_FN (BUILT_IN_CLZ):
13027 CASE_INT_FN (BUILT_IN_CLRSB):
13028 case BUILT_IN_BSWAP32:
13029 case BUILT_IN_BSWAP64:
13030 /* Always true. */
13031 return true;
13033 CASE_FLT_FN (BUILT_IN_SQRT):
13034 /* sqrt(-0.0) is -0.0. */
13035 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13036 return true;
13037 return tree_expr_nonnegative_warnv_p (arg0,
13038 strict_overflow_p);
13040 CASE_FLT_FN (BUILT_IN_ASINH):
13041 CASE_FLT_FN (BUILT_IN_ATAN):
13042 CASE_FLT_FN (BUILT_IN_ATANH):
13043 CASE_FLT_FN (BUILT_IN_CBRT):
13044 CASE_FLT_FN (BUILT_IN_CEIL):
13045 CASE_FLT_FN (BUILT_IN_ERF):
13046 CASE_FLT_FN (BUILT_IN_EXPM1):
13047 CASE_FLT_FN (BUILT_IN_FLOOR):
13048 CASE_FLT_FN (BUILT_IN_FMOD):
13049 CASE_FLT_FN (BUILT_IN_FREXP):
13050 CASE_FLT_FN (BUILT_IN_ICEIL):
13051 CASE_FLT_FN (BUILT_IN_IFLOOR):
13052 CASE_FLT_FN (BUILT_IN_IRINT):
13053 CASE_FLT_FN (BUILT_IN_IROUND):
13054 CASE_FLT_FN (BUILT_IN_LCEIL):
13055 CASE_FLT_FN (BUILT_IN_LDEXP):
13056 CASE_FLT_FN (BUILT_IN_LFLOOR):
13057 CASE_FLT_FN (BUILT_IN_LLCEIL):
13058 CASE_FLT_FN (BUILT_IN_LLFLOOR):
13059 CASE_FLT_FN (BUILT_IN_LLRINT):
13060 CASE_FLT_FN (BUILT_IN_LLROUND):
13061 CASE_FLT_FN (BUILT_IN_LRINT):
13062 CASE_FLT_FN (BUILT_IN_LROUND):
13063 CASE_FLT_FN (BUILT_IN_MODF):
13064 CASE_FLT_FN (BUILT_IN_NEARBYINT):
13065 CASE_FLT_FN (BUILT_IN_RINT):
13066 CASE_FLT_FN (BUILT_IN_ROUND):
13067 CASE_FLT_FN (BUILT_IN_SCALB):
13068 CASE_FLT_FN (BUILT_IN_SCALBLN):
13069 CASE_FLT_FN (BUILT_IN_SCALBN):
13070 CASE_FLT_FN (BUILT_IN_SIGNBIT):
13071 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
13072 CASE_FLT_FN (BUILT_IN_SINH):
13073 CASE_FLT_FN (BUILT_IN_TANH):
13074 CASE_FLT_FN (BUILT_IN_TRUNC):
13075 /* True if the 1st argument is nonnegative. */
13076 return tree_expr_nonnegative_warnv_p (arg0,
13077 strict_overflow_p);
13079 CASE_FLT_FN (BUILT_IN_FMAX):
13080 /* True if the 1st OR 2nd arguments are nonnegative. */
13081 return (tree_expr_nonnegative_warnv_p (arg0,
13082 strict_overflow_p)
13083 || (tree_expr_nonnegative_warnv_p (arg1,
13084 strict_overflow_p)));
13086 CASE_FLT_FN (BUILT_IN_FMIN):
13087 /* True if the 1st AND 2nd arguments are nonnegative. */
13088 return (tree_expr_nonnegative_warnv_p (arg0,
13089 strict_overflow_p)
13090 && (tree_expr_nonnegative_warnv_p (arg1,
13091 strict_overflow_p)));
13093 CASE_FLT_FN (BUILT_IN_COPYSIGN):
13094 /* True if the 2nd argument is nonnegative. */
13095 return tree_expr_nonnegative_warnv_p (arg1,
13096 strict_overflow_p);
13098 CASE_FLT_FN (BUILT_IN_POWI):
13099 /* True if the 1st argument is nonnegative or the second
13100 argument is an even integer. */
13101 if (TREE_CODE (arg1) == INTEGER_CST
13102 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13103 return true;
13104 return tree_expr_nonnegative_warnv_p (arg0,
13105 strict_overflow_p);
13107 CASE_FLT_FN (BUILT_IN_POW):
13108 /* True if the 1st argument is nonnegative or the second
13109 argument is an even integer valued real. */
13110 if (TREE_CODE (arg1) == REAL_CST)
13112 REAL_VALUE_TYPE c;
13113 HOST_WIDE_INT n;
13115 c = TREE_REAL_CST (arg1);
13116 n = real_to_integer (&c);
13117 if ((n & 1) == 0)
13119 REAL_VALUE_TYPE cint;
13120 real_from_integer (&cint, VOIDmode, n, SIGNED);
13121 if (real_identical (&c, &cint))
13122 return true;
13125 return tree_expr_nonnegative_warnv_p (arg0,
13126 strict_overflow_p);
13128 default:
13129 break;
13131 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
13132 type);
13135 /* Return true if T is known to be non-negative. If the return
13136 value is based on the assumption that signed overflow is undefined,
13137 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13138 *STRICT_OVERFLOW_P. */
13140 static bool
13141 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
13143 enum tree_code code = TREE_CODE (t);
13144 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13145 return true;
13147 switch (code)
13149 case TARGET_EXPR:
13151 tree temp = TARGET_EXPR_SLOT (t);
13152 t = TARGET_EXPR_INITIAL (t);
13154 /* If the initializer is non-void, then it's a normal expression
13155 that will be assigned to the slot. */
13156 if (!VOID_TYPE_P (t))
13157 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
13159 /* Otherwise, the initializer sets the slot in some way. One common
13160 way is an assignment statement at the end of the initializer. */
13161 while (1)
13163 if (TREE_CODE (t) == BIND_EXPR)
13164 t = expr_last (BIND_EXPR_BODY (t));
13165 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13166 || TREE_CODE (t) == TRY_CATCH_EXPR)
13167 t = expr_last (TREE_OPERAND (t, 0));
13168 else if (TREE_CODE (t) == STATEMENT_LIST)
13169 t = expr_last (t);
13170 else
13171 break;
13173 if (TREE_CODE (t) == MODIFY_EXPR
13174 && TREE_OPERAND (t, 0) == temp)
13175 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13176 strict_overflow_p);
13178 return false;
13181 case CALL_EXPR:
13183 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13184 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13186 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13187 get_callee_fndecl (t),
13188 arg0,
13189 arg1,
13190 strict_overflow_p);
13192 case COMPOUND_EXPR:
13193 case MODIFY_EXPR:
13194 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13195 strict_overflow_p);
13196 case BIND_EXPR:
13197 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
13198 strict_overflow_p);
13199 case SAVE_EXPR:
13200 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13201 strict_overflow_p);
13203 default:
13204 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
13205 TREE_TYPE (t));
13208 /* We don't know sign of `t', so be conservative and return false. */
13209 return false;
13212 /* Return true if T is known to be non-negative. If the return
13213 value is based on the assumption that signed overflow is undefined,
13214 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13215 *STRICT_OVERFLOW_P. */
13217 bool
13218 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
13220 enum tree_code code;
13221 if (t == error_mark_node)
13222 return false;
13224 code = TREE_CODE (t);
13225 switch (TREE_CODE_CLASS (code))
13227 case tcc_binary:
13228 case tcc_comparison:
13229 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13230 TREE_TYPE (t),
13231 TREE_OPERAND (t, 0),
13232 TREE_OPERAND (t, 1),
13233 strict_overflow_p);
13235 case tcc_unary:
13236 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13237 TREE_TYPE (t),
13238 TREE_OPERAND (t, 0),
13239 strict_overflow_p);
13241 case tcc_constant:
13242 case tcc_declaration:
13243 case tcc_reference:
13244 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
13246 default:
13247 break;
13250 switch (code)
13252 case TRUTH_AND_EXPR:
13253 case TRUTH_OR_EXPR:
13254 case TRUTH_XOR_EXPR:
13255 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13256 TREE_TYPE (t),
13257 TREE_OPERAND (t, 0),
13258 TREE_OPERAND (t, 1),
13259 strict_overflow_p);
13260 case TRUTH_NOT_EXPR:
13261 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13262 TREE_TYPE (t),
13263 TREE_OPERAND (t, 0),
13264 strict_overflow_p);
13266 case COND_EXPR:
13267 case CONSTRUCTOR:
13268 case OBJ_TYPE_REF:
13269 case ASSERT_EXPR:
13270 case ADDR_EXPR:
13271 case WITH_SIZE_EXPR:
13272 case SSA_NAME:
13273 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
13275 default:
13276 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
13280 /* Return true if `t' is known to be non-negative. Handle warnings
13281 about undefined signed overflow. */
13283 bool
13284 tree_expr_nonnegative_p (tree t)
13286 bool ret, strict_overflow_p;
13288 strict_overflow_p = false;
13289 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13290 if (strict_overflow_p)
13291 fold_overflow_warning (("assuming signed overflow does not occur when "
13292 "determining that expression is always "
13293 "non-negative"),
13294 WARN_STRICT_OVERFLOW_MISC);
13295 return ret;
13299 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13300 For floating point we further ensure that T is not denormal.
13301 Similar logic is present in nonzero_address in rtlanal.h.
13303 If the return value is based on the assumption that signed overflow
13304 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13305 change *STRICT_OVERFLOW_P. */
13307 bool
13308 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13309 bool *strict_overflow_p)
13311 switch (code)
13313 case ABS_EXPR:
13314 return tree_expr_nonzero_warnv_p (op0,
13315 strict_overflow_p);
13317 case NOP_EXPR:
13319 tree inner_type = TREE_TYPE (op0);
13320 tree outer_type = type;
13322 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13323 && tree_expr_nonzero_warnv_p (op0,
13324 strict_overflow_p));
13326 break;
13328 case NON_LVALUE_EXPR:
13329 return tree_expr_nonzero_warnv_p (op0,
13330 strict_overflow_p);
13332 default:
13333 break;
13336 return false;
13339 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13340 For floating point we further ensure that T is not denormal.
13341 Similar logic is present in nonzero_address in rtlanal.h.
13343 If the return value is based on the assumption that signed overflow
13344 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13345 change *STRICT_OVERFLOW_P. */
13347 bool
13348 tree_binary_nonzero_warnv_p (enum tree_code code,
13349 tree type,
13350 tree op0,
13351 tree op1, bool *strict_overflow_p)
13353 bool sub_strict_overflow_p;
13354 switch (code)
13356 case POINTER_PLUS_EXPR:
13357 case PLUS_EXPR:
13358 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13360 /* With the presence of negative values it is hard
13361 to say something. */
13362 sub_strict_overflow_p = false;
13363 if (!tree_expr_nonnegative_warnv_p (op0,
13364 &sub_strict_overflow_p)
13365 || !tree_expr_nonnegative_warnv_p (op1,
13366 &sub_strict_overflow_p))
13367 return false;
13368 /* One of operands must be positive and the other non-negative. */
13369 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13370 overflows, on a twos-complement machine the sum of two
13371 nonnegative numbers can never be zero. */
13372 return (tree_expr_nonzero_warnv_p (op0,
13373 strict_overflow_p)
13374 || tree_expr_nonzero_warnv_p (op1,
13375 strict_overflow_p));
13377 break;
13379 case MULT_EXPR:
13380 if (TYPE_OVERFLOW_UNDEFINED (type))
13382 if (tree_expr_nonzero_warnv_p (op0,
13383 strict_overflow_p)
13384 && tree_expr_nonzero_warnv_p (op1,
13385 strict_overflow_p))
13387 *strict_overflow_p = true;
13388 return true;
13391 break;
13393 case MIN_EXPR:
13394 sub_strict_overflow_p = false;
13395 if (tree_expr_nonzero_warnv_p (op0,
13396 &sub_strict_overflow_p)
13397 && tree_expr_nonzero_warnv_p (op1,
13398 &sub_strict_overflow_p))
13400 if (sub_strict_overflow_p)
13401 *strict_overflow_p = true;
13403 break;
13405 case MAX_EXPR:
13406 sub_strict_overflow_p = false;
13407 if (tree_expr_nonzero_warnv_p (op0,
13408 &sub_strict_overflow_p))
13410 if (sub_strict_overflow_p)
13411 *strict_overflow_p = true;
13413 /* When both operands are nonzero, then MAX must be too. */
13414 if (tree_expr_nonzero_warnv_p (op1,
13415 strict_overflow_p))
13416 return true;
13418 /* MAX where operand 0 is positive is positive. */
13419 return tree_expr_nonnegative_warnv_p (op0,
13420 strict_overflow_p);
13422 /* MAX where operand 1 is positive is positive. */
13423 else if (tree_expr_nonzero_warnv_p (op1,
13424 &sub_strict_overflow_p)
13425 && tree_expr_nonnegative_warnv_p (op1,
13426 &sub_strict_overflow_p))
13428 if (sub_strict_overflow_p)
13429 *strict_overflow_p = true;
13430 return true;
13432 break;
13434 case BIT_IOR_EXPR:
13435 return (tree_expr_nonzero_warnv_p (op1,
13436 strict_overflow_p)
13437 || tree_expr_nonzero_warnv_p (op0,
13438 strict_overflow_p));
13440 default:
13441 break;
13444 return false;
13447 /* Return true when T is an address and is known to be nonzero.
13448 For floating point we further ensure that T is not denormal.
13449 Similar logic is present in nonzero_address in rtlanal.h.
13451 If the return value is based on the assumption that signed overflow
13452 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13453 change *STRICT_OVERFLOW_P. */
13455 bool
13456 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13458 bool sub_strict_overflow_p;
13459 switch (TREE_CODE (t))
13461 case INTEGER_CST:
13462 return !integer_zerop (t);
13464 case ADDR_EXPR:
13466 tree base = TREE_OPERAND (t, 0);
13468 if (!DECL_P (base))
13469 base = get_base_address (base);
13471 if (!base)
13472 return false;
13474 /* For objects in symbol table check if we know they are non-zero.
13475 Don't do anything for variables and functions before symtab is built;
13476 it is quite possible that they will be declared weak later. */
13477 if (DECL_P (base) && decl_in_symtab_p (base))
13479 struct symtab_node *symbol;
13481 symbol = symtab_node::get_create (base);
13482 if (symbol)
13483 return symbol->nonzero_address ();
13484 else
13485 return false;
13488 /* Function local objects are never NULL. */
13489 if (DECL_P (base)
13490 && (DECL_CONTEXT (base)
13491 && TREE_CODE (DECL_CONTEXT (base)) == FUNCTION_DECL
13492 && auto_var_in_fn_p (base, DECL_CONTEXT (base))))
13493 return true;
13495 /* Constants are never weak. */
13496 if (CONSTANT_CLASS_P (base))
13497 return true;
13499 return false;
13502 case COND_EXPR:
13503 sub_strict_overflow_p = false;
13504 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13505 &sub_strict_overflow_p)
13506 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13507 &sub_strict_overflow_p))
13509 if (sub_strict_overflow_p)
13510 *strict_overflow_p = true;
13511 return true;
13513 break;
13515 default:
13516 break;
13518 return false;
13521 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13522 attempt to fold the expression to a constant without modifying TYPE,
13523 OP0 or OP1.
13525 If the expression could be simplified to a constant, then return
13526 the constant. If the expression would not be simplified to a
13527 constant, then return NULL_TREE. */
13529 tree
13530 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13532 tree tem = fold_binary (code, type, op0, op1);
13533 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13536 /* Given the components of a unary expression CODE, TYPE and OP0,
13537 attempt to fold the expression to a constant without modifying
13538 TYPE or OP0.
13540 If the expression could be simplified to a constant, then return
13541 the constant. If the expression would not be simplified to a
13542 constant, then return NULL_TREE. */
13544 tree
13545 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13547 tree tem = fold_unary (code, type, op0);
13548 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13551 /* If EXP represents referencing an element in a constant string
13552 (either via pointer arithmetic or array indexing), return the
13553 tree representing the value accessed, otherwise return NULL. */
13555 tree
13556 fold_read_from_constant_string (tree exp)
13558 if ((TREE_CODE (exp) == INDIRECT_REF
13559 || TREE_CODE (exp) == ARRAY_REF)
13560 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13562 tree exp1 = TREE_OPERAND (exp, 0);
13563 tree index;
13564 tree string;
13565 location_t loc = EXPR_LOCATION (exp);
13567 if (TREE_CODE (exp) == INDIRECT_REF)
13568 string = string_constant (exp1, &index);
13569 else
13571 tree low_bound = array_ref_low_bound (exp);
13572 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13574 /* Optimize the special-case of a zero lower bound.
13576 We convert the low_bound to sizetype to avoid some problems
13577 with constant folding. (E.g. suppose the lower bound is 1,
13578 and its mode is QI. Without the conversion,l (ARRAY
13579 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13580 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13581 if (! integer_zerop (low_bound))
13582 index = size_diffop_loc (loc, index,
13583 fold_convert_loc (loc, sizetype, low_bound));
13585 string = exp1;
13588 if (string
13589 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13590 && TREE_CODE (string) == STRING_CST
13591 && TREE_CODE (index) == INTEGER_CST
13592 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13593 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
13594 == MODE_INT)
13595 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
13596 return build_int_cst_type (TREE_TYPE (exp),
13597 (TREE_STRING_POINTER (string)
13598 [TREE_INT_CST_LOW (index)]));
13600 return NULL;
13603 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13604 an integer constant, real, or fixed-point constant.
13606 TYPE is the type of the result. */
13608 static tree
13609 fold_negate_const (tree arg0, tree type)
13611 tree t = NULL_TREE;
13613 switch (TREE_CODE (arg0))
13615 case INTEGER_CST:
13617 bool overflow;
13618 wide_int val = wi::neg (arg0, &overflow);
13619 t = force_fit_type (type, val, 1,
13620 (overflow | TREE_OVERFLOW (arg0))
13621 && !TYPE_UNSIGNED (type));
13622 break;
13625 case REAL_CST:
13626 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13627 break;
13629 case FIXED_CST:
13631 FIXED_VALUE_TYPE f;
13632 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13633 &(TREE_FIXED_CST (arg0)), NULL,
13634 TYPE_SATURATING (type));
13635 t = build_fixed (type, f);
13636 /* Propagate overflow flags. */
13637 if (overflow_p | TREE_OVERFLOW (arg0))
13638 TREE_OVERFLOW (t) = 1;
13639 break;
13642 default:
13643 gcc_unreachable ();
13646 return t;
13649 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13650 an integer constant or real constant.
13652 TYPE is the type of the result. */
13654 tree
13655 fold_abs_const (tree arg0, tree type)
13657 tree t = NULL_TREE;
13659 switch (TREE_CODE (arg0))
13661 case INTEGER_CST:
13663 /* If the value is unsigned or non-negative, then the absolute value
13664 is the same as the ordinary value. */
13665 if (!wi::neg_p (arg0, TYPE_SIGN (type)))
13666 t = arg0;
13668 /* If the value is negative, then the absolute value is
13669 its negation. */
13670 else
13672 bool overflow;
13673 wide_int val = wi::neg (arg0, &overflow);
13674 t = force_fit_type (type, val, -1,
13675 overflow | TREE_OVERFLOW (arg0));
13678 break;
13680 case REAL_CST:
13681 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13682 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13683 else
13684 t = arg0;
13685 break;
13687 default:
13688 gcc_unreachable ();
13691 return t;
13694 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13695 constant. TYPE is the type of the result. */
13697 static tree
13698 fold_not_const (const_tree arg0, tree type)
13700 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13702 return force_fit_type (type, wi::bit_not (arg0), 0, TREE_OVERFLOW (arg0));
13705 /* Given CODE, a relational operator, the target type, TYPE and two
13706 constant operands OP0 and OP1, return the result of the
13707 relational operation. If the result is not a compile time
13708 constant, then return NULL_TREE. */
13710 static tree
13711 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13713 int result, invert;
13715 /* From here on, the only cases we handle are when the result is
13716 known to be a constant. */
13718 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13720 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13721 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13723 /* Handle the cases where either operand is a NaN. */
13724 if (real_isnan (c0) || real_isnan (c1))
13726 switch (code)
13728 case EQ_EXPR:
13729 case ORDERED_EXPR:
13730 result = 0;
13731 break;
13733 case NE_EXPR:
13734 case UNORDERED_EXPR:
13735 case UNLT_EXPR:
13736 case UNLE_EXPR:
13737 case UNGT_EXPR:
13738 case UNGE_EXPR:
13739 case UNEQ_EXPR:
13740 result = 1;
13741 break;
13743 case LT_EXPR:
13744 case LE_EXPR:
13745 case GT_EXPR:
13746 case GE_EXPR:
13747 case LTGT_EXPR:
13748 if (flag_trapping_math)
13749 return NULL_TREE;
13750 result = 0;
13751 break;
13753 default:
13754 gcc_unreachable ();
13757 return constant_boolean_node (result, type);
13760 return constant_boolean_node (real_compare (code, c0, c1), type);
13763 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13765 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13766 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13767 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13770 /* Handle equality/inequality of complex constants. */
13771 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13773 tree rcond = fold_relational_const (code, type,
13774 TREE_REALPART (op0),
13775 TREE_REALPART (op1));
13776 tree icond = fold_relational_const (code, type,
13777 TREE_IMAGPART (op0),
13778 TREE_IMAGPART (op1));
13779 if (code == EQ_EXPR)
13780 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13781 else if (code == NE_EXPR)
13782 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13783 else
13784 return NULL_TREE;
13787 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13789 unsigned count = VECTOR_CST_NELTS (op0);
13790 tree *elts = XALLOCAVEC (tree, count);
13791 gcc_assert (VECTOR_CST_NELTS (op1) == count
13792 && TYPE_VECTOR_SUBPARTS (type) == count);
13794 for (unsigned i = 0; i < count; i++)
13796 tree elem_type = TREE_TYPE (type);
13797 tree elem0 = VECTOR_CST_ELT (op0, i);
13798 tree elem1 = VECTOR_CST_ELT (op1, i);
13800 tree tem = fold_relational_const (code, elem_type,
13801 elem0, elem1);
13803 if (tem == NULL_TREE)
13804 return NULL_TREE;
13806 elts[i] = build_int_cst (elem_type, integer_zerop (tem) ? 0 : -1);
13809 return build_vector (type, elts);
13812 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13814 To compute GT, swap the arguments and do LT.
13815 To compute GE, do LT and invert the result.
13816 To compute LE, swap the arguments, do LT and invert the result.
13817 To compute NE, do EQ and invert the result.
13819 Therefore, the code below must handle only EQ and LT. */
13821 if (code == LE_EXPR || code == GT_EXPR)
13823 std::swap (op0, op1);
13824 code = swap_tree_comparison (code);
13827 /* Note that it is safe to invert for real values here because we
13828 have already handled the one case that it matters. */
13830 invert = 0;
13831 if (code == NE_EXPR || code == GE_EXPR)
13833 invert = 1;
13834 code = invert_tree_comparison (code, false);
13837 /* Compute a result for LT or EQ if args permit;
13838 Otherwise return T. */
13839 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13841 if (code == EQ_EXPR)
13842 result = tree_int_cst_equal (op0, op1);
13843 else
13844 result = tree_int_cst_lt (op0, op1);
13846 else
13847 return NULL_TREE;
13849 if (invert)
13850 result ^= 1;
13851 return constant_boolean_node (result, type);
13854 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
13855 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
13856 itself. */
13858 tree
13859 fold_build_cleanup_point_expr (tree type, tree expr)
13861 /* If the expression does not have side effects then we don't have to wrap
13862 it with a cleanup point expression. */
13863 if (!TREE_SIDE_EFFECTS (expr))
13864 return expr;
13866 /* If the expression is a return, check to see if the expression inside the
13867 return has no side effects or the right hand side of the modify expression
13868 inside the return. If either don't have side effects set we don't need to
13869 wrap the expression in a cleanup point expression. Note we don't check the
13870 left hand side of the modify because it should always be a return decl. */
13871 if (TREE_CODE (expr) == RETURN_EXPR)
13873 tree op = TREE_OPERAND (expr, 0);
13874 if (!op || !TREE_SIDE_EFFECTS (op))
13875 return expr;
13876 op = TREE_OPERAND (op, 1);
13877 if (!TREE_SIDE_EFFECTS (op))
13878 return expr;
13881 return build1 (CLEANUP_POINT_EXPR, type, expr);
13884 /* Given a pointer value OP0 and a type TYPE, return a simplified version
13885 of an indirection through OP0, or NULL_TREE if no simplification is
13886 possible. */
13888 tree
13889 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
13891 tree sub = op0;
13892 tree subtype;
13894 STRIP_NOPS (sub);
13895 subtype = TREE_TYPE (sub);
13896 if (!POINTER_TYPE_P (subtype))
13897 return NULL_TREE;
13899 if (TREE_CODE (sub) == ADDR_EXPR)
13901 tree op = TREE_OPERAND (sub, 0);
13902 tree optype = TREE_TYPE (op);
13903 /* *&CONST_DECL -> to the value of the const decl. */
13904 if (TREE_CODE (op) == CONST_DECL)
13905 return DECL_INITIAL (op);
13906 /* *&p => p; make sure to handle *&"str"[cst] here. */
13907 if (type == optype)
13909 tree fop = fold_read_from_constant_string (op);
13910 if (fop)
13911 return fop;
13912 else
13913 return op;
13915 /* *(foo *)&fooarray => fooarray[0] */
13916 else if (TREE_CODE (optype) == ARRAY_TYPE
13917 && type == TREE_TYPE (optype)
13918 && (!in_gimple_form
13919 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
13921 tree type_domain = TYPE_DOMAIN (optype);
13922 tree min_val = size_zero_node;
13923 if (type_domain && TYPE_MIN_VALUE (type_domain))
13924 min_val = TYPE_MIN_VALUE (type_domain);
13925 if (in_gimple_form
13926 && TREE_CODE (min_val) != INTEGER_CST)
13927 return NULL_TREE;
13928 return build4_loc (loc, ARRAY_REF, type, op, min_val,
13929 NULL_TREE, NULL_TREE);
13931 /* *(foo *)&complexfoo => __real__ complexfoo */
13932 else if (TREE_CODE (optype) == COMPLEX_TYPE
13933 && type == TREE_TYPE (optype))
13934 return fold_build1_loc (loc, REALPART_EXPR, type, op);
13935 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
13936 else if (TREE_CODE (optype) == VECTOR_TYPE
13937 && type == TREE_TYPE (optype))
13939 tree part_width = TYPE_SIZE (type);
13940 tree index = bitsize_int (0);
13941 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
13945 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
13946 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
13948 tree op00 = TREE_OPERAND (sub, 0);
13949 tree op01 = TREE_OPERAND (sub, 1);
13951 STRIP_NOPS (op00);
13952 if (TREE_CODE (op00) == ADDR_EXPR)
13954 tree op00type;
13955 op00 = TREE_OPERAND (op00, 0);
13956 op00type = TREE_TYPE (op00);
13958 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
13959 if (TREE_CODE (op00type) == VECTOR_TYPE
13960 && type == TREE_TYPE (op00type))
13962 HOST_WIDE_INT offset = tree_to_shwi (op01);
13963 tree part_width = TYPE_SIZE (type);
13964 unsigned HOST_WIDE_INT part_widthi = tree_to_shwi (part_width)/BITS_PER_UNIT;
13965 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
13966 tree index = bitsize_int (indexi);
13968 if (offset / part_widthi < TYPE_VECTOR_SUBPARTS (op00type))
13969 return fold_build3_loc (loc,
13970 BIT_FIELD_REF, type, op00,
13971 part_width, index);
13974 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
13975 else if (TREE_CODE (op00type) == COMPLEX_TYPE
13976 && type == TREE_TYPE (op00type))
13978 tree size = TYPE_SIZE_UNIT (type);
13979 if (tree_int_cst_equal (size, op01))
13980 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
13982 /* ((foo *)&fooarray)[1] => fooarray[1] */
13983 else if (TREE_CODE (op00type) == ARRAY_TYPE
13984 && type == TREE_TYPE (op00type))
13986 tree type_domain = TYPE_DOMAIN (op00type);
13987 tree min_val = size_zero_node;
13988 if (type_domain && TYPE_MIN_VALUE (type_domain))
13989 min_val = TYPE_MIN_VALUE (type_domain);
13990 op01 = size_binop_loc (loc, EXACT_DIV_EXPR, op01,
13991 TYPE_SIZE_UNIT (type));
13992 op01 = size_binop_loc (loc, PLUS_EXPR, op01, min_val);
13993 return build4_loc (loc, ARRAY_REF, type, op00, op01,
13994 NULL_TREE, NULL_TREE);
13999 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14000 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14001 && type == TREE_TYPE (TREE_TYPE (subtype))
14002 && (!in_gimple_form
14003 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14005 tree type_domain;
14006 tree min_val = size_zero_node;
14007 sub = build_fold_indirect_ref_loc (loc, sub);
14008 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14009 if (type_domain && TYPE_MIN_VALUE (type_domain))
14010 min_val = TYPE_MIN_VALUE (type_domain);
14011 if (in_gimple_form
14012 && TREE_CODE (min_val) != INTEGER_CST)
14013 return NULL_TREE;
14014 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14015 NULL_TREE);
14018 return NULL_TREE;
14021 /* Builds an expression for an indirection through T, simplifying some
14022 cases. */
14024 tree
14025 build_fold_indirect_ref_loc (location_t loc, tree t)
14027 tree type = TREE_TYPE (TREE_TYPE (t));
14028 tree sub = fold_indirect_ref_1 (loc, type, t);
14030 if (sub)
14031 return sub;
14033 return build1_loc (loc, INDIRECT_REF, type, t);
14036 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14038 tree
14039 fold_indirect_ref_loc (location_t loc, tree t)
14041 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14043 if (sub)
14044 return sub;
14045 else
14046 return t;
14049 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14050 whose result is ignored. The type of the returned tree need not be
14051 the same as the original expression. */
14053 tree
14054 fold_ignored_result (tree t)
14056 if (!TREE_SIDE_EFFECTS (t))
14057 return integer_zero_node;
14059 for (;;)
14060 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14062 case tcc_unary:
14063 t = TREE_OPERAND (t, 0);
14064 break;
14066 case tcc_binary:
14067 case tcc_comparison:
14068 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14069 t = TREE_OPERAND (t, 0);
14070 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14071 t = TREE_OPERAND (t, 1);
14072 else
14073 return t;
14074 break;
14076 case tcc_expression:
14077 switch (TREE_CODE (t))
14079 case COMPOUND_EXPR:
14080 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14081 return t;
14082 t = TREE_OPERAND (t, 0);
14083 break;
14085 case COND_EXPR:
14086 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14087 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14088 return t;
14089 t = TREE_OPERAND (t, 0);
14090 break;
14092 default:
14093 return t;
14095 break;
14097 default:
14098 return t;
14102 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14104 tree
14105 round_up_loc (location_t loc, tree value, unsigned int divisor)
14107 tree div = NULL_TREE;
14109 if (divisor == 1)
14110 return value;
14112 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14113 have to do anything. Only do this when we are not given a const,
14114 because in that case, this check is more expensive than just
14115 doing it. */
14116 if (TREE_CODE (value) != INTEGER_CST)
14118 div = build_int_cst (TREE_TYPE (value), divisor);
14120 if (multiple_of_p (TREE_TYPE (value), value, div))
14121 return value;
14124 /* If divisor is a power of two, simplify this to bit manipulation. */
14125 if (divisor == (divisor & -divisor))
14127 if (TREE_CODE (value) == INTEGER_CST)
14129 wide_int val = value;
14130 bool overflow_p;
14132 if ((val & (divisor - 1)) == 0)
14133 return value;
14135 overflow_p = TREE_OVERFLOW (value);
14136 val += divisor - 1;
14137 val &= - (int) divisor;
14138 if (val == 0)
14139 overflow_p = true;
14141 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14143 else
14145 tree t;
14147 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14148 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14149 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14150 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14153 else
14155 if (!div)
14156 div = build_int_cst (TREE_TYPE (value), divisor);
14157 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14158 value = size_binop_loc (loc, MULT_EXPR, value, div);
14161 return value;
14164 /* Likewise, but round down. */
14166 tree
14167 round_down_loc (location_t loc, tree value, int divisor)
14169 tree div = NULL_TREE;
14171 gcc_assert (divisor > 0);
14172 if (divisor == 1)
14173 return value;
14175 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14176 have to do anything. Only do this when we are not given a const,
14177 because in that case, this check is more expensive than just
14178 doing it. */
14179 if (TREE_CODE (value) != INTEGER_CST)
14181 div = build_int_cst (TREE_TYPE (value), divisor);
14183 if (multiple_of_p (TREE_TYPE (value), value, div))
14184 return value;
14187 /* If divisor is a power of two, simplify this to bit manipulation. */
14188 if (divisor == (divisor & -divisor))
14190 tree t;
14192 t = build_int_cst (TREE_TYPE (value), -divisor);
14193 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14195 else
14197 if (!div)
14198 div = build_int_cst (TREE_TYPE (value), divisor);
14199 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14200 value = size_binop_loc (loc, MULT_EXPR, value, div);
14203 return value;
14206 /* Returns the pointer to the base of the object addressed by EXP and
14207 extracts the information about the offset of the access, storing it
14208 to PBITPOS and POFFSET. */
14210 static tree
14211 split_address_to_core_and_offset (tree exp,
14212 HOST_WIDE_INT *pbitpos, tree *poffset)
14214 tree core;
14215 machine_mode mode;
14216 int unsignedp, volatilep;
14217 HOST_WIDE_INT bitsize;
14218 location_t loc = EXPR_LOCATION (exp);
14220 if (TREE_CODE (exp) == ADDR_EXPR)
14222 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14223 poffset, &mode, &unsignedp, &volatilep,
14224 false);
14225 core = build_fold_addr_expr_loc (loc, core);
14227 else
14229 core = exp;
14230 *pbitpos = 0;
14231 *poffset = NULL_TREE;
14234 return core;
14237 /* Returns true if addresses of E1 and E2 differ by a constant, false
14238 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14240 bool
14241 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14243 tree core1, core2;
14244 HOST_WIDE_INT bitpos1, bitpos2;
14245 tree toffset1, toffset2, tdiff, type;
14247 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14248 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14250 if (bitpos1 % BITS_PER_UNIT != 0
14251 || bitpos2 % BITS_PER_UNIT != 0
14252 || !operand_equal_p (core1, core2, 0))
14253 return false;
14255 if (toffset1 && toffset2)
14257 type = TREE_TYPE (toffset1);
14258 if (type != TREE_TYPE (toffset2))
14259 toffset2 = fold_convert (type, toffset2);
14261 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14262 if (!cst_and_fits_in_hwi (tdiff))
14263 return false;
14265 *diff = int_cst_value (tdiff);
14267 else if (toffset1 || toffset2)
14269 /* If only one of the offsets is non-constant, the difference cannot
14270 be a constant. */
14271 return false;
14273 else
14274 *diff = 0;
14276 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
14277 return true;
14280 /* Simplify the floating point expression EXP when the sign of the
14281 result is not significant. Return NULL_TREE if no simplification
14282 is possible. */
14284 tree
14285 fold_strip_sign_ops (tree exp)
14287 tree arg0, arg1;
14288 location_t loc = EXPR_LOCATION (exp);
14290 switch (TREE_CODE (exp))
14292 case ABS_EXPR:
14293 case NEGATE_EXPR:
14294 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
14295 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
14297 case MULT_EXPR:
14298 case RDIV_EXPR:
14299 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (exp)))
14300 return NULL_TREE;
14301 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
14302 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
14303 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
14304 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
14305 arg0 ? arg0 : TREE_OPERAND (exp, 0),
14306 arg1 ? arg1 : TREE_OPERAND (exp, 1));
14307 break;
14309 case COMPOUND_EXPR:
14310 arg0 = TREE_OPERAND (exp, 0);
14311 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
14312 if (arg1)
14313 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
14314 break;
14316 case COND_EXPR:
14317 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
14318 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
14319 if (arg0 || arg1)
14320 return fold_build3_loc (loc,
14321 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
14322 arg0 ? arg0 : TREE_OPERAND (exp, 1),
14323 arg1 ? arg1 : TREE_OPERAND (exp, 2));
14324 break;
14326 case CALL_EXPR:
14328 const enum built_in_function fcode = builtin_mathfn_code (exp);
14329 switch (fcode)
14331 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14332 /* Strip copysign function call, return the 1st argument. */
14333 arg0 = CALL_EXPR_ARG (exp, 0);
14334 arg1 = CALL_EXPR_ARG (exp, 1);
14335 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
14337 default:
14338 /* Strip sign ops from the argument of "odd" math functions. */
14339 if (negate_mathfn_p (fcode))
14341 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
14342 if (arg0)
14343 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);
14345 break;
14348 break;
14350 default:
14351 break;
14353 return NULL_TREE;
14356 /* Return OFF converted to a pointer offset type suitable as offset for
14357 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14358 tree
14359 convert_to_ptrofftype_loc (location_t loc, tree off)
14361 return fold_convert_loc (loc, sizetype, off);
14364 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14365 tree
14366 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14368 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14369 ptr, convert_to_ptrofftype_loc (loc, off));
14372 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14373 tree
14374 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14376 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14377 ptr, size_int (off));