PR middle-end/77357 - strlen of constant strings not folded
[official-gcc.git] / gcc / fold-const.c
blob97c435fa5e0dcf4345b10ea214ffc244cd6e2b7d
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
43 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "backend.h"
47 #include "target.h"
48 #include "rtl.h"
49 #include "tree.h"
50 #include "gimple.h"
51 #include "predict.h"
52 #include "memmodel.h"
53 #include "tm_p.h"
54 #include "tree-ssa-operands.h"
55 #include "optabs-query.h"
56 #include "cgraph.h"
57 #include "diagnostic-core.h"
58 #include "flags.h"
59 #include "alias.h"
60 #include "fold-const.h"
61 #include "fold-const-call.h"
62 #include "stor-layout.h"
63 #include "calls.h"
64 #include "tree-iterator.h"
65 #include "expr.h"
66 #include "intl.h"
67 #include "langhooks.h"
68 #include "tree-eh.h"
69 #include "gimplify.h"
70 #include "tree-dfa.h"
71 #include "builtins.h"
72 #include "generic-match.h"
73 #include "gimple-fold.h"
74 #include "params.h"
75 #include "tree-into-ssa.h"
76 #include "md5.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
79 #include "tree-vrp.h"
80 #include "tree-ssanames.h"
81 #include "selftest.h"
82 #include "stringpool.h"
83 #include "attribs.h"
84 #include "tree-vector-builder.h"
85 #include "vec-perm-indices.h"
87 /* Nonzero if we are folding constants inside an initializer; zero
88 otherwise. */
89 int folding_initializer = 0;
91 /* The following constants represent a bit based encoding of GCC's
92 comparison operators. This encoding simplifies transformations
93 on relational comparison operators, such as AND and OR. */
94 enum comparison_code {
95 COMPCODE_FALSE = 0,
96 COMPCODE_LT = 1,
97 COMPCODE_EQ = 2,
98 COMPCODE_LE = 3,
99 COMPCODE_GT = 4,
100 COMPCODE_LTGT = 5,
101 COMPCODE_GE = 6,
102 COMPCODE_ORD = 7,
103 COMPCODE_UNORD = 8,
104 COMPCODE_UNLT = 9,
105 COMPCODE_UNEQ = 10,
106 COMPCODE_UNLE = 11,
107 COMPCODE_UNGT = 12,
108 COMPCODE_NE = 13,
109 COMPCODE_UNGE = 14,
110 COMPCODE_TRUE = 15
113 static bool negate_expr_p (tree);
114 static tree negate_expr (tree);
115 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
116 static enum comparison_code comparison_to_compcode (enum tree_code);
117 static enum tree_code compcode_to_comparison (enum comparison_code);
118 static int twoval_comparison_p (tree, tree *, tree *);
119 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
120 static tree optimize_bit_field_compare (location_t, enum tree_code,
121 tree, tree, tree);
122 static int simple_operand_p (const_tree);
123 static bool simple_operand_p_2 (tree);
124 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
125 static tree range_predecessor (tree);
126 static tree range_successor (tree);
127 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
128 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
129 static tree unextend (tree, int, int, tree);
130 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
131 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
132 static tree fold_binary_op_with_conditional_arg (location_t,
133 enum tree_code, tree,
134 tree, tree,
135 tree, tree, int);
136 static tree fold_negate_const (tree, tree);
137 static tree fold_not_const (const_tree, tree);
138 static tree fold_relational_const (enum tree_code, tree, tree, tree);
139 static tree fold_convert_const (enum tree_code, tree, tree);
140 static tree fold_view_convert_expr (tree, tree);
141 static tree fold_negate_expr (location_t, tree);
144 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
145 Otherwise, return LOC. */
147 static location_t
148 expr_location_or (tree t, location_t loc)
150 location_t tloc = EXPR_LOCATION (t);
151 return tloc == UNKNOWN_LOCATION ? loc : tloc;
154 /* Similar to protected_set_expr_location, but never modify x in place,
155 if location can and needs to be set, unshare it. */
157 static inline tree
158 protected_set_expr_location_unshare (tree x, location_t loc)
160 if (CAN_HAVE_LOCATION_P (x)
161 && EXPR_LOCATION (x) != loc
162 && !(TREE_CODE (x) == SAVE_EXPR
163 || TREE_CODE (x) == TARGET_EXPR
164 || TREE_CODE (x) == BIND_EXPR))
166 x = copy_node (x);
167 SET_EXPR_LOCATION (x, loc);
169 return x;
172 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
173 division and returns the quotient. Otherwise returns
174 NULL_TREE. */
176 tree
177 div_if_zero_remainder (const_tree arg1, const_tree arg2)
179 widest_int quo;
181 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
182 SIGNED, &quo))
183 return wide_int_to_tree (TREE_TYPE (arg1), quo);
185 return NULL_TREE;
188 /* This is nonzero if we should defer warnings about undefined
189 overflow. This facility exists because these warnings are a
190 special case. The code to estimate loop iterations does not want
191 to issue any warnings, since it works with expressions which do not
192 occur in user code. Various bits of cleanup code call fold(), but
193 only use the result if it has certain characteristics (e.g., is a
194 constant); that code only wants to issue a warning if the result is
195 used. */
197 static int fold_deferring_overflow_warnings;
199 /* If a warning about undefined overflow is deferred, this is the
200 warning. Note that this may cause us to turn two warnings into
201 one, but that is fine since it is sufficient to only give one
202 warning per expression. */
204 static const char* fold_deferred_overflow_warning;
206 /* If a warning about undefined overflow is deferred, this is the
207 level at which the warning should be emitted. */
209 static enum warn_strict_overflow_code fold_deferred_overflow_code;
211 /* Start deferring overflow warnings. We could use a stack here to
212 permit nested calls, but at present it is not necessary. */
214 void
215 fold_defer_overflow_warnings (void)
217 ++fold_deferring_overflow_warnings;
220 /* Stop deferring overflow warnings. If there is a pending warning,
221 and ISSUE is true, then issue the warning if appropriate. STMT is
222 the statement with which the warning should be associated (used for
223 location information); STMT may be NULL. CODE is the level of the
224 warning--a warn_strict_overflow_code value. This function will use
225 the smaller of CODE and the deferred code when deciding whether to
226 issue the warning. CODE may be zero to mean to always use the
227 deferred code. */
229 void
230 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
232 const char *warnmsg;
233 location_t locus;
235 gcc_assert (fold_deferring_overflow_warnings > 0);
236 --fold_deferring_overflow_warnings;
237 if (fold_deferring_overflow_warnings > 0)
239 if (fold_deferred_overflow_warning != NULL
240 && code != 0
241 && code < (int) fold_deferred_overflow_code)
242 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
243 return;
246 warnmsg = fold_deferred_overflow_warning;
247 fold_deferred_overflow_warning = NULL;
249 if (!issue || warnmsg == NULL)
250 return;
252 if (gimple_no_warning_p (stmt))
253 return;
255 /* Use the smallest code level when deciding to issue the
256 warning. */
257 if (code == 0 || code > (int) fold_deferred_overflow_code)
258 code = fold_deferred_overflow_code;
260 if (!issue_strict_overflow_warning (code))
261 return;
263 if (stmt == NULL)
264 locus = input_location;
265 else
266 locus = gimple_location (stmt);
267 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
270 /* Stop deferring overflow warnings, ignoring any deferred
271 warnings. */
273 void
274 fold_undefer_and_ignore_overflow_warnings (void)
276 fold_undefer_overflow_warnings (false, NULL, 0);
279 /* Whether we are deferring overflow warnings. */
281 bool
282 fold_deferring_overflow_warnings_p (void)
284 return fold_deferring_overflow_warnings > 0;
287 /* This is called when we fold something based on the fact that signed
288 overflow is undefined. */
290 void
291 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
293 if (fold_deferring_overflow_warnings > 0)
295 if (fold_deferred_overflow_warning == NULL
296 || wc < fold_deferred_overflow_code)
298 fold_deferred_overflow_warning = gmsgid;
299 fold_deferred_overflow_code = wc;
302 else if (issue_strict_overflow_warning (wc))
303 warning (OPT_Wstrict_overflow, gmsgid);
306 /* Return true if the built-in mathematical function specified by CODE
307 is odd, i.e. -f(x) == f(-x). */
309 bool
310 negate_mathfn_p (combined_fn fn)
312 switch (fn)
314 CASE_CFN_ASIN:
315 CASE_CFN_ASINH:
316 CASE_CFN_ATAN:
317 CASE_CFN_ATANH:
318 CASE_CFN_CASIN:
319 CASE_CFN_CASINH:
320 CASE_CFN_CATAN:
321 CASE_CFN_CATANH:
322 CASE_CFN_CBRT:
323 CASE_CFN_CPROJ:
324 CASE_CFN_CSIN:
325 CASE_CFN_CSINH:
326 CASE_CFN_CTAN:
327 CASE_CFN_CTANH:
328 CASE_CFN_ERF:
329 CASE_CFN_LLROUND:
330 CASE_CFN_LROUND:
331 CASE_CFN_ROUND:
332 CASE_CFN_SIN:
333 CASE_CFN_SINH:
334 CASE_CFN_TAN:
335 CASE_CFN_TANH:
336 CASE_CFN_TRUNC:
337 return true;
339 CASE_CFN_LLRINT:
340 CASE_CFN_LRINT:
341 CASE_CFN_NEARBYINT:
342 CASE_CFN_RINT:
343 return !flag_rounding_math;
345 default:
346 break;
348 return false;
351 /* Check whether we may negate an integer constant T without causing
352 overflow. */
354 bool
355 may_negate_without_overflow_p (const_tree t)
357 tree type;
359 gcc_assert (TREE_CODE (t) == INTEGER_CST);
361 type = TREE_TYPE (t);
362 if (TYPE_UNSIGNED (type))
363 return false;
365 return !wi::only_sign_bit_p (wi::to_wide (t));
368 /* Determine whether an expression T can be cheaply negated using
369 the function negate_expr without introducing undefined overflow. */
371 static bool
372 negate_expr_p (tree t)
374 tree type;
376 if (t == 0)
377 return false;
379 type = TREE_TYPE (t);
381 STRIP_SIGN_NOPS (t);
382 switch (TREE_CODE (t))
384 case INTEGER_CST:
385 if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
386 return true;
388 /* Check that -CST will not overflow type. */
389 return may_negate_without_overflow_p (t);
390 case BIT_NOT_EXPR:
391 return (INTEGRAL_TYPE_P (type)
392 && TYPE_OVERFLOW_WRAPS (type));
394 case FIXED_CST:
395 return true;
397 case NEGATE_EXPR:
398 return !TYPE_OVERFLOW_SANITIZED (type);
400 case REAL_CST:
401 /* We want to canonicalize to positive real constants. Pretend
402 that only negative ones can be easily negated. */
403 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
405 case COMPLEX_CST:
406 return negate_expr_p (TREE_REALPART (t))
407 && negate_expr_p (TREE_IMAGPART (t));
409 case VECTOR_CST:
411 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
412 return true;
414 /* Steps don't prevent negation. */
415 unsigned int count = vector_cst_encoded_nelts (t);
416 for (unsigned int i = 0; i < count; ++i)
417 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
418 return false;
420 return true;
423 case COMPLEX_EXPR:
424 return negate_expr_p (TREE_OPERAND (t, 0))
425 && negate_expr_p (TREE_OPERAND (t, 1));
427 case CONJ_EXPR:
428 return negate_expr_p (TREE_OPERAND (t, 0));
430 case PLUS_EXPR:
431 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
432 || HONOR_SIGNED_ZEROS (element_mode (type))
433 || (ANY_INTEGRAL_TYPE_P (type)
434 && ! TYPE_OVERFLOW_WRAPS (type)))
435 return false;
436 /* -(A + B) -> (-B) - A. */
437 if (negate_expr_p (TREE_OPERAND (t, 1)))
438 return true;
439 /* -(A + B) -> (-A) - B. */
440 return negate_expr_p (TREE_OPERAND (t, 0));
442 case MINUS_EXPR:
443 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
444 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
445 && !HONOR_SIGNED_ZEROS (element_mode (type))
446 && (! ANY_INTEGRAL_TYPE_P (type)
447 || TYPE_OVERFLOW_WRAPS (type));
449 case MULT_EXPR:
450 if (TYPE_UNSIGNED (type))
451 break;
452 /* INT_MIN/n * n doesn't overflow while negating one operand it does
453 if n is a (negative) power of two. */
454 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
455 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
456 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
457 && (wi::popcount
458 (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
459 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
460 && (wi::popcount
461 (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
462 break;
464 /* Fall through. */
466 case RDIV_EXPR:
467 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
468 return negate_expr_p (TREE_OPERAND (t, 1))
469 || negate_expr_p (TREE_OPERAND (t, 0));
470 break;
472 case TRUNC_DIV_EXPR:
473 case ROUND_DIV_EXPR:
474 case EXACT_DIV_EXPR:
475 if (TYPE_UNSIGNED (type))
476 break;
477 /* In general we can't negate A in A / B, because if A is INT_MIN and
478 B is not 1 we change the sign of the result. */
479 if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
480 && negate_expr_p (TREE_OPERAND (t, 0)))
481 return true;
482 /* In general we can't negate B in A / B, because if A is INT_MIN and
483 B is 1, we may turn this into INT_MIN / -1 which is undefined
484 and actually traps on some architectures. */
485 if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
486 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
487 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
488 && ! integer_onep (TREE_OPERAND (t, 1))))
489 return negate_expr_p (TREE_OPERAND (t, 1));
490 break;
492 case NOP_EXPR:
493 /* Negate -((double)float) as (double)(-float). */
494 if (TREE_CODE (type) == REAL_TYPE)
496 tree tem = strip_float_extensions (t);
497 if (tem != t)
498 return negate_expr_p (tem);
500 break;
502 case CALL_EXPR:
503 /* Negate -f(x) as f(-x). */
504 if (negate_mathfn_p (get_call_combined_fn (t)))
505 return negate_expr_p (CALL_EXPR_ARG (t, 0));
506 break;
508 case RSHIFT_EXPR:
509 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
510 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
512 tree op1 = TREE_OPERAND (t, 1);
513 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
514 return true;
516 break;
518 default:
519 break;
521 return false;
524 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
525 simplification is possible.
526 If negate_expr_p would return true for T, NULL_TREE will never be
527 returned. */
529 static tree
530 fold_negate_expr_1 (location_t loc, tree t)
532 tree type = TREE_TYPE (t);
533 tree tem;
535 switch (TREE_CODE (t))
537 /* Convert - (~A) to A + 1. */
538 case BIT_NOT_EXPR:
539 if (INTEGRAL_TYPE_P (type))
540 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
541 build_one_cst (type));
542 break;
544 case INTEGER_CST:
545 tem = fold_negate_const (t, type);
546 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
547 || (ANY_INTEGRAL_TYPE_P (type)
548 && !TYPE_OVERFLOW_TRAPS (type)
549 && TYPE_OVERFLOW_WRAPS (type))
550 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
551 return tem;
552 break;
554 case POLY_INT_CST:
555 case REAL_CST:
556 case FIXED_CST:
557 tem = fold_negate_const (t, type);
558 return tem;
560 case COMPLEX_CST:
562 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
563 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
564 if (rpart && ipart)
565 return build_complex (type, rpart, ipart);
567 break;
569 case VECTOR_CST:
571 tree_vector_builder elts;
572 elts.new_unary_operation (type, t, true);
573 unsigned int count = elts.encoded_nelts ();
574 for (unsigned int i = 0; i < count; ++i)
576 tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
577 if (elt == NULL_TREE)
578 return NULL_TREE;
579 elts.quick_push (elt);
582 return elts.build ();
585 case COMPLEX_EXPR:
586 if (negate_expr_p (t))
587 return fold_build2_loc (loc, COMPLEX_EXPR, type,
588 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
589 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
590 break;
592 case CONJ_EXPR:
593 if (negate_expr_p (t))
594 return fold_build1_loc (loc, CONJ_EXPR, type,
595 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
596 break;
598 case NEGATE_EXPR:
599 if (!TYPE_OVERFLOW_SANITIZED (type))
600 return TREE_OPERAND (t, 0);
601 break;
603 case PLUS_EXPR:
604 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
605 && !HONOR_SIGNED_ZEROS (element_mode (type)))
607 /* -(A + B) -> (-B) - A. */
608 if (negate_expr_p (TREE_OPERAND (t, 1)))
610 tem = negate_expr (TREE_OPERAND (t, 1));
611 return fold_build2_loc (loc, MINUS_EXPR, type,
612 tem, TREE_OPERAND (t, 0));
615 /* -(A + B) -> (-A) - B. */
616 if (negate_expr_p (TREE_OPERAND (t, 0)))
618 tem = negate_expr (TREE_OPERAND (t, 0));
619 return fold_build2_loc (loc, MINUS_EXPR, type,
620 tem, TREE_OPERAND (t, 1));
623 break;
625 case MINUS_EXPR:
626 /* - (A - B) -> B - A */
627 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
628 && !HONOR_SIGNED_ZEROS (element_mode (type)))
629 return fold_build2_loc (loc, MINUS_EXPR, type,
630 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
631 break;
633 case MULT_EXPR:
634 if (TYPE_UNSIGNED (type))
635 break;
637 /* Fall through. */
639 case RDIV_EXPR:
640 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
642 tem = TREE_OPERAND (t, 1);
643 if (negate_expr_p (tem))
644 return fold_build2_loc (loc, TREE_CODE (t), type,
645 TREE_OPERAND (t, 0), negate_expr (tem));
646 tem = TREE_OPERAND (t, 0);
647 if (negate_expr_p (tem))
648 return fold_build2_loc (loc, TREE_CODE (t), type,
649 negate_expr (tem), TREE_OPERAND (t, 1));
651 break;
653 case TRUNC_DIV_EXPR:
654 case ROUND_DIV_EXPR:
655 case EXACT_DIV_EXPR:
656 if (TYPE_UNSIGNED (type))
657 break;
658 /* In general we can't negate A in A / B, because if A is INT_MIN and
659 B is not 1 we change the sign of the result. */
660 if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
661 && negate_expr_p (TREE_OPERAND (t, 0)))
662 return fold_build2_loc (loc, TREE_CODE (t), type,
663 negate_expr (TREE_OPERAND (t, 0)),
664 TREE_OPERAND (t, 1));
665 /* In general we can't negate B in A / B, because if A is INT_MIN and
666 B is 1, we may turn this into INT_MIN / -1 which is undefined
667 and actually traps on some architectures. */
668 if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
669 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
670 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
671 && ! integer_onep (TREE_OPERAND (t, 1))))
672 && negate_expr_p (TREE_OPERAND (t, 1)))
673 return fold_build2_loc (loc, TREE_CODE (t), type,
674 TREE_OPERAND (t, 0),
675 negate_expr (TREE_OPERAND (t, 1)));
676 break;
678 case NOP_EXPR:
679 /* Convert -((double)float) into (double)(-float). */
680 if (TREE_CODE (type) == REAL_TYPE)
682 tem = strip_float_extensions (t);
683 if (tem != t && negate_expr_p (tem))
684 return fold_convert_loc (loc, type, negate_expr (tem));
686 break;
688 case CALL_EXPR:
689 /* Negate -f(x) as f(-x). */
690 if (negate_mathfn_p (get_call_combined_fn (t))
691 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
693 tree fndecl, arg;
695 fndecl = get_callee_fndecl (t);
696 arg = negate_expr (CALL_EXPR_ARG (t, 0));
697 return build_call_expr_loc (loc, fndecl, 1, arg);
699 break;
701 case RSHIFT_EXPR:
702 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
703 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
705 tree op1 = TREE_OPERAND (t, 1);
706 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
708 tree ntype = TYPE_UNSIGNED (type)
709 ? signed_type_for (type)
710 : unsigned_type_for (type);
711 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
712 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
713 return fold_convert_loc (loc, type, temp);
716 break;
718 default:
719 break;
722 return NULL_TREE;
725 /* A wrapper for fold_negate_expr_1. */
727 static tree
728 fold_negate_expr (location_t loc, tree t)
730 tree type = TREE_TYPE (t);
731 STRIP_SIGN_NOPS (t);
732 tree tem = fold_negate_expr_1 (loc, t);
733 if (tem == NULL_TREE)
734 return NULL_TREE;
735 return fold_convert_loc (loc, type, tem);
738 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
739 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
740 return NULL_TREE. */
742 static tree
743 negate_expr (tree t)
745 tree type, tem;
746 location_t loc;
748 if (t == NULL_TREE)
749 return NULL_TREE;
751 loc = EXPR_LOCATION (t);
752 type = TREE_TYPE (t);
753 STRIP_SIGN_NOPS (t);
755 tem = fold_negate_expr (loc, t);
756 if (!tem)
757 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
758 return fold_convert_loc (loc, type, tem);
761 /* Split a tree IN into a constant, literal and variable parts that could be
762 combined with CODE to make IN. "constant" means an expression with
763 TREE_CONSTANT but that isn't an actual constant. CODE must be a
764 commutative arithmetic operation. Store the constant part into *CONP,
765 the literal in *LITP and return the variable part. If a part isn't
766 present, set it to null. If the tree does not decompose in this way,
767 return the entire tree as the variable part and the other parts as null.
769 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
770 case, we negate an operand that was subtracted. Except if it is a
771 literal for which we use *MINUS_LITP instead.
773 If NEGATE_P is true, we are negating all of IN, again except a literal
774 for which we use *MINUS_LITP instead. If a variable part is of pointer
775 type, it is negated after converting to TYPE. This prevents us from
776 generating illegal MINUS pointer expression. LOC is the location of
777 the converted variable part.
779 If IN is itself a literal or constant, return it as appropriate.
781 Note that we do not guarantee that any of the three values will be the
782 same type as IN, but they will have the same signedness and mode. */
784 static tree
785 split_tree (tree in, tree type, enum tree_code code,
786 tree *minus_varp, tree *conp, tree *minus_conp,
787 tree *litp, tree *minus_litp, int negate_p)
789 tree var = 0;
790 *minus_varp = 0;
791 *conp = 0;
792 *minus_conp = 0;
793 *litp = 0;
794 *minus_litp = 0;
796 /* Strip any conversions that don't change the machine mode or signedness. */
797 STRIP_SIGN_NOPS (in);
799 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
800 || TREE_CODE (in) == FIXED_CST)
801 *litp = in;
802 else if (TREE_CODE (in) == code
803 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
804 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
805 /* We can associate addition and subtraction together (even
806 though the C standard doesn't say so) for integers because
807 the value is not affected. For reals, the value might be
808 affected, so we can't. */
809 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
810 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
811 || (code == MINUS_EXPR
812 && (TREE_CODE (in) == PLUS_EXPR
813 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
815 tree op0 = TREE_OPERAND (in, 0);
816 tree op1 = TREE_OPERAND (in, 1);
817 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
818 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
820 /* First see if either of the operands is a literal, then a constant. */
821 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
822 || TREE_CODE (op0) == FIXED_CST)
823 *litp = op0, op0 = 0;
824 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
825 || TREE_CODE (op1) == FIXED_CST)
826 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
828 if (op0 != 0 && TREE_CONSTANT (op0))
829 *conp = op0, op0 = 0;
830 else if (op1 != 0 && TREE_CONSTANT (op1))
831 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
833 /* If we haven't dealt with either operand, this is not a case we can
834 decompose. Otherwise, VAR is either of the ones remaining, if any. */
835 if (op0 != 0 && op1 != 0)
836 var = in;
837 else if (op0 != 0)
838 var = op0;
839 else
840 var = op1, neg_var_p = neg1_p;
842 /* Now do any needed negations. */
843 if (neg_litp_p)
844 *minus_litp = *litp, *litp = 0;
845 if (neg_conp_p && *conp)
846 *minus_conp = *conp, *conp = 0;
847 if (neg_var_p && var)
848 *minus_varp = var, var = 0;
850 else if (TREE_CONSTANT (in))
851 *conp = in;
852 else if (TREE_CODE (in) == BIT_NOT_EXPR
853 && code == PLUS_EXPR)
855 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
856 when IN is constant. */
857 *litp = build_minus_one_cst (type);
858 *minus_varp = TREE_OPERAND (in, 0);
860 else
861 var = in;
863 if (negate_p)
865 if (*litp)
866 *minus_litp = *litp, *litp = 0;
867 else if (*minus_litp)
868 *litp = *minus_litp, *minus_litp = 0;
869 if (*conp)
870 *minus_conp = *conp, *conp = 0;
871 else if (*minus_conp)
872 *conp = *minus_conp, *minus_conp = 0;
873 if (var)
874 *minus_varp = var, var = 0;
875 else if (*minus_varp)
876 var = *minus_varp, *minus_varp = 0;
879 if (*litp
880 && TREE_OVERFLOW_P (*litp))
881 *litp = drop_tree_overflow (*litp);
882 if (*minus_litp
883 && TREE_OVERFLOW_P (*minus_litp))
884 *minus_litp = drop_tree_overflow (*minus_litp);
886 return var;
889 /* Re-associate trees split by the above function. T1 and T2 are
890 either expressions to associate or null. Return the new
891 expression, if any. LOC is the location of the new expression. If
892 we build an operation, do it in TYPE and with CODE. */
894 static tree
895 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
897 if (t1 == 0)
899 gcc_assert (t2 == 0 || code != MINUS_EXPR);
900 return t2;
902 else if (t2 == 0)
903 return t1;
905 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
906 try to fold this since we will have infinite recursion. But do
907 deal with any NEGATE_EXPRs. */
908 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
909 || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
910 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
912 if (code == PLUS_EXPR)
914 if (TREE_CODE (t1) == NEGATE_EXPR)
915 return build2_loc (loc, MINUS_EXPR, type,
916 fold_convert_loc (loc, type, t2),
917 fold_convert_loc (loc, type,
918 TREE_OPERAND (t1, 0)));
919 else if (TREE_CODE (t2) == NEGATE_EXPR)
920 return build2_loc (loc, MINUS_EXPR, type,
921 fold_convert_loc (loc, type, t1),
922 fold_convert_loc (loc, type,
923 TREE_OPERAND (t2, 0)));
924 else if (integer_zerop (t2))
925 return fold_convert_loc (loc, type, t1);
927 else if (code == MINUS_EXPR)
929 if (integer_zerop (t2))
930 return fold_convert_loc (loc, type, t1);
933 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
934 fold_convert_loc (loc, type, t2));
937 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
938 fold_convert_loc (loc, type, t2));
941 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
942 for use in int_const_binop, size_binop and size_diffop. */
944 static bool
945 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
947 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
948 return false;
949 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
950 return false;
952 switch (code)
954 case LSHIFT_EXPR:
955 case RSHIFT_EXPR:
956 case LROTATE_EXPR:
957 case RROTATE_EXPR:
958 return true;
960 default:
961 break;
964 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
965 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
966 && TYPE_MODE (type1) == TYPE_MODE (type2);
969 /* Subroutine of int_const_binop_1 that handles two INTEGER_CSTs. */
971 static tree
972 int_const_binop_2 (enum tree_code code, const_tree parg1, const_tree parg2,
973 int overflowable)
975 wide_int res;
976 tree t;
977 tree type = TREE_TYPE (parg1);
978 signop sign = TYPE_SIGN (type);
979 wi::overflow_type overflow = wi::OVF_NONE;
981 wi::tree_to_wide_ref arg1 = wi::to_wide (parg1);
982 wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type));
984 switch (code)
986 case BIT_IOR_EXPR:
987 res = wi::bit_or (arg1, arg2);
988 break;
990 case BIT_XOR_EXPR:
991 res = wi::bit_xor (arg1, arg2);
992 break;
994 case BIT_AND_EXPR:
995 res = wi::bit_and (arg1, arg2);
996 break;
998 case RSHIFT_EXPR:
999 case LSHIFT_EXPR:
1000 if (wi::neg_p (arg2))
1002 arg2 = -arg2;
1003 if (code == RSHIFT_EXPR)
1004 code = LSHIFT_EXPR;
1005 else
1006 code = RSHIFT_EXPR;
1009 if (code == RSHIFT_EXPR)
1010 /* It's unclear from the C standard whether shifts can overflow.
1011 The following code ignores overflow; perhaps a C standard
1012 interpretation ruling is needed. */
1013 res = wi::rshift (arg1, arg2, sign);
1014 else
1015 res = wi::lshift (arg1, arg2);
1016 break;
1018 case RROTATE_EXPR:
1019 case LROTATE_EXPR:
1020 if (wi::neg_p (arg2))
1022 arg2 = -arg2;
1023 if (code == RROTATE_EXPR)
1024 code = LROTATE_EXPR;
1025 else
1026 code = RROTATE_EXPR;
1029 if (code == RROTATE_EXPR)
1030 res = wi::rrotate (arg1, arg2);
1031 else
1032 res = wi::lrotate (arg1, arg2);
1033 break;
1035 case PLUS_EXPR:
1036 res = wi::add (arg1, arg2, sign, &overflow);
1037 break;
1039 case MINUS_EXPR:
1040 res = wi::sub (arg1, arg2, sign, &overflow);
1041 break;
1043 case MULT_EXPR:
1044 res = wi::mul (arg1, arg2, sign, &overflow);
1045 break;
1047 case MULT_HIGHPART_EXPR:
1048 res = wi::mul_high (arg1, arg2, sign);
1049 break;
1051 case TRUNC_DIV_EXPR:
1052 case EXACT_DIV_EXPR:
1053 if (arg2 == 0)
1054 return NULL_TREE;
1055 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1056 break;
1058 case FLOOR_DIV_EXPR:
1059 if (arg2 == 0)
1060 return NULL_TREE;
1061 res = wi::div_floor (arg1, arg2, sign, &overflow);
1062 break;
1064 case CEIL_DIV_EXPR:
1065 if (arg2 == 0)
1066 return NULL_TREE;
1067 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1068 break;
1070 case ROUND_DIV_EXPR:
1071 if (arg2 == 0)
1072 return NULL_TREE;
1073 res = wi::div_round (arg1, arg2, sign, &overflow);
1074 break;
1076 case TRUNC_MOD_EXPR:
1077 if (arg2 == 0)
1078 return NULL_TREE;
1079 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1080 break;
1082 case FLOOR_MOD_EXPR:
1083 if (arg2 == 0)
1084 return NULL_TREE;
1085 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1086 break;
1088 case CEIL_MOD_EXPR:
1089 if (arg2 == 0)
1090 return NULL_TREE;
1091 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1092 break;
1094 case ROUND_MOD_EXPR:
1095 if (arg2 == 0)
1096 return NULL_TREE;
1097 res = wi::mod_round (arg1, arg2, sign, &overflow);
1098 break;
1100 case MIN_EXPR:
1101 res = wi::min (arg1, arg2, sign);
1102 break;
1104 case MAX_EXPR:
1105 res = wi::max (arg1, arg2, sign);
1106 break;
1108 default:
1109 return NULL_TREE;
1112 t = force_fit_type (type, res, overflowable,
1113 (((sign == SIGNED || overflowable == -1)
1114 && overflow)
1115 | TREE_OVERFLOW (parg1) | TREE_OVERFLOW (parg2)));
1117 return t;
1120 /* Combine two integer constants PARG1 and PARG2 under operation CODE
1121 to produce a new constant. Return NULL_TREE if we don't know how
1122 to evaluate CODE at compile-time. */
1124 static tree
1125 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree arg2,
1126 int overflowable)
1128 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1129 return int_const_binop_2 (code, arg1, arg2, overflowable);
1131 gcc_assert (NUM_POLY_INT_COEFFS != 1);
1133 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1135 poly_wide_int res;
1136 wi::overflow_type overflow;
1137 tree type = TREE_TYPE (arg1);
1138 signop sign = TYPE_SIGN (type);
1139 switch (code)
1141 case PLUS_EXPR:
1142 res = wi::add (wi::to_poly_wide (arg1),
1143 wi::to_poly_wide (arg2), sign, &overflow);
1144 break;
1146 case MINUS_EXPR:
1147 res = wi::sub (wi::to_poly_wide (arg1),
1148 wi::to_poly_wide (arg2), sign, &overflow);
1149 break;
1151 case MULT_EXPR:
1152 if (TREE_CODE (arg2) == INTEGER_CST)
1153 res = wi::mul (wi::to_poly_wide (arg1),
1154 wi::to_wide (arg2), sign, &overflow);
1155 else if (TREE_CODE (arg1) == INTEGER_CST)
1156 res = wi::mul (wi::to_poly_wide (arg2),
1157 wi::to_wide (arg1), sign, &overflow);
1158 else
1159 return NULL_TREE;
1160 break;
1162 case LSHIFT_EXPR:
1163 if (TREE_CODE (arg2) == INTEGER_CST)
1164 res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1165 else
1166 return NULL_TREE;
1167 break;
1169 case BIT_IOR_EXPR:
1170 if (TREE_CODE (arg2) != INTEGER_CST
1171 || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1172 &res))
1173 return NULL_TREE;
1174 break;
1176 default:
1177 return NULL_TREE;
1179 return force_fit_type (type, res, overflowable,
1180 (((sign == SIGNED || overflowable == -1)
1181 && overflow)
1182 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1185 return NULL_TREE;
1188 tree
1189 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1191 return int_const_binop_1 (code, arg1, arg2, 1);
1194 /* Return true if binary operation OP distributes over addition in operand
1195 OPNO, with the other operand being held constant. OPNO counts from 1. */
1197 static bool
1198 distributes_over_addition_p (tree_code op, int opno)
1200 switch (op)
1202 case PLUS_EXPR:
1203 case MINUS_EXPR:
1204 case MULT_EXPR:
1205 return true;
1207 case LSHIFT_EXPR:
1208 return opno == 1;
1210 default:
1211 return false;
1215 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1216 constant. We assume ARG1 and ARG2 have the same data type, or at least
1217 are the same kind of constant and the same machine mode. Return zero if
1218 combining the constants is not allowed in the current operating mode. */
1220 static tree
1221 const_binop (enum tree_code code, tree arg1, tree arg2)
1223 /* Sanity check for the recursive cases. */
1224 if (!arg1 || !arg2)
1225 return NULL_TREE;
1227 STRIP_NOPS (arg1);
1228 STRIP_NOPS (arg2);
1230 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1232 if (code == POINTER_PLUS_EXPR)
1233 return int_const_binop (PLUS_EXPR,
1234 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1236 return int_const_binop (code, arg1, arg2);
1239 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1241 machine_mode mode;
1242 REAL_VALUE_TYPE d1;
1243 REAL_VALUE_TYPE d2;
1244 REAL_VALUE_TYPE value;
1245 REAL_VALUE_TYPE result;
1246 bool inexact;
1247 tree t, type;
1249 /* The following codes are handled by real_arithmetic. */
1250 switch (code)
1252 case PLUS_EXPR:
1253 case MINUS_EXPR:
1254 case MULT_EXPR:
1255 case RDIV_EXPR:
1256 case MIN_EXPR:
1257 case MAX_EXPR:
1258 break;
1260 default:
1261 return NULL_TREE;
1264 d1 = TREE_REAL_CST (arg1);
1265 d2 = TREE_REAL_CST (arg2);
1267 type = TREE_TYPE (arg1);
1268 mode = TYPE_MODE (type);
1270 /* Don't perform operation if we honor signaling NaNs and
1271 either operand is a signaling NaN. */
1272 if (HONOR_SNANS (mode)
1273 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1274 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1275 return NULL_TREE;
1277 /* Don't perform operation if it would raise a division
1278 by zero exception. */
1279 if (code == RDIV_EXPR
1280 && real_equal (&d2, &dconst0)
1281 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1282 return NULL_TREE;
1284 /* If either operand is a NaN, just return it. Otherwise, set up
1285 for floating-point trap; we return an overflow. */
1286 if (REAL_VALUE_ISNAN (d1))
1288 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1289 is off. */
1290 d1.signalling = 0;
1291 t = build_real (type, d1);
1292 return t;
1294 else if (REAL_VALUE_ISNAN (d2))
1296 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1297 is off. */
1298 d2.signalling = 0;
1299 t = build_real (type, d2);
1300 return t;
1303 inexact = real_arithmetic (&value, code, &d1, &d2);
1304 real_convert (&result, mode, &value);
1306 /* Don't constant fold this floating point operation if
1307 the result has overflowed and flag_trapping_math. */
1308 if (flag_trapping_math
1309 && MODE_HAS_INFINITIES (mode)
1310 && REAL_VALUE_ISINF (result)
1311 && !REAL_VALUE_ISINF (d1)
1312 && !REAL_VALUE_ISINF (d2))
1313 return NULL_TREE;
1315 /* Don't constant fold this floating point operation if the
1316 result may dependent upon the run-time rounding mode and
1317 flag_rounding_math is set, or if GCC's software emulation
1318 is unable to accurately represent the result. */
1319 if ((flag_rounding_math
1320 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1321 && (inexact || !real_identical (&result, &value)))
1322 return NULL_TREE;
1324 t = build_real (type, result);
1326 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1327 return t;
1330 if (TREE_CODE (arg1) == FIXED_CST)
1332 FIXED_VALUE_TYPE f1;
1333 FIXED_VALUE_TYPE f2;
1334 FIXED_VALUE_TYPE result;
1335 tree t, type;
1336 int sat_p;
1337 bool overflow_p;
1339 /* The following codes are handled by fixed_arithmetic. */
1340 switch (code)
1342 case PLUS_EXPR:
1343 case MINUS_EXPR:
1344 case MULT_EXPR:
1345 case TRUNC_DIV_EXPR:
1346 if (TREE_CODE (arg2) != FIXED_CST)
1347 return NULL_TREE;
1348 f2 = TREE_FIXED_CST (arg2);
1349 break;
1351 case LSHIFT_EXPR:
1352 case RSHIFT_EXPR:
1354 if (TREE_CODE (arg2) != INTEGER_CST)
1355 return NULL_TREE;
1356 wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1357 f2.data.high = w2.elt (1);
1358 f2.data.low = w2.ulow ();
1359 f2.mode = SImode;
1361 break;
1363 default:
1364 return NULL_TREE;
1367 f1 = TREE_FIXED_CST (arg1);
1368 type = TREE_TYPE (arg1);
1369 sat_p = TYPE_SATURATING (type);
1370 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1371 t = build_fixed (type, result);
1372 /* Propagate overflow flags. */
1373 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1374 TREE_OVERFLOW (t) = 1;
1375 return t;
1378 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1380 tree type = TREE_TYPE (arg1);
1381 tree r1 = TREE_REALPART (arg1);
1382 tree i1 = TREE_IMAGPART (arg1);
1383 tree r2 = TREE_REALPART (arg2);
1384 tree i2 = TREE_IMAGPART (arg2);
1385 tree real, imag;
1387 switch (code)
1389 case PLUS_EXPR:
1390 case MINUS_EXPR:
1391 real = const_binop (code, r1, r2);
1392 imag = const_binop (code, i1, i2);
1393 break;
1395 case MULT_EXPR:
1396 if (COMPLEX_FLOAT_TYPE_P (type))
1397 return do_mpc_arg2 (arg1, arg2, type,
1398 /* do_nonfinite= */ folding_initializer,
1399 mpc_mul);
1401 real = const_binop (MINUS_EXPR,
1402 const_binop (MULT_EXPR, r1, r2),
1403 const_binop (MULT_EXPR, i1, i2));
1404 imag = const_binop (PLUS_EXPR,
1405 const_binop (MULT_EXPR, r1, i2),
1406 const_binop (MULT_EXPR, i1, r2));
1407 break;
1409 case RDIV_EXPR:
1410 if (COMPLEX_FLOAT_TYPE_P (type))
1411 return do_mpc_arg2 (arg1, arg2, type,
1412 /* do_nonfinite= */ folding_initializer,
1413 mpc_div);
1414 /* Fallthru. */
1415 case TRUNC_DIV_EXPR:
1416 case CEIL_DIV_EXPR:
1417 case FLOOR_DIV_EXPR:
1418 case ROUND_DIV_EXPR:
1419 if (flag_complex_method == 0)
1421 /* Keep this algorithm in sync with
1422 tree-complex.c:expand_complex_div_straight().
1424 Expand complex division to scalars, straightforward algorithm.
1425 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1426 t = br*br + bi*bi
1428 tree magsquared
1429 = const_binop (PLUS_EXPR,
1430 const_binop (MULT_EXPR, r2, r2),
1431 const_binop (MULT_EXPR, i2, i2));
1432 tree t1
1433 = const_binop (PLUS_EXPR,
1434 const_binop (MULT_EXPR, r1, r2),
1435 const_binop (MULT_EXPR, i1, i2));
1436 tree t2
1437 = const_binop (MINUS_EXPR,
1438 const_binop (MULT_EXPR, i1, r2),
1439 const_binop (MULT_EXPR, r1, i2));
1441 real = const_binop (code, t1, magsquared);
1442 imag = const_binop (code, t2, magsquared);
1444 else
1446 /* Keep this algorithm in sync with
1447 tree-complex.c:expand_complex_div_wide().
1449 Expand complex division to scalars, modified algorithm to minimize
1450 overflow with wide input ranges. */
1451 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1452 fold_abs_const (r2, TREE_TYPE (type)),
1453 fold_abs_const (i2, TREE_TYPE (type)));
1455 if (integer_nonzerop (compare))
1457 /* In the TRUE branch, we compute
1458 ratio = br/bi;
1459 div = (br * ratio) + bi;
1460 tr = (ar * ratio) + ai;
1461 ti = (ai * ratio) - ar;
1462 tr = tr / div;
1463 ti = ti / div; */
1464 tree ratio = const_binop (code, r2, i2);
1465 tree div = const_binop (PLUS_EXPR, i2,
1466 const_binop (MULT_EXPR, r2, ratio));
1467 real = const_binop (MULT_EXPR, r1, ratio);
1468 real = const_binop (PLUS_EXPR, real, i1);
1469 real = const_binop (code, real, div);
1471 imag = const_binop (MULT_EXPR, i1, ratio);
1472 imag = const_binop (MINUS_EXPR, imag, r1);
1473 imag = const_binop (code, imag, div);
1475 else
1477 /* In the FALSE branch, we compute
1478 ratio = d/c;
1479 divisor = (d * ratio) + c;
1480 tr = (b * ratio) + a;
1481 ti = b - (a * ratio);
1482 tr = tr / div;
1483 ti = ti / div; */
1484 tree ratio = const_binop (code, i2, r2);
1485 tree div = const_binop (PLUS_EXPR, r2,
1486 const_binop (MULT_EXPR, i2, ratio));
1488 real = const_binop (MULT_EXPR, i1, ratio);
1489 real = const_binop (PLUS_EXPR, real, r1);
1490 real = const_binop (code, real, div);
1492 imag = const_binop (MULT_EXPR, r1, ratio);
1493 imag = const_binop (MINUS_EXPR, i1, imag);
1494 imag = const_binop (code, imag, div);
1497 break;
1499 default:
1500 return NULL_TREE;
1503 if (real && imag)
1504 return build_complex (type, real, imag);
1507 if (TREE_CODE (arg1) == VECTOR_CST
1508 && TREE_CODE (arg2) == VECTOR_CST
1509 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1510 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1512 tree type = TREE_TYPE (arg1);
1513 bool step_ok_p;
1514 if (VECTOR_CST_STEPPED_P (arg1)
1515 && VECTOR_CST_STEPPED_P (arg2))
1516 /* We can operate directly on the encoding if:
1518 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1519 implies
1520 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1522 Addition and subtraction are the supported operators
1523 for which this is true. */
1524 step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1525 else if (VECTOR_CST_STEPPED_P (arg1))
1526 /* We can operate directly on stepped encodings if:
1528 a3 - a2 == a2 - a1
1529 implies:
1530 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1532 which is true if (x -> x op c) distributes over addition. */
1533 step_ok_p = distributes_over_addition_p (code, 1);
1534 else
1535 /* Similarly in reverse. */
1536 step_ok_p = distributes_over_addition_p (code, 2);
1537 tree_vector_builder elts;
1538 if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1539 return NULL_TREE;
1540 unsigned int count = elts.encoded_nelts ();
1541 for (unsigned int i = 0; i < count; ++i)
1543 tree elem1 = VECTOR_CST_ELT (arg1, i);
1544 tree elem2 = VECTOR_CST_ELT (arg2, i);
1546 tree elt = const_binop (code, elem1, elem2);
1548 /* It is possible that const_binop cannot handle the given
1549 code and return NULL_TREE */
1550 if (elt == NULL_TREE)
1551 return NULL_TREE;
1552 elts.quick_push (elt);
1555 return elts.build ();
1558 /* Shifts allow a scalar offset for a vector. */
1559 if (TREE_CODE (arg1) == VECTOR_CST
1560 && TREE_CODE (arg2) == INTEGER_CST)
1562 tree type = TREE_TYPE (arg1);
1563 bool step_ok_p = distributes_over_addition_p (code, 1);
1564 tree_vector_builder elts;
1565 if (!elts.new_unary_operation (type, arg1, step_ok_p))
1566 return NULL_TREE;
1567 unsigned int count = elts.encoded_nelts ();
1568 for (unsigned int i = 0; i < count; ++i)
1570 tree elem1 = VECTOR_CST_ELT (arg1, i);
1572 tree elt = const_binop (code, elem1, arg2);
1574 /* It is possible that const_binop cannot handle the given
1575 code and return NULL_TREE. */
1576 if (elt == NULL_TREE)
1577 return NULL_TREE;
1578 elts.quick_push (elt);
1581 return elts.build ();
1583 return NULL_TREE;
1586 /* Overload that adds a TYPE parameter to be able to dispatch
1587 to fold_relational_const. */
1589 tree
1590 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1592 if (TREE_CODE_CLASS (code) == tcc_comparison)
1593 return fold_relational_const (code, type, arg1, arg2);
1595 /* ??? Until we make the const_binop worker take the type of the
1596 result as argument put those cases that need it here. */
1597 switch (code)
1599 case VEC_SERIES_EXPR:
1600 if (CONSTANT_CLASS_P (arg1)
1601 && CONSTANT_CLASS_P (arg2))
1602 return build_vec_series (type, arg1, arg2);
1603 return NULL_TREE;
1605 case COMPLEX_EXPR:
1606 if ((TREE_CODE (arg1) == REAL_CST
1607 && TREE_CODE (arg2) == REAL_CST)
1608 || (TREE_CODE (arg1) == INTEGER_CST
1609 && TREE_CODE (arg2) == INTEGER_CST))
1610 return build_complex (type, arg1, arg2);
1611 return NULL_TREE;
1613 case POINTER_DIFF_EXPR:
1614 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1616 poly_offset_int res = (wi::to_poly_offset (arg1)
1617 - wi::to_poly_offset (arg2));
1618 return force_fit_type (type, res, 1,
1619 TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1621 return NULL_TREE;
1623 case VEC_PACK_TRUNC_EXPR:
1624 case VEC_PACK_FIX_TRUNC_EXPR:
1625 case VEC_PACK_FLOAT_EXPR:
1627 unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1629 if (TREE_CODE (arg1) != VECTOR_CST
1630 || TREE_CODE (arg2) != VECTOR_CST)
1631 return NULL_TREE;
1633 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1634 return NULL_TREE;
1636 out_nelts = in_nelts * 2;
1637 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1638 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1640 tree_vector_builder elts (type, out_nelts, 1);
1641 for (i = 0; i < out_nelts; i++)
1643 tree elt = (i < in_nelts
1644 ? VECTOR_CST_ELT (arg1, i)
1645 : VECTOR_CST_ELT (arg2, i - in_nelts));
1646 elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1647 ? NOP_EXPR
1648 : code == VEC_PACK_FLOAT_EXPR
1649 ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1650 TREE_TYPE (type), elt);
1651 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1652 return NULL_TREE;
1653 elts.quick_push (elt);
1656 return elts.build ();
1659 case VEC_WIDEN_MULT_LO_EXPR:
1660 case VEC_WIDEN_MULT_HI_EXPR:
1661 case VEC_WIDEN_MULT_EVEN_EXPR:
1662 case VEC_WIDEN_MULT_ODD_EXPR:
1664 unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1666 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1667 return NULL_TREE;
1669 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1670 return NULL_TREE;
1671 out_nelts = in_nelts / 2;
1672 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1673 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1675 if (code == VEC_WIDEN_MULT_LO_EXPR)
1676 scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1677 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1678 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1679 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1680 scale = 1, ofs = 0;
1681 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1682 scale = 1, ofs = 1;
1684 tree_vector_builder elts (type, out_nelts, 1);
1685 for (out = 0; out < out_nelts; out++)
1687 unsigned int in = (out << scale) + ofs;
1688 tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1689 VECTOR_CST_ELT (arg1, in));
1690 tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1691 VECTOR_CST_ELT (arg2, in));
1693 if (t1 == NULL_TREE || t2 == NULL_TREE)
1694 return NULL_TREE;
1695 tree elt = const_binop (MULT_EXPR, t1, t2);
1696 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1697 return NULL_TREE;
1698 elts.quick_push (elt);
1701 return elts.build ();
1704 default:;
1707 if (TREE_CODE_CLASS (code) != tcc_binary)
1708 return NULL_TREE;
1710 /* Make sure type and arg0 have the same saturating flag. */
1711 gcc_checking_assert (TYPE_SATURATING (type)
1712 == TYPE_SATURATING (TREE_TYPE (arg1)));
1714 return const_binop (code, arg1, arg2);
1717 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1718 Return zero if computing the constants is not possible. */
1720 tree
1721 const_unop (enum tree_code code, tree type, tree arg0)
1723 /* Don't perform the operation, other than NEGATE and ABS, if
1724 flag_signaling_nans is on and the operand is a signaling NaN. */
1725 if (TREE_CODE (arg0) == REAL_CST
1726 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1727 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1728 && code != NEGATE_EXPR
1729 && code != ABS_EXPR
1730 && code != ABSU_EXPR)
1731 return NULL_TREE;
1733 switch (code)
1735 CASE_CONVERT:
1736 case FLOAT_EXPR:
1737 case FIX_TRUNC_EXPR:
1738 case FIXED_CONVERT_EXPR:
1739 return fold_convert_const (code, type, arg0);
1741 case ADDR_SPACE_CONVERT_EXPR:
1742 /* If the source address is 0, and the source address space
1743 cannot have a valid object at 0, fold to dest type null. */
1744 if (integer_zerop (arg0)
1745 && !(targetm.addr_space.zero_address_valid
1746 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1747 return fold_convert_const (code, type, arg0);
1748 break;
1750 case VIEW_CONVERT_EXPR:
1751 return fold_view_convert_expr (type, arg0);
1753 case NEGATE_EXPR:
1755 /* Can't call fold_negate_const directly here as that doesn't
1756 handle all cases and we might not be able to negate some
1757 constants. */
1758 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1759 if (tem && CONSTANT_CLASS_P (tem))
1760 return tem;
1761 break;
1764 case ABS_EXPR:
1765 case ABSU_EXPR:
1766 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1767 return fold_abs_const (arg0, type);
1768 break;
1770 case CONJ_EXPR:
1771 if (TREE_CODE (arg0) == COMPLEX_CST)
1773 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1774 TREE_TYPE (type));
1775 return build_complex (type, TREE_REALPART (arg0), ipart);
1777 break;
1779 case BIT_NOT_EXPR:
1780 if (TREE_CODE (arg0) == INTEGER_CST)
1781 return fold_not_const (arg0, type);
1782 else if (POLY_INT_CST_P (arg0))
1783 return wide_int_to_tree (type, -poly_int_cst_value (arg0));
1784 /* Perform BIT_NOT_EXPR on each element individually. */
1785 else if (TREE_CODE (arg0) == VECTOR_CST)
1787 tree elem;
1789 /* This can cope with stepped encodings because ~x == -1 - x. */
1790 tree_vector_builder elements;
1791 elements.new_unary_operation (type, arg0, true);
1792 unsigned int i, count = elements.encoded_nelts ();
1793 for (i = 0; i < count; ++i)
1795 elem = VECTOR_CST_ELT (arg0, i);
1796 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1797 if (elem == NULL_TREE)
1798 break;
1799 elements.quick_push (elem);
1801 if (i == count)
1802 return elements.build ();
1804 break;
1806 case TRUTH_NOT_EXPR:
1807 if (TREE_CODE (arg0) == INTEGER_CST)
1808 return constant_boolean_node (integer_zerop (arg0), type);
1809 break;
1811 case REALPART_EXPR:
1812 if (TREE_CODE (arg0) == COMPLEX_CST)
1813 return fold_convert (type, TREE_REALPART (arg0));
1814 break;
1816 case IMAGPART_EXPR:
1817 if (TREE_CODE (arg0) == COMPLEX_CST)
1818 return fold_convert (type, TREE_IMAGPART (arg0));
1819 break;
1821 case VEC_UNPACK_LO_EXPR:
1822 case VEC_UNPACK_HI_EXPR:
1823 case VEC_UNPACK_FLOAT_LO_EXPR:
1824 case VEC_UNPACK_FLOAT_HI_EXPR:
1825 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1826 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1828 unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
1829 enum tree_code subcode;
1831 if (TREE_CODE (arg0) != VECTOR_CST)
1832 return NULL_TREE;
1834 if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
1835 return NULL_TREE;
1836 out_nelts = in_nelts / 2;
1837 gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1839 unsigned int offset = 0;
1840 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1841 || code == VEC_UNPACK_FLOAT_LO_EXPR
1842 || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
1843 offset = out_nelts;
1845 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1846 subcode = NOP_EXPR;
1847 else if (code == VEC_UNPACK_FLOAT_LO_EXPR
1848 || code == VEC_UNPACK_FLOAT_HI_EXPR)
1849 subcode = FLOAT_EXPR;
1850 else
1851 subcode = FIX_TRUNC_EXPR;
1853 tree_vector_builder elts (type, out_nelts, 1);
1854 for (i = 0; i < out_nelts; i++)
1856 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1857 VECTOR_CST_ELT (arg0, i + offset));
1858 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1859 return NULL_TREE;
1860 elts.quick_push (elt);
1863 return elts.build ();
1866 case VEC_DUPLICATE_EXPR:
1867 if (CONSTANT_CLASS_P (arg0))
1868 return build_vector_from_val (type, arg0);
1869 return NULL_TREE;
1871 default:
1872 break;
1875 return NULL_TREE;
1878 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1879 indicates which particular sizetype to create. */
1881 tree
1882 size_int_kind (poly_int64 number, enum size_type_kind kind)
1884 return build_int_cst (sizetype_tab[(int) kind], number);
1887 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1888 is a tree code. The type of the result is taken from the operands.
1889 Both must be equivalent integer types, ala int_binop_types_match_p.
1890 If the operands are constant, so is the result. */
1892 tree
1893 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1895 tree type = TREE_TYPE (arg0);
1897 if (arg0 == error_mark_node || arg1 == error_mark_node)
1898 return error_mark_node;
1900 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1901 TREE_TYPE (arg1)));
1903 /* Handle the special case of two poly_int constants faster. */
1904 if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1906 /* And some specific cases even faster than that. */
1907 if (code == PLUS_EXPR)
1909 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1910 return arg1;
1911 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1912 return arg0;
1914 else if (code == MINUS_EXPR)
1916 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1917 return arg0;
1919 else if (code == MULT_EXPR)
1921 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1922 return arg1;
1925 /* Handle general case of two integer constants. For sizetype
1926 constant calculations we always want to know about overflow,
1927 even in the unsigned case. */
1928 tree res = int_const_binop_1 (code, arg0, arg1, -1);
1929 if (res != NULL_TREE)
1930 return res;
1933 return fold_build2_loc (loc, code, type, arg0, arg1);
1936 /* Given two values, either both of sizetype or both of bitsizetype,
1937 compute the difference between the two values. Return the value
1938 in signed type corresponding to the type of the operands. */
1940 tree
1941 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1943 tree type = TREE_TYPE (arg0);
1944 tree ctype;
1946 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1947 TREE_TYPE (arg1)));
1949 /* If the type is already signed, just do the simple thing. */
1950 if (!TYPE_UNSIGNED (type))
1951 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1953 if (type == sizetype)
1954 ctype = ssizetype;
1955 else if (type == bitsizetype)
1956 ctype = sbitsizetype;
1957 else
1958 ctype = signed_type_for (type);
1960 /* If either operand is not a constant, do the conversions to the signed
1961 type and subtract. The hardware will do the right thing with any
1962 overflow in the subtraction. */
1963 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1964 return size_binop_loc (loc, MINUS_EXPR,
1965 fold_convert_loc (loc, ctype, arg0),
1966 fold_convert_loc (loc, ctype, arg1));
1968 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1969 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1970 overflow) and negate (which can't either). Special-case a result
1971 of zero while we're here. */
1972 if (tree_int_cst_equal (arg0, arg1))
1973 return build_int_cst (ctype, 0);
1974 else if (tree_int_cst_lt (arg1, arg0))
1975 return fold_convert_loc (loc, ctype,
1976 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1977 else
1978 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1979 fold_convert_loc (loc, ctype,
1980 size_binop_loc (loc,
1981 MINUS_EXPR,
1982 arg1, arg0)));
1985 /* A subroutine of fold_convert_const handling conversions of an
1986 INTEGER_CST to another integer type. */
1988 static tree
1989 fold_convert_const_int_from_int (tree type, const_tree arg1)
1991 /* Given an integer constant, make new constant with new type,
1992 appropriately sign-extended or truncated. Use widest_int
1993 so that any extension is done according ARG1's type. */
1994 return force_fit_type (type, wi::to_widest (arg1),
1995 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1996 TREE_OVERFLOW (arg1));
1999 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2000 to an integer type. */
2002 static tree
2003 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2005 bool overflow = false;
2006 tree t;
2008 /* The following code implements the floating point to integer
2009 conversion rules required by the Java Language Specification,
2010 that IEEE NaNs are mapped to zero and values that overflow
2011 the target precision saturate, i.e. values greater than
2012 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2013 are mapped to INT_MIN. These semantics are allowed by the
2014 C and C++ standards that simply state that the behavior of
2015 FP-to-integer conversion is unspecified upon overflow. */
2017 wide_int val;
2018 REAL_VALUE_TYPE r;
2019 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2021 switch (code)
2023 case FIX_TRUNC_EXPR:
2024 real_trunc (&r, VOIDmode, &x);
2025 break;
2027 default:
2028 gcc_unreachable ();
2031 /* If R is NaN, return zero and show we have an overflow. */
2032 if (REAL_VALUE_ISNAN (r))
2034 overflow = true;
2035 val = wi::zero (TYPE_PRECISION (type));
2038 /* See if R is less than the lower bound or greater than the
2039 upper bound. */
2041 if (! overflow)
2043 tree lt = TYPE_MIN_VALUE (type);
2044 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2045 if (real_less (&r, &l))
2047 overflow = true;
2048 val = wi::to_wide (lt);
2052 if (! overflow)
2054 tree ut = TYPE_MAX_VALUE (type);
2055 if (ut)
2057 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2058 if (real_less (&u, &r))
2060 overflow = true;
2061 val = wi::to_wide (ut);
2066 if (! overflow)
2067 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2069 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2070 return t;
2073 /* A subroutine of fold_convert_const handling conversions of a
2074 FIXED_CST to an integer type. */
2076 static tree
2077 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2079 tree t;
2080 double_int temp, temp_trunc;
2081 scalar_mode mode;
2083 /* Right shift FIXED_CST to temp by fbit. */
2084 temp = TREE_FIXED_CST (arg1).data;
2085 mode = TREE_FIXED_CST (arg1).mode;
2086 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2088 temp = temp.rshift (GET_MODE_FBIT (mode),
2089 HOST_BITS_PER_DOUBLE_INT,
2090 SIGNED_FIXED_POINT_MODE_P (mode));
2092 /* Left shift temp to temp_trunc by fbit. */
2093 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2094 HOST_BITS_PER_DOUBLE_INT,
2095 SIGNED_FIXED_POINT_MODE_P (mode));
2097 else
2099 temp = double_int_zero;
2100 temp_trunc = double_int_zero;
2103 /* If FIXED_CST is negative, we need to round the value toward 0.
2104 By checking if the fractional bits are not zero to add 1 to temp. */
2105 if (SIGNED_FIXED_POINT_MODE_P (mode)
2106 && temp_trunc.is_negative ()
2107 && TREE_FIXED_CST (arg1).data != temp_trunc)
2108 temp += double_int_one;
2110 /* Given a fixed-point constant, make new constant with new type,
2111 appropriately sign-extended or truncated. */
2112 t = force_fit_type (type, temp, -1,
2113 (temp.is_negative ()
2114 && (TYPE_UNSIGNED (type)
2115 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2116 | TREE_OVERFLOW (arg1));
2118 return t;
2121 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2122 to another floating point type. */
2124 static tree
2125 fold_convert_const_real_from_real (tree type, const_tree arg1)
2127 REAL_VALUE_TYPE value;
2128 tree t;
2130 /* Don't perform the operation if flag_signaling_nans is on
2131 and the operand is a signaling NaN. */
2132 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2133 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2134 return NULL_TREE;
2136 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2137 t = build_real (type, value);
2139 /* If converting an infinity or NAN to a representation that doesn't
2140 have one, set the overflow bit so that we can produce some kind of
2141 error message at the appropriate point if necessary. It's not the
2142 most user-friendly message, but it's better than nothing. */
2143 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2144 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2145 TREE_OVERFLOW (t) = 1;
2146 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2147 && !MODE_HAS_NANS (TYPE_MODE (type)))
2148 TREE_OVERFLOW (t) = 1;
2149 /* Regular overflow, conversion produced an infinity in a mode that
2150 can't represent them. */
2151 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2152 && REAL_VALUE_ISINF (value)
2153 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2154 TREE_OVERFLOW (t) = 1;
2155 else
2156 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2157 return t;
2160 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2161 to a floating point type. */
2163 static tree
2164 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2166 REAL_VALUE_TYPE value;
2167 tree t;
2169 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2170 &TREE_FIXED_CST (arg1));
2171 t = build_real (type, value);
2173 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2174 return t;
2177 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2178 to another fixed-point type. */
2180 static tree
2181 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2183 FIXED_VALUE_TYPE value;
2184 tree t;
2185 bool overflow_p;
2187 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2188 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2189 t = build_fixed (type, value);
2191 /* Propagate overflow flags. */
2192 if (overflow_p | TREE_OVERFLOW (arg1))
2193 TREE_OVERFLOW (t) = 1;
2194 return t;
2197 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2198 to a fixed-point type. */
2200 static tree
2201 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2203 FIXED_VALUE_TYPE value;
2204 tree t;
2205 bool overflow_p;
2206 double_int di;
2208 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2210 di.low = TREE_INT_CST_ELT (arg1, 0);
2211 if (TREE_INT_CST_NUNITS (arg1) == 1)
2212 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2213 else
2214 di.high = TREE_INT_CST_ELT (arg1, 1);
2216 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2217 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2218 TYPE_SATURATING (type));
2219 t = build_fixed (type, value);
2221 /* Propagate overflow flags. */
2222 if (overflow_p | TREE_OVERFLOW (arg1))
2223 TREE_OVERFLOW (t) = 1;
2224 return t;
2227 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2228 to a fixed-point type. */
2230 static tree
2231 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2233 FIXED_VALUE_TYPE value;
2234 tree t;
2235 bool overflow_p;
2237 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2238 &TREE_REAL_CST (arg1),
2239 TYPE_SATURATING (type));
2240 t = build_fixed (type, value);
2242 /* Propagate overflow flags. */
2243 if (overflow_p | TREE_OVERFLOW (arg1))
2244 TREE_OVERFLOW (t) = 1;
2245 return t;
2248 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2249 type TYPE. If no simplification can be done return NULL_TREE. */
2251 static tree
2252 fold_convert_const (enum tree_code code, tree type, tree arg1)
2254 tree arg_type = TREE_TYPE (arg1);
2255 if (arg_type == type)
2256 return arg1;
2258 /* We can't widen types, since the runtime value could overflow the
2259 original type before being extended to the new type. */
2260 if (POLY_INT_CST_P (arg1)
2261 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2262 && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2263 return build_poly_int_cst (type,
2264 poly_wide_int::from (poly_int_cst_value (arg1),
2265 TYPE_PRECISION (type),
2266 TYPE_SIGN (arg_type)));
2268 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2269 || TREE_CODE (type) == OFFSET_TYPE)
2271 if (TREE_CODE (arg1) == INTEGER_CST)
2272 return fold_convert_const_int_from_int (type, arg1);
2273 else if (TREE_CODE (arg1) == REAL_CST)
2274 return fold_convert_const_int_from_real (code, type, arg1);
2275 else if (TREE_CODE (arg1) == FIXED_CST)
2276 return fold_convert_const_int_from_fixed (type, arg1);
2278 else if (TREE_CODE (type) == REAL_TYPE)
2280 if (TREE_CODE (arg1) == INTEGER_CST)
2281 return build_real_from_int_cst (type, arg1);
2282 else if (TREE_CODE (arg1) == REAL_CST)
2283 return fold_convert_const_real_from_real (type, arg1);
2284 else if (TREE_CODE (arg1) == FIXED_CST)
2285 return fold_convert_const_real_from_fixed (type, arg1);
2287 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2289 if (TREE_CODE (arg1) == FIXED_CST)
2290 return fold_convert_const_fixed_from_fixed (type, arg1);
2291 else if (TREE_CODE (arg1) == INTEGER_CST)
2292 return fold_convert_const_fixed_from_int (type, arg1);
2293 else if (TREE_CODE (arg1) == REAL_CST)
2294 return fold_convert_const_fixed_from_real (type, arg1);
2296 else if (TREE_CODE (type) == VECTOR_TYPE)
2298 if (TREE_CODE (arg1) == VECTOR_CST
2299 && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2301 tree elttype = TREE_TYPE (type);
2302 tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2303 /* We can't handle steps directly when extending, since the
2304 values need to wrap at the original precision first. */
2305 bool step_ok_p
2306 = (INTEGRAL_TYPE_P (elttype)
2307 && INTEGRAL_TYPE_P (arg1_elttype)
2308 && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2309 tree_vector_builder v;
2310 if (!v.new_unary_operation (type, arg1, step_ok_p))
2311 return NULL_TREE;
2312 unsigned int len = v.encoded_nelts ();
2313 for (unsigned int i = 0; i < len; ++i)
2315 tree elt = VECTOR_CST_ELT (arg1, i);
2316 tree cvt = fold_convert_const (code, elttype, elt);
2317 if (cvt == NULL_TREE)
2318 return NULL_TREE;
2319 v.quick_push (cvt);
2321 return v.build ();
2324 return NULL_TREE;
2327 /* Construct a vector of zero elements of vector type TYPE. */
2329 static tree
2330 build_zero_vector (tree type)
2332 tree t;
2334 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2335 return build_vector_from_val (type, t);
2338 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2340 bool
2341 fold_convertible_p (const_tree type, const_tree arg)
2343 tree orig = TREE_TYPE (arg);
2345 if (type == orig)
2346 return true;
2348 if (TREE_CODE (arg) == ERROR_MARK
2349 || TREE_CODE (type) == ERROR_MARK
2350 || TREE_CODE (orig) == ERROR_MARK)
2351 return false;
2353 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2354 return true;
2356 switch (TREE_CODE (type))
2358 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2359 case POINTER_TYPE: case REFERENCE_TYPE:
2360 case OFFSET_TYPE:
2361 return (INTEGRAL_TYPE_P (orig)
2362 || (POINTER_TYPE_P (orig)
2363 && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2364 || TREE_CODE (orig) == OFFSET_TYPE);
2366 case REAL_TYPE:
2367 case FIXED_POINT_TYPE:
2368 case VECTOR_TYPE:
2369 case VOID_TYPE:
2370 return TREE_CODE (type) == TREE_CODE (orig);
2372 default:
2373 return false;
2377 /* Convert expression ARG to type TYPE. Used by the middle-end for
2378 simple conversions in preference to calling the front-end's convert. */
2380 tree
2381 fold_convert_loc (location_t loc, tree type, tree arg)
2383 tree orig = TREE_TYPE (arg);
2384 tree tem;
2386 if (type == orig)
2387 return arg;
2389 if (TREE_CODE (arg) == ERROR_MARK
2390 || TREE_CODE (type) == ERROR_MARK
2391 || TREE_CODE (orig) == ERROR_MARK)
2392 return error_mark_node;
2394 switch (TREE_CODE (type))
2396 case POINTER_TYPE:
2397 case REFERENCE_TYPE:
2398 /* Handle conversions between pointers to different address spaces. */
2399 if (POINTER_TYPE_P (orig)
2400 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2401 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2402 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2403 /* fall through */
2405 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2406 case OFFSET_TYPE:
2407 if (TREE_CODE (arg) == INTEGER_CST)
2409 tem = fold_convert_const (NOP_EXPR, type, arg);
2410 if (tem != NULL_TREE)
2411 return tem;
2413 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2414 || TREE_CODE (orig) == OFFSET_TYPE)
2415 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2416 if (TREE_CODE (orig) == COMPLEX_TYPE)
2417 return fold_convert_loc (loc, type,
2418 fold_build1_loc (loc, REALPART_EXPR,
2419 TREE_TYPE (orig), arg));
2420 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2421 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2422 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2424 case REAL_TYPE:
2425 if (TREE_CODE (arg) == INTEGER_CST)
2427 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2428 if (tem != NULL_TREE)
2429 return tem;
2431 else if (TREE_CODE (arg) == REAL_CST)
2433 tem = fold_convert_const (NOP_EXPR, type, arg);
2434 if (tem != NULL_TREE)
2435 return tem;
2437 else if (TREE_CODE (arg) == FIXED_CST)
2439 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2440 if (tem != NULL_TREE)
2441 return tem;
2444 switch (TREE_CODE (orig))
2446 case INTEGER_TYPE:
2447 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2448 case POINTER_TYPE: case REFERENCE_TYPE:
2449 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2451 case REAL_TYPE:
2452 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2454 case FIXED_POINT_TYPE:
2455 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2457 case COMPLEX_TYPE:
2458 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2459 return fold_convert_loc (loc, type, tem);
2461 default:
2462 gcc_unreachable ();
2465 case FIXED_POINT_TYPE:
2466 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2467 || TREE_CODE (arg) == REAL_CST)
2469 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2470 if (tem != NULL_TREE)
2471 goto fold_convert_exit;
2474 switch (TREE_CODE (orig))
2476 case FIXED_POINT_TYPE:
2477 case INTEGER_TYPE:
2478 case ENUMERAL_TYPE:
2479 case BOOLEAN_TYPE:
2480 case REAL_TYPE:
2481 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2483 case COMPLEX_TYPE:
2484 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2485 return fold_convert_loc (loc, type, tem);
2487 default:
2488 gcc_unreachable ();
2491 case COMPLEX_TYPE:
2492 switch (TREE_CODE (orig))
2494 case INTEGER_TYPE:
2495 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2496 case POINTER_TYPE: case REFERENCE_TYPE:
2497 case REAL_TYPE:
2498 case FIXED_POINT_TYPE:
2499 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2500 fold_convert_loc (loc, TREE_TYPE (type), arg),
2501 fold_convert_loc (loc, TREE_TYPE (type),
2502 integer_zero_node));
2503 case COMPLEX_TYPE:
2505 tree rpart, ipart;
2507 if (TREE_CODE (arg) == COMPLEX_EXPR)
2509 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2510 TREE_OPERAND (arg, 0));
2511 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2512 TREE_OPERAND (arg, 1));
2513 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2516 arg = save_expr (arg);
2517 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2518 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2519 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2520 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2521 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2524 default:
2525 gcc_unreachable ();
2528 case VECTOR_TYPE:
2529 if (integer_zerop (arg))
2530 return build_zero_vector (type);
2531 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2532 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2533 || TREE_CODE (orig) == VECTOR_TYPE);
2534 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2536 case VOID_TYPE:
2537 tem = fold_ignored_result (arg);
2538 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2540 default:
2541 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2542 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2543 gcc_unreachable ();
2545 fold_convert_exit:
2546 protected_set_expr_location_unshare (tem, loc);
2547 return tem;
2550 /* Return false if expr can be assumed not to be an lvalue, true
2551 otherwise. */
2553 static bool
2554 maybe_lvalue_p (const_tree x)
2556 /* We only need to wrap lvalue tree codes. */
2557 switch (TREE_CODE (x))
2559 case VAR_DECL:
2560 case PARM_DECL:
2561 case RESULT_DECL:
2562 case LABEL_DECL:
2563 case FUNCTION_DECL:
2564 case SSA_NAME:
2566 case COMPONENT_REF:
2567 case MEM_REF:
2568 case INDIRECT_REF:
2569 case ARRAY_REF:
2570 case ARRAY_RANGE_REF:
2571 case BIT_FIELD_REF:
2572 case OBJ_TYPE_REF:
2574 case REALPART_EXPR:
2575 case IMAGPART_EXPR:
2576 case PREINCREMENT_EXPR:
2577 case PREDECREMENT_EXPR:
2578 case SAVE_EXPR:
2579 case TRY_CATCH_EXPR:
2580 case WITH_CLEANUP_EXPR:
2581 case COMPOUND_EXPR:
2582 case MODIFY_EXPR:
2583 case TARGET_EXPR:
2584 case COND_EXPR:
2585 case BIND_EXPR:
2586 break;
2588 default:
2589 /* Assume the worst for front-end tree codes. */
2590 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2591 break;
2592 return false;
2595 return true;
2598 /* Return an expr equal to X but certainly not valid as an lvalue. */
2600 tree
2601 non_lvalue_loc (location_t loc, tree x)
2603 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2604 us. */
2605 if (in_gimple_form)
2606 return x;
2608 if (! maybe_lvalue_p (x))
2609 return x;
2610 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2613 /* When pedantic, return an expr equal to X but certainly not valid as a
2614 pedantic lvalue. Otherwise, return X. */
2616 static tree
2617 pedantic_non_lvalue_loc (location_t loc, tree x)
2619 return protected_set_expr_location_unshare (x, loc);
2622 /* Given a tree comparison code, return the code that is the logical inverse.
2623 It is generally not safe to do this for floating-point comparisons, except
2624 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2625 ERROR_MARK in this case. */
2627 enum tree_code
2628 invert_tree_comparison (enum tree_code code, bool honor_nans)
2630 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2631 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2632 return ERROR_MARK;
2634 switch (code)
2636 case EQ_EXPR:
2637 return NE_EXPR;
2638 case NE_EXPR:
2639 return EQ_EXPR;
2640 case GT_EXPR:
2641 return honor_nans ? UNLE_EXPR : LE_EXPR;
2642 case GE_EXPR:
2643 return honor_nans ? UNLT_EXPR : LT_EXPR;
2644 case LT_EXPR:
2645 return honor_nans ? UNGE_EXPR : GE_EXPR;
2646 case LE_EXPR:
2647 return honor_nans ? UNGT_EXPR : GT_EXPR;
2648 case LTGT_EXPR:
2649 return UNEQ_EXPR;
2650 case UNEQ_EXPR:
2651 return LTGT_EXPR;
2652 case UNGT_EXPR:
2653 return LE_EXPR;
2654 case UNGE_EXPR:
2655 return LT_EXPR;
2656 case UNLT_EXPR:
2657 return GE_EXPR;
2658 case UNLE_EXPR:
2659 return GT_EXPR;
2660 case ORDERED_EXPR:
2661 return UNORDERED_EXPR;
2662 case UNORDERED_EXPR:
2663 return ORDERED_EXPR;
2664 default:
2665 gcc_unreachable ();
2669 /* Similar, but return the comparison that results if the operands are
2670 swapped. This is safe for floating-point. */
2672 enum tree_code
2673 swap_tree_comparison (enum tree_code code)
2675 switch (code)
2677 case EQ_EXPR:
2678 case NE_EXPR:
2679 case ORDERED_EXPR:
2680 case UNORDERED_EXPR:
2681 case LTGT_EXPR:
2682 case UNEQ_EXPR:
2683 return code;
2684 case GT_EXPR:
2685 return LT_EXPR;
2686 case GE_EXPR:
2687 return LE_EXPR;
2688 case LT_EXPR:
2689 return GT_EXPR;
2690 case LE_EXPR:
2691 return GE_EXPR;
2692 case UNGT_EXPR:
2693 return UNLT_EXPR;
2694 case UNGE_EXPR:
2695 return UNLE_EXPR;
2696 case UNLT_EXPR:
2697 return UNGT_EXPR;
2698 case UNLE_EXPR:
2699 return UNGE_EXPR;
2700 default:
2701 gcc_unreachable ();
2706 /* Convert a comparison tree code from an enum tree_code representation
2707 into a compcode bit-based encoding. This function is the inverse of
2708 compcode_to_comparison. */
2710 static enum comparison_code
2711 comparison_to_compcode (enum tree_code code)
2713 switch (code)
2715 case LT_EXPR:
2716 return COMPCODE_LT;
2717 case EQ_EXPR:
2718 return COMPCODE_EQ;
2719 case LE_EXPR:
2720 return COMPCODE_LE;
2721 case GT_EXPR:
2722 return COMPCODE_GT;
2723 case NE_EXPR:
2724 return COMPCODE_NE;
2725 case GE_EXPR:
2726 return COMPCODE_GE;
2727 case ORDERED_EXPR:
2728 return COMPCODE_ORD;
2729 case UNORDERED_EXPR:
2730 return COMPCODE_UNORD;
2731 case UNLT_EXPR:
2732 return COMPCODE_UNLT;
2733 case UNEQ_EXPR:
2734 return COMPCODE_UNEQ;
2735 case UNLE_EXPR:
2736 return COMPCODE_UNLE;
2737 case UNGT_EXPR:
2738 return COMPCODE_UNGT;
2739 case LTGT_EXPR:
2740 return COMPCODE_LTGT;
2741 case UNGE_EXPR:
2742 return COMPCODE_UNGE;
2743 default:
2744 gcc_unreachable ();
2748 /* Convert a compcode bit-based encoding of a comparison operator back
2749 to GCC's enum tree_code representation. This function is the
2750 inverse of comparison_to_compcode. */
2752 static enum tree_code
2753 compcode_to_comparison (enum comparison_code code)
2755 switch (code)
2757 case COMPCODE_LT:
2758 return LT_EXPR;
2759 case COMPCODE_EQ:
2760 return EQ_EXPR;
2761 case COMPCODE_LE:
2762 return LE_EXPR;
2763 case COMPCODE_GT:
2764 return GT_EXPR;
2765 case COMPCODE_NE:
2766 return NE_EXPR;
2767 case COMPCODE_GE:
2768 return GE_EXPR;
2769 case COMPCODE_ORD:
2770 return ORDERED_EXPR;
2771 case COMPCODE_UNORD:
2772 return UNORDERED_EXPR;
2773 case COMPCODE_UNLT:
2774 return UNLT_EXPR;
2775 case COMPCODE_UNEQ:
2776 return UNEQ_EXPR;
2777 case COMPCODE_UNLE:
2778 return UNLE_EXPR;
2779 case COMPCODE_UNGT:
2780 return UNGT_EXPR;
2781 case COMPCODE_LTGT:
2782 return LTGT_EXPR;
2783 case COMPCODE_UNGE:
2784 return UNGE_EXPR;
2785 default:
2786 gcc_unreachable ();
2790 /* Return a tree for the comparison which is the combination of
2791 doing the AND or OR (depending on CODE) of the two operations LCODE
2792 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2793 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2794 if this makes the transformation invalid. */
2796 tree
2797 combine_comparisons (location_t loc,
2798 enum tree_code code, enum tree_code lcode,
2799 enum tree_code rcode, tree truth_type,
2800 tree ll_arg, tree lr_arg)
2802 bool honor_nans = HONOR_NANS (ll_arg);
2803 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2804 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2805 int compcode;
2807 switch (code)
2809 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2810 compcode = lcompcode & rcompcode;
2811 break;
2813 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2814 compcode = lcompcode | rcompcode;
2815 break;
2817 default:
2818 return NULL_TREE;
2821 if (!honor_nans)
2823 /* Eliminate unordered comparisons, as well as LTGT and ORD
2824 which are not used unless the mode has NaNs. */
2825 compcode &= ~COMPCODE_UNORD;
2826 if (compcode == COMPCODE_LTGT)
2827 compcode = COMPCODE_NE;
2828 else if (compcode == COMPCODE_ORD)
2829 compcode = COMPCODE_TRUE;
2831 else if (flag_trapping_math)
2833 /* Check that the original operation and the optimized ones will trap
2834 under the same condition. */
2835 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2836 && (lcompcode != COMPCODE_EQ)
2837 && (lcompcode != COMPCODE_ORD);
2838 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2839 && (rcompcode != COMPCODE_EQ)
2840 && (rcompcode != COMPCODE_ORD);
2841 bool trap = (compcode & COMPCODE_UNORD) == 0
2842 && (compcode != COMPCODE_EQ)
2843 && (compcode != COMPCODE_ORD);
2845 /* In a short-circuited boolean expression the LHS might be
2846 such that the RHS, if evaluated, will never trap. For
2847 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2848 if neither x nor y is NaN. (This is a mixed blessing: for
2849 example, the expression above will never trap, hence
2850 optimizing it to x < y would be invalid). */
2851 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2852 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2853 rtrap = false;
2855 /* If the comparison was short-circuited, and only the RHS
2856 trapped, we may now generate a spurious trap. */
2857 if (rtrap && !ltrap
2858 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2859 return NULL_TREE;
2861 /* If we changed the conditions that cause a trap, we lose. */
2862 if ((ltrap || rtrap) != trap)
2863 return NULL_TREE;
2866 if (compcode == COMPCODE_TRUE)
2867 return constant_boolean_node (true, truth_type);
2868 else if (compcode == COMPCODE_FALSE)
2869 return constant_boolean_node (false, truth_type);
2870 else
2872 enum tree_code tcode;
2874 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2875 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2879 /* Return nonzero if two operands (typically of the same tree node)
2880 are necessarily equal. FLAGS modifies behavior as follows:
2882 If OEP_ONLY_CONST is set, only return nonzero for constants.
2883 This function tests whether the operands are indistinguishable;
2884 it does not test whether they are equal using C's == operation.
2885 The distinction is important for IEEE floating point, because
2886 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2887 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2889 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2890 even though it may hold multiple values during a function.
2891 This is because a GCC tree node guarantees that nothing else is
2892 executed between the evaluation of its "operands" (which may often
2893 be evaluated in arbitrary order). Hence if the operands themselves
2894 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2895 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2896 unset means assuming isochronic (or instantaneous) tree equivalence.
2897 Unless comparing arbitrary expression trees, such as from different
2898 statements, this flag can usually be left unset.
2900 If OEP_PURE_SAME is set, then pure functions with identical arguments
2901 are considered the same. It is used when the caller has other ways
2902 to ensure that global memory is unchanged in between.
2904 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2905 not values of expressions.
2907 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2908 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2910 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2911 any operand with side effect. This is unnecesarily conservative in the
2912 case we know that arg0 and arg1 are in disjoint code paths (such as in
2913 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2914 addresses with TREE_CONSTANT flag set so we know that &var == &var
2915 even if var is volatile. */
2918 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2920 /* When checking, verify at the outermost operand_equal_p call that
2921 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2922 hash value. */
2923 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2925 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2927 if (arg0 != arg1)
2929 inchash::hash hstate0 (0), hstate1 (0);
2930 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2931 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2932 hashval_t h0 = hstate0.end ();
2933 hashval_t h1 = hstate1.end ();
2934 gcc_assert (h0 == h1);
2936 return 1;
2938 else
2939 return 0;
2942 /* If either is ERROR_MARK, they aren't equal. */
2943 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2944 || TREE_TYPE (arg0) == error_mark_node
2945 || TREE_TYPE (arg1) == error_mark_node)
2946 return 0;
2948 /* Similar, if either does not have a type (like a released SSA name),
2949 they aren't equal. */
2950 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2951 return 0;
2953 /* We cannot consider pointers to different address space equal. */
2954 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2955 && POINTER_TYPE_P (TREE_TYPE (arg1))
2956 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2957 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2958 return 0;
2960 /* Check equality of integer constants before bailing out due to
2961 precision differences. */
2962 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2964 /* Address of INTEGER_CST is not defined; check that we did not forget
2965 to drop the OEP_ADDRESS_OF flags. */
2966 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2967 return tree_int_cst_equal (arg0, arg1);
2970 if (!(flags & OEP_ADDRESS_OF))
2972 /* If both types don't have the same signedness, then we can't consider
2973 them equal. We must check this before the STRIP_NOPS calls
2974 because they may change the signedness of the arguments. As pointers
2975 strictly don't have a signedness, require either two pointers or
2976 two non-pointers as well. */
2977 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2978 || POINTER_TYPE_P (TREE_TYPE (arg0))
2979 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2980 return 0;
2982 /* If both types don't have the same precision, then it is not safe
2983 to strip NOPs. */
2984 if (element_precision (TREE_TYPE (arg0))
2985 != element_precision (TREE_TYPE (arg1)))
2986 return 0;
2988 STRIP_NOPS (arg0);
2989 STRIP_NOPS (arg1);
2991 #if 0
2992 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2993 sanity check once the issue is solved. */
2994 else
2995 /* Addresses of conversions and SSA_NAMEs (and many other things)
2996 are not defined. Check that we did not forget to drop the
2997 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2998 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2999 && TREE_CODE (arg0) != SSA_NAME);
3000 #endif
3002 /* In case both args are comparisons but with different comparison
3003 code, try to swap the comparison operands of one arg to produce
3004 a match and compare that variant. */
3005 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3006 && COMPARISON_CLASS_P (arg0)
3007 && COMPARISON_CLASS_P (arg1))
3009 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3011 if (TREE_CODE (arg0) == swap_code)
3012 return operand_equal_p (TREE_OPERAND (arg0, 0),
3013 TREE_OPERAND (arg1, 1), flags)
3014 && operand_equal_p (TREE_OPERAND (arg0, 1),
3015 TREE_OPERAND (arg1, 0), flags);
3018 if (TREE_CODE (arg0) != TREE_CODE (arg1))
3020 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3021 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3023 else if (flags & OEP_ADDRESS_OF)
3025 /* If we are interested in comparing addresses ignore
3026 MEM_REF wrappings of the base that can appear just for
3027 TBAA reasons. */
3028 if (TREE_CODE (arg0) == MEM_REF
3029 && DECL_P (arg1)
3030 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3031 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3032 && integer_zerop (TREE_OPERAND (arg0, 1)))
3033 return 1;
3034 else if (TREE_CODE (arg1) == MEM_REF
3035 && DECL_P (arg0)
3036 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3037 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3038 && integer_zerop (TREE_OPERAND (arg1, 1)))
3039 return 1;
3040 return 0;
3042 else
3043 return 0;
3046 /* When not checking adddresses, this is needed for conversions and for
3047 COMPONENT_REF. Might as well play it safe and always test this. */
3048 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3049 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3050 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
3051 && !(flags & OEP_ADDRESS_OF)))
3052 return 0;
3054 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3055 We don't care about side effects in that case because the SAVE_EXPR
3056 takes care of that for us. In all other cases, two expressions are
3057 equal if they have no side effects. If we have two identical
3058 expressions with side effects that should be treated the same due
3059 to the only side effects being identical SAVE_EXPR's, that will
3060 be detected in the recursive calls below.
3061 If we are taking an invariant address of two identical objects
3062 they are necessarily equal as well. */
3063 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3064 && (TREE_CODE (arg0) == SAVE_EXPR
3065 || (flags & OEP_MATCH_SIDE_EFFECTS)
3066 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3067 return 1;
3069 /* Next handle constant cases, those for which we can return 1 even
3070 if ONLY_CONST is set. */
3071 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3072 switch (TREE_CODE (arg0))
3074 case INTEGER_CST:
3075 return tree_int_cst_equal (arg0, arg1);
3077 case FIXED_CST:
3078 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3079 TREE_FIXED_CST (arg1));
3081 case REAL_CST:
3082 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3083 return 1;
3086 if (!HONOR_SIGNED_ZEROS (arg0))
3088 /* If we do not distinguish between signed and unsigned zero,
3089 consider them equal. */
3090 if (real_zerop (arg0) && real_zerop (arg1))
3091 return 1;
3093 return 0;
3095 case VECTOR_CST:
3097 if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3098 != VECTOR_CST_LOG2_NPATTERNS (arg1))
3099 return 0;
3101 if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3102 != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3103 return 0;
3105 unsigned int count = vector_cst_encoded_nelts (arg0);
3106 for (unsigned int i = 0; i < count; ++i)
3107 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3108 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3109 return 0;
3110 return 1;
3113 case COMPLEX_CST:
3114 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3115 flags)
3116 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3117 flags));
3119 case STRING_CST:
3120 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3121 && ! memcmp (TREE_STRING_POINTER (arg0),
3122 TREE_STRING_POINTER (arg1),
3123 TREE_STRING_LENGTH (arg0)));
3125 case ADDR_EXPR:
3126 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3127 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3128 flags | OEP_ADDRESS_OF
3129 | OEP_MATCH_SIDE_EFFECTS);
3130 case CONSTRUCTOR:
3131 /* In GIMPLE empty constructors are allowed in initializers of
3132 aggregates. */
3133 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
3134 default:
3135 break;
3138 if (flags & OEP_ONLY_CONST)
3139 return 0;
3141 /* Define macros to test an operand from arg0 and arg1 for equality and a
3142 variant that allows null and views null as being different from any
3143 non-null value. In the latter case, if either is null, the both
3144 must be; otherwise, do the normal comparison. */
3145 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3146 TREE_OPERAND (arg1, N), flags)
3148 #define OP_SAME_WITH_NULL(N) \
3149 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3150 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3152 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3154 case tcc_unary:
3155 /* Two conversions are equal only if signedness and modes match. */
3156 switch (TREE_CODE (arg0))
3158 CASE_CONVERT:
3159 case FIX_TRUNC_EXPR:
3160 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3161 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3162 return 0;
3163 break;
3164 default:
3165 break;
3168 return OP_SAME (0);
3171 case tcc_comparison:
3172 case tcc_binary:
3173 if (OP_SAME (0) && OP_SAME (1))
3174 return 1;
3176 /* For commutative ops, allow the other order. */
3177 return (commutative_tree_code (TREE_CODE (arg0))
3178 && operand_equal_p (TREE_OPERAND (arg0, 0),
3179 TREE_OPERAND (arg1, 1), flags)
3180 && operand_equal_p (TREE_OPERAND (arg0, 1),
3181 TREE_OPERAND (arg1, 0), flags));
3183 case tcc_reference:
3184 /* If either of the pointer (or reference) expressions we are
3185 dereferencing contain a side effect, these cannot be equal,
3186 but their addresses can be. */
3187 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3188 && (TREE_SIDE_EFFECTS (arg0)
3189 || TREE_SIDE_EFFECTS (arg1)))
3190 return 0;
3192 switch (TREE_CODE (arg0))
3194 case INDIRECT_REF:
3195 if (!(flags & OEP_ADDRESS_OF)
3196 && (TYPE_ALIGN (TREE_TYPE (arg0))
3197 != TYPE_ALIGN (TREE_TYPE (arg1))))
3198 return 0;
3199 flags &= ~OEP_ADDRESS_OF;
3200 return OP_SAME (0);
3202 case IMAGPART_EXPR:
3203 /* Require the same offset. */
3204 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3205 TYPE_SIZE (TREE_TYPE (arg1)),
3206 flags & ~OEP_ADDRESS_OF))
3207 return 0;
3209 /* Fallthru. */
3210 case REALPART_EXPR:
3211 case VIEW_CONVERT_EXPR:
3212 return OP_SAME (0);
3214 case TARGET_MEM_REF:
3215 case MEM_REF:
3216 if (!(flags & OEP_ADDRESS_OF))
3218 /* Require equal access sizes */
3219 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3220 && (!TYPE_SIZE (TREE_TYPE (arg0))
3221 || !TYPE_SIZE (TREE_TYPE (arg1))
3222 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3223 TYPE_SIZE (TREE_TYPE (arg1)),
3224 flags)))
3225 return 0;
3226 /* Verify that access happens in similar types. */
3227 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3228 return 0;
3229 /* Verify that accesses are TBAA compatible. */
3230 if (!alias_ptr_types_compatible_p
3231 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3232 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3233 || (MR_DEPENDENCE_CLIQUE (arg0)
3234 != MR_DEPENDENCE_CLIQUE (arg1))
3235 || (MR_DEPENDENCE_BASE (arg0)
3236 != MR_DEPENDENCE_BASE (arg1)))
3237 return 0;
3238 /* Verify that alignment is compatible. */
3239 if (TYPE_ALIGN (TREE_TYPE (arg0))
3240 != TYPE_ALIGN (TREE_TYPE (arg1)))
3241 return 0;
3243 flags &= ~OEP_ADDRESS_OF;
3244 return (OP_SAME (0) && OP_SAME (1)
3245 /* TARGET_MEM_REF require equal extra operands. */
3246 && (TREE_CODE (arg0) != TARGET_MEM_REF
3247 || (OP_SAME_WITH_NULL (2)
3248 && OP_SAME_WITH_NULL (3)
3249 && OP_SAME_WITH_NULL (4))));
3251 case ARRAY_REF:
3252 case ARRAY_RANGE_REF:
3253 if (!OP_SAME (0))
3254 return 0;
3255 flags &= ~OEP_ADDRESS_OF;
3256 /* Compare the array index by value if it is constant first as we
3257 may have different types but same value here. */
3258 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3259 TREE_OPERAND (arg1, 1))
3260 || OP_SAME (1))
3261 && OP_SAME_WITH_NULL (2)
3262 && OP_SAME_WITH_NULL (3)
3263 /* Compare low bound and element size as with OEP_ADDRESS_OF
3264 we have to account for the offset of the ref. */
3265 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3266 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3267 || (operand_equal_p (array_ref_low_bound
3268 (CONST_CAST_TREE (arg0)),
3269 array_ref_low_bound
3270 (CONST_CAST_TREE (arg1)), flags)
3271 && operand_equal_p (array_ref_element_size
3272 (CONST_CAST_TREE (arg0)),
3273 array_ref_element_size
3274 (CONST_CAST_TREE (arg1)),
3275 flags))));
3277 case COMPONENT_REF:
3278 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3279 may be NULL when we're called to compare MEM_EXPRs. */
3280 if (!OP_SAME_WITH_NULL (0)
3281 || !OP_SAME (1))
3282 return 0;
3283 flags &= ~OEP_ADDRESS_OF;
3284 return OP_SAME_WITH_NULL (2);
3286 case BIT_FIELD_REF:
3287 if (!OP_SAME (0))
3288 return 0;
3289 flags &= ~OEP_ADDRESS_OF;
3290 return OP_SAME (1) && OP_SAME (2);
3292 default:
3293 return 0;
3296 case tcc_expression:
3297 switch (TREE_CODE (arg0))
3299 case ADDR_EXPR:
3300 /* Be sure we pass right ADDRESS_OF flag. */
3301 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3302 return operand_equal_p (TREE_OPERAND (arg0, 0),
3303 TREE_OPERAND (arg1, 0),
3304 flags | OEP_ADDRESS_OF);
3306 case TRUTH_NOT_EXPR:
3307 return OP_SAME (0);
3309 case TRUTH_ANDIF_EXPR:
3310 case TRUTH_ORIF_EXPR:
3311 return OP_SAME (0) && OP_SAME (1);
3313 case WIDEN_MULT_PLUS_EXPR:
3314 case WIDEN_MULT_MINUS_EXPR:
3315 if (!OP_SAME (2))
3316 return 0;
3317 /* The multiplcation operands are commutative. */
3318 /* FALLTHRU */
3320 case TRUTH_AND_EXPR:
3321 case TRUTH_OR_EXPR:
3322 case TRUTH_XOR_EXPR:
3323 if (OP_SAME (0) && OP_SAME (1))
3324 return 1;
3326 /* Otherwise take into account this is a commutative operation. */
3327 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3328 TREE_OPERAND (arg1, 1), flags)
3329 && operand_equal_p (TREE_OPERAND (arg0, 1),
3330 TREE_OPERAND (arg1, 0), flags));
3332 case COND_EXPR:
3333 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3334 return 0;
3335 flags &= ~OEP_ADDRESS_OF;
3336 return OP_SAME (0);
3338 case BIT_INSERT_EXPR:
3339 /* BIT_INSERT_EXPR has an implict operand as the type precision
3340 of op1. Need to check to make sure they are the same. */
3341 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3342 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3343 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3344 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3345 return false;
3346 /* FALLTHRU */
3348 case VEC_COND_EXPR:
3349 case DOT_PROD_EXPR:
3350 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3352 case MODIFY_EXPR:
3353 case INIT_EXPR:
3354 case COMPOUND_EXPR:
3355 case PREDECREMENT_EXPR:
3356 case PREINCREMENT_EXPR:
3357 case POSTDECREMENT_EXPR:
3358 case POSTINCREMENT_EXPR:
3359 if (flags & OEP_LEXICOGRAPHIC)
3360 return OP_SAME (0) && OP_SAME (1);
3361 return 0;
3363 case CLEANUP_POINT_EXPR:
3364 case EXPR_STMT:
3365 case SAVE_EXPR:
3366 if (flags & OEP_LEXICOGRAPHIC)
3367 return OP_SAME (0);
3368 return 0;
3370 default:
3371 return 0;
3374 case tcc_vl_exp:
3375 switch (TREE_CODE (arg0))
3377 case CALL_EXPR:
3378 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3379 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3380 /* If not both CALL_EXPRs are either internal or normal function
3381 functions, then they are not equal. */
3382 return 0;
3383 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3385 /* If the CALL_EXPRs call different internal functions, then they
3386 are not equal. */
3387 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3388 return 0;
3390 else
3392 /* If the CALL_EXPRs call different functions, then they are not
3393 equal. */
3394 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3395 flags))
3396 return 0;
3399 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3401 unsigned int cef = call_expr_flags (arg0);
3402 if (flags & OEP_PURE_SAME)
3403 cef &= ECF_CONST | ECF_PURE;
3404 else
3405 cef &= ECF_CONST;
3406 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3407 return 0;
3410 /* Now see if all the arguments are the same. */
3412 const_call_expr_arg_iterator iter0, iter1;
3413 const_tree a0, a1;
3414 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3415 a1 = first_const_call_expr_arg (arg1, &iter1);
3416 a0 && a1;
3417 a0 = next_const_call_expr_arg (&iter0),
3418 a1 = next_const_call_expr_arg (&iter1))
3419 if (! operand_equal_p (a0, a1, flags))
3420 return 0;
3422 /* If we get here and both argument lists are exhausted
3423 then the CALL_EXPRs are equal. */
3424 return ! (a0 || a1);
3426 default:
3427 return 0;
3430 case tcc_declaration:
3431 /* Consider __builtin_sqrt equal to sqrt. */
3432 return (TREE_CODE (arg0) == FUNCTION_DECL
3433 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3434 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3435 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3437 case tcc_exceptional:
3438 if (TREE_CODE (arg0) == CONSTRUCTOR)
3440 /* In GIMPLE constructors are used only to build vectors from
3441 elements. Individual elements in the constructor must be
3442 indexed in increasing order and form an initial sequence.
3444 We make no effort to compare constructors in generic.
3445 (see sem_variable::equals in ipa-icf which can do so for
3446 constants). */
3447 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3448 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3449 return 0;
3451 /* Be sure that vectors constructed have the same representation.
3452 We only tested element precision and modes to match.
3453 Vectors may be BLKmode and thus also check that the number of
3454 parts match. */
3455 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
3456 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))))
3457 return 0;
3459 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3460 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3461 unsigned int len = vec_safe_length (v0);
3463 if (len != vec_safe_length (v1))
3464 return 0;
3466 for (unsigned int i = 0; i < len; i++)
3468 constructor_elt *c0 = &(*v0)[i];
3469 constructor_elt *c1 = &(*v1)[i];
3471 if (!operand_equal_p (c0->value, c1->value, flags)
3472 /* In GIMPLE the indexes can be either NULL or matching i.
3473 Double check this so we won't get false
3474 positives for GENERIC. */
3475 || (c0->index
3476 && (TREE_CODE (c0->index) != INTEGER_CST
3477 || !compare_tree_int (c0->index, i)))
3478 || (c1->index
3479 && (TREE_CODE (c1->index) != INTEGER_CST
3480 || !compare_tree_int (c1->index, i))))
3481 return 0;
3483 return 1;
3485 else if (TREE_CODE (arg0) == STATEMENT_LIST
3486 && (flags & OEP_LEXICOGRAPHIC))
3488 /* Compare the STATEMENT_LISTs. */
3489 tree_stmt_iterator tsi1, tsi2;
3490 tree body1 = CONST_CAST_TREE (arg0);
3491 tree body2 = CONST_CAST_TREE (arg1);
3492 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3493 tsi_next (&tsi1), tsi_next (&tsi2))
3495 /* The lists don't have the same number of statements. */
3496 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3497 return 0;
3498 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3499 return 1;
3500 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3501 flags & (OEP_LEXICOGRAPHIC
3502 | OEP_NO_HASH_CHECK)))
3503 return 0;
3506 return 0;
3508 case tcc_statement:
3509 switch (TREE_CODE (arg0))
3511 case RETURN_EXPR:
3512 if (flags & OEP_LEXICOGRAPHIC)
3513 return OP_SAME_WITH_NULL (0);
3514 return 0;
3515 case DEBUG_BEGIN_STMT:
3516 if (flags & OEP_LEXICOGRAPHIC)
3517 return 1;
3518 return 0;
3519 default:
3520 return 0;
3523 default:
3524 return 0;
3527 #undef OP_SAME
3528 #undef OP_SAME_WITH_NULL
3531 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
3532 with a different signedness or a narrower precision. */
3534 static bool
3535 operand_equal_for_comparison_p (tree arg0, tree arg1)
3537 if (operand_equal_p (arg0, arg1, 0))
3538 return true;
3540 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3541 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3542 return false;
3544 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3545 and see if the inner values are the same. This removes any
3546 signedness comparison, which doesn't matter here. */
3547 tree op0 = arg0;
3548 tree op1 = arg1;
3549 STRIP_NOPS (op0);
3550 STRIP_NOPS (op1);
3551 if (operand_equal_p (op0, op1, 0))
3552 return true;
3554 /* Discard a single widening conversion from ARG1 and see if the inner
3555 value is the same as ARG0. */
3556 if (CONVERT_EXPR_P (arg1)
3557 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3558 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3559 < TYPE_PRECISION (TREE_TYPE (arg1))
3560 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
3561 return true;
3563 return false;
3566 /* See if ARG is an expression that is either a comparison or is performing
3567 arithmetic on comparisons. The comparisons must only be comparing
3568 two different values, which will be stored in *CVAL1 and *CVAL2; if
3569 they are nonzero it means that some operands have already been found.
3570 No variables may be used anywhere else in the expression except in the
3571 comparisons.
3573 If this is true, return 1. Otherwise, return zero. */
3575 static int
3576 twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
3578 enum tree_code code = TREE_CODE (arg);
3579 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3581 /* We can handle some of the tcc_expression cases here. */
3582 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3583 tclass = tcc_unary;
3584 else if (tclass == tcc_expression
3585 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3586 || code == COMPOUND_EXPR))
3587 tclass = tcc_binary;
3589 switch (tclass)
3591 case tcc_unary:
3592 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
3594 case tcc_binary:
3595 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
3596 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
3598 case tcc_constant:
3599 return 1;
3601 case tcc_expression:
3602 if (code == COND_EXPR)
3603 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
3604 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
3605 && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
3606 return 0;
3608 case tcc_comparison:
3609 /* First see if we can handle the first operand, then the second. For
3610 the second operand, we know *CVAL1 can't be zero. It must be that
3611 one side of the comparison is each of the values; test for the
3612 case where this isn't true by failing if the two operands
3613 are the same. */
3615 if (operand_equal_p (TREE_OPERAND (arg, 0),
3616 TREE_OPERAND (arg, 1), 0))
3617 return 0;
3619 if (*cval1 == 0)
3620 *cval1 = TREE_OPERAND (arg, 0);
3621 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3623 else if (*cval2 == 0)
3624 *cval2 = TREE_OPERAND (arg, 0);
3625 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3627 else
3628 return 0;
3630 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3632 else if (*cval2 == 0)
3633 *cval2 = TREE_OPERAND (arg, 1);
3634 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3636 else
3637 return 0;
3639 return 1;
3641 default:
3642 return 0;
3646 /* ARG is a tree that is known to contain just arithmetic operations and
3647 comparisons. Evaluate the operations in the tree substituting NEW0 for
3648 any occurrence of OLD0 as an operand of a comparison and likewise for
3649 NEW1 and OLD1. */
3651 static tree
3652 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3653 tree old1, tree new1)
3655 tree type = TREE_TYPE (arg);
3656 enum tree_code code = TREE_CODE (arg);
3657 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3659 /* We can handle some of the tcc_expression cases here. */
3660 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3661 tclass = tcc_unary;
3662 else if (tclass == tcc_expression
3663 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3664 tclass = tcc_binary;
3666 switch (tclass)
3668 case tcc_unary:
3669 return fold_build1_loc (loc, code, type,
3670 eval_subst (loc, TREE_OPERAND (arg, 0),
3671 old0, new0, old1, new1));
3673 case tcc_binary:
3674 return fold_build2_loc (loc, code, type,
3675 eval_subst (loc, TREE_OPERAND (arg, 0),
3676 old0, new0, old1, new1),
3677 eval_subst (loc, TREE_OPERAND (arg, 1),
3678 old0, new0, old1, new1));
3680 case tcc_expression:
3681 switch (code)
3683 case SAVE_EXPR:
3684 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3685 old1, new1);
3687 case COMPOUND_EXPR:
3688 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3689 old1, new1);
3691 case COND_EXPR:
3692 return fold_build3_loc (loc, code, type,
3693 eval_subst (loc, TREE_OPERAND (arg, 0),
3694 old0, new0, old1, new1),
3695 eval_subst (loc, TREE_OPERAND (arg, 1),
3696 old0, new0, old1, new1),
3697 eval_subst (loc, TREE_OPERAND (arg, 2),
3698 old0, new0, old1, new1));
3699 default:
3700 break;
3702 /* Fall through - ??? */
3704 case tcc_comparison:
3706 tree arg0 = TREE_OPERAND (arg, 0);
3707 tree arg1 = TREE_OPERAND (arg, 1);
3709 /* We need to check both for exact equality and tree equality. The
3710 former will be true if the operand has a side-effect. In that
3711 case, we know the operand occurred exactly once. */
3713 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3714 arg0 = new0;
3715 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3716 arg0 = new1;
3718 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3719 arg1 = new0;
3720 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3721 arg1 = new1;
3723 return fold_build2_loc (loc, code, type, arg0, arg1);
3726 default:
3727 return arg;
3731 /* Return a tree for the case when the result of an expression is RESULT
3732 converted to TYPE and OMITTED was previously an operand of the expression
3733 but is now not needed (e.g., we folded OMITTED * 0).
3735 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3736 the conversion of RESULT to TYPE. */
3738 tree
3739 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3741 tree t = fold_convert_loc (loc, type, result);
3743 /* If the resulting operand is an empty statement, just return the omitted
3744 statement casted to void. */
3745 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3746 return build1_loc (loc, NOP_EXPR, void_type_node,
3747 fold_ignored_result (omitted));
3749 if (TREE_SIDE_EFFECTS (omitted))
3750 return build2_loc (loc, COMPOUND_EXPR, type,
3751 fold_ignored_result (omitted), t);
3753 return non_lvalue_loc (loc, t);
3756 /* Return a tree for the case when the result of an expression is RESULT
3757 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3758 of the expression but are now not needed.
3760 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3761 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3762 evaluated before OMITTED2. Otherwise, if neither has side effects,
3763 just do the conversion of RESULT to TYPE. */
3765 tree
3766 omit_two_operands_loc (location_t loc, tree type, tree result,
3767 tree omitted1, tree omitted2)
3769 tree t = fold_convert_loc (loc, type, result);
3771 if (TREE_SIDE_EFFECTS (omitted2))
3772 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3773 if (TREE_SIDE_EFFECTS (omitted1))
3774 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3776 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3780 /* Return a simplified tree node for the truth-negation of ARG. This
3781 never alters ARG itself. We assume that ARG is an operation that
3782 returns a truth value (0 or 1).
3784 FIXME: one would think we would fold the result, but it causes
3785 problems with the dominator optimizer. */
3787 static tree
3788 fold_truth_not_expr (location_t loc, tree arg)
3790 tree type = TREE_TYPE (arg);
3791 enum tree_code code = TREE_CODE (arg);
3792 location_t loc1, loc2;
3794 /* If this is a comparison, we can simply invert it, except for
3795 floating-point non-equality comparisons, in which case we just
3796 enclose a TRUTH_NOT_EXPR around what we have. */
3798 if (TREE_CODE_CLASS (code) == tcc_comparison)
3800 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3801 if (FLOAT_TYPE_P (op_type)
3802 && flag_trapping_math
3803 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3804 && code != NE_EXPR && code != EQ_EXPR)
3805 return NULL_TREE;
3807 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3808 if (code == ERROR_MARK)
3809 return NULL_TREE;
3811 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3812 TREE_OPERAND (arg, 1));
3813 if (TREE_NO_WARNING (arg))
3814 TREE_NO_WARNING (ret) = 1;
3815 return ret;
3818 switch (code)
3820 case INTEGER_CST:
3821 return constant_boolean_node (integer_zerop (arg), type);
3823 case TRUTH_AND_EXPR:
3824 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3825 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3826 return build2_loc (loc, TRUTH_OR_EXPR, type,
3827 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3828 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3830 case TRUTH_OR_EXPR:
3831 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3832 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3833 return build2_loc (loc, TRUTH_AND_EXPR, type,
3834 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3835 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3837 case TRUTH_XOR_EXPR:
3838 /* Here we can invert either operand. We invert the first operand
3839 unless the second operand is a TRUTH_NOT_EXPR in which case our
3840 result is the XOR of the first operand with the inside of the
3841 negation of the second operand. */
3843 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3844 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3845 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3846 else
3847 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3848 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3849 TREE_OPERAND (arg, 1));
3851 case TRUTH_ANDIF_EXPR:
3852 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3853 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3854 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3855 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3856 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3858 case TRUTH_ORIF_EXPR:
3859 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3860 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3861 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3862 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3863 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3865 case TRUTH_NOT_EXPR:
3866 return TREE_OPERAND (arg, 0);
3868 case COND_EXPR:
3870 tree arg1 = TREE_OPERAND (arg, 1);
3871 tree arg2 = TREE_OPERAND (arg, 2);
3873 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3874 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3876 /* A COND_EXPR may have a throw as one operand, which
3877 then has void type. Just leave void operands
3878 as they are. */
3879 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3880 VOID_TYPE_P (TREE_TYPE (arg1))
3881 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3882 VOID_TYPE_P (TREE_TYPE (arg2))
3883 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3886 case COMPOUND_EXPR:
3887 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3888 return build2_loc (loc, COMPOUND_EXPR, type,
3889 TREE_OPERAND (arg, 0),
3890 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3892 case NON_LVALUE_EXPR:
3893 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3894 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3896 CASE_CONVERT:
3897 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3898 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3900 /* fall through */
3902 case FLOAT_EXPR:
3903 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3904 return build1_loc (loc, TREE_CODE (arg), type,
3905 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3907 case BIT_AND_EXPR:
3908 if (!integer_onep (TREE_OPERAND (arg, 1)))
3909 return NULL_TREE;
3910 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3912 case SAVE_EXPR:
3913 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3915 case CLEANUP_POINT_EXPR:
3916 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3917 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3918 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3920 default:
3921 return NULL_TREE;
3925 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3926 assume that ARG is an operation that returns a truth value (0 or 1
3927 for scalars, 0 or -1 for vectors). Return the folded expression if
3928 folding is successful. Otherwise, return NULL_TREE. */
3930 static tree
3931 fold_invert_truthvalue (location_t loc, tree arg)
3933 tree type = TREE_TYPE (arg);
3934 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3935 ? BIT_NOT_EXPR
3936 : TRUTH_NOT_EXPR,
3937 type, arg);
3940 /* Return a simplified tree node for the truth-negation of ARG. This
3941 never alters ARG itself. We assume that ARG is an operation that
3942 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3944 tree
3945 invert_truthvalue_loc (location_t loc, tree arg)
3947 if (TREE_CODE (arg) == ERROR_MARK)
3948 return arg;
3950 tree type = TREE_TYPE (arg);
3951 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3952 ? BIT_NOT_EXPR
3953 : TRUTH_NOT_EXPR,
3954 type, arg);
3957 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3958 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3959 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3960 is the original memory reference used to preserve the alias set of
3961 the access. */
3963 static tree
3964 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3965 HOST_WIDE_INT bitsize, poly_int64 bitpos,
3966 int unsignedp, int reversep)
3968 tree result, bftype;
3970 /* Attempt not to lose the access path if possible. */
3971 if (TREE_CODE (orig_inner) == COMPONENT_REF)
3973 tree ninner = TREE_OPERAND (orig_inner, 0);
3974 machine_mode nmode;
3975 poly_int64 nbitsize, nbitpos;
3976 tree noffset;
3977 int nunsignedp, nreversep, nvolatilep = 0;
3978 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
3979 &noffset, &nmode, &nunsignedp,
3980 &nreversep, &nvolatilep);
3981 if (base == inner
3982 && noffset == NULL_TREE
3983 && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
3984 && !reversep
3985 && !nreversep
3986 && !nvolatilep)
3988 inner = ninner;
3989 bitpos -= nbitpos;
3993 alias_set_type iset = get_alias_set (orig_inner);
3994 if (iset == 0 && get_alias_set (inner) != iset)
3995 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3996 build_fold_addr_expr (inner),
3997 build_int_cst (ptr_type_node, 0));
3999 if (known_eq (bitpos, 0) && !reversep)
4001 tree size = TYPE_SIZE (TREE_TYPE (inner));
4002 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4003 || POINTER_TYPE_P (TREE_TYPE (inner)))
4004 && tree_fits_shwi_p (size)
4005 && tree_to_shwi (size) == bitsize)
4006 return fold_convert_loc (loc, type, inner);
4009 bftype = type;
4010 if (TYPE_PRECISION (bftype) != bitsize
4011 || TYPE_UNSIGNED (bftype) == !unsignedp)
4012 bftype = build_nonstandard_integer_type (bitsize, 0);
4014 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4015 bitsize_int (bitsize), bitsize_int (bitpos));
4016 REF_REVERSE_STORAGE_ORDER (result) = reversep;
4018 if (bftype != type)
4019 result = fold_convert_loc (loc, type, result);
4021 return result;
4024 /* Optimize a bit-field compare.
4026 There are two cases: First is a compare against a constant and the
4027 second is a comparison of two items where the fields are at the same
4028 bit position relative to the start of a chunk (byte, halfword, word)
4029 large enough to contain it. In these cases we can avoid the shift
4030 implicit in bitfield extractions.
4032 For constants, we emit a compare of the shifted constant with the
4033 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4034 compared. For two fields at the same position, we do the ANDs with the
4035 similar mask and compare the result of the ANDs.
4037 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4038 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4039 are the left and right operands of the comparison, respectively.
4041 If the optimization described above can be done, we return the resulting
4042 tree. Otherwise we return zero. */
4044 static tree
4045 optimize_bit_field_compare (location_t loc, enum tree_code code,
4046 tree compare_type, tree lhs, tree rhs)
4048 poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4049 HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4050 tree type = TREE_TYPE (lhs);
4051 tree unsigned_type;
4052 int const_p = TREE_CODE (rhs) == INTEGER_CST;
4053 machine_mode lmode, rmode;
4054 scalar_int_mode nmode;
4055 int lunsignedp, runsignedp;
4056 int lreversep, rreversep;
4057 int lvolatilep = 0, rvolatilep = 0;
4058 tree linner, rinner = NULL_TREE;
4059 tree mask;
4060 tree offset;
4062 /* Get all the information about the extractions being done. If the bit size
4063 is the same as the size of the underlying object, we aren't doing an
4064 extraction at all and so can do nothing. We also don't want to
4065 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4066 then will no longer be able to replace it. */
4067 linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4068 &lunsignedp, &lreversep, &lvolatilep);
4069 if (linner == lhs
4070 || !known_size_p (plbitsize)
4071 || !plbitsize.is_constant (&lbitsize)
4072 || !plbitpos.is_constant (&lbitpos)
4073 || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4074 || offset != 0
4075 || TREE_CODE (linner) == PLACEHOLDER_EXPR
4076 || lvolatilep)
4077 return 0;
4079 if (const_p)
4080 rreversep = lreversep;
4081 else
4083 /* If this is not a constant, we can only do something if bit positions,
4084 sizes, signedness and storage order are the same. */
4085 rinner
4086 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4087 &runsignedp, &rreversep, &rvolatilep);
4089 if (rinner == rhs
4090 || maybe_ne (lbitpos, rbitpos)
4091 || maybe_ne (lbitsize, rbitsize)
4092 || lunsignedp != runsignedp
4093 || lreversep != rreversep
4094 || offset != 0
4095 || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4096 || rvolatilep)
4097 return 0;
4100 /* Honor the C++ memory model and mimic what RTL expansion does. */
4101 poly_uint64 bitstart = 0;
4102 poly_uint64 bitend = 0;
4103 if (TREE_CODE (lhs) == COMPONENT_REF)
4105 get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4106 if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4107 return 0;
4110 /* See if we can find a mode to refer to this field. We should be able to,
4111 but fail if we can't. */
4112 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4113 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4114 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4115 TYPE_ALIGN (TREE_TYPE (rinner))),
4116 BITS_PER_WORD, false, &nmode))
4117 return 0;
4119 /* Set signed and unsigned types of the precision of this mode for the
4120 shifts below. */
4121 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4123 /* Compute the bit position and size for the new reference and our offset
4124 within it. If the new reference is the same size as the original, we
4125 won't optimize anything, so return zero. */
4126 nbitsize = GET_MODE_BITSIZE (nmode);
4127 nbitpos = lbitpos & ~ (nbitsize - 1);
4128 lbitpos -= nbitpos;
4129 if (nbitsize == lbitsize)
4130 return 0;
4132 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4133 lbitpos = nbitsize - lbitsize - lbitpos;
4135 /* Make the mask to be used against the extracted field. */
4136 mask = build_int_cst_type (unsigned_type, -1);
4137 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4138 mask = const_binop (RSHIFT_EXPR, mask,
4139 size_int (nbitsize - lbitsize - lbitpos));
4141 if (! const_p)
4143 if (nbitpos < 0)
4144 return 0;
4146 /* If not comparing with constant, just rework the comparison
4147 and return. */
4148 tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4149 nbitsize, nbitpos, 1, lreversep);
4150 t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4151 tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4152 nbitsize, nbitpos, 1, rreversep);
4153 t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4154 return fold_build2_loc (loc, code, compare_type, t1, t2);
4157 /* Otherwise, we are handling the constant case. See if the constant is too
4158 big for the field. Warn and return a tree for 0 (false) if so. We do
4159 this not only for its own sake, but to avoid having to test for this
4160 error case below. If we didn't, we might generate wrong code.
4162 For unsigned fields, the constant shifted right by the field length should
4163 be all zero. For signed fields, the high-order bits should agree with
4164 the sign bit. */
4166 if (lunsignedp)
4168 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4170 warning (0, "comparison is always %d due to width of bit-field",
4171 code == NE_EXPR);
4172 return constant_boolean_node (code == NE_EXPR, compare_type);
4175 else
4177 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4178 if (tem != 0 && tem != -1)
4180 warning (0, "comparison is always %d due to width of bit-field",
4181 code == NE_EXPR);
4182 return constant_boolean_node (code == NE_EXPR, compare_type);
4186 if (nbitpos < 0)
4187 return 0;
4189 /* Single-bit compares should always be against zero. */
4190 if (lbitsize == 1 && ! integer_zerop (rhs))
4192 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4193 rhs = build_int_cst (type, 0);
4196 /* Make a new bitfield reference, shift the constant over the
4197 appropriate number of bits and mask it with the computed mask
4198 (in case this was a signed field). If we changed it, make a new one. */
4199 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4200 nbitsize, nbitpos, 1, lreversep);
4202 rhs = const_binop (BIT_AND_EXPR,
4203 const_binop (LSHIFT_EXPR,
4204 fold_convert_loc (loc, unsigned_type, rhs),
4205 size_int (lbitpos)),
4206 mask);
4208 lhs = build2_loc (loc, code, compare_type,
4209 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4210 return lhs;
4213 /* Subroutine for fold_truth_andor_1: decode a field reference.
4215 If EXP is a comparison reference, we return the innermost reference.
4217 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4218 set to the starting bit number.
4220 If the innermost field can be completely contained in a mode-sized
4221 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4223 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4224 otherwise it is not changed.
4226 *PUNSIGNEDP is set to the signedness of the field.
4228 *PREVERSEP is set to the storage order of the field.
4230 *PMASK is set to the mask used. This is either contained in a
4231 BIT_AND_EXPR or derived from the width of the field.
4233 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4235 Return 0 if this is not a component reference or is one that we can't
4236 do anything with. */
4238 static tree
4239 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4240 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4241 int *punsignedp, int *preversep, int *pvolatilep,
4242 tree *pmask, tree *pand_mask)
4244 tree exp = *exp_;
4245 tree outer_type = 0;
4246 tree and_mask = 0;
4247 tree mask, inner, offset;
4248 tree unsigned_type;
4249 unsigned int precision;
4251 /* All the optimizations using this function assume integer fields.
4252 There are problems with FP fields since the type_for_size call
4253 below can fail for, e.g., XFmode. */
4254 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4255 return 0;
4257 /* We are interested in the bare arrangement of bits, so strip everything
4258 that doesn't affect the machine mode. However, record the type of the
4259 outermost expression if it may matter below. */
4260 if (CONVERT_EXPR_P (exp)
4261 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4262 outer_type = TREE_TYPE (exp);
4263 STRIP_NOPS (exp);
4265 if (TREE_CODE (exp) == BIT_AND_EXPR)
4267 and_mask = TREE_OPERAND (exp, 1);
4268 exp = TREE_OPERAND (exp, 0);
4269 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4270 if (TREE_CODE (and_mask) != INTEGER_CST)
4271 return 0;
4274 poly_int64 poly_bitsize, poly_bitpos;
4275 inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
4276 pmode, punsignedp, preversep, pvolatilep);
4277 if ((inner == exp && and_mask == 0)
4278 || !poly_bitsize.is_constant (pbitsize)
4279 || !poly_bitpos.is_constant (pbitpos)
4280 || *pbitsize < 0
4281 || offset != 0
4282 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4283 /* Reject out-of-bound accesses (PR79731). */
4284 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4285 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4286 *pbitpos + *pbitsize) < 0))
4287 return 0;
4289 *exp_ = exp;
4291 /* If the number of bits in the reference is the same as the bitsize of
4292 the outer type, then the outer type gives the signedness. Otherwise
4293 (in case of a small bitfield) the signedness is unchanged. */
4294 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4295 *punsignedp = TYPE_UNSIGNED (outer_type);
4297 /* Compute the mask to access the bitfield. */
4298 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4299 precision = TYPE_PRECISION (unsigned_type);
4301 mask = build_int_cst_type (unsigned_type, -1);
4303 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4304 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4306 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4307 if (and_mask != 0)
4308 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4309 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4311 *pmask = mask;
4312 *pand_mask = and_mask;
4313 return inner;
4316 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4317 bit positions and MASK is SIGNED. */
4319 static int
4320 all_ones_mask_p (const_tree mask, unsigned int size)
4322 tree type = TREE_TYPE (mask);
4323 unsigned int precision = TYPE_PRECISION (type);
4325 /* If this function returns true when the type of the mask is
4326 UNSIGNED, then there will be errors. In particular see
4327 gcc.c-torture/execute/990326-1.c. There does not appear to be
4328 any documentation paper trail as to why this is so. But the pre
4329 wide-int worked with that restriction and it has been preserved
4330 here. */
4331 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4332 return false;
4334 return wi::mask (size, false, precision) == wi::to_wide (mask);
4337 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4338 represents the sign bit of EXP's type. If EXP represents a sign
4339 or zero extension, also test VAL against the unextended type.
4340 The return value is the (sub)expression whose sign bit is VAL,
4341 or NULL_TREE otherwise. */
4343 tree
4344 sign_bit_p (tree exp, const_tree val)
4346 int width;
4347 tree t;
4349 /* Tree EXP must have an integral type. */
4350 t = TREE_TYPE (exp);
4351 if (! INTEGRAL_TYPE_P (t))
4352 return NULL_TREE;
4354 /* Tree VAL must be an integer constant. */
4355 if (TREE_CODE (val) != INTEGER_CST
4356 || TREE_OVERFLOW (val))
4357 return NULL_TREE;
4359 width = TYPE_PRECISION (t);
4360 if (wi::only_sign_bit_p (wi::to_wide (val), width))
4361 return exp;
4363 /* Handle extension from a narrower type. */
4364 if (TREE_CODE (exp) == NOP_EXPR
4365 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4366 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4368 return NULL_TREE;
4371 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4372 to be evaluated unconditionally. */
4374 static int
4375 simple_operand_p (const_tree exp)
4377 /* Strip any conversions that don't change the machine mode. */
4378 STRIP_NOPS (exp);
4380 return (CONSTANT_CLASS_P (exp)
4381 || TREE_CODE (exp) == SSA_NAME
4382 || (DECL_P (exp)
4383 && ! TREE_ADDRESSABLE (exp)
4384 && ! TREE_THIS_VOLATILE (exp)
4385 && ! DECL_NONLOCAL (exp)
4386 /* Don't regard global variables as simple. They may be
4387 allocated in ways unknown to the compiler (shared memory,
4388 #pragma weak, etc). */
4389 && ! TREE_PUBLIC (exp)
4390 && ! DECL_EXTERNAL (exp)
4391 /* Weakrefs are not safe to be read, since they can be NULL.
4392 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4393 have DECL_WEAK flag set. */
4394 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4395 /* Loading a static variable is unduly expensive, but global
4396 registers aren't expensive. */
4397 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4400 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4401 to be evaluated unconditionally.
4402 I addition to simple_operand_p, we assume that comparisons, conversions,
4403 and logic-not operations are simple, if their operands are simple, too. */
4405 static bool
4406 simple_operand_p_2 (tree exp)
4408 enum tree_code code;
4410 if (TREE_SIDE_EFFECTS (exp)
4411 || tree_could_trap_p (exp))
4412 return false;
4414 while (CONVERT_EXPR_P (exp))
4415 exp = TREE_OPERAND (exp, 0);
4417 code = TREE_CODE (exp);
4419 if (TREE_CODE_CLASS (code) == tcc_comparison)
4420 return (simple_operand_p (TREE_OPERAND (exp, 0))
4421 && simple_operand_p (TREE_OPERAND (exp, 1)));
4423 if (code == TRUTH_NOT_EXPR)
4424 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4426 return simple_operand_p (exp);
4430 /* The following functions are subroutines to fold_range_test and allow it to
4431 try to change a logical combination of comparisons into a range test.
4433 For example, both
4434 X == 2 || X == 3 || X == 4 || X == 5
4436 X >= 2 && X <= 5
4437 are converted to
4438 (unsigned) (X - 2) <= 3
4440 We describe each set of comparisons as being either inside or outside
4441 a range, using a variable named like IN_P, and then describe the
4442 range with a lower and upper bound. If one of the bounds is omitted,
4443 it represents either the highest or lowest value of the type.
4445 In the comments below, we represent a range by two numbers in brackets
4446 preceded by a "+" to designate being inside that range, or a "-" to
4447 designate being outside that range, so the condition can be inverted by
4448 flipping the prefix. An omitted bound is represented by a "-". For
4449 example, "- [-, 10]" means being outside the range starting at the lowest
4450 possible value and ending at 10, in other words, being greater than 10.
4451 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4452 always false.
4454 We set up things so that the missing bounds are handled in a consistent
4455 manner so neither a missing bound nor "true" and "false" need to be
4456 handled using a special case. */
4458 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4459 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4460 and UPPER1_P are nonzero if the respective argument is an upper bound
4461 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4462 must be specified for a comparison. ARG1 will be converted to ARG0's
4463 type if both are specified. */
4465 static tree
4466 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4467 tree arg1, int upper1_p)
4469 tree tem;
4470 int result;
4471 int sgn0, sgn1;
4473 /* If neither arg represents infinity, do the normal operation.
4474 Else, if not a comparison, return infinity. Else handle the special
4475 comparison rules. Note that most of the cases below won't occur, but
4476 are handled for consistency. */
4478 if (arg0 != 0 && arg1 != 0)
4480 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4481 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4482 STRIP_NOPS (tem);
4483 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4486 if (TREE_CODE_CLASS (code) != tcc_comparison)
4487 return 0;
4489 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4490 for neither. In real maths, we cannot assume open ended ranges are
4491 the same. But, this is computer arithmetic, where numbers are finite.
4492 We can therefore make the transformation of any unbounded range with
4493 the value Z, Z being greater than any representable number. This permits
4494 us to treat unbounded ranges as equal. */
4495 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4496 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4497 switch (code)
4499 case EQ_EXPR:
4500 result = sgn0 == sgn1;
4501 break;
4502 case NE_EXPR:
4503 result = sgn0 != sgn1;
4504 break;
4505 case LT_EXPR:
4506 result = sgn0 < sgn1;
4507 break;
4508 case LE_EXPR:
4509 result = sgn0 <= sgn1;
4510 break;
4511 case GT_EXPR:
4512 result = sgn0 > sgn1;
4513 break;
4514 case GE_EXPR:
4515 result = sgn0 >= sgn1;
4516 break;
4517 default:
4518 gcc_unreachable ();
4521 return constant_boolean_node (result, type);
4524 /* Helper routine for make_range. Perform one step for it, return
4525 new expression if the loop should continue or NULL_TREE if it should
4526 stop. */
4528 tree
4529 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4530 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4531 bool *strict_overflow_p)
4533 tree arg0_type = TREE_TYPE (arg0);
4534 tree n_low, n_high, low = *p_low, high = *p_high;
4535 int in_p = *p_in_p, n_in_p;
4537 switch (code)
4539 case TRUTH_NOT_EXPR:
4540 /* We can only do something if the range is testing for zero. */
4541 if (low == NULL_TREE || high == NULL_TREE
4542 || ! integer_zerop (low) || ! integer_zerop (high))
4543 return NULL_TREE;
4544 *p_in_p = ! in_p;
4545 return arg0;
4547 case EQ_EXPR: case NE_EXPR:
4548 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4549 /* We can only do something if the range is testing for zero
4550 and if the second operand is an integer constant. Note that
4551 saying something is "in" the range we make is done by
4552 complementing IN_P since it will set in the initial case of
4553 being not equal to zero; "out" is leaving it alone. */
4554 if (low == NULL_TREE || high == NULL_TREE
4555 || ! integer_zerop (low) || ! integer_zerop (high)
4556 || TREE_CODE (arg1) != INTEGER_CST)
4557 return NULL_TREE;
4559 switch (code)
4561 case NE_EXPR: /* - [c, c] */
4562 low = high = arg1;
4563 break;
4564 case EQ_EXPR: /* + [c, c] */
4565 in_p = ! in_p, low = high = arg1;
4566 break;
4567 case GT_EXPR: /* - [-, c] */
4568 low = 0, high = arg1;
4569 break;
4570 case GE_EXPR: /* + [c, -] */
4571 in_p = ! in_p, low = arg1, high = 0;
4572 break;
4573 case LT_EXPR: /* - [c, -] */
4574 low = arg1, high = 0;
4575 break;
4576 case LE_EXPR: /* + [-, c] */
4577 in_p = ! in_p, low = 0, high = arg1;
4578 break;
4579 default:
4580 gcc_unreachable ();
4583 /* If this is an unsigned comparison, we also know that EXP is
4584 greater than or equal to zero. We base the range tests we make
4585 on that fact, so we record it here so we can parse existing
4586 range tests. We test arg0_type since often the return type
4587 of, e.g. EQ_EXPR, is boolean. */
4588 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4590 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4591 in_p, low, high, 1,
4592 build_int_cst (arg0_type, 0),
4593 NULL_TREE))
4594 return NULL_TREE;
4596 in_p = n_in_p, low = n_low, high = n_high;
4598 /* If the high bound is missing, but we have a nonzero low
4599 bound, reverse the range so it goes from zero to the low bound
4600 minus 1. */
4601 if (high == 0 && low && ! integer_zerop (low))
4603 in_p = ! in_p;
4604 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4605 build_int_cst (TREE_TYPE (low), 1), 0);
4606 low = build_int_cst (arg0_type, 0);
4610 *p_low = low;
4611 *p_high = high;
4612 *p_in_p = in_p;
4613 return arg0;
4615 case NEGATE_EXPR:
4616 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4617 low and high are non-NULL, then normalize will DTRT. */
4618 if (!TYPE_UNSIGNED (arg0_type)
4619 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4621 if (low == NULL_TREE)
4622 low = TYPE_MIN_VALUE (arg0_type);
4623 if (high == NULL_TREE)
4624 high = TYPE_MAX_VALUE (arg0_type);
4627 /* (-x) IN [a,b] -> x in [-b, -a] */
4628 n_low = range_binop (MINUS_EXPR, exp_type,
4629 build_int_cst (exp_type, 0),
4630 0, high, 1);
4631 n_high = range_binop (MINUS_EXPR, exp_type,
4632 build_int_cst (exp_type, 0),
4633 0, low, 0);
4634 if (n_high != 0 && TREE_OVERFLOW (n_high))
4635 return NULL_TREE;
4636 goto normalize;
4638 case BIT_NOT_EXPR:
4639 /* ~ X -> -X - 1 */
4640 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4641 build_int_cst (exp_type, 1));
4643 case PLUS_EXPR:
4644 case MINUS_EXPR:
4645 if (TREE_CODE (arg1) != INTEGER_CST)
4646 return NULL_TREE;
4648 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4649 move a constant to the other side. */
4650 if (!TYPE_UNSIGNED (arg0_type)
4651 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4652 return NULL_TREE;
4654 /* If EXP is signed, any overflow in the computation is undefined,
4655 so we don't worry about it so long as our computations on
4656 the bounds don't overflow. For unsigned, overflow is defined
4657 and this is exactly the right thing. */
4658 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4659 arg0_type, low, 0, arg1, 0);
4660 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4661 arg0_type, high, 1, arg1, 0);
4662 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4663 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4664 return NULL_TREE;
4666 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4667 *strict_overflow_p = true;
4669 normalize:
4670 /* Check for an unsigned range which has wrapped around the maximum
4671 value thus making n_high < n_low, and normalize it. */
4672 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4674 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4675 build_int_cst (TREE_TYPE (n_high), 1), 0);
4676 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4677 build_int_cst (TREE_TYPE (n_low), 1), 0);
4679 /* If the range is of the form +/- [ x+1, x ], we won't
4680 be able to normalize it. But then, it represents the
4681 whole range or the empty set, so make it
4682 +/- [ -, - ]. */
4683 if (tree_int_cst_equal (n_low, low)
4684 && tree_int_cst_equal (n_high, high))
4685 low = high = 0;
4686 else
4687 in_p = ! in_p;
4689 else
4690 low = n_low, high = n_high;
4692 *p_low = low;
4693 *p_high = high;
4694 *p_in_p = in_p;
4695 return arg0;
4697 CASE_CONVERT:
4698 case NON_LVALUE_EXPR:
4699 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4700 return NULL_TREE;
4702 if (! INTEGRAL_TYPE_P (arg0_type)
4703 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4704 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4705 return NULL_TREE;
4707 n_low = low, n_high = high;
4709 if (n_low != 0)
4710 n_low = fold_convert_loc (loc, arg0_type, n_low);
4712 if (n_high != 0)
4713 n_high = fold_convert_loc (loc, arg0_type, n_high);
4715 /* If we're converting arg0 from an unsigned type, to exp,
4716 a signed type, we will be doing the comparison as unsigned.
4717 The tests above have already verified that LOW and HIGH
4718 are both positive.
4720 So we have to ensure that we will handle large unsigned
4721 values the same way that the current signed bounds treat
4722 negative values. */
4724 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4726 tree high_positive;
4727 tree equiv_type;
4728 /* For fixed-point modes, we need to pass the saturating flag
4729 as the 2nd parameter. */
4730 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4731 equiv_type
4732 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4733 TYPE_SATURATING (arg0_type));
4734 else
4735 equiv_type
4736 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4738 /* A range without an upper bound is, naturally, unbounded.
4739 Since convert would have cropped a very large value, use
4740 the max value for the destination type. */
4741 high_positive
4742 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4743 : TYPE_MAX_VALUE (arg0_type);
4745 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4746 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4747 fold_convert_loc (loc, arg0_type,
4748 high_positive),
4749 build_int_cst (arg0_type, 1));
4751 /* If the low bound is specified, "and" the range with the
4752 range for which the original unsigned value will be
4753 positive. */
4754 if (low != 0)
4756 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4757 1, fold_convert_loc (loc, arg0_type,
4758 integer_zero_node),
4759 high_positive))
4760 return NULL_TREE;
4762 in_p = (n_in_p == in_p);
4764 else
4766 /* Otherwise, "or" the range with the range of the input
4767 that will be interpreted as negative. */
4768 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4769 1, fold_convert_loc (loc, arg0_type,
4770 integer_zero_node),
4771 high_positive))
4772 return NULL_TREE;
4774 in_p = (in_p != n_in_p);
4778 *p_low = n_low;
4779 *p_high = n_high;
4780 *p_in_p = in_p;
4781 return arg0;
4783 default:
4784 return NULL_TREE;
4788 /* Given EXP, a logical expression, set the range it is testing into
4789 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4790 actually being tested. *PLOW and *PHIGH will be made of the same
4791 type as the returned expression. If EXP is not a comparison, we
4792 will most likely not be returning a useful value and range. Set
4793 *STRICT_OVERFLOW_P to true if the return value is only valid
4794 because signed overflow is undefined; otherwise, do not change
4795 *STRICT_OVERFLOW_P. */
4797 tree
4798 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4799 bool *strict_overflow_p)
4801 enum tree_code code;
4802 tree arg0, arg1 = NULL_TREE;
4803 tree exp_type, nexp;
4804 int in_p;
4805 tree low, high;
4806 location_t loc = EXPR_LOCATION (exp);
4808 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4809 and see if we can refine the range. Some of the cases below may not
4810 happen, but it doesn't seem worth worrying about this. We "continue"
4811 the outer loop when we've changed something; otherwise we "break"
4812 the switch, which will "break" the while. */
4814 in_p = 0;
4815 low = high = build_int_cst (TREE_TYPE (exp), 0);
4817 while (1)
4819 code = TREE_CODE (exp);
4820 exp_type = TREE_TYPE (exp);
4821 arg0 = NULL_TREE;
4823 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4825 if (TREE_OPERAND_LENGTH (exp) > 0)
4826 arg0 = TREE_OPERAND (exp, 0);
4827 if (TREE_CODE_CLASS (code) == tcc_binary
4828 || TREE_CODE_CLASS (code) == tcc_comparison
4829 || (TREE_CODE_CLASS (code) == tcc_expression
4830 && TREE_OPERAND_LENGTH (exp) > 1))
4831 arg1 = TREE_OPERAND (exp, 1);
4833 if (arg0 == NULL_TREE)
4834 break;
4836 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4837 &high, &in_p, strict_overflow_p);
4838 if (nexp == NULL_TREE)
4839 break;
4840 exp = nexp;
4843 /* If EXP is a constant, we can evaluate whether this is true or false. */
4844 if (TREE_CODE (exp) == INTEGER_CST)
4846 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4847 exp, 0, low, 0))
4848 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4849 exp, 1, high, 1)));
4850 low = high = 0;
4851 exp = 0;
4854 *pin_p = in_p, *plow = low, *phigh = high;
4855 return exp;
4858 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4859 a bitwise check i.e. when
4860 LOW == 0xXX...X00...0
4861 HIGH == 0xXX...X11...1
4862 Return corresponding mask in MASK and stem in VALUE. */
4864 static bool
4865 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4866 tree *value)
4868 if (TREE_CODE (low) != INTEGER_CST
4869 || TREE_CODE (high) != INTEGER_CST)
4870 return false;
4872 unsigned prec = TYPE_PRECISION (type);
4873 wide_int lo = wi::to_wide (low, prec);
4874 wide_int hi = wi::to_wide (high, prec);
4876 wide_int end_mask = lo ^ hi;
4877 if ((end_mask & (end_mask + 1)) != 0
4878 || (lo & end_mask) != 0)
4879 return false;
4881 wide_int stem_mask = ~end_mask;
4882 wide_int stem = lo & stem_mask;
4883 if (stem != (hi & stem_mask))
4884 return false;
4886 *mask = wide_int_to_tree (type, stem_mask);
4887 *value = wide_int_to_tree (type, stem);
4889 return true;
4892 /* Helper routine for build_range_check and match.pd. Return the type to
4893 perform the check or NULL if it shouldn't be optimized. */
4895 tree
4896 range_check_type (tree etype)
4898 /* First make sure that arithmetics in this type is valid, then make sure
4899 that it wraps around. */
4900 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4901 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4902 TYPE_UNSIGNED (etype));
4904 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4906 tree utype, minv, maxv;
4908 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4909 for the type in question, as we rely on this here. */
4910 utype = unsigned_type_for (etype);
4911 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4912 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4913 build_int_cst (TREE_TYPE (maxv), 1), 1);
4914 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4916 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4917 minv, 1, maxv, 1)))
4918 etype = utype;
4919 else
4920 return NULL_TREE;
4922 return etype;
4925 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4926 type, TYPE, return an expression to test if EXP is in (or out of, depending
4927 on IN_P) the range. Return 0 if the test couldn't be created. */
4929 tree
4930 build_range_check (location_t loc, tree type, tree exp, int in_p,
4931 tree low, tree high)
4933 tree etype = TREE_TYPE (exp), mask, value;
4935 /* Disable this optimization for function pointer expressions
4936 on targets that require function pointer canonicalization. */
4937 if (targetm.have_canonicalize_funcptr_for_compare ()
4938 && TREE_CODE (etype) == POINTER_TYPE
4939 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4940 return NULL_TREE;
4942 if (! in_p)
4944 value = build_range_check (loc, type, exp, 1, low, high);
4945 if (value != 0)
4946 return invert_truthvalue_loc (loc, value);
4948 return 0;
4951 if (low == 0 && high == 0)
4952 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4954 if (low == 0)
4955 return fold_build2_loc (loc, LE_EXPR, type, exp,
4956 fold_convert_loc (loc, etype, high));
4958 if (high == 0)
4959 return fold_build2_loc (loc, GE_EXPR, type, exp,
4960 fold_convert_loc (loc, etype, low));
4962 if (operand_equal_p (low, high, 0))
4963 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4964 fold_convert_loc (loc, etype, low));
4966 if (TREE_CODE (exp) == BIT_AND_EXPR
4967 && maskable_range_p (low, high, etype, &mask, &value))
4968 return fold_build2_loc (loc, EQ_EXPR, type,
4969 fold_build2_loc (loc, BIT_AND_EXPR, etype,
4970 exp, mask),
4971 value);
4973 if (integer_zerop (low))
4975 if (! TYPE_UNSIGNED (etype))
4977 etype = unsigned_type_for (etype);
4978 high = fold_convert_loc (loc, etype, high);
4979 exp = fold_convert_loc (loc, etype, exp);
4981 return build_range_check (loc, type, exp, 1, 0, high);
4984 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4985 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4987 int prec = TYPE_PRECISION (etype);
4989 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
4991 if (TYPE_UNSIGNED (etype))
4993 tree signed_etype = signed_type_for (etype);
4994 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4995 etype
4996 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4997 else
4998 etype = signed_etype;
4999 exp = fold_convert_loc (loc, etype, exp);
5001 return fold_build2_loc (loc, GT_EXPR, type, exp,
5002 build_int_cst (etype, 0));
5006 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5007 This requires wrap-around arithmetics for the type of the expression. */
5008 etype = range_check_type (etype);
5009 if (etype == NULL_TREE)
5010 return NULL_TREE;
5012 if (POINTER_TYPE_P (etype))
5013 etype = unsigned_type_for (etype);
5015 high = fold_convert_loc (loc, etype, high);
5016 low = fold_convert_loc (loc, etype, low);
5017 exp = fold_convert_loc (loc, etype, exp);
5019 value = const_binop (MINUS_EXPR, high, low);
5021 if (value != 0 && !TREE_OVERFLOW (value))
5022 return build_range_check (loc, type,
5023 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5024 1, build_int_cst (etype, 0), value);
5026 return 0;
5029 /* Return the predecessor of VAL in its type, handling the infinite case. */
5031 static tree
5032 range_predecessor (tree val)
5034 tree type = TREE_TYPE (val);
5036 if (INTEGRAL_TYPE_P (type)
5037 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5038 return 0;
5039 else
5040 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5041 build_int_cst (TREE_TYPE (val), 1), 0);
5044 /* Return the successor of VAL in its type, handling the infinite case. */
5046 static tree
5047 range_successor (tree val)
5049 tree type = TREE_TYPE (val);
5051 if (INTEGRAL_TYPE_P (type)
5052 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5053 return 0;
5054 else
5055 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5056 build_int_cst (TREE_TYPE (val), 1), 0);
5059 /* Given two ranges, see if we can merge them into one. Return 1 if we
5060 can, 0 if we can't. Set the output range into the specified parameters. */
5062 bool
5063 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5064 tree high0, int in1_p, tree low1, tree high1)
5066 int no_overlap;
5067 int subset;
5068 int temp;
5069 tree tem;
5070 int in_p;
5071 tree low, high;
5072 int lowequal = ((low0 == 0 && low1 == 0)
5073 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5074 low0, 0, low1, 0)));
5075 int highequal = ((high0 == 0 && high1 == 0)
5076 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5077 high0, 1, high1, 1)));
5079 /* Make range 0 be the range that starts first, or ends last if they
5080 start at the same value. Swap them if it isn't. */
5081 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5082 low0, 0, low1, 0))
5083 || (lowequal
5084 && integer_onep (range_binop (GT_EXPR, integer_type_node,
5085 high1, 1, high0, 1))))
5087 temp = in0_p, in0_p = in1_p, in1_p = temp;
5088 tem = low0, low0 = low1, low1 = tem;
5089 tem = high0, high0 = high1, high1 = tem;
5092 /* If the second range is != high1 where high1 is the type maximum of
5093 the type, try first merging with < high1 range. */
5094 if (low1
5095 && high1
5096 && TREE_CODE (low1) == INTEGER_CST
5097 && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5098 || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5099 && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5100 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5101 && operand_equal_p (low1, high1, 0))
5103 if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5104 && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5105 !in1_p, NULL_TREE, range_predecessor (low1)))
5106 return true;
5107 /* Similarly for the second range != low1 where low1 is the type minimum
5108 of the type, try first merging with > low1 range. */
5109 if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5110 && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5111 !in1_p, range_successor (low1), NULL_TREE))
5112 return true;
5115 /* Now flag two cases, whether the ranges are disjoint or whether the
5116 second range is totally subsumed in the first. Note that the tests
5117 below are simplified by the ones above. */
5118 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5119 high0, 1, low1, 0));
5120 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5121 high1, 1, high0, 1));
5123 /* We now have four cases, depending on whether we are including or
5124 excluding the two ranges. */
5125 if (in0_p && in1_p)
5127 /* If they don't overlap, the result is false. If the second range
5128 is a subset it is the result. Otherwise, the range is from the start
5129 of the second to the end of the first. */
5130 if (no_overlap)
5131 in_p = 0, low = high = 0;
5132 else if (subset)
5133 in_p = 1, low = low1, high = high1;
5134 else
5135 in_p = 1, low = low1, high = high0;
5138 else if (in0_p && ! in1_p)
5140 /* If they don't overlap, the result is the first range. If they are
5141 equal, the result is false. If the second range is a subset of the
5142 first, and the ranges begin at the same place, we go from just after
5143 the end of the second range to the end of the first. If the second
5144 range is not a subset of the first, or if it is a subset and both
5145 ranges end at the same place, the range starts at the start of the
5146 first range and ends just before the second range.
5147 Otherwise, we can't describe this as a single range. */
5148 if (no_overlap)
5149 in_p = 1, low = low0, high = high0;
5150 else if (lowequal && highequal)
5151 in_p = 0, low = high = 0;
5152 else if (subset && lowequal)
5154 low = range_successor (high1);
5155 high = high0;
5156 in_p = 1;
5157 if (low == 0)
5159 /* We are in the weird situation where high0 > high1 but
5160 high1 has no successor. Punt. */
5161 return 0;
5164 else if (! subset || highequal)
5166 low = low0;
5167 high = range_predecessor (low1);
5168 in_p = 1;
5169 if (high == 0)
5171 /* low0 < low1 but low1 has no predecessor. Punt. */
5172 return 0;
5175 else
5176 return 0;
5179 else if (! in0_p && in1_p)
5181 /* If they don't overlap, the result is the second range. If the second
5182 is a subset of the first, the result is false. Otherwise,
5183 the range starts just after the first range and ends at the
5184 end of the second. */
5185 if (no_overlap)
5186 in_p = 1, low = low1, high = high1;
5187 else if (subset || highequal)
5188 in_p = 0, low = high = 0;
5189 else
5191 low = range_successor (high0);
5192 high = high1;
5193 in_p = 1;
5194 if (low == 0)
5196 /* high1 > high0 but high0 has no successor. Punt. */
5197 return 0;
5202 else
5204 /* The case where we are excluding both ranges. Here the complex case
5205 is if they don't overlap. In that case, the only time we have a
5206 range is if they are adjacent. If the second is a subset of the
5207 first, the result is the first. Otherwise, the range to exclude
5208 starts at the beginning of the first range and ends at the end of the
5209 second. */
5210 if (no_overlap)
5212 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5213 range_successor (high0),
5214 1, low1, 0)))
5215 in_p = 0, low = low0, high = high1;
5216 else
5218 /* Canonicalize - [min, x] into - [-, x]. */
5219 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5220 switch (TREE_CODE (TREE_TYPE (low0)))
5222 case ENUMERAL_TYPE:
5223 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5224 GET_MODE_BITSIZE
5225 (TYPE_MODE (TREE_TYPE (low0)))))
5226 break;
5227 /* FALLTHROUGH */
5228 case INTEGER_TYPE:
5229 if (tree_int_cst_equal (low0,
5230 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5231 low0 = 0;
5232 break;
5233 case POINTER_TYPE:
5234 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5235 && integer_zerop (low0))
5236 low0 = 0;
5237 break;
5238 default:
5239 break;
5242 /* Canonicalize - [x, max] into - [x, -]. */
5243 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5244 switch (TREE_CODE (TREE_TYPE (high1)))
5246 case ENUMERAL_TYPE:
5247 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5248 GET_MODE_BITSIZE
5249 (TYPE_MODE (TREE_TYPE (high1)))))
5250 break;
5251 /* FALLTHROUGH */
5252 case INTEGER_TYPE:
5253 if (tree_int_cst_equal (high1,
5254 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5255 high1 = 0;
5256 break;
5257 case POINTER_TYPE:
5258 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5259 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5260 high1, 1,
5261 build_int_cst (TREE_TYPE (high1), 1),
5262 1)))
5263 high1 = 0;
5264 break;
5265 default:
5266 break;
5269 /* The ranges might be also adjacent between the maximum and
5270 minimum values of the given type. For
5271 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5272 return + [x + 1, y - 1]. */
5273 if (low0 == 0 && high1 == 0)
5275 low = range_successor (high0);
5276 high = range_predecessor (low1);
5277 if (low == 0 || high == 0)
5278 return 0;
5280 in_p = 1;
5282 else
5283 return 0;
5286 else if (subset)
5287 in_p = 0, low = low0, high = high0;
5288 else
5289 in_p = 0, low = low0, high = high1;
5292 *pin_p = in_p, *plow = low, *phigh = high;
5293 return 1;
5297 /* Subroutine of fold, looking inside expressions of the form
5298 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5299 of the COND_EXPR. This function is being used also to optimize
5300 A op B ? C : A, by reversing the comparison first.
5302 Return a folded expression whose code is not a COND_EXPR
5303 anymore, or NULL_TREE if no folding opportunity is found. */
5305 static tree
5306 fold_cond_expr_with_comparison (location_t loc, tree type,
5307 tree arg0, tree arg1, tree arg2)
5309 enum tree_code comp_code = TREE_CODE (arg0);
5310 tree arg00 = TREE_OPERAND (arg0, 0);
5311 tree arg01 = TREE_OPERAND (arg0, 1);
5312 tree arg1_type = TREE_TYPE (arg1);
5313 tree tem;
5315 STRIP_NOPS (arg1);
5316 STRIP_NOPS (arg2);
5318 /* If we have A op 0 ? A : -A, consider applying the following
5319 transformations:
5321 A == 0? A : -A same as -A
5322 A != 0? A : -A same as A
5323 A >= 0? A : -A same as abs (A)
5324 A > 0? A : -A same as abs (A)
5325 A <= 0? A : -A same as -abs (A)
5326 A < 0? A : -A same as -abs (A)
5328 None of these transformations work for modes with signed
5329 zeros. If A is +/-0, the first two transformations will
5330 change the sign of the result (from +0 to -0, or vice
5331 versa). The last four will fix the sign of the result,
5332 even though the original expressions could be positive or
5333 negative, depending on the sign of A.
5335 Note that all these transformations are correct if A is
5336 NaN, since the two alternatives (A and -A) are also NaNs. */
5337 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5338 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5339 ? real_zerop (arg01)
5340 : integer_zerop (arg01))
5341 && ((TREE_CODE (arg2) == NEGATE_EXPR
5342 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5343 /* In the case that A is of the form X-Y, '-A' (arg2) may
5344 have already been folded to Y-X, check for that. */
5345 || (TREE_CODE (arg1) == MINUS_EXPR
5346 && TREE_CODE (arg2) == MINUS_EXPR
5347 && operand_equal_p (TREE_OPERAND (arg1, 0),
5348 TREE_OPERAND (arg2, 1), 0)
5349 && operand_equal_p (TREE_OPERAND (arg1, 1),
5350 TREE_OPERAND (arg2, 0), 0))))
5351 switch (comp_code)
5353 case EQ_EXPR:
5354 case UNEQ_EXPR:
5355 tem = fold_convert_loc (loc, arg1_type, arg1);
5356 return fold_convert_loc (loc, type, negate_expr (tem));
5357 case NE_EXPR:
5358 case LTGT_EXPR:
5359 return fold_convert_loc (loc, type, arg1);
5360 case UNGE_EXPR:
5361 case UNGT_EXPR:
5362 if (flag_trapping_math)
5363 break;
5364 /* Fall through. */
5365 case GE_EXPR:
5366 case GT_EXPR:
5367 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5368 break;
5369 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5370 return fold_convert_loc (loc, type, tem);
5371 case UNLE_EXPR:
5372 case UNLT_EXPR:
5373 if (flag_trapping_math)
5374 break;
5375 /* FALLTHRU */
5376 case LE_EXPR:
5377 case LT_EXPR:
5378 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5379 break;
5380 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5381 return negate_expr (fold_convert_loc (loc, type, tem));
5382 default:
5383 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5384 break;
5387 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5388 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5389 both transformations are correct when A is NaN: A != 0
5390 is then true, and A == 0 is false. */
5392 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5393 && integer_zerop (arg01) && integer_zerop (arg2))
5395 if (comp_code == NE_EXPR)
5396 return fold_convert_loc (loc, type, arg1);
5397 else if (comp_code == EQ_EXPR)
5398 return build_zero_cst (type);
5401 /* Try some transformations of A op B ? A : B.
5403 A == B? A : B same as B
5404 A != B? A : B same as A
5405 A >= B? A : B same as max (A, B)
5406 A > B? A : B same as max (B, A)
5407 A <= B? A : B same as min (A, B)
5408 A < B? A : B same as min (B, A)
5410 As above, these transformations don't work in the presence
5411 of signed zeros. For example, if A and B are zeros of
5412 opposite sign, the first two transformations will change
5413 the sign of the result. In the last four, the original
5414 expressions give different results for (A=+0, B=-0) and
5415 (A=-0, B=+0), but the transformed expressions do not.
5417 The first two transformations are correct if either A or B
5418 is a NaN. In the first transformation, the condition will
5419 be false, and B will indeed be chosen. In the case of the
5420 second transformation, the condition A != B will be true,
5421 and A will be chosen.
5423 The conversions to max() and min() are not correct if B is
5424 a number and A is not. The conditions in the original
5425 expressions will be false, so all four give B. The min()
5426 and max() versions would give a NaN instead. */
5427 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5428 && operand_equal_for_comparison_p (arg01, arg2)
5429 /* Avoid these transformations if the COND_EXPR may be used
5430 as an lvalue in the C++ front-end. PR c++/19199. */
5431 && (in_gimple_form
5432 || VECTOR_TYPE_P (type)
5433 || (! lang_GNU_CXX ()
5434 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5435 || ! maybe_lvalue_p (arg1)
5436 || ! maybe_lvalue_p (arg2)))
5438 tree comp_op0 = arg00;
5439 tree comp_op1 = arg01;
5440 tree comp_type = TREE_TYPE (comp_op0);
5442 switch (comp_code)
5444 case EQ_EXPR:
5445 return fold_convert_loc (loc, type, arg2);
5446 case NE_EXPR:
5447 return fold_convert_loc (loc, type, arg1);
5448 case LE_EXPR:
5449 case LT_EXPR:
5450 case UNLE_EXPR:
5451 case UNLT_EXPR:
5452 /* In C++ a ?: expression can be an lvalue, so put the
5453 operand which will be used if they are equal first
5454 so that we can convert this back to the
5455 corresponding COND_EXPR. */
5456 if (!HONOR_NANS (arg1))
5458 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5459 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5460 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5461 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5462 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5463 comp_op1, comp_op0);
5464 return fold_convert_loc (loc, type, tem);
5466 break;
5467 case GE_EXPR:
5468 case GT_EXPR:
5469 case UNGE_EXPR:
5470 case UNGT_EXPR:
5471 if (!HONOR_NANS (arg1))
5473 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5474 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5475 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5476 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5477 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5478 comp_op1, comp_op0);
5479 return fold_convert_loc (loc, type, tem);
5481 break;
5482 case UNEQ_EXPR:
5483 if (!HONOR_NANS (arg1))
5484 return fold_convert_loc (loc, type, arg2);
5485 break;
5486 case LTGT_EXPR:
5487 if (!HONOR_NANS (arg1))
5488 return fold_convert_loc (loc, type, arg1);
5489 break;
5490 default:
5491 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5492 break;
5496 return NULL_TREE;
5501 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5502 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5503 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5504 false) >= 2)
5505 #endif
5507 /* EXP is some logical combination of boolean tests. See if we can
5508 merge it into some range test. Return the new tree if so. */
5510 static tree
5511 fold_range_test (location_t loc, enum tree_code code, tree type,
5512 tree op0, tree op1)
5514 int or_op = (code == TRUTH_ORIF_EXPR
5515 || code == TRUTH_OR_EXPR);
5516 int in0_p, in1_p, in_p;
5517 tree low0, low1, low, high0, high1, high;
5518 bool strict_overflow_p = false;
5519 tree tem, lhs, rhs;
5520 const char * const warnmsg = G_("assuming signed overflow does not occur "
5521 "when simplifying range test");
5523 if (!INTEGRAL_TYPE_P (type))
5524 return 0;
5526 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5527 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5529 /* If this is an OR operation, invert both sides; we will invert
5530 again at the end. */
5531 if (or_op)
5532 in0_p = ! in0_p, in1_p = ! in1_p;
5534 /* If both expressions are the same, if we can merge the ranges, and we
5535 can build the range test, return it or it inverted. If one of the
5536 ranges is always true or always false, consider it to be the same
5537 expression as the other. */
5538 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5539 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5540 in1_p, low1, high1)
5541 && (tem = (build_range_check (loc, type,
5542 lhs != 0 ? lhs
5543 : rhs != 0 ? rhs : integer_zero_node,
5544 in_p, low, high))) != 0)
5546 if (strict_overflow_p)
5547 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5548 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5551 /* On machines where the branch cost is expensive, if this is a
5552 short-circuited branch and the underlying object on both sides
5553 is the same, make a non-short-circuit operation. */
5554 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5555 && !flag_sanitize_coverage
5556 && lhs != 0 && rhs != 0
5557 && (code == TRUTH_ANDIF_EXPR
5558 || code == TRUTH_ORIF_EXPR)
5559 && operand_equal_p (lhs, rhs, 0))
5561 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5562 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5563 which cases we can't do this. */
5564 if (simple_operand_p (lhs))
5565 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5566 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5567 type, op0, op1);
5569 else if (!lang_hooks.decls.global_bindings_p ()
5570 && !CONTAINS_PLACEHOLDER_P (lhs))
5572 tree common = save_expr (lhs);
5574 if ((lhs = build_range_check (loc, type, common,
5575 or_op ? ! in0_p : in0_p,
5576 low0, high0)) != 0
5577 && (rhs = build_range_check (loc, type, common,
5578 or_op ? ! in1_p : in1_p,
5579 low1, high1)) != 0)
5581 if (strict_overflow_p)
5582 fold_overflow_warning (warnmsg,
5583 WARN_STRICT_OVERFLOW_COMPARISON);
5584 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5585 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5586 type, lhs, rhs);
5591 return 0;
5594 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5595 bit value. Arrange things so the extra bits will be set to zero if and
5596 only if C is signed-extended to its full width. If MASK is nonzero,
5597 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5599 static tree
5600 unextend (tree c, int p, int unsignedp, tree mask)
5602 tree type = TREE_TYPE (c);
5603 int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
5604 tree temp;
5606 if (p == modesize || unsignedp)
5607 return c;
5609 /* We work by getting just the sign bit into the low-order bit, then
5610 into the high-order bit, then sign-extend. We then XOR that value
5611 with C. */
5612 temp = build_int_cst (TREE_TYPE (c),
5613 wi::extract_uhwi (wi::to_wide (c), p - 1, 1));
5615 /* We must use a signed type in order to get an arithmetic right shift.
5616 However, we must also avoid introducing accidental overflows, so that
5617 a subsequent call to integer_zerop will work. Hence we must
5618 do the type conversion here. At this point, the constant is either
5619 zero or one, and the conversion to a signed type can never overflow.
5620 We could get an overflow if this conversion is done anywhere else. */
5621 if (TYPE_UNSIGNED (type))
5622 temp = fold_convert (signed_type_for (type), temp);
5624 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5625 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5626 if (mask != 0)
5627 temp = const_binop (BIT_AND_EXPR, temp,
5628 fold_convert (TREE_TYPE (c), mask));
5629 /* If necessary, convert the type back to match the type of C. */
5630 if (TYPE_UNSIGNED (type))
5631 temp = fold_convert (type, temp);
5633 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5636 /* For an expression that has the form
5637 (A && B) || ~B
5639 (A || B) && ~B,
5640 we can drop one of the inner expressions and simplify to
5641 A || ~B
5643 A && ~B
5644 LOC is the location of the resulting expression. OP is the inner
5645 logical operation; the left-hand side in the examples above, while CMPOP
5646 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5647 removing a condition that guards another, as in
5648 (A != NULL && A->...) || A == NULL
5649 which we must not transform. If RHS_ONLY is true, only eliminate the
5650 right-most operand of the inner logical operation. */
5652 static tree
5653 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5654 bool rhs_only)
5656 tree type = TREE_TYPE (cmpop);
5657 enum tree_code code = TREE_CODE (cmpop);
5658 enum tree_code truthop_code = TREE_CODE (op);
5659 tree lhs = TREE_OPERAND (op, 0);
5660 tree rhs = TREE_OPERAND (op, 1);
5661 tree orig_lhs = lhs, orig_rhs = rhs;
5662 enum tree_code rhs_code = TREE_CODE (rhs);
5663 enum tree_code lhs_code = TREE_CODE (lhs);
5664 enum tree_code inv_code;
5666 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5667 return NULL_TREE;
5669 if (TREE_CODE_CLASS (code) != tcc_comparison)
5670 return NULL_TREE;
5672 if (rhs_code == truthop_code)
5674 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5675 if (newrhs != NULL_TREE)
5677 rhs = newrhs;
5678 rhs_code = TREE_CODE (rhs);
5681 if (lhs_code == truthop_code && !rhs_only)
5683 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5684 if (newlhs != NULL_TREE)
5686 lhs = newlhs;
5687 lhs_code = TREE_CODE (lhs);
5691 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5692 if (inv_code == rhs_code
5693 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5694 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5695 return lhs;
5696 if (!rhs_only && inv_code == lhs_code
5697 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5698 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5699 return rhs;
5700 if (rhs != orig_rhs || lhs != orig_lhs)
5701 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5702 lhs, rhs);
5703 return NULL_TREE;
5706 /* Find ways of folding logical expressions of LHS and RHS:
5707 Try to merge two comparisons to the same innermost item.
5708 Look for range tests like "ch >= '0' && ch <= '9'".
5709 Look for combinations of simple terms on machines with expensive branches
5710 and evaluate the RHS unconditionally.
5712 For example, if we have p->a == 2 && p->b == 4 and we can make an
5713 object large enough to span both A and B, we can do this with a comparison
5714 against the object ANDed with the a mask.
5716 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5717 operations to do this with one comparison.
5719 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5720 function and the one above.
5722 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5723 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5725 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5726 two operands.
5728 We return the simplified tree or 0 if no optimization is possible. */
5730 static tree
5731 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5732 tree lhs, tree rhs)
5734 /* If this is the "or" of two comparisons, we can do something if
5735 the comparisons are NE_EXPR. If this is the "and", we can do something
5736 if the comparisons are EQ_EXPR. I.e.,
5737 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5739 WANTED_CODE is this operation code. For single bit fields, we can
5740 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5741 comparison for one-bit fields. */
5743 enum tree_code wanted_code;
5744 enum tree_code lcode, rcode;
5745 tree ll_arg, lr_arg, rl_arg, rr_arg;
5746 tree ll_inner, lr_inner, rl_inner, rr_inner;
5747 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5748 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5749 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5750 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5751 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5752 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5753 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5754 scalar_int_mode lnmode, rnmode;
5755 tree ll_mask, lr_mask, rl_mask, rr_mask;
5756 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5757 tree l_const, r_const;
5758 tree lntype, rntype, result;
5759 HOST_WIDE_INT first_bit, end_bit;
5760 int volatilep;
5762 /* Start by getting the comparison codes. Fail if anything is volatile.
5763 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5764 it were surrounded with a NE_EXPR. */
5766 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5767 return 0;
5769 lcode = TREE_CODE (lhs);
5770 rcode = TREE_CODE (rhs);
5772 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5774 lhs = build2 (NE_EXPR, truth_type, lhs,
5775 build_int_cst (TREE_TYPE (lhs), 0));
5776 lcode = NE_EXPR;
5779 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5781 rhs = build2 (NE_EXPR, truth_type, rhs,
5782 build_int_cst (TREE_TYPE (rhs), 0));
5783 rcode = NE_EXPR;
5786 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5787 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5788 return 0;
5790 ll_arg = TREE_OPERAND (lhs, 0);
5791 lr_arg = TREE_OPERAND (lhs, 1);
5792 rl_arg = TREE_OPERAND (rhs, 0);
5793 rr_arg = TREE_OPERAND (rhs, 1);
5795 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5796 if (simple_operand_p (ll_arg)
5797 && simple_operand_p (lr_arg))
5799 if (operand_equal_p (ll_arg, rl_arg, 0)
5800 && operand_equal_p (lr_arg, rr_arg, 0))
5802 result = combine_comparisons (loc, code, lcode, rcode,
5803 truth_type, ll_arg, lr_arg);
5804 if (result)
5805 return result;
5807 else if (operand_equal_p (ll_arg, rr_arg, 0)
5808 && operand_equal_p (lr_arg, rl_arg, 0))
5810 result = combine_comparisons (loc, code, lcode,
5811 swap_tree_comparison (rcode),
5812 truth_type, ll_arg, lr_arg);
5813 if (result)
5814 return result;
5818 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5819 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5821 /* If the RHS can be evaluated unconditionally and its operands are
5822 simple, it wins to evaluate the RHS unconditionally on machines
5823 with expensive branches. In this case, this isn't a comparison
5824 that can be merged. */
5826 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5827 false) >= 2
5828 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5829 && simple_operand_p (rl_arg)
5830 && simple_operand_p (rr_arg))
5832 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5833 if (code == TRUTH_OR_EXPR
5834 && lcode == NE_EXPR && integer_zerop (lr_arg)
5835 && rcode == NE_EXPR && integer_zerop (rr_arg)
5836 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5837 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5838 return build2_loc (loc, NE_EXPR, truth_type,
5839 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5840 ll_arg, rl_arg),
5841 build_int_cst (TREE_TYPE (ll_arg), 0));
5843 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5844 if (code == TRUTH_AND_EXPR
5845 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5846 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5847 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5848 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5849 return build2_loc (loc, EQ_EXPR, truth_type,
5850 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5851 ll_arg, rl_arg),
5852 build_int_cst (TREE_TYPE (ll_arg), 0));
5855 /* See if the comparisons can be merged. Then get all the parameters for
5856 each side. */
5858 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5859 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5860 return 0;
5862 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5863 volatilep = 0;
5864 ll_inner = decode_field_reference (loc, &ll_arg,
5865 &ll_bitsize, &ll_bitpos, &ll_mode,
5866 &ll_unsignedp, &ll_reversep, &volatilep,
5867 &ll_mask, &ll_and_mask);
5868 lr_inner = decode_field_reference (loc, &lr_arg,
5869 &lr_bitsize, &lr_bitpos, &lr_mode,
5870 &lr_unsignedp, &lr_reversep, &volatilep,
5871 &lr_mask, &lr_and_mask);
5872 rl_inner = decode_field_reference (loc, &rl_arg,
5873 &rl_bitsize, &rl_bitpos, &rl_mode,
5874 &rl_unsignedp, &rl_reversep, &volatilep,
5875 &rl_mask, &rl_and_mask);
5876 rr_inner = decode_field_reference (loc, &rr_arg,
5877 &rr_bitsize, &rr_bitpos, &rr_mode,
5878 &rr_unsignedp, &rr_reversep, &volatilep,
5879 &rr_mask, &rr_and_mask);
5881 /* It must be true that the inner operation on the lhs of each
5882 comparison must be the same if we are to be able to do anything.
5883 Then see if we have constants. If not, the same must be true for
5884 the rhs's. */
5885 if (volatilep
5886 || ll_reversep != rl_reversep
5887 || ll_inner == 0 || rl_inner == 0
5888 || ! operand_equal_p (ll_inner, rl_inner, 0))
5889 return 0;
5891 if (TREE_CODE (lr_arg) == INTEGER_CST
5892 && TREE_CODE (rr_arg) == INTEGER_CST)
5894 l_const = lr_arg, r_const = rr_arg;
5895 lr_reversep = ll_reversep;
5897 else if (lr_reversep != rr_reversep
5898 || lr_inner == 0 || rr_inner == 0
5899 || ! operand_equal_p (lr_inner, rr_inner, 0))
5900 return 0;
5901 else
5902 l_const = r_const = 0;
5904 /* If either comparison code is not correct for our logical operation,
5905 fail. However, we can convert a one-bit comparison against zero into
5906 the opposite comparison against that bit being set in the field. */
5908 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5909 if (lcode != wanted_code)
5911 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5913 /* Make the left operand unsigned, since we are only interested
5914 in the value of one bit. Otherwise we are doing the wrong
5915 thing below. */
5916 ll_unsignedp = 1;
5917 l_const = ll_mask;
5919 else
5920 return 0;
5923 /* This is analogous to the code for l_const above. */
5924 if (rcode != wanted_code)
5926 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5928 rl_unsignedp = 1;
5929 r_const = rl_mask;
5931 else
5932 return 0;
5935 /* See if we can find a mode that contains both fields being compared on
5936 the left. If we can't, fail. Otherwise, update all constants and masks
5937 to be relative to a field of that size. */
5938 first_bit = MIN (ll_bitpos, rl_bitpos);
5939 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5940 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5941 TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
5942 volatilep, &lnmode))
5943 return 0;
5945 lnbitsize = GET_MODE_BITSIZE (lnmode);
5946 lnbitpos = first_bit & ~ (lnbitsize - 1);
5947 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5948 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5950 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5952 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5953 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5956 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5957 size_int (xll_bitpos));
5958 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5959 size_int (xrl_bitpos));
5961 if (l_const)
5963 l_const = fold_convert_loc (loc, lntype, l_const);
5964 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5965 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5966 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5967 fold_build1_loc (loc, BIT_NOT_EXPR,
5968 lntype, ll_mask))))
5970 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5972 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5975 if (r_const)
5977 r_const = fold_convert_loc (loc, lntype, r_const);
5978 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5979 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5980 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5981 fold_build1_loc (loc, BIT_NOT_EXPR,
5982 lntype, rl_mask))))
5984 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5986 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5990 /* If the right sides are not constant, do the same for it. Also,
5991 disallow this optimization if a size or signedness mismatch occurs
5992 between the left and right sides. */
5993 if (l_const == 0)
5995 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5996 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5997 /* Make sure the two fields on the right
5998 correspond to the left without being swapped. */
5999 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
6000 return 0;
6002 first_bit = MIN (lr_bitpos, rr_bitpos);
6003 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
6004 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
6005 TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
6006 volatilep, &rnmode))
6007 return 0;
6009 rnbitsize = GET_MODE_BITSIZE (rnmode);
6010 rnbitpos = first_bit & ~ (rnbitsize - 1);
6011 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
6012 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
6014 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
6016 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
6017 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
6020 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6021 rntype, lr_mask),
6022 size_int (xlr_bitpos));
6023 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6024 rntype, rr_mask),
6025 size_int (xrr_bitpos));
6027 /* Make a mask that corresponds to both fields being compared.
6028 Do this for both items being compared. If the operands are the
6029 same size and the bits being compared are in the same position
6030 then we can do this by masking both and comparing the masked
6031 results. */
6032 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6033 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
6034 if (lnbitsize == rnbitsize
6035 && xll_bitpos == xlr_bitpos
6036 && lnbitpos >= 0
6037 && rnbitpos >= 0)
6039 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
6040 lntype, lnbitsize, lnbitpos,
6041 ll_unsignedp || rl_unsignedp, ll_reversep);
6042 if (! all_ones_mask_p (ll_mask, lnbitsize))
6043 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6045 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
6046 rntype, rnbitsize, rnbitpos,
6047 lr_unsignedp || rr_unsignedp, lr_reversep);
6048 if (! all_ones_mask_p (lr_mask, rnbitsize))
6049 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6051 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6054 /* There is still another way we can do something: If both pairs of
6055 fields being compared are adjacent, we may be able to make a wider
6056 field containing them both.
6058 Note that we still must mask the lhs/rhs expressions. Furthermore,
6059 the mask must be shifted to account for the shift done by
6060 make_bit_field_ref. */
6061 if (((ll_bitsize + ll_bitpos == rl_bitpos
6062 && lr_bitsize + lr_bitpos == rr_bitpos)
6063 || (ll_bitpos == rl_bitpos + rl_bitsize
6064 && lr_bitpos == rr_bitpos + rr_bitsize))
6065 && ll_bitpos >= 0
6066 && rl_bitpos >= 0
6067 && lr_bitpos >= 0
6068 && rr_bitpos >= 0)
6070 tree type;
6072 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
6073 ll_bitsize + rl_bitsize,
6074 MIN (ll_bitpos, rl_bitpos),
6075 ll_unsignedp, ll_reversep);
6076 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
6077 lr_bitsize + rr_bitsize,
6078 MIN (lr_bitpos, rr_bitpos),
6079 lr_unsignedp, lr_reversep);
6081 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6082 size_int (MIN (xll_bitpos, xrl_bitpos)));
6083 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6084 size_int (MIN (xlr_bitpos, xrr_bitpos)));
6086 /* Convert to the smaller type before masking out unwanted bits. */
6087 type = lntype;
6088 if (lntype != rntype)
6090 if (lnbitsize > rnbitsize)
6092 lhs = fold_convert_loc (loc, rntype, lhs);
6093 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6094 type = rntype;
6096 else if (lnbitsize < rnbitsize)
6098 rhs = fold_convert_loc (loc, lntype, rhs);
6099 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6100 type = lntype;
6104 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6105 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6107 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6108 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6110 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6113 return 0;
6116 /* Handle the case of comparisons with constants. If there is something in
6117 common between the masks, those bits of the constants must be the same.
6118 If not, the condition is always false. Test for this to avoid generating
6119 incorrect code below. */
6120 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
6121 if (! integer_zerop (result)
6122 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
6123 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
6125 if (wanted_code == NE_EXPR)
6127 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6128 return constant_boolean_node (true, truth_type);
6130 else
6132 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6133 return constant_boolean_node (false, truth_type);
6137 if (lnbitpos < 0)
6138 return 0;
6140 /* Construct the expression we will return. First get the component
6141 reference we will make. Unless the mask is all ones the width of
6142 that field, perform the mask operation. Then compare with the
6143 merged constant. */
6144 result = make_bit_field_ref (loc, ll_inner, ll_arg,
6145 lntype, lnbitsize, lnbitpos,
6146 ll_unsignedp || rl_unsignedp, ll_reversep);
6148 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6149 if (! all_ones_mask_p (ll_mask, lnbitsize))
6150 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
6152 return build2_loc (loc, wanted_code, truth_type, result,
6153 const_binop (BIT_IOR_EXPR, l_const, r_const));
6156 /* T is an integer expression that is being multiplied, divided, or taken a
6157 modulus (CODE says which and what kind of divide or modulus) by a
6158 constant C. See if we can eliminate that operation by folding it with
6159 other operations already in T. WIDE_TYPE, if non-null, is a type that
6160 should be used for the computation if wider than our type.
6162 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6163 (X * 2) + (Y * 4). We must, however, be assured that either the original
6164 expression would not overflow or that overflow is undefined for the type
6165 in the language in question.
6167 If we return a non-null expression, it is an equivalent form of the
6168 original computation, but need not be in the original type.
6170 We set *STRICT_OVERFLOW_P to true if the return values depends on
6171 signed overflow being undefined. Otherwise we do not change
6172 *STRICT_OVERFLOW_P. */
6174 static tree
6175 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6176 bool *strict_overflow_p)
6178 /* To avoid exponential search depth, refuse to allow recursion past
6179 three levels. Beyond that (1) it's highly unlikely that we'll find
6180 something interesting and (2) we've probably processed it before
6181 when we built the inner expression. */
6183 static int depth;
6184 tree ret;
6186 if (depth > 3)
6187 return NULL;
6189 depth++;
6190 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6191 depth--;
6193 return ret;
6196 static tree
6197 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6198 bool *strict_overflow_p)
6200 tree type = TREE_TYPE (t);
6201 enum tree_code tcode = TREE_CODE (t);
6202 tree ctype = (wide_type != 0
6203 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6204 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6205 ? wide_type : type);
6206 tree t1, t2;
6207 int same_p = tcode == code;
6208 tree op0 = NULL_TREE, op1 = NULL_TREE;
6209 bool sub_strict_overflow_p;
6211 /* Don't deal with constants of zero here; they confuse the code below. */
6212 if (integer_zerop (c))
6213 return NULL_TREE;
6215 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6216 op0 = TREE_OPERAND (t, 0);
6218 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6219 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6221 /* Note that we need not handle conditional operations here since fold
6222 already handles those cases. So just do arithmetic here. */
6223 switch (tcode)
6225 case INTEGER_CST:
6226 /* For a constant, we can always simplify if we are a multiply
6227 or (for divide and modulus) if it is a multiple of our constant. */
6228 if (code == MULT_EXPR
6229 || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6230 TYPE_SIGN (type)))
6232 tree tem = const_binop (code, fold_convert (ctype, t),
6233 fold_convert (ctype, c));
6234 /* If the multiplication overflowed, we lost information on it.
6235 See PR68142 and PR69845. */
6236 if (TREE_OVERFLOW (tem))
6237 return NULL_TREE;
6238 return tem;
6240 break;
6242 CASE_CONVERT: case NON_LVALUE_EXPR:
6243 /* If op0 is an expression ... */
6244 if ((COMPARISON_CLASS_P (op0)
6245 || UNARY_CLASS_P (op0)
6246 || BINARY_CLASS_P (op0)
6247 || VL_EXP_CLASS_P (op0)
6248 || EXPRESSION_CLASS_P (op0))
6249 /* ... and has wrapping overflow, and its type is smaller
6250 than ctype, then we cannot pass through as widening. */
6251 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6252 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6253 && (TYPE_PRECISION (ctype)
6254 > TYPE_PRECISION (TREE_TYPE (op0))))
6255 /* ... or this is a truncation (t is narrower than op0),
6256 then we cannot pass through this narrowing. */
6257 || (TYPE_PRECISION (type)
6258 < TYPE_PRECISION (TREE_TYPE (op0)))
6259 /* ... or signedness changes for division or modulus,
6260 then we cannot pass through this conversion. */
6261 || (code != MULT_EXPR
6262 && (TYPE_UNSIGNED (ctype)
6263 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6264 /* ... or has undefined overflow while the converted to
6265 type has not, we cannot do the operation in the inner type
6266 as that would introduce undefined overflow. */
6267 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6268 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6269 && !TYPE_OVERFLOW_UNDEFINED (type))))
6270 break;
6272 /* Pass the constant down and see if we can make a simplification. If
6273 we can, replace this expression with the inner simplification for
6274 possible later conversion to our or some other type. */
6275 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6276 && TREE_CODE (t2) == INTEGER_CST
6277 && !TREE_OVERFLOW (t2)
6278 && (t1 = extract_muldiv (op0, t2, code,
6279 code == MULT_EXPR ? ctype : NULL_TREE,
6280 strict_overflow_p)) != 0)
6281 return t1;
6282 break;
6284 case ABS_EXPR:
6285 /* If widening the type changes it from signed to unsigned, then we
6286 must avoid building ABS_EXPR itself as unsigned. */
6287 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6289 tree cstype = (*signed_type_for) (ctype);
6290 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6291 != 0)
6293 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6294 return fold_convert (ctype, t1);
6296 break;
6298 /* If the constant is negative, we cannot simplify this. */
6299 if (tree_int_cst_sgn (c) == -1)
6300 break;
6301 /* FALLTHROUGH */
6302 case NEGATE_EXPR:
6303 /* For division and modulus, type can't be unsigned, as e.g.
6304 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6305 For signed types, even with wrapping overflow, this is fine. */
6306 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6307 break;
6308 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6309 != 0)
6310 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6311 break;
6313 case MIN_EXPR: case MAX_EXPR:
6314 /* If widening the type changes the signedness, then we can't perform
6315 this optimization as that changes the result. */
6316 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6317 break;
6319 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6320 sub_strict_overflow_p = false;
6321 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6322 &sub_strict_overflow_p)) != 0
6323 && (t2 = extract_muldiv (op1, c, code, wide_type,
6324 &sub_strict_overflow_p)) != 0)
6326 if (tree_int_cst_sgn (c) < 0)
6327 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6328 if (sub_strict_overflow_p)
6329 *strict_overflow_p = true;
6330 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6331 fold_convert (ctype, t2));
6333 break;
6335 case LSHIFT_EXPR: case RSHIFT_EXPR:
6336 /* If the second operand is constant, this is a multiplication
6337 or floor division, by a power of two, so we can treat it that
6338 way unless the multiplier or divisor overflows. Signed
6339 left-shift overflow is implementation-defined rather than
6340 undefined in C90, so do not convert signed left shift into
6341 multiplication. */
6342 if (TREE_CODE (op1) == INTEGER_CST
6343 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6344 /* const_binop may not detect overflow correctly,
6345 so check for it explicitly here. */
6346 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6347 wi::to_wide (op1))
6348 && (t1 = fold_convert (ctype,
6349 const_binop (LSHIFT_EXPR, size_one_node,
6350 op1))) != 0
6351 && !TREE_OVERFLOW (t1))
6352 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6353 ? MULT_EXPR : FLOOR_DIV_EXPR,
6354 ctype,
6355 fold_convert (ctype, op0),
6356 t1),
6357 c, code, wide_type, strict_overflow_p);
6358 break;
6360 case PLUS_EXPR: case MINUS_EXPR:
6361 /* See if we can eliminate the operation on both sides. If we can, we
6362 can return a new PLUS or MINUS. If we can't, the only remaining
6363 cases where we can do anything are if the second operand is a
6364 constant. */
6365 sub_strict_overflow_p = false;
6366 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6367 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6368 if (t1 != 0 && t2 != 0
6369 && TYPE_OVERFLOW_WRAPS (ctype)
6370 && (code == MULT_EXPR
6371 /* If not multiplication, we can only do this if both operands
6372 are divisible by c. */
6373 || (multiple_of_p (ctype, op0, c)
6374 && multiple_of_p (ctype, op1, c))))
6376 if (sub_strict_overflow_p)
6377 *strict_overflow_p = true;
6378 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6379 fold_convert (ctype, t2));
6382 /* If this was a subtraction, negate OP1 and set it to be an addition.
6383 This simplifies the logic below. */
6384 if (tcode == MINUS_EXPR)
6386 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6387 /* If OP1 was not easily negatable, the constant may be OP0. */
6388 if (TREE_CODE (op0) == INTEGER_CST)
6390 std::swap (op0, op1);
6391 std::swap (t1, t2);
6395 if (TREE_CODE (op1) != INTEGER_CST)
6396 break;
6398 /* If either OP1 or C are negative, this optimization is not safe for
6399 some of the division and remainder types while for others we need
6400 to change the code. */
6401 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6403 if (code == CEIL_DIV_EXPR)
6404 code = FLOOR_DIV_EXPR;
6405 else if (code == FLOOR_DIV_EXPR)
6406 code = CEIL_DIV_EXPR;
6407 else if (code != MULT_EXPR
6408 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6409 break;
6412 /* If it's a multiply or a division/modulus operation of a multiple
6413 of our constant, do the operation and verify it doesn't overflow. */
6414 if (code == MULT_EXPR
6415 || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6416 TYPE_SIGN (type)))
6418 op1 = const_binop (code, fold_convert (ctype, op1),
6419 fold_convert (ctype, c));
6420 /* We allow the constant to overflow with wrapping semantics. */
6421 if (op1 == 0
6422 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6423 break;
6425 else
6426 break;
6428 /* If we have an unsigned type, we cannot widen the operation since it
6429 will change the result if the original computation overflowed. */
6430 if (TYPE_UNSIGNED (ctype) && ctype != type)
6431 break;
6433 /* The last case is if we are a multiply. In that case, we can
6434 apply the distributive law to commute the multiply and addition
6435 if the multiplication of the constants doesn't overflow
6436 and overflow is defined. With undefined overflow
6437 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6438 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6439 return fold_build2 (tcode, ctype,
6440 fold_build2 (code, ctype,
6441 fold_convert (ctype, op0),
6442 fold_convert (ctype, c)),
6443 op1);
6445 break;
6447 case MULT_EXPR:
6448 /* We have a special case here if we are doing something like
6449 (C * 8) % 4 since we know that's zero. */
6450 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6451 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6452 /* If the multiplication can overflow we cannot optimize this. */
6453 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6454 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6455 && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6456 TYPE_SIGN (type)))
6458 *strict_overflow_p = true;
6459 return omit_one_operand (type, integer_zero_node, op0);
6462 /* ... fall through ... */
6464 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6465 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6466 /* If we can extract our operation from the LHS, do so and return a
6467 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6468 do something only if the second operand is a constant. */
6469 if (same_p
6470 && TYPE_OVERFLOW_WRAPS (ctype)
6471 && (t1 = extract_muldiv (op0, c, code, wide_type,
6472 strict_overflow_p)) != 0)
6473 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6474 fold_convert (ctype, op1));
6475 else if (tcode == MULT_EXPR && code == MULT_EXPR
6476 && TYPE_OVERFLOW_WRAPS (ctype)
6477 && (t1 = extract_muldiv (op1, c, code, wide_type,
6478 strict_overflow_p)) != 0)
6479 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6480 fold_convert (ctype, t1));
6481 else if (TREE_CODE (op1) != INTEGER_CST)
6482 return 0;
6484 /* If these are the same operation types, we can associate them
6485 assuming no overflow. */
6486 if (tcode == code)
6488 bool overflow_p = false;
6489 wi::overflow_type overflow_mul;
6490 signop sign = TYPE_SIGN (ctype);
6491 unsigned prec = TYPE_PRECISION (ctype);
6492 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6493 wi::to_wide (c, prec),
6494 sign, &overflow_mul);
6495 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6496 if (overflow_mul
6497 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6498 overflow_p = true;
6499 if (!overflow_p)
6500 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6501 wide_int_to_tree (ctype, mul));
6504 /* If these operations "cancel" each other, we have the main
6505 optimizations of this pass, which occur when either constant is a
6506 multiple of the other, in which case we replace this with either an
6507 operation or CODE or TCODE.
6509 If we have an unsigned type, we cannot do this since it will change
6510 the result if the original computation overflowed. */
6511 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6512 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6513 || (tcode == MULT_EXPR
6514 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6515 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6516 && code != MULT_EXPR)))
6518 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6519 TYPE_SIGN (type)))
6521 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6522 *strict_overflow_p = true;
6523 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6524 fold_convert (ctype,
6525 const_binop (TRUNC_DIV_EXPR,
6526 op1, c)));
6528 else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6529 TYPE_SIGN (type)))
6531 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6532 *strict_overflow_p = true;
6533 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6534 fold_convert (ctype,
6535 const_binop (TRUNC_DIV_EXPR,
6536 c, op1)));
6539 break;
6541 default:
6542 break;
6545 return 0;
6548 /* Return a node which has the indicated constant VALUE (either 0 or
6549 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6550 and is of the indicated TYPE. */
6552 tree
6553 constant_boolean_node (bool value, tree type)
6555 if (type == integer_type_node)
6556 return value ? integer_one_node : integer_zero_node;
6557 else if (type == boolean_type_node)
6558 return value ? boolean_true_node : boolean_false_node;
6559 else if (TREE_CODE (type) == VECTOR_TYPE)
6560 return build_vector_from_val (type,
6561 build_int_cst (TREE_TYPE (type),
6562 value ? -1 : 0));
6563 else
6564 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6568 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6569 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6570 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6571 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6572 COND is the first argument to CODE; otherwise (as in the example
6573 given here), it is the second argument. TYPE is the type of the
6574 original expression. Return NULL_TREE if no simplification is
6575 possible. */
6577 static tree
6578 fold_binary_op_with_conditional_arg (location_t loc,
6579 enum tree_code code,
6580 tree type, tree op0, tree op1,
6581 tree cond, tree arg, int cond_first_p)
6583 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6584 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6585 tree test, true_value, false_value;
6586 tree lhs = NULL_TREE;
6587 tree rhs = NULL_TREE;
6588 enum tree_code cond_code = COND_EXPR;
6590 if (TREE_CODE (cond) == COND_EXPR
6591 || TREE_CODE (cond) == VEC_COND_EXPR)
6593 test = TREE_OPERAND (cond, 0);
6594 true_value = TREE_OPERAND (cond, 1);
6595 false_value = TREE_OPERAND (cond, 2);
6596 /* If this operand throws an expression, then it does not make
6597 sense to try to perform a logical or arithmetic operation
6598 involving it. */
6599 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6600 lhs = true_value;
6601 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6602 rhs = false_value;
6604 else if (!(TREE_CODE (type) != VECTOR_TYPE
6605 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6607 tree testtype = TREE_TYPE (cond);
6608 test = cond;
6609 true_value = constant_boolean_node (true, testtype);
6610 false_value = constant_boolean_node (false, testtype);
6612 else
6613 /* Detect the case of mixing vector and scalar types - bail out. */
6614 return NULL_TREE;
6616 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6617 cond_code = VEC_COND_EXPR;
6619 /* This transformation is only worthwhile if we don't have to wrap ARG
6620 in a SAVE_EXPR and the operation can be simplified without recursing
6621 on at least one of the branches once its pushed inside the COND_EXPR. */
6622 if (!TREE_CONSTANT (arg)
6623 && (TREE_SIDE_EFFECTS (arg)
6624 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6625 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6626 return NULL_TREE;
6628 arg = fold_convert_loc (loc, arg_type, arg);
6629 if (lhs == 0)
6631 true_value = fold_convert_loc (loc, cond_type, true_value);
6632 if (cond_first_p)
6633 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6634 else
6635 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6637 if (rhs == 0)
6639 false_value = fold_convert_loc (loc, cond_type, false_value);
6640 if (cond_first_p)
6641 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6642 else
6643 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6646 /* Check that we have simplified at least one of the branches. */
6647 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6648 return NULL_TREE;
6650 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6654 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6656 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6657 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6658 ADDEND is the same as X.
6660 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6661 and finite. The problematic cases are when X is zero, and its mode
6662 has signed zeros. In the case of rounding towards -infinity,
6663 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6664 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6666 bool
6667 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6669 if (!real_zerop (addend))
6670 return false;
6672 /* Don't allow the fold with -fsignaling-nans. */
6673 if (HONOR_SNANS (element_mode (type)))
6674 return false;
6676 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6677 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6678 return true;
6680 /* In a vector or complex, we would need to check the sign of all zeros. */
6681 if (TREE_CODE (addend) != REAL_CST)
6682 return false;
6684 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6685 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6686 negate = !negate;
6688 /* The mode has signed zeros, and we have to honor their sign.
6689 In this situation, there is only one case we can return true for.
6690 X - 0 is the same as X unless rounding towards -infinity is
6691 supported. */
6692 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6695 /* Subroutine of match.pd that optimizes comparisons of a division by
6696 a nonzero integer constant against an integer constant, i.e.
6697 X/C1 op C2.
6699 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6700 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6702 enum tree_code
6703 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
6704 tree *hi, bool *neg_overflow)
6706 tree prod, tmp, type = TREE_TYPE (c1);
6707 signop sign = TYPE_SIGN (type);
6708 wi::overflow_type overflow;
6710 /* We have to do this the hard way to detect unsigned overflow.
6711 prod = int_const_binop (MULT_EXPR, c1, c2); */
6712 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
6713 prod = force_fit_type (type, val, -1, overflow);
6714 *neg_overflow = false;
6716 if (sign == UNSIGNED)
6718 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6719 *lo = prod;
6721 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6722 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
6723 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
6725 else if (tree_int_cst_sgn (c1) >= 0)
6727 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6728 switch (tree_int_cst_sgn (c2))
6730 case -1:
6731 *neg_overflow = true;
6732 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
6733 *hi = prod;
6734 break;
6736 case 0:
6737 *lo = fold_negate_const (tmp, type);
6738 *hi = tmp;
6739 break;
6741 case 1:
6742 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
6743 *lo = prod;
6744 break;
6746 default:
6747 gcc_unreachable ();
6750 else
6752 /* A negative divisor reverses the relational operators. */
6753 code = swap_tree_comparison (code);
6755 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
6756 switch (tree_int_cst_sgn (c2))
6758 case -1:
6759 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
6760 *lo = prod;
6761 break;
6763 case 0:
6764 *hi = fold_negate_const (tmp, type);
6765 *lo = tmp;
6766 break;
6768 case 1:
6769 *neg_overflow = true;
6770 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
6771 *hi = prod;
6772 break;
6774 default:
6775 gcc_unreachable ();
6779 if (code != EQ_EXPR && code != NE_EXPR)
6780 return code;
6782 if (TREE_OVERFLOW (*lo)
6783 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
6784 *lo = NULL_TREE;
6785 if (TREE_OVERFLOW (*hi)
6786 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
6787 *hi = NULL_TREE;
6789 return code;
6793 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6794 equality/inequality test, then return a simplified form of the test
6795 using a sign testing. Otherwise return NULL. TYPE is the desired
6796 result type. */
6798 static tree
6799 fold_single_bit_test_into_sign_test (location_t loc,
6800 enum tree_code code, tree arg0, tree arg1,
6801 tree result_type)
6803 /* If this is testing a single bit, we can optimize the test. */
6804 if ((code == NE_EXPR || code == EQ_EXPR)
6805 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6806 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6808 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6809 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6810 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6812 if (arg00 != NULL_TREE
6813 /* This is only a win if casting to a signed type is cheap,
6814 i.e. when arg00's type is not a partial mode. */
6815 && type_has_mode_precision_p (TREE_TYPE (arg00)))
6817 tree stype = signed_type_for (TREE_TYPE (arg00));
6818 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6819 result_type,
6820 fold_convert_loc (loc, stype, arg00),
6821 build_int_cst (stype, 0));
6825 return NULL_TREE;
6828 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6829 equality/inequality test, then return a simplified form of
6830 the test using shifts and logical operations. Otherwise return
6831 NULL. TYPE is the desired result type. */
6833 tree
6834 fold_single_bit_test (location_t loc, enum tree_code code,
6835 tree arg0, tree arg1, tree result_type)
6837 /* If this is testing a single bit, we can optimize the test. */
6838 if ((code == NE_EXPR || code == EQ_EXPR)
6839 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6840 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6842 tree inner = TREE_OPERAND (arg0, 0);
6843 tree type = TREE_TYPE (arg0);
6844 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6845 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
6846 int ops_unsigned;
6847 tree signed_type, unsigned_type, intermediate_type;
6848 tree tem, one;
6850 /* First, see if we can fold the single bit test into a sign-bit
6851 test. */
6852 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6853 result_type);
6854 if (tem)
6855 return tem;
6857 /* Otherwise we have (A & C) != 0 where C is a single bit,
6858 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6859 Similarly for (A & C) == 0. */
6861 /* If INNER is a right shift of a constant and it plus BITNUM does
6862 not overflow, adjust BITNUM and INNER. */
6863 if (TREE_CODE (inner) == RSHIFT_EXPR
6864 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6865 && bitnum < TYPE_PRECISION (type)
6866 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner, 1)),
6867 TYPE_PRECISION (type) - bitnum))
6869 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6870 inner = TREE_OPERAND (inner, 0);
6873 /* If we are going to be able to omit the AND below, we must do our
6874 operations as unsigned. If we must use the AND, we have a choice.
6875 Normally unsigned is faster, but for some machines signed is. */
6876 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6877 && !flag_syntax_only) ? 0 : 1;
6879 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6880 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6881 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6882 inner = fold_convert_loc (loc, intermediate_type, inner);
6884 if (bitnum != 0)
6885 inner = build2 (RSHIFT_EXPR, intermediate_type,
6886 inner, size_int (bitnum));
6888 one = build_int_cst (intermediate_type, 1);
6890 if (code == EQ_EXPR)
6891 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6893 /* Put the AND last so it can combine with more things. */
6894 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6896 /* Make sure to return the proper type. */
6897 inner = fold_convert_loc (loc, result_type, inner);
6899 return inner;
6901 return NULL_TREE;
6904 /* Test whether it is preferable two swap two operands, ARG0 and
6905 ARG1, for example because ARG0 is an integer constant and ARG1
6906 isn't. */
6908 bool
6909 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6911 if (CONSTANT_CLASS_P (arg1))
6912 return 0;
6913 if (CONSTANT_CLASS_P (arg0))
6914 return 1;
6916 STRIP_NOPS (arg0);
6917 STRIP_NOPS (arg1);
6919 if (TREE_CONSTANT (arg1))
6920 return 0;
6921 if (TREE_CONSTANT (arg0))
6922 return 1;
6924 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6925 for commutative and comparison operators. Ensuring a canonical
6926 form allows the optimizers to find additional redundancies without
6927 having to explicitly check for both orderings. */
6928 if (TREE_CODE (arg0) == SSA_NAME
6929 && TREE_CODE (arg1) == SSA_NAME
6930 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6931 return 1;
6933 /* Put SSA_NAMEs last. */
6934 if (TREE_CODE (arg1) == SSA_NAME)
6935 return 0;
6936 if (TREE_CODE (arg0) == SSA_NAME)
6937 return 1;
6939 /* Put variables last. */
6940 if (DECL_P (arg1))
6941 return 0;
6942 if (DECL_P (arg0))
6943 return 1;
6945 return 0;
6949 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6950 means A >= Y && A != MAX, but in this case we know that
6951 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6953 static tree
6954 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6956 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6958 if (TREE_CODE (bound) == LT_EXPR)
6959 a = TREE_OPERAND (bound, 0);
6960 else if (TREE_CODE (bound) == GT_EXPR)
6961 a = TREE_OPERAND (bound, 1);
6962 else
6963 return NULL_TREE;
6965 typea = TREE_TYPE (a);
6966 if (!INTEGRAL_TYPE_P (typea)
6967 && !POINTER_TYPE_P (typea))
6968 return NULL_TREE;
6970 if (TREE_CODE (ineq) == LT_EXPR)
6972 a1 = TREE_OPERAND (ineq, 1);
6973 y = TREE_OPERAND (ineq, 0);
6975 else if (TREE_CODE (ineq) == GT_EXPR)
6977 a1 = TREE_OPERAND (ineq, 0);
6978 y = TREE_OPERAND (ineq, 1);
6980 else
6981 return NULL_TREE;
6983 if (TREE_TYPE (a1) != typea)
6984 return NULL_TREE;
6986 if (POINTER_TYPE_P (typea))
6988 /* Convert the pointer types into integer before taking the difference. */
6989 tree ta = fold_convert_loc (loc, ssizetype, a);
6990 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6991 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6993 else
6994 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6996 if (!diff || !integer_onep (diff))
6997 return NULL_TREE;
6999 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7002 /* Fold a sum or difference of at least one multiplication.
7003 Returns the folded tree or NULL if no simplification could be made. */
7005 static tree
7006 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7007 tree arg0, tree arg1)
7009 tree arg00, arg01, arg10, arg11;
7010 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7012 /* (A * C) +- (B * C) -> (A+-B) * C.
7013 (A * C) +- A -> A * (C+-1).
7014 We are most concerned about the case where C is a constant,
7015 but other combinations show up during loop reduction. Since
7016 it is not difficult, try all four possibilities. */
7018 if (TREE_CODE (arg0) == MULT_EXPR)
7020 arg00 = TREE_OPERAND (arg0, 0);
7021 arg01 = TREE_OPERAND (arg0, 1);
7023 else if (TREE_CODE (arg0) == INTEGER_CST)
7025 arg00 = build_one_cst (type);
7026 arg01 = arg0;
7028 else
7030 /* We cannot generate constant 1 for fract. */
7031 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7032 return NULL_TREE;
7033 arg00 = arg0;
7034 arg01 = build_one_cst (type);
7036 if (TREE_CODE (arg1) == MULT_EXPR)
7038 arg10 = TREE_OPERAND (arg1, 0);
7039 arg11 = TREE_OPERAND (arg1, 1);
7041 else if (TREE_CODE (arg1) == INTEGER_CST)
7043 arg10 = build_one_cst (type);
7044 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7045 the purpose of this canonicalization. */
7046 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7047 && negate_expr_p (arg1)
7048 && code == PLUS_EXPR)
7050 arg11 = negate_expr (arg1);
7051 code = MINUS_EXPR;
7053 else
7054 arg11 = arg1;
7056 else
7058 /* We cannot generate constant 1 for fract. */
7059 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7060 return NULL_TREE;
7061 arg10 = arg1;
7062 arg11 = build_one_cst (type);
7064 same = NULL_TREE;
7066 /* Prefer factoring a common non-constant. */
7067 if (operand_equal_p (arg00, arg10, 0))
7068 same = arg00, alt0 = arg01, alt1 = arg11;
7069 else if (operand_equal_p (arg01, arg11, 0))
7070 same = arg01, alt0 = arg00, alt1 = arg10;
7071 else if (operand_equal_p (arg00, arg11, 0))
7072 same = arg00, alt0 = arg01, alt1 = arg10;
7073 else if (operand_equal_p (arg01, arg10, 0))
7074 same = arg01, alt0 = arg00, alt1 = arg11;
7076 /* No identical multiplicands; see if we can find a common
7077 power-of-two factor in non-power-of-two multiplies. This
7078 can help in multi-dimensional array access. */
7079 else if (tree_fits_shwi_p (arg01)
7080 && tree_fits_shwi_p (arg11))
7082 HOST_WIDE_INT int01, int11, tmp;
7083 bool swap = false;
7084 tree maybe_same;
7085 int01 = tree_to_shwi (arg01);
7086 int11 = tree_to_shwi (arg11);
7088 /* Move min of absolute values to int11. */
7089 if (absu_hwi (int01) < absu_hwi (int11))
7091 tmp = int01, int01 = int11, int11 = tmp;
7092 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7093 maybe_same = arg01;
7094 swap = true;
7096 else
7097 maybe_same = arg11;
7099 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7100 /* The remainder should not be a constant, otherwise we
7101 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7102 increased the number of multiplications necessary. */
7103 && TREE_CODE (arg10) != INTEGER_CST)
7105 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7106 build_int_cst (TREE_TYPE (arg00),
7107 int01 / int11));
7108 alt1 = arg10;
7109 same = maybe_same;
7110 if (swap)
7111 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7115 if (!same)
7116 return NULL_TREE;
7118 if (! INTEGRAL_TYPE_P (type)
7119 || TYPE_OVERFLOW_WRAPS (type)
7120 /* We are neither factoring zero nor minus one. */
7121 || TREE_CODE (same) == INTEGER_CST)
7122 return fold_build2_loc (loc, MULT_EXPR, type,
7123 fold_build2_loc (loc, code, type,
7124 fold_convert_loc (loc, type, alt0),
7125 fold_convert_loc (loc, type, alt1)),
7126 fold_convert_loc (loc, type, same));
7128 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7129 same may be minus one and thus the multiplication may overflow. Perform
7130 the sum operation in an unsigned type. */
7131 tree utype = unsigned_type_for (type);
7132 tree tem = fold_build2_loc (loc, code, utype,
7133 fold_convert_loc (loc, utype, alt0),
7134 fold_convert_loc (loc, utype, alt1));
7135 /* If the sum evaluated to a constant that is not -INF the multiplication
7136 cannot overflow. */
7137 if (TREE_CODE (tem) == INTEGER_CST
7138 && (wi::to_wide (tem)
7139 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7140 return fold_build2_loc (loc, MULT_EXPR, type,
7141 fold_convert (type, tem), same);
7143 /* Do not resort to unsigned multiplication because
7144 we lose the no-overflow property of the expression. */
7145 return NULL_TREE;
7148 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7149 specified by EXPR into the buffer PTR of length LEN bytes.
7150 Return the number of bytes placed in the buffer, or zero
7151 upon failure. */
7153 static int
7154 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7156 tree type = TREE_TYPE (expr);
7157 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7158 int byte, offset, word, words;
7159 unsigned char value;
7161 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7162 return 0;
7163 if (off == -1)
7164 off = 0;
7166 if (ptr == NULL)
7167 /* Dry run. */
7168 return MIN (len, total_bytes - off);
7170 words = total_bytes / UNITS_PER_WORD;
7172 for (byte = 0; byte < total_bytes; byte++)
7174 int bitpos = byte * BITS_PER_UNIT;
7175 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7176 number of bytes. */
7177 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7179 if (total_bytes > UNITS_PER_WORD)
7181 word = byte / UNITS_PER_WORD;
7182 if (WORDS_BIG_ENDIAN)
7183 word = (words - 1) - word;
7184 offset = word * UNITS_PER_WORD;
7185 if (BYTES_BIG_ENDIAN)
7186 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7187 else
7188 offset += byte % UNITS_PER_WORD;
7190 else
7191 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7192 if (offset >= off && offset - off < len)
7193 ptr[offset - off] = value;
7195 return MIN (len, total_bytes - off);
7199 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7200 specified by EXPR into the buffer PTR of length LEN bytes.
7201 Return the number of bytes placed in the buffer, or zero
7202 upon failure. */
7204 static int
7205 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7207 tree type = TREE_TYPE (expr);
7208 scalar_mode mode = SCALAR_TYPE_MODE (type);
7209 int total_bytes = GET_MODE_SIZE (mode);
7210 FIXED_VALUE_TYPE value;
7211 tree i_value, i_type;
7213 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7214 return 0;
7216 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7218 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7219 return 0;
7221 value = TREE_FIXED_CST (expr);
7222 i_value = double_int_to_tree (i_type, value.data);
7224 return native_encode_int (i_value, ptr, len, off);
7228 /* Subroutine of native_encode_expr. Encode the REAL_CST
7229 specified by EXPR into the buffer PTR of length LEN bytes.
7230 Return the number of bytes placed in the buffer, or zero
7231 upon failure. */
7233 static int
7234 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7236 tree type = TREE_TYPE (expr);
7237 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7238 int byte, offset, word, words, bitpos;
7239 unsigned char value;
7241 /* There are always 32 bits in each long, no matter the size of
7242 the hosts long. We handle floating point representations with
7243 up to 192 bits. */
7244 long tmp[6];
7246 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7247 return 0;
7248 if (off == -1)
7249 off = 0;
7251 if (ptr == NULL)
7252 /* Dry run. */
7253 return MIN (len, total_bytes - off);
7255 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7257 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7259 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7260 bitpos += BITS_PER_UNIT)
7262 byte = (bitpos / BITS_PER_UNIT) & 3;
7263 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7265 if (UNITS_PER_WORD < 4)
7267 word = byte / UNITS_PER_WORD;
7268 if (WORDS_BIG_ENDIAN)
7269 word = (words - 1) - word;
7270 offset = word * UNITS_PER_WORD;
7271 if (BYTES_BIG_ENDIAN)
7272 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7273 else
7274 offset += byte % UNITS_PER_WORD;
7276 else
7278 offset = byte;
7279 if (BYTES_BIG_ENDIAN)
7281 /* Reverse bytes within each long, or within the entire float
7282 if it's smaller than a long (for HFmode). */
7283 offset = MIN (3, total_bytes - 1) - offset;
7284 gcc_assert (offset >= 0);
7287 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7288 if (offset >= off
7289 && offset - off < len)
7290 ptr[offset - off] = value;
7292 return MIN (len, total_bytes - off);
7295 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7296 specified by EXPR into the buffer PTR of length LEN bytes.
7297 Return the number of bytes placed in the buffer, or zero
7298 upon failure. */
7300 static int
7301 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7303 int rsize, isize;
7304 tree part;
7306 part = TREE_REALPART (expr);
7307 rsize = native_encode_expr (part, ptr, len, off);
7308 if (off == -1 && rsize == 0)
7309 return 0;
7310 part = TREE_IMAGPART (expr);
7311 if (off != -1)
7312 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7313 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7314 len - rsize, off);
7315 if (off == -1 && isize != rsize)
7316 return 0;
7317 return rsize + isize;
7321 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7322 specified by EXPR into the buffer PTR of length LEN bytes.
7323 Return the number of bytes placed in the buffer, or zero
7324 upon failure. */
7326 static int
7327 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7329 unsigned HOST_WIDE_INT i, count;
7330 int size, offset;
7331 tree itype, elem;
7333 offset = 0;
7334 if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7335 return 0;
7336 itype = TREE_TYPE (TREE_TYPE (expr));
7337 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7338 for (i = 0; i < count; i++)
7340 if (off >= size)
7342 off -= size;
7343 continue;
7345 elem = VECTOR_CST_ELT (expr, i);
7346 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7347 len - offset, off);
7348 if ((off == -1 && res != size) || res == 0)
7349 return 0;
7350 offset += res;
7351 if (offset >= len)
7352 return (off == -1 && i < count - 1) ? 0 : offset;
7353 if (off != -1)
7354 off = 0;
7356 return offset;
7360 /* Subroutine of native_encode_expr. Encode the STRING_CST
7361 specified by EXPR into the buffer PTR of length LEN bytes.
7362 Return the number of bytes placed in the buffer, or zero
7363 upon failure. */
7365 static int
7366 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7368 tree type = TREE_TYPE (expr);
7370 /* Wide-char strings are encoded in target byte-order so native
7371 encoding them is trivial. */
7372 if (BITS_PER_UNIT != CHAR_BIT
7373 || TREE_CODE (type) != ARRAY_TYPE
7374 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7375 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7376 return 0;
7378 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7379 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7380 return 0;
7381 if (off == -1)
7382 off = 0;
7383 if (ptr == NULL)
7384 /* Dry run. */;
7385 else if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7387 int written = 0;
7388 if (off < TREE_STRING_LENGTH (expr))
7390 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7391 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7393 memset (ptr + written, 0,
7394 MIN (total_bytes - written, len - written));
7396 else
7397 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7398 return MIN (total_bytes - off, len);
7402 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7403 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7404 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7405 anything, just do a dry run. If OFF is not -1 then start
7406 the encoding at byte offset OFF and encode at most LEN bytes.
7407 Return the number of bytes placed in the buffer, or zero upon failure. */
7410 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7412 /* We don't support starting at negative offset and -1 is special. */
7413 if (off < -1)
7414 return 0;
7416 switch (TREE_CODE (expr))
7418 case INTEGER_CST:
7419 return native_encode_int (expr, ptr, len, off);
7421 case REAL_CST:
7422 return native_encode_real (expr, ptr, len, off);
7424 case FIXED_CST:
7425 return native_encode_fixed (expr, ptr, len, off);
7427 case COMPLEX_CST:
7428 return native_encode_complex (expr, ptr, len, off);
7430 case VECTOR_CST:
7431 return native_encode_vector (expr, ptr, len, off);
7433 case STRING_CST:
7434 return native_encode_string (expr, ptr, len, off);
7436 default:
7437 return 0;
7442 /* Subroutine of native_interpret_expr. Interpret the contents of
7443 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7444 If the buffer cannot be interpreted, return NULL_TREE. */
7446 static tree
7447 native_interpret_int (tree type, const unsigned char *ptr, int len)
7449 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7451 if (total_bytes > len
7452 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7453 return NULL_TREE;
7455 wide_int result = wi::from_buffer (ptr, total_bytes);
7457 return wide_int_to_tree (type, result);
7461 /* Subroutine of native_interpret_expr. Interpret the contents of
7462 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7463 If the buffer cannot be interpreted, return NULL_TREE. */
7465 static tree
7466 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7468 scalar_mode mode = SCALAR_TYPE_MODE (type);
7469 int total_bytes = GET_MODE_SIZE (mode);
7470 double_int result;
7471 FIXED_VALUE_TYPE fixed_value;
7473 if (total_bytes > len
7474 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7475 return NULL_TREE;
7477 result = double_int::from_buffer (ptr, total_bytes);
7478 fixed_value = fixed_from_double_int (result, mode);
7480 return build_fixed (type, fixed_value);
7484 /* Subroutine of native_interpret_expr. Interpret the contents of
7485 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7486 If the buffer cannot be interpreted, return NULL_TREE. */
7488 static tree
7489 native_interpret_real (tree type, const unsigned char *ptr, int len)
7491 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
7492 int total_bytes = GET_MODE_SIZE (mode);
7493 unsigned char value;
7494 /* There are always 32 bits in each long, no matter the size of
7495 the hosts long. We handle floating point representations with
7496 up to 192 bits. */
7497 REAL_VALUE_TYPE r;
7498 long tmp[6];
7500 if (total_bytes > len || total_bytes > 24)
7501 return NULL_TREE;
7502 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7504 memset (tmp, 0, sizeof (tmp));
7505 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7506 bitpos += BITS_PER_UNIT)
7508 /* Both OFFSET and BYTE index within a long;
7509 bitpos indexes the whole float. */
7510 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7511 if (UNITS_PER_WORD < 4)
7513 int word = byte / UNITS_PER_WORD;
7514 if (WORDS_BIG_ENDIAN)
7515 word = (words - 1) - word;
7516 offset = word * UNITS_PER_WORD;
7517 if (BYTES_BIG_ENDIAN)
7518 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7519 else
7520 offset += byte % UNITS_PER_WORD;
7522 else
7524 offset = byte;
7525 if (BYTES_BIG_ENDIAN)
7527 /* Reverse bytes within each long, or within the entire float
7528 if it's smaller than a long (for HFmode). */
7529 offset = MIN (3, total_bytes - 1) - offset;
7530 gcc_assert (offset >= 0);
7533 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7535 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7538 real_from_target (&r, tmp, mode);
7539 return build_real (type, r);
7543 /* Subroutine of native_interpret_expr. Interpret the contents of
7544 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7545 If the buffer cannot be interpreted, return NULL_TREE. */
7547 static tree
7548 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7550 tree etype, rpart, ipart;
7551 int size;
7553 etype = TREE_TYPE (type);
7554 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7555 if (size * 2 > len)
7556 return NULL_TREE;
7557 rpart = native_interpret_expr (etype, ptr, size);
7558 if (!rpart)
7559 return NULL_TREE;
7560 ipart = native_interpret_expr (etype, ptr+size, size);
7561 if (!ipart)
7562 return NULL_TREE;
7563 return build_complex (type, rpart, ipart);
7567 /* Subroutine of native_interpret_expr. Interpret the contents of
7568 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7569 If the buffer cannot be interpreted, return NULL_TREE. */
7571 static tree
7572 native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
7574 tree etype, elem;
7575 unsigned int i, size;
7576 unsigned HOST_WIDE_INT count;
7578 etype = TREE_TYPE (type);
7579 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7580 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&count)
7581 || size * count > len)
7582 return NULL_TREE;
7584 tree_vector_builder elements (type, count, 1);
7585 for (i = 0; i < count; ++i)
7587 elem = native_interpret_expr (etype, ptr+(i*size), size);
7588 if (!elem)
7589 return NULL_TREE;
7590 elements.quick_push (elem);
7592 return elements.build ();
7596 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7597 the buffer PTR of length LEN as a constant of type TYPE. For
7598 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7599 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7600 return NULL_TREE. */
7602 tree
7603 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7605 switch (TREE_CODE (type))
7607 case INTEGER_TYPE:
7608 case ENUMERAL_TYPE:
7609 case BOOLEAN_TYPE:
7610 case POINTER_TYPE:
7611 case REFERENCE_TYPE:
7612 return native_interpret_int (type, ptr, len);
7614 case REAL_TYPE:
7615 return native_interpret_real (type, ptr, len);
7617 case FIXED_POINT_TYPE:
7618 return native_interpret_fixed (type, ptr, len);
7620 case COMPLEX_TYPE:
7621 return native_interpret_complex (type, ptr, len);
7623 case VECTOR_TYPE:
7624 return native_interpret_vector (type, ptr, len);
7626 default:
7627 return NULL_TREE;
7631 /* Returns true if we can interpret the contents of a native encoding
7632 as TYPE. */
7634 static bool
7635 can_native_interpret_type_p (tree type)
7637 switch (TREE_CODE (type))
7639 case INTEGER_TYPE:
7640 case ENUMERAL_TYPE:
7641 case BOOLEAN_TYPE:
7642 case POINTER_TYPE:
7643 case REFERENCE_TYPE:
7644 case FIXED_POINT_TYPE:
7645 case REAL_TYPE:
7646 case COMPLEX_TYPE:
7647 case VECTOR_TYPE:
7648 return true;
7649 default:
7650 return false;
7655 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7656 TYPE at compile-time. If we're unable to perform the conversion
7657 return NULL_TREE. */
7659 static tree
7660 fold_view_convert_expr (tree type, tree expr)
7662 /* We support up to 512-bit values (for V8DFmode). */
7663 unsigned char buffer[64];
7664 int len;
7666 /* Check that the host and target are sane. */
7667 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7668 return NULL_TREE;
7670 len = native_encode_expr (expr, buffer, sizeof (buffer));
7671 if (len == 0)
7672 return NULL_TREE;
7674 return native_interpret_expr (type, buffer, len);
7677 /* Build an expression for the address of T. Folds away INDIRECT_REF
7678 to avoid confusing the gimplify process. */
7680 tree
7681 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7683 /* The size of the object is not relevant when talking about its address. */
7684 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7685 t = TREE_OPERAND (t, 0);
7687 if (TREE_CODE (t) == INDIRECT_REF)
7689 t = TREE_OPERAND (t, 0);
7691 if (TREE_TYPE (t) != ptrtype)
7692 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7694 else if (TREE_CODE (t) == MEM_REF
7695 && integer_zerop (TREE_OPERAND (t, 1)))
7696 return TREE_OPERAND (t, 0);
7697 else if (TREE_CODE (t) == MEM_REF
7698 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7699 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7700 TREE_OPERAND (t, 0),
7701 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7702 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7704 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7706 if (TREE_TYPE (t) != ptrtype)
7707 t = fold_convert_loc (loc, ptrtype, t);
7709 else
7710 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7712 return t;
7715 /* Build an expression for the address of T. */
7717 tree
7718 build_fold_addr_expr_loc (location_t loc, tree t)
7720 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7722 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7725 /* Fold a unary expression of code CODE and type TYPE with operand
7726 OP0. Return the folded expression if folding is successful.
7727 Otherwise, return NULL_TREE. */
7729 tree
7730 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7732 tree tem;
7733 tree arg0;
7734 enum tree_code_class kind = TREE_CODE_CLASS (code);
7736 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7737 && TREE_CODE_LENGTH (code) == 1);
7739 arg0 = op0;
7740 if (arg0)
7742 if (CONVERT_EXPR_CODE_P (code)
7743 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7745 /* Don't use STRIP_NOPS, because signedness of argument type
7746 matters. */
7747 STRIP_SIGN_NOPS (arg0);
7749 else
7751 /* Strip any conversions that don't change the mode. This
7752 is safe for every expression, except for a comparison
7753 expression because its signedness is derived from its
7754 operands.
7756 Note that this is done as an internal manipulation within
7757 the constant folder, in order to find the simplest
7758 representation of the arguments so that their form can be
7759 studied. In any cases, the appropriate type conversions
7760 should be put back in the tree that will get out of the
7761 constant folder. */
7762 STRIP_NOPS (arg0);
7765 if (CONSTANT_CLASS_P (arg0))
7767 tree tem = const_unop (code, type, arg0);
7768 if (tem)
7770 if (TREE_TYPE (tem) != type)
7771 tem = fold_convert_loc (loc, type, tem);
7772 return tem;
7777 tem = generic_simplify (loc, code, type, op0);
7778 if (tem)
7779 return tem;
7781 if (TREE_CODE_CLASS (code) == tcc_unary)
7783 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7784 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7785 fold_build1_loc (loc, code, type,
7786 fold_convert_loc (loc, TREE_TYPE (op0),
7787 TREE_OPERAND (arg0, 1))));
7788 else if (TREE_CODE (arg0) == COND_EXPR)
7790 tree arg01 = TREE_OPERAND (arg0, 1);
7791 tree arg02 = TREE_OPERAND (arg0, 2);
7792 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7793 arg01 = fold_build1_loc (loc, code, type,
7794 fold_convert_loc (loc,
7795 TREE_TYPE (op0), arg01));
7796 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7797 arg02 = fold_build1_loc (loc, code, type,
7798 fold_convert_loc (loc,
7799 TREE_TYPE (op0), arg02));
7800 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7801 arg01, arg02);
7803 /* If this was a conversion, and all we did was to move into
7804 inside the COND_EXPR, bring it back out. But leave it if
7805 it is a conversion from integer to integer and the
7806 result precision is no wider than a word since such a
7807 conversion is cheap and may be optimized away by combine,
7808 while it couldn't if it were outside the COND_EXPR. Then return
7809 so we don't get into an infinite recursion loop taking the
7810 conversion out and then back in. */
7812 if ((CONVERT_EXPR_CODE_P (code)
7813 || code == NON_LVALUE_EXPR)
7814 && TREE_CODE (tem) == COND_EXPR
7815 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7816 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7817 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7818 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7819 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7820 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7821 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7822 && (INTEGRAL_TYPE_P
7823 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7824 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7825 || flag_syntax_only))
7826 tem = build1_loc (loc, code, type,
7827 build3 (COND_EXPR,
7828 TREE_TYPE (TREE_OPERAND
7829 (TREE_OPERAND (tem, 1), 0)),
7830 TREE_OPERAND (tem, 0),
7831 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7832 TREE_OPERAND (TREE_OPERAND (tem, 2),
7833 0)));
7834 return tem;
7838 switch (code)
7840 case NON_LVALUE_EXPR:
7841 if (!maybe_lvalue_p (op0))
7842 return fold_convert_loc (loc, type, op0);
7843 return NULL_TREE;
7845 CASE_CONVERT:
7846 case FLOAT_EXPR:
7847 case FIX_TRUNC_EXPR:
7848 if (COMPARISON_CLASS_P (op0))
7850 /* If we have (type) (a CMP b) and type is an integral type, return
7851 new expression involving the new type. Canonicalize
7852 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7853 non-integral type.
7854 Do not fold the result as that would not simplify further, also
7855 folding again results in recursions. */
7856 if (TREE_CODE (type) == BOOLEAN_TYPE)
7857 return build2_loc (loc, TREE_CODE (op0), type,
7858 TREE_OPERAND (op0, 0),
7859 TREE_OPERAND (op0, 1));
7860 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7861 && TREE_CODE (type) != VECTOR_TYPE)
7862 return build3_loc (loc, COND_EXPR, type, op0,
7863 constant_boolean_node (true, type),
7864 constant_boolean_node (false, type));
7867 /* Handle (T *)&A.B.C for A being of type T and B and C
7868 living at offset zero. This occurs frequently in
7869 C++ upcasting and then accessing the base. */
7870 if (TREE_CODE (op0) == ADDR_EXPR
7871 && POINTER_TYPE_P (type)
7872 && handled_component_p (TREE_OPERAND (op0, 0)))
7874 poly_int64 bitsize, bitpos;
7875 tree offset;
7876 machine_mode mode;
7877 int unsignedp, reversep, volatilep;
7878 tree base
7879 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7880 &offset, &mode, &unsignedp, &reversep,
7881 &volatilep);
7882 /* If the reference was to a (constant) zero offset, we can use
7883 the address of the base if it has the same base type
7884 as the result type and the pointer type is unqualified. */
7885 if (!offset
7886 && known_eq (bitpos, 0)
7887 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7888 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7889 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7890 return fold_convert_loc (loc, type,
7891 build_fold_addr_expr_loc (loc, base));
7894 if (TREE_CODE (op0) == MODIFY_EXPR
7895 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7896 /* Detect assigning a bitfield. */
7897 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7898 && DECL_BIT_FIELD
7899 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7901 /* Don't leave an assignment inside a conversion
7902 unless assigning a bitfield. */
7903 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7904 /* First do the assignment, then return converted constant. */
7905 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7906 TREE_NO_WARNING (tem) = 1;
7907 TREE_USED (tem) = 1;
7908 return tem;
7911 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7912 constants (if x has signed type, the sign bit cannot be set
7913 in c). This folds extension into the BIT_AND_EXPR.
7914 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7915 very likely don't have maximal range for their precision and this
7916 transformation effectively doesn't preserve non-maximal ranges. */
7917 if (TREE_CODE (type) == INTEGER_TYPE
7918 && TREE_CODE (op0) == BIT_AND_EXPR
7919 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7921 tree and_expr = op0;
7922 tree and0 = TREE_OPERAND (and_expr, 0);
7923 tree and1 = TREE_OPERAND (and_expr, 1);
7924 int change = 0;
7926 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7927 || (TYPE_PRECISION (type)
7928 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7929 change = 1;
7930 else if (TYPE_PRECISION (TREE_TYPE (and1))
7931 <= HOST_BITS_PER_WIDE_INT
7932 && tree_fits_uhwi_p (and1))
7934 unsigned HOST_WIDE_INT cst;
7936 cst = tree_to_uhwi (and1);
7937 cst &= HOST_WIDE_INT_M1U
7938 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7939 change = (cst == 0);
7940 if (change
7941 && !flag_syntax_only
7942 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7943 == ZERO_EXTEND))
7945 tree uns = unsigned_type_for (TREE_TYPE (and0));
7946 and0 = fold_convert_loc (loc, uns, and0);
7947 and1 = fold_convert_loc (loc, uns, and1);
7950 if (change)
7952 tem = force_fit_type (type, wi::to_widest (and1), 0,
7953 TREE_OVERFLOW (and1));
7954 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7955 fold_convert_loc (loc, type, and0), tem);
7959 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7960 cast (T1)X will fold away. We assume that this happens when X itself
7961 is a cast. */
7962 if (POINTER_TYPE_P (type)
7963 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7964 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7966 tree arg00 = TREE_OPERAND (arg0, 0);
7967 tree arg01 = TREE_OPERAND (arg0, 1);
7969 return fold_build_pointer_plus_loc
7970 (loc, fold_convert_loc (loc, type, arg00), arg01);
7973 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7974 of the same precision, and X is an integer type not narrower than
7975 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7976 if (INTEGRAL_TYPE_P (type)
7977 && TREE_CODE (op0) == BIT_NOT_EXPR
7978 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7979 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7980 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7982 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7983 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7984 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7985 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7986 fold_convert_loc (loc, type, tem));
7989 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7990 type of X and Y (integer types only). */
7991 if (INTEGRAL_TYPE_P (type)
7992 && TREE_CODE (op0) == MULT_EXPR
7993 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7994 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7996 /* Be careful not to introduce new overflows. */
7997 tree mult_type;
7998 if (TYPE_OVERFLOW_WRAPS (type))
7999 mult_type = type;
8000 else
8001 mult_type = unsigned_type_for (type);
8003 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8005 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8006 fold_convert_loc (loc, mult_type,
8007 TREE_OPERAND (op0, 0)),
8008 fold_convert_loc (loc, mult_type,
8009 TREE_OPERAND (op0, 1)));
8010 return fold_convert_loc (loc, type, tem);
8014 return NULL_TREE;
8016 case VIEW_CONVERT_EXPR:
8017 if (TREE_CODE (op0) == MEM_REF)
8019 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
8020 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
8021 tem = fold_build2_loc (loc, MEM_REF, type,
8022 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8023 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
8024 return tem;
8027 return NULL_TREE;
8029 case NEGATE_EXPR:
8030 tem = fold_negate_expr (loc, arg0);
8031 if (tem)
8032 return fold_convert_loc (loc, type, tem);
8033 return NULL_TREE;
8035 case ABS_EXPR:
8036 /* Convert fabs((double)float) into (double)fabsf(float). */
8037 if (TREE_CODE (arg0) == NOP_EXPR
8038 && TREE_CODE (type) == REAL_TYPE)
8040 tree targ0 = strip_float_extensions (arg0);
8041 if (targ0 != arg0)
8042 return fold_convert_loc (loc, type,
8043 fold_build1_loc (loc, ABS_EXPR,
8044 TREE_TYPE (targ0),
8045 targ0));
8047 return NULL_TREE;
8049 case BIT_NOT_EXPR:
8050 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8051 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8052 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8053 fold_convert_loc (loc, type,
8054 TREE_OPERAND (arg0, 0)))))
8055 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8056 fold_convert_loc (loc, type,
8057 TREE_OPERAND (arg0, 1)));
8058 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8059 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8060 fold_convert_loc (loc, type,
8061 TREE_OPERAND (arg0, 1)))))
8062 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8063 fold_convert_loc (loc, type,
8064 TREE_OPERAND (arg0, 0)), tem);
8066 return NULL_TREE;
8068 case TRUTH_NOT_EXPR:
8069 /* Note that the operand of this must be an int
8070 and its values must be 0 or 1.
8071 ("true" is a fixed value perhaps depending on the language,
8072 but we don't handle values other than 1 correctly yet.) */
8073 tem = fold_truth_not_expr (loc, arg0);
8074 if (!tem)
8075 return NULL_TREE;
8076 return fold_convert_loc (loc, type, tem);
8078 case INDIRECT_REF:
8079 /* Fold *&X to X if X is an lvalue. */
8080 if (TREE_CODE (op0) == ADDR_EXPR)
8082 tree op00 = TREE_OPERAND (op0, 0);
8083 if ((VAR_P (op00)
8084 || TREE_CODE (op00) == PARM_DECL
8085 || TREE_CODE (op00) == RESULT_DECL)
8086 && !TREE_READONLY (op00))
8087 return op00;
8089 return NULL_TREE;
8091 default:
8092 return NULL_TREE;
8093 } /* switch (code) */
8097 /* If the operation was a conversion do _not_ mark a resulting constant
8098 with TREE_OVERFLOW if the original constant was not. These conversions
8099 have implementation defined behavior and retaining the TREE_OVERFLOW
8100 flag here would confuse later passes such as VRP. */
8101 tree
8102 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8103 tree type, tree op0)
8105 tree res = fold_unary_loc (loc, code, type, op0);
8106 if (res
8107 && TREE_CODE (res) == INTEGER_CST
8108 && TREE_CODE (op0) == INTEGER_CST
8109 && CONVERT_EXPR_CODE_P (code))
8110 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8112 return res;
8115 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8116 operands OP0 and OP1. LOC is the location of the resulting expression.
8117 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8118 Return the folded expression if folding is successful. Otherwise,
8119 return NULL_TREE. */
8120 static tree
8121 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8122 tree arg0, tree arg1, tree op0, tree op1)
8124 tree tem;
8126 /* We only do these simplifications if we are optimizing. */
8127 if (!optimize)
8128 return NULL_TREE;
8130 /* Check for things like (A || B) && (A || C). We can convert this
8131 to A || (B && C). Note that either operator can be any of the four
8132 truth and/or operations and the transformation will still be
8133 valid. Also note that we only care about order for the
8134 ANDIF and ORIF operators. If B contains side effects, this
8135 might change the truth-value of A. */
8136 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8137 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8138 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8139 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8140 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8141 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8143 tree a00 = TREE_OPERAND (arg0, 0);
8144 tree a01 = TREE_OPERAND (arg0, 1);
8145 tree a10 = TREE_OPERAND (arg1, 0);
8146 tree a11 = TREE_OPERAND (arg1, 1);
8147 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8148 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8149 && (code == TRUTH_AND_EXPR
8150 || code == TRUTH_OR_EXPR));
8152 if (operand_equal_p (a00, a10, 0))
8153 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8154 fold_build2_loc (loc, code, type, a01, a11));
8155 else if (commutative && operand_equal_p (a00, a11, 0))
8156 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8157 fold_build2_loc (loc, code, type, a01, a10));
8158 else if (commutative && operand_equal_p (a01, a10, 0))
8159 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8160 fold_build2_loc (loc, code, type, a00, a11));
8162 /* This case if tricky because we must either have commutative
8163 operators or else A10 must not have side-effects. */
8165 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8166 && operand_equal_p (a01, a11, 0))
8167 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8168 fold_build2_loc (loc, code, type, a00, a10),
8169 a01);
8172 /* See if we can build a range comparison. */
8173 if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
8174 return tem;
8176 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8177 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8179 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8180 if (tem)
8181 return fold_build2_loc (loc, code, type, tem, arg1);
8184 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8185 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8187 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8188 if (tem)
8189 return fold_build2_loc (loc, code, type, arg0, tem);
8192 /* Check for the possibility of merging component references. If our
8193 lhs is another similar operation, try to merge its rhs with our
8194 rhs. Then try to merge our lhs and rhs. */
8195 if (TREE_CODE (arg0) == code
8196 && (tem = fold_truth_andor_1 (loc, code, type,
8197 TREE_OPERAND (arg0, 1), arg1)) != 0)
8198 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8200 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8201 return tem;
8203 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8204 && !flag_sanitize_coverage
8205 && (code == TRUTH_AND_EXPR
8206 || code == TRUTH_ANDIF_EXPR
8207 || code == TRUTH_OR_EXPR
8208 || code == TRUTH_ORIF_EXPR))
8210 enum tree_code ncode, icode;
8212 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8213 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8214 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8216 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8217 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8218 We don't want to pack more than two leafs to a non-IF AND/OR
8219 expression.
8220 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8221 equal to IF-CODE, then we don't want to add right-hand operand.
8222 If the inner right-hand side of left-hand operand has
8223 side-effects, or isn't simple, then we can't add to it,
8224 as otherwise we might destroy if-sequence. */
8225 if (TREE_CODE (arg0) == icode
8226 && simple_operand_p_2 (arg1)
8227 /* Needed for sequence points to handle trappings, and
8228 side-effects. */
8229 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8231 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8232 arg1);
8233 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8234 tem);
8236 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8237 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8238 else if (TREE_CODE (arg1) == icode
8239 && simple_operand_p_2 (arg0)
8240 /* Needed for sequence points to handle trappings, and
8241 side-effects. */
8242 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8244 tem = fold_build2_loc (loc, ncode, type,
8245 arg0, TREE_OPERAND (arg1, 0));
8246 return fold_build2_loc (loc, icode, type, tem,
8247 TREE_OPERAND (arg1, 1));
8249 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8250 into (A OR B).
8251 For sequence point consistancy, we need to check for trapping,
8252 and side-effects. */
8253 else if (code == icode && simple_operand_p_2 (arg0)
8254 && simple_operand_p_2 (arg1))
8255 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8258 return NULL_TREE;
8261 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8262 by changing CODE to reduce the magnitude of constants involved in
8263 ARG0 of the comparison.
8264 Returns a canonicalized comparison tree if a simplification was
8265 possible, otherwise returns NULL_TREE.
8266 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8267 valid if signed overflow is undefined. */
8269 static tree
8270 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8271 tree arg0, tree arg1,
8272 bool *strict_overflow_p)
8274 enum tree_code code0 = TREE_CODE (arg0);
8275 tree t, cst0 = NULL_TREE;
8276 int sgn0;
8278 /* Match A +- CST code arg1. We can change this only if overflow
8279 is undefined. */
8280 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8281 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8282 /* In principle pointers also have undefined overflow behavior,
8283 but that causes problems elsewhere. */
8284 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8285 && (code0 == MINUS_EXPR
8286 || code0 == PLUS_EXPR)
8287 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8288 return NULL_TREE;
8290 /* Identify the constant in arg0 and its sign. */
8291 cst0 = TREE_OPERAND (arg0, 1);
8292 sgn0 = tree_int_cst_sgn (cst0);
8294 /* Overflowed constants and zero will cause problems. */
8295 if (integer_zerop (cst0)
8296 || TREE_OVERFLOW (cst0))
8297 return NULL_TREE;
8299 /* See if we can reduce the magnitude of the constant in
8300 arg0 by changing the comparison code. */
8301 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8302 if (code == LT_EXPR
8303 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8304 code = LE_EXPR;
8305 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8306 else if (code == GT_EXPR
8307 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8308 code = GE_EXPR;
8309 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8310 else if (code == LE_EXPR
8311 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8312 code = LT_EXPR;
8313 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8314 else if (code == GE_EXPR
8315 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8316 code = GT_EXPR;
8317 else
8318 return NULL_TREE;
8319 *strict_overflow_p = true;
8321 /* Now build the constant reduced in magnitude. But not if that
8322 would produce one outside of its types range. */
8323 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8324 && ((sgn0 == 1
8325 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8326 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8327 || (sgn0 == -1
8328 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8329 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8330 return NULL_TREE;
8332 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8333 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8334 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8335 t = fold_convert (TREE_TYPE (arg1), t);
8337 return fold_build2_loc (loc, code, type, t, arg1);
8340 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8341 overflow further. Try to decrease the magnitude of constants involved
8342 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8343 and put sole constants at the second argument position.
8344 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8346 static tree
8347 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8348 tree arg0, tree arg1)
8350 tree t;
8351 bool strict_overflow_p;
8352 const char * const warnmsg = G_("assuming signed overflow does not occur "
8353 "when reducing constant in comparison");
8355 /* Try canonicalization by simplifying arg0. */
8356 strict_overflow_p = false;
8357 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8358 &strict_overflow_p);
8359 if (t)
8361 if (strict_overflow_p)
8362 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8363 return t;
8366 /* Try canonicalization by simplifying arg1 using the swapped
8367 comparison. */
8368 code = swap_tree_comparison (code);
8369 strict_overflow_p = false;
8370 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8371 &strict_overflow_p);
8372 if (t && strict_overflow_p)
8373 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8374 return t;
8377 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8378 space. This is used to avoid issuing overflow warnings for
8379 expressions like &p->x which can not wrap. */
8381 static bool
8382 pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
8384 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8385 return true;
8387 if (maybe_lt (bitpos, 0))
8388 return true;
8390 poly_wide_int wi_offset;
8391 int precision = TYPE_PRECISION (TREE_TYPE (base));
8392 if (offset == NULL_TREE)
8393 wi_offset = wi::zero (precision);
8394 else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
8395 return true;
8396 else
8397 wi_offset = wi::to_poly_wide (offset);
8399 wi::overflow_type overflow;
8400 poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
8401 precision);
8402 poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8403 if (overflow)
8404 return true;
8406 poly_uint64 total_hwi, size;
8407 if (!total.to_uhwi (&total_hwi)
8408 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
8409 &size)
8410 || known_eq (size, 0U))
8411 return true;
8413 if (known_le (total_hwi, size))
8414 return false;
8416 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8417 array. */
8418 if (TREE_CODE (base) == ADDR_EXPR
8419 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
8420 &size)
8421 && maybe_ne (size, 0U)
8422 && known_le (total_hwi, size))
8423 return false;
8425 return true;
8428 /* Return a positive integer when the symbol DECL is known to have
8429 a nonzero address, zero when it's known not to (e.g., it's a weak
8430 symbol), and a negative integer when the symbol is not yet in the
8431 symbol table and so whether or not its address is zero is unknown.
8432 For function local objects always return positive integer. */
8433 static int
8434 maybe_nonzero_address (tree decl)
8436 if (DECL_P (decl) && decl_in_symtab_p (decl))
8437 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8438 return symbol->nonzero_address ();
8440 /* Function local objects are never NULL. */
8441 if (DECL_P (decl)
8442 && (DECL_CONTEXT (decl)
8443 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8444 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8445 return 1;
8447 return -1;
8450 /* Subroutine of fold_binary. This routine performs all of the
8451 transformations that are common to the equality/inequality
8452 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8453 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8454 fold_binary should call fold_binary. Fold a comparison with
8455 tree code CODE and type TYPE with operands OP0 and OP1. Return
8456 the folded comparison or NULL_TREE. */
8458 static tree
8459 fold_comparison (location_t loc, enum tree_code code, tree type,
8460 tree op0, tree op1)
8462 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8463 tree arg0, arg1, tem;
8465 arg0 = op0;
8466 arg1 = op1;
8468 STRIP_SIGN_NOPS (arg0);
8469 STRIP_SIGN_NOPS (arg1);
8471 /* For comparisons of pointers we can decompose it to a compile time
8472 comparison of the base objects and the offsets into the object.
8473 This requires at least one operand being an ADDR_EXPR or a
8474 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8475 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8476 && (TREE_CODE (arg0) == ADDR_EXPR
8477 || TREE_CODE (arg1) == ADDR_EXPR
8478 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8479 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8481 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8482 poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
8483 machine_mode mode;
8484 int volatilep, reversep, unsignedp;
8485 bool indirect_base0 = false, indirect_base1 = false;
8487 /* Get base and offset for the access. Strip ADDR_EXPR for
8488 get_inner_reference, but put it back by stripping INDIRECT_REF
8489 off the base object if possible. indirect_baseN will be true
8490 if baseN is not an address but refers to the object itself. */
8491 base0 = arg0;
8492 if (TREE_CODE (arg0) == ADDR_EXPR)
8494 base0
8495 = get_inner_reference (TREE_OPERAND (arg0, 0),
8496 &bitsize, &bitpos0, &offset0, &mode,
8497 &unsignedp, &reversep, &volatilep);
8498 if (TREE_CODE (base0) == INDIRECT_REF)
8499 base0 = TREE_OPERAND (base0, 0);
8500 else
8501 indirect_base0 = true;
8503 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8505 base0 = TREE_OPERAND (arg0, 0);
8506 STRIP_SIGN_NOPS (base0);
8507 if (TREE_CODE (base0) == ADDR_EXPR)
8509 base0
8510 = get_inner_reference (TREE_OPERAND (base0, 0),
8511 &bitsize, &bitpos0, &offset0, &mode,
8512 &unsignedp, &reversep, &volatilep);
8513 if (TREE_CODE (base0) == INDIRECT_REF)
8514 base0 = TREE_OPERAND (base0, 0);
8515 else
8516 indirect_base0 = true;
8518 if (offset0 == NULL_TREE || integer_zerop (offset0))
8519 offset0 = TREE_OPERAND (arg0, 1);
8520 else
8521 offset0 = size_binop (PLUS_EXPR, offset0,
8522 TREE_OPERAND (arg0, 1));
8523 if (poly_int_tree_p (offset0))
8525 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
8526 TYPE_PRECISION (sizetype));
8527 tem <<= LOG2_BITS_PER_UNIT;
8528 tem += bitpos0;
8529 if (tem.to_shwi (&bitpos0))
8530 offset0 = NULL_TREE;
8534 base1 = arg1;
8535 if (TREE_CODE (arg1) == ADDR_EXPR)
8537 base1
8538 = get_inner_reference (TREE_OPERAND (arg1, 0),
8539 &bitsize, &bitpos1, &offset1, &mode,
8540 &unsignedp, &reversep, &volatilep);
8541 if (TREE_CODE (base1) == INDIRECT_REF)
8542 base1 = TREE_OPERAND (base1, 0);
8543 else
8544 indirect_base1 = true;
8546 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8548 base1 = TREE_OPERAND (arg1, 0);
8549 STRIP_SIGN_NOPS (base1);
8550 if (TREE_CODE (base1) == ADDR_EXPR)
8552 base1
8553 = get_inner_reference (TREE_OPERAND (base1, 0),
8554 &bitsize, &bitpos1, &offset1, &mode,
8555 &unsignedp, &reversep, &volatilep);
8556 if (TREE_CODE (base1) == INDIRECT_REF)
8557 base1 = TREE_OPERAND (base1, 0);
8558 else
8559 indirect_base1 = true;
8561 if (offset1 == NULL_TREE || integer_zerop (offset1))
8562 offset1 = TREE_OPERAND (arg1, 1);
8563 else
8564 offset1 = size_binop (PLUS_EXPR, offset1,
8565 TREE_OPERAND (arg1, 1));
8566 if (poly_int_tree_p (offset1))
8568 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
8569 TYPE_PRECISION (sizetype));
8570 tem <<= LOG2_BITS_PER_UNIT;
8571 tem += bitpos1;
8572 if (tem.to_shwi (&bitpos1))
8573 offset1 = NULL_TREE;
8577 /* If we have equivalent bases we might be able to simplify. */
8578 if (indirect_base0 == indirect_base1
8579 && operand_equal_p (base0, base1,
8580 indirect_base0 ? OEP_ADDRESS_OF : 0))
8582 /* We can fold this expression to a constant if the non-constant
8583 offset parts are equal. */
8584 if ((offset0 == offset1
8585 || (offset0 && offset1
8586 && operand_equal_p (offset0, offset1, 0)))
8587 && (equality_code
8588 || (indirect_base0
8589 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8590 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8592 if (!equality_code
8593 && maybe_ne (bitpos0, bitpos1)
8594 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8595 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8596 fold_overflow_warning (("assuming pointer wraparound does not "
8597 "occur when comparing P +- C1 with "
8598 "P +- C2"),
8599 WARN_STRICT_OVERFLOW_CONDITIONAL);
8601 switch (code)
8603 case EQ_EXPR:
8604 if (known_eq (bitpos0, bitpos1))
8605 return constant_boolean_node (true, type);
8606 if (known_ne (bitpos0, bitpos1))
8607 return constant_boolean_node (false, type);
8608 break;
8609 case NE_EXPR:
8610 if (known_ne (bitpos0, bitpos1))
8611 return constant_boolean_node (true, type);
8612 if (known_eq (bitpos0, bitpos1))
8613 return constant_boolean_node (false, type);
8614 break;
8615 case LT_EXPR:
8616 if (known_lt (bitpos0, bitpos1))
8617 return constant_boolean_node (true, type);
8618 if (known_ge (bitpos0, bitpos1))
8619 return constant_boolean_node (false, type);
8620 break;
8621 case LE_EXPR:
8622 if (known_le (bitpos0, bitpos1))
8623 return constant_boolean_node (true, type);
8624 if (known_gt (bitpos0, bitpos1))
8625 return constant_boolean_node (false, type);
8626 break;
8627 case GE_EXPR:
8628 if (known_ge (bitpos0, bitpos1))
8629 return constant_boolean_node (true, type);
8630 if (known_lt (bitpos0, bitpos1))
8631 return constant_boolean_node (false, type);
8632 break;
8633 case GT_EXPR:
8634 if (known_gt (bitpos0, bitpos1))
8635 return constant_boolean_node (true, type);
8636 if (known_le (bitpos0, bitpos1))
8637 return constant_boolean_node (false, type);
8638 break;
8639 default:;
8642 /* We can simplify the comparison to a comparison of the variable
8643 offset parts if the constant offset parts are equal.
8644 Be careful to use signed sizetype here because otherwise we
8645 mess with array offsets in the wrong way. This is possible
8646 because pointer arithmetic is restricted to retain within an
8647 object and overflow on pointer differences is undefined as of
8648 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8649 else if (known_eq (bitpos0, bitpos1)
8650 && (equality_code
8651 || (indirect_base0
8652 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8653 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8655 /* By converting to signed sizetype we cover middle-end pointer
8656 arithmetic which operates on unsigned pointer types of size
8657 type size and ARRAY_REF offsets which are properly sign or
8658 zero extended from their type in case it is narrower than
8659 sizetype. */
8660 if (offset0 == NULL_TREE)
8661 offset0 = build_int_cst (ssizetype, 0);
8662 else
8663 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8664 if (offset1 == NULL_TREE)
8665 offset1 = build_int_cst (ssizetype, 0);
8666 else
8667 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8669 if (!equality_code
8670 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8671 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8672 fold_overflow_warning (("assuming pointer wraparound does not "
8673 "occur when comparing P +- C1 with "
8674 "P +- C2"),
8675 WARN_STRICT_OVERFLOW_COMPARISON);
8677 return fold_build2_loc (loc, code, type, offset0, offset1);
8680 /* For equal offsets we can simplify to a comparison of the
8681 base addresses. */
8682 else if (known_eq (bitpos0, bitpos1)
8683 && (indirect_base0
8684 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8685 && (indirect_base1
8686 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8687 && ((offset0 == offset1)
8688 || (offset0 && offset1
8689 && operand_equal_p (offset0, offset1, 0))))
8691 if (indirect_base0)
8692 base0 = build_fold_addr_expr_loc (loc, base0);
8693 if (indirect_base1)
8694 base1 = build_fold_addr_expr_loc (loc, base1);
8695 return fold_build2_loc (loc, code, type, base0, base1);
8697 /* Comparison between an ordinary (non-weak) symbol and a null
8698 pointer can be eliminated since such symbols must have a non
8699 null address. In C, relational expressions between pointers
8700 to objects and null pointers are undefined. The results
8701 below follow the C++ rules with the additional property that
8702 every object pointer compares greater than a null pointer.
8704 else if (((DECL_P (base0)
8705 && maybe_nonzero_address (base0) > 0
8706 /* Avoid folding references to struct members at offset 0 to
8707 prevent tests like '&ptr->firstmember == 0' from getting
8708 eliminated. When ptr is null, although the -> expression
8709 is strictly speaking invalid, GCC retains it as a matter
8710 of QoI. See PR c/44555. */
8711 && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
8712 || CONSTANT_CLASS_P (base0))
8713 && indirect_base0
8714 /* The caller guarantees that when one of the arguments is
8715 constant (i.e., null in this case) it is second. */
8716 && integer_zerop (arg1))
8718 switch (code)
8720 case EQ_EXPR:
8721 case LE_EXPR:
8722 case LT_EXPR:
8723 return constant_boolean_node (false, type);
8724 case GE_EXPR:
8725 case GT_EXPR:
8726 case NE_EXPR:
8727 return constant_boolean_node (true, type);
8728 default:
8729 gcc_unreachable ();
8734 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8735 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8736 the resulting offset is smaller in absolute value than the
8737 original one and has the same sign. */
8738 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8739 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8740 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8741 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8742 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8743 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8744 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8745 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8747 tree const1 = TREE_OPERAND (arg0, 1);
8748 tree const2 = TREE_OPERAND (arg1, 1);
8749 tree variable1 = TREE_OPERAND (arg0, 0);
8750 tree variable2 = TREE_OPERAND (arg1, 0);
8751 tree cst;
8752 const char * const warnmsg = G_("assuming signed overflow does not "
8753 "occur when combining constants around "
8754 "a comparison");
8756 /* Put the constant on the side where it doesn't overflow and is
8757 of lower absolute value and of same sign than before. */
8758 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8759 ? MINUS_EXPR : PLUS_EXPR,
8760 const2, const1);
8761 if (!TREE_OVERFLOW (cst)
8762 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8763 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8765 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8766 return fold_build2_loc (loc, code, type,
8767 variable1,
8768 fold_build2_loc (loc, TREE_CODE (arg1),
8769 TREE_TYPE (arg1),
8770 variable2, cst));
8773 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8774 ? MINUS_EXPR : PLUS_EXPR,
8775 const1, const2);
8776 if (!TREE_OVERFLOW (cst)
8777 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8778 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8780 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8781 return fold_build2_loc (loc, code, type,
8782 fold_build2_loc (loc, TREE_CODE (arg0),
8783 TREE_TYPE (arg0),
8784 variable1, cst),
8785 variable2);
8789 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8790 if (tem)
8791 return tem;
8793 /* If we are comparing an expression that just has comparisons
8794 of two integer values, arithmetic expressions of those comparisons,
8795 and constants, we can simplify it. There are only three cases
8796 to check: the two values can either be equal, the first can be
8797 greater, or the second can be greater. Fold the expression for
8798 those three values. Since each value must be 0 or 1, we have
8799 eight possibilities, each of which corresponds to the constant 0
8800 or 1 or one of the six possible comparisons.
8802 This handles common cases like (a > b) == 0 but also handles
8803 expressions like ((x > y) - (y > x)) > 0, which supposedly
8804 occur in macroized code. */
8806 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8808 tree cval1 = 0, cval2 = 0;
8810 if (twoval_comparison_p (arg0, &cval1, &cval2)
8811 /* Don't handle degenerate cases here; they should already
8812 have been handled anyway. */
8813 && cval1 != 0 && cval2 != 0
8814 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8815 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8816 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8817 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8818 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8819 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8820 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8822 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8823 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8825 /* We can't just pass T to eval_subst in case cval1 or cval2
8826 was the same as ARG1. */
8828 tree high_result
8829 = fold_build2_loc (loc, code, type,
8830 eval_subst (loc, arg0, cval1, maxval,
8831 cval2, minval),
8832 arg1);
8833 tree equal_result
8834 = fold_build2_loc (loc, code, type,
8835 eval_subst (loc, arg0, cval1, maxval,
8836 cval2, maxval),
8837 arg1);
8838 tree low_result
8839 = fold_build2_loc (loc, code, type,
8840 eval_subst (loc, arg0, cval1, minval,
8841 cval2, maxval),
8842 arg1);
8844 /* All three of these results should be 0 or 1. Confirm they are.
8845 Then use those values to select the proper code to use. */
8847 if (TREE_CODE (high_result) == INTEGER_CST
8848 && TREE_CODE (equal_result) == INTEGER_CST
8849 && TREE_CODE (low_result) == INTEGER_CST)
8851 /* Make a 3-bit mask with the high-order bit being the
8852 value for `>', the next for '=', and the low for '<'. */
8853 switch ((integer_onep (high_result) * 4)
8854 + (integer_onep (equal_result) * 2)
8855 + integer_onep (low_result))
8857 case 0:
8858 /* Always false. */
8859 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8860 case 1:
8861 code = LT_EXPR;
8862 break;
8863 case 2:
8864 code = EQ_EXPR;
8865 break;
8866 case 3:
8867 code = LE_EXPR;
8868 break;
8869 case 4:
8870 code = GT_EXPR;
8871 break;
8872 case 5:
8873 code = NE_EXPR;
8874 break;
8875 case 6:
8876 code = GE_EXPR;
8877 break;
8878 case 7:
8879 /* Always true. */
8880 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8883 return fold_build2_loc (loc, code, type, cval1, cval2);
8888 return NULL_TREE;
8892 /* Subroutine of fold_binary. Optimize complex multiplications of the
8893 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8894 argument EXPR represents the expression "z" of type TYPE. */
8896 static tree
8897 fold_mult_zconjz (location_t loc, tree type, tree expr)
8899 tree itype = TREE_TYPE (type);
8900 tree rpart, ipart, tem;
8902 if (TREE_CODE (expr) == COMPLEX_EXPR)
8904 rpart = TREE_OPERAND (expr, 0);
8905 ipart = TREE_OPERAND (expr, 1);
8907 else if (TREE_CODE (expr) == COMPLEX_CST)
8909 rpart = TREE_REALPART (expr);
8910 ipart = TREE_IMAGPART (expr);
8912 else
8914 expr = save_expr (expr);
8915 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8916 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8919 rpart = save_expr (rpart);
8920 ipart = save_expr (ipart);
8921 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8922 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8923 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8924 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8925 build_zero_cst (itype));
8929 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8930 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8931 true if successful. */
8933 static bool
8934 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
8936 unsigned HOST_WIDE_INT i, nunits;
8938 if (TREE_CODE (arg) == VECTOR_CST
8939 && VECTOR_CST_NELTS (arg).is_constant (&nunits))
8941 for (i = 0; i < nunits; ++i)
8942 elts[i] = VECTOR_CST_ELT (arg, i);
8944 else if (TREE_CODE (arg) == CONSTRUCTOR)
8946 constructor_elt *elt;
8948 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8949 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8950 return false;
8951 else
8952 elts[i] = elt->value;
8954 else
8955 return false;
8956 for (; i < nelts; i++)
8957 elts[i]
8958 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8959 return true;
8962 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8963 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8964 NULL_TREE otherwise. */
8966 static tree
8967 fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
8969 unsigned int i;
8970 unsigned HOST_WIDE_INT nelts;
8971 bool need_ctor = false;
8973 if (!sel.length ().is_constant (&nelts))
8974 return NULL_TREE;
8975 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), nelts)
8976 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)), nelts)
8977 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)), nelts));
8978 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8979 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8980 return NULL_TREE;
8982 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
8983 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
8984 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
8985 return NULL_TREE;
8987 tree_vector_builder out_elts (type, nelts, 1);
8988 for (i = 0; i < nelts; i++)
8990 HOST_WIDE_INT index;
8991 if (!sel[i].is_constant (&index))
8992 return NULL_TREE;
8993 if (!CONSTANT_CLASS_P (in_elts[index]))
8994 need_ctor = true;
8995 out_elts.quick_push (unshare_expr (in_elts[index]));
8998 if (need_ctor)
9000 vec<constructor_elt, va_gc> *v;
9001 vec_alloc (v, nelts);
9002 for (i = 0; i < nelts; i++)
9003 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
9004 return build_constructor (type, v);
9006 else
9007 return out_elts.build ();
9010 /* Try to fold a pointer difference of type TYPE two address expressions of
9011 array references AREF0 and AREF1 using location LOC. Return a
9012 simplified expression for the difference or NULL_TREE. */
9014 static tree
9015 fold_addr_of_array_ref_difference (location_t loc, tree type,
9016 tree aref0, tree aref1,
9017 bool use_pointer_diff)
9019 tree base0 = TREE_OPERAND (aref0, 0);
9020 tree base1 = TREE_OPERAND (aref1, 0);
9021 tree base_offset = build_int_cst (type, 0);
9023 /* If the bases are array references as well, recurse. If the bases
9024 are pointer indirections compute the difference of the pointers.
9025 If the bases are equal, we are set. */
9026 if ((TREE_CODE (base0) == ARRAY_REF
9027 && TREE_CODE (base1) == ARRAY_REF
9028 && (base_offset
9029 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
9030 use_pointer_diff)))
9031 || (INDIRECT_REF_P (base0)
9032 && INDIRECT_REF_P (base1)
9033 && (base_offset
9034 = use_pointer_diff
9035 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
9036 TREE_OPERAND (base0, 0),
9037 TREE_OPERAND (base1, 0))
9038 : fold_binary_loc (loc, MINUS_EXPR, type,
9039 fold_convert (type,
9040 TREE_OPERAND (base0, 0)),
9041 fold_convert (type,
9042 TREE_OPERAND (base1, 0)))))
9043 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
9045 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9046 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9047 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9048 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
9049 return fold_build2_loc (loc, PLUS_EXPR, type,
9050 base_offset,
9051 fold_build2_loc (loc, MULT_EXPR, type,
9052 diff, esz));
9054 return NULL_TREE;
9057 /* If the real or vector real constant CST of type TYPE has an exact
9058 inverse, return it, else return NULL. */
9060 tree
9061 exact_inverse (tree type, tree cst)
9063 REAL_VALUE_TYPE r;
9064 tree unit_type;
9065 machine_mode mode;
9067 switch (TREE_CODE (cst))
9069 case REAL_CST:
9070 r = TREE_REAL_CST (cst);
9072 if (exact_real_inverse (TYPE_MODE (type), &r))
9073 return build_real (type, r);
9075 return NULL_TREE;
9077 case VECTOR_CST:
9079 unit_type = TREE_TYPE (type);
9080 mode = TYPE_MODE (unit_type);
9082 tree_vector_builder elts;
9083 if (!elts.new_unary_operation (type, cst, false))
9084 return NULL_TREE;
9085 unsigned int count = elts.encoded_nelts ();
9086 for (unsigned int i = 0; i < count; ++i)
9088 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9089 if (!exact_real_inverse (mode, &r))
9090 return NULL_TREE;
9091 elts.quick_push (build_real (unit_type, r));
9094 return elts.build ();
9097 default:
9098 return NULL_TREE;
9102 /* Mask out the tz least significant bits of X of type TYPE where
9103 tz is the number of trailing zeroes in Y. */
9104 static wide_int
9105 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9107 int tz = wi::ctz (y);
9108 if (tz > 0)
9109 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9110 return x;
9113 /* Return true when T is an address and is known to be nonzero.
9114 For floating point we further ensure that T is not denormal.
9115 Similar logic is present in nonzero_address in rtlanal.h.
9117 If the return value is based on the assumption that signed overflow
9118 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9119 change *STRICT_OVERFLOW_P. */
9121 static bool
9122 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9124 tree type = TREE_TYPE (t);
9125 enum tree_code code;
9127 /* Doing something useful for floating point would need more work. */
9128 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9129 return false;
9131 code = TREE_CODE (t);
9132 switch (TREE_CODE_CLASS (code))
9134 case tcc_unary:
9135 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9136 strict_overflow_p);
9137 case tcc_binary:
9138 case tcc_comparison:
9139 return tree_binary_nonzero_warnv_p (code, type,
9140 TREE_OPERAND (t, 0),
9141 TREE_OPERAND (t, 1),
9142 strict_overflow_p);
9143 case tcc_constant:
9144 case tcc_declaration:
9145 case tcc_reference:
9146 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9148 default:
9149 break;
9152 switch (code)
9154 case TRUTH_NOT_EXPR:
9155 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9156 strict_overflow_p);
9158 case TRUTH_AND_EXPR:
9159 case TRUTH_OR_EXPR:
9160 case TRUTH_XOR_EXPR:
9161 return tree_binary_nonzero_warnv_p (code, type,
9162 TREE_OPERAND (t, 0),
9163 TREE_OPERAND (t, 1),
9164 strict_overflow_p);
9166 case COND_EXPR:
9167 case CONSTRUCTOR:
9168 case OBJ_TYPE_REF:
9169 case ASSERT_EXPR:
9170 case ADDR_EXPR:
9171 case WITH_SIZE_EXPR:
9172 case SSA_NAME:
9173 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9175 case COMPOUND_EXPR:
9176 case MODIFY_EXPR:
9177 case BIND_EXPR:
9178 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9179 strict_overflow_p);
9181 case SAVE_EXPR:
9182 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9183 strict_overflow_p);
9185 case CALL_EXPR:
9187 tree fndecl = get_callee_fndecl (t);
9188 if (!fndecl) return false;
9189 if (flag_delete_null_pointer_checks && !flag_check_new
9190 && DECL_IS_OPERATOR_NEW (fndecl)
9191 && !TREE_NOTHROW (fndecl))
9192 return true;
9193 if (flag_delete_null_pointer_checks
9194 && lookup_attribute ("returns_nonnull",
9195 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9196 return true;
9197 return alloca_call_p (t);
9200 default:
9201 break;
9203 return false;
9206 /* Return true when T is an address and is known to be nonzero.
9207 Handle warnings about undefined signed overflow. */
9209 bool
9210 tree_expr_nonzero_p (tree t)
9212 bool ret, strict_overflow_p;
9214 strict_overflow_p = false;
9215 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9216 if (strict_overflow_p)
9217 fold_overflow_warning (("assuming signed overflow does not occur when "
9218 "determining that expression is always "
9219 "non-zero"),
9220 WARN_STRICT_OVERFLOW_MISC);
9221 return ret;
9224 /* Return true if T is known not to be equal to an integer W. */
9226 bool
9227 expr_not_equal_to (tree t, const wide_int &w)
9229 wide_int min, max, nz;
9230 value_range_type rtype;
9231 switch (TREE_CODE (t))
9233 case INTEGER_CST:
9234 return wi::to_wide (t) != w;
9236 case SSA_NAME:
9237 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9238 return false;
9239 rtype = get_range_info (t, &min, &max);
9240 if (rtype == VR_RANGE)
9242 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9243 return true;
9244 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9245 return true;
9247 else if (rtype == VR_ANTI_RANGE
9248 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9249 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9250 return true;
9251 /* If T has some known zero bits and W has any of those bits set,
9252 then T is known not to be equal to W. */
9253 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9254 TYPE_PRECISION (TREE_TYPE (t))), 0))
9255 return true;
9256 return false;
9258 default:
9259 return false;
9263 /* Fold a binary expression of code CODE and type TYPE with operands
9264 OP0 and OP1. LOC is the location of the resulting expression.
9265 Return the folded expression if folding is successful. Otherwise,
9266 return NULL_TREE. */
9268 tree
9269 fold_binary_loc (location_t loc, enum tree_code code, tree type,
9270 tree op0, tree op1)
9272 enum tree_code_class kind = TREE_CODE_CLASS (code);
9273 tree arg0, arg1, tem;
9274 tree t1 = NULL_TREE;
9275 bool strict_overflow_p;
9276 unsigned int prec;
9278 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9279 && TREE_CODE_LENGTH (code) == 2
9280 && op0 != NULL_TREE
9281 && op1 != NULL_TREE);
9283 arg0 = op0;
9284 arg1 = op1;
9286 /* Strip any conversions that don't change the mode. This is
9287 safe for every expression, except for a comparison expression
9288 because its signedness is derived from its operands. So, in
9289 the latter case, only strip conversions that don't change the
9290 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9291 preserved.
9293 Note that this is done as an internal manipulation within the
9294 constant folder, in order to find the simplest representation
9295 of the arguments so that their form can be studied. In any
9296 cases, the appropriate type conversions should be put back in
9297 the tree that will get out of the constant folder. */
9299 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9301 STRIP_SIGN_NOPS (arg0);
9302 STRIP_SIGN_NOPS (arg1);
9304 else
9306 STRIP_NOPS (arg0);
9307 STRIP_NOPS (arg1);
9310 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9311 constant but we can't do arithmetic on them. */
9312 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9314 tem = const_binop (code, type, arg0, arg1);
9315 if (tem != NULL_TREE)
9317 if (TREE_TYPE (tem) != type)
9318 tem = fold_convert_loc (loc, type, tem);
9319 return tem;
9323 /* If this is a commutative operation, and ARG0 is a constant, move it
9324 to ARG1 to reduce the number of tests below. */
9325 if (commutative_tree_code (code)
9326 && tree_swap_operands_p (arg0, arg1))
9327 return fold_build2_loc (loc, code, type, op1, op0);
9329 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9330 to ARG1 to reduce the number of tests below. */
9331 if (kind == tcc_comparison
9332 && tree_swap_operands_p (arg0, arg1))
9333 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9335 tem = generic_simplify (loc, code, type, op0, op1);
9336 if (tem)
9337 return tem;
9339 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9341 First check for cases where an arithmetic operation is applied to a
9342 compound, conditional, or comparison operation. Push the arithmetic
9343 operation inside the compound or conditional to see if any folding
9344 can then be done. Convert comparison to conditional for this purpose.
9345 The also optimizes non-constant cases that used to be done in
9346 expand_expr.
9348 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9349 one of the operands is a comparison and the other is a comparison, a
9350 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9351 code below would make the expression more complex. Change it to a
9352 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9353 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9355 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9356 || code == EQ_EXPR || code == NE_EXPR)
9357 && !VECTOR_TYPE_P (TREE_TYPE (arg0))
9358 && ((truth_value_p (TREE_CODE (arg0))
9359 && (truth_value_p (TREE_CODE (arg1))
9360 || (TREE_CODE (arg1) == BIT_AND_EXPR
9361 && integer_onep (TREE_OPERAND (arg1, 1)))))
9362 || (truth_value_p (TREE_CODE (arg1))
9363 && (truth_value_p (TREE_CODE (arg0))
9364 || (TREE_CODE (arg0) == BIT_AND_EXPR
9365 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9367 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9368 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9369 : TRUTH_XOR_EXPR,
9370 boolean_type_node,
9371 fold_convert_loc (loc, boolean_type_node, arg0),
9372 fold_convert_loc (loc, boolean_type_node, arg1));
9374 if (code == EQ_EXPR)
9375 tem = invert_truthvalue_loc (loc, tem);
9377 return fold_convert_loc (loc, type, tem);
9380 if (TREE_CODE_CLASS (code) == tcc_binary
9381 || TREE_CODE_CLASS (code) == tcc_comparison)
9383 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9385 tem = fold_build2_loc (loc, code, type,
9386 fold_convert_loc (loc, TREE_TYPE (op0),
9387 TREE_OPERAND (arg0, 1)), op1);
9388 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9389 tem);
9391 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9393 tem = fold_build2_loc (loc, code, type, op0,
9394 fold_convert_loc (loc, TREE_TYPE (op1),
9395 TREE_OPERAND (arg1, 1)));
9396 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9397 tem);
9400 if (TREE_CODE (arg0) == COND_EXPR
9401 || TREE_CODE (arg0) == VEC_COND_EXPR
9402 || COMPARISON_CLASS_P (arg0))
9404 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9405 arg0, arg1,
9406 /*cond_first_p=*/1);
9407 if (tem != NULL_TREE)
9408 return tem;
9411 if (TREE_CODE (arg1) == COND_EXPR
9412 || TREE_CODE (arg1) == VEC_COND_EXPR
9413 || COMPARISON_CLASS_P (arg1))
9415 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9416 arg1, arg0,
9417 /*cond_first_p=*/0);
9418 if (tem != NULL_TREE)
9419 return tem;
9423 switch (code)
9425 case MEM_REF:
9426 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9427 if (TREE_CODE (arg0) == ADDR_EXPR
9428 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9430 tree iref = TREE_OPERAND (arg0, 0);
9431 return fold_build2 (MEM_REF, type,
9432 TREE_OPERAND (iref, 0),
9433 int_const_binop (PLUS_EXPR, arg1,
9434 TREE_OPERAND (iref, 1)));
9437 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9438 if (TREE_CODE (arg0) == ADDR_EXPR
9439 && handled_component_p (TREE_OPERAND (arg0, 0)))
9441 tree base;
9442 poly_int64 coffset;
9443 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9444 &coffset);
9445 if (!base)
9446 return NULL_TREE;
9447 return fold_build2 (MEM_REF, type,
9448 build_fold_addr_expr (base),
9449 int_const_binop (PLUS_EXPR, arg1,
9450 size_int (coffset)));
9453 return NULL_TREE;
9455 case POINTER_PLUS_EXPR:
9456 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9457 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9458 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9459 return fold_convert_loc (loc, type,
9460 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9461 fold_convert_loc (loc, sizetype,
9462 arg1),
9463 fold_convert_loc (loc, sizetype,
9464 arg0)));
9466 return NULL_TREE;
9468 case PLUS_EXPR:
9469 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9471 /* X + (X / CST) * -CST is X % CST. */
9472 if (TREE_CODE (arg1) == MULT_EXPR
9473 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9474 && operand_equal_p (arg0,
9475 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9477 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9478 tree cst1 = TREE_OPERAND (arg1, 1);
9479 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9480 cst1, cst0);
9481 if (sum && integer_zerop (sum))
9482 return fold_convert_loc (loc, type,
9483 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9484 TREE_TYPE (arg0), arg0,
9485 cst0));
9489 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9490 one. Make sure the type is not saturating and has the signedness of
9491 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9492 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9493 if ((TREE_CODE (arg0) == MULT_EXPR
9494 || TREE_CODE (arg1) == MULT_EXPR)
9495 && !TYPE_SATURATING (type)
9496 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9497 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9498 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9500 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9501 if (tem)
9502 return tem;
9505 if (! FLOAT_TYPE_P (type))
9507 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9508 (plus (plus (mult) (mult)) (foo)) so that we can
9509 take advantage of the factoring cases below. */
9510 if (ANY_INTEGRAL_TYPE_P (type)
9511 && TYPE_OVERFLOW_WRAPS (type)
9512 && (((TREE_CODE (arg0) == PLUS_EXPR
9513 || TREE_CODE (arg0) == MINUS_EXPR)
9514 && TREE_CODE (arg1) == MULT_EXPR)
9515 || ((TREE_CODE (arg1) == PLUS_EXPR
9516 || TREE_CODE (arg1) == MINUS_EXPR)
9517 && TREE_CODE (arg0) == MULT_EXPR)))
9519 tree parg0, parg1, parg, marg;
9520 enum tree_code pcode;
9522 if (TREE_CODE (arg1) == MULT_EXPR)
9523 parg = arg0, marg = arg1;
9524 else
9525 parg = arg1, marg = arg0;
9526 pcode = TREE_CODE (parg);
9527 parg0 = TREE_OPERAND (parg, 0);
9528 parg1 = TREE_OPERAND (parg, 1);
9529 STRIP_NOPS (parg0);
9530 STRIP_NOPS (parg1);
9532 if (TREE_CODE (parg0) == MULT_EXPR
9533 && TREE_CODE (parg1) != MULT_EXPR)
9534 return fold_build2_loc (loc, pcode, type,
9535 fold_build2_loc (loc, PLUS_EXPR, type,
9536 fold_convert_loc (loc, type,
9537 parg0),
9538 fold_convert_loc (loc, type,
9539 marg)),
9540 fold_convert_loc (loc, type, parg1));
9541 if (TREE_CODE (parg0) != MULT_EXPR
9542 && TREE_CODE (parg1) == MULT_EXPR)
9543 return
9544 fold_build2_loc (loc, PLUS_EXPR, type,
9545 fold_convert_loc (loc, type, parg0),
9546 fold_build2_loc (loc, pcode, type,
9547 fold_convert_loc (loc, type, marg),
9548 fold_convert_loc (loc, type,
9549 parg1)));
9552 else
9554 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9555 to __complex__ ( x, y ). This is not the same for SNaNs or
9556 if signed zeros are involved. */
9557 if (!HONOR_SNANS (element_mode (arg0))
9558 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9559 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9561 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9562 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9563 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9564 bool arg0rz = false, arg0iz = false;
9565 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9566 || (arg0i && (arg0iz = real_zerop (arg0i))))
9568 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9569 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9570 if (arg0rz && arg1i && real_zerop (arg1i))
9572 tree rp = arg1r ? arg1r
9573 : build1 (REALPART_EXPR, rtype, arg1);
9574 tree ip = arg0i ? arg0i
9575 : build1 (IMAGPART_EXPR, rtype, arg0);
9576 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9578 else if (arg0iz && arg1r && real_zerop (arg1r))
9580 tree rp = arg0r ? arg0r
9581 : build1 (REALPART_EXPR, rtype, arg0);
9582 tree ip = arg1i ? arg1i
9583 : build1 (IMAGPART_EXPR, rtype, arg1);
9584 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9589 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9590 We associate floats only if the user has specified
9591 -fassociative-math. */
9592 if (flag_associative_math
9593 && TREE_CODE (arg1) == PLUS_EXPR
9594 && TREE_CODE (arg0) != MULT_EXPR)
9596 tree tree10 = TREE_OPERAND (arg1, 0);
9597 tree tree11 = TREE_OPERAND (arg1, 1);
9598 if (TREE_CODE (tree11) == MULT_EXPR
9599 && TREE_CODE (tree10) == MULT_EXPR)
9601 tree tree0;
9602 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9603 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9606 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9607 We associate floats only if the user has specified
9608 -fassociative-math. */
9609 if (flag_associative_math
9610 && TREE_CODE (arg0) == PLUS_EXPR
9611 && TREE_CODE (arg1) != MULT_EXPR)
9613 tree tree00 = TREE_OPERAND (arg0, 0);
9614 tree tree01 = TREE_OPERAND (arg0, 1);
9615 if (TREE_CODE (tree01) == MULT_EXPR
9616 && TREE_CODE (tree00) == MULT_EXPR)
9618 tree tree0;
9619 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9620 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9625 bit_rotate:
9626 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9627 is a rotate of A by C1 bits. */
9628 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9629 is a rotate of A by B bits.
9630 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
9631 though in this case CODE must be | and not + or ^, otherwise
9632 it doesn't return A when B is 0. */
9634 enum tree_code code0, code1;
9635 tree rtype;
9636 code0 = TREE_CODE (arg0);
9637 code1 = TREE_CODE (arg1);
9638 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9639 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9640 && operand_equal_p (TREE_OPERAND (arg0, 0),
9641 TREE_OPERAND (arg1, 0), 0)
9642 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9643 TYPE_UNSIGNED (rtype))
9644 /* Only create rotates in complete modes. Other cases are not
9645 expanded properly. */
9646 && (element_precision (rtype)
9647 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9649 tree tree01, tree11;
9650 tree orig_tree01, orig_tree11;
9651 enum tree_code code01, code11;
9653 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
9654 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
9655 STRIP_NOPS (tree01);
9656 STRIP_NOPS (tree11);
9657 code01 = TREE_CODE (tree01);
9658 code11 = TREE_CODE (tree11);
9659 if (code11 != MINUS_EXPR
9660 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
9662 std::swap (code0, code1);
9663 std::swap (code01, code11);
9664 std::swap (tree01, tree11);
9665 std::swap (orig_tree01, orig_tree11);
9667 if (code01 == INTEGER_CST
9668 && code11 == INTEGER_CST
9669 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9670 == element_precision (rtype)))
9672 tem = build2_loc (loc, LROTATE_EXPR,
9673 rtype, TREE_OPERAND (arg0, 0),
9674 code0 == LSHIFT_EXPR
9675 ? orig_tree01 : orig_tree11);
9676 return fold_convert_loc (loc, type, tem);
9678 else if (code11 == MINUS_EXPR)
9680 tree tree110, tree111;
9681 tree110 = TREE_OPERAND (tree11, 0);
9682 tree111 = TREE_OPERAND (tree11, 1);
9683 STRIP_NOPS (tree110);
9684 STRIP_NOPS (tree111);
9685 if (TREE_CODE (tree110) == INTEGER_CST
9686 && compare_tree_int (tree110,
9687 element_precision (rtype)) == 0
9688 && operand_equal_p (tree01, tree111, 0))
9690 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9691 ? LROTATE_EXPR : RROTATE_EXPR),
9692 rtype, TREE_OPERAND (arg0, 0),
9693 orig_tree01);
9694 return fold_convert_loc (loc, type, tem);
9697 else if (code == BIT_IOR_EXPR
9698 && code11 == BIT_AND_EXPR
9699 && pow2p_hwi (element_precision (rtype)))
9701 tree tree110, tree111;
9702 tree110 = TREE_OPERAND (tree11, 0);
9703 tree111 = TREE_OPERAND (tree11, 1);
9704 STRIP_NOPS (tree110);
9705 STRIP_NOPS (tree111);
9706 if (TREE_CODE (tree110) == NEGATE_EXPR
9707 && TREE_CODE (tree111) == INTEGER_CST
9708 && compare_tree_int (tree111,
9709 element_precision (rtype) - 1) == 0
9710 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
9712 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9713 ? LROTATE_EXPR : RROTATE_EXPR),
9714 rtype, TREE_OPERAND (arg0, 0),
9715 orig_tree01);
9716 return fold_convert_loc (loc, type, tem);
9722 associate:
9723 /* In most languages, can't associate operations on floats through
9724 parentheses. Rather than remember where the parentheses were, we
9725 don't associate floats at all, unless the user has specified
9726 -fassociative-math.
9727 And, we need to make sure type is not saturating. */
9729 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9730 && !TYPE_SATURATING (type))
9732 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9733 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9734 tree atype = type;
9735 bool ok = true;
9737 /* Split both trees into variables, constants, and literals. Then
9738 associate each group together, the constants with literals,
9739 then the result with variables. This increases the chances of
9740 literals being recombined later and of generating relocatable
9741 expressions for the sum of a constant and literal. */
9742 var0 = split_tree (arg0, type, code,
9743 &minus_var0, &con0, &minus_con0,
9744 &lit0, &minus_lit0, 0);
9745 var1 = split_tree (arg1, type, code,
9746 &minus_var1, &con1, &minus_con1,
9747 &lit1, &minus_lit1, code == MINUS_EXPR);
9749 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9750 if (code == MINUS_EXPR)
9751 code = PLUS_EXPR;
9753 /* With undefined overflow prefer doing association in a type
9754 which wraps on overflow, if that is one of the operand types. */
9755 if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
9756 && !TYPE_OVERFLOW_WRAPS (type))
9758 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9759 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9760 atype = TREE_TYPE (arg0);
9761 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9762 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9763 atype = TREE_TYPE (arg1);
9764 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9767 /* With undefined overflow we can only associate constants with one
9768 variable, and constants whose association doesn't overflow. */
9769 if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
9770 && !TYPE_OVERFLOW_WRAPS (atype))
9772 if ((var0 && var1) || (minus_var0 && minus_var1))
9774 /* ??? If split_tree would handle NEGATE_EXPR we could
9775 simply reject these cases and the allowed cases would
9776 be the var0/minus_var1 ones. */
9777 tree tmp0 = var0 ? var0 : minus_var0;
9778 tree tmp1 = var1 ? var1 : minus_var1;
9779 bool one_neg = false;
9781 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9783 tmp0 = TREE_OPERAND (tmp0, 0);
9784 one_neg = !one_neg;
9786 if (CONVERT_EXPR_P (tmp0)
9787 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9788 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9789 <= TYPE_PRECISION (atype)))
9790 tmp0 = TREE_OPERAND (tmp0, 0);
9791 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9793 tmp1 = TREE_OPERAND (tmp1, 0);
9794 one_neg = !one_neg;
9796 if (CONVERT_EXPR_P (tmp1)
9797 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9798 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9799 <= TYPE_PRECISION (atype)))
9800 tmp1 = TREE_OPERAND (tmp1, 0);
9801 /* The only case we can still associate with two variables
9802 is if they cancel out. */
9803 if (!one_neg
9804 || !operand_equal_p (tmp0, tmp1, 0))
9805 ok = false;
9807 else if ((var0 && minus_var1
9808 && ! operand_equal_p (var0, minus_var1, 0))
9809 || (minus_var0 && var1
9810 && ! operand_equal_p (minus_var0, var1, 0)))
9811 ok = false;
9814 /* Only do something if we found more than two objects. Otherwise,
9815 nothing has changed and we risk infinite recursion. */
9816 if (ok
9817 && ((var0 != 0) + (var1 != 0)
9818 + (minus_var0 != 0) + (minus_var1 != 0)
9819 + (con0 != 0) + (con1 != 0)
9820 + (minus_con0 != 0) + (minus_con1 != 0)
9821 + (lit0 != 0) + (lit1 != 0)
9822 + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
9824 var0 = associate_trees (loc, var0, var1, code, atype);
9825 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9826 code, atype);
9827 con0 = associate_trees (loc, con0, con1, code, atype);
9828 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9829 code, atype);
9830 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9831 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9832 code, atype);
9834 if (minus_var0 && var0)
9836 var0 = associate_trees (loc, var0, minus_var0,
9837 MINUS_EXPR, atype);
9838 minus_var0 = 0;
9840 if (minus_con0 && con0)
9842 con0 = associate_trees (loc, con0, minus_con0,
9843 MINUS_EXPR, atype);
9844 minus_con0 = 0;
9847 /* Preserve the MINUS_EXPR if the negative part of the literal is
9848 greater than the positive part. Otherwise, the multiplicative
9849 folding code (i.e extract_muldiv) may be fooled in case
9850 unsigned constants are subtracted, like in the following
9851 example: ((X*2 + 4) - 8U)/2. */
9852 if (minus_lit0 && lit0)
9854 if (TREE_CODE (lit0) == INTEGER_CST
9855 && TREE_CODE (minus_lit0) == INTEGER_CST
9856 && tree_int_cst_lt (lit0, minus_lit0)
9857 /* But avoid ending up with only negated parts. */
9858 && (var0 || con0))
9860 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9861 MINUS_EXPR, atype);
9862 lit0 = 0;
9864 else
9866 lit0 = associate_trees (loc, lit0, minus_lit0,
9867 MINUS_EXPR, atype);
9868 minus_lit0 = 0;
9872 /* Don't introduce overflows through reassociation. */
9873 if ((lit0 && TREE_OVERFLOW_P (lit0))
9874 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9875 return NULL_TREE;
9877 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9878 con0 = associate_trees (loc, con0, lit0, code, atype);
9879 lit0 = 0;
9880 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9881 code, atype);
9882 minus_lit0 = 0;
9884 /* Eliminate minus_con0. */
9885 if (minus_con0)
9887 if (con0)
9888 con0 = associate_trees (loc, con0, minus_con0,
9889 MINUS_EXPR, atype);
9890 else if (var0)
9891 var0 = associate_trees (loc, var0, minus_con0,
9892 MINUS_EXPR, atype);
9893 else
9894 gcc_unreachable ();
9895 minus_con0 = 0;
9898 /* Eliminate minus_var0. */
9899 if (minus_var0)
9901 if (con0)
9902 con0 = associate_trees (loc, con0, minus_var0,
9903 MINUS_EXPR, atype);
9904 else
9905 gcc_unreachable ();
9906 minus_var0 = 0;
9909 return
9910 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9911 code, atype));
9915 return NULL_TREE;
9917 case POINTER_DIFF_EXPR:
9918 case MINUS_EXPR:
9919 /* Fold &a[i] - &a[j] to i-j. */
9920 if (TREE_CODE (arg0) == ADDR_EXPR
9921 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9922 && TREE_CODE (arg1) == ADDR_EXPR
9923 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9925 tree tem = fold_addr_of_array_ref_difference (loc, type,
9926 TREE_OPERAND (arg0, 0),
9927 TREE_OPERAND (arg1, 0),
9928 code
9929 == POINTER_DIFF_EXPR);
9930 if (tem)
9931 return tem;
9934 /* Further transformations are not for pointers. */
9935 if (code == POINTER_DIFF_EXPR)
9936 return NULL_TREE;
9938 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9939 if (TREE_CODE (arg0) == NEGATE_EXPR
9940 && negate_expr_p (op1)
9941 /* If arg0 is e.g. unsigned int and type is int, then this could
9942 introduce UB, because if A is INT_MIN at runtime, the original
9943 expression can be well defined while the latter is not.
9944 See PR83269. */
9945 && !(ANY_INTEGRAL_TYPE_P (type)
9946 && TYPE_OVERFLOW_UNDEFINED (type)
9947 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9948 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
9949 return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
9950 fold_convert_loc (loc, type,
9951 TREE_OPERAND (arg0, 0)));
9953 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9954 __complex__ ( x, -y ). This is not the same for SNaNs or if
9955 signed zeros are involved. */
9956 if (!HONOR_SNANS (element_mode (arg0))
9957 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9958 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9960 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9961 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9962 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9963 bool arg0rz = false, arg0iz = false;
9964 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9965 || (arg0i && (arg0iz = real_zerop (arg0i))))
9967 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9968 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9969 if (arg0rz && arg1i && real_zerop (arg1i))
9971 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9972 arg1r ? arg1r
9973 : build1 (REALPART_EXPR, rtype, arg1));
9974 tree ip = arg0i ? arg0i
9975 : build1 (IMAGPART_EXPR, rtype, arg0);
9976 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9978 else if (arg0iz && arg1r && real_zerop (arg1r))
9980 tree rp = arg0r ? arg0r
9981 : build1 (REALPART_EXPR, rtype, arg0);
9982 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9983 arg1i ? arg1i
9984 : build1 (IMAGPART_EXPR, rtype, arg1));
9985 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9990 /* A - B -> A + (-B) if B is easily negatable. */
9991 if (negate_expr_p (op1)
9992 && ! TYPE_OVERFLOW_SANITIZED (type)
9993 && ((FLOAT_TYPE_P (type)
9994 /* Avoid this transformation if B is a positive REAL_CST. */
9995 && (TREE_CODE (op1) != REAL_CST
9996 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9997 || INTEGRAL_TYPE_P (type)))
9998 return fold_build2_loc (loc, PLUS_EXPR, type,
9999 fold_convert_loc (loc, type, arg0),
10000 negate_expr (op1));
10002 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
10003 one. Make sure the type is not saturating and has the signedness of
10004 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10005 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10006 if ((TREE_CODE (arg0) == MULT_EXPR
10007 || TREE_CODE (arg1) == MULT_EXPR)
10008 && !TYPE_SATURATING (type)
10009 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10010 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10011 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10013 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10014 if (tem)
10015 return tem;
10018 goto associate;
10020 case MULT_EXPR:
10021 if (! FLOAT_TYPE_P (type))
10023 /* Transform x * -C into -x * C if x is easily negatable. */
10024 if (TREE_CODE (op1) == INTEGER_CST
10025 && tree_int_cst_sgn (op1) == -1
10026 && negate_expr_p (op0)
10027 && negate_expr_p (op1)
10028 && (tem = negate_expr (op1)) != op1
10029 && ! TREE_OVERFLOW (tem))
10030 return fold_build2_loc (loc, MULT_EXPR, type,
10031 fold_convert_loc (loc, type,
10032 negate_expr (op0)), tem);
10034 strict_overflow_p = false;
10035 if (TREE_CODE (arg1) == INTEGER_CST
10036 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10037 &strict_overflow_p)) != 0)
10039 if (strict_overflow_p)
10040 fold_overflow_warning (("assuming signed overflow does not "
10041 "occur when simplifying "
10042 "multiplication"),
10043 WARN_STRICT_OVERFLOW_MISC);
10044 return fold_convert_loc (loc, type, tem);
10047 /* Optimize z * conj(z) for integer complex numbers. */
10048 if (TREE_CODE (arg0) == CONJ_EXPR
10049 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10050 return fold_mult_zconjz (loc, type, arg1);
10051 if (TREE_CODE (arg1) == CONJ_EXPR
10052 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10053 return fold_mult_zconjz (loc, type, arg0);
10055 else
10057 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10058 This is not the same for NaNs or if signed zeros are
10059 involved. */
10060 if (!HONOR_NANS (arg0)
10061 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10062 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10063 && TREE_CODE (arg1) == COMPLEX_CST
10064 && real_zerop (TREE_REALPART (arg1)))
10066 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10067 if (real_onep (TREE_IMAGPART (arg1)))
10068 return
10069 fold_build2_loc (loc, COMPLEX_EXPR, type,
10070 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10071 rtype, arg0)),
10072 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10073 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10074 return
10075 fold_build2_loc (loc, COMPLEX_EXPR, type,
10076 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10077 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10078 rtype, arg0)));
10081 /* Optimize z * conj(z) for floating point complex numbers.
10082 Guarded by flag_unsafe_math_optimizations as non-finite
10083 imaginary components don't produce scalar results. */
10084 if (flag_unsafe_math_optimizations
10085 && TREE_CODE (arg0) == CONJ_EXPR
10086 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10087 return fold_mult_zconjz (loc, type, arg1);
10088 if (flag_unsafe_math_optimizations
10089 && TREE_CODE (arg1) == CONJ_EXPR
10090 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10091 return fold_mult_zconjz (loc, type, arg0);
10093 goto associate;
10095 case BIT_IOR_EXPR:
10096 /* Canonicalize (X & C1) | C2. */
10097 if (TREE_CODE (arg0) == BIT_AND_EXPR
10098 && TREE_CODE (arg1) == INTEGER_CST
10099 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10101 int width = TYPE_PRECISION (type), w;
10102 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
10103 wide_int c2 = wi::to_wide (arg1);
10105 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10106 if ((c1 & c2) == c1)
10107 return omit_one_operand_loc (loc, type, arg1,
10108 TREE_OPERAND (arg0, 0));
10110 wide_int msk = wi::mask (width, false,
10111 TYPE_PRECISION (TREE_TYPE (arg1)));
10113 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10114 if (wi::bit_and_not (msk, c1 | c2) == 0)
10116 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10117 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10120 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10121 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10122 mode which allows further optimizations. */
10123 c1 &= msk;
10124 c2 &= msk;
10125 wide_int c3 = wi::bit_and_not (c1, c2);
10126 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10128 wide_int mask = wi::mask (w, false,
10129 TYPE_PRECISION (type));
10130 if (((c1 | c2) & mask) == mask
10131 && wi::bit_and_not (c1, mask) == 0)
10133 c3 = mask;
10134 break;
10138 if (c3 != c1)
10140 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10141 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
10142 wide_int_to_tree (type, c3));
10143 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10147 /* See if this can be simplified into a rotate first. If that
10148 is unsuccessful continue in the association code. */
10149 goto bit_rotate;
10151 case BIT_XOR_EXPR:
10152 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10153 if (TREE_CODE (arg0) == BIT_AND_EXPR
10154 && INTEGRAL_TYPE_P (type)
10155 && integer_onep (TREE_OPERAND (arg0, 1))
10156 && integer_onep (arg1))
10157 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10158 build_zero_cst (TREE_TYPE (arg0)));
10160 /* See if this can be simplified into a rotate first. If that
10161 is unsuccessful continue in the association code. */
10162 goto bit_rotate;
10164 case BIT_AND_EXPR:
10165 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10166 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10167 && INTEGRAL_TYPE_P (type)
10168 && integer_onep (TREE_OPERAND (arg0, 1))
10169 && integer_onep (arg1))
10171 tree tem2;
10172 tem = TREE_OPERAND (arg0, 0);
10173 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10174 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10175 tem, tem2);
10176 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10177 build_zero_cst (TREE_TYPE (tem)));
10179 /* Fold ~X & 1 as (X & 1) == 0. */
10180 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10181 && INTEGRAL_TYPE_P (type)
10182 && integer_onep (arg1))
10184 tree tem2;
10185 tem = TREE_OPERAND (arg0, 0);
10186 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10187 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10188 tem, tem2);
10189 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10190 build_zero_cst (TREE_TYPE (tem)));
10192 /* Fold !X & 1 as X == 0. */
10193 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10194 && integer_onep (arg1))
10196 tem = TREE_OPERAND (arg0, 0);
10197 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10198 build_zero_cst (TREE_TYPE (tem)));
10201 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10202 multiple of 1 << CST. */
10203 if (TREE_CODE (arg1) == INTEGER_CST)
10205 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10206 wide_int ncst1 = -cst1;
10207 if ((cst1 & ncst1) == ncst1
10208 && multiple_of_p (type, arg0,
10209 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10210 return fold_convert_loc (loc, type, arg0);
10213 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10214 bits from CST2. */
10215 if (TREE_CODE (arg1) == INTEGER_CST
10216 && TREE_CODE (arg0) == MULT_EXPR
10217 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10219 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
10220 wide_int masked
10221 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
10223 if (masked == 0)
10224 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10225 arg0, arg1);
10226 else if (masked != warg1)
10228 /* Avoid the transform if arg1 is a mask of some
10229 mode which allows further optimizations. */
10230 int pop = wi::popcount (warg1);
10231 if (!(pop >= BITS_PER_UNIT
10232 && pow2p_hwi (pop)
10233 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10234 return fold_build2_loc (loc, code, type, op0,
10235 wide_int_to_tree (type, masked));
10239 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10240 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10241 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10243 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10245 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
10246 if (mask == -1)
10247 return
10248 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10251 goto associate;
10253 case RDIV_EXPR:
10254 /* Don't touch a floating-point divide by zero unless the mode
10255 of the constant can represent infinity. */
10256 if (TREE_CODE (arg1) == REAL_CST
10257 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10258 && real_zerop (arg1))
10259 return NULL_TREE;
10261 /* (-A) / (-B) -> A / B */
10262 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10263 return fold_build2_loc (loc, RDIV_EXPR, type,
10264 TREE_OPERAND (arg0, 0),
10265 negate_expr (arg1));
10266 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10267 return fold_build2_loc (loc, RDIV_EXPR, type,
10268 negate_expr (arg0),
10269 TREE_OPERAND (arg1, 0));
10270 return NULL_TREE;
10272 case TRUNC_DIV_EXPR:
10273 /* Fall through */
10275 case FLOOR_DIV_EXPR:
10276 /* Simplify A / (B << N) where A and B are positive and B is
10277 a power of 2, to A >> (N + log2(B)). */
10278 strict_overflow_p = false;
10279 if (TREE_CODE (arg1) == LSHIFT_EXPR
10280 && (TYPE_UNSIGNED (type)
10281 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10283 tree sval = TREE_OPERAND (arg1, 0);
10284 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10286 tree sh_cnt = TREE_OPERAND (arg1, 1);
10287 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10288 wi::exact_log2 (wi::to_wide (sval)));
10290 if (strict_overflow_p)
10291 fold_overflow_warning (("assuming signed overflow does not "
10292 "occur when simplifying A / (B << N)"),
10293 WARN_STRICT_OVERFLOW_MISC);
10295 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10296 sh_cnt, pow2);
10297 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10298 fold_convert_loc (loc, type, arg0), sh_cnt);
10302 /* Fall through */
10304 case ROUND_DIV_EXPR:
10305 case CEIL_DIV_EXPR:
10306 case EXACT_DIV_EXPR:
10307 if (integer_zerop (arg1))
10308 return NULL_TREE;
10310 /* Convert -A / -B to A / B when the type is signed and overflow is
10311 undefined. */
10312 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10313 && TREE_CODE (op0) == NEGATE_EXPR
10314 && negate_expr_p (op1))
10316 if (INTEGRAL_TYPE_P (type))
10317 fold_overflow_warning (("assuming signed overflow does not occur "
10318 "when distributing negation across "
10319 "division"),
10320 WARN_STRICT_OVERFLOW_MISC);
10321 return fold_build2_loc (loc, code, type,
10322 fold_convert_loc (loc, type,
10323 TREE_OPERAND (arg0, 0)),
10324 negate_expr (op1));
10326 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10327 && TREE_CODE (arg1) == NEGATE_EXPR
10328 && negate_expr_p (op0))
10330 if (INTEGRAL_TYPE_P (type))
10331 fold_overflow_warning (("assuming signed overflow does not occur "
10332 "when distributing negation across "
10333 "division"),
10334 WARN_STRICT_OVERFLOW_MISC);
10335 return fold_build2_loc (loc, code, type,
10336 negate_expr (op0),
10337 fold_convert_loc (loc, type,
10338 TREE_OPERAND (arg1, 0)));
10341 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10342 operation, EXACT_DIV_EXPR.
10344 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10345 At one time others generated faster code, it's not clear if they do
10346 after the last round to changes to the DIV code in expmed.c. */
10347 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10348 && multiple_of_p (type, arg0, arg1))
10349 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10350 fold_convert (type, arg0),
10351 fold_convert (type, arg1));
10353 strict_overflow_p = false;
10354 if (TREE_CODE (arg1) == INTEGER_CST
10355 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10356 &strict_overflow_p)) != 0)
10358 if (strict_overflow_p)
10359 fold_overflow_warning (("assuming signed overflow does not occur "
10360 "when simplifying division"),
10361 WARN_STRICT_OVERFLOW_MISC);
10362 return fold_convert_loc (loc, type, tem);
10365 return NULL_TREE;
10367 case CEIL_MOD_EXPR:
10368 case FLOOR_MOD_EXPR:
10369 case ROUND_MOD_EXPR:
10370 case TRUNC_MOD_EXPR:
10371 strict_overflow_p = false;
10372 if (TREE_CODE (arg1) == INTEGER_CST
10373 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10374 &strict_overflow_p)) != 0)
10376 if (strict_overflow_p)
10377 fold_overflow_warning (("assuming signed overflow does not occur "
10378 "when simplifying modulus"),
10379 WARN_STRICT_OVERFLOW_MISC);
10380 return fold_convert_loc (loc, type, tem);
10383 return NULL_TREE;
10385 case LROTATE_EXPR:
10386 case RROTATE_EXPR:
10387 case RSHIFT_EXPR:
10388 case LSHIFT_EXPR:
10389 /* Since negative shift count is not well-defined,
10390 don't try to compute it in the compiler. */
10391 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10392 return NULL_TREE;
10394 prec = element_precision (type);
10396 /* If we have a rotate of a bit operation with the rotate count and
10397 the second operand of the bit operation both constant,
10398 permute the two operations. */
10399 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10400 && (TREE_CODE (arg0) == BIT_AND_EXPR
10401 || TREE_CODE (arg0) == BIT_IOR_EXPR
10402 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10403 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10405 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10406 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10407 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10408 fold_build2_loc (loc, code, type,
10409 arg00, arg1),
10410 fold_build2_loc (loc, code, type,
10411 arg01, arg1));
10414 /* Two consecutive rotates adding up to the some integer
10415 multiple of the precision of the type can be ignored. */
10416 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10417 && TREE_CODE (arg0) == RROTATE_EXPR
10418 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10419 && wi::umod_trunc (wi::to_wide (arg1)
10420 + wi::to_wide (TREE_OPERAND (arg0, 1)),
10421 prec) == 0)
10422 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10424 return NULL_TREE;
10426 case MIN_EXPR:
10427 case MAX_EXPR:
10428 goto associate;
10430 case TRUTH_ANDIF_EXPR:
10431 /* Note that the operands of this must be ints
10432 and their values must be 0 or 1.
10433 ("true" is a fixed value perhaps depending on the language.) */
10434 /* If first arg is constant zero, return it. */
10435 if (integer_zerop (arg0))
10436 return fold_convert_loc (loc, type, arg0);
10437 /* FALLTHRU */
10438 case TRUTH_AND_EXPR:
10439 /* If either arg is constant true, drop it. */
10440 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10441 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10442 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10443 /* Preserve sequence points. */
10444 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10445 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10446 /* If second arg is constant zero, result is zero, but first arg
10447 must be evaluated. */
10448 if (integer_zerop (arg1))
10449 return omit_one_operand_loc (loc, type, arg1, arg0);
10450 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10451 case will be handled here. */
10452 if (integer_zerop (arg0))
10453 return omit_one_operand_loc (loc, type, arg0, arg1);
10455 /* !X && X is always false. */
10456 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10457 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10458 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10459 /* X && !X is always false. */
10460 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10461 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10462 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10464 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10465 means A >= Y && A != MAX, but in this case we know that
10466 A < X <= MAX. */
10468 if (!TREE_SIDE_EFFECTS (arg0)
10469 && !TREE_SIDE_EFFECTS (arg1))
10471 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10472 if (tem && !operand_equal_p (tem, arg0, 0))
10473 return fold_build2_loc (loc, code, type, tem, arg1);
10475 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10476 if (tem && !operand_equal_p (tem, arg1, 0))
10477 return fold_build2_loc (loc, code, type, arg0, tem);
10480 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10481 != NULL_TREE)
10482 return tem;
10484 return NULL_TREE;
10486 case TRUTH_ORIF_EXPR:
10487 /* Note that the operands of this must be ints
10488 and their values must be 0 or true.
10489 ("true" is a fixed value perhaps depending on the language.) */
10490 /* If first arg is constant true, return it. */
10491 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10492 return fold_convert_loc (loc, type, arg0);
10493 /* FALLTHRU */
10494 case TRUTH_OR_EXPR:
10495 /* If either arg is constant zero, drop it. */
10496 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10497 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10498 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10499 /* Preserve sequence points. */
10500 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10501 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10502 /* If second arg is constant true, result is true, but we must
10503 evaluate first arg. */
10504 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10505 return omit_one_operand_loc (loc, type, arg1, arg0);
10506 /* Likewise for first arg, but note this only occurs here for
10507 TRUTH_OR_EXPR. */
10508 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10509 return omit_one_operand_loc (loc, type, arg0, arg1);
10511 /* !X || X is always true. */
10512 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10513 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10514 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10515 /* X || !X is always true. */
10516 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10517 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10518 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10520 /* (X && !Y) || (!X && Y) is X ^ Y */
10521 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10522 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10524 tree a0, a1, l0, l1, n0, n1;
10526 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10527 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10529 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10530 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10532 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10533 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10535 if ((operand_equal_p (n0, a0, 0)
10536 && operand_equal_p (n1, a1, 0))
10537 || (operand_equal_p (n0, a1, 0)
10538 && operand_equal_p (n1, a0, 0)))
10539 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10542 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10543 != NULL_TREE)
10544 return tem;
10546 return NULL_TREE;
10548 case TRUTH_XOR_EXPR:
10549 /* If the second arg is constant zero, drop it. */
10550 if (integer_zerop (arg1))
10551 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10552 /* If the second arg is constant true, this is a logical inversion. */
10553 if (integer_onep (arg1))
10555 tem = invert_truthvalue_loc (loc, arg0);
10556 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10558 /* Identical arguments cancel to zero. */
10559 if (operand_equal_p (arg0, arg1, 0))
10560 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10562 /* !X ^ X is always true. */
10563 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10564 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10565 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10567 /* X ^ !X is always true. */
10568 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10569 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10570 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10572 return NULL_TREE;
10574 case EQ_EXPR:
10575 case NE_EXPR:
10576 STRIP_NOPS (arg0);
10577 STRIP_NOPS (arg1);
10579 tem = fold_comparison (loc, code, type, op0, op1);
10580 if (tem != NULL_TREE)
10581 return tem;
10583 /* bool_var != 1 becomes !bool_var. */
10584 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10585 && code == NE_EXPR)
10586 return fold_convert_loc (loc, type,
10587 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10588 TREE_TYPE (arg0), arg0));
10590 /* bool_var == 0 becomes !bool_var. */
10591 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10592 && code == EQ_EXPR)
10593 return fold_convert_loc (loc, type,
10594 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10595 TREE_TYPE (arg0), arg0));
10597 /* !exp != 0 becomes !exp */
10598 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10599 && code == NE_EXPR)
10600 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10602 /* If this is an EQ or NE comparison with zero and ARG0 is
10603 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10604 two operations, but the latter can be done in one less insn
10605 on machines that have only two-operand insns or on which a
10606 constant cannot be the first operand. */
10607 if (TREE_CODE (arg0) == BIT_AND_EXPR
10608 && integer_zerop (arg1))
10610 tree arg00 = TREE_OPERAND (arg0, 0);
10611 tree arg01 = TREE_OPERAND (arg0, 1);
10612 if (TREE_CODE (arg00) == LSHIFT_EXPR
10613 && integer_onep (TREE_OPERAND (arg00, 0)))
10615 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10616 arg01, TREE_OPERAND (arg00, 1));
10617 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10618 build_int_cst (TREE_TYPE (arg0), 1));
10619 return fold_build2_loc (loc, code, type,
10620 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10621 arg1);
10623 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10624 && integer_onep (TREE_OPERAND (arg01, 0)))
10626 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10627 arg00, TREE_OPERAND (arg01, 1));
10628 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10629 build_int_cst (TREE_TYPE (arg0), 1));
10630 return fold_build2_loc (loc, code, type,
10631 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10632 arg1);
10636 /* If this is an NE or EQ comparison of zero against the result of a
10637 signed MOD operation whose second operand is a power of 2, make
10638 the MOD operation unsigned since it is simpler and equivalent. */
10639 if (integer_zerop (arg1)
10640 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10641 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10642 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10643 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10644 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10645 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10647 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10648 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10649 fold_convert_loc (loc, newtype,
10650 TREE_OPERAND (arg0, 0)),
10651 fold_convert_loc (loc, newtype,
10652 TREE_OPERAND (arg0, 1)));
10654 return fold_build2_loc (loc, code, type, newmod,
10655 fold_convert_loc (loc, newtype, arg1));
10658 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10659 C1 is a valid shift constant, and C2 is a power of two, i.e.
10660 a single bit. */
10661 if (TREE_CODE (arg0) == BIT_AND_EXPR
10662 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10663 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10664 == INTEGER_CST
10665 && integer_pow2p (TREE_OPERAND (arg0, 1))
10666 && integer_zerop (arg1))
10668 tree itype = TREE_TYPE (arg0);
10669 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10670 prec = TYPE_PRECISION (itype);
10672 /* Check for a valid shift count. */
10673 if (wi::ltu_p (wi::to_wide (arg001), prec))
10675 tree arg01 = TREE_OPERAND (arg0, 1);
10676 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10677 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10678 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10679 can be rewritten as (X & (C2 << C1)) != 0. */
10680 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10682 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10683 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10684 return fold_build2_loc (loc, code, type, tem,
10685 fold_convert_loc (loc, itype, arg1));
10687 /* Otherwise, for signed (arithmetic) shifts,
10688 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10689 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10690 else if (!TYPE_UNSIGNED (itype))
10691 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10692 arg000, build_int_cst (itype, 0));
10693 /* Otherwise, of unsigned (logical) shifts,
10694 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10695 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10696 else
10697 return omit_one_operand_loc (loc, type,
10698 code == EQ_EXPR ? integer_one_node
10699 : integer_zero_node,
10700 arg000);
10704 /* If this is a comparison of a field, we may be able to simplify it. */
10705 if ((TREE_CODE (arg0) == COMPONENT_REF
10706 || TREE_CODE (arg0) == BIT_FIELD_REF)
10707 /* Handle the constant case even without -O
10708 to make sure the warnings are given. */
10709 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10711 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10712 if (t1)
10713 return t1;
10716 /* Optimize comparisons of strlen vs zero to a compare of the
10717 first character of the string vs zero. To wit,
10718 strlen(ptr) == 0 => *ptr == 0
10719 strlen(ptr) != 0 => *ptr != 0
10720 Other cases should reduce to one of these two (or a constant)
10721 due to the return value of strlen being unsigned. */
10722 if (TREE_CODE (arg0) == CALL_EXPR
10723 && integer_zerop (arg1))
10725 tree fndecl = get_callee_fndecl (arg0);
10727 if (fndecl
10728 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10729 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10730 && call_expr_nargs (arg0) == 1
10731 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10733 tree iref = build_fold_indirect_ref_loc (loc,
10734 CALL_EXPR_ARG (arg0, 0));
10735 return fold_build2_loc (loc, code, type, iref,
10736 build_int_cst (TREE_TYPE (iref), 0));
10740 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10741 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10742 if (TREE_CODE (arg0) == RSHIFT_EXPR
10743 && integer_zerop (arg1)
10744 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10746 tree arg00 = TREE_OPERAND (arg0, 0);
10747 tree arg01 = TREE_OPERAND (arg0, 1);
10748 tree itype = TREE_TYPE (arg00);
10749 if (wi::to_wide (arg01) == element_precision (itype) - 1)
10751 if (TYPE_UNSIGNED (itype))
10753 itype = signed_type_for (itype);
10754 arg00 = fold_convert_loc (loc, itype, arg00);
10756 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10757 type, arg00, build_zero_cst (itype));
10761 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10762 (X & C) == 0 when C is a single bit. */
10763 if (TREE_CODE (arg0) == BIT_AND_EXPR
10764 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10765 && integer_zerop (arg1)
10766 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10768 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10769 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10770 TREE_OPERAND (arg0, 1));
10771 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10772 type, tem,
10773 fold_convert_loc (loc, TREE_TYPE (arg0),
10774 arg1));
10777 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10778 constant C is a power of two, i.e. a single bit. */
10779 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10780 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10781 && integer_zerop (arg1)
10782 && integer_pow2p (TREE_OPERAND (arg0, 1))
10783 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10784 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10786 tree arg00 = TREE_OPERAND (arg0, 0);
10787 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10788 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10791 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10792 when is C is a power of two, i.e. a single bit. */
10793 if (TREE_CODE (arg0) == BIT_AND_EXPR
10794 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10795 && integer_zerop (arg1)
10796 && integer_pow2p (TREE_OPERAND (arg0, 1))
10797 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10798 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10800 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10801 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10802 arg000, TREE_OPERAND (arg0, 1));
10803 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10804 tem, build_int_cst (TREE_TYPE (tem), 0));
10807 if (integer_zerop (arg1)
10808 && tree_expr_nonzero_p (arg0))
10810 tree res = constant_boolean_node (code==NE_EXPR, type);
10811 return omit_one_operand_loc (loc, type, res, arg0);
10814 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10815 if (TREE_CODE (arg0) == BIT_AND_EXPR
10816 && TREE_CODE (arg1) == BIT_AND_EXPR)
10818 tree arg00 = TREE_OPERAND (arg0, 0);
10819 tree arg01 = TREE_OPERAND (arg0, 1);
10820 tree arg10 = TREE_OPERAND (arg1, 0);
10821 tree arg11 = TREE_OPERAND (arg1, 1);
10822 tree itype = TREE_TYPE (arg0);
10824 if (operand_equal_p (arg01, arg11, 0))
10826 tem = fold_convert_loc (loc, itype, arg10);
10827 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10828 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10829 return fold_build2_loc (loc, code, type, tem,
10830 build_zero_cst (itype));
10832 if (operand_equal_p (arg01, arg10, 0))
10834 tem = fold_convert_loc (loc, itype, arg11);
10835 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10836 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10837 return fold_build2_loc (loc, code, type, tem,
10838 build_zero_cst (itype));
10840 if (operand_equal_p (arg00, arg11, 0))
10842 tem = fold_convert_loc (loc, itype, arg10);
10843 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10844 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10845 return fold_build2_loc (loc, code, type, tem,
10846 build_zero_cst (itype));
10848 if (operand_equal_p (arg00, arg10, 0))
10850 tem = fold_convert_loc (loc, itype, arg11);
10851 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10852 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10853 return fold_build2_loc (loc, code, type, tem,
10854 build_zero_cst (itype));
10858 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10859 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10861 tree arg00 = TREE_OPERAND (arg0, 0);
10862 tree arg01 = TREE_OPERAND (arg0, 1);
10863 tree arg10 = TREE_OPERAND (arg1, 0);
10864 tree arg11 = TREE_OPERAND (arg1, 1);
10865 tree itype = TREE_TYPE (arg0);
10867 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10868 operand_equal_p guarantees no side-effects so we don't need
10869 to use omit_one_operand on Z. */
10870 if (operand_equal_p (arg01, arg11, 0))
10871 return fold_build2_loc (loc, code, type, arg00,
10872 fold_convert_loc (loc, TREE_TYPE (arg00),
10873 arg10));
10874 if (operand_equal_p (arg01, arg10, 0))
10875 return fold_build2_loc (loc, code, type, arg00,
10876 fold_convert_loc (loc, TREE_TYPE (arg00),
10877 arg11));
10878 if (operand_equal_p (arg00, arg11, 0))
10879 return fold_build2_loc (loc, code, type, arg01,
10880 fold_convert_loc (loc, TREE_TYPE (arg01),
10881 arg10));
10882 if (operand_equal_p (arg00, arg10, 0))
10883 return fold_build2_loc (loc, code, type, arg01,
10884 fold_convert_loc (loc, TREE_TYPE (arg01),
10885 arg11));
10887 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10888 if (TREE_CODE (arg01) == INTEGER_CST
10889 && TREE_CODE (arg11) == INTEGER_CST)
10891 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10892 fold_convert_loc (loc, itype, arg11));
10893 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10894 return fold_build2_loc (loc, code, type, tem,
10895 fold_convert_loc (loc, itype, arg10));
10899 /* Attempt to simplify equality/inequality comparisons of complex
10900 values. Only lower the comparison if the result is known or
10901 can be simplified to a single scalar comparison. */
10902 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10903 || TREE_CODE (arg0) == COMPLEX_CST)
10904 && (TREE_CODE (arg1) == COMPLEX_EXPR
10905 || TREE_CODE (arg1) == COMPLEX_CST))
10907 tree real0, imag0, real1, imag1;
10908 tree rcond, icond;
10910 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10912 real0 = TREE_OPERAND (arg0, 0);
10913 imag0 = TREE_OPERAND (arg0, 1);
10915 else
10917 real0 = TREE_REALPART (arg0);
10918 imag0 = TREE_IMAGPART (arg0);
10921 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10923 real1 = TREE_OPERAND (arg1, 0);
10924 imag1 = TREE_OPERAND (arg1, 1);
10926 else
10928 real1 = TREE_REALPART (arg1);
10929 imag1 = TREE_IMAGPART (arg1);
10932 rcond = fold_binary_loc (loc, code, type, real0, real1);
10933 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10935 if (integer_zerop (rcond))
10937 if (code == EQ_EXPR)
10938 return omit_two_operands_loc (loc, type, boolean_false_node,
10939 imag0, imag1);
10940 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10942 else
10944 if (code == NE_EXPR)
10945 return omit_two_operands_loc (loc, type, boolean_true_node,
10946 imag0, imag1);
10947 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10951 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10952 if (icond && TREE_CODE (icond) == INTEGER_CST)
10954 if (integer_zerop (icond))
10956 if (code == EQ_EXPR)
10957 return omit_two_operands_loc (loc, type, boolean_false_node,
10958 real0, real1);
10959 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
10961 else
10963 if (code == NE_EXPR)
10964 return omit_two_operands_loc (loc, type, boolean_true_node,
10965 real0, real1);
10966 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
10971 return NULL_TREE;
10973 case LT_EXPR:
10974 case GT_EXPR:
10975 case LE_EXPR:
10976 case GE_EXPR:
10977 tem = fold_comparison (loc, code, type, op0, op1);
10978 if (tem != NULL_TREE)
10979 return tem;
10981 /* Transform comparisons of the form X +- C CMP X. */
10982 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
10983 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10984 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
10985 && !HONOR_SNANS (arg0))
10987 tree arg01 = TREE_OPERAND (arg0, 1);
10988 enum tree_code code0 = TREE_CODE (arg0);
10989 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
10991 /* (X - c) > X becomes false. */
10992 if (code == GT_EXPR
10993 && ((code0 == MINUS_EXPR && is_positive >= 0)
10994 || (code0 == PLUS_EXPR && is_positive <= 0)))
10995 return constant_boolean_node (0, type);
10997 /* Likewise (X + c) < X becomes false. */
10998 if (code == LT_EXPR
10999 && ((code0 == PLUS_EXPR && is_positive >= 0)
11000 || (code0 == MINUS_EXPR && is_positive <= 0)))
11001 return constant_boolean_node (0, type);
11003 /* Convert (X - c) <= X to true. */
11004 if (!HONOR_NANS (arg1)
11005 && code == LE_EXPR
11006 && ((code0 == MINUS_EXPR && is_positive >= 0)
11007 || (code0 == PLUS_EXPR && is_positive <= 0)))
11008 return constant_boolean_node (1, type);
11010 /* Convert (X + c) >= X to true. */
11011 if (!HONOR_NANS (arg1)
11012 && code == GE_EXPR
11013 && ((code0 == PLUS_EXPR && is_positive >= 0)
11014 || (code0 == MINUS_EXPR && is_positive <= 0)))
11015 return constant_boolean_node (1, type);
11018 /* If we are comparing an ABS_EXPR with a constant, we can
11019 convert all the cases into explicit comparisons, but they may
11020 well not be faster than doing the ABS and one comparison.
11021 But ABS (X) <= C is a range comparison, which becomes a subtraction
11022 and a comparison, and is probably faster. */
11023 if (code == LE_EXPR
11024 && TREE_CODE (arg1) == INTEGER_CST
11025 && TREE_CODE (arg0) == ABS_EXPR
11026 && ! TREE_SIDE_EFFECTS (arg0)
11027 && (tem = negate_expr (arg1)) != 0
11028 && TREE_CODE (tem) == INTEGER_CST
11029 && !TREE_OVERFLOW (tem))
11030 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11031 build2 (GE_EXPR, type,
11032 TREE_OPERAND (arg0, 0), tem),
11033 build2 (LE_EXPR, type,
11034 TREE_OPERAND (arg0, 0), arg1));
11036 /* Convert ABS_EXPR<x> >= 0 to true. */
11037 strict_overflow_p = false;
11038 if (code == GE_EXPR
11039 && (integer_zerop (arg1)
11040 || (! HONOR_NANS (arg0)
11041 && real_zerop (arg1)))
11042 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11044 if (strict_overflow_p)
11045 fold_overflow_warning (("assuming signed overflow does not occur "
11046 "when simplifying comparison of "
11047 "absolute value and zero"),
11048 WARN_STRICT_OVERFLOW_CONDITIONAL);
11049 return omit_one_operand_loc (loc, type,
11050 constant_boolean_node (true, type),
11051 arg0);
11054 /* Convert ABS_EXPR<x> < 0 to false. */
11055 strict_overflow_p = false;
11056 if (code == LT_EXPR
11057 && (integer_zerop (arg1) || real_zerop (arg1))
11058 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11060 if (strict_overflow_p)
11061 fold_overflow_warning (("assuming signed overflow does not occur "
11062 "when simplifying comparison of "
11063 "absolute value and zero"),
11064 WARN_STRICT_OVERFLOW_CONDITIONAL);
11065 return omit_one_operand_loc (loc, type,
11066 constant_boolean_node (false, type),
11067 arg0);
11070 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11071 and similarly for >= into !=. */
11072 if ((code == LT_EXPR || code == GE_EXPR)
11073 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11074 && TREE_CODE (arg1) == LSHIFT_EXPR
11075 && integer_onep (TREE_OPERAND (arg1, 0)))
11076 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11077 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11078 TREE_OPERAND (arg1, 1)),
11079 build_zero_cst (TREE_TYPE (arg0)));
11081 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11082 otherwise Y might be >= # of bits in X's type and thus e.g.
11083 (unsigned char) (1 << Y) for Y 15 might be 0.
11084 If the cast is widening, then 1 << Y should have unsigned type,
11085 otherwise if Y is number of bits in the signed shift type minus 1,
11086 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11087 31 might be 0xffffffff80000000. */
11088 if ((code == LT_EXPR || code == GE_EXPR)
11089 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11090 && CONVERT_EXPR_P (arg1)
11091 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11092 && (element_precision (TREE_TYPE (arg1))
11093 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11094 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11095 || (element_precision (TREE_TYPE (arg1))
11096 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11097 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11099 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11100 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11101 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11102 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11103 build_zero_cst (TREE_TYPE (arg0)));
11106 return NULL_TREE;
11108 case UNORDERED_EXPR:
11109 case ORDERED_EXPR:
11110 case UNLT_EXPR:
11111 case UNLE_EXPR:
11112 case UNGT_EXPR:
11113 case UNGE_EXPR:
11114 case UNEQ_EXPR:
11115 case LTGT_EXPR:
11116 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11118 tree targ0 = strip_float_extensions (arg0);
11119 tree targ1 = strip_float_extensions (arg1);
11120 tree newtype = TREE_TYPE (targ0);
11122 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11123 newtype = TREE_TYPE (targ1);
11125 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11126 return fold_build2_loc (loc, code, type,
11127 fold_convert_loc (loc, newtype, targ0),
11128 fold_convert_loc (loc, newtype, targ1));
11131 return NULL_TREE;
11133 case COMPOUND_EXPR:
11134 /* When pedantic, a compound expression can be neither an lvalue
11135 nor an integer constant expression. */
11136 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11137 return NULL_TREE;
11138 /* Don't let (0, 0) be null pointer constant. */
11139 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11140 : fold_convert_loc (loc, type, arg1);
11141 return pedantic_non_lvalue_loc (loc, tem);
11143 case ASSERT_EXPR:
11144 /* An ASSERT_EXPR should never be passed to fold_binary. */
11145 gcc_unreachable ();
11147 default:
11148 return NULL_TREE;
11149 } /* switch (code) */
11152 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11153 ((A & N) + B) & M -> (A + B) & M
11154 Similarly if (N & M) == 0,
11155 ((A | N) + B) & M -> (A + B) & M
11156 and for - instead of + (or unary - instead of +)
11157 and/or ^ instead of |.
11158 If B is constant and (B & M) == 0, fold into A & M.
11160 This function is a helper for match.pd patterns. Return non-NULL
11161 type in which the simplified operation should be performed only
11162 if any optimization is possible.
11164 ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
11165 then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
11166 Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
11167 +/-. */
11168 tree
11169 fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
11170 tree arg00, enum tree_code code00, tree arg000, tree arg001,
11171 tree arg01, enum tree_code code01, tree arg010, tree arg011,
11172 tree *pmop)
11174 gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
11175 gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
11176 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11177 if (~cst1 == 0
11178 || (cst1 & (cst1 + 1)) != 0
11179 || !INTEGRAL_TYPE_P (type)
11180 || (!TYPE_OVERFLOW_WRAPS (type)
11181 && TREE_CODE (type) != INTEGER_TYPE)
11182 || (wi::max_value (type) & cst1) != cst1)
11183 return NULL_TREE;
11185 enum tree_code codes[2] = { code00, code01 };
11186 tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
11187 int which = 0;
11188 wide_int cst0;
11190 /* Now we know that arg0 is (C + D) or (C - D) or -C and
11191 arg1 (M) is == (1LL << cst) - 1.
11192 Store C into PMOP[0] and D into PMOP[1]. */
11193 pmop[0] = arg00;
11194 pmop[1] = arg01;
11195 which = code != NEGATE_EXPR;
11197 for (; which >= 0; which--)
11198 switch (codes[which])
11200 case BIT_AND_EXPR:
11201 case BIT_IOR_EXPR:
11202 case BIT_XOR_EXPR:
11203 gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
11204 cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
11205 if (codes[which] == BIT_AND_EXPR)
11207 if (cst0 != cst1)
11208 break;
11210 else if (cst0 != 0)
11211 break;
11212 /* If C or D is of the form (A & N) where
11213 (N & M) == M, or of the form (A | N) or
11214 (A ^ N) where (N & M) == 0, replace it with A. */
11215 pmop[which] = arg0xx[2 * which];
11216 break;
11217 case ERROR_MARK:
11218 if (TREE_CODE (pmop[which]) != INTEGER_CST)
11219 break;
11220 /* If C or D is a N where (N & M) == 0, it can be
11221 omitted (replaced with 0). */
11222 if ((code == PLUS_EXPR
11223 || (code == MINUS_EXPR && which == 0))
11224 && (cst1 & wi::to_wide (pmop[which])) == 0)
11225 pmop[which] = build_int_cst (type, 0);
11226 /* Similarly, with C - N where (-N & M) == 0. */
11227 if (code == MINUS_EXPR
11228 && which == 1
11229 && (cst1 & -wi::to_wide (pmop[which])) == 0)
11230 pmop[which] = build_int_cst (type, 0);
11231 break;
11232 default:
11233 gcc_unreachable ();
11236 /* Only build anything new if we optimized one or both arguments above. */
11237 if (pmop[0] == arg00 && pmop[1] == arg01)
11238 return NULL_TREE;
11240 if (TYPE_OVERFLOW_WRAPS (type))
11241 return type;
11242 else
11243 return unsigned_type_for (type);
11246 /* Used by contains_label_[p1]. */
11248 struct contains_label_data
11250 hash_set<tree> *pset;
11251 bool inside_switch_p;
11254 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11255 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
11256 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
11258 static tree
11259 contains_label_1 (tree *tp, int *walk_subtrees, void *data)
11261 contains_label_data *d = (contains_label_data *) data;
11262 switch (TREE_CODE (*tp))
11264 case LABEL_EXPR:
11265 return *tp;
11267 case CASE_LABEL_EXPR:
11268 if (!d->inside_switch_p)
11269 return *tp;
11270 return NULL_TREE;
11272 case SWITCH_EXPR:
11273 if (!d->inside_switch_p)
11275 if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
11276 return *tp;
11277 d->inside_switch_p = true;
11278 if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
11279 return *tp;
11280 d->inside_switch_p = false;
11281 *walk_subtrees = 0;
11283 return NULL_TREE;
11285 case GOTO_EXPR:
11286 *walk_subtrees = 0;
11287 return NULL_TREE;
11289 default:
11290 return NULL_TREE;
11294 /* Return whether the sub-tree ST contains a label which is accessible from
11295 outside the sub-tree. */
11297 static bool
11298 contains_label_p (tree st)
11300 hash_set<tree> pset;
11301 contains_label_data data = { &pset, false };
11302 return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
11305 /* Fold a ternary expression of code CODE and type TYPE with operands
11306 OP0, OP1, and OP2. Return the folded expression if folding is
11307 successful. Otherwise, return NULL_TREE. */
11309 tree
11310 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11311 tree op0, tree op1, tree op2)
11313 tree tem;
11314 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11315 enum tree_code_class kind = TREE_CODE_CLASS (code);
11317 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11318 && TREE_CODE_LENGTH (code) == 3);
11320 /* If this is a commutative operation, and OP0 is a constant, move it
11321 to OP1 to reduce the number of tests below. */
11322 if (commutative_ternary_tree_code (code)
11323 && tree_swap_operands_p (op0, op1))
11324 return fold_build3_loc (loc, code, type, op1, op0, op2);
11326 tem = generic_simplify (loc, code, type, op0, op1, op2);
11327 if (tem)
11328 return tem;
11330 /* Strip any conversions that don't change the mode. This is safe
11331 for every expression, except for a comparison expression because
11332 its signedness is derived from its operands. So, in the latter
11333 case, only strip conversions that don't change the signedness.
11335 Note that this is done as an internal manipulation within the
11336 constant folder, in order to find the simplest representation of
11337 the arguments so that their form can be studied. In any cases,
11338 the appropriate type conversions should be put back in the tree
11339 that will get out of the constant folder. */
11340 if (op0)
11342 arg0 = op0;
11343 STRIP_NOPS (arg0);
11346 if (op1)
11348 arg1 = op1;
11349 STRIP_NOPS (arg1);
11352 if (op2)
11354 arg2 = op2;
11355 STRIP_NOPS (arg2);
11358 switch (code)
11360 case COMPONENT_REF:
11361 if (TREE_CODE (arg0) == CONSTRUCTOR
11362 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11364 unsigned HOST_WIDE_INT idx;
11365 tree field, value;
11366 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11367 if (field == arg1)
11368 return value;
11370 return NULL_TREE;
11372 case COND_EXPR:
11373 case VEC_COND_EXPR:
11374 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11375 so all simple results must be passed through pedantic_non_lvalue. */
11376 if (TREE_CODE (arg0) == INTEGER_CST)
11378 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11379 tem = integer_zerop (arg0) ? op2 : op1;
11380 /* Only optimize constant conditions when the selected branch
11381 has the same type as the COND_EXPR. This avoids optimizing
11382 away "c ? x : throw", where the throw has a void type.
11383 Avoid throwing away that operand which contains label. */
11384 if ((!TREE_SIDE_EFFECTS (unused_op)
11385 || !contains_label_p (unused_op))
11386 && (! VOID_TYPE_P (TREE_TYPE (tem))
11387 || VOID_TYPE_P (type)))
11388 return pedantic_non_lvalue_loc (loc, tem);
11389 return NULL_TREE;
11391 else if (TREE_CODE (arg0) == VECTOR_CST)
11393 unsigned HOST_WIDE_INT nelts;
11394 if ((TREE_CODE (arg1) == VECTOR_CST
11395 || TREE_CODE (arg1) == CONSTRUCTOR)
11396 && (TREE_CODE (arg2) == VECTOR_CST
11397 || TREE_CODE (arg2) == CONSTRUCTOR)
11398 && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
11400 vec_perm_builder sel (nelts, nelts, 1);
11401 for (unsigned int i = 0; i < nelts; i++)
11403 tree val = VECTOR_CST_ELT (arg0, i);
11404 if (integer_all_onesp (val))
11405 sel.quick_push (i);
11406 else if (integer_zerop (val))
11407 sel.quick_push (nelts + i);
11408 else /* Currently unreachable. */
11409 return NULL_TREE;
11411 vec_perm_indices indices (sel, 2, nelts);
11412 tree t = fold_vec_perm (type, arg1, arg2, indices);
11413 if (t != NULL_TREE)
11414 return t;
11418 /* If we have A op B ? A : C, we may be able to convert this to a
11419 simpler expression, depending on the operation and the values
11420 of B and C. Signed zeros prevent all of these transformations,
11421 for reasons given above each one.
11423 Also try swapping the arguments and inverting the conditional. */
11424 if (COMPARISON_CLASS_P (arg0)
11425 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
11426 && !HONOR_SIGNED_ZEROS (element_mode (op1)))
11428 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11429 if (tem)
11430 return tem;
11433 if (COMPARISON_CLASS_P (arg0)
11434 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11435 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11437 location_t loc0 = expr_location_or (arg0, loc);
11438 tem = fold_invert_truthvalue (loc0, arg0);
11439 if (tem && COMPARISON_CLASS_P (tem))
11441 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11442 if (tem)
11443 return tem;
11447 /* If the second operand is simpler than the third, swap them
11448 since that produces better jump optimization results. */
11449 if (truth_value_p (TREE_CODE (arg0))
11450 && tree_swap_operands_p (op1, op2))
11452 location_t loc0 = expr_location_or (arg0, loc);
11453 /* See if this can be inverted. If it can't, possibly because
11454 it was a floating-point inequality comparison, don't do
11455 anything. */
11456 tem = fold_invert_truthvalue (loc0, arg0);
11457 if (tem)
11458 return fold_build3_loc (loc, code, type, tem, op2, op1);
11461 /* Convert A ? 1 : 0 to simply A. */
11462 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11463 : (integer_onep (op1)
11464 && !VECTOR_TYPE_P (type)))
11465 && integer_zerop (op2)
11466 /* If we try to convert OP0 to our type, the
11467 call to fold will try to move the conversion inside
11468 a COND, which will recurse. In that case, the COND_EXPR
11469 is probably the best choice, so leave it alone. */
11470 && type == TREE_TYPE (arg0))
11471 return pedantic_non_lvalue_loc (loc, arg0);
11473 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11474 over COND_EXPR in cases such as floating point comparisons. */
11475 if (integer_zerop (op1)
11476 && code == COND_EXPR
11477 && integer_onep (op2)
11478 && !VECTOR_TYPE_P (type)
11479 && truth_value_p (TREE_CODE (arg0)))
11480 return pedantic_non_lvalue_loc (loc,
11481 fold_convert_loc (loc, type,
11482 invert_truthvalue_loc (loc,
11483 arg0)));
11485 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11486 if (TREE_CODE (arg0) == LT_EXPR
11487 && integer_zerop (TREE_OPERAND (arg0, 1))
11488 && integer_zerop (op2)
11489 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11491 /* sign_bit_p looks through both zero and sign extensions,
11492 but for this optimization only sign extensions are
11493 usable. */
11494 tree tem2 = TREE_OPERAND (arg0, 0);
11495 while (tem != tem2)
11497 if (TREE_CODE (tem2) != NOP_EXPR
11498 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11500 tem = NULL_TREE;
11501 break;
11503 tem2 = TREE_OPERAND (tem2, 0);
11505 /* sign_bit_p only checks ARG1 bits within A's precision.
11506 If <sign bit of A> has wider type than A, bits outside
11507 of A's precision in <sign bit of A> need to be checked.
11508 If they are all 0, this optimization needs to be done
11509 in unsigned A's type, if they are all 1 in signed A's type,
11510 otherwise this can't be done. */
11511 if (tem
11512 && TYPE_PRECISION (TREE_TYPE (tem))
11513 < TYPE_PRECISION (TREE_TYPE (arg1))
11514 && TYPE_PRECISION (TREE_TYPE (tem))
11515 < TYPE_PRECISION (type))
11517 int inner_width, outer_width;
11518 tree tem_type;
11520 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11521 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11522 if (outer_width > TYPE_PRECISION (type))
11523 outer_width = TYPE_PRECISION (type);
11525 wide_int mask = wi::shifted_mask
11526 (inner_width, outer_width - inner_width, false,
11527 TYPE_PRECISION (TREE_TYPE (arg1)));
11529 wide_int common = mask & wi::to_wide (arg1);
11530 if (common == mask)
11532 tem_type = signed_type_for (TREE_TYPE (tem));
11533 tem = fold_convert_loc (loc, tem_type, tem);
11535 else if (common == 0)
11537 tem_type = unsigned_type_for (TREE_TYPE (tem));
11538 tem = fold_convert_loc (loc, tem_type, tem);
11540 else
11541 tem = NULL;
11544 if (tem)
11545 return
11546 fold_convert_loc (loc, type,
11547 fold_build2_loc (loc, BIT_AND_EXPR,
11548 TREE_TYPE (tem), tem,
11549 fold_convert_loc (loc,
11550 TREE_TYPE (tem),
11551 arg1)));
11554 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11555 already handled above. */
11556 if (TREE_CODE (arg0) == BIT_AND_EXPR
11557 && integer_onep (TREE_OPERAND (arg0, 1))
11558 && integer_zerop (op2)
11559 && integer_pow2p (arg1))
11561 tree tem = TREE_OPERAND (arg0, 0);
11562 STRIP_NOPS (tem);
11563 if (TREE_CODE (tem) == RSHIFT_EXPR
11564 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11565 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11566 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11567 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11568 fold_convert_loc (loc, type,
11569 TREE_OPERAND (tem, 0)),
11570 op1);
11573 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11574 is probably obsolete because the first operand should be a
11575 truth value (that's why we have the two cases above), but let's
11576 leave it in until we can confirm this for all front-ends. */
11577 if (integer_zerop (op2)
11578 && TREE_CODE (arg0) == NE_EXPR
11579 && integer_zerop (TREE_OPERAND (arg0, 1))
11580 && integer_pow2p (arg1)
11581 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11582 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11583 arg1, OEP_ONLY_CONST))
11584 return pedantic_non_lvalue_loc (loc,
11585 fold_convert_loc (loc, type,
11586 TREE_OPERAND (arg0, 0)));
11588 /* Disable the transformations below for vectors, since
11589 fold_binary_op_with_conditional_arg may undo them immediately,
11590 yielding an infinite loop. */
11591 if (code == VEC_COND_EXPR)
11592 return NULL_TREE;
11594 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11595 if (integer_zerop (op2)
11596 && truth_value_p (TREE_CODE (arg0))
11597 && truth_value_p (TREE_CODE (arg1))
11598 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11599 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11600 : TRUTH_ANDIF_EXPR,
11601 type, fold_convert_loc (loc, type, arg0), op1);
11603 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11604 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11605 && truth_value_p (TREE_CODE (arg0))
11606 && truth_value_p (TREE_CODE (arg1))
11607 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11609 location_t loc0 = expr_location_or (arg0, loc);
11610 /* Only perform transformation if ARG0 is easily inverted. */
11611 tem = fold_invert_truthvalue (loc0, arg0);
11612 if (tem)
11613 return fold_build2_loc (loc, code == VEC_COND_EXPR
11614 ? BIT_IOR_EXPR
11615 : TRUTH_ORIF_EXPR,
11616 type, fold_convert_loc (loc, type, tem),
11617 op1);
11620 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11621 if (integer_zerop (arg1)
11622 && truth_value_p (TREE_CODE (arg0))
11623 && truth_value_p (TREE_CODE (op2))
11624 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11626 location_t loc0 = expr_location_or (arg0, loc);
11627 /* Only perform transformation if ARG0 is easily inverted. */
11628 tem = fold_invert_truthvalue (loc0, arg0);
11629 if (tem)
11630 return fold_build2_loc (loc, code == VEC_COND_EXPR
11631 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11632 type, fold_convert_loc (loc, type, tem),
11633 op2);
11636 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11637 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11638 && truth_value_p (TREE_CODE (arg0))
11639 && truth_value_p (TREE_CODE (op2))
11640 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11641 return fold_build2_loc (loc, code == VEC_COND_EXPR
11642 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11643 type, fold_convert_loc (loc, type, arg0), op2);
11645 return NULL_TREE;
11647 case CALL_EXPR:
11648 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11649 of fold_ternary on them. */
11650 gcc_unreachable ();
11652 case BIT_FIELD_REF:
11653 if (TREE_CODE (arg0) == VECTOR_CST
11654 && (type == TREE_TYPE (TREE_TYPE (arg0))
11655 || (VECTOR_TYPE_P (type)
11656 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
11657 && tree_fits_uhwi_p (op1)
11658 && tree_fits_uhwi_p (op2))
11660 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11661 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11662 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11663 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11665 if (n != 0
11666 && (idx % width) == 0
11667 && (n % width) == 0
11668 && known_le ((idx + n) / width,
11669 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
11671 idx = idx / width;
11672 n = n / width;
11674 if (TREE_CODE (arg0) == VECTOR_CST)
11676 if (n == 1)
11678 tem = VECTOR_CST_ELT (arg0, idx);
11679 if (VECTOR_TYPE_P (type))
11680 tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
11681 return tem;
11684 tree_vector_builder vals (type, n, 1);
11685 for (unsigned i = 0; i < n; ++i)
11686 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
11687 return vals.build ();
11692 /* On constants we can use native encode/interpret to constant
11693 fold (nearly) all BIT_FIELD_REFs. */
11694 if (CONSTANT_CLASS_P (arg0)
11695 && can_native_interpret_type_p (type)
11696 && BITS_PER_UNIT == 8
11697 && tree_fits_uhwi_p (op1)
11698 && tree_fits_uhwi_p (op2))
11700 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11701 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11702 /* Limit us to a reasonable amount of work. To relax the
11703 other limitations we need bit-shifting of the buffer
11704 and rounding up the size. */
11705 if (bitpos % BITS_PER_UNIT == 0
11706 && bitsize % BITS_PER_UNIT == 0
11707 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11709 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11710 unsigned HOST_WIDE_INT len
11711 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11712 bitpos / BITS_PER_UNIT);
11713 if (len > 0
11714 && len * BITS_PER_UNIT >= bitsize)
11716 tree v = native_interpret_expr (type, b,
11717 bitsize / BITS_PER_UNIT);
11718 if (v)
11719 return v;
11724 return NULL_TREE;
11726 case VEC_PERM_EXPR:
11727 if (TREE_CODE (arg2) == VECTOR_CST)
11729 /* Build a vector of integers from the tree mask. */
11730 vec_perm_builder builder;
11731 if (!tree_to_vec_perm_builder (&builder, arg2))
11732 return NULL_TREE;
11734 /* Create a vec_perm_indices for the integer vector. */
11735 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
11736 bool single_arg = (op0 == op1);
11737 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
11739 /* Check for cases that fold to OP0 or OP1 in their original
11740 element order. */
11741 if (sel.series_p (0, 1, 0, 1))
11742 return op0;
11743 if (sel.series_p (0, 1, nelts, 1))
11744 return op1;
11746 if (!single_arg)
11748 if (sel.all_from_input_p (0))
11749 op1 = op0;
11750 else if (sel.all_from_input_p (1))
11752 op0 = op1;
11753 sel.rotate_inputs (1);
11757 if ((TREE_CODE (op0) == VECTOR_CST
11758 || TREE_CODE (op0) == CONSTRUCTOR)
11759 && (TREE_CODE (op1) == VECTOR_CST
11760 || TREE_CODE (op1) == CONSTRUCTOR))
11762 tree t = fold_vec_perm (type, op0, op1, sel);
11763 if (t != NULL_TREE)
11764 return t;
11767 bool changed = (op0 == op1 && !single_arg);
11769 /* Generate a canonical form of the selector. */
11770 if (arg2 == op2 && sel.encoding () != builder)
11772 /* Some targets are deficient and fail to expand a single
11773 argument permutation while still allowing an equivalent
11774 2-argument version. */
11775 if (sel.ninputs () == 2
11776 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
11777 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11778 else
11780 vec_perm_indices sel2 (builder, 2, nelts);
11781 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
11782 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel2);
11783 else
11784 /* Not directly supported with either encoding,
11785 so use the preferred form. */
11786 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11788 changed = true;
11791 if (changed)
11792 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11794 return NULL_TREE;
11796 case BIT_INSERT_EXPR:
11797 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11798 if (TREE_CODE (arg0) == INTEGER_CST
11799 && TREE_CODE (arg1) == INTEGER_CST)
11801 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11802 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11803 wide_int tem = (wi::to_wide (arg0)
11804 & wi::shifted_mask (bitpos, bitsize, true,
11805 TYPE_PRECISION (type)));
11806 wide_int tem2
11807 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11808 bitsize), bitpos);
11809 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11811 else if (TREE_CODE (arg0) == VECTOR_CST
11812 && CONSTANT_CLASS_P (arg1)
11813 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11814 TREE_TYPE (arg1)))
11816 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11817 unsigned HOST_WIDE_INT elsize
11818 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11819 if (bitpos % elsize == 0)
11821 unsigned k = bitpos / elsize;
11822 unsigned HOST_WIDE_INT nelts;
11823 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11824 return arg0;
11825 else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
11827 tree_vector_builder elts (type, nelts, 1);
11828 elts.quick_grow (nelts);
11829 for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
11830 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
11831 return elts.build ();
11835 return NULL_TREE;
11837 default:
11838 return NULL_TREE;
11839 } /* switch (code) */
11842 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11843 of an array (or vector). */
11845 tree
11846 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11848 tree index_type = NULL_TREE;
11849 offset_int low_bound = 0;
11851 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11853 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11854 if (domain_type && TYPE_MIN_VALUE (domain_type))
11856 /* Static constructors for variably sized objects makes no sense. */
11857 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11858 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11859 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11863 if (index_type)
11864 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11865 TYPE_SIGN (index_type));
11867 offset_int index = low_bound - 1;
11868 if (index_type)
11869 index = wi::ext (index, TYPE_PRECISION (index_type),
11870 TYPE_SIGN (index_type));
11872 offset_int max_index;
11873 unsigned HOST_WIDE_INT cnt;
11874 tree cfield, cval;
11876 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11878 /* Array constructor might explicitly set index, or specify a range,
11879 or leave index NULL meaning that it is next index after previous
11880 one. */
11881 if (cfield)
11883 if (TREE_CODE (cfield) == INTEGER_CST)
11884 max_index = index = wi::to_offset (cfield);
11885 else
11887 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11888 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11889 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11892 else
11894 index += 1;
11895 if (index_type)
11896 index = wi::ext (index, TYPE_PRECISION (index_type),
11897 TYPE_SIGN (index_type));
11898 max_index = index;
11901 /* Do we have match? */
11902 if (wi::cmpu (access_index, index) >= 0
11903 && wi::cmpu (access_index, max_index) <= 0)
11904 return cval;
11906 return NULL_TREE;
11909 /* Perform constant folding and related simplification of EXPR.
11910 The related simplifications include x*1 => x, x*0 => 0, etc.,
11911 and application of the associative law.
11912 NOP_EXPR conversions may be removed freely (as long as we
11913 are careful not to change the type of the overall expression).
11914 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11915 but we can constant-fold them if they have constant operands. */
11917 #ifdef ENABLE_FOLD_CHECKING
11918 # define fold(x) fold_1 (x)
11919 static tree fold_1 (tree);
11920 static
11921 #endif
11922 tree
11923 fold (tree expr)
11925 const tree t = expr;
11926 enum tree_code code = TREE_CODE (t);
11927 enum tree_code_class kind = TREE_CODE_CLASS (code);
11928 tree tem;
11929 location_t loc = EXPR_LOCATION (expr);
11931 /* Return right away if a constant. */
11932 if (kind == tcc_constant)
11933 return t;
11935 /* CALL_EXPR-like objects with variable numbers of operands are
11936 treated specially. */
11937 if (kind == tcc_vl_exp)
11939 if (code == CALL_EXPR)
11941 tem = fold_call_expr (loc, expr, false);
11942 return tem ? tem : expr;
11944 return expr;
11947 if (IS_EXPR_CODE_CLASS (kind))
11949 tree type = TREE_TYPE (t);
11950 tree op0, op1, op2;
11952 switch (TREE_CODE_LENGTH (code))
11954 case 1:
11955 op0 = TREE_OPERAND (t, 0);
11956 tem = fold_unary_loc (loc, code, type, op0);
11957 return tem ? tem : expr;
11958 case 2:
11959 op0 = TREE_OPERAND (t, 0);
11960 op1 = TREE_OPERAND (t, 1);
11961 tem = fold_binary_loc (loc, code, type, op0, op1);
11962 return tem ? tem : expr;
11963 case 3:
11964 op0 = TREE_OPERAND (t, 0);
11965 op1 = TREE_OPERAND (t, 1);
11966 op2 = TREE_OPERAND (t, 2);
11967 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11968 return tem ? tem : expr;
11969 default:
11970 break;
11974 switch (code)
11976 case ARRAY_REF:
11978 tree op0 = TREE_OPERAND (t, 0);
11979 tree op1 = TREE_OPERAND (t, 1);
11981 if (TREE_CODE (op1) == INTEGER_CST
11982 && TREE_CODE (op0) == CONSTRUCTOR
11983 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11985 tree val = get_array_ctor_element_at_index (op0,
11986 wi::to_offset (op1));
11987 if (val)
11988 return val;
11991 return t;
11994 /* Return a VECTOR_CST if possible. */
11995 case CONSTRUCTOR:
11997 tree type = TREE_TYPE (t);
11998 if (TREE_CODE (type) != VECTOR_TYPE)
11999 return t;
12001 unsigned i;
12002 tree val;
12003 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
12004 if (! CONSTANT_CLASS_P (val))
12005 return t;
12007 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12010 case CONST_DECL:
12011 return fold (DECL_INITIAL (t));
12013 default:
12014 return t;
12015 } /* switch (code) */
12018 #ifdef ENABLE_FOLD_CHECKING
12019 #undef fold
12021 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12022 hash_table<nofree_ptr_hash<const tree_node> > *);
12023 static void fold_check_failed (const_tree, const_tree);
12024 void print_fold_checksum (const_tree);
12026 /* When --enable-checking=fold, compute a digest of expr before
12027 and after actual fold call to see if fold did not accidentally
12028 change original expr. */
12030 tree
12031 fold (tree expr)
12033 tree ret;
12034 struct md5_ctx ctx;
12035 unsigned char checksum_before[16], checksum_after[16];
12036 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12038 md5_init_ctx (&ctx);
12039 fold_checksum_tree (expr, &ctx, &ht);
12040 md5_finish_ctx (&ctx, checksum_before);
12041 ht.empty ();
12043 ret = fold_1 (expr);
12045 md5_init_ctx (&ctx);
12046 fold_checksum_tree (expr, &ctx, &ht);
12047 md5_finish_ctx (&ctx, checksum_after);
12049 if (memcmp (checksum_before, checksum_after, 16))
12050 fold_check_failed (expr, ret);
12052 return ret;
12055 void
12056 print_fold_checksum (const_tree expr)
12058 struct md5_ctx ctx;
12059 unsigned char checksum[16], cnt;
12060 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12062 md5_init_ctx (&ctx);
12063 fold_checksum_tree (expr, &ctx, &ht);
12064 md5_finish_ctx (&ctx, checksum);
12065 for (cnt = 0; cnt < 16; ++cnt)
12066 fprintf (stderr, "%02x", checksum[cnt]);
12067 putc ('\n', stderr);
12070 static void
12071 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12073 internal_error ("fold check: original tree changed by fold");
12076 static void
12077 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12078 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12080 const tree_node **slot;
12081 enum tree_code code;
12082 union tree_node buf;
12083 int i, len;
12085 recursive_label:
12086 if (expr == NULL)
12087 return;
12088 slot = ht->find_slot (expr, INSERT);
12089 if (*slot != NULL)
12090 return;
12091 *slot = expr;
12092 code = TREE_CODE (expr);
12093 if (TREE_CODE_CLASS (code) == tcc_declaration
12094 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12096 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12097 memcpy ((char *) &buf, expr, tree_size (expr));
12098 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12099 buf.decl_with_vis.symtab_node = NULL;
12100 expr = (tree) &buf;
12102 else if (TREE_CODE_CLASS (code) == tcc_type
12103 && (TYPE_POINTER_TO (expr)
12104 || TYPE_REFERENCE_TO (expr)
12105 || TYPE_CACHED_VALUES_P (expr)
12106 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12107 || TYPE_NEXT_VARIANT (expr)
12108 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12110 /* Allow these fields to be modified. */
12111 tree tmp;
12112 memcpy ((char *) &buf, expr, tree_size (expr));
12113 expr = tmp = (tree) &buf;
12114 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12115 TYPE_POINTER_TO (tmp) = NULL;
12116 TYPE_REFERENCE_TO (tmp) = NULL;
12117 TYPE_NEXT_VARIANT (tmp) = NULL;
12118 TYPE_ALIAS_SET (tmp) = -1;
12119 if (TYPE_CACHED_VALUES_P (tmp))
12121 TYPE_CACHED_VALUES_P (tmp) = 0;
12122 TYPE_CACHED_VALUES (tmp) = NULL;
12125 md5_process_bytes (expr, tree_size (expr), ctx);
12126 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12127 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12128 if (TREE_CODE_CLASS (code) != tcc_type
12129 && TREE_CODE_CLASS (code) != tcc_declaration
12130 && code != TREE_LIST
12131 && code != SSA_NAME
12132 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12133 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12134 switch (TREE_CODE_CLASS (code))
12136 case tcc_constant:
12137 switch (code)
12139 case STRING_CST:
12140 md5_process_bytes (TREE_STRING_POINTER (expr),
12141 TREE_STRING_LENGTH (expr), ctx);
12142 break;
12143 case COMPLEX_CST:
12144 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12145 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12146 break;
12147 case VECTOR_CST:
12148 len = vector_cst_encoded_nelts (expr);
12149 for (i = 0; i < len; ++i)
12150 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
12151 break;
12152 default:
12153 break;
12155 break;
12156 case tcc_exceptional:
12157 switch (code)
12159 case TREE_LIST:
12160 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12161 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12162 expr = TREE_CHAIN (expr);
12163 goto recursive_label;
12164 break;
12165 case TREE_VEC:
12166 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12167 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12168 break;
12169 default:
12170 break;
12172 break;
12173 case tcc_expression:
12174 case tcc_reference:
12175 case tcc_comparison:
12176 case tcc_unary:
12177 case tcc_binary:
12178 case tcc_statement:
12179 case tcc_vl_exp:
12180 len = TREE_OPERAND_LENGTH (expr);
12181 for (i = 0; i < len; ++i)
12182 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12183 break;
12184 case tcc_declaration:
12185 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12186 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12187 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12189 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12190 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12191 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12192 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12193 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12196 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12198 if (TREE_CODE (expr) == FUNCTION_DECL)
12200 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12201 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12203 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12205 break;
12206 case tcc_type:
12207 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12208 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12209 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12210 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12211 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12212 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12213 if (INTEGRAL_TYPE_P (expr)
12214 || SCALAR_FLOAT_TYPE_P (expr))
12216 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12217 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12219 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12220 if (TREE_CODE (expr) == RECORD_TYPE
12221 || TREE_CODE (expr) == UNION_TYPE
12222 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12223 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12224 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12225 break;
12226 default:
12227 break;
12231 /* Helper function for outputting the checksum of a tree T. When
12232 debugging with gdb, you can "define mynext" to be "next" followed
12233 by "call debug_fold_checksum (op0)", then just trace down till the
12234 outputs differ. */
12236 DEBUG_FUNCTION void
12237 debug_fold_checksum (const_tree t)
12239 int i;
12240 unsigned char checksum[16];
12241 struct md5_ctx ctx;
12242 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12244 md5_init_ctx (&ctx);
12245 fold_checksum_tree (t, &ctx, &ht);
12246 md5_finish_ctx (&ctx, checksum);
12247 ht.empty ();
12249 for (i = 0; i < 16; i++)
12250 fprintf (stderr, "%d ", checksum[i]);
12252 fprintf (stderr, "\n");
12255 #endif
12257 /* Fold a unary tree expression with code CODE of type TYPE with an
12258 operand OP0. LOC is the location of the resulting expression.
12259 Return a folded expression if successful. Otherwise, return a tree
12260 expression with code CODE of type TYPE with an operand OP0. */
12262 tree
12263 fold_build1_loc (location_t loc,
12264 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12266 tree tem;
12267 #ifdef ENABLE_FOLD_CHECKING
12268 unsigned char checksum_before[16], checksum_after[16];
12269 struct md5_ctx ctx;
12270 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12272 md5_init_ctx (&ctx);
12273 fold_checksum_tree (op0, &ctx, &ht);
12274 md5_finish_ctx (&ctx, checksum_before);
12275 ht.empty ();
12276 #endif
12278 tem = fold_unary_loc (loc, code, type, op0);
12279 if (!tem)
12280 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12282 #ifdef ENABLE_FOLD_CHECKING
12283 md5_init_ctx (&ctx);
12284 fold_checksum_tree (op0, &ctx, &ht);
12285 md5_finish_ctx (&ctx, checksum_after);
12287 if (memcmp (checksum_before, checksum_after, 16))
12288 fold_check_failed (op0, tem);
12289 #endif
12290 return tem;
12293 /* Fold a binary tree expression with code CODE of type TYPE with
12294 operands OP0 and OP1. LOC is the location of the resulting
12295 expression. Return a folded expression if successful. Otherwise,
12296 return a tree expression with code CODE of type TYPE with operands
12297 OP0 and OP1. */
12299 tree
12300 fold_build2_loc (location_t loc,
12301 enum tree_code code, tree type, tree op0, tree op1
12302 MEM_STAT_DECL)
12304 tree tem;
12305 #ifdef ENABLE_FOLD_CHECKING
12306 unsigned char checksum_before_op0[16],
12307 checksum_before_op1[16],
12308 checksum_after_op0[16],
12309 checksum_after_op1[16];
12310 struct md5_ctx ctx;
12311 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12313 md5_init_ctx (&ctx);
12314 fold_checksum_tree (op0, &ctx, &ht);
12315 md5_finish_ctx (&ctx, checksum_before_op0);
12316 ht.empty ();
12318 md5_init_ctx (&ctx);
12319 fold_checksum_tree (op1, &ctx, &ht);
12320 md5_finish_ctx (&ctx, checksum_before_op1);
12321 ht.empty ();
12322 #endif
12324 tem = fold_binary_loc (loc, code, type, op0, op1);
12325 if (!tem)
12326 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12328 #ifdef ENABLE_FOLD_CHECKING
12329 md5_init_ctx (&ctx);
12330 fold_checksum_tree (op0, &ctx, &ht);
12331 md5_finish_ctx (&ctx, checksum_after_op0);
12332 ht.empty ();
12334 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12335 fold_check_failed (op0, tem);
12337 md5_init_ctx (&ctx);
12338 fold_checksum_tree (op1, &ctx, &ht);
12339 md5_finish_ctx (&ctx, checksum_after_op1);
12341 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12342 fold_check_failed (op1, tem);
12343 #endif
12344 return tem;
12347 /* Fold a ternary tree expression with code CODE of type TYPE with
12348 operands OP0, OP1, and OP2. Return a folded expression if
12349 successful. Otherwise, return a tree expression with code CODE of
12350 type TYPE with operands OP0, OP1, and OP2. */
12352 tree
12353 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12354 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12356 tree tem;
12357 #ifdef ENABLE_FOLD_CHECKING
12358 unsigned char checksum_before_op0[16],
12359 checksum_before_op1[16],
12360 checksum_before_op2[16],
12361 checksum_after_op0[16],
12362 checksum_after_op1[16],
12363 checksum_after_op2[16];
12364 struct md5_ctx ctx;
12365 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12367 md5_init_ctx (&ctx);
12368 fold_checksum_tree (op0, &ctx, &ht);
12369 md5_finish_ctx (&ctx, checksum_before_op0);
12370 ht.empty ();
12372 md5_init_ctx (&ctx);
12373 fold_checksum_tree (op1, &ctx, &ht);
12374 md5_finish_ctx (&ctx, checksum_before_op1);
12375 ht.empty ();
12377 md5_init_ctx (&ctx);
12378 fold_checksum_tree (op2, &ctx, &ht);
12379 md5_finish_ctx (&ctx, checksum_before_op2);
12380 ht.empty ();
12381 #endif
12383 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12384 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12385 if (!tem)
12386 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12388 #ifdef ENABLE_FOLD_CHECKING
12389 md5_init_ctx (&ctx);
12390 fold_checksum_tree (op0, &ctx, &ht);
12391 md5_finish_ctx (&ctx, checksum_after_op0);
12392 ht.empty ();
12394 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12395 fold_check_failed (op0, tem);
12397 md5_init_ctx (&ctx);
12398 fold_checksum_tree (op1, &ctx, &ht);
12399 md5_finish_ctx (&ctx, checksum_after_op1);
12400 ht.empty ();
12402 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12403 fold_check_failed (op1, tem);
12405 md5_init_ctx (&ctx);
12406 fold_checksum_tree (op2, &ctx, &ht);
12407 md5_finish_ctx (&ctx, checksum_after_op2);
12409 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12410 fold_check_failed (op2, tem);
12411 #endif
12412 return tem;
12415 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12416 arguments in ARGARRAY, and a null static chain.
12417 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12418 of type TYPE from the given operands as constructed by build_call_array. */
12420 tree
12421 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12422 int nargs, tree *argarray)
12424 tree tem;
12425 #ifdef ENABLE_FOLD_CHECKING
12426 unsigned char checksum_before_fn[16],
12427 checksum_before_arglist[16],
12428 checksum_after_fn[16],
12429 checksum_after_arglist[16];
12430 struct md5_ctx ctx;
12431 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12432 int i;
12434 md5_init_ctx (&ctx);
12435 fold_checksum_tree (fn, &ctx, &ht);
12436 md5_finish_ctx (&ctx, checksum_before_fn);
12437 ht.empty ();
12439 md5_init_ctx (&ctx);
12440 for (i = 0; i < nargs; i++)
12441 fold_checksum_tree (argarray[i], &ctx, &ht);
12442 md5_finish_ctx (&ctx, checksum_before_arglist);
12443 ht.empty ();
12444 #endif
12446 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12447 if (!tem)
12448 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12450 #ifdef ENABLE_FOLD_CHECKING
12451 md5_init_ctx (&ctx);
12452 fold_checksum_tree (fn, &ctx, &ht);
12453 md5_finish_ctx (&ctx, checksum_after_fn);
12454 ht.empty ();
12456 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12457 fold_check_failed (fn, tem);
12459 md5_init_ctx (&ctx);
12460 for (i = 0; i < nargs; i++)
12461 fold_checksum_tree (argarray[i], &ctx, &ht);
12462 md5_finish_ctx (&ctx, checksum_after_arglist);
12464 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12465 fold_check_failed (NULL_TREE, tem);
12466 #endif
12467 return tem;
12470 /* Perform constant folding and related simplification of initializer
12471 expression EXPR. These behave identically to "fold_buildN" but ignore
12472 potential run-time traps and exceptions that fold must preserve. */
12474 #define START_FOLD_INIT \
12475 int saved_signaling_nans = flag_signaling_nans;\
12476 int saved_trapping_math = flag_trapping_math;\
12477 int saved_rounding_math = flag_rounding_math;\
12478 int saved_trapv = flag_trapv;\
12479 int saved_folding_initializer = folding_initializer;\
12480 flag_signaling_nans = 0;\
12481 flag_trapping_math = 0;\
12482 flag_rounding_math = 0;\
12483 flag_trapv = 0;\
12484 folding_initializer = 1;
12486 #define END_FOLD_INIT \
12487 flag_signaling_nans = saved_signaling_nans;\
12488 flag_trapping_math = saved_trapping_math;\
12489 flag_rounding_math = saved_rounding_math;\
12490 flag_trapv = saved_trapv;\
12491 folding_initializer = saved_folding_initializer;
12493 tree
12494 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12495 tree type, tree op)
12497 tree result;
12498 START_FOLD_INIT;
12500 result = fold_build1_loc (loc, code, type, op);
12502 END_FOLD_INIT;
12503 return result;
12506 tree
12507 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12508 tree type, tree op0, tree op1)
12510 tree result;
12511 START_FOLD_INIT;
12513 result = fold_build2_loc (loc, code, type, op0, op1);
12515 END_FOLD_INIT;
12516 return result;
12519 tree
12520 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12521 int nargs, tree *argarray)
12523 tree result;
12524 START_FOLD_INIT;
12526 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12528 END_FOLD_INIT;
12529 return result;
12532 #undef START_FOLD_INIT
12533 #undef END_FOLD_INIT
12535 /* Determine if first argument is a multiple of second argument. Return 0 if
12536 it is not, or we cannot easily determined it to be.
12538 An example of the sort of thing we care about (at this point; this routine
12539 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12540 fold cases do now) is discovering that
12542 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12544 is a multiple of
12546 SAVE_EXPR (J * 8)
12548 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12550 This code also handles discovering that
12552 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12554 is a multiple of 8 so we don't have to worry about dealing with a
12555 possible remainder.
12557 Note that we *look* inside a SAVE_EXPR only to determine how it was
12558 calculated; it is not safe for fold to do much of anything else with the
12559 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12560 at run time. For example, the latter example above *cannot* be implemented
12561 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12562 evaluation time of the original SAVE_EXPR is not necessarily the same at
12563 the time the new expression is evaluated. The only optimization of this
12564 sort that would be valid is changing
12566 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12568 divided by 8 to
12570 SAVE_EXPR (I) * SAVE_EXPR (J)
12572 (where the same SAVE_EXPR (J) is used in the original and the
12573 transformed version). */
12576 multiple_of_p (tree type, const_tree top, const_tree bottom)
12578 gimple *stmt;
12579 tree t1, op1, op2;
12581 if (operand_equal_p (top, bottom, 0))
12582 return 1;
12584 if (TREE_CODE (type) != INTEGER_TYPE)
12585 return 0;
12587 switch (TREE_CODE (top))
12589 case BIT_AND_EXPR:
12590 /* Bitwise and provides a power of two multiple. If the mask is
12591 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12592 if (!integer_pow2p (bottom))
12593 return 0;
12594 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12595 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12597 case MULT_EXPR:
12598 if (TREE_CODE (bottom) == INTEGER_CST)
12600 op1 = TREE_OPERAND (top, 0);
12601 op2 = TREE_OPERAND (top, 1);
12602 if (TREE_CODE (op1) == INTEGER_CST)
12603 std::swap (op1, op2);
12604 if (TREE_CODE (op2) == INTEGER_CST)
12606 if (multiple_of_p (type, op2, bottom))
12607 return 1;
12608 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
12609 if (multiple_of_p (type, bottom, op2))
12611 widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
12612 wi::to_widest (op2));
12613 if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
12615 op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
12616 return multiple_of_p (type, op1, op2);
12619 return multiple_of_p (type, op1, bottom);
12622 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12623 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12625 case MINUS_EXPR:
12626 /* It is impossible to prove if op0 - op1 is multiple of bottom
12627 precisely, so be conservative here checking if both op0 and op1
12628 are multiple of bottom. Note we check the second operand first
12629 since it's usually simpler. */
12630 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12631 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12633 case PLUS_EXPR:
12634 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12635 as op0 - 3 if the expression has unsigned type. For example,
12636 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12637 op1 = TREE_OPERAND (top, 1);
12638 if (TYPE_UNSIGNED (type)
12639 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12640 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12641 return (multiple_of_p (type, op1, bottom)
12642 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12644 case LSHIFT_EXPR:
12645 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12647 op1 = TREE_OPERAND (top, 1);
12648 /* const_binop may not detect overflow correctly,
12649 so check for it explicitly here. */
12650 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
12651 wi::to_wide (op1))
12652 && (t1 = fold_convert (type,
12653 const_binop (LSHIFT_EXPR, size_one_node,
12654 op1))) != 0
12655 && !TREE_OVERFLOW (t1))
12656 return multiple_of_p (type, t1, bottom);
12658 return 0;
12660 case NOP_EXPR:
12661 /* Can't handle conversions from non-integral or wider integral type. */
12662 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12663 || (TYPE_PRECISION (type)
12664 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12665 return 0;
12667 /* fall through */
12669 case SAVE_EXPR:
12670 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12672 case COND_EXPR:
12673 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12674 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12676 case INTEGER_CST:
12677 if (TREE_CODE (bottom) != INTEGER_CST
12678 || integer_zerop (bottom)
12679 || (TYPE_UNSIGNED (type)
12680 && (tree_int_cst_sgn (top) < 0
12681 || tree_int_cst_sgn (bottom) < 0)))
12682 return 0;
12683 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12684 SIGNED);
12686 case SSA_NAME:
12687 if (TREE_CODE (bottom) == INTEGER_CST
12688 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12689 && gimple_code (stmt) == GIMPLE_ASSIGN)
12691 enum tree_code code = gimple_assign_rhs_code (stmt);
12693 /* Check for special cases to see if top is defined as multiple
12694 of bottom:
12696 top = (X & ~(bottom - 1) ; bottom is power of 2
12700 Y = X % bottom
12701 top = X - Y. */
12702 if (code == BIT_AND_EXPR
12703 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12704 && TREE_CODE (op2) == INTEGER_CST
12705 && integer_pow2p (bottom)
12706 && wi::multiple_of_p (wi::to_widest (op2),
12707 wi::to_widest (bottom), UNSIGNED))
12708 return 1;
12710 op1 = gimple_assign_rhs1 (stmt);
12711 if (code == MINUS_EXPR
12712 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12713 && TREE_CODE (op2) == SSA_NAME
12714 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12715 && gimple_code (stmt) == GIMPLE_ASSIGN
12716 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12717 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12718 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12719 return 1;
12722 /* fall through */
12724 default:
12725 if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
12726 return multiple_p (wi::to_poly_widest (top),
12727 wi::to_poly_widest (bottom));
12729 return 0;
12733 #define tree_expr_nonnegative_warnv_p(X, Y) \
12734 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12736 #define RECURSE(X) \
12737 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12739 /* Return true if CODE or TYPE is known to be non-negative. */
12741 static bool
12742 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12744 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12745 && truth_value_p (code))
12746 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12747 have a signed:1 type (where the value is -1 and 0). */
12748 return true;
12749 return false;
12752 /* Return true if (CODE OP0) is known to be non-negative. If the return
12753 value is based on the assumption that signed overflow is undefined,
12754 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12755 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12757 bool
12758 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12759 bool *strict_overflow_p, int depth)
12761 if (TYPE_UNSIGNED (type))
12762 return true;
12764 switch (code)
12766 case ABS_EXPR:
12767 /* We can't return 1 if flag_wrapv is set because
12768 ABS_EXPR<INT_MIN> = INT_MIN. */
12769 if (!ANY_INTEGRAL_TYPE_P (type))
12770 return true;
12771 if (TYPE_OVERFLOW_UNDEFINED (type))
12773 *strict_overflow_p = true;
12774 return true;
12776 break;
12778 case NON_LVALUE_EXPR:
12779 case FLOAT_EXPR:
12780 case FIX_TRUNC_EXPR:
12781 return RECURSE (op0);
12783 CASE_CONVERT:
12785 tree inner_type = TREE_TYPE (op0);
12786 tree outer_type = type;
12788 if (TREE_CODE (outer_type) == REAL_TYPE)
12790 if (TREE_CODE (inner_type) == REAL_TYPE)
12791 return RECURSE (op0);
12792 if (INTEGRAL_TYPE_P (inner_type))
12794 if (TYPE_UNSIGNED (inner_type))
12795 return true;
12796 return RECURSE (op0);
12799 else if (INTEGRAL_TYPE_P (outer_type))
12801 if (TREE_CODE (inner_type) == REAL_TYPE)
12802 return RECURSE (op0);
12803 if (INTEGRAL_TYPE_P (inner_type))
12804 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12805 && TYPE_UNSIGNED (inner_type);
12808 break;
12810 default:
12811 return tree_simple_nonnegative_warnv_p (code, type);
12814 /* We don't know sign of `t', so be conservative and return false. */
12815 return false;
12818 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12819 value is based on the assumption that signed overflow is undefined,
12820 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12821 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12823 bool
12824 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12825 tree op1, bool *strict_overflow_p,
12826 int depth)
12828 if (TYPE_UNSIGNED (type))
12829 return true;
12831 switch (code)
12833 case POINTER_PLUS_EXPR:
12834 case PLUS_EXPR:
12835 if (FLOAT_TYPE_P (type))
12836 return RECURSE (op0) && RECURSE (op1);
12838 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12839 both unsigned and at least 2 bits shorter than the result. */
12840 if (TREE_CODE (type) == INTEGER_TYPE
12841 && TREE_CODE (op0) == NOP_EXPR
12842 && TREE_CODE (op1) == NOP_EXPR)
12844 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12845 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12846 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12847 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12849 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12850 TYPE_PRECISION (inner2)) + 1;
12851 return prec < TYPE_PRECISION (type);
12854 break;
12856 case MULT_EXPR:
12857 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12859 /* x * x is always non-negative for floating point x
12860 or without overflow. */
12861 if (operand_equal_p (op0, op1, 0)
12862 || (RECURSE (op0) && RECURSE (op1)))
12864 if (ANY_INTEGRAL_TYPE_P (type)
12865 && TYPE_OVERFLOW_UNDEFINED (type))
12866 *strict_overflow_p = true;
12867 return true;
12871 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12872 both unsigned and their total bits is shorter than the result. */
12873 if (TREE_CODE (type) == INTEGER_TYPE
12874 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12875 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12877 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12878 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12879 : TREE_TYPE (op0);
12880 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12881 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12882 : TREE_TYPE (op1);
12884 bool unsigned0 = TYPE_UNSIGNED (inner0);
12885 bool unsigned1 = TYPE_UNSIGNED (inner1);
12887 if (TREE_CODE (op0) == INTEGER_CST)
12888 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12890 if (TREE_CODE (op1) == INTEGER_CST)
12891 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12893 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12894 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12896 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12897 ? tree_int_cst_min_precision (op0, UNSIGNED)
12898 : TYPE_PRECISION (inner0);
12900 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12901 ? tree_int_cst_min_precision (op1, UNSIGNED)
12902 : TYPE_PRECISION (inner1);
12904 return precision0 + precision1 < TYPE_PRECISION (type);
12907 return false;
12909 case BIT_AND_EXPR:
12910 case MAX_EXPR:
12911 return RECURSE (op0) || RECURSE (op1);
12913 case BIT_IOR_EXPR:
12914 case BIT_XOR_EXPR:
12915 case MIN_EXPR:
12916 case RDIV_EXPR:
12917 case TRUNC_DIV_EXPR:
12918 case CEIL_DIV_EXPR:
12919 case FLOOR_DIV_EXPR:
12920 case ROUND_DIV_EXPR:
12921 return RECURSE (op0) && RECURSE (op1);
12923 case TRUNC_MOD_EXPR:
12924 return RECURSE (op0);
12926 case FLOOR_MOD_EXPR:
12927 return RECURSE (op1);
12929 case CEIL_MOD_EXPR:
12930 case ROUND_MOD_EXPR:
12931 default:
12932 return tree_simple_nonnegative_warnv_p (code, type);
12935 /* We don't know sign of `t', so be conservative and return false. */
12936 return false;
12939 /* Return true if T is known to be non-negative. If the return
12940 value is based on the assumption that signed overflow is undefined,
12941 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12942 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12944 bool
12945 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12947 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12948 return true;
12950 switch (TREE_CODE (t))
12952 case INTEGER_CST:
12953 return tree_int_cst_sgn (t) >= 0;
12955 case REAL_CST:
12956 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12958 case FIXED_CST:
12959 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12961 case COND_EXPR:
12962 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12964 case SSA_NAME:
12965 /* Limit the depth of recursion to avoid quadratic behavior.
12966 This is expected to catch almost all occurrences in practice.
12967 If this code misses important cases that unbounded recursion
12968 would not, passes that need this information could be revised
12969 to provide it through dataflow propagation. */
12970 return (!name_registered_for_update_p (t)
12971 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12972 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12973 strict_overflow_p, depth));
12975 default:
12976 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12980 /* Return true if T is known to be non-negative. If the return
12981 value is based on the assumption that signed overflow is undefined,
12982 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12983 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12985 bool
12986 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12987 bool *strict_overflow_p, int depth)
12989 switch (fn)
12991 CASE_CFN_ACOS:
12992 CASE_CFN_ACOSH:
12993 CASE_CFN_CABS:
12994 CASE_CFN_COSH:
12995 CASE_CFN_ERFC:
12996 CASE_CFN_EXP:
12997 CASE_CFN_EXP10:
12998 CASE_CFN_EXP2:
12999 CASE_CFN_FABS:
13000 CASE_CFN_FDIM:
13001 CASE_CFN_HYPOT:
13002 CASE_CFN_POW10:
13003 CASE_CFN_FFS:
13004 CASE_CFN_PARITY:
13005 CASE_CFN_POPCOUNT:
13006 CASE_CFN_CLZ:
13007 CASE_CFN_CLRSB:
13008 case CFN_BUILT_IN_BSWAP32:
13009 case CFN_BUILT_IN_BSWAP64:
13010 /* Always true. */
13011 return true;
13013 CASE_CFN_SQRT:
13014 CASE_CFN_SQRT_FN:
13015 /* sqrt(-0.0) is -0.0. */
13016 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13017 return true;
13018 return RECURSE (arg0);
13020 CASE_CFN_ASINH:
13021 CASE_CFN_ATAN:
13022 CASE_CFN_ATANH:
13023 CASE_CFN_CBRT:
13024 CASE_CFN_CEIL:
13025 CASE_CFN_CEIL_FN:
13026 CASE_CFN_ERF:
13027 CASE_CFN_EXPM1:
13028 CASE_CFN_FLOOR:
13029 CASE_CFN_FLOOR_FN:
13030 CASE_CFN_FMOD:
13031 CASE_CFN_FREXP:
13032 CASE_CFN_ICEIL:
13033 CASE_CFN_IFLOOR:
13034 CASE_CFN_IRINT:
13035 CASE_CFN_IROUND:
13036 CASE_CFN_LCEIL:
13037 CASE_CFN_LDEXP:
13038 CASE_CFN_LFLOOR:
13039 CASE_CFN_LLCEIL:
13040 CASE_CFN_LLFLOOR:
13041 CASE_CFN_LLRINT:
13042 CASE_CFN_LLROUND:
13043 CASE_CFN_LRINT:
13044 CASE_CFN_LROUND:
13045 CASE_CFN_MODF:
13046 CASE_CFN_NEARBYINT:
13047 CASE_CFN_NEARBYINT_FN:
13048 CASE_CFN_RINT:
13049 CASE_CFN_RINT_FN:
13050 CASE_CFN_ROUND:
13051 CASE_CFN_ROUND_FN:
13052 CASE_CFN_SCALB:
13053 CASE_CFN_SCALBLN:
13054 CASE_CFN_SCALBN:
13055 CASE_CFN_SIGNBIT:
13056 CASE_CFN_SIGNIFICAND:
13057 CASE_CFN_SINH:
13058 CASE_CFN_TANH:
13059 CASE_CFN_TRUNC:
13060 CASE_CFN_TRUNC_FN:
13061 /* True if the 1st argument is nonnegative. */
13062 return RECURSE (arg0);
13064 CASE_CFN_FMAX:
13065 CASE_CFN_FMAX_FN:
13066 /* True if the 1st OR 2nd arguments are nonnegative. */
13067 return RECURSE (arg0) || RECURSE (arg1);
13069 CASE_CFN_FMIN:
13070 CASE_CFN_FMIN_FN:
13071 /* True if the 1st AND 2nd arguments are nonnegative. */
13072 return RECURSE (arg0) && RECURSE (arg1);
13074 CASE_CFN_COPYSIGN:
13075 CASE_CFN_COPYSIGN_FN:
13076 /* True if the 2nd argument is nonnegative. */
13077 return RECURSE (arg1);
13079 CASE_CFN_POWI:
13080 /* True if the 1st argument is nonnegative or the second
13081 argument is an even integer. */
13082 if (TREE_CODE (arg1) == INTEGER_CST
13083 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13084 return true;
13085 return RECURSE (arg0);
13087 CASE_CFN_POW:
13088 /* True if the 1st argument is nonnegative or the second
13089 argument is an even integer valued real. */
13090 if (TREE_CODE (arg1) == REAL_CST)
13092 REAL_VALUE_TYPE c;
13093 HOST_WIDE_INT n;
13095 c = TREE_REAL_CST (arg1);
13096 n = real_to_integer (&c);
13097 if ((n & 1) == 0)
13099 REAL_VALUE_TYPE cint;
13100 real_from_integer (&cint, VOIDmode, n, SIGNED);
13101 if (real_identical (&c, &cint))
13102 return true;
13105 return RECURSE (arg0);
13107 default:
13108 break;
13110 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13113 /* Return true if T is known to be non-negative. If the return
13114 value is based on the assumption that signed overflow is undefined,
13115 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13116 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13118 static bool
13119 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13121 enum tree_code code = TREE_CODE (t);
13122 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13123 return true;
13125 switch (code)
13127 case TARGET_EXPR:
13129 tree temp = TARGET_EXPR_SLOT (t);
13130 t = TARGET_EXPR_INITIAL (t);
13132 /* If the initializer is non-void, then it's a normal expression
13133 that will be assigned to the slot. */
13134 if (!VOID_TYPE_P (t))
13135 return RECURSE (t);
13137 /* Otherwise, the initializer sets the slot in some way. One common
13138 way is an assignment statement at the end of the initializer. */
13139 while (1)
13141 if (TREE_CODE (t) == BIND_EXPR)
13142 t = expr_last (BIND_EXPR_BODY (t));
13143 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13144 || TREE_CODE (t) == TRY_CATCH_EXPR)
13145 t = expr_last (TREE_OPERAND (t, 0));
13146 else if (TREE_CODE (t) == STATEMENT_LIST)
13147 t = expr_last (t);
13148 else
13149 break;
13151 if (TREE_CODE (t) == MODIFY_EXPR
13152 && TREE_OPERAND (t, 0) == temp)
13153 return RECURSE (TREE_OPERAND (t, 1));
13155 return false;
13158 case CALL_EXPR:
13160 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13161 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13163 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13164 get_call_combined_fn (t),
13165 arg0,
13166 arg1,
13167 strict_overflow_p, depth);
13169 case COMPOUND_EXPR:
13170 case MODIFY_EXPR:
13171 return RECURSE (TREE_OPERAND (t, 1));
13173 case BIND_EXPR:
13174 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13176 case SAVE_EXPR:
13177 return RECURSE (TREE_OPERAND (t, 0));
13179 default:
13180 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13184 #undef RECURSE
13185 #undef tree_expr_nonnegative_warnv_p
13187 /* Return true if T is known to be non-negative. If the return
13188 value is based on the assumption that signed overflow is undefined,
13189 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13190 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13192 bool
13193 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13195 enum tree_code code;
13196 if (t == error_mark_node)
13197 return false;
13199 code = TREE_CODE (t);
13200 switch (TREE_CODE_CLASS (code))
13202 case tcc_binary:
13203 case tcc_comparison:
13204 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13205 TREE_TYPE (t),
13206 TREE_OPERAND (t, 0),
13207 TREE_OPERAND (t, 1),
13208 strict_overflow_p, depth);
13210 case tcc_unary:
13211 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13212 TREE_TYPE (t),
13213 TREE_OPERAND (t, 0),
13214 strict_overflow_p, depth);
13216 case tcc_constant:
13217 case tcc_declaration:
13218 case tcc_reference:
13219 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13221 default:
13222 break;
13225 switch (code)
13227 case TRUTH_AND_EXPR:
13228 case TRUTH_OR_EXPR:
13229 case TRUTH_XOR_EXPR:
13230 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13231 TREE_TYPE (t),
13232 TREE_OPERAND (t, 0),
13233 TREE_OPERAND (t, 1),
13234 strict_overflow_p, depth);
13235 case TRUTH_NOT_EXPR:
13236 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13237 TREE_TYPE (t),
13238 TREE_OPERAND (t, 0),
13239 strict_overflow_p, depth);
13241 case COND_EXPR:
13242 case CONSTRUCTOR:
13243 case OBJ_TYPE_REF:
13244 case ASSERT_EXPR:
13245 case ADDR_EXPR:
13246 case WITH_SIZE_EXPR:
13247 case SSA_NAME:
13248 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13250 default:
13251 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13255 /* Return true if `t' is known to be non-negative. Handle warnings
13256 about undefined signed overflow. */
13258 bool
13259 tree_expr_nonnegative_p (tree t)
13261 bool ret, strict_overflow_p;
13263 strict_overflow_p = false;
13264 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13265 if (strict_overflow_p)
13266 fold_overflow_warning (("assuming signed overflow does not occur when "
13267 "determining that expression is always "
13268 "non-negative"),
13269 WARN_STRICT_OVERFLOW_MISC);
13270 return ret;
13274 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13275 For floating point we further ensure that T is not denormal.
13276 Similar logic is present in nonzero_address in rtlanal.h.
13278 If the return value is based on the assumption that signed overflow
13279 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13280 change *STRICT_OVERFLOW_P. */
13282 bool
13283 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13284 bool *strict_overflow_p)
13286 switch (code)
13288 case ABS_EXPR:
13289 return tree_expr_nonzero_warnv_p (op0,
13290 strict_overflow_p);
13292 case NOP_EXPR:
13294 tree inner_type = TREE_TYPE (op0);
13295 tree outer_type = type;
13297 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13298 && tree_expr_nonzero_warnv_p (op0,
13299 strict_overflow_p));
13301 break;
13303 case NON_LVALUE_EXPR:
13304 return tree_expr_nonzero_warnv_p (op0,
13305 strict_overflow_p);
13307 default:
13308 break;
13311 return false;
13314 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13315 For floating point we further ensure that T is not denormal.
13316 Similar logic is present in nonzero_address in rtlanal.h.
13318 If the return value is based on the assumption that signed overflow
13319 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13320 change *STRICT_OVERFLOW_P. */
13322 bool
13323 tree_binary_nonzero_warnv_p (enum tree_code code,
13324 tree type,
13325 tree op0,
13326 tree op1, bool *strict_overflow_p)
13328 bool sub_strict_overflow_p;
13329 switch (code)
13331 case POINTER_PLUS_EXPR:
13332 case PLUS_EXPR:
13333 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13335 /* With the presence of negative values it is hard
13336 to say something. */
13337 sub_strict_overflow_p = false;
13338 if (!tree_expr_nonnegative_warnv_p (op0,
13339 &sub_strict_overflow_p)
13340 || !tree_expr_nonnegative_warnv_p (op1,
13341 &sub_strict_overflow_p))
13342 return false;
13343 /* One of operands must be positive and the other non-negative. */
13344 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13345 overflows, on a twos-complement machine the sum of two
13346 nonnegative numbers can never be zero. */
13347 return (tree_expr_nonzero_warnv_p (op0,
13348 strict_overflow_p)
13349 || tree_expr_nonzero_warnv_p (op1,
13350 strict_overflow_p));
13352 break;
13354 case MULT_EXPR:
13355 if (TYPE_OVERFLOW_UNDEFINED (type))
13357 if (tree_expr_nonzero_warnv_p (op0,
13358 strict_overflow_p)
13359 && tree_expr_nonzero_warnv_p (op1,
13360 strict_overflow_p))
13362 *strict_overflow_p = true;
13363 return true;
13366 break;
13368 case MIN_EXPR:
13369 sub_strict_overflow_p = false;
13370 if (tree_expr_nonzero_warnv_p (op0,
13371 &sub_strict_overflow_p)
13372 && tree_expr_nonzero_warnv_p (op1,
13373 &sub_strict_overflow_p))
13375 if (sub_strict_overflow_p)
13376 *strict_overflow_p = true;
13378 break;
13380 case MAX_EXPR:
13381 sub_strict_overflow_p = false;
13382 if (tree_expr_nonzero_warnv_p (op0,
13383 &sub_strict_overflow_p))
13385 if (sub_strict_overflow_p)
13386 *strict_overflow_p = true;
13388 /* When both operands are nonzero, then MAX must be too. */
13389 if (tree_expr_nonzero_warnv_p (op1,
13390 strict_overflow_p))
13391 return true;
13393 /* MAX where operand 0 is positive is positive. */
13394 return tree_expr_nonnegative_warnv_p (op0,
13395 strict_overflow_p);
13397 /* MAX where operand 1 is positive is positive. */
13398 else if (tree_expr_nonzero_warnv_p (op1,
13399 &sub_strict_overflow_p)
13400 && tree_expr_nonnegative_warnv_p (op1,
13401 &sub_strict_overflow_p))
13403 if (sub_strict_overflow_p)
13404 *strict_overflow_p = true;
13405 return true;
13407 break;
13409 case BIT_IOR_EXPR:
13410 return (tree_expr_nonzero_warnv_p (op1,
13411 strict_overflow_p)
13412 || tree_expr_nonzero_warnv_p (op0,
13413 strict_overflow_p));
13415 default:
13416 break;
13419 return false;
13422 /* Return true when T is an address and is known to be nonzero.
13423 For floating point we further ensure that T is not denormal.
13424 Similar logic is present in nonzero_address in rtlanal.h.
13426 If the return value is based on the assumption that signed overflow
13427 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13428 change *STRICT_OVERFLOW_P. */
13430 bool
13431 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13433 bool sub_strict_overflow_p;
13434 switch (TREE_CODE (t))
13436 case INTEGER_CST:
13437 return !integer_zerop (t);
13439 case ADDR_EXPR:
13441 tree base = TREE_OPERAND (t, 0);
13443 if (!DECL_P (base))
13444 base = get_base_address (base);
13446 if (base && TREE_CODE (base) == TARGET_EXPR)
13447 base = TARGET_EXPR_SLOT (base);
13449 if (!base)
13450 return false;
13452 /* For objects in symbol table check if we know they are non-zero.
13453 Don't do anything for variables and functions before symtab is built;
13454 it is quite possible that they will be declared weak later. */
13455 int nonzero_addr = maybe_nonzero_address (base);
13456 if (nonzero_addr >= 0)
13457 return nonzero_addr;
13459 /* Constants are never weak. */
13460 if (CONSTANT_CLASS_P (base))
13461 return true;
13463 return false;
13466 case COND_EXPR:
13467 sub_strict_overflow_p = false;
13468 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13469 &sub_strict_overflow_p)
13470 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13471 &sub_strict_overflow_p))
13473 if (sub_strict_overflow_p)
13474 *strict_overflow_p = true;
13475 return true;
13477 break;
13479 case SSA_NAME:
13480 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13481 break;
13482 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13484 default:
13485 break;
13487 return false;
13490 #define integer_valued_real_p(X) \
13491 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13493 #define RECURSE(X) \
13494 ((integer_valued_real_p) (X, depth + 1))
13496 /* Return true if the floating point result of (CODE OP0) has an
13497 integer value. We also allow +Inf, -Inf and NaN to be considered
13498 integer values. Return false for signaling NaN.
13500 DEPTH is the current nesting depth of the query. */
13502 bool
13503 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13505 switch (code)
13507 case FLOAT_EXPR:
13508 return true;
13510 case ABS_EXPR:
13511 return RECURSE (op0);
13513 CASE_CONVERT:
13515 tree type = TREE_TYPE (op0);
13516 if (TREE_CODE (type) == INTEGER_TYPE)
13517 return true;
13518 if (TREE_CODE (type) == REAL_TYPE)
13519 return RECURSE (op0);
13520 break;
13523 default:
13524 break;
13526 return false;
13529 /* Return true if the floating point result of (CODE OP0 OP1) has an
13530 integer value. We also allow +Inf, -Inf and NaN to be considered
13531 integer values. Return false for signaling NaN.
13533 DEPTH is the current nesting depth of the query. */
13535 bool
13536 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13538 switch (code)
13540 case PLUS_EXPR:
13541 case MINUS_EXPR:
13542 case MULT_EXPR:
13543 case MIN_EXPR:
13544 case MAX_EXPR:
13545 return RECURSE (op0) && RECURSE (op1);
13547 default:
13548 break;
13550 return false;
13553 /* Return true if the floating point result of calling FNDECL with arguments
13554 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13555 considered integer values. Return false for signaling NaN. If FNDECL
13556 takes fewer than 2 arguments, the remaining ARGn are null.
13558 DEPTH is the current nesting depth of the query. */
13560 bool
13561 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13563 switch (fn)
13565 CASE_CFN_CEIL:
13566 CASE_CFN_CEIL_FN:
13567 CASE_CFN_FLOOR:
13568 CASE_CFN_FLOOR_FN:
13569 CASE_CFN_NEARBYINT:
13570 CASE_CFN_NEARBYINT_FN:
13571 CASE_CFN_RINT:
13572 CASE_CFN_RINT_FN:
13573 CASE_CFN_ROUND:
13574 CASE_CFN_ROUND_FN:
13575 CASE_CFN_TRUNC:
13576 CASE_CFN_TRUNC_FN:
13577 return true;
13579 CASE_CFN_FMIN:
13580 CASE_CFN_FMIN_FN:
13581 CASE_CFN_FMAX:
13582 CASE_CFN_FMAX_FN:
13583 return RECURSE (arg0) && RECURSE (arg1);
13585 default:
13586 break;
13588 return false;
13591 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13592 has an integer value. We also allow +Inf, -Inf and NaN to be
13593 considered integer values. Return false for signaling NaN.
13595 DEPTH is the current nesting depth of the query. */
13597 bool
13598 integer_valued_real_single_p (tree t, int depth)
13600 switch (TREE_CODE (t))
13602 case REAL_CST:
13603 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13605 case COND_EXPR:
13606 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13608 case SSA_NAME:
13609 /* Limit the depth of recursion to avoid quadratic behavior.
13610 This is expected to catch almost all occurrences in practice.
13611 If this code misses important cases that unbounded recursion
13612 would not, passes that need this information could be revised
13613 to provide it through dataflow propagation. */
13614 return (!name_registered_for_update_p (t)
13615 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13616 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13617 depth));
13619 default:
13620 break;
13622 return false;
13625 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13626 has an integer value. We also allow +Inf, -Inf and NaN to be
13627 considered integer values. Return false for signaling NaN.
13629 DEPTH is the current nesting depth of the query. */
13631 static bool
13632 integer_valued_real_invalid_p (tree t, int depth)
13634 switch (TREE_CODE (t))
13636 case COMPOUND_EXPR:
13637 case MODIFY_EXPR:
13638 case BIND_EXPR:
13639 return RECURSE (TREE_OPERAND (t, 1));
13641 case SAVE_EXPR:
13642 return RECURSE (TREE_OPERAND (t, 0));
13644 default:
13645 break;
13647 return false;
13650 #undef RECURSE
13651 #undef integer_valued_real_p
13653 /* Return true if the floating point expression T has an integer value.
13654 We also allow +Inf, -Inf and NaN to be considered integer values.
13655 Return false for signaling NaN.
13657 DEPTH is the current nesting depth of the query. */
13659 bool
13660 integer_valued_real_p (tree t, int depth)
13662 if (t == error_mark_node)
13663 return false;
13665 tree_code code = TREE_CODE (t);
13666 switch (TREE_CODE_CLASS (code))
13668 case tcc_binary:
13669 case tcc_comparison:
13670 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13671 TREE_OPERAND (t, 1), depth);
13673 case tcc_unary:
13674 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13676 case tcc_constant:
13677 case tcc_declaration:
13678 case tcc_reference:
13679 return integer_valued_real_single_p (t, depth);
13681 default:
13682 break;
13685 switch (code)
13687 case COND_EXPR:
13688 case SSA_NAME:
13689 return integer_valued_real_single_p (t, depth);
13691 case CALL_EXPR:
13693 tree arg0 = (call_expr_nargs (t) > 0
13694 ? CALL_EXPR_ARG (t, 0)
13695 : NULL_TREE);
13696 tree arg1 = (call_expr_nargs (t) > 1
13697 ? CALL_EXPR_ARG (t, 1)
13698 : NULL_TREE);
13699 return integer_valued_real_call_p (get_call_combined_fn (t),
13700 arg0, arg1, depth);
13703 default:
13704 return integer_valued_real_invalid_p (t, depth);
13708 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13709 attempt to fold the expression to a constant without modifying TYPE,
13710 OP0 or OP1.
13712 If the expression could be simplified to a constant, then return
13713 the constant. If the expression would not be simplified to a
13714 constant, then return NULL_TREE. */
13716 tree
13717 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13719 tree tem = fold_binary (code, type, op0, op1);
13720 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13723 /* Given the components of a unary expression CODE, TYPE and OP0,
13724 attempt to fold the expression to a constant without modifying
13725 TYPE or OP0.
13727 If the expression could be simplified to a constant, then return
13728 the constant. If the expression would not be simplified to a
13729 constant, then return NULL_TREE. */
13731 tree
13732 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13734 tree tem = fold_unary (code, type, op0);
13735 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13738 /* If EXP represents referencing an element in a constant string
13739 (either via pointer arithmetic or array indexing), return the
13740 tree representing the value accessed, otherwise return NULL. */
13742 tree
13743 fold_read_from_constant_string (tree exp)
13745 if ((TREE_CODE (exp) == INDIRECT_REF
13746 || TREE_CODE (exp) == ARRAY_REF)
13747 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13749 tree exp1 = TREE_OPERAND (exp, 0);
13750 tree index;
13751 tree string;
13752 location_t loc = EXPR_LOCATION (exp);
13754 if (TREE_CODE (exp) == INDIRECT_REF)
13755 string = string_constant (exp1, &index);
13756 else
13758 tree low_bound = array_ref_low_bound (exp);
13759 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13761 /* Optimize the special-case of a zero lower bound.
13763 We convert the low_bound to sizetype to avoid some problems
13764 with constant folding. (E.g. suppose the lower bound is 1,
13765 and its mode is QI. Without the conversion,l (ARRAY
13766 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13767 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13768 if (! integer_zerop (low_bound))
13769 index = size_diffop_loc (loc, index,
13770 fold_convert_loc (loc, sizetype, low_bound));
13772 string = exp1;
13775 scalar_int_mode char_mode;
13776 if (string
13777 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13778 && TREE_CODE (string) == STRING_CST
13779 && TREE_CODE (index) == INTEGER_CST
13780 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13781 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13782 &char_mode)
13783 && GET_MODE_SIZE (char_mode) == 1)
13784 return build_int_cst_type (TREE_TYPE (exp),
13785 (TREE_STRING_POINTER (string)
13786 [TREE_INT_CST_LOW (index)]));
13788 return NULL;
13791 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13792 an integer constant, real, or fixed-point constant.
13794 TYPE is the type of the result. */
13796 static tree
13797 fold_negate_const (tree arg0, tree type)
13799 tree t = NULL_TREE;
13801 switch (TREE_CODE (arg0))
13803 case REAL_CST:
13804 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13805 break;
13807 case FIXED_CST:
13809 FIXED_VALUE_TYPE f;
13810 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13811 &(TREE_FIXED_CST (arg0)), NULL,
13812 TYPE_SATURATING (type));
13813 t = build_fixed (type, f);
13814 /* Propagate overflow flags. */
13815 if (overflow_p | TREE_OVERFLOW (arg0))
13816 TREE_OVERFLOW (t) = 1;
13817 break;
13820 default:
13821 if (poly_int_tree_p (arg0))
13823 wi::overflow_type overflow;
13824 poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
13825 t = force_fit_type (type, res, 1,
13826 (overflow && ! TYPE_UNSIGNED (type))
13827 || TREE_OVERFLOW (arg0));
13828 break;
13831 gcc_unreachable ();
13834 return t;
13837 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13838 an integer constant or real constant.
13840 TYPE is the type of the result. */
13842 tree
13843 fold_abs_const (tree arg0, tree type)
13845 tree t = NULL_TREE;
13847 switch (TREE_CODE (arg0))
13849 case INTEGER_CST:
13851 /* If the value is unsigned or non-negative, then the absolute value
13852 is the same as the ordinary value. */
13853 wide_int val = wi::to_wide (arg0);
13854 wi::overflow_type overflow = wi::OVF_NONE;
13855 if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
13858 /* If the value is negative, then the absolute value is
13859 its negation. */
13860 else
13861 val = wi::neg (val, &overflow);
13863 /* Force to the destination type, set TREE_OVERFLOW for signed
13864 TYPE only. */
13865 t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
13867 break;
13869 case REAL_CST:
13870 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13871 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13872 else
13873 t = arg0;
13874 break;
13876 default:
13877 gcc_unreachable ();
13880 return t;
13883 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13884 constant. TYPE is the type of the result. */
13886 static tree
13887 fold_not_const (const_tree arg0, tree type)
13889 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13891 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
13894 /* Given CODE, a relational operator, the target type, TYPE and two
13895 constant operands OP0 and OP1, return the result of the
13896 relational operation. If the result is not a compile time
13897 constant, then return NULL_TREE. */
13899 static tree
13900 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13902 int result, invert;
13904 /* From here on, the only cases we handle are when the result is
13905 known to be a constant. */
13907 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13909 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13910 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13912 /* Handle the cases where either operand is a NaN. */
13913 if (real_isnan (c0) || real_isnan (c1))
13915 switch (code)
13917 case EQ_EXPR:
13918 case ORDERED_EXPR:
13919 result = 0;
13920 break;
13922 case NE_EXPR:
13923 case UNORDERED_EXPR:
13924 case UNLT_EXPR:
13925 case UNLE_EXPR:
13926 case UNGT_EXPR:
13927 case UNGE_EXPR:
13928 case UNEQ_EXPR:
13929 result = 1;
13930 break;
13932 case LT_EXPR:
13933 case LE_EXPR:
13934 case GT_EXPR:
13935 case GE_EXPR:
13936 case LTGT_EXPR:
13937 if (flag_trapping_math)
13938 return NULL_TREE;
13939 result = 0;
13940 break;
13942 default:
13943 gcc_unreachable ();
13946 return constant_boolean_node (result, type);
13949 return constant_boolean_node (real_compare (code, c0, c1), type);
13952 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13954 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13955 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13956 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13959 /* Handle equality/inequality of complex constants. */
13960 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13962 tree rcond = fold_relational_const (code, type,
13963 TREE_REALPART (op0),
13964 TREE_REALPART (op1));
13965 tree icond = fold_relational_const (code, type,
13966 TREE_IMAGPART (op0),
13967 TREE_IMAGPART (op1));
13968 if (code == EQ_EXPR)
13969 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13970 else if (code == NE_EXPR)
13971 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13972 else
13973 return NULL_TREE;
13976 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13978 if (!VECTOR_TYPE_P (type))
13980 /* Have vector comparison with scalar boolean result. */
13981 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13982 && known_eq (VECTOR_CST_NELTS (op0),
13983 VECTOR_CST_NELTS (op1)));
13984 unsigned HOST_WIDE_INT nunits;
13985 if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
13986 return NULL_TREE;
13987 for (unsigned i = 0; i < nunits; i++)
13989 tree elem0 = VECTOR_CST_ELT (op0, i);
13990 tree elem1 = VECTOR_CST_ELT (op1, i);
13991 tree tmp = fold_relational_const (code, type, elem0, elem1);
13992 if (tmp == NULL_TREE)
13993 return NULL_TREE;
13994 if (integer_zerop (tmp))
13995 return constant_boolean_node (false, type);
13997 return constant_boolean_node (true, type);
13999 tree_vector_builder elts;
14000 if (!elts.new_binary_operation (type, op0, op1, false))
14001 return NULL_TREE;
14002 unsigned int count = elts.encoded_nelts ();
14003 for (unsigned i = 0; i < count; i++)
14005 tree elem_type = TREE_TYPE (type);
14006 tree elem0 = VECTOR_CST_ELT (op0, i);
14007 tree elem1 = VECTOR_CST_ELT (op1, i);
14009 tree tem = fold_relational_const (code, elem_type,
14010 elem0, elem1);
14012 if (tem == NULL_TREE)
14013 return NULL_TREE;
14015 elts.quick_push (build_int_cst (elem_type,
14016 integer_zerop (tem) ? 0 : -1));
14019 return elts.build ();
14022 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14024 To compute GT, swap the arguments and do LT.
14025 To compute GE, do LT and invert the result.
14026 To compute LE, swap the arguments, do LT and invert the result.
14027 To compute NE, do EQ and invert the result.
14029 Therefore, the code below must handle only EQ and LT. */
14031 if (code == LE_EXPR || code == GT_EXPR)
14033 std::swap (op0, op1);
14034 code = swap_tree_comparison (code);
14037 /* Note that it is safe to invert for real values here because we
14038 have already handled the one case that it matters. */
14040 invert = 0;
14041 if (code == NE_EXPR || code == GE_EXPR)
14043 invert = 1;
14044 code = invert_tree_comparison (code, false);
14047 /* Compute a result for LT or EQ if args permit;
14048 Otherwise return T. */
14049 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14051 if (code == EQ_EXPR)
14052 result = tree_int_cst_equal (op0, op1);
14053 else
14054 result = tree_int_cst_lt (op0, op1);
14056 else
14057 return NULL_TREE;
14059 if (invert)
14060 result ^= 1;
14061 return constant_boolean_node (result, type);
14064 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14065 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14066 itself. */
14068 tree
14069 fold_build_cleanup_point_expr (tree type, tree expr)
14071 /* If the expression does not have side effects then we don't have to wrap
14072 it with a cleanup point expression. */
14073 if (!TREE_SIDE_EFFECTS (expr))
14074 return expr;
14076 /* If the expression is a return, check to see if the expression inside the
14077 return has no side effects or the right hand side of the modify expression
14078 inside the return. If either don't have side effects set we don't need to
14079 wrap the expression in a cleanup point expression. Note we don't check the
14080 left hand side of the modify because it should always be a return decl. */
14081 if (TREE_CODE (expr) == RETURN_EXPR)
14083 tree op = TREE_OPERAND (expr, 0);
14084 if (!op || !TREE_SIDE_EFFECTS (op))
14085 return expr;
14086 op = TREE_OPERAND (op, 1);
14087 if (!TREE_SIDE_EFFECTS (op))
14088 return expr;
14091 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
14094 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14095 of an indirection through OP0, or NULL_TREE if no simplification is
14096 possible. */
14098 tree
14099 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14101 tree sub = op0;
14102 tree subtype;
14103 poly_uint64 const_op01;
14105 STRIP_NOPS (sub);
14106 subtype = TREE_TYPE (sub);
14107 if (!POINTER_TYPE_P (subtype)
14108 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
14109 return NULL_TREE;
14111 if (TREE_CODE (sub) == ADDR_EXPR)
14113 tree op = TREE_OPERAND (sub, 0);
14114 tree optype = TREE_TYPE (op);
14116 /* *&CONST_DECL -> to the value of the const decl. */
14117 if (TREE_CODE (op) == CONST_DECL)
14118 return DECL_INITIAL (op);
14119 /* *&p => p; make sure to handle *&"str"[cst] here. */
14120 if (type == optype)
14122 tree fop = fold_read_from_constant_string (op);
14123 if (fop)
14124 return fop;
14125 else
14126 return op;
14128 /* *(foo *)&fooarray => fooarray[0] */
14129 else if (TREE_CODE (optype) == ARRAY_TYPE
14130 && type == TREE_TYPE (optype)
14131 && (!in_gimple_form
14132 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14134 tree type_domain = TYPE_DOMAIN (optype);
14135 tree min_val = size_zero_node;
14136 if (type_domain && TYPE_MIN_VALUE (type_domain))
14137 min_val = TYPE_MIN_VALUE (type_domain);
14138 if (in_gimple_form
14139 && TREE_CODE (min_val) != INTEGER_CST)
14140 return NULL_TREE;
14141 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14142 NULL_TREE, NULL_TREE);
14144 /* *(foo *)&complexfoo => __real__ complexfoo */
14145 else if (TREE_CODE (optype) == COMPLEX_TYPE
14146 && type == TREE_TYPE (optype))
14147 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14148 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14149 else if (VECTOR_TYPE_P (optype)
14150 && type == TREE_TYPE (optype))
14152 tree part_width = TYPE_SIZE (type);
14153 tree index = bitsize_int (0);
14154 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
14155 index);
14159 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14160 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
14162 tree op00 = TREE_OPERAND (sub, 0);
14163 tree op01 = TREE_OPERAND (sub, 1);
14165 STRIP_NOPS (op00);
14166 if (TREE_CODE (op00) == ADDR_EXPR)
14168 tree op00type;
14169 op00 = TREE_OPERAND (op00, 0);
14170 op00type = TREE_TYPE (op00);
14172 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14173 if (VECTOR_TYPE_P (op00type)
14174 && type == TREE_TYPE (op00type)
14175 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
14176 but we want to treat offsets with MSB set as negative.
14177 For the code below negative offsets are invalid and
14178 TYPE_SIZE of the element is something unsigned, so
14179 check whether op01 fits into poly_int64, which implies
14180 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
14181 then just use poly_uint64 because we want to treat the
14182 value as unsigned. */
14183 && tree_fits_poly_int64_p (op01))
14185 tree part_width = TYPE_SIZE (type);
14186 poly_uint64 max_offset
14187 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14188 * TYPE_VECTOR_SUBPARTS (op00type));
14189 if (known_lt (const_op01, max_offset))
14191 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
14192 return fold_build3_loc (loc,
14193 BIT_FIELD_REF, type, op00,
14194 part_width, index);
14197 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14198 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14199 && type == TREE_TYPE (op00type))
14201 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
14202 const_op01))
14203 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14205 /* ((foo *)&fooarray)[1] => fooarray[1] */
14206 else if (TREE_CODE (op00type) == ARRAY_TYPE
14207 && type == TREE_TYPE (op00type))
14209 tree type_domain = TYPE_DOMAIN (op00type);
14210 tree min_val = size_zero_node;
14211 if (type_domain && TYPE_MIN_VALUE (type_domain))
14212 min_val = TYPE_MIN_VALUE (type_domain);
14213 poly_uint64 type_size, index;
14214 if (poly_int_tree_p (min_val)
14215 && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
14216 && multiple_p (const_op01, type_size, &index))
14218 poly_offset_int off = index + wi::to_poly_offset (min_val);
14219 op01 = wide_int_to_tree (sizetype, off);
14220 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14221 NULL_TREE, NULL_TREE);
14227 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14228 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14229 && type == TREE_TYPE (TREE_TYPE (subtype))
14230 && (!in_gimple_form
14231 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14233 tree type_domain;
14234 tree min_val = size_zero_node;
14235 sub = build_fold_indirect_ref_loc (loc, sub);
14236 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14237 if (type_domain && TYPE_MIN_VALUE (type_domain))
14238 min_val = TYPE_MIN_VALUE (type_domain);
14239 if (in_gimple_form
14240 && TREE_CODE (min_val) != INTEGER_CST)
14241 return NULL_TREE;
14242 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14243 NULL_TREE);
14246 return NULL_TREE;
14249 /* Builds an expression for an indirection through T, simplifying some
14250 cases. */
14252 tree
14253 build_fold_indirect_ref_loc (location_t loc, tree t)
14255 tree type = TREE_TYPE (TREE_TYPE (t));
14256 tree sub = fold_indirect_ref_1 (loc, type, t);
14258 if (sub)
14259 return sub;
14261 return build1_loc (loc, INDIRECT_REF, type, t);
14264 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14266 tree
14267 fold_indirect_ref_loc (location_t loc, tree t)
14269 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14271 if (sub)
14272 return sub;
14273 else
14274 return t;
14277 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14278 whose result is ignored. The type of the returned tree need not be
14279 the same as the original expression. */
14281 tree
14282 fold_ignored_result (tree t)
14284 if (!TREE_SIDE_EFFECTS (t))
14285 return integer_zero_node;
14287 for (;;)
14288 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14290 case tcc_unary:
14291 t = TREE_OPERAND (t, 0);
14292 break;
14294 case tcc_binary:
14295 case tcc_comparison:
14296 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14297 t = TREE_OPERAND (t, 0);
14298 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14299 t = TREE_OPERAND (t, 1);
14300 else
14301 return t;
14302 break;
14304 case tcc_expression:
14305 switch (TREE_CODE (t))
14307 case COMPOUND_EXPR:
14308 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14309 return t;
14310 t = TREE_OPERAND (t, 0);
14311 break;
14313 case COND_EXPR:
14314 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14315 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14316 return t;
14317 t = TREE_OPERAND (t, 0);
14318 break;
14320 default:
14321 return t;
14323 break;
14325 default:
14326 return t;
14330 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14332 tree
14333 round_up_loc (location_t loc, tree value, unsigned int divisor)
14335 tree div = NULL_TREE;
14337 if (divisor == 1)
14338 return value;
14340 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14341 have to do anything. Only do this when we are not given a const,
14342 because in that case, this check is more expensive than just
14343 doing it. */
14344 if (TREE_CODE (value) != INTEGER_CST)
14346 div = build_int_cst (TREE_TYPE (value), divisor);
14348 if (multiple_of_p (TREE_TYPE (value), value, div))
14349 return value;
14352 /* If divisor is a power of two, simplify this to bit manipulation. */
14353 if (pow2_or_zerop (divisor))
14355 if (TREE_CODE (value) == INTEGER_CST)
14357 wide_int val = wi::to_wide (value);
14358 bool overflow_p;
14360 if ((val & (divisor - 1)) == 0)
14361 return value;
14363 overflow_p = TREE_OVERFLOW (value);
14364 val += divisor - 1;
14365 val &= (int) -divisor;
14366 if (val == 0)
14367 overflow_p = true;
14369 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14371 else
14373 tree t;
14375 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14376 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14377 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14378 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14381 else
14383 if (!div)
14384 div = build_int_cst (TREE_TYPE (value), divisor);
14385 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14386 value = size_binop_loc (loc, MULT_EXPR, value, div);
14389 return value;
14392 /* Likewise, but round down. */
14394 tree
14395 round_down_loc (location_t loc, tree value, int divisor)
14397 tree div = NULL_TREE;
14399 gcc_assert (divisor > 0);
14400 if (divisor == 1)
14401 return value;
14403 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14404 have to do anything. Only do this when we are not given a const,
14405 because in that case, this check is more expensive than just
14406 doing it. */
14407 if (TREE_CODE (value) != INTEGER_CST)
14409 div = build_int_cst (TREE_TYPE (value), divisor);
14411 if (multiple_of_p (TREE_TYPE (value), value, div))
14412 return value;
14415 /* If divisor is a power of two, simplify this to bit manipulation. */
14416 if (pow2_or_zerop (divisor))
14418 tree t;
14420 t = build_int_cst (TREE_TYPE (value), -divisor);
14421 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14423 else
14425 if (!div)
14426 div = build_int_cst (TREE_TYPE (value), divisor);
14427 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14428 value = size_binop_loc (loc, MULT_EXPR, value, div);
14431 return value;
14434 /* Returns the pointer to the base of the object addressed by EXP and
14435 extracts the information about the offset of the access, storing it
14436 to PBITPOS and POFFSET. */
14438 static tree
14439 split_address_to_core_and_offset (tree exp,
14440 poly_int64_pod *pbitpos, tree *poffset)
14442 tree core;
14443 machine_mode mode;
14444 int unsignedp, reversep, volatilep;
14445 poly_int64 bitsize;
14446 location_t loc = EXPR_LOCATION (exp);
14448 if (TREE_CODE (exp) == ADDR_EXPR)
14450 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14451 poffset, &mode, &unsignedp, &reversep,
14452 &volatilep);
14453 core = build_fold_addr_expr_loc (loc, core);
14455 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14457 core = TREE_OPERAND (exp, 0);
14458 STRIP_NOPS (core);
14459 *pbitpos = 0;
14460 *poffset = TREE_OPERAND (exp, 1);
14461 if (poly_int_tree_p (*poffset))
14463 poly_offset_int tem
14464 = wi::sext (wi::to_poly_offset (*poffset),
14465 TYPE_PRECISION (TREE_TYPE (*poffset)));
14466 tem <<= LOG2_BITS_PER_UNIT;
14467 if (tem.to_shwi (pbitpos))
14468 *poffset = NULL_TREE;
14471 else
14473 core = exp;
14474 *pbitpos = 0;
14475 *poffset = NULL_TREE;
14478 return core;
14481 /* Returns true if addresses of E1 and E2 differ by a constant, false
14482 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14484 bool
14485 ptr_difference_const (tree e1, tree e2, poly_int64_pod *diff)
14487 tree core1, core2;
14488 poly_int64 bitpos1, bitpos2;
14489 tree toffset1, toffset2, tdiff, type;
14491 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14492 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14494 poly_int64 bytepos1, bytepos2;
14495 if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
14496 || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
14497 || !operand_equal_p (core1, core2, 0))
14498 return false;
14500 if (toffset1 && toffset2)
14502 type = TREE_TYPE (toffset1);
14503 if (type != TREE_TYPE (toffset2))
14504 toffset2 = fold_convert (type, toffset2);
14506 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14507 if (!cst_and_fits_in_hwi (tdiff))
14508 return false;
14510 *diff = int_cst_value (tdiff);
14512 else if (toffset1 || toffset2)
14514 /* If only one of the offsets is non-constant, the difference cannot
14515 be a constant. */
14516 return false;
14518 else
14519 *diff = 0;
14521 *diff += bytepos1 - bytepos2;
14522 return true;
14525 /* Return OFF converted to a pointer offset type suitable as offset for
14526 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14527 tree
14528 convert_to_ptrofftype_loc (location_t loc, tree off)
14530 return fold_convert_loc (loc, sizetype, off);
14533 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14534 tree
14535 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14537 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14538 ptr, convert_to_ptrofftype_loc (loc, off));
14541 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14542 tree
14543 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14545 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14546 ptr, size_int (off));
14549 /* Return a pointer P to a NUL-terminated string representing the sequence
14550 of constant characters referred to by SRC (or a subsequence of such
14551 characters within it if SRC is a reference to a string plus some
14552 constant offset). If STRLEN is non-null, store stgrlen(P) in *STRLEN.
14553 If STRSIZE is non-null, store in *STRSIZE the size of the array
14554 the string is stored in; in that case, even though P points to a NUL
14555 terminated string, SRC need not refer to one. This can happen when
14556 SRC refers to a constant character array initialized to all non-NUL
14557 values, as in the C declaration: char a[4] = "1234"; */
14559 const char *
14560 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen /* = NULL */,
14561 unsigned HOST_WIDE_INT *strsize /* = NULL */)
14563 tree offset_node;
14565 if (strlen)
14566 *strlen = 0;
14568 src = string_constant (src, &offset_node);
14569 if (src == 0)
14570 return NULL;
14572 unsigned HOST_WIDE_INT offset = 0;
14573 if (offset_node != NULL_TREE)
14575 if (!tree_fits_uhwi_p (offset_node))
14576 return NULL;
14577 else
14578 offset = tree_to_uhwi (offset_node);
14581 /* STRING_LENGTH is the size of the string literal, including any
14582 embedded NULs. STRING_SIZE is the size of the array the string
14583 literal is stored in. */
14584 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14585 unsigned HOST_WIDE_INT string_size = string_length;
14586 tree type = TREE_TYPE (src);
14587 if (tree size = TYPE_SIZE_UNIT (type))
14588 if (tree_fits_shwi_p (size))
14589 string_size = tree_to_uhwi (size);
14591 if (strlen)
14593 /* Compute and store the length of the substring at OFFSET.
14594 All offsets past the initial length refer to null strings. */
14595 if (offset <= string_length)
14596 *strlen = string_length - offset;
14597 else
14598 *strlen = 0;
14601 const char *string = TREE_STRING_POINTER (src);
14603 if (string_length == 0
14604 || offset >= string_size)
14605 return NULL;
14607 if (strsize)
14609 /* Support even constant character arrays that aren't proper
14610 NUL-terminated strings. */
14611 *strsize = string_size;
14613 else if (string[string_length - 1] != '\0')
14615 /* Support only properly NUL-terminated strings but handle
14616 consecutive strings within the same array, such as the six
14617 substrings in "1\0002\0003". */
14618 return NULL;
14621 return offset <= string_length ? string + offset : "";
14624 /* Given a tree T, compute which bits in T may be nonzero. */
14626 wide_int
14627 tree_nonzero_bits (const_tree t)
14629 switch (TREE_CODE (t))
14631 case INTEGER_CST:
14632 return wi::to_wide (t);
14633 case SSA_NAME:
14634 return get_nonzero_bits (t);
14635 case NON_LVALUE_EXPR:
14636 case SAVE_EXPR:
14637 return tree_nonzero_bits (TREE_OPERAND (t, 0));
14638 case BIT_AND_EXPR:
14639 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14640 tree_nonzero_bits (TREE_OPERAND (t, 1)));
14641 case BIT_IOR_EXPR:
14642 case BIT_XOR_EXPR:
14643 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14644 tree_nonzero_bits (TREE_OPERAND (t, 1)));
14645 case COND_EXPR:
14646 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1)),
14647 tree_nonzero_bits (TREE_OPERAND (t, 2)));
14648 CASE_CONVERT:
14649 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14650 TYPE_PRECISION (TREE_TYPE (t)),
14651 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0))));
14652 case PLUS_EXPR:
14653 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
14655 wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0));
14656 wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1));
14657 if (wi::bit_and (nzbits1, nzbits2) == 0)
14658 return wi::bit_or (nzbits1, nzbits2);
14660 break;
14661 case LSHIFT_EXPR:
14662 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
14664 tree type = TREE_TYPE (t);
14665 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
14666 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
14667 TYPE_PRECISION (type));
14668 return wi::neg_p (arg1)
14669 ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
14670 : wi::lshift (nzbits, arg1);
14672 break;
14673 case RSHIFT_EXPR:
14674 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
14676 tree type = TREE_TYPE (t);
14677 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
14678 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
14679 TYPE_PRECISION (type));
14680 return wi::neg_p (arg1)
14681 ? wi::lshift (nzbits, -arg1)
14682 : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
14684 break;
14685 default:
14686 break;
14689 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t)));
14692 #if CHECKING_P
14694 namespace selftest {
14696 /* Helper functions for writing tests of folding trees. */
14698 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14700 static void
14701 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14702 tree constant)
14704 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14707 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14708 wrapping WRAPPED_EXPR. */
14710 static void
14711 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14712 tree wrapped_expr)
14714 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14715 ASSERT_NE (wrapped_expr, result);
14716 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14717 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14720 /* Verify that various arithmetic binary operations are folded
14721 correctly. */
14723 static void
14724 test_arithmetic_folding ()
14726 tree type = integer_type_node;
14727 tree x = create_tmp_var_raw (type, "x");
14728 tree zero = build_zero_cst (type);
14729 tree one = build_int_cst (type, 1);
14731 /* Addition. */
14732 /* 1 <-- (0 + 1) */
14733 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14734 one);
14735 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14736 one);
14738 /* (nonlvalue)x <-- (x + 0) */
14739 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14742 /* Subtraction. */
14743 /* 0 <-- (x - x) */
14744 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14745 zero);
14746 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14749 /* Multiplication. */
14750 /* 0 <-- (x * 0) */
14751 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14752 zero);
14754 /* (nonlvalue)x <-- (x * 1) */
14755 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14759 /* Verify that various binary operations on vectors are folded
14760 correctly. */
14762 static void
14763 test_vector_folding ()
14765 tree inner_type = integer_type_node;
14766 tree type = build_vector_type (inner_type, 4);
14767 tree zero = build_zero_cst (type);
14768 tree one = build_one_cst (type);
14770 /* Verify equality tests that return a scalar boolean result. */
14771 tree res_type = boolean_type_node;
14772 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14773 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14774 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14775 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14778 /* Verify folding of VEC_DUPLICATE_EXPRs. */
14780 static void
14781 test_vec_duplicate_folding ()
14783 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
14784 machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
14785 /* This will be 1 if VEC_MODE isn't a vector mode. */
14786 poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
14788 tree type = build_vector_type (ssizetype, nunits);
14789 tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
14790 tree dup5_cst = build_vector_from_val (type, ssize_int (5));
14791 ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
14794 /* Run all of the selftests within this file. */
14796 void
14797 fold_const_c_tests ()
14799 test_arithmetic_folding ();
14800 test_vector_folding ();
14801 test_vec_duplicate_folding ();
14804 } // namespace selftest
14806 #endif /* CHECKING_P */