compiler: add missing Slice_info_expression::do_traverse
[official-gcc.git] / gcc / fold-const.cc
blob02a24c5fe65116f2bc6ba2cb0cd56a9fbbe96e5b
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2023 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-iterator.h"
74 #include "gimple-fold.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"
86 #include "asan.h"
87 #include "gimple-range.h"
89 /* Nonzero if we are folding constants inside an initializer or a C++
90 manifestly-constant-evaluated context; zero otherwise.
91 Should be used when folding in initializer enables additional
92 optimizations. */
93 int folding_initializer = 0;
95 /* Nonzero if we are folding C++ manifestly-constant-evaluated context; zero
96 otherwise.
97 Should be used when certain constructs shouldn't be optimized
98 during folding in that context. */
99 bool folding_cxx_constexpr = false;
101 /* The following constants represent a bit based encoding of GCC's
102 comparison operators. This encoding simplifies transformations
103 on relational comparison operators, such as AND and OR. */
104 enum comparison_code {
105 COMPCODE_FALSE = 0,
106 COMPCODE_LT = 1,
107 COMPCODE_EQ = 2,
108 COMPCODE_LE = 3,
109 COMPCODE_GT = 4,
110 COMPCODE_LTGT = 5,
111 COMPCODE_GE = 6,
112 COMPCODE_ORD = 7,
113 COMPCODE_UNORD = 8,
114 COMPCODE_UNLT = 9,
115 COMPCODE_UNEQ = 10,
116 COMPCODE_UNLE = 11,
117 COMPCODE_UNGT = 12,
118 COMPCODE_NE = 13,
119 COMPCODE_UNGE = 14,
120 COMPCODE_TRUE = 15
123 static bool negate_expr_p (tree);
124 static tree negate_expr (tree);
125 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
126 static enum comparison_code comparison_to_compcode (enum tree_code);
127 static enum tree_code compcode_to_comparison (enum comparison_code);
128 static bool twoval_comparison_p (tree, tree *, tree *);
129 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
130 static tree optimize_bit_field_compare (location_t, enum tree_code,
131 tree, tree, tree);
132 static bool simple_operand_p (const_tree);
133 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
134 static tree range_predecessor (tree);
135 static tree range_successor (tree);
136 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
137 static tree fold_cond_expr_with_comparison (location_t, tree, enum tree_code,
138 tree, tree, tree, tree);
139 static tree unextend (tree, int, int, tree);
140 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
141 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
142 static tree fold_binary_op_with_conditional_arg (location_t,
143 enum tree_code, tree,
144 tree, tree,
145 tree, tree, int);
146 static tree fold_negate_const (tree, tree);
147 static tree fold_not_const (const_tree, tree);
148 static tree fold_relational_const (enum tree_code, tree, tree, tree);
149 static tree fold_convert_const (enum tree_code, tree, tree);
150 static tree fold_view_convert_expr (tree, tree);
151 static tree fold_negate_expr (location_t, tree);
154 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
155 Otherwise, return LOC. */
157 static location_t
158 expr_location_or (tree t, location_t loc)
160 location_t tloc = EXPR_LOCATION (t);
161 return tloc == UNKNOWN_LOCATION ? loc : tloc;
164 /* Similar to protected_set_expr_location, but never modify x in place,
165 if location can and needs to be set, unshare it. */
167 tree
168 protected_set_expr_location_unshare (tree x, location_t loc)
170 if (CAN_HAVE_LOCATION_P (x)
171 && EXPR_LOCATION (x) != loc
172 && !(TREE_CODE (x) == SAVE_EXPR
173 || TREE_CODE (x) == TARGET_EXPR
174 || TREE_CODE (x) == BIND_EXPR))
176 x = copy_node (x);
177 SET_EXPR_LOCATION (x, loc);
179 return x;
182 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
183 division and returns the quotient. Otherwise returns
184 NULL_TREE. */
186 tree
187 div_if_zero_remainder (const_tree arg1, const_tree arg2)
189 widest_int quo;
191 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
192 SIGNED, &quo))
193 return wide_int_to_tree (TREE_TYPE (arg1), quo);
195 return NULL_TREE;
198 /* This is nonzero if we should defer warnings about undefined
199 overflow. This facility exists because these warnings are a
200 special case. The code to estimate loop iterations does not want
201 to issue any warnings, since it works with expressions which do not
202 occur in user code. Various bits of cleanup code call fold(), but
203 only use the result if it has certain characteristics (e.g., is a
204 constant); that code only wants to issue a warning if the result is
205 used. */
207 static int fold_deferring_overflow_warnings;
209 /* If a warning about undefined overflow is deferred, this is the
210 warning. Note that this may cause us to turn two warnings into
211 one, but that is fine since it is sufficient to only give one
212 warning per expression. */
214 static const char* fold_deferred_overflow_warning;
216 /* If a warning about undefined overflow is deferred, this is the
217 level at which the warning should be emitted. */
219 static enum warn_strict_overflow_code fold_deferred_overflow_code;
221 /* Start deferring overflow warnings. We could use a stack here to
222 permit nested calls, but at present it is not necessary. */
224 void
225 fold_defer_overflow_warnings (void)
227 ++fold_deferring_overflow_warnings;
230 /* Stop deferring overflow warnings. If there is a pending warning,
231 and ISSUE is true, then issue the warning if appropriate. STMT is
232 the statement with which the warning should be associated (used for
233 location information); STMT may be NULL. CODE is the level of the
234 warning--a warn_strict_overflow_code value. This function will use
235 the smaller of CODE and the deferred code when deciding whether to
236 issue the warning. CODE may be zero to mean to always use the
237 deferred code. */
239 void
240 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
242 const char *warnmsg;
243 location_t locus;
245 gcc_assert (fold_deferring_overflow_warnings > 0);
246 --fold_deferring_overflow_warnings;
247 if (fold_deferring_overflow_warnings > 0)
249 if (fold_deferred_overflow_warning != NULL
250 && code != 0
251 && code < (int) fold_deferred_overflow_code)
252 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
253 return;
256 warnmsg = fold_deferred_overflow_warning;
257 fold_deferred_overflow_warning = NULL;
259 if (!issue || warnmsg == NULL)
260 return;
262 if (warning_suppressed_p (stmt, OPT_Wstrict_overflow))
263 return;
265 /* Use the smallest code level when deciding to issue the
266 warning. */
267 if (code == 0 || code > (int) fold_deferred_overflow_code)
268 code = fold_deferred_overflow_code;
270 if (!issue_strict_overflow_warning (code))
271 return;
273 if (stmt == NULL)
274 locus = input_location;
275 else
276 locus = gimple_location (stmt);
277 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
280 /* Stop deferring overflow warnings, ignoring any deferred
281 warnings. */
283 void
284 fold_undefer_and_ignore_overflow_warnings (void)
286 fold_undefer_overflow_warnings (false, NULL, 0);
289 /* Whether we are deferring overflow warnings. */
291 bool
292 fold_deferring_overflow_warnings_p (void)
294 return fold_deferring_overflow_warnings > 0;
297 /* This is called when we fold something based on the fact that signed
298 overflow is undefined. */
300 void
301 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
303 if (fold_deferring_overflow_warnings > 0)
305 if (fold_deferred_overflow_warning == NULL
306 || wc < fold_deferred_overflow_code)
308 fold_deferred_overflow_warning = gmsgid;
309 fold_deferred_overflow_code = wc;
312 else if (issue_strict_overflow_warning (wc))
313 warning (OPT_Wstrict_overflow, gmsgid);
316 /* Return true if the built-in mathematical function specified by CODE
317 is odd, i.e. -f(x) == f(-x). */
319 bool
320 negate_mathfn_p (combined_fn fn)
322 switch (fn)
324 CASE_CFN_ASIN:
325 CASE_CFN_ASIN_FN:
326 CASE_CFN_ASINH:
327 CASE_CFN_ASINH_FN:
328 CASE_CFN_ATAN:
329 CASE_CFN_ATAN_FN:
330 CASE_CFN_ATANH:
331 CASE_CFN_ATANH_FN:
332 CASE_CFN_CASIN:
333 CASE_CFN_CASIN_FN:
334 CASE_CFN_CASINH:
335 CASE_CFN_CASINH_FN:
336 CASE_CFN_CATAN:
337 CASE_CFN_CATAN_FN:
338 CASE_CFN_CATANH:
339 CASE_CFN_CATANH_FN:
340 CASE_CFN_CBRT:
341 CASE_CFN_CBRT_FN:
342 CASE_CFN_CPROJ:
343 CASE_CFN_CPROJ_FN:
344 CASE_CFN_CSIN:
345 CASE_CFN_CSIN_FN:
346 CASE_CFN_CSINH:
347 CASE_CFN_CSINH_FN:
348 CASE_CFN_CTAN:
349 CASE_CFN_CTAN_FN:
350 CASE_CFN_CTANH:
351 CASE_CFN_CTANH_FN:
352 CASE_CFN_ERF:
353 CASE_CFN_ERF_FN:
354 CASE_CFN_LLROUND:
355 CASE_CFN_LLROUND_FN:
356 CASE_CFN_LROUND:
357 CASE_CFN_LROUND_FN:
358 CASE_CFN_ROUND:
359 CASE_CFN_ROUNDEVEN:
360 CASE_CFN_ROUNDEVEN_FN:
361 CASE_CFN_SIN:
362 CASE_CFN_SIN_FN:
363 CASE_CFN_SINH:
364 CASE_CFN_SINH_FN:
365 CASE_CFN_TAN:
366 CASE_CFN_TAN_FN:
367 CASE_CFN_TANH:
368 CASE_CFN_TANH_FN:
369 CASE_CFN_TRUNC:
370 CASE_CFN_TRUNC_FN:
371 return true;
373 CASE_CFN_LLRINT:
374 CASE_CFN_LLRINT_FN:
375 CASE_CFN_LRINT:
376 CASE_CFN_LRINT_FN:
377 CASE_CFN_NEARBYINT:
378 CASE_CFN_NEARBYINT_FN:
379 CASE_CFN_RINT:
380 CASE_CFN_RINT_FN:
381 return !flag_rounding_math;
383 default:
384 break;
386 return false;
389 /* Check whether we may negate an integer constant T without causing
390 overflow. */
392 bool
393 may_negate_without_overflow_p (const_tree t)
395 tree type;
397 gcc_assert (TREE_CODE (t) == INTEGER_CST);
399 type = TREE_TYPE (t);
400 if (TYPE_UNSIGNED (type))
401 return false;
403 return !wi::only_sign_bit_p (wi::to_wide (t));
406 /* Determine whether an expression T can be cheaply negated using
407 the function negate_expr without introducing undefined overflow. */
409 static bool
410 negate_expr_p (tree t)
412 tree type;
414 if (t == 0)
415 return false;
417 type = TREE_TYPE (t);
419 STRIP_SIGN_NOPS (t);
420 switch (TREE_CODE (t))
422 case INTEGER_CST:
423 if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
424 return true;
426 /* Check that -CST will not overflow type. */
427 return may_negate_without_overflow_p (t);
428 case BIT_NOT_EXPR:
429 return (INTEGRAL_TYPE_P (type)
430 && TYPE_OVERFLOW_WRAPS (type));
432 case FIXED_CST:
433 return true;
435 case NEGATE_EXPR:
436 return !TYPE_OVERFLOW_SANITIZED (type);
438 case REAL_CST:
439 /* We want to canonicalize to positive real constants. Pretend
440 that only negative ones can be easily negated. */
441 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
443 case COMPLEX_CST:
444 return negate_expr_p (TREE_REALPART (t))
445 && negate_expr_p (TREE_IMAGPART (t));
447 case VECTOR_CST:
449 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
450 return true;
452 /* Steps don't prevent negation. */
453 unsigned int count = vector_cst_encoded_nelts (t);
454 for (unsigned int i = 0; i < count; ++i)
455 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
456 return false;
458 return true;
461 case COMPLEX_EXPR:
462 return negate_expr_p (TREE_OPERAND (t, 0))
463 && negate_expr_p (TREE_OPERAND (t, 1));
465 case CONJ_EXPR:
466 return negate_expr_p (TREE_OPERAND (t, 0));
468 case PLUS_EXPR:
469 if (HONOR_SIGN_DEPENDENT_ROUNDING (type)
470 || HONOR_SIGNED_ZEROS (type)
471 || (ANY_INTEGRAL_TYPE_P (type)
472 && ! TYPE_OVERFLOW_WRAPS (type)))
473 return false;
474 /* -(A + B) -> (-B) - A. */
475 if (negate_expr_p (TREE_OPERAND (t, 1)))
476 return true;
477 /* -(A + B) -> (-A) - B. */
478 return negate_expr_p (TREE_OPERAND (t, 0));
480 case MINUS_EXPR:
481 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
482 return !HONOR_SIGN_DEPENDENT_ROUNDING (type)
483 && !HONOR_SIGNED_ZEROS (type)
484 && (! ANY_INTEGRAL_TYPE_P (type)
485 || TYPE_OVERFLOW_WRAPS (type));
487 case MULT_EXPR:
488 if (TYPE_UNSIGNED (type))
489 break;
490 /* INT_MIN/n * n doesn't overflow while negating one operand it does
491 if n is a (negative) power of two. */
492 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
493 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
494 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
495 && (wi::popcount
496 (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
497 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
498 && (wi::popcount
499 (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
500 break;
502 /* Fall through. */
504 case RDIV_EXPR:
505 if (! HONOR_SIGN_DEPENDENT_ROUNDING (t))
506 return negate_expr_p (TREE_OPERAND (t, 1))
507 || negate_expr_p (TREE_OPERAND (t, 0));
508 break;
510 case TRUNC_DIV_EXPR:
511 case ROUND_DIV_EXPR:
512 case EXACT_DIV_EXPR:
513 if (TYPE_UNSIGNED (type))
514 break;
515 /* In general we can't negate A in A / B, because if A is INT_MIN and
516 B is not 1 we change the sign of the result. */
517 if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
518 && negate_expr_p (TREE_OPERAND (t, 0)))
519 return true;
520 /* In general we can't negate B in A / B, because if A is INT_MIN and
521 B is 1, we may turn this into INT_MIN / -1 which is undefined
522 and actually traps on some architectures. */
523 if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
524 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
525 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
526 && ! integer_onep (TREE_OPERAND (t, 1))))
527 return negate_expr_p (TREE_OPERAND (t, 1));
528 break;
530 case NOP_EXPR:
531 /* Negate -((double)float) as (double)(-float). */
532 if (TREE_CODE (type) == REAL_TYPE)
534 tree tem = strip_float_extensions (t);
535 if (tem != t)
536 return negate_expr_p (tem);
538 break;
540 case CALL_EXPR:
541 /* Negate -f(x) as f(-x). */
542 if (negate_mathfn_p (get_call_combined_fn (t)))
543 return negate_expr_p (CALL_EXPR_ARG (t, 0));
544 break;
546 case RSHIFT_EXPR:
547 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
548 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
550 tree op1 = TREE_OPERAND (t, 1);
551 if (wi::to_wide (op1) == element_precision (type) - 1)
552 return true;
554 break;
556 default:
557 break;
559 return false;
562 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
563 simplification is possible.
564 If negate_expr_p would return true for T, NULL_TREE will never be
565 returned. */
567 static tree
568 fold_negate_expr_1 (location_t loc, tree t)
570 tree type = TREE_TYPE (t);
571 tree tem;
573 switch (TREE_CODE (t))
575 /* Convert - (~A) to A + 1. */
576 case BIT_NOT_EXPR:
577 if (INTEGRAL_TYPE_P (type))
578 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
579 build_one_cst (type));
580 break;
582 case INTEGER_CST:
583 tem = fold_negate_const (t, type);
584 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
585 || (ANY_INTEGRAL_TYPE_P (type)
586 && !TYPE_OVERFLOW_TRAPS (type)
587 && TYPE_OVERFLOW_WRAPS (type))
588 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
589 return tem;
590 break;
592 case POLY_INT_CST:
593 case REAL_CST:
594 case FIXED_CST:
595 tem = fold_negate_const (t, type);
596 return tem;
598 case COMPLEX_CST:
600 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
601 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
602 if (rpart && ipart)
603 return build_complex (type, rpart, ipart);
605 break;
607 case VECTOR_CST:
609 tree_vector_builder elts;
610 elts.new_unary_operation (type, t, true);
611 unsigned int count = elts.encoded_nelts ();
612 for (unsigned int i = 0; i < count; ++i)
614 tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
615 if (elt == NULL_TREE)
616 return NULL_TREE;
617 elts.quick_push (elt);
620 return elts.build ();
623 case COMPLEX_EXPR:
624 if (negate_expr_p (t))
625 return fold_build2_loc (loc, COMPLEX_EXPR, type,
626 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
627 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
628 break;
630 case CONJ_EXPR:
631 if (negate_expr_p (t))
632 return fold_build1_loc (loc, CONJ_EXPR, type,
633 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
634 break;
636 case NEGATE_EXPR:
637 if (!TYPE_OVERFLOW_SANITIZED (type))
638 return TREE_OPERAND (t, 0);
639 break;
641 case PLUS_EXPR:
642 if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
643 && !HONOR_SIGNED_ZEROS (type))
645 /* -(A + B) -> (-B) - A. */
646 if (negate_expr_p (TREE_OPERAND (t, 1)))
648 tem = negate_expr (TREE_OPERAND (t, 1));
649 return fold_build2_loc (loc, MINUS_EXPR, type,
650 tem, TREE_OPERAND (t, 0));
653 /* -(A + B) -> (-A) - B. */
654 if (negate_expr_p (TREE_OPERAND (t, 0)))
656 tem = negate_expr (TREE_OPERAND (t, 0));
657 return fold_build2_loc (loc, MINUS_EXPR, type,
658 tem, TREE_OPERAND (t, 1));
661 break;
663 case MINUS_EXPR:
664 /* - (A - B) -> B - A */
665 if (!HONOR_SIGN_DEPENDENT_ROUNDING (type)
666 && !HONOR_SIGNED_ZEROS (type))
667 return fold_build2_loc (loc, MINUS_EXPR, type,
668 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
669 break;
671 case MULT_EXPR:
672 if (TYPE_UNSIGNED (type))
673 break;
675 /* Fall through. */
677 case RDIV_EXPR:
678 if (! HONOR_SIGN_DEPENDENT_ROUNDING (type))
680 tem = TREE_OPERAND (t, 1);
681 if (negate_expr_p (tem))
682 return fold_build2_loc (loc, TREE_CODE (t), type,
683 TREE_OPERAND (t, 0), negate_expr (tem));
684 tem = TREE_OPERAND (t, 0);
685 if (negate_expr_p (tem))
686 return fold_build2_loc (loc, TREE_CODE (t), type,
687 negate_expr (tem), TREE_OPERAND (t, 1));
689 break;
691 case TRUNC_DIV_EXPR:
692 case ROUND_DIV_EXPR:
693 case EXACT_DIV_EXPR:
694 if (TYPE_UNSIGNED (type))
695 break;
696 /* In general we can't negate A in A / B, because if A is INT_MIN and
697 B is not 1 we change the sign of the result. */
698 if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
699 && negate_expr_p (TREE_OPERAND (t, 0)))
700 return fold_build2_loc (loc, TREE_CODE (t), type,
701 negate_expr (TREE_OPERAND (t, 0)),
702 TREE_OPERAND (t, 1));
703 /* In general we can't negate B in A / B, because if A is INT_MIN and
704 B is 1, we may turn this into INT_MIN / -1 which is undefined
705 and actually traps on some architectures. */
706 if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
707 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
708 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
709 && ! integer_onep (TREE_OPERAND (t, 1))))
710 && negate_expr_p (TREE_OPERAND (t, 1)))
711 return fold_build2_loc (loc, TREE_CODE (t), type,
712 TREE_OPERAND (t, 0),
713 negate_expr (TREE_OPERAND (t, 1)));
714 break;
716 case NOP_EXPR:
717 /* Convert -((double)float) into (double)(-float). */
718 if (TREE_CODE (type) == REAL_TYPE)
720 tem = strip_float_extensions (t);
721 if (tem != t && negate_expr_p (tem))
722 return fold_convert_loc (loc, type, negate_expr (tem));
724 break;
726 case CALL_EXPR:
727 /* Negate -f(x) as f(-x). */
728 if (negate_mathfn_p (get_call_combined_fn (t))
729 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
731 tree fndecl, arg;
733 fndecl = get_callee_fndecl (t);
734 arg = negate_expr (CALL_EXPR_ARG (t, 0));
735 return build_call_expr_loc (loc, fndecl, 1, arg);
737 break;
739 case RSHIFT_EXPR:
740 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
741 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
743 tree op1 = TREE_OPERAND (t, 1);
744 if (wi::to_wide (op1) == element_precision (type) - 1)
746 tree ntype = TYPE_UNSIGNED (type)
747 ? signed_type_for (type)
748 : unsigned_type_for (type);
749 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
750 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
751 return fold_convert_loc (loc, type, temp);
754 break;
756 default:
757 break;
760 return NULL_TREE;
763 /* A wrapper for fold_negate_expr_1. */
765 static tree
766 fold_negate_expr (location_t loc, tree t)
768 tree type = TREE_TYPE (t);
769 STRIP_SIGN_NOPS (t);
770 tree tem = fold_negate_expr_1 (loc, t);
771 if (tem == NULL_TREE)
772 return NULL_TREE;
773 return fold_convert_loc (loc, type, tem);
776 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
777 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
778 return NULL_TREE. */
780 static tree
781 negate_expr (tree t)
783 tree type, tem;
784 location_t loc;
786 if (t == NULL_TREE)
787 return NULL_TREE;
789 loc = EXPR_LOCATION (t);
790 type = TREE_TYPE (t);
791 STRIP_SIGN_NOPS (t);
793 tem = fold_negate_expr (loc, t);
794 if (!tem)
795 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
796 return fold_convert_loc (loc, type, tem);
799 /* Split a tree IN into a constant, literal and variable parts that could be
800 combined with CODE to make IN. "constant" means an expression with
801 TREE_CONSTANT but that isn't an actual constant. CODE must be a
802 commutative arithmetic operation. Store the constant part into *CONP,
803 the literal in *LITP and return the variable part. If a part isn't
804 present, set it to null. If the tree does not decompose in this way,
805 return the entire tree as the variable part and the other parts as null.
807 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
808 case, we negate an operand that was subtracted. Except if it is a
809 literal for which we use *MINUS_LITP instead.
811 If NEGATE_P is true, we are negating all of IN, again except a literal
812 for which we use *MINUS_LITP instead. If a variable part is of pointer
813 type, it is negated after converting to TYPE. This prevents us from
814 generating illegal MINUS pointer expression. LOC is the location of
815 the converted variable part.
817 If IN is itself a literal or constant, return it as appropriate.
819 Note that we do not guarantee that any of the three values will be the
820 same type as IN, but they will have the same signedness and mode. */
822 static tree
823 split_tree (tree in, tree type, enum tree_code code,
824 tree *minus_varp, tree *conp, tree *minus_conp,
825 tree *litp, tree *minus_litp, int negate_p)
827 tree var = 0;
828 *minus_varp = 0;
829 *conp = 0;
830 *minus_conp = 0;
831 *litp = 0;
832 *minus_litp = 0;
834 /* Strip any conversions that don't change the machine mode or signedness. */
835 STRIP_SIGN_NOPS (in);
837 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
838 || TREE_CODE (in) == FIXED_CST)
839 *litp = in;
840 else if (TREE_CODE (in) == code
841 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
842 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
843 /* We can associate addition and subtraction together (even
844 though the C standard doesn't say so) for integers because
845 the value is not affected. For reals, the value might be
846 affected, so we can't. */
847 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
848 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
849 || (code == MINUS_EXPR
850 && (TREE_CODE (in) == PLUS_EXPR
851 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
853 tree op0 = TREE_OPERAND (in, 0);
854 tree op1 = TREE_OPERAND (in, 1);
855 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
856 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
858 /* First see if either of the operands is a literal, then a constant. */
859 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
860 || TREE_CODE (op0) == FIXED_CST)
861 *litp = op0, op0 = 0;
862 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
863 || TREE_CODE (op1) == FIXED_CST)
864 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
866 if (op0 != 0 && TREE_CONSTANT (op0))
867 *conp = op0, op0 = 0;
868 else if (op1 != 0 && TREE_CONSTANT (op1))
869 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
871 /* If we haven't dealt with either operand, this is not a case we can
872 decompose. Otherwise, VAR is either of the ones remaining, if any. */
873 if (op0 != 0 && op1 != 0)
874 var = in;
875 else if (op0 != 0)
876 var = op0;
877 else
878 var = op1, neg_var_p = neg1_p;
880 /* Now do any needed negations. */
881 if (neg_litp_p)
882 *minus_litp = *litp, *litp = 0;
883 if (neg_conp_p && *conp)
884 *minus_conp = *conp, *conp = 0;
885 if (neg_var_p && var)
886 *minus_varp = var, var = 0;
888 else if (TREE_CONSTANT (in))
889 *conp = in;
890 else if (TREE_CODE (in) == BIT_NOT_EXPR
891 && code == PLUS_EXPR)
893 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
894 when IN is constant. */
895 *litp = build_minus_one_cst (type);
896 *minus_varp = TREE_OPERAND (in, 0);
898 else
899 var = in;
901 if (negate_p)
903 if (*litp)
904 *minus_litp = *litp, *litp = 0;
905 else if (*minus_litp)
906 *litp = *minus_litp, *minus_litp = 0;
907 if (*conp)
908 *minus_conp = *conp, *conp = 0;
909 else if (*minus_conp)
910 *conp = *minus_conp, *minus_conp = 0;
911 if (var)
912 *minus_varp = var, var = 0;
913 else if (*minus_varp)
914 var = *minus_varp, *minus_varp = 0;
917 if (*litp
918 && TREE_OVERFLOW_P (*litp))
919 *litp = drop_tree_overflow (*litp);
920 if (*minus_litp
921 && TREE_OVERFLOW_P (*minus_litp))
922 *minus_litp = drop_tree_overflow (*minus_litp);
924 return var;
927 /* Re-associate trees split by the above function. T1 and T2 are
928 either expressions to associate or null. Return the new
929 expression, if any. LOC is the location of the new expression. If
930 we build an operation, do it in TYPE and with CODE. */
932 static tree
933 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
935 if (t1 == 0)
937 gcc_assert (t2 == 0 || code != MINUS_EXPR);
938 return t2;
940 else if (t2 == 0)
941 return t1;
943 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
944 try to fold this since we will have infinite recursion. But do
945 deal with any NEGATE_EXPRs. */
946 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
947 || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
948 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
950 if (code == PLUS_EXPR)
952 if (TREE_CODE (t1) == NEGATE_EXPR)
953 return build2_loc (loc, MINUS_EXPR, type,
954 fold_convert_loc (loc, type, t2),
955 fold_convert_loc (loc, type,
956 TREE_OPERAND (t1, 0)));
957 else if (TREE_CODE (t2) == NEGATE_EXPR)
958 return build2_loc (loc, MINUS_EXPR, type,
959 fold_convert_loc (loc, type, t1),
960 fold_convert_loc (loc, type,
961 TREE_OPERAND (t2, 0)));
962 else if (integer_zerop (t2))
963 return fold_convert_loc (loc, type, t1);
965 else if (code == MINUS_EXPR)
967 if (integer_zerop (t2))
968 return fold_convert_loc (loc, type, t1);
971 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
972 fold_convert_loc (loc, type, t2));
975 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
976 fold_convert_loc (loc, type, t2));
979 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
980 for use in int_const_binop, size_binop and size_diffop. */
982 static bool
983 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
985 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
986 return false;
987 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
988 return false;
990 switch (code)
992 case LSHIFT_EXPR:
993 case RSHIFT_EXPR:
994 case LROTATE_EXPR:
995 case RROTATE_EXPR:
996 return true;
998 default:
999 break;
1002 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1003 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1004 && TYPE_MODE (type1) == TYPE_MODE (type2);
1007 /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
1008 a new constant in RES. Return FALSE if we don't know how to
1009 evaluate CODE at compile-time. */
1011 bool
1012 wide_int_binop (wide_int &res,
1013 enum tree_code code, const wide_int &arg1, const wide_int &arg2,
1014 signop sign, wi::overflow_type *overflow)
1016 wide_int tmp;
1017 *overflow = wi::OVF_NONE;
1018 switch (code)
1020 case BIT_IOR_EXPR:
1021 res = wi::bit_or (arg1, arg2);
1022 break;
1024 case BIT_XOR_EXPR:
1025 res = wi::bit_xor (arg1, arg2);
1026 break;
1028 case BIT_AND_EXPR:
1029 res = wi::bit_and (arg1, arg2);
1030 break;
1032 case LSHIFT_EXPR:
1033 if (wi::neg_p (arg2))
1034 return false;
1035 res = wi::lshift (arg1, arg2);
1036 break;
1038 case RSHIFT_EXPR:
1039 if (wi::neg_p (arg2))
1040 return false;
1041 /* It's unclear from the C standard whether shifts can overflow.
1042 The following code ignores overflow; perhaps a C standard
1043 interpretation ruling is needed. */
1044 res = wi::rshift (arg1, arg2, sign);
1045 break;
1047 case RROTATE_EXPR:
1048 case LROTATE_EXPR:
1049 if (wi::neg_p (arg2))
1051 tmp = -arg2;
1052 if (code == RROTATE_EXPR)
1053 code = LROTATE_EXPR;
1054 else
1055 code = RROTATE_EXPR;
1057 else
1058 tmp = arg2;
1060 if (code == RROTATE_EXPR)
1061 res = wi::rrotate (arg1, tmp);
1062 else
1063 res = wi::lrotate (arg1, tmp);
1064 break;
1066 case PLUS_EXPR:
1067 res = wi::add (arg1, arg2, sign, overflow);
1068 break;
1070 case MINUS_EXPR:
1071 res = wi::sub (arg1, arg2, sign, overflow);
1072 break;
1074 case MULT_EXPR:
1075 res = wi::mul (arg1, arg2, sign, overflow);
1076 break;
1078 case MULT_HIGHPART_EXPR:
1079 res = wi::mul_high (arg1, arg2, sign);
1080 break;
1082 case TRUNC_DIV_EXPR:
1083 case EXACT_DIV_EXPR:
1084 if (arg2 == 0)
1085 return false;
1086 res = wi::div_trunc (arg1, arg2, sign, overflow);
1087 break;
1089 case FLOOR_DIV_EXPR:
1090 if (arg2 == 0)
1091 return false;
1092 res = wi::div_floor (arg1, arg2, sign, overflow);
1093 break;
1095 case CEIL_DIV_EXPR:
1096 if (arg2 == 0)
1097 return false;
1098 res = wi::div_ceil (arg1, arg2, sign, overflow);
1099 break;
1101 case ROUND_DIV_EXPR:
1102 if (arg2 == 0)
1103 return false;
1104 res = wi::div_round (arg1, arg2, sign, overflow);
1105 break;
1107 case TRUNC_MOD_EXPR:
1108 if (arg2 == 0)
1109 return false;
1110 res = wi::mod_trunc (arg1, arg2, sign, overflow);
1111 break;
1113 case FLOOR_MOD_EXPR:
1114 if (arg2 == 0)
1115 return false;
1116 res = wi::mod_floor (arg1, arg2, sign, overflow);
1117 break;
1119 case CEIL_MOD_EXPR:
1120 if (arg2 == 0)
1121 return false;
1122 res = wi::mod_ceil (arg1, arg2, sign, overflow);
1123 break;
1125 case ROUND_MOD_EXPR:
1126 if (arg2 == 0)
1127 return false;
1128 res = wi::mod_round (arg1, arg2, sign, overflow);
1129 break;
1131 case MIN_EXPR:
1132 res = wi::min (arg1, arg2, sign);
1133 break;
1135 case MAX_EXPR:
1136 res = wi::max (arg1, arg2, sign);
1137 break;
1139 default:
1140 return false;
1142 return true;
1145 /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1146 produce a new constant in RES. Return FALSE if we don't know how
1147 to evaluate CODE at compile-time. */
1149 static bool
1150 poly_int_binop (poly_wide_int &res, enum tree_code code,
1151 const_tree arg1, const_tree arg2,
1152 signop sign, wi::overflow_type *overflow)
1154 gcc_assert (NUM_POLY_INT_COEFFS != 1);
1155 gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1156 switch (code)
1158 case PLUS_EXPR:
1159 res = wi::add (wi::to_poly_wide (arg1),
1160 wi::to_poly_wide (arg2), sign, overflow);
1161 break;
1163 case MINUS_EXPR:
1164 res = wi::sub (wi::to_poly_wide (arg1),
1165 wi::to_poly_wide (arg2), sign, overflow);
1166 break;
1168 case MULT_EXPR:
1169 if (TREE_CODE (arg2) == INTEGER_CST)
1170 res = wi::mul (wi::to_poly_wide (arg1),
1171 wi::to_wide (arg2), sign, overflow);
1172 else if (TREE_CODE (arg1) == INTEGER_CST)
1173 res = wi::mul (wi::to_poly_wide (arg2),
1174 wi::to_wide (arg1), sign, overflow);
1175 else
1176 return NULL_TREE;
1177 break;
1179 case LSHIFT_EXPR:
1180 if (TREE_CODE (arg2) == INTEGER_CST)
1181 res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1182 else
1183 return false;
1184 break;
1186 case BIT_IOR_EXPR:
1187 if (TREE_CODE (arg2) != INTEGER_CST
1188 || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1189 &res))
1190 return false;
1191 break;
1193 default:
1194 return false;
1196 return true;
1199 /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1200 produce a new constant. Return NULL_TREE if we don't know how to
1201 evaluate CODE at compile-time. */
1203 tree
1204 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1205 int overflowable)
1207 poly_wide_int poly_res;
1208 tree type = TREE_TYPE (arg1);
1209 signop sign = TYPE_SIGN (type);
1210 wi::overflow_type overflow = wi::OVF_NONE;
1212 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1214 wide_int warg1 = wi::to_wide (arg1), res;
1215 wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (type));
1216 if (!wide_int_binop (res, code, warg1, warg2, sign, &overflow))
1217 return NULL_TREE;
1218 poly_res = res;
1220 else if (!poly_int_tree_p (arg1)
1221 || !poly_int_tree_p (arg2)
1222 || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1223 return NULL_TREE;
1224 return force_fit_type (type, poly_res, overflowable,
1225 (((sign == SIGNED || overflowable == -1)
1226 && overflow)
1227 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1230 /* Return true if binary operation OP distributes over addition in operand
1231 OPNO, with the other operand being held constant. OPNO counts from 1. */
1233 static bool
1234 distributes_over_addition_p (tree_code op, int opno)
1236 switch (op)
1238 case PLUS_EXPR:
1239 case MINUS_EXPR:
1240 case MULT_EXPR:
1241 return true;
1243 case LSHIFT_EXPR:
1244 return opno == 1;
1246 default:
1247 return false;
1251 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1252 constant. We assume ARG1 and ARG2 have the same data type, or at least
1253 are the same kind of constant and the same machine mode. Return zero if
1254 combining the constants is not allowed in the current operating mode. */
1256 static tree
1257 const_binop (enum tree_code code, tree arg1, tree arg2)
1259 /* Sanity check for the recursive cases. */
1260 if (!arg1 || !arg2)
1261 return NULL_TREE;
1263 STRIP_NOPS (arg1);
1264 STRIP_NOPS (arg2);
1266 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1268 if (code == POINTER_PLUS_EXPR)
1269 return int_const_binop (PLUS_EXPR,
1270 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1272 return int_const_binop (code, arg1, arg2);
1275 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1277 machine_mode mode;
1278 REAL_VALUE_TYPE d1;
1279 REAL_VALUE_TYPE d2;
1280 REAL_VALUE_TYPE value;
1281 REAL_VALUE_TYPE result;
1282 bool inexact;
1283 tree t, type;
1285 /* The following codes are handled by real_arithmetic. */
1286 switch (code)
1288 case PLUS_EXPR:
1289 case MINUS_EXPR:
1290 case MULT_EXPR:
1291 case RDIV_EXPR:
1292 case MIN_EXPR:
1293 case MAX_EXPR:
1294 break;
1296 default:
1297 return NULL_TREE;
1300 d1 = TREE_REAL_CST (arg1);
1301 d2 = TREE_REAL_CST (arg2);
1303 type = TREE_TYPE (arg1);
1304 mode = TYPE_MODE (type);
1306 /* Don't perform operation if we honor signaling NaNs and
1307 either operand is a signaling NaN. */
1308 if (HONOR_SNANS (mode)
1309 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1310 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1311 return NULL_TREE;
1313 /* Don't perform operation if it would raise a division
1314 by zero exception. */
1315 if (code == RDIV_EXPR
1316 && real_equal (&d2, &dconst0)
1317 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1318 return NULL_TREE;
1320 /* If either operand is a NaN, just return it. Otherwise, set up
1321 for floating-point trap; we return an overflow. */
1322 if (REAL_VALUE_ISNAN (d1))
1324 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1325 is off. */
1326 d1.signalling = 0;
1327 t = build_real (type, d1);
1328 return t;
1330 else if (REAL_VALUE_ISNAN (d2))
1332 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1333 is off. */
1334 d2.signalling = 0;
1335 t = build_real (type, d2);
1336 return t;
1339 inexact = real_arithmetic (&value, code, &d1, &d2);
1340 real_convert (&result, mode, &value);
1342 /* Don't constant fold this floating point operation if
1343 both operands are not NaN but the result is NaN, and
1344 flag_trapping_math. Such operations should raise an
1345 invalid operation exception. */
1346 if (flag_trapping_math
1347 && MODE_HAS_NANS (mode)
1348 && REAL_VALUE_ISNAN (result)
1349 && !REAL_VALUE_ISNAN (d1)
1350 && !REAL_VALUE_ISNAN (d2))
1351 return NULL_TREE;
1353 /* Don't constant fold this floating point operation if
1354 the result has overflowed and flag_trapping_math. */
1355 if (flag_trapping_math
1356 && MODE_HAS_INFINITIES (mode)
1357 && REAL_VALUE_ISINF (result)
1358 && !REAL_VALUE_ISINF (d1)
1359 && !REAL_VALUE_ISINF (d2))
1360 return NULL_TREE;
1362 /* Don't constant fold this floating point operation if the
1363 result may dependent upon the run-time rounding mode and
1364 flag_rounding_math is set, or if GCC's software emulation
1365 is unable to accurately represent the result. */
1366 if ((flag_rounding_math
1367 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1368 && (inexact || !real_identical (&result, &value)))
1369 return NULL_TREE;
1371 t = build_real (type, result);
1373 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1374 return t;
1377 if (TREE_CODE (arg1) == FIXED_CST)
1379 FIXED_VALUE_TYPE f1;
1380 FIXED_VALUE_TYPE f2;
1381 FIXED_VALUE_TYPE result;
1382 tree t, type;
1383 int sat_p;
1384 bool overflow_p;
1386 /* The following codes are handled by fixed_arithmetic. */
1387 switch (code)
1389 case PLUS_EXPR:
1390 case MINUS_EXPR:
1391 case MULT_EXPR:
1392 case TRUNC_DIV_EXPR:
1393 if (TREE_CODE (arg2) != FIXED_CST)
1394 return NULL_TREE;
1395 f2 = TREE_FIXED_CST (arg2);
1396 break;
1398 case LSHIFT_EXPR:
1399 case RSHIFT_EXPR:
1401 if (TREE_CODE (arg2) != INTEGER_CST)
1402 return NULL_TREE;
1403 wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1404 f2.data.high = w2.elt (1);
1405 f2.data.low = w2.ulow ();
1406 f2.mode = SImode;
1408 break;
1410 default:
1411 return NULL_TREE;
1414 f1 = TREE_FIXED_CST (arg1);
1415 type = TREE_TYPE (arg1);
1416 sat_p = TYPE_SATURATING (type);
1417 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1418 t = build_fixed (type, result);
1419 /* Propagate overflow flags. */
1420 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1421 TREE_OVERFLOW (t) = 1;
1422 return t;
1425 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1427 tree type = TREE_TYPE (arg1);
1428 tree r1 = TREE_REALPART (arg1);
1429 tree i1 = TREE_IMAGPART (arg1);
1430 tree r2 = TREE_REALPART (arg2);
1431 tree i2 = TREE_IMAGPART (arg2);
1432 tree real, imag;
1434 switch (code)
1436 case PLUS_EXPR:
1437 case MINUS_EXPR:
1438 real = const_binop (code, r1, r2);
1439 imag = const_binop (code, i1, i2);
1440 break;
1442 case MULT_EXPR:
1443 if (COMPLEX_FLOAT_TYPE_P (type))
1444 return do_mpc_arg2 (arg1, arg2, type,
1445 /* do_nonfinite= */ folding_initializer,
1446 mpc_mul);
1448 real = const_binop (MINUS_EXPR,
1449 const_binop (MULT_EXPR, r1, r2),
1450 const_binop (MULT_EXPR, i1, i2));
1451 imag = const_binop (PLUS_EXPR,
1452 const_binop (MULT_EXPR, r1, i2),
1453 const_binop (MULT_EXPR, i1, r2));
1454 break;
1456 case RDIV_EXPR:
1457 if (COMPLEX_FLOAT_TYPE_P (type))
1458 return do_mpc_arg2 (arg1, arg2, type,
1459 /* do_nonfinite= */ folding_initializer,
1460 mpc_div);
1461 /* Fallthru. */
1462 case TRUNC_DIV_EXPR:
1463 case CEIL_DIV_EXPR:
1464 case FLOOR_DIV_EXPR:
1465 case ROUND_DIV_EXPR:
1466 if (flag_complex_method == 0)
1468 /* Keep this algorithm in sync with
1469 tree-complex.cc:expand_complex_div_straight().
1471 Expand complex division to scalars, straightforward algorithm.
1472 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1473 t = br*br + bi*bi
1475 tree magsquared
1476 = const_binop (PLUS_EXPR,
1477 const_binop (MULT_EXPR, r2, r2),
1478 const_binop (MULT_EXPR, i2, i2));
1479 tree t1
1480 = const_binop (PLUS_EXPR,
1481 const_binop (MULT_EXPR, r1, r2),
1482 const_binop (MULT_EXPR, i1, i2));
1483 tree t2
1484 = const_binop (MINUS_EXPR,
1485 const_binop (MULT_EXPR, i1, r2),
1486 const_binop (MULT_EXPR, r1, i2));
1488 real = const_binop (code, t1, magsquared);
1489 imag = const_binop (code, t2, magsquared);
1491 else
1493 /* Keep this algorithm in sync with
1494 tree-complex.cc:expand_complex_div_wide().
1496 Expand complex division to scalars, modified algorithm to minimize
1497 overflow with wide input ranges. */
1498 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1499 fold_abs_const (r2, TREE_TYPE (type)),
1500 fold_abs_const (i2, TREE_TYPE (type)));
1502 if (integer_nonzerop (compare))
1504 /* In the TRUE branch, we compute
1505 ratio = br/bi;
1506 div = (br * ratio) + bi;
1507 tr = (ar * ratio) + ai;
1508 ti = (ai * ratio) - ar;
1509 tr = tr / div;
1510 ti = ti / div; */
1511 tree ratio = const_binop (code, r2, i2);
1512 tree div = const_binop (PLUS_EXPR, i2,
1513 const_binop (MULT_EXPR, r2, ratio));
1514 real = const_binop (MULT_EXPR, r1, ratio);
1515 real = const_binop (PLUS_EXPR, real, i1);
1516 real = const_binop (code, real, div);
1518 imag = const_binop (MULT_EXPR, i1, ratio);
1519 imag = const_binop (MINUS_EXPR, imag, r1);
1520 imag = const_binop (code, imag, div);
1522 else
1524 /* In the FALSE branch, we compute
1525 ratio = d/c;
1526 divisor = (d * ratio) + c;
1527 tr = (b * ratio) + a;
1528 ti = b - (a * ratio);
1529 tr = tr / div;
1530 ti = ti / div; */
1531 tree ratio = const_binop (code, i2, r2);
1532 tree div = const_binop (PLUS_EXPR, r2,
1533 const_binop (MULT_EXPR, i2, ratio));
1535 real = const_binop (MULT_EXPR, i1, ratio);
1536 real = const_binop (PLUS_EXPR, real, r1);
1537 real = const_binop (code, real, div);
1539 imag = const_binop (MULT_EXPR, r1, ratio);
1540 imag = const_binop (MINUS_EXPR, i1, imag);
1541 imag = const_binop (code, imag, div);
1544 break;
1546 default:
1547 return NULL_TREE;
1550 if (real && imag)
1551 return build_complex (type, real, imag);
1554 if (TREE_CODE (arg1) == VECTOR_CST
1555 && TREE_CODE (arg2) == VECTOR_CST
1556 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1557 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1559 tree type = TREE_TYPE (arg1);
1560 bool step_ok_p;
1561 if (VECTOR_CST_STEPPED_P (arg1)
1562 && VECTOR_CST_STEPPED_P (arg2))
1563 /* We can operate directly on the encoding if:
1565 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1566 implies
1567 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1569 Addition and subtraction are the supported operators
1570 for which this is true. */
1571 step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1572 else if (VECTOR_CST_STEPPED_P (arg1))
1573 /* We can operate directly on stepped encodings if:
1575 a3 - a2 == a2 - a1
1576 implies:
1577 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1579 which is true if (x -> x op c) distributes over addition. */
1580 step_ok_p = distributes_over_addition_p (code, 1);
1581 else
1582 /* Similarly in reverse. */
1583 step_ok_p = distributes_over_addition_p (code, 2);
1584 tree_vector_builder elts;
1585 if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1586 return NULL_TREE;
1587 unsigned int count = elts.encoded_nelts ();
1588 for (unsigned int i = 0; i < count; ++i)
1590 tree elem1 = VECTOR_CST_ELT (arg1, i);
1591 tree elem2 = VECTOR_CST_ELT (arg2, i);
1593 tree elt = const_binop (code, elem1, elem2);
1595 /* It is possible that const_binop cannot handle the given
1596 code and return NULL_TREE */
1597 if (elt == NULL_TREE)
1598 return NULL_TREE;
1599 elts.quick_push (elt);
1602 return elts.build ();
1605 /* Shifts allow a scalar offset for a vector. */
1606 if (TREE_CODE (arg1) == VECTOR_CST
1607 && TREE_CODE (arg2) == INTEGER_CST)
1609 tree type = TREE_TYPE (arg1);
1610 bool step_ok_p = distributes_over_addition_p (code, 1);
1611 tree_vector_builder elts;
1612 if (!elts.new_unary_operation (type, arg1, step_ok_p))
1613 return NULL_TREE;
1614 unsigned int count = elts.encoded_nelts ();
1615 for (unsigned int i = 0; i < count; ++i)
1617 tree elem1 = VECTOR_CST_ELT (arg1, i);
1619 tree elt = const_binop (code, elem1, arg2);
1621 /* It is possible that const_binop cannot handle the given
1622 code and return NULL_TREE. */
1623 if (elt == NULL_TREE)
1624 return NULL_TREE;
1625 elts.quick_push (elt);
1628 return elts.build ();
1630 return NULL_TREE;
1633 /* Overload that adds a TYPE parameter to be able to dispatch
1634 to fold_relational_const. */
1636 tree
1637 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1639 if (TREE_CODE_CLASS (code) == tcc_comparison)
1640 return fold_relational_const (code, type, arg1, arg2);
1642 /* ??? Until we make the const_binop worker take the type of the
1643 result as argument put those cases that need it here. */
1644 switch (code)
1646 case VEC_SERIES_EXPR:
1647 if (CONSTANT_CLASS_P (arg1)
1648 && CONSTANT_CLASS_P (arg2))
1649 return build_vec_series (type, arg1, arg2);
1650 return NULL_TREE;
1652 case COMPLEX_EXPR:
1653 if ((TREE_CODE (arg1) == REAL_CST
1654 && TREE_CODE (arg2) == REAL_CST)
1655 || (TREE_CODE (arg1) == INTEGER_CST
1656 && TREE_CODE (arg2) == INTEGER_CST))
1657 return build_complex (type, arg1, arg2);
1658 return NULL_TREE;
1660 case POINTER_DIFF_EXPR:
1661 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1663 poly_offset_int res = (wi::to_poly_offset (arg1)
1664 - wi::to_poly_offset (arg2));
1665 return force_fit_type (type, res, 1,
1666 TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1668 return NULL_TREE;
1670 case VEC_PACK_TRUNC_EXPR:
1671 case VEC_PACK_FIX_TRUNC_EXPR:
1672 case VEC_PACK_FLOAT_EXPR:
1674 unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1676 if (TREE_CODE (arg1) != VECTOR_CST
1677 || TREE_CODE (arg2) != VECTOR_CST)
1678 return NULL_TREE;
1680 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1681 return NULL_TREE;
1683 out_nelts = in_nelts * 2;
1684 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1685 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1687 tree_vector_builder elts (type, out_nelts, 1);
1688 for (i = 0; i < out_nelts; i++)
1690 tree elt = (i < in_nelts
1691 ? VECTOR_CST_ELT (arg1, i)
1692 : VECTOR_CST_ELT (arg2, i - in_nelts));
1693 elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1694 ? NOP_EXPR
1695 : code == VEC_PACK_FLOAT_EXPR
1696 ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1697 TREE_TYPE (type), elt);
1698 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1699 return NULL_TREE;
1700 elts.quick_push (elt);
1703 return elts.build ();
1706 case VEC_WIDEN_MULT_LO_EXPR:
1707 case VEC_WIDEN_MULT_HI_EXPR:
1708 case VEC_WIDEN_MULT_EVEN_EXPR:
1709 case VEC_WIDEN_MULT_ODD_EXPR:
1711 unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1713 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1714 return NULL_TREE;
1716 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1717 return NULL_TREE;
1718 out_nelts = in_nelts / 2;
1719 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1720 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1722 if (code == VEC_WIDEN_MULT_LO_EXPR)
1723 scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1724 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1725 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1726 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1727 scale = 1, ofs = 0;
1728 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1729 scale = 1, ofs = 1;
1731 tree_vector_builder elts (type, out_nelts, 1);
1732 for (out = 0; out < out_nelts; out++)
1734 unsigned int in = (out << scale) + ofs;
1735 tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1736 VECTOR_CST_ELT (arg1, in));
1737 tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1738 VECTOR_CST_ELT (arg2, in));
1740 if (t1 == NULL_TREE || t2 == NULL_TREE)
1741 return NULL_TREE;
1742 tree elt = const_binop (MULT_EXPR, t1, t2);
1743 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1744 return NULL_TREE;
1745 elts.quick_push (elt);
1748 return elts.build ();
1751 default:;
1754 if (TREE_CODE_CLASS (code) != tcc_binary)
1755 return NULL_TREE;
1757 /* Make sure type and arg0 have the same saturating flag. */
1758 gcc_checking_assert (TYPE_SATURATING (type)
1759 == TYPE_SATURATING (TREE_TYPE (arg1)));
1761 return const_binop (code, arg1, arg2);
1764 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1765 Return zero if computing the constants is not possible. */
1767 tree
1768 const_unop (enum tree_code code, tree type, tree arg0)
1770 /* Don't perform the operation, other than NEGATE and ABS, if
1771 flag_signaling_nans is on and the operand is a signaling NaN. */
1772 if (TREE_CODE (arg0) == REAL_CST
1773 && HONOR_SNANS (arg0)
1774 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1775 && code != NEGATE_EXPR
1776 && code != ABS_EXPR
1777 && code != ABSU_EXPR)
1778 return NULL_TREE;
1780 switch (code)
1782 CASE_CONVERT:
1783 case FLOAT_EXPR:
1784 case FIX_TRUNC_EXPR:
1785 case FIXED_CONVERT_EXPR:
1786 return fold_convert_const (code, type, arg0);
1788 case ADDR_SPACE_CONVERT_EXPR:
1789 /* If the source address is 0, and the source address space
1790 cannot have a valid object at 0, fold to dest type null. */
1791 if (integer_zerop (arg0)
1792 && !(targetm.addr_space.zero_address_valid
1793 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1794 return fold_convert_const (code, type, arg0);
1795 break;
1797 case VIEW_CONVERT_EXPR:
1798 return fold_view_convert_expr (type, arg0);
1800 case NEGATE_EXPR:
1802 /* Can't call fold_negate_const directly here as that doesn't
1803 handle all cases and we might not be able to negate some
1804 constants. */
1805 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1806 if (tem && CONSTANT_CLASS_P (tem))
1807 return tem;
1808 break;
1811 case ABS_EXPR:
1812 case ABSU_EXPR:
1813 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1814 return fold_abs_const (arg0, type);
1815 break;
1817 case CONJ_EXPR:
1818 if (TREE_CODE (arg0) == COMPLEX_CST)
1820 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1821 TREE_TYPE (type));
1822 return build_complex (type, TREE_REALPART (arg0), ipart);
1824 break;
1826 case BIT_NOT_EXPR:
1827 if (TREE_CODE (arg0) == INTEGER_CST)
1828 return fold_not_const (arg0, type);
1829 else if (POLY_INT_CST_P (arg0))
1830 return wide_int_to_tree (type, -poly_int_cst_value (arg0));
1831 /* Perform BIT_NOT_EXPR on each element individually. */
1832 else if (TREE_CODE (arg0) == VECTOR_CST)
1834 tree elem;
1836 /* This can cope with stepped encodings because ~x == -1 - x. */
1837 tree_vector_builder elements;
1838 elements.new_unary_operation (type, arg0, true);
1839 unsigned int i, count = elements.encoded_nelts ();
1840 for (i = 0; i < count; ++i)
1842 elem = VECTOR_CST_ELT (arg0, i);
1843 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1844 if (elem == NULL_TREE)
1845 break;
1846 elements.quick_push (elem);
1848 if (i == count)
1849 return elements.build ();
1851 break;
1853 case TRUTH_NOT_EXPR:
1854 if (TREE_CODE (arg0) == INTEGER_CST)
1855 return constant_boolean_node (integer_zerop (arg0), type);
1856 break;
1858 case REALPART_EXPR:
1859 if (TREE_CODE (arg0) == COMPLEX_CST)
1860 return fold_convert (type, TREE_REALPART (arg0));
1861 break;
1863 case IMAGPART_EXPR:
1864 if (TREE_CODE (arg0) == COMPLEX_CST)
1865 return fold_convert (type, TREE_IMAGPART (arg0));
1866 break;
1868 case VEC_UNPACK_LO_EXPR:
1869 case VEC_UNPACK_HI_EXPR:
1870 case VEC_UNPACK_FLOAT_LO_EXPR:
1871 case VEC_UNPACK_FLOAT_HI_EXPR:
1872 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1873 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1875 unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
1876 enum tree_code subcode;
1878 if (TREE_CODE (arg0) != VECTOR_CST)
1879 return NULL_TREE;
1881 if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
1882 return NULL_TREE;
1883 out_nelts = in_nelts / 2;
1884 gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1886 unsigned int offset = 0;
1887 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1888 || code == VEC_UNPACK_FLOAT_LO_EXPR
1889 || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
1890 offset = out_nelts;
1892 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1893 subcode = NOP_EXPR;
1894 else if (code == VEC_UNPACK_FLOAT_LO_EXPR
1895 || code == VEC_UNPACK_FLOAT_HI_EXPR)
1896 subcode = FLOAT_EXPR;
1897 else
1898 subcode = FIX_TRUNC_EXPR;
1900 tree_vector_builder elts (type, out_nelts, 1);
1901 for (i = 0; i < out_nelts; i++)
1903 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1904 VECTOR_CST_ELT (arg0, i + offset));
1905 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1906 return NULL_TREE;
1907 elts.quick_push (elt);
1910 return elts.build ();
1913 case VEC_DUPLICATE_EXPR:
1914 if (CONSTANT_CLASS_P (arg0))
1915 return build_vector_from_val (type, arg0);
1916 return NULL_TREE;
1918 default:
1919 break;
1922 return NULL_TREE;
1925 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1926 indicates which particular sizetype to create. */
1928 tree
1929 size_int_kind (poly_int64 number, enum size_type_kind kind)
1931 return build_int_cst (sizetype_tab[(int) kind], number);
1934 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1935 is a tree code. The type of the result is taken from the operands.
1936 Both must be equivalent integer types, ala int_binop_types_match_p.
1937 If the operands are constant, so is the result. */
1939 tree
1940 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1942 tree type = TREE_TYPE (arg0);
1944 if (arg0 == error_mark_node || arg1 == error_mark_node)
1945 return error_mark_node;
1947 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1948 TREE_TYPE (arg1)));
1950 /* Handle the special case of two poly_int constants faster. */
1951 if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1953 /* And some specific cases even faster than that. */
1954 if (code == PLUS_EXPR)
1956 if (integer_zerop (arg0)
1957 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
1958 return arg1;
1959 if (integer_zerop (arg1)
1960 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
1961 return arg0;
1963 else if (code == MINUS_EXPR)
1965 if (integer_zerop (arg1)
1966 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
1967 return arg0;
1969 else if (code == MULT_EXPR)
1971 if (integer_onep (arg0)
1972 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
1973 return arg1;
1976 /* Handle general case of two integer constants. For sizetype
1977 constant calculations we always want to know about overflow,
1978 even in the unsigned case. */
1979 tree res = int_const_binop (code, arg0, arg1, -1);
1980 if (res != NULL_TREE)
1981 return res;
1984 return fold_build2_loc (loc, code, type, arg0, arg1);
1987 /* Given two values, either both of sizetype or both of bitsizetype,
1988 compute the difference between the two values. Return the value
1989 in signed type corresponding to the type of the operands. */
1991 tree
1992 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1994 tree type = TREE_TYPE (arg0);
1995 tree ctype;
1997 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1998 TREE_TYPE (arg1)));
2000 /* If the type is already signed, just do the simple thing. */
2001 if (!TYPE_UNSIGNED (type))
2002 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2004 if (type == sizetype)
2005 ctype = ssizetype;
2006 else if (type == bitsizetype)
2007 ctype = sbitsizetype;
2008 else
2009 ctype = signed_type_for (type);
2011 /* If either operand is not a constant, do the conversions to the signed
2012 type and subtract. The hardware will do the right thing with any
2013 overflow in the subtraction. */
2014 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2015 return size_binop_loc (loc, MINUS_EXPR,
2016 fold_convert_loc (loc, ctype, arg0),
2017 fold_convert_loc (loc, ctype, arg1));
2019 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2020 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2021 overflow) and negate (which can't either). Special-case a result
2022 of zero while we're here. */
2023 if (tree_int_cst_equal (arg0, arg1))
2024 return build_int_cst (ctype, 0);
2025 else if (tree_int_cst_lt (arg1, arg0))
2026 return fold_convert_loc (loc, ctype,
2027 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2028 else
2029 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2030 fold_convert_loc (loc, ctype,
2031 size_binop_loc (loc,
2032 MINUS_EXPR,
2033 arg1, arg0)));
2036 /* A subroutine of fold_convert_const handling conversions of an
2037 INTEGER_CST to another integer type. */
2039 static tree
2040 fold_convert_const_int_from_int (tree type, const_tree arg1)
2042 /* Given an integer constant, make new constant with new type,
2043 appropriately sign-extended or truncated. Use widest_int
2044 so that any extension is done according ARG1's type. */
2045 return force_fit_type (type, wi::to_widest (arg1),
2046 !POINTER_TYPE_P (TREE_TYPE (arg1)),
2047 TREE_OVERFLOW (arg1));
2050 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2051 to an integer type. */
2053 static tree
2054 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2056 bool overflow = false;
2057 tree t;
2059 /* The following code implements the floating point to integer
2060 conversion rules required by the Java Language Specification,
2061 that IEEE NaNs are mapped to zero and values that overflow
2062 the target precision saturate, i.e. values greater than
2063 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2064 are mapped to INT_MIN. These semantics are allowed by the
2065 C and C++ standards that simply state that the behavior of
2066 FP-to-integer conversion is unspecified upon overflow. */
2068 wide_int val;
2069 REAL_VALUE_TYPE r;
2070 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2072 switch (code)
2074 case FIX_TRUNC_EXPR:
2075 real_trunc (&r, VOIDmode, &x);
2076 break;
2078 default:
2079 gcc_unreachable ();
2082 /* If R is NaN, return zero and show we have an overflow. */
2083 if (REAL_VALUE_ISNAN (r))
2085 overflow = true;
2086 val = wi::zero (TYPE_PRECISION (type));
2089 /* See if R is less than the lower bound or greater than the
2090 upper bound. */
2092 if (! overflow)
2094 tree lt = TYPE_MIN_VALUE (type);
2095 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2096 if (real_less (&r, &l))
2098 overflow = true;
2099 val = wi::to_wide (lt);
2103 if (! overflow)
2105 tree ut = TYPE_MAX_VALUE (type);
2106 if (ut)
2108 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2109 if (real_less (&u, &r))
2111 overflow = true;
2112 val = wi::to_wide (ut);
2117 if (! overflow)
2118 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2120 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2121 return t;
2124 /* A subroutine of fold_convert_const handling conversions of a
2125 FIXED_CST to an integer type. */
2127 static tree
2128 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2130 tree t;
2131 double_int temp, temp_trunc;
2132 scalar_mode mode;
2134 /* Right shift FIXED_CST to temp by fbit. */
2135 temp = TREE_FIXED_CST (arg1).data;
2136 mode = TREE_FIXED_CST (arg1).mode;
2137 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2139 temp = temp.rshift (GET_MODE_FBIT (mode),
2140 HOST_BITS_PER_DOUBLE_INT,
2141 SIGNED_FIXED_POINT_MODE_P (mode));
2143 /* Left shift temp to temp_trunc by fbit. */
2144 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2145 HOST_BITS_PER_DOUBLE_INT,
2146 SIGNED_FIXED_POINT_MODE_P (mode));
2148 else
2150 temp = double_int_zero;
2151 temp_trunc = double_int_zero;
2154 /* If FIXED_CST is negative, we need to round the value toward 0.
2155 By checking if the fractional bits are not zero to add 1 to temp. */
2156 if (SIGNED_FIXED_POINT_MODE_P (mode)
2157 && temp_trunc.is_negative ()
2158 && TREE_FIXED_CST (arg1).data != temp_trunc)
2159 temp += double_int_one;
2161 /* Given a fixed-point constant, make new constant with new type,
2162 appropriately sign-extended or truncated. */
2163 t = force_fit_type (type, temp, -1,
2164 (temp.is_negative ()
2165 && (TYPE_UNSIGNED (type)
2166 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2167 | TREE_OVERFLOW (arg1));
2169 return t;
2172 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2173 to another floating point type. */
2175 static tree
2176 fold_convert_const_real_from_real (tree type, const_tree arg1)
2178 REAL_VALUE_TYPE value;
2179 tree t;
2181 /* If the underlying modes are the same, simply treat it as
2182 copy and rebuild with TREE_REAL_CST information and the
2183 given type. */
2184 if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (arg1)))
2186 t = build_real (type, TREE_REAL_CST (arg1));
2187 return t;
2190 /* Don't perform the operation if flag_signaling_nans is on
2191 and the operand is a signaling NaN. */
2192 if (HONOR_SNANS (arg1)
2193 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2194 return NULL_TREE;
2196 /* With flag_rounding_math we should respect the current rounding mode
2197 unless the conversion is exact. */
2198 if (HONOR_SIGN_DEPENDENT_ROUNDING (arg1)
2199 && !exact_real_truncate (TYPE_MODE (type), &TREE_REAL_CST (arg1)))
2200 return NULL_TREE;
2202 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2203 t = build_real (type, value);
2205 /* If converting an infinity or NAN to a representation that doesn't
2206 have one, set the overflow bit so that we can produce some kind of
2207 error message at the appropriate point if necessary. It's not the
2208 most user-friendly message, but it's better than nothing. */
2209 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2210 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2211 TREE_OVERFLOW (t) = 1;
2212 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2213 && !MODE_HAS_NANS (TYPE_MODE (type)))
2214 TREE_OVERFLOW (t) = 1;
2215 /* Regular overflow, conversion produced an infinity in a mode that
2216 can't represent them. */
2217 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2218 && REAL_VALUE_ISINF (value)
2219 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2220 TREE_OVERFLOW (t) = 1;
2221 else
2222 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2223 return t;
2226 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2227 to a floating point type. */
2229 static tree
2230 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2232 REAL_VALUE_TYPE value;
2233 tree t;
2235 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2236 &TREE_FIXED_CST (arg1));
2237 t = build_real (type, value);
2239 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2240 return t;
2243 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2244 to another fixed-point type. */
2246 static tree
2247 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2249 FIXED_VALUE_TYPE value;
2250 tree t;
2251 bool overflow_p;
2253 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2254 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2255 t = build_fixed (type, value);
2257 /* Propagate overflow flags. */
2258 if (overflow_p | TREE_OVERFLOW (arg1))
2259 TREE_OVERFLOW (t) = 1;
2260 return t;
2263 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2264 to a fixed-point type. */
2266 static tree
2267 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2269 FIXED_VALUE_TYPE value;
2270 tree t;
2271 bool overflow_p;
2272 double_int di;
2274 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2276 di.low = TREE_INT_CST_ELT (arg1, 0);
2277 if (TREE_INT_CST_NUNITS (arg1) == 1)
2278 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2279 else
2280 di.high = TREE_INT_CST_ELT (arg1, 1);
2282 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2283 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2284 TYPE_SATURATING (type));
2285 t = build_fixed (type, value);
2287 /* Propagate overflow flags. */
2288 if (overflow_p | TREE_OVERFLOW (arg1))
2289 TREE_OVERFLOW (t) = 1;
2290 return t;
2293 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2294 to a fixed-point type. */
2296 static tree
2297 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2299 FIXED_VALUE_TYPE value;
2300 tree t;
2301 bool overflow_p;
2303 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2304 &TREE_REAL_CST (arg1),
2305 TYPE_SATURATING (type));
2306 t = build_fixed (type, value);
2308 /* Propagate overflow flags. */
2309 if (overflow_p | TREE_OVERFLOW (arg1))
2310 TREE_OVERFLOW (t) = 1;
2311 return t;
2314 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2315 type TYPE. If no simplification can be done return NULL_TREE. */
2317 static tree
2318 fold_convert_const (enum tree_code code, tree type, tree arg1)
2320 tree arg_type = TREE_TYPE (arg1);
2321 if (arg_type == type)
2322 return arg1;
2324 /* We can't widen types, since the runtime value could overflow the
2325 original type before being extended to the new type. */
2326 if (POLY_INT_CST_P (arg1)
2327 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2328 && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2329 return build_poly_int_cst (type,
2330 poly_wide_int::from (poly_int_cst_value (arg1),
2331 TYPE_PRECISION (type),
2332 TYPE_SIGN (arg_type)));
2334 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2335 || TREE_CODE (type) == OFFSET_TYPE)
2337 if (TREE_CODE (arg1) == INTEGER_CST)
2338 return fold_convert_const_int_from_int (type, arg1);
2339 else if (TREE_CODE (arg1) == REAL_CST)
2340 return fold_convert_const_int_from_real (code, type, arg1);
2341 else if (TREE_CODE (arg1) == FIXED_CST)
2342 return fold_convert_const_int_from_fixed (type, arg1);
2344 else if (TREE_CODE (type) == REAL_TYPE)
2346 if (TREE_CODE (arg1) == INTEGER_CST)
2348 tree res = build_real_from_int_cst (type, arg1);
2349 /* Avoid the folding if flag_rounding_math is on and the
2350 conversion is not exact. */
2351 if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
2353 bool fail = false;
2354 wide_int w = real_to_integer (&TREE_REAL_CST (res), &fail,
2355 TYPE_PRECISION (TREE_TYPE (arg1)));
2356 if (fail || wi::ne_p (w, wi::to_wide (arg1)))
2357 return NULL_TREE;
2359 return res;
2361 else if (TREE_CODE (arg1) == REAL_CST)
2362 return fold_convert_const_real_from_real (type, arg1);
2363 else if (TREE_CODE (arg1) == FIXED_CST)
2364 return fold_convert_const_real_from_fixed (type, arg1);
2366 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2368 if (TREE_CODE (arg1) == FIXED_CST)
2369 return fold_convert_const_fixed_from_fixed (type, arg1);
2370 else if (TREE_CODE (arg1) == INTEGER_CST)
2371 return fold_convert_const_fixed_from_int (type, arg1);
2372 else if (TREE_CODE (arg1) == REAL_CST)
2373 return fold_convert_const_fixed_from_real (type, arg1);
2375 else if (TREE_CODE (type) == VECTOR_TYPE)
2377 if (TREE_CODE (arg1) == VECTOR_CST
2378 && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2380 tree elttype = TREE_TYPE (type);
2381 tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2382 /* We can't handle steps directly when extending, since the
2383 values need to wrap at the original precision first. */
2384 bool step_ok_p
2385 = (INTEGRAL_TYPE_P (elttype)
2386 && INTEGRAL_TYPE_P (arg1_elttype)
2387 && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2388 tree_vector_builder v;
2389 if (!v.new_unary_operation (type, arg1, step_ok_p))
2390 return NULL_TREE;
2391 unsigned int len = v.encoded_nelts ();
2392 for (unsigned int i = 0; i < len; ++i)
2394 tree elt = VECTOR_CST_ELT (arg1, i);
2395 tree cvt = fold_convert_const (code, elttype, elt);
2396 if (cvt == NULL_TREE)
2397 return NULL_TREE;
2398 v.quick_push (cvt);
2400 return v.build ();
2403 return NULL_TREE;
2406 /* Construct a vector of zero elements of vector type TYPE. */
2408 static tree
2409 build_zero_vector (tree type)
2411 tree t;
2413 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2414 return build_vector_from_val (type, t);
2417 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2419 bool
2420 fold_convertible_p (const_tree type, const_tree arg)
2422 const_tree orig = TREE_TYPE (arg);
2424 if (type == orig)
2425 return true;
2427 if (TREE_CODE (arg) == ERROR_MARK
2428 || TREE_CODE (type) == ERROR_MARK
2429 || TREE_CODE (orig) == ERROR_MARK)
2430 return false;
2432 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2433 return true;
2435 switch (TREE_CODE (type))
2437 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2438 case POINTER_TYPE: case REFERENCE_TYPE:
2439 case OFFSET_TYPE:
2440 return (INTEGRAL_TYPE_P (orig)
2441 || (POINTER_TYPE_P (orig)
2442 && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2443 || TREE_CODE (orig) == OFFSET_TYPE);
2445 case REAL_TYPE:
2446 case FIXED_POINT_TYPE:
2447 case VOID_TYPE:
2448 return TREE_CODE (type) == TREE_CODE (orig);
2450 case VECTOR_TYPE:
2451 return (VECTOR_TYPE_P (orig)
2452 && known_eq (TYPE_VECTOR_SUBPARTS (type),
2453 TYPE_VECTOR_SUBPARTS (orig))
2454 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2456 default:
2457 return false;
2461 /* Convert expression ARG to type TYPE. Used by the middle-end for
2462 simple conversions in preference to calling the front-end's convert. */
2464 tree
2465 fold_convert_loc (location_t loc, tree type, tree arg)
2467 tree orig = TREE_TYPE (arg);
2468 tree tem;
2470 if (type == orig)
2471 return arg;
2473 if (TREE_CODE (arg) == ERROR_MARK
2474 || TREE_CODE (type) == ERROR_MARK
2475 || TREE_CODE (orig) == ERROR_MARK)
2476 return error_mark_node;
2478 switch (TREE_CODE (type))
2480 case POINTER_TYPE:
2481 case REFERENCE_TYPE:
2482 /* Handle conversions between pointers to different address spaces. */
2483 if (POINTER_TYPE_P (orig)
2484 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2485 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2486 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2487 /* fall through */
2489 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2490 case OFFSET_TYPE:
2491 if (TREE_CODE (arg) == INTEGER_CST)
2493 tem = fold_convert_const (NOP_EXPR, type, arg);
2494 if (tem != NULL_TREE)
2495 return tem;
2497 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2498 || TREE_CODE (orig) == OFFSET_TYPE)
2499 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2500 if (TREE_CODE (orig) == COMPLEX_TYPE)
2501 return fold_convert_loc (loc, type,
2502 fold_build1_loc (loc, REALPART_EXPR,
2503 TREE_TYPE (orig), arg));
2504 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2505 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2506 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2508 case REAL_TYPE:
2509 if (TREE_CODE (arg) == INTEGER_CST)
2511 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2512 if (tem != NULL_TREE)
2513 return tem;
2515 else if (TREE_CODE (arg) == REAL_CST)
2517 tem = fold_convert_const (NOP_EXPR, type, arg);
2518 if (tem != NULL_TREE)
2519 return tem;
2521 else if (TREE_CODE (arg) == FIXED_CST)
2523 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2524 if (tem != NULL_TREE)
2525 return tem;
2528 switch (TREE_CODE (orig))
2530 case INTEGER_TYPE:
2531 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2532 case POINTER_TYPE: case REFERENCE_TYPE:
2533 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2535 case REAL_TYPE:
2536 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2538 case FIXED_POINT_TYPE:
2539 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2541 case COMPLEX_TYPE:
2542 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2543 return fold_convert_loc (loc, type, tem);
2545 default:
2546 gcc_unreachable ();
2549 case FIXED_POINT_TYPE:
2550 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2551 || TREE_CODE (arg) == REAL_CST)
2553 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2554 if (tem != NULL_TREE)
2555 goto fold_convert_exit;
2558 switch (TREE_CODE (orig))
2560 case FIXED_POINT_TYPE:
2561 case INTEGER_TYPE:
2562 case ENUMERAL_TYPE:
2563 case BOOLEAN_TYPE:
2564 case REAL_TYPE:
2565 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2567 case COMPLEX_TYPE:
2568 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2569 return fold_convert_loc (loc, type, tem);
2571 default:
2572 gcc_unreachable ();
2575 case COMPLEX_TYPE:
2576 switch (TREE_CODE (orig))
2578 case INTEGER_TYPE:
2579 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2580 case POINTER_TYPE: case REFERENCE_TYPE:
2581 case REAL_TYPE:
2582 case FIXED_POINT_TYPE:
2583 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2584 fold_convert_loc (loc, TREE_TYPE (type), arg),
2585 fold_convert_loc (loc, TREE_TYPE (type),
2586 integer_zero_node));
2587 case COMPLEX_TYPE:
2589 tree rpart, ipart;
2591 if (TREE_CODE (arg) == COMPLEX_EXPR)
2593 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2594 TREE_OPERAND (arg, 0));
2595 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2596 TREE_OPERAND (arg, 1));
2597 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2600 arg = save_expr (arg);
2601 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2602 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2603 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2604 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2605 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2608 default:
2609 gcc_unreachable ();
2612 case VECTOR_TYPE:
2613 if (integer_zerop (arg))
2614 return build_zero_vector (type);
2615 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2616 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2617 || TREE_CODE (orig) == VECTOR_TYPE);
2618 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2620 case VOID_TYPE:
2621 tem = fold_ignored_result (arg);
2622 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2624 default:
2625 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2626 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2627 gcc_unreachable ();
2629 fold_convert_exit:
2630 tem = protected_set_expr_location_unshare (tem, loc);
2631 return tem;
2634 /* Return false if expr can be assumed not to be an lvalue, true
2635 otherwise. */
2637 static bool
2638 maybe_lvalue_p (const_tree x)
2640 /* We only need to wrap lvalue tree codes. */
2641 switch (TREE_CODE (x))
2643 case VAR_DECL:
2644 case PARM_DECL:
2645 case RESULT_DECL:
2646 case LABEL_DECL:
2647 case FUNCTION_DECL:
2648 case SSA_NAME:
2650 case COMPONENT_REF:
2651 case MEM_REF:
2652 case INDIRECT_REF:
2653 case ARRAY_REF:
2654 case ARRAY_RANGE_REF:
2655 case BIT_FIELD_REF:
2656 case OBJ_TYPE_REF:
2658 case REALPART_EXPR:
2659 case IMAGPART_EXPR:
2660 case PREINCREMENT_EXPR:
2661 case PREDECREMENT_EXPR:
2662 case SAVE_EXPR:
2663 case TRY_CATCH_EXPR:
2664 case WITH_CLEANUP_EXPR:
2665 case COMPOUND_EXPR:
2666 case MODIFY_EXPR:
2667 case TARGET_EXPR:
2668 case COND_EXPR:
2669 case BIND_EXPR:
2670 case VIEW_CONVERT_EXPR:
2671 break;
2673 default:
2674 /* Assume the worst for front-end tree codes. */
2675 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2676 break;
2677 return false;
2680 return true;
2683 /* Return an expr equal to X but certainly not valid as an lvalue. */
2685 tree
2686 non_lvalue_loc (location_t loc, tree x)
2688 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2689 us. */
2690 if (in_gimple_form)
2691 return x;
2693 if (! maybe_lvalue_p (x))
2694 return x;
2695 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2698 /* Given a tree comparison code, return the code that is the logical inverse.
2699 It is generally not safe to do this for floating-point comparisons, except
2700 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2701 ERROR_MARK in this case. */
2703 enum tree_code
2704 invert_tree_comparison (enum tree_code code, bool honor_nans)
2706 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2707 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2708 return ERROR_MARK;
2710 switch (code)
2712 case EQ_EXPR:
2713 return NE_EXPR;
2714 case NE_EXPR:
2715 return EQ_EXPR;
2716 case GT_EXPR:
2717 return honor_nans ? UNLE_EXPR : LE_EXPR;
2718 case GE_EXPR:
2719 return honor_nans ? UNLT_EXPR : LT_EXPR;
2720 case LT_EXPR:
2721 return honor_nans ? UNGE_EXPR : GE_EXPR;
2722 case LE_EXPR:
2723 return honor_nans ? UNGT_EXPR : GT_EXPR;
2724 case LTGT_EXPR:
2725 return UNEQ_EXPR;
2726 case UNEQ_EXPR:
2727 return LTGT_EXPR;
2728 case UNGT_EXPR:
2729 return LE_EXPR;
2730 case UNGE_EXPR:
2731 return LT_EXPR;
2732 case UNLT_EXPR:
2733 return GE_EXPR;
2734 case UNLE_EXPR:
2735 return GT_EXPR;
2736 case ORDERED_EXPR:
2737 return UNORDERED_EXPR;
2738 case UNORDERED_EXPR:
2739 return ORDERED_EXPR;
2740 default:
2741 gcc_unreachable ();
2745 /* Similar, but return the comparison that results if the operands are
2746 swapped. This is safe for floating-point. */
2748 enum tree_code
2749 swap_tree_comparison (enum tree_code code)
2751 switch (code)
2753 case EQ_EXPR:
2754 case NE_EXPR:
2755 case ORDERED_EXPR:
2756 case UNORDERED_EXPR:
2757 case LTGT_EXPR:
2758 case UNEQ_EXPR:
2759 return code;
2760 case GT_EXPR:
2761 return LT_EXPR;
2762 case GE_EXPR:
2763 return LE_EXPR;
2764 case LT_EXPR:
2765 return GT_EXPR;
2766 case LE_EXPR:
2767 return GE_EXPR;
2768 case UNGT_EXPR:
2769 return UNLT_EXPR;
2770 case UNGE_EXPR:
2771 return UNLE_EXPR;
2772 case UNLT_EXPR:
2773 return UNGT_EXPR;
2774 case UNLE_EXPR:
2775 return UNGE_EXPR;
2776 default:
2777 gcc_unreachable ();
2782 /* Convert a comparison tree code from an enum tree_code representation
2783 into a compcode bit-based encoding. This function is the inverse of
2784 compcode_to_comparison. */
2786 static enum comparison_code
2787 comparison_to_compcode (enum tree_code code)
2789 switch (code)
2791 case LT_EXPR:
2792 return COMPCODE_LT;
2793 case EQ_EXPR:
2794 return COMPCODE_EQ;
2795 case LE_EXPR:
2796 return COMPCODE_LE;
2797 case GT_EXPR:
2798 return COMPCODE_GT;
2799 case NE_EXPR:
2800 return COMPCODE_NE;
2801 case GE_EXPR:
2802 return COMPCODE_GE;
2803 case ORDERED_EXPR:
2804 return COMPCODE_ORD;
2805 case UNORDERED_EXPR:
2806 return COMPCODE_UNORD;
2807 case UNLT_EXPR:
2808 return COMPCODE_UNLT;
2809 case UNEQ_EXPR:
2810 return COMPCODE_UNEQ;
2811 case UNLE_EXPR:
2812 return COMPCODE_UNLE;
2813 case UNGT_EXPR:
2814 return COMPCODE_UNGT;
2815 case LTGT_EXPR:
2816 return COMPCODE_LTGT;
2817 case UNGE_EXPR:
2818 return COMPCODE_UNGE;
2819 default:
2820 gcc_unreachable ();
2824 /* Convert a compcode bit-based encoding of a comparison operator back
2825 to GCC's enum tree_code representation. This function is the
2826 inverse of comparison_to_compcode. */
2828 static enum tree_code
2829 compcode_to_comparison (enum comparison_code code)
2831 switch (code)
2833 case COMPCODE_LT:
2834 return LT_EXPR;
2835 case COMPCODE_EQ:
2836 return EQ_EXPR;
2837 case COMPCODE_LE:
2838 return LE_EXPR;
2839 case COMPCODE_GT:
2840 return GT_EXPR;
2841 case COMPCODE_NE:
2842 return NE_EXPR;
2843 case COMPCODE_GE:
2844 return GE_EXPR;
2845 case COMPCODE_ORD:
2846 return ORDERED_EXPR;
2847 case COMPCODE_UNORD:
2848 return UNORDERED_EXPR;
2849 case COMPCODE_UNLT:
2850 return UNLT_EXPR;
2851 case COMPCODE_UNEQ:
2852 return UNEQ_EXPR;
2853 case COMPCODE_UNLE:
2854 return UNLE_EXPR;
2855 case COMPCODE_UNGT:
2856 return UNGT_EXPR;
2857 case COMPCODE_LTGT:
2858 return LTGT_EXPR;
2859 case COMPCODE_UNGE:
2860 return UNGE_EXPR;
2861 default:
2862 gcc_unreachable ();
2866 /* Return true if COND1 tests the opposite condition of COND2. */
2868 bool
2869 inverse_conditions_p (const_tree cond1, const_tree cond2)
2871 return (COMPARISON_CLASS_P (cond1)
2872 && COMPARISON_CLASS_P (cond2)
2873 && (invert_tree_comparison
2874 (TREE_CODE (cond1),
2875 HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
2876 && operand_equal_p (TREE_OPERAND (cond1, 0),
2877 TREE_OPERAND (cond2, 0), 0)
2878 && operand_equal_p (TREE_OPERAND (cond1, 1),
2879 TREE_OPERAND (cond2, 1), 0));
2882 /* Return a tree for the comparison which is the combination of
2883 doing the AND or OR (depending on CODE) of the two operations LCODE
2884 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2885 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2886 if this makes the transformation invalid. */
2888 tree
2889 combine_comparisons (location_t loc,
2890 enum tree_code code, enum tree_code lcode,
2891 enum tree_code rcode, tree truth_type,
2892 tree ll_arg, tree lr_arg)
2894 bool honor_nans = HONOR_NANS (ll_arg);
2895 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2896 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2897 int compcode;
2899 switch (code)
2901 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2902 compcode = lcompcode & rcompcode;
2903 break;
2905 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2906 compcode = lcompcode | rcompcode;
2907 break;
2909 default:
2910 return NULL_TREE;
2913 if (!honor_nans)
2915 /* Eliminate unordered comparisons, as well as LTGT and ORD
2916 which are not used unless the mode has NaNs. */
2917 compcode &= ~COMPCODE_UNORD;
2918 if (compcode == COMPCODE_LTGT)
2919 compcode = COMPCODE_NE;
2920 else if (compcode == COMPCODE_ORD)
2921 compcode = COMPCODE_TRUE;
2923 else if (flag_trapping_math)
2925 /* Check that the original operation and the optimized ones will trap
2926 under the same condition. */
2927 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2928 && (lcompcode != COMPCODE_EQ)
2929 && (lcompcode != COMPCODE_ORD);
2930 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2931 && (rcompcode != COMPCODE_EQ)
2932 && (rcompcode != COMPCODE_ORD);
2933 bool trap = (compcode & COMPCODE_UNORD) == 0
2934 && (compcode != COMPCODE_EQ)
2935 && (compcode != COMPCODE_ORD);
2937 /* In a short-circuited boolean expression the LHS might be
2938 such that the RHS, if evaluated, will never trap. For
2939 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2940 if neither x nor y is NaN. (This is a mixed blessing: for
2941 example, the expression above will never trap, hence
2942 optimizing it to x < y would be invalid). */
2943 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2944 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2945 rtrap = false;
2947 /* If the comparison was short-circuited, and only the RHS
2948 trapped, we may now generate a spurious trap. */
2949 if (rtrap && !ltrap
2950 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2951 return NULL_TREE;
2953 /* If we changed the conditions that cause a trap, we lose. */
2954 if ((ltrap || rtrap) != trap)
2955 return NULL_TREE;
2958 if (compcode == COMPCODE_TRUE)
2959 return constant_boolean_node (true, truth_type);
2960 else if (compcode == COMPCODE_FALSE)
2961 return constant_boolean_node (false, truth_type);
2962 else
2964 enum tree_code tcode;
2966 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2967 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2971 /* Return nonzero if two operands (typically of the same tree node)
2972 are necessarily equal. FLAGS modifies behavior as follows:
2974 If OEP_ONLY_CONST is set, only return nonzero for constants.
2975 This function tests whether the operands are indistinguishable;
2976 it does not test whether they are equal using C's == operation.
2977 The distinction is important for IEEE floating point, because
2978 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2979 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2981 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2982 even though it may hold multiple values during a function.
2983 This is because a GCC tree node guarantees that nothing else is
2984 executed between the evaluation of its "operands" (which may often
2985 be evaluated in arbitrary order). Hence if the operands themselves
2986 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2987 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2988 unset means assuming isochronic (or instantaneous) tree equivalence.
2989 Unless comparing arbitrary expression trees, such as from different
2990 statements, this flag can usually be left unset.
2992 If OEP_PURE_SAME is set, then pure functions with identical arguments
2993 are considered the same. It is used when the caller has other ways
2994 to ensure that global memory is unchanged in between.
2996 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2997 not values of expressions.
2999 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
3000 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
3002 If OEP_BITWISE is set, then require the values to be bitwise identical
3003 rather than simply numerically equal. Do not take advantage of things
3004 like math-related flags or undefined behavior; only return true for
3005 values that are provably bitwise identical in all circumstances.
3007 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
3008 any operand with side effect. This is unnecesarily conservative in the
3009 case we know that arg0 and arg1 are in disjoint code paths (such as in
3010 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
3011 addresses with TREE_CONSTANT flag set so we know that &var == &var
3012 even if var is volatile. */
3014 bool
3015 operand_compare::operand_equal_p (const_tree arg0, const_tree arg1,
3016 unsigned int flags)
3018 bool r;
3019 if (verify_hash_value (arg0, arg1, flags, &r))
3020 return r;
3022 STRIP_ANY_LOCATION_WRAPPER (arg0);
3023 STRIP_ANY_LOCATION_WRAPPER (arg1);
3025 /* If either is ERROR_MARK, they aren't equal. */
3026 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
3027 || TREE_TYPE (arg0) == error_mark_node
3028 || TREE_TYPE (arg1) == error_mark_node)
3029 return false;
3031 /* Similar, if either does not have a type (like a template id),
3032 they aren't equal. */
3033 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
3034 return false;
3036 /* Bitwise identity makes no sense if the values have different layouts. */
3037 if ((flags & OEP_BITWISE)
3038 && !tree_nop_conversion_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3039 return false;
3041 /* We cannot consider pointers to different address space equal. */
3042 if (POINTER_TYPE_P (TREE_TYPE (arg0))
3043 && POINTER_TYPE_P (TREE_TYPE (arg1))
3044 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
3045 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
3046 return false;
3048 /* Check equality of integer constants before bailing out due to
3049 precision differences. */
3050 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3052 /* Address of INTEGER_CST is not defined; check that we did not forget
3053 to drop the OEP_ADDRESS_OF flags. */
3054 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3055 return tree_int_cst_equal (arg0, arg1);
3058 if (!(flags & OEP_ADDRESS_OF))
3060 /* If both types don't have the same signedness, then we can't consider
3061 them equal. We must check this before the STRIP_NOPS calls
3062 because they may change the signedness of the arguments. As pointers
3063 strictly don't have a signedness, require either two pointers or
3064 two non-pointers as well. */
3065 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3066 || POINTER_TYPE_P (TREE_TYPE (arg0))
3067 != POINTER_TYPE_P (TREE_TYPE (arg1)))
3068 return false;
3070 /* If both types don't have the same precision, then it is not safe
3071 to strip NOPs. */
3072 if (element_precision (TREE_TYPE (arg0))
3073 != element_precision (TREE_TYPE (arg1)))
3074 return false;
3076 STRIP_NOPS (arg0);
3077 STRIP_NOPS (arg1);
3079 #if 0
3080 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3081 sanity check once the issue is solved. */
3082 else
3083 /* Addresses of conversions and SSA_NAMEs (and many other things)
3084 are not defined. Check that we did not forget to drop the
3085 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3086 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3087 && TREE_CODE (arg0) != SSA_NAME);
3088 #endif
3090 /* In case both args are comparisons but with different comparison
3091 code, try to swap the comparison operands of one arg to produce
3092 a match and compare that variant. */
3093 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3094 && COMPARISON_CLASS_P (arg0)
3095 && COMPARISON_CLASS_P (arg1))
3097 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3099 if (TREE_CODE (arg0) == swap_code)
3100 return operand_equal_p (TREE_OPERAND (arg0, 0),
3101 TREE_OPERAND (arg1, 1), flags)
3102 && operand_equal_p (TREE_OPERAND (arg0, 1),
3103 TREE_OPERAND (arg1, 0), flags);
3106 if (TREE_CODE (arg0) != TREE_CODE (arg1))
3108 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3109 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3111 else if (flags & OEP_ADDRESS_OF)
3113 /* If we are interested in comparing addresses ignore
3114 MEM_REF wrappings of the base that can appear just for
3115 TBAA reasons. */
3116 if (TREE_CODE (arg0) == MEM_REF
3117 && DECL_P (arg1)
3118 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3119 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3120 && integer_zerop (TREE_OPERAND (arg0, 1)))
3121 return true;
3122 else if (TREE_CODE (arg1) == MEM_REF
3123 && DECL_P (arg0)
3124 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3125 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3126 && integer_zerop (TREE_OPERAND (arg1, 1)))
3127 return true;
3128 return false;
3130 else
3131 return false;
3134 /* When not checking adddresses, this is needed for conversions and for
3135 COMPONENT_REF. Might as well play it safe and always test this. */
3136 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3137 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3138 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
3139 && !(flags & OEP_ADDRESS_OF)))
3140 return false;
3142 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3143 We don't care about side effects in that case because the SAVE_EXPR
3144 takes care of that for us. In all other cases, two expressions are
3145 equal if they have no side effects. If we have two identical
3146 expressions with side effects that should be treated the same due
3147 to the only side effects being identical SAVE_EXPR's, that will
3148 be detected in the recursive calls below.
3149 If we are taking an invariant address of two identical objects
3150 they are necessarily equal as well. */
3151 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3152 && (TREE_CODE (arg0) == SAVE_EXPR
3153 || (flags & OEP_MATCH_SIDE_EFFECTS)
3154 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3155 return true;
3157 /* Next handle constant cases, those for which we can return 1 even
3158 if ONLY_CONST is set. */
3159 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3160 switch (TREE_CODE (arg0))
3162 case INTEGER_CST:
3163 return tree_int_cst_equal (arg0, arg1);
3165 case FIXED_CST:
3166 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3167 TREE_FIXED_CST (arg1));
3169 case REAL_CST:
3170 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3171 return true;
3173 if (!(flags & OEP_BITWISE) && !HONOR_SIGNED_ZEROS (arg0))
3175 /* If we do not distinguish between signed and unsigned zero,
3176 consider them equal. */
3177 if (real_zerop (arg0) && real_zerop (arg1))
3178 return true;
3180 return false;
3182 case VECTOR_CST:
3184 if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3185 != VECTOR_CST_LOG2_NPATTERNS (arg1))
3186 return false;
3188 if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3189 != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3190 return false;
3192 unsigned int count = vector_cst_encoded_nelts (arg0);
3193 for (unsigned int i = 0; i < count; ++i)
3194 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3195 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3196 return false;
3197 return true;
3200 case COMPLEX_CST:
3201 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3202 flags)
3203 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3204 flags));
3206 case STRING_CST:
3207 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3208 && ! memcmp (TREE_STRING_POINTER (arg0),
3209 TREE_STRING_POINTER (arg1),
3210 TREE_STRING_LENGTH (arg0)));
3212 case ADDR_EXPR:
3213 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3214 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3215 flags | OEP_ADDRESS_OF
3216 | OEP_MATCH_SIDE_EFFECTS);
3217 case CONSTRUCTOR:
3218 /* In GIMPLE empty constructors are allowed in initializers of
3219 aggregates. */
3220 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
3221 default:
3222 break;
3225 /* Don't handle more cases for OEP_BITWISE, since we can't guarantee that
3226 two instances of undefined behavior will give identical results. */
3227 if (flags & (OEP_ONLY_CONST | OEP_BITWISE))
3228 return false;
3230 /* Define macros to test an operand from arg0 and arg1 for equality and a
3231 variant that allows null and views null as being different from any
3232 non-null value. In the latter case, if either is null, the both
3233 must be; otherwise, do the normal comparison. */
3234 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3235 TREE_OPERAND (arg1, N), flags)
3237 #define OP_SAME_WITH_NULL(N) \
3238 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3239 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3241 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3243 case tcc_unary:
3244 /* Two conversions are equal only if signedness and modes match. */
3245 switch (TREE_CODE (arg0))
3247 CASE_CONVERT:
3248 case FIX_TRUNC_EXPR:
3249 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3250 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3251 return false;
3252 break;
3253 default:
3254 break;
3257 return OP_SAME (0);
3260 case tcc_comparison:
3261 case tcc_binary:
3262 if (OP_SAME (0) && OP_SAME (1))
3263 return true;
3265 /* For commutative ops, allow the other order. */
3266 return (commutative_tree_code (TREE_CODE (arg0))
3267 && operand_equal_p (TREE_OPERAND (arg0, 0),
3268 TREE_OPERAND (arg1, 1), flags)
3269 && operand_equal_p (TREE_OPERAND (arg0, 1),
3270 TREE_OPERAND (arg1, 0), flags));
3272 case tcc_reference:
3273 /* If either of the pointer (or reference) expressions we are
3274 dereferencing contain a side effect, these cannot be equal,
3275 but their addresses can be. */
3276 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3277 && (TREE_SIDE_EFFECTS (arg0)
3278 || TREE_SIDE_EFFECTS (arg1)))
3279 return false;
3281 switch (TREE_CODE (arg0))
3283 case INDIRECT_REF:
3284 if (!(flags & OEP_ADDRESS_OF))
3286 if (TYPE_ALIGN (TREE_TYPE (arg0))
3287 != TYPE_ALIGN (TREE_TYPE (arg1)))
3288 return false;
3289 /* Verify that the access types are compatible. */
3290 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg0))
3291 != TYPE_MAIN_VARIANT (TREE_TYPE (arg1)))
3292 return false;
3294 flags &= ~OEP_ADDRESS_OF;
3295 return OP_SAME (0);
3297 case IMAGPART_EXPR:
3298 /* Require the same offset. */
3299 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3300 TYPE_SIZE (TREE_TYPE (arg1)),
3301 flags & ~OEP_ADDRESS_OF))
3302 return false;
3304 /* Fallthru. */
3305 case REALPART_EXPR:
3306 case VIEW_CONVERT_EXPR:
3307 return OP_SAME (0);
3309 case TARGET_MEM_REF:
3310 case MEM_REF:
3311 if (!(flags & OEP_ADDRESS_OF))
3313 /* Require equal access sizes */
3314 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3315 && (!TYPE_SIZE (TREE_TYPE (arg0))
3316 || !TYPE_SIZE (TREE_TYPE (arg1))
3317 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3318 TYPE_SIZE (TREE_TYPE (arg1)),
3319 flags)))
3320 return false;
3321 /* Verify that access happens in similar types. */
3322 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3323 return false;
3324 /* Verify that accesses are TBAA compatible. */
3325 if (!alias_ptr_types_compatible_p
3326 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3327 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3328 || (MR_DEPENDENCE_CLIQUE (arg0)
3329 != MR_DEPENDENCE_CLIQUE (arg1))
3330 || (MR_DEPENDENCE_BASE (arg0)
3331 != MR_DEPENDENCE_BASE (arg1)))
3332 return false;
3333 /* Verify that alignment is compatible. */
3334 if (TYPE_ALIGN (TREE_TYPE (arg0))
3335 != TYPE_ALIGN (TREE_TYPE (arg1)))
3336 return false;
3338 flags &= ~OEP_ADDRESS_OF;
3339 return (OP_SAME (0) && OP_SAME (1)
3340 /* TARGET_MEM_REF require equal extra operands. */
3341 && (TREE_CODE (arg0) != TARGET_MEM_REF
3342 || (OP_SAME_WITH_NULL (2)
3343 && OP_SAME_WITH_NULL (3)
3344 && OP_SAME_WITH_NULL (4))));
3346 case ARRAY_REF:
3347 case ARRAY_RANGE_REF:
3348 if (!OP_SAME (0))
3349 return false;
3350 flags &= ~OEP_ADDRESS_OF;
3351 /* Compare the array index by value if it is constant first as we
3352 may have different types but same value here. */
3353 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3354 TREE_OPERAND (arg1, 1))
3355 || OP_SAME (1))
3356 && OP_SAME_WITH_NULL (2)
3357 && OP_SAME_WITH_NULL (3)
3358 /* Compare low bound and element size as with OEP_ADDRESS_OF
3359 we have to account for the offset of the ref. */
3360 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3361 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3362 || (operand_equal_p (array_ref_low_bound
3363 (CONST_CAST_TREE (arg0)),
3364 array_ref_low_bound
3365 (CONST_CAST_TREE (arg1)), flags)
3366 && operand_equal_p (array_ref_element_size
3367 (CONST_CAST_TREE (arg0)),
3368 array_ref_element_size
3369 (CONST_CAST_TREE (arg1)),
3370 flags))));
3372 case COMPONENT_REF:
3373 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3374 may be NULL when we're called to compare MEM_EXPRs. */
3375 if (!OP_SAME_WITH_NULL (0))
3376 return false;
3378 bool compare_address = flags & OEP_ADDRESS_OF;
3380 /* Most of time we only need to compare FIELD_DECLs for equality.
3381 However when determining address look into actual offsets.
3382 These may match for unions and unshared record types. */
3383 flags &= ~OEP_ADDRESS_OF;
3384 if (!OP_SAME (1))
3386 if (compare_address
3387 && (flags & OEP_ADDRESS_OF_SAME_FIELD) == 0)
3389 tree field0 = TREE_OPERAND (arg0, 1);
3390 tree field1 = TREE_OPERAND (arg1, 1);
3392 /* Non-FIELD_DECL operands can appear in C++ templates. */
3393 if (TREE_CODE (field0) != FIELD_DECL
3394 || TREE_CODE (field1) != FIELD_DECL
3395 || !operand_equal_p (DECL_FIELD_OFFSET (field0),
3396 DECL_FIELD_OFFSET (field1), flags)
3397 || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
3398 DECL_FIELD_BIT_OFFSET (field1),
3399 flags))
3400 return false;
3402 else
3403 return false;
3406 return OP_SAME_WITH_NULL (2);
3408 case BIT_FIELD_REF:
3409 if (!OP_SAME (0))
3410 return false;
3411 flags &= ~OEP_ADDRESS_OF;
3412 return OP_SAME (1) && OP_SAME (2);
3414 default:
3415 return false;
3418 case tcc_expression:
3419 switch (TREE_CODE (arg0))
3421 case ADDR_EXPR:
3422 /* Be sure we pass right ADDRESS_OF flag. */
3423 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3424 return operand_equal_p (TREE_OPERAND (arg0, 0),
3425 TREE_OPERAND (arg1, 0),
3426 flags | OEP_ADDRESS_OF);
3428 case TRUTH_NOT_EXPR:
3429 return OP_SAME (0);
3431 case TRUTH_ANDIF_EXPR:
3432 case TRUTH_ORIF_EXPR:
3433 return OP_SAME (0) && OP_SAME (1);
3435 case WIDEN_MULT_PLUS_EXPR:
3436 case WIDEN_MULT_MINUS_EXPR:
3437 if (!OP_SAME (2))
3438 return false;
3439 /* The multiplcation operands are commutative. */
3440 /* FALLTHRU */
3442 case TRUTH_AND_EXPR:
3443 case TRUTH_OR_EXPR:
3444 case TRUTH_XOR_EXPR:
3445 if (OP_SAME (0) && OP_SAME (1))
3446 return true;
3448 /* Otherwise take into account this is a commutative operation. */
3449 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3450 TREE_OPERAND (arg1, 1), flags)
3451 && operand_equal_p (TREE_OPERAND (arg0, 1),
3452 TREE_OPERAND (arg1, 0), flags));
3454 case COND_EXPR:
3455 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3456 return false;
3457 flags &= ~OEP_ADDRESS_OF;
3458 return OP_SAME (0);
3460 case BIT_INSERT_EXPR:
3461 /* BIT_INSERT_EXPR has an implict operand as the type precision
3462 of op1. Need to check to make sure they are the same. */
3463 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3464 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3465 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3466 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3467 return false;
3468 /* FALLTHRU */
3470 case VEC_COND_EXPR:
3471 case DOT_PROD_EXPR:
3472 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3474 case MODIFY_EXPR:
3475 case INIT_EXPR:
3476 case COMPOUND_EXPR:
3477 case PREDECREMENT_EXPR:
3478 case PREINCREMENT_EXPR:
3479 case POSTDECREMENT_EXPR:
3480 case POSTINCREMENT_EXPR:
3481 if (flags & OEP_LEXICOGRAPHIC)
3482 return OP_SAME (0) && OP_SAME (1);
3483 return false;
3485 case CLEANUP_POINT_EXPR:
3486 case EXPR_STMT:
3487 case SAVE_EXPR:
3488 if (flags & OEP_LEXICOGRAPHIC)
3489 return OP_SAME (0);
3490 return false;
3492 case OBJ_TYPE_REF:
3493 /* Virtual table reference. */
3494 if (!operand_equal_p (OBJ_TYPE_REF_EXPR (arg0),
3495 OBJ_TYPE_REF_EXPR (arg1), flags))
3496 return false;
3497 flags &= ~OEP_ADDRESS_OF;
3498 if (tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg0))
3499 != tree_to_uhwi (OBJ_TYPE_REF_TOKEN (arg1)))
3500 return false;
3501 if (!operand_equal_p (OBJ_TYPE_REF_OBJECT (arg0),
3502 OBJ_TYPE_REF_OBJECT (arg1), flags))
3503 return false;
3504 if (virtual_method_call_p (arg0))
3506 if (!virtual_method_call_p (arg1))
3507 return false;
3508 return types_same_for_odr (obj_type_ref_class (arg0),
3509 obj_type_ref_class (arg1));
3511 return false;
3513 default:
3514 return false;
3517 case tcc_vl_exp:
3518 switch (TREE_CODE (arg0))
3520 case CALL_EXPR:
3521 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3522 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3523 /* If not both CALL_EXPRs are either internal or normal function
3524 functions, then they are not equal. */
3525 return false;
3526 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3528 /* If the CALL_EXPRs call different internal functions, then they
3529 are not equal. */
3530 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3531 return false;
3533 else
3535 /* If the CALL_EXPRs call different functions, then they are not
3536 equal. */
3537 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3538 flags))
3539 return false;
3542 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3544 unsigned int cef = call_expr_flags (arg0);
3545 if (flags & OEP_PURE_SAME)
3546 cef &= ECF_CONST | ECF_PURE;
3547 else
3548 cef &= ECF_CONST;
3549 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3550 return false;
3553 /* Now see if all the arguments are the same. */
3555 const_call_expr_arg_iterator iter0, iter1;
3556 const_tree a0, a1;
3557 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3558 a1 = first_const_call_expr_arg (arg1, &iter1);
3559 a0 && a1;
3560 a0 = next_const_call_expr_arg (&iter0),
3561 a1 = next_const_call_expr_arg (&iter1))
3562 if (! operand_equal_p (a0, a1, flags))
3563 return false;
3565 /* If we get here and both argument lists are exhausted
3566 then the CALL_EXPRs are equal. */
3567 return ! (a0 || a1);
3569 default:
3570 return false;
3573 case tcc_declaration:
3574 /* Consider __builtin_sqrt equal to sqrt. */
3575 if (TREE_CODE (arg0) == FUNCTION_DECL)
3576 return (fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3577 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3578 && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3579 == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3581 if (DECL_P (arg0)
3582 && (flags & OEP_DECL_NAME)
3583 && (flags & OEP_LEXICOGRAPHIC))
3585 /* Consider decls with the same name equal. The caller needs
3586 to make sure they refer to the same entity (such as a function
3587 formal parameter). */
3588 tree a0name = DECL_NAME (arg0);
3589 tree a1name = DECL_NAME (arg1);
3590 const char *a0ns = a0name ? IDENTIFIER_POINTER (a0name) : NULL;
3591 const char *a1ns = a1name ? IDENTIFIER_POINTER (a1name) : NULL;
3592 return a0ns && a1ns && strcmp (a0ns, a1ns) == 0;
3594 return false;
3596 case tcc_exceptional:
3597 if (TREE_CODE (arg0) == CONSTRUCTOR)
3599 if (CONSTRUCTOR_NO_CLEARING (arg0) != CONSTRUCTOR_NO_CLEARING (arg1))
3600 return false;
3602 /* In GIMPLE constructors are used only to build vectors from
3603 elements. Individual elements in the constructor must be
3604 indexed in increasing order and form an initial sequence.
3606 We make no effort to compare constructors in generic.
3607 (see sem_variable::equals in ipa-icf which can do so for
3608 constants). */
3609 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3610 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3611 return false;
3613 /* Be sure that vectors constructed have the same representation.
3614 We only tested element precision and modes to match.
3615 Vectors may be BLKmode and thus also check that the number of
3616 parts match. */
3617 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
3618 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))))
3619 return false;
3621 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3622 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3623 unsigned int len = vec_safe_length (v0);
3625 if (len != vec_safe_length (v1))
3626 return false;
3628 for (unsigned int i = 0; i < len; i++)
3630 constructor_elt *c0 = &(*v0)[i];
3631 constructor_elt *c1 = &(*v1)[i];
3633 if (!operand_equal_p (c0->value, c1->value, flags)
3634 /* In GIMPLE the indexes can be either NULL or matching i.
3635 Double check this so we won't get false
3636 positives for GENERIC. */
3637 || (c0->index
3638 && (TREE_CODE (c0->index) != INTEGER_CST
3639 || compare_tree_int (c0->index, i)))
3640 || (c1->index
3641 && (TREE_CODE (c1->index) != INTEGER_CST
3642 || compare_tree_int (c1->index, i))))
3643 return false;
3645 return true;
3647 else if (TREE_CODE (arg0) == STATEMENT_LIST
3648 && (flags & OEP_LEXICOGRAPHIC))
3650 /* Compare the STATEMENT_LISTs. */
3651 tree_stmt_iterator tsi1, tsi2;
3652 tree body1 = CONST_CAST_TREE (arg0);
3653 tree body2 = CONST_CAST_TREE (arg1);
3654 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3655 tsi_next (&tsi1), tsi_next (&tsi2))
3657 /* The lists don't have the same number of statements. */
3658 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3659 return false;
3660 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3661 return true;
3662 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3663 flags & (OEP_LEXICOGRAPHIC
3664 | OEP_NO_HASH_CHECK)))
3665 return false;
3668 return false;
3670 case tcc_statement:
3671 switch (TREE_CODE (arg0))
3673 case RETURN_EXPR:
3674 if (flags & OEP_LEXICOGRAPHIC)
3675 return OP_SAME_WITH_NULL (0);
3676 return false;
3677 case DEBUG_BEGIN_STMT:
3678 if (flags & OEP_LEXICOGRAPHIC)
3679 return true;
3680 return false;
3681 default:
3682 return false;
3685 default:
3686 return false;
3689 #undef OP_SAME
3690 #undef OP_SAME_WITH_NULL
3693 /* Generate a hash value for an expression. This can be used iteratively
3694 by passing a previous result as the HSTATE argument. */
3696 void
3697 operand_compare::hash_operand (const_tree t, inchash::hash &hstate,
3698 unsigned int flags)
3700 int i;
3701 enum tree_code code;
3702 enum tree_code_class tclass;
3704 if (t == NULL_TREE || t == error_mark_node)
3706 hstate.merge_hash (0);
3707 return;
3710 STRIP_ANY_LOCATION_WRAPPER (t);
3712 if (!(flags & OEP_ADDRESS_OF))
3713 STRIP_NOPS (t);
3715 code = TREE_CODE (t);
3717 switch (code)
3719 /* Alas, constants aren't shared, so we can't rely on pointer
3720 identity. */
3721 case VOID_CST:
3722 hstate.merge_hash (0);
3723 return;
3724 case INTEGER_CST:
3725 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3726 for (i = 0; i < TREE_INT_CST_EXT_NUNITS (t); i++)
3727 hstate.add_hwi (TREE_INT_CST_ELT (t, i));
3728 return;
3729 case REAL_CST:
3731 unsigned int val2;
3732 if (!HONOR_SIGNED_ZEROS (t) && real_zerop (t))
3733 val2 = rvc_zero;
3734 else
3735 val2 = real_hash (TREE_REAL_CST_PTR (t));
3736 hstate.merge_hash (val2);
3737 return;
3739 case FIXED_CST:
3741 unsigned int val2 = fixed_hash (TREE_FIXED_CST_PTR (t));
3742 hstate.merge_hash (val2);
3743 return;
3745 case STRING_CST:
3746 hstate.add ((const void *) TREE_STRING_POINTER (t),
3747 TREE_STRING_LENGTH (t));
3748 return;
3749 case COMPLEX_CST:
3750 hash_operand (TREE_REALPART (t), hstate, flags);
3751 hash_operand (TREE_IMAGPART (t), hstate, flags);
3752 return;
3753 case VECTOR_CST:
3755 hstate.add_int (VECTOR_CST_NPATTERNS (t));
3756 hstate.add_int (VECTOR_CST_NELTS_PER_PATTERN (t));
3757 unsigned int count = vector_cst_encoded_nelts (t);
3758 for (unsigned int i = 0; i < count; ++i)
3759 hash_operand (VECTOR_CST_ENCODED_ELT (t, i), hstate, flags);
3760 return;
3762 case SSA_NAME:
3763 /* We can just compare by pointer. */
3764 hstate.add_hwi (SSA_NAME_VERSION (t));
3765 return;
3766 case PLACEHOLDER_EXPR:
3767 /* The node itself doesn't matter. */
3768 return;
3769 case BLOCK:
3770 case OMP_CLAUSE:
3771 /* Ignore. */
3772 return;
3773 case TREE_LIST:
3774 /* A list of expressions, for a CALL_EXPR or as the elements of a
3775 VECTOR_CST. */
3776 for (; t; t = TREE_CHAIN (t))
3777 hash_operand (TREE_VALUE (t), hstate, flags);
3778 return;
3779 case CONSTRUCTOR:
3781 unsigned HOST_WIDE_INT idx;
3782 tree field, value;
3783 flags &= ~OEP_ADDRESS_OF;
3784 hstate.add_int (CONSTRUCTOR_NO_CLEARING (t));
3785 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), idx, field, value)
3787 /* In GIMPLE the indexes can be either NULL or matching i. */
3788 if (field == NULL_TREE)
3789 field = bitsize_int (idx);
3790 hash_operand (field, hstate, flags);
3791 hash_operand (value, hstate, flags);
3793 return;
3795 case STATEMENT_LIST:
3797 tree_stmt_iterator i;
3798 for (i = tsi_start (CONST_CAST_TREE (t));
3799 !tsi_end_p (i); tsi_next (&i))
3800 hash_operand (tsi_stmt (i), hstate, flags);
3801 return;
3803 case TREE_VEC:
3804 for (i = 0; i < TREE_VEC_LENGTH (t); ++i)
3805 hash_operand (TREE_VEC_ELT (t, i), hstate, flags);
3806 return;
3807 case IDENTIFIER_NODE:
3808 hstate.add_object (IDENTIFIER_HASH_VALUE (t));
3809 return;
3810 case FUNCTION_DECL:
3811 /* When referring to a built-in FUNCTION_DECL, use the __builtin__ form.
3812 Otherwise nodes that compare equal according to operand_equal_p might
3813 get different hash codes. However, don't do this for machine specific
3814 or front end builtins, since the function code is overloaded in those
3815 cases. */
3816 if (DECL_BUILT_IN_CLASS (t) == BUILT_IN_NORMAL
3817 && builtin_decl_explicit_p (DECL_FUNCTION_CODE (t)))
3819 t = builtin_decl_explicit (DECL_FUNCTION_CODE (t));
3820 code = TREE_CODE (t);
3822 /* FALL THROUGH */
3823 default:
3824 if (POLY_INT_CST_P (t))
3826 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
3827 hstate.add_wide_int (wi::to_wide (POLY_INT_CST_COEFF (t, i)));
3828 return;
3830 tclass = TREE_CODE_CLASS (code);
3832 if (tclass == tcc_declaration)
3834 /* DECL's have a unique ID */
3835 hstate.add_hwi (DECL_UID (t));
3837 else if (tclass == tcc_comparison && !commutative_tree_code (code))
3839 /* For comparisons that can be swapped, use the lower
3840 tree code. */
3841 enum tree_code ccode = swap_tree_comparison (code);
3842 if (code < ccode)
3843 ccode = code;
3844 hstate.add_object (ccode);
3845 hash_operand (TREE_OPERAND (t, ccode != code), hstate, flags);
3846 hash_operand (TREE_OPERAND (t, ccode == code), hstate, flags);
3848 else if (CONVERT_EXPR_CODE_P (code))
3850 /* NOP_EXPR and CONVERT_EXPR are considered equal by
3851 operand_equal_p. */
3852 enum tree_code ccode = NOP_EXPR;
3853 hstate.add_object (ccode);
3855 /* Don't hash the type, that can lead to having nodes which
3856 compare equal according to operand_equal_p, but which
3857 have different hash codes. Make sure to include signedness
3858 in the hash computation. */
3859 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
3860 hash_operand (TREE_OPERAND (t, 0), hstate, flags);
3862 /* For OEP_ADDRESS_OF, hash MEM_EXPR[&decl, 0] the same as decl. */
3863 else if (code == MEM_REF
3864 && (flags & OEP_ADDRESS_OF) != 0
3865 && TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
3866 && DECL_P (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
3867 && integer_zerop (TREE_OPERAND (t, 1)))
3868 hash_operand (TREE_OPERAND (TREE_OPERAND (t, 0), 0),
3869 hstate, flags);
3870 /* Don't ICE on FE specific trees, or their arguments etc.
3871 during operand_equal_p hash verification. */
3872 else if (!IS_EXPR_CODE_CLASS (tclass))
3873 gcc_assert (flags & OEP_HASH_CHECK);
3874 else
3876 unsigned int sflags = flags;
3878 hstate.add_object (code);
3880 switch (code)
3882 case ADDR_EXPR:
3883 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3884 flags |= OEP_ADDRESS_OF;
3885 sflags = flags;
3886 break;
3888 case INDIRECT_REF:
3889 case MEM_REF:
3890 case TARGET_MEM_REF:
3891 flags &= ~OEP_ADDRESS_OF;
3892 sflags = flags;
3893 break;
3895 case COMPONENT_REF:
3896 if (sflags & OEP_ADDRESS_OF)
3898 hash_operand (TREE_OPERAND (t, 0), hstate, flags);
3899 hash_operand (DECL_FIELD_OFFSET (TREE_OPERAND (t, 1)),
3900 hstate, flags & ~OEP_ADDRESS_OF);
3901 hash_operand (DECL_FIELD_BIT_OFFSET (TREE_OPERAND (t, 1)),
3902 hstate, flags & ~OEP_ADDRESS_OF);
3903 return;
3905 break;
3906 case ARRAY_REF:
3907 case ARRAY_RANGE_REF:
3908 case BIT_FIELD_REF:
3909 sflags &= ~OEP_ADDRESS_OF;
3910 break;
3912 case COND_EXPR:
3913 flags &= ~OEP_ADDRESS_OF;
3914 break;
3916 case WIDEN_MULT_PLUS_EXPR:
3917 case WIDEN_MULT_MINUS_EXPR:
3919 /* The multiplication operands are commutative. */
3920 inchash::hash one, two;
3921 hash_operand (TREE_OPERAND (t, 0), one, flags);
3922 hash_operand (TREE_OPERAND (t, 1), two, flags);
3923 hstate.add_commutative (one, two);
3924 hash_operand (TREE_OPERAND (t, 2), two, flags);
3925 return;
3928 case CALL_EXPR:
3929 if (CALL_EXPR_FN (t) == NULL_TREE)
3930 hstate.add_int (CALL_EXPR_IFN (t));
3931 break;
3933 case TARGET_EXPR:
3934 /* For TARGET_EXPR, just hash on the TARGET_EXPR_SLOT.
3935 Usually different TARGET_EXPRs just should use
3936 different temporaries in their slots. */
3937 hash_operand (TARGET_EXPR_SLOT (t), hstate, flags);
3938 return;
3940 case OBJ_TYPE_REF:
3941 /* Virtual table reference. */
3942 inchash::add_expr (OBJ_TYPE_REF_EXPR (t), hstate, flags);
3943 flags &= ~OEP_ADDRESS_OF;
3944 inchash::add_expr (OBJ_TYPE_REF_TOKEN (t), hstate, flags);
3945 inchash::add_expr (OBJ_TYPE_REF_OBJECT (t), hstate, flags);
3946 if (!virtual_method_call_p (t))
3947 return;
3948 if (tree c = obj_type_ref_class (t))
3950 c = TYPE_NAME (TYPE_MAIN_VARIANT (c));
3951 /* We compute mangled names only when free_lang_data is run.
3952 In that case we can hash precisely. */
3953 if (TREE_CODE (c) == TYPE_DECL
3954 && DECL_ASSEMBLER_NAME_SET_P (c))
3955 hstate.add_object
3956 (IDENTIFIER_HASH_VALUE
3957 (DECL_ASSEMBLER_NAME (c)));
3959 return;
3960 default:
3961 break;
3964 /* Don't hash the type, that can lead to having nodes which
3965 compare equal according to operand_equal_p, but which
3966 have different hash codes. */
3967 if (code == NON_LVALUE_EXPR)
3969 /* Make sure to include signness in the hash computation. */
3970 hstate.add_int (TYPE_UNSIGNED (TREE_TYPE (t)));
3971 hash_operand (TREE_OPERAND (t, 0), hstate, flags);
3974 else if (commutative_tree_code (code))
3976 /* It's a commutative expression. We want to hash it the same
3977 however it appears. We do this by first hashing both operands
3978 and then rehashing based on the order of their independent
3979 hashes. */
3980 inchash::hash one, two;
3981 hash_operand (TREE_OPERAND (t, 0), one, flags);
3982 hash_operand (TREE_OPERAND (t, 1), two, flags);
3983 hstate.add_commutative (one, two);
3985 else
3986 for (i = TREE_OPERAND_LENGTH (t) - 1; i >= 0; --i)
3987 hash_operand (TREE_OPERAND (t, i), hstate,
3988 i == 0 ? flags : sflags);
3990 return;
3994 bool
3995 operand_compare::verify_hash_value (const_tree arg0, const_tree arg1,
3996 unsigned int flags, bool *ret)
3998 /* When checking and unless comparing DECL names, verify that if
3999 the outermost operand_equal_p call returns non-zero then ARG0
4000 and ARG1 have the same hash value. */
4001 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
4003 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
4005 if (arg0 != arg1 && !(flags & OEP_DECL_NAME))
4007 inchash::hash hstate0 (0), hstate1 (0);
4008 hash_operand (arg0, hstate0, flags | OEP_HASH_CHECK);
4009 hash_operand (arg1, hstate1, flags | OEP_HASH_CHECK);
4010 hashval_t h0 = hstate0.end ();
4011 hashval_t h1 = hstate1.end ();
4012 gcc_assert (h0 == h1);
4014 *ret = true;
4016 else
4017 *ret = false;
4019 return true;
4022 return false;
4026 static operand_compare default_compare_instance;
4028 /* Conveinece wrapper around operand_compare class because usually we do
4029 not need to play with the valueizer. */
4031 bool
4032 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
4034 return default_compare_instance.operand_equal_p (arg0, arg1, flags);
4037 namespace inchash
4040 /* Generate a hash value for an expression. This can be used iteratively
4041 by passing a previous result as the HSTATE argument.
4043 This function is intended to produce the same hash for expressions which
4044 would compare equal using operand_equal_p. */
4045 void
4046 add_expr (const_tree t, inchash::hash &hstate, unsigned int flags)
4048 default_compare_instance.hash_operand (t, hstate, flags);
4053 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
4054 with a different signedness or a narrower precision. */
4056 static bool
4057 operand_equal_for_comparison_p (tree arg0, tree arg1)
4059 if (operand_equal_p (arg0, arg1, 0))
4060 return true;
4062 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
4063 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
4064 return false;
4066 /* Discard any conversions that don't change the modes of ARG0 and ARG1
4067 and see if the inner values are the same. This removes any
4068 signedness comparison, which doesn't matter here. */
4069 tree op0 = arg0;
4070 tree op1 = arg1;
4071 STRIP_NOPS (op0);
4072 STRIP_NOPS (op1);
4073 if (operand_equal_p (op0, op1, 0))
4074 return true;
4076 /* Discard a single widening conversion from ARG1 and see if the inner
4077 value is the same as ARG0. */
4078 if (CONVERT_EXPR_P (arg1)
4079 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4080 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
4081 < TYPE_PRECISION (TREE_TYPE (arg1))
4082 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
4083 return true;
4085 return false;
4088 /* See if ARG is an expression that is either a comparison or is performing
4089 arithmetic on comparisons. The comparisons must only be comparing
4090 two different values, which will be stored in *CVAL1 and *CVAL2; if
4091 they are nonzero it means that some operands have already been found.
4092 No variables may be used anywhere else in the expression except in the
4093 comparisons.
4095 If this is true, return 1. Otherwise, return zero. */
4097 static bool
4098 twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
4100 enum tree_code code = TREE_CODE (arg);
4101 enum tree_code_class tclass = TREE_CODE_CLASS (code);
4103 /* We can handle some of the tcc_expression cases here. */
4104 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4105 tclass = tcc_unary;
4106 else if (tclass == tcc_expression
4107 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
4108 || code == COMPOUND_EXPR))
4109 tclass = tcc_binary;
4111 switch (tclass)
4113 case tcc_unary:
4114 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
4116 case tcc_binary:
4117 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4118 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
4120 case tcc_constant:
4121 return true;
4123 case tcc_expression:
4124 if (code == COND_EXPR)
4125 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
4126 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
4127 && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
4128 return false;
4130 case tcc_comparison:
4131 /* First see if we can handle the first operand, then the second. For
4132 the second operand, we know *CVAL1 can't be zero. It must be that
4133 one side of the comparison is each of the values; test for the
4134 case where this isn't true by failing if the two operands
4135 are the same. */
4137 if (operand_equal_p (TREE_OPERAND (arg, 0),
4138 TREE_OPERAND (arg, 1), 0))
4139 return false;
4141 if (*cval1 == 0)
4142 *cval1 = TREE_OPERAND (arg, 0);
4143 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
4145 else if (*cval2 == 0)
4146 *cval2 = TREE_OPERAND (arg, 0);
4147 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
4149 else
4150 return false;
4152 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
4154 else if (*cval2 == 0)
4155 *cval2 = TREE_OPERAND (arg, 1);
4156 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
4158 else
4159 return false;
4161 return true;
4163 default:
4164 return false;
4168 /* ARG is a tree that is known to contain just arithmetic operations and
4169 comparisons. Evaluate the operations in the tree substituting NEW0 for
4170 any occurrence of OLD0 as an operand of a comparison and likewise for
4171 NEW1 and OLD1. */
4173 static tree
4174 eval_subst (location_t loc, tree arg, tree old0, tree new0,
4175 tree old1, tree new1)
4177 tree type = TREE_TYPE (arg);
4178 enum tree_code code = TREE_CODE (arg);
4179 enum tree_code_class tclass = TREE_CODE_CLASS (code);
4181 /* We can handle some of the tcc_expression cases here. */
4182 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
4183 tclass = tcc_unary;
4184 else if (tclass == tcc_expression
4185 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
4186 tclass = tcc_binary;
4188 switch (tclass)
4190 case tcc_unary:
4191 return fold_build1_loc (loc, code, type,
4192 eval_subst (loc, TREE_OPERAND (arg, 0),
4193 old0, new0, old1, new1));
4195 case tcc_binary:
4196 return fold_build2_loc (loc, code, type,
4197 eval_subst (loc, TREE_OPERAND (arg, 0),
4198 old0, new0, old1, new1),
4199 eval_subst (loc, TREE_OPERAND (arg, 1),
4200 old0, new0, old1, new1));
4202 case tcc_expression:
4203 switch (code)
4205 case SAVE_EXPR:
4206 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
4207 old1, new1);
4209 case COMPOUND_EXPR:
4210 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
4211 old1, new1);
4213 case COND_EXPR:
4214 return fold_build3_loc (loc, code, type,
4215 eval_subst (loc, TREE_OPERAND (arg, 0),
4216 old0, new0, old1, new1),
4217 eval_subst (loc, TREE_OPERAND (arg, 1),
4218 old0, new0, old1, new1),
4219 eval_subst (loc, TREE_OPERAND (arg, 2),
4220 old0, new0, old1, new1));
4221 default:
4222 break;
4224 /* Fall through - ??? */
4226 case tcc_comparison:
4228 tree arg0 = TREE_OPERAND (arg, 0);
4229 tree arg1 = TREE_OPERAND (arg, 1);
4231 /* We need to check both for exact equality and tree equality. The
4232 former will be true if the operand has a side-effect. In that
4233 case, we know the operand occurred exactly once. */
4235 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
4236 arg0 = new0;
4237 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
4238 arg0 = new1;
4240 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
4241 arg1 = new0;
4242 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
4243 arg1 = new1;
4245 return fold_build2_loc (loc, code, type, arg0, arg1);
4248 default:
4249 return arg;
4253 /* Return a tree for the case when the result of an expression is RESULT
4254 converted to TYPE and OMITTED was previously an operand of the expression
4255 but is now not needed (e.g., we folded OMITTED * 0).
4257 If OMITTED has side effects, we must evaluate it. Otherwise, just do
4258 the conversion of RESULT to TYPE. */
4260 tree
4261 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
4263 tree t = fold_convert_loc (loc, type, result);
4265 /* If the resulting operand is an empty statement, just return the omitted
4266 statement casted to void. */
4267 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
4268 return build1_loc (loc, NOP_EXPR, void_type_node,
4269 fold_ignored_result (omitted));
4271 if (TREE_SIDE_EFFECTS (omitted))
4272 return build2_loc (loc, COMPOUND_EXPR, type,
4273 fold_ignored_result (omitted), t);
4275 return non_lvalue_loc (loc, t);
4278 /* Return a tree for the case when the result of an expression is RESULT
4279 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
4280 of the expression but are now not needed.
4282 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
4283 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
4284 evaluated before OMITTED2. Otherwise, if neither has side effects,
4285 just do the conversion of RESULT to TYPE. */
4287 tree
4288 omit_two_operands_loc (location_t loc, tree type, tree result,
4289 tree omitted1, tree omitted2)
4291 tree t = fold_convert_loc (loc, type, result);
4293 if (TREE_SIDE_EFFECTS (omitted2))
4294 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
4295 if (TREE_SIDE_EFFECTS (omitted1))
4296 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
4298 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
4302 /* Return a simplified tree node for the truth-negation of ARG. This
4303 never alters ARG itself. We assume that ARG is an operation that
4304 returns a truth value (0 or 1).
4306 FIXME: one would think we would fold the result, but it causes
4307 problems with the dominator optimizer. */
4309 static tree
4310 fold_truth_not_expr (location_t loc, tree arg)
4312 tree type = TREE_TYPE (arg);
4313 enum tree_code code = TREE_CODE (arg);
4314 location_t loc1, loc2;
4316 /* If this is a comparison, we can simply invert it, except for
4317 floating-point non-equality comparisons, in which case we just
4318 enclose a TRUTH_NOT_EXPR around what we have. */
4320 if (TREE_CODE_CLASS (code) == tcc_comparison)
4322 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
4323 if (FLOAT_TYPE_P (op_type)
4324 && flag_trapping_math
4325 && code != ORDERED_EXPR && code != UNORDERED_EXPR
4326 && code != NE_EXPR && code != EQ_EXPR)
4327 return NULL_TREE;
4329 code = invert_tree_comparison (code, HONOR_NANS (op_type));
4330 if (code == ERROR_MARK)
4331 return NULL_TREE;
4333 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
4334 TREE_OPERAND (arg, 1));
4335 copy_warning (ret, arg);
4336 return ret;
4339 switch (code)
4341 case INTEGER_CST:
4342 return constant_boolean_node (integer_zerop (arg), type);
4344 case TRUTH_AND_EXPR:
4345 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4346 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4347 return build2_loc (loc, TRUTH_OR_EXPR, type,
4348 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4349 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4351 case TRUTH_OR_EXPR:
4352 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4353 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4354 return build2_loc (loc, TRUTH_AND_EXPR, type,
4355 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4356 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4358 case TRUTH_XOR_EXPR:
4359 /* Here we can invert either operand. We invert the first operand
4360 unless the second operand is a TRUTH_NOT_EXPR in which case our
4361 result is the XOR of the first operand with the inside of the
4362 negation of the second operand. */
4364 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
4365 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
4366 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
4367 else
4368 return build2_loc (loc, TRUTH_XOR_EXPR, type,
4369 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
4370 TREE_OPERAND (arg, 1));
4372 case TRUTH_ANDIF_EXPR:
4373 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4374 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4375 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
4376 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4377 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4379 case TRUTH_ORIF_EXPR:
4380 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4381 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4382 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
4383 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
4384 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
4386 case TRUTH_NOT_EXPR:
4387 return TREE_OPERAND (arg, 0);
4389 case COND_EXPR:
4391 tree arg1 = TREE_OPERAND (arg, 1);
4392 tree arg2 = TREE_OPERAND (arg, 2);
4394 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4395 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
4397 /* A COND_EXPR may have a throw as one operand, which
4398 then has void type. Just leave void operands
4399 as they are. */
4400 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
4401 VOID_TYPE_P (TREE_TYPE (arg1))
4402 ? arg1 : invert_truthvalue_loc (loc1, arg1),
4403 VOID_TYPE_P (TREE_TYPE (arg2))
4404 ? arg2 : invert_truthvalue_loc (loc2, arg2));
4407 case COMPOUND_EXPR:
4408 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
4409 return build2_loc (loc, COMPOUND_EXPR, type,
4410 TREE_OPERAND (arg, 0),
4411 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
4413 case NON_LVALUE_EXPR:
4414 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4415 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
4417 CASE_CONVERT:
4418 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4419 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4421 /* fall through */
4423 case FLOAT_EXPR:
4424 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4425 return build1_loc (loc, TREE_CODE (arg), type,
4426 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4428 case BIT_AND_EXPR:
4429 if (!integer_onep (TREE_OPERAND (arg, 1)))
4430 return NULL_TREE;
4431 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
4433 case SAVE_EXPR:
4434 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
4436 case CLEANUP_POINT_EXPR:
4437 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
4438 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
4439 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
4441 default:
4442 return NULL_TREE;
4446 /* Fold the truth-negation of ARG. This never alters ARG itself. We
4447 assume that ARG is an operation that returns a truth value (0 or 1
4448 for scalars, 0 or -1 for vectors). Return the folded expression if
4449 folding is successful. Otherwise, return NULL_TREE. */
4451 static tree
4452 fold_invert_truthvalue (location_t loc, tree arg)
4454 tree type = TREE_TYPE (arg);
4455 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
4456 ? BIT_NOT_EXPR
4457 : TRUTH_NOT_EXPR,
4458 type, arg);
4461 /* Return a simplified tree node for the truth-negation of ARG. This
4462 never alters ARG itself. We assume that ARG is an operation that
4463 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
4465 tree
4466 invert_truthvalue_loc (location_t loc, tree arg)
4468 if (TREE_CODE (arg) == ERROR_MARK)
4469 return arg;
4471 tree type = TREE_TYPE (arg);
4472 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
4473 ? BIT_NOT_EXPR
4474 : TRUTH_NOT_EXPR,
4475 type, arg);
4478 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4479 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
4480 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
4481 is the original memory reference used to preserve the alias set of
4482 the access. */
4484 static tree
4485 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
4486 HOST_WIDE_INT bitsize, poly_int64 bitpos,
4487 int unsignedp, int reversep)
4489 tree result, bftype;
4491 /* Attempt not to lose the access path if possible. */
4492 if (TREE_CODE (orig_inner) == COMPONENT_REF)
4494 tree ninner = TREE_OPERAND (orig_inner, 0);
4495 machine_mode nmode;
4496 poly_int64 nbitsize, nbitpos;
4497 tree noffset;
4498 int nunsignedp, nreversep, nvolatilep = 0;
4499 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4500 &noffset, &nmode, &nunsignedp,
4501 &nreversep, &nvolatilep);
4502 if (base == inner
4503 && noffset == NULL_TREE
4504 && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4505 && !reversep
4506 && !nreversep
4507 && !nvolatilep)
4509 inner = ninner;
4510 bitpos -= nbitpos;
4514 alias_set_type iset = get_alias_set (orig_inner);
4515 if (iset == 0 && get_alias_set (inner) != iset)
4516 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4517 build_fold_addr_expr (inner),
4518 build_int_cst (ptr_type_node, 0));
4520 if (known_eq (bitpos, 0) && !reversep)
4522 tree size = TYPE_SIZE (TREE_TYPE (inner));
4523 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4524 || POINTER_TYPE_P (TREE_TYPE (inner)))
4525 && tree_fits_shwi_p (size)
4526 && tree_to_shwi (size) == bitsize)
4527 return fold_convert_loc (loc, type, inner);
4530 bftype = type;
4531 if (TYPE_PRECISION (bftype) != bitsize
4532 || TYPE_UNSIGNED (bftype) == !unsignedp)
4533 bftype = build_nonstandard_integer_type (bitsize, 0);
4535 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4536 bitsize_int (bitsize), bitsize_int (bitpos));
4537 REF_REVERSE_STORAGE_ORDER (result) = reversep;
4539 if (bftype != type)
4540 result = fold_convert_loc (loc, type, result);
4542 return result;
4545 /* Optimize a bit-field compare.
4547 There are two cases: First is a compare against a constant and the
4548 second is a comparison of two items where the fields are at the same
4549 bit position relative to the start of a chunk (byte, halfword, word)
4550 large enough to contain it. In these cases we can avoid the shift
4551 implicit in bitfield extractions.
4553 For constants, we emit a compare of the shifted constant with the
4554 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4555 compared. For two fields at the same position, we do the ANDs with the
4556 similar mask and compare the result of the ANDs.
4558 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4559 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4560 are the left and right operands of the comparison, respectively.
4562 If the optimization described above can be done, we return the resulting
4563 tree. Otherwise we return zero. */
4565 static tree
4566 optimize_bit_field_compare (location_t loc, enum tree_code code,
4567 tree compare_type, tree lhs, tree rhs)
4569 poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4570 HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4571 tree type = TREE_TYPE (lhs);
4572 tree unsigned_type;
4573 int const_p = TREE_CODE (rhs) == INTEGER_CST;
4574 machine_mode lmode, rmode;
4575 scalar_int_mode nmode;
4576 int lunsignedp, runsignedp;
4577 int lreversep, rreversep;
4578 int lvolatilep = 0, rvolatilep = 0;
4579 tree linner, rinner = NULL_TREE;
4580 tree mask;
4581 tree offset;
4583 /* Get all the information about the extractions being done. If the bit size
4584 is the same as the size of the underlying object, we aren't doing an
4585 extraction at all and so can do nothing. We also don't want to
4586 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4587 then will no longer be able to replace it. */
4588 linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4589 &lunsignedp, &lreversep, &lvolatilep);
4590 if (linner == lhs
4591 || !known_size_p (plbitsize)
4592 || !plbitsize.is_constant (&lbitsize)
4593 || !plbitpos.is_constant (&lbitpos)
4594 || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4595 || offset != 0
4596 || TREE_CODE (linner) == PLACEHOLDER_EXPR
4597 || lvolatilep)
4598 return 0;
4600 if (const_p)
4601 rreversep = lreversep;
4602 else
4604 /* If this is not a constant, we can only do something if bit positions,
4605 sizes, signedness and storage order are the same. */
4606 rinner
4607 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4608 &runsignedp, &rreversep, &rvolatilep);
4610 if (rinner == rhs
4611 || maybe_ne (lbitpos, rbitpos)
4612 || maybe_ne (lbitsize, rbitsize)
4613 || lunsignedp != runsignedp
4614 || lreversep != rreversep
4615 || offset != 0
4616 || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4617 || rvolatilep)
4618 return 0;
4621 /* Honor the C++ memory model and mimic what RTL expansion does. */
4622 poly_uint64 bitstart = 0;
4623 poly_uint64 bitend = 0;
4624 if (TREE_CODE (lhs) == COMPONENT_REF)
4626 get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4627 if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4628 return 0;
4631 /* See if we can find a mode to refer to this field. We should be able to,
4632 but fail if we can't. */
4633 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4634 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4635 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4636 TYPE_ALIGN (TREE_TYPE (rinner))),
4637 BITS_PER_WORD, false, &nmode))
4638 return 0;
4640 /* Set signed and unsigned types of the precision of this mode for the
4641 shifts below. */
4642 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4644 /* Compute the bit position and size for the new reference and our offset
4645 within it. If the new reference is the same size as the original, we
4646 won't optimize anything, so return zero. */
4647 nbitsize = GET_MODE_BITSIZE (nmode);
4648 nbitpos = lbitpos & ~ (nbitsize - 1);
4649 lbitpos -= nbitpos;
4650 if (nbitsize == lbitsize)
4651 return 0;
4653 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4654 lbitpos = nbitsize - lbitsize - lbitpos;
4656 /* Make the mask to be used against the extracted field. */
4657 mask = build_int_cst_type (unsigned_type, -1);
4658 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4659 mask = const_binop (RSHIFT_EXPR, mask,
4660 size_int (nbitsize - lbitsize - lbitpos));
4662 if (! const_p)
4664 if (nbitpos < 0)
4665 return 0;
4667 /* If not comparing with constant, just rework the comparison
4668 and return. */
4669 tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4670 nbitsize, nbitpos, 1, lreversep);
4671 t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4672 tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4673 nbitsize, nbitpos, 1, rreversep);
4674 t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4675 return fold_build2_loc (loc, code, compare_type, t1, t2);
4678 /* Otherwise, we are handling the constant case. See if the constant is too
4679 big for the field. Warn and return a tree for 0 (false) if so. We do
4680 this not only for its own sake, but to avoid having to test for this
4681 error case below. If we didn't, we might generate wrong code.
4683 For unsigned fields, the constant shifted right by the field length should
4684 be all zero. For signed fields, the high-order bits should agree with
4685 the sign bit. */
4687 if (lunsignedp)
4689 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4691 warning (0, "comparison is always %d due to width of bit-field",
4692 code == NE_EXPR);
4693 return constant_boolean_node (code == NE_EXPR, compare_type);
4696 else
4698 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4699 if (tem != 0 && tem != -1)
4701 warning (0, "comparison is always %d due to width of bit-field",
4702 code == NE_EXPR);
4703 return constant_boolean_node (code == NE_EXPR, compare_type);
4707 if (nbitpos < 0)
4708 return 0;
4710 /* Single-bit compares should always be against zero. */
4711 if (lbitsize == 1 && ! integer_zerop (rhs))
4713 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4714 rhs = build_int_cst (type, 0);
4717 /* Make a new bitfield reference, shift the constant over the
4718 appropriate number of bits and mask it with the computed mask
4719 (in case this was a signed field). If we changed it, make a new one. */
4720 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4721 nbitsize, nbitpos, 1, lreversep);
4723 rhs = const_binop (BIT_AND_EXPR,
4724 const_binop (LSHIFT_EXPR,
4725 fold_convert_loc (loc, unsigned_type, rhs),
4726 size_int (lbitpos)),
4727 mask);
4729 lhs = build2_loc (loc, code, compare_type,
4730 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4731 return lhs;
4734 /* Subroutine for fold_truth_andor_1: decode a field reference.
4736 If EXP is a comparison reference, we return the innermost reference.
4738 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4739 set to the starting bit number.
4741 If the innermost field can be completely contained in a mode-sized
4742 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4744 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4745 otherwise it is not changed.
4747 *PUNSIGNEDP is set to the signedness of the field.
4749 *PREVERSEP is set to the storage order of the field.
4751 *PMASK is set to the mask used. This is either contained in a
4752 BIT_AND_EXPR or derived from the width of the field.
4754 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4756 Return 0 if this is not a component reference or is one that we can't
4757 do anything with. */
4759 static tree
4760 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4761 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4762 int *punsignedp, int *preversep, int *pvolatilep,
4763 tree *pmask, tree *pand_mask)
4765 tree exp = *exp_;
4766 tree outer_type = 0;
4767 tree and_mask = 0;
4768 tree mask, inner, offset;
4769 tree unsigned_type;
4770 unsigned int precision;
4772 /* All the optimizations using this function assume integer fields.
4773 There are problems with FP fields since the type_for_size call
4774 below can fail for, e.g., XFmode. */
4775 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4776 return NULL_TREE;
4778 /* We are interested in the bare arrangement of bits, so strip everything
4779 that doesn't affect the machine mode. However, record the type of the
4780 outermost expression if it may matter below. */
4781 if (CONVERT_EXPR_P (exp)
4782 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4783 outer_type = TREE_TYPE (exp);
4784 STRIP_NOPS (exp);
4786 if (TREE_CODE (exp) == BIT_AND_EXPR)
4788 and_mask = TREE_OPERAND (exp, 1);
4789 exp = TREE_OPERAND (exp, 0);
4790 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4791 if (TREE_CODE (and_mask) != INTEGER_CST)
4792 return NULL_TREE;
4795 poly_int64 poly_bitsize, poly_bitpos;
4796 inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
4797 pmode, punsignedp, preversep, pvolatilep);
4798 if ((inner == exp && and_mask == 0)
4799 || !poly_bitsize.is_constant (pbitsize)
4800 || !poly_bitpos.is_constant (pbitpos)
4801 || *pbitsize < 0
4802 || offset != 0
4803 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4804 /* Reject out-of-bound accesses (PR79731). */
4805 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4806 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4807 *pbitpos + *pbitsize) < 0))
4808 return NULL_TREE;
4810 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4811 if (unsigned_type == NULL_TREE)
4812 return NULL_TREE;
4814 *exp_ = exp;
4816 /* If the number of bits in the reference is the same as the bitsize of
4817 the outer type, then the outer type gives the signedness. Otherwise
4818 (in case of a small bitfield) the signedness is unchanged. */
4819 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4820 *punsignedp = TYPE_UNSIGNED (outer_type);
4822 /* Compute the mask to access the bitfield. */
4823 precision = TYPE_PRECISION (unsigned_type);
4825 mask = build_int_cst_type (unsigned_type, -1);
4827 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4828 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4830 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4831 if (and_mask != 0)
4832 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4833 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4835 *pmask = mask;
4836 *pand_mask = and_mask;
4837 return inner;
4840 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4841 bit positions and MASK is SIGNED. */
4843 static bool
4844 all_ones_mask_p (const_tree mask, unsigned int size)
4846 tree type = TREE_TYPE (mask);
4847 unsigned int precision = TYPE_PRECISION (type);
4849 /* If this function returns true when the type of the mask is
4850 UNSIGNED, then there will be errors. In particular see
4851 gcc.c-torture/execute/990326-1.c. There does not appear to be
4852 any documentation paper trail as to why this is so. But the pre
4853 wide-int worked with that restriction and it has been preserved
4854 here. */
4855 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4856 return false;
4858 return wi::mask (size, false, precision) == wi::to_wide (mask);
4861 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4862 represents the sign bit of EXP's type. If EXP represents a sign
4863 or zero extension, also test VAL against the unextended type.
4864 The return value is the (sub)expression whose sign bit is VAL,
4865 or NULL_TREE otherwise. */
4867 tree
4868 sign_bit_p (tree exp, const_tree val)
4870 int width;
4871 tree t;
4873 /* Tree EXP must have an integral type. */
4874 t = TREE_TYPE (exp);
4875 if (! INTEGRAL_TYPE_P (t))
4876 return NULL_TREE;
4878 /* Tree VAL must be an integer constant. */
4879 if (TREE_CODE (val) != INTEGER_CST
4880 || TREE_OVERFLOW (val))
4881 return NULL_TREE;
4883 width = TYPE_PRECISION (t);
4884 if (wi::only_sign_bit_p (wi::to_wide (val), width))
4885 return exp;
4887 /* Handle extension from a narrower type. */
4888 if (TREE_CODE (exp) == NOP_EXPR
4889 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4890 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4892 return NULL_TREE;
4895 /* Subroutine for fold_truth_andor_1 and simple_condition_p: determine if an
4896 operand is simple enough to be evaluated unconditionally. */
4898 static bool
4899 simple_operand_p (const_tree exp)
4901 /* Strip any conversions that don't change the machine mode. */
4902 STRIP_NOPS (exp);
4904 return (CONSTANT_CLASS_P (exp)
4905 || TREE_CODE (exp) == SSA_NAME
4906 || (DECL_P (exp)
4907 && ! TREE_ADDRESSABLE (exp)
4908 && ! TREE_THIS_VOLATILE (exp)
4909 && ! DECL_NONLOCAL (exp)
4910 /* Don't regard global variables as simple. They may be
4911 allocated in ways unknown to the compiler (shared memory,
4912 #pragma weak, etc). */
4913 && ! TREE_PUBLIC (exp)
4914 && ! DECL_EXTERNAL (exp)
4915 /* Weakrefs are not safe to be read, since they can be NULL.
4916 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4917 have DECL_WEAK flag set. */
4918 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4919 /* Loading a static variable is unduly expensive, but global
4920 registers aren't expensive. */
4921 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4924 /* Determine if an operand is simple enough to be evaluated unconditionally.
4925 In addition to simple_operand_p, we assume that comparisons, conversions,
4926 and logic-not operations are simple, if their operands are simple, too. */
4928 bool
4929 simple_condition_p (tree exp)
4931 enum tree_code code;
4933 if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
4934 return false;
4936 while (CONVERT_EXPR_P (exp))
4937 exp = TREE_OPERAND (exp, 0);
4939 code = TREE_CODE (exp);
4941 if (TREE_CODE_CLASS (code) == tcc_comparison)
4942 return (simple_operand_p (TREE_OPERAND (exp, 0))
4943 && simple_operand_p (TREE_OPERAND (exp, 1)));
4945 if (code == TRUTH_NOT_EXPR)
4946 return simple_condition_p (TREE_OPERAND (exp, 0));
4948 return simple_operand_p (exp);
4952 /* The following functions are subroutines to fold_range_test and allow it to
4953 try to change a logical combination of comparisons into a range test.
4955 For example, both
4956 X == 2 || X == 3 || X == 4 || X == 5
4958 X >= 2 && X <= 5
4959 are converted to
4960 (unsigned) (X - 2) <= 3
4962 We describe each set of comparisons as being either inside or outside
4963 a range, using a variable named like IN_P, and then describe the
4964 range with a lower and upper bound. If one of the bounds is omitted,
4965 it represents either the highest or lowest value of the type.
4967 In the comments below, we represent a range by two numbers in brackets
4968 preceded by a "+" to designate being inside that range, or a "-" to
4969 designate being outside that range, so the condition can be inverted by
4970 flipping the prefix. An omitted bound is represented by a "-". For
4971 example, "- [-, 10]" means being outside the range starting at the lowest
4972 possible value and ending at 10, in other words, being greater than 10.
4973 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4974 always false.
4976 We set up things so that the missing bounds are handled in a consistent
4977 manner so neither a missing bound nor "true" and "false" need to be
4978 handled using a special case. */
4980 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4981 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4982 and UPPER1_P are nonzero if the respective argument is an upper bound
4983 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4984 must be specified for a comparison. ARG1 will be converted to ARG0's
4985 type if both are specified. */
4987 static tree
4988 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4989 tree arg1, int upper1_p)
4991 tree tem;
4992 int result;
4993 int sgn0, sgn1;
4995 /* If neither arg represents infinity, do the normal operation.
4996 Else, if not a comparison, return infinity. Else handle the special
4997 comparison rules. Note that most of the cases below won't occur, but
4998 are handled for consistency. */
5000 if (arg0 != 0 && arg1 != 0)
5002 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
5003 arg0, fold_convert (TREE_TYPE (arg0), arg1));
5004 STRIP_NOPS (tem);
5005 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
5008 if (TREE_CODE_CLASS (code) != tcc_comparison)
5009 return 0;
5011 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
5012 for neither. In real maths, we cannot assume open ended ranges are
5013 the same. But, this is computer arithmetic, where numbers are finite.
5014 We can therefore make the transformation of any unbounded range with
5015 the value Z, Z being greater than any representable number. This permits
5016 us to treat unbounded ranges as equal. */
5017 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
5018 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
5019 switch (code)
5021 case EQ_EXPR:
5022 result = sgn0 == sgn1;
5023 break;
5024 case NE_EXPR:
5025 result = sgn0 != sgn1;
5026 break;
5027 case LT_EXPR:
5028 result = sgn0 < sgn1;
5029 break;
5030 case LE_EXPR:
5031 result = sgn0 <= sgn1;
5032 break;
5033 case GT_EXPR:
5034 result = sgn0 > sgn1;
5035 break;
5036 case GE_EXPR:
5037 result = sgn0 >= sgn1;
5038 break;
5039 default:
5040 gcc_unreachable ();
5043 return constant_boolean_node (result, type);
5046 /* Helper routine for make_range. Perform one step for it, return
5047 new expression if the loop should continue or NULL_TREE if it should
5048 stop. */
5050 tree
5051 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
5052 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
5053 bool *strict_overflow_p)
5055 tree arg0_type = TREE_TYPE (arg0);
5056 tree n_low, n_high, low = *p_low, high = *p_high;
5057 int in_p = *p_in_p, n_in_p;
5059 switch (code)
5061 case TRUTH_NOT_EXPR:
5062 /* We can only do something if the range is testing for zero. */
5063 if (low == NULL_TREE || high == NULL_TREE
5064 || ! integer_zerop (low) || ! integer_zerop (high))
5065 return NULL_TREE;
5066 *p_in_p = ! in_p;
5067 return arg0;
5069 case EQ_EXPR: case NE_EXPR:
5070 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
5071 /* We can only do something if the range is testing for zero
5072 and if the second operand is an integer constant. Note that
5073 saying something is "in" the range we make is done by
5074 complementing IN_P since it will set in the initial case of
5075 being not equal to zero; "out" is leaving it alone. */
5076 if (low == NULL_TREE || high == NULL_TREE
5077 || ! integer_zerop (low) || ! integer_zerop (high)
5078 || TREE_CODE (arg1) != INTEGER_CST)
5079 return NULL_TREE;
5081 switch (code)
5083 case NE_EXPR: /* - [c, c] */
5084 low = high = arg1;
5085 break;
5086 case EQ_EXPR: /* + [c, c] */
5087 in_p = ! in_p, low = high = arg1;
5088 break;
5089 case GT_EXPR: /* - [-, c] */
5090 low = 0, high = arg1;
5091 break;
5092 case GE_EXPR: /* + [c, -] */
5093 in_p = ! in_p, low = arg1, high = 0;
5094 break;
5095 case LT_EXPR: /* - [c, -] */
5096 low = arg1, high = 0;
5097 break;
5098 case LE_EXPR: /* + [-, c] */
5099 in_p = ! in_p, low = 0, high = arg1;
5100 break;
5101 default:
5102 gcc_unreachable ();
5105 /* If this is an unsigned comparison, we also know that EXP is
5106 greater than or equal to zero. We base the range tests we make
5107 on that fact, so we record it here so we can parse existing
5108 range tests. We test arg0_type since often the return type
5109 of, e.g. EQ_EXPR, is boolean. */
5110 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
5112 if (! merge_ranges (&n_in_p, &n_low, &n_high,
5113 in_p, low, high, 1,
5114 build_int_cst (arg0_type, 0),
5115 NULL_TREE))
5116 return NULL_TREE;
5118 in_p = n_in_p, low = n_low, high = n_high;
5120 /* If the high bound is missing, but we have a nonzero low
5121 bound, reverse the range so it goes from zero to the low bound
5122 minus 1. */
5123 if (high == 0 && low && ! integer_zerop (low))
5125 in_p = ! in_p;
5126 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
5127 build_int_cst (TREE_TYPE (low), 1), 0);
5128 low = build_int_cst (arg0_type, 0);
5132 *p_low = low;
5133 *p_high = high;
5134 *p_in_p = in_p;
5135 return arg0;
5137 case NEGATE_EXPR:
5138 /* If flag_wrapv and ARG0_TYPE is signed, make sure
5139 low and high are non-NULL, then normalize will DTRT. */
5140 if (!TYPE_UNSIGNED (arg0_type)
5141 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5143 if (low == NULL_TREE)
5144 low = TYPE_MIN_VALUE (arg0_type);
5145 if (high == NULL_TREE)
5146 high = TYPE_MAX_VALUE (arg0_type);
5149 /* (-x) IN [a,b] -> x in [-b, -a] */
5150 n_low = range_binop (MINUS_EXPR, exp_type,
5151 build_int_cst (exp_type, 0),
5152 0, high, 1);
5153 n_high = range_binop (MINUS_EXPR, exp_type,
5154 build_int_cst (exp_type, 0),
5155 0, low, 0);
5156 if (n_high != 0 && TREE_OVERFLOW (n_high))
5157 return NULL_TREE;
5158 goto normalize;
5160 case BIT_NOT_EXPR:
5161 /* ~ X -> -X - 1 */
5162 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
5163 build_int_cst (exp_type, 1));
5165 case PLUS_EXPR:
5166 case MINUS_EXPR:
5167 if (TREE_CODE (arg1) != INTEGER_CST)
5168 return NULL_TREE;
5170 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
5171 move a constant to the other side. */
5172 if (!TYPE_UNSIGNED (arg0_type)
5173 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
5174 return NULL_TREE;
5176 /* If EXP is signed, any overflow in the computation is undefined,
5177 so we don't worry about it so long as our computations on
5178 the bounds don't overflow. For unsigned, overflow is defined
5179 and this is exactly the right thing. */
5180 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5181 arg0_type, low, 0, arg1, 0);
5182 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
5183 arg0_type, high, 1, arg1, 0);
5184 if ((n_low != 0 && TREE_OVERFLOW (n_low))
5185 || (n_high != 0 && TREE_OVERFLOW (n_high)))
5186 return NULL_TREE;
5188 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
5189 *strict_overflow_p = true;
5191 normalize:
5192 /* Check for an unsigned range which has wrapped around the maximum
5193 value thus making n_high < n_low, and normalize it. */
5194 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
5196 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
5197 build_int_cst (TREE_TYPE (n_high), 1), 0);
5198 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
5199 build_int_cst (TREE_TYPE (n_low), 1), 0);
5201 /* If the range is of the form +/- [ x+1, x ], we won't
5202 be able to normalize it. But then, it represents the
5203 whole range or the empty set, so make it
5204 +/- [ -, - ]. */
5205 if (tree_int_cst_equal (n_low, low)
5206 && tree_int_cst_equal (n_high, high))
5207 low = high = 0;
5208 else
5209 in_p = ! in_p;
5211 else
5212 low = n_low, high = n_high;
5214 *p_low = low;
5215 *p_high = high;
5216 *p_in_p = in_p;
5217 return arg0;
5219 CASE_CONVERT:
5220 case NON_LVALUE_EXPR:
5221 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
5222 return NULL_TREE;
5224 if (! INTEGRAL_TYPE_P (arg0_type)
5225 || (low != 0 && ! int_fits_type_p (low, arg0_type))
5226 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
5227 return NULL_TREE;
5229 n_low = low, n_high = high;
5231 if (n_low != 0)
5232 n_low = fold_convert_loc (loc, arg0_type, n_low);
5234 if (n_high != 0)
5235 n_high = fold_convert_loc (loc, arg0_type, n_high);
5237 /* If we're converting arg0 from an unsigned type, to exp,
5238 a signed type, we will be doing the comparison as unsigned.
5239 The tests above have already verified that LOW and HIGH
5240 are both positive.
5242 So we have to ensure that we will handle large unsigned
5243 values the same way that the current signed bounds treat
5244 negative values. */
5246 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
5248 tree high_positive;
5249 tree equiv_type;
5250 /* For fixed-point modes, we need to pass the saturating flag
5251 as the 2nd parameter. */
5252 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
5253 equiv_type
5254 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
5255 TYPE_SATURATING (arg0_type));
5256 else
5257 equiv_type
5258 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
5260 /* A range without an upper bound is, naturally, unbounded.
5261 Since convert would have cropped a very large value, use
5262 the max value for the destination type. */
5263 high_positive
5264 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
5265 : TYPE_MAX_VALUE (arg0_type);
5267 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
5268 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
5269 fold_convert_loc (loc, arg0_type,
5270 high_positive),
5271 build_int_cst (arg0_type, 1));
5273 /* If the low bound is specified, "and" the range with the
5274 range for which the original unsigned value will be
5275 positive. */
5276 if (low != 0)
5278 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
5279 1, fold_convert_loc (loc, arg0_type,
5280 integer_zero_node),
5281 high_positive))
5282 return NULL_TREE;
5284 in_p = (n_in_p == in_p);
5286 else
5288 /* Otherwise, "or" the range with the range of the input
5289 that will be interpreted as negative. */
5290 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
5291 1, fold_convert_loc (loc, arg0_type,
5292 integer_zero_node),
5293 high_positive))
5294 return NULL_TREE;
5296 in_p = (in_p != n_in_p);
5300 /* Otherwise, if we are converting arg0 from signed type, to exp,
5301 an unsigned type, we will do the comparison as signed. If
5302 high is non-NULL, we punt above if it doesn't fit in the signed
5303 type, so if we get through here, +[-, high] or +[low, high] are
5304 equivalent to +[-, n_high] or +[n_low, n_high]. Similarly,
5305 +[-, -] or -[-, -] are equivalent too. But if low is specified and
5306 high is not, the +[low, -] range is equivalent to union of
5307 +[n_low, -] and +[-, -1] ranges, so +[low, -] is equivalent to
5308 -[0, n_low-1] and similarly -[low, -] to +[0, n_low-1], except for
5309 low being 0, which should be treated as [-, -]. */
5310 else if (TYPE_UNSIGNED (exp_type)
5311 && !TYPE_UNSIGNED (arg0_type)
5312 && low
5313 && !high)
5315 if (integer_zerop (low))
5316 n_low = NULL_TREE;
5317 else
5319 n_high = fold_build2_loc (loc, PLUS_EXPR, arg0_type,
5320 n_low, build_int_cst (arg0_type, -1));
5321 n_low = build_zero_cst (arg0_type);
5322 in_p = !in_p;
5326 *p_low = n_low;
5327 *p_high = n_high;
5328 *p_in_p = in_p;
5329 return arg0;
5331 default:
5332 return NULL_TREE;
5336 /* Given EXP, a logical expression, set the range it is testing into
5337 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
5338 actually being tested. *PLOW and *PHIGH will be made of the same
5339 type as the returned expression. If EXP is not a comparison, we
5340 will most likely not be returning a useful value and range. Set
5341 *STRICT_OVERFLOW_P to true if the return value is only valid
5342 because signed overflow is undefined; otherwise, do not change
5343 *STRICT_OVERFLOW_P. */
5345 tree
5346 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
5347 bool *strict_overflow_p)
5349 enum tree_code code;
5350 tree arg0, arg1 = NULL_TREE;
5351 tree exp_type, nexp;
5352 int in_p;
5353 tree low, high;
5354 location_t loc = EXPR_LOCATION (exp);
5356 /* Start with simply saying "EXP != 0" and then look at the code of EXP
5357 and see if we can refine the range. Some of the cases below may not
5358 happen, but it doesn't seem worth worrying about this. We "continue"
5359 the outer loop when we've changed something; otherwise we "break"
5360 the switch, which will "break" the while. */
5362 in_p = 0;
5363 low = high = build_int_cst (TREE_TYPE (exp), 0);
5365 while (1)
5367 code = TREE_CODE (exp);
5368 exp_type = TREE_TYPE (exp);
5369 arg0 = NULL_TREE;
5371 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
5373 if (TREE_OPERAND_LENGTH (exp) > 0)
5374 arg0 = TREE_OPERAND (exp, 0);
5375 if (TREE_CODE_CLASS (code) == tcc_binary
5376 || TREE_CODE_CLASS (code) == tcc_comparison
5377 || (TREE_CODE_CLASS (code) == tcc_expression
5378 && TREE_OPERAND_LENGTH (exp) > 1))
5379 arg1 = TREE_OPERAND (exp, 1);
5381 if (arg0 == NULL_TREE)
5382 break;
5384 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
5385 &high, &in_p, strict_overflow_p);
5386 if (nexp == NULL_TREE)
5387 break;
5388 exp = nexp;
5391 /* If EXP is a constant, we can evaluate whether this is true or false. */
5392 if (TREE_CODE (exp) == INTEGER_CST)
5394 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
5395 exp, 0, low, 0))
5396 && integer_onep (range_binop (LE_EXPR, integer_type_node,
5397 exp, 1, high, 1)));
5398 low = high = 0;
5399 exp = 0;
5402 *pin_p = in_p, *plow = low, *phigh = high;
5403 return exp;
5406 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
5407 a bitwise check i.e. when
5408 LOW == 0xXX...X00...0
5409 HIGH == 0xXX...X11...1
5410 Return corresponding mask in MASK and stem in VALUE. */
5412 static bool
5413 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
5414 tree *value)
5416 if (TREE_CODE (low) != INTEGER_CST
5417 || TREE_CODE (high) != INTEGER_CST)
5418 return false;
5420 unsigned prec = TYPE_PRECISION (type);
5421 wide_int lo = wi::to_wide (low, prec);
5422 wide_int hi = wi::to_wide (high, prec);
5424 wide_int end_mask = lo ^ hi;
5425 if ((end_mask & (end_mask + 1)) != 0
5426 || (lo & end_mask) != 0)
5427 return false;
5429 wide_int stem_mask = ~end_mask;
5430 wide_int stem = lo & stem_mask;
5431 if (stem != (hi & stem_mask))
5432 return false;
5434 *mask = wide_int_to_tree (type, stem_mask);
5435 *value = wide_int_to_tree (type, stem);
5437 return true;
5440 /* Helper routine for build_range_check and match.pd. Return the type to
5441 perform the check or NULL if it shouldn't be optimized. */
5443 tree
5444 range_check_type (tree etype)
5446 /* First make sure that arithmetics in this type is valid, then make sure
5447 that it wraps around. */
5448 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
5449 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
5451 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
5453 tree utype, minv, maxv;
5455 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5456 for the type in question, as we rely on this here. */
5457 utype = unsigned_type_for (etype);
5458 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
5459 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5460 build_int_cst (TREE_TYPE (maxv), 1), 1);
5461 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
5463 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5464 minv, 1, maxv, 1)))
5465 etype = utype;
5466 else
5467 return NULL_TREE;
5469 else if (POINTER_TYPE_P (etype) || TREE_CODE (etype) == OFFSET_TYPE)
5470 etype = unsigned_type_for (etype);
5471 return etype;
5474 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
5475 type, TYPE, return an expression to test if EXP is in (or out of, depending
5476 on IN_P) the range. Return 0 if the test couldn't be created. */
5478 tree
5479 build_range_check (location_t loc, tree type, tree exp, int in_p,
5480 tree low, tree high)
5482 tree etype = TREE_TYPE (exp), mask, value;
5484 /* Disable this optimization for function pointer expressions
5485 on targets that require function pointer canonicalization. */
5486 if (targetm.have_canonicalize_funcptr_for_compare ()
5487 && POINTER_TYPE_P (etype)
5488 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
5489 return NULL_TREE;
5491 if (! in_p)
5493 value = build_range_check (loc, type, exp, 1, low, high);
5494 if (value != 0)
5495 return invert_truthvalue_loc (loc, value);
5497 return 0;
5500 if (low == 0 && high == 0)
5501 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
5503 if (low == 0)
5504 return fold_build2_loc (loc, LE_EXPR, type, exp,
5505 fold_convert_loc (loc, etype, high));
5507 if (high == 0)
5508 return fold_build2_loc (loc, GE_EXPR, type, exp,
5509 fold_convert_loc (loc, etype, low));
5511 if (operand_equal_p (low, high, 0))
5512 return fold_build2_loc (loc, EQ_EXPR, type, exp,
5513 fold_convert_loc (loc, etype, low));
5515 if (TREE_CODE (exp) == BIT_AND_EXPR
5516 && maskable_range_p (low, high, etype, &mask, &value))
5517 return fold_build2_loc (loc, EQ_EXPR, type,
5518 fold_build2_loc (loc, BIT_AND_EXPR, etype,
5519 exp, mask),
5520 value);
5522 if (integer_zerop (low))
5524 if (! TYPE_UNSIGNED (etype))
5526 etype = unsigned_type_for (etype);
5527 high = fold_convert_loc (loc, etype, high);
5528 exp = fold_convert_loc (loc, etype, exp);
5530 return build_range_check (loc, type, exp, 1, 0, high);
5533 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5534 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5536 int prec = TYPE_PRECISION (etype);
5538 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5540 if (TYPE_UNSIGNED (etype))
5542 tree signed_etype = signed_type_for (etype);
5543 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5544 etype
5545 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5546 else
5547 etype = signed_etype;
5548 exp = fold_convert_loc (loc, etype, exp);
5550 return fold_build2_loc (loc, GT_EXPR, type, exp,
5551 build_int_cst (etype, 0));
5555 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5556 This requires wrap-around arithmetics for the type of the expression. */
5557 etype = range_check_type (etype);
5558 if (etype == NULL_TREE)
5559 return NULL_TREE;
5561 high = fold_convert_loc (loc, etype, high);
5562 low = fold_convert_loc (loc, etype, low);
5563 exp = fold_convert_loc (loc, etype, exp);
5565 value = const_binop (MINUS_EXPR, high, low);
5567 if (value != 0 && !TREE_OVERFLOW (value))
5568 return build_range_check (loc, type,
5569 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5570 1, build_int_cst (etype, 0), value);
5572 return 0;
5575 /* Return the predecessor of VAL in its type, handling the infinite case. */
5577 static tree
5578 range_predecessor (tree val)
5580 tree type = TREE_TYPE (val);
5582 if (INTEGRAL_TYPE_P (type)
5583 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5584 return 0;
5585 else
5586 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5587 build_int_cst (TREE_TYPE (val), 1), 0);
5590 /* Return the successor of VAL in its type, handling the infinite case. */
5592 static tree
5593 range_successor (tree val)
5595 tree type = TREE_TYPE (val);
5597 if (INTEGRAL_TYPE_P (type)
5598 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5599 return 0;
5600 else
5601 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5602 build_int_cst (TREE_TYPE (val), 1), 0);
5605 /* Given two ranges, see if we can merge them into one. Return 1 if we
5606 can, 0 if we can't. Set the output range into the specified parameters. */
5608 bool
5609 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5610 tree high0, int in1_p, tree low1, tree high1)
5612 int no_overlap;
5613 int subset;
5614 int temp;
5615 tree tem;
5616 int in_p;
5617 tree low, high;
5618 int lowequal = ((low0 == 0 && low1 == 0)
5619 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5620 low0, 0, low1, 0)));
5621 int highequal = ((high0 == 0 && high1 == 0)
5622 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5623 high0, 1, high1, 1)));
5625 /* Make range 0 be the range that starts first, or ends last if they
5626 start at the same value. Swap them if it isn't. */
5627 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5628 low0, 0, low1, 0))
5629 || (lowequal
5630 && integer_onep (range_binop (GT_EXPR, integer_type_node,
5631 high1, 1, high0, 1))))
5633 temp = in0_p, in0_p = in1_p, in1_p = temp;
5634 tem = low0, low0 = low1, low1 = tem;
5635 tem = high0, high0 = high1, high1 = tem;
5638 /* If the second range is != high1 where high1 is the type maximum of
5639 the type, try first merging with < high1 range. */
5640 if (low1
5641 && high1
5642 && TREE_CODE (low1) == INTEGER_CST
5643 && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5644 || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5645 && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5646 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5647 && operand_equal_p (low1, high1, 0))
5649 if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5650 && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5651 !in1_p, NULL_TREE, range_predecessor (low1)))
5652 return true;
5653 /* Similarly for the second range != low1 where low1 is the type minimum
5654 of the type, try first merging with > low1 range. */
5655 if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5656 && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5657 !in1_p, range_successor (low1), NULL_TREE))
5658 return true;
5661 /* Now flag two cases, whether the ranges are disjoint or whether the
5662 second range is totally subsumed in the first. Note that the tests
5663 below are simplified by the ones above. */
5664 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5665 high0, 1, low1, 0));
5666 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5667 high1, 1, high0, 1));
5669 /* We now have four cases, depending on whether we are including or
5670 excluding the two ranges. */
5671 if (in0_p && in1_p)
5673 /* If they don't overlap, the result is false. If the second range
5674 is a subset it is the result. Otherwise, the range is from the start
5675 of the second to the end of the first. */
5676 if (no_overlap)
5677 in_p = 0, low = high = 0;
5678 else if (subset)
5679 in_p = 1, low = low1, high = high1;
5680 else
5681 in_p = 1, low = low1, high = high0;
5684 else if (in0_p && ! in1_p)
5686 /* If they don't overlap, the result is the first range. If they are
5687 equal, the result is false. If the second range is a subset of the
5688 first, and the ranges begin at the same place, we go from just after
5689 the end of the second range to the end of the first. If the second
5690 range is not a subset of the first, or if it is a subset and both
5691 ranges end at the same place, the range starts at the start of the
5692 first range and ends just before the second range.
5693 Otherwise, we can't describe this as a single range. */
5694 if (no_overlap)
5695 in_p = 1, low = low0, high = high0;
5696 else if (lowequal && highequal)
5697 in_p = 0, low = high = 0;
5698 else if (subset && lowequal)
5700 low = range_successor (high1);
5701 high = high0;
5702 in_p = 1;
5703 if (low == 0)
5705 /* We are in the weird situation where high0 > high1 but
5706 high1 has no successor. Punt. */
5707 return 0;
5710 else if (! subset || highequal)
5712 low = low0;
5713 high = range_predecessor (low1);
5714 in_p = 1;
5715 if (high == 0)
5717 /* low0 < low1 but low1 has no predecessor. Punt. */
5718 return 0;
5721 else
5722 return 0;
5725 else if (! in0_p && in1_p)
5727 /* If they don't overlap, the result is the second range. If the second
5728 is a subset of the first, the result is false. Otherwise,
5729 the range starts just after the first range and ends at the
5730 end of the second. */
5731 if (no_overlap)
5732 in_p = 1, low = low1, high = high1;
5733 else if (subset || highequal)
5734 in_p = 0, low = high = 0;
5735 else
5737 low = range_successor (high0);
5738 high = high1;
5739 in_p = 1;
5740 if (low == 0)
5742 /* high1 > high0 but high0 has no successor. Punt. */
5743 return 0;
5748 else
5750 /* The case where we are excluding both ranges. Here the complex case
5751 is if they don't overlap. In that case, the only time we have a
5752 range is if they are adjacent. If the second is a subset of the
5753 first, the result is the first. Otherwise, the range to exclude
5754 starts at the beginning of the first range and ends at the end of the
5755 second. */
5756 if (no_overlap)
5758 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5759 range_successor (high0),
5760 1, low1, 0)))
5761 in_p = 0, low = low0, high = high1;
5762 else
5764 /* Canonicalize - [min, x] into - [-, x]. */
5765 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5766 switch (TREE_CODE (TREE_TYPE (low0)))
5768 case ENUMERAL_TYPE:
5769 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5770 GET_MODE_BITSIZE
5771 (TYPE_MODE (TREE_TYPE (low0)))))
5772 break;
5773 /* FALLTHROUGH */
5774 case INTEGER_TYPE:
5775 if (tree_int_cst_equal (low0,
5776 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5777 low0 = 0;
5778 break;
5779 case POINTER_TYPE:
5780 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5781 && integer_zerop (low0))
5782 low0 = 0;
5783 break;
5784 default:
5785 break;
5788 /* Canonicalize - [x, max] into - [x, -]. */
5789 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5790 switch (TREE_CODE (TREE_TYPE (high1)))
5792 case ENUMERAL_TYPE:
5793 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5794 GET_MODE_BITSIZE
5795 (TYPE_MODE (TREE_TYPE (high1)))))
5796 break;
5797 /* FALLTHROUGH */
5798 case INTEGER_TYPE:
5799 if (tree_int_cst_equal (high1,
5800 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5801 high1 = 0;
5802 break;
5803 case POINTER_TYPE:
5804 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5805 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5806 high1, 1,
5807 build_int_cst (TREE_TYPE (high1), 1),
5808 1)))
5809 high1 = 0;
5810 break;
5811 default:
5812 break;
5815 /* The ranges might be also adjacent between the maximum and
5816 minimum values of the given type. For
5817 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5818 return + [x + 1, y - 1]. */
5819 if (low0 == 0 && high1 == 0)
5821 low = range_successor (high0);
5822 high = range_predecessor (low1);
5823 if (low == 0 || high == 0)
5824 return 0;
5826 in_p = 1;
5828 else
5829 return 0;
5832 else if (subset)
5833 in_p = 0, low = low0, high = high0;
5834 else
5835 in_p = 0, low = low0, high = high1;
5838 *pin_p = in_p, *plow = low, *phigh = high;
5839 return 1;
5843 /* Subroutine of fold, looking inside expressions of the form
5844 A op B ? A : C, where (ARG00, COMP_CODE, ARG01), ARG1 and ARG2
5845 are the three operands of the COND_EXPR. This function is
5846 being used also to optimize A op B ? C : A, by reversing the
5847 comparison first.
5849 Return a folded expression whose code is not a COND_EXPR
5850 anymore, or NULL_TREE if no folding opportunity is found. */
5852 static tree
5853 fold_cond_expr_with_comparison (location_t loc, tree type,
5854 enum tree_code comp_code,
5855 tree arg00, tree arg01, tree arg1, tree arg2)
5857 tree arg1_type = TREE_TYPE (arg1);
5858 tree tem;
5860 STRIP_NOPS (arg1);
5861 STRIP_NOPS (arg2);
5863 /* If we have A op 0 ? A : -A, consider applying the following
5864 transformations:
5866 A == 0? A : -A same as -A
5867 A != 0? A : -A same as A
5868 A >= 0? A : -A same as abs (A)
5869 A > 0? A : -A same as abs (A)
5870 A <= 0? A : -A same as -abs (A)
5871 A < 0? A : -A same as -abs (A)
5873 None of these transformations work for modes with signed
5874 zeros. If A is +/-0, the first two transformations will
5875 change the sign of the result (from +0 to -0, or vice
5876 versa). The last four will fix the sign of the result,
5877 even though the original expressions could be positive or
5878 negative, depending on the sign of A.
5880 Note that all these transformations are correct if A is
5881 NaN, since the two alternatives (A and -A) are also NaNs. */
5882 if (!HONOR_SIGNED_ZEROS (type)
5883 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5884 ? real_zerop (arg01)
5885 : integer_zerop (arg01))
5886 && ((TREE_CODE (arg2) == NEGATE_EXPR
5887 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5888 /* In the case that A is of the form X-Y, '-A' (arg2) may
5889 have already been folded to Y-X, check for that. */
5890 || (TREE_CODE (arg1) == MINUS_EXPR
5891 && TREE_CODE (arg2) == MINUS_EXPR
5892 && operand_equal_p (TREE_OPERAND (arg1, 0),
5893 TREE_OPERAND (arg2, 1), 0)
5894 && operand_equal_p (TREE_OPERAND (arg1, 1),
5895 TREE_OPERAND (arg2, 0), 0))))
5896 switch (comp_code)
5898 case EQ_EXPR:
5899 case UNEQ_EXPR:
5900 tem = fold_convert_loc (loc, arg1_type, arg1);
5901 return fold_convert_loc (loc, type, negate_expr (tem));
5902 case NE_EXPR:
5903 case LTGT_EXPR:
5904 return fold_convert_loc (loc, type, arg1);
5905 case UNGE_EXPR:
5906 case UNGT_EXPR:
5907 if (flag_trapping_math)
5908 break;
5909 /* Fall through. */
5910 case GE_EXPR:
5911 case GT_EXPR:
5912 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5913 break;
5914 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5915 return fold_convert_loc (loc, type, tem);
5916 case UNLE_EXPR:
5917 case UNLT_EXPR:
5918 if (flag_trapping_math)
5919 break;
5920 /* FALLTHRU */
5921 case LE_EXPR:
5922 case LT_EXPR:
5923 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5924 break;
5925 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg1))
5926 && !TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
5928 /* A <= 0 ? A : -A for A INT_MIN is valid, but -abs(INT_MIN)
5929 is not, invokes UB both in abs and in the negation of it.
5930 So, use ABSU_EXPR instead. */
5931 tree utype = unsigned_type_for (TREE_TYPE (arg1));
5932 tem = fold_build1_loc (loc, ABSU_EXPR, utype, arg1);
5933 tem = negate_expr (tem);
5934 return fold_convert_loc (loc, type, tem);
5936 else
5938 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5939 return negate_expr (fold_convert_loc (loc, type, tem));
5941 default:
5942 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5943 break;
5946 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5947 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5948 both transformations are correct when A is NaN: A != 0
5949 is then true, and A == 0 is false. */
5951 if (!HONOR_SIGNED_ZEROS (type)
5952 && integer_zerop (arg01) && integer_zerop (arg2))
5954 if (comp_code == NE_EXPR)
5955 return fold_convert_loc (loc, type, arg1);
5956 else if (comp_code == EQ_EXPR)
5957 return build_zero_cst (type);
5960 /* Try some transformations of A op B ? A : B.
5962 A == B? A : B same as B
5963 A != B? A : B same as A
5964 A >= B? A : B same as max (A, B)
5965 A > B? A : B same as max (B, A)
5966 A <= B? A : B same as min (A, B)
5967 A < B? A : B same as min (B, A)
5969 As above, these transformations don't work in the presence
5970 of signed zeros. For example, if A and B are zeros of
5971 opposite sign, the first two transformations will change
5972 the sign of the result. In the last four, the original
5973 expressions give different results for (A=+0, B=-0) and
5974 (A=-0, B=+0), but the transformed expressions do not.
5976 The first two transformations are correct if either A or B
5977 is a NaN. In the first transformation, the condition will
5978 be false, and B will indeed be chosen. In the case of the
5979 second transformation, the condition A != B will be true,
5980 and A will be chosen.
5982 The conversions to max() and min() are not correct if B is
5983 a number and A is not. The conditions in the original
5984 expressions will be false, so all four give B. The min()
5985 and max() versions would give a NaN instead. */
5986 if (!HONOR_SIGNED_ZEROS (type)
5987 && operand_equal_for_comparison_p (arg01, arg2)
5988 /* Avoid these transformations if the COND_EXPR may be used
5989 as an lvalue in the C++ front-end. PR c++/19199. */
5990 && (in_gimple_form
5991 || VECTOR_TYPE_P (type)
5992 || (! lang_GNU_CXX ()
5993 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5994 || ! maybe_lvalue_p (arg1)
5995 || ! maybe_lvalue_p (arg2)))
5997 tree comp_op0 = arg00;
5998 tree comp_op1 = arg01;
5999 tree comp_type = TREE_TYPE (comp_op0);
6001 switch (comp_code)
6003 case EQ_EXPR:
6004 return fold_convert_loc (loc, type, arg2);
6005 case NE_EXPR:
6006 return fold_convert_loc (loc, type, arg1);
6007 case LE_EXPR:
6008 case LT_EXPR:
6009 case UNLE_EXPR:
6010 case UNLT_EXPR:
6011 /* In C++ a ?: expression can be an lvalue, so put the
6012 operand which will be used if they are equal first
6013 so that we can convert this back to the
6014 corresponding COND_EXPR. */
6015 if (!HONOR_NANS (arg1))
6017 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6018 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6019 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
6020 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
6021 : fold_build2_loc (loc, MIN_EXPR, comp_type,
6022 comp_op1, comp_op0);
6023 return fold_convert_loc (loc, type, tem);
6025 break;
6026 case GE_EXPR:
6027 case GT_EXPR:
6028 case UNGE_EXPR:
6029 case UNGT_EXPR:
6030 if (!HONOR_NANS (arg1))
6032 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
6033 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
6034 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
6035 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
6036 : fold_build2_loc (loc, MAX_EXPR, comp_type,
6037 comp_op1, comp_op0);
6038 return fold_convert_loc (loc, type, tem);
6040 break;
6041 case UNEQ_EXPR:
6042 if (!HONOR_NANS (arg1))
6043 return fold_convert_loc (loc, type, arg2);
6044 break;
6045 case LTGT_EXPR:
6046 if (!HONOR_NANS (arg1))
6047 return fold_convert_loc (loc, type, arg1);
6048 break;
6049 default:
6050 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
6051 break;
6055 return NULL_TREE;
6060 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
6061 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
6062 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
6063 false) >= 2)
6064 #endif
6066 /* EXP is some logical combination of boolean tests. See if we can
6067 merge it into some range test. Return the new tree if so. */
6069 static tree
6070 fold_range_test (location_t loc, enum tree_code code, tree type,
6071 tree op0, tree op1)
6073 int or_op = (code == TRUTH_ORIF_EXPR
6074 || code == TRUTH_OR_EXPR);
6075 int in0_p, in1_p, in_p;
6076 tree low0, low1, low, high0, high1, high;
6077 bool strict_overflow_p = false;
6078 tree tem, lhs, rhs;
6079 const char * const warnmsg = G_("assuming signed overflow does not occur "
6080 "when simplifying range test");
6082 if (!INTEGRAL_TYPE_P (type))
6083 return 0;
6085 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
6086 /* If op0 is known true or false and this is a short-circuiting
6087 operation we must not merge with op1 since that makes side-effects
6088 unconditional. So special-case this. */
6089 if (!lhs
6090 && ((code == TRUTH_ORIF_EXPR && in0_p)
6091 || (code == TRUTH_ANDIF_EXPR && !in0_p)))
6092 return op0;
6093 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
6095 /* If this is an OR operation, invert both sides; we will invert
6096 again at the end. */
6097 if (or_op)
6098 in0_p = ! in0_p, in1_p = ! in1_p;
6100 /* If both expressions are the same, if we can merge the ranges, and we
6101 can build the range test, return it or it inverted. If one of the
6102 ranges is always true or always false, consider it to be the same
6103 expression as the other. */
6104 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
6105 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
6106 in1_p, low1, high1)
6107 && (tem = (build_range_check (loc, type,
6108 lhs != 0 ? lhs
6109 : rhs != 0 ? rhs : integer_zero_node,
6110 in_p, low, high))) != 0)
6112 if (strict_overflow_p)
6113 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
6114 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
6117 /* On machines where the branch cost is expensive, if this is a
6118 short-circuited branch and the underlying object on both sides
6119 is the same, make a non-short-circuit operation. */
6120 bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
6121 if (param_logical_op_non_short_circuit != -1)
6122 logical_op_non_short_circuit
6123 = param_logical_op_non_short_circuit;
6124 if (logical_op_non_short_circuit
6125 && !sanitize_coverage_p ()
6126 && lhs != 0 && rhs != 0
6127 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
6128 && operand_equal_p (lhs, rhs, 0))
6130 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
6131 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
6132 which cases we can't do this. */
6133 if (simple_operand_p (lhs))
6134 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6135 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6136 type, op0, op1);
6138 else if (!lang_hooks.decls.global_bindings_p ()
6139 && !CONTAINS_PLACEHOLDER_P (lhs))
6141 tree common = save_expr (lhs);
6143 if ((lhs = build_range_check (loc, type, common,
6144 or_op ? ! in0_p : in0_p,
6145 low0, high0)) != 0
6146 && (rhs = build_range_check (loc, type, common,
6147 or_op ? ! in1_p : in1_p,
6148 low1, high1)) != 0)
6150 if (strict_overflow_p)
6151 fold_overflow_warning (warnmsg,
6152 WARN_STRICT_OVERFLOW_COMPARISON);
6153 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
6154 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
6155 type, lhs, rhs);
6160 return 0;
6163 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
6164 bit value. Arrange things so the extra bits will be set to zero if and
6165 only if C is signed-extended to its full width. If MASK is nonzero,
6166 it is an INTEGER_CST that should be AND'ed with the extra bits. */
6168 static tree
6169 unextend (tree c, int p, int unsignedp, tree mask)
6171 tree type = TREE_TYPE (c);
6172 int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
6173 tree temp;
6175 if (p == modesize || unsignedp)
6176 return c;
6178 /* We work by getting just the sign bit into the low-order bit, then
6179 into the high-order bit, then sign-extend. We then XOR that value
6180 with C. */
6181 temp = build_int_cst (TREE_TYPE (c),
6182 wi::extract_uhwi (wi::to_wide (c), p - 1, 1));
6184 /* We must use a signed type in order to get an arithmetic right shift.
6185 However, we must also avoid introducing accidental overflows, so that
6186 a subsequent call to integer_zerop will work. Hence we must
6187 do the type conversion here. At this point, the constant is either
6188 zero or one, and the conversion to a signed type can never overflow.
6189 We could get an overflow if this conversion is done anywhere else. */
6190 if (TYPE_UNSIGNED (type))
6191 temp = fold_convert (signed_type_for (type), temp);
6193 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
6194 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
6195 if (mask != 0)
6196 temp = const_binop (BIT_AND_EXPR, temp,
6197 fold_convert (TREE_TYPE (c), mask));
6198 /* If necessary, convert the type back to match the type of C. */
6199 if (TYPE_UNSIGNED (type))
6200 temp = fold_convert (type, temp);
6202 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
6205 /* For an expression that has the form
6206 (A && B) || ~B
6208 (A || B) && ~B,
6209 we can drop one of the inner expressions and simplify to
6210 A || ~B
6212 A && ~B
6213 LOC is the location of the resulting expression. OP is the inner
6214 logical operation; the left-hand side in the examples above, while CMPOP
6215 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
6216 removing a condition that guards another, as in
6217 (A != NULL && A->...) || A == NULL
6218 which we must not transform. If RHS_ONLY is true, only eliminate the
6219 right-most operand of the inner logical operation. */
6221 static tree
6222 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
6223 bool rhs_only)
6225 tree type = TREE_TYPE (cmpop);
6226 enum tree_code code = TREE_CODE (cmpop);
6227 enum tree_code truthop_code = TREE_CODE (op);
6228 tree lhs = TREE_OPERAND (op, 0);
6229 tree rhs = TREE_OPERAND (op, 1);
6230 tree orig_lhs = lhs, orig_rhs = rhs;
6231 enum tree_code rhs_code = TREE_CODE (rhs);
6232 enum tree_code lhs_code = TREE_CODE (lhs);
6233 enum tree_code inv_code;
6235 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
6236 return NULL_TREE;
6238 if (TREE_CODE_CLASS (code) != tcc_comparison)
6239 return NULL_TREE;
6241 if (rhs_code == truthop_code)
6243 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
6244 if (newrhs != NULL_TREE)
6246 rhs = newrhs;
6247 rhs_code = TREE_CODE (rhs);
6250 if (lhs_code == truthop_code && !rhs_only)
6252 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
6253 if (newlhs != NULL_TREE)
6255 lhs = newlhs;
6256 lhs_code = TREE_CODE (lhs);
6260 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
6261 if (inv_code == rhs_code
6262 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
6263 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
6264 return lhs;
6265 if (!rhs_only && inv_code == lhs_code
6266 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
6267 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
6268 return rhs;
6269 if (rhs != orig_rhs || lhs != orig_lhs)
6270 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
6271 lhs, rhs);
6272 return NULL_TREE;
6275 /* Find ways of folding logical expressions of LHS and RHS:
6276 Try to merge two comparisons to the same innermost item.
6277 Look for range tests like "ch >= '0' && ch <= '9'".
6278 Look for combinations of simple terms on machines with expensive branches
6279 and evaluate the RHS unconditionally.
6281 For example, if we have p->a == 2 && p->b == 4 and we can make an
6282 object large enough to span both A and B, we can do this with a comparison
6283 against the object ANDed with the a mask.
6285 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
6286 operations to do this with one comparison.
6288 We check for both normal comparisons and the BIT_AND_EXPRs made this by
6289 function and the one above.
6291 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
6292 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
6294 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
6295 two operands.
6297 We return the simplified tree or 0 if no optimization is possible. */
6299 static tree
6300 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
6301 tree lhs, tree rhs)
6303 /* If this is the "or" of two comparisons, we can do something if
6304 the comparisons are NE_EXPR. If this is the "and", we can do something
6305 if the comparisons are EQ_EXPR. I.e.,
6306 (a->b == 2 && a->c == 4) can become (a->new == NEW).
6308 WANTED_CODE is this operation code. For single bit fields, we can
6309 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
6310 comparison for one-bit fields. */
6312 enum tree_code wanted_code;
6313 enum tree_code lcode, rcode;
6314 tree ll_arg, lr_arg, rl_arg, rr_arg;
6315 tree ll_inner, lr_inner, rl_inner, rr_inner;
6316 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
6317 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
6318 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
6319 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
6320 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
6321 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
6322 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
6323 scalar_int_mode lnmode, rnmode;
6324 tree ll_mask, lr_mask, rl_mask, rr_mask;
6325 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
6326 tree l_const, r_const;
6327 tree lntype, rntype, result;
6328 HOST_WIDE_INT first_bit, end_bit;
6329 int volatilep;
6331 /* Start by getting the comparison codes. Fail if anything is volatile.
6332 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
6333 it were surrounded with a NE_EXPR. */
6335 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
6336 return 0;
6338 lcode = TREE_CODE (lhs);
6339 rcode = TREE_CODE (rhs);
6341 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
6343 lhs = build2 (NE_EXPR, truth_type, lhs,
6344 build_int_cst (TREE_TYPE (lhs), 0));
6345 lcode = NE_EXPR;
6348 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
6350 rhs = build2 (NE_EXPR, truth_type, rhs,
6351 build_int_cst (TREE_TYPE (rhs), 0));
6352 rcode = NE_EXPR;
6355 if (TREE_CODE_CLASS (lcode) != tcc_comparison
6356 || TREE_CODE_CLASS (rcode) != tcc_comparison)
6357 return 0;
6359 ll_arg = TREE_OPERAND (lhs, 0);
6360 lr_arg = TREE_OPERAND (lhs, 1);
6361 rl_arg = TREE_OPERAND (rhs, 0);
6362 rr_arg = TREE_OPERAND (rhs, 1);
6364 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
6365 if (simple_operand_p (ll_arg)
6366 && simple_operand_p (lr_arg))
6368 if (operand_equal_p (ll_arg, rl_arg, 0)
6369 && operand_equal_p (lr_arg, rr_arg, 0))
6371 result = combine_comparisons (loc, code, lcode, rcode,
6372 truth_type, ll_arg, lr_arg);
6373 if (result)
6374 return result;
6376 else if (operand_equal_p (ll_arg, rr_arg, 0)
6377 && operand_equal_p (lr_arg, rl_arg, 0))
6379 result = combine_comparisons (loc, code, lcode,
6380 swap_tree_comparison (rcode),
6381 truth_type, ll_arg, lr_arg);
6382 if (result)
6383 return result;
6387 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
6388 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
6390 /* If the RHS can be evaluated unconditionally and its operands are
6391 simple, it wins to evaluate the RHS unconditionally on machines
6392 with expensive branches. In this case, this isn't a comparison
6393 that can be merged. */
6395 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
6396 false) >= 2
6397 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
6398 && simple_operand_p (rl_arg)
6399 && simple_operand_p (rr_arg))
6401 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
6402 if (code == TRUTH_OR_EXPR
6403 && lcode == NE_EXPR && integer_zerop (lr_arg)
6404 && rcode == NE_EXPR && integer_zerop (rr_arg)
6405 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6406 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6407 return build2_loc (loc, NE_EXPR, truth_type,
6408 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6409 ll_arg, rl_arg),
6410 build_int_cst (TREE_TYPE (ll_arg), 0));
6412 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
6413 if (code == TRUTH_AND_EXPR
6414 && lcode == EQ_EXPR && integer_zerop (lr_arg)
6415 && rcode == EQ_EXPR && integer_zerop (rr_arg)
6416 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
6417 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
6418 return build2_loc (loc, EQ_EXPR, truth_type,
6419 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
6420 ll_arg, rl_arg),
6421 build_int_cst (TREE_TYPE (ll_arg), 0));
6424 /* See if the comparisons can be merged. Then get all the parameters for
6425 each side. */
6427 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
6428 || (rcode != EQ_EXPR && rcode != NE_EXPR))
6429 return 0;
6431 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
6432 volatilep = 0;
6433 ll_inner = decode_field_reference (loc, &ll_arg,
6434 &ll_bitsize, &ll_bitpos, &ll_mode,
6435 &ll_unsignedp, &ll_reversep, &volatilep,
6436 &ll_mask, &ll_and_mask);
6437 lr_inner = decode_field_reference (loc, &lr_arg,
6438 &lr_bitsize, &lr_bitpos, &lr_mode,
6439 &lr_unsignedp, &lr_reversep, &volatilep,
6440 &lr_mask, &lr_and_mask);
6441 rl_inner = decode_field_reference (loc, &rl_arg,
6442 &rl_bitsize, &rl_bitpos, &rl_mode,
6443 &rl_unsignedp, &rl_reversep, &volatilep,
6444 &rl_mask, &rl_and_mask);
6445 rr_inner = decode_field_reference (loc, &rr_arg,
6446 &rr_bitsize, &rr_bitpos, &rr_mode,
6447 &rr_unsignedp, &rr_reversep, &volatilep,
6448 &rr_mask, &rr_and_mask);
6450 /* It must be true that the inner operation on the lhs of each
6451 comparison must be the same if we are to be able to do anything.
6452 Then see if we have constants. If not, the same must be true for
6453 the rhs's. */
6454 if (volatilep
6455 || ll_reversep != rl_reversep
6456 || ll_inner == 0 || rl_inner == 0
6457 || ! operand_equal_p (ll_inner, rl_inner, 0))
6458 return 0;
6460 if (TREE_CODE (lr_arg) == INTEGER_CST
6461 && TREE_CODE (rr_arg) == INTEGER_CST)
6463 l_const = lr_arg, r_const = rr_arg;
6464 lr_reversep = ll_reversep;
6466 else if (lr_reversep != rr_reversep
6467 || lr_inner == 0 || rr_inner == 0
6468 || ! operand_equal_p (lr_inner, rr_inner, 0))
6469 return 0;
6470 else
6471 l_const = r_const = 0;
6473 /* If either comparison code is not correct for our logical operation,
6474 fail. However, we can convert a one-bit comparison against zero into
6475 the opposite comparison against that bit being set in the field. */
6477 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
6478 if (lcode != wanted_code)
6480 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
6482 /* Make the left operand unsigned, since we are only interested
6483 in the value of one bit. Otherwise we are doing the wrong
6484 thing below. */
6485 ll_unsignedp = 1;
6486 l_const = ll_mask;
6488 else
6489 return 0;
6492 /* This is analogous to the code for l_const above. */
6493 if (rcode != wanted_code)
6495 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
6497 rl_unsignedp = 1;
6498 r_const = rl_mask;
6500 else
6501 return 0;
6504 /* See if we can find a mode that contains both fields being compared on
6505 the left. If we can't, fail. Otherwise, update all constants and masks
6506 to be relative to a field of that size. */
6507 first_bit = MIN (ll_bitpos, rl_bitpos);
6508 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
6509 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
6510 TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
6511 volatilep, &lnmode))
6512 return 0;
6514 lnbitsize = GET_MODE_BITSIZE (lnmode);
6515 lnbitpos = first_bit & ~ (lnbitsize - 1);
6516 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
6517 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
6519 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
6521 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
6522 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
6525 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
6526 size_int (xll_bitpos));
6527 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
6528 size_int (xrl_bitpos));
6529 if (ll_mask == NULL_TREE || rl_mask == NULL_TREE)
6530 return 0;
6532 if (l_const)
6534 l_const = fold_convert_loc (loc, lntype, l_const);
6535 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
6536 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
6537 if (l_const == NULL_TREE)
6538 return 0;
6539 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
6540 fold_build1_loc (loc, BIT_NOT_EXPR,
6541 lntype, ll_mask))))
6543 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6545 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6548 if (r_const)
6550 r_const = fold_convert_loc (loc, lntype, r_const);
6551 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
6552 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
6553 if (r_const == NULL_TREE)
6554 return 0;
6555 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
6556 fold_build1_loc (loc, BIT_NOT_EXPR,
6557 lntype, rl_mask))))
6559 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6561 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6565 /* If the right sides are not constant, do the same for it. Also,
6566 disallow this optimization if a size, signedness or storage order
6567 mismatch occurs between the left and right sides. */
6568 if (l_const == 0)
6570 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
6571 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
6572 || ll_reversep != lr_reversep
6573 /* Make sure the two fields on the right
6574 correspond to the left without being swapped. */
6575 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
6576 return 0;
6578 first_bit = MIN (lr_bitpos, rr_bitpos);
6579 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
6580 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
6581 TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
6582 volatilep, &rnmode))
6583 return 0;
6585 rnbitsize = GET_MODE_BITSIZE (rnmode);
6586 rnbitpos = first_bit & ~ (rnbitsize - 1);
6587 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
6588 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
6590 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
6592 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
6593 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
6596 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6597 rntype, lr_mask),
6598 size_int (xlr_bitpos));
6599 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6600 rntype, rr_mask),
6601 size_int (xrr_bitpos));
6602 if (lr_mask == NULL_TREE || rr_mask == NULL_TREE)
6603 return 0;
6605 /* Make a mask that corresponds to both fields being compared.
6606 Do this for both items being compared. If the operands are the
6607 same size and the bits being compared are in the same position
6608 then we can do this by masking both and comparing the masked
6609 results. */
6610 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6611 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
6612 if (lnbitsize == rnbitsize
6613 && xll_bitpos == xlr_bitpos
6614 && lnbitpos >= 0
6615 && rnbitpos >= 0)
6617 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
6618 lntype, lnbitsize, lnbitpos,
6619 ll_unsignedp || rl_unsignedp, ll_reversep);
6620 if (! all_ones_mask_p (ll_mask, lnbitsize))
6621 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6623 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
6624 rntype, rnbitsize, rnbitpos,
6625 lr_unsignedp || rr_unsignedp, lr_reversep);
6626 if (! all_ones_mask_p (lr_mask, rnbitsize))
6627 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6629 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6632 /* There is still another way we can do something: If both pairs of
6633 fields being compared are adjacent, we may be able to make a wider
6634 field containing them both.
6636 Note that we still must mask the lhs/rhs expressions. Furthermore,
6637 the mask must be shifted to account for the shift done by
6638 make_bit_field_ref. */
6639 if (((ll_bitsize + ll_bitpos == rl_bitpos
6640 && lr_bitsize + lr_bitpos == rr_bitpos)
6641 || (ll_bitpos == rl_bitpos + rl_bitsize
6642 && lr_bitpos == rr_bitpos + rr_bitsize))
6643 && ll_bitpos >= 0
6644 && rl_bitpos >= 0
6645 && lr_bitpos >= 0
6646 && rr_bitpos >= 0)
6648 tree type;
6650 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
6651 ll_bitsize + rl_bitsize,
6652 MIN (ll_bitpos, rl_bitpos),
6653 ll_unsignedp, ll_reversep);
6654 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
6655 lr_bitsize + rr_bitsize,
6656 MIN (lr_bitpos, rr_bitpos),
6657 lr_unsignedp, lr_reversep);
6659 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6660 size_int (MIN (xll_bitpos, xrl_bitpos)));
6661 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6662 size_int (MIN (xlr_bitpos, xrr_bitpos)));
6663 if (ll_mask == NULL_TREE || lr_mask == NULL_TREE)
6664 return 0;
6666 /* Convert to the smaller type before masking out unwanted bits. */
6667 type = lntype;
6668 if (lntype != rntype)
6670 if (lnbitsize > rnbitsize)
6672 lhs = fold_convert_loc (loc, rntype, lhs);
6673 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6674 type = rntype;
6676 else if (lnbitsize < rnbitsize)
6678 rhs = fold_convert_loc (loc, lntype, rhs);
6679 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6680 type = lntype;
6684 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6685 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6687 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6688 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6690 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6693 return 0;
6696 /* Handle the case of comparisons with constants. If there is something in
6697 common between the masks, those bits of the constants must be the same.
6698 If not, the condition is always false. Test for this to avoid generating
6699 incorrect code below. */
6700 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
6701 if (! integer_zerop (result)
6702 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
6703 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
6705 if (wanted_code == NE_EXPR)
6707 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6708 return constant_boolean_node (true, truth_type);
6710 else
6712 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6713 return constant_boolean_node (false, truth_type);
6717 if (lnbitpos < 0)
6718 return 0;
6720 /* Construct the expression we will return. First get the component
6721 reference we will make. Unless the mask is all ones the width of
6722 that field, perform the mask operation. Then compare with the
6723 merged constant. */
6724 result = make_bit_field_ref (loc, ll_inner, ll_arg,
6725 lntype, lnbitsize, lnbitpos,
6726 ll_unsignedp || rl_unsignedp, ll_reversep);
6728 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6729 if (! all_ones_mask_p (ll_mask, lnbitsize))
6730 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
6732 return build2_loc (loc, wanted_code, truth_type, result,
6733 const_binop (BIT_IOR_EXPR, l_const, r_const));
6736 /* T is an integer expression that is being multiplied, divided, or taken a
6737 modulus (CODE says which and what kind of divide or modulus) by a
6738 constant C. See if we can eliminate that operation by folding it with
6739 other operations already in T. WIDE_TYPE, if non-null, is a type that
6740 should be used for the computation if wider than our type.
6742 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6743 (X * 2) + (Y * 4). We must, however, be assured that either the original
6744 expression would not overflow or that overflow is undefined for the type
6745 in the language in question.
6747 If we return a non-null expression, it is an equivalent form of the
6748 original computation, but need not be in the original type.
6750 We set *STRICT_OVERFLOW_P to true if the return values depends on
6751 signed overflow being undefined. Otherwise we do not change
6752 *STRICT_OVERFLOW_P. */
6754 static tree
6755 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6756 bool *strict_overflow_p)
6758 /* To avoid exponential search depth, refuse to allow recursion past
6759 three levels. Beyond that (1) it's highly unlikely that we'll find
6760 something interesting and (2) we've probably processed it before
6761 when we built the inner expression. */
6763 static int depth;
6764 tree ret;
6766 if (depth > 3)
6767 return NULL;
6769 depth++;
6770 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6771 depth--;
6773 return ret;
6776 static tree
6777 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6778 bool *strict_overflow_p)
6780 tree type = TREE_TYPE (t);
6781 enum tree_code tcode = TREE_CODE (t);
6782 tree ctype = (wide_type != 0
6783 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6784 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6785 ? wide_type : type);
6786 tree t1, t2;
6787 int same_p = tcode == code;
6788 tree op0 = NULL_TREE, op1 = NULL_TREE;
6789 bool sub_strict_overflow_p;
6791 /* Don't deal with constants of zero here; they confuse the code below. */
6792 if (integer_zerop (c))
6793 return NULL_TREE;
6795 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6796 op0 = TREE_OPERAND (t, 0);
6798 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6799 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6801 /* Note that we need not handle conditional operations here since fold
6802 already handles those cases. So just do arithmetic here. */
6803 switch (tcode)
6805 case INTEGER_CST:
6806 /* For a constant, we can always simplify if we are a multiply
6807 or (for divide and modulus) if it is a multiple of our constant. */
6808 if (code == MULT_EXPR
6809 || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6810 TYPE_SIGN (type)))
6812 tree tem = const_binop (code, fold_convert (ctype, t),
6813 fold_convert (ctype, c));
6814 /* If the multiplication overflowed, we lost information on it.
6815 See PR68142 and PR69845. */
6816 if (TREE_OVERFLOW (tem))
6817 return NULL_TREE;
6818 return tem;
6820 break;
6822 CASE_CONVERT: case NON_LVALUE_EXPR:
6823 if (!INTEGRAL_TYPE_P (TREE_TYPE (op0)))
6824 break;
6825 /* If op0 is an expression ... */
6826 if ((COMPARISON_CLASS_P (op0)
6827 || UNARY_CLASS_P (op0)
6828 || BINARY_CLASS_P (op0)
6829 || VL_EXP_CLASS_P (op0)
6830 || EXPRESSION_CLASS_P (op0))
6831 /* ... and has wrapping overflow, and its type is smaller
6832 than ctype, then we cannot pass through as widening. */
6833 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6834 && (TYPE_PRECISION (ctype)
6835 > TYPE_PRECISION (TREE_TYPE (op0))))
6836 /* ... or this is a truncation (t is narrower than op0),
6837 then we cannot pass through this narrowing. */
6838 || (TYPE_PRECISION (type)
6839 < TYPE_PRECISION (TREE_TYPE (op0)))
6840 /* ... or signedness changes for division or modulus,
6841 then we cannot pass through this conversion. */
6842 || (code != MULT_EXPR
6843 && (TYPE_UNSIGNED (ctype)
6844 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6845 /* ... or has undefined overflow while the converted to
6846 type has not, we cannot do the operation in the inner type
6847 as that would introduce undefined overflow. */
6848 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6849 && !TYPE_OVERFLOW_UNDEFINED (type))))
6850 break;
6852 /* Pass the constant down and see if we can make a simplification. If
6853 we can, replace this expression with the inner simplification for
6854 possible later conversion to our or some other type. */
6855 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6856 && TREE_CODE (t2) == INTEGER_CST
6857 && !TREE_OVERFLOW (t2)
6858 && (t1 = extract_muldiv (op0, t2, code,
6859 code == MULT_EXPR ? ctype : NULL_TREE,
6860 strict_overflow_p)) != 0)
6861 return t1;
6862 break;
6864 case ABS_EXPR:
6865 /* If widening the type changes it from signed to unsigned, then we
6866 must avoid building ABS_EXPR itself as unsigned. */
6867 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6869 tree cstype = (*signed_type_for) (ctype);
6870 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6871 != 0)
6873 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6874 return fold_convert (ctype, t1);
6876 break;
6878 /* If the constant is negative, we cannot simplify this. */
6879 if (tree_int_cst_sgn (c) == -1)
6880 break;
6881 /* FALLTHROUGH */
6882 case NEGATE_EXPR:
6883 /* For division and modulus, type can't be unsigned, as e.g.
6884 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6885 For signed types, even with wrapping overflow, this is fine. */
6886 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6887 break;
6888 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6889 != 0)
6890 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6891 break;
6893 case MIN_EXPR: case MAX_EXPR:
6894 /* If widening the type changes the signedness, then we can't perform
6895 this optimization as that changes the result. */
6896 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6897 break;
6899 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6900 sub_strict_overflow_p = false;
6901 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6902 &sub_strict_overflow_p)) != 0
6903 && (t2 = extract_muldiv (op1, c, code, wide_type,
6904 &sub_strict_overflow_p)) != 0)
6906 if (tree_int_cst_sgn (c) < 0)
6907 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6908 if (sub_strict_overflow_p)
6909 *strict_overflow_p = true;
6910 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6911 fold_convert (ctype, t2));
6913 break;
6915 case LSHIFT_EXPR: case RSHIFT_EXPR:
6916 /* If the second operand is constant, this is a multiplication
6917 or floor division, by a power of two, so we can treat it that
6918 way unless the multiplier or divisor overflows. Signed
6919 left-shift overflow is implementation-defined rather than
6920 undefined in C90, so do not convert signed left shift into
6921 multiplication. */
6922 if (TREE_CODE (op1) == INTEGER_CST
6923 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6924 /* const_binop may not detect overflow correctly,
6925 so check for it explicitly here. */
6926 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6927 wi::to_wide (op1))
6928 && (t1 = fold_convert (ctype,
6929 const_binop (LSHIFT_EXPR, size_one_node,
6930 op1))) != 0
6931 && !TREE_OVERFLOW (t1))
6932 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6933 ? MULT_EXPR : FLOOR_DIV_EXPR,
6934 ctype,
6935 fold_convert (ctype, op0),
6936 t1),
6937 c, code, wide_type, strict_overflow_p);
6938 break;
6940 case PLUS_EXPR: case MINUS_EXPR:
6941 /* See if we can eliminate the operation on both sides. If we can, we
6942 can return a new PLUS or MINUS. If we can't, the only remaining
6943 cases where we can do anything are if the second operand is a
6944 constant. */
6945 sub_strict_overflow_p = false;
6946 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6947 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6948 if (t1 != 0 && t2 != 0
6949 && TYPE_OVERFLOW_WRAPS (ctype)
6950 && (code == MULT_EXPR
6951 /* If not multiplication, we can only do this if both operands
6952 are divisible by c. */
6953 || (multiple_of_p (ctype, op0, c)
6954 && multiple_of_p (ctype, op1, c))))
6956 if (sub_strict_overflow_p)
6957 *strict_overflow_p = true;
6958 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6959 fold_convert (ctype, t2));
6962 /* If this was a subtraction, negate OP1 and set it to be an addition.
6963 This simplifies the logic below. */
6964 if (tcode == MINUS_EXPR)
6966 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6967 /* If OP1 was not easily negatable, the constant may be OP0. */
6968 if (TREE_CODE (op0) == INTEGER_CST)
6970 std::swap (op0, op1);
6971 std::swap (t1, t2);
6975 if (TREE_CODE (op1) != INTEGER_CST)
6976 break;
6978 /* If either OP1 or C are negative, this optimization is not safe for
6979 some of the division and remainder types while for others we need
6980 to change the code. */
6981 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6983 if (code == CEIL_DIV_EXPR)
6984 code = FLOOR_DIV_EXPR;
6985 else if (code == FLOOR_DIV_EXPR)
6986 code = CEIL_DIV_EXPR;
6987 else if (code != MULT_EXPR
6988 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6989 break;
6992 /* If it's a multiply or a division/modulus operation of a multiple
6993 of our constant, do the operation and verify it doesn't overflow. */
6994 if (code == MULT_EXPR
6995 || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6996 TYPE_SIGN (type)))
6998 op1 = const_binop (code, fold_convert (ctype, op1),
6999 fold_convert (ctype, c));
7000 /* We allow the constant to overflow with wrapping semantics. */
7001 if (op1 == 0
7002 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
7003 break;
7005 else
7006 break;
7008 /* If we have an unsigned type, we cannot widen the operation since it
7009 will change the result if the original computation overflowed. */
7010 if (TYPE_UNSIGNED (ctype) && ctype != type)
7011 break;
7013 /* The last case is if we are a multiply. In that case, we can
7014 apply the distributive law to commute the multiply and addition
7015 if the multiplication of the constants doesn't overflow
7016 and overflow is defined. With undefined overflow
7017 op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
7018 But fold_plusminus_mult_expr would factor back any power-of-two
7019 value so do not distribute in the first place in this case. */
7020 if (code == MULT_EXPR
7021 && TYPE_OVERFLOW_WRAPS (ctype)
7022 && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
7023 return fold_build2 (tcode, ctype,
7024 fold_build2 (code, ctype,
7025 fold_convert (ctype, op0),
7026 fold_convert (ctype, c)),
7027 op1);
7029 break;
7031 case MULT_EXPR:
7032 /* We have a special case here if we are doing something like
7033 (C * 8) % 4 since we know that's zero. */
7034 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
7035 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
7036 /* If the multiplication can overflow we cannot optimize this. */
7037 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
7038 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
7039 && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
7040 TYPE_SIGN (type)))
7042 *strict_overflow_p = true;
7043 return omit_one_operand (type, integer_zero_node, op0);
7046 /* ... fall through ... */
7048 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
7049 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
7050 /* If we can extract our operation from the LHS, do so and return a
7051 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
7052 do something only if the second operand is a constant. */
7053 if (same_p
7054 && TYPE_OVERFLOW_WRAPS (ctype)
7055 && (t1 = extract_muldiv (op0, c, code, wide_type,
7056 strict_overflow_p)) != 0)
7057 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
7058 fold_convert (ctype, op1));
7059 else if (tcode == MULT_EXPR && code == MULT_EXPR
7060 && TYPE_OVERFLOW_WRAPS (ctype)
7061 && (t1 = extract_muldiv (op1, c, code, wide_type,
7062 strict_overflow_p)) != 0)
7063 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
7064 fold_convert (ctype, t1));
7065 else if (TREE_CODE (op1) != INTEGER_CST)
7066 return 0;
7068 /* If these are the same operation types, we can associate them
7069 assuming no overflow. */
7070 if (tcode == code)
7072 bool overflow_p = false;
7073 wi::overflow_type overflow_mul;
7074 signop sign = TYPE_SIGN (ctype);
7075 unsigned prec = TYPE_PRECISION (ctype);
7076 wide_int mul = wi::mul (wi::to_wide (op1, prec),
7077 wi::to_wide (c, prec),
7078 sign, &overflow_mul);
7079 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
7080 if (overflow_mul
7081 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
7082 overflow_p = true;
7083 if (!overflow_p)
7084 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
7085 wide_int_to_tree (ctype, mul));
7088 /* If these operations "cancel" each other, we have the main
7089 optimizations of this pass, which occur when either constant is a
7090 multiple of the other, in which case we replace this with either an
7091 operation or CODE or TCODE.
7093 If we have an unsigned type, we cannot do this since it will change
7094 the result if the original computation overflowed. */
7095 if (TYPE_OVERFLOW_UNDEFINED (ctype)
7096 && !TYPE_OVERFLOW_SANITIZED (ctype)
7097 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
7098 || (tcode == MULT_EXPR
7099 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
7100 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
7101 && code != MULT_EXPR)))
7103 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
7104 TYPE_SIGN (type)))
7106 *strict_overflow_p = true;
7107 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
7108 fold_convert (ctype,
7109 const_binop (TRUNC_DIV_EXPR,
7110 op1, c)));
7112 else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
7113 TYPE_SIGN (type)))
7115 *strict_overflow_p = true;
7116 return fold_build2 (code, ctype, fold_convert (ctype, op0),
7117 fold_convert (ctype,
7118 const_binop (TRUNC_DIV_EXPR,
7119 c, op1)));
7122 break;
7124 default:
7125 break;
7128 return 0;
7131 /* Return a node which has the indicated constant VALUE (either 0 or
7132 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
7133 and is of the indicated TYPE. */
7135 tree
7136 constant_boolean_node (bool value, tree type)
7138 if (type == integer_type_node)
7139 return value ? integer_one_node : integer_zero_node;
7140 else if (type == boolean_type_node)
7141 return value ? boolean_true_node : boolean_false_node;
7142 else if (TREE_CODE (type) == VECTOR_TYPE)
7143 return build_vector_from_val (type,
7144 build_int_cst (TREE_TYPE (type),
7145 value ? -1 : 0));
7146 else
7147 return fold_convert (type, value ? integer_one_node : integer_zero_node);
7151 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
7152 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
7153 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
7154 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
7155 COND is the first argument to CODE; otherwise (as in the example
7156 given here), it is the second argument. TYPE is the type of the
7157 original expression. Return NULL_TREE if no simplification is
7158 possible. */
7160 static tree
7161 fold_binary_op_with_conditional_arg (location_t loc,
7162 enum tree_code code,
7163 tree type, tree op0, tree op1,
7164 tree cond, tree arg, int cond_first_p)
7166 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
7167 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
7168 tree test, true_value, false_value;
7169 tree lhs = NULL_TREE;
7170 tree rhs = NULL_TREE;
7171 enum tree_code cond_code = COND_EXPR;
7173 /* Do not move possibly trapping operations into the conditional as this
7174 pessimizes code and causes gimplification issues when applied late. */
7175 if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
7176 ANY_INTEGRAL_TYPE_P (type)
7177 && TYPE_OVERFLOW_TRAPS (type), op1))
7178 return NULL_TREE;
7180 if (TREE_CODE (cond) == COND_EXPR
7181 || TREE_CODE (cond) == VEC_COND_EXPR)
7183 test = TREE_OPERAND (cond, 0);
7184 true_value = TREE_OPERAND (cond, 1);
7185 false_value = TREE_OPERAND (cond, 2);
7186 /* If this operand throws an expression, then it does not make
7187 sense to try to perform a logical or arithmetic operation
7188 involving it. */
7189 if (VOID_TYPE_P (TREE_TYPE (true_value)))
7190 lhs = true_value;
7191 if (VOID_TYPE_P (TREE_TYPE (false_value)))
7192 rhs = false_value;
7194 else if (!(TREE_CODE (type) != VECTOR_TYPE
7195 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
7197 tree testtype = TREE_TYPE (cond);
7198 test = cond;
7199 true_value = constant_boolean_node (true, testtype);
7200 false_value = constant_boolean_node (false, testtype);
7202 else
7203 /* Detect the case of mixing vector and scalar types - bail out. */
7204 return NULL_TREE;
7206 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
7207 cond_code = VEC_COND_EXPR;
7209 /* This transformation is only worthwhile if we don't have to wrap ARG
7210 in a SAVE_EXPR and the operation can be simplified without recursing
7211 on at least one of the branches once its pushed inside the COND_EXPR. */
7212 if (!TREE_CONSTANT (arg)
7213 && (TREE_SIDE_EFFECTS (arg)
7214 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
7215 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
7216 return NULL_TREE;
7218 arg = fold_convert_loc (loc, arg_type, arg);
7219 if (lhs == 0)
7221 true_value = fold_convert_loc (loc, cond_type, true_value);
7222 if (cond_first_p)
7223 lhs = fold_build2_loc (loc, code, type, true_value, arg);
7224 else
7225 lhs = fold_build2_loc (loc, code, type, arg, true_value);
7227 if (rhs == 0)
7229 false_value = fold_convert_loc (loc, cond_type, false_value);
7230 if (cond_first_p)
7231 rhs = fold_build2_loc (loc, code, type, false_value, arg);
7232 else
7233 rhs = fold_build2_loc (loc, code, type, arg, false_value);
7236 /* Check that we have simplified at least one of the branches. */
7237 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
7238 return NULL_TREE;
7240 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
7244 /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7246 If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7247 type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7248 if ARG - ZERO_ARG is the same as X.
7250 If ARG is NULL, check for any value of type TYPE.
7252 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7253 and finite. The problematic cases are when X is zero, and its mode
7254 has signed zeros. In the case of rounding towards -infinity,
7255 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7256 modes, X + 0 is not the same as X because -0 + 0 is 0. */
7258 bool
7259 fold_real_zero_addition_p (const_tree type, const_tree arg,
7260 const_tree zero_arg, int negate)
7262 if (!real_zerop (zero_arg))
7263 return false;
7265 /* Don't allow the fold with -fsignaling-nans. */
7266 if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type))
7267 return false;
7269 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7270 if (!HONOR_SIGNED_ZEROS (type))
7271 return true;
7273 /* There is no case that is safe for all rounding modes. */
7274 if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
7275 return false;
7277 /* In a vector or complex, we would need to check the sign of all zeros. */
7278 if (TREE_CODE (zero_arg) == VECTOR_CST)
7279 zero_arg = uniform_vector_p (zero_arg);
7280 if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
7281 return false;
7283 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7284 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7285 negate = !negate;
7287 /* The mode has signed zeros, and we have to honor their sign.
7288 In this situation, there are only two cases we can return true for.
7289 (i) X - 0 is the same as X with default rounding.
7290 (ii) X + 0 is X when X can't possibly be -0.0. */
7291 return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
7294 /* Subroutine of match.pd that optimizes comparisons of a division by
7295 a nonzero integer constant against an integer constant, i.e.
7296 X/C1 op C2.
7298 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7299 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7301 enum tree_code
7302 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7303 tree *hi, bool *neg_overflow)
7305 tree prod, tmp, type = TREE_TYPE (c1);
7306 signop sign = TYPE_SIGN (type);
7307 wi::overflow_type overflow;
7309 /* We have to do this the hard way to detect unsigned overflow.
7310 prod = int_const_binop (MULT_EXPR, c1, c2); */
7311 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7312 prod = force_fit_type (type, val, -1, overflow);
7313 *neg_overflow = false;
7315 if (sign == UNSIGNED)
7317 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7318 *lo = prod;
7320 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7321 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7322 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7324 else if (tree_int_cst_sgn (c1) >= 0)
7326 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7327 switch (tree_int_cst_sgn (c2))
7329 case -1:
7330 *neg_overflow = true;
7331 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7332 *hi = prod;
7333 break;
7335 case 0:
7336 *lo = fold_negate_const (tmp, type);
7337 *hi = tmp;
7338 break;
7340 case 1:
7341 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7342 *lo = prod;
7343 break;
7345 default:
7346 gcc_unreachable ();
7349 else
7351 /* A negative divisor reverses the relational operators. */
7352 code = swap_tree_comparison (code);
7354 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
7355 switch (tree_int_cst_sgn (c2))
7357 case -1:
7358 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
7359 *lo = prod;
7360 break;
7362 case 0:
7363 *hi = fold_negate_const (tmp, type);
7364 *lo = tmp;
7365 break;
7367 case 1:
7368 *neg_overflow = true;
7369 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
7370 *hi = prod;
7371 break;
7373 default:
7374 gcc_unreachable ();
7378 if (code != EQ_EXPR && code != NE_EXPR)
7379 return code;
7381 if (TREE_OVERFLOW (*lo)
7382 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7383 *lo = NULL_TREE;
7384 if (TREE_OVERFLOW (*hi)
7385 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7386 *hi = NULL_TREE;
7388 return code;
7392 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7393 equality/inequality test, then return a simplified form of the test
7394 using a sign testing. Otherwise return NULL. TYPE is the desired
7395 result type. */
7397 static tree
7398 fold_single_bit_test_into_sign_test (location_t loc,
7399 enum tree_code code, tree arg0, tree arg1,
7400 tree result_type)
7402 /* If this is testing a single bit, we can optimize the test. */
7403 if ((code == NE_EXPR || code == EQ_EXPR)
7404 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
7405 && integer_pow2p (TREE_OPERAND (arg0, 1)))
7407 /* If we have (A & C) != 0 where C is the sign bit of A, convert
7408 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
7409 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
7411 if (arg00 != NULL_TREE
7412 /* This is only a win if casting to a signed type is cheap,
7413 i.e. when arg00's type is not a partial mode. */
7414 && type_has_mode_precision_p (TREE_TYPE (arg00)))
7416 tree stype = signed_type_for (TREE_TYPE (arg00));
7417 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
7418 result_type,
7419 fold_convert_loc (loc, stype, arg00),
7420 build_int_cst (stype, 0));
7424 return NULL_TREE;
7427 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7428 equality/inequality test, then return a simplified form of
7429 the test using shifts and logical operations. Otherwise return
7430 NULL. TYPE is the desired result type. */
7432 tree
7433 fold_single_bit_test (location_t loc, enum tree_code code,
7434 tree arg0, tree arg1, tree result_type)
7436 /* If this is testing a single bit, we can optimize the test. */
7437 if ((code == NE_EXPR || code == EQ_EXPR)
7438 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
7439 && integer_pow2p (TREE_OPERAND (arg0, 1)))
7441 tree inner = TREE_OPERAND (arg0, 0);
7442 tree type = TREE_TYPE (arg0);
7443 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
7444 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
7445 int ops_unsigned;
7446 tree signed_type, unsigned_type, intermediate_type;
7447 tree tem, one;
7449 /* First, see if we can fold the single bit test into a sign-bit
7450 test. */
7451 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
7452 result_type);
7453 if (tem)
7454 return tem;
7456 /* Otherwise we have (A & C) != 0 where C is a single bit,
7457 convert that into ((A >> C2) & 1). Where C2 = log2(C).
7458 Similarly for (A & C) == 0. */
7460 /* If INNER is a right shift of a constant and it plus BITNUM does
7461 not overflow, adjust BITNUM and INNER. */
7462 if (TREE_CODE (inner) == RSHIFT_EXPR
7463 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
7464 && bitnum < TYPE_PRECISION (type)
7465 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner, 1)),
7466 TYPE_PRECISION (type) - bitnum))
7468 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
7469 inner = TREE_OPERAND (inner, 0);
7472 /* If we are going to be able to omit the AND below, we must do our
7473 operations as unsigned. If we must use the AND, we have a choice.
7474 Normally unsigned is faster, but for some machines signed is. */
7475 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
7476 && !flag_syntax_only) ? 0 : 1;
7478 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
7479 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
7480 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
7481 inner = fold_convert_loc (loc, intermediate_type, inner);
7483 if (bitnum != 0)
7484 inner = build2 (RSHIFT_EXPR, intermediate_type,
7485 inner, size_int (bitnum));
7487 one = build_int_cst (intermediate_type, 1);
7489 if (code == EQ_EXPR)
7490 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
7492 /* Put the AND last so it can combine with more things. */
7493 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
7495 /* Make sure to return the proper type. */
7496 inner = fold_convert_loc (loc, result_type, inner);
7498 return inner;
7500 return NULL_TREE;
7503 /* Test whether it is preferable to swap two operands, ARG0 and
7504 ARG1, for example because ARG0 is an integer constant and ARG1
7505 isn't. */
7507 bool
7508 tree_swap_operands_p (const_tree arg0, const_tree arg1)
7510 if (CONSTANT_CLASS_P (arg1))
7511 return 0;
7512 if (CONSTANT_CLASS_P (arg0))
7513 return 1;
7515 STRIP_NOPS (arg0);
7516 STRIP_NOPS (arg1);
7518 if (TREE_CONSTANT (arg1))
7519 return 0;
7520 if (TREE_CONSTANT (arg0))
7521 return 1;
7523 /* It is preferable to swap two SSA_NAME to ensure a canonical form
7524 for commutative and comparison operators. Ensuring a canonical
7525 form allows the optimizers to find additional redundancies without
7526 having to explicitly check for both orderings. */
7527 if (TREE_CODE (arg0) == SSA_NAME
7528 && TREE_CODE (arg1) == SSA_NAME
7529 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7530 return 1;
7532 /* Put SSA_NAMEs last. */
7533 if (TREE_CODE (arg1) == SSA_NAME)
7534 return 0;
7535 if (TREE_CODE (arg0) == SSA_NAME)
7536 return 1;
7538 /* Put variables last. */
7539 if (DECL_P (arg1))
7540 return 0;
7541 if (DECL_P (arg0))
7542 return 1;
7544 return 0;
7548 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7549 means A >= Y && A != MAX, but in this case we know that
7550 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7552 static tree
7553 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7555 tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7557 if (TREE_CODE (bound) == LT_EXPR)
7558 a = TREE_OPERAND (bound, 0);
7559 else if (TREE_CODE (bound) == GT_EXPR)
7560 a = TREE_OPERAND (bound, 1);
7561 else
7562 return NULL_TREE;
7564 typea = TREE_TYPE (a);
7565 if (!INTEGRAL_TYPE_P (typea)
7566 && !POINTER_TYPE_P (typea))
7567 return NULL_TREE;
7569 if (TREE_CODE (ineq) == LT_EXPR)
7571 a1 = TREE_OPERAND (ineq, 1);
7572 y = TREE_OPERAND (ineq, 0);
7574 else if (TREE_CODE (ineq) == GT_EXPR)
7576 a1 = TREE_OPERAND (ineq, 0);
7577 y = TREE_OPERAND (ineq, 1);
7579 else
7580 return NULL_TREE;
7582 if (TREE_TYPE (a1) != typea)
7583 return NULL_TREE;
7585 if (POINTER_TYPE_P (typea))
7587 /* Convert the pointer types into integer before taking the difference. */
7588 tree ta = fold_convert_loc (loc, ssizetype, a);
7589 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7590 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7592 else
7593 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7595 if (!diff || !integer_onep (diff))
7596 return NULL_TREE;
7598 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7601 /* Fold a sum or difference of at least one multiplication.
7602 Returns the folded tree or NULL if no simplification could be made. */
7604 static tree
7605 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7606 tree arg0, tree arg1)
7608 tree arg00, arg01, arg10, arg11;
7609 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7611 /* (A * C) +- (B * C) -> (A+-B) * C.
7612 (A * C) +- A -> A * (C+-1).
7613 We are most concerned about the case where C is a constant,
7614 but other combinations show up during loop reduction. Since
7615 it is not difficult, try all four possibilities. */
7617 if (TREE_CODE (arg0) == MULT_EXPR)
7619 arg00 = TREE_OPERAND (arg0, 0);
7620 arg01 = TREE_OPERAND (arg0, 1);
7622 else if (TREE_CODE (arg0) == INTEGER_CST)
7624 arg00 = build_one_cst (type);
7625 arg01 = arg0;
7627 else
7629 /* We cannot generate constant 1 for fract. */
7630 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7631 return NULL_TREE;
7632 arg00 = arg0;
7633 arg01 = build_one_cst (type);
7635 if (TREE_CODE (arg1) == MULT_EXPR)
7637 arg10 = TREE_OPERAND (arg1, 0);
7638 arg11 = TREE_OPERAND (arg1, 1);
7640 else if (TREE_CODE (arg1) == INTEGER_CST)
7642 arg10 = build_one_cst (type);
7643 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7644 the purpose of this canonicalization. */
7645 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7646 && negate_expr_p (arg1)
7647 && code == PLUS_EXPR)
7649 arg11 = negate_expr (arg1);
7650 code = MINUS_EXPR;
7652 else
7653 arg11 = arg1;
7655 else
7657 /* We cannot generate constant 1 for fract. */
7658 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7659 return NULL_TREE;
7660 arg10 = arg1;
7661 arg11 = build_one_cst (type);
7663 same = NULL_TREE;
7665 /* Prefer factoring a common non-constant. */
7666 if (operand_equal_p (arg00, arg10, 0))
7667 same = arg00, alt0 = arg01, alt1 = arg11;
7668 else if (operand_equal_p (arg01, arg11, 0))
7669 same = arg01, alt0 = arg00, alt1 = arg10;
7670 else if (operand_equal_p (arg00, arg11, 0))
7671 same = arg00, alt0 = arg01, alt1 = arg10;
7672 else if (operand_equal_p (arg01, arg10, 0))
7673 same = arg01, alt0 = arg00, alt1 = arg11;
7675 /* No identical multiplicands; see if we can find a common
7676 power-of-two factor in non-power-of-two multiplies. This
7677 can help in multi-dimensional array access. */
7678 else if (tree_fits_shwi_p (arg01) && tree_fits_shwi_p (arg11))
7680 HOST_WIDE_INT int01 = tree_to_shwi (arg01);
7681 HOST_WIDE_INT int11 = tree_to_shwi (arg11);
7682 HOST_WIDE_INT tmp;
7683 bool swap = false;
7684 tree maybe_same;
7686 /* Move min of absolute values to int11. */
7687 if (absu_hwi (int01) < absu_hwi (int11))
7689 tmp = int01, int01 = int11, int11 = tmp;
7690 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7691 maybe_same = arg01;
7692 swap = true;
7694 else
7695 maybe_same = arg11;
7697 const unsigned HOST_WIDE_INT factor = absu_hwi (int11);
7698 if (factor > 1
7699 && pow2p_hwi (factor)
7700 && (int01 & (factor - 1)) == 0
7701 /* The remainder should not be a constant, otherwise we
7702 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7703 increased the number of multiplications necessary. */
7704 && TREE_CODE (arg10) != INTEGER_CST)
7706 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7707 build_int_cst (TREE_TYPE (arg00),
7708 int01 / int11));
7709 alt1 = arg10;
7710 same = maybe_same;
7711 if (swap)
7712 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7716 if (!same)
7717 return NULL_TREE;
7719 if (! ANY_INTEGRAL_TYPE_P (type)
7720 || TYPE_OVERFLOW_WRAPS (type)
7721 /* We are neither factoring zero nor minus one. */
7722 || TREE_CODE (same) == INTEGER_CST)
7723 return fold_build2_loc (loc, MULT_EXPR, type,
7724 fold_build2_loc (loc, code, type,
7725 fold_convert_loc (loc, type, alt0),
7726 fold_convert_loc (loc, type, alt1)),
7727 fold_convert_loc (loc, type, same));
7729 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7730 same may be minus one and thus the multiplication may overflow. Perform
7731 the sum operation in an unsigned type. */
7732 tree utype = unsigned_type_for (type);
7733 tree tem = fold_build2_loc (loc, code, utype,
7734 fold_convert_loc (loc, utype, alt0),
7735 fold_convert_loc (loc, utype, alt1));
7736 /* If the sum evaluated to a constant that is not -INF the multiplication
7737 cannot overflow. */
7738 if (TREE_CODE (tem) == INTEGER_CST
7739 && (wi::to_wide (tem)
7740 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7741 return fold_build2_loc (loc, MULT_EXPR, type,
7742 fold_convert (type, tem), same);
7744 /* Do not resort to unsigned multiplication because
7745 we lose the no-overflow property of the expression. */
7746 return NULL_TREE;
7749 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7750 specified by EXPR into the buffer PTR of length LEN bytes.
7751 Return the number of bytes placed in the buffer, or zero
7752 upon failure. */
7754 static int
7755 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7757 tree type = TREE_TYPE (expr);
7758 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7759 int byte, offset, word, words;
7760 unsigned char value;
7762 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7763 return 0;
7764 if (off == -1)
7765 off = 0;
7767 if (ptr == NULL)
7768 /* Dry run. */
7769 return MIN (len, total_bytes - off);
7771 words = total_bytes / UNITS_PER_WORD;
7773 for (byte = 0; byte < total_bytes; byte++)
7775 int bitpos = byte * BITS_PER_UNIT;
7776 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7777 number of bytes. */
7778 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7780 if (total_bytes > UNITS_PER_WORD)
7782 word = byte / UNITS_PER_WORD;
7783 if (WORDS_BIG_ENDIAN)
7784 word = (words - 1) - word;
7785 offset = word * UNITS_PER_WORD;
7786 if (BYTES_BIG_ENDIAN)
7787 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7788 else
7789 offset += byte % UNITS_PER_WORD;
7791 else
7792 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7793 if (offset >= off && offset - off < len)
7794 ptr[offset - off] = value;
7796 return MIN (len, total_bytes - off);
7800 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7801 specified by EXPR into the buffer PTR of length LEN bytes.
7802 Return the number of bytes placed in the buffer, or zero
7803 upon failure. */
7805 static int
7806 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7808 tree type = TREE_TYPE (expr);
7809 scalar_mode mode = SCALAR_TYPE_MODE (type);
7810 int total_bytes = GET_MODE_SIZE (mode);
7811 FIXED_VALUE_TYPE value;
7812 tree i_value, i_type;
7814 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7815 return 0;
7817 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7819 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7820 return 0;
7822 value = TREE_FIXED_CST (expr);
7823 i_value = double_int_to_tree (i_type, value.data);
7825 return native_encode_int (i_value, ptr, len, off);
7829 /* Subroutine of native_encode_expr. Encode the REAL_CST
7830 specified by EXPR into the buffer PTR of length LEN bytes.
7831 Return the number of bytes placed in the buffer, or zero
7832 upon failure. */
7834 static int
7835 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7837 tree type = TREE_TYPE (expr);
7838 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7839 int byte, offset, word, words, bitpos;
7840 unsigned char value;
7842 /* There are always 32 bits in each long, no matter the size of
7843 the hosts long. We handle floating point representations with
7844 up to 192 bits. */
7845 long tmp[6];
7847 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7848 return 0;
7849 if (off == -1)
7850 off = 0;
7852 if (ptr == NULL)
7853 /* Dry run. */
7854 return MIN (len, total_bytes - off);
7856 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7858 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7860 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7861 bitpos += BITS_PER_UNIT)
7863 byte = (bitpos / BITS_PER_UNIT) & 3;
7864 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7866 if (UNITS_PER_WORD < 4)
7868 word = byte / UNITS_PER_WORD;
7869 if (WORDS_BIG_ENDIAN)
7870 word = (words - 1) - word;
7871 offset = word * UNITS_PER_WORD;
7872 if (BYTES_BIG_ENDIAN)
7873 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7874 else
7875 offset += byte % UNITS_PER_WORD;
7877 else
7879 offset = byte;
7880 if (BYTES_BIG_ENDIAN)
7882 /* Reverse bytes within each long, or within the entire float
7883 if it's smaller than a long (for HFmode). */
7884 offset = MIN (3, total_bytes - 1) - offset;
7885 gcc_assert (offset >= 0);
7888 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7889 if (offset >= off
7890 && offset - off < len)
7891 ptr[offset - off] = value;
7893 return MIN (len, total_bytes - off);
7896 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7897 specified by EXPR into the buffer PTR of length LEN bytes.
7898 Return the number of bytes placed in the buffer, or zero
7899 upon failure. */
7901 static int
7902 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7904 int rsize, isize;
7905 tree part;
7907 part = TREE_REALPART (expr);
7908 rsize = native_encode_expr (part, ptr, len, off);
7909 if (off == -1 && rsize == 0)
7910 return 0;
7911 part = TREE_IMAGPART (expr);
7912 if (off != -1)
7913 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7914 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7915 len - rsize, off);
7916 if (off == -1 && isize != rsize)
7917 return 0;
7918 return rsize + isize;
7921 /* Like native_encode_vector, but only encode the first COUNT elements.
7922 The other arguments are as for native_encode_vector. */
7924 static int
7925 native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7926 int off, unsigned HOST_WIDE_INT count)
7928 tree itype = TREE_TYPE (TREE_TYPE (expr));
7929 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7930 && TYPE_PRECISION (itype) <= BITS_PER_UNIT)
7932 /* This is the only case in which elements can be smaller than a byte.
7933 Element 0 is always in the lsb of the containing byte. */
7934 unsigned int elt_bits = TYPE_PRECISION (itype);
7935 int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7936 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7937 return 0;
7939 if (off == -1)
7940 off = 0;
7942 /* Zero the buffer and then set bits later where necessary. */
7943 int extract_bytes = MIN (len, total_bytes - off);
7944 if (ptr)
7945 memset (ptr, 0, extract_bytes);
7947 unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7948 unsigned int first_elt = off * elts_per_byte;
7949 unsigned int extract_elts = extract_bytes * elts_per_byte;
7950 for (unsigned int i = 0; i < extract_elts; ++i)
7952 tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7953 if (TREE_CODE (elt) != INTEGER_CST)
7954 return 0;
7956 if (ptr && wi::extract_uhwi (wi::to_wide (elt), 0, 1))
7958 unsigned int bit = i * elt_bits;
7959 ptr[bit / BITS_PER_UNIT] |= 1 << (bit % BITS_PER_UNIT);
7962 return extract_bytes;
7965 int offset = 0;
7966 int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7967 for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7969 if (off >= size)
7971 off -= size;
7972 continue;
7974 tree elem = VECTOR_CST_ELT (expr, i);
7975 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7976 len - offset, off);
7977 if ((off == -1 && res != size) || res == 0)
7978 return 0;
7979 offset += res;
7980 if (offset >= len)
7981 return (off == -1 && i < count - 1) ? 0 : offset;
7982 if (off != -1)
7983 off = 0;
7985 return offset;
7988 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7989 specified by EXPR into the buffer PTR of length LEN bytes.
7990 Return the number of bytes placed in the buffer, or zero
7991 upon failure. */
7993 static int
7994 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7996 unsigned HOST_WIDE_INT count;
7997 if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7998 return 0;
7999 return native_encode_vector_part (expr, ptr, len, off, count);
8003 /* Subroutine of native_encode_expr. Encode the STRING_CST
8004 specified by EXPR into the buffer PTR of length LEN bytes.
8005 Return the number of bytes placed in the buffer, or zero
8006 upon failure. */
8008 static int
8009 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
8011 tree type = TREE_TYPE (expr);
8013 /* Wide-char strings are encoded in target byte-order so native
8014 encoding them is trivial. */
8015 if (BITS_PER_UNIT != CHAR_BIT
8016 || TREE_CODE (type) != ARRAY_TYPE
8017 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
8018 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
8019 return 0;
8021 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
8022 if ((off == -1 && total_bytes > len) || off >= total_bytes)
8023 return 0;
8024 if (off == -1)
8025 off = 0;
8026 len = MIN (total_bytes - off, len);
8027 if (ptr == NULL)
8028 /* Dry run. */;
8029 else
8031 int written = 0;
8032 if (off < TREE_STRING_LENGTH (expr))
8034 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
8035 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
8037 memset (ptr + written, 0, len - written);
8039 return len;
8043 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
8044 FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
8045 the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
8046 anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
8047 sufficient to encode the entire EXPR, or if OFF is out of bounds.
8048 Otherwise, start at byte offset OFF and encode at most LEN bytes.
8049 Return the number of bytes placed in the buffer, or zero upon failure. */
8052 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
8054 /* We don't support starting at negative offset and -1 is special. */
8055 if (off < -1)
8056 return 0;
8058 switch (TREE_CODE (expr))
8060 case INTEGER_CST:
8061 return native_encode_int (expr, ptr, len, off);
8063 case REAL_CST:
8064 return native_encode_real (expr, ptr, len, off);
8066 case FIXED_CST:
8067 return native_encode_fixed (expr, ptr, len, off);
8069 case COMPLEX_CST:
8070 return native_encode_complex (expr, ptr, len, off);
8072 case VECTOR_CST:
8073 return native_encode_vector (expr, ptr, len, off);
8075 case STRING_CST:
8076 return native_encode_string (expr, ptr, len, off);
8078 default:
8079 return 0;
8083 /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
8084 or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
8085 of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
8086 machine modes, we can't just use build_nonstandard_integer_type. */
8088 tree
8089 find_bitfield_repr_type (int fieldsize, int len)
8091 machine_mode mode;
8092 for (int pass = 0; pass < 2; pass++)
8094 enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT;
8095 FOR_EACH_MODE_IN_CLASS (mode, mclass)
8096 if (known_ge (GET_MODE_SIZE (mode), fieldsize)
8097 && known_eq (GET_MODE_PRECISION (mode),
8098 GET_MODE_BITSIZE (mode))
8099 && known_le (GET_MODE_SIZE (mode), len))
8101 tree ret = lang_hooks.types.type_for_mode (mode, 1);
8102 if (ret && TYPE_MODE (ret) == mode)
8103 return ret;
8107 for (int i = 0; i < NUM_INT_N_ENTS; i ++)
8108 if (int_n_enabled_p[i]
8109 && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize)
8110 && int_n_trees[i].unsigned_type)
8112 tree ret = int_n_trees[i].unsigned_type;
8113 mode = TYPE_MODE (ret);
8114 if (known_ge (GET_MODE_SIZE (mode), fieldsize)
8115 && known_eq (GET_MODE_PRECISION (mode),
8116 GET_MODE_BITSIZE (mode))
8117 && known_le (GET_MODE_SIZE (mode), len))
8118 return ret;
8121 return NULL_TREE;
8124 /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
8125 NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
8126 to be non-NULL and OFF zero), then in addition to filling the
8127 bytes pointed by PTR with the value also clear any bits pointed
8128 by MASK that are known to be initialized, keep them as is for
8129 e.g. uninitialized padding bits or uninitialized fields. */
8132 native_encode_initializer (tree init, unsigned char *ptr, int len,
8133 int off, unsigned char *mask)
8135 int r;
8137 /* We don't support starting at negative offset and -1 is special. */
8138 if (off < -1 || init == NULL_TREE)
8139 return 0;
8141 gcc_assert (mask == NULL || (off == 0 && ptr));
8143 STRIP_NOPS (init);
8144 switch (TREE_CODE (init))
8146 case VIEW_CONVERT_EXPR:
8147 case NON_LVALUE_EXPR:
8148 return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off,
8149 mask);
8150 default:
8151 r = native_encode_expr (init, ptr, len, off);
8152 if (mask)
8153 memset (mask, 0, r);
8154 return r;
8155 case CONSTRUCTOR:
8156 tree type = TREE_TYPE (init);
8157 HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
8158 if (total_bytes < 0)
8159 return 0;
8160 if ((off == -1 && total_bytes > len) || off >= total_bytes)
8161 return 0;
8162 int o = off == -1 ? 0 : off;
8163 if (TREE_CODE (type) == ARRAY_TYPE)
8165 tree min_index;
8166 unsigned HOST_WIDE_INT cnt;
8167 HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
8168 constructor_elt *ce;
8170 if (!TYPE_DOMAIN (type)
8171 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
8172 return 0;
8174 fieldsize = int_size_in_bytes (TREE_TYPE (type));
8175 if (fieldsize <= 0)
8176 return 0;
8178 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8179 if (ptr)
8180 memset (ptr, '\0', MIN (total_bytes - off, len));
8182 for (cnt = 0; ; cnt++)
8184 tree val = NULL_TREE, index = NULL_TREE;
8185 HOST_WIDE_INT pos = curpos, count = 0;
8186 bool full = false;
8187 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8189 val = ce->value;
8190 index = ce->index;
8192 else if (mask == NULL
8193 || CONSTRUCTOR_NO_CLEARING (init)
8194 || curpos >= total_bytes)
8195 break;
8196 else
8197 pos = total_bytes;
8199 if (index && TREE_CODE (index) == RANGE_EXPR)
8201 if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST
8202 || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST)
8203 return 0;
8204 offset_int wpos
8205 = wi::sext (wi::to_offset (TREE_OPERAND (index, 0))
8206 - wi::to_offset (min_index),
8207 TYPE_PRECISION (sizetype));
8208 wpos *= fieldsize;
8209 if (!wi::fits_shwi_p (pos))
8210 return 0;
8211 pos = wpos.to_shwi ();
8212 offset_int wcount
8213 = wi::sext (wi::to_offset (TREE_OPERAND (index, 1))
8214 - wi::to_offset (TREE_OPERAND (index, 0)),
8215 TYPE_PRECISION (sizetype));
8216 if (!wi::fits_shwi_p (wcount))
8217 return 0;
8218 count = wcount.to_shwi ();
8220 else if (index)
8222 if (TREE_CODE (index) != INTEGER_CST)
8223 return 0;
8224 offset_int wpos
8225 = wi::sext (wi::to_offset (index)
8226 - wi::to_offset (min_index),
8227 TYPE_PRECISION (sizetype));
8228 wpos *= fieldsize;
8229 if (!wi::fits_shwi_p (wpos))
8230 return 0;
8231 pos = wpos.to_shwi ();
8234 if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos)
8236 if (valueinit == -1)
8238 tree zero = build_zero_cst (TREE_TYPE (type));
8239 r = native_encode_initializer (zero, ptr + curpos,
8240 fieldsize, 0,
8241 mask + curpos);
8242 if (TREE_CODE (zero) == CONSTRUCTOR)
8243 ggc_free (zero);
8244 if (!r)
8245 return 0;
8246 valueinit = curpos;
8247 curpos += fieldsize;
8249 while (curpos != pos)
8251 memcpy (ptr + curpos, ptr + valueinit, fieldsize);
8252 memcpy (mask + curpos, mask + valueinit, fieldsize);
8253 curpos += fieldsize;
8257 curpos = pos;
8258 if (val)
8261 if (off == -1
8262 || (curpos >= off
8263 && (curpos + fieldsize
8264 <= (HOST_WIDE_INT) off + len)))
8266 if (full)
8268 if (ptr)
8269 memcpy (ptr + (curpos - o), ptr + (pos - o),
8270 fieldsize);
8271 if (mask)
8272 memcpy (mask + curpos, mask + pos, fieldsize);
8274 else if (!native_encode_initializer (val,
8276 ? ptr + curpos - o
8277 : NULL,
8278 fieldsize,
8279 off == -1 ? -1
8280 : 0,
8281 mask
8282 ? mask + curpos
8283 : NULL))
8284 return 0;
8285 else
8287 full = true;
8288 pos = curpos;
8291 else if (curpos + fieldsize > off
8292 && curpos < (HOST_WIDE_INT) off + len)
8294 /* Partial overlap. */
8295 unsigned char *p = NULL;
8296 int no = 0;
8297 int l;
8298 gcc_assert (mask == NULL);
8299 if (curpos >= off)
8301 if (ptr)
8302 p = ptr + curpos - off;
8303 l = MIN ((HOST_WIDE_INT) off + len - curpos,
8304 fieldsize);
8306 else
8308 p = ptr;
8309 no = off - curpos;
8310 l = len;
8312 if (!native_encode_initializer (val, p, l, no, NULL))
8313 return 0;
8315 curpos += fieldsize;
8317 while (count-- != 0);
8319 return MIN (total_bytes - off, len);
8321 else if (TREE_CODE (type) == RECORD_TYPE
8322 || TREE_CODE (type) == UNION_TYPE)
8324 unsigned HOST_WIDE_INT cnt;
8325 constructor_elt *ce;
8326 tree fld_base = TYPE_FIELDS (type);
8327 tree to_free = NULL_TREE;
8329 gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8330 if (ptr != NULL)
8331 memset (ptr, '\0', MIN (total_bytes - o, len));
8332 for (cnt = 0; ; cnt++)
8334 tree val = NULL_TREE, field = NULL_TREE;
8335 HOST_WIDE_INT pos = 0, fieldsize;
8336 unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8338 if (to_free)
8340 ggc_free (to_free);
8341 to_free = NULL_TREE;
8344 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8346 val = ce->value;
8347 field = ce->index;
8348 if (field == NULL_TREE)
8349 return 0;
8351 pos = int_byte_position (field);
8352 if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8353 continue;
8355 else if (mask == NULL
8356 || CONSTRUCTOR_NO_CLEARING (init))
8357 break;
8358 else
8359 pos = total_bytes;
8361 if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8363 tree fld;
8364 for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8366 if (TREE_CODE (fld) != FIELD_DECL)
8367 continue;
8368 if (fld == field)
8369 break;
8370 if (DECL_PADDING_P (fld))
8371 continue;
8372 if (DECL_SIZE_UNIT (fld) == NULL_TREE
8373 || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld)))
8374 return 0;
8375 if (integer_zerop (DECL_SIZE_UNIT (fld)))
8376 continue;
8377 break;
8379 if (fld == NULL_TREE)
8381 if (ce == NULL)
8382 break;
8383 return 0;
8385 fld_base = DECL_CHAIN (fld);
8386 if (fld != field)
8388 cnt--;
8389 field = fld;
8390 pos = int_byte_position (field);
8391 val = build_zero_cst (TREE_TYPE (fld));
8392 if (TREE_CODE (val) == CONSTRUCTOR)
8393 to_free = val;
8397 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8398 && TYPE_DOMAIN (TREE_TYPE (field))
8399 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
8401 if (mask || off != -1)
8402 return 0;
8403 if (val == NULL_TREE)
8404 continue;
8405 if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE)
8406 return 0;
8407 fieldsize = int_size_in_bytes (TREE_TYPE (val));
8408 if (fieldsize < 0
8409 || (int) fieldsize != fieldsize
8410 || (pos + fieldsize) > INT_MAX)
8411 return 0;
8412 if (pos + fieldsize > total_bytes)
8414 if (ptr != NULL && total_bytes < len)
8415 memset (ptr + total_bytes, '\0',
8416 MIN (pos + fieldsize, len) - total_bytes);
8417 total_bytes = pos + fieldsize;
8420 else
8422 if (DECL_SIZE_UNIT (field) == NULL_TREE
8423 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8424 return 0;
8425 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8427 if (fieldsize == 0)
8428 continue;
8430 if (DECL_BIT_FIELD (field))
8432 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8433 return 0;
8434 fieldsize = TYPE_PRECISION (TREE_TYPE (field));
8435 bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8436 if (bpos % BITS_PER_UNIT)
8437 bpos %= BITS_PER_UNIT;
8438 else
8439 bpos = 0;
8440 fieldsize += bpos;
8441 epos = fieldsize % BITS_PER_UNIT;
8442 fieldsize += BITS_PER_UNIT - 1;
8443 fieldsize /= BITS_PER_UNIT;
8446 if (off != -1 && pos + fieldsize <= off)
8447 continue;
8449 if (val == NULL_TREE)
8450 continue;
8452 if (DECL_BIT_FIELD (field))
8454 /* FIXME: Handle PDP endian. */
8455 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8456 return 0;
8458 if (TREE_CODE (val) != INTEGER_CST)
8459 return 0;
8461 tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8462 tree repr_type = NULL_TREE;
8463 HOST_WIDE_INT rpos = 0;
8464 if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8466 rpos = int_byte_position (repr);
8467 repr_type = TREE_TYPE (repr);
8469 else
8471 repr_type = find_bitfield_repr_type (fieldsize, len);
8472 if (repr_type == NULL_TREE)
8473 return 0;
8474 HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type);
8475 gcc_assert (repr_size > 0 && repr_size <= len);
8476 if (pos + repr_size <= o + len)
8477 rpos = pos;
8478 else
8480 rpos = o + len - repr_size;
8481 gcc_assert (rpos <= pos);
8485 if (rpos > pos)
8486 return 0;
8487 wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8488 int diff = (TYPE_PRECISION (repr_type)
8489 - TYPE_PRECISION (TREE_TYPE (field)));
8490 HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8491 if (!BYTES_BIG_ENDIAN)
8492 w = wi::lshift (w, bitoff);
8493 else
8494 w = wi::lshift (w, diff - bitoff);
8495 val = wide_int_to_tree (repr_type, w);
8497 unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8498 / BITS_PER_UNIT + 1];
8499 int l = native_encode_int (val, buf, sizeof buf, 0);
8500 if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8501 return 0;
8503 if (ptr == NULL)
8504 continue;
8506 /* If the bitfield does not start at byte boundary, handle
8507 the partial byte at the start. */
8508 if (bpos
8509 && (off == -1 || (pos >= off && len >= 1)))
8511 if (!BYTES_BIG_ENDIAN)
8513 int msk = (1 << bpos) - 1;
8514 buf[pos - rpos] &= ~msk;
8515 buf[pos - rpos] |= ptr[pos - o] & msk;
8516 if (mask)
8518 if (fieldsize > 1 || epos == 0)
8519 mask[pos] &= msk;
8520 else
8521 mask[pos] &= (msk | ~((1 << epos) - 1));
8524 else
8526 int msk = (1 << (BITS_PER_UNIT - bpos)) - 1;
8527 buf[pos - rpos] &= msk;
8528 buf[pos - rpos] |= ptr[pos - o] & ~msk;
8529 if (mask)
8531 if (fieldsize > 1 || epos == 0)
8532 mask[pos] &= ~msk;
8533 else
8534 mask[pos] &= (~msk
8535 | ((1 << (BITS_PER_UNIT - epos))
8536 - 1));
8540 /* If the bitfield does not end at byte boundary, handle
8541 the partial byte at the end. */
8542 if (epos
8543 && (off == -1
8544 || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8546 if (!BYTES_BIG_ENDIAN)
8548 int msk = (1 << epos) - 1;
8549 buf[pos - rpos + fieldsize - 1] &= msk;
8550 buf[pos - rpos + fieldsize - 1]
8551 |= ptr[pos + fieldsize - 1 - o] & ~msk;
8552 if (mask && (fieldsize > 1 || bpos == 0))
8553 mask[pos + fieldsize - 1] &= ~msk;
8555 else
8557 int msk = (1 << (BITS_PER_UNIT - epos)) - 1;
8558 buf[pos - rpos + fieldsize - 1] &= ~msk;
8559 buf[pos - rpos + fieldsize - 1]
8560 |= ptr[pos + fieldsize - 1 - o] & msk;
8561 if (mask && (fieldsize > 1 || bpos == 0))
8562 mask[pos + fieldsize - 1] &= msk;
8565 if (off == -1
8566 || (pos >= off
8567 && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8569 memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8570 if (mask && (fieldsize > (bpos != 0) + (epos != 0)))
8571 memset (mask + pos + (bpos != 0), 0,
8572 fieldsize - (bpos != 0) - (epos != 0));
8574 else
8576 /* Partial overlap. */
8577 HOST_WIDE_INT fsz = fieldsize;
8578 gcc_assert (mask == NULL);
8579 if (pos < off)
8581 fsz -= (off - pos);
8582 pos = off;
8584 if (pos + fsz > (HOST_WIDE_INT) off + len)
8585 fsz = (HOST_WIDE_INT) off + len - pos;
8586 memcpy (ptr + pos - off, buf + (pos - rpos), fsz);
8588 continue;
8591 if (off == -1
8592 || (pos >= off
8593 && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8595 int fldsize = fieldsize;
8596 if (off == -1)
8598 tree fld = DECL_CHAIN (field);
8599 while (fld)
8601 if (TREE_CODE (fld) == FIELD_DECL)
8602 break;
8603 fld = DECL_CHAIN (fld);
8605 if (fld == NULL_TREE)
8606 fldsize = len - pos;
8608 r = native_encode_initializer (val, ptr ? ptr + pos - o
8609 : NULL,
8610 fldsize,
8611 off == -1 ? -1 : 0,
8612 mask ? mask + pos : NULL);
8613 if (!r)
8614 return 0;
8615 if (off == -1
8616 && fldsize != fieldsize
8617 && r > fieldsize
8618 && pos + r > total_bytes)
8619 total_bytes = pos + r;
8621 else
8623 /* Partial overlap. */
8624 unsigned char *p = NULL;
8625 int no = 0;
8626 int l;
8627 gcc_assert (mask == NULL);
8628 if (pos >= off)
8630 if (ptr)
8631 p = ptr + pos - off;
8632 l = MIN ((HOST_WIDE_INT) off + len - pos,
8633 fieldsize);
8635 else
8637 p = ptr;
8638 no = off - pos;
8639 l = len;
8641 if (!native_encode_initializer (val, p, l, no, NULL))
8642 return 0;
8645 return MIN (total_bytes - off, len);
8647 return 0;
8652 /* Subroutine of native_interpret_expr. Interpret the contents of
8653 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8654 If the buffer cannot be interpreted, return NULL_TREE. */
8656 static tree
8657 native_interpret_int (tree type, const unsigned char *ptr, int len)
8659 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8661 if (total_bytes > len
8662 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8663 return NULL_TREE;
8665 wide_int result = wi::from_buffer (ptr, total_bytes);
8667 return wide_int_to_tree (type, result);
8671 /* Subroutine of native_interpret_expr. Interpret the contents of
8672 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8673 If the buffer cannot be interpreted, return NULL_TREE. */
8675 static tree
8676 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
8678 scalar_mode mode = SCALAR_TYPE_MODE (type);
8679 int total_bytes = GET_MODE_SIZE (mode);
8680 double_int result;
8681 FIXED_VALUE_TYPE fixed_value;
8683 if (total_bytes > len
8684 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8685 return NULL_TREE;
8687 result = double_int::from_buffer (ptr, total_bytes);
8688 fixed_value = fixed_from_double_int (result, mode);
8690 return build_fixed (type, fixed_value);
8694 /* Subroutine of native_interpret_expr. Interpret the contents of
8695 the buffer PTR of length LEN as a REAL_CST of type TYPE.
8696 If the buffer cannot be interpreted, return NULL_TREE. */
8698 tree
8699 native_interpret_real (tree type, const unsigned char *ptr, int len)
8701 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8702 int total_bytes = GET_MODE_SIZE (mode);
8703 unsigned char value;
8704 /* There are always 32 bits in each long, no matter the size of
8705 the hosts long. We handle floating point representations with
8706 up to 192 bits. */
8707 REAL_VALUE_TYPE r;
8708 long tmp[6];
8710 if (total_bytes > len || total_bytes > 24)
8711 return NULL_TREE;
8712 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8714 memset (tmp, 0, sizeof (tmp));
8715 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8716 bitpos += BITS_PER_UNIT)
8718 /* Both OFFSET and BYTE index within a long;
8719 bitpos indexes the whole float. */
8720 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8721 if (UNITS_PER_WORD < 4)
8723 int word = byte / UNITS_PER_WORD;
8724 if (WORDS_BIG_ENDIAN)
8725 word = (words - 1) - word;
8726 offset = word * UNITS_PER_WORD;
8727 if (BYTES_BIG_ENDIAN)
8728 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8729 else
8730 offset += byte % UNITS_PER_WORD;
8732 else
8734 offset = byte;
8735 if (BYTES_BIG_ENDIAN)
8737 /* Reverse bytes within each long, or within the entire float
8738 if it's smaller than a long (for HFmode). */
8739 offset = MIN (3, total_bytes - 1) - offset;
8740 gcc_assert (offset >= 0);
8743 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8745 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8748 real_from_target (&r, tmp, mode);
8749 return build_real (type, r);
8753 /* Subroutine of native_interpret_expr. Interpret the contents of
8754 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8755 If the buffer cannot be interpreted, return NULL_TREE. */
8757 static tree
8758 native_interpret_complex (tree type, const unsigned char *ptr, int len)
8760 tree etype, rpart, ipart;
8761 int size;
8763 etype = TREE_TYPE (type);
8764 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8765 if (size * 2 > len)
8766 return NULL_TREE;
8767 rpart = native_interpret_expr (etype, ptr, size);
8768 if (!rpart)
8769 return NULL_TREE;
8770 ipart = native_interpret_expr (etype, ptr+size, size);
8771 if (!ipart)
8772 return NULL_TREE;
8773 return build_complex (type, rpart, ipart);
8776 /* Read a vector of type TYPE from the target memory image given by BYTES,
8777 which contains LEN bytes. The vector is known to be encodable using
8778 NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8780 Return the vector on success, otherwise return null. */
8782 static tree
8783 native_interpret_vector_part (tree type, const unsigned char *bytes,
8784 unsigned int len, unsigned int npatterns,
8785 unsigned int nelts_per_pattern)
8787 tree elt_type = TREE_TYPE (type);
8788 if (VECTOR_BOOLEAN_TYPE_P (type)
8789 && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT)
8791 /* This is the only case in which elements can be smaller than a byte.
8792 Element 0 is always in the lsb of the containing byte. */
8793 unsigned int elt_bits = TYPE_PRECISION (elt_type);
8794 if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT)
8795 return NULL_TREE;
8797 tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8798 for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8800 unsigned int bit_index = i * elt_bits;
8801 unsigned int byte_index = bit_index / BITS_PER_UNIT;
8802 unsigned int lsb = bit_index % BITS_PER_UNIT;
8803 builder.quick_push (bytes[byte_index] & (1 << lsb)
8804 ? build_all_ones_cst (elt_type)
8805 : build_zero_cst (elt_type));
8807 return builder.build ();
8810 unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8811 if (elt_bytes * npatterns * nelts_per_pattern > len)
8812 return NULL_TREE;
8814 tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8815 for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8817 tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8818 if (!elt)
8819 return NULL_TREE;
8820 builder.quick_push (elt);
8821 bytes += elt_bytes;
8823 return builder.build ();
8826 /* Subroutine of native_interpret_expr. Interpret the contents of
8827 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8828 If the buffer cannot be interpreted, return NULL_TREE. */
8830 static tree
8831 native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8833 tree etype;
8834 unsigned int size;
8835 unsigned HOST_WIDE_INT count;
8837 etype = TREE_TYPE (type);
8838 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8839 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&count)
8840 || size * count > len)
8841 return NULL_TREE;
8843 return native_interpret_vector_part (type, ptr, len, count, 1);
8847 /* Subroutine of fold_view_convert_expr. Interpret the contents of
8848 the buffer PTR of length LEN as a constant of type TYPE. For
8849 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8850 we return a REAL_CST, etc... If the buffer cannot be interpreted,
8851 return NULL_TREE. */
8853 tree
8854 native_interpret_expr (tree type, const unsigned char *ptr, int len)
8856 switch (TREE_CODE (type))
8858 case INTEGER_TYPE:
8859 case ENUMERAL_TYPE:
8860 case BOOLEAN_TYPE:
8861 case POINTER_TYPE:
8862 case REFERENCE_TYPE:
8863 case OFFSET_TYPE:
8864 return native_interpret_int (type, ptr, len);
8866 case REAL_TYPE:
8867 if (tree ret = native_interpret_real (type, ptr, len))
8869 /* For floating point values in composite modes, punt if this
8870 folding doesn't preserve bit representation. As the mode doesn't
8871 have fixed precision while GCC pretends it does, there could be
8872 valid values that GCC can't really represent accurately.
8873 See PR95450. Even for other modes, e.g. x86 XFmode can have some
8874 bit combinationations which GCC doesn't preserve. */
8875 unsigned char buf[24 * 2];
8876 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8877 int total_bytes = GET_MODE_SIZE (mode);
8878 memcpy (buf + 24, ptr, total_bytes);
8879 clear_type_padding_in_mask (type, buf + 24);
8880 if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8881 || memcmp (buf + 24, buf, total_bytes) != 0)
8882 return NULL_TREE;
8883 return ret;
8885 return NULL_TREE;
8887 case FIXED_POINT_TYPE:
8888 return native_interpret_fixed (type, ptr, len);
8890 case COMPLEX_TYPE:
8891 return native_interpret_complex (type, ptr, len);
8893 case VECTOR_TYPE:
8894 return native_interpret_vector (type, ptr, len);
8896 default:
8897 return NULL_TREE;
8901 /* Returns true if we can interpret the contents of a native encoding
8902 as TYPE. */
8904 bool
8905 can_native_interpret_type_p (tree type)
8907 switch (TREE_CODE (type))
8909 case INTEGER_TYPE:
8910 case ENUMERAL_TYPE:
8911 case BOOLEAN_TYPE:
8912 case POINTER_TYPE:
8913 case REFERENCE_TYPE:
8914 case FIXED_POINT_TYPE:
8915 case REAL_TYPE:
8916 case COMPLEX_TYPE:
8917 case VECTOR_TYPE:
8918 case OFFSET_TYPE:
8919 return true;
8920 default:
8921 return false;
8925 /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8926 byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8928 tree
8929 native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8930 int len)
8932 vec<constructor_elt, va_gc> *elts = NULL;
8933 if (TREE_CODE (type) == ARRAY_TYPE)
8935 HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type));
8936 if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE)
8937 return NULL_TREE;
8939 HOST_WIDE_INT cnt = 0;
8940 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8942 if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
8943 return NULL_TREE;
8944 cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1;
8946 if (eltsz == 0)
8947 cnt = 0;
8948 HOST_WIDE_INT pos = 0;
8949 for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz)
8951 tree v = NULL_TREE;
8952 if (pos >= len || pos + eltsz > len)
8953 return NULL_TREE;
8954 if (can_native_interpret_type_p (TREE_TYPE (type)))
8956 v = native_interpret_expr (TREE_TYPE (type),
8957 ptr + off + pos, eltsz);
8958 if (v == NULL_TREE)
8959 return NULL_TREE;
8961 else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
8962 || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
8963 v = native_interpret_aggregate (TREE_TYPE (type), ptr, off + pos,
8964 eltsz);
8965 if (v == NULL_TREE)
8966 return NULL_TREE;
8967 CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v);
8969 return build_constructor (type, elts);
8971 if (TREE_CODE (type) != RECORD_TYPE)
8972 return NULL_TREE;
8973 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8975 if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field))
8976 continue;
8977 tree fld = field;
8978 HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8979 int diff = 0;
8980 tree v = NULL_TREE;
8981 if (DECL_BIT_FIELD (field))
8983 fld = DECL_BIT_FIELD_REPRESENTATIVE (field);
8984 if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld)))
8986 poly_int64 bitoffset;
8987 poly_uint64 field_offset, fld_offset;
8988 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
8989 && poly_int_tree_p (DECL_FIELD_OFFSET (fld), &fld_offset))
8990 bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT;
8991 else
8992 bitoffset = 0;
8993 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
8994 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)));
8995 diff = (TYPE_PRECISION (TREE_TYPE (fld))
8996 - TYPE_PRECISION (TREE_TYPE (field)));
8997 if (!bitoffset.is_constant (&bitoff)
8998 || bitoff < 0
8999 || bitoff > diff)
9000 return NULL_TREE;
9002 else
9004 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
9005 return NULL_TREE;
9006 int fieldsize = TYPE_PRECISION (TREE_TYPE (field));
9007 int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
9008 bpos %= BITS_PER_UNIT;
9009 fieldsize += bpos;
9010 fieldsize += BITS_PER_UNIT - 1;
9011 fieldsize /= BITS_PER_UNIT;
9012 tree repr_type = find_bitfield_repr_type (fieldsize, len);
9013 if (repr_type == NULL_TREE)
9014 return NULL_TREE;
9015 sz = int_size_in_bytes (repr_type);
9016 if (sz < 0 || sz > len)
9017 return NULL_TREE;
9018 pos = int_byte_position (field);
9019 if (pos < 0 || pos > len || pos + fieldsize > len)
9020 return NULL_TREE;
9021 HOST_WIDE_INT rpos;
9022 if (pos + sz <= len)
9023 rpos = pos;
9024 else
9026 rpos = len - sz;
9027 gcc_assert (rpos <= pos);
9029 bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos;
9030 pos = rpos;
9031 diff = (TYPE_PRECISION (repr_type)
9032 - TYPE_PRECISION (TREE_TYPE (field)));
9033 v = native_interpret_expr (repr_type, ptr + off + pos, sz);
9034 if (v == NULL_TREE)
9035 return NULL_TREE;
9036 fld = NULL_TREE;
9040 if (fld)
9042 sz = int_size_in_bytes (TREE_TYPE (fld));
9043 if (sz < 0 || sz > len)
9044 return NULL_TREE;
9045 tree byte_pos = byte_position (fld);
9046 if (!tree_fits_shwi_p (byte_pos))
9047 return NULL_TREE;
9048 pos = tree_to_shwi (byte_pos);
9049 if (pos < 0 || pos > len || pos + sz > len)
9050 return NULL_TREE;
9052 if (fld == NULL_TREE)
9053 /* Already handled above. */;
9054 else if (can_native_interpret_type_p (TREE_TYPE (fld)))
9056 v = native_interpret_expr (TREE_TYPE (fld),
9057 ptr + off + pos, sz);
9058 if (v == NULL_TREE)
9059 return NULL_TREE;
9061 else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE
9062 || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE)
9063 v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off + pos, sz);
9064 if (v == NULL_TREE)
9065 return NULL_TREE;
9066 if (fld != field)
9068 if (TREE_CODE (v) != INTEGER_CST)
9069 return NULL_TREE;
9071 /* FIXME: Figure out how to handle PDP endian bitfields. */
9072 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9073 return NULL_TREE;
9074 if (!BYTES_BIG_ENDIAN)
9075 v = wide_int_to_tree (TREE_TYPE (field),
9076 wi::lrshift (wi::to_wide (v), bitoff));
9077 else
9078 v = wide_int_to_tree (TREE_TYPE (field),
9079 wi::lrshift (wi::to_wide (v),
9080 diff - bitoff));
9082 CONSTRUCTOR_APPEND_ELT (elts, field, v);
9084 return build_constructor (type, elts);
9087 /* Routines for manipulation of native_encode_expr encoded data if the encoded
9088 or extracted constant positions and/or sizes aren't byte aligned. */
9090 /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
9091 bits between adjacent elements. AMNT should be within
9092 [0, BITS_PER_UNIT).
9093 Example, AMNT = 2:
9094 00011111|11100000 << 2 = 01111111|10000000
9095 PTR[1] | PTR[0] PTR[1] | PTR[0]. */
9097 void
9098 shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
9099 unsigned int amnt)
9101 if (amnt == 0)
9102 return;
9104 unsigned char carry_over = 0U;
9105 unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
9106 unsigned char clear_mask = (~0U) << amnt;
9108 for (unsigned int i = 0; i < sz; i++)
9110 unsigned prev_carry_over = carry_over;
9111 carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
9113 ptr[i] <<= amnt;
9114 if (i != 0)
9116 ptr[i] &= clear_mask;
9117 ptr[i] |= prev_carry_over;
9122 /* Like shift_bytes_in_array_left but for big-endian.
9123 Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9124 bits between adjacent elements. AMNT should be within
9125 [0, BITS_PER_UNIT).
9126 Example, AMNT = 2:
9127 00011111|11100000 >> 2 = 00000111|11111000
9128 PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9130 void
9131 shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
9132 unsigned int amnt)
9134 if (amnt == 0)
9135 return;
9137 unsigned char carry_over = 0U;
9138 unsigned char carry_mask = ~(~0U << amnt);
9140 for (unsigned int i = 0; i < sz; i++)
9142 unsigned prev_carry_over = carry_over;
9143 carry_over = ptr[i] & carry_mask;
9145 carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
9146 ptr[i] >>= amnt;
9147 ptr[i] |= prev_carry_over;
9151 /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9152 directly on the VECTOR_CST encoding, in a way that works for variable-
9153 length vectors. Return the resulting VECTOR_CST on success or null
9154 on failure. */
9156 static tree
9157 fold_view_convert_vector_encoding (tree type, tree expr)
9159 tree expr_type = TREE_TYPE (expr);
9160 poly_uint64 type_bits, expr_bits;
9161 if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9162 || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9163 return NULL_TREE;
9165 poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9166 poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9167 unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9168 unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units);
9170 /* We can only preserve the semantics of a stepped pattern if the new
9171 vector element is an integer of the same size. */
9172 if (VECTOR_CST_STEPPED_P (expr)
9173 && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits))
9174 return NULL_TREE;
9176 /* The number of bits needed to encode one element from every pattern
9177 of the original vector. */
9178 unsigned int expr_sequence_bits
9179 = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits;
9181 /* The number of bits needed to encode one element from every pattern
9182 of the result. */
9183 unsigned int type_sequence_bits
9184 = least_common_multiple (expr_sequence_bits, type_elt_bits);
9186 /* Don't try to read more bytes than are available, which can happen
9187 for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9188 The general VIEW_CONVERT handling can cope with that case, so there's
9189 no point complicating things here. */
9190 unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9191 unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9192 BITS_PER_UNIT);
9193 unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9194 if (known_gt (buffer_bits, expr_bits))
9195 return NULL_TREE;
9197 /* Get enough bytes of EXPR to form the new encoding. */
9198 auto_vec<unsigned char, 128> buffer (buffer_bytes);
9199 buffer.quick_grow (buffer_bytes);
9200 if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9201 buffer_bits / expr_elt_bits)
9202 != (int) buffer_bytes)
9203 return NULL_TREE;
9205 /* Reencode the bytes as TYPE. */
9206 unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9207 return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9208 type_npatterns, nelts_per_pattern);
9211 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9212 TYPE at compile-time. If we're unable to perform the conversion
9213 return NULL_TREE. */
9215 static tree
9216 fold_view_convert_expr (tree type, tree expr)
9218 /* We support up to 512-bit values (for V8DFmode). */
9219 unsigned char buffer[64];
9220 int len;
9222 /* Check that the host and target are sane. */
9223 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9224 return NULL_TREE;
9226 if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9227 if (tree res = fold_view_convert_vector_encoding (type, expr))
9228 return res;
9230 len = native_encode_expr (expr, buffer, sizeof (buffer));
9231 if (len == 0)
9232 return NULL_TREE;
9234 return native_interpret_expr (type, buffer, len);
9237 /* Build an expression for the address of T. Folds away INDIRECT_REF
9238 to avoid confusing the gimplify process. */
9240 tree
9241 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
9243 /* The size of the object is not relevant when talking about its address. */
9244 if (TREE_CODE (t) == WITH_SIZE_EXPR)
9245 t = TREE_OPERAND (t, 0);
9247 if (TREE_CODE (t) == INDIRECT_REF)
9249 t = TREE_OPERAND (t, 0);
9251 if (TREE_TYPE (t) != ptrtype)
9252 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9254 else if (TREE_CODE (t) == MEM_REF
9255 && integer_zerop (TREE_OPERAND (t, 1)))
9257 t = TREE_OPERAND (t, 0);
9259 if (TREE_TYPE (t) != ptrtype)
9260 t = fold_convert_loc (loc, ptrtype, t);
9262 else if (TREE_CODE (t) == MEM_REF
9263 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9264 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9265 TREE_OPERAND (t, 0),
9266 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9267 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9269 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9271 if (TREE_TYPE (t) != ptrtype)
9272 t = fold_convert_loc (loc, ptrtype, t);
9274 else
9275 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
9277 return t;
9280 /* Build an expression for the address of T. */
9282 tree
9283 build_fold_addr_expr_loc (location_t loc, tree t)
9285 tree ptrtype = build_pointer_type (TREE_TYPE (t));
9287 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
9290 /* Fold a unary expression of code CODE and type TYPE with operand
9291 OP0. Return the folded expression if folding is successful.
9292 Otherwise, return NULL_TREE. */
9294 tree
9295 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9297 tree tem;
9298 tree arg0;
9299 enum tree_code_class kind = TREE_CODE_CLASS (code);
9301 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9302 && TREE_CODE_LENGTH (code) == 1);
9304 arg0 = op0;
9305 if (arg0)
9307 if (CONVERT_EXPR_CODE_P (code)
9308 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
9310 /* Don't use STRIP_NOPS, because signedness of argument type
9311 matters. */
9312 STRIP_SIGN_NOPS (arg0);
9314 else
9316 /* Strip any conversions that don't change the mode. This
9317 is safe for every expression, except for a comparison
9318 expression because its signedness is derived from its
9319 operands.
9321 Note that this is done as an internal manipulation within
9322 the constant folder, in order to find the simplest
9323 representation of the arguments so that their form can be
9324 studied. In any cases, the appropriate type conversions
9325 should be put back in the tree that will get out of the
9326 constant folder. */
9327 STRIP_NOPS (arg0);
9330 if (CONSTANT_CLASS_P (arg0))
9332 tree tem = const_unop (code, type, arg0);
9333 if (tem)
9335 if (TREE_TYPE (tem) != type)
9336 tem = fold_convert_loc (loc, type, tem);
9337 return tem;
9342 tem = generic_simplify (loc, code, type, op0);
9343 if (tem)
9344 return tem;
9346 if (TREE_CODE_CLASS (code) == tcc_unary)
9348 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9349 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9350 fold_build1_loc (loc, code, type,
9351 fold_convert_loc (loc, TREE_TYPE (op0),
9352 TREE_OPERAND (arg0, 1))));
9353 else if (TREE_CODE (arg0) == COND_EXPR)
9355 tree arg01 = TREE_OPERAND (arg0, 1);
9356 tree arg02 = TREE_OPERAND (arg0, 2);
9357 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9358 arg01 = fold_build1_loc (loc, code, type,
9359 fold_convert_loc (loc,
9360 TREE_TYPE (op0), arg01));
9361 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9362 arg02 = fold_build1_loc (loc, code, type,
9363 fold_convert_loc (loc,
9364 TREE_TYPE (op0), arg02));
9365 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
9366 arg01, arg02);
9368 /* If this was a conversion, and all we did was to move into
9369 inside the COND_EXPR, bring it back out. But leave it if
9370 it is a conversion from integer to integer and the
9371 result precision is no wider than a word since such a
9372 conversion is cheap and may be optimized away by combine,
9373 while it couldn't if it were outside the COND_EXPR. Then return
9374 so we don't get into an infinite recursion loop taking the
9375 conversion out and then back in. */
9377 if ((CONVERT_EXPR_CODE_P (code)
9378 || code == NON_LVALUE_EXPR)
9379 && TREE_CODE (tem) == COND_EXPR
9380 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9381 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9382 && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9383 && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9384 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9385 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9386 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9387 && (INTEGRAL_TYPE_P
9388 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9389 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9390 || flag_syntax_only))
9391 tem = build1_loc (loc, code, type,
9392 build3 (COND_EXPR,
9393 TREE_TYPE (TREE_OPERAND
9394 (TREE_OPERAND (tem, 1), 0)),
9395 TREE_OPERAND (tem, 0),
9396 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9397 TREE_OPERAND (TREE_OPERAND (tem, 2),
9398 0)));
9399 return tem;
9403 switch (code)
9405 case NON_LVALUE_EXPR:
9406 if (!maybe_lvalue_p (op0))
9407 return fold_convert_loc (loc, type, op0);
9408 return NULL_TREE;
9410 CASE_CONVERT:
9411 case FLOAT_EXPR:
9412 case FIX_TRUNC_EXPR:
9413 if (COMPARISON_CLASS_P (op0))
9415 /* If we have (type) (a CMP b) and type is an integral type, return
9416 new expression involving the new type. Canonicalize
9417 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9418 non-integral type.
9419 Do not fold the result as that would not simplify further, also
9420 folding again results in recursions. */
9421 if (TREE_CODE (type) == BOOLEAN_TYPE)
9422 return build2_loc (loc, TREE_CODE (op0), type,
9423 TREE_OPERAND (op0, 0),
9424 TREE_OPERAND (op0, 1));
9425 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9426 && TREE_CODE (type) != VECTOR_TYPE)
9427 return build3_loc (loc, COND_EXPR, type, op0,
9428 constant_boolean_node (true, type),
9429 constant_boolean_node (false, type));
9432 /* Handle (T *)&A.B.C for A being of type T and B and C
9433 living at offset zero. This occurs frequently in
9434 C++ upcasting and then accessing the base. */
9435 if (TREE_CODE (op0) == ADDR_EXPR
9436 && POINTER_TYPE_P (type)
9437 && handled_component_p (TREE_OPERAND (op0, 0)))
9439 poly_int64 bitsize, bitpos;
9440 tree offset;
9441 machine_mode mode;
9442 int unsignedp, reversep, volatilep;
9443 tree base
9444 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
9445 &offset, &mode, &unsignedp, &reversep,
9446 &volatilep);
9447 /* If the reference was to a (constant) zero offset, we can use
9448 the address of the base if it has the same base type
9449 as the result type and the pointer type is unqualified. */
9450 if (!offset
9451 && known_eq (bitpos, 0)
9452 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9453 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9454 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9455 return fold_convert_loc (loc, type,
9456 build_fold_addr_expr_loc (loc, base));
9459 if (TREE_CODE (op0) == MODIFY_EXPR
9460 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9461 /* Detect assigning a bitfield. */
9462 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
9463 && DECL_BIT_FIELD
9464 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
9466 /* Don't leave an assignment inside a conversion
9467 unless assigning a bitfield. */
9468 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9469 /* First do the assignment, then return converted constant. */
9470 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9471 suppress_warning (tem /* What warning? */);
9472 TREE_USED (tem) = 1;
9473 return tem;
9476 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9477 constants (if x has signed type, the sign bit cannot be set
9478 in c). This folds extension into the BIT_AND_EXPR.
9479 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9480 very likely don't have maximal range for their precision and this
9481 transformation effectively doesn't preserve non-maximal ranges. */
9482 if (TREE_CODE (type) == INTEGER_TYPE
9483 && TREE_CODE (op0) == BIT_AND_EXPR
9484 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9486 tree and_expr = op0;
9487 tree and0 = TREE_OPERAND (and_expr, 0);
9488 tree and1 = TREE_OPERAND (and_expr, 1);
9489 int change = 0;
9491 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9492 || (TYPE_PRECISION (type)
9493 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9494 change = 1;
9495 else if (TYPE_PRECISION (TREE_TYPE (and1))
9496 <= HOST_BITS_PER_WIDE_INT
9497 && tree_fits_uhwi_p (and1))
9499 unsigned HOST_WIDE_INT cst;
9501 cst = tree_to_uhwi (and1);
9502 cst &= HOST_WIDE_INT_M1U
9503 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9504 change = (cst == 0);
9505 if (change
9506 && !flag_syntax_only
9507 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
9508 == ZERO_EXTEND))
9510 tree uns = unsigned_type_for (TREE_TYPE (and0));
9511 and0 = fold_convert_loc (loc, uns, and0);
9512 and1 = fold_convert_loc (loc, uns, and1);
9515 if (change)
9517 tem = force_fit_type (type, wi::to_widest (and1), 0,
9518 TREE_OVERFLOW (and1));
9519 return fold_build2_loc (loc, BIT_AND_EXPR, type,
9520 fold_convert_loc (loc, type, and0), tem);
9524 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9525 cast (T1)X will fold away. We assume that this happens when X itself
9526 is a cast. */
9527 if (POINTER_TYPE_P (type)
9528 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9529 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9531 tree arg00 = TREE_OPERAND (arg0, 0);
9532 tree arg01 = TREE_OPERAND (arg0, 1);
9534 /* If -fsanitize=alignment, avoid this optimization in GENERIC
9535 when the pointed type needs higher alignment than
9536 the p+ first operand's pointed type. */
9537 if (!in_gimple_form
9538 && sanitize_flags_p (SANITIZE_ALIGNMENT)
9539 && (min_align_of_type (TREE_TYPE (type))
9540 > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00)))))
9541 return NULL_TREE;
9543 /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9544 when type is a reference type and arg00's type is not,
9545 because arg00 could be validly nullptr and if arg01 doesn't return,
9546 we don't want false positive binding of reference to nullptr. */
9547 if (TREE_CODE (type) == REFERENCE_TYPE
9548 && !in_gimple_form
9549 && sanitize_flags_p (SANITIZE_NULL)
9550 && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9551 return NULL_TREE;
9553 arg00 = fold_convert_loc (loc, type, arg00);
9554 return fold_build_pointer_plus_loc (loc, arg00, arg01);
9557 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9558 of the same precision, and X is an integer type not narrower than
9559 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9560 if (INTEGRAL_TYPE_P (type)
9561 && TREE_CODE (op0) == BIT_NOT_EXPR
9562 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9563 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9564 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9566 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9567 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9568 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9569 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9570 fold_convert_loc (loc, type, tem));
9573 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9574 type of X and Y (integer types only). */
9575 if (INTEGRAL_TYPE_P (type)
9576 && TREE_CODE (op0) == MULT_EXPR
9577 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9578 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0))
9579 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
9580 || !sanitize_flags_p (SANITIZE_SI_OVERFLOW)))
9582 /* Be careful not to introduce new overflows. */
9583 tree mult_type;
9584 if (TYPE_OVERFLOW_WRAPS (type))
9585 mult_type = type;
9586 else
9587 mult_type = unsigned_type_for (type);
9589 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9591 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9592 fold_convert_loc (loc, mult_type,
9593 TREE_OPERAND (op0, 0)),
9594 fold_convert_loc (loc, mult_type,
9595 TREE_OPERAND (op0, 1)));
9596 return fold_convert_loc (loc, type, tem);
9600 return NULL_TREE;
9602 case VIEW_CONVERT_EXPR:
9603 if (TREE_CODE (op0) == MEM_REF)
9605 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9606 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9607 tem = fold_build2_loc (loc, MEM_REF, type,
9608 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9609 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9610 return tem;
9613 return NULL_TREE;
9615 case NEGATE_EXPR:
9616 tem = fold_negate_expr (loc, arg0);
9617 if (tem)
9618 return fold_convert_loc (loc, type, tem);
9619 return NULL_TREE;
9621 case ABS_EXPR:
9622 /* Convert fabs((double)float) into (double)fabsf(float). */
9623 if (TREE_CODE (arg0) == NOP_EXPR
9624 && TREE_CODE (type) == REAL_TYPE)
9626 tree targ0 = strip_float_extensions (arg0);
9627 if (targ0 != arg0)
9628 return fold_convert_loc (loc, type,
9629 fold_build1_loc (loc, ABS_EXPR,
9630 TREE_TYPE (targ0),
9631 targ0));
9633 return NULL_TREE;
9635 case BIT_NOT_EXPR:
9636 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9637 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9638 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9639 fold_convert_loc (loc, type,
9640 TREE_OPERAND (arg0, 0)))))
9641 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
9642 fold_convert_loc (loc, type,
9643 TREE_OPERAND (arg0, 1)));
9644 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9645 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9646 fold_convert_loc (loc, type,
9647 TREE_OPERAND (arg0, 1)))))
9648 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
9649 fold_convert_loc (loc, type,
9650 TREE_OPERAND (arg0, 0)), tem);
9652 return NULL_TREE;
9654 case TRUTH_NOT_EXPR:
9655 /* Note that the operand of this must be an int
9656 and its values must be 0 or 1.
9657 ("true" is a fixed value perhaps depending on the language,
9658 but we don't handle values other than 1 correctly yet.) */
9659 tem = fold_truth_not_expr (loc, arg0);
9660 if (!tem)
9661 return NULL_TREE;
9662 return fold_convert_loc (loc, type, tem);
9664 case INDIRECT_REF:
9665 /* Fold *&X to X if X is an lvalue. */
9666 if (TREE_CODE (op0) == ADDR_EXPR)
9668 tree op00 = TREE_OPERAND (op0, 0);
9669 if ((VAR_P (op00)
9670 || TREE_CODE (op00) == PARM_DECL
9671 || TREE_CODE (op00) == RESULT_DECL)
9672 && !TREE_READONLY (op00))
9673 return op00;
9675 return NULL_TREE;
9677 default:
9678 return NULL_TREE;
9679 } /* switch (code) */
9683 /* If the operation was a conversion do _not_ mark a resulting constant
9684 with TREE_OVERFLOW if the original constant was not. These conversions
9685 have implementation defined behavior and retaining the TREE_OVERFLOW
9686 flag here would confuse later passes such as VRP. */
9687 tree
9688 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
9689 tree type, tree op0)
9691 tree res = fold_unary_loc (loc, code, type, op0);
9692 if (res
9693 && TREE_CODE (res) == INTEGER_CST
9694 && TREE_CODE (op0) == INTEGER_CST
9695 && CONVERT_EXPR_CODE_P (code))
9696 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
9698 return res;
9701 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9702 operands OP0 and OP1. LOC is the location of the resulting expression.
9703 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9704 Return the folded expression if folding is successful. Otherwise,
9705 return NULL_TREE. */
9706 static tree
9707 fold_truth_andor (location_t loc, enum tree_code code, tree type,
9708 tree arg0, tree arg1, tree op0, tree op1)
9710 tree tem;
9712 /* We only do these simplifications if we are optimizing. */
9713 if (!optimize)
9714 return NULL_TREE;
9716 /* Check for things like (A || B) && (A || C). We can convert this
9717 to A || (B && C). Note that either operator can be any of the four
9718 truth and/or operations and the transformation will still be
9719 valid. Also note that we only care about order for the
9720 ANDIF and ORIF operators. If B contains side effects, this
9721 might change the truth-value of A. */
9722 if (TREE_CODE (arg0) == TREE_CODE (arg1)
9723 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9724 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9725 || TREE_CODE (arg0) == TRUTH_AND_EXPR
9726 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9727 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9729 tree a00 = TREE_OPERAND (arg0, 0);
9730 tree a01 = TREE_OPERAND (arg0, 1);
9731 tree a10 = TREE_OPERAND (arg1, 0);
9732 tree a11 = TREE_OPERAND (arg1, 1);
9733 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9734 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9735 && (code == TRUTH_AND_EXPR
9736 || code == TRUTH_OR_EXPR));
9738 if (operand_equal_p (a00, a10, 0))
9739 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9740 fold_build2_loc (loc, code, type, a01, a11));
9741 else if (commutative && operand_equal_p (a00, a11, 0))
9742 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9743 fold_build2_loc (loc, code, type, a01, a10));
9744 else if (commutative && operand_equal_p (a01, a10, 0))
9745 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
9746 fold_build2_loc (loc, code, type, a00, a11));
9748 /* This case if tricky because we must either have commutative
9749 operators or else A10 must not have side-effects. */
9751 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9752 && operand_equal_p (a01, a11, 0))
9753 return fold_build2_loc (loc, TREE_CODE (arg0), type,
9754 fold_build2_loc (loc, code, type, a00, a10),
9755 a01);
9758 /* See if we can build a range comparison. */
9759 if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9760 return tem;
9762 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9763 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9765 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9766 if (tem)
9767 return fold_build2_loc (loc, code, type, tem, arg1);
9770 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9771 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9773 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9774 if (tem)
9775 return fold_build2_loc (loc, code, type, arg0, tem);
9778 /* Check for the possibility of merging component references. If our
9779 lhs is another similar operation, try to merge its rhs with our
9780 rhs. Then try to merge our lhs and rhs. */
9781 if (TREE_CODE (arg0) == code
9782 && (tem = fold_truth_andor_1 (loc, code, type,
9783 TREE_OPERAND (arg0, 1), arg1)) != 0)
9784 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9786 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9787 return tem;
9789 bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9790 if (param_logical_op_non_short_circuit != -1)
9791 logical_op_non_short_circuit
9792 = param_logical_op_non_short_circuit;
9793 if (logical_op_non_short_circuit
9794 && !sanitize_coverage_p ()
9795 && (code == TRUTH_AND_EXPR
9796 || code == TRUTH_ANDIF_EXPR
9797 || code == TRUTH_OR_EXPR
9798 || code == TRUTH_ORIF_EXPR))
9800 enum tree_code ncode, icode;
9802 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9803 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9804 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
9806 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9807 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9808 We don't want to pack more than two leafs to a non-IF AND/OR
9809 expression.
9810 If tree-code of left-hand operand isn't an AND/OR-IF code and not
9811 equal to IF-CODE, then we don't want to add right-hand operand.
9812 If the inner right-hand side of left-hand operand has
9813 side-effects, or isn't simple, then we can't add to it,
9814 as otherwise we might destroy if-sequence. */
9815 if (TREE_CODE (arg0) == icode
9816 && simple_condition_p (arg1)
9817 /* Needed for sequence points to handle trappings, and
9818 side-effects. */
9819 && simple_condition_p (TREE_OPERAND (arg0, 1)))
9821 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9822 arg1);
9823 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9824 tem);
9826 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9827 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9828 else if (TREE_CODE (arg1) == icode
9829 && simple_condition_p (arg0)
9830 /* Needed for sequence points to handle trappings, and
9831 side-effects. */
9832 && simple_condition_p (TREE_OPERAND (arg1, 0)))
9834 tem = fold_build2_loc (loc, ncode, type,
9835 arg0, TREE_OPERAND (arg1, 0));
9836 return fold_build2_loc (loc, icode, type, tem,
9837 TREE_OPERAND (arg1, 1));
9839 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9840 into (A OR B).
9841 For sequence point consistancy, we need to check for trapping,
9842 and side-effects. */
9843 else if (code == icode && simple_condition_p (arg0)
9844 && simple_condition_p (arg1))
9845 return fold_build2_loc (loc, ncode, type, arg0, arg1);
9848 return NULL_TREE;
9851 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9852 by changing CODE to reduce the magnitude of constants involved in
9853 ARG0 of the comparison.
9854 Returns a canonicalized comparison tree if a simplification was
9855 possible, otherwise returns NULL_TREE.
9856 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9857 valid if signed overflow is undefined. */
9859 static tree
9860 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9861 tree arg0, tree arg1,
9862 bool *strict_overflow_p)
9864 enum tree_code code0 = TREE_CODE (arg0);
9865 tree t, cst0 = NULL_TREE;
9866 int sgn0;
9868 /* Match A +- CST code arg1. We can change this only if overflow
9869 is undefined. */
9870 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9871 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9872 /* In principle pointers also have undefined overflow behavior,
9873 but that causes problems elsewhere. */
9874 && !POINTER_TYPE_P (TREE_TYPE (arg0))
9875 && (code0 == MINUS_EXPR
9876 || code0 == PLUS_EXPR)
9877 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9878 return NULL_TREE;
9880 /* Identify the constant in arg0 and its sign. */
9881 cst0 = TREE_OPERAND (arg0, 1);
9882 sgn0 = tree_int_cst_sgn (cst0);
9884 /* Overflowed constants and zero will cause problems. */
9885 if (integer_zerop (cst0)
9886 || TREE_OVERFLOW (cst0))
9887 return NULL_TREE;
9889 /* See if we can reduce the magnitude of the constant in
9890 arg0 by changing the comparison code. */
9891 /* A - CST < arg1 -> A - CST-1 <= arg1. */
9892 if (code == LT_EXPR
9893 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9894 code = LE_EXPR;
9895 /* A + CST > arg1 -> A + CST-1 >= arg1. */
9896 else if (code == GT_EXPR
9897 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9898 code = GE_EXPR;
9899 /* A + CST <= arg1 -> A + CST-1 < arg1. */
9900 else if (code == LE_EXPR
9901 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9902 code = LT_EXPR;
9903 /* A - CST >= arg1 -> A - CST-1 > arg1. */
9904 else if (code == GE_EXPR
9905 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9906 code = GT_EXPR;
9907 else
9908 return NULL_TREE;
9909 *strict_overflow_p = true;
9911 /* Now build the constant reduced in magnitude. But not if that
9912 would produce one outside of its types range. */
9913 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9914 && ((sgn0 == 1
9915 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9916 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9917 || (sgn0 == -1
9918 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9919 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9920 return NULL_TREE;
9922 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9923 cst0, build_int_cst (TREE_TYPE (cst0), 1));
9924 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9925 t = fold_convert (TREE_TYPE (arg1), t);
9927 return fold_build2_loc (loc, code, type, t, arg1);
9930 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9931 overflow further. Try to decrease the magnitude of constants involved
9932 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9933 and put sole constants at the second argument position.
9934 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9936 static tree
9937 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9938 tree arg0, tree arg1)
9940 tree t;
9941 bool strict_overflow_p;
9942 const char * const warnmsg = G_("assuming signed overflow does not occur "
9943 "when reducing constant in comparison");
9945 /* Try canonicalization by simplifying arg0. */
9946 strict_overflow_p = false;
9947 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
9948 &strict_overflow_p);
9949 if (t)
9951 if (strict_overflow_p)
9952 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9953 return t;
9956 /* Try canonicalization by simplifying arg1 using the swapped
9957 comparison. */
9958 code = swap_tree_comparison (code);
9959 strict_overflow_p = false;
9960 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
9961 &strict_overflow_p);
9962 if (t && strict_overflow_p)
9963 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9964 return t;
9967 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9968 space. This is used to avoid issuing overflow warnings for
9969 expressions like &p->x which cannot wrap. */
9971 static bool
9972 pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
9974 if (!POINTER_TYPE_P (TREE_TYPE (base)))
9975 return true;
9977 if (maybe_lt (bitpos, 0))
9978 return true;
9980 poly_wide_int wi_offset;
9981 int precision = TYPE_PRECISION (TREE_TYPE (base));
9982 if (offset == NULL_TREE)
9983 wi_offset = wi::zero (precision);
9984 else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
9985 return true;
9986 else
9987 wi_offset = wi::to_poly_wide (offset);
9989 wi::overflow_type overflow;
9990 poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
9991 precision);
9992 poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
9993 if (overflow)
9994 return true;
9996 poly_uint64 total_hwi, size;
9997 if (!total.to_uhwi (&total_hwi)
9998 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
9999 &size)
10000 || known_eq (size, 0U))
10001 return true;
10003 if (known_le (total_hwi, size))
10004 return false;
10006 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
10007 array. */
10008 if (TREE_CODE (base) == ADDR_EXPR
10009 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
10010 &size)
10011 && maybe_ne (size, 0U)
10012 && known_le (total_hwi, size))
10013 return false;
10015 return true;
10018 /* Return a positive integer when the symbol DECL is known to have
10019 a nonzero address, zero when it's known not to (e.g., it's a weak
10020 symbol), and a negative integer when the symbol is not yet in the
10021 symbol table and so whether or not its address is zero is unknown.
10022 For function local objects always return positive integer. */
10023 static int
10024 maybe_nonzero_address (tree decl)
10026 /* Normally, don't do anything for variables and functions before symtab is
10027 built; it is quite possible that DECL will be declared weak later.
10028 But if folding_initializer, we need a constant answer now, so create
10029 the symtab entry and prevent later weak declaration. */
10030 if (DECL_P (decl) && decl_in_symtab_p (decl))
10031 if (struct symtab_node *symbol
10032 = (folding_initializer
10033 ? symtab_node::get_create (decl)
10034 : symtab_node::get (decl)))
10035 return symbol->nonzero_address ();
10037 /* Function local objects are never NULL. */
10038 if (DECL_P (decl)
10039 && (DECL_CONTEXT (decl)
10040 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
10041 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
10042 return 1;
10044 return -1;
10047 /* Subroutine of fold_binary. This routine performs all of the
10048 transformations that are common to the equality/inequality
10049 operators (EQ_EXPR and NE_EXPR) and the ordering operators
10050 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
10051 fold_binary should call fold_binary. Fold a comparison with
10052 tree code CODE and type TYPE with operands OP0 and OP1. Return
10053 the folded comparison or NULL_TREE. */
10055 static tree
10056 fold_comparison (location_t loc, enum tree_code code, tree type,
10057 tree op0, tree op1)
10059 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
10060 tree arg0, arg1, tem;
10062 arg0 = op0;
10063 arg1 = op1;
10065 STRIP_SIGN_NOPS (arg0);
10066 STRIP_SIGN_NOPS (arg1);
10068 /* For comparisons of pointers we can decompose it to a compile time
10069 comparison of the base objects and the offsets into the object.
10070 This requires at least one operand being an ADDR_EXPR or a
10071 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
10072 if (POINTER_TYPE_P (TREE_TYPE (arg0))
10073 && (TREE_CODE (arg0) == ADDR_EXPR
10074 || TREE_CODE (arg1) == ADDR_EXPR
10075 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10076 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
10078 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
10079 poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
10080 machine_mode mode;
10081 int volatilep, reversep, unsignedp;
10082 bool indirect_base0 = false, indirect_base1 = false;
10084 /* Get base and offset for the access. Strip ADDR_EXPR for
10085 get_inner_reference, but put it back by stripping INDIRECT_REF
10086 off the base object if possible. indirect_baseN will be true
10087 if baseN is not an address but refers to the object itself. */
10088 base0 = arg0;
10089 if (TREE_CODE (arg0) == ADDR_EXPR)
10091 base0
10092 = get_inner_reference (TREE_OPERAND (arg0, 0),
10093 &bitsize, &bitpos0, &offset0, &mode,
10094 &unsignedp, &reversep, &volatilep);
10095 if (TREE_CODE (base0) == INDIRECT_REF)
10096 base0 = TREE_OPERAND (base0, 0);
10097 else
10098 indirect_base0 = true;
10100 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10102 base0 = TREE_OPERAND (arg0, 0);
10103 STRIP_SIGN_NOPS (base0);
10104 if (TREE_CODE (base0) == ADDR_EXPR)
10106 base0
10107 = get_inner_reference (TREE_OPERAND (base0, 0),
10108 &bitsize, &bitpos0, &offset0, &mode,
10109 &unsignedp, &reversep, &volatilep);
10110 if (TREE_CODE (base0) == INDIRECT_REF)
10111 base0 = TREE_OPERAND (base0, 0);
10112 else
10113 indirect_base0 = true;
10115 if (offset0 == NULL_TREE || integer_zerop (offset0))
10116 offset0 = TREE_OPERAND (arg0, 1);
10117 else
10118 offset0 = size_binop (PLUS_EXPR, offset0,
10119 TREE_OPERAND (arg0, 1));
10120 if (poly_int_tree_p (offset0))
10122 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
10123 TYPE_PRECISION (sizetype));
10124 tem <<= LOG2_BITS_PER_UNIT;
10125 tem += bitpos0;
10126 if (tem.to_shwi (&bitpos0))
10127 offset0 = NULL_TREE;
10131 base1 = arg1;
10132 if (TREE_CODE (arg1) == ADDR_EXPR)
10134 base1
10135 = get_inner_reference (TREE_OPERAND (arg1, 0),
10136 &bitsize, &bitpos1, &offset1, &mode,
10137 &unsignedp, &reversep, &volatilep);
10138 if (TREE_CODE (base1) == INDIRECT_REF)
10139 base1 = TREE_OPERAND (base1, 0);
10140 else
10141 indirect_base1 = true;
10143 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10145 base1 = TREE_OPERAND (arg1, 0);
10146 STRIP_SIGN_NOPS (base1);
10147 if (TREE_CODE (base1) == ADDR_EXPR)
10149 base1
10150 = get_inner_reference (TREE_OPERAND (base1, 0),
10151 &bitsize, &bitpos1, &offset1, &mode,
10152 &unsignedp, &reversep, &volatilep);
10153 if (TREE_CODE (base1) == INDIRECT_REF)
10154 base1 = TREE_OPERAND (base1, 0);
10155 else
10156 indirect_base1 = true;
10158 if (offset1 == NULL_TREE || integer_zerop (offset1))
10159 offset1 = TREE_OPERAND (arg1, 1);
10160 else
10161 offset1 = size_binop (PLUS_EXPR, offset1,
10162 TREE_OPERAND (arg1, 1));
10163 if (poly_int_tree_p (offset1))
10165 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
10166 TYPE_PRECISION (sizetype));
10167 tem <<= LOG2_BITS_PER_UNIT;
10168 tem += bitpos1;
10169 if (tem.to_shwi (&bitpos1))
10170 offset1 = NULL_TREE;
10174 /* If we have equivalent bases we might be able to simplify. */
10175 if (indirect_base0 == indirect_base1
10176 && operand_equal_p (base0, base1,
10177 indirect_base0 ? OEP_ADDRESS_OF : 0))
10179 /* We can fold this expression to a constant if the non-constant
10180 offset parts are equal. */
10181 if ((offset0 == offset1
10182 || (offset0 && offset1
10183 && operand_equal_p (offset0, offset1, 0)))
10184 && (equality_code
10185 || (indirect_base0
10186 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10187 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10189 if (!equality_code
10190 && maybe_ne (bitpos0, bitpos1)
10191 && (pointer_may_wrap_p (base0, offset0, bitpos0)
10192 || pointer_may_wrap_p (base1, offset1, bitpos1)))
10193 fold_overflow_warning (("assuming pointer wraparound does not "
10194 "occur when comparing P +- C1 with "
10195 "P +- C2"),
10196 WARN_STRICT_OVERFLOW_CONDITIONAL);
10198 switch (code)
10200 case EQ_EXPR:
10201 if (known_eq (bitpos0, bitpos1))
10202 return constant_boolean_node (true, type);
10203 if (known_ne (bitpos0, bitpos1))
10204 return constant_boolean_node (false, type);
10205 break;
10206 case NE_EXPR:
10207 if (known_ne (bitpos0, bitpos1))
10208 return constant_boolean_node (true, type);
10209 if (known_eq (bitpos0, bitpos1))
10210 return constant_boolean_node (false, type);
10211 break;
10212 case LT_EXPR:
10213 if (known_lt (bitpos0, bitpos1))
10214 return constant_boolean_node (true, type);
10215 if (known_ge (bitpos0, bitpos1))
10216 return constant_boolean_node (false, type);
10217 break;
10218 case LE_EXPR:
10219 if (known_le (bitpos0, bitpos1))
10220 return constant_boolean_node (true, type);
10221 if (known_gt (bitpos0, bitpos1))
10222 return constant_boolean_node (false, type);
10223 break;
10224 case GE_EXPR:
10225 if (known_ge (bitpos0, bitpos1))
10226 return constant_boolean_node (true, type);
10227 if (known_lt (bitpos0, bitpos1))
10228 return constant_boolean_node (false, type);
10229 break;
10230 case GT_EXPR:
10231 if (known_gt (bitpos0, bitpos1))
10232 return constant_boolean_node (true, type);
10233 if (known_le (bitpos0, bitpos1))
10234 return constant_boolean_node (false, type);
10235 break;
10236 default:;
10239 /* We can simplify the comparison to a comparison of the variable
10240 offset parts if the constant offset parts are equal.
10241 Be careful to use signed sizetype here because otherwise we
10242 mess with array offsets in the wrong way. This is possible
10243 because pointer arithmetic is restricted to retain within an
10244 object and overflow on pointer differences is undefined as of
10245 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10246 else if (known_eq (bitpos0, bitpos1)
10247 && (equality_code
10248 || (indirect_base0
10249 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10250 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10252 /* By converting to signed sizetype we cover middle-end pointer
10253 arithmetic which operates on unsigned pointer types of size
10254 type size and ARRAY_REF offsets which are properly sign or
10255 zero extended from their type in case it is narrower than
10256 sizetype. */
10257 if (offset0 == NULL_TREE)
10258 offset0 = build_int_cst (ssizetype, 0);
10259 else
10260 offset0 = fold_convert_loc (loc, ssizetype, offset0);
10261 if (offset1 == NULL_TREE)
10262 offset1 = build_int_cst (ssizetype, 0);
10263 else
10264 offset1 = fold_convert_loc (loc, ssizetype, offset1);
10266 if (!equality_code
10267 && (pointer_may_wrap_p (base0, offset0, bitpos0)
10268 || pointer_may_wrap_p (base1, offset1, bitpos1)))
10269 fold_overflow_warning (("assuming pointer wraparound does not "
10270 "occur when comparing P +- C1 with "
10271 "P +- C2"),
10272 WARN_STRICT_OVERFLOW_COMPARISON);
10274 return fold_build2_loc (loc, code, type, offset0, offset1);
10277 /* For equal offsets we can simplify to a comparison of the
10278 base addresses. */
10279 else if (known_eq (bitpos0, bitpos1)
10280 && (indirect_base0
10281 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10282 && (indirect_base1
10283 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10284 && ((offset0 == offset1)
10285 || (offset0 && offset1
10286 && operand_equal_p (offset0, offset1, 0))))
10288 if (indirect_base0)
10289 base0 = build_fold_addr_expr_loc (loc, base0);
10290 if (indirect_base1)
10291 base1 = build_fold_addr_expr_loc (loc, base1);
10292 return fold_build2_loc (loc, code, type, base0, base1);
10294 /* Comparison between an ordinary (non-weak) symbol and a null
10295 pointer can be eliminated since such symbols must have a non
10296 null address. In C, relational expressions between pointers
10297 to objects and null pointers are undefined. The results
10298 below follow the C++ rules with the additional property that
10299 every object pointer compares greater than a null pointer.
10301 else if (((DECL_P (base0)
10302 && maybe_nonzero_address (base0) > 0
10303 /* Avoid folding references to struct members at offset 0 to
10304 prevent tests like '&ptr->firstmember == 0' from getting
10305 eliminated. When ptr is null, although the -> expression
10306 is strictly speaking invalid, GCC retains it as a matter
10307 of QoI. See PR c/44555. */
10308 && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10309 || CONSTANT_CLASS_P (base0))
10310 && indirect_base0
10311 /* The caller guarantees that when one of the arguments is
10312 constant (i.e., null in this case) it is second. */
10313 && integer_zerop (arg1))
10315 switch (code)
10317 case EQ_EXPR:
10318 case LE_EXPR:
10319 case LT_EXPR:
10320 return constant_boolean_node (false, type);
10321 case GE_EXPR:
10322 case GT_EXPR:
10323 case NE_EXPR:
10324 return constant_boolean_node (true, type);
10325 default:
10326 gcc_unreachable ();
10331 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10332 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10333 the resulting offset is smaller in absolute value than the
10334 original one and has the same sign. */
10335 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10336 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10337 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10338 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10339 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10340 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10341 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10342 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10344 tree const1 = TREE_OPERAND (arg0, 1);
10345 tree const2 = TREE_OPERAND (arg1, 1);
10346 tree variable1 = TREE_OPERAND (arg0, 0);
10347 tree variable2 = TREE_OPERAND (arg1, 0);
10348 tree cst;
10349 const char * const warnmsg = G_("assuming signed overflow does not "
10350 "occur when combining constants around "
10351 "a comparison");
10353 /* Put the constant on the side where it doesn't overflow and is
10354 of lower absolute value and of same sign than before. */
10355 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10356 ? MINUS_EXPR : PLUS_EXPR,
10357 const2, const1);
10358 if (!TREE_OVERFLOW (cst)
10359 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10360 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10362 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10363 return fold_build2_loc (loc, code, type,
10364 variable1,
10365 fold_build2_loc (loc, TREE_CODE (arg1),
10366 TREE_TYPE (arg1),
10367 variable2, cst));
10370 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10371 ? MINUS_EXPR : PLUS_EXPR,
10372 const1, const2);
10373 if (!TREE_OVERFLOW (cst)
10374 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10375 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10377 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10378 return fold_build2_loc (loc, code, type,
10379 fold_build2_loc (loc, TREE_CODE (arg0),
10380 TREE_TYPE (arg0),
10381 variable1, cst),
10382 variable2);
10386 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10387 if (tem)
10388 return tem;
10390 /* If we are comparing an expression that just has comparisons
10391 of two integer values, arithmetic expressions of those comparisons,
10392 and constants, we can simplify it. There are only three cases
10393 to check: the two values can either be equal, the first can be
10394 greater, or the second can be greater. Fold the expression for
10395 those three values. Since each value must be 0 or 1, we have
10396 eight possibilities, each of which corresponds to the constant 0
10397 or 1 or one of the six possible comparisons.
10399 This handles common cases like (a > b) == 0 but also handles
10400 expressions like ((x > y) - (y > x)) > 0, which supposedly
10401 occur in macroized code. */
10403 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10405 tree cval1 = 0, cval2 = 0;
10407 if (twoval_comparison_p (arg0, &cval1, &cval2)
10408 /* Don't handle degenerate cases here; they should already
10409 have been handled anyway. */
10410 && cval1 != 0 && cval2 != 0
10411 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10412 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10413 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10414 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10415 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10416 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10417 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10419 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10420 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
10422 /* We can't just pass T to eval_subst in case cval1 or cval2
10423 was the same as ARG1. */
10425 tree high_result
10426 = fold_build2_loc (loc, code, type,
10427 eval_subst (loc, arg0, cval1, maxval,
10428 cval2, minval),
10429 arg1);
10430 tree equal_result
10431 = fold_build2_loc (loc, code, type,
10432 eval_subst (loc, arg0, cval1, maxval,
10433 cval2, maxval),
10434 arg1);
10435 tree low_result
10436 = fold_build2_loc (loc, code, type,
10437 eval_subst (loc, arg0, cval1, minval,
10438 cval2, maxval),
10439 arg1);
10441 /* All three of these results should be 0 or 1. Confirm they are.
10442 Then use those values to select the proper code to use. */
10444 if (TREE_CODE (high_result) == INTEGER_CST
10445 && TREE_CODE (equal_result) == INTEGER_CST
10446 && TREE_CODE (low_result) == INTEGER_CST)
10448 /* Make a 3-bit mask with the high-order bit being the
10449 value for `>', the next for '=', and the low for '<'. */
10450 switch ((integer_onep (high_result) * 4)
10451 + (integer_onep (equal_result) * 2)
10452 + integer_onep (low_result))
10454 case 0:
10455 /* Always false. */
10456 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10457 case 1:
10458 code = LT_EXPR;
10459 break;
10460 case 2:
10461 code = EQ_EXPR;
10462 break;
10463 case 3:
10464 code = LE_EXPR;
10465 break;
10466 case 4:
10467 code = GT_EXPR;
10468 break;
10469 case 5:
10470 code = NE_EXPR;
10471 break;
10472 case 6:
10473 code = GE_EXPR;
10474 break;
10475 case 7:
10476 /* Always true. */
10477 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10480 return fold_build2_loc (loc, code, type, cval1, cval2);
10485 return NULL_TREE;
10489 /* Subroutine of fold_binary. Optimize complex multiplications of the
10490 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10491 argument EXPR represents the expression "z" of type TYPE. */
10493 static tree
10494 fold_mult_zconjz (location_t loc, tree type, tree expr)
10496 tree itype = TREE_TYPE (type);
10497 tree rpart, ipart, tem;
10499 if (TREE_CODE (expr) == COMPLEX_EXPR)
10501 rpart = TREE_OPERAND (expr, 0);
10502 ipart = TREE_OPERAND (expr, 1);
10504 else if (TREE_CODE (expr) == COMPLEX_CST)
10506 rpart = TREE_REALPART (expr);
10507 ipart = TREE_IMAGPART (expr);
10509 else
10511 expr = save_expr (expr);
10512 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
10513 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
10516 rpart = save_expr (rpart);
10517 ipart = save_expr (ipart);
10518 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
10519 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
10520 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
10521 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
10522 build_zero_cst (itype));
10526 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10527 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10528 true if successful. */
10530 static bool
10531 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10533 unsigned HOST_WIDE_INT i, nunits;
10535 if (TREE_CODE (arg) == VECTOR_CST
10536 && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10538 for (i = 0; i < nunits; ++i)
10539 elts[i] = VECTOR_CST_ELT (arg, i);
10541 else if (TREE_CODE (arg) == CONSTRUCTOR)
10543 constructor_elt *elt;
10545 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10546 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10547 return false;
10548 else
10549 elts[i] = elt->value;
10551 else
10552 return false;
10553 for (; i < nelts; i++)
10554 elts[i]
10555 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
10556 return true;
10559 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10560 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10561 NULL_TREE otherwise. */
10563 tree
10564 fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10566 unsigned int i;
10567 unsigned HOST_WIDE_INT nelts;
10568 bool need_ctor = false;
10570 if (!sel.length ().is_constant (&nelts))
10571 return NULL_TREE;
10572 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), nelts)
10573 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)), nelts)
10574 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)), nelts));
10575 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10576 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10577 return NULL_TREE;
10579 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10580 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10581 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10582 return NULL_TREE;
10584 tree_vector_builder out_elts (type, nelts, 1);
10585 for (i = 0; i < nelts; i++)
10587 HOST_WIDE_INT index;
10588 if (!sel[i].is_constant (&index))
10589 return NULL_TREE;
10590 if (!CONSTANT_CLASS_P (in_elts[index]))
10591 need_ctor = true;
10592 out_elts.quick_push (unshare_expr (in_elts[index]));
10595 if (need_ctor)
10597 vec<constructor_elt, va_gc> *v;
10598 vec_alloc (v, nelts);
10599 for (i = 0; i < nelts; i++)
10600 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
10601 return build_constructor (type, v);
10603 else
10604 return out_elts.build ();
10607 /* Try to fold a pointer difference of type TYPE two address expressions of
10608 array references AREF0 and AREF1 using location LOC. Return a
10609 simplified expression for the difference or NULL_TREE. */
10611 static tree
10612 fold_addr_of_array_ref_difference (location_t loc, tree type,
10613 tree aref0, tree aref1,
10614 bool use_pointer_diff)
10616 tree base0 = TREE_OPERAND (aref0, 0);
10617 tree base1 = TREE_OPERAND (aref1, 0);
10618 tree base_offset = build_int_cst (type, 0);
10620 /* If the bases are array references as well, recurse. If the bases
10621 are pointer indirections compute the difference of the pointers.
10622 If the bases are equal, we are set. */
10623 if ((TREE_CODE (base0) == ARRAY_REF
10624 && TREE_CODE (base1) == ARRAY_REF
10625 && (base_offset
10626 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
10627 use_pointer_diff)))
10628 || (INDIRECT_REF_P (base0)
10629 && INDIRECT_REF_P (base1)
10630 && (base_offset
10631 = use_pointer_diff
10632 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
10633 TREE_OPERAND (base0, 0),
10634 TREE_OPERAND (base1, 0))
10635 : fold_binary_loc (loc, MINUS_EXPR, type,
10636 fold_convert (type,
10637 TREE_OPERAND (base0, 0)),
10638 fold_convert (type,
10639 TREE_OPERAND (base1, 0)))))
10640 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
10642 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10643 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10644 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
10645 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
10646 return fold_build2_loc (loc, PLUS_EXPR, type,
10647 base_offset,
10648 fold_build2_loc (loc, MULT_EXPR, type,
10649 diff, esz));
10651 return NULL_TREE;
10654 /* If the real or vector real constant CST of type TYPE has an exact
10655 inverse, return it, else return NULL. */
10657 tree
10658 exact_inverse (tree type, tree cst)
10660 REAL_VALUE_TYPE r;
10661 tree unit_type;
10662 machine_mode mode;
10664 switch (TREE_CODE (cst))
10666 case REAL_CST:
10667 r = TREE_REAL_CST (cst);
10669 if (exact_real_inverse (TYPE_MODE (type), &r))
10670 return build_real (type, r);
10672 return NULL_TREE;
10674 case VECTOR_CST:
10676 unit_type = TREE_TYPE (type);
10677 mode = TYPE_MODE (unit_type);
10679 tree_vector_builder elts;
10680 if (!elts.new_unary_operation (type, cst, false))
10681 return NULL_TREE;
10682 unsigned int count = elts.encoded_nelts ();
10683 for (unsigned int i = 0; i < count; ++i)
10685 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
10686 if (!exact_real_inverse (mode, &r))
10687 return NULL_TREE;
10688 elts.quick_push (build_real (unit_type, r));
10691 return elts.build ();
10694 default:
10695 return NULL_TREE;
10699 /* Mask out the tz least significant bits of X of type TYPE where
10700 tz is the number of trailing zeroes in Y. */
10701 static wide_int
10702 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10704 int tz = wi::ctz (y);
10705 if (tz > 0)
10706 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10707 return x;
10710 /* Return true when T is an address and is known to be nonzero.
10711 For floating point we further ensure that T is not denormal.
10712 Similar logic is present in nonzero_address in rtlanal.h.
10714 If the return value is based on the assumption that signed overflow
10715 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
10716 change *STRICT_OVERFLOW_P. */
10718 static bool
10719 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
10721 tree type = TREE_TYPE (t);
10722 enum tree_code code;
10724 /* Doing something useful for floating point would need more work. */
10725 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10726 return false;
10728 code = TREE_CODE (t);
10729 switch (TREE_CODE_CLASS (code))
10731 case tcc_unary:
10732 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10733 strict_overflow_p);
10734 case tcc_binary:
10735 case tcc_comparison:
10736 return tree_binary_nonzero_warnv_p (code, type,
10737 TREE_OPERAND (t, 0),
10738 TREE_OPERAND (t, 1),
10739 strict_overflow_p);
10740 case tcc_constant:
10741 case tcc_declaration:
10742 case tcc_reference:
10743 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10745 default:
10746 break;
10749 switch (code)
10751 case TRUTH_NOT_EXPR:
10752 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10753 strict_overflow_p);
10755 case TRUTH_AND_EXPR:
10756 case TRUTH_OR_EXPR:
10757 case TRUTH_XOR_EXPR:
10758 return tree_binary_nonzero_warnv_p (code, type,
10759 TREE_OPERAND (t, 0),
10760 TREE_OPERAND (t, 1),
10761 strict_overflow_p);
10763 case COND_EXPR:
10764 case CONSTRUCTOR:
10765 case OBJ_TYPE_REF:
10766 case ADDR_EXPR:
10767 case WITH_SIZE_EXPR:
10768 case SSA_NAME:
10769 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10771 case COMPOUND_EXPR:
10772 case MODIFY_EXPR:
10773 case BIND_EXPR:
10774 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
10775 strict_overflow_p);
10777 case SAVE_EXPR:
10778 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
10779 strict_overflow_p);
10781 case CALL_EXPR:
10783 tree fndecl = get_callee_fndecl (t);
10784 if (!fndecl) return false;
10785 if (flag_delete_null_pointer_checks && !flag_check_new
10786 && DECL_IS_OPERATOR_NEW_P (fndecl)
10787 && !TREE_NOTHROW (fndecl))
10788 return true;
10789 if (flag_delete_null_pointer_checks
10790 && lookup_attribute ("returns_nonnull",
10791 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10792 return true;
10793 return alloca_call_p (t);
10796 default:
10797 break;
10799 return false;
10802 /* Return true when T is an address and is known to be nonzero.
10803 Handle warnings about undefined signed overflow. */
10805 bool
10806 tree_expr_nonzero_p (tree t)
10808 bool ret, strict_overflow_p;
10810 strict_overflow_p = false;
10811 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
10812 if (strict_overflow_p)
10813 fold_overflow_warning (("assuming signed overflow does not occur when "
10814 "determining that expression is always "
10815 "non-zero"),
10816 WARN_STRICT_OVERFLOW_MISC);
10817 return ret;
10820 /* Return true if T is known not to be equal to an integer W. */
10822 bool
10823 expr_not_equal_to (tree t, const wide_int &w)
10825 int_range_max vr;
10826 switch (TREE_CODE (t))
10828 case INTEGER_CST:
10829 return wi::to_wide (t) != w;
10831 case SSA_NAME:
10832 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10833 return false;
10835 if (cfun)
10836 get_range_query (cfun)->range_of_expr (vr, t);
10837 else
10838 get_global_range_query ()->range_of_expr (vr, t);
10840 if (!vr.undefined_p ()
10841 && !vr.contains_p (wide_int_to_tree (TREE_TYPE (t), w)))
10842 return true;
10843 /* If T has some known zero bits and W has any of those bits set,
10844 then T is known not to be equal to W. */
10845 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10846 TYPE_PRECISION (TREE_TYPE (t))), 0))
10847 return true;
10848 return false;
10850 default:
10851 return false;
10855 /* Fold a binary expression of code CODE and type TYPE with operands
10856 OP0 and OP1. LOC is the location of the resulting expression.
10857 Return the folded expression if folding is successful. Otherwise,
10858 return NULL_TREE. */
10860 tree
10861 fold_binary_loc (location_t loc, enum tree_code code, tree type,
10862 tree op0, tree op1)
10864 enum tree_code_class kind = TREE_CODE_CLASS (code);
10865 tree arg0, arg1, tem;
10866 tree t1 = NULL_TREE;
10867 bool strict_overflow_p;
10868 unsigned int prec;
10870 gcc_assert (IS_EXPR_CODE_CLASS (kind)
10871 && TREE_CODE_LENGTH (code) == 2
10872 && op0 != NULL_TREE
10873 && op1 != NULL_TREE);
10875 arg0 = op0;
10876 arg1 = op1;
10878 /* Strip any conversions that don't change the mode. This is
10879 safe for every expression, except for a comparison expression
10880 because its signedness is derived from its operands. So, in
10881 the latter case, only strip conversions that don't change the
10882 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10883 preserved.
10885 Note that this is done as an internal manipulation within the
10886 constant folder, in order to find the simplest representation
10887 of the arguments so that their form can be studied. In any
10888 cases, the appropriate type conversions should be put back in
10889 the tree that will get out of the constant folder. */
10891 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10893 STRIP_SIGN_NOPS (arg0);
10894 STRIP_SIGN_NOPS (arg1);
10896 else
10898 STRIP_NOPS (arg0);
10899 STRIP_NOPS (arg1);
10902 /* Note that TREE_CONSTANT isn't enough: static var addresses are
10903 constant but we can't do arithmetic on them. */
10904 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
10906 tem = const_binop (code, type, arg0, arg1);
10907 if (tem != NULL_TREE)
10909 if (TREE_TYPE (tem) != type)
10910 tem = fold_convert_loc (loc, type, tem);
10911 return tem;
10915 /* If this is a commutative operation, and ARG0 is a constant, move it
10916 to ARG1 to reduce the number of tests below. */
10917 if (commutative_tree_code (code)
10918 && tree_swap_operands_p (arg0, arg1))
10919 return fold_build2_loc (loc, code, type, op1, op0);
10921 /* Likewise if this is a comparison, and ARG0 is a constant, move it
10922 to ARG1 to reduce the number of tests below. */
10923 if (kind == tcc_comparison
10924 && tree_swap_operands_p (arg0, arg1))
10925 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
10927 tem = generic_simplify (loc, code, type, op0, op1);
10928 if (tem)
10929 return tem;
10931 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10933 First check for cases where an arithmetic operation is applied to a
10934 compound, conditional, or comparison operation. Push the arithmetic
10935 operation inside the compound or conditional to see if any folding
10936 can then be done. Convert comparison to conditional for this purpose.
10937 The also optimizes non-constant cases that used to be done in
10938 expand_expr.
10940 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10941 one of the operands is a comparison and the other is a comparison, a
10942 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10943 code below would make the expression more complex. Change it to a
10944 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10945 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10947 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10948 || code == EQ_EXPR || code == NE_EXPR)
10949 && !VECTOR_TYPE_P (TREE_TYPE (arg0))
10950 && ((truth_value_p (TREE_CODE (arg0))
10951 && (truth_value_p (TREE_CODE (arg1))
10952 || (TREE_CODE (arg1) == BIT_AND_EXPR
10953 && integer_onep (TREE_OPERAND (arg1, 1)))))
10954 || (truth_value_p (TREE_CODE (arg1))
10955 && (truth_value_p (TREE_CODE (arg0))
10956 || (TREE_CODE (arg0) == BIT_AND_EXPR
10957 && integer_onep (TREE_OPERAND (arg0, 1)))))))
10959 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10960 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
10961 : TRUTH_XOR_EXPR,
10962 boolean_type_node,
10963 fold_convert_loc (loc, boolean_type_node, arg0),
10964 fold_convert_loc (loc, boolean_type_node, arg1));
10966 if (code == EQ_EXPR)
10967 tem = invert_truthvalue_loc (loc, tem);
10969 return fold_convert_loc (loc, type, tem);
10972 if (TREE_CODE_CLASS (code) == tcc_binary
10973 || TREE_CODE_CLASS (code) == tcc_comparison)
10975 if (TREE_CODE (arg0) == COMPOUND_EXPR)
10977 tem = fold_build2_loc (loc, code, type,
10978 fold_convert_loc (loc, TREE_TYPE (op0),
10979 TREE_OPERAND (arg0, 1)), op1);
10980 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
10981 tem);
10983 if (TREE_CODE (arg1) == COMPOUND_EXPR)
10985 tem = fold_build2_loc (loc, code, type, op0,
10986 fold_convert_loc (loc, TREE_TYPE (op1),
10987 TREE_OPERAND (arg1, 1)));
10988 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
10989 tem);
10992 if (TREE_CODE (arg0) == COND_EXPR
10993 || TREE_CODE (arg0) == VEC_COND_EXPR
10994 || COMPARISON_CLASS_P (arg0))
10996 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10997 arg0, arg1,
10998 /*cond_first_p=*/1);
10999 if (tem != NULL_TREE)
11000 return tem;
11003 if (TREE_CODE (arg1) == COND_EXPR
11004 || TREE_CODE (arg1) == VEC_COND_EXPR
11005 || COMPARISON_CLASS_P (arg1))
11007 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11008 arg1, arg0,
11009 /*cond_first_p=*/0);
11010 if (tem != NULL_TREE)
11011 return tem;
11015 switch (code)
11017 case MEM_REF:
11018 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11019 if (TREE_CODE (arg0) == ADDR_EXPR
11020 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
11022 tree iref = TREE_OPERAND (arg0, 0);
11023 return fold_build2 (MEM_REF, type,
11024 TREE_OPERAND (iref, 0),
11025 int_const_binop (PLUS_EXPR, arg1,
11026 TREE_OPERAND (iref, 1)));
11029 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
11030 if (TREE_CODE (arg0) == ADDR_EXPR
11031 && handled_component_p (TREE_OPERAND (arg0, 0)))
11033 tree base;
11034 poly_int64 coffset;
11035 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
11036 &coffset);
11037 if (!base)
11038 return NULL_TREE;
11039 return fold_build2 (MEM_REF, type,
11040 build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
11041 int_const_binop (PLUS_EXPR, arg1,
11042 size_int (coffset)));
11045 return NULL_TREE;
11047 case POINTER_PLUS_EXPR:
11048 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11049 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11050 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11051 return fold_convert_loc (loc, type,
11052 fold_build2_loc (loc, PLUS_EXPR, sizetype,
11053 fold_convert_loc (loc, sizetype,
11054 arg1),
11055 fold_convert_loc (loc, sizetype,
11056 arg0)));
11058 return NULL_TREE;
11060 case PLUS_EXPR:
11061 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11063 /* X + (X / CST) * -CST is X % CST. */
11064 if (TREE_CODE (arg1) == MULT_EXPR
11065 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11066 && operand_equal_p (arg0,
11067 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
11069 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
11070 tree cst1 = TREE_OPERAND (arg1, 1);
11071 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
11072 cst1, cst0);
11073 if (sum && integer_zerop (sum))
11074 return fold_convert_loc (loc, type,
11075 fold_build2_loc (loc, TRUNC_MOD_EXPR,
11076 TREE_TYPE (arg0), arg0,
11077 cst0));
11081 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11082 one. Make sure the type is not saturating and has the signedness of
11083 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11084 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11085 if ((TREE_CODE (arg0) == MULT_EXPR
11086 || TREE_CODE (arg1) == MULT_EXPR)
11087 && !TYPE_SATURATING (type)
11088 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11089 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11090 && (!FLOAT_TYPE_P (type) || flag_associative_math))
11092 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11093 if (tem)
11094 return tem;
11097 if (! FLOAT_TYPE_P (type))
11099 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11100 (plus (plus (mult) (mult)) (foo)) so that we can
11101 take advantage of the factoring cases below. */
11102 if (ANY_INTEGRAL_TYPE_P (type)
11103 && TYPE_OVERFLOW_WRAPS (type)
11104 && (((TREE_CODE (arg0) == PLUS_EXPR
11105 || TREE_CODE (arg0) == MINUS_EXPR)
11106 && TREE_CODE (arg1) == MULT_EXPR)
11107 || ((TREE_CODE (arg1) == PLUS_EXPR
11108 || TREE_CODE (arg1) == MINUS_EXPR)
11109 && TREE_CODE (arg0) == MULT_EXPR)))
11111 tree parg0, parg1, parg, marg;
11112 enum tree_code pcode;
11114 if (TREE_CODE (arg1) == MULT_EXPR)
11115 parg = arg0, marg = arg1;
11116 else
11117 parg = arg1, marg = arg0;
11118 pcode = TREE_CODE (parg);
11119 parg0 = TREE_OPERAND (parg, 0);
11120 parg1 = TREE_OPERAND (parg, 1);
11121 STRIP_NOPS (parg0);
11122 STRIP_NOPS (parg1);
11124 if (TREE_CODE (parg0) == MULT_EXPR
11125 && TREE_CODE (parg1) != MULT_EXPR)
11126 return fold_build2_loc (loc, pcode, type,
11127 fold_build2_loc (loc, PLUS_EXPR, type,
11128 fold_convert_loc (loc, type,
11129 parg0),
11130 fold_convert_loc (loc, type,
11131 marg)),
11132 fold_convert_loc (loc, type, parg1));
11133 if (TREE_CODE (parg0) != MULT_EXPR
11134 && TREE_CODE (parg1) == MULT_EXPR)
11135 return
11136 fold_build2_loc (loc, PLUS_EXPR, type,
11137 fold_convert_loc (loc, type, parg0),
11138 fold_build2_loc (loc, pcode, type,
11139 fold_convert_loc (loc, type, marg),
11140 fold_convert_loc (loc, type,
11141 parg1)));
11144 else
11146 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11147 to __complex__ ( x, y ). This is not the same for SNaNs or
11148 if signed zeros are involved. */
11149 if (!HONOR_SNANS (arg0)
11150 && !HONOR_SIGNED_ZEROS (arg0)
11151 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11153 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11154 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11155 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11156 bool arg0rz = false, arg0iz = false;
11157 if ((arg0r && (arg0rz = real_zerop (arg0r)))
11158 || (arg0i && (arg0iz = real_zerop (arg0i))))
11160 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11161 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11162 if (arg0rz && arg1i && real_zerop (arg1i))
11164 tree rp = arg1r ? arg1r
11165 : build1 (REALPART_EXPR, rtype, arg1);
11166 tree ip = arg0i ? arg0i
11167 : build1 (IMAGPART_EXPR, rtype, arg0);
11168 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11170 else if (arg0iz && arg1r && real_zerop (arg1r))
11172 tree rp = arg0r ? arg0r
11173 : build1 (REALPART_EXPR, rtype, arg0);
11174 tree ip = arg1i ? arg1i
11175 : build1 (IMAGPART_EXPR, rtype, arg1);
11176 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11181 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11182 We associate floats only if the user has specified
11183 -fassociative-math. */
11184 if (flag_associative_math
11185 && TREE_CODE (arg1) == PLUS_EXPR
11186 && TREE_CODE (arg0) != MULT_EXPR)
11188 tree tree10 = TREE_OPERAND (arg1, 0);
11189 tree tree11 = TREE_OPERAND (arg1, 1);
11190 if (TREE_CODE (tree11) == MULT_EXPR
11191 && TREE_CODE (tree10) == MULT_EXPR)
11193 tree tree0;
11194 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11195 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11198 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11199 We associate floats only if the user has specified
11200 -fassociative-math. */
11201 if (flag_associative_math
11202 && TREE_CODE (arg0) == PLUS_EXPR
11203 && TREE_CODE (arg1) != MULT_EXPR)
11205 tree tree00 = TREE_OPERAND (arg0, 0);
11206 tree tree01 = TREE_OPERAND (arg0, 1);
11207 if (TREE_CODE (tree01) == MULT_EXPR
11208 && TREE_CODE (tree00) == MULT_EXPR)
11210 tree tree0;
11211 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11212 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11217 bit_rotate:
11218 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11219 is a rotate of A by C1 bits. */
11220 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11221 is a rotate of A by B bits.
11222 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11223 though in this case CODE must be | and not + or ^, otherwise
11224 it doesn't return A when B is 0. */
11226 enum tree_code code0, code1;
11227 tree rtype;
11228 code0 = TREE_CODE (arg0);
11229 code1 = TREE_CODE (arg1);
11230 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11231 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11232 && operand_equal_p (TREE_OPERAND (arg0, 0),
11233 TREE_OPERAND (arg1, 0), 0)
11234 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11235 TYPE_UNSIGNED (rtype))
11236 /* Only create rotates in complete modes. Other cases are not
11237 expanded properly. */
11238 && (element_precision (rtype)
11239 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11241 tree tree01, tree11;
11242 tree orig_tree01, orig_tree11;
11243 enum tree_code code01, code11;
11245 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11246 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11247 STRIP_NOPS (tree01);
11248 STRIP_NOPS (tree11);
11249 code01 = TREE_CODE (tree01);
11250 code11 = TREE_CODE (tree11);
11251 if (code11 != MINUS_EXPR
11252 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11254 std::swap (code0, code1);
11255 std::swap (code01, code11);
11256 std::swap (tree01, tree11);
11257 std::swap (orig_tree01, orig_tree11);
11259 if (code01 == INTEGER_CST
11260 && code11 == INTEGER_CST
11261 && (wi::to_widest (tree01) + wi::to_widest (tree11)
11262 == element_precision (rtype)))
11264 tem = build2_loc (loc, LROTATE_EXPR,
11265 rtype, TREE_OPERAND (arg0, 0),
11266 code0 == LSHIFT_EXPR
11267 ? orig_tree01 : orig_tree11);
11268 return fold_convert_loc (loc, type, tem);
11270 else if (code11 == MINUS_EXPR)
11272 tree tree110, tree111;
11273 tree110 = TREE_OPERAND (tree11, 0);
11274 tree111 = TREE_OPERAND (tree11, 1);
11275 STRIP_NOPS (tree110);
11276 STRIP_NOPS (tree111);
11277 if (TREE_CODE (tree110) == INTEGER_CST
11278 && compare_tree_int (tree110,
11279 element_precision (rtype)) == 0
11280 && operand_equal_p (tree01, tree111, 0))
11282 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11283 ? LROTATE_EXPR : RROTATE_EXPR),
11284 rtype, TREE_OPERAND (arg0, 0),
11285 orig_tree01);
11286 return fold_convert_loc (loc, type, tem);
11289 else if (code == BIT_IOR_EXPR
11290 && code11 == BIT_AND_EXPR
11291 && pow2p_hwi (element_precision (rtype)))
11293 tree tree110, tree111;
11294 tree110 = TREE_OPERAND (tree11, 0);
11295 tree111 = TREE_OPERAND (tree11, 1);
11296 STRIP_NOPS (tree110);
11297 STRIP_NOPS (tree111);
11298 if (TREE_CODE (tree110) == NEGATE_EXPR
11299 && TREE_CODE (tree111) == INTEGER_CST
11300 && compare_tree_int (tree111,
11301 element_precision (rtype) - 1) == 0
11302 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11304 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11305 ? LROTATE_EXPR : RROTATE_EXPR),
11306 rtype, TREE_OPERAND (arg0, 0),
11307 orig_tree01);
11308 return fold_convert_loc (loc, type, tem);
11314 associate:
11315 /* In most languages, can't associate operations on floats through
11316 parentheses. Rather than remember where the parentheses were, we
11317 don't associate floats at all, unless the user has specified
11318 -fassociative-math.
11319 And, we need to make sure type is not saturating. */
11321 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11322 && !TYPE_SATURATING (type))
11324 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11325 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11326 tree atype = type;
11327 bool ok = true;
11329 /* Split both trees into variables, constants, and literals. Then
11330 associate each group together, the constants with literals,
11331 then the result with variables. This increases the chances of
11332 literals being recombined later and of generating relocatable
11333 expressions for the sum of a constant and literal. */
11334 var0 = split_tree (arg0, type, code,
11335 &minus_var0, &con0, &minus_con0,
11336 &lit0, &minus_lit0, 0);
11337 var1 = split_tree (arg1, type, code,
11338 &minus_var1, &con1, &minus_con1,
11339 &lit1, &minus_lit1, code == MINUS_EXPR);
11341 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11342 if (code == MINUS_EXPR)
11343 code = PLUS_EXPR;
11345 /* With undefined overflow prefer doing association in a type
11346 which wraps on overflow, if that is one of the operand types. */
11347 if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11348 && !TYPE_OVERFLOW_WRAPS (type))
11350 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11351 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11352 atype = TREE_TYPE (arg0);
11353 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11354 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11355 atype = TREE_TYPE (arg1);
11356 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11359 /* With undefined overflow we can only associate constants with one
11360 variable, and constants whose association doesn't overflow. */
11361 if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11362 && !TYPE_OVERFLOW_WRAPS (atype))
11364 if ((var0 && var1) || (minus_var0 && minus_var1))
11366 /* ??? If split_tree would handle NEGATE_EXPR we could
11367 simply reject these cases and the allowed cases would
11368 be the var0/minus_var1 ones. */
11369 tree tmp0 = var0 ? var0 : minus_var0;
11370 tree tmp1 = var1 ? var1 : minus_var1;
11371 bool one_neg = false;
11373 if (TREE_CODE (tmp0) == NEGATE_EXPR)
11375 tmp0 = TREE_OPERAND (tmp0, 0);
11376 one_neg = !one_neg;
11378 if (CONVERT_EXPR_P (tmp0)
11379 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11380 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11381 <= TYPE_PRECISION (atype)))
11382 tmp0 = TREE_OPERAND (tmp0, 0);
11383 if (TREE_CODE (tmp1) == NEGATE_EXPR)
11385 tmp1 = TREE_OPERAND (tmp1, 0);
11386 one_neg = !one_neg;
11388 if (CONVERT_EXPR_P (tmp1)
11389 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11390 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11391 <= TYPE_PRECISION (atype)))
11392 tmp1 = TREE_OPERAND (tmp1, 0);
11393 /* The only case we can still associate with two variables
11394 is if they cancel out. */
11395 if (!one_neg
11396 || !operand_equal_p (tmp0, tmp1, 0))
11397 ok = false;
11399 else if ((var0 && minus_var1
11400 && ! operand_equal_p (var0, minus_var1, 0))
11401 || (minus_var0 && var1
11402 && ! operand_equal_p (minus_var0, var1, 0)))
11403 ok = false;
11406 /* Only do something if we found more than two objects. Otherwise,
11407 nothing has changed and we risk infinite recursion. */
11408 if (ok
11409 && ((var0 != 0) + (var1 != 0)
11410 + (minus_var0 != 0) + (minus_var1 != 0)
11411 + (con0 != 0) + (con1 != 0)
11412 + (minus_con0 != 0) + (minus_con1 != 0)
11413 + (lit0 != 0) + (lit1 != 0)
11414 + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11416 var0 = associate_trees (loc, var0, var1, code, atype);
11417 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11418 code, atype);
11419 con0 = associate_trees (loc, con0, con1, code, atype);
11420 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11421 code, atype);
11422 lit0 = associate_trees (loc, lit0, lit1, code, atype);
11423 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11424 code, atype);
11426 if (minus_var0 && var0)
11428 var0 = associate_trees (loc, var0, minus_var0,
11429 MINUS_EXPR, atype);
11430 minus_var0 = 0;
11432 if (minus_con0 && con0)
11434 con0 = associate_trees (loc, con0, minus_con0,
11435 MINUS_EXPR, atype);
11436 minus_con0 = 0;
11439 /* Preserve the MINUS_EXPR if the negative part of the literal is
11440 greater than the positive part. Otherwise, the multiplicative
11441 folding code (i.e extract_muldiv) may be fooled in case
11442 unsigned constants are subtracted, like in the following
11443 example: ((X*2 + 4) - 8U)/2. */
11444 if (minus_lit0 && lit0)
11446 if (TREE_CODE (lit0) == INTEGER_CST
11447 && TREE_CODE (minus_lit0) == INTEGER_CST
11448 && tree_int_cst_lt (lit0, minus_lit0)
11449 /* But avoid ending up with only negated parts. */
11450 && (var0 || con0))
11452 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11453 MINUS_EXPR, atype);
11454 lit0 = 0;
11456 else
11458 lit0 = associate_trees (loc, lit0, minus_lit0,
11459 MINUS_EXPR, atype);
11460 minus_lit0 = 0;
11464 /* Don't introduce overflows through reassociation. */
11465 if ((lit0 && TREE_OVERFLOW_P (lit0))
11466 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11467 return NULL_TREE;
11469 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11470 con0 = associate_trees (loc, con0, lit0, code, atype);
11471 lit0 = 0;
11472 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11473 code, atype);
11474 minus_lit0 = 0;
11476 /* Eliminate minus_con0. */
11477 if (minus_con0)
11479 if (con0)
11480 con0 = associate_trees (loc, con0, minus_con0,
11481 MINUS_EXPR, atype);
11482 else if (var0)
11483 var0 = associate_trees (loc, var0, minus_con0,
11484 MINUS_EXPR, atype);
11485 else
11486 gcc_unreachable ();
11487 minus_con0 = 0;
11490 /* Eliminate minus_var0. */
11491 if (minus_var0)
11493 if (con0)
11494 con0 = associate_trees (loc, con0, minus_var0,
11495 MINUS_EXPR, atype);
11496 else
11497 gcc_unreachable ();
11498 minus_var0 = 0;
11501 return
11502 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11503 code, atype));
11507 return NULL_TREE;
11509 case POINTER_DIFF_EXPR:
11510 case MINUS_EXPR:
11511 /* Fold &a[i] - &a[j] to i-j. */
11512 if (TREE_CODE (arg0) == ADDR_EXPR
11513 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11514 && TREE_CODE (arg1) == ADDR_EXPR
11515 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11517 tree tem = fold_addr_of_array_ref_difference (loc, type,
11518 TREE_OPERAND (arg0, 0),
11519 TREE_OPERAND (arg1, 0),
11520 code
11521 == POINTER_DIFF_EXPR);
11522 if (tem)
11523 return tem;
11526 /* Further transformations are not for pointers. */
11527 if (code == POINTER_DIFF_EXPR)
11528 return NULL_TREE;
11530 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11531 if (TREE_CODE (arg0) == NEGATE_EXPR
11532 && negate_expr_p (op1)
11533 /* If arg0 is e.g. unsigned int and type is int, then this could
11534 introduce UB, because if A is INT_MIN at runtime, the original
11535 expression can be well defined while the latter is not.
11536 See PR83269. */
11537 && !(ANY_INTEGRAL_TYPE_P (type)
11538 && TYPE_OVERFLOW_UNDEFINED (type)
11539 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11540 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11541 return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11542 fold_convert_loc (loc, type,
11543 TREE_OPERAND (arg0, 0)));
11545 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11546 __complex__ ( x, -y ). This is not the same for SNaNs or if
11547 signed zeros are involved. */
11548 if (!HONOR_SNANS (arg0)
11549 && !HONOR_SIGNED_ZEROS (arg0)
11550 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11552 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11553 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11554 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11555 bool arg0rz = false, arg0iz = false;
11556 if ((arg0r && (arg0rz = real_zerop (arg0r)))
11557 || (arg0i && (arg0iz = real_zerop (arg0i))))
11559 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11560 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11561 if (arg0rz && arg1i && real_zerop (arg1i))
11563 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11564 arg1r ? arg1r
11565 : build1 (REALPART_EXPR, rtype, arg1));
11566 tree ip = arg0i ? arg0i
11567 : build1 (IMAGPART_EXPR, rtype, arg0);
11568 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11570 else if (arg0iz && arg1r && real_zerop (arg1r))
11572 tree rp = arg0r ? arg0r
11573 : build1 (REALPART_EXPR, rtype, arg0);
11574 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11575 arg1i ? arg1i
11576 : build1 (IMAGPART_EXPR, rtype, arg1));
11577 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11582 /* A - B -> A + (-B) if B is easily negatable. */
11583 if (negate_expr_p (op1)
11584 && ! TYPE_OVERFLOW_SANITIZED (type)
11585 && ((FLOAT_TYPE_P (type)
11586 /* Avoid this transformation if B is a positive REAL_CST. */
11587 && (TREE_CODE (op1) != REAL_CST
11588 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11589 || INTEGRAL_TYPE_P (type)))
11590 return fold_build2_loc (loc, PLUS_EXPR, type,
11591 fold_convert_loc (loc, type, arg0),
11592 negate_expr (op1));
11594 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11595 one. Make sure the type is not saturating and has the signedness of
11596 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11597 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11598 if ((TREE_CODE (arg0) == MULT_EXPR
11599 || TREE_CODE (arg1) == MULT_EXPR)
11600 && !TYPE_SATURATING (type)
11601 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11602 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11603 && (!FLOAT_TYPE_P (type) || flag_associative_math))
11605 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11606 if (tem)
11607 return tem;
11610 goto associate;
11612 case MULT_EXPR:
11613 if (! FLOAT_TYPE_P (type))
11615 /* Transform x * -C into -x * C if x is easily negatable. */
11616 if (TREE_CODE (op1) == INTEGER_CST
11617 && tree_int_cst_sgn (op1) == -1
11618 && negate_expr_p (op0)
11619 && negate_expr_p (op1)
11620 && (tem = negate_expr (op1)) != op1
11621 && ! TREE_OVERFLOW (tem))
11622 return fold_build2_loc (loc, MULT_EXPR, type,
11623 fold_convert_loc (loc, type,
11624 negate_expr (op0)), tem);
11626 strict_overflow_p = false;
11627 if (TREE_CODE (arg1) == INTEGER_CST
11628 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11629 &strict_overflow_p)) != 0)
11631 if (strict_overflow_p)
11632 fold_overflow_warning (("assuming signed overflow does not "
11633 "occur when simplifying "
11634 "multiplication"),
11635 WARN_STRICT_OVERFLOW_MISC);
11636 return fold_convert_loc (loc, type, tem);
11639 /* Optimize z * conj(z) for integer complex numbers. */
11640 if (TREE_CODE (arg0) == CONJ_EXPR
11641 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11642 return fold_mult_zconjz (loc, type, arg1);
11643 if (TREE_CODE (arg1) == CONJ_EXPR
11644 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11645 return fold_mult_zconjz (loc, type, arg0);
11647 else
11649 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11650 This is not the same for NaNs or if signed zeros are
11651 involved. */
11652 if (!HONOR_NANS (arg0)
11653 && !HONOR_SIGNED_ZEROS (arg0)
11654 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11655 && TREE_CODE (arg1) == COMPLEX_CST
11656 && real_zerop (TREE_REALPART (arg1)))
11658 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11659 if (real_onep (TREE_IMAGPART (arg1)))
11660 return
11661 fold_build2_loc (loc, COMPLEX_EXPR, type,
11662 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
11663 rtype, arg0)),
11664 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
11665 else if (real_minus_onep (TREE_IMAGPART (arg1)))
11666 return
11667 fold_build2_loc (loc, COMPLEX_EXPR, type,
11668 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
11669 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
11670 rtype, arg0)));
11673 /* Optimize z * conj(z) for floating point complex numbers.
11674 Guarded by flag_unsafe_math_optimizations as non-finite
11675 imaginary components don't produce scalar results. */
11676 if (flag_unsafe_math_optimizations
11677 && TREE_CODE (arg0) == CONJ_EXPR
11678 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11679 return fold_mult_zconjz (loc, type, arg1);
11680 if (flag_unsafe_math_optimizations
11681 && TREE_CODE (arg1) == CONJ_EXPR
11682 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11683 return fold_mult_zconjz (loc, type, arg0);
11685 goto associate;
11687 case BIT_IOR_EXPR:
11688 /* Canonicalize (X & C1) | C2. */
11689 if (TREE_CODE (arg0) == BIT_AND_EXPR
11690 && TREE_CODE (arg1) == INTEGER_CST
11691 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11693 int width = TYPE_PRECISION (type), w;
11694 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11695 wide_int c2 = wi::to_wide (arg1);
11697 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11698 if ((c1 & c2) == c1)
11699 return omit_one_operand_loc (loc, type, arg1,
11700 TREE_OPERAND (arg0, 0));
11702 wide_int msk = wi::mask (width, false,
11703 TYPE_PRECISION (TREE_TYPE (arg1)));
11705 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11706 if (wi::bit_and_not (msk, c1 | c2) == 0)
11708 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11709 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11712 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11713 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11714 mode which allows further optimizations. */
11715 c1 &= msk;
11716 c2 &= msk;
11717 wide_int c3 = wi::bit_and_not (c1, c2);
11718 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11720 wide_int mask = wi::mask (w, false,
11721 TYPE_PRECISION (type));
11722 if (((c1 | c2) & mask) == mask
11723 && wi::bit_and_not (c1, mask) == 0)
11725 c3 = mask;
11726 break;
11730 if (c3 != c1)
11732 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11733 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11734 wide_int_to_tree (type, c3));
11735 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11739 /* See if this can be simplified into a rotate first. If that
11740 is unsuccessful continue in the association code. */
11741 goto bit_rotate;
11743 case BIT_XOR_EXPR:
11744 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11745 if (TREE_CODE (arg0) == BIT_AND_EXPR
11746 && INTEGRAL_TYPE_P (type)
11747 && integer_onep (TREE_OPERAND (arg0, 1))
11748 && integer_onep (arg1))
11749 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11750 build_zero_cst (TREE_TYPE (arg0)));
11752 /* See if this can be simplified into a rotate first. If that
11753 is unsuccessful continue in the association code. */
11754 goto bit_rotate;
11756 case BIT_AND_EXPR:
11757 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11758 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11759 && INTEGRAL_TYPE_P (type)
11760 && integer_onep (TREE_OPERAND (arg0, 1))
11761 && integer_onep (arg1))
11763 tree tem2;
11764 tem = TREE_OPERAND (arg0, 0);
11765 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
11766 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
11767 tem, tem2);
11768 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
11769 build_zero_cst (TREE_TYPE (tem)));
11771 /* Fold ~X & 1 as (X & 1) == 0. */
11772 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11773 && INTEGRAL_TYPE_P (type)
11774 && integer_onep (arg1))
11776 tree tem2;
11777 tem = TREE_OPERAND (arg0, 0);
11778 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
11779 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
11780 tem, tem2);
11781 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
11782 build_zero_cst (TREE_TYPE (tem)));
11784 /* Fold !X & 1 as X == 0. */
11785 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11786 && integer_onep (arg1))
11788 tem = TREE_OPERAND (arg0, 0);
11789 return fold_build2_loc (loc, EQ_EXPR, type, tem,
11790 build_zero_cst (TREE_TYPE (tem)));
11793 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11794 multiple of 1 << CST. */
11795 if (TREE_CODE (arg1) == INTEGER_CST)
11797 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11798 wide_int ncst1 = -cst1;
11799 if ((cst1 & ncst1) == ncst1
11800 && multiple_of_p (type, arg0,
11801 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11802 return fold_convert_loc (loc, type, arg0);
11805 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11806 bits from CST2. */
11807 if (TREE_CODE (arg1) == INTEGER_CST
11808 && TREE_CODE (arg0) == MULT_EXPR
11809 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11811 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11812 wide_int masked
11813 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11815 if (masked == 0)
11816 return omit_two_operands_loc (loc, type, build_zero_cst (type),
11817 arg0, arg1);
11818 else if (masked != warg1)
11820 /* Avoid the transform if arg1 is a mask of some
11821 mode which allows further optimizations. */
11822 int pop = wi::popcount (warg1);
11823 if (!(pop >= BITS_PER_UNIT
11824 && pow2p_hwi (pop)
11825 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11826 return fold_build2_loc (loc, code, type, op0,
11827 wide_int_to_tree (type, masked));
11831 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11832 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11833 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11835 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11837 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11838 if (mask == -1)
11839 return
11840 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11843 goto associate;
11845 case RDIV_EXPR:
11846 /* Don't touch a floating-point divide by zero unless the mode
11847 of the constant can represent infinity. */
11848 if (TREE_CODE (arg1) == REAL_CST
11849 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11850 && real_zerop (arg1))
11851 return NULL_TREE;
11853 /* (-A) / (-B) -> A / B */
11854 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11855 return fold_build2_loc (loc, RDIV_EXPR, type,
11856 TREE_OPERAND (arg0, 0),
11857 negate_expr (arg1));
11858 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11859 return fold_build2_loc (loc, RDIV_EXPR, type,
11860 negate_expr (arg0),
11861 TREE_OPERAND (arg1, 0));
11862 return NULL_TREE;
11864 case TRUNC_DIV_EXPR:
11865 /* Fall through */
11867 case FLOOR_DIV_EXPR:
11868 /* Simplify A / (B << N) where A and B are positive and B is
11869 a power of 2, to A >> (N + log2(B)). */
11870 strict_overflow_p = false;
11871 if (TREE_CODE (arg1) == LSHIFT_EXPR
11872 && (TYPE_UNSIGNED (type)
11873 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11875 tree sval = TREE_OPERAND (arg1, 0);
11876 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11878 tree sh_cnt = TREE_OPERAND (arg1, 1);
11879 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
11880 wi::exact_log2 (wi::to_wide (sval)));
11882 if (strict_overflow_p)
11883 fold_overflow_warning (("assuming signed overflow does not "
11884 "occur when simplifying A / (B << N)"),
11885 WARN_STRICT_OVERFLOW_MISC);
11887 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11888 sh_cnt, pow2);
11889 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11890 fold_convert_loc (loc, type, arg0), sh_cnt);
11894 /* Fall through */
11896 case ROUND_DIV_EXPR:
11897 case CEIL_DIV_EXPR:
11898 case EXACT_DIV_EXPR:
11899 if (integer_zerop (arg1))
11900 return NULL_TREE;
11902 /* Convert -A / -B to A / B when the type is signed and overflow is
11903 undefined. */
11904 if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11905 && TREE_CODE (op0) == NEGATE_EXPR
11906 && negate_expr_p (op1))
11908 if (ANY_INTEGRAL_TYPE_P (type))
11909 fold_overflow_warning (("assuming signed overflow does not occur "
11910 "when distributing negation across "
11911 "division"),
11912 WARN_STRICT_OVERFLOW_MISC);
11913 return fold_build2_loc (loc, code, type,
11914 fold_convert_loc (loc, type,
11915 TREE_OPERAND (arg0, 0)),
11916 negate_expr (op1));
11918 if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11919 && TREE_CODE (arg1) == NEGATE_EXPR
11920 && negate_expr_p (op0))
11922 if (ANY_INTEGRAL_TYPE_P (type))
11923 fold_overflow_warning (("assuming signed overflow does not occur "
11924 "when distributing negation across "
11925 "division"),
11926 WARN_STRICT_OVERFLOW_MISC);
11927 return fold_build2_loc (loc, code, type,
11928 negate_expr (op0),
11929 fold_convert_loc (loc, type,
11930 TREE_OPERAND (arg1, 0)));
11933 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11934 operation, EXACT_DIV_EXPR.
11936 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11937 At one time others generated faster code, it's not clear if they do
11938 after the last round to changes to the DIV code in expmed.cc. */
11939 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11940 && multiple_of_p (type, arg0, arg1))
11941 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
11942 fold_convert (type, arg0),
11943 fold_convert (type, arg1));
11945 strict_overflow_p = false;
11946 if (TREE_CODE (arg1) == INTEGER_CST
11947 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11948 &strict_overflow_p)) != 0)
11950 if (strict_overflow_p)
11951 fold_overflow_warning (("assuming signed overflow does not occur "
11952 "when simplifying division"),
11953 WARN_STRICT_OVERFLOW_MISC);
11954 return fold_convert_loc (loc, type, tem);
11957 return NULL_TREE;
11959 case CEIL_MOD_EXPR:
11960 case FLOOR_MOD_EXPR:
11961 case ROUND_MOD_EXPR:
11962 case TRUNC_MOD_EXPR:
11963 strict_overflow_p = false;
11964 if (TREE_CODE (arg1) == INTEGER_CST
11965 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11966 &strict_overflow_p)) != 0)
11968 if (strict_overflow_p)
11969 fold_overflow_warning (("assuming signed overflow does not occur "
11970 "when simplifying modulus"),
11971 WARN_STRICT_OVERFLOW_MISC);
11972 return fold_convert_loc (loc, type, tem);
11975 return NULL_TREE;
11977 case LROTATE_EXPR:
11978 case RROTATE_EXPR:
11979 case RSHIFT_EXPR:
11980 case LSHIFT_EXPR:
11981 /* Since negative shift count is not well-defined,
11982 don't try to compute it in the compiler. */
11983 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11984 return NULL_TREE;
11986 prec = element_precision (type);
11988 /* If we have a rotate of a bit operation with the rotate count and
11989 the second operand of the bit operation both constant,
11990 permute the two operations. */
11991 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11992 && (TREE_CODE (arg0) == BIT_AND_EXPR
11993 || TREE_CODE (arg0) == BIT_IOR_EXPR
11994 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11995 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11997 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11998 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11999 return fold_build2_loc (loc, TREE_CODE (arg0), type,
12000 fold_build2_loc (loc, code, type,
12001 arg00, arg1),
12002 fold_build2_loc (loc, code, type,
12003 arg01, arg1));
12006 /* Two consecutive rotates adding up to the some integer
12007 multiple of the precision of the type can be ignored. */
12008 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12009 && TREE_CODE (arg0) == RROTATE_EXPR
12010 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12011 && wi::umod_trunc (wi::to_wide (arg1)
12012 + wi::to_wide (TREE_OPERAND (arg0, 1)),
12013 prec) == 0)
12014 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12016 return NULL_TREE;
12018 case MIN_EXPR:
12019 case MAX_EXPR:
12020 goto associate;
12022 case TRUTH_ANDIF_EXPR:
12023 /* Note that the operands of this must be ints
12024 and their values must be 0 or 1.
12025 ("true" is a fixed value perhaps depending on the language.) */
12026 /* If first arg is constant zero, return it. */
12027 if (integer_zerop (arg0))
12028 return fold_convert_loc (loc, type, arg0);
12029 /* FALLTHRU */
12030 case TRUTH_AND_EXPR:
12031 /* If either arg is constant true, drop it. */
12032 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12033 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12034 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12035 /* Preserve sequence points. */
12036 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12037 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12038 /* If second arg is constant zero, result is zero, but first arg
12039 must be evaluated. */
12040 if (integer_zerop (arg1))
12041 return omit_one_operand_loc (loc, type, arg1, arg0);
12042 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12043 case will be handled here. */
12044 if (integer_zerop (arg0))
12045 return omit_one_operand_loc (loc, type, arg0, arg1);
12047 /* !X && X is always false. */
12048 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12049 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12050 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12051 /* X && !X is always false. */
12052 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12053 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12054 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12056 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12057 means A >= Y && A != MAX, but in this case we know that
12058 A < X <= MAX. */
12060 if (!TREE_SIDE_EFFECTS (arg0)
12061 && !TREE_SIDE_EFFECTS (arg1))
12063 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12064 if (tem && !operand_equal_p (tem, arg0, 0))
12065 return fold_convert (type,
12066 fold_build2_loc (loc, code, TREE_TYPE (arg1),
12067 tem, arg1));
12069 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12070 if (tem && !operand_equal_p (tem, arg1, 0))
12071 return fold_convert (type,
12072 fold_build2_loc (loc, code, TREE_TYPE (arg0),
12073 arg0, tem));
12076 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12077 != NULL_TREE)
12078 return tem;
12080 return NULL_TREE;
12082 case TRUTH_ORIF_EXPR:
12083 /* Note that the operands of this must be ints
12084 and their values must be 0 or true.
12085 ("true" is a fixed value perhaps depending on the language.) */
12086 /* If first arg is constant true, return it. */
12087 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12088 return fold_convert_loc (loc, type, arg0);
12089 /* FALLTHRU */
12090 case TRUTH_OR_EXPR:
12091 /* If either arg is constant zero, drop it. */
12092 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12093 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12094 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12095 /* Preserve sequence points. */
12096 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12097 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12098 /* If second arg is constant true, result is true, but we must
12099 evaluate first arg. */
12100 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12101 return omit_one_operand_loc (loc, type, arg1, arg0);
12102 /* Likewise for first arg, but note this only occurs here for
12103 TRUTH_OR_EXPR. */
12104 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12105 return omit_one_operand_loc (loc, type, arg0, arg1);
12107 /* !X || X is always true. */
12108 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12109 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12110 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12111 /* X || !X is always true. */
12112 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12113 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12114 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12116 /* (X && !Y) || (!X && Y) is X ^ Y */
12117 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12118 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12120 tree a0, a1, l0, l1, n0, n1;
12122 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12123 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12125 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12126 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12128 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12129 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12131 if ((operand_equal_p (n0, a0, 0)
12132 && operand_equal_p (n1, a1, 0))
12133 || (operand_equal_p (n0, a1, 0)
12134 && operand_equal_p (n1, a0, 0)))
12135 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12138 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12139 != NULL_TREE)
12140 return tem;
12142 return NULL_TREE;
12144 case TRUTH_XOR_EXPR:
12145 /* If the second arg is constant zero, drop it. */
12146 if (integer_zerop (arg1))
12147 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12148 /* If the second arg is constant true, this is a logical inversion. */
12149 if (integer_onep (arg1))
12151 tem = invert_truthvalue_loc (loc, arg0);
12152 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12154 /* Identical arguments cancel to zero. */
12155 if (operand_equal_p (arg0, arg1, 0))
12156 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12158 /* !X ^ X is always true. */
12159 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12160 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12161 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12163 /* X ^ !X is always true. */
12164 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12165 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12166 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12168 return NULL_TREE;
12170 case EQ_EXPR:
12171 case NE_EXPR:
12172 STRIP_NOPS (arg0);
12173 STRIP_NOPS (arg1);
12175 tem = fold_comparison (loc, code, type, op0, op1);
12176 if (tem != NULL_TREE)
12177 return tem;
12179 /* bool_var != 1 becomes !bool_var. */
12180 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12181 && code == NE_EXPR)
12182 return fold_convert_loc (loc, type,
12183 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12184 TREE_TYPE (arg0), arg0));
12186 /* bool_var == 0 becomes !bool_var. */
12187 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12188 && code == EQ_EXPR)
12189 return fold_convert_loc (loc, type,
12190 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12191 TREE_TYPE (arg0), arg0));
12193 /* !exp != 0 becomes !exp */
12194 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12195 && code == NE_EXPR)
12196 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12198 /* If this is an EQ or NE comparison with zero and ARG0 is
12199 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12200 two operations, but the latter can be done in one less insn
12201 on machines that have only two-operand insns or on which a
12202 constant cannot be the first operand. */
12203 if (TREE_CODE (arg0) == BIT_AND_EXPR
12204 && integer_zerop (arg1))
12206 tree arg00 = TREE_OPERAND (arg0, 0);
12207 tree arg01 = TREE_OPERAND (arg0, 1);
12208 if (TREE_CODE (arg00) == LSHIFT_EXPR
12209 && integer_onep (TREE_OPERAND (arg00, 0)))
12211 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12212 arg01, TREE_OPERAND (arg00, 1));
12213 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12214 build_one_cst (TREE_TYPE (arg0)));
12215 return fold_build2_loc (loc, code, type,
12216 fold_convert_loc (loc, TREE_TYPE (arg1),
12217 tem), arg1);
12219 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12220 && integer_onep (TREE_OPERAND (arg01, 0)))
12222 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12223 arg00, TREE_OPERAND (arg01, 1));
12224 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12225 build_one_cst (TREE_TYPE (arg0)));
12226 return fold_build2_loc (loc, code, type,
12227 fold_convert_loc (loc, TREE_TYPE (arg1),
12228 tem), arg1);
12232 /* If this is a comparison of a field, we may be able to simplify it. */
12233 if ((TREE_CODE (arg0) == COMPONENT_REF
12234 || TREE_CODE (arg0) == BIT_FIELD_REF)
12235 /* Handle the constant case even without -O
12236 to make sure the warnings are given. */
12237 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12239 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12240 if (t1)
12241 return t1;
12244 /* Optimize comparisons of strlen vs zero to a compare of the
12245 first character of the string vs zero. To wit,
12246 strlen(ptr) == 0 => *ptr == 0
12247 strlen(ptr) != 0 => *ptr != 0
12248 Other cases should reduce to one of these two (or a constant)
12249 due to the return value of strlen being unsigned. */
12250 if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12252 tree fndecl = get_callee_fndecl (arg0);
12254 if (fndecl
12255 && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12256 && call_expr_nargs (arg0) == 1
12257 && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12258 == POINTER_TYPE))
12260 tree ptrtype
12261 = build_pointer_type (build_qualified_type (char_type_node,
12262 TYPE_QUAL_CONST));
12263 tree ptr = fold_convert_loc (loc, ptrtype,
12264 CALL_EXPR_ARG (arg0, 0));
12265 tree iref = build_fold_indirect_ref_loc (loc, ptr);
12266 return fold_build2_loc (loc, code, type, iref,
12267 build_int_cst (TREE_TYPE (iref), 0));
12271 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12272 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12273 if (TREE_CODE (arg0) == RSHIFT_EXPR
12274 && integer_zerop (arg1)
12275 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12277 tree arg00 = TREE_OPERAND (arg0, 0);
12278 tree arg01 = TREE_OPERAND (arg0, 1);
12279 tree itype = TREE_TYPE (arg00);
12280 if (wi::to_wide (arg01) == element_precision (itype) - 1)
12282 if (TYPE_UNSIGNED (itype))
12284 itype = signed_type_for (itype);
12285 arg00 = fold_convert_loc (loc, itype, arg00);
12287 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12288 type, arg00, build_zero_cst (itype));
12292 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12293 (X & C) == 0 when C is a single bit. */
12294 if (TREE_CODE (arg0) == BIT_AND_EXPR
12295 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12296 && integer_zerop (arg1)
12297 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12299 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12300 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12301 TREE_OPERAND (arg0, 1));
12302 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12303 type, tem,
12304 fold_convert_loc (loc, TREE_TYPE (arg0),
12305 arg1));
12308 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12309 constant C is a power of two, i.e. a single bit. */
12310 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12311 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12312 && integer_zerop (arg1)
12313 && integer_pow2p (TREE_OPERAND (arg0, 1))
12314 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12315 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12317 tree arg00 = TREE_OPERAND (arg0, 0);
12318 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12319 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12322 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12323 when is C is a power of two, i.e. a single bit. */
12324 if (TREE_CODE (arg0) == BIT_AND_EXPR
12325 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12326 && integer_zerop (arg1)
12327 && integer_pow2p (TREE_OPERAND (arg0, 1))
12328 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12329 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12331 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12332 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12333 arg000, TREE_OPERAND (arg0, 1));
12334 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12335 tem, build_int_cst (TREE_TYPE (tem), 0));
12338 if (integer_zerop (arg1)
12339 && tree_expr_nonzero_p (arg0))
12341 tree res = constant_boolean_node (code==NE_EXPR, type);
12342 return omit_one_operand_loc (loc, type, res, arg0);
12345 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12346 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12348 tree arg00 = TREE_OPERAND (arg0, 0);
12349 tree arg01 = TREE_OPERAND (arg0, 1);
12350 tree arg10 = TREE_OPERAND (arg1, 0);
12351 tree arg11 = TREE_OPERAND (arg1, 1);
12352 tree itype = TREE_TYPE (arg0);
12354 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12355 operand_equal_p guarantees no side-effects so we don't need
12356 to use omit_one_operand on Z. */
12357 if (operand_equal_p (arg01, arg11, 0))
12358 return fold_build2_loc (loc, code, type, arg00,
12359 fold_convert_loc (loc, TREE_TYPE (arg00),
12360 arg10));
12361 if (operand_equal_p (arg01, arg10, 0))
12362 return fold_build2_loc (loc, code, type, arg00,
12363 fold_convert_loc (loc, TREE_TYPE (arg00),
12364 arg11));
12365 if (operand_equal_p (arg00, arg11, 0))
12366 return fold_build2_loc (loc, code, type, arg01,
12367 fold_convert_loc (loc, TREE_TYPE (arg01),
12368 arg10));
12369 if (operand_equal_p (arg00, arg10, 0))
12370 return fold_build2_loc (loc, code, type, arg01,
12371 fold_convert_loc (loc, TREE_TYPE (arg01),
12372 arg11));
12374 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12375 if (TREE_CODE (arg01) == INTEGER_CST
12376 && TREE_CODE (arg11) == INTEGER_CST)
12378 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12379 fold_convert_loc (loc, itype, arg11));
12380 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12381 return fold_build2_loc (loc, code, type, tem,
12382 fold_convert_loc (loc, itype, arg10));
12386 /* Attempt to simplify equality/inequality comparisons of complex
12387 values. Only lower the comparison if the result is known or
12388 can be simplified to a single scalar comparison. */
12389 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12390 || TREE_CODE (arg0) == COMPLEX_CST)
12391 && (TREE_CODE (arg1) == COMPLEX_EXPR
12392 || TREE_CODE (arg1) == COMPLEX_CST))
12394 tree real0, imag0, real1, imag1;
12395 tree rcond, icond;
12397 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12399 real0 = TREE_OPERAND (arg0, 0);
12400 imag0 = TREE_OPERAND (arg0, 1);
12402 else
12404 real0 = TREE_REALPART (arg0);
12405 imag0 = TREE_IMAGPART (arg0);
12408 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12410 real1 = TREE_OPERAND (arg1, 0);
12411 imag1 = TREE_OPERAND (arg1, 1);
12413 else
12415 real1 = TREE_REALPART (arg1);
12416 imag1 = TREE_IMAGPART (arg1);
12419 rcond = fold_binary_loc (loc, code, type, real0, real1);
12420 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12422 if (integer_zerop (rcond))
12424 if (code == EQ_EXPR)
12425 return omit_two_operands_loc (loc, type, boolean_false_node,
12426 imag0, imag1);
12427 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12429 else
12431 if (code == NE_EXPR)
12432 return omit_two_operands_loc (loc, type, boolean_true_node,
12433 imag0, imag1);
12434 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12438 icond = fold_binary_loc (loc, code, type, imag0, imag1);
12439 if (icond && TREE_CODE (icond) == INTEGER_CST)
12441 if (integer_zerop (icond))
12443 if (code == EQ_EXPR)
12444 return omit_two_operands_loc (loc, type, boolean_false_node,
12445 real0, real1);
12446 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12448 else
12450 if (code == NE_EXPR)
12451 return omit_two_operands_loc (loc, type, boolean_true_node,
12452 real0, real1);
12453 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12458 return NULL_TREE;
12460 case LT_EXPR:
12461 case GT_EXPR:
12462 case LE_EXPR:
12463 case GE_EXPR:
12464 tem = fold_comparison (loc, code, type, op0, op1);
12465 if (tem != NULL_TREE)
12466 return tem;
12468 /* Transform comparisons of the form X +- C CMP X. */
12469 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12470 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12471 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12472 && !HONOR_SNANS (arg0))
12474 tree arg01 = TREE_OPERAND (arg0, 1);
12475 enum tree_code code0 = TREE_CODE (arg0);
12476 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12478 /* (X - c) > X becomes false. */
12479 if (code == GT_EXPR
12480 && ((code0 == MINUS_EXPR && is_positive >= 0)
12481 || (code0 == PLUS_EXPR && is_positive <= 0)))
12482 return constant_boolean_node (0, type);
12484 /* Likewise (X + c) < X becomes false. */
12485 if (code == LT_EXPR
12486 && ((code0 == PLUS_EXPR && is_positive >= 0)
12487 || (code0 == MINUS_EXPR && is_positive <= 0)))
12488 return constant_boolean_node (0, type);
12490 /* Convert (X - c) <= X to true. */
12491 if (!HONOR_NANS (arg1)
12492 && code == LE_EXPR
12493 && ((code0 == MINUS_EXPR && is_positive >= 0)
12494 || (code0 == PLUS_EXPR && is_positive <= 0)))
12495 return constant_boolean_node (1, type);
12497 /* Convert (X + c) >= X to true. */
12498 if (!HONOR_NANS (arg1)
12499 && code == GE_EXPR
12500 && ((code0 == PLUS_EXPR && is_positive >= 0)
12501 || (code0 == MINUS_EXPR && is_positive <= 0)))
12502 return constant_boolean_node (1, type);
12505 /* If we are comparing an ABS_EXPR with a constant, we can
12506 convert all the cases into explicit comparisons, but they may
12507 well not be faster than doing the ABS and one comparison.
12508 But ABS (X) <= C is a range comparison, which becomes a subtraction
12509 and a comparison, and is probably faster. */
12510 if (code == LE_EXPR
12511 && TREE_CODE (arg1) == INTEGER_CST
12512 && TREE_CODE (arg0) == ABS_EXPR
12513 && ! TREE_SIDE_EFFECTS (arg0)
12514 && (tem = negate_expr (arg1)) != 0
12515 && TREE_CODE (tem) == INTEGER_CST
12516 && !TREE_OVERFLOW (tem))
12517 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12518 build2 (GE_EXPR, type,
12519 TREE_OPERAND (arg0, 0), tem),
12520 build2 (LE_EXPR, type,
12521 TREE_OPERAND (arg0, 0), arg1));
12523 /* Convert ABS_EXPR<x> >= 0 to true. */
12524 strict_overflow_p = false;
12525 if (code == GE_EXPR
12526 && (integer_zerop (arg1)
12527 || (! HONOR_NANS (arg0)
12528 && real_zerop (arg1)))
12529 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12531 if (strict_overflow_p)
12532 fold_overflow_warning (("assuming signed overflow does not occur "
12533 "when simplifying comparison of "
12534 "absolute value and zero"),
12535 WARN_STRICT_OVERFLOW_CONDITIONAL);
12536 return omit_one_operand_loc (loc, type,
12537 constant_boolean_node (true, type),
12538 arg0);
12541 /* Convert ABS_EXPR<x> < 0 to false. */
12542 strict_overflow_p = false;
12543 if (code == LT_EXPR
12544 && (integer_zerop (arg1) || real_zerop (arg1))
12545 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12547 if (strict_overflow_p)
12548 fold_overflow_warning (("assuming signed overflow does not occur "
12549 "when simplifying comparison of "
12550 "absolute value and zero"),
12551 WARN_STRICT_OVERFLOW_CONDITIONAL);
12552 return omit_one_operand_loc (loc, type,
12553 constant_boolean_node (false, type),
12554 arg0);
12557 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12558 and similarly for >= into !=. */
12559 if ((code == LT_EXPR || code == GE_EXPR)
12560 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12561 && TREE_CODE (arg1) == LSHIFT_EXPR
12562 && integer_onep (TREE_OPERAND (arg1, 0)))
12563 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12564 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12565 TREE_OPERAND (arg1, 1)),
12566 build_zero_cst (TREE_TYPE (arg0)));
12568 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12569 otherwise Y might be >= # of bits in X's type and thus e.g.
12570 (unsigned char) (1 << Y) for Y 15 might be 0.
12571 If the cast is widening, then 1 << Y should have unsigned type,
12572 otherwise if Y is number of bits in the signed shift type minus 1,
12573 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12574 31 might be 0xffffffff80000000. */
12575 if ((code == LT_EXPR || code == GE_EXPR)
12576 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12577 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12578 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12579 && CONVERT_EXPR_P (arg1)
12580 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12581 && (element_precision (TREE_TYPE (arg1))
12582 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12583 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12584 || (element_precision (TREE_TYPE (arg1))
12585 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12586 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12588 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12589 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12590 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12591 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12592 build_zero_cst (TREE_TYPE (arg0)));
12595 return NULL_TREE;
12597 case UNORDERED_EXPR:
12598 case ORDERED_EXPR:
12599 case UNLT_EXPR:
12600 case UNLE_EXPR:
12601 case UNGT_EXPR:
12602 case UNGE_EXPR:
12603 case UNEQ_EXPR:
12604 case LTGT_EXPR:
12605 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12607 tree targ0 = strip_float_extensions (arg0);
12608 tree targ1 = strip_float_extensions (arg1);
12609 tree newtype = TREE_TYPE (targ0);
12611 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
12612 newtype = TREE_TYPE (targ1);
12614 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
12615 return fold_build2_loc (loc, code, type,
12616 fold_convert_loc (loc, newtype, targ0),
12617 fold_convert_loc (loc, newtype, targ1));
12620 return NULL_TREE;
12622 case COMPOUND_EXPR:
12623 /* When pedantic, a compound expression can be neither an lvalue
12624 nor an integer constant expression. */
12625 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12626 return NULL_TREE;
12627 /* Don't let (0, 0) be null pointer constant. */
12628 tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12629 : fold_convert_loc (loc, type, arg1);
12630 return tem;
12632 default:
12633 return NULL_TREE;
12634 } /* switch (code) */
12637 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12638 ((A & N) + B) & M -> (A + B) & M
12639 Similarly if (N & M) == 0,
12640 ((A | N) + B) & M -> (A + B) & M
12641 and for - instead of + (or unary - instead of +)
12642 and/or ^ instead of |.
12643 If B is constant and (B & M) == 0, fold into A & M.
12645 This function is a helper for match.pd patterns. Return non-NULL
12646 type in which the simplified operation should be performed only
12647 if any optimization is possible.
12649 ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12650 then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12651 Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12652 +/-. */
12653 tree
12654 fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12655 tree arg00, enum tree_code code00, tree arg000, tree arg001,
12656 tree arg01, enum tree_code code01, tree arg010, tree arg011,
12657 tree *pmop)
12659 gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12660 gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12661 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12662 if (~cst1 == 0
12663 || (cst1 & (cst1 + 1)) != 0
12664 || !INTEGRAL_TYPE_P (type)
12665 || (!TYPE_OVERFLOW_WRAPS (type)
12666 && TREE_CODE (type) != INTEGER_TYPE)
12667 || (wi::max_value (type) & cst1) != cst1)
12668 return NULL_TREE;
12670 enum tree_code codes[2] = { code00, code01 };
12671 tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12672 int which = 0;
12673 wide_int cst0;
12675 /* Now we know that arg0 is (C + D) or (C - D) or -C and
12676 arg1 (M) is == (1LL << cst) - 1.
12677 Store C into PMOP[0] and D into PMOP[1]. */
12678 pmop[0] = arg00;
12679 pmop[1] = arg01;
12680 which = code != NEGATE_EXPR;
12682 for (; which >= 0; which--)
12683 switch (codes[which])
12685 case BIT_AND_EXPR:
12686 case BIT_IOR_EXPR:
12687 case BIT_XOR_EXPR:
12688 gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12689 cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12690 if (codes[which] == BIT_AND_EXPR)
12692 if (cst0 != cst1)
12693 break;
12695 else if (cst0 != 0)
12696 break;
12697 /* If C or D is of the form (A & N) where
12698 (N & M) == M, or of the form (A | N) or
12699 (A ^ N) where (N & M) == 0, replace it with A. */
12700 pmop[which] = arg0xx[2 * which];
12701 break;
12702 case ERROR_MARK:
12703 if (TREE_CODE (pmop[which]) != INTEGER_CST)
12704 break;
12705 /* If C or D is a N where (N & M) == 0, it can be
12706 omitted (replaced with 0). */
12707 if ((code == PLUS_EXPR
12708 || (code == MINUS_EXPR && which == 0))
12709 && (cst1 & wi::to_wide (pmop[which])) == 0)
12710 pmop[which] = build_int_cst (type, 0);
12711 /* Similarly, with C - N where (-N & M) == 0. */
12712 if (code == MINUS_EXPR
12713 && which == 1
12714 && (cst1 & -wi::to_wide (pmop[which])) == 0)
12715 pmop[which] = build_int_cst (type, 0);
12716 break;
12717 default:
12718 gcc_unreachable ();
12721 /* Only build anything new if we optimized one or both arguments above. */
12722 if (pmop[0] == arg00 && pmop[1] == arg01)
12723 return NULL_TREE;
12725 if (TYPE_OVERFLOW_WRAPS (type))
12726 return type;
12727 else
12728 return unsigned_type_for (type);
12731 /* Used by contains_label_[p1]. */
12733 struct contains_label_data
12735 hash_set<tree> *pset;
12736 bool inside_switch_p;
12739 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12740 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12741 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12743 static tree
12744 contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12746 contains_label_data *d = (contains_label_data *) data;
12747 switch (TREE_CODE (*tp))
12749 case LABEL_EXPR:
12750 return *tp;
12752 case CASE_LABEL_EXPR:
12753 if (!d->inside_switch_p)
12754 return *tp;
12755 return NULL_TREE;
12757 case SWITCH_EXPR:
12758 if (!d->inside_switch_p)
12760 if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12761 return *tp;
12762 d->inside_switch_p = true;
12763 if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12764 return *tp;
12765 d->inside_switch_p = false;
12766 *walk_subtrees = 0;
12768 return NULL_TREE;
12770 case GOTO_EXPR:
12771 *walk_subtrees = 0;
12772 return NULL_TREE;
12774 default:
12775 return NULL_TREE;
12779 /* Return whether the sub-tree ST contains a label which is accessible from
12780 outside the sub-tree. */
12782 static bool
12783 contains_label_p (tree st)
12785 hash_set<tree> pset;
12786 contains_label_data data = { &pset, false };
12787 return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12790 /* Fold a ternary expression of code CODE and type TYPE with operands
12791 OP0, OP1, and OP2. Return the folded expression if folding is
12792 successful. Otherwise, return NULL_TREE. */
12794 tree
12795 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12796 tree op0, tree op1, tree op2)
12798 tree tem;
12799 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12800 enum tree_code_class kind = TREE_CODE_CLASS (code);
12802 gcc_assert (IS_EXPR_CODE_CLASS (kind)
12803 && TREE_CODE_LENGTH (code) == 3);
12805 /* If this is a commutative operation, and OP0 is a constant, move it
12806 to OP1 to reduce the number of tests below. */
12807 if (commutative_ternary_tree_code (code)
12808 && tree_swap_operands_p (op0, op1))
12809 return fold_build3_loc (loc, code, type, op1, op0, op2);
12811 tem = generic_simplify (loc, code, type, op0, op1, op2);
12812 if (tem)
12813 return tem;
12815 /* Strip any conversions that don't change the mode. This is safe
12816 for every expression, except for a comparison expression because
12817 its signedness is derived from its operands. So, in the latter
12818 case, only strip conversions that don't change the signedness.
12820 Note that this is done as an internal manipulation within the
12821 constant folder, in order to find the simplest representation of
12822 the arguments so that their form can be studied. In any cases,
12823 the appropriate type conversions should be put back in the tree
12824 that will get out of the constant folder. */
12825 if (op0)
12827 arg0 = op0;
12828 STRIP_NOPS (arg0);
12831 if (op1)
12833 arg1 = op1;
12834 STRIP_NOPS (arg1);
12837 if (op2)
12839 arg2 = op2;
12840 STRIP_NOPS (arg2);
12843 switch (code)
12845 case COMPONENT_REF:
12846 if (TREE_CODE (arg0) == CONSTRUCTOR
12847 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12849 unsigned HOST_WIDE_INT idx;
12850 tree field, value;
12851 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12852 if (field == arg1)
12853 return value;
12855 return NULL_TREE;
12857 case COND_EXPR:
12858 case VEC_COND_EXPR:
12859 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12860 so all simple results must be passed through pedantic_non_lvalue. */
12861 if (TREE_CODE (arg0) == INTEGER_CST)
12863 tree unused_op = integer_zerop (arg0) ? op1 : op2;
12864 tem = integer_zerop (arg0) ? op2 : op1;
12865 /* Only optimize constant conditions when the selected branch
12866 has the same type as the COND_EXPR. This avoids optimizing
12867 away "c ? x : throw", where the throw has a void type.
12868 Avoid throwing away that operand which contains label. */
12869 if ((!TREE_SIDE_EFFECTS (unused_op)
12870 || !contains_label_p (unused_op))
12871 && (! VOID_TYPE_P (TREE_TYPE (tem))
12872 || VOID_TYPE_P (type)))
12873 return protected_set_expr_location_unshare (tem, loc);
12874 return NULL_TREE;
12876 else if (TREE_CODE (arg0) == VECTOR_CST)
12878 unsigned HOST_WIDE_INT nelts;
12879 if ((TREE_CODE (arg1) == VECTOR_CST
12880 || TREE_CODE (arg1) == CONSTRUCTOR)
12881 && (TREE_CODE (arg2) == VECTOR_CST
12882 || TREE_CODE (arg2) == CONSTRUCTOR)
12883 && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
12885 vec_perm_builder sel (nelts, nelts, 1);
12886 for (unsigned int i = 0; i < nelts; i++)
12888 tree val = VECTOR_CST_ELT (arg0, i);
12889 if (integer_all_onesp (val))
12890 sel.quick_push (i);
12891 else if (integer_zerop (val))
12892 sel.quick_push (nelts + i);
12893 else /* Currently unreachable. */
12894 return NULL_TREE;
12896 vec_perm_indices indices (sel, 2, nelts);
12897 tree t = fold_vec_perm (type, arg1, arg2, indices);
12898 if (t != NULL_TREE)
12899 return t;
12903 /* If we have A op B ? A : C, we may be able to convert this to a
12904 simpler expression, depending on the operation and the values
12905 of B and C. Signed zeros prevent all of these transformations,
12906 for reasons given above each one.
12908 Also try swapping the arguments and inverting the conditional. */
12909 if (COMPARISON_CLASS_P (arg0)
12910 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
12911 && !HONOR_SIGNED_ZEROS (op1))
12913 tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
12914 TREE_OPERAND (arg0, 0),
12915 TREE_OPERAND (arg0, 1),
12916 op1, op2);
12917 if (tem)
12918 return tem;
12921 if (COMPARISON_CLASS_P (arg0)
12922 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
12923 && !HONOR_SIGNED_ZEROS (op2))
12925 enum tree_code comp_code = TREE_CODE (arg0);
12926 tree arg00 = TREE_OPERAND (arg0, 0);
12927 tree arg01 = TREE_OPERAND (arg0, 1);
12928 comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
12929 if (comp_code != ERROR_MARK)
12930 tem = fold_cond_expr_with_comparison (loc, type, comp_code,
12931 arg00,
12932 arg01,
12933 op2, op1);
12934 if (tem)
12935 return tem;
12938 /* If the second operand is simpler than the third, swap them
12939 since that produces better jump optimization results. */
12940 if (truth_value_p (TREE_CODE (arg0))
12941 && tree_swap_operands_p (op1, op2))
12943 location_t loc0 = expr_location_or (arg0, loc);
12944 /* See if this can be inverted. If it can't, possibly because
12945 it was a floating-point inequality comparison, don't do
12946 anything. */
12947 tem = fold_invert_truthvalue (loc0, arg0);
12948 if (tem)
12949 return fold_build3_loc (loc, code, type, tem, op2, op1);
12952 /* Convert A ? 1 : 0 to simply A. */
12953 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
12954 : (integer_onep (op1)
12955 && !VECTOR_TYPE_P (type)))
12956 && integer_zerop (op2)
12957 /* If we try to convert OP0 to our type, the
12958 call to fold will try to move the conversion inside
12959 a COND, which will recurse. In that case, the COND_EXPR
12960 is probably the best choice, so leave it alone. */
12961 && type == TREE_TYPE (arg0))
12962 return protected_set_expr_location_unshare (arg0, loc);
12964 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12965 over COND_EXPR in cases such as floating point comparisons. */
12966 if (integer_zerop (op1)
12967 && code == COND_EXPR
12968 && integer_onep (op2)
12969 && !VECTOR_TYPE_P (type)
12970 && truth_value_p (TREE_CODE (arg0)))
12971 return fold_convert_loc (loc, type,
12972 invert_truthvalue_loc (loc, arg0));
12974 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12975 if (TREE_CODE (arg0) == LT_EXPR
12976 && integer_zerop (TREE_OPERAND (arg0, 1))
12977 && integer_zerop (op2)
12978 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12980 /* sign_bit_p looks through both zero and sign extensions,
12981 but for this optimization only sign extensions are
12982 usable. */
12983 tree tem2 = TREE_OPERAND (arg0, 0);
12984 while (tem != tem2)
12986 if (TREE_CODE (tem2) != NOP_EXPR
12987 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
12989 tem = NULL_TREE;
12990 break;
12992 tem2 = TREE_OPERAND (tem2, 0);
12994 /* sign_bit_p only checks ARG1 bits within A's precision.
12995 If <sign bit of A> has wider type than A, bits outside
12996 of A's precision in <sign bit of A> need to be checked.
12997 If they are all 0, this optimization needs to be done
12998 in unsigned A's type, if they are all 1 in signed A's type,
12999 otherwise this can't be done. */
13000 if (tem
13001 && TYPE_PRECISION (TREE_TYPE (tem))
13002 < TYPE_PRECISION (TREE_TYPE (arg1))
13003 && TYPE_PRECISION (TREE_TYPE (tem))
13004 < TYPE_PRECISION (type))
13006 int inner_width, outer_width;
13007 tree tem_type;
13009 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13010 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13011 if (outer_width > TYPE_PRECISION (type))
13012 outer_width = TYPE_PRECISION (type);
13014 wide_int mask = wi::shifted_mask
13015 (inner_width, outer_width - inner_width, false,
13016 TYPE_PRECISION (TREE_TYPE (arg1)));
13018 wide_int common = mask & wi::to_wide (arg1);
13019 if (common == mask)
13021 tem_type = signed_type_for (TREE_TYPE (tem));
13022 tem = fold_convert_loc (loc, tem_type, tem);
13024 else if (common == 0)
13026 tem_type = unsigned_type_for (TREE_TYPE (tem));
13027 tem = fold_convert_loc (loc, tem_type, tem);
13029 else
13030 tem = NULL;
13033 if (tem)
13034 return
13035 fold_convert_loc (loc, type,
13036 fold_build2_loc (loc, BIT_AND_EXPR,
13037 TREE_TYPE (tem), tem,
13038 fold_convert_loc (loc,
13039 TREE_TYPE (tem),
13040 arg1)));
13043 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13044 already handled above. */
13045 if (TREE_CODE (arg0) == BIT_AND_EXPR
13046 && integer_onep (TREE_OPERAND (arg0, 1))
13047 && integer_zerop (op2)
13048 && integer_pow2p (arg1))
13050 tree tem = TREE_OPERAND (arg0, 0);
13051 STRIP_NOPS (tem);
13052 if (TREE_CODE (tem) == RSHIFT_EXPR
13053 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
13054 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
13055 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
13056 return fold_build2_loc (loc, BIT_AND_EXPR, type,
13057 fold_convert_loc (loc, type,
13058 TREE_OPERAND (tem, 0)),
13059 op1);
13062 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13063 is probably obsolete because the first operand should be a
13064 truth value (that's why we have the two cases above), but let's
13065 leave it in until we can confirm this for all front-ends. */
13066 if (integer_zerop (op2)
13067 && TREE_CODE (arg0) == NE_EXPR
13068 && integer_zerop (TREE_OPERAND (arg0, 1))
13069 && integer_pow2p (arg1)
13070 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13071 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13072 arg1, OEP_ONLY_CONST)
13073 /* operand_equal_p compares just value, not precision, so e.g.
13074 arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13075 second operand 32-bit -128, which is not a power of two (or vice
13076 versa. */
13077 && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
13078 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
13080 /* Disable the transformations below for vectors, since
13081 fold_binary_op_with_conditional_arg may undo them immediately,
13082 yielding an infinite loop. */
13083 if (code == VEC_COND_EXPR)
13084 return NULL_TREE;
13086 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13087 if (integer_zerop (op2)
13088 && truth_value_p (TREE_CODE (arg0))
13089 && truth_value_p (TREE_CODE (arg1))
13090 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13091 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13092 : TRUTH_ANDIF_EXPR,
13093 type, fold_convert_loc (loc, type, arg0), op1);
13095 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13096 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13097 && truth_value_p (TREE_CODE (arg0))
13098 && truth_value_p (TREE_CODE (arg1))
13099 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13101 location_t loc0 = expr_location_or (arg0, loc);
13102 /* Only perform transformation if ARG0 is easily inverted. */
13103 tem = fold_invert_truthvalue (loc0, arg0);
13104 if (tem)
13105 return fold_build2_loc (loc, code == VEC_COND_EXPR
13106 ? BIT_IOR_EXPR
13107 : TRUTH_ORIF_EXPR,
13108 type, fold_convert_loc (loc, type, tem),
13109 op1);
13112 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13113 if (integer_zerop (arg1)
13114 && truth_value_p (TREE_CODE (arg0))
13115 && truth_value_p (TREE_CODE (op2))
13116 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13118 location_t loc0 = expr_location_or (arg0, loc);
13119 /* Only perform transformation if ARG0 is easily inverted. */
13120 tem = fold_invert_truthvalue (loc0, arg0);
13121 if (tem)
13122 return fold_build2_loc (loc, code == VEC_COND_EXPR
13123 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13124 type, fold_convert_loc (loc, type, tem),
13125 op2);
13128 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13129 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13130 && truth_value_p (TREE_CODE (arg0))
13131 && truth_value_p (TREE_CODE (op2))
13132 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13133 return fold_build2_loc (loc, code == VEC_COND_EXPR
13134 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13135 type, fold_convert_loc (loc, type, arg0), op2);
13137 return NULL_TREE;
13139 case CALL_EXPR:
13140 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13141 of fold_ternary on them. */
13142 gcc_unreachable ();
13144 case BIT_FIELD_REF:
13145 if (TREE_CODE (arg0) == VECTOR_CST
13146 && (type == TREE_TYPE (TREE_TYPE (arg0))
13147 || (VECTOR_TYPE_P (type)
13148 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13149 && tree_fits_uhwi_p (op1)
13150 && tree_fits_uhwi_p (op2))
13152 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13153 unsigned HOST_WIDE_INT width
13154 = (TREE_CODE (eltype) == BOOLEAN_TYPE
13155 ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13156 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13157 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13159 if (n != 0
13160 && (idx % width) == 0
13161 && (n % width) == 0
13162 && known_le ((idx + n) / width,
13163 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13165 idx = idx / width;
13166 n = n / width;
13168 if (TREE_CODE (arg0) == VECTOR_CST)
13170 if (n == 1)
13172 tem = VECTOR_CST_ELT (arg0, idx);
13173 if (VECTOR_TYPE_P (type))
13174 tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13175 return tem;
13178 tree_vector_builder vals (type, n, 1);
13179 for (unsigned i = 0; i < n; ++i)
13180 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13181 return vals.build ();
13186 /* On constants we can use native encode/interpret to constant
13187 fold (nearly) all BIT_FIELD_REFs. */
13188 if (CONSTANT_CLASS_P (arg0)
13189 && can_native_interpret_type_p (type)
13190 && BITS_PER_UNIT == 8
13191 && tree_fits_uhwi_p (op1)
13192 && tree_fits_uhwi_p (op2))
13194 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13195 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13196 /* Limit us to a reasonable amount of work. To relax the
13197 other limitations we need bit-shifting of the buffer
13198 and rounding up the size. */
13199 if (bitpos % BITS_PER_UNIT == 0
13200 && bitsize % BITS_PER_UNIT == 0
13201 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13203 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13204 unsigned HOST_WIDE_INT len
13205 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13206 bitpos / BITS_PER_UNIT);
13207 if (len > 0
13208 && len * BITS_PER_UNIT >= bitsize)
13210 tree v = native_interpret_expr (type, b,
13211 bitsize / BITS_PER_UNIT);
13212 if (v)
13213 return v;
13218 return NULL_TREE;
13220 case VEC_PERM_EXPR:
13221 /* Perform constant folding of BIT_INSERT_EXPR. */
13222 if (TREE_CODE (arg2) == VECTOR_CST
13223 && TREE_CODE (op0) == VECTOR_CST
13224 && TREE_CODE (op1) == VECTOR_CST)
13226 /* Build a vector of integers from the tree mask. */
13227 vec_perm_builder builder;
13228 if (!tree_to_vec_perm_builder (&builder, arg2))
13229 return NULL_TREE;
13231 /* Create a vec_perm_indices for the integer vector. */
13232 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13233 bool single_arg = (op0 == op1);
13234 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13235 return fold_vec_perm (type, op0, op1, sel);
13237 return NULL_TREE;
13239 case BIT_INSERT_EXPR:
13240 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13241 if (TREE_CODE (arg0) == INTEGER_CST
13242 && TREE_CODE (arg1) == INTEGER_CST)
13244 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13245 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13246 wide_int tem = (wi::to_wide (arg0)
13247 & wi::shifted_mask (bitpos, bitsize, true,
13248 TYPE_PRECISION (type)));
13249 wide_int tem2
13250 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13251 bitsize), bitpos);
13252 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13254 else if (TREE_CODE (arg0) == VECTOR_CST
13255 && CONSTANT_CLASS_P (arg1)
13256 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13257 TREE_TYPE (arg1)))
13259 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13260 unsigned HOST_WIDE_INT elsize
13261 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13262 if (bitpos % elsize == 0)
13264 unsigned k = bitpos / elsize;
13265 unsigned HOST_WIDE_INT nelts;
13266 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13267 return arg0;
13268 else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13270 tree_vector_builder elts (type, nelts, 1);
13271 elts.quick_grow (nelts);
13272 for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13273 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13274 return elts.build ();
13278 return NULL_TREE;
13280 default:
13281 return NULL_TREE;
13282 } /* switch (code) */
13285 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13286 of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13287 constructor element index of the value returned. If the element is
13288 not found NULL_TREE is returned and *CTOR_IDX is updated to
13289 the index of the element after the ACCESS_INDEX position (which
13290 may be outside of the CTOR array). */
13292 tree
13293 get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13294 unsigned *ctor_idx)
13296 tree index_type = NULL_TREE;
13297 signop index_sgn = UNSIGNED;
13298 offset_int low_bound = 0;
13300 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13302 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13303 if (domain_type && TYPE_MIN_VALUE (domain_type))
13305 /* Static constructors for variably sized objects makes no sense. */
13306 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13307 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13308 /* ??? When it is obvious that the range is signed, treat it so. */
13309 if (TYPE_UNSIGNED (index_type)
13310 && TYPE_MAX_VALUE (domain_type)
13311 && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13312 TYPE_MIN_VALUE (domain_type)))
13314 index_sgn = SIGNED;
13315 low_bound
13316 = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13317 SIGNED);
13319 else
13321 index_sgn = TYPE_SIGN (index_type);
13322 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13327 if (index_type)
13328 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13329 index_sgn);
13331 offset_int index = low_bound;
13332 if (index_type)
13333 index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13335 offset_int max_index = index;
13336 unsigned cnt;
13337 tree cfield, cval;
13338 bool first_p = true;
13340 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13342 /* Array constructor might explicitly set index, or specify a range,
13343 or leave index NULL meaning that it is next index after previous
13344 one. */
13345 if (cfield)
13347 if (TREE_CODE (cfield) == INTEGER_CST)
13348 max_index = index
13349 = offset_int::from (wi::to_wide (cfield), index_sgn);
13350 else
13352 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13353 index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13354 index_sgn);
13355 max_index
13356 = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13357 index_sgn);
13358 gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13361 else if (!first_p)
13363 index = max_index + 1;
13364 if (index_type)
13365 index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13366 gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13367 max_index = index;
13369 else
13370 first_p = false;
13372 /* Do we have match? */
13373 if (wi::cmp (access_index, index, index_sgn) >= 0)
13375 if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13377 if (ctor_idx)
13378 *ctor_idx = cnt;
13379 return cval;
13382 else if (in_gimple_form)
13383 /* We're past the element we search for. Note during parsing
13384 the elements might not be sorted.
13385 ??? We should use a binary search and a flag on the
13386 CONSTRUCTOR as to whether elements are sorted in declaration
13387 order. */
13388 break;
13390 if (ctor_idx)
13391 *ctor_idx = cnt;
13392 return NULL_TREE;
13395 /* Perform constant folding and related simplification of EXPR.
13396 The related simplifications include x*1 => x, x*0 => 0, etc.,
13397 and application of the associative law.
13398 NOP_EXPR conversions may be removed freely (as long as we
13399 are careful not to change the type of the overall expression).
13400 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13401 but we can constant-fold them if they have constant operands. */
13403 #ifdef ENABLE_FOLD_CHECKING
13404 # define fold(x) fold_1 (x)
13405 static tree fold_1 (tree);
13406 static
13407 #endif
13408 tree
13409 fold (tree expr)
13411 const tree t = expr;
13412 enum tree_code code = TREE_CODE (t);
13413 enum tree_code_class kind = TREE_CODE_CLASS (code);
13414 tree tem;
13415 location_t loc = EXPR_LOCATION (expr);
13417 /* Return right away if a constant. */
13418 if (kind == tcc_constant)
13419 return t;
13421 /* CALL_EXPR-like objects with variable numbers of operands are
13422 treated specially. */
13423 if (kind == tcc_vl_exp)
13425 if (code == CALL_EXPR)
13427 tem = fold_call_expr (loc, expr, false);
13428 return tem ? tem : expr;
13430 return expr;
13433 if (IS_EXPR_CODE_CLASS (kind))
13435 tree type = TREE_TYPE (t);
13436 tree op0, op1, op2;
13438 switch (TREE_CODE_LENGTH (code))
13440 case 1:
13441 op0 = TREE_OPERAND (t, 0);
13442 tem = fold_unary_loc (loc, code, type, op0);
13443 return tem ? tem : expr;
13444 case 2:
13445 op0 = TREE_OPERAND (t, 0);
13446 op1 = TREE_OPERAND (t, 1);
13447 tem = fold_binary_loc (loc, code, type, op0, op1);
13448 return tem ? tem : expr;
13449 case 3:
13450 op0 = TREE_OPERAND (t, 0);
13451 op1 = TREE_OPERAND (t, 1);
13452 op2 = TREE_OPERAND (t, 2);
13453 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13454 return tem ? tem : expr;
13455 default:
13456 break;
13460 switch (code)
13462 case ARRAY_REF:
13464 tree op0 = TREE_OPERAND (t, 0);
13465 tree op1 = TREE_OPERAND (t, 1);
13467 if (TREE_CODE (op1) == INTEGER_CST
13468 && TREE_CODE (op0) == CONSTRUCTOR
13469 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13471 tree val = get_array_ctor_element_at_index (op0,
13472 wi::to_offset (op1));
13473 if (val)
13474 return val;
13477 return t;
13480 /* Return a VECTOR_CST if possible. */
13481 case CONSTRUCTOR:
13483 tree type = TREE_TYPE (t);
13484 if (TREE_CODE (type) != VECTOR_TYPE)
13485 return t;
13487 unsigned i;
13488 tree val;
13489 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13490 if (! CONSTANT_CLASS_P (val))
13491 return t;
13493 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13496 case CONST_DECL:
13497 return fold (DECL_INITIAL (t));
13499 default:
13500 return t;
13501 } /* switch (code) */
13504 #ifdef ENABLE_FOLD_CHECKING
13505 #undef fold
13507 static void fold_checksum_tree (const_tree, struct md5_ctx *,
13508 hash_table<nofree_ptr_hash<const tree_node> > *);
13509 static void fold_check_failed (const_tree, const_tree);
13510 void print_fold_checksum (const_tree);
13512 /* When --enable-checking=fold, compute a digest of expr before
13513 and after actual fold call to see if fold did not accidentally
13514 change original expr. */
13516 tree
13517 fold (tree expr)
13519 tree ret;
13520 struct md5_ctx ctx;
13521 unsigned char checksum_before[16], checksum_after[16];
13522 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13524 md5_init_ctx (&ctx);
13525 fold_checksum_tree (expr, &ctx, &ht);
13526 md5_finish_ctx (&ctx, checksum_before);
13527 ht.empty ();
13529 ret = fold_1 (expr);
13531 md5_init_ctx (&ctx);
13532 fold_checksum_tree (expr, &ctx, &ht);
13533 md5_finish_ctx (&ctx, checksum_after);
13535 if (memcmp (checksum_before, checksum_after, 16))
13536 fold_check_failed (expr, ret);
13538 return ret;
13541 void
13542 print_fold_checksum (const_tree expr)
13544 struct md5_ctx ctx;
13545 unsigned char checksum[16], cnt;
13546 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13548 md5_init_ctx (&ctx);
13549 fold_checksum_tree (expr, &ctx, &ht);
13550 md5_finish_ctx (&ctx, checksum);
13551 for (cnt = 0; cnt < 16; ++cnt)
13552 fprintf (stderr, "%02x", checksum[cnt]);
13553 putc ('\n', stderr);
13556 static void
13557 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13559 internal_error ("fold check: original tree changed by fold");
13562 static void
13563 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13564 hash_table<nofree_ptr_hash <const tree_node> > *ht)
13566 const tree_node **slot;
13567 enum tree_code code;
13568 union tree_node *buf;
13569 int i, len;
13571 recursive_label:
13572 if (expr == NULL)
13573 return;
13574 slot = ht->find_slot (expr, INSERT);
13575 if (*slot != NULL)
13576 return;
13577 *slot = expr;
13578 code = TREE_CODE (expr);
13579 if (TREE_CODE_CLASS (code) == tcc_declaration
13580 && HAS_DECL_ASSEMBLER_NAME_P (expr))
13582 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13583 size_t sz = tree_size (expr);
13584 buf = XALLOCAVAR (union tree_node, sz);
13585 memcpy ((char *) buf, expr, sz);
13586 SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13587 buf->decl_with_vis.symtab_node = NULL;
13588 buf->base.nowarning_flag = 0;
13589 expr = (tree) buf;
13591 else if (TREE_CODE_CLASS (code) == tcc_type
13592 && (TYPE_POINTER_TO (expr)
13593 || TYPE_REFERENCE_TO (expr)
13594 || TYPE_CACHED_VALUES_P (expr)
13595 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13596 || TYPE_NEXT_VARIANT (expr)
13597 || TYPE_ALIAS_SET_KNOWN_P (expr)))
13599 /* Allow these fields to be modified. */
13600 tree tmp;
13601 size_t sz = tree_size (expr);
13602 buf = XALLOCAVAR (union tree_node, sz);
13603 memcpy ((char *) buf, expr, sz);
13604 expr = tmp = (tree) buf;
13605 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13606 TYPE_POINTER_TO (tmp) = NULL;
13607 TYPE_REFERENCE_TO (tmp) = NULL;
13608 TYPE_NEXT_VARIANT (tmp) = NULL;
13609 TYPE_ALIAS_SET (tmp) = -1;
13610 if (TYPE_CACHED_VALUES_P (tmp))
13612 TYPE_CACHED_VALUES_P (tmp) = 0;
13613 TYPE_CACHED_VALUES (tmp) = NULL;
13616 else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13618 /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13619 that and change builtins.cc etc. instead - see PR89543. */
13620 size_t sz = tree_size (expr);
13621 buf = XALLOCAVAR (union tree_node, sz);
13622 memcpy ((char *) buf, expr, sz);
13623 buf->base.nowarning_flag = 0;
13624 expr = (tree) buf;
13626 md5_process_bytes (expr, tree_size (expr), ctx);
13627 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13628 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13629 if (TREE_CODE_CLASS (code) != tcc_type
13630 && TREE_CODE_CLASS (code) != tcc_declaration
13631 && code != TREE_LIST
13632 && code != SSA_NAME
13633 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13634 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13635 switch (TREE_CODE_CLASS (code))
13637 case tcc_constant:
13638 switch (code)
13640 case STRING_CST:
13641 md5_process_bytes (TREE_STRING_POINTER (expr),
13642 TREE_STRING_LENGTH (expr), ctx);
13643 break;
13644 case COMPLEX_CST:
13645 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13646 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13647 break;
13648 case VECTOR_CST:
13649 len = vector_cst_encoded_nelts (expr);
13650 for (i = 0; i < len; ++i)
13651 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13652 break;
13653 default:
13654 break;
13656 break;
13657 case tcc_exceptional:
13658 switch (code)
13660 case TREE_LIST:
13661 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13662 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13663 expr = TREE_CHAIN (expr);
13664 goto recursive_label;
13665 break;
13666 case TREE_VEC:
13667 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13668 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13669 break;
13670 default:
13671 break;
13673 break;
13674 case tcc_expression:
13675 case tcc_reference:
13676 case tcc_comparison:
13677 case tcc_unary:
13678 case tcc_binary:
13679 case tcc_statement:
13680 case tcc_vl_exp:
13681 len = TREE_OPERAND_LENGTH (expr);
13682 for (i = 0; i < len; ++i)
13683 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13684 break;
13685 case tcc_declaration:
13686 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13687 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13688 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13690 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13691 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13692 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13693 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13694 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13697 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13699 if (TREE_CODE (expr) == FUNCTION_DECL)
13701 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13702 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13704 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13706 break;
13707 case tcc_type:
13708 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13709 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13710 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13711 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13712 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13713 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13714 if (INTEGRAL_TYPE_P (expr)
13715 || SCALAR_FLOAT_TYPE_P (expr))
13717 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13718 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13720 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13721 if (TREE_CODE (expr) == RECORD_TYPE
13722 || TREE_CODE (expr) == UNION_TYPE
13723 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13724 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13725 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13726 break;
13727 default:
13728 break;
13732 /* Helper function for outputting the checksum of a tree T. When
13733 debugging with gdb, you can "define mynext" to be "next" followed
13734 by "call debug_fold_checksum (op0)", then just trace down till the
13735 outputs differ. */
13737 DEBUG_FUNCTION void
13738 debug_fold_checksum (const_tree t)
13740 int i;
13741 unsigned char checksum[16];
13742 struct md5_ctx ctx;
13743 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13745 md5_init_ctx (&ctx);
13746 fold_checksum_tree (t, &ctx, &ht);
13747 md5_finish_ctx (&ctx, checksum);
13748 ht.empty ();
13750 for (i = 0; i < 16; i++)
13751 fprintf (stderr, "%d ", checksum[i]);
13753 fprintf (stderr, "\n");
13756 #endif
13758 /* Fold a unary tree expression with code CODE of type TYPE with an
13759 operand OP0. LOC is the location of the resulting expression.
13760 Return a folded expression if successful. Otherwise, return a tree
13761 expression with code CODE of type TYPE with an operand OP0. */
13763 tree
13764 fold_build1_loc (location_t loc,
13765 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13767 tree tem;
13768 #ifdef ENABLE_FOLD_CHECKING
13769 unsigned char checksum_before[16], checksum_after[16];
13770 struct md5_ctx ctx;
13771 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13773 md5_init_ctx (&ctx);
13774 fold_checksum_tree (op0, &ctx, &ht);
13775 md5_finish_ctx (&ctx, checksum_before);
13776 ht.empty ();
13777 #endif
13779 tem = fold_unary_loc (loc, code, type, op0);
13780 if (!tem)
13781 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13783 #ifdef ENABLE_FOLD_CHECKING
13784 md5_init_ctx (&ctx);
13785 fold_checksum_tree (op0, &ctx, &ht);
13786 md5_finish_ctx (&ctx, checksum_after);
13788 if (memcmp (checksum_before, checksum_after, 16))
13789 fold_check_failed (op0, tem);
13790 #endif
13791 return tem;
13794 /* Fold a binary tree expression with code CODE of type TYPE with
13795 operands OP0 and OP1. LOC is the location of the resulting
13796 expression. Return a folded expression if successful. Otherwise,
13797 return a tree expression with code CODE of type TYPE with operands
13798 OP0 and OP1. */
13800 tree
13801 fold_build2_loc (location_t loc,
13802 enum tree_code code, tree type, tree op0, tree op1
13803 MEM_STAT_DECL)
13805 tree tem;
13806 #ifdef ENABLE_FOLD_CHECKING
13807 unsigned char checksum_before_op0[16],
13808 checksum_before_op1[16],
13809 checksum_after_op0[16],
13810 checksum_after_op1[16];
13811 struct md5_ctx ctx;
13812 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13814 md5_init_ctx (&ctx);
13815 fold_checksum_tree (op0, &ctx, &ht);
13816 md5_finish_ctx (&ctx, checksum_before_op0);
13817 ht.empty ();
13819 md5_init_ctx (&ctx);
13820 fold_checksum_tree (op1, &ctx, &ht);
13821 md5_finish_ctx (&ctx, checksum_before_op1);
13822 ht.empty ();
13823 #endif
13825 tem = fold_binary_loc (loc, code, type, op0, op1);
13826 if (!tem)
13827 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13829 #ifdef ENABLE_FOLD_CHECKING
13830 md5_init_ctx (&ctx);
13831 fold_checksum_tree (op0, &ctx, &ht);
13832 md5_finish_ctx (&ctx, checksum_after_op0);
13833 ht.empty ();
13835 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13836 fold_check_failed (op0, tem);
13838 md5_init_ctx (&ctx);
13839 fold_checksum_tree (op1, &ctx, &ht);
13840 md5_finish_ctx (&ctx, checksum_after_op1);
13842 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13843 fold_check_failed (op1, tem);
13844 #endif
13845 return tem;
13848 /* Fold a ternary tree expression with code CODE of type TYPE with
13849 operands OP0, OP1, and OP2. Return a folded expression if
13850 successful. Otherwise, return a tree expression with code CODE of
13851 type TYPE with operands OP0, OP1, and OP2. */
13853 tree
13854 fold_build3_loc (location_t loc, enum tree_code code, tree type,
13855 tree op0, tree op1, tree op2 MEM_STAT_DECL)
13857 tree tem;
13858 #ifdef ENABLE_FOLD_CHECKING
13859 unsigned char checksum_before_op0[16],
13860 checksum_before_op1[16],
13861 checksum_before_op2[16],
13862 checksum_after_op0[16],
13863 checksum_after_op1[16],
13864 checksum_after_op2[16];
13865 struct md5_ctx ctx;
13866 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13868 md5_init_ctx (&ctx);
13869 fold_checksum_tree (op0, &ctx, &ht);
13870 md5_finish_ctx (&ctx, checksum_before_op0);
13871 ht.empty ();
13873 md5_init_ctx (&ctx);
13874 fold_checksum_tree (op1, &ctx, &ht);
13875 md5_finish_ctx (&ctx, checksum_before_op1);
13876 ht.empty ();
13878 md5_init_ctx (&ctx);
13879 fold_checksum_tree (op2, &ctx, &ht);
13880 md5_finish_ctx (&ctx, checksum_before_op2);
13881 ht.empty ();
13882 #endif
13884 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13885 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13886 if (!tem)
13887 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
13889 #ifdef ENABLE_FOLD_CHECKING
13890 md5_init_ctx (&ctx);
13891 fold_checksum_tree (op0, &ctx, &ht);
13892 md5_finish_ctx (&ctx, checksum_after_op0);
13893 ht.empty ();
13895 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13896 fold_check_failed (op0, tem);
13898 md5_init_ctx (&ctx);
13899 fold_checksum_tree (op1, &ctx, &ht);
13900 md5_finish_ctx (&ctx, checksum_after_op1);
13901 ht.empty ();
13903 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13904 fold_check_failed (op1, tem);
13906 md5_init_ctx (&ctx);
13907 fold_checksum_tree (op2, &ctx, &ht);
13908 md5_finish_ctx (&ctx, checksum_after_op2);
13910 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13911 fold_check_failed (op2, tem);
13912 #endif
13913 return tem;
13916 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13917 arguments in ARGARRAY, and a null static chain.
13918 Return a folded expression if successful. Otherwise, return a CALL_EXPR
13919 of type TYPE from the given operands as constructed by build_call_array. */
13921 tree
13922 fold_build_call_array_loc (location_t loc, tree type, tree fn,
13923 int nargs, tree *argarray)
13925 tree tem;
13926 #ifdef ENABLE_FOLD_CHECKING
13927 unsigned char checksum_before_fn[16],
13928 checksum_before_arglist[16],
13929 checksum_after_fn[16],
13930 checksum_after_arglist[16];
13931 struct md5_ctx ctx;
13932 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13933 int i;
13935 md5_init_ctx (&ctx);
13936 fold_checksum_tree (fn, &ctx, &ht);
13937 md5_finish_ctx (&ctx, checksum_before_fn);
13938 ht.empty ();
13940 md5_init_ctx (&ctx);
13941 for (i = 0; i < nargs; i++)
13942 fold_checksum_tree (argarray[i], &ctx, &ht);
13943 md5_finish_ctx (&ctx, checksum_before_arglist);
13944 ht.empty ();
13945 #endif
13947 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13948 if (!tem)
13949 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
13951 #ifdef ENABLE_FOLD_CHECKING
13952 md5_init_ctx (&ctx);
13953 fold_checksum_tree (fn, &ctx, &ht);
13954 md5_finish_ctx (&ctx, checksum_after_fn);
13955 ht.empty ();
13957 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13958 fold_check_failed (fn, tem);
13960 md5_init_ctx (&ctx);
13961 for (i = 0; i < nargs; i++)
13962 fold_checksum_tree (argarray[i], &ctx, &ht);
13963 md5_finish_ctx (&ctx, checksum_after_arglist);
13965 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13966 fold_check_failed (NULL_TREE, tem);
13967 #endif
13968 return tem;
13971 /* Perform constant folding and related simplification of initializer
13972 expression EXPR. These behave identically to "fold_buildN" but ignore
13973 potential run-time traps and exceptions that fold must preserve. */
13975 #define START_FOLD_INIT \
13976 int saved_signaling_nans = flag_signaling_nans;\
13977 int saved_trapping_math = flag_trapping_math;\
13978 int saved_rounding_math = flag_rounding_math;\
13979 int saved_trapv = flag_trapv;\
13980 int saved_folding_initializer = folding_initializer;\
13981 flag_signaling_nans = 0;\
13982 flag_trapping_math = 0;\
13983 flag_rounding_math = 0;\
13984 flag_trapv = 0;\
13985 folding_initializer = 1;
13987 #define END_FOLD_INIT \
13988 flag_signaling_nans = saved_signaling_nans;\
13989 flag_trapping_math = saved_trapping_math;\
13990 flag_rounding_math = saved_rounding_math;\
13991 flag_trapv = saved_trapv;\
13992 folding_initializer = saved_folding_initializer;
13994 tree
13995 fold_init (tree expr)
13997 tree result;
13998 START_FOLD_INIT;
14000 result = fold (expr);
14002 END_FOLD_INIT;
14003 return result;
14006 tree
14007 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14008 tree type, tree op)
14010 tree result;
14011 START_FOLD_INIT;
14013 result = fold_build1_loc (loc, code, type, op);
14015 END_FOLD_INIT;
14016 return result;
14019 tree
14020 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14021 tree type, tree op0, tree op1)
14023 tree result;
14024 START_FOLD_INIT;
14026 result = fold_build2_loc (loc, code, type, op0, op1);
14028 END_FOLD_INIT;
14029 return result;
14032 tree
14033 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14034 int nargs, tree *argarray)
14036 tree result;
14037 START_FOLD_INIT;
14039 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14041 END_FOLD_INIT;
14042 return result;
14045 tree
14046 fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
14047 tree lhs, tree rhs)
14049 tree result;
14050 START_FOLD_INIT;
14052 result = fold_binary_loc (loc, code, type, lhs, rhs);
14054 END_FOLD_INIT;
14055 return result;
14058 #undef START_FOLD_INIT
14059 #undef END_FOLD_INIT
14061 /* Determine if first argument is a multiple of second argument. Return 0 if
14062 it is not, or we cannot easily determined it to be.
14064 An example of the sort of thing we care about (at this point; this routine
14065 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14066 fold cases do now) is discovering that
14068 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14070 is a multiple of
14072 SAVE_EXPR (J * 8)
14074 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14076 This code also handles discovering that
14078 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14080 is a multiple of 8 so we don't have to worry about dealing with a
14081 possible remainder.
14083 Note that we *look* inside a SAVE_EXPR only to determine how it was
14084 calculated; it is not safe for fold to do much of anything else with the
14085 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14086 at run time. For example, the latter example above *cannot* be implemented
14087 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14088 evaluation time of the original SAVE_EXPR is not necessarily the same at
14089 the time the new expression is evaluated. The only optimization of this
14090 sort that would be valid is changing
14092 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14094 divided by 8 to
14096 SAVE_EXPR (I) * SAVE_EXPR (J)
14098 (where the same SAVE_EXPR (J) is used in the original and the
14099 transformed version).
14101 NOWRAP specifies whether all outer operations in TYPE should
14102 be considered not wrapping. Any type conversion within TOP acts
14103 as a barrier and we will fall back to NOWRAP being false.
14104 NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14105 as not wrapping even though they are generally using unsigned arithmetic. */
14108 multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14110 gimple *stmt;
14111 tree op1, op2;
14113 if (operand_equal_p (top, bottom, 0))
14114 return 1;
14116 if (TREE_CODE (type) != INTEGER_TYPE)
14117 return 0;
14119 switch (TREE_CODE (top))
14121 case BIT_AND_EXPR:
14122 /* Bitwise and provides a power of two multiple. If the mask is
14123 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14124 if (!integer_pow2p (bottom))
14125 return 0;
14126 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14127 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14129 case MULT_EXPR:
14130 /* If the multiplication can wrap we cannot recurse further unless
14131 the bottom is a power of two which is where wrapping does not
14132 matter. */
14133 if (!nowrap
14134 && !TYPE_OVERFLOW_UNDEFINED (type)
14135 && !integer_pow2p (bottom))
14136 return 0;
14137 if (TREE_CODE (bottom) == INTEGER_CST)
14139 op1 = TREE_OPERAND (top, 0);
14140 op2 = TREE_OPERAND (top, 1);
14141 if (TREE_CODE (op1) == INTEGER_CST)
14142 std::swap (op1, op2);
14143 if (TREE_CODE (op2) == INTEGER_CST)
14145 if (multiple_of_p (type, op2, bottom, nowrap))
14146 return 1;
14147 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14148 if (multiple_of_p (type, bottom, op2, nowrap))
14150 widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14151 wi::to_widest (op2));
14152 if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14154 op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14155 return multiple_of_p (type, op1, op2, nowrap);
14158 return multiple_of_p (type, op1, bottom, nowrap);
14161 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14162 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14164 case LSHIFT_EXPR:
14165 /* Handle X << CST as X * (1 << CST) and only process the constant. */
14166 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14168 op1 = TREE_OPERAND (top, 1);
14169 if (wi::to_widest (op1) < TYPE_PRECISION (type))
14171 wide_int mul_op
14172 = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
14173 return multiple_of_p (type,
14174 wide_int_to_tree (type, mul_op), bottom,
14175 nowrap);
14178 return 0;
14180 case MINUS_EXPR:
14181 case PLUS_EXPR:
14182 /* If the addition or subtraction can wrap we cannot recurse further
14183 unless bottom is a power of two which is where wrapping does not
14184 matter. */
14185 if (!nowrap
14186 && !TYPE_OVERFLOW_UNDEFINED (type)
14187 && !integer_pow2p (bottom))
14188 return 0;
14190 /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14191 unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14192 but 0xfffffffd is not. */
14193 op1 = TREE_OPERAND (top, 1);
14194 if (TREE_CODE (top) == PLUS_EXPR
14195 && nowrap
14196 && TYPE_UNSIGNED (type)
14197 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14198 op1 = fold_build1 (NEGATE_EXPR, type, op1);
14200 /* It is impossible to prove if op0 +- op1 is multiple of bottom
14201 precisely, so be conservative here checking if both op0 and op1
14202 are multiple of bottom. Note we check the second operand first
14203 since it's usually simpler. */
14204 return (multiple_of_p (type, op1, bottom, nowrap)
14205 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14207 CASE_CONVERT:
14208 /* Can't handle conversions from non-integral or wider integral type. */
14209 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14210 || (TYPE_PRECISION (type)
14211 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14212 return 0;
14213 /* NOWRAP only extends to operations in the outermost type so
14214 make sure to strip it off here. */
14215 return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14216 TREE_OPERAND (top, 0), bottom, false);
14218 case SAVE_EXPR:
14219 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14221 case COND_EXPR:
14222 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14223 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14225 case INTEGER_CST:
14226 if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14227 return 0;
14228 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14229 SIGNED);
14231 case SSA_NAME:
14232 if (TREE_CODE (bottom) == INTEGER_CST
14233 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14234 && gimple_code (stmt) == GIMPLE_ASSIGN)
14236 enum tree_code code = gimple_assign_rhs_code (stmt);
14238 /* Check for special cases to see if top is defined as multiple
14239 of bottom:
14241 top = (X & ~(bottom - 1) ; bottom is power of 2
14245 Y = X % bottom
14246 top = X - Y. */
14247 if (code == BIT_AND_EXPR
14248 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14249 && TREE_CODE (op2) == INTEGER_CST
14250 && integer_pow2p (bottom)
14251 && wi::multiple_of_p (wi::to_widest (op2),
14252 wi::to_widest (bottom), UNSIGNED))
14253 return 1;
14255 op1 = gimple_assign_rhs1 (stmt);
14256 if (code == MINUS_EXPR
14257 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14258 && TREE_CODE (op2) == SSA_NAME
14259 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14260 && gimple_code (stmt) == GIMPLE_ASSIGN
14261 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14262 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14263 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14264 return 1;
14267 /* fall through */
14269 default:
14270 if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14271 return multiple_p (wi::to_poly_widest (top),
14272 wi::to_poly_widest (bottom));
14274 return 0;
14278 /* Return true if expression X cannot be (or contain) a NaN or infinity.
14279 This function returns true for integer expressions, and returns
14280 false if uncertain. */
14282 bool
14283 tree_expr_finite_p (const_tree x)
14285 machine_mode mode = element_mode (x);
14286 if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14287 return true;
14288 switch (TREE_CODE (x))
14290 case REAL_CST:
14291 return real_isfinite (TREE_REAL_CST_PTR (x));
14292 case COMPLEX_CST:
14293 return tree_expr_finite_p (TREE_REALPART (x))
14294 && tree_expr_finite_p (TREE_IMAGPART (x));
14295 case FLOAT_EXPR:
14296 return true;
14297 case ABS_EXPR:
14298 case CONVERT_EXPR:
14299 case NON_LVALUE_EXPR:
14300 case NEGATE_EXPR:
14301 case SAVE_EXPR:
14302 return tree_expr_finite_p (TREE_OPERAND (x, 0));
14303 case MIN_EXPR:
14304 case MAX_EXPR:
14305 return tree_expr_finite_p (TREE_OPERAND (x, 0))
14306 && tree_expr_finite_p (TREE_OPERAND (x, 1));
14307 case COND_EXPR:
14308 return tree_expr_finite_p (TREE_OPERAND (x, 1))
14309 && tree_expr_finite_p (TREE_OPERAND (x, 2));
14310 case CALL_EXPR:
14311 switch (get_call_combined_fn (x))
14313 CASE_CFN_FABS:
14314 CASE_CFN_FABS_FN:
14315 return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14316 CASE_CFN_FMAX:
14317 CASE_CFN_FMAX_FN:
14318 CASE_CFN_FMIN:
14319 CASE_CFN_FMIN_FN:
14320 return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14321 && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14322 default:
14323 return false;
14326 default:
14327 return false;
14331 /* Return true if expression X evaluates to an infinity.
14332 This function returns false for integer expressions. */
14334 bool
14335 tree_expr_infinite_p (const_tree x)
14337 if (!HONOR_INFINITIES (x))
14338 return false;
14339 switch (TREE_CODE (x))
14341 case REAL_CST:
14342 return real_isinf (TREE_REAL_CST_PTR (x));
14343 case ABS_EXPR:
14344 case NEGATE_EXPR:
14345 case NON_LVALUE_EXPR:
14346 case SAVE_EXPR:
14347 return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14348 case COND_EXPR:
14349 return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14350 && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14351 default:
14352 return false;
14356 /* Return true if expression X could evaluate to an infinity.
14357 This function returns false for integer expressions, and returns
14358 true if uncertain. */
14360 bool
14361 tree_expr_maybe_infinite_p (const_tree x)
14363 if (!HONOR_INFINITIES (x))
14364 return false;
14365 switch (TREE_CODE (x))
14367 case REAL_CST:
14368 return real_isinf (TREE_REAL_CST_PTR (x));
14369 case FLOAT_EXPR:
14370 return false;
14371 case ABS_EXPR:
14372 case NEGATE_EXPR:
14373 return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14374 case COND_EXPR:
14375 return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14376 || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14377 default:
14378 return true;
14382 /* Return true if expression X evaluates to a signaling NaN.
14383 This function returns false for integer expressions. */
14385 bool
14386 tree_expr_signaling_nan_p (const_tree x)
14388 if (!HONOR_SNANS (x))
14389 return false;
14390 switch (TREE_CODE (x))
14392 case REAL_CST:
14393 return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14394 case NON_LVALUE_EXPR:
14395 case SAVE_EXPR:
14396 return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14397 case COND_EXPR:
14398 return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14399 && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14400 default:
14401 return false;
14405 /* Return true if expression X could evaluate to a signaling NaN.
14406 This function returns false for integer expressions, and returns
14407 true if uncertain. */
14409 bool
14410 tree_expr_maybe_signaling_nan_p (const_tree x)
14412 if (!HONOR_SNANS (x))
14413 return false;
14414 switch (TREE_CODE (x))
14416 case REAL_CST:
14417 return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14418 case FLOAT_EXPR:
14419 return false;
14420 case ABS_EXPR:
14421 case CONVERT_EXPR:
14422 case NEGATE_EXPR:
14423 case NON_LVALUE_EXPR:
14424 case SAVE_EXPR:
14425 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14426 case MIN_EXPR:
14427 case MAX_EXPR:
14428 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0))
14429 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1));
14430 case COND_EXPR:
14431 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14432 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14433 case CALL_EXPR:
14434 switch (get_call_combined_fn (x))
14436 CASE_CFN_FABS:
14437 CASE_CFN_FABS_FN:
14438 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14439 CASE_CFN_FMAX:
14440 CASE_CFN_FMAX_FN:
14441 CASE_CFN_FMIN:
14442 CASE_CFN_FMIN_FN:
14443 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0))
14444 || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 1));
14445 default:
14446 return true;
14448 default:
14449 return true;
14453 /* Return true if expression X evaluates to a NaN.
14454 This function returns false for integer expressions. */
14456 bool
14457 tree_expr_nan_p (const_tree x)
14459 if (!HONOR_NANS (x))
14460 return false;
14461 switch (TREE_CODE (x))
14463 case REAL_CST:
14464 return real_isnan (TREE_REAL_CST_PTR (x));
14465 case NON_LVALUE_EXPR:
14466 case SAVE_EXPR:
14467 return tree_expr_nan_p (TREE_OPERAND (x, 0));
14468 case COND_EXPR:
14469 return tree_expr_nan_p (TREE_OPERAND (x, 1))
14470 && tree_expr_nan_p (TREE_OPERAND (x, 2));
14471 default:
14472 return false;
14476 /* Return true if expression X could evaluate to a NaN.
14477 This function returns false for integer expressions, and returns
14478 true if uncertain. */
14480 bool
14481 tree_expr_maybe_nan_p (const_tree x)
14483 if (!HONOR_NANS (x))
14484 return false;
14485 switch (TREE_CODE (x))
14487 case REAL_CST:
14488 return real_isnan (TREE_REAL_CST_PTR (x));
14489 case FLOAT_EXPR:
14490 return false;
14491 case PLUS_EXPR:
14492 case MINUS_EXPR:
14493 case MULT_EXPR:
14494 return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14495 || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14496 case ABS_EXPR:
14497 case CONVERT_EXPR:
14498 case NEGATE_EXPR:
14499 case NON_LVALUE_EXPR:
14500 case SAVE_EXPR:
14501 return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14502 case MIN_EXPR:
14503 case MAX_EXPR:
14504 return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14505 || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14506 case COND_EXPR:
14507 return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14508 || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14509 case CALL_EXPR:
14510 switch (get_call_combined_fn (x))
14512 CASE_CFN_FABS:
14513 CASE_CFN_FABS_FN:
14514 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14515 CASE_CFN_FMAX:
14516 CASE_CFN_FMAX_FN:
14517 CASE_CFN_FMIN:
14518 CASE_CFN_FMIN_FN:
14519 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14520 || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14521 default:
14522 return true;
14524 default:
14525 return true;
14529 /* Return true if expression X could evaluate to -0.0.
14530 This function returns true if uncertain. */
14532 bool
14533 tree_expr_maybe_real_minus_zero_p (const_tree x)
14535 if (!HONOR_SIGNED_ZEROS (x))
14536 return false;
14537 switch (TREE_CODE (x))
14539 case REAL_CST:
14540 return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14541 case INTEGER_CST:
14542 case FLOAT_EXPR:
14543 case ABS_EXPR:
14544 return false;
14545 case NON_LVALUE_EXPR:
14546 case SAVE_EXPR:
14547 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14548 case COND_EXPR:
14549 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14550 || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14551 case CALL_EXPR:
14552 switch (get_call_combined_fn (x))
14554 CASE_CFN_FABS:
14555 CASE_CFN_FABS_FN:
14556 return false;
14557 default:
14558 break;
14560 default:
14561 break;
14563 /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14564 * but currently those predicates require tree and not const_tree. */
14565 return true;
14568 #define tree_expr_nonnegative_warnv_p(X, Y) \
14569 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14571 #define RECURSE(X) \
14572 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
14574 /* Return true if CODE or TYPE is known to be non-negative. */
14576 static bool
14577 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14579 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14580 && truth_value_p (code))
14581 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14582 have a signed:1 type (where the value is -1 and 0). */
14583 return true;
14584 return false;
14587 /* Return true if (CODE OP0) is known to be non-negative. If the return
14588 value is based on the assumption that signed overflow is undefined,
14589 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14590 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14592 bool
14593 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14594 bool *strict_overflow_p, int depth)
14596 if (TYPE_UNSIGNED (type))
14597 return true;
14599 switch (code)
14601 case ABS_EXPR:
14602 /* We can't return 1 if flag_wrapv is set because
14603 ABS_EXPR<INT_MIN> = INT_MIN. */
14604 if (!ANY_INTEGRAL_TYPE_P (type))
14605 return true;
14606 if (TYPE_OVERFLOW_UNDEFINED (type))
14608 *strict_overflow_p = true;
14609 return true;
14611 break;
14613 case NON_LVALUE_EXPR:
14614 case FLOAT_EXPR:
14615 case FIX_TRUNC_EXPR:
14616 return RECURSE (op0);
14618 CASE_CONVERT:
14620 tree inner_type = TREE_TYPE (op0);
14621 tree outer_type = type;
14623 if (TREE_CODE (outer_type) == REAL_TYPE)
14625 if (TREE_CODE (inner_type) == REAL_TYPE)
14626 return RECURSE (op0);
14627 if (INTEGRAL_TYPE_P (inner_type))
14629 if (TYPE_UNSIGNED (inner_type))
14630 return true;
14631 return RECURSE (op0);
14634 else if (INTEGRAL_TYPE_P (outer_type))
14636 if (TREE_CODE (inner_type) == REAL_TYPE)
14637 return RECURSE (op0);
14638 if (INTEGRAL_TYPE_P (inner_type))
14639 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14640 && TYPE_UNSIGNED (inner_type);
14643 break;
14645 default:
14646 return tree_simple_nonnegative_warnv_p (code, type);
14649 /* We don't know sign of `t', so be conservative and return false. */
14650 return false;
14653 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14654 value is based on the assumption that signed overflow is undefined,
14655 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14656 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14658 bool
14659 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14660 tree op1, bool *strict_overflow_p,
14661 int depth)
14663 if (TYPE_UNSIGNED (type))
14664 return true;
14666 switch (code)
14668 case POINTER_PLUS_EXPR:
14669 case PLUS_EXPR:
14670 if (FLOAT_TYPE_P (type))
14671 return RECURSE (op0) && RECURSE (op1);
14673 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14674 both unsigned and at least 2 bits shorter than the result. */
14675 if (TREE_CODE (type) == INTEGER_TYPE
14676 && TREE_CODE (op0) == NOP_EXPR
14677 && TREE_CODE (op1) == NOP_EXPR)
14679 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14680 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14681 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14682 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14684 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14685 TYPE_PRECISION (inner2)) + 1;
14686 return prec < TYPE_PRECISION (type);
14689 break;
14691 case MULT_EXPR:
14692 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14694 /* x * x is always non-negative for floating point x
14695 or without overflow. */
14696 if (operand_equal_p (op0, op1, 0)
14697 || (RECURSE (op0) && RECURSE (op1)))
14699 if (ANY_INTEGRAL_TYPE_P (type)
14700 && TYPE_OVERFLOW_UNDEFINED (type))
14701 *strict_overflow_p = true;
14702 return true;
14706 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14707 both unsigned and their total bits is shorter than the result. */
14708 if (TREE_CODE (type) == INTEGER_TYPE
14709 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14710 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14712 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14713 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14714 : TREE_TYPE (op0);
14715 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14716 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14717 : TREE_TYPE (op1);
14719 bool unsigned0 = TYPE_UNSIGNED (inner0);
14720 bool unsigned1 = TYPE_UNSIGNED (inner1);
14722 if (TREE_CODE (op0) == INTEGER_CST)
14723 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14725 if (TREE_CODE (op1) == INTEGER_CST)
14726 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14728 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14729 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14731 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14732 ? tree_int_cst_min_precision (op0, UNSIGNED)
14733 : TYPE_PRECISION (inner0);
14735 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14736 ? tree_int_cst_min_precision (op1, UNSIGNED)
14737 : TYPE_PRECISION (inner1);
14739 return precision0 + precision1 < TYPE_PRECISION (type);
14742 return false;
14744 case BIT_AND_EXPR:
14745 return RECURSE (op0) || RECURSE (op1);
14747 case MAX_EXPR:
14748 /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14749 things. */
14750 if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14751 return RECURSE (op0) && RECURSE (op1);
14752 return RECURSE (op0) || RECURSE (op1);
14754 case BIT_IOR_EXPR:
14755 case BIT_XOR_EXPR:
14756 case MIN_EXPR:
14757 case RDIV_EXPR:
14758 case TRUNC_DIV_EXPR:
14759 case CEIL_DIV_EXPR:
14760 case FLOOR_DIV_EXPR:
14761 case ROUND_DIV_EXPR:
14762 return RECURSE (op0) && RECURSE (op1);
14764 case TRUNC_MOD_EXPR:
14765 return RECURSE (op0);
14767 case FLOOR_MOD_EXPR:
14768 return RECURSE (op1);
14770 case CEIL_MOD_EXPR:
14771 case ROUND_MOD_EXPR:
14772 default:
14773 return tree_simple_nonnegative_warnv_p (code, type);
14776 /* We don't know sign of `t', so be conservative and return false. */
14777 return false;
14780 /* Return true if T is known to be non-negative. If the return
14781 value is based on the assumption that signed overflow is undefined,
14782 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14783 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14785 bool
14786 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
14788 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14789 return true;
14791 switch (TREE_CODE (t))
14793 case INTEGER_CST:
14794 return tree_int_cst_sgn (t) >= 0;
14796 case REAL_CST:
14797 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14799 case FIXED_CST:
14800 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14802 case COND_EXPR:
14803 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14805 case SSA_NAME:
14806 /* Limit the depth of recursion to avoid quadratic behavior.
14807 This is expected to catch almost all occurrences in practice.
14808 If this code misses important cases that unbounded recursion
14809 would not, passes that need this information could be revised
14810 to provide it through dataflow propagation. */
14811 return (!name_registered_for_update_p (t)
14812 && depth < param_max_ssa_name_query_depth
14813 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
14814 strict_overflow_p, depth));
14816 default:
14817 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14821 /* Return true if T is known to be non-negative. If the return
14822 value is based on the assumption that signed overflow is undefined,
14823 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14824 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14826 bool
14827 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
14828 bool *strict_overflow_p, int depth)
14830 switch (fn)
14832 CASE_CFN_ACOS:
14833 CASE_CFN_ACOS_FN:
14834 CASE_CFN_ACOSH:
14835 CASE_CFN_ACOSH_FN:
14836 CASE_CFN_CABS:
14837 CASE_CFN_CABS_FN:
14838 CASE_CFN_COSH:
14839 CASE_CFN_COSH_FN:
14840 CASE_CFN_ERFC:
14841 CASE_CFN_ERFC_FN:
14842 CASE_CFN_EXP:
14843 CASE_CFN_EXP_FN:
14844 CASE_CFN_EXP10:
14845 CASE_CFN_EXP2:
14846 CASE_CFN_EXP2_FN:
14847 CASE_CFN_FABS:
14848 CASE_CFN_FABS_FN:
14849 CASE_CFN_FDIM:
14850 CASE_CFN_FDIM_FN:
14851 CASE_CFN_HYPOT:
14852 CASE_CFN_HYPOT_FN:
14853 CASE_CFN_POW10:
14854 CASE_CFN_FFS:
14855 CASE_CFN_PARITY:
14856 CASE_CFN_POPCOUNT:
14857 CASE_CFN_CLZ:
14858 CASE_CFN_CLRSB:
14859 case CFN_BUILT_IN_BSWAP16:
14860 case CFN_BUILT_IN_BSWAP32:
14861 case CFN_BUILT_IN_BSWAP64:
14862 case CFN_BUILT_IN_BSWAP128:
14863 /* Always true. */
14864 return true;
14866 CASE_CFN_SQRT:
14867 CASE_CFN_SQRT_FN:
14868 /* sqrt(-0.0) is -0.0. */
14869 if (!HONOR_SIGNED_ZEROS (type))
14870 return true;
14871 return RECURSE (arg0);
14873 CASE_CFN_ASINH:
14874 CASE_CFN_ASINH_FN:
14875 CASE_CFN_ATAN:
14876 CASE_CFN_ATAN_FN:
14877 CASE_CFN_ATANH:
14878 CASE_CFN_ATANH_FN:
14879 CASE_CFN_CBRT:
14880 CASE_CFN_CBRT_FN:
14881 CASE_CFN_CEIL:
14882 CASE_CFN_CEIL_FN:
14883 CASE_CFN_ERF:
14884 CASE_CFN_ERF_FN:
14885 CASE_CFN_EXPM1:
14886 CASE_CFN_EXPM1_FN:
14887 CASE_CFN_FLOOR:
14888 CASE_CFN_FLOOR_FN:
14889 CASE_CFN_FMOD:
14890 CASE_CFN_FMOD_FN:
14891 CASE_CFN_FREXP:
14892 CASE_CFN_FREXP_FN:
14893 CASE_CFN_ICEIL:
14894 CASE_CFN_IFLOOR:
14895 CASE_CFN_IRINT:
14896 CASE_CFN_IROUND:
14897 CASE_CFN_LCEIL:
14898 CASE_CFN_LDEXP:
14899 CASE_CFN_LFLOOR:
14900 CASE_CFN_LLCEIL:
14901 CASE_CFN_LLFLOOR:
14902 CASE_CFN_LLRINT:
14903 CASE_CFN_LLRINT_FN:
14904 CASE_CFN_LLROUND:
14905 CASE_CFN_LLROUND_FN:
14906 CASE_CFN_LRINT:
14907 CASE_CFN_LRINT_FN:
14908 CASE_CFN_LROUND:
14909 CASE_CFN_LROUND_FN:
14910 CASE_CFN_MODF:
14911 CASE_CFN_MODF_FN:
14912 CASE_CFN_NEARBYINT:
14913 CASE_CFN_NEARBYINT_FN:
14914 CASE_CFN_RINT:
14915 CASE_CFN_RINT_FN:
14916 CASE_CFN_ROUND:
14917 CASE_CFN_ROUND_FN:
14918 CASE_CFN_ROUNDEVEN:
14919 CASE_CFN_ROUNDEVEN_FN:
14920 CASE_CFN_SCALB:
14921 CASE_CFN_SCALBLN:
14922 CASE_CFN_SCALBLN_FN:
14923 CASE_CFN_SCALBN:
14924 CASE_CFN_SCALBN_FN:
14925 CASE_CFN_SIGNBIT:
14926 CASE_CFN_SIGNIFICAND:
14927 CASE_CFN_SINH:
14928 CASE_CFN_SINH_FN:
14929 CASE_CFN_TANH:
14930 CASE_CFN_TANH_FN:
14931 CASE_CFN_TRUNC:
14932 CASE_CFN_TRUNC_FN:
14933 /* True if the 1st argument is nonnegative. */
14934 return RECURSE (arg0);
14936 CASE_CFN_FMAX:
14937 CASE_CFN_FMAX_FN:
14938 /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
14939 things. In the presence of sNaNs, we're only guaranteed to be
14940 non-negative if both operands are non-negative. In the presence
14941 of qNaNs, we're non-negative if either operand is non-negative
14942 and can't be a qNaN, or if both operands are non-negative. */
14943 if (tree_expr_maybe_signaling_nan_p (arg0) ||
14944 tree_expr_maybe_signaling_nan_p (arg1))
14945 return RECURSE (arg0) && RECURSE (arg1);
14946 return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
14947 || RECURSE (arg1))
14948 : (RECURSE (arg1)
14949 && !tree_expr_maybe_nan_p (arg1));
14951 CASE_CFN_FMIN:
14952 CASE_CFN_FMIN_FN:
14953 /* True if the 1st AND 2nd arguments are nonnegative. */
14954 return RECURSE (arg0) && RECURSE (arg1);
14956 CASE_CFN_COPYSIGN:
14957 CASE_CFN_COPYSIGN_FN:
14958 /* True if the 2nd argument is nonnegative. */
14959 return RECURSE (arg1);
14961 CASE_CFN_POWI:
14962 /* True if the 1st argument is nonnegative or the second
14963 argument is an even integer. */
14964 if (TREE_CODE (arg1) == INTEGER_CST
14965 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14966 return true;
14967 return RECURSE (arg0);
14969 CASE_CFN_POW:
14970 CASE_CFN_POW_FN:
14971 /* True if the 1st argument is nonnegative or the second
14972 argument is an even integer valued real. */
14973 if (TREE_CODE (arg1) == REAL_CST)
14975 REAL_VALUE_TYPE c;
14976 HOST_WIDE_INT n;
14978 c = TREE_REAL_CST (arg1);
14979 n = real_to_integer (&c);
14980 if ((n & 1) == 0)
14982 REAL_VALUE_TYPE cint;
14983 real_from_integer (&cint, VOIDmode, n, SIGNED);
14984 if (real_identical (&c, &cint))
14985 return true;
14988 return RECURSE (arg0);
14990 default:
14991 break;
14993 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
14996 /* Return true if T is known to be non-negative. If the return
14997 value is based on the assumption that signed overflow is undefined,
14998 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14999 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15001 static bool
15002 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15004 enum tree_code code = TREE_CODE (t);
15005 if (TYPE_UNSIGNED (TREE_TYPE (t)))
15006 return true;
15008 switch (code)
15010 case TARGET_EXPR:
15012 tree temp = TARGET_EXPR_SLOT (t);
15013 t = TARGET_EXPR_INITIAL (t);
15015 /* If the initializer is non-void, then it's a normal expression
15016 that will be assigned to the slot. */
15017 if (!VOID_TYPE_P (TREE_TYPE (t)))
15018 return RECURSE (t);
15020 /* Otherwise, the initializer sets the slot in some way. One common
15021 way is an assignment statement at the end of the initializer. */
15022 while (1)
15024 if (TREE_CODE (t) == BIND_EXPR)
15025 t = expr_last (BIND_EXPR_BODY (t));
15026 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15027 || TREE_CODE (t) == TRY_CATCH_EXPR)
15028 t = expr_last (TREE_OPERAND (t, 0));
15029 else if (TREE_CODE (t) == STATEMENT_LIST)
15030 t = expr_last (t);
15031 else
15032 break;
15034 if (TREE_CODE (t) == MODIFY_EXPR
15035 && TREE_OPERAND (t, 0) == temp)
15036 return RECURSE (TREE_OPERAND (t, 1));
15038 return false;
15041 case CALL_EXPR:
15043 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15044 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15046 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15047 get_call_combined_fn (t),
15048 arg0,
15049 arg1,
15050 strict_overflow_p, depth);
15052 case COMPOUND_EXPR:
15053 case MODIFY_EXPR:
15054 return RECURSE (TREE_OPERAND (t, 1));
15056 case BIND_EXPR:
15057 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
15059 case SAVE_EXPR:
15060 return RECURSE (TREE_OPERAND (t, 0));
15062 default:
15063 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
15067 #undef RECURSE
15068 #undef tree_expr_nonnegative_warnv_p
15070 /* Return true if T is known to be non-negative. If the return
15071 value is based on the assumption that signed overflow is undefined,
15072 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15073 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15075 bool
15076 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15078 enum tree_code code;
15079 if (t == error_mark_node)
15080 return false;
15082 code = TREE_CODE (t);
15083 switch (TREE_CODE_CLASS (code))
15085 case tcc_binary:
15086 case tcc_comparison:
15087 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15088 TREE_TYPE (t),
15089 TREE_OPERAND (t, 0),
15090 TREE_OPERAND (t, 1),
15091 strict_overflow_p, depth);
15093 case tcc_unary:
15094 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15095 TREE_TYPE (t),
15096 TREE_OPERAND (t, 0),
15097 strict_overflow_p, depth);
15099 case tcc_constant:
15100 case tcc_declaration:
15101 case tcc_reference:
15102 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15104 default:
15105 break;
15108 switch (code)
15110 case TRUTH_AND_EXPR:
15111 case TRUTH_OR_EXPR:
15112 case TRUTH_XOR_EXPR:
15113 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15114 TREE_TYPE (t),
15115 TREE_OPERAND (t, 0),
15116 TREE_OPERAND (t, 1),
15117 strict_overflow_p, depth);
15118 case TRUTH_NOT_EXPR:
15119 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15120 TREE_TYPE (t),
15121 TREE_OPERAND (t, 0),
15122 strict_overflow_p, depth);
15124 case COND_EXPR:
15125 case CONSTRUCTOR:
15126 case OBJ_TYPE_REF:
15127 case ADDR_EXPR:
15128 case WITH_SIZE_EXPR:
15129 case SSA_NAME:
15130 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15132 default:
15133 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
15137 /* Return true if `t' is known to be non-negative. Handle warnings
15138 about undefined signed overflow. */
15140 bool
15141 tree_expr_nonnegative_p (tree t)
15143 bool ret, strict_overflow_p;
15145 strict_overflow_p = false;
15146 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15147 if (strict_overflow_p)
15148 fold_overflow_warning (("assuming signed overflow does not occur when "
15149 "determining that expression is always "
15150 "non-negative"),
15151 WARN_STRICT_OVERFLOW_MISC);
15152 return ret;
15156 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15157 For floating point we further ensure that T is not denormal.
15158 Similar logic is present in nonzero_address in rtlanal.h.
15160 If the return value is based on the assumption that signed overflow
15161 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15162 change *STRICT_OVERFLOW_P. */
15164 bool
15165 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15166 bool *strict_overflow_p)
15168 switch (code)
15170 case ABS_EXPR:
15171 return tree_expr_nonzero_warnv_p (op0,
15172 strict_overflow_p);
15174 case NOP_EXPR:
15176 tree inner_type = TREE_TYPE (op0);
15177 tree outer_type = type;
15179 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15180 && tree_expr_nonzero_warnv_p (op0,
15181 strict_overflow_p));
15183 break;
15185 case NON_LVALUE_EXPR:
15186 return tree_expr_nonzero_warnv_p (op0,
15187 strict_overflow_p);
15189 default:
15190 break;
15193 return false;
15196 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15197 For floating point we further ensure that T is not denormal.
15198 Similar logic is present in nonzero_address in rtlanal.h.
15200 If the return value is based on the assumption that signed overflow
15201 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15202 change *STRICT_OVERFLOW_P. */
15204 bool
15205 tree_binary_nonzero_warnv_p (enum tree_code code,
15206 tree type,
15207 tree op0,
15208 tree op1, bool *strict_overflow_p)
15210 bool sub_strict_overflow_p;
15211 switch (code)
15213 case POINTER_PLUS_EXPR:
15214 case PLUS_EXPR:
15215 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15217 /* With the presence of negative values it is hard
15218 to say something. */
15219 sub_strict_overflow_p = false;
15220 if (!tree_expr_nonnegative_warnv_p (op0,
15221 &sub_strict_overflow_p)
15222 || !tree_expr_nonnegative_warnv_p (op1,
15223 &sub_strict_overflow_p))
15224 return false;
15225 /* One of operands must be positive and the other non-negative. */
15226 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15227 overflows, on a twos-complement machine the sum of two
15228 nonnegative numbers can never be zero. */
15229 return (tree_expr_nonzero_warnv_p (op0,
15230 strict_overflow_p)
15231 || tree_expr_nonzero_warnv_p (op1,
15232 strict_overflow_p));
15234 break;
15236 case MULT_EXPR:
15237 if (TYPE_OVERFLOW_UNDEFINED (type))
15239 if (tree_expr_nonzero_warnv_p (op0,
15240 strict_overflow_p)
15241 && tree_expr_nonzero_warnv_p (op1,
15242 strict_overflow_p))
15244 *strict_overflow_p = true;
15245 return true;
15248 break;
15250 case MIN_EXPR:
15251 sub_strict_overflow_p = false;
15252 if (tree_expr_nonzero_warnv_p (op0,
15253 &sub_strict_overflow_p)
15254 && tree_expr_nonzero_warnv_p (op1,
15255 &sub_strict_overflow_p))
15257 if (sub_strict_overflow_p)
15258 *strict_overflow_p = true;
15260 break;
15262 case MAX_EXPR:
15263 sub_strict_overflow_p = false;
15264 if (tree_expr_nonzero_warnv_p (op0,
15265 &sub_strict_overflow_p))
15267 if (sub_strict_overflow_p)
15268 *strict_overflow_p = true;
15270 /* When both operands are nonzero, then MAX must be too. */
15271 if (tree_expr_nonzero_warnv_p (op1,
15272 strict_overflow_p))
15273 return true;
15275 /* MAX where operand 0 is positive is positive. */
15276 return tree_expr_nonnegative_warnv_p (op0,
15277 strict_overflow_p);
15279 /* MAX where operand 1 is positive is positive. */
15280 else if (tree_expr_nonzero_warnv_p (op1,
15281 &sub_strict_overflow_p)
15282 && tree_expr_nonnegative_warnv_p (op1,
15283 &sub_strict_overflow_p))
15285 if (sub_strict_overflow_p)
15286 *strict_overflow_p = true;
15287 return true;
15289 break;
15291 case BIT_IOR_EXPR:
15292 return (tree_expr_nonzero_warnv_p (op1,
15293 strict_overflow_p)
15294 || tree_expr_nonzero_warnv_p (op0,
15295 strict_overflow_p));
15297 default:
15298 break;
15301 return false;
15304 /* Return true when T is an address and is known to be nonzero.
15305 For floating point we further ensure that T is not denormal.
15306 Similar logic is present in nonzero_address in rtlanal.h.
15308 If the return value is based on the assumption that signed overflow
15309 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15310 change *STRICT_OVERFLOW_P. */
15312 bool
15313 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15315 bool sub_strict_overflow_p;
15316 switch (TREE_CODE (t))
15318 case INTEGER_CST:
15319 return !integer_zerop (t);
15321 case ADDR_EXPR:
15323 tree base = TREE_OPERAND (t, 0);
15325 if (!DECL_P (base))
15326 base = get_base_address (base);
15328 if (base && TREE_CODE (base) == TARGET_EXPR)
15329 base = TARGET_EXPR_SLOT (base);
15331 if (!base)
15332 return false;
15334 /* For objects in symbol table check if we know they are non-zero.
15335 Don't do anything for variables and functions before symtab is built;
15336 it is quite possible that they will be declared weak later. */
15337 int nonzero_addr = maybe_nonzero_address (base);
15338 if (nonzero_addr >= 0)
15339 return nonzero_addr;
15341 /* Constants are never weak. */
15342 if (CONSTANT_CLASS_P (base))
15343 return true;
15345 return false;
15348 case COND_EXPR:
15349 sub_strict_overflow_p = false;
15350 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15351 &sub_strict_overflow_p)
15352 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15353 &sub_strict_overflow_p))
15355 if (sub_strict_overflow_p)
15356 *strict_overflow_p = true;
15357 return true;
15359 break;
15361 case SSA_NAME:
15362 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15363 break;
15364 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15366 default:
15367 break;
15369 return false;
15372 #define integer_valued_real_p(X) \
15373 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15375 #define RECURSE(X) \
15376 ((integer_valued_real_p) (X, depth + 1))
15378 /* Return true if the floating point result of (CODE OP0) has an
15379 integer value. We also allow +Inf, -Inf and NaN to be considered
15380 integer values. Return false for signaling NaN.
15382 DEPTH is the current nesting depth of the query. */
15384 bool
15385 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15387 switch (code)
15389 case FLOAT_EXPR:
15390 return true;
15392 case ABS_EXPR:
15393 return RECURSE (op0);
15395 CASE_CONVERT:
15397 tree type = TREE_TYPE (op0);
15398 if (TREE_CODE (type) == INTEGER_TYPE)
15399 return true;
15400 if (TREE_CODE (type) == REAL_TYPE)
15401 return RECURSE (op0);
15402 break;
15405 default:
15406 break;
15408 return false;
15411 /* Return true if the floating point result of (CODE OP0 OP1) has an
15412 integer value. We also allow +Inf, -Inf and NaN to be considered
15413 integer values. Return false for signaling NaN.
15415 DEPTH is the current nesting depth of the query. */
15417 bool
15418 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15420 switch (code)
15422 case PLUS_EXPR:
15423 case MINUS_EXPR:
15424 case MULT_EXPR:
15425 case MIN_EXPR:
15426 case MAX_EXPR:
15427 return RECURSE (op0) && RECURSE (op1);
15429 default:
15430 break;
15432 return false;
15435 /* Return true if the floating point result of calling FNDECL with arguments
15436 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15437 considered integer values. Return false for signaling NaN. If FNDECL
15438 takes fewer than 2 arguments, the remaining ARGn are null.
15440 DEPTH is the current nesting depth of the query. */
15442 bool
15443 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15445 switch (fn)
15447 CASE_CFN_CEIL:
15448 CASE_CFN_CEIL_FN:
15449 CASE_CFN_FLOOR:
15450 CASE_CFN_FLOOR_FN:
15451 CASE_CFN_NEARBYINT:
15452 CASE_CFN_NEARBYINT_FN:
15453 CASE_CFN_RINT:
15454 CASE_CFN_RINT_FN:
15455 CASE_CFN_ROUND:
15456 CASE_CFN_ROUND_FN:
15457 CASE_CFN_ROUNDEVEN:
15458 CASE_CFN_ROUNDEVEN_FN:
15459 CASE_CFN_TRUNC:
15460 CASE_CFN_TRUNC_FN:
15461 return true;
15463 CASE_CFN_FMIN:
15464 CASE_CFN_FMIN_FN:
15465 CASE_CFN_FMAX:
15466 CASE_CFN_FMAX_FN:
15467 return RECURSE (arg0) && RECURSE (arg1);
15469 default:
15470 break;
15472 return false;
15475 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15476 has an integer value. We also allow +Inf, -Inf and NaN to be
15477 considered integer values. Return false for signaling NaN.
15479 DEPTH is the current nesting depth of the query. */
15481 bool
15482 integer_valued_real_single_p (tree t, int depth)
15484 switch (TREE_CODE (t))
15486 case REAL_CST:
15487 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15489 case COND_EXPR:
15490 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15492 case SSA_NAME:
15493 /* Limit the depth of recursion to avoid quadratic behavior.
15494 This is expected to catch almost all occurrences in practice.
15495 If this code misses important cases that unbounded recursion
15496 would not, passes that need this information could be revised
15497 to provide it through dataflow propagation. */
15498 return (!name_registered_for_update_p (t)
15499 && depth < param_max_ssa_name_query_depth
15500 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15501 depth));
15503 default:
15504 break;
15506 return false;
15509 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15510 has an integer value. We also allow +Inf, -Inf and NaN to be
15511 considered integer values. Return false for signaling NaN.
15513 DEPTH is the current nesting depth of the query. */
15515 static bool
15516 integer_valued_real_invalid_p (tree t, int depth)
15518 switch (TREE_CODE (t))
15520 case COMPOUND_EXPR:
15521 case MODIFY_EXPR:
15522 case BIND_EXPR:
15523 return RECURSE (TREE_OPERAND (t, 1));
15525 case SAVE_EXPR:
15526 return RECURSE (TREE_OPERAND (t, 0));
15528 default:
15529 break;
15531 return false;
15534 #undef RECURSE
15535 #undef integer_valued_real_p
15537 /* Return true if the floating point expression T has an integer value.
15538 We also allow +Inf, -Inf and NaN to be considered integer values.
15539 Return false for signaling NaN.
15541 DEPTH is the current nesting depth of the query. */
15543 bool
15544 integer_valued_real_p (tree t, int depth)
15546 if (t == error_mark_node)
15547 return false;
15549 STRIP_ANY_LOCATION_WRAPPER (t);
15551 tree_code code = TREE_CODE (t);
15552 switch (TREE_CODE_CLASS (code))
15554 case tcc_binary:
15555 case tcc_comparison:
15556 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15557 TREE_OPERAND (t, 1), depth);
15559 case tcc_unary:
15560 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15562 case tcc_constant:
15563 case tcc_declaration:
15564 case tcc_reference:
15565 return integer_valued_real_single_p (t, depth);
15567 default:
15568 break;
15571 switch (code)
15573 case COND_EXPR:
15574 case SSA_NAME:
15575 return integer_valued_real_single_p (t, depth);
15577 case CALL_EXPR:
15579 tree arg0 = (call_expr_nargs (t) > 0
15580 ? CALL_EXPR_ARG (t, 0)
15581 : NULL_TREE);
15582 tree arg1 = (call_expr_nargs (t) > 1
15583 ? CALL_EXPR_ARG (t, 1)
15584 : NULL_TREE);
15585 return integer_valued_real_call_p (get_call_combined_fn (t),
15586 arg0, arg1, depth);
15589 default:
15590 return integer_valued_real_invalid_p (t, depth);
15594 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15595 attempt to fold the expression to a constant without modifying TYPE,
15596 OP0 or OP1.
15598 If the expression could be simplified to a constant, then return
15599 the constant. If the expression would not be simplified to a
15600 constant, then return NULL_TREE. */
15602 tree
15603 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15605 tree tem = fold_binary (code, type, op0, op1);
15606 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15609 /* Given the components of a unary expression CODE, TYPE and OP0,
15610 attempt to fold the expression to a constant without modifying
15611 TYPE or OP0.
15613 If the expression could be simplified to a constant, then return
15614 the constant. If the expression would not be simplified to a
15615 constant, then return NULL_TREE. */
15617 tree
15618 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15620 tree tem = fold_unary (code, type, op0);
15621 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15624 /* If EXP represents referencing an element in a constant string
15625 (either via pointer arithmetic or array indexing), return the
15626 tree representing the value accessed, otherwise return NULL. */
15628 tree
15629 fold_read_from_constant_string (tree exp)
15631 if ((TREE_CODE (exp) == INDIRECT_REF
15632 || TREE_CODE (exp) == ARRAY_REF)
15633 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15635 tree exp1 = TREE_OPERAND (exp, 0);
15636 tree index;
15637 tree string;
15638 location_t loc = EXPR_LOCATION (exp);
15640 if (TREE_CODE (exp) == INDIRECT_REF)
15641 string = string_constant (exp1, &index, NULL, NULL);
15642 else
15644 tree low_bound = array_ref_low_bound (exp);
15645 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15647 /* Optimize the special-case of a zero lower bound.
15649 We convert the low_bound to sizetype to avoid some problems
15650 with constant folding. (E.g. suppose the lower bound is 1,
15651 and its mode is QI. Without the conversion,l (ARRAY
15652 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15653 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15654 if (! integer_zerop (low_bound))
15655 index = size_diffop_loc (loc, index,
15656 fold_convert_loc (loc, sizetype, low_bound));
15658 string = exp1;
15661 scalar_int_mode char_mode;
15662 if (string
15663 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15664 && TREE_CODE (string) == STRING_CST
15665 && tree_fits_uhwi_p (index)
15666 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15667 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15668 &char_mode)
15669 && GET_MODE_SIZE (char_mode) == 1)
15670 return build_int_cst_type (TREE_TYPE (exp),
15671 (TREE_STRING_POINTER (string)
15672 [TREE_INT_CST_LOW (index)]));
15674 return NULL;
15677 /* Folds a read from vector element at IDX of vector ARG. */
15679 tree
15680 fold_read_from_vector (tree arg, poly_uint64 idx)
15682 unsigned HOST_WIDE_INT i;
15683 if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15684 && known_ge (idx, 0u)
15685 && idx.is_constant (&i))
15687 if (TREE_CODE (arg) == VECTOR_CST)
15688 return VECTOR_CST_ELT (arg, i);
15689 else if (TREE_CODE (arg) == CONSTRUCTOR)
15691 if (CONSTRUCTOR_NELTS (arg)
15692 && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15693 return NULL_TREE;
15694 if (i >= CONSTRUCTOR_NELTS (arg))
15695 return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15696 return CONSTRUCTOR_ELT (arg, i)->value;
15699 return NULL_TREE;
15702 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15703 an integer constant, real, or fixed-point constant.
15705 TYPE is the type of the result. */
15707 static tree
15708 fold_negate_const (tree arg0, tree type)
15710 tree t = NULL_TREE;
15712 switch (TREE_CODE (arg0))
15714 case REAL_CST:
15715 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15716 break;
15718 case FIXED_CST:
15720 FIXED_VALUE_TYPE f;
15721 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15722 &(TREE_FIXED_CST (arg0)), NULL,
15723 TYPE_SATURATING (type));
15724 t = build_fixed (type, f);
15725 /* Propagate overflow flags. */
15726 if (overflow_p | TREE_OVERFLOW (arg0))
15727 TREE_OVERFLOW (t) = 1;
15728 break;
15731 default:
15732 if (poly_int_tree_p (arg0))
15734 wi::overflow_type overflow;
15735 poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15736 t = force_fit_type (type, res, 1,
15737 (overflow && ! TYPE_UNSIGNED (type))
15738 || TREE_OVERFLOW (arg0));
15739 break;
15742 gcc_unreachable ();
15745 return t;
15748 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15749 an integer constant or real constant.
15751 TYPE is the type of the result. */
15753 tree
15754 fold_abs_const (tree arg0, tree type)
15756 tree t = NULL_TREE;
15758 switch (TREE_CODE (arg0))
15760 case INTEGER_CST:
15762 /* If the value is unsigned or non-negative, then the absolute value
15763 is the same as the ordinary value. */
15764 wide_int val = wi::to_wide (arg0);
15765 wi::overflow_type overflow = wi::OVF_NONE;
15766 if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15769 /* If the value is negative, then the absolute value is
15770 its negation. */
15771 else
15772 val = wi::neg (val, &overflow);
15774 /* Force to the destination type, set TREE_OVERFLOW for signed
15775 TYPE only. */
15776 t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15778 break;
15780 case REAL_CST:
15781 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15782 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15783 else
15784 t = arg0;
15785 break;
15787 default:
15788 gcc_unreachable ();
15791 return t;
15794 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15795 constant. TYPE is the type of the result. */
15797 static tree
15798 fold_not_const (const_tree arg0, tree type)
15800 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15802 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15805 /* Given CODE, a relational operator, the target type, TYPE and two
15806 constant operands OP0 and OP1, return the result of the
15807 relational operation. If the result is not a compile time
15808 constant, then return NULL_TREE. */
15810 static tree
15811 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15813 int result, invert;
15815 /* From here on, the only cases we handle are when the result is
15816 known to be a constant. */
15818 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15820 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15821 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15823 /* Handle the cases where either operand is a NaN. */
15824 if (real_isnan (c0) || real_isnan (c1))
15826 switch (code)
15828 case EQ_EXPR:
15829 case ORDERED_EXPR:
15830 result = 0;
15831 break;
15833 case NE_EXPR:
15834 case UNORDERED_EXPR:
15835 case UNLT_EXPR:
15836 case UNLE_EXPR:
15837 case UNGT_EXPR:
15838 case UNGE_EXPR:
15839 case UNEQ_EXPR:
15840 result = 1;
15841 break;
15843 case LT_EXPR:
15844 case LE_EXPR:
15845 case GT_EXPR:
15846 case GE_EXPR:
15847 case LTGT_EXPR:
15848 if (flag_trapping_math)
15849 return NULL_TREE;
15850 result = 0;
15851 break;
15853 default:
15854 gcc_unreachable ();
15857 return constant_boolean_node (result, type);
15860 return constant_boolean_node (real_compare (code, c0, c1), type);
15863 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15865 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15866 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15867 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15870 /* Handle equality/inequality of complex constants. */
15871 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15873 tree rcond = fold_relational_const (code, type,
15874 TREE_REALPART (op0),
15875 TREE_REALPART (op1));
15876 tree icond = fold_relational_const (code, type,
15877 TREE_IMAGPART (op0),
15878 TREE_IMAGPART (op1));
15879 if (code == EQ_EXPR)
15880 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15881 else if (code == NE_EXPR)
15882 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15883 else
15884 return NULL_TREE;
15887 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15889 if (!VECTOR_TYPE_P (type))
15891 /* Have vector comparison with scalar boolean result. */
15892 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
15893 && known_eq (VECTOR_CST_NELTS (op0),
15894 VECTOR_CST_NELTS (op1)));
15895 unsigned HOST_WIDE_INT nunits;
15896 if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
15897 return NULL_TREE;
15898 for (unsigned i = 0; i < nunits; i++)
15900 tree elem0 = VECTOR_CST_ELT (op0, i);
15901 tree elem1 = VECTOR_CST_ELT (op1, i);
15902 tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
15903 if (tmp == NULL_TREE)
15904 return NULL_TREE;
15905 if (integer_zerop (tmp))
15906 return constant_boolean_node (code == NE_EXPR, type);
15908 return constant_boolean_node (code == EQ_EXPR, type);
15910 tree_vector_builder elts;
15911 if (!elts.new_binary_operation (type, op0, op1, false))
15912 return NULL_TREE;
15913 unsigned int count = elts.encoded_nelts ();
15914 for (unsigned i = 0; i < count; i++)
15916 tree elem_type = TREE_TYPE (type);
15917 tree elem0 = VECTOR_CST_ELT (op0, i);
15918 tree elem1 = VECTOR_CST_ELT (op1, i);
15920 tree tem = fold_relational_const (code, elem_type,
15921 elem0, elem1);
15923 if (tem == NULL_TREE)
15924 return NULL_TREE;
15926 elts.quick_push (build_int_cst (elem_type,
15927 integer_zerop (tem) ? 0 : -1));
15930 return elts.build ();
15933 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15935 To compute GT, swap the arguments and do LT.
15936 To compute GE, do LT and invert the result.
15937 To compute LE, swap the arguments, do LT and invert the result.
15938 To compute NE, do EQ and invert the result.
15940 Therefore, the code below must handle only EQ and LT. */
15942 if (code == LE_EXPR || code == GT_EXPR)
15944 std::swap (op0, op1);
15945 code = swap_tree_comparison (code);
15948 /* Note that it is safe to invert for real values here because we
15949 have already handled the one case that it matters. */
15951 invert = 0;
15952 if (code == NE_EXPR || code == GE_EXPR)
15954 invert = 1;
15955 code = invert_tree_comparison (code, false);
15958 /* Compute a result for LT or EQ if args permit;
15959 Otherwise return T. */
15960 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15962 if (code == EQ_EXPR)
15963 result = tree_int_cst_equal (op0, op1);
15964 else
15965 result = tree_int_cst_lt (op0, op1);
15967 else
15968 return NULL_TREE;
15970 if (invert)
15971 result ^= 1;
15972 return constant_boolean_node (result, type);
15975 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15976 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15977 itself. */
15979 tree
15980 fold_build_cleanup_point_expr (tree type, tree expr)
15982 /* If the expression does not have side effects then we don't have to wrap
15983 it with a cleanup point expression. */
15984 if (!TREE_SIDE_EFFECTS (expr))
15985 return expr;
15987 /* If the expression is a return, check to see if the expression inside the
15988 return has no side effects or the right hand side of the modify expression
15989 inside the return. If either don't have side effects set we don't need to
15990 wrap the expression in a cleanup point expression. Note we don't check the
15991 left hand side of the modify because it should always be a return decl. */
15992 if (TREE_CODE (expr) == RETURN_EXPR)
15994 tree op = TREE_OPERAND (expr, 0);
15995 if (!op || !TREE_SIDE_EFFECTS (op))
15996 return expr;
15997 op = TREE_OPERAND (op, 1);
15998 if (!TREE_SIDE_EFFECTS (op))
15999 return expr;
16002 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
16005 /* Given a pointer value OP0 and a type TYPE, return a simplified version
16006 of an indirection through OP0, or NULL_TREE if no simplification is
16007 possible. */
16009 tree
16010 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16012 tree sub = op0;
16013 tree subtype;
16014 poly_uint64 const_op01;
16016 STRIP_NOPS (sub);
16017 subtype = TREE_TYPE (sub);
16018 if (!POINTER_TYPE_P (subtype)
16019 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
16020 return NULL_TREE;
16022 if (TREE_CODE (sub) == ADDR_EXPR)
16024 tree op = TREE_OPERAND (sub, 0);
16025 tree optype = TREE_TYPE (op);
16027 /* *&CONST_DECL -> to the value of the const decl. */
16028 if (TREE_CODE (op) == CONST_DECL)
16029 return DECL_INITIAL (op);
16030 /* *&p => p; make sure to handle *&"str"[cst] here. */
16031 if (type == optype)
16033 tree fop = fold_read_from_constant_string (op);
16034 if (fop)
16035 return fop;
16036 else
16037 return op;
16039 /* *(foo *)&fooarray => fooarray[0] */
16040 else if (TREE_CODE (optype) == ARRAY_TYPE
16041 && type == TREE_TYPE (optype)
16042 && (!in_gimple_form
16043 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16045 tree type_domain = TYPE_DOMAIN (optype);
16046 tree min_val = size_zero_node;
16047 if (type_domain && TYPE_MIN_VALUE (type_domain))
16048 min_val = TYPE_MIN_VALUE (type_domain);
16049 if (in_gimple_form
16050 && TREE_CODE (min_val) != INTEGER_CST)
16051 return NULL_TREE;
16052 return build4_loc (loc, ARRAY_REF, type, op, min_val,
16053 NULL_TREE, NULL_TREE);
16055 /* *(foo *)&complexfoo => __real__ complexfoo */
16056 else if (TREE_CODE (optype) == COMPLEX_TYPE
16057 && type == TREE_TYPE (optype))
16058 return fold_build1_loc (loc, REALPART_EXPR, type, op);
16059 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16060 else if (VECTOR_TYPE_P (optype)
16061 && type == TREE_TYPE (optype))
16063 tree part_width = TYPE_SIZE (type);
16064 tree index = bitsize_int (0);
16065 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
16066 index);
16070 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16071 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
16073 tree op00 = TREE_OPERAND (sub, 0);
16074 tree op01 = TREE_OPERAND (sub, 1);
16076 STRIP_NOPS (op00);
16077 if (TREE_CODE (op00) == ADDR_EXPR)
16079 tree op00type;
16080 op00 = TREE_OPERAND (op00, 0);
16081 op00type = TREE_TYPE (op00);
16083 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16084 if (VECTOR_TYPE_P (op00type)
16085 && type == TREE_TYPE (op00type)
16086 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16087 but we want to treat offsets with MSB set as negative.
16088 For the code below negative offsets are invalid and
16089 TYPE_SIZE of the element is something unsigned, so
16090 check whether op01 fits into poly_int64, which implies
16091 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16092 then just use poly_uint64 because we want to treat the
16093 value as unsigned. */
16094 && tree_fits_poly_int64_p (op01))
16096 tree part_width = TYPE_SIZE (type);
16097 poly_uint64 max_offset
16098 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
16099 * TYPE_VECTOR_SUBPARTS (op00type));
16100 if (known_lt (const_op01, max_offset))
16102 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
16103 return fold_build3_loc (loc,
16104 BIT_FIELD_REF, type, op00,
16105 part_width, index);
16108 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16109 else if (TREE_CODE (op00type) == COMPLEX_TYPE
16110 && type == TREE_TYPE (op00type))
16112 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
16113 const_op01))
16114 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16116 /* ((foo *)&fooarray)[1] => fooarray[1] */
16117 else if (TREE_CODE (op00type) == ARRAY_TYPE
16118 && type == TREE_TYPE (op00type))
16120 tree type_domain = TYPE_DOMAIN (op00type);
16121 tree min_val = size_zero_node;
16122 if (type_domain && TYPE_MIN_VALUE (type_domain))
16123 min_val = TYPE_MIN_VALUE (type_domain);
16124 poly_uint64 type_size, index;
16125 if (poly_int_tree_p (min_val)
16126 && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
16127 && multiple_p (const_op01, type_size, &index))
16129 poly_offset_int off = index + wi::to_poly_offset (min_val);
16130 op01 = wide_int_to_tree (sizetype, off);
16131 return build4_loc (loc, ARRAY_REF, type, op00, op01,
16132 NULL_TREE, NULL_TREE);
16138 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16139 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16140 && type == TREE_TYPE (TREE_TYPE (subtype))
16141 && (!in_gimple_form
16142 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16144 tree type_domain;
16145 tree min_val = size_zero_node;
16146 sub = build_fold_indirect_ref_loc (loc, sub);
16147 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16148 if (type_domain && TYPE_MIN_VALUE (type_domain))
16149 min_val = TYPE_MIN_VALUE (type_domain);
16150 if (in_gimple_form
16151 && TREE_CODE (min_val) != INTEGER_CST)
16152 return NULL_TREE;
16153 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16154 NULL_TREE);
16157 return NULL_TREE;
16160 /* Builds an expression for an indirection through T, simplifying some
16161 cases. */
16163 tree
16164 build_fold_indirect_ref_loc (location_t loc, tree t)
16166 tree type = TREE_TYPE (TREE_TYPE (t));
16167 tree sub = fold_indirect_ref_1 (loc, type, t);
16169 if (sub)
16170 return sub;
16172 return build1_loc (loc, INDIRECT_REF, type, t);
16175 /* Given an INDIRECT_REF T, return either T or a simplified version. */
16177 tree
16178 fold_indirect_ref_loc (location_t loc, tree t)
16180 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16182 if (sub)
16183 return sub;
16184 else
16185 return t;
16188 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16189 whose result is ignored. The type of the returned tree need not be
16190 the same as the original expression. */
16192 tree
16193 fold_ignored_result (tree t)
16195 if (!TREE_SIDE_EFFECTS (t))
16196 return integer_zero_node;
16198 for (;;)
16199 switch (TREE_CODE_CLASS (TREE_CODE (t)))
16201 case tcc_unary:
16202 t = TREE_OPERAND (t, 0);
16203 break;
16205 case tcc_binary:
16206 case tcc_comparison:
16207 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16208 t = TREE_OPERAND (t, 0);
16209 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16210 t = TREE_OPERAND (t, 1);
16211 else
16212 return t;
16213 break;
16215 case tcc_expression:
16216 switch (TREE_CODE (t))
16218 case COMPOUND_EXPR:
16219 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16220 return t;
16221 t = TREE_OPERAND (t, 0);
16222 break;
16224 case COND_EXPR:
16225 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16226 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16227 return t;
16228 t = TREE_OPERAND (t, 0);
16229 break;
16231 default:
16232 return t;
16234 break;
16236 default:
16237 return t;
16241 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16243 tree
16244 round_up_loc (location_t loc, tree value, unsigned int divisor)
16246 tree div = NULL_TREE;
16248 if (divisor == 1)
16249 return value;
16251 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16252 have to do anything. Only do this when we are not given a const,
16253 because in that case, this check is more expensive than just
16254 doing it. */
16255 if (TREE_CODE (value) != INTEGER_CST)
16257 div = build_int_cst (TREE_TYPE (value), divisor);
16259 if (multiple_of_p (TREE_TYPE (value), value, div))
16260 return value;
16263 /* If divisor is a power of two, simplify this to bit manipulation. */
16264 if (pow2_or_zerop (divisor))
16266 if (TREE_CODE (value) == INTEGER_CST)
16268 wide_int val = wi::to_wide (value);
16269 bool overflow_p;
16271 if ((val & (divisor - 1)) == 0)
16272 return value;
16274 overflow_p = TREE_OVERFLOW (value);
16275 val += divisor - 1;
16276 val &= (int) -divisor;
16277 if (val == 0)
16278 overflow_p = true;
16280 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16282 else
16284 tree t;
16286 t = build_int_cst (TREE_TYPE (value), divisor - 1);
16287 value = size_binop_loc (loc, PLUS_EXPR, value, t);
16288 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16289 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16292 else
16294 if (!div)
16295 div = build_int_cst (TREE_TYPE (value), divisor);
16296 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16297 value = size_binop_loc (loc, MULT_EXPR, value, div);
16300 return value;
16303 /* Likewise, but round down. */
16305 tree
16306 round_down_loc (location_t loc, tree value, int divisor)
16308 tree div = NULL_TREE;
16310 gcc_assert (divisor > 0);
16311 if (divisor == 1)
16312 return value;
16314 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16315 have to do anything. Only do this when we are not given a const,
16316 because in that case, this check is more expensive than just
16317 doing it. */
16318 if (TREE_CODE (value) != INTEGER_CST)
16320 div = build_int_cst (TREE_TYPE (value), divisor);
16322 if (multiple_of_p (TREE_TYPE (value), value, div))
16323 return value;
16326 /* If divisor is a power of two, simplify this to bit manipulation. */
16327 if (pow2_or_zerop (divisor))
16329 tree t;
16331 t = build_int_cst (TREE_TYPE (value), -divisor);
16332 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16334 else
16336 if (!div)
16337 div = build_int_cst (TREE_TYPE (value), divisor);
16338 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16339 value = size_binop_loc (loc, MULT_EXPR, value, div);
16342 return value;
16345 /* Returns the pointer to the base of the object addressed by EXP and
16346 extracts the information about the offset of the access, storing it
16347 to PBITPOS and POFFSET. */
16349 static tree
16350 split_address_to_core_and_offset (tree exp,
16351 poly_int64_pod *pbitpos, tree *poffset)
16353 tree core;
16354 machine_mode mode;
16355 int unsignedp, reversep, volatilep;
16356 poly_int64 bitsize;
16357 location_t loc = EXPR_LOCATION (exp);
16359 if (TREE_CODE (exp) == SSA_NAME)
16360 if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16361 if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16362 exp = gimple_assign_rhs1 (def);
16364 if (TREE_CODE (exp) == ADDR_EXPR)
16366 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16367 poffset, &mode, &unsignedp, &reversep,
16368 &volatilep);
16369 core = build_fold_addr_expr_loc (loc, core);
16371 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16373 core = TREE_OPERAND (exp, 0);
16374 STRIP_NOPS (core);
16375 *pbitpos = 0;
16376 *poffset = TREE_OPERAND (exp, 1);
16377 if (poly_int_tree_p (*poffset))
16379 poly_offset_int tem
16380 = wi::sext (wi::to_poly_offset (*poffset),
16381 TYPE_PRECISION (TREE_TYPE (*poffset)));
16382 tem <<= LOG2_BITS_PER_UNIT;
16383 if (tem.to_shwi (pbitpos))
16384 *poffset = NULL_TREE;
16387 else
16389 core = exp;
16390 *pbitpos = 0;
16391 *poffset = NULL_TREE;
16394 return core;
16397 /* Returns true if addresses of E1 and E2 differ by a constant, false
16398 otherwise. If they do, E1 - E2 is stored in *DIFF. */
16400 bool
16401 ptr_difference_const (tree e1, tree e2, poly_int64_pod *diff)
16403 tree core1, core2;
16404 poly_int64 bitpos1, bitpos2;
16405 tree toffset1, toffset2, tdiff, type;
16407 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16408 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16410 poly_int64 bytepos1, bytepos2;
16411 if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16412 || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16413 || !operand_equal_p (core1, core2, 0))
16414 return false;
16416 if (toffset1 && toffset2)
16418 type = TREE_TYPE (toffset1);
16419 if (type != TREE_TYPE (toffset2))
16420 toffset2 = fold_convert (type, toffset2);
16422 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16423 if (!cst_and_fits_in_hwi (tdiff))
16424 return false;
16426 *diff = int_cst_value (tdiff);
16428 else if (toffset1 || toffset2)
16430 /* If only one of the offsets is non-constant, the difference cannot
16431 be a constant. */
16432 return false;
16434 else
16435 *diff = 0;
16437 *diff += bytepos1 - bytepos2;
16438 return true;
16441 /* Return OFF converted to a pointer offset type suitable as offset for
16442 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16443 tree
16444 convert_to_ptrofftype_loc (location_t loc, tree off)
16446 if (ptrofftype_p (TREE_TYPE (off)))
16447 return off;
16448 return fold_convert_loc (loc, sizetype, off);
16451 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16452 tree
16453 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16455 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16456 ptr, convert_to_ptrofftype_loc (loc, off));
16459 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16460 tree
16461 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16463 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16464 ptr, size_int (off));
16467 /* Return a pointer to a NUL-terminated string containing the sequence
16468 of bytes corresponding to the representation of the object referred to
16469 by SRC (or a subsequence of such bytes within it if SRC is a reference
16470 to an initialized constant array plus some constant offset).
16471 Set *STRSIZE the number of bytes in the constant sequence including
16472 the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16473 where A is the array that stores the constant sequence that SRC points
16474 to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16475 need not point to a string or even an array of characters but may point
16476 to an object of any type. */
16478 const char *
16479 getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16481 /* The offset into the array A storing the string, and A's byte size. */
16482 tree offset_node;
16483 tree mem_size;
16485 if (strsize)
16486 *strsize = 0;
16488 if (strsize)
16489 src = byte_representation (src, &offset_node, &mem_size, NULL);
16490 else
16491 src = string_constant (src, &offset_node, &mem_size, NULL);
16492 if (!src)
16493 return NULL;
16495 unsigned HOST_WIDE_INT offset = 0;
16496 if (offset_node != NULL_TREE)
16498 if (!tree_fits_uhwi_p (offset_node))
16499 return NULL;
16500 else
16501 offset = tree_to_uhwi (offset_node);
16504 if (!tree_fits_uhwi_p (mem_size))
16505 return NULL;
16507 /* ARRAY_SIZE is the byte size of the array the constant sequence
16508 is stored in and equal to sizeof A. INIT_BYTES is the number
16509 of bytes in the constant sequence used to initialize the array,
16510 including any embedded NULs as well as the terminating NUL (for
16511 strings), but not including any trailing zeros/NULs past
16512 the terminating one appended implicitly to a string literal to
16513 zero out the remainder of the array it's stored in. For example,
16514 given:
16515 const char a[7] = "abc\0d";
16516 n = strlen (a + 1);
16517 ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16518 (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16519 is equal to strlen (A) + 1. */
16520 const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16521 unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16522 const char *string = TREE_STRING_POINTER (src);
16524 /* Ideally this would turn into a gcc_checking_assert over time. */
16525 if (init_bytes > array_size)
16526 init_bytes = array_size;
16528 if (init_bytes == 0 || offset >= array_size)
16529 return NULL;
16531 if (strsize)
16533 /* Compute and store the number of characters from the beginning
16534 of the substring at OFFSET to the end, including the terminating
16535 nul. Offsets past the initial length refer to null strings. */
16536 if (offset < init_bytes)
16537 *strsize = init_bytes - offset;
16538 else
16539 *strsize = 1;
16541 else
16543 tree eltype = TREE_TYPE (TREE_TYPE (src));
16544 /* Support only properly NUL-terminated single byte strings. */
16545 if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16546 return NULL;
16547 if (string[init_bytes - 1] != '\0')
16548 return NULL;
16551 return offset < init_bytes ? string + offset : "";
16554 /* Return a pointer to a NUL-terminated string corresponding to
16555 the expression STR referencing a constant string, possibly
16556 involving a constant offset. Return null if STR either doesn't
16557 reference a constant string or if it involves a nonconstant
16558 offset. */
16560 const char *
16561 c_getstr (tree str)
16563 return getbyterep (str, NULL);
16566 /* Given a tree T, compute which bits in T may be nonzero. */
16568 wide_int
16569 tree_nonzero_bits (const_tree t)
16571 switch (TREE_CODE (t))
16573 case INTEGER_CST:
16574 return wi::to_wide (t);
16575 case SSA_NAME:
16576 return get_nonzero_bits (t);
16577 case NON_LVALUE_EXPR:
16578 case SAVE_EXPR:
16579 return tree_nonzero_bits (TREE_OPERAND (t, 0));
16580 case BIT_AND_EXPR:
16581 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16582 tree_nonzero_bits (TREE_OPERAND (t, 1)));
16583 case BIT_IOR_EXPR:
16584 case BIT_XOR_EXPR:
16585 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16586 tree_nonzero_bits (TREE_OPERAND (t, 1)));
16587 case COND_EXPR:
16588 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1)),
16589 tree_nonzero_bits (TREE_OPERAND (t, 2)));
16590 CASE_CONVERT:
16591 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16592 TYPE_PRECISION (TREE_TYPE (t)),
16593 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0))));
16594 case PLUS_EXPR:
16595 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16597 wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0));
16598 wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1));
16599 if (wi::bit_and (nzbits1, nzbits2) == 0)
16600 return wi::bit_or (nzbits1, nzbits2);
16602 break;
16603 case LSHIFT_EXPR:
16604 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16606 tree type = TREE_TYPE (t);
16607 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16608 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16609 TYPE_PRECISION (type));
16610 return wi::neg_p (arg1)
16611 ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16612 : wi::lshift (nzbits, arg1);
16614 break;
16615 case RSHIFT_EXPR:
16616 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16618 tree type = TREE_TYPE (t);
16619 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16620 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16621 TYPE_PRECISION (type));
16622 return wi::neg_p (arg1)
16623 ? wi::lshift (nzbits, -arg1)
16624 : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16626 break;
16627 default:
16628 break;
16631 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t)));
16634 /* Helper function for address compare simplifications in match.pd.
16635 OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16636 TYPE is the type of comparison operands.
16637 BASE0, BASE1, OFF0 and OFF1 are set by the function.
16638 GENERIC is true if GENERIC folding and false for GIMPLE folding.
16639 Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16640 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16641 and 2 if unknown. */
16644 address_compare (tree_code code, tree type, tree op0, tree op1,
16645 tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16646 bool generic)
16648 if (TREE_CODE (op0) == SSA_NAME)
16649 op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16650 if (TREE_CODE (op1) == SSA_NAME)
16651 op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16652 gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16653 gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16654 base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16655 base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16656 if (base0 && TREE_CODE (base0) == MEM_REF)
16658 off0 += mem_ref_offset (base0).force_shwi ();
16659 base0 = TREE_OPERAND (base0, 0);
16661 if (base1 && TREE_CODE (base1) == MEM_REF)
16663 off1 += mem_ref_offset (base1).force_shwi ();
16664 base1 = TREE_OPERAND (base1, 0);
16666 if (base0 == NULL_TREE || base1 == NULL_TREE)
16667 return 2;
16669 int equal = 2;
16670 /* Punt in GENERIC on variables with value expressions;
16671 the value expressions might point to fields/elements
16672 of other vars etc. */
16673 if (generic
16674 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16675 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16676 return 2;
16677 else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16679 symtab_node *node0 = symtab_node::get_create (base0);
16680 symtab_node *node1 = symtab_node::get_create (base1);
16681 equal = node0->equal_address_to (node1);
16683 else if ((DECL_P (base0)
16684 || TREE_CODE (base0) == SSA_NAME
16685 || TREE_CODE (base0) == STRING_CST)
16686 && (DECL_P (base1)
16687 || TREE_CODE (base1) == SSA_NAME
16688 || TREE_CODE (base1) == STRING_CST))
16689 equal = (base0 == base1);
16690 /* Assume different STRING_CSTs with the same content will be
16691 merged. */
16692 if (equal == 0
16693 && TREE_CODE (base0) == STRING_CST
16694 && TREE_CODE (base1) == STRING_CST
16695 && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16696 && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16697 TREE_STRING_LENGTH (base0)) == 0)
16698 equal = 1;
16699 if (equal == 1)
16701 if (code == EQ_EXPR
16702 || code == NE_EXPR
16703 /* If the offsets are equal we can ignore overflow. */
16704 || known_eq (off0, off1)
16705 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16706 /* Or if we compare using pointers to decls or strings. */
16707 || (POINTER_TYPE_P (type)
16708 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16709 return 1;
16710 return 2;
16712 if (equal != 0)
16713 return equal;
16714 if (code != EQ_EXPR && code != NE_EXPR)
16715 return 2;
16717 /* At this point we know (or assume) the two pointers point at
16718 different objects. */
16719 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16720 off0.is_constant (&ioff0);
16721 off1.is_constant (&ioff1);
16722 /* Punt on non-zero offsets from functions. */
16723 if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16724 || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16725 return 2;
16726 /* Or if the bases are neither decls nor string literals. */
16727 if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16728 return 2;
16729 if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16730 return 2;
16731 /* For initializers, assume addresses of different functions are
16732 different. */
16733 if (folding_initializer
16734 && TREE_CODE (base0) == FUNCTION_DECL
16735 && TREE_CODE (base1) == FUNCTION_DECL)
16736 return 0;
16738 /* Compute whether one address points to the start of one
16739 object and another one to the end of another one. */
16740 poly_int64 size0 = 0, size1 = 0;
16741 if (TREE_CODE (base0) == STRING_CST)
16743 if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16744 equal = 2;
16745 else
16746 size0 = TREE_STRING_LENGTH (base0);
16748 else if (TREE_CODE (base0) == FUNCTION_DECL)
16749 size0 = 1;
16750 else
16752 tree sz0 = DECL_SIZE_UNIT (base0);
16753 if (!tree_fits_poly_int64_p (sz0))
16754 equal = 2;
16755 else
16756 size0 = tree_to_poly_int64 (sz0);
16758 if (TREE_CODE (base1) == STRING_CST)
16760 if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16761 equal = 2;
16762 else
16763 size1 = TREE_STRING_LENGTH (base1);
16765 else if (TREE_CODE (base1) == FUNCTION_DECL)
16766 size1 = 1;
16767 else
16769 tree sz1 = DECL_SIZE_UNIT (base1);
16770 if (!tree_fits_poly_int64_p (sz1))
16771 equal = 2;
16772 else
16773 size1 = tree_to_poly_int64 (sz1);
16775 if (equal == 0)
16777 /* If one offset is pointing (or could be) to the beginning of one
16778 object and the other is pointing to one past the last byte of the
16779 other object, punt. */
16780 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16781 equal = 2;
16782 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16783 equal = 2;
16784 /* If both offsets are the same, there are some cases we know that are
16785 ok. Either if we know they aren't zero, or if we know both sizes
16786 are no zero. */
16787 if (equal == 2
16788 && known_eq (off0, off1)
16789 && (known_ne (off0, 0)
16790 || (known_ne (size0, 0) && known_ne (size1, 0))))
16791 equal = 0;
16794 /* At this point, equal is 2 if either one or both pointers are out of
16795 bounds of their object, or one points to start of its object and the
16796 other points to end of its object. This is unspecified behavior
16797 e.g. in C++. Otherwise equal is 0. */
16798 if (folding_cxx_constexpr && equal)
16799 return equal;
16801 /* When both pointers point to string literals, even when equal is 0,
16802 due to tail merging of string literals the pointers might be the same. */
16803 if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16805 if (ioff0 < 0
16806 || ioff1 < 0
16807 || ioff0 > TREE_STRING_LENGTH (base0)
16808 || ioff1 > TREE_STRING_LENGTH (base1))
16809 return 2;
16811 /* If the bytes in the string literals starting at the pointers
16812 differ, the pointers need to be different. */
16813 if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16814 TREE_STRING_POINTER (base1) + ioff1,
16815 MIN (TREE_STRING_LENGTH (base0) - ioff0,
16816 TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16818 HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16819 if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16820 TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16821 ioffmin) == 0)
16822 /* If even the bytes in the string literal before the
16823 pointers are the same, the string literals could be
16824 tail merged. */
16825 return 2;
16827 return 0;
16830 if (folding_cxx_constexpr)
16831 return 0;
16833 /* If this is a pointer comparison, ignore for now even
16834 valid equalities where one pointer is the offset zero
16835 of one object and the other to one past end of another one. */
16836 if (!INTEGRAL_TYPE_P (type))
16837 return 0;
16839 /* Assume that string literals can't be adjacent to variables
16840 (automatic or global). */
16841 if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
16842 return 0;
16844 /* Assume that automatic variables can't be adjacent to global
16845 variables. */
16846 if (is_global_var (base0) != is_global_var (base1))
16847 return 0;
16849 return equal;
16852 /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16853 tree
16854 ctor_single_nonzero_element (const_tree t)
16856 unsigned HOST_WIDE_INT idx;
16857 constructor_elt *ce;
16858 tree elt = NULL_TREE;
16860 if (TREE_CODE (t) != CONSTRUCTOR)
16861 return NULL_TREE;
16862 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
16863 if (!integer_zerop (ce->value) && !real_zerop (ce->value))
16865 if (elt)
16866 return NULL_TREE;
16867 elt = ce->value;
16869 return elt;
16872 #if CHECKING_P
16874 namespace selftest {
16876 /* Helper functions for writing tests of folding trees. */
16878 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16880 static void
16881 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
16882 tree constant)
16884 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
16887 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16888 wrapping WRAPPED_EXPR. */
16890 static void
16891 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
16892 tree wrapped_expr)
16894 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
16895 ASSERT_NE (wrapped_expr, result);
16896 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
16897 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
16900 /* Verify that various arithmetic binary operations are folded
16901 correctly. */
16903 static void
16904 test_arithmetic_folding ()
16906 tree type = integer_type_node;
16907 tree x = create_tmp_var_raw (type, "x");
16908 tree zero = build_zero_cst (type);
16909 tree one = build_int_cst (type, 1);
16911 /* Addition. */
16912 /* 1 <-- (0 + 1) */
16913 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
16914 one);
16915 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
16916 one);
16918 /* (nonlvalue)x <-- (x + 0) */
16919 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
16922 /* Subtraction. */
16923 /* 0 <-- (x - x) */
16924 assert_binop_folds_to_const (x, MINUS_EXPR, x,
16925 zero);
16926 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
16929 /* Multiplication. */
16930 /* 0 <-- (x * 0) */
16931 assert_binop_folds_to_const (x, MULT_EXPR, zero,
16932 zero);
16934 /* (nonlvalue)x <-- (x * 1) */
16935 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
16939 /* Verify that various binary operations on vectors are folded
16940 correctly. */
16942 static void
16943 test_vector_folding ()
16945 tree inner_type = integer_type_node;
16946 tree type = build_vector_type (inner_type, 4);
16947 tree zero = build_zero_cst (type);
16948 tree one = build_one_cst (type);
16949 tree index = build_index_vector (type, 0, 1);
16951 /* Verify equality tests that return a scalar boolean result. */
16952 tree res_type = boolean_type_node;
16953 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
16954 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
16955 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
16956 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
16957 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
16958 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
16959 index, one)));
16960 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
16961 index, index)));
16962 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
16963 index, index)));
16966 /* Verify folding of VEC_DUPLICATE_EXPRs. */
16968 static void
16969 test_vec_duplicate_folding ()
16971 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
16972 machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
16973 /* This will be 1 if VEC_MODE isn't a vector mode. */
16974 poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
16976 tree type = build_vector_type (ssizetype, nunits);
16977 tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
16978 tree dup5_cst = build_vector_from_val (type, ssize_int (5));
16979 ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
16982 /* Run all of the selftests within this file. */
16984 void
16985 fold_const_cc_tests ()
16987 test_arithmetic_folding ();
16988 test_vector_folding ();
16989 test_vec_duplicate_folding ();
16992 } // namespace selftest
16994 #endif /* CHECKING_P */