[Ada] Unnesting: properly handle local subprogram in declare blocks
[official-gcc.git] / gcc / fold-const.c
blob6f80f1b1d695fd6920ddab46ce8ce8db47bb9ed3
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 bool overflow = false;
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 bool 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 return NULL_TREE;
1732 switch (code)
1734 CASE_CONVERT:
1735 case FLOAT_EXPR:
1736 case FIX_TRUNC_EXPR:
1737 case FIXED_CONVERT_EXPR:
1738 return fold_convert_const (code, type, arg0);
1740 case ADDR_SPACE_CONVERT_EXPR:
1741 /* If the source address is 0, and the source address space
1742 cannot have a valid object at 0, fold to dest type null. */
1743 if (integer_zerop (arg0)
1744 && !(targetm.addr_space.zero_address_valid
1745 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1746 return fold_convert_const (code, type, arg0);
1747 break;
1749 case VIEW_CONVERT_EXPR:
1750 return fold_view_convert_expr (type, arg0);
1752 case NEGATE_EXPR:
1754 /* Can't call fold_negate_const directly here as that doesn't
1755 handle all cases and we might not be able to negate some
1756 constants. */
1757 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1758 if (tem && CONSTANT_CLASS_P (tem))
1759 return tem;
1760 break;
1763 case ABS_EXPR:
1764 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1765 return fold_abs_const (arg0, type);
1766 break;
1768 case CONJ_EXPR:
1769 if (TREE_CODE (arg0) == COMPLEX_CST)
1771 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1772 TREE_TYPE (type));
1773 return build_complex (type, TREE_REALPART (arg0), ipart);
1775 break;
1777 case BIT_NOT_EXPR:
1778 if (TREE_CODE (arg0) == INTEGER_CST)
1779 return fold_not_const (arg0, type);
1780 else if (POLY_INT_CST_P (arg0))
1781 return wide_int_to_tree (type, -poly_int_cst_value (arg0));
1782 /* Perform BIT_NOT_EXPR on each element individually. */
1783 else if (TREE_CODE (arg0) == VECTOR_CST)
1785 tree elem;
1787 /* This can cope with stepped encodings because ~x == -1 - x. */
1788 tree_vector_builder elements;
1789 elements.new_unary_operation (type, arg0, true);
1790 unsigned int i, count = elements.encoded_nelts ();
1791 for (i = 0; i < count; ++i)
1793 elem = VECTOR_CST_ELT (arg0, i);
1794 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1795 if (elem == NULL_TREE)
1796 break;
1797 elements.quick_push (elem);
1799 if (i == count)
1800 return elements.build ();
1802 break;
1804 case TRUTH_NOT_EXPR:
1805 if (TREE_CODE (arg0) == INTEGER_CST)
1806 return constant_boolean_node (integer_zerop (arg0), type);
1807 break;
1809 case REALPART_EXPR:
1810 if (TREE_CODE (arg0) == COMPLEX_CST)
1811 return fold_convert (type, TREE_REALPART (arg0));
1812 break;
1814 case IMAGPART_EXPR:
1815 if (TREE_CODE (arg0) == COMPLEX_CST)
1816 return fold_convert (type, TREE_IMAGPART (arg0));
1817 break;
1819 case VEC_UNPACK_LO_EXPR:
1820 case VEC_UNPACK_HI_EXPR:
1821 case VEC_UNPACK_FLOAT_LO_EXPR:
1822 case VEC_UNPACK_FLOAT_HI_EXPR:
1823 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1824 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1826 unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
1827 enum tree_code subcode;
1829 if (TREE_CODE (arg0) != VECTOR_CST)
1830 return NULL_TREE;
1832 if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
1833 return NULL_TREE;
1834 out_nelts = in_nelts / 2;
1835 gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1837 unsigned int offset = 0;
1838 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1839 || code == VEC_UNPACK_FLOAT_LO_EXPR
1840 || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
1841 offset = out_nelts;
1843 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1844 subcode = NOP_EXPR;
1845 else if (code == VEC_UNPACK_FLOAT_LO_EXPR
1846 || code == VEC_UNPACK_FLOAT_HI_EXPR)
1847 subcode = FLOAT_EXPR;
1848 else
1849 subcode = FIX_TRUNC_EXPR;
1851 tree_vector_builder elts (type, out_nelts, 1);
1852 for (i = 0; i < out_nelts; i++)
1854 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1855 VECTOR_CST_ELT (arg0, i + offset));
1856 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1857 return NULL_TREE;
1858 elts.quick_push (elt);
1861 return elts.build ();
1864 case VEC_DUPLICATE_EXPR:
1865 if (CONSTANT_CLASS_P (arg0))
1866 return build_vector_from_val (type, arg0);
1867 return NULL_TREE;
1869 default:
1870 break;
1873 return NULL_TREE;
1876 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1877 indicates which particular sizetype to create. */
1879 tree
1880 size_int_kind (poly_int64 number, enum size_type_kind kind)
1882 return build_int_cst (sizetype_tab[(int) kind], number);
1885 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1886 is a tree code. The type of the result is taken from the operands.
1887 Both must be equivalent integer types, ala int_binop_types_match_p.
1888 If the operands are constant, so is the result. */
1890 tree
1891 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1893 tree type = TREE_TYPE (arg0);
1895 if (arg0 == error_mark_node || arg1 == error_mark_node)
1896 return error_mark_node;
1898 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1899 TREE_TYPE (arg1)));
1901 /* Handle the special case of two poly_int constants faster. */
1902 if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1904 /* And some specific cases even faster than that. */
1905 if (code == PLUS_EXPR)
1907 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1908 return arg1;
1909 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1910 return arg0;
1912 else if (code == MINUS_EXPR)
1914 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1915 return arg0;
1917 else if (code == MULT_EXPR)
1919 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1920 return arg1;
1923 /* Handle general case of two integer constants. For sizetype
1924 constant calculations we always want to know about overflow,
1925 even in the unsigned case. */
1926 tree res = int_const_binop_1 (code, arg0, arg1, -1);
1927 if (res != NULL_TREE)
1928 return res;
1931 return fold_build2_loc (loc, code, type, arg0, arg1);
1934 /* Given two values, either both of sizetype or both of bitsizetype,
1935 compute the difference between the two values. Return the value
1936 in signed type corresponding to the type of the operands. */
1938 tree
1939 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1941 tree type = TREE_TYPE (arg0);
1942 tree ctype;
1944 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1945 TREE_TYPE (arg1)));
1947 /* If the type is already signed, just do the simple thing. */
1948 if (!TYPE_UNSIGNED (type))
1949 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1951 if (type == sizetype)
1952 ctype = ssizetype;
1953 else if (type == bitsizetype)
1954 ctype = sbitsizetype;
1955 else
1956 ctype = signed_type_for (type);
1958 /* If either operand is not a constant, do the conversions to the signed
1959 type and subtract. The hardware will do the right thing with any
1960 overflow in the subtraction. */
1961 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1962 return size_binop_loc (loc, MINUS_EXPR,
1963 fold_convert_loc (loc, ctype, arg0),
1964 fold_convert_loc (loc, ctype, arg1));
1966 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1967 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1968 overflow) and negate (which can't either). Special-case a result
1969 of zero while we're here. */
1970 if (tree_int_cst_equal (arg0, arg1))
1971 return build_int_cst (ctype, 0);
1972 else if (tree_int_cst_lt (arg1, arg0))
1973 return fold_convert_loc (loc, ctype,
1974 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1975 else
1976 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1977 fold_convert_loc (loc, ctype,
1978 size_binop_loc (loc,
1979 MINUS_EXPR,
1980 arg1, arg0)));
1983 /* A subroutine of fold_convert_const handling conversions of an
1984 INTEGER_CST to another integer type. */
1986 static tree
1987 fold_convert_const_int_from_int (tree type, const_tree arg1)
1989 /* Given an integer constant, make new constant with new type,
1990 appropriately sign-extended or truncated. Use widest_int
1991 so that any extension is done according ARG1's type. */
1992 return force_fit_type (type, wi::to_widest (arg1),
1993 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1994 TREE_OVERFLOW (arg1));
1997 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1998 to an integer type. */
2000 static tree
2001 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2003 bool overflow = false;
2004 tree t;
2006 /* The following code implements the floating point to integer
2007 conversion rules required by the Java Language Specification,
2008 that IEEE NaNs are mapped to zero and values that overflow
2009 the target precision saturate, i.e. values greater than
2010 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2011 are mapped to INT_MIN. These semantics are allowed by the
2012 C and C++ standards that simply state that the behavior of
2013 FP-to-integer conversion is unspecified upon overflow. */
2015 wide_int val;
2016 REAL_VALUE_TYPE r;
2017 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2019 switch (code)
2021 case FIX_TRUNC_EXPR:
2022 real_trunc (&r, VOIDmode, &x);
2023 break;
2025 default:
2026 gcc_unreachable ();
2029 /* If R is NaN, return zero and show we have an overflow. */
2030 if (REAL_VALUE_ISNAN (r))
2032 overflow = true;
2033 val = wi::zero (TYPE_PRECISION (type));
2036 /* See if R is less than the lower bound or greater than the
2037 upper bound. */
2039 if (! overflow)
2041 tree lt = TYPE_MIN_VALUE (type);
2042 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2043 if (real_less (&r, &l))
2045 overflow = true;
2046 val = wi::to_wide (lt);
2050 if (! overflow)
2052 tree ut = TYPE_MAX_VALUE (type);
2053 if (ut)
2055 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2056 if (real_less (&u, &r))
2058 overflow = true;
2059 val = wi::to_wide (ut);
2064 if (! overflow)
2065 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2067 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2068 return t;
2071 /* A subroutine of fold_convert_const handling conversions of a
2072 FIXED_CST to an integer type. */
2074 static tree
2075 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2077 tree t;
2078 double_int temp, temp_trunc;
2079 scalar_mode mode;
2081 /* Right shift FIXED_CST to temp by fbit. */
2082 temp = TREE_FIXED_CST (arg1).data;
2083 mode = TREE_FIXED_CST (arg1).mode;
2084 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2086 temp = temp.rshift (GET_MODE_FBIT (mode),
2087 HOST_BITS_PER_DOUBLE_INT,
2088 SIGNED_FIXED_POINT_MODE_P (mode));
2090 /* Left shift temp to temp_trunc by fbit. */
2091 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2092 HOST_BITS_PER_DOUBLE_INT,
2093 SIGNED_FIXED_POINT_MODE_P (mode));
2095 else
2097 temp = double_int_zero;
2098 temp_trunc = double_int_zero;
2101 /* If FIXED_CST is negative, we need to round the value toward 0.
2102 By checking if the fractional bits are not zero to add 1 to temp. */
2103 if (SIGNED_FIXED_POINT_MODE_P (mode)
2104 && temp_trunc.is_negative ()
2105 && TREE_FIXED_CST (arg1).data != temp_trunc)
2106 temp += double_int_one;
2108 /* Given a fixed-point constant, make new constant with new type,
2109 appropriately sign-extended or truncated. */
2110 t = force_fit_type (type, temp, -1,
2111 (temp.is_negative ()
2112 && (TYPE_UNSIGNED (type)
2113 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2114 | TREE_OVERFLOW (arg1));
2116 return t;
2119 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2120 to another floating point type. */
2122 static tree
2123 fold_convert_const_real_from_real (tree type, const_tree arg1)
2125 REAL_VALUE_TYPE value;
2126 tree t;
2128 /* Don't perform the operation if flag_signaling_nans is on
2129 and the operand is a signaling NaN. */
2130 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2131 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2132 return NULL_TREE;
2134 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2135 t = build_real (type, value);
2137 /* If converting an infinity or NAN to a representation that doesn't
2138 have one, set the overflow bit so that we can produce some kind of
2139 error message at the appropriate point if necessary. It's not the
2140 most user-friendly message, but it's better than nothing. */
2141 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2142 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2143 TREE_OVERFLOW (t) = 1;
2144 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2145 && !MODE_HAS_NANS (TYPE_MODE (type)))
2146 TREE_OVERFLOW (t) = 1;
2147 /* Regular overflow, conversion produced an infinity in a mode that
2148 can't represent them. */
2149 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2150 && REAL_VALUE_ISINF (value)
2151 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2152 TREE_OVERFLOW (t) = 1;
2153 else
2154 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2155 return t;
2158 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2159 to a floating point type. */
2161 static tree
2162 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2164 REAL_VALUE_TYPE value;
2165 tree t;
2167 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2168 &TREE_FIXED_CST (arg1));
2169 t = build_real (type, value);
2171 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2172 return t;
2175 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2176 to another fixed-point type. */
2178 static tree
2179 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2181 FIXED_VALUE_TYPE value;
2182 tree t;
2183 bool overflow_p;
2185 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2186 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2187 t = build_fixed (type, value);
2189 /* Propagate overflow flags. */
2190 if (overflow_p | TREE_OVERFLOW (arg1))
2191 TREE_OVERFLOW (t) = 1;
2192 return t;
2195 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2196 to a fixed-point type. */
2198 static tree
2199 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2201 FIXED_VALUE_TYPE value;
2202 tree t;
2203 bool overflow_p;
2204 double_int di;
2206 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2208 di.low = TREE_INT_CST_ELT (arg1, 0);
2209 if (TREE_INT_CST_NUNITS (arg1) == 1)
2210 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2211 else
2212 di.high = TREE_INT_CST_ELT (arg1, 1);
2214 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2215 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2216 TYPE_SATURATING (type));
2217 t = build_fixed (type, value);
2219 /* Propagate overflow flags. */
2220 if (overflow_p | TREE_OVERFLOW (arg1))
2221 TREE_OVERFLOW (t) = 1;
2222 return t;
2225 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2226 to a fixed-point type. */
2228 static tree
2229 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2231 FIXED_VALUE_TYPE value;
2232 tree t;
2233 bool overflow_p;
2235 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2236 &TREE_REAL_CST (arg1),
2237 TYPE_SATURATING (type));
2238 t = build_fixed (type, value);
2240 /* Propagate overflow flags. */
2241 if (overflow_p | TREE_OVERFLOW (arg1))
2242 TREE_OVERFLOW (t) = 1;
2243 return t;
2246 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2247 type TYPE. If no simplification can be done return NULL_TREE. */
2249 static tree
2250 fold_convert_const (enum tree_code code, tree type, tree arg1)
2252 tree arg_type = TREE_TYPE (arg1);
2253 if (arg_type == type)
2254 return arg1;
2256 /* We can't widen types, since the runtime value could overflow the
2257 original type before being extended to the new type. */
2258 if (POLY_INT_CST_P (arg1)
2259 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2260 && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2261 return build_poly_int_cst (type,
2262 poly_wide_int::from (poly_int_cst_value (arg1),
2263 TYPE_PRECISION (type),
2264 TYPE_SIGN (arg_type)));
2266 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2267 || TREE_CODE (type) == OFFSET_TYPE)
2269 if (TREE_CODE (arg1) == INTEGER_CST)
2270 return fold_convert_const_int_from_int (type, arg1);
2271 else if (TREE_CODE (arg1) == REAL_CST)
2272 return fold_convert_const_int_from_real (code, type, arg1);
2273 else if (TREE_CODE (arg1) == FIXED_CST)
2274 return fold_convert_const_int_from_fixed (type, arg1);
2276 else if (TREE_CODE (type) == REAL_TYPE)
2278 if (TREE_CODE (arg1) == INTEGER_CST)
2279 return build_real_from_int_cst (type, arg1);
2280 else if (TREE_CODE (arg1) == REAL_CST)
2281 return fold_convert_const_real_from_real (type, arg1);
2282 else if (TREE_CODE (arg1) == FIXED_CST)
2283 return fold_convert_const_real_from_fixed (type, arg1);
2285 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2287 if (TREE_CODE (arg1) == FIXED_CST)
2288 return fold_convert_const_fixed_from_fixed (type, arg1);
2289 else if (TREE_CODE (arg1) == INTEGER_CST)
2290 return fold_convert_const_fixed_from_int (type, arg1);
2291 else if (TREE_CODE (arg1) == REAL_CST)
2292 return fold_convert_const_fixed_from_real (type, arg1);
2294 else if (TREE_CODE (type) == VECTOR_TYPE)
2296 if (TREE_CODE (arg1) == VECTOR_CST
2297 && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2299 tree elttype = TREE_TYPE (type);
2300 tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2301 /* We can't handle steps directly when extending, since the
2302 values need to wrap at the original precision first. */
2303 bool step_ok_p
2304 = (INTEGRAL_TYPE_P (elttype)
2305 && INTEGRAL_TYPE_P (arg1_elttype)
2306 && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2307 tree_vector_builder v;
2308 if (!v.new_unary_operation (type, arg1, step_ok_p))
2309 return NULL_TREE;
2310 unsigned int len = v.encoded_nelts ();
2311 for (unsigned int i = 0; i < len; ++i)
2313 tree elt = VECTOR_CST_ELT (arg1, i);
2314 tree cvt = fold_convert_const (code, elttype, elt);
2315 if (cvt == NULL_TREE)
2316 return NULL_TREE;
2317 v.quick_push (cvt);
2319 return v.build ();
2322 return NULL_TREE;
2325 /* Construct a vector of zero elements of vector type TYPE. */
2327 static tree
2328 build_zero_vector (tree type)
2330 tree t;
2332 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2333 return build_vector_from_val (type, t);
2336 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2338 bool
2339 fold_convertible_p (const_tree type, const_tree arg)
2341 tree orig = TREE_TYPE (arg);
2343 if (type == orig)
2344 return true;
2346 if (TREE_CODE (arg) == ERROR_MARK
2347 || TREE_CODE (type) == ERROR_MARK
2348 || TREE_CODE (orig) == ERROR_MARK)
2349 return false;
2351 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2352 return true;
2354 switch (TREE_CODE (type))
2356 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2357 case POINTER_TYPE: case REFERENCE_TYPE:
2358 case OFFSET_TYPE:
2359 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2360 || TREE_CODE (orig) == OFFSET_TYPE);
2362 case REAL_TYPE:
2363 case FIXED_POINT_TYPE:
2364 case VECTOR_TYPE:
2365 case VOID_TYPE:
2366 return TREE_CODE (type) == TREE_CODE (orig);
2368 default:
2369 return false;
2373 /* Convert expression ARG to type TYPE. Used by the middle-end for
2374 simple conversions in preference to calling the front-end's convert. */
2376 tree
2377 fold_convert_loc (location_t loc, tree type, tree arg)
2379 tree orig = TREE_TYPE (arg);
2380 tree tem;
2382 if (type == orig)
2383 return arg;
2385 if (TREE_CODE (arg) == ERROR_MARK
2386 || TREE_CODE (type) == ERROR_MARK
2387 || TREE_CODE (orig) == ERROR_MARK)
2388 return error_mark_node;
2390 switch (TREE_CODE (type))
2392 case POINTER_TYPE:
2393 case REFERENCE_TYPE:
2394 /* Handle conversions between pointers to different address spaces. */
2395 if (POINTER_TYPE_P (orig)
2396 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2397 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2398 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2399 /* fall through */
2401 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2402 case OFFSET_TYPE:
2403 if (TREE_CODE (arg) == INTEGER_CST)
2405 tem = fold_convert_const (NOP_EXPR, type, arg);
2406 if (tem != NULL_TREE)
2407 return tem;
2409 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2410 || TREE_CODE (orig) == OFFSET_TYPE)
2411 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2412 if (TREE_CODE (orig) == COMPLEX_TYPE)
2413 return fold_convert_loc (loc, type,
2414 fold_build1_loc (loc, REALPART_EXPR,
2415 TREE_TYPE (orig), arg));
2416 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2417 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2418 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2420 case REAL_TYPE:
2421 if (TREE_CODE (arg) == INTEGER_CST)
2423 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2424 if (tem != NULL_TREE)
2425 return tem;
2427 else if (TREE_CODE (arg) == REAL_CST)
2429 tem = fold_convert_const (NOP_EXPR, type, arg);
2430 if (tem != NULL_TREE)
2431 return tem;
2433 else if (TREE_CODE (arg) == FIXED_CST)
2435 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2436 if (tem != NULL_TREE)
2437 return tem;
2440 switch (TREE_CODE (orig))
2442 case INTEGER_TYPE:
2443 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2444 case POINTER_TYPE: case REFERENCE_TYPE:
2445 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2447 case REAL_TYPE:
2448 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2450 case FIXED_POINT_TYPE:
2451 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2453 case COMPLEX_TYPE:
2454 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2455 return fold_convert_loc (loc, type, tem);
2457 default:
2458 gcc_unreachable ();
2461 case FIXED_POINT_TYPE:
2462 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2463 || TREE_CODE (arg) == REAL_CST)
2465 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2466 if (tem != NULL_TREE)
2467 goto fold_convert_exit;
2470 switch (TREE_CODE (orig))
2472 case FIXED_POINT_TYPE:
2473 case INTEGER_TYPE:
2474 case ENUMERAL_TYPE:
2475 case BOOLEAN_TYPE:
2476 case REAL_TYPE:
2477 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2479 case COMPLEX_TYPE:
2480 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2481 return fold_convert_loc (loc, type, tem);
2483 default:
2484 gcc_unreachable ();
2487 case COMPLEX_TYPE:
2488 switch (TREE_CODE (orig))
2490 case INTEGER_TYPE:
2491 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2492 case POINTER_TYPE: case REFERENCE_TYPE:
2493 case REAL_TYPE:
2494 case FIXED_POINT_TYPE:
2495 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2496 fold_convert_loc (loc, TREE_TYPE (type), arg),
2497 fold_convert_loc (loc, TREE_TYPE (type),
2498 integer_zero_node));
2499 case COMPLEX_TYPE:
2501 tree rpart, ipart;
2503 if (TREE_CODE (arg) == COMPLEX_EXPR)
2505 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2506 TREE_OPERAND (arg, 0));
2507 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2508 TREE_OPERAND (arg, 1));
2509 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2512 arg = save_expr (arg);
2513 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2514 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2515 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2516 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2517 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2520 default:
2521 gcc_unreachable ();
2524 case VECTOR_TYPE:
2525 if (integer_zerop (arg))
2526 return build_zero_vector (type);
2527 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2528 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2529 || TREE_CODE (orig) == VECTOR_TYPE);
2530 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2532 case VOID_TYPE:
2533 tem = fold_ignored_result (arg);
2534 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2536 default:
2537 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2538 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2539 gcc_unreachable ();
2541 fold_convert_exit:
2542 protected_set_expr_location_unshare (tem, loc);
2543 return tem;
2546 /* Return false if expr can be assumed not to be an lvalue, true
2547 otherwise. */
2549 static bool
2550 maybe_lvalue_p (const_tree x)
2552 /* We only need to wrap lvalue tree codes. */
2553 switch (TREE_CODE (x))
2555 case VAR_DECL:
2556 case PARM_DECL:
2557 case RESULT_DECL:
2558 case LABEL_DECL:
2559 case FUNCTION_DECL:
2560 case SSA_NAME:
2562 case COMPONENT_REF:
2563 case MEM_REF:
2564 case INDIRECT_REF:
2565 case ARRAY_REF:
2566 case ARRAY_RANGE_REF:
2567 case BIT_FIELD_REF:
2568 case OBJ_TYPE_REF:
2570 case REALPART_EXPR:
2571 case IMAGPART_EXPR:
2572 case PREINCREMENT_EXPR:
2573 case PREDECREMENT_EXPR:
2574 case SAVE_EXPR:
2575 case TRY_CATCH_EXPR:
2576 case WITH_CLEANUP_EXPR:
2577 case COMPOUND_EXPR:
2578 case MODIFY_EXPR:
2579 case TARGET_EXPR:
2580 case COND_EXPR:
2581 case BIND_EXPR:
2582 break;
2584 default:
2585 /* Assume the worst for front-end tree codes. */
2586 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2587 break;
2588 return false;
2591 return true;
2594 /* Return an expr equal to X but certainly not valid as an lvalue. */
2596 tree
2597 non_lvalue_loc (location_t loc, tree x)
2599 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2600 us. */
2601 if (in_gimple_form)
2602 return x;
2604 if (! maybe_lvalue_p (x))
2605 return x;
2606 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2609 /* When pedantic, return an expr equal to X but certainly not valid as a
2610 pedantic lvalue. Otherwise, return X. */
2612 static tree
2613 pedantic_non_lvalue_loc (location_t loc, tree x)
2615 return protected_set_expr_location_unshare (x, loc);
2618 /* Given a tree comparison code, return the code that is the logical inverse.
2619 It is generally not safe to do this for floating-point comparisons, except
2620 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2621 ERROR_MARK in this case. */
2623 enum tree_code
2624 invert_tree_comparison (enum tree_code code, bool honor_nans)
2626 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2627 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2628 return ERROR_MARK;
2630 switch (code)
2632 case EQ_EXPR:
2633 return NE_EXPR;
2634 case NE_EXPR:
2635 return EQ_EXPR;
2636 case GT_EXPR:
2637 return honor_nans ? UNLE_EXPR : LE_EXPR;
2638 case GE_EXPR:
2639 return honor_nans ? UNLT_EXPR : LT_EXPR;
2640 case LT_EXPR:
2641 return honor_nans ? UNGE_EXPR : GE_EXPR;
2642 case LE_EXPR:
2643 return honor_nans ? UNGT_EXPR : GT_EXPR;
2644 case LTGT_EXPR:
2645 return UNEQ_EXPR;
2646 case UNEQ_EXPR:
2647 return LTGT_EXPR;
2648 case UNGT_EXPR:
2649 return LE_EXPR;
2650 case UNGE_EXPR:
2651 return LT_EXPR;
2652 case UNLT_EXPR:
2653 return GE_EXPR;
2654 case UNLE_EXPR:
2655 return GT_EXPR;
2656 case ORDERED_EXPR:
2657 return UNORDERED_EXPR;
2658 case UNORDERED_EXPR:
2659 return ORDERED_EXPR;
2660 default:
2661 gcc_unreachable ();
2665 /* Similar, but return the comparison that results if the operands are
2666 swapped. This is safe for floating-point. */
2668 enum tree_code
2669 swap_tree_comparison (enum tree_code code)
2671 switch (code)
2673 case EQ_EXPR:
2674 case NE_EXPR:
2675 case ORDERED_EXPR:
2676 case UNORDERED_EXPR:
2677 case LTGT_EXPR:
2678 case UNEQ_EXPR:
2679 return code;
2680 case GT_EXPR:
2681 return LT_EXPR;
2682 case GE_EXPR:
2683 return LE_EXPR;
2684 case LT_EXPR:
2685 return GT_EXPR;
2686 case LE_EXPR:
2687 return GE_EXPR;
2688 case UNGT_EXPR:
2689 return UNLT_EXPR;
2690 case UNGE_EXPR:
2691 return UNLE_EXPR;
2692 case UNLT_EXPR:
2693 return UNGT_EXPR;
2694 case UNLE_EXPR:
2695 return UNGE_EXPR;
2696 default:
2697 gcc_unreachable ();
2702 /* Convert a comparison tree code from an enum tree_code representation
2703 into a compcode bit-based encoding. This function is the inverse of
2704 compcode_to_comparison. */
2706 static enum comparison_code
2707 comparison_to_compcode (enum tree_code code)
2709 switch (code)
2711 case LT_EXPR:
2712 return COMPCODE_LT;
2713 case EQ_EXPR:
2714 return COMPCODE_EQ;
2715 case LE_EXPR:
2716 return COMPCODE_LE;
2717 case GT_EXPR:
2718 return COMPCODE_GT;
2719 case NE_EXPR:
2720 return COMPCODE_NE;
2721 case GE_EXPR:
2722 return COMPCODE_GE;
2723 case ORDERED_EXPR:
2724 return COMPCODE_ORD;
2725 case UNORDERED_EXPR:
2726 return COMPCODE_UNORD;
2727 case UNLT_EXPR:
2728 return COMPCODE_UNLT;
2729 case UNEQ_EXPR:
2730 return COMPCODE_UNEQ;
2731 case UNLE_EXPR:
2732 return COMPCODE_UNLE;
2733 case UNGT_EXPR:
2734 return COMPCODE_UNGT;
2735 case LTGT_EXPR:
2736 return COMPCODE_LTGT;
2737 case UNGE_EXPR:
2738 return COMPCODE_UNGE;
2739 default:
2740 gcc_unreachable ();
2744 /* Convert a compcode bit-based encoding of a comparison operator back
2745 to GCC's enum tree_code representation. This function is the
2746 inverse of comparison_to_compcode. */
2748 static enum tree_code
2749 compcode_to_comparison (enum comparison_code code)
2751 switch (code)
2753 case COMPCODE_LT:
2754 return LT_EXPR;
2755 case COMPCODE_EQ:
2756 return EQ_EXPR;
2757 case COMPCODE_LE:
2758 return LE_EXPR;
2759 case COMPCODE_GT:
2760 return GT_EXPR;
2761 case COMPCODE_NE:
2762 return NE_EXPR;
2763 case COMPCODE_GE:
2764 return GE_EXPR;
2765 case COMPCODE_ORD:
2766 return ORDERED_EXPR;
2767 case COMPCODE_UNORD:
2768 return UNORDERED_EXPR;
2769 case COMPCODE_UNLT:
2770 return UNLT_EXPR;
2771 case COMPCODE_UNEQ:
2772 return UNEQ_EXPR;
2773 case COMPCODE_UNLE:
2774 return UNLE_EXPR;
2775 case COMPCODE_UNGT:
2776 return UNGT_EXPR;
2777 case COMPCODE_LTGT:
2778 return LTGT_EXPR;
2779 case COMPCODE_UNGE:
2780 return UNGE_EXPR;
2781 default:
2782 gcc_unreachable ();
2786 /* Return a tree for the comparison which is the combination of
2787 doing the AND or OR (depending on CODE) of the two operations LCODE
2788 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2789 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2790 if this makes the transformation invalid. */
2792 tree
2793 combine_comparisons (location_t loc,
2794 enum tree_code code, enum tree_code lcode,
2795 enum tree_code rcode, tree truth_type,
2796 tree ll_arg, tree lr_arg)
2798 bool honor_nans = HONOR_NANS (ll_arg);
2799 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2800 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2801 int compcode;
2803 switch (code)
2805 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2806 compcode = lcompcode & rcompcode;
2807 break;
2809 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2810 compcode = lcompcode | rcompcode;
2811 break;
2813 default:
2814 return NULL_TREE;
2817 if (!honor_nans)
2819 /* Eliminate unordered comparisons, as well as LTGT and ORD
2820 which are not used unless the mode has NaNs. */
2821 compcode &= ~COMPCODE_UNORD;
2822 if (compcode == COMPCODE_LTGT)
2823 compcode = COMPCODE_NE;
2824 else if (compcode == COMPCODE_ORD)
2825 compcode = COMPCODE_TRUE;
2827 else if (flag_trapping_math)
2829 /* Check that the original operation and the optimized ones will trap
2830 under the same condition. */
2831 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2832 && (lcompcode != COMPCODE_EQ)
2833 && (lcompcode != COMPCODE_ORD);
2834 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2835 && (rcompcode != COMPCODE_EQ)
2836 && (rcompcode != COMPCODE_ORD);
2837 bool trap = (compcode & COMPCODE_UNORD) == 0
2838 && (compcode != COMPCODE_EQ)
2839 && (compcode != COMPCODE_ORD);
2841 /* In a short-circuited boolean expression the LHS might be
2842 such that the RHS, if evaluated, will never trap. For
2843 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2844 if neither x nor y is NaN. (This is a mixed blessing: for
2845 example, the expression above will never trap, hence
2846 optimizing it to x < y would be invalid). */
2847 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2848 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2849 rtrap = false;
2851 /* If the comparison was short-circuited, and only the RHS
2852 trapped, we may now generate a spurious trap. */
2853 if (rtrap && !ltrap
2854 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2855 return NULL_TREE;
2857 /* If we changed the conditions that cause a trap, we lose. */
2858 if ((ltrap || rtrap) != trap)
2859 return NULL_TREE;
2862 if (compcode == COMPCODE_TRUE)
2863 return constant_boolean_node (true, truth_type);
2864 else if (compcode == COMPCODE_FALSE)
2865 return constant_boolean_node (false, truth_type);
2866 else
2868 enum tree_code tcode;
2870 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2871 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2875 /* Return nonzero if two operands (typically of the same tree node)
2876 are necessarily equal. FLAGS modifies behavior as follows:
2878 If OEP_ONLY_CONST is set, only return nonzero for constants.
2879 This function tests whether the operands are indistinguishable;
2880 it does not test whether they are equal using C's == operation.
2881 The distinction is important for IEEE floating point, because
2882 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2883 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2885 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2886 even though it may hold multiple values during a function.
2887 This is because a GCC tree node guarantees that nothing else is
2888 executed between the evaluation of its "operands" (which may often
2889 be evaluated in arbitrary order). Hence if the operands themselves
2890 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2891 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2892 unset means assuming isochronic (or instantaneous) tree equivalence.
2893 Unless comparing arbitrary expression trees, such as from different
2894 statements, this flag can usually be left unset.
2896 If OEP_PURE_SAME is set, then pure functions with identical arguments
2897 are considered the same. It is used when the caller has other ways
2898 to ensure that global memory is unchanged in between.
2900 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2901 not values of expressions.
2903 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2904 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2906 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2907 any operand with side effect. This is unnecesarily conservative in the
2908 case we know that arg0 and arg1 are in disjoint code paths (such as in
2909 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2910 addresses with TREE_CONSTANT flag set so we know that &var == &var
2911 even if var is volatile. */
2914 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2916 /* When checking, verify at the outermost operand_equal_p call that
2917 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2918 hash value. */
2919 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2921 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2923 if (arg0 != arg1)
2925 inchash::hash hstate0 (0), hstate1 (0);
2926 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2927 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2928 hashval_t h0 = hstate0.end ();
2929 hashval_t h1 = hstate1.end ();
2930 gcc_assert (h0 == h1);
2932 return 1;
2934 else
2935 return 0;
2938 /* If either is ERROR_MARK, they aren't equal. */
2939 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2940 || TREE_TYPE (arg0) == error_mark_node
2941 || TREE_TYPE (arg1) == error_mark_node)
2942 return 0;
2944 /* Similar, if either does not have a type (like a released SSA name),
2945 they aren't equal. */
2946 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2947 return 0;
2949 /* We cannot consider pointers to different address space equal. */
2950 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2951 && POINTER_TYPE_P (TREE_TYPE (arg1))
2952 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2953 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2954 return 0;
2956 /* Check equality of integer constants before bailing out due to
2957 precision differences. */
2958 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2960 /* Address of INTEGER_CST is not defined; check that we did not forget
2961 to drop the OEP_ADDRESS_OF flags. */
2962 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2963 return tree_int_cst_equal (arg0, arg1);
2966 if (!(flags & OEP_ADDRESS_OF))
2968 /* If both types don't have the same signedness, then we can't consider
2969 them equal. We must check this before the STRIP_NOPS calls
2970 because they may change the signedness of the arguments. As pointers
2971 strictly don't have a signedness, require either two pointers or
2972 two non-pointers as well. */
2973 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2974 || POINTER_TYPE_P (TREE_TYPE (arg0))
2975 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2976 return 0;
2978 /* If both types don't have the same precision, then it is not safe
2979 to strip NOPs. */
2980 if (element_precision (TREE_TYPE (arg0))
2981 != element_precision (TREE_TYPE (arg1)))
2982 return 0;
2984 STRIP_NOPS (arg0);
2985 STRIP_NOPS (arg1);
2987 #if 0
2988 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2989 sanity check once the issue is solved. */
2990 else
2991 /* Addresses of conversions and SSA_NAMEs (and many other things)
2992 are not defined. Check that we did not forget to drop the
2993 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2994 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2995 && TREE_CODE (arg0) != SSA_NAME);
2996 #endif
2998 /* In case both args are comparisons but with different comparison
2999 code, try to swap the comparison operands of one arg to produce
3000 a match and compare that variant. */
3001 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3002 && COMPARISON_CLASS_P (arg0)
3003 && COMPARISON_CLASS_P (arg1))
3005 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3007 if (TREE_CODE (arg0) == swap_code)
3008 return operand_equal_p (TREE_OPERAND (arg0, 0),
3009 TREE_OPERAND (arg1, 1), flags)
3010 && operand_equal_p (TREE_OPERAND (arg0, 1),
3011 TREE_OPERAND (arg1, 0), flags);
3014 if (TREE_CODE (arg0) != TREE_CODE (arg1))
3016 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3017 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3019 else if (flags & OEP_ADDRESS_OF)
3021 /* If we are interested in comparing addresses ignore
3022 MEM_REF wrappings of the base that can appear just for
3023 TBAA reasons. */
3024 if (TREE_CODE (arg0) == MEM_REF
3025 && DECL_P (arg1)
3026 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3027 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3028 && integer_zerop (TREE_OPERAND (arg0, 1)))
3029 return 1;
3030 else if (TREE_CODE (arg1) == MEM_REF
3031 && DECL_P (arg0)
3032 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3033 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3034 && integer_zerop (TREE_OPERAND (arg1, 1)))
3035 return 1;
3036 return 0;
3038 else
3039 return 0;
3042 /* When not checking adddresses, this is needed for conversions and for
3043 COMPONENT_REF. Might as well play it safe and always test this. */
3044 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3045 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3046 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
3047 && !(flags & OEP_ADDRESS_OF)))
3048 return 0;
3050 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3051 We don't care about side effects in that case because the SAVE_EXPR
3052 takes care of that for us. In all other cases, two expressions are
3053 equal if they have no side effects. If we have two identical
3054 expressions with side effects that should be treated the same due
3055 to the only side effects being identical SAVE_EXPR's, that will
3056 be detected in the recursive calls below.
3057 If we are taking an invariant address of two identical objects
3058 they are necessarily equal as well. */
3059 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3060 && (TREE_CODE (arg0) == SAVE_EXPR
3061 || (flags & OEP_MATCH_SIDE_EFFECTS)
3062 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3063 return 1;
3065 /* Next handle constant cases, those for which we can return 1 even
3066 if ONLY_CONST is set. */
3067 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3068 switch (TREE_CODE (arg0))
3070 case INTEGER_CST:
3071 return tree_int_cst_equal (arg0, arg1);
3073 case FIXED_CST:
3074 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3075 TREE_FIXED_CST (arg1));
3077 case REAL_CST:
3078 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3079 return 1;
3082 if (!HONOR_SIGNED_ZEROS (arg0))
3084 /* If we do not distinguish between signed and unsigned zero,
3085 consider them equal. */
3086 if (real_zerop (arg0) && real_zerop (arg1))
3087 return 1;
3089 return 0;
3091 case VECTOR_CST:
3093 if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3094 != VECTOR_CST_LOG2_NPATTERNS (arg1))
3095 return 0;
3097 if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3098 != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3099 return 0;
3101 unsigned int count = vector_cst_encoded_nelts (arg0);
3102 for (unsigned int i = 0; i < count; ++i)
3103 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3104 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3105 return 0;
3106 return 1;
3109 case COMPLEX_CST:
3110 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3111 flags)
3112 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3113 flags));
3115 case STRING_CST:
3116 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3117 && ! memcmp (TREE_STRING_POINTER (arg0),
3118 TREE_STRING_POINTER (arg1),
3119 TREE_STRING_LENGTH (arg0)));
3121 case ADDR_EXPR:
3122 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3123 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3124 flags | OEP_ADDRESS_OF
3125 | OEP_MATCH_SIDE_EFFECTS);
3126 case CONSTRUCTOR:
3127 /* In GIMPLE empty constructors are allowed in initializers of
3128 aggregates. */
3129 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
3130 default:
3131 break;
3134 if (flags & OEP_ONLY_CONST)
3135 return 0;
3137 /* Define macros to test an operand from arg0 and arg1 for equality and a
3138 variant that allows null and views null as being different from any
3139 non-null value. In the latter case, if either is null, the both
3140 must be; otherwise, do the normal comparison. */
3141 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3142 TREE_OPERAND (arg1, N), flags)
3144 #define OP_SAME_WITH_NULL(N) \
3145 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3146 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3148 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3150 case tcc_unary:
3151 /* Two conversions are equal only if signedness and modes match. */
3152 switch (TREE_CODE (arg0))
3154 CASE_CONVERT:
3155 case FIX_TRUNC_EXPR:
3156 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3157 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3158 return 0;
3159 break;
3160 default:
3161 break;
3164 return OP_SAME (0);
3167 case tcc_comparison:
3168 case tcc_binary:
3169 if (OP_SAME (0) && OP_SAME (1))
3170 return 1;
3172 /* For commutative ops, allow the other order. */
3173 return (commutative_tree_code (TREE_CODE (arg0))
3174 && operand_equal_p (TREE_OPERAND (arg0, 0),
3175 TREE_OPERAND (arg1, 1), flags)
3176 && operand_equal_p (TREE_OPERAND (arg0, 1),
3177 TREE_OPERAND (arg1, 0), flags));
3179 case tcc_reference:
3180 /* If either of the pointer (or reference) expressions we are
3181 dereferencing contain a side effect, these cannot be equal,
3182 but their addresses can be. */
3183 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3184 && (TREE_SIDE_EFFECTS (arg0)
3185 || TREE_SIDE_EFFECTS (arg1)))
3186 return 0;
3188 switch (TREE_CODE (arg0))
3190 case INDIRECT_REF:
3191 if (!(flags & OEP_ADDRESS_OF)
3192 && (TYPE_ALIGN (TREE_TYPE (arg0))
3193 != TYPE_ALIGN (TREE_TYPE (arg1))))
3194 return 0;
3195 flags &= ~OEP_ADDRESS_OF;
3196 return OP_SAME (0);
3198 case IMAGPART_EXPR:
3199 /* Require the same offset. */
3200 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3201 TYPE_SIZE (TREE_TYPE (arg1)),
3202 flags & ~OEP_ADDRESS_OF))
3203 return 0;
3205 /* Fallthru. */
3206 case REALPART_EXPR:
3207 case VIEW_CONVERT_EXPR:
3208 return OP_SAME (0);
3210 case TARGET_MEM_REF:
3211 case MEM_REF:
3212 if (!(flags & OEP_ADDRESS_OF))
3214 /* Require equal access sizes */
3215 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3216 && (!TYPE_SIZE (TREE_TYPE (arg0))
3217 || !TYPE_SIZE (TREE_TYPE (arg1))
3218 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3219 TYPE_SIZE (TREE_TYPE (arg1)),
3220 flags)))
3221 return 0;
3222 /* Verify that access happens in similar types. */
3223 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3224 return 0;
3225 /* Verify that accesses are TBAA compatible. */
3226 if (!alias_ptr_types_compatible_p
3227 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3228 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3229 || (MR_DEPENDENCE_CLIQUE (arg0)
3230 != MR_DEPENDENCE_CLIQUE (arg1))
3231 || (MR_DEPENDENCE_BASE (arg0)
3232 != MR_DEPENDENCE_BASE (arg1)))
3233 return 0;
3234 /* Verify that alignment is compatible. */
3235 if (TYPE_ALIGN (TREE_TYPE (arg0))
3236 != TYPE_ALIGN (TREE_TYPE (arg1)))
3237 return 0;
3239 flags &= ~OEP_ADDRESS_OF;
3240 return (OP_SAME (0) && OP_SAME (1)
3241 /* TARGET_MEM_REF require equal extra operands. */
3242 && (TREE_CODE (arg0) != TARGET_MEM_REF
3243 || (OP_SAME_WITH_NULL (2)
3244 && OP_SAME_WITH_NULL (3)
3245 && OP_SAME_WITH_NULL (4))));
3247 case ARRAY_REF:
3248 case ARRAY_RANGE_REF:
3249 if (!OP_SAME (0))
3250 return 0;
3251 flags &= ~OEP_ADDRESS_OF;
3252 /* Compare the array index by value if it is constant first as we
3253 may have different types but same value here. */
3254 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3255 TREE_OPERAND (arg1, 1))
3256 || OP_SAME (1))
3257 && OP_SAME_WITH_NULL (2)
3258 && OP_SAME_WITH_NULL (3)
3259 /* Compare low bound and element size as with OEP_ADDRESS_OF
3260 we have to account for the offset of the ref. */
3261 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3262 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3263 || (operand_equal_p (array_ref_low_bound
3264 (CONST_CAST_TREE (arg0)),
3265 array_ref_low_bound
3266 (CONST_CAST_TREE (arg1)), flags)
3267 && operand_equal_p (array_ref_element_size
3268 (CONST_CAST_TREE (arg0)),
3269 array_ref_element_size
3270 (CONST_CAST_TREE (arg1)),
3271 flags))));
3273 case COMPONENT_REF:
3274 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3275 may be NULL when we're called to compare MEM_EXPRs. */
3276 if (!OP_SAME_WITH_NULL (0)
3277 || !OP_SAME (1))
3278 return 0;
3279 flags &= ~OEP_ADDRESS_OF;
3280 return OP_SAME_WITH_NULL (2);
3282 case BIT_FIELD_REF:
3283 if (!OP_SAME (0))
3284 return 0;
3285 flags &= ~OEP_ADDRESS_OF;
3286 return OP_SAME (1) && OP_SAME (2);
3288 default:
3289 return 0;
3292 case tcc_expression:
3293 switch (TREE_CODE (arg0))
3295 case ADDR_EXPR:
3296 /* Be sure we pass right ADDRESS_OF flag. */
3297 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3298 return operand_equal_p (TREE_OPERAND (arg0, 0),
3299 TREE_OPERAND (arg1, 0),
3300 flags | OEP_ADDRESS_OF);
3302 case TRUTH_NOT_EXPR:
3303 return OP_SAME (0);
3305 case TRUTH_ANDIF_EXPR:
3306 case TRUTH_ORIF_EXPR:
3307 return OP_SAME (0) && OP_SAME (1);
3309 case WIDEN_MULT_PLUS_EXPR:
3310 case WIDEN_MULT_MINUS_EXPR:
3311 if (!OP_SAME (2))
3312 return 0;
3313 /* The multiplcation operands are commutative. */
3314 /* FALLTHRU */
3316 case TRUTH_AND_EXPR:
3317 case TRUTH_OR_EXPR:
3318 case TRUTH_XOR_EXPR:
3319 if (OP_SAME (0) && OP_SAME (1))
3320 return 1;
3322 /* Otherwise take into account this is a commutative operation. */
3323 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3324 TREE_OPERAND (arg1, 1), flags)
3325 && operand_equal_p (TREE_OPERAND (arg0, 1),
3326 TREE_OPERAND (arg1, 0), flags));
3328 case COND_EXPR:
3329 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3330 return 0;
3331 flags &= ~OEP_ADDRESS_OF;
3332 return OP_SAME (0);
3334 case BIT_INSERT_EXPR:
3335 /* BIT_INSERT_EXPR has an implict operand as the type precision
3336 of op1. Need to check to make sure they are the same. */
3337 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3338 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3339 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3340 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3341 return false;
3342 /* FALLTHRU */
3344 case VEC_COND_EXPR:
3345 case DOT_PROD_EXPR:
3346 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3348 case MODIFY_EXPR:
3349 case INIT_EXPR:
3350 case COMPOUND_EXPR:
3351 case PREDECREMENT_EXPR:
3352 case PREINCREMENT_EXPR:
3353 case POSTDECREMENT_EXPR:
3354 case POSTINCREMENT_EXPR:
3355 if (flags & OEP_LEXICOGRAPHIC)
3356 return OP_SAME (0) && OP_SAME (1);
3357 return 0;
3359 case CLEANUP_POINT_EXPR:
3360 case EXPR_STMT:
3361 if (flags & OEP_LEXICOGRAPHIC)
3362 return OP_SAME (0);
3363 return 0;
3365 default:
3366 return 0;
3369 case tcc_vl_exp:
3370 switch (TREE_CODE (arg0))
3372 case CALL_EXPR:
3373 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3374 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3375 /* If not both CALL_EXPRs are either internal or normal function
3376 functions, then they are not equal. */
3377 return 0;
3378 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3380 /* If the CALL_EXPRs call different internal functions, then they
3381 are not equal. */
3382 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3383 return 0;
3385 else
3387 /* If the CALL_EXPRs call different functions, then they are not
3388 equal. */
3389 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3390 flags))
3391 return 0;
3394 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3396 unsigned int cef = call_expr_flags (arg0);
3397 if (flags & OEP_PURE_SAME)
3398 cef &= ECF_CONST | ECF_PURE;
3399 else
3400 cef &= ECF_CONST;
3401 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3402 return 0;
3405 /* Now see if all the arguments are the same. */
3407 const_call_expr_arg_iterator iter0, iter1;
3408 const_tree a0, a1;
3409 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3410 a1 = first_const_call_expr_arg (arg1, &iter1);
3411 a0 && a1;
3412 a0 = next_const_call_expr_arg (&iter0),
3413 a1 = next_const_call_expr_arg (&iter1))
3414 if (! operand_equal_p (a0, a1, flags))
3415 return 0;
3417 /* If we get here and both argument lists are exhausted
3418 then the CALL_EXPRs are equal. */
3419 return ! (a0 || a1);
3421 default:
3422 return 0;
3425 case tcc_declaration:
3426 /* Consider __builtin_sqrt equal to sqrt. */
3427 return (TREE_CODE (arg0) == FUNCTION_DECL
3428 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3429 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3430 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3432 case tcc_exceptional:
3433 if (TREE_CODE (arg0) == CONSTRUCTOR)
3435 /* In GIMPLE constructors are used only to build vectors from
3436 elements. Individual elements in the constructor must be
3437 indexed in increasing order and form an initial sequence.
3439 We make no effort to compare constructors in generic.
3440 (see sem_variable::equals in ipa-icf which can do so for
3441 constants). */
3442 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3443 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3444 return 0;
3446 /* Be sure that vectors constructed have the same representation.
3447 We only tested element precision and modes to match.
3448 Vectors may be BLKmode and thus also check that the number of
3449 parts match. */
3450 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
3451 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))))
3452 return 0;
3454 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3455 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3456 unsigned int len = vec_safe_length (v0);
3458 if (len != vec_safe_length (v1))
3459 return 0;
3461 for (unsigned int i = 0; i < len; i++)
3463 constructor_elt *c0 = &(*v0)[i];
3464 constructor_elt *c1 = &(*v1)[i];
3466 if (!operand_equal_p (c0->value, c1->value, flags)
3467 /* In GIMPLE the indexes can be either NULL or matching i.
3468 Double check this so we won't get false
3469 positives for GENERIC. */
3470 || (c0->index
3471 && (TREE_CODE (c0->index) != INTEGER_CST
3472 || !compare_tree_int (c0->index, i)))
3473 || (c1->index
3474 && (TREE_CODE (c1->index) != INTEGER_CST
3475 || !compare_tree_int (c1->index, i))))
3476 return 0;
3478 return 1;
3480 else if (TREE_CODE (arg0) == STATEMENT_LIST
3481 && (flags & OEP_LEXICOGRAPHIC))
3483 /* Compare the STATEMENT_LISTs. */
3484 tree_stmt_iterator tsi1, tsi2;
3485 tree body1 = CONST_CAST_TREE (arg0);
3486 tree body2 = CONST_CAST_TREE (arg1);
3487 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3488 tsi_next (&tsi1), tsi_next (&tsi2))
3490 /* The lists don't have the same number of statements. */
3491 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3492 return 0;
3493 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3494 return 1;
3495 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3496 flags & (OEP_LEXICOGRAPHIC
3497 | OEP_NO_HASH_CHECK)))
3498 return 0;
3501 return 0;
3503 case tcc_statement:
3504 switch (TREE_CODE (arg0))
3506 case RETURN_EXPR:
3507 if (flags & OEP_LEXICOGRAPHIC)
3508 return OP_SAME_WITH_NULL (0);
3509 return 0;
3510 case DEBUG_BEGIN_STMT:
3511 if (flags & OEP_LEXICOGRAPHIC)
3512 return 1;
3513 return 0;
3514 default:
3515 return 0;
3518 default:
3519 return 0;
3522 #undef OP_SAME
3523 #undef OP_SAME_WITH_NULL
3526 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
3527 with a different signedness or a narrower precision. */
3529 static bool
3530 operand_equal_for_comparison_p (tree arg0, tree arg1)
3532 if (operand_equal_p (arg0, arg1, 0))
3533 return true;
3535 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3536 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3537 return false;
3539 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3540 and see if the inner values are the same. This removes any
3541 signedness comparison, which doesn't matter here. */
3542 tree op0 = arg0;
3543 tree op1 = arg1;
3544 STRIP_NOPS (op0);
3545 STRIP_NOPS (op1);
3546 if (operand_equal_p (op0, op1, 0))
3547 return true;
3549 /* Discard a single widening conversion from ARG1 and see if the inner
3550 value is the same as ARG0. */
3551 if (CONVERT_EXPR_P (arg1)
3552 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3553 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3554 < TYPE_PRECISION (TREE_TYPE (arg1))
3555 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
3556 return true;
3558 return false;
3561 /* See if ARG is an expression that is either a comparison or is performing
3562 arithmetic on comparisons. The comparisons must only be comparing
3563 two different values, which will be stored in *CVAL1 and *CVAL2; if
3564 they are nonzero it means that some operands have already been found.
3565 No variables may be used anywhere else in the expression except in the
3566 comparisons.
3568 If this is true, return 1. Otherwise, return zero. */
3570 static int
3571 twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
3573 enum tree_code code = TREE_CODE (arg);
3574 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3576 /* We can handle some of the tcc_expression cases here. */
3577 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3578 tclass = tcc_unary;
3579 else if (tclass == tcc_expression
3580 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3581 || code == COMPOUND_EXPR))
3582 tclass = tcc_binary;
3584 switch (tclass)
3586 case tcc_unary:
3587 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
3589 case tcc_binary:
3590 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
3591 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
3593 case tcc_constant:
3594 return 1;
3596 case tcc_expression:
3597 if (code == COND_EXPR)
3598 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
3599 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
3600 && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
3601 return 0;
3603 case tcc_comparison:
3604 /* First see if we can handle the first operand, then the second. For
3605 the second operand, we know *CVAL1 can't be zero. It must be that
3606 one side of the comparison is each of the values; test for the
3607 case where this isn't true by failing if the two operands
3608 are the same. */
3610 if (operand_equal_p (TREE_OPERAND (arg, 0),
3611 TREE_OPERAND (arg, 1), 0))
3612 return 0;
3614 if (*cval1 == 0)
3615 *cval1 = TREE_OPERAND (arg, 0);
3616 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3618 else if (*cval2 == 0)
3619 *cval2 = TREE_OPERAND (arg, 0);
3620 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3622 else
3623 return 0;
3625 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3627 else if (*cval2 == 0)
3628 *cval2 = TREE_OPERAND (arg, 1);
3629 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3631 else
3632 return 0;
3634 return 1;
3636 default:
3637 return 0;
3641 /* ARG is a tree that is known to contain just arithmetic operations and
3642 comparisons. Evaluate the operations in the tree substituting NEW0 for
3643 any occurrence of OLD0 as an operand of a comparison and likewise for
3644 NEW1 and OLD1. */
3646 static tree
3647 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3648 tree old1, tree new1)
3650 tree type = TREE_TYPE (arg);
3651 enum tree_code code = TREE_CODE (arg);
3652 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3654 /* We can handle some of the tcc_expression cases here. */
3655 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3656 tclass = tcc_unary;
3657 else if (tclass == tcc_expression
3658 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3659 tclass = tcc_binary;
3661 switch (tclass)
3663 case tcc_unary:
3664 return fold_build1_loc (loc, code, type,
3665 eval_subst (loc, TREE_OPERAND (arg, 0),
3666 old0, new0, old1, new1));
3668 case tcc_binary:
3669 return fold_build2_loc (loc, code, type,
3670 eval_subst (loc, TREE_OPERAND (arg, 0),
3671 old0, new0, old1, new1),
3672 eval_subst (loc, TREE_OPERAND (arg, 1),
3673 old0, new0, old1, new1));
3675 case tcc_expression:
3676 switch (code)
3678 case SAVE_EXPR:
3679 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3680 old1, new1);
3682 case COMPOUND_EXPR:
3683 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3684 old1, new1);
3686 case COND_EXPR:
3687 return fold_build3_loc (loc, code, type,
3688 eval_subst (loc, TREE_OPERAND (arg, 0),
3689 old0, new0, old1, new1),
3690 eval_subst (loc, TREE_OPERAND (arg, 1),
3691 old0, new0, old1, new1),
3692 eval_subst (loc, TREE_OPERAND (arg, 2),
3693 old0, new0, old1, new1));
3694 default:
3695 break;
3697 /* Fall through - ??? */
3699 case tcc_comparison:
3701 tree arg0 = TREE_OPERAND (arg, 0);
3702 tree arg1 = TREE_OPERAND (arg, 1);
3704 /* We need to check both for exact equality and tree equality. The
3705 former will be true if the operand has a side-effect. In that
3706 case, we know the operand occurred exactly once. */
3708 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3709 arg0 = new0;
3710 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3711 arg0 = new1;
3713 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3714 arg1 = new0;
3715 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3716 arg1 = new1;
3718 return fold_build2_loc (loc, code, type, arg0, arg1);
3721 default:
3722 return arg;
3726 /* Return a tree for the case when the result of an expression is RESULT
3727 converted to TYPE and OMITTED was previously an operand of the expression
3728 but is now not needed (e.g., we folded OMITTED * 0).
3730 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3731 the conversion of RESULT to TYPE. */
3733 tree
3734 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3736 tree t = fold_convert_loc (loc, type, result);
3738 /* If the resulting operand is an empty statement, just return the omitted
3739 statement casted to void. */
3740 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3741 return build1_loc (loc, NOP_EXPR, void_type_node,
3742 fold_ignored_result (omitted));
3744 if (TREE_SIDE_EFFECTS (omitted))
3745 return build2_loc (loc, COMPOUND_EXPR, type,
3746 fold_ignored_result (omitted), t);
3748 return non_lvalue_loc (loc, t);
3751 /* Return a tree for the case when the result of an expression is RESULT
3752 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3753 of the expression but are now not needed.
3755 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3756 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3757 evaluated before OMITTED2. Otherwise, if neither has side effects,
3758 just do the conversion of RESULT to TYPE. */
3760 tree
3761 omit_two_operands_loc (location_t loc, tree type, tree result,
3762 tree omitted1, tree omitted2)
3764 tree t = fold_convert_loc (loc, type, result);
3766 if (TREE_SIDE_EFFECTS (omitted2))
3767 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3768 if (TREE_SIDE_EFFECTS (omitted1))
3769 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3771 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3775 /* Return a simplified tree node for the truth-negation of ARG. This
3776 never alters ARG itself. We assume that ARG is an operation that
3777 returns a truth value (0 or 1).
3779 FIXME: one would think we would fold the result, but it causes
3780 problems with the dominator optimizer. */
3782 static tree
3783 fold_truth_not_expr (location_t loc, tree arg)
3785 tree type = TREE_TYPE (arg);
3786 enum tree_code code = TREE_CODE (arg);
3787 location_t loc1, loc2;
3789 /* If this is a comparison, we can simply invert it, except for
3790 floating-point non-equality comparisons, in which case we just
3791 enclose a TRUTH_NOT_EXPR around what we have. */
3793 if (TREE_CODE_CLASS (code) == tcc_comparison)
3795 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3796 if (FLOAT_TYPE_P (op_type)
3797 && flag_trapping_math
3798 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3799 && code != NE_EXPR && code != EQ_EXPR)
3800 return NULL_TREE;
3802 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3803 if (code == ERROR_MARK)
3804 return NULL_TREE;
3806 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3807 TREE_OPERAND (arg, 1));
3808 if (TREE_NO_WARNING (arg))
3809 TREE_NO_WARNING (ret) = 1;
3810 return ret;
3813 switch (code)
3815 case INTEGER_CST:
3816 return constant_boolean_node (integer_zerop (arg), type);
3818 case TRUTH_AND_EXPR:
3819 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3820 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3821 return build2_loc (loc, TRUTH_OR_EXPR, type,
3822 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3823 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3825 case TRUTH_OR_EXPR:
3826 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3827 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3828 return build2_loc (loc, TRUTH_AND_EXPR, type,
3829 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3830 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3832 case TRUTH_XOR_EXPR:
3833 /* Here we can invert either operand. We invert the first operand
3834 unless the second operand is a TRUTH_NOT_EXPR in which case our
3835 result is the XOR of the first operand with the inside of the
3836 negation of the second operand. */
3838 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3839 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3840 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3841 else
3842 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3843 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3844 TREE_OPERAND (arg, 1));
3846 case TRUTH_ANDIF_EXPR:
3847 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3848 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3849 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3850 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3851 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3853 case TRUTH_ORIF_EXPR:
3854 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3855 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3856 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3857 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3858 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3860 case TRUTH_NOT_EXPR:
3861 return TREE_OPERAND (arg, 0);
3863 case COND_EXPR:
3865 tree arg1 = TREE_OPERAND (arg, 1);
3866 tree arg2 = TREE_OPERAND (arg, 2);
3868 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3869 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3871 /* A COND_EXPR may have a throw as one operand, which
3872 then has void type. Just leave void operands
3873 as they are. */
3874 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3875 VOID_TYPE_P (TREE_TYPE (arg1))
3876 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3877 VOID_TYPE_P (TREE_TYPE (arg2))
3878 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3881 case COMPOUND_EXPR:
3882 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3883 return build2_loc (loc, COMPOUND_EXPR, type,
3884 TREE_OPERAND (arg, 0),
3885 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3887 case NON_LVALUE_EXPR:
3888 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3889 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3891 CASE_CONVERT:
3892 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3893 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3895 /* fall through */
3897 case FLOAT_EXPR:
3898 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3899 return build1_loc (loc, TREE_CODE (arg), type,
3900 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3902 case BIT_AND_EXPR:
3903 if (!integer_onep (TREE_OPERAND (arg, 1)))
3904 return NULL_TREE;
3905 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3907 case SAVE_EXPR:
3908 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3910 case CLEANUP_POINT_EXPR:
3911 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3912 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3913 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3915 default:
3916 return NULL_TREE;
3920 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3921 assume that ARG is an operation that returns a truth value (0 or 1
3922 for scalars, 0 or -1 for vectors). Return the folded expression if
3923 folding is successful. Otherwise, return NULL_TREE. */
3925 static tree
3926 fold_invert_truthvalue (location_t loc, tree arg)
3928 tree type = TREE_TYPE (arg);
3929 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3930 ? BIT_NOT_EXPR
3931 : TRUTH_NOT_EXPR,
3932 type, arg);
3935 /* Return a simplified tree node for the truth-negation of ARG. This
3936 never alters ARG itself. We assume that ARG is an operation that
3937 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3939 tree
3940 invert_truthvalue_loc (location_t loc, tree arg)
3942 if (TREE_CODE (arg) == ERROR_MARK)
3943 return arg;
3945 tree type = TREE_TYPE (arg);
3946 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3947 ? BIT_NOT_EXPR
3948 : TRUTH_NOT_EXPR,
3949 type, arg);
3952 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3953 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3954 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3955 is the original memory reference used to preserve the alias set of
3956 the access. */
3958 static tree
3959 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3960 HOST_WIDE_INT bitsize, poly_int64 bitpos,
3961 int unsignedp, int reversep)
3963 tree result, bftype;
3965 /* Attempt not to lose the access path if possible. */
3966 if (TREE_CODE (orig_inner) == COMPONENT_REF)
3968 tree ninner = TREE_OPERAND (orig_inner, 0);
3969 machine_mode nmode;
3970 poly_int64 nbitsize, nbitpos;
3971 tree noffset;
3972 int nunsignedp, nreversep, nvolatilep = 0;
3973 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
3974 &noffset, &nmode, &nunsignedp,
3975 &nreversep, &nvolatilep);
3976 if (base == inner
3977 && noffset == NULL_TREE
3978 && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
3979 && !reversep
3980 && !nreversep
3981 && !nvolatilep)
3983 inner = ninner;
3984 bitpos -= nbitpos;
3988 alias_set_type iset = get_alias_set (orig_inner);
3989 if (iset == 0 && get_alias_set (inner) != iset)
3990 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3991 build_fold_addr_expr (inner),
3992 build_int_cst (ptr_type_node, 0));
3994 if (known_eq (bitpos, 0) && !reversep)
3996 tree size = TYPE_SIZE (TREE_TYPE (inner));
3997 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3998 || POINTER_TYPE_P (TREE_TYPE (inner)))
3999 && tree_fits_shwi_p (size)
4000 && tree_to_shwi (size) == bitsize)
4001 return fold_convert_loc (loc, type, inner);
4004 bftype = type;
4005 if (TYPE_PRECISION (bftype) != bitsize
4006 || TYPE_UNSIGNED (bftype) == !unsignedp)
4007 bftype = build_nonstandard_integer_type (bitsize, 0);
4009 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4010 bitsize_int (bitsize), bitsize_int (bitpos));
4011 REF_REVERSE_STORAGE_ORDER (result) = reversep;
4013 if (bftype != type)
4014 result = fold_convert_loc (loc, type, result);
4016 return result;
4019 /* Optimize a bit-field compare.
4021 There are two cases: First is a compare against a constant and the
4022 second is a comparison of two items where the fields are at the same
4023 bit position relative to the start of a chunk (byte, halfword, word)
4024 large enough to contain it. In these cases we can avoid the shift
4025 implicit in bitfield extractions.
4027 For constants, we emit a compare of the shifted constant with the
4028 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4029 compared. For two fields at the same position, we do the ANDs with the
4030 similar mask and compare the result of the ANDs.
4032 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4033 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4034 are the left and right operands of the comparison, respectively.
4036 If the optimization described above can be done, we return the resulting
4037 tree. Otherwise we return zero. */
4039 static tree
4040 optimize_bit_field_compare (location_t loc, enum tree_code code,
4041 tree compare_type, tree lhs, tree rhs)
4043 poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4044 HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4045 tree type = TREE_TYPE (lhs);
4046 tree unsigned_type;
4047 int const_p = TREE_CODE (rhs) == INTEGER_CST;
4048 machine_mode lmode, rmode;
4049 scalar_int_mode nmode;
4050 int lunsignedp, runsignedp;
4051 int lreversep, rreversep;
4052 int lvolatilep = 0, rvolatilep = 0;
4053 tree linner, rinner = NULL_TREE;
4054 tree mask;
4055 tree offset;
4057 /* Get all the information about the extractions being done. If the bit size
4058 is the same as the size of the underlying object, we aren't doing an
4059 extraction at all and so can do nothing. We also don't want to
4060 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4061 then will no longer be able to replace it. */
4062 linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4063 &lunsignedp, &lreversep, &lvolatilep);
4064 if (linner == lhs
4065 || !known_size_p (plbitsize)
4066 || !plbitsize.is_constant (&lbitsize)
4067 || !plbitpos.is_constant (&lbitpos)
4068 || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4069 || offset != 0
4070 || TREE_CODE (linner) == PLACEHOLDER_EXPR
4071 || lvolatilep)
4072 return 0;
4074 if (const_p)
4075 rreversep = lreversep;
4076 else
4078 /* If this is not a constant, we can only do something if bit positions,
4079 sizes, signedness and storage order are the same. */
4080 rinner
4081 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4082 &runsignedp, &rreversep, &rvolatilep);
4084 if (rinner == rhs
4085 || maybe_ne (lbitpos, rbitpos)
4086 || maybe_ne (lbitsize, rbitsize)
4087 || lunsignedp != runsignedp
4088 || lreversep != rreversep
4089 || offset != 0
4090 || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4091 || rvolatilep)
4092 return 0;
4095 /* Honor the C++ memory model and mimic what RTL expansion does. */
4096 poly_uint64 bitstart = 0;
4097 poly_uint64 bitend = 0;
4098 if (TREE_CODE (lhs) == COMPONENT_REF)
4100 get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4101 if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4102 return 0;
4105 /* See if we can find a mode to refer to this field. We should be able to,
4106 but fail if we can't. */
4107 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4108 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4109 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4110 TYPE_ALIGN (TREE_TYPE (rinner))),
4111 BITS_PER_WORD, false, &nmode))
4112 return 0;
4114 /* Set signed and unsigned types of the precision of this mode for the
4115 shifts below. */
4116 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4118 /* Compute the bit position and size for the new reference and our offset
4119 within it. If the new reference is the same size as the original, we
4120 won't optimize anything, so return zero. */
4121 nbitsize = GET_MODE_BITSIZE (nmode);
4122 nbitpos = lbitpos & ~ (nbitsize - 1);
4123 lbitpos -= nbitpos;
4124 if (nbitsize == lbitsize)
4125 return 0;
4127 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4128 lbitpos = nbitsize - lbitsize - lbitpos;
4130 /* Make the mask to be used against the extracted field. */
4131 mask = build_int_cst_type (unsigned_type, -1);
4132 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4133 mask = const_binop (RSHIFT_EXPR, mask,
4134 size_int (nbitsize - lbitsize - lbitpos));
4136 if (! const_p)
4138 if (nbitpos < 0)
4139 return 0;
4141 /* If not comparing with constant, just rework the comparison
4142 and return. */
4143 tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4144 nbitsize, nbitpos, 1, lreversep);
4145 t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4146 tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4147 nbitsize, nbitpos, 1, rreversep);
4148 t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4149 return fold_build2_loc (loc, code, compare_type, t1, t2);
4152 /* Otherwise, we are handling the constant case. See if the constant is too
4153 big for the field. Warn and return a tree for 0 (false) if so. We do
4154 this not only for its own sake, but to avoid having to test for this
4155 error case below. If we didn't, we might generate wrong code.
4157 For unsigned fields, the constant shifted right by the field length should
4158 be all zero. For signed fields, the high-order bits should agree with
4159 the sign bit. */
4161 if (lunsignedp)
4163 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4165 warning (0, "comparison is always %d due to width of bit-field",
4166 code == NE_EXPR);
4167 return constant_boolean_node (code == NE_EXPR, compare_type);
4170 else
4172 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4173 if (tem != 0 && tem != -1)
4175 warning (0, "comparison is always %d due to width of bit-field",
4176 code == NE_EXPR);
4177 return constant_boolean_node (code == NE_EXPR, compare_type);
4181 if (nbitpos < 0)
4182 return 0;
4184 /* Single-bit compares should always be against zero. */
4185 if (lbitsize == 1 && ! integer_zerop (rhs))
4187 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4188 rhs = build_int_cst (type, 0);
4191 /* Make a new bitfield reference, shift the constant over the
4192 appropriate number of bits and mask it with the computed mask
4193 (in case this was a signed field). If we changed it, make a new one. */
4194 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4195 nbitsize, nbitpos, 1, lreversep);
4197 rhs = const_binop (BIT_AND_EXPR,
4198 const_binop (LSHIFT_EXPR,
4199 fold_convert_loc (loc, unsigned_type, rhs),
4200 size_int (lbitpos)),
4201 mask);
4203 lhs = build2_loc (loc, code, compare_type,
4204 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4205 return lhs;
4208 /* Subroutine for fold_truth_andor_1: decode a field reference.
4210 If EXP is a comparison reference, we return the innermost reference.
4212 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4213 set to the starting bit number.
4215 If the innermost field can be completely contained in a mode-sized
4216 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4218 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4219 otherwise it is not changed.
4221 *PUNSIGNEDP is set to the signedness of the field.
4223 *PREVERSEP is set to the storage order of the field.
4225 *PMASK is set to the mask used. This is either contained in a
4226 BIT_AND_EXPR or derived from the width of the field.
4228 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4230 Return 0 if this is not a component reference or is one that we can't
4231 do anything with. */
4233 static tree
4234 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4235 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4236 int *punsignedp, int *preversep, int *pvolatilep,
4237 tree *pmask, tree *pand_mask)
4239 tree exp = *exp_;
4240 tree outer_type = 0;
4241 tree and_mask = 0;
4242 tree mask, inner, offset;
4243 tree unsigned_type;
4244 unsigned int precision;
4246 /* All the optimizations using this function assume integer fields.
4247 There are problems with FP fields since the type_for_size call
4248 below can fail for, e.g., XFmode. */
4249 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4250 return 0;
4252 /* We are interested in the bare arrangement of bits, so strip everything
4253 that doesn't affect the machine mode. However, record the type of the
4254 outermost expression if it may matter below. */
4255 if (CONVERT_EXPR_P (exp)
4256 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4257 outer_type = TREE_TYPE (exp);
4258 STRIP_NOPS (exp);
4260 if (TREE_CODE (exp) == BIT_AND_EXPR)
4262 and_mask = TREE_OPERAND (exp, 1);
4263 exp = TREE_OPERAND (exp, 0);
4264 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4265 if (TREE_CODE (and_mask) != INTEGER_CST)
4266 return 0;
4269 poly_int64 poly_bitsize, poly_bitpos;
4270 inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
4271 pmode, punsignedp, preversep, pvolatilep);
4272 if ((inner == exp && and_mask == 0)
4273 || !poly_bitsize.is_constant (pbitsize)
4274 || !poly_bitpos.is_constant (pbitpos)
4275 || *pbitsize < 0
4276 || offset != 0
4277 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4278 /* Reject out-of-bound accesses (PR79731). */
4279 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4280 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4281 *pbitpos + *pbitsize) < 0))
4282 return 0;
4284 *exp_ = exp;
4286 /* If the number of bits in the reference is the same as the bitsize of
4287 the outer type, then the outer type gives the signedness. Otherwise
4288 (in case of a small bitfield) the signedness is unchanged. */
4289 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4290 *punsignedp = TYPE_UNSIGNED (outer_type);
4292 /* Compute the mask to access the bitfield. */
4293 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4294 precision = TYPE_PRECISION (unsigned_type);
4296 mask = build_int_cst_type (unsigned_type, -1);
4298 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4299 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4301 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4302 if (and_mask != 0)
4303 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4304 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4306 *pmask = mask;
4307 *pand_mask = and_mask;
4308 return inner;
4311 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4312 bit positions and MASK is SIGNED. */
4314 static int
4315 all_ones_mask_p (const_tree mask, unsigned int size)
4317 tree type = TREE_TYPE (mask);
4318 unsigned int precision = TYPE_PRECISION (type);
4320 /* If this function returns true when the type of the mask is
4321 UNSIGNED, then there will be errors. In particular see
4322 gcc.c-torture/execute/990326-1.c. There does not appear to be
4323 any documentation paper trail as to why this is so. But the pre
4324 wide-int worked with that restriction and it has been preserved
4325 here. */
4326 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4327 return false;
4329 return wi::mask (size, false, precision) == wi::to_wide (mask);
4332 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4333 represents the sign bit of EXP's type. If EXP represents a sign
4334 or zero extension, also test VAL against the unextended type.
4335 The return value is the (sub)expression whose sign bit is VAL,
4336 or NULL_TREE otherwise. */
4338 tree
4339 sign_bit_p (tree exp, const_tree val)
4341 int width;
4342 tree t;
4344 /* Tree EXP must have an integral type. */
4345 t = TREE_TYPE (exp);
4346 if (! INTEGRAL_TYPE_P (t))
4347 return NULL_TREE;
4349 /* Tree VAL must be an integer constant. */
4350 if (TREE_CODE (val) != INTEGER_CST
4351 || TREE_OVERFLOW (val))
4352 return NULL_TREE;
4354 width = TYPE_PRECISION (t);
4355 if (wi::only_sign_bit_p (wi::to_wide (val), width))
4356 return exp;
4358 /* Handle extension from a narrower type. */
4359 if (TREE_CODE (exp) == NOP_EXPR
4360 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4361 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4363 return NULL_TREE;
4366 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4367 to be evaluated unconditionally. */
4369 static int
4370 simple_operand_p (const_tree exp)
4372 /* Strip any conversions that don't change the machine mode. */
4373 STRIP_NOPS (exp);
4375 return (CONSTANT_CLASS_P (exp)
4376 || TREE_CODE (exp) == SSA_NAME
4377 || (DECL_P (exp)
4378 && ! TREE_ADDRESSABLE (exp)
4379 && ! TREE_THIS_VOLATILE (exp)
4380 && ! DECL_NONLOCAL (exp)
4381 /* Don't regard global variables as simple. They may be
4382 allocated in ways unknown to the compiler (shared memory,
4383 #pragma weak, etc). */
4384 && ! TREE_PUBLIC (exp)
4385 && ! DECL_EXTERNAL (exp)
4386 /* Weakrefs are not safe to be read, since they can be NULL.
4387 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4388 have DECL_WEAK flag set. */
4389 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4390 /* Loading a static variable is unduly expensive, but global
4391 registers aren't expensive. */
4392 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4395 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4396 to be evaluated unconditionally.
4397 I addition to simple_operand_p, we assume that comparisons, conversions,
4398 and logic-not operations are simple, if their operands are simple, too. */
4400 static bool
4401 simple_operand_p_2 (tree exp)
4403 enum tree_code code;
4405 if (TREE_SIDE_EFFECTS (exp)
4406 || tree_could_trap_p (exp))
4407 return false;
4409 while (CONVERT_EXPR_P (exp))
4410 exp = TREE_OPERAND (exp, 0);
4412 code = TREE_CODE (exp);
4414 if (TREE_CODE_CLASS (code) == tcc_comparison)
4415 return (simple_operand_p (TREE_OPERAND (exp, 0))
4416 && simple_operand_p (TREE_OPERAND (exp, 1)));
4418 if (code == TRUTH_NOT_EXPR)
4419 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4421 return simple_operand_p (exp);
4425 /* The following functions are subroutines to fold_range_test and allow it to
4426 try to change a logical combination of comparisons into a range test.
4428 For example, both
4429 X == 2 || X == 3 || X == 4 || X == 5
4431 X >= 2 && X <= 5
4432 are converted to
4433 (unsigned) (X - 2) <= 3
4435 We describe each set of comparisons as being either inside or outside
4436 a range, using a variable named like IN_P, and then describe the
4437 range with a lower and upper bound. If one of the bounds is omitted,
4438 it represents either the highest or lowest value of the type.
4440 In the comments below, we represent a range by two numbers in brackets
4441 preceded by a "+" to designate being inside that range, or a "-" to
4442 designate being outside that range, so the condition can be inverted by
4443 flipping the prefix. An omitted bound is represented by a "-". For
4444 example, "- [-, 10]" means being outside the range starting at the lowest
4445 possible value and ending at 10, in other words, being greater than 10.
4446 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4447 always false.
4449 We set up things so that the missing bounds are handled in a consistent
4450 manner so neither a missing bound nor "true" and "false" need to be
4451 handled using a special case. */
4453 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4454 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4455 and UPPER1_P are nonzero if the respective argument is an upper bound
4456 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4457 must be specified for a comparison. ARG1 will be converted to ARG0's
4458 type if both are specified. */
4460 static tree
4461 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4462 tree arg1, int upper1_p)
4464 tree tem;
4465 int result;
4466 int sgn0, sgn1;
4468 /* If neither arg represents infinity, do the normal operation.
4469 Else, if not a comparison, return infinity. Else handle the special
4470 comparison rules. Note that most of the cases below won't occur, but
4471 are handled for consistency. */
4473 if (arg0 != 0 && arg1 != 0)
4475 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4476 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4477 STRIP_NOPS (tem);
4478 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4481 if (TREE_CODE_CLASS (code) != tcc_comparison)
4482 return 0;
4484 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4485 for neither. In real maths, we cannot assume open ended ranges are
4486 the same. But, this is computer arithmetic, where numbers are finite.
4487 We can therefore make the transformation of any unbounded range with
4488 the value Z, Z being greater than any representable number. This permits
4489 us to treat unbounded ranges as equal. */
4490 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4491 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4492 switch (code)
4494 case EQ_EXPR:
4495 result = sgn0 == sgn1;
4496 break;
4497 case NE_EXPR:
4498 result = sgn0 != sgn1;
4499 break;
4500 case LT_EXPR:
4501 result = sgn0 < sgn1;
4502 break;
4503 case LE_EXPR:
4504 result = sgn0 <= sgn1;
4505 break;
4506 case GT_EXPR:
4507 result = sgn0 > sgn1;
4508 break;
4509 case GE_EXPR:
4510 result = sgn0 >= sgn1;
4511 break;
4512 default:
4513 gcc_unreachable ();
4516 return constant_boolean_node (result, type);
4519 /* Helper routine for make_range. Perform one step for it, return
4520 new expression if the loop should continue or NULL_TREE if it should
4521 stop. */
4523 tree
4524 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4525 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4526 bool *strict_overflow_p)
4528 tree arg0_type = TREE_TYPE (arg0);
4529 tree n_low, n_high, low = *p_low, high = *p_high;
4530 int in_p = *p_in_p, n_in_p;
4532 switch (code)
4534 case TRUTH_NOT_EXPR:
4535 /* We can only do something if the range is testing for zero. */
4536 if (low == NULL_TREE || high == NULL_TREE
4537 || ! integer_zerop (low) || ! integer_zerop (high))
4538 return NULL_TREE;
4539 *p_in_p = ! in_p;
4540 return arg0;
4542 case EQ_EXPR: case NE_EXPR:
4543 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4544 /* We can only do something if the range is testing for zero
4545 and if the second operand is an integer constant. Note that
4546 saying something is "in" the range we make is done by
4547 complementing IN_P since it will set in the initial case of
4548 being not equal to zero; "out" is leaving it alone. */
4549 if (low == NULL_TREE || high == NULL_TREE
4550 || ! integer_zerop (low) || ! integer_zerop (high)
4551 || TREE_CODE (arg1) != INTEGER_CST)
4552 return NULL_TREE;
4554 switch (code)
4556 case NE_EXPR: /* - [c, c] */
4557 low = high = arg1;
4558 break;
4559 case EQ_EXPR: /* + [c, c] */
4560 in_p = ! in_p, low = high = arg1;
4561 break;
4562 case GT_EXPR: /* - [-, c] */
4563 low = 0, high = arg1;
4564 break;
4565 case GE_EXPR: /* + [c, -] */
4566 in_p = ! in_p, low = arg1, high = 0;
4567 break;
4568 case LT_EXPR: /* - [c, -] */
4569 low = arg1, high = 0;
4570 break;
4571 case LE_EXPR: /* + [-, c] */
4572 in_p = ! in_p, low = 0, high = arg1;
4573 break;
4574 default:
4575 gcc_unreachable ();
4578 /* If this is an unsigned comparison, we also know that EXP is
4579 greater than or equal to zero. We base the range tests we make
4580 on that fact, so we record it here so we can parse existing
4581 range tests. We test arg0_type since often the return type
4582 of, e.g. EQ_EXPR, is boolean. */
4583 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4585 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4586 in_p, low, high, 1,
4587 build_int_cst (arg0_type, 0),
4588 NULL_TREE))
4589 return NULL_TREE;
4591 in_p = n_in_p, low = n_low, high = n_high;
4593 /* If the high bound is missing, but we have a nonzero low
4594 bound, reverse the range so it goes from zero to the low bound
4595 minus 1. */
4596 if (high == 0 && low && ! integer_zerop (low))
4598 in_p = ! in_p;
4599 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4600 build_int_cst (TREE_TYPE (low), 1), 0);
4601 low = build_int_cst (arg0_type, 0);
4605 *p_low = low;
4606 *p_high = high;
4607 *p_in_p = in_p;
4608 return arg0;
4610 case NEGATE_EXPR:
4611 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4612 low and high are non-NULL, then normalize will DTRT. */
4613 if (!TYPE_UNSIGNED (arg0_type)
4614 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4616 if (low == NULL_TREE)
4617 low = TYPE_MIN_VALUE (arg0_type);
4618 if (high == NULL_TREE)
4619 high = TYPE_MAX_VALUE (arg0_type);
4622 /* (-x) IN [a,b] -> x in [-b, -a] */
4623 n_low = range_binop (MINUS_EXPR, exp_type,
4624 build_int_cst (exp_type, 0),
4625 0, high, 1);
4626 n_high = range_binop (MINUS_EXPR, exp_type,
4627 build_int_cst (exp_type, 0),
4628 0, low, 0);
4629 if (n_high != 0 && TREE_OVERFLOW (n_high))
4630 return NULL_TREE;
4631 goto normalize;
4633 case BIT_NOT_EXPR:
4634 /* ~ X -> -X - 1 */
4635 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4636 build_int_cst (exp_type, 1));
4638 case PLUS_EXPR:
4639 case MINUS_EXPR:
4640 if (TREE_CODE (arg1) != INTEGER_CST)
4641 return NULL_TREE;
4643 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4644 move a constant to the other side. */
4645 if (!TYPE_UNSIGNED (arg0_type)
4646 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4647 return NULL_TREE;
4649 /* If EXP is signed, any overflow in the computation is undefined,
4650 so we don't worry about it so long as our computations on
4651 the bounds don't overflow. For unsigned, overflow is defined
4652 and this is exactly the right thing. */
4653 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4654 arg0_type, low, 0, arg1, 0);
4655 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4656 arg0_type, high, 1, arg1, 0);
4657 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4658 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4659 return NULL_TREE;
4661 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4662 *strict_overflow_p = true;
4664 normalize:
4665 /* Check for an unsigned range which has wrapped around the maximum
4666 value thus making n_high < n_low, and normalize it. */
4667 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4669 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4670 build_int_cst (TREE_TYPE (n_high), 1), 0);
4671 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4672 build_int_cst (TREE_TYPE (n_low), 1), 0);
4674 /* If the range is of the form +/- [ x+1, x ], we won't
4675 be able to normalize it. But then, it represents the
4676 whole range or the empty set, so make it
4677 +/- [ -, - ]. */
4678 if (tree_int_cst_equal (n_low, low)
4679 && tree_int_cst_equal (n_high, high))
4680 low = high = 0;
4681 else
4682 in_p = ! in_p;
4684 else
4685 low = n_low, high = n_high;
4687 *p_low = low;
4688 *p_high = high;
4689 *p_in_p = in_p;
4690 return arg0;
4692 CASE_CONVERT:
4693 case NON_LVALUE_EXPR:
4694 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4695 return NULL_TREE;
4697 if (! INTEGRAL_TYPE_P (arg0_type)
4698 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4699 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4700 return NULL_TREE;
4702 n_low = low, n_high = high;
4704 if (n_low != 0)
4705 n_low = fold_convert_loc (loc, arg0_type, n_low);
4707 if (n_high != 0)
4708 n_high = fold_convert_loc (loc, arg0_type, n_high);
4710 /* If we're converting arg0 from an unsigned type, to exp,
4711 a signed type, we will be doing the comparison as unsigned.
4712 The tests above have already verified that LOW and HIGH
4713 are both positive.
4715 So we have to ensure that we will handle large unsigned
4716 values the same way that the current signed bounds treat
4717 negative values. */
4719 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4721 tree high_positive;
4722 tree equiv_type;
4723 /* For fixed-point modes, we need to pass the saturating flag
4724 as the 2nd parameter. */
4725 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4726 equiv_type
4727 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4728 TYPE_SATURATING (arg0_type));
4729 else
4730 equiv_type
4731 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4733 /* A range without an upper bound is, naturally, unbounded.
4734 Since convert would have cropped a very large value, use
4735 the max value for the destination type. */
4736 high_positive
4737 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4738 : TYPE_MAX_VALUE (arg0_type);
4740 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4741 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4742 fold_convert_loc (loc, arg0_type,
4743 high_positive),
4744 build_int_cst (arg0_type, 1));
4746 /* If the low bound is specified, "and" the range with the
4747 range for which the original unsigned value will be
4748 positive. */
4749 if (low != 0)
4751 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4752 1, fold_convert_loc (loc, arg0_type,
4753 integer_zero_node),
4754 high_positive))
4755 return NULL_TREE;
4757 in_p = (n_in_p == in_p);
4759 else
4761 /* Otherwise, "or" the range with the range of the input
4762 that will be interpreted as negative. */
4763 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4764 1, fold_convert_loc (loc, arg0_type,
4765 integer_zero_node),
4766 high_positive))
4767 return NULL_TREE;
4769 in_p = (in_p != n_in_p);
4773 *p_low = n_low;
4774 *p_high = n_high;
4775 *p_in_p = in_p;
4776 return arg0;
4778 default:
4779 return NULL_TREE;
4783 /* Given EXP, a logical expression, set the range it is testing into
4784 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4785 actually being tested. *PLOW and *PHIGH will be made of the same
4786 type as the returned expression. If EXP is not a comparison, we
4787 will most likely not be returning a useful value and range. Set
4788 *STRICT_OVERFLOW_P to true if the return value is only valid
4789 because signed overflow is undefined; otherwise, do not change
4790 *STRICT_OVERFLOW_P. */
4792 tree
4793 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4794 bool *strict_overflow_p)
4796 enum tree_code code;
4797 tree arg0, arg1 = NULL_TREE;
4798 tree exp_type, nexp;
4799 int in_p;
4800 tree low, high;
4801 location_t loc = EXPR_LOCATION (exp);
4803 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4804 and see if we can refine the range. Some of the cases below may not
4805 happen, but it doesn't seem worth worrying about this. We "continue"
4806 the outer loop when we've changed something; otherwise we "break"
4807 the switch, which will "break" the while. */
4809 in_p = 0;
4810 low = high = build_int_cst (TREE_TYPE (exp), 0);
4812 while (1)
4814 code = TREE_CODE (exp);
4815 exp_type = TREE_TYPE (exp);
4816 arg0 = NULL_TREE;
4818 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4820 if (TREE_OPERAND_LENGTH (exp) > 0)
4821 arg0 = TREE_OPERAND (exp, 0);
4822 if (TREE_CODE_CLASS (code) == tcc_binary
4823 || TREE_CODE_CLASS (code) == tcc_comparison
4824 || (TREE_CODE_CLASS (code) == tcc_expression
4825 && TREE_OPERAND_LENGTH (exp) > 1))
4826 arg1 = TREE_OPERAND (exp, 1);
4828 if (arg0 == NULL_TREE)
4829 break;
4831 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4832 &high, &in_p, strict_overflow_p);
4833 if (nexp == NULL_TREE)
4834 break;
4835 exp = nexp;
4838 /* If EXP is a constant, we can evaluate whether this is true or false. */
4839 if (TREE_CODE (exp) == INTEGER_CST)
4841 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4842 exp, 0, low, 0))
4843 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4844 exp, 1, high, 1)));
4845 low = high = 0;
4846 exp = 0;
4849 *pin_p = in_p, *plow = low, *phigh = high;
4850 return exp;
4853 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4854 a bitwise check i.e. when
4855 LOW == 0xXX...X00...0
4856 HIGH == 0xXX...X11...1
4857 Return corresponding mask in MASK and stem in VALUE. */
4859 static bool
4860 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4861 tree *value)
4863 if (TREE_CODE (low) != INTEGER_CST
4864 || TREE_CODE (high) != INTEGER_CST)
4865 return false;
4867 unsigned prec = TYPE_PRECISION (type);
4868 wide_int lo = wi::to_wide (low, prec);
4869 wide_int hi = wi::to_wide (high, prec);
4871 wide_int end_mask = lo ^ hi;
4872 if ((end_mask & (end_mask + 1)) != 0
4873 || (lo & end_mask) != 0)
4874 return false;
4876 wide_int stem_mask = ~end_mask;
4877 wide_int stem = lo & stem_mask;
4878 if (stem != (hi & stem_mask))
4879 return false;
4881 *mask = wide_int_to_tree (type, stem_mask);
4882 *value = wide_int_to_tree (type, stem);
4884 return true;
4887 /* Helper routine for build_range_check and match.pd. Return the type to
4888 perform the check or NULL if it shouldn't be optimized. */
4890 tree
4891 range_check_type (tree etype)
4893 /* First make sure that arithmetics in this type is valid, then make sure
4894 that it wraps around. */
4895 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4896 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4897 TYPE_UNSIGNED (etype));
4899 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4901 tree utype, minv, maxv;
4903 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4904 for the type in question, as we rely on this here. */
4905 utype = unsigned_type_for (etype);
4906 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4907 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4908 build_int_cst (TREE_TYPE (maxv), 1), 1);
4909 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4911 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4912 minv, 1, maxv, 1)))
4913 etype = utype;
4914 else
4915 return NULL_TREE;
4917 return etype;
4920 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4921 type, TYPE, return an expression to test if EXP is in (or out of, depending
4922 on IN_P) the range. Return 0 if the test couldn't be created. */
4924 tree
4925 build_range_check (location_t loc, tree type, tree exp, int in_p,
4926 tree low, tree high)
4928 tree etype = TREE_TYPE (exp), mask, value;
4930 /* Disable this optimization for function pointer expressions
4931 on targets that require function pointer canonicalization. */
4932 if (targetm.have_canonicalize_funcptr_for_compare ()
4933 && TREE_CODE (etype) == POINTER_TYPE
4934 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4935 return NULL_TREE;
4937 if (! in_p)
4939 value = build_range_check (loc, type, exp, 1, low, high);
4940 if (value != 0)
4941 return invert_truthvalue_loc (loc, value);
4943 return 0;
4946 if (low == 0 && high == 0)
4947 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4949 if (low == 0)
4950 return fold_build2_loc (loc, LE_EXPR, type, exp,
4951 fold_convert_loc (loc, etype, high));
4953 if (high == 0)
4954 return fold_build2_loc (loc, GE_EXPR, type, exp,
4955 fold_convert_loc (loc, etype, low));
4957 if (operand_equal_p (low, high, 0))
4958 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4959 fold_convert_loc (loc, etype, low));
4961 if (TREE_CODE (exp) == BIT_AND_EXPR
4962 && maskable_range_p (low, high, etype, &mask, &value))
4963 return fold_build2_loc (loc, EQ_EXPR, type,
4964 fold_build2_loc (loc, BIT_AND_EXPR, etype,
4965 exp, mask),
4966 value);
4968 if (integer_zerop (low))
4970 if (! TYPE_UNSIGNED (etype))
4972 etype = unsigned_type_for (etype);
4973 high = fold_convert_loc (loc, etype, high);
4974 exp = fold_convert_loc (loc, etype, exp);
4976 return build_range_check (loc, type, exp, 1, 0, high);
4979 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4980 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4982 int prec = TYPE_PRECISION (etype);
4984 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
4986 if (TYPE_UNSIGNED (etype))
4988 tree signed_etype = signed_type_for (etype);
4989 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4990 etype
4991 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4992 else
4993 etype = signed_etype;
4994 exp = fold_convert_loc (loc, etype, exp);
4996 return fold_build2_loc (loc, GT_EXPR, type, exp,
4997 build_int_cst (etype, 0));
5001 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5002 This requires wrap-around arithmetics for the type of the expression. */
5003 etype = range_check_type (etype);
5004 if (etype == NULL_TREE)
5005 return NULL_TREE;
5007 if (POINTER_TYPE_P (etype))
5008 etype = unsigned_type_for (etype);
5010 high = fold_convert_loc (loc, etype, high);
5011 low = fold_convert_loc (loc, etype, low);
5012 exp = fold_convert_loc (loc, etype, exp);
5014 value = const_binop (MINUS_EXPR, high, low);
5016 if (value != 0 && !TREE_OVERFLOW (value))
5017 return build_range_check (loc, type,
5018 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5019 1, build_int_cst (etype, 0), value);
5021 return 0;
5024 /* Return the predecessor of VAL in its type, handling the infinite case. */
5026 static tree
5027 range_predecessor (tree val)
5029 tree type = TREE_TYPE (val);
5031 if (INTEGRAL_TYPE_P (type)
5032 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5033 return 0;
5034 else
5035 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5036 build_int_cst (TREE_TYPE (val), 1), 0);
5039 /* Return the successor of VAL in its type, handling the infinite case. */
5041 static tree
5042 range_successor (tree val)
5044 tree type = TREE_TYPE (val);
5046 if (INTEGRAL_TYPE_P (type)
5047 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5048 return 0;
5049 else
5050 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5051 build_int_cst (TREE_TYPE (val), 1), 0);
5054 /* Given two ranges, see if we can merge them into one. Return 1 if we
5055 can, 0 if we can't. Set the output range into the specified parameters. */
5057 bool
5058 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5059 tree high0, int in1_p, tree low1, tree high1)
5061 int no_overlap;
5062 int subset;
5063 int temp;
5064 tree tem;
5065 int in_p;
5066 tree low, high;
5067 int lowequal = ((low0 == 0 && low1 == 0)
5068 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5069 low0, 0, low1, 0)));
5070 int highequal = ((high0 == 0 && high1 == 0)
5071 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5072 high0, 1, high1, 1)));
5074 /* Make range 0 be the range that starts first, or ends last if they
5075 start at the same value. Swap them if it isn't. */
5076 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5077 low0, 0, low1, 0))
5078 || (lowequal
5079 && integer_onep (range_binop (GT_EXPR, integer_type_node,
5080 high1, 1, high0, 1))))
5082 temp = in0_p, in0_p = in1_p, in1_p = temp;
5083 tem = low0, low0 = low1, low1 = tem;
5084 tem = high0, high0 = high1, high1 = tem;
5087 /* Now flag two cases, whether the ranges are disjoint or whether the
5088 second range is totally subsumed in the first. Note that the tests
5089 below are simplified by the ones above. */
5090 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5091 high0, 1, low1, 0));
5092 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5093 high1, 1, high0, 1));
5095 /* We now have four cases, depending on whether we are including or
5096 excluding the two ranges. */
5097 if (in0_p && in1_p)
5099 /* If they don't overlap, the result is false. If the second range
5100 is a subset it is the result. Otherwise, the range is from the start
5101 of the second to the end of the first. */
5102 if (no_overlap)
5103 in_p = 0, low = high = 0;
5104 else if (subset)
5105 in_p = 1, low = low1, high = high1;
5106 else
5107 in_p = 1, low = low1, high = high0;
5110 else if (in0_p && ! in1_p)
5112 /* If they don't overlap, the result is the first range. If they are
5113 equal, the result is false. If the second range is a subset of the
5114 first, and the ranges begin at the same place, we go from just after
5115 the end of the second range to the end of the first. If the second
5116 range is not a subset of the first, or if it is a subset and both
5117 ranges end at the same place, the range starts at the start of the
5118 first range and ends just before the second range.
5119 Otherwise, we can't describe this as a single range. */
5120 if (no_overlap)
5121 in_p = 1, low = low0, high = high0;
5122 else if (lowequal && highequal)
5123 in_p = 0, low = high = 0;
5124 else if (subset && lowequal)
5126 low = range_successor (high1);
5127 high = high0;
5128 in_p = 1;
5129 if (low == 0)
5131 /* We are in the weird situation where high0 > high1 but
5132 high1 has no successor. Punt. */
5133 return 0;
5136 else if (! subset || highequal)
5138 low = low0;
5139 high = range_predecessor (low1);
5140 in_p = 1;
5141 if (high == 0)
5143 /* low0 < low1 but low1 has no predecessor. Punt. */
5144 return 0;
5147 else
5148 return 0;
5151 else if (! in0_p && in1_p)
5153 /* If they don't overlap, the result is the second range. If the second
5154 is a subset of the first, the result is false. Otherwise,
5155 the range starts just after the first range and ends at the
5156 end of the second. */
5157 if (no_overlap)
5158 in_p = 1, low = low1, high = high1;
5159 else if (subset || highequal)
5160 in_p = 0, low = high = 0;
5161 else
5163 low = range_successor (high0);
5164 high = high1;
5165 in_p = 1;
5166 if (low == 0)
5168 /* high1 > high0 but high0 has no successor. Punt. */
5169 return 0;
5174 else
5176 /* The case where we are excluding both ranges. Here the complex case
5177 is if they don't overlap. In that case, the only time we have a
5178 range is if they are adjacent. If the second is a subset of the
5179 first, the result is the first. Otherwise, the range to exclude
5180 starts at the beginning of the first range and ends at the end of the
5181 second. */
5182 if (no_overlap)
5184 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5185 range_successor (high0),
5186 1, low1, 0)))
5187 in_p = 0, low = low0, high = high1;
5188 else
5190 /* Canonicalize - [min, x] into - [-, x]. */
5191 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5192 switch (TREE_CODE (TREE_TYPE (low0)))
5194 case ENUMERAL_TYPE:
5195 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5196 GET_MODE_BITSIZE
5197 (TYPE_MODE (TREE_TYPE (low0)))))
5198 break;
5199 /* FALLTHROUGH */
5200 case INTEGER_TYPE:
5201 if (tree_int_cst_equal (low0,
5202 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5203 low0 = 0;
5204 break;
5205 case POINTER_TYPE:
5206 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5207 && integer_zerop (low0))
5208 low0 = 0;
5209 break;
5210 default:
5211 break;
5214 /* Canonicalize - [x, max] into - [x, -]. */
5215 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5216 switch (TREE_CODE (TREE_TYPE (high1)))
5218 case ENUMERAL_TYPE:
5219 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5220 GET_MODE_BITSIZE
5221 (TYPE_MODE (TREE_TYPE (high1)))))
5222 break;
5223 /* FALLTHROUGH */
5224 case INTEGER_TYPE:
5225 if (tree_int_cst_equal (high1,
5226 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5227 high1 = 0;
5228 break;
5229 case POINTER_TYPE:
5230 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5231 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5232 high1, 1,
5233 build_int_cst (TREE_TYPE (high1), 1),
5234 1)))
5235 high1 = 0;
5236 break;
5237 default:
5238 break;
5241 /* The ranges might be also adjacent between the maximum and
5242 minimum values of the given type. For
5243 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5244 return + [x + 1, y - 1]. */
5245 if (low0 == 0 && high1 == 0)
5247 low = range_successor (high0);
5248 high = range_predecessor (low1);
5249 if (low == 0 || high == 0)
5250 return 0;
5252 in_p = 1;
5254 else
5255 return 0;
5258 else if (subset)
5259 in_p = 0, low = low0, high = high0;
5260 else
5261 in_p = 0, low = low0, high = high1;
5264 *pin_p = in_p, *plow = low, *phigh = high;
5265 return 1;
5269 /* Subroutine of fold, looking inside expressions of the form
5270 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5271 of the COND_EXPR. This function is being used also to optimize
5272 A op B ? C : A, by reversing the comparison first.
5274 Return a folded expression whose code is not a COND_EXPR
5275 anymore, or NULL_TREE if no folding opportunity is found. */
5277 static tree
5278 fold_cond_expr_with_comparison (location_t loc, tree type,
5279 tree arg0, tree arg1, tree arg2)
5281 enum tree_code comp_code = TREE_CODE (arg0);
5282 tree arg00 = TREE_OPERAND (arg0, 0);
5283 tree arg01 = TREE_OPERAND (arg0, 1);
5284 tree arg1_type = TREE_TYPE (arg1);
5285 tree tem;
5287 STRIP_NOPS (arg1);
5288 STRIP_NOPS (arg2);
5290 /* If we have A op 0 ? A : -A, consider applying the following
5291 transformations:
5293 A == 0? A : -A same as -A
5294 A != 0? A : -A same as A
5295 A >= 0? A : -A same as abs (A)
5296 A > 0? A : -A same as abs (A)
5297 A <= 0? A : -A same as -abs (A)
5298 A < 0? A : -A same as -abs (A)
5300 None of these transformations work for modes with signed
5301 zeros. If A is +/-0, the first two transformations will
5302 change the sign of the result (from +0 to -0, or vice
5303 versa). The last four will fix the sign of the result,
5304 even though the original expressions could be positive or
5305 negative, depending on the sign of A.
5307 Note that all these transformations are correct if A is
5308 NaN, since the two alternatives (A and -A) are also NaNs. */
5309 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5310 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5311 ? real_zerop (arg01)
5312 : integer_zerop (arg01))
5313 && ((TREE_CODE (arg2) == NEGATE_EXPR
5314 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5315 /* In the case that A is of the form X-Y, '-A' (arg2) may
5316 have already been folded to Y-X, check for that. */
5317 || (TREE_CODE (arg1) == MINUS_EXPR
5318 && TREE_CODE (arg2) == MINUS_EXPR
5319 && operand_equal_p (TREE_OPERAND (arg1, 0),
5320 TREE_OPERAND (arg2, 1), 0)
5321 && operand_equal_p (TREE_OPERAND (arg1, 1),
5322 TREE_OPERAND (arg2, 0), 0))))
5323 switch (comp_code)
5325 case EQ_EXPR:
5326 case UNEQ_EXPR:
5327 tem = fold_convert_loc (loc, arg1_type, arg1);
5328 return fold_convert_loc (loc, type, negate_expr (tem));
5329 case NE_EXPR:
5330 case LTGT_EXPR:
5331 return fold_convert_loc (loc, type, arg1);
5332 case UNGE_EXPR:
5333 case UNGT_EXPR:
5334 if (flag_trapping_math)
5335 break;
5336 /* Fall through. */
5337 case GE_EXPR:
5338 case GT_EXPR:
5339 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5340 break;
5341 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5342 return fold_convert_loc (loc, type, tem);
5343 case UNLE_EXPR:
5344 case UNLT_EXPR:
5345 if (flag_trapping_math)
5346 break;
5347 /* FALLTHRU */
5348 case LE_EXPR:
5349 case LT_EXPR:
5350 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5351 break;
5352 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5353 return negate_expr (fold_convert_loc (loc, type, tem));
5354 default:
5355 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5356 break;
5359 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5360 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5361 both transformations are correct when A is NaN: A != 0
5362 is then true, and A == 0 is false. */
5364 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5365 && integer_zerop (arg01) && integer_zerop (arg2))
5367 if (comp_code == NE_EXPR)
5368 return fold_convert_loc (loc, type, arg1);
5369 else if (comp_code == EQ_EXPR)
5370 return build_zero_cst (type);
5373 /* Try some transformations of A op B ? A : B.
5375 A == B? A : B same as B
5376 A != B? A : B same as A
5377 A >= B? A : B same as max (A, B)
5378 A > B? A : B same as max (B, A)
5379 A <= B? A : B same as min (A, B)
5380 A < B? A : B same as min (B, A)
5382 As above, these transformations don't work in the presence
5383 of signed zeros. For example, if A and B are zeros of
5384 opposite sign, the first two transformations will change
5385 the sign of the result. In the last four, the original
5386 expressions give different results for (A=+0, B=-0) and
5387 (A=-0, B=+0), but the transformed expressions do not.
5389 The first two transformations are correct if either A or B
5390 is a NaN. In the first transformation, the condition will
5391 be false, and B will indeed be chosen. In the case of the
5392 second transformation, the condition A != B will be true,
5393 and A will be chosen.
5395 The conversions to max() and min() are not correct if B is
5396 a number and A is not. The conditions in the original
5397 expressions will be false, so all four give B. The min()
5398 and max() versions would give a NaN instead. */
5399 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5400 && operand_equal_for_comparison_p (arg01, arg2)
5401 /* Avoid these transformations if the COND_EXPR may be used
5402 as an lvalue in the C++ front-end. PR c++/19199. */
5403 && (in_gimple_form
5404 || VECTOR_TYPE_P (type)
5405 || (! lang_GNU_CXX ()
5406 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5407 || ! maybe_lvalue_p (arg1)
5408 || ! maybe_lvalue_p (arg2)))
5410 tree comp_op0 = arg00;
5411 tree comp_op1 = arg01;
5412 tree comp_type = TREE_TYPE (comp_op0);
5414 switch (comp_code)
5416 case EQ_EXPR:
5417 return fold_convert_loc (loc, type, arg2);
5418 case NE_EXPR:
5419 return fold_convert_loc (loc, type, arg1);
5420 case LE_EXPR:
5421 case LT_EXPR:
5422 case UNLE_EXPR:
5423 case UNLT_EXPR:
5424 /* In C++ a ?: expression can be an lvalue, so put the
5425 operand which will be used if they are equal first
5426 so that we can convert this back to the
5427 corresponding COND_EXPR. */
5428 if (!HONOR_NANS (arg1))
5430 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5431 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5432 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5433 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5434 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5435 comp_op1, comp_op0);
5436 return fold_convert_loc (loc, type, tem);
5438 break;
5439 case GE_EXPR:
5440 case GT_EXPR:
5441 case UNGE_EXPR:
5442 case UNGT_EXPR:
5443 if (!HONOR_NANS (arg1))
5445 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5446 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5447 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5448 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5449 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5450 comp_op1, comp_op0);
5451 return fold_convert_loc (loc, type, tem);
5453 break;
5454 case UNEQ_EXPR:
5455 if (!HONOR_NANS (arg1))
5456 return fold_convert_loc (loc, type, arg2);
5457 break;
5458 case LTGT_EXPR:
5459 if (!HONOR_NANS (arg1))
5460 return fold_convert_loc (loc, type, arg1);
5461 break;
5462 default:
5463 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5464 break;
5468 return NULL_TREE;
5473 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5474 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5475 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5476 false) >= 2)
5477 #endif
5479 /* EXP is some logical combination of boolean tests. See if we can
5480 merge it into some range test. Return the new tree if so. */
5482 static tree
5483 fold_range_test (location_t loc, enum tree_code code, tree type,
5484 tree op0, tree op1)
5486 int or_op = (code == TRUTH_ORIF_EXPR
5487 || code == TRUTH_OR_EXPR);
5488 int in0_p, in1_p, in_p;
5489 tree low0, low1, low, high0, high1, high;
5490 bool strict_overflow_p = false;
5491 tree tem, lhs, rhs;
5492 const char * const warnmsg = G_("assuming signed overflow does not occur "
5493 "when simplifying range test");
5495 if (!INTEGRAL_TYPE_P (type))
5496 return 0;
5498 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5499 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5501 /* If this is an OR operation, invert both sides; we will invert
5502 again at the end. */
5503 if (or_op)
5504 in0_p = ! in0_p, in1_p = ! in1_p;
5506 /* If both expressions are the same, if we can merge the ranges, and we
5507 can build the range test, return it or it inverted. If one of the
5508 ranges is always true or always false, consider it to be the same
5509 expression as the other. */
5510 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5511 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5512 in1_p, low1, high1)
5513 && (tem = (build_range_check (loc, type,
5514 lhs != 0 ? lhs
5515 : rhs != 0 ? rhs : integer_zero_node,
5516 in_p, low, high))) != 0)
5518 if (strict_overflow_p)
5519 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5520 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5523 /* On machines where the branch cost is expensive, if this is a
5524 short-circuited branch and the underlying object on both sides
5525 is the same, make a non-short-circuit operation. */
5526 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5527 && !flag_sanitize_coverage
5528 && lhs != 0 && rhs != 0
5529 && (code == TRUTH_ANDIF_EXPR
5530 || code == TRUTH_ORIF_EXPR)
5531 && operand_equal_p (lhs, rhs, 0))
5533 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5534 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5535 which cases we can't do this. */
5536 if (simple_operand_p (lhs))
5537 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5538 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5539 type, op0, op1);
5541 else if (!lang_hooks.decls.global_bindings_p ()
5542 && !CONTAINS_PLACEHOLDER_P (lhs))
5544 tree common = save_expr (lhs);
5546 if ((lhs = build_range_check (loc, type, common,
5547 or_op ? ! in0_p : in0_p,
5548 low0, high0)) != 0
5549 && (rhs = build_range_check (loc, type, common,
5550 or_op ? ! in1_p : in1_p,
5551 low1, high1)) != 0)
5553 if (strict_overflow_p)
5554 fold_overflow_warning (warnmsg,
5555 WARN_STRICT_OVERFLOW_COMPARISON);
5556 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5557 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5558 type, lhs, rhs);
5563 return 0;
5566 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5567 bit value. Arrange things so the extra bits will be set to zero if and
5568 only if C is signed-extended to its full width. If MASK is nonzero,
5569 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5571 static tree
5572 unextend (tree c, int p, int unsignedp, tree mask)
5574 tree type = TREE_TYPE (c);
5575 int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
5576 tree temp;
5578 if (p == modesize || unsignedp)
5579 return c;
5581 /* We work by getting just the sign bit into the low-order bit, then
5582 into the high-order bit, then sign-extend. We then XOR that value
5583 with C. */
5584 temp = build_int_cst (TREE_TYPE (c),
5585 wi::extract_uhwi (wi::to_wide (c), p - 1, 1));
5587 /* We must use a signed type in order to get an arithmetic right shift.
5588 However, we must also avoid introducing accidental overflows, so that
5589 a subsequent call to integer_zerop will work. Hence we must
5590 do the type conversion here. At this point, the constant is either
5591 zero or one, and the conversion to a signed type can never overflow.
5592 We could get an overflow if this conversion is done anywhere else. */
5593 if (TYPE_UNSIGNED (type))
5594 temp = fold_convert (signed_type_for (type), temp);
5596 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5597 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5598 if (mask != 0)
5599 temp = const_binop (BIT_AND_EXPR, temp,
5600 fold_convert (TREE_TYPE (c), mask));
5601 /* If necessary, convert the type back to match the type of C. */
5602 if (TYPE_UNSIGNED (type))
5603 temp = fold_convert (type, temp);
5605 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5608 /* For an expression that has the form
5609 (A && B) || ~B
5611 (A || B) && ~B,
5612 we can drop one of the inner expressions and simplify to
5613 A || ~B
5615 A && ~B
5616 LOC is the location of the resulting expression. OP is the inner
5617 logical operation; the left-hand side in the examples above, while CMPOP
5618 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5619 removing a condition that guards another, as in
5620 (A != NULL && A->...) || A == NULL
5621 which we must not transform. If RHS_ONLY is true, only eliminate the
5622 right-most operand of the inner logical operation. */
5624 static tree
5625 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5626 bool rhs_only)
5628 tree type = TREE_TYPE (cmpop);
5629 enum tree_code code = TREE_CODE (cmpop);
5630 enum tree_code truthop_code = TREE_CODE (op);
5631 tree lhs = TREE_OPERAND (op, 0);
5632 tree rhs = TREE_OPERAND (op, 1);
5633 tree orig_lhs = lhs, orig_rhs = rhs;
5634 enum tree_code rhs_code = TREE_CODE (rhs);
5635 enum tree_code lhs_code = TREE_CODE (lhs);
5636 enum tree_code inv_code;
5638 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5639 return NULL_TREE;
5641 if (TREE_CODE_CLASS (code) != tcc_comparison)
5642 return NULL_TREE;
5644 if (rhs_code == truthop_code)
5646 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5647 if (newrhs != NULL_TREE)
5649 rhs = newrhs;
5650 rhs_code = TREE_CODE (rhs);
5653 if (lhs_code == truthop_code && !rhs_only)
5655 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5656 if (newlhs != NULL_TREE)
5658 lhs = newlhs;
5659 lhs_code = TREE_CODE (lhs);
5663 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5664 if (inv_code == rhs_code
5665 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5666 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5667 return lhs;
5668 if (!rhs_only && inv_code == lhs_code
5669 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5670 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5671 return rhs;
5672 if (rhs != orig_rhs || lhs != orig_lhs)
5673 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5674 lhs, rhs);
5675 return NULL_TREE;
5678 /* Find ways of folding logical expressions of LHS and RHS:
5679 Try to merge two comparisons to the same innermost item.
5680 Look for range tests like "ch >= '0' && ch <= '9'".
5681 Look for combinations of simple terms on machines with expensive branches
5682 and evaluate the RHS unconditionally.
5684 For example, if we have p->a == 2 && p->b == 4 and we can make an
5685 object large enough to span both A and B, we can do this with a comparison
5686 against the object ANDed with the a mask.
5688 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5689 operations to do this with one comparison.
5691 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5692 function and the one above.
5694 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5695 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5697 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5698 two operands.
5700 We return the simplified tree or 0 if no optimization is possible. */
5702 static tree
5703 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5704 tree lhs, tree rhs)
5706 /* If this is the "or" of two comparisons, we can do something if
5707 the comparisons are NE_EXPR. If this is the "and", we can do something
5708 if the comparisons are EQ_EXPR. I.e.,
5709 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5711 WANTED_CODE is this operation code. For single bit fields, we can
5712 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5713 comparison for one-bit fields. */
5715 enum tree_code wanted_code;
5716 enum tree_code lcode, rcode;
5717 tree ll_arg, lr_arg, rl_arg, rr_arg;
5718 tree ll_inner, lr_inner, rl_inner, rr_inner;
5719 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5720 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5721 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5722 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5723 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5724 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5725 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5726 scalar_int_mode lnmode, rnmode;
5727 tree ll_mask, lr_mask, rl_mask, rr_mask;
5728 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5729 tree l_const, r_const;
5730 tree lntype, rntype, result;
5731 HOST_WIDE_INT first_bit, end_bit;
5732 int volatilep;
5734 /* Start by getting the comparison codes. Fail if anything is volatile.
5735 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5736 it were surrounded with a NE_EXPR. */
5738 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5739 return 0;
5741 lcode = TREE_CODE (lhs);
5742 rcode = TREE_CODE (rhs);
5744 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5746 lhs = build2 (NE_EXPR, truth_type, lhs,
5747 build_int_cst (TREE_TYPE (lhs), 0));
5748 lcode = NE_EXPR;
5751 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5753 rhs = build2 (NE_EXPR, truth_type, rhs,
5754 build_int_cst (TREE_TYPE (rhs), 0));
5755 rcode = NE_EXPR;
5758 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5759 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5760 return 0;
5762 ll_arg = TREE_OPERAND (lhs, 0);
5763 lr_arg = TREE_OPERAND (lhs, 1);
5764 rl_arg = TREE_OPERAND (rhs, 0);
5765 rr_arg = TREE_OPERAND (rhs, 1);
5767 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5768 if (simple_operand_p (ll_arg)
5769 && simple_operand_p (lr_arg))
5771 if (operand_equal_p (ll_arg, rl_arg, 0)
5772 && operand_equal_p (lr_arg, rr_arg, 0))
5774 result = combine_comparisons (loc, code, lcode, rcode,
5775 truth_type, ll_arg, lr_arg);
5776 if (result)
5777 return result;
5779 else if (operand_equal_p (ll_arg, rr_arg, 0)
5780 && operand_equal_p (lr_arg, rl_arg, 0))
5782 result = combine_comparisons (loc, code, lcode,
5783 swap_tree_comparison (rcode),
5784 truth_type, ll_arg, lr_arg);
5785 if (result)
5786 return result;
5790 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5791 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5793 /* If the RHS can be evaluated unconditionally and its operands are
5794 simple, it wins to evaluate the RHS unconditionally on machines
5795 with expensive branches. In this case, this isn't a comparison
5796 that can be merged. */
5798 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5799 false) >= 2
5800 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5801 && simple_operand_p (rl_arg)
5802 && simple_operand_p (rr_arg))
5804 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5805 if (code == TRUTH_OR_EXPR
5806 && lcode == NE_EXPR && integer_zerop (lr_arg)
5807 && rcode == NE_EXPR && integer_zerop (rr_arg)
5808 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5809 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5810 return build2_loc (loc, NE_EXPR, truth_type,
5811 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5812 ll_arg, rl_arg),
5813 build_int_cst (TREE_TYPE (ll_arg), 0));
5815 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5816 if (code == TRUTH_AND_EXPR
5817 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5818 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5819 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5820 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5821 return build2_loc (loc, EQ_EXPR, truth_type,
5822 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5823 ll_arg, rl_arg),
5824 build_int_cst (TREE_TYPE (ll_arg), 0));
5827 /* See if the comparisons can be merged. Then get all the parameters for
5828 each side. */
5830 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5831 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5832 return 0;
5834 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5835 volatilep = 0;
5836 ll_inner = decode_field_reference (loc, &ll_arg,
5837 &ll_bitsize, &ll_bitpos, &ll_mode,
5838 &ll_unsignedp, &ll_reversep, &volatilep,
5839 &ll_mask, &ll_and_mask);
5840 lr_inner = decode_field_reference (loc, &lr_arg,
5841 &lr_bitsize, &lr_bitpos, &lr_mode,
5842 &lr_unsignedp, &lr_reversep, &volatilep,
5843 &lr_mask, &lr_and_mask);
5844 rl_inner = decode_field_reference (loc, &rl_arg,
5845 &rl_bitsize, &rl_bitpos, &rl_mode,
5846 &rl_unsignedp, &rl_reversep, &volatilep,
5847 &rl_mask, &rl_and_mask);
5848 rr_inner = decode_field_reference (loc, &rr_arg,
5849 &rr_bitsize, &rr_bitpos, &rr_mode,
5850 &rr_unsignedp, &rr_reversep, &volatilep,
5851 &rr_mask, &rr_and_mask);
5853 /* It must be true that the inner operation on the lhs of each
5854 comparison must be the same if we are to be able to do anything.
5855 Then see if we have constants. If not, the same must be true for
5856 the rhs's. */
5857 if (volatilep
5858 || ll_reversep != rl_reversep
5859 || ll_inner == 0 || rl_inner == 0
5860 || ! operand_equal_p (ll_inner, rl_inner, 0))
5861 return 0;
5863 if (TREE_CODE (lr_arg) == INTEGER_CST
5864 && TREE_CODE (rr_arg) == INTEGER_CST)
5866 l_const = lr_arg, r_const = rr_arg;
5867 lr_reversep = ll_reversep;
5869 else if (lr_reversep != rr_reversep
5870 || lr_inner == 0 || rr_inner == 0
5871 || ! operand_equal_p (lr_inner, rr_inner, 0))
5872 return 0;
5873 else
5874 l_const = r_const = 0;
5876 /* If either comparison code is not correct for our logical operation,
5877 fail. However, we can convert a one-bit comparison against zero into
5878 the opposite comparison against that bit being set in the field. */
5880 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5881 if (lcode != wanted_code)
5883 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5885 /* Make the left operand unsigned, since we are only interested
5886 in the value of one bit. Otherwise we are doing the wrong
5887 thing below. */
5888 ll_unsignedp = 1;
5889 l_const = ll_mask;
5891 else
5892 return 0;
5895 /* This is analogous to the code for l_const above. */
5896 if (rcode != wanted_code)
5898 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5900 rl_unsignedp = 1;
5901 r_const = rl_mask;
5903 else
5904 return 0;
5907 /* See if we can find a mode that contains both fields being compared on
5908 the left. If we can't, fail. Otherwise, update all constants and masks
5909 to be relative to a field of that size. */
5910 first_bit = MIN (ll_bitpos, rl_bitpos);
5911 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5912 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5913 TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
5914 volatilep, &lnmode))
5915 return 0;
5917 lnbitsize = GET_MODE_BITSIZE (lnmode);
5918 lnbitpos = first_bit & ~ (lnbitsize - 1);
5919 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5920 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5922 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5924 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5925 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5928 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5929 size_int (xll_bitpos));
5930 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5931 size_int (xrl_bitpos));
5933 if (l_const)
5935 l_const = fold_convert_loc (loc, lntype, l_const);
5936 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5937 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5938 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5939 fold_build1_loc (loc, BIT_NOT_EXPR,
5940 lntype, ll_mask))))
5942 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5944 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5947 if (r_const)
5949 r_const = fold_convert_loc (loc, lntype, r_const);
5950 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5951 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5952 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5953 fold_build1_loc (loc, BIT_NOT_EXPR,
5954 lntype, rl_mask))))
5956 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5958 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5962 /* If the right sides are not constant, do the same for it. Also,
5963 disallow this optimization if a size or signedness mismatch occurs
5964 between the left and right sides. */
5965 if (l_const == 0)
5967 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5968 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5969 /* Make sure the two fields on the right
5970 correspond to the left without being swapped. */
5971 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5972 return 0;
5974 first_bit = MIN (lr_bitpos, rr_bitpos);
5975 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5976 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5977 TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
5978 volatilep, &rnmode))
5979 return 0;
5981 rnbitsize = GET_MODE_BITSIZE (rnmode);
5982 rnbitpos = first_bit & ~ (rnbitsize - 1);
5983 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5984 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5986 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5988 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5989 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5992 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5993 rntype, lr_mask),
5994 size_int (xlr_bitpos));
5995 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5996 rntype, rr_mask),
5997 size_int (xrr_bitpos));
5999 /* Make a mask that corresponds to both fields being compared.
6000 Do this for both items being compared. If the operands are the
6001 same size and the bits being compared are in the same position
6002 then we can do this by masking both and comparing the masked
6003 results. */
6004 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6005 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
6006 if (lnbitsize == rnbitsize
6007 && xll_bitpos == xlr_bitpos
6008 && lnbitpos >= 0
6009 && rnbitpos >= 0)
6011 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
6012 lntype, lnbitsize, lnbitpos,
6013 ll_unsignedp || rl_unsignedp, ll_reversep);
6014 if (! all_ones_mask_p (ll_mask, lnbitsize))
6015 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6017 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
6018 rntype, rnbitsize, rnbitpos,
6019 lr_unsignedp || rr_unsignedp, lr_reversep);
6020 if (! all_ones_mask_p (lr_mask, rnbitsize))
6021 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6023 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6026 /* There is still another way we can do something: If both pairs of
6027 fields being compared are adjacent, we may be able to make a wider
6028 field containing them both.
6030 Note that we still must mask the lhs/rhs expressions. Furthermore,
6031 the mask must be shifted to account for the shift done by
6032 make_bit_field_ref. */
6033 if (((ll_bitsize + ll_bitpos == rl_bitpos
6034 && lr_bitsize + lr_bitpos == rr_bitpos)
6035 || (ll_bitpos == rl_bitpos + rl_bitsize
6036 && lr_bitpos == rr_bitpos + rr_bitsize))
6037 && ll_bitpos >= 0
6038 && rl_bitpos >= 0
6039 && lr_bitpos >= 0
6040 && rr_bitpos >= 0)
6042 tree type;
6044 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
6045 ll_bitsize + rl_bitsize,
6046 MIN (ll_bitpos, rl_bitpos),
6047 ll_unsignedp, ll_reversep);
6048 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
6049 lr_bitsize + rr_bitsize,
6050 MIN (lr_bitpos, rr_bitpos),
6051 lr_unsignedp, lr_reversep);
6053 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6054 size_int (MIN (xll_bitpos, xrl_bitpos)));
6055 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6056 size_int (MIN (xlr_bitpos, xrr_bitpos)));
6058 /* Convert to the smaller type before masking out unwanted bits. */
6059 type = lntype;
6060 if (lntype != rntype)
6062 if (lnbitsize > rnbitsize)
6064 lhs = fold_convert_loc (loc, rntype, lhs);
6065 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6066 type = rntype;
6068 else if (lnbitsize < rnbitsize)
6070 rhs = fold_convert_loc (loc, lntype, rhs);
6071 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6072 type = lntype;
6076 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6077 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6079 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6080 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6082 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6085 return 0;
6088 /* Handle the case of comparisons with constants. If there is something in
6089 common between the masks, those bits of the constants must be the same.
6090 If not, the condition is always false. Test for this to avoid generating
6091 incorrect code below. */
6092 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
6093 if (! integer_zerop (result)
6094 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
6095 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
6097 if (wanted_code == NE_EXPR)
6099 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6100 return constant_boolean_node (true, truth_type);
6102 else
6104 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6105 return constant_boolean_node (false, truth_type);
6109 if (lnbitpos < 0)
6110 return 0;
6112 /* Construct the expression we will return. First get the component
6113 reference we will make. Unless the mask is all ones the width of
6114 that field, perform the mask operation. Then compare with the
6115 merged constant. */
6116 result = make_bit_field_ref (loc, ll_inner, ll_arg,
6117 lntype, lnbitsize, lnbitpos,
6118 ll_unsignedp || rl_unsignedp, ll_reversep);
6120 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6121 if (! all_ones_mask_p (ll_mask, lnbitsize))
6122 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
6124 return build2_loc (loc, wanted_code, truth_type, result,
6125 const_binop (BIT_IOR_EXPR, l_const, r_const));
6128 /* T is an integer expression that is being multiplied, divided, or taken a
6129 modulus (CODE says which and what kind of divide or modulus) by a
6130 constant C. See if we can eliminate that operation by folding it with
6131 other operations already in T. WIDE_TYPE, if non-null, is a type that
6132 should be used for the computation if wider than our type.
6134 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6135 (X * 2) + (Y * 4). We must, however, be assured that either the original
6136 expression would not overflow or that overflow is undefined for the type
6137 in the language in question.
6139 If we return a non-null expression, it is an equivalent form of the
6140 original computation, but need not be in the original type.
6142 We set *STRICT_OVERFLOW_P to true if the return values depends on
6143 signed overflow being undefined. Otherwise we do not change
6144 *STRICT_OVERFLOW_P. */
6146 static tree
6147 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6148 bool *strict_overflow_p)
6150 /* To avoid exponential search depth, refuse to allow recursion past
6151 three levels. Beyond that (1) it's highly unlikely that we'll find
6152 something interesting and (2) we've probably processed it before
6153 when we built the inner expression. */
6155 static int depth;
6156 tree ret;
6158 if (depth > 3)
6159 return NULL;
6161 depth++;
6162 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6163 depth--;
6165 return ret;
6168 static tree
6169 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6170 bool *strict_overflow_p)
6172 tree type = TREE_TYPE (t);
6173 enum tree_code tcode = TREE_CODE (t);
6174 tree ctype = (wide_type != 0
6175 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6176 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6177 ? wide_type : type);
6178 tree t1, t2;
6179 int same_p = tcode == code;
6180 tree op0 = NULL_TREE, op1 = NULL_TREE;
6181 bool sub_strict_overflow_p;
6183 /* Don't deal with constants of zero here; they confuse the code below. */
6184 if (integer_zerop (c))
6185 return NULL_TREE;
6187 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6188 op0 = TREE_OPERAND (t, 0);
6190 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6191 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6193 /* Note that we need not handle conditional operations here since fold
6194 already handles those cases. So just do arithmetic here. */
6195 switch (tcode)
6197 case INTEGER_CST:
6198 /* For a constant, we can always simplify if we are a multiply
6199 or (for divide and modulus) if it is a multiple of our constant. */
6200 if (code == MULT_EXPR
6201 || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6202 TYPE_SIGN (type)))
6204 tree tem = const_binop (code, fold_convert (ctype, t),
6205 fold_convert (ctype, c));
6206 /* If the multiplication overflowed, we lost information on it.
6207 See PR68142 and PR69845. */
6208 if (TREE_OVERFLOW (tem))
6209 return NULL_TREE;
6210 return tem;
6212 break;
6214 CASE_CONVERT: case NON_LVALUE_EXPR:
6215 /* If op0 is an expression ... */
6216 if ((COMPARISON_CLASS_P (op0)
6217 || UNARY_CLASS_P (op0)
6218 || BINARY_CLASS_P (op0)
6219 || VL_EXP_CLASS_P (op0)
6220 || EXPRESSION_CLASS_P (op0))
6221 /* ... and has wrapping overflow, and its type is smaller
6222 than ctype, then we cannot pass through as widening. */
6223 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6224 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6225 && (TYPE_PRECISION (ctype)
6226 > TYPE_PRECISION (TREE_TYPE (op0))))
6227 /* ... or this is a truncation (t is narrower than op0),
6228 then we cannot pass through this narrowing. */
6229 || (TYPE_PRECISION (type)
6230 < TYPE_PRECISION (TREE_TYPE (op0)))
6231 /* ... or signedness changes for division or modulus,
6232 then we cannot pass through this conversion. */
6233 || (code != MULT_EXPR
6234 && (TYPE_UNSIGNED (ctype)
6235 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6236 /* ... or has undefined overflow while the converted to
6237 type has not, we cannot do the operation in the inner type
6238 as that would introduce undefined overflow. */
6239 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6240 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6241 && !TYPE_OVERFLOW_UNDEFINED (type))))
6242 break;
6244 /* Pass the constant down and see if we can make a simplification. If
6245 we can, replace this expression with the inner simplification for
6246 possible later conversion to our or some other type. */
6247 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6248 && TREE_CODE (t2) == INTEGER_CST
6249 && !TREE_OVERFLOW (t2)
6250 && (t1 = extract_muldiv (op0, t2, code,
6251 code == MULT_EXPR ? ctype : NULL_TREE,
6252 strict_overflow_p)) != 0)
6253 return t1;
6254 break;
6256 case ABS_EXPR:
6257 /* If widening the type changes it from signed to unsigned, then we
6258 must avoid building ABS_EXPR itself as unsigned. */
6259 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6261 tree cstype = (*signed_type_for) (ctype);
6262 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6263 != 0)
6265 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6266 return fold_convert (ctype, t1);
6268 break;
6270 /* If the constant is negative, we cannot simplify this. */
6271 if (tree_int_cst_sgn (c) == -1)
6272 break;
6273 /* FALLTHROUGH */
6274 case NEGATE_EXPR:
6275 /* For division and modulus, type can't be unsigned, as e.g.
6276 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6277 For signed types, even with wrapping overflow, this is fine. */
6278 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6279 break;
6280 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6281 != 0)
6282 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6283 break;
6285 case MIN_EXPR: case MAX_EXPR:
6286 /* If widening the type changes the signedness, then we can't perform
6287 this optimization as that changes the result. */
6288 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6289 break;
6291 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6292 sub_strict_overflow_p = false;
6293 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6294 &sub_strict_overflow_p)) != 0
6295 && (t2 = extract_muldiv (op1, c, code, wide_type,
6296 &sub_strict_overflow_p)) != 0)
6298 if (tree_int_cst_sgn (c) < 0)
6299 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6300 if (sub_strict_overflow_p)
6301 *strict_overflow_p = true;
6302 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6303 fold_convert (ctype, t2));
6305 break;
6307 case LSHIFT_EXPR: case RSHIFT_EXPR:
6308 /* If the second operand is constant, this is a multiplication
6309 or floor division, by a power of two, so we can treat it that
6310 way unless the multiplier or divisor overflows. Signed
6311 left-shift overflow is implementation-defined rather than
6312 undefined in C90, so do not convert signed left shift into
6313 multiplication. */
6314 if (TREE_CODE (op1) == INTEGER_CST
6315 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6316 /* const_binop may not detect overflow correctly,
6317 so check for it explicitly here. */
6318 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6319 wi::to_wide (op1))
6320 && (t1 = fold_convert (ctype,
6321 const_binop (LSHIFT_EXPR, size_one_node,
6322 op1))) != 0
6323 && !TREE_OVERFLOW (t1))
6324 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6325 ? MULT_EXPR : FLOOR_DIV_EXPR,
6326 ctype,
6327 fold_convert (ctype, op0),
6328 t1),
6329 c, code, wide_type, strict_overflow_p);
6330 break;
6332 case PLUS_EXPR: case MINUS_EXPR:
6333 /* See if we can eliminate the operation on both sides. If we can, we
6334 can return a new PLUS or MINUS. If we can't, the only remaining
6335 cases where we can do anything are if the second operand is a
6336 constant. */
6337 sub_strict_overflow_p = false;
6338 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6339 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6340 if (t1 != 0 && t2 != 0
6341 && TYPE_OVERFLOW_WRAPS (ctype)
6342 && (code == MULT_EXPR
6343 /* If not multiplication, we can only do this if both operands
6344 are divisible by c. */
6345 || (multiple_of_p (ctype, op0, c)
6346 && multiple_of_p (ctype, op1, c))))
6348 if (sub_strict_overflow_p)
6349 *strict_overflow_p = true;
6350 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6351 fold_convert (ctype, t2));
6354 /* If this was a subtraction, negate OP1 and set it to be an addition.
6355 This simplifies the logic below. */
6356 if (tcode == MINUS_EXPR)
6358 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6359 /* If OP1 was not easily negatable, the constant may be OP0. */
6360 if (TREE_CODE (op0) == INTEGER_CST)
6362 std::swap (op0, op1);
6363 std::swap (t1, t2);
6367 if (TREE_CODE (op1) != INTEGER_CST)
6368 break;
6370 /* If either OP1 or C are negative, this optimization is not safe for
6371 some of the division and remainder types while for others we need
6372 to change the code. */
6373 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6375 if (code == CEIL_DIV_EXPR)
6376 code = FLOOR_DIV_EXPR;
6377 else if (code == FLOOR_DIV_EXPR)
6378 code = CEIL_DIV_EXPR;
6379 else if (code != MULT_EXPR
6380 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6381 break;
6384 /* If it's a multiply or a division/modulus operation of a multiple
6385 of our constant, do the operation and verify it doesn't overflow. */
6386 if (code == MULT_EXPR
6387 || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6388 TYPE_SIGN (type)))
6390 op1 = const_binop (code, fold_convert (ctype, op1),
6391 fold_convert (ctype, c));
6392 /* We allow the constant to overflow with wrapping semantics. */
6393 if (op1 == 0
6394 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6395 break;
6397 else
6398 break;
6400 /* If we have an unsigned type, we cannot widen the operation since it
6401 will change the result if the original computation overflowed. */
6402 if (TYPE_UNSIGNED (ctype) && ctype != type)
6403 break;
6405 /* The last case is if we are a multiply. In that case, we can
6406 apply the distributive law to commute the multiply and addition
6407 if the multiplication of the constants doesn't overflow
6408 and overflow is defined. With undefined overflow
6409 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6410 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6411 return fold_build2 (tcode, ctype,
6412 fold_build2 (code, ctype,
6413 fold_convert (ctype, op0),
6414 fold_convert (ctype, c)),
6415 op1);
6417 break;
6419 case MULT_EXPR:
6420 /* We have a special case here if we are doing something like
6421 (C * 8) % 4 since we know that's zero. */
6422 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6423 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6424 /* If the multiplication can overflow we cannot optimize this. */
6425 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6426 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6427 && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6428 TYPE_SIGN (type)))
6430 *strict_overflow_p = true;
6431 return omit_one_operand (type, integer_zero_node, op0);
6434 /* ... fall through ... */
6436 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6437 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6438 /* If we can extract our operation from the LHS, do so and return a
6439 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6440 do something only if the second operand is a constant. */
6441 if (same_p
6442 && TYPE_OVERFLOW_WRAPS (ctype)
6443 && (t1 = extract_muldiv (op0, c, code, wide_type,
6444 strict_overflow_p)) != 0)
6445 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6446 fold_convert (ctype, op1));
6447 else if (tcode == MULT_EXPR && code == MULT_EXPR
6448 && TYPE_OVERFLOW_WRAPS (ctype)
6449 && (t1 = extract_muldiv (op1, c, code, wide_type,
6450 strict_overflow_p)) != 0)
6451 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6452 fold_convert (ctype, t1));
6453 else if (TREE_CODE (op1) != INTEGER_CST)
6454 return 0;
6456 /* If these are the same operation types, we can associate them
6457 assuming no overflow. */
6458 if (tcode == code)
6460 bool overflow_p = false;
6461 bool overflow_mul_p;
6462 signop sign = TYPE_SIGN (ctype);
6463 unsigned prec = TYPE_PRECISION (ctype);
6464 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6465 wi::to_wide (c, prec),
6466 sign, &overflow_mul_p);
6467 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6468 if (overflow_mul_p
6469 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6470 overflow_p = true;
6471 if (!overflow_p)
6472 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6473 wide_int_to_tree (ctype, mul));
6476 /* If these operations "cancel" each other, we have the main
6477 optimizations of this pass, which occur when either constant is a
6478 multiple of the other, in which case we replace this with either an
6479 operation or CODE or TCODE.
6481 If we have an unsigned type, we cannot do this since it will change
6482 the result if the original computation overflowed. */
6483 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6484 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6485 || (tcode == MULT_EXPR
6486 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6487 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6488 && code != MULT_EXPR)))
6490 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6491 TYPE_SIGN (type)))
6493 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6494 *strict_overflow_p = true;
6495 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6496 fold_convert (ctype,
6497 const_binop (TRUNC_DIV_EXPR,
6498 op1, c)));
6500 else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6501 TYPE_SIGN (type)))
6503 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6504 *strict_overflow_p = true;
6505 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6506 fold_convert (ctype,
6507 const_binop (TRUNC_DIV_EXPR,
6508 c, op1)));
6511 break;
6513 default:
6514 break;
6517 return 0;
6520 /* Return a node which has the indicated constant VALUE (either 0 or
6521 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6522 and is of the indicated TYPE. */
6524 tree
6525 constant_boolean_node (bool value, tree type)
6527 if (type == integer_type_node)
6528 return value ? integer_one_node : integer_zero_node;
6529 else if (type == boolean_type_node)
6530 return value ? boolean_true_node : boolean_false_node;
6531 else if (TREE_CODE (type) == VECTOR_TYPE)
6532 return build_vector_from_val (type,
6533 build_int_cst (TREE_TYPE (type),
6534 value ? -1 : 0));
6535 else
6536 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6540 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6541 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6542 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6543 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6544 COND is the first argument to CODE; otherwise (as in the example
6545 given here), it is the second argument. TYPE is the type of the
6546 original expression. Return NULL_TREE if no simplification is
6547 possible. */
6549 static tree
6550 fold_binary_op_with_conditional_arg (location_t loc,
6551 enum tree_code code,
6552 tree type, tree op0, tree op1,
6553 tree cond, tree arg, int cond_first_p)
6555 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6556 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6557 tree test, true_value, false_value;
6558 tree lhs = NULL_TREE;
6559 tree rhs = NULL_TREE;
6560 enum tree_code cond_code = COND_EXPR;
6562 if (TREE_CODE (cond) == COND_EXPR
6563 || TREE_CODE (cond) == VEC_COND_EXPR)
6565 test = TREE_OPERAND (cond, 0);
6566 true_value = TREE_OPERAND (cond, 1);
6567 false_value = TREE_OPERAND (cond, 2);
6568 /* If this operand throws an expression, then it does not make
6569 sense to try to perform a logical or arithmetic operation
6570 involving it. */
6571 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6572 lhs = true_value;
6573 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6574 rhs = false_value;
6576 else if (!(TREE_CODE (type) != VECTOR_TYPE
6577 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6579 tree testtype = TREE_TYPE (cond);
6580 test = cond;
6581 true_value = constant_boolean_node (true, testtype);
6582 false_value = constant_boolean_node (false, testtype);
6584 else
6585 /* Detect the case of mixing vector and scalar types - bail out. */
6586 return NULL_TREE;
6588 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6589 cond_code = VEC_COND_EXPR;
6591 /* This transformation is only worthwhile if we don't have to wrap ARG
6592 in a SAVE_EXPR and the operation can be simplified without recursing
6593 on at least one of the branches once its pushed inside the COND_EXPR. */
6594 if (!TREE_CONSTANT (arg)
6595 && (TREE_SIDE_EFFECTS (arg)
6596 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6597 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6598 return NULL_TREE;
6600 arg = fold_convert_loc (loc, arg_type, arg);
6601 if (lhs == 0)
6603 true_value = fold_convert_loc (loc, cond_type, true_value);
6604 if (cond_first_p)
6605 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6606 else
6607 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6609 if (rhs == 0)
6611 false_value = fold_convert_loc (loc, cond_type, false_value);
6612 if (cond_first_p)
6613 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6614 else
6615 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6618 /* Check that we have simplified at least one of the branches. */
6619 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6620 return NULL_TREE;
6622 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6626 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6628 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6629 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6630 ADDEND is the same as X.
6632 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6633 and finite. The problematic cases are when X is zero, and its mode
6634 has signed zeros. In the case of rounding towards -infinity,
6635 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6636 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6638 bool
6639 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6641 if (!real_zerop (addend))
6642 return false;
6644 /* Don't allow the fold with -fsignaling-nans. */
6645 if (HONOR_SNANS (element_mode (type)))
6646 return false;
6648 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6649 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6650 return true;
6652 /* In a vector or complex, we would need to check the sign of all zeros. */
6653 if (TREE_CODE (addend) != REAL_CST)
6654 return false;
6656 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6657 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6658 negate = !negate;
6660 /* The mode has signed zeros, and we have to honor their sign.
6661 In this situation, there is only one case we can return true for.
6662 X - 0 is the same as X unless rounding towards -infinity is
6663 supported. */
6664 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6667 /* Subroutine of match.pd that optimizes comparisons of a division by
6668 a nonzero integer constant against an integer constant, i.e.
6669 X/C1 op C2.
6671 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6672 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6674 enum tree_code
6675 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
6676 tree *hi, bool *neg_overflow)
6678 tree prod, tmp, type = TREE_TYPE (c1);
6679 signop sign = TYPE_SIGN (type);
6680 bool overflow;
6682 /* We have to do this the hard way to detect unsigned overflow.
6683 prod = int_const_binop (MULT_EXPR, c1, c2); */
6684 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
6685 prod = force_fit_type (type, val, -1, overflow);
6686 *neg_overflow = false;
6688 if (sign == UNSIGNED)
6690 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6691 *lo = prod;
6693 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6694 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
6695 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
6697 else if (tree_int_cst_sgn (c1) >= 0)
6699 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6700 switch (tree_int_cst_sgn (c2))
6702 case -1:
6703 *neg_overflow = true;
6704 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
6705 *hi = prod;
6706 break;
6708 case 0:
6709 *lo = fold_negate_const (tmp, type);
6710 *hi = tmp;
6711 break;
6713 case 1:
6714 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
6715 *lo = prod;
6716 break;
6718 default:
6719 gcc_unreachable ();
6722 else
6724 /* A negative divisor reverses the relational operators. */
6725 code = swap_tree_comparison (code);
6727 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
6728 switch (tree_int_cst_sgn (c2))
6730 case -1:
6731 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
6732 *lo = prod;
6733 break;
6735 case 0:
6736 *hi = fold_negate_const (tmp, type);
6737 *lo = tmp;
6738 break;
6740 case 1:
6741 *neg_overflow = true;
6742 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
6743 *hi = prod;
6744 break;
6746 default:
6747 gcc_unreachable ();
6751 if (code != EQ_EXPR && code != NE_EXPR)
6752 return code;
6754 if (TREE_OVERFLOW (*lo)
6755 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
6756 *lo = NULL_TREE;
6757 if (TREE_OVERFLOW (*hi)
6758 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
6759 *hi = NULL_TREE;
6761 return code;
6765 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6766 equality/inequality test, then return a simplified form of the test
6767 using a sign testing. Otherwise return NULL. TYPE is the desired
6768 result type. */
6770 static tree
6771 fold_single_bit_test_into_sign_test (location_t loc,
6772 enum tree_code code, tree arg0, tree arg1,
6773 tree result_type)
6775 /* If this is testing a single bit, we can optimize the test. */
6776 if ((code == NE_EXPR || code == EQ_EXPR)
6777 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6778 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6780 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6781 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6782 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6784 if (arg00 != NULL_TREE
6785 /* This is only a win if casting to a signed type is cheap,
6786 i.e. when arg00's type is not a partial mode. */
6787 && type_has_mode_precision_p (TREE_TYPE (arg00)))
6789 tree stype = signed_type_for (TREE_TYPE (arg00));
6790 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6791 result_type,
6792 fold_convert_loc (loc, stype, arg00),
6793 build_int_cst (stype, 0));
6797 return NULL_TREE;
6800 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6801 equality/inequality test, then return a simplified form of
6802 the test using shifts and logical operations. Otherwise return
6803 NULL. TYPE is the desired result type. */
6805 tree
6806 fold_single_bit_test (location_t loc, enum tree_code code,
6807 tree arg0, tree arg1, tree result_type)
6809 /* If this is testing a single bit, we can optimize the test. */
6810 if ((code == NE_EXPR || code == EQ_EXPR)
6811 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6812 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6814 tree inner = TREE_OPERAND (arg0, 0);
6815 tree type = TREE_TYPE (arg0);
6816 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6817 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
6818 int ops_unsigned;
6819 tree signed_type, unsigned_type, intermediate_type;
6820 tree tem, one;
6822 /* First, see if we can fold the single bit test into a sign-bit
6823 test. */
6824 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6825 result_type);
6826 if (tem)
6827 return tem;
6829 /* Otherwise we have (A & C) != 0 where C is a single bit,
6830 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6831 Similarly for (A & C) == 0. */
6833 /* If INNER is a right shift of a constant and it plus BITNUM does
6834 not overflow, adjust BITNUM and INNER. */
6835 if (TREE_CODE (inner) == RSHIFT_EXPR
6836 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6837 && bitnum < TYPE_PRECISION (type)
6838 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner, 1)),
6839 TYPE_PRECISION (type) - bitnum))
6841 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6842 inner = TREE_OPERAND (inner, 0);
6845 /* If we are going to be able to omit the AND below, we must do our
6846 operations as unsigned. If we must use the AND, we have a choice.
6847 Normally unsigned is faster, but for some machines signed is. */
6848 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6849 && !flag_syntax_only) ? 0 : 1;
6851 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6852 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6853 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6854 inner = fold_convert_loc (loc, intermediate_type, inner);
6856 if (bitnum != 0)
6857 inner = build2 (RSHIFT_EXPR, intermediate_type,
6858 inner, size_int (bitnum));
6860 one = build_int_cst (intermediate_type, 1);
6862 if (code == EQ_EXPR)
6863 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6865 /* Put the AND last so it can combine with more things. */
6866 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6868 /* Make sure to return the proper type. */
6869 inner = fold_convert_loc (loc, result_type, inner);
6871 return inner;
6873 return NULL_TREE;
6876 /* Test whether it is preferable two swap two operands, ARG0 and
6877 ARG1, for example because ARG0 is an integer constant and ARG1
6878 isn't. */
6880 bool
6881 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6883 if (CONSTANT_CLASS_P (arg1))
6884 return 0;
6885 if (CONSTANT_CLASS_P (arg0))
6886 return 1;
6888 STRIP_NOPS (arg0);
6889 STRIP_NOPS (arg1);
6891 if (TREE_CONSTANT (arg1))
6892 return 0;
6893 if (TREE_CONSTANT (arg0))
6894 return 1;
6896 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6897 for commutative and comparison operators. Ensuring a canonical
6898 form allows the optimizers to find additional redundancies without
6899 having to explicitly check for both orderings. */
6900 if (TREE_CODE (arg0) == SSA_NAME
6901 && TREE_CODE (arg1) == SSA_NAME
6902 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6903 return 1;
6905 /* Put SSA_NAMEs last. */
6906 if (TREE_CODE (arg1) == SSA_NAME)
6907 return 0;
6908 if (TREE_CODE (arg0) == SSA_NAME)
6909 return 1;
6911 /* Put variables last. */
6912 if (DECL_P (arg1))
6913 return 0;
6914 if (DECL_P (arg0))
6915 return 1;
6917 return 0;
6921 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6922 means A >= Y && A != MAX, but in this case we know that
6923 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6925 static tree
6926 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6928 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6930 if (TREE_CODE (bound) == LT_EXPR)
6931 a = TREE_OPERAND (bound, 0);
6932 else if (TREE_CODE (bound) == GT_EXPR)
6933 a = TREE_OPERAND (bound, 1);
6934 else
6935 return NULL_TREE;
6937 typea = TREE_TYPE (a);
6938 if (!INTEGRAL_TYPE_P (typea)
6939 && !POINTER_TYPE_P (typea))
6940 return NULL_TREE;
6942 if (TREE_CODE (ineq) == LT_EXPR)
6944 a1 = TREE_OPERAND (ineq, 1);
6945 y = TREE_OPERAND (ineq, 0);
6947 else if (TREE_CODE (ineq) == GT_EXPR)
6949 a1 = TREE_OPERAND (ineq, 0);
6950 y = TREE_OPERAND (ineq, 1);
6952 else
6953 return NULL_TREE;
6955 if (TREE_TYPE (a1) != typea)
6956 return NULL_TREE;
6958 if (POINTER_TYPE_P (typea))
6960 /* Convert the pointer types into integer before taking the difference. */
6961 tree ta = fold_convert_loc (loc, ssizetype, a);
6962 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6963 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6965 else
6966 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6968 if (!diff || !integer_onep (diff))
6969 return NULL_TREE;
6971 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6974 /* Fold a sum or difference of at least one multiplication.
6975 Returns the folded tree or NULL if no simplification could be made. */
6977 static tree
6978 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6979 tree arg0, tree arg1)
6981 tree arg00, arg01, arg10, arg11;
6982 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6984 /* (A * C) +- (B * C) -> (A+-B) * C.
6985 (A * C) +- A -> A * (C+-1).
6986 We are most concerned about the case where C is a constant,
6987 but other combinations show up during loop reduction. Since
6988 it is not difficult, try all four possibilities. */
6990 if (TREE_CODE (arg0) == MULT_EXPR)
6992 arg00 = TREE_OPERAND (arg0, 0);
6993 arg01 = TREE_OPERAND (arg0, 1);
6995 else if (TREE_CODE (arg0) == INTEGER_CST)
6997 arg00 = build_one_cst (type);
6998 arg01 = arg0;
7000 else
7002 /* We cannot generate constant 1 for fract. */
7003 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7004 return NULL_TREE;
7005 arg00 = arg0;
7006 arg01 = build_one_cst (type);
7008 if (TREE_CODE (arg1) == MULT_EXPR)
7010 arg10 = TREE_OPERAND (arg1, 0);
7011 arg11 = TREE_OPERAND (arg1, 1);
7013 else if (TREE_CODE (arg1) == INTEGER_CST)
7015 arg10 = build_one_cst (type);
7016 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7017 the purpose of this canonicalization. */
7018 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7019 && negate_expr_p (arg1)
7020 && code == PLUS_EXPR)
7022 arg11 = negate_expr (arg1);
7023 code = MINUS_EXPR;
7025 else
7026 arg11 = arg1;
7028 else
7030 /* We cannot generate constant 1 for fract. */
7031 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7032 return NULL_TREE;
7033 arg10 = arg1;
7034 arg11 = build_one_cst (type);
7036 same = NULL_TREE;
7038 /* Prefer factoring a common non-constant. */
7039 if (operand_equal_p (arg00, arg10, 0))
7040 same = arg00, alt0 = arg01, alt1 = arg11;
7041 else if (operand_equal_p (arg01, arg11, 0))
7042 same = arg01, alt0 = arg00, alt1 = arg10;
7043 else if (operand_equal_p (arg00, arg11, 0))
7044 same = arg00, alt0 = arg01, alt1 = arg10;
7045 else if (operand_equal_p (arg01, arg10, 0))
7046 same = arg01, alt0 = arg00, alt1 = arg11;
7048 /* No identical multiplicands; see if we can find a common
7049 power-of-two factor in non-power-of-two multiplies. This
7050 can help in multi-dimensional array access. */
7051 else if (tree_fits_shwi_p (arg01)
7052 && tree_fits_shwi_p (arg11))
7054 HOST_WIDE_INT int01, int11, tmp;
7055 bool swap = false;
7056 tree maybe_same;
7057 int01 = tree_to_shwi (arg01);
7058 int11 = tree_to_shwi (arg11);
7060 /* Move min of absolute values to int11. */
7061 if (absu_hwi (int01) < absu_hwi (int11))
7063 tmp = int01, int01 = int11, int11 = tmp;
7064 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7065 maybe_same = arg01;
7066 swap = true;
7068 else
7069 maybe_same = arg11;
7071 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7072 /* The remainder should not be a constant, otherwise we
7073 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7074 increased the number of multiplications necessary. */
7075 && TREE_CODE (arg10) != INTEGER_CST)
7077 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7078 build_int_cst (TREE_TYPE (arg00),
7079 int01 / int11));
7080 alt1 = arg10;
7081 same = maybe_same;
7082 if (swap)
7083 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7087 if (!same)
7088 return NULL_TREE;
7090 if (! INTEGRAL_TYPE_P (type)
7091 || TYPE_OVERFLOW_WRAPS (type)
7092 /* We are neither factoring zero nor minus one. */
7093 || TREE_CODE (same) == INTEGER_CST)
7094 return fold_build2_loc (loc, MULT_EXPR, type,
7095 fold_build2_loc (loc, code, type,
7096 fold_convert_loc (loc, type, alt0),
7097 fold_convert_loc (loc, type, alt1)),
7098 fold_convert_loc (loc, type, same));
7100 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7101 same may be minus one and thus the multiplication may overflow. Perform
7102 the sum operation in an unsigned type. */
7103 tree utype = unsigned_type_for (type);
7104 tree tem = fold_build2_loc (loc, code, utype,
7105 fold_convert_loc (loc, utype, alt0),
7106 fold_convert_loc (loc, utype, alt1));
7107 /* If the sum evaluated to a constant that is not -INF the multiplication
7108 cannot overflow. */
7109 if (TREE_CODE (tem) == INTEGER_CST
7110 && (wi::to_wide (tem)
7111 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7112 return fold_build2_loc (loc, MULT_EXPR, type,
7113 fold_convert (type, tem), same);
7115 /* Do not resort to unsigned multiplication because
7116 we lose the no-overflow property of the expression. */
7117 return NULL_TREE;
7120 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7121 specified by EXPR into the buffer PTR of length LEN bytes.
7122 Return the number of bytes placed in the buffer, or zero
7123 upon failure. */
7125 static int
7126 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7128 tree type = TREE_TYPE (expr);
7129 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7130 int byte, offset, word, words;
7131 unsigned char value;
7133 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7134 return 0;
7135 if (off == -1)
7136 off = 0;
7138 if (ptr == NULL)
7139 /* Dry run. */
7140 return MIN (len, total_bytes - off);
7142 words = total_bytes / UNITS_PER_WORD;
7144 for (byte = 0; byte < total_bytes; byte++)
7146 int bitpos = byte * BITS_PER_UNIT;
7147 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7148 number of bytes. */
7149 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7151 if (total_bytes > UNITS_PER_WORD)
7153 word = byte / UNITS_PER_WORD;
7154 if (WORDS_BIG_ENDIAN)
7155 word = (words - 1) - word;
7156 offset = word * UNITS_PER_WORD;
7157 if (BYTES_BIG_ENDIAN)
7158 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7159 else
7160 offset += byte % UNITS_PER_WORD;
7162 else
7163 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7164 if (offset >= off && offset - off < len)
7165 ptr[offset - off] = value;
7167 return MIN (len, total_bytes - off);
7171 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7172 specified by EXPR into the buffer PTR of length LEN bytes.
7173 Return the number of bytes placed in the buffer, or zero
7174 upon failure. */
7176 static int
7177 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7179 tree type = TREE_TYPE (expr);
7180 scalar_mode mode = SCALAR_TYPE_MODE (type);
7181 int total_bytes = GET_MODE_SIZE (mode);
7182 FIXED_VALUE_TYPE value;
7183 tree i_value, i_type;
7185 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7186 return 0;
7188 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7190 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7191 return 0;
7193 value = TREE_FIXED_CST (expr);
7194 i_value = double_int_to_tree (i_type, value.data);
7196 return native_encode_int (i_value, ptr, len, off);
7200 /* Subroutine of native_encode_expr. Encode the REAL_CST
7201 specified by EXPR into the buffer PTR of length LEN bytes.
7202 Return the number of bytes placed in the buffer, or zero
7203 upon failure. */
7205 static int
7206 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7208 tree type = TREE_TYPE (expr);
7209 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7210 int byte, offset, word, words, bitpos;
7211 unsigned char value;
7213 /* There are always 32 bits in each long, no matter the size of
7214 the hosts long. We handle floating point representations with
7215 up to 192 bits. */
7216 long tmp[6];
7218 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7219 return 0;
7220 if (off == -1)
7221 off = 0;
7223 if (ptr == NULL)
7224 /* Dry run. */
7225 return MIN (len, total_bytes - off);
7227 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7229 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7231 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7232 bitpos += BITS_PER_UNIT)
7234 byte = (bitpos / BITS_PER_UNIT) & 3;
7235 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7237 if (UNITS_PER_WORD < 4)
7239 word = byte / UNITS_PER_WORD;
7240 if (WORDS_BIG_ENDIAN)
7241 word = (words - 1) - word;
7242 offset = word * UNITS_PER_WORD;
7243 if (BYTES_BIG_ENDIAN)
7244 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7245 else
7246 offset += byte % UNITS_PER_WORD;
7248 else
7250 offset = byte;
7251 if (BYTES_BIG_ENDIAN)
7253 /* Reverse bytes within each long, or within the entire float
7254 if it's smaller than a long (for HFmode). */
7255 offset = MIN (3, total_bytes - 1) - offset;
7256 gcc_assert (offset >= 0);
7259 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7260 if (offset >= off
7261 && offset - off < len)
7262 ptr[offset - off] = value;
7264 return MIN (len, total_bytes - off);
7267 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7268 specified by EXPR into the buffer PTR of length LEN bytes.
7269 Return the number of bytes placed in the buffer, or zero
7270 upon failure. */
7272 static int
7273 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7275 int rsize, isize;
7276 tree part;
7278 part = TREE_REALPART (expr);
7279 rsize = native_encode_expr (part, ptr, len, off);
7280 if (off == -1 && rsize == 0)
7281 return 0;
7282 part = TREE_IMAGPART (expr);
7283 if (off != -1)
7284 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7285 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7286 len - rsize, off);
7287 if (off == -1 && isize != rsize)
7288 return 0;
7289 return rsize + isize;
7293 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7294 specified by EXPR into the buffer PTR of length LEN bytes.
7295 Return the number of bytes placed in the buffer, or zero
7296 upon failure. */
7298 static int
7299 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7301 unsigned HOST_WIDE_INT i, count;
7302 int size, offset;
7303 tree itype, elem;
7305 offset = 0;
7306 if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7307 return 0;
7308 itype = TREE_TYPE (TREE_TYPE (expr));
7309 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7310 for (i = 0; i < count; i++)
7312 if (off >= size)
7314 off -= size;
7315 continue;
7317 elem = VECTOR_CST_ELT (expr, i);
7318 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7319 len - offset, off);
7320 if ((off == -1 && res != size) || res == 0)
7321 return 0;
7322 offset += res;
7323 if (offset >= len)
7324 return (off == -1 && i < count - 1) ? 0 : offset;
7325 if (off != -1)
7326 off = 0;
7328 return offset;
7332 /* Subroutine of native_encode_expr. Encode the STRING_CST
7333 specified by EXPR into the buffer PTR of length LEN bytes.
7334 Return the number of bytes placed in the buffer, or zero
7335 upon failure. */
7337 static int
7338 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7340 tree type = TREE_TYPE (expr);
7342 /* Wide-char strings are encoded in target byte-order so native
7343 encoding them is trivial. */
7344 if (BITS_PER_UNIT != CHAR_BIT
7345 || TREE_CODE (type) != ARRAY_TYPE
7346 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7347 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7348 return 0;
7350 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7351 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7352 return 0;
7353 if (off == -1)
7354 off = 0;
7355 if (ptr == NULL)
7356 /* Dry run. */;
7357 else if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7359 int written = 0;
7360 if (off < TREE_STRING_LENGTH (expr))
7362 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7363 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7365 memset (ptr + written, 0,
7366 MIN (total_bytes - written, len - written));
7368 else
7369 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7370 return MIN (total_bytes - off, len);
7374 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7375 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7376 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7377 anything, just do a dry run. If OFF is not -1 then start
7378 the encoding at byte offset OFF and encode at most LEN bytes.
7379 Return the number of bytes placed in the buffer, or zero upon failure. */
7382 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7384 /* We don't support starting at negative offset and -1 is special. */
7385 if (off < -1)
7386 return 0;
7388 switch (TREE_CODE (expr))
7390 case INTEGER_CST:
7391 return native_encode_int (expr, ptr, len, off);
7393 case REAL_CST:
7394 return native_encode_real (expr, ptr, len, off);
7396 case FIXED_CST:
7397 return native_encode_fixed (expr, ptr, len, off);
7399 case COMPLEX_CST:
7400 return native_encode_complex (expr, ptr, len, off);
7402 case VECTOR_CST:
7403 return native_encode_vector (expr, ptr, len, off);
7405 case STRING_CST:
7406 return native_encode_string (expr, ptr, len, off);
7408 default:
7409 return 0;
7414 /* Subroutine of native_interpret_expr. Interpret the contents of
7415 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7416 If the buffer cannot be interpreted, return NULL_TREE. */
7418 static tree
7419 native_interpret_int (tree type, const unsigned char *ptr, int len)
7421 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7423 if (total_bytes > len
7424 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7425 return NULL_TREE;
7427 wide_int result = wi::from_buffer (ptr, total_bytes);
7429 return wide_int_to_tree (type, result);
7433 /* Subroutine of native_interpret_expr. Interpret the contents of
7434 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7435 If the buffer cannot be interpreted, return NULL_TREE. */
7437 static tree
7438 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7440 scalar_mode mode = SCALAR_TYPE_MODE (type);
7441 int total_bytes = GET_MODE_SIZE (mode);
7442 double_int result;
7443 FIXED_VALUE_TYPE fixed_value;
7445 if (total_bytes > len
7446 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7447 return NULL_TREE;
7449 result = double_int::from_buffer (ptr, total_bytes);
7450 fixed_value = fixed_from_double_int (result, mode);
7452 return build_fixed (type, fixed_value);
7456 /* Subroutine of native_interpret_expr. Interpret the contents of
7457 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7458 If the buffer cannot be interpreted, return NULL_TREE. */
7460 static tree
7461 native_interpret_real (tree type, const unsigned char *ptr, int len)
7463 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
7464 int total_bytes = GET_MODE_SIZE (mode);
7465 unsigned char value;
7466 /* There are always 32 bits in each long, no matter the size of
7467 the hosts long. We handle floating point representations with
7468 up to 192 bits. */
7469 REAL_VALUE_TYPE r;
7470 long tmp[6];
7472 if (total_bytes > len || total_bytes > 24)
7473 return NULL_TREE;
7474 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7476 memset (tmp, 0, sizeof (tmp));
7477 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7478 bitpos += BITS_PER_UNIT)
7480 /* Both OFFSET and BYTE index within a long;
7481 bitpos indexes the whole float. */
7482 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7483 if (UNITS_PER_WORD < 4)
7485 int word = byte / UNITS_PER_WORD;
7486 if (WORDS_BIG_ENDIAN)
7487 word = (words - 1) - word;
7488 offset = word * UNITS_PER_WORD;
7489 if (BYTES_BIG_ENDIAN)
7490 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7491 else
7492 offset += byte % UNITS_PER_WORD;
7494 else
7496 offset = byte;
7497 if (BYTES_BIG_ENDIAN)
7499 /* Reverse bytes within each long, or within the entire float
7500 if it's smaller than a long (for HFmode). */
7501 offset = MIN (3, total_bytes - 1) - offset;
7502 gcc_assert (offset >= 0);
7505 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7507 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7510 real_from_target (&r, tmp, mode);
7511 return build_real (type, r);
7515 /* Subroutine of native_interpret_expr. Interpret the contents of
7516 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7517 If the buffer cannot be interpreted, return NULL_TREE. */
7519 static tree
7520 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7522 tree etype, rpart, ipart;
7523 int size;
7525 etype = TREE_TYPE (type);
7526 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7527 if (size * 2 > len)
7528 return NULL_TREE;
7529 rpart = native_interpret_expr (etype, ptr, size);
7530 if (!rpart)
7531 return NULL_TREE;
7532 ipart = native_interpret_expr (etype, ptr+size, size);
7533 if (!ipart)
7534 return NULL_TREE;
7535 return build_complex (type, rpart, ipart);
7539 /* Subroutine of native_interpret_expr. Interpret the contents of
7540 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7541 If the buffer cannot be interpreted, return NULL_TREE. */
7543 static tree
7544 native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
7546 tree etype, elem;
7547 unsigned int i, size;
7548 unsigned HOST_WIDE_INT count;
7550 etype = TREE_TYPE (type);
7551 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7552 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&count)
7553 || size * count > len)
7554 return NULL_TREE;
7556 tree_vector_builder elements (type, count, 1);
7557 for (i = 0; i < count; ++i)
7559 elem = native_interpret_expr (etype, ptr+(i*size), size);
7560 if (!elem)
7561 return NULL_TREE;
7562 elements.quick_push (elem);
7564 return elements.build ();
7568 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7569 the buffer PTR of length LEN as a constant of type TYPE. For
7570 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7571 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7572 return NULL_TREE. */
7574 tree
7575 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7577 switch (TREE_CODE (type))
7579 case INTEGER_TYPE:
7580 case ENUMERAL_TYPE:
7581 case BOOLEAN_TYPE:
7582 case POINTER_TYPE:
7583 case REFERENCE_TYPE:
7584 return native_interpret_int (type, ptr, len);
7586 case REAL_TYPE:
7587 return native_interpret_real (type, ptr, len);
7589 case FIXED_POINT_TYPE:
7590 return native_interpret_fixed (type, ptr, len);
7592 case COMPLEX_TYPE:
7593 return native_interpret_complex (type, ptr, len);
7595 case VECTOR_TYPE:
7596 return native_interpret_vector (type, ptr, len);
7598 default:
7599 return NULL_TREE;
7603 /* Returns true if we can interpret the contents of a native encoding
7604 as TYPE. */
7606 static bool
7607 can_native_interpret_type_p (tree type)
7609 switch (TREE_CODE (type))
7611 case INTEGER_TYPE:
7612 case ENUMERAL_TYPE:
7613 case BOOLEAN_TYPE:
7614 case POINTER_TYPE:
7615 case REFERENCE_TYPE:
7616 case FIXED_POINT_TYPE:
7617 case REAL_TYPE:
7618 case COMPLEX_TYPE:
7619 case VECTOR_TYPE:
7620 return true;
7621 default:
7622 return false;
7627 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7628 TYPE at compile-time. If we're unable to perform the conversion
7629 return NULL_TREE. */
7631 static tree
7632 fold_view_convert_expr (tree type, tree expr)
7634 /* We support up to 512-bit values (for V8DFmode). */
7635 unsigned char buffer[64];
7636 int len;
7638 /* Check that the host and target are sane. */
7639 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7640 return NULL_TREE;
7642 len = native_encode_expr (expr, buffer, sizeof (buffer));
7643 if (len == 0)
7644 return NULL_TREE;
7646 return native_interpret_expr (type, buffer, len);
7649 /* Build an expression for the address of T. Folds away INDIRECT_REF
7650 to avoid confusing the gimplify process. */
7652 tree
7653 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7655 /* The size of the object is not relevant when talking about its address. */
7656 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7657 t = TREE_OPERAND (t, 0);
7659 if (TREE_CODE (t) == INDIRECT_REF)
7661 t = TREE_OPERAND (t, 0);
7663 if (TREE_TYPE (t) != ptrtype)
7664 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7666 else if (TREE_CODE (t) == MEM_REF
7667 && integer_zerop (TREE_OPERAND (t, 1)))
7668 return TREE_OPERAND (t, 0);
7669 else if (TREE_CODE (t) == MEM_REF
7670 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7671 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7672 TREE_OPERAND (t, 0),
7673 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7674 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7676 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7678 if (TREE_TYPE (t) != ptrtype)
7679 t = fold_convert_loc (loc, ptrtype, t);
7681 else
7682 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7684 return t;
7687 /* Build an expression for the address of T. */
7689 tree
7690 build_fold_addr_expr_loc (location_t loc, tree t)
7692 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7694 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7697 /* Fold a unary expression of code CODE and type TYPE with operand
7698 OP0. Return the folded expression if folding is successful.
7699 Otherwise, return NULL_TREE. */
7701 tree
7702 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7704 tree tem;
7705 tree arg0;
7706 enum tree_code_class kind = TREE_CODE_CLASS (code);
7708 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7709 && TREE_CODE_LENGTH (code) == 1);
7711 arg0 = op0;
7712 if (arg0)
7714 if (CONVERT_EXPR_CODE_P (code)
7715 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7717 /* Don't use STRIP_NOPS, because signedness of argument type
7718 matters. */
7719 STRIP_SIGN_NOPS (arg0);
7721 else
7723 /* Strip any conversions that don't change the mode. This
7724 is safe for every expression, except for a comparison
7725 expression because its signedness is derived from its
7726 operands.
7728 Note that this is done as an internal manipulation within
7729 the constant folder, in order to find the simplest
7730 representation of the arguments so that their form can be
7731 studied. In any cases, the appropriate type conversions
7732 should be put back in the tree that will get out of the
7733 constant folder. */
7734 STRIP_NOPS (arg0);
7737 if (CONSTANT_CLASS_P (arg0))
7739 tree tem = const_unop (code, type, arg0);
7740 if (tem)
7742 if (TREE_TYPE (tem) != type)
7743 tem = fold_convert_loc (loc, type, tem);
7744 return tem;
7749 tem = generic_simplify (loc, code, type, op0);
7750 if (tem)
7751 return tem;
7753 if (TREE_CODE_CLASS (code) == tcc_unary)
7755 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7756 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7757 fold_build1_loc (loc, code, type,
7758 fold_convert_loc (loc, TREE_TYPE (op0),
7759 TREE_OPERAND (arg0, 1))));
7760 else if (TREE_CODE (arg0) == COND_EXPR)
7762 tree arg01 = TREE_OPERAND (arg0, 1);
7763 tree arg02 = TREE_OPERAND (arg0, 2);
7764 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7765 arg01 = fold_build1_loc (loc, code, type,
7766 fold_convert_loc (loc,
7767 TREE_TYPE (op0), arg01));
7768 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7769 arg02 = fold_build1_loc (loc, code, type,
7770 fold_convert_loc (loc,
7771 TREE_TYPE (op0), arg02));
7772 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7773 arg01, arg02);
7775 /* If this was a conversion, and all we did was to move into
7776 inside the COND_EXPR, bring it back out. But leave it if
7777 it is a conversion from integer to integer and the
7778 result precision is no wider than a word since such a
7779 conversion is cheap and may be optimized away by combine,
7780 while it couldn't if it were outside the COND_EXPR. Then return
7781 so we don't get into an infinite recursion loop taking the
7782 conversion out and then back in. */
7784 if ((CONVERT_EXPR_CODE_P (code)
7785 || code == NON_LVALUE_EXPR)
7786 && TREE_CODE (tem) == COND_EXPR
7787 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7788 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7789 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7790 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7791 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7792 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7793 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7794 && (INTEGRAL_TYPE_P
7795 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7796 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7797 || flag_syntax_only))
7798 tem = build1_loc (loc, code, type,
7799 build3 (COND_EXPR,
7800 TREE_TYPE (TREE_OPERAND
7801 (TREE_OPERAND (tem, 1), 0)),
7802 TREE_OPERAND (tem, 0),
7803 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7804 TREE_OPERAND (TREE_OPERAND (tem, 2),
7805 0)));
7806 return tem;
7810 switch (code)
7812 case NON_LVALUE_EXPR:
7813 if (!maybe_lvalue_p (op0))
7814 return fold_convert_loc (loc, type, op0);
7815 return NULL_TREE;
7817 CASE_CONVERT:
7818 case FLOAT_EXPR:
7819 case FIX_TRUNC_EXPR:
7820 if (COMPARISON_CLASS_P (op0))
7822 /* If we have (type) (a CMP b) and type is an integral type, return
7823 new expression involving the new type. Canonicalize
7824 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7825 non-integral type.
7826 Do not fold the result as that would not simplify further, also
7827 folding again results in recursions. */
7828 if (TREE_CODE (type) == BOOLEAN_TYPE)
7829 return build2_loc (loc, TREE_CODE (op0), type,
7830 TREE_OPERAND (op0, 0),
7831 TREE_OPERAND (op0, 1));
7832 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7833 && TREE_CODE (type) != VECTOR_TYPE)
7834 return build3_loc (loc, COND_EXPR, type, op0,
7835 constant_boolean_node (true, type),
7836 constant_boolean_node (false, type));
7839 /* Handle (T *)&A.B.C for A being of type T and B and C
7840 living at offset zero. This occurs frequently in
7841 C++ upcasting and then accessing the base. */
7842 if (TREE_CODE (op0) == ADDR_EXPR
7843 && POINTER_TYPE_P (type)
7844 && handled_component_p (TREE_OPERAND (op0, 0)))
7846 poly_int64 bitsize, bitpos;
7847 tree offset;
7848 machine_mode mode;
7849 int unsignedp, reversep, volatilep;
7850 tree base
7851 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7852 &offset, &mode, &unsignedp, &reversep,
7853 &volatilep);
7854 /* If the reference was to a (constant) zero offset, we can use
7855 the address of the base if it has the same base type
7856 as the result type and the pointer type is unqualified. */
7857 if (!offset
7858 && known_eq (bitpos, 0)
7859 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7860 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7861 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7862 return fold_convert_loc (loc, type,
7863 build_fold_addr_expr_loc (loc, base));
7866 if (TREE_CODE (op0) == MODIFY_EXPR
7867 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7868 /* Detect assigning a bitfield. */
7869 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7870 && DECL_BIT_FIELD
7871 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7873 /* Don't leave an assignment inside a conversion
7874 unless assigning a bitfield. */
7875 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7876 /* First do the assignment, then return converted constant. */
7877 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7878 TREE_NO_WARNING (tem) = 1;
7879 TREE_USED (tem) = 1;
7880 return tem;
7883 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7884 constants (if x has signed type, the sign bit cannot be set
7885 in c). This folds extension into the BIT_AND_EXPR.
7886 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7887 very likely don't have maximal range for their precision and this
7888 transformation effectively doesn't preserve non-maximal ranges. */
7889 if (TREE_CODE (type) == INTEGER_TYPE
7890 && TREE_CODE (op0) == BIT_AND_EXPR
7891 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7893 tree and_expr = op0;
7894 tree and0 = TREE_OPERAND (and_expr, 0);
7895 tree and1 = TREE_OPERAND (and_expr, 1);
7896 int change = 0;
7898 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7899 || (TYPE_PRECISION (type)
7900 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7901 change = 1;
7902 else if (TYPE_PRECISION (TREE_TYPE (and1))
7903 <= HOST_BITS_PER_WIDE_INT
7904 && tree_fits_uhwi_p (and1))
7906 unsigned HOST_WIDE_INT cst;
7908 cst = tree_to_uhwi (and1);
7909 cst &= HOST_WIDE_INT_M1U
7910 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7911 change = (cst == 0);
7912 if (change
7913 && !flag_syntax_only
7914 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7915 == ZERO_EXTEND))
7917 tree uns = unsigned_type_for (TREE_TYPE (and0));
7918 and0 = fold_convert_loc (loc, uns, and0);
7919 and1 = fold_convert_loc (loc, uns, and1);
7922 if (change)
7924 tem = force_fit_type (type, wi::to_widest (and1), 0,
7925 TREE_OVERFLOW (and1));
7926 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7927 fold_convert_loc (loc, type, and0), tem);
7931 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7932 cast (T1)X will fold away. We assume that this happens when X itself
7933 is a cast. */
7934 if (POINTER_TYPE_P (type)
7935 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7936 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7938 tree arg00 = TREE_OPERAND (arg0, 0);
7939 tree arg01 = TREE_OPERAND (arg0, 1);
7941 return fold_build_pointer_plus_loc
7942 (loc, fold_convert_loc (loc, type, arg00), arg01);
7945 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7946 of the same precision, and X is an integer type not narrower than
7947 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7948 if (INTEGRAL_TYPE_P (type)
7949 && TREE_CODE (op0) == BIT_NOT_EXPR
7950 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7951 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7952 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7954 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7955 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7956 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7957 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7958 fold_convert_loc (loc, type, tem));
7961 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7962 type of X and Y (integer types only). */
7963 if (INTEGRAL_TYPE_P (type)
7964 && TREE_CODE (op0) == MULT_EXPR
7965 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7966 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7968 /* Be careful not to introduce new overflows. */
7969 tree mult_type;
7970 if (TYPE_OVERFLOW_WRAPS (type))
7971 mult_type = type;
7972 else
7973 mult_type = unsigned_type_for (type);
7975 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7977 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7978 fold_convert_loc (loc, mult_type,
7979 TREE_OPERAND (op0, 0)),
7980 fold_convert_loc (loc, mult_type,
7981 TREE_OPERAND (op0, 1)));
7982 return fold_convert_loc (loc, type, tem);
7986 return NULL_TREE;
7988 case VIEW_CONVERT_EXPR:
7989 if (TREE_CODE (op0) == MEM_REF)
7991 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7992 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7993 tem = fold_build2_loc (loc, MEM_REF, type,
7994 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7995 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7996 return tem;
7999 return NULL_TREE;
8001 case NEGATE_EXPR:
8002 tem = fold_negate_expr (loc, arg0);
8003 if (tem)
8004 return fold_convert_loc (loc, type, tem);
8005 return NULL_TREE;
8007 case ABS_EXPR:
8008 /* Convert fabs((double)float) into (double)fabsf(float). */
8009 if (TREE_CODE (arg0) == NOP_EXPR
8010 && TREE_CODE (type) == REAL_TYPE)
8012 tree targ0 = strip_float_extensions (arg0);
8013 if (targ0 != arg0)
8014 return fold_convert_loc (loc, type,
8015 fold_build1_loc (loc, ABS_EXPR,
8016 TREE_TYPE (targ0),
8017 targ0));
8019 return NULL_TREE;
8021 case BIT_NOT_EXPR:
8022 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8023 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8024 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8025 fold_convert_loc (loc, type,
8026 TREE_OPERAND (arg0, 0)))))
8027 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8028 fold_convert_loc (loc, type,
8029 TREE_OPERAND (arg0, 1)));
8030 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8031 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8032 fold_convert_loc (loc, type,
8033 TREE_OPERAND (arg0, 1)))))
8034 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8035 fold_convert_loc (loc, type,
8036 TREE_OPERAND (arg0, 0)), tem);
8038 return NULL_TREE;
8040 case TRUTH_NOT_EXPR:
8041 /* Note that the operand of this must be an int
8042 and its values must be 0 or 1.
8043 ("true" is a fixed value perhaps depending on the language,
8044 but we don't handle values other than 1 correctly yet.) */
8045 tem = fold_truth_not_expr (loc, arg0);
8046 if (!tem)
8047 return NULL_TREE;
8048 return fold_convert_loc (loc, type, tem);
8050 case INDIRECT_REF:
8051 /* Fold *&X to X if X is an lvalue. */
8052 if (TREE_CODE (op0) == ADDR_EXPR)
8054 tree op00 = TREE_OPERAND (op0, 0);
8055 if ((VAR_P (op00)
8056 || TREE_CODE (op00) == PARM_DECL
8057 || TREE_CODE (op00) == RESULT_DECL)
8058 && !TREE_READONLY (op00))
8059 return op00;
8061 return NULL_TREE;
8063 default:
8064 return NULL_TREE;
8065 } /* switch (code) */
8069 /* If the operation was a conversion do _not_ mark a resulting constant
8070 with TREE_OVERFLOW if the original constant was not. These conversions
8071 have implementation defined behavior and retaining the TREE_OVERFLOW
8072 flag here would confuse later passes such as VRP. */
8073 tree
8074 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8075 tree type, tree op0)
8077 tree res = fold_unary_loc (loc, code, type, op0);
8078 if (res
8079 && TREE_CODE (res) == INTEGER_CST
8080 && TREE_CODE (op0) == INTEGER_CST
8081 && CONVERT_EXPR_CODE_P (code))
8082 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8084 return res;
8087 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8088 operands OP0 and OP1. LOC is the location of the resulting expression.
8089 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8090 Return the folded expression if folding is successful. Otherwise,
8091 return NULL_TREE. */
8092 static tree
8093 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8094 tree arg0, tree arg1, tree op0, tree op1)
8096 tree tem;
8098 /* We only do these simplifications if we are optimizing. */
8099 if (!optimize)
8100 return NULL_TREE;
8102 /* Check for things like (A || B) && (A || C). We can convert this
8103 to A || (B && C). Note that either operator can be any of the four
8104 truth and/or operations and the transformation will still be
8105 valid. Also note that we only care about order for the
8106 ANDIF and ORIF operators. If B contains side effects, this
8107 might change the truth-value of A. */
8108 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8109 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8110 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8111 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8112 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8113 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8115 tree a00 = TREE_OPERAND (arg0, 0);
8116 tree a01 = TREE_OPERAND (arg0, 1);
8117 tree a10 = TREE_OPERAND (arg1, 0);
8118 tree a11 = TREE_OPERAND (arg1, 1);
8119 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8120 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8121 && (code == TRUTH_AND_EXPR
8122 || code == TRUTH_OR_EXPR));
8124 if (operand_equal_p (a00, a10, 0))
8125 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8126 fold_build2_loc (loc, code, type, a01, a11));
8127 else if (commutative && operand_equal_p (a00, a11, 0))
8128 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8129 fold_build2_loc (loc, code, type, a01, a10));
8130 else if (commutative && operand_equal_p (a01, a10, 0))
8131 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8132 fold_build2_loc (loc, code, type, a00, a11));
8134 /* This case if tricky because we must either have commutative
8135 operators or else A10 must not have side-effects. */
8137 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8138 && operand_equal_p (a01, a11, 0))
8139 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8140 fold_build2_loc (loc, code, type, a00, a10),
8141 a01);
8144 /* See if we can build a range comparison. */
8145 if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
8146 return tem;
8148 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8149 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8151 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8152 if (tem)
8153 return fold_build2_loc (loc, code, type, tem, arg1);
8156 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8157 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8159 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8160 if (tem)
8161 return fold_build2_loc (loc, code, type, arg0, tem);
8164 /* Check for the possibility of merging component references. If our
8165 lhs is another similar operation, try to merge its rhs with our
8166 rhs. Then try to merge our lhs and rhs. */
8167 if (TREE_CODE (arg0) == code
8168 && (tem = fold_truth_andor_1 (loc, code, type,
8169 TREE_OPERAND (arg0, 1), arg1)) != 0)
8170 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8172 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8173 return tem;
8175 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8176 && !flag_sanitize_coverage
8177 && (code == TRUTH_AND_EXPR
8178 || code == TRUTH_ANDIF_EXPR
8179 || code == TRUTH_OR_EXPR
8180 || code == TRUTH_ORIF_EXPR))
8182 enum tree_code ncode, icode;
8184 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8185 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8186 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8188 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8189 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8190 We don't want to pack more than two leafs to a non-IF AND/OR
8191 expression.
8192 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8193 equal to IF-CODE, then we don't want to add right-hand operand.
8194 If the inner right-hand side of left-hand operand has
8195 side-effects, or isn't simple, then we can't add to it,
8196 as otherwise we might destroy if-sequence. */
8197 if (TREE_CODE (arg0) == icode
8198 && simple_operand_p_2 (arg1)
8199 /* Needed for sequence points to handle trappings, and
8200 side-effects. */
8201 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8203 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8204 arg1);
8205 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8206 tem);
8208 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8209 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8210 else if (TREE_CODE (arg1) == icode
8211 && simple_operand_p_2 (arg0)
8212 /* Needed for sequence points to handle trappings, and
8213 side-effects. */
8214 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8216 tem = fold_build2_loc (loc, ncode, type,
8217 arg0, TREE_OPERAND (arg1, 0));
8218 return fold_build2_loc (loc, icode, type, tem,
8219 TREE_OPERAND (arg1, 1));
8221 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8222 into (A OR B).
8223 For sequence point consistancy, we need to check for trapping,
8224 and side-effects. */
8225 else if (code == icode && simple_operand_p_2 (arg0)
8226 && simple_operand_p_2 (arg1))
8227 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8230 return NULL_TREE;
8233 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8234 by changing CODE to reduce the magnitude of constants involved in
8235 ARG0 of the comparison.
8236 Returns a canonicalized comparison tree if a simplification was
8237 possible, otherwise returns NULL_TREE.
8238 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8239 valid if signed overflow is undefined. */
8241 static tree
8242 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8243 tree arg0, tree arg1,
8244 bool *strict_overflow_p)
8246 enum tree_code code0 = TREE_CODE (arg0);
8247 tree t, cst0 = NULL_TREE;
8248 int sgn0;
8250 /* Match A +- CST code arg1. We can change this only if overflow
8251 is undefined. */
8252 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8253 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8254 /* In principle pointers also have undefined overflow behavior,
8255 but that causes problems elsewhere. */
8256 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8257 && (code0 == MINUS_EXPR
8258 || code0 == PLUS_EXPR)
8259 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8260 return NULL_TREE;
8262 /* Identify the constant in arg0 and its sign. */
8263 cst0 = TREE_OPERAND (arg0, 1);
8264 sgn0 = tree_int_cst_sgn (cst0);
8266 /* Overflowed constants and zero will cause problems. */
8267 if (integer_zerop (cst0)
8268 || TREE_OVERFLOW (cst0))
8269 return NULL_TREE;
8271 /* See if we can reduce the magnitude of the constant in
8272 arg0 by changing the comparison code. */
8273 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8274 if (code == LT_EXPR
8275 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8276 code = LE_EXPR;
8277 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8278 else if (code == GT_EXPR
8279 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8280 code = GE_EXPR;
8281 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8282 else if (code == LE_EXPR
8283 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8284 code = LT_EXPR;
8285 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8286 else if (code == GE_EXPR
8287 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8288 code = GT_EXPR;
8289 else
8290 return NULL_TREE;
8291 *strict_overflow_p = true;
8293 /* Now build the constant reduced in magnitude. But not if that
8294 would produce one outside of its types range. */
8295 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8296 && ((sgn0 == 1
8297 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8298 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8299 || (sgn0 == -1
8300 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8301 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8302 return NULL_TREE;
8304 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8305 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8306 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8307 t = fold_convert (TREE_TYPE (arg1), t);
8309 return fold_build2_loc (loc, code, type, t, arg1);
8312 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8313 overflow further. Try to decrease the magnitude of constants involved
8314 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8315 and put sole constants at the second argument position.
8316 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8318 static tree
8319 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8320 tree arg0, tree arg1)
8322 tree t;
8323 bool strict_overflow_p;
8324 const char * const warnmsg = G_("assuming signed overflow does not occur "
8325 "when reducing constant in comparison");
8327 /* Try canonicalization by simplifying arg0. */
8328 strict_overflow_p = false;
8329 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8330 &strict_overflow_p);
8331 if (t)
8333 if (strict_overflow_p)
8334 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8335 return t;
8338 /* Try canonicalization by simplifying arg1 using the swapped
8339 comparison. */
8340 code = swap_tree_comparison (code);
8341 strict_overflow_p = false;
8342 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8343 &strict_overflow_p);
8344 if (t && strict_overflow_p)
8345 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8346 return t;
8349 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8350 space. This is used to avoid issuing overflow warnings for
8351 expressions like &p->x which can not wrap. */
8353 static bool
8354 pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
8356 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8357 return true;
8359 if (maybe_lt (bitpos, 0))
8360 return true;
8362 poly_wide_int wi_offset;
8363 int precision = TYPE_PRECISION (TREE_TYPE (base));
8364 if (offset == NULL_TREE)
8365 wi_offset = wi::zero (precision);
8366 else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
8367 return true;
8368 else
8369 wi_offset = wi::to_poly_wide (offset);
8371 bool overflow;
8372 poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
8373 precision);
8374 poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8375 if (overflow)
8376 return true;
8378 poly_uint64 total_hwi, size;
8379 if (!total.to_uhwi (&total_hwi)
8380 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
8381 &size)
8382 || known_eq (size, 0U))
8383 return true;
8385 if (known_le (total_hwi, size))
8386 return false;
8388 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8389 array. */
8390 if (TREE_CODE (base) == ADDR_EXPR
8391 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
8392 &size)
8393 && maybe_ne (size, 0U)
8394 && known_le (total_hwi, size))
8395 return false;
8397 return true;
8400 /* Return a positive integer when the symbol DECL is known to have
8401 a nonzero address, zero when it's known not to (e.g., it's a weak
8402 symbol), and a negative integer when the symbol is not yet in the
8403 symbol table and so whether or not its address is zero is unknown.
8404 For function local objects always return positive integer. */
8405 static int
8406 maybe_nonzero_address (tree decl)
8408 if (DECL_P (decl) && decl_in_symtab_p (decl))
8409 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8410 return symbol->nonzero_address ();
8412 /* Function local objects are never NULL. */
8413 if (DECL_P (decl)
8414 && (DECL_CONTEXT (decl)
8415 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8416 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8417 return 1;
8419 return -1;
8422 /* Subroutine of fold_binary. This routine performs all of the
8423 transformations that are common to the equality/inequality
8424 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8425 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8426 fold_binary should call fold_binary. Fold a comparison with
8427 tree code CODE and type TYPE with operands OP0 and OP1. Return
8428 the folded comparison or NULL_TREE. */
8430 static tree
8431 fold_comparison (location_t loc, enum tree_code code, tree type,
8432 tree op0, tree op1)
8434 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8435 tree arg0, arg1, tem;
8437 arg0 = op0;
8438 arg1 = op1;
8440 STRIP_SIGN_NOPS (arg0);
8441 STRIP_SIGN_NOPS (arg1);
8443 /* For comparisons of pointers we can decompose it to a compile time
8444 comparison of the base objects and the offsets into the object.
8445 This requires at least one operand being an ADDR_EXPR or a
8446 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8447 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8448 && (TREE_CODE (arg0) == ADDR_EXPR
8449 || TREE_CODE (arg1) == ADDR_EXPR
8450 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8451 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8453 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8454 poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
8455 machine_mode mode;
8456 int volatilep, reversep, unsignedp;
8457 bool indirect_base0 = false, indirect_base1 = false;
8459 /* Get base and offset for the access. Strip ADDR_EXPR for
8460 get_inner_reference, but put it back by stripping INDIRECT_REF
8461 off the base object if possible. indirect_baseN will be true
8462 if baseN is not an address but refers to the object itself. */
8463 base0 = arg0;
8464 if (TREE_CODE (arg0) == ADDR_EXPR)
8466 base0
8467 = get_inner_reference (TREE_OPERAND (arg0, 0),
8468 &bitsize, &bitpos0, &offset0, &mode,
8469 &unsignedp, &reversep, &volatilep);
8470 if (TREE_CODE (base0) == INDIRECT_REF)
8471 base0 = TREE_OPERAND (base0, 0);
8472 else
8473 indirect_base0 = true;
8475 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8477 base0 = TREE_OPERAND (arg0, 0);
8478 STRIP_SIGN_NOPS (base0);
8479 if (TREE_CODE (base0) == ADDR_EXPR)
8481 base0
8482 = get_inner_reference (TREE_OPERAND (base0, 0),
8483 &bitsize, &bitpos0, &offset0, &mode,
8484 &unsignedp, &reversep, &volatilep);
8485 if (TREE_CODE (base0) == INDIRECT_REF)
8486 base0 = TREE_OPERAND (base0, 0);
8487 else
8488 indirect_base0 = true;
8490 if (offset0 == NULL_TREE || integer_zerop (offset0))
8491 offset0 = TREE_OPERAND (arg0, 1);
8492 else
8493 offset0 = size_binop (PLUS_EXPR, offset0,
8494 TREE_OPERAND (arg0, 1));
8495 if (poly_int_tree_p (offset0))
8497 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
8498 TYPE_PRECISION (sizetype));
8499 tem <<= LOG2_BITS_PER_UNIT;
8500 tem += bitpos0;
8501 if (tem.to_shwi (&bitpos0))
8502 offset0 = NULL_TREE;
8506 base1 = arg1;
8507 if (TREE_CODE (arg1) == ADDR_EXPR)
8509 base1
8510 = get_inner_reference (TREE_OPERAND (arg1, 0),
8511 &bitsize, &bitpos1, &offset1, &mode,
8512 &unsignedp, &reversep, &volatilep);
8513 if (TREE_CODE (base1) == INDIRECT_REF)
8514 base1 = TREE_OPERAND (base1, 0);
8515 else
8516 indirect_base1 = true;
8518 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8520 base1 = TREE_OPERAND (arg1, 0);
8521 STRIP_SIGN_NOPS (base1);
8522 if (TREE_CODE (base1) == ADDR_EXPR)
8524 base1
8525 = get_inner_reference (TREE_OPERAND (base1, 0),
8526 &bitsize, &bitpos1, &offset1, &mode,
8527 &unsignedp, &reversep, &volatilep);
8528 if (TREE_CODE (base1) == INDIRECT_REF)
8529 base1 = TREE_OPERAND (base1, 0);
8530 else
8531 indirect_base1 = true;
8533 if (offset1 == NULL_TREE || integer_zerop (offset1))
8534 offset1 = TREE_OPERAND (arg1, 1);
8535 else
8536 offset1 = size_binop (PLUS_EXPR, offset1,
8537 TREE_OPERAND (arg1, 1));
8538 if (poly_int_tree_p (offset1))
8540 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
8541 TYPE_PRECISION (sizetype));
8542 tem <<= LOG2_BITS_PER_UNIT;
8543 tem += bitpos1;
8544 if (tem.to_shwi (&bitpos1))
8545 offset1 = NULL_TREE;
8549 /* If we have equivalent bases we might be able to simplify. */
8550 if (indirect_base0 == indirect_base1
8551 && operand_equal_p (base0, base1,
8552 indirect_base0 ? OEP_ADDRESS_OF : 0))
8554 /* We can fold this expression to a constant if the non-constant
8555 offset parts are equal. */
8556 if ((offset0 == offset1
8557 || (offset0 && offset1
8558 && operand_equal_p (offset0, offset1, 0)))
8559 && (equality_code
8560 || (indirect_base0
8561 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8562 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8564 if (!equality_code
8565 && maybe_ne (bitpos0, bitpos1)
8566 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8567 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8568 fold_overflow_warning (("assuming pointer wraparound does not "
8569 "occur when comparing P +- C1 with "
8570 "P +- C2"),
8571 WARN_STRICT_OVERFLOW_CONDITIONAL);
8573 switch (code)
8575 case EQ_EXPR:
8576 if (known_eq (bitpos0, bitpos1))
8577 return constant_boolean_node (true, type);
8578 if (known_ne (bitpos0, bitpos1))
8579 return constant_boolean_node (false, type);
8580 break;
8581 case NE_EXPR:
8582 if (known_ne (bitpos0, bitpos1))
8583 return constant_boolean_node (true, type);
8584 if (known_eq (bitpos0, bitpos1))
8585 return constant_boolean_node (false, type);
8586 break;
8587 case LT_EXPR:
8588 if (known_lt (bitpos0, bitpos1))
8589 return constant_boolean_node (true, type);
8590 if (known_ge (bitpos0, bitpos1))
8591 return constant_boolean_node (false, type);
8592 break;
8593 case LE_EXPR:
8594 if (known_le (bitpos0, bitpos1))
8595 return constant_boolean_node (true, type);
8596 if (known_gt (bitpos0, bitpos1))
8597 return constant_boolean_node (false, type);
8598 break;
8599 case GE_EXPR:
8600 if (known_ge (bitpos0, bitpos1))
8601 return constant_boolean_node (true, type);
8602 if (known_lt (bitpos0, bitpos1))
8603 return constant_boolean_node (false, type);
8604 break;
8605 case GT_EXPR:
8606 if (known_gt (bitpos0, bitpos1))
8607 return constant_boolean_node (true, type);
8608 if (known_le (bitpos0, bitpos1))
8609 return constant_boolean_node (false, type);
8610 break;
8611 default:;
8614 /* We can simplify the comparison to a comparison of the variable
8615 offset parts if the constant offset parts are equal.
8616 Be careful to use signed sizetype here because otherwise we
8617 mess with array offsets in the wrong way. This is possible
8618 because pointer arithmetic is restricted to retain within an
8619 object and overflow on pointer differences is undefined as of
8620 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8621 else if (known_eq (bitpos0, bitpos1)
8622 && (equality_code
8623 || (indirect_base0
8624 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8625 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8627 /* By converting to signed sizetype we cover middle-end pointer
8628 arithmetic which operates on unsigned pointer types of size
8629 type size and ARRAY_REF offsets which are properly sign or
8630 zero extended from their type in case it is narrower than
8631 sizetype. */
8632 if (offset0 == NULL_TREE)
8633 offset0 = build_int_cst (ssizetype, 0);
8634 else
8635 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8636 if (offset1 == NULL_TREE)
8637 offset1 = build_int_cst (ssizetype, 0);
8638 else
8639 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8641 if (!equality_code
8642 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8643 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8644 fold_overflow_warning (("assuming pointer wraparound does not "
8645 "occur when comparing P +- C1 with "
8646 "P +- C2"),
8647 WARN_STRICT_OVERFLOW_COMPARISON);
8649 return fold_build2_loc (loc, code, type, offset0, offset1);
8652 /* For equal offsets we can simplify to a comparison of the
8653 base addresses. */
8654 else if (known_eq (bitpos0, bitpos1)
8655 && (indirect_base0
8656 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8657 && (indirect_base1
8658 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8659 && ((offset0 == offset1)
8660 || (offset0 && offset1
8661 && operand_equal_p (offset0, offset1, 0))))
8663 if (indirect_base0)
8664 base0 = build_fold_addr_expr_loc (loc, base0);
8665 if (indirect_base1)
8666 base1 = build_fold_addr_expr_loc (loc, base1);
8667 return fold_build2_loc (loc, code, type, base0, base1);
8669 /* Comparison between an ordinary (non-weak) symbol and a null
8670 pointer can be eliminated since such symbols must have a non
8671 null address. In C, relational expressions between pointers
8672 to objects and null pointers are undefined. The results
8673 below follow the C++ rules with the additional property that
8674 every object pointer compares greater than a null pointer.
8676 else if (((DECL_P (base0)
8677 && maybe_nonzero_address (base0) > 0
8678 /* Avoid folding references to struct members at offset 0 to
8679 prevent tests like '&ptr->firstmember == 0' from getting
8680 eliminated. When ptr is null, although the -> expression
8681 is strictly speaking invalid, GCC retains it as a matter
8682 of QoI. See PR c/44555. */
8683 && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
8684 || CONSTANT_CLASS_P (base0))
8685 && indirect_base0
8686 /* The caller guarantees that when one of the arguments is
8687 constant (i.e., null in this case) it is second. */
8688 && integer_zerop (arg1))
8690 switch (code)
8692 case EQ_EXPR:
8693 case LE_EXPR:
8694 case LT_EXPR:
8695 return constant_boolean_node (false, type);
8696 case GE_EXPR:
8697 case GT_EXPR:
8698 case NE_EXPR:
8699 return constant_boolean_node (true, type);
8700 default:
8701 gcc_unreachable ();
8706 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8707 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8708 the resulting offset is smaller in absolute value than the
8709 original one and has the same sign. */
8710 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8711 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8712 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8713 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8714 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8715 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8716 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8717 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8719 tree const1 = TREE_OPERAND (arg0, 1);
8720 tree const2 = TREE_OPERAND (arg1, 1);
8721 tree variable1 = TREE_OPERAND (arg0, 0);
8722 tree variable2 = TREE_OPERAND (arg1, 0);
8723 tree cst;
8724 const char * const warnmsg = G_("assuming signed overflow does not "
8725 "occur when combining constants around "
8726 "a comparison");
8728 /* Put the constant on the side where it doesn't overflow and is
8729 of lower absolute value and of same sign than before. */
8730 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8731 ? MINUS_EXPR : PLUS_EXPR,
8732 const2, const1);
8733 if (!TREE_OVERFLOW (cst)
8734 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8735 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8737 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8738 return fold_build2_loc (loc, code, type,
8739 variable1,
8740 fold_build2_loc (loc, TREE_CODE (arg1),
8741 TREE_TYPE (arg1),
8742 variable2, cst));
8745 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8746 ? MINUS_EXPR : PLUS_EXPR,
8747 const1, const2);
8748 if (!TREE_OVERFLOW (cst)
8749 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8750 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8752 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8753 return fold_build2_loc (loc, code, type,
8754 fold_build2_loc (loc, TREE_CODE (arg0),
8755 TREE_TYPE (arg0),
8756 variable1, cst),
8757 variable2);
8761 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8762 if (tem)
8763 return tem;
8765 /* If we are comparing an expression that just has comparisons
8766 of two integer values, arithmetic expressions of those comparisons,
8767 and constants, we can simplify it. There are only three cases
8768 to check: the two values can either be equal, the first can be
8769 greater, or the second can be greater. Fold the expression for
8770 those three values. Since each value must be 0 or 1, we have
8771 eight possibilities, each of which corresponds to the constant 0
8772 or 1 or one of the six possible comparisons.
8774 This handles common cases like (a > b) == 0 but also handles
8775 expressions like ((x > y) - (y > x)) > 0, which supposedly
8776 occur in macroized code. */
8778 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8780 tree cval1 = 0, cval2 = 0;
8782 if (twoval_comparison_p (arg0, &cval1, &cval2)
8783 /* Don't handle degenerate cases here; they should already
8784 have been handled anyway. */
8785 && cval1 != 0 && cval2 != 0
8786 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8787 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8788 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8789 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8790 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8791 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8792 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8794 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8795 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8797 /* We can't just pass T to eval_subst in case cval1 or cval2
8798 was the same as ARG1. */
8800 tree high_result
8801 = fold_build2_loc (loc, code, type,
8802 eval_subst (loc, arg0, cval1, maxval,
8803 cval2, minval),
8804 arg1);
8805 tree equal_result
8806 = fold_build2_loc (loc, code, type,
8807 eval_subst (loc, arg0, cval1, maxval,
8808 cval2, maxval),
8809 arg1);
8810 tree low_result
8811 = fold_build2_loc (loc, code, type,
8812 eval_subst (loc, arg0, cval1, minval,
8813 cval2, maxval),
8814 arg1);
8816 /* All three of these results should be 0 or 1. Confirm they are.
8817 Then use those values to select the proper code to use. */
8819 if (TREE_CODE (high_result) == INTEGER_CST
8820 && TREE_CODE (equal_result) == INTEGER_CST
8821 && TREE_CODE (low_result) == INTEGER_CST)
8823 /* Make a 3-bit mask with the high-order bit being the
8824 value for `>', the next for '=', and the low for '<'. */
8825 switch ((integer_onep (high_result) * 4)
8826 + (integer_onep (equal_result) * 2)
8827 + integer_onep (low_result))
8829 case 0:
8830 /* Always false. */
8831 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8832 case 1:
8833 code = LT_EXPR;
8834 break;
8835 case 2:
8836 code = EQ_EXPR;
8837 break;
8838 case 3:
8839 code = LE_EXPR;
8840 break;
8841 case 4:
8842 code = GT_EXPR;
8843 break;
8844 case 5:
8845 code = NE_EXPR;
8846 break;
8847 case 6:
8848 code = GE_EXPR;
8849 break;
8850 case 7:
8851 /* Always true. */
8852 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8855 return fold_build2_loc (loc, code, type, cval1, cval2);
8860 return NULL_TREE;
8864 /* Subroutine of fold_binary. Optimize complex multiplications of the
8865 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8866 argument EXPR represents the expression "z" of type TYPE. */
8868 static tree
8869 fold_mult_zconjz (location_t loc, tree type, tree expr)
8871 tree itype = TREE_TYPE (type);
8872 tree rpart, ipart, tem;
8874 if (TREE_CODE (expr) == COMPLEX_EXPR)
8876 rpart = TREE_OPERAND (expr, 0);
8877 ipart = TREE_OPERAND (expr, 1);
8879 else if (TREE_CODE (expr) == COMPLEX_CST)
8881 rpart = TREE_REALPART (expr);
8882 ipart = TREE_IMAGPART (expr);
8884 else
8886 expr = save_expr (expr);
8887 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8888 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8891 rpart = save_expr (rpart);
8892 ipart = save_expr (ipart);
8893 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8894 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8895 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8896 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8897 build_zero_cst (itype));
8901 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8902 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8903 true if successful. */
8905 static bool
8906 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
8908 unsigned HOST_WIDE_INT i, nunits;
8910 if (TREE_CODE (arg) == VECTOR_CST
8911 && VECTOR_CST_NELTS (arg).is_constant (&nunits))
8913 for (i = 0; i < nunits; ++i)
8914 elts[i] = VECTOR_CST_ELT (arg, i);
8916 else if (TREE_CODE (arg) == CONSTRUCTOR)
8918 constructor_elt *elt;
8920 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8921 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8922 return false;
8923 else
8924 elts[i] = elt->value;
8926 else
8927 return false;
8928 for (; i < nelts; i++)
8929 elts[i]
8930 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8931 return true;
8934 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8935 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8936 NULL_TREE otherwise. */
8938 static tree
8939 fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
8941 unsigned int i;
8942 unsigned HOST_WIDE_INT nelts;
8943 bool need_ctor = false;
8945 if (!sel.length ().is_constant (&nelts))
8946 return NULL_TREE;
8947 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), nelts)
8948 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)), nelts)
8949 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)), nelts));
8950 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8951 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8952 return NULL_TREE;
8954 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
8955 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
8956 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
8957 return NULL_TREE;
8959 tree_vector_builder out_elts (type, nelts, 1);
8960 for (i = 0; i < nelts; i++)
8962 HOST_WIDE_INT index;
8963 if (!sel[i].is_constant (&index))
8964 return NULL_TREE;
8965 if (!CONSTANT_CLASS_P (in_elts[index]))
8966 need_ctor = true;
8967 out_elts.quick_push (unshare_expr (in_elts[index]));
8970 if (need_ctor)
8972 vec<constructor_elt, va_gc> *v;
8973 vec_alloc (v, nelts);
8974 for (i = 0; i < nelts; i++)
8975 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
8976 return build_constructor (type, v);
8978 else
8979 return out_elts.build ();
8982 /* Try to fold a pointer difference of type TYPE two address expressions of
8983 array references AREF0 and AREF1 using location LOC. Return a
8984 simplified expression for the difference or NULL_TREE. */
8986 static tree
8987 fold_addr_of_array_ref_difference (location_t loc, tree type,
8988 tree aref0, tree aref1,
8989 bool use_pointer_diff)
8991 tree base0 = TREE_OPERAND (aref0, 0);
8992 tree base1 = TREE_OPERAND (aref1, 0);
8993 tree base_offset = build_int_cst (type, 0);
8995 /* If the bases are array references as well, recurse. If the bases
8996 are pointer indirections compute the difference of the pointers.
8997 If the bases are equal, we are set. */
8998 if ((TREE_CODE (base0) == ARRAY_REF
8999 && TREE_CODE (base1) == ARRAY_REF
9000 && (base_offset
9001 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
9002 use_pointer_diff)))
9003 || (INDIRECT_REF_P (base0)
9004 && INDIRECT_REF_P (base1)
9005 && (base_offset
9006 = use_pointer_diff
9007 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
9008 TREE_OPERAND (base0, 0),
9009 TREE_OPERAND (base1, 0))
9010 : fold_binary_loc (loc, MINUS_EXPR, type,
9011 fold_convert (type,
9012 TREE_OPERAND (base0, 0)),
9013 fold_convert (type,
9014 TREE_OPERAND (base1, 0)))))
9015 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
9017 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9018 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9019 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9020 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
9021 return fold_build2_loc (loc, PLUS_EXPR, type,
9022 base_offset,
9023 fold_build2_loc (loc, MULT_EXPR, type,
9024 diff, esz));
9026 return NULL_TREE;
9029 /* If the real or vector real constant CST of type TYPE has an exact
9030 inverse, return it, else return NULL. */
9032 tree
9033 exact_inverse (tree type, tree cst)
9035 REAL_VALUE_TYPE r;
9036 tree unit_type;
9037 machine_mode mode;
9039 switch (TREE_CODE (cst))
9041 case REAL_CST:
9042 r = TREE_REAL_CST (cst);
9044 if (exact_real_inverse (TYPE_MODE (type), &r))
9045 return build_real (type, r);
9047 return NULL_TREE;
9049 case VECTOR_CST:
9051 unit_type = TREE_TYPE (type);
9052 mode = TYPE_MODE (unit_type);
9054 tree_vector_builder elts;
9055 if (!elts.new_unary_operation (type, cst, false))
9056 return NULL_TREE;
9057 unsigned int count = elts.encoded_nelts ();
9058 for (unsigned int i = 0; i < count; ++i)
9060 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9061 if (!exact_real_inverse (mode, &r))
9062 return NULL_TREE;
9063 elts.quick_push (build_real (unit_type, r));
9066 return elts.build ();
9069 default:
9070 return NULL_TREE;
9074 /* Mask out the tz least significant bits of X of type TYPE where
9075 tz is the number of trailing zeroes in Y. */
9076 static wide_int
9077 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9079 int tz = wi::ctz (y);
9080 if (tz > 0)
9081 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9082 return x;
9085 /* Return true when T is an address and is known to be nonzero.
9086 For floating point we further ensure that T is not denormal.
9087 Similar logic is present in nonzero_address in rtlanal.h.
9089 If the return value is based on the assumption that signed overflow
9090 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9091 change *STRICT_OVERFLOW_P. */
9093 static bool
9094 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9096 tree type = TREE_TYPE (t);
9097 enum tree_code code;
9099 /* Doing something useful for floating point would need more work. */
9100 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9101 return false;
9103 code = TREE_CODE (t);
9104 switch (TREE_CODE_CLASS (code))
9106 case tcc_unary:
9107 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9108 strict_overflow_p);
9109 case tcc_binary:
9110 case tcc_comparison:
9111 return tree_binary_nonzero_warnv_p (code, type,
9112 TREE_OPERAND (t, 0),
9113 TREE_OPERAND (t, 1),
9114 strict_overflow_p);
9115 case tcc_constant:
9116 case tcc_declaration:
9117 case tcc_reference:
9118 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9120 default:
9121 break;
9124 switch (code)
9126 case TRUTH_NOT_EXPR:
9127 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9128 strict_overflow_p);
9130 case TRUTH_AND_EXPR:
9131 case TRUTH_OR_EXPR:
9132 case TRUTH_XOR_EXPR:
9133 return tree_binary_nonzero_warnv_p (code, type,
9134 TREE_OPERAND (t, 0),
9135 TREE_OPERAND (t, 1),
9136 strict_overflow_p);
9138 case COND_EXPR:
9139 case CONSTRUCTOR:
9140 case OBJ_TYPE_REF:
9141 case ASSERT_EXPR:
9142 case ADDR_EXPR:
9143 case WITH_SIZE_EXPR:
9144 case SSA_NAME:
9145 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9147 case COMPOUND_EXPR:
9148 case MODIFY_EXPR:
9149 case BIND_EXPR:
9150 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9151 strict_overflow_p);
9153 case SAVE_EXPR:
9154 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9155 strict_overflow_p);
9157 case CALL_EXPR:
9159 tree fndecl = get_callee_fndecl (t);
9160 if (!fndecl) return false;
9161 if (flag_delete_null_pointer_checks && !flag_check_new
9162 && DECL_IS_OPERATOR_NEW (fndecl)
9163 && !TREE_NOTHROW (fndecl))
9164 return true;
9165 if (flag_delete_null_pointer_checks
9166 && lookup_attribute ("returns_nonnull",
9167 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9168 return true;
9169 return alloca_call_p (t);
9172 default:
9173 break;
9175 return false;
9178 /* Return true when T is an address and is known to be nonzero.
9179 Handle warnings about undefined signed overflow. */
9181 bool
9182 tree_expr_nonzero_p (tree t)
9184 bool ret, strict_overflow_p;
9186 strict_overflow_p = false;
9187 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9188 if (strict_overflow_p)
9189 fold_overflow_warning (("assuming signed overflow does not occur when "
9190 "determining that expression is always "
9191 "non-zero"),
9192 WARN_STRICT_OVERFLOW_MISC);
9193 return ret;
9196 /* Return true if T is known not to be equal to an integer W. */
9198 bool
9199 expr_not_equal_to (tree t, const wide_int &w)
9201 wide_int min, max, nz;
9202 value_range_type rtype;
9203 switch (TREE_CODE (t))
9205 case INTEGER_CST:
9206 return wi::to_wide (t) != w;
9208 case SSA_NAME:
9209 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9210 return false;
9211 rtype = get_range_info (t, &min, &max);
9212 if (rtype == VR_RANGE)
9214 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9215 return true;
9216 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9217 return true;
9219 else if (rtype == VR_ANTI_RANGE
9220 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9221 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9222 return true;
9223 /* If T has some known zero bits and W has any of those bits set,
9224 then T is known not to be equal to W. */
9225 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9226 TYPE_PRECISION (TREE_TYPE (t))), 0))
9227 return true;
9228 return false;
9230 default:
9231 return false;
9235 /* Fold a binary expression of code CODE and type TYPE with operands
9236 OP0 and OP1. LOC is the location of the resulting expression.
9237 Return the folded expression if folding is successful. Otherwise,
9238 return NULL_TREE. */
9240 tree
9241 fold_binary_loc (location_t loc, enum tree_code code, tree type,
9242 tree op0, tree op1)
9244 enum tree_code_class kind = TREE_CODE_CLASS (code);
9245 tree arg0, arg1, tem;
9246 tree t1 = NULL_TREE;
9247 bool strict_overflow_p;
9248 unsigned int prec;
9250 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9251 && TREE_CODE_LENGTH (code) == 2
9252 && op0 != NULL_TREE
9253 && op1 != NULL_TREE);
9255 arg0 = op0;
9256 arg1 = op1;
9258 /* Strip any conversions that don't change the mode. This is
9259 safe for every expression, except for a comparison expression
9260 because its signedness is derived from its operands. So, in
9261 the latter case, only strip conversions that don't change the
9262 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9263 preserved.
9265 Note that this is done as an internal manipulation within the
9266 constant folder, in order to find the simplest representation
9267 of the arguments so that their form can be studied. In any
9268 cases, the appropriate type conversions should be put back in
9269 the tree that will get out of the constant folder. */
9271 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9273 STRIP_SIGN_NOPS (arg0);
9274 STRIP_SIGN_NOPS (arg1);
9276 else
9278 STRIP_NOPS (arg0);
9279 STRIP_NOPS (arg1);
9282 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9283 constant but we can't do arithmetic on them. */
9284 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9286 tem = const_binop (code, type, arg0, arg1);
9287 if (tem != NULL_TREE)
9289 if (TREE_TYPE (tem) != type)
9290 tem = fold_convert_loc (loc, type, tem);
9291 return tem;
9295 /* If this is a commutative operation, and ARG0 is a constant, move it
9296 to ARG1 to reduce the number of tests below. */
9297 if (commutative_tree_code (code)
9298 && tree_swap_operands_p (arg0, arg1))
9299 return fold_build2_loc (loc, code, type, op1, op0);
9301 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9302 to ARG1 to reduce the number of tests below. */
9303 if (kind == tcc_comparison
9304 && tree_swap_operands_p (arg0, arg1))
9305 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9307 tem = generic_simplify (loc, code, type, op0, op1);
9308 if (tem)
9309 return tem;
9311 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9313 First check for cases where an arithmetic operation is applied to a
9314 compound, conditional, or comparison operation. Push the arithmetic
9315 operation inside the compound or conditional to see if any folding
9316 can then be done. Convert comparison to conditional for this purpose.
9317 The also optimizes non-constant cases that used to be done in
9318 expand_expr.
9320 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9321 one of the operands is a comparison and the other is a comparison, a
9322 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9323 code below would make the expression more complex. Change it to a
9324 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9325 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9327 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9328 || code == EQ_EXPR || code == NE_EXPR)
9329 && !VECTOR_TYPE_P (TREE_TYPE (arg0))
9330 && ((truth_value_p (TREE_CODE (arg0))
9331 && (truth_value_p (TREE_CODE (arg1))
9332 || (TREE_CODE (arg1) == BIT_AND_EXPR
9333 && integer_onep (TREE_OPERAND (arg1, 1)))))
9334 || (truth_value_p (TREE_CODE (arg1))
9335 && (truth_value_p (TREE_CODE (arg0))
9336 || (TREE_CODE (arg0) == BIT_AND_EXPR
9337 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9339 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9340 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9341 : TRUTH_XOR_EXPR,
9342 boolean_type_node,
9343 fold_convert_loc (loc, boolean_type_node, arg0),
9344 fold_convert_loc (loc, boolean_type_node, arg1));
9346 if (code == EQ_EXPR)
9347 tem = invert_truthvalue_loc (loc, tem);
9349 return fold_convert_loc (loc, type, tem);
9352 if (TREE_CODE_CLASS (code) == tcc_binary
9353 || TREE_CODE_CLASS (code) == tcc_comparison)
9355 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9357 tem = fold_build2_loc (loc, code, type,
9358 fold_convert_loc (loc, TREE_TYPE (op0),
9359 TREE_OPERAND (arg0, 1)), op1);
9360 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9361 tem);
9363 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9365 tem = fold_build2_loc (loc, code, type, op0,
9366 fold_convert_loc (loc, TREE_TYPE (op1),
9367 TREE_OPERAND (arg1, 1)));
9368 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9369 tem);
9372 if (TREE_CODE (arg0) == COND_EXPR
9373 || TREE_CODE (arg0) == VEC_COND_EXPR
9374 || COMPARISON_CLASS_P (arg0))
9376 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9377 arg0, arg1,
9378 /*cond_first_p=*/1);
9379 if (tem != NULL_TREE)
9380 return tem;
9383 if (TREE_CODE (arg1) == COND_EXPR
9384 || TREE_CODE (arg1) == VEC_COND_EXPR
9385 || COMPARISON_CLASS_P (arg1))
9387 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9388 arg1, arg0,
9389 /*cond_first_p=*/0);
9390 if (tem != NULL_TREE)
9391 return tem;
9395 switch (code)
9397 case MEM_REF:
9398 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9399 if (TREE_CODE (arg0) == ADDR_EXPR
9400 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9402 tree iref = TREE_OPERAND (arg0, 0);
9403 return fold_build2 (MEM_REF, type,
9404 TREE_OPERAND (iref, 0),
9405 int_const_binop (PLUS_EXPR, arg1,
9406 TREE_OPERAND (iref, 1)));
9409 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9410 if (TREE_CODE (arg0) == ADDR_EXPR
9411 && handled_component_p (TREE_OPERAND (arg0, 0)))
9413 tree base;
9414 poly_int64 coffset;
9415 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9416 &coffset);
9417 if (!base)
9418 return NULL_TREE;
9419 return fold_build2 (MEM_REF, type,
9420 build_fold_addr_expr (base),
9421 int_const_binop (PLUS_EXPR, arg1,
9422 size_int (coffset)));
9425 return NULL_TREE;
9427 case POINTER_PLUS_EXPR:
9428 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9429 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9430 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9431 return fold_convert_loc (loc, type,
9432 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9433 fold_convert_loc (loc, sizetype,
9434 arg1),
9435 fold_convert_loc (loc, sizetype,
9436 arg0)));
9438 return NULL_TREE;
9440 case PLUS_EXPR:
9441 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9443 /* X + (X / CST) * -CST is X % CST. */
9444 if (TREE_CODE (arg1) == MULT_EXPR
9445 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9446 && operand_equal_p (arg0,
9447 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9449 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9450 tree cst1 = TREE_OPERAND (arg1, 1);
9451 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9452 cst1, cst0);
9453 if (sum && integer_zerop (sum))
9454 return fold_convert_loc (loc, type,
9455 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9456 TREE_TYPE (arg0), arg0,
9457 cst0));
9461 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9462 one. Make sure the type is not saturating and has the signedness of
9463 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9464 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9465 if ((TREE_CODE (arg0) == MULT_EXPR
9466 || TREE_CODE (arg1) == MULT_EXPR)
9467 && !TYPE_SATURATING (type)
9468 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9469 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9470 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9472 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9473 if (tem)
9474 return tem;
9477 if (! FLOAT_TYPE_P (type))
9479 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9480 (plus (plus (mult) (mult)) (foo)) so that we can
9481 take advantage of the factoring cases below. */
9482 if (ANY_INTEGRAL_TYPE_P (type)
9483 && TYPE_OVERFLOW_WRAPS (type)
9484 && (((TREE_CODE (arg0) == PLUS_EXPR
9485 || TREE_CODE (arg0) == MINUS_EXPR)
9486 && TREE_CODE (arg1) == MULT_EXPR)
9487 || ((TREE_CODE (arg1) == PLUS_EXPR
9488 || TREE_CODE (arg1) == MINUS_EXPR)
9489 && TREE_CODE (arg0) == MULT_EXPR)))
9491 tree parg0, parg1, parg, marg;
9492 enum tree_code pcode;
9494 if (TREE_CODE (arg1) == MULT_EXPR)
9495 parg = arg0, marg = arg1;
9496 else
9497 parg = arg1, marg = arg0;
9498 pcode = TREE_CODE (parg);
9499 parg0 = TREE_OPERAND (parg, 0);
9500 parg1 = TREE_OPERAND (parg, 1);
9501 STRIP_NOPS (parg0);
9502 STRIP_NOPS (parg1);
9504 if (TREE_CODE (parg0) == MULT_EXPR
9505 && TREE_CODE (parg1) != MULT_EXPR)
9506 return fold_build2_loc (loc, pcode, type,
9507 fold_build2_loc (loc, PLUS_EXPR, type,
9508 fold_convert_loc (loc, type,
9509 parg0),
9510 fold_convert_loc (loc, type,
9511 marg)),
9512 fold_convert_loc (loc, type, parg1));
9513 if (TREE_CODE (parg0) != MULT_EXPR
9514 && TREE_CODE (parg1) == MULT_EXPR)
9515 return
9516 fold_build2_loc (loc, PLUS_EXPR, type,
9517 fold_convert_loc (loc, type, parg0),
9518 fold_build2_loc (loc, pcode, type,
9519 fold_convert_loc (loc, type, marg),
9520 fold_convert_loc (loc, type,
9521 parg1)));
9524 else
9526 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9527 to __complex__ ( x, y ). This is not the same for SNaNs or
9528 if signed zeros are involved. */
9529 if (!HONOR_SNANS (element_mode (arg0))
9530 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9531 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9533 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9534 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9535 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9536 bool arg0rz = false, arg0iz = false;
9537 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9538 || (arg0i && (arg0iz = real_zerop (arg0i))))
9540 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9541 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9542 if (arg0rz && arg1i && real_zerop (arg1i))
9544 tree rp = arg1r ? arg1r
9545 : build1 (REALPART_EXPR, rtype, arg1);
9546 tree ip = arg0i ? arg0i
9547 : build1 (IMAGPART_EXPR, rtype, arg0);
9548 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9550 else if (arg0iz && arg1r && real_zerop (arg1r))
9552 tree rp = arg0r ? arg0r
9553 : build1 (REALPART_EXPR, rtype, arg0);
9554 tree ip = arg1i ? arg1i
9555 : build1 (IMAGPART_EXPR, rtype, arg1);
9556 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9561 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9562 We associate floats only if the user has specified
9563 -fassociative-math. */
9564 if (flag_associative_math
9565 && TREE_CODE (arg1) == PLUS_EXPR
9566 && TREE_CODE (arg0) != MULT_EXPR)
9568 tree tree10 = TREE_OPERAND (arg1, 0);
9569 tree tree11 = TREE_OPERAND (arg1, 1);
9570 if (TREE_CODE (tree11) == MULT_EXPR
9571 && TREE_CODE (tree10) == MULT_EXPR)
9573 tree tree0;
9574 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9575 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9578 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9579 We associate floats only if the user has specified
9580 -fassociative-math. */
9581 if (flag_associative_math
9582 && TREE_CODE (arg0) == PLUS_EXPR
9583 && TREE_CODE (arg1) != MULT_EXPR)
9585 tree tree00 = TREE_OPERAND (arg0, 0);
9586 tree tree01 = TREE_OPERAND (arg0, 1);
9587 if (TREE_CODE (tree01) == MULT_EXPR
9588 && TREE_CODE (tree00) == MULT_EXPR)
9590 tree tree0;
9591 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9592 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9597 bit_rotate:
9598 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9599 is a rotate of A by C1 bits. */
9600 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9601 is a rotate of A by B bits.
9602 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
9603 though in this case CODE must be | and not + or ^, otherwise
9604 it doesn't return A when B is 0. */
9606 enum tree_code code0, code1;
9607 tree rtype;
9608 code0 = TREE_CODE (arg0);
9609 code1 = TREE_CODE (arg1);
9610 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9611 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9612 && operand_equal_p (TREE_OPERAND (arg0, 0),
9613 TREE_OPERAND (arg1, 0), 0)
9614 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9615 TYPE_UNSIGNED (rtype))
9616 /* Only create rotates in complete modes. Other cases are not
9617 expanded properly. */
9618 && (element_precision (rtype)
9619 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9621 tree tree01, tree11;
9622 tree orig_tree01, orig_tree11;
9623 enum tree_code code01, code11;
9625 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
9626 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
9627 STRIP_NOPS (tree01);
9628 STRIP_NOPS (tree11);
9629 code01 = TREE_CODE (tree01);
9630 code11 = TREE_CODE (tree11);
9631 if (code11 != MINUS_EXPR
9632 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
9634 std::swap (code0, code1);
9635 std::swap (code01, code11);
9636 std::swap (tree01, tree11);
9637 std::swap (orig_tree01, orig_tree11);
9639 if (code01 == INTEGER_CST
9640 && code11 == INTEGER_CST
9641 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9642 == element_precision (rtype)))
9644 tem = build2_loc (loc, LROTATE_EXPR,
9645 rtype, TREE_OPERAND (arg0, 0),
9646 code0 == LSHIFT_EXPR
9647 ? orig_tree01 : orig_tree11);
9648 return fold_convert_loc (loc, type, tem);
9650 else if (code11 == MINUS_EXPR)
9652 tree tree110, tree111;
9653 tree110 = TREE_OPERAND (tree11, 0);
9654 tree111 = TREE_OPERAND (tree11, 1);
9655 STRIP_NOPS (tree110);
9656 STRIP_NOPS (tree111);
9657 if (TREE_CODE (tree110) == INTEGER_CST
9658 && compare_tree_int (tree110,
9659 element_precision (rtype)) == 0
9660 && operand_equal_p (tree01, tree111, 0))
9662 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9663 ? LROTATE_EXPR : RROTATE_EXPR),
9664 rtype, TREE_OPERAND (arg0, 0),
9665 orig_tree01);
9666 return fold_convert_loc (loc, type, tem);
9669 else if (code == BIT_IOR_EXPR
9670 && code11 == BIT_AND_EXPR
9671 && pow2p_hwi (element_precision (rtype)))
9673 tree tree110, tree111;
9674 tree110 = TREE_OPERAND (tree11, 0);
9675 tree111 = TREE_OPERAND (tree11, 1);
9676 STRIP_NOPS (tree110);
9677 STRIP_NOPS (tree111);
9678 if (TREE_CODE (tree110) == NEGATE_EXPR
9679 && TREE_CODE (tree111) == INTEGER_CST
9680 && compare_tree_int (tree111,
9681 element_precision (rtype) - 1) == 0
9682 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
9684 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9685 ? LROTATE_EXPR : RROTATE_EXPR),
9686 rtype, TREE_OPERAND (arg0, 0),
9687 orig_tree01);
9688 return fold_convert_loc (loc, type, tem);
9694 associate:
9695 /* In most languages, can't associate operations on floats through
9696 parentheses. Rather than remember where the parentheses were, we
9697 don't associate floats at all, unless the user has specified
9698 -fassociative-math.
9699 And, we need to make sure type is not saturating. */
9701 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9702 && !TYPE_SATURATING (type))
9704 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9705 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9706 tree atype = type;
9707 bool ok = true;
9709 /* Split both trees into variables, constants, and literals. Then
9710 associate each group together, the constants with literals,
9711 then the result with variables. This increases the chances of
9712 literals being recombined later and of generating relocatable
9713 expressions for the sum of a constant and literal. */
9714 var0 = split_tree (arg0, type, code,
9715 &minus_var0, &con0, &minus_con0,
9716 &lit0, &minus_lit0, 0);
9717 var1 = split_tree (arg1, type, code,
9718 &minus_var1, &con1, &minus_con1,
9719 &lit1, &minus_lit1, code == MINUS_EXPR);
9721 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9722 if (code == MINUS_EXPR)
9723 code = PLUS_EXPR;
9725 /* With undefined overflow prefer doing association in a type
9726 which wraps on overflow, if that is one of the operand types. */
9727 if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
9728 && !TYPE_OVERFLOW_WRAPS (type))
9730 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9731 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9732 atype = TREE_TYPE (arg0);
9733 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9734 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9735 atype = TREE_TYPE (arg1);
9736 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9739 /* With undefined overflow we can only associate constants with one
9740 variable, and constants whose association doesn't overflow. */
9741 if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
9742 && !TYPE_OVERFLOW_WRAPS (atype))
9744 if ((var0 && var1) || (minus_var0 && minus_var1))
9746 /* ??? If split_tree would handle NEGATE_EXPR we could
9747 simply reject these cases and the allowed cases would
9748 be the var0/minus_var1 ones. */
9749 tree tmp0 = var0 ? var0 : minus_var0;
9750 tree tmp1 = var1 ? var1 : minus_var1;
9751 bool one_neg = false;
9753 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9755 tmp0 = TREE_OPERAND (tmp0, 0);
9756 one_neg = !one_neg;
9758 if (CONVERT_EXPR_P (tmp0)
9759 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9760 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9761 <= TYPE_PRECISION (atype)))
9762 tmp0 = TREE_OPERAND (tmp0, 0);
9763 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9765 tmp1 = TREE_OPERAND (tmp1, 0);
9766 one_neg = !one_neg;
9768 if (CONVERT_EXPR_P (tmp1)
9769 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9770 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9771 <= TYPE_PRECISION (atype)))
9772 tmp1 = TREE_OPERAND (tmp1, 0);
9773 /* The only case we can still associate with two variables
9774 is if they cancel out. */
9775 if (!one_neg
9776 || !operand_equal_p (tmp0, tmp1, 0))
9777 ok = false;
9779 else if ((var0 && minus_var1
9780 && ! operand_equal_p (var0, minus_var1, 0))
9781 || (minus_var0 && var1
9782 && ! operand_equal_p (minus_var0, var1, 0)))
9783 ok = false;
9786 /* Only do something if we found more than two objects. Otherwise,
9787 nothing has changed and we risk infinite recursion. */
9788 if (ok
9789 && ((var0 != 0) + (var1 != 0)
9790 + (minus_var0 != 0) + (minus_var1 != 0)
9791 + (con0 != 0) + (con1 != 0)
9792 + (minus_con0 != 0) + (minus_con1 != 0)
9793 + (lit0 != 0) + (lit1 != 0)
9794 + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
9796 var0 = associate_trees (loc, var0, var1, code, atype);
9797 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9798 code, atype);
9799 con0 = associate_trees (loc, con0, con1, code, atype);
9800 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9801 code, atype);
9802 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9803 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9804 code, atype);
9806 if (minus_var0 && var0)
9808 var0 = associate_trees (loc, var0, minus_var0,
9809 MINUS_EXPR, atype);
9810 minus_var0 = 0;
9812 if (minus_con0 && con0)
9814 con0 = associate_trees (loc, con0, minus_con0,
9815 MINUS_EXPR, atype);
9816 minus_con0 = 0;
9819 /* Preserve the MINUS_EXPR if the negative part of the literal is
9820 greater than the positive part. Otherwise, the multiplicative
9821 folding code (i.e extract_muldiv) may be fooled in case
9822 unsigned constants are subtracted, like in the following
9823 example: ((X*2 + 4) - 8U)/2. */
9824 if (minus_lit0 && lit0)
9826 if (TREE_CODE (lit0) == INTEGER_CST
9827 && TREE_CODE (minus_lit0) == INTEGER_CST
9828 && tree_int_cst_lt (lit0, minus_lit0)
9829 /* But avoid ending up with only negated parts. */
9830 && (var0 || con0))
9832 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9833 MINUS_EXPR, atype);
9834 lit0 = 0;
9836 else
9838 lit0 = associate_trees (loc, lit0, minus_lit0,
9839 MINUS_EXPR, atype);
9840 minus_lit0 = 0;
9844 /* Don't introduce overflows through reassociation. */
9845 if ((lit0 && TREE_OVERFLOW_P (lit0))
9846 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9847 return NULL_TREE;
9849 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9850 con0 = associate_trees (loc, con0, lit0, code, atype);
9851 lit0 = 0;
9852 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9853 code, atype);
9854 minus_lit0 = 0;
9856 /* Eliminate minus_con0. */
9857 if (minus_con0)
9859 if (con0)
9860 con0 = associate_trees (loc, con0, minus_con0,
9861 MINUS_EXPR, atype);
9862 else if (var0)
9863 var0 = associate_trees (loc, var0, minus_con0,
9864 MINUS_EXPR, atype);
9865 else
9866 gcc_unreachable ();
9867 minus_con0 = 0;
9870 /* Eliminate minus_var0. */
9871 if (minus_var0)
9873 if (con0)
9874 con0 = associate_trees (loc, con0, minus_var0,
9875 MINUS_EXPR, atype);
9876 else
9877 gcc_unreachable ();
9878 minus_var0 = 0;
9881 return
9882 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9883 code, atype));
9887 return NULL_TREE;
9889 case POINTER_DIFF_EXPR:
9890 case MINUS_EXPR:
9891 /* Fold &a[i] - &a[j] to i-j. */
9892 if (TREE_CODE (arg0) == ADDR_EXPR
9893 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9894 && TREE_CODE (arg1) == ADDR_EXPR
9895 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9897 tree tem = fold_addr_of_array_ref_difference (loc, type,
9898 TREE_OPERAND (arg0, 0),
9899 TREE_OPERAND (arg1, 0),
9900 code
9901 == POINTER_DIFF_EXPR);
9902 if (tem)
9903 return tem;
9906 /* Further transformations are not for pointers. */
9907 if (code == POINTER_DIFF_EXPR)
9908 return NULL_TREE;
9910 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9911 if (TREE_CODE (arg0) == NEGATE_EXPR
9912 && negate_expr_p (op1)
9913 /* If arg0 is e.g. unsigned int and type is int, then this could
9914 introduce UB, because if A is INT_MIN at runtime, the original
9915 expression can be well defined while the latter is not.
9916 See PR83269. */
9917 && !(ANY_INTEGRAL_TYPE_P (type)
9918 && TYPE_OVERFLOW_UNDEFINED (type)
9919 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9920 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
9921 return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
9922 fold_convert_loc (loc, type,
9923 TREE_OPERAND (arg0, 0)));
9925 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9926 __complex__ ( x, -y ). This is not the same for SNaNs or if
9927 signed zeros are involved. */
9928 if (!HONOR_SNANS (element_mode (arg0))
9929 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9930 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9932 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9933 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9934 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9935 bool arg0rz = false, arg0iz = false;
9936 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9937 || (arg0i && (arg0iz = real_zerop (arg0i))))
9939 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9940 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9941 if (arg0rz && arg1i && real_zerop (arg1i))
9943 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9944 arg1r ? arg1r
9945 : build1 (REALPART_EXPR, rtype, arg1));
9946 tree ip = arg0i ? arg0i
9947 : build1 (IMAGPART_EXPR, rtype, arg0);
9948 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9950 else if (arg0iz && arg1r && real_zerop (arg1r))
9952 tree rp = arg0r ? arg0r
9953 : build1 (REALPART_EXPR, rtype, arg0);
9954 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9955 arg1i ? arg1i
9956 : build1 (IMAGPART_EXPR, rtype, arg1));
9957 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9962 /* A - B -> A + (-B) if B is easily negatable. */
9963 if (negate_expr_p (op1)
9964 && ! TYPE_OVERFLOW_SANITIZED (type)
9965 && ((FLOAT_TYPE_P (type)
9966 /* Avoid this transformation if B is a positive REAL_CST. */
9967 && (TREE_CODE (op1) != REAL_CST
9968 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9969 || INTEGRAL_TYPE_P (type)))
9970 return fold_build2_loc (loc, PLUS_EXPR, type,
9971 fold_convert_loc (loc, type, arg0),
9972 negate_expr (op1));
9974 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9975 one. Make sure the type is not saturating and has the signedness of
9976 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9977 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9978 if ((TREE_CODE (arg0) == MULT_EXPR
9979 || TREE_CODE (arg1) == MULT_EXPR)
9980 && !TYPE_SATURATING (type)
9981 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9982 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9983 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9985 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9986 if (tem)
9987 return tem;
9990 goto associate;
9992 case MULT_EXPR:
9993 if (! FLOAT_TYPE_P (type))
9995 /* Transform x * -C into -x * C if x is easily negatable. */
9996 if (TREE_CODE (op1) == INTEGER_CST
9997 && tree_int_cst_sgn (op1) == -1
9998 && negate_expr_p (op0)
9999 && negate_expr_p (op1)
10000 && (tem = negate_expr (op1)) != op1
10001 && ! TREE_OVERFLOW (tem))
10002 return fold_build2_loc (loc, MULT_EXPR, type,
10003 fold_convert_loc (loc, type,
10004 negate_expr (op0)), tem);
10006 strict_overflow_p = false;
10007 if (TREE_CODE (arg1) == INTEGER_CST
10008 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10009 &strict_overflow_p)) != 0)
10011 if (strict_overflow_p)
10012 fold_overflow_warning (("assuming signed overflow does not "
10013 "occur when simplifying "
10014 "multiplication"),
10015 WARN_STRICT_OVERFLOW_MISC);
10016 return fold_convert_loc (loc, type, tem);
10019 /* Optimize z * conj(z) for integer complex numbers. */
10020 if (TREE_CODE (arg0) == CONJ_EXPR
10021 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10022 return fold_mult_zconjz (loc, type, arg1);
10023 if (TREE_CODE (arg1) == CONJ_EXPR
10024 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10025 return fold_mult_zconjz (loc, type, arg0);
10027 else
10029 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10030 This is not the same for NaNs or if signed zeros are
10031 involved. */
10032 if (!HONOR_NANS (arg0)
10033 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10034 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10035 && TREE_CODE (arg1) == COMPLEX_CST
10036 && real_zerop (TREE_REALPART (arg1)))
10038 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10039 if (real_onep (TREE_IMAGPART (arg1)))
10040 return
10041 fold_build2_loc (loc, COMPLEX_EXPR, type,
10042 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10043 rtype, arg0)),
10044 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10045 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10046 return
10047 fold_build2_loc (loc, COMPLEX_EXPR, type,
10048 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10049 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10050 rtype, arg0)));
10053 /* Optimize z * conj(z) for floating point complex numbers.
10054 Guarded by flag_unsafe_math_optimizations as non-finite
10055 imaginary components don't produce scalar results. */
10056 if (flag_unsafe_math_optimizations
10057 && TREE_CODE (arg0) == CONJ_EXPR
10058 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10059 return fold_mult_zconjz (loc, type, arg1);
10060 if (flag_unsafe_math_optimizations
10061 && TREE_CODE (arg1) == CONJ_EXPR
10062 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10063 return fold_mult_zconjz (loc, type, arg0);
10065 goto associate;
10067 case BIT_IOR_EXPR:
10068 /* Canonicalize (X & C1) | C2. */
10069 if (TREE_CODE (arg0) == BIT_AND_EXPR
10070 && TREE_CODE (arg1) == INTEGER_CST
10071 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10073 int width = TYPE_PRECISION (type), w;
10074 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
10075 wide_int c2 = wi::to_wide (arg1);
10077 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10078 if ((c1 & c2) == c1)
10079 return omit_one_operand_loc (loc, type, arg1,
10080 TREE_OPERAND (arg0, 0));
10082 wide_int msk = wi::mask (width, false,
10083 TYPE_PRECISION (TREE_TYPE (arg1)));
10085 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10086 if (wi::bit_and_not (msk, c1 | c2) == 0)
10088 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10089 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10092 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10093 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10094 mode which allows further optimizations. */
10095 c1 &= msk;
10096 c2 &= msk;
10097 wide_int c3 = wi::bit_and_not (c1, c2);
10098 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10100 wide_int mask = wi::mask (w, false,
10101 TYPE_PRECISION (type));
10102 if (((c1 | c2) & mask) == mask
10103 && wi::bit_and_not (c1, mask) == 0)
10105 c3 = mask;
10106 break;
10110 if (c3 != c1)
10112 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10113 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
10114 wide_int_to_tree (type, c3));
10115 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10119 /* See if this can be simplified into a rotate first. If that
10120 is unsuccessful continue in the association code. */
10121 goto bit_rotate;
10123 case BIT_XOR_EXPR:
10124 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10125 if (TREE_CODE (arg0) == BIT_AND_EXPR
10126 && INTEGRAL_TYPE_P (type)
10127 && integer_onep (TREE_OPERAND (arg0, 1))
10128 && integer_onep (arg1))
10129 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10130 build_zero_cst (TREE_TYPE (arg0)));
10132 /* See if this can be simplified into a rotate first. If that
10133 is unsuccessful continue in the association code. */
10134 goto bit_rotate;
10136 case BIT_AND_EXPR:
10137 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10138 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10139 && INTEGRAL_TYPE_P (type)
10140 && integer_onep (TREE_OPERAND (arg0, 1))
10141 && integer_onep (arg1))
10143 tree tem2;
10144 tem = TREE_OPERAND (arg0, 0);
10145 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10146 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10147 tem, tem2);
10148 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10149 build_zero_cst (TREE_TYPE (tem)));
10151 /* Fold ~X & 1 as (X & 1) == 0. */
10152 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10153 && INTEGRAL_TYPE_P (type)
10154 && integer_onep (arg1))
10156 tree tem2;
10157 tem = TREE_OPERAND (arg0, 0);
10158 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10159 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10160 tem, tem2);
10161 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10162 build_zero_cst (TREE_TYPE (tem)));
10164 /* Fold !X & 1 as X == 0. */
10165 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10166 && integer_onep (arg1))
10168 tem = TREE_OPERAND (arg0, 0);
10169 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10170 build_zero_cst (TREE_TYPE (tem)));
10173 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10174 multiple of 1 << CST. */
10175 if (TREE_CODE (arg1) == INTEGER_CST)
10177 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10178 wide_int ncst1 = -cst1;
10179 if ((cst1 & ncst1) == ncst1
10180 && multiple_of_p (type, arg0,
10181 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10182 return fold_convert_loc (loc, type, arg0);
10185 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10186 bits from CST2. */
10187 if (TREE_CODE (arg1) == INTEGER_CST
10188 && TREE_CODE (arg0) == MULT_EXPR
10189 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10191 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
10192 wide_int masked
10193 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
10195 if (masked == 0)
10196 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10197 arg0, arg1);
10198 else if (masked != warg1)
10200 /* Avoid the transform if arg1 is a mask of some
10201 mode which allows further optimizations. */
10202 int pop = wi::popcount (warg1);
10203 if (!(pop >= BITS_PER_UNIT
10204 && pow2p_hwi (pop)
10205 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10206 return fold_build2_loc (loc, code, type, op0,
10207 wide_int_to_tree (type, masked));
10211 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10212 ((A & N) + B) & M -> (A + B) & M
10213 Similarly if (N & M) == 0,
10214 ((A | N) + B) & M -> (A + B) & M
10215 and for - instead of + (or unary - instead of +)
10216 and/or ^ instead of |.
10217 If B is constant and (B & M) == 0, fold into A & M. */
10218 if (TREE_CODE (arg1) == INTEGER_CST)
10220 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10221 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10222 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10223 && (TREE_CODE (arg0) == PLUS_EXPR
10224 || TREE_CODE (arg0) == MINUS_EXPR
10225 || TREE_CODE (arg0) == NEGATE_EXPR)
10226 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10227 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10229 tree pmop[2];
10230 int which = 0;
10231 wide_int cst0;
10233 /* Now we know that arg0 is (C + D) or (C - D) or
10234 -C and arg1 (M) is == (1LL << cst) - 1.
10235 Store C into PMOP[0] and D into PMOP[1]. */
10236 pmop[0] = TREE_OPERAND (arg0, 0);
10237 pmop[1] = NULL;
10238 if (TREE_CODE (arg0) != NEGATE_EXPR)
10240 pmop[1] = TREE_OPERAND (arg0, 1);
10241 which = 1;
10244 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10245 which = -1;
10247 for (; which >= 0; which--)
10248 switch (TREE_CODE (pmop[which]))
10250 case BIT_AND_EXPR:
10251 case BIT_IOR_EXPR:
10252 case BIT_XOR_EXPR:
10253 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10254 != INTEGER_CST)
10255 break;
10256 cst0 = wi::to_wide (TREE_OPERAND (pmop[which], 1)) & cst1;
10257 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10259 if (cst0 != cst1)
10260 break;
10262 else if (cst0 != 0)
10263 break;
10264 /* If C or D is of the form (A & N) where
10265 (N & M) == M, or of the form (A | N) or
10266 (A ^ N) where (N & M) == 0, replace it with A. */
10267 pmop[which] = TREE_OPERAND (pmop[which], 0);
10268 break;
10269 case INTEGER_CST:
10270 /* If C or D is a N where (N & M) == 0, it can be
10271 omitted (assumed 0). */
10272 if ((TREE_CODE (arg0) == PLUS_EXPR
10273 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10274 && (cst1 & wi::to_wide (pmop[which])) == 0)
10275 pmop[which] = NULL;
10276 break;
10277 default:
10278 break;
10281 /* Only build anything new if we optimized one or both arguments
10282 above. */
10283 if (pmop[0] != TREE_OPERAND (arg0, 0)
10284 || (TREE_CODE (arg0) != NEGATE_EXPR
10285 && pmop[1] != TREE_OPERAND (arg0, 1)))
10287 tree utype = TREE_TYPE (arg0);
10288 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10290 /* Perform the operations in a type that has defined
10291 overflow behavior. */
10292 utype = unsigned_type_for (TREE_TYPE (arg0));
10293 if (pmop[0] != NULL)
10294 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10295 if (pmop[1] != NULL)
10296 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10299 if (TREE_CODE (arg0) == NEGATE_EXPR)
10300 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10301 else if (TREE_CODE (arg0) == PLUS_EXPR)
10303 if (pmop[0] != NULL && pmop[1] != NULL)
10304 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10305 pmop[0], pmop[1]);
10306 else if (pmop[0] != NULL)
10307 tem = pmop[0];
10308 else if (pmop[1] != NULL)
10309 tem = pmop[1];
10310 else
10311 return build_int_cst (type, 0);
10313 else if (pmop[0] == NULL)
10314 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10315 else
10316 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10317 pmop[0], pmop[1]);
10318 /* TEM is now the new binary +, - or unary - replacement. */
10319 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10320 fold_convert_loc (loc, utype, arg1));
10321 return fold_convert_loc (loc, type, tem);
10326 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10327 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10328 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10330 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10332 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
10333 if (mask == -1)
10334 return
10335 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10338 goto associate;
10340 case RDIV_EXPR:
10341 /* Don't touch a floating-point divide by zero unless the mode
10342 of the constant can represent infinity. */
10343 if (TREE_CODE (arg1) == REAL_CST
10344 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10345 && real_zerop (arg1))
10346 return NULL_TREE;
10348 /* (-A) / (-B) -> A / B */
10349 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10350 return fold_build2_loc (loc, RDIV_EXPR, type,
10351 TREE_OPERAND (arg0, 0),
10352 negate_expr (arg1));
10353 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10354 return fold_build2_loc (loc, RDIV_EXPR, type,
10355 negate_expr (arg0),
10356 TREE_OPERAND (arg1, 0));
10357 return NULL_TREE;
10359 case TRUNC_DIV_EXPR:
10360 /* Fall through */
10362 case FLOOR_DIV_EXPR:
10363 /* Simplify A / (B << N) where A and B are positive and B is
10364 a power of 2, to A >> (N + log2(B)). */
10365 strict_overflow_p = false;
10366 if (TREE_CODE (arg1) == LSHIFT_EXPR
10367 && (TYPE_UNSIGNED (type)
10368 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10370 tree sval = TREE_OPERAND (arg1, 0);
10371 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10373 tree sh_cnt = TREE_OPERAND (arg1, 1);
10374 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10375 wi::exact_log2 (wi::to_wide (sval)));
10377 if (strict_overflow_p)
10378 fold_overflow_warning (("assuming signed overflow does not "
10379 "occur when simplifying A / (B << N)"),
10380 WARN_STRICT_OVERFLOW_MISC);
10382 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10383 sh_cnt, pow2);
10384 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10385 fold_convert_loc (loc, type, arg0), sh_cnt);
10389 /* Fall through */
10391 case ROUND_DIV_EXPR:
10392 case CEIL_DIV_EXPR:
10393 case EXACT_DIV_EXPR:
10394 if (integer_zerop (arg1))
10395 return NULL_TREE;
10397 /* Convert -A / -B to A / B when the type is signed and overflow is
10398 undefined. */
10399 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10400 && TREE_CODE (op0) == NEGATE_EXPR
10401 && negate_expr_p (op1))
10403 if (INTEGRAL_TYPE_P (type))
10404 fold_overflow_warning (("assuming signed overflow does not occur "
10405 "when distributing negation across "
10406 "division"),
10407 WARN_STRICT_OVERFLOW_MISC);
10408 return fold_build2_loc (loc, code, type,
10409 fold_convert_loc (loc, type,
10410 TREE_OPERAND (arg0, 0)),
10411 negate_expr (op1));
10413 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10414 && TREE_CODE (arg1) == NEGATE_EXPR
10415 && negate_expr_p (op0))
10417 if (INTEGRAL_TYPE_P (type))
10418 fold_overflow_warning (("assuming signed overflow does not occur "
10419 "when distributing negation across "
10420 "division"),
10421 WARN_STRICT_OVERFLOW_MISC);
10422 return fold_build2_loc (loc, code, type,
10423 negate_expr (op0),
10424 fold_convert_loc (loc, type,
10425 TREE_OPERAND (arg1, 0)));
10428 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10429 operation, EXACT_DIV_EXPR.
10431 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10432 At one time others generated faster code, it's not clear if they do
10433 after the last round to changes to the DIV code in expmed.c. */
10434 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10435 && multiple_of_p (type, arg0, arg1))
10436 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10437 fold_convert (type, arg0),
10438 fold_convert (type, arg1));
10440 strict_overflow_p = false;
10441 if (TREE_CODE (arg1) == INTEGER_CST
10442 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10443 &strict_overflow_p)) != 0)
10445 if (strict_overflow_p)
10446 fold_overflow_warning (("assuming signed overflow does not occur "
10447 "when simplifying division"),
10448 WARN_STRICT_OVERFLOW_MISC);
10449 return fold_convert_loc (loc, type, tem);
10452 return NULL_TREE;
10454 case CEIL_MOD_EXPR:
10455 case FLOOR_MOD_EXPR:
10456 case ROUND_MOD_EXPR:
10457 case TRUNC_MOD_EXPR:
10458 strict_overflow_p = false;
10459 if (TREE_CODE (arg1) == INTEGER_CST
10460 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10461 &strict_overflow_p)) != 0)
10463 if (strict_overflow_p)
10464 fold_overflow_warning (("assuming signed overflow does not occur "
10465 "when simplifying modulus"),
10466 WARN_STRICT_OVERFLOW_MISC);
10467 return fold_convert_loc (loc, type, tem);
10470 return NULL_TREE;
10472 case LROTATE_EXPR:
10473 case RROTATE_EXPR:
10474 case RSHIFT_EXPR:
10475 case LSHIFT_EXPR:
10476 /* Since negative shift count is not well-defined,
10477 don't try to compute it in the compiler. */
10478 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10479 return NULL_TREE;
10481 prec = element_precision (type);
10483 /* If we have a rotate of a bit operation with the rotate count and
10484 the second operand of the bit operation both constant,
10485 permute the two operations. */
10486 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10487 && (TREE_CODE (arg0) == BIT_AND_EXPR
10488 || TREE_CODE (arg0) == BIT_IOR_EXPR
10489 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10490 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10492 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10493 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10494 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10495 fold_build2_loc (loc, code, type,
10496 arg00, arg1),
10497 fold_build2_loc (loc, code, type,
10498 arg01, arg1));
10501 /* Two consecutive rotates adding up to the some integer
10502 multiple of the precision of the type can be ignored. */
10503 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10504 && TREE_CODE (arg0) == RROTATE_EXPR
10505 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10506 && wi::umod_trunc (wi::to_wide (arg1)
10507 + wi::to_wide (TREE_OPERAND (arg0, 1)),
10508 prec) == 0)
10509 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10511 return NULL_TREE;
10513 case MIN_EXPR:
10514 case MAX_EXPR:
10515 goto associate;
10517 case TRUTH_ANDIF_EXPR:
10518 /* Note that the operands of this must be ints
10519 and their values must be 0 or 1.
10520 ("true" is a fixed value perhaps depending on the language.) */
10521 /* If first arg is constant zero, return it. */
10522 if (integer_zerop (arg0))
10523 return fold_convert_loc (loc, type, arg0);
10524 /* FALLTHRU */
10525 case TRUTH_AND_EXPR:
10526 /* If either arg is constant true, drop it. */
10527 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10528 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10529 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10530 /* Preserve sequence points. */
10531 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10532 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10533 /* If second arg is constant zero, result is zero, but first arg
10534 must be evaluated. */
10535 if (integer_zerop (arg1))
10536 return omit_one_operand_loc (loc, type, arg1, arg0);
10537 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10538 case will be handled here. */
10539 if (integer_zerop (arg0))
10540 return omit_one_operand_loc (loc, type, arg0, arg1);
10542 /* !X && X is always false. */
10543 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10544 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10545 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10546 /* X && !X is always false. */
10547 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10548 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10549 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10551 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10552 means A >= Y && A != MAX, but in this case we know that
10553 A < X <= MAX. */
10555 if (!TREE_SIDE_EFFECTS (arg0)
10556 && !TREE_SIDE_EFFECTS (arg1))
10558 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10559 if (tem && !operand_equal_p (tem, arg0, 0))
10560 return fold_build2_loc (loc, code, type, tem, arg1);
10562 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10563 if (tem && !operand_equal_p (tem, arg1, 0))
10564 return fold_build2_loc (loc, code, type, arg0, tem);
10567 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10568 != NULL_TREE)
10569 return tem;
10571 return NULL_TREE;
10573 case TRUTH_ORIF_EXPR:
10574 /* Note that the operands of this must be ints
10575 and their values must be 0 or true.
10576 ("true" is a fixed value perhaps depending on the language.) */
10577 /* If first arg is constant true, return it. */
10578 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10579 return fold_convert_loc (loc, type, arg0);
10580 /* FALLTHRU */
10581 case TRUTH_OR_EXPR:
10582 /* If either arg is constant zero, drop it. */
10583 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10584 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10585 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10586 /* Preserve sequence points. */
10587 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10588 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10589 /* If second arg is constant true, result is true, but we must
10590 evaluate first arg. */
10591 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10592 return omit_one_operand_loc (loc, type, arg1, arg0);
10593 /* Likewise for first arg, but note this only occurs here for
10594 TRUTH_OR_EXPR. */
10595 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10596 return omit_one_operand_loc (loc, type, arg0, arg1);
10598 /* !X || X is always true. */
10599 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10600 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10601 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10602 /* X || !X is always true. */
10603 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10604 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10605 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10607 /* (X && !Y) || (!X && Y) is X ^ Y */
10608 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10609 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10611 tree a0, a1, l0, l1, n0, n1;
10613 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10614 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10616 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10617 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10619 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10620 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10622 if ((operand_equal_p (n0, a0, 0)
10623 && operand_equal_p (n1, a1, 0))
10624 || (operand_equal_p (n0, a1, 0)
10625 && operand_equal_p (n1, a0, 0)))
10626 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10629 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10630 != NULL_TREE)
10631 return tem;
10633 return NULL_TREE;
10635 case TRUTH_XOR_EXPR:
10636 /* If the second arg is constant zero, drop it. */
10637 if (integer_zerop (arg1))
10638 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10639 /* If the second arg is constant true, this is a logical inversion. */
10640 if (integer_onep (arg1))
10642 tem = invert_truthvalue_loc (loc, arg0);
10643 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10645 /* Identical arguments cancel to zero. */
10646 if (operand_equal_p (arg0, arg1, 0))
10647 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10649 /* !X ^ X is always true. */
10650 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10651 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10652 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10654 /* X ^ !X is always true. */
10655 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10656 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10657 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10659 return NULL_TREE;
10661 case EQ_EXPR:
10662 case NE_EXPR:
10663 STRIP_NOPS (arg0);
10664 STRIP_NOPS (arg1);
10666 tem = fold_comparison (loc, code, type, op0, op1);
10667 if (tem != NULL_TREE)
10668 return tem;
10670 /* bool_var != 1 becomes !bool_var. */
10671 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10672 && code == NE_EXPR)
10673 return fold_convert_loc (loc, type,
10674 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10675 TREE_TYPE (arg0), arg0));
10677 /* bool_var == 0 becomes !bool_var. */
10678 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10679 && code == EQ_EXPR)
10680 return fold_convert_loc (loc, type,
10681 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10682 TREE_TYPE (arg0), arg0));
10684 /* !exp != 0 becomes !exp */
10685 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10686 && code == NE_EXPR)
10687 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10689 /* If this is an EQ or NE comparison with zero and ARG0 is
10690 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10691 two operations, but the latter can be done in one less insn
10692 on machines that have only two-operand insns or on which a
10693 constant cannot be the first operand. */
10694 if (TREE_CODE (arg0) == BIT_AND_EXPR
10695 && integer_zerop (arg1))
10697 tree arg00 = TREE_OPERAND (arg0, 0);
10698 tree arg01 = TREE_OPERAND (arg0, 1);
10699 if (TREE_CODE (arg00) == LSHIFT_EXPR
10700 && integer_onep (TREE_OPERAND (arg00, 0)))
10702 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10703 arg01, TREE_OPERAND (arg00, 1));
10704 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10705 build_int_cst (TREE_TYPE (arg0), 1));
10706 return fold_build2_loc (loc, code, type,
10707 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10708 arg1);
10710 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10711 && integer_onep (TREE_OPERAND (arg01, 0)))
10713 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10714 arg00, TREE_OPERAND (arg01, 1));
10715 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10716 build_int_cst (TREE_TYPE (arg0), 1));
10717 return fold_build2_loc (loc, code, type,
10718 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10719 arg1);
10723 /* If this is an NE or EQ comparison of zero against the result of a
10724 signed MOD operation whose second operand is a power of 2, make
10725 the MOD operation unsigned since it is simpler and equivalent. */
10726 if (integer_zerop (arg1)
10727 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10728 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10729 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10730 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10731 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10732 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10734 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10735 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10736 fold_convert_loc (loc, newtype,
10737 TREE_OPERAND (arg0, 0)),
10738 fold_convert_loc (loc, newtype,
10739 TREE_OPERAND (arg0, 1)));
10741 return fold_build2_loc (loc, code, type, newmod,
10742 fold_convert_loc (loc, newtype, arg1));
10745 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10746 C1 is a valid shift constant, and C2 is a power of two, i.e.
10747 a single bit. */
10748 if (TREE_CODE (arg0) == BIT_AND_EXPR
10749 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10750 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10751 == INTEGER_CST
10752 && integer_pow2p (TREE_OPERAND (arg0, 1))
10753 && integer_zerop (arg1))
10755 tree itype = TREE_TYPE (arg0);
10756 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10757 prec = TYPE_PRECISION (itype);
10759 /* Check for a valid shift count. */
10760 if (wi::ltu_p (wi::to_wide (arg001), prec))
10762 tree arg01 = TREE_OPERAND (arg0, 1);
10763 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10764 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10765 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10766 can be rewritten as (X & (C2 << C1)) != 0. */
10767 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10769 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10770 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10771 return fold_build2_loc (loc, code, type, tem,
10772 fold_convert_loc (loc, itype, arg1));
10774 /* Otherwise, for signed (arithmetic) shifts,
10775 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10776 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10777 else if (!TYPE_UNSIGNED (itype))
10778 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10779 arg000, build_int_cst (itype, 0));
10780 /* Otherwise, of unsigned (logical) shifts,
10781 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10782 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10783 else
10784 return omit_one_operand_loc (loc, type,
10785 code == EQ_EXPR ? integer_one_node
10786 : integer_zero_node,
10787 arg000);
10791 /* If this is a comparison of a field, we may be able to simplify it. */
10792 if ((TREE_CODE (arg0) == COMPONENT_REF
10793 || TREE_CODE (arg0) == BIT_FIELD_REF)
10794 /* Handle the constant case even without -O
10795 to make sure the warnings are given. */
10796 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10798 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10799 if (t1)
10800 return t1;
10803 /* Optimize comparisons of strlen vs zero to a compare of the
10804 first character of the string vs zero. To wit,
10805 strlen(ptr) == 0 => *ptr == 0
10806 strlen(ptr) != 0 => *ptr != 0
10807 Other cases should reduce to one of these two (or a constant)
10808 due to the return value of strlen being unsigned. */
10809 if (TREE_CODE (arg0) == CALL_EXPR
10810 && integer_zerop (arg1))
10812 tree fndecl = get_callee_fndecl (arg0);
10814 if (fndecl
10815 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10816 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10817 && call_expr_nargs (arg0) == 1
10818 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10820 tree iref = build_fold_indirect_ref_loc (loc,
10821 CALL_EXPR_ARG (arg0, 0));
10822 return fold_build2_loc (loc, code, type, iref,
10823 build_int_cst (TREE_TYPE (iref), 0));
10827 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10828 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10829 if (TREE_CODE (arg0) == RSHIFT_EXPR
10830 && integer_zerop (arg1)
10831 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10833 tree arg00 = TREE_OPERAND (arg0, 0);
10834 tree arg01 = TREE_OPERAND (arg0, 1);
10835 tree itype = TREE_TYPE (arg00);
10836 if (wi::to_wide (arg01) == element_precision (itype) - 1)
10838 if (TYPE_UNSIGNED (itype))
10840 itype = signed_type_for (itype);
10841 arg00 = fold_convert_loc (loc, itype, arg00);
10843 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10844 type, arg00, build_zero_cst (itype));
10848 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10849 (X & C) == 0 when C is a single bit. */
10850 if (TREE_CODE (arg0) == BIT_AND_EXPR
10851 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10852 && integer_zerop (arg1)
10853 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10855 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10856 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10857 TREE_OPERAND (arg0, 1));
10858 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10859 type, tem,
10860 fold_convert_loc (loc, TREE_TYPE (arg0),
10861 arg1));
10864 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10865 constant C is a power of two, i.e. a single bit. */
10866 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10867 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10868 && integer_zerop (arg1)
10869 && integer_pow2p (TREE_OPERAND (arg0, 1))
10870 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10871 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10873 tree arg00 = TREE_OPERAND (arg0, 0);
10874 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10875 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10878 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10879 when is C is a power of two, i.e. a single bit. */
10880 if (TREE_CODE (arg0) == BIT_AND_EXPR
10881 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10882 && integer_zerop (arg1)
10883 && integer_pow2p (TREE_OPERAND (arg0, 1))
10884 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10885 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10887 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10888 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10889 arg000, TREE_OPERAND (arg0, 1));
10890 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10891 tem, build_int_cst (TREE_TYPE (tem), 0));
10894 if (integer_zerop (arg1)
10895 && tree_expr_nonzero_p (arg0))
10897 tree res = constant_boolean_node (code==NE_EXPR, type);
10898 return omit_one_operand_loc (loc, type, res, arg0);
10901 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10902 if (TREE_CODE (arg0) == BIT_AND_EXPR
10903 && TREE_CODE (arg1) == BIT_AND_EXPR)
10905 tree arg00 = TREE_OPERAND (arg0, 0);
10906 tree arg01 = TREE_OPERAND (arg0, 1);
10907 tree arg10 = TREE_OPERAND (arg1, 0);
10908 tree arg11 = TREE_OPERAND (arg1, 1);
10909 tree itype = TREE_TYPE (arg0);
10911 if (operand_equal_p (arg01, arg11, 0))
10913 tem = fold_convert_loc (loc, itype, arg10);
10914 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10915 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10916 return fold_build2_loc (loc, code, type, tem,
10917 build_zero_cst (itype));
10919 if (operand_equal_p (arg01, arg10, 0))
10921 tem = fold_convert_loc (loc, itype, arg11);
10922 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10923 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10924 return fold_build2_loc (loc, code, type, tem,
10925 build_zero_cst (itype));
10927 if (operand_equal_p (arg00, arg11, 0))
10929 tem = fold_convert_loc (loc, itype, arg10);
10930 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10931 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10932 return fold_build2_loc (loc, code, type, tem,
10933 build_zero_cst (itype));
10935 if (operand_equal_p (arg00, arg10, 0))
10937 tem = fold_convert_loc (loc, itype, arg11);
10938 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10939 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10940 return fold_build2_loc (loc, code, type, tem,
10941 build_zero_cst (itype));
10945 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10946 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10948 tree arg00 = TREE_OPERAND (arg0, 0);
10949 tree arg01 = TREE_OPERAND (arg0, 1);
10950 tree arg10 = TREE_OPERAND (arg1, 0);
10951 tree arg11 = TREE_OPERAND (arg1, 1);
10952 tree itype = TREE_TYPE (arg0);
10954 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10955 operand_equal_p guarantees no side-effects so we don't need
10956 to use omit_one_operand on Z. */
10957 if (operand_equal_p (arg01, arg11, 0))
10958 return fold_build2_loc (loc, code, type, arg00,
10959 fold_convert_loc (loc, TREE_TYPE (arg00),
10960 arg10));
10961 if (operand_equal_p (arg01, arg10, 0))
10962 return fold_build2_loc (loc, code, type, arg00,
10963 fold_convert_loc (loc, TREE_TYPE (arg00),
10964 arg11));
10965 if (operand_equal_p (arg00, arg11, 0))
10966 return fold_build2_loc (loc, code, type, arg01,
10967 fold_convert_loc (loc, TREE_TYPE (arg01),
10968 arg10));
10969 if (operand_equal_p (arg00, arg10, 0))
10970 return fold_build2_loc (loc, code, type, arg01,
10971 fold_convert_loc (loc, TREE_TYPE (arg01),
10972 arg11));
10974 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10975 if (TREE_CODE (arg01) == INTEGER_CST
10976 && TREE_CODE (arg11) == INTEGER_CST)
10978 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10979 fold_convert_loc (loc, itype, arg11));
10980 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10981 return fold_build2_loc (loc, code, type, tem,
10982 fold_convert_loc (loc, itype, arg10));
10986 /* Attempt to simplify equality/inequality comparisons of complex
10987 values. Only lower the comparison if the result is known or
10988 can be simplified to a single scalar comparison. */
10989 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10990 || TREE_CODE (arg0) == COMPLEX_CST)
10991 && (TREE_CODE (arg1) == COMPLEX_EXPR
10992 || TREE_CODE (arg1) == COMPLEX_CST))
10994 tree real0, imag0, real1, imag1;
10995 tree rcond, icond;
10997 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10999 real0 = TREE_OPERAND (arg0, 0);
11000 imag0 = TREE_OPERAND (arg0, 1);
11002 else
11004 real0 = TREE_REALPART (arg0);
11005 imag0 = TREE_IMAGPART (arg0);
11008 if (TREE_CODE (arg1) == COMPLEX_EXPR)
11010 real1 = TREE_OPERAND (arg1, 0);
11011 imag1 = TREE_OPERAND (arg1, 1);
11013 else
11015 real1 = TREE_REALPART (arg1);
11016 imag1 = TREE_IMAGPART (arg1);
11019 rcond = fold_binary_loc (loc, code, type, real0, real1);
11020 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11022 if (integer_zerop (rcond))
11024 if (code == EQ_EXPR)
11025 return omit_two_operands_loc (loc, type, boolean_false_node,
11026 imag0, imag1);
11027 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
11029 else
11031 if (code == NE_EXPR)
11032 return omit_two_operands_loc (loc, type, boolean_true_node,
11033 imag0, imag1);
11034 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
11038 icond = fold_binary_loc (loc, code, type, imag0, imag1);
11039 if (icond && TREE_CODE (icond) == INTEGER_CST)
11041 if (integer_zerop (icond))
11043 if (code == EQ_EXPR)
11044 return omit_two_operands_loc (loc, type, boolean_false_node,
11045 real0, real1);
11046 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11048 else
11050 if (code == NE_EXPR)
11051 return omit_two_operands_loc (loc, type, boolean_true_node,
11052 real0, real1);
11053 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11058 return NULL_TREE;
11060 case LT_EXPR:
11061 case GT_EXPR:
11062 case LE_EXPR:
11063 case GE_EXPR:
11064 tem = fold_comparison (loc, code, type, op0, op1);
11065 if (tem != NULL_TREE)
11066 return tem;
11068 /* Transform comparisons of the form X +- C CMP X. */
11069 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11070 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11071 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11072 && !HONOR_SNANS (arg0))
11074 tree arg01 = TREE_OPERAND (arg0, 1);
11075 enum tree_code code0 = TREE_CODE (arg0);
11076 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11078 /* (X - c) > X becomes false. */
11079 if (code == GT_EXPR
11080 && ((code0 == MINUS_EXPR && is_positive >= 0)
11081 || (code0 == PLUS_EXPR && is_positive <= 0)))
11082 return constant_boolean_node (0, type);
11084 /* Likewise (X + c) < X becomes false. */
11085 if (code == LT_EXPR
11086 && ((code0 == PLUS_EXPR && is_positive >= 0)
11087 || (code0 == MINUS_EXPR && is_positive <= 0)))
11088 return constant_boolean_node (0, type);
11090 /* Convert (X - c) <= X to true. */
11091 if (!HONOR_NANS (arg1)
11092 && code == LE_EXPR
11093 && ((code0 == MINUS_EXPR && is_positive >= 0)
11094 || (code0 == PLUS_EXPR && is_positive <= 0)))
11095 return constant_boolean_node (1, type);
11097 /* Convert (X + c) >= X to true. */
11098 if (!HONOR_NANS (arg1)
11099 && code == GE_EXPR
11100 && ((code0 == PLUS_EXPR && is_positive >= 0)
11101 || (code0 == MINUS_EXPR && is_positive <= 0)))
11102 return constant_boolean_node (1, type);
11105 /* If we are comparing an ABS_EXPR with a constant, we can
11106 convert all the cases into explicit comparisons, but they may
11107 well not be faster than doing the ABS and one comparison.
11108 But ABS (X) <= C is a range comparison, which becomes a subtraction
11109 and a comparison, and is probably faster. */
11110 if (code == LE_EXPR
11111 && TREE_CODE (arg1) == INTEGER_CST
11112 && TREE_CODE (arg0) == ABS_EXPR
11113 && ! TREE_SIDE_EFFECTS (arg0)
11114 && (tem = negate_expr (arg1)) != 0
11115 && TREE_CODE (tem) == INTEGER_CST
11116 && !TREE_OVERFLOW (tem))
11117 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11118 build2 (GE_EXPR, type,
11119 TREE_OPERAND (arg0, 0), tem),
11120 build2 (LE_EXPR, type,
11121 TREE_OPERAND (arg0, 0), arg1));
11123 /* Convert ABS_EXPR<x> >= 0 to true. */
11124 strict_overflow_p = false;
11125 if (code == GE_EXPR
11126 && (integer_zerop (arg1)
11127 || (! HONOR_NANS (arg0)
11128 && real_zerop (arg1)))
11129 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11131 if (strict_overflow_p)
11132 fold_overflow_warning (("assuming signed overflow does not occur "
11133 "when simplifying comparison of "
11134 "absolute value and zero"),
11135 WARN_STRICT_OVERFLOW_CONDITIONAL);
11136 return omit_one_operand_loc (loc, type,
11137 constant_boolean_node (true, type),
11138 arg0);
11141 /* Convert ABS_EXPR<x> < 0 to false. */
11142 strict_overflow_p = false;
11143 if (code == LT_EXPR
11144 && (integer_zerop (arg1) || real_zerop (arg1))
11145 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11147 if (strict_overflow_p)
11148 fold_overflow_warning (("assuming signed overflow does not occur "
11149 "when simplifying comparison of "
11150 "absolute value and zero"),
11151 WARN_STRICT_OVERFLOW_CONDITIONAL);
11152 return omit_one_operand_loc (loc, type,
11153 constant_boolean_node (false, type),
11154 arg0);
11157 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11158 and similarly for >= into !=. */
11159 if ((code == LT_EXPR || code == GE_EXPR)
11160 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11161 && TREE_CODE (arg1) == LSHIFT_EXPR
11162 && integer_onep (TREE_OPERAND (arg1, 0)))
11163 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11164 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11165 TREE_OPERAND (arg1, 1)),
11166 build_zero_cst (TREE_TYPE (arg0)));
11168 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11169 otherwise Y might be >= # of bits in X's type and thus e.g.
11170 (unsigned char) (1 << Y) for Y 15 might be 0.
11171 If the cast is widening, then 1 << Y should have unsigned type,
11172 otherwise if Y is number of bits in the signed shift type minus 1,
11173 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11174 31 might be 0xffffffff80000000. */
11175 if ((code == LT_EXPR || code == GE_EXPR)
11176 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11177 && CONVERT_EXPR_P (arg1)
11178 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11179 && (element_precision (TREE_TYPE (arg1))
11180 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11181 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11182 || (element_precision (TREE_TYPE (arg1))
11183 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11184 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11186 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11187 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11188 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11189 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11190 build_zero_cst (TREE_TYPE (arg0)));
11193 return NULL_TREE;
11195 case UNORDERED_EXPR:
11196 case ORDERED_EXPR:
11197 case UNLT_EXPR:
11198 case UNLE_EXPR:
11199 case UNGT_EXPR:
11200 case UNGE_EXPR:
11201 case UNEQ_EXPR:
11202 case LTGT_EXPR:
11203 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11205 tree targ0 = strip_float_extensions (arg0);
11206 tree targ1 = strip_float_extensions (arg1);
11207 tree newtype = TREE_TYPE (targ0);
11209 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11210 newtype = TREE_TYPE (targ1);
11212 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11213 return fold_build2_loc (loc, code, type,
11214 fold_convert_loc (loc, newtype, targ0),
11215 fold_convert_loc (loc, newtype, targ1));
11218 return NULL_TREE;
11220 case COMPOUND_EXPR:
11221 /* When pedantic, a compound expression can be neither an lvalue
11222 nor an integer constant expression. */
11223 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11224 return NULL_TREE;
11225 /* Don't let (0, 0) be null pointer constant. */
11226 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11227 : fold_convert_loc (loc, type, arg1);
11228 return pedantic_non_lvalue_loc (loc, tem);
11230 case ASSERT_EXPR:
11231 /* An ASSERT_EXPR should never be passed to fold_binary. */
11232 gcc_unreachable ();
11234 default:
11235 return NULL_TREE;
11236 } /* switch (code) */
11239 /* Used by contains_label_[p1]. */
11241 struct contains_label_data
11243 hash_set<tree> *pset;
11244 bool inside_switch_p;
11247 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11248 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
11249 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
11251 static tree
11252 contains_label_1 (tree *tp, int *walk_subtrees, void *data)
11254 contains_label_data *d = (contains_label_data *) data;
11255 switch (TREE_CODE (*tp))
11257 case LABEL_EXPR:
11258 return *tp;
11260 case CASE_LABEL_EXPR:
11261 if (!d->inside_switch_p)
11262 return *tp;
11263 return NULL_TREE;
11265 case SWITCH_EXPR:
11266 if (!d->inside_switch_p)
11268 if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
11269 return *tp;
11270 d->inside_switch_p = true;
11271 if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
11272 return *tp;
11273 d->inside_switch_p = false;
11274 *walk_subtrees = 0;
11276 return NULL_TREE;
11278 case GOTO_EXPR:
11279 *walk_subtrees = 0;
11280 return NULL_TREE;
11282 default:
11283 return NULL_TREE;
11287 /* Return whether the sub-tree ST contains a label which is accessible from
11288 outside the sub-tree. */
11290 static bool
11291 contains_label_p (tree st)
11293 hash_set<tree> pset;
11294 contains_label_data data = { &pset, false };
11295 return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
11298 /* Fold a ternary expression of code CODE and type TYPE with operands
11299 OP0, OP1, and OP2. Return the folded expression if folding is
11300 successful. Otherwise, return NULL_TREE. */
11302 tree
11303 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11304 tree op0, tree op1, tree op2)
11306 tree tem;
11307 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11308 enum tree_code_class kind = TREE_CODE_CLASS (code);
11310 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11311 && TREE_CODE_LENGTH (code) == 3);
11313 /* If this is a commutative operation, and OP0 is a constant, move it
11314 to OP1 to reduce the number of tests below. */
11315 if (commutative_ternary_tree_code (code)
11316 && tree_swap_operands_p (op0, op1))
11317 return fold_build3_loc (loc, code, type, op1, op0, op2);
11319 tem = generic_simplify (loc, code, type, op0, op1, op2);
11320 if (tem)
11321 return tem;
11323 /* Strip any conversions that don't change the mode. This is safe
11324 for every expression, except for a comparison expression because
11325 its signedness is derived from its operands. So, in the latter
11326 case, only strip conversions that don't change the signedness.
11328 Note that this is done as an internal manipulation within the
11329 constant folder, in order to find the simplest representation of
11330 the arguments so that their form can be studied. In any cases,
11331 the appropriate type conversions should be put back in the tree
11332 that will get out of the constant folder. */
11333 if (op0)
11335 arg0 = op0;
11336 STRIP_NOPS (arg0);
11339 if (op1)
11341 arg1 = op1;
11342 STRIP_NOPS (arg1);
11345 if (op2)
11347 arg2 = op2;
11348 STRIP_NOPS (arg2);
11351 switch (code)
11353 case COMPONENT_REF:
11354 if (TREE_CODE (arg0) == CONSTRUCTOR
11355 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11357 unsigned HOST_WIDE_INT idx;
11358 tree field, value;
11359 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11360 if (field == arg1)
11361 return value;
11363 return NULL_TREE;
11365 case COND_EXPR:
11366 case VEC_COND_EXPR:
11367 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11368 so all simple results must be passed through pedantic_non_lvalue. */
11369 if (TREE_CODE (arg0) == INTEGER_CST)
11371 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11372 tem = integer_zerop (arg0) ? op2 : op1;
11373 /* Only optimize constant conditions when the selected branch
11374 has the same type as the COND_EXPR. This avoids optimizing
11375 away "c ? x : throw", where the throw has a void type.
11376 Avoid throwing away that operand which contains label. */
11377 if ((!TREE_SIDE_EFFECTS (unused_op)
11378 || !contains_label_p (unused_op))
11379 && (! VOID_TYPE_P (TREE_TYPE (tem))
11380 || VOID_TYPE_P (type)))
11381 return pedantic_non_lvalue_loc (loc, tem);
11382 return NULL_TREE;
11384 else if (TREE_CODE (arg0) == VECTOR_CST)
11386 unsigned HOST_WIDE_INT nelts;
11387 if ((TREE_CODE (arg1) == VECTOR_CST
11388 || TREE_CODE (arg1) == CONSTRUCTOR)
11389 && (TREE_CODE (arg2) == VECTOR_CST
11390 || TREE_CODE (arg2) == CONSTRUCTOR)
11391 && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
11393 vec_perm_builder sel (nelts, nelts, 1);
11394 for (unsigned int i = 0; i < nelts; i++)
11396 tree val = VECTOR_CST_ELT (arg0, i);
11397 if (integer_all_onesp (val))
11398 sel.quick_push (i);
11399 else if (integer_zerop (val))
11400 sel.quick_push (nelts + i);
11401 else /* Currently unreachable. */
11402 return NULL_TREE;
11404 vec_perm_indices indices (sel, 2, nelts);
11405 tree t = fold_vec_perm (type, arg1, arg2, indices);
11406 if (t != NULL_TREE)
11407 return t;
11411 /* If we have A op B ? A : C, we may be able to convert this to a
11412 simpler expression, depending on the operation and the values
11413 of B and C. Signed zeros prevent all of these transformations,
11414 for reasons given above each one.
11416 Also try swapping the arguments and inverting the conditional. */
11417 if (COMPARISON_CLASS_P (arg0)
11418 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
11419 && !HONOR_SIGNED_ZEROS (element_mode (op1)))
11421 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11422 if (tem)
11423 return tem;
11426 if (COMPARISON_CLASS_P (arg0)
11427 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11428 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11430 location_t loc0 = expr_location_or (arg0, loc);
11431 tem = fold_invert_truthvalue (loc0, arg0);
11432 if (tem && COMPARISON_CLASS_P (tem))
11434 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11435 if (tem)
11436 return tem;
11440 /* If the second operand is simpler than the third, swap them
11441 since that produces better jump optimization results. */
11442 if (truth_value_p (TREE_CODE (arg0))
11443 && tree_swap_operands_p (op1, op2))
11445 location_t loc0 = expr_location_or (arg0, loc);
11446 /* See if this can be inverted. If it can't, possibly because
11447 it was a floating-point inequality comparison, don't do
11448 anything. */
11449 tem = fold_invert_truthvalue (loc0, arg0);
11450 if (tem)
11451 return fold_build3_loc (loc, code, type, tem, op2, op1);
11454 /* Convert A ? 1 : 0 to simply A. */
11455 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11456 : (integer_onep (op1)
11457 && !VECTOR_TYPE_P (type)))
11458 && integer_zerop (op2)
11459 /* If we try to convert OP0 to our type, the
11460 call to fold will try to move the conversion inside
11461 a COND, which will recurse. In that case, the COND_EXPR
11462 is probably the best choice, so leave it alone. */
11463 && type == TREE_TYPE (arg0))
11464 return pedantic_non_lvalue_loc (loc, arg0);
11466 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11467 over COND_EXPR in cases such as floating point comparisons. */
11468 if (integer_zerop (op1)
11469 && code == COND_EXPR
11470 && integer_onep (op2)
11471 && !VECTOR_TYPE_P (type)
11472 && truth_value_p (TREE_CODE (arg0)))
11473 return pedantic_non_lvalue_loc (loc,
11474 fold_convert_loc (loc, type,
11475 invert_truthvalue_loc (loc,
11476 arg0)));
11478 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11479 if (TREE_CODE (arg0) == LT_EXPR
11480 && integer_zerop (TREE_OPERAND (arg0, 1))
11481 && integer_zerop (op2)
11482 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11484 /* sign_bit_p looks through both zero and sign extensions,
11485 but for this optimization only sign extensions are
11486 usable. */
11487 tree tem2 = TREE_OPERAND (arg0, 0);
11488 while (tem != tem2)
11490 if (TREE_CODE (tem2) != NOP_EXPR
11491 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11493 tem = NULL_TREE;
11494 break;
11496 tem2 = TREE_OPERAND (tem2, 0);
11498 /* sign_bit_p only checks ARG1 bits within A's precision.
11499 If <sign bit of A> has wider type than A, bits outside
11500 of A's precision in <sign bit of A> need to be checked.
11501 If they are all 0, this optimization needs to be done
11502 in unsigned A's type, if they are all 1 in signed A's type,
11503 otherwise this can't be done. */
11504 if (tem
11505 && TYPE_PRECISION (TREE_TYPE (tem))
11506 < TYPE_PRECISION (TREE_TYPE (arg1))
11507 && TYPE_PRECISION (TREE_TYPE (tem))
11508 < TYPE_PRECISION (type))
11510 int inner_width, outer_width;
11511 tree tem_type;
11513 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11514 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11515 if (outer_width > TYPE_PRECISION (type))
11516 outer_width = TYPE_PRECISION (type);
11518 wide_int mask = wi::shifted_mask
11519 (inner_width, outer_width - inner_width, false,
11520 TYPE_PRECISION (TREE_TYPE (arg1)));
11522 wide_int common = mask & wi::to_wide (arg1);
11523 if (common == mask)
11525 tem_type = signed_type_for (TREE_TYPE (tem));
11526 tem = fold_convert_loc (loc, tem_type, tem);
11528 else if (common == 0)
11530 tem_type = unsigned_type_for (TREE_TYPE (tem));
11531 tem = fold_convert_loc (loc, tem_type, tem);
11533 else
11534 tem = NULL;
11537 if (tem)
11538 return
11539 fold_convert_loc (loc, type,
11540 fold_build2_loc (loc, BIT_AND_EXPR,
11541 TREE_TYPE (tem), tem,
11542 fold_convert_loc (loc,
11543 TREE_TYPE (tem),
11544 arg1)));
11547 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11548 already handled above. */
11549 if (TREE_CODE (arg0) == BIT_AND_EXPR
11550 && integer_onep (TREE_OPERAND (arg0, 1))
11551 && integer_zerop (op2)
11552 && integer_pow2p (arg1))
11554 tree tem = TREE_OPERAND (arg0, 0);
11555 STRIP_NOPS (tem);
11556 if (TREE_CODE (tem) == RSHIFT_EXPR
11557 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11558 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11559 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11560 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11561 fold_convert_loc (loc, type,
11562 TREE_OPERAND (tem, 0)),
11563 op1);
11566 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11567 is probably obsolete because the first operand should be a
11568 truth value (that's why we have the two cases above), but let's
11569 leave it in until we can confirm this for all front-ends. */
11570 if (integer_zerop (op2)
11571 && TREE_CODE (arg0) == NE_EXPR
11572 && integer_zerop (TREE_OPERAND (arg0, 1))
11573 && integer_pow2p (arg1)
11574 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11575 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11576 arg1, OEP_ONLY_CONST))
11577 return pedantic_non_lvalue_loc (loc,
11578 fold_convert_loc (loc, type,
11579 TREE_OPERAND (arg0, 0)));
11581 /* Disable the transformations below for vectors, since
11582 fold_binary_op_with_conditional_arg may undo them immediately,
11583 yielding an infinite loop. */
11584 if (code == VEC_COND_EXPR)
11585 return NULL_TREE;
11587 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11588 if (integer_zerop (op2)
11589 && truth_value_p (TREE_CODE (arg0))
11590 && truth_value_p (TREE_CODE (arg1))
11591 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11592 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11593 : TRUTH_ANDIF_EXPR,
11594 type, fold_convert_loc (loc, type, arg0), op1);
11596 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11597 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11598 && truth_value_p (TREE_CODE (arg0))
11599 && truth_value_p (TREE_CODE (arg1))
11600 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11602 location_t loc0 = expr_location_or (arg0, loc);
11603 /* Only perform transformation if ARG0 is easily inverted. */
11604 tem = fold_invert_truthvalue (loc0, arg0);
11605 if (tem)
11606 return fold_build2_loc (loc, code == VEC_COND_EXPR
11607 ? BIT_IOR_EXPR
11608 : TRUTH_ORIF_EXPR,
11609 type, fold_convert_loc (loc, type, tem),
11610 op1);
11613 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11614 if (integer_zerop (arg1)
11615 && truth_value_p (TREE_CODE (arg0))
11616 && truth_value_p (TREE_CODE (op2))
11617 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11619 location_t loc0 = expr_location_or (arg0, loc);
11620 /* Only perform transformation if ARG0 is easily inverted. */
11621 tem = fold_invert_truthvalue (loc0, arg0);
11622 if (tem)
11623 return fold_build2_loc (loc, code == VEC_COND_EXPR
11624 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11625 type, fold_convert_loc (loc, type, tem),
11626 op2);
11629 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11630 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11631 && truth_value_p (TREE_CODE (arg0))
11632 && truth_value_p (TREE_CODE (op2))
11633 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11634 return fold_build2_loc (loc, code == VEC_COND_EXPR
11635 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11636 type, fold_convert_loc (loc, type, arg0), op2);
11638 return NULL_TREE;
11640 case CALL_EXPR:
11641 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11642 of fold_ternary on them. */
11643 gcc_unreachable ();
11645 case BIT_FIELD_REF:
11646 if (TREE_CODE (arg0) == VECTOR_CST
11647 && (type == TREE_TYPE (TREE_TYPE (arg0))
11648 || (VECTOR_TYPE_P (type)
11649 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
11650 && tree_fits_uhwi_p (op1)
11651 && tree_fits_uhwi_p (op2))
11653 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11654 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11655 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11656 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11658 if (n != 0
11659 && (idx % width) == 0
11660 && (n % width) == 0
11661 && known_le ((idx + n) / width,
11662 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
11664 idx = idx / width;
11665 n = n / width;
11667 if (TREE_CODE (arg0) == VECTOR_CST)
11669 if (n == 1)
11671 tem = VECTOR_CST_ELT (arg0, idx);
11672 if (VECTOR_TYPE_P (type))
11673 tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
11674 return tem;
11677 tree_vector_builder vals (type, n, 1);
11678 for (unsigned i = 0; i < n; ++i)
11679 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
11680 return vals.build ();
11685 /* On constants we can use native encode/interpret to constant
11686 fold (nearly) all BIT_FIELD_REFs. */
11687 if (CONSTANT_CLASS_P (arg0)
11688 && can_native_interpret_type_p (type)
11689 && BITS_PER_UNIT == 8
11690 && tree_fits_uhwi_p (op1)
11691 && tree_fits_uhwi_p (op2))
11693 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11694 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11695 /* Limit us to a reasonable amount of work. To relax the
11696 other limitations we need bit-shifting of the buffer
11697 and rounding up the size. */
11698 if (bitpos % BITS_PER_UNIT == 0
11699 && bitsize % BITS_PER_UNIT == 0
11700 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11702 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11703 unsigned HOST_WIDE_INT len
11704 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11705 bitpos / BITS_PER_UNIT);
11706 if (len > 0
11707 && len * BITS_PER_UNIT >= bitsize)
11709 tree v = native_interpret_expr (type, b,
11710 bitsize / BITS_PER_UNIT);
11711 if (v)
11712 return v;
11717 return NULL_TREE;
11719 case VEC_PERM_EXPR:
11720 if (TREE_CODE (arg2) == VECTOR_CST)
11722 /* Build a vector of integers from the tree mask. */
11723 vec_perm_builder builder;
11724 if (!tree_to_vec_perm_builder (&builder, arg2))
11725 return NULL_TREE;
11727 /* Create a vec_perm_indices for the integer vector. */
11728 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
11729 bool single_arg = (op0 == op1);
11730 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
11732 /* Check for cases that fold to OP0 or OP1 in their original
11733 element order. */
11734 if (sel.series_p (0, 1, 0, 1))
11735 return op0;
11736 if (sel.series_p (0, 1, nelts, 1))
11737 return op1;
11739 if (!single_arg)
11741 if (sel.all_from_input_p (0))
11742 op1 = op0;
11743 else if (sel.all_from_input_p (1))
11745 op0 = op1;
11746 sel.rotate_inputs (1);
11750 if ((TREE_CODE (op0) == VECTOR_CST
11751 || TREE_CODE (op0) == CONSTRUCTOR)
11752 && (TREE_CODE (op1) == VECTOR_CST
11753 || TREE_CODE (op1) == CONSTRUCTOR))
11755 tree t = fold_vec_perm (type, op0, op1, sel);
11756 if (t != NULL_TREE)
11757 return t;
11760 bool changed = (op0 == op1 && !single_arg);
11762 /* Generate a canonical form of the selector. */
11763 if (arg2 == op2 && sel.encoding () != builder)
11765 /* Some targets are deficient and fail to expand a single
11766 argument permutation while still allowing an equivalent
11767 2-argument version. */
11768 if (sel.ninputs () == 2
11769 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
11770 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11771 else
11773 vec_perm_indices sel2 (builder, 2, nelts);
11774 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
11775 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel2);
11776 else
11777 /* Not directly supported with either encoding,
11778 so use the preferred form. */
11779 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11781 changed = true;
11784 if (changed)
11785 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11787 return NULL_TREE;
11789 case BIT_INSERT_EXPR:
11790 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11791 if (TREE_CODE (arg0) == INTEGER_CST
11792 && TREE_CODE (arg1) == INTEGER_CST)
11794 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11795 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11796 wide_int tem = (wi::to_wide (arg0)
11797 & wi::shifted_mask (bitpos, bitsize, true,
11798 TYPE_PRECISION (type)));
11799 wide_int tem2
11800 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11801 bitsize), bitpos);
11802 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11804 else if (TREE_CODE (arg0) == VECTOR_CST
11805 && CONSTANT_CLASS_P (arg1)
11806 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11807 TREE_TYPE (arg1)))
11809 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11810 unsigned HOST_WIDE_INT elsize
11811 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11812 if (bitpos % elsize == 0)
11814 unsigned k = bitpos / elsize;
11815 unsigned HOST_WIDE_INT nelts;
11816 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11817 return arg0;
11818 else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
11820 tree_vector_builder elts (type, nelts, 1);
11821 elts.quick_grow (nelts);
11822 for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
11823 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
11824 return elts.build ();
11828 return NULL_TREE;
11830 default:
11831 return NULL_TREE;
11832 } /* switch (code) */
11835 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11836 of an array (or vector). */
11838 tree
11839 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11841 tree index_type = NULL_TREE;
11842 offset_int low_bound = 0;
11844 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11846 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11847 if (domain_type && TYPE_MIN_VALUE (domain_type))
11849 /* Static constructors for variably sized objects makes no sense. */
11850 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11851 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11852 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11856 if (index_type)
11857 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11858 TYPE_SIGN (index_type));
11860 offset_int index = low_bound - 1;
11861 if (index_type)
11862 index = wi::ext (index, TYPE_PRECISION (index_type),
11863 TYPE_SIGN (index_type));
11865 offset_int max_index;
11866 unsigned HOST_WIDE_INT cnt;
11867 tree cfield, cval;
11869 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11871 /* Array constructor might explicitly set index, or specify a range,
11872 or leave index NULL meaning that it is next index after previous
11873 one. */
11874 if (cfield)
11876 if (TREE_CODE (cfield) == INTEGER_CST)
11877 max_index = index = wi::to_offset (cfield);
11878 else
11880 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11881 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11882 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11885 else
11887 index += 1;
11888 if (index_type)
11889 index = wi::ext (index, TYPE_PRECISION (index_type),
11890 TYPE_SIGN (index_type));
11891 max_index = index;
11894 /* Do we have match? */
11895 if (wi::cmpu (access_index, index) >= 0
11896 && wi::cmpu (access_index, max_index) <= 0)
11897 return cval;
11899 return NULL_TREE;
11902 /* Perform constant folding and related simplification of EXPR.
11903 The related simplifications include x*1 => x, x*0 => 0, etc.,
11904 and application of the associative law.
11905 NOP_EXPR conversions may be removed freely (as long as we
11906 are careful not to change the type of the overall expression).
11907 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11908 but we can constant-fold them if they have constant operands. */
11910 #ifdef ENABLE_FOLD_CHECKING
11911 # define fold(x) fold_1 (x)
11912 static tree fold_1 (tree);
11913 static
11914 #endif
11915 tree
11916 fold (tree expr)
11918 const tree t = expr;
11919 enum tree_code code = TREE_CODE (t);
11920 enum tree_code_class kind = TREE_CODE_CLASS (code);
11921 tree tem;
11922 location_t loc = EXPR_LOCATION (expr);
11924 /* Return right away if a constant. */
11925 if (kind == tcc_constant)
11926 return t;
11928 /* CALL_EXPR-like objects with variable numbers of operands are
11929 treated specially. */
11930 if (kind == tcc_vl_exp)
11932 if (code == CALL_EXPR)
11934 tem = fold_call_expr (loc, expr, false);
11935 return tem ? tem : expr;
11937 return expr;
11940 if (IS_EXPR_CODE_CLASS (kind))
11942 tree type = TREE_TYPE (t);
11943 tree op0, op1, op2;
11945 switch (TREE_CODE_LENGTH (code))
11947 case 1:
11948 op0 = TREE_OPERAND (t, 0);
11949 tem = fold_unary_loc (loc, code, type, op0);
11950 return tem ? tem : expr;
11951 case 2:
11952 op0 = TREE_OPERAND (t, 0);
11953 op1 = TREE_OPERAND (t, 1);
11954 tem = fold_binary_loc (loc, code, type, op0, op1);
11955 return tem ? tem : expr;
11956 case 3:
11957 op0 = TREE_OPERAND (t, 0);
11958 op1 = TREE_OPERAND (t, 1);
11959 op2 = TREE_OPERAND (t, 2);
11960 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11961 return tem ? tem : expr;
11962 default:
11963 break;
11967 switch (code)
11969 case ARRAY_REF:
11971 tree op0 = TREE_OPERAND (t, 0);
11972 tree op1 = TREE_OPERAND (t, 1);
11974 if (TREE_CODE (op1) == INTEGER_CST
11975 && TREE_CODE (op0) == CONSTRUCTOR
11976 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11978 tree val = get_array_ctor_element_at_index (op0,
11979 wi::to_offset (op1));
11980 if (val)
11981 return val;
11984 return t;
11987 /* Return a VECTOR_CST if possible. */
11988 case CONSTRUCTOR:
11990 tree type = TREE_TYPE (t);
11991 if (TREE_CODE (type) != VECTOR_TYPE)
11992 return t;
11994 unsigned i;
11995 tree val;
11996 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
11997 if (! CONSTANT_CLASS_P (val))
11998 return t;
12000 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12003 case CONST_DECL:
12004 return fold (DECL_INITIAL (t));
12006 default:
12007 return t;
12008 } /* switch (code) */
12011 #ifdef ENABLE_FOLD_CHECKING
12012 #undef fold
12014 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12015 hash_table<nofree_ptr_hash<const tree_node> > *);
12016 static void fold_check_failed (const_tree, const_tree);
12017 void print_fold_checksum (const_tree);
12019 /* When --enable-checking=fold, compute a digest of expr before
12020 and after actual fold call to see if fold did not accidentally
12021 change original expr. */
12023 tree
12024 fold (tree expr)
12026 tree ret;
12027 struct md5_ctx ctx;
12028 unsigned char checksum_before[16], checksum_after[16];
12029 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12031 md5_init_ctx (&ctx);
12032 fold_checksum_tree (expr, &ctx, &ht);
12033 md5_finish_ctx (&ctx, checksum_before);
12034 ht.empty ();
12036 ret = fold_1 (expr);
12038 md5_init_ctx (&ctx);
12039 fold_checksum_tree (expr, &ctx, &ht);
12040 md5_finish_ctx (&ctx, checksum_after);
12042 if (memcmp (checksum_before, checksum_after, 16))
12043 fold_check_failed (expr, ret);
12045 return ret;
12048 void
12049 print_fold_checksum (const_tree expr)
12051 struct md5_ctx ctx;
12052 unsigned char checksum[16], cnt;
12053 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12055 md5_init_ctx (&ctx);
12056 fold_checksum_tree (expr, &ctx, &ht);
12057 md5_finish_ctx (&ctx, checksum);
12058 for (cnt = 0; cnt < 16; ++cnt)
12059 fprintf (stderr, "%02x", checksum[cnt]);
12060 putc ('\n', stderr);
12063 static void
12064 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12066 internal_error ("fold check: original tree changed by fold");
12069 static void
12070 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12071 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12073 const tree_node **slot;
12074 enum tree_code code;
12075 union tree_node buf;
12076 int i, len;
12078 recursive_label:
12079 if (expr == NULL)
12080 return;
12081 slot = ht->find_slot (expr, INSERT);
12082 if (*slot != NULL)
12083 return;
12084 *slot = expr;
12085 code = TREE_CODE (expr);
12086 if (TREE_CODE_CLASS (code) == tcc_declaration
12087 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12089 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12090 memcpy ((char *) &buf, expr, tree_size (expr));
12091 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12092 buf.decl_with_vis.symtab_node = NULL;
12093 expr = (tree) &buf;
12095 else if (TREE_CODE_CLASS (code) == tcc_type
12096 && (TYPE_POINTER_TO (expr)
12097 || TYPE_REFERENCE_TO (expr)
12098 || TYPE_CACHED_VALUES_P (expr)
12099 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12100 || TYPE_NEXT_VARIANT (expr)
12101 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12103 /* Allow these fields to be modified. */
12104 tree tmp;
12105 memcpy ((char *) &buf, expr, tree_size (expr));
12106 expr = tmp = (tree) &buf;
12107 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12108 TYPE_POINTER_TO (tmp) = NULL;
12109 TYPE_REFERENCE_TO (tmp) = NULL;
12110 TYPE_NEXT_VARIANT (tmp) = NULL;
12111 TYPE_ALIAS_SET (tmp) = -1;
12112 if (TYPE_CACHED_VALUES_P (tmp))
12114 TYPE_CACHED_VALUES_P (tmp) = 0;
12115 TYPE_CACHED_VALUES (tmp) = NULL;
12118 md5_process_bytes (expr, tree_size (expr), ctx);
12119 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12120 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12121 if (TREE_CODE_CLASS (code) != tcc_type
12122 && TREE_CODE_CLASS (code) != tcc_declaration
12123 && code != TREE_LIST
12124 && code != SSA_NAME
12125 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12126 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12127 switch (TREE_CODE_CLASS (code))
12129 case tcc_constant:
12130 switch (code)
12132 case STRING_CST:
12133 md5_process_bytes (TREE_STRING_POINTER (expr),
12134 TREE_STRING_LENGTH (expr), ctx);
12135 break;
12136 case COMPLEX_CST:
12137 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12138 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12139 break;
12140 case VECTOR_CST:
12141 len = vector_cst_encoded_nelts (expr);
12142 for (i = 0; i < len; ++i)
12143 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
12144 break;
12145 default:
12146 break;
12148 break;
12149 case tcc_exceptional:
12150 switch (code)
12152 case TREE_LIST:
12153 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12154 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12155 expr = TREE_CHAIN (expr);
12156 goto recursive_label;
12157 break;
12158 case TREE_VEC:
12159 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12160 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12161 break;
12162 default:
12163 break;
12165 break;
12166 case tcc_expression:
12167 case tcc_reference:
12168 case tcc_comparison:
12169 case tcc_unary:
12170 case tcc_binary:
12171 case tcc_statement:
12172 case tcc_vl_exp:
12173 len = TREE_OPERAND_LENGTH (expr);
12174 for (i = 0; i < len; ++i)
12175 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12176 break;
12177 case tcc_declaration:
12178 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12179 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12180 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12182 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12183 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12184 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12185 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12186 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12189 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12191 if (TREE_CODE (expr) == FUNCTION_DECL)
12193 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12194 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12196 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12198 break;
12199 case tcc_type:
12200 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12201 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12202 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12203 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12204 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12205 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12206 if (INTEGRAL_TYPE_P (expr)
12207 || SCALAR_FLOAT_TYPE_P (expr))
12209 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12210 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12212 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12213 if (TREE_CODE (expr) == RECORD_TYPE
12214 || TREE_CODE (expr) == UNION_TYPE
12215 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12216 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12217 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12218 break;
12219 default:
12220 break;
12224 /* Helper function for outputting the checksum of a tree T. When
12225 debugging with gdb, you can "define mynext" to be "next" followed
12226 by "call debug_fold_checksum (op0)", then just trace down till the
12227 outputs differ. */
12229 DEBUG_FUNCTION void
12230 debug_fold_checksum (const_tree t)
12232 int i;
12233 unsigned char checksum[16];
12234 struct md5_ctx ctx;
12235 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12237 md5_init_ctx (&ctx);
12238 fold_checksum_tree (t, &ctx, &ht);
12239 md5_finish_ctx (&ctx, checksum);
12240 ht.empty ();
12242 for (i = 0; i < 16; i++)
12243 fprintf (stderr, "%d ", checksum[i]);
12245 fprintf (stderr, "\n");
12248 #endif
12250 /* Fold a unary tree expression with code CODE of type TYPE with an
12251 operand OP0. LOC is the location of the resulting expression.
12252 Return a folded expression if successful. Otherwise, return a tree
12253 expression with code CODE of type TYPE with an operand OP0. */
12255 tree
12256 fold_build1_loc (location_t loc,
12257 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12259 tree tem;
12260 #ifdef ENABLE_FOLD_CHECKING
12261 unsigned char checksum_before[16], checksum_after[16];
12262 struct md5_ctx ctx;
12263 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12265 md5_init_ctx (&ctx);
12266 fold_checksum_tree (op0, &ctx, &ht);
12267 md5_finish_ctx (&ctx, checksum_before);
12268 ht.empty ();
12269 #endif
12271 tem = fold_unary_loc (loc, code, type, op0);
12272 if (!tem)
12273 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12275 #ifdef ENABLE_FOLD_CHECKING
12276 md5_init_ctx (&ctx);
12277 fold_checksum_tree (op0, &ctx, &ht);
12278 md5_finish_ctx (&ctx, checksum_after);
12280 if (memcmp (checksum_before, checksum_after, 16))
12281 fold_check_failed (op0, tem);
12282 #endif
12283 return tem;
12286 /* Fold a binary tree expression with code CODE of type TYPE with
12287 operands OP0 and OP1. LOC is the location of the resulting
12288 expression. Return a folded expression if successful. Otherwise,
12289 return a tree expression with code CODE of type TYPE with operands
12290 OP0 and OP1. */
12292 tree
12293 fold_build2_loc (location_t loc,
12294 enum tree_code code, tree type, tree op0, tree op1
12295 MEM_STAT_DECL)
12297 tree tem;
12298 #ifdef ENABLE_FOLD_CHECKING
12299 unsigned char checksum_before_op0[16],
12300 checksum_before_op1[16],
12301 checksum_after_op0[16],
12302 checksum_after_op1[16];
12303 struct md5_ctx ctx;
12304 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12306 md5_init_ctx (&ctx);
12307 fold_checksum_tree (op0, &ctx, &ht);
12308 md5_finish_ctx (&ctx, checksum_before_op0);
12309 ht.empty ();
12311 md5_init_ctx (&ctx);
12312 fold_checksum_tree (op1, &ctx, &ht);
12313 md5_finish_ctx (&ctx, checksum_before_op1);
12314 ht.empty ();
12315 #endif
12317 tem = fold_binary_loc (loc, code, type, op0, op1);
12318 if (!tem)
12319 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12321 #ifdef ENABLE_FOLD_CHECKING
12322 md5_init_ctx (&ctx);
12323 fold_checksum_tree (op0, &ctx, &ht);
12324 md5_finish_ctx (&ctx, checksum_after_op0);
12325 ht.empty ();
12327 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12328 fold_check_failed (op0, tem);
12330 md5_init_ctx (&ctx);
12331 fold_checksum_tree (op1, &ctx, &ht);
12332 md5_finish_ctx (&ctx, checksum_after_op1);
12334 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12335 fold_check_failed (op1, tem);
12336 #endif
12337 return tem;
12340 /* Fold a ternary tree expression with code CODE of type TYPE with
12341 operands OP0, OP1, and OP2. Return a folded expression if
12342 successful. Otherwise, return a tree expression with code CODE of
12343 type TYPE with operands OP0, OP1, and OP2. */
12345 tree
12346 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12347 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12349 tree tem;
12350 #ifdef ENABLE_FOLD_CHECKING
12351 unsigned char checksum_before_op0[16],
12352 checksum_before_op1[16],
12353 checksum_before_op2[16],
12354 checksum_after_op0[16],
12355 checksum_after_op1[16],
12356 checksum_after_op2[16];
12357 struct md5_ctx ctx;
12358 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12360 md5_init_ctx (&ctx);
12361 fold_checksum_tree (op0, &ctx, &ht);
12362 md5_finish_ctx (&ctx, checksum_before_op0);
12363 ht.empty ();
12365 md5_init_ctx (&ctx);
12366 fold_checksum_tree (op1, &ctx, &ht);
12367 md5_finish_ctx (&ctx, checksum_before_op1);
12368 ht.empty ();
12370 md5_init_ctx (&ctx);
12371 fold_checksum_tree (op2, &ctx, &ht);
12372 md5_finish_ctx (&ctx, checksum_before_op2);
12373 ht.empty ();
12374 #endif
12376 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12377 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12378 if (!tem)
12379 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12381 #ifdef ENABLE_FOLD_CHECKING
12382 md5_init_ctx (&ctx);
12383 fold_checksum_tree (op0, &ctx, &ht);
12384 md5_finish_ctx (&ctx, checksum_after_op0);
12385 ht.empty ();
12387 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12388 fold_check_failed (op0, tem);
12390 md5_init_ctx (&ctx);
12391 fold_checksum_tree (op1, &ctx, &ht);
12392 md5_finish_ctx (&ctx, checksum_after_op1);
12393 ht.empty ();
12395 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12396 fold_check_failed (op1, tem);
12398 md5_init_ctx (&ctx);
12399 fold_checksum_tree (op2, &ctx, &ht);
12400 md5_finish_ctx (&ctx, checksum_after_op2);
12402 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12403 fold_check_failed (op2, tem);
12404 #endif
12405 return tem;
12408 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12409 arguments in ARGARRAY, and a null static chain.
12410 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12411 of type TYPE from the given operands as constructed by build_call_array. */
12413 tree
12414 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12415 int nargs, tree *argarray)
12417 tree tem;
12418 #ifdef ENABLE_FOLD_CHECKING
12419 unsigned char checksum_before_fn[16],
12420 checksum_before_arglist[16],
12421 checksum_after_fn[16],
12422 checksum_after_arglist[16];
12423 struct md5_ctx ctx;
12424 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12425 int i;
12427 md5_init_ctx (&ctx);
12428 fold_checksum_tree (fn, &ctx, &ht);
12429 md5_finish_ctx (&ctx, checksum_before_fn);
12430 ht.empty ();
12432 md5_init_ctx (&ctx);
12433 for (i = 0; i < nargs; i++)
12434 fold_checksum_tree (argarray[i], &ctx, &ht);
12435 md5_finish_ctx (&ctx, checksum_before_arglist);
12436 ht.empty ();
12437 #endif
12439 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12440 if (!tem)
12441 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12443 #ifdef ENABLE_FOLD_CHECKING
12444 md5_init_ctx (&ctx);
12445 fold_checksum_tree (fn, &ctx, &ht);
12446 md5_finish_ctx (&ctx, checksum_after_fn);
12447 ht.empty ();
12449 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12450 fold_check_failed (fn, tem);
12452 md5_init_ctx (&ctx);
12453 for (i = 0; i < nargs; i++)
12454 fold_checksum_tree (argarray[i], &ctx, &ht);
12455 md5_finish_ctx (&ctx, checksum_after_arglist);
12457 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12458 fold_check_failed (NULL_TREE, tem);
12459 #endif
12460 return tem;
12463 /* Perform constant folding and related simplification of initializer
12464 expression EXPR. These behave identically to "fold_buildN" but ignore
12465 potential run-time traps and exceptions that fold must preserve. */
12467 #define START_FOLD_INIT \
12468 int saved_signaling_nans = flag_signaling_nans;\
12469 int saved_trapping_math = flag_trapping_math;\
12470 int saved_rounding_math = flag_rounding_math;\
12471 int saved_trapv = flag_trapv;\
12472 int saved_folding_initializer = folding_initializer;\
12473 flag_signaling_nans = 0;\
12474 flag_trapping_math = 0;\
12475 flag_rounding_math = 0;\
12476 flag_trapv = 0;\
12477 folding_initializer = 1;
12479 #define END_FOLD_INIT \
12480 flag_signaling_nans = saved_signaling_nans;\
12481 flag_trapping_math = saved_trapping_math;\
12482 flag_rounding_math = saved_rounding_math;\
12483 flag_trapv = saved_trapv;\
12484 folding_initializer = saved_folding_initializer;
12486 tree
12487 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12488 tree type, tree op)
12490 tree result;
12491 START_FOLD_INIT;
12493 result = fold_build1_loc (loc, code, type, op);
12495 END_FOLD_INIT;
12496 return result;
12499 tree
12500 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12501 tree type, tree op0, tree op1)
12503 tree result;
12504 START_FOLD_INIT;
12506 result = fold_build2_loc (loc, code, type, op0, op1);
12508 END_FOLD_INIT;
12509 return result;
12512 tree
12513 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12514 int nargs, tree *argarray)
12516 tree result;
12517 START_FOLD_INIT;
12519 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12521 END_FOLD_INIT;
12522 return result;
12525 #undef START_FOLD_INIT
12526 #undef END_FOLD_INIT
12528 /* Determine if first argument is a multiple of second argument. Return 0 if
12529 it is not, or we cannot easily determined it to be.
12531 An example of the sort of thing we care about (at this point; this routine
12532 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12533 fold cases do now) is discovering that
12535 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12537 is a multiple of
12539 SAVE_EXPR (J * 8)
12541 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12543 This code also handles discovering that
12545 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12547 is a multiple of 8 so we don't have to worry about dealing with a
12548 possible remainder.
12550 Note that we *look* inside a SAVE_EXPR only to determine how it was
12551 calculated; it is not safe for fold to do much of anything else with the
12552 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12553 at run time. For example, the latter example above *cannot* be implemented
12554 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12555 evaluation time of the original SAVE_EXPR is not necessarily the same at
12556 the time the new expression is evaluated. The only optimization of this
12557 sort that would be valid is changing
12559 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12561 divided by 8 to
12563 SAVE_EXPR (I) * SAVE_EXPR (J)
12565 (where the same SAVE_EXPR (J) is used in the original and the
12566 transformed version). */
12569 multiple_of_p (tree type, const_tree top, const_tree bottom)
12571 gimple *stmt;
12572 tree t1, op1, op2;
12574 if (operand_equal_p (top, bottom, 0))
12575 return 1;
12577 if (TREE_CODE (type) != INTEGER_TYPE)
12578 return 0;
12580 switch (TREE_CODE (top))
12582 case BIT_AND_EXPR:
12583 /* Bitwise and provides a power of two multiple. If the mask is
12584 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12585 if (!integer_pow2p (bottom))
12586 return 0;
12587 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12588 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12590 case MULT_EXPR:
12591 if (TREE_CODE (bottom) == INTEGER_CST)
12593 op1 = TREE_OPERAND (top, 0);
12594 op2 = TREE_OPERAND (top, 1);
12595 if (TREE_CODE (op1) == INTEGER_CST)
12596 std::swap (op1, op2);
12597 if (TREE_CODE (op2) == INTEGER_CST)
12599 if (multiple_of_p (type, op2, bottom))
12600 return 1;
12601 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
12602 if (multiple_of_p (type, bottom, op2))
12604 widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
12605 wi::to_widest (op2));
12606 if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
12608 op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
12609 return multiple_of_p (type, op1, op2);
12612 return multiple_of_p (type, op1, bottom);
12615 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12616 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12618 case MINUS_EXPR:
12619 /* It is impossible to prove if op0 - op1 is multiple of bottom
12620 precisely, so be conservative here checking if both op0 and op1
12621 are multiple of bottom. Note we check the second operand first
12622 since it's usually simpler. */
12623 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12624 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12626 case PLUS_EXPR:
12627 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12628 as op0 - 3 if the expression has unsigned type. For example,
12629 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12630 op1 = TREE_OPERAND (top, 1);
12631 if (TYPE_UNSIGNED (type)
12632 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12633 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12634 return (multiple_of_p (type, op1, bottom)
12635 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12637 case LSHIFT_EXPR:
12638 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12640 op1 = TREE_OPERAND (top, 1);
12641 /* const_binop may not detect overflow correctly,
12642 so check for it explicitly here. */
12643 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
12644 wi::to_wide (op1))
12645 && (t1 = fold_convert (type,
12646 const_binop (LSHIFT_EXPR, size_one_node,
12647 op1))) != 0
12648 && !TREE_OVERFLOW (t1))
12649 return multiple_of_p (type, t1, bottom);
12651 return 0;
12653 case NOP_EXPR:
12654 /* Can't handle conversions from non-integral or wider integral type. */
12655 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12656 || (TYPE_PRECISION (type)
12657 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12658 return 0;
12660 /* fall through */
12662 case SAVE_EXPR:
12663 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12665 case COND_EXPR:
12666 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12667 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12669 case INTEGER_CST:
12670 if (TREE_CODE (bottom) != INTEGER_CST
12671 || integer_zerop (bottom)
12672 || (TYPE_UNSIGNED (type)
12673 && (tree_int_cst_sgn (top) < 0
12674 || tree_int_cst_sgn (bottom) < 0)))
12675 return 0;
12676 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12677 SIGNED);
12679 case SSA_NAME:
12680 if (TREE_CODE (bottom) == INTEGER_CST
12681 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12682 && gimple_code (stmt) == GIMPLE_ASSIGN)
12684 enum tree_code code = gimple_assign_rhs_code (stmt);
12686 /* Check for special cases to see if top is defined as multiple
12687 of bottom:
12689 top = (X & ~(bottom - 1) ; bottom is power of 2
12693 Y = X % bottom
12694 top = X - Y. */
12695 if (code == BIT_AND_EXPR
12696 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12697 && TREE_CODE (op2) == INTEGER_CST
12698 && integer_pow2p (bottom)
12699 && wi::multiple_of_p (wi::to_widest (op2),
12700 wi::to_widest (bottom), UNSIGNED))
12701 return 1;
12703 op1 = gimple_assign_rhs1 (stmt);
12704 if (code == MINUS_EXPR
12705 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12706 && TREE_CODE (op2) == SSA_NAME
12707 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12708 && gimple_code (stmt) == GIMPLE_ASSIGN
12709 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12710 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12711 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12712 return 1;
12715 /* fall through */
12717 default:
12718 if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
12719 return multiple_p (wi::to_poly_widest (top),
12720 wi::to_poly_widest (bottom));
12722 return 0;
12726 #define tree_expr_nonnegative_warnv_p(X, Y) \
12727 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12729 #define RECURSE(X) \
12730 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12732 /* Return true if CODE or TYPE is known to be non-negative. */
12734 static bool
12735 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12737 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12738 && truth_value_p (code))
12739 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12740 have a signed:1 type (where the value is -1 and 0). */
12741 return true;
12742 return false;
12745 /* Return true if (CODE OP0) is known to be non-negative. If the return
12746 value is based on the assumption that signed overflow is undefined,
12747 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12748 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12750 bool
12751 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12752 bool *strict_overflow_p, int depth)
12754 if (TYPE_UNSIGNED (type))
12755 return true;
12757 switch (code)
12759 case ABS_EXPR:
12760 /* We can't return 1 if flag_wrapv is set because
12761 ABS_EXPR<INT_MIN> = INT_MIN. */
12762 if (!ANY_INTEGRAL_TYPE_P (type))
12763 return true;
12764 if (TYPE_OVERFLOW_UNDEFINED (type))
12766 *strict_overflow_p = true;
12767 return true;
12769 break;
12771 case NON_LVALUE_EXPR:
12772 case FLOAT_EXPR:
12773 case FIX_TRUNC_EXPR:
12774 return RECURSE (op0);
12776 CASE_CONVERT:
12778 tree inner_type = TREE_TYPE (op0);
12779 tree outer_type = type;
12781 if (TREE_CODE (outer_type) == REAL_TYPE)
12783 if (TREE_CODE (inner_type) == REAL_TYPE)
12784 return RECURSE (op0);
12785 if (INTEGRAL_TYPE_P (inner_type))
12787 if (TYPE_UNSIGNED (inner_type))
12788 return true;
12789 return RECURSE (op0);
12792 else if (INTEGRAL_TYPE_P (outer_type))
12794 if (TREE_CODE (inner_type) == REAL_TYPE)
12795 return RECURSE (op0);
12796 if (INTEGRAL_TYPE_P (inner_type))
12797 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12798 && TYPE_UNSIGNED (inner_type);
12801 break;
12803 default:
12804 return tree_simple_nonnegative_warnv_p (code, type);
12807 /* We don't know sign of `t', so be conservative and return false. */
12808 return false;
12811 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12812 value is based on the assumption that signed overflow is undefined,
12813 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12814 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12816 bool
12817 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12818 tree op1, bool *strict_overflow_p,
12819 int depth)
12821 if (TYPE_UNSIGNED (type))
12822 return true;
12824 switch (code)
12826 case POINTER_PLUS_EXPR:
12827 case PLUS_EXPR:
12828 if (FLOAT_TYPE_P (type))
12829 return RECURSE (op0) && RECURSE (op1);
12831 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12832 both unsigned and at least 2 bits shorter than the result. */
12833 if (TREE_CODE (type) == INTEGER_TYPE
12834 && TREE_CODE (op0) == NOP_EXPR
12835 && TREE_CODE (op1) == NOP_EXPR)
12837 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12838 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12839 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12840 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12842 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12843 TYPE_PRECISION (inner2)) + 1;
12844 return prec < TYPE_PRECISION (type);
12847 break;
12849 case MULT_EXPR:
12850 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12852 /* x * x is always non-negative for floating point x
12853 or without overflow. */
12854 if (operand_equal_p (op0, op1, 0)
12855 || (RECURSE (op0) && RECURSE (op1)))
12857 if (ANY_INTEGRAL_TYPE_P (type)
12858 && TYPE_OVERFLOW_UNDEFINED (type))
12859 *strict_overflow_p = true;
12860 return true;
12864 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12865 both unsigned and their total bits is shorter than the result. */
12866 if (TREE_CODE (type) == INTEGER_TYPE
12867 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12868 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12870 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12871 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12872 : TREE_TYPE (op0);
12873 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12874 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12875 : TREE_TYPE (op1);
12877 bool unsigned0 = TYPE_UNSIGNED (inner0);
12878 bool unsigned1 = TYPE_UNSIGNED (inner1);
12880 if (TREE_CODE (op0) == INTEGER_CST)
12881 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12883 if (TREE_CODE (op1) == INTEGER_CST)
12884 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12886 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12887 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12889 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12890 ? tree_int_cst_min_precision (op0, UNSIGNED)
12891 : TYPE_PRECISION (inner0);
12893 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12894 ? tree_int_cst_min_precision (op1, UNSIGNED)
12895 : TYPE_PRECISION (inner1);
12897 return precision0 + precision1 < TYPE_PRECISION (type);
12900 return false;
12902 case BIT_AND_EXPR:
12903 case MAX_EXPR:
12904 return RECURSE (op0) || RECURSE (op1);
12906 case BIT_IOR_EXPR:
12907 case BIT_XOR_EXPR:
12908 case MIN_EXPR:
12909 case RDIV_EXPR:
12910 case TRUNC_DIV_EXPR:
12911 case CEIL_DIV_EXPR:
12912 case FLOOR_DIV_EXPR:
12913 case ROUND_DIV_EXPR:
12914 return RECURSE (op0) && RECURSE (op1);
12916 case TRUNC_MOD_EXPR:
12917 return RECURSE (op0);
12919 case FLOOR_MOD_EXPR:
12920 return RECURSE (op1);
12922 case CEIL_MOD_EXPR:
12923 case ROUND_MOD_EXPR:
12924 default:
12925 return tree_simple_nonnegative_warnv_p (code, type);
12928 /* We don't know sign of `t', so be conservative and return false. */
12929 return false;
12932 /* Return true if T is known to be non-negative. If the return
12933 value is based on the assumption that signed overflow is undefined,
12934 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12935 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12937 bool
12938 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12940 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12941 return true;
12943 switch (TREE_CODE (t))
12945 case INTEGER_CST:
12946 return tree_int_cst_sgn (t) >= 0;
12948 case REAL_CST:
12949 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12951 case FIXED_CST:
12952 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12954 case COND_EXPR:
12955 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12957 case SSA_NAME:
12958 /* Limit the depth of recursion to avoid quadratic behavior.
12959 This is expected to catch almost all occurrences in practice.
12960 If this code misses important cases that unbounded recursion
12961 would not, passes that need this information could be revised
12962 to provide it through dataflow propagation. */
12963 return (!name_registered_for_update_p (t)
12964 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12965 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12966 strict_overflow_p, depth));
12968 default:
12969 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12973 /* Return true if T is known to be non-negative. If the return
12974 value is based on the assumption that signed overflow is undefined,
12975 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12976 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12978 bool
12979 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12980 bool *strict_overflow_p, int depth)
12982 switch (fn)
12984 CASE_CFN_ACOS:
12985 CASE_CFN_ACOSH:
12986 CASE_CFN_CABS:
12987 CASE_CFN_COSH:
12988 CASE_CFN_ERFC:
12989 CASE_CFN_EXP:
12990 CASE_CFN_EXP10:
12991 CASE_CFN_EXP2:
12992 CASE_CFN_FABS:
12993 CASE_CFN_FDIM:
12994 CASE_CFN_HYPOT:
12995 CASE_CFN_POW10:
12996 CASE_CFN_FFS:
12997 CASE_CFN_PARITY:
12998 CASE_CFN_POPCOUNT:
12999 CASE_CFN_CLZ:
13000 CASE_CFN_CLRSB:
13001 case CFN_BUILT_IN_BSWAP32:
13002 case CFN_BUILT_IN_BSWAP64:
13003 /* Always true. */
13004 return true;
13006 CASE_CFN_SQRT:
13007 CASE_CFN_SQRT_FN:
13008 /* sqrt(-0.0) is -0.0. */
13009 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13010 return true;
13011 return RECURSE (arg0);
13013 CASE_CFN_ASINH:
13014 CASE_CFN_ATAN:
13015 CASE_CFN_ATANH:
13016 CASE_CFN_CBRT:
13017 CASE_CFN_CEIL:
13018 CASE_CFN_CEIL_FN:
13019 CASE_CFN_ERF:
13020 CASE_CFN_EXPM1:
13021 CASE_CFN_FLOOR:
13022 CASE_CFN_FLOOR_FN:
13023 CASE_CFN_FMOD:
13024 CASE_CFN_FREXP:
13025 CASE_CFN_ICEIL:
13026 CASE_CFN_IFLOOR:
13027 CASE_CFN_IRINT:
13028 CASE_CFN_IROUND:
13029 CASE_CFN_LCEIL:
13030 CASE_CFN_LDEXP:
13031 CASE_CFN_LFLOOR:
13032 CASE_CFN_LLCEIL:
13033 CASE_CFN_LLFLOOR:
13034 CASE_CFN_LLRINT:
13035 CASE_CFN_LLROUND:
13036 CASE_CFN_LRINT:
13037 CASE_CFN_LROUND:
13038 CASE_CFN_MODF:
13039 CASE_CFN_NEARBYINT:
13040 CASE_CFN_NEARBYINT_FN:
13041 CASE_CFN_RINT:
13042 CASE_CFN_RINT_FN:
13043 CASE_CFN_ROUND:
13044 CASE_CFN_ROUND_FN:
13045 CASE_CFN_SCALB:
13046 CASE_CFN_SCALBLN:
13047 CASE_CFN_SCALBN:
13048 CASE_CFN_SIGNBIT:
13049 CASE_CFN_SIGNIFICAND:
13050 CASE_CFN_SINH:
13051 CASE_CFN_TANH:
13052 CASE_CFN_TRUNC:
13053 CASE_CFN_TRUNC_FN:
13054 /* True if the 1st argument is nonnegative. */
13055 return RECURSE (arg0);
13057 CASE_CFN_FMAX:
13058 CASE_CFN_FMAX_FN:
13059 /* True if the 1st OR 2nd arguments are nonnegative. */
13060 return RECURSE (arg0) || RECURSE (arg1);
13062 CASE_CFN_FMIN:
13063 CASE_CFN_FMIN_FN:
13064 /* True if the 1st AND 2nd arguments are nonnegative. */
13065 return RECURSE (arg0) && RECURSE (arg1);
13067 CASE_CFN_COPYSIGN:
13068 CASE_CFN_COPYSIGN_FN:
13069 /* True if the 2nd argument is nonnegative. */
13070 return RECURSE (arg1);
13072 CASE_CFN_POWI:
13073 /* True if the 1st argument is nonnegative or the second
13074 argument is an even integer. */
13075 if (TREE_CODE (arg1) == INTEGER_CST
13076 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13077 return true;
13078 return RECURSE (arg0);
13080 CASE_CFN_POW:
13081 /* True if the 1st argument is nonnegative or the second
13082 argument is an even integer valued real. */
13083 if (TREE_CODE (arg1) == REAL_CST)
13085 REAL_VALUE_TYPE c;
13086 HOST_WIDE_INT n;
13088 c = TREE_REAL_CST (arg1);
13089 n = real_to_integer (&c);
13090 if ((n & 1) == 0)
13092 REAL_VALUE_TYPE cint;
13093 real_from_integer (&cint, VOIDmode, n, SIGNED);
13094 if (real_identical (&c, &cint))
13095 return true;
13098 return RECURSE (arg0);
13100 default:
13101 break;
13103 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13106 /* Return true if T is known to be non-negative. If the return
13107 value is based on the assumption that signed overflow is undefined,
13108 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13109 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13111 static bool
13112 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13114 enum tree_code code = TREE_CODE (t);
13115 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13116 return true;
13118 switch (code)
13120 case TARGET_EXPR:
13122 tree temp = TARGET_EXPR_SLOT (t);
13123 t = TARGET_EXPR_INITIAL (t);
13125 /* If the initializer is non-void, then it's a normal expression
13126 that will be assigned to the slot. */
13127 if (!VOID_TYPE_P (t))
13128 return RECURSE (t);
13130 /* Otherwise, the initializer sets the slot in some way. One common
13131 way is an assignment statement at the end of the initializer. */
13132 while (1)
13134 if (TREE_CODE (t) == BIND_EXPR)
13135 t = expr_last (BIND_EXPR_BODY (t));
13136 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13137 || TREE_CODE (t) == TRY_CATCH_EXPR)
13138 t = expr_last (TREE_OPERAND (t, 0));
13139 else if (TREE_CODE (t) == STATEMENT_LIST)
13140 t = expr_last (t);
13141 else
13142 break;
13144 if (TREE_CODE (t) == MODIFY_EXPR
13145 && TREE_OPERAND (t, 0) == temp)
13146 return RECURSE (TREE_OPERAND (t, 1));
13148 return false;
13151 case CALL_EXPR:
13153 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13154 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13156 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13157 get_call_combined_fn (t),
13158 arg0,
13159 arg1,
13160 strict_overflow_p, depth);
13162 case COMPOUND_EXPR:
13163 case MODIFY_EXPR:
13164 return RECURSE (TREE_OPERAND (t, 1));
13166 case BIND_EXPR:
13167 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13169 case SAVE_EXPR:
13170 return RECURSE (TREE_OPERAND (t, 0));
13172 default:
13173 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13177 #undef RECURSE
13178 #undef tree_expr_nonnegative_warnv_p
13180 /* Return true if T is known to be non-negative. If the return
13181 value is based on the assumption that signed overflow is undefined,
13182 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13183 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13185 bool
13186 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13188 enum tree_code code;
13189 if (t == error_mark_node)
13190 return false;
13192 code = TREE_CODE (t);
13193 switch (TREE_CODE_CLASS (code))
13195 case tcc_binary:
13196 case tcc_comparison:
13197 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13198 TREE_TYPE (t),
13199 TREE_OPERAND (t, 0),
13200 TREE_OPERAND (t, 1),
13201 strict_overflow_p, depth);
13203 case tcc_unary:
13204 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13205 TREE_TYPE (t),
13206 TREE_OPERAND (t, 0),
13207 strict_overflow_p, depth);
13209 case tcc_constant:
13210 case tcc_declaration:
13211 case tcc_reference:
13212 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13214 default:
13215 break;
13218 switch (code)
13220 case TRUTH_AND_EXPR:
13221 case TRUTH_OR_EXPR:
13222 case TRUTH_XOR_EXPR:
13223 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13224 TREE_TYPE (t),
13225 TREE_OPERAND (t, 0),
13226 TREE_OPERAND (t, 1),
13227 strict_overflow_p, depth);
13228 case TRUTH_NOT_EXPR:
13229 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13230 TREE_TYPE (t),
13231 TREE_OPERAND (t, 0),
13232 strict_overflow_p, depth);
13234 case COND_EXPR:
13235 case CONSTRUCTOR:
13236 case OBJ_TYPE_REF:
13237 case ASSERT_EXPR:
13238 case ADDR_EXPR:
13239 case WITH_SIZE_EXPR:
13240 case SSA_NAME:
13241 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13243 default:
13244 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13248 /* Return true if `t' is known to be non-negative. Handle warnings
13249 about undefined signed overflow. */
13251 bool
13252 tree_expr_nonnegative_p (tree t)
13254 bool ret, strict_overflow_p;
13256 strict_overflow_p = false;
13257 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13258 if (strict_overflow_p)
13259 fold_overflow_warning (("assuming signed overflow does not occur when "
13260 "determining that expression is always "
13261 "non-negative"),
13262 WARN_STRICT_OVERFLOW_MISC);
13263 return ret;
13267 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13268 For floating point we further ensure that T is not denormal.
13269 Similar logic is present in nonzero_address in rtlanal.h.
13271 If the return value is based on the assumption that signed overflow
13272 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13273 change *STRICT_OVERFLOW_P. */
13275 bool
13276 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13277 bool *strict_overflow_p)
13279 switch (code)
13281 case ABS_EXPR:
13282 return tree_expr_nonzero_warnv_p (op0,
13283 strict_overflow_p);
13285 case NOP_EXPR:
13287 tree inner_type = TREE_TYPE (op0);
13288 tree outer_type = type;
13290 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13291 && tree_expr_nonzero_warnv_p (op0,
13292 strict_overflow_p));
13294 break;
13296 case NON_LVALUE_EXPR:
13297 return tree_expr_nonzero_warnv_p (op0,
13298 strict_overflow_p);
13300 default:
13301 break;
13304 return false;
13307 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13308 For floating point we further ensure that T is not denormal.
13309 Similar logic is present in nonzero_address in rtlanal.h.
13311 If the return value is based on the assumption that signed overflow
13312 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13313 change *STRICT_OVERFLOW_P. */
13315 bool
13316 tree_binary_nonzero_warnv_p (enum tree_code code,
13317 tree type,
13318 tree op0,
13319 tree op1, bool *strict_overflow_p)
13321 bool sub_strict_overflow_p;
13322 switch (code)
13324 case POINTER_PLUS_EXPR:
13325 case PLUS_EXPR:
13326 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13328 /* With the presence of negative values it is hard
13329 to say something. */
13330 sub_strict_overflow_p = false;
13331 if (!tree_expr_nonnegative_warnv_p (op0,
13332 &sub_strict_overflow_p)
13333 || !tree_expr_nonnegative_warnv_p (op1,
13334 &sub_strict_overflow_p))
13335 return false;
13336 /* One of operands must be positive and the other non-negative. */
13337 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13338 overflows, on a twos-complement machine the sum of two
13339 nonnegative numbers can never be zero. */
13340 return (tree_expr_nonzero_warnv_p (op0,
13341 strict_overflow_p)
13342 || tree_expr_nonzero_warnv_p (op1,
13343 strict_overflow_p));
13345 break;
13347 case MULT_EXPR:
13348 if (TYPE_OVERFLOW_UNDEFINED (type))
13350 if (tree_expr_nonzero_warnv_p (op0,
13351 strict_overflow_p)
13352 && tree_expr_nonzero_warnv_p (op1,
13353 strict_overflow_p))
13355 *strict_overflow_p = true;
13356 return true;
13359 break;
13361 case MIN_EXPR:
13362 sub_strict_overflow_p = false;
13363 if (tree_expr_nonzero_warnv_p (op0,
13364 &sub_strict_overflow_p)
13365 && tree_expr_nonzero_warnv_p (op1,
13366 &sub_strict_overflow_p))
13368 if (sub_strict_overflow_p)
13369 *strict_overflow_p = true;
13371 break;
13373 case MAX_EXPR:
13374 sub_strict_overflow_p = false;
13375 if (tree_expr_nonzero_warnv_p (op0,
13376 &sub_strict_overflow_p))
13378 if (sub_strict_overflow_p)
13379 *strict_overflow_p = true;
13381 /* When both operands are nonzero, then MAX must be too. */
13382 if (tree_expr_nonzero_warnv_p (op1,
13383 strict_overflow_p))
13384 return true;
13386 /* MAX where operand 0 is positive is positive. */
13387 return tree_expr_nonnegative_warnv_p (op0,
13388 strict_overflow_p);
13390 /* MAX where operand 1 is positive is positive. */
13391 else if (tree_expr_nonzero_warnv_p (op1,
13392 &sub_strict_overflow_p)
13393 && tree_expr_nonnegative_warnv_p (op1,
13394 &sub_strict_overflow_p))
13396 if (sub_strict_overflow_p)
13397 *strict_overflow_p = true;
13398 return true;
13400 break;
13402 case BIT_IOR_EXPR:
13403 return (tree_expr_nonzero_warnv_p (op1,
13404 strict_overflow_p)
13405 || tree_expr_nonzero_warnv_p (op0,
13406 strict_overflow_p));
13408 default:
13409 break;
13412 return false;
13415 /* Return true when T is an address and is known to be nonzero.
13416 For floating point we further ensure that T is not denormal.
13417 Similar logic is present in nonzero_address in rtlanal.h.
13419 If the return value is based on the assumption that signed overflow
13420 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13421 change *STRICT_OVERFLOW_P. */
13423 bool
13424 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13426 bool sub_strict_overflow_p;
13427 switch (TREE_CODE (t))
13429 case INTEGER_CST:
13430 return !integer_zerop (t);
13432 case ADDR_EXPR:
13434 tree base = TREE_OPERAND (t, 0);
13436 if (!DECL_P (base))
13437 base = get_base_address (base);
13439 if (base && TREE_CODE (base) == TARGET_EXPR)
13440 base = TARGET_EXPR_SLOT (base);
13442 if (!base)
13443 return false;
13445 /* For objects in symbol table check if we know they are non-zero.
13446 Don't do anything for variables and functions before symtab is built;
13447 it is quite possible that they will be declared weak later. */
13448 int nonzero_addr = maybe_nonzero_address (base);
13449 if (nonzero_addr >= 0)
13450 return nonzero_addr;
13452 /* Constants are never weak. */
13453 if (CONSTANT_CLASS_P (base))
13454 return true;
13456 return false;
13459 case COND_EXPR:
13460 sub_strict_overflow_p = false;
13461 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13462 &sub_strict_overflow_p)
13463 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13464 &sub_strict_overflow_p))
13466 if (sub_strict_overflow_p)
13467 *strict_overflow_p = true;
13468 return true;
13470 break;
13472 case SSA_NAME:
13473 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13474 break;
13475 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13477 default:
13478 break;
13480 return false;
13483 #define integer_valued_real_p(X) \
13484 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13486 #define RECURSE(X) \
13487 ((integer_valued_real_p) (X, depth + 1))
13489 /* Return true if the floating point result of (CODE OP0) has an
13490 integer value. We also allow +Inf, -Inf and NaN to be considered
13491 integer values. Return false for signaling NaN.
13493 DEPTH is the current nesting depth of the query. */
13495 bool
13496 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13498 switch (code)
13500 case FLOAT_EXPR:
13501 return true;
13503 case ABS_EXPR:
13504 return RECURSE (op0);
13506 CASE_CONVERT:
13508 tree type = TREE_TYPE (op0);
13509 if (TREE_CODE (type) == INTEGER_TYPE)
13510 return true;
13511 if (TREE_CODE (type) == REAL_TYPE)
13512 return RECURSE (op0);
13513 break;
13516 default:
13517 break;
13519 return false;
13522 /* Return true if the floating point result of (CODE OP0 OP1) has an
13523 integer value. We also allow +Inf, -Inf and NaN to be considered
13524 integer values. Return false for signaling NaN.
13526 DEPTH is the current nesting depth of the query. */
13528 bool
13529 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13531 switch (code)
13533 case PLUS_EXPR:
13534 case MINUS_EXPR:
13535 case MULT_EXPR:
13536 case MIN_EXPR:
13537 case MAX_EXPR:
13538 return RECURSE (op0) && RECURSE (op1);
13540 default:
13541 break;
13543 return false;
13546 /* Return true if the floating point result of calling FNDECL with arguments
13547 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13548 considered integer values. Return false for signaling NaN. If FNDECL
13549 takes fewer than 2 arguments, the remaining ARGn are null.
13551 DEPTH is the current nesting depth of the query. */
13553 bool
13554 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13556 switch (fn)
13558 CASE_CFN_CEIL:
13559 CASE_CFN_CEIL_FN:
13560 CASE_CFN_FLOOR:
13561 CASE_CFN_FLOOR_FN:
13562 CASE_CFN_NEARBYINT:
13563 CASE_CFN_NEARBYINT_FN:
13564 CASE_CFN_RINT:
13565 CASE_CFN_RINT_FN:
13566 CASE_CFN_ROUND:
13567 CASE_CFN_ROUND_FN:
13568 CASE_CFN_TRUNC:
13569 CASE_CFN_TRUNC_FN:
13570 return true;
13572 CASE_CFN_FMIN:
13573 CASE_CFN_FMIN_FN:
13574 CASE_CFN_FMAX:
13575 CASE_CFN_FMAX_FN:
13576 return RECURSE (arg0) && RECURSE (arg1);
13578 default:
13579 break;
13581 return false;
13584 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13585 has an integer value. We also allow +Inf, -Inf and NaN to be
13586 considered integer values. Return false for signaling NaN.
13588 DEPTH is the current nesting depth of the query. */
13590 bool
13591 integer_valued_real_single_p (tree t, int depth)
13593 switch (TREE_CODE (t))
13595 case REAL_CST:
13596 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13598 case COND_EXPR:
13599 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13601 case SSA_NAME:
13602 /* Limit the depth of recursion to avoid quadratic behavior.
13603 This is expected to catch almost all occurrences in practice.
13604 If this code misses important cases that unbounded recursion
13605 would not, passes that need this information could be revised
13606 to provide it through dataflow propagation. */
13607 return (!name_registered_for_update_p (t)
13608 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13609 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13610 depth));
13612 default:
13613 break;
13615 return false;
13618 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13619 has an integer value. We also allow +Inf, -Inf and NaN to be
13620 considered integer values. Return false for signaling NaN.
13622 DEPTH is the current nesting depth of the query. */
13624 static bool
13625 integer_valued_real_invalid_p (tree t, int depth)
13627 switch (TREE_CODE (t))
13629 case COMPOUND_EXPR:
13630 case MODIFY_EXPR:
13631 case BIND_EXPR:
13632 return RECURSE (TREE_OPERAND (t, 1));
13634 case SAVE_EXPR:
13635 return RECURSE (TREE_OPERAND (t, 0));
13637 default:
13638 break;
13640 return false;
13643 #undef RECURSE
13644 #undef integer_valued_real_p
13646 /* Return true if the floating point expression T has an integer value.
13647 We also allow +Inf, -Inf and NaN to be considered integer values.
13648 Return false for signaling NaN.
13650 DEPTH is the current nesting depth of the query. */
13652 bool
13653 integer_valued_real_p (tree t, int depth)
13655 if (t == error_mark_node)
13656 return false;
13658 tree_code code = TREE_CODE (t);
13659 switch (TREE_CODE_CLASS (code))
13661 case tcc_binary:
13662 case tcc_comparison:
13663 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13664 TREE_OPERAND (t, 1), depth);
13666 case tcc_unary:
13667 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13669 case tcc_constant:
13670 case tcc_declaration:
13671 case tcc_reference:
13672 return integer_valued_real_single_p (t, depth);
13674 default:
13675 break;
13678 switch (code)
13680 case COND_EXPR:
13681 case SSA_NAME:
13682 return integer_valued_real_single_p (t, depth);
13684 case CALL_EXPR:
13686 tree arg0 = (call_expr_nargs (t) > 0
13687 ? CALL_EXPR_ARG (t, 0)
13688 : NULL_TREE);
13689 tree arg1 = (call_expr_nargs (t) > 1
13690 ? CALL_EXPR_ARG (t, 1)
13691 : NULL_TREE);
13692 return integer_valued_real_call_p (get_call_combined_fn (t),
13693 arg0, arg1, depth);
13696 default:
13697 return integer_valued_real_invalid_p (t, depth);
13701 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13702 attempt to fold the expression to a constant without modifying TYPE,
13703 OP0 or OP1.
13705 If the expression could be simplified to a constant, then return
13706 the constant. If the expression would not be simplified to a
13707 constant, then return NULL_TREE. */
13709 tree
13710 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13712 tree tem = fold_binary (code, type, op0, op1);
13713 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13716 /* Given the components of a unary expression CODE, TYPE and OP0,
13717 attempt to fold the expression to a constant without modifying
13718 TYPE or OP0.
13720 If the expression could be simplified to a constant, then return
13721 the constant. If the expression would not be simplified to a
13722 constant, then return NULL_TREE. */
13724 tree
13725 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13727 tree tem = fold_unary (code, type, op0);
13728 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13731 /* If EXP represents referencing an element in a constant string
13732 (either via pointer arithmetic or array indexing), return the
13733 tree representing the value accessed, otherwise return NULL. */
13735 tree
13736 fold_read_from_constant_string (tree exp)
13738 if ((TREE_CODE (exp) == INDIRECT_REF
13739 || TREE_CODE (exp) == ARRAY_REF)
13740 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13742 tree exp1 = TREE_OPERAND (exp, 0);
13743 tree index;
13744 tree string;
13745 location_t loc = EXPR_LOCATION (exp);
13747 if (TREE_CODE (exp) == INDIRECT_REF)
13748 string = string_constant (exp1, &index);
13749 else
13751 tree low_bound = array_ref_low_bound (exp);
13752 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13754 /* Optimize the special-case of a zero lower bound.
13756 We convert the low_bound to sizetype to avoid some problems
13757 with constant folding. (E.g. suppose the lower bound is 1,
13758 and its mode is QI. Without the conversion,l (ARRAY
13759 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13760 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13761 if (! integer_zerop (low_bound))
13762 index = size_diffop_loc (loc, index,
13763 fold_convert_loc (loc, sizetype, low_bound));
13765 string = exp1;
13768 scalar_int_mode char_mode;
13769 if (string
13770 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13771 && TREE_CODE (string) == STRING_CST
13772 && TREE_CODE (index) == INTEGER_CST
13773 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13774 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13775 &char_mode)
13776 && GET_MODE_SIZE (char_mode) == 1)
13777 return build_int_cst_type (TREE_TYPE (exp),
13778 (TREE_STRING_POINTER (string)
13779 [TREE_INT_CST_LOW (index)]));
13781 return NULL;
13784 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13785 an integer constant, real, or fixed-point constant.
13787 TYPE is the type of the result. */
13789 static tree
13790 fold_negate_const (tree arg0, tree type)
13792 tree t = NULL_TREE;
13794 switch (TREE_CODE (arg0))
13796 case REAL_CST:
13797 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13798 break;
13800 case FIXED_CST:
13802 FIXED_VALUE_TYPE f;
13803 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13804 &(TREE_FIXED_CST (arg0)), NULL,
13805 TYPE_SATURATING (type));
13806 t = build_fixed (type, f);
13807 /* Propagate overflow flags. */
13808 if (overflow_p | TREE_OVERFLOW (arg0))
13809 TREE_OVERFLOW (t) = 1;
13810 break;
13813 default:
13814 if (poly_int_tree_p (arg0))
13816 bool overflow;
13817 poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
13818 t = force_fit_type (type, res, 1,
13819 (overflow && ! TYPE_UNSIGNED (type))
13820 || TREE_OVERFLOW (arg0));
13821 break;
13824 gcc_unreachable ();
13827 return t;
13830 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13831 an integer constant or real constant.
13833 TYPE is the type of the result. */
13835 tree
13836 fold_abs_const (tree arg0, tree type)
13838 tree t = NULL_TREE;
13840 switch (TREE_CODE (arg0))
13842 case INTEGER_CST:
13844 /* If the value is unsigned or non-negative, then the absolute value
13845 is the same as the ordinary value. */
13846 if (!wi::neg_p (wi::to_wide (arg0), TYPE_SIGN (type)))
13847 t = arg0;
13849 /* If the value is negative, then the absolute value is
13850 its negation. */
13851 else
13853 bool overflow;
13854 wide_int val = wi::neg (wi::to_wide (arg0), &overflow);
13855 t = force_fit_type (type, val, -1,
13856 overflow | TREE_OVERFLOW (arg0));
13859 break;
13861 case REAL_CST:
13862 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13863 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13864 else
13865 t = arg0;
13866 break;
13868 default:
13869 gcc_unreachable ();
13872 return t;
13875 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13876 constant. TYPE is the type of the result. */
13878 static tree
13879 fold_not_const (const_tree arg0, tree type)
13881 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13883 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
13886 /* Given CODE, a relational operator, the target type, TYPE and two
13887 constant operands OP0 and OP1, return the result of the
13888 relational operation. If the result is not a compile time
13889 constant, then return NULL_TREE. */
13891 static tree
13892 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13894 int result, invert;
13896 /* From here on, the only cases we handle are when the result is
13897 known to be a constant. */
13899 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13901 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13902 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13904 /* Handle the cases where either operand is a NaN. */
13905 if (real_isnan (c0) || real_isnan (c1))
13907 switch (code)
13909 case EQ_EXPR:
13910 case ORDERED_EXPR:
13911 result = 0;
13912 break;
13914 case NE_EXPR:
13915 case UNORDERED_EXPR:
13916 case UNLT_EXPR:
13917 case UNLE_EXPR:
13918 case UNGT_EXPR:
13919 case UNGE_EXPR:
13920 case UNEQ_EXPR:
13921 result = 1;
13922 break;
13924 case LT_EXPR:
13925 case LE_EXPR:
13926 case GT_EXPR:
13927 case GE_EXPR:
13928 case LTGT_EXPR:
13929 if (flag_trapping_math)
13930 return NULL_TREE;
13931 result = 0;
13932 break;
13934 default:
13935 gcc_unreachable ();
13938 return constant_boolean_node (result, type);
13941 return constant_boolean_node (real_compare (code, c0, c1), type);
13944 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13946 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13947 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13948 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13951 /* Handle equality/inequality of complex constants. */
13952 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13954 tree rcond = fold_relational_const (code, type,
13955 TREE_REALPART (op0),
13956 TREE_REALPART (op1));
13957 tree icond = fold_relational_const (code, type,
13958 TREE_IMAGPART (op0),
13959 TREE_IMAGPART (op1));
13960 if (code == EQ_EXPR)
13961 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13962 else if (code == NE_EXPR)
13963 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13964 else
13965 return NULL_TREE;
13968 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13970 if (!VECTOR_TYPE_P (type))
13972 /* Have vector comparison with scalar boolean result. */
13973 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13974 && known_eq (VECTOR_CST_NELTS (op0),
13975 VECTOR_CST_NELTS (op1)));
13976 unsigned HOST_WIDE_INT nunits;
13977 if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
13978 return NULL_TREE;
13979 for (unsigned i = 0; i < nunits; i++)
13981 tree elem0 = VECTOR_CST_ELT (op0, i);
13982 tree elem1 = VECTOR_CST_ELT (op1, i);
13983 tree tmp = fold_relational_const (code, type, elem0, elem1);
13984 if (tmp == NULL_TREE)
13985 return NULL_TREE;
13986 if (integer_zerop (tmp))
13987 return constant_boolean_node (false, type);
13989 return constant_boolean_node (true, type);
13991 tree_vector_builder elts;
13992 if (!elts.new_binary_operation (type, op0, op1, false))
13993 return NULL_TREE;
13994 unsigned int count = elts.encoded_nelts ();
13995 for (unsigned i = 0; i < count; i++)
13997 tree elem_type = TREE_TYPE (type);
13998 tree elem0 = VECTOR_CST_ELT (op0, i);
13999 tree elem1 = VECTOR_CST_ELT (op1, i);
14001 tree tem = fold_relational_const (code, elem_type,
14002 elem0, elem1);
14004 if (tem == NULL_TREE)
14005 return NULL_TREE;
14007 elts.quick_push (build_int_cst (elem_type,
14008 integer_zerop (tem) ? 0 : -1));
14011 return elts.build ();
14014 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14016 To compute GT, swap the arguments and do LT.
14017 To compute GE, do LT and invert the result.
14018 To compute LE, swap the arguments, do LT and invert the result.
14019 To compute NE, do EQ and invert the result.
14021 Therefore, the code below must handle only EQ and LT. */
14023 if (code == LE_EXPR || code == GT_EXPR)
14025 std::swap (op0, op1);
14026 code = swap_tree_comparison (code);
14029 /* Note that it is safe to invert for real values here because we
14030 have already handled the one case that it matters. */
14032 invert = 0;
14033 if (code == NE_EXPR || code == GE_EXPR)
14035 invert = 1;
14036 code = invert_tree_comparison (code, false);
14039 /* Compute a result for LT or EQ if args permit;
14040 Otherwise return T. */
14041 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14043 if (code == EQ_EXPR)
14044 result = tree_int_cst_equal (op0, op1);
14045 else
14046 result = tree_int_cst_lt (op0, op1);
14048 else
14049 return NULL_TREE;
14051 if (invert)
14052 result ^= 1;
14053 return constant_boolean_node (result, type);
14056 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14057 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14058 itself. */
14060 tree
14061 fold_build_cleanup_point_expr (tree type, tree expr)
14063 /* If the expression does not have side effects then we don't have to wrap
14064 it with a cleanup point expression. */
14065 if (!TREE_SIDE_EFFECTS (expr))
14066 return expr;
14068 /* If the expression is a return, check to see if the expression inside the
14069 return has no side effects or the right hand side of the modify expression
14070 inside the return. If either don't have side effects set we don't need to
14071 wrap the expression in a cleanup point expression. Note we don't check the
14072 left hand side of the modify because it should always be a return decl. */
14073 if (TREE_CODE (expr) == RETURN_EXPR)
14075 tree op = TREE_OPERAND (expr, 0);
14076 if (!op || !TREE_SIDE_EFFECTS (op))
14077 return expr;
14078 op = TREE_OPERAND (op, 1);
14079 if (!TREE_SIDE_EFFECTS (op))
14080 return expr;
14083 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
14086 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14087 of an indirection through OP0, or NULL_TREE if no simplification is
14088 possible. */
14090 tree
14091 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14093 tree sub = op0;
14094 tree subtype;
14095 poly_uint64 const_op01;
14097 STRIP_NOPS (sub);
14098 subtype = TREE_TYPE (sub);
14099 if (!POINTER_TYPE_P (subtype)
14100 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
14101 return NULL_TREE;
14103 if (TREE_CODE (sub) == ADDR_EXPR)
14105 tree op = TREE_OPERAND (sub, 0);
14106 tree optype = TREE_TYPE (op);
14108 /* *&CONST_DECL -> to the value of the const decl. */
14109 if (TREE_CODE (op) == CONST_DECL)
14110 return DECL_INITIAL (op);
14111 /* *&p => p; make sure to handle *&"str"[cst] here. */
14112 if (type == optype)
14114 tree fop = fold_read_from_constant_string (op);
14115 if (fop)
14116 return fop;
14117 else
14118 return op;
14120 /* *(foo *)&fooarray => fooarray[0] */
14121 else if (TREE_CODE (optype) == ARRAY_TYPE
14122 && type == TREE_TYPE (optype)
14123 && (!in_gimple_form
14124 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14126 tree type_domain = TYPE_DOMAIN (optype);
14127 tree min_val = size_zero_node;
14128 if (type_domain && TYPE_MIN_VALUE (type_domain))
14129 min_val = TYPE_MIN_VALUE (type_domain);
14130 if (in_gimple_form
14131 && TREE_CODE (min_val) != INTEGER_CST)
14132 return NULL_TREE;
14133 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14134 NULL_TREE, NULL_TREE);
14136 /* *(foo *)&complexfoo => __real__ complexfoo */
14137 else if (TREE_CODE (optype) == COMPLEX_TYPE
14138 && type == TREE_TYPE (optype))
14139 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14140 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14141 else if (VECTOR_TYPE_P (optype)
14142 && type == TREE_TYPE (optype))
14144 tree part_width = TYPE_SIZE (type);
14145 tree index = bitsize_int (0);
14146 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
14147 index);
14151 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14152 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
14154 tree op00 = TREE_OPERAND (sub, 0);
14155 tree op01 = TREE_OPERAND (sub, 1);
14157 STRIP_NOPS (op00);
14158 if (TREE_CODE (op00) == ADDR_EXPR)
14160 tree op00type;
14161 op00 = TREE_OPERAND (op00, 0);
14162 op00type = TREE_TYPE (op00);
14164 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14165 if (VECTOR_TYPE_P (op00type)
14166 && type == TREE_TYPE (op00type)
14167 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
14168 but we want to treat offsets with MSB set as negative.
14169 For the code below negative offsets are invalid and
14170 TYPE_SIZE of the element is something unsigned, so
14171 check whether op01 fits into poly_int64, which implies
14172 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
14173 then just use poly_uint64 because we want to treat the
14174 value as unsigned. */
14175 && tree_fits_poly_int64_p (op01))
14177 tree part_width = TYPE_SIZE (type);
14178 poly_uint64 max_offset
14179 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14180 * TYPE_VECTOR_SUBPARTS (op00type));
14181 if (known_lt (const_op01, max_offset))
14183 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
14184 return fold_build3_loc (loc,
14185 BIT_FIELD_REF, type, op00,
14186 part_width, index);
14189 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14190 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14191 && type == TREE_TYPE (op00type))
14193 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
14194 const_op01))
14195 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14197 /* ((foo *)&fooarray)[1] => fooarray[1] */
14198 else if (TREE_CODE (op00type) == ARRAY_TYPE
14199 && type == TREE_TYPE (op00type))
14201 tree type_domain = TYPE_DOMAIN (op00type);
14202 tree min_val = size_zero_node;
14203 if (type_domain && TYPE_MIN_VALUE (type_domain))
14204 min_val = TYPE_MIN_VALUE (type_domain);
14205 poly_uint64 type_size, index;
14206 if (poly_int_tree_p (min_val)
14207 && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
14208 && multiple_p (const_op01, type_size, &index))
14210 poly_offset_int off = index + wi::to_poly_offset (min_val);
14211 op01 = wide_int_to_tree (sizetype, off);
14212 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14213 NULL_TREE, NULL_TREE);
14219 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14220 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14221 && type == TREE_TYPE (TREE_TYPE (subtype))
14222 && (!in_gimple_form
14223 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14225 tree type_domain;
14226 tree min_val = size_zero_node;
14227 sub = build_fold_indirect_ref_loc (loc, sub);
14228 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14229 if (type_domain && TYPE_MIN_VALUE (type_domain))
14230 min_val = TYPE_MIN_VALUE (type_domain);
14231 if (in_gimple_form
14232 && TREE_CODE (min_val) != INTEGER_CST)
14233 return NULL_TREE;
14234 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14235 NULL_TREE);
14238 return NULL_TREE;
14241 /* Builds an expression for an indirection through T, simplifying some
14242 cases. */
14244 tree
14245 build_fold_indirect_ref_loc (location_t loc, tree t)
14247 tree type = TREE_TYPE (TREE_TYPE (t));
14248 tree sub = fold_indirect_ref_1 (loc, type, t);
14250 if (sub)
14251 return sub;
14253 return build1_loc (loc, INDIRECT_REF, type, t);
14256 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14258 tree
14259 fold_indirect_ref_loc (location_t loc, tree t)
14261 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14263 if (sub)
14264 return sub;
14265 else
14266 return t;
14269 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14270 whose result is ignored. The type of the returned tree need not be
14271 the same as the original expression. */
14273 tree
14274 fold_ignored_result (tree t)
14276 if (!TREE_SIDE_EFFECTS (t))
14277 return integer_zero_node;
14279 for (;;)
14280 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14282 case tcc_unary:
14283 t = TREE_OPERAND (t, 0);
14284 break;
14286 case tcc_binary:
14287 case tcc_comparison:
14288 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14289 t = TREE_OPERAND (t, 0);
14290 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14291 t = TREE_OPERAND (t, 1);
14292 else
14293 return t;
14294 break;
14296 case tcc_expression:
14297 switch (TREE_CODE (t))
14299 case COMPOUND_EXPR:
14300 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14301 return t;
14302 t = TREE_OPERAND (t, 0);
14303 break;
14305 case COND_EXPR:
14306 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14307 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14308 return t;
14309 t = TREE_OPERAND (t, 0);
14310 break;
14312 default:
14313 return t;
14315 break;
14317 default:
14318 return t;
14322 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14324 tree
14325 round_up_loc (location_t loc, tree value, unsigned int divisor)
14327 tree div = NULL_TREE;
14329 if (divisor == 1)
14330 return value;
14332 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14333 have to do anything. Only do this when we are not given a const,
14334 because in that case, this check is more expensive than just
14335 doing it. */
14336 if (TREE_CODE (value) != INTEGER_CST)
14338 div = build_int_cst (TREE_TYPE (value), divisor);
14340 if (multiple_of_p (TREE_TYPE (value), value, div))
14341 return value;
14344 /* If divisor is a power of two, simplify this to bit manipulation. */
14345 if (pow2_or_zerop (divisor))
14347 if (TREE_CODE (value) == INTEGER_CST)
14349 wide_int val = wi::to_wide (value);
14350 bool overflow_p;
14352 if ((val & (divisor - 1)) == 0)
14353 return value;
14355 overflow_p = TREE_OVERFLOW (value);
14356 val += divisor - 1;
14357 val &= (int) -divisor;
14358 if (val == 0)
14359 overflow_p = true;
14361 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14363 else
14365 tree t;
14367 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14368 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14369 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14370 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14373 else
14375 if (!div)
14376 div = build_int_cst (TREE_TYPE (value), divisor);
14377 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14378 value = size_binop_loc (loc, MULT_EXPR, value, div);
14381 return value;
14384 /* Likewise, but round down. */
14386 tree
14387 round_down_loc (location_t loc, tree value, int divisor)
14389 tree div = NULL_TREE;
14391 gcc_assert (divisor > 0);
14392 if (divisor == 1)
14393 return value;
14395 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14396 have to do anything. Only do this when we are not given a const,
14397 because in that case, this check is more expensive than just
14398 doing it. */
14399 if (TREE_CODE (value) != INTEGER_CST)
14401 div = build_int_cst (TREE_TYPE (value), divisor);
14403 if (multiple_of_p (TREE_TYPE (value), value, div))
14404 return value;
14407 /* If divisor is a power of two, simplify this to bit manipulation. */
14408 if (pow2_or_zerop (divisor))
14410 tree t;
14412 t = build_int_cst (TREE_TYPE (value), -divisor);
14413 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14415 else
14417 if (!div)
14418 div = build_int_cst (TREE_TYPE (value), divisor);
14419 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14420 value = size_binop_loc (loc, MULT_EXPR, value, div);
14423 return value;
14426 /* Returns the pointer to the base of the object addressed by EXP and
14427 extracts the information about the offset of the access, storing it
14428 to PBITPOS and POFFSET. */
14430 static tree
14431 split_address_to_core_and_offset (tree exp,
14432 poly_int64_pod *pbitpos, tree *poffset)
14434 tree core;
14435 machine_mode mode;
14436 int unsignedp, reversep, volatilep;
14437 poly_int64 bitsize;
14438 location_t loc = EXPR_LOCATION (exp);
14440 if (TREE_CODE (exp) == ADDR_EXPR)
14442 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14443 poffset, &mode, &unsignedp, &reversep,
14444 &volatilep);
14445 core = build_fold_addr_expr_loc (loc, core);
14447 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14449 core = TREE_OPERAND (exp, 0);
14450 STRIP_NOPS (core);
14451 *pbitpos = 0;
14452 *poffset = TREE_OPERAND (exp, 1);
14453 if (poly_int_tree_p (*poffset))
14455 poly_offset_int tem
14456 = wi::sext (wi::to_poly_offset (*poffset),
14457 TYPE_PRECISION (TREE_TYPE (*poffset)));
14458 tem <<= LOG2_BITS_PER_UNIT;
14459 if (tem.to_shwi (pbitpos))
14460 *poffset = NULL_TREE;
14463 else
14465 core = exp;
14466 *pbitpos = 0;
14467 *poffset = NULL_TREE;
14470 return core;
14473 /* Returns true if addresses of E1 and E2 differ by a constant, false
14474 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14476 bool
14477 ptr_difference_const (tree e1, tree e2, poly_int64_pod *diff)
14479 tree core1, core2;
14480 poly_int64 bitpos1, bitpos2;
14481 tree toffset1, toffset2, tdiff, type;
14483 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14484 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14486 poly_int64 bytepos1, bytepos2;
14487 if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
14488 || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
14489 || !operand_equal_p (core1, core2, 0))
14490 return false;
14492 if (toffset1 && toffset2)
14494 type = TREE_TYPE (toffset1);
14495 if (type != TREE_TYPE (toffset2))
14496 toffset2 = fold_convert (type, toffset2);
14498 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14499 if (!cst_and_fits_in_hwi (tdiff))
14500 return false;
14502 *diff = int_cst_value (tdiff);
14504 else if (toffset1 || toffset2)
14506 /* If only one of the offsets is non-constant, the difference cannot
14507 be a constant. */
14508 return false;
14510 else
14511 *diff = 0;
14513 *diff += bytepos1 - bytepos2;
14514 return true;
14517 /* Return OFF converted to a pointer offset type suitable as offset for
14518 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14519 tree
14520 convert_to_ptrofftype_loc (location_t loc, tree off)
14522 return fold_convert_loc (loc, sizetype, off);
14525 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14526 tree
14527 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14529 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14530 ptr, convert_to_ptrofftype_loc (loc, off));
14533 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14534 tree
14535 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14537 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14538 ptr, size_int (off));
14541 /* Return a char pointer for a C string if it is a string constant
14542 or sum of string constant and integer constant. We only support
14543 string constants properly terminated with '\0' character.
14544 If STRLEN is a valid pointer, length (including terminating character)
14545 of returned string is stored to the argument. */
14547 const char *
14548 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14550 tree offset_node;
14552 if (strlen)
14553 *strlen = 0;
14555 src = string_constant (src, &offset_node);
14556 if (src == 0)
14557 return NULL;
14559 unsigned HOST_WIDE_INT offset = 0;
14560 if (offset_node != NULL_TREE)
14562 if (!tree_fits_uhwi_p (offset_node))
14563 return NULL;
14564 else
14565 offset = tree_to_uhwi (offset_node);
14568 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14569 const char *string = TREE_STRING_POINTER (src);
14571 /* Support only properly null-terminated strings. */
14572 if (string_length == 0
14573 || string[string_length - 1] != '\0'
14574 || offset >= string_length)
14575 return NULL;
14577 if (strlen)
14578 *strlen = string_length - offset;
14579 return string + offset;
14582 /* Given a tree T, compute which bits in T may be nonzero. */
14584 wide_int
14585 tree_nonzero_bits (const_tree t)
14587 switch (TREE_CODE (t))
14589 case INTEGER_CST:
14590 return wi::to_wide (t);
14591 case SSA_NAME:
14592 return get_nonzero_bits (t);
14593 case NON_LVALUE_EXPR:
14594 case SAVE_EXPR:
14595 return tree_nonzero_bits (TREE_OPERAND (t, 0));
14596 case BIT_AND_EXPR:
14597 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14598 tree_nonzero_bits (TREE_OPERAND (t, 1)));
14599 case BIT_IOR_EXPR:
14600 case BIT_XOR_EXPR:
14601 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14602 tree_nonzero_bits (TREE_OPERAND (t, 1)));
14603 case COND_EXPR:
14604 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1)),
14605 tree_nonzero_bits (TREE_OPERAND (t, 2)));
14606 CASE_CONVERT:
14607 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14608 TYPE_PRECISION (TREE_TYPE (t)),
14609 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0))));
14610 case PLUS_EXPR:
14611 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
14613 wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0));
14614 wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1));
14615 if (wi::bit_and (nzbits1, nzbits2) == 0)
14616 return wi::bit_or (nzbits1, nzbits2);
14618 break;
14619 case LSHIFT_EXPR:
14620 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
14622 tree type = TREE_TYPE (t);
14623 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
14624 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
14625 TYPE_PRECISION (type));
14626 return wi::neg_p (arg1)
14627 ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
14628 : wi::lshift (nzbits, arg1);
14630 break;
14631 case RSHIFT_EXPR:
14632 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
14634 tree type = TREE_TYPE (t);
14635 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
14636 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
14637 TYPE_PRECISION (type));
14638 return wi::neg_p (arg1)
14639 ? wi::lshift (nzbits, -arg1)
14640 : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
14642 break;
14643 default:
14644 break;
14647 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t)));
14650 #if CHECKING_P
14652 namespace selftest {
14654 /* Helper functions for writing tests of folding trees. */
14656 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14658 static void
14659 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14660 tree constant)
14662 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14665 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14666 wrapping WRAPPED_EXPR. */
14668 static void
14669 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14670 tree wrapped_expr)
14672 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14673 ASSERT_NE (wrapped_expr, result);
14674 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14675 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14678 /* Verify that various arithmetic binary operations are folded
14679 correctly. */
14681 static void
14682 test_arithmetic_folding ()
14684 tree type = integer_type_node;
14685 tree x = create_tmp_var_raw (type, "x");
14686 tree zero = build_zero_cst (type);
14687 tree one = build_int_cst (type, 1);
14689 /* Addition. */
14690 /* 1 <-- (0 + 1) */
14691 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14692 one);
14693 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14694 one);
14696 /* (nonlvalue)x <-- (x + 0) */
14697 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14700 /* Subtraction. */
14701 /* 0 <-- (x - x) */
14702 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14703 zero);
14704 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14707 /* Multiplication. */
14708 /* 0 <-- (x * 0) */
14709 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14710 zero);
14712 /* (nonlvalue)x <-- (x * 1) */
14713 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14717 /* Verify that various binary operations on vectors are folded
14718 correctly. */
14720 static void
14721 test_vector_folding ()
14723 tree inner_type = integer_type_node;
14724 tree type = build_vector_type (inner_type, 4);
14725 tree zero = build_zero_cst (type);
14726 tree one = build_one_cst (type);
14728 /* Verify equality tests that return a scalar boolean result. */
14729 tree res_type = boolean_type_node;
14730 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14731 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14732 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14733 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14736 /* Verify folding of VEC_DUPLICATE_EXPRs. */
14738 static void
14739 test_vec_duplicate_folding ()
14741 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
14742 machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
14743 /* This will be 1 if VEC_MODE isn't a vector mode. */
14744 poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
14746 tree type = build_vector_type (ssizetype, nunits);
14747 tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
14748 tree dup5_cst = build_vector_from_val (type, ssize_int (5));
14749 ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
14752 /* Run all of the selftests within this file. */
14754 void
14755 fold_const_c_tests ()
14757 test_arithmetic_folding ();
14758 test_vector_folding ();
14759 test_vec_duplicate_folding ();
14762 } // namespace selftest
14764 #endif /* CHECKING_P */