Fix GNU coding style for G_.
[official-gcc.git] / gcc / fold-const.c
blobfaa184a2bbdd3df8ff05b896e60eeadeb23db327
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 (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1616 offset_int res = wi::sub (wi::to_offset (arg1),
1617 wi::to_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:
1626 unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1628 if (TREE_CODE (arg1) != VECTOR_CST
1629 || TREE_CODE (arg2) != VECTOR_CST)
1630 return NULL_TREE;
1632 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1633 return NULL_TREE;
1635 out_nelts = in_nelts * 2;
1636 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1637 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1639 tree_vector_builder elts (type, out_nelts, 1);
1640 for (i = 0; i < out_nelts; i++)
1642 tree elt = (i < in_nelts
1643 ? VECTOR_CST_ELT (arg1, i)
1644 : VECTOR_CST_ELT (arg2, i - in_nelts));
1645 elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1646 ? NOP_EXPR : FIX_TRUNC_EXPR,
1647 TREE_TYPE (type), elt);
1648 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1649 return NULL_TREE;
1650 elts.quick_push (elt);
1653 return elts.build ();
1656 case VEC_WIDEN_MULT_LO_EXPR:
1657 case VEC_WIDEN_MULT_HI_EXPR:
1658 case VEC_WIDEN_MULT_EVEN_EXPR:
1659 case VEC_WIDEN_MULT_ODD_EXPR:
1661 unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1663 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1664 return NULL_TREE;
1666 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1667 return NULL_TREE;
1668 out_nelts = in_nelts / 2;
1669 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1670 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1672 if (code == VEC_WIDEN_MULT_LO_EXPR)
1673 scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1674 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1675 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1676 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1677 scale = 1, ofs = 0;
1678 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1679 scale = 1, ofs = 1;
1681 tree_vector_builder elts (type, out_nelts, 1);
1682 for (out = 0; out < out_nelts; out++)
1684 unsigned int in = (out << scale) + ofs;
1685 tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1686 VECTOR_CST_ELT (arg1, in));
1687 tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1688 VECTOR_CST_ELT (arg2, in));
1690 if (t1 == NULL_TREE || t2 == NULL_TREE)
1691 return NULL_TREE;
1692 tree elt = const_binop (MULT_EXPR, t1, t2);
1693 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1694 return NULL_TREE;
1695 elts.quick_push (elt);
1698 return elts.build ();
1701 default:;
1704 if (TREE_CODE_CLASS (code) != tcc_binary)
1705 return NULL_TREE;
1707 /* Make sure type and arg0 have the same saturating flag. */
1708 gcc_checking_assert (TYPE_SATURATING (type)
1709 == TYPE_SATURATING (TREE_TYPE (arg1)));
1711 return const_binop (code, arg1, arg2);
1714 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1715 Return zero if computing the constants is not possible. */
1717 tree
1718 const_unop (enum tree_code code, tree type, tree arg0)
1720 /* Don't perform the operation, other than NEGATE and ABS, if
1721 flag_signaling_nans is on and the operand is a signaling NaN. */
1722 if (TREE_CODE (arg0) == REAL_CST
1723 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1724 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1725 && code != NEGATE_EXPR
1726 && code != ABS_EXPR)
1727 return NULL_TREE;
1729 switch (code)
1731 CASE_CONVERT:
1732 case FLOAT_EXPR:
1733 case FIX_TRUNC_EXPR:
1734 case FIXED_CONVERT_EXPR:
1735 return fold_convert_const (code, type, arg0);
1737 case ADDR_SPACE_CONVERT_EXPR:
1738 /* If the source address is 0, and the source address space
1739 cannot have a valid object at 0, fold to dest type null. */
1740 if (integer_zerop (arg0)
1741 && !(targetm.addr_space.zero_address_valid
1742 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1743 return fold_convert_const (code, type, arg0);
1744 break;
1746 case VIEW_CONVERT_EXPR:
1747 return fold_view_convert_expr (type, arg0);
1749 case NEGATE_EXPR:
1751 /* Can't call fold_negate_const directly here as that doesn't
1752 handle all cases and we might not be able to negate some
1753 constants. */
1754 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1755 if (tem && CONSTANT_CLASS_P (tem))
1756 return tem;
1757 break;
1760 case ABS_EXPR:
1761 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1762 return fold_abs_const (arg0, type);
1763 break;
1765 case CONJ_EXPR:
1766 if (TREE_CODE (arg0) == COMPLEX_CST)
1768 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1769 TREE_TYPE (type));
1770 return build_complex (type, TREE_REALPART (arg0), ipart);
1772 break;
1774 case BIT_NOT_EXPR:
1775 if (TREE_CODE (arg0) == INTEGER_CST)
1776 return fold_not_const (arg0, type);
1777 else if (POLY_INT_CST_P (arg0))
1778 return wide_int_to_tree (type, -poly_int_cst_value (arg0));
1779 /* Perform BIT_NOT_EXPR on each element individually. */
1780 else if (TREE_CODE (arg0) == VECTOR_CST)
1782 tree elem;
1784 /* This can cope with stepped encodings because ~x == -1 - x. */
1785 tree_vector_builder elements;
1786 elements.new_unary_operation (type, arg0, true);
1787 unsigned int i, count = elements.encoded_nelts ();
1788 for (i = 0; i < count; ++i)
1790 elem = VECTOR_CST_ELT (arg0, i);
1791 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1792 if (elem == NULL_TREE)
1793 break;
1794 elements.quick_push (elem);
1796 if (i == count)
1797 return elements.build ();
1799 break;
1801 case TRUTH_NOT_EXPR:
1802 if (TREE_CODE (arg0) == INTEGER_CST)
1803 return constant_boolean_node (integer_zerop (arg0), type);
1804 break;
1806 case REALPART_EXPR:
1807 if (TREE_CODE (arg0) == COMPLEX_CST)
1808 return fold_convert (type, TREE_REALPART (arg0));
1809 break;
1811 case IMAGPART_EXPR:
1812 if (TREE_CODE (arg0) == COMPLEX_CST)
1813 return fold_convert (type, TREE_IMAGPART (arg0));
1814 break;
1816 case VEC_UNPACK_LO_EXPR:
1817 case VEC_UNPACK_HI_EXPR:
1818 case VEC_UNPACK_FLOAT_LO_EXPR:
1819 case VEC_UNPACK_FLOAT_HI_EXPR:
1821 unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
1822 enum tree_code subcode;
1824 if (TREE_CODE (arg0) != VECTOR_CST)
1825 return NULL_TREE;
1827 if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
1828 return NULL_TREE;
1829 out_nelts = in_nelts / 2;
1830 gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1832 unsigned int offset = 0;
1833 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1834 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1835 offset = out_nelts;
1837 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1838 subcode = NOP_EXPR;
1839 else
1840 subcode = FLOAT_EXPR;
1842 tree_vector_builder elts (type, out_nelts, 1);
1843 for (i = 0; i < out_nelts; i++)
1845 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1846 VECTOR_CST_ELT (arg0, i + offset));
1847 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1848 return NULL_TREE;
1849 elts.quick_push (elt);
1852 return elts.build ();
1855 case VEC_DUPLICATE_EXPR:
1856 if (CONSTANT_CLASS_P (arg0))
1857 return build_vector_from_val (type, arg0);
1858 return NULL_TREE;
1860 default:
1861 break;
1864 return NULL_TREE;
1867 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1868 indicates which particular sizetype to create. */
1870 tree
1871 size_int_kind (poly_int64 number, enum size_type_kind kind)
1873 return build_int_cst (sizetype_tab[(int) kind], number);
1876 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1877 is a tree code. The type of the result is taken from the operands.
1878 Both must be equivalent integer types, ala int_binop_types_match_p.
1879 If the operands are constant, so is the result. */
1881 tree
1882 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1884 tree type = TREE_TYPE (arg0);
1886 if (arg0 == error_mark_node || arg1 == error_mark_node)
1887 return error_mark_node;
1889 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1890 TREE_TYPE (arg1)));
1892 /* Handle the special case of two poly_int constants faster. */
1893 if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1895 /* And some specific cases even faster than that. */
1896 if (code == PLUS_EXPR)
1898 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1899 return arg1;
1900 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1901 return arg0;
1903 else if (code == MINUS_EXPR)
1905 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1906 return arg0;
1908 else if (code == MULT_EXPR)
1910 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1911 return arg1;
1914 /* Handle general case of two integer constants. For sizetype
1915 constant calculations we always want to know about overflow,
1916 even in the unsigned case. */
1917 tree res = int_const_binop_1 (code, arg0, arg1, -1);
1918 if (res != NULL_TREE)
1919 return res;
1922 return fold_build2_loc (loc, code, type, arg0, arg1);
1925 /* Given two values, either both of sizetype or both of bitsizetype,
1926 compute the difference between the two values. Return the value
1927 in signed type corresponding to the type of the operands. */
1929 tree
1930 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1932 tree type = TREE_TYPE (arg0);
1933 tree ctype;
1935 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1936 TREE_TYPE (arg1)));
1938 /* If the type is already signed, just do the simple thing. */
1939 if (!TYPE_UNSIGNED (type))
1940 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1942 if (type == sizetype)
1943 ctype = ssizetype;
1944 else if (type == bitsizetype)
1945 ctype = sbitsizetype;
1946 else
1947 ctype = signed_type_for (type);
1949 /* If either operand is not a constant, do the conversions to the signed
1950 type and subtract. The hardware will do the right thing with any
1951 overflow in the subtraction. */
1952 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1953 return size_binop_loc (loc, MINUS_EXPR,
1954 fold_convert_loc (loc, ctype, arg0),
1955 fold_convert_loc (loc, ctype, arg1));
1957 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1958 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1959 overflow) and negate (which can't either). Special-case a result
1960 of zero while we're here. */
1961 if (tree_int_cst_equal (arg0, arg1))
1962 return build_int_cst (ctype, 0);
1963 else if (tree_int_cst_lt (arg1, arg0))
1964 return fold_convert_loc (loc, ctype,
1965 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1966 else
1967 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1968 fold_convert_loc (loc, ctype,
1969 size_binop_loc (loc,
1970 MINUS_EXPR,
1971 arg1, arg0)));
1974 /* A subroutine of fold_convert_const handling conversions of an
1975 INTEGER_CST to another integer type. */
1977 static tree
1978 fold_convert_const_int_from_int (tree type, const_tree arg1)
1980 /* Given an integer constant, make new constant with new type,
1981 appropriately sign-extended or truncated. Use widest_int
1982 so that any extension is done according ARG1's type. */
1983 return force_fit_type (type, wi::to_widest (arg1),
1984 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1985 TREE_OVERFLOW (arg1));
1988 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1989 to an integer type. */
1991 static tree
1992 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1994 bool overflow = false;
1995 tree t;
1997 /* The following code implements the floating point to integer
1998 conversion rules required by the Java Language Specification,
1999 that IEEE NaNs are mapped to zero and values that overflow
2000 the target precision saturate, i.e. values greater than
2001 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2002 are mapped to INT_MIN. These semantics are allowed by the
2003 C and C++ standards that simply state that the behavior of
2004 FP-to-integer conversion is unspecified upon overflow. */
2006 wide_int val;
2007 REAL_VALUE_TYPE r;
2008 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2010 switch (code)
2012 case FIX_TRUNC_EXPR:
2013 real_trunc (&r, VOIDmode, &x);
2014 break;
2016 default:
2017 gcc_unreachable ();
2020 /* If R is NaN, return zero and show we have an overflow. */
2021 if (REAL_VALUE_ISNAN (r))
2023 overflow = true;
2024 val = wi::zero (TYPE_PRECISION (type));
2027 /* See if R is less than the lower bound or greater than the
2028 upper bound. */
2030 if (! overflow)
2032 tree lt = TYPE_MIN_VALUE (type);
2033 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2034 if (real_less (&r, &l))
2036 overflow = true;
2037 val = wi::to_wide (lt);
2041 if (! overflow)
2043 tree ut = TYPE_MAX_VALUE (type);
2044 if (ut)
2046 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2047 if (real_less (&u, &r))
2049 overflow = true;
2050 val = wi::to_wide (ut);
2055 if (! overflow)
2056 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2058 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2059 return t;
2062 /* A subroutine of fold_convert_const handling conversions of a
2063 FIXED_CST to an integer type. */
2065 static tree
2066 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2068 tree t;
2069 double_int temp, temp_trunc;
2070 scalar_mode mode;
2072 /* Right shift FIXED_CST to temp by fbit. */
2073 temp = TREE_FIXED_CST (arg1).data;
2074 mode = TREE_FIXED_CST (arg1).mode;
2075 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2077 temp = temp.rshift (GET_MODE_FBIT (mode),
2078 HOST_BITS_PER_DOUBLE_INT,
2079 SIGNED_FIXED_POINT_MODE_P (mode));
2081 /* Left shift temp to temp_trunc by fbit. */
2082 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2083 HOST_BITS_PER_DOUBLE_INT,
2084 SIGNED_FIXED_POINT_MODE_P (mode));
2086 else
2088 temp = double_int_zero;
2089 temp_trunc = double_int_zero;
2092 /* If FIXED_CST is negative, we need to round the value toward 0.
2093 By checking if the fractional bits are not zero to add 1 to temp. */
2094 if (SIGNED_FIXED_POINT_MODE_P (mode)
2095 && temp_trunc.is_negative ()
2096 && TREE_FIXED_CST (arg1).data != temp_trunc)
2097 temp += double_int_one;
2099 /* Given a fixed-point constant, make new constant with new type,
2100 appropriately sign-extended or truncated. */
2101 t = force_fit_type (type, temp, -1,
2102 (temp.is_negative ()
2103 && (TYPE_UNSIGNED (type)
2104 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2105 | TREE_OVERFLOW (arg1));
2107 return t;
2110 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2111 to another floating point type. */
2113 static tree
2114 fold_convert_const_real_from_real (tree type, const_tree arg1)
2116 REAL_VALUE_TYPE value;
2117 tree t;
2119 /* Don't perform the operation if flag_signaling_nans is on
2120 and the operand is a signaling NaN. */
2121 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2122 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2123 return NULL_TREE;
2125 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2126 t = build_real (type, value);
2128 /* If converting an infinity or NAN to a representation that doesn't
2129 have one, set the overflow bit so that we can produce some kind of
2130 error message at the appropriate point if necessary. It's not the
2131 most user-friendly message, but it's better than nothing. */
2132 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2133 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2134 TREE_OVERFLOW (t) = 1;
2135 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2136 && !MODE_HAS_NANS (TYPE_MODE (type)))
2137 TREE_OVERFLOW (t) = 1;
2138 /* Regular overflow, conversion produced an infinity in a mode that
2139 can't represent them. */
2140 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2141 && REAL_VALUE_ISINF (value)
2142 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2143 TREE_OVERFLOW (t) = 1;
2144 else
2145 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2146 return t;
2149 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2150 to a floating point type. */
2152 static tree
2153 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2155 REAL_VALUE_TYPE value;
2156 tree t;
2158 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2159 &TREE_FIXED_CST (arg1));
2160 t = build_real (type, value);
2162 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2163 return t;
2166 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2167 to another fixed-point type. */
2169 static tree
2170 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2172 FIXED_VALUE_TYPE value;
2173 tree t;
2174 bool overflow_p;
2176 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2177 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2178 t = build_fixed (type, value);
2180 /* Propagate overflow flags. */
2181 if (overflow_p | TREE_OVERFLOW (arg1))
2182 TREE_OVERFLOW (t) = 1;
2183 return t;
2186 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2187 to a fixed-point type. */
2189 static tree
2190 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2192 FIXED_VALUE_TYPE value;
2193 tree t;
2194 bool overflow_p;
2195 double_int di;
2197 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2199 di.low = TREE_INT_CST_ELT (arg1, 0);
2200 if (TREE_INT_CST_NUNITS (arg1) == 1)
2201 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2202 else
2203 di.high = TREE_INT_CST_ELT (arg1, 1);
2205 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2206 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2207 TYPE_SATURATING (type));
2208 t = build_fixed (type, value);
2210 /* Propagate overflow flags. */
2211 if (overflow_p | TREE_OVERFLOW (arg1))
2212 TREE_OVERFLOW (t) = 1;
2213 return t;
2216 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2217 to a fixed-point type. */
2219 static tree
2220 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2222 FIXED_VALUE_TYPE value;
2223 tree t;
2224 bool overflow_p;
2226 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2227 &TREE_REAL_CST (arg1),
2228 TYPE_SATURATING (type));
2229 t = build_fixed (type, value);
2231 /* Propagate overflow flags. */
2232 if (overflow_p | TREE_OVERFLOW (arg1))
2233 TREE_OVERFLOW (t) = 1;
2234 return t;
2237 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2238 type TYPE. If no simplification can be done return NULL_TREE. */
2240 static tree
2241 fold_convert_const (enum tree_code code, tree type, tree arg1)
2243 tree arg_type = TREE_TYPE (arg1);
2244 if (arg_type == type)
2245 return arg1;
2247 /* We can't widen types, since the runtime value could overflow the
2248 original type before being extended to the new type. */
2249 if (POLY_INT_CST_P (arg1)
2250 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2251 && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2252 return build_poly_int_cst (type,
2253 poly_wide_int::from (poly_int_cst_value (arg1),
2254 TYPE_PRECISION (type),
2255 TYPE_SIGN (arg_type)));
2257 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2258 || TREE_CODE (type) == OFFSET_TYPE)
2260 if (TREE_CODE (arg1) == INTEGER_CST)
2261 return fold_convert_const_int_from_int (type, arg1);
2262 else if (TREE_CODE (arg1) == REAL_CST)
2263 return fold_convert_const_int_from_real (code, type, arg1);
2264 else if (TREE_CODE (arg1) == FIXED_CST)
2265 return fold_convert_const_int_from_fixed (type, arg1);
2267 else if (TREE_CODE (type) == REAL_TYPE)
2269 if (TREE_CODE (arg1) == INTEGER_CST)
2270 return build_real_from_int_cst (type, arg1);
2271 else if (TREE_CODE (arg1) == REAL_CST)
2272 return fold_convert_const_real_from_real (type, arg1);
2273 else if (TREE_CODE (arg1) == FIXED_CST)
2274 return fold_convert_const_real_from_fixed (type, arg1);
2276 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2278 if (TREE_CODE (arg1) == FIXED_CST)
2279 return fold_convert_const_fixed_from_fixed (type, arg1);
2280 else if (TREE_CODE (arg1) == INTEGER_CST)
2281 return fold_convert_const_fixed_from_int (type, arg1);
2282 else if (TREE_CODE (arg1) == REAL_CST)
2283 return fold_convert_const_fixed_from_real (type, arg1);
2285 else if (TREE_CODE (type) == VECTOR_TYPE)
2287 if (TREE_CODE (arg1) == VECTOR_CST
2288 && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2290 tree elttype = TREE_TYPE (type);
2291 tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2292 /* We can't handle steps directly when extending, since the
2293 values need to wrap at the original precision first. */
2294 bool step_ok_p
2295 = (INTEGRAL_TYPE_P (elttype)
2296 && INTEGRAL_TYPE_P (arg1_elttype)
2297 && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2298 tree_vector_builder v;
2299 if (!v.new_unary_operation (type, arg1, step_ok_p))
2300 return NULL_TREE;
2301 unsigned int len = v.encoded_nelts ();
2302 for (unsigned int i = 0; i < len; ++i)
2304 tree elt = VECTOR_CST_ELT (arg1, i);
2305 tree cvt = fold_convert_const (code, elttype, elt);
2306 if (cvt == NULL_TREE)
2307 return NULL_TREE;
2308 v.quick_push (cvt);
2310 return v.build ();
2313 return NULL_TREE;
2316 /* Construct a vector of zero elements of vector type TYPE. */
2318 static tree
2319 build_zero_vector (tree type)
2321 tree t;
2323 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2324 return build_vector_from_val (type, t);
2327 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2329 bool
2330 fold_convertible_p (const_tree type, const_tree arg)
2332 tree orig = TREE_TYPE (arg);
2334 if (type == orig)
2335 return true;
2337 if (TREE_CODE (arg) == ERROR_MARK
2338 || TREE_CODE (type) == ERROR_MARK
2339 || TREE_CODE (orig) == ERROR_MARK)
2340 return false;
2342 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2343 return true;
2345 switch (TREE_CODE (type))
2347 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2348 case POINTER_TYPE: case REFERENCE_TYPE:
2349 case OFFSET_TYPE:
2350 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2351 || TREE_CODE (orig) == OFFSET_TYPE);
2353 case REAL_TYPE:
2354 case FIXED_POINT_TYPE:
2355 case VECTOR_TYPE:
2356 case VOID_TYPE:
2357 return TREE_CODE (type) == TREE_CODE (orig);
2359 default:
2360 return false;
2364 /* Convert expression ARG to type TYPE. Used by the middle-end for
2365 simple conversions in preference to calling the front-end's convert. */
2367 tree
2368 fold_convert_loc (location_t loc, tree type, tree arg)
2370 tree orig = TREE_TYPE (arg);
2371 tree tem;
2373 if (type == orig)
2374 return arg;
2376 if (TREE_CODE (arg) == ERROR_MARK
2377 || TREE_CODE (type) == ERROR_MARK
2378 || TREE_CODE (orig) == ERROR_MARK)
2379 return error_mark_node;
2381 switch (TREE_CODE (type))
2383 case POINTER_TYPE:
2384 case REFERENCE_TYPE:
2385 /* Handle conversions between pointers to different address spaces. */
2386 if (POINTER_TYPE_P (orig)
2387 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2388 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2389 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2390 /* fall through */
2392 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2393 case OFFSET_TYPE:
2394 if (TREE_CODE (arg) == INTEGER_CST)
2396 tem = fold_convert_const (NOP_EXPR, type, arg);
2397 if (tem != NULL_TREE)
2398 return tem;
2400 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2401 || TREE_CODE (orig) == OFFSET_TYPE)
2402 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2403 if (TREE_CODE (orig) == COMPLEX_TYPE)
2404 return fold_convert_loc (loc, type,
2405 fold_build1_loc (loc, REALPART_EXPR,
2406 TREE_TYPE (orig), arg));
2407 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2408 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2409 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2411 case REAL_TYPE:
2412 if (TREE_CODE (arg) == INTEGER_CST)
2414 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2415 if (tem != NULL_TREE)
2416 return tem;
2418 else if (TREE_CODE (arg) == REAL_CST)
2420 tem = fold_convert_const (NOP_EXPR, type, arg);
2421 if (tem != NULL_TREE)
2422 return tem;
2424 else if (TREE_CODE (arg) == FIXED_CST)
2426 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2427 if (tem != NULL_TREE)
2428 return tem;
2431 switch (TREE_CODE (orig))
2433 case INTEGER_TYPE:
2434 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2435 case POINTER_TYPE: case REFERENCE_TYPE:
2436 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2438 case REAL_TYPE:
2439 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2441 case FIXED_POINT_TYPE:
2442 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2444 case COMPLEX_TYPE:
2445 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2446 return fold_convert_loc (loc, type, tem);
2448 default:
2449 gcc_unreachable ();
2452 case FIXED_POINT_TYPE:
2453 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2454 || TREE_CODE (arg) == REAL_CST)
2456 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2457 if (tem != NULL_TREE)
2458 goto fold_convert_exit;
2461 switch (TREE_CODE (orig))
2463 case FIXED_POINT_TYPE:
2464 case INTEGER_TYPE:
2465 case ENUMERAL_TYPE:
2466 case BOOLEAN_TYPE:
2467 case REAL_TYPE:
2468 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2470 case COMPLEX_TYPE:
2471 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2472 return fold_convert_loc (loc, type, tem);
2474 default:
2475 gcc_unreachable ();
2478 case COMPLEX_TYPE:
2479 switch (TREE_CODE (orig))
2481 case INTEGER_TYPE:
2482 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2483 case POINTER_TYPE: case REFERENCE_TYPE:
2484 case REAL_TYPE:
2485 case FIXED_POINT_TYPE:
2486 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2487 fold_convert_loc (loc, TREE_TYPE (type), arg),
2488 fold_convert_loc (loc, TREE_TYPE (type),
2489 integer_zero_node));
2490 case COMPLEX_TYPE:
2492 tree rpart, ipart;
2494 if (TREE_CODE (arg) == COMPLEX_EXPR)
2496 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2497 TREE_OPERAND (arg, 0));
2498 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2499 TREE_OPERAND (arg, 1));
2500 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2503 arg = save_expr (arg);
2504 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2505 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2506 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2507 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2508 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2511 default:
2512 gcc_unreachable ();
2515 case VECTOR_TYPE:
2516 if (integer_zerop (arg))
2517 return build_zero_vector (type);
2518 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2519 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2520 || TREE_CODE (orig) == VECTOR_TYPE);
2521 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2523 case VOID_TYPE:
2524 tem = fold_ignored_result (arg);
2525 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2527 default:
2528 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2529 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2530 gcc_unreachable ();
2532 fold_convert_exit:
2533 protected_set_expr_location_unshare (tem, loc);
2534 return tem;
2537 /* Return false if expr can be assumed not to be an lvalue, true
2538 otherwise. */
2540 static bool
2541 maybe_lvalue_p (const_tree x)
2543 /* We only need to wrap lvalue tree codes. */
2544 switch (TREE_CODE (x))
2546 case VAR_DECL:
2547 case PARM_DECL:
2548 case RESULT_DECL:
2549 case LABEL_DECL:
2550 case FUNCTION_DECL:
2551 case SSA_NAME:
2553 case COMPONENT_REF:
2554 case MEM_REF:
2555 case INDIRECT_REF:
2556 case ARRAY_REF:
2557 case ARRAY_RANGE_REF:
2558 case BIT_FIELD_REF:
2559 case OBJ_TYPE_REF:
2561 case REALPART_EXPR:
2562 case IMAGPART_EXPR:
2563 case PREINCREMENT_EXPR:
2564 case PREDECREMENT_EXPR:
2565 case SAVE_EXPR:
2566 case TRY_CATCH_EXPR:
2567 case WITH_CLEANUP_EXPR:
2568 case COMPOUND_EXPR:
2569 case MODIFY_EXPR:
2570 case TARGET_EXPR:
2571 case COND_EXPR:
2572 case BIND_EXPR:
2573 break;
2575 default:
2576 /* Assume the worst for front-end tree codes. */
2577 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2578 break;
2579 return false;
2582 return true;
2585 /* Return an expr equal to X but certainly not valid as an lvalue. */
2587 tree
2588 non_lvalue_loc (location_t loc, tree x)
2590 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2591 us. */
2592 if (in_gimple_form)
2593 return x;
2595 if (! maybe_lvalue_p (x))
2596 return x;
2597 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2600 /* When pedantic, return an expr equal to X but certainly not valid as a
2601 pedantic lvalue. Otherwise, return X. */
2603 static tree
2604 pedantic_non_lvalue_loc (location_t loc, tree x)
2606 return protected_set_expr_location_unshare (x, loc);
2609 /* Given a tree comparison code, return the code that is the logical inverse.
2610 It is generally not safe to do this for floating-point comparisons, except
2611 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2612 ERROR_MARK in this case. */
2614 enum tree_code
2615 invert_tree_comparison (enum tree_code code, bool honor_nans)
2617 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2618 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2619 return ERROR_MARK;
2621 switch (code)
2623 case EQ_EXPR:
2624 return NE_EXPR;
2625 case NE_EXPR:
2626 return EQ_EXPR;
2627 case GT_EXPR:
2628 return honor_nans ? UNLE_EXPR : LE_EXPR;
2629 case GE_EXPR:
2630 return honor_nans ? UNLT_EXPR : LT_EXPR;
2631 case LT_EXPR:
2632 return honor_nans ? UNGE_EXPR : GE_EXPR;
2633 case LE_EXPR:
2634 return honor_nans ? UNGT_EXPR : GT_EXPR;
2635 case LTGT_EXPR:
2636 return UNEQ_EXPR;
2637 case UNEQ_EXPR:
2638 return LTGT_EXPR;
2639 case UNGT_EXPR:
2640 return LE_EXPR;
2641 case UNGE_EXPR:
2642 return LT_EXPR;
2643 case UNLT_EXPR:
2644 return GE_EXPR;
2645 case UNLE_EXPR:
2646 return GT_EXPR;
2647 case ORDERED_EXPR:
2648 return UNORDERED_EXPR;
2649 case UNORDERED_EXPR:
2650 return ORDERED_EXPR;
2651 default:
2652 gcc_unreachable ();
2656 /* Similar, but return the comparison that results if the operands are
2657 swapped. This is safe for floating-point. */
2659 enum tree_code
2660 swap_tree_comparison (enum tree_code code)
2662 switch (code)
2664 case EQ_EXPR:
2665 case NE_EXPR:
2666 case ORDERED_EXPR:
2667 case UNORDERED_EXPR:
2668 case LTGT_EXPR:
2669 case UNEQ_EXPR:
2670 return code;
2671 case GT_EXPR:
2672 return LT_EXPR;
2673 case GE_EXPR:
2674 return LE_EXPR;
2675 case LT_EXPR:
2676 return GT_EXPR;
2677 case LE_EXPR:
2678 return GE_EXPR;
2679 case UNGT_EXPR:
2680 return UNLT_EXPR;
2681 case UNGE_EXPR:
2682 return UNLE_EXPR;
2683 case UNLT_EXPR:
2684 return UNGT_EXPR;
2685 case UNLE_EXPR:
2686 return UNGE_EXPR;
2687 default:
2688 gcc_unreachable ();
2693 /* Convert a comparison tree code from an enum tree_code representation
2694 into a compcode bit-based encoding. This function is the inverse of
2695 compcode_to_comparison. */
2697 static enum comparison_code
2698 comparison_to_compcode (enum tree_code code)
2700 switch (code)
2702 case LT_EXPR:
2703 return COMPCODE_LT;
2704 case EQ_EXPR:
2705 return COMPCODE_EQ;
2706 case LE_EXPR:
2707 return COMPCODE_LE;
2708 case GT_EXPR:
2709 return COMPCODE_GT;
2710 case NE_EXPR:
2711 return COMPCODE_NE;
2712 case GE_EXPR:
2713 return COMPCODE_GE;
2714 case ORDERED_EXPR:
2715 return COMPCODE_ORD;
2716 case UNORDERED_EXPR:
2717 return COMPCODE_UNORD;
2718 case UNLT_EXPR:
2719 return COMPCODE_UNLT;
2720 case UNEQ_EXPR:
2721 return COMPCODE_UNEQ;
2722 case UNLE_EXPR:
2723 return COMPCODE_UNLE;
2724 case UNGT_EXPR:
2725 return COMPCODE_UNGT;
2726 case LTGT_EXPR:
2727 return COMPCODE_LTGT;
2728 case UNGE_EXPR:
2729 return COMPCODE_UNGE;
2730 default:
2731 gcc_unreachable ();
2735 /* Convert a compcode bit-based encoding of a comparison operator back
2736 to GCC's enum tree_code representation. This function is the
2737 inverse of comparison_to_compcode. */
2739 static enum tree_code
2740 compcode_to_comparison (enum comparison_code code)
2742 switch (code)
2744 case COMPCODE_LT:
2745 return LT_EXPR;
2746 case COMPCODE_EQ:
2747 return EQ_EXPR;
2748 case COMPCODE_LE:
2749 return LE_EXPR;
2750 case COMPCODE_GT:
2751 return GT_EXPR;
2752 case COMPCODE_NE:
2753 return NE_EXPR;
2754 case COMPCODE_GE:
2755 return GE_EXPR;
2756 case COMPCODE_ORD:
2757 return ORDERED_EXPR;
2758 case COMPCODE_UNORD:
2759 return UNORDERED_EXPR;
2760 case COMPCODE_UNLT:
2761 return UNLT_EXPR;
2762 case COMPCODE_UNEQ:
2763 return UNEQ_EXPR;
2764 case COMPCODE_UNLE:
2765 return UNLE_EXPR;
2766 case COMPCODE_UNGT:
2767 return UNGT_EXPR;
2768 case COMPCODE_LTGT:
2769 return LTGT_EXPR;
2770 case COMPCODE_UNGE:
2771 return UNGE_EXPR;
2772 default:
2773 gcc_unreachable ();
2777 /* Return a tree for the comparison which is the combination of
2778 doing the AND or OR (depending on CODE) of the two operations LCODE
2779 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2780 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2781 if this makes the transformation invalid. */
2783 tree
2784 combine_comparisons (location_t loc,
2785 enum tree_code code, enum tree_code lcode,
2786 enum tree_code rcode, tree truth_type,
2787 tree ll_arg, tree lr_arg)
2789 bool honor_nans = HONOR_NANS (ll_arg);
2790 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2791 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2792 int compcode;
2794 switch (code)
2796 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2797 compcode = lcompcode & rcompcode;
2798 break;
2800 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2801 compcode = lcompcode | rcompcode;
2802 break;
2804 default:
2805 return NULL_TREE;
2808 if (!honor_nans)
2810 /* Eliminate unordered comparisons, as well as LTGT and ORD
2811 which are not used unless the mode has NaNs. */
2812 compcode &= ~COMPCODE_UNORD;
2813 if (compcode == COMPCODE_LTGT)
2814 compcode = COMPCODE_NE;
2815 else if (compcode == COMPCODE_ORD)
2816 compcode = COMPCODE_TRUE;
2818 else if (flag_trapping_math)
2820 /* Check that the original operation and the optimized ones will trap
2821 under the same condition. */
2822 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2823 && (lcompcode != COMPCODE_EQ)
2824 && (lcompcode != COMPCODE_ORD);
2825 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2826 && (rcompcode != COMPCODE_EQ)
2827 && (rcompcode != COMPCODE_ORD);
2828 bool trap = (compcode & COMPCODE_UNORD) == 0
2829 && (compcode != COMPCODE_EQ)
2830 && (compcode != COMPCODE_ORD);
2832 /* In a short-circuited boolean expression the LHS might be
2833 such that the RHS, if evaluated, will never trap. For
2834 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2835 if neither x nor y is NaN. (This is a mixed blessing: for
2836 example, the expression above will never trap, hence
2837 optimizing it to x < y would be invalid). */
2838 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2839 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2840 rtrap = false;
2842 /* If the comparison was short-circuited, and only the RHS
2843 trapped, we may now generate a spurious trap. */
2844 if (rtrap && !ltrap
2845 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2846 return NULL_TREE;
2848 /* If we changed the conditions that cause a trap, we lose. */
2849 if ((ltrap || rtrap) != trap)
2850 return NULL_TREE;
2853 if (compcode == COMPCODE_TRUE)
2854 return constant_boolean_node (true, truth_type);
2855 else if (compcode == COMPCODE_FALSE)
2856 return constant_boolean_node (false, truth_type);
2857 else
2859 enum tree_code tcode;
2861 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2862 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2866 /* Return nonzero if two operands (typically of the same tree node)
2867 are necessarily equal. FLAGS modifies behavior as follows:
2869 If OEP_ONLY_CONST is set, only return nonzero for constants.
2870 This function tests whether the operands are indistinguishable;
2871 it does not test whether they are equal using C's == operation.
2872 The distinction is important for IEEE floating point, because
2873 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2874 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2876 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2877 even though it may hold multiple values during a function.
2878 This is because a GCC tree node guarantees that nothing else is
2879 executed between the evaluation of its "operands" (which may often
2880 be evaluated in arbitrary order). Hence if the operands themselves
2881 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2882 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2883 unset means assuming isochronic (or instantaneous) tree equivalence.
2884 Unless comparing arbitrary expression trees, such as from different
2885 statements, this flag can usually be left unset.
2887 If OEP_PURE_SAME is set, then pure functions with identical arguments
2888 are considered the same. It is used when the caller has other ways
2889 to ensure that global memory is unchanged in between.
2891 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2892 not values of expressions.
2894 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2895 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2897 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2898 any operand with side effect. This is unnecesarily conservative in the
2899 case we know that arg0 and arg1 are in disjoint code paths (such as in
2900 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2901 addresses with TREE_CONSTANT flag set so we know that &var == &var
2902 even if var is volatile. */
2905 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2907 /* When checking, verify at the outermost operand_equal_p call that
2908 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2909 hash value. */
2910 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2912 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2914 if (arg0 != arg1)
2916 inchash::hash hstate0 (0), hstate1 (0);
2917 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2918 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2919 hashval_t h0 = hstate0.end ();
2920 hashval_t h1 = hstate1.end ();
2921 gcc_assert (h0 == h1);
2923 return 1;
2925 else
2926 return 0;
2929 /* If either is ERROR_MARK, they aren't equal. */
2930 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2931 || TREE_TYPE (arg0) == error_mark_node
2932 || TREE_TYPE (arg1) == error_mark_node)
2933 return 0;
2935 /* Similar, if either does not have a type (like a released SSA name),
2936 they aren't equal. */
2937 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2938 return 0;
2940 /* We cannot consider pointers to different address space equal. */
2941 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2942 && POINTER_TYPE_P (TREE_TYPE (arg1))
2943 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2944 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2945 return 0;
2947 /* Check equality of integer constants before bailing out due to
2948 precision differences. */
2949 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2951 /* Address of INTEGER_CST is not defined; check that we did not forget
2952 to drop the OEP_ADDRESS_OF flags. */
2953 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2954 return tree_int_cst_equal (arg0, arg1);
2957 if (!(flags & OEP_ADDRESS_OF))
2959 /* If both types don't have the same signedness, then we can't consider
2960 them equal. We must check this before the STRIP_NOPS calls
2961 because they may change the signedness of the arguments. As pointers
2962 strictly don't have a signedness, require either two pointers or
2963 two non-pointers as well. */
2964 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2965 || POINTER_TYPE_P (TREE_TYPE (arg0))
2966 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2967 return 0;
2969 /* If both types don't have the same precision, then it is not safe
2970 to strip NOPs. */
2971 if (element_precision (TREE_TYPE (arg0))
2972 != element_precision (TREE_TYPE (arg1)))
2973 return 0;
2975 STRIP_NOPS (arg0);
2976 STRIP_NOPS (arg1);
2978 #if 0
2979 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2980 sanity check once the issue is solved. */
2981 else
2982 /* Addresses of conversions and SSA_NAMEs (and many other things)
2983 are not defined. Check that we did not forget to drop the
2984 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2985 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2986 && TREE_CODE (arg0) != SSA_NAME);
2987 #endif
2989 /* In case both args are comparisons but with different comparison
2990 code, try to swap the comparison operands of one arg to produce
2991 a match and compare that variant. */
2992 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2993 && COMPARISON_CLASS_P (arg0)
2994 && COMPARISON_CLASS_P (arg1))
2996 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2998 if (TREE_CODE (arg0) == swap_code)
2999 return operand_equal_p (TREE_OPERAND (arg0, 0),
3000 TREE_OPERAND (arg1, 1), flags)
3001 && operand_equal_p (TREE_OPERAND (arg0, 1),
3002 TREE_OPERAND (arg1, 0), flags);
3005 if (TREE_CODE (arg0) != TREE_CODE (arg1))
3007 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3008 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3010 else if (flags & OEP_ADDRESS_OF)
3012 /* If we are interested in comparing addresses ignore
3013 MEM_REF wrappings of the base that can appear just for
3014 TBAA reasons. */
3015 if (TREE_CODE (arg0) == MEM_REF
3016 && DECL_P (arg1)
3017 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3018 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3019 && integer_zerop (TREE_OPERAND (arg0, 1)))
3020 return 1;
3021 else if (TREE_CODE (arg1) == MEM_REF
3022 && DECL_P (arg0)
3023 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3024 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3025 && integer_zerop (TREE_OPERAND (arg1, 1)))
3026 return 1;
3027 return 0;
3029 else
3030 return 0;
3033 /* When not checking adddresses, this is needed for conversions and for
3034 COMPONENT_REF. Might as well play it safe and always test this. */
3035 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3036 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3037 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
3038 && !(flags & OEP_ADDRESS_OF)))
3039 return 0;
3041 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3042 We don't care about side effects in that case because the SAVE_EXPR
3043 takes care of that for us. In all other cases, two expressions are
3044 equal if they have no side effects. If we have two identical
3045 expressions with side effects that should be treated the same due
3046 to the only side effects being identical SAVE_EXPR's, that will
3047 be detected in the recursive calls below.
3048 If we are taking an invariant address of two identical objects
3049 they are necessarily equal as well. */
3050 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3051 && (TREE_CODE (arg0) == SAVE_EXPR
3052 || (flags & OEP_MATCH_SIDE_EFFECTS)
3053 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3054 return 1;
3056 /* Next handle constant cases, those for which we can return 1 even
3057 if ONLY_CONST is set. */
3058 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3059 switch (TREE_CODE (arg0))
3061 case INTEGER_CST:
3062 return tree_int_cst_equal (arg0, arg1);
3064 case FIXED_CST:
3065 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3066 TREE_FIXED_CST (arg1));
3068 case REAL_CST:
3069 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3070 return 1;
3073 if (!HONOR_SIGNED_ZEROS (arg0))
3075 /* If we do not distinguish between signed and unsigned zero,
3076 consider them equal. */
3077 if (real_zerop (arg0) && real_zerop (arg1))
3078 return 1;
3080 return 0;
3082 case VECTOR_CST:
3084 if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3085 != VECTOR_CST_LOG2_NPATTERNS (arg1))
3086 return 0;
3088 if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3089 != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3090 return 0;
3092 unsigned int count = vector_cst_encoded_nelts (arg0);
3093 for (unsigned int i = 0; i < count; ++i)
3094 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3095 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3096 return 0;
3097 return 1;
3100 case COMPLEX_CST:
3101 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3102 flags)
3103 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3104 flags));
3106 case STRING_CST:
3107 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3108 && ! memcmp (TREE_STRING_POINTER (arg0),
3109 TREE_STRING_POINTER (arg1),
3110 TREE_STRING_LENGTH (arg0)));
3112 case ADDR_EXPR:
3113 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3114 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3115 flags | OEP_ADDRESS_OF
3116 | OEP_MATCH_SIDE_EFFECTS);
3117 case CONSTRUCTOR:
3118 /* In GIMPLE empty constructors are allowed in initializers of
3119 aggregates. */
3120 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
3121 default:
3122 break;
3125 if (flags & OEP_ONLY_CONST)
3126 return 0;
3128 /* Define macros to test an operand from arg0 and arg1 for equality and a
3129 variant that allows null and views null as being different from any
3130 non-null value. In the latter case, if either is null, the both
3131 must be; otherwise, do the normal comparison. */
3132 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3133 TREE_OPERAND (arg1, N), flags)
3135 #define OP_SAME_WITH_NULL(N) \
3136 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3137 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3139 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3141 case tcc_unary:
3142 /* Two conversions are equal only if signedness and modes match. */
3143 switch (TREE_CODE (arg0))
3145 CASE_CONVERT:
3146 case FIX_TRUNC_EXPR:
3147 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3148 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3149 return 0;
3150 break;
3151 default:
3152 break;
3155 return OP_SAME (0);
3158 case tcc_comparison:
3159 case tcc_binary:
3160 if (OP_SAME (0) && OP_SAME (1))
3161 return 1;
3163 /* For commutative ops, allow the other order. */
3164 return (commutative_tree_code (TREE_CODE (arg0))
3165 && operand_equal_p (TREE_OPERAND (arg0, 0),
3166 TREE_OPERAND (arg1, 1), flags)
3167 && operand_equal_p (TREE_OPERAND (arg0, 1),
3168 TREE_OPERAND (arg1, 0), flags));
3170 case tcc_reference:
3171 /* If either of the pointer (or reference) expressions we are
3172 dereferencing contain a side effect, these cannot be equal,
3173 but their addresses can be. */
3174 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3175 && (TREE_SIDE_EFFECTS (arg0)
3176 || TREE_SIDE_EFFECTS (arg1)))
3177 return 0;
3179 switch (TREE_CODE (arg0))
3181 case INDIRECT_REF:
3182 if (!(flags & OEP_ADDRESS_OF)
3183 && (TYPE_ALIGN (TREE_TYPE (arg0))
3184 != TYPE_ALIGN (TREE_TYPE (arg1))))
3185 return 0;
3186 flags &= ~OEP_ADDRESS_OF;
3187 return OP_SAME (0);
3189 case IMAGPART_EXPR:
3190 /* Require the same offset. */
3191 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3192 TYPE_SIZE (TREE_TYPE (arg1)),
3193 flags & ~OEP_ADDRESS_OF))
3194 return 0;
3196 /* Fallthru. */
3197 case REALPART_EXPR:
3198 case VIEW_CONVERT_EXPR:
3199 return OP_SAME (0);
3201 case TARGET_MEM_REF:
3202 case MEM_REF:
3203 if (!(flags & OEP_ADDRESS_OF))
3205 /* Require equal access sizes */
3206 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3207 && (!TYPE_SIZE (TREE_TYPE (arg0))
3208 || !TYPE_SIZE (TREE_TYPE (arg1))
3209 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3210 TYPE_SIZE (TREE_TYPE (arg1)),
3211 flags)))
3212 return 0;
3213 /* Verify that access happens in similar types. */
3214 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3215 return 0;
3216 /* Verify that accesses are TBAA compatible. */
3217 if (!alias_ptr_types_compatible_p
3218 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3219 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3220 || (MR_DEPENDENCE_CLIQUE (arg0)
3221 != MR_DEPENDENCE_CLIQUE (arg1))
3222 || (MR_DEPENDENCE_BASE (arg0)
3223 != MR_DEPENDENCE_BASE (arg1)))
3224 return 0;
3225 /* Verify that alignment is compatible. */
3226 if (TYPE_ALIGN (TREE_TYPE (arg0))
3227 != TYPE_ALIGN (TREE_TYPE (arg1)))
3228 return 0;
3230 flags &= ~OEP_ADDRESS_OF;
3231 return (OP_SAME (0) && OP_SAME (1)
3232 /* TARGET_MEM_REF require equal extra operands. */
3233 && (TREE_CODE (arg0) != TARGET_MEM_REF
3234 || (OP_SAME_WITH_NULL (2)
3235 && OP_SAME_WITH_NULL (3)
3236 && OP_SAME_WITH_NULL (4))));
3238 case ARRAY_REF:
3239 case ARRAY_RANGE_REF:
3240 if (!OP_SAME (0))
3241 return 0;
3242 flags &= ~OEP_ADDRESS_OF;
3243 /* Compare the array index by value if it is constant first as we
3244 may have different types but same value here. */
3245 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3246 TREE_OPERAND (arg1, 1))
3247 || OP_SAME (1))
3248 && OP_SAME_WITH_NULL (2)
3249 && OP_SAME_WITH_NULL (3)
3250 /* Compare low bound and element size as with OEP_ADDRESS_OF
3251 we have to account for the offset of the ref. */
3252 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3253 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3254 || (operand_equal_p (array_ref_low_bound
3255 (CONST_CAST_TREE (arg0)),
3256 array_ref_low_bound
3257 (CONST_CAST_TREE (arg1)), flags)
3258 && operand_equal_p (array_ref_element_size
3259 (CONST_CAST_TREE (arg0)),
3260 array_ref_element_size
3261 (CONST_CAST_TREE (arg1)),
3262 flags))));
3264 case COMPONENT_REF:
3265 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3266 may be NULL when we're called to compare MEM_EXPRs. */
3267 if (!OP_SAME_WITH_NULL (0)
3268 || !OP_SAME (1))
3269 return 0;
3270 flags &= ~OEP_ADDRESS_OF;
3271 return OP_SAME_WITH_NULL (2);
3273 case BIT_FIELD_REF:
3274 if (!OP_SAME (0))
3275 return 0;
3276 flags &= ~OEP_ADDRESS_OF;
3277 return OP_SAME (1) && OP_SAME (2);
3279 default:
3280 return 0;
3283 case tcc_expression:
3284 switch (TREE_CODE (arg0))
3286 case ADDR_EXPR:
3287 /* Be sure we pass right ADDRESS_OF flag. */
3288 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3289 return operand_equal_p (TREE_OPERAND (arg0, 0),
3290 TREE_OPERAND (arg1, 0),
3291 flags | OEP_ADDRESS_OF);
3293 case TRUTH_NOT_EXPR:
3294 return OP_SAME (0);
3296 case TRUTH_ANDIF_EXPR:
3297 case TRUTH_ORIF_EXPR:
3298 return OP_SAME (0) && OP_SAME (1);
3300 case FMA_EXPR:
3301 case WIDEN_MULT_PLUS_EXPR:
3302 case WIDEN_MULT_MINUS_EXPR:
3303 if (!OP_SAME (2))
3304 return 0;
3305 /* The multiplcation operands are commutative. */
3306 /* FALLTHRU */
3308 case TRUTH_AND_EXPR:
3309 case TRUTH_OR_EXPR:
3310 case TRUTH_XOR_EXPR:
3311 if (OP_SAME (0) && OP_SAME (1))
3312 return 1;
3314 /* Otherwise take into account this is a commutative operation. */
3315 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3316 TREE_OPERAND (arg1, 1), flags)
3317 && operand_equal_p (TREE_OPERAND (arg0, 1),
3318 TREE_OPERAND (arg1, 0), flags));
3320 case COND_EXPR:
3321 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3322 return 0;
3323 flags &= ~OEP_ADDRESS_OF;
3324 return OP_SAME (0);
3326 case BIT_INSERT_EXPR:
3327 /* BIT_INSERT_EXPR has an implict operand as the type precision
3328 of op1. Need to check to make sure they are the same. */
3329 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3330 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3331 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3332 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3333 return false;
3334 /* FALLTHRU */
3336 case VEC_COND_EXPR:
3337 case DOT_PROD_EXPR:
3338 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3340 case MODIFY_EXPR:
3341 case INIT_EXPR:
3342 case COMPOUND_EXPR:
3343 case PREDECREMENT_EXPR:
3344 case PREINCREMENT_EXPR:
3345 case POSTDECREMENT_EXPR:
3346 case POSTINCREMENT_EXPR:
3347 if (flags & OEP_LEXICOGRAPHIC)
3348 return OP_SAME (0) && OP_SAME (1);
3349 return 0;
3351 case CLEANUP_POINT_EXPR:
3352 case EXPR_STMT:
3353 if (flags & OEP_LEXICOGRAPHIC)
3354 return OP_SAME (0);
3355 return 0;
3357 default:
3358 return 0;
3361 case tcc_vl_exp:
3362 switch (TREE_CODE (arg0))
3364 case CALL_EXPR:
3365 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3366 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3367 /* If not both CALL_EXPRs are either internal or normal function
3368 functions, then they are not equal. */
3369 return 0;
3370 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3372 /* If the CALL_EXPRs call different internal functions, then they
3373 are not equal. */
3374 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3375 return 0;
3377 else
3379 /* If the CALL_EXPRs call different functions, then they are not
3380 equal. */
3381 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3382 flags))
3383 return 0;
3386 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3388 unsigned int cef = call_expr_flags (arg0);
3389 if (flags & OEP_PURE_SAME)
3390 cef &= ECF_CONST | ECF_PURE;
3391 else
3392 cef &= ECF_CONST;
3393 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3394 return 0;
3397 /* Now see if all the arguments are the same. */
3399 const_call_expr_arg_iterator iter0, iter1;
3400 const_tree a0, a1;
3401 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3402 a1 = first_const_call_expr_arg (arg1, &iter1);
3403 a0 && a1;
3404 a0 = next_const_call_expr_arg (&iter0),
3405 a1 = next_const_call_expr_arg (&iter1))
3406 if (! operand_equal_p (a0, a1, flags))
3407 return 0;
3409 /* If we get here and both argument lists are exhausted
3410 then the CALL_EXPRs are equal. */
3411 return ! (a0 || a1);
3413 default:
3414 return 0;
3417 case tcc_declaration:
3418 /* Consider __builtin_sqrt equal to sqrt. */
3419 return (TREE_CODE (arg0) == FUNCTION_DECL
3420 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3421 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3422 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3424 case tcc_exceptional:
3425 if (TREE_CODE (arg0) == CONSTRUCTOR)
3427 /* In GIMPLE constructors are used only to build vectors from
3428 elements. Individual elements in the constructor must be
3429 indexed in increasing order and form an initial sequence.
3431 We make no effort to compare constructors in generic.
3432 (see sem_variable::equals in ipa-icf which can do so for
3433 constants). */
3434 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3435 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3436 return 0;
3438 /* Be sure that vectors constructed have the same representation.
3439 We only tested element precision and modes to match.
3440 Vectors may be BLKmode and thus also check that the number of
3441 parts match. */
3442 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
3443 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))))
3444 return 0;
3446 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3447 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3448 unsigned int len = vec_safe_length (v0);
3450 if (len != vec_safe_length (v1))
3451 return 0;
3453 for (unsigned int i = 0; i < len; i++)
3455 constructor_elt *c0 = &(*v0)[i];
3456 constructor_elt *c1 = &(*v1)[i];
3458 if (!operand_equal_p (c0->value, c1->value, flags)
3459 /* In GIMPLE the indexes can be either NULL or matching i.
3460 Double check this so we won't get false
3461 positives for GENERIC. */
3462 || (c0->index
3463 && (TREE_CODE (c0->index) != INTEGER_CST
3464 || !compare_tree_int (c0->index, i)))
3465 || (c1->index
3466 && (TREE_CODE (c1->index) != INTEGER_CST
3467 || !compare_tree_int (c1->index, i))))
3468 return 0;
3470 return 1;
3472 else if (TREE_CODE (arg0) == STATEMENT_LIST
3473 && (flags & OEP_LEXICOGRAPHIC))
3475 /* Compare the STATEMENT_LISTs. */
3476 tree_stmt_iterator tsi1, tsi2;
3477 tree body1 = CONST_CAST_TREE (arg0);
3478 tree body2 = CONST_CAST_TREE (arg1);
3479 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3480 tsi_next (&tsi1), tsi_next (&tsi2))
3482 /* The lists don't have the same number of statements. */
3483 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3484 return 0;
3485 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3486 return 1;
3487 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3488 flags & (OEP_LEXICOGRAPHIC
3489 | OEP_NO_HASH_CHECK)))
3490 return 0;
3493 return 0;
3495 case tcc_statement:
3496 switch (TREE_CODE (arg0))
3498 case RETURN_EXPR:
3499 if (flags & OEP_LEXICOGRAPHIC)
3500 return OP_SAME_WITH_NULL (0);
3501 return 0;
3502 case DEBUG_BEGIN_STMT:
3503 if (flags & OEP_LEXICOGRAPHIC)
3504 return 1;
3505 return 0;
3506 default:
3507 return 0;
3510 default:
3511 return 0;
3514 #undef OP_SAME
3515 #undef OP_SAME_WITH_NULL
3518 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
3519 with a different signedness or a narrower precision. */
3521 static bool
3522 operand_equal_for_comparison_p (tree arg0, tree arg1)
3524 if (operand_equal_p (arg0, arg1, 0))
3525 return true;
3527 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3528 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3529 return false;
3531 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3532 and see if the inner values are the same. This removes any
3533 signedness comparison, which doesn't matter here. */
3534 tree op0 = arg0;
3535 tree op1 = arg1;
3536 STRIP_NOPS (op0);
3537 STRIP_NOPS (op1);
3538 if (operand_equal_p (op0, op1, 0))
3539 return true;
3541 /* Discard a single widening conversion from ARG1 and see if the inner
3542 value is the same as ARG0. */
3543 if (CONVERT_EXPR_P (arg1)
3544 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3545 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3546 < TYPE_PRECISION (TREE_TYPE (arg1))
3547 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
3548 return true;
3550 return false;
3553 /* See if ARG is an expression that is either a comparison or is performing
3554 arithmetic on comparisons. The comparisons must only be comparing
3555 two different values, which will be stored in *CVAL1 and *CVAL2; if
3556 they are nonzero it means that some operands have already been found.
3557 No variables may be used anywhere else in the expression except in the
3558 comparisons.
3560 If this is true, return 1. Otherwise, return zero. */
3562 static int
3563 twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
3565 enum tree_code code = TREE_CODE (arg);
3566 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3568 /* We can handle some of the tcc_expression cases here. */
3569 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3570 tclass = tcc_unary;
3571 else if (tclass == tcc_expression
3572 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3573 || code == COMPOUND_EXPR))
3574 tclass = tcc_binary;
3576 switch (tclass)
3578 case tcc_unary:
3579 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
3581 case tcc_binary:
3582 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
3583 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
3585 case tcc_constant:
3586 return 1;
3588 case tcc_expression:
3589 if (code == COND_EXPR)
3590 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
3591 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
3592 && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
3593 return 0;
3595 case tcc_comparison:
3596 /* First see if we can handle the first operand, then the second. For
3597 the second operand, we know *CVAL1 can't be zero. It must be that
3598 one side of the comparison is each of the values; test for the
3599 case where this isn't true by failing if the two operands
3600 are the same. */
3602 if (operand_equal_p (TREE_OPERAND (arg, 0),
3603 TREE_OPERAND (arg, 1), 0))
3604 return 0;
3606 if (*cval1 == 0)
3607 *cval1 = TREE_OPERAND (arg, 0);
3608 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3610 else if (*cval2 == 0)
3611 *cval2 = TREE_OPERAND (arg, 0);
3612 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3614 else
3615 return 0;
3617 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3619 else if (*cval2 == 0)
3620 *cval2 = TREE_OPERAND (arg, 1);
3621 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3623 else
3624 return 0;
3626 return 1;
3628 default:
3629 return 0;
3633 /* ARG is a tree that is known to contain just arithmetic operations and
3634 comparisons. Evaluate the operations in the tree substituting NEW0 for
3635 any occurrence of OLD0 as an operand of a comparison and likewise for
3636 NEW1 and OLD1. */
3638 static tree
3639 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3640 tree old1, tree new1)
3642 tree type = TREE_TYPE (arg);
3643 enum tree_code code = TREE_CODE (arg);
3644 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3646 /* We can handle some of the tcc_expression cases here. */
3647 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3648 tclass = tcc_unary;
3649 else if (tclass == tcc_expression
3650 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3651 tclass = tcc_binary;
3653 switch (tclass)
3655 case tcc_unary:
3656 return fold_build1_loc (loc, code, type,
3657 eval_subst (loc, TREE_OPERAND (arg, 0),
3658 old0, new0, old1, new1));
3660 case tcc_binary:
3661 return fold_build2_loc (loc, code, type,
3662 eval_subst (loc, TREE_OPERAND (arg, 0),
3663 old0, new0, old1, new1),
3664 eval_subst (loc, TREE_OPERAND (arg, 1),
3665 old0, new0, old1, new1));
3667 case tcc_expression:
3668 switch (code)
3670 case SAVE_EXPR:
3671 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3672 old1, new1);
3674 case COMPOUND_EXPR:
3675 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3676 old1, new1);
3678 case COND_EXPR:
3679 return fold_build3_loc (loc, code, type,
3680 eval_subst (loc, TREE_OPERAND (arg, 0),
3681 old0, new0, old1, new1),
3682 eval_subst (loc, TREE_OPERAND (arg, 1),
3683 old0, new0, old1, new1),
3684 eval_subst (loc, TREE_OPERAND (arg, 2),
3685 old0, new0, old1, new1));
3686 default:
3687 break;
3689 /* Fall through - ??? */
3691 case tcc_comparison:
3693 tree arg0 = TREE_OPERAND (arg, 0);
3694 tree arg1 = TREE_OPERAND (arg, 1);
3696 /* We need to check both for exact equality and tree equality. The
3697 former will be true if the operand has a side-effect. In that
3698 case, we know the operand occurred exactly once. */
3700 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3701 arg0 = new0;
3702 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3703 arg0 = new1;
3705 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3706 arg1 = new0;
3707 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3708 arg1 = new1;
3710 return fold_build2_loc (loc, code, type, arg0, arg1);
3713 default:
3714 return arg;
3718 /* Return a tree for the case when the result of an expression is RESULT
3719 converted to TYPE and OMITTED was previously an operand of the expression
3720 but is now not needed (e.g., we folded OMITTED * 0).
3722 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3723 the conversion of RESULT to TYPE. */
3725 tree
3726 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3728 tree t = fold_convert_loc (loc, type, result);
3730 /* If the resulting operand is an empty statement, just return the omitted
3731 statement casted to void. */
3732 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3733 return build1_loc (loc, NOP_EXPR, void_type_node,
3734 fold_ignored_result (omitted));
3736 if (TREE_SIDE_EFFECTS (omitted))
3737 return build2_loc (loc, COMPOUND_EXPR, type,
3738 fold_ignored_result (omitted), t);
3740 return non_lvalue_loc (loc, t);
3743 /* Return a tree for the case when the result of an expression is RESULT
3744 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3745 of the expression but are now not needed.
3747 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3748 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3749 evaluated before OMITTED2. Otherwise, if neither has side effects,
3750 just do the conversion of RESULT to TYPE. */
3752 tree
3753 omit_two_operands_loc (location_t loc, tree type, tree result,
3754 tree omitted1, tree omitted2)
3756 tree t = fold_convert_loc (loc, type, result);
3758 if (TREE_SIDE_EFFECTS (omitted2))
3759 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3760 if (TREE_SIDE_EFFECTS (omitted1))
3761 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3763 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3767 /* Return a simplified tree node for the truth-negation of ARG. This
3768 never alters ARG itself. We assume that ARG is an operation that
3769 returns a truth value (0 or 1).
3771 FIXME: one would think we would fold the result, but it causes
3772 problems with the dominator optimizer. */
3774 static tree
3775 fold_truth_not_expr (location_t loc, tree arg)
3777 tree type = TREE_TYPE (arg);
3778 enum tree_code code = TREE_CODE (arg);
3779 location_t loc1, loc2;
3781 /* If this is a comparison, we can simply invert it, except for
3782 floating-point non-equality comparisons, in which case we just
3783 enclose a TRUTH_NOT_EXPR around what we have. */
3785 if (TREE_CODE_CLASS (code) == tcc_comparison)
3787 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3788 if (FLOAT_TYPE_P (op_type)
3789 && flag_trapping_math
3790 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3791 && code != NE_EXPR && code != EQ_EXPR)
3792 return NULL_TREE;
3794 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3795 if (code == ERROR_MARK)
3796 return NULL_TREE;
3798 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3799 TREE_OPERAND (arg, 1));
3800 if (TREE_NO_WARNING (arg))
3801 TREE_NO_WARNING (ret) = 1;
3802 return ret;
3805 switch (code)
3807 case INTEGER_CST:
3808 return constant_boolean_node (integer_zerop (arg), type);
3810 case TRUTH_AND_EXPR:
3811 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3812 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3813 return build2_loc (loc, TRUTH_OR_EXPR, type,
3814 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3815 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3817 case TRUTH_OR_EXPR:
3818 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3819 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3820 return build2_loc (loc, TRUTH_AND_EXPR, type,
3821 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3822 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3824 case TRUTH_XOR_EXPR:
3825 /* Here we can invert either operand. We invert the first operand
3826 unless the second operand is a TRUTH_NOT_EXPR in which case our
3827 result is the XOR of the first operand with the inside of the
3828 negation of the second operand. */
3830 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3831 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3832 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3833 else
3834 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3835 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3836 TREE_OPERAND (arg, 1));
3838 case TRUTH_ANDIF_EXPR:
3839 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3840 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3841 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3842 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3843 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3845 case TRUTH_ORIF_EXPR:
3846 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3847 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3848 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3849 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3850 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3852 case TRUTH_NOT_EXPR:
3853 return TREE_OPERAND (arg, 0);
3855 case COND_EXPR:
3857 tree arg1 = TREE_OPERAND (arg, 1);
3858 tree arg2 = TREE_OPERAND (arg, 2);
3860 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3861 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3863 /* A COND_EXPR may have a throw as one operand, which
3864 then has void type. Just leave void operands
3865 as they are. */
3866 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3867 VOID_TYPE_P (TREE_TYPE (arg1))
3868 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3869 VOID_TYPE_P (TREE_TYPE (arg2))
3870 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3873 case COMPOUND_EXPR:
3874 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3875 return build2_loc (loc, COMPOUND_EXPR, type,
3876 TREE_OPERAND (arg, 0),
3877 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3879 case NON_LVALUE_EXPR:
3880 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3881 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3883 CASE_CONVERT:
3884 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3885 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3887 /* fall through */
3889 case FLOAT_EXPR:
3890 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3891 return build1_loc (loc, TREE_CODE (arg), type,
3892 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3894 case BIT_AND_EXPR:
3895 if (!integer_onep (TREE_OPERAND (arg, 1)))
3896 return NULL_TREE;
3897 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3899 case SAVE_EXPR:
3900 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3902 case CLEANUP_POINT_EXPR:
3903 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3904 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3905 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3907 default:
3908 return NULL_TREE;
3912 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3913 assume that ARG is an operation that returns a truth value (0 or 1
3914 for scalars, 0 or -1 for vectors). Return the folded expression if
3915 folding is successful. Otherwise, return NULL_TREE. */
3917 static tree
3918 fold_invert_truthvalue (location_t loc, tree arg)
3920 tree type = TREE_TYPE (arg);
3921 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3922 ? BIT_NOT_EXPR
3923 : TRUTH_NOT_EXPR,
3924 type, arg);
3927 /* Return a simplified tree node for the truth-negation of ARG. This
3928 never alters ARG itself. We assume that ARG is an operation that
3929 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3931 tree
3932 invert_truthvalue_loc (location_t loc, tree arg)
3934 if (TREE_CODE (arg) == ERROR_MARK)
3935 return arg;
3937 tree type = TREE_TYPE (arg);
3938 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3939 ? BIT_NOT_EXPR
3940 : TRUTH_NOT_EXPR,
3941 type, arg);
3944 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3945 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3946 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3947 is the original memory reference used to preserve the alias set of
3948 the access. */
3950 static tree
3951 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3952 HOST_WIDE_INT bitsize, poly_int64 bitpos,
3953 int unsignedp, int reversep)
3955 tree result, bftype;
3957 /* Attempt not to lose the access path if possible. */
3958 if (TREE_CODE (orig_inner) == COMPONENT_REF)
3960 tree ninner = TREE_OPERAND (orig_inner, 0);
3961 machine_mode nmode;
3962 poly_int64 nbitsize, nbitpos;
3963 tree noffset;
3964 int nunsignedp, nreversep, nvolatilep = 0;
3965 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
3966 &noffset, &nmode, &nunsignedp,
3967 &nreversep, &nvolatilep);
3968 if (base == inner
3969 && noffset == NULL_TREE
3970 && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
3971 && !reversep
3972 && !nreversep
3973 && !nvolatilep)
3975 inner = ninner;
3976 bitpos -= nbitpos;
3980 alias_set_type iset = get_alias_set (orig_inner);
3981 if (iset == 0 && get_alias_set (inner) != iset)
3982 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3983 build_fold_addr_expr (inner),
3984 build_int_cst (ptr_type_node, 0));
3986 if (known_eq (bitpos, 0) && !reversep)
3988 tree size = TYPE_SIZE (TREE_TYPE (inner));
3989 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3990 || POINTER_TYPE_P (TREE_TYPE (inner)))
3991 && tree_fits_shwi_p (size)
3992 && tree_to_shwi (size) == bitsize)
3993 return fold_convert_loc (loc, type, inner);
3996 bftype = type;
3997 if (TYPE_PRECISION (bftype) != bitsize
3998 || TYPE_UNSIGNED (bftype) == !unsignedp)
3999 bftype = build_nonstandard_integer_type (bitsize, 0);
4001 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4002 bitsize_int (bitsize), bitsize_int (bitpos));
4003 REF_REVERSE_STORAGE_ORDER (result) = reversep;
4005 if (bftype != type)
4006 result = fold_convert_loc (loc, type, result);
4008 return result;
4011 /* Optimize a bit-field compare.
4013 There are two cases: First is a compare against a constant and the
4014 second is a comparison of two items where the fields are at the same
4015 bit position relative to the start of a chunk (byte, halfword, word)
4016 large enough to contain it. In these cases we can avoid the shift
4017 implicit in bitfield extractions.
4019 For constants, we emit a compare of the shifted constant with the
4020 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4021 compared. For two fields at the same position, we do the ANDs with the
4022 similar mask and compare the result of the ANDs.
4024 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4025 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4026 are the left and right operands of the comparison, respectively.
4028 If the optimization described above can be done, we return the resulting
4029 tree. Otherwise we return zero. */
4031 static tree
4032 optimize_bit_field_compare (location_t loc, enum tree_code code,
4033 tree compare_type, tree lhs, tree rhs)
4035 poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4036 HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4037 tree type = TREE_TYPE (lhs);
4038 tree unsigned_type;
4039 int const_p = TREE_CODE (rhs) == INTEGER_CST;
4040 machine_mode lmode, rmode;
4041 scalar_int_mode nmode;
4042 int lunsignedp, runsignedp;
4043 int lreversep, rreversep;
4044 int lvolatilep = 0, rvolatilep = 0;
4045 tree linner, rinner = NULL_TREE;
4046 tree mask;
4047 tree offset;
4049 /* Get all the information about the extractions being done. If the bit size
4050 is the same as the size of the underlying object, we aren't doing an
4051 extraction at all and so can do nothing. We also don't want to
4052 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4053 then will no longer be able to replace it. */
4054 linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4055 &lunsignedp, &lreversep, &lvolatilep);
4056 if (linner == lhs
4057 || !known_size_p (plbitsize)
4058 || !plbitsize.is_constant (&lbitsize)
4059 || !plbitpos.is_constant (&lbitpos)
4060 || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4061 || offset != 0
4062 || TREE_CODE (linner) == PLACEHOLDER_EXPR
4063 || lvolatilep)
4064 return 0;
4066 if (const_p)
4067 rreversep = lreversep;
4068 else
4070 /* If this is not a constant, we can only do something if bit positions,
4071 sizes, signedness and storage order are the same. */
4072 rinner
4073 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4074 &runsignedp, &rreversep, &rvolatilep);
4076 if (rinner == rhs
4077 || maybe_ne (lbitpos, rbitpos)
4078 || maybe_ne (lbitsize, rbitsize)
4079 || lunsignedp != runsignedp
4080 || lreversep != rreversep
4081 || offset != 0
4082 || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4083 || rvolatilep)
4084 return 0;
4087 /* Honor the C++ memory model and mimic what RTL expansion does. */
4088 poly_uint64 bitstart = 0;
4089 poly_uint64 bitend = 0;
4090 if (TREE_CODE (lhs) == COMPONENT_REF)
4092 get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4093 if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4094 return 0;
4097 /* See if we can find a mode to refer to this field. We should be able to,
4098 but fail if we can't. */
4099 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4100 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4101 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4102 TYPE_ALIGN (TREE_TYPE (rinner))),
4103 BITS_PER_WORD, false, &nmode))
4104 return 0;
4106 /* Set signed and unsigned types of the precision of this mode for the
4107 shifts below. */
4108 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4110 /* Compute the bit position and size for the new reference and our offset
4111 within it. If the new reference is the same size as the original, we
4112 won't optimize anything, so return zero. */
4113 nbitsize = GET_MODE_BITSIZE (nmode);
4114 nbitpos = lbitpos & ~ (nbitsize - 1);
4115 lbitpos -= nbitpos;
4116 if (nbitsize == lbitsize)
4117 return 0;
4119 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4120 lbitpos = nbitsize - lbitsize - lbitpos;
4122 /* Make the mask to be used against the extracted field. */
4123 mask = build_int_cst_type (unsigned_type, -1);
4124 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4125 mask = const_binop (RSHIFT_EXPR, mask,
4126 size_int (nbitsize - lbitsize - lbitpos));
4128 if (! const_p)
4130 if (nbitpos < 0)
4131 return 0;
4133 /* If not comparing with constant, just rework the comparison
4134 and return. */
4135 tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4136 nbitsize, nbitpos, 1, lreversep);
4137 t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4138 tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4139 nbitsize, nbitpos, 1, rreversep);
4140 t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4141 return fold_build2_loc (loc, code, compare_type, t1, t2);
4144 /* Otherwise, we are handling the constant case. See if the constant is too
4145 big for the field. Warn and return a tree for 0 (false) if so. We do
4146 this not only for its own sake, but to avoid having to test for this
4147 error case below. If we didn't, we might generate wrong code.
4149 For unsigned fields, the constant shifted right by the field length should
4150 be all zero. For signed fields, the high-order bits should agree with
4151 the sign bit. */
4153 if (lunsignedp)
4155 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4157 warning (0, "comparison is always %d due to width of bit-field",
4158 code == NE_EXPR);
4159 return constant_boolean_node (code == NE_EXPR, compare_type);
4162 else
4164 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4165 if (tem != 0 && tem != -1)
4167 warning (0, "comparison is always %d due to width of bit-field",
4168 code == NE_EXPR);
4169 return constant_boolean_node (code == NE_EXPR, compare_type);
4173 if (nbitpos < 0)
4174 return 0;
4176 /* Single-bit compares should always be against zero. */
4177 if (lbitsize == 1 && ! integer_zerop (rhs))
4179 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4180 rhs = build_int_cst (type, 0);
4183 /* Make a new bitfield reference, shift the constant over the
4184 appropriate number of bits and mask it with the computed mask
4185 (in case this was a signed field). If we changed it, make a new one. */
4186 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4187 nbitsize, nbitpos, 1, lreversep);
4189 rhs = const_binop (BIT_AND_EXPR,
4190 const_binop (LSHIFT_EXPR,
4191 fold_convert_loc (loc, unsigned_type, rhs),
4192 size_int (lbitpos)),
4193 mask);
4195 lhs = build2_loc (loc, code, compare_type,
4196 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4197 return lhs;
4200 /* Subroutine for fold_truth_andor_1: decode a field reference.
4202 If EXP is a comparison reference, we return the innermost reference.
4204 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4205 set to the starting bit number.
4207 If the innermost field can be completely contained in a mode-sized
4208 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4210 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4211 otherwise it is not changed.
4213 *PUNSIGNEDP is set to the signedness of the field.
4215 *PREVERSEP is set to the storage order of the field.
4217 *PMASK is set to the mask used. This is either contained in a
4218 BIT_AND_EXPR or derived from the width of the field.
4220 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4222 Return 0 if this is not a component reference or is one that we can't
4223 do anything with. */
4225 static tree
4226 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4227 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4228 int *punsignedp, int *preversep, int *pvolatilep,
4229 tree *pmask, tree *pand_mask)
4231 tree exp = *exp_;
4232 tree outer_type = 0;
4233 tree and_mask = 0;
4234 tree mask, inner, offset;
4235 tree unsigned_type;
4236 unsigned int precision;
4238 /* All the optimizations using this function assume integer fields.
4239 There are problems with FP fields since the type_for_size call
4240 below can fail for, e.g., XFmode. */
4241 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4242 return 0;
4244 /* We are interested in the bare arrangement of bits, so strip everything
4245 that doesn't affect the machine mode. However, record the type of the
4246 outermost expression if it may matter below. */
4247 if (CONVERT_EXPR_P (exp)
4248 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4249 outer_type = TREE_TYPE (exp);
4250 STRIP_NOPS (exp);
4252 if (TREE_CODE (exp) == BIT_AND_EXPR)
4254 and_mask = TREE_OPERAND (exp, 1);
4255 exp = TREE_OPERAND (exp, 0);
4256 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4257 if (TREE_CODE (and_mask) != INTEGER_CST)
4258 return 0;
4261 poly_int64 poly_bitsize, poly_bitpos;
4262 inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
4263 pmode, punsignedp, preversep, pvolatilep);
4264 if ((inner == exp && and_mask == 0)
4265 || !poly_bitsize.is_constant (pbitsize)
4266 || !poly_bitpos.is_constant (pbitpos)
4267 || *pbitsize < 0
4268 || offset != 0
4269 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4270 /* Reject out-of-bound accesses (PR79731). */
4271 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4272 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4273 *pbitpos + *pbitsize) < 0))
4274 return 0;
4276 *exp_ = exp;
4278 /* If the number of bits in the reference is the same as the bitsize of
4279 the outer type, then the outer type gives the signedness. Otherwise
4280 (in case of a small bitfield) the signedness is unchanged. */
4281 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4282 *punsignedp = TYPE_UNSIGNED (outer_type);
4284 /* Compute the mask to access the bitfield. */
4285 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4286 precision = TYPE_PRECISION (unsigned_type);
4288 mask = build_int_cst_type (unsigned_type, -1);
4290 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4291 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4293 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4294 if (and_mask != 0)
4295 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4296 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4298 *pmask = mask;
4299 *pand_mask = and_mask;
4300 return inner;
4303 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4304 bit positions and MASK is SIGNED. */
4306 static int
4307 all_ones_mask_p (const_tree mask, unsigned int size)
4309 tree type = TREE_TYPE (mask);
4310 unsigned int precision = TYPE_PRECISION (type);
4312 /* If this function returns true when the type of the mask is
4313 UNSIGNED, then there will be errors. In particular see
4314 gcc.c-torture/execute/990326-1.c. There does not appear to be
4315 any documentation paper trail as to why this is so. But the pre
4316 wide-int worked with that restriction and it has been preserved
4317 here. */
4318 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4319 return false;
4321 return wi::mask (size, false, precision) == wi::to_wide (mask);
4324 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4325 represents the sign bit of EXP's type. If EXP represents a sign
4326 or zero extension, also test VAL against the unextended type.
4327 The return value is the (sub)expression whose sign bit is VAL,
4328 or NULL_TREE otherwise. */
4330 tree
4331 sign_bit_p (tree exp, const_tree val)
4333 int width;
4334 tree t;
4336 /* Tree EXP must have an integral type. */
4337 t = TREE_TYPE (exp);
4338 if (! INTEGRAL_TYPE_P (t))
4339 return NULL_TREE;
4341 /* Tree VAL must be an integer constant. */
4342 if (TREE_CODE (val) != INTEGER_CST
4343 || TREE_OVERFLOW (val))
4344 return NULL_TREE;
4346 width = TYPE_PRECISION (t);
4347 if (wi::only_sign_bit_p (wi::to_wide (val), width))
4348 return exp;
4350 /* Handle extension from a narrower type. */
4351 if (TREE_CODE (exp) == NOP_EXPR
4352 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4353 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4355 return NULL_TREE;
4358 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4359 to be evaluated unconditionally. */
4361 static int
4362 simple_operand_p (const_tree exp)
4364 /* Strip any conversions that don't change the machine mode. */
4365 STRIP_NOPS (exp);
4367 return (CONSTANT_CLASS_P (exp)
4368 || TREE_CODE (exp) == SSA_NAME
4369 || (DECL_P (exp)
4370 && ! TREE_ADDRESSABLE (exp)
4371 && ! TREE_THIS_VOLATILE (exp)
4372 && ! DECL_NONLOCAL (exp)
4373 /* Don't regard global variables as simple. They may be
4374 allocated in ways unknown to the compiler (shared memory,
4375 #pragma weak, etc). */
4376 && ! TREE_PUBLIC (exp)
4377 && ! DECL_EXTERNAL (exp)
4378 /* Weakrefs are not safe to be read, since they can be NULL.
4379 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4380 have DECL_WEAK flag set. */
4381 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4382 /* Loading a static variable is unduly expensive, but global
4383 registers aren't expensive. */
4384 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4387 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4388 to be evaluated unconditionally.
4389 I addition to simple_operand_p, we assume that comparisons, conversions,
4390 and logic-not operations are simple, if their operands are simple, too. */
4392 static bool
4393 simple_operand_p_2 (tree exp)
4395 enum tree_code code;
4397 if (TREE_SIDE_EFFECTS (exp)
4398 || tree_could_trap_p (exp))
4399 return false;
4401 while (CONVERT_EXPR_P (exp))
4402 exp = TREE_OPERAND (exp, 0);
4404 code = TREE_CODE (exp);
4406 if (TREE_CODE_CLASS (code) == tcc_comparison)
4407 return (simple_operand_p (TREE_OPERAND (exp, 0))
4408 && simple_operand_p (TREE_OPERAND (exp, 1)));
4410 if (code == TRUTH_NOT_EXPR)
4411 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4413 return simple_operand_p (exp);
4417 /* The following functions are subroutines to fold_range_test and allow it to
4418 try to change a logical combination of comparisons into a range test.
4420 For example, both
4421 X == 2 || X == 3 || X == 4 || X == 5
4423 X >= 2 && X <= 5
4424 are converted to
4425 (unsigned) (X - 2) <= 3
4427 We describe each set of comparisons as being either inside or outside
4428 a range, using a variable named like IN_P, and then describe the
4429 range with a lower and upper bound. If one of the bounds is omitted,
4430 it represents either the highest or lowest value of the type.
4432 In the comments below, we represent a range by two numbers in brackets
4433 preceded by a "+" to designate being inside that range, or a "-" to
4434 designate being outside that range, so the condition can be inverted by
4435 flipping the prefix. An omitted bound is represented by a "-". For
4436 example, "- [-, 10]" means being outside the range starting at the lowest
4437 possible value and ending at 10, in other words, being greater than 10.
4438 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4439 always false.
4441 We set up things so that the missing bounds are handled in a consistent
4442 manner so neither a missing bound nor "true" and "false" need to be
4443 handled using a special case. */
4445 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4446 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4447 and UPPER1_P are nonzero if the respective argument is an upper bound
4448 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4449 must be specified for a comparison. ARG1 will be converted to ARG0's
4450 type if both are specified. */
4452 static tree
4453 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4454 tree arg1, int upper1_p)
4456 tree tem;
4457 int result;
4458 int sgn0, sgn1;
4460 /* If neither arg represents infinity, do the normal operation.
4461 Else, if not a comparison, return infinity. Else handle the special
4462 comparison rules. Note that most of the cases below won't occur, but
4463 are handled for consistency. */
4465 if (arg0 != 0 && arg1 != 0)
4467 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4468 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4469 STRIP_NOPS (tem);
4470 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4473 if (TREE_CODE_CLASS (code) != tcc_comparison)
4474 return 0;
4476 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4477 for neither. In real maths, we cannot assume open ended ranges are
4478 the same. But, this is computer arithmetic, where numbers are finite.
4479 We can therefore make the transformation of any unbounded range with
4480 the value Z, Z being greater than any representable number. This permits
4481 us to treat unbounded ranges as equal. */
4482 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4483 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4484 switch (code)
4486 case EQ_EXPR:
4487 result = sgn0 == sgn1;
4488 break;
4489 case NE_EXPR:
4490 result = sgn0 != sgn1;
4491 break;
4492 case LT_EXPR:
4493 result = sgn0 < sgn1;
4494 break;
4495 case LE_EXPR:
4496 result = sgn0 <= sgn1;
4497 break;
4498 case GT_EXPR:
4499 result = sgn0 > sgn1;
4500 break;
4501 case GE_EXPR:
4502 result = sgn0 >= sgn1;
4503 break;
4504 default:
4505 gcc_unreachable ();
4508 return constant_boolean_node (result, type);
4511 /* Helper routine for make_range. Perform one step for it, return
4512 new expression if the loop should continue or NULL_TREE if it should
4513 stop. */
4515 tree
4516 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4517 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4518 bool *strict_overflow_p)
4520 tree arg0_type = TREE_TYPE (arg0);
4521 tree n_low, n_high, low = *p_low, high = *p_high;
4522 int in_p = *p_in_p, n_in_p;
4524 switch (code)
4526 case TRUTH_NOT_EXPR:
4527 /* We can only do something if the range is testing for zero. */
4528 if (low == NULL_TREE || high == NULL_TREE
4529 || ! integer_zerop (low) || ! integer_zerop (high))
4530 return NULL_TREE;
4531 *p_in_p = ! in_p;
4532 return arg0;
4534 case EQ_EXPR: case NE_EXPR:
4535 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4536 /* We can only do something if the range is testing for zero
4537 and if the second operand is an integer constant. Note that
4538 saying something is "in" the range we make is done by
4539 complementing IN_P since it will set in the initial case of
4540 being not equal to zero; "out" is leaving it alone. */
4541 if (low == NULL_TREE || high == NULL_TREE
4542 || ! integer_zerop (low) || ! integer_zerop (high)
4543 || TREE_CODE (arg1) != INTEGER_CST)
4544 return NULL_TREE;
4546 switch (code)
4548 case NE_EXPR: /* - [c, c] */
4549 low = high = arg1;
4550 break;
4551 case EQ_EXPR: /* + [c, c] */
4552 in_p = ! in_p, low = high = arg1;
4553 break;
4554 case GT_EXPR: /* - [-, c] */
4555 low = 0, high = arg1;
4556 break;
4557 case GE_EXPR: /* + [c, -] */
4558 in_p = ! in_p, low = arg1, high = 0;
4559 break;
4560 case LT_EXPR: /* - [c, -] */
4561 low = arg1, high = 0;
4562 break;
4563 case LE_EXPR: /* + [-, c] */
4564 in_p = ! in_p, low = 0, high = arg1;
4565 break;
4566 default:
4567 gcc_unreachable ();
4570 /* If this is an unsigned comparison, we also know that EXP is
4571 greater than or equal to zero. We base the range tests we make
4572 on that fact, so we record it here so we can parse existing
4573 range tests. We test arg0_type since often the return type
4574 of, e.g. EQ_EXPR, is boolean. */
4575 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4577 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4578 in_p, low, high, 1,
4579 build_int_cst (arg0_type, 0),
4580 NULL_TREE))
4581 return NULL_TREE;
4583 in_p = n_in_p, low = n_low, high = n_high;
4585 /* If the high bound is missing, but we have a nonzero low
4586 bound, reverse the range so it goes from zero to the low bound
4587 minus 1. */
4588 if (high == 0 && low && ! integer_zerop (low))
4590 in_p = ! in_p;
4591 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4592 build_int_cst (TREE_TYPE (low), 1), 0);
4593 low = build_int_cst (arg0_type, 0);
4597 *p_low = low;
4598 *p_high = high;
4599 *p_in_p = in_p;
4600 return arg0;
4602 case NEGATE_EXPR:
4603 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4604 low and high are non-NULL, then normalize will DTRT. */
4605 if (!TYPE_UNSIGNED (arg0_type)
4606 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4608 if (low == NULL_TREE)
4609 low = TYPE_MIN_VALUE (arg0_type);
4610 if (high == NULL_TREE)
4611 high = TYPE_MAX_VALUE (arg0_type);
4614 /* (-x) IN [a,b] -> x in [-b, -a] */
4615 n_low = range_binop (MINUS_EXPR, exp_type,
4616 build_int_cst (exp_type, 0),
4617 0, high, 1);
4618 n_high = range_binop (MINUS_EXPR, exp_type,
4619 build_int_cst (exp_type, 0),
4620 0, low, 0);
4621 if (n_high != 0 && TREE_OVERFLOW (n_high))
4622 return NULL_TREE;
4623 goto normalize;
4625 case BIT_NOT_EXPR:
4626 /* ~ X -> -X - 1 */
4627 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4628 build_int_cst (exp_type, 1));
4630 case PLUS_EXPR:
4631 case MINUS_EXPR:
4632 if (TREE_CODE (arg1) != INTEGER_CST)
4633 return NULL_TREE;
4635 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4636 move a constant to the other side. */
4637 if (!TYPE_UNSIGNED (arg0_type)
4638 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4639 return NULL_TREE;
4641 /* If EXP is signed, any overflow in the computation is undefined,
4642 so we don't worry about it so long as our computations on
4643 the bounds don't overflow. For unsigned, overflow is defined
4644 and this is exactly the right thing. */
4645 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4646 arg0_type, low, 0, arg1, 0);
4647 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4648 arg0_type, high, 1, arg1, 0);
4649 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4650 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4651 return NULL_TREE;
4653 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4654 *strict_overflow_p = true;
4656 normalize:
4657 /* Check for an unsigned range which has wrapped around the maximum
4658 value thus making n_high < n_low, and normalize it. */
4659 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4661 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4662 build_int_cst (TREE_TYPE (n_high), 1), 0);
4663 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4664 build_int_cst (TREE_TYPE (n_low), 1), 0);
4666 /* If the range is of the form +/- [ x+1, x ], we won't
4667 be able to normalize it. But then, it represents the
4668 whole range or the empty set, so make it
4669 +/- [ -, - ]. */
4670 if (tree_int_cst_equal (n_low, low)
4671 && tree_int_cst_equal (n_high, high))
4672 low = high = 0;
4673 else
4674 in_p = ! in_p;
4676 else
4677 low = n_low, high = n_high;
4679 *p_low = low;
4680 *p_high = high;
4681 *p_in_p = in_p;
4682 return arg0;
4684 CASE_CONVERT:
4685 case NON_LVALUE_EXPR:
4686 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4687 return NULL_TREE;
4689 if (! INTEGRAL_TYPE_P (arg0_type)
4690 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4691 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4692 return NULL_TREE;
4694 n_low = low, n_high = high;
4696 if (n_low != 0)
4697 n_low = fold_convert_loc (loc, arg0_type, n_low);
4699 if (n_high != 0)
4700 n_high = fold_convert_loc (loc, arg0_type, n_high);
4702 /* If we're converting arg0 from an unsigned type, to exp,
4703 a signed type, we will be doing the comparison as unsigned.
4704 The tests above have already verified that LOW and HIGH
4705 are both positive.
4707 So we have to ensure that we will handle large unsigned
4708 values the same way that the current signed bounds treat
4709 negative values. */
4711 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4713 tree high_positive;
4714 tree equiv_type;
4715 /* For fixed-point modes, we need to pass the saturating flag
4716 as the 2nd parameter. */
4717 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4718 equiv_type
4719 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4720 TYPE_SATURATING (arg0_type));
4721 else
4722 equiv_type
4723 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4725 /* A range without an upper bound is, naturally, unbounded.
4726 Since convert would have cropped a very large value, use
4727 the max value for the destination type. */
4728 high_positive
4729 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4730 : TYPE_MAX_VALUE (arg0_type);
4732 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4733 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4734 fold_convert_loc (loc, arg0_type,
4735 high_positive),
4736 build_int_cst (arg0_type, 1));
4738 /* If the low bound is specified, "and" the range with the
4739 range for which the original unsigned value will be
4740 positive. */
4741 if (low != 0)
4743 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4744 1, fold_convert_loc (loc, arg0_type,
4745 integer_zero_node),
4746 high_positive))
4747 return NULL_TREE;
4749 in_p = (n_in_p == in_p);
4751 else
4753 /* Otherwise, "or" the range with the range of the input
4754 that will be interpreted as negative. */
4755 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4756 1, fold_convert_loc (loc, arg0_type,
4757 integer_zero_node),
4758 high_positive))
4759 return NULL_TREE;
4761 in_p = (in_p != n_in_p);
4765 *p_low = n_low;
4766 *p_high = n_high;
4767 *p_in_p = in_p;
4768 return arg0;
4770 default:
4771 return NULL_TREE;
4775 /* Given EXP, a logical expression, set the range it is testing into
4776 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4777 actually being tested. *PLOW and *PHIGH will be made of the same
4778 type as the returned expression. If EXP is not a comparison, we
4779 will most likely not be returning a useful value and range. Set
4780 *STRICT_OVERFLOW_P to true if the return value is only valid
4781 because signed overflow is undefined; otherwise, do not change
4782 *STRICT_OVERFLOW_P. */
4784 tree
4785 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4786 bool *strict_overflow_p)
4788 enum tree_code code;
4789 tree arg0, arg1 = NULL_TREE;
4790 tree exp_type, nexp;
4791 int in_p;
4792 tree low, high;
4793 location_t loc = EXPR_LOCATION (exp);
4795 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4796 and see if we can refine the range. Some of the cases below may not
4797 happen, but it doesn't seem worth worrying about this. We "continue"
4798 the outer loop when we've changed something; otherwise we "break"
4799 the switch, which will "break" the while. */
4801 in_p = 0;
4802 low = high = build_int_cst (TREE_TYPE (exp), 0);
4804 while (1)
4806 code = TREE_CODE (exp);
4807 exp_type = TREE_TYPE (exp);
4808 arg0 = NULL_TREE;
4810 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4812 if (TREE_OPERAND_LENGTH (exp) > 0)
4813 arg0 = TREE_OPERAND (exp, 0);
4814 if (TREE_CODE_CLASS (code) == tcc_binary
4815 || TREE_CODE_CLASS (code) == tcc_comparison
4816 || (TREE_CODE_CLASS (code) == tcc_expression
4817 && TREE_OPERAND_LENGTH (exp) > 1))
4818 arg1 = TREE_OPERAND (exp, 1);
4820 if (arg0 == NULL_TREE)
4821 break;
4823 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4824 &high, &in_p, strict_overflow_p);
4825 if (nexp == NULL_TREE)
4826 break;
4827 exp = nexp;
4830 /* If EXP is a constant, we can evaluate whether this is true or false. */
4831 if (TREE_CODE (exp) == INTEGER_CST)
4833 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4834 exp, 0, low, 0))
4835 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4836 exp, 1, high, 1)));
4837 low = high = 0;
4838 exp = 0;
4841 *pin_p = in_p, *plow = low, *phigh = high;
4842 return exp;
4845 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4846 a bitwise check i.e. when
4847 LOW == 0xXX...X00...0
4848 HIGH == 0xXX...X11...1
4849 Return corresponding mask in MASK and stem in VALUE. */
4851 static bool
4852 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4853 tree *value)
4855 if (TREE_CODE (low) != INTEGER_CST
4856 || TREE_CODE (high) != INTEGER_CST)
4857 return false;
4859 unsigned prec = TYPE_PRECISION (type);
4860 wide_int lo = wi::to_wide (low, prec);
4861 wide_int hi = wi::to_wide (high, prec);
4863 wide_int end_mask = lo ^ hi;
4864 if ((end_mask & (end_mask + 1)) != 0
4865 || (lo & end_mask) != 0)
4866 return false;
4868 wide_int stem_mask = ~end_mask;
4869 wide_int stem = lo & stem_mask;
4870 if (stem != (hi & stem_mask))
4871 return false;
4873 *mask = wide_int_to_tree (type, stem_mask);
4874 *value = wide_int_to_tree (type, stem);
4876 return true;
4879 /* Helper routine for build_range_check and match.pd. Return the type to
4880 perform the check or NULL if it shouldn't be optimized. */
4882 tree
4883 range_check_type (tree etype)
4885 /* First make sure that arithmetics in this type is valid, then make sure
4886 that it wraps around. */
4887 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4888 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4889 TYPE_UNSIGNED (etype));
4891 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4893 tree utype, minv, maxv;
4895 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4896 for the type in question, as we rely on this here. */
4897 utype = unsigned_type_for (etype);
4898 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4899 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4900 build_int_cst (TREE_TYPE (maxv), 1), 1);
4901 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4903 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4904 minv, 1, maxv, 1)))
4905 etype = utype;
4906 else
4907 return NULL_TREE;
4909 return etype;
4912 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4913 type, TYPE, return an expression to test if EXP is in (or out of, depending
4914 on IN_P) the range. Return 0 if the test couldn't be created. */
4916 tree
4917 build_range_check (location_t loc, tree type, tree exp, int in_p,
4918 tree low, tree high)
4920 tree etype = TREE_TYPE (exp), mask, value;
4922 /* Disable this optimization for function pointer expressions
4923 on targets that require function pointer canonicalization. */
4924 if (targetm.have_canonicalize_funcptr_for_compare ()
4925 && TREE_CODE (etype) == POINTER_TYPE
4926 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4927 return NULL_TREE;
4929 if (! in_p)
4931 value = build_range_check (loc, type, exp, 1, low, high);
4932 if (value != 0)
4933 return invert_truthvalue_loc (loc, value);
4935 return 0;
4938 if (low == 0 && high == 0)
4939 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4941 if (low == 0)
4942 return fold_build2_loc (loc, LE_EXPR, type, exp,
4943 fold_convert_loc (loc, etype, high));
4945 if (high == 0)
4946 return fold_build2_loc (loc, GE_EXPR, type, exp,
4947 fold_convert_loc (loc, etype, low));
4949 if (operand_equal_p (low, high, 0))
4950 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4951 fold_convert_loc (loc, etype, low));
4953 if (TREE_CODE (exp) == BIT_AND_EXPR
4954 && maskable_range_p (low, high, etype, &mask, &value))
4955 return fold_build2_loc (loc, EQ_EXPR, type,
4956 fold_build2_loc (loc, BIT_AND_EXPR, etype,
4957 exp, mask),
4958 value);
4960 if (integer_zerop (low))
4962 if (! TYPE_UNSIGNED (etype))
4964 etype = unsigned_type_for (etype);
4965 high = fold_convert_loc (loc, etype, high);
4966 exp = fold_convert_loc (loc, etype, exp);
4968 return build_range_check (loc, type, exp, 1, 0, high);
4971 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4972 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4974 int prec = TYPE_PRECISION (etype);
4976 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
4978 if (TYPE_UNSIGNED (etype))
4980 tree signed_etype = signed_type_for (etype);
4981 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4982 etype
4983 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4984 else
4985 etype = signed_etype;
4986 exp = fold_convert_loc (loc, etype, exp);
4988 return fold_build2_loc (loc, GT_EXPR, type, exp,
4989 build_int_cst (etype, 0));
4993 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4994 This requires wrap-around arithmetics for the type of the expression. */
4995 etype = range_check_type (etype);
4996 if (etype == NULL_TREE)
4997 return NULL_TREE;
4999 if (POINTER_TYPE_P (etype))
5000 etype = unsigned_type_for (etype);
5002 high = fold_convert_loc (loc, etype, high);
5003 low = fold_convert_loc (loc, etype, low);
5004 exp = fold_convert_loc (loc, etype, exp);
5006 value = const_binop (MINUS_EXPR, high, low);
5008 if (value != 0 && !TREE_OVERFLOW (value))
5009 return build_range_check (loc, type,
5010 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5011 1, build_int_cst (etype, 0), value);
5013 return 0;
5016 /* Return the predecessor of VAL in its type, handling the infinite case. */
5018 static tree
5019 range_predecessor (tree val)
5021 tree type = TREE_TYPE (val);
5023 if (INTEGRAL_TYPE_P (type)
5024 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5025 return 0;
5026 else
5027 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5028 build_int_cst (TREE_TYPE (val), 1), 0);
5031 /* Return the successor of VAL in its type, handling the infinite case. */
5033 static tree
5034 range_successor (tree val)
5036 tree type = TREE_TYPE (val);
5038 if (INTEGRAL_TYPE_P (type)
5039 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5040 return 0;
5041 else
5042 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5043 build_int_cst (TREE_TYPE (val), 1), 0);
5046 /* Given two ranges, see if we can merge them into one. Return 1 if we
5047 can, 0 if we can't. Set the output range into the specified parameters. */
5049 bool
5050 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5051 tree high0, int in1_p, tree low1, tree high1)
5053 int no_overlap;
5054 int subset;
5055 int temp;
5056 tree tem;
5057 int in_p;
5058 tree low, high;
5059 int lowequal = ((low0 == 0 && low1 == 0)
5060 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5061 low0, 0, low1, 0)));
5062 int highequal = ((high0 == 0 && high1 == 0)
5063 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5064 high0, 1, high1, 1)));
5066 /* Make range 0 be the range that starts first, or ends last if they
5067 start at the same value. Swap them if it isn't. */
5068 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5069 low0, 0, low1, 0))
5070 || (lowequal
5071 && integer_onep (range_binop (GT_EXPR, integer_type_node,
5072 high1, 1, high0, 1))))
5074 temp = in0_p, in0_p = in1_p, in1_p = temp;
5075 tem = low0, low0 = low1, low1 = tem;
5076 tem = high0, high0 = high1, high1 = tem;
5079 /* Now flag two cases, whether the ranges are disjoint or whether the
5080 second range is totally subsumed in the first. Note that the tests
5081 below are simplified by the ones above. */
5082 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5083 high0, 1, low1, 0));
5084 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5085 high1, 1, high0, 1));
5087 /* We now have four cases, depending on whether we are including or
5088 excluding the two ranges. */
5089 if (in0_p && in1_p)
5091 /* If they don't overlap, the result is false. If the second range
5092 is a subset it is the result. Otherwise, the range is from the start
5093 of the second to the end of the first. */
5094 if (no_overlap)
5095 in_p = 0, low = high = 0;
5096 else if (subset)
5097 in_p = 1, low = low1, high = high1;
5098 else
5099 in_p = 1, low = low1, high = high0;
5102 else if (in0_p && ! in1_p)
5104 /* If they don't overlap, the result is the first range. If they are
5105 equal, the result is false. If the second range is a subset of the
5106 first, and the ranges begin at the same place, we go from just after
5107 the end of the second range to the end of the first. If the second
5108 range is not a subset of the first, or if it is a subset and both
5109 ranges end at the same place, the range starts at the start of the
5110 first range and ends just before the second range.
5111 Otherwise, we can't describe this as a single range. */
5112 if (no_overlap)
5113 in_p = 1, low = low0, high = high0;
5114 else if (lowequal && highequal)
5115 in_p = 0, low = high = 0;
5116 else if (subset && lowequal)
5118 low = range_successor (high1);
5119 high = high0;
5120 in_p = 1;
5121 if (low == 0)
5123 /* We are in the weird situation where high0 > high1 but
5124 high1 has no successor. Punt. */
5125 return 0;
5128 else if (! subset || highequal)
5130 low = low0;
5131 high = range_predecessor (low1);
5132 in_p = 1;
5133 if (high == 0)
5135 /* low0 < low1 but low1 has no predecessor. Punt. */
5136 return 0;
5139 else
5140 return 0;
5143 else if (! in0_p && in1_p)
5145 /* If they don't overlap, the result is the second range. If the second
5146 is a subset of the first, the result is false. Otherwise,
5147 the range starts just after the first range and ends at the
5148 end of the second. */
5149 if (no_overlap)
5150 in_p = 1, low = low1, high = high1;
5151 else if (subset || highequal)
5152 in_p = 0, low = high = 0;
5153 else
5155 low = range_successor (high0);
5156 high = high1;
5157 in_p = 1;
5158 if (low == 0)
5160 /* high1 > high0 but high0 has no successor. Punt. */
5161 return 0;
5166 else
5168 /* The case where we are excluding both ranges. Here the complex case
5169 is if they don't overlap. In that case, the only time we have a
5170 range is if they are adjacent. If the second is a subset of the
5171 first, the result is the first. Otherwise, the range to exclude
5172 starts at the beginning of the first range and ends at the end of the
5173 second. */
5174 if (no_overlap)
5176 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5177 range_successor (high0),
5178 1, low1, 0)))
5179 in_p = 0, low = low0, high = high1;
5180 else
5182 /* Canonicalize - [min, x] into - [-, x]. */
5183 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5184 switch (TREE_CODE (TREE_TYPE (low0)))
5186 case ENUMERAL_TYPE:
5187 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5188 GET_MODE_BITSIZE
5189 (TYPE_MODE (TREE_TYPE (low0)))))
5190 break;
5191 /* FALLTHROUGH */
5192 case INTEGER_TYPE:
5193 if (tree_int_cst_equal (low0,
5194 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5195 low0 = 0;
5196 break;
5197 case POINTER_TYPE:
5198 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5199 && integer_zerop (low0))
5200 low0 = 0;
5201 break;
5202 default:
5203 break;
5206 /* Canonicalize - [x, max] into - [x, -]. */
5207 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5208 switch (TREE_CODE (TREE_TYPE (high1)))
5210 case ENUMERAL_TYPE:
5211 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5212 GET_MODE_BITSIZE
5213 (TYPE_MODE (TREE_TYPE (high1)))))
5214 break;
5215 /* FALLTHROUGH */
5216 case INTEGER_TYPE:
5217 if (tree_int_cst_equal (high1,
5218 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5219 high1 = 0;
5220 break;
5221 case POINTER_TYPE:
5222 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5223 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5224 high1, 1,
5225 build_int_cst (TREE_TYPE (high1), 1),
5226 1)))
5227 high1 = 0;
5228 break;
5229 default:
5230 break;
5233 /* The ranges might be also adjacent between the maximum and
5234 minimum values of the given type. For
5235 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5236 return + [x + 1, y - 1]. */
5237 if (low0 == 0 && high1 == 0)
5239 low = range_successor (high0);
5240 high = range_predecessor (low1);
5241 if (low == 0 || high == 0)
5242 return 0;
5244 in_p = 1;
5246 else
5247 return 0;
5250 else if (subset)
5251 in_p = 0, low = low0, high = high0;
5252 else
5253 in_p = 0, low = low0, high = high1;
5256 *pin_p = in_p, *plow = low, *phigh = high;
5257 return 1;
5261 /* Subroutine of fold, looking inside expressions of the form
5262 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5263 of the COND_EXPR. This function is being used also to optimize
5264 A op B ? C : A, by reversing the comparison first.
5266 Return a folded expression whose code is not a COND_EXPR
5267 anymore, or NULL_TREE if no folding opportunity is found. */
5269 static tree
5270 fold_cond_expr_with_comparison (location_t loc, tree type,
5271 tree arg0, tree arg1, tree arg2)
5273 enum tree_code comp_code = TREE_CODE (arg0);
5274 tree arg00 = TREE_OPERAND (arg0, 0);
5275 tree arg01 = TREE_OPERAND (arg0, 1);
5276 tree arg1_type = TREE_TYPE (arg1);
5277 tree tem;
5279 STRIP_NOPS (arg1);
5280 STRIP_NOPS (arg2);
5282 /* If we have A op 0 ? A : -A, consider applying the following
5283 transformations:
5285 A == 0? A : -A same as -A
5286 A != 0? A : -A same as A
5287 A >= 0? A : -A same as abs (A)
5288 A > 0? A : -A same as abs (A)
5289 A <= 0? A : -A same as -abs (A)
5290 A < 0? A : -A same as -abs (A)
5292 None of these transformations work for modes with signed
5293 zeros. If A is +/-0, the first two transformations will
5294 change the sign of the result (from +0 to -0, or vice
5295 versa). The last four will fix the sign of the result,
5296 even though the original expressions could be positive or
5297 negative, depending on the sign of A.
5299 Note that all these transformations are correct if A is
5300 NaN, since the two alternatives (A and -A) are also NaNs. */
5301 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5302 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5303 ? real_zerop (arg01)
5304 : integer_zerop (arg01))
5305 && ((TREE_CODE (arg2) == NEGATE_EXPR
5306 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5307 /* In the case that A is of the form X-Y, '-A' (arg2) may
5308 have already been folded to Y-X, check for that. */
5309 || (TREE_CODE (arg1) == MINUS_EXPR
5310 && TREE_CODE (arg2) == MINUS_EXPR
5311 && operand_equal_p (TREE_OPERAND (arg1, 0),
5312 TREE_OPERAND (arg2, 1), 0)
5313 && operand_equal_p (TREE_OPERAND (arg1, 1),
5314 TREE_OPERAND (arg2, 0), 0))))
5315 switch (comp_code)
5317 case EQ_EXPR:
5318 case UNEQ_EXPR:
5319 tem = fold_convert_loc (loc, arg1_type, arg1);
5320 return fold_convert_loc (loc, type, negate_expr (tem));
5321 case NE_EXPR:
5322 case LTGT_EXPR:
5323 return fold_convert_loc (loc, type, arg1);
5324 case UNGE_EXPR:
5325 case UNGT_EXPR:
5326 if (flag_trapping_math)
5327 break;
5328 /* Fall through. */
5329 case GE_EXPR:
5330 case GT_EXPR:
5331 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5332 break;
5333 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5334 return fold_convert_loc (loc, type, tem);
5335 case UNLE_EXPR:
5336 case UNLT_EXPR:
5337 if (flag_trapping_math)
5338 break;
5339 /* FALLTHRU */
5340 case LE_EXPR:
5341 case LT_EXPR:
5342 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5343 break;
5344 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5345 return negate_expr (fold_convert_loc (loc, type, tem));
5346 default:
5347 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5348 break;
5351 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5352 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5353 both transformations are correct when A is NaN: A != 0
5354 is then true, and A == 0 is false. */
5356 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5357 && integer_zerop (arg01) && integer_zerop (arg2))
5359 if (comp_code == NE_EXPR)
5360 return fold_convert_loc (loc, type, arg1);
5361 else if (comp_code == EQ_EXPR)
5362 return build_zero_cst (type);
5365 /* Try some transformations of A op B ? A : B.
5367 A == B? A : B same as B
5368 A != B? A : B same as A
5369 A >= B? A : B same as max (A, B)
5370 A > B? A : B same as max (B, A)
5371 A <= B? A : B same as min (A, B)
5372 A < B? A : B same as min (B, A)
5374 As above, these transformations don't work in the presence
5375 of signed zeros. For example, if A and B are zeros of
5376 opposite sign, the first two transformations will change
5377 the sign of the result. In the last four, the original
5378 expressions give different results for (A=+0, B=-0) and
5379 (A=-0, B=+0), but the transformed expressions do not.
5381 The first two transformations are correct if either A or B
5382 is a NaN. In the first transformation, the condition will
5383 be false, and B will indeed be chosen. In the case of the
5384 second transformation, the condition A != B will be true,
5385 and A will be chosen.
5387 The conversions to max() and min() are not correct if B is
5388 a number and A is not. The conditions in the original
5389 expressions will be false, so all four give B. The min()
5390 and max() versions would give a NaN instead. */
5391 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5392 && operand_equal_for_comparison_p (arg01, arg2)
5393 /* Avoid these transformations if the COND_EXPR may be used
5394 as an lvalue in the C++ front-end. PR c++/19199. */
5395 && (in_gimple_form
5396 || VECTOR_TYPE_P (type)
5397 || (! lang_GNU_CXX ()
5398 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5399 || ! maybe_lvalue_p (arg1)
5400 || ! maybe_lvalue_p (arg2)))
5402 tree comp_op0 = arg00;
5403 tree comp_op1 = arg01;
5404 tree comp_type = TREE_TYPE (comp_op0);
5406 switch (comp_code)
5408 case EQ_EXPR:
5409 return fold_convert_loc (loc, type, arg2);
5410 case NE_EXPR:
5411 return fold_convert_loc (loc, type, arg1);
5412 case LE_EXPR:
5413 case LT_EXPR:
5414 case UNLE_EXPR:
5415 case UNLT_EXPR:
5416 /* In C++ a ?: expression can be an lvalue, so put the
5417 operand which will be used if they are equal first
5418 so that we can convert this back to the
5419 corresponding COND_EXPR. */
5420 if (!HONOR_NANS (arg1))
5422 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5423 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5424 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5425 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5426 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5427 comp_op1, comp_op0);
5428 return fold_convert_loc (loc, type, tem);
5430 break;
5431 case GE_EXPR:
5432 case GT_EXPR:
5433 case UNGE_EXPR:
5434 case UNGT_EXPR:
5435 if (!HONOR_NANS (arg1))
5437 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5438 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5439 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5440 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5441 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5442 comp_op1, comp_op0);
5443 return fold_convert_loc (loc, type, tem);
5445 break;
5446 case UNEQ_EXPR:
5447 if (!HONOR_NANS (arg1))
5448 return fold_convert_loc (loc, type, arg2);
5449 break;
5450 case LTGT_EXPR:
5451 if (!HONOR_NANS (arg1))
5452 return fold_convert_loc (loc, type, arg1);
5453 break;
5454 default:
5455 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5456 break;
5460 return NULL_TREE;
5465 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5466 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5467 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5468 false) >= 2)
5469 #endif
5471 /* EXP is some logical combination of boolean tests. See if we can
5472 merge it into some range test. Return the new tree if so. */
5474 static tree
5475 fold_range_test (location_t loc, enum tree_code code, tree type,
5476 tree op0, tree op1)
5478 int or_op = (code == TRUTH_ORIF_EXPR
5479 || code == TRUTH_OR_EXPR);
5480 int in0_p, in1_p, in_p;
5481 tree low0, low1, low, high0, high1, high;
5482 bool strict_overflow_p = false;
5483 tree tem, lhs, rhs;
5484 const char * const warnmsg = G_("assuming signed overflow does not occur "
5485 "when simplifying range test");
5487 if (!INTEGRAL_TYPE_P (type))
5488 return 0;
5490 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5491 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5493 /* If this is an OR operation, invert both sides; we will invert
5494 again at the end. */
5495 if (or_op)
5496 in0_p = ! in0_p, in1_p = ! in1_p;
5498 /* If both expressions are the same, if we can merge the ranges, and we
5499 can build the range test, return it or it inverted. If one of the
5500 ranges is always true or always false, consider it to be the same
5501 expression as the other. */
5502 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5503 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5504 in1_p, low1, high1)
5505 && (tem = (build_range_check (loc, type,
5506 lhs != 0 ? lhs
5507 : rhs != 0 ? rhs : integer_zero_node,
5508 in_p, low, high))) != 0)
5510 if (strict_overflow_p)
5511 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5512 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5515 /* On machines where the branch cost is expensive, if this is a
5516 short-circuited branch and the underlying object on both sides
5517 is the same, make a non-short-circuit operation. */
5518 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5519 && !flag_sanitize_coverage
5520 && lhs != 0 && rhs != 0
5521 && (code == TRUTH_ANDIF_EXPR
5522 || code == TRUTH_ORIF_EXPR)
5523 && operand_equal_p (lhs, rhs, 0))
5525 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5526 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5527 which cases we can't do this. */
5528 if (simple_operand_p (lhs))
5529 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5530 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5531 type, op0, op1);
5533 else if (!lang_hooks.decls.global_bindings_p ()
5534 && !CONTAINS_PLACEHOLDER_P (lhs))
5536 tree common = save_expr (lhs);
5538 if ((lhs = build_range_check (loc, type, common,
5539 or_op ? ! in0_p : in0_p,
5540 low0, high0)) != 0
5541 && (rhs = build_range_check (loc, type, common,
5542 or_op ? ! in1_p : in1_p,
5543 low1, high1)) != 0)
5545 if (strict_overflow_p)
5546 fold_overflow_warning (warnmsg,
5547 WARN_STRICT_OVERFLOW_COMPARISON);
5548 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5549 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5550 type, lhs, rhs);
5555 return 0;
5558 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5559 bit value. Arrange things so the extra bits will be set to zero if and
5560 only if C is signed-extended to its full width. If MASK is nonzero,
5561 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5563 static tree
5564 unextend (tree c, int p, int unsignedp, tree mask)
5566 tree type = TREE_TYPE (c);
5567 int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
5568 tree temp;
5570 if (p == modesize || unsignedp)
5571 return c;
5573 /* We work by getting just the sign bit into the low-order bit, then
5574 into the high-order bit, then sign-extend. We then XOR that value
5575 with C. */
5576 temp = build_int_cst (TREE_TYPE (c),
5577 wi::extract_uhwi (wi::to_wide (c), p - 1, 1));
5579 /* We must use a signed type in order to get an arithmetic right shift.
5580 However, we must also avoid introducing accidental overflows, so that
5581 a subsequent call to integer_zerop will work. Hence we must
5582 do the type conversion here. At this point, the constant is either
5583 zero or one, and the conversion to a signed type can never overflow.
5584 We could get an overflow if this conversion is done anywhere else. */
5585 if (TYPE_UNSIGNED (type))
5586 temp = fold_convert (signed_type_for (type), temp);
5588 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5589 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5590 if (mask != 0)
5591 temp = const_binop (BIT_AND_EXPR, temp,
5592 fold_convert (TREE_TYPE (c), mask));
5593 /* If necessary, convert the type back to match the type of C. */
5594 if (TYPE_UNSIGNED (type))
5595 temp = fold_convert (type, temp);
5597 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5600 /* For an expression that has the form
5601 (A && B) || ~B
5603 (A || B) && ~B,
5604 we can drop one of the inner expressions and simplify to
5605 A || ~B
5607 A && ~B
5608 LOC is the location of the resulting expression. OP is the inner
5609 logical operation; the left-hand side in the examples above, while CMPOP
5610 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5611 removing a condition that guards another, as in
5612 (A != NULL && A->...) || A == NULL
5613 which we must not transform. If RHS_ONLY is true, only eliminate the
5614 right-most operand of the inner logical operation. */
5616 static tree
5617 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5618 bool rhs_only)
5620 tree type = TREE_TYPE (cmpop);
5621 enum tree_code code = TREE_CODE (cmpop);
5622 enum tree_code truthop_code = TREE_CODE (op);
5623 tree lhs = TREE_OPERAND (op, 0);
5624 tree rhs = TREE_OPERAND (op, 1);
5625 tree orig_lhs = lhs, orig_rhs = rhs;
5626 enum tree_code rhs_code = TREE_CODE (rhs);
5627 enum tree_code lhs_code = TREE_CODE (lhs);
5628 enum tree_code inv_code;
5630 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5631 return NULL_TREE;
5633 if (TREE_CODE_CLASS (code) != tcc_comparison)
5634 return NULL_TREE;
5636 if (rhs_code == truthop_code)
5638 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5639 if (newrhs != NULL_TREE)
5641 rhs = newrhs;
5642 rhs_code = TREE_CODE (rhs);
5645 if (lhs_code == truthop_code && !rhs_only)
5647 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5648 if (newlhs != NULL_TREE)
5650 lhs = newlhs;
5651 lhs_code = TREE_CODE (lhs);
5655 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5656 if (inv_code == rhs_code
5657 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5658 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5659 return lhs;
5660 if (!rhs_only && inv_code == lhs_code
5661 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5662 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5663 return rhs;
5664 if (rhs != orig_rhs || lhs != orig_lhs)
5665 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5666 lhs, rhs);
5667 return NULL_TREE;
5670 /* Find ways of folding logical expressions of LHS and RHS:
5671 Try to merge two comparisons to the same innermost item.
5672 Look for range tests like "ch >= '0' && ch <= '9'".
5673 Look for combinations of simple terms on machines with expensive branches
5674 and evaluate the RHS unconditionally.
5676 For example, if we have p->a == 2 && p->b == 4 and we can make an
5677 object large enough to span both A and B, we can do this with a comparison
5678 against the object ANDed with the a mask.
5680 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5681 operations to do this with one comparison.
5683 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5684 function and the one above.
5686 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5687 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5689 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5690 two operands.
5692 We return the simplified tree or 0 if no optimization is possible. */
5694 static tree
5695 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5696 tree lhs, tree rhs)
5698 /* If this is the "or" of two comparisons, we can do something if
5699 the comparisons are NE_EXPR. If this is the "and", we can do something
5700 if the comparisons are EQ_EXPR. I.e.,
5701 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5703 WANTED_CODE is this operation code. For single bit fields, we can
5704 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5705 comparison for one-bit fields. */
5707 enum tree_code wanted_code;
5708 enum tree_code lcode, rcode;
5709 tree ll_arg, lr_arg, rl_arg, rr_arg;
5710 tree ll_inner, lr_inner, rl_inner, rr_inner;
5711 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5712 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5713 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5714 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5715 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5716 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5717 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5718 scalar_int_mode lnmode, rnmode;
5719 tree ll_mask, lr_mask, rl_mask, rr_mask;
5720 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5721 tree l_const, r_const;
5722 tree lntype, rntype, result;
5723 HOST_WIDE_INT first_bit, end_bit;
5724 int volatilep;
5726 /* Start by getting the comparison codes. Fail if anything is volatile.
5727 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5728 it were surrounded with a NE_EXPR. */
5730 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5731 return 0;
5733 lcode = TREE_CODE (lhs);
5734 rcode = TREE_CODE (rhs);
5736 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5738 lhs = build2 (NE_EXPR, truth_type, lhs,
5739 build_int_cst (TREE_TYPE (lhs), 0));
5740 lcode = NE_EXPR;
5743 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5745 rhs = build2 (NE_EXPR, truth_type, rhs,
5746 build_int_cst (TREE_TYPE (rhs), 0));
5747 rcode = NE_EXPR;
5750 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5751 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5752 return 0;
5754 ll_arg = TREE_OPERAND (lhs, 0);
5755 lr_arg = TREE_OPERAND (lhs, 1);
5756 rl_arg = TREE_OPERAND (rhs, 0);
5757 rr_arg = TREE_OPERAND (rhs, 1);
5759 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5760 if (simple_operand_p (ll_arg)
5761 && simple_operand_p (lr_arg))
5763 if (operand_equal_p (ll_arg, rl_arg, 0)
5764 && operand_equal_p (lr_arg, rr_arg, 0))
5766 result = combine_comparisons (loc, code, lcode, rcode,
5767 truth_type, ll_arg, lr_arg);
5768 if (result)
5769 return result;
5771 else if (operand_equal_p (ll_arg, rr_arg, 0)
5772 && operand_equal_p (lr_arg, rl_arg, 0))
5774 result = combine_comparisons (loc, code, lcode,
5775 swap_tree_comparison (rcode),
5776 truth_type, ll_arg, lr_arg);
5777 if (result)
5778 return result;
5782 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5783 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5785 /* If the RHS can be evaluated unconditionally and its operands are
5786 simple, it wins to evaluate the RHS unconditionally on machines
5787 with expensive branches. In this case, this isn't a comparison
5788 that can be merged. */
5790 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5791 false) >= 2
5792 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5793 && simple_operand_p (rl_arg)
5794 && simple_operand_p (rr_arg))
5796 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5797 if (code == TRUTH_OR_EXPR
5798 && lcode == NE_EXPR && integer_zerop (lr_arg)
5799 && rcode == NE_EXPR && integer_zerop (rr_arg)
5800 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5801 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5802 return build2_loc (loc, NE_EXPR, truth_type,
5803 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5804 ll_arg, rl_arg),
5805 build_int_cst (TREE_TYPE (ll_arg), 0));
5807 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5808 if (code == TRUTH_AND_EXPR
5809 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5810 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5811 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5812 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5813 return build2_loc (loc, EQ_EXPR, truth_type,
5814 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5815 ll_arg, rl_arg),
5816 build_int_cst (TREE_TYPE (ll_arg), 0));
5819 /* See if the comparisons can be merged. Then get all the parameters for
5820 each side. */
5822 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5823 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5824 return 0;
5826 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5827 volatilep = 0;
5828 ll_inner = decode_field_reference (loc, &ll_arg,
5829 &ll_bitsize, &ll_bitpos, &ll_mode,
5830 &ll_unsignedp, &ll_reversep, &volatilep,
5831 &ll_mask, &ll_and_mask);
5832 lr_inner = decode_field_reference (loc, &lr_arg,
5833 &lr_bitsize, &lr_bitpos, &lr_mode,
5834 &lr_unsignedp, &lr_reversep, &volatilep,
5835 &lr_mask, &lr_and_mask);
5836 rl_inner = decode_field_reference (loc, &rl_arg,
5837 &rl_bitsize, &rl_bitpos, &rl_mode,
5838 &rl_unsignedp, &rl_reversep, &volatilep,
5839 &rl_mask, &rl_and_mask);
5840 rr_inner = decode_field_reference (loc, &rr_arg,
5841 &rr_bitsize, &rr_bitpos, &rr_mode,
5842 &rr_unsignedp, &rr_reversep, &volatilep,
5843 &rr_mask, &rr_and_mask);
5845 /* It must be true that the inner operation on the lhs of each
5846 comparison must be the same if we are to be able to do anything.
5847 Then see if we have constants. If not, the same must be true for
5848 the rhs's. */
5849 if (volatilep
5850 || ll_reversep != rl_reversep
5851 || ll_inner == 0 || rl_inner == 0
5852 || ! operand_equal_p (ll_inner, rl_inner, 0))
5853 return 0;
5855 if (TREE_CODE (lr_arg) == INTEGER_CST
5856 && TREE_CODE (rr_arg) == INTEGER_CST)
5858 l_const = lr_arg, r_const = rr_arg;
5859 lr_reversep = ll_reversep;
5861 else if (lr_reversep != rr_reversep
5862 || lr_inner == 0 || rr_inner == 0
5863 || ! operand_equal_p (lr_inner, rr_inner, 0))
5864 return 0;
5865 else
5866 l_const = r_const = 0;
5868 /* If either comparison code is not correct for our logical operation,
5869 fail. However, we can convert a one-bit comparison against zero into
5870 the opposite comparison against that bit being set in the field. */
5872 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5873 if (lcode != wanted_code)
5875 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5877 /* Make the left operand unsigned, since we are only interested
5878 in the value of one bit. Otherwise we are doing the wrong
5879 thing below. */
5880 ll_unsignedp = 1;
5881 l_const = ll_mask;
5883 else
5884 return 0;
5887 /* This is analogous to the code for l_const above. */
5888 if (rcode != wanted_code)
5890 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5892 rl_unsignedp = 1;
5893 r_const = rl_mask;
5895 else
5896 return 0;
5899 /* See if we can find a mode that contains both fields being compared on
5900 the left. If we can't, fail. Otherwise, update all constants and masks
5901 to be relative to a field of that size. */
5902 first_bit = MIN (ll_bitpos, rl_bitpos);
5903 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5904 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5905 TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
5906 volatilep, &lnmode))
5907 return 0;
5909 lnbitsize = GET_MODE_BITSIZE (lnmode);
5910 lnbitpos = first_bit & ~ (lnbitsize - 1);
5911 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5912 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5914 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5916 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5917 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5920 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5921 size_int (xll_bitpos));
5922 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5923 size_int (xrl_bitpos));
5925 if (l_const)
5927 l_const = fold_convert_loc (loc, lntype, l_const);
5928 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5929 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5930 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5931 fold_build1_loc (loc, BIT_NOT_EXPR,
5932 lntype, ll_mask))))
5934 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5936 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5939 if (r_const)
5941 r_const = fold_convert_loc (loc, lntype, r_const);
5942 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5943 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5944 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5945 fold_build1_loc (loc, BIT_NOT_EXPR,
5946 lntype, rl_mask))))
5948 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5950 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5954 /* If the right sides are not constant, do the same for it. Also,
5955 disallow this optimization if a size or signedness mismatch occurs
5956 between the left and right sides. */
5957 if (l_const == 0)
5959 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5960 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5961 /* Make sure the two fields on the right
5962 correspond to the left without being swapped. */
5963 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5964 return 0;
5966 first_bit = MIN (lr_bitpos, rr_bitpos);
5967 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5968 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5969 TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
5970 volatilep, &rnmode))
5971 return 0;
5973 rnbitsize = GET_MODE_BITSIZE (rnmode);
5974 rnbitpos = first_bit & ~ (rnbitsize - 1);
5975 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5976 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5978 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5980 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5981 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5984 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5985 rntype, lr_mask),
5986 size_int (xlr_bitpos));
5987 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5988 rntype, rr_mask),
5989 size_int (xrr_bitpos));
5991 /* Make a mask that corresponds to both fields being compared.
5992 Do this for both items being compared. If the operands are the
5993 same size and the bits being compared are in the same position
5994 then we can do this by masking both and comparing the masked
5995 results. */
5996 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5997 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5998 if (lnbitsize == rnbitsize
5999 && xll_bitpos == xlr_bitpos
6000 && lnbitpos >= 0
6001 && rnbitpos >= 0)
6003 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
6004 lntype, lnbitsize, lnbitpos,
6005 ll_unsignedp || rl_unsignedp, ll_reversep);
6006 if (! all_ones_mask_p (ll_mask, lnbitsize))
6007 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6009 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
6010 rntype, rnbitsize, rnbitpos,
6011 lr_unsignedp || rr_unsignedp, lr_reversep);
6012 if (! all_ones_mask_p (lr_mask, rnbitsize))
6013 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6015 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6018 /* There is still another way we can do something: If both pairs of
6019 fields being compared are adjacent, we may be able to make a wider
6020 field containing them both.
6022 Note that we still must mask the lhs/rhs expressions. Furthermore,
6023 the mask must be shifted to account for the shift done by
6024 make_bit_field_ref. */
6025 if (((ll_bitsize + ll_bitpos == rl_bitpos
6026 && lr_bitsize + lr_bitpos == rr_bitpos)
6027 || (ll_bitpos == rl_bitpos + rl_bitsize
6028 && lr_bitpos == rr_bitpos + rr_bitsize))
6029 && ll_bitpos >= 0
6030 && rl_bitpos >= 0
6031 && lr_bitpos >= 0
6032 && rr_bitpos >= 0)
6034 tree type;
6036 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
6037 ll_bitsize + rl_bitsize,
6038 MIN (ll_bitpos, rl_bitpos),
6039 ll_unsignedp, ll_reversep);
6040 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
6041 lr_bitsize + rr_bitsize,
6042 MIN (lr_bitpos, rr_bitpos),
6043 lr_unsignedp, lr_reversep);
6045 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6046 size_int (MIN (xll_bitpos, xrl_bitpos)));
6047 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6048 size_int (MIN (xlr_bitpos, xrr_bitpos)));
6050 /* Convert to the smaller type before masking out unwanted bits. */
6051 type = lntype;
6052 if (lntype != rntype)
6054 if (lnbitsize > rnbitsize)
6056 lhs = fold_convert_loc (loc, rntype, lhs);
6057 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6058 type = rntype;
6060 else if (lnbitsize < rnbitsize)
6062 rhs = fold_convert_loc (loc, lntype, rhs);
6063 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6064 type = lntype;
6068 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6069 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6071 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6072 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6074 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6077 return 0;
6080 /* Handle the case of comparisons with constants. If there is something in
6081 common between the masks, those bits of the constants must be the same.
6082 If not, the condition is always false. Test for this to avoid generating
6083 incorrect code below. */
6084 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
6085 if (! integer_zerop (result)
6086 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
6087 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
6089 if (wanted_code == NE_EXPR)
6091 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6092 return constant_boolean_node (true, truth_type);
6094 else
6096 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6097 return constant_boolean_node (false, truth_type);
6101 if (lnbitpos < 0)
6102 return 0;
6104 /* Construct the expression we will return. First get the component
6105 reference we will make. Unless the mask is all ones the width of
6106 that field, perform the mask operation. Then compare with the
6107 merged constant. */
6108 result = make_bit_field_ref (loc, ll_inner, ll_arg,
6109 lntype, lnbitsize, lnbitpos,
6110 ll_unsignedp || rl_unsignedp, ll_reversep);
6112 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6113 if (! all_ones_mask_p (ll_mask, lnbitsize))
6114 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
6116 return build2_loc (loc, wanted_code, truth_type, result,
6117 const_binop (BIT_IOR_EXPR, l_const, r_const));
6120 /* T is an integer expression that is being multiplied, divided, or taken a
6121 modulus (CODE says which and what kind of divide or modulus) by a
6122 constant C. See if we can eliminate that operation by folding it with
6123 other operations already in T. WIDE_TYPE, if non-null, is a type that
6124 should be used for the computation if wider than our type.
6126 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6127 (X * 2) + (Y * 4). We must, however, be assured that either the original
6128 expression would not overflow or that overflow is undefined for the type
6129 in the language in question.
6131 If we return a non-null expression, it is an equivalent form of the
6132 original computation, but need not be in the original type.
6134 We set *STRICT_OVERFLOW_P to true if the return values depends on
6135 signed overflow being undefined. Otherwise we do not change
6136 *STRICT_OVERFLOW_P. */
6138 static tree
6139 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6140 bool *strict_overflow_p)
6142 /* To avoid exponential search depth, refuse to allow recursion past
6143 three levels. Beyond that (1) it's highly unlikely that we'll find
6144 something interesting and (2) we've probably processed it before
6145 when we built the inner expression. */
6147 static int depth;
6148 tree ret;
6150 if (depth > 3)
6151 return NULL;
6153 depth++;
6154 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6155 depth--;
6157 return ret;
6160 static tree
6161 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6162 bool *strict_overflow_p)
6164 tree type = TREE_TYPE (t);
6165 enum tree_code tcode = TREE_CODE (t);
6166 tree ctype = (wide_type != 0
6167 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6168 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6169 ? wide_type : type);
6170 tree t1, t2;
6171 int same_p = tcode == code;
6172 tree op0 = NULL_TREE, op1 = NULL_TREE;
6173 bool sub_strict_overflow_p;
6175 /* Don't deal with constants of zero here; they confuse the code below. */
6176 if (integer_zerop (c))
6177 return NULL_TREE;
6179 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6180 op0 = TREE_OPERAND (t, 0);
6182 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6183 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6185 /* Note that we need not handle conditional operations here since fold
6186 already handles those cases. So just do arithmetic here. */
6187 switch (tcode)
6189 case INTEGER_CST:
6190 /* For a constant, we can always simplify if we are a multiply
6191 or (for divide and modulus) if it is a multiple of our constant. */
6192 if (code == MULT_EXPR
6193 || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6194 TYPE_SIGN (type)))
6196 tree tem = const_binop (code, fold_convert (ctype, t),
6197 fold_convert (ctype, c));
6198 /* If the multiplication overflowed, we lost information on it.
6199 See PR68142 and PR69845. */
6200 if (TREE_OVERFLOW (tem))
6201 return NULL_TREE;
6202 return tem;
6204 break;
6206 CASE_CONVERT: case NON_LVALUE_EXPR:
6207 /* If op0 is an expression ... */
6208 if ((COMPARISON_CLASS_P (op0)
6209 || UNARY_CLASS_P (op0)
6210 || BINARY_CLASS_P (op0)
6211 || VL_EXP_CLASS_P (op0)
6212 || EXPRESSION_CLASS_P (op0))
6213 /* ... and has wrapping overflow, and its type is smaller
6214 than ctype, then we cannot pass through as widening. */
6215 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6216 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6217 && (TYPE_PRECISION (ctype)
6218 > TYPE_PRECISION (TREE_TYPE (op0))))
6219 /* ... or this is a truncation (t is narrower than op0),
6220 then we cannot pass through this narrowing. */
6221 || (TYPE_PRECISION (type)
6222 < TYPE_PRECISION (TREE_TYPE (op0)))
6223 /* ... or signedness changes for division or modulus,
6224 then we cannot pass through this conversion. */
6225 || (code != MULT_EXPR
6226 && (TYPE_UNSIGNED (ctype)
6227 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6228 /* ... or has undefined overflow while the converted to
6229 type has not, we cannot do the operation in the inner type
6230 as that would introduce undefined overflow. */
6231 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6232 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6233 && !TYPE_OVERFLOW_UNDEFINED (type))))
6234 break;
6236 /* Pass the constant down and see if we can make a simplification. If
6237 we can, replace this expression with the inner simplification for
6238 possible later conversion to our or some other type. */
6239 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6240 && TREE_CODE (t2) == INTEGER_CST
6241 && !TREE_OVERFLOW (t2)
6242 && (t1 = extract_muldiv (op0, t2, code,
6243 code == MULT_EXPR ? ctype : NULL_TREE,
6244 strict_overflow_p)) != 0)
6245 return t1;
6246 break;
6248 case ABS_EXPR:
6249 /* If widening the type changes it from signed to unsigned, then we
6250 must avoid building ABS_EXPR itself as unsigned. */
6251 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6253 tree cstype = (*signed_type_for) (ctype);
6254 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6255 != 0)
6257 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6258 return fold_convert (ctype, t1);
6260 break;
6262 /* If the constant is negative, we cannot simplify this. */
6263 if (tree_int_cst_sgn (c) == -1)
6264 break;
6265 /* FALLTHROUGH */
6266 case NEGATE_EXPR:
6267 /* For division and modulus, type can't be unsigned, as e.g.
6268 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6269 For signed types, even with wrapping overflow, this is fine. */
6270 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6271 break;
6272 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6273 != 0)
6274 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6275 break;
6277 case MIN_EXPR: case MAX_EXPR:
6278 /* If widening the type changes the signedness, then we can't perform
6279 this optimization as that changes the result. */
6280 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6281 break;
6283 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6284 sub_strict_overflow_p = false;
6285 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6286 &sub_strict_overflow_p)) != 0
6287 && (t2 = extract_muldiv (op1, c, code, wide_type,
6288 &sub_strict_overflow_p)) != 0)
6290 if (tree_int_cst_sgn (c) < 0)
6291 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6292 if (sub_strict_overflow_p)
6293 *strict_overflow_p = true;
6294 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6295 fold_convert (ctype, t2));
6297 break;
6299 case LSHIFT_EXPR: case RSHIFT_EXPR:
6300 /* If the second operand is constant, this is a multiplication
6301 or floor division, by a power of two, so we can treat it that
6302 way unless the multiplier or divisor overflows. Signed
6303 left-shift overflow is implementation-defined rather than
6304 undefined in C90, so do not convert signed left shift into
6305 multiplication. */
6306 if (TREE_CODE (op1) == INTEGER_CST
6307 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6308 /* const_binop may not detect overflow correctly,
6309 so check for it explicitly here. */
6310 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6311 wi::to_wide (op1))
6312 && (t1 = fold_convert (ctype,
6313 const_binop (LSHIFT_EXPR, size_one_node,
6314 op1))) != 0
6315 && !TREE_OVERFLOW (t1))
6316 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6317 ? MULT_EXPR : FLOOR_DIV_EXPR,
6318 ctype,
6319 fold_convert (ctype, op0),
6320 t1),
6321 c, code, wide_type, strict_overflow_p);
6322 break;
6324 case PLUS_EXPR: case MINUS_EXPR:
6325 /* See if we can eliminate the operation on both sides. If we can, we
6326 can return a new PLUS or MINUS. If we can't, the only remaining
6327 cases where we can do anything are if the second operand is a
6328 constant. */
6329 sub_strict_overflow_p = false;
6330 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6331 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6332 if (t1 != 0 && t2 != 0
6333 && TYPE_OVERFLOW_WRAPS (ctype)
6334 && (code == MULT_EXPR
6335 /* If not multiplication, we can only do this if both operands
6336 are divisible by c. */
6337 || (multiple_of_p (ctype, op0, c)
6338 && multiple_of_p (ctype, op1, c))))
6340 if (sub_strict_overflow_p)
6341 *strict_overflow_p = true;
6342 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6343 fold_convert (ctype, t2));
6346 /* If this was a subtraction, negate OP1 and set it to be an addition.
6347 This simplifies the logic below. */
6348 if (tcode == MINUS_EXPR)
6350 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6351 /* If OP1 was not easily negatable, the constant may be OP0. */
6352 if (TREE_CODE (op0) == INTEGER_CST)
6354 std::swap (op0, op1);
6355 std::swap (t1, t2);
6359 if (TREE_CODE (op1) != INTEGER_CST)
6360 break;
6362 /* If either OP1 or C are negative, this optimization is not safe for
6363 some of the division and remainder types while for others we need
6364 to change the code. */
6365 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6367 if (code == CEIL_DIV_EXPR)
6368 code = FLOOR_DIV_EXPR;
6369 else if (code == FLOOR_DIV_EXPR)
6370 code = CEIL_DIV_EXPR;
6371 else if (code != MULT_EXPR
6372 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6373 break;
6376 /* If it's a multiply or a division/modulus operation of a multiple
6377 of our constant, do the operation and verify it doesn't overflow. */
6378 if (code == MULT_EXPR
6379 || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6380 TYPE_SIGN (type)))
6382 op1 = const_binop (code, fold_convert (ctype, op1),
6383 fold_convert (ctype, c));
6384 /* We allow the constant to overflow with wrapping semantics. */
6385 if (op1 == 0
6386 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6387 break;
6389 else
6390 break;
6392 /* If we have an unsigned type, we cannot widen the operation since it
6393 will change the result if the original computation overflowed. */
6394 if (TYPE_UNSIGNED (ctype) && ctype != type)
6395 break;
6397 /* The last case is if we are a multiply. In that case, we can
6398 apply the distributive law to commute the multiply and addition
6399 if the multiplication of the constants doesn't overflow
6400 and overflow is defined. With undefined overflow
6401 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6402 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6403 return fold_build2 (tcode, ctype,
6404 fold_build2 (code, ctype,
6405 fold_convert (ctype, op0),
6406 fold_convert (ctype, c)),
6407 op1);
6409 break;
6411 case MULT_EXPR:
6412 /* We have a special case here if we are doing something like
6413 (C * 8) % 4 since we know that's zero. */
6414 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6415 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6416 /* If the multiplication can overflow we cannot optimize this. */
6417 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6418 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6419 && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6420 TYPE_SIGN (type)))
6422 *strict_overflow_p = true;
6423 return omit_one_operand (type, integer_zero_node, op0);
6426 /* ... fall through ... */
6428 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6429 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6430 /* If we can extract our operation from the LHS, do so and return a
6431 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6432 do something only if the second operand is a constant. */
6433 if (same_p
6434 && TYPE_OVERFLOW_WRAPS (ctype)
6435 && (t1 = extract_muldiv (op0, c, code, wide_type,
6436 strict_overflow_p)) != 0)
6437 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6438 fold_convert (ctype, op1));
6439 else if (tcode == MULT_EXPR && code == MULT_EXPR
6440 && TYPE_OVERFLOW_WRAPS (ctype)
6441 && (t1 = extract_muldiv (op1, c, code, wide_type,
6442 strict_overflow_p)) != 0)
6443 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6444 fold_convert (ctype, t1));
6445 else if (TREE_CODE (op1) != INTEGER_CST)
6446 return 0;
6448 /* If these are the same operation types, we can associate them
6449 assuming no overflow. */
6450 if (tcode == code)
6452 bool overflow_p = false;
6453 bool overflow_mul_p;
6454 signop sign = TYPE_SIGN (ctype);
6455 unsigned prec = TYPE_PRECISION (ctype);
6456 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6457 wi::to_wide (c, prec),
6458 sign, &overflow_mul_p);
6459 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6460 if (overflow_mul_p
6461 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6462 overflow_p = true;
6463 if (!overflow_p)
6464 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6465 wide_int_to_tree (ctype, mul));
6468 /* If these operations "cancel" each other, we have the main
6469 optimizations of this pass, which occur when either constant is a
6470 multiple of the other, in which case we replace this with either an
6471 operation or CODE or TCODE.
6473 If we have an unsigned type, we cannot do this since it will change
6474 the result if the original computation overflowed. */
6475 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6476 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6477 || (tcode == MULT_EXPR
6478 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6479 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6480 && code != MULT_EXPR)))
6482 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6483 TYPE_SIGN (type)))
6485 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6486 *strict_overflow_p = true;
6487 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6488 fold_convert (ctype,
6489 const_binop (TRUNC_DIV_EXPR,
6490 op1, c)));
6492 else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6493 TYPE_SIGN (type)))
6495 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6496 *strict_overflow_p = true;
6497 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6498 fold_convert (ctype,
6499 const_binop (TRUNC_DIV_EXPR,
6500 c, op1)));
6503 break;
6505 default:
6506 break;
6509 return 0;
6512 /* Return a node which has the indicated constant VALUE (either 0 or
6513 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6514 and is of the indicated TYPE. */
6516 tree
6517 constant_boolean_node (bool value, tree type)
6519 if (type == integer_type_node)
6520 return value ? integer_one_node : integer_zero_node;
6521 else if (type == boolean_type_node)
6522 return value ? boolean_true_node : boolean_false_node;
6523 else if (TREE_CODE (type) == VECTOR_TYPE)
6524 return build_vector_from_val (type,
6525 build_int_cst (TREE_TYPE (type),
6526 value ? -1 : 0));
6527 else
6528 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6532 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6533 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6534 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6535 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6536 COND is the first argument to CODE; otherwise (as in the example
6537 given here), it is the second argument. TYPE is the type of the
6538 original expression. Return NULL_TREE if no simplification is
6539 possible. */
6541 static tree
6542 fold_binary_op_with_conditional_arg (location_t loc,
6543 enum tree_code code,
6544 tree type, tree op0, tree op1,
6545 tree cond, tree arg, int cond_first_p)
6547 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6548 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6549 tree test, true_value, false_value;
6550 tree lhs = NULL_TREE;
6551 tree rhs = NULL_TREE;
6552 enum tree_code cond_code = COND_EXPR;
6554 if (TREE_CODE (cond) == COND_EXPR
6555 || TREE_CODE (cond) == VEC_COND_EXPR)
6557 test = TREE_OPERAND (cond, 0);
6558 true_value = TREE_OPERAND (cond, 1);
6559 false_value = TREE_OPERAND (cond, 2);
6560 /* If this operand throws an expression, then it does not make
6561 sense to try to perform a logical or arithmetic operation
6562 involving it. */
6563 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6564 lhs = true_value;
6565 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6566 rhs = false_value;
6568 else if (!(TREE_CODE (type) != VECTOR_TYPE
6569 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6571 tree testtype = TREE_TYPE (cond);
6572 test = cond;
6573 true_value = constant_boolean_node (true, testtype);
6574 false_value = constant_boolean_node (false, testtype);
6576 else
6577 /* Detect the case of mixing vector and scalar types - bail out. */
6578 return NULL_TREE;
6580 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6581 cond_code = VEC_COND_EXPR;
6583 /* This transformation is only worthwhile if we don't have to wrap ARG
6584 in a SAVE_EXPR and the operation can be simplified without recursing
6585 on at least one of the branches once its pushed inside the COND_EXPR. */
6586 if (!TREE_CONSTANT (arg)
6587 && (TREE_SIDE_EFFECTS (arg)
6588 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6589 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6590 return NULL_TREE;
6592 arg = fold_convert_loc (loc, arg_type, arg);
6593 if (lhs == 0)
6595 true_value = fold_convert_loc (loc, cond_type, true_value);
6596 if (cond_first_p)
6597 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6598 else
6599 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6601 if (rhs == 0)
6603 false_value = fold_convert_loc (loc, cond_type, false_value);
6604 if (cond_first_p)
6605 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6606 else
6607 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6610 /* Check that we have simplified at least one of the branches. */
6611 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6612 return NULL_TREE;
6614 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6618 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6620 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6621 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6622 ADDEND is the same as X.
6624 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6625 and finite. The problematic cases are when X is zero, and its mode
6626 has signed zeros. In the case of rounding towards -infinity,
6627 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6628 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6630 bool
6631 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6633 if (!real_zerop (addend))
6634 return false;
6636 /* Don't allow the fold with -fsignaling-nans. */
6637 if (HONOR_SNANS (element_mode (type)))
6638 return false;
6640 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6641 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6642 return true;
6644 /* In a vector or complex, we would need to check the sign of all zeros. */
6645 if (TREE_CODE (addend) != REAL_CST)
6646 return false;
6648 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6649 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6650 negate = !negate;
6652 /* The mode has signed zeros, and we have to honor their sign.
6653 In this situation, there is only one case we can return true for.
6654 X - 0 is the same as X unless rounding towards -infinity is
6655 supported. */
6656 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6659 /* Subroutine of match.pd that optimizes comparisons of a division by
6660 a nonzero integer constant against an integer constant, i.e.
6661 X/C1 op C2.
6663 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6664 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6666 enum tree_code
6667 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
6668 tree *hi, bool *neg_overflow)
6670 tree prod, tmp, type = TREE_TYPE (c1);
6671 signop sign = TYPE_SIGN (type);
6672 bool overflow;
6674 /* We have to do this the hard way to detect unsigned overflow.
6675 prod = int_const_binop (MULT_EXPR, c1, c2); */
6676 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
6677 prod = force_fit_type (type, val, -1, overflow);
6678 *neg_overflow = false;
6680 if (sign == UNSIGNED)
6682 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6683 *lo = prod;
6685 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6686 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
6687 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
6689 else if (tree_int_cst_sgn (c1) >= 0)
6691 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6692 switch (tree_int_cst_sgn (c2))
6694 case -1:
6695 *neg_overflow = true;
6696 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
6697 *hi = prod;
6698 break;
6700 case 0:
6701 *lo = fold_negate_const (tmp, type);
6702 *hi = tmp;
6703 break;
6705 case 1:
6706 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
6707 *lo = prod;
6708 break;
6710 default:
6711 gcc_unreachable ();
6714 else
6716 /* A negative divisor reverses the relational operators. */
6717 code = swap_tree_comparison (code);
6719 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
6720 switch (tree_int_cst_sgn (c2))
6722 case -1:
6723 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
6724 *lo = prod;
6725 break;
6727 case 0:
6728 *hi = fold_negate_const (tmp, type);
6729 *lo = tmp;
6730 break;
6732 case 1:
6733 *neg_overflow = true;
6734 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
6735 *hi = prod;
6736 break;
6738 default:
6739 gcc_unreachable ();
6743 if (code != EQ_EXPR && code != NE_EXPR)
6744 return code;
6746 if (TREE_OVERFLOW (*lo)
6747 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
6748 *lo = NULL_TREE;
6749 if (TREE_OVERFLOW (*hi)
6750 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
6751 *hi = NULL_TREE;
6753 return code;
6757 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6758 equality/inequality test, then return a simplified form of the test
6759 using a sign testing. Otherwise return NULL. TYPE is the desired
6760 result type. */
6762 static tree
6763 fold_single_bit_test_into_sign_test (location_t loc,
6764 enum tree_code code, tree arg0, tree arg1,
6765 tree result_type)
6767 /* If this is testing a single bit, we can optimize the test. */
6768 if ((code == NE_EXPR || code == EQ_EXPR)
6769 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6770 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6772 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6773 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6774 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6776 if (arg00 != NULL_TREE
6777 /* This is only a win if casting to a signed type is cheap,
6778 i.e. when arg00's type is not a partial mode. */
6779 && type_has_mode_precision_p (TREE_TYPE (arg00)))
6781 tree stype = signed_type_for (TREE_TYPE (arg00));
6782 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6783 result_type,
6784 fold_convert_loc (loc, stype, arg00),
6785 build_int_cst (stype, 0));
6789 return NULL_TREE;
6792 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6793 equality/inequality test, then return a simplified form of
6794 the test using shifts and logical operations. Otherwise return
6795 NULL. TYPE is the desired result type. */
6797 tree
6798 fold_single_bit_test (location_t loc, enum tree_code code,
6799 tree arg0, tree arg1, tree result_type)
6801 /* If this is testing a single bit, we can optimize the test. */
6802 if ((code == NE_EXPR || code == EQ_EXPR)
6803 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6804 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6806 tree inner = TREE_OPERAND (arg0, 0);
6807 tree type = TREE_TYPE (arg0);
6808 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6809 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
6810 int ops_unsigned;
6811 tree signed_type, unsigned_type, intermediate_type;
6812 tree tem, one;
6814 /* First, see if we can fold the single bit test into a sign-bit
6815 test. */
6816 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6817 result_type);
6818 if (tem)
6819 return tem;
6821 /* Otherwise we have (A & C) != 0 where C is a single bit,
6822 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6823 Similarly for (A & C) == 0. */
6825 /* If INNER is a right shift of a constant and it plus BITNUM does
6826 not overflow, adjust BITNUM and INNER. */
6827 if (TREE_CODE (inner) == RSHIFT_EXPR
6828 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6829 && bitnum < TYPE_PRECISION (type)
6830 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner, 1)),
6831 TYPE_PRECISION (type) - bitnum))
6833 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6834 inner = TREE_OPERAND (inner, 0);
6837 /* If we are going to be able to omit the AND below, we must do our
6838 operations as unsigned. If we must use the AND, we have a choice.
6839 Normally unsigned is faster, but for some machines signed is. */
6840 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6841 && !flag_syntax_only) ? 0 : 1;
6843 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6844 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6845 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6846 inner = fold_convert_loc (loc, intermediate_type, inner);
6848 if (bitnum != 0)
6849 inner = build2 (RSHIFT_EXPR, intermediate_type,
6850 inner, size_int (bitnum));
6852 one = build_int_cst (intermediate_type, 1);
6854 if (code == EQ_EXPR)
6855 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6857 /* Put the AND last so it can combine with more things. */
6858 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6860 /* Make sure to return the proper type. */
6861 inner = fold_convert_loc (loc, result_type, inner);
6863 return inner;
6865 return NULL_TREE;
6868 /* Test whether it is preferable two swap two operands, ARG0 and
6869 ARG1, for example because ARG0 is an integer constant and ARG1
6870 isn't. */
6872 bool
6873 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6875 if (CONSTANT_CLASS_P (arg1))
6876 return 0;
6877 if (CONSTANT_CLASS_P (arg0))
6878 return 1;
6880 STRIP_NOPS (arg0);
6881 STRIP_NOPS (arg1);
6883 if (TREE_CONSTANT (arg1))
6884 return 0;
6885 if (TREE_CONSTANT (arg0))
6886 return 1;
6888 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6889 for commutative and comparison operators. Ensuring a canonical
6890 form allows the optimizers to find additional redundancies without
6891 having to explicitly check for both orderings. */
6892 if (TREE_CODE (arg0) == SSA_NAME
6893 && TREE_CODE (arg1) == SSA_NAME
6894 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6895 return 1;
6897 /* Put SSA_NAMEs last. */
6898 if (TREE_CODE (arg1) == SSA_NAME)
6899 return 0;
6900 if (TREE_CODE (arg0) == SSA_NAME)
6901 return 1;
6903 /* Put variables last. */
6904 if (DECL_P (arg1))
6905 return 0;
6906 if (DECL_P (arg0))
6907 return 1;
6909 return 0;
6913 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6914 means A >= Y && A != MAX, but in this case we know that
6915 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6917 static tree
6918 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6920 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6922 if (TREE_CODE (bound) == LT_EXPR)
6923 a = TREE_OPERAND (bound, 0);
6924 else if (TREE_CODE (bound) == GT_EXPR)
6925 a = TREE_OPERAND (bound, 1);
6926 else
6927 return NULL_TREE;
6929 typea = TREE_TYPE (a);
6930 if (!INTEGRAL_TYPE_P (typea)
6931 && !POINTER_TYPE_P (typea))
6932 return NULL_TREE;
6934 if (TREE_CODE (ineq) == LT_EXPR)
6936 a1 = TREE_OPERAND (ineq, 1);
6937 y = TREE_OPERAND (ineq, 0);
6939 else if (TREE_CODE (ineq) == GT_EXPR)
6941 a1 = TREE_OPERAND (ineq, 0);
6942 y = TREE_OPERAND (ineq, 1);
6944 else
6945 return NULL_TREE;
6947 if (TREE_TYPE (a1) != typea)
6948 return NULL_TREE;
6950 if (POINTER_TYPE_P (typea))
6952 /* Convert the pointer types into integer before taking the difference. */
6953 tree ta = fold_convert_loc (loc, ssizetype, a);
6954 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6955 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6957 else
6958 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6960 if (!diff || !integer_onep (diff))
6961 return NULL_TREE;
6963 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6966 /* Fold a sum or difference of at least one multiplication.
6967 Returns the folded tree or NULL if no simplification could be made. */
6969 static tree
6970 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6971 tree arg0, tree arg1)
6973 tree arg00, arg01, arg10, arg11;
6974 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6976 /* (A * C) +- (B * C) -> (A+-B) * C.
6977 (A * C) +- A -> A * (C+-1).
6978 We are most concerned about the case where C is a constant,
6979 but other combinations show up during loop reduction. Since
6980 it is not difficult, try all four possibilities. */
6982 if (TREE_CODE (arg0) == MULT_EXPR)
6984 arg00 = TREE_OPERAND (arg0, 0);
6985 arg01 = TREE_OPERAND (arg0, 1);
6987 else if (TREE_CODE (arg0) == INTEGER_CST)
6989 arg00 = build_one_cst (type);
6990 arg01 = arg0;
6992 else
6994 /* We cannot generate constant 1 for fract. */
6995 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6996 return NULL_TREE;
6997 arg00 = arg0;
6998 arg01 = build_one_cst (type);
7000 if (TREE_CODE (arg1) == MULT_EXPR)
7002 arg10 = TREE_OPERAND (arg1, 0);
7003 arg11 = TREE_OPERAND (arg1, 1);
7005 else if (TREE_CODE (arg1) == INTEGER_CST)
7007 arg10 = build_one_cst (type);
7008 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7009 the purpose of this canonicalization. */
7010 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7011 && negate_expr_p (arg1)
7012 && code == PLUS_EXPR)
7014 arg11 = negate_expr (arg1);
7015 code = MINUS_EXPR;
7017 else
7018 arg11 = arg1;
7020 else
7022 /* We cannot generate constant 1 for fract. */
7023 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7024 return NULL_TREE;
7025 arg10 = arg1;
7026 arg11 = build_one_cst (type);
7028 same = NULL_TREE;
7030 /* Prefer factoring a common non-constant. */
7031 if (operand_equal_p (arg00, arg10, 0))
7032 same = arg00, alt0 = arg01, alt1 = arg11;
7033 else if (operand_equal_p (arg01, arg11, 0))
7034 same = arg01, alt0 = arg00, alt1 = arg10;
7035 else if (operand_equal_p (arg00, arg11, 0))
7036 same = arg00, alt0 = arg01, alt1 = arg10;
7037 else if (operand_equal_p (arg01, arg10, 0))
7038 same = arg01, alt0 = arg00, alt1 = arg11;
7040 /* No identical multiplicands; see if we can find a common
7041 power-of-two factor in non-power-of-two multiplies. This
7042 can help in multi-dimensional array access. */
7043 else if (tree_fits_shwi_p (arg01)
7044 && tree_fits_shwi_p (arg11))
7046 HOST_WIDE_INT int01, int11, tmp;
7047 bool swap = false;
7048 tree maybe_same;
7049 int01 = tree_to_shwi (arg01);
7050 int11 = tree_to_shwi (arg11);
7052 /* Move min of absolute values to int11. */
7053 if (absu_hwi (int01) < absu_hwi (int11))
7055 tmp = int01, int01 = int11, int11 = tmp;
7056 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7057 maybe_same = arg01;
7058 swap = true;
7060 else
7061 maybe_same = arg11;
7063 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7064 /* The remainder should not be a constant, otherwise we
7065 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7066 increased the number of multiplications necessary. */
7067 && TREE_CODE (arg10) != INTEGER_CST)
7069 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7070 build_int_cst (TREE_TYPE (arg00),
7071 int01 / int11));
7072 alt1 = arg10;
7073 same = maybe_same;
7074 if (swap)
7075 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7079 if (!same)
7080 return NULL_TREE;
7082 if (! INTEGRAL_TYPE_P (type)
7083 || TYPE_OVERFLOW_WRAPS (type)
7084 /* We are neither factoring zero nor minus one. */
7085 || TREE_CODE (same) == INTEGER_CST)
7086 return fold_build2_loc (loc, MULT_EXPR, type,
7087 fold_build2_loc (loc, code, type,
7088 fold_convert_loc (loc, type, alt0),
7089 fold_convert_loc (loc, type, alt1)),
7090 fold_convert_loc (loc, type, same));
7092 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7093 same may be minus one and thus the multiplication may overflow. Perform
7094 the sum operation in an unsigned type. */
7095 tree utype = unsigned_type_for (type);
7096 tree tem = fold_build2_loc (loc, code, utype,
7097 fold_convert_loc (loc, utype, alt0),
7098 fold_convert_loc (loc, utype, alt1));
7099 /* If the sum evaluated to a constant that is not -INF the multiplication
7100 cannot overflow. */
7101 if (TREE_CODE (tem) == INTEGER_CST
7102 && (wi::to_wide (tem)
7103 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7104 return fold_build2_loc (loc, MULT_EXPR, type,
7105 fold_convert (type, tem), same);
7107 /* Do not resort to unsigned multiplication because
7108 we lose the no-overflow property of the expression. */
7109 return NULL_TREE;
7112 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7113 specified by EXPR into the buffer PTR of length LEN bytes.
7114 Return the number of bytes placed in the buffer, or zero
7115 upon failure. */
7117 static int
7118 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7120 tree type = TREE_TYPE (expr);
7121 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7122 int byte, offset, word, words;
7123 unsigned char value;
7125 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7126 return 0;
7127 if (off == -1)
7128 off = 0;
7130 if (ptr == NULL)
7131 /* Dry run. */
7132 return MIN (len, total_bytes - off);
7134 words = total_bytes / UNITS_PER_WORD;
7136 for (byte = 0; byte < total_bytes; byte++)
7138 int bitpos = byte * BITS_PER_UNIT;
7139 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7140 number of bytes. */
7141 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7143 if (total_bytes > UNITS_PER_WORD)
7145 word = byte / UNITS_PER_WORD;
7146 if (WORDS_BIG_ENDIAN)
7147 word = (words - 1) - word;
7148 offset = word * UNITS_PER_WORD;
7149 if (BYTES_BIG_ENDIAN)
7150 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7151 else
7152 offset += byte % UNITS_PER_WORD;
7154 else
7155 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7156 if (offset >= off && offset - off < len)
7157 ptr[offset - off] = value;
7159 return MIN (len, total_bytes - off);
7163 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7164 specified by EXPR into the buffer PTR of length LEN bytes.
7165 Return the number of bytes placed in the buffer, or zero
7166 upon failure. */
7168 static int
7169 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7171 tree type = TREE_TYPE (expr);
7172 scalar_mode mode = SCALAR_TYPE_MODE (type);
7173 int total_bytes = GET_MODE_SIZE (mode);
7174 FIXED_VALUE_TYPE value;
7175 tree i_value, i_type;
7177 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7178 return 0;
7180 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7182 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7183 return 0;
7185 value = TREE_FIXED_CST (expr);
7186 i_value = double_int_to_tree (i_type, value.data);
7188 return native_encode_int (i_value, ptr, len, off);
7192 /* Subroutine of native_encode_expr. Encode the REAL_CST
7193 specified by EXPR into the buffer PTR of length LEN bytes.
7194 Return the number of bytes placed in the buffer, or zero
7195 upon failure. */
7197 static int
7198 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7200 tree type = TREE_TYPE (expr);
7201 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7202 int byte, offset, word, words, bitpos;
7203 unsigned char value;
7205 /* There are always 32 bits in each long, no matter the size of
7206 the hosts long. We handle floating point representations with
7207 up to 192 bits. */
7208 long tmp[6];
7210 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7211 return 0;
7212 if (off == -1)
7213 off = 0;
7215 if (ptr == NULL)
7216 /* Dry run. */
7217 return MIN (len, total_bytes - off);
7219 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7221 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7223 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7224 bitpos += BITS_PER_UNIT)
7226 byte = (bitpos / BITS_PER_UNIT) & 3;
7227 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7229 if (UNITS_PER_WORD < 4)
7231 word = byte / UNITS_PER_WORD;
7232 if (WORDS_BIG_ENDIAN)
7233 word = (words - 1) - word;
7234 offset = word * UNITS_PER_WORD;
7235 if (BYTES_BIG_ENDIAN)
7236 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7237 else
7238 offset += byte % UNITS_PER_WORD;
7240 else
7242 offset = byte;
7243 if (BYTES_BIG_ENDIAN)
7245 /* Reverse bytes within each long, or within the entire float
7246 if it's smaller than a long (for HFmode). */
7247 offset = MIN (3, total_bytes - 1) - offset;
7248 gcc_assert (offset >= 0);
7251 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7252 if (offset >= off
7253 && offset - off < len)
7254 ptr[offset - off] = value;
7256 return MIN (len, total_bytes - off);
7259 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7260 specified by EXPR into the buffer PTR of length LEN bytes.
7261 Return the number of bytes placed in the buffer, or zero
7262 upon failure. */
7264 static int
7265 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7267 int rsize, isize;
7268 tree part;
7270 part = TREE_REALPART (expr);
7271 rsize = native_encode_expr (part, ptr, len, off);
7272 if (off == -1 && rsize == 0)
7273 return 0;
7274 part = TREE_IMAGPART (expr);
7275 if (off != -1)
7276 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7277 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7278 len - rsize, off);
7279 if (off == -1 && isize != rsize)
7280 return 0;
7281 return rsize + isize;
7285 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7286 specified by EXPR into the buffer PTR of length LEN bytes.
7287 Return the number of bytes placed in the buffer, or zero
7288 upon failure. */
7290 static int
7291 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7293 unsigned HOST_WIDE_INT i, count;
7294 int size, offset;
7295 tree itype, elem;
7297 offset = 0;
7298 if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7299 return 0;
7300 itype = TREE_TYPE (TREE_TYPE (expr));
7301 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7302 for (i = 0; i < count; i++)
7304 if (off >= size)
7306 off -= size;
7307 continue;
7309 elem = VECTOR_CST_ELT (expr, i);
7310 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7311 len - offset, off);
7312 if ((off == -1 && res != size) || res == 0)
7313 return 0;
7314 offset += res;
7315 if (offset >= len)
7316 return (off == -1 && i < count - 1) ? 0 : offset;
7317 if (off != -1)
7318 off = 0;
7320 return offset;
7324 /* Subroutine of native_encode_expr. Encode the STRING_CST
7325 specified by EXPR into the buffer PTR of length LEN bytes.
7326 Return the number of bytes placed in the buffer, or zero
7327 upon failure. */
7329 static int
7330 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7332 tree type = TREE_TYPE (expr);
7334 /* Wide-char strings are encoded in target byte-order so native
7335 encoding them is trivial. */
7336 if (BITS_PER_UNIT != CHAR_BIT
7337 || TREE_CODE (type) != ARRAY_TYPE
7338 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7339 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7340 return 0;
7342 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7343 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7344 return 0;
7345 if (off == -1)
7346 off = 0;
7347 if (ptr == NULL)
7348 /* Dry run. */;
7349 else if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7351 int written = 0;
7352 if (off < TREE_STRING_LENGTH (expr))
7354 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7355 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7357 memset (ptr + written, 0,
7358 MIN (total_bytes - written, len - written));
7360 else
7361 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7362 return MIN (total_bytes - off, len);
7366 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7367 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7368 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7369 anything, just do a dry run. If OFF is not -1 then start
7370 the encoding at byte offset OFF and encode at most LEN bytes.
7371 Return the number of bytes placed in the buffer, or zero upon failure. */
7374 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7376 /* We don't support starting at negative offset and -1 is special. */
7377 if (off < -1)
7378 return 0;
7380 switch (TREE_CODE (expr))
7382 case INTEGER_CST:
7383 return native_encode_int (expr, ptr, len, off);
7385 case REAL_CST:
7386 return native_encode_real (expr, ptr, len, off);
7388 case FIXED_CST:
7389 return native_encode_fixed (expr, ptr, len, off);
7391 case COMPLEX_CST:
7392 return native_encode_complex (expr, ptr, len, off);
7394 case VECTOR_CST:
7395 return native_encode_vector (expr, ptr, len, off);
7397 case STRING_CST:
7398 return native_encode_string (expr, ptr, len, off);
7400 default:
7401 return 0;
7406 /* Subroutine of native_interpret_expr. Interpret the contents of
7407 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7408 If the buffer cannot be interpreted, return NULL_TREE. */
7410 static tree
7411 native_interpret_int (tree type, const unsigned char *ptr, int len)
7413 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7415 if (total_bytes > len
7416 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7417 return NULL_TREE;
7419 wide_int result = wi::from_buffer (ptr, total_bytes);
7421 return wide_int_to_tree (type, result);
7425 /* Subroutine of native_interpret_expr. Interpret the contents of
7426 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7427 If the buffer cannot be interpreted, return NULL_TREE. */
7429 static tree
7430 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7432 scalar_mode mode = SCALAR_TYPE_MODE (type);
7433 int total_bytes = GET_MODE_SIZE (mode);
7434 double_int result;
7435 FIXED_VALUE_TYPE fixed_value;
7437 if (total_bytes > len
7438 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7439 return NULL_TREE;
7441 result = double_int::from_buffer (ptr, total_bytes);
7442 fixed_value = fixed_from_double_int (result, mode);
7444 return build_fixed (type, fixed_value);
7448 /* Subroutine of native_interpret_expr. Interpret the contents of
7449 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7450 If the buffer cannot be interpreted, return NULL_TREE. */
7452 static tree
7453 native_interpret_real (tree type, const unsigned char *ptr, int len)
7455 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
7456 int total_bytes = GET_MODE_SIZE (mode);
7457 unsigned char value;
7458 /* There are always 32 bits in each long, no matter the size of
7459 the hosts long. We handle floating point representations with
7460 up to 192 bits. */
7461 REAL_VALUE_TYPE r;
7462 long tmp[6];
7464 if (total_bytes > len || total_bytes > 24)
7465 return NULL_TREE;
7466 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7468 memset (tmp, 0, sizeof (tmp));
7469 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7470 bitpos += BITS_PER_UNIT)
7472 /* Both OFFSET and BYTE index within a long;
7473 bitpos indexes the whole float. */
7474 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7475 if (UNITS_PER_WORD < 4)
7477 int word = byte / UNITS_PER_WORD;
7478 if (WORDS_BIG_ENDIAN)
7479 word = (words - 1) - word;
7480 offset = word * UNITS_PER_WORD;
7481 if (BYTES_BIG_ENDIAN)
7482 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7483 else
7484 offset += byte % UNITS_PER_WORD;
7486 else
7488 offset = byte;
7489 if (BYTES_BIG_ENDIAN)
7491 /* Reverse bytes within each long, or within the entire float
7492 if it's smaller than a long (for HFmode). */
7493 offset = MIN (3, total_bytes - 1) - offset;
7494 gcc_assert (offset >= 0);
7497 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7499 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7502 real_from_target (&r, tmp, mode);
7503 return build_real (type, r);
7507 /* Subroutine of native_interpret_expr. Interpret the contents of
7508 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7509 If the buffer cannot be interpreted, return NULL_TREE. */
7511 static tree
7512 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7514 tree etype, rpart, ipart;
7515 int size;
7517 etype = TREE_TYPE (type);
7518 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7519 if (size * 2 > len)
7520 return NULL_TREE;
7521 rpart = native_interpret_expr (etype, ptr, size);
7522 if (!rpart)
7523 return NULL_TREE;
7524 ipart = native_interpret_expr (etype, ptr+size, size);
7525 if (!ipart)
7526 return NULL_TREE;
7527 return build_complex (type, rpart, ipart);
7531 /* Subroutine of native_interpret_expr. Interpret the contents of
7532 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7533 If the buffer cannot be interpreted, return NULL_TREE. */
7535 static tree
7536 native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
7538 tree etype, elem;
7539 unsigned int i, size;
7540 unsigned HOST_WIDE_INT count;
7542 etype = TREE_TYPE (type);
7543 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7544 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&count)
7545 || size * count > len)
7546 return NULL_TREE;
7548 tree_vector_builder elements (type, count, 1);
7549 for (i = 0; i < count; ++i)
7551 elem = native_interpret_expr (etype, ptr+(i*size), size);
7552 if (!elem)
7553 return NULL_TREE;
7554 elements.quick_push (elem);
7556 return elements.build ();
7560 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7561 the buffer PTR of length LEN as a constant of type TYPE. For
7562 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7563 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7564 return NULL_TREE. */
7566 tree
7567 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7569 switch (TREE_CODE (type))
7571 case INTEGER_TYPE:
7572 case ENUMERAL_TYPE:
7573 case BOOLEAN_TYPE:
7574 case POINTER_TYPE:
7575 case REFERENCE_TYPE:
7576 return native_interpret_int (type, ptr, len);
7578 case REAL_TYPE:
7579 return native_interpret_real (type, ptr, len);
7581 case FIXED_POINT_TYPE:
7582 return native_interpret_fixed (type, ptr, len);
7584 case COMPLEX_TYPE:
7585 return native_interpret_complex (type, ptr, len);
7587 case VECTOR_TYPE:
7588 return native_interpret_vector (type, ptr, len);
7590 default:
7591 return NULL_TREE;
7595 /* Returns true if we can interpret the contents of a native encoding
7596 as TYPE. */
7598 static bool
7599 can_native_interpret_type_p (tree type)
7601 switch (TREE_CODE (type))
7603 case INTEGER_TYPE:
7604 case ENUMERAL_TYPE:
7605 case BOOLEAN_TYPE:
7606 case POINTER_TYPE:
7607 case REFERENCE_TYPE:
7608 case FIXED_POINT_TYPE:
7609 case REAL_TYPE:
7610 case COMPLEX_TYPE:
7611 case VECTOR_TYPE:
7612 return true;
7613 default:
7614 return false;
7619 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7620 TYPE at compile-time. If we're unable to perform the conversion
7621 return NULL_TREE. */
7623 static tree
7624 fold_view_convert_expr (tree type, tree expr)
7626 /* We support up to 512-bit values (for V8DFmode). */
7627 unsigned char buffer[64];
7628 int len;
7630 /* Check that the host and target are sane. */
7631 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7632 return NULL_TREE;
7634 len = native_encode_expr (expr, buffer, sizeof (buffer));
7635 if (len == 0)
7636 return NULL_TREE;
7638 return native_interpret_expr (type, buffer, len);
7641 /* Build an expression for the address of T. Folds away INDIRECT_REF
7642 to avoid confusing the gimplify process. */
7644 tree
7645 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7647 /* The size of the object is not relevant when talking about its address. */
7648 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7649 t = TREE_OPERAND (t, 0);
7651 if (TREE_CODE (t) == INDIRECT_REF)
7653 t = TREE_OPERAND (t, 0);
7655 if (TREE_TYPE (t) != ptrtype)
7656 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7658 else if (TREE_CODE (t) == MEM_REF
7659 && integer_zerop (TREE_OPERAND (t, 1)))
7660 return TREE_OPERAND (t, 0);
7661 else if (TREE_CODE (t) == MEM_REF
7662 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7663 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7664 TREE_OPERAND (t, 0),
7665 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7666 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7668 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7670 if (TREE_TYPE (t) != ptrtype)
7671 t = fold_convert_loc (loc, ptrtype, t);
7673 else
7674 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7676 return t;
7679 /* Build an expression for the address of T. */
7681 tree
7682 build_fold_addr_expr_loc (location_t loc, tree t)
7684 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7686 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7689 /* Fold a unary expression of code CODE and type TYPE with operand
7690 OP0. Return the folded expression if folding is successful.
7691 Otherwise, return NULL_TREE. */
7693 tree
7694 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7696 tree tem;
7697 tree arg0;
7698 enum tree_code_class kind = TREE_CODE_CLASS (code);
7700 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7701 && TREE_CODE_LENGTH (code) == 1);
7703 arg0 = op0;
7704 if (arg0)
7706 if (CONVERT_EXPR_CODE_P (code)
7707 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7709 /* Don't use STRIP_NOPS, because signedness of argument type
7710 matters. */
7711 STRIP_SIGN_NOPS (arg0);
7713 else
7715 /* Strip any conversions that don't change the mode. This
7716 is safe for every expression, except for a comparison
7717 expression because its signedness is derived from its
7718 operands.
7720 Note that this is done as an internal manipulation within
7721 the constant folder, in order to find the simplest
7722 representation of the arguments so that their form can be
7723 studied. In any cases, the appropriate type conversions
7724 should be put back in the tree that will get out of the
7725 constant folder. */
7726 STRIP_NOPS (arg0);
7729 if (CONSTANT_CLASS_P (arg0))
7731 tree tem = const_unop (code, type, arg0);
7732 if (tem)
7734 if (TREE_TYPE (tem) != type)
7735 tem = fold_convert_loc (loc, type, tem);
7736 return tem;
7741 tem = generic_simplify (loc, code, type, op0);
7742 if (tem)
7743 return tem;
7745 if (TREE_CODE_CLASS (code) == tcc_unary)
7747 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7748 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7749 fold_build1_loc (loc, code, type,
7750 fold_convert_loc (loc, TREE_TYPE (op0),
7751 TREE_OPERAND (arg0, 1))));
7752 else if (TREE_CODE (arg0) == COND_EXPR)
7754 tree arg01 = TREE_OPERAND (arg0, 1);
7755 tree arg02 = TREE_OPERAND (arg0, 2);
7756 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7757 arg01 = fold_build1_loc (loc, code, type,
7758 fold_convert_loc (loc,
7759 TREE_TYPE (op0), arg01));
7760 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7761 arg02 = fold_build1_loc (loc, code, type,
7762 fold_convert_loc (loc,
7763 TREE_TYPE (op0), arg02));
7764 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7765 arg01, arg02);
7767 /* If this was a conversion, and all we did was to move into
7768 inside the COND_EXPR, bring it back out. But leave it if
7769 it is a conversion from integer to integer and the
7770 result precision is no wider than a word since such a
7771 conversion is cheap and may be optimized away by combine,
7772 while it couldn't if it were outside the COND_EXPR. Then return
7773 so we don't get into an infinite recursion loop taking the
7774 conversion out and then back in. */
7776 if ((CONVERT_EXPR_CODE_P (code)
7777 || code == NON_LVALUE_EXPR)
7778 && TREE_CODE (tem) == COND_EXPR
7779 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7780 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7781 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7782 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7783 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7784 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7785 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7786 && (INTEGRAL_TYPE_P
7787 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7788 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7789 || flag_syntax_only))
7790 tem = build1_loc (loc, code, type,
7791 build3 (COND_EXPR,
7792 TREE_TYPE (TREE_OPERAND
7793 (TREE_OPERAND (tem, 1), 0)),
7794 TREE_OPERAND (tem, 0),
7795 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7796 TREE_OPERAND (TREE_OPERAND (tem, 2),
7797 0)));
7798 return tem;
7802 switch (code)
7804 case NON_LVALUE_EXPR:
7805 if (!maybe_lvalue_p (op0))
7806 return fold_convert_loc (loc, type, op0);
7807 return NULL_TREE;
7809 CASE_CONVERT:
7810 case FLOAT_EXPR:
7811 case FIX_TRUNC_EXPR:
7812 if (COMPARISON_CLASS_P (op0))
7814 /* If we have (type) (a CMP b) and type is an integral type, return
7815 new expression involving the new type. Canonicalize
7816 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7817 non-integral type.
7818 Do not fold the result as that would not simplify further, also
7819 folding again results in recursions. */
7820 if (TREE_CODE (type) == BOOLEAN_TYPE)
7821 return build2_loc (loc, TREE_CODE (op0), type,
7822 TREE_OPERAND (op0, 0),
7823 TREE_OPERAND (op0, 1));
7824 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7825 && TREE_CODE (type) != VECTOR_TYPE)
7826 return build3_loc (loc, COND_EXPR, type, op0,
7827 constant_boolean_node (true, type),
7828 constant_boolean_node (false, type));
7831 /* Handle (T *)&A.B.C for A being of type T and B and C
7832 living at offset zero. This occurs frequently in
7833 C++ upcasting and then accessing the base. */
7834 if (TREE_CODE (op0) == ADDR_EXPR
7835 && POINTER_TYPE_P (type)
7836 && handled_component_p (TREE_OPERAND (op0, 0)))
7838 poly_int64 bitsize, bitpos;
7839 tree offset;
7840 machine_mode mode;
7841 int unsignedp, reversep, volatilep;
7842 tree base
7843 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7844 &offset, &mode, &unsignedp, &reversep,
7845 &volatilep);
7846 /* If the reference was to a (constant) zero offset, we can use
7847 the address of the base if it has the same base type
7848 as the result type and the pointer type is unqualified. */
7849 if (!offset
7850 && known_eq (bitpos, 0)
7851 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7852 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7853 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7854 return fold_convert_loc (loc, type,
7855 build_fold_addr_expr_loc (loc, base));
7858 if (TREE_CODE (op0) == MODIFY_EXPR
7859 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7860 /* Detect assigning a bitfield. */
7861 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7862 && DECL_BIT_FIELD
7863 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7865 /* Don't leave an assignment inside a conversion
7866 unless assigning a bitfield. */
7867 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7868 /* First do the assignment, then return converted constant. */
7869 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7870 TREE_NO_WARNING (tem) = 1;
7871 TREE_USED (tem) = 1;
7872 return tem;
7875 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7876 constants (if x has signed type, the sign bit cannot be set
7877 in c). This folds extension into the BIT_AND_EXPR.
7878 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7879 very likely don't have maximal range for their precision and this
7880 transformation effectively doesn't preserve non-maximal ranges. */
7881 if (TREE_CODE (type) == INTEGER_TYPE
7882 && TREE_CODE (op0) == BIT_AND_EXPR
7883 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7885 tree and_expr = op0;
7886 tree and0 = TREE_OPERAND (and_expr, 0);
7887 tree and1 = TREE_OPERAND (and_expr, 1);
7888 int change = 0;
7890 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7891 || (TYPE_PRECISION (type)
7892 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7893 change = 1;
7894 else if (TYPE_PRECISION (TREE_TYPE (and1))
7895 <= HOST_BITS_PER_WIDE_INT
7896 && tree_fits_uhwi_p (and1))
7898 unsigned HOST_WIDE_INT cst;
7900 cst = tree_to_uhwi (and1);
7901 cst &= HOST_WIDE_INT_M1U
7902 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7903 change = (cst == 0);
7904 if (change
7905 && !flag_syntax_only
7906 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7907 == ZERO_EXTEND))
7909 tree uns = unsigned_type_for (TREE_TYPE (and0));
7910 and0 = fold_convert_loc (loc, uns, and0);
7911 and1 = fold_convert_loc (loc, uns, and1);
7914 if (change)
7916 tem = force_fit_type (type, wi::to_widest (and1), 0,
7917 TREE_OVERFLOW (and1));
7918 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7919 fold_convert_loc (loc, type, and0), tem);
7923 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7924 cast (T1)X will fold away. We assume that this happens when X itself
7925 is a cast. */
7926 if (POINTER_TYPE_P (type)
7927 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7928 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7930 tree arg00 = TREE_OPERAND (arg0, 0);
7931 tree arg01 = TREE_OPERAND (arg0, 1);
7933 return fold_build_pointer_plus_loc
7934 (loc, fold_convert_loc (loc, type, arg00), arg01);
7937 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7938 of the same precision, and X is an integer type not narrower than
7939 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7940 if (INTEGRAL_TYPE_P (type)
7941 && TREE_CODE (op0) == BIT_NOT_EXPR
7942 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7943 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7944 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7946 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7947 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7948 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7949 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7950 fold_convert_loc (loc, type, tem));
7953 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7954 type of X and Y (integer types only). */
7955 if (INTEGRAL_TYPE_P (type)
7956 && TREE_CODE (op0) == MULT_EXPR
7957 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7958 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7960 /* Be careful not to introduce new overflows. */
7961 tree mult_type;
7962 if (TYPE_OVERFLOW_WRAPS (type))
7963 mult_type = type;
7964 else
7965 mult_type = unsigned_type_for (type);
7967 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7969 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7970 fold_convert_loc (loc, mult_type,
7971 TREE_OPERAND (op0, 0)),
7972 fold_convert_loc (loc, mult_type,
7973 TREE_OPERAND (op0, 1)));
7974 return fold_convert_loc (loc, type, tem);
7978 return NULL_TREE;
7980 case VIEW_CONVERT_EXPR:
7981 if (TREE_CODE (op0) == MEM_REF)
7983 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7984 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7985 tem = fold_build2_loc (loc, MEM_REF, type,
7986 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7987 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7988 return tem;
7991 return NULL_TREE;
7993 case NEGATE_EXPR:
7994 tem = fold_negate_expr (loc, arg0);
7995 if (tem)
7996 return fold_convert_loc (loc, type, tem);
7997 return NULL_TREE;
7999 case ABS_EXPR:
8000 /* Convert fabs((double)float) into (double)fabsf(float). */
8001 if (TREE_CODE (arg0) == NOP_EXPR
8002 && TREE_CODE (type) == REAL_TYPE)
8004 tree targ0 = strip_float_extensions (arg0);
8005 if (targ0 != arg0)
8006 return fold_convert_loc (loc, type,
8007 fold_build1_loc (loc, ABS_EXPR,
8008 TREE_TYPE (targ0),
8009 targ0));
8011 return NULL_TREE;
8013 case BIT_NOT_EXPR:
8014 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8015 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8016 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8017 fold_convert_loc (loc, type,
8018 TREE_OPERAND (arg0, 0)))))
8019 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8020 fold_convert_loc (loc, type,
8021 TREE_OPERAND (arg0, 1)));
8022 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8023 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8024 fold_convert_loc (loc, type,
8025 TREE_OPERAND (arg0, 1)))))
8026 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8027 fold_convert_loc (loc, type,
8028 TREE_OPERAND (arg0, 0)), tem);
8030 return NULL_TREE;
8032 case TRUTH_NOT_EXPR:
8033 /* Note that the operand of this must be an int
8034 and its values must be 0 or 1.
8035 ("true" is a fixed value perhaps depending on the language,
8036 but we don't handle values other than 1 correctly yet.) */
8037 tem = fold_truth_not_expr (loc, arg0);
8038 if (!tem)
8039 return NULL_TREE;
8040 return fold_convert_loc (loc, type, tem);
8042 case INDIRECT_REF:
8043 /* Fold *&X to X if X is an lvalue. */
8044 if (TREE_CODE (op0) == ADDR_EXPR)
8046 tree op00 = TREE_OPERAND (op0, 0);
8047 if ((VAR_P (op00)
8048 || TREE_CODE (op00) == PARM_DECL
8049 || TREE_CODE (op00) == RESULT_DECL)
8050 && !TREE_READONLY (op00))
8051 return op00;
8053 return NULL_TREE;
8055 default:
8056 return NULL_TREE;
8057 } /* switch (code) */
8061 /* If the operation was a conversion do _not_ mark a resulting constant
8062 with TREE_OVERFLOW if the original constant was not. These conversions
8063 have implementation defined behavior and retaining the TREE_OVERFLOW
8064 flag here would confuse later passes such as VRP. */
8065 tree
8066 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8067 tree type, tree op0)
8069 tree res = fold_unary_loc (loc, code, type, op0);
8070 if (res
8071 && TREE_CODE (res) == INTEGER_CST
8072 && TREE_CODE (op0) == INTEGER_CST
8073 && CONVERT_EXPR_CODE_P (code))
8074 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8076 return res;
8079 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8080 operands OP0 and OP1. LOC is the location of the resulting expression.
8081 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8082 Return the folded expression if folding is successful. Otherwise,
8083 return NULL_TREE. */
8084 static tree
8085 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8086 tree arg0, tree arg1, tree op0, tree op1)
8088 tree tem;
8090 /* We only do these simplifications if we are optimizing. */
8091 if (!optimize)
8092 return NULL_TREE;
8094 /* Check for things like (A || B) && (A || C). We can convert this
8095 to A || (B && C). Note that either operator can be any of the four
8096 truth and/or operations and the transformation will still be
8097 valid. Also note that we only care about order for the
8098 ANDIF and ORIF operators. If B contains side effects, this
8099 might change the truth-value of A. */
8100 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8101 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8102 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8103 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8104 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8105 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8107 tree a00 = TREE_OPERAND (arg0, 0);
8108 tree a01 = TREE_OPERAND (arg0, 1);
8109 tree a10 = TREE_OPERAND (arg1, 0);
8110 tree a11 = TREE_OPERAND (arg1, 1);
8111 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8112 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8113 && (code == TRUTH_AND_EXPR
8114 || code == TRUTH_OR_EXPR));
8116 if (operand_equal_p (a00, a10, 0))
8117 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8118 fold_build2_loc (loc, code, type, a01, a11));
8119 else if (commutative && operand_equal_p (a00, a11, 0))
8120 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8121 fold_build2_loc (loc, code, type, a01, a10));
8122 else if (commutative && operand_equal_p (a01, a10, 0))
8123 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8124 fold_build2_loc (loc, code, type, a00, a11));
8126 /* This case if tricky because we must either have commutative
8127 operators or else A10 must not have side-effects. */
8129 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8130 && operand_equal_p (a01, a11, 0))
8131 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8132 fold_build2_loc (loc, code, type, a00, a10),
8133 a01);
8136 /* See if we can build a range comparison. */
8137 if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
8138 return tem;
8140 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8141 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8143 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8144 if (tem)
8145 return fold_build2_loc (loc, code, type, tem, arg1);
8148 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8149 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8151 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8152 if (tem)
8153 return fold_build2_loc (loc, code, type, arg0, tem);
8156 /* Check for the possibility of merging component references. If our
8157 lhs is another similar operation, try to merge its rhs with our
8158 rhs. Then try to merge our lhs and rhs. */
8159 if (TREE_CODE (arg0) == code
8160 && (tem = fold_truth_andor_1 (loc, code, type,
8161 TREE_OPERAND (arg0, 1), arg1)) != 0)
8162 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8164 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8165 return tem;
8167 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8168 && !flag_sanitize_coverage
8169 && (code == TRUTH_AND_EXPR
8170 || code == TRUTH_ANDIF_EXPR
8171 || code == TRUTH_OR_EXPR
8172 || code == TRUTH_ORIF_EXPR))
8174 enum tree_code ncode, icode;
8176 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8177 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8178 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8180 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8181 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8182 We don't want to pack more than two leafs to a non-IF AND/OR
8183 expression.
8184 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8185 equal to IF-CODE, then we don't want to add right-hand operand.
8186 If the inner right-hand side of left-hand operand has
8187 side-effects, or isn't simple, then we can't add to it,
8188 as otherwise we might destroy if-sequence. */
8189 if (TREE_CODE (arg0) == icode
8190 && simple_operand_p_2 (arg1)
8191 /* Needed for sequence points to handle trappings, and
8192 side-effects. */
8193 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8195 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8196 arg1);
8197 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8198 tem);
8200 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8201 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8202 else if (TREE_CODE (arg1) == icode
8203 && simple_operand_p_2 (arg0)
8204 /* Needed for sequence points to handle trappings, and
8205 side-effects. */
8206 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8208 tem = fold_build2_loc (loc, ncode, type,
8209 arg0, TREE_OPERAND (arg1, 0));
8210 return fold_build2_loc (loc, icode, type, tem,
8211 TREE_OPERAND (arg1, 1));
8213 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8214 into (A OR B).
8215 For sequence point consistancy, we need to check for trapping,
8216 and side-effects. */
8217 else if (code == icode && simple_operand_p_2 (arg0)
8218 && simple_operand_p_2 (arg1))
8219 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8222 return NULL_TREE;
8225 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8226 by changing CODE to reduce the magnitude of constants involved in
8227 ARG0 of the comparison.
8228 Returns a canonicalized comparison tree if a simplification was
8229 possible, otherwise returns NULL_TREE.
8230 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8231 valid if signed overflow is undefined. */
8233 static tree
8234 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8235 tree arg0, tree arg1,
8236 bool *strict_overflow_p)
8238 enum tree_code code0 = TREE_CODE (arg0);
8239 tree t, cst0 = NULL_TREE;
8240 int sgn0;
8242 /* Match A +- CST code arg1. We can change this only if overflow
8243 is undefined. */
8244 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8245 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8246 /* In principle pointers also have undefined overflow behavior,
8247 but that causes problems elsewhere. */
8248 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8249 && (code0 == MINUS_EXPR
8250 || code0 == PLUS_EXPR)
8251 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8252 return NULL_TREE;
8254 /* Identify the constant in arg0 and its sign. */
8255 cst0 = TREE_OPERAND (arg0, 1);
8256 sgn0 = tree_int_cst_sgn (cst0);
8258 /* Overflowed constants and zero will cause problems. */
8259 if (integer_zerop (cst0)
8260 || TREE_OVERFLOW (cst0))
8261 return NULL_TREE;
8263 /* See if we can reduce the magnitude of the constant in
8264 arg0 by changing the comparison code. */
8265 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8266 if (code == LT_EXPR
8267 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8268 code = LE_EXPR;
8269 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8270 else if (code == GT_EXPR
8271 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8272 code = GE_EXPR;
8273 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8274 else if (code == LE_EXPR
8275 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8276 code = LT_EXPR;
8277 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8278 else if (code == GE_EXPR
8279 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8280 code = GT_EXPR;
8281 else
8282 return NULL_TREE;
8283 *strict_overflow_p = true;
8285 /* Now build the constant reduced in magnitude. But not if that
8286 would produce one outside of its types range. */
8287 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8288 && ((sgn0 == 1
8289 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8290 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8291 || (sgn0 == -1
8292 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8293 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8294 return NULL_TREE;
8296 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8297 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8298 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8299 t = fold_convert (TREE_TYPE (arg1), t);
8301 return fold_build2_loc (loc, code, type, t, arg1);
8304 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8305 overflow further. Try to decrease the magnitude of constants involved
8306 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8307 and put sole constants at the second argument position.
8308 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8310 static tree
8311 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8312 tree arg0, tree arg1)
8314 tree t;
8315 bool strict_overflow_p;
8316 const char * const warnmsg = G_("assuming signed overflow does not occur "
8317 "when reducing constant in comparison");
8319 /* Try canonicalization by simplifying arg0. */
8320 strict_overflow_p = false;
8321 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8322 &strict_overflow_p);
8323 if (t)
8325 if (strict_overflow_p)
8326 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8327 return t;
8330 /* Try canonicalization by simplifying arg1 using the swapped
8331 comparison. */
8332 code = swap_tree_comparison (code);
8333 strict_overflow_p = false;
8334 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8335 &strict_overflow_p);
8336 if (t && strict_overflow_p)
8337 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8338 return t;
8341 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8342 space. This is used to avoid issuing overflow warnings for
8343 expressions like &p->x which can not wrap. */
8345 static bool
8346 pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
8348 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8349 return true;
8351 if (maybe_lt (bitpos, 0))
8352 return true;
8354 poly_wide_int wi_offset;
8355 int precision = TYPE_PRECISION (TREE_TYPE (base));
8356 if (offset == NULL_TREE)
8357 wi_offset = wi::zero (precision);
8358 else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
8359 return true;
8360 else
8361 wi_offset = wi::to_poly_wide (offset);
8363 bool overflow;
8364 poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
8365 precision);
8366 poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8367 if (overflow)
8368 return true;
8370 poly_uint64 total_hwi, size;
8371 if (!total.to_uhwi (&total_hwi)
8372 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
8373 &size)
8374 || known_eq (size, 0U))
8375 return true;
8377 if (known_le (total_hwi, size))
8378 return false;
8380 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8381 array. */
8382 if (TREE_CODE (base) == ADDR_EXPR
8383 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
8384 &size)
8385 && maybe_ne (size, 0U)
8386 && known_le (total_hwi, size))
8387 return false;
8389 return true;
8392 /* Return a positive integer when the symbol DECL is known to have
8393 a nonzero address, zero when it's known not to (e.g., it's a weak
8394 symbol), and a negative integer when the symbol is not yet in the
8395 symbol table and so whether or not its address is zero is unknown.
8396 For function local objects always return positive integer. */
8397 static int
8398 maybe_nonzero_address (tree decl)
8400 if (DECL_P (decl) && decl_in_symtab_p (decl))
8401 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8402 return symbol->nonzero_address ();
8404 /* Function local objects are never NULL. */
8405 if (DECL_P (decl)
8406 && (DECL_CONTEXT (decl)
8407 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8408 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8409 return 1;
8411 return -1;
8414 /* Subroutine of fold_binary. This routine performs all of the
8415 transformations that are common to the equality/inequality
8416 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8417 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8418 fold_binary should call fold_binary. Fold a comparison with
8419 tree code CODE and type TYPE with operands OP0 and OP1. Return
8420 the folded comparison or NULL_TREE. */
8422 static tree
8423 fold_comparison (location_t loc, enum tree_code code, tree type,
8424 tree op0, tree op1)
8426 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8427 tree arg0, arg1, tem;
8429 arg0 = op0;
8430 arg1 = op1;
8432 STRIP_SIGN_NOPS (arg0);
8433 STRIP_SIGN_NOPS (arg1);
8435 /* For comparisons of pointers we can decompose it to a compile time
8436 comparison of the base objects and the offsets into the object.
8437 This requires at least one operand being an ADDR_EXPR or a
8438 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8439 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8440 && (TREE_CODE (arg0) == ADDR_EXPR
8441 || TREE_CODE (arg1) == ADDR_EXPR
8442 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8443 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8445 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8446 poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
8447 machine_mode mode;
8448 int volatilep, reversep, unsignedp;
8449 bool indirect_base0 = false, indirect_base1 = false;
8451 /* Get base and offset for the access. Strip ADDR_EXPR for
8452 get_inner_reference, but put it back by stripping INDIRECT_REF
8453 off the base object if possible. indirect_baseN will be true
8454 if baseN is not an address but refers to the object itself. */
8455 base0 = arg0;
8456 if (TREE_CODE (arg0) == ADDR_EXPR)
8458 base0
8459 = get_inner_reference (TREE_OPERAND (arg0, 0),
8460 &bitsize, &bitpos0, &offset0, &mode,
8461 &unsignedp, &reversep, &volatilep);
8462 if (TREE_CODE (base0) == INDIRECT_REF)
8463 base0 = TREE_OPERAND (base0, 0);
8464 else
8465 indirect_base0 = true;
8467 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8469 base0 = TREE_OPERAND (arg0, 0);
8470 STRIP_SIGN_NOPS (base0);
8471 if (TREE_CODE (base0) == ADDR_EXPR)
8473 base0
8474 = get_inner_reference (TREE_OPERAND (base0, 0),
8475 &bitsize, &bitpos0, &offset0, &mode,
8476 &unsignedp, &reversep, &volatilep);
8477 if (TREE_CODE (base0) == INDIRECT_REF)
8478 base0 = TREE_OPERAND (base0, 0);
8479 else
8480 indirect_base0 = true;
8482 if (offset0 == NULL_TREE || integer_zerop (offset0))
8483 offset0 = TREE_OPERAND (arg0, 1);
8484 else
8485 offset0 = size_binop (PLUS_EXPR, offset0,
8486 TREE_OPERAND (arg0, 1));
8487 if (poly_int_tree_p (offset0))
8489 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
8490 TYPE_PRECISION (sizetype));
8491 tem <<= LOG2_BITS_PER_UNIT;
8492 tem += bitpos0;
8493 if (tem.to_shwi (&bitpos0))
8494 offset0 = NULL_TREE;
8498 base1 = arg1;
8499 if (TREE_CODE (arg1) == ADDR_EXPR)
8501 base1
8502 = get_inner_reference (TREE_OPERAND (arg1, 0),
8503 &bitsize, &bitpos1, &offset1, &mode,
8504 &unsignedp, &reversep, &volatilep);
8505 if (TREE_CODE (base1) == INDIRECT_REF)
8506 base1 = TREE_OPERAND (base1, 0);
8507 else
8508 indirect_base1 = true;
8510 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8512 base1 = TREE_OPERAND (arg1, 0);
8513 STRIP_SIGN_NOPS (base1);
8514 if (TREE_CODE (base1) == ADDR_EXPR)
8516 base1
8517 = get_inner_reference (TREE_OPERAND (base1, 0),
8518 &bitsize, &bitpos1, &offset1, &mode,
8519 &unsignedp, &reversep, &volatilep);
8520 if (TREE_CODE (base1) == INDIRECT_REF)
8521 base1 = TREE_OPERAND (base1, 0);
8522 else
8523 indirect_base1 = true;
8525 if (offset1 == NULL_TREE || integer_zerop (offset1))
8526 offset1 = TREE_OPERAND (arg1, 1);
8527 else
8528 offset1 = size_binop (PLUS_EXPR, offset1,
8529 TREE_OPERAND (arg1, 1));
8530 if (poly_int_tree_p (offset1))
8532 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
8533 TYPE_PRECISION (sizetype));
8534 tem <<= LOG2_BITS_PER_UNIT;
8535 tem += bitpos1;
8536 if (tem.to_shwi (&bitpos1))
8537 offset1 = NULL_TREE;
8541 /* If we have equivalent bases we might be able to simplify. */
8542 if (indirect_base0 == indirect_base1
8543 && operand_equal_p (base0, base1,
8544 indirect_base0 ? OEP_ADDRESS_OF : 0))
8546 /* We can fold this expression to a constant if the non-constant
8547 offset parts are equal. */
8548 if ((offset0 == offset1
8549 || (offset0 && offset1
8550 && operand_equal_p (offset0, offset1, 0)))
8551 && (equality_code
8552 || (indirect_base0
8553 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8554 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8556 if (!equality_code
8557 && maybe_ne (bitpos0, bitpos1)
8558 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8559 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8560 fold_overflow_warning (("assuming pointer wraparound does not "
8561 "occur when comparing P +- C1 with "
8562 "P +- C2"),
8563 WARN_STRICT_OVERFLOW_CONDITIONAL);
8565 switch (code)
8567 case EQ_EXPR:
8568 if (known_eq (bitpos0, bitpos1))
8569 return constant_boolean_node (true, type);
8570 if (known_ne (bitpos0, bitpos1))
8571 return constant_boolean_node (false, type);
8572 break;
8573 case NE_EXPR:
8574 if (known_ne (bitpos0, bitpos1))
8575 return constant_boolean_node (true, type);
8576 if (known_eq (bitpos0, bitpos1))
8577 return constant_boolean_node (false, type);
8578 break;
8579 case LT_EXPR:
8580 if (known_lt (bitpos0, bitpos1))
8581 return constant_boolean_node (true, type);
8582 if (known_ge (bitpos0, bitpos1))
8583 return constant_boolean_node (false, type);
8584 break;
8585 case LE_EXPR:
8586 if (known_le (bitpos0, bitpos1))
8587 return constant_boolean_node (true, type);
8588 if (known_gt (bitpos0, bitpos1))
8589 return constant_boolean_node (false, type);
8590 break;
8591 case GE_EXPR:
8592 if (known_ge (bitpos0, bitpos1))
8593 return constant_boolean_node (true, type);
8594 if (known_lt (bitpos0, bitpos1))
8595 return constant_boolean_node (false, type);
8596 break;
8597 case GT_EXPR:
8598 if (known_gt (bitpos0, bitpos1))
8599 return constant_boolean_node (true, type);
8600 if (known_le (bitpos0, bitpos1))
8601 return constant_boolean_node (false, type);
8602 break;
8603 default:;
8606 /* We can simplify the comparison to a comparison of the variable
8607 offset parts if the constant offset parts are equal.
8608 Be careful to use signed sizetype here because otherwise we
8609 mess with array offsets in the wrong way. This is possible
8610 because pointer arithmetic is restricted to retain within an
8611 object and overflow on pointer differences is undefined as of
8612 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8613 else if (known_eq (bitpos0, bitpos1)
8614 && (equality_code
8615 || (indirect_base0
8616 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8617 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8619 /* By converting to signed sizetype we cover middle-end pointer
8620 arithmetic which operates on unsigned pointer types of size
8621 type size and ARRAY_REF offsets which are properly sign or
8622 zero extended from their type in case it is narrower than
8623 sizetype. */
8624 if (offset0 == NULL_TREE)
8625 offset0 = build_int_cst (ssizetype, 0);
8626 else
8627 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8628 if (offset1 == NULL_TREE)
8629 offset1 = build_int_cst (ssizetype, 0);
8630 else
8631 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8633 if (!equality_code
8634 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8635 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8636 fold_overflow_warning (("assuming pointer wraparound does not "
8637 "occur when comparing P +- C1 with "
8638 "P +- C2"),
8639 WARN_STRICT_OVERFLOW_COMPARISON);
8641 return fold_build2_loc (loc, code, type, offset0, offset1);
8644 /* For equal offsets we can simplify to a comparison of the
8645 base addresses. */
8646 else if (known_eq (bitpos0, bitpos1)
8647 && (indirect_base0
8648 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8649 && (indirect_base1
8650 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8651 && ((offset0 == offset1)
8652 || (offset0 && offset1
8653 && operand_equal_p (offset0, offset1, 0))))
8655 if (indirect_base0)
8656 base0 = build_fold_addr_expr_loc (loc, base0);
8657 if (indirect_base1)
8658 base1 = build_fold_addr_expr_loc (loc, base1);
8659 return fold_build2_loc (loc, code, type, base0, base1);
8661 /* Comparison between an ordinary (non-weak) symbol and a null
8662 pointer can be eliminated since such symbols must have a non
8663 null address. In C, relational expressions between pointers
8664 to objects and null pointers are undefined. The results
8665 below follow the C++ rules with the additional property that
8666 every object pointer compares greater than a null pointer.
8668 else if (((DECL_P (base0)
8669 && maybe_nonzero_address (base0) > 0
8670 /* Avoid folding references to struct members at offset 0 to
8671 prevent tests like '&ptr->firstmember == 0' from getting
8672 eliminated. When ptr is null, although the -> expression
8673 is strictly speaking invalid, GCC retains it as a matter
8674 of QoI. See PR c/44555. */
8675 && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
8676 || CONSTANT_CLASS_P (base0))
8677 && indirect_base0
8678 /* The caller guarantees that when one of the arguments is
8679 constant (i.e., null in this case) it is second. */
8680 && integer_zerop (arg1))
8682 switch (code)
8684 case EQ_EXPR:
8685 case LE_EXPR:
8686 case LT_EXPR:
8687 return constant_boolean_node (false, type);
8688 case GE_EXPR:
8689 case GT_EXPR:
8690 case NE_EXPR:
8691 return constant_boolean_node (true, type);
8692 default:
8693 gcc_unreachable ();
8698 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8699 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8700 the resulting offset is smaller in absolute value than the
8701 original one and has the same sign. */
8702 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8703 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8704 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8705 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8706 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8707 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8708 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8709 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8711 tree const1 = TREE_OPERAND (arg0, 1);
8712 tree const2 = TREE_OPERAND (arg1, 1);
8713 tree variable1 = TREE_OPERAND (arg0, 0);
8714 tree variable2 = TREE_OPERAND (arg1, 0);
8715 tree cst;
8716 const char * const warnmsg = G_("assuming signed overflow does not "
8717 "occur when combining constants around "
8718 "a comparison");
8720 /* Put the constant on the side where it doesn't overflow and is
8721 of lower absolute value and of same sign than before. */
8722 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8723 ? MINUS_EXPR : PLUS_EXPR,
8724 const2, const1);
8725 if (!TREE_OVERFLOW (cst)
8726 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8727 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8729 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8730 return fold_build2_loc (loc, code, type,
8731 variable1,
8732 fold_build2_loc (loc, TREE_CODE (arg1),
8733 TREE_TYPE (arg1),
8734 variable2, cst));
8737 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8738 ? MINUS_EXPR : PLUS_EXPR,
8739 const1, const2);
8740 if (!TREE_OVERFLOW (cst)
8741 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8742 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8744 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8745 return fold_build2_loc (loc, code, type,
8746 fold_build2_loc (loc, TREE_CODE (arg0),
8747 TREE_TYPE (arg0),
8748 variable1, cst),
8749 variable2);
8753 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8754 if (tem)
8755 return tem;
8757 /* If we are comparing an expression that just has comparisons
8758 of two integer values, arithmetic expressions of those comparisons,
8759 and constants, we can simplify it. There are only three cases
8760 to check: the two values can either be equal, the first can be
8761 greater, or the second can be greater. Fold the expression for
8762 those three values. Since each value must be 0 or 1, we have
8763 eight possibilities, each of which corresponds to the constant 0
8764 or 1 or one of the six possible comparisons.
8766 This handles common cases like (a > b) == 0 but also handles
8767 expressions like ((x > y) - (y > x)) > 0, which supposedly
8768 occur in macroized code. */
8770 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8772 tree cval1 = 0, cval2 = 0;
8774 if (twoval_comparison_p (arg0, &cval1, &cval2)
8775 /* Don't handle degenerate cases here; they should already
8776 have been handled anyway. */
8777 && cval1 != 0 && cval2 != 0
8778 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8779 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8780 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8781 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8782 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8783 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8784 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8786 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8787 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8789 /* We can't just pass T to eval_subst in case cval1 or cval2
8790 was the same as ARG1. */
8792 tree high_result
8793 = fold_build2_loc (loc, code, type,
8794 eval_subst (loc, arg0, cval1, maxval,
8795 cval2, minval),
8796 arg1);
8797 tree equal_result
8798 = fold_build2_loc (loc, code, type,
8799 eval_subst (loc, arg0, cval1, maxval,
8800 cval2, maxval),
8801 arg1);
8802 tree low_result
8803 = fold_build2_loc (loc, code, type,
8804 eval_subst (loc, arg0, cval1, minval,
8805 cval2, maxval),
8806 arg1);
8808 /* All three of these results should be 0 or 1. Confirm they are.
8809 Then use those values to select the proper code to use. */
8811 if (TREE_CODE (high_result) == INTEGER_CST
8812 && TREE_CODE (equal_result) == INTEGER_CST
8813 && TREE_CODE (low_result) == INTEGER_CST)
8815 /* Make a 3-bit mask with the high-order bit being the
8816 value for `>', the next for '=', and the low for '<'. */
8817 switch ((integer_onep (high_result) * 4)
8818 + (integer_onep (equal_result) * 2)
8819 + integer_onep (low_result))
8821 case 0:
8822 /* Always false. */
8823 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8824 case 1:
8825 code = LT_EXPR;
8826 break;
8827 case 2:
8828 code = EQ_EXPR;
8829 break;
8830 case 3:
8831 code = LE_EXPR;
8832 break;
8833 case 4:
8834 code = GT_EXPR;
8835 break;
8836 case 5:
8837 code = NE_EXPR;
8838 break;
8839 case 6:
8840 code = GE_EXPR;
8841 break;
8842 case 7:
8843 /* Always true. */
8844 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8847 return fold_build2_loc (loc, code, type, cval1, cval2);
8852 return NULL_TREE;
8856 /* Subroutine of fold_binary. Optimize complex multiplications of the
8857 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8858 argument EXPR represents the expression "z" of type TYPE. */
8860 static tree
8861 fold_mult_zconjz (location_t loc, tree type, tree expr)
8863 tree itype = TREE_TYPE (type);
8864 tree rpart, ipart, tem;
8866 if (TREE_CODE (expr) == COMPLEX_EXPR)
8868 rpart = TREE_OPERAND (expr, 0);
8869 ipart = TREE_OPERAND (expr, 1);
8871 else if (TREE_CODE (expr) == COMPLEX_CST)
8873 rpart = TREE_REALPART (expr);
8874 ipart = TREE_IMAGPART (expr);
8876 else
8878 expr = save_expr (expr);
8879 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8880 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8883 rpart = save_expr (rpart);
8884 ipart = save_expr (ipart);
8885 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8886 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8887 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8888 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8889 build_zero_cst (itype));
8893 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8894 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8895 true if successful. */
8897 static bool
8898 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
8900 unsigned HOST_WIDE_INT i, nunits;
8902 if (TREE_CODE (arg) == VECTOR_CST
8903 && VECTOR_CST_NELTS (arg).is_constant (&nunits))
8905 for (i = 0; i < nunits; ++i)
8906 elts[i] = VECTOR_CST_ELT (arg, i);
8908 else if (TREE_CODE (arg) == CONSTRUCTOR)
8910 constructor_elt *elt;
8912 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8913 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8914 return false;
8915 else
8916 elts[i] = elt->value;
8918 else
8919 return false;
8920 for (; i < nelts; i++)
8921 elts[i]
8922 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8923 return true;
8926 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8927 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8928 NULL_TREE otherwise. */
8930 static tree
8931 fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
8933 unsigned int i;
8934 unsigned HOST_WIDE_INT nelts;
8935 bool need_ctor = false;
8937 if (!sel.length ().is_constant (&nelts))
8938 return NULL_TREE;
8939 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), nelts)
8940 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)), nelts)
8941 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)), nelts));
8942 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8943 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8944 return NULL_TREE;
8946 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
8947 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
8948 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
8949 return NULL_TREE;
8951 tree_vector_builder out_elts (type, nelts, 1);
8952 for (i = 0; i < nelts; i++)
8954 HOST_WIDE_INT index;
8955 if (!sel[i].is_constant (&index))
8956 return NULL_TREE;
8957 if (!CONSTANT_CLASS_P (in_elts[index]))
8958 need_ctor = true;
8959 out_elts.quick_push (unshare_expr (in_elts[index]));
8962 if (need_ctor)
8964 vec<constructor_elt, va_gc> *v;
8965 vec_alloc (v, nelts);
8966 for (i = 0; i < nelts; i++)
8967 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
8968 return build_constructor (type, v);
8970 else
8971 return out_elts.build ();
8974 /* Try to fold a pointer difference of type TYPE two address expressions of
8975 array references AREF0 and AREF1 using location LOC. Return a
8976 simplified expression for the difference or NULL_TREE. */
8978 static tree
8979 fold_addr_of_array_ref_difference (location_t loc, tree type,
8980 tree aref0, tree aref1,
8981 bool use_pointer_diff)
8983 tree base0 = TREE_OPERAND (aref0, 0);
8984 tree base1 = TREE_OPERAND (aref1, 0);
8985 tree base_offset = build_int_cst (type, 0);
8987 /* If the bases are array references as well, recurse. If the bases
8988 are pointer indirections compute the difference of the pointers.
8989 If the bases are equal, we are set. */
8990 if ((TREE_CODE (base0) == ARRAY_REF
8991 && TREE_CODE (base1) == ARRAY_REF
8992 && (base_offset
8993 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
8994 use_pointer_diff)))
8995 || (INDIRECT_REF_P (base0)
8996 && INDIRECT_REF_P (base1)
8997 && (base_offset
8998 = use_pointer_diff
8999 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
9000 TREE_OPERAND (base0, 0),
9001 TREE_OPERAND (base1, 0))
9002 : fold_binary_loc (loc, MINUS_EXPR, type,
9003 fold_convert (type,
9004 TREE_OPERAND (base0, 0)),
9005 fold_convert (type,
9006 TREE_OPERAND (base1, 0)))))
9007 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
9009 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9010 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9011 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9012 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
9013 return fold_build2_loc (loc, PLUS_EXPR, type,
9014 base_offset,
9015 fold_build2_loc (loc, MULT_EXPR, type,
9016 diff, esz));
9018 return NULL_TREE;
9021 /* If the real or vector real constant CST of type TYPE has an exact
9022 inverse, return it, else return NULL. */
9024 tree
9025 exact_inverse (tree type, tree cst)
9027 REAL_VALUE_TYPE r;
9028 tree unit_type;
9029 machine_mode mode;
9031 switch (TREE_CODE (cst))
9033 case REAL_CST:
9034 r = TREE_REAL_CST (cst);
9036 if (exact_real_inverse (TYPE_MODE (type), &r))
9037 return build_real (type, r);
9039 return NULL_TREE;
9041 case VECTOR_CST:
9043 unit_type = TREE_TYPE (type);
9044 mode = TYPE_MODE (unit_type);
9046 tree_vector_builder elts;
9047 if (!elts.new_unary_operation (type, cst, false))
9048 return NULL_TREE;
9049 unsigned int count = elts.encoded_nelts ();
9050 for (unsigned int i = 0; i < count; ++i)
9052 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9053 if (!exact_real_inverse (mode, &r))
9054 return NULL_TREE;
9055 elts.quick_push (build_real (unit_type, r));
9058 return elts.build ();
9061 default:
9062 return NULL_TREE;
9066 /* Mask out the tz least significant bits of X of type TYPE where
9067 tz is the number of trailing zeroes in Y. */
9068 static wide_int
9069 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9071 int tz = wi::ctz (y);
9072 if (tz > 0)
9073 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9074 return x;
9077 /* Return true when T is an address and is known to be nonzero.
9078 For floating point we further ensure that T is not denormal.
9079 Similar logic is present in nonzero_address in rtlanal.h.
9081 If the return value is based on the assumption that signed overflow
9082 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9083 change *STRICT_OVERFLOW_P. */
9085 static bool
9086 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9088 tree type = TREE_TYPE (t);
9089 enum tree_code code;
9091 /* Doing something useful for floating point would need more work. */
9092 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9093 return false;
9095 code = TREE_CODE (t);
9096 switch (TREE_CODE_CLASS (code))
9098 case tcc_unary:
9099 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9100 strict_overflow_p);
9101 case tcc_binary:
9102 case tcc_comparison:
9103 return tree_binary_nonzero_warnv_p (code, type,
9104 TREE_OPERAND (t, 0),
9105 TREE_OPERAND (t, 1),
9106 strict_overflow_p);
9107 case tcc_constant:
9108 case tcc_declaration:
9109 case tcc_reference:
9110 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9112 default:
9113 break;
9116 switch (code)
9118 case TRUTH_NOT_EXPR:
9119 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9120 strict_overflow_p);
9122 case TRUTH_AND_EXPR:
9123 case TRUTH_OR_EXPR:
9124 case TRUTH_XOR_EXPR:
9125 return tree_binary_nonzero_warnv_p (code, type,
9126 TREE_OPERAND (t, 0),
9127 TREE_OPERAND (t, 1),
9128 strict_overflow_p);
9130 case COND_EXPR:
9131 case CONSTRUCTOR:
9132 case OBJ_TYPE_REF:
9133 case ASSERT_EXPR:
9134 case ADDR_EXPR:
9135 case WITH_SIZE_EXPR:
9136 case SSA_NAME:
9137 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9139 case COMPOUND_EXPR:
9140 case MODIFY_EXPR:
9141 case BIND_EXPR:
9142 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9143 strict_overflow_p);
9145 case SAVE_EXPR:
9146 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9147 strict_overflow_p);
9149 case CALL_EXPR:
9151 tree fndecl = get_callee_fndecl (t);
9152 if (!fndecl) return false;
9153 if (flag_delete_null_pointer_checks && !flag_check_new
9154 && DECL_IS_OPERATOR_NEW (fndecl)
9155 && !TREE_NOTHROW (fndecl))
9156 return true;
9157 if (flag_delete_null_pointer_checks
9158 && lookup_attribute ("returns_nonnull",
9159 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9160 return true;
9161 return alloca_call_p (t);
9164 default:
9165 break;
9167 return false;
9170 /* Return true when T is an address and is known to be nonzero.
9171 Handle warnings about undefined signed overflow. */
9173 bool
9174 tree_expr_nonzero_p (tree t)
9176 bool ret, strict_overflow_p;
9178 strict_overflow_p = false;
9179 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9180 if (strict_overflow_p)
9181 fold_overflow_warning (("assuming signed overflow does not occur when "
9182 "determining that expression is always "
9183 "non-zero"),
9184 WARN_STRICT_OVERFLOW_MISC);
9185 return ret;
9188 /* Return true if T is known not to be equal to an integer W. */
9190 bool
9191 expr_not_equal_to (tree t, const wide_int &w)
9193 wide_int min, max, nz;
9194 value_range_type rtype;
9195 switch (TREE_CODE (t))
9197 case INTEGER_CST:
9198 return wi::to_wide (t) != w;
9200 case SSA_NAME:
9201 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9202 return false;
9203 rtype = get_range_info (t, &min, &max);
9204 if (rtype == VR_RANGE)
9206 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9207 return true;
9208 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9209 return true;
9211 else if (rtype == VR_ANTI_RANGE
9212 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9213 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9214 return true;
9215 /* If T has some known zero bits and W has any of those bits set,
9216 then T is known not to be equal to W. */
9217 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9218 TYPE_PRECISION (TREE_TYPE (t))), 0))
9219 return true;
9220 return false;
9222 default:
9223 return false;
9227 /* Fold a binary expression of code CODE and type TYPE with operands
9228 OP0 and OP1. LOC is the location of the resulting expression.
9229 Return the folded expression if folding is successful. Otherwise,
9230 return NULL_TREE. */
9232 tree
9233 fold_binary_loc (location_t loc, enum tree_code code, tree type,
9234 tree op0, tree op1)
9236 enum tree_code_class kind = TREE_CODE_CLASS (code);
9237 tree arg0, arg1, tem;
9238 tree t1 = NULL_TREE;
9239 bool strict_overflow_p;
9240 unsigned int prec;
9242 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9243 && TREE_CODE_LENGTH (code) == 2
9244 && op0 != NULL_TREE
9245 && op1 != NULL_TREE);
9247 arg0 = op0;
9248 arg1 = op1;
9250 /* Strip any conversions that don't change the mode. This is
9251 safe for every expression, except for a comparison expression
9252 because its signedness is derived from its operands. So, in
9253 the latter case, only strip conversions that don't change the
9254 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9255 preserved.
9257 Note that this is done as an internal manipulation within the
9258 constant folder, in order to find the simplest representation
9259 of the arguments so that their form can be studied. In any
9260 cases, the appropriate type conversions should be put back in
9261 the tree that will get out of the constant folder. */
9263 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9265 STRIP_SIGN_NOPS (arg0);
9266 STRIP_SIGN_NOPS (arg1);
9268 else
9270 STRIP_NOPS (arg0);
9271 STRIP_NOPS (arg1);
9274 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9275 constant but we can't do arithmetic on them. */
9276 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9278 tem = const_binop (code, type, arg0, arg1);
9279 if (tem != NULL_TREE)
9281 if (TREE_TYPE (tem) != type)
9282 tem = fold_convert_loc (loc, type, tem);
9283 return tem;
9287 /* If this is a commutative operation, and ARG0 is a constant, move it
9288 to ARG1 to reduce the number of tests below. */
9289 if (commutative_tree_code (code)
9290 && tree_swap_operands_p (arg0, arg1))
9291 return fold_build2_loc (loc, code, type, op1, op0);
9293 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9294 to ARG1 to reduce the number of tests below. */
9295 if (kind == tcc_comparison
9296 && tree_swap_operands_p (arg0, arg1))
9297 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9299 tem = generic_simplify (loc, code, type, op0, op1);
9300 if (tem)
9301 return tem;
9303 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9305 First check for cases where an arithmetic operation is applied to a
9306 compound, conditional, or comparison operation. Push the arithmetic
9307 operation inside the compound or conditional to see if any folding
9308 can then be done. Convert comparison to conditional for this purpose.
9309 The also optimizes non-constant cases that used to be done in
9310 expand_expr.
9312 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9313 one of the operands is a comparison and the other is a comparison, a
9314 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9315 code below would make the expression more complex. Change it to a
9316 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9317 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9319 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9320 || code == EQ_EXPR || code == NE_EXPR)
9321 && !VECTOR_TYPE_P (TREE_TYPE (arg0))
9322 && ((truth_value_p (TREE_CODE (arg0))
9323 && (truth_value_p (TREE_CODE (arg1))
9324 || (TREE_CODE (arg1) == BIT_AND_EXPR
9325 && integer_onep (TREE_OPERAND (arg1, 1)))))
9326 || (truth_value_p (TREE_CODE (arg1))
9327 && (truth_value_p (TREE_CODE (arg0))
9328 || (TREE_CODE (arg0) == BIT_AND_EXPR
9329 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9331 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9332 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9333 : TRUTH_XOR_EXPR,
9334 boolean_type_node,
9335 fold_convert_loc (loc, boolean_type_node, arg0),
9336 fold_convert_loc (loc, boolean_type_node, arg1));
9338 if (code == EQ_EXPR)
9339 tem = invert_truthvalue_loc (loc, tem);
9341 return fold_convert_loc (loc, type, tem);
9344 if (TREE_CODE_CLASS (code) == tcc_binary
9345 || TREE_CODE_CLASS (code) == tcc_comparison)
9347 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9349 tem = fold_build2_loc (loc, code, type,
9350 fold_convert_loc (loc, TREE_TYPE (op0),
9351 TREE_OPERAND (arg0, 1)), op1);
9352 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9353 tem);
9355 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9357 tem = fold_build2_loc (loc, code, type, op0,
9358 fold_convert_loc (loc, TREE_TYPE (op1),
9359 TREE_OPERAND (arg1, 1)));
9360 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9361 tem);
9364 if (TREE_CODE (arg0) == COND_EXPR
9365 || TREE_CODE (arg0) == VEC_COND_EXPR
9366 || COMPARISON_CLASS_P (arg0))
9368 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9369 arg0, arg1,
9370 /*cond_first_p=*/1);
9371 if (tem != NULL_TREE)
9372 return tem;
9375 if (TREE_CODE (arg1) == COND_EXPR
9376 || TREE_CODE (arg1) == VEC_COND_EXPR
9377 || COMPARISON_CLASS_P (arg1))
9379 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9380 arg1, arg0,
9381 /*cond_first_p=*/0);
9382 if (tem != NULL_TREE)
9383 return tem;
9387 switch (code)
9389 case MEM_REF:
9390 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9391 if (TREE_CODE (arg0) == ADDR_EXPR
9392 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9394 tree iref = TREE_OPERAND (arg0, 0);
9395 return fold_build2 (MEM_REF, type,
9396 TREE_OPERAND (iref, 0),
9397 int_const_binop (PLUS_EXPR, arg1,
9398 TREE_OPERAND (iref, 1)));
9401 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9402 if (TREE_CODE (arg0) == ADDR_EXPR
9403 && handled_component_p (TREE_OPERAND (arg0, 0)))
9405 tree base;
9406 poly_int64 coffset;
9407 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9408 &coffset);
9409 if (!base)
9410 return NULL_TREE;
9411 return fold_build2 (MEM_REF, type,
9412 build_fold_addr_expr (base),
9413 int_const_binop (PLUS_EXPR, arg1,
9414 size_int (coffset)));
9417 return NULL_TREE;
9419 case POINTER_PLUS_EXPR:
9420 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9421 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9422 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9423 return fold_convert_loc (loc, type,
9424 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9425 fold_convert_loc (loc, sizetype,
9426 arg1),
9427 fold_convert_loc (loc, sizetype,
9428 arg0)));
9430 return NULL_TREE;
9432 case PLUS_EXPR:
9433 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9435 /* X + (X / CST) * -CST is X % CST. */
9436 if (TREE_CODE (arg1) == MULT_EXPR
9437 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9438 && operand_equal_p (arg0,
9439 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9441 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9442 tree cst1 = TREE_OPERAND (arg1, 1);
9443 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9444 cst1, cst0);
9445 if (sum && integer_zerop (sum))
9446 return fold_convert_loc (loc, type,
9447 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9448 TREE_TYPE (arg0), arg0,
9449 cst0));
9453 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9454 one. Make sure the type is not saturating and has the signedness of
9455 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9456 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9457 if ((TREE_CODE (arg0) == MULT_EXPR
9458 || TREE_CODE (arg1) == MULT_EXPR)
9459 && !TYPE_SATURATING (type)
9460 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9461 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9462 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9464 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9465 if (tem)
9466 return tem;
9469 if (! FLOAT_TYPE_P (type))
9471 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9472 (plus (plus (mult) (mult)) (foo)) so that we can
9473 take advantage of the factoring cases below. */
9474 if (ANY_INTEGRAL_TYPE_P (type)
9475 && TYPE_OVERFLOW_WRAPS (type)
9476 && (((TREE_CODE (arg0) == PLUS_EXPR
9477 || TREE_CODE (arg0) == MINUS_EXPR)
9478 && TREE_CODE (arg1) == MULT_EXPR)
9479 || ((TREE_CODE (arg1) == PLUS_EXPR
9480 || TREE_CODE (arg1) == MINUS_EXPR)
9481 && TREE_CODE (arg0) == MULT_EXPR)))
9483 tree parg0, parg1, parg, marg;
9484 enum tree_code pcode;
9486 if (TREE_CODE (arg1) == MULT_EXPR)
9487 parg = arg0, marg = arg1;
9488 else
9489 parg = arg1, marg = arg0;
9490 pcode = TREE_CODE (parg);
9491 parg0 = TREE_OPERAND (parg, 0);
9492 parg1 = TREE_OPERAND (parg, 1);
9493 STRIP_NOPS (parg0);
9494 STRIP_NOPS (parg1);
9496 if (TREE_CODE (parg0) == MULT_EXPR
9497 && TREE_CODE (parg1) != MULT_EXPR)
9498 return fold_build2_loc (loc, pcode, type,
9499 fold_build2_loc (loc, PLUS_EXPR, type,
9500 fold_convert_loc (loc, type,
9501 parg0),
9502 fold_convert_loc (loc, type,
9503 marg)),
9504 fold_convert_loc (loc, type, parg1));
9505 if (TREE_CODE (parg0) != MULT_EXPR
9506 && TREE_CODE (parg1) == MULT_EXPR)
9507 return
9508 fold_build2_loc (loc, PLUS_EXPR, type,
9509 fold_convert_loc (loc, type, parg0),
9510 fold_build2_loc (loc, pcode, type,
9511 fold_convert_loc (loc, type, marg),
9512 fold_convert_loc (loc, type,
9513 parg1)));
9516 else
9518 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9519 to __complex__ ( x, y ). This is not the same for SNaNs or
9520 if signed zeros are involved. */
9521 if (!HONOR_SNANS (element_mode (arg0))
9522 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9523 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9525 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9526 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9527 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9528 bool arg0rz = false, arg0iz = false;
9529 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9530 || (arg0i && (arg0iz = real_zerop (arg0i))))
9532 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9533 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9534 if (arg0rz && arg1i && real_zerop (arg1i))
9536 tree rp = arg1r ? arg1r
9537 : build1 (REALPART_EXPR, rtype, arg1);
9538 tree ip = arg0i ? arg0i
9539 : build1 (IMAGPART_EXPR, rtype, arg0);
9540 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9542 else if (arg0iz && arg1r && real_zerop (arg1r))
9544 tree rp = arg0r ? arg0r
9545 : build1 (REALPART_EXPR, rtype, arg0);
9546 tree ip = arg1i ? arg1i
9547 : build1 (IMAGPART_EXPR, rtype, arg1);
9548 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9553 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9554 We associate floats only if the user has specified
9555 -fassociative-math. */
9556 if (flag_associative_math
9557 && TREE_CODE (arg1) == PLUS_EXPR
9558 && TREE_CODE (arg0) != MULT_EXPR)
9560 tree tree10 = TREE_OPERAND (arg1, 0);
9561 tree tree11 = TREE_OPERAND (arg1, 1);
9562 if (TREE_CODE (tree11) == MULT_EXPR
9563 && TREE_CODE (tree10) == MULT_EXPR)
9565 tree tree0;
9566 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9567 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9570 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9571 We associate floats only if the user has specified
9572 -fassociative-math. */
9573 if (flag_associative_math
9574 && TREE_CODE (arg0) == PLUS_EXPR
9575 && TREE_CODE (arg1) != MULT_EXPR)
9577 tree tree00 = TREE_OPERAND (arg0, 0);
9578 tree tree01 = TREE_OPERAND (arg0, 1);
9579 if (TREE_CODE (tree01) == MULT_EXPR
9580 && TREE_CODE (tree00) == MULT_EXPR)
9582 tree tree0;
9583 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9584 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9589 bit_rotate:
9590 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9591 is a rotate of A by C1 bits. */
9592 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9593 is a rotate of A by B bits.
9594 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
9595 though in this case CODE must be | and not + or ^, otherwise
9596 it doesn't return A when B is 0. */
9598 enum tree_code code0, code1;
9599 tree rtype;
9600 code0 = TREE_CODE (arg0);
9601 code1 = TREE_CODE (arg1);
9602 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9603 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9604 && operand_equal_p (TREE_OPERAND (arg0, 0),
9605 TREE_OPERAND (arg1, 0), 0)
9606 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9607 TYPE_UNSIGNED (rtype))
9608 /* Only create rotates in complete modes. Other cases are not
9609 expanded properly. */
9610 && (element_precision (rtype)
9611 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9613 tree tree01, tree11;
9614 tree orig_tree01, orig_tree11;
9615 enum tree_code code01, code11;
9617 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
9618 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
9619 STRIP_NOPS (tree01);
9620 STRIP_NOPS (tree11);
9621 code01 = TREE_CODE (tree01);
9622 code11 = TREE_CODE (tree11);
9623 if (code11 != MINUS_EXPR
9624 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
9626 std::swap (code0, code1);
9627 std::swap (code01, code11);
9628 std::swap (tree01, tree11);
9629 std::swap (orig_tree01, orig_tree11);
9631 if (code01 == INTEGER_CST
9632 && code11 == INTEGER_CST
9633 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9634 == element_precision (rtype)))
9636 tem = build2_loc (loc, LROTATE_EXPR,
9637 rtype, TREE_OPERAND (arg0, 0),
9638 code0 == LSHIFT_EXPR
9639 ? orig_tree01 : orig_tree11);
9640 return fold_convert_loc (loc, type, tem);
9642 else if (code11 == MINUS_EXPR)
9644 tree tree110, tree111;
9645 tree110 = TREE_OPERAND (tree11, 0);
9646 tree111 = TREE_OPERAND (tree11, 1);
9647 STRIP_NOPS (tree110);
9648 STRIP_NOPS (tree111);
9649 if (TREE_CODE (tree110) == INTEGER_CST
9650 && compare_tree_int (tree110,
9651 element_precision (rtype)) == 0
9652 && operand_equal_p (tree01, tree111, 0))
9654 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9655 ? LROTATE_EXPR : RROTATE_EXPR),
9656 rtype, TREE_OPERAND (arg0, 0),
9657 orig_tree01);
9658 return fold_convert_loc (loc, type, tem);
9661 else if (code == BIT_IOR_EXPR
9662 && code11 == BIT_AND_EXPR
9663 && pow2p_hwi (element_precision (rtype)))
9665 tree tree110, tree111;
9666 tree110 = TREE_OPERAND (tree11, 0);
9667 tree111 = TREE_OPERAND (tree11, 1);
9668 STRIP_NOPS (tree110);
9669 STRIP_NOPS (tree111);
9670 if (TREE_CODE (tree110) == NEGATE_EXPR
9671 && TREE_CODE (tree111) == INTEGER_CST
9672 && compare_tree_int (tree111,
9673 element_precision (rtype) - 1) == 0
9674 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
9676 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9677 ? LROTATE_EXPR : RROTATE_EXPR),
9678 rtype, TREE_OPERAND (arg0, 0),
9679 orig_tree01);
9680 return fold_convert_loc (loc, type, tem);
9686 associate:
9687 /* In most languages, can't associate operations on floats through
9688 parentheses. Rather than remember where the parentheses were, we
9689 don't associate floats at all, unless the user has specified
9690 -fassociative-math.
9691 And, we need to make sure type is not saturating. */
9693 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9694 && !TYPE_SATURATING (type))
9696 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9697 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9698 tree atype = type;
9699 bool ok = true;
9701 /* Split both trees into variables, constants, and literals. Then
9702 associate each group together, the constants with literals,
9703 then the result with variables. This increases the chances of
9704 literals being recombined later and of generating relocatable
9705 expressions for the sum of a constant and literal. */
9706 var0 = split_tree (arg0, type, code,
9707 &minus_var0, &con0, &minus_con0,
9708 &lit0, &minus_lit0, 0);
9709 var1 = split_tree (arg1, type, code,
9710 &minus_var1, &con1, &minus_con1,
9711 &lit1, &minus_lit1, code == MINUS_EXPR);
9713 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9714 if (code == MINUS_EXPR)
9715 code = PLUS_EXPR;
9717 /* With undefined overflow prefer doing association in a type
9718 which wraps on overflow, if that is one of the operand types. */
9719 if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
9720 && !TYPE_OVERFLOW_WRAPS (type))
9722 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9723 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9724 atype = TREE_TYPE (arg0);
9725 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9726 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9727 atype = TREE_TYPE (arg1);
9728 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9731 /* With undefined overflow we can only associate constants with one
9732 variable, and constants whose association doesn't overflow. */
9733 if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
9734 && !TYPE_OVERFLOW_WRAPS (atype))
9736 if ((var0 && var1) || (minus_var0 && minus_var1))
9738 /* ??? If split_tree would handle NEGATE_EXPR we could
9739 simply reject these cases and the allowed cases would
9740 be the var0/minus_var1 ones. */
9741 tree tmp0 = var0 ? var0 : minus_var0;
9742 tree tmp1 = var1 ? var1 : minus_var1;
9743 bool one_neg = false;
9745 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9747 tmp0 = TREE_OPERAND (tmp0, 0);
9748 one_neg = !one_neg;
9750 if (CONVERT_EXPR_P (tmp0)
9751 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9752 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9753 <= TYPE_PRECISION (atype)))
9754 tmp0 = TREE_OPERAND (tmp0, 0);
9755 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9757 tmp1 = TREE_OPERAND (tmp1, 0);
9758 one_neg = !one_neg;
9760 if (CONVERT_EXPR_P (tmp1)
9761 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9762 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9763 <= TYPE_PRECISION (atype)))
9764 tmp1 = TREE_OPERAND (tmp1, 0);
9765 /* The only case we can still associate with two variables
9766 is if they cancel out. */
9767 if (!one_neg
9768 || !operand_equal_p (tmp0, tmp1, 0))
9769 ok = false;
9771 else if ((var0 && minus_var1
9772 && ! operand_equal_p (var0, minus_var1, 0))
9773 || (minus_var0 && var1
9774 && ! operand_equal_p (minus_var0, var1, 0)))
9775 ok = false;
9778 /* Only do something if we found more than two objects. Otherwise,
9779 nothing has changed and we risk infinite recursion. */
9780 if (ok
9781 && ((var0 != 0) + (var1 != 0)
9782 + (minus_var0 != 0) + (minus_var1 != 0)
9783 + (con0 != 0) + (con1 != 0)
9784 + (minus_con0 != 0) + (minus_con1 != 0)
9785 + (lit0 != 0) + (lit1 != 0)
9786 + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
9788 var0 = associate_trees (loc, var0, var1, code, atype);
9789 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9790 code, atype);
9791 con0 = associate_trees (loc, con0, con1, code, atype);
9792 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9793 code, atype);
9794 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9795 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9796 code, atype);
9798 if (minus_var0 && var0)
9800 var0 = associate_trees (loc, var0, minus_var0,
9801 MINUS_EXPR, atype);
9802 minus_var0 = 0;
9804 if (minus_con0 && con0)
9806 con0 = associate_trees (loc, con0, minus_con0,
9807 MINUS_EXPR, atype);
9808 minus_con0 = 0;
9811 /* Preserve the MINUS_EXPR if the negative part of the literal is
9812 greater than the positive part. Otherwise, the multiplicative
9813 folding code (i.e extract_muldiv) may be fooled in case
9814 unsigned constants are subtracted, like in the following
9815 example: ((X*2 + 4) - 8U)/2. */
9816 if (minus_lit0 && lit0)
9818 if (TREE_CODE (lit0) == INTEGER_CST
9819 && TREE_CODE (minus_lit0) == INTEGER_CST
9820 && tree_int_cst_lt (lit0, minus_lit0)
9821 /* But avoid ending up with only negated parts. */
9822 && (var0 || con0))
9824 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9825 MINUS_EXPR, atype);
9826 lit0 = 0;
9828 else
9830 lit0 = associate_trees (loc, lit0, minus_lit0,
9831 MINUS_EXPR, atype);
9832 minus_lit0 = 0;
9836 /* Don't introduce overflows through reassociation. */
9837 if ((lit0 && TREE_OVERFLOW_P (lit0))
9838 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9839 return NULL_TREE;
9841 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9842 con0 = associate_trees (loc, con0, lit0, code, atype);
9843 lit0 = 0;
9844 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9845 code, atype);
9846 minus_lit0 = 0;
9848 /* Eliminate minus_con0. */
9849 if (minus_con0)
9851 if (con0)
9852 con0 = associate_trees (loc, con0, minus_con0,
9853 MINUS_EXPR, atype);
9854 else if (var0)
9855 var0 = associate_trees (loc, var0, minus_con0,
9856 MINUS_EXPR, atype);
9857 else
9858 gcc_unreachable ();
9859 minus_con0 = 0;
9862 /* Eliminate minus_var0. */
9863 if (minus_var0)
9865 if (con0)
9866 con0 = associate_trees (loc, con0, minus_var0,
9867 MINUS_EXPR, atype);
9868 else
9869 gcc_unreachable ();
9870 minus_var0 = 0;
9873 return
9874 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9875 code, atype));
9879 return NULL_TREE;
9881 case POINTER_DIFF_EXPR:
9882 case MINUS_EXPR:
9883 /* Fold &a[i] - &a[j] to i-j. */
9884 if (TREE_CODE (arg0) == ADDR_EXPR
9885 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9886 && TREE_CODE (arg1) == ADDR_EXPR
9887 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9889 tree tem = fold_addr_of_array_ref_difference (loc, type,
9890 TREE_OPERAND (arg0, 0),
9891 TREE_OPERAND (arg1, 0),
9892 code
9893 == POINTER_DIFF_EXPR);
9894 if (tem)
9895 return tem;
9898 /* Further transformations are not for pointers. */
9899 if (code == POINTER_DIFF_EXPR)
9900 return NULL_TREE;
9902 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9903 if (TREE_CODE (arg0) == NEGATE_EXPR
9904 && negate_expr_p (op1)
9905 /* If arg0 is e.g. unsigned int and type is int, then this could
9906 introduce UB, because if A is INT_MIN at runtime, the original
9907 expression can be well defined while the latter is not.
9908 See PR83269. */
9909 && !(ANY_INTEGRAL_TYPE_P (type)
9910 && TYPE_OVERFLOW_UNDEFINED (type)
9911 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9912 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
9913 return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
9914 fold_convert_loc (loc, type,
9915 TREE_OPERAND (arg0, 0)));
9917 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9918 __complex__ ( x, -y ). This is not the same for SNaNs or if
9919 signed zeros are involved. */
9920 if (!HONOR_SNANS (element_mode (arg0))
9921 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9922 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9924 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9925 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9926 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9927 bool arg0rz = false, arg0iz = false;
9928 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9929 || (arg0i && (arg0iz = real_zerop (arg0i))))
9931 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9932 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9933 if (arg0rz && arg1i && real_zerop (arg1i))
9935 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9936 arg1r ? arg1r
9937 : build1 (REALPART_EXPR, rtype, arg1));
9938 tree ip = arg0i ? arg0i
9939 : build1 (IMAGPART_EXPR, rtype, arg0);
9940 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9942 else if (arg0iz && arg1r && real_zerop (arg1r))
9944 tree rp = arg0r ? arg0r
9945 : build1 (REALPART_EXPR, rtype, arg0);
9946 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9947 arg1i ? arg1i
9948 : build1 (IMAGPART_EXPR, rtype, arg1));
9949 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9954 /* A - B -> A + (-B) if B is easily negatable. */
9955 if (negate_expr_p (op1)
9956 && ! TYPE_OVERFLOW_SANITIZED (type)
9957 && ((FLOAT_TYPE_P (type)
9958 /* Avoid this transformation if B is a positive REAL_CST. */
9959 && (TREE_CODE (op1) != REAL_CST
9960 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9961 || INTEGRAL_TYPE_P (type)))
9962 return fold_build2_loc (loc, PLUS_EXPR, type,
9963 fold_convert_loc (loc, type, arg0),
9964 negate_expr (op1));
9966 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9967 one. Make sure the type is not saturating and has the signedness of
9968 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9969 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9970 if ((TREE_CODE (arg0) == MULT_EXPR
9971 || TREE_CODE (arg1) == MULT_EXPR)
9972 && !TYPE_SATURATING (type)
9973 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9974 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9975 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9977 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9978 if (tem)
9979 return tem;
9982 goto associate;
9984 case MULT_EXPR:
9985 if (! FLOAT_TYPE_P (type))
9987 /* Transform x * -C into -x * C if x is easily negatable. */
9988 if (TREE_CODE (op1) == INTEGER_CST
9989 && tree_int_cst_sgn (op1) == -1
9990 && negate_expr_p (op0)
9991 && negate_expr_p (op1)
9992 && (tem = negate_expr (op1)) != op1
9993 && ! TREE_OVERFLOW (tem))
9994 return fold_build2_loc (loc, MULT_EXPR, type,
9995 fold_convert_loc (loc, type,
9996 negate_expr (op0)), tem);
9998 strict_overflow_p = false;
9999 if (TREE_CODE (arg1) == INTEGER_CST
10000 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10001 &strict_overflow_p)) != 0)
10003 if (strict_overflow_p)
10004 fold_overflow_warning (("assuming signed overflow does not "
10005 "occur when simplifying "
10006 "multiplication"),
10007 WARN_STRICT_OVERFLOW_MISC);
10008 return fold_convert_loc (loc, type, tem);
10011 /* Optimize z * conj(z) for integer complex numbers. */
10012 if (TREE_CODE (arg0) == CONJ_EXPR
10013 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10014 return fold_mult_zconjz (loc, type, arg1);
10015 if (TREE_CODE (arg1) == CONJ_EXPR
10016 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10017 return fold_mult_zconjz (loc, type, arg0);
10019 else
10021 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10022 This is not the same for NaNs or if signed zeros are
10023 involved. */
10024 if (!HONOR_NANS (arg0)
10025 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10026 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10027 && TREE_CODE (arg1) == COMPLEX_CST
10028 && real_zerop (TREE_REALPART (arg1)))
10030 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10031 if (real_onep (TREE_IMAGPART (arg1)))
10032 return
10033 fold_build2_loc (loc, COMPLEX_EXPR, type,
10034 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10035 rtype, arg0)),
10036 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10037 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10038 return
10039 fold_build2_loc (loc, COMPLEX_EXPR, type,
10040 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10041 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10042 rtype, arg0)));
10045 /* Optimize z * conj(z) for floating point complex numbers.
10046 Guarded by flag_unsafe_math_optimizations as non-finite
10047 imaginary components don't produce scalar results. */
10048 if (flag_unsafe_math_optimizations
10049 && TREE_CODE (arg0) == CONJ_EXPR
10050 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10051 return fold_mult_zconjz (loc, type, arg1);
10052 if (flag_unsafe_math_optimizations
10053 && TREE_CODE (arg1) == CONJ_EXPR
10054 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10055 return fold_mult_zconjz (loc, type, arg0);
10057 goto associate;
10059 case BIT_IOR_EXPR:
10060 /* Canonicalize (X & C1) | C2. */
10061 if (TREE_CODE (arg0) == BIT_AND_EXPR
10062 && TREE_CODE (arg1) == INTEGER_CST
10063 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10065 int width = TYPE_PRECISION (type), w;
10066 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
10067 wide_int c2 = wi::to_wide (arg1);
10069 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10070 if ((c1 & c2) == c1)
10071 return omit_one_operand_loc (loc, type, arg1,
10072 TREE_OPERAND (arg0, 0));
10074 wide_int msk = wi::mask (width, false,
10075 TYPE_PRECISION (TREE_TYPE (arg1)));
10077 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10078 if (wi::bit_and_not (msk, c1 | c2) == 0)
10080 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10081 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10084 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10085 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10086 mode which allows further optimizations. */
10087 c1 &= msk;
10088 c2 &= msk;
10089 wide_int c3 = wi::bit_and_not (c1, c2);
10090 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10092 wide_int mask = wi::mask (w, false,
10093 TYPE_PRECISION (type));
10094 if (((c1 | c2) & mask) == mask
10095 && wi::bit_and_not (c1, mask) == 0)
10097 c3 = mask;
10098 break;
10102 if (c3 != c1)
10104 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10105 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
10106 wide_int_to_tree (type, c3));
10107 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10111 /* See if this can be simplified into a rotate first. If that
10112 is unsuccessful continue in the association code. */
10113 goto bit_rotate;
10115 case BIT_XOR_EXPR:
10116 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10117 if (TREE_CODE (arg0) == BIT_AND_EXPR
10118 && INTEGRAL_TYPE_P (type)
10119 && integer_onep (TREE_OPERAND (arg0, 1))
10120 && integer_onep (arg1))
10121 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10122 build_zero_cst (TREE_TYPE (arg0)));
10124 /* See if this can be simplified into a rotate first. If that
10125 is unsuccessful continue in the association code. */
10126 goto bit_rotate;
10128 case BIT_AND_EXPR:
10129 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10130 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10131 && INTEGRAL_TYPE_P (type)
10132 && integer_onep (TREE_OPERAND (arg0, 1))
10133 && integer_onep (arg1))
10135 tree tem2;
10136 tem = TREE_OPERAND (arg0, 0);
10137 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10138 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10139 tem, tem2);
10140 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10141 build_zero_cst (TREE_TYPE (tem)));
10143 /* Fold ~X & 1 as (X & 1) == 0. */
10144 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10145 && INTEGRAL_TYPE_P (type)
10146 && integer_onep (arg1))
10148 tree tem2;
10149 tem = TREE_OPERAND (arg0, 0);
10150 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10151 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10152 tem, tem2);
10153 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10154 build_zero_cst (TREE_TYPE (tem)));
10156 /* Fold !X & 1 as X == 0. */
10157 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10158 && integer_onep (arg1))
10160 tem = TREE_OPERAND (arg0, 0);
10161 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10162 build_zero_cst (TREE_TYPE (tem)));
10165 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10166 multiple of 1 << CST. */
10167 if (TREE_CODE (arg1) == INTEGER_CST)
10169 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10170 wide_int ncst1 = -cst1;
10171 if ((cst1 & ncst1) == ncst1
10172 && multiple_of_p (type, arg0,
10173 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10174 return fold_convert_loc (loc, type, arg0);
10177 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10178 bits from CST2. */
10179 if (TREE_CODE (arg1) == INTEGER_CST
10180 && TREE_CODE (arg0) == MULT_EXPR
10181 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10183 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
10184 wide_int masked
10185 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
10187 if (masked == 0)
10188 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10189 arg0, arg1);
10190 else if (masked != warg1)
10192 /* Avoid the transform if arg1 is a mask of some
10193 mode which allows further optimizations. */
10194 int pop = wi::popcount (warg1);
10195 if (!(pop >= BITS_PER_UNIT
10196 && pow2p_hwi (pop)
10197 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10198 return fold_build2_loc (loc, code, type, op0,
10199 wide_int_to_tree (type, masked));
10203 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10204 ((A & N) + B) & M -> (A + B) & M
10205 Similarly if (N & M) == 0,
10206 ((A | N) + B) & M -> (A + B) & M
10207 and for - instead of + (or unary - instead of +)
10208 and/or ^ instead of |.
10209 If B is constant and (B & M) == 0, fold into A & M. */
10210 if (TREE_CODE (arg1) == INTEGER_CST)
10212 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10213 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10214 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10215 && (TREE_CODE (arg0) == PLUS_EXPR
10216 || TREE_CODE (arg0) == MINUS_EXPR
10217 || TREE_CODE (arg0) == NEGATE_EXPR)
10218 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10219 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10221 tree pmop[2];
10222 int which = 0;
10223 wide_int cst0;
10225 /* Now we know that arg0 is (C + D) or (C - D) or
10226 -C and arg1 (M) is == (1LL << cst) - 1.
10227 Store C into PMOP[0] and D into PMOP[1]. */
10228 pmop[0] = TREE_OPERAND (arg0, 0);
10229 pmop[1] = NULL;
10230 if (TREE_CODE (arg0) != NEGATE_EXPR)
10232 pmop[1] = TREE_OPERAND (arg0, 1);
10233 which = 1;
10236 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10237 which = -1;
10239 for (; which >= 0; which--)
10240 switch (TREE_CODE (pmop[which]))
10242 case BIT_AND_EXPR:
10243 case BIT_IOR_EXPR:
10244 case BIT_XOR_EXPR:
10245 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10246 != INTEGER_CST)
10247 break;
10248 cst0 = wi::to_wide (TREE_OPERAND (pmop[which], 1)) & cst1;
10249 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10251 if (cst0 != cst1)
10252 break;
10254 else if (cst0 != 0)
10255 break;
10256 /* If C or D is of the form (A & N) where
10257 (N & M) == M, or of the form (A | N) or
10258 (A ^ N) where (N & M) == 0, replace it with A. */
10259 pmop[which] = TREE_OPERAND (pmop[which], 0);
10260 break;
10261 case INTEGER_CST:
10262 /* If C or D is a N where (N & M) == 0, it can be
10263 omitted (assumed 0). */
10264 if ((TREE_CODE (arg0) == PLUS_EXPR
10265 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10266 && (cst1 & wi::to_wide (pmop[which])) == 0)
10267 pmop[which] = NULL;
10268 break;
10269 default:
10270 break;
10273 /* Only build anything new if we optimized one or both arguments
10274 above. */
10275 if (pmop[0] != TREE_OPERAND (arg0, 0)
10276 || (TREE_CODE (arg0) != NEGATE_EXPR
10277 && pmop[1] != TREE_OPERAND (arg0, 1)))
10279 tree utype = TREE_TYPE (arg0);
10280 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10282 /* Perform the operations in a type that has defined
10283 overflow behavior. */
10284 utype = unsigned_type_for (TREE_TYPE (arg0));
10285 if (pmop[0] != NULL)
10286 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10287 if (pmop[1] != NULL)
10288 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10291 if (TREE_CODE (arg0) == NEGATE_EXPR)
10292 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10293 else if (TREE_CODE (arg0) == PLUS_EXPR)
10295 if (pmop[0] != NULL && pmop[1] != NULL)
10296 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10297 pmop[0], pmop[1]);
10298 else if (pmop[0] != NULL)
10299 tem = pmop[0];
10300 else if (pmop[1] != NULL)
10301 tem = pmop[1];
10302 else
10303 return build_int_cst (type, 0);
10305 else if (pmop[0] == NULL)
10306 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10307 else
10308 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10309 pmop[0], pmop[1]);
10310 /* TEM is now the new binary +, - or unary - replacement. */
10311 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10312 fold_convert_loc (loc, utype, arg1));
10313 return fold_convert_loc (loc, type, tem);
10318 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10319 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10320 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10322 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10324 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
10325 if (mask == -1)
10326 return
10327 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10330 goto associate;
10332 case RDIV_EXPR:
10333 /* Don't touch a floating-point divide by zero unless the mode
10334 of the constant can represent infinity. */
10335 if (TREE_CODE (arg1) == REAL_CST
10336 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10337 && real_zerop (arg1))
10338 return NULL_TREE;
10340 /* (-A) / (-B) -> A / B */
10341 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10342 return fold_build2_loc (loc, RDIV_EXPR, type,
10343 TREE_OPERAND (arg0, 0),
10344 negate_expr (arg1));
10345 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10346 return fold_build2_loc (loc, RDIV_EXPR, type,
10347 negate_expr (arg0),
10348 TREE_OPERAND (arg1, 0));
10349 return NULL_TREE;
10351 case TRUNC_DIV_EXPR:
10352 /* Fall through */
10354 case FLOOR_DIV_EXPR:
10355 /* Simplify A / (B << N) where A and B are positive and B is
10356 a power of 2, to A >> (N + log2(B)). */
10357 strict_overflow_p = false;
10358 if (TREE_CODE (arg1) == LSHIFT_EXPR
10359 && (TYPE_UNSIGNED (type)
10360 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10362 tree sval = TREE_OPERAND (arg1, 0);
10363 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10365 tree sh_cnt = TREE_OPERAND (arg1, 1);
10366 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10367 wi::exact_log2 (wi::to_wide (sval)));
10369 if (strict_overflow_p)
10370 fold_overflow_warning (("assuming signed overflow does not "
10371 "occur when simplifying A / (B << N)"),
10372 WARN_STRICT_OVERFLOW_MISC);
10374 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10375 sh_cnt, pow2);
10376 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10377 fold_convert_loc (loc, type, arg0), sh_cnt);
10381 /* Fall through */
10383 case ROUND_DIV_EXPR:
10384 case CEIL_DIV_EXPR:
10385 case EXACT_DIV_EXPR:
10386 if (integer_zerop (arg1))
10387 return NULL_TREE;
10389 /* Convert -A / -B to A / B when the type is signed and overflow is
10390 undefined. */
10391 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10392 && TREE_CODE (op0) == NEGATE_EXPR
10393 && negate_expr_p (op1))
10395 if (INTEGRAL_TYPE_P (type))
10396 fold_overflow_warning (("assuming signed overflow does not occur "
10397 "when distributing negation across "
10398 "division"),
10399 WARN_STRICT_OVERFLOW_MISC);
10400 return fold_build2_loc (loc, code, type,
10401 fold_convert_loc (loc, type,
10402 TREE_OPERAND (arg0, 0)),
10403 negate_expr (op1));
10405 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10406 && TREE_CODE (arg1) == NEGATE_EXPR
10407 && negate_expr_p (op0))
10409 if (INTEGRAL_TYPE_P (type))
10410 fold_overflow_warning (("assuming signed overflow does not occur "
10411 "when distributing negation across "
10412 "division"),
10413 WARN_STRICT_OVERFLOW_MISC);
10414 return fold_build2_loc (loc, code, type,
10415 negate_expr (op0),
10416 fold_convert_loc (loc, type,
10417 TREE_OPERAND (arg1, 0)));
10420 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10421 operation, EXACT_DIV_EXPR.
10423 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10424 At one time others generated faster code, it's not clear if they do
10425 after the last round to changes to the DIV code in expmed.c. */
10426 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10427 && multiple_of_p (type, arg0, arg1))
10428 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10429 fold_convert (type, arg0),
10430 fold_convert (type, arg1));
10432 strict_overflow_p = false;
10433 if (TREE_CODE (arg1) == INTEGER_CST
10434 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10435 &strict_overflow_p)) != 0)
10437 if (strict_overflow_p)
10438 fold_overflow_warning (("assuming signed overflow does not occur "
10439 "when simplifying division"),
10440 WARN_STRICT_OVERFLOW_MISC);
10441 return fold_convert_loc (loc, type, tem);
10444 return NULL_TREE;
10446 case CEIL_MOD_EXPR:
10447 case FLOOR_MOD_EXPR:
10448 case ROUND_MOD_EXPR:
10449 case TRUNC_MOD_EXPR:
10450 strict_overflow_p = false;
10451 if (TREE_CODE (arg1) == INTEGER_CST
10452 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10453 &strict_overflow_p)) != 0)
10455 if (strict_overflow_p)
10456 fold_overflow_warning (("assuming signed overflow does not occur "
10457 "when simplifying modulus"),
10458 WARN_STRICT_OVERFLOW_MISC);
10459 return fold_convert_loc (loc, type, tem);
10462 return NULL_TREE;
10464 case LROTATE_EXPR:
10465 case RROTATE_EXPR:
10466 case RSHIFT_EXPR:
10467 case LSHIFT_EXPR:
10468 /* Since negative shift count is not well-defined,
10469 don't try to compute it in the compiler. */
10470 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10471 return NULL_TREE;
10473 prec = element_precision (type);
10475 /* If we have a rotate of a bit operation with the rotate count and
10476 the second operand of the bit operation both constant,
10477 permute the two operations. */
10478 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10479 && (TREE_CODE (arg0) == BIT_AND_EXPR
10480 || TREE_CODE (arg0) == BIT_IOR_EXPR
10481 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10482 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10484 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10485 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10486 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10487 fold_build2_loc (loc, code, type,
10488 arg00, arg1),
10489 fold_build2_loc (loc, code, type,
10490 arg01, arg1));
10493 /* Two consecutive rotates adding up to the some integer
10494 multiple of the precision of the type can be ignored. */
10495 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10496 && TREE_CODE (arg0) == RROTATE_EXPR
10497 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10498 && wi::umod_trunc (wi::to_wide (arg1)
10499 + wi::to_wide (TREE_OPERAND (arg0, 1)),
10500 prec) == 0)
10501 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10503 return NULL_TREE;
10505 case MIN_EXPR:
10506 case MAX_EXPR:
10507 goto associate;
10509 case TRUTH_ANDIF_EXPR:
10510 /* Note that the operands of this must be ints
10511 and their values must be 0 or 1.
10512 ("true" is a fixed value perhaps depending on the language.) */
10513 /* If first arg is constant zero, return it. */
10514 if (integer_zerop (arg0))
10515 return fold_convert_loc (loc, type, arg0);
10516 /* FALLTHRU */
10517 case TRUTH_AND_EXPR:
10518 /* If either arg is constant true, drop it. */
10519 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10520 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10521 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10522 /* Preserve sequence points. */
10523 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10524 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10525 /* If second arg is constant zero, result is zero, but first arg
10526 must be evaluated. */
10527 if (integer_zerop (arg1))
10528 return omit_one_operand_loc (loc, type, arg1, arg0);
10529 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10530 case will be handled here. */
10531 if (integer_zerop (arg0))
10532 return omit_one_operand_loc (loc, type, arg0, arg1);
10534 /* !X && X is always false. */
10535 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10536 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10537 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10538 /* X && !X is always false. */
10539 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10540 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10541 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10543 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10544 means A >= Y && A != MAX, but in this case we know that
10545 A < X <= MAX. */
10547 if (!TREE_SIDE_EFFECTS (arg0)
10548 && !TREE_SIDE_EFFECTS (arg1))
10550 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10551 if (tem && !operand_equal_p (tem, arg0, 0))
10552 return fold_build2_loc (loc, code, type, tem, arg1);
10554 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10555 if (tem && !operand_equal_p (tem, arg1, 0))
10556 return fold_build2_loc (loc, code, type, arg0, tem);
10559 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10560 != NULL_TREE)
10561 return tem;
10563 return NULL_TREE;
10565 case TRUTH_ORIF_EXPR:
10566 /* Note that the operands of this must be ints
10567 and their values must be 0 or true.
10568 ("true" is a fixed value perhaps depending on the language.) */
10569 /* If first arg is constant true, return it. */
10570 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10571 return fold_convert_loc (loc, type, arg0);
10572 /* FALLTHRU */
10573 case TRUTH_OR_EXPR:
10574 /* If either arg is constant zero, drop it. */
10575 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10576 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10577 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10578 /* Preserve sequence points. */
10579 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10580 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10581 /* If second arg is constant true, result is true, but we must
10582 evaluate first arg. */
10583 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10584 return omit_one_operand_loc (loc, type, arg1, arg0);
10585 /* Likewise for first arg, but note this only occurs here for
10586 TRUTH_OR_EXPR. */
10587 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10588 return omit_one_operand_loc (loc, type, arg0, arg1);
10590 /* !X || X is always true. */
10591 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10592 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10593 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10594 /* X || !X is always true. */
10595 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10596 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10597 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10599 /* (X && !Y) || (!X && Y) is X ^ Y */
10600 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10601 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10603 tree a0, a1, l0, l1, n0, n1;
10605 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10606 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10608 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10609 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10611 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10612 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10614 if ((operand_equal_p (n0, a0, 0)
10615 && operand_equal_p (n1, a1, 0))
10616 || (operand_equal_p (n0, a1, 0)
10617 && operand_equal_p (n1, a0, 0)))
10618 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10621 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10622 != NULL_TREE)
10623 return tem;
10625 return NULL_TREE;
10627 case TRUTH_XOR_EXPR:
10628 /* If the second arg is constant zero, drop it. */
10629 if (integer_zerop (arg1))
10630 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10631 /* If the second arg is constant true, this is a logical inversion. */
10632 if (integer_onep (arg1))
10634 tem = invert_truthvalue_loc (loc, arg0);
10635 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10637 /* Identical arguments cancel to zero. */
10638 if (operand_equal_p (arg0, arg1, 0))
10639 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10641 /* !X ^ X is always true. */
10642 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10643 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10644 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10646 /* X ^ !X is always true. */
10647 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10648 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10649 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10651 return NULL_TREE;
10653 case EQ_EXPR:
10654 case NE_EXPR:
10655 STRIP_NOPS (arg0);
10656 STRIP_NOPS (arg1);
10658 tem = fold_comparison (loc, code, type, op0, op1);
10659 if (tem != NULL_TREE)
10660 return tem;
10662 /* bool_var != 1 becomes !bool_var. */
10663 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10664 && code == NE_EXPR)
10665 return fold_convert_loc (loc, type,
10666 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10667 TREE_TYPE (arg0), arg0));
10669 /* bool_var == 0 becomes !bool_var. */
10670 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10671 && code == EQ_EXPR)
10672 return fold_convert_loc (loc, type,
10673 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10674 TREE_TYPE (arg0), arg0));
10676 /* !exp != 0 becomes !exp */
10677 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10678 && code == NE_EXPR)
10679 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10681 /* If this is an EQ or NE comparison with zero and ARG0 is
10682 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10683 two operations, but the latter can be done in one less insn
10684 on machines that have only two-operand insns or on which a
10685 constant cannot be the first operand. */
10686 if (TREE_CODE (arg0) == BIT_AND_EXPR
10687 && integer_zerop (arg1))
10689 tree arg00 = TREE_OPERAND (arg0, 0);
10690 tree arg01 = TREE_OPERAND (arg0, 1);
10691 if (TREE_CODE (arg00) == LSHIFT_EXPR
10692 && integer_onep (TREE_OPERAND (arg00, 0)))
10694 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10695 arg01, TREE_OPERAND (arg00, 1));
10696 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10697 build_int_cst (TREE_TYPE (arg0), 1));
10698 return fold_build2_loc (loc, code, type,
10699 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10700 arg1);
10702 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10703 && integer_onep (TREE_OPERAND (arg01, 0)))
10705 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10706 arg00, TREE_OPERAND (arg01, 1));
10707 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10708 build_int_cst (TREE_TYPE (arg0), 1));
10709 return fold_build2_loc (loc, code, type,
10710 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10711 arg1);
10715 /* If this is an NE or EQ comparison of zero against the result of a
10716 signed MOD operation whose second operand is a power of 2, make
10717 the MOD operation unsigned since it is simpler and equivalent. */
10718 if (integer_zerop (arg1)
10719 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10720 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10721 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10722 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10723 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10724 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10726 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10727 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10728 fold_convert_loc (loc, newtype,
10729 TREE_OPERAND (arg0, 0)),
10730 fold_convert_loc (loc, newtype,
10731 TREE_OPERAND (arg0, 1)));
10733 return fold_build2_loc (loc, code, type, newmod,
10734 fold_convert_loc (loc, newtype, arg1));
10737 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10738 C1 is a valid shift constant, and C2 is a power of two, i.e.
10739 a single bit. */
10740 if (TREE_CODE (arg0) == BIT_AND_EXPR
10741 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10742 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10743 == INTEGER_CST
10744 && integer_pow2p (TREE_OPERAND (arg0, 1))
10745 && integer_zerop (arg1))
10747 tree itype = TREE_TYPE (arg0);
10748 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10749 prec = TYPE_PRECISION (itype);
10751 /* Check for a valid shift count. */
10752 if (wi::ltu_p (wi::to_wide (arg001), prec))
10754 tree arg01 = TREE_OPERAND (arg0, 1);
10755 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10756 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10757 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10758 can be rewritten as (X & (C2 << C1)) != 0. */
10759 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10761 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10762 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10763 return fold_build2_loc (loc, code, type, tem,
10764 fold_convert_loc (loc, itype, arg1));
10766 /* Otherwise, for signed (arithmetic) shifts,
10767 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10768 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10769 else if (!TYPE_UNSIGNED (itype))
10770 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10771 arg000, build_int_cst (itype, 0));
10772 /* Otherwise, of unsigned (logical) shifts,
10773 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10774 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10775 else
10776 return omit_one_operand_loc (loc, type,
10777 code == EQ_EXPR ? integer_one_node
10778 : integer_zero_node,
10779 arg000);
10783 /* If this is a comparison of a field, we may be able to simplify it. */
10784 if ((TREE_CODE (arg0) == COMPONENT_REF
10785 || TREE_CODE (arg0) == BIT_FIELD_REF)
10786 /* Handle the constant case even without -O
10787 to make sure the warnings are given. */
10788 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10790 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10791 if (t1)
10792 return t1;
10795 /* Optimize comparisons of strlen vs zero to a compare of the
10796 first character of the string vs zero. To wit,
10797 strlen(ptr) == 0 => *ptr == 0
10798 strlen(ptr) != 0 => *ptr != 0
10799 Other cases should reduce to one of these two (or a constant)
10800 due to the return value of strlen being unsigned. */
10801 if (TREE_CODE (arg0) == CALL_EXPR
10802 && integer_zerop (arg1))
10804 tree fndecl = get_callee_fndecl (arg0);
10806 if (fndecl
10807 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10808 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10809 && call_expr_nargs (arg0) == 1
10810 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10812 tree iref = build_fold_indirect_ref_loc (loc,
10813 CALL_EXPR_ARG (arg0, 0));
10814 return fold_build2_loc (loc, code, type, iref,
10815 build_int_cst (TREE_TYPE (iref), 0));
10819 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10820 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10821 if (TREE_CODE (arg0) == RSHIFT_EXPR
10822 && integer_zerop (arg1)
10823 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10825 tree arg00 = TREE_OPERAND (arg0, 0);
10826 tree arg01 = TREE_OPERAND (arg0, 1);
10827 tree itype = TREE_TYPE (arg00);
10828 if (wi::to_wide (arg01) == element_precision (itype) - 1)
10830 if (TYPE_UNSIGNED (itype))
10832 itype = signed_type_for (itype);
10833 arg00 = fold_convert_loc (loc, itype, arg00);
10835 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10836 type, arg00, build_zero_cst (itype));
10840 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10841 (X & C) == 0 when C is a single bit. */
10842 if (TREE_CODE (arg0) == BIT_AND_EXPR
10843 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10844 && integer_zerop (arg1)
10845 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10847 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10848 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10849 TREE_OPERAND (arg0, 1));
10850 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10851 type, tem,
10852 fold_convert_loc (loc, TREE_TYPE (arg0),
10853 arg1));
10856 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10857 constant C is a power of two, i.e. a single bit. */
10858 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10859 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10860 && integer_zerop (arg1)
10861 && integer_pow2p (TREE_OPERAND (arg0, 1))
10862 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10863 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10865 tree arg00 = TREE_OPERAND (arg0, 0);
10866 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10867 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10870 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10871 when is C is a power of two, i.e. a single bit. */
10872 if (TREE_CODE (arg0) == BIT_AND_EXPR
10873 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10874 && integer_zerop (arg1)
10875 && integer_pow2p (TREE_OPERAND (arg0, 1))
10876 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10877 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10879 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10880 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10881 arg000, TREE_OPERAND (arg0, 1));
10882 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10883 tem, build_int_cst (TREE_TYPE (tem), 0));
10886 if (integer_zerop (arg1)
10887 && tree_expr_nonzero_p (arg0))
10889 tree res = constant_boolean_node (code==NE_EXPR, type);
10890 return omit_one_operand_loc (loc, type, res, arg0);
10893 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10894 if (TREE_CODE (arg0) == BIT_AND_EXPR
10895 && TREE_CODE (arg1) == BIT_AND_EXPR)
10897 tree arg00 = TREE_OPERAND (arg0, 0);
10898 tree arg01 = TREE_OPERAND (arg0, 1);
10899 tree arg10 = TREE_OPERAND (arg1, 0);
10900 tree arg11 = TREE_OPERAND (arg1, 1);
10901 tree itype = TREE_TYPE (arg0);
10903 if (operand_equal_p (arg01, arg11, 0))
10905 tem = fold_convert_loc (loc, itype, arg10);
10906 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10907 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10908 return fold_build2_loc (loc, code, type, tem,
10909 build_zero_cst (itype));
10911 if (operand_equal_p (arg01, arg10, 0))
10913 tem = fold_convert_loc (loc, itype, arg11);
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 (arg00, arg11, 0))
10921 tem = fold_convert_loc (loc, itype, arg10);
10922 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10923 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10924 return fold_build2_loc (loc, code, type, tem,
10925 build_zero_cst (itype));
10927 if (operand_equal_p (arg00, arg10, 0))
10929 tem = fold_convert_loc (loc, itype, arg11);
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));
10937 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10938 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10940 tree arg00 = TREE_OPERAND (arg0, 0);
10941 tree arg01 = TREE_OPERAND (arg0, 1);
10942 tree arg10 = TREE_OPERAND (arg1, 0);
10943 tree arg11 = TREE_OPERAND (arg1, 1);
10944 tree itype = TREE_TYPE (arg0);
10946 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10947 operand_equal_p guarantees no side-effects so we don't need
10948 to use omit_one_operand on Z. */
10949 if (operand_equal_p (arg01, arg11, 0))
10950 return fold_build2_loc (loc, code, type, arg00,
10951 fold_convert_loc (loc, TREE_TYPE (arg00),
10952 arg10));
10953 if (operand_equal_p (arg01, arg10, 0))
10954 return fold_build2_loc (loc, code, type, arg00,
10955 fold_convert_loc (loc, TREE_TYPE (arg00),
10956 arg11));
10957 if (operand_equal_p (arg00, arg11, 0))
10958 return fold_build2_loc (loc, code, type, arg01,
10959 fold_convert_loc (loc, TREE_TYPE (arg01),
10960 arg10));
10961 if (operand_equal_p (arg00, arg10, 0))
10962 return fold_build2_loc (loc, code, type, arg01,
10963 fold_convert_loc (loc, TREE_TYPE (arg01),
10964 arg11));
10966 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10967 if (TREE_CODE (arg01) == INTEGER_CST
10968 && TREE_CODE (arg11) == INTEGER_CST)
10970 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10971 fold_convert_loc (loc, itype, arg11));
10972 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10973 return fold_build2_loc (loc, code, type, tem,
10974 fold_convert_loc (loc, itype, arg10));
10978 /* Attempt to simplify equality/inequality comparisons of complex
10979 values. Only lower the comparison if the result is known or
10980 can be simplified to a single scalar comparison. */
10981 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10982 || TREE_CODE (arg0) == COMPLEX_CST)
10983 && (TREE_CODE (arg1) == COMPLEX_EXPR
10984 || TREE_CODE (arg1) == COMPLEX_CST))
10986 tree real0, imag0, real1, imag1;
10987 tree rcond, icond;
10989 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10991 real0 = TREE_OPERAND (arg0, 0);
10992 imag0 = TREE_OPERAND (arg0, 1);
10994 else
10996 real0 = TREE_REALPART (arg0);
10997 imag0 = TREE_IMAGPART (arg0);
11000 if (TREE_CODE (arg1) == COMPLEX_EXPR)
11002 real1 = TREE_OPERAND (arg1, 0);
11003 imag1 = TREE_OPERAND (arg1, 1);
11005 else
11007 real1 = TREE_REALPART (arg1);
11008 imag1 = TREE_IMAGPART (arg1);
11011 rcond = fold_binary_loc (loc, code, type, real0, real1);
11012 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11014 if (integer_zerop (rcond))
11016 if (code == EQ_EXPR)
11017 return omit_two_operands_loc (loc, type, boolean_false_node,
11018 imag0, imag1);
11019 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
11021 else
11023 if (code == NE_EXPR)
11024 return omit_two_operands_loc (loc, type, boolean_true_node,
11025 imag0, imag1);
11026 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
11030 icond = fold_binary_loc (loc, code, type, imag0, imag1);
11031 if (icond && TREE_CODE (icond) == INTEGER_CST)
11033 if (integer_zerop (icond))
11035 if (code == EQ_EXPR)
11036 return omit_two_operands_loc (loc, type, boolean_false_node,
11037 real0, real1);
11038 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11040 else
11042 if (code == NE_EXPR)
11043 return omit_two_operands_loc (loc, type, boolean_true_node,
11044 real0, real1);
11045 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11050 return NULL_TREE;
11052 case LT_EXPR:
11053 case GT_EXPR:
11054 case LE_EXPR:
11055 case GE_EXPR:
11056 tem = fold_comparison (loc, code, type, op0, op1);
11057 if (tem != NULL_TREE)
11058 return tem;
11060 /* Transform comparisons of the form X +- C CMP X. */
11061 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11062 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11063 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11064 && !HONOR_SNANS (arg0))
11066 tree arg01 = TREE_OPERAND (arg0, 1);
11067 enum tree_code code0 = TREE_CODE (arg0);
11068 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11070 /* (X - c) > X becomes false. */
11071 if (code == GT_EXPR
11072 && ((code0 == MINUS_EXPR && is_positive >= 0)
11073 || (code0 == PLUS_EXPR && is_positive <= 0)))
11074 return constant_boolean_node (0, type);
11076 /* Likewise (X + c) < X becomes false. */
11077 if (code == LT_EXPR
11078 && ((code0 == PLUS_EXPR && is_positive >= 0)
11079 || (code0 == MINUS_EXPR && is_positive <= 0)))
11080 return constant_boolean_node (0, type);
11082 /* Convert (X - c) <= X to true. */
11083 if (!HONOR_NANS (arg1)
11084 && code == LE_EXPR
11085 && ((code0 == MINUS_EXPR && is_positive >= 0)
11086 || (code0 == PLUS_EXPR && is_positive <= 0)))
11087 return constant_boolean_node (1, type);
11089 /* Convert (X + c) >= X to true. */
11090 if (!HONOR_NANS (arg1)
11091 && code == GE_EXPR
11092 && ((code0 == PLUS_EXPR && is_positive >= 0)
11093 || (code0 == MINUS_EXPR && is_positive <= 0)))
11094 return constant_boolean_node (1, type);
11097 /* If we are comparing an ABS_EXPR with a constant, we can
11098 convert all the cases into explicit comparisons, but they may
11099 well not be faster than doing the ABS and one comparison.
11100 But ABS (X) <= C is a range comparison, which becomes a subtraction
11101 and a comparison, and is probably faster. */
11102 if (code == LE_EXPR
11103 && TREE_CODE (arg1) == INTEGER_CST
11104 && TREE_CODE (arg0) == ABS_EXPR
11105 && ! TREE_SIDE_EFFECTS (arg0)
11106 && (tem = negate_expr (arg1)) != 0
11107 && TREE_CODE (tem) == INTEGER_CST
11108 && !TREE_OVERFLOW (tem))
11109 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11110 build2 (GE_EXPR, type,
11111 TREE_OPERAND (arg0, 0), tem),
11112 build2 (LE_EXPR, type,
11113 TREE_OPERAND (arg0, 0), arg1));
11115 /* Convert ABS_EXPR<x> >= 0 to true. */
11116 strict_overflow_p = false;
11117 if (code == GE_EXPR
11118 && (integer_zerop (arg1)
11119 || (! HONOR_NANS (arg0)
11120 && real_zerop (arg1)))
11121 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11123 if (strict_overflow_p)
11124 fold_overflow_warning (("assuming signed overflow does not occur "
11125 "when simplifying comparison of "
11126 "absolute value and zero"),
11127 WARN_STRICT_OVERFLOW_CONDITIONAL);
11128 return omit_one_operand_loc (loc, type,
11129 constant_boolean_node (true, type),
11130 arg0);
11133 /* Convert ABS_EXPR<x> < 0 to false. */
11134 strict_overflow_p = false;
11135 if (code == LT_EXPR
11136 && (integer_zerop (arg1) || real_zerop (arg1))
11137 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11139 if (strict_overflow_p)
11140 fold_overflow_warning (("assuming signed overflow does not occur "
11141 "when simplifying comparison of "
11142 "absolute value and zero"),
11143 WARN_STRICT_OVERFLOW_CONDITIONAL);
11144 return omit_one_operand_loc (loc, type,
11145 constant_boolean_node (false, type),
11146 arg0);
11149 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11150 and similarly for >= into !=. */
11151 if ((code == LT_EXPR || code == GE_EXPR)
11152 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11153 && TREE_CODE (arg1) == LSHIFT_EXPR
11154 && integer_onep (TREE_OPERAND (arg1, 0)))
11155 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11156 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11157 TREE_OPERAND (arg1, 1)),
11158 build_zero_cst (TREE_TYPE (arg0)));
11160 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11161 otherwise Y might be >= # of bits in X's type and thus e.g.
11162 (unsigned char) (1 << Y) for Y 15 might be 0.
11163 If the cast is widening, then 1 << Y should have unsigned type,
11164 otherwise if Y is number of bits in the signed shift type minus 1,
11165 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11166 31 might be 0xffffffff80000000. */
11167 if ((code == LT_EXPR || code == GE_EXPR)
11168 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11169 && CONVERT_EXPR_P (arg1)
11170 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11171 && (element_precision (TREE_TYPE (arg1))
11172 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11173 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11174 || (element_precision (TREE_TYPE (arg1))
11175 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11176 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11178 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11179 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11180 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11181 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11182 build_zero_cst (TREE_TYPE (arg0)));
11185 return NULL_TREE;
11187 case UNORDERED_EXPR:
11188 case ORDERED_EXPR:
11189 case UNLT_EXPR:
11190 case UNLE_EXPR:
11191 case UNGT_EXPR:
11192 case UNGE_EXPR:
11193 case UNEQ_EXPR:
11194 case LTGT_EXPR:
11195 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11197 tree targ0 = strip_float_extensions (arg0);
11198 tree targ1 = strip_float_extensions (arg1);
11199 tree newtype = TREE_TYPE (targ0);
11201 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11202 newtype = TREE_TYPE (targ1);
11204 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11205 return fold_build2_loc (loc, code, type,
11206 fold_convert_loc (loc, newtype, targ0),
11207 fold_convert_loc (loc, newtype, targ1));
11210 return NULL_TREE;
11212 case COMPOUND_EXPR:
11213 /* When pedantic, a compound expression can be neither an lvalue
11214 nor an integer constant expression. */
11215 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11216 return NULL_TREE;
11217 /* Don't let (0, 0) be null pointer constant. */
11218 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11219 : fold_convert_loc (loc, type, arg1);
11220 return pedantic_non_lvalue_loc (loc, tem);
11222 case ASSERT_EXPR:
11223 /* An ASSERT_EXPR should never be passed to fold_binary. */
11224 gcc_unreachable ();
11226 default:
11227 return NULL_TREE;
11228 } /* switch (code) */
11231 /* Used by contains_label_[p1]. */
11233 struct contains_label_data
11235 hash_set<tree> *pset;
11236 bool inside_switch_p;
11239 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11240 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
11241 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
11243 static tree
11244 contains_label_1 (tree *tp, int *walk_subtrees, void *data)
11246 contains_label_data *d = (contains_label_data *) data;
11247 switch (TREE_CODE (*tp))
11249 case LABEL_EXPR:
11250 return *tp;
11252 case CASE_LABEL_EXPR:
11253 if (!d->inside_switch_p)
11254 return *tp;
11255 return NULL_TREE;
11257 case SWITCH_EXPR:
11258 if (!d->inside_switch_p)
11260 if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
11261 return *tp;
11262 d->inside_switch_p = true;
11263 if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
11264 return *tp;
11265 d->inside_switch_p = false;
11266 *walk_subtrees = 0;
11268 return NULL_TREE;
11270 case GOTO_EXPR:
11271 *walk_subtrees = 0;
11272 return NULL_TREE;
11274 default:
11275 return NULL_TREE;
11279 /* Return whether the sub-tree ST contains a label which is accessible from
11280 outside the sub-tree. */
11282 static bool
11283 contains_label_p (tree st)
11285 hash_set<tree> pset;
11286 contains_label_data data = { &pset, false };
11287 return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
11290 /* Fold a ternary expression of code CODE and type TYPE with operands
11291 OP0, OP1, and OP2. Return the folded expression if folding is
11292 successful. Otherwise, return NULL_TREE. */
11294 tree
11295 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11296 tree op0, tree op1, tree op2)
11298 tree tem;
11299 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11300 enum tree_code_class kind = TREE_CODE_CLASS (code);
11302 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11303 && TREE_CODE_LENGTH (code) == 3);
11305 /* If this is a commutative operation, and OP0 is a constant, move it
11306 to OP1 to reduce the number of tests below. */
11307 if (commutative_ternary_tree_code (code)
11308 && tree_swap_operands_p (op0, op1))
11309 return fold_build3_loc (loc, code, type, op1, op0, op2);
11311 tem = generic_simplify (loc, code, type, op0, op1, op2);
11312 if (tem)
11313 return tem;
11315 /* Strip any conversions that don't change the mode. This is safe
11316 for every expression, except for a comparison expression because
11317 its signedness is derived from its operands. So, in the latter
11318 case, only strip conversions that don't change the signedness.
11320 Note that this is done as an internal manipulation within the
11321 constant folder, in order to find the simplest representation of
11322 the arguments so that their form can be studied. In any cases,
11323 the appropriate type conversions should be put back in the tree
11324 that will get out of the constant folder. */
11325 if (op0)
11327 arg0 = op0;
11328 STRIP_NOPS (arg0);
11331 if (op1)
11333 arg1 = op1;
11334 STRIP_NOPS (arg1);
11337 if (op2)
11339 arg2 = op2;
11340 STRIP_NOPS (arg2);
11343 switch (code)
11345 case COMPONENT_REF:
11346 if (TREE_CODE (arg0) == CONSTRUCTOR
11347 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11349 unsigned HOST_WIDE_INT idx;
11350 tree field, value;
11351 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11352 if (field == arg1)
11353 return value;
11355 return NULL_TREE;
11357 case COND_EXPR:
11358 case VEC_COND_EXPR:
11359 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11360 so all simple results must be passed through pedantic_non_lvalue. */
11361 if (TREE_CODE (arg0) == INTEGER_CST)
11363 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11364 tem = integer_zerop (arg0) ? op2 : op1;
11365 /* Only optimize constant conditions when the selected branch
11366 has the same type as the COND_EXPR. This avoids optimizing
11367 away "c ? x : throw", where the throw has a void type.
11368 Avoid throwing away that operand which contains label. */
11369 if ((!TREE_SIDE_EFFECTS (unused_op)
11370 || !contains_label_p (unused_op))
11371 && (! VOID_TYPE_P (TREE_TYPE (tem))
11372 || VOID_TYPE_P (type)))
11373 return pedantic_non_lvalue_loc (loc, tem);
11374 return NULL_TREE;
11376 else if (TREE_CODE (arg0) == VECTOR_CST)
11378 unsigned HOST_WIDE_INT nelts;
11379 if ((TREE_CODE (arg1) == VECTOR_CST
11380 || TREE_CODE (arg1) == CONSTRUCTOR)
11381 && (TREE_CODE (arg2) == VECTOR_CST
11382 || TREE_CODE (arg2) == CONSTRUCTOR)
11383 && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
11385 vec_perm_builder sel (nelts, nelts, 1);
11386 for (unsigned int i = 0; i < nelts; i++)
11388 tree val = VECTOR_CST_ELT (arg0, i);
11389 if (integer_all_onesp (val))
11390 sel.quick_push (i);
11391 else if (integer_zerop (val))
11392 sel.quick_push (nelts + i);
11393 else /* Currently unreachable. */
11394 return NULL_TREE;
11396 vec_perm_indices indices (sel, 2, nelts);
11397 tree t = fold_vec_perm (type, arg1, arg2, indices);
11398 if (t != NULL_TREE)
11399 return t;
11403 /* If we have A op B ? A : C, we may be able to convert this to a
11404 simpler expression, depending on the operation and the values
11405 of B and C. Signed zeros prevent all of these transformations,
11406 for reasons given above each one.
11408 Also try swapping the arguments and inverting the conditional. */
11409 if (COMPARISON_CLASS_P (arg0)
11410 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
11411 && !HONOR_SIGNED_ZEROS (element_mode (op1)))
11413 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11414 if (tem)
11415 return tem;
11418 if (COMPARISON_CLASS_P (arg0)
11419 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11420 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11422 location_t loc0 = expr_location_or (arg0, loc);
11423 tem = fold_invert_truthvalue (loc0, arg0);
11424 if (tem && COMPARISON_CLASS_P (tem))
11426 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11427 if (tem)
11428 return tem;
11432 /* If the second operand is simpler than the third, swap them
11433 since that produces better jump optimization results. */
11434 if (truth_value_p (TREE_CODE (arg0))
11435 && tree_swap_operands_p (op1, op2))
11437 location_t loc0 = expr_location_or (arg0, loc);
11438 /* See if this can be inverted. If it can't, possibly because
11439 it was a floating-point inequality comparison, don't do
11440 anything. */
11441 tem = fold_invert_truthvalue (loc0, arg0);
11442 if (tem)
11443 return fold_build3_loc (loc, code, type, tem, op2, op1);
11446 /* Convert A ? 1 : 0 to simply A. */
11447 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11448 : (integer_onep (op1)
11449 && !VECTOR_TYPE_P (type)))
11450 && integer_zerop (op2)
11451 /* If we try to convert OP0 to our type, the
11452 call to fold will try to move the conversion inside
11453 a COND, which will recurse. In that case, the COND_EXPR
11454 is probably the best choice, so leave it alone. */
11455 && type == TREE_TYPE (arg0))
11456 return pedantic_non_lvalue_loc (loc, arg0);
11458 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11459 over COND_EXPR in cases such as floating point comparisons. */
11460 if (integer_zerop (op1)
11461 && code == COND_EXPR
11462 && integer_onep (op2)
11463 && !VECTOR_TYPE_P (type)
11464 && truth_value_p (TREE_CODE (arg0)))
11465 return pedantic_non_lvalue_loc (loc,
11466 fold_convert_loc (loc, type,
11467 invert_truthvalue_loc (loc,
11468 arg0)));
11470 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11471 if (TREE_CODE (arg0) == LT_EXPR
11472 && integer_zerop (TREE_OPERAND (arg0, 1))
11473 && integer_zerop (op2)
11474 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11476 /* sign_bit_p looks through both zero and sign extensions,
11477 but for this optimization only sign extensions are
11478 usable. */
11479 tree tem2 = TREE_OPERAND (arg0, 0);
11480 while (tem != tem2)
11482 if (TREE_CODE (tem2) != NOP_EXPR
11483 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11485 tem = NULL_TREE;
11486 break;
11488 tem2 = TREE_OPERAND (tem2, 0);
11490 /* sign_bit_p only checks ARG1 bits within A's precision.
11491 If <sign bit of A> has wider type than A, bits outside
11492 of A's precision in <sign bit of A> need to be checked.
11493 If they are all 0, this optimization needs to be done
11494 in unsigned A's type, if they are all 1 in signed A's type,
11495 otherwise this can't be done. */
11496 if (tem
11497 && TYPE_PRECISION (TREE_TYPE (tem))
11498 < TYPE_PRECISION (TREE_TYPE (arg1))
11499 && TYPE_PRECISION (TREE_TYPE (tem))
11500 < TYPE_PRECISION (type))
11502 int inner_width, outer_width;
11503 tree tem_type;
11505 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11506 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11507 if (outer_width > TYPE_PRECISION (type))
11508 outer_width = TYPE_PRECISION (type);
11510 wide_int mask = wi::shifted_mask
11511 (inner_width, outer_width - inner_width, false,
11512 TYPE_PRECISION (TREE_TYPE (arg1)));
11514 wide_int common = mask & wi::to_wide (arg1);
11515 if (common == mask)
11517 tem_type = signed_type_for (TREE_TYPE (tem));
11518 tem = fold_convert_loc (loc, tem_type, tem);
11520 else if (common == 0)
11522 tem_type = unsigned_type_for (TREE_TYPE (tem));
11523 tem = fold_convert_loc (loc, tem_type, tem);
11525 else
11526 tem = NULL;
11529 if (tem)
11530 return
11531 fold_convert_loc (loc, type,
11532 fold_build2_loc (loc, BIT_AND_EXPR,
11533 TREE_TYPE (tem), tem,
11534 fold_convert_loc (loc,
11535 TREE_TYPE (tem),
11536 arg1)));
11539 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11540 already handled above. */
11541 if (TREE_CODE (arg0) == BIT_AND_EXPR
11542 && integer_onep (TREE_OPERAND (arg0, 1))
11543 && integer_zerop (op2)
11544 && integer_pow2p (arg1))
11546 tree tem = TREE_OPERAND (arg0, 0);
11547 STRIP_NOPS (tem);
11548 if (TREE_CODE (tem) == RSHIFT_EXPR
11549 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11550 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11551 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11552 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11553 fold_convert_loc (loc, type,
11554 TREE_OPERAND (tem, 0)),
11555 op1);
11558 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11559 is probably obsolete because the first operand should be a
11560 truth value (that's why we have the two cases above), but let's
11561 leave it in until we can confirm this for all front-ends. */
11562 if (integer_zerop (op2)
11563 && TREE_CODE (arg0) == NE_EXPR
11564 && integer_zerop (TREE_OPERAND (arg0, 1))
11565 && integer_pow2p (arg1)
11566 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11567 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11568 arg1, OEP_ONLY_CONST))
11569 return pedantic_non_lvalue_loc (loc,
11570 fold_convert_loc (loc, type,
11571 TREE_OPERAND (arg0, 0)));
11573 /* Disable the transformations below for vectors, since
11574 fold_binary_op_with_conditional_arg may undo them immediately,
11575 yielding an infinite loop. */
11576 if (code == VEC_COND_EXPR)
11577 return NULL_TREE;
11579 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11580 if (integer_zerop (op2)
11581 && truth_value_p (TREE_CODE (arg0))
11582 && truth_value_p (TREE_CODE (arg1))
11583 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11584 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11585 : TRUTH_ANDIF_EXPR,
11586 type, fold_convert_loc (loc, type, arg0), op1);
11588 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11589 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11590 && truth_value_p (TREE_CODE (arg0))
11591 && truth_value_p (TREE_CODE (arg1))
11592 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11594 location_t loc0 = expr_location_or (arg0, loc);
11595 /* Only perform transformation if ARG0 is easily inverted. */
11596 tem = fold_invert_truthvalue (loc0, arg0);
11597 if (tem)
11598 return fold_build2_loc (loc, code == VEC_COND_EXPR
11599 ? BIT_IOR_EXPR
11600 : TRUTH_ORIF_EXPR,
11601 type, fold_convert_loc (loc, type, tem),
11602 op1);
11605 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11606 if (integer_zerop (arg1)
11607 && truth_value_p (TREE_CODE (arg0))
11608 && truth_value_p (TREE_CODE (op2))
11609 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11611 location_t loc0 = expr_location_or (arg0, loc);
11612 /* Only perform transformation if ARG0 is easily inverted. */
11613 tem = fold_invert_truthvalue (loc0, arg0);
11614 if (tem)
11615 return fold_build2_loc (loc, code == VEC_COND_EXPR
11616 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11617 type, fold_convert_loc (loc, type, tem),
11618 op2);
11621 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11622 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11623 && truth_value_p (TREE_CODE (arg0))
11624 && truth_value_p (TREE_CODE (op2))
11625 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11626 return fold_build2_loc (loc, code == VEC_COND_EXPR
11627 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11628 type, fold_convert_loc (loc, type, arg0), op2);
11630 return NULL_TREE;
11632 case CALL_EXPR:
11633 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11634 of fold_ternary on them. */
11635 gcc_unreachable ();
11637 case BIT_FIELD_REF:
11638 if (TREE_CODE (arg0) == VECTOR_CST
11639 && (type == TREE_TYPE (TREE_TYPE (arg0))
11640 || (VECTOR_TYPE_P (type)
11641 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
11642 && tree_fits_uhwi_p (op1)
11643 && tree_fits_uhwi_p (op2))
11645 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11646 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11647 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11648 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11650 if (n != 0
11651 && (idx % width) == 0
11652 && (n % width) == 0
11653 && known_le ((idx + n) / width,
11654 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
11656 idx = idx / width;
11657 n = n / width;
11659 if (TREE_CODE (arg0) == VECTOR_CST)
11661 if (n == 1)
11663 tem = VECTOR_CST_ELT (arg0, idx);
11664 if (VECTOR_TYPE_P (type))
11665 tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
11666 return tem;
11669 tree_vector_builder vals (type, n, 1);
11670 for (unsigned i = 0; i < n; ++i)
11671 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
11672 return vals.build ();
11677 /* On constants we can use native encode/interpret to constant
11678 fold (nearly) all BIT_FIELD_REFs. */
11679 if (CONSTANT_CLASS_P (arg0)
11680 && can_native_interpret_type_p (type)
11681 && BITS_PER_UNIT == 8
11682 && tree_fits_uhwi_p (op1)
11683 && tree_fits_uhwi_p (op2))
11685 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11686 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11687 /* Limit us to a reasonable amount of work. To relax the
11688 other limitations we need bit-shifting of the buffer
11689 and rounding up the size. */
11690 if (bitpos % BITS_PER_UNIT == 0
11691 && bitsize % BITS_PER_UNIT == 0
11692 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11694 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11695 unsigned HOST_WIDE_INT len
11696 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11697 bitpos / BITS_PER_UNIT);
11698 if (len > 0
11699 && len * BITS_PER_UNIT >= bitsize)
11701 tree v = native_interpret_expr (type, b,
11702 bitsize / BITS_PER_UNIT);
11703 if (v)
11704 return v;
11709 return NULL_TREE;
11711 case FMA_EXPR:
11712 /* For integers we can decompose the FMA if possible. */
11713 if (TREE_CODE (arg0) == INTEGER_CST
11714 && TREE_CODE (arg1) == INTEGER_CST)
11715 return fold_build2_loc (loc, PLUS_EXPR, type,
11716 const_binop (MULT_EXPR, arg0, arg1), arg2);
11717 if (integer_zerop (arg2))
11718 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11720 return fold_fma (loc, type, arg0, arg1, arg2);
11722 case VEC_PERM_EXPR:
11723 if (TREE_CODE (arg2) == VECTOR_CST)
11725 /* Build a vector of integers from the tree mask. */
11726 vec_perm_builder builder;
11727 if (!tree_to_vec_perm_builder (&builder, arg2))
11728 return NULL_TREE;
11730 /* Create a vec_perm_indices for the integer vector. */
11731 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
11732 bool single_arg = (op0 == op1);
11733 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
11735 /* Check for cases that fold to OP0 or OP1 in their original
11736 element order. */
11737 if (sel.series_p (0, 1, 0, 1))
11738 return op0;
11739 if (sel.series_p (0, 1, nelts, 1))
11740 return op1;
11742 if (!single_arg)
11744 if (sel.all_from_input_p (0))
11745 op1 = op0;
11746 else if (sel.all_from_input_p (1))
11748 op0 = op1;
11749 sel.rotate_inputs (1);
11753 if ((TREE_CODE (op0) == VECTOR_CST
11754 || TREE_CODE (op0) == CONSTRUCTOR)
11755 && (TREE_CODE (op1) == VECTOR_CST
11756 || TREE_CODE (op1) == CONSTRUCTOR))
11758 tree t = fold_vec_perm (type, op0, op1, sel);
11759 if (t != NULL_TREE)
11760 return t;
11763 bool changed = (op0 == op1 && !single_arg);
11765 /* Generate a canonical form of the selector. */
11766 if (arg2 == op2 && sel.encoding () != builder)
11768 /* Some targets are deficient and fail to expand a single
11769 argument permutation while still allowing an equivalent
11770 2-argument version. */
11771 if (sel.ninputs () == 2
11772 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
11773 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11774 else
11776 vec_perm_indices sel2 (builder, 2, nelts);
11777 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
11778 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel2);
11779 else
11780 /* Not directly supported with either encoding,
11781 so use the preferred form. */
11782 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11784 changed = true;
11787 if (changed)
11788 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11790 return NULL_TREE;
11792 case BIT_INSERT_EXPR:
11793 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11794 if (TREE_CODE (arg0) == INTEGER_CST
11795 && TREE_CODE (arg1) == INTEGER_CST)
11797 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11798 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11799 wide_int tem = (wi::to_wide (arg0)
11800 & wi::shifted_mask (bitpos, bitsize, true,
11801 TYPE_PRECISION (type)));
11802 wide_int tem2
11803 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11804 bitsize), bitpos);
11805 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11807 else if (TREE_CODE (arg0) == VECTOR_CST
11808 && CONSTANT_CLASS_P (arg1)
11809 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11810 TREE_TYPE (arg1)))
11812 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11813 unsigned HOST_WIDE_INT elsize
11814 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11815 if (bitpos % elsize == 0)
11817 unsigned k = bitpos / elsize;
11818 unsigned HOST_WIDE_INT nelts;
11819 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11820 return arg0;
11821 else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
11823 tree_vector_builder elts (type, nelts, 1);
11824 elts.quick_grow (nelts);
11825 for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
11826 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
11827 return elts.build ();
11831 return NULL_TREE;
11833 default:
11834 return NULL_TREE;
11835 } /* switch (code) */
11838 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11839 of an array (or vector). */
11841 tree
11842 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11844 tree index_type = NULL_TREE;
11845 offset_int low_bound = 0;
11847 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11849 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11850 if (domain_type && TYPE_MIN_VALUE (domain_type))
11852 /* Static constructors for variably sized objects makes no sense. */
11853 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11854 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11855 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11859 if (index_type)
11860 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11861 TYPE_SIGN (index_type));
11863 offset_int index = low_bound - 1;
11864 if (index_type)
11865 index = wi::ext (index, TYPE_PRECISION (index_type),
11866 TYPE_SIGN (index_type));
11868 offset_int max_index;
11869 unsigned HOST_WIDE_INT cnt;
11870 tree cfield, cval;
11872 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11874 /* Array constructor might explicitly set index, or specify a range,
11875 or leave index NULL meaning that it is next index after previous
11876 one. */
11877 if (cfield)
11879 if (TREE_CODE (cfield) == INTEGER_CST)
11880 max_index = index = wi::to_offset (cfield);
11881 else
11883 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11884 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11885 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11888 else
11890 index += 1;
11891 if (index_type)
11892 index = wi::ext (index, TYPE_PRECISION (index_type),
11893 TYPE_SIGN (index_type));
11894 max_index = index;
11897 /* Do we have match? */
11898 if (wi::cmpu (access_index, index) >= 0
11899 && wi::cmpu (access_index, max_index) <= 0)
11900 return cval;
11902 return NULL_TREE;
11905 /* Perform constant folding and related simplification of EXPR.
11906 The related simplifications include x*1 => x, x*0 => 0, etc.,
11907 and application of the associative law.
11908 NOP_EXPR conversions may be removed freely (as long as we
11909 are careful not to change the type of the overall expression).
11910 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11911 but we can constant-fold them if they have constant operands. */
11913 #ifdef ENABLE_FOLD_CHECKING
11914 # define fold(x) fold_1 (x)
11915 static tree fold_1 (tree);
11916 static
11917 #endif
11918 tree
11919 fold (tree expr)
11921 const tree t = expr;
11922 enum tree_code code = TREE_CODE (t);
11923 enum tree_code_class kind = TREE_CODE_CLASS (code);
11924 tree tem;
11925 location_t loc = EXPR_LOCATION (expr);
11927 /* Return right away if a constant. */
11928 if (kind == tcc_constant)
11929 return t;
11931 /* CALL_EXPR-like objects with variable numbers of operands are
11932 treated specially. */
11933 if (kind == tcc_vl_exp)
11935 if (code == CALL_EXPR)
11937 tem = fold_call_expr (loc, expr, false);
11938 return tem ? tem : expr;
11940 return expr;
11943 if (IS_EXPR_CODE_CLASS (kind))
11945 tree type = TREE_TYPE (t);
11946 tree op0, op1, op2;
11948 switch (TREE_CODE_LENGTH (code))
11950 case 1:
11951 op0 = TREE_OPERAND (t, 0);
11952 tem = fold_unary_loc (loc, code, type, op0);
11953 return tem ? tem : expr;
11954 case 2:
11955 op0 = TREE_OPERAND (t, 0);
11956 op1 = TREE_OPERAND (t, 1);
11957 tem = fold_binary_loc (loc, code, type, op0, op1);
11958 return tem ? tem : expr;
11959 case 3:
11960 op0 = TREE_OPERAND (t, 0);
11961 op1 = TREE_OPERAND (t, 1);
11962 op2 = TREE_OPERAND (t, 2);
11963 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11964 return tem ? tem : expr;
11965 default:
11966 break;
11970 switch (code)
11972 case ARRAY_REF:
11974 tree op0 = TREE_OPERAND (t, 0);
11975 tree op1 = TREE_OPERAND (t, 1);
11977 if (TREE_CODE (op1) == INTEGER_CST
11978 && TREE_CODE (op0) == CONSTRUCTOR
11979 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11981 tree val = get_array_ctor_element_at_index (op0,
11982 wi::to_offset (op1));
11983 if (val)
11984 return val;
11987 return t;
11990 /* Return a VECTOR_CST if possible. */
11991 case CONSTRUCTOR:
11993 tree type = TREE_TYPE (t);
11994 if (TREE_CODE (type) != VECTOR_TYPE)
11995 return t;
11997 unsigned i;
11998 tree val;
11999 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
12000 if (! CONSTANT_CLASS_P (val))
12001 return t;
12003 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12006 case CONST_DECL:
12007 return fold (DECL_INITIAL (t));
12009 default:
12010 return t;
12011 } /* switch (code) */
12014 #ifdef ENABLE_FOLD_CHECKING
12015 #undef fold
12017 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12018 hash_table<nofree_ptr_hash<const tree_node> > *);
12019 static void fold_check_failed (const_tree, const_tree);
12020 void print_fold_checksum (const_tree);
12022 /* When --enable-checking=fold, compute a digest of expr before
12023 and after actual fold call to see if fold did not accidentally
12024 change original expr. */
12026 tree
12027 fold (tree expr)
12029 tree ret;
12030 struct md5_ctx ctx;
12031 unsigned char checksum_before[16], checksum_after[16];
12032 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12034 md5_init_ctx (&ctx);
12035 fold_checksum_tree (expr, &ctx, &ht);
12036 md5_finish_ctx (&ctx, checksum_before);
12037 ht.empty ();
12039 ret = fold_1 (expr);
12041 md5_init_ctx (&ctx);
12042 fold_checksum_tree (expr, &ctx, &ht);
12043 md5_finish_ctx (&ctx, checksum_after);
12045 if (memcmp (checksum_before, checksum_after, 16))
12046 fold_check_failed (expr, ret);
12048 return ret;
12051 void
12052 print_fold_checksum (const_tree expr)
12054 struct md5_ctx ctx;
12055 unsigned char checksum[16], cnt;
12056 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12058 md5_init_ctx (&ctx);
12059 fold_checksum_tree (expr, &ctx, &ht);
12060 md5_finish_ctx (&ctx, checksum);
12061 for (cnt = 0; cnt < 16; ++cnt)
12062 fprintf (stderr, "%02x", checksum[cnt]);
12063 putc ('\n', stderr);
12066 static void
12067 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12069 internal_error ("fold check: original tree changed by fold");
12072 static void
12073 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12074 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12076 const tree_node **slot;
12077 enum tree_code code;
12078 union tree_node buf;
12079 int i, len;
12081 recursive_label:
12082 if (expr == NULL)
12083 return;
12084 slot = ht->find_slot (expr, INSERT);
12085 if (*slot != NULL)
12086 return;
12087 *slot = expr;
12088 code = TREE_CODE (expr);
12089 if (TREE_CODE_CLASS (code) == tcc_declaration
12090 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12092 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12093 memcpy ((char *) &buf, expr, tree_size (expr));
12094 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12095 buf.decl_with_vis.symtab_node = NULL;
12096 expr = (tree) &buf;
12098 else if (TREE_CODE_CLASS (code) == tcc_type
12099 && (TYPE_POINTER_TO (expr)
12100 || TYPE_REFERENCE_TO (expr)
12101 || TYPE_CACHED_VALUES_P (expr)
12102 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12103 || TYPE_NEXT_VARIANT (expr)
12104 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12106 /* Allow these fields to be modified. */
12107 tree tmp;
12108 memcpy ((char *) &buf, expr, tree_size (expr));
12109 expr = tmp = (tree) &buf;
12110 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12111 TYPE_POINTER_TO (tmp) = NULL;
12112 TYPE_REFERENCE_TO (tmp) = NULL;
12113 TYPE_NEXT_VARIANT (tmp) = NULL;
12114 TYPE_ALIAS_SET (tmp) = -1;
12115 if (TYPE_CACHED_VALUES_P (tmp))
12117 TYPE_CACHED_VALUES_P (tmp) = 0;
12118 TYPE_CACHED_VALUES (tmp) = NULL;
12121 md5_process_bytes (expr, tree_size (expr), ctx);
12122 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12123 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12124 if (TREE_CODE_CLASS (code) != tcc_type
12125 && TREE_CODE_CLASS (code) != tcc_declaration
12126 && code != TREE_LIST
12127 && code != SSA_NAME
12128 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12129 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12130 switch (TREE_CODE_CLASS (code))
12132 case tcc_constant:
12133 switch (code)
12135 case STRING_CST:
12136 md5_process_bytes (TREE_STRING_POINTER (expr),
12137 TREE_STRING_LENGTH (expr), ctx);
12138 break;
12139 case COMPLEX_CST:
12140 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12141 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12142 break;
12143 case VECTOR_CST:
12144 len = vector_cst_encoded_nelts (expr);
12145 for (i = 0; i < len; ++i)
12146 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
12147 break;
12148 default:
12149 break;
12151 break;
12152 case tcc_exceptional:
12153 switch (code)
12155 case TREE_LIST:
12156 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12157 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12158 expr = TREE_CHAIN (expr);
12159 goto recursive_label;
12160 break;
12161 case TREE_VEC:
12162 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12163 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12164 break;
12165 default:
12166 break;
12168 break;
12169 case tcc_expression:
12170 case tcc_reference:
12171 case tcc_comparison:
12172 case tcc_unary:
12173 case tcc_binary:
12174 case tcc_statement:
12175 case tcc_vl_exp:
12176 len = TREE_OPERAND_LENGTH (expr);
12177 for (i = 0; i < len; ++i)
12178 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12179 break;
12180 case tcc_declaration:
12181 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12182 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12183 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12185 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12186 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12187 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12188 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12189 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12192 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12194 if (TREE_CODE (expr) == FUNCTION_DECL)
12196 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12197 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12199 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12201 break;
12202 case tcc_type:
12203 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12204 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12205 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12206 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12207 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12208 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12209 if (INTEGRAL_TYPE_P (expr)
12210 || SCALAR_FLOAT_TYPE_P (expr))
12212 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12213 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12215 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12216 if (TREE_CODE (expr) == RECORD_TYPE
12217 || TREE_CODE (expr) == UNION_TYPE
12218 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12219 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12220 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12221 break;
12222 default:
12223 break;
12227 /* Helper function for outputting the checksum of a tree T. When
12228 debugging with gdb, you can "define mynext" to be "next" followed
12229 by "call debug_fold_checksum (op0)", then just trace down till the
12230 outputs differ. */
12232 DEBUG_FUNCTION void
12233 debug_fold_checksum (const_tree t)
12235 int i;
12236 unsigned char checksum[16];
12237 struct md5_ctx ctx;
12238 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12240 md5_init_ctx (&ctx);
12241 fold_checksum_tree (t, &ctx, &ht);
12242 md5_finish_ctx (&ctx, checksum);
12243 ht.empty ();
12245 for (i = 0; i < 16; i++)
12246 fprintf (stderr, "%d ", checksum[i]);
12248 fprintf (stderr, "\n");
12251 #endif
12253 /* Fold a unary tree expression with code CODE of type TYPE with an
12254 operand OP0. LOC is the location of the resulting expression.
12255 Return a folded expression if successful. Otherwise, return a tree
12256 expression with code CODE of type TYPE with an operand OP0. */
12258 tree
12259 fold_build1_loc (location_t loc,
12260 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12262 tree tem;
12263 #ifdef ENABLE_FOLD_CHECKING
12264 unsigned char checksum_before[16], checksum_after[16];
12265 struct md5_ctx ctx;
12266 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12268 md5_init_ctx (&ctx);
12269 fold_checksum_tree (op0, &ctx, &ht);
12270 md5_finish_ctx (&ctx, checksum_before);
12271 ht.empty ();
12272 #endif
12274 tem = fold_unary_loc (loc, code, type, op0);
12275 if (!tem)
12276 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12278 #ifdef ENABLE_FOLD_CHECKING
12279 md5_init_ctx (&ctx);
12280 fold_checksum_tree (op0, &ctx, &ht);
12281 md5_finish_ctx (&ctx, checksum_after);
12283 if (memcmp (checksum_before, checksum_after, 16))
12284 fold_check_failed (op0, tem);
12285 #endif
12286 return tem;
12289 /* Fold a binary tree expression with code CODE of type TYPE with
12290 operands OP0 and OP1. LOC is the location of the resulting
12291 expression. Return a folded expression if successful. Otherwise,
12292 return a tree expression with code CODE of type TYPE with operands
12293 OP0 and OP1. */
12295 tree
12296 fold_build2_loc (location_t loc,
12297 enum tree_code code, tree type, tree op0, tree op1
12298 MEM_STAT_DECL)
12300 tree tem;
12301 #ifdef ENABLE_FOLD_CHECKING
12302 unsigned char checksum_before_op0[16],
12303 checksum_before_op1[16],
12304 checksum_after_op0[16],
12305 checksum_after_op1[16];
12306 struct md5_ctx ctx;
12307 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12309 md5_init_ctx (&ctx);
12310 fold_checksum_tree (op0, &ctx, &ht);
12311 md5_finish_ctx (&ctx, checksum_before_op0);
12312 ht.empty ();
12314 md5_init_ctx (&ctx);
12315 fold_checksum_tree (op1, &ctx, &ht);
12316 md5_finish_ctx (&ctx, checksum_before_op1);
12317 ht.empty ();
12318 #endif
12320 tem = fold_binary_loc (loc, code, type, op0, op1);
12321 if (!tem)
12322 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12324 #ifdef ENABLE_FOLD_CHECKING
12325 md5_init_ctx (&ctx);
12326 fold_checksum_tree (op0, &ctx, &ht);
12327 md5_finish_ctx (&ctx, checksum_after_op0);
12328 ht.empty ();
12330 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12331 fold_check_failed (op0, tem);
12333 md5_init_ctx (&ctx);
12334 fold_checksum_tree (op1, &ctx, &ht);
12335 md5_finish_ctx (&ctx, checksum_after_op1);
12337 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12338 fold_check_failed (op1, tem);
12339 #endif
12340 return tem;
12343 /* Fold a ternary tree expression with code CODE of type TYPE with
12344 operands OP0, OP1, and OP2. Return a folded expression if
12345 successful. Otherwise, return a tree expression with code CODE of
12346 type TYPE with operands OP0, OP1, and OP2. */
12348 tree
12349 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12350 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12352 tree tem;
12353 #ifdef ENABLE_FOLD_CHECKING
12354 unsigned char checksum_before_op0[16],
12355 checksum_before_op1[16],
12356 checksum_before_op2[16],
12357 checksum_after_op0[16],
12358 checksum_after_op1[16],
12359 checksum_after_op2[16];
12360 struct md5_ctx ctx;
12361 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12363 md5_init_ctx (&ctx);
12364 fold_checksum_tree (op0, &ctx, &ht);
12365 md5_finish_ctx (&ctx, checksum_before_op0);
12366 ht.empty ();
12368 md5_init_ctx (&ctx);
12369 fold_checksum_tree (op1, &ctx, &ht);
12370 md5_finish_ctx (&ctx, checksum_before_op1);
12371 ht.empty ();
12373 md5_init_ctx (&ctx);
12374 fold_checksum_tree (op2, &ctx, &ht);
12375 md5_finish_ctx (&ctx, checksum_before_op2);
12376 ht.empty ();
12377 #endif
12379 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12380 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12381 if (!tem)
12382 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12384 #ifdef ENABLE_FOLD_CHECKING
12385 md5_init_ctx (&ctx);
12386 fold_checksum_tree (op0, &ctx, &ht);
12387 md5_finish_ctx (&ctx, checksum_after_op0);
12388 ht.empty ();
12390 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12391 fold_check_failed (op0, tem);
12393 md5_init_ctx (&ctx);
12394 fold_checksum_tree (op1, &ctx, &ht);
12395 md5_finish_ctx (&ctx, checksum_after_op1);
12396 ht.empty ();
12398 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12399 fold_check_failed (op1, tem);
12401 md5_init_ctx (&ctx);
12402 fold_checksum_tree (op2, &ctx, &ht);
12403 md5_finish_ctx (&ctx, checksum_after_op2);
12405 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12406 fold_check_failed (op2, tem);
12407 #endif
12408 return tem;
12411 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12412 arguments in ARGARRAY, and a null static chain.
12413 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12414 of type TYPE from the given operands as constructed by build_call_array. */
12416 tree
12417 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12418 int nargs, tree *argarray)
12420 tree tem;
12421 #ifdef ENABLE_FOLD_CHECKING
12422 unsigned char checksum_before_fn[16],
12423 checksum_before_arglist[16],
12424 checksum_after_fn[16],
12425 checksum_after_arglist[16];
12426 struct md5_ctx ctx;
12427 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12428 int i;
12430 md5_init_ctx (&ctx);
12431 fold_checksum_tree (fn, &ctx, &ht);
12432 md5_finish_ctx (&ctx, checksum_before_fn);
12433 ht.empty ();
12435 md5_init_ctx (&ctx);
12436 for (i = 0; i < nargs; i++)
12437 fold_checksum_tree (argarray[i], &ctx, &ht);
12438 md5_finish_ctx (&ctx, checksum_before_arglist);
12439 ht.empty ();
12440 #endif
12442 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12443 if (!tem)
12444 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12446 #ifdef ENABLE_FOLD_CHECKING
12447 md5_init_ctx (&ctx);
12448 fold_checksum_tree (fn, &ctx, &ht);
12449 md5_finish_ctx (&ctx, checksum_after_fn);
12450 ht.empty ();
12452 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12453 fold_check_failed (fn, tem);
12455 md5_init_ctx (&ctx);
12456 for (i = 0; i < nargs; i++)
12457 fold_checksum_tree (argarray[i], &ctx, &ht);
12458 md5_finish_ctx (&ctx, checksum_after_arglist);
12460 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12461 fold_check_failed (NULL_TREE, tem);
12462 #endif
12463 return tem;
12466 /* Perform constant folding and related simplification of initializer
12467 expression EXPR. These behave identically to "fold_buildN" but ignore
12468 potential run-time traps and exceptions that fold must preserve. */
12470 #define START_FOLD_INIT \
12471 int saved_signaling_nans = flag_signaling_nans;\
12472 int saved_trapping_math = flag_trapping_math;\
12473 int saved_rounding_math = flag_rounding_math;\
12474 int saved_trapv = flag_trapv;\
12475 int saved_folding_initializer = folding_initializer;\
12476 flag_signaling_nans = 0;\
12477 flag_trapping_math = 0;\
12478 flag_rounding_math = 0;\
12479 flag_trapv = 0;\
12480 folding_initializer = 1;
12482 #define END_FOLD_INIT \
12483 flag_signaling_nans = saved_signaling_nans;\
12484 flag_trapping_math = saved_trapping_math;\
12485 flag_rounding_math = saved_rounding_math;\
12486 flag_trapv = saved_trapv;\
12487 folding_initializer = saved_folding_initializer;
12489 tree
12490 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12491 tree type, tree op)
12493 tree result;
12494 START_FOLD_INIT;
12496 result = fold_build1_loc (loc, code, type, op);
12498 END_FOLD_INIT;
12499 return result;
12502 tree
12503 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12504 tree type, tree op0, tree op1)
12506 tree result;
12507 START_FOLD_INIT;
12509 result = fold_build2_loc (loc, code, type, op0, op1);
12511 END_FOLD_INIT;
12512 return result;
12515 tree
12516 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12517 int nargs, tree *argarray)
12519 tree result;
12520 START_FOLD_INIT;
12522 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12524 END_FOLD_INIT;
12525 return result;
12528 #undef START_FOLD_INIT
12529 #undef END_FOLD_INIT
12531 /* Determine if first argument is a multiple of second argument. Return 0 if
12532 it is not, or we cannot easily determined it to be.
12534 An example of the sort of thing we care about (at this point; this routine
12535 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12536 fold cases do now) is discovering that
12538 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12540 is a multiple of
12542 SAVE_EXPR (J * 8)
12544 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12546 This code also handles discovering that
12548 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12550 is a multiple of 8 so we don't have to worry about dealing with a
12551 possible remainder.
12553 Note that we *look* inside a SAVE_EXPR only to determine how it was
12554 calculated; it is not safe for fold to do much of anything else with the
12555 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12556 at run time. For example, the latter example above *cannot* be implemented
12557 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12558 evaluation time of the original SAVE_EXPR is not necessarily the same at
12559 the time the new expression is evaluated. The only optimization of this
12560 sort that would be valid is changing
12562 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12564 divided by 8 to
12566 SAVE_EXPR (I) * SAVE_EXPR (J)
12568 (where the same SAVE_EXPR (J) is used in the original and the
12569 transformed version). */
12572 multiple_of_p (tree type, const_tree top, const_tree bottom)
12574 gimple *stmt;
12575 tree t1, op1, op2;
12577 if (operand_equal_p (top, bottom, 0))
12578 return 1;
12580 if (TREE_CODE (type) != INTEGER_TYPE)
12581 return 0;
12583 switch (TREE_CODE (top))
12585 case BIT_AND_EXPR:
12586 /* Bitwise and provides a power of two multiple. If the mask is
12587 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12588 if (!integer_pow2p (bottom))
12589 return 0;
12590 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12591 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12593 case MULT_EXPR:
12594 if (TREE_CODE (bottom) == INTEGER_CST)
12596 op1 = TREE_OPERAND (top, 0);
12597 op2 = TREE_OPERAND (top, 1);
12598 if (TREE_CODE (op1) == INTEGER_CST)
12599 std::swap (op1, op2);
12600 if (TREE_CODE (op2) == INTEGER_CST)
12602 if (multiple_of_p (type, op2, bottom))
12603 return 1;
12604 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
12605 if (multiple_of_p (type, bottom, op2))
12607 widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
12608 wi::to_widest (op2));
12609 if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
12611 op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
12612 return multiple_of_p (type, op1, op2);
12615 return multiple_of_p (type, op1, bottom);
12618 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12619 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12621 case MINUS_EXPR:
12622 /* It is impossible to prove if op0 - op1 is multiple of bottom
12623 precisely, so be conservative here checking if both op0 and op1
12624 are multiple of bottom. Note we check the second operand first
12625 since it's usually simpler. */
12626 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12627 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12629 case PLUS_EXPR:
12630 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12631 as op0 - 3 if the expression has unsigned type. For example,
12632 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12633 op1 = TREE_OPERAND (top, 1);
12634 if (TYPE_UNSIGNED (type)
12635 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12636 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12637 return (multiple_of_p (type, op1, bottom)
12638 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12640 case LSHIFT_EXPR:
12641 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12643 op1 = TREE_OPERAND (top, 1);
12644 /* const_binop may not detect overflow correctly,
12645 so check for it explicitly here. */
12646 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
12647 wi::to_wide (op1))
12648 && (t1 = fold_convert (type,
12649 const_binop (LSHIFT_EXPR, size_one_node,
12650 op1))) != 0
12651 && !TREE_OVERFLOW (t1))
12652 return multiple_of_p (type, t1, bottom);
12654 return 0;
12656 case NOP_EXPR:
12657 /* Can't handle conversions from non-integral or wider integral type. */
12658 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12659 || (TYPE_PRECISION (type)
12660 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12661 return 0;
12663 /* fall through */
12665 case SAVE_EXPR:
12666 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12668 case COND_EXPR:
12669 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12670 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12672 case INTEGER_CST:
12673 if (TREE_CODE (bottom) != INTEGER_CST
12674 || integer_zerop (bottom)
12675 || (TYPE_UNSIGNED (type)
12676 && (tree_int_cst_sgn (top) < 0
12677 || tree_int_cst_sgn (bottom) < 0)))
12678 return 0;
12679 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12680 SIGNED);
12682 case SSA_NAME:
12683 if (TREE_CODE (bottom) == INTEGER_CST
12684 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12685 && gimple_code (stmt) == GIMPLE_ASSIGN)
12687 enum tree_code code = gimple_assign_rhs_code (stmt);
12689 /* Check for special cases to see if top is defined as multiple
12690 of bottom:
12692 top = (X & ~(bottom - 1) ; bottom is power of 2
12696 Y = X % bottom
12697 top = X - Y. */
12698 if (code == BIT_AND_EXPR
12699 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12700 && TREE_CODE (op2) == INTEGER_CST
12701 && integer_pow2p (bottom)
12702 && wi::multiple_of_p (wi::to_widest (op2),
12703 wi::to_widest (bottom), UNSIGNED))
12704 return 1;
12706 op1 = gimple_assign_rhs1 (stmt);
12707 if (code == MINUS_EXPR
12708 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12709 && TREE_CODE (op2) == SSA_NAME
12710 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12711 && gimple_code (stmt) == GIMPLE_ASSIGN
12712 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12713 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12714 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12715 return 1;
12718 /* fall through */
12720 default:
12721 if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
12722 return multiple_p (wi::to_poly_widest (top),
12723 wi::to_poly_widest (bottom));
12725 return 0;
12729 #define tree_expr_nonnegative_warnv_p(X, Y) \
12730 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12732 #define RECURSE(X) \
12733 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12735 /* Return true if CODE or TYPE is known to be non-negative. */
12737 static bool
12738 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12740 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12741 && truth_value_p (code))
12742 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12743 have a signed:1 type (where the value is -1 and 0). */
12744 return true;
12745 return false;
12748 /* Return true if (CODE OP0) is known to be non-negative. If the return
12749 value is based on the assumption that signed overflow is undefined,
12750 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12751 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12753 bool
12754 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12755 bool *strict_overflow_p, int depth)
12757 if (TYPE_UNSIGNED (type))
12758 return true;
12760 switch (code)
12762 case ABS_EXPR:
12763 /* We can't return 1 if flag_wrapv is set because
12764 ABS_EXPR<INT_MIN> = INT_MIN. */
12765 if (!ANY_INTEGRAL_TYPE_P (type))
12766 return true;
12767 if (TYPE_OVERFLOW_UNDEFINED (type))
12769 *strict_overflow_p = true;
12770 return true;
12772 break;
12774 case NON_LVALUE_EXPR:
12775 case FLOAT_EXPR:
12776 case FIX_TRUNC_EXPR:
12777 return RECURSE (op0);
12779 CASE_CONVERT:
12781 tree inner_type = TREE_TYPE (op0);
12782 tree outer_type = type;
12784 if (TREE_CODE (outer_type) == REAL_TYPE)
12786 if (TREE_CODE (inner_type) == REAL_TYPE)
12787 return RECURSE (op0);
12788 if (INTEGRAL_TYPE_P (inner_type))
12790 if (TYPE_UNSIGNED (inner_type))
12791 return true;
12792 return RECURSE (op0);
12795 else if (INTEGRAL_TYPE_P (outer_type))
12797 if (TREE_CODE (inner_type) == REAL_TYPE)
12798 return RECURSE (op0);
12799 if (INTEGRAL_TYPE_P (inner_type))
12800 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12801 && TYPE_UNSIGNED (inner_type);
12804 break;
12806 default:
12807 return tree_simple_nonnegative_warnv_p (code, type);
12810 /* We don't know sign of `t', so be conservative and return false. */
12811 return false;
12814 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12815 value is based on the assumption that signed overflow is undefined,
12816 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12817 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12819 bool
12820 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12821 tree op1, bool *strict_overflow_p,
12822 int depth)
12824 if (TYPE_UNSIGNED (type))
12825 return true;
12827 switch (code)
12829 case POINTER_PLUS_EXPR:
12830 case PLUS_EXPR:
12831 if (FLOAT_TYPE_P (type))
12832 return RECURSE (op0) && RECURSE (op1);
12834 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12835 both unsigned and at least 2 bits shorter than the result. */
12836 if (TREE_CODE (type) == INTEGER_TYPE
12837 && TREE_CODE (op0) == NOP_EXPR
12838 && TREE_CODE (op1) == NOP_EXPR)
12840 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12841 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12842 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12843 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12845 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12846 TYPE_PRECISION (inner2)) + 1;
12847 return prec < TYPE_PRECISION (type);
12850 break;
12852 case MULT_EXPR:
12853 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12855 /* x * x is always non-negative for floating point x
12856 or without overflow. */
12857 if (operand_equal_p (op0, op1, 0)
12858 || (RECURSE (op0) && RECURSE (op1)))
12860 if (ANY_INTEGRAL_TYPE_P (type)
12861 && TYPE_OVERFLOW_UNDEFINED (type))
12862 *strict_overflow_p = true;
12863 return true;
12867 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12868 both unsigned and their total bits is shorter than the result. */
12869 if (TREE_CODE (type) == INTEGER_TYPE
12870 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12871 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12873 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12874 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12875 : TREE_TYPE (op0);
12876 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12877 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12878 : TREE_TYPE (op1);
12880 bool unsigned0 = TYPE_UNSIGNED (inner0);
12881 bool unsigned1 = TYPE_UNSIGNED (inner1);
12883 if (TREE_CODE (op0) == INTEGER_CST)
12884 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12886 if (TREE_CODE (op1) == INTEGER_CST)
12887 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12889 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12890 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12892 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12893 ? tree_int_cst_min_precision (op0, UNSIGNED)
12894 : TYPE_PRECISION (inner0);
12896 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12897 ? tree_int_cst_min_precision (op1, UNSIGNED)
12898 : TYPE_PRECISION (inner1);
12900 return precision0 + precision1 < TYPE_PRECISION (type);
12903 return false;
12905 case BIT_AND_EXPR:
12906 case MAX_EXPR:
12907 return RECURSE (op0) || RECURSE (op1);
12909 case BIT_IOR_EXPR:
12910 case BIT_XOR_EXPR:
12911 case MIN_EXPR:
12912 case RDIV_EXPR:
12913 case TRUNC_DIV_EXPR:
12914 case CEIL_DIV_EXPR:
12915 case FLOOR_DIV_EXPR:
12916 case ROUND_DIV_EXPR:
12917 return RECURSE (op0) && RECURSE (op1);
12919 case TRUNC_MOD_EXPR:
12920 return RECURSE (op0);
12922 case FLOOR_MOD_EXPR:
12923 return RECURSE (op1);
12925 case CEIL_MOD_EXPR:
12926 case ROUND_MOD_EXPR:
12927 default:
12928 return tree_simple_nonnegative_warnv_p (code, type);
12931 /* We don't know sign of `t', so be conservative and return false. */
12932 return false;
12935 /* Return true if T is known to be non-negative. If the return
12936 value is based on the assumption that signed overflow is undefined,
12937 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12938 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12940 bool
12941 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12943 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12944 return true;
12946 switch (TREE_CODE (t))
12948 case INTEGER_CST:
12949 return tree_int_cst_sgn (t) >= 0;
12951 case REAL_CST:
12952 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12954 case FIXED_CST:
12955 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12957 case COND_EXPR:
12958 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12960 case SSA_NAME:
12961 /* Limit the depth of recursion to avoid quadratic behavior.
12962 This is expected to catch almost all occurrences in practice.
12963 If this code misses important cases that unbounded recursion
12964 would not, passes that need this information could be revised
12965 to provide it through dataflow propagation. */
12966 return (!name_registered_for_update_p (t)
12967 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12968 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12969 strict_overflow_p, depth));
12971 default:
12972 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12976 /* Return true if T is known to be non-negative. If the return
12977 value is based on the assumption that signed overflow is undefined,
12978 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12979 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12981 bool
12982 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12983 bool *strict_overflow_p, int depth)
12985 switch (fn)
12987 CASE_CFN_ACOS:
12988 CASE_CFN_ACOSH:
12989 CASE_CFN_CABS:
12990 CASE_CFN_COSH:
12991 CASE_CFN_ERFC:
12992 CASE_CFN_EXP:
12993 CASE_CFN_EXP10:
12994 CASE_CFN_EXP2:
12995 CASE_CFN_FABS:
12996 CASE_CFN_FDIM:
12997 CASE_CFN_HYPOT:
12998 CASE_CFN_POW10:
12999 CASE_CFN_FFS:
13000 CASE_CFN_PARITY:
13001 CASE_CFN_POPCOUNT:
13002 CASE_CFN_CLZ:
13003 CASE_CFN_CLRSB:
13004 case CFN_BUILT_IN_BSWAP32:
13005 case CFN_BUILT_IN_BSWAP64:
13006 /* Always true. */
13007 return true;
13009 CASE_CFN_SQRT:
13010 CASE_CFN_SQRT_FN:
13011 /* sqrt(-0.0) is -0.0. */
13012 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13013 return true;
13014 return RECURSE (arg0);
13016 CASE_CFN_ASINH:
13017 CASE_CFN_ATAN:
13018 CASE_CFN_ATANH:
13019 CASE_CFN_CBRT:
13020 CASE_CFN_CEIL:
13021 CASE_CFN_CEIL_FN:
13022 CASE_CFN_ERF:
13023 CASE_CFN_EXPM1:
13024 CASE_CFN_FLOOR:
13025 CASE_CFN_FLOOR_FN:
13026 CASE_CFN_FMOD:
13027 CASE_CFN_FREXP:
13028 CASE_CFN_ICEIL:
13029 CASE_CFN_IFLOOR:
13030 CASE_CFN_IRINT:
13031 CASE_CFN_IROUND:
13032 CASE_CFN_LCEIL:
13033 CASE_CFN_LDEXP:
13034 CASE_CFN_LFLOOR:
13035 CASE_CFN_LLCEIL:
13036 CASE_CFN_LLFLOOR:
13037 CASE_CFN_LLRINT:
13038 CASE_CFN_LLROUND:
13039 CASE_CFN_LRINT:
13040 CASE_CFN_LROUND:
13041 CASE_CFN_MODF:
13042 CASE_CFN_NEARBYINT:
13043 CASE_CFN_NEARBYINT_FN:
13044 CASE_CFN_RINT:
13045 CASE_CFN_RINT_FN:
13046 CASE_CFN_ROUND:
13047 CASE_CFN_ROUND_FN:
13048 CASE_CFN_SCALB:
13049 CASE_CFN_SCALBLN:
13050 CASE_CFN_SCALBN:
13051 CASE_CFN_SIGNBIT:
13052 CASE_CFN_SIGNIFICAND:
13053 CASE_CFN_SINH:
13054 CASE_CFN_TANH:
13055 CASE_CFN_TRUNC:
13056 CASE_CFN_TRUNC_FN:
13057 /* True if the 1st argument is nonnegative. */
13058 return RECURSE (arg0);
13060 CASE_CFN_FMAX:
13061 CASE_CFN_FMAX_FN:
13062 /* True if the 1st OR 2nd arguments are nonnegative. */
13063 return RECURSE (arg0) || RECURSE (arg1);
13065 CASE_CFN_FMIN:
13066 CASE_CFN_FMIN_FN:
13067 /* True if the 1st AND 2nd arguments are nonnegative. */
13068 return RECURSE (arg0) && RECURSE (arg1);
13070 CASE_CFN_COPYSIGN:
13071 CASE_CFN_COPYSIGN_FN:
13072 /* True if the 2nd argument is nonnegative. */
13073 return RECURSE (arg1);
13075 CASE_CFN_POWI:
13076 /* True if the 1st argument is nonnegative or the second
13077 argument is an even integer. */
13078 if (TREE_CODE (arg1) == INTEGER_CST
13079 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13080 return true;
13081 return RECURSE (arg0);
13083 CASE_CFN_POW:
13084 /* True if the 1st argument is nonnegative or the second
13085 argument is an even integer valued real. */
13086 if (TREE_CODE (arg1) == REAL_CST)
13088 REAL_VALUE_TYPE c;
13089 HOST_WIDE_INT n;
13091 c = TREE_REAL_CST (arg1);
13092 n = real_to_integer (&c);
13093 if ((n & 1) == 0)
13095 REAL_VALUE_TYPE cint;
13096 real_from_integer (&cint, VOIDmode, n, SIGNED);
13097 if (real_identical (&c, &cint))
13098 return true;
13101 return RECURSE (arg0);
13103 default:
13104 break;
13106 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13109 /* Return true if T is known to be non-negative. If the return
13110 value is based on the assumption that signed overflow is undefined,
13111 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13112 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13114 static bool
13115 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13117 enum tree_code code = TREE_CODE (t);
13118 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13119 return true;
13121 switch (code)
13123 case TARGET_EXPR:
13125 tree temp = TARGET_EXPR_SLOT (t);
13126 t = TARGET_EXPR_INITIAL (t);
13128 /* If the initializer is non-void, then it's a normal expression
13129 that will be assigned to the slot. */
13130 if (!VOID_TYPE_P (t))
13131 return RECURSE (t);
13133 /* Otherwise, the initializer sets the slot in some way. One common
13134 way is an assignment statement at the end of the initializer. */
13135 while (1)
13137 if (TREE_CODE (t) == BIND_EXPR)
13138 t = expr_last (BIND_EXPR_BODY (t));
13139 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13140 || TREE_CODE (t) == TRY_CATCH_EXPR)
13141 t = expr_last (TREE_OPERAND (t, 0));
13142 else if (TREE_CODE (t) == STATEMENT_LIST)
13143 t = expr_last (t);
13144 else
13145 break;
13147 if (TREE_CODE (t) == MODIFY_EXPR
13148 && TREE_OPERAND (t, 0) == temp)
13149 return RECURSE (TREE_OPERAND (t, 1));
13151 return false;
13154 case CALL_EXPR:
13156 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13157 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13159 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13160 get_call_combined_fn (t),
13161 arg0,
13162 arg1,
13163 strict_overflow_p, depth);
13165 case COMPOUND_EXPR:
13166 case MODIFY_EXPR:
13167 return RECURSE (TREE_OPERAND (t, 1));
13169 case BIND_EXPR:
13170 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13172 case SAVE_EXPR:
13173 return RECURSE (TREE_OPERAND (t, 0));
13175 default:
13176 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13180 #undef RECURSE
13181 #undef tree_expr_nonnegative_warnv_p
13183 /* Return true if T is known to be non-negative. If the return
13184 value is based on the assumption that signed overflow is undefined,
13185 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13186 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13188 bool
13189 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13191 enum tree_code code;
13192 if (t == error_mark_node)
13193 return false;
13195 code = TREE_CODE (t);
13196 switch (TREE_CODE_CLASS (code))
13198 case tcc_binary:
13199 case tcc_comparison:
13200 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13201 TREE_TYPE (t),
13202 TREE_OPERAND (t, 0),
13203 TREE_OPERAND (t, 1),
13204 strict_overflow_p, depth);
13206 case tcc_unary:
13207 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13208 TREE_TYPE (t),
13209 TREE_OPERAND (t, 0),
13210 strict_overflow_p, depth);
13212 case tcc_constant:
13213 case tcc_declaration:
13214 case tcc_reference:
13215 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13217 default:
13218 break;
13221 switch (code)
13223 case TRUTH_AND_EXPR:
13224 case TRUTH_OR_EXPR:
13225 case TRUTH_XOR_EXPR:
13226 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13227 TREE_TYPE (t),
13228 TREE_OPERAND (t, 0),
13229 TREE_OPERAND (t, 1),
13230 strict_overflow_p, depth);
13231 case TRUTH_NOT_EXPR:
13232 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13233 TREE_TYPE (t),
13234 TREE_OPERAND (t, 0),
13235 strict_overflow_p, depth);
13237 case COND_EXPR:
13238 case CONSTRUCTOR:
13239 case OBJ_TYPE_REF:
13240 case ASSERT_EXPR:
13241 case ADDR_EXPR:
13242 case WITH_SIZE_EXPR:
13243 case SSA_NAME:
13244 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13246 default:
13247 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13251 /* Return true if `t' is known to be non-negative. Handle warnings
13252 about undefined signed overflow. */
13254 bool
13255 tree_expr_nonnegative_p (tree t)
13257 bool ret, strict_overflow_p;
13259 strict_overflow_p = false;
13260 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13261 if (strict_overflow_p)
13262 fold_overflow_warning (("assuming signed overflow does not occur when "
13263 "determining that expression is always "
13264 "non-negative"),
13265 WARN_STRICT_OVERFLOW_MISC);
13266 return ret;
13270 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13271 For floating point we further ensure that T is not denormal.
13272 Similar logic is present in nonzero_address in rtlanal.h.
13274 If the return value is based on the assumption that signed overflow
13275 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13276 change *STRICT_OVERFLOW_P. */
13278 bool
13279 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13280 bool *strict_overflow_p)
13282 switch (code)
13284 case ABS_EXPR:
13285 return tree_expr_nonzero_warnv_p (op0,
13286 strict_overflow_p);
13288 case NOP_EXPR:
13290 tree inner_type = TREE_TYPE (op0);
13291 tree outer_type = type;
13293 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13294 && tree_expr_nonzero_warnv_p (op0,
13295 strict_overflow_p));
13297 break;
13299 case NON_LVALUE_EXPR:
13300 return tree_expr_nonzero_warnv_p (op0,
13301 strict_overflow_p);
13303 default:
13304 break;
13307 return false;
13310 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13311 For floating point we further ensure that T is not denormal.
13312 Similar logic is present in nonzero_address in rtlanal.h.
13314 If the return value is based on the assumption that signed overflow
13315 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13316 change *STRICT_OVERFLOW_P. */
13318 bool
13319 tree_binary_nonzero_warnv_p (enum tree_code code,
13320 tree type,
13321 tree op0,
13322 tree op1, bool *strict_overflow_p)
13324 bool sub_strict_overflow_p;
13325 switch (code)
13327 case POINTER_PLUS_EXPR:
13328 case PLUS_EXPR:
13329 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13331 /* With the presence of negative values it is hard
13332 to say something. */
13333 sub_strict_overflow_p = false;
13334 if (!tree_expr_nonnegative_warnv_p (op0,
13335 &sub_strict_overflow_p)
13336 || !tree_expr_nonnegative_warnv_p (op1,
13337 &sub_strict_overflow_p))
13338 return false;
13339 /* One of operands must be positive and the other non-negative. */
13340 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13341 overflows, on a twos-complement machine the sum of two
13342 nonnegative numbers can never be zero. */
13343 return (tree_expr_nonzero_warnv_p (op0,
13344 strict_overflow_p)
13345 || tree_expr_nonzero_warnv_p (op1,
13346 strict_overflow_p));
13348 break;
13350 case MULT_EXPR:
13351 if (TYPE_OVERFLOW_UNDEFINED (type))
13353 if (tree_expr_nonzero_warnv_p (op0,
13354 strict_overflow_p)
13355 && tree_expr_nonzero_warnv_p (op1,
13356 strict_overflow_p))
13358 *strict_overflow_p = true;
13359 return true;
13362 break;
13364 case MIN_EXPR:
13365 sub_strict_overflow_p = false;
13366 if (tree_expr_nonzero_warnv_p (op0,
13367 &sub_strict_overflow_p)
13368 && tree_expr_nonzero_warnv_p (op1,
13369 &sub_strict_overflow_p))
13371 if (sub_strict_overflow_p)
13372 *strict_overflow_p = true;
13374 break;
13376 case MAX_EXPR:
13377 sub_strict_overflow_p = false;
13378 if (tree_expr_nonzero_warnv_p (op0,
13379 &sub_strict_overflow_p))
13381 if (sub_strict_overflow_p)
13382 *strict_overflow_p = true;
13384 /* When both operands are nonzero, then MAX must be too. */
13385 if (tree_expr_nonzero_warnv_p (op1,
13386 strict_overflow_p))
13387 return true;
13389 /* MAX where operand 0 is positive is positive. */
13390 return tree_expr_nonnegative_warnv_p (op0,
13391 strict_overflow_p);
13393 /* MAX where operand 1 is positive is positive. */
13394 else if (tree_expr_nonzero_warnv_p (op1,
13395 &sub_strict_overflow_p)
13396 && tree_expr_nonnegative_warnv_p (op1,
13397 &sub_strict_overflow_p))
13399 if (sub_strict_overflow_p)
13400 *strict_overflow_p = true;
13401 return true;
13403 break;
13405 case BIT_IOR_EXPR:
13406 return (tree_expr_nonzero_warnv_p (op1,
13407 strict_overflow_p)
13408 || tree_expr_nonzero_warnv_p (op0,
13409 strict_overflow_p));
13411 default:
13412 break;
13415 return false;
13418 /* Return true when T is an address and is known to be nonzero.
13419 For floating point we further ensure that T is not denormal.
13420 Similar logic is present in nonzero_address in rtlanal.h.
13422 If the return value is based on the assumption that signed overflow
13423 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13424 change *STRICT_OVERFLOW_P. */
13426 bool
13427 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13429 bool sub_strict_overflow_p;
13430 switch (TREE_CODE (t))
13432 case INTEGER_CST:
13433 return !integer_zerop (t);
13435 case ADDR_EXPR:
13437 tree base = TREE_OPERAND (t, 0);
13439 if (!DECL_P (base))
13440 base = get_base_address (base);
13442 if (base && TREE_CODE (base) == TARGET_EXPR)
13443 base = TARGET_EXPR_SLOT (base);
13445 if (!base)
13446 return false;
13448 /* For objects in symbol table check if we know they are non-zero.
13449 Don't do anything for variables and functions before symtab is built;
13450 it is quite possible that they will be declared weak later. */
13451 int nonzero_addr = maybe_nonzero_address (base);
13452 if (nonzero_addr >= 0)
13453 return nonzero_addr;
13455 /* Constants are never weak. */
13456 if (CONSTANT_CLASS_P (base))
13457 return true;
13459 return false;
13462 case COND_EXPR:
13463 sub_strict_overflow_p = false;
13464 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13465 &sub_strict_overflow_p)
13466 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13467 &sub_strict_overflow_p))
13469 if (sub_strict_overflow_p)
13470 *strict_overflow_p = true;
13471 return true;
13473 break;
13475 case SSA_NAME:
13476 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13477 break;
13478 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13480 default:
13481 break;
13483 return false;
13486 #define integer_valued_real_p(X) \
13487 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13489 #define RECURSE(X) \
13490 ((integer_valued_real_p) (X, depth + 1))
13492 /* Return true if the floating point result of (CODE OP0) has an
13493 integer value. We also allow +Inf, -Inf and NaN to be considered
13494 integer values. Return false for signaling NaN.
13496 DEPTH is the current nesting depth of the query. */
13498 bool
13499 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13501 switch (code)
13503 case FLOAT_EXPR:
13504 return true;
13506 case ABS_EXPR:
13507 return RECURSE (op0);
13509 CASE_CONVERT:
13511 tree type = TREE_TYPE (op0);
13512 if (TREE_CODE (type) == INTEGER_TYPE)
13513 return true;
13514 if (TREE_CODE (type) == REAL_TYPE)
13515 return RECURSE (op0);
13516 break;
13519 default:
13520 break;
13522 return false;
13525 /* Return true if the floating point result of (CODE OP0 OP1) has an
13526 integer value. We also allow +Inf, -Inf and NaN to be considered
13527 integer values. Return false for signaling NaN.
13529 DEPTH is the current nesting depth of the query. */
13531 bool
13532 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13534 switch (code)
13536 case PLUS_EXPR:
13537 case MINUS_EXPR:
13538 case MULT_EXPR:
13539 case MIN_EXPR:
13540 case MAX_EXPR:
13541 return RECURSE (op0) && RECURSE (op1);
13543 default:
13544 break;
13546 return false;
13549 /* Return true if the floating point result of calling FNDECL with arguments
13550 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13551 considered integer values. Return false for signaling NaN. If FNDECL
13552 takes fewer than 2 arguments, the remaining ARGn are null.
13554 DEPTH is the current nesting depth of the query. */
13556 bool
13557 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13559 switch (fn)
13561 CASE_CFN_CEIL:
13562 CASE_CFN_CEIL_FN:
13563 CASE_CFN_FLOOR:
13564 CASE_CFN_FLOOR_FN:
13565 CASE_CFN_NEARBYINT:
13566 CASE_CFN_NEARBYINT_FN:
13567 CASE_CFN_RINT:
13568 CASE_CFN_RINT_FN:
13569 CASE_CFN_ROUND:
13570 CASE_CFN_ROUND_FN:
13571 CASE_CFN_TRUNC:
13572 CASE_CFN_TRUNC_FN:
13573 return true;
13575 CASE_CFN_FMIN:
13576 CASE_CFN_FMIN_FN:
13577 CASE_CFN_FMAX:
13578 CASE_CFN_FMAX_FN:
13579 return RECURSE (arg0) && RECURSE (arg1);
13581 default:
13582 break;
13584 return false;
13587 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13588 has an integer value. We also allow +Inf, -Inf and NaN to be
13589 considered integer values. Return false for signaling NaN.
13591 DEPTH is the current nesting depth of the query. */
13593 bool
13594 integer_valued_real_single_p (tree t, int depth)
13596 switch (TREE_CODE (t))
13598 case REAL_CST:
13599 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13601 case COND_EXPR:
13602 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13604 case SSA_NAME:
13605 /* Limit the depth of recursion to avoid quadratic behavior.
13606 This is expected to catch almost all occurrences in practice.
13607 If this code misses important cases that unbounded recursion
13608 would not, passes that need this information could be revised
13609 to provide it through dataflow propagation. */
13610 return (!name_registered_for_update_p (t)
13611 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13612 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13613 depth));
13615 default:
13616 break;
13618 return false;
13621 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13622 has an integer value. We also allow +Inf, -Inf and NaN to be
13623 considered integer values. Return false for signaling NaN.
13625 DEPTH is the current nesting depth of the query. */
13627 static bool
13628 integer_valued_real_invalid_p (tree t, int depth)
13630 switch (TREE_CODE (t))
13632 case COMPOUND_EXPR:
13633 case MODIFY_EXPR:
13634 case BIND_EXPR:
13635 return RECURSE (TREE_OPERAND (t, 1));
13637 case SAVE_EXPR:
13638 return RECURSE (TREE_OPERAND (t, 0));
13640 default:
13641 break;
13643 return false;
13646 #undef RECURSE
13647 #undef integer_valued_real_p
13649 /* Return true if the floating point expression T has an integer value.
13650 We also allow +Inf, -Inf and NaN to be considered integer values.
13651 Return false for signaling NaN.
13653 DEPTH is the current nesting depth of the query. */
13655 bool
13656 integer_valued_real_p (tree t, int depth)
13658 if (t == error_mark_node)
13659 return false;
13661 tree_code code = TREE_CODE (t);
13662 switch (TREE_CODE_CLASS (code))
13664 case tcc_binary:
13665 case tcc_comparison:
13666 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13667 TREE_OPERAND (t, 1), depth);
13669 case tcc_unary:
13670 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13672 case tcc_constant:
13673 case tcc_declaration:
13674 case tcc_reference:
13675 return integer_valued_real_single_p (t, depth);
13677 default:
13678 break;
13681 switch (code)
13683 case COND_EXPR:
13684 case SSA_NAME:
13685 return integer_valued_real_single_p (t, depth);
13687 case CALL_EXPR:
13689 tree arg0 = (call_expr_nargs (t) > 0
13690 ? CALL_EXPR_ARG (t, 0)
13691 : NULL_TREE);
13692 tree arg1 = (call_expr_nargs (t) > 1
13693 ? CALL_EXPR_ARG (t, 1)
13694 : NULL_TREE);
13695 return integer_valued_real_call_p (get_call_combined_fn (t),
13696 arg0, arg1, depth);
13699 default:
13700 return integer_valued_real_invalid_p (t, depth);
13704 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13705 attempt to fold the expression to a constant without modifying TYPE,
13706 OP0 or OP1.
13708 If the expression could be simplified to a constant, then return
13709 the constant. If the expression would not be simplified to a
13710 constant, then return NULL_TREE. */
13712 tree
13713 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13715 tree tem = fold_binary (code, type, op0, op1);
13716 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13719 /* Given the components of a unary expression CODE, TYPE and OP0,
13720 attempt to fold the expression to a constant without modifying
13721 TYPE or OP0.
13723 If the expression could be simplified to a constant, then return
13724 the constant. If the expression would not be simplified to a
13725 constant, then return NULL_TREE. */
13727 tree
13728 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13730 tree tem = fold_unary (code, type, op0);
13731 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13734 /* If EXP represents referencing an element in a constant string
13735 (either via pointer arithmetic or array indexing), return the
13736 tree representing the value accessed, otherwise return NULL. */
13738 tree
13739 fold_read_from_constant_string (tree exp)
13741 if ((TREE_CODE (exp) == INDIRECT_REF
13742 || TREE_CODE (exp) == ARRAY_REF)
13743 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13745 tree exp1 = TREE_OPERAND (exp, 0);
13746 tree index;
13747 tree string;
13748 location_t loc = EXPR_LOCATION (exp);
13750 if (TREE_CODE (exp) == INDIRECT_REF)
13751 string = string_constant (exp1, &index);
13752 else
13754 tree low_bound = array_ref_low_bound (exp);
13755 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13757 /* Optimize the special-case of a zero lower bound.
13759 We convert the low_bound to sizetype to avoid some problems
13760 with constant folding. (E.g. suppose the lower bound is 1,
13761 and its mode is QI. Without the conversion,l (ARRAY
13762 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13763 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13764 if (! integer_zerop (low_bound))
13765 index = size_diffop_loc (loc, index,
13766 fold_convert_loc (loc, sizetype, low_bound));
13768 string = exp1;
13771 scalar_int_mode char_mode;
13772 if (string
13773 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13774 && TREE_CODE (string) == STRING_CST
13775 && TREE_CODE (index) == INTEGER_CST
13776 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13777 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13778 &char_mode)
13779 && GET_MODE_SIZE (char_mode) == 1)
13780 return build_int_cst_type (TREE_TYPE (exp),
13781 (TREE_STRING_POINTER (string)
13782 [TREE_INT_CST_LOW (index)]));
13784 return NULL;
13787 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13788 an integer constant, real, or fixed-point constant.
13790 TYPE is the type of the result. */
13792 static tree
13793 fold_negate_const (tree arg0, tree type)
13795 tree t = NULL_TREE;
13797 switch (TREE_CODE (arg0))
13799 case REAL_CST:
13800 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13801 break;
13803 case FIXED_CST:
13805 FIXED_VALUE_TYPE f;
13806 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13807 &(TREE_FIXED_CST (arg0)), NULL,
13808 TYPE_SATURATING (type));
13809 t = build_fixed (type, f);
13810 /* Propagate overflow flags. */
13811 if (overflow_p | TREE_OVERFLOW (arg0))
13812 TREE_OVERFLOW (t) = 1;
13813 break;
13816 default:
13817 if (poly_int_tree_p (arg0))
13819 bool overflow;
13820 poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
13821 t = force_fit_type (type, res, 1,
13822 (overflow && ! TYPE_UNSIGNED (type))
13823 || TREE_OVERFLOW (arg0));
13824 break;
13827 gcc_unreachable ();
13830 return t;
13833 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13834 an integer constant or real constant.
13836 TYPE is the type of the result. */
13838 tree
13839 fold_abs_const (tree arg0, tree type)
13841 tree t = NULL_TREE;
13843 switch (TREE_CODE (arg0))
13845 case INTEGER_CST:
13847 /* If the value is unsigned or non-negative, then the absolute value
13848 is the same as the ordinary value. */
13849 if (!wi::neg_p (wi::to_wide (arg0), TYPE_SIGN (type)))
13850 t = arg0;
13852 /* If the value is negative, then the absolute value is
13853 its negation. */
13854 else
13856 bool overflow;
13857 wide_int val = wi::neg (wi::to_wide (arg0), &overflow);
13858 t = force_fit_type (type, val, -1,
13859 overflow | TREE_OVERFLOW (arg0));
13862 break;
13864 case REAL_CST:
13865 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13866 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13867 else
13868 t = arg0;
13869 break;
13871 default:
13872 gcc_unreachable ();
13875 return t;
13878 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13879 constant. TYPE is the type of the result. */
13881 static tree
13882 fold_not_const (const_tree arg0, tree type)
13884 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13886 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
13889 /* Given CODE, a relational operator, the target type, TYPE and two
13890 constant operands OP0 and OP1, return the result of the
13891 relational operation. If the result is not a compile time
13892 constant, then return NULL_TREE. */
13894 static tree
13895 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13897 int result, invert;
13899 /* From here on, the only cases we handle are when the result is
13900 known to be a constant. */
13902 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13904 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13905 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13907 /* Handle the cases where either operand is a NaN. */
13908 if (real_isnan (c0) || real_isnan (c1))
13910 switch (code)
13912 case EQ_EXPR:
13913 case ORDERED_EXPR:
13914 result = 0;
13915 break;
13917 case NE_EXPR:
13918 case UNORDERED_EXPR:
13919 case UNLT_EXPR:
13920 case UNLE_EXPR:
13921 case UNGT_EXPR:
13922 case UNGE_EXPR:
13923 case UNEQ_EXPR:
13924 result = 1;
13925 break;
13927 case LT_EXPR:
13928 case LE_EXPR:
13929 case GT_EXPR:
13930 case GE_EXPR:
13931 case LTGT_EXPR:
13932 if (flag_trapping_math)
13933 return NULL_TREE;
13934 result = 0;
13935 break;
13937 default:
13938 gcc_unreachable ();
13941 return constant_boolean_node (result, type);
13944 return constant_boolean_node (real_compare (code, c0, c1), type);
13947 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13949 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13950 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13951 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13954 /* Handle equality/inequality of complex constants. */
13955 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13957 tree rcond = fold_relational_const (code, type,
13958 TREE_REALPART (op0),
13959 TREE_REALPART (op1));
13960 tree icond = fold_relational_const (code, type,
13961 TREE_IMAGPART (op0),
13962 TREE_IMAGPART (op1));
13963 if (code == EQ_EXPR)
13964 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13965 else if (code == NE_EXPR)
13966 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13967 else
13968 return NULL_TREE;
13971 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13973 if (!VECTOR_TYPE_P (type))
13975 /* Have vector comparison with scalar boolean result. */
13976 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13977 && known_eq (VECTOR_CST_NELTS (op0),
13978 VECTOR_CST_NELTS (op1)));
13979 unsigned HOST_WIDE_INT nunits;
13980 if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
13981 return NULL_TREE;
13982 for (unsigned i = 0; i < nunits; i++)
13984 tree elem0 = VECTOR_CST_ELT (op0, i);
13985 tree elem1 = VECTOR_CST_ELT (op1, i);
13986 tree tmp = fold_relational_const (code, type, elem0, elem1);
13987 if (tmp == NULL_TREE)
13988 return NULL_TREE;
13989 if (integer_zerop (tmp))
13990 return constant_boolean_node (false, type);
13992 return constant_boolean_node (true, type);
13994 tree_vector_builder elts;
13995 if (!elts.new_binary_operation (type, op0, op1, false))
13996 return NULL_TREE;
13997 unsigned int count = elts.encoded_nelts ();
13998 for (unsigned i = 0; i < count; i++)
14000 tree elem_type = TREE_TYPE (type);
14001 tree elem0 = VECTOR_CST_ELT (op0, i);
14002 tree elem1 = VECTOR_CST_ELT (op1, i);
14004 tree tem = fold_relational_const (code, elem_type,
14005 elem0, elem1);
14007 if (tem == NULL_TREE)
14008 return NULL_TREE;
14010 elts.quick_push (build_int_cst (elem_type,
14011 integer_zerop (tem) ? 0 : -1));
14014 return elts.build ();
14017 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14019 To compute GT, swap the arguments and do LT.
14020 To compute GE, do LT and invert the result.
14021 To compute LE, swap the arguments, do LT and invert the result.
14022 To compute NE, do EQ and invert the result.
14024 Therefore, the code below must handle only EQ and LT. */
14026 if (code == LE_EXPR || code == GT_EXPR)
14028 std::swap (op0, op1);
14029 code = swap_tree_comparison (code);
14032 /* Note that it is safe to invert for real values here because we
14033 have already handled the one case that it matters. */
14035 invert = 0;
14036 if (code == NE_EXPR || code == GE_EXPR)
14038 invert = 1;
14039 code = invert_tree_comparison (code, false);
14042 /* Compute a result for LT or EQ if args permit;
14043 Otherwise return T. */
14044 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14046 if (code == EQ_EXPR)
14047 result = tree_int_cst_equal (op0, op1);
14048 else
14049 result = tree_int_cst_lt (op0, op1);
14051 else
14052 return NULL_TREE;
14054 if (invert)
14055 result ^= 1;
14056 return constant_boolean_node (result, type);
14059 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14060 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14061 itself. */
14063 tree
14064 fold_build_cleanup_point_expr (tree type, tree expr)
14066 /* If the expression does not have side effects then we don't have to wrap
14067 it with a cleanup point expression. */
14068 if (!TREE_SIDE_EFFECTS (expr))
14069 return expr;
14071 /* If the expression is a return, check to see if the expression inside the
14072 return has no side effects or the right hand side of the modify expression
14073 inside the return. If either don't have side effects set we don't need to
14074 wrap the expression in a cleanup point expression. Note we don't check the
14075 left hand side of the modify because it should always be a return decl. */
14076 if (TREE_CODE (expr) == RETURN_EXPR)
14078 tree op = TREE_OPERAND (expr, 0);
14079 if (!op || !TREE_SIDE_EFFECTS (op))
14080 return expr;
14081 op = TREE_OPERAND (op, 1);
14082 if (!TREE_SIDE_EFFECTS (op))
14083 return expr;
14086 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
14089 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14090 of an indirection through OP0, or NULL_TREE if no simplification is
14091 possible. */
14093 tree
14094 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14096 tree sub = op0;
14097 tree subtype;
14098 poly_uint64 const_op01;
14100 STRIP_NOPS (sub);
14101 subtype = TREE_TYPE (sub);
14102 if (!POINTER_TYPE_P (subtype)
14103 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
14104 return NULL_TREE;
14106 if (TREE_CODE (sub) == ADDR_EXPR)
14108 tree op = TREE_OPERAND (sub, 0);
14109 tree optype = TREE_TYPE (op);
14111 /* *&CONST_DECL -> to the value of the const decl. */
14112 if (TREE_CODE (op) == CONST_DECL)
14113 return DECL_INITIAL (op);
14114 /* *&p => p; make sure to handle *&"str"[cst] here. */
14115 if (type == optype)
14117 tree fop = fold_read_from_constant_string (op);
14118 if (fop)
14119 return fop;
14120 else
14121 return op;
14123 /* *(foo *)&fooarray => fooarray[0] */
14124 else if (TREE_CODE (optype) == ARRAY_TYPE
14125 && type == TREE_TYPE (optype)
14126 && (!in_gimple_form
14127 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14129 tree type_domain = TYPE_DOMAIN (optype);
14130 tree min_val = size_zero_node;
14131 if (type_domain && TYPE_MIN_VALUE (type_domain))
14132 min_val = TYPE_MIN_VALUE (type_domain);
14133 if (in_gimple_form
14134 && TREE_CODE (min_val) != INTEGER_CST)
14135 return NULL_TREE;
14136 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14137 NULL_TREE, NULL_TREE);
14139 /* *(foo *)&complexfoo => __real__ complexfoo */
14140 else if (TREE_CODE (optype) == COMPLEX_TYPE
14141 && type == TREE_TYPE (optype))
14142 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14143 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14144 else if (VECTOR_TYPE_P (optype)
14145 && type == TREE_TYPE (optype))
14147 tree part_width = TYPE_SIZE (type);
14148 tree index = bitsize_int (0);
14149 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
14150 index);
14154 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14155 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
14157 tree op00 = TREE_OPERAND (sub, 0);
14158 tree op01 = TREE_OPERAND (sub, 1);
14160 STRIP_NOPS (op00);
14161 if (TREE_CODE (op00) == ADDR_EXPR)
14163 tree op00type;
14164 op00 = TREE_OPERAND (op00, 0);
14165 op00type = TREE_TYPE (op00);
14167 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14168 if (VECTOR_TYPE_P (op00type)
14169 && type == TREE_TYPE (op00type)
14170 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
14171 but we want to treat offsets with MSB set as negative.
14172 For the code below negative offsets are invalid and
14173 TYPE_SIZE of the element is something unsigned, so
14174 check whether op01 fits into poly_int64, which implies
14175 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
14176 then just use poly_uint64 because we want to treat the
14177 value as unsigned. */
14178 && tree_fits_poly_int64_p (op01))
14180 tree part_width = TYPE_SIZE (type);
14181 poly_uint64 max_offset
14182 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14183 * TYPE_VECTOR_SUBPARTS (op00type));
14184 if (known_lt (const_op01, max_offset))
14186 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
14187 return fold_build3_loc (loc,
14188 BIT_FIELD_REF, type, op00,
14189 part_width, index);
14192 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14193 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14194 && type == TREE_TYPE (op00type))
14196 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
14197 const_op01))
14198 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14200 /* ((foo *)&fooarray)[1] => fooarray[1] */
14201 else if (TREE_CODE (op00type) == ARRAY_TYPE
14202 && type == TREE_TYPE (op00type))
14204 tree type_domain = TYPE_DOMAIN (op00type);
14205 tree min_val = size_zero_node;
14206 if (type_domain && TYPE_MIN_VALUE (type_domain))
14207 min_val = TYPE_MIN_VALUE (type_domain);
14208 offset_int off = wi::to_offset (op01);
14209 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type));
14210 offset_int remainder;
14211 off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder);
14212 if (remainder == 0 && TREE_CODE (min_val) == INTEGER_CST)
14214 off = off + wi::to_offset (min_val);
14215 op01 = wide_int_to_tree (sizetype, off);
14216 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14217 NULL_TREE, NULL_TREE);
14223 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14224 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14225 && type == TREE_TYPE (TREE_TYPE (subtype))
14226 && (!in_gimple_form
14227 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14229 tree type_domain;
14230 tree min_val = size_zero_node;
14231 sub = build_fold_indirect_ref_loc (loc, sub);
14232 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14233 if (type_domain && TYPE_MIN_VALUE (type_domain))
14234 min_val = TYPE_MIN_VALUE (type_domain);
14235 if (in_gimple_form
14236 && TREE_CODE (min_val) != INTEGER_CST)
14237 return NULL_TREE;
14238 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14239 NULL_TREE);
14242 return NULL_TREE;
14245 /* Builds an expression for an indirection through T, simplifying some
14246 cases. */
14248 tree
14249 build_fold_indirect_ref_loc (location_t loc, tree t)
14251 tree type = TREE_TYPE (TREE_TYPE (t));
14252 tree sub = fold_indirect_ref_1 (loc, type, t);
14254 if (sub)
14255 return sub;
14257 return build1_loc (loc, INDIRECT_REF, type, t);
14260 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14262 tree
14263 fold_indirect_ref_loc (location_t loc, tree t)
14265 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14267 if (sub)
14268 return sub;
14269 else
14270 return t;
14273 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14274 whose result is ignored. The type of the returned tree need not be
14275 the same as the original expression. */
14277 tree
14278 fold_ignored_result (tree t)
14280 if (!TREE_SIDE_EFFECTS (t))
14281 return integer_zero_node;
14283 for (;;)
14284 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14286 case tcc_unary:
14287 t = TREE_OPERAND (t, 0);
14288 break;
14290 case tcc_binary:
14291 case tcc_comparison:
14292 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14293 t = TREE_OPERAND (t, 0);
14294 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14295 t = TREE_OPERAND (t, 1);
14296 else
14297 return t;
14298 break;
14300 case tcc_expression:
14301 switch (TREE_CODE (t))
14303 case COMPOUND_EXPR:
14304 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14305 return t;
14306 t = TREE_OPERAND (t, 0);
14307 break;
14309 case COND_EXPR:
14310 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14311 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14312 return t;
14313 t = TREE_OPERAND (t, 0);
14314 break;
14316 default:
14317 return t;
14319 break;
14321 default:
14322 return t;
14326 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14328 tree
14329 round_up_loc (location_t loc, tree value, unsigned int divisor)
14331 tree div = NULL_TREE;
14333 if (divisor == 1)
14334 return value;
14336 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14337 have to do anything. Only do this when we are not given a const,
14338 because in that case, this check is more expensive than just
14339 doing it. */
14340 if (TREE_CODE (value) != INTEGER_CST)
14342 div = build_int_cst (TREE_TYPE (value), divisor);
14344 if (multiple_of_p (TREE_TYPE (value), value, div))
14345 return value;
14348 /* If divisor is a power of two, simplify this to bit manipulation. */
14349 if (pow2_or_zerop (divisor))
14351 if (TREE_CODE (value) == INTEGER_CST)
14353 wide_int val = wi::to_wide (value);
14354 bool overflow_p;
14356 if ((val & (divisor - 1)) == 0)
14357 return value;
14359 overflow_p = TREE_OVERFLOW (value);
14360 val += divisor - 1;
14361 val &= (int) -divisor;
14362 if (val == 0)
14363 overflow_p = true;
14365 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14367 else
14369 tree t;
14371 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14372 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14373 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14374 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14377 else
14379 if (!div)
14380 div = build_int_cst (TREE_TYPE (value), divisor);
14381 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14382 value = size_binop_loc (loc, MULT_EXPR, value, div);
14385 return value;
14388 /* Likewise, but round down. */
14390 tree
14391 round_down_loc (location_t loc, tree value, int divisor)
14393 tree div = NULL_TREE;
14395 gcc_assert (divisor > 0);
14396 if (divisor == 1)
14397 return value;
14399 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14400 have to do anything. Only do this when we are not given a const,
14401 because in that case, this check is more expensive than just
14402 doing it. */
14403 if (TREE_CODE (value) != INTEGER_CST)
14405 div = build_int_cst (TREE_TYPE (value), divisor);
14407 if (multiple_of_p (TREE_TYPE (value), value, div))
14408 return value;
14411 /* If divisor is a power of two, simplify this to bit manipulation. */
14412 if (pow2_or_zerop (divisor))
14414 tree t;
14416 t = build_int_cst (TREE_TYPE (value), -divisor);
14417 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14419 else
14421 if (!div)
14422 div = build_int_cst (TREE_TYPE (value), divisor);
14423 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14424 value = size_binop_loc (loc, MULT_EXPR, value, div);
14427 return value;
14430 /* Returns the pointer to the base of the object addressed by EXP and
14431 extracts the information about the offset of the access, storing it
14432 to PBITPOS and POFFSET. */
14434 static tree
14435 split_address_to_core_and_offset (tree exp,
14436 poly_int64_pod *pbitpos, tree *poffset)
14438 tree core;
14439 machine_mode mode;
14440 int unsignedp, reversep, volatilep;
14441 poly_int64 bitsize;
14442 location_t loc = EXPR_LOCATION (exp);
14444 if (TREE_CODE (exp) == ADDR_EXPR)
14446 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14447 poffset, &mode, &unsignedp, &reversep,
14448 &volatilep);
14449 core = build_fold_addr_expr_loc (loc, core);
14451 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14453 core = TREE_OPERAND (exp, 0);
14454 STRIP_NOPS (core);
14455 *pbitpos = 0;
14456 *poffset = TREE_OPERAND (exp, 1);
14457 if (poly_int_tree_p (*poffset))
14459 poly_offset_int tem
14460 = wi::sext (wi::to_poly_offset (*poffset),
14461 TYPE_PRECISION (TREE_TYPE (*poffset)));
14462 tem <<= LOG2_BITS_PER_UNIT;
14463 if (tem.to_shwi (pbitpos))
14464 *poffset = NULL_TREE;
14467 else
14469 core = exp;
14470 *pbitpos = 0;
14471 *poffset = NULL_TREE;
14474 return core;
14477 /* Returns true if addresses of E1 and E2 differ by a constant, false
14478 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14480 bool
14481 ptr_difference_const (tree e1, tree e2, poly_int64_pod *diff)
14483 tree core1, core2;
14484 poly_int64 bitpos1, bitpos2;
14485 tree toffset1, toffset2, tdiff, type;
14487 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14488 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14490 poly_int64 bytepos1, bytepos2;
14491 if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
14492 || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
14493 || !operand_equal_p (core1, core2, 0))
14494 return false;
14496 if (toffset1 && toffset2)
14498 type = TREE_TYPE (toffset1);
14499 if (type != TREE_TYPE (toffset2))
14500 toffset2 = fold_convert (type, toffset2);
14502 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14503 if (!cst_and_fits_in_hwi (tdiff))
14504 return false;
14506 *diff = int_cst_value (tdiff);
14508 else if (toffset1 || toffset2)
14510 /* If only one of the offsets is non-constant, the difference cannot
14511 be a constant. */
14512 return false;
14514 else
14515 *diff = 0;
14517 *diff += bytepos1 - bytepos2;
14518 return true;
14521 /* Return OFF converted to a pointer offset type suitable as offset for
14522 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14523 tree
14524 convert_to_ptrofftype_loc (location_t loc, tree off)
14526 return fold_convert_loc (loc, sizetype, off);
14529 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14530 tree
14531 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14533 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14534 ptr, convert_to_ptrofftype_loc (loc, off));
14537 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14538 tree
14539 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14541 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14542 ptr, size_int (off));
14545 /* Return a char pointer for a C string if it is a string constant
14546 or sum of string constant and integer constant. We only support
14547 string constants properly terminated with '\0' character.
14548 If STRLEN is a valid pointer, length (including terminating character)
14549 of returned string is stored to the argument. */
14551 const char *
14552 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14554 tree offset_node;
14556 if (strlen)
14557 *strlen = 0;
14559 src = string_constant (src, &offset_node);
14560 if (src == 0)
14561 return NULL;
14563 unsigned HOST_WIDE_INT offset = 0;
14564 if (offset_node != NULL_TREE)
14566 if (!tree_fits_uhwi_p (offset_node))
14567 return NULL;
14568 else
14569 offset = tree_to_uhwi (offset_node);
14572 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14573 const char *string = TREE_STRING_POINTER (src);
14575 /* Support only properly null-terminated strings. */
14576 if (string_length == 0
14577 || string[string_length - 1] != '\0'
14578 || offset >= string_length)
14579 return NULL;
14581 if (strlen)
14582 *strlen = string_length - offset;
14583 return string + offset;
14586 #if CHECKING_P
14588 namespace selftest {
14590 /* Helper functions for writing tests of folding trees. */
14592 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14594 static void
14595 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14596 tree constant)
14598 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14601 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14602 wrapping WRAPPED_EXPR. */
14604 static void
14605 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14606 tree wrapped_expr)
14608 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14609 ASSERT_NE (wrapped_expr, result);
14610 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14611 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14614 /* Verify that various arithmetic binary operations are folded
14615 correctly. */
14617 static void
14618 test_arithmetic_folding ()
14620 tree type = integer_type_node;
14621 tree x = create_tmp_var_raw (type, "x");
14622 tree zero = build_zero_cst (type);
14623 tree one = build_int_cst (type, 1);
14625 /* Addition. */
14626 /* 1 <-- (0 + 1) */
14627 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14628 one);
14629 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14630 one);
14632 /* (nonlvalue)x <-- (x + 0) */
14633 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14636 /* Subtraction. */
14637 /* 0 <-- (x - x) */
14638 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14639 zero);
14640 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14643 /* Multiplication. */
14644 /* 0 <-- (x * 0) */
14645 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14646 zero);
14648 /* (nonlvalue)x <-- (x * 1) */
14649 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14653 /* Verify that various binary operations on vectors are folded
14654 correctly. */
14656 static void
14657 test_vector_folding ()
14659 tree inner_type = integer_type_node;
14660 tree type = build_vector_type (inner_type, 4);
14661 tree zero = build_zero_cst (type);
14662 tree one = build_one_cst (type);
14664 /* Verify equality tests that return a scalar boolean result. */
14665 tree res_type = boolean_type_node;
14666 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14667 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14668 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14669 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14672 /* Verify folding of VEC_DUPLICATE_EXPRs. */
14674 static void
14675 test_vec_duplicate_folding ()
14677 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
14678 machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
14679 /* This will be 1 if VEC_MODE isn't a vector mode. */
14680 poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
14682 tree type = build_vector_type (ssizetype, nunits);
14683 tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
14684 tree dup5_cst = build_vector_from_val (type, ssize_int (5));
14685 ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
14688 /* Run all of the selftests within this file. */
14690 void
14691 fold_const_c_tests ()
14693 test_arithmetic_folding ();
14694 test_vector_folding ();
14695 test_vec_duplicate_folding ();
14698 } // namespace selftest
14700 #endif /* CHECKING_P */