Wattributes-10.c: Add -fno-common option on hppa*-*-hpux*.
[official-gcc.git] / gcc / fold-const.c
blobec28b431eb8f499988deff99d73dc8bef9f6378c
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2019 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
43 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "backend.h"
47 #include "target.h"
48 #include "rtl.h"
49 #include "tree.h"
50 #include "gimple.h"
51 #include "predict.h"
52 #include "memmodel.h"
53 #include "tm_p.h"
54 #include "tree-ssa-operands.h"
55 #include "optabs-query.h"
56 #include "cgraph.h"
57 #include "diagnostic-core.h"
58 #include "flags.h"
59 #include "alias.h"
60 #include "fold-const.h"
61 #include "fold-const-call.h"
62 #include "stor-layout.h"
63 #include "calls.h"
64 #include "tree-iterator.h"
65 #include "expr.h"
66 #include "intl.h"
67 #include "langhooks.h"
68 #include "tree-eh.h"
69 #include "gimplify.h"
70 #include "tree-dfa.h"
71 #include "builtins.h"
72 #include "generic-match.h"
73 #include "gimple-fold.h"
74 #include "params.h"
75 #include "tree-into-ssa.h"
76 #include "md5.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
79 #include "tree-vrp.h"
80 #include "tree-ssanames.h"
81 #include "selftest.h"
82 #include "stringpool.h"
83 #include "attribs.h"
84 #include "tree-vector-builder.h"
85 #include "vec-perm-indices.h"
87 /* Nonzero if we are folding constants inside an initializer; zero
88 otherwise. */
89 int folding_initializer = 0;
91 /* The following constants represent a bit based encoding of GCC's
92 comparison operators. This encoding simplifies transformations
93 on relational comparison operators, such as AND and OR. */
94 enum comparison_code {
95 COMPCODE_FALSE = 0,
96 COMPCODE_LT = 1,
97 COMPCODE_EQ = 2,
98 COMPCODE_LE = 3,
99 COMPCODE_GT = 4,
100 COMPCODE_LTGT = 5,
101 COMPCODE_GE = 6,
102 COMPCODE_ORD = 7,
103 COMPCODE_UNORD = 8,
104 COMPCODE_UNLT = 9,
105 COMPCODE_UNEQ = 10,
106 COMPCODE_UNLE = 11,
107 COMPCODE_UNGT = 12,
108 COMPCODE_NE = 13,
109 COMPCODE_UNGE = 14,
110 COMPCODE_TRUE = 15
113 static bool negate_expr_p (tree);
114 static tree negate_expr (tree);
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 twoval_comparison_p (tree, tree *, tree *);
119 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
120 static tree optimize_bit_field_compare (location_t, enum tree_code,
121 tree, tree, tree);
122 static int simple_operand_p (const_tree);
123 static bool simple_operand_p_2 (tree);
124 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
125 static tree range_predecessor (tree);
126 static tree range_successor (tree);
127 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
128 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
129 static tree unextend (tree, int, int, tree);
130 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
131 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
132 static tree fold_binary_op_with_conditional_arg (location_t,
133 enum tree_code, tree,
134 tree, tree,
135 tree, tree, int);
136 static tree fold_negate_const (tree, tree);
137 static tree fold_not_const (const_tree, tree);
138 static tree fold_relational_const (enum tree_code, tree, tree, tree);
139 static tree fold_convert_const (enum tree_code, tree, tree);
140 static tree fold_view_convert_expr (tree, tree);
141 static tree fold_negate_expr (location_t, tree);
144 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
145 Otherwise, return LOC. */
147 static location_t
148 expr_location_or (tree t, location_t loc)
150 location_t tloc = EXPR_LOCATION (t);
151 return tloc == UNKNOWN_LOCATION ? loc : tloc;
154 /* Similar to protected_set_expr_location, but never modify x in place,
155 if location can and needs to be set, unshare it. */
157 static inline tree
158 protected_set_expr_location_unshare (tree x, location_t loc)
160 if (CAN_HAVE_LOCATION_P (x)
161 && EXPR_LOCATION (x) != loc
162 && !(TREE_CODE (x) == SAVE_EXPR
163 || TREE_CODE (x) == TARGET_EXPR
164 || TREE_CODE (x) == BIND_EXPR))
166 x = copy_node (x);
167 SET_EXPR_LOCATION (x, loc);
169 return x;
172 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
173 division and returns the quotient. Otherwise returns
174 NULL_TREE. */
176 tree
177 div_if_zero_remainder (const_tree arg1, const_tree arg2)
179 widest_int quo;
181 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
182 SIGNED, &quo))
183 return wide_int_to_tree (TREE_TYPE (arg1), quo);
185 return NULL_TREE;
188 /* This is nonzero if we should defer warnings about undefined
189 overflow. This facility exists because these warnings are a
190 special case. The code to estimate loop iterations does not want
191 to issue any warnings, since it works with expressions which do not
192 occur in user code. Various bits of cleanup code call fold(), but
193 only use the result if it has certain characteristics (e.g., is a
194 constant); that code only wants to issue a warning if the result is
195 used. */
197 static int fold_deferring_overflow_warnings;
199 /* If a warning about undefined overflow is deferred, this is the
200 warning. Note that this may cause us to turn two warnings into
201 one, but that is fine since it is sufficient to only give one
202 warning per expression. */
204 static const char* fold_deferred_overflow_warning;
206 /* If a warning about undefined overflow is deferred, this is the
207 level at which the warning should be emitted. */
209 static enum warn_strict_overflow_code fold_deferred_overflow_code;
211 /* Start deferring overflow warnings. We could use a stack here to
212 permit nested calls, but at present it is not necessary. */
214 void
215 fold_defer_overflow_warnings (void)
217 ++fold_deferring_overflow_warnings;
220 /* Stop deferring overflow warnings. If there is a pending warning,
221 and ISSUE is true, then issue the warning if appropriate. STMT is
222 the statement with which the warning should be associated (used for
223 location information); STMT may be NULL. CODE is the level of the
224 warning--a warn_strict_overflow_code value. This function will use
225 the smaller of CODE and the deferred code when deciding whether to
226 issue the warning. CODE may be zero to mean to always use the
227 deferred code. */
229 void
230 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
232 const char *warnmsg;
233 location_t locus;
235 gcc_assert (fold_deferring_overflow_warnings > 0);
236 --fold_deferring_overflow_warnings;
237 if (fold_deferring_overflow_warnings > 0)
239 if (fold_deferred_overflow_warning != NULL
240 && code != 0
241 && code < (int) fold_deferred_overflow_code)
242 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
243 return;
246 warnmsg = fold_deferred_overflow_warning;
247 fold_deferred_overflow_warning = NULL;
249 if (!issue || warnmsg == NULL)
250 return;
252 if (gimple_no_warning_p (stmt))
253 return;
255 /* Use the smallest code level when deciding to issue the
256 warning. */
257 if (code == 0 || code > (int) fold_deferred_overflow_code)
258 code = fold_deferred_overflow_code;
260 if (!issue_strict_overflow_warning (code))
261 return;
263 if (stmt == NULL)
264 locus = input_location;
265 else
266 locus = gimple_location (stmt);
267 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
270 /* Stop deferring overflow warnings, ignoring any deferred
271 warnings. */
273 void
274 fold_undefer_and_ignore_overflow_warnings (void)
276 fold_undefer_overflow_warnings (false, NULL, 0);
279 /* Whether we are deferring overflow warnings. */
281 bool
282 fold_deferring_overflow_warnings_p (void)
284 return fold_deferring_overflow_warnings > 0;
287 /* This is called when we fold something based on the fact that signed
288 overflow is undefined. */
290 void
291 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
293 if (fold_deferring_overflow_warnings > 0)
295 if (fold_deferred_overflow_warning == NULL
296 || wc < fold_deferred_overflow_code)
298 fold_deferred_overflow_warning = gmsgid;
299 fold_deferred_overflow_code = wc;
302 else if (issue_strict_overflow_warning (wc))
303 warning (OPT_Wstrict_overflow, gmsgid);
306 /* Return true if the built-in mathematical function specified by CODE
307 is odd, i.e. -f(x) == f(-x). */
309 bool
310 negate_mathfn_p (combined_fn fn)
312 switch (fn)
314 CASE_CFN_ASIN:
315 CASE_CFN_ASINH:
316 CASE_CFN_ATAN:
317 CASE_CFN_ATANH:
318 CASE_CFN_CASIN:
319 CASE_CFN_CASINH:
320 CASE_CFN_CATAN:
321 CASE_CFN_CATANH:
322 CASE_CFN_CBRT:
323 CASE_CFN_CPROJ:
324 CASE_CFN_CSIN:
325 CASE_CFN_CSINH:
326 CASE_CFN_CTAN:
327 CASE_CFN_CTANH:
328 CASE_CFN_ERF:
329 CASE_CFN_LLROUND:
330 CASE_CFN_LROUND:
331 CASE_CFN_ROUND:
332 CASE_CFN_SIN:
333 CASE_CFN_SINH:
334 CASE_CFN_TAN:
335 CASE_CFN_TANH:
336 CASE_CFN_TRUNC:
337 return true;
339 CASE_CFN_LLRINT:
340 CASE_CFN_LRINT:
341 CASE_CFN_NEARBYINT:
342 CASE_CFN_RINT:
343 return !flag_rounding_math;
345 default:
346 break;
348 return false;
351 /* Check whether we may negate an integer constant T without causing
352 overflow. */
354 bool
355 may_negate_without_overflow_p (const_tree t)
357 tree type;
359 gcc_assert (TREE_CODE (t) == INTEGER_CST);
361 type = TREE_TYPE (t);
362 if (TYPE_UNSIGNED (type))
363 return false;
365 return !wi::only_sign_bit_p (wi::to_wide (t));
368 /* Determine whether an expression T can be cheaply negated using
369 the function negate_expr without introducing undefined overflow. */
371 static bool
372 negate_expr_p (tree t)
374 tree type;
376 if (t == 0)
377 return false;
379 type = TREE_TYPE (t);
381 STRIP_SIGN_NOPS (t);
382 switch (TREE_CODE (t))
384 case INTEGER_CST:
385 if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
386 return true;
388 /* Check that -CST will not overflow type. */
389 return may_negate_without_overflow_p (t);
390 case BIT_NOT_EXPR:
391 return (INTEGRAL_TYPE_P (type)
392 && TYPE_OVERFLOW_WRAPS (type));
394 case FIXED_CST:
395 return true;
397 case NEGATE_EXPR:
398 return !TYPE_OVERFLOW_SANITIZED (type);
400 case REAL_CST:
401 /* We want to canonicalize to positive real constants. Pretend
402 that only negative ones can be easily negated. */
403 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
405 case COMPLEX_CST:
406 return negate_expr_p (TREE_REALPART (t))
407 && negate_expr_p (TREE_IMAGPART (t));
409 case VECTOR_CST:
411 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
412 return true;
414 /* Steps don't prevent negation. */
415 unsigned int count = vector_cst_encoded_nelts (t);
416 for (unsigned int i = 0; i < count; ++i)
417 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
418 return false;
420 return true;
423 case COMPLEX_EXPR:
424 return negate_expr_p (TREE_OPERAND (t, 0))
425 && negate_expr_p (TREE_OPERAND (t, 1));
427 case CONJ_EXPR:
428 return negate_expr_p (TREE_OPERAND (t, 0));
430 case PLUS_EXPR:
431 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
432 || HONOR_SIGNED_ZEROS (element_mode (type))
433 || (ANY_INTEGRAL_TYPE_P (type)
434 && ! TYPE_OVERFLOW_WRAPS (type)))
435 return false;
436 /* -(A + B) -> (-B) - A. */
437 if (negate_expr_p (TREE_OPERAND (t, 1)))
438 return true;
439 /* -(A + B) -> (-A) - B. */
440 return negate_expr_p (TREE_OPERAND (t, 0));
442 case MINUS_EXPR:
443 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
444 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
445 && !HONOR_SIGNED_ZEROS (element_mode (type))
446 && (! ANY_INTEGRAL_TYPE_P (type)
447 || TYPE_OVERFLOW_WRAPS (type));
449 case MULT_EXPR:
450 if (TYPE_UNSIGNED (type))
451 break;
452 /* INT_MIN/n * n doesn't overflow while negating one operand it does
453 if n is a (negative) power of two. */
454 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
455 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
456 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
457 && (wi::popcount
458 (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
459 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
460 && (wi::popcount
461 (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
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 if (TYPE_UNSIGNED (type))
476 break;
477 /* In general we can't negate A in A / B, because if A is INT_MIN and
478 B is not 1 we change the sign of the result. */
479 if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
480 && negate_expr_p (TREE_OPERAND (t, 0)))
481 return true;
482 /* In general we can't negate B in A / B, because if A is INT_MIN and
483 B is 1, we may turn this into INT_MIN / -1 which is undefined
484 and actually traps on some architectures. */
485 if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
486 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
487 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
488 && ! integer_onep (TREE_OPERAND (t, 1))))
489 return negate_expr_p (TREE_OPERAND (t, 1));
490 break;
492 case NOP_EXPR:
493 /* Negate -((double)float) as (double)(-float). */
494 if (TREE_CODE (type) == REAL_TYPE)
496 tree tem = strip_float_extensions (t);
497 if (tem != t)
498 return negate_expr_p (tem);
500 break;
502 case CALL_EXPR:
503 /* Negate -f(x) as f(-x). */
504 if (negate_mathfn_p (get_call_combined_fn (t)))
505 return negate_expr_p (CALL_EXPR_ARG (t, 0));
506 break;
508 case RSHIFT_EXPR:
509 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
510 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
512 tree op1 = TREE_OPERAND (t, 1);
513 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
514 return true;
516 break;
518 default:
519 break;
521 return false;
524 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
525 simplification is possible.
526 If negate_expr_p would return true for T, NULL_TREE will never be
527 returned. */
529 static tree
530 fold_negate_expr_1 (location_t loc, tree t)
532 tree type = TREE_TYPE (t);
533 tree tem;
535 switch (TREE_CODE (t))
537 /* Convert - (~A) to A + 1. */
538 case BIT_NOT_EXPR:
539 if (INTEGRAL_TYPE_P (type))
540 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
541 build_one_cst (type));
542 break;
544 case INTEGER_CST:
545 tem = fold_negate_const (t, type);
546 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
547 || (ANY_INTEGRAL_TYPE_P (type)
548 && !TYPE_OVERFLOW_TRAPS (type)
549 && TYPE_OVERFLOW_WRAPS (type))
550 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
551 return tem;
552 break;
554 case POLY_INT_CST:
555 case REAL_CST:
556 case FIXED_CST:
557 tem = fold_negate_const (t, type);
558 return tem;
560 case COMPLEX_CST:
562 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
563 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
564 if (rpart && ipart)
565 return build_complex (type, rpart, ipart);
567 break;
569 case VECTOR_CST:
571 tree_vector_builder elts;
572 elts.new_unary_operation (type, t, true);
573 unsigned int count = elts.encoded_nelts ();
574 for (unsigned int i = 0; i < count; ++i)
576 tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
577 if (elt == NULL_TREE)
578 return NULL_TREE;
579 elts.quick_push (elt);
582 return elts.build ();
585 case COMPLEX_EXPR:
586 if (negate_expr_p (t))
587 return fold_build2_loc (loc, COMPLEX_EXPR, type,
588 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
589 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
590 break;
592 case CONJ_EXPR:
593 if (negate_expr_p (t))
594 return fold_build1_loc (loc, CONJ_EXPR, type,
595 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
596 break;
598 case NEGATE_EXPR:
599 if (!TYPE_OVERFLOW_SANITIZED (type))
600 return TREE_OPERAND (t, 0);
601 break;
603 case PLUS_EXPR:
604 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
605 && !HONOR_SIGNED_ZEROS (element_mode (type)))
607 /* -(A + B) -> (-B) - A. */
608 if (negate_expr_p (TREE_OPERAND (t, 1)))
610 tem = negate_expr (TREE_OPERAND (t, 1));
611 return fold_build2_loc (loc, MINUS_EXPR, type,
612 tem, TREE_OPERAND (t, 0));
615 /* -(A + B) -> (-A) - B. */
616 if (negate_expr_p (TREE_OPERAND (t, 0)))
618 tem = negate_expr (TREE_OPERAND (t, 0));
619 return fold_build2_loc (loc, MINUS_EXPR, type,
620 tem, TREE_OPERAND (t, 1));
623 break;
625 case MINUS_EXPR:
626 /* - (A - B) -> B - A */
627 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
628 && !HONOR_SIGNED_ZEROS (element_mode (type)))
629 return fold_build2_loc (loc, MINUS_EXPR, type,
630 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
631 break;
633 case MULT_EXPR:
634 if (TYPE_UNSIGNED (type))
635 break;
637 /* Fall through. */
639 case RDIV_EXPR:
640 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
642 tem = TREE_OPERAND (t, 1);
643 if (negate_expr_p (tem))
644 return fold_build2_loc (loc, TREE_CODE (t), type,
645 TREE_OPERAND (t, 0), negate_expr (tem));
646 tem = TREE_OPERAND (t, 0);
647 if (negate_expr_p (tem))
648 return fold_build2_loc (loc, TREE_CODE (t), type,
649 negate_expr (tem), TREE_OPERAND (t, 1));
651 break;
653 case TRUNC_DIV_EXPR:
654 case ROUND_DIV_EXPR:
655 case EXACT_DIV_EXPR:
656 if (TYPE_UNSIGNED (type))
657 break;
658 /* In general we can't negate A in A / B, because if A is INT_MIN and
659 B is not 1 we change the sign of the result. */
660 if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
661 && negate_expr_p (TREE_OPERAND (t, 0)))
662 return fold_build2_loc (loc, TREE_CODE (t), type,
663 negate_expr (TREE_OPERAND (t, 0)),
664 TREE_OPERAND (t, 1));
665 /* In general we can't negate B in A / B, because if A is INT_MIN and
666 B is 1, we may turn this into INT_MIN / -1 which is undefined
667 and actually traps on some architectures. */
668 if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
669 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
670 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
671 && ! integer_onep (TREE_OPERAND (t, 1))))
672 && negate_expr_p (TREE_OPERAND (t, 1)))
673 return fold_build2_loc (loc, TREE_CODE (t), type,
674 TREE_OPERAND (t, 0),
675 negate_expr (TREE_OPERAND (t, 1)));
676 break;
678 case NOP_EXPR:
679 /* Convert -((double)float) into (double)(-float). */
680 if (TREE_CODE (type) == REAL_TYPE)
682 tem = strip_float_extensions (t);
683 if (tem != t && negate_expr_p (tem))
684 return fold_convert_loc (loc, type, negate_expr (tem));
686 break;
688 case CALL_EXPR:
689 /* Negate -f(x) as f(-x). */
690 if (negate_mathfn_p (get_call_combined_fn (t))
691 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
693 tree fndecl, arg;
695 fndecl = get_callee_fndecl (t);
696 arg = negate_expr (CALL_EXPR_ARG (t, 0));
697 return build_call_expr_loc (loc, fndecl, 1, arg);
699 break;
701 case RSHIFT_EXPR:
702 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
703 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
705 tree op1 = TREE_OPERAND (t, 1);
706 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
708 tree ntype = TYPE_UNSIGNED (type)
709 ? signed_type_for (type)
710 : unsigned_type_for (type);
711 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
712 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
713 return fold_convert_loc (loc, type, temp);
716 break;
718 default:
719 break;
722 return NULL_TREE;
725 /* A wrapper for fold_negate_expr_1. */
727 static tree
728 fold_negate_expr (location_t loc, tree t)
730 tree type = TREE_TYPE (t);
731 STRIP_SIGN_NOPS (t);
732 tree tem = fold_negate_expr_1 (loc, t);
733 if (tem == NULL_TREE)
734 return NULL_TREE;
735 return fold_convert_loc (loc, type, tem);
738 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
739 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
740 return NULL_TREE. */
742 static tree
743 negate_expr (tree t)
745 tree type, tem;
746 location_t loc;
748 if (t == NULL_TREE)
749 return NULL_TREE;
751 loc = EXPR_LOCATION (t);
752 type = TREE_TYPE (t);
753 STRIP_SIGN_NOPS (t);
755 tem = fold_negate_expr (loc, t);
756 if (!tem)
757 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
758 return fold_convert_loc (loc, type, tem);
761 /* Split a tree IN into a constant, literal and variable parts that could be
762 combined with CODE to make IN. "constant" means an expression with
763 TREE_CONSTANT but that isn't an actual constant. CODE must be a
764 commutative arithmetic operation. Store the constant part into *CONP,
765 the literal in *LITP and return the variable part. If a part isn't
766 present, set it to null. If the tree does not decompose in this way,
767 return the entire tree as the variable part and the other parts as null.
769 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
770 case, we negate an operand that was subtracted. Except if it is a
771 literal for which we use *MINUS_LITP instead.
773 If NEGATE_P is true, we are negating all of IN, again except a literal
774 for which we use *MINUS_LITP instead. If a variable part is of pointer
775 type, it is negated after converting to TYPE. This prevents us from
776 generating illegal MINUS pointer expression. LOC is the location of
777 the converted variable part.
779 If IN is itself a literal or constant, return it as appropriate.
781 Note that we do not guarantee that any of the three values will be the
782 same type as IN, but they will have the same signedness and mode. */
784 static tree
785 split_tree (tree in, tree type, enum tree_code code,
786 tree *minus_varp, tree *conp, tree *minus_conp,
787 tree *litp, tree *minus_litp, int negate_p)
789 tree var = 0;
790 *minus_varp = 0;
791 *conp = 0;
792 *minus_conp = 0;
793 *litp = 0;
794 *minus_litp = 0;
796 /* Strip any conversions that don't change the machine mode or signedness. */
797 STRIP_SIGN_NOPS (in);
799 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
800 || TREE_CODE (in) == FIXED_CST)
801 *litp = in;
802 else if (TREE_CODE (in) == code
803 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
804 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
805 /* We can associate addition and subtraction together (even
806 though the C standard doesn't say so) for integers because
807 the value is not affected. For reals, the value might be
808 affected, so we can't. */
809 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
810 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
811 || (code == MINUS_EXPR
812 && (TREE_CODE (in) == PLUS_EXPR
813 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
815 tree op0 = TREE_OPERAND (in, 0);
816 tree op1 = TREE_OPERAND (in, 1);
817 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
818 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
820 /* First see if either of the operands is a literal, then a constant. */
821 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
822 || TREE_CODE (op0) == FIXED_CST)
823 *litp = op0, op0 = 0;
824 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
825 || TREE_CODE (op1) == FIXED_CST)
826 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
828 if (op0 != 0 && TREE_CONSTANT (op0))
829 *conp = op0, op0 = 0;
830 else if (op1 != 0 && TREE_CONSTANT (op1))
831 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
833 /* If we haven't dealt with either operand, this is not a case we can
834 decompose. Otherwise, VAR is either of the ones remaining, if any. */
835 if (op0 != 0 && op1 != 0)
836 var = in;
837 else if (op0 != 0)
838 var = op0;
839 else
840 var = op1, neg_var_p = neg1_p;
842 /* Now do any needed negations. */
843 if (neg_litp_p)
844 *minus_litp = *litp, *litp = 0;
845 if (neg_conp_p && *conp)
846 *minus_conp = *conp, *conp = 0;
847 if (neg_var_p && var)
848 *minus_varp = var, var = 0;
850 else if (TREE_CONSTANT (in))
851 *conp = in;
852 else if (TREE_CODE (in) == BIT_NOT_EXPR
853 && code == PLUS_EXPR)
855 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
856 when IN is constant. */
857 *litp = build_minus_one_cst (type);
858 *minus_varp = TREE_OPERAND (in, 0);
860 else
861 var = in;
863 if (negate_p)
865 if (*litp)
866 *minus_litp = *litp, *litp = 0;
867 else if (*minus_litp)
868 *litp = *minus_litp, *minus_litp = 0;
869 if (*conp)
870 *minus_conp = *conp, *conp = 0;
871 else if (*minus_conp)
872 *conp = *minus_conp, *minus_conp = 0;
873 if (var)
874 *minus_varp = var, var = 0;
875 else if (*minus_varp)
876 var = *minus_varp, *minus_varp = 0;
879 if (*litp
880 && TREE_OVERFLOW_P (*litp))
881 *litp = drop_tree_overflow (*litp);
882 if (*minus_litp
883 && TREE_OVERFLOW_P (*minus_litp))
884 *minus_litp = drop_tree_overflow (*minus_litp);
886 return var;
889 /* Re-associate trees split by the above function. T1 and T2 are
890 either expressions to associate or null. Return the new
891 expression, if any. LOC is the location of the new expression. If
892 we build an operation, do it in TYPE and with CODE. */
894 static tree
895 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
897 if (t1 == 0)
899 gcc_assert (t2 == 0 || code != MINUS_EXPR);
900 return t2;
902 else if (t2 == 0)
903 return t1;
905 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
906 try to fold this since we will have infinite recursion. But do
907 deal with any NEGATE_EXPRs. */
908 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
909 || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
910 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
912 if (code == PLUS_EXPR)
914 if (TREE_CODE (t1) == NEGATE_EXPR)
915 return build2_loc (loc, MINUS_EXPR, type,
916 fold_convert_loc (loc, type, t2),
917 fold_convert_loc (loc, type,
918 TREE_OPERAND (t1, 0)));
919 else if (TREE_CODE (t2) == NEGATE_EXPR)
920 return build2_loc (loc, MINUS_EXPR, type,
921 fold_convert_loc (loc, type, t1),
922 fold_convert_loc (loc, type,
923 TREE_OPERAND (t2, 0)));
924 else if (integer_zerop (t2))
925 return fold_convert_loc (loc, type, t1);
927 else if (code == MINUS_EXPR)
929 if (integer_zerop (t2))
930 return fold_convert_loc (loc, type, t1);
933 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
934 fold_convert_loc (loc, type, t2));
937 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
938 fold_convert_loc (loc, type, t2));
941 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
942 for use in int_const_binop, size_binop and size_diffop. */
944 static bool
945 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
947 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
948 return false;
949 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
950 return false;
952 switch (code)
954 case LSHIFT_EXPR:
955 case RSHIFT_EXPR:
956 case LROTATE_EXPR:
957 case RROTATE_EXPR:
958 return true;
960 default:
961 break;
964 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
965 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
966 && TYPE_MODE (type1) == TYPE_MODE (type2);
969 /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
970 a new constant in RES. Return FALSE if we don't know how to
971 evaluate CODE at compile-time. */
973 bool
974 wide_int_binop (wide_int &res,
975 enum tree_code code, const wide_int &arg1, const wide_int &arg2,
976 signop sign, wi::overflow_type *overflow)
978 wide_int tmp;
979 *overflow = wi::OVF_NONE;
980 switch (code)
982 case BIT_IOR_EXPR:
983 res = wi::bit_or (arg1, arg2);
984 break;
986 case BIT_XOR_EXPR:
987 res = wi::bit_xor (arg1, arg2);
988 break;
990 case BIT_AND_EXPR:
991 res = wi::bit_and (arg1, arg2);
992 break;
994 case RSHIFT_EXPR:
995 case LSHIFT_EXPR:
996 if (wi::neg_p (arg2))
998 tmp = -arg2;
999 if (code == RSHIFT_EXPR)
1000 code = LSHIFT_EXPR;
1001 else
1002 code = RSHIFT_EXPR;
1004 else
1005 tmp = arg2;
1007 if (code == RSHIFT_EXPR)
1008 /* It's unclear from the C standard whether shifts can overflow.
1009 The following code ignores overflow; perhaps a C standard
1010 interpretation ruling is needed. */
1011 res = wi::rshift (arg1, tmp, sign);
1012 else
1013 res = wi::lshift (arg1, tmp);
1014 break;
1016 case RROTATE_EXPR:
1017 case LROTATE_EXPR:
1018 if (wi::neg_p (arg2))
1020 tmp = -arg2;
1021 if (code == RROTATE_EXPR)
1022 code = LROTATE_EXPR;
1023 else
1024 code = RROTATE_EXPR;
1026 else
1027 tmp = arg2;
1029 if (code == RROTATE_EXPR)
1030 res = wi::rrotate (arg1, tmp);
1031 else
1032 res = wi::lrotate (arg1, tmp);
1033 break;
1035 case PLUS_EXPR:
1036 res = wi::add (arg1, arg2, sign, overflow);
1037 break;
1039 case MINUS_EXPR:
1040 res = wi::sub (arg1, arg2, sign, overflow);
1041 break;
1043 case MULT_EXPR:
1044 res = wi::mul (arg1, arg2, sign, overflow);
1045 break;
1047 case MULT_HIGHPART_EXPR:
1048 res = wi::mul_high (arg1, arg2, sign);
1049 break;
1051 case TRUNC_DIV_EXPR:
1052 case EXACT_DIV_EXPR:
1053 if (arg2 == 0)
1054 return false;
1055 res = wi::div_trunc (arg1, arg2, sign, overflow);
1056 break;
1058 case FLOOR_DIV_EXPR:
1059 if (arg2 == 0)
1060 return false;
1061 res = wi::div_floor (arg1, arg2, sign, overflow);
1062 break;
1064 case CEIL_DIV_EXPR:
1065 if (arg2 == 0)
1066 return false;
1067 res = wi::div_ceil (arg1, arg2, sign, overflow);
1068 break;
1070 case ROUND_DIV_EXPR:
1071 if (arg2 == 0)
1072 return false;
1073 res = wi::div_round (arg1, arg2, sign, overflow);
1074 break;
1076 case TRUNC_MOD_EXPR:
1077 if (arg2 == 0)
1078 return false;
1079 res = wi::mod_trunc (arg1, arg2, sign, overflow);
1080 break;
1082 case FLOOR_MOD_EXPR:
1083 if (arg2 == 0)
1084 return false;
1085 res = wi::mod_floor (arg1, arg2, sign, overflow);
1086 break;
1088 case CEIL_MOD_EXPR:
1089 if (arg2 == 0)
1090 return false;
1091 res = wi::mod_ceil (arg1, arg2, sign, overflow);
1092 break;
1094 case ROUND_MOD_EXPR:
1095 if (arg2 == 0)
1096 return false;
1097 res = wi::mod_round (arg1, arg2, sign, overflow);
1098 break;
1100 case MIN_EXPR:
1101 res = wi::min (arg1, arg2, sign);
1102 break;
1104 case MAX_EXPR:
1105 res = wi::max (arg1, arg2, sign);
1106 break;
1108 default:
1109 return false;
1111 return true;
1114 /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1115 produce a new constant in RES. Return FALSE if we don't know how
1116 to evaluate CODE at compile-time. */
1118 static bool
1119 poly_int_binop (poly_wide_int &res, enum tree_code code,
1120 const_tree arg1, const_tree arg2,
1121 signop sign, wi::overflow_type *overflow)
1123 gcc_assert (NUM_POLY_INT_COEFFS != 1);
1124 gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1125 switch (code)
1127 case PLUS_EXPR:
1128 res = wi::add (wi::to_poly_wide (arg1),
1129 wi::to_poly_wide (arg2), sign, overflow);
1130 break;
1132 case MINUS_EXPR:
1133 res = wi::sub (wi::to_poly_wide (arg1),
1134 wi::to_poly_wide (arg2), sign, overflow);
1135 break;
1137 case MULT_EXPR:
1138 if (TREE_CODE (arg2) == INTEGER_CST)
1139 res = wi::mul (wi::to_poly_wide (arg1),
1140 wi::to_wide (arg2), sign, overflow);
1141 else if (TREE_CODE (arg1) == INTEGER_CST)
1142 res = wi::mul (wi::to_poly_wide (arg2),
1143 wi::to_wide (arg1), sign, overflow);
1144 else
1145 return NULL_TREE;
1146 break;
1148 case LSHIFT_EXPR:
1149 if (TREE_CODE (arg2) == INTEGER_CST)
1150 res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1151 else
1152 return false;
1153 break;
1155 case BIT_IOR_EXPR:
1156 if (TREE_CODE (arg2) != INTEGER_CST
1157 || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1158 &res))
1159 return false;
1160 break;
1162 default:
1163 return false;
1165 return true;
1168 /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1169 produce a new constant. Return NULL_TREE if we don't know how to
1170 evaluate CODE at compile-time. */
1172 tree
1173 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1174 int overflowable)
1176 bool success = false;
1177 poly_wide_int poly_res;
1178 tree type = TREE_TYPE (arg1);
1179 signop sign = TYPE_SIGN (type);
1180 wi::overflow_type overflow = wi::OVF_NONE;
1182 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1184 wide_int warg1 = wi::to_wide (arg1), res;
1185 wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (type));
1186 success = wide_int_binop (res, code, warg1, warg2, sign, &overflow);
1187 poly_res = res;
1189 else if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1190 success = poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow);
1191 if (success)
1192 return force_fit_type (type, poly_res, overflowable,
1193 (((sign == SIGNED || overflowable == -1)
1194 && overflow)
1195 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1196 return NULL_TREE;
1199 /* Return true if binary operation OP distributes over addition in operand
1200 OPNO, with the other operand being held constant. OPNO counts from 1. */
1202 static bool
1203 distributes_over_addition_p (tree_code op, int opno)
1205 switch (op)
1207 case PLUS_EXPR:
1208 case MINUS_EXPR:
1209 case MULT_EXPR:
1210 return true;
1212 case LSHIFT_EXPR:
1213 return opno == 1;
1215 default:
1216 return false;
1220 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1221 constant. We assume ARG1 and ARG2 have the same data type, or at least
1222 are the same kind of constant and the same machine mode. Return zero if
1223 combining the constants is not allowed in the current operating mode. */
1225 static tree
1226 const_binop (enum tree_code code, tree arg1, tree arg2)
1228 /* Sanity check for the recursive cases. */
1229 if (!arg1 || !arg2)
1230 return NULL_TREE;
1232 STRIP_NOPS (arg1);
1233 STRIP_NOPS (arg2);
1235 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1237 if (code == POINTER_PLUS_EXPR)
1238 return int_const_binop (PLUS_EXPR,
1239 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1241 return int_const_binop (code, arg1, arg2);
1244 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1246 machine_mode mode;
1247 REAL_VALUE_TYPE d1;
1248 REAL_VALUE_TYPE d2;
1249 REAL_VALUE_TYPE value;
1250 REAL_VALUE_TYPE result;
1251 bool inexact;
1252 tree t, type;
1254 /* The following codes are handled by real_arithmetic. */
1255 switch (code)
1257 case PLUS_EXPR:
1258 case MINUS_EXPR:
1259 case MULT_EXPR:
1260 case RDIV_EXPR:
1261 case MIN_EXPR:
1262 case MAX_EXPR:
1263 break;
1265 default:
1266 return NULL_TREE;
1269 d1 = TREE_REAL_CST (arg1);
1270 d2 = TREE_REAL_CST (arg2);
1272 type = TREE_TYPE (arg1);
1273 mode = TYPE_MODE (type);
1275 /* Don't perform operation if we honor signaling NaNs and
1276 either operand is a signaling NaN. */
1277 if (HONOR_SNANS (mode)
1278 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1279 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1280 return NULL_TREE;
1282 /* Don't perform operation if it would raise a division
1283 by zero exception. */
1284 if (code == RDIV_EXPR
1285 && real_equal (&d2, &dconst0)
1286 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1287 return NULL_TREE;
1289 /* If either operand is a NaN, just return it. Otherwise, set up
1290 for floating-point trap; we return an overflow. */
1291 if (REAL_VALUE_ISNAN (d1))
1293 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1294 is off. */
1295 d1.signalling = 0;
1296 t = build_real (type, d1);
1297 return t;
1299 else if (REAL_VALUE_ISNAN (d2))
1301 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1302 is off. */
1303 d2.signalling = 0;
1304 t = build_real (type, d2);
1305 return t;
1308 inexact = real_arithmetic (&value, code, &d1, &d2);
1309 real_convert (&result, mode, &value);
1311 /* Don't constant fold this floating point operation if
1312 the result has overflowed and flag_trapping_math. */
1313 if (flag_trapping_math
1314 && MODE_HAS_INFINITIES (mode)
1315 && REAL_VALUE_ISINF (result)
1316 && !REAL_VALUE_ISINF (d1)
1317 && !REAL_VALUE_ISINF (d2))
1318 return NULL_TREE;
1320 /* Don't constant fold this floating point operation if the
1321 result may dependent upon the run-time rounding mode and
1322 flag_rounding_math is set, or if GCC's software emulation
1323 is unable to accurately represent the result. */
1324 if ((flag_rounding_math
1325 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1326 && (inexact || !real_identical (&result, &value)))
1327 return NULL_TREE;
1329 t = build_real (type, result);
1331 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1332 return t;
1335 if (TREE_CODE (arg1) == FIXED_CST)
1337 FIXED_VALUE_TYPE f1;
1338 FIXED_VALUE_TYPE f2;
1339 FIXED_VALUE_TYPE result;
1340 tree t, type;
1341 int sat_p;
1342 bool overflow_p;
1344 /* The following codes are handled by fixed_arithmetic. */
1345 switch (code)
1347 case PLUS_EXPR:
1348 case MINUS_EXPR:
1349 case MULT_EXPR:
1350 case TRUNC_DIV_EXPR:
1351 if (TREE_CODE (arg2) != FIXED_CST)
1352 return NULL_TREE;
1353 f2 = TREE_FIXED_CST (arg2);
1354 break;
1356 case LSHIFT_EXPR:
1357 case RSHIFT_EXPR:
1359 if (TREE_CODE (arg2) != INTEGER_CST)
1360 return NULL_TREE;
1361 wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1362 f2.data.high = w2.elt (1);
1363 f2.data.low = w2.ulow ();
1364 f2.mode = SImode;
1366 break;
1368 default:
1369 return NULL_TREE;
1372 f1 = TREE_FIXED_CST (arg1);
1373 type = TREE_TYPE (arg1);
1374 sat_p = TYPE_SATURATING (type);
1375 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1376 t = build_fixed (type, result);
1377 /* Propagate overflow flags. */
1378 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1379 TREE_OVERFLOW (t) = 1;
1380 return t;
1383 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1385 tree type = TREE_TYPE (arg1);
1386 tree r1 = TREE_REALPART (arg1);
1387 tree i1 = TREE_IMAGPART (arg1);
1388 tree r2 = TREE_REALPART (arg2);
1389 tree i2 = TREE_IMAGPART (arg2);
1390 tree real, imag;
1392 switch (code)
1394 case PLUS_EXPR:
1395 case MINUS_EXPR:
1396 real = const_binop (code, r1, r2);
1397 imag = const_binop (code, i1, i2);
1398 break;
1400 case MULT_EXPR:
1401 if (COMPLEX_FLOAT_TYPE_P (type))
1402 return do_mpc_arg2 (arg1, arg2, type,
1403 /* do_nonfinite= */ folding_initializer,
1404 mpc_mul);
1406 real = const_binop (MINUS_EXPR,
1407 const_binop (MULT_EXPR, r1, r2),
1408 const_binop (MULT_EXPR, i1, i2));
1409 imag = const_binop (PLUS_EXPR,
1410 const_binop (MULT_EXPR, r1, i2),
1411 const_binop (MULT_EXPR, i1, r2));
1412 break;
1414 case RDIV_EXPR:
1415 if (COMPLEX_FLOAT_TYPE_P (type))
1416 return do_mpc_arg2 (arg1, arg2, type,
1417 /* do_nonfinite= */ folding_initializer,
1418 mpc_div);
1419 /* Fallthru. */
1420 case TRUNC_DIV_EXPR:
1421 case CEIL_DIV_EXPR:
1422 case FLOOR_DIV_EXPR:
1423 case ROUND_DIV_EXPR:
1424 if (flag_complex_method == 0)
1426 /* Keep this algorithm in sync with
1427 tree-complex.c:expand_complex_div_straight().
1429 Expand complex division to scalars, straightforward algorithm.
1430 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1431 t = br*br + bi*bi
1433 tree magsquared
1434 = const_binop (PLUS_EXPR,
1435 const_binop (MULT_EXPR, r2, r2),
1436 const_binop (MULT_EXPR, i2, i2));
1437 tree t1
1438 = const_binop (PLUS_EXPR,
1439 const_binop (MULT_EXPR, r1, r2),
1440 const_binop (MULT_EXPR, i1, i2));
1441 tree t2
1442 = const_binop (MINUS_EXPR,
1443 const_binop (MULT_EXPR, i1, r2),
1444 const_binop (MULT_EXPR, r1, i2));
1446 real = const_binop (code, t1, magsquared);
1447 imag = const_binop (code, t2, magsquared);
1449 else
1451 /* Keep this algorithm in sync with
1452 tree-complex.c:expand_complex_div_wide().
1454 Expand complex division to scalars, modified algorithm to minimize
1455 overflow with wide input ranges. */
1456 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1457 fold_abs_const (r2, TREE_TYPE (type)),
1458 fold_abs_const (i2, TREE_TYPE (type)));
1460 if (integer_nonzerop (compare))
1462 /* In the TRUE branch, we compute
1463 ratio = br/bi;
1464 div = (br * ratio) + bi;
1465 tr = (ar * ratio) + ai;
1466 ti = (ai * ratio) - ar;
1467 tr = tr / div;
1468 ti = ti / div; */
1469 tree ratio = const_binop (code, r2, i2);
1470 tree div = const_binop (PLUS_EXPR, i2,
1471 const_binop (MULT_EXPR, r2, ratio));
1472 real = const_binop (MULT_EXPR, r1, ratio);
1473 real = const_binop (PLUS_EXPR, real, i1);
1474 real = const_binop (code, real, div);
1476 imag = const_binop (MULT_EXPR, i1, ratio);
1477 imag = const_binop (MINUS_EXPR, imag, r1);
1478 imag = const_binop (code, imag, div);
1480 else
1482 /* In the FALSE branch, we compute
1483 ratio = d/c;
1484 divisor = (d * ratio) + c;
1485 tr = (b * ratio) + a;
1486 ti = b - (a * ratio);
1487 tr = tr / div;
1488 ti = ti / div; */
1489 tree ratio = const_binop (code, i2, r2);
1490 tree div = const_binop (PLUS_EXPR, r2,
1491 const_binop (MULT_EXPR, i2, ratio));
1493 real = const_binop (MULT_EXPR, i1, ratio);
1494 real = const_binop (PLUS_EXPR, real, r1);
1495 real = const_binop (code, real, div);
1497 imag = const_binop (MULT_EXPR, r1, ratio);
1498 imag = const_binop (MINUS_EXPR, i1, imag);
1499 imag = const_binop (code, imag, div);
1502 break;
1504 default:
1505 return NULL_TREE;
1508 if (real && imag)
1509 return build_complex (type, real, imag);
1512 if (TREE_CODE (arg1) == VECTOR_CST
1513 && TREE_CODE (arg2) == VECTOR_CST
1514 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1515 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1517 tree type = TREE_TYPE (arg1);
1518 bool step_ok_p;
1519 if (VECTOR_CST_STEPPED_P (arg1)
1520 && VECTOR_CST_STEPPED_P (arg2))
1521 /* We can operate directly on the encoding if:
1523 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1524 implies
1525 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1527 Addition and subtraction are the supported operators
1528 for which this is true. */
1529 step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1530 else if (VECTOR_CST_STEPPED_P (arg1))
1531 /* We can operate directly on stepped encodings if:
1533 a3 - a2 == a2 - a1
1534 implies:
1535 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1537 which is true if (x -> x op c) distributes over addition. */
1538 step_ok_p = distributes_over_addition_p (code, 1);
1539 else
1540 /* Similarly in reverse. */
1541 step_ok_p = distributes_over_addition_p (code, 2);
1542 tree_vector_builder elts;
1543 if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1544 return NULL_TREE;
1545 unsigned int count = elts.encoded_nelts ();
1546 for (unsigned int i = 0; i < count; ++i)
1548 tree elem1 = VECTOR_CST_ELT (arg1, i);
1549 tree elem2 = VECTOR_CST_ELT (arg2, i);
1551 tree elt = const_binop (code, elem1, elem2);
1553 /* It is possible that const_binop cannot handle the given
1554 code and return NULL_TREE */
1555 if (elt == NULL_TREE)
1556 return NULL_TREE;
1557 elts.quick_push (elt);
1560 return elts.build ();
1563 /* Shifts allow a scalar offset for a vector. */
1564 if (TREE_CODE (arg1) == VECTOR_CST
1565 && TREE_CODE (arg2) == INTEGER_CST)
1567 tree type = TREE_TYPE (arg1);
1568 bool step_ok_p = distributes_over_addition_p (code, 1);
1569 tree_vector_builder elts;
1570 if (!elts.new_unary_operation (type, arg1, step_ok_p))
1571 return NULL_TREE;
1572 unsigned int count = elts.encoded_nelts ();
1573 for (unsigned int i = 0; i < count; ++i)
1575 tree elem1 = VECTOR_CST_ELT (arg1, i);
1577 tree elt = const_binop (code, elem1, arg2);
1579 /* It is possible that const_binop cannot handle the given
1580 code and return NULL_TREE. */
1581 if (elt == NULL_TREE)
1582 return NULL_TREE;
1583 elts.quick_push (elt);
1586 return elts.build ();
1588 return NULL_TREE;
1591 /* Overload that adds a TYPE parameter to be able to dispatch
1592 to fold_relational_const. */
1594 tree
1595 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1597 if (TREE_CODE_CLASS (code) == tcc_comparison)
1598 return fold_relational_const (code, type, arg1, arg2);
1600 /* ??? Until we make the const_binop worker take the type of the
1601 result as argument put those cases that need it here. */
1602 switch (code)
1604 case VEC_SERIES_EXPR:
1605 if (CONSTANT_CLASS_P (arg1)
1606 && CONSTANT_CLASS_P (arg2))
1607 return build_vec_series (type, arg1, arg2);
1608 return NULL_TREE;
1610 case COMPLEX_EXPR:
1611 if ((TREE_CODE (arg1) == REAL_CST
1612 && TREE_CODE (arg2) == REAL_CST)
1613 || (TREE_CODE (arg1) == INTEGER_CST
1614 && TREE_CODE (arg2) == INTEGER_CST))
1615 return build_complex (type, arg1, arg2);
1616 return NULL_TREE;
1618 case POINTER_DIFF_EXPR:
1619 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1621 poly_offset_int res = (wi::to_poly_offset (arg1)
1622 - wi::to_poly_offset (arg2));
1623 return force_fit_type (type, res, 1,
1624 TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1626 return NULL_TREE;
1628 case VEC_PACK_TRUNC_EXPR:
1629 case VEC_PACK_FIX_TRUNC_EXPR:
1630 case VEC_PACK_FLOAT_EXPR:
1632 unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1634 if (TREE_CODE (arg1) != VECTOR_CST
1635 || TREE_CODE (arg2) != VECTOR_CST)
1636 return NULL_TREE;
1638 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1639 return NULL_TREE;
1641 out_nelts = in_nelts * 2;
1642 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1643 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1645 tree_vector_builder elts (type, out_nelts, 1);
1646 for (i = 0; i < out_nelts; i++)
1648 tree elt = (i < in_nelts
1649 ? VECTOR_CST_ELT (arg1, i)
1650 : VECTOR_CST_ELT (arg2, i - in_nelts));
1651 elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1652 ? NOP_EXPR
1653 : code == VEC_PACK_FLOAT_EXPR
1654 ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1655 TREE_TYPE (type), elt);
1656 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1657 return NULL_TREE;
1658 elts.quick_push (elt);
1661 return elts.build ();
1664 case VEC_WIDEN_MULT_LO_EXPR:
1665 case VEC_WIDEN_MULT_HI_EXPR:
1666 case VEC_WIDEN_MULT_EVEN_EXPR:
1667 case VEC_WIDEN_MULT_ODD_EXPR:
1669 unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1671 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1672 return NULL_TREE;
1674 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1675 return NULL_TREE;
1676 out_nelts = in_nelts / 2;
1677 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1678 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1680 if (code == VEC_WIDEN_MULT_LO_EXPR)
1681 scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1682 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1683 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1684 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1685 scale = 1, ofs = 0;
1686 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1687 scale = 1, ofs = 1;
1689 tree_vector_builder elts (type, out_nelts, 1);
1690 for (out = 0; out < out_nelts; out++)
1692 unsigned int in = (out << scale) + ofs;
1693 tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1694 VECTOR_CST_ELT (arg1, in));
1695 tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1696 VECTOR_CST_ELT (arg2, in));
1698 if (t1 == NULL_TREE || t2 == NULL_TREE)
1699 return NULL_TREE;
1700 tree elt = const_binop (MULT_EXPR, t1, t2);
1701 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1702 return NULL_TREE;
1703 elts.quick_push (elt);
1706 return elts.build ();
1709 default:;
1712 if (TREE_CODE_CLASS (code) != tcc_binary)
1713 return NULL_TREE;
1715 /* Make sure type and arg0 have the same saturating flag. */
1716 gcc_checking_assert (TYPE_SATURATING (type)
1717 == TYPE_SATURATING (TREE_TYPE (arg1)));
1719 return const_binop (code, arg1, arg2);
1722 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1723 Return zero if computing the constants is not possible. */
1725 tree
1726 const_unop (enum tree_code code, tree type, tree arg0)
1728 /* Don't perform the operation, other than NEGATE and ABS, if
1729 flag_signaling_nans is on and the operand is a signaling NaN. */
1730 if (TREE_CODE (arg0) == REAL_CST
1731 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1732 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1733 && code != NEGATE_EXPR
1734 && code != ABS_EXPR
1735 && code != ABSU_EXPR)
1736 return NULL_TREE;
1738 switch (code)
1740 CASE_CONVERT:
1741 case FLOAT_EXPR:
1742 case FIX_TRUNC_EXPR:
1743 case FIXED_CONVERT_EXPR:
1744 return fold_convert_const (code, type, arg0);
1746 case ADDR_SPACE_CONVERT_EXPR:
1747 /* If the source address is 0, and the source address space
1748 cannot have a valid object at 0, fold to dest type null. */
1749 if (integer_zerop (arg0)
1750 && !(targetm.addr_space.zero_address_valid
1751 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1752 return fold_convert_const (code, type, arg0);
1753 break;
1755 case VIEW_CONVERT_EXPR:
1756 return fold_view_convert_expr (type, arg0);
1758 case NEGATE_EXPR:
1760 /* Can't call fold_negate_const directly here as that doesn't
1761 handle all cases and we might not be able to negate some
1762 constants. */
1763 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1764 if (tem && CONSTANT_CLASS_P (tem))
1765 return tem;
1766 break;
1769 case ABS_EXPR:
1770 case ABSU_EXPR:
1771 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1772 return fold_abs_const (arg0, type);
1773 break;
1775 case CONJ_EXPR:
1776 if (TREE_CODE (arg0) == COMPLEX_CST)
1778 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1779 TREE_TYPE (type));
1780 return build_complex (type, TREE_REALPART (arg0), ipart);
1782 break;
1784 case BIT_NOT_EXPR:
1785 if (TREE_CODE (arg0) == INTEGER_CST)
1786 return fold_not_const (arg0, type);
1787 else if (POLY_INT_CST_P (arg0))
1788 return wide_int_to_tree (type, -poly_int_cst_value (arg0));
1789 /* Perform BIT_NOT_EXPR on each element individually. */
1790 else if (TREE_CODE (arg0) == VECTOR_CST)
1792 tree elem;
1794 /* This can cope with stepped encodings because ~x == -1 - x. */
1795 tree_vector_builder elements;
1796 elements.new_unary_operation (type, arg0, true);
1797 unsigned int i, count = elements.encoded_nelts ();
1798 for (i = 0; i < count; ++i)
1800 elem = VECTOR_CST_ELT (arg0, i);
1801 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1802 if (elem == NULL_TREE)
1803 break;
1804 elements.quick_push (elem);
1806 if (i == count)
1807 return elements.build ();
1809 break;
1811 case TRUTH_NOT_EXPR:
1812 if (TREE_CODE (arg0) == INTEGER_CST)
1813 return constant_boolean_node (integer_zerop (arg0), type);
1814 break;
1816 case REALPART_EXPR:
1817 if (TREE_CODE (arg0) == COMPLEX_CST)
1818 return fold_convert (type, TREE_REALPART (arg0));
1819 break;
1821 case IMAGPART_EXPR:
1822 if (TREE_CODE (arg0) == COMPLEX_CST)
1823 return fold_convert (type, TREE_IMAGPART (arg0));
1824 break;
1826 case VEC_UNPACK_LO_EXPR:
1827 case VEC_UNPACK_HI_EXPR:
1828 case VEC_UNPACK_FLOAT_LO_EXPR:
1829 case VEC_UNPACK_FLOAT_HI_EXPR:
1830 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1831 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1833 unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
1834 enum tree_code subcode;
1836 if (TREE_CODE (arg0) != VECTOR_CST)
1837 return NULL_TREE;
1839 if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
1840 return NULL_TREE;
1841 out_nelts = in_nelts / 2;
1842 gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1844 unsigned int offset = 0;
1845 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1846 || code == VEC_UNPACK_FLOAT_LO_EXPR
1847 || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
1848 offset = out_nelts;
1850 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1851 subcode = NOP_EXPR;
1852 else if (code == VEC_UNPACK_FLOAT_LO_EXPR
1853 || code == VEC_UNPACK_FLOAT_HI_EXPR)
1854 subcode = FLOAT_EXPR;
1855 else
1856 subcode = FIX_TRUNC_EXPR;
1858 tree_vector_builder elts (type, out_nelts, 1);
1859 for (i = 0; i < out_nelts; i++)
1861 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1862 VECTOR_CST_ELT (arg0, i + offset));
1863 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1864 return NULL_TREE;
1865 elts.quick_push (elt);
1868 return elts.build ();
1871 case VEC_DUPLICATE_EXPR:
1872 if (CONSTANT_CLASS_P (arg0))
1873 return build_vector_from_val (type, arg0);
1874 return NULL_TREE;
1876 default:
1877 break;
1880 return NULL_TREE;
1883 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1884 indicates which particular sizetype to create. */
1886 tree
1887 size_int_kind (poly_int64 number, enum size_type_kind kind)
1889 return build_int_cst (sizetype_tab[(int) kind], number);
1892 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1893 is a tree code. The type of the result is taken from the operands.
1894 Both must be equivalent integer types, ala int_binop_types_match_p.
1895 If the operands are constant, so is the result. */
1897 tree
1898 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1900 tree type = TREE_TYPE (arg0);
1902 if (arg0 == error_mark_node || arg1 == error_mark_node)
1903 return error_mark_node;
1905 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1906 TREE_TYPE (arg1)));
1908 /* Handle the special case of two poly_int constants faster. */
1909 if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1911 /* And some specific cases even faster than that. */
1912 if (code == PLUS_EXPR)
1914 if (integer_zerop (arg0)
1915 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
1916 return arg1;
1917 if (integer_zerop (arg1)
1918 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
1919 return arg0;
1921 else if (code == MINUS_EXPR)
1923 if (integer_zerop (arg1)
1924 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
1925 return arg0;
1927 else if (code == MULT_EXPR)
1929 if (integer_onep (arg0)
1930 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
1931 return arg1;
1934 /* Handle general case of two integer constants. For sizetype
1935 constant calculations we always want to know about overflow,
1936 even in the unsigned case. */
1937 tree res = int_const_binop (code, arg0, arg1, -1);
1938 if (res != NULL_TREE)
1939 return res;
1942 return fold_build2_loc (loc, code, type, arg0, arg1);
1945 /* Given two values, either both of sizetype or both of bitsizetype,
1946 compute the difference between the two values. Return the value
1947 in signed type corresponding to the type of the operands. */
1949 tree
1950 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1952 tree type = TREE_TYPE (arg0);
1953 tree ctype;
1955 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1956 TREE_TYPE (arg1)));
1958 /* If the type is already signed, just do the simple thing. */
1959 if (!TYPE_UNSIGNED (type))
1960 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1962 if (type == sizetype)
1963 ctype = ssizetype;
1964 else if (type == bitsizetype)
1965 ctype = sbitsizetype;
1966 else
1967 ctype = signed_type_for (type);
1969 /* If either operand is not a constant, do the conversions to the signed
1970 type and subtract. The hardware will do the right thing with any
1971 overflow in the subtraction. */
1972 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1973 return size_binop_loc (loc, MINUS_EXPR,
1974 fold_convert_loc (loc, ctype, arg0),
1975 fold_convert_loc (loc, ctype, arg1));
1977 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1978 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1979 overflow) and negate (which can't either). Special-case a result
1980 of zero while we're here. */
1981 if (tree_int_cst_equal (arg0, arg1))
1982 return build_int_cst (ctype, 0);
1983 else if (tree_int_cst_lt (arg1, arg0))
1984 return fold_convert_loc (loc, ctype,
1985 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1986 else
1987 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1988 fold_convert_loc (loc, ctype,
1989 size_binop_loc (loc,
1990 MINUS_EXPR,
1991 arg1, arg0)));
1994 /* A subroutine of fold_convert_const handling conversions of an
1995 INTEGER_CST to another integer type. */
1997 static tree
1998 fold_convert_const_int_from_int (tree type, const_tree arg1)
2000 /* Given an integer constant, make new constant with new type,
2001 appropriately sign-extended or truncated. Use widest_int
2002 so that any extension is done according ARG1's type. */
2003 return force_fit_type (type, wi::to_widest (arg1),
2004 !POINTER_TYPE_P (TREE_TYPE (arg1)),
2005 TREE_OVERFLOW (arg1));
2008 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2009 to an integer type. */
2011 static tree
2012 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2014 bool overflow = false;
2015 tree t;
2017 /* The following code implements the floating point to integer
2018 conversion rules required by the Java Language Specification,
2019 that IEEE NaNs are mapped to zero and values that overflow
2020 the target precision saturate, i.e. values greater than
2021 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2022 are mapped to INT_MIN. These semantics are allowed by the
2023 C and C++ standards that simply state that the behavior of
2024 FP-to-integer conversion is unspecified upon overflow. */
2026 wide_int val;
2027 REAL_VALUE_TYPE r;
2028 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2030 switch (code)
2032 case FIX_TRUNC_EXPR:
2033 real_trunc (&r, VOIDmode, &x);
2034 break;
2036 default:
2037 gcc_unreachable ();
2040 /* If R is NaN, return zero and show we have an overflow. */
2041 if (REAL_VALUE_ISNAN (r))
2043 overflow = true;
2044 val = wi::zero (TYPE_PRECISION (type));
2047 /* See if R is less than the lower bound or greater than the
2048 upper bound. */
2050 if (! overflow)
2052 tree lt = TYPE_MIN_VALUE (type);
2053 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2054 if (real_less (&r, &l))
2056 overflow = true;
2057 val = wi::to_wide (lt);
2061 if (! overflow)
2063 tree ut = TYPE_MAX_VALUE (type);
2064 if (ut)
2066 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2067 if (real_less (&u, &r))
2069 overflow = true;
2070 val = wi::to_wide (ut);
2075 if (! overflow)
2076 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2078 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2079 return t;
2082 /* A subroutine of fold_convert_const handling conversions of a
2083 FIXED_CST to an integer type. */
2085 static tree
2086 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2088 tree t;
2089 double_int temp, temp_trunc;
2090 scalar_mode mode;
2092 /* Right shift FIXED_CST to temp by fbit. */
2093 temp = TREE_FIXED_CST (arg1).data;
2094 mode = TREE_FIXED_CST (arg1).mode;
2095 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2097 temp = temp.rshift (GET_MODE_FBIT (mode),
2098 HOST_BITS_PER_DOUBLE_INT,
2099 SIGNED_FIXED_POINT_MODE_P (mode));
2101 /* Left shift temp to temp_trunc by fbit. */
2102 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2103 HOST_BITS_PER_DOUBLE_INT,
2104 SIGNED_FIXED_POINT_MODE_P (mode));
2106 else
2108 temp = double_int_zero;
2109 temp_trunc = double_int_zero;
2112 /* If FIXED_CST is negative, we need to round the value toward 0.
2113 By checking if the fractional bits are not zero to add 1 to temp. */
2114 if (SIGNED_FIXED_POINT_MODE_P (mode)
2115 && temp_trunc.is_negative ()
2116 && TREE_FIXED_CST (arg1).data != temp_trunc)
2117 temp += double_int_one;
2119 /* Given a fixed-point constant, make new constant with new type,
2120 appropriately sign-extended or truncated. */
2121 t = force_fit_type (type, temp, -1,
2122 (temp.is_negative ()
2123 && (TYPE_UNSIGNED (type)
2124 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2125 | TREE_OVERFLOW (arg1));
2127 return t;
2130 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2131 to another floating point type. */
2133 static tree
2134 fold_convert_const_real_from_real (tree type, const_tree arg1)
2136 REAL_VALUE_TYPE value;
2137 tree t;
2139 /* Don't perform the operation if flag_signaling_nans is on
2140 and the operand is a signaling NaN. */
2141 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2142 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2143 return NULL_TREE;
2145 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2146 t = build_real (type, value);
2148 /* If converting an infinity or NAN to a representation that doesn't
2149 have one, set the overflow bit so that we can produce some kind of
2150 error message at the appropriate point if necessary. It's not the
2151 most user-friendly message, but it's better than nothing. */
2152 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2153 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2154 TREE_OVERFLOW (t) = 1;
2155 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2156 && !MODE_HAS_NANS (TYPE_MODE (type)))
2157 TREE_OVERFLOW (t) = 1;
2158 /* Regular overflow, conversion produced an infinity in a mode that
2159 can't represent them. */
2160 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2161 && REAL_VALUE_ISINF (value)
2162 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2163 TREE_OVERFLOW (t) = 1;
2164 else
2165 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2166 return t;
2169 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2170 to a floating point type. */
2172 static tree
2173 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2175 REAL_VALUE_TYPE value;
2176 tree t;
2178 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2179 &TREE_FIXED_CST (arg1));
2180 t = build_real (type, value);
2182 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2183 return t;
2186 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2187 to another fixed-point type. */
2189 static tree
2190 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2192 FIXED_VALUE_TYPE value;
2193 tree t;
2194 bool overflow_p;
2196 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2197 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2198 t = build_fixed (type, value);
2200 /* Propagate overflow flags. */
2201 if (overflow_p | TREE_OVERFLOW (arg1))
2202 TREE_OVERFLOW (t) = 1;
2203 return t;
2206 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2207 to a fixed-point type. */
2209 static tree
2210 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2212 FIXED_VALUE_TYPE value;
2213 tree t;
2214 bool overflow_p;
2215 double_int di;
2217 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2219 di.low = TREE_INT_CST_ELT (arg1, 0);
2220 if (TREE_INT_CST_NUNITS (arg1) == 1)
2221 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2222 else
2223 di.high = TREE_INT_CST_ELT (arg1, 1);
2225 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2226 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2227 TYPE_SATURATING (type));
2228 t = build_fixed (type, value);
2230 /* Propagate overflow flags. */
2231 if (overflow_p | TREE_OVERFLOW (arg1))
2232 TREE_OVERFLOW (t) = 1;
2233 return t;
2236 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2237 to a fixed-point type. */
2239 static tree
2240 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2242 FIXED_VALUE_TYPE value;
2243 tree t;
2244 bool overflow_p;
2246 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2247 &TREE_REAL_CST (arg1),
2248 TYPE_SATURATING (type));
2249 t = build_fixed (type, value);
2251 /* Propagate overflow flags. */
2252 if (overflow_p | TREE_OVERFLOW (arg1))
2253 TREE_OVERFLOW (t) = 1;
2254 return t;
2257 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2258 type TYPE. If no simplification can be done return NULL_TREE. */
2260 static tree
2261 fold_convert_const (enum tree_code code, tree type, tree arg1)
2263 tree arg_type = TREE_TYPE (arg1);
2264 if (arg_type == type)
2265 return arg1;
2267 /* We can't widen types, since the runtime value could overflow the
2268 original type before being extended to the new type. */
2269 if (POLY_INT_CST_P (arg1)
2270 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2271 && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2272 return build_poly_int_cst (type,
2273 poly_wide_int::from (poly_int_cst_value (arg1),
2274 TYPE_PRECISION (type),
2275 TYPE_SIGN (arg_type)));
2277 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2278 || TREE_CODE (type) == OFFSET_TYPE)
2280 if (TREE_CODE (arg1) == INTEGER_CST)
2281 return fold_convert_const_int_from_int (type, arg1);
2282 else if (TREE_CODE (arg1) == REAL_CST)
2283 return fold_convert_const_int_from_real (code, type, arg1);
2284 else if (TREE_CODE (arg1) == FIXED_CST)
2285 return fold_convert_const_int_from_fixed (type, arg1);
2287 else if (TREE_CODE (type) == REAL_TYPE)
2289 if (TREE_CODE (arg1) == INTEGER_CST)
2290 return build_real_from_int_cst (type, arg1);
2291 else if (TREE_CODE (arg1) == REAL_CST)
2292 return fold_convert_const_real_from_real (type, arg1);
2293 else if (TREE_CODE (arg1) == FIXED_CST)
2294 return fold_convert_const_real_from_fixed (type, arg1);
2296 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2298 if (TREE_CODE (arg1) == FIXED_CST)
2299 return fold_convert_const_fixed_from_fixed (type, arg1);
2300 else if (TREE_CODE (arg1) == INTEGER_CST)
2301 return fold_convert_const_fixed_from_int (type, arg1);
2302 else if (TREE_CODE (arg1) == REAL_CST)
2303 return fold_convert_const_fixed_from_real (type, arg1);
2305 else if (TREE_CODE (type) == VECTOR_TYPE)
2307 if (TREE_CODE (arg1) == VECTOR_CST
2308 && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2310 tree elttype = TREE_TYPE (type);
2311 tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2312 /* We can't handle steps directly when extending, since the
2313 values need to wrap at the original precision first. */
2314 bool step_ok_p
2315 = (INTEGRAL_TYPE_P (elttype)
2316 && INTEGRAL_TYPE_P (arg1_elttype)
2317 && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2318 tree_vector_builder v;
2319 if (!v.new_unary_operation (type, arg1, step_ok_p))
2320 return NULL_TREE;
2321 unsigned int len = v.encoded_nelts ();
2322 for (unsigned int i = 0; i < len; ++i)
2324 tree elt = VECTOR_CST_ELT (arg1, i);
2325 tree cvt = fold_convert_const (code, elttype, elt);
2326 if (cvt == NULL_TREE)
2327 return NULL_TREE;
2328 v.quick_push (cvt);
2330 return v.build ();
2333 return NULL_TREE;
2336 /* Construct a vector of zero elements of vector type TYPE. */
2338 static tree
2339 build_zero_vector (tree type)
2341 tree t;
2343 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2344 return build_vector_from_val (type, t);
2347 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2349 bool
2350 fold_convertible_p (const_tree type, const_tree arg)
2352 tree orig = TREE_TYPE (arg);
2354 if (type == orig)
2355 return true;
2357 if (TREE_CODE (arg) == ERROR_MARK
2358 || TREE_CODE (type) == ERROR_MARK
2359 || TREE_CODE (orig) == ERROR_MARK)
2360 return false;
2362 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2363 return true;
2365 switch (TREE_CODE (type))
2367 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2368 case POINTER_TYPE: case REFERENCE_TYPE:
2369 case OFFSET_TYPE:
2370 return (INTEGRAL_TYPE_P (orig)
2371 || (POINTER_TYPE_P (orig)
2372 && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2373 || TREE_CODE (orig) == OFFSET_TYPE);
2375 case REAL_TYPE:
2376 case FIXED_POINT_TYPE:
2377 case VECTOR_TYPE:
2378 case VOID_TYPE:
2379 return TREE_CODE (type) == TREE_CODE (orig);
2381 default:
2382 return false;
2386 /* Convert expression ARG to type TYPE. Used by the middle-end for
2387 simple conversions in preference to calling the front-end's convert. */
2389 tree
2390 fold_convert_loc (location_t loc, tree type, tree arg)
2392 tree orig = TREE_TYPE (arg);
2393 tree tem;
2395 if (type == orig)
2396 return arg;
2398 if (TREE_CODE (arg) == ERROR_MARK
2399 || TREE_CODE (type) == ERROR_MARK
2400 || TREE_CODE (orig) == ERROR_MARK)
2401 return error_mark_node;
2403 switch (TREE_CODE (type))
2405 case POINTER_TYPE:
2406 case REFERENCE_TYPE:
2407 /* Handle conversions between pointers to different address spaces. */
2408 if (POINTER_TYPE_P (orig)
2409 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2410 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2411 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2412 /* fall through */
2414 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2415 case OFFSET_TYPE:
2416 if (TREE_CODE (arg) == INTEGER_CST)
2418 tem = fold_convert_const (NOP_EXPR, type, arg);
2419 if (tem != NULL_TREE)
2420 return tem;
2422 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2423 || TREE_CODE (orig) == OFFSET_TYPE)
2424 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2425 if (TREE_CODE (orig) == COMPLEX_TYPE)
2426 return fold_convert_loc (loc, type,
2427 fold_build1_loc (loc, REALPART_EXPR,
2428 TREE_TYPE (orig), arg));
2429 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2430 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2431 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2433 case REAL_TYPE:
2434 if (TREE_CODE (arg) == INTEGER_CST)
2436 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2437 if (tem != NULL_TREE)
2438 return tem;
2440 else if (TREE_CODE (arg) == REAL_CST)
2442 tem = fold_convert_const (NOP_EXPR, type, arg);
2443 if (tem != NULL_TREE)
2444 return tem;
2446 else if (TREE_CODE (arg) == FIXED_CST)
2448 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2449 if (tem != NULL_TREE)
2450 return tem;
2453 switch (TREE_CODE (orig))
2455 case INTEGER_TYPE:
2456 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2457 case POINTER_TYPE: case REFERENCE_TYPE:
2458 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2460 case REAL_TYPE:
2461 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2463 case FIXED_POINT_TYPE:
2464 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2466 case COMPLEX_TYPE:
2467 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2468 return fold_convert_loc (loc, type, tem);
2470 default:
2471 gcc_unreachable ();
2474 case FIXED_POINT_TYPE:
2475 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2476 || TREE_CODE (arg) == REAL_CST)
2478 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2479 if (tem != NULL_TREE)
2480 goto fold_convert_exit;
2483 switch (TREE_CODE (orig))
2485 case FIXED_POINT_TYPE:
2486 case INTEGER_TYPE:
2487 case ENUMERAL_TYPE:
2488 case BOOLEAN_TYPE:
2489 case REAL_TYPE:
2490 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2492 case COMPLEX_TYPE:
2493 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2494 return fold_convert_loc (loc, type, tem);
2496 default:
2497 gcc_unreachable ();
2500 case COMPLEX_TYPE:
2501 switch (TREE_CODE (orig))
2503 case INTEGER_TYPE:
2504 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2505 case POINTER_TYPE: case REFERENCE_TYPE:
2506 case REAL_TYPE:
2507 case FIXED_POINT_TYPE:
2508 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2509 fold_convert_loc (loc, TREE_TYPE (type), arg),
2510 fold_convert_loc (loc, TREE_TYPE (type),
2511 integer_zero_node));
2512 case COMPLEX_TYPE:
2514 tree rpart, ipart;
2516 if (TREE_CODE (arg) == COMPLEX_EXPR)
2518 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2519 TREE_OPERAND (arg, 0));
2520 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2521 TREE_OPERAND (arg, 1));
2522 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2525 arg = save_expr (arg);
2526 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2527 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2528 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2529 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2530 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2533 default:
2534 gcc_unreachable ();
2537 case VECTOR_TYPE:
2538 if (integer_zerop (arg))
2539 return build_zero_vector (type);
2540 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2541 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2542 || TREE_CODE (orig) == VECTOR_TYPE);
2543 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2545 case VOID_TYPE:
2546 tem = fold_ignored_result (arg);
2547 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2549 default:
2550 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2551 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2552 gcc_unreachable ();
2554 fold_convert_exit:
2555 protected_set_expr_location_unshare (tem, loc);
2556 return tem;
2559 /* Return false if expr can be assumed not to be an lvalue, true
2560 otherwise. */
2562 static bool
2563 maybe_lvalue_p (const_tree x)
2565 /* We only need to wrap lvalue tree codes. */
2566 switch (TREE_CODE (x))
2568 case VAR_DECL:
2569 case PARM_DECL:
2570 case RESULT_DECL:
2571 case LABEL_DECL:
2572 case FUNCTION_DECL:
2573 case SSA_NAME:
2575 case COMPONENT_REF:
2576 case MEM_REF:
2577 case INDIRECT_REF:
2578 case ARRAY_REF:
2579 case ARRAY_RANGE_REF:
2580 case BIT_FIELD_REF:
2581 case OBJ_TYPE_REF:
2583 case REALPART_EXPR:
2584 case IMAGPART_EXPR:
2585 case PREINCREMENT_EXPR:
2586 case PREDECREMENT_EXPR:
2587 case SAVE_EXPR:
2588 case TRY_CATCH_EXPR:
2589 case WITH_CLEANUP_EXPR:
2590 case COMPOUND_EXPR:
2591 case MODIFY_EXPR:
2592 case TARGET_EXPR:
2593 case COND_EXPR:
2594 case BIND_EXPR:
2595 break;
2597 default:
2598 /* Assume the worst for front-end tree codes. */
2599 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2600 break;
2601 return false;
2604 return true;
2607 /* Return an expr equal to X but certainly not valid as an lvalue. */
2609 tree
2610 non_lvalue_loc (location_t loc, tree x)
2612 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2613 us. */
2614 if (in_gimple_form)
2615 return x;
2617 if (! maybe_lvalue_p (x))
2618 return x;
2619 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2622 /* When pedantic, return an expr equal to X but certainly not valid as a
2623 pedantic lvalue. Otherwise, return X. */
2625 static tree
2626 pedantic_non_lvalue_loc (location_t loc, tree x)
2628 return protected_set_expr_location_unshare (x, loc);
2631 /* Given a tree comparison code, return the code that is the logical inverse.
2632 It is generally not safe to do this for floating-point comparisons, except
2633 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2634 ERROR_MARK in this case. */
2636 enum tree_code
2637 invert_tree_comparison (enum tree_code code, bool honor_nans)
2639 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2640 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2641 return ERROR_MARK;
2643 switch (code)
2645 case EQ_EXPR:
2646 return NE_EXPR;
2647 case NE_EXPR:
2648 return EQ_EXPR;
2649 case GT_EXPR:
2650 return honor_nans ? UNLE_EXPR : LE_EXPR;
2651 case GE_EXPR:
2652 return honor_nans ? UNLT_EXPR : LT_EXPR;
2653 case LT_EXPR:
2654 return honor_nans ? UNGE_EXPR : GE_EXPR;
2655 case LE_EXPR:
2656 return honor_nans ? UNGT_EXPR : GT_EXPR;
2657 case LTGT_EXPR:
2658 return UNEQ_EXPR;
2659 case UNEQ_EXPR:
2660 return LTGT_EXPR;
2661 case UNGT_EXPR:
2662 return LE_EXPR;
2663 case UNGE_EXPR:
2664 return LT_EXPR;
2665 case UNLT_EXPR:
2666 return GE_EXPR;
2667 case UNLE_EXPR:
2668 return GT_EXPR;
2669 case ORDERED_EXPR:
2670 return UNORDERED_EXPR;
2671 case UNORDERED_EXPR:
2672 return ORDERED_EXPR;
2673 default:
2674 gcc_unreachable ();
2678 /* Similar, but return the comparison that results if the operands are
2679 swapped. This is safe for floating-point. */
2681 enum tree_code
2682 swap_tree_comparison (enum tree_code code)
2684 switch (code)
2686 case EQ_EXPR:
2687 case NE_EXPR:
2688 case ORDERED_EXPR:
2689 case UNORDERED_EXPR:
2690 case LTGT_EXPR:
2691 case UNEQ_EXPR:
2692 return code;
2693 case GT_EXPR:
2694 return LT_EXPR;
2695 case GE_EXPR:
2696 return LE_EXPR;
2697 case LT_EXPR:
2698 return GT_EXPR;
2699 case LE_EXPR:
2700 return GE_EXPR;
2701 case UNGT_EXPR:
2702 return UNLT_EXPR;
2703 case UNGE_EXPR:
2704 return UNLE_EXPR;
2705 case UNLT_EXPR:
2706 return UNGT_EXPR;
2707 case UNLE_EXPR:
2708 return UNGE_EXPR;
2709 default:
2710 gcc_unreachable ();
2715 /* Convert a comparison tree code from an enum tree_code representation
2716 into a compcode bit-based encoding. This function is the inverse of
2717 compcode_to_comparison. */
2719 static enum comparison_code
2720 comparison_to_compcode (enum tree_code code)
2722 switch (code)
2724 case LT_EXPR:
2725 return COMPCODE_LT;
2726 case EQ_EXPR:
2727 return COMPCODE_EQ;
2728 case LE_EXPR:
2729 return COMPCODE_LE;
2730 case GT_EXPR:
2731 return COMPCODE_GT;
2732 case NE_EXPR:
2733 return COMPCODE_NE;
2734 case GE_EXPR:
2735 return COMPCODE_GE;
2736 case ORDERED_EXPR:
2737 return COMPCODE_ORD;
2738 case UNORDERED_EXPR:
2739 return COMPCODE_UNORD;
2740 case UNLT_EXPR:
2741 return COMPCODE_UNLT;
2742 case UNEQ_EXPR:
2743 return COMPCODE_UNEQ;
2744 case UNLE_EXPR:
2745 return COMPCODE_UNLE;
2746 case UNGT_EXPR:
2747 return COMPCODE_UNGT;
2748 case LTGT_EXPR:
2749 return COMPCODE_LTGT;
2750 case UNGE_EXPR:
2751 return COMPCODE_UNGE;
2752 default:
2753 gcc_unreachable ();
2757 /* Convert a compcode bit-based encoding of a comparison operator back
2758 to GCC's enum tree_code representation. This function is the
2759 inverse of comparison_to_compcode. */
2761 static enum tree_code
2762 compcode_to_comparison (enum comparison_code code)
2764 switch (code)
2766 case COMPCODE_LT:
2767 return LT_EXPR;
2768 case COMPCODE_EQ:
2769 return EQ_EXPR;
2770 case COMPCODE_LE:
2771 return LE_EXPR;
2772 case COMPCODE_GT:
2773 return GT_EXPR;
2774 case COMPCODE_NE:
2775 return NE_EXPR;
2776 case COMPCODE_GE:
2777 return GE_EXPR;
2778 case COMPCODE_ORD:
2779 return ORDERED_EXPR;
2780 case COMPCODE_UNORD:
2781 return UNORDERED_EXPR;
2782 case COMPCODE_UNLT:
2783 return UNLT_EXPR;
2784 case COMPCODE_UNEQ:
2785 return UNEQ_EXPR;
2786 case COMPCODE_UNLE:
2787 return UNLE_EXPR;
2788 case COMPCODE_UNGT:
2789 return UNGT_EXPR;
2790 case COMPCODE_LTGT:
2791 return LTGT_EXPR;
2792 case COMPCODE_UNGE:
2793 return UNGE_EXPR;
2794 default:
2795 gcc_unreachable ();
2799 /* Return true if COND1 tests the opposite condition of COND2. */
2801 bool
2802 inverse_conditions_p (const_tree cond1, const_tree cond2)
2804 return (COMPARISON_CLASS_P (cond1)
2805 && COMPARISON_CLASS_P (cond2)
2806 && (invert_tree_comparison
2807 (TREE_CODE (cond1),
2808 HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
2809 && operand_equal_p (TREE_OPERAND (cond1, 0),
2810 TREE_OPERAND (cond2, 0), 0)
2811 && operand_equal_p (TREE_OPERAND (cond1, 1),
2812 TREE_OPERAND (cond2, 1), 0));
2815 /* Return a tree for the comparison which is the combination of
2816 doing the AND or OR (depending on CODE) of the two operations LCODE
2817 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2818 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2819 if this makes the transformation invalid. */
2821 tree
2822 combine_comparisons (location_t loc,
2823 enum tree_code code, enum tree_code lcode,
2824 enum tree_code rcode, tree truth_type,
2825 tree ll_arg, tree lr_arg)
2827 bool honor_nans = HONOR_NANS (ll_arg);
2828 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2829 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2830 int compcode;
2832 switch (code)
2834 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2835 compcode = lcompcode & rcompcode;
2836 break;
2838 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2839 compcode = lcompcode | rcompcode;
2840 break;
2842 default:
2843 return NULL_TREE;
2846 if (!honor_nans)
2848 /* Eliminate unordered comparisons, as well as LTGT and ORD
2849 which are not used unless the mode has NaNs. */
2850 compcode &= ~COMPCODE_UNORD;
2851 if (compcode == COMPCODE_LTGT)
2852 compcode = COMPCODE_NE;
2853 else if (compcode == COMPCODE_ORD)
2854 compcode = COMPCODE_TRUE;
2856 else if (flag_trapping_math)
2858 /* Check that the original operation and the optimized ones will trap
2859 under the same condition. */
2860 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2861 && (lcompcode != COMPCODE_EQ)
2862 && (lcompcode != COMPCODE_ORD);
2863 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2864 && (rcompcode != COMPCODE_EQ)
2865 && (rcompcode != COMPCODE_ORD);
2866 bool trap = (compcode & COMPCODE_UNORD) == 0
2867 && (compcode != COMPCODE_EQ)
2868 && (compcode != COMPCODE_ORD);
2870 /* In a short-circuited boolean expression the LHS might be
2871 such that the RHS, if evaluated, will never trap. For
2872 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2873 if neither x nor y is NaN. (This is a mixed blessing: for
2874 example, the expression above will never trap, hence
2875 optimizing it to x < y would be invalid). */
2876 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2877 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2878 rtrap = false;
2880 /* If the comparison was short-circuited, and only the RHS
2881 trapped, we may now generate a spurious trap. */
2882 if (rtrap && !ltrap
2883 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2884 return NULL_TREE;
2886 /* If we changed the conditions that cause a trap, we lose. */
2887 if ((ltrap || rtrap) != trap)
2888 return NULL_TREE;
2891 if (compcode == COMPCODE_TRUE)
2892 return constant_boolean_node (true, truth_type);
2893 else if (compcode == COMPCODE_FALSE)
2894 return constant_boolean_node (false, truth_type);
2895 else
2897 enum tree_code tcode;
2899 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2900 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2904 /* Return nonzero if two operands (typically of the same tree node)
2905 are necessarily equal. FLAGS modifies behavior as follows:
2907 If OEP_ONLY_CONST is set, only return nonzero for constants.
2908 This function tests whether the operands are indistinguishable;
2909 it does not test whether they are equal using C's == operation.
2910 The distinction is important for IEEE floating point, because
2911 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2912 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2914 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2915 even though it may hold multiple values during a function.
2916 This is because a GCC tree node guarantees that nothing else is
2917 executed between the evaluation of its "operands" (which may often
2918 be evaluated in arbitrary order). Hence if the operands themselves
2919 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2920 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2921 unset means assuming isochronic (or instantaneous) tree equivalence.
2922 Unless comparing arbitrary expression trees, such as from different
2923 statements, this flag can usually be left unset.
2925 If OEP_PURE_SAME is set, then pure functions with identical arguments
2926 are considered the same. It is used when the caller has other ways
2927 to ensure that global memory is unchanged in between.
2929 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2930 not values of expressions.
2932 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2933 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2935 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2936 any operand with side effect. This is unnecesarily conservative in the
2937 case we know that arg0 and arg1 are in disjoint code paths (such as in
2938 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2939 addresses with TREE_CONSTANT flag set so we know that &var == &var
2940 even if var is volatile. */
2943 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2945 /* When checking, verify at the outermost operand_equal_p call that
2946 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2947 hash value. */
2948 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2950 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2952 if (arg0 != arg1)
2954 inchash::hash hstate0 (0), hstate1 (0);
2955 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2956 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2957 hashval_t h0 = hstate0.end ();
2958 hashval_t h1 = hstate1.end ();
2959 gcc_assert (h0 == h1);
2961 return 1;
2963 else
2964 return 0;
2967 STRIP_ANY_LOCATION_WRAPPER (arg0);
2968 STRIP_ANY_LOCATION_WRAPPER (arg1);
2970 /* If either is ERROR_MARK, they aren't equal. */
2971 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2972 || TREE_TYPE (arg0) == error_mark_node
2973 || TREE_TYPE (arg1) == error_mark_node)
2974 return 0;
2976 /* Similar, if either does not have a type (like a released SSA name),
2977 they aren't equal. */
2978 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2979 return 0;
2981 /* We cannot consider pointers to different address space equal. */
2982 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2983 && POINTER_TYPE_P (TREE_TYPE (arg1))
2984 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2985 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2986 return 0;
2988 /* Check equality of integer constants before bailing out due to
2989 precision differences. */
2990 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2992 /* Address of INTEGER_CST is not defined; check that we did not forget
2993 to drop the OEP_ADDRESS_OF flags. */
2994 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2995 return tree_int_cst_equal (arg0, arg1);
2998 if (!(flags & OEP_ADDRESS_OF))
3000 /* If both types don't have the same signedness, then we can't consider
3001 them equal. We must check this before the STRIP_NOPS calls
3002 because they may change the signedness of the arguments. As pointers
3003 strictly don't have a signedness, require either two pointers or
3004 two non-pointers as well. */
3005 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3006 || POINTER_TYPE_P (TREE_TYPE (arg0))
3007 != POINTER_TYPE_P (TREE_TYPE (arg1)))
3008 return 0;
3010 /* If both types don't have the same precision, then it is not safe
3011 to strip NOPs. */
3012 if (element_precision (TREE_TYPE (arg0))
3013 != element_precision (TREE_TYPE (arg1)))
3014 return 0;
3016 STRIP_NOPS (arg0);
3017 STRIP_NOPS (arg1);
3019 #if 0
3020 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3021 sanity check once the issue is solved. */
3022 else
3023 /* Addresses of conversions and SSA_NAMEs (and many other things)
3024 are not defined. Check that we did not forget to drop the
3025 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3026 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3027 && TREE_CODE (arg0) != SSA_NAME);
3028 #endif
3030 /* In case both args are comparisons but with different comparison
3031 code, try to swap the comparison operands of one arg to produce
3032 a match and compare that variant. */
3033 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3034 && COMPARISON_CLASS_P (arg0)
3035 && COMPARISON_CLASS_P (arg1))
3037 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3039 if (TREE_CODE (arg0) == swap_code)
3040 return operand_equal_p (TREE_OPERAND (arg0, 0),
3041 TREE_OPERAND (arg1, 1), flags)
3042 && operand_equal_p (TREE_OPERAND (arg0, 1),
3043 TREE_OPERAND (arg1, 0), flags);
3046 if (TREE_CODE (arg0) != TREE_CODE (arg1))
3048 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3049 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3051 else if (flags & OEP_ADDRESS_OF)
3053 /* If we are interested in comparing addresses ignore
3054 MEM_REF wrappings of the base that can appear just for
3055 TBAA reasons. */
3056 if (TREE_CODE (arg0) == MEM_REF
3057 && DECL_P (arg1)
3058 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3059 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3060 && integer_zerop (TREE_OPERAND (arg0, 1)))
3061 return 1;
3062 else if (TREE_CODE (arg1) == MEM_REF
3063 && DECL_P (arg0)
3064 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3065 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3066 && integer_zerop (TREE_OPERAND (arg1, 1)))
3067 return 1;
3068 return 0;
3070 else
3071 return 0;
3074 /* When not checking adddresses, this is needed for conversions and for
3075 COMPONENT_REF. Might as well play it safe and always test this. */
3076 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3077 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3078 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
3079 && !(flags & OEP_ADDRESS_OF)))
3080 return 0;
3082 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3083 We don't care about side effects in that case because the SAVE_EXPR
3084 takes care of that for us. In all other cases, two expressions are
3085 equal if they have no side effects. If we have two identical
3086 expressions with side effects that should be treated the same due
3087 to the only side effects being identical SAVE_EXPR's, that will
3088 be detected in the recursive calls below.
3089 If we are taking an invariant address of two identical objects
3090 they are necessarily equal as well. */
3091 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3092 && (TREE_CODE (arg0) == SAVE_EXPR
3093 || (flags & OEP_MATCH_SIDE_EFFECTS)
3094 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3095 return 1;
3097 /* Next handle constant cases, those for which we can return 1 even
3098 if ONLY_CONST is set. */
3099 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3100 switch (TREE_CODE (arg0))
3102 case INTEGER_CST:
3103 return tree_int_cst_equal (arg0, arg1);
3105 case FIXED_CST:
3106 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3107 TREE_FIXED_CST (arg1));
3109 case REAL_CST:
3110 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3111 return 1;
3114 if (!HONOR_SIGNED_ZEROS (arg0))
3116 /* If we do not distinguish between signed and unsigned zero,
3117 consider them equal. */
3118 if (real_zerop (arg0) && real_zerop (arg1))
3119 return 1;
3121 return 0;
3123 case VECTOR_CST:
3125 if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3126 != VECTOR_CST_LOG2_NPATTERNS (arg1))
3127 return 0;
3129 if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3130 != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3131 return 0;
3133 unsigned int count = vector_cst_encoded_nelts (arg0);
3134 for (unsigned int i = 0; i < count; ++i)
3135 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3136 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3137 return 0;
3138 return 1;
3141 case COMPLEX_CST:
3142 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3143 flags)
3144 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3145 flags));
3147 case STRING_CST:
3148 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3149 && ! memcmp (TREE_STRING_POINTER (arg0),
3150 TREE_STRING_POINTER (arg1),
3151 TREE_STRING_LENGTH (arg0)));
3153 case ADDR_EXPR:
3154 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3155 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3156 flags | OEP_ADDRESS_OF
3157 | OEP_MATCH_SIDE_EFFECTS);
3158 case CONSTRUCTOR:
3159 /* In GIMPLE empty constructors are allowed in initializers of
3160 aggregates. */
3161 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
3162 default:
3163 break;
3166 if (flags & OEP_ONLY_CONST)
3167 return 0;
3169 /* Define macros to test an operand from arg0 and arg1 for equality and a
3170 variant that allows null and views null as being different from any
3171 non-null value. In the latter case, if either is null, the both
3172 must be; otherwise, do the normal comparison. */
3173 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3174 TREE_OPERAND (arg1, N), flags)
3176 #define OP_SAME_WITH_NULL(N) \
3177 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3178 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3180 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3182 case tcc_unary:
3183 /* Two conversions are equal only if signedness and modes match. */
3184 switch (TREE_CODE (arg0))
3186 CASE_CONVERT:
3187 case FIX_TRUNC_EXPR:
3188 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3189 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3190 return 0;
3191 break;
3192 default:
3193 break;
3196 return OP_SAME (0);
3199 case tcc_comparison:
3200 case tcc_binary:
3201 if (OP_SAME (0) && OP_SAME (1))
3202 return 1;
3204 /* For commutative ops, allow the other order. */
3205 return (commutative_tree_code (TREE_CODE (arg0))
3206 && operand_equal_p (TREE_OPERAND (arg0, 0),
3207 TREE_OPERAND (arg1, 1), flags)
3208 && operand_equal_p (TREE_OPERAND (arg0, 1),
3209 TREE_OPERAND (arg1, 0), flags));
3211 case tcc_reference:
3212 /* If either of the pointer (or reference) expressions we are
3213 dereferencing contain a side effect, these cannot be equal,
3214 but their addresses can be. */
3215 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3216 && (TREE_SIDE_EFFECTS (arg0)
3217 || TREE_SIDE_EFFECTS (arg1)))
3218 return 0;
3220 switch (TREE_CODE (arg0))
3222 case INDIRECT_REF:
3223 if (!(flags & OEP_ADDRESS_OF))
3225 if (TYPE_ALIGN (TREE_TYPE (arg0))
3226 != TYPE_ALIGN (TREE_TYPE (arg1)))
3227 return 0;
3228 /* Verify that the access types are compatible. */
3229 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg0))
3230 != TYPE_MAIN_VARIANT (TREE_TYPE (arg1)))
3231 return 0;
3233 flags &= ~OEP_ADDRESS_OF;
3234 return OP_SAME (0);
3236 case IMAGPART_EXPR:
3237 /* Require the same offset. */
3238 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3239 TYPE_SIZE (TREE_TYPE (arg1)),
3240 flags & ~OEP_ADDRESS_OF))
3241 return 0;
3243 /* Fallthru. */
3244 case REALPART_EXPR:
3245 case VIEW_CONVERT_EXPR:
3246 return OP_SAME (0);
3248 case TARGET_MEM_REF:
3249 case MEM_REF:
3250 if (!(flags & OEP_ADDRESS_OF))
3252 /* Require equal access sizes */
3253 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3254 && (!TYPE_SIZE (TREE_TYPE (arg0))
3255 || !TYPE_SIZE (TREE_TYPE (arg1))
3256 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3257 TYPE_SIZE (TREE_TYPE (arg1)),
3258 flags)))
3259 return 0;
3260 /* Verify that access happens in similar types. */
3261 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3262 return 0;
3263 /* Verify that accesses are TBAA compatible. */
3264 if (!alias_ptr_types_compatible_p
3265 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3266 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3267 || (MR_DEPENDENCE_CLIQUE (arg0)
3268 != MR_DEPENDENCE_CLIQUE (arg1))
3269 || (MR_DEPENDENCE_BASE (arg0)
3270 != MR_DEPENDENCE_BASE (arg1)))
3271 return 0;
3272 /* Verify that alignment is compatible. */
3273 if (TYPE_ALIGN (TREE_TYPE (arg0))
3274 != TYPE_ALIGN (TREE_TYPE (arg1)))
3275 return 0;
3277 flags &= ~OEP_ADDRESS_OF;
3278 return (OP_SAME (0) && OP_SAME (1)
3279 /* TARGET_MEM_REF require equal extra operands. */
3280 && (TREE_CODE (arg0) != TARGET_MEM_REF
3281 || (OP_SAME_WITH_NULL (2)
3282 && OP_SAME_WITH_NULL (3)
3283 && OP_SAME_WITH_NULL (4))));
3285 case ARRAY_REF:
3286 case ARRAY_RANGE_REF:
3287 if (!OP_SAME (0))
3288 return 0;
3289 flags &= ~OEP_ADDRESS_OF;
3290 /* Compare the array index by value if it is constant first as we
3291 may have different types but same value here. */
3292 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3293 TREE_OPERAND (arg1, 1))
3294 || OP_SAME (1))
3295 && OP_SAME_WITH_NULL (2)
3296 && OP_SAME_WITH_NULL (3)
3297 /* Compare low bound and element size as with OEP_ADDRESS_OF
3298 we have to account for the offset of the ref. */
3299 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3300 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3301 || (operand_equal_p (array_ref_low_bound
3302 (CONST_CAST_TREE (arg0)),
3303 array_ref_low_bound
3304 (CONST_CAST_TREE (arg1)), flags)
3305 && operand_equal_p (array_ref_element_size
3306 (CONST_CAST_TREE (arg0)),
3307 array_ref_element_size
3308 (CONST_CAST_TREE (arg1)),
3309 flags))));
3311 case COMPONENT_REF:
3312 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3313 may be NULL when we're called to compare MEM_EXPRs. */
3314 if (!OP_SAME_WITH_NULL (0)
3315 || !OP_SAME (1))
3316 return 0;
3317 flags &= ~OEP_ADDRESS_OF;
3318 return OP_SAME_WITH_NULL (2);
3320 case BIT_FIELD_REF:
3321 if (!OP_SAME (0))
3322 return 0;
3323 flags &= ~OEP_ADDRESS_OF;
3324 return OP_SAME (1) && OP_SAME (2);
3326 default:
3327 return 0;
3330 case tcc_expression:
3331 switch (TREE_CODE (arg0))
3333 case ADDR_EXPR:
3334 /* Be sure we pass right ADDRESS_OF flag. */
3335 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3336 return operand_equal_p (TREE_OPERAND (arg0, 0),
3337 TREE_OPERAND (arg1, 0),
3338 flags | OEP_ADDRESS_OF);
3340 case TRUTH_NOT_EXPR:
3341 return OP_SAME (0);
3343 case TRUTH_ANDIF_EXPR:
3344 case TRUTH_ORIF_EXPR:
3345 return OP_SAME (0) && OP_SAME (1);
3347 case WIDEN_MULT_PLUS_EXPR:
3348 case WIDEN_MULT_MINUS_EXPR:
3349 if (!OP_SAME (2))
3350 return 0;
3351 /* The multiplcation operands are commutative. */
3352 /* FALLTHRU */
3354 case TRUTH_AND_EXPR:
3355 case TRUTH_OR_EXPR:
3356 case TRUTH_XOR_EXPR:
3357 if (OP_SAME (0) && OP_SAME (1))
3358 return 1;
3360 /* Otherwise take into account this is a commutative operation. */
3361 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3362 TREE_OPERAND (arg1, 1), flags)
3363 && operand_equal_p (TREE_OPERAND (arg0, 1),
3364 TREE_OPERAND (arg1, 0), flags));
3366 case COND_EXPR:
3367 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3368 return 0;
3369 flags &= ~OEP_ADDRESS_OF;
3370 return OP_SAME (0);
3372 case BIT_INSERT_EXPR:
3373 /* BIT_INSERT_EXPR has an implict operand as the type precision
3374 of op1. Need to check to make sure they are the same. */
3375 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3376 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3377 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3378 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3379 return false;
3380 /* FALLTHRU */
3382 case VEC_COND_EXPR:
3383 case DOT_PROD_EXPR:
3384 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3386 case MODIFY_EXPR:
3387 case INIT_EXPR:
3388 case COMPOUND_EXPR:
3389 case PREDECREMENT_EXPR:
3390 case PREINCREMENT_EXPR:
3391 case POSTDECREMENT_EXPR:
3392 case POSTINCREMENT_EXPR:
3393 if (flags & OEP_LEXICOGRAPHIC)
3394 return OP_SAME (0) && OP_SAME (1);
3395 return 0;
3397 case CLEANUP_POINT_EXPR:
3398 case EXPR_STMT:
3399 case SAVE_EXPR:
3400 if (flags & OEP_LEXICOGRAPHIC)
3401 return OP_SAME (0);
3402 return 0;
3404 default:
3405 return 0;
3408 case tcc_vl_exp:
3409 switch (TREE_CODE (arg0))
3411 case CALL_EXPR:
3412 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3413 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3414 /* If not both CALL_EXPRs are either internal or normal function
3415 functions, then they are not equal. */
3416 return 0;
3417 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3419 /* If the CALL_EXPRs call different internal functions, then they
3420 are not equal. */
3421 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3422 return 0;
3424 else
3426 /* If the CALL_EXPRs call different functions, then they are not
3427 equal. */
3428 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3429 flags))
3430 return 0;
3433 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3435 unsigned int cef = call_expr_flags (arg0);
3436 if (flags & OEP_PURE_SAME)
3437 cef &= ECF_CONST | ECF_PURE;
3438 else
3439 cef &= ECF_CONST;
3440 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3441 return 0;
3444 /* Now see if all the arguments are the same. */
3446 const_call_expr_arg_iterator iter0, iter1;
3447 const_tree a0, a1;
3448 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3449 a1 = first_const_call_expr_arg (arg1, &iter1);
3450 a0 && a1;
3451 a0 = next_const_call_expr_arg (&iter0),
3452 a1 = next_const_call_expr_arg (&iter1))
3453 if (! operand_equal_p (a0, a1, flags))
3454 return 0;
3456 /* If we get here and both argument lists are exhausted
3457 then the CALL_EXPRs are equal. */
3458 return ! (a0 || a1);
3460 default:
3461 return 0;
3464 case tcc_declaration:
3465 /* Consider __builtin_sqrt equal to sqrt. */
3466 return (TREE_CODE (arg0) == FUNCTION_DECL
3467 && fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3468 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3469 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3471 case tcc_exceptional:
3472 if (TREE_CODE (arg0) == CONSTRUCTOR)
3474 /* In GIMPLE constructors are used only to build vectors from
3475 elements. Individual elements in the constructor must be
3476 indexed in increasing order and form an initial sequence.
3478 We make no effort to compare constructors in generic.
3479 (see sem_variable::equals in ipa-icf which can do so for
3480 constants). */
3481 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3482 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3483 return 0;
3485 /* Be sure that vectors constructed have the same representation.
3486 We only tested element precision and modes to match.
3487 Vectors may be BLKmode and thus also check that the number of
3488 parts match. */
3489 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
3490 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))))
3491 return 0;
3493 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3494 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3495 unsigned int len = vec_safe_length (v0);
3497 if (len != vec_safe_length (v1))
3498 return 0;
3500 for (unsigned int i = 0; i < len; i++)
3502 constructor_elt *c0 = &(*v0)[i];
3503 constructor_elt *c1 = &(*v1)[i];
3505 if (!operand_equal_p (c0->value, c1->value, flags)
3506 /* In GIMPLE the indexes can be either NULL or matching i.
3507 Double check this so we won't get false
3508 positives for GENERIC. */
3509 || (c0->index
3510 && (TREE_CODE (c0->index) != INTEGER_CST
3511 || !compare_tree_int (c0->index, i)))
3512 || (c1->index
3513 && (TREE_CODE (c1->index) != INTEGER_CST
3514 || !compare_tree_int (c1->index, i))))
3515 return 0;
3517 return 1;
3519 else if (TREE_CODE (arg0) == STATEMENT_LIST
3520 && (flags & OEP_LEXICOGRAPHIC))
3522 /* Compare the STATEMENT_LISTs. */
3523 tree_stmt_iterator tsi1, tsi2;
3524 tree body1 = CONST_CAST_TREE (arg0);
3525 tree body2 = CONST_CAST_TREE (arg1);
3526 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3527 tsi_next (&tsi1), tsi_next (&tsi2))
3529 /* The lists don't have the same number of statements. */
3530 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3531 return 0;
3532 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3533 return 1;
3534 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3535 flags & (OEP_LEXICOGRAPHIC
3536 | OEP_NO_HASH_CHECK)))
3537 return 0;
3540 return 0;
3542 case tcc_statement:
3543 switch (TREE_CODE (arg0))
3545 case RETURN_EXPR:
3546 if (flags & OEP_LEXICOGRAPHIC)
3547 return OP_SAME_WITH_NULL (0);
3548 return 0;
3549 case DEBUG_BEGIN_STMT:
3550 if (flags & OEP_LEXICOGRAPHIC)
3551 return 1;
3552 return 0;
3553 default:
3554 return 0;
3557 default:
3558 return 0;
3561 #undef OP_SAME
3562 #undef OP_SAME_WITH_NULL
3565 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
3566 with a different signedness or a narrower precision. */
3568 static bool
3569 operand_equal_for_comparison_p (tree arg0, tree arg1)
3571 if (operand_equal_p (arg0, arg1, 0))
3572 return true;
3574 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3575 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3576 return false;
3578 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3579 and see if the inner values are the same. This removes any
3580 signedness comparison, which doesn't matter here. */
3581 tree op0 = arg0;
3582 tree op1 = arg1;
3583 STRIP_NOPS (op0);
3584 STRIP_NOPS (op1);
3585 if (operand_equal_p (op0, op1, 0))
3586 return true;
3588 /* Discard a single widening conversion from ARG1 and see if the inner
3589 value is the same as ARG0. */
3590 if (CONVERT_EXPR_P (arg1)
3591 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3592 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3593 < TYPE_PRECISION (TREE_TYPE (arg1))
3594 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
3595 return true;
3597 return false;
3600 /* See if ARG is an expression that is either a comparison or is performing
3601 arithmetic on comparisons. The comparisons must only be comparing
3602 two different values, which will be stored in *CVAL1 and *CVAL2; if
3603 they are nonzero it means that some operands have already been found.
3604 No variables may be used anywhere else in the expression except in the
3605 comparisons.
3607 If this is true, return 1. Otherwise, return zero. */
3609 static int
3610 twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
3612 enum tree_code code = TREE_CODE (arg);
3613 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3615 /* We can handle some of the tcc_expression cases here. */
3616 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3617 tclass = tcc_unary;
3618 else if (tclass == tcc_expression
3619 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3620 || code == COMPOUND_EXPR))
3621 tclass = tcc_binary;
3623 switch (tclass)
3625 case tcc_unary:
3626 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
3628 case tcc_binary:
3629 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
3630 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
3632 case tcc_constant:
3633 return 1;
3635 case tcc_expression:
3636 if (code == COND_EXPR)
3637 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
3638 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
3639 && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
3640 return 0;
3642 case tcc_comparison:
3643 /* First see if we can handle the first operand, then the second. For
3644 the second operand, we know *CVAL1 can't be zero. It must be that
3645 one side of the comparison is each of the values; test for the
3646 case where this isn't true by failing if the two operands
3647 are the same. */
3649 if (operand_equal_p (TREE_OPERAND (arg, 0),
3650 TREE_OPERAND (arg, 1), 0))
3651 return 0;
3653 if (*cval1 == 0)
3654 *cval1 = TREE_OPERAND (arg, 0);
3655 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3657 else if (*cval2 == 0)
3658 *cval2 = TREE_OPERAND (arg, 0);
3659 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3661 else
3662 return 0;
3664 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3666 else if (*cval2 == 0)
3667 *cval2 = TREE_OPERAND (arg, 1);
3668 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3670 else
3671 return 0;
3673 return 1;
3675 default:
3676 return 0;
3680 /* ARG is a tree that is known to contain just arithmetic operations and
3681 comparisons. Evaluate the operations in the tree substituting NEW0 for
3682 any occurrence of OLD0 as an operand of a comparison and likewise for
3683 NEW1 and OLD1. */
3685 static tree
3686 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3687 tree old1, tree new1)
3689 tree type = TREE_TYPE (arg);
3690 enum tree_code code = TREE_CODE (arg);
3691 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3693 /* We can handle some of the tcc_expression cases here. */
3694 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3695 tclass = tcc_unary;
3696 else if (tclass == tcc_expression
3697 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3698 tclass = tcc_binary;
3700 switch (tclass)
3702 case tcc_unary:
3703 return fold_build1_loc (loc, code, type,
3704 eval_subst (loc, TREE_OPERAND (arg, 0),
3705 old0, new0, old1, new1));
3707 case tcc_binary:
3708 return fold_build2_loc (loc, code, type,
3709 eval_subst (loc, TREE_OPERAND (arg, 0),
3710 old0, new0, old1, new1),
3711 eval_subst (loc, TREE_OPERAND (arg, 1),
3712 old0, new0, old1, new1));
3714 case tcc_expression:
3715 switch (code)
3717 case SAVE_EXPR:
3718 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3719 old1, new1);
3721 case COMPOUND_EXPR:
3722 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3723 old1, new1);
3725 case COND_EXPR:
3726 return fold_build3_loc (loc, code, type,
3727 eval_subst (loc, TREE_OPERAND (arg, 0),
3728 old0, new0, old1, new1),
3729 eval_subst (loc, TREE_OPERAND (arg, 1),
3730 old0, new0, old1, new1),
3731 eval_subst (loc, TREE_OPERAND (arg, 2),
3732 old0, new0, old1, new1));
3733 default:
3734 break;
3736 /* Fall through - ??? */
3738 case tcc_comparison:
3740 tree arg0 = TREE_OPERAND (arg, 0);
3741 tree arg1 = TREE_OPERAND (arg, 1);
3743 /* We need to check both for exact equality and tree equality. The
3744 former will be true if the operand has a side-effect. In that
3745 case, we know the operand occurred exactly once. */
3747 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3748 arg0 = new0;
3749 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3750 arg0 = new1;
3752 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3753 arg1 = new0;
3754 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3755 arg1 = new1;
3757 return fold_build2_loc (loc, code, type, arg0, arg1);
3760 default:
3761 return arg;
3765 /* Return a tree for the case when the result of an expression is RESULT
3766 converted to TYPE and OMITTED was previously an operand of the expression
3767 but is now not needed (e.g., we folded OMITTED * 0).
3769 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3770 the conversion of RESULT to TYPE. */
3772 tree
3773 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3775 tree t = fold_convert_loc (loc, type, result);
3777 /* If the resulting operand is an empty statement, just return the omitted
3778 statement casted to void. */
3779 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3780 return build1_loc (loc, NOP_EXPR, void_type_node,
3781 fold_ignored_result (omitted));
3783 if (TREE_SIDE_EFFECTS (omitted))
3784 return build2_loc (loc, COMPOUND_EXPR, type,
3785 fold_ignored_result (omitted), t);
3787 return non_lvalue_loc (loc, t);
3790 /* Return a tree for the case when the result of an expression is RESULT
3791 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3792 of the expression but are now not needed.
3794 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3795 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3796 evaluated before OMITTED2. Otherwise, if neither has side effects,
3797 just do the conversion of RESULT to TYPE. */
3799 tree
3800 omit_two_operands_loc (location_t loc, tree type, tree result,
3801 tree omitted1, tree omitted2)
3803 tree t = fold_convert_loc (loc, type, result);
3805 if (TREE_SIDE_EFFECTS (omitted2))
3806 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3807 if (TREE_SIDE_EFFECTS (omitted1))
3808 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3810 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3814 /* Return a simplified tree node for the truth-negation of ARG. This
3815 never alters ARG itself. We assume that ARG is an operation that
3816 returns a truth value (0 or 1).
3818 FIXME: one would think we would fold the result, but it causes
3819 problems with the dominator optimizer. */
3821 static tree
3822 fold_truth_not_expr (location_t loc, tree arg)
3824 tree type = TREE_TYPE (arg);
3825 enum tree_code code = TREE_CODE (arg);
3826 location_t loc1, loc2;
3828 /* If this is a comparison, we can simply invert it, except for
3829 floating-point non-equality comparisons, in which case we just
3830 enclose a TRUTH_NOT_EXPR around what we have. */
3832 if (TREE_CODE_CLASS (code) == tcc_comparison)
3834 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3835 if (FLOAT_TYPE_P (op_type)
3836 && flag_trapping_math
3837 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3838 && code != NE_EXPR && code != EQ_EXPR)
3839 return NULL_TREE;
3841 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3842 if (code == ERROR_MARK)
3843 return NULL_TREE;
3845 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3846 TREE_OPERAND (arg, 1));
3847 if (TREE_NO_WARNING (arg))
3848 TREE_NO_WARNING (ret) = 1;
3849 return ret;
3852 switch (code)
3854 case INTEGER_CST:
3855 return constant_boolean_node (integer_zerop (arg), type);
3857 case TRUTH_AND_EXPR:
3858 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3859 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3860 return build2_loc (loc, TRUTH_OR_EXPR, type,
3861 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3862 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3864 case TRUTH_OR_EXPR:
3865 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3866 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3867 return build2_loc (loc, TRUTH_AND_EXPR, type,
3868 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3869 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3871 case TRUTH_XOR_EXPR:
3872 /* Here we can invert either operand. We invert the first operand
3873 unless the second operand is a TRUTH_NOT_EXPR in which case our
3874 result is the XOR of the first operand with the inside of the
3875 negation of the second operand. */
3877 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3878 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3879 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3880 else
3881 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3882 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3883 TREE_OPERAND (arg, 1));
3885 case TRUTH_ANDIF_EXPR:
3886 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3887 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3888 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3889 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3890 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3892 case TRUTH_ORIF_EXPR:
3893 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3894 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3895 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3896 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3897 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3899 case TRUTH_NOT_EXPR:
3900 return TREE_OPERAND (arg, 0);
3902 case COND_EXPR:
3904 tree arg1 = TREE_OPERAND (arg, 1);
3905 tree arg2 = TREE_OPERAND (arg, 2);
3907 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3908 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3910 /* A COND_EXPR may have a throw as one operand, which
3911 then has void type. Just leave void operands
3912 as they are. */
3913 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3914 VOID_TYPE_P (TREE_TYPE (arg1))
3915 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3916 VOID_TYPE_P (TREE_TYPE (arg2))
3917 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3920 case COMPOUND_EXPR:
3921 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3922 return build2_loc (loc, COMPOUND_EXPR, type,
3923 TREE_OPERAND (arg, 0),
3924 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3926 case NON_LVALUE_EXPR:
3927 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3928 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3930 CASE_CONVERT:
3931 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3932 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3934 /* fall through */
3936 case FLOAT_EXPR:
3937 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3938 return build1_loc (loc, TREE_CODE (arg), type,
3939 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3941 case BIT_AND_EXPR:
3942 if (!integer_onep (TREE_OPERAND (arg, 1)))
3943 return NULL_TREE;
3944 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3946 case SAVE_EXPR:
3947 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3949 case CLEANUP_POINT_EXPR:
3950 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3951 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3952 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3954 default:
3955 return NULL_TREE;
3959 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3960 assume that ARG is an operation that returns a truth value (0 or 1
3961 for scalars, 0 or -1 for vectors). Return the folded expression if
3962 folding is successful. Otherwise, return NULL_TREE. */
3964 static tree
3965 fold_invert_truthvalue (location_t loc, tree arg)
3967 tree type = TREE_TYPE (arg);
3968 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3969 ? BIT_NOT_EXPR
3970 : TRUTH_NOT_EXPR,
3971 type, arg);
3974 /* Return a simplified tree node for the truth-negation of ARG. This
3975 never alters ARG itself. We assume that ARG is an operation that
3976 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3978 tree
3979 invert_truthvalue_loc (location_t loc, tree arg)
3981 if (TREE_CODE (arg) == ERROR_MARK)
3982 return arg;
3984 tree type = TREE_TYPE (arg);
3985 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3986 ? BIT_NOT_EXPR
3987 : TRUTH_NOT_EXPR,
3988 type, arg);
3991 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3992 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3993 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3994 is the original memory reference used to preserve the alias set of
3995 the access. */
3997 static tree
3998 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3999 HOST_WIDE_INT bitsize, poly_int64 bitpos,
4000 int unsignedp, int reversep)
4002 tree result, bftype;
4004 /* Attempt not to lose the access path if possible. */
4005 if (TREE_CODE (orig_inner) == COMPONENT_REF)
4007 tree ninner = TREE_OPERAND (orig_inner, 0);
4008 machine_mode nmode;
4009 poly_int64 nbitsize, nbitpos;
4010 tree noffset;
4011 int nunsignedp, nreversep, nvolatilep = 0;
4012 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4013 &noffset, &nmode, &nunsignedp,
4014 &nreversep, &nvolatilep);
4015 if (base == inner
4016 && noffset == NULL_TREE
4017 && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4018 && !reversep
4019 && !nreversep
4020 && !nvolatilep)
4022 inner = ninner;
4023 bitpos -= nbitpos;
4027 alias_set_type iset = get_alias_set (orig_inner);
4028 if (iset == 0 && get_alias_set (inner) != iset)
4029 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4030 build_fold_addr_expr (inner),
4031 build_int_cst (ptr_type_node, 0));
4033 if (known_eq (bitpos, 0) && !reversep)
4035 tree size = TYPE_SIZE (TREE_TYPE (inner));
4036 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4037 || POINTER_TYPE_P (TREE_TYPE (inner)))
4038 && tree_fits_shwi_p (size)
4039 && tree_to_shwi (size) == bitsize)
4040 return fold_convert_loc (loc, type, inner);
4043 bftype = type;
4044 if (TYPE_PRECISION (bftype) != bitsize
4045 || TYPE_UNSIGNED (bftype) == !unsignedp)
4046 bftype = build_nonstandard_integer_type (bitsize, 0);
4048 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4049 bitsize_int (bitsize), bitsize_int (bitpos));
4050 REF_REVERSE_STORAGE_ORDER (result) = reversep;
4052 if (bftype != type)
4053 result = fold_convert_loc (loc, type, result);
4055 return result;
4058 /* Optimize a bit-field compare.
4060 There are two cases: First is a compare against a constant and the
4061 second is a comparison of two items where the fields are at the same
4062 bit position relative to the start of a chunk (byte, halfword, word)
4063 large enough to contain it. In these cases we can avoid the shift
4064 implicit in bitfield extractions.
4066 For constants, we emit a compare of the shifted constant with the
4067 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4068 compared. For two fields at the same position, we do the ANDs with the
4069 similar mask and compare the result of the ANDs.
4071 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4072 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4073 are the left and right operands of the comparison, respectively.
4075 If the optimization described above can be done, we return the resulting
4076 tree. Otherwise we return zero. */
4078 static tree
4079 optimize_bit_field_compare (location_t loc, enum tree_code code,
4080 tree compare_type, tree lhs, tree rhs)
4082 poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4083 HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4084 tree type = TREE_TYPE (lhs);
4085 tree unsigned_type;
4086 int const_p = TREE_CODE (rhs) == INTEGER_CST;
4087 machine_mode lmode, rmode;
4088 scalar_int_mode nmode;
4089 int lunsignedp, runsignedp;
4090 int lreversep, rreversep;
4091 int lvolatilep = 0, rvolatilep = 0;
4092 tree linner, rinner = NULL_TREE;
4093 tree mask;
4094 tree offset;
4096 /* Get all the information about the extractions being done. If the bit size
4097 is the same as the size of the underlying object, we aren't doing an
4098 extraction at all and so can do nothing. We also don't want to
4099 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4100 then will no longer be able to replace it. */
4101 linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4102 &lunsignedp, &lreversep, &lvolatilep);
4103 if (linner == lhs
4104 || !known_size_p (plbitsize)
4105 || !plbitsize.is_constant (&lbitsize)
4106 || !plbitpos.is_constant (&lbitpos)
4107 || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4108 || offset != 0
4109 || TREE_CODE (linner) == PLACEHOLDER_EXPR
4110 || lvolatilep)
4111 return 0;
4113 if (const_p)
4114 rreversep = lreversep;
4115 else
4117 /* If this is not a constant, we can only do something if bit positions,
4118 sizes, signedness and storage order are the same. */
4119 rinner
4120 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4121 &runsignedp, &rreversep, &rvolatilep);
4123 if (rinner == rhs
4124 || maybe_ne (lbitpos, rbitpos)
4125 || maybe_ne (lbitsize, rbitsize)
4126 || lunsignedp != runsignedp
4127 || lreversep != rreversep
4128 || offset != 0
4129 || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4130 || rvolatilep)
4131 return 0;
4134 /* Honor the C++ memory model and mimic what RTL expansion does. */
4135 poly_uint64 bitstart = 0;
4136 poly_uint64 bitend = 0;
4137 if (TREE_CODE (lhs) == COMPONENT_REF)
4139 get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4140 if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4141 return 0;
4144 /* See if we can find a mode to refer to this field. We should be able to,
4145 but fail if we can't. */
4146 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4147 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4148 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4149 TYPE_ALIGN (TREE_TYPE (rinner))),
4150 BITS_PER_WORD, false, &nmode))
4151 return 0;
4153 /* Set signed and unsigned types of the precision of this mode for the
4154 shifts below. */
4155 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4157 /* Compute the bit position and size for the new reference and our offset
4158 within it. If the new reference is the same size as the original, we
4159 won't optimize anything, so return zero. */
4160 nbitsize = GET_MODE_BITSIZE (nmode);
4161 nbitpos = lbitpos & ~ (nbitsize - 1);
4162 lbitpos -= nbitpos;
4163 if (nbitsize == lbitsize)
4164 return 0;
4166 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4167 lbitpos = nbitsize - lbitsize - lbitpos;
4169 /* Make the mask to be used against the extracted field. */
4170 mask = build_int_cst_type (unsigned_type, -1);
4171 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4172 mask = const_binop (RSHIFT_EXPR, mask,
4173 size_int (nbitsize - lbitsize - lbitpos));
4175 if (! const_p)
4177 if (nbitpos < 0)
4178 return 0;
4180 /* If not comparing with constant, just rework the comparison
4181 and return. */
4182 tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4183 nbitsize, nbitpos, 1, lreversep);
4184 t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4185 tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4186 nbitsize, nbitpos, 1, rreversep);
4187 t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4188 return fold_build2_loc (loc, code, compare_type, t1, t2);
4191 /* Otherwise, we are handling the constant case. See if the constant is too
4192 big for the field. Warn and return a tree for 0 (false) if so. We do
4193 this not only for its own sake, but to avoid having to test for this
4194 error case below. If we didn't, we might generate wrong code.
4196 For unsigned fields, the constant shifted right by the field length should
4197 be all zero. For signed fields, the high-order bits should agree with
4198 the sign bit. */
4200 if (lunsignedp)
4202 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4204 warning (0, "comparison is always %d due to width of bit-field",
4205 code == NE_EXPR);
4206 return constant_boolean_node (code == NE_EXPR, compare_type);
4209 else
4211 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4212 if (tem != 0 && tem != -1)
4214 warning (0, "comparison is always %d due to width of bit-field",
4215 code == NE_EXPR);
4216 return constant_boolean_node (code == NE_EXPR, compare_type);
4220 if (nbitpos < 0)
4221 return 0;
4223 /* Single-bit compares should always be against zero. */
4224 if (lbitsize == 1 && ! integer_zerop (rhs))
4226 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4227 rhs = build_int_cst (type, 0);
4230 /* Make a new bitfield reference, shift the constant over the
4231 appropriate number of bits and mask it with the computed mask
4232 (in case this was a signed field). If we changed it, make a new one. */
4233 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4234 nbitsize, nbitpos, 1, lreversep);
4236 rhs = const_binop (BIT_AND_EXPR,
4237 const_binop (LSHIFT_EXPR,
4238 fold_convert_loc (loc, unsigned_type, rhs),
4239 size_int (lbitpos)),
4240 mask);
4242 lhs = build2_loc (loc, code, compare_type,
4243 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4244 return lhs;
4247 /* Subroutine for fold_truth_andor_1: decode a field reference.
4249 If EXP is a comparison reference, we return the innermost reference.
4251 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4252 set to the starting bit number.
4254 If the innermost field can be completely contained in a mode-sized
4255 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4257 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4258 otherwise it is not changed.
4260 *PUNSIGNEDP is set to the signedness of the field.
4262 *PREVERSEP is set to the storage order of the field.
4264 *PMASK is set to the mask used. This is either contained in a
4265 BIT_AND_EXPR or derived from the width of the field.
4267 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4269 Return 0 if this is not a component reference or is one that we can't
4270 do anything with. */
4272 static tree
4273 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4274 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4275 int *punsignedp, int *preversep, int *pvolatilep,
4276 tree *pmask, tree *pand_mask)
4278 tree exp = *exp_;
4279 tree outer_type = 0;
4280 tree and_mask = 0;
4281 tree mask, inner, offset;
4282 tree unsigned_type;
4283 unsigned int precision;
4285 /* All the optimizations using this function assume integer fields.
4286 There are problems with FP fields since the type_for_size call
4287 below can fail for, e.g., XFmode. */
4288 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4289 return NULL_TREE;
4291 /* We are interested in the bare arrangement of bits, so strip everything
4292 that doesn't affect the machine mode. However, record the type of the
4293 outermost expression if it may matter below. */
4294 if (CONVERT_EXPR_P (exp)
4295 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4296 outer_type = TREE_TYPE (exp);
4297 STRIP_NOPS (exp);
4299 if (TREE_CODE (exp) == BIT_AND_EXPR)
4301 and_mask = TREE_OPERAND (exp, 1);
4302 exp = TREE_OPERAND (exp, 0);
4303 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4304 if (TREE_CODE (and_mask) != INTEGER_CST)
4305 return NULL_TREE;
4308 poly_int64 poly_bitsize, poly_bitpos;
4309 inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
4310 pmode, punsignedp, preversep, pvolatilep);
4311 if ((inner == exp && and_mask == 0)
4312 || !poly_bitsize.is_constant (pbitsize)
4313 || !poly_bitpos.is_constant (pbitpos)
4314 || *pbitsize < 0
4315 || offset != 0
4316 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4317 /* Reject out-of-bound accesses (PR79731). */
4318 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4319 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4320 *pbitpos + *pbitsize) < 0))
4321 return NULL_TREE;
4323 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4324 if (unsigned_type == NULL_TREE)
4325 return NULL_TREE;
4327 *exp_ = exp;
4329 /* If the number of bits in the reference is the same as the bitsize of
4330 the outer type, then the outer type gives the signedness. Otherwise
4331 (in case of a small bitfield) the signedness is unchanged. */
4332 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4333 *punsignedp = TYPE_UNSIGNED (outer_type);
4335 /* Compute the mask to access the bitfield. */
4336 precision = TYPE_PRECISION (unsigned_type);
4338 mask = build_int_cst_type (unsigned_type, -1);
4340 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4341 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4343 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4344 if (and_mask != 0)
4345 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4346 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4348 *pmask = mask;
4349 *pand_mask = and_mask;
4350 return inner;
4353 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4354 bit positions and MASK is SIGNED. */
4356 static int
4357 all_ones_mask_p (const_tree mask, unsigned int size)
4359 tree type = TREE_TYPE (mask);
4360 unsigned int precision = TYPE_PRECISION (type);
4362 /* If this function returns true when the type of the mask is
4363 UNSIGNED, then there will be errors. In particular see
4364 gcc.c-torture/execute/990326-1.c. There does not appear to be
4365 any documentation paper trail as to why this is so. But the pre
4366 wide-int worked with that restriction and it has been preserved
4367 here. */
4368 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4369 return false;
4371 return wi::mask (size, false, precision) == wi::to_wide (mask);
4374 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4375 represents the sign bit of EXP's type. If EXP represents a sign
4376 or zero extension, also test VAL against the unextended type.
4377 The return value is the (sub)expression whose sign bit is VAL,
4378 or NULL_TREE otherwise. */
4380 tree
4381 sign_bit_p (tree exp, const_tree val)
4383 int width;
4384 tree t;
4386 /* Tree EXP must have an integral type. */
4387 t = TREE_TYPE (exp);
4388 if (! INTEGRAL_TYPE_P (t))
4389 return NULL_TREE;
4391 /* Tree VAL must be an integer constant. */
4392 if (TREE_CODE (val) != INTEGER_CST
4393 || TREE_OVERFLOW (val))
4394 return NULL_TREE;
4396 width = TYPE_PRECISION (t);
4397 if (wi::only_sign_bit_p (wi::to_wide (val), width))
4398 return exp;
4400 /* Handle extension from a narrower type. */
4401 if (TREE_CODE (exp) == NOP_EXPR
4402 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4403 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4405 return NULL_TREE;
4408 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4409 to be evaluated unconditionally. */
4411 static int
4412 simple_operand_p (const_tree exp)
4414 /* Strip any conversions that don't change the machine mode. */
4415 STRIP_NOPS (exp);
4417 return (CONSTANT_CLASS_P (exp)
4418 || TREE_CODE (exp) == SSA_NAME
4419 || (DECL_P (exp)
4420 && ! TREE_ADDRESSABLE (exp)
4421 && ! TREE_THIS_VOLATILE (exp)
4422 && ! DECL_NONLOCAL (exp)
4423 /* Don't regard global variables as simple. They may be
4424 allocated in ways unknown to the compiler (shared memory,
4425 #pragma weak, etc). */
4426 && ! TREE_PUBLIC (exp)
4427 && ! DECL_EXTERNAL (exp)
4428 /* Weakrefs are not safe to be read, since they can be NULL.
4429 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4430 have DECL_WEAK flag set. */
4431 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4432 /* Loading a static variable is unduly expensive, but global
4433 registers aren't expensive. */
4434 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4437 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4438 to be evaluated unconditionally.
4439 I addition to simple_operand_p, we assume that comparisons, conversions,
4440 and logic-not operations are simple, if their operands are simple, too. */
4442 static bool
4443 simple_operand_p_2 (tree exp)
4445 enum tree_code code;
4447 if (TREE_SIDE_EFFECTS (exp)
4448 || tree_could_trap_p (exp))
4449 return false;
4451 while (CONVERT_EXPR_P (exp))
4452 exp = TREE_OPERAND (exp, 0);
4454 code = TREE_CODE (exp);
4456 if (TREE_CODE_CLASS (code) == tcc_comparison)
4457 return (simple_operand_p (TREE_OPERAND (exp, 0))
4458 && simple_operand_p (TREE_OPERAND (exp, 1)));
4460 if (code == TRUTH_NOT_EXPR)
4461 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4463 return simple_operand_p (exp);
4467 /* The following functions are subroutines to fold_range_test and allow it to
4468 try to change a logical combination of comparisons into a range test.
4470 For example, both
4471 X == 2 || X == 3 || X == 4 || X == 5
4473 X >= 2 && X <= 5
4474 are converted to
4475 (unsigned) (X - 2) <= 3
4477 We describe each set of comparisons as being either inside or outside
4478 a range, using a variable named like IN_P, and then describe the
4479 range with a lower and upper bound. If one of the bounds is omitted,
4480 it represents either the highest or lowest value of the type.
4482 In the comments below, we represent a range by two numbers in brackets
4483 preceded by a "+" to designate being inside that range, or a "-" to
4484 designate being outside that range, so the condition can be inverted by
4485 flipping the prefix. An omitted bound is represented by a "-". For
4486 example, "- [-, 10]" means being outside the range starting at the lowest
4487 possible value and ending at 10, in other words, being greater than 10.
4488 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4489 always false.
4491 We set up things so that the missing bounds are handled in a consistent
4492 manner so neither a missing bound nor "true" and "false" need to be
4493 handled using a special case. */
4495 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4496 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4497 and UPPER1_P are nonzero if the respective argument is an upper bound
4498 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4499 must be specified for a comparison. ARG1 will be converted to ARG0's
4500 type if both are specified. */
4502 static tree
4503 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4504 tree arg1, int upper1_p)
4506 tree tem;
4507 int result;
4508 int sgn0, sgn1;
4510 /* If neither arg represents infinity, do the normal operation.
4511 Else, if not a comparison, return infinity. Else handle the special
4512 comparison rules. Note that most of the cases below won't occur, but
4513 are handled for consistency. */
4515 if (arg0 != 0 && arg1 != 0)
4517 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4518 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4519 STRIP_NOPS (tem);
4520 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4523 if (TREE_CODE_CLASS (code) != tcc_comparison)
4524 return 0;
4526 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4527 for neither. In real maths, we cannot assume open ended ranges are
4528 the same. But, this is computer arithmetic, where numbers are finite.
4529 We can therefore make the transformation of any unbounded range with
4530 the value Z, Z being greater than any representable number. This permits
4531 us to treat unbounded ranges as equal. */
4532 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4533 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4534 switch (code)
4536 case EQ_EXPR:
4537 result = sgn0 == sgn1;
4538 break;
4539 case NE_EXPR:
4540 result = sgn0 != sgn1;
4541 break;
4542 case LT_EXPR:
4543 result = sgn0 < sgn1;
4544 break;
4545 case LE_EXPR:
4546 result = sgn0 <= sgn1;
4547 break;
4548 case GT_EXPR:
4549 result = sgn0 > sgn1;
4550 break;
4551 case GE_EXPR:
4552 result = sgn0 >= sgn1;
4553 break;
4554 default:
4555 gcc_unreachable ();
4558 return constant_boolean_node (result, type);
4561 /* Helper routine for make_range. Perform one step for it, return
4562 new expression if the loop should continue or NULL_TREE if it should
4563 stop. */
4565 tree
4566 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4567 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4568 bool *strict_overflow_p)
4570 tree arg0_type = TREE_TYPE (arg0);
4571 tree n_low, n_high, low = *p_low, high = *p_high;
4572 int in_p = *p_in_p, n_in_p;
4574 switch (code)
4576 case TRUTH_NOT_EXPR:
4577 /* We can only do something if the range is testing for zero. */
4578 if (low == NULL_TREE || high == NULL_TREE
4579 || ! integer_zerop (low) || ! integer_zerop (high))
4580 return NULL_TREE;
4581 *p_in_p = ! in_p;
4582 return arg0;
4584 case EQ_EXPR: case NE_EXPR:
4585 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4586 /* We can only do something if the range is testing for zero
4587 and if the second operand is an integer constant. Note that
4588 saying something is "in" the range we make is done by
4589 complementing IN_P since it will set in the initial case of
4590 being not equal to zero; "out" is leaving it alone. */
4591 if (low == NULL_TREE || high == NULL_TREE
4592 || ! integer_zerop (low) || ! integer_zerop (high)
4593 || TREE_CODE (arg1) != INTEGER_CST)
4594 return NULL_TREE;
4596 switch (code)
4598 case NE_EXPR: /* - [c, c] */
4599 low = high = arg1;
4600 break;
4601 case EQ_EXPR: /* + [c, c] */
4602 in_p = ! in_p, low = high = arg1;
4603 break;
4604 case GT_EXPR: /* - [-, c] */
4605 low = 0, high = arg1;
4606 break;
4607 case GE_EXPR: /* + [c, -] */
4608 in_p = ! in_p, low = arg1, high = 0;
4609 break;
4610 case LT_EXPR: /* - [c, -] */
4611 low = arg1, high = 0;
4612 break;
4613 case LE_EXPR: /* + [-, c] */
4614 in_p = ! in_p, low = 0, high = arg1;
4615 break;
4616 default:
4617 gcc_unreachable ();
4620 /* If this is an unsigned comparison, we also know that EXP is
4621 greater than or equal to zero. We base the range tests we make
4622 on that fact, so we record it here so we can parse existing
4623 range tests. We test arg0_type since often the return type
4624 of, e.g. EQ_EXPR, is boolean. */
4625 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4627 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4628 in_p, low, high, 1,
4629 build_int_cst (arg0_type, 0),
4630 NULL_TREE))
4631 return NULL_TREE;
4633 in_p = n_in_p, low = n_low, high = n_high;
4635 /* If the high bound is missing, but we have a nonzero low
4636 bound, reverse the range so it goes from zero to the low bound
4637 minus 1. */
4638 if (high == 0 && low && ! integer_zerop (low))
4640 in_p = ! in_p;
4641 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4642 build_int_cst (TREE_TYPE (low), 1), 0);
4643 low = build_int_cst (arg0_type, 0);
4647 *p_low = low;
4648 *p_high = high;
4649 *p_in_p = in_p;
4650 return arg0;
4652 case NEGATE_EXPR:
4653 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4654 low and high are non-NULL, then normalize will DTRT. */
4655 if (!TYPE_UNSIGNED (arg0_type)
4656 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4658 if (low == NULL_TREE)
4659 low = TYPE_MIN_VALUE (arg0_type);
4660 if (high == NULL_TREE)
4661 high = TYPE_MAX_VALUE (arg0_type);
4664 /* (-x) IN [a,b] -> x in [-b, -a] */
4665 n_low = range_binop (MINUS_EXPR, exp_type,
4666 build_int_cst (exp_type, 0),
4667 0, high, 1);
4668 n_high = range_binop (MINUS_EXPR, exp_type,
4669 build_int_cst (exp_type, 0),
4670 0, low, 0);
4671 if (n_high != 0 && TREE_OVERFLOW (n_high))
4672 return NULL_TREE;
4673 goto normalize;
4675 case BIT_NOT_EXPR:
4676 /* ~ X -> -X - 1 */
4677 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4678 build_int_cst (exp_type, 1));
4680 case PLUS_EXPR:
4681 case MINUS_EXPR:
4682 if (TREE_CODE (arg1) != INTEGER_CST)
4683 return NULL_TREE;
4685 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4686 move a constant to the other side. */
4687 if (!TYPE_UNSIGNED (arg0_type)
4688 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4689 return NULL_TREE;
4691 /* If EXP is signed, any overflow in the computation is undefined,
4692 so we don't worry about it so long as our computations on
4693 the bounds don't overflow. For unsigned, overflow is defined
4694 and this is exactly the right thing. */
4695 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4696 arg0_type, low, 0, arg1, 0);
4697 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4698 arg0_type, high, 1, arg1, 0);
4699 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4700 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4701 return NULL_TREE;
4703 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4704 *strict_overflow_p = true;
4706 normalize:
4707 /* Check for an unsigned range which has wrapped around the maximum
4708 value thus making n_high < n_low, and normalize it. */
4709 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4711 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4712 build_int_cst (TREE_TYPE (n_high), 1), 0);
4713 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4714 build_int_cst (TREE_TYPE (n_low), 1), 0);
4716 /* If the range is of the form +/- [ x+1, x ], we won't
4717 be able to normalize it. But then, it represents the
4718 whole range or the empty set, so make it
4719 +/- [ -, - ]. */
4720 if (tree_int_cst_equal (n_low, low)
4721 && tree_int_cst_equal (n_high, high))
4722 low = high = 0;
4723 else
4724 in_p = ! in_p;
4726 else
4727 low = n_low, high = n_high;
4729 *p_low = low;
4730 *p_high = high;
4731 *p_in_p = in_p;
4732 return arg0;
4734 CASE_CONVERT:
4735 case NON_LVALUE_EXPR:
4736 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4737 return NULL_TREE;
4739 if (! INTEGRAL_TYPE_P (arg0_type)
4740 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4741 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4742 return NULL_TREE;
4744 n_low = low, n_high = high;
4746 if (n_low != 0)
4747 n_low = fold_convert_loc (loc, arg0_type, n_low);
4749 if (n_high != 0)
4750 n_high = fold_convert_loc (loc, arg0_type, n_high);
4752 /* If we're converting arg0 from an unsigned type, to exp,
4753 a signed type, we will be doing the comparison as unsigned.
4754 The tests above have already verified that LOW and HIGH
4755 are both positive.
4757 So we have to ensure that we will handle large unsigned
4758 values the same way that the current signed bounds treat
4759 negative values. */
4761 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4763 tree high_positive;
4764 tree equiv_type;
4765 /* For fixed-point modes, we need to pass the saturating flag
4766 as the 2nd parameter. */
4767 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4768 equiv_type
4769 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4770 TYPE_SATURATING (arg0_type));
4771 else
4772 equiv_type
4773 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4775 /* A range without an upper bound is, naturally, unbounded.
4776 Since convert would have cropped a very large value, use
4777 the max value for the destination type. */
4778 high_positive
4779 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4780 : TYPE_MAX_VALUE (arg0_type);
4782 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4783 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4784 fold_convert_loc (loc, arg0_type,
4785 high_positive),
4786 build_int_cst (arg0_type, 1));
4788 /* If the low bound is specified, "and" the range with the
4789 range for which the original unsigned value will be
4790 positive. */
4791 if (low != 0)
4793 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4794 1, fold_convert_loc (loc, arg0_type,
4795 integer_zero_node),
4796 high_positive))
4797 return NULL_TREE;
4799 in_p = (n_in_p == in_p);
4801 else
4803 /* Otherwise, "or" the range with the range of the input
4804 that will be interpreted as negative. */
4805 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4806 1, fold_convert_loc (loc, arg0_type,
4807 integer_zero_node),
4808 high_positive))
4809 return NULL_TREE;
4811 in_p = (in_p != n_in_p);
4815 *p_low = n_low;
4816 *p_high = n_high;
4817 *p_in_p = in_p;
4818 return arg0;
4820 default:
4821 return NULL_TREE;
4825 /* Given EXP, a logical expression, set the range it is testing into
4826 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4827 actually being tested. *PLOW and *PHIGH will be made of the same
4828 type as the returned expression. If EXP is not a comparison, we
4829 will most likely not be returning a useful value and range. Set
4830 *STRICT_OVERFLOW_P to true if the return value is only valid
4831 because signed overflow is undefined; otherwise, do not change
4832 *STRICT_OVERFLOW_P. */
4834 tree
4835 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4836 bool *strict_overflow_p)
4838 enum tree_code code;
4839 tree arg0, arg1 = NULL_TREE;
4840 tree exp_type, nexp;
4841 int in_p;
4842 tree low, high;
4843 location_t loc = EXPR_LOCATION (exp);
4845 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4846 and see if we can refine the range. Some of the cases below may not
4847 happen, but it doesn't seem worth worrying about this. We "continue"
4848 the outer loop when we've changed something; otherwise we "break"
4849 the switch, which will "break" the while. */
4851 in_p = 0;
4852 low = high = build_int_cst (TREE_TYPE (exp), 0);
4854 while (1)
4856 code = TREE_CODE (exp);
4857 exp_type = TREE_TYPE (exp);
4858 arg0 = NULL_TREE;
4860 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4862 if (TREE_OPERAND_LENGTH (exp) > 0)
4863 arg0 = TREE_OPERAND (exp, 0);
4864 if (TREE_CODE_CLASS (code) == tcc_binary
4865 || TREE_CODE_CLASS (code) == tcc_comparison
4866 || (TREE_CODE_CLASS (code) == tcc_expression
4867 && TREE_OPERAND_LENGTH (exp) > 1))
4868 arg1 = TREE_OPERAND (exp, 1);
4870 if (arg0 == NULL_TREE)
4871 break;
4873 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4874 &high, &in_p, strict_overflow_p);
4875 if (nexp == NULL_TREE)
4876 break;
4877 exp = nexp;
4880 /* If EXP is a constant, we can evaluate whether this is true or false. */
4881 if (TREE_CODE (exp) == INTEGER_CST)
4883 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4884 exp, 0, low, 0))
4885 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4886 exp, 1, high, 1)));
4887 low = high = 0;
4888 exp = 0;
4891 *pin_p = in_p, *plow = low, *phigh = high;
4892 return exp;
4895 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4896 a bitwise check i.e. when
4897 LOW == 0xXX...X00...0
4898 HIGH == 0xXX...X11...1
4899 Return corresponding mask in MASK and stem in VALUE. */
4901 static bool
4902 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4903 tree *value)
4905 if (TREE_CODE (low) != INTEGER_CST
4906 || TREE_CODE (high) != INTEGER_CST)
4907 return false;
4909 unsigned prec = TYPE_PRECISION (type);
4910 wide_int lo = wi::to_wide (low, prec);
4911 wide_int hi = wi::to_wide (high, prec);
4913 wide_int end_mask = lo ^ hi;
4914 if ((end_mask & (end_mask + 1)) != 0
4915 || (lo & end_mask) != 0)
4916 return false;
4918 wide_int stem_mask = ~end_mask;
4919 wide_int stem = lo & stem_mask;
4920 if (stem != (hi & stem_mask))
4921 return false;
4923 *mask = wide_int_to_tree (type, stem_mask);
4924 *value = wide_int_to_tree (type, stem);
4926 return true;
4929 /* Helper routine for build_range_check and match.pd. Return the type to
4930 perform the check or NULL if it shouldn't be optimized. */
4932 tree
4933 range_check_type (tree etype)
4935 /* First make sure that arithmetics in this type is valid, then make sure
4936 that it wraps around. */
4937 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4938 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4939 TYPE_UNSIGNED (etype));
4941 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4943 tree utype, minv, maxv;
4945 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4946 for the type in question, as we rely on this here. */
4947 utype = unsigned_type_for (etype);
4948 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4949 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4950 build_int_cst (TREE_TYPE (maxv), 1), 1);
4951 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4953 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4954 minv, 1, maxv, 1)))
4955 etype = utype;
4956 else
4957 return NULL_TREE;
4959 return etype;
4962 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4963 type, TYPE, return an expression to test if EXP is in (or out of, depending
4964 on IN_P) the range. Return 0 if the test couldn't be created. */
4966 tree
4967 build_range_check (location_t loc, tree type, tree exp, int in_p,
4968 tree low, tree high)
4970 tree etype = TREE_TYPE (exp), mask, value;
4972 /* Disable this optimization for function pointer expressions
4973 on targets that require function pointer canonicalization. */
4974 if (targetm.have_canonicalize_funcptr_for_compare ()
4975 && POINTER_TYPE_P (etype)
4976 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
4977 return NULL_TREE;
4979 if (! in_p)
4981 value = build_range_check (loc, type, exp, 1, low, high);
4982 if (value != 0)
4983 return invert_truthvalue_loc (loc, value);
4985 return 0;
4988 if (low == 0 && high == 0)
4989 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4991 if (low == 0)
4992 return fold_build2_loc (loc, LE_EXPR, type, exp,
4993 fold_convert_loc (loc, etype, high));
4995 if (high == 0)
4996 return fold_build2_loc (loc, GE_EXPR, type, exp,
4997 fold_convert_loc (loc, etype, low));
4999 if (operand_equal_p (low, high, 0))
5000 return fold_build2_loc (loc, EQ_EXPR, type, exp,
5001 fold_convert_loc (loc, etype, low));
5003 if (TREE_CODE (exp) == BIT_AND_EXPR
5004 && maskable_range_p (low, high, etype, &mask, &value))
5005 return fold_build2_loc (loc, EQ_EXPR, type,
5006 fold_build2_loc (loc, BIT_AND_EXPR, etype,
5007 exp, mask),
5008 value);
5010 if (integer_zerop (low))
5012 if (! TYPE_UNSIGNED (etype))
5014 etype = unsigned_type_for (etype);
5015 high = fold_convert_loc (loc, etype, high);
5016 exp = fold_convert_loc (loc, etype, exp);
5018 return build_range_check (loc, type, exp, 1, 0, high);
5021 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5022 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5024 int prec = TYPE_PRECISION (etype);
5026 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5028 if (TYPE_UNSIGNED (etype))
5030 tree signed_etype = signed_type_for (etype);
5031 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5032 etype
5033 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5034 else
5035 etype = signed_etype;
5036 exp = fold_convert_loc (loc, etype, exp);
5038 return fold_build2_loc (loc, GT_EXPR, type, exp,
5039 build_int_cst (etype, 0));
5043 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5044 This requires wrap-around arithmetics for the type of the expression. */
5045 etype = range_check_type (etype);
5046 if (etype == NULL_TREE)
5047 return NULL_TREE;
5049 if (POINTER_TYPE_P (etype))
5050 etype = unsigned_type_for (etype);
5052 high = fold_convert_loc (loc, etype, high);
5053 low = fold_convert_loc (loc, etype, low);
5054 exp = fold_convert_loc (loc, etype, exp);
5056 value = const_binop (MINUS_EXPR, high, low);
5058 if (value != 0 && !TREE_OVERFLOW (value))
5059 return build_range_check (loc, type,
5060 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5061 1, build_int_cst (etype, 0), value);
5063 return 0;
5066 /* Return the predecessor of VAL in its type, handling the infinite case. */
5068 static tree
5069 range_predecessor (tree val)
5071 tree type = TREE_TYPE (val);
5073 if (INTEGRAL_TYPE_P (type)
5074 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5075 return 0;
5076 else
5077 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5078 build_int_cst (TREE_TYPE (val), 1), 0);
5081 /* Return the successor of VAL in its type, handling the infinite case. */
5083 static tree
5084 range_successor (tree val)
5086 tree type = TREE_TYPE (val);
5088 if (INTEGRAL_TYPE_P (type)
5089 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5090 return 0;
5091 else
5092 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5093 build_int_cst (TREE_TYPE (val), 1), 0);
5096 /* Given two ranges, see if we can merge them into one. Return 1 if we
5097 can, 0 if we can't. Set the output range into the specified parameters. */
5099 bool
5100 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5101 tree high0, int in1_p, tree low1, tree high1)
5103 int no_overlap;
5104 int subset;
5105 int temp;
5106 tree tem;
5107 int in_p;
5108 tree low, high;
5109 int lowequal = ((low0 == 0 && low1 == 0)
5110 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5111 low0, 0, low1, 0)));
5112 int highequal = ((high0 == 0 && high1 == 0)
5113 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5114 high0, 1, high1, 1)));
5116 /* Make range 0 be the range that starts first, or ends last if they
5117 start at the same value. Swap them if it isn't. */
5118 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5119 low0, 0, low1, 0))
5120 || (lowequal
5121 && integer_onep (range_binop (GT_EXPR, integer_type_node,
5122 high1, 1, high0, 1))))
5124 temp = in0_p, in0_p = in1_p, in1_p = temp;
5125 tem = low0, low0 = low1, low1 = tem;
5126 tem = high0, high0 = high1, high1 = tem;
5129 /* If the second range is != high1 where high1 is the type maximum of
5130 the type, try first merging with < high1 range. */
5131 if (low1
5132 && high1
5133 && TREE_CODE (low1) == INTEGER_CST
5134 && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5135 || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5136 && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5137 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5138 && operand_equal_p (low1, high1, 0))
5140 if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5141 && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5142 !in1_p, NULL_TREE, range_predecessor (low1)))
5143 return true;
5144 /* Similarly for the second range != low1 where low1 is the type minimum
5145 of the type, try first merging with > low1 range. */
5146 if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5147 && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5148 !in1_p, range_successor (low1), NULL_TREE))
5149 return true;
5152 /* Now flag two cases, whether the ranges are disjoint or whether the
5153 second range is totally subsumed in the first. Note that the tests
5154 below are simplified by the ones above. */
5155 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5156 high0, 1, low1, 0));
5157 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5158 high1, 1, high0, 1));
5160 /* We now have four cases, depending on whether we are including or
5161 excluding the two ranges. */
5162 if (in0_p && in1_p)
5164 /* If they don't overlap, the result is false. If the second range
5165 is a subset it is the result. Otherwise, the range is from the start
5166 of the second to the end of the first. */
5167 if (no_overlap)
5168 in_p = 0, low = high = 0;
5169 else if (subset)
5170 in_p = 1, low = low1, high = high1;
5171 else
5172 in_p = 1, low = low1, high = high0;
5175 else if (in0_p && ! in1_p)
5177 /* If they don't overlap, the result is the first range. If they are
5178 equal, the result is false. If the second range is a subset of the
5179 first, and the ranges begin at the same place, we go from just after
5180 the end of the second range to the end of the first. If the second
5181 range is not a subset of the first, or if it is a subset and both
5182 ranges end at the same place, the range starts at the start of the
5183 first range and ends just before the second range.
5184 Otherwise, we can't describe this as a single range. */
5185 if (no_overlap)
5186 in_p = 1, low = low0, high = high0;
5187 else if (lowequal && highequal)
5188 in_p = 0, low = high = 0;
5189 else if (subset && lowequal)
5191 low = range_successor (high1);
5192 high = high0;
5193 in_p = 1;
5194 if (low == 0)
5196 /* We are in the weird situation where high0 > high1 but
5197 high1 has no successor. Punt. */
5198 return 0;
5201 else if (! subset || highequal)
5203 low = low0;
5204 high = range_predecessor (low1);
5205 in_p = 1;
5206 if (high == 0)
5208 /* low0 < low1 but low1 has no predecessor. Punt. */
5209 return 0;
5212 else
5213 return 0;
5216 else if (! in0_p && in1_p)
5218 /* If they don't overlap, the result is the second range. If the second
5219 is a subset of the first, the result is false. Otherwise,
5220 the range starts just after the first range and ends at the
5221 end of the second. */
5222 if (no_overlap)
5223 in_p = 1, low = low1, high = high1;
5224 else if (subset || highequal)
5225 in_p = 0, low = high = 0;
5226 else
5228 low = range_successor (high0);
5229 high = high1;
5230 in_p = 1;
5231 if (low == 0)
5233 /* high1 > high0 but high0 has no successor. Punt. */
5234 return 0;
5239 else
5241 /* The case where we are excluding both ranges. Here the complex case
5242 is if they don't overlap. In that case, the only time we have a
5243 range is if they are adjacent. If the second is a subset of the
5244 first, the result is the first. Otherwise, the range to exclude
5245 starts at the beginning of the first range and ends at the end of the
5246 second. */
5247 if (no_overlap)
5249 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5250 range_successor (high0),
5251 1, low1, 0)))
5252 in_p = 0, low = low0, high = high1;
5253 else
5255 /* Canonicalize - [min, x] into - [-, x]. */
5256 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5257 switch (TREE_CODE (TREE_TYPE (low0)))
5259 case ENUMERAL_TYPE:
5260 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5261 GET_MODE_BITSIZE
5262 (TYPE_MODE (TREE_TYPE (low0)))))
5263 break;
5264 /* FALLTHROUGH */
5265 case INTEGER_TYPE:
5266 if (tree_int_cst_equal (low0,
5267 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5268 low0 = 0;
5269 break;
5270 case POINTER_TYPE:
5271 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5272 && integer_zerop (low0))
5273 low0 = 0;
5274 break;
5275 default:
5276 break;
5279 /* Canonicalize - [x, max] into - [x, -]. */
5280 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5281 switch (TREE_CODE (TREE_TYPE (high1)))
5283 case ENUMERAL_TYPE:
5284 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5285 GET_MODE_BITSIZE
5286 (TYPE_MODE (TREE_TYPE (high1)))))
5287 break;
5288 /* FALLTHROUGH */
5289 case INTEGER_TYPE:
5290 if (tree_int_cst_equal (high1,
5291 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5292 high1 = 0;
5293 break;
5294 case POINTER_TYPE:
5295 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5296 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5297 high1, 1,
5298 build_int_cst (TREE_TYPE (high1), 1),
5299 1)))
5300 high1 = 0;
5301 break;
5302 default:
5303 break;
5306 /* The ranges might be also adjacent between the maximum and
5307 minimum values of the given type. For
5308 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5309 return + [x + 1, y - 1]. */
5310 if (low0 == 0 && high1 == 0)
5312 low = range_successor (high0);
5313 high = range_predecessor (low1);
5314 if (low == 0 || high == 0)
5315 return 0;
5317 in_p = 1;
5319 else
5320 return 0;
5323 else if (subset)
5324 in_p = 0, low = low0, high = high0;
5325 else
5326 in_p = 0, low = low0, high = high1;
5329 *pin_p = in_p, *plow = low, *phigh = high;
5330 return 1;
5334 /* Subroutine of fold, looking inside expressions of the form
5335 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5336 of the COND_EXPR. This function is being used also to optimize
5337 A op B ? C : A, by reversing the comparison first.
5339 Return a folded expression whose code is not a COND_EXPR
5340 anymore, or NULL_TREE if no folding opportunity is found. */
5342 static tree
5343 fold_cond_expr_with_comparison (location_t loc, tree type,
5344 tree arg0, tree arg1, tree arg2)
5346 enum tree_code comp_code = TREE_CODE (arg0);
5347 tree arg00 = TREE_OPERAND (arg0, 0);
5348 tree arg01 = TREE_OPERAND (arg0, 1);
5349 tree arg1_type = TREE_TYPE (arg1);
5350 tree tem;
5352 STRIP_NOPS (arg1);
5353 STRIP_NOPS (arg2);
5355 /* If we have A op 0 ? A : -A, consider applying the following
5356 transformations:
5358 A == 0? A : -A same as -A
5359 A != 0? A : -A same as A
5360 A >= 0? A : -A same as abs (A)
5361 A > 0? A : -A same as abs (A)
5362 A <= 0? A : -A same as -abs (A)
5363 A < 0? A : -A same as -abs (A)
5365 None of these transformations work for modes with signed
5366 zeros. If A is +/-0, the first two transformations will
5367 change the sign of the result (from +0 to -0, or vice
5368 versa). The last four will fix the sign of the result,
5369 even though the original expressions could be positive or
5370 negative, depending on the sign of A.
5372 Note that all these transformations are correct if A is
5373 NaN, since the two alternatives (A and -A) are also NaNs. */
5374 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5375 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5376 ? real_zerop (arg01)
5377 : integer_zerop (arg01))
5378 && ((TREE_CODE (arg2) == NEGATE_EXPR
5379 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5380 /* In the case that A is of the form X-Y, '-A' (arg2) may
5381 have already been folded to Y-X, check for that. */
5382 || (TREE_CODE (arg1) == MINUS_EXPR
5383 && TREE_CODE (arg2) == MINUS_EXPR
5384 && operand_equal_p (TREE_OPERAND (arg1, 0),
5385 TREE_OPERAND (arg2, 1), 0)
5386 && operand_equal_p (TREE_OPERAND (arg1, 1),
5387 TREE_OPERAND (arg2, 0), 0))))
5388 switch (comp_code)
5390 case EQ_EXPR:
5391 case UNEQ_EXPR:
5392 tem = fold_convert_loc (loc, arg1_type, arg1);
5393 return fold_convert_loc (loc, type, negate_expr (tem));
5394 case NE_EXPR:
5395 case LTGT_EXPR:
5396 return fold_convert_loc (loc, type, arg1);
5397 case UNGE_EXPR:
5398 case UNGT_EXPR:
5399 if (flag_trapping_math)
5400 break;
5401 /* Fall through. */
5402 case GE_EXPR:
5403 case GT_EXPR:
5404 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5405 break;
5406 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5407 return fold_convert_loc (loc, type, tem);
5408 case UNLE_EXPR:
5409 case UNLT_EXPR:
5410 if (flag_trapping_math)
5411 break;
5412 /* FALLTHRU */
5413 case LE_EXPR:
5414 case LT_EXPR:
5415 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5416 break;
5417 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5418 return negate_expr (fold_convert_loc (loc, type, tem));
5419 default:
5420 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5421 break;
5424 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5425 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5426 both transformations are correct when A is NaN: A != 0
5427 is then true, and A == 0 is false. */
5429 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5430 && integer_zerop (arg01) && integer_zerop (arg2))
5432 if (comp_code == NE_EXPR)
5433 return fold_convert_loc (loc, type, arg1);
5434 else if (comp_code == EQ_EXPR)
5435 return build_zero_cst (type);
5438 /* Try some transformations of A op B ? A : B.
5440 A == B? A : B same as B
5441 A != B? A : B same as A
5442 A >= B? A : B same as max (A, B)
5443 A > B? A : B same as max (B, A)
5444 A <= B? A : B same as min (A, B)
5445 A < B? A : B same as min (B, A)
5447 As above, these transformations don't work in the presence
5448 of signed zeros. For example, if A and B are zeros of
5449 opposite sign, the first two transformations will change
5450 the sign of the result. In the last four, the original
5451 expressions give different results for (A=+0, B=-0) and
5452 (A=-0, B=+0), but the transformed expressions do not.
5454 The first two transformations are correct if either A or B
5455 is a NaN. In the first transformation, the condition will
5456 be false, and B will indeed be chosen. In the case of the
5457 second transformation, the condition A != B will be true,
5458 and A will be chosen.
5460 The conversions to max() and min() are not correct if B is
5461 a number and A is not. The conditions in the original
5462 expressions will be false, so all four give B. The min()
5463 and max() versions would give a NaN instead. */
5464 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5465 && operand_equal_for_comparison_p (arg01, arg2)
5466 /* Avoid these transformations if the COND_EXPR may be used
5467 as an lvalue in the C++ front-end. PR c++/19199. */
5468 && (in_gimple_form
5469 || VECTOR_TYPE_P (type)
5470 || (! lang_GNU_CXX ()
5471 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5472 || ! maybe_lvalue_p (arg1)
5473 || ! maybe_lvalue_p (arg2)))
5475 tree comp_op0 = arg00;
5476 tree comp_op1 = arg01;
5477 tree comp_type = TREE_TYPE (comp_op0);
5479 switch (comp_code)
5481 case EQ_EXPR:
5482 return fold_convert_loc (loc, type, arg2);
5483 case NE_EXPR:
5484 return fold_convert_loc (loc, type, arg1);
5485 case LE_EXPR:
5486 case LT_EXPR:
5487 case UNLE_EXPR:
5488 case UNLT_EXPR:
5489 /* In C++ a ?: expression can be an lvalue, so put the
5490 operand which will be used if they are equal first
5491 so that we can convert this back to the
5492 corresponding COND_EXPR. */
5493 if (!HONOR_NANS (arg1))
5495 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5496 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5497 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5498 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5499 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5500 comp_op1, comp_op0);
5501 return fold_convert_loc (loc, type, tem);
5503 break;
5504 case GE_EXPR:
5505 case GT_EXPR:
5506 case UNGE_EXPR:
5507 case UNGT_EXPR:
5508 if (!HONOR_NANS (arg1))
5510 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5511 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5512 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5513 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5514 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5515 comp_op1, comp_op0);
5516 return fold_convert_loc (loc, type, tem);
5518 break;
5519 case UNEQ_EXPR:
5520 if (!HONOR_NANS (arg1))
5521 return fold_convert_loc (loc, type, arg2);
5522 break;
5523 case LTGT_EXPR:
5524 if (!HONOR_NANS (arg1))
5525 return fold_convert_loc (loc, type, arg1);
5526 break;
5527 default:
5528 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5529 break;
5533 return NULL_TREE;
5538 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5539 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5540 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5541 false) >= 2)
5542 #endif
5544 /* EXP is some logical combination of boolean tests. See if we can
5545 merge it into some range test. Return the new tree if so. */
5547 static tree
5548 fold_range_test (location_t loc, enum tree_code code, tree type,
5549 tree op0, tree op1)
5551 int or_op = (code == TRUTH_ORIF_EXPR
5552 || code == TRUTH_OR_EXPR);
5553 int in0_p, in1_p, in_p;
5554 tree low0, low1, low, high0, high1, high;
5555 bool strict_overflow_p = false;
5556 tree tem, lhs, rhs;
5557 const char * const warnmsg = G_("assuming signed overflow does not occur "
5558 "when simplifying range test");
5560 if (!INTEGRAL_TYPE_P (type))
5561 return 0;
5563 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5564 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5566 /* If this is an OR operation, invert both sides; we will invert
5567 again at the end. */
5568 if (or_op)
5569 in0_p = ! in0_p, in1_p = ! in1_p;
5571 /* If both expressions are the same, if we can merge the ranges, and we
5572 can build the range test, return it or it inverted. If one of the
5573 ranges is always true or always false, consider it to be the same
5574 expression as the other. */
5575 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5576 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5577 in1_p, low1, high1)
5578 && (tem = (build_range_check (loc, type,
5579 lhs != 0 ? lhs
5580 : rhs != 0 ? rhs : integer_zero_node,
5581 in_p, low, high))) != 0)
5583 if (strict_overflow_p)
5584 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5585 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5588 /* On machines where the branch cost is expensive, if this is a
5589 short-circuited branch and the underlying object on both sides
5590 is the same, make a non-short-circuit operation. */
5591 bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
5592 if (PARAM_VALUE (PARAM_LOGICAL_OP_NON_SHORT_CIRCUIT) != -1)
5593 logical_op_non_short_circuit
5594 = PARAM_VALUE (PARAM_LOGICAL_OP_NON_SHORT_CIRCUIT);
5595 if (logical_op_non_short_circuit
5596 && !flag_sanitize_coverage
5597 && lhs != 0 && rhs != 0
5598 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
5599 && operand_equal_p (lhs, rhs, 0))
5601 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5602 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5603 which cases we can't do this. */
5604 if (simple_operand_p (lhs))
5605 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5606 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5607 type, op0, op1);
5609 else if (!lang_hooks.decls.global_bindings_p ()
5610 && !CONTAINS_PLACEHOLDER_P (lhs))
5612 tree common = save_expr (lhs);
5614 if ((lhs = build_range_check (loc, type, common,
5615 or_op ? ! in0_p : in0_p,
5616 low0, high0)) != 0
5617 && (rhs = build_range_check (loc, type, common,
5618 or_op ? ! in1_p : in1_p,
5619 low1, high1)) != 0)
5621 if (strict_overflow_p)
5622 fold_overflow_warning (warnmsg,
5623 WARN_STRICT_OVERFLOW_COMPARISON);
5624 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5625 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5626 type, lhs, rhs);
5631 return 0;
5634 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5635 bit value. Arrange things so the extra bits will be set to zero if and
5636 only if C is signed-extended to its full width. If MASK is nonzero,
5637 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5639 static tree
5640 unextend (tree c, int p, int unsignedp, tree mask)
5642 tree type = TREE_TYPE (c);
5643 int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
5644 tree temp;
5646 if (p == modesize || unsignedp)
5647 return c;
5649 /* We work by getting just the sign bit into the low-order bit, then
5650 into the high-order bit, then sign-extend. We then XOR that value
5651 with C. */
5652 temp = build_int_cst (TREE_TYPE (c),
5653 wi::extract_uhwi (wi::to_wide (c), p - 1, 1));
5655 /* We must use a signed type in order to get an arithmetic right shift.
5656 However, we must also avoid introducing accidental overflows, so that
5657 a subsequent call to integer_zerop will work. Hence we must
5658 do the type conversion here. At this point, the constant is either
5659 zero or one, and the conversion to a signed type can never overflow.
5660 We could get an overflow if this conversion is done anywhere else. */
5661 if (TYPE_UNSIGNED (type))
5662 temp = fold_convert (signed_type_for (type), temp);
5664 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5665 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5666 if (mask != 0)
5667 temp = const_binop (BIT_AND_EXPR, temp,
5668 fold_convert (TREE_TYPE (c), mask));
5669 /* If necessary, convert the type back to match the type of C. */
5670 if (TYPE_UNSIGNED (type))
5671 temp = fold_convert (type, temp);
5673 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5676 /* For an expression that has the form
5677 (A && B) || ~B
5679 (A || B) && ~B,
5680 we can drop one of the inner expressions and simplify to
5681 A || ~B
5683 A && ~B
5684 LOC is the location of the resulting expression. OP is the inner
5685 logical operation; the left-hand side in the examples above, while CMPOP
5686 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5687 removing a condition that guards another, as in
5688 (A != NULL && A->...) || A == NULL
5689 which we must not transform. If RHS_ONLY is true, only eliminate the
5690 right-most operand of the inner logical operation. */
5692 static tree
5693 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5694 bool rhs_only)
5696 tree type = TREE_TYPE (cmpop);
5697 enum tree_code code = TREE_CODE (cmpop);
5698 enum tree_code truthop_code = TREE_CODE (op);
5699 tree lhs = TREE_OPERAND (op, 0);
5700 tree rhs = TREE_OPERAND (op, 1);
5701 tree orig_lhs = lhs, orig_rhs = rhs;
5702 enum tree_code rhs_code = TREE_CODE (rhs);
5703 enum tree_code lhs_code = TREE_CODE (lhs);
5704 enum tree_code inv_code;
5706 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5707 return NULL_TREE;
5709 if (TREE_CODE_CLASS (code) != tcc_comparison)
5710 return NULL_TREE;
5712 if (rhs_code == truthop_code)
5714 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5715 if (newrhs != NULL_TREE)
5717 rhs = newrhs;
5718 rhs_code = TREE_CODE (rhs);
5721 if (lhs_code == truthop_code && !rhs_only)
5723 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5724 if (newlhs != NULL_TREE)
5726 lhs = newlhs;
5727 lhs_code = TREE_CODE (lhs);
5731 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5732 if (inv_code == rhs_code
5733 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5734 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5735 return lhs;
5736 if (!rhs_only && inv_code == lhs_code
5737 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5738 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5739 return rhs;
5740 if (rhs != orig_rhs || lhs != orig_lhs)
5741 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5742 lhs, rhs);
5743 return NULL_TREE;
5746 /* Find ways of folding logical expressions of LHS and RHS:
5747 Try to merge two comparisons to the same innermost item.
5748 Look for range tests like "ch >= '0' && ch <= '9'".
5749 Look for combinations of simple terms on machines with expensive branches
5750 and evaluate the RHS unconditionally.
5752 For example, if we have p->a == 2 && p->b == 4 and we can make an
5753 object large enough to span both A and B, we can do this with a comparison
5754 against the object ANDed with the a mask.
5756 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5757 operations to do this with one comparison.
5759 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5760 function and the one above.
5762 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5763 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5765 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5766 two operands.
5768 We return the simplified tree or 0 if no optimization is possible. */
5770 static tree
5771 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5772 tree lhs, tree rhs)
5774 /* If this is the "or" of two comparisons, we can do something if
5775 the comparisons are NE_EXPR. If this is the "and", we can do something
5776 if the comparisons are EQ_EXPR. I.e.,
5777 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5779 WANTED_CODE is this operation code. For single bit fields, we can
5780 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5781 comparison for one-bit fields. */
5783 enum tree_code wanted_code;
5784 enum tree_code lcode, rcode;
5785 tree ll_arg, lr_arg, rl_arg, rr_arg;
5786 tree ll_inner, lr_inner, rl_inner, rr_inner;
5787 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5788 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5789 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5790 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5791 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5792 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5793 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5794 scalar_int_mode lnmode, rnmode;
5795 tree ll_mask, lr_mask, rl_mask, rr_mask;
5796 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5797 tree l_const, r_const;
5798 tree lntype, rntype, result;
5799 HOST_WIDE_INT first_bit, end_bit;
5800 int volatilep;
5802 /* Start by getting the comparison codes. Fail if anything is volatile.
5803 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5804 it were surrounded with a NE_EXPR. */
5806 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5807 return 0;
5809 lcode = TREE_CODE (lhs);
5810 rcode = TREE_CODE (rhs);
5812 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5814 lhs = build2 (NE_EXPR, truth_type, lhs,
5815 build_int_cst (TREE_TYPE (lhs), 0));
5816 lcode = NE_EXPR;
5819 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5821 rhs = build2 (NE_EXPR, truth_type, rhs,
5822 build_int_cst (TREE_TYPE (rhs), 0));
5823 rcode = NE_EXPR;
5826 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5827 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5828 return 0;
5830 ll_arg = TREE_OPERAND (lhs, 0);
5831 lr_arg = TREE_OPERAND (lhs, 1);
5832 rl_arg = TREE_OPERAND (rhs, 0);
5833 rr_arg = TREE_OPERAND (rhs, 1);
5835 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5836 if (simple_operand_p (ll_arg)
5837 && simple_operand_p (lr_arg))
5839 if (operand_equal_p (ll_arg, rl_arg, 0)
5840 && operand_equal_p (lr_arg, rr_arg, 0))
5842 result = combine_comparisons (loc, code, lcode, rcode,
5843 truth_type, ll_arg, lr_arg);
5844 if (result)
5845 return result;
5847 else if (operand_equal_p (ll_arg, rr_arg, 0)
5848 && operand_equal_p (lr_arg, rl_arg, 0))
5850 result = combine_comparisons (loc, code, lcode,
5851 swap_tree_comparison (rcode),
5852 truth_type, ll_arg, lr_arg);
5853 if (result)
5854 return result;
5858 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5859 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5861 /* If the RHS can be evaluated unconditionally and its operands are
5862 simple, it wins to evaluate the RHS unconditionally on machines
5863 with expensive branches. In this case, this isn't a comparison
5864 that can be merged. */
5866 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5867 false) >= 2
5868 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5869 && simple_operand_p (rl_arg)
5870 && simple_operand_p (rr_arg))
5872 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5873 if (code == TRUTH_OR_EXPR
5874 && lcode == NE_EXPR && integer_zerop (lr_arg)
5875 && rcode == NE_EXPR && integer_zerop (rr_arg)
5876 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5877 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5878 return build2_loc (loc, NE_EXPR, truth_type,
5879 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5880 ll_arg, rl_arg),
5881 build_int_cst (TREE_TYPE (ll_arg), 0));
5883 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5884 if (code == TRUTH_AND_EXPR
5885 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5886 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5887 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5888 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5889 return build2_loc (loc, EQ_EXPR, truth_type,
5890 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5891 ll_arg, rl_arg),
5892 build_int_cst (TREE_TYPE (ll_arg), 0));
5895 /* See if the comparisons can be merged. Then get all the parameters for
5896 each side. */
5898 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5899 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5900 return 0;
5902 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5903 volatilep = 0;
5904 ll_inner = decode_field_reference (loc, &ll_arg,
5905 &ll_bitsize, &ll_bitpos, &ll_mode,
5906 &ll_unsignedp, &ll_reversep, &volatilep,
5907 &ll_mask, &ll_and_mask);
5908 lr_inner = decode_field_reference (loc, &lr_arg,
5909 &lr_bitsize, &lr_bitpos, &lr_mode,
5910 &lr_unsignedp, &lr_reversep, &volatilep,
5911 &lr_mask, &lr_and_mask);
5912 rl_inner = decode_field_reference (loc, &rl_arg,
5913 &rl_bitsize, &rl_bitpos, &rl_mode,
5914 &rl_unsignedp, &rl_reversep, &volatilep,
5915 &rl_mask, &rl_and_mask);
5916 rr_inner = decode_field_reference (loc, &rr_arg,
5917 &rr_bitsize, &rr_bitpos, &rr_mode,
5918 &rr_unsignedp, &rr_reversep, &volatilep,
5919 &rr_mask, &rr_and_mask);
5921 /* It must be true that the inner operation on the lhs of each
5922 comparison must be the same if we are to be able to do anything.
5923 Then see if we have constants. If not, the same must be true for
5924 the rhs's. */
5925 if (volatilep
5926 || ll_reversep != rl_reversep
5927 || ll_inner == 0 || rl_inner == 0
5928 || ! operand_equal_p (ll_inner, rl_inner, 0))
5929 return 0;
5931 if (TREE_CODE (lr_arg) == INTEGER_CST
5932 && TREE_CODE (rr_arg) == INTEGER_CST)
5934 l_const = lr_arg, r_const = rr_arg;
5935 lr_reversep = ll_reversep;
5937 else if (lr_reversep != rr_reversep
5938 || lr_inner == 0 || rr_inner == 0
5939 || ! operand_equal_p (lr_inner, rr_inner, 0))
5940 return 0;
5941 else
5942 l_const = r_const = 0;
5944 /* If either comparison code is not correct for our logical operation,
5945 fail. However, we can convert a one-bit comparison against zero into
5946 the opposite comparison against that bit being set in the field. */
5948 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5949 if (lcode != wanted_code)
5951 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5953 /* Make the left operand unsigned, since we are only interested
5954 in the value of one bit. Otherwise we are doing the wrong
5955 thing below. */
5956 ll_unsignedp = 1;
5957 l_const = ll_mask;
5959 else
5960 return 0;
5963 /* This is analogous to the code for l_const above. */
5964 if (rcode != wanted_code)
5966 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5968 rl_unsignedp = 1;
5969 r_const = rl_mask;
5971 else
5972 return 0;
5975 /* See if we can find a mode that contains both fields being compared on
5976 the left. If we can't, fail. Otherwise, update all constants and masks
5977 to be relative to a field of that size. */
5978 first_bit = MIN (ll_bitpos, rl_bitpos);
5979 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5980 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5981 TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
5982 volatilep, &lnmode))
5983 return 0;
5985 lnbitsize = GET_MODE_BITSIZE (lnmode);
5986 lnbitpos = first_bit & ~ (lnbitsize - 1);
5987 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5988 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5990 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5992 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5993 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5996 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5997 size_int (xll_bitpos));
5998 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5999 size_int (xrl_bitpos));
6001 if (l_const)
6003 l_const = fold_convert_loc (loc, lntype, l_const);
6004 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
6005 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
6006 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
6007 fold_build1_loc (loc, BIT_NOT_EXPR,
6008 lntype, ll_mask))))
6010 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6012 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6015 if (r_const)
6017 r_const = fold_convert_loc (loc, lntype, r_const);
6018 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
6019 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
6020 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
6021 fold_build1_loc (loc, BIT_NOT_EXPR,
6022 lntype, rl_mask))))
6024 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6026 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6030 /* If the right sides are not constant, do the same for it. Also,
6031 disallow this optimization if a size, signedness or storage order
6032 mismatch occurs between the left and right sides. */
6033 if (l_const == 0)
6035 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
6036 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
6037 || ll_reversep != lr_reversep
6038 /* Make sure the two fields on the right
6039 correspond to the left without being swapped. */
6040 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
6041 return 0;
6043 first_bit = MIN (lr_bitpos, rr_bitpos);
6044 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
6045 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
6046 TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
6047 volatilep, &rnmode))
6048 return 0;
6050 rnbitsize = GET_MODE_BITSIZE (rnmode);
6051 rnbitpos = first_bit & ~ (rnbitsize - 1);
6052 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
6053 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
6055 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
6057 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
6058 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
6061 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6062 rntype, lr_mask),
6063 size_int (xlr_bitpos));
6064 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6065 rntype, rr_mask),
6066 size_int (xrr_bitpos));
6068 /* Make a mask that corresponds to both fields being compared.
6069 Do this for both items being compared. If the operands are the
6070 same size and the bits being compared are in the same position
6071 then we can do this by masking both and comparing the masked
6072 results. */
6073 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6074 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
6075 if (lnbitsize == rnbitsize
6076 && xll_bitpos == xlr_bitpos
6077 && lnbitpos >= 0
6078 && rnbitpos >= 0)
6080 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
6081 lntype, lnbitsize, lnbitpos,
6082 ll_unsignedp || rl_unsignedp, ll_reversep);
6083 if (! all_ones_mask_p (ll_mask, lnbitsize))
6084 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6086 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
6087 rntype, rnbitsize, rnbitpos,
6088 lr_unsignedp || rr_unsignedp, lr_reversep);
6089 if (! all_ones_mask_p (lr_mask, rnbitsize))
6090 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6092 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6095 /* There is still another way we can do something: If both pairs of
6096 fields being compared are adjacent, we may be able to make a wider
6097 field containing them both.
6099 Note that we still must mask the lhs/rhs expressions. Furthermore,
6100 the mask must be shifted to account for the shift done by
6101 make_bit_field_ref. */
6102 if (((ll_bitsize + ll_bitpos == rl_bitpos
6103 && lr_bitsize + lr_bitpos == rr_bitpos)
6104 || (ll_bitpos == rl_bitpos + rl_bitsize
6105 && lr_bitpos == rr_bitpos + rr_bitsize))
6106 && ll_bitpos >= 0
6107 && rl_bitpos >= 0
6108 && lr_bitpos >= 0
6109 && rr_bitpos >= 0)
6111 tree type;
6113 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
6114 ll_bitsize + rl_bitsize,
6115 MIN (ll_bitpos, rl_bitpos),
6116 ll_unsignedp, ll_reversep);
6117 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
6118 lr_bitsize + rr_bitsize,
6119 MIN (lr_bitpos, rr_bitpos),
6120 lr_unsignedp, lr_reversep);
6122 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6123 size_int (MIN (xll_bitpos, xrl_bitpos)));
6124 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6125 size_int (MIN (xlr_bitpos, xrr_bitpos)));
6127 /* Convert to the smaller type before masking out unwanted bits. */
6128 type = lntype;
6129 if (lntype != rntype)
6131 if (lnbitsize > rnbitsize)
6133 lhs = fold_convert_loc (loc, rntype, lhs);
6134 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6135 type = rntype;
6137 else if (lnbitsize < rnbitsize)
6139 rhs = fold_convert_loc (loc, lntype, rhs);
6140 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6141 type = lntype;
6145 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6146 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6148 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6149 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6151 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6154 return 0;
6157 /* Handle the case of comparisons with constants. If there is something in
6158 common between the masks, those bits of the constants must be the same.
6159 If not, the condition is always false. Test for this to avoid generating
6160 incorrect code below. */
6161 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
6162 if (! integer_zerop (result)
6163 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
6164 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
6166 if (wanted_code == NE_EXPR)
6168 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6169 return constant_boolean_node (true, truth_type);
6171 else
6173 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6174 return constant_boolean_node (false, truth_type);
6178 if (lnbitpos < 0)
6179 return 0;
6181 /* Construct the expression we will return. First get the component
6182 reference we will make. Unless the mask is all ones the width of
6183 that field, perform the mask operation. Then compare with the
6184 merged constant. */
6185 result = make_bit_field_ref (loc, ll_inner, ll_arg,
6186 lntype, lnbitsize, lnbitpos,
6187 ll_unsignedp || rl_unsignedp, ll_reversep);
6189 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6190 if (! all_ones_mask_p (ll_mask, lnbitsize))
6191 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
6193 return build2_loc (loc, wanted_code, truth_type, result,
6194 const_binop (BIT_IOR_EXPR, l_const, r_const));
6197 /* T is an integer expression that is being multiplied, divided, or taken a
6198 modulus (CODE says which and what kind of divide or modulus) by a
6199 constant C. See if we can eliminate that operation by folding it with
6200 other operations already in T. WIDE_TYPE, if non-null, is a type that
6201 should be used for the computation if wider than our type.
6203 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6204 (X * 2) + (Y * 4). We must, however, be assured that either the original
6205 expression would not overflow or that overflow is undefined for the type
6206 in the language in question.
6208 If we return a non-null expression, it is an equivalent form of the
6209 original computation, but need not be in the original type.
6211 We set *STRICT_OVERFLOW_P to true if the return values depends on
6212 signed overflow being undefined. Otherwise we do not change
6213 *STRICT_OVERFLOW_P. */
6215 static tree
6216 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6217 bool *strict_overflow_p)
6219 /* To avoid exponential search depth, refuse to allow recursion past
6220 three levels. Beyond that (1) it's highly unlikely that we'll find
6221 something interesting and (2) we've probably processed it before
6222 when we built the inner expression. */
6224 static int depth;
6225 tree ret;
6227 if (depth > 3)
6228 return NULL;
6230 depth++;
6231 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6232 depth--;
6234 return ret;
6237 static tree
6238 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6239 bool *strict_overflow_p)
6241 tree type = TREE_TYPE (t);
6242 enum tree_code tcode = TREE_CODE (t);
6243 tree ctype = (wide_type != 0
6244 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6245 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6246 ? wide_type : type);
6247 tree t1, t2;
6248 int same_p = tcode == code;
6249 tree op0 = NULL_TREE, op1 = NULL_TREE;
6250 bool sub_strict_overflow_p;
6252 /* Don't deal with constants of zero here; they confuse the code below. */
6253 if (integer_zerop (c))
6254 return NULL_TREE;
6256 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6257 op0 = TREE_OPERAND (t, 0);
6259 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6260 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6262 /* Note that we need not handle conditional operations here since fold
6263 already handles those cases. So just do arithmetic here. */
6264 switch (tcode)
6266 case INTEGER_CST:
6267 /* For a constant, we can always simplify if we are a multiply
6268 or (for divide and modulus) if it is a multiple of our constant. */
6269 if (code == MULT_EXPR
6270 || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6271 TYPE_SIGN (type)))
6273 tree tem = const_binop (code, fold_convert (ctype, t),
6274 fold_convert (ctype, c));
6275 /* If the multiplication overflowed, we lost information on it.
6276 See PR68142 and PR69845. */
6277 if (TREE_OVERFLOW (tem))
6278 return NULL_TREE;
6279 return tem;
6281 break;
6283 CASE_CONVERT: case NON_LVALUE_EXPR:
6284 /* If op0 is an expression ... */
6285 if ((COMPARISON_CLASS_P (op0)
6286 || UNARY_CLASS_P (op0)
6287 || BINARY_CLASS_P (op0)
6288 || VL_EXP_CLASS_P (op0)
6289 || EXPRESSION_CLASS_P (op0))
6290 /* ... and has wrapping overflow, and its type is smaller
6291 than ctype, then we cannot pass through as widening. */
6292 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6293 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6294 && (TYPE_PRECISION (ctype)
6295 > TYPE_PRECISION (TREE_TYPE (op0))))
6296 /* ... or this is a truncation (t is narrower than op0),
6297 then we cannot pass through this narrowing. */
6298 || (TYPE_PRECISION (type)
6299 < TYPE_PRECISION (TREE_TYPE (op0)))
6300 /* ... or signedness changes for division or modulus,
6301 then we cannot pass through this conversion. */
6302 || (code != MULT_EXPR
6303 && (TYPE_UNSIGNED (ctype)
6304 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6305 /* ... or has undefined overflow while the converted to
6306 type has not, we cannot do the operation in the inner type
6307 as that would introduce undefined overflow. */
6308 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6309 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6310 && !TYPE_OVERFLOW_UNDEFINED (type))))
6311 break;
6313 /* Pass the constant down and see if we can make a simplification. If
6314 we can, replace this expression with the inner simplification for
6315 possible later conversion to our or some other type. */
6316 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6317 && TREE_CODE (t2) == INTEGER_CST
6318 && !TREE_OVERFLOW (t2)
6319 && (t1 = extract_muldiv (op0, t2, code,
6320 code == MULT_EXPR ? ctype : NULL_TREE,
6321 strict_overflow_p)) != 0)
6322 return t1;
6323 break;
6325 case ABS_EXPR:
6326 /* If widening the type changes it from signed to unsigned, then we
6327 must avoid building ABS_EXPR itself as unsigned. */
6328 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6330 tree cstype = (*signed_type_for) (ctype);
6331 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6332 != 0)
6334 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6335 return fold_convert (ctype, t1);
6337 break;
6339 /* If the constant is negative, we cannot simplify this. */
6340 if (tree_int_cst_sgn (c) == -1)
6341 break;
6342 /* FALLTHROUGH */
6343 case NEGATE_EXPR:
6344 /* For division and modulus, type can't be unsigned, as e.g.
6345 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6346 For signed types, even with wrapping overflow, this is fine. */
6347 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6348 break;
6349 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6350 != 0)
6351 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6352 break;
6354 case MIN_EXPR: case MAX_EXPR:
6355 /* If widening the type changes the signedness, then we can't perform
6356 this optimization as that changes the result. */
6357 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6358 break;
6360 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6361 sub_strict_overflow_p = false;
6362 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6363 &sub_strict_overflow_p)) != 0
6364 && (t2 = extract_muldiv (op1, c, code, wide_type,
6365 &sub_strict_overflow_p)) != 0)
6367 if (tree_int_cst_sgn (c) < 0)
6368 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6369 if (sub_strict_overflow_p)
6370 *strict_overflow_p = true;
6371 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6372 fold_convert (ctype, t2));
6374 break;
6376 case LSHIFT_EXPR: case RSHIFT_EXPR:
6377 /* If the second operand is constant, this is a multiplication
6378 or floor division, by a power of two, so we can treat it that
6379 way unless the multiplier or divisor overflows. Signed
6380 left-shift overflow is implementation-defined rather than
6381 undefined in C90, so do not convert signed left shift into
6382 multiplication. */
6383 if (TREE_CODE (op1) == INTEGER_CST
6384 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6385 /* const_binop may not detect overflow correctly,
6386 so check for it explicitly here. */
6387 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6388 wi::to_wide (op1))
6389 && (t1 = fold_convert (ctype,
6390 const_binop (LSHIFT_EXPR, size_one_node,
6391 op1))) != 0
6392 && !TREE_OVERFLOW (t1))
6393 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6394 ? MULT_EXPR : FLOOR_DIV_EXPR,
6395 ctype,
6396 fold_convert (ctype, op0),
6397 t1),
6398 c, code, wide_type, strict_overflow_p);
6399 break;
6401 case PLUS_EXPR: case MINUS_EXPR:
6402 /* See if we can eliminate the operation on both sides. If we can, we
6403 can return a new PLUS or MINUS. If we can't, the only remaining
6404 cases where we can do anything are if the second operand is a
6405 constant. */
6406 sub_strict_overflow_p = false;
6407 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6408 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6409 if (t1 != 0 && t2 != 0
6410 && TYPE_OVERFLOW_WRAPS (ctype)
6411 && (code == MULT_EXPR
6412 /* If not multiplication, we can only do this if both operands
6413 are divisible by c. */
6414 || (multiple_of_p (ctype, op0, c)
6415 && multiple_of_p (ctype, op1, c))))
6417 if (sub_strict_overflow_p)
6418 *strict_overflow_p = true;
6419 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6420 fold_convert (ctype, t2));
6423 /* If this was a subtraction, negate OP1 and set it to be an addition.
6424 This simplifies the logic below. */
6425 if (tcode == MINUS_EXPR)
6427 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6428 /* If OP1 was not easily negatable, the constant may be OP0. */
6429 if (TREE_CODE (op0) == INTEGER_CST)
6431 std::swap (op0, op1);
6432 std::swap (t1, t2);
6436 if (TREE_CODE (op1) != INTEGER_CST)
6437 break;
6439 /* If either OP1 or C are negative, this optimization is not safe for
6440 some of the division and remainder types while for others we need
6441 to change the code. */
6442 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6444 if (code == CEIL_DIV_EXPR)
6445 code = FLOOR_DIV_EXPR;
6446 else if (code == FLOOR_DIV_EXPR)
6447 code = CEIL_DIV_EXPR;
6448 else if (code != MULT_EXPR
6449 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6450 break;
6453 /* If it's a multiply or a division/modulus operation of a multiple
6454 of our constant, do the operation and verify it doesn't overflow. */
6455 if (code == MULT_EXPR
6456 || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6457 TYPE_SIGN (type)))
6459 op1 = const_binop (code, fold_convert (ctype, op1),
6460 fold_convert (ctype, c));
6461 /* We allow the constant to overflow with wrapping semantics. */
6462 if (op1 == 0
6463 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6464 break;
6466 else
6467 break;
6469 /* If we have an unsigned type, we cannot widen the operation since it
6470 will change the result if the original computation overflowed. */
6471 if (TYPE_UNSIGNED (ctype) && ctype != type)
6472 break;
6474 /* The last case is if we are a multiply. In that case, we can
6475 apply the distributive law to commute the multiply and addition
6476 if the multiplication of the constants doesn't overflow
6477 and overflow is defined. With undefined overflow
6478 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6479 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6480 return fold_build2 (tcode, ctype,
6481 fold_build2 (code, ctype,
6482 fold_convert (ctype, op0),
6483 fold_convert (ctype, c)),
6484 op1);
6486 break;
6488 case MULT_EXPR:
6489 /* We have a special case here if we are doing something like
6490 (C * 8) % 4 since we know that's zero. */
6491 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6492 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6493 /* If the multiplication can overflow we cannot optimize this. */
6494 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6495 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6496 && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6497 TYPE_SIGN (type)))
6499 *strict_overflow_p = true;
6500 return omit_one_operand (type, integer_zero_node, op0);
6503 /* ... fall through ... */
6505 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6506 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6507 /* If we can extract our operation from the LHS, do so and return a
6508 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6509 do something only if the second operand is a constant. */
6510 if (same_p
6511 && TYPE_OVERFLOW_WRAPS (ctype)
6512 && (t1 = extract_muldiv (op0, c, code, wide_type,
6513 strict_overflow_p)) != 0)
6514 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6515 fold_convert (ctype, op1));
6516 else if (tcode == MULT_EXPR && code == MULT_EXPR
6517 && TYPE_OVERFLOW_WRAPS (ctype)
6518 && (t1 = extract_muldiv (op1, c, code, wide_type,
6519 strict_overflow_p)) != 0)
6520 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6521 fold_convert (ctype, t1));
6522 else if (TREE_CODE (op1) != INTEGER_CST)
6523 return 0;
6525 /* If these are the same operation types, we can associate them
6526 assuming no overflow. */
6527 if (tcode == code)
6529 bool overflow_p = false;
6530 wi::overflow_type overflow_mul;
6531 signop sign = TYPE_SIGN (ctype);
6532 unsigned prec = TYPE_PRECISION (ctype);
6533 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6534 wi::to_wide (c, prec),
6535 sign, &overflow_mul);
6536 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6537 if (overflow_mul
6538 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6539 overflow_p = true;
6540 if (!overflow_p)
6541 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6542 wide_int_to_tree (ctype, mul));
6545 /* If these operations "cancel" each other, we have the main
6546 optimizations of this pass, which occur when either constant is a
6547 multiple of the other, in which case we replace this with either an
6548 operation or CODE or TCODE.
6550 If we have an unsigned type, we cannot do this since it will change
6551 the result if the original computation overflowed. */
6552 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6553 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6554 || (tcode == MULT_EXPR
6555 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6556 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6557 && code != MULT_EXPR)))
6559 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6560 TYPE_SIGN (type)))
6562 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6563 *strict_overflow_p = true;
6564 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6565 fold_convert (ctype,
6566 const_binop (TRUNC_DIV_EXPR,
6567 op1, c)));
6569 else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6570 TYPE_SIGN (type)))
6572 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6573 *strict_overflow_p = true;
6574 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6575 fold_convert (ctype,
6576 const_binop (TRUNC_DIV_EXPR,
6577 c, op1)));
6580 break;
6582 default:
6583 break;
6586 return 0;
6589 /* Return a node which has the indicated constant VALUE (either 0 or
6590 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6591 and is of the indicated TYPE. */
6593 tree
6594 constant_boolean_node (bool value, tree type)
6596 if (type == integer_type_node)
6597 return value ? integer_one_node : integer_zero_node;
6598 else if (type == boolean_type_node)
6599 return value ? boolean_true_node : boolean_false_node;
6600 else if (TREE_CODE (type) == VECTOR_TYPE)
6601 return build_vector_from_val (type,
6602 build_int_cst (TREE_TYPE (type),
6603 value ? -1 : 0));
6604 else
6605 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6609 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6610 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6611 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6612 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6613 COND is the first argument to CODE; otherwise (as in the example
6614 given here), it is the second argument. TYPE is the type of the
6615 original expression. Return NULL_TREE if no simplification is
6616 possible. */
6618 static tree
6619 fold_binary_op_with_conditional_arg (location_t loc,
6620 enum tree_code code,
6621 tree type, tree op0, tree op1,
6622 tree cond, tree arg, int cond_first_p)
6624 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6625 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6626 tree test, true_value, false_value;
6627 tree lhs = NULL_TREE;
6628 tree rhs = NULL_TREE;
6629 enum tree_code cond_code = COND_EXPR;
6631 /* Do not move possibly trapping operations into the conditional as this
6632 pessimizes code and causes gimplification issues when applied late. */
6633 if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
6634 ANY_INTEGRAL_TYPE_P (type)
6635 && TYPE_OVERFLOW_TRAPS (type), op1))
6636 return NULL_TREE;
6638 if (TREE_CODE (cond) == COND_EXPR
6639 || TREE_CODE (cond) == VEC_COND_EXPR)
6641 test = TREE_OPERAND (cond, 0);
6642 true_value = TREE_OPERAND (cond, 1);
6643 false_value = TREE_OPERAND (cond, 2);
6644 /* If this operand throws an expression, then it does not make
6645 sense to try to perform a logical or arithmetic operation
6646 involving it. */
6647 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6648 lhs = true_value;
6649 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6650 rhs = false_value;
6652 else if (!(TREE_CODE (type) != VECTOR_TYPE
6653 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6655 tree testtype = TREE_TYPE (cond);
6656 test = cond;
6657 true_value = constant_boolean_node (true, testtype);
6658 false_value = constant_boolean_node (false, testtype);
6660 else
6661 /* Detect the case of mixing vector and scalar types - bail out. */
6662 return NULL_TREE;
6664 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6665 cond_code = VEC_COND_EXPR;
6667 /* This transformation is only worthwhile if we don't have to wrap ARG
6668 in a SAVE_EXPR and the operation can be simplified without recursing
6669 on at least one of the branches once its pushed inside the COND_EXPR. */
6670 if (!TREE_CONSTANT (arg)
6671 && (TREE_SIDE_EFFECTS (arg)
6672 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6673 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6674 return NULL_TREE;
6676 arg = fold_convert_loc (loc, arg_type, arg);
6677 if (lhs == 0)
6679 true_value = fold_convert_loc (loc, cond_type, true_value);
6680 if (cond_first_p)
6681 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6682 else
6683 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6685 if (rhs == 0)
6687 false_value = fold_convert_loc (loc, cond_type, false_value);
6688 if (cond_first_p)
6689 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6690 else
6691 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6694 /* Check that we have simplified at least one of the branches. */
6695 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6696 return NULL_TREE;
6698 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6702 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6704 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6705 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6706 ADDEND is the same as X.
6708 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6709 and finite. The problematic cases are when X is zero, and its mode
6710 has signed zeros. In the case of rounding towards -infinity,
6711 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6712 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6714 bool
6715 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6717 if (!real_zerop (addend))
6718 return false;
6720 /* Don't allow the fold with -fsignaling-nans. */
6721 if (HONOR_SNANS (element_mode (type)))
6722 return false;
6724 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6725 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6726 return true;
6728 /* In a vector or complex, we would need to check the sign of all zeros. */
6729 if (TREE_CODE (addend) != REAL_CST)
6730 return false;
6732 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6733 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6734 negate = !negate;
6736 /* The mode has signed zeros, and we have to honor their sign.
6737 In this situation, there is only one case we can return true for.
6738 X - 0 is the same as X unless rounding towards -infinity is
6739 supported. */
6740 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6743 /* Subroutine of match.pd that optimizes comparisons of a division by
6744 a nonzero integer constant against an integer constant, i.e.
6745 X/C1 op C2.
6747 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6748 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6750 enum tree_code
6751 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
6752 tree *hi, bool *neg_overflow)
6754 tree prod, tmp, type = TREE_TYPE (c1);
6755 signop sign = TYPE_SIGN (type);
6756 wi::overflow_type overflow;
6758 /* We have to do this the hard way to detect unsigned overflow.
6759 prod = int_const_binop (MULT_EXPR, c1, c2); */
6760 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
6761 prod = force_fit_type (type, val, -1, overflow);
6762 *neg_overflow = false;
6764 if (sign == UNSIGNED)
6766 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6767 *lo = prod;
6769 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6770 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
6771 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
6773 else if (tree_int_cst_sgn (c1) >= 0)
6775 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6776 switch (tree_int_cst_sgn (c2))
6778 case -1:
6779 *neg_overflow = true;
6780 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
6781 *hi = prod;
6782 break;
6784 case 0:
6785 *lo = fold_negate_const (tmp, type);
6786 *hi = tmp;
6787 break;
6789 case 1:
6790 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
6791 *lo = prod;
6792 break;
6794 default:
6795 gcc_unreachable ();
6798 else
6800 /* A negative divisor reverses the relational operators. */
6801 code = swap_tree_comparison (code);
6803 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
6804 switch (tree_int_cst_sgn (c2))
6806 case -1:
6807 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
6808 *lo = prod;
6809 break;
6811 case 0:
6812 *hi = fold_negate_const (tmp, type);
6813 *lo = tmp;
6814 break;
6816 case 1:
6817 *neg_overflow = true;
6818 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
6819 *hi = prod;
6820 break;
6822 default:
6823 gcc_unreachable ();
6827 if (code != EQ_EXPR && code != NE_EXPR)
6828 return code;
6830 if (TREE_OVERFLOW (*lo)
6831 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
6832 *lo = NULL_TREE;
6833 if (TREE_OVERFLOW (*hi)
6834 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
6835 *hi = NULL_TREE;
6837 return code;
6841 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6842 equality/inequality test, then return a simplified form of the test
6843 using a sign testing. Otherwise return NULL. TYPE is the desired
6844 result type. */
6846 static tree
6847 fold_single_bit_test_into_sign_test (location_t loc,
6848 enum tree_code code, tree arg0, tree arg1,
6849 tree result_type)
6851 /* If this is testing a single bit, we can optimize the test. */
6852 if ((code == NE_EXPR || code == EQ_EXPR)
6853 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6854 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6856 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6857 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6858 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6860 if (arg00 != NULL_TREE
6861 /* This is only a win if casting to a signed type is cheap,
6862 i.e. when arg00's type is not a partial mode. */
6863 && type_has_mode_precision_p (TREE_TYPE (arg00)))
6865 tree stype = signed_type_for (TREE_TYPE (arg00));
6866 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6867 result_type,
6868 fold_convert_loc (loc, stype, arg00),
6869 build_int_cst (stype, 0));
6873 return NULL_TREE;
6876 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6877 equality/inequality test, then return a simplified form of
6878 the test using shifts and logical operations. Otherwise return
6879 NULL. TYPE is the desired result type. */
6881 tree
6882 fold_single_bit_test (location_t loc, enum tree_code code,
6883 tree arg0, tree arg1, tree result_type)
6885 /* If this is testing a single bit, we can optimize the test. */
6886 if ((code == NE_EXPR || code == EQ_EXPR)
6887 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6888 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6890 tree inner = TREE_OPERAND (arg0, 0);
6891 tree type = TREE_TYPE (arg0);
6892 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6893 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
6894 int ops_unsigned;
6895 tree signed_type, unsigned_type, intermediate_type;
6896 tree tem, one;
6898 /* First, see if we can fold the single bit test into a sign-bit
6899 test. */
6900 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6901 result_type);
6902 if (tem)
6903 return tem;
6905 /* Otherwise we have (A & C) != 0 where C is a single bit,
6906 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6907 Similarly for (A & C) == 0. */
6909 /* If INNER is a right shift of a constant and it plus BITNUM does
6910 not overflow, adjust BITNUM and INNER. */
6911 if (TREE_CODE (inner) == RSHIFT_EXPR
6912 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6913 && bitnum < TYPE_PRECISION (type)
6914 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner, 1)),
6915 TYPE_PRECISION (type) - bitnum))
6917 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6918 inner = TREE_OPERAND (inner, 0);
6921 /* If we are going to be able to omit the AND below, we must do our
6922 operations as unsigned. If we must use the AND, we have a choice.
6923 Normally unsigned is faster, but for some machines signed is. */
6924 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6925 && !flag_syntax_only) ? 0 : 1;
6927 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6928 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6929 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6930 inner = fold_convert_loc (loc, intermediate_type, inner);
6932 if (bitnum != 0)
6933 inner = build2 (RSHIFT_EXPR, intermediate_type,
6934 inner, size_int (bitnum));
6936 one = build_int_cst (intermediate_type, 1);
6938 if (code == EQ_EXPR)
6939 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6941 /* Put the AND last so it can combine with more things. */
6942 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6944 /* Make sure to return the proper type. */
6945 inner = fold_convert_loc (loc, result_type, inner);
6947 return inner;
6949 return NULL_TREE;
6952 /* Test whether it is preferable two swap two operands, ARG0 and
6953 ARG1, for example because ARG0 is an integer constant and ARG1
6954 isn't. */
6956 bool
6957 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6959 if (CONSTANT_CLASS_P (arg1))
6960 return 0;
6961 if (CONSTANT_CLASS_P (arg0))
6962 return 1;
6964 STRIP_NOPS (arg0);
6965 STRIP_NOPS (arg1);
6967 if (TREE_CONSTANT (arg1))
6968 return 0;
6969 if (TREE_CONSTANT (arg0))
6970 return 1;
6972 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6973 for commutative and comparison operators. Ensuring a canonical
6974 form allows the optimizers to find additional redundancies without
6975 having to explicitly check for both orderings. */
6976 if (TREE_CODE (arg0) == SSA_NAME
6977 && TREE_CODE (arg1) == SSA_NAME
6978 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6979 return 1;
6981 /* Put SSA_NAMEs last. */
6982 if (TREE_CODE (arg1) == SSA_NAME)
6983 return 0;
6984 if (TREE_CODE (arg0) == SSA_NAME)
6985 return 1;
6987 /* Put variables last. */
6988 if (DECL_P (arg1))
6989 return 0;
6990 if (DECL_P (arg0))
6991 return 1;
6993 return 0;
6997 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6998 means A >= Y && A != MAX, but in this case we know that
6999 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7001 static tree
7002 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7004 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7006 if (TREE_CODE (bound) == LT_EXPR)
7007 a = TREE_OPERAND (bound, 0);
7008 else if (TREE_CODE (bound) == GT_EXPR)
7009 a = TREE_OPERAND (bound, 1);
7010 else
7011 return NULL_TREE;
7013 typea = TREE_TYPE (a);
7014 if (!INTEGRAL_TYPE_P (typea)
7015 && !POINTER_TYPE_P (typea))
7016 return NULL_TREE;
7018 if (TREE_CODE (ineq) == LT_EXPR)
7020 a1 = TREE_OPERAND (ineq, 1);
7021 y = TREE_OPERAND (ineq, 0);
7023 else if (TREE_CODE (ineq) == GT_EXPR)
7025 a1 = TREE_OPERAND (ineq, 0);
7026 y = TREE_OPERAND (ineq, 1);
7028 else
7029 return NULL_TREE;
7031 if (TREE_TYPE (a1) != typea)
7032 return NULL_TREE;
7034 if (POINTER_TYPE_P (typea))
7036 /* Convert the pointer types into integer before taking the difference. */
7037 tree ta = fold_convert_loc (loc, ssizetype, a);
7038 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7039 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7041 else
7042 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7044 if (!diff || !integer_onep (diff))
7045 return NULL_TREE;
7047 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7050 /* Fold a sum or difference of at least one multiplication.
7051 Returns the folded tree or NULL if no simplification could be made. */
7053 static tree
7054 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7055 tree arg0, tree arg1)
7057 tree arg00, arg01, arg10, arg11;
7058 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7060 /* (A * C) +- (B * C) -> (A+-B) * C.
7061 (A * C) +- A -> A * (C+-1).
7062 We are most concerned about the case where C is a constant,
7063 but other combinations show up during loop reduction. Since
7064 it is not difficult, try all four possibilities. */
7066 if (TREE_CODE (arg0) == MULT_EXPR)
7068 arg00 = TREE_OPERAND (arg0, 0);
7069 arg01 = TREE_OPERAND (arg0, 1);
7071 else if (TREE_CODE (arg0) == INTEGER_CST)
7073 arg00 = build_one_cst (type);
7074 arg01 = arg0;
7076 else
7078 /* We cannot generate constant 1 for fract. */
7079 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7080 return NULL_TREE;
7081 arg00 = arg0;
7082 arg01 = build_one_cst (type);
7084 if (TREE_CODE (arg1) == MULT_EXPR)
7086 arg10 = TREE_OPERAND (arg1, 0);
7087 arg11 = TREE_OPERAND (arg1, 1);
7089 else if (TREE_CODE (arg1) == INTEGER_CST)
7091 arg10 = build_one_cst (type);
7092 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7093 the purpose of this canonicalization. */
7094 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7095 && negate_expr_p (arg1)
7096 && code == PLUS_EXPR)
7098 arg11 = negate_expr (arg1);
7099 code = MINUS_EXPR;
7101 else
7102 arg11 = arg1;
7104 else
7106 /* We cannot generate constant 1 for fract. */
7107 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7108 return NULL_TREE;
7109 arg10 = arg1;
7110 arg11 = build_one_cst (type);
7112 same = NULL_TREE;
7114 /* Prefer factoring a common non-constant. */
7115 if (operand_equal_p (arg00, arg10, 0))
7116 same = arg00, alt0 = arg01, alt1 = arg11;
7117 else if (operand_equal_p (arg01, arg11, 0))
7118 same = arg01, alt0 = arg00, alt1 = arg10;
7119 else if (operand_equal_p (arg00, arg11, 0))
7120 same = arg00, alt0 = arg01, alt1 = arg10;
7121 else if (operand_equal_p (arg01, arg10, 0))
7122 same = arg01, alt0 = arg00, alt1 = arg11;
7124 /* No identical multiplicands; see if we can find a common
7125 power-of-two factor in non-power-of-two multiplies. This
7126 can help in multi-dimensional array access. */
7127 else if (tree_fits_shwi_p (arg01)
7128 && tree_fits_shwi_p (arg11))
7130 HOST_WIDE_INT int01, int11, tmp;
7131 bool swap = false;
7132 tree maybe_same;
7133 int01 = tree_to_shwi (arg01);
7134 int11 = tree_to_shwi (arg11);
7136 /* Move min of absolute values to int11. */
7137 if (absu_hwi (int01) < absu_hwi (int11))
7139 tmp = int01, int01 = int11, int11 = tmp;
7140 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7141 maybe_same = arg01;
7142 swap = true;
7144 else
7145 maybe_same = arg11;
7147 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7148 /* The remainder should not be a constant, otherwise we
7149 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7150 increased the number of multiplications necessary. */
7151 && TREE_CODE (arg10) != INTEGER_CST)
7153 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7154 build_int_cst (TREE_TYPE (arg00),
7155 int01 / int11));
7156 alt1 = arg10;
7157 same = maybe_same;
7158 if (swap)
7159 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7163 if (!same)
7164 return NULL_TREE;
7166 if (! ANY_INTEGRAL_TYPE_P (type)
7167 || TYPE_OVERFLOW_WRAPS (type)
7168 /* We are neither factoring zero nor minus one. */
7169 || TREE_CODE (same) == INTEGER_CST)
7170 return fold_build2_loc (loc, MULT_EXPR, type,
7171 fold_build2_loc (loc, code, type,
7172 fold_convert_loc (loc, type, alt0),
7173 fold_convert_loc (loc, type, alt1)),
7174 fold_convert_loc (loc, type, same));
7176 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7177 same may be minus one and thus the multiplication may overflow. Perform
7178 the sum operation in an unsigned type. */
7179 tree utype = unsigned_type_for (type);
7180 tree tem = fold_build2_loc (loc, code, utype,
7181 fold_convert_loc (loc, utype, alt0),
7182 fold_convert_loc (loc, utype, alt1));
7183 /* If the sum evaluated to a constant that is not -INF the multiplication
7184 cannot overflow. */
7185 if (TREE_CODE (tem) == INTEGER_CST
7186 && (wi::to_wide (tem)
7187 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7188 return fold_build2_loc (loc, MULT_EXPR, type,
7189 fold_convert (type, tem), same);
7191 /* Do not resort to unsigned multiplication because
7192 we lose the no-overflow property of the expression. */
7193 return NULL_TREE;
7196 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7197 specified by EXPR into the buffer PTR of length LEN bytes.
7198 Return the number of bytes placed in the buffer, or zero
7199 upon failure. */
7201 static int
7202 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7204 tree type = TREE_TYPE (expr);
7205 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7206 int byte, offset, word, words;
7207 unsigned char value;
7209 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7210 return 0;
7211 if (off == -1)
7212 off = 0;
7214 if (ptr == NULL)
7215 /* Dry run. */
7216 return MIN (len, total_bytes - off);
7218 words = total_bytes / UNITS_PER_WORD;
7220 for (byte = 0; byte < total_bytes; byte++)
7222 int bitpos = byte * BITS_PER_UNIT;
7223 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7224 number of bytes. */
7225 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7227 if (total_bytes > UNITS_PER_WORD)
7229 word = byte / UNITS_PER_WORD;
7230 if (WORDS_BIG_ENDIAN)
7231 word = (words - 1) - word;
7232 offset = word * UNITS_PER_WORD;
7233 if (BYTES_BIG_ENDIAN)
7234 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7235 else
7236 offset += byte % UNITS_PER_WORD;
7238 else
7239 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7240 if (offset >= off && offset - off < len)
7241 ptr[offset - off] = value;
7243 return MIN (len, total_bytes - off);
7247 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7248 specified by EXPR into the buffer PTR of length LEN bytes.
7249 Return the number of bytes placed in the buffer, or zero
7250 upon failure. */
7252 static int
7253 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7255 tree type = TREE_TYPE (expr);
7256 scalar_mode mode = SCALAR_TYPE_MODE (type);
7257 int total_bytes = GET_MODE_SIZE (mode);
7258 FIXED_VALUE_TYPE value;
7259 tree i_value, i_type;
7261 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7262 return 0;
7264 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7266 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7267 return 0;
7269 value = TREE_FIXED_CST (expr);
7270 i_value = double_int_to_tree (i_type, value.data);
7272 return native_encode_int (i_value, ptr, len, off);
7276 /* Subroutine of native_encode_expr. Encode the REAL_CST
7277 specified by EXPR into the buffer PTR of length LEN bytes.
7278 Return the number of bytes placed in the buffer, or zero
7279 upon failure. */
7281 static int
7282 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7284 tree type = TREE_TYPE (expr);
7285 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7286 int byte, offset, word, words, bitpos;
7287 unsigned char value;
7289 /* There are always 32 bits in each long, no matter the size of
7290 the hosts long. We handle floating point representations with
7291 up to 192 bits. */
7292 long tmp[6];
7294 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7295 return 0;
7296 if (off == -1)
7297 off = 0;
7299 if (ptr == NULL)
7300 /* Dry run. */
7301 return MIN (len, total_bytes - off);
7303 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7305 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7307 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7308 bitpos += BITS_PER_UNIT)
7310 byte = (bitpos / BITS_PER_UNIT) & 3;
7311 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7313 if (UNITS_PER_WORD < 4)
7315 word = byte / UNITS_PER_WORD;
7316 if (WORDS_BIG_ENDIAN)
7317 word = (words - 1) - word;
7318 offset = word * UNITS_PER_WORD;
7319 if (BYTES_BIG_ENDIAN)
7320 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7321 else
7322 offset += byte % UNITS_PER_WORD;
7324 else
7326 offset = byte;
7327 if (BYTES_BIG_ENDIAN)
7329 /* Reverse bytes within each long, or within the entire float
7330 if it's smaller than a long (for HFmode). */
7331 offset = MIN (3, total_bytes - 1) - offset;
7332 gcc_assert (offset >= 0);
7335 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7336 if (offset >= off
7337 && offset - off < len)
7338 ptr[offset - off] = value;
7340 return MIN (len, total_bytes - off);
7343 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7344 specified by EXPR into the buffer PTR of length LEN bytes.
7345 Return the number of bytes placed in the buffer, or zero
7346 upon failure. */
7348 static int
7349 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7351 int rsize, isize;
7352 tree part;
7354 part = TREE_REALPART (expr);
7355 rsize = native_encode_expr (part, ptr, len, off);
7356 if (off == -1 && rsize == 0)
7357 return 0;
7358 part = TREE_IMAGPART (expr);
7359 if (off != -1)
7360 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7361 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7362 len - rsize, off);
7363 if (off == -1 && isize != rsize)
7364 return 0;
7365 return rsize + isize;
7369 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7370 specified by EXPR into the buffer PTR of length LEN bytes.
7371 Return the number of bytes placed in the buffer, or zero
7372 upon failure. */
7374 static int
7375 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7377 unsigned HOST_WIDE_INT i, count;
7378 int size, offset;
7379 tree itype, elem;
7381 offset = 0;
7382 if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7383 return 0;
7384 itype = TREE_TYPE (TREE_TYPE (expr));
7385 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7386 for (i = 0; i < count; i++)
7388 if (off >= size)
7390 off -= size;
7391 continue;
7393 elem = VECTOR_CST_ELT (expr, i);
7394 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7395 len - offset, off);
7396 if ((off == -1 && res != size) || res == 0)
7397 return 0;
7398 offset += res;
7399 if (offset >= len)
7400 return (off == -1 && i < count - 1) ? 0 : offset;
7401 if (off != -1)
7402 off = 0;
7404 return offset;
7408 /* Subroutine of native_encode_expr. Encode the STRING_CST
7409 specified by EXPR into the buffer PTR of length LEN bytes.
7410 Return the number of bytes placed in the buffer, or zero
7411 upon failure. */
7413 static int
7414 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7416 tree type = TREE_TYPE (expr);
7418 /* Wide-char strings are encoded in target byte-order so native
7419 encoding them is trivial. */
7420 if (BITS_PER_UNIT != CHAR_BIT
7421 || TREE_CODE (type) != ARRAY_TYPE
7422 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7423 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7424 return 0;
7426 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7427 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7428 return 0;
7429 if (off == -1)
7430 off = 0;
7431 if (ptr == NULL)
7432 /* Dry run. */;
7433 else if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7435 int written = 0;
7436 if (off < TREE_STRING_LENGTH (expr))
7438 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7439 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7441 memset (ptr + written, 0,
7442 MIN (total_bytes - written, len - written));
7444 else
7445 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7446 return MIN (total_bytes - off, len);
7450 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7451 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7452 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7453 anything, just do a dry run. If OFF is not -1 then start
7454 the encoding at byte offset OFF and encode at most LEN bytes.
7455 Return the number of bytes placed in the buffer, or zero upon failure. */
7458 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7460 /* We don't support starting at negative offset and -1 is special. */
7461 if (off < -1)
7462 return 0;
7464 switch (TREE_CODE (expr))
7466 case INTEGER_CST:
7467 return native_encode_int (expr, ptr, len, off);
7469 case REAL_CST:
7470 return native_encode_real (expr, ptr, len, off);
7472 case FIXED_CST:
7473 return native_encode_fixed (expr, ptr, len, off);
7475 case COMPLEX_CST:
7476 return native_encode_complex (expr, ptr, len, off);
7478 case VECTOR_CST:
7479 return native_encode_vector (expr, ptr, len, off);
7481 case STRING_CST:
7482 return native_encode_string (expr, ptr, len, off);
7484 default:
7485 return 0;
7490 /* Subroutine of native_interpret_expr. Interpret the contents of
7491 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7492 If the buffer cannot be interpreted, return NULL_TREE. */
7494 static tree
7495 native_interpret_int (tree type, const unsigned char *ptr, int len)
7497 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7499 if (total_bytes > len
7500 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7501 return NULL_TREE;
7503 wide_int result = wi::from_buffer (ptr, total_bytes);
7505 return wide_int_to_tree (type, result);
7509 /* Subroutine of native_interpret_expr. Interpret the contents of
7510 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7511 If the buffer cannot be interpreted, return NULL_TREE. */
7513 static tree
7514 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7516 scalar_mode mode = SCALAR_TYPE_MODE (type);
7517 int total_bytes = GET_MODE_SIZE (mode);
7518 double_int result;
7519 FIXED_VALUE_TYPE fixed_value;
7521 if (total_bytes > len
7522 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7523 return NULL_TREE;
7525 result = double_int::from_buffer (ptr, total_bytes);
7526 fixed_value = fixed_from_double_int (result, mode);
7528 return build_fixed (type, fixed_value);
7532 /* Subroutine of native_interpret_expr. Interpret the contents of
7533 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7534 If the buffer cannot be interpreted, return NULL_TREE. */
7536 static tree
7537 native_interpret_real (tree type, const unsigned char *ptr, int len)
7539 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
7540 int total_bytes = GET_MODE_SIZE (mode);
7541 unsigned char value;
7542 /* There are always 32 bits in each long, no matter the size of
7543 the hosts long. We handle floating point representations with
7544 up to 192 bits. */
7545 REAL_VALUE_TYPE r;
7546 long tmp[6];
7548 if (total_bytes > len || total_bytes > 24)
7549 return NULL_TREE;
7550 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7552 memset (tmp, 0, sizeof (tmp));
7553 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7554 bitpos += BITS_PER_UNIT)
7556 /* Both OFFSET and BYTE index within a long;
7557 bitpos indexes the whole float. */
7558 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7559 if (UNITS_PER_WORD < 4)
7561 int word = byte / UNITS_PER_WORD;
7562 if (WORDS_BIG_ENDIAN)
7563 word = (words - 1) - word;
7564 offset = word * UNITS_PER_WORD;
7565 if (BYTES_BIG_ENDIAN)
7566 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7567 else
7568 offset += byte % UNITS_PER_WORD;
7570 else
7572 offset = byte;
7573 if (BYTES_BIG_ENDIAN)
7575 /* Reverse bytes within each long, or within the entire float
7576 if it's smaller than a long (for HFmode). */
7577 offset = MIN (3, total_bytes - 1) - offset;
7578 gcc_assert (offset >= 0);
7581 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7583 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7586 real_from_target (&r, tmp, mode);
7587 return build_real (type, r);
7591 /* Subroutine of native_interpret_expr. Interpret the contents of
7592 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7593 If the buffer cannot be interpreted, return NULL_TREE. */
7595 static tree
7596 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7598 tree etype, rpart, ipart;
7599 int size;
7601 etype = TREE_TYPE (type);
7602 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7603 if (size * 2 > len)
7604 return NULL_TREE;
7605 rpart = native_interpret_expr (etype, ptr, size);
7606 if (!rpart)
7607 return NULL_TREE;
7608 ipart = native_interpret_expr (etype, ptr+size, size);
7609 if (!ipart)
7610 return NULL_TREE;
7611 return build_complex (type, rpart, ipart);
7615 /* Subroutine of native_interpret_expr. Interpret the contents of
7616 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7617 If the buffer cannot be interpreted, return NULL_TREE. */
7619 static tree
7620 native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
7622 tree etype, elem;
7623 unsigned int i, size;
7624 unsigned HOST_WIDE_INT count;
7626 etype = TREE_TYPE (type);
7627 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7628 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&count)
7629 || size * count > len)
7630 return NULL_TREE;
7632 tree_vector_builder elements (type, count, 1);
7633 for (i = 0; i < count; ++i)
7635 elem = native_interpret_expr (etype, ptr+(i*size), size);
7636 if (!elem)
7637 return NULL_TREE;
7638 elements.quick_push (elem);
7640 return elements.build ();
7644 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7645 the buffer PTR of length LEN as a constant of type TYPE. For
7646 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7647 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7648 return NULL_TREE. */
7650 tree
7651 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7653 switch (TREE_CODE (type))
7655 case INTEGER_TYPE:
7656 case ENUMERAL_TYPE:
7657 case BOOLEAN_TYPE:
7658 case POINTER_TYPE:
7659 case REFERENCE_TYPE:
7660 return native_interpret_int (type, ptr, len);
7662 case REAL_TYPE:
7663 return native_interpret_real (type, ptr, len);
7665 case FIXED_POINT_TYPE:
7666 return native_interpret_fixed (type, ptr, len);
7668 case COMPLEX_TYPE:
7669 return native_interpret_complex (type, ptr, len);
7671 case VECTOR_TYPE:
7672 return native_interpret_vector (type, ptr, len);
7674 default:
7675 return NULL_TREE;
7679 /* Returns true if we can interpret the contents of a native encoding
7680 as TYPE. */
7682 static bool
7683 can_native_interpret_type_p (tree type)
7685 switch (TREE_CODE (type))
7687 case INTEGER_TYPE:
7688 case ENUMERAL_TYPE:
7689 case BOOLEAN_TYPE:
7690 case POINTER_TYPE:
7691 case REFERENCE_TYPE:
7692 case FIXED_POINT_TYPE:
7693 case REAL_TYPE:
7694 case COMPLEX_TYPE:
7695 case VECTOR_TYPE:
7696 return true;
7697 default:
7698 return false;
7703 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7704 TYPE at compile-time. If we're unable to perform the conversion
7705 return NULL_TREE. */
7707 static tree
7708 fold_view_convert_expr (tree type, tree expr)
7710 /* We support up to 512-bit values (for V8DFmode). */
7711 unsigned char buffer[64];
7712 int len;
7714 /* Check that the host and target are sane. */
7715 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7716 return NULL_TREE;
7718 len = native_encode_expr (expr, buffer, sizeof (buffer));
7719 if (len == 0)
7720 return NULL_TREE;
7722 return native_interpret_expr (type, buffer, len);
7725 /* Build an expression for the address of T. Folds away INDIRECT_REF
7726 to avoid confusing the gimplify process. */
7728 tree
7729 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7731 /* The size of the object is not relevant when talking about its address. */
7732 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7733 t = TREE_OPERAND (t, 0);
7735 if (TREE_CODE (t) == INDIRECT_REF)
7737 t = TREE_OPERAND (t, 0);
7739 if (TREE_TYPE (t) != ptrtype)
7740 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7742 else if (TREE_CODE (t) == MEM_REF
7743 && integer_zerop (TREE_OPERAND (t, 1)))
7744 return TREE_OPERAND (t, 0);
7745 else if (TREE_CODE (t) == MEM_REF
7746 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7747 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7748 TREE_OPERAND (t, 0),
7749 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7750 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7752 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7754 if (TREE_TYPE (t) != ptrtype)
7755 t = fold_convert_loc (loc, ptrtype, t);
7757 else
7758 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7760 return t;
7763 /* Build an expression for the address of T. */
7765 tree
7766 build_fold_addr_expr_loc (location_t loc, tree t)
7768 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7770 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7773 /* Fold a unary expression of code CODE and type TYPE with operand
7774 OP0. Return the folded expression if folding is successful.
7775 Otherwise, return NULL_TREE. */
7777 tree
7778 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7780 tree tem;
7781 tree arg0;
7782 enum tree_code_class kind = TREE_CODE_CLASS (code);
7784 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7785 && TREE_CODE_LENGTH (code) == 1);
7787 arg0 = op0;
7788 if (arg0)
7790 if (CONVERT_EXPR_CODE_P (code)
7791 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7793 /* Don't use STRIP_NOPS, because signedness of argument type
7794 matters. */
7795 STRIP_SIGN_NOPS (arg0);
7797 else
7799 /* Strip any conversions that don't change the mode. This
7800 is safe for every expression, except for a comparison
7801 expression because its signedness is derived from its
7802 operands.
7804 Note that this is done as an internal manipulation within
7805 the constant folder, in order to find the simplest
7806 representation of the arguments so that their form can be
7807 studied. In any cases, the appropriate type conversions
7808 should be put back in the tree that will get out of the
7809 constant folder. */
7810 STRIP_NOPS (arg0);
7813 if (CONSTANT_CLASS_P (arg0))
7815 tree tem = const_unop (code, type, arg0);
7816 if (tem)
7818 if (TREE_TYPE (tem) != type)
7819 tem = fold_convert_loc (loc, type, tem);
7820 return tem;
7825 tem = generic_simplify (loc, code, type, op0);
7826 if (tem)
7827 return tem;
7829 if (TREE_CODE_CLASS (code) == tcc_unary)
7831 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7832 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7833 fold_build1_loc (loc, code, type,
7834 fold_convert_loc (loc, TREE_TYPE (op0),
7835 TREE_OPERAND (arg0, 1))));
7836 else if (TREE_CODE (arg0) == COND_EXPR)
7838 tree arg01 = TREE_OPERAND (arg0, 1);
7839 tree arg02 = TREE_OPERAND (arg0, 2);
7840 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7841 arg01 = fold_build1_loc (loc, code, type,
7842 fold_convert_loc (loc,
7843 TREE_TYPE (op0), arg01));
7844 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7845 arg02 = fold_build1_loc (loc, code, type,
7846 fold_convert_loc (loc,
7847 TREE_TYPE (op0), arg02));
7848 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7849 arg01, arg02);
7851 /* If this was a conversion, and all we did was to move into
7852 inside the COND_EXPR, bring it back out. But leave it if
7853 it is a conversion from integer to integer and the
7854 result precision is no wider than a word since such a
7855 conversion is cheap and may be optimized away by combine,
7856 while it couldn't if it were outside the COND_EXPR. Then return
7857 so we don't get into an infinite recursion loop taking the
7858 conversion out and then back in. */
7860 if ((CONVERT_EXPR_CODE_P (code)
7861 || code == NON_LVALUE_EXPR)
7862 && TREE_CODE (tem) == COND_EXPR
7863 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7864 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7865 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7866 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7867 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7868 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7869 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7870 && (INTEGRAL_TYPE_P
7871 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7872 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7873 || flag_syntax_only))
7874 tem = build1_loc (loc, code, type,
7875 build3 (COND_EXPR,
7876 TREE_TYPE (TREE_OPERAND
7877 (TREE_OPERAND (tem, 1), 0)),
7878 TREE_OPERAND (tem, 0),
7879 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7880 TREE_OPERAND (TREE_OPERAND (tem, 2),
7881 0)));
7882 return tem;
7886 switch (code)
7888 case NON_LVALUE_EXPR:
7889 if (!maybe_lvalue_p (op0))
7890 return fold_convert_loc (loc, type, op0);
7891 return NULL_TREE;
7893 CASE_CONVERT:
7894 case FLOAT_EXPR:
7895 case FIX_TRUNC_EXPR:
7896 if (COMPARISON_CLASS_P (op0))
7898 /* If we have (type) (a CMP b) and type is an integral type, return
7899 new expression involving the new type. Canonicalize
7900 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7901 non-integral type.
7902 Do not fold the result as that would not simplify further, also
7903 folding again results in recursions. */
7904 if (TREE_CODE (type) == BOOLEAN_TYPE)
7905 return build2_loc (loc, TREE_CODE (op0), type,
7906 TREE_OPERAND (op0, 0),
7907 TREE_OPERAND (op0, 1));
7908 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7909 && TREE_CODE (type) != VECTOR_TYPE)
7910 return build3_loc (loc, COND_EXPR, type, op0,
7911 constant_boolean_node (true, type),
7912 constant_boolean_node (false, type));
7915 /* Handle (T *)&A.B.C for A being of type T and B and C
7916 living at offset zero. This occurs frequently in
7917 C++ upcasting and then accessing the base. */
7918 if (TREE_CODE (op0) == ADDR_EXPR
7919 && POINTER_TYPE_P (type)
7920 && handled_component_p (TREE_OPERAND (op0, 0)))
7922 poly_int64 bitsize, bitpos;
7923 tree offset;
7924 machine_mode mode;
7925 int unsignedp, reversep, volatilep;
7926 tree base
7927 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7928 &offset, &mode, &unsignedp, &reversep,
7929 &volatilep);
7930 /* If the reference was to a (constant) zero offset, we can use
7931 the address of the base if it has the same base type
7932 as the result type and the pointer type is unqualified. */
7933 if (!offset
7934 && known_eq (bitpos, 0)
7935 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7936 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7937 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7938 return fold_convert_loc (loc, type,
7939 build_fold_addr_expr_loc (loc, base));
7942 if (TREE_CODE (op0) == MODIFY_EXPR
7943 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7944 /* Detect assigning a bitfield. */
7945 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7946 && DECL_BIT_FIELD
7947 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7949 /* Don't leave an assignment inside a conversion
7950 unless assigning a bitfield. */
7951 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7952 /* First do the assignment, then return converted constant. */
7953 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7954 TREE_NO_WARNING (tem) = 1;
7955 TREE_USED (tem) = 1;
7956 return tem;
7959 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7960 constants (if x has signed type, the sign bit cannot be set
7961 in c). This folds extension into the BIT_AND_EXPR.
7962 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7963 very likely don't have maximal range for their precision and this
7964 transformation effectively doesn't preserve non-maximal ranges. */
7965 if (TREE_CODE (type) == INTEGER_TYPE
7966 && TREE_CODE (op0) == BIT_AND_EXPR
7967 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7969 tree and_expr = op0;
7970 tree and0 = TREE_OPERAND (and_expr, 0);
7971 tree and1 = TREE_OPERAND (and_expr, 1);
7972 int change = 0;
7974 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7975 || (TYPE_PRECISION (type)
7976 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7977 change = 1;
7978 else if (TYPE_PRECISION (TREE_TYPE (and1))
7979 <= HOST_BITS_PER_WIDE_INT
7980 && tree_fits_uhwi_p (and1))
7982 unsigned HOST_WIDE_INT cst;
7984 cst = tree_to_uhwi (and1);
7985 cst &= HOST_WIDE_INT_M1U
7986 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7987 change = (cst == 0);
7988 if (change
7989 && !flag_syntax_only
7990 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7991 == ZERO_EXTEND))
7993 tree uns = unsigned_type_for (TREE_TYPE (and0));
7994 and0 = fold_convert_loc (loc, uns, and0);
7995 and1 = fold_convert_loc (loc, uns, and1);
7998 if (change)
8000 tem = force_fit_type (type, wi::to_widest (and1), 0,
8001 TREE_OVERFLOW (and1));
8002 return fold_build2_loc (loc, BIT_AND_EXPR, type,
8003 fold_convert_loc (loc, type, and0), tem);
8007 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
8008 cast (T1)X will fold away. We assume that this happens when X itself
8009 is a cast. */
8010 if (POINTER_TYPE_P (type)
8011 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8012 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
8014 tree arg00 = TREE_OPERAND (arg0, 0);
8015 tree arg01 = TREE_OPERAND (arg0, 1);
8017 return fold_build_pointer_plus_loc
8018 (loc, fold_convert_loc (loc, type, arg00), arg01);
8021 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8022 of the same precision, and X is an integer type not narrower than
8023 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8024 if (INTEGRAL_TYPE_P (type)
8025 && TREE_CODE (op0) == BIT_NOT_EXPR
8026 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8027 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8028 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8030 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8031 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8032 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8033 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
8034 fold_convert_loc (loc, type, tem));
8037 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8038 type of X and Y (integer types only). */
8039 if (INTEGRAL_TYPE_P (type)
8040 && TREE_CODE (op0) == MULT_EXPR
8041 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8042 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8044 /* Be careful not to introduce new overflows. */
8045 tree mult_type;
8046 if (TYPE_OVERFLOW_WRAPS (type))
8047 mult_type = type;
8048 else
8049 mult_type = unsigned_type_for (type);
8051 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8053 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8054 fold_convert_loc (loc, mult_type,
8055 TREE_OPERAND (op0, 0)),
8056 fold_convert_loc (loc, mult_type,
8057 TREE_OPERAND (op0, 1)));
8058 return fold_convert_loc (loc, type, tem);
8062 return NULL_TREE;
8064 case VIEW_CONVERT_EXPR:
8065 if (TREE_CODE (op0) == MEM_REF)
8067 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
8068 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
8069 tem = fold_build2_loc (loc, MEM_REF, type,
8070 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8071 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
8072 return tem;
8075 return NULL_TREE;
8077 case NEGATE_EXPR:
8078 tem = fold_negate_expr (loc, arg0);
8079 if (tem)
8080 return fold_convert_loc (loc, type, tem);
8081 return NULL_TREE;
8083 case ABS_EXPR:
8084 /* Convert fabs((double)float) into (double)fabsf(float). */
8085 if (TREE_CODE (arg0) == NOP_EXPR
8086 && TREE_CODE (type) == REAL_TYPE)
8088 tree targ0 = strip_float_extensions (arg0);
8089 if (targ0 != arg0)
8090 return fold_convert_loc (loc, type,
8091 fold_build1_loc (loc, ABS_EXPR,
8092 TREE_TYPE (targ0),
8093 targ0));
8095 return NULL_TREE;
8097 case BIT_NOT_EXPR:
8098 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8099 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8100 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8101 fold_convert_loc (loc, type,
8102 TREE_OPERAND (arg0, 0)))))
8103 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8104 fold_convert_loc (loc, type,
8105 TREE_OPERAND (arg0, 1)));
8106 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8107 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8108 fold_convert_loc (loc, type,
8109 TREE_OPERAND (arg0, 1)))))
8110 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8111 fold_convert_loc (loc, type,
8112 TREE_OPERAND (arg0, 0)), tem);
8114 return NULL_TREE;
8116 case TRUTH_NOT_EXPR:
8117 /* Note that the operand of this must be an int
8118 and its values must be 0 or 1.
8119 ("true" is a fixed value perhaps depending on the language,
8120 but we don't handle values other than 1 correctly yet.) */
8121 tem = fold_truth_not_expr (loc, arg0);
8122 if (!tem)
8123 return NULL_TREE;
8124 return fold_convert_loc (loc, type, tem);
8126 case INDIRECT_REF:
8127 /* Fold *&X to X if X is an lvalue. */
8128 if (TREE_CODE (op0) == ADDR_EXPR)
8130 tree op00 = TREE_OPERAND (op0, 0);
8131 if ((VAR_P (op00)
8132 || TREE_CODE (op00) == PARM_DECL
8133 || TREE_CODE (op00) == RESULT_DECL)
8134 && !TREE_READONLY (op00))
8135 return op00;
8137 return NULL_TREE;
8139 default:
8140 return NULL_TREE;
8141 } /* switch (code) */
8145 /* If the operation was a conversion do _not_ mark a resulting constant
8146 with TREE_OVERFLOW if the original constant was not. These conversions
8147 have implementation defined behavior and retaining the TREE_OVERFLOW
8148 flag here would confuse later passes such as VRP. */
8149 tree
8150 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8151 tree type, tree op0)
8153 tree res = fold_unary_loc (loc, code, type, op0);
8154 if (res
8155 && TREE_CODE (res) == INTEGER_CST
8156 && TREE_CODE (op0) == INTEGER_CST
8157 && CONVERT_EXPR_CODE_P (code))
8158 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8160 return res;
8163 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8164 operands OP0 and OP1. LOC is the location of the resulting expression.
8165 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8166 Return the folded expression if folding is successful. Otherwise,
8167 return NULL_TREE. */
8168 static tree
8169 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8170 tree arg0, tree arg1, tree op0, tree op1)
8172 tree tem;
8174 /* We only do these simplifications if we are optimizing. */
8175 if (!optimize)
8176 return NULL_TREE;
8178 /* Check for things like (A || B) && (A || C). We can convert this
8179 to A || (B && C). Note that either operator can be any of the four
8180 truth and/or operations and the transformation will still be
8181 valid. Also note that we only care about order for the
8182 ANDIF and ORIF operators. If B contains side effects, this
8183 might change the truth-value of A. */
8184 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8185 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8186 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8187 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8188 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8189 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8191 tree a00 = TREE_OPERAND (arg0, 0);
8192 tree a01 = TREE_OPERAND (arg0, 1);
8193 tree a10 = TREE_OPERAND (arg1, 0);
8194 tree a11 = TREE_OPERAND (arg1, 1);
8195 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8196 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8197 && (code == TRUTH_AND_EXPR
8198 || code == TRUTH_OR_EXPR));
8200 if (operand_equal_p (a00, a10, 0))
8201 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8202 fold_build2_loc (loc, code, type, a01, a11));
8203 else if (commutative && operand_equal_p (a00, a11, 0))
8204 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8205 fold_build2_loc (loc, code, type, a01, a10));
8206 else if (commutative && operand_equal_p (a01, a10, 0))
8207 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8208 fold_build2_loc (loc, code, type, a00, a11));
8210 /* This case if tricky because we must either have commutative
8211 operators or else A10 must not have side-effects. */
8213 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8214 && operand_equal_p (a01, a11, 0))
8215 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8216 fold_build2_loc (loc, code, type, a00, a10),
8217 a01);
8220 /* See if we can build a range comparison. */
8221 if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
8222 return tem;
8224 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8225 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8227 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8228 if (tem)
8229 return fold_build2_loc (loc, code, type, tem, arg1);
8232 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8233 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8235 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8236 if (tem)
8237 return fold_build2_loc (loc, code, type, arg0, tem);
8240 /* Check for the possibility of merging component references. If our
8241 lhs is another similar operation, try to merge its rhs with our
8242 rhs. Then try to merge our lhs and rhs. */
8243 if (TREE_CODE (arg0) == code
8244 && (tem = fold_truth_andor_1 (loc, code, type,
8245 TREE_OPERAND (arg0, 1), arg1)) != 0)
8246 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8248 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8249 return tem;
8251 bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
8252 if (PARAM_VALUE (PARAM_LOGICAL_OP_NON_SHORT_CIRCUIT) != -1)
8253 logical_op_non_short_circuit
8254 = PARAM_VALUE (PARAM_LOGICAL_OP_NON_SHORT_CIRCUIT);
8255 if (logical_op_non_short_circuit
8256 && !flag_sanitize_coverage
8257 && (code == TRUTH_AND_EXPR
8258 || code == TRUTH_ANDIF_EXPR
8259 || code == TRUTH_OR_EXPR
8260 || code == TRUTH_ORIF_EXPR))
8262 enum tree_code ncode, icode;
8264 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8265 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8266 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8268 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8269 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8270 We don't want to pack more than two leafs to a non-IF AND/OR
8271 expression.
8272 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8273 equal to IF-CODE, then we don't want to add right-hand operand.
8274 If the inner right-hand side of left-hand operand has
8275 side-effects, or isn't simple, then we can't add to it,
8276 as otherwise we might destroy if-sequence. */
8277 if (TREE_CODE (arg0) == icode
8278 && simple_operand_p_2 (arg1)
8279 /* Needed for sequence points to handle trappings, and
8280 side-effects. */
8281 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8283 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8284 arg1);
8285 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8286 tem);
8288 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8289 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8290 else if (TREE_CODE (arg1) == icode
8291 && simple_operand_p_2 (arg0)
8292 /* Needed for sequence points to handle trappings, and
8293 side-effects. */
8294 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8296 tem = fold_build2_loc (loc, ncode, type,
8297 arg0, TREE_OPERAND (arg1, 0));
8298 return fold_build2_loc (loc, icode, type, tem,
8299 TREE_OPERAND (arg1, 1));
8301 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8302 into (A OR B).
8303 For sequence point consistancy, we need to check for trapping,
8304 and side-effects. */
8305 else if (code == icode && simple_operand_p_2 (arg0)
8306 && simple_operand_p_2 (arg1))
8307 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8310 return NULL_TREE;
8313 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8314 by changing CODE to reduce the magnitude of constants involved in
8315 ARG0 of the comparison.
8316 Returns a canonicalized comparison tree if a simplification was
8317 possible, otherwise returns NULL_TREE.
8318 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8319 valid if signed overflow is undefined. */
8321 static tree
8322 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8323 tree arg0, tree arg1,
8324 bool *strict_overflow_p)
8326 enum tree_code code0 = TREE_CODE (arg0);
8327 tree t, cst0 = NULL_TREE;
8328 int sgn0;
8330 /* Match A +- CST code arg1. We can change this only if overflow
8331 is undefined. */
8332 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8333 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8334 /* In principle pointers also have undefined overflow behavior,
8335 but that causes problems elsewhere. */
8336 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8337 && (code0 == MINUS_EXPR
8338 || code0 == PLUS_EXPR)
8339 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8340 return NULL_TREE;
8342 /* Identify the constant in arg0 and its sign. */
8343 cst0 = TREE_OPERAND (arg0, 1);
8344 sgn0 = tree_int_cst_sgn (cst0);
8346 /* Overflowed constants and zero will cause problems. */
8347 if (integer_zerop (cst0)
8348 || TREE_OVERFLOW (cst0))
8349 return NULL_TREE;
8351 /* See if we can reduce the magnitude of the constant in
8352 arg0 by changing the comparison code. */
8353 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8354 if (code == LT_EXPR
8355 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8356 code = LE_EXPR;
8357 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8358 else if (code == GT_EXPR
8359 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8360 code = GE_EXPR;
8361 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8362 else if (code == LE_EXPR
8363 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8364 code = LT_EXPR;
8365 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8366 else if (code == GE_EXPR
8367 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8368 code = GT_EXPR;
8369 else
8370 return NULL_TREE;
8371 *strict_overflow_p = true;
8373 /* Now build the constant reduced in magnitude. But not if that
8374 would produce one outside of its types range. */
8375 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8376 && ((sgn0 == 1
8377 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8378 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8379 || (sgn0 == -1
8380 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8381 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8382 return NULL_TREE;
8384 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8385 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8386 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8387 t = fold_convert (TREE_TYPE (arg1), t);
8389 return fold_build2_loc (loc, code, type, t, arg1);
8392 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8393 overflow further. Try to decrease the magnitude of constants involved
8394 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8395 and put sole constants at the second argument position.
8396 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8398 static tree
8399 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8400 tree arg0, tree arg1)
8402 tree t;
8403 bool strict_overflow_p;
8404 const char * const warnmsg = G_("assuming signed overflow does not occur "
8405 "when reducing constant in comparison");
8407 /* Try canonicalization by simplifying arg0. */
8408 strict_overflow_p = false;
8409 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8410 &strict_overflow_p);
8411 if (t)
8413 if (strict_overflow_p)
8414 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8415 return t;
8418 /* Try canonicalization by simplifying arg1 using the swapped
8419 comparison. */
8420 code = swap_tree_comparison (code);
8421 strict_overflow_p = false;
8422 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8423 &strict_overflow_p);
8424 if (t && strict_overflow_p)
8425 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8426 return t;
8429 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8430 space. This is used to avoid issuing overflow warnings for
8431 expressions like &p->x which cannot wrap. */
8433 static bool
8434 pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
8436 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8437 return true;
8439 if (maybe_lt (bitpos, 0))
8440 return true;
8442 poly_wide_int wi_offset;
8443 int precision = TYPE_PRECISION (TREE_TYPE (base));
8444 if (offset == NULL_TREE)
8445 wi_offset = wi::zero (precision);
8446 else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
8447 return true;
8448 else
8449 wi_offset = wi::to_poly_wide (offset);
8451 wi::overflow_type overflow;
8452 poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
8453 precision);
8454 poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8455 if (overflow)
8456 return true;
8458 poly_uint64 total_hwi, size;
8459 if (!total.to_uhwi (&total_hwi)
8460 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
8461 &size)
8462 || known_eq (size, 0U))
8463 return true;
8465 if (known_le (total_hwi, size))
8466 return false;
8468 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8469 array. */
8470 if (TREE_CODE (base) == ADDR_EXPR
8471 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
8472 &size)
8473 && maybe_ne (size, 0U)
8474 && known_le (total_hwi, size))
8475 return false;
8477 return true;
8480 /* Return a positive integer when the symbol DECL is known to have
8481 a nonzero address, zero when it's known not to (e.g., it's a weak
8482 symbol), and a negative integer when the symbol is not yet in the
8483 symbol table and so whether or not its address is zero is unknown.
8484 For function local objects always return positive integer. */
8485 static int
8486 maybe_nonzero_address (tree decl)
8488 if (DECL_P (decl) && decl_in_symtab_p (decl))
8489 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8490 return symbol->nonzero_address ();
8492 /* Function local objects are never NULL. */
8493 if (DECL_P (decl)
8494 && (DECL_CONTEXT (decl)
8495 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8496 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8497 return 1;
8499 return -1;
8502 /* Subroutine of fold_binary. This routine performs all of the
8503 transformations that are common to the equality/inequality
8504 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8505 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8506 fold_binary should call fold_binary. Fold a comparison with
8507 tree code CODE and type TYPE with operands OP0 and OP1. Return
8508 the folded comparison or NULL_TREE. */
8510 static tree
8511 fold_comparison (location_t loc, enum tree_code code, tree type,
8512 tree op0, tree op1)
8514 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8515 tree arg0, arg1, tem;
8517 arg0 = op0;
8518 arg1 = op1;
8520 STRIP_SIGN_NOPS (arg0);
8521 STRIP_SIGN_NOPS (arg1);
8523 /* For comparisons of pointers we can decompose it to a compile time
8524 comparison of the base objects and the offsets into the object.
8525 This requires at least one operand being an ADDR_EXPR or a
8526 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8527 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8528 && (TREE_CODE (arg0) == ADDR_EXPR
8529 || TREE_CODE (arg1) == ADDR_EXPR
8530 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8531 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8533 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8534 poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
8535 machine_mode mode;
8536 int volatilep, reversep, unsignedp;
8537 bool indirect_base0 = false, indirect_base1 = false;
8539 /* Get base and offset for the access. Strip ADDR_EXPR for
8540 get_inner_reference, but put it back by stripping INDIRECT_REF
8541 off the base object if possible. indirect_baseN will be true
8542 if baseN is not an address but refers to the object itself. */
8543 base0 = arg0;
8544 if (TREE_CODE (arg0) == ADDR_EXPR)
8546 base0
8547 = get_inner_reference (TREE_OPERAND (arg0, 0),
8548 &bitsize, &bitpos0, &offset0, &mode,
8549 &unsignedp, &reversep, &volatilep);
8550 if (TREE_CODE (base0) == INDIRECT_REF)
8551 base0 = TREE_OPERAND (base0, 0);
8552 else
8553 indirect_base0 = true;
8555 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8557 base0 = TREE_OPERAND (arg0, 0);
8558 STRIP_SIGN_NOPS (base0);
8559 if (TREE_CODE (base0) == ADDR_EXPR)
8561 base0
8562 = get_inner_reference (TREE_OPERAND (base0, 0),
8563 &bitsize, &bitpos0, &offset0, &mode,
8564 &unsignedp, &reversep, &volatilep);
8565 if (TREE_CODE (base0) == INDIRECT_REF)
8566 base0 = TREE_OPERAND (base0, 0);
8567 else
8568 indirect_base0 = true;
8570 if (offset0 == NULL_TREE || integer_zerop (offset0))
8571 offset0 = TREE_OPERAND (arg0, 1);
8572 else
8573 offset0 = size_binop (PLUS_EXPR, offset0,
8574 TREE_OPERAND (arg0, 1));
8575 if (poly_int_tree_p (offset0))
8577 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
8578 TYPE_PRECISION (sizetype));
8579 tem <<= LOG2_BITS_PER_UNIT;
8580 tem += bitpos0;
8581 if (tem.to_shwi (&bitpos0))
8582 offset0 = NULL_TREE;
8586 base1 = arg1;
8587 if (TREE_CODE (arg1) == ADDR_EXPR)
8589 base1
8590 = get_inner_reference (TREE_OPERAND (arg1, 0),
8591 &bitsize, &bitpos1, &offset1, &mode,
8592 &unsignedp, &reversep, &volatilep);
8593 if (TREE_CODE (base1) == INDIRECT_REF)
8594 base1 = TREE_OPERAND (base1, 0);
8595 else
8596 indirect_base1 = true;
8598 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8600 base1 = TREE_OPERAND (arg1, 0);
8601 STRIP_SIGN_NOPS (base1);
8602 if (TREE_CODE (base1) == ADDR_EXPR)
8604 base1
8605 = get_inner_reference (TREE_OPERAND (base1, 0),
8606 &bitsize, &bitpos1, &offset1, &mode,
8607 &unsignedp, &reversep, &volatilep);
8608 if (TREE_CODE (base1) == INDIRECT_REF)
8609 base1 = TREE_OPERAND (base1, 0);
8610 else
8611 indirect_base1 = true;
8613 if (offset1 == NULL_TREE || integer_zerop (offset1))
8614 offset1 = TREE_OPERAND (arg1, 1);
8615 else
8616 offset1 = size_binop (PLUS_EXPR, offset1,
8617 TREE_OPERAND (arg1, 1));
8618 if (poly_int_tree_p (offset1))
8620 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
8621 TYPE_PRECISION (sizetype));
8622 tem <<= LOG2_BITS_PER_UNIT;
8623 tem += bitpos1;
8624 if (tem.to_shwi (&bitpos1))
8625 offset1 = NULL_TREE;
8629 /* If we have equivalent bases we might be able to simplify. */
8630 if (indirect_base0 == indirect_base1
8631 && operand_equal_p (base0, base1,
8632 indirect_base0 ? OEP_ADDRESS_OF : 0))
8634 /* We can fold this expression to a constant if the non-constant
8635 offset parts are equal. */
8636 if ((offset0 == offset1
8637 || (offset0 && offset1
8638 && operand_equal_p (offset0, offset1, 0)))
8639 && (equality_code
8640 || (indirect_base0
8641 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8642 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8644 if (!equality_code
8645 && maybe_ne (bitpos0, bitpos1)
8646 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8647 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8648 fold_overflow_warning (("assuming pointer wraparound does not "
8649 "occur when comparing P +- C1 with "
8650 "P +- C2"),
8651 WARN_STRICT_OVERFLOW_CONDITIONAL);
8653 switch (code)
8655 case EQ_EXPR:
8656 if (known_eq (bitpos0, bitpos1))
8657 return constant_boolean_node (true, type);
8658 if (known_ne (bitpos0, bitpos1))
8659 return constant_boolean_node (false, type);
8660 break;
8661 case NE_EXPR:
8662 if (known_ne (bitpos0, bitpos1))
8663 return constant_boolean_node (true, type);
8664 if (known_eq (bitpos0, bitpos1))
8665 return constant_boolean_node (false, type);
8666 break;
8667 case LT_EXPR:
8668 if (known_lt (bitpos0, bitpos1))
8669 return constant_boolean_node (true, type);
8670 if (known_ge (bitpos0, bitpos1))
8671 return constant_boolean_node (false, type);
8672 break;
8673 case LE_EXPR:
8674 if (known_le (bitpos0, bitpos1))
8675 return constant_boolean_node (true, type);
8676 if (known_gt (bitpos0, bitpos1))
8677 return constant_boolean_node (false, type);
8678 break;
8679 case GE_EXPR:
8680 if (known_ge (bitpos0, bitpos1))
8681 return constant_boolean_node (true, type);
8682 if (known_lt (bitpos0, bitpos1))
8683 return constant_boolean_node (false, type);
8684 break;
8685 case GT_EXPR:
8686 if (known_gt (bitpos0, bitpos1))
8687 return constant_boolean_node (true, type);
8688 if (known_le (bitpos0, bitpos1))
8689 return constant_boolean_node (false, type);
8690 break;
8691 default:;
8694 /* We can simplify the comparison to a comparison of the variable
8695 offset parts if the constant offset parts are equal.
8696 Be careful to use signed sizetype here because otherwise we
8697 mess with array offsets in the wrong way. This is possible
8698 because pointer arithmetic is restricted to retain within an
8699 object and overflow on pointer differences is undefined as of
8700 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8701 else if (known_eq (bitpos0, bitpos1)
8702 && (equality_code
8703 || (indirect_base0
8704 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8705 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8707 /* By converting to signed sizetype we cover middle-end pointer
8708 arithmetic which operates on unsigned pointer types of size
8709 type size and ARRAY_REF offsets which are properly sign or
8710 zero extended from their type in case it is narrower than
8711 sizetype. */
8712 if (offset0 == NULL_TREE)
8713 offset0 = build_int_cst (ssizetype, 0);
8714 else
8715 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8716 if (offset1 == NULL_TREE)
8717 offset1 = build_int_cst (ssizetype, 0);
8718 else
8719 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8721 if (!equality_code
8722 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8723 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8724 fold_overflow_warning (("assuming pointer wraparound does not "
8725 "occur when comparing P +- C1 with "
8726 "P +- C2"),
8727 WARN_STRICT_OVERFLOW_COMPARISON);
8729 return fold_build2_loc (loc, code, type, offset0, offset1);
8732 /* For equal offsets we can simplify to a comparison of the
8733 base addresses. */
8734 else if (known_eq (bitpos0, bitpos1)
8735 && (indirect_base0
8736 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8737 && (indirect_base1
8738 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8739 && ((offset0 == offset1)
8740 || (offset0 && offset1
8741 && operand_equal_p (offset0, offset1, 0))))
8743 if (indirect_base0)
8744 base0 = build_fold_addr_expr_loc (loc, base0);
8745 if (indirect_base1)
8746 base1 = build_fold_addr_expr_loc (loc, base1);
8747 return fold_build2_loc (loc, code, type, base0, base1);
8749 /* Comparison between an ordinary (non-weak) symbol and a null
8750 pointer can be eliminated since such symbols must have a non
8751 null address. In C, relational expressions between pointers
8752 to objects and null pointers are undefined. The results
8753 below follow the C++ rules with the additional property that
8754 every object pointer compares greater than a null pointer.
8756 else if (((DECL_P (base0)
8757 && maybe_nonzero_address (base0) > 0
8758 /* Avoid folding references to struct members at offset 0 to
8759 prevent tests like '&ptr->firstmember == 0' from getting
8760 eliminated. When ptr is null, although the -> expression
8761 is strictly speaking invalid, GCC retains it as a matter
8762 of QoI. See PR c/44555. */
8763 && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
8764 || CONSTANT_CLASS_P (base0))
8765 && indirect_base0
8766 /* The caller guarantees that when one of the arguments is
8767 constant (i.e., null in this case) it is second. */
8768 && integer_zerop (arg1))
8770 switch (code)
8772 case EQ_EXPR:
8773 case LE_EXPR:
8774 case LT_EXPR:
8775 return constant_boolean_node (false, type);
8776 case GE_EXPR:
8777 case GT_EXPR:
8778 case NE_EXPR:
8779 return constant_boolean_node (true, type);
8780 default:
8781 gcc_unreachable ();
8786 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8787 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8788 the resulting offset is smaller in absolute value than the
8789 original one and has the same sign. */
8790 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8791 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8792 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8793 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8794 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8795 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8796 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8797 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8799 tree const1 = TREE_OPERAND (arg0, 1);
8800 tree const2 = TREE_OPERAND (arg1, 1);
8801 tree variable1 = TREE_OPERAND (arg0, 0);
8802 tree variable2 = TREE_OPERAND (arg1, 0);
8803 tree cst;
8804 const char * const warnmsg = G_("assuming signed overflow does not "
8805 "occur when combining constants around "
8806 "a comparison");
8808 /* Put the constant on the side where it doesn't overflow and is
8809 of lower absolute value and of same sign than before. */
8810 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8811 ? MINUS_EXPR : PLUS_EXPR,
8812 const2, const1);
8813 if (!TREE_OVERFLOW (cst)
8814 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8815 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8817 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8818 return fold_build2_loc (loc, code, type,
8819 variable1,
8820 fold_build2_loc (loc, TREE_CODE (arg1),
8821 TREE_TYPE (arg1),
8822 variable2, cst));
8825 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8826 ? MINUS_EXPR : PLUS_EXPR,
8827 const1, const2);
8828 if (!TREE_OVERFLOW (cst)
8829 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8830 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8832 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8833 return fold_build2_loc (loc, code, type,
8834 fold_build2_loc (loc, TREE_CODE (arg0),
8835 TREE_TYPE (arg0),
8836 variable1, cst),
8837 variable2);
8841 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8842 if (tem)
8843 return tem;
8845 /* If we are comparing an expression that just has comparisons
8846 of two integer values, arithmetic expressions of those comparisons,
8847 and constants, we can simplify it. There are only three cases
8848 to check: the two values can either be equal, the first can be
8849 greater, or the second can be greater. Fold the expression for
8850 those three values. Since each value must be 0 or 1, we have
8851 eight possibilities, each of which corresponds to the constant 0
8852 or 1 or one of the six possible comparisons.
8854 This handles common cases like (a > b) == 0 but also handles
8855 expressions like ((x > y) - (y > x)) > 0, which supposedly
8856 occur in macroized code. */
8858 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8860 tree cval1 = 0, cval2 = 0;
8862 if (twoval_comparison_p (arg0, &cval1, &cval2)
8863 /* Don't handle degenerate cases here; they should already
8864 have been handled anyway. */
8865 && cval1 != 0 && cval2 != 0
8866 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8867 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8868 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8869 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8870 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8871 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8872 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8874 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8875 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8877 /* We can't just pass T to eval_subst in case cval1 or cval2
8878 was the same as ARG1. */
8880 tree high_result
8881 = fold_build2_loc (loc, code, type,
8882 eval_subst (loc, arg0, cval1, maxval,
8883 cval2, minval),
8884 arg1);
8885 tree equal_result
8886 = fold_build2_loc (loc, code, type,
8887 eval_subst (loc, arg0, cval1, maxval,
8888 cval2, maxval),
8889 arg1);
8890 tree low_result
8891 = fold_build2_loc (loc, code, type,
8892 eval_subst (loc, arg0, cval1, minval,
8893 cval2, maxval),
8894 arg1);
8896 /* All three of these results should be 0 or 1. Confirm they are.
8897 Then use those values to select the proper code to use. */
8899 if (TREE_CODE (high_result) == INTEGER_CST
8900 && TREE_CODE (equal_result) == INTEGER_CST
8901 && TREE_CODE (low_result) == INTEGER_CST)
8903 /* Make a 3-bit mask with the high-order bit being the
8904 value for `>', the next for '=', and the low for '<'. */
8905 switch ((integer_onep (high_result) * 4)
8906 + (integer_onep (equal_result) * 2)
8907 + integer_onep (low_result))
8909 case 0:
8910 /* Always false. */
8911 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8912 case 1:
8913 code = LT_EXPR;
8914 break;
8915 case 2:
8916 code = EQ_EXPR;
8917 break;
8918 case 3:
8919 code = LE_EXPR;
8920 break;
8921 case 4:
8922 code = GT_EXPR;
8923 break;
8924 case 5:
8925 code = NE_EXPR;
8926 break;
8927 case 6:
8928 code = GE_EXPR;
8929 break;
8930 case 7:
8931 /* Always true. */
8932 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8935 return fold_build2_loc (loc, code, type, cval1, cval2);
8940 return NULL_TREE;
8944 /* Subroutine of fold_binary. Optimize complex multiplications of the
8945 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8946 argument EXPR represents the expression "z" of type TYPE. */
8948 static tree
8949 fold_mult_zconjz (location_t loc, tree type, tree expr)
8951 tree itype = TREE_TYPE (type);
8952 tree rpart, ipart, tem;
8954 if (TREE_CODE (expr) == COMPLEX_EXPR)
8956 rpart = TREE_OPERAND (expr, 0);
8957 ipart = TREE_OPERAND (expr, 1);
8959 else if (TREE_CODE (expr) == COMPLEX_CST)
8961 rpart = TREE_REALPART (expr);
8962 ipart = TREE_IMAGPART (expr);
8964 else
8966 expr = save_expr (expr);
8967 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8968 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8971 rpart = save_expr (rpart);
8972 ipart = save_expr (ipart);
8973 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8974 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8975 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8976 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8977 build_zero_cst (itype));
8981 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8982 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8983 true if successful. */
8985 static bool
8986 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
8988 unsigned HOST_WIDE_INT i, nunits;
8990 if (TREE_CODE (arg) == VECTOR_CST
8991 && VECTOR_CST_NELTS (arg).is_constant (&nunits))
8993 for (i = 0; i < nunits; ++i)
8994 elts[i] = VECTOR_CST_ELT (arg, i);
8996 else if (TREE_CODE (arg) == CONSTRUCTOR)
8998 constructor_elt *elt;
9000 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
9001 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
9002 return false;
9003 else
9004 elts[i] = elt->value;
9006 else
9007 return false;
9008 for (; i < nelts; i++)
9009 elts[i]
9010 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
9011 return true;
9014 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
9015 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
9016 NULL_TREE otherwise. */
9018 static tree
9019 fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
9021 unsigned int i;
9022 unsigned HOST_WIDE_INT nelts;
9023 bool need_ctor = false;
9025 if (!sel.length ().is_constant (&nelts))
9026 return NULL_TREE;
9027 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), nelts)
9028 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)), nelts)
9029 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)), nelts));
9030 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
9031 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
9032 return NULL_TREE;
9034 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
9035 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
9036 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
9037 return NULL_TREE;
9039 tree_vector_builder out_elts (type, nelts, 1);
9040 for (i = 0; i < nelts; i++)
9042 HOST_WIDE_INT index;
9043 if (!sel[i].is_constant (&index))
9044 return NULL_TREE;
9045 if (!CONSTANT_CLASS_P (in_elts[index]))
9046 need_ctor = true;
9047 out_elts.quick_push (unshare_expr (in_elts[index]));
9050 if (need_ctor)
9052 vec<constructor_elt, va_gc> *v;
9053 vec_alloc (v, nelts);
9054 for (i = 0; i < nelts; i++)
9055 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
9056 return build_constructor (type, v);
9058 else
9059 return out_elts.build ();
9062 /* Try to fold a pointer difference of type TYPE two address expressions of
9063 array references AREF0 and AREF1 using location LOC. Return a
9064 simplified expression for the difference or NULL_TREE. */
9066 static tree
9067 fold_addr_of_array_ref_difference (location_t loc, tree type,
9068 tree aref0, tree aref1,
9069 bool use_pointer_diff)
9071 tree base0 = TREE_OPERAND (aref0, 0);
9072 tree base1 = TREE_OPERAND (aref1, 0);
9073 tree base_offset = build_int_cst (type, 0);
9075 /* If the bases are array references as well, recurse. If the bases
9076 are pointer indirections compute the difference of the pointers.
9077 If the bases are equal, we are set. */
9078 if ((TREE_CODE (base0) == ARRAY_REF
9079 && TREE_CODE (base1) == ARRAY_REF
9080 && (base_offset
9081 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
9082 use_pointer_diff)))
9083 || (INDIRECT_REF_P (base0)
9084 && INDIRECT_REF_P (base1)
9085 && (base_offset
9086 = use_pointer_diff
9087 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
9088 TREE_OPERAND (base0, 0),
9089 TREE_OPERAND (base1, 0))
9090 : fold_binary_loc (loc, MINUS_EXPR, type,
9091 fold_convert (type,
9092 TREE_OPERAND (base0, 0)),
9093 fold_convert (type,
9094 TREE_OPERAND (base1, 0)))))
9095 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
9097 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9098 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9099 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9100 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
9101 return fold_build2_loc (loc, PLUS_EXPR, type,
9102 base_offset,
9103 fold_build2_loc (loc, MULT_EXPR, type,
9104 diff, esz));
9106 return NULL_TREE;
9109 /* If the real or vector real constant CST of type TYPE has an exact
9110 inverse, return it, else return NULL. */
9112 tree
9113 exact_inverse (tree type, tree cst)
9115 REAL_VALUE_TYPE r;
9116 tree unit_type;
9117 machine_mode mode;
9119 switch (TREE_CODE (cst))
9121 case REAL_CST:
9122 r = TREE_REAL_CST (cst);
9124 if (exact_real_inverse (TYPE_MODE (type), &r))
9125 return build_real (type, r);
9127 return NULL_TREE;
9129 case VECTOR_CST:
9131 unit_type = TREE_TYPE (type);
9132 mode = TYPE_MODE (unit_type);
9134 tree_vector_builder elts;
9135 if (!elts.new_unary_operation (type, cst, false))
9136 return NULL_TREE;
9137 unsigned int count = elts.encoded_nelts ();
9138 for (unsigned int i = 0; i < count; ++i)
9140 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9141 if (!exact_real_inverse (mode, &r))
9142 return NULL_TREE;
9143 elts.quick_push (build_real (unit_type, r));
9146 return elts.build ();
9149 default:
9150 return NULL_TREE;
9154 /* Mask out the tz least significant bits of X of type TYPE where
9155 tz is the number of trailing zeroes in Y. */
9156 static wide_int
9157 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9159 int tz = wi::ctz (y);
9160 if (tz > 0)
9161 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9162 return x;
9165 /* Return true when T is an address and is known to be nonzero.
9166 For floating point we further ensure that T is not denormal.
9167 Similar logic is present in nonzero_address in rtlanal.h.
9169 If the return value is based on the assumption that signed overflow
9170 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9171 change *STRICT_OVERFLOW_P. */
9173 static bool
9174 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9176 tree type = TREE_TYPE (t);
9177 enum tree_code code;
9179 /* Doing something useful for floating point would need more work. */
9180 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9181 return false;
9183 code = TREE_CODE (t);
9184 switch (TREE_CODE_CLASS (code))
9186 case tcc_unary:
9187 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9188 strict_overflow_p);
9189 case tcc_binary:
9190 case tcc_comparison:
9191 return tree_binary_nonzero_warnv_p (code, type,
9192 TREE_OPERAND (t, 0),
9193 TREE_OPERAND (t, 1),
9194 strict_overflow_p);
9195 case tcc_constant:
9196 case tcc_declaration:
9197 case tcc_reference:
9198 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9200 default:
9201 break;
9204 switch (code)
9206 case TRUTH_NOT_EXPR:
9207 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9208 strict_overflow_p);
9210 case TRUTH_AND_EXPR:
9211 case TRUTH_OR_EXPR:
9212 case TRUTH_XOR_EXPR:
9213 return tree_binary_nonzero_warnv_p (code, type,
9214 TREE_OPERAND (t, 0),
9215 TREE_OPERAND (t, 1),
9216 strict_overflow_p);
9218 case COND_EXPR:
9219 case CONSTRUCTOR:
9220 case OBJ_TYPE_REF:
9221 case ASSERT_EXPR:
9222 case ADDR_EXPR:
9223 case WITH_SIZE_EXPR:
9224 case SSA_NAME:
9225 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9227 case COMPOUND_EXPR:
9228 case MODIFY_EXPR:
9229 case BIND_EXPR:
9230 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9231 strict_overflow_p);
9233 case SAVE_EXPR:
9234 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9235 strict_overflow_p);
9237 case CALL_EXPR:
9239 tree fndecl = get_callee_fndecl (t);
9240 if (!fndecl) return false;
9241 if (flag_delete_null_pointer_checks && !flag_check_new
9242 && DECL_IS_OPERATOR_NEW (fndecl)
9243 && !TREE_NOTHROW (fndecl))
9244 return true;
9245 if (flag_delete_null_pointer_checks
9246 && lookup_attribute ("returns_nonnull",
9247 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9248 return true;
9249 return alloca_call_p (t);
9252 default:
9253 break;
9255 return false;
9258 /* Return true when T is an address and is known to be nonzero.
9259 Handle warnings about undefined signed overflow. */
9261 bool
9262 tree_expr_nonzero_p (tree t)
9264 bool ret, strict_overflow_p;
9266 strict_overflow_p = false;
9267 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9268 if (strict_overflow_p)
9269 fold_overflow_warning (("assuming signed overflow does not occur when "
9270 "determining that expression is always "
9271 "non-zero"),
9272 WARN_STRICT_OVERFLOW_MISC);
9273 return ret;
9276 /* Return true if T is known not to be equal to an integer W. */
9278 bool
9279 expr_not_equal_to (tree t, const wide_int &w)
9281 wide_int min, max, nz;
9282 value_range_kind rtype;
9283 switch (TREE_CODE (t))
9285 case INTEGER_CST:
9286 return wi::to_wide (t) != w;
9288 case SSA_NAME:
9289 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9290 return false;
9291 rtype = get_range_info (t, &min, &max);
9292 if (rtype == VR_RANGE)
9294 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9295 return true;
9296 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9297 return true;
9299 else if (rtype == VR_ANTI_RANGE
9300 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9301 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9302 return true;
9303 /* If T has some known zero bits and W has any of those bits set,
9304 then T is known not to be equal to W. */
9305 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9306 TYPE_PRECISION (TREE_TYPE (t))), 0))
9307 return true;
9308 return false;
9310 default:
9311 return false;
9315 /* Fold a binary expression of code CODE and type TYPE with operands
9316 OP0 and OP1. LOC is the location of the resulting expression.
9317 Return the folded expression if folding is successful. Otherwise,
9318 return NULL_TREE. */
9320 tree
9321 fold_binary_loc (location_t loc, enum tree_code code, tree type,
9322 tree op0, tree op1)
9324 enum tree_code_class kind = TREE_CODE_CLASS (code);
9325 tree arg0, arg1, tem;
9326 tree t1 = NULL_TREE;
9327 bool strict_overflow_p;
9328 unsigned int prec;
9330 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9331 && TREE_CODE_LENGTH (code) == 2
9332 && op0 != NULL_TREE
9333 && op1 != NULL_TREE);
9335 arg0 = op0;
9336 arg1 = op1;
9338 /* Strip any conversions that don't change the mode. This is
9339 safe for every expression, except for a comparison expression
9340 because its signedness is derived from its operands. So, in
9341 the latter case, only strip conversions that don't change the
9342 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9343 preserved.
9345 Note that this is done as an internal manipulation within the
9346 constant folder, in order to find the simplest representation
9347 of the arguments so that their form can be studied. In any
9348 cases, the appropriate type conversions should be put back in
9349 the tree that will get out of the constant folder. */
9351 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9353 STRIP_SIGN_NOPS (arg0);
9354 STRIP_SIGN_NOPS (arg1);
9356 else
9358 STRIP_NOPS (arg0);
9359 STRIP_NOPS (arg1);
9362 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9363 constant but we can't do arithmetic on them. */
9364 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9366 tem = const_binop (code, type, arg0, arg1);
9367 if (tem != NULL_TREE)
9369 if (TREE_TYPE (tem) != type)
9370 tem = fold_convert_loc (loc, type, tem);
9371 return tem;
9375 /* If this is a commutative operation, and ARG0 is a constant, move it
9376 to ARG1 to reduce the number of tests below. */
9377 if (commutative_tree_code (code)
9378 && tree_swap_operands_p (arg0, arg1))
9379 return fold_build2_loc (loc, code, type, op1, op0);
9381 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9382 to ARG1 to reduce the number of tests below. */
9383 if (kind == tcc_comparison
9384 && tree_swap_operands_p (arg0, arg1))
9385 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9387 tem = generic_simplify (loc, code, type, op0, op1);
9388 if (tem)
9389 return tem;
9391 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9393 First check for cases where an arithmetic operation is applied to a
9394 compound, conditional, or comparison operation. Push the arithmetic
9395 operation inside the compound or conditional to see if any folding
9396 can then be done. Convert comparison to conditional for this purpose.
9397 The also optimizes non-constant cases that used to be done in
9398 expand_expr.
9400 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9401 one of the operands is a comparison and the other is a comparison, a
9402 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9403 code below would make the expression more complex. Change it to a
9404 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9405 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9407 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9408 || code == EQ_EXPR || code == NE_EXPR)
9409 && !VECTOR_TYPE_P (TREE_TYPE (arg0))
9410 && ((truth_value_p (TREE_CODE (arg0))
9411 && (truth_value_p (TREE_CODE (arg1))
9412 || (TREE_CODE (arg1) == BIT_AND_EXPR
9413 && integer_onep (TREE_OPERAND (arg1, 1)))))
9414 || (truth_value_p (TREE_CODE (arg1))
9415 && (truth_value_p (TREE_CODE (arg0))
9416 || (TREE_CODE (arg0) == BIT_AND_EXPR
9417 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9419 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9420 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9421 : TRUTH_XOR_EXPR,
9422 boolean_type_node,
9423 fold_convert_loc (loc, boolean_type_node, arg0),
9424 fold_convert_loc (loc, boolean_type_node, arg1));
9426 if (code == EQ_EXPR)
9427 tem = invert_truthvalue_loc (loc, tem);
9429 return fold_convert_loc (loc, type, tem);
9432 if (TREE_CODE_CLASS (code) == tcc_binary
9433 || TREE_CODE_CLASS (code) == tcc_comparison)
9435 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9437 tem = fold_build2_loc (loc, code, type,
9438 fold_convert_loc (loc, TREE_TYPE (op0),
9439 TREE_OPERAND (arg0, 1)), op1);
9440 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9441 tem);
9443 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9445 tem = fold_build2_loc (loc, code, type, op0,
9446 fold_convert_loc (loc, TREE_TYPE (op1),
9447 TREE_OPERAND (arg1, 1)));
9448 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9449 tem);
9452 if (TREE_CODE (arg0) == COND_EXPR
9453 || TREE_CODE (arg0) == VEC_COND_EXPR
9454 || COMPARISON_CLASS_P (arg0))
9456 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9457 arg0, arg1,
9458 /*cond_first_p=*/1);
9459 if (tem != NULL_TREE)
9460 return tem;
9463 if (TREE_CODE (arg1) == COND_EXPR
9464 || TREE_CODE (arg1) == VEC_COND_EXPR
9465 || COMPARISON_CLASS_P (arg1))
9467 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9468 arg1, arg0,
9469 /*cond_first_p=*/0);
9470 if (tem != NULL_TREE)
9471 return tem;
9475 switch (code)
9477 case MEM_REF:
9478 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9479 if (TREE_CODE (arg0) == ADDR_EXPR
9480 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9482 tree iref = TREE_OPERAND (arg0, 0);
9483 return fold_build2 (MEM_REF, type,
9484 TREE_OPERAND (iref, 0),
9485 int_const_binop (PLUS_EXPR, arg1,
9486 TREE_OPERAND (iref, 1)));
9489 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9490 if (TREE_CODE (arg0) == ADDR_EXPR
9491 && handled_component_p (TREE_OPERAND (arg0, 0)))
9493 tree base;
9494 poly_int64 coffset;
9495 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9496 &coffset);
9497 if (!base)
9498 return NULL_TREE;
9499 return fold_build2 (MEM_REF, type,
9500 build_fold_addr_expr (base),
9501 int_const_binop (PLUS_EXPR, arg1,
9502 size_int (coffset)));
9505 return NULL_TREE;
9507 case POINTER_PLUS_EXPR:
9508 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9509 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9510 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9511 return fold_convert_loc (loc, type,
9512 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9513 fold_convert_loc (loc, sizetype,
9514 arg1),
9515 fold_convert_loc (loc, sizetype,
9516 arg0)));
9518 return NULL_TREE;
9520 case PLUS_EXPR:
9521 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9523 /* X + (X / CST) * -CST is X % CST. */
9524 if (TREE_CODE (arg1) == MULT_EXPR
9525 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9526 && operand_equal_p (arg0,
9527 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9529 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9530 tree cst1 = TREE_OPERAND (arg1, 1);
9531 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9532 cst1, cst0);
9533 if (sum && integer_zerop (sum))
9534 return fold_convert_loc (loc, type,
9535 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9536 TREE_TYPE (arg0), arg0,
9537 cst0));
9541 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9542 one. Make sure the type is not saturating and has the signedness of
9543 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9544 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9545 if ((TREE_CODE (arg0) == MULT_EXPR
9546 || TREE_CODE (arg1) == MULT_EXPR)
9547 && !TYPE_SATURATING (type)
9548 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9549 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9550 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9552 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9553 if (tem)
9554 return tem;
9557 if (! FLOAT_TYPE_P (type))
9559 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9560 (plus (plus (mult) (mult)) (foo)) so that we can
9561 take advantage of the factoring cases below. */
9562 if (ANY_INTEGRAL_TYPE_P (type)
9563 && TYPE_OVERFLOW_WRAPS (type)
9564 && (((TREE_CODE (arg0) == PLUS_EXPR
9565 || TREE_CODE (arg0) == MINUS_EXPR)
9566 && TREE_CODE (arg1) == MULT_EXPR)
9567 || ((TREE_CODE (arg1) == PLUS_EXPR
9568 || TREE_CODE (arg1) == MINUS_EXPR)
9569 && TREE_CODE (arg0) == MULT_EXPR)))
9571 tree parg0, parg1, parg, marg;
9572 enum tree_code pcode;
9574 if (TREE_CODE (arg1) == MULT_EXPR)
9575 parg = arg0, marg = arg1;
9576 else
9577 parg = arg1, marg = arg0;
9578 pcode = TREE_CODE (parg);
9579 parg0 = TREE_OPERAND (parg, 0);
9580 parg1 = TREE_OPERAND (parg, 1);
9581 STRIP_NOPS (parg0);
9582 STRIP_NOPS (parg1);
9584 if (TREE_CODE (parg0) == MULT_EXPR
9585 && TREE_CODE (parg1) != MULT_EXPR)
9586 return fold_build2_loc (loc, pcode, type,
9587 fold_build2_loc (loc, PLUS_EXPR, type,
9588 fold_convert_loc (loc, type,
9589 parg0),
9590 fold_convert_loc (loc, type,
9591 marg)),
9592 fold_convert_loc (loc, type, parg1));
9593 if (TREE_CODE (parg0) != MULT_EXPR
9594 && TREE_CODE (parg1) == MULT_EXPR)
9595 return
9596 fold_build2_loc (loc, PLUS_EXPR, type,
9597 fold_convert_loc (loc, type, parg0),
9598 fold_build2_loc (loc, pcode, type,
9599 fold_convert_loc (loc, type, marg),
9600 fold_convert_loc (loc, type,
9601 parg1)));
9604 else
9606 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9607 to __complex__ ( x, y ). This is not the same for SNaNs or
9608 if signed zeros are involved. */
9609 if (!HONOR_SNANS (element_mode (arg0))
9610 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9611 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9613 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9614 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9615 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9616 bool arg0rz = false, arg0iz = false;
9617 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9618 || (arg0i && (arg0iz = real_zerop (arg0i))))
9620 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9621 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9622 if (arg0rz && arg1i && real_zerop (arg1i))
9624 tree rp = arg1r ? arg1r
9625 : build1 (REALPART_EXPR, rtype, arg1);
9626 tree ip = arg0i ? arg0i
9627 : build1 (IMAGPART_EXPR, rtype, arg0);
9628 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9630 else if (arg0iz && arg1r && real_zerop (arg1r))
9632 tree rp = arg0r ? arg0r
9633 : build1 (REALPART_EXPR, rtype, arg0);
9634 tree ip = arg1i ? arg1i
9635 : build1 (IMAGPART_EXPR, rtype, arg1);
9636 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9641 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9642 We associate floats only if the user has specified
9643 -fassociative-math. */
9644 if (flag_associative_math
9645 && TREE_CODE (arg1) == PLUS_EXPR
9646 && TREE_CODE (arg0) != MULT_EXPR)
9648 tree tree10 = TREE_OPERAND (arg1, 0);
9649 tree tree11 = TREE_OPERAND (arg1, 1);
9650 if (TREE_CODE (tree11) == MULT_EXPR
9651 && TREE_CODE (tree10) == MULT_EXPR)
9653 tree tree0;
9654 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9655 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9658 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9659 We associate floats only if the user has specified
9660 -fassociative-math. */
9661 if (flag_associative_math
9662 && TREE_CODE (arg0) == PLUS_EXPR
9663 && TREE_CODE (arg1) != MULT_EXPR)
9665 tree tree00 = TREE_OPERAND (arg0, 0);
9666 tree tree01 = TREE_OPERAND (arg0, 1);
9667 if (TREE_CODE (tree01) == MULT_EXPR
9668 && TREE_CODE (tree00) == MULT_EXPR)
9670 tree tree0;
9671 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9672 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9677 bit_rotate:
9678 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9679 is a rotate of A by C1 bits. */
9680 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9681 is a rotate of A by B bits.
9682 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
9683 though in this case CODE must be | and not + or ^, otherwise
9684 it doesn't return A when B is 0. */
9686 enum tree_code code0, code1;
9687 tree rtype;
9688 code0 = TREE_CODE (arg0);
9689 code1 = TREE_CODE (arg1);
9690 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9691 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9692 && operand_equal_p (TREE_OPERAND (arg0, 0),
9693 TREE_OPERAND (arg1, 0), 0)
9694 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9695 TYPE_UNSIGNED (rtype))
9696 /* Only create rotates in complete modes. Other cases are not
9697 expanded properly. */
9698 && (element_precision (rtype)
9699 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9701 tree tree01, tree11;
9702 tree orig_tree01, orig_tree11;
9703 enum tree_code code01, code11;
9705 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
9706 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
9707 STRIP_NOPS (tree01);
9708 STRIP_NOPS (tree11);
9709 code01 = TREE_CODE (tree01);
9710 code11 = TREE_CODE (tree11);
9711 if (code11 != MINUS_EXPR
9712 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
9714 std::swap (code0, code1);
9715 std::swap (code01, code11);
9716 std::swap (tree01, tree11);
9717 std::swap (orig_tree01, orig_tree11);
9719 if (code01 == INTEGER_CST
9720 && code11 == INTEGER_CST
9721 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9722 == element_precision (rtype)))
9724 tem = build2_loc (loc, LROTATE_EXPR,
9725 rtype, TREE_OPERAND (arg0, 0),
9726 code0 == LSHIFT_EXPR
9727 ? orig_tree01 : orig_tree11);
9728 return fold_convert_loc (loc, type, tem);
9730 else if (code11 == MINUS_EXPR)
9732 tree tree110, tree111;
9733 tree110 = TREE_OPERAND (tree11, 0);
9734 tree111 = TREE_OPERAND (tree11, 1);
9735 STRIP_NOPS (tree110);
9736 STRIP_NOPS (tree111);
9737 if (TREE_CODE (tree110) == INTEGER_CST
9738 && compare_tree_int (tree110,
9739 element_precision (rtype)) == 0
9740 && operand_equal_p (tree01, tree111, 0))
9742 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9743 ? LROTATE_EXPR : RROTATE_EXPR),
9744 rtype, TREE_OPERAND (arg0, 0),
9745 orig_tree01);
9746 return fold_convert_loc (loc, type, tem);
9749 else if (code == BIT_IOR_EXPR
9750 && code11 == BIT_AND_EXPR
9751 && pow2p_hwi (element_precision (rtype)))
9753 tree tree110, tree111;
9754 tree110 = TREE_OPERAND (tree11, 0);
9755 tree111 = TREE_OPERAND (tree11, 1);
9756 STRIP_NOPS (tree110);
9757 STRIP_NOPS (tree111);
9758 if (TREE_CODE (tree110) == NEGATE_EXPR
9759 && TREE_CODE (tree111) == INTEGER_CST
9760 && compare_tree_int (tree111,
9761 element_precision (rtype) - 1) == 0
9762 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
9764 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9765 ? LROTATE_EXPR : RROTATE_EXPR),
9766 rtype, TREE_OPERAND (arg0, 0),
9767 orig_tree01);
9768 return fold_convert_loc (loc, type, tem);
9774 associate:
9775 /* In most languages, can't associate operations on floats through
9776 parentheses. Rather than remember where the parentheses were, we
9777 don't associate floats at all, unless the user has specified
9778 -fassociative-math.
9779 And, we need to make sure type is not saturating. */
9781 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9782 && !TYPE_SATURATING (type))
9784 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9785 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9786 tree atype = type;
9787 bool ok = true;
9789 /* Split both trees into variables, constants, and literals. Then
9790 associate each group together, the constants with literals,
9791 then the result with variables. This increases the chances of
9792 literals being recombined later and of generating relocatable
9793 expressions for the sum of a constant and literal. */
9794 var0 = split_tree (arg0, type, code,
9795 &minus_var0, &con0, &minus_con0,
9796 &lit0, &minus_lit0, 0);
9797 var1 = split_tree (arg1, type, code,
9798 &minus_var1, &con1, &minus_con1,
9799 &lit1, &minus_lit1, code == MINUS_EXPR);
9801 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9802 if (code == MINUS_EXPR)
9803 code = PLUS_EXPR;
9805 /* With undefined overflow prefer doing association in a type
9806 which wraps on overflow, if that is one of the operand types. */
9807 if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
9808 && !TYPE_OVERFLOW_WRAPS (type))
9810 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9811 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9812 atype = TREE_TYPE (arg0);
9813 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9814 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9815 atype = TREE_TYPE (arg1);
9816 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9819 /* With undefined overflow we can only associate constants with one
9820 variable, and constants whose association doesn't overflow. */
9821 if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
9822 && !TYPE_OVERFLOW_WRAPS (atype))
9824 if ((var0 && var1) || (minus_var0 && minus_var1))
9826 /* ??? If split_tree would handle NEGATE_EXPR we could
9827 simply reject these cases and the allowed cases would
9828 be the var0/minus_var1 ones. */
9829 tree tmp0 = var0 ? var0 : minus_var0;
9830 tree tmp1 = var1 ? var1 : minus_var1;
9831 bool one_neg = false;
9833 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9835 tmp0 = TREE_OPERAND (tmp0, 0);
9836 one_neg = !one_neg;
9838 if (CONVERT_EXPR_P (tmp0)
9839 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9840 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9841 <= TYPE_PRECISION (atype)))
9842 tmp0 = TREE_OPERAND (tmp0, 0);
9843 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9845 tmp1 = TREE_OPERAND (tmp1, 0);
9846 one_neg = !one_neg;
9848 if (CONVERT_EXPR_P (tmp1)
9849 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9850 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9851 <= TYPE_PRECISION (atype)))
9852 tmp1 = TREE_OPERAND (tmp1, 0);
9853 /* The only case we can still associate with two variables
9854 is if they cancel out. */
9855 if (!one_neg
9856 || !operand_equal_p (tmp0, tmp1, 0))
9857 ok = false;
9859 else if ((var0 && minus_var1
9860 && ! operand_equal_p (var0, minus_var1, 0))
9861 || (minus_var0 && var1
9862 && ! operand_equal_p (minus_var0, var1, 0)))
9863 ok = false;
9866 /* Only do something if we found more than two objects. Otherwise,
9867 nothing has changed and we risk infinite recursion. */
9868 if (ok
9869 && ((var0 != 0) + (var1 != 0)
9870 + (minus_var0 != 0) + (minus_var1 != 0)
9871 + (con0 != 0) + (con1 != 0)
9872 + (minus_con0 != 0) + (minus_con1 != 0)
9873 + (lit0 != 0) + (lit1 != 0)
9874 + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
9876 var0 = associate_trees (loc, var0, var1, code, atype);
9877 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9878 code, atype);
9879 con0 = associate_trees (loc, con0, con1, code, atype);
9880 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9881 code, atype);
9882 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9883 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9884 code, atype);
9886 if (minus_var0 && var0)
9888 var0 = associate_trees (loc, var0, minus_var0,
9889 MINUS_EXPR, atype);
9890 minus_var0 = 0;
9892 if (minus_con0 && con0)
9894 con0 = associate_trees (loc, con0, minus_con0,
9895 MINUS_EXPR, atype);
9896 minus_con0 = 0;
9899 /* Preserve the MINUS_EXPR if the negative part of the literal is
9900 greater than the positive part. Otherwise, the multiplicative
9901 folding code (i.e extract_muldiv) may be fooled in case
9902 unsigned constants are subtracted, like in the following
9903 example: ((X*2 + 4) - 8U)/2. */
9904 if (minus_lit0 && lit0)
9906 if (TREE_CODE (lit0) == INTEGER_CST
9907 && TREE_CODE (minus_lit0) == INTEGER_CST
9908 && tree_int_cst_lt (lit0, minus_lit0)
9909 /* But avoid ending up with only negated parts. */
9910 && (var0 || con0))
9912 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9913 MINUS_EXPR, atype);
9914 lit0 = 0;
9916 else
9918 lit0 = associate_trees (loc, lit0, minus_lit0,
9919 MINUS_EXPR, atype);
9920 minus_lit0 = 0;
9924 /* Don't introduce overflows through reassociation. */
9925 if ((lit0 && TREE_OVERFLOW_P (lit0))
9926 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9927 return NULL_TREE;
9929 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9930 con0 = associate_trees (loc, con0, lit0, code, atype);
9931 lit0 = 0;
9932 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9933 code, atype);
9934 minus_lit0 = 0;
9936 /* Eliminate minus_con0. */
9937 if (minus_con0)
9939 if (con0)
9940 con0 = associate_trees (loc, con0, minus_con0,
9941 MINUS_EXPR, atype);
9942 else if (var0)
9943 var0 = associate_trees (loc, var0, minus_con0,
9944 MINUS_EXPR, atype);
9945 else
9946 gcc_unreachable ();
9947 minus_con0 = 0;
9950 /* Eliminate minus_var0. */
9951 if (minus_var0)
9953 if (con0)
9954 con0 = associate_trees (loc, con0, minus_var0,
9955 MINUS_EXPR, atype);
9956 else
9957 gcc_unreachable ();
9958 minus_var0 = 0;
9961 return
9962 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9963 code, atype));
9967 return NULL_TREE;
9969 case POINTER_DIFF_EXPR:
9970 case MINUS_EXPR:
9971 /* Fold &a[i] - &a[j] to i-j. */
9972 if (TREE_CODE (arg0) == ADDR_EXPR
9973 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9974 && TREE_CODE (arg1) == ADDR_EXPR
9975 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9977 tree tem = fold_addr_of_array_ref_difference (loc, type,
9978 TREE_OPERAND (arg0, 0),
9979 TREE_OPERAND (arg1, 0),
9980 code
9981 == POINTER_DIFF_EXPR);
9982 if (tem)
9983 return tem;
9986 /* Further transformations are not for pointers. */
9987 if (code == POINTER_DIFF_EXPR)
9988 return NULL_TREE;
9990 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9991 if (TREE_CODE (arg0) == NEGATE_EXPR
9992 && negate_expr_p (op1)
9993 /* If arg0 is e.g. unsigned int and type is int, then this could
9994 introduce UB, because if A is INT_MIN at runtime, the original
9995 expression can be well defined while the latter is not.
9996 See PR83269. */
9997 && !(ANY_INTEGRAL_TYPE_P (type)
9998 && TYPE_OVERFLOW_UNDEFINED (type)
9999 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10000 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10001 return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
10002 fold_convert_loc (loc, type,
10003 TREE_OPERAND (arg0, 0)));
10005 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10006 __complex__ ( x, -y ). This is not the same for SNaNs or if
10007 signed zeros are involved. */
10008 if (!HONOR_SNANS (element_mode (arg0))
10009 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10010 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10012 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10013 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10014 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10015 bool arg0rz = false, arg0iz = false;
10016 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10017 || (arg0i && (arg0iz = real_zerop (arg0i))))
10019 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10020 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10021 if (arg0rz && arg1i && real_zerop (arg1i))
10023 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10024 arg1r ? arg1r
10025 : build1 (REALPART_EXPR, rtype, arg1));
10026 tree ip = arg0i ? arg0i
10027 : build1 (IMAGPART_EXPR, rtype, arg0);
10028 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10030 else if (arg0iz && arg1r && real_zerop (arg1r))
10032 tree rp = arg0r ? arg0r
10033 : build1 (REALPART_EXPR, rtype, arg0);
10034 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10035 arg1i ? arg1i
10036 : build1 (IMAGPART_EXPR, rtype, arg1));
10037 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10042 /* A - B -> A + (-B) if B is easily negatable. */
10043 if (negate_expr_p (op1)
10044 && ! TYPE_OVERFLOW_SANITIZED (type)
10045 && ((FLOAT_TYPE_P (type)
10046 /* Avoid this transformation if B is a positive REAL_CST. */
10047 && (TREE_CODE (op1) != REAL_CST
10048 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
10049 || INTEGRAL_TYPE_P (type)))
10050 return fold_build2_loc (loc, PLUS_EXPR, type,
10051 fold_convert_loc (loc, type, arg0),
10052 negate_expr (op1));
10054 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
10055 one. Make sure the type is not saturating and has the signedness of
10056 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10057 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10058 if ((TREE_CODE (arg0) == MULT_EXPR
10059 || TREE_CODE (arg1) == MULT_EXPR)
10060 && !TYPE_SATURATING (type)
10061 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10062 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10063 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10065 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10066 if (tem)
10067 return tem;
10070 goto associate;
10072 case MULT_EXPR:
10073 if (! FLOAT_TYPE_P (type))
10075 /* Transform x * -C into -x * C if x is easily negatable. */
10076 if (TREE_CODE (op1) == INTEGER_CST
10077 && tree_int_cst_sgn (op1) == -1
10078 && negate_expr_p (op0)
10079 && negate_expr_p (op1)
10080 && (tem = negate_expr (op1)) != op1
10081 && ! TREE_OVERFLOW (tem))
10082 return fold_build2_loc (loc, MULT_EXPR, type,
10083 fold_convert_loc (loc, type,
10084 negate_expr (op0)), tem);
10086 strict_overflow_p = false;
10087 if (TREE_CODE (arg1) == INTEGER_CST
10088 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10089 &strict_overflow_p)) != 0)
10091 if (strict_overflow_p)
10092 fold_overflow_warning (("assuming signed overflow does not "
10093 "occur when simplifying "
10094 "multiplication"),
10095 WARN_STRICT_OVERFLOW_MISC);
10096 return fold_convert_loc (loc, type, tem);
10099 /* Optimize z * conj(z) for integer complex numbers. */
10100 if (TREE_CODE (arg0) == CONJ_EXPR
10101 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10102 return fold_mult_zconjz (loc, type, arg1);
10103 if (TREE_CODE (arg1) == CONJ_EXPR
10104 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10105 return fold_mult_zconjz (loc, type, arg0);
10107 else
10109 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10110 This is not the same for NaNs or if signed zeros are
10111 involved. */
10112 if (!HONOR_NANS (arg0)
10113 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10114 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10115 && TREE_CODE (arg1) == COMPLEX_CST
10116 && real_zerop (TREE_REALPART (arg1)))
10118 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10119 if (real_onep (TREE_IMAGPART (arg1)))
10120 return
10121 fold_build2_loc (loc, COMPLEX_EXPR, type,
10122 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10123 rtype, arg0)),
10124 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10125 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10126 return
10127 fold_build2_loc (loc, COMPLEX_EXPR, type,
10128 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10129 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10130 rtype, arg0)));
10133 /* Optimize z * conj(z) for floating point complex numbers.
10134 Guarded by flag_unsafe_math_optimizations as non-finite
10135 imaginary components don't produce scalar results. */
10136 if (flag_unsafe_math_optimizations
10137 && TREE_CODE (arg0) == CONJ_EXPR
10138 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10139 return fold_mult_zconjz (loc, type, arg1);
10140 if (flag_unsafe_math_optimizations
10141 && TREE_CODE (arg1) == CONJ_EXPR
10142 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10143 return fold_mult_zconjz (loc, type, arg0);
10145 goto associate;
10147 case BIT_IOR_EXPR:
10148 /* Canonicalize (X & C1) | C2. */
10149 if (TREE_CODE (arg0) == BIT_AND_EXPR
10150 && TREE_CODE (arg1) == INTEGER_CST
10151 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10153 int width = TYPE_PRECISION (type), w;
10154 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
10155 wide_int c2 = wi::to_wide (arg1);
10157 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10158 if ((c1 & c2) == c1)
10159 return omit_one_operand_loc (loc, type, arg1,
10160 TREE_OPERAND (arg0, 0));
10162 wide_int msk = wi::mask (width, false,
10163 TYPE_PRECISION (TREE_TYPE (arg1)));
10165 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10166 if (wi::bit_and_not (msk, c1 | c2) == 0)
10168 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10169 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10172 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10173 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10174 mode which allows further optimizations. */
10175 c1 &= msk;
10176 c2 &= msk;
10177 wide_int c3 = wi::bit_and_not (c1, c2);
10178 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10180 wide_int mask = wi::mask (w, false,
10181 TYPE_PRECISION (type));
10182 if (((c1 | c2) & mask) == mask
10183 && wi::bit_and_not (c1, mask) == 0)
10185 c3 = mask;
10186 break;
10190 if (c3 != c1)
10192 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10193 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
10194 wide_int_to_tree (type, c3));
10195 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10199 /* See if this can be simplified into a rotate first. If that
10200 is unsuccessful continue in the association code. */
10201 goto bit_rotate;
10203 case BIT_XOR_EXPR:
10204 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10205 if (TREE_CODE (arg0) == BIT_AND_EXPR
10206 && INTEGRAL_TYPE_P (type)
10207 && integer_onep (TREE_OPERAND (arg0, 1))
10208 && integer_onep (arg1))
10209 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10210 build_zero_cst (TREE_TYPE (arg0)));
10212 /* See if this can be simplified into a rotate first. If that
10213 is unsuccessful continue in the association code. */
10214 goto bit_rotate;
10216 case BIT_AND_EXPR:
10217 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10218 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10219 && INTEGRAL_TYPE_P (type)
10220 && integer_onep (TREE_OPERAND (arg0, 1))
10221 && integer_onep (arg1))
10223 tree tem2;
10224 tem = TREE_OPERAND (arg0, 0);
10225 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10226 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10227 tem, tem2);
10228 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10229 build_zero_cst (TREE_TYPE (tem)));
10231 /* Fold ~X & 1 as (X & 1) == 0. */
10232 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10233 && INTEGRAL_TYPE_P (type)
10234 && integer_onep (arg1))
10236 tree tem2;
10237 tem = TREE_OPERAND (arg0, 0);
10238 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10239 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10240 tem, tem2);
10241 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10242 build_zero_cst (TREE_TYPE (tem)));
10244 /* Fold !X & 1 as X == 0. */
10245 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10246 && integer_onep (arg1))
10248 tem = TREE_OPERAND (arg0, 0);
10249 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10250 build_zero_cst (TREE_TYPE (tem)));
10253 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10254 multiple of 1 << CST. */
10255 if (TREE_CODE (arg1) == INTEGER_CST)
10257 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10258 wide_int ncst1 = -cst1;
10259 if ((cst1 & ncst1) == ncst1
10260 && multiple_of_p (type, arg0,
10261 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10262 return fold_convert_loc (loc, type, arg0);
10265 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10266 bits from CST2. */
10267 if (TREE_CODE (arg1) == INTEGER_CST
10268 && TREE_CODE (arg0) == MULT_EXPR
10269 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10271 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
10272 wide_int masked
10273 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
10275 if (masked == 0)
10276 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10277 arg0, arg1);
10278 else if (masked != warg1)
10280 /* Avoid the transform if arg1 is a mask of some
10281 mode which allows further optimizations. */
10282 int pop = wi::popcount (warg1);
10283 if (!(pop >= BITS_PER_UNIT
10284 && pow2p_hwi (pop)
10285 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10286 return fold_build2_loc (loc, code, type, op0,
10287 wide_int_to_tree (type, masked));
10291 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10292 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10293 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10295 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10297 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
10298 if (mask == -1)
10299 return
10300 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10303 goto associate;
10305 case RDIV_EXPR:
10306 /* Don't touch a floating-point divide by zero unless the mode
10307 of the constant can represent infinity. */
10308 if (TREE_CODE (arg1) == REAL_CST
10309 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10310 && real_zerop (arg1))
10311 return NULL_TREE;
10313 /* (-A) / (-B) -> A / B */
10314 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10315 return fold_build2_loc (loc, RDIV_EXPR, type,
10316 TREE_OPERAND (arg0, 0),
10317 negate_expr (arg1));
10318 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10319 return fold_build2_loc (loc, RDIV_EXPR, type,
10320 negate_expr (arg0),
10321 TREE_OPERAND (arg1, 0));
10322 return NULL_TREE;
10324 case TRUNC_DIV_EXPR:
10325 /* Fall through */
10327 case FLOOR_DIV_EXPR:
10328 /* Simplify A / (B << N) where A and B are positive and B is
10329 a power of 2, to A >> (N + log2(B)). */
10330 strict_overflow_p = false;
10331 if (TREE_CODE (arg1) == LSHIFT_EXPR
10332 && (TYPE_UNSIGNED (type)
10333 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10335 tree sval = TREE_OPERAND (arg1, 0);
10336 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10338 tree sh_cnt = TREE_OPERAND (arg1, 1);
10339 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10340 wi::exact_log2 (wi::to_wide (sval)));
10342 if (strict_overflow_p)
10343 fold_overflow_warning (("assuming signed overflow does not "
10344 "occur when simplifying A / (B << N)"),
10345 WARN_STRICT_OVERFLOW_MISC);
10347 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10348 sh_cnt, pow2);
10349 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10350 fold_convert_loc (loc, type, arg0), sh_cnt);
10354 /* Fall through */
10356 case ROUND_DIV_EXPR:
10357 case CEIL_DIV_EXPR:
10358 case EXACT_DIV_EXPR:
10359 if (integer_zerop (arg1))
10360 return NULL_TREE;
10362 /* Convert -A / -B to A / B when the type is signed and overflow is
10363 undefined. */
10364 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10365 && TREE_CODE (op0) == NEGATE_EXPR
10366 && negate_expr_p (op1))
10368 if (INTEGRAL_TYPE_P (type))
10369 fold_overflow_warning (("assuming signed overflow does not occur "
10370 "when distributing negation across "
10371 "division"),
10372 WARN_STRICT_OVERFLOW_MISC);
10373 return fold_build2_loc (loc, code, type,
10374 fold_convert_loc (loc, type,
10375 TREE_OPERAND (arg0, 0)),
10376 negate_expr (op1));
10378 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10379 && TREE_CODE (arg1) == NEGATE_EXPR
10380 && negate_expr_p (op0))
10382 if (INTEGRAL_TYPE_P (type))
10383 fold_overflow_warning (("assuming signed overflow does not occur "
10384 "when distributing negation across "
10385 "division"),
10386 WARN_STRICT_OVERFLOW_MISC);
10387 return fold_build2_loc (loc, code, type,
10388 negate_expr (op0),
10389 fold_convert_loc (loc, type,
10390 TREE_OPERAND (arg1, 0)));
10393 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10394 operation, EXACT_DIV_EXPR.
10396 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10397 At one time others generated faster code, it's not clear if they do
10398 after the last round to changes to the DIV code in expmed.c. */
10399 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10400 && multiple_of_p (type, arg0, arg1))
10401 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10402 fold_convert (type, arg0),
10403 fold_convert (type, arg1));
10405 strict_overflow_p = false;
10406 if (TREE_CODE (arg1) == INTEGER_CST
10407 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10408 &strict_overflow_p)) != 0)
10410 if (strict_overflow_p)
10411 fold_overflow_warning (("assuming signed overflow does not occur "
10412 "when simplifying division"),
10413 WARN_STRICT_OVERFLOW_MISC);
10414 return fold_convert_loc (loc, type, tem);
10417 return NULL_TREE;
10419 case CEIL_MOD_EXPR:
10420 case FLOOR_MOD_EXPR:
10421 case ROUND_MOD_EXPR:
10422 case TRUNC_MOD_EXPR:
10423 strict_overflow_p = false;
10424 if (TREE_CODE (arg1) == INTEGER_CST
10425 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10426 &strict_overflow_p)) != 0)
10428 if (strict_overflow_p)
10429 fold_overflow_warning (("assuming signed overflow does not occur "
10430 "when simplifying modulus"),
10431 WARN_STRICT_OVERFLOW_MISC);
10432 return fold_convert_loc (loc, type, tem);
10435 return NULL_TREE;
10437 case LROTATE_EXPR:
10438 case RROTATE_EXPR:
10439 case RSHIFT_EXPR:
10440 case LSHIFT_EXPR:
10441 /* Since negative shift count is not well-defined,
10442 don't try to compute it in the compiler. */
10443 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10444 return NULL_TREE;
10446 prec = element_precision (type);
10448 /* If we have a rotate of a bit operation with the rotate count and
10449 the second operand of the bit operation both constant,
10450 permute the two operations. */
10451 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10452 && (TREE_CODE (arg0) == BIT_AND_EXPR
10453 || TREE_CODE (arg0) == BIT_IOR_EXPR
10454 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10455 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10457 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10458 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10459 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10460 fold_build2_loc (loc, code, type,
10461 arg00, arg1),
10462 fold_build2_loc (loc, code, type,
10463 arg01, arg1));
10466 /* Two consecutive rotates adding up to the some integer
10467 multiple of the precision of the type can be ignored. */
10468 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10469 && TREE_CODE (arg0) == RROTATE_EXPR
10470 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10471 && wi::umod_trunc (wi::to_wide (arg1)
10472 + wi::to_wide (TREE_OPERAND (arg0, 1)),
10473 prec) == 0)
10474 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10476 return NULL_TREE;
10478 case MIN_EXPR:
10479 case MAX_EXPR:
10480 goto associate;
10482 case TRUTH_ANDIF_EXPR:
10483 /* Note that the operands of this must be ints
10484 and their values must be 0 or 1.
10485 ("true" is a fixed value perhaps depending on the language.) */
10486 /* If first arg is constant zero, return it. */
10487 if (integer_zerop (arg0))
10488 return fold_convert_loc (loc, type, arg0);
10489 /* FALLTHRU */
10490 case TRUTH_AND_EXPR:
10491 /* If either arg is constant true, drop it. */
10492 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10493 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10494 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10495 /* Preserve sequence points. */
10496 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10497 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10498 /* If second arg is constant zero, result is zero, but first arg
10499 must be evaluated. */
10500 if (integer_zerop (arg1))
10501 return omit_one_operand_loc (loc, type, arg1, arg0);
10502 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10503 case will be handled here. */
10504 if (integer_zerop (arg0))
10505 return omit_one_operand_loc (loc, type, arg0, arg1);
10507 /* !X && X is always false. */
10508 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10509 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10510 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10511 /* X && !X is always false. */
10512 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10513 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10514 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10516 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10517 means A >= Y && A != MAX, but in this case we know that
10518 A < X <= MAX. */
10520 if (!TREE_SIDE_EFFECTS (arg0)
10521 && !TREE_SIDE_EFFECTS (arg1))
10523 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10524 if (tem && !operand_equal_p (tem, arg0, 0))
10525 return fold_build2_loc (loc, code, type, tem, arg1);
10527 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10528 if (tem && !operand_equal_p (tem, arg1, 0))
10529 return fold_build2_loc (loc, code, type, arg0, tem);
10532 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10533 != NULL_TREE)
10534 return tem;
10536 return NULL_TREE;
10538 case TRUTH_ORIF_EXPR:
10539 /* Note that the operands of this must be ints
10540 and their values must be 0 or true.
10541 ("true" is a fixed value perhaps depending on the language.) */
10542 /* If first arg is constant true, return it. */
10543 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10544 return fold_convert_loc (loc, type, arg0);
10545 /* FALLTHRU */
10546 case TRUTH_OR_EXPR:
10547 /* If either arg is constant zero, drop it. */
10548 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10549 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10550 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10551 /* Preserve sequence points. */
10552 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10553 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10554 /* If second arg is constant true, result is true, but we must
10555 evaluate first arg. */
10556 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10557 return omit_one_operand_loc (loc, type, arg1, arg0);
10558 /* Likewise for first arg, but note this only occurs here for
10559 TRUTH_OR_EXPR. */
10560 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10561 return omit_one_operand_loc (loc, type, arg0, arg1);
10563 /* !X || X is always true. */
10564 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10565 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10566 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10567 /* X || !X is always true. */
10568 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10569 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10570 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10572 /* (X && !Y) || (!X && Y) is X ^ Y */
10573 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10574 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10576 tree a0, a1, l0, l1, n0, n1;
10578 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10579 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10581 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10582 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10584 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10585 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10587 if ((operand_equal_p (n0, a0, 0)
10588 && operand_equal_p (n1, a1, 0))
10589 || (operand_equal_p (n0, a1, 0)
10590 && operand_equal_p (n1, a0, 0)))
10591 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10594 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10595 != NULL_TREE)
10596 return tem;
10598 return NULL_TREE;
10600 case TRUTH_XOR_EXPR:
10601 /* If the second arg is constant zero, drop it. */
10602 if (integer_zerop (arg1))
10603 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10604 /* If the second arg is constant true, this is a logical inversion. */
10605 if (integer_onep (arg1))
10607 tem = invert_truthvalue_loc (loc, arg0);
10608 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10610 /* Identical arguments cancel to zero. */
10611 if (operand_equal_p (arg0, arg1, 0))
10612 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10614 /* !X ^ X is always true. */
10615 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10616 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10617 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10619 /* X ^ !X is always true. */
10620 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10621 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10622 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10624 return NULL_TREE;
10626 case EQ_EXPR:
10627 case NE_EXPR:
10628 STRIP_NOPS (arg0);
10629 STRIP_NOPS (arg1);
10631 tem = fold_comparison (loc, code, type, op0, op1);
10632 if (tem != NULL_TREE)
10633 return tem;
10635 /* bool_var != 1 becomes !bool_var. */
10636 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10637 && code == NE_EXPR)
10638 return fold_convert_loc (loc, type,
10639 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10640 TREE_TYPE (arg0), arg0));
10642 /* bool_var == 0 becomes !bool_var. */
10643 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10644 && code == EQ_EXPR)
10645 return fold_convert_loc (loc, type,
10646 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10647 TREE_TYPE (arg0), arg0));
10649 /* !exp != 0 becomes !exp */
10650 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10651 && code == NE_EXPR)
10652 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10654 /* If this is an EQ or NE comparison with zero and ARG0 is
10655 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10656 two operations, but the latter can be done in one less insn
10657 on machines that have only two-operand insns or on which a
10658 constant cannot be the first operand. */
10659 if (TREE_CODE (arg0) == BIT_AND_EXPR
10660 && integer_zerop (arg1))
10662 tree arg00 = TREE_OPERAND (arg0, 0);
10663 tree arg01 = TREE_OPERAND (arg0, 1);
10664 if (TREE_CODE (arg00) == LSHIFT_EXPR
10665 && integer_onep (TREE_OPERAND (arg00, 0)))
10667 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10668 arg01, TREE_OPERAND (arg00, 1));
10669 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10670 build_int_cst (TREE_TYPE (arg0), 1));
10671 return fold_build2_loc (loc, code, type,
10672 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10673 arg1);
10675 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10676 && integer_onep (TREE_OPERAND (arg01, 0)))
10678 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10679 arg00, TREE_OPERAND (arg01, 1));
10680 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10681 build_int_cst (TREE_TYPE (arg0), 1));
10682 return fold_build2_loc (loc, code, type,
10683 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10684 arg1);
10688 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10689 C1 is a valid shift constant, and C2 is a power of two, i.e.
10690 a single bit. */
10691 if (TREE_CODE (arg0) == BIT_AND_EXPR
10692 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10693 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10694 == INTEGER_CST
10695 && integer_pow2p (TREE_OPERAND (arg0, 1))
10696 && integer_zerop (arg1))
10698 tree itype = TREE_TYPE (arg0);
10699 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10700 prec = TYPE_PRECISION (itype);
10702 /* Check for a valid shift count. */
10703 if (wi::ltu_p (wi::to_wide (arg001), prec))
10705 tree arg01 = TREE_OPERAND (arg0, 1);
10706 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10707 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10708 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10709 can be rewritten as (X & (C2 << C1)) != 0. */
10710 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10712 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10713 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10714 return fold_build2_loc (loc, code, type, tem,
10715 fold_convert_loc (loc, itype, arg1));
10717 /* Otherwise, for signed (arithmetic) shifts,
10718 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10719 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10720 else if (!TYPE_UNSIGNED (itype))
10721 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10722 arg000, build_int_cst (itype, 0));
10723 /* Otherwise, of unsigned (logical) shifts,
10724 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10725 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10726 else
10727 return omit_one_operand_loc (loc, type,
10728 code == EQ_EXPR ? integer_one_node
10729 : integer_zero_node,
10730 arg000);
10734 /* If this is a comparison of a field, we may be able to simplify it. */
10735 if ((TREE_CODE (arg0) == COMPONENT_REF
10736 || TREE_CODE (arg0) == BIT_FIELD_REF)
10737 /* Handle the constant case even without -O
10738 to make sure the warnings are given. */
10739 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10741 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10742 if (t1)
10743 return t1;
10746 /* Optimize comparisons of strlen vs zero to a compare of the
10747 first character of the string vs zero. To wit,
10748 strlen(ptr) == 0 => *ptr == 0
10749 strlen(ptr) != 0 => *ptr != 0
10750 Other cases should reduce to one of these two (or a constant)
10751 due to the return value of strlen being unsigned. */
10752 if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
10754 tree fndecl = get_callee_fndecl (arg0);
10756 if (fndecl
10757 && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
10758 && call_expr_nargs (arg0) == 1
10759 && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
10760 == POINTER_TYPE))
10762 tree ptrtype
10763 = build_pointer_type (build_qualified_type (char_type_node,
10764 TYPE_QUAL_CONST));
10765 tree ptr = fold_convert_loc (loc, ptrtype,
10766 CALL_EXPR_ARG (arg0, 0));
10767 tree iref = build_fold_indirect_ref_loc (loc, ptr);
10768 return fold_build2_loc (loc, code, type, iref,
10769 build_int_cst (TREE_TYPE (iref), 0));
10773 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10774 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10775 if (TREE_CODE (arg0) == RSHIFT_EXPR
10776 && integer_zerop (arg1)
10777 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10779 tree arg00 = TREE_OPERAND (arg0, 0);
10780 tree arg01 = TREE_OPERAND (arg0, 1);
10781 tree itype = TREE_TYPE (arg00);
10782 if (wi::to_wide (arg01) == element_precision (itype) - 1)
10784 if (TYPE_UNSIGNED (itype))
10786 itype = signed_type_for (itype);
10787 arg00 = fold_convert_loc (loc, itype, arg00);
10789 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10790 type, arg00, build_zero_cst (itype));
10794 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10795 (X & C) == 0 when C is a single bit. */
10796 if (TREE_CODE (arg0) == BIT_AND_EXPR
10797 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10798 && integer_zerop (arg1)
10799 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10801 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10802 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10803 TREE_OPERAND (arg0, 1));
10804 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10805 type, tem,
10806 fold_convert_loc (loc, TREE_TYPE (arg0),
10807 arg1));
10810 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10811 constant C is a power of two, i.e. a single bit. */
10812 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10813 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10814 && integer_zerop (arg1)
10815 && integer_pow2p (TREE_OPERAND (arg0, 1))
10816 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10817 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10819 tree arg00 = TREE_OPERAND (arg0, 0);
10820 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10821 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10824 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10825 when is C is a power of two, i.e. a single bit. */
10826 if (TREE_CODE (arg0) == BIT_AND_EXPR
10827 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10828 && integer_zerop (arg1)
10829 && integer_pow2p (TREE_OPERAND (arg0, 1))
10830 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10831 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10833 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10834 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10835 arg000, TREE_OPERAND (arg0, 1));
10836 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10837 tem, build_int_cst (TREE_TYPE (tem), 0));
10840 if (integer_zerop (arg1)
10841 && tree_expr_nonzero_p (arg0))
10843 tree res = constant_boolean_node (code==NE_EXPR, type);
10844 return omit_one_operand_loc (loc, type, res, arg0);
10847 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10848 if (TREE_CODE (arg0) == BIT_AND_EXPR
10849 && TREE_CODE (arg1) == BIT_AND_EXPR)
10851 tree arg00 = TREE_OPERAND (arg0, 0);
10852 tree arg01 = TREE_OPERAND (arg0, 1);
10853 tree arg10 = TREE_OPERAND (arg1, 0);
10854 tree arg11 = TREE_OPERAND (arg1, 1);
10855 tree itype = TREE_TYPE (arg0);
10857 if (operand_equal_p (arg01, arg11, 0))
10859 tem = fold_convert_loc (loc, itype, arg10);
10860 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10861 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10862 return fold_build2_loc (loc, code, type, tem,
10863 build_zero_cst (itype));
10865 if (operand_equal_p (arg01, arg10, 0))
10867 tem = fold_convert_loc (loc, itype, arg11);
10868 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10869 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10870 return fold_build2_loc (loc, code, type, tem,
10871 build_zero_cst (itype));
10873 if (operand_equal_p (arg00, arg11, 0))
10875 tem = fold_convert_loc (loc, itype, arg10);
10876 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10877 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10878 return fold_build2_loc (loc, code, type, tem,
10879 build_zero_cst (itype));
10881 if (operand_equal_p (arg00, arg10, 0))
10883 tem = fold_convert_loc (loc, itype, arg11);
10884 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10885 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10886 return fold_build2_loc (loc, code, type, tem,
10887 build_zero_cst (itype));
10891 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10892 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10894 tree arg00 = TREE_OPERAND (arg0, 0);
10895 tree arg01 = TREE_OPERAND (arg0, 1);
10896 tree arg10 = TREE_OPERAND (arg1, 0);
10897 tree arg11 = TREE_OPERAND (arg1, 1);
10898 tree itype = TREE_TYPE (arg0);
10900 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10901 operand_equal_p guarantees no side-effects so we don't need
10902 to use omit_one_operand on Z. */
10903 if (operand_equal_p (arg01, arg11, 0))
10904 return fold_build2_loc (loc, code, type, arg00,
10905 fold_convert_loc (loc, TREE_TYPE (arg00),
10906 arg10));
10907 if (operand_equal_p (arg01, arg10, 0))
10908 return fold_build2_loc (loc, code, type, arg00,
10909 fold_convert_loc (loc, TREE_TYPE (arg00),
10910 arg11));
10911 if (operand_equal_p (arg00, arg11, 0))
10912 return fold_build2_loc (loc, code, type, arg01,
10913 fold_convert_loc (loc, TREE_TYPE (arg01),
10914 arg10));
10915 if (operand_equal_p (arg00, arg10, 0))
10916 return fold_build2_loc (loc, code, type, arg01,
10917 fold_convert_loc (loc, TREE_TYPE (arg01),
10918 arg11));
10920 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10921 if (TREE_CODE (arg01) == INTEGER_CST
10922 && TREE_CODE (arg11) == INTEGER_CST)
10924 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10925 fold_convert_loc (loc, itype, arg11));
10926 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10927 return fold_build2_loc (loc, code, type, tem,
10928 fold_convert_loc (loc, itype, arg10));
10932 /* Attempt to simplify equality/inequality comparisons of complex
10933 values. Only lower the comparison if the result is known or
10934 can be simplified to a single scalar comparison. */
10935 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10936 || TREE_CODE (arg0) == COMPLEX_CST)
10937 && (TREE_CODE (arg1) == COMPLEX_EXPR
10938 || TREE_CODE (arg1) == COMPLEX_CST))
10940 tree real0, imag0, real1, imag1;
10941 tree rcond, icond;
10943 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10945 real0 = TREE_OPERAND (arg0, 0);
10946 imag0 = TREE_OPERAND (arg0, 1);
10948 else
10950 real0 = TREE_REALPART (arg0);
10951 imag0 = TREE_IMAGPART (arg0);
10954 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10956 real1 = TREE_OPERAND (arg1, 0);
10957 imag1 = TREE_OPERAND (arg1, 1);
10959 else
10961 real1 = TREE_REALPART (arg1);
10962 imag1 = TREE_IMAGPART (arg1);
10965 rcond = fold_binary_loc (loc, code, type, real0, real1);
10966 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10968 if (integer_zerop (rcond))
10970 if (code == EQ_EXPR)
10971 return omit_two_operands_loc (loc, type, boolean_false_node,
10972 imag0, imag1);
10973 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10975 else
10977 if (code == NE_EXPR)
10978 return omit_two_operands_loc (loc, type, boolean_true_node,
10979 imag0, imag1);
10980 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10984 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10985 if (icond && TREE_CODE (icond) == INTEGER_CST)
10987 if (integer_zerop (icond))
10989 if (code == EQ_EXPR)
10990 return omit_two_operands_loc (loc, type, boolean_false_node,
10991 real0, real1);
10992 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
10994 else
10996 if (code == NE_EXPR)
10997 return omit_two_operands_loc (loc, type, boolean_true_node,
10998 real0, real1);
10999 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11004 return NULL_TREE;
11006 case LT_EXPR:
11007 case GT_EXPR:
11008 case LE_EXPR:
11009 case GE_EXPR:
11010 tem = fold_comparison (loc, code, type, op0, op1);
11011 if (tem != NULL_TREE)
11012 return tem;
11014 /* Transform comparisons of the form X +- C CMP X. */
11015 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11016 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11017 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11018 && !HONOR_SNANS (arg0))
11020 tree arg01 = TREE_OPERAND (arg0, 1);
11021 enum tree_code code0 = TREE_CODE (arg0);
11022 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11024 /* (X - c) > X becomes false. */
11025 if (code == GT_EXPR
11026 && ((code0 == MINUS_EXPR && is_positive >= 0)
11027 || (code0 == PLUS_EXPR && is_positive <= 0)))
11028 return constant_boolean_node (0, type);
11030 /* Likewise (X + c) < X becomes false. */
11031 if (code == LT_EXPR
11032 && ((code0 == PLUS_EXPR && is_positive >= 0)
11033 || (code0 == MINUS_EXPR && is_positive <= 0)))
11034 return constant_boolean_node (0, type);
11036 /* Convert (X - c) <= X to true. */
11037 if (!HONOR_NANS (arg1)
11038 && code == LE_EXPR
11039 && ((code0 == MINUS_EXPR && is_positive >= 0)
11040 || (code0 == PLUS_EXPR && is_positive <= 0)))
11041 return constant_boolean_node (1, type);
11043 /* Convert (X + c) >= X to true. */
11044 if (!HONOR_NANS (arg1)
11045 && code == GE_EXPR
11046 && ((code0 == PLUS_EXPR && is_positive >= 0)
11047 || (code0 == MINUS_EXPR && is_positive <= 0)))
11048 return constant_boolean_node (1, type);
11051 /* If we are comparing an ABS_EXPR with a constant, we can
11052 convert all the cases into explicit comparisons, but they may
11053 well not be faster than doing the ABS and one comparison.
11054 But ABS (X) <= C is a range comparison, which becomes a subtraction
11055 and a comparison, and is probably faster. */
11056 if (code == LE_EXPR
11057 && TREE_CODE (arg1) == INTEGER_CST
11058 && TREE_CODE (arg0) == ABS_EXPR
11059 && ! TREE_SIDE_EFFECTS (arg0)
11060 && (tem = negate_expr (arg1)) != 0
11061 && TREE_CODE (tem) == INTEGER_CST
11062 && !TREE_OVERFLOW (tem))
11063 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11064 build2 (GE_EXPR, type,
11065 TREE_OPERAND (arg0, 0), tem),
11066 build2 (LE_EXPR, type,
11067 TREE_OPERAND (arg0, 0), arg1));
11069 /* Convert ABS_EXPR<x> >= 0 to true. */
11070 strict_overflow_p = false;
11071 if (code == GE_EXPR
11072 && (integer_zerop (arg1)
11073 || (! HONOR_NANS (arg0)
11074 && real_zerop (arg1)))
11075 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11077 if (strict_overflow_p)
11078 fold_overflow_warning (("assuming signed overflow does not occur "
11079 "when simplifying comparison of "
11080 "absolute value and zero"),
11081 WARN_STRICT_OVERFLOW_CONDITIONAL);
11082 return omit_one_operand_loc (loc, type,
11083 constant_boolean_node (true, type),
11084 arg0);
11087 /* Convert ABS_EXPR<x> < 0 to false. */
11088 strict_overflow_p = false;
11089 if (code == LT_EXPR
11090 && (integer_zerop (arg1) || real_zerop (arg1))
11091 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11093 if (strict_overflow_p)
11094 fold_overflow_warning (("assuming signed overflow does not occur "
11095 "when simplifying comparison of "
11096 "absolute value and zero"),
11097 WARN_STRICT_OVERFLOW_CONDITIONAL);
11098 return omit_one_operand_loc (loc, type,
11099 constant_boolean_node (false, type),
11100 arg0);
11103 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11104 and similarly for >= into !=. */
11105 if ((code == LT_EXPR || code == GE_EXPR)
11106 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11107 && TREE_CODE (arg1) == LSHIFT_EXPR
11108 && integer_onep (TREE_OPERAND (arg1, 0)))
11109 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11110 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11111 TREE_OPERAND (arg1, 1)),
11112 build_zero_cst (TREE_TYPE (arg0)));
11114 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11115 otherwise Y might be >= # of bits in X's type and thus e.g.
11116 (unsigned char) (1 << Y) for Y 15 might be 0.
11117 If the cast is widening, then 1 << Y should have unsigned type,
11118 otherwise if Y is number of bits in the signed shift type minus 1,
11119 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11120 31 might be 0xffffffff80000000. */
11121 if ((code == LT_EXPR || code == GE_EXPR)
11122 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11123 && CONVERT_EXPR_P (arg1)
11124 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11125 && (element_precision (TREE_TYPE (arg1))
11126 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11127 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11128 || (element_precision (TREE_TYPE (arg1))
11129 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11130 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11132 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11133 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11134 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11135 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11136 build_zero_cst (TREE_TYPE (arg0)));
11139 return NULL_TREE;
11141 case UNORDERED_EXPR:
11142 case ORDERED_EXPR:
11143 case UNLT_EXPR:
11144 case UNLE_EXPR:
11145 case UNGT_EXPR:
11146 case UNGE_EXPR:
11147 case UNEQ_EXPR:
11148 case LTGT_EXPR:
11149 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11151 tree targ0 = strip_float_extensions (arg0);
11152 tree targ1 = strip_float_extensions (arg1);
11153 tree newtype = TREE_TYPE (targ0);
11155 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11156 newtype = TREE_TYPE (targ1);
11158 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11159 return fold_build2_loc (loc, code, type,
11160 fold_convert_loc (loc, newtype, targ0),
11161 fold_convert_loc (loc, newtype, targ1));
11164 return NULL_TREE;
11166 case COMPOUND_EXPR:
11167 /* When pedantic, a compound expression can be neither an lvalue
11168 nor an integer constant expression. */
11169 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11170 return NULL_TREE;
11171 /* Don't let (0, 0) be null pointer constant. */
11172 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11173 : fold_convert_loc (loc, type, arg1);
11174 return pedantic_non_lvalue_loc (loc, tem);
11176 case ASSERT_EXPR:
11177 /* An ASSERT_EXPR should never be passed to fold_binary. */
11178 gcc_unreachable ();
11180 default:
11181 return NULL_TREE;
11182 } /* switch (code) */
11185 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11186 ((A & N) + B) & M -> (A + B) & M
11187 Similarly if (N & M) == 0,
11188 ((A | N) + B) & M -> (A + B) & M
11189 and for - instead of + (or unary - instead of +)
11190 and/or ^ instead of |.
11191 If B is constant and (B & M) == 0, fold into A & M.
11193 This function is a helper for match.pd patterns. Return non-NULL
11194 type in which the simplified operation should be performed only
11195 if any optimization is possible.
11197 ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
11198 then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
11199 Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
11200 +/-. */
11201 tree
11202 fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
11203 tree arg00, enum tree_code code00, tree arg000, tree arg001,
11204 tree arg01, enum tree_code code01, tree arg010, tree arg011,
11205 tree *pmop)
11207 gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
11208 gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
11209 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11210 if (~cst1 == 0
11211 || (cst1 & (cst1 + 1)) != 0
11212 || !INTEGRAL_TYPE_P (type)
11213 || (!TYPE_OVERFLOW_WRAPS (type)
11214 && TREE_CODE (type) != INTEGER_TYPE)
11215 || (wi::max_value (type) & cst1) != cst1)
11216 return NULL_TREE;
11218 enum tree_code codes[2] = { code00, code01 };
11219 tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
11220 int which = 0;
11221 wide_int cst0;
11223 /* Now we know that arg0 is (C + D) or (C - D) or -C and
11224 arg1 (M) is == (1LL << cst) - 1.
11225 Store C into PMOP[0] and D into PMOP[1]. */
11226 pmop[0] = arg00;
11227 pmop[1] = arg01;
11228 which = code != NEGATE_EXPR;
11230 for (; which >= 0; which--)
11231 switch (codes[which])
11233 case BIT_AND_EXPR:
11234 case BIT_IOR_EXPR:
11235 case BIT_XOR_EXPR:
11236 gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
11237 cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
11238 if (codes[which] == BIT_AND_EXPR)
11240 if (cst0 != cst1)
11241 break;
11243 else if (cst0 != 0)
11244 break;
11245 /* If C or D is of the form (A & N) where
11246 (N & M) == M, or of the form (A | N) or
11247 (A ^ N) where (N & M) == 0, replace it with A. */
11248 pmop[which] = arg0xx[2 * which];
11249 break;
11250 case ERROR_MARK:
11251 if (TREE_CODE (pmop[which]) != INTEGER_CST)
11252 break;
11253 /* If C or D is a N where (N & M) == 0, it can be
11254 omitted (replaced with 0). */
11255 if ((code == PLUS_EXPR
11256 || (code == MINUS_EXPR && which == 0))
11257 && (cst1 & wi::to_wide (pmop[which])) == 0)
11258 pmop[which] = build_int_cst (type, 0);
11259 /* Similarly, with C - N where (-N & M) == 0. */
11260 if (code == MINUS_EXPR
11261 && which == 1
11262 && (cst1 & -wi::to_wide (pmop[which])) == 0)
11263 pmop[which] = build_int_cst (type, 0);
11264 break;
11265 default:
11266 gcc_unreachable ();
11269 /* Only build anything new if we optimized one or both arguments above. */
11270 if (pmop[0] == arg00 && pmop[1] == arg01)
11271 return NULL_TREE;
11273 if (TYPE_OVERFLOW_WRAPS (type))
11274 return type;
11275 else
11276 return unsigned_type_for (type);
11279 /* Used by contains_label_[p1]. */
11281 struct contains_label_data
11283 hash_set<tree> *pset;
11284 bool inside_switch_p;
11287 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11288 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
11289 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
11291 static tree
11292 contains_label_1 (tree *tp, int *walk_subtrees, void *data)
11294 contains_label_data *d = (contains_label_data *) data;
11295 switch (TREE_CODE (*tp))
11297 case LABEL_EXPR:
11298 return *tp;
11300 case CASE_LABEL_EXPR:
11301 if (!d->inside_switch_p)
11302 return *tp;
11303 return NULL_TREE;
11305 case SWITCH_EXPR:
11306 if (!d->inside_switch_p)
11308 if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
11309 return *tp;
11310 d->inside_switch_p = true;
11311 if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
11312 return *tp;
11313 d->inside_switch_p = false;
11314 *walk_subtrees = 0;
11316 return NULL_TREE;
11318 case GOTO_EXPR:
11319 *walk_subtrees = 0;
11320 return NULL_TREE;
11322 default:
11323 return NULL_TREE;
11327 /* Return whether the sub-tree ST contains a label which is accessible from
11328 outside the sub-tree. */
11330 static bool
11331 contains_label_p (tree st)
11333 hash_set<tree> pset;
11334 contains_label_data data = { &pset, false };
11335 return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
11338 /* Fold a ternary expression of code CODE and type TYPE with operands
11339 OP0, OP1, and OP2. Return the folded expression if folding is
11340 successful. Otherwise, return NULL_TREE. */
11342 tree
11343 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11344 tree op0, tree op1, tree op2)
11346 tree tem;
11347 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11348 enum tree_code_class kind = TREE_CODE_CLASS (code);
11350 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11351 && TREE_CODE_LENGTH (code) == 3);
11353 /* If this is a commutative operation, and OP0 is a constant, move it
11354 to OP1 to reduce the number of tests below. */
11355 if (commutative_ternary_tree_code (code)
11356 && tree_swap_operands_p (op0, op1))
11357 return fold_build3_loc (loc, code, type, op1, op0, op2);
11359 tem = generic_simplify (loc, code, type, op0, op1, op2);
11360 if (tem)
11361 return tem;
11363 /* Strip any conversions that don't change the mode. This is safe
11364 for every expression, except for a comparison expression because
11365 its signedness is derived from its operands. So, in the latter
11366 case, only strip conversions that don't change the signedness.
11368 Note that this is done as an internal manipulation within the
11369 constant folder, in order to find the simplest representation of
11370 the arguments so that their form can be studied. In any cases,
11371 the appropriate type conversions should be put back in the tree
11372 that will get out of the constant folder. */
11373 if (op0)
11375 arg0 = op0;
11376 STRIP_NOPS (arg0);
11379 if (op1)
11381 arg1 = op1;
11382 STRIP_NOPS (arg1);
11385 if (op2)
11387 arg2 = op2;
11388 STRIP_NOPS (arg2);
11391 switch (code)
11393 case COMPONENT_REF:
11394 if (TREE_CODE (arg0) == CONSTRUCTOR
11395 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11397 unsigned HOST_WIDE_INT idx;
11398 tree field, value;
11399 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11400 if (field == arg1)
11401 return value;
11403 return NULL_TREE;
11405 case COND_EXPR:
11406 case VEC_COND_EXPR:
11407 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11408 so all simple results must be passed through pedantic_non_lvalue. */
11409 if (TREE_CODE (arg0) == INTEGER_CST)
11411 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11412 tem = integer_zerop (arg0) ? op2 : op1;
11413 /* Only optimize constant conditions when the selected branch
11414 has the same type as the COND_EXPR. This avoids optimizing
11415 away "c ? x : throw", where the throw has a void type.
11416 Avoid throwing away that operand which contains label. */
11417 if ((!TREE_SIDE_EFFECTS (unused_op)
11418 || !contains_label_p (unused_op))
11419 && (! VOID_TYPE_P (TREE_TYPE (tem))
11420 || VOID_TYPE_P (type)))
11421 return pedantic_non_lvalue_loc (loc, tem);
11422 return NULL_TREE;
11424 else if (TREE_CODE (arg0) == VECTOR_CST)
11426 unsigned HOST_WIDE_INT nelts;
11427 if ((TREE_CODE (arg1) == VECTOR_CST
11428 || TREE_CODE (arg1) == CONSTRUCTOR)
11429 && (TREE_CODE (arg2) == VECTOR_CST
11430 || TREE_CODE (arg2) == CONSTRUCTOR)
11431 && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
11433 vec_perm_builder sel (nelts, nelts, 1);
11434 for (unsigned int i = 0; i < nelts; i++)
11436 tree val = VECTOR_CST_ELT (arg0, i);
11437 if (integer_all_onesp (val))
11438 sel.quick_push (i);
11439 else if (integer_zerop (val))
11440 sel.quick_push (nelts + i);
11441 else /* Currently unreachable. */
11442 return NULL_TREE;
11444 vec_perm_indices indices (sel, 2, nelts);
11445 tree t = fold_vec_perm (type, arg1, arg2, indices);
11446 if (t != NULL_TREE)
11447 return t;
11451 /* If we have A op B ? A : C, we may be able to convert this to a
11452 simpler expression, depending on the operation and the values
11453 of B and C. Signed zeros prevent all of these transformations,
11454 for reasons given above each one.
11456 Also try swapping the arguments and inverting the conditional. */
11457 if (COMPARISON_CLASS_P (arg0)
11458 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
11459 && !HONOR_SIGNED_ZEROS (element_mode (op1)))
11461 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11462 if (tem)
11463 return tem;
11466 if (COMPARISON_CLASS_P (arg0)
11467 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11468 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11470 location_t loc0 = expr_location_or (arg0, loc);
11471 tem = fold_invert_truthvalue (loc0, arg0);
11472 if (tem && COMPARISON_CLASS_P (tem))
11474 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11475 if (tem)
11476 return tem;
11480 /* If the second operand is simpler than the third, swap them
11481 since that produces better jump optimization results. */
11482 if (truth_value_p (TREE_CODE (arg0))
11483 && tree_swap_operands_p (op1, op2))
11485 location_t loc0 = expr_location_or (arg0, loc);
11486 /* See if this can be inverted. If it can't, possibly because
11487 it was a floating-point inequality comparison, don't do
11488 anything. */
11489 tem = fold_invert_truthvalue (loc0, arg0);
11490 if (tem)
11491 return fold_build3_loc (loc, code, type, tem, op2, op1);
11494 /* Convert A ? 1 : 0 to simply A. */
11495 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11496 : (integer_onep (op1)
11497 && !VECTOR_TYPE_P (type)))
11498 && integer_zerop (op2)
11499 /* If we try to convert OP0 to our type, the
11500 call to fold will try to move the conversion inside
11501 a COND, which will recurse. In that case, the COND_EXPR
11502 is probably the best choice, so leave it alone. */
11503 && type == TREE_TYPE (arg0))
11504 return pedantic_non_lvalue_loc (loc, arg0);
11506 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11507 over COND_EXPR in cases such as floating point comparisons. */
11508 if (integer_zerop (op1)
11509 && code == COND_EXPR
11510 && integer_onep (op2)
11511 && !VECTOR_TYPE_P (type)
11512 && truth_value_p (TREE_CODE (arg0)))
11513 return pedantic_non_lvalue_loc (loc,
11514 fold_convert_loc (loc, type,
11515 invert_truthvalue_loc (loc,
11516 arg0)));
11518 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11519 if (TREE_CODE (arg0) == LT_EXPR
11520 && integer_zerop (TREE_OPERAND (arg0, 1))
11521 && integer_zerop (op2)
11522 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11524 /* sign_bit_p looks through both zero and sign extensions,
11525 but for this optimization only sign extensions are
11526 usable. */
11527 tree tem2 = TREE_OPERAND (arg0, 0);
11528 while (tem != tem2)
11530 if (TREE_CODE (tem2) != NOP_EXPR
11531 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11533 tem = NULL_TREE;
11534 break;
11536 tem2 = TREE_OPERAND (tem2, 0);
11538 /* sign_bit_p only checks ARG1 bits within A's precision.
11539 If <sign bit of A> has wider type than A, bits outside
11540 of A's precision in <sign bit of A> need to be checked.
11541 If they are all 0, this optimization needs to be done
11542 in unsigned A's type, if they are all 1 in signed A's type,
11543 otherwise this can't be done. */
11544 if (tem
11545 && TYPE_PRECISION (TREE_TYPE (tem))
11546 < TYPE_PRECISION (TREE_TYPE (arg1))
11547 && TYPE_PRECISION (TREE_TYPE (tem))
11548 < TYPE_PRECISION (type))
11550 int inner_width, outer_width;
11551 tree tem_type;
11553 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11554 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11555 if (outer_width > TYPE_PRECISION (type))
11556 outer_width = TYPE_PRECISION (type);
11558 wide_int mask = wi::shifted_mask
11559 (inner_width, outer_width - inner_width, false,
11560 TYPE_PRECISION (TREE_TYPE (arg1)));
11562 wide_int common = mask & wi::to_wide (arg1);
11563 if (common == mask)
11565 tem_type = signed_type_for (TREE_TYPE (tem));
11566 tem = fold_convert_loc (loc, tem_type, tem);
11568 else if (common == 0)
11570 tem_type = unsigned_type_for (TREE_TYPE (tem));
11571 tem = fold_convert_loc (loc, tem_type, tem);
11573 else
11574 tem = NULL;
11577 if (tem)
11578 return
11579 fold_convert_loc (loc, type,
11580 fold_build2_loc (loc, BIT_AND_EXPR,
11581 TREE_TYPE (tem), tem,
11582 fold_convert_loc (loc,
11583 TREE_TYPE (tem),
11584 arg1)));
11587 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11588 already handled above. */
11589 if (TREE_CODE (arg0) == BIT_AND_EXPR
11590 && integer_onep (TREE_OPERAND (arg0, 1))
11591 && integer_zerop (op2)
11592 && integer_pow2p (arg1))
11594 tree tem = TREE_OPERAND (arg0, 0);
11595 STRIP_NOPS (tem);
11596 if (TREE_CODE (tem) == RSHIFT_EXPR
11597 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11598 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11599 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11600 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11601 fold_convert_loc (loc, type,
11602 TREE_OPERAND (tem, 0)),
11603 op1);
11606 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11607 is probably obsolete because the first operand should be a
11608 truth value (that's why we have the two cases above), but let's
11609 leave it in until we can confirm this for all front-ends. */
11610 if (integer_zerop (op2)
11611 && TREE_CODE (arg0) == NE_EXPR
11612 && integer_zerop (TREE_OPERAND (arg0, 1))
11613 && integer_pow2p (arg1)
11614 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11615 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11616 arg1, OEP_ONLY_CONST)
11617 /* operand_equal_p compares just value, not precision, so e.g.
11618 arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
11619 second operand 32-bit -128, which is not a power of two (or vice
11620 versa. */
11621 && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
11622 return pedantic_non_lvalue_loc (loc,
11623 fold_convert_loc (loc, type,
11624 TREE_OPERAND (arg0,
11625 0)));
11627 /* Disable the transformations below for vectors, since
11628 fold_binary_op_with_conditional_arg may undo them immediately,
11629 yielding an infinite loop. */
11630 if (code == VEC_COND_EXPR)
11631 return NULL_TREE;
11633 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11634 if (integer_zerop (op2)
11635 && truth_value_p (TREE_CODE (arg0))
11636 && truth_value_p (TREE_CODE (arg1))
11637 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11638 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11639 : TRUTH_ANDIF_EXPR,
11640 type, fold_convert_loc (loc, type, arg0), op1);
11642 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11643 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11644 && truth_value_p (TREE_CODE (arg0))
11645 && truth_value_p (TREE_CODE (arg1))
11646 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11648 location_t loc0 = expr_location_or (arg0, loc);
11649 /* Only perform transformation if ARG0 is easily inverted. */
11650 tem = fold_invert_truthvalue (loc0, arg0);
11651 if (tem)
11652 return fold_build2_loc (loc, code == VEC_COND_EXPR
11653 ? BIT_IOR_EXPR
11654 : TRUTH_ORIF_EXPR,
11655 type, fold_convert_loc (loc, type, tem),
11656 op1);
11659 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11660 if (integer_zerop (arg1)
11661 && truth_value_p (TREE_CODE (arg0))
11662 && truth_value_p (TREE_CODE (op2))
11663 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11665 location_t loc0 = expr_location_or (arg0, loc);
11666 /* Only perform transformation if ARG0 is easily inverted. */
11667 tem = fold_invert_truthvalue (loc0, arg0);
11668 if (tem)
11669 return fold_build2_loc (loc, code == VEC_COND_EXPR
11670 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11671 type, fold_convert_loc (loc, type, tem),
11672 op2);
11675 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11676 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11677 && truth_value_p (TREE_CODE (arg0))
11678 && truth_value_p (TREE_CODE (op2))
11679 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11680 return fold_build2_loc (loc, code == VEC_COND_EXPR
11681 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11682 type, fold_convert_loc (loc, type, arg0), op2);
11684 return NULL_TREE;
11686 case CALL_EXPR:
11687 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11688 of fold_ternary on them. */
11689 gcc_unreachable ();
11691 case BIT_FIELD_REF:
11692 if (TREE_CODE (arg0) == VECTOR_CST
11693 && (type == TREE_TYPE (TREE_TYPE (arg0))
11694 || (VECTOR_TYPE_P (type)
11695 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
11696 && tree_fits_uhwi_p (op1)
11697 && tree_fits_uhwi_p (op2))
11699 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11700 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11701 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11702 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11704 if (n != 0
11705 && (idx % width) == 0
11706 && (n % width) == 0
11707 && known_le ((idx + n) / width,
11708 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
11710 idx = idx / width;
11711 n = n / width;
11713 if (TREE_CODE (arg0) == VECTOR_CST)
11715 if (n == 1)
11717 tem = VECTOR_CST_ELT (arg0, idx);
11718 if (VECTOR_TYPE_P (type))
11719 tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
11720 return tem;
11723 tree_vector_builder vals (type, n, 1);
11724 for (unsigned i = 0; i < n; ++i)
11725 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
11726 return vals.build ();
11731 /* On constants we can use native encode/interpret to constant
11732 fold (nearly) all BIT_FIELD_REFs. */
11733 if (CONSTANT_CLASS_P (arg0)
11734 && can_native_interpret_type_p (type)
11735 && BITS_PER_UNIT == 8
11736 && tree_fits_uhwi_p (op1)
11737 && tree_fits_uhwi_p (op2))
11739 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11740 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11741 /* Limit us to a reasonable amount of work. To relax the
11742 other limitations we need bit-shifting of the buffer
11743 and rounding up the size. */
11744 if (bitpos % BITS_PER_UNIT == 0
11745 && bitsize % BITS_PER_UNIT == 0
11746 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11748 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11749 unsigned HOST_WIDE_INT len
11750 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11751 bitpos / BITS_PER_UNIT);
11752 if (len > 0
11753 && len * BITS_PER_UNIT >= bitsize)
11755 tree v = native_interpret_expr (type, b,
11756 bitsize / BITS_PER_UNIT);
11757 if (v)
11758 return v;
11763 return NULL_TREE;
11765 case VEC_PERM_EXPR:
11766 if (TREE_CODE (arg2) == VECTOR_CST)
11768 /* Build a vector of integers from the tree mask. */
11769 vec_perm_builder builder;
11770 if (!tree_to_vec_perm_builder (&builder, arg2))
11771 return NULL_TREE;
11773 /* Create a vec_perm_indices for the integer vector. */
11774 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
11775 bool single_arg = (op0 == op1);
11776 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
11778 /* Check for cases that fold to OP0 or OP1 in their original
11779 element order. */
11780 if (sel.series_p (0, 1, 0, 1))
11781 return op0;
11782 if (sel.series_p (0, 1, nelts, 1))
11783 return op1;
11785 if (!single_arg)
11787 if (sel.all_from_input_p (0))
11788 op1 = op0;
11789 else if (sel.all_from_input_p (1))
11791 op0 = op1;
11792 sel.rotate_inputs (1);
11796 if ((TREE_CODE (op0) == VECTOR_CST
11797 || TREE_CODE (op0) == CONSTRUCTOR)
11798 && (TREE_CODE (op1) == VECTOR_CST
11799 || TREE_CODE (op1) == CONSTRUCTOR))
11801 tree t = fold_vec_perm (type, op0, op1, sel);
11802 if (t != NULL_TREE)
11803 return t;
11806 bool changed = (op0 == op1 && !single_arg);
11808 /* Generate a canonical form of the selector. */
11809 if (arg2 == op2 && sel.encoding () != builder)
11811 /* Some targets are deficient and fail to expand a single
11812 argument permutation while still allowing an equivalent
11813 2-argument version. */
11814 if (sel.ninputs () == 2
11815 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
11816 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11817 else
11819 vec_perm_indices sel2 (builder, 2, nelts);
11820 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
11821 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel2);
11822 else
11823 /* Not directly supported with either encoding,
11824 so use the preferred form. */
11825 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11827 changed = true;
11830 if (changed)
11831 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11833 return NULL_TREE;
11835 case BIT_INSERT_EXPR:
11836 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11837 if (TREE_CODE (arg0) == INTEGER_CST
11838 && TREE_CODE (arg1) == INTEGER_CST)
11840 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11841 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11842 wide_int tem = (wi::to_wide (arg0)
11843 & wi::shifted_mask (bitpos, bitsize, true,
11844 TYPE_PRECISION (type)));
11845 wide_int tem2
11846 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11847 bitsize), bitpos);
11848 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11850 else if (TREE_CODE (arg0) == VECTOR_CST
11851 && CONSTANT_CLASS_P (arg1)
11852 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11853 TREE_TYPE (arg1)))
11855 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11856 unsigned HOST_WIDE_INT elsize
11857 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11858 if (bitpos % elsize == 0)
11860 unsigned k = bitpos / elsize;
11861 unsigned HOST_WIDE_INT nelts;
11862 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11863 return arg0;
11864 else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
11866 tree_vector_builder elts (type, nelts, 1);
11867 elts.quick_grow (nelts);
11868 for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
11869 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
11870 return elts.build ();
11874 return NULL_TREE;
11876 default:
11877 return NULL_TREE;
11878 } /* switch (code) */
11881 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11882 of an array (or vector). */
11884 tree
11885 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11887 tree index_type = NULL_TREE;
11888 offset_int low_bound = 0;
11890 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11892 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11893 if (domain_type && TYPE_MIN_VALUE (domain_type))
11895 /* Static constructors for variably sized objects makes no sense. */
11896 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11897 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11898 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11902 if (index_type)
11903 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11904 TYPE_SIGN (index_type));
11906 offset_int index = low_bound - 1;
11907 if (index_type)
11908 index = wi::ext (index, TYPE_PRECISION (index_type),
11909 TYPE_SIGN (index_type));
11911 offset_int max_index;
11912 unsigned HOST_WIDE_INT cnt;
11913 tree cfield, cval;
11915 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11917 /* Array constructor might explicitly set index, or specify a range,
11918 or leave index NULL meaning that it is next index after previous
11919 one. */
11920 if (cfield)
11922 if (TREE_CODE (cfield) == INTEGER_CST)
11923 max_index = index = wi::to_offset (cfield);
11924 else
11926 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11927 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11928 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11931 else
11933 index += 1;
11934 if (index_type)
11935 index = wi::ext (index, TYPE_PRECISION (index_type),
11936 TYPE_SIGN (index_type));
11937 max_index = index;
11940 /* Do we have match? */
11941 if (wi::cmpu (access_index, index) >= 0
11942 && wi::cmpu (access_index, max_index) <= 0)
11943 return cval;
11945 return NULL_TREE;
11948 /* Perform constant folding and related simplification of EXPR.
11949 The related simplifications include x*1 => x, x*0 => 0, etc.,
11950 and application of the associative law.
11951 NOP_EXPR conversions may be removed freely (as long as we
11952 are careful not to change the type of the overall expression).
11953 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11954 but we can constant-fold them if they have constant operands. */
11956 #ifdef ENABLE_FOLD_CHECKING
11957 # define fold(x) fold_1 (x)
11958 static tree fold_1 (tree);
11959 static
11960 #endif
11961 tree
11962 fold (tree expr)
11964 const tree t = expr;
11965 enum tree_code code = TREE_CODE (t);
11966 enum tree_code_class kind = TREE_CODE_CLASS (code);
11967 tree tem;
11968 location_t loc = EXPR_LOCATION (expr);
11970 /* Return right away if a constant. */
11971 if (kind == tcc_constant)
11972 return t;
11974 /* CALL_EXPR-like objects with variable numbers of operands are
11975 treated specially. */
11976 if (kind == tcc_vl_exp)
11978 if (code == CALL_EXPR)
11980 tem = fold_call_expr (loc, expr, false);
11981 return tem ? tem : expr;
11983 return expr;
11986 if (IS_EXPR_CODE_CLASS (kind))
11988 tree type = TREE_TYPE (t);
11989 tree op0, op1, op2;
11991 switch (TREE_CODE_LENGTH (code))
11993 case 1:
11994 op0 = TREE_OPERAND (t, 0);
11995 tem = fold_unary_loc (loc, code, type, op0);
11996 return tem ? tem : expr;
11997 case 2:
11998 op0 = TREE_OPERAND (t, 0);
11999 op1 = TREE_OPERAND (t, 1);
12000 tem = fold_binary_loc (loc, code, type, op0, op1);
12001 return tem ? tem : expr;
12002 case 3:
12003 op0 = TREE_OPERAND (t, 0);
12004 op1 = TREE_OPERAND (t, 1);
12005 op2 = TREE_OPERAND (t, 2);
12006 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12007 return tem ? tem : expr;
12008 default:
12009 break;
12013 switch (code)
12015 case ARRAY_REF:
12017 tree op0 = TREE_OPERAND (t, 0);
12018 tree op1 = TREE_OPERAND (t, 1);
12020 if (TREE_CODE (op1) == INTEGER_CST
12021 && TREE_CODE (op0) == CONSTRUCTOR
12022 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
12024 tree val = get_array_ctor_element_at_index (op0,
12025 wi::to_offset (op1));
12026 if (val)
12027 return val;
12030 return t;
12033 /* Return a VECTOR_CST if possible. */
12034 case CONSTRUCTOR:
12036 tree type = TREE_TYPE (t);
12037 if (TREE_CODE (type) != VECTOR_TYPE)
12038 return t;
12040 unsigned i;
12041 tree val;
12042 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
12043 if (! CONSTANT_CLASS_P (val))
12044 return t;
12046 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12049 case CONST_DECL:
12050 return fold (DECL_INITIAL (t));
12052 default:
12053 return t;
12054 } /* switch (code) */
12057 #ifdef ENABLE_FOLD_CHECKING
12058 #undef fold
12060 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12061 hash_table<nofree_ptr_hash<const tree_node> > *);
12062 static void fold_check_failed (const_tree, const_tree);
12063 void print_fold_checksum (const_tree);
12065 /* When --enable-checking=fold, compute a digest of expr before
12066 and after actual fold call to see if fold did not accidentally
12067 change original expr. */
12069 tree
12070 fold (tree expr)
12072 tree ret;
12073 struct md5_ctx ctx;
12074 unsigned char checksum_before[16], checksum_after[16];
12075 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12077 md5_init_ctx (&ctx);
12078 fold_checksum_tree (expr, &ctx, &ht);
12079 md5_finish_ctx (&ctx, checksum_before);
12080 ht.empty ();
12082 ret = fold_1 (expr);
12084 md5_init_ctx (&ctx);
12085 fold_checksum_tree (expr, &ctx, &ht);
12086 md5_finish_ctx (&ctx, checksum_after);
12088 if (memcmp (checksum_before, checksum_after, 16))
12089 fold_check_failed (expr, ret);
12091 return ret;
12094 void
12095 print_fold_checksum (const_tree expr)
12097 struct md5_ctx ctx;
12098 unsigned char checksum[16], cnt;
12099 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12101 md5_init_ctx (&ctx);
12102 fold_checksum_tree (expr, &ctx, &ht);
12103 md5_finish_ctx (&ctx, checksum);
12104 for (cnt = 0; cnt < 16; ++cnt)
12105 fprintf (stderr, "%02x", checksum[cnt]);
12106 putc ('\n', stderr);
12109 static void
12110 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12112 internal_error ("fold check: original tree changed by fold");
12115 static void
12116 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12117 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12119 const tree_node **slot;
12120 enum tree_code code;
12121 union tree_node *buf;
12122 int i, len;
12124 recursive_label:
12125 if (expr == NULL)
12126 return;
12127 slot = ht->find_slot (expr, INSERT);
12128 if (*slot != NULL)
12129 return;
12130 *slot = expr;
12131 code = TREE_CODE (expr);
12132 if (TREE_CODE_CLASS (code) == tcc_declaration
12133 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12135 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12136 size_t sz = tree_size (expr);
12137 buf = XALLOCAVAR (union tree_node, sz);
12138 memcpy ((char *) buf, expr, sz);
12139 SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
12140 buf->decl_with_vis.symtab_node = NULL;
12141 buf->base.nowarning_flag = 0;
12142 expr = (tree) buf;
12144 else if (TREE_CODE_CLASS (code) == tcc_type
12145 && (TYPE_POINTER_TO (expr)
12146 || TYPE_REFERENCE_TO (expr)
12147 || TYPE_CACHED_VALUES_P (expr)
12148 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12149 || TYPE_NEXT_VARIANT (expr)
12150 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12152 /* Allow these fields to be modified. */
12153 tree tmp;
12154 size_t sz = tree_size (expr);
12155 buf = XALLOCAVAR (union tree_node, sz);
12156 memcpy ((char *) buf, expr, sz);
12157 expr = tmp = (tree) buf;
12158 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12159 TYPE_POINTER_TO (tmp) = NULL;
12160 TYPE_REFERENCE_TO (tmp) = NULL;
12161 TYPE_NEXT_VARIANT (tmp) = NULL;
12162 TYPE_ALIAS_SET (tmp) = -1;
12163 if (TYPE_CACHED_VALUES_P (tmp))
12165 TYPE_CACHED_VALUES_P (tmp) = 0;
12166 TYPE_CACHED_VALUES (tmp) = NULL;
12169 else if (TREE_NO_WARNING (expr) && (DECL_P (expr) || EXPR_P (expr)))
12171 /* Allow TREE_NO_WARNING to be set. Perhaps we shouldn't allow that
12172 and change builtins.c etc. instead - see PR89543. */
12173 size_t sz = tree_size (expr);
12174 buf = XALLOCAVAR (union tree_node, sz);
12175 memcpy ((char *) buf, expr, sz);
12176 buf->base.nowarning_flag = 0;
12177 expr = (tree) buf;
12179 md5_process_bytes (expr, tree_size (expr), ctx);
12180 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12181 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12182 if (TREE_CODE_CLASS (code) != tcc_type
12183 && TREE_CODE_CLASS (code) != tcc_declaration
12184 && code != TREE_LIST
12185 && code != SSA_NAME
12186 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12187 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12188 switch (TREE_CODE_CLASS (code))
12190 case tcc_constant:
12191 switch (code)
12193 case STRING_CST:
12194 md5_process_bytes (TREE_STRING_POINTER (expr),
12195 TREE_STRING_LENGTH (expr), ctx);
12196 break;
12197 case COMPLEX_CST:
12198 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12199 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12200 break;
12201 case VECTOR_CST:
12202 len = vector_cst_encoded_nelts (expr);
12203 for (i = 0; i < len; ++i)
12204 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
12205 break;
12206 default:
12207 break;
12209 break;
12210 case tcc_exceptional:
12211 switch (code)
12213 case TREE_LIST:
12214 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12215 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12216 expr = TREE_CHAIN (expr);
12217 goto recursive_label;
12218 break;
12219 case TREE_VEC:
12220 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12221 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12222 break;
12223 default:
12224 break;
12226 break;
12227 case tcc_expression:
12228 case tcc_reference:
12229 case tcc_comparison:
12230 case tcc_unary:
12231 case tcc_binary:
12232 case tcc_statement:
12233 case tcc_vl_exp:
12234 len = TREE_OPERAND_LENGTH (expr);
12235 for (i = 0; i < len; ++i)
12236 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12237 break;
12238 case tcc_declaration:
12239 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12240 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12241 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12243 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12244 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12245 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12246 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12247 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12250 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12252 if (TREE_CODE (expr) == FUNCTION_DECL)
12254 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12255 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12257 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12259 break;
12260 case tcc_type:
12261 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12262 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12263 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12264 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12265 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12266 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12267 if (INTEGRAL_TYPE_P (expr)
12268 || SCALAR_FLOAT_TYPE_P (expr))
12270 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12271 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12273 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12274 if (TREE_CODE (expr) == RECORD_TYPE
12275 || TREE_CODE (expr) == UNION_TYPE
12276 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12277 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12278 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12279 break;
12280 default:
12281 break;
12285 /* Helper function for outputting the checksum of a tree T. When
12286 debugging with gdb, you can "define mynext" to be "next" followed
12287 by "call debug_fold_checksum (op0)", then just trace down till the
12288 outputs differ. */
12290 DEBUG_FUNCTION void
12291 debug_fold_checksum (const_tree t)
12293 int i;
12294 unsigned char checksum[16];
12295 struct md5_ctx ctx;
12296 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12298 md5_init_ctx (&ctx);
12299 fold_checksum_tree (t, &ctx, &ht);
12300 md5_finish_ctx (&ctx, checksum);
12301 ht.empty ();
12303 for (i = 0; i < 16; i++)
12304 fprintf (stderr, "%d ", checksum[i]);
12306 fprintf (stderr, "\n");
12309 #endif
12311 /* Fold a unary tree expression with code CODE of type TYPE with an
12312 operand OP0. LOC is the location of the resulting expression.
12313 Return a folded expression if successful. Otherwise, return a tree
12314 expression with code CODE of type TYPE with an operand OP0. */
12316 tree
12317 fold_build1_loc (location_t loc,
12318 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12320 tree tem;
12321 #ifdef ENABLE_FOLD_CHECKING
12322 unsigned char checksum_before[16], checksum_after[16];
12323 struct md5_ctx ctx;
12324 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12326 md5_init_ctx (&ctx);
12327 fold_checksum_tree (op0, &ctx, &ht);
12328 md5_finish_ctx (&ctx, checksum_before);
12329 ht.empty ();
12330 #endif
12332 tem = fold_unary_loc (loc, code, type, op0);
12333 if (!tem)
12334 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12336 #ifdef ENABLE_FOLD_CHECKING
12337 md5_init_ctx (&ctx);
12338 fold_checksum_tree (op0, &ctx, &ht);
12339 md5_finish_ctx (&ctx, checksum_after);
12341 if (memcmp (checksum_before, checksum_after, 16))
12342 fold_check_failed (op0, tem);
12343 #endif
12344 return tem;
12347 /* Fold a binary tree expression with code CODE of type TYPE with
12348 operands OP0 and OP1. LOC is the location of the resulting
12349 expression. Return a folded expression if successful. Otherwise,
12350 return a tree expression with code CODE of type TYPE with operands
12351 OP0 and OP1. */
12353 tree
12354 fold_build2_loc (location_t loc,
12355 enum tree_code code, tree type, tree op0, tree op1
12356 MEM_STAT_DECL)
12358 tree tem;
12359 #ifdef ENABLE_FOLD_CHECKING
12360 unsigned char checksum_before_op0[16],
12361 checksum_before_op1[16],
12362 checksum_after_op0[16],
12363 checksum_after_op1[16];
12364 struct md5_ctx ctx;
12365 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12367 md5_init_ctx (&ctx);
12368 fold_checksum_tree (op0, &ctx, &ht);
12369 md5_finish_ctx (&ctx, checksum_before_op0);
12370 ht.empty ();
12372 md5_init_ctx (&ctx);
12373 fold_checksum_tree (op1, &ctx, &ht);
12374 md5_finish_ctx (&ctx, checksum_before_op1);
12375 ht.empty ();
12376 #endif
12378 tem = fold_binary_loc (loc, code, type, op0, op1);
12379 if (!tem)
12380 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12382 #ifdef ENABLE_FOLD_CHECKING
12383 md5_init_ctx (&ctx);
12384 fold_checksum_tree (op0, &ctx, &ht);
12385 md5_finish_ctx (&ctx, checksum_after_op0);
12386 ht.empty ();
12388 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12389 fold_check_failed (op0, tem);
12391 md5_init_ctx (&ctx);
12392 fold_checksum_tree (op1, &ctx, &ht);
12393 md5_finish_ctx (&ctx, checksum_after_op1);
12395 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12396 fold_check_failed (op1, tem);
12397 #endif
12398 return tem;
12401 /* Fold a ternary tree expression with code CODE of type TYPE with
12402 operands OP0, OP1, and OP2. Return a folded expression if
12403 successful. Otherwise, return a tree expression with code CODE of
12404 type TYPE with operands OP0, OP1, and OP2. */
12406 tree
12407 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12408 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12410 tree tem;
12411 #ifdef ENABLE_FOLD_CHECKING
12412 unsigned char checksum_before_op0[16],
12413 checksum_before_op1[16],
12414 checksum_before_op2[16],
12415 checksum_after_op0[16],
12416 checksum_after_op1[16],
12417 checksum_after_op2[16];
12418 struct md5_ctx ctx;
12419 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12421 md5_init_ctx (&ctx);
12422 fold_checksum_tree (op0, &ctx, &ht);
12423 md5_finish_ctx (&ctx, checksum_before_op0);
12424 ht.empty ();
12426 md5_init_ctx (&ctx);
12427 fold_checksum_tree (op1, &ctx, &ht);
12428 md5_finish_ctx (&ctx, checksum_before_op1);
12429 ht.empty ();
12431 md5_init_ctx (&ctx);
12432 fold_checksum_tree (op2, &ctx, &ht);
12433 md5_finish_ctx (&ctx, checksum_before_op2);
12434 ht.empty ();
12435 #endif
12437 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12438 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12439 if (!tem)
12440 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12442 #ifdef ENABLE_FOLD_CHECKING
12443 md5_init_ctx (&ctx);
12444 fold_checksum_tree (op0, &ctx, &ht);
12445 md5_finish_ctx (&ctx, checksum_after_op0);
12446 ht.empty ();
12448 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12449 fold_check_failed (op0, tem);
12451 md5_init_ctx (&ctx);
12452 fold_checksum_tree (op1, &ctx, &ht);
12453 md5_finish_ctx (&ctx, checksum_after_op1);
12454 ht.empty ();
12456 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12457 fold_check_failed (op1, tem);
12459 md5_init_ctx (&ctx);
12460 fold_checksum_tree (op2, &ctx, &ht);
12461 md5_finish_ctx (&ctx, checksum_after_op2);
12463 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12464 fold_check_failed (op2, tem);
12465 #endif
12466 return tem;
12469 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12470 arguments in ARGARRAY, and a null static chain.
12471 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12472 of type TYPE from the given operands as constructed by build_call_array. */
12474 tree
12475 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12476 int nargs, tree *argarray)
12478 tree tem;
12479 #ifdef ENABLE_FOLD_CHECKING
12480 unsigned char checksum_before_fn[16],
12481 checksum_before_arglist[16],
12482 checksum_after_fn[16],
12483 checksum_after_arglist[16];
12484 struct md5_ctx ctx;
12485 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12486 int i;
12488 md5_init_ctx (&ctx);
12489 fold_checksum_tree (fn, &ctx, &ht);
12490 md5_finish_ctx (&ctx, checksum_before_fn);
12491 ht.empty ();
12493 md5_init_ctx (&ctx);
12494 for (i = 0; i < nargs; i++)
12495 fold_checksum_tree (argarray[i], &ctx, &ht);
12496 md5_finish_ctx (&ctx, checksum_before_arglist);
12497 ht.empty ();
12498 #endif
12500 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12501 if (!tem)
12502 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12504 #ifdef ENABLE_FOLD_CHECKING
12505 md5_init_ctx (&ctx);
12506 fold_checksum_tree (fn, &ctx, &ht);
12507 md5_finish_ctx (&ctx, checksum_after_fn);
12508 ht.empty ();
12510 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12511 fold_check_failed (fn, tem);
12513 md5_init_ctx (&ctx);
12514 for (i = 0; i < nargs; i++)
12515 fold_checksum_tree (argarray[i], &ctx, &ht);
12516 md5_finish_ctx (&ctx, checksum_after_arglist);
12518 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12519 fold_check_failed (NULL_TREE, tem);
12520 #endif
12521 return tem;
12524 /* Perform constant folding and related simplification of initializer
12525 expression EXPR. These behave identically to "fold_buildN" but ignore
12526 potential run-time traps and exceptions that fold must preserve. */
12528 #define START_FOLD_INIT \
12529 int saved_signaling_nans = flag_signaling_nans;\
12530 int saved_trapping_math = flag_trapping_math;\
12531 int saved_rounding_math = flag_rounding_math;\
12532 int saved_trapv = flag_trapv;\
12533 int saved_folding_initializer = folding_initializer;\
12534 flag_signaling_nans = 0;\
12535 flag_trapping_math = 0;\
12536 flag_rounding_math = 0;\
12537 flag_trapv = 0;\
12538 folding_initializer = 1;
12540 #define END_FOLD_INIT \
12541 flag_signaling_nans = saved_signaling_nans;\
12542 flag_trapping_math = saved_trapping_math;\
12543 flag_rounding_math = saved_rounding_math;\
12544 flag_trapv = saved_trapv;\
12545 folding_initializer = saved_folding_initializer;
12547 tree
12548 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12549 tree type, tree op)
12551 tree result;
12552 START_FOLD_INIT;
12554 result = fold_build1_loc (loc, code, type, op);
12556 END_FOLD_INIT;
12557 return result;
12560 tree
12561 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12562 tree type, tree op0, tree op1)
12564 tree result;
12565 START_FOLD_INIT;
12567 result = fold_build2_loc (loc, code, type, op0, op1);
12569 END_FOLD_INIT;
12570 return result;
12573 tree
12574 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12575 int nargs, tree *argarray)
12577 tree result;
12578 START_FOLD_INIT;
12580 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12582 END_FOLD_INIT;
12583 return result;
12586 #undef START_FOLD_INIT
12587 #undef END_FOLD_INIT
12589 /* Determine if first argument is a multiple of second argument. Return 0 if
12590 it is not, or we cannot easily determined it to be.
12592 An example of the sort of thing we care about (at this point; this routine
12593 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12594 fold cases do now) is discovering that
12596 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12598 is a multiple of
12600 SAVE_EXPR (J * 8)
12602 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12604 This code also handles discovering that
12606 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12608 is a multiple of 8 so we don't have to worry about dealing with a
12609 possible remainder.
12611 Note that we *look* inside a SAVE_EXPR only to determine how it was
12612 calculated; it is not safe for fold to do much of anything else with the
12613 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12614 at run time. For example, the latter example above *cannot* be implemented
12615 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12616 evaluation time of the original SAVE_EXPR is not necessarily the same at
12617 the time the new expression is evaluated. The only optimization of this
12618 sort that would be valid is changing
12620 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12622 divided by 8 to
12624 SAVE_EXPR (I) * SAVE_EXPR (J)
12626 (where the same SAVE_EXPR (J) is used in the original and the
12627 transformed version). */
12630 multiple_of_p (tree type, const_tree top, const_tree bottom)
12632 gimple *stmt;
12633 tree t1, op1, op2;
12635 if (operand_equal_p (top, bottom, 0))
12636 return 1;
12638 if (TREE_CODE (type) != INTEGER_TYPE)
12639 return 0;
12641 switch (TREE_CODE (top))
12643 case BIT_AND_EXPR:
12644 /* Bitwise and provides a power of two multiple. If the mask is
12645 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12646 if (!integer_pow2p (bottom))
12647 return 0;
12648 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12649 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12651 case MULT_EXPR:
12652 if (TREE_CODE (bottom) == INTEGER_CST)
12654 op1 = TREE_OPERAND (top, 0);
12655 op2 = TREE_OPERAND (top, 1);
12656 if (TREE_CODE (op1) == INTEGER_CST)
12657 std::swap (op1, op2);
12658 if (TREE_CODE (op2) == INTEGER_CST)
12660 if (multiple_of_p (type, op2, bottom))
12661 return 1;
12662 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
12663 if (multiple_of_p (type, bottom, op2))
12665 widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
12666 wi::to_widest (op2));
12667 if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
12669 op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
12670 return multiple_of_p (type, op1, op2);
12673 return multiple_of_p (type, op1, bottom);
12676 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12677 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12679 case MINUS_EXPR:
12680 /* It is impossible to prove if op0 - op1 is multiple of bottom
12681 precisely, so be conservative here checking if both op0 and op1
12682 are multiple of bottom. Note we check the second operand first
12683 since it's usually simpler. */
12684 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12685 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12687 case PLUS_EXPR:
12688 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12689 as op0 - 3 if the expression has unsigned type. For example,
12690 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12691 op1 = TREE_OPERAND (top, 1);
12692 if (TYPE_UNSIGNED (type)
12693 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12694 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12695 return (multiple_of_p (type, op1, bottom)
12696 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12698 case LSHIFT_EXPR:
12699 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12701 op1 = TREE_OPERAND (top, 1);
12702 /* const_binop may not detect overflow correctly,
12703 so check for it explicitly here. */
12704 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
12705 wi::to_wide (op1))
12706 && (t1 = fold_convert (type,
12707 const_binop (LSHIFT_EXPR, size_one_node,
12708 op1))) != 0
12709 && !TREE_OVERFLOW (t1))
12710 return multiple_of_p (type, t1, bottom);
12712 return 0;
12714 case NOP_EXPR:
12715 /* Can't handle conversions from non-integral or wider integral type. */
12716 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12717 || (TYPE_PRECISION (type)
12718 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12719 return 0;
12721 /* fall through */
12723 case SAVE_EXPR:
12724 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12726 case COND_EXPR:
12727 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12728 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12730 case INTEGER_CST:
12731 if (TREE_CODE (bottom) != INTEGER_CST
12732 || integer_zerop (bottom)
12733 || (TYPE_UNSIGNED (type)
12734 && (tree_int_cst_sgn (top) < 0
12735 || tree_int_cst_sgn (bottom) < 0)))
12736 return 0;
12737 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12738 SIGNED);
12740 case SSA_NAME:
12741 if (TREE_CODE (bottom) == INTEGER_CST
12742 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12743 && gimple_code (stmt) == GIMPLE_ASSIGN)
12745 enum tree_code code = gimple_assign_rhs_code (stmt);
12747 /* Check for special cases to see if top is defined as multiple
12748 of bottom:
12750 top = (X & ~(bottom - 1) ; bottom is power of 2
12754 Y = X % bottom
12755 top = X - Y. */
12756 if (code == BIT_AND_EXPR
12757 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12758 && TREE_CODE (op2) == INTEGER_CST
12759 && integer_pow2p (bottom)
12760 && wi::multiple_of_p (wi::to_widest (op2),
12761 wi::to_widest (bottom), UNSIGNED))
12762 return 1;
12764 op1 = gimple_assign_rhs1 (stmt);
12765 if (code == MINUS_EXPR
12766 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12767 && TREE_CODE (op2) == SSA_NAME
12768 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12769 && gimple_code (stmt) == GIMPLE_ASSIGN
12770 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12771 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12772 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12773 return 1;
12776 /* fall through */
12778 default:
12779 if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
12780 return multiple_p (wi::to_poly_widest (top),
12781 wi::to_poly_widest (bottom));
12783 return 0;
12787 #define tree_expr_nonnegative_warnv_p(X, Y) \
12788 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12790 #define RECURSE(X) \
12791 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12793 /* Return true if CODE or TYPE is known to be non-negative. */
12795 static bool
12796 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12798 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12799 && truth_value_p (code))
12800 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12801 have a signed:1 type (where the value is -1 and 0). */
12802 return true;
12803 return false;
12806 /* Return true if (CODE OP0) is known to be non-negative. If the return
12807 value is based on the assumption that signed overflow is undefined,
12808 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12809 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12811 bool
12812 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12813 bool *strict_overflow_p, int depth)
12815 if (TYPE_UNSIGNED (type))
12816 return true;
12818 switch (code)
12820 case ABS_EXPR:
12821 /* We can't return 1 if flag_wrapv is set because
12822 ABS_EXPR<INT_MIN> = INT_MIN. */
12823 if (!ANY_INTEGRAL_TYPE_P (type))
12824 return true;
12825 if (TYPE_OVERFLOW_UNDEFINED (type))
12827 *strict_overflow_p = true;
12828 return true;
12830 break;
12832 case NON_LVALUE_EXPR:
12833 case FLOAT_EXPR:
12834 case FIX_TRUNC_EXPR:
12835 return RECURSE (op0);
12837 CASE_CONVERT:
12839 tree inner_type = TREE_TYPE (op0);
12840 tree outer_type = type;
12842 if (TREE_CODE (outer_type) == REAL_TYPE)
12844 if (TREE_CODE (inner_type) == REAL_TYPE)
12845 return RECURSE (op0);
12846 if (INTEGRAL_TYPE_P (inner_type))
12848 if (TYPE_UNSIGNED (inner_type))
12849 return true;
12850 return RECURSE (op0);
12853 else if (INTEGRAL_TYPE_P (outer_type))
12855 if (TREE_CODE (inner_type) == REAL_TYPE)
12856 return RECURSE (op0);
12857 if (INTEGRAL_TYPE_P (inner_type))
12858 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12859 && TYPE_UNSIGNED (inner_type);
12862 break;
12864 default:
12865 return tree_simple_nonnegative_warnv_p (code, type);
12868 /* We don't know sign of `t', so be conservative and return false. */
12869 return false;
12872 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12873 value is based on the assumption that signed overflow is undefined,
12874 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12875 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12877 bool
12878 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12879 tree op1, bool *strict_overflow_p,
12880 int depth)
12882 if (TYPE_UNSIGNED (type))
12883 return true;
12885 switch (code)
12887 case POINTER_PLUS_EXPR:
12888 case PLUS_EXPR:
12889 if (FLOAT_TYPE_P (type))
12890 return RECURSE (op0) && RECURSE (op1);
12892 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12893 both unsigned and at least 2 bits shorter than the result. */
12894 if (TREE_CODE (type) == INTEGER_TYPE
12895 && TREE_CODE (op0) == NOP_EXPR
12896 && TREE_CODE (op1) == NOP_EXPR)
12898 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12899 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12900 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12901 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12903 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12904 TYPE_PRECISION (inner2)) + 1;
12905 return prec < TYPE_PRECISION (type);
12908 break;
12910 case MULT_EXPR:
12911 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12913 /* x * x is always non-negative for floating point x
12914 or without overflow. */
12915 if (operand_equal_p (op0, op1, 0)
12916 || (RECURSE (op0) && RECURSE (op1)))
12918 if (ANY_INTEGRAL_TYPE_P (type)
12919 && TYPE_OVERFLOW_UNDEFINED (type))
12920 *strict_overflow_p = true;
12921 return true;
12925 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12926 both unsigned and their total bits is shorter than the result. */
12927 if (TREE_CODE (type) == INTEGER_TYPE
12928 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12929 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12931 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12932 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12933 : TREE_TYPE (op0);
12934 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12935 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12936 : TREE_TYPE (op1);
12938 bool unsigned0 = TYPE_UNSIGNED (inner0);
12939 bool unsigned1 = TYPE_UNSIGNED (inner1);
12941 if (TREE_CODE (op0) == INTEGER_CST)
12942 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12944 if (TREE_CODE (op1) == INTEGER_CST)
12945 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12947 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12948 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12950 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12951 ? tree_int_cst_min_precision (op0, UNSIGNED)
12952 : TYPE_PRECISION (inner0);
12954 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12955 ? tree_int_cst_min_precision (op1, UNSIGNED)
12956 : TYPE_PRECISION (inner1);
12958 return precision0 + precision1 < TYPE_PRECISION (type);
12961 return false;
12963 case BIT_AND_EXPR:
12964 case MAX_EXPR:
12965 return RECURSE (op0) || RECURSE (op1);
12967 case BIT_IOR_EXPR:
12968 case BIT_XOR_EXPR:
12969 case MIN_EXPR:
12970 case RDIV_EXPR:
12971 case TRUNC_DIV_EXPR:
12972 case CEIL_DIV_EXPR:
12973 case FLOOR_DIV_EXPR:
12974 case ROUND_DIV_EXPR:
12975 return RECURSE (op0) && RECURSE (op1);
12977 case TRUNC_MOD_EXPR:
12978 return RECURSE (op0);
12980 case FLOOR_MOD_EXPR:
12981 return RECURSE (op1);
12983 case CEIL_MOD_EXPR:
12984 case ROUND_MOD_EXPR:
12985 default:
12986 return tree_simple_nonnegative_warnv_p (code, type);
12989 /* We don't know sign of `t', so be conservative and return false. */
12990 return false;
12993 /* Return true if T is known to be non-negative. If the return
12994 value is based on the assumption that signed overflow is undefined,
12995 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12996 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12998 bool
12999 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13001 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13002 return true;
13004 switch (TREE_CODE (t))
13006 case INTEGER_CST:
13007 return tree_int_cst_sgn (t) >= 0;
13009 case REAL_CST:
13010 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
13012 case FIXED_CST:
13013 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
13015 case COND_EXPR:
13016 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13018 case SSA_NAME:
13019 /* Limit the depth of recursion to avoid quadratic behavior.
13020 This is expected to catch almost all occurrences in practice.
13021 If this code misses important cases that unbounded recursion
13022 would not, passes that need this information could be revised
13023 to provide it through dataflow propagation. */
13024 return (!name_registered_for_update_p (t)
13025 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13026 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
13027 strict_overflow_p, depth));
13029 default:
13030 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13034 /* Return true if T is known to be non-negative. If the return
13035 value is based on the assumption that signed overflow is undefined,
13036 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13037 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13039 bool
13040 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
13041 bool *strict_overflow_p, int depth)
13043 switch (fn)
13045 CASE_CFN_ACOS:
13046 CASE_CFN_ACOSH:
13047 CASE_CFN_CABS:
13048 CASE_CFN_COSH:
13049 CASE_CFN_ERFC:
13050 CASE_CFN_EXP:
13051 CASE_CFN_EXP10:
13052 CASE_CFN_EXP2:
13053 CASE_CFN_FABS:
13054 CASE_CFN_FDIM:
13055 CASE_CFN_HYPOT:
13056 CASE_CFN_POW10:
13057 CASE_CFN_FFS:
13058 CASE_CFN_PARITY:
13059 CASE_CFN_POPCOUNT:
13060 CASE_CFN_CLZ:
13061 CASE_CFN_CLRSB:
13062 case CFN_BUILT_IN_BSWAP32:
13063 case CFN_BUILT_IN_BSWAP64:
13064 /* Always true. */
13065 return true;
13067 CASE_CFN_SQRT:
13068 CASE_CFN_SQRT_FN:
13069 /* sqrt(-0.0) is -0.0. */
13070 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13071 return true;
13072 return RECURSE (arg0);
13074 CASE_CFN_ASINH:
13075 CASE_CFN_ATAN:
13076 CASE_CFN_ATANH:
13077 CASE_CFN_CBRT:
13078 CASE_CFN_CEIL:
13079 CASE_CFN_CEIL_FN:
13080 CASE_CFN_ERF:
13081 CASE_CFN_EXPM1:
13082 CASE_CFN_FLOOR:
13083 CASE_CFN_FLOOR_FN:
13084 CASE_CFN_FMOD:
13085 CASE_CFN_FREXP:
13086 CASE_CFN_ICEIL:
13087 CASE_CFN_IFLOOR:
13088 CASE_CFN_IRINT:
13089 CASE_CFN_IROUND:
13090 CASE_CFN_LCEIL:
13091 CASE_CFN_LDEXP:
13092 CASE_CFN_LFLOOR:
13093 CASE_CFN_LLCEIL:
13094 CASE_CFN_LLFLOOR:
13095 CASE_CFN_LLRINT:
13096 CASE_CFN_LLROUND:
13097 CASE_CFN_LRINT:
13098 CASE_CFN_LROUND:
13099 CASE_CFN_MODF:
13100 CASE_CFN_NEARBYINT:
13101 CASE_CFN_NEARBYINT_FN:
13102 CASE_CFN_RINT:
13103 CASE_CFN_RINT_FN:
13104 CASE_CFN_ROUND:
13105 CASE_CFN_ROUND_FN:
13106 CASE_CFN_SCALB:
13107 CASE_CFN_SCALBLN:
13108 CASE_CFN_SCALBN:
13109 CASE_CFN_SIGNBIT:
13110 CASE_CFN_SIGNIFICAND:
13111 CASE_CFN_SINH:
13112 CASE_CFN_TANH:
13113 CASE_CFN_TRUNC:
13114 CASE_CFN_TRUNC_FN:
13115 /* True if the 1st argument is nonnegative. */
13116 return RECURSE (arg0);
13118 CASE_CFN_FMAX:
13119 CASE_CFN_FMAX_FN:
13120 /* True if the 1st OR 2nd arguments are nonnegative. */
13121 return RECURSE (arg0) || RECURSE (arg1);
13123 CASE_CFN_FMIN:
13124 CASE_CFN_FMIN_FN:
13125 /* True if the 1st AND 2nd arguments are nonnegative. */
13126 return RECURSE (arg0) && RECURSE (arg1);
13128 CASE_CFN_COPYSIGN:
13129 CASE_CFN_COPYSIGN_FN:
13130 /* True if the 2nd argument is nonnegative. */
13131 return RECURSE (arg1);
13133 CASE_CFN_POWI:
13134 /* True if the 1st argument is nonnegative or the second
13135 argument is an even integer. */
13136 if (TREE_CODE (arg1) == INTEGER_CST
13137 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13138 return true;
13139 return RECURSE (arg0);
13141 CASE_CFN_POW:
13142 /* True if the 1st argument is nonnegative or the second
13143 argument is an even integer valued real. */
13144 if (TREE_CODE (arg1) == REAL_CST)
13146 REAL_VALUE_TYPE c;
13147 HOST_WIDE_INT n;
13149 c = TREE_REAL_CST (arg1);
13150 n = real_to_integer (&c);
13151 if ((n & 1) == 0)
13153 REAL_VALUE_TYPE cint;
13154 real_from_integer (&cint, VOIDmode, n, SIGNED);
13155 if (real_identical (&c, &cint))
13156 return true;
13159 return RECURSE (arg0);
13161 default:
13162 break;
13164 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13167 /* Return true if T is known to be non-negative. If the return
13168 value is based on the assumption that signed overflow is undefined,
13169 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13170 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13172 static bool
13173 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13175 enum tree_code code = TREE_CODE (t);
13176 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13177 return true;
13179 switch (code)
13181 case TARGET_EXPR:
13183 tree temp = TARGET_EXPR_SLOT (t);
13184 t = TARGET_EXPR_INITIAL (t);
13186 /* If the initializer is non-void, then it's a normal expression
13187 that will be assigned to the slot. */
13188 if (!VOID_TYPE_P (t))
13189 return RECURSE (t);
13191 /* Otherwise, the initializer sets the slot in some way. One common
13192 way is an assignment statement at the end of the initializer. */
13193 while (1)
13195 if (TREE_CODE (t) == BIND_EXPR)
13196 t = expr_last (BIND_EXPR_BODY (t));
13197 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13198 || TREE_CODE (t) == TRY_CATCH_EXPR)
13199 t = expr_last (TREE_OPERAND (t, 0));
13200 else if (TREE_CODE (t) == STATEMENT_LIST)
13201 t = expr_last (t);
13202 else
13203 break;
13205 if (TREE_CODE (t) == MODIFY_EXPR
13206 && TREE_OPERAND (t, 0) == temp)
13207 return RECURSE (TREE_OPERAND (t, 1));
13209 return false;
13212 case CALL_EXPR:
13214 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13215 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13217 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13218 get_call_combined_fn (t),
13219 arg0,
13220 arg1,
13221 strict_overflow_p, depth);
13223 case COMPOUND_EXPR:
13224 case MODIFY_EXPR:
13225 return RECURSE (TREE_OPERAND (t, 1));
13227 case BIND_EXPR:
13228 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13230 case SAVE_EXPR:
13231 return RECURSE (TREE_OPERAND (t, 0));
13233 default:
13234 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13238 #undef RECURSE
13239 #undef tree_expr_nonnegative_warnv_p
13241 /* Return true if T is known to be non-negative. If the return
13242 value is based on the assumption that signed overflow is undefined,
13243 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13244 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13246 bool
13247 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13249 enum tree_code code;
13250 if (t == error_mark_node)
13251 return false;
13253 code = TREE_CODE (t);
13254 switch (TREE_CODE_CLASS (code))
13256 case tcc_binary:
13257 case tcc_comparison:
13258 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13259 TREE_TYPE (t),
13260 TREE_OPERAND (t, 0),
13261 TREE_OPERAND (t, 1),
13262 strict_overflow_p, depth);
13264 case tcc_unary:
13265 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13266 TREE_TYPE (t),
13267 TREE_OPERAND (t, 0),
13268 strict_overflow_p, depth);
13270 case tcc_constant:
13271 case tcc_declaration:
13272 case tcc_reference:
13273 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13275 default:
13276 break;
13279 switch (code)
13281 case TRUTH_AND_EXPR:
13282 case TRUTH_OR_EXPR:
13283 case TRUTH_XOR_EXPR:
13284 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13285 TREE_TYPE (t),
13286 TREE_OPERAND (t, 0),
13287 TREE_OPERAND (t, 1),
13288 strict_overflow_p, depth);
13289 case TRUTH_NOT_EXPR:
13290 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13291 TREE_TYPE (t),
13292 TREE_OPERAND (t, 0),
13293 strict_overflow_p, depth);
13295 case COND_EXPR:
13296 case CONSTRUCTOR:
13297 case OBJ_TYPE_REF:
13298 case ASSERT_EXPR:
13299 case ADDR_EXPR:
13300 case WITH_SIZE_EXPR:
13301 case SSA_NAME:
13302 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13304 default:
13305 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13309 /* Return true if `t' is known to be non-negative. Handle warnings
13310 about undefined signed overflow. */
13312 bool
13313 tree_expr_nonnegative_p (tree t)
13315 bool ret, strict_overflow_p;
13317 strict_overflow_p = false;
13318 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13319 if (strict_overflow_p)
13320 fold_overflow_warning (("assuming signed overflow does not occur when "
13321 "determining that expression is always "
13322 "non-negative"),
13323 WARN_STRICT_OVERFLOW_MISC);
13324 return ret;
13328 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13329 For floating point we further ensure that T is not denormal.
13330 Similar logic is present in nonzero_address in rtlanal.h.
13332 If the return value is based on the assumption that signed overflow
13333 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13334 change *STRICT_OVERFLOW_P. */
13336 bool
13337 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13338 bool *strict_overflow_p)
13340 switch (code)
13342 case ABS_EXPR:
13343 return tree_expr_nonzero_warnv_p (op0,
13344 strict_overflow_p);
13346 case NOP_EXPR:
13348 tree inner_type = TREE_TYPE (op0);
13349 tree outer_type = type;
13351 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13352 && tree_expr_nonzero_warnv_p (op0,
13353 strict_overflow_p));
13355 break;
13357 case NON_LVALUE_EXPR:
13358 return tree_expr_nonzero_warnv_p (op0,
13359 strict_overflow_p);
13361 default:
13362 break;
13365 return false;
13368 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13369 For floating point we further ensure that T is not denormal.
13370 Similar logic is present in nonzero_address in rtlanal.h.
13372 If the return value is based on the assumption that signed overflow
13373 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13374 change *STRICT_OVERFLOW_P. */
13376 bool
13377 tree_binary_nonzero_warnv_p (enum tree_code code,
13378 tree type,
13379 tree op0,
13380 tree op1, bool *strict_overflow_p)
13382 bool sub_strict_overflow_p;
13383 switch (code)
13385 case POINTER_PLUS_EXPR:
13386 case PLUS_EXPR:
13387 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13389 /* With the presence of negative values it is hard
13390 to say something. */
13391 sub_strict_overflow_p = false;
13392 if (!tree_expr_nonnegative_warnv_p (op0,
13393 &sub_strict_overflow_p)
13394 || !tree_expr_nonnegative_warnv_p (op1,
13395 &sub_strict_overflow_p))
13396 return false;
13397 /* One of operands must be positive and the other non-negative. */
13398 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13399 overflows, on a twos-complement machine the sum of two
13400 nonnegative numbers can never be zero. */
13401 return (tree_expr_nonzero_warnv_p (op0,
13402 strict_overflow_p)
13403 || tree_expr_nonzero_warnv_p (op1,
13404 strict_overflow_p));
13406 break;
13408 case MULT_EXPR:
13409 if (TYPE_OVERFLOW_UNDEFINED (type))
13411 if (tree_expr_nonzero_warnv_p (op0,
13412 strict_overflow_p)
13413 && tree_expr_nonzero_warnv_p (op1,
13414 strict_overflow_p))
13416 *strict_overflow_p = true;
13417 return true;
13420 break;
13422 case MIN_EXPR:
13423 sub_strict_overflow_p = false;
13424 if (tree_expr_nonzero_warnv_p (op0,
13425 &sub_strict_overflow_p)
13426 && tree_expr_nonzero_warnv_p (op1,
13427 &sub_strict_overflow_p))
13429 if (sub_strict_overflow_p)
13430 *strict_overflow_p = true;
13432 break;
13434 case MAX_EXPR:
13435 sub_strict_overflow_p = false;
13436 if (tree_expr_nonzero_warnv_p (op0,
13437 &sub_strict_overflow_p))
13439 if (sub_strict_overflow_p)
13440 *strict_overflow_p = true;
13442 /* When both operands are nonzero, then MAX must be too. */
13443 if (tree_expr_nonzero_warnv_p (op1,
13444 strict_overflow_p))
13445 return true;
13447 /* MAX where operand 0 is positive is positive. */
13448 return tree_expr_nonnegative_warnv_p (op0,
13449 strict_overflow_p);
13451 /* MAX where operand 1 is positive is positive. */
13452 else if (tree_expr_nonzero_warnv_p (op1,
13453 &sub_strict_overflow_p)
13454 && tree_expr_nonnegative_warnv_p (op1,
13455 &sub_strict_overflow_p))
13457 if (sub_strict_overflow_p)
13458 *strict_overflow_p = true;
13459 return true;
13461 break;
13463 case BIT_IOR_EXPR:
13464 return (tree_expr_nonzero_warnv_p (op1,
13465 strict_overflow_p)
13466 || tree_expr_nonzero_warnv_p (op0,
13467 strict_overflow_p));
13469 default:
13470 break;
13473 return false;
13476 /* Return true when T is an address and is known to be nonzero.
13477 For floating point we further ensure that T is not denormal.
13478 Similar logic is present in nonzero_address in rtlanal.h.
13480 If the return value is based on the assumption that signed overflow
13481 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13482 change *STRICT_OVERFLOW_P. */
13484 bool
13485 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13487 bool sub_strict_overflow_p;
13488 switch (TREE_CODE (t))
13490 case INTEGER_CST:
13491 return !integer_zerop (t);
13493 case ADDR_EXPR:
13495 tree base = TREE_OPERAND (t, 0);
13497 if (!DECL_P (base))
13498 base = get_base_address (base);
13500 if (base && TREE_CODE (base) == TARGET_EXPR)
13501 base = TARGET_EXPR_SLOT (base);
13503 if (!base)
13504 return false;
13506 /* For objects in symbol table check if we know they are non-zero.
13507 Don't do anything for variables and functions before symtab is built;
13508 it is quite possible that they will be declared weak later. */
13509 int nonzero_addr = maybe_nonzero_address (base);
13510 if (nonzero_addr >= 0)
13511 return nonzero_addr;
13513 /* Constants are never weak. */
13514 if (CONSTANT_CLASS_P (base))
13515 return true;
13517 return false;
13520 case COND_EXPR:
13521 sub_strict_overflow_p = false;
13522 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13523 &sub_strict_overflow_p)
13524 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13525 &sub_strict_overflow_p))
13527 if (sub_strict_overflow_p)
13528 *strict_overflow_p = true;
13529 return true;
13531 break;
13533 case SSA_NAME:
13534 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13535 break;
13536 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13538 default:
13539 break;
13541 return false;
13544 #define integer_valued_real_p(X) \
13545 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13547 #define RECURSE(X) \
13548 ((integer_valued_real_p) (X, depth + 1))
13550 /* Return true if the floating point result of (CODE OP0) has an
13551 integer value. We also allow +Inf, -Inf and NaN to be considered
13552 integer values. Return false for signaling NaN.
13554 DEPTH is the current nesting depth of the query. */
13556 bool
13557 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13559 switch (code)
13561 case FLOAT_EXPR:
13562 return true;
13564 case ABS_EXPR:
13565 return RECURSE (op0);
13567 CASE_CONVERT:
13569 tree type = TREE_TYPE (op0);
13570 if (TREE_CODE (type) == INTEGER_TYPE)
13571 return true;
13572 if (TREE_CODE (type) == REAL_TYPE)
13573 return RECURSE (op0);
13574 break;
13577 default:
13578 break;
13580 return false;
13583 /* Return true if the floating point result of (CODE OP0 OP1) has an
13584 integer value. We also allow +Inf, -Inf and NaN to be considered
13585 integer values. Return false for signaling NaN.
13587 DEPTH is the current nesting depth of the query. */
13589 bool
13590 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13592 switch (code)
13594 case PLUS_EXPR:
13595 case MINUS_EXPR:
13596 case MULT_EXPR:
13597 case MIN_EXPR:
13598 case MAX_EXPR:
13599 return RECURSE (op0) && RECURSE (op1);
13601 default:
13602 break;
13604 return false;
13607 /* Return true if the floating point result of calling FNDECL with arguments
13608 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13609 considered integer values. Return false for signaling NaN. If FNDECL
13610 takes fewer than 2 arguments, the remaining ARGn are null.
13612 DEPTH is the current nesting depth of the query. */
13614 bool
13615 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13617 switch (fn)
13619 CASE_CFN_CEIL:
13620 CASE_CFN_CEIL_FN:
13621 CASE_CFN_FLOOR:
13622 CASE_CFN_FLOOR_FN:
13623 CASE_CFN_NEARBYINT:
13624 CASE_CFN_NEARBYINT_FN:
13625 CASE_CFN_RINT:
13626 CASE_CFN_RINT_FN:
13627 CASE_CFN_ROUND:
13628 CASE_CFN_ROUND_FN:
13629 CASE_CFN_TRUNC:
13630 CASE_CFN_TRUNC_FN:
13631 return true;
13633 CASE_CFN_FMIN:
13634 CASE_CFN_FMIN_FN:
13635 CASE_CFN_FMAX:
13636 CASE_CFN_FMAX_FN:
13637 return RECURSE (arg0) && RECURSE (arg1);
13639 default:
13640 break;
13642 return false;
13645 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13646 has an integer value. We also allow +Inf, -Inf and NaN to be
13647 considered integer values. Return false for signaling NaN.
13649 DEPTH is the current nesting depth of the query. */
13651 bool
13652 integer_valued_real_single_p (tree t, int depth)
13654 switch (TREE_CODE (t))
13656 case REAL_CST:
13657 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13659 case COND_EXPR:
13660 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13662 case SSA_NAME:
13663 /* Limit the depth of recursion to avoid quadratic behavior.
13664 This is expected to catch almost all occurrences in practice.
13665 If this code misses important cases that unbounded recursion
13666 would not, passes that need this information could be revised
13667 to provide it through dataflow propagation. */
13668 return (!name_registered_for_update_p (t)
13669 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13670 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13671 depth));
13673 default:
13674 break;
13676 return false;
13679 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13680 has an integer value. We also allow +Inf, -Inf and NaN to be
13681 considered integer values. Return false for signaling NaN.
13683 DEPTH is the current nesting depth of the query. */
13685 static bool
13686 integer_valued_real_invalid_p (tree t, int depth)
13688 switch (TREE_CODE (t))
13690 case COMPOUND_EXPR:
13691 case MODIFY_EXPR:
13692 case BIND_EXPR:
13693 return RECURSE (TREE_OPERAND (t, 1));
13695 case SAVE_EXPR:
13696 return RECURSE (TREE_OPERAND (t, 0));
13698 default:
13699 break;
13701 return false;
13704 #undef RECURSE
13705 #undef integer_valued_real_p
13707 /* Return true if the floating point expression T has an integer value.
13708 We also allow +Inf, -Inf and NaN to be considered integer values.
13709 Return false for signaling NaN.
13711 DEPTH is the current nesting depth of the query. */
13713 bool
13714 integer_valued_real_p (tree t, int depth)
13716 if (t == error_mark_node)
13717 return false;
13719 STRIP_ANY_LOCATION_WRAPPER (t);
13721 tree_code code = TREE_CODE (t);
13722 switch (TREE_CODE_CLASS (code))
13724 case tcc_binary:
13725 case tcc_comparison:
13726 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13727 TREE_OPERAND (t, 1), depth);
13729 case tcc_unary:
13730 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13732 case tcc_constant:
13733 case tcc_declaration:
13734 case tcc_reference:
13735 return integer_valued_real_single_p (t, depth);
13737 default:
13738 break;
13741 switch (code)
13743 case COND_EXPR:
13744 case SSA_NAME:
13745 return integer_valued_real_single_p (t, depth);
13747 case CALL_EXPR:
13749 tree arg0 = (call_expr_nargs (t) > 0
13750 ? CALL_EXPR_ARG (t, 0)
13751 : NULL_TREE);
13752 tree arg1 = (call_expr_nargs (t) > 1
13753 ? CALL_EXPR_ARG (t, 1)
13754 : NULL_TREE);
13755 return integer_valued_real_call_p (get_call_combined_fn (t),
13756 arg0, arg1, depth);
13759 default:
13760 return integer_valued_real_invalid_p (t, depth);
13764 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13765 attempt to fold the expression to a constant without modifying TYPE,
13766 OP0 or OP1.
13768 If the expression could be simplified to a constant, then return
13769 the constant. If the expression would not be simplified to a
13770 constant, then return NULL_TREE. */
13772 tree
13773 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13775 tree tem = fold_binary (code, type, op0, op1);
13776 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13779 /* Given the components of a unary expression CODE, TYPE and OP0,
13780 attempt to fold the expression to a constant without modifying
13781 TYPE or OP0.
13783 If the expression could be simplified to a constant, then return
13784 the constant. If the expression would not be simplified to a
13785 constant, then return NULL_TREE. */
13787 tree
13788 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13790 tree tem = fold_unary (code, type, op0);
13791 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13794 /* If EXP represents referencing an element in a constant string
13795 (either via pointer arithmetic or array indexing), return the
13796 tree representing the value accessed, otherwise return NULL. */
13798 tree
13799 fold_read_from_constant_string (tree exp)
13801 if ((TREE_CODE (exp) == INDIRECT_REF
13802 || TREE_CODE (exp) == ARRAY_REF)
13803 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13805 tree exp1 = TREE_OPERAND (exp, 0);
13806 tree index;
13807 tree string;
13808 location_t loc = EXPR_LOCATION (exp);
13810 if (TREE_CODE (exp) == INDIRECT_REF)
13811 string = string_constant (exp1, &index, NULL, NULL);
13812 else
13814 tree low_bound = array_ref_low_bound (exp);
13815 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13817 /* Optimize the special-case of a zero lower bound.
13819 We convert the low_bound to sizetype to avoid some problems
13820 with constant folding. (E.g. suppose the lower bound is 1,
13821 and its mode is QI. Without the conversion,l (ARRAY
13822 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13823 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13824 if (! integer_zerop (low_bound))
13825 index = size_diffop_loc (loc, index,
13826 fold_convert_loc (loc, sizetype, low_bound));
13828 string = exp1;
13831 scalar_int_mode char_mode;
13832 if (string
13833 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13834 && TREE_CODE (string) == STRING_CST
13835 && TREE_CODE (index) == INTEGER_CST
13836 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13837 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13838 &char_mode)
13839 && GET_MODE_SIZE (char_mode) == 1)
13840 return build_int_cst_type (TREE_TYPE (exp),
13841 (TREE_STRING_POINTER (string)
13842 [TREE_INT_CST_LOW (index)]));
13844 return NULL;
13847 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13848 an integer constant, real, or fixed-point constant.
13850 TYPE is the type of the result. */
13852 static tree
13853 fold_negate_const (tree arg0, tree type)
13855 tree t = NULL_TREE;
13857 switch (TREE_CODE (arg0))
13859 case REAL_CST:
13860 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13861 break;
13863 case FIXED_CST:
13865 FIXED_VALUE_TYPE f;
13866 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13867 &(TREE_FIXED_CST (arg0)), NULL,
13868 TYPE_SATURATING (type));
13869 t = build_fixed (type, f);
13870 /* Propagate overflow flags. */
13871 if (overflow_p | TREE_OVERFLOW (arg0))
13872 TREE_OVERFLOW (t) = 1;
13873 break;
13876 default:
13877 if (poly_int_tree_p (arg0))
13879 wi::overflow_type overflow;
13880 poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
13881 t = force_fit_type (type, res, 1,
13882 (overflow && ! TYPE_UNSIGNED (type))
13883 || TREE_OVERFLOW (arg0));
13884 break;
13887 gcc_unreachable ();
13890 return t;
13893 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13894 an integer constant or real constant.
13896 TYPE is the type of the result. */
13898 tree
13899 fold_abs_const (tree arg0, tree type)
13901 tree t = NULL_TREE;
13903 switch (TREE_CODE (arg0))
13905 case INTEGER_CST:
13907 /* If the value is unsigned or non-negative, then the absolute value
13908 is the same as the ordinary value. */
13909 wide_int val = wi::to_wide (arg0);
13910 wi::overflow_type overflow = wi::OVF_NONE;
13911 if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
13914 /* If the value is negative, then the absolute value is
13915 its negation. */
13916 else
13917 val = wi::neg (val, &overflow);
13919 /* Force to the destination type, set TREE_OVERFLOW for signed
13920 TYPE only. */
13921 t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
13923 break;
13925 case REAL_CST:
13926 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13927 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13928 else
13929 t = arg0;
13930 break;
13932 default:
13933 gcc_unreachable ();
13936 return t;
13939 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13940 constant. TYPE is the type of the result. */
13942 static tree
13943 fold_not_const (const_tree arg0, tree type)
13945 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13947 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
13950 /* Given CODE, a relational operator, the target type, TYPE and two
13951 constant operands OP0 and OP1, return the result of the
13952 relational operation. If the result is not a compile time
13953 constant, then return NULL_TREE. */
13955 static tree
13956 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13958 int result, invert;
13960 /* From here on, the only cases we handle are when the result is
13961 known to be a constant. */
13963 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13965 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13966 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13968 /* Handle the cases where either operand is a NaN. */
13969 if (real_isnan (c0) || real_isnan (c1))
13971 switch (code)
13973 case EQ_EXPR:
13974 case ORDERED_EXPR:
13975 result = 0;
13976 break;
13978 case NE_EXPR:
13979 case UNORDERED_EXPR:
13980 case UNLT_EXPR:
13981 case UNLE_EXPR:
13982 case UNGT_EXPR:
13983 case UNGE_EXPR:
13984 case UNEQ_EXPR:
13985 result = 1;
13986 break;
13988 case LT_EXPR:
13989 case LE_EXPR:
13990 case GT_EXPR:
13991 case GE_EXPR:
13992 case LTGT_EXPR:
13993 if (flag_trapping_math)
13994 return NULL_TREE;
13995 result = 0;
13996 break;
13998 default:
13999 gcc_unreachable ();
14002 return constant_boolean_node (result, type);
14005 return constant_boolean_node (real_compare (code, c0, c1), type);
14008 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
14010 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
14011 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
14012 return constant_boolean_node (fixed_compare (code, c0, c1), type);
14015 /* Handle equality/inequality of complex constants. */
14016 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
14018 tree rcond = fold_relational_const (code, type,
14019 TREE_REALPART (op0),
14020 TREE_REALPART (op1));
14021 tree icond = fold_relational_const (code, type,
14022 TREE_IMAGPART (op0),
14023 TREE_IMAGPART (op1));
14024 if (code == EQ_EXPR)
14025 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
14026 else if (code == NE_EXPR)
14027 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
14028 else
14029 return NULL_TREE;
14032 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
14034 if (!VECTOR_TYPE_P (type))
14036 /* Have vector comparison with scalar boolean result. */
14037 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
14038 && known_eq (VECTOR_CST_NELTS (op0),
14039 VECTOR_CST_NELTS (op1)));
14040 unsigned HOST_WIDE_INT nunits;
14041 if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
14042 return NULL_TREE;
14043 for (unsigned i = 0; i < nunits; i++)
14045 tree elem0 = VECTOR_CST_ELT (op0, i);
14046 tree elem1 = VECTOR_CST_ELT (op1, i);
14047 tree tmp = fold_relational_const (code, type, elem0, elem1);
14048 if (tmp == NULL_TREE)
14049 return NULL_TREE;
14050 if (integer_zerop (tmp))
14051 return constant_boolean_node (false, type);
14053 return constant_boolean_node (true, type);
14055 tree_vector_builder elts;
14056 if (!elts.new_binary_operation (type, op0, op1, false))
14057 return NULL_TREE;
14058 unsigned int count = elts.encoded_nelts ();
14059 for (unsigned i = 0; i < count; i++)
14061 tree elem_type = TREE_TYPE (type);
14062 tree elem0 = VECTOR_CST_ELT (op0, i);
14063 tree elem1 = VECTOR_CST_ELT (op1, i);
14065 tree tem = fold_relational_const (code, elem_type,
14066 elem0, elem1);
14068 if (tem == NULL_TREE)
14069 return NULL_TREE;
14071 elts.quick_push (build_int_cst (elem_type,
14072 integer_zerop (tem) ? 0 : -1));
14075 return elts.build ();
14078 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14080 To compute GT, swap the arguments and do LT.
14081 To compute GE, do LT and invert the result.
14082 To compute LE, swap the arguments, do LT and invert the result.
14083 To compute NE, do EQ and invert the result.
14085 Therefore, the code below must handle only EQ and LT. */
14087 if (code == LE_EXPR || code == GT_EXPR)
14089 std::swap (op0, op1);
14090 code = swap_tree_comparison (code);
14093 /* Note that it is safe to invert for real values here because we
14094 have already handled the one case that it matters. */
14096 invert = 0;
14097 if (code == NE_EXPR || code == GE_EXPR)
14099 invert = 1;
14100 code = invert_tree_comparison (code, false);
14103 /* Compute a result for LT or EQ if args permit;
14104 Otherwise return T. */
14105 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14107 if (code == EQ_EXPR)
14108 result = tree_int_cst_equal (op0, op1);
14109 else
14110 result = tree_int_cst_lt (op0, op1);
14112 else
14113 return NULL_TREE;
14115 if (invert)
14116 result ^= 1;
14117 return constant_boolean_node (result, type);
14120 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14121 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14122 itself. */
14124 tree
14125 fold_build_cleanup_point_expr (tree type, tree expr)
14127 /* If the expression does not have side effects then we don't have to wrap
14128 it with a cleanup point expression. */
14129 if (!TREE_SIDE_EFFECTS (expr))
14130 return expr;
14132 /* If the expression is a return, check to see if the expression inside the
14133 return has no side effects or the right hand side of the modify expression
14134 inside the return. If either don't have side effects set we don't need to
14135 wrap the expression in a cleanup point expression. Note we don't check the
14136 left hand side of the modify because it should always be a return decl. */
14137 if (TREE_CODE (expr) == RETURN_EXPR)
14139 tree op = TREE_OPERAND (expr, 0);
14140 if (!op || !TREE_SIDE_EFFECTS (op))
14141 return expr;
14142 op = TREE_OPERAND (op, 1);
14143 if (!TREE_SIDE_EFFECTS (op))
14144 return expr;
14147 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
14150 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14151 of an indirection through OP0, or NULL_TREE if no simplification is
14152 possible. */
14154 tree
14155 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14157 tree sub = op0;
14158 tree subtype;
14159 poly_uint64 const_op01;
14161 STRIP_NOPS (sub);
14162 subtype = TREE_TYPE (sub);
14163 if (!POINTER_TYPE_P (subtype)
14164 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
14165 return NULL_TREE;
14167 if (TREE_CODE (sub) == ADDR_EXPR)
14169 tree op = TREE_OPERAND (sub, 0);
14170 tree optype = TREE_TYPE (op);
14172 /* *&CONST_DECL -> to the value of the const decl. */
14173 if (TREE_CODE (op) == CONST_DECL)
14174 return DECL_INITIAL (op);
14175 /* *&p => p; make sure to handle *&"str"[cst] here. */
14176 if (type == optype)
14178 tree fop = fold_read_from_constant_string (op);
14179 if (fop)
14180 return fop;
14181 else
14182 return op;
14184 /* *(foo *)&fooarray => fooarray[0] */
14185 else if (TREE_CODE (optype) == ARRAY_TYPE
14186 && type == TREE_TYPE (optype)
14187 && (!in_gimple_form
14188 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14190 tree type_domain = TYPE_DOMAIN (optype);
14191 tree min_val = size_zero_node;
14192 if (type_domain && TYPE_MIN_VALUE (type_domain))
14193 min_val = TYPE_MIN_VALUE (type_domain);
14194 if (in_gimple_form
14195 && TREE_CODE (min_val) != INTEGER_CST)
14196 return NULL_TREE;
14197 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14198 NULL_TREE, NULL_TREE);
14200 /* *(foo *)&complexfoo => __real__ complexfoo */
14201 else if (TREE_CODE (optype) == COMPLEX_TYPE
14202 && type == TREE_TYPE (optype))
14203 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14204 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14205 else if (VECTOR_TYPE_P (optype)
14206 && type == TREE_TYPE (optype))
14208 tree part_width = TYPE_SIZE (type);
14209 tree index = bitsize_int (0);
14210 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
14211 index);
14215 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14216 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
14218 tree op00 = TREE_OPERAND (sub, 0);
14219 tree op01 = TREE_OPERAND (sub, 1);
14221 STRIP_NOPS (op00);
14222 if (TREE_CODE (op00) == ADDR_EXPR)
14224 tree op00type;
14225 op00 = TREE_OPERAND (op00, 0);
14226 op00type = TREE_TYPE (op00);
14228 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14229 if (VECTOR_TYPE_P (op00type)
14230 && type == TREE_TYPE (op00type)
14231 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
14232 but we want to treat offsets with MSB set as negative.
14233 For the code below negative offsets are invalid and
14234 TYPE_SIZE of the element is something unsigned, so
14235 check whether op01 fits into poly_int64, which implies
14236 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
14237 then just use poly_uint64 because we want to treat the
14238 value as unsigned. */
14239 && tree_fits_poly_int64_p (op01))
14241 tree part_width = TYPE_SIZE (type);
14242 poly_uint64 max_offset
14243 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14244 * TYPE_VECTOR_SUBPARTS (op00type));
14245 if (known_lt (const_op01, max_offset))
14247 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
14248 return fold_build3_loc (loc,
14249 BIT_FIELD_REF, type, op00,
14250 part_width, index);
14253 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14254 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14255 && type == TREE_TYPE (op00type))
14257 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
14258 const_op01))
14259 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14261 /* ((foo *)&fooarray)[1] => fooarray[1] */
14262 else if (TREE_CODE (op00type) == ARRAY_TYPE
14263 && type == TREE_TYPE (op00type))
14265 tree type_domain = TYPE_DOMAIN (op00type);
14266 tree min_val = size_zero_node;
14267 if (type_domain && TYPE_MIN_VALUE (type_domain))
14268 min_val = TYPE_MIN_VALUE (type_domain);
14269 poly_uint64 type_size, index;
14270 if (poly_int_tree_p (min_val)
14271 && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
14272 && multiple_p (const_op01, type_size, &index))
14274 poly_offset_int off = index + wi::to_poly_offset (min_val);
14275 op01 = wide_int_to_tree (sizetype, off);
14276 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14277 NULL_TREE, NULL_TREE);
14283 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14284 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14285 && type == TREE_TYPE (TREE_TYPE (subtype))
14286 && (!in_gimple_form
14287 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14289 tree type_domain;
14290 tree min_val = size_zero_node;
14291 sub = build_fold_indirect_ref_loc (loc, sub);
14292 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14293 if (type_domain && TYPE_MIN_VALUE (type_domain))
14294 min_val = TYPE_MIN_VALUE (type_domain);
14295 if (in_gimple_form
14296 && TREE_CODE (min_val) != INTEGER_CST)
14297 return NULL_TREE;
14298 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14299 NULL_TREE);
14302 return NULL_TREE;
14305 /* Builds an expression for an indirection through T, simplifying some
14306 cases. */
14308 tree
14309 build_fold_indirect_ref_loc (location_t loc, tree t)
14311 tree type = TREE_TYPE (TREE_TYPE (t));
14312 tree sub = fold_indirect_ref_1 (loc, type, t);
14314 if (sub)
14315 return sub;
14317 return build1_loc (loc, INDIRECT_REF, type, t);
14320 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14322 tree
14323 fold_indirect_ref_loc (location_t loc, tree t)
14325 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14327 if (sub)
14328 return sub;
14329 else
14330 return t;
14333 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14334 whose result is ignored. The type of the returned tree need not be
14335 the same as the original expression. */
14337 tree
14338 fold_ignored_result (tree t)
14340 if (!TREE_SIDE_EFFECTS (t))
14341 return integer_zero_node;
14343 for (;;)
14344 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14346 case tcc_unary:
14347 t = TREE_OPERAND (t, 0);
14348 break;
14350 case tcc_binary:
14351 case tcc_comparison:
14352 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14353 t = TREE_OPERAND (t, 0);
14354 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14355 t = TREE_OPERAND (t, 1);
14356 else
14357 return t;
14358 break;
14360 case tcc_expression:
14361 switch (TREE_CODE (t))
14363 case COMPOUND_EXPR:
14364 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14365 return t;
14366 t = TREE_OPERAND (t, 0);
14367 break;
14369 case COND_EXPR:
14370 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14371 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14372 return t;
14373 t = TREE_OPERAND (t, 0);
14374 break;
14376 default:
14377 return t;
14379 break;
14381 default:
14382 return t;
14386 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14388 tree
14389 round_up_loc (location_t loc, tree value, unsigned int divisor)
14391 tree div = NULL_TREE;
14393 if (divisor == 1)
14394 return value;
14396 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14397 have to do anything. Only do this when we are not given a const,
14398 because in that case, this check is more expensive than just
14399 doing it. */
14400 if (TREE_CODE (value) != INTEGER_CST)
14402 div = build_int_cst (TREE_TYPE (value), divisor);
14404 if (multiple_of_p (TREE_TYPE (value), value, div))
14405 return value;
14408 /* If divisor is a power of two, simplify this to bit manipulation. */
14409 if (pow2_or_zerop (divisor))
14411 if (TREE_CODE (value) == INTEGER_CST)
14413 wide_int val = wi::to_wide (value);
14414 bool overflow_p;
14416 if ((val & (divisor - 1)) == 0)
14417 return value;
14419 overflow_p = TREE_OVERFLOW (value);
14420 val += divisor - 1;
14421 val &= (int) -divisor;
14422 if (val == 0)
14423 overflow_p = true;
14425 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14427 else
14429 tree t;
14431 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14432 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14433 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14434 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14437 else
14439 if (!div)
14440 div = build_int_cst (TREE_TYPE (value), divisor);
14441 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14442 value = size_binop_loc (loc, MULT_EXPR, value, div);
14445 return value;
14448 /* Likewise, but round down. */
14450 tree
14451 round_down_loc (location_t loc, tree value, int divisor)
14453 tree div = NULL_TREE;
14455 gcc_assert (divisor > 0);
14456 if (divisor == 1)
14457 return value;
14459 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14460 have to do anything. Only do this when we are not given a const,
14461 because in that case, this check is more expensive than just
14462 doing it. */
14463 if (TREE_CODE (value) != INTEGER_CST)
14465 div = build_int_cst (TREE_TYPE (value), divisor);
14467 if (multiple_of_p (TREE_TYPE (value), value, div))
14468 return value;
14471 /* If divisor is a power of two, simplify this to bit manipulation. */
14472 if (pow2_or_zerop (divisor))
14474 tree t;
14476 t = build_int_cst (TREE_TYPE (value), -divisor);
14477 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14479 else
14481 if (!div)
14482 div = build_int_cst (TREE_TYPE (value), divisor);
14483 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14484 value = size_binop_loc (loc, MULT_EXPR, value, div);
14487 return value;
14490 /* Returns the pointer to the base of the object addressed by EXP and
14491 extracts the information about the offset of the access, storing it
14492 to PBITPOS and POFFSET. */
14494 static tree
14495 split_address_to_core_and_offset (tree exp,
14496 poly_int64_pod *pbitpos, tree *poffset)
14498 tree core;
14499 machine_mode mode;
14500 int unsignedp, reversep, volatilep;
14501 poly_int64 bitsize;
14502 location_t loc = EXPR_LOCATION (exp);
14504 if (TREE_CODE (exp) == ADDR_EXPR)
14506 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14507 poffset, &mode, &unsignedp, &reversep,
14508 &volatilep);
14509 core = build_fold_addr_expr_loc (loc, core);
14511 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14513 core = TREE_OPERAND (exp, 0);
14514 STRIP_NOPS (core);
14515 *pbitpos = 0;
14516 *poffset = TREE_OPERAND (exp, 1);
14517 if (poly_int_tree_p (*poffset))
14519 poly_offset_int tem
14520 = wi::sext (wi::to_poly_offset (*poffset),
14521 TYPE_PRECISION (TREE_TYPE (*poffset)));
14522 tem <<= LOG2_BITS_PER_UNIT;
14523 if (tem.to_shwi (pbitpos))
14524 *poffset = NULL_TREE;
14527 else
14529 core = exp;
14530 *pbitpos = 0;
14531 *poffset = NULL_TREE;
14534 return core;
14537 /* Returns true if addresses of E1 and E2 differ by a constant, false
14538 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14540 bool
14541 ptr_difference_const (tree e1, tree e2, poly_int64_pod *diff)
14543 tree core1, core2;
14544 poly_int64 bitpos1, bitpos2;
14545 tree toffset1, toffset2, tdiff, type;
14547 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14548 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14550 poly_int64 bytepos1, bytepos2;
14551 if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
14552 || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
14553 || !operand_equal_p (core1, core2, 0))
14554 return false;
14556 if (toffset1 && toffset2)
14558 type = TREE_TYPE (toffset1);
14559 if (type != TREE_TYPE (toffset2))
14560 toffset2 = fold_convert (type, toffset2);
14562 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14563 if (!cst_and_fits_in_hwi (tdiff))
14564 return false;
14566 *diff = int_cst_value (tdiff);
14568 else if (toffset1 || toffset2)
14570 /* If only one of the offsets is non-constant, the difference cannot
14571 be a constant. */
14572 return false;
14574 else
14575 *diff = 0;
14577 *diff += bytepos1 - bytepos2;
14578 return true;
14581 /* Return OFF converted to a pointer offset type suitable as offset for
14582 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14583 tree
14584 convert_to_ptrofftype_loc (location_t loc, tree off)
14586 return fold_convert_loc (loc, sizetype, off);
14589 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14590 tree
14591 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14593 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14594 ptr, convert_to_ptrofftype_loc (loc, off));
14597 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14598 tree
14599 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14601 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14602 ptr, size_int (off));
14605 /* Return a pointer P to a NUL-terminated string representing the sequence
14606 of constant characters referred to by SRC (or a subsequence of such
14607 characters within it if SRC is a reference to a string plus some
14608 constant offset). If STRLEN is non-null, store the number of bytes
14609 in the string constant including the terminating NUL char. *STRLEN is
14610 typically strlen(P) + 1 in the absence of embedded NUL characters. */
14612 const char *
14613 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen /* = NULL */)
14615 tree offset_node;
14616 tree mem_size;
14618 if (strlen)
14619 *strlen = 0;
14621 src = string_constant (src, &offset_node, &mem_size, NULL);
14622 if (src == 0)
14623 return NULL;
14625 unsigned HOST_WIDE_INT offset = 0;
14626 if (offset_node != NULL_TREE)
14628 if (!tree_fits_uhwi_p (offset_node))
14629 return NULL;
14630 else
14631 offset = tree_to_uhwi (offset_node);
14634 if (!tree_fits_uhwi_p (mem_size))
14635 return NULL;
14637 /* STRING_LENGTH is the size of the string literal, including any
14638 embedded NULs. STRING_SIZE is the size of the array the string
14639 literal is stored in. */
14640 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14641 unsigned HOST_WIDE_INT string_size = tree_to_uhwi (mem_size);
14643 /* Ideally this would turn into a gcc_checking_assert over time. */
14644 if (string_length > string_size)
14645 string_length = string_size;
14647 const char *string = TREE_STRING_POINTER (src);
14649 /* Ideally this would turn into a gcc_checking_assert over time. */
14650 if (string_length > string_size)
14651 string_length = string_size;
14653 if (string_length == 0
14654 || offset >= string_size)
14655 return NULL;
14657 if (strlen)
14659 /* Compute and store the length of the substring at OFFSET.
14660 All offsets past the initial length refer to null strings. */
14661 if (offset < string_length)
14662 *strlen = string_length - offset;
14663 else
14664 *strlen = 1;
14666 else
14668 tree eltype = TREE_TYPE (TREE_TYPE (src));
14669 /* Support only properly NUL-terminated single byte strings. */
14670 if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
14671 return NULL;
14672 if (string[string_length - 1] != '\0')
14673 return NULL;
14676 return offset < string_length ? string + offset : "";
14679 /* Given a tree T, compute which bits in T may be nonzero. */
14681 wide_int
14682 tree_nonzero_bits (const_tree t)
14684 switch (TREE_CODE (t))
14686 case INTEGER_CST:
14687 return wi::to_wide (t);
14688 case SSA_NAME:
14689 return get_nonzero_bits (t);
14690 case NON_LVALUE_EXPR:
14691 case SAVE_EXPR:
14692 return tree_nonzero_bits (TREE_OPERAND (t, 0));
14693 case BIT_AND_EXPR:
14694 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14695 tree_nonzero_bits (TREE_OPERAND (t, 1)));
14696 case BIT_IOR_EXPR:
14697 case BIT_XOR_EXPR:
14698 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14699 tree_nonzero_bits (TREE_OPERAND (t, 1)));
14700 case COND_EXPR:
14701 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1)),
14702 tree_nonzero_bits (TREE_OPERAND (t, 2)));
14703 CASE_CONVERT:
14704 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14705 TYPE_PRECISION (TREE_TYPE (t)),
14706 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0))));
14707 case PLUS_EXPR:
14708 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
14710 wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0));
14711 wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1));
14712 if (wi::bit_and (nzbits1, nzbits2) == 0)
14713 return wi::bit_or (nzbits1, nzbits2);
14715 break;
14716 case LSHIFT_EXPR:
14717 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
14719 tree type = TREE_TYPE (t);
14720 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
14721 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
14722 TYPE_PRECISION (type));
14723 return wi::neg_p (arg1)
14724 ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
14725 : wi::lshift (nzbits, arg1);
14727 break;
14728 case RSHIFT_EXPR:
14729 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
14731 tree type = TREE_TYPE (t);
14732 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
14733 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
14734 TYPE_PRECISION (type));
14735 return wi::neg_p (arg1)
14736 ? wi::lshift (nzbits, -arg1)
14737 : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
14739 break;
14740 default:
14741 break;
14744 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t)));
14747 #if CHECKING_P
14749 namespace selftest {
14751 /* Helper functions for writing tests of folding trees. */
14753 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14755 static void
14756 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14757 tree constant)
14759 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14762 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14763 wrapping WRAPPED_EXPR. */
14765 static void
14766 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14767 tree wrapped_expr)
14769 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14770 ASSERT_NE (wrapped_expr, result);
14771 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14772 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14775 /* Verify that various arithmetic binary operations are folded
14776 correctly. */
14778 static void
14779 test_arithmetic_folding ()
14781 tree type = integer_type_node;
14782 tree x = create_tmp_var_raw (type, "x");
14783 tree zero = build_zero_cst (type);
14784 tree one = build_int_cst (type, 1);
14786 /* Addition. */
14787 /* 1 <-- (0 + 1) */
14788 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14789 one);
14790 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14791 one);
14793 /* (nonlvalue)x <-- (x + 0) */
14794 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14797 /* Subtraction. */
14798 /* 0 <-- (x - x) */
14799 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14800 zero);
14801 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14804 /* Multiplication. */
14805 /* 0 <-- (x * 0) */
14806 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14807 zero);
14809 /* (nonlvalue)x <-- (x * 1) */
14810 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14814 /* Verify that various binary operations on vectors are folded
14815 correctly. */
14817 static void
14818 test_vector_folding ()
14820 tree inner_type = integer_type_node;
14821 tree type = build_vector_type (inner_type, 4);
14822 tree zero = build_zero_cst (type);
14823 tree one = build_one_cst (type);
14825 /* Verify equality tests that return a scalar boolean result. */
14826 tree res_type = boolean_type_node;
14827 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14828 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14829 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14830 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14833 /* Verify folding of VEC_DUPLICATE_EXPRs. */
14835 static void
14836 test_vec_duplicate_folding ()
14838 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
14839 machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
14840 /* This will be 1 if VEC_MODE isn't a vector mode. */
14841 poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
14843 tree type = build_vector_type (ssizetype, nunits);
14844 tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
14845 tree dup5_cst = build_vector_from_val (type, ssize_int (5));
14846 ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
14849 /* Run all of the selftests within this file. */
14851 void
14852 fold_const_c_tests ()
14854 test_arithmetic_folding ();
14855 test_vector_folding ();
14856 test_vec_duplicate_folding ();
14859 } // namespace selftest
14861 #endif /* CHECKING_P */