Skip -fwhole-program when merging LTO options.
[official-gcc.git] / gcc / fold-const.cc
blobd4ee3b30983427a9c922a134dd7e879f04b2260d
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2022 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 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
7097 || (tcode == MULT_EXPR
7098 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
7099 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
7100 && code != MULT_EXPR)))
7102 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
7103 TYPE_SIGN (type)))
7105 if (TYPE_OVERFLOW_UNDEFINED (ctype))
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 if (TYPE_OVERFLOW_UNDEFINED (ctype))
7116 *strict_overflow_p = true;
7117 return fold_build2 (code, ctype, fold_convert (ctype, op0),
7118 fold_convert (ctype,
7119 const_binop (TRUNC_DIV_EXPR,
7120 c, op1)));
7123 break;
7125 default:
7126 break;
7129 return 0;
7132 /* Return a node which has the indicated constant VALUE (either 0 or
7133 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
7134 and is of the indicated TYPE. */
7136 tree
7137 constant_boolean_node (bool value, tree type)
7139 if (type == integer_type_node)
7140 return value ? integer_one_node : integer_zero_node;
7141 else if (type == boolean_type_node)
7142 return value ? boolean_true_node : boolean_false_node;
7143 else if (TREE_CODE (type) == VECTOR_TYPE)
7144 return build_vector_from_val (type,
7145 build_int_cst (TREE_TYPE (type),
7146 value ? -1 : 0));
7147 else
7148 return fold_convert (type, value ? integer_one_node : integer_zero_node);
7152 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
7153 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
7154 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
7155 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
7156 COND is the first argument to CODE; otherwise (as in the example
7157 given here), it is the second argument. TYPE is the type of the
7158 original expression. Return NULL_TREE if no simplification is
7159 possible. */
7161 static tree
7162 fold_binary_op_with_conditional_arg (location_t loc,
7163 enum tree_code code,
7164 tree type, tree op0, tree op1,
7165 tree cond, tree arg, int cond_first_p)
7167 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
7168 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
7169 tree test, true_value, false_value;
7170 tree lhs = NULL_TREE;
7171 tree rhs = NULL_TREE;
7172 enum tree_code cond_code = COND_EXPR;
7174 /* Do not move possibly trapping operations into the conditional as this
7175 pessimizes code and causes gimplification issues when applied late. */
7176 if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
7177 ANY_INTEGRAL_TYPE_P (type)
7178 && TYPE_OVERFLOW_TRAPS (type), op1))
7179 return NULL_TREE;
7181 if (TREE_CODE (cond) == COND_EXPR
7182 || TREE_CODE (cond) == VEC_COND_EXPR)
7184 test = TREE_OPERAND (cond, 0);
7185 true_value = TREE_OPERAND (cond, 1);
7186 false_value = TREE_OPERAND (cond, 2);
7187 /* If this operand throws an expression, then it does not make
7188 sense to try to perform a logical or arithmetic operation
7189 involving it. */
7190 if (VOID_TYPE_P (TREE_TYPE (true_value)))
7191 lhs = true_value;
7192 if (VOID_TYPE_P (TREE_TYPE (false_value)))
7193 rhs = false_value;
7195 else if (!(TREE_CODE (type) != VECTOR_TYPE
7196 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
7198 tree testtype = TREE_TYPE (cond);
7199 test = cond;
7200 true_value = constant_boolean_node (true, testtype);
7201 false_value = constant_boolean_node (false, testtype);
7203 else
7204 /* Detect the case of mixing vector and scalar types - bail out. */
7205 return NULL_TREE;
7207 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
7208 cond_code = VEC_COND_EXPR;
7210 /* This transformation is only worthwhile if we don't have to wrap ARG
7211 in a SAVE_EXPR and the operation can be simplified without recursing
7212 on at least one of the branches once its pushed inside the COND_EXPR. */
7213 if (!TREE_CONSTANT (arg)
7214 && (TREE_SIDE_EFFECTS (arg)
7215 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
7216 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
7217 return NULL_TREE;
7219 arg = fold_convert_loc (loc, arg_type, arg);
7220 if (lhs == 0)
7222 true_value = fold_convert_loc (loc, cond_type, true_value);
7223 if (cond_first_p)
7224 lhs = fold_build2_loc (loc, code, type, true_value, arg);
7225 else
7226 lhs = fold_build2_loc (loc, code, type, arg, true_value);
7228 if (rhs == 0)
7230 false_value = fold_convert_loc (loc, cond_type, false_value);
7231 if (cond_first_p)
7232 rhs = fold_build2_loc (loc, code, type, false_value, arg);
7233 else
7234 rhs = fold_build2_loc (loc, code, type, arg, false_value);
7237 /* Check that we have simplified at least one of the branches. */
7238 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
7239 return NULL_TREE;
7241 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
7245 /* Subroutine of fold() that checks for the addition of ARG +/- 0.0.
7247 If !NEGATE, return true if ZERO_ARG is +/-0.0 and, for all ARG of
7248 type TYPE, ARG + ZERO_ARG is the same as ARG. If NEGATE, return true
7249 if ARG - ZERO_ARG is the same as X.
7251 If ARG is NULL, check for any value of type TYPE.
7253 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
7254 and finite. The problematic cases are when X is zero, and its mode
7255 has signed zeros. In the case of rounding towards -infinity,
7256 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
7257 modes, X + 0 is not the same as X because -0 + 0 is 0. */
7259 bool
7260 fold_real_zero_addition_p (const_tree type, const_tree arg,
7261 const_tree zero_arg, int negate)
7263 if (!real_zerop (zero_arg))
7264 return false;
7266 /* Don't allow the fold with -fsignaling-nans. */
7267 if (arg ? tree_expr_maybe_signaling_nan_p (arg) : HONOR_SNANS (type))
7268 return false;
7270 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
7271 if (!HONOR_SIGNED_ZEROS (type))
7272 return true;
7274 /* There is no case that is safe for all rounding modes. */
7275 if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
7276 return false;
7278 /* In a vector or complex, we would need to check the sign of all zeros. */
7279 if (TREE_CODE (zero_arg) == VECTOR_CST)
7280 zero_arg = uniform_vector_p (zero_arg);
7281 if (!zero_arg || TREE_CODE (zero_arg) != REAL_CST)
7282 return false;
7284 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
7285 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (zero_arg)))
7286 negate = !negate;
7288 /* The mode has signed zeros, and we have to honor their sign.
7289 In this situation, there are only two cases we can return true for.
7290 (i) X - 0 is the same as X with default rounding.
7291 (ii) X + 0 is X when X can't possibly be -0.0. */
7292 return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
7295 /* Subroutine of match.pd that optimizes comparisons of a division by
7296 a nonzero integer constant against an integer constant, i.e.
7297 X/C1 op C2.
7299 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7300 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
7302 enum tree_code
7303 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
7304 tree *hi, bool *neg_overflow)
7306 tree prod, tmp, type = TREE_TYPE (c1);
7307 signop sign = TYPE_SIGN (type);
7308 wi::overflow_type overflow;
7310 /* We have to do this the hard way to detect unsigned overflow.
7311 prod = int_const_binop (MULT_EXPR, c1, c2); */
7312 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
7313 prod = force_fit_type (type, val, -1, overflow);
7314 *neg_overflow = false;
7316 if (sign == UNSIGNED)
7318 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7319 *lo = prod;
7321 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
7322 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
7323 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
7325 else if (tree_int_cst_sgn (c1) >= 0)
7327 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
7328 switch (tree_int_cst_sgn (c2))
7330 case -1:
7331 *neg_overflow = true;
7332 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
7333 *hi = prod;
7334 break;
7336 case 0:
7337 *lo = fold_negate_const (tmp, type);
7338 *hi = tmp;
7339 break;
7341 case 1:
7342 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
7343 *lo = prod;
7344 break;
7346 default:
7347 gcc_unreachable ();
7350 else
7352 /* A negative divisor reverses the relational operators. */
7353 code = swap_tree_comparison (code);
7355 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
7356 switch (tree_int_cst_sgn (c2))
7358 case -1:
7359 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
7360 *lo = prod;
7361 break;
7363 case 0:
7364 *hi = fold_negate_const (tmp, type);
7365 *lo = tmp;
7366 break;
7368 case 1:
7369 *neg_overflow = true;
7370 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
7371 *hi = prod;
7372 break;
7374 default:
7375 gcc_unreachable ();
7379 if (code != EQ_EXPR && code != NE_EXPR)
7380 return code;
7382 if (TREE_OVERFLOW (*lo)
7383 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
7384 *lo = NULL_TREE;
7385 if (TREE_OVERFLOW (*hi)
7386 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
7387 *hi = NULL_TREE;
7389 return code;
7393 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7394 equality/inequality test, then return a simplified form of the test
7395 using a sign testing. Otherwise return NULL. TYPE is the desired
7396 result type. */
7398 static tree
7399 fold_single_bit_test_into_sign_test (location_t loc,
7400 enum tree_code code, tree arg0, tree arg1,
7401 tree result_type)
7403 /* If this is testing a single bit, we can optimize the test. */
7404 if ((code == NE_EXPR || code == EQ_EXPR)
7405 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
7406 && integer_pow2p (TREE_OPERAND (arg0, 1)))
7408 /* If we have (A & C) != 0 where C is the sign bit of A, convert
7409 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
7410 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
7412 if (arg00 != NULL_TREE
7413 /* This is only a win if casting to a signed type is cheap,
7414 i.e. when arg00's type is not a partial mode. */
7415 && type_has_mode_precision_p (TREE_TYPE (arg00)))
7417 tree stype = signed_type_for (TREE_TYPE (arg00));
7418 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
7419 result_type,
7420 fold_convert_loc (loc, stype, arg00),
7421 build_int_cst (stype, 0));
7425 return NULL_TREE;
7428 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7429 equality/inequality test, then return a simplified form of
7430 the test using shifts and logical operations. Otherwise return
7431 NULL. TYPE is the desired result type. */
7433 tree
7434 fold_single_bit_test (location_t loc, enum tree_code code,
7435 tree arg0, tree arg1, tree result_type)
7437 /* If this is testing a single bit, we can optimize the test. */
7438 if ((code == NE_EXPR || code == EQ_EXPR)
7439 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
7440 && integer_pow2p (TREE_OPERAND (arg0, 1)))
7442 tree inner = TREE_OPERAND (arg0, 0);
7443 tree type = TREE_TYPE (arg0);
7444 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
7445 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
7446 int ops_unsigned;
7447 tree signed_type, unsigned_type, intermediate_type;
7448 tree tem, one;
7450 /* First, see if we can fold the single bit test into a sign-bit
7451 test. */
7452 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
7453 result_type);
7454 if (tem)
7455 return tem;
7457 /* Otherwise we have (A & C) != 0 where C is a single bit,
7458 convert that into ((A >> C2) & 1). Where C2 = log2(C).
7459 Similarly for (A & C) == 0. */
7461 /* If INNER is a right shift of a constant and it plus BITNUM does
7462 not overflow, adjust BITNUM and INNER. */
7463 if (TREE_CODE (inner) == RSHIFT_EXPR
7464 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
7465 && bitnum < TYPE_PRECISION (type)
7466 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner, 1)),
7467 TYPE_PRECISION (type) - bitnum))
7469 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
7470 inner = TREE_OPERAND (inner, 0);
7473 /* If we are going to be able to omit the AND below, we must do our
7474 operations as unsigned. If we must use the AND, we have a choice.
7475 Normally unsigned is faster, but for some machines signed is. */
7476 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
7477 && !flag_syntax_only) ? 0 : 1;
7479 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
7480 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
7481 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
7482 inner = fold_convert_loc (loc, intermediate_type, inner);
7484 if (bitnum != 0)
7485 inner = build2 (RSHIFT_EXPR, intermediate_type,
7486 inner, size_int (bitnum));
7488 one = build_int_cst (intermediate_type, 1);
7490 if (code == EQ_EXPR)
7491 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
7493 /* Put the AND last so it can combine with more things. */
7494 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
7496 /* Make sure to return the proper type. */
7497 inner = fold_convert_loc (loc, result_type, inner);
7499 return inner;
7501 return NULL_TREE;
7504 /* Test whether it is preferable to swap two operands, ARG0 and
7505 ARG1, for example because ARG0 is an integer constant and ARG1
7506 isn't. */
7508 bool
7509 tree_swap_operands_p (const_tree arg0, const_tree arg1)
7511 if (CONSTANT_CLASS_P (arg1))
7512 return 0;
7513 if (CONSTANT_CLASS_P (arg0))
7514 return 1;
7516 STRIP_NOPS (arg0);
7517 STRIP_NOPS (arg1);
7519 if (TREE_CONSTANT (arg1))
7520 return 0;
7521 if (TREE_CONSTANT (arg0))
7522 return 1;
7524 /* It is preferable to swap two SSA_NAME to ensure a canonical form
7525 for commutative and comparison operators. Ensuring a canonical
7526 form allows the optimizers to find additional redundancies without
7527 having to explicitly check for both orderings. */
7528 if (TREE_CODE (arg0) == SSA_NAME
7529 && TREE_CODE (arg1) == SSA_NAME
7530 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7531 return 1;
7533 /* Put SSA_NAMEs last. */
7534 if (TREE_CODE (arg1) == SSA_NAME)
7535 return 0;
7536 if (TREE_CODE (arg0) == SSA_NAME)
7537 return 1;
7539 /* Put variables last. */
7540 if (DECL_P (arg1))
7541 return 0;
7542 if (DECL_P (arg0))
7543 return 1;
7545 return 0;
7549 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7550 means A >= Y && A != MAX, but in this case we know that
7551 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7553 static tree
7554 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7556 tree a, typea, type = TREE_TYPE (bound), a1, diff, y;
7558 if (TREE_CODE (bound) == LT_EXPR)
7559 a = TREE_OPERAND (bound, 0);
7560 else if (TREE_CODE (bound) == GT_EXPR)
7561 a = TREE_OPERAND (bound, 1);
7562 else
7563 return NULL_TREE;
7565 typea = TREE_TYPE (a);
7566 if (!INTEGRAL_TYPE_P (typea)
7567 && !POINTER_TYPE_P (typea))
7568 return NULL_TREE;
7570 if (TREE_CODE (ineq) == LT_EXPR)
7572 a1 = TREE_OPERAND (ineq, 1);
7573 y = TREE_OPERAND (ineq, 0);
7575 else if (TREE_CODE (ineq) == GT_EXPR)
7577 a1 = TREE_OPERAND (ineq, 0);
7578 y = TREE_OPERAND (ineq, 1);
7580 else
7581 return NULL_TREE;
7583 if (TREE_TYPE (a1) != typea)
7584 return NULL_TREE;
7586 if (POINTER_TYPE_P (typea))
7588 /* Convert the pointer types into integer before taking the difference. */
7589 tree ta = fold_convert_loc (loc, ssizetype, a);
7590 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7591 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7593 else
7594 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7596 if (!diff || !integer_onep (diff))
7597 return NULL_TREE;
7599 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7602 /* Fold a sum or difference of at least one multiplication.
7603 Returns the folded tree or NULL if no simplification could be made. */
7605 static tree
7606 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7607 tree arg0, tree arg1)
7609 tree arg00, arg01, arg10, arg11;
7610 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7612 /* (A * C) +- (B * C) -> (A+-B) * C.
7613 (A * C) +- A -> A * (C+-1).
7614 We are most concerned about the case where C is a constant,
7615 but other combinations show up during loop reduction. Since
7616 it is not difficult, try all four possibilities. */
7618 if (TREE_CODE (arg0) == MULT_EXPR)
7620 arg00 = TREE_OPERAND (arg0, 0);
7621 arg01 = TREE_OPERAND (arg0, 1);
7623 else if (TREE_CODE (arg0) == INTEGER_CST)
7625 arg00 = build_one_cst (type);
7626 arg01 = arg0;
7628 else
7630 /* We cannot generate constant 1 for fract. */
7631 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7632 return NULL_TREE;
7633 arg00 = arg0;
7634 arg01 = build_one_cst (type);
7636 if (TREE_CODE (arg1) == MULT_EXPR)
7638 arg10 = TREE_OPERAND (arg1, 0);
7639 arg11 = TREE_OPERAND (arg1, 1);
7641 else if (TREE_CODE (arg1) == INTEGER_CST)
7643 arg10 = build_one_cst (type);
7644 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7645 the purpose of this canonicalization. */
7646 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7647 && negate_expr_p (arg1)
7648 && code == PLUS_EXPR)
7650 arg11 = negate_expr (arg1);
7651 code = MINUS_EXPR;
7653 else
7654 arg11 = arg1;
7656 else
7658 /* We cannot generate constant 1 for fract. */
7659 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7660 return NULL_TREE;
7661 arg10 = arg1;
7662 arg11 = build_one_cst (type);
7664 same = NULL_TREE;
7666 /* Prefer factoring a common non-constant. */
7667 if (operand_equal_p (arg00, arg10, 0))
7668 same = arg00, alt0 = arg01, alt1 = arg11;
7669 else if (operand_equal_p (arg01, arg11, 0))
7670 same = arg01, alt0 = arg00, alt1 = arg10;
7671 else if (operand_equal_p (arg00, arg11, 0))
7672 same = arg00, alt0 = arg01, alt1 = arg10;
7673 else if (operand_equal_p (arg01, arg10, 0))
7674 same = arg01, alt0 = arg00, alt1 = arg11;
7676 /* No identical multiplicands; see if we can find a common
7677 power-of-two factor in non-power-of-two multiplies. This
7678 can help in multi-dimensional array access. */
7679 else if (tree_fits_shwi_p (arg01) && tree_fits_shwi_p (arg11))
7681 HOST_WIDE_INT int01 = tree_to_shwi (arg01);
7682 HOST_WIDE_INT int11 = tree_to_shwi (arg11);
7683 HOST_WIDE_INT tmp;
7684 bool swap = false;
7685 tree maybe_same;
7687 /* Move min of absolute values to int11. */
7688 if (absu_hwi (int01) < absu_hwi (int11))
7690 tmp = int01, int01 = int11, int11 = tmp;
7691 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7692 maybe_same = arg01;
7693 swap = true;
7695 else
7696 maybe_same = arg11;
7698 const unsigned HOST_WIDE_INT factor = absu_hwi (int11);
7699 if (factor > 1
7700 && pow2p_hwi (factor)
7701 && (int01 & (factor - 1)) == 0
7702 /* The remainder should not be a constant, otherwise we
7703 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7704 increased the number of multiplications necessary. */
7705 && TREE_CODE (arg10) != INTEGER_CST)
7707 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7708 build_int_cst (TREE_TYPE (arg00),
7709 int01 / int11));
7710 alt1 = arg10;
7711 same = maybe_same;
7712 if (swap)
7713 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7717 if (!same)
7718 return NULL_TREE;
7720 if (! ANY_INTEGRAL_TYPE_P (type)
7721 || TYPE_OVERFLOW_WRAPS (type)
7722 /* We are neither factoring zero nor minus one. */
7723 || TREE_CODE (same) == INTEGER_CST)
7724 return fold_build2_loc (loc, MULT_EXPR, type,
7725 fold_build2_loc (loc, code, type,
7726 fold_convert_loc (loc, type, alt0),
7727 fold_convert_loc (loc, type, alt1)),
7728 fold_convert_loc (loc, type, same));
7730 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7731 same may be minus one and thus the multiplication may overflow. Perform
7732 the sum operation in an unsigned type. */
7733 tree utype = unsigned_type_for (type);
7734 tree tem = fold_build2_loc (loc, code, utype,
7735 fold_convert_loc (loc, utype, alt0),
7736 fold_convert_loc (loc, utype, alt1));
7737 /* If the sum evaluated to a constant that is not -INF the multiplication
7738 cannot overflow. */
7739 if (TREE_CODE (tem) == INTEGER_CST
7740 && (wi::to_wide (tem)
7741 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7742 return fold_build2_loc (loc, MULT_EXPR, type,
7743 fold_convert (type, tem), same);
7745 /* Do not resort to unsigned multiplication because
7746 we lose the no-overflow property of the expression. */
7747 return NULL_TREE;
7750 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7751 specified by EXPR into the buffer PTR of length LEN bytes.
7752 Return the number of bytes placed in the buffer, or zero
7753 upon failure. */
7755 static int
7756 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7758 tree type = TREE_TYPE (expr);
7759 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7760 int byte, offset, word, words;
7761 unsigned char value;
7763 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7764 return 0;
7765 if (off == -1)
7766 off = 0;
7768 if (ptr == NULL)
7769 /* Dry run. */
7770 return MIN (len, total_bytes - off);
7772 words = total_bytes / UNITS_PER_WORD;
7774 for (byte = 0; byte < total_bytes; byte++)
7776 int bitpos = byte * BITS_PER_UNIT;
7777 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7778 number of bytes. */
7779 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7781 if (total_bytes > UNITS_PER_WORD)
7783 word = byte / UNITS_PER_WORD;
7784 if (WORDS_BIG_ENDIAN)
7785 word = (words - 1) - word;
7786 offset = word * UNITS_PER_WORD;
7787 if (BYTES_BIG_ENDIAN)
7788 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7789 else
7790 offset += byte % UNITS_PER_WORD;
7792 else
7793 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7794 if (offset >= off && offset - off < len)
7795 ptr[offset - off] = value;
7797 return MIN (len, total_bytes - off);
7801 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7802 specified by EXPR into the buffer PTR of length LEN bytes.
7803 Return the number of bytes placed in the buffer, or zero
7804 upon failure. */
7806 static int
7807 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7809 tree type = TREE_TYPE (expr);
7810 scalar_mode mode = SCALAR_TYPE_MODE (type);
7811 int total_bytes = GET_MODE_SIZE (mode);
7812 FIXED_VALUE_TYPE value;
7813 tree i_value, i_type;
7815 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7816 return 0;
7818 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7820 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7821 return 0;
7823 value = TREE_FIXED_CST (expr);
7824 i_value = double_int_to_tree (i_type, value.data);
7826 return native_encode_int (i_value, ptr, len, off);
7830 /* Subroutine of native_encode_expr. Encode the REAL_CST
7831 specified by EXPR into the buffer PTR of length LEN bytes.
7832 Return the number of bytes placed in the buffer, or zero
7833 upon failure. */
7835 static int
7836 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7838 tree type = TREE_TYPE (expr);
7839 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7840 int byte, offset, word, words, bitpos;
7841 unsigned char value;
7843 /* There are always 32 bits in each long, no matter the size of
7844 the hosts long. We handle floating point representations with
7845 up to 192 bits. */
7846 long tmp[6];
7848 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7849 return 0;
7850 if (off == -1)
7851 off = 0;
7853 if (ptr == NULL)
7854 /* Dry run. */
7855 return MIN (len, total_bytes - off);
7857 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7859 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7861 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7862 bitpos += BITS_PER_UNIT)
7864 byte = (bitpos / BITS_PER_UNIT) & 3;
7865 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7867 if (UNITS_PER_WORD < 4)
7869 word = byte / UNITS_PER_WORD;
7870 if (WORDS_BIG_ENDIAN)
7871 word = (words - 1) - word;
7872 offset = word * UNITS_PER_WORD;
7873 if (BYTES_BIG_ENDIAN)
7874 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7875 else
7876 offset += byte % UNITS_PER_WORD;
7878 else
7880 offset = byte;
7881 if (BYTES_BIG_ENDIAN)
7883 /* Reverse bytes within each long, or within the entire float
7884 if it's smaller than a long (for HFmode). */
7885 offset = MIN (3, total_bytes - 1) - offset;
7886 gcc_assert (offset >= 0);
7889 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7890 if (offset >= off
7891 && offset - off < len)
7892 ptr[offset - off] = value;
7894 return MIN (len, total_bytes - off);
7897 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7898 specified by EXPR into the buffer PTR of length LEN bytes.
7899 Return the number of bytes placed in the buffer, or zero
7900 upon failure. */
7902 static int
7903 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7905 int rsize, isize;
7906 tree part;
7908 part = TREE_REALPART (expr);
7909 rsize = native_encode_expr (part, ptr, len, off);
7910 if (off == -1 && rsize == 0)
7911 return 0;
7912 part = TREE_IMAGPART (expr);
7913 if (off != -1)
7914 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7915 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7916 len - rsize, off);
7917 if (off == -1 && isize != rsize)
7918 return 0;
7919 return rsize + isize;
7922 /* Like native_encode_vector, but only encode the first COUNT elements.
7923 The other arguments are as for native_encode_vector. */
7925 static int
7926 native_encode_vector_part (const_tree expr, unsigned char *ptr, int len,
7927 int off, unsigned HOST_WIDE_INT count)
7929 tree itype = TREE_TYPE (TREE_TYPE (expr));
7930 if (VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
7931 && TYPE_PRECISION (itype) <= BITS_PER_UNIT)
7933 /* This is the only case in which elements can be smaller than a byte.
7934 Element 0 is always in the lsb of the containing byte. */
7935 unsigned int elt_bits = TYPE_PRECISION (itype);
7936 int total_bytes = CEIL (elt_bits * count, BITS_PER_UNIT);
7937 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7938 return 0;
7940 if (off == -1)
7941 off = 0;
7943 /* Zero the buffer and then set bits later where necessary. */
7944 int extract_bytes = MIN (len, total_bytes - off);
7945 if (ptr)
7946 memset (ptr, 0, extract_bytes);
7948 unsigned int elts_per_byte = BITS_PER_UNIT / elt_bits;
7949 unsigned int first_elt = off * elts_per_byte;
7950 unsigned int extract_elts = extract_bytes * elts_per_byte;
7951 for (unsigned int i = 0; i < extract_elts; ++i)
7953 tree elt = VECTOR_CST_ELT (expr, first_elt + i);
7954 if (TREE_CODE (elt) != INTEGER_CST)
7955 return 0;
7957 if (ptr && wi::extract_uhwi (wi::to_wide (elt), 0, 1))
7959 unsigned int bit = i * elt_bits;
7960 ptr[bit / BITS_PER_UNIT] |= 1 << (bit % BITS_PER_UNIT);
7963 return extract_bytes;
7966 int offset = 0;
7967 int size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7968 for (unsigned HOST_WIDE_INT i = 0; i < count; i++)
7970 if (off >= size)
7972 off -= size;
7973 continue;
7975 tree elem = VECTOR_CST_ELT (expr, i);
7976 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7977 len - offset, off);
7978 if ((off == -1 && res != size) || res == 0)
7979 return 0;
7980 offset += res;
7981 if (offset >= len)
7982 return (off == -1 && i < count - 1) ? 0 : offset;
7983 if (off != -1)
7984 off = 0;
7986 return offset;
7989 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7990 specified by EXPR into the buffer PTR of length LEN bytes.
7991 Return the number of bytes placed in the buffer, or zero
7992 upon failure. */
7994 static int
7995 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7997 unsigned HOST_WIDE_INT count;
7998 if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7999 return 0;
8000 return native_encode_vector_part (expr, ptr, len, off, count);
8004 /* Subroutine of native_encode_expr. Encode the STRING_CST
8005 specified by EXPR into the buffer PTR of length LEN bytes.
8006 Return the number of bytes placed in the buffer, or zero
8007 upon failure. */
8009 static int
8010 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
8012 tree type = TREE_TYPE (expr);
8014 /* Wide-char strings are encoded in target byte-order so native
8015 encoding them is trivial. */
8016 if (BITS_PER_UNIT != CHAR_BIT
8017 || TREE_CODE (type) != ARRAY_TYPE
8018 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
8019 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
8020 return 0;
8022 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
8023 if ((off == -1 && total_bytes > len) || off >= total_bytes)
8024 return 0;
8025 if (off == -1)
8026 off = 0;
8027 len = MIN (total_bytes - off, len);
8028 if (ptr == NULL)
8029 /* Dry run. */;
8030 else
8032 int written = 0;
8033 if (off < TREE_STRING_LENGTH (expr))
8035 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
8036 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
8038 memset (ptr + written, 0, len - written);
8040 return len;
8044 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST, REAL_CST,
8045 FIXED_CST, COMPLEX_CST, STRING_CST, or VECTOR_CST specified by EXPR into
8046 the buffer PTR of size LEN bytes. If PTR is NULL, don't actually store
8047 anything, just do a dry run. Fail either if OFF is -1 and LEN isn't
8048 sufficient to encode the entire EXPR, or if OFF is out of bounds.
8049 Otherwise, start at byte offset OFF and encode at most LEN bytes.
8050 Return the number of bytes placed in the buffer, or zero upon failure. */
8053 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
8055 /* We don't support starting at negative offset and -1 is special. */
8056 if (off < -1)
8057 return 0;
8059 switch (TREE_CODE (expr))
8061 case INTEGER_CST:
8062 return native_encode_int (expr, ptr, len, off);
8064 case REAL_CST:
8065 return native_encode_real (expr, ptr, len, off);
8067 case FIXED_CST:
8068 return native_encode_fixed (expr, ptr, len, off);
8070 case COMPLEX_CST:
8071 return native_encode_complex (expr, ptr, len, off);
8073 case VECTOR_CST:
8074 return native_encode_vector (expr, ptr, len, off);
8076 case STRING_CST:
8077 return native_encode_string (expr, ptr, len, off);
8079 default:
8080 return 0;
8084 /* Try to find a type whose byte size is smaller or equal to LEN bytes larger
8085 or equal to FIELDSIZE bytes, with underlying mode precision/size multiple
8086 of BITS_PER_UNIT. As native_{interpret,encode}_int works in term of
8087 machine modes, we can't just use build_nonstandard_integer_type. */
8089 tree
8090 find_bitfield_repr_type (int fieldsize, int len)
8092 machine_mode mode;
8093 for (int pass = 0; pass < 2; pass++)
8095 enum mode_class mclass = pass ? MODE_PARTIAL_INT : MODE_INT;
8096 FOR_EACH_MODE_IN_CLASS (mode, mclass)
8097 if (known_ge (GET_MODE_SIZE (mode), fieldsize)
8098 && known_eq (GET_MODE_PRECISION (mode),
8099 GET_MODE_BITSIZE (mode))
8100 && known_le (GET_MODE_SIZE (mode), len))
8102 tree ret = lang_hooks.types.type_for_mode (mode, 1);
8103 if (ret && TYPE_MODE (ret) == mode)
8104 return ret;
8108 for (int i = 0; i < NUM_INT_N_ENTS; i ++)
8109 if (int_n_enabled_p[i]
8110 && int_n_data[i].bitsize >= (unsigned) (BITS_PER_UNIT * fieldsize)
8111 && int_n_trees[i].unsigned_type)
8113 tree ret = int_n_trees[i].unsigned_type;
8114 mode = TYPE_MODE (ret);
8115 if (known_ge (GET_MODE_SIZE (mode), fieldsize)
8116 && known_eq (GET_MODE_PRECISION (mode),
8117 GET_MODE_BITSIZE (mode))
8118 && known_le (GET_MODE_SIZE (mode), len))
8119 return ret;
8122 return NULL_TREE;
8125 /* Similar to native_encode_expr, but also handle CONSTRUCTORs, VCEs,
8126 NON_LVALUE_EXPRs and nops. If MASK is non-NULL (then PTR has
8127 to be non-NULL and OFF zero), then in addition to filling the
8128 bytes pointed by PTR with the value also clear any bits pointed
8129 by MASK that are known to be initialized, keep them as is for
8130 e.g. uninitialized padding bits or uninitialized fields. */
8133 native_encode_initializer (tree init, unsigned char *ptr, int len,
8134 int off, unsigned char *mask)
8136 int r;
8138 /* We don't support starting at negative offset and -1 is special. */
8139 if (off < -1 || init == NULL_TREE)
8140 return 0;
8142 gcc_assert (mask == NULL || (off == 0 && ptr));
8144 STRIP_NOPS (init);
8145 switch (TREE_CODE (init))
8147 case VIEW_CONVERT_EXPR:
8148 case NON_LVALUE_EXPR:
8149 return native_encode_initializer (TREE_OPERAND (init, 0), ptr, len, off,
8150 mask);
8151 default:
8152 r = native_encode_expr (init, ptr, len, off);
8153 if (mask)
8154 memset (mask, 0, r);
8155 return r;
8156 case CONSTRUCTOR:
8157 tree type = TREE_TYPE (init);
8158 HOST_WIDE_INT total_bytes = int_size_in_bytes (type);
8159 if (total_bytes < 0)
8160 return 0;
8161 if ((off == -1 && total_bytes > len) || off >= total_bytes)
8162 return 0;
8163 int o = off == -1 ? 0 : off;
8164 if (TREE_CODE (type) == ARRAY_TYPE)
8166 tree min_index;
8167 unsigned HOST_WIDE_INT cnt;
8168 HOST_WIDE_INT curpos = 0, fieldsize, valueinit = -1;
8169 constructor_elt *ce;
8171 if (!TYPE_DOMAIN (type)
8172 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
8173 return 0;
8175 fieldsize = int_size_in_bytes (TREE_TYPE (type));
8176 if (fieldsize <= 0)
8177 return 0;
8179 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
8180 if (ptr)
8181 memset (ptr, '\0', MIN (total_bytes - off, len));
8183 for (cnt = 0; ; cnt++)
8185 tree val = NULL_TREE, index = NULL_TREE;
8186 HOST_WIDE_INT pos = curpos, count = 0;
8187 bool full = false;
8188 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8190 val = ce->value;
8191 index = ce->index;
8193 else if (mask == NULL
8194 || CONSTRUCTOR_NO_CLEARING (init)
8195 || curpos >= total_bytes)
8196 break;
8197 else
8198 pos = total_bytes;
8200 if (index && TREE_CODE (index) == RANGE_EXPR)
8202 if (TREE_CODE (TREE_OPERAND (index, 0)) != INTEGER_CST
8203 || TREE_CODE (TREE_OPERAND (index, 1)) != INTEGER_CST)
8204 return 0;
8205 offset_int wpos
8206 = wi::sext (wi::to_offset (TREE_OPERAND (index, 0))
8207 - wi::to_offset (min_index),
8208 TYPE_PRECISION (sizetype));
8209 wpos *= fieldsize;
8210 if (!wi::fits_shwi_p (pos))
8211 return 0;
8212 pos = wpos.to_shwi ();
8213 offset_int wcount
8214 = wi::sext (wi::to_offset (TREE_OPERAND (index, 1))
8215 - wi::to_offset (TREE_OPERAND (index, 0)),
8216 TYPE_PRECISION (sizetype));
8217 if (!wi::fits_shwi_p (wcount))
8218 return 0;
8219 count = wcount.to_shwi ();
8221 else if (index)
8223 if (TREE_CODE (index) != INTEGER_CST)
8224 return 0;
8225 offset_int wpos
8226 = wi::sext (wi::to_offset (index)
8227 - wi::to_offset (min_index),
8228 TYPE_PRECISION (sizetype));
8229 wpos *= fieldsize;
8230 if (!wi::fits_shwi_p (wpos))
8231 return 0;
8232 pos = wpos.to_shwi ();
8235 if (mask && !CONSTRUCTOR_NO_CLEARING (init) && curpos != pos)
8237 if (valueinit == -1)
8239 tree zero = build_zero_cst (TREE_TYPE (type));
8240 r = native_encode_initializer (zero, ptr + curpos,
8241 fieldsize, 0,
8242 mask + curpos);
8243 if (TREE_CODE (zero) == CONSTRUCTOR)
8244 ggc_free (zero);
8245 if (!r)
8246 return 0;
8247 valueinit = curpos;
8248 curpos += fieldsize;
8250 while (curpos != pos)
8252 memcpy (ptr + curpos, ptr + valueinit, fieldsize);
8253 memcpy (mask + curpos, mask + valueinit, fieldsize);
8254 curpos += fieldsize;
8258 curpos = pos;
8259 if (val)
8262 if (off == -1
8263 || (curpos >= off
8264 && (curpos + fieldsize
8265 <= (HOST_WIDE_INT) off + len)))
8267 if (full)
8269 if (ptr)
8270 memcpy (ptr + (curpos - o), ptr + (pos - o),
8271 fieldsize);
8272 if (mask)
8273 memcpy (mask + curpos, mask + pos, fieldsize);
8275 else if (!native_encode_initializer (val,
8277 ? ptr + curpos - o
8278 : NULL,
8279 fieldsize,
8280 off == -1 ? -1
8281 : 0,
8282 mask
8283 ? mask + curpos
8284 : NULL))
8285 return 0;
8286 else
8288 full = true;
8289 pos = curpos;
8292 else if (curpos + fieldsize > off
8293 && curpos < (HOST_WIDE_INT) off + len)
8295 /* Partial overlap. */
8296 unsigned char *p = NULL;
8297 int no = 0;
8298 int l;
8299 gcc_assert (mask == NULL);
8300 if (curpos >= off)
8302 if (ptr)
8303 p = ptr + curpos - off;
8304 l = MIN ((HOST_WIDE_INT) off + len - curpos,
8305 fieldsize);
8307 else
8309 p = ptr;
8310 no = off - curpos;
8311 l = len;
8313 if (!native_encode_initializer (val, p, l, no, NULL))
8314 return 0;
8316 curpos += fieldsize;
8318 while (count-- != 0);
8320 return MIN (total_bytes - off, len);
8322 else if (TREE_CODE (type) == RECORD_TYPE
8323 || TREE_CODE (type) == UNION_TYPE)
8325 unsigned HOST_WIDE_INT cnt;
8326 constructor_elt *ce;
8327 tree fld_base = TYPE_FIELDS (type);
8328 tree to_free = NULL_TREE;
8330 gcc_assert (TREE_CODE (type) == RECORD_TYPE || mask == NULL);
8331 if (ptr != NULL)
8332 memset (ptr, '\0', MIN (total_bytes - o, len));
8333 for (cnt = 0; ; cnt++)
8335 tree val = NULL_TREE, field = NULL_TREE;
8336 HOST_WIDE_INT pos = 0, fieldsize;
8337 unsigned HOST_WIDE_INT bpos = 0, epos = 0;
8339 if (to_free)
8341 ggc_free (to_free);
8342 to_free = NULL_TREE;
8345 if (vec_safe_iterate (CONSTRUCTOR_ELTS (init), cnt, &ce))
8347 val = ce->value;
8348 field = ce->index;
8349 if (field == NULL_TREE)
8350 return 0;
8352 pos = int_byte_position (field);
8353 if (off != -1 && (HOST_WIDE_INT) off + len <= pos)
8354 continue;
8356 else if (mask == NULL
8357 || CONSTRUCTOR_NO_CLEARING (init))
8358 break;
8359 else
8360 pos = total_bytes;
8362 if (mask && !CONSTRUCTOR_NO_CLEARING (init))
8364 tree fld;
8365 for (fld = fld_base; fld; fld = DECL_CHAIN (fld))
8367 if (TREE_CODE (fld) != FIELD_DECL)
8368 continue;
8369 if (fld == field)
8370 break;
8371 if (DECL_PADDING_P (fld))
8372 continue;
8373 if (DECL_SIZE_UNIT (fld) == NULL_TREE
8374 || !tree_fits_shwi_p (DECL_SIZE_UNIT (fld)))
8375 return 0;
8376 if (integer_zerop (DECL_SIZE_UNIT (fld)))
8377 continue;
8378 break;
8380 if (fld == NULL_TREE)
8382 if (ce == NULL)
8383 break;
8384 return 0;
8386 fld_base = DECL_CHAIN (fld);
8387 if (fld != field)
8389 cnt--;
8390 field = fld;
8391 pos = int_byte_position (field);
8392 val = build_zero_cst (TREE_TYPE (fld));
8393 if (TREE_CODE (val) == CONSTRUCTOR)
8394 to_free = val;
8398 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
8399 && TYPE_DOMAIN (TREE_TYPE (field))
8400 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
8402 if (mask || off != -1)
8403 return 0;
8404 if (val == NULL_TREE)
8405 continue;
8406 if (TREE_CODE (TREE_TYPE (val)) != ARRAY_TYPE)
8407 return 0;
8408 fieldsize = int_size_in_bytes (TREE_TYPE (val));
8409 if (fieldsize < 0
8410 || (int) fieldsize != fieldsize
8411 || (pos + fieldsize) > INT_MAX)
8412 return 0;
8413 if (pos + fieldsize > total_bytes)
8415 if (ptr != NULL && total_bytes < len)
8416 memset (ptr + total_bytes, '\0',
8417 MIN (pos + fieldsize, len) - total_bytes);
8418 total_bytes = pos + fieldsize;
8421 else
8423 if (DECL_SIZE_UNIT (field) == NULL_TREE
8424 || !tree_fits_shwi_p (DECL_SIZE_UNIT (field)))
8425 return 0;
8426 fieldsize = tree_to_shwi (DECL_SIZE_UNIT (field));
8428 if (fieldsize == 0)
8429 continue;
8431 if (DECL_BIT_FIELD (field))
8433 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
8434 return 0;
8435 fieldsize = TYPE_PRECISION (TREE_TYPE (field));
8436 bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
8437 if (bpos % BITS_PER_UNIT)
8438 bpos %= BITS_PER_UNIT;
8439 else
8440 bpos = 0;
8441 fieldsize += bpos;
8442 epos = fieldsize % BITS_PER_UNIT;
8443 fieldsize += BITS_PER_UNIT - 1;
8444 fieldsize /= BITS_PER_UNIT;
8447 if (off != -1 && pos + fieldsize <= off)
8448 continue;
8450 if (val == NULL_TREE)
8451 continue;
8453 if (DECL_BIT_FIELD (field))
8455 /* FIXME: Handle PDP endian. */
8456 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
8457 return 0;
8459 if (TREE_CODE (val) != INTEGER_CST)
8460 return 0;
8462 tree repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
8463 tree repr_type = NULL_TREE;
8464 HOST_WIDE_INT rpos = 0;
8465 if (repr && INTEGRAL_TYPE_P (TREE_TYPE (repr)))
8467 rpos = int_byte_position (repr);
8468 repr_type = TREE_TYPE (repr);
8470 else
8472 repr_type = find_bitfield_repr_type (fieldsize, len);
8473 if (repr_type == NULL_TREE)
8474 return 0;
8475 HOST_WIDE_INT repr_size = int_size_in_bytes (repr_type);
8476 gcc_assert (repr_size > 0 && repr_size <= len);
8477 if (pos + repr_size <= o + len)
8478 rpos = pos;
8479 else
8481 rpos = o + len - repr_size;
8482 gcc_assert (rpos <= pos);
8486 if (rpos > pos)
8487 return 0;
8488 wide_int w = wi::to_wide (val, TYPE_PRECISION (repr_type));
8489 int diff = (TYPE_PRECISION (repr_type)
8490 - TYPE_PRECISION (TREE_TYPE (field)));
8491 HOST_WIDE_INT bitoff = (pos - rpos) * BITS_PER_UNIT + bpos;
8492 if (!BYTES_BIG_ENDIAN)
8493 w = wi::lshift (w, bitoff);
8494 else
8495 w = wi::lshift (w, diff - bitoff);
8496 val = wide_int_to_tree (repr_type, w);
8498 unsigned char buf[MAX_BITSIZE_MODE_ANY_INT
8499 / BITS_PER_UNIT + 1];
8500 int l = native_encode_int (val, buf, sizeof buf, 0);
8501 if (l * BITS_PER_UNIT != TYPE_PRECISION (repr_type))
8502 return 0;
8504 if (ptr == NULL)
8505 continue;
8507 /* If the bitfield does not start at byte boundary, handle
8508 the partial byte at the start. */
8509 if (bpos
8510 && (off == -1 || (pos >= off && len >= 1)))
8512 if (!BYTES_BIG_ENDIAN)
8514 int msk = (1 << bpos) - 1;
8515 buf[pos - rpos] &= ~msk;
8516 buf[pos - rpos] |= ptr[pos - o] & msk;
8517 if (mask)
8519 if (fieldsize > 1 || epos == 0)
8520 mask[pos] &= msk;
8521 else
8522 mask[pos] &= (msk | ~((1 << epos) - 1));
8525 else
8527 int msk = (1 << (BITS_PER_UNIT - bpos)) - 1;
8528 buf[pos - rpos] &= msk;
8529 buf[pos - rpos] |= ptr[pos - o] & ~msk;
8530 if (mask)
8532 if (fieldsize > 1 || epos == 0)
8533 mask[pos] &= ~msk;
8534 else
8535 mask[pos] &= (~msk
8536 | ((1 << (BITS_PER_UNIT - epos))
8537 - 1));
8541 /* If the bitfield does not end at byte boundary, handle
8542 the partial byte at the end. */
8543 if (epos
8544 && (off == -1
8545 || pos + fieldsize <= (HOST_WIDE_INT) off + len))
8547 if (!BYTES_BIG_ENDIAN)
8549 int msk = (1 << epos) - 1;
8550 buf[pos - rpos + fieldsize - 1] &= msk;
8551 buf[pos - rpos + fieldsize - 1]
8552 |= ptr[pos + fieldsize - 1 - o] & ~msk;
8553 if (mask && (fieldsize > 1 || bpos == 0))
8554 mask[pos + fieldsize - 1] &= ~msk;
8556 else
8558 int msk = (1 << (BITS_PER_UNIT - epos)) - 1;
8559 buf[pos - rpos + fieldsize - 1] &= ~msk;
8560 buf[pos - rpos + fieldsize - 1]
8561 |= ptr[pos + fieldsize - 1 - o] & msk;
8562 if (mask && (fieldsize > 1 || bpos == 0))
8563 mask[pos + fieldsize - 1] &= msk;
8566 if (off == -1
8567 || (pos >= off
8568 && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8570 memcpy (ptr + pos - o, buf + (pos - rpos), fieldsize);
8571 if (mask && (fieldsize > (bpos != 0) + (epos != 0)))
8572 memset (mask + pos + (bpos != 0), 0,
8573 fieldsize - (bpos != 0) - (epos != 0));
8575 else
8577 /* Partial overlap. */
8578 HOST_WIDE_INT fsz = fieldsize;
8579 gcc_assert (mask == NULL);
8580 if (pos < off)
8582 fsz -= (off - pos);
8583 pos = off;
8585 if (pos + fsz > (HOST_WIDE_INT) off + len)
8586 fsz = (HOST_WIDE_INT) off + len - pos;
8587 memcpy (ptr + pos - off, buf + (pos - rpos), fsz);
8589 continue;
8592 if (off == -1
8593 || (pos >= off
8594 && (pos + fieldsize <= (HOST_WIDE_INT) off + len)))
8596 int fldsize = fieldsize;
8597 if (off == -1)
8599 tree fld = DECL_CHAIN (field);
8600 while (fld)
8602 if (TREE_CODE (fld) == FIELD_DECL)
8603 break;
8604 fld = DECL_CHAIN (fld);
8606 if (fld == NULL_TREE)
8607 fldsize = len - pos;
8609 r = native_encode_initializer (val, ptr ? ptr + pos - o
8610 : NULL,
8611 fldsize,
8612 off == -1 ? -1 : 0,
8613 mask ? mask + pos : NULL);
8614 if (!r)
8615 return 0;
8616 if (off == -1
8617 && fldsize != fieldsize
8618 && r > fieldsize
8619 && pos + r > total_bytes)
8620 total_bytes = pos + r;
8622 else
8624 /* Partial overlap. */
8625 unsigned char *p = NULL;
8626 int no = 0;
8627 int l;
8628 gcc_assert (mask == NULL);
8629 if (pos >= off)
8631 if (ptr)
8632 p = ptr + pos - off;
8633 l = MIN ((HOST_WIDE_INT) off + len - pos,
8634 fieldsize);
8636 else
8638 p = ptr;
8639 no = off - pos;
8640 l = len;
8642 if (!native_encode_initializer (val, p, l, no, NULL))
8643 return 0;
8646 return MIN (total_bytes - off, len);
8648 return 0;
8653 /* Subroutine of native_interpret_expr. Interpret the contents of
8654 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8655 If the buffer cannot be interpreted, return NULL_TREE. */
8657 static tree
8658 native_interpret_int (tree type, const unsigned char *ptr, int len)
8660 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
8662 if (total_bytes > len
8663 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8664 return NULL_TREE;
8666 wide_int result = wi::from_buffer (ptr, total_bytes);
8668 return wide_int_to_tree (type, result);
8672 /* Subroutine of native_interpret_expr. Interpret the contents of
8673 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
8674 If the buffer cannot be interpreted, return NULL_TREE. */
8676 static tree
8677 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
8679 scalar_mode mode = SCALAR_TYPE_MODE (type);
8680 int total_bytes = GET_MODE_SIZE (mode);
8681 double_int result;
8682 FIXED_VALUE_TYPE fixed_value;
8684 if (total_bytes > len
8685 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
8686 return NULL_TREE;
8688 result = double_int::from_buffer (ptr, total_bytes);
8689 fixed_value = fixed_from_double_int (result, mode);
8691 return build_fixed (type, fixed_value);
8695 /* Subroutine of native_interpret_expr. Interpret the contents of
8696 the buffer PTR of length LEN as a REAL_CST of type TYPE.
8697 If the buffer cannot be interpreted, return NULL_TREE. */
8699 tree
8700 native_interpret_real (tree type, const unsigned char *ptr, int len)
8702 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8703 int total_bytes = GET_MODE_SIZE (mode);
8704 unsigned char value;
8705 /* There are always 32 bits in each long, no matter the size of
8706 the hosts long. We handle floating point representations with
8707 up to 192 bits. */
8708 REAL_VALUE_TYPE r;
8709 long tmp[6];
8711 if (total_bytes > len || total_bytes > 24)
8712 return NULL_TREE;
8713 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8715 memset (tmp, 0, sizeof (tmp));
8716 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8717 bitpos += BITS_PER_UNIT)
8719 /* Both OFFSET and BYTE index within a long;
8720 bitpos indexes the whole float. */
8721 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
8722 if (UNITS_PER_WORD < 4)
8724 int word = byte / UNITS_PER_WORD;
8725 if (WORDS_BIG_ENDIAN)
8726 word = (words - 1) - word;
8727 offset = word * UNITS_PER_WORD;
8728 if (BYTES_BIG_ENDIAN)
8729 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8730 else
8731 offset += byte % UNITS_PER_WORD;
8733 else
8735 offset = byte;
8736 if (BYTES_BIG_ENDIAN)
8738 /* Reverse bytes within each long, or within the entire float
8739 if it's smaller than a long (for HFmode). */
8740 offset = MIN (3, total_bytes - 1) - offset;
8741 gcc_assert (offset >= 0);
8744 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8746 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8749 real_from_target (&r, tmp, mode);
8750 return build_real (type, r);
8754 /* Subroutine of native_interpret_expr. Interpret the contents of
8755 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8756 If the buffer cannot be interpreted, return NULL_TREE. */
8758 static tree
8759 native_interpret_complex (tree type, const unsigned char *ptr, int len)
8761 tree etype, rpart, ipart;
8762 int size;
8764 etype = TREE_TYPE (type);
8765 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8766 if (size * 2 > len)
8767 return NULL_TREE;
8768 rpart = native_interpret_expr (etype, ptr, size);
8769 if (!rpart)
8770 return NULL_TREE;
8771 ipart = native_interpret_expr (etype, ptr+size, size);
8772 if (!ipart)
8773 return NULL_TREE;
8774 return build_complex (type, rpart, ipart);
8777 /* Read a vector of type TYPE from the target memory image given by BYTES,
8778 which contains LEN bytes. The vector is known to be encodable using
8779 NPATTERNS interleaved patterns with NELTS_PER_PATTERN elements each.
8781 Return the vector on success, otherwise return null. */
8783 static tree
8784 native_interpret_vector_part (tree type, const unsigned char *bytes,
8785 unsigned int len, unsigned int npatterns,
8786 unsigned int nelts_per_pattern)
8788 tree elt_type = TREE_TYPE (type);
8789 if (VECTOR_BOOLEAN_TYPE_P (type)
8790 && TYPE_PRECISION (elt_type) <= BITS_PER_UNIT)
8792 /* This is the only case in which elements can be smaller than a byte.
8793 Element 0 is always in the lsb of the containing byte. */
8794 unsigned int elt_bits = TYPE_PRECISION (elt_type);
8795 if (elt_bits * npatterns * nelts_per_pattern > len * BITS_PER_UNIT)
8796 return NULL_TREE;
8798 tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8799 for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8801 unsigned int bit_index = i * elt_bits;
8802 unsigned int byte_index = bit_index / BITS_PER_UNIT;
8803 unsigned int lsb = bit_index % BITS_PER_UNIT;
8804 builder.quick_push (bytes[byte_index] & (1 << lsb)
8805 ? build_all_ones_cst (elt_type)
8806 : build_zero_cst (elt_type));
8808 return builder.build ();
8811 unsigned int elt_bytes = tree_to_uhwi (TYPE_SIZE_UNIT (elt_type));
8812 if (elt_bytes * npatterns * nelts_per_pattern > len)
8813 return NULL_TREE;
8815 tree_vector_builder builder (type, npatterns, nelts_per_pattern);
8816 for (unsigned int i = 0; i < builder.encoded_nelts (); ++i)
8818 tree elt = native_interpret_expr (elt_type, bytes, elt_bytes);
8819 if (!elt)
8820 return NULL_TREE;
8821 builder.quick_push (elt);
8822 bytes += elt_bytes;
8824 return builder.build ();
8827 /* Subroutine of native_interpret_expr. Interpret the contents of
8828 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8829 If the buffer cannot be interpreted, return NULL_TREE. */
8831 static tree
8832 native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
8834 tree etype;
8835 unsigned int size;
8836 unsigned HOST_WIDE_INT count;
8838 etype = TREE_TYPE (type);
8839 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
8840 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&count)
8841 || size * count > len)
8842 return NULL_TREE;
8844 return native_interpret_vector_part (type, ptr, len, count, 1);
8848 /* Subroutine of fold_view_convert_expr. Interpret the contents of
8849 the buffer PTR of length LEN as a constant of type TYPE. For
8850 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8851 we return a REAL_CST, etc... If the buffer cannot be interpreted,
8852 return NULL_TREE. */
8854 tree
8855 native_interpret_expr (tree type, const unsigned char *ptr, int len)
8857 switch (TREE_CODE (type))
8859 case INTEGER_TYPE:
8860 case ENUMERAL_TYPE:
8861 case BOOLEAN_TYPE:
8862 case POINTER_TYPE:
8863 case REFERENCE_TYPE:
8864 case OFFSET_TYPE:
8865 return native_interpret_int (type, ptr, len);
8867 case REAL_TYPE:
8868 if (tree ret = native_interpret_real (type, ptr, len))
8870 /* For floating point values in composite modes, punt if this
8871 folding doesn't preserve bit representation. As the mode doesn't
8872 have fixed precision while GCC pretends it does, there could be
8873 valid values that GCC can't really represent accurately.
8874 See PR95450. Even for other modes, e.g. x86 XFmode can have some
8875 bit combinationations which GCC doesn't preserve. */
8876 unsigned char buf[24];
8877 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
8878 int total_bytes = GET_MODE_SIZE (mode);
8879 if (native_encode_expr (ret, buf, total_bytes, 0) != total_bytes
8880 || memcmp (ptr, buf, total_bytes) != 0)
8881 return NULL_TREE;
8882 return ret;
8884 return NULL_TREE;
8886 case FIXED_POINT_TYPE:
8887 return native_interpret_fixed (type, ptr, len);
8889 case COMPLEX_TYPE:
8890 return native_interpret_complex (type, ptr, len);
8892 case VECTOR_TYPE:
8893 return native_interpret_vector (type, ptr, len);
8895 default:
8896 return NULL_TREE;
8900 /* Returns true if we can interpret the contents of a native encoding
8901 as TYPE. */
8903 bool
8904 can_native_interpret_type_p (tree type)
8906 switch (TREE_CODE (type))
8908 case INTEGER_TYPE:
8909 case ENUMERAL_TYPE:
8910 case BOOLEAN_TYPE:
8911 case POINTER_TYPE:
8912 case REFERENCE_TYPE:
8913 case FIXED_POINT_TYPE:
8914 case REAL_TYPE:
8915 case COMPLEX_TYPE:
8916 case VECTOR_TYPE:
8917 case OFFSET_TYPE:
8918 return true;
8919 default:
8920 return false;
8924 /* Attempt to interpret aggregate of TYPE from bytes encoded in target
8925 byte order at PTR + OFF with LEN bytes. Does not handle unions. */
8927 tree
8928 native_interpret_aggregate (tree type, const unsigned char *ptr, int off,
8929 int len)
8931 vec<constructor_elt, va_gc> *elts = NULL;
8932 if (TREE_CODE (type) == ARRAY_TYPE)
8934 HOST_WIDE_INT eltsz = int_size_in_bytes (TREE_TYPE (type));
8935 if (eltsz < 0 || eltsz > len || TYPE_DOMAIN (type) == NULL_TREE)
8936 return NULL_TREE;
8938 HOST_WIDE_INT cnt = 0;
8939 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
8941 if (!tree_fits_shwi_p (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
8942 return NULL_TREE;
8943 cnt = tree_to_shwi (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) + 1;
8945 if (eltsz == 0)
8946 cnt = 0;
8947 HOST_WIDE_INT pos = 0;
8948 for (HOST_WIDE_INT i = 0; i < cnt; i++, pos += eltsz)
8950 tree v = NULL_TREE;
8951 if (pos >= len || pos + eltsz > len)
8952 return NULL_TREE;
8953 if (can_native_interpret_type_p (TREE_TYPE (type)))
8955 v = native_interpret_expr (TREE_TYPE (type),
8956 ptr + off + pos, eltsz);
8957 if (v == NULL_TREE)
8958 return NULL_TREE;
8960 else if (TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
8961 || TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
8962 v = native_interpret_aggregate (TREE_TYPE (type), ptr, off + pos,
8963 eltsz);
8964 if (v == NULL_TREE)
8965 return NULL_TREE;
8966 CONSTRUCTOR_APPEND_ELT (elts, size_int (i), v);
8968 return build_constructor (type, elts);
8970 if (TREE_CODE (type) != RECORD_TYPE)
8971 return NULL_TREE;
8972 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
8974 if (TREE_CODE (field) != FIELD_DECL || DECL_PADDING_P (field))
8975 continue;
8976 tree fld = field;
8977 HOST_WIDE_INT bitoff = 0, pos = 0, sz = 0;
8978 int diff = 0;
8979 tree v = NULL_TREE;
8980 if (DECL_BIT_FIELD (field))
8982 fld = DECL_BIT_FIELD_REPRESENTATIVE (field);
8983 if (fld && INTEGRAL_TYPE_P (TREE_TYPE (fld)))
8985 poly_int64 bitoffset;
8986 poly_uint64 field_offset, fld_offset;
8987 if (poly_int_tree_p (DECL_FIELD_OFFSET (field), &field_offset)
8988 && poly_int_tree_p (DECL_FIELD_OFFSET (fld), &fld_offset))
8989 bitoffset = (field_offset - fld_offset) * BITS_PER_UNIT;
8990 else
8991 bitoffset = 0;
8992 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field))
8993 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)));
8994 diff = (TYPE_PRECISION (TREE_TYPE (fld))
8995 - TYPE_PRECISION (TREE_TYPE (field)));
8996 if (!bitoffset.is_constant (&bitoff)
8997 || bitoff < 0
8998 || bitoff > diff)
8999 return NULL_TREE;
9001 else
9003 if (!tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (field)))
9004 return NULL_TREE;
9005 int fieldsize = TYPE_PRECISION (TREE_TYPE (field));
9006 int bpos = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (field));
9007 bpos %= BITS_PER_UNIT;
9008 fieldsize += bpos;
9009 fieldsize += BITS_PER_UNIT - 1;
9010 fieldsize /= BITS_PER_UNIT;
9011 tree repr_type = find_bitfield_repr_type (fieldsize, len);
9012 if (repr_type == NULL_TREE)
9013 return NULL_TREE;
9014 sz = int_size_in_bytes (repr_type);
9015 if (sz < 0 || sz > len)
9016 return NULL_TREE;
9017 pos = int_byte_position (field);
9018 if (pos < 0 || pos > len || pos + fieldsize > len)
9019 return NULL_TREE;
9020 HOST_WIDE_INT rpos;
9021 if (pos + sz <= len)
9022 rpos = pos;
9023 else
9025 rpos = len - sz;
9026 gcc_assert (rpos <= pos);
9028 bitoff = (HOST_WIDE_INT) (pos - rpos) * BITS_PER_UNIT + bpos;
9029 pos = rpos;
9030 diff = (TYPE_PRECISION (repr_type)
9031 - TYPE_PRECISION (TREE_TYPE (field)));
9032 v = native_interpret_expr (repr_type, ptr + off + pos, sz);
9033 if (v == NULL_TREE)
9034 return NULL_TREE;
9035 fld = NULL_TREE;
9039 if (fld)
9041 sz = int_size_in_bytes (TREE_TYPE (fld));
9042 if (sz < 0 || sz > len)
9043 return NULL_TREE;
9044 tree byte_pos = byte_position (fld);
9045 if (!tree_fits_shwi_p (byte_pos))
9046 return NULL_TREE;
9047 pos = tree_to_shwi (byte_pos);
9048 if (pos < 0 || pos > len || pos + sz > len)
9049 return NULL_TREE;
9051 if (fld == NULL_TREE)
9052 /* Already handled above. */;
9053 else if (can_native_interpret_type_p (TREE_TYPE (fld)))
9055 v = native_interpret_expr (TREE_TYPE (fld),
9056 ptr + off + pos, sz);
9057 if (v == NULL_TREE)
9058 return NULL_TREE;
9060 else if (TREE_CODE (TREE_TYPE (fld)) == RECORD_TYPE
9061 || TREE_CODE (TREE_TYPE (fld)) == ARRAY_TYPE)
9062 v = native_interpret_aggregate (TREE_TYPE (fld), ptr, off + pos, sz);
9063 if (v == NULL_TREE)
9064 return NULL_TREE;
9065 if (fld != field)
9067 if (TREE_CODE (v) != INTEGER_CST)
9068 return NULL_TREE;
9070 /* FIXME: Figure out how to handle PDP endian bitfields. */
9071 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9072 return NULL_TREE;
9073 if (!BYTES_BIG_ENDIAN)
9074 v = wide_int_to_tree (TREE_TYPE (field),
9075 wi::lrshift (wi::to_wide (v), bitoff));
9076 else
9077 v = wide_int_to_tree (TREE_TYPE (field),
9078 wi::lrshift (wi::to_wide (v),
9079 diff - bitoff));
9081 CONSTRUCTOR_APPEND_ELT (elts, field, v);
9083 return build_constructor (type, elts);
9086 /* Routines for manipulation of native_encode_expr encoded data if the encoded
9087 or extracted constant positions and/or sizes aren't byte aligned. */
9089 /* Shift left the bytes in PTR of SZ elements by AMNT bits, carrying over the
9090 bits between adjacent elements. AMNT should be within
9091 [0, BITS_PER_UNIT).
9092 Example, AMNT = 2:
9093 00011111|11100000 << 2 = 01111111|10000000
9094 PTR[1] | PTR[0] PTR[1] | PTR[0]. */
9096 void
9097 shift_bytes_in_array_left (unsigned char *ptr, unsigned int sz,
9098 unsigned int amnt)
9100 if (amnt == 0)
9101 return;
9103 unsigned char carry_over = 0U;
9104 unsigned char carry_mask = (~0U) << (unsigned char) (BITS_PER_UNIT - amnt);
9105 unsigned char clear_mask = (~0U) << amnt;
9107 for (unsigned int i = 0; i < sz; i++)
9109 unsigned prev_carry_over = carry_over;
9110 carry_over = (ptr[i] & carry_mask) >> (BITS_PER_UNIT - amnt);
9112 ptr[i] <<= amnt;
9113 if (i != 0)
9115 ptr[i] &= clear_mask;
9116 ptr[i] |= prev_carry_over;
9121 /* Like shift_bytes_in_array_left but for big-endian.
9122 Shift right the bytes in PTR of SZ elements by AMNT bits, carrying over the
9123 bits between adjacent elements. AMNT should be within
9124 [0, BITS_PER_UNIT).
9125 Example, AMNT = 2:
9126 00011111|11100000 >> 2 = 00000111|11111000
9127 PTR[0] | PTR[1] PTR[0] | PTR[1]. */
9129 void
9130 shift_bytes_in_array_right (unsigned char *ptr, unsigned int sz,
9131 unsigned int amnt)
9133 if (amnt == 0)
9134 return;
9136 unsigned char carry_over = 0U;
9137 unsigned char carry_mask = ~(~0U << amnt);
9139 for (unsigned int i = 0; i < sz; i++)
9141 unsigned prev_carry_over = carry_over;
9142 carry_over = ptr[i] & carry_mask;
9144 carry_over <<= (unsigned char) BITS_PER_UNIT - amnt;
9145 ptr[i] >>= amnt;
9146 ptr[i] |= prev_carry_over;
9150 /* Try to view-convert VECTOR_CST EXPR to VECTOR_TYPE TYPE by operating
9151 directly on the VECTOR_CST encoding, in a way that works for variable-
9152 length vectors. Return the resulting VECTOR_CST on success or null
9153 on failure. */
9155 static tree
9156 fold_view_convert_vector_encoding (tree type, tree expr)
9158 tree expr_type = TREE_TYPE (expr);
9159 poly_uint64 type_bits, expr_bits;
9160 if (!poly_int_tree_p (TYPE_SIZE (type), &type_bits)
9161 || !poly_int_tree_p (TYPE_SIZE (expr_type), &expr_bits))
9162 return NULL_TREE;
9164 poly_uint64 type_units = TYPE_VECTOR_SUBPARTS (type);
9165 poly_uint64 expr_units = TYPE_VECTOR_SUBPARTS (expr_type);
9166 unsigned int type_elt_bits = vector_element_size (type_bits, type_units);
9167 unsigned int expr_elt_bits = vector_element_size (expr_bits, expr_units);
9169 /* We can only preserve the semantics of a stepped pattern if the new
9170 vector element is an integer of the same size. */
9171 if (VECTOR_CST_STEPPED_P (expr)
9172 && (!INTEGRAL_TYPE_P (type) || type_elt_bits != expr_elt_bits))
9173 return NULL_TREE;
9175 /* The number of bits needed to encode one element from every pattern
9176 of the original vector. */
9177 unsigned int expr_sequence_bits
9178 = VECTOR_CST_NPATTERNS (expr) * expr_elt_bits;
9180 /* The number of bits needed to encode one element from every pattern
9181 of the result. */
9182 unsigned int type_sequence_bits
9183 = least_common_multiple (expr_sequence_bits, type_elt_bits);
9185 /* Don't try to read more bytes than are available, which can happen
9186 for constant-sized vectors if TYPE has larger elements than EXPR_TYPE.
9187 The general VIEW_CONVERT handling can cope with that case, so there's
9188 no point complicating things here. */
9189 unsigned int nelts_per_pattern = VECTOR_CST_NELTS_PER_PATTERN (expr);
9190 unsigned int buffer_bytes = CEIL (nelts_per_pattern * type_sequence_bits,
9191 BITS_PER_UNIT);
9192 unsigned int buffer_bits = buffer_bytes * BITS_PER_UNIT;
9193 if (known_gt (buffer_bits, expr_bits))
9194 return NULL_TREE;
9196 /* Get enough bytes of EXPR to form the new encoding. */
9197 auto_vec<unsigned char, 128> buffer (buffer_bytes);
9198 buffer.quick_grow (buffer_bytes);
9199 if (native_encode_vector_part (expr, buffer.address (), buffer_bytes, 0,
9200 buffer_bits / expr_elt_bits)
9201 != (int) buffer_bytes)
9202 return NULL_TREE;
9204 /* Reencode the bytes as TYPE. */
9205 unsigned int type_npatterns = type_sequence_bits / type_elt_bits;
9206 return native_interpret_vector_part (type, &buffer[0], buffer.length (),
9207 type_npatterns, nelts_per_pattern);
9210 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
9211 TYPE at compile-time. If we're unable to perform the conversion
9212 return NULL_TREE. */
9214 static tree
9215 fold_view_convert_expr (tree type, tree expr)
9217 /* We support up to 512-bit values (for V8DFmode). */
9218 unsigned char buffer[64];
9219 int len;
9221 /* Check that the host and target are sane. */
9222 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
9223 return NULL_TREE;
9225 if (VECTOR_TYPE_P (type) && TREE_CODE (expr) == VECTOR_CST)
9226 if (tree res = fold_view_convert_vector_encoding (type, expr))
9227 return res;
9229 len = native_encode_expr (expr, buffer, sizeof (buffer));
9230 if (len == 0)
9231 return NULL_TREE;
9233 return native_interpret_expr (type, buffer, len);
9236 /* Build an expression for the address of T. Folds away INDIRECT_REF
9237 to avoid confusing the gimplify process. */
9239 tree
9240 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
9242 /* The size of the object is not relevant when talking about its address. */
9243 if (TREE_CODE (t) == WITH_SIZE_EXPR)
9244 t = TREE_OPERAND (t, 0);
9246 if (TREE_CODE (t) == INDIRECT_REF)
9248 t = TREE_OPERAND (t, 0);
9250 if (TREE_TYPE (t) != ptrtype)
9251 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
9253 else if (TREE_CODE (t) == MEM_REF
9254 && integer_zerop (TREE_OPERAND (t, 1)))
9256 t = TREE_OPERAND (t, 0);
9258 if (TREE_TYPE (t) != ptrtype)
9259 t = fold_convert_loc (loc, ptrtype, t);
9261 else if (TREE_CODE (t) == MEM_REF
9262 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
9263 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
9264 TREE_OPERAND (t, 0),
9265 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
9266 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
9268 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
9270 if (TREE_TYPE (t) != ptrtype)
9271 t = fold_convert_loc (loc, ptrtype, t);
9273 else
9274 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
9276 return t;
9279 /* Build an expression for the address of T. */
9281 tree
9282 build_fold_addr_expr_loc (location_t loc, tree t)
9284 tree ptrtype = build_pointer_type (TREE_TYPE (t));
9286 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
9289 /* Fold a unary expression of code CODE and type TYPE with operand
9290 OP0. Return the folded expression if folding is successful.
9291 Otherwise, return NULL_TREE. */
9293 tree
9294 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
9296 tree tem;
9297 tree arg0;
9298 enum tree_code_class kind = TREE_CODE_CLASS (code);
9300 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9301 && TREE_CODE_LENGTH (code) == 1);
9303 arg0 = op0;
9304 if (arg0)
9306 if (CONVERT_EXPR_CODE_P (code)
9307 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
9309 /* Don't use STRIP_NOPS, because signedness of argument type
9310 matters. */
9311 STRIP_SIGN_NOPS (arg0);
9313 else
9315 /* Strip any conversions that don't change the mode. This
9316 is safe for every expression, except for a comparison
9317 expression because its signedness is derived from its
9318 operands.
9320 Note that this is done as an internal manipulation within
9321 the constant folder, in order to find the simplest
9322 representation of the arguments so that their form can be
9323 studied. In any cases, the appropriate type conversions
9324 should be put back in the tree that will get out of the
9325 constant folder. */
9326 STRIP_NOPS (arg0);
9329 if (CONSTANT_CLASS_P (arg0))
9331 tree tem = const_unop (code, type, arg0);
9332 if (tem)
9334 if (TREE_TYPE (tem) != type)
9335 tem = fold_convert_loc (loc, type, tem);
9336 return tem;
9341 tem = generic_simplify (loc, code, type, op0);
9342 if (tem)
9343 return tem;
9345 if (TREE_CODE_CLASS (code) == tcc_unary)
9347 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9348 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9349 fold_build1_loc (loc, code, type,
9350 fold_convert_loc (loc, TREE_TYPE (op0),
9351 TREE_OPERAND (arg0, 1))));
9352 else if (TREE_CODE (arg0) == COND_EXPR)
9354 tree arg01 = TREE_OPERAND (arg0, 1);
9355 tree arg02 = TREE_OPERAND (arg0, 2);
9356 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
9357 arg01 = fold_build1_loc (loc, code, type,
9358 fold_convert_loc (loc,
9359 TREE_TYPE (op0), arg01));
9360 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
9361 arg02 = fold_build1_loc (loc, code, type,
9362 fold_convert_loc (loc,
9363 TREE_TYPE (op0), arg02));
9364 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
9365 arg01, arg02);
9367 /* If this was a conversion, and all we did was to move into
9368 inside the COND_EXPR, bring it back out. But leave it if
9369 it is a conversion from integer to integer and the
9370 result precision is no wider than a word since such a
9371 conversion is cheap and may be optimized away by combine,
9372 while it couldn't if it were outside the COND_EXPR. Then return
9373 so we don't get into an infinite recursion loop taking the
9374 conversion out and then back in. */
9376 if ((CONVERT_EXPR_CODE_P (code)
9377 || code == NON_LVALUE_EXPR)
9378 && TREE_CODE (tem) == COND_EXPR
9379 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
9380 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
9381 && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 1)))
9382 && ! VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (tem, 2)))
9383 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
9384 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
9385 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9386 && (INTEGRAL_TYPE_P
9387 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
9388 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
9389 || flag_syntax_only))
9390 tem = build1_loc (loc, code, type,
9391 build3 (COND_EXPR,
9392 TREE_TYPE (TREE_OPERAND
9393 (TREE_OPERAND (tem, 1), 0)),
9394 TREE_OPERAND (tem, 0),
9395 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
9396 TREE_OPERAND (TREE_OPERAND (tem, 2),
9397 0)));
9398 return tem;
9402 switch (code)
9404 case NON_LVALUE_EXPR:
9405 if (!maybe_lvalue_p (op0))
9406 return fold_convert_loc (loc, type, op0);
9407 return NULL_TREE;
9409 CASE_CONVERT:
9410 case FLOAT_EXPR:
9411 case FIX_TRUNC_EXPR:
9412 if (COMPARISON_CLASS_P (op0))
9414 /* If we have (type) (a CMP b) and type is an integral type, return
9415 new expression involving the new type. Canonicalize
9416 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
9417 non-integral type.
9418 Do not fold the result as that would not simplify further, also
9419 folding again results in recursions. */
9420 if (TREE_CODE (type) == BOOLEAN_TYPE)
9421 return build2_loc (loc, TREE_CODE (op0), type,
9422 TREE_OPERAND (op0, 0),
9423 TREE_OPERAND (op0, 1));
9424 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
9425 && TREE_CODE (type) != VECTOR_TYPE)
9426 return build3_loc (loc, COND_EXPR, type, op0,
9427 constant_boolean_node (true, type),
9428 constant_boolean_node (false, type));
9431 /* Handle (T *)&A.B.C for A being of type T and B and C
9432 living at offset zero. This occurs frequently in
9433 C++ upcasting and then accessing the base. */
9434 if (TREE_CODE (op0) == ADDR_EXPR
9435 && POINTER_TYPE_P (type)
9436 && handled_component_p (TREE_OPERAND (op0, 0)))
9438 poly_int64 bitsize, bitpos;
9439 tree offset;
9440 machine_mode mode;
9441 int unsignedp, reversep, volatilep;
9442 tree base
9443 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
9444 &offset, &mode, &unsignedp, &reversep,
9445 &volatilep);
9446 /* If the reference was to a (constant) zero offset, we can use
9447 the address of the base if it has the same base type
9448 as the result type and the pointer type is unqualified. */
9449 if (!offset
9450 && known_eq (bitpos, 0)
9451 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
9452 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
9453 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
9454 return fold_convert_loc (loc, type,
9455 build_fold_addr_expr_loc (loc, base));
9458 if (TREE_CODE (op0) == MODIFY_EXPR
9459 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
9460 /* Detect assigning a bitfield. */
9461 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
9462 && DECL_BIT_FIELD
9463 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
9465 /* Don't leave an assignment inside a conversion
9466 unless assigning a bitfield. */
9467 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
9468 /* First do the assignment, then return converted constant. */
9469 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
9470 suppress_warning (tem /* What warning? */);
9471 TREE_USED (tem) = 1;
9472 return tem;
9475 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
9476 constants (if x has signed type, the sign bit cannot be set
9477 in c). This folds extension into the BIT_AND_EXPR.
9478 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
9479 very likely don't have maximal range for their precision and this
9480 transformation effectively doesn't preserve non-maximal ranges. */
9481 if (TREE_CODE (type) == INTEGER_TYPE
9482 && TREE_CODE (op0) == BIT_AND_EXPR
9483 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9485 tree and_expr = op0;
9486 tree and0 = TREE_OPERAND (and_expr, 0);
9487 tree and1 = TREE_OPERAND (and_expr, 1);
9488 int change = 0;
9490 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
9491 || (TYPE_PRECISION (type)
9492 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
9493 change = 1;
9494 else if (TYPE_PRECISION (TREE_TYPE (and1))
9495 <= HOST_BITS_PER_WIDE_INT
9496 && tree_fits_uhwi_p (and1))
9498 unsigned HOST_WIDE_INT cst;
9500 cst = tree_to_uhwi (and1);
9501 cst &= HOST_WIDE_INT_M1U
9502 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
9503 change = (cst == 0);
9504 if (change
9505 && !flag_syntax_only
9506 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
9507 == ZERO_EXTEND))
9509 tree uns = unsigned_type_for (TREE_TYPE (and0));
9510 and0 = fold_convert_loc (loc, uns, and0);
9511 and1 = fold_convert_loc (loc, uns, and1);
9514 if (change)
9516 tem = force_fit_type (type, wi::to_widest (and1), 0,
9517 TREE_OVERFLOW (and1));
9518 return fold_build2_loc (loc, BIT_AND_EXPR, type,
9519 fold_convert_loc (loc, type, and0), tem);
9523 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
9524 cast (T1)X will fold away. We assume that this happens when X itself
9525 is a cast. */
9526 if (POINTER_TYPE_P (type)
9527 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
9528 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
9530 tree arg00 = TREE_OPERAND (arg0, 0);
9531 tree arg01 = TREE_OPERAND (arg0, 1);
9533 /* If -fsanitize=alignment, avoid this optimization in GENERIC
9534 when the pointed type needs higher alignment than
9535 the p+ first operand's pointed type. */
9536 if (!in_gimple_form
9537 && sanitize_flags_p (SANITIZE_ALIGNMENT)
9538 && (min_align_of_type (TREE_TYPE (type))
9539 > min_align_of_type (TREE_TYPE (TREE_TYPE (arg00)))))
9540 return NULL_TREE;
9542 /* Similarly, avoid this optimization in GENERIC for -fsanitize=null
9543 when type is a reference type and arg00's type is not,
9544 because arg00 could be validly nullptr and if arg01 doesn't return,
9545 we don't want false positive binding of reference to nullptr. */
9546 if (TREE_CODE (type) == REFERENCE_TYPE
9547 && !in_gimple_form
9548 && sanitize_flags_p (SANITIZE_NULL)
9549 && TREE_CODE (TREE_TYPE (arg00)) != REFERENCE_TYPE)
9550 return NULL_TREE;
9552 arg00 = fold_convert_loc (loc, type, arg00);
9553 return fold_build_pointer_plus_loc (loc, arg00, arg01);
9556 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
9557 of the same precision, and X is an integer type not narrower than
9558 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
9559 if (INTEGRAL_TYPE_P (type)
9560 && TREE_CODE (op0) == BIT_NOT_EXPR
9561 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9562 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
9563 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
9565 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
9566 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
9567 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
9568 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
9569 fold_convert_loc (loc, type, tem));
9572 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
9573 type of X and Y (integer types only). */
9574 if (INTEGRAL_TYPE_P (type)
9575 && TREE_CODE (op0) == MULT_EXPR
9576 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
9577 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
9579 /* Be careful not to introduce new overflows. */
9580 tree mult_type;
9581 if (TYPE_OVERFLOW_WRAPS (type))
9582 mult_type = type;
9583 else
9584 mult_type = unsigned_type_for (type);
9586 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
9588 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
9589 fold_convert_loc (loc, mult_type,
9590 TREE_OPERAND (op0, 0)),
9591 fold_convert_loc (loc, mult_type,
9592 TREE_OPERAND (op0, 1)));
9593 return fold_convert_loc (loc, type, tem);
9597 return NULL_TREE;
9599 case VIEW_CONVERT_EXPR:
9600 if (TREE_CODE (op0) == MEM_REF)
9602 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
9603 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
9604 tem = fold_build2_loc (loc, MEM_REF, type,
9605 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
9606 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
9607 return tem;
9610 return NULL_TREE;
9612 case NEGATE_EXPR:
9613 tem = fold_negate_expr (loc, arg0);
9614 if (tem)
9615 return fold_convert_loc (loc, type, tem);
9616 return NULL_TREE;
9618 case ABS_EXPR:
9619 /* Convert fabs((double)float) into (double)fabsf(float). */
9620 if (TREE_CODE (arg0) == NOP_EXPR
9621 && TREE_CODE (type) == REAL_TYPE)
9623 tree targ0 = strip_float_extensions (arg0);
9624 if (targ0 != arg0)
9625 return fold_convert_loc (loc, type,
9626 fold_build1_loc (loc, ABS_EXPR,
9627 TREE_TYPE (targ0),
9628 targ0));
9630 return NULL_TREE;
9632 case BIT_NOT_EXPR:
9633 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
9634 if (TREE_CODE (arg0) == BIT_XOR_EXPR
9635 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9636 fold_convert_loc (loc, type,
9637 TREE_OPERAND (arg0, 0)))))
9638 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
9639 fold_convert_loc (loc, type,
9640 TREE_OPERAND (arg0, 1)));
9641 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
9642 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
9643 fold_convert_loc (loc, type,
9644 TREE_OPERAND (arg0, 1)))))
9645 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
9646 fold_convert_loc (loc, type,
9647 TREE_OPERAND (arg0, 0)), tem);
9649 return NULL_TREE;
9651 case TRUTH_NOT_EXPR:
9652 /* Note that the operand of this must be an int
9653 and its values must be 0 or 1.
9654 ("true" is a fixed value perhaps depending on the language,
9655 but we don't handle values other than 1 correctly yet.) */
9656 tem = fold_truth_not_expr (loc, arg0);
9657 if (!tem)
9658 return NULL_TREE;
9659 return fold_convert_loc (loc, type, tem);
9661 case INDIRECT_REF:
9662 /* Fold *&X to X if X is an lvalue. */
9663 if (TREE_CODE (op0) == ADDR_EXPR)
9665 tree op00 = TREE_OPERAND (op0, 0);
9666 if ((VAR_P (op00)
9667 || TREE_CODE (op00) == PARM_DECL
9668 || TREE_CODE (op00) == RESULT_DECL)
9669 && !TREE_READONLY (op00))
9670 return op00;
9672 return NULL_TREE;
9674 default:
9675 return NULL_TREE;
9676 } /* switch (code) */
9680 /* If the operation was a conversion do _not_ mark a resulting constant
9681 with TREE_OVERFLOW if the original constant was not. These conversions
9682 have implementation defined behavior and retaining the TREE_OVERFLOW
9683 flag here would confuse later passes such as VRP. */
9684 tree
9685 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
9686 tree type, tree op0)
9688 tree res = fold_unary_loc (loc, code, type, op0);
9689 if (res
9690 && TREE_CODE (res) == INTEGER_CST
9691 && TREE_CODE (op0) == INTEGER_CST
9692 && CONVERT_EXPR_CODE_P (code))
9693 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
9695 return res;
9698 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
9699 operands OP0 and OP1. LOC is the location of the resulting expression.
9700 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
9701 Return the folded expression if folding is successful. Otherwise,
9702 return NULL_TREE. */
9703 static tree
9704 fold_truth_andor (location_t loc, enum tree_code code, tree type,
9705 tree arg0, tree arg1, tree op0, tree op1)
9707 tree tem;
9709 /* We only do these simplifications if we are optimizing. */
9710 if (!optimize)
9711 return NULL_TREE;
9713 /* Check for things like (A || B) && (A || C). We can convert this
9714 to A || (B && C). Note that either operator can be any of the four
9715 truth and/or operations and the transformation will still be
9716 valid. Also note that we only care about order for the
9717 ANDIF and ORIF operators. If B contains side effects, this
9718 might change the truth-value of A. */
9719 if (TREE_CODE (arg0) == TREE_CODE (arg1)
9720 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
9721 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
9722 || TREE_CODE (arg0) == TRUTH_AND_EXPR
9723 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
9724 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
9726 tree a00 = TREE_OPERAND (arg0, 0);
9727 tree a01 = TREE_OPERAND (arg0, 1);
9728 tree a10 = TREE_OPERAND (arg1, 0);
9729 tree a11 = TREE_OPERAND (arg1, 1);
9730 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
9731 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
9732 && (code == TRUTH_AND_EXPR
9733 || code == TRUTH_OR_EXPR));
9735 if (operand_equal_p (a00, a10, 0))
9736 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9737 fold_build2_loc (loc, code, type, a01, a11));
9738 else if (commutative && operand_equal_p (a00, a11, 0))
9739 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
9740 fold_build2_loc (loc, code, type, a01, a10));
9741 else if (commutative && operand_equal_p (a01, a10, 0))
9742 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
9743 fold_build2_loc (loc, code, type, a00, a11));
9745 /* This case if tricky because we must either have commutative
9746 operators or else A10 must not have side-effects. */
9748 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
9749 && operand_equal_p (a01, a11, 0))
9750 return fold_build2_loc (loc, TREE_CODE (arg0), type,
9751 fold_build2_loc (loc, code, type, a00, a10),
9752 a01);
9755 /* See if we can build a range comparison. */
9756 if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
9757 return tem;
9759 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
9760 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
9762 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
9763 if (tem)
9764 return fold_build2_loc (loc, code, type, tem, arg1);
9767 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
9768 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
9770 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
9771 if (tem)
9772 return fold_build2_loc (loc, code, type, arg0, tem);
9775 /* Check for the possibility of merging component references. If our
9776 lhs is another similar operation, try to merge its rhs with our
9777 rhs. Then try to merge our lhs and rhs. */
9778 if (TREE_CODE (arg0) == code
9779 && (tem = fold_truth_andor_1 (loc, code, type,
9780 TREE_OPERAND (arg0, 1), arg1)) != 0)
9781 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9783 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
9784 return tem;
9786 bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
9787 if (param_logical_op_non_short_circuit != -1)
9788 logical_op_non_short_circuit
9789 = param_logical_op_non_short_circuit;
9790 if (logical_op_non_short_circuit
9791 && !sanitize_coverage_p ()
9792 && (code == TRUTH_AND_EXPR
9793 || code == TRUTH_ANDIF_EXPR
9794 || code == TRUTH_OR_EXPR
9795 || code == TRUTH_ORIF_EXPR))
9797 enum tree_code ncode, icode;
9799 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
9800 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
9801 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
9803 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
9804 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
9805 We don't want to pack more than two leafs to a non-IF AND/OR
9806 expression.
9807 If tree-code of left-hand operand isn't an AND/OR-IF code and not
9808 equal to IF-CODE, then we don't want to add right-hand operand.
9809 If the inner right-hand side of left-hand operand has
9810 side-effects, or isn't simple, then we can't add to it,
9811 as otherwise we might destroy if-sequence. */
9812 if (TREE_CODE (arg0) == icode
9813 && simple_condition_p (arg1)
9814 /* Needed for sequence points to handle trappings, and
9815 side-effects. */
9816 && simple_condition_p (TREE_OPERAND (arg0, 1)))
9818 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
9819 arg1);
9820 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
9821 tem);
9823 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
9824 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
9825 else if (TREE_CODE (arg1) == icode
9826 && simple_condition_p (arg0)
9827 /* Needed for sequence points to handle trappings, and
9828 side-effects. */
9829 && simple_condition_p (TREE_OPERAND (arg1, 0)))
9831 tem = fold_build2_loc (loc, ncode, type,
9832 arg0, TREE_OPERAND (arg1, 0));
9833 return fold_build2_loc (loc, icode, type, tem,
9834 TREE_OPERAND (arg1, 1));
9836 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
9837 into (A OR B).
9838 For sequence point consistancy, we need to check for trapping,
9839 and side-effects. */
9840 else if (code == icode && simple_condition_p (arg0)
9841 && simple_condition_p (arg1))
9842 return fold_build2_loc (loc, ncode, type, arg0, arg1);
9845 return NULL_TREE;
9848 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9849 by changing CODE to reduce the magnitude of constants involved in
9850 ARG0 of the comparison.
9851 Returns a canonicalized comparison tree if a simplification was
9852 possible, otherwise returns NULL_TREE.
9853 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9854 valid if signed overflow is undefined. */
9856 static tree
9857 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9858 tree arg0, tree arg1,
9859 bool *strict_overflow_p)
9861 enum tree_code code0 = TREE_CODE (arg0);
9862 tree t, cst0 = NULL_TREE;
9863 int sgn0;
9865 /* Match A +- CST code arg1. We can change this only if overflow
9866 is undefined. */
9867 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9868 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
9869 /* In principle pointers also have undefined overflow behavior,
9870 but that causes problems elsewhere. */
9871 && !POINTER_TYPE_P (TREE_TYPE (arg0))
9872 && (code0 == MINUS_EXPR
9873 || code0 == PLUS_EXPR)
9874 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
9875 return NULL_TREE;
9877 /* Identify the constant in arg0 and its sign. */
9878 cst0 = TREE_OPERAND (arg0, 1);
9879 sgn0 = tree_int_cst_sgn (cst0);
9881 /* Overflowed constants and zero will cause problems. */
9882 if (integer_zerop (cst0)
9883 || TREE_OVERFLOW (cst0))
9884 return NULL_TREE;
9886 /* See if we can reduce the magnitude of the constant in
9887 arg0 by changing the comparison code. */
9888 /* A - CST < arg1 -> A - CST-1 <= arg1. */
9889 if (code == LT_EXPR
9890 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9891 code = LE_EXPR;
9892 /* A + CST > arg1 -> A + CST-1 >= arg1. */
9893 else if (code == GT_EXPR
9894 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9895 code = GE_EXPR;
9896 /* A + CST <= arg1 -> A + CST-1 < arg1. */
9897 else if (code == LE_EXPR
9898 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9899 code = LT_EXPR;
9900 /* A - CST >= arg1 -> A - CST-1 > arg1. */
9901 else if (code == GE_EXPR
9902 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9903 code = GT_EXPR;
9904 else
9905 return NULL_TREE;
9906 *strict_overflow_p = true;
9908 /* Now build the constant reduced in magnitude. But not if that
9909 would produce one outside of its types range. */
9910 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9911 && ((sgn0 == 1
9912 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9913 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9914 || (sgn0 == -1
9915 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9916 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9917 return NULL_TREE;
9919 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9920 cst0, build_int_cst (TREE_TYPE (cst0), 1));
9921 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9922 t = fold_convert (TREE_TYPE (arg1), t);
9924 return fold_build2_loc (loc, code, type, t, arg1);
9927 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9928 overflow further. Try to decrease the magnitude of constants involved
9929 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9930 and put sole constants at the second argument position.
9931 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9933 static tree
9934 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9935 tree arg0, tree arg1)
9937 tree t;
9938 bool strict_overflow_p;
9939 const char * const warnmsg = G_("assuming signed overflow does not occur "
9940 "when reducing constant in comparison");
9942 /* Try canonicalization by simplifying arg0. */
9943 strict_overflow_p = false;
9944 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
9945 &strict_overflow_p);
9946 if (t)
9948 if (strict_overflow_p)
9949 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9950 return t;
9953 /* Try canonicalization by simplifying arg1 using the swapped
9954 comparison. */
9955 code = swap_tree_comparison (code);
9956 strict_overflow_p = false;
9957 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
9958 &strict_overflow_p);
9959 if (t && strict_overflow_p)
9960 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9961 return t;
9964 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9965 space. This is used to avoid issuing overflow warnings for
9966 expressions like &p->x which cannot wrap. */
9968 static bool
9969 pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
9971 if (!POINTER_TYPE_P (TREE_TYPE (base)))
9972 return true;
9974 if (maybe_lt (bitpos, 0))
9975 return true;
9977 poly_wide_int wi_offset;
9978 int precision = TYPE_PRECISION (TREE_TYPE (base));
9979 if (offset == NULL_TREE)
9980 wi_offset = wi::zero (precision);
9981 else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
9982 return true;
9983 else
9984 wi_offset = wi::to_poly_wide (offset);
9986 wi::overflow_type overflow;
9987 poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
9988 precision);
9989 poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
9990 if (overflow)
9991 return true;
9993 poly_uint64 total_hwi, size;
9994 if (!total.to_uhwi (&total_hwi)
9995 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
9996 &size)
9997 || known_eq (size, 0U))
9998 return true;
10000 if (known_le (total_hwi, size))
10001 return false;
10003 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
10004 array. */
10005 if (TREE_CODE (base) == ADDR_EXPR
10006 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
10007 &size)
10008 && maybe_ne (size, 0U)
10009 && known_le (total_hwi, size))
10010 return false;
10012 return true;
10015 /* Return a positive integer when the symbol DECL is known to have
10016 a nonzero address, zero when it's known not to (e.g., it's a weak
10017 symbol), and a negative integer when the symbol is not yet in the
10018 symbol table and so whether or not its address is zero is unknown.
10019 For function local objects always return positive integer. */
10020 static int
10021 maybe_nonzero_address (tree decl)
10023 /* Normally, don't do anything for variables and functions before symtab is
10024 built; it is quite possible that DECL will be declared weak later.
10025 But if folding_initializer, we need a constant answer now, so create
10026 the symtab entry and prevent later weak declaration. */
10027 if (DECL_P (decl) && decl_in_symtab_p (decl))
10028 if (struct symtab_node *symbol
10029 = (folding_initializer
10030 ? symtab_node::get_create (decl)
10031 : symtab_node::get (decl)))
10032 return symbol->nonzero_address ();
10034 /* Function local objects are never NULL. */
10035 if (DECL_P (decl)
10036 && (DECL_CONTEXT (decl)
10037 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
10038 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
10039 return 1;
10041 return -1;
10044 /* Subroutine of fold_binary. This routine performs all of the
10045 transformations that are common to the equality/inequality
10046 operators (EQ_EXPR and NE_EXPR) and the ordering operators
10047 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
10048 fold_binary should call fold_binary. Fold a comparison with
10049 tree code CODE and type TYPE with operands OP0 and OP1. Return
10050 the folded comparison or NULL_TREE. */
10052 static tree
10053 fold_comparison (location_t loc, enum tree_code code, tree type,
10054 tree op0, tree op1)
10056 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
10057 tree arg0, arg1, tem;
10059 arg0 = op0;
10060 arg1 = op1;
10062 STRIP_SIGN_NOPS (arg0);
10063 STRIP_SIGN_NOPS (arg1);
10065 /* For comparisons of pointers we can decompose it to a compile time
10066 comparison of the base objects and the offsets into the object.
10067 This requires at least one operand being an ADDR_EXPR or a
10068 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
10069 if (POINTER_TYPE_P (TREE_TYPE (arg0))
10070 && (TREE_CODE (arg0) == ADDR_EXPR
10071 || TREE_CODE (arg1) == ADDR_EXPR
10072 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
10073 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
10075 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
10076 poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
10077 machine_mode mode;
10078 int volatilep, reversep, unsignedp;
10079 bool indirect_base0 = false, indirect_base1 = false;
10081 /* Get base and offset for the access. Strip ADDR_EXPR for
10082 get_inner_reference, but put it back by stripping INDIRECT_REF
10083 off the base object if possible. indirect_baseN will be true
10084 if baseN is not an address but refers to the object itself. */
10085 base0 = arg0;
10086 if (TREE_CODE (arg0) == ADDR_EXPR)
10088 base0
10089 = get_inner_reference (TREE_OPERAND (arg0, 0),
10090 &bitsize, &bitpos0, &offset0, &mode,
10091 &unsignedp, &reversep, &volatilep);
10092 if (TREE_CODE (base0) == INDIRECT_REF)
10093 base0 = TREE_OPERAND (base0, 0);
10094 else
10095 indirect_base0 = true;
10097 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10099 base0 = TREE_OPERAND (arg0, 0);
10100 STRIP_SIGN_NOPS (base0);
10101 if (TREE_CODE (base0) == ADDR_EXPR)
10103 base0
10104 = get_inner_reference (TREE_OPERAND (base0, 0),
10105 &bitsize, &bitpos0, &offset0, &mode,
10106 &unsignedp, &reversep, &volatilep);
10107 if (TREE_CODE (base0) == INDIRECT_REF)
10108 base0 = TREE_OPERAND (base0, 0);
10109 else
10110 indirect_base0 = true;
10112 if (offset0 == NULL_TREE || integer_zerop (offset0))
10113 offset0 = TREE_OPERAND (arg0, 1);
10114 else
10115 offset0 = size_binop (PLUS_EXPR, offset0,
10116 TREE_OPERAND (arg0, 1));
10117 if (poly_int_tree_p (offset0))
10119 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
10120 TYPE_PRECISION (sizetype));
10121 tem <<= LOG2_BITS_PER_UNIT;
10122 tem += bitpos0;
10123 if (tem.to_shwi (&bitpos0))
10124 offset0 = NULL_TREE;
10128 base1 = arg1;
10129 if (TREE_CODE (arg1) == ADDR_EXPR)
10131 base1
10132 = get_inner_reference (TREE_OPERAND (arg1, 0),
10133 &bitsize, &bitpos1, &offset1, &mode,
10134 &unsignedp, &reversep, &volatilep);
10135 if (TREE_CODE (base1) == INDIRECT_REF)
10136 base1 = TREE_OPERAND (base1, 0);
10137 else
10138 indirect_base1 = true;
10140 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10142 base1 = TREE_OPERAND (arg1, 0);
10143 STRIP_SIGN_NOPS (base1);
10144 if (TREE_CODE (base1) == ADDR_EXPR)
10146 base1
10147 = get_inner_reference (TREE_OPERAND (base1, 0),
10148 &bitsize, &bitpos1, &offset1, &mode,
10149 &unsignedp, &reversep, &volatilep);
10150 if (TREE_CODE (base1) == INDIRECT_REF)
10151 base1 = TREE_OPERAND (base1, 0);
10152 else
10153 indirect_base1 = true;
10155 if (offset1 == NULL_TREE || integer_zerop (offset1))
10156 offset1 = TREE_OPERAND (arg1, 1);
10157 else
10158 offset1 = size_binop (PLUS_EXPR, offset1,
10159 TREE_OPERAND (arg1, 1));
10160 if (poly_int_tree_p (offset1))
10162 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
10163 TYPE_PRECISION (sizetype));
10164 tem <<= LOG2_BITS_PER_UNIT;
10165 tem += bitpos1;
10166 if (tem.to_shwi (&bitpos1))
10167 offset1 = NULL_TREE;
10171 /* If we have equivalent bases we might be able to simplify. */
10172 if (indirect_base0 == indirect_base1
10173 && operand_equal_p (base0, base1,
10174 indirect_base0 ? OEP_ADDRESS_OF : 0))
10176 /* We can fold this expression to a constant if the non-constant
10177 offset parts are equal. */
10178 if ((offset0 == offset1
10179 || (offset0 && offset1
10180 && operand_equal_p (offset0, offset1, 0)))
10181 && (equality_code
10182 || (indirect_base0
10183 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10184 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10186 if (!equality_code
10187 && maybe_ne (bitpos0, bitpos1)
10188 && (pointer_may_wrap_p (base0, offset0, bitpos0)
10189 || pointer_may_wrap_p (base1, offset1, bitpos1)))
10190 fold_overflow_warning (("assuming pointer wraparound does not "
10191 "occur when comparing P +- C1 with "
10192 "P +- C2"),
10193 WARN_STRICT_OVERFLOW_CONDITIONAL);
10195 switch (code)
10197 case EQ_EXPR:
10198 if (known_eq (bitpos0, bitpos1))
10199 return constant_boolean_node (true, type);
10200 if (known_ne (bitpos0, bitpos1))
10201 return constant_boolean_node (false, type);
10202 break;
10203 case NE_EXPR:
10204 if (known_ne (bitpos0, bitpos1))
10205 return constant_boolean_node (true, type);
10206 if (known_eq (bitpos0, bitpos1))
10207 return constant_boolean_node (false, type);
10208 break;
10209 case LT_EXPR:
10210 if (known_lt (bitpos0, bitpos1))
10211 return constant_boolean_node (true, type);
10212 if (known_ge (bitpos0, bitpos1))
10213 return constant_boolean_node (false, type);
10214 break;
10215 case LE_EXPR:
10216 if (known_le (bitpos0, bitpos1))
10217 return constant_boolean_node (true, type);
10218 if (known_gt (bitpos0, bitpos1))
10219 return constant_boolean_node (false, type);
10220 break;
10221 case GE_EXPR:
10222 if (known_ge (bitpos0, bitpos1))
10223 return constant_boolean_node (true, type);
10224 if (known_lt (bitpos0, bitpos1))
10225 return constant_boolean_node (false, type);
10226 break;
10227 case GT_EXPR:
10228 if (known_gt (bitpos0, bitpos1))
10229 return constant_boolean_node (true, type);
10230 if (known_le (bitpos0, bitpos1))
10231 return constant_boolean_node (false, type);
10232 break;
10233 default:;
10236 /* We can simplify the comparison to a comparison of the variable
10237 offset parts if the constant offset parts are equal.
10238 Be careful to use signed sizetype here because otherwise we
10239 mess with array offsets in the wrong way. This is possible
10240 because pointer arithmetic is restricted to retain within an
10241 object and overflow on pointer differences is undefined as of
10242 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
10243 else if (known_eq (bitpos0, bitpos1)
10244 && (equality_code
10245 || (indirect_base0
10246 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
10247 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10249 /* By converting to signed sizetype we cover middle-end pointer
10250 arithmetic which operates on unsigned pointer types of size
10251 type size and ARRAY_REF offsets which are properly sign or
10252 zero extended from their type in case it is narrower than
10253 sizetype. */
10254 if (offset0 == NULL_TREE)
10255 offset0 = build_int_cst (ssizetype, 0);
10256 else
10257 offset0 = fold_convert_loc (loc, ssizetype, offset0);
10258 if (offset1 == NULL_TREE)
10259 offset1 = build_int_cst (ssizetype, 0);
10260 else
10261 offset1 = fold_convert_loc (loc, ssizetype, offset1);
10263 if (!equality_code
10264 && (pointer_may_wrap_p (base0, offset0, bitpos0)
10265 || pointer_may_wrap_p (base1, offset1, bitpos1)))
10266 fold_overflow_warning (("assuming pointer wraparound does not "
10267 "occur when comparing P +- C1 with "
10268 "P +- C2"),
10269 WARN_STRICT_OVERFLOW_COMPARISON);
10271 return fold_build2_loc (loc, code, type, offset0, offset1);
10274 /* For equal offsets we can simplify to a comparison of the
10275 base addresses. */
10276 else if (known_eq (bitpos0, bitpos1)
10277 && (indirect_base0
10278 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
10279 && (indirect_base1
10280 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
10281 && ((offset0 == offset1)
10282 || (offset0 && offset1
10283 && operand_equal_p (offset0, offset1, 0))))
10285 if (indirect_base0)
10286 base0 = build_fold_addr_expr_loc (loc, base0);
10287 if (indirect_base1)
10288 base1 = build_fold_addr_expr_loc (loc, base1);
10289 return fold_build2_loc (loc, code, type, base0, base1);
10291 /* Comparison between an ordinary (non-weak) symbol and a null
10292 pointer can be eliminated since such symbols must have a non
10293 null address. In C, relational expressions between pointers
10294 to objects and null pointers are undefined. The results
10295 below follow the C++ rules with the additional property that
10296 every object pointer compares greater than a null pointer.
10298 else if (((DECL_P (base0)
10299 && maybe_nonzero_address (base0) > 0
10300 /* Avoid folding references to struct members at offset 0 to
10301 prevent tests like '&ptr->firstmember == 0' from getting
10302 eliminated. When ptr is null, although the -> expression
10303 is strictly speaking invalid, GCC retains it as a matter
10304 of QoI. See PR c/44555. */
10305 && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
10306 || CONSTANT_CLASS_P (base0))
10307 && indirect_base0
10308 /* The caller guarantees that when one of the arguments is
10309 constant (i.e., null in this case) it is second. */
10310 && integer_zerop (arg1))
10312 switch (code)
10314 case EQ_EXPR:
10315 case LE_EXPR:
10316 case LT_EXPR:
10317 return constant_boolean_node (false, type);
10318 case GE_EXPR:
10319 case GT_EXPR:
10320 case NE_EXPR:
10321 return constant_boolean_node (true, type);
10322 default:
10323 gcc_unreachable ();
10328 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
10329 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
10330 the resulting offset is smaller in absolute value than the
10331 original one and has the same sign. */
10332 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10333 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
10334 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10335 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10336 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
10337 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
10338 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10339 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
10341 tree const1 = TREE_OPERAND (arg0, 1);
10342 tree const2 = TREE_OPERAND (arg1, 1);
10343 tree variable1 = TREE_OPERAND (arg0, 0);
10344 tree variable2 = TREE_OPERAND (arg1, 0);
10345 tree cst;
10346 const char * const warnmsg = G_("assuming signed overflow does not "
10347 "occur when combining constants around "
10348 "a comparison");
10350 /* Put the constant on the side where it doesn't overflow and is
10351 of lower absolute value and of same sign than before. */
10352 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10353 ? MINUS_EXPR : PLUS_EXPR,
10354 const2, const1);
10355 if (!TREE_OVERFLOW (cst)
10356 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
10357 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
10359 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10360 return fold_build2_loc (loc, code, type,
10361 variable1,
10362 fold_build2_loc (loc, TREE_CODE (arg1),
10363 TREE_TYPE (arg1),
10364 variable2, cst));
10367 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
10368 ? MINUS_EXPR : PLUS_EXPR,
10369 const1, const2);
10370 if (!TREE_OVERFLOW (cst)
10371 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
10372 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
10374 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
10375 return fold_build2_loc (loc, code, type,
10376 fold_build2_loc (loc, TREE_CODE (arg0),
10377 TREE_TYPE (arg0),
10378 variable1, cst),
10379 variable2);
10383 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
10384 if (tem)
10385 return tem;
10387 /* If we are comparing an expression that just has comparisons
10388 of two integer values, arithmetic expressions of those comparisons,
10389 and constants, we can simplify it. There are only three cases
10390 to check: the two values can either be equal, the first can be
10391 greater, or the second can be greater. Fold the expression for
10392 those three values. Since each value must be 0 or 1, we have
10393 eight possibilities, each of which corresponds to the constant 0
10394 or 1 or one of the six possible comparisons.
10396 This handles common cases like (a > b) == 0 but also handles
10397 expressions like ((x > y) - (y > x)) > 0, which supposedly
10398 occur in macroized code. */
10400 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
10402 tree cval1 = 0, cval2 = 0;
10404 if (twoval_comparison_p (arg0, &cval1, &cval2)
10405 /* Don't handle degenerate cases here; they should already
10406 have been handled anyway. */
10407 && cval1 != 0 && cval2 != 0
10408 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
10409 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
10410 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
10411 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
10412 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
10413 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
10414 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
10416 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
10417 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
10419 /* We can't just pass T to eval_subst in case cval1 or cval2
10420 was the same as ARG1. */
10422 tree high_result
10423 = fold_build2_loc (loc, code, type,
10424 eval_subst (loc, arg0, cval1, maxval,
10425 cval2, minval),
10426 arg1);
10427 tree equal_result
10428 = fold_build2_loc (loc, code, type,
10429 eval_subst (loc, arg0, cval1, maxval,
10430 cval2, maxval),
10431 arg1);
10432 tree low_result
10433 = fold_build2_loc (loc, code, type,
10434 eval_subst (loc, arg0, cval1, minval,
10435 cval2, maxval),
10436 arg1);
10438 /* All three of these results should be 0 or 1. Confirm they are.
10439 Then use those values to select the proper code to use. */
10441 if (TREE_CODE (high_result) == INTEGER_CST
10442 && TREE_CODE (equal_result) == INTEGER_CST
10443 && TREE_CODE (low_result) == INTEGER_CST)
10445 /* Make a 3-bit mask with the high-order bit being the
10446 value for `>', the next for '=', and the low for '<'. */
10447 switch ((integer_onep (high_result) * 4)
10448 + (integer_onep (equal_result) * 2)
10449 + integer_onep (low_result))
10451 case 0:
10452 /* Always false. */
10453 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10454 case 1:
10455 code = LT_EXPR;
10456 break;
10457 case 2:
10458 code = EQ_EXPR;
10459 break;
10460 case 3:
10461 code = LE_EXPR;
10462 break;
10463 case 4:
10464 code = GT_EXPR;
10465 break;
10466 case 5:
10467 code = NE_EXPR;
10468 break;
10469 case 6:
10470 code = GE_EXPR;
10471 break;
10472 case 7:
10473 /* Always true. */
10474 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10477 return fold_build2_loc (loc, code, type, cval1, cval2);
10482 return NULL_TREE;
10486 /* Subroutine of fold_binary. Optimize complex multiplications of the
10487 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
10488 argument EXPR represents the expression "z" of type TYPE. */
10490 static tree
10491 fold_mult_zconjz (location_t loc, tree type, tree expr)
10493 tree itype = TREE_TYPE (type);
10494 tree rpart, ipart, tem;
10496 if (TREE_CODE (expr) == COMPLEX_EXPR)
10498 rpart = TREE_OPERAND (expr, 0);
10499 ipart = TREE_OPERAND (expr, 1);
10501 else if (TREE_CODE (expr) == COMPLEX_CST)
10503 rpart = TREE_REALPART (expr);
10504 ipart = TREE_IMAGPART (expr);
10506 else
10508 expr = save_expr (expr);
10509 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
10510 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
10513 rpart = save_expr (rpart);
10514 ipart = save_expr (ipart);
10515 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
10516 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
10517 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
10518 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
10519 build_zero_cst (itype));
10523 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
10524 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
10525 true if successful. */
10527 static bool
10528 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
10530 unsigned HOST_WIDE_INT i, nunits;
10532 if (TREE_CODE (arg) == VECTOR_CST
10533 && VECTOR_CST_NELTS (arg).is_constant (&nunits))
10535 for (i = 0; i < nunits; ++i)
10536 elts[i] = VECTOR_CST_ELT (arg, i);
10538 else if (TREE_CODE (arg) == CONSTRUCTOR)
10540 constructor_elt *elt;
10542 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
10543 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
10544 return false;
10545 else
10546 elts[i] = elt->value;
10548 else
10549 return false;
10550 for (; i < nelts; i++)
10551 elts[i]
10552 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
10553 return true;
10556 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
10557 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
10558 NULL_TREE otherwise. */
10560 tree
10561 fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
10563 unsigned int i;
10564 unsigned HOST_WIDE_INT nelts;
10565 bool need_ctor = false;
10567 if (!sel.length ().is_constant (&nelts))
10568 return NULL_TREE;
10569 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), nelts)
10570 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)), nelts)
10571 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)), nelts));
10572 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
10573 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
10574 return NULL_TREE;
10576 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
10577 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
10578 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
10579 return NULL_TREE;
10581 tree_vector_builder out_elts (type, nelts, 1);
10582 for (i = 0; i < nelts; i++)
10584 HOST_WIDE_INT index;
10585 if (!sel[i].is_constant (&index))
10586 return NULL_TREE;
10587 if (!CONSTANT_CLASS_P (in_elts[index]))
10588 need_ctor = true;
10589 out_elts.quick_push (unshare_expr (in_elts[index]));
10592 if (need_ctor)
10594 vec<constructor_elt, va_gc> *v;
10595 vec_alloc (v, nelts);
10596 for (i = 0; i < nelts; i++)
10597 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
10598 return build_constructor (type, v);
10600 else
10601 return out_elts.build ();
10604 /* Try to fold a pointer difference of type TYPE two address expressions of
10605 array references AREF0 and AREF1 using location LOC. Return a
10606 simplified expression for the difference or NULL_TREE. */
10608 static tree
10609 fold_addr_of_array_ref_difference (location_t loc, tree type,
10610 tree aref0, tree aref1,
10611 bool use_pointer_diff)
10613 tree base0 = TREE_OPERAND (aref0, 0);
10614 tree base1 = TREE_OPERAND (aref1, 0);
10615 tree base_offset = build_int_cst (type, 0);
10617 /* If the bases are array references as well, recurse. If the bases
10618 are pointer indirections compute the difference of the pointers.
10619 If the bases are equal, we are set. */
10620 if ((TREE_CODE (base0) == ARRAY_REF
10621 && TREE_CODE (base1) == ARRAY_REF
10622 && (base_offset
10623 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
10624 use_pointer_diff)))
10625 || (INDIRECT_REF_P (base0)
10626 && INDIRECT_REF_P (base1)
10627 && (base_offset
10628 = use_pointer_diff
10629 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
10630 TREE_OPERAND (base0, 0),
10631 TREE_OPERAND (base1, 0))
10632 : fold_binary_loc (loc, MINUS_EXPR, type,
10633 fold_convert (type,
10634 TREE_OPERAND (base0, 0)),
10635 fold_convert (type,
10636 TREE_OPERAND (base1, 0)))))
10637 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
10639 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10640 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10641 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
10642 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
10643 return fold_build2_loc (loc, PLUS_EXPR, type,
10644 base_offset,
10645 fold_build2_loc (loc, MULT_EXPR, type,
10646 diff, esz));
10648 return NULL_TREE;
10651 /* If the real or vector real constant CST of type TYPE has an exact
10652 inverse, return it, else return NULL. */
10654 tree
10655 exact_inverse (tree type, tree cst)
10657 REAL_VALUE_TYPE r;
10658 tree unit_type;
10659 machine_mode mode;
10661 switch (TREE_CODE (cst))
10663 case REAL_CST:
10664 r = TREE_REAL_CST (cst);
10666 if (exact_real_inverse (TYPE_MODE (type), &r))
10667 return build_real (type, r);
10669 return NULL_TREE;
10671 case VECTOR_CST:
10673 unit_type = TREE_TYPE (type);
10674 mode = TYPE_MODE (unit_type);
10676 tree_vector_builder elts;
10677 if (!elts.new_unary_operation (type, cst, false))
10678 return NULL_TREE;
10679 unsigned int count = elts.encoded_nelts ();
10680 for (unsigned int i = 0; i < count; ++i)
10682 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
10683 if (!exact_real_inverse (mode, &r))
10684 return NULL_TREE;
10685 elts.quick_push (build_real (unit_type, r));
10688 return elts.build ();
10691 default:
10692 return NULL_TREE;
10696 /* Mask out the tz least significant bits of X of type TYPE where
10697 tz is the number of trailing zeroes in Y. */
10698 static wide_int
10699 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
10701 int tz = wi::ctz (y);
10702 if (tz > 0)
10703 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
10704 return x;
10707 /* Return true when T is an address and is known to be nonzero.
10708 For floating point we further ensure that T is not denormal.
10709 Similar logic is present in nonzero_address in rtlanal.h.
10711 If the return value is based on the assumption that signed overflow
10712 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
10713 change *STRICT_OVERFLOW_P. */
10715 static bool
10716 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
10718 tree type = TREE_TYPE (t);
10719 enum tree_code code;
10721 /* Doing something useful for floating point would need more work. */
10722 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10723 return false;
10725 code = TREE_CODE (t);
10726 switch (TREE_CODE_CLASS (code))
10728 case tcc_unary:
10729 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10730 strict_overflow_p);
10731 case tcc_binary:
10732 case tcc_comparison:
10733 return tree_binary_nonzero_warnv_p (code, type,
10734 TREE_OPERAND (t, 0),
10735 TREE_OPERAND (t, 1),
10736 strict_overflow_p);
10737 case tcc_constant:
10738 case tcc_declaration:
10739 case tcc_reference:
10740 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10742 default:
10743 break;
10746 switch (code)
10748 case TRUTH_NOT_EXPR:
10749 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
10750 strict_overflow_p);
10752 case TRUTH_AND_EXPR:
10753 case TRUTH_OR_EXPR:
10754 case TRUTH_XOR_EXPR:
10755 return tree_binary_nonzero_warnv_p (code, type,
10756 TREE_OPERAND (t, 0),
10757 TREE_OPERAND (t, 1),
10758 strict_overflow_p);
10760 case COND_EXPR:
10761 case CONSTRUCTOR:
10762 case OBJ_TYPE_REF:
10763 case ADDR_EXPR:
10764 case WITH_SIZE_EXPR:
10765 case SSA_NAME:
10766 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
10768 case COMPOUND_EXPR:
10769 case MODIFY_EXPR:
10770 case BIND_EXPR:
10771 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
10772 strict_overflow_p);
10774 case SAVE_EXPR:
10775 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
10776 strict_overflow_p);
10778 case CALL_EXPR:
10780 tree fndecl = get_callee_fndecl (t);
10781 if (!fndecl) return false;
10782 if (flag_delete_null_pointer_checks && !flag_check_new
10783 && DECL_IS_OPERATOR_NEW_P (fndecl)
10784 && !TREE_NOTHROW (fndecl))
10785 return true;
10786 if (flag_delete_null_pointer_checks
10787 && lookup_attribute ("returns_nonnull",
10788 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
10789 return true;
10790 return alloca_call_p (t);
10793 default:
10794 break;
10796 return false;
10799 /* Return true when T is an address and is known to be nonzero.
10800 Handle warnings about undefined signed overflow. */
10802 bool
10803 tree_expr_nonzero_p (tree t)
10805 bool ret, strict_overflow_p;
10807 strict_overflow_p = false;
10808 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
10809 if (strict_overflow_p)
10810 fold_overflow_warning (("assuming signed overflow does not occur when "
10811 "determining that expression is always "
10812 "non-zero"),
10813 WARN_STRICT_OVERFLOW_MISC);
10814 return ret;
10817 /* Return true if T is known not to be equal to an integer W. */
10819 bool
10820 expr_not_equal_to (tree t, const wide_int &w)
10822 int_range_max vr;
10823 switch (TREE_CODE (t))
10825 case INTEGER_CST:
10826 return wi::to_wide (t) != w;
10828 case SSA_NAME:
10829 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
10830 return false;
10832 if (cfun)
10833 get_range_query (cfun)->range_of_expr (vr, t);
10834 else
10835 get_global_range_query ()->range_of_expr (vr, t);
10837 if (!vr.undefined_p ()
10838 && !vr.contains_p (wide_int_to_tree (TREE_TYPE (t), w)))
10839 return true;
10840 /* If T has some known zero bits and W has any of those bits set,
10841 then T is known not to be equal to W. */
10842 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
10843 TYPE_PRECISION (TREE_TYPE (t))), 0))
10844 return true;
10845 return false;
10847 default:
10848 return false;
10852 /* Fold a binary expression of code CODE and type TYPE with operands
10853 OP0 and OP1. LOC is the location of the resulting expression.
10854 Return the folded expression if folding is successful. Otherwise,
10855 return NULL_TREE. */
10857 tree
10858 fold_binary_loc (location_t loc, enum tree_code code, tree type,
10859 tree op0, tree op1)
10861 enum tree_code_class kind = TREE_CODE_CLASS (code);
10862 tree arg0, arg1, tem;
10863 tree t1 = NULL_TREE;
10864 bool strict_overflow_p;
10865 unsigned int prec;
10867 gcc_assert (IS_EXPR_CODE_CLASS (kind)
10868 && TREE_CODE_LENGTH (code) == 2
10869 && op0 != NULL_TREE
10870 && op1 != NULL_TREE);
10872 arg0 = op0;
10873 arg1 = op1;
10875 /* Strip any conversions that don't change the mode. This is
10876 safe for every expression, except for a comparison expression
10877 because its signedness is derived from its operands. So, in
10878 the latter case, only strip conversions that don't change the
10879 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10880 preserved.
10882 Note that this is done as an internal manipulation within the
10883 constant folder, in order to find the simplest representation
10884 of the arguments so that their form can be studied. In any
10885 cases, the appropriate type conversions should be put back in
10886 the tree that will get out of the constant folder. */
10888 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10890 STRIP_SIGN_NOPS (arg0);
10891 STRIP_SIGN_NOPS (arg1);
10893 else
10895 STRIP_NOPS (arg0);
10896 STRIP_NOPS (arg1);
10899 /* Note that TREE_CONSTANT isn't enough: static var addresses are
10900 constant but we can't do arithmetic on them. */
10901 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
10903 tem = const_binop (code, type, arg0, arg1);
10904 if (tem != NULL_TREE)
10906 if (TREE_TYPE (tem) != type)
10907 tem = fold_convert_loc (loc, type, tem);
10908 return tem;
10912 /* If this is a commutative operation, and ARG0 is a constant, move it
10913 to ARG1 to reduce the number of tests below. */
10914 if (commutative_tree_code (code)
10915 && tree_swap_operands_p (arg0, arg1))
10916 return fold_build2_loc (loc, code, type, op1, op0);
10918 /* Likewise if this is a comparison, and ARG0 is a constant, move it
10919 to ARG1 to reduce the number of tests below. */
10920 if (kind == tcc_comparison
10921 && tree_swap_operands_p (arg0, arg1))
10922 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
10924 tem = generic_simplify (loc, code, type, op0, op1);
10925 if (tem)
10926 return tem;
10928 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10930 First check for cases where an arithmetic operation is applied to a
10931 compound, conditional, or comparison operation. Push the arithmetic
10932 operation inside the compound or conditional to see if any folding
10933 can then be done. Convert comparison to conditional for this purpose.
10934 The also optimizes non-constant cases that used to be done in
10935 expand_expr.
10937 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10938 one of the operands is a comparison and the other is a comparison, a
10939 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10940 code below would make the expression more complex. Change it to a
10941 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10942 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10944 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10945 || code == EQ_EXPR || code == NE_EXPR)
10946 && !VECTOR_TYPE_P (TREE_TYPE (arg0))
10947 && ((truth_value_p (TREE_CODE (arg0))
10948 && (truth_value_p (TREE_CODE (arg1))
10949 || (TREE_CODE (arg1) == BIT_AND_EXPR
10950 && integer_onep (TREE_OPERAND (arg1, 1)))))
10951 || (truth_value_p (TREE_CODE (arg1))
10952 && (truth_value_p (TREE_CODE (arg0))
10953 || (TREE_CODE (arg0) == BIT_AND_EXPR
10954 && integer_onep (TREE_OPERAND (arg0, 1)))))))
10956 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10957 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
10958 : TRUTH_XOR_EXPR,
10959 boolean_type_node,
10960 fold_convert_loc (loc, boolean_type_node, arg0),
10961 fold_convert_loc (loc, boolean_type_node, arg1));
10963 if (code == EQ_EXPR)
10964 tem = invert_truthvalue_loc (loc, tem);
10966 return fold_convert_loc (loc, type, tem);
10969 if (TREE_CODE_CLASS (code) == tcc_binary
10970 || TREE_CODE_CLASS (code) == tcc_comparison)
10972 if (TREE_CODE (arg0) == COMPOUND_EXPR)
10974 tem = fold_build2_loc (loc, code, type,
10975 fold_convert_loc (loc, TREE_TYPE (op0),
10976 TREE_OPERAND (arg0, 1)), op1);
10977 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
10978 tem);
10980 if (TREE_CODE (arg1) == COMPOUND_EXPR)
10982 tem = fold_build2_loc (loc, code, type, op0,
10983 fold_convert_loc (loc, TREE_TYPE (op1),
10984 TREE_OPERAND (arg1, 1)));
10985 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
10986 tem);
10989 if (TREE_CODE (arg0) == COND_EXPR
10990 || TREE_CODE (arg0) == VEC_COND_EXPR
10991 || COMPARISON_CLASS_P (arg0))
10993 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10994 arg0, arg1,
10995 /*cond_first_p=*/1);
10996 if (tem != NULL_TREE)
10997 return tem;
11000 if (TREE_CODE (arg1) == COND_EXPR
11001 || TREE_CODE (arg1) == VEC_COND_EXPR
11002 || COMPARISON_CLASS_P (arg1))
11004 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
11005 arg1, arg0,
11006 /*cond_first_p=*/0);
11007 if (tem != NULL_TREE)
11008 return tem;
11012 switch (code)
11014 case MEM_REF:
11015 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
11016 if (TREE_CODE (arg0) == ADDR_EXPR
11017 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
11019 tree iref = TREE_OPERAND (arg0, 0);
11020 return fold_build2 (MEM_REF, type,
11021 TREE_OPERAND (iref, 0),
11022 int_const_binop (PLUS_EXPR, arg1,
11023 TREE_OPERAND (iref, 1)));
11026 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
11027 if (TREE_CODE (arg0) == ADDR_EXPR
11028 && handled_component_p (TREE_OPERAND (arg0, 0)))
11030 tree base;
11031 poly_int64 coffset;
11032 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
11033 &coffset);
11034 if (!base)
11035 return NULL_TREE;
11036 return fold_build2 (MEM_REF, type,
11037 build1 (ADDR_EXPR, TREE_TYPE (arg0), base),
11038 int_const_binop (PLUS_EXPR, arg1,
11039 size_int (coffset)));
11042 return NULL_TREE;
11044 case POINTER_PLUS_EXPR:
11045 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
11046 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11047 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
11048 return fold_convert_loc (loc, type,
11049 fold_build2_loc (loc, PLUS_EXPR, sizetype,
11050 fold_convert_loc (loc, sizetype,
11051 arg1),
11052 fold_convert_loc (loc, sizetype,
11053 arg0)));
11055 return NULL_TREE;
11057 case PLUS_EXPR:
11058 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
11060 /* X + (X / CST) * -CST is X % CST. */
11061 if (TREE_CODE (arg1) == MULT_EXPR
11062 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
11063 && operand_equal_p (arg0,
11064 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
11066 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
11067 tree cst1 = TREE_OPERAND (arg1, 1);
11068 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
11069 cst1, cst0);
11070 if (sum && integer_zerop (sum))
11071 return fold_convert_loc (loc, type,
11072 fold_build2_loc (loc, TRUNC_MOD_EXPR,
11073 TREE_TYPE (arg0), arg0,
11074 cst0));
11078 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
11079 one. Make sure the type is not saturating and has the signedness of
11080 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11081 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11082 if ((TREE_CODE (arg0) == MULT_EXPR
11083 || TREE_CODE (arg1) == MULT_EXPR)
11084 && !TYPE_SATURATING (type)
11085 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11086 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11087 && (!FLOAT_TYPE_P (type) || flag_associative_math))
11089 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11090 if (tem)
11091 return tem;
11094 if (! FLOAT_TYPE_P (type))
11096 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
11097 (plus (plus (mult) (mult)) (foo)) so that we can
11098 take advantage of the factoring cases below. */
11099 if (ANY_INTEGRAL_TYPE_P (type)
11100 && TYPE_OVERFLOW_WRAPS (type)
11101 && (((TREE_CODE (arg0) == PLUS_EXPR
11102 || TREE_CODE (arg0) == MINUS_EXPR)
11103 && TREE_CODE (arg1) == MULT_EXPR)
11104 || ((TREE_CODE (arg1) == PLUS_EXPR
11105 || TREE_CODE (arg1) == MINUS_EXPR)
11106 && TREE_CODE (arg0) == MULT_EXPR)))
11108 tree parg0, parg1, parg, marg;
11109 enum tree_code pcode;
11111 if (TREE_CODE (arg1) == MULT_EXPR)
11112 parg = arg0, marg = arg1;
11113 else
11114 parg = arg1, marg = arg0;
11115 pcode = TREE_CODE (parg);
11116 parg0 = TREE_OPERAND (parg, 0);
11117 parg1 = TREE_OPERAND (parg, 1);
11118 STRIP_NOPS (parg0);
11119 STRIP_NOPS (parg1);
11121 if (TREE_CODE (parg0) == MULT_EXPR
11122 && TREE_CODE (parg1) != MULT_EXPR)
11123 return fold_build2_loc (loc, pcode, type,
11124 fold_build2_loc (loc, PLUS_EXPR, type,
11125 fold_convert_loc (loc, type,
11126 parg0),
11127 fold_convert_loc (loc, type,
11128 marg)),
11129 fold_convert_loc (loc, type, parg1));
11130 if (TREE_CODE (parg0) != MULT_EXPR
11131 && TREE_CODE (parg1) == MULT_EXPR)
11132 return
11133 fold_build2_loc (loc, PLUS_EXPR, type,
11134 fold_convert_loc (loc, type, parg0),
11135 fold_build2_loc (loc, pcode, type,
11136 fold_convert_loc (loc, type, marg),
11137 fold_convert_loc (loc, type,
11138 parg1)));
11141 else
11143 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
11144 to __complex__ ( x, y ). This is not the same for SNaNs or
11145 if signed zeros are involved. */
11146 if (!HONOR_SNANS (arg0)
11147 && !HONOR_SIGNED_ZEROS (arg0)
11148 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11150 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11151 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11152 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11153 bool arg0rz = false, arg0iz = false;
11154 if ((arg0r && (arg0rz = real_zerop (arg0r)))
11155 || (arg0i && (arg0iz = real_zerop (arg0i))))
11157 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11158 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11159 if (arg0rz && arg1i && real_zerop (arg1i))
11161 tree rp = arg1r ? arg1r
11162 : build1 (REALPART_EXPR, rtype, arg1);
11163 tree ip = arg0i ? arg0i
11164 : build1 (IMAGPART_EXPR, rtype, arg0);
11165 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11167 else if (arg0iz && arg1r && real_zerop (arg1r))
11169 tree rp = arg0r ? arg0r
11170 : build1 (REALPART_EXPR, rtype, arg0);
11171 tree ip = arg1i ? arg1i
11172 : build1 (IMAGPART_EXPR, rtype, arg1);
11173 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11178 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
11179 We associate floats only if the user has specified
11180 -fassociative-math. */
11181 if (flag_associative_math
11182 && TREE_CODE (arg1) == PLUS_EXPR
11183 && TREE_CODE (arg0) != MULT_EXPR)
11185 tree tree10 = TREE_OPERAND (arg1, 0);
11186 tree tree11 = TREE_OPERAND (arg1, 1);
11187 if (TREE_CODE (tree11) == MULT_EXPR
11188 && TREE_CODE (tree10) == MULT_EXPR)
11190 tree tree0;
11191 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
11192 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
11195 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
11196 We associate floats only if the user has specified
11197 -fassociative-math. */
11198 if (flag_associative_math
11199 && TREE_CODE (arg0) == PLUS_EXPR
11200 && TREE_CODE (arg1) != MULT_EXPR)
11202 tree tree00 = TREE_OPERAND (arg0, 0);
11203 tree tree01 = TREE_OPERAND (arg0, 1);
11204 if (TREE_CODE (tree01) == MULT_EXPR
11205 && TREE_CODE (tree00) == MULT_EXPR)
11207 tree tree0;
11208 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
11209 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
11214 bit_rotate:
11215 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
11216 is a rotate of A by C1 bits. */
11217 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
11218 is a rotate of A by B bits.
11219 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
11220 though in this case CODE must be | and not + or ^, otherwise
11221 it doesn't return A when B is 0. */
11223 enum tree_code code0, code1;
11224 tree rtype;
11225 code0 = TREE_CODE (arg0);
11226 code1 = TREE_CODE (arg1);
11227 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
11228 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
11229 && operand_equal_p (TREE_OPERAND (arg0, 0),
11230 TREE_OPERAND (arg1, 0), 0)
11231 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
11232 TYPE_UNSIGNED (rtype))
11233 /* Only create rotates in complete modes. Other cases are not
11234 expanded properly. */
11235 && (element_precision (rtype)
11236 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
11238 tree tree01, tree11;
11239 tree orig_tree01, orig_tree11;
11240 enum tree_code code01, code11;
11242 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
11243 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
11244 STRIP_NOPS (tree01);
11245 STRIP_NOPS (tree11);
11246 code01 = TREE_CODE (tree01);
11247 code11 = TREE_CODE (tree11);
11248 if (code11 != MINUS_EXPR
11249 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
11251 std::swap (code0, code1);
11252 std::swap (code01, code11);
11253 std::swap (tree01, tree11);
11254 std::swap (orig_tree01, orig_tree11);
11256 if (code01 == INTEGER_CST
11257 && code11 == INTEGER_CST
11258 && (wi::to_widest (tree01) + wi::to_widest (tree11)
11259 == element_precision (rtype)))
11261 tem = build2_loc (loc, LROTATE_EXPR,
11262 rtype, TREE_OPERAND (arg0, 0),
11263 code0 == LSHIFT_EXPR
11264 ? orig_tree01 : orig_tree11);
11265 return fold_convert_loc (loc, type, tem);
11267 else if (code11 == MINUS_EXPR)
11269 tree tree110, tree111;
11270 tree110 = TREE_OPERAND (tree11, 0);
11271 tree111 = TREE_OPERAND (tree11, 1);
11272 STRIP_NOPS (tree110);
11273 STRIP_NOPS (tree111);
11274 if (TREE_CODE (tree110) == INTEGER_CST
11275 && compare_tree_int (tree110,
11276 element_precision (rtype)) == 0
11277 && operand_equal_p (tree01, tree111, 0))
11279 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11280 ? LROTATE_EXPR : RROTATE_EXPR),
11281 rtype, TREE_OPERAND (arg0, 0),
11282 orig_tree01);
11283 return fold_convert_loc (loc, type, tem);
11286 else if (code == BIT_IOR_EXPR
11287 && code11 == BIT_AND_EXPR
11288 && pow2p_hwi (element_precision (rtype)))
11290 tree tree110, tree111;
11291 tree110 = TREE_OPERAND (tree11, 0);
11292 tree111 = TREE_OPERAND (tree11, 1);
11293 STRIP_NOPS (tree110);
11294 STRIP_NOPS (tree111);
11295 if (TREE_CODE (tree110) == NEGATE_EXPR
11296 && TREE_CODE (tree111) == INTEGER_CST
11297 && compare_tree_int (tree111,
11298 element_precision (rtype) - 1) == 0
11299 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
11301 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
11302 ? LROTATE_EXPR : RROTATE_EXPR),
11303 rtype, TREE_OPERAND (arg0, 0),
11304 orig_tree01);
11305 return fold_convert_loc (loc, type, tem);
11311 associate:
11312 /* In most languages, can't associate operations on floats through
11313 parentheses. Rather than remember where the parentheses were, we
11314 don't associate floats at all, unless the user has specified
11315 -fassociative-math.
11316 And, we need to make sure type is not saturating. */
11318 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
11319 && !TYPE_SATURATING (type))
11321 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
11322 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
11323 tree atype = type;
11324 bool ok = true;
11326 /* Split both trees into variables, constants, and literals. Then
11327 associate each group together, the constants with literals,
11328 then the result with variables. This increases the chances of
11329 literals being recombined later and of generating relocatable
11330 expressions for the sum of a constant and literal. */
11331 var0 = split_tree (arg0, type, code,
11332 &minus_var0, &con0, &minus_con0,
11333 &lit0, &minus_lit0, 0);
11334 var1 = split_tree (arg1, type, code,
11335 &minus_var1, &con1, &minus_con1,
11336 &lit1, &minus_lit1, code == MINUS_EXPR);
11338 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
11339 if (code == MINUS_EXPR)
11340 code = PLUS_EXPR;
11342 /* With undefined overflow prefer doing association in a type
11343 which wraps on overflow, if that is one of the operand types. */
11344 if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
11345 && !TYPE_OVERFLOW_WRAPS (type))
11347 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11348 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
11349 atype = TREE_TYPE (arg0);
11350 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
11351 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
11352 atype = TREE_TYPE (arg1);
11353 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
11356 /* With undefined overflow we can only associate constants with one
11357 variable, and constants whose association doesn't overflow. */
11358 if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
11359 && !TYPE_OVERFLOW_WRAPS (atype))
11361 if ((var0 && var1) || (minus_var0 && minus_var1))
11363 /* ??? If split_tree would handle NEGATE_EXPR we could
11364 simply reject these cases and the allowed cases would
11365 be the var0/minus_var1 ones. */
11366 tree tmp0 = var0 ? var0 : minus_var0;
11367 tree tmp1 = var1 ? var1 : minus_var1;
11368 bool one_neg = false;
11370 if (TREE_CODE (tmp0) == NEGATE_EXPR)
11372 tmp0 = TREE_OPERAND (tmp0, 0);
11373 one_neg = !one_neg;
11375 if (CONVERT_EXPR_P (tmp0)
11376 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11377 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
11378 <= TYPE_PRECISION (atype)))
11379 tmp0 = TREE_OPERAND (tmp0, 0);
11380 if (TREE_CODE (tmp1) == NEGATE_EXPR)
11382 tmp1 = TREE_OPERAND (tmp1, 0);
11383 one_neg = !one_neg;
11385 if (CONVERT_EXPR_P (tmp1)
11386 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11387 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
11388 <= TYPE_PRECISION (atype)))
11389 tmp1 = TREE_OPERAND (tmp1, 0);
11390 /* The only case we can still associate with two variables
11391 is if they cancel out. */
11392 if (!one_neg
11393 || !operand_equal_p (tmp0, tmp1, 0))
11394 ok = false;
11396 else if ((var0 && minus_var1
11397 && ! operand_equal_p (var0, minus_var1, 0))
11398 || (minus_var0 && var1
11399 && ! operand_equal_p (minus_var0, var1, 0)))
11400 ok = false;
11403 /* Only do something if we found more than two objects. Otherwise,
11404 nothing has changed and we risk infinite recursion. */
11405 if (ok
11406 && ((var0 != 0) + (var1 != 0)
11407 + (minus_var0 != 0) + (minus_var1 != 0)
11408 + (con0 != 0) + (con1 != 0)
11409 + (minus_con0 != 0) + (minus_con1 != 0)
11410 + (lit0 != 0) + (lit1 != 0)
11411 + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
11413 var0 = associate_trees (loc, var0, var1, code, atype);
11414 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
11415 code, atype);
11416 con0 = associate_trees (loc, con0, con1, code, atype);
11417 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
11418 code, atype);
11419 lit0 = associate_trees (loc, lit0, lit1, code, atype);
11420 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
11421 code, atype);
11423 if (minus_var0 && var0)
11425 var0 = associate_trees (loc, var0, minus_var0,
11426 MINUS_EXPR, atype);
11427 minus_var0 = 0;
11429 if (minus_con0 && con0)
11431 con0 = associate_trees (loc, con0, minus_con0,
11432 MINUS_EXPR, atype);
11433 minus_con0 = 0;
11436 /* Preserve the MINUS_EXPR if the negative part of the literal is
11437 greater than the positive part. Otherwise, the multiplicative
11438 folding code (i.e extract_muldiv) may be fooled in case
11439 unsigned constants are subtracted, like in the following
11440 example: ((X*2 + 4) - 8U)/2. */
11441 if (minus_lit0 && lit0)
11443 if (TREE_CODE (lit0) == INTEGER_CST
11444 && TREE_CODE (minus_lit0) == INTEGER_CST
11445 && tree_int_cst_lt (lit0, minus_lit0)
11446 /* But avoid ending up with only negated parts. */
11447 && (var0 || con0))
11449 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
11450 MINUS_EXPR, atype);
11451 lit0 = 0;
11453 else
11455 lit0 = associate_trees (loc, lit0, minus_lit0,
11456 MINUS_EXPR, atype);
11457 minus_lit0 = 0;
11461 /* Don't introduce overflows through reassociation. */
11462 if ((lit0 && TREE_OVERFLOW_P (lit0))
11463 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
11464 return NULL_TREE;
11466 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
11467 con0 = associate_trees (loc, con0, lit0, code, atype);
11468 lit0 = 0;
11469 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
11470 code, atype);
11471 minus_lit0 = 0;
11473 /* Eliminate minus_con0. */
11474 if (minus_con0)
11476 if (con0)
11477 con0 = associate_trees (loc, con0, minus_con0,
11478 MINUS_EXPR, atype);
11479 else if (var0)
11480 var0 = associate_trees (loc, var0, minus_con0,
11481 MINUS_EXPR, atype);
11482 else
11483 gcc_unreachable ();
11484 minus_con0 = 0;
11487 /* Eliminate minus_var0. */
11488 if (minus_var0)
11490 if (con0)
11491 con0 = associate_trees (loc, con0, minus_var0,
11492 MINUS_EXPR, atype);
11493 else
11494 gcc_unreachable ();
11495 minus_var0 = 0;
11498 return
11499 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
11500 code, atype));
11504 return NULL_TREE;
11506 case POINTER_DIFF_EXPR:
11507 case MINUS_EXPR:
11508 /* Fold &a[i] - &a[j] to i-j. */
11509 if (TREE_CODE (arg0) == ADDR_EXPR
11510 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
11511 && TREE_CODE (arg1) == ADDR_EXPR
11512 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
11514 tree tem = fold_addr_of_array_ref_difference (loc, type,
11515 TREE_OPERAND (arg0, 0),
11516 TREE_OPERAND (arg1, 0),
11517 code
11518 == POINTER_DIFF_EXPR);
11519 if (tem)
11520 return tem;
11523 /* Further transformations are not for pointers. */
11524 if (code == POINTER_DIFF_EXPR)
11525 return NULL_TREE;
11527 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
11528 if (TREE_CODE (arg0) == NEGATE_EXPR
11529 && negate_expr_p (op1)
11530 /* If arg0 is e.g. unsigned int and type is int, then this could
11531 introduce UB, because if A is INT_MIN at runtime, the original
11532 expression can be well defined while the latter is not.
11533 See PR83269. */
11534 && !(ANY_INTEGRAL_TYPE_P (type)
11535 && TYPE_OVERFLOW_UNDEFINED (type)
11536 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11537 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
11538 return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
11539 fold_convert_loc (loc, type,
11540 TREE_OPERAND (arg0, 0)));
11542 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
11543 __complex__ ( x, -y ). This is not the same for SNaNs or if
11544 signed zeros are involved. */
11545 if (!HONOR_SNANS (arg0)
11546 && !HONOR_SIGNED_ZEROS (arg0)
11547 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11549 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11550 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
11551 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
11552 bool arg0rz = false, arg0iz = false;
11553 if ((arg0r && (arg0rz = real_zerop (arg0r)))
11554 || (arg0i && (arg0iz = real_zerop (arg0i))))
11556 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
11557 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
11558 if (arg0rz && arg1i && real_zerop (arg1i))
11560 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11561 arg1r ? arg1r
11562 : build1 (REALPART_EXPR, rtype, arg1));
11563 tree ip = arg0i ? arg0i
11564 : build1 (IMAGPART_EXPR, rtype, arg0);
11565 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11567 else if (arg0iz && arg1r && real_zerop (arg1r))
11569 tree rp = arg0r ? arg0r
11570 : build1 (REALPART_EXPR, rtype, arg0);
11571 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
11572 arg1i ? arg1i
11573 : build1 (IMAGPART_EXPR, rtype, arg1));
11574 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
11579 /* A - B -> A + (-B) if B is easily negatable. */
11580 if (negate_expr_p (op1)
11581 && ! TYPE_OVERFLOW_SANITIZED (type)
11582 && ((FLOAT_TYPE_P (type)
11583 /* Avoid this transformation if B is a positive REAL_CST. */
11584 && (TREE_CODE (op1) != REAL_CST
11585 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
11586 || INTEGRAL_TYPE_P (type)))
11587 return fold_build2_loc (loc, PLUS_EXPR, type,
11588 fold_convert_loc (loc, type, arg0),
11589 negate_expr (op1));
11591 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
11592 one. Make sure the type is not saturating and has the signedness of
11593 the stripped operands, as fold_plusminus_mult_expr will re-associate.
11594 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
11595 if ((TREE_CODE (arg0) == MULT_EXPR
11596 || TREE_CODE (arg1) == MULT_EXPR)
11597 && !TYPE_SATURATING (type)
11598 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
11599 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
11600 && (!FLOAT_TYPE_P (type) || flag_associative_math))
11602 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
11603 if (tem)
11604 return tem;
11607 goto associate;
11609 case MULT_EXPR:
11610 if (! FLOAT_TYPE_P (type))
11612 /* Transform x * -C into -x * C if x is easily negatable. */
11613 if (TREE_CODE (op1) == INTEGER_CST
11614 && tree_int_cst_sgn (op1) == -1
11615 && negate_expr_p (op0)
11616 && negate_expr_p (op1)
11617 && (tem = negate_expr (op1)) != op1
11618 && ! TREE_OVERFLOW (tem))
11619 return fold_build2_loc (loc, MULT_EXPR, type,
11620 fold_convert_loc (loc, type,
11621 negate_expr (op0)), tem);
11623 strict_overflow_p = false;
11624 if (TREE_CODE (arg1) == INTEGER_CST
11625 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11626 &strict_overflow_p)) != 0)
11628 if (strict_overflow_p)
11629 fold_overflow_warning (("assuming signed overflow does not "
11630 "occur when simplifying "
11631 "multiplication"),
11632 WARN_STRICT_OVERFLOW_MISC);
11633 return fold_convert_loc (loc, type, tem);
11636 /* Optimize z * conj(z) for integer complex numbers. */
11637 if (TREE_CODE (arg0) == CONJ_EXPR
11638 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11639 return fold_mult_zconjz (loc, type, arg1);
11640 if (TREE_CODE (arg1) == CONJ_EXPR
11641 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11642 return fold_mult_zconjz (loc, type, arg0);
11644 else
11646 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11647 This is not the same for NaNs or if signed zeros are
11648 involved. */
11649 if (!HONOR_NANS (arg0)
11650 && !HONOR_SIGNED_ZEROS (arg0)
11651 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11652 && TREE_CODE (arg1) == COMPLEX_CST
11653 && real_zerop (TREE_REALPART (arg1)))
11655 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11656 if (real_onep (TREE_IMAGPART (arg1)))
11657 return
11658 fold_build2_loc (loc, COMPLEX_EXPR, type,
11659 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
11660 rtype, arg0)),
11661 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
11662 else if (real_minus_onep (TREE_IMAGPART (arg1)))
11663 return
11664 fold_build2_loc (loc, COMPLEX_EXPR, type,
11665 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
11666 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
11667 rtype, arg0)));
11670 /* Optimize z * conj(z) for floating point complex numbers.
11671 Guarded by flag_unsafe_math_optimizations as non-finite
11672 imaginary components don't produce scalar results. */
11673 if (flag_unsafe_math_optimizations
11674 && TREE_CODE (arg0) == CONJ_EXPR
11675 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11676 return fold_mult_zconjz (loc, type, arg1);
11677 if (flag_unsafe_math_optimizations
11678 && TREE_CODE (arg1) == CONJ_EXPR
11679 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11680 return fold_mult_zconjz (loc, type, arg0);
11682 goto associate;
11684 case BIT_IOR_EXPR:
11685 /* Canonicalize (X & C1) | C2. */
11686 if (TREE_CODE (arg0) == BIT_AND_EXPR
11687 && TREE_CODE (arg1) == INTEGER_CST
11688 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11690 int width = TYPE_PRECISION (type), w;
11691 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
11692 wide_int c2 = wi::to_wide (arg1);
11694 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11695 if ((c1 & c2) == c1)
11696 return omit_one_operand_loc (loc, type, arg1,
11697 TREE_OPERAND (arg0, 0));
11699 wide_int msk = wi::mask (width, false,
11700 TYPE_PRECISION (TREE_TYPE (arg1)));
11702 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11703 if (wi::bit_and_not (msk, c1 | c2) == 0)
11705 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11706 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11709 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11710 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11711 mode which allows further optimizations. */
11712 c1 &= msk;
11713 c2 &= msk;
11714 wide_int c3 = wi::bit_and_not (c1, c2);
11715 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
11717 wide_int mask = wi::mask (w, false,
11718 TYPE_PRECISION (type));
11719 if (((c1 | c2) & mask) == mask
11720 && wi::bit_and_not (c1, mask) == 0)
11722 c3 = mask;
11723 break;
11727 if (c3 != c1)
11729 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11730 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
11731 wide_int_to_tree (type, c3));
11732 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
11736 /* See if this can be simplified into a rotate first. If that
11737 is unsuccessful continue in the association code. */
11738 goto bit_rotate;
11740 case BIT_XOR_EXPR:
11741 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11742 if (TREE_CODE (arg0) == BIT_AND_EXPR
11743 && INTEGRAL_TYPE_P (type)
11744 && integer_onep (TREE_OPERAND (arg0, 1))
11745 && integer_onep (arg1))
11746 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11747 build_zero_cst (TREE_TYPE (arg0)));
11749 /* See if this can be simplified into a rotate first. If that
11750 is unsuccessful continue in the association code. */
11751 goto bit_rotate;
11753 case BIT_AND_EXPR:
11754 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11755 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11756 && INTEGRAL_TYPE_P (type)
11757 && integer_onep (TREE_OPERAND (arg0, 1))
11758 && integer_onep (arg1))
11760 tree tem2;
11761 tem = TREE_OPERAND (arg0, 0);
11762 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
11763 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
11764 tem, tem2);
11765 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
11766 build_zero_cst (TREE_TYPE (tem)));
11768 /* Fold ~X & 1 as (X & 1) == 0. */
11769 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11770 && INTEGRAL_TYPE_P (type)
11771 && integer_onep (arg1))
11773 tree tem2;
11774 tem = TREE_OPERAND (arg0, 0);
11775 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
11776 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
11777 tem, tem2);
11778 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
11779 build_zero_cst (TREE_TYPE (tem)));
11781 /* Fold !X & 1 as X == 0. */
11782 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11783 && integer_onep (arg1))
11785 tem = TREE_OPERAND (arg0, 0);
11786 return fold_build2_loc (loc, EQ_EXPR, type, tem,
11787 build_zero_cst (TREE_TYPE (tem)));
11790 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
11791 multiple of 1 << CST. */
11792 if (TREE_CODE (arg1) == INTEGER_CST)
11794 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11795 wide_int ncst1 = -cst1;
11796 if ((cst1 & ncst1) == ncst1
11797 && multiple_of_p (type, arg0,
11798 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
11799 return fold_convert_loc (loc, type, arg0);
11802 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
11803 bits from CST2. */
11804 if (TREE_CODE (arg1) == INTEGER_CST
11805 && TREE_CODE (arg0) == MULT_EXPR
11806 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11808 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
11809 wide_int masked
11810 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
11812 if (masked == 0)
11813 return omit_two_operands_loc (loc, type, build_zero_cst (type),
11814 arg0, arg1);
11815 else if (masked != warg1)
11817 /* Avoid the transform if arg1 is a mask of some
11818 mode which allows further optimizations. */
11819 int pop = wi::popcount (warg1);
11820 if (!(pop >= BITS_PER_UNIT
11821 && pow2p_hwi (pop)
11822 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
11823 return fold_build2_loc (loc, code, type, op0,
11824 wide_int_to_tree (type, masked));
11828 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11829 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11830 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11832 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11834 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
11835 if (mask == -1)
11836 return
11837 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11840 goto associate;
11842 case RDIV_EXPR:
11843 /* Don't touch a floating-point divide by zero unless the mode
11844 of the constant can represent infinity. */
11845 if (TREE_CODE (arg1) == REAL_CST
11846 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11847 && real_zerop (arg1))
11848 return NULL_TREE;
11850 /* (-A) / (-B) -> A / B */
11851 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11852 return fold_build2_loc (loc, RDIV_EXPR, type,
11853 TREE_OPERAND (arg0, 0),
11854 negate_expr (arg1));
11855 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11856 return fold_build2_loc (loc, RDIV_EXPR, type,
11857 negate_expr (arg0),
11858 TREE_OPERAND (arg1, 0));
11859 return NULL_TREE;
11861 case TRUNC_DIV_EXPR:
11862 /* Fall through */
11864 case FLOOR_DIV_EXPR:
11865 /* Simplify A / (B << N) where A and B are positive and B is
11866 a power of 2, to A >> (N + log2(B)). */
11867 strict_overflow_p = false;
11868 if (TREE_CODE (arg1) == LSHIFT_EXPR
11869 && (TYPE_UNSIGNED (type)
11870 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11872 tree sval = TREE_OPERAND (arg1, 0);
11873 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11875 tree sh_cnt = TREE_OPERAND (arg1, 1);
11876 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
11877 wi::exact_log2 (wi::to_wide (sval)));
11879 if (strict_overflow_p)
11880 fold_overflow_warning (("assuming signed overflow does not "
11881 "occur when simplifying A / (B << N)"),
11882 WARN_STRICT_OVERFLOW_MISC);
11884 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
11885 sh_cnt, pow2);
11886 return fold_build2_loc (loc, RSHIFT_EXPR, type,
11887 fold_convert_loc (loc, type, arg0), sh_cnt);
11891 /* Fall through */
11893 case ROUND_DIV_EXPR:
11894 case CEIL_DIV_EXPR:
11895 case EXACT_DIV_EXPR:
11896 if (integer_zerop (arg1))
11897 return NULL_TREE;
11899 /* Convert -A / -B to A / B when the type is signed and overflow is
11900 undefined. */
11901 if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11902 && TREE_CODE (op0) == NEGATE_EXPR
11903 && negate_expr_p (op1))
11905 if (ANY_INTEGRAL_TYPE_P (type))
11906 fold_overflow_warning (("assuming signed overflow does not occur "
11907 "when distributing negation across "
11908 "division"),
11909 WARN_STRICT_OVERFLOW_MISC);
11910 return fold_build2_loc (loc, code, type,
11911 fold_convert_loc (loc, type,
11912 TREE_OPERAND (arg0, 0)),
11913 negate_expr (op1));
11915 if ((!ANY_INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11916 && TREE_CODE (arg1) == NEGATE_EXPR
11917 && negate_expr_p (op0))
11919 if (ANY_INTEGRAL_TYPE_P (type))
11920 fold_overflow_warning (("assuming signed overflow does not occur "
11921 "when distributing negation across "
11922 "division"),
11923 WARN_STRICT_OVERFLOW_MISC);
11924 return fold_build2_loc (loc, code, type,
11925 negate_expr (op0),
11926 fold_convert_loc (loc, type,
11927 TREE_OPERAND (arg1, 0)));
11930 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11931 operation, EXACT_DIV_EXPR.
11933 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11934 At one time others generated faster code, it's not clear if they do
11935 after the last round to changes to the DIV code in expmed.cc. */
11936 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11937 && multiple_of_p (type, arg0, arg1))
11938 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
11939 fold_convert (type, arg0),
11940 fold_convert (type, arg1));
11942 strict_overflow_p = false;
11943 if (TREE_CODE (arg1) == INTEGER_CST
11944 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11945 &strict_overflow_p)) != 0)
11947 if (strict_overflow_p)
11948 fold_overflow_warning (("assuming signed overflow does not occur "
11949 "when simplifying division"),
11950 WARN_STRICT_OVERFLOW_MISC);
11951 return fold_convert_loc (loc, type, tem);
11954 return NULL_TREE;
11956 case CEIL_MOD_EXPR:
11957 case FLOOR_MOD_EXPR:
11958 case ROUND_MOD_EXPR:
11959 case TRUNC_MOD_EXPR:
11960 strict_overflow_p = false;
11961 if (TREE_CODE (arg1) == INTEGER_CST
11962 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11963 &strict_overflow_p)) != 0)
11965 if (strict_overflow_p)
11966 fold_overflow_warning (("assuming signed overflow does not occur "
11967 "when simplifying modulus"),
11968 WARN_STRICT_OVERFLOW_MISC);
11969 return fold_convert_loc (loc, type, tem);
11972 return NULL_TREE;
11974 case LROTATE_EXPR:
11975 case RROTATE_EXPR:
11976 case RSHIFT_EXPR:
11977 case LSHIFT_EXPR:
11978 /* Since negative shift count is not well-defined,
11979 don't try to compute it in the compiler. */
11980 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11981 return NULL_TREE;
11983 prec = element_precision (type);
11985 /* If we have a rotate of a bit operation with the rotate count and
11986 the second operand of the bit operation both constant,
11987 permute the two operations. */
11988 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11989 && (TREE_CODE (arg0) == BIT_AND_EXPR
11990 || TREE_CODE (arg0) == BIT_IOR_EXPR
11991 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11992 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11994 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11995 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11996 return fold_build2_loc (loc, TREE_CODE (arg0), type,
11997 fold_build2_loc (loc, code, type,
11998 arg00, arg1),
11999 fold_build2_loc (loc, code, type,
12000 arg01, arg1));
12003 /* Two consecutive rotates adding up to the some integer
12004 multiple of the precision of the type can be ignored. */
12005 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12006 && TREE_CODE (arg0) == RROTATE_EXPR
12007 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12008 && wi::umod_trunc (wi::to_wide (arg1)
12009 + wi::to_wide (TREE_OPERAND (arg0, 1)),
12010 prec) == 0)
12011 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12013 return NULL_TREE;
12015 case MIN_EXPR:
12016 case MAX_EXPR:
12017 goto associate;
12019 case TRUTH_ANDIF_EXPR:
12020 /* Note that the operands of this must be ints
12021 and their values must be 0 or 1.
12022 ("true" is a fixed value perhaps depending on the language.) */
12023 /* If first arg is constant zero, return it. */
12024 if (integer_zerop (arg0))
12025 return fold_convert_loc (loc, type, arg0);
12026 /* FALLTHRU */
12027 case TRUTH_AND_EXPR:
12028 /* If either arg is constant true, drop it. */
12029 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12030 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12031 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12032 /* Preserve sequence points. */
12033 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12034 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12035 /* If second arg is constant zero, result is zero, but first arg
12036 must be evaluated. */
12037 if (integer_zerop (arg1))
12038 return omit_one_operand_loc (loc, type, arg1, arg0);
12039 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12040 case will be handled here. */
12041 if (integer_zerop (arg0))
12042 return omit_one_operand_loc (loc, type, arg0, arg1);
12044 /* !X && X is always false. */
12045 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12046 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12047 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12048 /* X && !X is always false. */
12049 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12050 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12051 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12053 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12054 means A >= Y && A != MAX, but in this case we know that
12055 A < X <= MAX. */
12057 if (!TREE_SIDE_EFFECTS (arg0)
12058 && !TREE_SIDE_EFFECTS (arg1))
12060 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12061 if (tem && !operand_equal_p (tem, arg0, 0))
12062 return fold_convert (type,
12063 fold_build2_loc (loc, code, TREE_TYPE (arg1),
12064 tem, arg1));
12066 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12067 if (tem && !operand_equal_p (tem, arg1, 0))
12068 return fold_convert (type,
12069 fold_build2_loc (loc, code, TREE_TYPE (arg0),
12070 arg0, tem));
12073 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12074 != NULL_TREE)
12075 return tem;
12077 return NULL_TREE;
12079 case TRUTH_ORIF_EXPR:
12080 /* Note that the operands of this must be ints
12081 and their values must be 0 or true.
12082 ("true" is a fixed value perhaps depending on the language.) */
12083 /* If first arg is constant true, return it. */
12084 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12085 return fold_convert_loc (loc, type, arg0);
12086 /* FALLTHRU */
12087 case TRUTH_OR_EXPR:
12088 /* If either arg is constant zero, drop it. */
12089 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12090 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12091 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12092 /* Preserve sequence points. */
12093 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12094 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12095 /* If second arg is constant true, result is true, but we must
12096 evaluate first arg. */
12097 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12098 return omit_one_operand_loc (loc, type, arg1, arg0);
12099 /* Likewise for first arg, but note this only occurs here for
12100 TRUTH_OR_EXPR. */
12101 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12102 return omit_one_operand_loc (loc, type, arg0, arg1);
12104 /* !X || X is always true. */
12105 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12106 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12107 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12108 /* X || !X is always true. */
12109 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12110 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12111 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12113 /* (X && !Y) || (!X && Y) is X ^ Y */
12114 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
12115 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
12117 tree a0, a1, l0, l1, n0, n1;
12119 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
12120 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
12122 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12123 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
12125 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
12126 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
12128 if ((operand_equal_p (n0, a0, 0)
12129 && operand_equal_p (n1, a1, 0))
12130 || (operand_equal_p (n0, a1, 0)
12131 && operand_equal_p (n1, a0, 0)))
12132 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
12135 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
12136 != NULL_TREE)
12137 return tem;
12139 return NULL_TREE;
12141 case TRUTH_XOR_EXPR:
12142 /* If the second arg is constant zero, drop it. */
12143 if (integer_zerop (arg1))
12144 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12145 /* If the second arg is constant true, this is a logical inversion. */
12146 if (integer_onep (arg1))
12148 tem = invert_truthvalue_loc (loc, arg0);
12149 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12151 /* Identical arguments cancel to zero. */
12152 if (operand_equal_p (arg0, arg1, 0))
12153 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12155 /* !X ^ X is always true. */
12156 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12157 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12158 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12160 /* X ^ !X is always true. */
12161 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12162 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12163 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12165 return NULL_TREE;
12167 case EQ_EXPR:
12168 case NE_EXPR:
12169 STRIP_NOPS (arg0);
12170 STRIP_NOPS (arg1);
12172 tem = fold_comparison (loc, code, type, op0, op1);
12173 if (tem != NULL_TREE)
12174 return tem;
12176 /* bool_var != 1 becomes !bool_var. */
12177 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12178 && code == NE_EXPR)
12179 return fold_convert_loc (loc, type,
12180 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12181 TREE_TYPE (arg0), arg0));
12183 /* bool_var == 0 becomes !bool_var. */
12184 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12185 && code == EQ_EXPR)
12186 return fold_convert_loc (loc, type,
12187 fold_build1_loc (loc, TRUTH_NOT_EXPR,
12188 TREE_TYPE (arg0), arg0));
12190 /* !exp != 0 becomes !exp */
12191 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12192 && code == NE_EXPR)
12193 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12195 /* If this is an EQ or NE comparison with zero and ARG0 is
12196 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12197 two operations, but the latter can be done in one less insn
12198 on machines that have only two-operand insns or on which a
12199 constant cannot be the first operand. */
12200 if (TREE_CODE (arg0) == BIT_AND_EXPR
12201 && integer_zerop (arg1))
12203 tree arg00 = TREE_OPERAND (arg0, 0);
12204 tree arg01 = TREE_OPERAND (arg0, 1);
12205 if (TREE_CODE (arg00) == LSHIFT_EXPR
12206 && integer_onep (TREE_OPERAND (arg00, 0)))
12208 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12209 arg01, TREE_OPERAND (arg00, 1));
12210 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12211 build_one_cst (TREE_TYPE (arg0)));
12212 return fold_build2_loc (loc, code, type,
12213 fold_convert_loc (loc, TREE_TYPE (arg1),
12214 tem), arg1);
12216 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12217 && integer_onep (TREE_OPERAND (arg01, 0)))
12219 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12220 arg00, TREE_OPERAND (arg01, 1));
12221 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12222 build_one_cst (TREE_TYPE (arg0)));
12223 return fold_build2_loc (loc, code, type,
12224 fold_convert_loc (loc, TREE_TYPE (arg1),
12225 tem), arg1);
12229 /* If this is a comparison of a field, we may be able to simplify it. */
12230 if ((TREE_CODE (arg0) == COMPONENT_REF
12231 || TREE_CODE (arg0) == BIT_FIELD_REF)
12232 /* Handle the constant case even without -O
12233 to make sure the warnings are given. */
12234 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12236 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12237 if (t1)
12238 return t1;
12241 /* Optimize comparisons of strlen vs zero to a compare of the
12242 first character of the string vs zero. To wit,
12243 strlen(ptr) == 0 => *ptr == 0
12244 strlen(ptr) != 0 => *ptr != 0
12245 Other cases should reduce to one of these two (or a constant)
12246 due to the return value of strlen being unsigned. */
12247 if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
12249 tree fndecl = get_callee_fndecl (arg0);
12251 if (fndecl
12252 && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
12253 && call_expr_nargs (arg0) == 1
12254 && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
12255 == POINTER_TYPE))
12257 tree ptrtype
12258 = build_pointer_type (build_qualified_type (char_type_node,
12259 TYPE_QUAL_CONST));
12260 tree ptr = fold_convert_loc (loc, ptrtype,
12261 CALL_EXPR_ARG (arg0, 0));
12262 tree iref = build_fold_indirect_ref_loc (loc, ptr);
12263 return fold_build2_loc (loc, code, type, iref,
12264 build_int_cst (TREE_TYPE (iref), 0));
12268 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12269 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12270 if (TREE_CODE (arg0) == RSHIFT_EXPR
12271 && integer_zerop (arg1)
12272 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12274 tree arg00 = TREE_OPERAND (arg0, 0);
12275 tree arg01 = TREE_OPERAND (arg0, 1);
12276 tree itype = TREE_TYPE (arg00);
12277 if (wi::to_wide (arg01) == element_precision (itype) - 1)
12279 if (TYPE_UNSIGNED (itype))
12281 itype = signed_type_for (itype);
12282 arg00 = fold_convert_loc (loc, itype, arg00);
12284 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12285 type, arg00, build_zero_cst (itype));
12289 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12290 (X & C) == 0 when C is a single bit. */
12291 if (TREE_CODE (arg0) == BIT_AND_EXPR
12292 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12293 && integer_zerop (arg1)
12294 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12296 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12297 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12298 TREE_OPERAND (arg0, 1));
12299 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12300 type, tem,
12301 fold_convert_loc (loc, TREE_TYPE (arg0),
12302 arg1));
12305 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12306 constant C is a power of two, i.e. a single bit. */
12307 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12308 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12309 && integer_zerop (arg1)
12310 && integer_pow2p (TREE_OPERAND (arg0, 1))
12311 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12312 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12314 tree arg00 = TREE_OPERAND (arg0, 0);
12315 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12316 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12319 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12320 when is C is a power of two, i.e. a single bit. */
12321 if (TREE_CODE (arg0) == BIT_AND_EXPR
12322 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12323 && integer_zerop (arg1)
12324 && integer_pow2p (TREE_OPERAND (arg0, 1))
12325 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12326 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12328 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12329 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
12330 arg000, TREE_OPERAND (arg0, 1));
12331 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12332 tem, build_int_cst (TREE_TYPE (tem), 0));
12335 if (integer_zerop (arg1)
12336 && tree_expr_nonzero_p (arg0))
12338 tree res = constant_boolean_node (code==NE_EXPR, type);
12339 return omit_one_operand_loc (loc, type, res, arg0);
12342 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12343 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12345 tree arg00 = TREE_OPERAND (arg0, 0);
12346 tree arg01 = TREE_OPERAND (arg0, 1);
12347 tree arg10 = TREE_OPERAND (arg1, 0);
12348 tree arg11 = TREE_OPERAND (arg1, 1);
12349 tree itype = TREE_TYPE (arg0);
12351 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12352 operand_equal_p guarantees no side-effects so we don't need
12353 to use omit_one_operand on Z. */
12354 if (operand_equal_p (arg01, arg11, 0))
12355 return fold_build2_loc (loc, code, type, arg00,
12356 fold_convert_loc (loc, TREE_TYPE (arg00),
12357 arg10));
12358 if (operand_equal_p (arg01, arg10, 0))
12359 return fold_build2_loc (loc, code, type, arg00,
12360 fold_convert_loc (loc, TREE_TYPE (arg00),
12361 arg11));
12362 if (operand_equal_p (arg00, arg11, 0))
12363 return fold_build2_loc (loc, code, type, arg01,
12364 fold_convert_loc (loc, TREE_TYPE (arg01),
12365 arg10));
12366 if (operand_equal_p (arg00, arg10, 0))
12367 return fold_build2_loc (loc, code, type, arg01,
12368 fold_convert_loc (loc, TREE_TYPE (arg01),
12369 arg11));
12371 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12372 if (TREE_CODE (arg01) == INTEGER_CST
12373 && TREE_CODE (arg11) == INTEGER_CST)
12375 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
12376 fold_convert_loc (loc, itype, arg11));
12377 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
12378 return fold_build2_loc (loc, code, type, tem,
12379 fold_convert_loc (loc, itype, arg10));
12383 /* Attempt to simplify equality/inequality comparisons of complex
12384 values. Only lower the comparison if the result is known or
12385 can be simplified to a single scalar comparison. */
12386 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12387 || TREE_CODE (arg0) == COMPLEX_CST)
12388 && (TREE_CODE (arg1) == COMPLEX_EXPR
12389 || TREE_CODE (arg1) == COMPLEX_CST))
12391 tree real0, imag0, real1, imag1;
12392 tree rcond, icond;
12394 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12396 real0 = TREE_OPERAND (arg0, 0);
12397 imag0 = TREE_OPERAND (arg0, 1);
12399 else
12401 real0 = TREE_REALPART (arg0);
12402 imag0 = TREE_IMAGPART (arg0);
12405 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12407 real1 = TREE_OPERAND (arg1, 0);
12408 imag1 = TREE_OPERAND (arg1, 1);
12410 else
12412 real1 = TREE_REALPART (arg1);
12413 imag1 = TREE_IMAGPART (arg1);
12416 rcond = fold_binary_loc (loc, code, type, real0, real1);
12417 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12419 if (integer_zerop (rcond))
12421 if (code == EQ_EXPR)
12422 return omit_two_operands_loc (loc, type, boolean_false_node,
12423 imag0, imag1);
12424 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
12426 else
12428 if (code == NE_EXPR)
12429 return omit_two_operands_loc (loc, type, boolean_true_node,
12430 imag0, imag1);
12431 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
12435 icond = fold_binary_loc (loc, code, type, imag0, imag1);
12436 if (icond && TREE_CODE (icond) == INTEGER_CST)
12438 if (integer_zerop (icond))
12440 if (code == EQ_EXPR)
12441 return omit_two_operands_loc (loc, type, boolean_false_node,
12442 real0, real1);
12443 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
12445 else
12447 if (code == NE_EXPR)
12448 return omit_two_operands_loc (loc, type, boolean_true_node,
12449 real0, real1);
12450 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
12455 return NULL_TREE;
12457 case LT_EXPR:
12458 case GT_EXPR:
12459 case LE_EXPR:
12460 case GE_EXPR:
12461 tem = fold_comparison (loc, code, type, op0, op1);
12462 if (tem != NULL_TREE)
12463 return tem;
12465 /* Transform comparisons of the form X +- C CMP X. */
12466 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12467 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12468 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12469 && !HONOR_SNANS (arg0))
12471 tree arg01 = TREE_OPERAND (arg0, 1);
12472 enum tree_code code0 = TREE_CODE (arg0);
12473 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12475 /* (X - c) > X becomes false. */
12476 if (code == GT_EXPR
12477 && ((code0 == MINUS_EXPR && is_positive >= 0)
12478 || (code0 == PLUS_EXPR && is_positive <= 0)))
12479 return constant_boolean_node (0, type);
12481 /* Likewise (X + c) < X becomes false. */
12482 if (code == LT_EXPR
12483 && ((code0 == PLUS_EXPR && is_positive >= 0)
12484 || (code0 == MINUS_EXPR && is_positive <= 0)))
12485 return constant_boolean_node (0, type);
12487 /* Convert (X - c) <= X to true. */
12488 if (!HONOR_NANS (arg1)
12489 && code == LE_EXPR
12490 && ((code0 == MINUS_EXPR && is_positive >= 0)
12491 || (code0 == PLUS_EXPR && is_positive <= 0)))
12492 return constant_boolean_node (1, type);
12494 /* Convert (X + c) >= X to true. */
12495 if (!HONOR_NANS (arg1)
12496 && code == GE_EXPR
12497 && ((code0 == PLUS_EXPR && is_positive >= 0)
12498 || (code0 == MINUS_EXPR && is_positive <= 0)))
12499 return constant_boolean_node (1, type);
12502 /* If we are comparing an ABS_EXPR with a constant, we can
12503 convert all the cases into explicit comparisons, but they may
12504 well not be faster than doing the ABS and one comparison.
12505 But ABS (X) <= C is a range comparison, which becomes a subtraction
12506 and a comparison, and is probably faster. */
12507 if (code == LE_EXPR
12508 && TREE_CODE (arg1) == INTEGER_CST
12509 && TREE_CODE (arg0) == ABS_EXPR
12510 && ! TREE_SIDE_EFFECTS (arg0)
12511 && (tem = negate_expr (arg1)) != 0
12512 && TREE_CODE (tem) == INTEGER_CST
12513 && !TREE_OVERFLOW (tem))
12514 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
12515 build2 (GE_EXPR, type,
12516 TREE_OPERAND (arg0, 0), tem),
12517 build2 (LE_EXPR, type,
12518 TREE_OPERAND (arg0, 0), arg1));
12520 /* Convert ABS_EXPR<x> >= 0 to true. */
12521 strict_overflow_p = false;
12522 if (code == GE_EXPR
12523 && (integer_zerop (arg1)
12524 || (! HONOR_NANS (arg0)
12525 && real_zerop (arg1)))
12526 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12528 if (strict_overflow_p)
12529 fold_overflow_warning (("assuming signed overflow does not occur "
12530 "when simplifying comparison of "
12531 "absolute value and zero"),
12532 WARN_STRICT_OVERFLOW_CONDITIONAL);
12533 return omit_one_operand_loc (loc, type,
12534 constant_boolean_node (true, type),
12535 arg0);
12538 /* Convert ABS_EXPR<x> < 0 to false. */
12539 strict_overflow_p = false;
12540 if (code == LT_EXPR
12541 && (integer_zerop (arg1) || real_zerop (arg1))
12542 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12544 if (strict_overflow_p)
12545 fold_overflow_warning (("assuming signed overflow does not occur "
12546 "when simplifying comparison of "
12547 "absolute value and zero"),
12548 WARN_STRICT_OVERFLOW_CONDITIONAL);
12549 return omit_one_operand_loc (loc, type,
12550 constant_boolean_node (false, type),
12551 arg0);
12554 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12555 and similarly for >= into !=. */
12556 if ((code == LT_EXPR || code == GE_EXPR)
12557 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12558 && TREE_CODE (arg1) == LSHIFT_EXPR
12559 && integer_onep (TREE_OPERAND (arg1, 0)))
12560 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12561 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12562 TREE_OPERAND (arg1, 1)),
12563 build_zero_cst (TREE_TYPE (arg0)));
12565 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
12566 otherwise Y might be >= # of bits in X's type and thus e.g.
12567 (unsigned char) (1 << Y) for Y 15 might be 0.
12568 If the cast is widening, then 1 << Y should have unsigned type,
12569 otherwise if Y is number of bits in the signed shift type minus 1,
12570 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
12571 31 might be 0xffffffff80000000. */
12572 if ((code == LT_EXPR || code == GE_EXPR)
12573 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12574 || VECTOR_INTEGER_TYPE_P (TREE_TYPE (arg0)))
12575 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12576 && CONVERT_EXPR_P (arg1)
12577 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12578 && (element_precision (TREE_TYPE (arg1))
12579 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
12580 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
12581 || (element_precision (TREE_TYPE (arg1))
12582 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
12583 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12585 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12586 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
12587 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12588 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
12589 build_zero_cst (TREE_TYPE (arg0)));
12592 return NULL_TREE;
12594 case UNORDERED_EXPR:
12595 case ORDERED_EXPR:
12596 case UNLT_EXPR:
12597 case UNLE_EXPR:
12598 case UNGT_EXPR:
12599 case UNGE_EXPR:
12600 case UNEQ_EXPR:
12601 case LTGT_EXPR:
12602 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12604 tree targ0 = strip_float_extensions (arg0);
12605 tree targ1 = strip_float_extensions (arg1);
12606 tree newtype = TREE_TYPE (targ0);
12608 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
12609 newtype = TREE_TYPE (targ1);
12611 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
12612 return fold_build2_loc (loc, code, type,
12613 fold_convert_loc (loc, newtype, targ0),
12614 fold_convert_loc (loc, newtype, targ1));
12617 return NULL_TREE;
12619 case COMPOUND_EXPR:
12620 /* When pedantic, a compound expression can be neither an lvalue
12621 nor an integer constant expression. */
12622 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12623 return NULL_TREE;
12624 /* Don't let (0, 0) be null pointer constant. */
12625 tem = integer_zerop (arg1) ? build1_loc (loc, NOP_EXPR, type, arg1)
12626 : fold_convert_loc (loc, type, arg1);
12627 return tem;
12629 default:
12630 return NULL_TREE;
12631 } /* switch (code) */
12634 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
12635 ((A & N) + B) & M -> (A + B) & M
12636 Similarly if (N & M) == 0,
12637 ((A | N) + B) & M -> (A + B) & M
12638 and for - instead of + (or unary - instead of +)
12639 and/or ^ instead of |.
12640 If B is constant and (B & M) == 0, fold into A & M.
12642 This function is a helper for match.pd patterns. Return non-NULL
12643 type in which the simplified operation should be performed only
12644 if any optimization is possible.
12646 ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
12647 then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
12648 Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
12649 +/-. */
12650 tree
12651 fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
12652 tree arg00, enum tree_code code00, tree arg000, tree arg001,
12653 tree arg01, enum tree_code code01, tree arg010, tree arg011,
12654 tree *pmop)
12656 gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
12657 gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
12658 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
12659 if (~cst1 == 0
12660 || (cst1 & (cst1 + 1)) != 0
12661 || !INTEGRAL_TYPE_P (type)
12662 || (!TYPE_OVERFLOW_WRAPS (type)
12663 && TREE_CODE (type) != INTEGER_TYPE)
12664 || (wi::max_value (type) & cst1) != cst1)
12665 return NULL_TREE;
12667 enum tree_code codes[2] = { code00, code01 };
12668 tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
12669 int which = 0;
12670 wide_int cst0;
12672 /* Now we know that arg0 is (C + D) or (C - D) or -C and
12673 arg1 (M) is == (1LL << cst) - 1.
12674 Store C into PMOP[0] and D into PMOP[1]. */
12675 pmop[0] = arg00;
12676 pmop[1] = arg01;
12677 which = code != NEGATE_EXPR;
12679 for (; which >= 0; which--)
12680 switch (codes[which])
12682 case BIT_AND_EXPR:
12683 case BIT_IOR_EXPR:
12684 case BIT_XOR_EXPR:
12685 gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
12686 cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
12687 if (codes[which] == BIT_AND_EXPR)
12689 if (cst0 != cst1)
12690 break;
12692 else if (cst0 != 0)
12693 break;
12694 /* If C or D is of the form (A & N) where
12695 (N & M) == M, or of the form (A | N) or
12696 (A ^ N) where (N & M) == 0, replace it with A. */
12697 pmop[which] = arg0xx[2 * which];
12698 break;
12699 case ERROR_MARK:
12700 if (TREE_CODE (pmop[which]) != INTEGER_CST)
12701 break;
12702 /* If C or D is a N where (N & M) == 0, it can be
12703 omitted (replaced with 0). */
12704 if ((code == PLUS_EXPR
12705 || (code == MINUS_EXPR && which == 0))
12706 && (cst1 & wi::to_wide (pmop[which])) == 0)
12707 pmop[which] = build_int_cst (type, 0);
12708 /* Similarly, with C - N where (-N & M) == 0. */
12709 if (code == MINUS_EXPR
12710 && which == 1
12711 && (cst1 & -wi::to_wide (pmop[which])) == 0)
12712 pmop[which] = build_int_cst (type, 0);
12713 break;
12714 default:
12715 gcc_unreachable ();
12718 /* Only build anything new if we optimized one or both arguments above. */
12719 if (pmop[0] == arg00 && pmop[1] == arg01)
12720 return NULL_TREE;
12722 if (TYPE_OVERFLOW_WRAPS (type))
12723 return type;
12724 else
12725 return unsigned_type_for (type);
12728 /* Used by contains_label_[p1]. */
12730 struct contains_label_data
12732 hash_set<tree> *pset;
12733 bool inside_switch_p;
12736 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
12737 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
12738 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
12740 static tree
12741 contains_label_1 (tree *tp, int *walk_subtrees, void *data)
12743 contains_label_data *d = (contains_label_data *) data;
12744 switch (TREE_CODE (*tp))
12746 case LABEL_EXPR:
12747 return *tp;
12749 case CASE_LABEL_EXPR:
12750 if (!d->inside_switch_p)
12751 return *tp;
12752 return NULL_TREE;
12754 case SWITCH_EXPR:
12755 if (!d->inside_switch_p)
12757 if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
12758 return *tp;
12759 d->inside_switch_p = true;
12760 if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
12761 return *tp;
12762 d->inside_switch_p = false;
12763 *walk_subtrees = 0;
12765 return NULL_TREE;
12767 case GOTO_EXPR:
12768 *walk_subtrees = 0;
12769 return NULL_TREE;
12771 default:
12772 return NULL_TREE;
12776 /* Return whether the sub-tree ST contains a label which is accessible from
12777 outside the sub-tree. */
12779 static bool
12780 contains_label_p (tree st)
12782 hash_set<tree> pset;
12783 contains_label_data data = { &pset, false };
12784 return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
12787 /* Fold a ternary expression of code CODE and type TYPE with operands
12788 OP0, OP1, and OP2. Return the folded expression if folding is
12789 successful. Otherwise, return NULL_TREE. */
12791 tree
12792 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
12793 tree op0, tree op1, tree op2)
12795 tree tem;
12796 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
12797 enum tree_code_class kind = TREE_CODE_CLASS (code);
12799 gcc_assert (IS_EXPR_CODE_CLASS (kind)
12800 && TREE_CODE_LENGTH (code) == 3);
12802 /* If this is a commutative operation, and OP0 is a constant, move it
12803 to OP1 to reduce the number of tests below. */
12804 if (commutative_ternary_tree_code (code)
12805 && tree_swap_operands_p (op0, op1))
12806 return fold_build3_loc (loc, code, type, op1, op0, op2);
12808 tem = generic_simplify (loc, code, type, op0, op1, op2);
12809 if (tem)
12810 return tem;
12812 /* Strip any conversions that don't change the mode. This is safe
12813 for every expression, except for a comparison expression because
12814 its signedness is derived from its operands. So, in the latter
12815 case, only strip conversions that don't change the signedness.
12817 Note that this is done as an internal manipulation within the
12818 constant folder, in order to find the simplest representation of
12819 the arguments so that their form can be studied. In any cases,
12820 the appropriate type conversions should be put back in the tree
12821 that will get out of the constant folder. */
12822 if (op0)
12824 arg0 = op0;
12825 STRIP_NOPS (arg0);
12828 if (op1)
12830 arg1 = op1;
12831 STRIP_NOPS (arg1);
12834 if (op2)
12836 arg2 = op2;
12837 STRIP_NOPS (arg2);
12840 switch (code)
12842 case COMPONENT_REF:
12843 if (TREE_CODE (arg0) == CONSTRUCTOR
12844 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12846 unsigned HOST_WIDE_INT idx;
12847 tree field, value;
12848 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12849 if (field == arg1)
12850 return value;
12852 return NULL_TREE;
12854 case COND_EXPR:
12855 case VEC_COND_EXPR:
12856 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12857 so all simple results must be passed through pedantic_non_lvalue. */
12858 if (TREE_CODE (arg0) == INTEGER_CST)
12860 tree unused_op = integer_zerop (arg0) ? op1 : op2;
12861 tem = integer_zerop (arg0) ? op2 : op1;
12862 /* Only optimize constant conditions when the selected branch
12863 has the same type as the COND_EXPR. This avoids optimizing
12864 away "c ? x : throw", where the throw has a void type.
12865 Avoid throwing away that operand which contains label. */
12866 if ((!TREE_SIDE_EFFECTS (unused_op)
12867 || !contains_label_p (unused_op))
12868 && (! VOID_TYPE_P (TREE_TYPE (tem))
12869 || VOID_TYPE_P (type)))
12870 return protected_set_expr_location_unshare (tem, loc);
12871 return NULL_TREE;
12873 else if (TREE_CODE (arg0) == VECTOR_CST)
12875 unsigned HOST_WIDE_INT nelts;
12876 if ((TREE_CODE (arg1) == VECTOR_CST
12877 || TREE_CODE (arg1) == CONSTRUCTOR)
12878 && (TREE_CODE (arg2) == VECTOR_CST
12879 || TREE_CODE (arg2) == CONSTRUCTOR)
12880 && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
12882 vec_perm_builder sel (nelts, nelts, 1);
12883 for (unsigned int i = 0; i < nelts; i++)
12885 tree val = VECTOR_CST_ELT (arg0, i);
12886 if (integer_all_onesp (val))
12887 sel.quick_push (i);
12888 else if (integer_zerop (val))
12889 sel.quick_push (nelts + i);
12890 else /* Currently unreachable. */
12891 return NULL_TREE;
12893 vec_perm_indices indices (sel, 2, nelts);
12894 tree t = fold_vec_perm (type, arg1, arg2, indices);
12895 if (t != NULL_TREE)
12896 return t;
12900 /* If we have A op B ? A : C, we may be able to convert this to a
12901 simpler expression, depending on the operation and the values
12902 of B and C. Signed zeros prevent all of these transformations,
12903 for reasons given above each one.
12905 Also try swapping the arguments and inverting the conditional. */
12906 if (COMPARISON_CLASS_P (arg0)
12907 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
12908 && !HONOR_SIGNED_ZEROS (op1))
12910 tem = fold_cond_expr_with_comparison (loc, type, TREE_CODE (arg0),
12911 TREE_OPERAND (arg0, 0),
12912 TREE_OPERAND (arg0, 1),
12913 op1, op2);
12914 if (tem)
12915 return tem;
12918 if (COMPARISON_CLASS_P (arg0)
12919 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
12920 && !HONOR_SIGNED_ZEROS (op2))
12922 enum tree_code comp_code = TREE_CODE (arg0);
12923 tree arg00 = TREE_OPERAND (arg0, 0);
12924 tree arg01 = TREE_OPERAND (arg0, 1);
12925 comp_code = invert_tree_comparison (comp_code, HONOR_NANS (arg00));
12926 if (comp_code != ERROR_MARK)
12927 tem = fold_cond_expr_with_comparison (loc, type, comp_code,
12928 arg00,
12929 arg01,
12930 op2, op1);
12931 if (tem)
12932 return tem;
12935 /* If the second operand is simpler than the third, swap them
12936 since that produces better jump optimization results. */
12937 if (truth_value_p (TREE_CODE (arg0))
12938 && tree_swap_operands_p (op1, op2))
12940 location_t loc0 = expr_location_or (arg0, loc);
12941 /* See if this can be inverted. If it can't, possibly because
12942 it was a floating-point inequality comparison, don't do
12943 anything. */
12944 tem = fold_invert_truthvalue (loc0, arg0);
12945 if (tem)
12946 return fold_build3_loc (loc, code, type, tem, op2, op1);
12949 /* Convert A ? 1 : 0 to simply A. */
12950 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
12951 : (integer_onep (op1)
12952 && !VECTOR_TYPE_P (type)))
12953 && integer_zerop (op2)
12954 /* If we try to convert OP0 to our type, the
12955 call to fold will try to move the conversion inside
12956 a COND, which will recurse. In that case, the COND_EXPR
12957 is probably the best choice, so leave it alone. */
12958 && type == TREE_TYPE (arg0))
12959 return protected_set_expr_location_unshare (arg0, loc);
12961 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12962 over COND_EXPR in cases such as floating point comparisons. */
12963 if (integer_zerop (op1)
12964 && code == COND_EXPR
12965 && integer_onep (op2)
12966 && !VECTOR_TYPE_P (type)
12967 && truth_value_p (TREE_CODE (arg0)))
12968 return fold_convert_loc (loc, type,
12969 invert_truthvalue_loc (loc, arg0));
12971 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12972 if (TREE_CODE (arg0) == LT_EXPR
12973 && integer_zerop (TREE_OPERAND (arg0, 1))
12974 && integer_zerop (op2)
12975 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12977 /* sign_bit_p looks through both zero and sign extensions,
12978 but for this optimization only sign extensions are
12979 usable. */
12980 tree tem2 = TREE_OPERAND (arg0, 0);
12981 while (tem != tem2)
12983 if (TREE_CODE (tem2) != NOP_EXPR
12984 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
12986 tem = NULL_TREE;
12987 break;
12989 tem2 = TREE_OPERAND (tem2, 0);
12991 /* sign_bit_p only checks ARG1 bits within A's precision.
12992 If <sign bit of A> has wider type than A, bits outside
12993 of A's precision in <sign bit of A> need to be checked.
12994 If they are all 0, this optimization needs to be done
12995 in unsigned A's type, if they are all 1 in signed A's type,
12996 otherwise this can't be done. */
12997 if (tem
12998 && TYPE_PRECISION (TREE_TYPE (tem))
12999 < TYPE_PRECISION (TREE_TYPE (arg1))
13000 && TYPE_PRECISION (TREE_TYPE (tem))
13001 < TYPE_PRECISION (type))
13003 int inner_width, outer_width;
13004 tree tem_type;
13006 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13007 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13008 if (outer_width > TYPE_PRECISION (type))
13009 outer_width = TYPE_PRECISION (type);
13011 wide_int mask = wi::shifted_mask
13012 (inner_width, outer_width - inner_width, false,
13013 TYPE_PRECISION (TREE_TYPE (arg1)));
13015 wide_int common = mask & wi::to_wide (arg1);
13016 if (common == mask)
13018 tem_type = signed_type_for (TREE_TYPE (tem));
13019 tem = fold_convert_loc (loc, tem_type, tem);
13021 else if (common == 0)
13023 tem_type = unsigned_type_for (TREE_TYPE (tem));
13024 tem = fold_convert_loc (loc, tem_type, tem);
13026 else
13027 tem = NULL;
13030 if (tem)
13031 return
13032 fold_convert_loc (loc, type,
13033 fold_build2_loc (loc, BIT_AND_EXPR,
13034 TREE_TYPE (tem), tem,
13035 fold_convert_loc (loc,
13036 TREE_TYPE (tem),
13037 arg1)));
13040 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13041 already handled above. */
13042 if (TREE_CODE (arg0) == BIT_AND_EXPR
13043 && integer_onep (TREE_OPERAND (arg0, 1))
13044 && integer_zerop (op2)
13045 && integer_pow2p (arg1))
13047 tree tem = TREE_OPERAND (arg0, 0);
13048 STRIP_NOPS (tem);
13049 if (TREE_CODE (tem) == RSHIFT_EXPR
13050 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
13051 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
13052 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
13053 return fold_build2_loc (loc, BIT_AND_EXPR, type,
13054 fold_convert_loc (loc, type,
13055 TREE_OPERAND (tem, 0)),
13056 op1);
13059 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13060 is probably obsolete because the first operand should be a
13061 truth value (that's why we have the two cases above), but let's
13062 leave it in until we can confirm this for all front-ends. */
13063 if (integer_zerop (op2)
13064 && TREE_CODE (arg0) == NE_EXPR
13065 && integer_zerop (TREE_OPERAND (arg0, 1))
13066 && integer_pow2p (arg1)
13067 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13068 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13069 arg1, OEP_ONLY_CONST)
13070 /* operand_equal_p compares just value, not precision, so e.g.
13071 arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
13072 second operand 32-bit -128, which is not a power of two (or vice
13073 versa. */
13074 && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
13075 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
13077 /* Disable the transformations below for vectors, since
13078 fold_binary_op_with_conditional_arg may undo them immediately,
13079 yielding an infinite loop. */
13080 if (code == VEC_COND_EXPR)
13081 return NULL_TREE;
13083 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13084 if (integer_zerop (op2)
13085 && truth_value_p (TREE_CODE (arg0))
13086 && truth_value_p (TREE_CODE (arg1))
13087 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13088 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
13089 : TRUTH_ANDIF_EXPR,
13090 type, fold_convert_loc (loc, type, arg0), op1);
13092 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13093 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
13094 && truth_value_p (TREE_CODE (arg0))
13095 && truth_value_p (TREE_CODE (arg1))
13096 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13098 location_t loc0 = expr_location_or (arg0, loc);
13099 /* Only perform transformation if ARG0 is easily inverted. */
13100 tem = fold_invert_truthvalue (loc0, arg0);
13101 if (tem)
13102 return fold_build2_loc (loc, code == VEC_COND_EXPR
13103 ? BIT_IOR_EXPR
13104 : TRUTH_ORIF_EXPR,
13105 type, fold_convert_loc (loc, type, tem),
13106 op1);
13109 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13110 if (integer_zerop (arg1)
13111 && truth_value_p (TREE_CODE (arg0))
13112 && truth_value_p (TREE_CODE (op2))
13113 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13115 location_t loc0 = expr_location_or (arg0, loc);
13116 /* Only perform transformation if ARG0 is easily inverted. */
13117 tem = fold_invert_truthvalue (loc0, arg0);
13118 if (tem)
13119 return fold_build2_loc (loc, code == VEC_COND_EXPR
13120 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
13121 type, fold_convert_loc (loc, type, tem),
13122 op2);
13125 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13126 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
13127 && truth_value_p (TREE_CODE (arg0))
13128 && truth_value_p (TREE_CODE (op2))
13129 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
13130 return fold_build2_loc (loc, code == VEC_COND_EXPR
13131 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
13132 type, fold_convert_loc (loc, type, arg0), op2);
13134 return NULL_TREE;
13136 case CALL_EXPR:
13137 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13138 of fold_ternary on them. */
13139 gcc_unreachable ();
13141 case BIT_FIELD_REF:
13142 if (TREE_CODE (arg0) == VECTOR_CST
13143 && (type == TREE_TYPE (TREE_TYPE (arg0))
13144 || (VECTOR_TYPE_P (type)
13145 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
13146 && tree_fits_uhwi_p (op1)
13147 && tree_fits_uhwi_p (op2))
13149 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
13150 unsigned HOST_WIDE_INT width
13151 = (TREE_CODE (eltype) == BOOLEAN_TYPE
13152 ? TYPE_PRECISION (eltype) : tree_to_uhwi (TYPE_SIZE (eltype)));
13153 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
13154 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
13156 if (n != 0
13157 && (idx % width) == 0
13158 && (n % width) == 0
13159 && known_le ((idx + n) / width,
13160 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
13162 idx = idx / width;
13163 n = n / width;
13165 if (TREE_CODE (arg0) == VECTOR_CST)
13167 if (n == 1)
13169 tem = VECTOR_CST_ELT (arg0, idx);
13170 if (VECTOR_TYPE_P (type))
13171 tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
13172 return tem;
13175 tree_vector_builder vals (type, n, 1);
13176 for (unsigned i = 0; i < n; ++i)
13177 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
13178 return vals.build ();
13183 /* On constants we can use native encode/interpret to constant
13184 fold (nearly) all BIT_FIELD_REFs. */
13185 if (CONSTANT_CLASS_P (arg0)
13186 && can_native_interpret_type_p (type)
13187 && BITS_PER_UNIT == 8
13188 && tree_fits_uhwi_p (op1)
13189 && tree_fits_uhwi_p (op2))
13191 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13192 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
13193 /* Limit us to a reasonable amount of work. To relax the
13194 other limitations we need bit-shifting of the buffer
13195 and rounding up the size. */
13196 if (bitpos % BITS_PER_UNIT == 0
13197 && bitsize % BITS_PER_UNIT == 0
13198 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
13200 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
13201 unsigned HOST_WIDE_INT len
13202 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
13203 bitpos / BITS_PER_UNIT);
13204 if (len > 0
13205 && len * BITS_PER_UNIT >= bitsize)
13207 tree v = native_interpret_expr (type, b,
13208 bitsize / BITS_PER_UNIT);
13209 if (v)
13210 return v;
13215 return NULL_TREE;
13217 case VEC_PERM_EXPR:
13218 /* Perform constant folding of BIT_INSERT_EXPR. */
13219 if (TREE_CODE (arg2) == VECTOR_CST
13220 && TREE_CODE (op0) == VECTOR_CST
13221 && TREE_CODE (op1) == VECTOR_CST)
13223 /* Build a vector of integers from the tree mask. */
13224 vec_perm_builder builder;
13225 if (!tree_to_vec_perm_builder (&builder, arg2))
13226 return NULL_TREE;
13228 /* Create a vec_perm_indices for the integer vector. */
13229 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
13230 bool single_arg = (op0 == op1);
13231 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
13232 return fold_vec_perm (type, op0, op1, sel);
13234 return NULL_TREE;
13236 case BIT_INSERT_EXPR:
13237 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
13238 if (TREE_CODE (arg0) == INTEGER_CST
13239 && TREE_CODE (arg1) == INTEGER_CST)
13241 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13242 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
13243 wide_int tem = (wi::to_wide (arg0)
13244 & wi::shifted_mask (bitpos, bitsize, true,
13245 TYPE_PRECISION (type)));
13246 wide_int tem2
13247 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
13248 bitsize), bitpos);
13249 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
13251 else if (TREE_CODE (arg0) == VECTOR_CST
13252 && CONSTANT_CLASS_P (arg1)
13253 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
13254 TREE_TYPE (arg1)))
13256 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
13257 unsigned HOST_WIDE_INT elsize
13258 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
13259 if (bitpos % elsize == 0)
13261 unsigned k = bitpos / elsize;
13262 unsigned HOST_WIDE_INT nelts;
13263 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
13264 return arg0;
13265 else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
13267 tree_vector_builder elts (type, nelts, 1);
13268 elts.quick_grow (nelts);
13269 for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
13270 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
13271 return elts.build ();
13275 return NULL_TREE;
13277 default:
13278 return NULL_TREE;
13279 } /* switch (code) */
13282 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
13283 of an array (or vector). *CTOR_IDX if non-NULL is updated with the
13284 constructor element index of the value returned. If the element is
13285 not found NULL_TREE is returned and *CTOR_IDX is updated to
13286 the index of the element after the ACCESS_INDEX position (which
13287 may be outside of the CTOR array). */
13289 tree
13290 get_array_ctor_element_at_index (tree ctor, offset_int access_index,
13291 unsigned *ctor_idx)
13293 tree index_type = NULL_TREE;
13294 signop index_sgn = UNSIGNED;
13295 offset_int low_bound = 0;
13297 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
13299 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
13300 if (domain_type && TYPE_MIN_VALUE (domain_type))
13302 /* Static constructors for variably sized objects makes no sense. */
13303 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
13304 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
13305 /* ??? When it is obvious that the range is signed, treat it so. */
13306 if (TYPE_UNSIGNED (index_type)
13307 && TYPE_MAX_VALUE (domain_type)
13308 && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
13309 TYPE_MIN_VALUE (domain_type)))
13311 index_sgn = SIGNED;
13312 low_bound
13313 = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
13314 SIGNED);
13316 else
13318 index_sgn = TYPE_SIGN (index_type);
13319 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
13324 if (index_type)
13325 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
13326 index_sgn);
13328 offset_int index = low_bound;
13329 if (index_type)
13330 index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13332 offset_int max_index = index;
13333 unsigned cnt;
13334 tree cfield, cval;
13335 bool first_p = true;
13337 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
13339 /* Array constructor might explicitly set index, or specify a range,
13340 or leave index NULL meaning that it is next index after previous
13341 one. */
13342 if (cfield)
13344 if (TREE_CODE (cfield) == INTEGER_CST)
13345 max_index = index
13346 = offset_int::from (wi::to_wide (cfield), index_sgn);
13347 else
13349 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
13350 index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
13351 index_sgn);
13352 max_index
13353 = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
13354 index_sgn);
13355 gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
13358 else if (!first_p)
13360 index = max_index + 1;
13361 if (index_type)
13362 index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
13363 gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
13364 max_index = index;
13366 else
13367 first_p = false;
13369 /* Do we have match? */
13370 if (wi::cmp (access_index, index, index_sgn) >= 0)
13372 if (wi::cmp (access_index, max_index, index_sgn) <= 0)
13374 if (ctor_idx)
13375 *ctor_idx = cnt;
13376 return cval;
13379 else if (in_gimple_form)
13380 /* We're past the element we search for. Note during parsing
13381 the elements might not be sorted.
13382 ??? We should use a binary search and a flag on the
13383 CONSTRUCTOR as to whether elements are sorted in declaration
13384 order. */
13385 break;
13387 if (ctor_idx)
13388 *ctor_idx = cnt;
13389 return NULL_TREE;
13392 /* Perform constant folding and related simplification of EXPR.
13393 The related simplifications include x*1 => x, x*0 => 0, etc.,
13394 and application of the associative law.
13395 NOP_EXPR conversions may be removed freely (as long as we
13396 are careful not to change the type of the overall expression).
13397 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13398 but we can constant-fold them if they have constant operands. */
13400 #ifdef ENABLE_FOLD_CHECKING
13401 # define fold(x) fold_1 (x)
13402 static tree fold_1 (tree);
13403 static
13404 #endif
13405 tree
13406 fold (tree expr)
13408 const tree t = expr;
13409 enum tree_code code = TREE_CODE (t);
13410 enum tree_code_class kind = TREE_CODE_CLASS (code);
13411 tree tem;
13412 location_t loc = EXPR_LOCATION (expr);
13414 /* Return right away if a constant. */
13415 if (kind == tcc_constant)
13416 return t;
13418 /* CALL_EXPR-like objects with variable numbers of operands are
13419 treated specially. */
13420 if (kind == tcc_vl_exp)
13422 if (code == CALL_EXPR)
13424 tem = fold_call_expr (loc, expr, false);
13425 return tem ? tem : expr;
13427 return expr;
13430 if (IS_EXPR_CODE_CLASS (kind))
13432 tree type = TREE_TYPE (t);
13433 tree op0, op1, op2;
13435 switch (TREE_CODE_LENGTH (code))
13437 case 1:
13438 op0 = TREE_OPERAND (t, 0);
13439 tem = fold_unary_loc (loc, code, type, op0);
13440 return tem ? tem : expr;
13441 case 2:
13442 op0 = TREE_OPERAND (t, 0);
13443 op1 = TREE_OPERAND (t, 1);
13444 tem = fold_binary_loc (loc, code, type, op0, op1);
13445 return tem ? tem : expr;
13446 case 3:
13447 op0 = TREE_OPERAND (t, 0);
13448 op1 = TREE_OPERAND (t, 1);
13449 op2 = TREE_OPERAND (t, 2);
13450 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13451 return tem ? tem : expr;
13452 default:
13453 break;
13457 switch (code)
13459 case ARRAY_REF:
13461 tree op0 = TREE_OPERAND (t, 0);
13462 tree op1 = TREE_OPERAND (t, 1);
13464 if (TREE_CODE (op1) == INTEGER_CST
13465 && TREE_CODE (op0) == CONSTRUCTOR
13466 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13468 tree val = get_array_ctor_element_at_index (op0,
13469 wi::to_offset (op1));
13470 if (val)
13471 return val;
13474 return t;
13477 /* Return a VECTOR_CST if possible. */
13478 case CONSTRUCTOR:
13480 tree type = TREE_TYPE (t);
13481 if (TREE_CODE (type) != VECTOR_TYPE)
13482 return t;
13484 unsigned i;
13485 tree val;
13486 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
13487 if (! CONSTANT_CLASS_P (val))
13488 return t;
13490 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
13493 case CONST_DECL:
13494 return fold (DECL_INITIAL (t));
13496 default:
13497 return t;
13498 } /* switch (code) */
13501 #ifdef ENABLE_FOLD_CHECKING
13502 #undef fold
13504 static void fold_checksum_tree (const_tree, struct md5_ctx *,
13505 hash_table<nofree_ptr_hash<const tree_node> > *);
13506 static void fold_check_failed (const_tree, const_tree);
13507 void print_fold_checksum (const_tree);
13509 /* When --enable-checking=fold, compute a digest of expr before
13510 and after actual fold call to see if fold did not accidentally
13511 change original expr. */
13513 tree
13514 fold (tree expr)
13516 tree ret;
13517 struct md5_ctx ctx;
13518 unsigned char checksum_before[16], checksum_after[16];
13519 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13521 md5_init_ctx (&ctx);
13522 fold_checksum_tree (expr, &ctx, &ht);
13523 md5_finish_ctx (&ctx, checksum_before);
13524 ht.empty ();
13526 ret = fold_1 (expr);
13528 md5_init_ctx (&ctx);
13529 fold_checksum_tree (expr, &ctx, &ht);
13530 md5_finish_ctx (&ctx, checksum_after);
13532 if (memcmp (checksum_before, checksum_after, 16))
13533 fold_check_failed (expr, ret);
13535 return ret;
13538 void
13539 print_fold_checksum (const_tree expr)
13541 struct md5_ctx ctx;
13542 unsigned char checksum[16], cnt;
13543 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13545 md5_init_ctx (&ctx);
13546 fold_checksum_tree (expr, &ctx, &ht);
13547 md5_finish_ctx (&ctx, checksum);
13548 for (cnt = 0; cnt < 16; ++cnt)
13549 fprintf (stderr, "%02x", checksum[cnt]);
13550 putc ('\n', stderr);
13553 static void
13554 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13556 internal_error ("fold check: original tree changed by fold");
13559 static void
13560 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
13561 hash_table<nofree_ptr_hash <const tree_node> > *ht)
13563 const tree_node **slot;
13564 enum tree_code code;
13565 union tree_node *buf;
13566 int i, len;
13568 recursive_label:
13569 if (expr == NULL)
13570 return;
13571 slot = ht->find_slot (expr, INSERT);
13572 if (*slot != NULL)
13573 return;
13574 *slot = expr;
13575 code = TREE_CODE (expr);
13576 if (TREE_CODE_CLASS (code) == tcc_declaration
13577 && HAS_DECL_ASSEMBLER_NAME_P (expr))
13579 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
13580 size_t sz = tree_size (expr);
13581 buf = XALLOCAVAR (union tree_node, sz);
13582 memcpy ((char *) buf, expr, sz);
13583 SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
13584 buf->decl_with_vis.symtab_node = NULL;
13585 buf->base.nowarning_flag = 0;
13586 expr = (tree) buf;
13588 else if (TREE_CODE_CLASS (code) == tcc_type
13589 && (TYPE_POINTER_TO (expr)
13590 || TYPE_REFERENCE_TO (expr)
13591 || TYPE_CACHED_VALUES_P (expr)
13592 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13593 || TYPE_NEXT_VARIANT (expr)
13594 || TYPE_ALIAS_SET_KNOWN_P (expr)))
13596 /* Allow these fields to be modified. */
13597 tree tmp;
13598 size_t sz = tree_size (expr);
13599 buf = XALLOCAVAR (union tree_node, sz);
13600 memcpy ((char *) buf, expr, sz);
13601 expr = tmp = (tree) buf;
13602 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13603 TYPE_POINTER_TO (tmp) = NULL;
13604 TYPE_REFERENCE_TO (tmp) = NULL;
13605 TYPE_NEXT_VARIANT (tmp) = NULL;
13606 TYPE_ALIAS_SET (tmp) = -1;
13607 if (TYPE_CACHED_VALUES_P (tmp))
13609 TYPE_CACHED_VALUES_P (tmp) = 0;
13610 TYPE_CACHED_VALUES (tmp) = NULL;
13613 else if (warning_suppressed_p (expr) && (DECL_P (expr) || EXPR_P (expr)))
13615 /* Allow the no-warning bit to be set. Perhaps we shouldn't allow
13616 that and change builtins.cc etc. instead - see PR89543. */
13617 size_t sz = tree_size (expr);
13618 buf = XALLOCAVAR (union tree_node, sz);
13619 memcpy ((char *) buf, expr, sz);
13620 buf->base.nowarning_flag = 0;
13621 expr = (tree) buf;
13623 md5_process_bytes (expr, tree_size (expr), ctx);
13624 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
13625 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13626 if (TREE_CODE_CLASS (code) != tcc_type
13627 && TREE_CODE_CLASS (code) != tcc_declaration
13628 && code != TREE_LIST
13629 && code != SSA_NAME
13630 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
13631 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13632 switch (TREE_CODE_CLASS (code))
13634 case tcc_constant:
13635 switch (code)
13637 case STRING_CST:
13638 md5_process_bytes (TREE_STRING_POINTER (expr),
13639 TREE_STRING_LENGTH (expr), ctx);
13640 break;
13641 case COMPLEX_CST:
13642 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13643 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13644 break;
13645 case VECTOR_CST:
13646 len = vector_cst_encoded_nelts (expr);
13647 for (i = 0; i < len; ++i)
13648 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
13649 break;
13650 default:
13651 break;
13653 break;
13654 case tcc_exceptional:
13655 switch (code)
13657 case TREE_LIST:
13658 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13659 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13660 expr = TREE_CHAIN (expr);
13661 goto recursive_label;
13662 break;
13663 case TREE_VEC:
13664 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13665 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13666 break;
13667 default:
13668 break;
13670 break;
13671 case tcc_expression:
13672 case tcc_reference:
13673 case tcc_comparison:
13674 case tcc_unary:
13675 case tcc_binary:
13676 case tcc_statement:
13677 case tcc_vl_exp:
13678 len = TREE_OPERAND_LENGTH (expr);
13679 for (i = 0; i < len; ++i)
13680 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13681 break;
13682 case tcc_declaration:
13683 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13684 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13685 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13687 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13688 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13689 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13690 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13691 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13694 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13696 if (TREE_CODE (expr) == FUNCTION_DECL)
13698 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13699 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
13701 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13703 break;
13704 case tcc_type:
13705 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13706 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13707 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13708 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13709 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13710 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13711 if (INTEGRAL_TYPE_P (expr)
13712 || SCALAR_FLOAT_TYPE_P (expr))
13714 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13715 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13717 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13718 if (TREE_CODE (expr) == RECORD_TYPE
13719 || TREE_CODE (expr) == UNION_TYPE
13720 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13721 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13722 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13723 break;
13724 default:
13725 break;
13729 /* Helper function for outputting the checksum of a tree T. When
13730 debugging with gdb, you can "define mynext" to be "next" followed
13731 by "call debug_fold_checksum (op0)", then just trace down till the
13732 outputs differ. */
13734 DEBUG_FUNCTION void
13735 debug_fold_checksum (const_tree t)
13737 int i;
13738 unsigned char checksum[16];
13739 struct md5_ctx ctx;
13740 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13742 md5_init_ctx (&ctx);
13743 fold_checksum_tree (t, &ctx, &ht);
13744 md5_finish_ctx (&ctx, checksum);
13745 ht.empty ();
13747 for (i = 0; i < 16; i++)
13748 fprintf (stderr, "%d ", checksum[i]);
13750 fprintf (stderr, "\n");
13753 #endif
13755 /* Fold a unary tree expression with code CODE of type TYPE with an
13756 operand OP0. LOC is the location of the resulting expression.
13757 Return a folded expression if successful. Otherwise, return a tree
13758 expression with code CODE of type TYPE with an operand OP0. */
13760 tree
13761 fold_build1_loc (location_t loc,
13762 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13764 tree tem;
13765 #ifdef ENABLE_FOLD_CHECKING
13766 unsigned char checksum_before[16], checksum_after[16];
13767 struct md5_ctx ctx;
13768 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13770 md5_init_ctx (&ctx);
13771 fold_checksum_tree (op0, &ctx, &ht);
13772 md5_finish_ctx (&ctx, checksum_before);
13773 ht.empty ();
13774 #endif
13776 tem = fold_unary_loc (loc, code, type, op0);
13777 if (!tem)
13778 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
13780 #ifdef ENABLE_FOLD_CHECKING
13781 md5_init_ctx (&ctx);
13782 fold_checksum_tree (op0, &ctx, &ht);
13783 md5_finish_ctx (&ctx, checksum_after);
13785 if (memcmp (checksum_before, checksum_after, 16))
13786 fold_check_failed (op0, tem);
13787 #endif
13788 return tem;
13791 /* Fold a binary tree expression with code CODE of type TYPE with
13792 operands OP0 and OP1. LOC is the location of the resulting
13793 expression. Return a folded expression if successful. Otherwise,
13794 return a tree expression with code CODE of type TYPE with operands
13795 OP0 and OP1. */
13797 tree
13798 fold_build2_loc (location_t loc,
13799 enum tree_code code, tree type, tree op0, tree op1
13800 MEM_STAT_DECL)
13802 tree tem;
13803 #ifdef ENABLE_FOLD_CHECKING
13804 unsigned char checksum_before_op0[16],
13805 checksum_before_op1[16],
13806 checksum_after_op0[16],
13807 checksum_after_op1[16];
13808 struct md5_ctx ctx;
13809 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13811 md5_init_ctx (&ctx);
13812 fold_checksum_tree (op0, &ctx, &ht);
13813 md5_finish_ctx (&ctx, checksum_before_op0);
13814 ht.empty ();
13816 md5_init_ctx (&ctx);
13817 fold_checksum_tree (op1, &ctx, &ht);
13818 md5_finish_ctx (&ctx, checksum_before_op1);
13819 ht.empty ();
13820 #endif
13822 tem = fold_binary_loc (loc, code, type, op0, op1);
13823 if (!tem)
13824 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
13826 #ifdef ENABLE_FOLD_CHECKING
13827 md5_init_ctx (&ctx);
13828 fold_checksum_tree (op0, &ctx, &ht);
13829 md5_finish_ctx (&ctx, checksum_after_op0);
13830 ht.empty ();
13832 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13833 fold_check_failed (op0, tem);
13835 md5_init_ctx (&ctx);
13836 fold_checksum_tree (op1, &ctx, &ht);
13837 md5_finish_ctx (&ctx, checksum_after_op1);
13839 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13840 fold_check_failed (op1, tem);
13841 #endif
13842 return tem;
13845 /* Fold a ternary tree expression with code CODE of type TYPE with
13846 operands OP0, OP1, and OP2. Return a folded expression if
13847 successful. Otherwise, return a tree expression with code CODE of
13848 type TYPE with operands OP0, OP1, and OP2. */
13850 tree
13851 fold_build3_loc (location_t loc, enum tree_code code, tree type,
13852 tree op0, tree op1, tree op2 MEM_STAT_DECL)
13854 tree tem;
13855 #ifdef ENABLE_FOLD_CHECKING
13856 unsigned char checksum_before_op0[16],
13857 checksum_before_op1[16],
13858 checksum_before_op2[16],
13859 checksum_after_op0[16],
13860 checksum_after_op1[16],
13861 checksum_after_op2[16];
13862 struct md5_ctx ctx;
13863 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13865 md5_init_ctx (&ctx);
13866 fold_checksum_tree (op0, &ctx, &ht);
13867 md5_finish_ctx (&ctx, checksum_before_op0);
13868 ht.empty ();
13870 md5_init_ctx (&ctx);
13871 fold_checksum_tree (op1, &ctx, &ht);
13872 md5_finish_ctx (&ctx, checksum_before_op1);
13873 ht.empty ();
13875 md5_init_ctx (&ctx);
13876 fold_checksum_tree (op2, &ctx, &ht);
13877 md5_finish_ctx (&ctx, checksum_before_op2);
13878 ht.empty ();
13879 #endif
13881 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13882 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
13883 if (!tem)
13884 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
13886 #ifdef ENABLE_FOLD_CHECKING
13887 md5_init_ctx (&ctx);
13888 fold_checksum_tree (op0, &ctx, &ht);
13889 md5_finish_ctx (&ctx, checksum_after_op0);
13890 ht.empty ();
13892 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13893 fold_check_failed (op0, tem);
13895 md5_init_ctx (&ctx);
13896 fold_checksum_tree (op1, &ctx, &ht);
13897 md5_finish_ctx (&ctx, checksum_after_op1);
13898 ht.empty ();
13900 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13901 fold_check_failed (op1, tem);
13903 md5_init_ctx (&ctx);
13904 fold_checksum_tree (op2, &ctx, &ht);
13905 md5_finish_ctx (&ctx, checksum_after_op2);
13907 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13908 fold_check_failed (op2, tem);
13909 #endif
13910 return tem;
13913 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13914 arguments in ARGARRAY, and a null static chain.
13915 Return a folded expression if successful. Otherwise, return a CALL_EXPR
13916 of type TYPE from the given operands as constructed by build_call_array. */
13918 tree
13919 fold_build_call_array_loc (location_t loc, tree type, tree fn,
13920 int nargs, tree *argarray)
13922 tree tem;
13923 #ifdef ENABLE_FOLD_CHECKING
13924 unsigned char checksum_before_fn[16],
13925 checksum_before_arglist[16],
13926 checksum_after_fn[16],
13927 checksum_after_arglist[16];
13928 struct md5_ctx ctx;
13929 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
13930 int i;
13932 md5_init_ctx (&ctx);
13933 fold_checksum_tree (fn, &ctx, &ht);
13934 md5_finish_ctx (&ctx, checksum_before_fn);
13935 ht.empty ();
13937 md5_init_ctx (&ctx);
13938 for (i = 0; i < nargs; i++)
13939 fold_checksum_tree (argarray[i], &ctx, &ht);
13940 md5_finish_ctx (&ctx, checksum_before_arglist);
13941 ht.empty ();
13942 #endif
13944 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
13945 if (!tem)
13946 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
13948 #ifdef ENABLE_FOLD_CHECKING
13949 md5_init_ctx (&ctx);
13950 fold_checksum_tree (fn, &ctx, &ht);
13951 md5_finish_ctx (&ctx, checksum_after_fn);
13952 ht.empty ();
13954 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13955 fold_check_failed (fn, tem);
13957 md5_init_ctx (&ctx);
13958 for (i = 0; i < nargs; i++)
13959 fold_checksum_tree (argarray[i], &ctx, &ht);
13960 md5_finish_ctx (&ctx, checksum_after_arglist);
13962 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13963 fold_check_failed (NULL_TREE, tem);
13964 #endif
13965 return tem;
13968 /* Perform constant folding and related simplification of initializer
13969 expression EXPR. These behave identically to "fold_buildN" but ignore
13970 potential run-time traps and exceptions that fold must preserve. */
13972 #define START_FOLD_INIT \
13973 int saved_signaling_nans = flag_signaling_nans;\
13974 int saved_trapping_math = flag_trapping_math;\
13975 int saved_rounding_math = flag_rounding_math;\
13976 int saved_trapv = flag_trapv;\
13977 int saved_folding_initializer = folding_initializer;\
13978 flag_signaling_nans = 0;\
13979 flag_trapping_math = 0;\
13980 flag_rounding_math = 0;\
13981 flag_trapv = 0;\
13982 folding_initializer = 1;
13984 #define END_FOLD_INIT \
13985 flag_signaling_nans = saved_signaling_nans;\
13986 flag_trapping_math = saved_trapping_math;\
13987 flag_rounding_math = saved_rounding_math;\
13988 flag_trapv = saved_trapv;\
13989 folding_initializer = saved_folding_initializer;
13991 tree
13992 fold_init (tree expr)
13994 tree result;
13995 START_FOLD_INIT;
13997 result = fold (expr);
13999 END_FOLD_INIT;
14000 return result;
14003 tree
14004 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14005 tree type, tree op)
14007 tree result;
14008 START_FOLD_INIT;
14010 result = fold_build1_loc (loc, code, type, op);
14012 END_FOLD_INIT;
14013 return result;
14016 tree
14017 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14018 tree type, tree op0, tree op1)
14020 tree result;
14021 START_FOLD_INIT;
14023 result = fold_build2_loc (loc, code, type, op0, op1);
14025 END_FOLD_INIT;
14026 return result;
14029 tree
14030 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14031 int nargs, tree *argarray)
14033 tree result;
14034 START_FOLD_INIT;
14036 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14038 END_FOLD_INIT;
14039 return result;
14042 tree
14043 fold_binary_initializer_loc (location_t loc, tree_code code, tree type,
14044 tree lhs, tree rhs)
14046 tree result;
14047 START_FOLD_INIT;
14049 result = fold_binary_loc (loc, code, type, lhs, rhs);
14051 END_FOLD_INIT;
14052 return result;
14055 #undef START_FOLD_INIT
14056 #undef END_FOLD_INIT
14058 /* Determine if first argument is a multiple of second argument. Return 0 if
14059 it is not, or we cannot easily determined it to be.
14061 An example of the sort of thing we care about (at this point; this routine
14062 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14063 fold cases do now) is discovering that
14065 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14067 is a multiple of
14069 SAVE_EXPR (J * 8)
14071 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14073 This code also handles discovering that
14075 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14077 is a multiple of 8 so we don't have to worry about dealing with a
14078 possible remainder.
14080 Note that we *look* inside a SAVE_EXPR only to determine how it was
14081 calculated; it is not safe for fold to do much of anything else with the
14082 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14083 at run time. For example, the latter example above *cannot* be implemented
14084 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14085 evaluation time of the original SAVE_EXPR is not necessarily the same at
14086 the time the new expression is evaluated. The only optimization of this
14087 sort that would be valid is changing
14089 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14091 divided by 8 to
14093 SAVE_EXPR (I) * SAVE_EXPR (J)
14095 (where the same SAVE_EXPR (J) is used in the original and the
14096 transformed version).
14098 NOWRAP specifies whether all outer operations in TYPE should
14099 be considered not wrapping. Any type conversion within TOP acts
14100 as a barrier and we will fall back to NOWRAP being false.
14101 NOWRAP is mostly used to treat expressions in TYPE_SIZE and friends
14102 as not wrapping even though they are generally using unsigned arithmetic. */
14105 multiple_of_p (tree type, const_tree top, const_tree bottom, bool nowrap)
14107 gimple *stmt;
14108 tree op1, op2;
14110 if (operand_equal_p (top, bottom, 0))
14111 return 1;
14113 if (TREE_CODE (type) != INTEGER_TYPE)
14114 return 0;
14116 switch (TREE_CODE (top))
14118 case BIT_AND_EXPR:
14119 /* Bitwise and provides a power of two multiple. If the mask is
14120 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14121 if (!integer_pow2p (bottom))
14122 return 0;
14123 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14124 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14126 case MULT_EXPR:
14127 /* If the multiplication can wrap we cannot recurse further unless
14128 the bottom is a power of two which is where wrapping does not
14129 matter. */
14130 if (!nowrap
14131 && !TYPE_OVERFLOW_UNDEFINED (type)
14132 && !integer_pow2p (bottom))
14133 return 0;
14134 if (TREE_CODE (bottom) == INTEGER_CST)
14136 op1 = TREE_OPERAND (top, 0);
14137 op2 = TREE_OPERAND (top, 1);
14138 if (TREE_CODE (op1) == INTEGER_CST)
14139 std::swap (op1, op2);
14140 if (TREE_CODE (op2) == INTEGER_CST)
14142 if (multiple_of_p (type, op2, bottom, nowrap))
14143 return 1;
14144 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
14145 if (multiple_of_p (type, bottom, op2, nowrap))
14147 widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
14148 wi::to_widest (op2));
14149 if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
14151 op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
14152 return multiple_of_p (type, op1, op2, nowrap);
14155 return multiple_of_p (type, op1, bottom, nowrap);
14158 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14159 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14161 case LSHIFT_EXPR:
14162 /* Handle X << CST as X * (1 << CST) and only process the constant. */
14163 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14165 op1 = TREE_OPERAND (top, 1);
14166 if (wi::to_widest (op1) < TYPE_PRECISION (type))
14168 wide_int mul_op
14169 = wi::one (TYPE_PRECISION (type)) << wi::to_wide (op1);
14170 return multiple_of_p (type,
14171 wide_int_to_tree (type, mul_op), bottom,
14172 nowrap);
14175 return 0;
14177 case MINUS_EXPR:
14178 case PLUS_EXPR:
14179 /* If the addition or subtraction can wrap we cannot recurse further
14180 unless bottom is a power of two which is where wrapping does not
14181 matter. */
14182 if (!nowrap
14183 && !TYPE_OVERFLOW_UNDEFINED (type)
14184 && !integer_pow2p (bottom))
14185 return 0;
14187 /* Handle cases like op0 + 0xfffffffd as op0 - 3 if the expression has
14188 unsigned type. For example, (X / 3) + 0xfffffffd is multiple of 3,
14189 but 0xfffffffd is not. */
14190 op1 = TREE_OPERAND (top, 1);
14191 if (TREE_CODE (top) == PLUS_EXPR
14192 && nowrap
14193 && TYPE_UNSIGNED (type)
14194 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
14195 op1 = fold_build1 (NEGATE_EXPR, type, op1);
14197 /* It is impossible to prove if op0 +- op1 is multiple of bottom
14198 precisely, so be conservative here checking if both op0 and op1
14199 are multiple of bottom. Note we check the second operand first
14200 since it's usually simpler. */
14201 return (multiple_of_p (type, op1, bottom, nowrap)
14202 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap));
14204 CASE_CONVERT:
14205 /* Can't handle conversions from non-integral or wider integral type. */
14206 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14207 || (TYPE_PRECISION (type)
14208 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14209 return 0;
14210 /* NOWRAP only extends to operations in the outermost type so
14211 make sure to strip it off here. */
14212 return multiple_of_p (TREE_TYPE (TREE_OPERAND (top, 0)),
14213 TREE_OPERAND (top, 0), bottom, false);
14215 case SAVE_EXPR:
14216 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom, nowrap);
14218 case COND_EXPR:
14219 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom, nowrap)
14220 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom, nowrap));
14222 case INTEGER_CST:
14223 if (TREE_CODE (bottom) != INTEGER_CST || integer_zerop (bottom))
14224 return 0;
14225 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
14226 SIGNED);
14228 case SSA_NAME:
14229 if (TREE_CODE (bottom) == INTEGER_CST
14230 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
14231 && gimple_code (stmt) == GIMPLE_ASSIGN)
14233 enum tree_code code = gimple_assign_rhs_code (stmt);
14235 /* Check for special cases to see if top is defined as multiple
14236 of bottom:
14238 top = (X & ~(bottom - 1) ; bottom is power of 2
14242 Y = X % bottom
14243 top = X - Y. */
14244 if (code == BIT_AND_EXPR
14245 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14246 && TREE_CODE (op2) == INTEGER_CST
14247 && integer_pow2p (bottom)
14248 && wi::multiple_of_p (wi::to_widest (op2),
14249 wi::to_widest (bottom), UNSIGNED))
14250 return 1;
14252 op1 = gimple_assign_rhs1 (stmt);
14253 if (code == MINUS_EXPR
14254 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
14255 && TREE_CODE (op2) == SSA_NAME
14256 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
14257 && gimple_code (stmt) == GIMPLE_ASSIGN
14258 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
14259 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
14260 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
14261 return 1;
14264 /* fall through */
14266 default:
14267 if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
14268 return multiple_p (wi::to_poly_widest (top),
14269 wi::to_poly_widest (bottom));
14271 return 0;
14275 /* Return true if expression X cannot be (or contain) a NaN or infinity.
14276 This function returns true for integer expressions, and returns
14277 false if uncertain. */
14279 bool
14280 tree_expr_finite_p (const_tree x)
14282 machine_mode mode = element_mode (x);
14283 if (!HONOR_NANS (mode) && !HONOR_INFINITIES (mode))
14284 return true;
14285 switch (TREE_CODE (x))
14287 case REAL_CST:
14288 return real_isfinite (TREE_REAL_CST_PTR (x));
14289 case COMPLEX_CST:
14290 return tree_expr_finite_p (TREE_REALPART (x))
14291 && tree_expr_finite_p (TREE_IMAGPART (x));
14292 case FLOAT_EXPR:
14293 return true;
14294 case ABS_EXPR:
14295 case CONVERT_EXPR:
14296 case NON_LVALUE_EXPR:
14297 case NEGATE_EXPR:
14298 case SAVE_EXPR:
14299 return tree_expr_finite_p (TREE_OPERAND (x, 0));
14300 case MIN_EXPR:
14301 case MAX_EXPR:
14302 return tree_expr_finite_p (TREE_OPERAND (x, 0))
14303 && tree_expr_finite_p (TREE_OPERAND (x, 1));
14304 case COND_EXPR:
14305 return tree_expr_finite_p (TREE_OPERAND (x, 1))
14306 && tree_expr_finite_p (TREE_OPERAND (x, 2));
14307 case CALL_EXPR:
14308 switch (get_call_combined_fn (x))
14310 CASE_CFN_FABS:
14311 CASE_CFN_FABS_FN:
14312 return tree_expr_finite_p (CALL_EXPR_ARG (x, 0));
14313 CASE_CFN_FMAX:
14314 CASE_CFN_FMAX_FN:
14315 CASE_CFN_FMIN:
14316 CASE_CFN_FMIN_FN:
14317 return tree_expr_finite_p (CALL_EXPR_ARG (x, 0))
14318 && tree_expr_finite_p (CALL_EXPR_ARG (x, 1));
14319 default:
14320 return false;
14323 default:
14324 return false;
14328 /* Return true if expression X evaluates to an infinity.
14329 This function returns false for integer expressions. */
14331 bool
14332 tree_expr_infinite_p (const_tree x)
14334 if (!HONOR_INFINITIES (x))
14335 return false;
14336 switch (TREE_CODE (x))
14338 case REAL_CST:
14339 return real_isinf (TREE_REAL_CST_PTR (x));
14340 case ABS_EXPR:
14341 case NEGATE_EXPR:
14342 case NON_LVALUE_EXPR:
14343 case SAVE_EXPR:
14344 return tree_expr_infinite_p (TREE_OPERAND (x, 0));
14345 case COND_EXPR:
14346 return tree_expr_infinite_p (TREE_OPERAND (x, 1))
14347 && tree_expr_infinite_p (TREE_OPERAND (x, 2));
14348 default:
14349 return false;
14353 /* Return true if expression X could evaluate to an infinity.
14354 This function returns false for integer expressions, and returns
14355 true if uncertain. */
14357 bool
14358 tree_expr_maybe_infinite_p (const_tree x)
14360 if (!HONOR_INFINITIES (x))
14361 return false;
14362 switch (TREE_CODE (x))
14364 case REAL_CST:
14365 return real_isinf (TREE_REAL_CST_PTR (x));
14366 case FLOAT_EXPR:
14367 return false;
14368 case ABS_EXPR:
14369 case NEGATE_EXPR:
14370 return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 0));
14371 case COND_EXPR:
14372 return tree_expr_maybe_infinite_p (TREE_OPERAND (x, 1))
14373 || tree_expr_maybe_infinite_p (TREE_OPERAND (x, 2));
14374 default:
14375 return true;
14379 /* Return true if expression X evaluates to a signaling NaN.
14380 This function returns false for integer expressions. */
14382 bool
14383 tree_expr_signaling_nan_p (const_tree x)
14385 if (!HONOR_SNANS (x))
14386 return false;
14387 switch (TREE_CODE (x))
14389 case REAL_CST:
14390 return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14391 case NON_LVALUE_EXPR:
14392 case SAVE_EXPR:
14393 return tree_expr_signaling_nan_p (TREE_OPERAND (x, 0));
14394 case COND_EXPR:
14395 return tree_expr_signaling_nan_p (TREE_OPERAND (x, 1))
14396 && tree_expr_signaling_nan_p (TREE_OPERAND (x, 2));
14397 default:
14398 return false;
14402 /* Return true if expression X could evaluate to a signaling NaN.
14403 This function returns false for integer expressions, and returns
14404 true if uncertain. */
14406 bool
14407 tree_expr_maybe_signaling_nan_p (const_tree x)
14409 if (!HONOR_SNANS (x))
14410 return false;
14411 switch (TREE_CODE (x))
14413 case REAL_CST:
14414 return real_issignaling_nan (TREE_REAL_CST_PTR (x));
14415 case FLOAT_EXPR:
14416 return false;
14417 case ABS_EXPR:
14418 case CONVERT_EXPR:
14419 case NEGATE_EXPR:
14420 case NON_LVALUE_EXPR:
14421 case SAVE_EXPR:
14422 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0));
14423 case MIN_EXPR:
14424 case MAX_EXPR:
14425 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 0))
14426 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1));
14427 case COND_EXPR:
14428 return tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 1))
14429 || tree_expr_maybe_signaling_nan_p (TREE_OPERAND (x, 2));
14430 case CALL_EXPR:
14431 switch (get_call_combined_fn (x))
14433 CASE_CFN_FABS:
14434 CASE_CFN_FABS_FN:
14435 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0));
14436 CASE_CFN_FMAX:
14437 CASE_CFN_FMAX_FN:
14438 CASE_CFN_FMIN:
14439 CASE_CFN_FMIN_FN:
14440 return tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 0))
14441 || tree_expr_maybe_signaling_nan_p (CALL_EXPR_ARG (x, 1));
14442 default:
14443 return true;
14445 default:
14446 return true;
14450 /* Return true if expression X evaluates to a NaN.
14451 This function returns false for integer expressions. */
14453 bool
14454 tree_expr_nan_p (const_tree x)
14456 if (!HONOR_NANS (x))
14457 return false;
14458 switch (TREE_CODE (x))
14460 case REAL_CST:
14461 return real_isnan (TREE_REAL_CST_PTR (x));
14462 case NON_LVALUE_EXPR:
14463 case SAVE_EXPR:
14464 return tree_expr_nan_p (TREE_OPERAND (x, 0));
14465 case COND_EXPR:
14466 return tree_expr_nan_p (TREE_OPERAND (x, 1))
14467 && tree_expr_nan_p (TREE_OPERAND (x, 2));
14468 default:
14469 return false;
14473 /* Return true if expression X could evaluate to a NaN.
14474 This function returns false for integer expressions, and returns
14475 true if uncertain. */
14477 bool
14478 tree_expr_maybe_nan_p (const_tree x)
14480 if (!HONOR_NANS (x))
14481 return false;
14482 switch (TREE_CODE (x))
14484 case REAL_CST:
14485 return real_isnan (TREE_REAL_CST_PTR (x));
14486 case FLOAT_EXPR:
14487 return false;
14488 case PLUS_EXPR:
14489 case MINUS_EXPR:
14490 case MULT_EXPR:
14491 return !tree_expr_finite_p (TREE_OPERAND (x, 0))
14492 || !tree_expr_finite_p (TREE_OPERAND (x, 1));
14493 case ABS_EXPR:
14494 case CONVERT_EXPR:
14495 case NEGATE_EXPR:
14496 case NON_LVALUE_EXPR:
14497 case SAVE_EXPR:
14498 return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0));
14499 case MIN_EXPR:
14500 case MAX_EXPR:
14501 return tree_expr_maybe_nan_p (TREE_OPERAND (x, 0))
14502 || tree_expr_maybe_nan_p (TREE_OPERAND (x, 1));
14503 case COND_EXPR:
14504 return tree_expr_maybe_nan_p (TREE_OPERAND (x, 1))
14505 || tree_expr_maybe_nan_p (TREE_OPERAND (x, 2));
14506 case CALL_EXPR:
14507 switch (get_call_combined_fn (x))
14509 CASE_CFN_FABS:
14510 CASE_CFN_FABS_FN:
14511 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0));
14512 CASE_CFN_FMAX:
14513 CASE_CFN_FMAX_FN:
14514 CASE_CFN_FMIN:
14515 CASE_CFN_FMIN_FN:
14516 return tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 0))
14517 || tree_expr_maybe_nan_p (CALL_EXPR_ARG (x, 1));
14518 default:
14519 return true;
14521 default:
14522 return true;
14526 /* Return true if expression X could evaluate to -0.0.
14527 This function returns true if uncertain. */
14529 bool
14530 tree_expr_maybe_real_minus_zero_p (const_tree x)
14532 if (!HONOR_SIGNED_ZEROS (x))
14533 return false;
14534 switch (TREE_CODE (x))
14536 case REAL_CST:
14537 return REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (x));
14538 case INTEGER_CST:
14539 case FLOAT_EXPR:
14540 case ABS_EXPR:
14541 return false;
14542 case NON_LVALUE_EXPR:
14543 case SAVE_EXPR:
14544 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 0));
14545 case COND_EXPR:
14546 return tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 1))
14547 || tree_expr_maybe_real_minus_zero_p (TREE_OPERAND (x, 2));
14548 case CALL_EXPR:
14549 switch (get_call_combined_fn (x))
14551 CASE_CFN_FABS:
14552 CASE_CFN_FABS_FN:
14553 return false;
14554 default:
14555 break;
14557 default:
14558 break;
14560 /* Ideally !(tree_expr_nonzero_p (X) || tree_expr_nonnegative_p (X))
14561 * but currently those predicates require tree and not const_tree. */
14562 return true;
14565 #define tree_expr_nonnegative_warnv_p(X, Y) \
14566 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
14568 #define RECURSE(X) \
14569 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
14571 /* Return true if CODE or TYPE is known to be non-negative. */
14573 static bool
14574 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14576 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14577 && truth_value_p (code))
14578 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14579 have a signed:1 type (where the value is -1 and 0). */
14580 return true;
14581 return false;
14584 /* Return true if (CODE OP0) is known to be non-negative. If the return
14585 value is based on the assumption that signed overflow is undefined,
14586 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14587 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14589 bool
14590 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14591 bool *strict_overflow_p, int depth)
14593 if (TYPE_UNSIGNED (type))
14594 return true;
14596 switch (code)
14598 case ABS_EXPR:
14599 /* We can't return 1 if flag_wrapv is set because
14600 ABS_EXPR<INT_MIN> = INT_MIN. */
14601 if (!ANY_INTEGRAL_TYPE_P (type))
14602 return true;
14603 if (TYPE_OVERFLOW_UNDEFINED (type))
14605 *strict_overflow_p = true;
14606 return true;
14608 break;
14610 case NON_LVALUE_EXPR:
14611 case FLOAT_EXPR:
14612 case FIX_TRUNC_EXPR:
14613 return RECURSE (op0);
14615 CASE_CONVERT:
14617 tree inner_type = TREE_TYPE (op0);
14618 tree outer_type = type;
14620 if (TREE_CODE (outer_type) == REAL_TYPE)
14622 if (TREE_CODE (inner_type) == REAL_TYPE)
14623 return RECURSE (op0);
14624 if (INTEGRAL_TYPE_P (inner_type))
14626 if (TYPE_UNSIGNED (inner_type))
14627 return true;
14628 return RECURSE (op0);
14631 else if (INTEGRAL_TYPE_P (outer_type))
14633 if (TREE_CODE (inner_type) == REAL_TYPE)
14634 return RECURSE (op0);
14635 if (INTEGRAL_TYPE_P (inner_type))
14636 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14637 && TYPE_UNSIGNED (inner_type);
14640 break;
14642 default:
14643 return tree_simple_nonnegative_warnv_p (code, type);
14646 /* We don't know sign of `t', so be conservative and return false. */
14647 return false;
14650 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14651 value is based on the assumption that signed overflow is undefined,
14652 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14653 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14655 bool
14656 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14657 tree op1, bool *strict_overflow_p,
14658 int depth)
14660 if (TYPE_UNSIGNED (type))
14661 return true;
14663 switch (code)
14665 case POINTER_PLUS_EXPR:
14666 case PLUS_EXPR:
14667 if (FLOAT_TYPE_P (type))
14668 return RECURSE (op0) && RECURSE (op1);
14670 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14671 both unsigned and at least 2 bits shorter than the result. */
14672 if (TREE_CODE (type) == INTEGER_TYPE
14673 && TREE_CODE (op0) == NOP_EXPR
14674 && TREE_CODE (op1) == NOP_EXPR)
14676 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14677 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14678 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14679 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14681 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14682 TYPE_PRECISION (inner2)) + 1;
14683 return prec < TYPE_PRECISION (type);
14686 break;
14688 case MULT_EXPR:
14689 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
14691 /* x * x is always non-negative for floating point x
14692 or without overflow. */
14693 if (operand_equal_p (op0, op1, 0)
14694 || (RECURSE (op0) && RECURSE (op1)))
14696 if (ANY_INTEGRAL_TYPE_P (type)
14697 && TYPE_OVERFLOW_UNDEFINED (type))
14698 *strict_overflow_p = true;
14699 return true;
14703 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14704 both unsigned and their total bits is shorter than the result. */
14705 if (TREE_CODE (type) == INTEGER_TYPE
14706 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14707 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14709 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14710 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14711 : TREE_TYPE (op0);
14712 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14713 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14714 : TREE_TYPE (op1);
14716 bool unsigned0 = TYPE_UNSIGNED (inner0);
14717 bool unsigned1 = TYPE_UNSIGNED (inner1);
14719 if (TREE_CODE (op0) == INTEGER_CST)
14720 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14722 if (TREE_CODE (op1) == INTEGER_CST)
14723 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14725 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14726 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14728 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14729 ? tree_int_cst_min_precision (op0, UNSIGNED)
14730 : TYPE_PRECISION (inner0);
14732 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14733 ? tree_int_cst_min_precision (op1, UNSIGNED)
14734 : TYPE_PRECISION (inner1);
14736 return precision0 + precision1 < TYPE_PRECISION (type);
14739 return false;
14741 case BIT_AND_EXPR:
14742 return RECURSE (op0) || RECURSE (op1);
14744 case MAX_EXPR:
14745 /* Usually RECURSE (op0) || RECURSE (op1) but NaNs complicate
14746 things. */
14747 if (tree_expr_maybe_nan_p (op0) || tree_expr_maybe_nan_p (op1))
14748 return RECURSE (op0) && RECURSE (op1);
14749 return RECURSE (op0) || RECURSE (op1);
14751 case BIT_IOR_EXPR:
14752 case BIT_XOR_EXPR:
14753 case MIN_EXPR:
14754 case RDIV_EXPR:
14755 case TRUNC_DIV_EXPR:
14756 case CEIL_DIV_EXPR:
14757 case FLOOR_DIV_EXPR:
14758 case ROUND_DIV_EXPR:
14759 return RECURSE (op0) && RECURSE (op1);
14761 case TRUNC_MOD_EXPR:
14762 return RECURSE (op0);
14764 case FLOOR_MOD_EXPR:
14765 return RECURSE (op1);
14767 case CEIL_MOD_EXPR:
14768 case ROUND_MOD_EXPR:
14769 default:
14770 return tree_simple_nonnegative_warnv_p (code, type);
14773 /* We don't know sign of `t', so be conservative and return false. */
14774 return false;
14777 /* Return true if T is known to be non-negative. If the return
14778 value is based on the assumption that signed overflow is undefined,
14779 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14780 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14782 bool
14783 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
14785 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14786 return true;
14788 switch (TREE_CODE (t))
14790 case INTEGER_CST:
14791 return tree_int_cst_sgn (t) >= 0;
14793 case REAL_CST:
14794 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14796 case FIXED_CST:
14797 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14799 case COND_EXPR:
14800 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
14802 case SSA_NAME:
14803 /* Limit the depth of recursion to avoid quadratic behavior.
14804 This is expected to catch almost all occurrences in practice.
14805 If this code misses important cases that unbounded recursion
14806 would not, passes that need this information could be revised
14807 to provide it through dataflow propagation. */
14808 return (!name_registered_for_update_p (t)
14809 && depth < param_max_ssa_name_query_depth
14810 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
14811 strict_overflow_p, depth));
14813 default:
14814 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
14818 /* Return true if T is known to be non-negative. If the return
14819 value is based on the assumption that signed overflow is undefined,
14820 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14821 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14823 bool
14824 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
14825 bool *strict_overflow_p, int depth)
14827 switch (fn)
14829 CASE_CFN_ACOS:
14830 CASE_CFN_ACOS_FN:
14831 CASE_CFN_ACOSH:
14832 CASE_CFN_ACOSH_FN:
14833 CASE_CFN_CABS:
14834 CASE_CFN_CABS_FN:
14835 CASE_CFN_COSH:
14836 CASE_CFN_COSH_FN:
14837 CASE_CFN_ERFC:
14838 CASE_CFN_ERFC_FN:
14839 CASE_CFN_EXP:
14840 CASE_CFN_EXP_FN:
14841 CASE_CFN_EXP10:
14842 CASE_CFN_EXP2:
14843 CASE_CFN_EXP2_FN:
14844 CASE_CFN_FABS:
14845 CASE_CFN_FABS_FN:
14846 CASE_CFN_FDIM:
14847 CASE_CFN_FDIM_FN:
14848 CASE_CFN_HYPOT:
14849 CASE_CFN_HYPOT_FN:
14850 CASE_CFN_POW10:
14851 CASE_CFN_FFS:
14852 CASE_CFN_PARITY:
14853 CASE_CFN_POPCOUNT:
14854 CASE_CFN_CLZ:
14855 CASE_CFN_CLRSB:
14856 case CFN_BUILT_IN_BSWAP16:
14857 case CFN_BUILT_IN_BSWAP32:
14858 case CFN_BUILT_IN_BSWAP64:
14859 case CFN_BUILT_IN_BSWAP128:
14860 /* Always true. */
14861 return true;
14863 CASE_CFN_SQRT:
14864 CASE_CFN_SQRT_FN:
14865 /* sqrt(-0.0) is -0.0. */
14866 if (!HONOR_SIGNED_ZEROS (type))
14867 return true;
14868 return RECURSE (arg0);
14870 CASE_CFN_ASINH:
14871 CASE_CFN_ASINH_FN:
14872 CASE_CFN_ATAN:
14873 CASE_CFN_ATAN_FN:
14874 CASE_CFN_ATANH:
14875 CASE_CFN_ATANH_FN:
14876 CASE_CFN_CBRT:
14877 CASE_CFN_CBRT_FN:
14878 CASE_CFN_CEIL:
14879 CASE_CFN_CEIL_FN:
14880 CASE_CFN_ERF:
14881 CASE_CFN_ERF_FN:
14882 CASE_CFN_EXPM1:
14883 CASE_CFN_EXPM1_FN:
14884 CASE_CFN_FLOOR:
14885 CASE_CFN_FLOOR_FN:
14886 CASE_CFN_FMOD:
14887 CASE_CFN_FMOD_FN:
14888 CASE_CFN_FREXP:
14889 CASE_CFN_FREXP_FN:
14890 CASE_CFN_ICEIL:
14891 CASE_CFN_IFLOOR:
14892 CASE_CFN_IRINT:
14893 CASE_CFN_IROUND:
14894 CASE_CFN_LCEIL:
14895 CASE_CFN_LDEXP:
14896 CASE_CFN_LFLOOR:
14897 CASE_CFN_LLCEIL:
14898 CASE_CFN_LLFLOOR:
14899 CASE_CFN_LLRINT:
14900 CASE_CFN_LLRINT_FN:
14901 CASE_CFN_LLROUND:
14902 CASE_CFN_LLROUND_FN:
14903 CASE_CFN_LRINT:
14904 CASE_CFN_LRINT_FN:
14905 CASE_CFN_LROUND:
14906 CASE_CFN_LROUND_FN:
14907 CASE_CFN_MODF:
14908 CASE_CFN_MODF_FN:
14909 CASE_CFN_NEARBYINT:
14910 CASE_CFN_NEARBYINT_FN:
14911 CASE_CFN_RINT:
14912 CASE_CFN_RINT_FN:
14913 CASE_CFN_ROUND:
14914 CASE_CFN_ROUND_FN:
14915 CASE_CFN_ROUNDEVEN:
14916 CASE_CFN_ROUNDEVEN_FN:
14917 CASE_CFN_SCALB:
14918 CASE_CFN_SCALBLN:
14919 CASE_CFN_SCALBLN_FN:
14920 CASE_CFN_SCALBN:
14921 CASE_CFN_SCALBN_FN:
14922 CASE_CFN_SIGNBIT:
14923 CASE_CFN_SIGNIFICAND:
14924 CASE_CFN_SINH:
14925 CASE_CFN_SINH_FN:
14926 CASE_CFN_TANH:
14927 CASE_CFN_TANH_FN:
14928 CASE_CFN_TRUNC:
14929 CASE_CFN_TRUNC_FN:
14930 /* True if the 1st argument is nonnegative. */
14931 return RECURSE (arg0);
14933 CASE_CFN_FMAX:
14934 CASE_CFN_FMAX_FN:
14935 /* Usually RECURSE (arg0) || RECURSE (arg1) but NaNs complicate
14936 things. In the presence of sNaNs, we're only guaranteed to be
14937 non-negative if both operands are non-negative. In the presence
14938 of qNaNs, we're non-negative if either operand is non-negative
14939 and can't be a qNaN, or if both operands are non-negative. */
14940 if (tree_expr_maybe_signaling_nan_p (arg0) ||
14941 tree_expr_maybe_signaling_nan_p (arg1))
14942 return RECURSE (arg0) && RECURSE (arg1);
14943 return RECURSE (arg0) ? (!tree_expr_maybe_nan_p (arg0)
14944 || RECURSE (arg1))
14945 : (RECURSE (arg1)
14946 && !tree_expr_maybe_nan_p (arg1));
14948 CASE_CFN_FMIN:
14949 CASE_CFN_FMIN_FN:
14950 /* True if the 1st AND 2nd arguments are nonnegative. */
14951 return RECURSE (arg0) && RECURSE (arg1);
14953 CASE_CFN_COPYSIGN:
14954 CASE_CFN_COPYSIGN_FN:
14955 /* True if the 2nd argument is nonnegative. */
14956 return RECURSE (arg1);
14958 CASE_CFN_POWI:
14959 /* True if the 1st argument is nonnegative or the second
14960 argument is an even integer. */
14961 if (TREE_CODE (arg1) == INTEGER_CST
14962 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14963 return true;
14964 return RECURSE (arg0);
14966 CASE_CFN_POW:
14967 CASE_CFN_POW_FN:
14968 /* True if the 1st argument is nonnegative or the second
14969 argument is an even integer valued real. */
14970 if (TREE_CODE (arg1) == REAL_CST)
14972 REAL_VALUE_TYPE c;
14973 HOST_WIDE_INT n;
14975 c = TREE_REAL_CST (arg1);
14976 n = real_to_integer (&c);
14977 if ((n & 1) == 0)
14979 REAL_VALUE_TYPE cint;
14980 real_from_integer (&cint, VOIDmode, n, SIGNED);
14981 if (real_identical (&c, &cint))
14982 return true;
14985 return RECURSE (arg0);
14987 default:
14988 break;
14990 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
14993 /* Return true if T is known to be non-negative. If the return
14994 value is based on the assumption that signed overflow is undefined,
14995 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14996 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
14998 static bool
14999 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15001 enum tree_code code = TREE_CODE (t);
15002 if (TYPE_UNSIGNED (TREE_TYPE (t)))
15003 return true;
15005 switch (code)
15007 case TARGET_EXPR:
15009 tree temp = TARGET_EXPR_SLOT (t);
15010 t = TARGET_EXPR_INITIAL (t);
15012 /* If the initializer is non-void, then it's a normal expression
15013 that will be assigned to the slot. */
15014 if (!VOID_TYPE_P (TREE_TYPE (t)))
15015 return RECURSE (t);
15017 /* Otherwise, the initializer sets the slot in some way. One common
15018 way is an assignment statement at the end of the initializer. */
15019 while (1)
15021 if (TREE_CODE (t) == BIND_EXPR)
15022 t = expr_last (BIND_EXPR_BODY (t));
15023 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15024 || TREE_CODE (t) == TRY_CATCH_EXPR)
15025 t = expr_last (TREE_OPERAND (t, 0));
15026 else if (TREE_CODE (t) == STATEMENT_LIST)
15027 t = expr_last (t);
15028 else
15029 break;
15031 if (TREE_CODE (t) == MODIFY_EXPR
15032 && TREE_OPERAND (t, 0) == temp)
15033 return RECURSE (TREE_OPERAND (t, 1));
15035 return false;
15038 case CALL_EXPR:
15040 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15041 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15043 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15044 get_call_combined_fn (t),
15045 arg0,
15046 arg1,
15047 strict_overflow_p, depth);
15049 case COMPOUND_EXPR:
15050 case MODIFY_EXPR:
15051 return RECURSE (TREE_OPERAND (t, 1));
15053 case BIND_EXPR:
15054 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
15056 case SAVE_EXPR:
15057 return RECURSE (TREE_OPERAND (t, 0));
15059 default:
15060 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
15064 #undef RECURSE
15065 #undef tree_expr_nonnegative_warnv_p
15067 /* Return true if T is known to be non-negative. If the return
15068 value is based on the assumption that signed overflow is undefined,
15069 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15070 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
15072 bool
15073 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
15075 enum tree_code code;
15076 if (t == error_mark_node)
15077 return false;
15079 code = TREE_CODE (t);
15080 switch (TREE_CODE_CLASS (code))
15082 case tcc_binary:
15083 case tcc_comparison:
15084 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15085 TREE_TYPE (t),
15086 TREE_OPERAND (t, 0),
15087 TREE_OPERAND (t, 1),
15088 strict_overflow_p, depth);
15090 case tcc_unary:
15091 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15092 TREE_TYPE (t),
15093 TREE_OPERAND (t, 0),
15094 strict_overflow_p, depth);
15096 case tcc_constant:
15097 case tcc_declaration:
15098 case tcc_reference:
15099 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15101 default:
15102 break;
15105 switch (code)
15107 case TRUTH_AND_EXPR:
15108 case TRUTH_OR_EXPR:
15109 case TRUTH_XOR_EXPR:
15110 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15111 TREE_TYPE (t),
15112 TREE_OPERAND (t, 0),
15113 TREE_OPERAND (t, 1),
15114 strict_overflow_p, depth);
15115 case TRUTH_NOT_EXPR:
15116 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15117 TREE_TYPE (t),
15118 TREE_OPERAND (t, 0),
15119 strict_overflow_p, depth);
15121 case COND_EXPR:
15122 case CONSTRUCTOR:
15123 case OBJ_TYPE_REF:
15124 case ADDR_EXPR:
15125 case WITH_SIZE_EXPR:
15126 case SSA_NAME:
15127 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
15129 default:
15130 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
15134 /* Return true if `t' is known to be non-negative. Handle warnings
15135 about undefined signed overflow. */
15137 bool
15138 tree_expr_nonnegative_p (tree t)
15140 bool ret, strict_overflow_p;
15142 strict_overflow_p = false;
15143 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15144 if (strict_overflow_p)
15145 fold_overflow_warning (("assuming signed overflow does not occur when "
15146 "determining that expression is always "
15147 "non-negative"),
15148 WARN_STRICT_OVERFLOW_MISC);
15149 return ret;
15153 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15154 For floating point we further ensure that T is not denormal.
15155 Similar logic is present in nonzero_address in rtlanal.h.
15157 If the return value is based on the assumption that signed overflow
15158 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15159 change *STRICT_OVERFLOW_P. */
15161 bool
15162 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15163 bool *strict_overflow_p)
15165 switch (code)
15167 case ABS_EXPR:
15168 return tree_expr_nonzero_warnv_p (op0,
15169 strict_overflow_p);
15171 case NOP_EXPR:
15173 tree inner_type = TREE_TYPE (op0);
15174 tree outer_type = type;
15176 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15177 && tree_expr_nonzero_warnv_p (op0,
15178 strict_overflow_p));
15180 break;
15182 case NON_LVALUE_EXPR:
15183 return tree_expr_nonzero_warnv_p (op0,
15184 strict_overflow_p);
15186 default:
15187 break;
15190 return false;
15193 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15194 For floating point we further ensure that T is not denormal.
15195 Similar logic is present in nonzero_address in rtlanal.h.
15197 If the return value is based on the assumption that signed overflow
15198 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15199 change *STRICT_OVERFLOW_P. */
15201 bool
15202 tree_binary_nonzero_warnv_p (enum tree_code code,
15203 tree type,
15204 tree op0,
15205 tree op1, bool *strict_overflow_p)
15207 bool sub_strict_overflow_p;
15208 switch (code)
15210 case POINTER_PLUS_EXPR:
15211 case PLUS_EXPR:
15212 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
15214 /* With the presence of negative values it is hard
15215 to say something. */
15216 sub_strict_overflow_p = false;
15217 if (!tree_expr_nonnegative_warnv_p (op0,
15218 &sub_strict_overflow_p)
15219 || !tree_expr_nonnegative_warnv_p (op1,
15220 &sub_strict_overflow_p))
15221 return false;
15222 /* One of operands must be positive and the other non-negative. */
15223 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15224 overflows, on a twos-complement machine the sum of two
15225 nonnegative numbers can never be zero. */
15226 return (tree_expr_nonzero_warnv_p (op0,
15227 strict_overflow_p)
15228 || tree_expr_nonzero_warnv_p (op1,
15229 strict_overflow_p));
15231 break;
15233 case MULT_EXPR:
15234 if (TYPE_OVERFLOW_UNDEFINED (type))
15236 if (tree_expr_nonzero_warnv_p (op0,
15237 strict_overflow_p)
15238 && tree_expr_nonzero_warnv_p (op1,
15239 strict_overflow_p))
15241 *strict_overflow_p = true;
15242 return true;
15245 break;
15247 case MIN_EXPR:
15248 sub_strict_overflow_p = false;
15249 if (tree_expr_nonzero_warnv_p (op0,
15250 &sub_strict_overflow_p)
15251 && tree_expr_nonzero_warnv_p (op1,
15252 &sub_strict_overflow_p))
15254 if (sub_strict_overflow_p)
15255 *strict_overflow_p = true;
15257 break;
15259 case MAX_EXPR:
15260 sub_strict_overflow_p = false;
15261 if (tree_expr_nonzero_warnv_p (op0,
15262 &sub_strict_overflow_p))
15264 if (sub_strict_overflow_p)
15265 *strict_overflow_p = true;
15267 /* When both operands are nonzero, then MAX must be too. */
15268 if (tree_expr_nonzero_warnv_p (op1,
15269 strict_overflow_p))
15270 return true;
15272 /* MAX where operand 0 is positive is positive. */
15273 return tree_expr_nonnegative_warnv_p (op0,
15274 strict_overflow_p);
15276 /* MAX where operand 1 is positive is positive. */
15277 else if (tree_expr_nonzero_warnv_p (op1,
15278 &sub_strict_overflow_p)
15279 && tree_expr_nonnegative_warnv_p (op1,
15280 &sub_strict_overflow_p))
15282 if (sub_strict_overflow_p)
15283 *strict_overflow_p = true;
15284 return true;
15286 break;
15288 case BIT_IOR_EXPR:
15289 return (tree_expr_nonzero_warnv_p (op1,
15290 strict_overflow_p)
15291 || tree_expr_nonzero_warnv_p (op0,
15292 strict_overflow_p));
15294 default:
15295 break;
15298 return false;
15301 /* Return true when T is an address and is known to be nonzero.
15302 For floating point we further ensure that T is not denormal.
15303 Similar logic is present in nonzero_address in rtlanal.h.
15305 If the return value is based on the assumption that signed overflow
15306 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15307 change *STRICT_OVERFLOW_P. */
15309 bool
15310 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15312 bool sub_strict_overflow_p;
15313 switch (TREE_CODE (t))
15315 case INTEGER_CST:
15316 return !integer_zerop (t);
15318 case ADDR_EXPR:
15320 tree base = TREE_OPERAND (t, 0);
15322 if (!DECL_P (base))
15323 base = get_base_address (base);
15325 if (base && TREE_CODE (base) == TARGET_EXPR)
15326 base = TARGET_EXPR_SLOT (base);
15328 if (!base)
15329 return false;
15331 /* For objects in symbol table check if we know they are non-zero.
15332 Don't do anything for variables and functions before symtab is built;
15333 it is quite possible that they will be declared weak later. */
15334 int nonzero_addr = maybe_nonzero_address (base);
15335 if (nonzero_addr >= 0)
15336 return nonzero_addr;
15338 /* Constants are never weak. */
15339 if (CONSTANT_CLASS_P (base))
15340 return true;
15342 return false;
15345 case COND_EXPR:
15346 sub_strict_overflow_p = false;
15347 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15348 &sub_strict_overflow_p)
15349 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15350 &sub_strict_overflow_p))
15352 if (sub_strict_overflow_p)
15353 *strict_overflow_p = true;
15354 return true;
15356 break;
15358 case SSA_NAME:
15359 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
15360 break;
15361 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
15363 default:
15364 break;
15366 return false;
15369 #define integer_valued_real_p(X) \
15370 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
15372 #define RECURSE(X) \
15373 ((integer_valued_real_p) (X, depth + 1))
15375 /* Return true if the floating point result of (CODE OP0) has an
15376 integer value. We also allow +Inf, -Inf and NaN to be considered
15377 integer values. Return false for signaling NaN.
15379 DEPTH is the current nesting depth of the query. */
15381 bool
15382 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
15384 switch (code)
15386 case FLOAT_EXPR:
15387 return true;
15389 case ABS_EXPR:
15390 return RECURSE (op0);
15392 CASE_CONVERT:
15394 tree type = TREE_TYPE (op0);
15395 if (TREE_CODE (type) == INTEGER_TYPE)
15396 return true;
15397 if (TREE_CODE (type) == REAL_TYPE)
15398 return RECURSE (op0);
15399 break;
15402 default:
15403 break;
15405 return false;
15408 /* Return true if the floating point result of (CODE OP0 OP1) has an
15409 integer value. We also allow +Inf, -Inf and NaN to be considered
15410 integer values. Return false for signaling NaN.
15412 DEPTH is the current nesting depth of the query. */
15414 bool
15415 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
15417 switch (code)
15419 case PLUS_EXPR:
15420 case MINUS_EXPR:
15421 case MULT_EXPR:
15422 case MIN_EXPR:
15423 case MAX_EXPR:
15424 return RECURSE (op0) && RECURSE (op1);
15426 default:
15427 break;
15429 return false;
15432 /* Return true if the floating point result of calling FNDECL with arguments
15433 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
15434 considered integer values. Return false for signaling NaN. If FNDECL
15435 takes fewer than 2 arguments, the remaining ARGn are null.
15437 DEPTH is the current nesting depth of the query. */
15439 bool
15440 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
15442 switch (fn)
15444 CASE_CFN_CEIL:
15445 CASE_CFN_CEIL_FN:
15446 CASE_CFN_FLOOR:
15447 CASE_CFN_FLOOR_FN:
15448 CASE_CFN_NEARBYINT:
15449 CASE_CFN_NEARBYINT_FN:
15450 CASE_CFN_RINT:
15451 CASE_CFN_RINT_FN:
15452 CASE_CFN_ROUND:
15453 CASE_CFN_ROUND_FN:
15454 CASE_CFN_ROUNDEVEN:
15455 CASE_CFN_ROUNDEVEN_FN:
15456 CASE_CFN_TRUNC:
15457 CASE_CFN_TRUNC_FN:
15458 return true;
15460 CASE_CFN_FMIN:
15461 CASE_CFN_FMIN_FN:
15462 CASE_CFN_FMAX:
15463 CASE_CFN_FMAX_FN:
15464 return RECURSE (arg0) && RECURSE (arg1);
15466 default:
15467 break;
15469 return false;
15472 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
15473 has an integer value. We also allow +Inf, -Inf and NaN to be
15474 considered integer values. Return false for signaling NaN.
15476 DEPTH is the current nesting depth of the query. */
15478 bool
15479 integer_valued_real_single_p (tree t, int depth)
15481 switch (TREE_CODE (t))
15483 case REAL_CST:
15484 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
15486 case COND_EXPR:
15487 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
15489 case SSA_NAME:
15490 /* Limit the depth of recursion to avoid quadratic behavior.
15491 This is expected to catch almost all occurrences in practice.
15492 If this code misses important cases that unbounded recursion
15493 would not, passes that need this information could be revised
15494 to provide it through dataflow propagation. */
15495 return (!name_registered_for_update_p (t)
15496 && depth < param_max_ssa_name_query_depth
15497 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
15498 depth));
15500 default:
15501 break;
15503 return false;
15506 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
15507 has an integer value. We also allow +Inf, -Inf and NaN to be
15508 considered integer values. Return false for signaling NaN.
15510 DEPTH is the current nesting depth of the query. */
15512 static bool
15513 integer_valued_real_invalid_p (tree t, int depth)
15515 switch (TREE_CODE (t))
15517 case COMPOUND_EXPR:
15518 case MODIFY_EXPR:
15519 case BIND_EXPR:
15520 return RECURSE (TREE_OPERAND (t, 1));
15522 case SAVE_EXPR:
15523 return RECURSE (TREE_OPERAND (t, 0));
15525 default:
15526 break;
15528 return false;
15531 #undef RECURSE
15532 #undef integer_valued_real_p
15534 /* Return true if the floating point expression T has an integer value.
15535 We also allow +Inf, -Inf and NaN to be considered integer values.
15536 Return false for signaling NaN.
15538 DEPTH is the current nesting depth of the query. */
15540 bool
15541 integer_valued_real_p (tree t, int depth)
15543 if (t == error_mark_node)
15544 return false;
15546 STRIP_ANY_LOCATION_WRAPPER (t);
15548 tree_code code = TREE_CODE (t);
15549 switch (TREE_CODE_CLASS (code))
15551 case tcc_binary:
15552 case tcc_comparison:
15553 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
15554 TREE_OPERAND (t, 1), depth);
15556 case tcc_unary:
15557 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
15559 case tcc_constant:
15560 case tcc_declaration:
15561 case tcc_reference:
15562 return integer_valued_real_single_p (t, depth);
15564 default:
15565 break;
15568 switch (code)
15570 case COND_EXPR:
15571 case SSA_NAME:
15572 return integer_valued_real_single_p (t, depth);
15574 case CALL_EXPR:
15576 tree arg0 = (call_expr_nargs (t) > 0
15577 ? CALL_EXPR_ARG (t, 0)
15578 : NULL_TREE);
15579 tree arg1 = (call_expr_nargs (t) > 1
15580 ? CALL_EXPR_ARG (t, 1)
15581 : NULL_TREE);
15582 return integer_valued_real_call_p (get_call_combined_fn (t),
15583 arg0, arg1, depth);
15586 default:
15587 return integer_valued_real_invalid_p (t, depth);
15591 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15592 attempt to fold the expression to a constant without modifying TYPE,
15593 OP0 or OP1.
15595 If the expression could be simplified to a constant, then return
15596 the constant. If the expression would not be simplified to a
15597 constant, then return NULL_TREE. */
15599 tree
15600 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15602 tree tem = fold_binary (code, type, op0, op1);
15603 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15606 /* Given the components of a unary expression CODE, TYPE and OP0,
15607 attempt to fold the expression to a constant without modifying
15608 TYPE or OP0.
15610 If the expression could be simplified to a constant, then return
15611 the constant. If the expression would not be simplified to a
15612 constant, then return NULL_TREE. */
15614 tree
15615 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15617 tree tem = fold_unary (code, type, op0);
15618 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15621 /* If EXP represents referencing an element in a constant string
15622 (either via pointer arithmetic or array indexing), return the
15623 tree representing the value accessed, otherwise return NULL. */
15625 tree
15626 fold_read_from_constant_string (tree exp)
15628 if ((TREE_CODE (exp) == INDIRECT_REF
15629 || TREE_CODE (exp) == ARRAY_REF)
15630 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15632 tree exp1 = TREE_OPERAND (exp, 0);
15633 tree index;
15634 tree string;
15635 location_t loc = EXPR_LOCATION (exp);
15637 if (TREE_CODE (exp) == INDIRECT_REF)
15638 string = string_constant (exp1, &index, NULL, NULL);
15639 else
15641 tree low_bound = array_ref_low_bound (exp);
15642 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15644 /* Optimize the special-case of a zero lower bound.
15646 We convert the low_bound to sizetype to avoid some problems
15647 with constant folding. (E.g. suppose the lower bound is 1,
15648 and its mode is QI. Without the conversion,l (ARRAY
15649 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15650 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15651 if (! integer_zerop (low_bound))
15652 index = size_diffop_loc (loc, index,
15653 fold_convert_loc (loc, sizetype, low_bound));
15655 string = exp1;
15658 scalar_int_mode char_mode;
15659 if (string
15660 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15661 && TREE_CODE (string) == STRING_CST
15662 && tree_fits_uhwi_p (index)
15663 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15664 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
15665 &char_mode)
15666 && GET_MODE_SIZE (char_mode) == 1)
15667 return build_int_cst_type (TREE_TYPE (exp),
15668 (TREE_STRING_POINTER (string)
15669 [TREE_INT_CST_LOW (index)]));
15671 return NULL;
15674 /* Folds a read from vector element at IDX of vector ARG. */
15676 tree
15677 fold_read_from_vector (tree arg, poly_uint64 idx)
15679 unsigned HOST_WIDE_INT i;
15680 if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
15681 && known_ge (idx, 0u)
15682 && idx.is_constant (&i))
15684 if (TREE_CODE (arg) == VECTOR_CST)
15685 return VECTOR_CST_ELT (arg, i);
15686 else if (TREE_CODE (arg) == CONSTRUCTOR)
15688 if (CONSTRUCTOR_NELTS (arg)
15689 && VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (arg, 0)->value)))
15690 return NULL_TREE;
15691 if (i >= CONSTRUCTOR_NELTS (arg))
15692 return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
15693 return CONSTRUCTOR_ELT (arg, i)->value;
15696 return NULL_TREE;
15699 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15700 an integer constant, real, or fixed-point constant.
15702 TYPE is the type of the result. */
15704 static tree
15705 fold_negate_const (tree arg0, tree type)
15707 tree t = NULL_TREE;
15709 switch (TREE_CODE (arg0))
15711 case REAL_CST:
15712 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15713 break;
15715 case FIXED_CST:
15717 FIXED_VALUE_TYPE f;
15718 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15719 &(TREE_FIXED_CST (arg0)), NULL,
15720 TYPE_SATURATING (type));
15721 t = build_fixed (type, f);
15722 /* Propagate overflow flags. */
15723 if (overflow_p | TREE_OVERFLOW (arg0))
15724 TREE_OVERFLOW (t) = 1;
15725 break;
15728 default:
15729 if (poly_int_tree_p (arg0))
15731 wi::overflow_type overflow;
15732 poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
15733 t = force_fit_type (type, res, 1,
15734 (overflow && ! TYPE_UNSIGNED (type))
15735 || TREE_OVERFLOW (arg0));
15736 break;
15739 gcc_unreachable ();
15742 return t;
15745 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15746 an integer constant or real constant.
15748 TYPE is the type of the result. */
15750 tree
15751 fold_abs_const (tree arg0, tree type)
15753 tree t = NULL_TREE;
15755 switch (TREE_CODE (arg0))
15757 case INTEGER_CST:
15759 /* If the value is unsigned or non-negative, then the absolute value
15760 is the same as the ordinary value. */
15761 wide_int val = wi::to_wide (arg0);
15762 wi::overflow_type overflow = wi::OVF_NONE;
15763 if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
15766 /* If the value is negative, then the absolute value is
15767 its negation. */
15768 else
15769 val = wi::neg (val, &overflow);
15771 /* Force to the destination type, set TREE_OVERFLOW for signed
15772 TYPE only. */
15773 t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
15775 break;
15777 case REAL_CST:
15778 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15779 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
15780 else
15781 t = arg0;
15782 break;
15784 default:
15785 gcc_unreachable ();
15788 return t;
15791 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15792 constant. TYPE is the type of the result. */
15794 static tree
15795 fold_not_const (const_tree arg0, tree type)
15797 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15799 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
15802 /* Given CODE, a relational operator, the target type, TYPE and two
15803 constant operands OP0 and OP1, return the result of the
15804 relational operation. If the result is not a compile time
15805 constant, then return NULL_TREE. */
15807 static tree
15808 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15810 int result, invert;
15812 /* From here on, the only cases we handle are when the result is
15813 known to be a constant. */
15815 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15817 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15818 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15820 /* Handle the cases where either operand is a NaN. */
15821 if (real_isnan (c0) || real_isnan (c1))
15823 switch (code)
15825 case EQ_EXPR:
15826 case ORDERED_EXPR:
15827 result = 0;
15828 break;
15830 case NE_EXPR:
15831 case UNORDERED_EXPR:
15832 case UNLT_EXPR:
15833 case UNLE_EXPR:
15834 case UNGT_EXPR:
15835 case UNGE_EXPR:
15836 case UNEQ_EXPR:
15837 result = 1;
15838 break;
15840 case LT_EXPR:
15841 case LE_EXPR:
15842 case GT_EXPR:
15843 case GE_EXPR:
15844 case LTGT_EXPR:
15845 if (flag_trapping_math)
15846 return NULL_TREE;
15847 result = 0;
15848 break;
15850 default:
15851 gcc_unreachable ();
15854 return constant_boolean_node (result, type);
15857 return constant_boolean_node (real_compare (code, c0, c1), type);
15860 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15862 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15863 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15864 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15867 /* Handle equality/inequality of complex constants. */
15868 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15870 tree rcond = fold_relational_const (code, type,
15871 TREE_REALPART (op0),
15872 TREE_REALPART (op1));
15873 tree icond = fold_relational_const (code, type,
15874 TREE_IMAGPART (op0),
15875 TREE_IMAGPART (op1));
15876 if (code == EQ_EXPR)
15877 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15878 else if (code == NE_EXPR)
15879 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15880 else
15881 return NULL_TREE;
15884 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
15886 if (!VECTOR_TYPE_P (type))
15888 /* Have vector comparison with scalar boolean result. */
15889 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
15890 && known_eq (VECTOR_CST_NELTS (op0),
15891 VECTOR_CST_NELTS (op1)));
15892 unsigned HOST_WIDE_INT nunits;
15893 if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
15894 return NULL_TREE;
15895 for (unsigned i = 0; i < nunits; i++)
15897 tree elem0 = VECTOR_CST_ELT (op0, i);
15898 tree elem1 = VECTOR_CST_ELT (op1, i);
15899 tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
15900 if (tmp == NULL_TREE)
15901 return NULL_TREE;
15902 if (integer_zerop (tmp))
15903 return constant_boolean_node (code == NE_EXPR, type);
15905 return constant_boolean_node (code == EQ_EXPR, type);
15907 tree_vector_builder elts;
15908 if (!elts.new_binary_operation (type, op0, op1, false))
15909 return NULL_TREE;
15910 unsigned int count = elts.encoded_nelts ();
15911 for (unsigned i = 0; i < count; i++)
15913 tree elem_type = TREE_TYPE (type);
15914 tree elem0 = VECTOR_CST_ELT (op0, i);
15915 tree elem1 = VECTOR_CST_ELT (op1, i);
15917 tree tem = fold_relational_const (code, elem_type,
15918 elem0, elem1);
15920 if (tem == NULL_TREE)
15921 return NULL_TREE;
15923 elts.quick_push (build_int_cst (elem_type,
15924 integer_zerop (tem) ? 0 : -1));
15927 return elts.build ();
15930 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15932 To compute GT, swap the arguments and do LT.
15933 To compute GE, do LT and invert the result.
15934 To compute LE, swap the arguments, do LT and invert the result.
15935 To compute NE, do EQ and invert the result.
15937 Therefore, the code below must handle only EQ and LT. */
15939 if (code == LE_EXPR || code == GT_EXPR)
15941 std::swap (op0, op1);
15942 code = swap_tree_comparison (code);
15945 /* Note that it is safe to invert for real values here because we
15946 have already handled the one case that it matters. */
15948 invert = 0;
15949 if (code == NE_EXPR || code == GE_EXPR)
15951 invert = 1;
15952 code = invert_tree_comparison (code, false);
15955 /* Compute a result for LT or EQ if args permit;
15956 Otherwise return T. */
15957 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15959 if (code == EQ_EXPR)
15960 result = tree_int_cst_equal (op0, op1);
15961 else
15962 result = tree_int_cst_lt (op0, op1);
15964 else
15965 return NULL_TREE;
15967 if (invert)
15968 result ^= 1;
15969 return constant_boolean_node (result, type);
15972 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15973 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15974 itself. */
15976 tree
15977 fold_build_cleanup_point_expr (tree type, tree expr)
15979 /* If the expression does not have side effects then we don't have to wrap
15980 it with a cleanup point expression. */
15981 if (!TREE_SIDE_EFFECTS (expr))
15982 return expr;
15984 /* If the expression is a return, check to see if the expression inside the
15985 return has no side effects or the right hand side of the modify expression
15986 inside the return. If either don't have side effects set we don't need to
15987 wrap the expression in a cleanup point expression. Note we don't check the
15988 left hand side of the modify because it should always be a return decl. */
15989 if (TREE_CODE (expr) == RETURN_EXPR)
15991 tree op = TREE_OPERAND (expr, 0);
15992 if (!op || !TREE_SIDE_EFFECTS (op))
15993 return expr;
15994 op = TREE_OPERAND (op, 1);
15995 if (!TREE_SIDE_EFFECTS (op))
15996 return expr;
15999 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
16002 /* Given a pointer value OP0 and a type TYPE, return a simplified version
16003 of an indirection through OP0, or NULL_TREE if no simplification is
16004 possible. */
16006 tree
16007 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16009 tree sub = op0;
16010 tree subtype;
16011 poly_uint64 const_op01;
16013 STRIP_NOPS (sub);
16014 subtype = TREE_TYPE (sub);
16015 if (!POINTER_TYPE_P (subtype)
16016 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
16017 return NULL_TREE;
16019 if (TREE_CODE (sub) == ADDR_EXPR)
16021 tree op = TREE_OPERAND (sub, 0);
16022 tree optype = TREE_TYPE (op);
16024 /* *&CONST_DECL -> to the value of the const decl. */
16025 if (TREE_CODE (op) == CONST_DECL)
16026 return DECL_INITIAL (op);
16027 /* *&p => p; make sure to handle *&"str"[cst] here. */
16028 if (type == optype)
16030 tree fop = fold_read_from_constant_string (op);
16031 if (fop)
16032 return fop;
16033 else
16034 return op;
16036 /* *(foo *)&fooarray => fooarray[0] */
16037 else if (TREE_CODE (optype) == ARRAY_TYPE
16038 && type == TREE_TYPE (optype)
16039 && (!in_gimple_form
16040 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16042 tree type_domain = TYPE_DOMAIN (optype);
16043 tree min_val = size_zero_node;
16044 if (type_domain && TYPE_MIN_VALUE (type_domain))
16045 min_val = TYPE_MIN_VALUE (type_domain);
16046 if (in_gimple_form
16047 && TREE_CODE (min_val) != INTEGER_CST)
16048 return NULL_TREE;
16049 return build4_loc (loc, ARRAY_REF, type, op, min_val,
16050 NULL_TREE, NULL_TREE);
16052 /* *(foo *)&complexfoo => __real__ complexfoo */
16053 else if (TREE_CODE (optype) == COMPLEX_TYPE
16054 && type == TREE_TYPE (optype))
16055 return fold_build1_loc (loc, REALPART_EXPR, type, op);
16056 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16057 else if (VECTOR_TYPE_P (optype)
16058 && type == TREE_TYPE (optype))
16060 tree part_width = TYPE_SIZE (type);
16061 tree index = bitsize_int (0);
16062 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
16063 index);
16067 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16068 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
16070 tree op00 = TREE_OPERAND (sub, 0);
16071 tree op01 = TREE_OPERAND (sub, 1);
16073 STRIP_NOPS (op00);
16074 if (TREE_CODE (op00) == ADDR_EXPR)
16076 tree op00type;
16077 op00 = TREE_OPERAND (op00, 0);
16078 op00type = TREE_TYPE (op00);
16080 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16081 if (VECTOR_TYPE_P (op00type)
16082 && type == TREE_TYPE (op00type)
16083 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
16084 but we want to treat offsets with MSB set as negative.
16085 For the code below negative offsets are invalid and
16086 TYPE_SIZE of the element is something unsigned, so
16087 check whether op01 fits into poly_int64, which implies
16088 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
16089 then just use poly_uint64 because we want to treat the
16090 value as unsigned. */
16091 && tree_fits_poly_int64_p (op01))
16093 tree part_width = TYPE_SIZE (type);
16094 poly_uint64 max_offset
16095 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
16096 * TYPE_VECTOR_SUBPARTS (op00type));
16097 if (known_lt (const_op01, max_offset))
16099 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
16100 return fold_build3_loc (loc,
16101 BIT_FIELD_REF, type, op00,
16102 part_width, index);
16105 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16106 else if (TREE_CODE (op00type) == COMPLEX_TYPE
16107 && type == TREE_TYPE (op00type))
16109 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
16110 const_op01))
16111 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
16113 /* ((foo *)&fooarray)[1] => fooarray[1] */
16114 else if (TREE_CODE (op00type) == ARRAY_TYPE
16115 && type == TREE_TYPE (op00type))
16117 tree type_domain = TYPE_DOMAIN (op00type);
16118 tree min_val = size_zero_node;
16119 if (type_domain && TYPE_MIN_VALUE (type_domain))
16120 min_val = TYPE_MIN_VALUE (type_domain);
16121 poly_uint64 type_size, index;
16122 if (poly_int_tree_p (min_val)
16123 && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
16124 && multiple_p (const_op01, type_size, &index))
16126 poly_offset_int off = index + wi::to_poly_offset (min_val);
16127 op01 = wide_int_to_tree (sizetype, off);
16128 return build4_loc (loc, ARRAY_REF, type, op00, op01,
16129 NULL_TREE, NULL_TREE);
16135 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16136 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16137 && type == TREE_TYPE (TREE_TYPE (subtype))
16138 && (!in_gimple_form
16139 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
16141 tree type_domain;
16142 tree min_val = size_zero_node;
16143 sub = build_fold_indirect_ref_loc (loc, sub);
16144 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16145 if (type_domain && TYPE_MIN_VALUE (type_domain))
16146 min_val = TYPE_MIN_VALUE (type_domain);
16147 if (in_gimple_form
16148 && TREE_CODE (min_val) != INTEGER_CST)
16149 return NULL_TREE;
16150 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
16151 NULL_TREE);
16154 return NULL_TREE;
16157 /* Builds an expression for an indirection through T, simplifying some
16158 cases. */
16160 tree
16161 build_fold_indirect_ref_loc (location_t loc, tree t)
16163 tree type = TREE_TYPE (TREE_TYPE (t));
16164 tree sub = fold_indirect_ref_1 (loc, type, t);
16166 if (sub)
16167 return sub;
16169 return build1_loc (loc, INDIRECT_REF, type, t);
16172 /* Given an INDIRECT_REF T, return either T or a simplified version. */
16174 tree
16175 fold_indirect_ref_loc (location_t loc, tree t)
16177 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16179 if (sub)
16180 return sub;
16181 else
16182 return t;
16185 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16186 whose result is ignored. The type of the returned tree need not be
16187 the same as the original expression. */
16189 tree
16190 fold_ignored_result (tree t)
16192 if (!TREE_SIDE_EFFECTS (t))
16193 return integer_zero_node;
16195 for (;;)
16196 switch (TREE_CODE_CLASS (TREE_CODE (t)))
16198 case tcc_unary:
16199 t = TREE_OPERAND (t, 0);
16200 break;
16202 case tcc_binary:
16203 case tcc_comparison:
16204 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16205 t = TREE_OPERAND (t, 0);
16206 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16207 t = TREE_OPERAND (t, 1);
16208 else
16209 return t;
16210 break;
16212 case tcc_expression:
16213 switch (TREE_CODE (t))
16215 case COMPOUND_EXPR:
16216 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16217 return t;
16218 t = TREE_OPERAND (t, 0);
16219 break;
16221 case COND_EXPR:
16222 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16223 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16224 return t;
16225 t = TREE_OPERAND (t, 0);
16226 break;
16228 default:
16229 return t;
16231 break;
16233 default:
16234 return t;
16238 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
16240 tree
16241 round_up_loc (location_t loc, tree value, unsigned int divisor)
16243 tree div = NULL_TREE;
16245 if (divisor == 1)
16246 return value;
16248 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16249 have to do anything. Only do this when we are not given a const,
16250 because in that case, this check is more expensive than just
16251 doing it. */
16252 if (TREE_CODE (value) != INTEGER_CST)
16254 div = build_int_cst (TREE_TYPE (value), divisor);
16256 if (multiple_of_p (TREE_TYPE (value), value, div))
16257 return value;
16260 /* If divisor is a power of two, simplify this to bit manipulation. */
16261 if (pow2_or_zerop (divisor))
16263 if (TREE_CODE (value) == INTEGER_CST)
16265 wide_int val = wi::to_wide (value);
16266 bool overflow_p;
16268 if ((val & (divisor - 1)) == 0)
16269 return value;
16271 overflow_p = TREE_OVERFLOW (value);
16272 val += divisor - 1;
16273 val &= (int) -divisor;
16274 if (val == 0)
16275 overflow_p = true;
16277 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
16279 else
16281 tree t;
16283 t = build_int_cst (TREE_TYPE (value), divisor - 1);
16284 value = size_binop_loc (loc, PLUS_EXPR, value, t);
16285 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
16286 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16289 else
16291 if (!div)
16292 div = build_int_cst (TREE_TYPE (value), divisor);
16293 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16294 value = size_binop_loc (loc, MULT_EXPR, value, div);
16297 return value;
16300 /* Likewise, but round down. */
16302 tree
16303 round_down_loc (location_t loc, tree value, int divisor)
16305 tree div = NULL_TREE;
16307 gcc_assert (divisor > 0);
16308 if (divisor == 1)
16309 return value;
16311 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16312 have to do anything. Only do this when we are not given a const,
16313 because in that case, this check is more expensive than just
16314 doing it. */
16315 if (TREE_CODE (value) != INTEGER_CST)
16317 div = build_int_cst (TREE_TYPE (value), divisor);
16319 if (multiple_of_p (TREE_TYPE (value), value, div))
16320 return value;
16323 /* If divisor is a power of two, simplify this to bit manipulation. */
16324 if (pow2_or_zerop (divisor))
16326 tree t;
16328 t = build_int_cst (TREE_TYPE (value), -divisor);
16329 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16331 else
16333 if (!div)
16334 div = build_int_cst (TREE_TYPE (value), divisor);
16335 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16336 value = size_binop_loc (loc, MULT_EXPR, value, div);
16339 return value;
16342 /* Returns the pointer to the base of the object addressed by EXP and
16343 extracts the information about the offset of the access, storing it
16344 to PBITPOS and POFFSET. */
16346 static tree
16347 split_address_to_core_and_offset (tree exp,
16348 poly_int64_pod *pbitpos, tree *poffset)
16350 tree core;
16351 machine_mode mode;
16352 int unsignedp, reversep, volatilep;
16353 poly_int64 bitsize;
16354 location_t loc = EXPR_LOCATION (exp);
16356 if (TREE_CODE (exp) == SSA_NAME)
16357 if (gassign *def = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (exp)))
16358 if (gimple_assign_rhs_code (def) == ADDR_EXPR)
16359 exp = gimple_assign_rhs1 (def);
16361 if (TREE_CODE (exp) == ADDR_EXPR)
16363 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16364 poffset, &mode, &unsignedp, &reversep,
16365 &volatilep);
16366 core = build_fold_addr_expr_loc (loc, core);
16368 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
16370 core = TREE_OPERAND (exp, 0);
16371 STRIP_NOPS (core);
16372 *pbitpos = 0;
16373 *poffset = TREE_OPERAND (exp, 1);
16374 if (poly_int_tree_p (*poffset))
16376 poly_offset_int tem
16377 = wi::sext (wi::to_poly_offset (*poffset),
16378 TYPE_PRECISION (TREE_TYPE (*poffset)));
16379 tem <<= LOG2_BITS_PER_UNIT;
16380 if (tem.to_shwi (pbitpos))
16381 *poffset = NULL_TREE;
16384 else
16386 core = exp;
16387 *pbitpos = 0;
16388 *poffset = NULL_TREE;
16391 return core;
16394 /* Returns true if addresses of E1 and E2 differ by a constant, false
16395 otherwise. If they do, E1 - E2 is stored in *DIFF. */
16397 bool
16398 ptr_difference_const (tree e1, tree e2, poly_int64_pod *diff)
16400 tree core1, core2;
16401 poly_int64 bitpos1, bitpos2;
16402 tree toffset1, toffset2, tdiff, type;
16404 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16405 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16407 poly_int64 bytepos1, bytepos2;
16408 if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
16409 || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
16410 || !operand_equal_p (core1, core2, 0))
16411 return false;
16413 if (toffset1 && toffset2)
16415 type = TREE_TYPE (toffset1);
16416 if (type != TREE_TYPE (toffset2))
16417 toffset2 = fold_convert (type, toffset2);
16419 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16420 if (!cst_and_fits_in_hwi (tdiff))
16421 return false;
16423 *diff = int_cst_value (tdiff);
16425 else if (toffset1 || toffset2)
16427 /* If only one of the offsets is non-constant, the difference cannot
16428 be a constant. */
16429 return false;
16431 else
16432 *diff = 0;
16434 *diff += bytepos1 - bytepos2;
16435 return true;
16438 /* Return OFF converted to a pointer offset type suitable as offset for
16439 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
16440 tree
16441 convert_to_ptrofftype_loc (location_t loc, tree off)
16443 if (ptrofftype_p (TREE_TYPE (off)))
16444 return off;
16445 return fold_convert_loc (loc, sizetype, off);
16448 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16449 tree
16450 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
16452 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16453 ptr, convert_to_ptrofftype_loc (loc, off));
16456 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
16457 tree
16458 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
16460 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
16461 ptr, size_int (off));
16464 /* Return a pointer to a NUL-terminated string containing the sequence
16465 of bytes corresponding to the representation of the object referred to
16466 by SRC (or a subsequence of such bytes within it if SRC is a reference
16467 to an initialized constant array plus some constant offset).
16468 Set *STRSIZE the number of bytes in the constant sequence including
16469 the terminating NUL byte. *STRSIZE is equal to sizeof(A) - OFFSET
16470 where A is the array that stores the constant sequence that SRC points
16471 to and OFFSET is the byte offset of SRC from the beginning of A. SRC
16472 need not point to a string or even an array of characters but may point
16473 to an object of any type. */
16475 const char *
16476 getbyterep (tree src, unsigned HOST_WIDE_INT *strsize)
16478 /* The offset into the array A storing the string, and A's byte size. */
16479 tree offset_node;
16480 tree mem_size;
16482 if (strsize)
16483 *strsize = 0;
16485 if (strsize)
16486 src = byte_representation (src, &offset_node, &mem_size, NULL);
16487 else
16488 src = string_constant (src, &offset_node, &mem_size, NULL);
16489 if (!src)
16490 return NULL;
16492 unsigned HOST_WIDE_INT offset = 0;
16493 if (offset_node != NULL_TREE)
16495 if (!tree_fits_uhwi_p (offset_node))
16496 return NULL;
16497 else
16498 offset = tree_to_uhwi (offset_node);
16501 if (!tree_fits_uhwi_p (mem_size))
16502 return NULL;
16504 /* ARRAY_SIZE is the byte size of the array the constant sequence
16505 is stored in and equal to sizeof A. INIT_BYTES is the number
16506 of bytes in the constant sequence used to initialize the array,
16507 including any embedded NULs as well as the terminating NUL (for
16508 strings), but not including any trailing zeros/NULs past
16509 the terminating one appended implicitly to a string literal to
16510 zero out the remainder of the array it's stored in. For example,
16511 given:
16512 const char a[7] = "abc\0d";
16513 n = strlen (a + 1);
16514 ARRAY_SIZE is 7, INIT_BYTES is 6, and OFFSET is 1. For a valid
16515 (i.e., nul-terminated) string with no embedded nuls, INIT_BYTES
16516 is equal to strlen (A) + 1. */
16517 const unsigned HOST_WIDE_INT array_size = tree_to_uhwi (mem_size);
16518 unsigned HOST_WIDE_INT init_bytes = TREE_STRING_LENGTH (src);
16519 const char *string = TREE_STRING_POINTER (src);
16521 /* Ideally this would turn into a gcc_checking_assert over time. */
16522 if (init_bytes > array_size)
16523 init_bytes = array_size;
16525 if (init_bytes == 0 || offset >= array_size)
16526 return NULL;
16528 if (strsize)
16530 /* Compute and store the number of characters from the beginning
16531 of the substring at OFFSET to the end, including the terminating
16532 nul. Offsets past the initial length refer to null strings. */
16533 if (offset < init_bytes)
16534 *strsize = init_bytes - offset;
16535 else
16536 *strsize = 1;
16538 else
16540 tree eltype = TREE_TYPE (TREE_TYPE (src));
16541 /* Support only properly NUL-terminated single byte strings. */
16542 if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
16543 return NULL;
16544 if (string[init_bytes - 1] != '\0')
16545 return NULL;
16548 return offset < init_bytes ? string + offset : "";
16551 /* Return a pointer to a NUL-terminated string corresponding to
16552 the expression STR referencing a constant string, possibly
16553 involving a constant offset. Return null if STR either doesn't
16554 reference a constant string or if it involves a nonconstant
16555 offset. */
16557 const char *
16558 c_getstr (tree str)
16560 return getbyterep (str, NULL);
16563 /* Given a tree T, compute which bits in T may be nonzero. */
16565 wide_int
16566 tree_nonzero_bits (const_tree t)
16568 switch (TREE_CODE (t))
16570 case INTEGER_CST:
16571 return wi::to_wide (t);
16572 case SSA_NAME:
16573 return get_nonzero_bits (t);
16574 case NON_LVALUE_EXPR:
16575 case SAVE_EXPR:
16576 return tree_nonzero_bits (TREE_OPERAND (t, 0));
16577 case BIT_AND_EXPR:
16578 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16579 tree_nonzero_bits (TREE_OPERAND (t, 1)));
16580 case BIT_IOR_EXPR:
16581 case BIT_XOR_EXPR:
16582 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16583 tree_nonzero_bits (TREE_OPERAND (t, 1)));
16584 case COND_EXPR:
16585 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1)),
16586 tree_nonzero_bits (TREE_OPERAND (t, 2)));
16587 CASE_CONVERT:
16588 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t, 0)),
16589 TYPE_PRECISION (TREE_TYPE (t)),
16590 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0))));
16591 case PLUS_EXPR:
16592 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
16594 wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0));
16595 wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1));
16596 if (wi::bit_and (nzbits1, nzbits2) == 0)
16597 return wi::bit_or (nzbits1, nzbits2);
16599 break;
16600 case LSHIFT_EXPR:
16601 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16603 tree type = TREE_TYPE (t);
16604 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16605 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16606 TYPE_PRECISION (type));
16607 return wi::neg_p (arg1)
16608 ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
16609 : wi::lshift (nzbits, arg1);
16611 break;
16612 case RSHIFT_EXPR:
16613 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
16615 tree type = TREE_TYPE (t);
16616 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
16617 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
16618 TYPE_PRECISION (type));
16619 return wi::neg_p (arg1)
16620 ? wi::lshift (nzbits, -arg1)
16621 : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
16623 break;
16624 default:
16625 break;
16628 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t)));
16631 /* Helper function for address compare simplifications in match.pd.
16632 OP0 and OP1 are ADDR_EXPR operands being compared by CODE.
16633 TYPE is the type of comparison operands.
16634 BASE0, BASE1, OFF0 and OFF1 are set by the function.
16635 GENERIC is true if GENERIC folding and false for GIMPLE folding.
16636 Returns 0 if OP0 is known to be unequal to OP1 regardless of OFF{0,1},
16637 1 if bases are known to be equal and OP0 cmp OP1 depends on OFF0 cmp OFF1,
16638 and 2 if unknown. */
16641 address_compare (tree_code code, tree type, tree op0, tree op1,
16642 tree &base0, tree &base1, poly_int64 &off0, poly_int64 &off1,
16643 bool generic)
16645 if (TREE_CODE (op0) == SSA_NAME)
16646 op0 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op0));
16647 if (TREE_CODE (op1) == SSA_NAME)
16648 op1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (op1));
16649 gcc_checking_assert (TREE_CODE (op0) == ADDR_EXPR);
16650 gcc_checking_assert (TREE_CODE (op1) == ADDR_EXPR);
16651 base0 = get_addr_base_and_unit_offset (TREE_OPERAND (op0, 0), &off0);
16652 base1 = get_addr_base_and_unit_offset (TREE_OPERAND (op1, 0), &off1);
16653 if (base0 && TREE_CODE (base0) == MEM_REF)
16655 off0 += mem_ref_offset (base0).force_shwi ();
16656 base0 = TREE_OPERAND (base0, 0);
16658 if (base1 && TREE_CODE (base1) == MEM_REF)
16660 off1 += mem_ref_offset (base1).force_shwi ();
16661 base1 = TREE_OPERAND (base1, 0);
16663 if (base0 == NULL_TREE || base1 == NULL_TREE)
16664 return 2;
16666 int equal = 2;
16667 /* Punt in GENERIC on variables with value expressions;
16668 the value expressions might point to fields/elements
16669 of other vars etc. */
16670 if (generic
16671 && ((VAR_P (base0) && DECL_HAS_VALUE_EXPR_P (base0))
16672 || (VAR_P (base1) && DECL_HAS_VALUE_EXPR_P (base1))))
16673 return 2;
16674 else if (decl_in_symtab_p (base0) && decl_in_symtab_p (base1))
16676 symtab_node *node0 = symtab_node::get_create (base0);
16677 symtab_node *node1 = symtab_node::get_create (base1);
16678 equal = node0->equal_address_to (node1);
16680 else if ((DECL_P (base0)
16681 || TREE_CODE (base0) == SSA_NAME
16682 || TREE_CODE (base0) == STRING_CST)
16683 && (DECL_P (base1)
16684 || TREE_CODE (base1) == SSA_NAME
16685 || TREE_CODE (base1) == STRING_CST))
16686 equal = (base0 == base1);
16687 /* Assume different STRING_CSTs with the same content will be
16688 merged. */
16689 if (equal == 0
16690 && TREE_CODE (base0) == STRING_CST
16691 && TREE_CODE (base1) == STRING_CST
16692 && TREE_STRING_LENGTH (base0) == TREE_STRING_LENGTH (base1)
16693 && memcmp (TREE_STRING_POINTER (base0), TREE_STRING_POINTER (base1),
16694 TREE_STRING_LENGTH (base0)) == 0)
16695 equal = 1;
16696 if (equal == 1)
16698 if (code == EQ_EXPR
16699 || code == NE_EXPR
16700 /* If the offsets are equal we can ignore overflow. */
16701 || known_eq (off0, off1)
16702 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
16703 /* Or if we compare using pointers to decls or strings. */
16704 || (POINTER_TYPE_P (type)
16705 && (DECL_P (base0) || TREE_CODE (base0) == STRING_CST)))
16706 return 1;
16707 return 2;
16709 if (equal != 0)
16710 return equal;
16711 if (code != EQ_EXPR && code != NE_EXPR)
16712 return 2;
16714 /* At this point we know (or assume) the two pointers point at
16715 different objects. */
16716 HOST_WIDE_INT ioff0 = -1, ioff1 = -1;
16717 off0.is_constant (&ioff0);
16718 off1.is_constant (&ioff1);
16719 /* Punt on non-zero offsets from functions. */
16720 if ((TREE_CODE (base0) == FUNCTION_DECL && ioff0)
16721 || (TREE_CODE (base1) == FUNCTION_DECL && ioff1))
16722 return 2;
16723 /* Or if the bases are neither decls nor string literals. */
16724 if (!DECL_P (base0) && TREE_CODE (base0) != STRING_CST)
16725 return 2;
16726 if (!DECL_P (base1) && TREE_CODE (base1) != STRING_CST)
16727 return 2;
16728 /* For initializers, assume addresses of different functions are
16729 different. */
16730 if (folding_initializer
16731 && TREE_CODE (base0) == FUNCTION_DECL
16732 && TREE_CODE (base1) == FUNCTION_DECL)
16733 return 0;
16735 /* Compute whether one address points to the start of one
16736 object and another one to the end of another one. */
16737 poly_int64 size0 = 0, size1 = 0;
16738 if (TREE_CODE (base0) == STRING_CST)
16740 if (ioff0 < 0 || ioff0 > TREE_STRING_LENGTH (base0))
16741 equal = 2;
16742 else
16743 size0 = TREE_STRING_LENGTH (base0);
16745 else if (TREE_CODE (base0) == FUNCTION_DECL)
16746 size0 = 1;
16747 else
16749 tree sz0 = DECL_SIZE_UNIT (base0);
16750 if (!tree_fits_poly_int64_p (sz0))
16751 equal = 2;
16752 else
16753 size0 = tree_to_poly_int64 (sz0);
16755 if (TREE_CODE (base1) == STRING_CST)
16757 if (ioff1 < 0 || ioff1 > TREE_STRING_LENGTH (base1))
16758 equal = 2;
16759 else
16760 size1 = TREE_STRING_LENGTH (base1);
16762 else if (TREE_CODE (base1) == FUNCTION_DECL)
16763 size1 = 1;
16764 else
16766 tree sz1 = DECL_SIZE_UNIT (base1);
16767 if (!tree_fits_poly_int64_p (sz1))
16768 equal = 2;
16769 else
16770 size1 = tree_to_poly_int64 (sz1);
16772 if (equal == 0)
16774 /* If one offset is pointing (or could be) to the beginning of one
16775 object and the other is pointing to one past the last byte of the
16776 other object, punt. */
16777 if (maybe_eq (off0, 0) && maybe_eq (off1, size1))
16778 equal = 2;
16779 else if (maybe_eq (off1, 0) && maybe_eq (off0, size0))
16780 equal = 2;
16781 /* If both offsets are the same, there are some cases we know that are
16782 ok. Either if we know they aren't zero, or if we know both sizes
16783 are no zero. */
16784 if (equal == 2
16785 && known_eq (off0, off1)
16786 && (known_ne (off0, 0)
16787 || (known_ne (size0, 0) && known_ne (size1, 0))))
16788 equal = 0;
16791 /* At this point, equal is 2 if either one or both pointers are out of
16792 bounds of their object, or one points to start of its object and the
16793 other points to end of its object. This is unspecified behavior
16794 e.g. in C++. Otherwise equal is 0. */
16795 if (folding_cxx_constexpr && equal)
16796 return equal;
16798 /* When both pointers point to string literals, even when equal is 0,
16799 due to tail merging of string literals the pointers might be the same. */
16800 if (TREE_CODE (base0) == STRING_CST && TREE_CODE (base1) == STRING_CST)
16802 if (ioff0 < 0
16803 || ioff1 < 0
16804 || ioff0 > TREE_STRING_LENGTH (base0)
16805 || ioff1 > TREE_STRING_LENGTH (base1))
16806 return 2;
16808 /* If the bytes in the string literals starting at the pointers
16809 differ, the pointers need to be different. */
16810 if (memcmp (TREE_STRING_POINTER (base0) + ioff0,
16811 TREE_STRING_POINTER (base1) + ioff1,
16812 MIN (TREE_STRING_LENGTH (base0) - ioff0,
16813 TREE_STRING_LENGTH (base1) - ioff1)) == 0)
16815 HOST_WIDE_INT ioffmin = MIN (ioff0, ioff1);
16816 if (memcmp (TREE_STRING_POINTER (base0) + ioff0 - ioffmin,
16817 TREE_STRING_POINTER (base1) + ioff1 - ioffmin,
16818 ioffmin) == 0)
16819 /* If even the bytes in the string literal before the
16820 pointers are the same, the string literals could be
16821 tail merged. */
16822 return 2;
16824 return 0;
16827 if (folding_cxx_constexpr)
16828 return 0;
16830 /* If this is a pointer comparison, ignore for now even
16831 valid equalities where one pointer is the offset zero
16832 of one object and the other to one past end of another one. */
16833 if (!INTEGRAL_TYPE_P (type))
16834 return 0;
16836 /* Assume that string literals can't be adjacent to variables
16837 (automatic or global). */
16838 if (TREE_CODE (base0) == STRING_CST || TREE_CODE (base1) == STRING_CST)
16839 return 0;
16841 /* Assume that automatic variables can't be adjacent to global
16842 variables. */
16843 if (is_global_var (base0) != is_global_var (base1))
16844 return 0;
16846 return equal;
16849 /* Return the single non-zero element of a CONSTRUCTOR or NULL_TREE. */
16850 tree
16851 ctor_single_nonzero_element (const_tree t)
16853 unsigned HOST_WIDE_INT idx;
16854 constructor_elt *ce;
16855 tree elt = NULL_TREE;
16857 if (TREE_CODE (t) != CONSTRUCTOR)
16858 return NULL_TREE;
16859 for (idx = 0; vec_safe_iterate (CONSTRUCTOR_ELTS (t), idx, &ce); idx++)
16860 if (!integer_zerop (ce->value) && !real_zerop (ce->value))
16862 if (elt)
16863 return NULL_TREE;
16864 elt = ce->value;
16866 return elt;
16869 #if CHECKING_P
16871 namespace selftest {
16873 /* Helper functions for writing tests of folding trees. */
16875 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
16877 static void
16878 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
16879 tree constant)
16881 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
16884 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
16885 wrapping WRAPPED_EXPR. */
16887 static void
16888 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
16889 tree wrapped_expr)
16891 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
16892 ASSERT_NE (wrapped_expr, result);
16893 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
16894 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
16897 /* Verify that various arithmetic binary operations are folded
16898 correctly. */
16900 static void
16901 test_arithmetic_folding ()
16903 tree type = integer_type_node;
16904 tree x = create_tmp_var_raw (type, "x");
16905 tree zero = build_zero_cst (type);
16906 tree one = build_int_cst (type, 1);
16908 /* Addition. */
16909 /* 1 <-- (0 + 1) */
16910 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
16911 one);
16912 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
16913 one);
16915 /* (nonlvalue)x <-- (x + 0) */
16916 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
16919 /* Subtraction. */
16920 /* 0 <-- (x - x) */
16921 assert_binop_folds_to_const (x, MINUS_EXPR, x,
16922 zero);
16923 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
16926 /* Multiplication. */
16927 /* 0 <-- (x * 0) */
16928 assert_binop_folds_to_const (x, MULT_EXPR, zero,
16929 zero);
16931 /* (nonlvalue)x <-- (x * 1) */
16932 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
16936 /* Verify that various binary operations on vectors are folded
16937 correctly. */
16939 static void
16940 test_vector_folding ()
16942 tree inner_type = integer_type_node;
16943 tree type = build_vector_type (inner_type, 4);
16944 tree zero = build_zero_cst (type);
16945 tree one = build_one_cst (type);
16946 tree index = build_index_vector (type, 0, 1);
16948 /* Verify equality tests that return a scalar boolean result. */
16949 tree res_type = boolean_type_node;
16950 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
16951 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
16952 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
16953 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
16954 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
16955 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
16956 index, one)));
16957 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
16958 index, index)));
16959 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
16960 index, index)));
16963 /* Verify folding of VEC_DUPLICATE_EXPRs. */
16965 static void
16966 test_vec_duplicate_folding ()
16968 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
16969 machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
16970 /* This will be 1 if VEC_MODE isn't a vector mode. */
16971 poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
16973 tree type = build_vector_type (ssizetype, nunits);
16974 tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
16975 tree dup5_cst = build_vector_from_val (type, ssize_int (5));
16976 ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
16979 /* Run all of the selftests within this file. */
16981 void
16982 fold_const_cc_tests ()
16984 test_arithmetic_folding ();
16985 test_vector_folding ();
16986 test_vec_duplicate_folding ();
16989 } // namespace selftest
16991 #endif /* CHECKING_P */