poly_int: GET_MODE_NUNITS
[official-gcc.git] / gcc / fold-const.c
blob9f558e26c80d944f88b5761c2f598ca86784a8cb
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 *, int *);
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 if (negate_expr_p (TREE_OPERAND (t, 0)))
478 return true;
479 /* In general we can't negate B in A / B, because if A is INT_MIN and
480 B is 1, we may turn this into INT_MIN / -1 which is undefined
481 and actually traps on some architectures. */
482 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
483 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
484 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
485 && ! integer_onep (TREE_OPERAND (t, 1))))
486 return negate_expr_p (TREE_OPERAND (t, 1));
487 break;
489 case NOP_EXPR:
490 /* Negate -((double)float) as (double)(-float). */
491 if (TREE_CODE (type) == REAL_TYPE)
493 tree tem = strip_float_extensions (t);
494 if (tem != t)
495 return negate_expr_p (tem);
497 break;
499 case CALL_EXPR:
500 /* Negate -f(x) as f(-x). */
501 if (negate_mathfn_p (get_call_combined_fn (t)))
502 return negate_expr_p (CALL_EXPR_ARG (t, 0));
503 break;
505 case RSHIFT_EXPR:
506 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
507 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
509 tree op1 = TREE_OPERAND (t, 1);
510 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
511 return true;
513 break;
515 default:
516 break;
518 return false;
521 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
522 simplification is possible.
523 If negate_expr_p would return true for T, NULL_TREE will never be
524 returned. */
526 static tree
527 fold_negate_expr_1 (location_t loc, tree t)
529 tree type = TREE_TYPE (t);
530 tree tem;
532 switch (TREE_CODE (t))
534 /* Convert - (~A) to A + 1. */
535 case BIT_NOT_EXPR:
536 if (INTEGRAL_TYPE_P (type))
537 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
538 build_one_cst (type));
539 break;
541 case INTEGER_CST:
542 tem = fold_negate_const (t, type);
543 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
544 || (ANY_INTEGRAL_TYPE_P (type)
545 && !TYPE_OVERFLOW_TRAPS (type)
546 && TYPE_OVERFLOW_WRAPS (type))
547 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
548 return tem;
549 break;
551 case POLY_INT_CST:
552 case REAL_CST:
553 case FIXED_CST:
554 tem = fold_negate_const (t, type);
555 return tem;
557 case COMPLEX_CST:
559 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
560 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
561 if (rpart && ipart)
562 return build_complex (type, rpart, ipart);
564 break;
566 case VECTOR_CST:
568 tree_vector_builder elts;
569 elts.new_unary_operation (type, t, true);
570 unsigned int count = elts.encoded_nelts ();
571 for (unsigned int i = 0; i < count; ++i)
573 tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
574 if (elt == NULL_TREE)
575 return NULL_TREE;
576 elts.quick_push (elt);
579 return elts.build ();
582 case COMPLEX_EXPR:
583 if (negate_expr_p (t))
584 return fold_build2_loc (loc, COMPLEX_EXPR, type,
585 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
586 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
587 break;
589 case CONJ_EXPR:
590 if (negate_expr_p (t))
591 return fold_build1_loc (loc, CONJ_EXPR, type,
592 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
593 break;
595 case NEGATE_EXPR:
596 if (!TYPE_OVERFLOW_SANITIZED (type))
597 return TREE_OPERAND (t, 0);
598 break;
600 case PLUS_EXPR:
601 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
602 && !HONOR_SIGNED_ZEROS (element_mode (type)))
604 /* -(A + B) -> (-B) - A. */
605 if (negate_expr_p (TREE_OPERAND (t, 1)))
607 tem = negate_expr (TREE_OPERAND (t, 1));
608 return fold_build2_loc (loc, MINUS_EXPR, type,
609 tem, TREE_OPERAND (t, 0));
612 /* -(A + B) -> (-A) - B. */
613 if (negate_expr_p (TREE_OPERAND (t, 0)))
615 tem = negate_expr (TREE_OPERAND (t, 0));
616 return fold_build2_loc (loc, MINUS_EXPR, type,
617 tem, TREE_OPERAND (t, 1));
620 break;
622 case MINUS_EXPR:
623 /* - (A - B) -> B - A */
624 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
625 && !HONOR_SIGNED_ZEROS (element_mode (type)))
626 return fold_build2_loc (loc, MINUS_EXPR, type,
627 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
628 break;
630 case MULT_EXPR:
631 if (TYPE_UNSIGNED (type))
632 break;
634 /* Fall through. */
636 case RDIV_EXPR:
637 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
639 tem = TREE_OPERAND (t, 1);
640 if (negate_expr_p (tem))
641 return fold_build2_loc (loc, TREE_CODE (t), type,
642 TREE_OPERAND (t, 0), negate_expr (tem));
643 tem = TREE_OPERAND (t, 0);
644 if (negate_expr_p (tem))
645 return fold_build2_loc (loc, TREE_CODE (t), type,
646 negate_expr (tem), TREE_OPERAND (t, 1));
648 break;
650 case TRUNC_DIV_EXPR:
651 case ROUND_DIV_EXPR:
652 case EXACT_DIV_EXPR:
653 if (TYPE_UNSIGNED (type))
654 break;
655 if (negate_expr_p (TREE_OPERAND (t, 0)))
656 return fold_build2_loc (loc, TREE_CODE (t), type,
657 negate_expr (TREE_OPERAND (t, 0)),
658 TREE_OPERAND (t, 1));
659 /* In general we can't negate B in A / B, because if A is INT_MIN and
660 B is 1, we may turn this into INT_MIN / -1 which is undefined
661 and actually traps on some architectures. */
662 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
663 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
664 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
665 && ! integer_onep (TREE_OPERAND (t, 1))))
666 && negate_expr_p (TREE_OPERAND (t, 1)))
667 return fold_build2_loc (loc, TREE_CODE (t), type,
668 TREE_OPERAND (t, 0),
669 negate_expr (TREE_OPERAND (t, 1)));
670 break;
672 case NOP_EXPR:
673 /* Convert -((double)float) into (double)(-float). */
674 if (TREE_CODE (type) == REAL_TYPE)
676 tem = strip_float_extensions (t);
677 if (tem != t && negate_expr_p (tem))
678 return fold_convert_loc (loc, type, negate_expr (tem));
680 break;
682 case CALL_EXPR:
683 /* Negate -f(x) as f(-x). */
684 if (negate_mathfn_p (get_call_combined_fn (t))
685 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
687 tree fndecl, arg;
689 fndecl = get_callee_fndecl (t);
690 arg = negate_expr (CALL_EXPR_ARG (t, 0));
691 return build_call_expr_loc (loc, fndecl, 1, arg);
693 break;
695 case RSHIFT_EXPR:
696 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
697 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
699 tree op1 = TREE_OPERAND (t, 1);
700 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
702 tree ntype = TYPE_UNSIGNED (type)
703 ? signed_type_for (type)
704 : unsigned_type_for (type);
705 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
706 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
707 return fold_convert_loc (loc, type, temp);
710 break;
712 default:
713 break;
716 return NULL_TREE;
719 /* A wrapper for fold_negate_expr_1. */
721 static tree
722 fold_negate_expr (location_t loc, tree t)
724 tree type = TREE_TYPE (t);
725 STRIP_SIGN_NOPS (t);
726 tree tem = fold_negate_expr_1 (loc, t);
727 if (tem == NULL_TREE)
728 return NULL_TREE;
729 return fold_convert_loc (loc, type, tem);
732 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
733 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
734 return NULL_TREE. */
736 static tree
737 negate_expr (tree t)
739 tree type, tem;
740 location_t loc;
742 if (t == NULL_TREE)
743 return NULL_TREE;
745 loc = EXPR_LOCATION (t);
746 type = TREE_TYPE (t);
747 STRIP_SIGN_NOPS (t);
749 tem = fold_negate_expr (loc, t);
750 if (!tem)
751 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
752 return fold_convert_loc (loc, type, tem);
755 /* Split a tree IN into a constant, literal and variable parts that could be
756 combined with CODE to make IN. "constant" means an expression with
757 TREE_CONSTANT but that isn't an actual constant. CODE must be a
758 commutative arithmetic operation. Store the constant part into *CONP,
759 the literal in *LITP and return the variable part. If a part isn't
760 present, set it to null. If the tree does not decompose in this way,
761 return the entire tree as the variable part and the other parts as null.
763 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
764 case, we negate an operand that was subtracted. Except if it is a
765 literal for which we use *MINUS_LITP instead.
767 If NEGATE_P is true, we are negating all of IN, again except a literal
768 for which we use *MINUS_LITP instead. If a variable part is of pointer
769 type, it is negated after converting to TYPE. This prevents us from
770 generating illegal MINUS pointer expression. LOC is the location of
771 the converted variable part.
773 If IN is itself a literal or constant, return it as appropriate.
775 Note that we do not guarantee that any of the three values will be the
776 same type as IN, but they will have the same signedness and mode. */
778 static tree
779 split_tree (tree in, tree type, enum tree_code code,
780 tree *minus_varp, tree *conp, tree *minus_conp,
781 tree *litp, tree *minus_litp, int negate_p)
783 tree var = 0;
784 *minus_varp = 0;
785 *conp = 0;
786 *minus_conp = 0;
787 *litp = 0;
788 *minus_litp = 0;
790 /* Strip any conversions that don't change the machine mode or signedness. */
791 STRIP_SIGN_NOPS (in);
793 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
794 || TREE_CODE (in) == FIXED_CST)
795 *litp = in;
796 else if (TREE_CODE (in) == code
797 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
798 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
799 /* We can associate addition and subtraction together (even
800 though the C standard doesn't say so) for integers because
801 the value is not affected. For reals, the value might be
802 affected, so we can't. */
803 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
804 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
805 || (code == MINUS_EXPR
806 && (TREE_CODE (in) == PLUS_EXPR
807 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
809 tree op0 = TREE_OPERAND (in, 0);
810 tree op1 = TREE_OPERAND (in, 1);
811 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
812 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
814 /* First see if either of the operands is a literal, then a constant. */
815 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
816 || TREE_CODE (op0) == FIXED_CST)
817 *litp = op0, op0 = 0;
818 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
819 || TREE_CODE (op1) == FIXED_CST)
820 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
822 if (op0 != 0 && TREE_CONSTANT (op0))
823 *conp = op0, op0 = 0;
824 else if (op1 != 0 && TREE_CONSTANT (op1))
825 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
827 /* If we haven't dealt with either operand, this is not a case we can
828 decompose. Otherwise, VAR is either of the ones remaining, if any. */
829 if (op0 != 0 && op1 != 0)
830 var = in;
831 else if (op0 != 0)
832 var = op0;
833 else
834 var = op1, neg_var_p = neg1_p;
836 /* Now do any needed negations. */
837 if (neg_litp_p)
838 *minus_litp = *litp, *litp = 0;
839 if (neg_conp_p && *conp)
840 *minus_conp = *conp, *conp = 0;
841 if (neg_var_p && var)
842 *minus_varp = var, var = 0;
844 else if (TREE_CONSTANT (in))
845 *conp = in;
846 else if (TREE_CODE (in) == BIT_NOT_EXPR
847 && code == PLUS_EXPR)
849 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
850 when IN is constant. */
851 *litp = build_minus_one_cst (type);
852 *minus_varp = TREE_OPERAND (in, 0);
854 else
855 var = in;
857 if (negate_p)
859 if (*litp)
860 *minus_litp = *litp, *litp = 0;
861 else if (*minus_litp)
862 *litp = *minus_litp, *minus_litp = 0;
863 if (*conp)
864 *minus_conp = *conp, *conp = 0;
865 else if (*minus_conp)
866 *conp = *minus_conp, *minus_conp = 0;
867 if (var)
868 *minus_varp = var, var = 0;
869 else if (*minus_varp)
870 var = *minus_varp, *minus_varp = 0;
873 if (*litp
874 && TREE_OVERFLOW_P (*litp))
875 *litp = drop_tree_overflow (*litp);
876 if (*minus_litp
877 && TREE_OVERFLOW_P (*minus_litp))
878 *minus_litp = drop_tree_overflow (*minus_litp);
880 return var;
883 /* Re-associate trees split by the above function. T1 and T2 are
884 either expressions to associate or null. Return the new
885 expression, if any. LOC is the location of the new expression. If
886 we build an operation, do it in TYPE and with CODE. */
888 static tree
889 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
891 if (t1 == 0)
893 gcc_assert (t2 == 0 || code != MINUS_EXPR);
894 return t2;
896 else if (t2 == 0)
897 return t1;
899 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
900 try to fold this since we will have infinite recursion. But do
901 deal with any NEGATE_EXPRs. */
902 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
903 || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
904 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
906 if (code == PLUS_EXPR)
908 if (TREE_CODE (t1) == NEGATE_EXPR)
909 return build2_loc (loc, MINUS_EXPR, type,
910 fold_convert_loc (loc, type, t2),
911 fold_convert_loc (loc, type,
912 TREE_OPERAND (t1, 0)));
913 else if (TREE_CODE (t2) == NEGATE_EXPR)
914 return build2_loc (loc, MINUS_EXPR, type,
915 fold_convert_loc (loc, type, t1),
916 fold_convert_loc (loc, type,
917 TREE_OPERAND (t2, 0)));
918 else if (integer_zerop (t2))
919 return fold_convert_loc (loc, type, t1);
921 else if (code == MINUS_EXPR)
923 if (integer_zerop (t2))
924 return fold_convert_loc (loc, type, t1);
927 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
928 fold_convert_loc (loc, type, t2));
931 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
932 fold_convert_loc (loc, type, t2));
935 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
936 for use in int_const_binop, size_binop and size_diffop. */
938 static bool
939 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
941 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
942 return false;
943 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
944 return false;
946 switch (code)
948 case LSHIFT_EXPR:
949 case RSHIFT_EXPR:
950 case LROTATE_EXPR:
951 case RROTATE_EXPR:
952 return true;
954 default:
955 break;
958 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
959 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
960 && TYPE_MODE (type1) == TYPE_MODE (type2);
963 /* Subroutine of int_const_binop_1 that handles two INTEGER_CSTs. */
965 static tree
966 int_const_binop_2 (enum tree_code code, const_tree parg1, const_tree parg2,
967 int overflowable)
969 wide_int res;
970 tree t;
971 tree type = TREE_TYPE (parg1);
972 signop sign = TYPE_SIGN (type);
973 bool overflow = false;
975 wi::tree_to_wide_ref arg1 = wi::to_wide (parg1);
976 wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type));
978 switch (code)
980 case BIT_IOR_EXPR:
981 res = wi::bit_or (arg1, arg2);
982 break;
984 case BIT_XOR_EXPR:
985 res = wi::bit_xor (arg1, arg2);
986 break;
988 case BIT_AND_EXPR:
989 res = wi::bit_and (arg1, arg2);
990 break;
992 case RSHIFT_EXPR:
993 case LSHIFT_EXPR:
994 if (wi::neg_p (arg2))
996 arg2 = -arg2;
997 if (code == RSHIFT_EXPR)
998 code = LSHIFT_EXPR;
999 else
1000 code = RSHIFT_EXPR;
1003 if (code == RSHIFT_EXPR)
1004 /* It's unclear from the C standard whether shifts can overflow.
1005 The following code ignores overflow; perhaps a C standard
1006 interpretation ruling is needed. */
1007 res = wi::rshift (arg1, arg2, sign);
1008 else
1009 res = wi::lshift (arg1, arg2);
1010 break;
1012 case RROTATE_EXPR:
1013 case LROTATE_EXPR:
1014 if (wi::neg_p (arg2))
1016 arg2 = -arg2;
1017 if (code == RROTATE_EXPR)
1018 code = LROTATE_EXPR;
1019 else
1020 code = RROTATE_EXPR;
1023 if (code == RROTATE_EXPR)
1024 res = wi::rrotate (arg1, arg2);
1025 else
1026 res = wi::lrotate (arg1, arg2);
1027 break;
1029 case PLUS_EXPR:
1030 res = wi::add (arg1, arg2, sign, &overflow);
1031 break;
1033 case MINUS_EXPR:
1034 res = wi::sub (arg1, arg2, sign, &overflow);
1035 break;
1037 case MULT_EXPR:
1038 res = wi::mul (arg1, arg2, sign, &overflow);
1039 break;
1041 case MULT_HIGHPART_EXPR:
1042 res = wi::mul_high (arg1, arg2, sign);
1043 break;
1045 case TRUNC_DIV_EXPR:
1046 case EXACT_DIV_EXPR:
1047 if (arg2 == 0)
1048 return NULL_TREE;
1049 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1050 break;
1052 case FLOOR_DIV_EXPR:
1053 if (arg2 == 0)
1054 return NULL_TREE;
1055 res = wi::div_floor (arg1, arg2, sign, &overflow);
1056 break;
1058 case CEIL_DIV_EXPR:
1059 if (arg2 == 0)
1060 return NULL_TREE;
1061 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1062 break;
1064 case ROUND_DIV_EXPR:
1065 if (arg2 == 0)
1066 return NULL_TREE;
1067 res = wi::div_round (arg1, arg2, sign, &overflow);
1068 break;
1070 case TRUNC_MOD_EXPR:
1071 if (arg2 == 0)
1072 return NULL_TREE;
1073 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1074 break;
1076 case FLOOR_MOD_EXPR:
1077 if (arg2 == 0)
1078 return NULL_TREE;
1079 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1080 break;
1082 case CEIL_MOD_EXPR:
1083 if (arg2 == 0)
1084 return NULL_TREE;
1085 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1086 break;
1088 case ROUND_MOD_EXPR:
1089 if (arg2 == 0)
1090 return NULL_TREE;
1091 res = wi::mod_round (arg1, arg2, sign, &overflow);
1092 break;
1094 case MIN_EXPR:
1095 res = wi::min (arg1, arg2, sign);
1096 break;
1098 case MAX_EXPR:
1099 res = wi::max (arg1, arg2, sign);
1100 break;
1102 default:
1103 return NULL_TREE;
1106 t = force_fit_type (type, res, overflowable,
1107 (((sign == SIGNED || overflowable == -1)
1108 && overflow)
1109 | TREE_OVERFLOW (parg1) | TREE_OVERFLOW (parg2)));
1111 return t;
1114 /* Combine two integer constants PARG1 and PARG2 under operation CODE
1115 to produce a new constant. Return NULL_TREE if we don't know how
1116 to evaluate CODE at compile-time. */
1118 static tree
1119 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree arg2,
1120 int overflowable)
1122 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1123 return int_const_binop_2 (code, arg1, arg2, overflowable);
1125 gcc_assert (NUM_POLY_INT_COEFFS != 1);
1127 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1129 poly_wide_int res;
1130 bool overflow;
1131 tree type = TREE_TYPE (arg1);
1132 signop sign = TYPE_SIGN (type);
1133 switch (code)
1135 case PLUS_EXPR:
1136 res = wi::add (wi::to_poly_wide (arg1),
1137 wi::to_poly_wide (arg2), sign, &overflow);
1138 break;
1140 case MINUS_EXPR:
1141 res = wi::sub (wi::to_poly_wide (arg1),
1142 wi::to_poly_wide (arg2), sign, &overflow);
1143 break;
1145 case MULT_EXPR:
1146 if (TREE_CODE (arg2) == INTEGER_CST)
1147 res = wi::mul (wi::to_poly_wide (arg1),
1148 wi::to_wide (arg2), sign, &overflow);
1149 else if (TREE_CODE (arg1) == INTEGER_CST)
1150 res = wi::mul (wi::to_poly_wide (arg2),
1151 wi::to_wide (arg1), sign, &overflow);
1152 else
1153 return NULL_TREE;
1154 break;
1156 case LSHIFT_EXPR:
1157 if (TREE_CODE (arg2) == INTEGER_CST)
1158 res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1159 else
1160 return NULL_TREE;
1161 break;
1163 case BIT_IOR_EXPR:
1164 if (TREE_CODE (arg2) != INTEGER_CST
1165 || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1166 &res))
1167 return NULL_TREE;
1168 break;
1170 default:
1171 return NULL_TREE;
1173 return force_fit_type (type, res, overflowable,
1174 (((sign == SIGNED || overflowable == -1)
1175 && overflow)
1176 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1179 return NULL_TREE;
1182 tree
1183 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1185 return int_const_binop_1 (code, arg1, arg2, 1);
1188 /* Return true if binary operation OP distributes over addition in operand
1189 OPNO, with the other operand being held constant. OPNO counts from 1. */
1191 static bool
1192 distributes_over_addition_p (tree_code op, int opno)
1194 switch (op)
1196 case PLUS_EXPR:
1197 case MINUS_EXPR:
1198 case MULT_EXPR:
1199 return true;
1201 case LSHIFT_EXPR:
1202 return opno == 1;
1204 default:
1205 return false;
1209 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1210 constant. We assume ARG1 and ARG2 have the same data type, or at least
1211 are the same kind of constant and the same machine mode. Return zero if
1212 combining the constants is not allowed in the current operating mode. */
1214 static tree
1215 const_binop (enum tree_code code, tree arg1, tree arg2)
1217 /* Sanity check for the recursive cases. */
1218 if (!arg1 || !arg2)
1219 return NULL_TREE;
1221 STRIP_NOPS (arg1);
1222 STRIP_NOPS (arg2);
1224 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1226 if (code == POINTER_PLUS_EXPR)
1227 return int_const_binop (PLUS_EXPR,
1228 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1230 return int_const_binop (code, arg1, arg2);
1233 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1235 machine_mode mode;
1236 REAL_VALUE_TYPE d1;
1237 REAL_VALUE_TYPE d2;
1238 REAL_VALUE_TYPE value;
1239 REAL_VALUE_TYPE result;
1240 bool inexact;
1241 tree t, type;
1243 /* The following codes are handled by real_arithmetic. */
1244 switch (code)
1246 case PLUS_EXPR:
1247 case MINUS_EXPR:
1248 case MULT_EXPR:
1249 case RDIV_EXPR:
1250 case MIN_EXPR:
1251 case MAX_EXPR:
1252 break;
1254 default:
1255 return NULL_TREE;
1258 d1 = TREE_REAL_CST (arg1);
1259 d2 = TREE_REAL_CST (arg2);
1261 type = TREE_TYPE (arg1);
1262 mode = TYPE_MODE (type);
1264 /* Don't perform operation if we honor signaling NaNs and
1265 either operand is a signaling NaN. */
1266 if (HONOR_SNANS (mode)
1267 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1268 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1269 return NULL_TREE;
1271 /* Don't perform operation if it would raise a division
1272 by zero exception. */
1273 if (code == RDIV_EXPR
1274 && real_equal (&d2, &dconst0)
1275 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1276 return NULL_TREE;
1278 /* If either operand is a NaN, just return it. Otherwise, set up
1279 for floating-point trap; we return an overflow. */
1280 if (REAL_VALUE_ISNAN (d1))
1282 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1283 is off. */
1284 d1.signalling = 0;
1285 t = build_real (type, d1);
1286 return t;
1288 else if (REAL_VALUE_ISNAN (d2))
1290 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1291 is off. */
1292 d2.signalling = 0;
1293 t = build_real (type, d2);
1294 return t;
1297 inexact = real_arithmetic (&value, code, &d1, &d2);
1298 real_convert (&result, mode, &value);
1300 /* Don't constant fold this floating point operation if
1301 the result has overflowed and flag_trapping_math. */
1302 if (flag_trapping_math
1303 && MODE_HAS_INFINITIES (mode)
1304 && REAL_VALUE_ISINF (result)
1305 && !REAL_VALUE_ISINF (d1)
1306 && !REAL_VALUE_ISINF (d2))
1307 return NULL_TREE;
1309 /* Don't constant fold this floating point operation if the
1310 result may dependent upon the run-time rounding mode and
1311 flag_rounding_math is set, or if GCC's software emulation
1312 is unable to accurately represent the result. */
1313 if ((flag_rounding_math
1314 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1315 && (inexact || !real_identical (&result, &value)))
1316 return NULL_TREE;
1318 t = build_real (type, result);
1320 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1321 return t;
1324 if (TREE_CODE (arg1) == FIXED_CST)
1326 FIXED_VALUE_TYPE f1;
1327 FIXED_VALUE_TYPE f2;
1328 FIXED_VALUE_TYPE result;
1329 tree t, type;
1330 int sat_p;
1331 bool overflow_p;
1333 /* The following codes are handled by fixed_arithmetic. */
1334 switch (code)
1336 case PLUS_EXPR:
1337 case MINUS_EXPR:
1338 case MULT_EXPR:
1339 case TRUNC_DIV_EXPR:
1340 if (TREE_CODE (arg2) != FIXED_CST)
1341 return NULL_TREE;
1342 f2 = TREE_FIXED_CST (arg2);
1343 break;
1345 case LSHIFT_EXPR:
1346 case RSHIFT_EXPR:
1348 if (TREE_CODE (arg2) != INTEGER_CST)
1349 return NULL_TREE;
1350 wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1351 f2.data.high = w2.elt (1);
1352 f2.data.low = w2.ulow ();
1353 f2.mode = SImode;
1355 break;
1357 default:
1358 return NULL_TREE;
1361 f1 = TREE_FIXED_CST (arg1);
1362 type = TREE_TYPE (arg1);
1363 sat_p = TYPE_SATURATING (type);
1364 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1365 t = build_fixed (type, result);
1366 /* Propagate overflow flags. */
1367 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1368 TREE_OVERFLOW (t) = 1;
1369 return t;
1372 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1374 tree type = TREE_TYPE (arg1);
1375 tree r1 = TREE_REALPART (arg1);
1376 tree i1 = TREE_IMAGPART (arg1);
1377 tree r2 = TREE_REALPART (arg2);
1378 tree i2 = TREE_IMAGPART (arg2);
1379 tree real, imag;
1381 switch (code)
1383 case PLUS_EXPR:
1384 case MINUS_EXPR:
1385 real = const_binop (code, r1, r2);
1386 imag = const_binop (code, i1, i2);
1387 break;
1389 case MULT_EXPR:
1390 if (COMPLEX_FLOAT_TYPE_P (type))
1391 return do_mpc_arg2 (arg1, arg2, type,
1392 /* do_nonfinite= */ folding_initializer,
1393 mpc_mul);
1395 real = const_binop (MINUS_EXPR,
1396 const_binop (MULT_EXPR, r1, r2),
1397 const_binop (MULT_EXPR, i1, i2));
1398 imag = const_binop (PLUS_EXPR,
1399 const_binop (MULT_EXPR, r1, i2),
1400 const_binop (MULT_EXPR, i1, r2));
1401 break;
1403 case RDIV_EXPR:
1404 if (COMPLEX_FLOAT_TYPE_P (type))
1405 return do_mpc_arg2 (arg1, arg2, type,
1406 /* do_nonfinite= */ folding_initializer,
1407 mpc_div);
1408 /* Fallthru. */
1409 case TRUNC_DIV_EXPR:
1410 case CEIL_DIV_EXPR:
1411 case FLOOR_DIV_EXPR:
1412 case ROUND_DIV_EXPR:
1413 if (flag_complex_method == 0)
1415 /* Keep this algorithm in sync with
1416 tree-complex.c:expand_complex_div_straight().
1418 Expand complex division to scalars, straightforward algorithm.
1419 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1420 t = br*br + bi*bi
1422 tree magsquared
1423 = const_binop (PLUS_EXPR,
1424 const_binop (MULT_EXPR, r2, r2),
1425 const_binop (MULT_EXPR, i2, i2));
1426 tree t1
1427 = const_binop (PLUS_EXPR,
1428 const_binop (MULT_EXPR, r1, r2),
1429 const_binop (MULT_EXPR, i1, i2));
1430 tree t2
1431 = const_binop (MINUS_EXPR,
1432 const_binop (MULT_EXPR, i1, r2),
1433 const_binop (MULT_EXPR, r1, i2));
1435 real = const_binop (code, t1, magsquared);
1436 imag = const_binop (code, t2, magsquared);
1438 else
1440 /* Keep this algorithm in sync with
1441 tree-complex.c:expand_complex_div_wide().
1443 Expand complex division to scalars, modified algorithm to minimize
1444 overflow with wide input ranges. */
1445 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1446 fold_abs_const (r2, TREE_TYPE (type)),
1447 fold_abs_const (i2, TREE_TYPE (type)));
1449 if (integer_nonzerop (compare))
1451 /* In the TRUE branch, we compute
1452 ratio = br/bi;
1453 div = (br * ratio) + bi;
1454 tr = (ar * ratio) + ai;
1455 ti = (ai * ratio) - ar;
1456 tr = tr / div;
1457 ti = ti / div; */
1458 tree ratio = const_binop (code, r2, i2);
1459 tree div = const_binop (PLUS_EXPR, i2,
1460 const_binop (MULT_EXPR, r2, ratio));
1461 real = const_binop (MULT_EXPR, r1, ratio);
1462 real = const_binop (PLUS_EXPR, real, i1);
1463 real = const_binop (code, real, div);
1465 imag = const_binop (MULT_EXPR, i1, ratio);
1466 imag = const_binop (MINUS_EXPR, imag, r1);
1467 imag = const_binop (code, imag, div);
1469 else
1471 /* In the FALSE branch, we compute
1472 ratio = d/c;
1473 divisor = (d * ratio) + c;
1474 tr = (b * ratio) + a;
1475 ti = b - (a * ratio);
1476 tr = tr / div;
1477 ti = ti / div; */
1478 tree ratio = const_binop (code, i2, r2);
1479 tree div = const_binop (PLUS_EXPR, r2,
1480 const_binop (MULT_EXPR, i2, ratio));
1482 real = const_binop (MULT_EXPR, i1, ratio);
1483 real = const_binop (PLUS_EXPR, real, r1);
1484 real = const_binop (code, real, div);
1486 imag = const_binop (MULT_EXPR, r1, ratio);
1487 imag = const_binop (MINUS_EXPR, i1, imag);
1488 imag = const_binop (code, imag, div);
1491 break;
1493 default:
1494 return NULL_TREE;
1497 if (real && imag)
1498 return build_complex (type, real, imag);
1501 if (TREE_CODE (arg1) == VECTOR_CST
1502 && TREE_CODE (arg2) == VECTOR_CST
1503 && (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))
1504 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1506 tree type = TREE_TYPE (arg1);
1507 bool step_ok_p;
1508 if (VECTOR_CST_STEPPED_P (arg1)
1509 && VECTOR_CST_STEPPED_P (arg2))
1510 /* We can operate directly on the encoding if:
1512 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1513 implies
1514 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1516 Addition and subtraction are the supported operators
1517 for which this is true. */
1518 step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1519 else if (VECTOR_CST_STEPPED_P (arg1))
1520 /* We can operate directly on stepped encodings if:
1522 a3 - a2 == a2 - a1
1523 implies:
1524 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1526 which is true if (x -> x op c) distributes over addition. */
1527 step_ok_p = distributes_over_addition_p (code, 1);
1528 else
1529 /* Similarly in reverse. */
1530 step_ok_p = distributes_over_addition_p (code, 2);
1531 tree_vector_builder elts;
1532 if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1533 return NULL_TREE;
1534 unsigned int count = elts.encoded_nelts ();
1535 for (unsigned int i = 0; i < count; ++i)
1537 tree elem1 = VECTOR_CST_ELT (arg1, i);
1538 tree elem2 = VECTOR_CST_ELT (arg2, i);
1540 tree elt = const_binop (code, elem1, elem2);
1542 /* It is possible that const_binop cannot handle the given
1543 code and return NULL_TREE */
1544 if (elt == NULL_TREE)
1545 return NULL_TREE;
1546 elts.quick_push (elt);
1549 return elts.build ();
1552 /* Shifts allow a scalar offset for a vector. */
1553 if (TREE_CODE (arg1) == VECTOR_CST
1554 && TREE_CODE (arg2) == INTEGER_CST)
1556 tree type = TREE_TYPE (arg1);
1557 bool step_ok_p = distributes_over_addition_p (code, 1);
1558 tree_vector_builder elts;
1559 if (!elts.new_unary_operation (type, arg1, step_ok_p))
1560 return NULL_TREE;
1561 unsigned int count = elts.encoded_nelts ();
1562 for (unsigned int i = 0; i < count; ++i)
1564 tree elem1 = VECTOR_CST_ELT (arg1, i);
1566 tree elt = const_binop (code, elem1, arg2);
1568 /* It is possible that const_binop cannot handle the given
1569 code and return NULL_TREE. */
1570 if (elt == NULL_TREE)
1571 return NULL_TREE;
1572 elts.quick_push (elt);
1575 return elts.build ();
1577 return NULL_TREE;
1580 /* Overload that adds a TYPE parameter to be able to dispatch
1581 to fold_relational_const. */
1583 tree
1584 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1586 if (TREE_CODE_CLASS (code) == tcc_comparison)
1587 return fold_relational_const (code, type, arg1, arg2);
1589 /* ??? Until we make the const_binop worker take the type of the
1590 result as argument put those cases that need it here. */
1591 switch (code)
1593 case VEC_SERIES_EXPR:
1594 if (CONSTANT_CLASS_P (arg1)
1595 && CONSTANT_CLASS_P (arg2))
1596 return build_vec_series (type, arg1, arg2);
1597 return NULL_TREE;
1599 case COMPLEX_EXPR:
1600 if ((TREE_CODE (arg1) == REAL_CST
1601 && TREE_CODE (arg2) == REAL_CST)
1602 || (TREE_CODE (arg1) == INTEGER_CST
1603 && TREE_CODE (arg2) == INTEGER_CST))
1604 return build_complex (type, arg1, arg2);
1605 return NULL_TREE;
1607 case POINTER_DIFF_EXPR:
1608 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1610 offset_int res = wi::sub (wi::to_offset (arg1),
1611 wi::to_offset (arg2));
1612 return force_fit_type (type, res, 1,
1613 TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1615 return NULL_TREE;
1617 case VEC_PACK_TRUNC_EXPR:
1618 case VEC_PACK_FIX_TRUNC_EXPR:
1620 unsigned int out_nelts, in_nelts, i;
1622 if (TREE_CODE (arg1) != VECTOR_CST
1623 || TREE_CODE (arg2) != VECTOR_CST)
1624 return NULL_TREE;
1626 in_nelts = VECTOR_CST_NELTS (arg1);
1627 out_nelts = in_nelts * 2;
1628 gcc_assert (in_nelts == VECTOR_CST_NELTS (arg2)
1629 && out_nelts == TYPE_VECTOR_SUBPARTS (type));
1631 tree_vector_builder elts (type, out_nelts, 1);
1632 for (i = 0; i < out_nelts; i++)
1634 tree elt = (i < in_nelts
1635 ? VECTOR_CST_ELT (arg1, i)
1636 : VECTOR_CST_ELT (arg2, i - in_nelts));
1637 elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1638 ? NOP_EXPR : FIX_TRUNC_EXPR,
1639 TREE_TYPE (type), elt);
1640 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1641 return NULL_TREE;
1642 elts.quick_push (elt);
1645 return elts.build ();
1648 case VEC_WIDEN_MULT_LO_EXPR:
1649 case VEC_WIDEN_MULT_HI_EXPR:
1650 case VEC_WIDEN_MULT_EVEN_EXPR:
1651 case VEC_WIDEN_MULT_ODD_EXPR:
1653 unsigned int out_nelts, in_nelts, out, ofs, scale;
1655 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1656 return NULL_TREE;
1658 in_nelts = VECTOR_CST_NELTS (arg1);
1659 out_nelts = in_nelts / 2;
1660 gcc_assert (in_nelts == VECTOR_CST_NELTS (arg2)
1661 && out_nelts == TYPE_VECTOR_SUBPARTS (type));
1663 if (code == VEC_WIDEN_MULT_LO_EXPR)
1664 scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1665 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1666 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1667 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1668 scale = 1, ofs = 0;
1669 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1670 scale = 1, ofs = 1;
1672 tree_vector_builder elts (type, out_nelts, 1);
1673 for (out = 0; out < out_nelts; out++)
1675 unsigned int in = (out << scale) + ofs;
1676 tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1677 VECTOR_CST_ELT (arg1, in));
1678 tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1679 VECTOR_CST_ELT (arg2, in));
1681 if (t1 == NULL_TREE || t2 == NULL_TREE)
1682 return NULL_TREE;
1683 tree elt = const_binop (MULT_EXPR, t1, t2);
1684 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1685 return NULL_TREE;
1686 elts.quick_push (elt);
1689 return elts.build ();
1692 default:;
1695 if (TREE_CODE_CLASS (code) != tcc_binary)
1696 return NULL_TREE;
1698 /* Make sure type and arg0 have the same saturating flag. */
1699 gcc_checking_assert (TYPE_SATURATING (type)
1700 == TYPE_SATURATING (TREE_TYPE (arg1)));
1702 return const_binop (code, arg1, arg2);
1705 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1706 Return zero if computing the constants is not possible. */
1708 tree
1709 const_unop (enum tree_code code, tree type, tree arg0)
1711 /* Don't perform the operation, other than NEGATE and ABS, if
1712 flag_signaling_nans is on and the operand is a signaling NaN. */
1713 if (TREE_CODE (arg0) == REAL_CST
1714 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1715 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1716 && code != NEGATE_EXPR
1717 && code != ABS_EXPR)
1718 return NULL_TREE;
1720 switch (code)
1722 CASE_CONVERT:
1723 case FLOAT_EXPR:
1724 case FIX_TRUNC_EXPR:
1725 case FIXED_CONVERT_EXPR:
1726 return fold_convert_const (code, type, arg0);
1728 case ADDR_SPACE_CONVERT_EXPR:
1729 /* If the source address is 0, and the source address space
1730 cannot have a valid object at 0, fold to dest type null. */
1731 if (integer_zerop (arg0)
1732 && !(targetm.addr_space.zero_address_valid
1733 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1734 return fold_convert_const (code, type, arg0);
1735 break;
1737 case VIEW_CONVERT_EXPR:
1738 return fold_view_convert_expr (type, arg0);
1740 case NEGATE_EXPR:
1742 /* Can't call fold_negate_const directly here as that doesn't
1743 handle all cases and we might not be able to negate some
1744 constants. */
1745 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1746 if (tem && CONSTANT_CLASS_P (tem))
1747 return tem;
1748 break;
1751 case ABS_EXPR:
1752 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1753 return fold_abs_const (arg0, type);
1754 break;
1756 case CONJ_EXPR:
1757 if (TREE_CODE (arg0) == COMPLEX_CST)
1759 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1760 TREE_TYPE (type));
1761 return build_complex (type, TREE_REALPART (arg0), ipart);
1763 break;
1765 case BIT_NOT_EXPR:
1766 if (TREE_CODE (arg0) == INTEGER_CST)
1767 return fold_not_const (arg0, type);
1768 else if (POLY_INT_CST_P (arg0))
1769 return wide_int_to_tree (type, -poly_int_cst_value (arg0));
1770 /* Perform BIT_NOT_EXPR on each element individually. */
1771 else if (TREE_CODE (arg0) == VECTOR_CST)
1773 tree elem;
1775 /* This can cope with stepped encodings because ~x == -1 - x. */
1776 tree_vector_builder elements;
1777 elements.new_unary_operation (type, arg0, true);
1778 unsigned int i, count = elements.encoded_nelts ();
1779 for (i = 0; i < count; ++i)
1781 elem = VECTOR_CST_ELT (arg0, i);
1782 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1783 if (elem == NULL_TREE)
1784 break;
1785 elements.quick_push (elem);
1787 if (i == count)
1788 return elements.build ();
1790 break;
1792 case TRUTH_NOT_EXPR:
1793 if (TREE_CODE (arg0) == INTEGER_CST)
1794 return constant_boolean_node (integer_zerop (arg0), type);
1795 break;
1797 case REALPART_EXPR:
1798 if (TREE_CODE (arg0) == COMPLEX_CST)
1799 return fold_convert (type, TREE_REALPART (arg0));
1800 break;
1802 case IMAGPART_EXPR:
1803 if (TREE_CODE (arg0) == COMPLEX_CST)
1804 return fold_convert (type, TREE_IMAGPART (arg0));
1805 break;
1807 case VEC_UNPACK_LO_EXPR:
1808 case VEC_UNPACK_HI_EXPR:
1809 case VEC_UNPACK_FLOAT_LO_EXPR:
1810 case VEC_UNPACK_FLOAT_HI_EXPR:
1812 unsigned int out_nelts, in_nelts, i;
1813 enum tree_code subcode;
1815 if (TREE_CODE (arg0) != VECTOR_CST)
1816 return NULL_TREE;
1818 in_nelts = VECTOR_CST_NELTS (arg0);
1819 out_nelts = in_nelts / 2;
1820 gcc_assert (out_nelts == TYPE_VECTOR_SUBPARTS (type));
1822 unsigned int offset = 0;
1823 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1824 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1825 offset = out_nelts;
1827 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1828 subcode = NOP_EXPR;
1829 else
1830 subcode = FLOAT_EXPR;
1832 tree_vector_builder elts (type, out_nelts, 1);
1833 for (i = 0; i < out_nelts; i++)
1835 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1836 VECTOR_CST_ELT (arg0, i + offset));
1837 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1838 return NULL_TREE;
1839 elts.quick_push (elt);
1842 return elts.build ();
1845 case VEC_DUPLICATE_EXPR:
1846 if (CONSTANT_CLASS_P (arg0))
1847 return build_vector_from_val (type, arg0);
1848 return NULL_TREE;
1850 default:
1851 break;
1854 return NULL_TREE;
1857 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1858 indicates which particular sizetype to create. */
1860 tree
1861 size_int_kind (poly_int64 number, enum size_type_kind kind)
1863 return build_int_cst (sizetype_tab[(int) kind], number);
1866 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1867 is a tree code. The type of the result is taken from the operands.
1868 Both must be equivalent integer types, ala int_binop_types_match_p.
1869 If the operands are constant, so is the result. */
1871 tree
1872 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1874 tree type = TREE_TYPE (arg0);
1876 if (arg0 == error_mark_node || arg1 == error_mark_node)
1877 return error_mark_node;
1879 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1880 TREE_TYPE (arg1)));
1882 /* Handle the special case of two poly_int constants faster. */
1883 if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1885 /* And some specific cases even faster than that. */
1886 if (code == PLUS_EXPR)
1888 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1889 return arg1;
1890 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1891 return arg0;
1893 else if (code == MINUS_EXPR)
1895 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1896 return arg0;
1898 else if (code == MULT_EXPR)
1900 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1901 return arg1;
1904 /* Handle general case of two integer constants. For sizetype
1905 constant calculations we always want to know about overflow,
1906 even in the unsigned case. */
1907 tree res = int_const_binop_1 (code, arg0, arg1, -1);
1908 if (res != NULL_TREE)
1909 return res;
1912 return fold_build2_loc (loc, code, type, arg0, arg1);
1915 /* Given two values, either both of sizetype or both of bitsizetype,
1916 compute the difference between the two values. Return the value
1917 in signed type corresponding to the type of the operands. */
1919 tree
1920 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1922 tree type = TREE_TYPE (arg0);
1923 tree ctype;
1925 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1926 TREE_TYPE (arg1)));
1928 /* If the type is already signed, just do the simple thing. */
1929 if (!TYPE_UNSIGNED (type))
1930 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1932 if (type == sizetype)
1933 ctype = ssizetype;
1934 else if (type == bitsizetype)
1935 ctype = sbitsizetype;
1936 else
1937 ctype = signed_type_for (type);
1939 /* If either operand is not a constant, do the conversions to the signed
1940 type and subtract. The hardware will do the right thing with any
1941 overflow in the subtraction. */
1942 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1943 return size_binop_loc (loc, MINUS_EXPR,
1944 fold_convert_loc (loc, ctype, arg0),
1945 fold_convert_loc (loc, ctype, arg1));
1947 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1948 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1949 overflow) and negate (which can't either). Special-case a result
1950 of zero while we're here. */
1951 if (tree_int_cst_equal (arg0, arg1))
1952 return build_int_cst (ctype, 0);
1953 else if (tree_int_cst_lt (arg1, arg0))
1954 return fold_convert_loc (loc, ctype,
1955 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1956 else
1957 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1958 fold_convert_loc (loc, ctype,
1959 size_binop_loc (loc,
1960 MINUS_EXPR,
1961 arg1, arg0)));
1964 /* A subroutine of fold_convert_const handling conversions of an
1965 INTEGER_CST to another integer type. */
1967 static tree
1968 fold_convert_const_int_from_int (tree type, const_tree arg1)
1970 /* Given an integer constant, make new constant with new type,
1971 appropriately sign-extended or truncated. Use widest_int
1972 so that any extension is done according ARG1's type. */
1973 return force_fit_type (type, wi::to_widest (arg1),
1974 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1975 TREE_OVERFLOW (arg1));
1978 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1979 to an integer type. */
1981 static tree
1982 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1984 bool overflow = false;
1985 tree t;
1987 /* The following code implements the floating point to integer
1988 conversion rules required by the Java Language Specification,
1989 that IEEE NaNs are mapped to zero and values that overflow
1990 the target precision saturate, i.e. values greater than
1991 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1992 are mapped to INT_MIN. These semantics are allowed by the
1993 C and C++ standards that simply state that the behavior of
1994 FP-to-integer conversion is unspecified upon overflow. */
1996 wide_int val;
1997 REAL_VALUE_TYPE r;
1998 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2000 switch (code)
2002 case FIX_TRUNC_EXPR:
2003 real_trunc (&r, VOIDmode, &x);
2004 break;
2006 default:
2007 gcc_unreachable ();
2010 /* If R is NaN, return zero and show we have an overflow. */
2011 if (REAL_VALUE_ISNAN (r))
2013 overflow = true;
2014 val = wi::zero (TYPE_PRECISION (type));
2017 /* See if R is less than the lower bound or greater than the
2018 upper bound. */
2020 if (! overflow)
2022 tree lt = TYPE_MIN_VALUE (type);
2023 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2024 if (real_less (&r, &l))
2026 overflow = true;
2027 val = wi::to_wide (lt);
2031 if (! overflow)
2033 tree ut = TYPE_MAX_VALUE (type);
2034 if (ut)
2036 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2037 if (real_less (&u, &r))
2039 overflow = true;
2040 val = wi::to_wide (ut);
2045 if (! overflow)
2046 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2048 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2049 return t;
2052 /* A subroutine of fold_convert_const handling conversions of a
2053 FIXED_CST to an integer type. */
2055 static tree
2056 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2058 tree t;
2059 double_int temp, temp_trunc;
2060 scalar_mode mode;
2062 /* Right shift FIXED_CST to temp by fbit. */
2063 temp = TREE_FIXED_CST (arg1).data;
2064 mode = TREE_FIXED_CST (arg1).mode;
2065 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2067 temp = temp.rshift (GET_MODE_FBIT (mode),
2068 HOST_BITS_PER_DOUBLE_INT,
2069 SIGNED_FIXED_POINT_MODE_P (mode));
2071 /* Left shift temp to temp_trunc by fbit. */
2072 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2073 HOST_BITS_PER_DOUBLE_INT,
2074 SIGNED_FIXED_POINT_MODE_P (mode));
2076 else
2078 temp = double_int_zero;
2079 temp_trunc = double_int_zero;
2082 /* If FIXED_CST is negative, we need to round the value toward 0.
2083 By checking if the fractional bits are not zero to add 1 to temp. */
2084 if (SIGNED_FIXED_POINT_MODE_P (mode)
2085 && temp_trunc.is_negative ()
2086 && TREE_FIXED_CST (arg1).data != temp_trunc)
2087 temp += double_int_one;
2089 /* Given a fixed-point constant, make new constant with new type,
2090 appropriately sign-extended or truncated. */
2091 t = force_fit_type (type, temp, -1,
2092 (temp.is_negative ()
2093 && (TYPE_UNSIGNED (type)
2094 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2095 | TREE_OVERFLOW (arg1));
2097 return t;
2100 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2101 to another floating point type. */
2103 static tree
2104 fold_convert_const_real_from_real (tree type, const_tree arg1)
2106 REAL_VALUE_TYPE value;
2107 tree t;
2109 /* Don't perform the operation if flag_signaling_nans is on
2110 and the operand is a signaling NaN. */
2111 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2112 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2113 return NULL_TREE;
2115 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2116 t = build_real (type, value);
2118 /* If converting an infinity or NAN to a representation that doesn't
2119 have one, set the overflow bit so that we can produce some kind of
2120 error message at the appropriate point if necessary. It's not the
2121 most user-friendly message, but it's better than nothing. */
2122 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2123 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2124 TREE_OVERFLOW (t) = 1;
2125 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2126 && !MODE_HAS_NANS (TYPE_MODE (type)))
2127 TREE_OVERFLOW (t) = 1;
2128 /* Regular overflow, conversion produced an infinity in a mode that
2129 can't represent them. */
2130 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2131 && REAL_VALUE_ISINF (value)
2132 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2133 TREE_OVERFLOW (t) = 1;
2134 else
2135 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2136 return t;
2139 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2140 to a floating point type. */
2142 static tree
2143 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2145 REAL_VALUE_TYPE value;
2146 tree t;
2148 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2149 &TREE_FIXED_CST (arg1));
2150 t = build_real (type, value);
2152 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2153 return t;
2156 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2157 to another fixed-point type. */
2159 static tree
2160 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2162 FIXED_VALUE_TYPE value;
2163 tree t;
2164 bool overflow_p;
2166 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2167 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2168 t = build_fixed (type, value);
2170 /* Propagate overflow flags. */
2171 if (overflow_p | TREE_OVERFLOW (arg1))
2172 TREE_OVERFLOW (t) = 1;
2173 return t;
2176 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2177 to a fixed-point type. */
2179 static tree
2180 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2182 FIXED_VALUE_TYPE value;
2183 tree t;
2184 bool overflow_p;
2185 double_int di;
2187 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2189 di.low = TREE_INT_CST_ELT (arg1, 0);
2190 if (TREE_INT_CST_NUNITS (arg1) == 1)
2191 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2192 else
2193 di.high = TREE_INT_CST_ELT (arg1, 1);
2195 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2196 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2197 TYPE_SATURATING (type));
2198 t = build_fixed (type, value);
2200 /* Propagate overflow flags. */
2201 if (overflow_p | TREE_OVERFLOW (arg1))
2202 TREE_OVERFLOW (t) = 1;
2203 return t;
2206 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2207 to a fixed-point type. */
2209 static tree
2210 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2212 FIXED_VALUE_TYPE value;
2213 tree t;
2214 bool overflow_p;
2216 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2217 &TREE_REAL_CST (arg1),
2218 TYPE_SATURATING (type));
2219 t = build_fixed (type, value);
2221 /* Propagate overflow flags. */
2222 if (overflow_p | TREE_OVERFLOW (arg1))
2223 TREE_OVERFLOW (t) = 1;
2224 return t;
2227 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2228 type TYPE. If no simplification can be done return NULL_TREE. */
2230 static tree
2231 fold_convert_const (enum tree_code code, tree type, tree arg1)
2233 tree arg_type = TREE_TYPE (arg1);
2234 if (arg_type == type)
2235 return arg1;
2237 /* We can't widen types, since the runtime value could overflow the
2238 original type before being extended to the new type. */
2239 if (POLY_INT_CST_P (arg1)
2240 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2241 && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2242 return build_poly_int_cst (type,
2243 poly_wide_int::from (poly_int_cst_value (arg1),
2244 TYPE_PRECISION (type),
2245 TYPE_SIGN (arg_type)));
2247 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2248 || TREE_CODE (type) == OFFSET_TYPE)
2250 if (TREE_CODE (arg1) == INTEGER_CST)
2251 return fold_convert_const_int_from_int (type, arg1);
2252 else if (TREE_CODE (arg1) == REAL_CST)
2253 return fold_convert_const_int_from_real (code, type, arg1);
2254 else if (TREE_CODE (arg1) == FIXED_CST)
2255 return fold_convert_const_int_from_fixed (type, arg1);
2257 else if (TREE_CODE (type) == REAL_TYPE)
2259 if (TREE_CODE (arg1) == INTEGER_CST)
2260 return build_real_from_int_cst (type, arg1);
2261 else if (TREE_CODE (arg1) == REAL_CST)
2262 return fold_convert_const_real_from_real (type, arg1);
2263 else if (TREE_CODE (arg1) == FIXED_CST)
2264 return fold_convert_const_real_from_fixed (type, arg1);
2266 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2268 if (TREE_CODE (arg1) == FIXED_CST)
2269 return fold_convert_const_fixed_from_fixed (type, arg1);
2270 else if (TREE_CODE (arg1) == INTEGER_CST)
2271 return fold_convert_const_fixed_from_int (type, arg1);
2272 else if (TREE_CODE (arg1) == REAL_CST)
2273 return fold_convert_const_fixed_from_real (type, arg1);
2275 else if (TREE_CODE (type) == VECTOR_TYPE)
2277 if (TREE_CODE (arg1) == VECTOR_CST
2278 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2280 tree elttype = TREE_TYPE (type);
2281 tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2282 /* We can't handle steps directly when extending, since the
2283 values need to wrap at the original precision first. */
2284 bool step_ok_p
2285 = (INTEGRAL_TYPE_P (elttype)
2286 && INTEGRAL_TYPE_P (arg1_elttype)
2287 && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2288 tree_vector_builder v;
2289 if (!v.new_unary_operation (type, arg1, step_ok_p))
2290 return NULL_TREE;
2291 unsigned int len = v.encoded_nelts ();
2292 for (unsigned int i = 0; i < len; ++i)
2294 tree elt = VECTOR_CST_ELT (arg1, i);
2295 tree cvt = fold_convert_const (code, elttype, elt);
2296 if (cvt == NULL_TREE)
2297 return NULL_TREE;
2298 v.quick_push (cvt);
2300 return v.build ();
2303 return NULL_TREE;
2306 /* Construct a vector of zero elements of vector type TYPE. */
2308 static tree
2309 build_zero_vector (tree type)
2311 tree t;
2313 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2314 return build_vector_from_val (type, t);
2317 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2319 bool
2320 fold_convertible_p (const_tree type, const_tree arg)
2322 tree orig = TREE_TYPE (arg);
2324 if (type == orig)
2325 return true;
2327 if (TREE_CODE (arg) == ERROR_MARK
2328 || TREE_CODE (type) == ERROR_MARK
2329 || TREE_CODE (orig) == ERROR_MARK)
2330 return false;
2332 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2333 return true;
2335 switch (TREE_CODE (type))
2337 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2338 case POINTER_TYPE: case REFERENCE_TYPE:
2339 case OFFSET_TYPE:
2340 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2341 || TREE_CODE (orig) == OFFSET_TYPE);
2343 case REAL_TYPE:
2344 case FIXED_POINT_TYPE:
2345 case VECTOR_TYPE:
2346 case VOID_TYPE:
2347 return TREE_CODE (type) == TREE_CODE (orig);
2349 default:
2350 return false;
2354 /* Convert expression ARG to type TYPE. Used by the middle-end for
2355 simple conversions in preference to calling the front-end's convert. */
2357 tree
2358 fold_convert_loc (location_t loc, tree type, tree arg)
2360 tree orig = TREE_TYPE (arg);
2361 tree tem;
2363 if (type == orig)
2364 return arg;
2366 if (TREE_CODE (arg) == ERROR_MARK
2367 || TREE_CODE (type) == ERROR_MARK
2368 || TREE_CODE (orig) == ERROR_MARK)
2369 return error_mark_node;
2371 switch (TREE_CODE (type))
2373 case POINTER_TYPE:
2374 case REFERENCE_TYPE:
2375 /* Handle conversions between pointers to different address spaces. */
2376 if (POINTER_TYPE_P (orig)
2377 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2378 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2379 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2380 /* fall through */
2382 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2383 case OFFSET_TYPE:
2384 if (TREE_CODE (arg) == INTEGER_CST)
2386 tem = fold_convert_const (NOP_EXPR, type, arg);
2387 if (tem != NULL_TREE)
2388 return tem;
2390 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2391 || TREE_CODE (orig) == OFFSET_TYPE)
2392 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2393 if (TREE_CODE (orig) == COMPLEX_TYPE)
2394 return fold_convert_loc (loc, type,
2395 fold_build1_loc (loc, REALPART_EXPR,
2396 TREE_TYPE (orig), arg));
2397 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2398 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2399 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2401 case REAL_TYPE:
2402 if (TREE_CODE (arg) == INTEGER_CST)
2404 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2405 if (tem != NULL_TREE)
2406 return tem;
2408 else if (TREE_CODE (arg) == REAL_CST)
2410 tem = fold_convert_const (NOP_EXPR, type, arg);
2411 if (tem != NULL_TREE)
2412 return tem;
2414 else if (TREE_CODE (arg) == FIXED_CST)
2416 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2417 if (tem != NULL_TREE)
2418 return tem;
2421 switch (TREE_CODE (orig))
2423 case INTEGER_TYPE:
2424 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2425 case POINTER_TYPE: case REFERENCE_TYPE:
2426 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2428 case REAL_TYPE:
2429 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2431 case FIXED_POINT_TYPE:
2432 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2434 case COMPLEX_TYPE:
2435 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2436 return fold_convert_loc (loc, type, tem);
2438 default:
2439 gcc_unreachable ();
2442 case FIXED_POINT_TYPE:
2443 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2444 || TREE_CODE (arg) == REAL_CST)
2446 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2447 if (tem != NULL_TREE)
2448 goto fold_convert_exit;
2451 switch (TREE_CODE (orig))
2453 case FIXED_POINT_TYPE:
2454 case INTEGER_TYPE:
2455 case ENUMERAL_TYPE:
2456 case BOOLEAN_TYPE:
2457 case REAL_TYPE:
2458 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2460 case COMPLEX_TYPE:
2461 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2462 return fold_convert_loc (loc, type, tem);
2464 default:
2465 gcc_unreachable ();
2468 case COMPLEX_TYPE:
2469 switch (TREE_CODE (orig))
2471 case INTEGER_TYPE:
2472 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2473 case POINTER_TYPE: case REFERENCE_TYPE:
2474 case REAL_TYPE:
2475 case FIXED_POINT_TYPE:
2476 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2477 fold_convert_loc (loc, TREE_TYPE (type), arg),
2478 fold_convert_loc (loc, TREE_TYPE (type),
2479 integer_zero_node));
2480 case COMPLEX_TYPE:
2482 tree rpart, ipart;
2484 if (TREE_CODE (arg) == COMPLEX_EXPR)
2486 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2487 TREE_OPERAND (arg, 0));
2488 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2489 TREE_OPERAND (arg, 1));
2490 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2493 arg = save_expr (arg);
2494 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2495 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2496 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2497 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2498 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2501 default:
2502 gcc_unreachable ();
2505 case VECTOR_TYPE:
2506 if (integer_zerop (arg))
2507 return build_zero_vector (type);
2508 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2509 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2510 || TREE_CODE (orig) == VECTOR_TYPE);
2511 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2513 case VOID_TYPE:
2514 tem = fold_ignored_result (arg);
2515 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2517 default:
2518 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2519 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2520 gcc_unreachable ();
2522 fold_convert_exit:
2523 protected_set_expr_location_unshare (tem, loc);
2524 return tem;
2527 /* Return false if expr can be assumed not to be an lvalue, true
2528 otherwise. */
2530 static bool
2531 maybe_lvalue_p (const_tree x)
2533 /* We only need to wrap lvalue tree codes. */
2534 switch (TREE_CODE (x))
2536 case VAR_DECL:
2537 case PARM_DECL:
2538 case RESULT_DECL:
2539 case LABEL_DECL:
2540 case FUNCTION_DECL:
2541 case SSA_NAME:
2543 case COMPONENT_REF:
2544 case MEM_REF:
2545 case INDIRECT_REF:
2546 case ARRAY_REF:
2547 case ARRAY_RANGE_REF:
2548 case BIT_FIELD_REF:
2549 case OBJ_TYPE_REF:
2551 case REALPART_EXPR:
2552 case IMAGPART_EXPR:
2553 case PREINCREMENT_EXPR:
2554 case PREDECREMENT_EXPR:
2555 case SAVE_EXPR:
2556 case TRY_CATCH_EXPR:
2557 case WITH_CLEANUP_EXPR:
2558 case COMPOUND_EXPR:
2559 case MODIFY_EXPR:
2560 case TARGET_EXPR:
2561 case COND_EXPR:
2562 case BIND_EXPR:
2563 break;
2565 default:
2566 /* Assume the worst for front-end tree codes. */
2567 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2568 break;
2569 return false;
2572 return true;
2575 /* Return an expr equal to X but certainly not valid as an lvalue. */
2577 tree
2578 non_lvalue_loc (location_t loc, tree x)
2580 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2581 us. */
2582 if (in_gimple_form)
2583 return x;
2585 if (! maybe_lvalue_p (x))
2586 return x;
2587 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2590 /* When pedantic, return an expr equal to X but certainly not valid as a
2591 pedantic lvalue. Otherwise, return X. */
2593 static tree
2594 pedantic_non_lvalue_loc (location_t loc, tree x)
2596 return protected_set_expr_location_unshare (x, loc);
2599 /* Given a tree comparison code, return the code that is the logical inverse.
2600 It is generally not safe to do this for floating-point comparisons, except
2601 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2602 ERROR_MARK in this case. */
2604 enum tree_code
2605 invert_tree_comparison (enum tree_code code, bool honor_nans)
2607 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2608 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2609 return ERROR_MARK;
2611 switch (code)
2613 case EQ_EXPR:
2614 return NE_EXPR;
2615 case NE_EXPR:
2616 return EQ_EXPR;
2617 case GT_EXPR:
2618 return honor_nans ? UNLE_EXPR : LE_EXPR;
2619 case GE_EXPR:
2620 return honor_nans ? UNLT_EXPR : LT_EXPR;
2621 case LT_EXPR:
2622 return honor_nans ? UNGE_EXPR : GE_EXPR;
2623 case LE_EXPR:
2624 return honor_nans ? UNGT_EXPR : GT_EXPR;
2625 case LTGT_EXPR:
2626 return UNEQ_EXPR;
2627 case UNEQ_EXPR:
2628 return LTGT_EXPR;
2629 case UNGT_EXPR:
2630 return LE_EXPR;
2631 case UNGE_EXPR:
2632 return LT_EXPR;
2633 case UNLT_EXPR:
2634 return GE_EXPR;
2635 case UNLE_EXPR:
2636 return GT_EXPR;
2637 case ORDERED_EXPR:
2638 return UNORDERED_EXPR;
2639 case UNORDERED_EXPR:
2640 return ORDERED_EXPR;
2641 default:
2642 gcc_unreachable ();
2646 /* Similar, but return the comparison that results if the operands are
2647 swapped. This is safe for floating-point. */
2649 enum tree_code
2650 swap_tree_comparison (enum tree_code code)
2652 switch (code)
2654 case EQ_EXPR:
2655 case NE_EXPR:
2656 case ORDERED_EXPR:
2657 case UNORDERED_EXPR:
2658 case LTGT_EXPR:
2659 case UNEQ_EXPR:
2660 return code;
2661 case GT_EXPR:
2662 return LT_EXPR;
2663 case GE_EXPR:
2664 return LE_EXPR;
2665 case LT_EXPR:
2666 return GT_EXPR;
2667 case LE_EXPR:
2668 return GE_EXPR;
2669 case UNGT_EXPR:
2670 return UNLT_EXPR;
2671 case UNGE_EXPR:
2672 return UNLE_EXPR;
2673 case UNLT_EXPR:
2674 return UNGT_EXPR;
2675 case UNLE_EXPR:
2676 return UNGE_EXPR;
2677 default:
2678 gcc_unreachable ();
2683 /* Convert a comparison tree code from an enum tree_code representation
2684 into a compcode bit-based encoding. This function is the inverse of
2685 compcode_to_comparison. */
2687 static enum comparison_code
2688 comparison_to_compcode (enum tree_code code)
2690 switch (code)
2692 case LT_EXPR:
2693 return COMPCODE_LT;
2694 case EQ_EXPR:
2695 return COMPCODE_EQ;
2696 case LE_EXPR:
2697 return COMPCODE_LE;
2698 case GT_EXPR:
2699 return COMPCODE_GT;
2700 case NE_EXPR:
2701 return COMPCODE_NE;
2702 case GE_EXPR:
2703 return COMPCODE_GE;
2704 case ORDERED_EXPR:
2705 return COMPCODE_ORD;
2706 case UNORDERED_EXPR:
2707 return COMPCODE_UNORD;
2708 case UNLT_EXPR:
2709 return COMPCODE_UNLT;
2710 case UNEQ_EXPR:
2711 return COMPCODE_UNEQ;
2712 case UNLE_EXPR:
2713 return COMPCODE_UNLE;
2714 case UNGT_EXPR:
2715 return COMPCODE_UNGT;
2716 case LTGT_EXPR:
2717 return COMPCODE_LTGT;
2718 case UNGE_EXPR:
2719 return COMPCODE_UNGE;
2720 default:
2721 gcc_unreachable ();
2725 /* Convert a compcode bit-based encoding of a comparison operator back
2726 to GCC's enum tree_code representation. This function is the
2727 inverse of comparison_to_compcode. */
2729 static enum tree_code
2730 compcode_to_comparison (enum comparison_code code)
2732 switch (code)
2734 case COMPCODE_LT:
2735 return LT_EXPR;
2736 case COMPCODE_EQ:
2737 return EQ_EXPR;
2738 case COMPCODE_LE:
2739 return LE_EXPR;
2740 case COMPCODE_GT:
2741 return GT_EXPR;
2742 case COMPCODE_NE:
2743 return NE_EXPR;
2744 case COMPCODE_GE:
2745 return GE_EXPR;
2746 case COMPCODE_ORD:
2747 return ORDERED_EXPR;
2748 case COMPCODE_UNORD:
2749 return UNORDERED_EXPR;
2750 case COMPCODE_UNLT:
2751 return UNLT_EXPR;
2752 case COMPCODE_UNEQ:
2753 return UNEQ_EXPR;
2754 case COMPCODE_UNLE:
2755 return UNLE_EXPR;
2756 case COMPCODE_UNGT:
2757 return UNGT_EXPR;
2758 case COMPCODE_LTGT:
2759 return LTGT_EXPR;
2760 case COMPCODE_UNGE:
2761 return UNGE_EXPR;
2762 default:
2763 gcc_unreachable ();
2767 /* Return a tree for the comparison which is the combination of
2768 doing the AND or OR (depending on CODE) of the two operations LCODE
2769 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2770 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2771 if this makes the transformation invalid. */
2773 tree
2774 combine_comparisons (location_t loc,
2775 enum tree_code code, enum tree_code lcode,
2776 enum tree_code rcode, tree truth_type,
2777 tree ll_arg, tree lr_arg)
2779 bool honor_nans = HONOR_NANS (ll_arg);
2780 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2781 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2782 int compcode;
2784 switch (code)
2786 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2787 compcode = lcompcode & rcompcode;
2788 break;
2790 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2791 compcode = lcompcode | rcompcode;
2792 break;
2794 default:
2795 return NULL_TREE;
2798 if (!honor_nans)
2800 /* Eliminate unordered comparisons, as well as LTGT and ORD
2801 which are not used unless the mode has NaNs. */
2802 compcode &= ~COMPCODE_UNORD;
2803 if (compcode == COMPCODE_LTGT)
2804 compcode = COMPCODE_NE;
2805 else if (compcode == COMPCODE_ORD)
2806 compcode = COMPCODE_TRUE;
2808 else if (flag_trapping_math)
2810 /* Check that the original operation and the optimized ones will trap
2811 under the same condition. */
2812 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2813 && (lcompcode != COMPCODE_EQ)
2814 && (lcompcode != COMPCODE_ORD);
2815 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2816 && (rcompcode != COMPCODE_EQ)
2817 && (rcompcode != COMPCODE_ORD);
2818 bool trap = (compcode & COMPCODE_UNORD) == 0
2819 && (compcode != COMPCODE_EQ)
2820 && (compcode != COMPCODE_ORD);
2822 /* In a short-circuited boolean expression the LHS might be
2823 such that the RHS, if evaluated, will never trap. For
2824 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2825 if neither x nor y is NaN. (This is a mixed blessing: for
2826 example, the expression above will never trap, hence
2827 optimizing it to x < y would be invalid). */
2828 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2829 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2830 rtrap = false;
2832 /* If the comparison was short-circuited, and only the RHS
2833 trapped, we may now generate a spurious trap. */
2834 if (rtrap && !ltrap
2835 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2836 return NULL_TREE;
2838 /* If we changed the conditions that cause a trap, we lose. */
2839 if ((ltrap || rtrap) != trap)
2840 return NULL_TREE;
2843 if (compcode == COMPCODE_TRUE)
2844 return constant_boolean_node (true, truth_type);
2845 else if (compcode == COMPCODE_FALSE)
2846 return constant_boolean_node (false, truth_type);
2847 else
2849 enum tree_code tcode;
2851 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2852 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2856 /* Return nonzero if two operands (typically of the same tree node)
2857 are necessarily equal. FLAGS modifies behavior as follows:
2859 If OEP_ONLY_CONST is set, only return nonzero for constants.
2860 This function tests whether the operands are indistinguishable;
2861 it does not test whether they are equal using C's == operation.
2862 The distinction is important for IEEE floating point, because
2863 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2864 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2866 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2867 even though it may hold multiple values during a function.
2868 This is because a GCC tree node guarantees that nothing else is
2869 executed between the evaluation of its "operands" (which may often
2870 be evaluated in arbitrary order). Hence if the operands themselves
2871 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2872 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2873 unset means assuming isochronic (or instantaneous) tree equivalence.
2874 Unless comparing arbitrary expression trees, such as from different
2875 statements, this flag can usually be left unset.
2877 If OEP_PURE_SAME is set, then pure functions with identical arguments
2878 are considered the same. It is used when the caller has other ways
2879 to ensure that global memory is unchanged in between.
2881 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2882 not values of expressions.
2884 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2885 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2887 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2888 any operand with side effect. This is unnecesarily conservative in the
2889 case we know that arg0 and arg1 are in disjoint code paths (such as in
2890 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2891 addresses with TREE_CONSTANT flag set so we know that &var == &var
2892 even if var is volatile. */
2895 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2897 /* When checking, verify at the outermost operand_equal_p call that
2898 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2899 hash value. */
2900 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2902 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2904 if (arg0 != arg1)
2906 inchash::hash hstate0 (0), hstate1 (0);
2907 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2908 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2909 hashval_t h0 = hstate0.end ();
2910 hashval_t h1 = hstate1.end ();
2911 gcc_assert (h0 == h1);
2913 return 1;
2915 else
2916 return 0;
2919 /* If either is ERROR_MARK, they aren't equal. */
2920 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2921 || TREE_TYPE (arg0) == error_mark_node
2922 || TREE_TYPE (arg1) == error_mark_node)
2923 return 0;
2925 /* Similar, if either does not have a type (like a released SSA name),
2926 they aren't equal. */
2927 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2928 return 0;
2930 /* We cannot consider pointers to different address space equal. */
2931 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2932 && POINTER_TYPE_P (TREE_TYPE (arg1))
2933 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2934 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2935 return 0;
2937 /* Check equality of integer constants before bailing out due to
2938 precision differences. */
2939 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2941 /* Address of INTEGER_CST is not defined; check that we did not forget
2942 to drop the OEP_ADDRESS_OF flags. */
2943 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2944 return tree_int_cst_equal (arg0, arg1);
2947 if (!(flags & OEP_ADDRESS_OF))
2949 /* If both types don't have the same signedness, then we can't consider
2950 them equal. We must check this before the STRIP_NOPS calls
2951 because they may change the signedness of the arguments. As pointers
2952 strictly don't have a signedness, require either two pointers or
2953 two non-pointers as well. */
2954 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2955 || POINTER_TYPE_P (TREE_TYPE (arg0))
2956 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2957 return 0;
2959 /* If both types don't have the same precision, then it is not safe
2960 to strip NOPs. */
2961 if (element_precision (TREE_TYPE (arg0))
2962 != element_precision (TREE_TYPE (arg1)))
2963 return 0;
2965 STRIP_NOPS (arg0);
2966 STRIP_NOPS (arg1);
2968 #if 0
2969 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2970 sanity check once the issue is solved. */
2971 else
2972 /* Addresses of conversions and SSA_NAMEs (and many other things)
2973 are not defined. Check that we did not forget to drop the
2974 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2975 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2976 && TREE_CODE (arg0) != SSA_NAME);
2977 #endif
2979 /* In case both args are comparisons but with different comparison
2980 code, try to swap the comparison operands of one arg to produce
2981 a match and compare that variant. */
2982 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2983 && COMPARISON_CLASS_P (arg0)
2984 && COMPARISON_CLASS_P (arg1))
2986 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2988 if (TREE_CODE (arg0) == swap_code)
2989 return operand_equal_p (TREE_OPERAND (arg0, 0),
2990 TREE_OPERAND (arg1, 1), flags)
2991 && operand_equal_p (TREE_OPERAND (arg0, 1),
2992 TREE_OPERAND (arg1, 0), flags);
2995 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2997 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2998 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3000 else if (flags & OEP_ADDRESS_OF)
3002 /* If we are interested in comparing addresses ignore
3003 MEM_REF wrappings of the base that can appear just for
3004 TBAA reasons. */
3005 if (TREE_CODE (arg0) == MEM_REF
3006 && DECL_P (arg1)
3007 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3008 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3009 && integer_zerop (TREE_OPERAND (arg0, 1)))
3010 return 1;
3011 else if (TREE_CODE (arg1) == MEM_REF
3012 && DECL_P (arg0)
3013 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3014 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3015 && integer_zerop (TREE_OPERAND (arg1, 1)))
3016 return 1;
3017 return 0;
3019 else
3020 return 0;
3023 /* When not checking adddresses, this is needed for conversions and for
3024 COMPONENT_REF. Might as well play it safe and always test this. */
3025 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3026 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3027 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
3028 && !(flags & OEP_ADDRESS_OF)))
3029 return 0;
3031 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3032 We don't care about side effects in that case because the SAVE_EXPR
3033 takes care of that for us. In all other cases, two expressions are
3034 equal if they have no side effects. If we have two identical
3035 expressions with side effects that should be treated the same due
3036 to the only side effects being identical SAVE_EXPR's, that will
3037 be detected in the recursive calls below.
3038 If we are taking an invariant address of two identical objects
3039 they are necessarily equal as well. */
3040 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3041 && (TREE_CODE (arg0) == SAVE_EXPR
3042 || (flags & OEP_MATCH_SIDE_EFFECTS)
3043 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3044 return 1;
3046 /* Next handle constant cases, those for which we can return 1 even
3047 if ONLY_CONST is set. */
3048 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3049 switch (TREE_CODE (arg0))
3051 case INTEGER_CST:
3052 return tree_int_cst_equal (arg0, arg1);
3054 case FIXED_CST:
3055 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3056 TREE_FIXED_CST (arg1));
3058 case REAL_CST:
3059 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3060 return 1;
3063 if (!HONOR_SIGNED_ZEROS (arg0))
3065 /* If we do not distinguish between signed and unsigned zero,
3066 consider them equal. */
3067 if (real_zerop (arg0) && real_zerop (arg1))
3068 return 1;
3070 return 0;
3072 case VECTOR_CST:
3074 if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3075 != VECTOR_CST_LOG2_NPATTERNS (arg1))
3076 return 0;
3078 if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3079 != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3080 return 0;
3082 unsigned int count = vector_cst_encoded_nelts (arg0);
3083 for (unsigned int i = 0; i < count; ++i)
3084 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3085 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3086 return 0;
3087 return 1;
3090 case COMPLEX_CST:
3091 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3092 flags)
3093 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3094 flags));
3096 case STRING_CST:
3097 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3098 && ! memcmp (TREE_STRING_POINTER (arg0),
3099 TREE_STRING_POINTER (arg1),
3100 TREE_STRING_LENGTH (arg0)));
3102 case ADDR_EXPR:
3103 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3104 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3105 flags | OEP_ADDRESS_OF
3106 | OEP_MATCH_SIDE_EFFECTS);
3107 case CONSTRUCTOR:
3108 /* In GIMPLE empty constructors are allowed in initializers of
3109 aggregates. */
3110 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
3111 default:
3112 break;
3115 if (flags & OEP_ONLY_CONST)
3116 return 0;
3118 /* Define macros to test an operand from arg0 and arg1 for equality and a
3119 variant that allows null and views null as being different from any
3120 non-null value. In the latter case, if either is null, the both
3121 must be; otherwise, do the normal comparison. */
3122 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3123 TREE_OPERAND (arg1, N), flags)
3125 #define OP_SAME_WITH_NULL(N) \
3126 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3127 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3129 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3131 case tcc_unary:
3132 /* Two conversions are equal only if signedness and modes match. */
3133 switch (TREE_CODE (arg0))
3135 CASE_CONVERT:
3136 case FIX_TRUNC_EXPR:
3137 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3138 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3139 return 0;
3140 break;
3141 default:
3142 break;
3145 return OP_SAME (0);
3148 case tcc_comparison:
3149 case tcc_binary:
3150 if (OP_SAME (0) && OP_SAME (1))
3151 return 1;
3153 /* For commutative ops, allow the other order. */
3154 return (commutative_tree_code (TREE_CODE (arg0))
3155 && operand_equal_p (TREE_OPERAND (arg0, 0),
3156 TREE_OPERAND (arg1, 1), flags)
3157 && operand_equal_p (TREE_OPERAND (arg0, 1),
3158 TREE_OPERAND (arg1, 0), flags));
3160 case tcc_reference:
3161 /* If either of the pointer (or reference) expressions we are
3162 dereferencing contain a side effect, these cannot be equal,
3163 but their addresses can be. */
3164 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3165 && (TREE_SIDE_EFFECTS (arg0)
3166 || TREE_SIDE_EFFECTS (arg1)))
3167 return 0;
3169 switch (TREE_CODE (arg0))
3171 case INDIRECT_REF:
3172 if (!(flags & OEP_ADDRESS_OF)
3173 && (TYPE_ALIGN (TREE_TYPE (arg0))
3174 != TYPE_ALIGN (TREE_TYPE (arg1))))
3175 return 0;
3176 flags &= ~OEP_ADDRESS_OF;
3177 return OP_SAME (0);
3179 case IMAGPART_EXPR:
3180 /* Require the same offset. */
3181 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3182 TYPE_SIZE (TREE_TYPE (arg1)),
3183 flags & ~OEP_ADDRESS_OF))
3184 return 0;
3186 /* Fallthru. */
3187 case REALPART_EXPR:
3188 case VIEW_CONVERT_EXPR:
3189 return OP_SAME (0);
3191 case TARGET_MEM_REF:
3192 case MEM_REF:
3193 if (!(flags & OEP_ADDRESS_OF))
3195 /* Require equal access sizes */
3196 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3197 && (!TYPE_SIZE (TREE_TYPE (arg0))
3198 || !TYPE_SIZE (TREE_TYPE (arg1))
3199 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3200 TYPE_SIZE (TREE_TYPE (arg1)),
3201 flags)))
3202 return 0;
3203 /* Verify that access happens in similar types. */
3204 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3205 return 0;
3206 /* Verify that accesses are TBAA compatible. */
3207 if (!alias_ptr_types_compatible_p
3208 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3209 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3210 || (MR_DEPENDENCE_CLIQUE (arg0)
3211 != MR_DEPENDENCE_CLIQUE (arg1))
3212 || (MR_DEPENDENCE_BASE (arg0)
3213 != MR_DEPENDENCE_BASE (arg1)))
3214 return 0;
3215 /* Verify that alignment is compatible. */
3216 if (TYPE_ALIGN (TREE_TYPE (arg0))
3217 != TYPE_ALIGN (TREE_TYPE (arg1)))
3218 return 0;
3220 flags &= ~OEP_ADDRESS_OF;
3221 return (OP_SAME (0) && OP_SAME (1)
3222 /* TARGET_MEM_REF require equal extra operands. */
3223 && (TREE_CODE (arg0) != TARGET_MEM_REF
3224 || (OP_SAME_WITH_NULL (2)
3225 && OP_SAME_WITH_NULL (3)
3226 && OP_SAME_WITH_NULL (4))));
3228 case ARRAY_REF:
3229 case ARRAY_RANGE_REF:
3230 if (!OP_SAME (0))
3231 return 0;
3232 flags &= ~OEP_ADDRESS_OF;
3233 /* Compare the array index by value if it is constant first as we
3234 may have different types but same value here. */
3235 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3236 TREE_OPERAND (arg1, 1))
3237 || OP_SAME (1))
3238 && OP_SAME_WITH_NULL (2)
3239 && OP_SAME_WITH_NULL (3)
3240 /* Compare low bound and element size as with OEP_ADDRESS_OF
3241 we have to account for the offset of the ref. */
3242 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3243 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3244 || (operand_equal_p (array_ref_low_bound
3245 (CONST_CAST_TREE (arg0)),
3246 array_ref_low_bound
3247 (CONST_CAST_TREE (arg1)), flags)
3248 && operand_equal_p (array_ref_element_size
3249 (CONST_CAST_TREE (arg0)),
3250 array_ref_element_size
3251 (CONST_CAST_TREE (arg1)),
3252 flags))));
3254 case COMPONENT_REF:
3255 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3256 may be NULL when we're called to compare MEM_EXPRs. */
3257 if (!OP_SAME_WITH_NULL (0)
3258 || !OP_SAME (1))
3259 return 0;
3260 flags &= ~OEP_ADDRESS_OF;
3261 return OP_SAME_WITH_NULL (2);
3263 case BIT_FIELD_REF:
3264 if (!OP_SAME (0))
3265 return 0;
3266 flags &= ~OEP_ADDRESS_OF;
3267 return OP_SAME (1) && OP_SAME (2);
3269 default:
3270 return 0;
3273 case tcc_expression:
3274 switch (TREE_CODE (arg0))
3276 case ADDR_EXPR:
3277 /* Be sure we pass right ADDRESS_OF flag. */
3278 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3279 return operand_equal_p (TREE_OPERAND (arg0, 0),
3280 TREE_OPERAND (arg1, 0),
3281 flags | OEP_ADDRESS_OF);
3283 case TRUTH_NOT_EXPR:
3284 return OP_SAME (0);
3286 case TRUTH_ANDIF_EXPR:
3287 case TRUTH_ORIF_EXPR:
3288 return OP_SAME (0) && OP_SAME (1);
3290 case FMA_EXPR:
3291 case WIDEN_MULT_PLUS_EXPR:
3292 case WIDEN_MULT_MINUS_EXPR:
3293 if (!OP_SAME (2))
3294 return 0;
3295 /* The multiplcation operands are commutative. */
3296 /* FALLTHRU */
3298 case TRUTH_AND_EXPR:
3299 case TRUTH_OR_EXPR:
3300 case TRUTH_XOR_EXPR:
3301 if (OP_SAME (0) && OP_SAME (1))
3302 return 1;
3304 /* Otherwise take into account this is a commutative operation. */
3305 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3306 TREE_OPERAND (arg1, 1), flags)
3307 && operand_equal_p (TREE_OPERAND (arg0, 1),
3308 TREE_OPERAND (arg1, 0), flags));
3310 case COND_EXPR:
3311 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3312 return 0;
3313 flags &= ~OEP_ADDRESS_OF;
3314 return OP_SAME (0);
3316 case BIT_INSERT_EXPR:
3317 /* BIT_INSERT_EXPR has an implict operand as the type precision
3318 of op1. Need to check to make sure they are the same. */
3319 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3320 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3321 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3322 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3323 return false;
3324 /* FALLTHRU */
3326 case VEC_COND_EXPR:
3327 case DOT_PROD_EXPR:
3328 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3330 case MODIFY_EXPR:
3331 case INIT_EXPR:
3332 case COMPOUND_EXPR:
3333 case PREDECREMENT_EXPR:
3334 case PREINCREMENT_EXPR:
3335 case POSTDECREMENT_EXPR:
3336 case POSTINCREMENT_EXPR:
3337 if (flags & OEP_LEXICOGRAPHIC)
3338 return OP_SAME (0) && OP_SAME (1);
3339 return 0;
3341 case CLEANUP_POINT_EXPR:
3342 case EXPR_STMT:
3343 if (flags & OEP_LEXICOGRAPHIC)
3344 return OP_SAME (0);
3345 return 0;
3347 default:
3348 return 0;
3351 case tcc_vl_exp:
3352 switch (TREE_CODE (arg0))
3354 case CALL_EXPR:
3355 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3356 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3357 /* If not both CALL_EXPRs are either internal or normal function
3358 functions, then they are not equal. */
3359 return 0;
3360 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3362 /* If the CALL_EXPRs call different internal functions, then they
3363 are not equal. */
3364 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3365 return 0;
3367 else
3369 /* If the CALL_EXPRs call different functions, then they are not
3370 equal. */
3371 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3372 flags))
3373 return 0;
3376 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3378 unsigned int cef = call_expr_flags (arg0);
3379 if (flags & OEP_PURE_SAME)
3380 cef &= ECF_CONST | ECF_PURE;
3381 else
3382 cef &= ECF_CONST;
3383 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3384 return 0;
3387 /* Now see if all the arguments are the same. */
3389 const_call_expr_arg_iterator iter0, iter1;
3390 const_tree a0, a1;
3391 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3392 a1 = first_const_call_expr_arg (arg1, &iter1);
3393 a0 && a1;
3394 a0 = next_const_call_expr_arg (&iter0),
3395 a1 = next_const_call_expr_arg (&iter1))
3396 if (! operand_equal_p (a0, a1, flags))
3397 return 0;
3399 /* If we get here and both argument lists are exhausted
3400 then the CALL_EXPRs are equal. */
3401 return ! (a0 || a1);
3403 default:
3404 return 0;
3407 case tcc_declaration:
3408 /* Consider __builtin_sqrt equal to sqrt. */
3409 return (TREE_CODE (arg0) == FUNCTION_DECL
3410 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3411 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3412 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3414 case tcc_exceptional:
3415 if (TREE_CODE (arg0) == CONSTRUCTOR)
3417 /* In GIMPLE constructors are used only to build vectors from
3418 elements. Individual elements in the constructor must be
3419 indexed in increasing order and form an initial sequence.
3421 We make no effort to compare constructors in generic.
3422 (see sem_variable::equals in ipa-icf which can do so for
3423 constants). */
3424 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3425 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3426 return 0;
3428 /* Be sure that vectors constructed have the same representation.
3429 We only tested element precision and modes to match.
3430 Vectors may be BLKmode and thus also check that the number of
3431 parts match. */
3432 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3433 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3434 return 0;
3436 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3437 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3438 unsigned int len = vec_safe_length (v0);
3440 if (len != vec_safe_length (v1))
3441 return 0;
3443 for (unsigned int i = 0; i < len; i++)
3445 constructor_elt *c0 = &(*v0)[i];
3446 constructor_elt *c1 = &(*v1)[i];
3448 if (!operand_equal_p (c0->value, c1->value, flags)
3449 /* In GIMPLE the indexes can be either NULL or matching i.
3450 Double check this so we won't get false
3451 positives for GENERIC. */
3452 || (c0->index
3453 && (TREE_CODE (c0->index) != INTEGER_CST
3454 || !compare_tree_int (c0->index, i)))
3455 || (c1->index
3456 && (TREE_CODE (c1->index) != INTEGER_CST
3457 || !compare_tree_int (c1->index, i))))
3458 return 0;
3460 return 1;
3462 else if (TREE_CODE (arg0) == STATEMENT_LIST
3463 && (flags & OEP_LEXICOGRAPHIC))
3465 /* Compare the STATEMENT_LISTs. */
3466 tree_stmt_iterator tsi1, tsi2;
3467 tree body1 = CONST_CAST_TREE (arg0);
3468 tree body2 = CONST_CAST_TREE (arg1);
3469 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3470 tsi_next (&tsi1), tsi_next (&tsi2))
3472 /* The lists don't have the same number of statements. */
3473 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3474 return 0;
3475 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3476 return 1;
3477 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3478 OEP_LEXICOGRAPHIC))
3479 return 0;
3482 return 0;
3484 case tcc_statement:
3485 switch (TREE_CODE (arg0))
3487 case RETURN_EXPR:
3488 if (flags & OEP_LEXICOGRAPHIC)
3489 return OP_SAME_WITH_NULL (0);
3490 return 0;
3491 default:
3492 return 0;
3495 default:
3496 return 0;
3499 #undef OP_SAME
3500 #undef OP_SAME_WITH_NULL
3503 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
3504 with a different signedness or a narrower precision. */
3506 static bool
3507 operand_equal_for_comparison_p (tree arg0, tree arg1)
3509 if (operand_equal_p (arg0, arg1, 0))
3510 return true;
3512 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3513 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3514 return false;
3516 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3517 and see if the inner values are the same. This removes any
3518 signedness comparison, which doesn't matter here. */
3519 tree op0 = arg0;
3520 tree op1 = arg1;
3521 STRIP_NOPS (op0);
3522 STRIP_NOPS (op1);
3523 if (operand_equal_p (op0, op1, 0))
3524 return true;
3526 /* Discard a single widening conversion from ARG1 and see if the inner
3527 value is the same as ARG0. */
3528 if (CONVERT_EXPR_P (arg1)
3529 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3530 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3531 < TYPE_PRECISION (TREE_TYPE (arg1))
3532 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
3533 return true;
3535 return false;
3538 /* See if ARG is an expression that is either a comparison or is performing
3539 arithmetic on comparisons. The comparisons must only be comparing
3540 two different values, which will be stored in *CVAL1 and *CVAL2; if
3541 they are nonzero it means that some operands have already been found.
3542 No variables may be used anywhere else in the expression except in the
3543 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3544 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3546 If this is true, return 1. Otherwise, return zero. */
3548 static int
3549 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3551 enum tree_code code = TREE_CODE (arg);
3552 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3554 /* We can handle some of the tcc_expression cases here. */
3555 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3556 tclass = tcc_unary;
3557 else if (tclass == tcc_expression
3558 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3559 || code == COMPOUND_EXPR))
3560 tclass = tcc_binary;
3562 else if (tclass == tcc_expression && code == SAVE_EXPR
3563 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3565 /* If we've already found a CVAL1 or CVAL2, this expression is
3566 two complex to handle. */
3567 if (*cval1 || *cval2)
3568 return 0;
3570 tclass = tcc_unary;
3571 *save_p = 1;
3574 switch (tclass)
3576 case tcc_unary:
3577 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3579 case tcc_binary:
3580 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3581 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3582 cval1, cval2, save_p));
3584 case tcc_constant:
3585 return 1;
3587 case tcc_expression:
3588 if (code == COND_EXPR)
3589 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3590 cval1, cval2, save_p)
3591 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3592 cval1, cval2, save_p)
3593 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3594 cval1, cval2, save_p));
3595 return 0;
3597 case tcc_comparison:
3598 /* First see if we can handle the first operand, then the second. For
3599 the second operand, we know *CVAL1 can't be zero. It must be that
3600 one side of the comparison is each of the values; test for the
3601 case where this isn't true by failing if the two operands
3602 are the same. */
3604 if (operand_equal_p (TREE_OPERAND (arg, 0),
3605 TREE_OPERAND (arg, 1), 0))
3606 return 0;
3608 if (*cval1 == 0)
3609 *cval1 = TREE_OPERAND (arg, 0);
3610 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3612 else if (*cval2 == 0)
3613 *cval2 = TREE_OPERAND (arg, 0);
3614 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3616 else
3617 return 0;
3619 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3621 else if (*cval2 == 0)
3622 *cval2 = TREE_OPERAND (arg, 1);
3623 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3625 else
3626 return 0;
3628 return 1;
3630 default:
3631 return 0;
3635 /* ARG is a tree that is known to contain just arithmetic operations and
3636 comparisons. Evaluate the operations in the tree substituting NEW0 for
3637 any occurrence of OLD0 as an operand of a comparison and likewise for
3638 NEW1 and OLD1. */
3640 static tree
3641 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3642 tree old1, tree new1)
3644 tree type = TREE_TYPE (arg);
3645 enum tree_code code = TREE_CODE (arg);
3646 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3648 /* We can handle some of the tcc_expression cases here. */
3649 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3650 tclass = tcc_unary;
3651 else if (tclass == tcc_expression
3652 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3653 tclass = tcc_binary;
3655 switch (tclass)
3657 case tcc_unary:
3658 return fold_build1_loc (loc, code, type,
3659 eval_subst (loc, TREE_OPERAND (arg, 0),
3660 old0, new0, old1, new1));
3662 case tcc_binary:
3663 return fold_build2_loc (loc, code, type,
3664 eval_subst (loc, TREE_OPERAND (arg, 0),
3665 old0, new0, old1, new1),
3666 eval_subst (loc, TREE_OPERAND (arg, 1),
3667 old0, new0, old1, new1));
3669 case tcc_expression:
3670 switch (code)
3672 case SAVE_EXPR:
3673 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3674 old1, new1);
3676 case COMPOUND_EXPR:
3677 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3678 old1, new1);
3680 case COND_EXPR:
3681 return fold_build3_loc (loc, code, type,
3682 eval_subst (loc, TREE_OPERAND (arg, 0),
3683 old0, new0, old1, new1),
3684 eval_subst (loc, TREE_OPERAND (arg, 1),
3685 old0, new0, old1, new1),
3686 eval_subst (loc, TREE_OPERAND (arg, 2),
3687 old0, new0, old1, new1));
3688 default:
3689 break;
3691 /* Fall through - ??? */
3693 case tcc_comparison:
3695 tree arg0 = TREE_OPERAND (arg, 0);
3696 tree arg1 = TREE_OPERAND (arg, 1);
3698 /* We need to check both for exact equality and tree equality. The
3699 former will be true if the operand has a side-effect. In that
3700 case, we know the operand occurred exactly once. */
3702 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3703 arg0 = new0;
3704 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3705 arg0 = new1;
3707 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3708 arg1 = new0;
3709 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3710 arg1 = new1;
3712 return fold_build2_loc (loc, code, type, arg0, arg1);
3715 default:
3716 return arg;
3720 /* Return a tree for the case when the result of an expression is RESULT
3721 converted to TYPE and OMITTED was previously an operand of the expression
3722 but is now not needed (e.g., we folded OMITTED * 0).
3724 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3725 the conversion of RESULT to TYPE. */
3727 tree
3728 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3730 tree t = fold_convert_loc (loc, type, result);
3732 /* If the resulting operand is an empty statement, just return the omitted
3733 statement casted to void. */
3734 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3735 return build1_loc (loc, NOP_EXPR, void_type_node,
3736 fold_ignored_result (omitted));
3738 if (TREE_SIDE_EFFECTS (omitted))
3739 return build2_loc (loc, COMPOUND_EXPR, type,
3740 fold_ignored_result (omitted), t);
3742 return non_lvalue_loc (loc, t);
3745 /* Return a tree for the case when the result of an expression is RESULT
3746 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3747 of the expression but are now not needed.
3749 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3750 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3751 evaluated before OMITTED2. Otherwise, if neither has side effects,
3752 just do the conversion of RESULT to TYPE. */
3754 tree
3755 omit_two_operands_loc (location_t loc, tree type, tree result,
3756 tree omitted1, tree omitted2)
3758 tree t = fold_convert_loc (loc, type, result);
3760 if (TREE_SIDE_EFFECTS (omitted2))
3761 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3762 if (TREE_SIDE_EFFECTS (omitted1))
3763 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3765 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3769 /* Return a simplified tree node for the truth-negation of ARG. This
3770 never alters ARG itself. We assume that ARG is an operation that
3771 returns a truth value (0 or 1).
3773 FIXME: one would think we would fold the result, but it causes
3774 problems with the dominator optimizer. */
3776 static tree
3777 fold_truth_not_expr (location_t loc, tree arg)
3779 tree type = TREE_TYPE (arg);
3780 enum tree_code code = TREE_CODE (arg);
3781 location_t loc1, loc2;
3783 /* If this is a comparison, we can simply invert it, except for
3784 floating-point non-equality comparisons, in which case we just
3785 enclose a TRUTH_NOT_EXPR around what we have. */
3787 if (TREE_CODE_CLASS (code) == tcc_comparison)
3789 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3790 if (FLOAT_TYPE_P (op_type)
3791 && flag_trapping_math
3792 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3793 && code != NE_EXPR && code != EQ_EXPR)
3794 return NULL_TREE;
3796 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3797 if (code == ERROR_MARK)
3798 return NULL_TREE;
3800 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3801 TREE_OPERAND (arg, 1));
3802 if (TREE_NO_WARNING (arg))
3803 TREE_NO_WARNING (ret) = 1;
3804 return ret;
3807 switch (code)
3809 case INTEGER_CST:
3810 return constant_boolean_node (integer_zerop (arg), type);
3812 case TRUTH_AND_EXPR:
3813 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3814 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3815 return build2_loc (loc, TRUTH_OR_EXPR, type,
3816 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3817 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3819 case TRUTH_OR_EXPR:
3820 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3821 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3822 return build2_loc (loc, TRUTH_AND_EXPR, type,
3823 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3824 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3826 case TRUTH_XOR_EXPR:
3827 /* Here we can invert either operand. We invert the first operand
3828 unless the second operand is a TRUTH_NOT_EXPR in which case our
3829 result is the XOR of the first operand with the inside of the
3830 negation of the second operand. */
3832 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3833 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3834 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3835 else
3836 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3837 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3838 TREE_OPERAND (arg, 1));
3840 case TRUTH_ANDIF_EXPR:
3841 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3842 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3843 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3844 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3845 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3847 case TRUTH_ORIF_EXPR:
3848 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3849 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3850 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3851 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3852 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3854 case TRUTH_NOT_EXPR:
3855 return TREE_OPERAND (arg, 0);
3857 case COND_EXPR:
3859 tree arg1 = TREE_OPERAND (arg, 1);
3860 tree arg2 = TREE_OPERAND (arg, 2);
3862 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3863 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3865 /* A COND_EXPR may have a throw as one operand, which
3866 then has void type. Just leave void operands
3867 as they are. */
3868 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3869 VOID_TYPE_P (TREE_TYPE (arg1))
3870 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3871 VOID_TYPE_P (TREE_TYPE (arg2))
3872 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3875 case COMPOUND_EXPR:
3876 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3877 return build2_loc (loc, COMPOUND_EXPR, type,
3878 TREE_OPERAND (arg, 0),
3879 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3881 case NON_LVALUE_EXPR:
3882 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3883 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3885 CASE_CONVERT:
3886 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3887 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3889 /* fall through */
3891 case FLOAT_EXPR:
3892 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3893 return build1_loc (loc, TREE_CODE (arg), type,
3894 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3896 case BIT_AND_EXPR:
3897 if (!integer_onep (TREE_OPERAND (arg, 1)))
3898 return NULL_TREE;
3899 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3901 case SAVE_EXPR:
3902 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3904 case CLEANUP_POINT_EXPR:
3905 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3906 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3907 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3909 default:
3910 return NULL_TREE;
3914 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3915 assume that ARG is an operation that returns a truth value (0 or 1
3916 for scalars, 0 or -1 for vectors). Return the folded expression if
3917 folding is successful. Otherwise, return NULL_TREE. */
3919 static tree
3920 fold_invert_truthvalue (location_t loc, tree arg)
3922 tree type = TREE_TYPE (arg);
3923 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3924 ? BIT_NOT_EXPR
3925 : TRUTH_NOT_EXPR,
3926 type, arg);
3929 /* Return a simplified tree node for the truth-negation of ARG. This
3930 never alters ARG itself. We assume that ARG is an operation that
3931 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3933 tree
3934 invert_truthvalue_loc (location_t loc, tree arg)
3936 if (TREE_CODE (arg) == ERROR_MARK)
3937 return arg;
3939 tree type = TREE_TYPE (arg);
3940 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3941 ? BIT_NOT_EXPR
3942 : TRUTH_NOT_EXPR,
3943 type, arg);
3946 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3947 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3948 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3949 is the original memory reference used to preserve the alias set of
3950 the access. */
3952 static tree
3953 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3954 HOST_WIDE_INT bitsize, poly_int64 bitpos,
3955 int unsignedp, int reversep)
3957 tree result, bftype;
3959 /* Attempt not to lose the access path if possible. */
3960 if (TREE_CODE (orig_inner) == COMPONENT_REF)
3962 tree ninner = TREE_OPERAND (orig_inner, 0);
3963 machine_mode nmode;
3964 poly_int64 nbitsize, nbitpos;
3965 tree noffset;
3966 int nunsignedp, nreversep, nvolatilep = 0;
3967 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
3968 &noffset, &nmode, &nunsignedp,
3969 &nreversep, &nvolatilep);
3970 if (base == inner
3971 && noffset == NULL_TREE
3972 && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
3973 && !reversep
3974 && !nreversep
3975 && !nvolatilep)
3977 inner = ninner;
3978 bitpos -= nbitpos;
3982 alias_set_type iset = get_alias_set (orig_inner);
3983 if (iset == 0 && get_alias_set (inner) != iset)
3984 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3985 build_fold_addr_expr (inner),
3986 build_int_cst (ptr_type_node, 0));
3988 if (known_eq (bitpos, 0) && !reversep)
3990 tree size = TYPE_SIZE (TREE_TYPE (inner));
3991 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3992 || POINTER_TYPE_P (TREE_TYPE (inner)))
3993 && tree_fits_shwi_p (size)
3994 && tree_to_shwi (size) == bitsize)
3995 return fold_convert_loc (loc, type, inner);
3998 bftype = type;
3999 if (TYPE_PRECISION (bftype) != bitsize
4000 || TYPE_UNSIGNED (bftype) == !unsignedp)
4001 bftype = build_nonstandard_integer_type (bitsize, 0);
4003 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4004 bitsize_int (bitsize), bitsize_int (bitpos));
4005 REF_REVERSE_STORAGE_ORDER (result) = reversep;
4007 if (bftype != type)
4008 result = fold_convert_loc (loc, type, result);
4010 return result;
4013 /* Optimize a bit-field compare.
4015 There are two cases: First is a compare against a constant and the
4016 second is a comparison of two items where the fields are at the same
4017 bit position relative to the start of a chunk (byte, halfword, word)
4018 large enough to contain it. In these cases we can avoid the shift
4019 implicit in bitfield extractions.
4021 For constants, we emit a compare of the shifted constant with the
4022 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4023 compared. For two fields at the same position, we do the ANDs with the
4024 similar mask and compare the result of the ANDs.
4026 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4027 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4028 are the left and right operands of the comparison, respectively.
4030 If the optimization described above can be done, we return the resulting
4031 tree. Otherwise we return zero. */
4033 static tree
4034 optimize_bit_field_compare (location_t loc, enum tree_code code,
4035 tree compare_type, tree lhs, tree rhs)
4037 poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4038 HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4039 tree type = TREE_TYPE (lhs);
4040 tree unsigned_type;
4041 int const_p = TREE_CODE (rhs) == INTEGER_CST;
4042 machine_mode lmode, rmode;
4043 scalar_int_mode nmode;
4044 int lunsignedp, runsignedp;
4045 int lreversep, rreversep;
4046 int lvolatilep = 0, rvolatilep = 0;
4047 tree linner, rinner = NULL_TREE;
4048 tree mask;
4049 tree offset;
4051 /* Get all the information about the extractions being done. If the bit size
4052 is the same as the size of the underlying object, we aren't doing an
4053 extraction at all and so can do nothing. We also don't want to
4054 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4055 then will no longer be able to replace it. */
4056 linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4057 &lunsignedp, &lreversep, &lvolatilep);
4058 if (linner == lhs
4059 || !known_size_p (plbitsize)
4060 || !plbitsize.is_constant (&lbitsize)
4061 || !plbitpos.is_constant (&lbitpos)
4062 || lbitsize == GET_MODE_BITSIZE (lmode)
4063 || offset != 0
4064 || TREE_CODE (linner) == PLACEHOLDER_EXPR
4065 || lvolatilep)
4066 return 0;
4068 if (const_p)
4069 rreversep = lreversep;
4070 else
4072 /* If this is not a constant, we can only do something if bit positions,
4073 sizes, signedness and storage order are the same. */
4074 rinner
4075 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4076 &runsignedp, &rreversep, &rvolatilep);
4078 if (rinner == rhs
4079 || maybe_ne (lbitpos, rbitpos)
4080 || maybe_ne (lbitsize, rbitsize)
4081 || lunsignedp != runsignedp
4082 || lreversep != rreversep
4083 || offset != 0
4084 || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4085 || rvolatilep)
4086 return 0;
4089 /* Honor the C++ memory model and mimic what RTL expansion does. */
4090 poly_uint64 bitstart = 0;
4091 poly_uint64 bitend = 0;
4092 if (TREE_CODE (lhs) == COMPONENT_REF)
4094 get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4095 if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4096 return 0;
4099 /* See if we can find a mode to refer to this field. We should be able to,
4100 but fail if we can't. */
4101 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4102 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4103 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4104 TYPE_ALIGN (TREE_TYPE (rinner))),
4105 BITS_PER_WORD, false, &nmode))
4106 return 0;
4108 /* Set signed and unsigned types of the precision of this mode for the
4109 shifts below. */
4110 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4112 /* Compute the bit position and size for the new reference and our offset
4113 within it. If the new reference is the same size as the original, we
4114 won't optimize anything, so return zero. */
4115 nbitsize = GET_MODE_BITSIZE (nmode);
4116 nbitpos = lbitpos & ~ (nbitsize - 1);
4117 lbitpos -= nbitpos;
4118 if (nbitsize == lbitsize)
4119 return 0;
4121 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4122 lbitpos = nbitsize - lbitsize - lbitpos;
4124 /* Make the mask to be used against the extracted field. */
4125 mask = build_int_cst_type (unsigned_type, -1);
4126 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4127 mask = const_binop (RSHIFT_EXPR, mask,
4128 size_int (nbitsize - lbitsize - lbitpos));
4130 if (! const_p)
4132 if (nbitpos < 0)
4133 return 0;
4135 /* If not comparing with constant, just rework the comparison
4136 and return. */
4137 tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4138 nbitsize, nbitpos, 1, lreversep);
4139 t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4140 tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4141 nbitsize, nbitpos, 1, rreversep);
4142 t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4143 return fold_build2_loc (loc, code, compare_type, t1, t2);
4146 /* Otherwise, we are handling the constant case. See if the constant is too
4147 big for the field. Warn and return a tree for 0 (false) if so. We do
4148 this not only for its own sake, but to avoid having to test for this
4149 error case below. If we didn't, we might generate wrong code.
4151 For unsigned fields, the constant shifted right by the field length should
4152 be all zero. For signed fields, the high-order bits should agree with
4153 the sign bit. */
4155 if (lunsignedp)
4157 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4159 warning (0, "comparison is always %d due to width of bit-field",
4160 code == NE_EXPR);
4161 return constant_boolean_node (code == NE_EXPR, compare_type);
4164 else
4166 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4167 if (tem != 0 && tem != -1)
4169 warning (0, "comparison is always %d due to width of bit-field",
4170 code == NE_EXPR);
4171 return constant_boolean_node (code == NE_EXPR, compare_type);
4175 if (nbitpos < 0)
4176 return 0;
4178 /* Single-bit compares should always be against zero. */
4179 if (lbitsize == 1 && ! integer_zerop (rhs))
4181 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4182 rhs = build_int_cst (type, 0);
4185 /* Make a new bitfield reference, shift the constant over the
4186 appropriate number of bits and mask it with the computed mask
4187 (in case this was a signed field). If we changed it, make a new one. */
4188 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4189 nbitsize, nbitpos, 1, lreversep);
4191 rhs = const_binop (BIT_AND_EXPR,
4192 const_binop (LSHIFT_EXPR,
4193 fold_convert_loc (loc, unsigned_type, rhs),
4194 size_int (lbitpos)),
4195 mask);
4197 lhs = build2_loc (loc, code, compare_type,
4198 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4199 return lhs;
4202 /* Subroutine for fold_truth_andor_1: decode a field reference.
4204 If EXP is a comparison reference, we return the innermost reference.
4206 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4207 set to the starting bit number.
4209 If the innermost field can be completely contained in a mode-sized
4210 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4212 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4213 otherwise it is not changed.
4215 *PUNSIGNEDP is set to the signedness of the field.
4217 *PREVERSEP is set to the storage order of the field.
4219 *PMASK is set to the mask used. This is either contained in a
4220 BIT_AND_EXPR or derived from the width of the field.
4222 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4224 Return 0 if this is not a component reference or is one that we can't
4225 do anything with. */
4227 static tree
4228 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4229 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4230 int *punsignedp, int *preversep, int *pvolatilep,
4231 tree *pmask, tree *pand_mask)
4233 tree exp = *exp_;
4234 tree outer_type = 0;
4235 tree and_mask = 0;
4236 tree mask, inner, offset;
4237 tree unsigned_type;
4238 unsigned int precision;
4240 /* All the optimizations using this function assume integer fields.
4241 There are problems with FP fields since the type_for_size call
4242 below can fail for, e.g., XFmode. */
4243 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4244 return 0;
4246 /* We are interested in the bare arrangement of bits, so strip everything
4247 that doesn't affect the machine mode. However, record the type of the
4248 outermost expression if it may matter below. */
4249 if (CONVERT_EXPR_P (exp)
4250 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4251 outer_type = TREE_TYPE (exp);
4252 STRIP_NOPS (exp);
4254 if (TREE_CODE (exp) == BIT_AND_EXPR)
4256 and_mask = TREE_OPERAND (exp, 1);
4257 exp = TREE_OPERAND (exp, 0);
4258 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4259 if (TREE_CODE (and_mask) != INTEGER_CST)
4260 return 0;
4263 poly_int64 poly_bitsize, poly_bitpos;
4264 inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
4265 pmode, punsignedp, preversep, pvolatilep);
4266 if ((inner == exp && and_mask == 0)
4267 || !poly_bitsize.is_constant (pbitsize)
4268 || !poly_bitpos.is_constant (pbitpos)
4269 || *pbitsize < 0
4270 || offset != 0
4271 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4272 /* Reject out-of-bound accesses (PR79731). */
4273 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4274 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4275 *pbitpos + *pbitsize) < 0))
4276 return 0;
4278 *exp_ = exp;
4280 /* If the number of bits in the reference is the same as the bitsize of
4281 the outer type, then the outer type gives the signedness. Otherwise
4282 (in case of a small bitfield) the signedness is unchanged. */
4283 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4284 *punsignedp = TYPE_UNSIGNED (outer_type);
4286 /* Compute the mask to access the bitfield. */
4287 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4288 precision = TYPE_PRECISION (unsigned_type);
4290 mask = build_int_cst_type (unsigned_type, -1);
4292 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4293 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4295 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4296 if (and_mask != 0)
4297 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4298 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4300 *pmask = mask;
4301 *pand_mask = and_mask;
4302 return inner;
4305 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4306 bit positions and MASK is SIGNED. */
4308 static int
4309 all_ones_mask_p (const_tree mask, unsigned int size)
4311 tree type = TREE_TYPE (mask);
4312 unsigned int precision = TYPE_PRECISION (type);
4314 /* If this function returns true when the type of the mask is
4315 UNSIGNED, then there will be errors. In particular see
4316 gcc.c-torture/execute/990326-1.c. There does not appear to be
4317 any documentation paper trail as to why this is so. But the pre
4318 wide-int worked with that restriction and it has been preserved
4319 here. */
4320 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4321 return false;
4323 return wi::mask (size, false, precision) == wi::to_wide (mask);
4326 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4327 represents the sign bit of EXP's type. If EXP represents a sign
4328 or zero extension, also test VAL against the unextended type.
4329 The return value is the (sub)expression whose sign bit is VAL,
4330 or NULL_TREE otherwise. */
4332 tree
4333 sign_bit_p (tree exp, const_tree val)
4335 int width;
4336 tree t;
4338 /* Tree EXP must have an integral type. */
4339 t = TREE_TYPE (exp);
4340 if (! INTEGRAL_TYPE_P (t))
4341 return NULL_TREE;
4343 /* Tree VAL must be an integer constant. */
4344 if (TREE_CODE (val) != INTEGER_CST
4345 || TREE_OVERFLOW (val))
4346 return NULL_TREE;
4348 width = TYPE_PRECISION (t);
4349 if (wi::only_sign_bit_p (wi::to_wide (val), width))
4350 return exp;
4352 /* Handle extension from a narrower type. */
4353 if (TREE_CODE (exp) == NOP_EXPR
4354 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4355 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4357 return NULL_TREE;
4360 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4361 to be evaluated unconditionally. */
4363 static int
4364 simple_operand_p (const_tree exp)
4366 /* Strip any conversions that don't change the machine mode. */
4367 STRIP_NOPS (exp);
4369 return (CONSTANT_CLASS_P (exp)
4370 || TREE_CODE (exp) == SSA_NAME
4371 || (DECL_P (exp)
4372 && ! TREE_ADDRESSABLE (exp)
4373 && ! TREE_THIS_VOLATILE (exp)
4374 && ! DECL_NONLOCAL (exp)
4375 /* Don't regard global variables as simple. They may be
4376 allocated in ways unknown to the compiler (shared memory,
4377 #pragma weak, etc). */
4378 && ! TREE_PUBLIC (exp)
4379 && ! DECL_EXTERNAL (exp)
4380 /* Weakrefs are not safe to be read, since they can be NULL.
4381 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4382 have DECL_WEAK flag set. */
4383 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4384 /* Loading a static variable is unduly expensive, but global
4385 registers aren't expensive. */
4386 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4389 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4390 to be evaluated unconditionally.
4391 I addition to simple_operand_p, we assume that comparisons, conversions,
4392 and logic-not operations are simple, if their operands are simple, too. */
4394 static bool
4395 simple_operand_p_2 (tree exp)
4397 enum tree_code code;
4399 if (TREE_SIDE_EFFECTS (exp)
4400 || tree_could_trap_p (exp))
4401 return false;
4403 while (CONVERT_EXPR_P (exp))
4404 exp = TREE_OPERAND (exp, 0);
4406 code = TREE_CODE (exp);
4408 if (TREE_CODE_CLASS (code) == tcc_comparison)
4409 return (simple_operand_p (TREE_OPERAND (exp, 0))
4410 && simple_operand_p (TREE_OPERAND (exp, 1)));
4412 if (code == TRUTH_NOT_EXPR)
4413 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4415 return simple_operand_p (exp);
4419 /* The following functions are subroutines to fold_range_test and allow it to
4420 try to change a logical combination of comparisons into a range test.
4422 For example, both
4423 X == 2 || X == 3 || X == 4 || X == 5
4425 X >= 2 && X <= 5
4426 are converted to
4427 (unsigned) (X - 2) <= 3
4429 We describe each set of comparisons as being either inside or outside
4430 a range, using a variable named like IN_P, and then describe the
4431 range with a lower and upper bound. If one of the bounds is omitted,
4432 it represents either the highest or lowest value of the type.
4434 In the comments below, we represent a range by two numbers in brackets
4435 preceded by a "+" to designate being inside that range, or a "-" to
4436 designate being outside that range, so the condition can be inverted by
4437 flipping the prefix. An omitted bound is represented by a "-". For
4438 example, "- [-, 10]" means being outside the range starting at the lowest
4439 possible value and ending at 10, in other words, being greater than 10.
4440 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4441 always false.
4443 We set up things so that the missing bounds are handled in a consistent
4444 manner so neither a missing bound nor "true" and "false" need to be
4445 handled using a special case. */
4447 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4448 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4449 and UPPER1_P are nonzero if the respective argument is an upper bound
4450 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4451 must be specified for a comparison. ARG1 will be converted to ARG0's
4452 type if both are specified. */
4454 static tree
4455 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4456 tree arg1, int upper1_p)
4458 tree tem;
4459 int result;
4460 int sgn0, sgn1;
4462 /* If neither arg represents infinity, do the normal operation.
4463 Else, if not a comparison, return infinity. Else handle the special
4464 comparison rules. Note that most of the cases below won't occur, but
4465 are handled for consistency. */
4467 if (arg0 != 0 && arg1 != 0)
4469 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4470 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4471 STRIP_NOPS (tem);
4472 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4475 if (TREE_CODE_CLASS (code) != tcc_comparison)
4476 return 0;
4478 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4479 for neither. In real maths, we cannot assume open ended ranges are
4480 the same. But, this is computer arithmetic, where numbers are finite.
4481 We can therefore make the transformation of any unbounded range with
4482 the value Z, Z being greater than any representable number. This permits
4483 us to treat unbounded ranges as equal. */
4484 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4485 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4486 switch (code)
4488 case EQ_EXPR:
4489 result = sgn0 == sgn1;
4490 break;
4491 case NE_EXPR:
4492 result = sgn0 != sgn1;
4493 break;
4494 case LT_EXPR:
4495 result = sgn0 < sgn1;
4496 break;
4497 case LE_EXPR:
4498 result = sgn0 <= sgn1;
4499 break;
4500 case GT_EXPR:
4501 result = sgn0 > sgn1;
4502 break;
4503 case GE_EXPR:
4504 result = sgn0 >= sgn1;
4505 break;
4506 default:
4507 gcc_unreachable ();
4510 return constant_boolean_node (result, type);
4513 /* Helper routine for make_range. Perform one step for it, return
4514 new expression if the loop should continue or NULL_TREE if it should
4515 stop. */
4517 tree
4518 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4519 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4520 bool *strict_overflow_p)
4522 tree arg0_type = TREE_TYPE (arg0);
4523 tree n_low, n_high, low = *p_low, high = *p_high;
4524 int in_p = *p_in_p, n_in_p;
4526 switch (code)
4528 case TRUTH_NOT_EXPR:
4529 /* We can only do something if the range is testing for zero. */
4530 if (low == NULL_TREE || high == NULL_TREE
4531 || ! integer_zerop (low) || ! integer_zerop (high))
4532 return NULL_TREE;
4533 *p_in_p = ! in_p;
4534 return arg0;
4536 case EQ_EXPR: case NE_EXPR:
4537 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4538 /* We can only do something if the range is testing for zero
4539 and if the second operand is an integer constant. Note that
4540 saying something is "in" the range we make is done by
4541 complementing IN_P since it will set in the initial case of
4542 being not equal to zero; "out" is leaving it alone. */
4543 if (low == NULL_TREE || high == NULL_TREE
4544 || ! integer_zerop (low) || ! integer_zerop (high)
4545 || TREE_CODE (arg1) != INTEGER_CST)
4546 return NULL_TREE;
4548 switch (code)
4550 case NE_EXPR: /* - [c, c] */
4551 low = high = arg1;
4552 break;
4553 case EQ_EXPR: /* + [c, c] */
4554 in_p = ! in_p, low = high = arg1;
4555 break;
4556 case GT_EXPR: /* - [-, c] */
4557 low = 0, high = arg1;
4558 break;
4559 case GE_EXPR: /* + [c, -] */
4560 in_p = ! in_p, low = arg1, high = 0;
4561 break;
4562 case LT_EXPR: /* - [c, -] */
4563 low = arg1, high = 0;
4564 break;
4565 case LE_EXPR: /* + [-, c] */
4566 in_p = ! in_p, low = 0, high = arg1;
4567 break;
4568 default:
4569 gcc_unreachable ();
4572 /* If this is an unsigned comparison, we also know that EXP is
4573 greater than or equal to zero. We base the range tests we make
4574 on that fact, so we record it here so we can parse existing
4575 range tests. We test arg0_type since often the return type
4576 of, e.g. EQ_EXPR, is boolean. */
4577 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4579 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4580 in_p, low, high, 1,
4581 build_int_cst (arg0_type, 0),
4582 NULL_TREE))
4583 return NULL_TREE;
4585 in_p = n_in_p, low = n_low, high = n_high;
4587 /* If the high bound is missing, but we have a nonzero low
4588 bound, reverse the range so it goes from zero to the low bound
4589 minus 1. */
4590 if (high == 0 && low && ! integer_zerop (low))
4592 in_p = ! in_p;
4593 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4594 build_int_cst (TREE_TYPE (low), 1), 0);
4595 low = build_int_cst (arg0_type, 0);
4599 *p_low = low;
4600 *p_high = high;
4601 *p_in_p = in_p;
4602 return arg0;
4604 case NEGATE_EXPR:
4605 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4606 low and high are non-NULL, then normalize will DTRT. */
4607 if (!TYPE_UNSIGNED (arg0_type)
4608 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4610 if (low == NULL_TREE)
4611 low = TYPE_MIN_VALUE (arg0_type);
4612 if (high == NULL_TREE)
4613 high = TYPE_MAX_VALUE (arg0_type);
4616 /* (-x) IN [a,b] -> x in [-b, -a] */
4617 n_low = range_binop (MINUS_EXPR, exp_type,
4618 build_int_cst (exp_type, 0),
4619 0, high, 1);
4620 n_high = range_binop (MINUS_EXPR, exp_type,
4621 build_int_cst (exp_type, 0),
4622 0, low, 0);
4623 if (n_high != 0 && TREE_OVERFLOW (n_high))
4624 return NULL_TREE;
4625 goto normalize;
4627 case BIT_NOT_EXPR:
4628 /* ~ X -> -X - 1 */
4629 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4630 build_int_cst (exp_type, 1));
4632 case PLUS_EXPR:
4633 case MINUS_EXPR:
4634 if (TREE_CODE (arg1) != INTEGER_CST)
4635 return NULL_TREE;
4637 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4638 move a constant to the other side. */
4639 if (!TYPE_UNSIGNED (arg0_type)
4640 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4641 return NULL_TREE;
4643 /* If EXP is signed, any overflow in the computation is undefined,
4644 so we don't worry about it so long as our computations on
4645 the bounds don't overflow. For unsigned, overflow is defined
4646 and this is exactly the right thing. */
4647 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4648 arg0_type, low, 0, arg1, 0);
4649 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4650 arg0_type, high, 1, arg1, 0);
4651 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4652 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4653 return NULL_TREE;
4655 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4656 *strict_overflow_p = true;
4658 normalize:
4659 /* Check for an unsigned range which has wrapped around the maximum
4660 value thus making n_high < n_low, and normalize it. */
4661 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4663 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4664 build_int_cst (TREE_TYPE (n_high), 1), 0);
4665 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4666 build_int_cst (TREE_TYPE (n_low), 1), 0);
4668 /* If the range is of the form +/- [ x+1, x ], we won't
4669 be able to normalize it. But then, it represents the
4670 whole range or the empty set, so make it
4671 +/- [ -, - ]. */
4672 if (tree_int_cst_equal (n_low, low)
4673 && tree_int_cst_equal (n_high, high))
4674 low = high = 0;
4675 else
4676 in_p = ! in_p;
4678 else
4679 low = n_low, high = n_high;
4681 *p_low = low;
4682 *p_high = high;
4683 *p_in_p = in_p;
4684 return arg0;
4686 CASE_CONVERT:
4687 case NON_LVALUE_EXPR:
4688 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4689 return NULL_TREE;
4691 if (! INTEGRAL_TYPE_P (arg0_type)
4692 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4693 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4694 return NULL_TREE;
4696 n_low = low, n_high = high;
4698 if (n_low != 0)
4699 n_low = fold_convert_loc (loc, arg0_type, n_low);
4701 if (n_high != 0)
4702 n_high = fold_convert_loc (loc, arg0_type, n_high);
4704 /* If we're converting arg0 from an unsigned type, to exp,
4705 a signed type, we will be doing the comparison as unsigned.
4706 The tests above have already verified that LOW and HIGH
4707 are both positive.
4709 So we have to ensure that we will handle large unsigned
4710 values the same way that the current signed bounds treat
4711 negative values. */
4713 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4715 tree high_positive;
4716 tree equiv_type;
4717 /* For fixed-point modes, we need to pass the saturating flag
4718 as the 2nd parameter. */
4719 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4720 equiv_type
4721 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4722 TYPE_SATURATING (arg0_type));
4723 else
4724 equiv_type
4725 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4727 /* A range without an upper bound is, naturally, unbounded.
4728 Since convert would have cropped a very large value, use
4729 the max value for the destination type. */
4730 high_positive
4731 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4732 : TYPE_MAX_VALUE (arg0_type);
4734 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4735 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4736 fold_convert_loc (loc, arg0_type,
4737 high_positive),
4738 build_int_cst (arg0_type, 1));
4740 /* If the low bound is specified, "and" the range with the
4741 range for which the original unsigned value will be
4742 positive. */
4743 if (low != 0)
4745 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4746 1, fold_convert_loc (loc, arg0_type,
4747 integer_zero_node),
4748 high_positive))
4749 return NULL_TREE;
4751 in_p = (n_in_p == in_p);
4753 else
4755 /* Otherwise, "or" the range with the range of the input
4756 that will be interpreted as negative. */
4757 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4758 1, fold_convert_loc (loc, arg0_type,
4759 integer_zero_node),
4760 high_positive))
4761 return NULL_TREE;
4763 in_p = (in_p != n_in_p);
4767 *p_low = n_low;
4768 *p_high = n_high;
4769 *p_in_p = in_p;
4770 return arg0;
4772 default:
4773 return NULL_TREE;
4777 /* Given EXP, a logical expression, set the range it is testing into
4778 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4779 actually being tested. *PLOW and *PHIGH will be made of the same
4780 type as the returned expression. If EXP is not a comparison, we
4781 will most likely not be returning a useful value and range. Set
4782 *STRICT_OVERFLOW_P to true if the return value is only valid
4783 because signed overflow is undefined; otherwise, do not change
4784 *STRICT_OVERFLOW_P. */
4786 tree
4787 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4788 bool *strict_overflow_p)
4790 enum tree_code code;
4791 tree arg0, arg1 = NULL_TREE;
4792 tree exp_type, nexp;
4793 int in_p;
4794 tree low, high;
4795 location_t loc = EXPR_LOCATION (exp);
4797 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4798 and see if we can refine the range. Some of the cases below may not
4799 happen, but it doesn't seem worth worrying about this. We "continue"
4800 the outer loop when we've changed something; otherwise we "break"
4801 the switch, which will "break" the while. */
4803 in_p = 0;
4804 low = high = build_int_cst (TREE_TYPE (exp), 0);
4806 while (1)
4808 code = TREE_CODE (exp);
4809 exp_type = TREE_TYPE (exp);
4810 arg0 = NULL_TREE;
4812 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4814 if (TREE_OPERAND_LENGTH (exp) > 0)
4815 arg0 = TREE_OPERAND (exp, 0);
4816 if (TREE_CODE_CLASS (code) == tcc_binary
4817 || TREE_CODE_CLASS (code) == tcc_comparison
4818 || (TREE_CODE_CLASS (code) == tcc_expression
4819 && TREE_OPERAND_LENGTH (exp) > 1))
4820 arg1 = TREE_OPERAND (exp, 1);
4822 if (arg0 == NULL_TREE)
4823 break;
4825 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4826 &high, &in_p, strict_overflow_p);
4827 if (nexp == NULL_TREE)
4828 break;
4829 exp = nexp;
4832 /* If EXP is a constant, we can evaluate whether this is true or false. */
4833 if (TREE_CODE (exp) == INTEGER_CST)
4835 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4836 exp, 0, low, 0))
4837 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4838 exp, 1, high, 1)));
4839 low = high = 0;
4840 exp = 0;
4843 *pin_p = in_p, *plow = low, *phigh = high;
4844 return exp;
4847 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4848 a bitwise check i.e. when
4849 LOW == 0xXX...X00...0
4850 HIGH == 0xXX...X11...1
4851 Return corresponding mask in MASK and stem in VALUE. */
4853 static bool
4854 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4855 tree *value)
4857 if (TREE_CODE (low) != INTEGER_CST
4858 || TREE_CODE (high) != INTEGER_CST)
4859 return false;
4861 unsigned prec = TYPE_PRECISION (type);
4862 wide_int lo = wi::to_wide (low, prec);
4863 wide_int hi = wi::to_wide (high, prec);
4865 wide_int end_mask = lo ^ hi;
4866 if ((end_mask & (end_mask + 1)) != 0
4867 || (lo & end_mask) != 0)
4868 return false;
4870 wide_int stem_mask = ~end_mask;
4871 wide_int stem = lo & stem_mask;
4872 if (stem != (hi & stem_mask))
4873 return false;
4875 *mask = wide_int_to_tree (type, stem_mask);
4876 *value = wide_int_to_tree (type, stem);
4878 return true;
4881 /* Helper routine for build_range_check and match.pd. Return the type to
4882 perform the check or NULL if it shouldn't be optimized. */
4884 tree
4885 range_check_type (tree etype)
4887 /* First make sure that arithmetics in this type is valid, then make sure
4888 that it wraps around. */
4889 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4890 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4891 TYPE_UNSIGNED (etype));
4893 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4895 tree utype, minv, maxv;
4897 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4898 for the type in question, as we rely on this here. */
4899 utype = unsigned_type_for (etype);
4900 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4901 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4902 build_int_cst (TREE_TYPE (maxv), 1), 1);
4903 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4905 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4906 minv, 1, maxv, 1)))
4907 etype = utype;
4908 else
4909 return NULL_TREE;
4911 return etype;
4914 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4915 type, TYPE, return an expression to test if EXP is in (or out of, depending
4916 on IN_P) the range. Return 0 if the test couldn't be created. */
4918 tree
4919 build_range_check (location_t loc, tree type, tree exp, int in_p,
4920 tree low, tree high)
4922 tree etype = TREE_TYPE (exp), mask, value;
4924 /* Disable this optimization for function pointer expressions
4925 on targets that require function pointer canonicalization. */
4926 if (targetm.have_canonicalize_funcptr_for_compare ()
4927 && TREE_CODE (etype) == POINTER_TYPE
4928 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4929 return NULL_TREE;
4931 if (! in_p)
4933 value = build_range_check (loc, type, exp, 1, low, high);
4934 if (value != 0)
4935 return invert_truthvalue_loc (loc, value);
4937 return 0;
4940 if (low == 0 && high == 0)
4941 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4943 if (low == 0)
4944 return fold_build2_loc (loc, LE_EXPR, type, exp,
4945 fold_convert_loc (loc, etype, high));
4947 if (high == 0)
4948 return fold_build2_loc (loc, GE_EXPR, type, exp,
4949 fold_convert_loc (loc, etype, low));
4951 if (operand_equal_p (low, high, 0))
4952 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4953 fold_convert_loc (loc, etype, low));
4955 if (TREE_CODE (exp) == BIT_AND_EXPR
4956 && maskable_range_p (low, high, etype, &mask, &value))
4957 return fold_build2_loc (loc, EQ_EXPR, type,
4958 fold_build2_loc (loc, BIT_AND_EXPR, etype,
4959 exp, mask),
4960 value);
4962 if (integer_zerop (low))
4964 if (! TYPE_UNSIGNED (etype))
4966 etype = unsigned_type_for (etype);
4967 high = fold_convert_loc (loc, etype, high);
4968 exp = fold_convert_loc (loc, etype, exp);
4970 return build_range_check (loc, type, exp, 1, 0, high);
4973 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4974 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4976 int prec = TYPE_PRECISION (etype);
4978 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
4980 if (TYPE_UNSIGNED (etype))
4982 tree signed_etype = signed_type_for (etype);
4983 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4984 etype
4985 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4986 else
4987 etype = signed_etype;
4988 exp = fold_convert_loc (loc, etype, exp);
4990 return fold_build2_loc (loc, GT_EXPR, type, exp,
4991 build_int_cst (etype, 0));
4995 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4996 This requires wrap-around arithmetics for the type of the expression. */
4997 etype = range_check_type (etype);
4998 if (etype == NULL_TREE)
4999 return NULL_TREE;
5001 if (POINTER_TYPE_P (etype))
5002 etype = unsigned_type_for (etype);
5004 high = fold_convert_loc (loc, etype, high);
5005 low = fold_convert_loc (loc, etype, low);
5006 exp = fold_convert_loc (loc, etype, exp);
5008 value = const_binop (MINUS_EXPR, high, low);
5010 if (value != 0 && !TREE_OVERFLOW (value))
5011 return build_range_check (loc, type,
5012 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5013 1, build_int_cst (etype, 0), value);
5015 return 0;
5018 /* Return the predecessor of VAL in its type, handling the infinite case. */
5020 static tree
5021 range_predecessor (tree val)
5023 tree type = TREE_TYPE (val);
5025 if (INTEGRAL_TYPE_P (type)
5026 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5027 return 0;
5028 else
5029 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5030 build_int_cst (TREE_TYPE (val), 1), 0);
5033 /* Return the successor of VAL in its type, handling the infinite case. */
5035 static tree
5036 range_successor (tree val)
5038 tree type = TREE_TYPE (val);
5040 if (INTEGRAL_TYPE_P (type)
5041 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5042 return 0;
5043 else
5044 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5045 build_int_cst (TREE_TYPE (val), 1), 0);
5048 /* Given two ranges, see if we can merge them into one. Return 1 if we
5049 can, 0 if we can't. Set the output range into the specified parameters. */
5051 bool
5052 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5053 tree high0, int in1_p, tree low1, tree high1)
5055 int no_overlap;
5056 int subset;
5057 int temp;
5058 tree tem;
5059 int in_p;
5060 tree low, high;
5061 int lowequal = ((low0 == 0 && low1 == 0)
5062 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5063 low0, 0, low1, 0)));
5064 int highequal = ((high0 == 0 && high1 == 0)
5065 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5066 high0, 1, high1, 1)));
5068 /* Make range 0 be the range that starts first, or ends last if they
5069 start at the same value. Swap them if it isn't. */
5070 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5071 low0, 0, low1, 0))
5072 || (lowequal
5073 && integer_onep (range_binop (GT_EXPR, integer_type_node,
5074 high1, 1, high0, 1))))
5076 temp = in0_p, in0_p = in1_p, in1_p = temp;
5077 tem = low0, low0 = low1, low1 = tem;
5078 tem = high0, high0 = high1, high1 = tem;
5081 /* Now flag two cases, whether the ranges are disjoint or whether the
5082 second range is totally subsumed in the first. Note that the tests
5083 below are simplified by the ones above. */
5084 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5085 high0, 1, low1, 0));
5086 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5087 high1, 1, high0, 1));
5089 /* We now have four cases, depending on whether we are including or
5090 excluding the two ranges. */
5091 if (in0_p && in1_p)
5093 /* If they don't overlap, the result is false. If the second range
5094 is a subset it is the result. Otherwise, the range is from the start
5095 of the second to the end of the first. */
5096 if (no_overlap)
5097 in_p = 0, low = high = 0;
5098 else if (subset)
5099 in_p = 1, low = low1, high = high1;
5100 else
5101 in_p = 1, low = low1, high = high0;
5104 else if (in0_p && ! in1_p)
5106 /* If they don't overlap, the result is the first range. If they are
5107 equal, the result is false. If the second range is a subset of the
5108 first, and the ranges begin at the same place, we go from just after
5109 the end of the second range to the end of the first. If the second
5110 range is not a subset of the first, or if it is a subset and both
5111 ranges end at the same place, the range starts at the start of the
5112 first range and ends just before the second range.
5113 Otherwise, we can't describe this as a single range. */
5114 if (no_overlap)
5115 in_p = 1, low = low0, high = high0;
5116 else if (lowequal && highequal)
5117 in_p = 0, low = high = 0;
5118 else if (subset && lowequal)
5120 low = range_successor (high1);
5121 high = high0;
5122 in_p = 1;
5123 if (low == 0)
5125 /* We are in the weird situation where high0 > high1 but
5126 high1 has no successor. Punt. */
5127 return 0;
5130 else if (! subset || highequal)
5132 low = low0;
5133 high = range_predecessor (low1);
5134 in_p = 1;
5135 if (high == 0)
5137 /* low0 < low1 but low1 has no predecessor. Punt. */
5138 return 0;
5141 else
5142 return 0;
5145 else if (! in0_p && in1_p)
5147 /* If they don't overlap, the result is the second range. If the second
5148 is a subset of the first, the result is false. Otherwise,
5149 the range starts just after the first range and ends at the
5150 end of the second. */
5151 if (no_overlap)
5152 in_p = 1, low = low1, high = high1;
5153 else if (subset || highequal)
5154 in_p = 0, low = high = 0;
5155 else
5157 low = range_successor (high0);
5158 high = high1;
5159 in_p = 1;
5160 if (low == 0)
5162 /* high1 > high0 but high0 has no successor. Punt. */
5163 return 0;
5168 else
5170 /* The case where we are excluding both ranges. Here the complex case
5171 is if they don't overlap. In that case, the only time we have a
5172 range is if they are adjacent. If the second is a subset of the
5173 first, the result is the first. Otherwise, the range to exclude
5174 starts at the beginning of the first range and ends at the end of the
5175 second. */
5176 if (no_overlap)
5178 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5179 range_successor (high0),
5180 1, low1, 0)))
5181 in_p = 0, low = low0, high = high1;
5182 else
5184 /* Canonicalize - [min, x] into - [-, x]. */
5185 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5186 switch (TREE_CODE (TREE_TYPE (low0)))
5188 case ENUMERAL_TYPE:
5189 if (TYPE_PRECISION (TREE_TYPE (low0))
5190 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
5191 break;
5192 /* FALLTHROUGH */
5193 case INTEGER_TYPE:
5194 if (tree_int_cst_equal (low0,
5195 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5196 low0 = 0;
5197 break;
5198 case POINTER_TYPE:
5199 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5200 && integer_zerop (low0))
5201 low0 = 0;
5202 break;
5203 default:
5204 break;
5207 /* Canonicalize - [x, max] into - [x, -]. */
5208 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5209 switch (TREE_CODE (TREE_TYPE (high1)))
5211 case ENUMERAL_TYPE:
5212 if (TYPE_PRECISION (TREE_TYPE (high1))
5213 != GET_MODE_BITSIZE (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 operations 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 return fold_convert_loc (loc, type,
7108 fold_build2_loc (loc, MULT_EXPR, utype, tem,
7109 fold_convert_loc (loc, utype, same)));
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 i, count;
7294 int size, offset;
7295 tree itype, elem;
7297 offset = 0;
7298 count = VECTOR_CST_NELTS (expr);
7299 itype = TREE_TYPE (TREE_TYPE (expr));
7300 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7301 for (i = 0; i < count; i++)
7303 if (off >= size)
7305 off -= size;
7306 continue;
7308 elem = VECTOR_CST_ELT (expr, i);
7309 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7310 len - offset, off);
7311 if ((off == -1 && res != size) || res == 0)
7312 return 0;
7313 offset += res;
7314 if (offset >= len)
7315 return offset;
7316 if (off != -1)
7317 off = 0;
7319 return offset;
7323 /* Subroutine of native_encode_expr. Encode the STRING_CST
7324 specified by EXPR into the buffer PTR of length LEN bytes.
7325 Return the number of bytes placed in the buffer, or zero
7326 upon failure. */
7328 static int
7329 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7331 tree type = TREE_TYPE (expr);
7333 /* Wide-char strings are encoded in target byte-order so native
7334 encoding them is trivial. */
7335 if (BITS_PER_UNIT != CHAR_BIT
7336 || TREE_CODE (type) != ARRAY_TYPE
7337 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7338 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7339 return 0;
7341 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7342 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7343 return 0;
7344 if (off == -1)
7345 off = 0;
7346 if (ptr == NULL)
7347 /* Dry run. */;
7348 else if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7350 int written = 0;
7351 if (off < TREE_STRING_LENGTH (expr))
7353 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7354 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7356 memset (ptr + written, 0,
7357 MIN (total_bytes - written, len - written));
7359 else
7360 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7361 return MIN (total_bytes - off, len);
7365 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7366 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7367 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7368 anything, just do a dry run. If OFF is not -1 then start
7369 the encoding at byte offset OFF and encode at most LEN bytes.
7370 Return the number of bytes placed in the buffer, or zero upon failure. */
7373 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7375 /* We don't support starting at negative offset and -1 is special. */
7376 if (off < -1)
7377 return 0;
7379 switch (TREE_CODE (expr))
7381 case INTEGER_CST:
7382 return native_encode_int (expr, ptr, len, off);
7384 case REAL_CST:
7385 return native_encode_real (expr, ptr, len, off);
7387 case FIXED_CST:
7388 return native_encode_fixed (expr, ptr, len, off);
7390 case COMPLEX_CST:
7391 return native_encode_complex (expr, ptr, len, off);
7393 case VECTOR_CST:
7394 return native_encode_vector (expr, ptr, len, off);
7396 case STRING_CST:
7397 return native_encode_string (expr, ptr, len, off);
7399 default:
7400 return 0;
7405 /* Subroutine of native_interpret_expr. Interpret the contents of
7406 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7407 If the buffer cannot be interpreted, return NULL_TREE. */
7409 static tree
7410 native_interpret_int (tree type, const unsigned char *ptr, int len)
7412 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7414 if (total_bytes > len
7415 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7416 return NULL_TREE;
7418 wide_int result = wi::from_buffer (ptr, total_bytes);
7420 return wide_int_to_tree (type, result);
7424 /* Subroutine of native_interpret_expr. Interpret the contents of
7425 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7426 If the buffer cannot be interpreted, return NULL_TREE. */
7428 static tree
7429 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7431 scalar_mode mode = SCALAR_TYPE_MODE (type);
7432 int total_bytes = GET_MODE_SIZE (mode);
7433 double_int result;
7434 FIXED_VALUE_TYPE fixed_value;
7436 if (total_bytes > len
7437 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7438 return NULL_TREE;
7440 result = double_int::from_buffer (ptr, total_bytes);
7441 fixed_value = fixed_from_double_int (result, mode);
7443 return build_fixed (type, fixed_value);
7447 /* Subroutine of native_interpret_expr. Interpret the contents of
7448 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7449 If the buffer cannot be interpreted, return NULL_TREE. */
7451 static tree
7452 native_interpret_real (tree type, const unsigned char *ptr, int len)
7454 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
7455 int total_bytes = GET_MODE_SIZE (mode);
7456 unsigned char value;
7457 /* There are always 32 bits in each long, no matter the size of
7458 the hosts long. We handle floating point representations with
7459 up to 192 bits. */
7460 REAL_VALUE_TYPE r;
7461 long tmp[6];
7463 if (total_bytes > len || total_bytes > 24)
7464 return NULL_TREE;
7465 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7467 memset (tmp, 0, sizeof (tmp));
7468 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7469 bitpos += BITS_PER_UNIT)
7471 /* Both OFFSET and BYTE index within a long;
7472 bitpos indexes the whole float. */
7473 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7474 if (UNITS_PER_WORD < 4)
7476 int word = byte / UNITS_PER_WORD;
7477 if (WORDS_BIG_ENDIAN)
7478 word = (words - 1) - word;
7479 offset = word * UNITS_PER_WORD;
7480 if (BYTES_BIG_ENDIAN)
7481 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7482 else
7483 offset += byte % UNITS_PER_WORD;
7485 else
7487 offset = byte;
7488 if (BYTES_BIG_ENDIAN)
7490 /* Reverse bytes within each long, or within the entire float
7491 if it's smaller than a long (for HFmode). */
7492 offset = MIN (3, total_bytes - 1) - offset;
7493 gcc_assert (offset >= 0);
7496 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7498 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7501 real_from_target (&r, tmp, mode);
7502 return build_real (type, r);
7506 /* Subroutine of native_interpret_expr. Interpret the contents of
7507 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7508 If the buffer cannot be interpreted, return NULL_TREE. */
7510 static tree
7511 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7513 tree etype, rpart, ipart;
7514 int size;
7516 etype = TREE_TYPE (type);
7517 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7518 if (size * 2 > len)
7519 return NULL_TREE;
7520 rpart = native_interpret_expr (etype, ptr, size);
7521 if (!rpart)
7522 return NULL_TREE;
7523 ipart = native_interpret_expr (etype, ptr+size, size);
7524 if (!ipart)
7525 return NULL_TREE;
7526 return build_complex (type, rpart, ipart);
7530 /* Subroutine of native_interpret_expr. Interpret the contents of
7531 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7532 If the buffer cannot be interpreted, return NULL_TREE. */
7534 static tree
7535 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7537 tree etype, elem;
7538 int i, size, count;
7540 etype = TREE_TYPE (type);
7541 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7542 count = TYPE_VECTOR_SUBPARTS (type);
7543 if (size * count > len)
7544 return NULL_TREE;
7546 tree_vector_builder elements (type, count, 1);
7547 for (i = 0; i < count; ++i)
7549 elem = native_interpret_expr (etype, ptr+(i*size), size);
7550 if (!elem)
7551 return NULL_TREE;
7552 elements.quick_push (elem);
7554 return elements.build ();
7558 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7559 the buffer PTR of length LEN as a constant of type TYPE. For
7560 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7561 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7562 return NULL_TREE. */
7564 tree
7565 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7567 switch (TREE_CODE (type))
7569 case INTEGER_TYPE:
7570 case ENUMERAL_TYPE:
7571 case BOOLEAN_TYPE:
7572 case POINTER_TYPE:
7573 case REFERENCE_TYPE:
7574 return native_interpret_int (type, ptr, len);
7576 case REAL_TYPE:
7577 return native_interpret_real (type, ptr, len);
7579 case FIXED_POINT_TYPE:
7580 return native_interpret_fixed (type, ptr, len);
7582 case COMPLEX_TYPE:
7583 return native_interpret_complex (type, ptr, len);
7585 case VECTOR_TYPE:
7586 return native_interpret_vector (type, ptr, len);
7588 default:
7589 return NULL_TREE;
7593 /* Returns true if we can interpret the contents of a native encoding
7594 as TYPE. */
7596 static bool
7597 can_native_interpret_type_p (tree type)
7599 switch (TREE_CODE (type))
7601 case INTEGER_TYPE:
7602 case ENUMERAL_TYPE:
7603 case BOOLEAN_TYPE:
7604 case POINTER_TYPE:
7605 case REFERENCE_TYPE:
7606 case FIXED_POINT_TYPE:
7607 case REAL_TYPE:
7608 case COMPLEX_TYPE:
7609 case VECTOR_TYPE:
7610 return true;
7611 default:
7612 return false;
7617 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7618 TYPE at compile-time. If we're unable to perform the conversion
7619 return NULL_TREE. */
7621 static tree
7622 fold_view_convert_expr (tree type, tree expr)
7624 /* We support up to 512-bit values (for V8DFmode). */
7625 unsigned char buffer[64];
7626 int len;
7628 /* Check that the host and target are sane. */
7629 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7630 return NULL_TREE;
7632 len = native_encode_expr (expr, buffer, sizeof (buffer));
7633 if (len == 0)
7634 return NULL_TREE;
7636 return native_interpret_expr (type, buffer, len);
7639 /* Build an expression for the address of T. Folds away INDIRECT_REF
7640 to avoid confusing the gimplify process. */
7642 tree
7643 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7645 /* The size of the object is not relevant when talking about its address. */
7646 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7647 t = TREE_OPERAND (t, 0);
7649 if (TREE_CODE (t) == INDIRECT_REF)
7651 t = TREE_OPERAND (t, 0);
7653 if (TREE_TYPE (t) != ptrtype)
7654 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7656 else if (TREE_CODE (t) == MEM_REF
7657 && integer_zerop (TREE_OPERAND (t, 1)))
7658 return TREE_OPERAND (t, 0);
7659 else if (TREE_CODE (t) == MEM_REF
7660 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7661 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7662 TREE_OPERAND (t, 0),
7663 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7664 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7666 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7668 if (TREE_TYPE (t) != ptrtype)
7669 t = fold_convert_loc (loc, ptrtype, t);
7671 else
7672 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7674 return t;
7677 /* Build an expression for the address of T. */
7679 tree
7680 build_fold_addr_expr_loc (location_t loc, tree t)
7682 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7684 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7687 /* Fold a unary expression of code CODE and type TYPE with operand
7688 OP0. Return the folded expression if folding is successful.
7689 Otherwise, return NULL_TREE. */
7691 tree
7692 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7694 tree tem;
7695 tree arg0;
7696 enum tree_code_class kind = TREE_CODE_CLASS (code);
7698 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7699 && TREE_CODE_LENGTH (code) == 1);
7701 arg0 = op0;
7702 if (arg0)
7704 if (CONVERT_EXPR_CODE_P (code)
7705 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7707 /* Don't use STRIP_NOPS, because signedness of argument type
7708 matters. */
7709 STRIP_SIGN_NOPS (arg0);
7711 else
7713 /* Strip any conversions that don't change the mode. This
7714 is safe for every expression, except for a comparison
7715 expression because its signedness is derived from its
7716 operands.
7718 Note that this is done as an internal manipulation within
7719 the constant folder, in order to find the simplest
7720 representation of the arguments so that their form can be
7721 studied. In any cases, the appropriate type conversions
7722 should be put back in the tree that will get out of the
7723 constant folder. */
7724 STRIP_NOPS (arg0);
7727 if (CONSTANT_CLASS_P (arg0))
7729 tree tem = const_unop (code, type, arg0);
7730 if (tem)
7732 if (TREE_TYPE (tem) != type)
7733 tem = fold_convert_loc (loc, type, tem);
7734 return tem;
7739 tem = generic_simplify (loc, code, type, op0);
7740 if (tem)
7741 return tem;
7743 if (TREE_CODE_CLASS (code) == tcc_unary)
7745 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7746 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7747 fold_build1_loc (loc, code, type,
7748 fold_convert_loc (loc, TREE_TYPE (op0),
7749 TREE_OPERAND (arg0, 1))));
7750 else if (TREE_CODE (arg0) == COND_EXPR)
7752 tree arg01 = TREE_OPERAND (arg0, 1);
7753 tree arg02 = TREE_OPERAND (arg0, 2);
7754 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7755 arg01 = fold_build1_loc (loc, code, type,
7756 fold_convert_loc (loc,
7757 TREE_TYPE (op0), arg01));
7758 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7759 arg02 = fold_build1_loc (loc, code, type,
7760 fold_convert_loc (loc,
7761 TREE_TYPE (op0), arg02));
7762 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7763 arg01, arg02);
7765 /* If this was a conversion, and all we did was to move into
7766 inside the COND_EXPR, bring it back out. But leave it if
7767 it is a conversion from integer to integer and the
7768 result precision is no wider than a word since such a
7769 conversion is cheap and may be optimized away by combine,
7770 while it couldn't if it were outside the COND_EXPR. Then return
7771 so we don't get into an infinite recursion loop taking the
7772 conversion out and then back in. */
7774 if ((CONVERT_EXPR_CODE_P (code)
7775 || code == NON_LVALUE_EXPR)
7776 && TREE_CODE (tem) == COND_EXPR
7777 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7778 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7779 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7780 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7781 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7782 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7783 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7784 && (INTEGRAL_TYPE_P
7785 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7786 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7787 || flag_syntax_only))
7788 tem = build1_loc (loc, code, type,
7789 build3 (COND_EXPR,
7790 TREE_TYPE (TREE_OPERAND
7791 (TREE_OPERAND (tem, 1), 0)),
7792 TREE_OPERAND (tem, 0),
7793 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7794 TREE_OPERAND (TREE_OPERAND (tem, 2),
7795 0)));
7796 return tem;
7800 switch (code)
7802 case NON_LVALUE_EXPR:
7803 if (!maybe_lvalue_p (op0))
7804 return fold_convert_loc (loc, type, op0);
7805 return NULL_TREE;
7807 CASE_CONVERT:
7808 case FLOAT_EXPR:
7809 case FIX_TRUNC_EXPR:
7810 if (COMPARISON_CLASS_P (op0))
7812 /* If we have (type) (a CMP b) and type is an integral type, return
7813 new expression involving the new type. Canonicalize
7814 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7815 non-integral type.
7816 Do not fold the result as that would not simplify further, also
7817 folding again results in recursions. */
7818 if (TREE_CODE (type) == BOOLEAN_TYPE)
7819 return build2_loc (loc, TREE_CODE (op0), type,
7820 TREE_OPERAND (op0, 0),
7821 TREE_OPERAND (op0, 1));
7822 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7823 && TREE_CODE (type) != VECTOR_TYPE)
7824 return build3_loc (loc, COND_EXPR, type, op0,
7825 constant_boolean_node (true, type),
7826 constant_boolean_node (false, type));
7829 /* Handle (T *)&A.B.C for A being of type T and B and C
7830 living at offset zero. This occurs frequently in
7831 C++ upcasting and then accessing the base. */
7832 if (TREE_CODE (op0) == ADDR_EXPR
7833 && POINTER_TYPE_P (type)
7834 && handled_component_p (TREE_OPERAND (op0, 0)))
7836 poly_int64 bitsize, bitpos;
7837 tree offset;
7838 machine_mode mode;
7839 int unsignedp, reversep, volatilep;
7840 tree base
7841 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7842 &offset, &mode, &unsignedp, &reversep,
7843 &volatilep);
7844 /* If the reference was to a (constant) zero offset, we can use
7845 the address of the base if it has the same base type
7846 as the result type and the pointer type is unqualified. */
7847 if (!offset
7848 && known_eq (bitpos, 0)
7849 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7850 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7851 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7852 return fold_convert_loc (loc, type,
7853 build_fold_addr_expr_loc (loc, base));
7856 if (TREE_CODE (op0) == MODIFY_EXPR
7857 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7858 /* Detect assigning a bitfield. */
7859 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7860 && DECL_BIT_FIELD
7861 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7863 /* Don't leave an assignment inside a conversion
7864 unless assigning a bitfield. */
7865 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7866 /* First do the assignment, then return converted constant. */
7867 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7868 TREE_NO_WARNING (tem) = 1;
7869 TREE_USED (tem) = 1;
7870 return tem;
7873 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7874 constants (if x has signed type, the sign bit cannot be set
7875 in c). This folds extension into the BIT_AND_EXPR.
7876 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7877 very likely don't have maximal range for their precision and this
7878 transformation effectively doesn't preserve non-maximal ranges. */
7879 if (TREE_CODE (type) == INTEGER_TYPE
7880 && TREE_CODE (op0) == BIT_AND_EXPR
7881 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7883 tree and_expr = op0;
7884 tree and0 = TREE_OPERAND (and_expr, 0);
7885 tree and1 = TREE_OPERAND (and_expr, 1);
7886 int change = 0;
7888 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7889 || (TYPE_PRECISION (type)
7890 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7891 change = 1;
7892 else if (TYPE_PRECISION (TREE_TYPE (and1))
7893 <= HOST_BITS_PER_WIDE_INT
7894 && tree_fits_uhwi_p (and1))
7896 unsigned HOST_WIDE_INT cst;
7898 cst = tree_to_uhwi (and1);
7899 cst &= HOST_WIDE_INT_M1U
7900 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7901 change = (cst == 0);
7902 if (change
7903 && !flag_syntax_only
7904 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7905 == ZERO_EXTEND))
7907 tree uns = unsigned_type_for (TREE_TYPE (and0));
7908 and0 = fold_convert_loc (loc, uns, and0);
7909 and1 = fold_convert_loc (loc, uns, and1);
7912 if (change)
7914 tem = force_fit_type (type, wi::to_widest (and1), 0,
7915 TREE_OVERFLOW (and1));
7916 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7917 fold_convert_loc (loc, type, and0), tem);
7921 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7922 cast (T1)X will fold away. We assume that this happens when X itself
7923 is a cast. */
7924 if (POINTER_TYPE_P (type)
7925 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7926 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7928 tree arg00 = TREE_OPERAND (arg0, 0);
7929 tree arg01 = TREE_OPERAND (arg0, 1);
7931 return fold_build_pointer_plus_loc
7932 (loc, fold_convert_loc (loc, type, arg00), arg01);
7935 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7936 of the same precision, and X is an integer type not narrower than
7937 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7938 if (INTEGRAL_TYPE_P (type)
7939 && TREE_CODE (op0) == BIT_NOT_EXPR
7940 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7941 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7942 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7944 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7945 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7946 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7947 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7948 fold_convert_loc (loc, type, tem));
7951 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7952 type of X and Y (integer types only). */
7953 if (INTEGRAL_TYPE_P (type)
7954 && TREE_CODE (op0) == MULT_EXPR
7955 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7956 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7958 /* Be careful not to introduce new overflows. */
7959 tree mult_type;
7960 if (TYPE_OVERFLOW_WRAPS (type))
7961 mult_type = type;
7962 else
7963 mult_type = unsigned_type_for (type);
7965 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7967 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7968 fold_convert_loc (loc, mult_type,
7969 TREE_OPERAND (op0, 0)),
7970 fold_convert_loc (loc, mult_type,
7971 TREE_OPERAND (op0, 1)));
7972 return fold_convert_loc (loc, type, tem);
7976 return NULL_TREE;
7978 case VIEW_CONVERT_EXPR:
7979 if (TREE_CODE (op0) == MEM_REF)
7981 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7982 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7983 tem = fold_build2_loc (loc, MEM_REF, type,
7984 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7985 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7986 return tem;
7989 return NULL_TREE;
7991 case NEGATE_EXPR:
7992 tem = fold_negate_expr (loc, arg0);
7993 if (tem)
7994 return fold_convert_loc (loc, type, tem);
7995 return NULL_TREE;
7997 case ABS_EXPR:
7998 /* Convert fabs((double)float) into (double)fabsf(float). */
7999 if (TREE_CODE (arg0) == NOP_EXPR
8000 && TREE_CODE (type) == REAL_TYPE)
8002 tree targ0 = strip_float_extensions (arg0);
8003 if (targ0 != arg0)
8004 return fold_convert_loc (loc, type,
8005 fold_build1_loc (loc, ABS_EXPR,
8006 TREE_TYPE (targ0),
8007 targ0));
8009 return NULL_TREE;
8011 case BIT_NOT_EXPR:
8012 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8013 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8014 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8015 fold_convert_loc (loc, type,
8016 TREE_OPERAND (arg0, 0)))))
8017 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8018 fold_convert_loc (loc, type,
8019 TREE_OPERAND (arg0, 1)));
8020 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8021 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8022 fold_convert_loc (loc, type,
8023 TREE_OPERAND (arg0, 1)))))
8024 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8025 fold_convert_loc (loc, type,
8026 TREE_OPERAND (arg0, 0)), tem);
8028 return NULL_TREE;
8030 case TRUTH_NOT_EXPR:
8031 /* Note that the operand of this must be an int
8032 and its values must be 0 or 1.
8033 ("true" is a fixed value perhaps depending on the language,
8034 but we don't handle values other than 1 correctly yet.) */
8035 tem = fold_truth_not_expr (loc, arg0);
8036 if (!tem)
8037 return NULL_TREE;
8038 return fold_convert_loc (loc, type, tem);
8040 case INDIRECT_REF:
8041 /* Fold *&X to X if X is an lvalue. */
8042 if (TREE_CODE (op0) == ADDR_EXPR)
8044 tree op00 = TREE_OPERAND (op0, 0);
8045 if ((VAR_P (op00)
8046 || TREE_CODE (op00) == PARM_DECL
8047 || TREE_CODE (op00) == RESULT_DECL)
8048 && !TREE_READONLY (op00))
8049 return op00;
8051 return NULL_TREE;
8053 default:
8054 return NULL_TREE;
8055 } /* switch (code) */
8059 /* If the operation was a conversion do _not_ mark a resulting constant
8060 with TREE_OVERFLOW if the original constant was not. These conversions
8061 have implementation defined behavior and retaining the TREE_OVERFLOW
8062 flag here would confuse later passes such as VRP. */
8063 tree
8064 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8065 tree type, tree op0)
8067 tree res = fold_unary_loc (loc, code, type, op0);
8068 if (res
8069 && TREE_CODE (res) == INTEGER_CST
8070 && TREE_CODE (op0) == INTEGER_CST
8071 && CONVERT_EXPR_CODE_P (code))
8072 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8074 return res;
8077 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8078 operands OP0 and OP1. LOC is the location of the resulting expression.
8079 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8080 Return the folded expression if folding is successful. Otherwise,
8081 return NULL_TREE. */
8082 static tree
8083 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8084 tree arg0, tree arg1, tree op0, tree op1)
8086 tree tem;
8088 /* We only do these simplifications if we are optimizing. */
8089 if (!optimize)
8090 return NULL_TREE;
8092 /* Check for things like (A || B) && (A || C). We can convert this
8093 to A || (B && C). Note that either operator can be any of the four
8094 truth and/or operations and the transformation will still be
8095 valid. Also note that we only care about order for the
8096 ANDIF and ORIF operators. If B contains side effects, this
8097 might change the truth-value of A. */
8098 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8099 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8100 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8101 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8102 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8103 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8105 tree a00 = TREE_OPERAND (arg0, 0);
8106 tree a01 = TREE_OPERAND (arg0, 1);
8107 tree a10 = TREE_OPERAND (arg1, 0);
8108 tree a11 = TREE_OPERAND (arg1, 1);
8109 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8110 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8111 && (code == TRUTH_AND_EXPR
8112 || code == TRUTH_OR_EXPR));
8114 if (operand_equal_p (a00, a10, 0))
8115 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8116 fold_build2_loc (loc, code, type, a01, a11));
8117 else if (commutative && operand_equal_p (a00, a11, 0))
8118 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8119 fold_build2_loc (loc, code, type, a01, a10));
8120 else if (commutative && operand_equal_p (a01, a10, 0))
8121 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8122 fold_build2_loc (loc, code, type, a00, a11));
8124 /* This case if tricky because we must either have commutative
8125 operators or else A10 must not have side-effects. */
8127 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8128 && operand_equal_p (a01, a11, 0))
8129 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8130 fold_build2_loc (loc, code, type, a00, a10),
8131 a01);
8134 /* See if we can build a range comparison. */
8135 if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
8136 return tem;
8138 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8139 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8141 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8142 if (tem)
8143 return fold_build2_loc (loc, code, type, tem, arg1);
8146 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8147 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8149 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8150 if (tem)
8151 return fold_build2_loc (loc, code, type, arg0, tem);
8154 /* Check for the possibility of merging component references. If our
8155 lhs is another similar operation, try to merge its rhs with our
8156 rhs. Then try to merge our lhs and rhs. */
8157 if (TREE_CODE (arg0) == code
8158 && (tem = fold_truth_andor_1 (loc, code, type,
8159 TREE_OPERAND (arg0, 1), arg1)) != 0)
8160 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8162 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8163 return tem;
8165 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8166 && !flag_sanitize_coverage
8167 && (code == TRUTH_AND_EXPR
8168 || code == TRUTH_ANDIF_EXPR
8169 || code == TRUTH_OR_EXPR
8170 || code == TRUTH_ORIF_EXPR))
8172 enum tree_code ncode, icode;
8174 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8175 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8176 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8178 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8179 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8180 We don't want to pack more than two leafs to a non-IF AND/OR
8181 expression.
8182 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8183 equal to IF-CODE, then we don't want to add right-hand operand.
8184 If the inner right-hand side of left-hand operand has
8185 side-effects, or isn't simple, then we can't add to it,
8186 as otherwise we might destroy if-sequence. */
8187 if (TREE_CODE (arg0) == icode
8188 && simple_operand_p_2 (arg1)
8189 /* Needed for sequence points to handle trappings, and
8190 side-effects. */
8191 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8193 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8194 arg1);
8195 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8196 tem);
8198 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8199 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8200 else if (TREE_CODE (arg1) == icode
8201 && simple_operand_p_2 (arg0)
8202 /* Needed for sequence points to handle trappings, and
8203 side-effects. */
8204 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8206 tem = fold_build2_loc (loc, ncode, type,
8207 arg0, TREE_OPERAND (arg1, 0));
8208 return fold_build2_loc (loc, icode, type, tem,
8209 TREE_OPERAND (arg1, 1));
8211 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8212 into (A OR B).
8213 For sequence point consistancy, we need to check for trapping,
8214 and side-effects. */
8215 else if (code == icode && simple_operand_p_2 (arg0)
8216 && simple_operand_p_2 (arg1))
8217 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8220 return NULL_TREE;
8223 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8224 by changing CODE to reduce the magnitude of constants involved in
8225 ARG0 of the comparison.
8226 Returns a canonicalized comparison tree if a simplification was
8227 possible, otherwise returns NULL_TREE.
8228 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8229 valid if signed overflow is undefined. */
8231 static tree
8232 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8233 tree arg0, tree arg1,
8234 bool *strict_overflow_p)
8236 enum tree_code code0 = TREE_CODE (arg0);
8237 tree t, cst0 = NULL_TREE;
8238 int sgn0;
8240 /* Match A +- CST code arg1. We can change this only if overflow
8241 is undefined. */
8242 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8243 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8244 /* In principle pointers also have undefined overflow behavior,
8245 but that causes problems elsewhere. */
8246 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8247 && (code0 == MINUS_EXPR
8248 || code0 == PLUS_EXPR)
8249 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8250 return NULL_TREE;
8252 /* Identify the constant in arg0 and its sign. */
8253 cst0 = TREE_OPERAND (arg0, 1);
8254 sgn0 = tree_int_cst_sgn (cst0);
8256 /* Overflowed constants and zero will cause problems. */
8257 if (integer_zerop (cst0)
8258 || TREE_OVERFLOW (cst0))
8259 return NULL_TREE;
8261 /* See if we can reduce the magnitude of the constant in
8262 arg0 by changing the comparison code. */
8263 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8264 if (code == LT_EXPR
8265 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8266 code = LE_EXPR;
8267 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8268 else if (code == GT_EXPR
8269 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8270 code = GE_EXPR;
8271 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8272 else if (code == LE_EXPR
8273 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8274 code = LT_EXPR;
8275 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8276 else if (code == GE_EXPR
8277 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8278 code = GT_EXPR;
8279 else
8280 return NULL_TREE;
8281 *strict_overflow_p = true;
8283 /* Now build the constant reduced in magnitude. But not if that
8284 would produce one outside of its types range. */
8285 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8286 && ((sgn0 == 1
8287 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8288 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8289 || (sgn0 == -1
8290 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8291 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8292 return NULL_TREE;
8294 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8295 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8296 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8297 t = fold_convert (TREE_TYPE (arg1), t);
8299 return fold_build2_loc (loc, code, type, t, arg1);
8302 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8303 overflow further. Try to decrease the magnitude of constants involved
8304 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8305 and put sole constants at the second argument position.
8306 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8308 static tree
8309 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8310 tree arg0, tree arg1)
8312 tree t;
8313 bool strict_overflow_p;
8314 const char * const warnmsg = G_("assuming signed overflow does not occur "
8315 "when reducing constant in comparison");
8317 /* Try canonicalization by simplifying arg0. */
8318 strict_overflow_p = false;
8319 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8320 &strict_overflow_p);
8321 if (t)
8323 if (strict_overflow_p)
8324 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8325 return t;
8328 /* Try canonicalization by simplifying arg1 using the swapped
8329 comparison. */
8330 code = swap_tree_comparison (code);
8331 strict_overflow_p = false;
8332 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8333 &strict_overflow_p);
8334 if (t && strict_overflow_p)
8335 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8336 return t;
8339 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8340 space. This is used to avoid issuing overflow warnings for
8341 expressions like &p->x which can not wrap. */
8343 static bool
8344 pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
8346 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8347 return true;
8349 if (maybe_lt (bitpos, 0))
8350 return true;
8352 poly_wide_int wi_offset;
8353 int precision = TYPE_PRECISION (TREE_TYPE (base));
8354 if (offset == NULL_TREE)
8355 wi_offset = wi::zero (precision);
8356 else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
8357 return true;
8358 else
8359 wi_offset = wi::to_poly_wide (offset);
8361 bool overflow;
8362 poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
8363 precision);
8364 poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8365 if (overflow)
8366 return true;
8368 poly_uint64 total_hwi, size;
8369 if (!total.to_uhwi (&total_hwi)
8370 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
8371 &size)
8372 || known_eq (size, 0U))
8373 return true;
8375 if (known_le (total_hwi, size))
8376 return false;
8378 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8379 array. */
8380 if (TREE_CODE (base) == ADDR_EXPR
8381 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
8382 &size)
8383 && maybe_ne (size, 0U)
8384 && known_le (total_hwi, size))
8385 return false;
8387 return true;
8390 /* Return a positive integer when the symbol DECL is known to have
8391 a nonzero address, zero when it's known not to (e.g., it's a weak
8392 symbol), and a negative integer when the symbol is not yet in the
8393 symbol table and so whether or not its address is zero is unknown.
8394 For function local objects always return positive integer. */
8395 static int
8396 maybe_nonzero_address (tree decl)
8398 if (DECL_P (decl) && decl_in_symtab_p (decl))
8399 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8400 return symbol->nonzero_address ();
8402 /* Function local objects are never NULL. */
8403 if (DECL_P (decl)
8404 && (DECL_CONTEXT (decl)
8405 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8406 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8407 return 1;
8409 return -1;
8412 /* Subroutine of fold_binary. This routine performs all of the
8413 transformations that are common to the equality/inequality
8414 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8415 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8416 fold_binary should call fold_binary. Fold a comparison with
8417 tree code CODE and type TYPE with operands OP0 and OP1. Return
8418 the folded comparison or NULL_TREE. */
8420 static tree
8421 fold_comparison (location_t loc, enum tree_code code, tree type,
8422 tree op0, tree op1)
8424 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8425 tree arg0, arg1, tem;
8427 arg0 = op0;
8428 arg1 = op1;
8430 STRIP_SIGN_NOPS (arg0);
8431 STRIP_SIGN_NOPS (arg1);
8433 /* For comparisons of pointers we can decompose it to a compile time
8434 comparison of the base objects and the offsets into the object.
8435 This requires at least one operand being an ADDR_EXPR or a
8436 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8437 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8438 && (TREE_CODE (arg0) == ADDR_EXPR
8439 || TREE_CODE (arg1) == ADDR_EXPR
8440 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8441 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8443 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8444 poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
8445 machine_mode mode;
8446 int volatilep, reversep, unsignedp;
8447 bool indirect_base0 = false, indirect_base1 = false;
8449 /* Get base and offset for the access. Strip ADDR_EXPR for
8450 get_inner_reference, but put it back by stripping INDIRECT_REF
8451 off the base object if possible. indirect_baseN will be true
8452 if baseN is not an address but refers to the object itself. */
8453 base0 = arg0;
8454 if (TREE_CODE (arg0) == ADDR_EXPR)
8456 base0
8457 = get_inner_reference (TREE_OPERAND (arg0, 0),
8458 &bitsize, &bitpos0, &offset0, &mode,
8459 &unsignedp, &reversep, &volatilep);
8460 if (TREE_CODE (base0) == INDIRECT_REF)
8461 base0 = TREE_OPERAND (base0, 0);
8462 else
8463 indirect_base0 = true;
8465 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8467 base0 = TREE_OPERAND (arg0, 0);
8468 STRIP_SIGN_NOPS (base0);
8469 if (TREE_CODE (base0) == ADDR_EXPR)
8471 base0
8472 = get_inner_reference (TREE_OPERAND (base0, 0),
8473 &bitsize, &bitpos0, &offset0, &mode,
8474 &unsignedp, &reversep, &volatilep);
8475 if (TREE_CODE (base0) == INDIRECT_REF)
8476 base0 = TREE_OPERAND (base0, 0);
8477 else
8478 indirect_base0 = true;
8480 if (offset0 == NULL_TREE || integer_zerop (offset0))
8481 offset0 = TREE_OPERAND (arg0, 1);
8482 else
8483 offset0 = size_binop (PLUS_EXPR, offset0,
8484 TREE_OPERAND (arg0, 1));
8485 if (poly_int_tree_p (offset0))
8487 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
8488 TYPE_PRECISION (sizetype));
8489 tem <<= LOG2_BITS_PER_UNIT;
8490 tem += bitpos0;
8491 if (tem.to_shwi (&bitpos0))
8492 offset0 = NULL_TREE;
8496 base1 = arg1;
8497 if (TREE_CODE (arg1) == ADDR_EXPR)
8499 base1
8500 = get_inner_reference (TREE_OPERAND (arg1, 0),
8501 &bitsize, &bitpos1, &offset1, &mode,
8502 &unsignedp, &reversep, &volatilep);
8503 if (TREE_CODE (base1) == INDIRECT_REF)
8504 base1 = TREE_OPERAND (base1, 0);
8505 else
8506 indirect_base1 = true;
8508 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8510 base1 = TREE_OPERAND (arg1, 0);
8511 STRIP_SIGN_NOPS (base1);
8512 if (TREE_CODE (base1) == ADDR_EXPR)
8514 base1
8515 = get_inner_reference (TREE_OPERAND (base1, 0),
8516 &bitsize, &bitpos1, &offset1, &mode,
8517 &unsignedp, &reversep, &volatilep);
8518 if (TREE_CODE (base1) == INDIRECT_REF)
8519 base1 = TREE_OPERAND (base1, 0);
8520 else
8521 indirect_base1 = true;
8523 if (offset1 == NULL_TREE || integer_zerop (offset1))
8524 offset1 = TREE_OPERAND (arg1, 1);
8525 else
8526 offset1 = size_binop (PLUS_EXPR, offset1,
8527 TREE_OPERAND (arg1, 1));
8528 if (poly_int_tree_p (offset1))
8530 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
8531 TYPE_PRECISION (sizetype));
8532 tem <<= LOG2_BITS_PER_UNIT;
8533 tem += bitpos1;
8534 if (tem.to_shwi (&bitpos1))
8535 offset1 = NULL_TREE;
8539 /* If we have equivalent bases we might be able to simplify. */
8540 if (indirect_base0 == indirect_base1
8541 && operand_equal_p (base0, base1,
8542 indirect_base0 ? OEP_ADDRESS_OF : 0))
8544 /* We can fold this expression to a constant if the non-constant
8545 offset parts are equal. */
8546 if (offset0 == offset1
8547 || (offset0 && offset1
8548 && operand_equal_p (offset0, offset1, 0)))
8550 if (!equality_code
8551 && maybe_ne (bitpos0, bitpos1)
8552 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8553 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8554 fold_overflow_warning (("assuming pointer wraparound does not "
8555 "occur when comparing P +- C1 with "
8556 "P +- C2"),
8557 WARN_STRICT_OVERFLOW_CONDITIONAL);
8559 switch (code)
8561 case EQ_EXPR:
8562 if (known_eq (bitpos0, bitpos1))
8563 return boolean_true_node;
8564 if (known_ne (bitpos0, bitpos1))
8565 return boolean_false_node;
8566 break;
8567 case NE_EXPR:
8568 if (known_ne (bitpos0, bitpos1))
8569 return boolean_true_node;
8570 if (known_eq (bitpos0, bitpos1))
8571 return boolean_false_node;
8572 break;
8573 case LT_EXPR:
8574 if (known_lt (bitpos0, bitpos1))
8575 return boolean_true_node;
8576 if (known_ge (bitpos0, bitpos1))
8577 return boolean_false_node;
8578 break;
8579 case LE_EXPR:
8580 if (known_le (bitpos0, bitpos1))
8581 return boolean_true_node;
8582 if (known_gt (bitpos0, bitpos1))
8583 return boolean_false_node;
8584 break;
8585 case GE_EXPR:
8586 if (known_ge (bitpos0, bitpos1))
8587 return boolean_true_node;
8588 if (known_lt (bitpos0, bitpos1))
8589 return boolean_false_node;
8590 break;
8591 case GT_EXPR:
8592 if (known_gt (bitpos0, bitpos1))
8593 return boolean_true_node;
8594 if (known_le (bitpos0, bitpos1))
8595 return boolean_false_node;
8596 break;
8597 default:;
8600 /* We can simplify the comparison to a comparison of the variable
8601 offset parts if the constant offset parts are equal.
8602 Be careful to use signed sizetype here because otherwise we
8603 mess with array offsets in the wrong way. This is possible
8604 because pointer arithmetic is restricted to retain within an
8605 object and overflow on pointer differences is undefined as of
8606 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8607 else if (known_eq (bitpos0, bitpos1))
8609 /* By converting to signed sizetype we cover middle-end pointer
8610 arithmetic which operates on unsigned pointer types of size
8611 type size and ARRAY_REF offsets which are properly sign or
8612 zero extended from their type in case it is narrower than
8613 sizetype. */
8614 if (offset0 == NULL_TREE)
8615 offset0 = build_int_cst (ssizetype, 0);
8616 else
8617 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8618 if (offset1 == NULL_TREE)
8619 offset1 = build_int_cst (ssizetype, 0);
8620 else
8621 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8623 if (!equality_code
8624 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8625 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8626 fold_overflow_warning (("assuming pointer wraparound does not "
8627 "occur when comparing P +- C1 with "
8628 "P +- C2"),
8629 WARN_STRICT_OVERFLOW_COMPARISON);
8631 return fold_build2_loc (loc, code, type, offset0, offset1);
8634 /* For equal offsets we can simplify to a comparison of the
8635 base addresses. */
8636 else if (known_eq (bitpos0, bitpos1)
8637 && (indirect_base0
8638 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8639 && (indirect_base1
8640 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8641 && ((offset0 == offset1)
8642 || (offset0 && offset1
8643 && operand_equal_p (offset0, offset1, 0))))
8645 if (indirect_base0)
8646 base0 = build_fold_addr_expr_loc (loc, base0);
8647 if (indirect_base1)
8648 base1 = build_fold_addr_expr_loc (loc, base1);
8649 return fold_build2_loc (loc, code, type, base0, base1);
8651 /* Comparison between an ordinary (non-weak) symbol and a null
8652 pointer can be eliminated since such symbols must have a non
8653 null address. In C, relational expressions between pointers
8654 to objects and null pointers are undefined. The results
8655 below follow the C++ rules with the additional property that
8656 every object pointer compares greater than a null pointer.
8658 else if (((DECL_P (base0)
8659 && maybe_nonzero_address (base0) > 0
8660 /* Avoid folding references to struct members at offset 0 to
8661 prevent tests like '&ptr->firstmember == 0' from getting
8662 eliminated. When ptr is null, although the -> expression
8663 is strictly speaking invalid, GCC retains it as a matter
8664 of QoI. See PR c/44555. */
8665 && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
8666 || CONSTANT_CLASS_P (base0))
8667 && indirect_base0
8668 /* The caller guarantees that when one of the arguments is
8669 constant (i.e., null in this case) it is second. */
8670 && integer_zerop (arg1))
8672 switch (code)
8674 case EQ_EXPR:
8675 case LE_EXPR:
8676 case LT_EXPR:
8677 return constant_boolean_node (false, type);
8678 case GE_EXPR:
8679 case GT_EXPR:
8680 case NE_EXPR:
8681 return constant_boolean_node (true, type);
8682 default:
8683 gcc_unreachable ();
8688 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8689 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8690 the resulting offset is smaller in absolute value than the
8691 original one and has the same sign. */
8692 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8693 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8694 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8695 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8696 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8697 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8698 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8699 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8701 tree const1 = TREE_OPERAND (arg0, 1);
8702 tree const2 = TREE_OPERAND (arg1, 1);
8703 tree variable1 = TREE_OPERAND (arg0, 0);
8704 tree variable2 = TREE_OPERAND (arg1, 0);
8705 tree cst;
8706 const char * const warnmsg = G_("assuming signed overflow does not "
8707 "occur when combining constants around "
8708 "a comparison");
8710 /* Put the constant on the side where it doesn't overflow and is
8711 of lower absolute value and of same sign than before. */
8712 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8713 ? MINUS_EXPR : PLUS_EXPR,
8714 const2, const1);
8715 if (!TREE_OVERFLOW (cst)
8716 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8717 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8719 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8720 return fold_build2_loc (loc, code, type,
8721 variable1,
8722 fold_build2_loc (loc, TREE_CODE (arg1),
8723 TREE_TYPE (arg1),
8724 variable2, cst));
8727 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8728 ? MINUS_EXPR : PLUS_EXPR,
8729 const1, const2);
8730 if (!TREE_OVERFLOW (cst)
8731 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8732 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8734 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8735 return fold_build2_loc (loc, code, type,
8736 fold_build2_loc (loc, TREE_CODE (arg0),
8737 TREE_TYPE (arg0),
8738 variable1, cst),
8739 variable2);
8743 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8744 if (tem)
8745 return tem;
8747 /* If we are comparing an expression that just has comparisons
8748 of two integer values, arithmetic expressions of those comparisons,
8749 and constants, we can simplify it. There are only three cases
8750 to check: the two values can either be equal, the first can be
8751 greater, or the second can be greater. Fold the expression for
8752 those three values. Since each value must be 0 or 1, we have
8753 eight possibilities, each of which corresponds to the constant 0
8754 or 1 or one of the six possible comparisons.
8756 This handles common cases like (a > b) == 0 but also handles
8757 expressions like ((x > y) - (y > x)) > 0, which supposedly
8758 occur in macroized code. */
8760 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8762 tree cval1 = 0, cval2 = 0;
8763 int save_p = 0;
8765 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8766 /* Don't handle degenerate cases here; they should already
8767 have been handled anyway. */
8768 && cval1 != 0 && cval2 != 0
8769 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8770 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8771 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8772 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8773 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8774 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8775 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8777 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8778 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8780 /* We can't just pass T to eval_subst in case cval1 or cval2
8781 was the same as ARG1. */
8783 tree high_result
8784 = fold_build2_loc (loc, code, type,
8785 eval_subst (loc, arg0, cval1, maxval,
8786 cval2, minval),
8787 arg1);
8788 tree equal_result
8789 = fold_build2_loc (loc, code, type,
8790 eval_subst (loc, arg0, cval1, maxval,
8791 cval2, maxval),
8792 arg1);
8793 tree low_result
8794 = fold_build2_loc (loc, code, type,
8795 eval_subst (loc, arg0, cval1, minval,
8796 cval2, maxval),
8797 arg1);
8799 /* All three of these results should be 0 or 1. Confirm they are.
8800 Then use those values to select the proper code to use. */
8802 if (TREE_CODE (high_result) == INTEGER_CST
8803 && TREE_CODE (equal_result) == INTEGER_CST
8804 && TREE_CODE (low_result) == INTEGER_CST)
8806 /* Make a 3-bit mask with the high-order bit being the
8807 value for `>', the next for '=', and the low for '<'. */
8808 switch ((integer_onep (high_result) * 4)
8809 + (integer_onep (equal_result) * 2)
8810 + integer_onep (low_result))
8812 case 0:
8813 /* Always false. */
8814 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8815 case 1:
8816 code = LT_EXPR;
8817 break;
8818 case 2:
8819 code = EQ_EXPR;
8820 break;
8821 case 3:
8822 code = LE_EXPR;
8823 break;
8824 case 4:
8825 code = GT_EXPR;
8826 break;
8827 case 5:
8828 code = NE_EXPR;
8829 break;
8830 case 6:
8831 code = GE_EXPR;
8832 break;
8833 case 7:
8834 /* Always true. */
8835 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8838 if (save_p)
8840 tem = save_expr (build2 (code, type, cval1, cval2));
8841 protected_set_expr_location (tem, loc);
8842 return tem;
8844 return fold_build2_loc (loc, code, type, cval1, cval2);
8849 return NULL_TREE;
8853 /* Subroutine of fold_binary. Optimize complex multiplications of the
8854 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8855 argument EXPR represents the expression "z" of type TYPE. */
8857 static tree
8858 fold_mult_zconjz (location_t loc, tree type, tree expr)
8860 tree itype = TREE_TYPE (type);
8861 tree rpart, ipart, tem;
8863 if (TREE_CODE (expr) == COMPLEX_EXPR)
8865 rpart = TREE_OPERAND (expr, 0);
8866 ipart = TREE_OPERAND (expr, 1);
8868 else if (TREE_CODE (expr) == COMPLEX_CST)
8870 rpart = TREE_REALPART (expr);
8871 ipart = TREE_IMAGPART (expr);
8873 else
8875 expr = save_expr (expr);
8876 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8877 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8880 rpart = save_expr (rpart);
8881 ipart = save_expr (ipart);
8882 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8883 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8884 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8885 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8886 build_zero_cst (itype));
8890 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8891 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8892 true if successful. */
8894 static bool
8895 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
8897 unsigned int i;
8899 if (TREE_CODE (arg) == VECTOR_CST)
8901 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8902 elts[i] = VECTOR_CST_ELT (arg, i);
8904 else if (TREE_CODE (arg) == CONSTRUCTOR)
8906 constructor_elt *elt;
8908 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8909 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8910 return false;
8911 else
8912 elts[i] = elt->value;
8914 else
8915 return false;
8916 for (; i < nelts; i++)
8917 elts[i]
8918 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8919 return true;
8922 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8923 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8924 NULL_TREE otherwise. */
8926 static tree
8927 fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
8929 unsigned int i;
8930 unsigned HOST_WIDE_INT nelts;
8931 bool need_ctor = false;
8933 if (!sel.length ().is_constant (&nelts))
8934 return NULL_TREE;
8935 gcc_assert (TYPE_VECTOR_SUBPARTS (type) == nelts
8936 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8937 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8938 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8939 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8940 return NULL_TREE;
8942 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
8943 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
8944 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
8945 return NULL_TREE;
8947 tree_vector_builder out_elts (type, nelts, 1);
8948 for (i = 0; i < nelts; i++)
8950 HOST_WIDE_INT index;
8951 if (!sel[i].is_constant (&index))
8952 return NULL_TREE;
8953 if (!CONSTANT_CLASS_P (in_elts[index]))
8954 need_ctor = true;
8955 out_elts.quick_push (unshare_expr (in_elts[index]));
8958 if (need_ctor)
8960 vec<constructor_elt, va_gc> *v;
8961 vec_alloc (v, nelts);
8962 for (i = 0; i < nelts; i++)
8963 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
8964 return build_constructor (type, v);
8966 else
8967 return out_elts.build ();
8970 /* Try to fold a pointer difference of type TYPE two address expressions of
8971 array references AREF0 and AREF1 using location LOC. Return a
8972 simplified expression for the difference or NULL_TREE. */
8974 static tree
8975 fold_addr_of_array_ref_difference (location_t loc, tree type,
8976 tree aref0, tree aref1,
8977 bool use_pointer_diff)
8979 tree base0 = TREE_OPERAND (aref0, 0);
8980 tree base1 = TREE_OPERAND (aref1, 0);
8981 tree base_offset = build_int_cst (type, 0);
8983 /* If the bases are array references as well, recurse. If the bases
8984 are pointer indirections compute the difference of the pointers.
8985 If the bases are equal, we are set. */
8986 if ((TREE_CODE (base0) == ARRAY_REF
8987 && TREE_CODE (base1) == ARRAY_REF
8988 && (base_offset
8989 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
8990 use_pointer_diff)))
8991 || (INDIRECT_REF_P (base0)
8992 && INDIRECT_REF_P (base1)
8993 && (base_offset
8994 = use_pointer_diff
8995 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
8996 TREE_OPERAND (base0, 0),
8997 TREE_OPERAND (base1, 0))
8998 : fold_binary_loc (loc, MINUS_EXPR, type,
8999 fold_convert (type,
9000 TREE_OPERAND (base0, 0)),
9001 fold_convert (type,
9002 TREE_OPERAND (base1, 0)))))
9003 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
9005 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9006 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9007 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9008 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
9009 return fold_build2_loc (loc, PLUS_EXPR, type,
9010 base_offset,
9011 fold_build2_loc (loc, MULT_EXPR, type,
9012 diff, esz));
9014 return NULL_TREE;
9017 /* If the real or vector real constant CST of type TYPE has an exact
9018 inverse, return it, else return NULL. */
9020 tree
9021 exact_inverse (tree type, tree cst)
9023 REAL_VALUE_TYPE r;
9024 tree unit_type;
9025 machine_mode mode;
9027 switch (TREE_CODE (cst))
9029 case REAL_CST:
9030 r = TREE_REAL_CST (cst);
9032 if (exact_real_inverse (TYPE_MODE (type), &r))
9033 return build_real (type, r);
9035 return NULL_TREE;
9037 case VECTOR_CST:
9039 unit_type = TREE_TYPE (type);
9040 mode = TYPE_MODE (unit_type);
9042 tree_vector_builder elts;
9043 if (!elts.new_unary_operation (type, cst, false))
9044 return NULL_TREE;
9045 unsigned int count = elts.encoded_nelts ();
9046 for (unsigned int i = 0; i < count; ++i)
9048 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9049 if (!exact_real_inverse (mode, &r))
9050 return NULL_TREE;
9051 elts.quick_push (build_real (unit_type, r));
9054 return elts.build ();
9057 default:
9058 return NULL_TREE;
9062 /* Mask out the tz least significant bits of X of type TYPE where
9063 tz is the number of trailing zeroes in Y. */
9064 static wide_int
9065 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9067 int tz = wi::ctz (y);
9068 if (tz > 0)
9069 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9070 return x;
9073 /* Return true when T is an address and is known to be nonzero.
9074 For floating point we further ensure that T is not denormal.
9075 Similar logic is present in nonzero_address in rtlanal.h.
9077 If the return value is based on the assumption that signed overflow
9078 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9079 change *STRICT_OVERFLOW_P. */
9081 static bool
9082 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9084 tree type = TREE_TYPE (t);
9085 enum tree_code code;
9087 /* Doing something useful for floating point would need more work. */
9088 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9089 return false;
9091 code = TREE_CODE (t);
9092 switch (TREE_CODE_CLASS (code))
9094 case tcc_unary:
9095 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9096 strict_overflow_p);
9097 case tcc_binary:
9098 case tcc_comparison:
9099 return tree_binary_nonzero_warnv_p (code, type,
9100 TREE_OPERAND (t, 0),
9101 TREE_OPERAND (t, 1),
9102 strict_overflow_p);
9103 case tcc_constant:
9104 case tcc_declaration:
9105 case tcc_reference:
9106 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9108 default:
9109 break;
9112 switch (code)
9114 case TRUTH_NOT_EXPR:
9115 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9116 strict_overflow_p);
9118 case TRUTH_AND_EXPR:
9119 case TRUTH_OR_EXPR:
9120 case TRUTH_XOR_EXPR:
9121 return tree_binary_nonzero_warnv_p (code, type,
9122 TREE_OPERAND (t, 0),
9123 TREE_OPERAND (t, 1),
9124 strict_overflow_p);
9126 case COND_EXPR:
9127 case CONSTRUCTOR:
9128 case OBJ_TYPE_REF:
9129 case ASSERT_EXPR:
9130 case ADDR_EXPR:
9131 case WITH_SIZE_EXPR:
9132 case SSA_NAME:
9133 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9135 case COMPOUND_EXPR:
9136 case MODIFY_EXPR:
9137 case BIND_EXPR:
9138 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9139 strict_overflow_p);
9141 case SAVE_EXPR:
9142 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9143 strict_overflow_p);
9145 case CALL_EXPR:
9147 tree fndecl = get_callee_fndecl (t);
9148 if (!fndecl) return false;
9149 if (flag_delete_null_pointer_checks && !flag_check_new
9150 && DECL_IS_OPERATOR_NEW (fndecl)
9151 && !TREE_NOTHROW (fndecl))
9152 return true;
9153 if (flag_delete_null_pointer_checks
9154 && lookup_attribute ("returns_nonnull",
9155 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9156 return true;
9157 return alloca_call_p (t);
9160 default:
9161 break;
9163 return false;
9166 /* Return true when T is an address and is known to be nonzero.
9167 Handle warnings about undefined signed overflow. */
9169 bool
9170 tree_expr_nonzero_p (tree t)
9172 bool ret, strict_overflow_p;
9174 strict_overflow_p = false;
9175 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9176 if (strict_overflow_p)
9177 fold_overflow_warning (("assuming signed overflow does not occur when "
9178 "determining that expression is always "
9179 "non-zero"),
9180 WARN_STRICT_OVERFLOW_MISC);
9181 return ret;
9184 /* Return true if T is known not to be equal to an integer W. */
9186 bool
9187 expr_not_equal_to (tree t, const wide_int &w)
9189 wide_int min, max, nz;
9190 value_range_type rtype;
9191 switch (TREE_CODE (t))
9193 case INTEGER_CST:
9194 return wi::to_wide (t) != w;
9196 case SSA_NAME:
9197 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9198 return false;
9199 rtype = get_range_info (t, &min, &max);
9200 if (rtype == VR_RANGE)
9202 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9203 return true;
9204 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9205 return true;
9207 else if (rtype == VR_ANTI_RANGE
9208 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9209 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9210 return true;
9211 /* If T has some known zero bits and W has any of those bits set,
9212 then T is known not to be equal to W. */
9213 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9214 TYPE_PRECISION (TREE_TYPE (t))), 0))
9215 return true;
9216 return false;
9218 default:
9219 return false;
9223 /* Fold a binary expression of code CODE and type TYPE with operands
9224 OP0 and OP1. LOC is the location of the resulting expression.
9225 Return the folded expression if folding is successful. Otherwise,
9226 return NULL_TREE. */
9228 tree
9229 fold_binary_loc (location_t loc, enum tree_code code, tree type,
9230 tree op0, tree op1)
9232 enum tree_code_class kind = TREE_CODE_CLASS (code);
9233 tree arg0, arg1, tem;
9234 tree t1 = NULL_TREE;
9235 bool strict_overflow_p;
9236 unsigned int prec;
9238 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9239 && TREE_CODE_LENGTH (code) == 2
9240 && op0 != NULL_TREE
9241 && op1 != NULL_TREE);
9243 arg0 = op0;
9244 arg1 = op1;
9246 /* Strip any conversions that don't change the mode. This is
9247 safe for every expression, except for a comparison expression
9248 because its signedness is derived from its operands. So, in
9249 the latter case, only strip conversions that don't change the
9250 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9251 preserved.
9253 Note that this is done as an internal manipulation within the
9254 constant folder, in order to find the simplest representation
9255 of the arguments so that their form can be studied. In any
9256 cases, the appropriate type conversions should be put back in
9257 the tree that will get out of the constant folder. */
9259 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9261 STRIP_SIGN_NOPS (arg0);
9262 STRIP_SIGN_NOPS (arg1);
9264 else
9266 STRIP_NOPS (arg0);
9267 STRIP_NOPS (arg1);
9270 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9271 constant but we can't do arithmetic on them. */
9272 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9274 tem = const_binop (code, type, arg0, arg1);
9275 if (tem != NULL_TREE)
9277 if (TREE_TYPE (tem) != type)
9278 tem = fold_convert_loc (loc, type, tem);
9279 return tem;
9283 /* If this is a commutative operation, and ARG0 is a constant, move it
9284 to ARG1 to reduce the number of tests below. */
9285 if (commutative_tree_code (code)
9286 && tree_swap_operands_p (arg0, arg1))
9287 return fold_build2_loc (loc, code, type, op1, op0);
9289 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9290 to ARG1 to reduce the number of tests below. */
9291 if (kind == tcc_comparison
9292 && tree_swap_operands_p (arg0, arg1))
9293 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9295 tem = generic_simplify (loc, code, type, op0, op1);
9296 if (tem)
9297 return tem;
9299 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9301 First check for cases where an arithmetic operation is applied to a
9302 compound, conditional, or comparison operation. Push the arithmetic
9303 operation inside the compound or conditional to see if any folding
9304 can then be done. Convert comparison to conditional for this purpose.
9305 The also optimizes non-constant cases that used to be done in
9306 expand_expr.
9308 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9309 one of the operands is a comparison and the other is a comparison, a
9310 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9311 code below would make the expression more complex. Change it to a
9312 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9313 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9315 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9316 || code == EQ_EXPR || code == NE_EXPR)
9317 && TREE_CODE (type) != VECTOR_TYPE
9318 && ((truth_value_p (TREE_CODE (arg0))
9319 && (truth_value_p (TREE_CODE (arg1))
9320 || (TREE_CODE (arg1) == BIT_AND_EXPR
9321 && integer_onep (TREE_OPERAND (arg1, 1)))))
9322 || (truth_value_p (TREE_CODE (arg1))
9323 && (truth_value_p (TREE_CODE (arg0))
9324 || (TREE_CODE (arg0) == BIT_AND_EXPR
9325 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9327 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9328 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9329 : TRUTH_XOR_EXPR,
9330 boolean_type_node,
9331 fold_convert_loc (loc, boolean_type_node, arg0),
9332 fold_convert_loc (loc, boolean_type_node, arg1));
9334 if (code == EQ_EXPR)
9335 tem = invert_truthvalue_loc (loc, tem);
9337 return fold_convert_loc (loc, type, tem);
9340 if (TREE_CODE_CLASS (code) == tcc_binary
9341 || TREE_CODE_CLASS (code) == tcc_comparison)
9343 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9345 tem = fold_build2_loc (loc, code, type,
9346 fold_convert_loc (loc, TREE_TYPE (op0),
9347 TREE_OPERAND (arg0, 1)), op1);
9348 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9349 tem);
9351 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9353 tem = fold_build2_loc (loc, code, type, op0,
9354 fold_convert_loc (loc, TREE_TYPE (op1),
9355 TREE_OPERAND (arg1, 1)));
9356 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9357 tem);
9360 if (TREE_CODE (arg0) == COND_EXPR
9361 || TREE_CODE (arg0) == VEC_COND_EXPR
9362 || COMPARISON_CLASS_P (arg0))
9364 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9365 arg0, arg1,
9366 /*cond_first_p=*/1);
9367 if (tem != NULL_TREE)
9368 return tem;
9371 if (TREE_CODE (arg1) == COND_EXPR
9372 || TREE_CODE (arg1) == VEC_COND_EXPR
9373 || COMPARISON_CLASS_P (arg1))
9375 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9376 arg1, arg0,
9377 /*cond_first_p=*/0);
9378 if (tem != NULL_TREE)
9379 return tem;
9383 switch (code)
9385 case MEM_REF:
9386 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9387 if (TREE_CODE (arg0) == ADDR_EXPR
9388 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9390 tree iref = TREE_OPERAND (arg0, 0);
9391 return fold_build2 (MEM_REF, type,
9392 TREE_OPERAND (iref, 0),
9393 int_const_binop (PLUS_EXPR, arg1,
9394 TREE_OPERAND (iref, 1)));
9397 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9398 if (TREE_CODE (arg0) == ADDR_EXPR
9399 && handled_component_p (TREE_OPERAND (arg0, 0)))
9401 tree base;
9402 poly_int64 coffset;
9403 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9404 &coffset);
9405 if (!base)
9406 return NULL_TREE;
9407 return fold_build2 (MEM_REF, type,
9408 build_fold_addr_expr (base),
9409 int_const_binop (PLUS_EXPR, arg1,
9410 size_int (coffset)));
9413 return NULL_TREE;
9415 case POINTER_PLUS_EXPR:
9416 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9417 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9418 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9419 return fold_convert_loc (loc, type,
9420 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9421 fold_convert_loc (loc, sizetype,
9422 arg1),
9423 fold_convert_loc (loc, sizetype,
9424 arg0)));
9426 return NULL_TREE;
9428 case PLUS_EXPR:
9429 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9431 /* X + (X / CST) * -CST is X % CST. */
9432 if (TREE_CODE (arg1) == MULT_EXPR
9433 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9434 && operand_equal_p (arg0,
9435 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9437 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9438 tree cst1 = TREE_OPERAND (arg1, 1);
9439 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9440 cst1, cst0);
9441 if (sum && integer_zerop (sum))
9442 return fold_convert_loc (loc, type,
9443 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9444 TREE_TYPE (arg0), arg0,
9445 cst0));
9449 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9450 one. Make sure the type is not saturating and has the signedness of
9451 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9452 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9453 if ((TREE_CODE (arg0) == MULT_EXPR
9454 || TREE_CODE (arg1) == MULT_EXPR)
9455 && !TYPE_SATURATING (type)
9456 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9457 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9458 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9460 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9461 if (tem)
9462 return tem;
9465 if (! FLOAT_TYPE_P (type))
9467 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9468 (plus (plus (mult) (mult)) (foo)) so that we can
9469 take advantage of the factoring cases below. */
9470 if (ANY_INTEGRAL_TYPE_P (type)
9471 && TYPE_OVERFLOW_WRAPS (type)
9472 && (((TREE_CODE (arg0) == PLUS_EXPR
9473 || TREE_CODE (arg0) == MINUS_EXPR)
9474 && TREE_CODE (arg1) == MULT_EXPR)
9475 || ((TREE_CODE (arg1) == PLUS_EXPR
9476 || TREE_CODE (arg1) == MINUS_EXPR)
9477 && TREE_CODE (arg0) == MULT_EXPR)))
9479 tree parg0, parg1, parg, marg;
9480 enum tree_code pcode;
9482 if (TREE_CODE (arg1) == MULT_EXPR)
9483 parg = arg0, marg = arg1;
9484 else
9485 parg = arg1, marg = arg0;
9486 pcode = TREE_CODE (parg);
9487 parg0 = TREE_OPERAND (parg, 0);
9488 parg1 = TREE_OPERAND (parg, 1);
9489 STRIP_NOPS (parg0);
9490 STRIP_NOPS (parg1);
9492 if (TREE_CODE (parg0) == MULT_EXPR
9493 && TREE_CODE (parg1) != MULT_EXPR)
9494 return fold_build2_loc (loc, pcode, type,
9495 fold_build2_loc (loc, PLUS_EXPR, type,
9496 fold_convert_loc (loc, type,
9497 parg0),
9498 fold_convert_loc (loc, type,
9499 marg)),
9500 fold_convert_loc (loc, type, parg1));
9501 if (TREE_CODE (parg0) != MULT_EXPR
9502 && TREE_CODE (parg1) == MULT_EXPR)
9503 return
9504 fold_build2_loc (loc, PLUS_EXPR, type,
9505 fold_convert_loc (loc, type, parg0),
9506 fold_build2_loc (loc, pcode, type,
9507 fold_convert_loc (loc, type, marg),
9508 fold_convert_loc (loc, type,
9509 parg1)));
9512 else
9514 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9515 to __complex__ ( x, y ). This is not the same for SNaNs or
9516 if signed zeros are involved. */
9517 if (!HONOR_SNANS (element_mode (arg0))
9518 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9519 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9521 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9522 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9523 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9524 bool arg0rz = false, arg0iz = false;
9525 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9526 || (arg0i && (arg0iz = real_zerop (arg0i))))
9528 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9529 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9530 if (arg0rz && arg1i && real_zerop (arg1i))
9532 tree rp = arg1r ? arg1r
9533 : build1 (REALPART_EXPR, rtype, arg1);
9534 tree ip = arg0i ? arg0i
9535 : build1 (IMAGPART_EXPR, rtype, arg0);
9536 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9538 else if (arg0iz && arg1r && real_zerop (arg1r))
9540 tree rp = arg0r ? arg0r
9541 : build1 (REALPART_EXPR, rtype, arg0);
9542 tree ip = arg1i ? arg1i
9543 : build1 (IMAGPART_EXPR, rtype, arg1);
9544 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9549 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9550 We associate floats only if the user has specified
9551 -fassociative-math. */
9552 if (flag_associative_math
9553 && TREE_CODE (arg1) == PLUS_EXPR
9554 && TREE_CODE (arg0) != MULT_EXPR)
9556 tree tree10 = TREE_OPERAND (arg1, 0);
9557 tree tree11 = TREE_OPERAND (arg1, 1);
9558 if (TREE_CODE (tree11) == MULT_EXPR
9559 && TREE_CODE (tree10) == MULT_EXPR)
9561 tree tree0;
9562 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9563 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9566 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9567 We associate floats only if the user has specified
9568 -fassociative-math. */
9569 if (flag_associative_math
9570 && TREE_CODE (arg0) == PLUS_EXPR
9571 && TREE_CODE (arg1) != MULT_EXPR)
9573 tree tree00 = TREE_OPERAND (arg0, 0);
9574 tree tree01 = TREE_OPERAND (arg0, 1);
9575 if (TREE_CODE (tree01) == MULT_EXPR
9576 && TREE_CODE (tree00) == MULT_EXPR)
9578 tree tree0;
9579 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9580 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9585 bit_rotate:
9586 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9587 is a rotate of A by C1 bits. */
9588 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9589 is a rotate of A by B bits.
9590 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
9591 though in this case CODE must be | and not + or ^, otherwise
9592 it doesn't return A when B is 0. */
9594 enum tree_code code0, code1;
9595 tree rtype;
9596 code0 = TREE_CODE (arg0);
9597 code1 = TREE_CODE (arg1);
9598 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9599 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9600 && operand_equal_p (TREE_OPERAND (arg0, 0),
9601 TREE_OPERAND (arg1, 0), 0)
9602 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9603 TYPE_UNSIGNED (rtype))
9604 /* Only create rotates in complete modes. Other cases are not
9605 expanded properly. */
9606 && (element_precision (rtype)
9607 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9609 tree tree01, tree11;
9610 tree orig_tree01, orig_tree11;
9611 enum tree_code code01, code11;
9613 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
9614 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
9615 STRIP_NOPS (tree01);
9616 STRIP_NOPS (tree11);
9617 code01 = TREE_CODE (tree01);
9618 code11 = TREE_CODE (tree11);
9619 if (code11 != MINUS_EXPR
9620 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
9622 std::swap (code0, code1);
9623 std::swap (code01, code11);
9624 std::swap (tree01, tree11);
9625 std::swap (orig_tree01, orig_tree11);
9627 if (code01 == INTEGER_CST
9628 && code11 == INTEGER_CST
9629 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9630 == element_precision (rtype)))
9632 tem = build2_loc (loc, LROTATE_EXPR,
9633 rtype, TREE_OPERAND (arg0, 0),
9634 code0 == LSHIFT_EXPR
9635 ? orig_tree01 : orig_tree11);
9636 return fold_convert_loc (loc, type, tem);
9638 else if (code11 == MINUS_EXPR)
9640 tree tree110, tree111;
9641 tree110 = TREE_OPERAND (tree11, 0);
9642 tree111 = TREE_OPERAND (tree11, 1);
9643 STRIP_NOPS (tree110);
9644 STRIP_NOPS (tree111);
9645 if (TREE_CODE (tree110) == INTEGER_CST
9646 && compare_tree_int (tree110,
9647 element_precision (rtype)) == 0
9648 && operand_equal_p (tree01, tree111, 0))
9650 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9651 ? LROTATE_EXPR : RROTATE_EXPR),
9652 rtype, TREE_OPERAND (arg0, 0),
9653 orig_tree01);
9654 return fold_convert_loc (loc, type, tem);
9657 else if (code == BIT_IOR_EXPR
9658 && code11 == BIT_AND_EXPR
9659 && pow2p_hwi (element_precision (rtype)))
9661 tree tree110, tree111;
9662 tree110 = TREE_OPERAND (tree11, 0);
9663 tree111 = TREE_OPERAND (tree11, 1);
9664 STRIP_NOPS (tree110);
9665 STRIP_NOPS (tree111);
9666 if (TREE_CODE (tree110) == NEGATE_EXPR
9667 && TREE_CODE (tree111) == INTEGER_CST
9668 && compare_tree_int (tree111,
9669 element_precision (rtype) - 1) == 0
9670 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
9672 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9673 ? LROTATE_EXPR : RROTATE_EXPR),
9674 rtype, TREE_OPERAND (arg0, 0),
9675 orig_tree01);
9676 return fold_convert_loc (loc, type, tem);
9682 associate:
9683 /* In most languages, can't associate operations on floats through
9684 parentheses. Rather than remember where the parentheses were, we
9685 don't associate floats at all, unless the user has specified
9686 -fassociative-math.
9687 And, we need to make sure type is not saturating. */
9689 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9690 && !TYPE_SATURATING (type))
9692 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9693 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9694 tree atype = type;
9695 bool ok = true;
9697 /* Split both trees into variables, constants, and literals. Then
9698 associate each group together, the constants with literals,
9699 then the result with variables. This increases the chances of
9700 literals being recombined later and of generating relocatable
9701 expressions for the sum of a constant and literal. */
9702 var0 = split_tree (arg0, type, code,
9703 &minus_var0, &con0, &minus_con0,
9704 &lit0, &minus_lit0, 0);
9705 var1 = split_tree (arg1, type, code,
9706 &minus_var1, &con1, &minus_con1,
9707 &lit1, &minus_lit1, code == MINUS_EXPR);
9709 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9710 if (code == MINUS_EXPR)
9711 code = PLUS_EXPR;
9713 /* With undefined overflow prefer doing association in a type
9714 which wraps on overflow, if that is one of the operand types. */
9715 if (POINTER_TYPE_P (type)
9716 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9718 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9719 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9720 atype = TREE_TYPE (arg0);
9721 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9722 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9723 atype = TREE_TYPE (arg1);
9724 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9727 /* With undefined overflow we can only associate constants with one
9728 variable, and constants whose association doesn't overflow. */
9729 if (POINTER_TYPE_P (atype)
9730 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9732 if ((var0 && var1) || (minus_var0 && minus_var1))
9734 /* ??? If split_tree would handle NEGATE_EXPR we could
9735 simply reject these cases and the allowed cases would
9736 be the var0/minus_var1 ones. */
9737 tree tmp0 = var0 ? var0 : minus_var0;
9738 tree tmp1 = var1 ? var1 : minus_var1;
9739 bool one_neg = false;
9741 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9743 tmp0 = TREE_OPERAND (tmp0, 0);
9744 one_neg = !one_neg;
9746 if (CONVERT_EXPR_P (tmp0)
9747 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9748 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9749 <= TYPE_PRECISION (atype)))
9750 tmp0 = TREE_OPERAND (tmp0, 0);
9751 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9753 tmp1 = TREE_OPERAND (tmp1, 0);
9754 one_neg = !one_neg;
9756 if (CONVERT_EXPR_P (tmp1)
9757 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9758 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9759 <= TYPE_PRECISION (atype)))
9760 tmp1 = TREE_OPERAND (tmp1, 0);
9761 /* The only case we can still associate with two variables
9762 is if they cancel out. */
9763 if (!one_neg
9764 || !operand_equal_p (tmp0, tmp1, 0))
9765 ok = false;
9767 else if ((var0 && minus_var1
9768 && ! operand_equal_p (var0, minus_var1, 0))
9769 || (minus_var0 && var1
9770 && ! operand_equal_p (minus_var0, var1, 0)))
9771 ok = false;
9774 /* Only do something if we found more than two objects. Otherwise,
9775 nothing has changed and we risk infinite recursion. */
9776 if (ok
9777 && ((var0 != 0) + (var1 != 0)
9778 + (minus_var0 != 0) + (minus_var1 != 0)
9779 + (con0 != 0) + (con1 != 0)
9780 + (minus_con0 != 0) + (minus_con1 != 0)
9781 + (lit0 != 0) + (lit1 != 0)
9782 + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
9784 var0 = associate_trees (loc, var0, var1, code, atype);
9785 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9786 code, atype);
9787 con0 = associate_trees (loc, con0, con1, code, atype);
9788 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9789 code, atype);
9790 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9791 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9792 code, atype);
9794 if (minus_var0 && var0)
9796 var0 = associate_trees (loc, var0, minus_var0,
9797 MINUS_EXPR, atype);
9798 minus_var0 = 0;
9800 if (minus_con0 && con0)
9802 con0 = associate_trees (loc, con0, minus_con0,
9803 MINUS_EXPR, atype);
9804 minus_con0 = 0;
9807 /* Preserve the MINUS_EXPR if the negative part of the literal is
9808 greater than the positive part. Otherwise, the multiplicative
9809 folding code (i.e extract_muldiv) may be fooled in case
9810 unsigned constants are subtracted, like in the following
9811 example: ((X*2 + 4) - 8U)/2. */
9812 if (minus_lit0 && lit0)
9814 if (TREE_CODE (lit0) == INTEGER_CST
9815 && TREE_CODE (minus_lit0) == INTEGER_CST
9816 && tree_int_cst_lt (lit0, minus_lit0)
9817 /* But avoid ending up with only negated parts. */
9818 && (var0 || con0))
9820 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9821 MINUS_EXPR, atype);
9822 lit0 = 0;
9824 else
9826 lit0 = associate_trees (loc, lit0, minus_lit0,
9827 MINUS_EXPR, atype);
9828 minus_lit0 = 0;
9832 /* Don't introduce overflows through reassociation. */
9833 if ((lit0 && TREE_OVERFLOW_P (lit0))
9834 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9835 return NULL_TREE;
9837 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9838 con0 = associate_trees (loc, con0, lit0, code, atype);
9839 lit0 = 0;
9840 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9841 code, atype);
9842 minus_lit0 = 0;
9844 /* Eliminate minus_con0. */
9845 if (minus_con0)
9847 if (con0)
9848 con0 = associate_trees (loc, con0, minus_con0,
9849 MINUS_EXPR, atype);
9850 else if (var0)
9851 var0 = associate_trees (loc, var0, minus_con0,
9852 MINUS_EXPR, atype);
9853 else
9854 gcc_unreachable ();
9855 minus_con0 = 0;
9858 /* Eliminate minus_var0. */
9859 if (minus_var0)
9861 if (con0)
9862 con0 = associate_trees (loc, con0, minus_var0,
9863 MINUS_EXPR, atype);
9864 else
9865 gcc_unreachable ();
9866 minus_var0 = 0;
9869 return
9870 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9871 code, atype));
9875 return NULL_TREE;
9877 case POINTER_DIFF_EXPR:
9878 case MINUS_EXPR:
9879 /* Fold &a[i] - &a[j] to i-j. */
9880 if (TREE_CODE (arg0) == ADDR_EXPR
9881 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9882 && TREE_CODE (arg1) == ADDR_EXPR
9883 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9885 tree tem = fold_addr_of_array_ref_difference (loc, type,
9886 TREE_OPERAND (arg0, 0),
9887 TREE_OPERAND (arg1, 0),
9888 code
9889 == POINTER_DIFF_EXPR);
9890 if (tem)
9891 return tem;
9894 /* Further transformations are not for pointers. */
9895 if (code == POINTER_DIFF_EXPR)
9896 return NULL_TREE;
9898 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9899 if (TREE_CODE (arg0) == NEGATE_EXPR
9900 && negate_expr_p (op1)
9901 /* If arg0 is e.g. unsigned int and type is int, then this could
9902 introduce UB, because if A is INT_MIN at runtime, the original
9903 expression can be well defined while the latter is not.
9904 See PR83269. */
9905 && !(ANY_INTEGRAL_TYPE_P (type)
9906 && TYPE_OVERFLOW_UNDEFINED (type)
9907 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9908 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
9909 return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
9910 fold_convert_loc (loc, type,
9911 TREE_OPERAND (arg0, 0)));
9913 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9914 __complex__ ( x, -y ). This is not the same for SNaNs or if
9915 signed zeros are involved. */
9916 if (!HONOR_SNANS (element_mode (arg0))
9917 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9918 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9920 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9921 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9922 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9923 bool arg0rz = false, arg0iz = false;
9924 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9925 || (arg0i && (arg0iz = real_zerop (arg0i))))
9927 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9928 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9929 if (arg0rz && arg1i && real_zerop (arg1i))
9931 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9932 arg1r ? arg1r
9933 : build1 (REALPART_EXPR, rtype, arg1));
9934 tree ip = arg0i ? arg0i
9935 : build1 (IMAGPART_EXPR, rtype, arg0);
9936 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9938 else if (arg0iz && arg1r && real_zerop (arg1r))
9940 tree rp = arg0r ? arg0r
9941 : build1 (REALPART_EXPR, rtype, arg0);
9942 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9943 arg1i ? arg1i
9944 : build1 (IMAGPART_EXPR, rtype, arg1));
9945 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9950 /* A - B -> A + (-B) if B is easily negatable. */
9951 if (negate_expr_p (op1)
9952 && ! TYPE_OVERFLOW_SANITIZED (type)
9953 && ((FLOAT_TYPE_P (type)
9954 /* Avoid this transformation if B is a positive REAL_CST. */
9955 && (TREE_CODE (op1) != REAL_CST
9956 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9957 || INTEGRAL_TYPE_P (type)))
9958 return fold_build2_loc (loc, PLUS_EXPR, type,
9959 fold_convert_loc (loc, type, arg0),
9960 negate_expr (op1));
9962 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9963 one. Make sure the type is not saturating and has the signedness of
9964 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9965 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9966 if ((TREE_CODE (arg0) == MULT_EXPR
9967 || TREE_CODE (arg1) == MULT_EXPR)
9968 && !TYPE_SATURATING (type)
9969 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9970 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9971 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9973 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9974 if (tem)
9975 return tem;
9978 goto associate;
9980 case MULT_EXPR:
9981 if (! FLOAT_TYPE_P (type))
9983 /* Transform x * -C into -x * C if x is easily negatable. */
9984 if (TREE_CODE (op1) == INTEGER_CST
9985 && tree_int_cst_sgn (op1) == -1
9986 && negate_expr_p (op0)
9987 && negate_expr_p (op1)
9988 && (tem = negate_expr (op1)) != op1
9989 && ! TREE_OVERFLOW (tem))
9990 return fold_build2_loc (loc, MULT_EXPR, type,
9991 fold_convert_loc (loc, type,
9992 negate_expr (op0)), tem);
9994 strict_overflow_p = false;
9995 if (TREE_CODE (arg1) == INTEGER_CST
9996 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9997 &strict_overflow_p)) != 0)
9999 if (strict_overflow_p)
10000 fold_overflow_warning (("assuming signed overflow does not "
10001 "occur when simplifying "
10002 "multiplication"),
10003 WARN_STRICT_OVERFLOW_MISC);
10004 return fold_convert_loc (loc, type, tem);
10007 /* Optimize z * conj(z) for integer complex numbers. */
10008 if (TREE_CODE (arg0) == CONJ_EXPR
10009 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10010 return fold_mult_zconjz (loc, type, arg1);
10011 if (TREE_CODE (arg1) == CONJ_EXPR
10012 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10013 return fold_mult_zconjz (loc, type, arg0);
10015 else
10017 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10018 This is not the same for NaNs or if signed zeros are
10019 involved. */
10020 if (!HONOR_NANS (arg0)
10021 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10022 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10023 && TREE_CODE (arg1) == COMPLEX_CST
10024 && real_zerop (TREE_REALPART (arg1)))
10026 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10027 if (real_onep (TREE_IMAGPART (arg1)))
10028 return
10029 fold_build2_loc (loc, COMPLEX_EXPR, type,
10030 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10031 rtype, arg0)),
10032 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10033 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10034 return
10035 fold_build2_loc (loc, COMPLEX_EXPR, type,
10036 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10037 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10038 rtype, arg0)));
10041 /* Optimize z * conj(z) for floating point complex numbers.
10042 Guarded by flag_unsafe_math_optimizations as non-finite
10043 imaginary components don't produce scalar results. */
10044 if (flag_unsafe_math_optimizations
10045 && TREE_CODE (arg0) == CONJ_EXPR
10046 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10047 return fold_mult_zconjz (loc, type, arg1);
10048 if (flag_unsafe_math_optimizations
10049 && TREE_CODE (arg1) == CONJ_EXPR
10050 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10051 return fold_mult_zconjz (loc, type, arg0);
10053 goto associate;
10055 case BIT_IOR_EXPR:
10056 /* Canonicalize (X & C1) | C2. */
10057 if (TREE_CODE (arg0) == BIT_AND_EXPR
10058 && TREE_CODE (arg1) == INTEGER_CST
10059 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10061 int width = TYPE_PRECISION (type), w;
10062 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
10063 wide_int c2 = wi::to_wide (arg1);
10065 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10066 if ((c1 & c2) == c1)
10067 return omit_one_operand_loc (loc, type, arg1,
10068 TREE_OPERAND (arg0, 0));
10070 wide_int msk = wi::mask (width, false,
10071 TYPE_PRECISION (TREE_TYPE (arg1)));
10073 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10074 if (wi::bit_and_not (msk, c1 | c2) == 0)
10076 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10077 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10080 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10081 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10082 mode which allows further optimizations. */
10083 c1 &= msk;
10084 c2 &= msk;
10085 wide_int c3 = wi::bit_and_not (c1, c2);
10086 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10088 wide_int mask = wi::mask (w, false,
10089 TYPE_PRECISION (type));
10090 if (((c1 | c2) & mask) == mask
10091 && wi::bit_and_not (c1, mask) == 0)
10093 c3 = mask;
10094 break;
10098 if (c3 != c1)
10100 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10101 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
10102 wide_int_to_tree (type, c3));
10103 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10107 /* See if this can be simplified into a rotate first. If that
10108 is unsuccessful continue in the association code. */
10109 goto bit_rotate;
10111 case BIT_XOR_EXPR:
10112 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10113 if (TREE_CODE (arg0) == BIT_AND_EXPR
10114 && INTEGRAL_TYPE_P (type)
10115 && integer_onep (TREE_OPERAND (arg0, 1))
10116 && integer_onep (arg1))
10117 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10118 build_zero_cst (TREE_TYPE (arg0)));
10120 /* See if this can be simplified into a rotate first. If that
10121 is unsuccessful continue in the association code. */
10122 goto bit_rotate;
10124 case BIT_AND_EXPR:
10125 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10126 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10127 && INTEGRAL_TYPE_P (type)
10128 && integer_onep (TREE_OPERAND (arg0, 1))
10129 && integer_onep (arg1))
10131 tree tem2;
10132 tem = TREE_OPERAND (arg0, 0);
10133 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10134 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10135 tem, tem2);
10136 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10137 build_zero_cst (TREE_TYPE (tem)));
10139 /* Fold ~X & 1 as (X & 1) == 0. */
10140 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10141 && INTEGRAL_TYPE_P (type)
10142 && integer_onep (arg1))
10144 tree tem2;
10145 tem = TREE_OPERAND (arg0, 0);
10146 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10147 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10148 tem, tem2);
10149 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10150 build_zero_cst (TREE_TYPE (tem)));
10152 /* Fold !X & 1 as X == 0. */
10153 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10154 && integer_onep (arg1))
10156 tem = TREE_OPERAND (arg0, 0);
10157 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10158 build_zero_cst (TREE_TYPE (tem)));
10161 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10162 multiple of 1 << CST. */
10163 if (TREE_CODE (arg1) == INTEGER_CST)
10165 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10166 wide_int ncst1 = -cst1;
10167 if ((cst1 & ncst1) == ncst1
10168 && multiple_of_p (type, arg0,
10169 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10170 return fold_convert_loc (loc, type, arg0);
10173 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10174 bits from CST2. */
10175 if (TREE_CODE (arg1) == INTEGER_CST
10176 && TREE_CODE (arg0) == MULT_EXPR
10177 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10179 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
10180 wide_int masked
10181 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
10183 if (masked == 0)
10184 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10185 arg0, arg1);
10186 else if (masked != warg1)
10188 /* Avoid the transform if arg1 is a mask of some
10189 mode which allows further optimizations. */
10190 int pop = wi::popcount (warg1);
10191 if (!(pop >= BITS_PER_UNIT
10192 && pow2p_hwi (pop)
10193 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10194 return fold_build2_loc (loc, code, type, op0,
10195 wide_int_to_tree (type, masked));
10199 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10200 ((A & N) + B) & M -> (A + B) & M
10201 Similarly if (N & M) == 0,
10202 ((A | N) + B) & M -> (A + B) & M
10203 and for - instead of + (or unary - instead of +)
10204 and/or ^ instead of |.
10205 If B is constant and (B & M) == 0, fold into A & M. */
10206 if (TREE_CODE (arg1) == INTEGER_CST)
10208 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10209 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10210 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10211 && (TREE_CODE (arg0) == PLUS_EXPR
10212 || TREE_CODE (arg0) == MINUS_EXPR
10213 || TREE_CODE (arg0) == NEGATE_EXPR)
10214 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10215 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10217 tree pmop[2];
10218 int which = 0;
10219 wide_int cst0;
10221 /* Now we know that arg0 is (C + D) or (C - D) or
10222 -C and arg1 (M) is == (1LL << cst) - 1.
10223 Store C into PMOP[0] and D into PMOP[1]. */
10224 pmop[0] = TREE_OPERAND (arg0, 0);
10225 pmop[1] = NULL;
10226 if (TREE_CODE (arg0) != NEGATE_EXPR)
10228 pmop[1] = TREE_OPERAND (arg0, 1);
10229 which = 1;
10232 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10233 which = -1;
10235 for (; which >= 0; which--)
10236 switch (TREE_CODE (pmop[which]))
10238 case BIT_AND_EXPR:
10239 case BIT_IOR_EXPR:
10240 case BIT_XOR_EXPR:
10241 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10242 != INTEGER_CST)
10243 break;
10244 cst0 = wi::to_wide (TREE_OPERAND (pmop[which], 1)) & cst1;
10245 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10247 if (cst0 != cst1)
10248 break;
10250 else if (cst0 != 0)
10251 break;
10252 /* If C or D is of the form (A & N) where
10253 (N & M) == M, or of the form (A | N) or
10254 (A ^ N) where (N & M) == 0, replace it with A. */
10255 pmop[which] = TREE_OPERAND (pmop[which], 0);
10256 break;
10257 case INTEGER_CST:
10258 /* If C or D is a N where (N & M) == 0, it can be
10259 omitted (assumed 0). */
10260 if ((TREE_CODE (arg0) == PLUS_EXPR
10261 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10262 && (cst1 & wi::to_wide (pmop[which])) == 0)
10263 pmop[which] = NULL;
10264 break;
10265 default:
10266 break;
10269 /* Only build anything new if we optimized one or both arguments
10270 above. */
10271 if (pmop[0] != TREE_OPERAND (arg0, 0)
10272 || (TREE_CODE (arg0) != NEGATE_EXPR
10273 && pmop[1] != TREE_OPERAND (arg0, 1)))
10275 tree utype = TREE_TYPE (arg0);
10276 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10278 /* Perform the operations in a type that has defined
10279 overflow behavior. */
10280 utype = unsigned_type_for (TREE_TYPE (arg0));
10281 if (pmop[0] != NULL)
10282 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10283 if (pmop[1] != NULL)
10284 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10287 if (TREE_CODE (arg0) == NEGATE_EXPR)
10288 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10289 else if (TREE_CODE (arg0) == PLUS_EXPR)
10291 if (pmop[0] != NULL && pmop[1] != NULL)
10292 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10293 pmop[0], pmop[1]);
10294 else if (pmop[0] != NULL)
10295 tem = pmop[0];
10296 else if (pmop[1] != NULL)
10297 tem = pmop[1];
10298 else
10299 return build_int_cst (type, 0);
10301 else if (pmop[0] == NULL)
10302 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10303 else
10304 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10305 pmop[0], pmop[1]);
10306 /* TEM is now the new binary +, - or unary - replacement. */
10307 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10308 fold_convert_loc (loc, utype, arg1));
10309 return fold_convert_loc (loc, type, tem);
10314 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10315 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10316 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10318 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10320 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
10321 if (mask == -1)
10322 return
10323 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10326 goto associate;
10328 case RDIV_EXPR:
10329 /* Don't touch a floating-point divide by zero unless the mode
10330 of the constant can represent infinity. */
10331 if (TREE_CODE (arg1) == REAL_CST
10332 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10333 && real_zerop (arg1))
10334 return NULL_TREE;
10336 /* (-A) / (-B) -> A / B */
10337 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10338 return fold_build2_loc (loc, RDIV_EXPR, type,
10339 TREE_OPERAND (arg0, 0),
10340 negate_expr (arg1));
10341 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10342 return fold_build2_loc (loc, RDIV_EXPR, type,
10343 negate_expr (arg0),
10344 TREE_OPERAND (arg1, 0));
10345 return NULL_TREE;
10347 case TRUNC_DIV_EXPR:
10348 /* Fall through */
10350 case FLOOR_DIV_EXPR:
10351 /* Simplify A / (B << N) where A and B are positive and B is
10352 a power of 2, to A >> (N + log2(B)). */
10353 strict_overflow_p = false;
10354 if (TREE_CODE (arg1) == LSHIFT_EXPR
10355 && (TYPE_UNSIGNED (type)
10356 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10358 tree sval = TREE_OPERAND (arg1, 0);
10359 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10361 tree sh_cnt = TREE_OPERAND (arg1, 1);
10362 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10363 wi::exact_log2 (wi::to_wide (sval)));
10365 if (strict_overflow_p)
10366 fold_overflow_warning (("assuming signed overflow does not "
10367 "occur when simplifying A / (B << N)"),
10368 WARN_STRICT_OVERFLOW_MISC);
10370 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10371 sh_cnt, pow2);
10372 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10373 fold_convert_loc (loc, type, arg0), sh_cnt);
10377 /* Fall through */
10379 case ROUND_DIV_EXPR:
10380 case CEIL_DIV_EXPR:
10381 case EXACT_DIV_EXPR:
10382 if (integer_zerop (arg1))
10383 return NULL_TREE;
10385 /* Convert -A / -B to A / B when the type is signed and overflow is
10386 undefined. */
10387 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10388 && TREE_CODE (op0) == NEGATE_EXPR
10389 && negate_expr_p (op1))
10391 if (INTEGRAL_TYPE_P (type))
10392 fold_overflow_warning (("assuming signed overflow does not occur "
10393 "when distributing negation across "
10394 "division"),
10395 WARN_STRICT_OVERFLOW_MISC);
10396 return fold_build2_loc (loc, code, type,
10397 fold_convert_loc (loc, type,
10398 TREE_OPERAND (arg0, 0)),
10399 negate_expr (op1));
10401 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10402 && TREE_CODE (arg1) == NEGATE_EXPR
10403 && negate_expr_p (op0))
10405 if (INTEGRAL_TYPE_P (type))
10406 fold_overflow_warning (("assuming signed overflow does not occur "
10407 "when distributing negation across "
10408 "division"),
10409 WARN_STRICT_OVERFLOW_MISC);
10410 return fold_build2_loc (loc, code, type,
10411 negate_expr (op0),
10412 fold_convert_loc (loc, type,
10413 TREE_OPERAND (arg1, 0)));
10416 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10417 operation, EXACT_DIV_EXPR.
10419 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10420 At one time others generated faster code, it's not clear if they do
10421 after the last round to changes to the DIV code in expmed.c. */
10422 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10423 && multiple_of_p (type, arg0, arg1))
10424 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10425 fold_convert (type, arg0),
10426 fold_convert (type, arg1));
10428 strict_overflow_p = false;
10429 if (TREE_CODE (arg1) == INTEGER_CST
10430 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10431 &strict_overflow_p)) != 0)
10433 if (strict_overflow_p)
10434 fold_overflow_warning (("assuming signed overflow does not occur "
10435 "when simplifying division"),
10436 WARN_STRICT_OVERFLOW_MISC);
10437 return fold_convert_loc (loc, type, tem);
10440 return NULL_TREE;
10442 case CEIL_MOD_EXPR:
10443 case FLOOR_MOD_EXPR:
10444 case ROUND_MOD_EXPR:
10445 case TRUNC_MOD_EXPR:
10446 strict_overflow_p = false;
10447 if (TREE_CODE (arg1) == INTEGER_CST
10448 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10449 &strict_overflow_p)) != 0)
10451 if (strict_overflow_p)
10452 fold_overflow_warning (("assuming signed overflow does not occur "
10453 "when simplifying modulus"),
10454 WARN_STRICT_OVERFLOW_MISC);
10455 return fold_convert_loc (loc, type, tem);
10458 return NULL_TREE;
10460 case LROTATE_EXPR:
10461 case RROTATE_EXPR:
10462 case RSHIFT_EXPR:
10463 case LSHIFT_EXPR:
10464 /* Since negative shift count is not well-defined,
10465 don't try to compute it in the compiler. */
10466 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10467 return NULL_TREE;
10469 prec = element_precision (type);
10471 /* If we have a rotate of a bit operation with the rotate count and
10472 the second operand of the bit operation both constant,
10473 permute the two operations. */
10474 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10475 && (TREE_CODE (arg0) == BIT_AND_EXPR
10476 || TREE_CODE (arg0) == BIT_IOR_EXPR
10477 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10478 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10480 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10481 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10482 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10483 fold_build2_loc (loc, code, type,
10484 arg00, arg1),
10485 fold_build2_loc (loc, code, type,
10486 arg01, arg1));
10489 /* Two consecutive rotates adding up to the some integer
10490 multiple of the precision of the type can be ignored. */
10491 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10492 && TREE_CODE (arg0) == RROTATE_EXPR
10493 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10494 && wi::umod_trunc (wi::to_wide (arg1)
10495 + wi::to_wide (TREE_OPERAND (arg0, 1)),
10496 prec) == 0)
10497 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10499 return NULL_TREE;
10501 case MIN_EXPR:
10502 case MAX_EXPR:
10503 goto associate;
10505 case TRUTH_ANDIF_EXPR:
10506 /* Note that the operands of this must be ints
10507 and their values must be 0 or 1.
10508 ("true" is a fixed value perhaps depending on the language.) */
10509 /* If first arg is constant zero, return it. */
10510 if (integer_zerop (arg0))
10511 return fold_convert_loc (loc, type, arg0);
10512 /* FALLTHRU */
10513 case TRUTH_AND_EXPR:
10514 /* If either arg is constant true, drop it. */
10515 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10516 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10517 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10518 /* Preserve sequence points. */
10519 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10520 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10521 /* If second arg is constant zero, result is zero, but first arg
10522 must be evaluated. */
10523 if (integer_zerop (arg1))
10524 return omit_one_operand_loc (loc, type, arg1, arg0);
10525 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10526 case will be handled here. */
10527 if (integer_zerop (arg0))
10528 return omit_one_operand_loc (loc, type, arg0, arg1);
10530 /* !X && X is always false. */
10531 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10532 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10533 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10534 /* X && !X is always false. */
10535 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10536 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10537 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10539 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10540 means A >= Y && A != MAX, but in this case we know that
10541 A < X <= MAX. */
10543 if (!TREE_SIDE_EFFECTS (arg0)
10544 && !TREE_SIDE_EFFECTS (arg1))
10546 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10547 if (tem && !operand_equal_p (tem, arg0, 0))
10548 return fold_build2_loc (loc, code, type, tem, arg1);
10550 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10551 if (tem && !operand_equal_p (tem, arg1, 0))
10552 return fold_build2_loc (loc, code, type, arg0, tem);
10555 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10556 != NULL_TREE)
10557 return tem;
10559 return NULL_TREE;
10561 case TRUTH_ORIF_EXPR:
10562 /* Note that the operands of this must be ints
10563 and their values must be 0 or true.
10564 ("true" is a fixed value perhaps depending on the language.) */
10565 /* If first arg is constant true, return it. */
10566 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10567 return fold_convert_loc (loc, type, arg0);
10568 /* FALLTHRU */
10569 case TRUTH_OR_EXPR:
10570 /* If either arg is constant zero, drop it. */
10571 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10572 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10573 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10574 /* Preserve sequence points. */
10575 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10576 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10577 /* If second arg is constant true, result is true, but we must
10578 evaluate first arg. */
10579 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10580 return omit_one_operand_loc (loc, type, arg1, arg0);
10581 /* Likewise for first arg, but note this only occurs here for
10582 TRUTH_OR_EXPR. */
10583 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10584 return omit_one_operand_loc (loc, type, arg0, arg1);
10586 /* !X || X is always true. */
10587 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10588 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10589 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10590 /* X || !X is always true. */
10591 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10592 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10593 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10595 /* (X && !Y) || (!X && Y) is X ^ Y */
10596 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10597 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10599 tree a0, a1, l0, l1, n0, n1;
10601 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10602 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10604 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10605 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10607 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10608 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10610 if ((operand_equal_p (n0, a0, 0)
10611 && operand_equal_p (n1, a1, 0))
10612 || (operand_equal_p (n0, a1, 0)
10613 && operand_equal_p (n1, a0, 0)))
10614 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10617 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10618 != NULL_TREE)
10619 return tem;
10621 return NULL_TREE;
10623 case TRUTH_XOR_EXPR:
10624 /* If the second arg is constant zero, drop it. */
10625 if (integer_zerop (arg1))
10626 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10627 /* If the second arg is constant true, this is a logical inversion. */
10628 if (integer_onep (arg1))
10630 tem = invert_truthvalue_loc (loc, arg0);
10631 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10633 /* Identical arguments cancel to zero. */
10634 if (operand_equal_p (arg0, arg1, 0))
10635 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10637 /* !X ^ X is always true. */
10638 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10639 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10640 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10642 /* X ^ !X is always true. */
10643 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10644 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10645 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10647 return NULL_TREE;
10649 case EQ_EXPR:
10650 case NE_EXPR:
10651 STRIP_NOPS (arg0);
10652 STRIP_NOPS (arg1);
10654 tem = fold_comparison (loc, code, type, op0, op1);
10655 if (tem != NULL_TREE)
10656 return tem;
10658 /* bool_var != 1 becomes !bool_var. */
10659 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10660 && code == NE_EXPR)
10661 return fold_convert_loc (loc, type,
10662 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10663 TREE_TYPE (arg0), arg0));
10665 /* bool_var == 0 becomes !bool_var. */
10666 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10667 && code == EQ_EXPR)
10668 return fold_convert_loc (loc, type,
10669 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10670 TREE_TYPE (arg0), arg0));
10672 /* !exp != 0 becomes !exp */
10673 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10674 && code == NE_EXPR)
10675 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10677 /* If this is an EQ or NE comparison with zero and ARG0 is
10678 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10679 two operations, but the latter can be done in one less insn
10680 on machines that have only two-operand insns or on which a
10681 constant cannot be the first operand. */
10682 if (TREE_CODE (arg0) == BIT_AND_EXPR
10683 && integer_zerop (arg1))
10685 tree arg00 = TREE_OPERAND (arg0, 0);
10686 tree arg01 = TREE_OPERAND (arg0, 1);
10687 if (TREE_CODE (arg00) == LSHIFT_EXPR
10688 && integer_onep (TREE_OPERAND (arg00, 0)))
10690 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10691 arg01, TREE_OPERAND (arg00, 1));
10692 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10693 build_int_cst (TREE_TYPE (arg0), 1));
10694 return fold_build2_loc (loc, code, type,
10695 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10696 arg1);
10698 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10699 && integer_onep (TREE_OPERAND (arg01, 0)))
10701 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10702 arg00, TREE_OPERAND (arg01, 1));
10703 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10704 build_int_cst (TREE_TYPE (arg0), 1));
10705 return fold_build2_loc (loc, code, type,
10706 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10707 arg1);
10711 /* If this is an NE or EQ comparison of zero against the result of a
10712 signed MOD operation whose second operand is a power of 2, make
10713 the MOD operation unsigned since it is simpler and equivalent. */
10714 if (integer_zerop (arg1)
10715 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10716 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10717 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10718 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10719 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10720 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10722 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10723 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10724 fold_convert_loc (loc, newtype,
10725 TREE_OPERAND (arg0, 0)),
10726 fold_convert_loc (loc, newtype,
10727 TREE_OPERAND (arg0, 1)));
10729 return fold_build2_loc (loc, code, type, newmod,
10730 fold_convert_loc (loc, newtype, arg1));
10733 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10734 C1 is a valid shift constant, and C2 is a power of two, i.e.
10735 a single bit. */
10736 if (TREE_CODE (arg0) == BIT_AND_EXPR
10737 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10738 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10739 == INTEGER_CST
10740 && integer_pow2p (TREE_OPERAND (arg0, 1))
10741 && integer_zerop (arg1))
10743 tree itype = TREE_TYPE (arg0);
10744 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10745 prec = TYPE_PRECISION (itype);
10747 /* Check for a valid shift count. */
10748 if (wi::ltu_p (wi::to_wide (arg001), prec))
10750 tree arg01 = TREE_OPERAND (arg0, 1);
10751 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10752 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10753 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10754 can be rewritten as (X & (C2 << C1)) != 0. */
10755 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10757 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10758 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10759 return fold_build2_loc (loc, code, type, tem,
10760 fold_convert_loc (loc, itype, arg1));
10762 /* Otherwise, for signed (arithmetic) shifts,
10763 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10764 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10765 else if (!TYPE_UNSIGNED (itype))
10766 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10767 arg000, build_int_cst (itype, 0));
10768 /* Otherwise, of unsigned (logical) shifts,
10769 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10770 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10771 else
10772 return omit_one_operand_loc (loc, type,
10773 code == EQ_EXPR ? integer_one_node
10774 : integer_zero_node,
10775 arg000);
10779 /* If this is a comparison of a field, we may be able to simplify it. */
10780 if ((TREE_CODE (arg0) == COMPONENT_REF
10781 || TREE_CODE (arg0) == BIT_FIELD_REF)
10782 /* Handle the constant case even without -O
10783 to make sure the warnings are given. */
10784 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10786 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10787 if (t1)
10788 return t1;
10791 /* Optimize comparisons of strlen vs zero to a compare of the
10792 first character of the string vs zero. To wit,
10793 strlen(ptr) == 0 => *ptr == 0
10794 strlen(ptr) != 0 => *ptr != 0
10795 Other cases should reduce to one of these two (or a constant)
10796 due to the return value of strlen being unsigned. */
10797 if (TREE_CODE (arg0) == CALL_EXPR
10798 && integer_zerop (arg1))
10800 tree fndecl = get_callee_fndecl (arg0);
10802 if (fndecl
10803 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10804 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10805 && call_expr_nargs (arg0) == 1
10806 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10808 tree iref = build_fold_indirect_ref_loc (loc,
10809 CALL_EXPR_ARG (arg0, 0));
10810 return fold_build2_loc (loc, code, type, iref,
10811 build_int_cst (TREE_TYPE (iref), 0));
10815 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10816 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10817 if (TREE_CODE (arg0) == RSHIFT_EXPR
10818 && integer_zerop (arg1)
10819 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10821 tree arg00 = TREE_OPERAND (arg0, 0);
10822 tree arg01 = TREE_OPERAND (arg0, 1);
10823 tree itype = TREE_TYPE (arg00);
10824 if (wi::to_wide (arg01) == element_precision (itype) - 1)
10826 if (TYPE_UNSIGNED (itype))
10828 itype = signed_type_for (itype);
10829 arg00 = fold_convert_loc (loc, itype, arg00);
10831 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10832 type, arg00, build_zero_cst (itype));
10836 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10837 (X & C) == 0 when C is a single bit. */
10838 if (TREE_CODE (arg0) == BIT_AND_EXPR
10839 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10840 && integer_zerop (arg1)
10841 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10843 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10844 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10845 TREE_OPERAND (arg0, 1));
10846 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10847 type, tem,
10848 fold_convert_loc (loc, TREE_TYPE (arg0),
10849 arg1));
10852 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10853 constant C is a power of two, i.e. a single bit. */
10854 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10855 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10856 && integer_zerop (arg1)
10857 && integer_pow2p (TREE_OPERAND (arg0, 1))
10858 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10859 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10861 tree arg00 = TREE_OPERAND (arg0, 0);
10862 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10863 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10866 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10867 when is C is a power of two, i.e. a single bit. */
10868 if (TREE_CODE (arg0) == BIT_AND_EXPR
10869 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10870 && integer_zerop (arg1)
10871 && integer_pow2p (TREE_OPERAND (arg0, 1))
10872 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10873 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10875 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10876 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10877 arg000, TREE_OPERAND (arg0, 1));
10878 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10879 tem, build_int_cst (TREE_TYPE (tem), 0));
10882 if (integer_zerop (arg1)
10883 && tree_expr_nonzero_p (arg0))
10885 tree res = constant_boolean_node (code==NE_EXPR, type);
10886 return omit_one_operand_loc (loc, type, res, arg0);
10889 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10890 if (TREE_CODE (arg0) == BIT_AND_EXPR
10891 && TREE_CODE (arg1) == BIT_AND_EXPR)
10893 tree arg00 = TREE_OPERAND (arg0, 0);
10894 tree arg01 = TREE_OPERAND (arg0, 1);
10895 tree arg10 = TREE_OPERAND (arg1, 0);
10896 tree arg11 = TREE_OPERAND (arg1, 1);
10897 tree itype = TREE_TYPE (arg0);
10899 if (operand_equal_p (arg01, arg11, 0))
10901 tem = fold_convert_loc (loc, itype, arg10);
10902 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10903 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10904 return fold_build2_loc (loc, code, type, tem,
10905 build_zero_cst (itype));
10907 if (operand_equal_p (arg01, arg10, 0))
10909 tem = fold_convert_loc (loc, itype, arg11);
10910 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10911 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10912 return fold_build2_loc (loc, code, type, tem,
10913 build_zero_cst (itype));
10915 if (operand_equal_p (arg00, arg11, 0))
10917 tem = fold_convert_loc (loc, itype, arg10);
10918 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10919 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10920 return fold_build2_loc (loc, code, type, tem,
10921 build_zero_cst (itype));
10923 if (operand_equal_p (arg00, arg10, 0))
10925 tem = fold_convert_loc (loc, itype, arg11);
10926 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10927 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10928 return fold_build2_loc (loc, code, type, tem,
10929 build_zero_cst (itype));
10933 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10934 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10936 tree arg00 = TREE_OPERAND (arg0, 0);
10937 tree arg01 = TREE_OPERAND (arg0, 1);
10938 tree arg10 = TREE_OPERAND (arg1, 0);
10939 tree arg11 = TREE_OPERAND (arg1, 1);
10940 tree itype = TREE_TYPE (arg0);
10942 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10943 operand_equal_p guarantees no side-effects so we don't need
10944 to use omit_one_operand on Z. */
10945 if (operand_equal_p (arg01, arg11, 0))
10946 return fold_build2_loc (loc, code, type, arg00,
10947 fold_convert_loc (loc, TREE_TYPE (arg00),
10948 arg10));
10949 if (operand_equal_p (arg01, arg10, 0))
10950 return fold_build2_loc (loc, code, type, arg00,
10951 fold_convert_loc (loc, TREE_TYPE (arg00),
10952 arg11));
10953 if (operand_equal_p (arg00, arg11, 0))
10954 return fold_build2_loc (loc, code, type, arg01,
10955 fold_convert_loc (loc, TREE_TYPE (arg01),
10956 arg10));
10957 if (operand_equal_p (arg00, arg10, 0))
10958 return fold_build2_loc (loc, code, type, arg01,
10959 fold_convert_loc (loc, TREE_TYPE (arg01),
10960 arg11));
10962 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10963 if (TREE_CODE (arg01) == INTEGER_CST
10964 && TREE_CODE (arg11) == INTEGER_CST)
10966 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10967 fold_convert_loc (loc, itype, arg11));
10968 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10969 return fold_build2_loc (loc, code, type, tem,
10970 fold_convert_loc (loc, itype, arg10));
10974 /* Attempt to simplify equality/inequality comparisons of complex
10975 values. Only lower the comparison if the result is known or
10976 can be simplified to a single scalar comparison. */
10977 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10978 || TREE_CODE (arg0) == COMPLEX_CST)
10979 && (TREE_CODE (arg1) == COMPLEX_EXPR
10980 || TREE_CODE (arg1) == COMPLEX_CST))
10982 tree real0, imag0, real1, imag1;
10983 tree rcond, icond;
10985 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10987 real0 = TREE_OPERAND (arg0, 0);
10988 imag0 = TREE_OPERAND (arg0, 1);
10990 else
10992 real0 = TREE_REALPART (arg0);
10993 imag0 = TREE_IMAGPART (arg0);
10996 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10998 real1 = TREE_OPERAND (arg1, 0);
10999 imag1 = TREE_OPERAND (arg1, 1);
11001 else
11003 real1 = TREE_REALPART (arg1);
11004 imag1 = TREE_IMAGPART (arg1);
11007 rcond = fold_binary_loc (loc, code, type, real0, real1);
11008 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11010 if (integer_zerop (rcond))
11012 if (code == EQ_EXPR)
11013 return omit_two_operands_loc (loc, type, boolean_false_node,
11014 imag0, imag1);
11015 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
11017 else
11019 if (code == NE_EXPR)
11020 return omit_two_operands_loc (loc, type, boolean_true_node,
11021 imag0, imag1);
11022 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
11026 icond = fold_binary_loc (loc, code, type, imag0, imag1);
11027 if (icond && TREE_CODE (icond) == INTEGER_CST)
11029 if (integer_zerop (icond))
11031 if (code == EQ_EXPR)
11032 return omit_two_operands_loc (loc, type, boolean_false_node,
11033 real0, real1);
11034 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11036 else
11038 if (code == NE_EXPR)
11039 return omit_two_operands_loc (loc, type, boolean_true_node,
11040 real0, real1);
11041 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11046 return NULL_TREE;
11048 case LT_EXPR:
11049 case GT_EXPR:
11050 case LE_EXPR:
11051 case GE_EXPR:
11052 tem = fold_comparison (loc, code, type, op0, op1);
11053 if (tem != NULL_TREE)
11054 return tem;
11056 /* Transform comparisons of the form X +- C CMP X. */
11057 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11058 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11059 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11060 && !HONOR_SNANS (arg0))
11062 tree arg01 = TREE_OPERAND (arg0, 1);
11063 enum tree_code code0 = TREE_CODE (arg0);
11064 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11066 /* (X - c) > X becomes false. */
11067 if (code == GT_EXPR
11068 && ((code0 == MINUS_EXPR && is_positive >= 0)
11069 || (code0 == PLUS_EXPR && is_positive <= 0)))
11070 return constant_boolean_node (0, type);
11072 /* Likewise (X + c) < X becomes false. */
11073 if (code == LT_EXPR
11074 && ((code0 == PLUS_EXPR && is_positive >= 0)
11075 || (code0 == MINUS_EXPR && is_positive <= 0)))
11076 return constant_boolean_node (0, type);
11078 /* Convert (X - c) <= X to true. */
11079 if (!HONOR_NANS (arg1)
11080 && code == LE_EXPR
11081 && ((code0 == MINUS_EXPR && is_positive >= 0)
11082 || (code0 == PLUS_EXPR && is_positive <= 0)))
11083 return constant_boolean_node (1, type);
11085 /* Convert (X + c) >= X to true. */
11086 if (!HONOR_NANS (arg1)
11087 && code == GE_EXPR
11088 && ((code0 == PLUS_EXPR && is_positive >= 0)
11089 || (code0 == MINUS_EXPR && is_positive <= 0)))
11090 return constant_boolean_node (1, type);
11093 /* If we are comparing an ABS_EXPR with a constant, we can
11094 convert all the cases into explicit comparisons, but they may
11095 well not be faster than doing the ABS and one comparison.
11096 But ABS (X) <= C is a range comparison, which becomes a subtraction
11097 and a comparison, and is probably faster. */
11098 if (code == LE_EXPR
11099 && TREE_CODE (arg1) == INTEGER_CST
11100 && TREE_CODE (arg0) == ABS_EXPR
11101 && ! TREE_SIDE_EFFECTS (arg0)
11102 && (tem = negate_expr (arg1)) != 0
11103 && TREE_CODE (tem) == INTEGER_CST
11104 && !TREE_OVERFLOW (tem))
11105 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11106 build2 (GE_EXPR, type,
11107 TREE_OPERAND (arg0, 0), tem),
11108 build2 (LE_EXPR, type,
11109 TREE_OPERAND (arg0, 0), arg1));
11111 /* Convert ABS_EXPR<x> >= 0 to true. */
11112 strict_overflow_p = false;
11113 if (code == GE_EXPR
11114 && (integer_zerop (arg1)
11115 || (! HONOR_NANS (arg0)
11116 && real_zerop (arg1)))
11117 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11119 if (strict_overflow_p)
11120 fold_overflow_warning (("assuming signed overflow does not occur "
11121 "when simplifying comparison of "
11122 "absolute value and zero"),
11123 WARN_STRICT_OVERFLOW_CONDITIONAL);
11124 return omit_one_operand_loc (loc, type,
11125 constant_boolean_node (true, type),
11126 arg0);
11129 /* Convert ABS_EXPR<x> < 0 to false. */
11130 strict_overflow_p = false;
11131 if (code == LT_EXPR
11132 && (integer_zerop (arg1) || real_zerop (arg1))
11133 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11135 if (strict_overflow_p)
11136 fold_overflow_warning (("assuming signed overflow does not occur "
11137 "when simplifying comparison of "
11138 "absolute value and zero"),
11139 WARN_STRICT_OVERFLOW_CONDITIONAL);
11140 return omit_one_operand_loc (loc, type,
11141 constant_boolean_node (false, type),
11142 arg0);
11145 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11146 and similarly for >= into !=. */
11147 if ((code == LT_EXPR || code == GE_EXPR)
11148 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11149 && TREE_CODE (arg1) == LSHIFT_EXPR
11150 && integer_onep (TREE_OPERAND (arg1, 0)))
11151 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11152 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11153 TREE_OPERAND (arg1, 1)),
11154 build_zero_cst (TREE_TYPE (arg0)));
11156 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11157 otherwise Y might be >= # of bits in X's type and thus e.g.
11158 (unsigned char) (1 << Y) for Y 15 might be 0.
11159 If the cast is widening, then 1 << Y should have unsigned type,
11160 otherwise if Y is number of bits in the signed shift type minus 1,
11161 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11162 31 might be 0xffffffff80000000. */
11163 if ((code == LT_EXPR || code == GE_EXPR)
11164 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11165 && CONVERT_EXPR_P (arg1)
11166 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11167 && (element_precision (TREE_TYPE (arg1))
11168 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11169 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11170 || (element_precision (TREE_TYPE (arg1))
11171 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11172 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11174 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11175 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11176 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11177 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11178 build_zero_cst (TREE_TYPE (arg0)));
11181 return NULL_TREE;
11183 case UNORDERED_EXPR:
11184 case ORDERED_EXPR:
11185 case UNLT_EXPR:
11186 case UNLE_EXPR:
11187 case UNGT_EXPR:
11188 case UNGE_EXPR:
11189 case UNEQ_EXPR:
11190 case LTGT_EXPR:
11191 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11193 tree targ0 = strip_float_extensions (arg0);
11194 tree targ1 = strip_float_extensions (arg1);
11195 tree newtype = TREE_TYPE (targ0);
11197 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11198 newtype = TREE_TYPE (targ1);
11200 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11201 return fold_build2_loc (loc, code, type,
11202 fold_convert_loc (loc, newtype, targ0),
11203 fold_convert_loc (loc, newtype, targ1));
11206 return NULL_TREE;
11208 case COMPOUND_EXPR:
11209 /* When pedantic, a compound expression can be neither an lvalue
11210 nor an integer constant expression. */
11211 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11212 return NULL_TREE;
11213 /* Don't let (0, 0) be null pointer constant. */
11214 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11215 : fold_convert_loc (loc, type, arg1);
11216 return pedantic_non_lvalue_loc (loc, tem);
11218 case ASSERT_EXPR:
11219 /* An ASSERT_EXPR should never be passed to fold_binary. */
11220 gcc_unreachable ();
11222 default:
11223 return NULL_TREE;
11224 } /* switch (code) */
11227 /* Used by contains_label_[p1]. */
11229 struct contains_label_data
11231 hash_set<tree> *pset;
11232 bool inside_switch_p;
11235 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11236 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
11237 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
11239 static tree
11240 contains_label_1 (tree *tp, int *walk_subtrees, void *data)
11242 contains_label_data *d = (contains_label_data *) data;
11243 switch (TREE_CODE (*tp))
11245 case LABEL_EXPR:
11246 return *tp;
11248 case CASE_LABEL_EXPR:
11249 if (!d->inside_switch_p)
11250 return *tp;
11251 return NULL_TREE;
11253 case SWITCH_EXPR:
11254 if (!d->inside_switch_p)
11256 if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
11257 return *tp;
11258 d->inside_switch_p = true;
11259 if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
11260 return *tp;
11261 d->inside_switch_p = false;
11262 *walk_subtrees = 0;
11264 return NULL_TREE;
11266 case GOTO_EXPR:
11267 *walk_subtrees = 0;
11268 return NULL_TREE;
11270 default:
11271 return NULL_TREE;
11275 /* Return whether the sub-tree ST contains a label which is accessible from
11276 outside the sub-tree. */
11278 static bool
11279 contains_label_p (tree st)
11281 hash_set<tree> pset;
11282 contains_label_data data = { &pset, false };
11283 return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
11286 /* Fold a ternary expression of code CODE and type TYPE with operands
11287 OP0, OP1, and OP2. Return the folded expression if folding is
11288 successful. Otherwise, return NULL_TREE. */
11290 tree
11291 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11292 tree op0, tree op1, tree op2)
11294 tree tem;
11295 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11296 enum tree_code_class kind = TREE_CODE_CLASS (code);
11298 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11299 && TREE_CODE_LENGTH (code) == 3);
11301 /* If this is a commutative operation, and OP0 is a constant, move it
11302 to OP1 to reduce the number of tests below. */
11303 if (commutative_ternary_tree_code (code)
11304 && tree_swap_operands_p (op0, op1))
11305 return fold_build3_loc (loc, code, type, op1, op0, op2);
11307 tem = generic_simplify (loc, code, type, op0, op1, op2);
11308 if (tem)
11309 return tem;
11311 /* Strip any conversions that don't change the mode. This is safe
11312 for every expression, except for a comparison expression because
11313 its signedness is derived from its operands. So, in the latter
11314 case, only strip conversions that don't change the signedness.
11316 Note that this is done as an internal manipulation within the
11317 constant folder, in order to find the simplest representation of
11318 the arguments so that their form can be studied. In any cases,
11319 the appropriate type conversions should be put back in the tree
11320 that will get out of the constant folder. */
11321 if (op0)
11323 arg0 = op0;
11324 STRIP_NOPS (arg0);
11327 if (op1)
11329 arg1 = op1;
11330 STRIP_NOPS (arg1);
11333 if (op2)
11335 arg2 = op2;
11336 STRIP_NOPS (arg2);
11339 switch (code)
11341 case COMPONENT_REF:
11342 if (TREE_CODE (arg0) == CONSTRUCTOR
11343 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11345 unsigned HOST_WIDE_INT idx;
11346 tree field, value;
11347 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11348 if (field == arg1)
11349 return value;
11351 return NULL_TREE;
11353 case COND_EXPR:
11354 case VEC_COND_EXPR:
11355 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11356 so all simple results must be passed through pedantic_non_lvalue. */
11357 if (TREE_CODE (arg0) == INTEGER_CST)
11359 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11360 tem = integer_zerop (arg0) ? op2 : op1;
11361 /* Only optimize constant conditions when the selected branch
11362 has the same type as the COND_EXPR. This avoids optimizing
11363 away "c ? x : throw", where the throw has a void type.
11364 Avoid throwing away that operand which contains label. */
11365 if ((!TREE_SIDE_EFFECTS (unused_op)
11366 || !contains_label_p (unused_op))
11367 && (! VOID_TYPE_P (TREE_TYPE (tem))
11368 || VOID_TYPE_P (type)))
11369 return pedantic_non_lvalue_loc (loc, tem);
11370 return NULL_TREE;
11372 else if (TREE_CODE (arg0) == VECTOR_CST)
11374 if ((TREE_CODE (arg1) == VECTOR_CST
11375 || TREE_CODE (arg1) == CONSTRUCTOR)
11376 && (TREE_CODE (arg2) == VECTOR_CST
11377 || TREE_CODE (arg2) == CONSTRUCTOR))
11379 unsigned int nelts = VECTOR_CST_NELTS (arg0), i;
11380 gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
11381 vec_perm_builder sel (nelts, nelts, 1);
11382 for (i = 0; i < nelts; i++)
11384 tree val = VECTOR_CST_ELT (arg0, i);
11385 if (integer_all_onesp (val))
11386 sel.quick_push (i);
11387 else if (integer_zerop (val))
11388 sel.quick_push (nelts + i);
11389 else /* Currently unreachable. */
11390 return NULL_TREE;
11392 tree t = fold_vec_perm (type, arg1, arg2,
11393 vec_perm_indices (sel, 2, nelts));
11394 if (t != NULL_TREE)
11395 return t;
11399 /* If we have A op B ? A : C, we may be able to convert this to a
11400 simpler expression, depending on the operation and the values
11401 of B and C. Signed zeros prevent all of these transformations,
11402 for reasons given above each one.
11404 Also try swapping the arguments and inverting the conditional. */
11405 if (COMPARISON_CLASS_P (arg0)
11406 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
11407 && !HONOR_SIGNED_ZEROS (element_mode (op1)))
11409 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11410 if (tem)
11411 return tem;
11414 if (COMPARISON_CLASS_P (arg0)
11415 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11416 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11418 location_t loc0 = expr_location_or (arg0, loc);
11419 tem = fold_invert_truthvalue (loc0, arg0);
11420 if (tem && COMPARISON_CLASS_P (tem))
11422 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11423 if (tem)
11424 return tem;
11428 /* If the second operand is simpler than the third, swap them
11429 since that produces better jump optimization results. */
11430 if (truth_value_p (TREE_CODE (arg0))
11431 && tree_swap_operands_p (op1, op2))
11433 location_t loc0 = expr_location_or (arg0, loc);
11434 /* See if this can be inverted. If it can't, possibly because
11435 it was a floating-point inequality comparison, don't do
11436 anything. */
11437 tem = fold_invert_truthvalue (loc0, arg0);
11438 if (tem)
11439 return fold_build3_loc (loc, code, type, tem, op2, op1);
11442 /* Convert A ? 1 : 0 to simply A. */
11443 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11444 : (integer_onep (op1)
11445 && !VECTOR_TYPE_P (type)))
11446 && integer_zerop (op2)
11447 /* If we try to convert OP0 to our type, the
11448 call to fold will try to move the conversion inside
11449 a COND, which will recurse. In that case, the COND_EXPR
11450 is probably the best choice, so leave it alone. */
11451 && type == TREE_TYPE (arg0))
11452 return pedantic_non_lvalue_loc (loc, arg0);
11454 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11455 over COND_EXPR in cases such as floating point comparisons. */
11456 if (integer_zerop (op1)
11457 && code == COND_EXPR
11458 && integer_onep (op2)
11459 && !VECTOR_TYPE_P (type)
11460 && truth_value_p (TREE_CODE (arg0)))
11461 return pedantic_non_lvalue_loc (loc,
11462 fold_convert_loc (loc, type,
11463 invert_truthvalue_loc (loc,
11464 arg0)));
11466 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11467 if (TREE_CODE (arg0) == LT_EXPR
11468 && integer_zerop (TREE_OPERAND (arg0, 1))
11469 && integer_zerop (op2)
11470 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11472 /* sign_bit_p looks through both zero and sign extensions,
11473 but for this optimization only sign extensions are
11474 usable. */
11475 tree tem2 = TREE_OPERAND (arg0, 0);
11476 while (tem != tem2)
11478 if (TREE_CODE (tem2) != NOP_EXPR
11479 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11481 tem = NULL_TREE;
11482 break;
11484 tem2 = TREE_OPERAND (tem2, 0);
11486 /* sign_bit_p only checks ARG1 bits within A's precision.
11487 If <sign bit of A> has wider type than A, bits outside
11488 of A's precision in <sign bit of A> need to be checked.
11489 If they are all 0, this optimization needs to be done
11490 in unsigned A's type, if they are all 1 in signed A's type,
11491 otherwise this can't be done. */
11492 if (tem
11493 && TYPE_PRECISION (TREE_TYPE (tem))
11494 < TYPE_PRECISION (TREE_TYPE (arg1))
11495 && TYPE_PRECISION (TREE_TYPE (tem))
11496 < TYPE_PRECISION (type))
11498 int inner_width, outer_width;
11499 tree tem_type;
11501 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11502 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11503 if (outer_width > TYPE_PRECISION (type))
11504 outer_width = TYPE_PRECISION (type);
11506 wide_int mask = wi::shifted_mask
11507 (inner_width, outer_width - inner_width, false,
11508 TYPE_PRECISION (TREE_TYPE (arg1)));
11510 wide_int common = mask & wi::to_wide (arg1);
11511 if (common == mask)
11513 tem_type = signed_type_for (TREE_TYPE (tem));
11514 tem = fold_convert_loc (loc, tem_type, tem);
11516 else if (common == 0)
11518 tem_type = unsigned_type_for (TREE_TYPE (tem));
11519 tem = fold_convert_loc (loc, tem_type, tem);
11521 else
11522 tem = NULL;
11525 if (tem)
11526 return
11527 fold_convert_loc (loc, type,
11528 fold_build2_loc (loc, BIT_AND_EXPR,
11529 TREE_TYPE (tem), tem,
11530 fold_convert_loc (loc,
11531 TREE_TYPE (tem),
11532 arg1)));
11535 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11536 already handled above. */
11537 if (TREE_CODE (arg0) == BIT_AND_EXPR
11538 && integer_onep (TREE_OPERAND (arg0, 1))
11539 && integer_zerop (op2)
11540 && integer_pow2p (arg1))
11542 tree tem = TREE_OPERAND (arg0, 0);
11543 STRIP_NOPS (tem);
11544 if (TREE_CODE (tem) == RSHIFT_EXPR
11545 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11546 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11547 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11548 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11549 fold_convert_loc (loc, type,
11550 TREE_OPERAND (tem, 0)),
11551 op1);
11554 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11555 is probably obsolete because the first operand should be a
11556 truth value (that's why we have the two cases above), but let's
11557 leave it in until we can confirm this for all front-ends. */
11558 if (integer_zerop (op2)
11559 && TREE_CODE (arg0) == NE_EXPR
11560 && integer_zerop (TREE_OPERAND (arg0, 1))
11561 && integer_pow2p (arg1)
11562 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11563 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11564 arg1, OEP_ONLY_CONST))
11565 return pedantic_non_lvalue_loc (loc,
11566 fold_convert_loc (loc, type,
11567 TREE_OPERAND (arg0, 0)));
11569 /* Disable the transformations below for vectors, since
11570 fold_binary_op_with_conditional_arg may undo them immediately,
11571 yielding an infinite loop. */
11572 if (code == VEC_COND_EXPR)
11573 return NULL_TREE;
11575 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11576 if (integer_zerop (op2)
11577 && truth_value_p (TREE_CODE (arg0))
11578 && truth_value_p (TREE_CODE (arg1))
11579 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11580 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11581 : TRUTH_ANDIF_EXPR,
11582 type, fold_convert_loc (loc, type, arg0), op1);
11584 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11585 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11586 && truth_value_p (TREE_CODE (arg0))
11587 && truth_value_p (TREE_CODE (arg1))
11588 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11590 location_t loc0 = expr_location_or (arg0, loc);
11591 /* Only perform transformation if ARG0 is easily inverted. */
11592 tem = fold_invert_truthvalue (loc0, arg0);
11593 if (tem)
11594 return fold_build2_loc (loc, code == VEC_COND_EXPR
11595 ? BIT_IOR_EXPR
11596 : TRUTH_ORIF_EXPR,
11597 type, fold_convert_loc (loc, type, tem),
11598 op1);
11601 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11602 if (integer_zerop (arg1)
11603 && truth_value_p (TREE_CODE (arg0))
11604 && truth_value_p (TREE_CODE (op2))
11605 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11607 location_t loc0 = expr_location_or (arg0, loc);
11608 /* Only perform transformation if ARG0 is easily inverted. */
11609 tem = fold_invert_truthvalue (loc0, arg0);
11610 if (tem)
11611 return fold_build2_loc (loc, code == VEC_COND_EXPR
11612 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11613 type, fold_convert_loc (loc, type, tem),
11614 op2);
11617 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11618 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11619 && truth_value_p (TREE_CODE (arg0))
11620 && truth_value_p (TREE_CODE (op2))
11621 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11622 return fold_build2_loc (loc, code == VEC_COND_EXPR
11623 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11624 type, fold_convert_loc (loc, type, arg0), op2);
11626 return NULL_TREE;
11628 case CALL_EXPR:
11629 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11630 of fold_ternary on them. */
11631 gcc_unreachable ();
11633 case BIT_FIELD_REF:
11634 if (TREE_CODE (arg0) == VECTOR_CST
11635 && (type == TREE_TYPE (TREE_TYPE (arg0))
11636 || (TREE_CODE (type) == VECTOR_TYPE
11637 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11639 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11640 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11641 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11642 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11644 if (n != 0
11645 && (idx % width) == 0
11646 && (n % width) == 0
11647 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11649 idx = idx / width;
11650 n = n / width;
11652 if (TREE_CODE (arg0) == VECTOR_CST)
11654 if (n == 1)
11655 return VECTOR_CST_ELT (arg0, idx);
11657 tree_vector_builder vals (type, n, 1);
11658 for (unsigned i = 0; i < n; ++i)
11659 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
11660 return vals.build ();
11665 /* On constants we can use native encode/interpret to constant
11666 fold (nearly) all BIT_FIELD_REFs. */
11667 if (CONSTANT_CLASS_P (arg0)
11668 && can_native_interpret_type_p (type)
11669 && BITS_PER_UNIT == 8
11670 && tree_fits_uhwi_p (op1)
11671 && tree_fits_uhwi_p (op2))
11673 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11674 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11675 /* Limit us to a reasonable amount of work. To relax the
11676 other limitations we need bit-shifting of the buffer
11677 and rounding up the size. */
11678 if (bitpos % BITS_PER_UNIT == 0
11679 && bitsize % BITS_PER_UNIT == 0
11680 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11682 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11683 unsigned HOST_WIDE_INT len
11684 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11685 bitpos / BITS_PER_UNIT);
11686 if (len > 0
11687 && len * BITS_PER_UNIT >= bitsize)
11689 tree v = native_interpret_expr (type, b,
11690 bitsize / BITS_PER_UNIT);
11691 if (v)
11692 return v;
11697 return NULL_TREE;
11699 case FMA_EXPR:
11700 /* For integers we can decompose the FMA if possible. */
11701 if (TREE_CODE (arg0) == INTEGER_CST
11702 && TREE_CODE (arg1) == INTEGER_CST)
11703 return fold_build2_loc (loc, PLUS_EXPR, type,
11704 const_binop (MULT_EXPR, arg0, arg1), arg2);
11705 if (integer_zerop (arg2))
11706 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11708 return fold_fma (loc, type, arg0, arg1, arg2);
11710 case VEC_PERM_EXPR:
11711 if (TREE_CODE (arg2) == VECTOR_CST)
11713 /* Build a vector of integers from the tree mask. */
11714 vec_perm_builder builder;
11715 if (!tree_to_vec_perm_builder (&builder, arg2))
11716 return NULL_TREE;
11718 /* Create a vec_perm_indices for the integer vector. */
11719 unsigned int nelts = TYPE_VECTOR_SUBPARTS (type);
11720 bool single_arg = (op0 == op1);
11721 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
11723 /* Check for cases that fold to OP0 or OP1 in their original
11724 element order. */
11725 if (sel.series_p (0, 1, 0, 1))
11726 return op0;
11727 if (sel.series_p (0, 1, nelts, 1))
11728 return op1;
11730 if (!single_arg)
11732 if (sel.all_from_input_p (0))
11733 op1 = op0;
11734 else if (sel.all_from_input_p (1))
11736 op0 = op1;
11737 sel.rotate_inputs (1);
11741 if ((TREE_CODE (op0) == VECTOR_CST
11742 || TREE_CODE (op0) == CONSTRUCTOR)
11743 && (TREE_CODE (op1) == VECTOR_CST
11744 || TREE_CODE (op1) == CONSTRUCTOR))
11746 tree t = fold_vec_perm (type, op0, op1, sel);
11747 if (t != NULL_TREE)
11748 return t;
11751 bool changed = (op0 == op1 && !single_arg);
11753 /* Generate a canonical form of the selector. */
11754 if (arg2 == op2 && sel.encoding () != builder)
11756 /* Some targets are deficient and fail to expand a single
11757 argument permutation while still allowing an equivalent
11758 2-argument version. */
11759 if (sel.ninputs () == 2
11760 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
11761 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11762 else
11764 vec_perm_indices sel2 (builder, 2, nelts);
11765 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
11766 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel2);
11767 else
11768 /* Not directly supported with either encoding,
11769 so use the preferred form. */
11770 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11772 changed = true;
11775 if (changed)
11776 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11778 return NULL_TREE;
11780 case BIT_INSERT_EXPR:
11781 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11782 if (TREE_CODE (arg0) == INTEGER_CST
11783 && TREE_CODE (arg1) == INTEGER_CST)
11785 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11786 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11787 wide_int tem = (wi::to_wide (arg0)
11788 & wi::shifted_mask (bitpos, bitsize, true,
11789 TYPE_PRECISION (type)));
11790 wide_int tem2
11791 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11792 bitsize), bitpos);
11793 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11795 else if (TREE_CODE (arg0) == VECTOR_CST
11796 && CONSTANT_CLASS_P (arg1)
11797 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11798 TREE_TYPE (arg1)))
11800 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11801 unsigned HOST_WIDE_INT elsize
11802 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11803 if (bitpos % elsize == 0)
11805 unsigned k = bitpos / elsize;
11806 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11807 return arg0;
11808 else
11810 unsigned int nelts = VECTOR_CST_NELTS (arg0);
11811 tree_vector_builder elts (type, nelts, 1);
11812 elts.quick_grow (nelts);
11813 for (unsigned int i = 0; i < nelts; ++i)
11814 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
11815 return elts.build ();
11819 return NULL_TREE;
11821 default:
11822 return NULL_TREE;
11823 } /* switch (code) */
11826 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11827 of an array (or vector). */
11829 tree
11830 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11832 tree index_type = NULL_TREE;
11833 offset_int low_bound = 0;
11835 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11837 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11838 if (domain_type && TYPE_MIN_VALUE (domain_type))
11840 /* Static constructors for variably sized objects makes no sense. */
11841 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11842 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11843 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11847 if (index_type)
11848 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11849 TYPE_SIGN (index_type));
11851 offset_int index = low_bound - 1;
11852 if (index_type)
11853 index = wi::ext (index, TYPE_PRECISION (index_type),
11854 TYPE_SIGN (index_type));
11856 offset_int max_index;
11857 unsigned HOST_WIDE_INT cnt;
11858 tree cfield, cval;
11860 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11862 /* Array constructor might explicitly set index, or specify a range,
11863 or leave index NULL meaning that it is next index after previous
11864 one. */
11865 if (cfield)
11867 if (TREE_CODE (cfield) == INTEGER_CST)
11868 max_index = index = wi::to_offset (cfield);
11869 else
11871 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11872 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11873 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11876 else
11878 index += 1;
11879 if (index_type)
11880 index = wi::ext (index, TYPE_PRECISION (index_type),
11881 TYPE_SIGN (index_type));
11882 max_index = index;
11885 /* Do we have match? */
11886 if (wi::cmpu (access_index, index) >= 0
11887 && wi::cmpu (access_index, max_index) <= 0)
11888 return cval;
11890 return NULL_TREE;
11893 /* Perform constant folding and related simplification of EXPR.
11894 The related simplifications include x*1 => x, x*0 => 0, etc.,
11895 and application of the associative law.
11896 NOP_EXPR conversions may be removed freely (as long as we
11897 are careful not to change the type of the overall expression).
11898 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11899 but we can constant-fold them if they have constant operands. */
11901 #ifdef ENABLE_FOLD_CHECKING
11902 # define fold(x) fold_1 (x)
11903 static tree fold_1 (tree);
11904 static
11905 #endif
11906 tree
11907 fold (tree expr)
11909 const tree t = expr;
11910 enum tree_code code = TREE_CODE (t);
11911 enum tree_code_class kind = TREE_CODE_CLASS (code);
11912 tree tem;
11913 location_t loc = EXPR_LOCATION (expr);
11915 /* Return right away if a constant. */
11916 if (kind == tcc_constant)
11917 return t;
11919 /* CALL_EXPR-like objects with variable numbers of operands are
11920 treated specially. */
11921 if (kind == tcc_vl_exp)
11923 if (code == CALL_EXPR)
11925 tem = fold_call_expr (loc, expr, false);
11926 return tem ? tem : expr;
11928 return expr;
11931 if (IS_EXPR_CODE_CLASS (kind))
11933 tree type = TREE_TYPE (t);
11934 tree op0, op1, op2;
11936 switch (TREE_CODE_LENGTH (code))
11938 case 1:
11939 op0 = TREE_OPERAND (t, 0);
11940 tem = fold_unary_loc (loc, code, type, op0);
11941 return tem ? tem : expr;
11942 case 2:
11943 op0 = TREE_OPERAND (t, 0);
11944 op1 = TREE_OPERAND (t, 1);
11945 tem = fold_binary_loc (loc, code, type, op0, op1);
11946 return tem ? tem : expr;
11947 case 3:
11948 op0 = TREE_OPERAND (t, 0);
11949 op1 = TREE_OPERAND (t, 1);
11950 op2 = TREE_OPERAND (t, 2);
11951 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11952 return tem ? tem : expr;
11953 default:
11954 break;
11958 switch (code)
11960 case ARRAY_REF:
11962 tree op0 = TREE_OPERAND (t, 0);
11963 tree op1 = TREE_OPERAND (t, 1);
11965 if (TREE_CODE (op1) == INTEGER_CST
11966 && TREE_CODE (op0) == CONSTRUCTOR
11967 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11969 tree val = get_array_ctor_element_at_index (op0,
11970 wi::to_offset (op1));
11971 if (val)
11972 return val;
11975 return t;
11978 /* Return a VECTOR_CST if possible. */
11979 case CONSTRUCTOR:
11981 tree type = TREE_TYPE (t);
11982 if (TREE_CODE (type) != VECTOR_TYPE)
11983 return t;
11985 unsigned i;
11986 tree val;
11987 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
11988 if (! CONSTANT_CLASS_P (val))
11989 return t;
11991 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
11994 case CONST_DECL:
11995 return fold (DECL_INITIAL (t));
11997 default:
11998 return t;
11999 } /* switch (code) */
12002 #ifdef ENABLE_FOLD_CHECKING
12003 #undef fold
12005 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12006 hash_table<nofree_ptr_hash<const tree_node> > *);
12007 static void fold_check_failed (const_tree, const_tree);
12008 void print_fold_checksum (const_tree);
12010 /* When --enable-checking=fold, compute a digest of expr before
12011 and after actual fold call to see if fold did not accidentally
12012 change original expr. */
12014 tree
12015 fold (tree expr)
12017 tree ret;
12018 struct md5_ctx ctx;
12019 unsigned char checksum_before[16], checksum_after[16];
12020 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12022 md5_init_ctx (&ctx);
12023 fold_checksum_tree (expr, &ctx, &ht);
12024 md5_finish_ctx (&ctx, checksum_before);
12025 ht.empty ();
12027 ret = fold_1 (expr);
12029 md5_init_ctx (&ctx);
12030 fold_checksum_tree (expr, &ctx, &ht);
12031 md5_finish_ctx (&ctx, checksum_after);
12033 if (memcmp (checksum_before, checksum_after, 16))
12034 fold_check_failed (expr, ret);
12036 return ret;
12039 void
12040 print_fold_checksum (const_tree expr)
12042 struct md5_ctx ctx;
12043 unsigned char checksum[16], cnt;
12044 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12046 md5_init_ctx (&ctx);
12047 fold_checksum_tree (expr, &ctx, &ht);
12048 md5_finish_ctx (&ctx, checksum);
12049 for (cnt = 0; cnt < 16; ++cnt)
12050 fprintf (stderr, "%02x", checksum[cnt]);
12051 putc ('\n', stderr);
12054 static void
12055 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12057 internal_error ("fold check: original tree changed by fold");
12060 static void
12061 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12062 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12064 const tree_node **slot;
12065 enum tree_code code;
12066 union tree_node buf;
12067 int i, len;
12069 recursive_label:
12070 if (expr == NULL)
12071 return;
12072 slot = ht->find_slot (expr, INSERT);
12073 if (*slot != NULL)
12074 return;
12075 *slot = expr;
12076 code = TREE_CODE (expr);
12077 if (TREE_CODE_CLASS (code) == tcc_declaration
12078 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12080 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12081 memcpy ((char *) &buf, expr, tree_size (expr));
12082 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12083 buf.decl_with_vis.symtab_node = NULL;
12084 expr = (tree) &buf;
12086 else if (TREE_CODE_CLASS (code) == tcc_type
12087 && (TYPE_POINTER_TO (expr)
12088 || TYPE_REFERENCE_TO (expr)
12089 || TYPE_CACHED_VALUES_P (expr)
12090 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12091 || TYPE_NEXT_VARIANT (expr)
12092 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12094 /* Allow these fields to be modified. */
12095 tree tmp;
12096 memcpy ((char *) &buf, expr, tree_size (expr));
12097 expr = tmp = (tree) &buf;
12098 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12099 TYPE_POINTER_TO (tmp) = NULL;
12100 TYPE_REFERENCE_TO (tmp) = NULL;
12101 TYPE_NEXT_VARIANT (tmp) = NULL;
12102 TYPE_ALIAS_SET (tmp) = -1;
12103 if (TYPE_CACHED_VALUES_P (tmp))
12105 TYPE_CACHED_VALUES_P (tmp) = 0;
12106 TYPE_CACHED_VALUES (tmp) = NULL;
12109 md5_process_bytes (expr, tree_size (expr), ctx);
12110 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12111 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12112 if (TREE_CODE_CLASS (code) != tcc_type
12113 && TREE_CODE_CLASS (code) != tcc_declaration
12114 && code != TREE_LIST
12115 && code != SSA_NAME
12116 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12117 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12118 switch (TREE_CODE_CLASS (code))
12120 case tcc_constant:
12121 switch (code)
12123 case STRING_CST:
12124 md5_process_bytes (TREE_STRING_POINTER (expr),
12125 TREE_STRING_LENGTH (expr), ctx);
12126 break;
12127 case COMPLEX_CST:
12128 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12129 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12130 break;
12131 case VECTOR_CST:
12132 len = vector_cst_encoded_nelts (expr);
12133 for (i = 0; i < len; ++i)
12134 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
12135 break;
12136 default:
12137 break;
12139 break;
12140 case tcc_exceptional:
12141 switch (code)
12143 case TREE_LIST:
12144 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12145 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12146 expr = TREE_CHAIN (expr);
12147 goto recursive_label;
12148 break;
12149 case TREE_VEC:
12150 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12151 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12152 break;
12153 default:
12154 break;
12156 break;
12157 case tcc_expression:
12158 case tcc_reference:
12159 case tcc_comparison:
12160 case tcc_unary:
12161 case tcc_binary:
12162 case tcc_statement:
12163 case tcc_vl_exp:
12164 len = TREE_OPERAND_LENGTH (expr);
12165 for (i = 0; i < len; ++i)
12166 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12167 break;
12168 case tcc_declaration:
12169 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12170 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12171 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12173 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12174 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12175 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12176 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12177 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12180 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12182 if (TREE_CODE (expr) == FUNCTION_DECL)
12184 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12185 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12187 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12189 break;
12190 case tcc_type:
12191 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12192 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12193 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12194 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12195 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12196 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12197 if (INTEGRAL_TYPE_P (expr)
12198 || SCALAR_FLOAT_TYPE_P (expr))
12200 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12201 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12203 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12204 if (TREE_CODE (expr) == RECORD_TYPE
12205 || TREE_CODE (expr) == UNION_TYPE
12206 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12207 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12208 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12209 break;
12210 default:
12211 break;
12215 /* Helper function for outputting the checksum of a tree T. When
12216 debugging with gdb, you can "define mynext" to be "next" followed
12217 by "call debug_fold_checksum (op0)", then just trace down till the
12218 outputs differ. */
12220 DEBUG_FUNCTION void
12221 debug_fold_checksum (const_tree t)
12223 int i;
12224 unsigned char checksum[16];
12225 struct md5_ctx ctx;
12226 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12228 md5_init_ctx (&ctx);
12229 fold_checksum_tree (t, &ctx, &ht);
12230 md5_finish_ctx (&ctx, checksum);
12231 ht.empty ();
12233 for (i = 0; i < 16; i++)
12234 fprintf (stderr, "%d ", checksum[i]);
12236 fprintf (stderr, "\n");
12239 #endif
12241 /* Fold a unary tree expression with code CODE of type TYPE with an
12242 operand OP0. LOC is the location of the resulting expression.
12243 Return a folded expression if successful. Otherwise, return a tree
12244 expression with code CODE of type TYPE with an operand OP0. */
12246 tree
12247 fold_build1_loc (location_t loc,
12248 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12250 tree tem;
12251 #ifdef ENABLE_FOLD_CHECKING
12252 unsigned char checksum_before[16], checksum_after[16];
12253 struct md5_ctx ctx;
12254 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12256 md5_init_ctx (&ctx);
12257 fold_checksum_tree (op0, &ctx, &ht);
12258 md5_finish_ctx (&ctx, checksum_before);
12259 ht.empty ();
12260 #endif
12262 tem = fold_unary_loc (loc, code, type, op0);
12263 if (!tem)
12264 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12266 #ifdef ENABLE_FOLD_CHECKING
12267 md5_init_ctx (&ctx);
12268 fold_checksum_tree (op0, &ctx, &ht);
12269 md5_finish_ctx (&ctx, checksum_after);
12271 if (memcmp (checksum_before, checksum_after, 16))
12272 fold_check_failed (op0, tem);
12273 #endif
12274 return tem;
12277 /* Fold a binary tree expression with code CODE of type TYPE with
12278 operands OP0 and OP1. LOC is the location of the resulting
12279 expression. Return a folded expression if successful. Otherwise,
12280 return a tree expression with code CODE of type TYPE with operands
12281 OP0 and OP1. */
12283 tree
12284 fold_build2_loc (location_t loc,
12285 enum tree_code code, tree type, tree op0, tree op1
12286 MEM_STAT_DECL)
12288 tree tem;
12289 #ifdef ENABLE_FOLD_CHECKING
12290 unsigned char checksum_before_op0[16],
12291 checksum_before_op1[16],
12292 checksum_after_op0[16],
12293 checksum_after_op1[16];
12294 struct md5_ctx ctx;
12295 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12297 md5_init_ctx (&ctx);
12298 fold_checksum_tree (op0, &ctx, &ht);
12299 md5_finish_ctx (&ctx, checksum_before_op0);
12300 ht.empty ();
12302 md5_init_ctx (&ctx);
12303 fold_checksum_tree (op1, &ctx, &ht);
12304 md5_finish_ctx (&ctx, checksum_before_op1);
12305 ht.empty ();
12306 #endif
12308 tem = fold_binary_loc (loc, code, type, op0, op1);
12309 if (!tem)
12310 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12312 #ifdef ENABLE_FOLD_CHECKING
12313 md5_init_ctx (&ctx);
12314 fold_checksum_tree (op0, &ctx, &ht);
12315 md5_finish_ctx (&ctx, checksum_after_op0);
12316 ht.empty ();
12318 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12319 fold_check_failed (op0, tem);
12321 md5_init_ctx (&ctx);
12322 fold_checksum_tree (op1, &ctx, &ht);
12323 md5_finish_ctx (&ctx, checksum_after_op1);
12325 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12326 fold_check_failed (op1, tem);
12327 #endif
12328 return tem;
12331 /* Fold a ternary tree expression with code CODE of type TYPE with
12332 operands OP0, OP1, and OP2. Return a folded expression if
12333 successful. Otherwise, return a tree expression with code CODE of
12334 type TYPE with operands OP0, OP1, and OP2. */
12336 tree
12337 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12338 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12340 tree tem;
12341 #ifdef ENABLE_FOLD_CHECKING
12342 unsigned char checksum_before_op0[16],
12343 checksum_before_op1[16],
12344 checksum_before_op2[16],
12345 checksum_after_op0[16],
12346 checksum_after_op1[16],
12347 checksum_after_op2[16];
12348 struct md5_ctx ctx;
12349 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12351 md5_init_ctx (&ctx);
12352 fold_checksum_tree (op0, &ctx, &ht);
12353 md5_finish_ctx (&ctx, checksum_before_op0);
12354 ht.empty ();
12356 md5_init_ctx (&ctx);
12357 fold_checksum_tree (op1, &ctx, &ht);
12358 md5_finish_ctx (&ctx, checksum_before_op1);
12359 ht.empty ();
12361 md5_init_ctx (&ctx);
12362 fold_checksum_tree (op2, &ctx, &ht);
12363 md5_finish_ctx (&ctx, checksum_before_op2);
12364 ht.empty ();
12365 #endif
12367 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12368 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12369 if (!tem)
12370 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12372 #ifdef ENABLE_FOLD_CHECKING
12373 md5_init_ctx (&ctx);
12374 fold_checksum_tree (op0, &ctx, &ht);
12375 md5_finish_ctx (&ctx, checksum_after_op0);
12376 ht.empty ();
12378 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12379 fold_check_failed (op0, tem);
12381 md5_init_ctx (&ctx);
12382 fold_checksum_tree (op1, &ctx, &ht);
12383 md5_finish_ctx (&ctx, checksum_after_op1);
12384 ht.empty ();
12386 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12387 fold_check_failed (op1, tem);
12389 md5_init_ctx (&ctx);
12390 fold_checksum_tree (op2, &ctx, &ht);
12391 md5_finish_ctx (&ctx, checksum_after_op2);
12393 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12394 fold_check_failed (op2, tem);
12395 #endif
12396 return tem;
12399 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12400 arguments in ARGARRAY, and a null static chain.
12401 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12402 of type TYPE from the given operands as constructed by build_call_array. */
12404 tree
12405 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12406 int nargs, tree *argarray)
12408 tree tem;
12409 #ifdef ENABLE_FOLD_CHECKING
12410 unsigned char checksum_before_fn[16],
12411 checksum_before_arglist[16],
12412 checksum_after_fn[16],
12413 checksum_after_arglist[16];
12414 struct md5_ctx ctx;
12415 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12416 int i;
12418 md5_init_ctx (&ctx);
12419 fold_checksum_tree (fn, &ctx, &ht);
12420 md5_finish_ctx (&ctx, checksum_before_fn);
12421 ht.empty ();
12423 md5_init_ctx (&ctx);
12424 for (i = 0; i < nargs; i++)
12425 fold_checksum_tree (argarray[i], &ctx, &ht);
12426 md5_finish_ctx (&ctx, checksum_before_arglist);
12427 ht.empty ();
12428 #endif
12430 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12431 if (!tem)
12432 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12434 #ifdef ENABLE_FOLD_CHECKING
12435 md5_init_ctx (&ctx);
12436 fold_checksum_tree (fn, &ctx, &ht);
12437 md5_finish_ctx (&ctx, checksum_after_fn);
12438 ht.empty ();
12440 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12441 fold_check_failed (fn, tem);
12443 md5_init_ctx (&ctx);
12444 for (i = 0; i < nargs; i++)
12445 fold_checksum_tree (argarray[i], &ctx, &ht);
12446 md5_finish_ctx (&ctx, checksum_after_arglist);
12448 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12449 fold_check_failed (NULL_TREE, tem);
12450 #endif
12451 return tem;
12454 /* Perform constant folding and related simplification of initializer
12455 expression EXPR. These behave identically to "fold_buildN" but ignore
12456 potential run-time traps and exceptions that fold must preserve. */
12458 #define START_FOLD_INIT \
12459 int saved_signaling_nans = flag_signaling_nans;\
12460 int saved_trapping_math = flag_trapping_math;\
12461 int saved_rounding_math = flag_rounding_math;\
12462 int saved_trapv = flag_trapv;\
12463 int saved_folding_initializer = folding_initializer;\
12464 flag_signaling_nans = 0;\
12465 flag_trapping_math = 0;\
12466 flag_rounding_math = 0;\
12467 flag_trapv = 0;\
12468 folding_initializer = 1;
12470 #define END_FOLD_INIT \
12471 flag_signaling_nans = saved_signaling_nans;\
12472 flag_trapping_math = saved_trapping_math;\
12473 flag_rounding_math = saved_rounding_math;\
12474 flag_trapv = saved_trapv;\
12475 folding_initializer = saved_folding_initializer;
12477 tree
12478 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12479 tree type, tree op)
12481 tree result;
12482 START_FOLD_INIT;
12484 result = fold_build1_loc (loc, code, type, op);
12486 END_FOLD_INIT;
12487 return result;
12490 tree
12491 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12492 tree type, tree op0, tree op1)
12494 tree result;
12495 START_FOLD_INIT;
12497 result = fold_build2_loc (loc, code, type, op0, op1);
12499 END_FOLD_INIT;
12500 return result;
12503 tree
12504 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12505 int nargs, tree *argarray)
12507 tree result;
12508 START_FOLD_INIT;
12510 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12512 END_FOLD_INIT;
12513 return result;
12516 #undef START_FOLD_INIT
12517 #undef END_FOLD_INIT
12519 /* Determine if first argument is a multiple of second argument. Return 0 if
12520 it is not, or we cannot easily determined it to be.
12522 An example of the sort of thing we care about (at this point; this routine
12523 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12524 fold cases do now) is discovering that
12526 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12528 is a multiple of
12530 SAVE_EXPR (J * 8)
12532 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12534 This code also handles discovering that
12536 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12538 is a multiple of 8 so we don't have to worry about dealing with a
12539 possible remainder.
12541 Note that we *look* inside a SAVE_EXPR only to determine how it was
12542 calculated; it is not safe for fold to do much of anything else with the
12543 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12544 at run time. For example, the latter example above *cannot* be implemented
12545 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12546 evaluation time of the original SAVE_EXPR is not necessarily the same at
12547 the time the new expression is evaluated. The only optimization of this
12548 sort that would be valid is changing
12550 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12552 divided by 8 to
12554 SAVE_EXPR (I) * SAVE_EXPR (J)
12556 (where the same SAVE_EXPR (J) is used in the original and the
12557 transformed version). */
12560 multiple_of_p (tree type, const_tree top, const_tree bottom)
12562 gimple *stmt;
12563 tree t1, op1, op2;
12565 if (operand_equal_p (top, bottom, 0))
12566 return 1;
12568 if (TREE_CODE (type) != INTEGER_TYPE)
12569 return 0;
12571 switch (TREE_CODE (top))
12573 case BIT_AND_EXPR:
12574 /* Bitwise and provides a power of two multiple. If the mask is
12575 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12576 if (!integer_pow2p (bottom))
12577 return 0;
12578 /* FALLTHRU */
12580 case MULT_EXPR:
12581 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12582 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12584 case MINUS_EXPR:
12585 /* It is impossible to prove if op0 - op1 is multiple of bottom
12586 precisely, so be conservative here checking if both op0 and op1
12587 are multiple of bottom. Note we check the second operand first
12588 since it's usually simpler. */
12589 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12590 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12592 case PLUS_EXPR:
12593 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12594 as op0 - 3 if the expression has unsigned type. For example,
12595 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12596 op1 = TREE_OPERAND (top, 1);
12597 if (TYPE_UNSIGNED (type)
12598 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12599 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12600 return (multiple_of_p (type, op1, bottom)
12601 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12603 case LSHIFT_EXPR:
12604 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12606 op1 = TREE_OPERAND (top, 1);
12607 /* const_binop may not detect overflow correctly,
12608 so check for it explicitly here. */
12609 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
12610 wi::to_wide (op1))
12611 && (t1 = fold_convert (type,
12612 const_binop (LSHIFT_EXPR, size_one_node,
12613 op1))) != 0
12614 && !TREE_OVERFLOW (t1))
12615 return multiple_of_p (type, t1, bottom);
12617 return 0;
12619 case NOP_EXPR:
12620 /* Can't handle conversions from non-integral or wider integral type. */
12621 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12622 || (TYPE_PRECISION (type)
12623 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12624 return 0;
12626 /* fall through */
12628 case SAVE_EXPR:
12629 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12631 case COND_EXPR:
12632 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12633 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12635 case INTEGER_CST:
12636 if (TREE_CODE (bottom) != INTEGER_CST
12637 || integer_zerop (bottom)
12638 || (TYPE_UNSIGNED (type)
12639 && (tree_int_cst_sgn (top) < 0
12640 || tree_int_cst_sgn (bottom) < 0)))
12641 return 0;
12642 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12643 SIGNED);
12645 case SSA_NAME:
12646 if (TREE_CODE (bottom) == INTEGER_CST
12647 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12648 && gimple_code (stmt) == GIMPLE_ASSIGN)
12650 enum tree_code code = gimple_assign_rhs_code (stmt);
12652 /* Check for special cases to see if top is defined as multiple
12653 of bottom:
12655 top = (X & ~(bottom - 1) ; bottom is power of 2
12659 Y = X % bottom
12660 top = X - Y. */
12661 if (code == BIT_AND_EXPR
12662 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12663 && TREE_CODE (op2) == INTEGER_CST
12664 && integer_pow2p (bottom)
12665 && wi::multiple_of_p (wi::to_widest (op2),
12666 wi::to_widest (bottom), UNSIGNED))
12667 return 1;
12669 op1 = gimple_assign_rhs1 (stmt);
12670 if (code == MINUS_EXPR
12671 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12672 && TREE_CODE (op2) == SSA_NAME
12673 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12674 && gimple_code (stmt) == GIMPLE_ASSIGN
12675 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12676 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12677 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12678 return 1;
12681 /* fall through */
12683 default:
12684 if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
12685 return multiple_p (wi::to_poly_widest (top),
12686 wi::to_poly_widest (bottom));
12688 return 0;
12692 #define tree_expr_nonnegative_warnv_p(X, Y) \
12693 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12695 #define RECURSE(X) \
12696 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12698 /* Return true if CODE or TYPE is known to be non-negative. */
12700 static bool
12701 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12703 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12704 && truth_value_p (code))
12705 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12706 have a signed:1 type (where the value is -1 and 0). */
12707 return true;
12708 return false;
12711 /* Return true if (CODE OP0) is known to be non-negative. If the return
12712 value is based on the assumption that signed overflow is undefined,
12713 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12714 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12716 bool
12717 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12718 bool *strict_overflow_p, int depth)
12720 if (TYPE_UNSIGNED (type))
12721 return true;
12723 switch (code)
12725 case ABS_EXPR:
12726 /* We can't return 1 if flag_wrapv is set because
12727 ABS_EXPR<INT_MIN> = INT_MIN. */
12728 if (!ANY_INTEGRAL_TYPE_P (type))
12729 return true;
12730 if (TYPE_OVERFLOW_UNDEFINED (type))
12732 *strict_overflow_p = true;
12733 return true;
12735 break;
12737 case NON_LVALUE_EXPR:
12738 case FLOAT_EXPR:
12739 case FIX_TRUNC_EXPR:
12740 return RECURSE (op0);
12742 CASE_CONVERT:
12744 tree inner_type = TREE_TYPE (op0);
12745 tree outer_type = type;
12747 if (TREE_CODE (outer_type) == REAL_TYPE)
12749 if (TREE_CODE (inner_type) == REAL_TYPE)
12750 return RECURSE (op0);
12751 if (INTEGRAL_TYPE_P (inner_type))
12753 if (TYPE_UNSIGNED (inner_type))
12754 return true;
12755 return RECURSE (op0);
12758 else if (INTEGRAL_TYPE_P (outer_type))
12760 if (TREE_CODE (inner_type) == REAL_TYPE)
12761 return RECURSE (op0);
12762 if (INTEGRAL_TYPE_P (inner_type))
12763 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12764 && TYPE_UNSIGNED (inner_type);
12767 break;
12769 default:
12770 return tree_simple_nonnegative_warnv_p (code, type);
12773 /* We don't know sign of `t', so be conservative and return false. */
12774 return false;
12777 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12778 value is based on the assumption that signed overflow is undefined,
12779 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12780 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12782 bool
12783 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12784 tree op1, bool *strict_overflow_p,
12785 int depth)
12787 if (TYPE_UNSIGNED (type))
12788 return true;
12790 switch (code)
12792 case POINTER_PLUS_EXPR:
12793 case PLUS_EXPR:
12794 if (FLOAT_TYPE_P (type))
12795 return RECURSE (op0) && RECURSE (op1);
12797 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12798 both unsigned and at least 2 bits shorter than the result. */
12799 if (TREE_CODE (type) == INTEGER_TYPE
12800 && TREE_CODE (op0) == NOP_EXPR
12801 && TREE_CODE (op1) == NOP_EXPR)
12803 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12804 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12805 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12806 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12808 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12809 TYPE_PRECISION (inner2)) + 1;
12810 return prec < TYPE_PRECISION (type);
12813 break;
12815 case MULT_EXPR:
12816 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12818 /* x * x is always non-negative for floating point x
12819 or without overflow. */
12820 if (operand_equal_p (op0, op1, 0)
12821 || (RECURSE (op0) && RECURSE (op1)))
12823 if (ANY_INTEGRAL_TYPE_P (type)
12824 && TYPE_OVERFLOW_UNDEFINED (type))
12825 *strict_overflow_p = true;
12826 return true;
12830 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12831 both unsigned and their total bits is shorter than the result. */
12832 if (TREE_CODE (type) == INTEGER_TYPE
12833 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12834 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12836 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12837 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12838 : TREE_TYPE (op0);
12839 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12840 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12841 : TREE_TYPE (op1);
12843 bool unsigned0 = TYPE_UNSIGNED (inner0);
12844 bool unsigned1 = TYPE_UNSIGNED (inner1);
12846 if (TREE_CODE (op0) == INTEGER_CST)
12847 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12849 if (TREE_CODE (op1) == INTEGER_CST)
12850 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12852 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12853 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12855 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12856 ? tree_int_cst_min_precision (op0, UNSIGNED)
12857 : TYPE_PRECISION (inner0);
12859 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12860 ? tree_int_cst_min_precision (op1, UNSIGNED)
12861 : TYPE_PRECISION (inner1);
12863 return precision0 + precision1 < TYPE_PRECISION (type);
12866 return false;
12868 case BIT_AND_EXPR:
12869 case MAX_EXPR:
12870 return RECURSE (op0) || RECURSE (op1);
12872 case BIT_IOR_EXPR:
12873 case BIT_XOR_EXPR:
12874 case MIN_EXPR:
12875 case RDIV_EXPR:
12876 case TRUNC_DIV_EXPR:
12877 case CEIL_DIV_EXPR:
12878 case FLOOR_DIV_EXPR:
12879 case ROUND_DIV_EXPR:
12880 return RECURSE (op0) && RECURSE (op1);
12882 case TRUNC_MOD_EXPR:
12883 return RECURSE (op0);
12885 case FLOOR_MOD_EXPR:
12886 return RECURSE (op1);
12888 case CEIL_MOD_EXPR:
12889 case ROUND_MOD_EXPR:
12890 default:
12891 return tree_simple_nonnegative_warnv_p (code, type);
12894 /* We don't know sign of `t', so be conservative and return false. */
12895 return false;
12898 /* Return true if T is known to be non-negative. If the return
12899 value is based on the assumption that signed overflow is undefined,
12900 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12901 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12903 bool
12904 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12906 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12907 return true;
12909 switch (TREE_CODE (t))
12911 case INTEGER_CST:
12912 return tree_int_cst_sgn (t) >= 0;
12914 case REAL_CST:
12915 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12917 case FIXED_CST:
12918 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12920 case COND_EXPR:
12921 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12923 case SSA_NAME:
12924 /* Limit the depth of recursion to avoid quadratic behavior.
12925 This is expected to catch almost all occurrences in practice.
12926 If this code misses important cases that unbounded recursion
12927 would not, passes that need this information could be revised
12928 to provide it through dataflow propagation. */
12929 return (!name_registered_for_update_p (t)
12930 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12931 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12932 strict_overflow_p, depth));
12934 default:
12935 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12939 /* Return true if T is known to be non-negative. If the return
12940 value is based on the assumption that signed overflow is undefined,
12941 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12942 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12944 bool
12945 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12946 bool *strict_overflow_p, int depth)
12948 switch (fn)
12950 CASE_CFN_ACOS:
12951 CASE_CFN_ACOSH:
12952 CASE_CFN_CABS:
12953 CASE_CFN_COSH:
12954 CASE_CFN_ERFC:
12955 CASE_CFN_EXP:
12956 CASE_CFN_EXP10:
12957 CASE_CFN_EXP2:
12958 CASE_CFN_FABS:
12959 CASE_CFN_FDIM:
12960 CASE_CFN_HYPOT:
12961 CASE_CFN_POW10:
12962 CASE_CFN_FFS:
12963 CASE_CFN_PARITY:
12964 CASE_CFN_POPCOUNT:
12965 CASE_CFN_CLZ:
12966 CASE_CFN_CLRSB:
12967 case CFN_BUILT_IN_BSWAP32:
12968 case CFN_BUILT_IN_BSWAP64:
12969 /* Always true. */
12970 return true;
12972 CASE_CFN_SQRT:
12973 CASE_CFN_SQRT_FN:
12974 /* sqrt(-0.0) is -0.0. */
12975 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
12976 return true;
12977 return RECURSE (arg0);
12979 CASE_CFN_ASINH:
12980 CASE_CFN_ATAN:
12981 CASE_CFN_ATANH:
12982 CASE_CFN_CBRT:
12983 CASE_CFN_CEIL:
12984 CASE_CFN_CEIL_FN:
12985 CASE_CFN_ERF:
12986 CASE_CFN_EXPM1:
12987 CASE_CFN_FLOOR:
12988 CASE_CFN_FLOOR_FN:
12989 CASE_CFN_FMOD:
12990 CASE_CFN_FREXP:
12991 CASE_CFN_ICEIL:
12992 CASE_CFN_IFLOOR:
12993 CASE_CFN_IRINT:
12994 CASE_CFN_IROUND:
12995 CASE_CFN_LCEIL:
12996 CASE_CFN_LDEXP:
12997 CASE_CFN_LFLOOR:
12998 CASE_CFN_LLCEIL:
12999 CASE_CFN_LLFLOOR:
13000 CASE_CFN_LLRINT:
13001 CASE_CFN_LLROUND:
13002 CASE_CFN_LRINT:
13003 CASE_CFN_LROUND:
13004 CASE_CFN_MODF:
13005 CASE_CFN_NEARBYINT:
13006 CASE_CFN_NEARBYINT_FN:
13007 CASE_CFN_RINT:
13008 CASE_CFN_RINT_FN:
13009 CASE_CFN_ROUND:
13010 CASE_CFN_ROUND_FN:
13011 CASE_CFN_SCALB:
13012 CASE_CFN_SCALBLN:
13013 CASE_CFN_SCALBN:
13014 CASE_CFN_SIGNBIT:
13015 CASE_CFN_SIGNIFICAND:
13016 CASE_CFN_SINH:
13017 CASE_CFN_TANH:
13018 CASE_CFN_TRUNC:
13019 CASE_CFN_TRUNC_FN:
13020 /* True if the 1st argument is nonnegative. */
13021 return RECURSE (arg0);
13023 CASE_CFN_FMAX:
13024 CASE_CFN_FMAX_FN:
13025 /* True if the 1st OR 2nd arguments are nonnegative. */
13026 return RECURSE (arg0) || RECURSE (arg1);
13028 CASE_CFN_FMIN:
13029 CASE_CFN_FMIN_FN:
13030 /* True if the 1st AND 2nd arguments are nonnegative. */
13031 return RECURSE (arg0) && RECURSE (arg1);
13033 CASE_CFN_COPYSIGN:
13034 CASE_CFN_COPYSIGN_FN:
13035 /* True if the 2nd argument is nonnegative. */
13036 return RECURSE (arg1);
13038 CASE_CFN_POWI:
13039 /* True if the 1st argument is nonnegative or the second
13040 argument is an even integer. */
13041 if (TREE_CODE (arg1) == INTEGER_CST
13042 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13043 return true;
13044 return RECURSE (arg0);
13046 CASE_CFN_POW:
13047 /* True if the 1st argument is nonnegative or the second
13048 argument is an even integer valued real. */
13049 if (TREE_CODE (arg1) == REAL_CST)
13051 REAL_VALUE_TYPE c;
13052 HOST_WIDE_INT n;
13054 c = TREE_REAL_CST (arg1);
13055 n = real_to_integer (&c);
13056 if ((n & 1) == 0)
13058 REAL_VALUE_TYPE cint;
13059 real_from_integer (&cint, VOIDmode, n, SIGNED);
13060 if (real_identical (&c, &cint))
13061 return true;
13064 return RECURSE (arg0);
13066 default:
13067 break;
13069 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13072 /* Return true if T is known to be non-negative. If the return
13073 value is based on the assumption that signed overflow is undefined,
13074 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13075 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13077 static bool
13078 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13080 enum tree_code code = TREE_CODE (t);
13081 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13082 return true;
13084 switch (code)
13086 case TARGET_EXPR:
13088 tree temp = TARGET_EXPR_SLOT (t);
13089 t = TARGET_EXPR_INITIAL (t);
13091 /* If the initializer is non-void, then it's a normal expression
13092 that will be assigned to the slot. */
13093 if (!VOID_TYPE_P (t))
13094 return RECURSE (t);
13096 /* Otherwise, the initializer sets the slot in some way. One common
13097 way is an assignment statement at the end of the initializer. */
13098 while (1)
13100 if (TREE_CODE (t) == BIND_EXPR)
13101 t = expr_last (BIND_EXPR_BODY (t));
13102 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13103 || TREE_CODE (t) == TRY_CATCH_EXPR)
13104 t = expr_last (TREE_OPERAND (t, 0));
13105 else if (TREE_CODE (t) == STATEMENT_LIST)
13106 t = expr_last (t);
13107 else
13108 break;
13110 if (TREE_CODE (t) == MODIFY_EXPR
13111 && TREE_OPERAND (t, 0) == temp)
13112 return RECURSE (TREE_OPERAND (t, 1));
13114 return false;
13117 case CALL_EXPR:
13119 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13120 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13122 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13123 get_call_combined_fn (t),
13124 arg0,
13125 arg1,
13126 strict_overflow_p, depth);
13128 case COMPOUND_EXPR:
13129 case MODIFY_EXPR:
13130 return RECURSE (TREE_OPERAND (t, 1));
13132 case BIND_EXPR:
13133 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13135 case SAVE_EXPR:
13136 return RECURSE (TREE_OPERAND (t, 0));
13138 default:
13139 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13143 #undef RECURSE
13144 #undef tree_expr_nonnegative_warnv_p
13146 /* Return true if T is known to be non-negative. If the return
13147 value is based on the assumption that signed overflow is undefined,
13148 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13149 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13151 bool
13152 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13154 enum tree_code code;
13155 if (t == error_mark_node)
13156 return false;
13158 code = TREE_CODE (t);
13159 switch (TREE_CODE_CLASS (code))
13161 case tcc_binary:
13162 case tcc_comparison:
13163 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13164 TREE_TYPE (t),
13165 TREE_OPERAND (t, 0),
13166 TREE_OPERAND (t, 1),
13167 strict_overflow_p, depth);
13169 case tcc_unary:
13170 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13171 TREE_TYPE (t),
13172 TREE_OPERAND (t, 0),
13173 strict_overflow_p, depth);
13175 case tcc_constant:
13176 case tcc_declaration:
13177 case tcc_reference:
13178 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13180 default:
13181 break;
13184 switch (code)
13186 case TRUTH_AND_EXPR:
13187 case TRUTH_OR_EXPR:
13188 case TRUTH_XOR_EXPR:
13189 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13190 TREE_TYPE (t),
13191 TREE_OPERAND (t, 0),
13192 TREE_OPERAND (t, 1),
13193 strict_overflow_p, depth);
13194 case TRUTH_NOT_EXPR:
13195 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13196 TREE_TYPE (t),
13197 TREE_OPERAND (t, 0),
13198 strict_overflow_p, depth);
13200 case COND_EXPR:
13201 case CONSTRUCTOR:
13202 case OBJ_TYPE_REF:
13203 case ASSERT_EXPR:
13204 case ADDR_EXPR:
13205 case WITH_SIZE_EXPR:
13206 case SSA_NAME:
13207 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13209 default:
13210 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13214 /* Return true if `t' is known to be non-negative. Handle warnings
13215 about undefined signed overflow. */
13217 bool
13218 tree_expr_nonnegative_p (tree t)
13220 bool ret, strict_overflow_p;
13222 strict_overflow_p = false;
13223 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13224 if (strict_overflow_p)
13225 fold_overflow_warning (("assuming signed overflow does not occur when "
13226 "determining that expression is always "
13227 "non-negative"),
13228 WARN_STRICT_OVERFLOW_MISC);
13229 return ret;
13233 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13234 For floating point we further ensure that T is not denormal.
13235 Similar logic is present in nonzero_address in rtlanal.h.
13237 If the return value is based on the assumption that signed overflow
13238 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13239 change *STRICT_OVERFLOW_P. */
13241 bool
13242 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13243 bool *strict_overflow_p)
13245 switch (code)
13247 case ABS_EXPR:
13248 return tree_expr_nonzero_warnv_p (op0,
13249 strict_overflow_p);
13251 case NOP_EXPR:
13253 tree inner_type = TREE_TYPE (op0);
13254 tree outer_type = type;
13256 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13257 && tree_expr_nonzero_warnv_p (op0,
13258 strict_overflow_p));
13260 break;
13262 case NON_LVALUE_EXPR:
13263 return tree_expr_nonzero_warnv_p (op0,
13264 strict_overflow_p);
13266 default:
13267 break;
13270 return false;
13273 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13274 For floating point we further ensure that T is not denormal.
13275 Similar logic is present in nonzero_address in rtlanal.h.
13277 If the return value is based on the assumption that signed overflow
13278 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13279 change *STRICT_OVERFLOW_P. */
13281 bool
13282 tree_binary_nonzero_warnv_p (enum tree_code code,
13283 tree type,
13284 tree op0,
13285 tree op1, bool *strict_overflow_p)
13287 bool sub_strict_overflow_p;
13288 switch (code)
13290 case POINTER_PLUS_EXPR:
13291 case PLUS_EXPR:
13292 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13294 /* With the presence of negative values it is hard
13295 to say something. */
13296 sub_strict_overflow_p = false;
13297 if (!tree_expr_nonnegative_warnv_p (op0,
13298 &sub_strict_overflow_p)
13299 || !tree_expr_nonnegative_warnv_p (op1,
13300 &sub_strict_overflow_p))
13301 return false;
13302 /* One of operands must be positive and the other non-negative. */
13303 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13304 overflows, on a twos-complement machine the sum of two
13305 nonnegative numbers can never be zero. */
13306 return (tree_expr_nonzero_warnv_p (op0,
13307 strict_overflow_p)
13308 || tree_expr_nonzero_warnv_p (op1,
13309 strict_overflow_p));
13311 break;
13313 case MULT_EXPR:
13314 if (TYPE_OVERFLOW_UNDEFINED (type))
13316 if (tree_expr_nonzero_warnv_p (op0,
13317 strict_overflow_p)
13318 && tree_expr_nonzero_warnv_p (op1,
13319 strict_overflow_p))
13321 *strict_overflow_p = true;
13322 return true;
13325 break;
13327 case MIN_EXPR:
13328 sub_strict_overflow_p = false;
13329 if (tree_expr_nonzero_warnv_p (op0,
13330 &sub_strict_overflow_p)
13331 && tree_expr_nonzero_warnv_p (op1,
13332 &sub_strict_overflow_p))
13334 if (sub_strict_overflow_p)
13335 *strict_overflow_p = true;
13337 break;
13339 case MAX_EXPR:
13340 sub_strict_overflow_p = false;
13341 if (tree_expr_nonzero_warnv_p (op0,
13342 &sub_strict_overflow_p))
13344 if (sub_strict_overflow_p)
13345 *strict_overflow_p = true;
13347 /* When both operands are nonzero, then MAX must be too. */
13348 if (tree_expr_nonzero_warnv_p (op1,
13349 strict_overflow_p))
13350 return true;
13352 /* MAX where operand 0 is positive is positive. */
13353 return tree_expr_nonnegative_warnv_p (op0,
13354 strict_overflow_p);
13356 /* MAX where operand 1 is positive is positive. */
13357 else if (tree_expr_nonzero_warnv_p (op1,
13358 &sub_strict_overflow_p)
13359 && tree_expr_nonnegative_warnv_p (op1,
13360 &sub_strict_overflow_p))
13362 if (sub_strict_overflow_p)
13363 *strict_overflow_p = true;
13364 return true;
13366 break;
13368 case BIT_IOR_EXPR:
13369 return (tree_expr_nonzero_warnv_p (op1,
13370 strict_overflow_p)
13371 || tree_expr_nonzero_warnv_p (op0,
13372 strict_overflow_p));
13374 default:
13375 break;
13378 return false;
13381 /* Return true when T is an address and is known to be nonzero.
13382 For floating point we further ensure that T is not denormal.
13383 Similar logic is present in nonzero_address in rtlanal.h.
13385 If the return value is based on the assumption that signed overflow
13386 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13387 change *STRICT_OVERFLOW_P. */
13389 bool
13390 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13392 bool sub_strict_overflow_p;
13393 switch (TREE_CODE (t))
13395 case INTEGER_CST:
13396 return !integer_zerop (t);
13398 case ADDR_EXPR:
13400 tree base = TREE_OPERAND (t, 0);
13402 if (!DECL_P (base))
13403 base = get_base_address (base);
13405 if (base && TREE_CODE (base) == TARGET_EXPR)
13406 base = TARGET_EXPR_SLOT (base);
13408 if (!base)
13409 return false;
13411 /* For objects in symbol table check if we know they are non-zero.
13412 Don't do anything for variables and functions before symtab is built;
13413 it is quite possible that they will be declared weak later. */
13414 int nonzero_addr = maybe_nonzero_address (base);
13415 if (nonzero_addr >= 0)
13416 return nonzero_addr;
13418 /* Constants are never weak. */
13419 if (CONSTANT_CLASS_P (base))
13420 return true;
13422 return false;
13425 case COND_EXPR:
13426 sub_strict_overflow_p = false;
13427 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13428 &sub_strict_overflow_p)
13429 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13430 &sub_strict_overflow_p))
13432 if (sub_strict_overflow_p)
13433 *strict_overflow_p = true;
13434 return true;
13436 break;
13438 case SSA_NAME:
13439 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13440 break;
13441 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13443 default:
13444 break;
13446 return false;
13449 #define integer_valued_real_p(X) \
13450 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13452 #define RECURSE(X) \
13453 ((integer_valued_real_p) (X, depth + 1))
13455 /* Return true if the floating point result of (CODE OP0) has an
13456 integer value. We also allow +Inf, -Inf and NaN to be considered
13457 integer values. Return false for signaling NaN.
13459 DEPTH is the current nesting depth of the query. */
13461 bool
13462 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13464 switch (code)
13466 case FLOAT_EXPR:
13467 return true;
13469 case ABS_EXPR:
13470 return RECURSE (op0);
13472 CASE_CONVERT:
13474 tree type = TREE_TYPE (op0);
13475 if (TREE_CODE (type) == INTEGER_TYPE)
13476 return true;
13477 if (TREE_CODE (type) == REAL_TYPE)
13478 return RECURSE (op0);
13479 break;
13482 default:
13483 break;
13485 return false;
13488 /* Return true if the floating point result of (CODE OP0 OP1) has an
13489 integer value. We also allow +Inf, -Inf and NaN to be considered
13490 integer values. Return false for signaling NaN.
13492 DEPTH is the current nesting depth of the query. */
13494 bool
13495 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13497 switch (code)
13499 case PLUS_EXPR:
13500 case MINUS_EXPR:
13501 case MULT_EXPR:
13502 case MIN_EXPR:
13503 case MAX_EXPR:
13504 return RECURSE (op0) && RECURSE (op1);
13506 default:
13507 break;
13509 return false;
13512 /* Return true if the floating point result of calling FNDECL with arguments
13513 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13514 considered integer values. Return false for signaling NaN. If FNDECL
13515 takes fewer than 2 arguments, the remaining ARGn are null.
13517 DEPTH is the current nesting depth of the query. */
13519 bool
13520 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13522 switch (fn)
13524 CASE_CFN_CEIL:
13525 CASE_CFN_CEIL_FN:
13526 CASE_CFN_FLOOR:
13527 CASE_CFN_FLOOR_FN:
13528 CASE_CFN_NEARBYINT:
13529 CASE_CFN_NEARBYINT_FN:
13530 CASE_CFN_RINT:
13531 CASE_CFN_RINT_FN:
13532 CASE_CFN_ROUND:
13533 CASE_CFN_ROUND_FN:
13534 CASE_CFN_TRUNC:
13535 CASE_CFN_TRUNC_FN:
13536 return true;
13538 CASE_CFN_FMIN:
13539 CASE_CFN_FMIN_FN:
13540 CASE_CFN_FMAX:
13541 CASE_CFN_FMAX_FN:
13542 return RECURSE (arg0) && RECURSE (arg1);
13544 default:
13545 break;
13547 return false;
13550 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13551 has an integer value. We also allow +Inf, -Inf and NaN to be
13552 considered integer values. Return false for signaling NaN.
13554 DEPTH is the current nesting depth of the query. */
13556 bool
13557 integer_valued_real_single_p (tree t, int depth)
13559 switch (TREE_CODE (t))
13561 case REAL_CST:
13562 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13564 case COND_EXPR:
13565 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13567 case SSA_NAME:
13568 /* Limit the depth of recursion to avoid quadratic behavior.
13569 This is expected to catch almost all occurrences in practice.
13570 If this code misses important cases that unbounded recursion
13571 would not, passes that need this information could be revised
13572 to provide it through dataflow propagation. */
13573 return (!name_registered_for_update_p (t)
13574 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13575 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13576 depth));
13578 default:
13579 break;
13581 return false;
13584 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13585 has an integer value. We also allow +Inf, -Inf and NaN to be
13586 considered integer values. Return false for signaling NaN.
13588 DEPTH is the current nesting depth of the query. */
13590 static bool
13591 integer_valued_real_invalid_p (tree t, int depth)
13593 switch (TREE_CODE (t))
13595 case COMPOUND_EXPR:
13596 case MODIFY_EXPR:
13597 case BIND_EXPR:
13598 return RECURSE (TREE_OPERAND (t, 1));
13600 case SAVE_EXPR:
13601 return RECURSE (TREE_OPERAND (t, 0));
13603 default:
13604 break;
13606 return false;
13609 #undef RECURSE
13610 #undef integer_valued_real_p
13612 /* Return true if the floating point expression T has an integer value.
13613 We also allow +Inf, -Inf and NaN to be considered integer values.
13614 Return false for signaling NaN.
13616 DEPTH is the current nesting depth of the query. */
13618 bool
13619 integer_valued_real_p (tree t, int depth)
13621 if (t == error_mark_node)
13622 return false;
13624 tree_code code = TREE_CODE (t);
13625 switch (TREE_CODE_CLASS (code))
13627 case tcc_binary:
13628 case tcc_comparison:
13629 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13630 TREE_OPERAND (t, 1), depth);
13632 case tcc_unary:
13633 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13635 case tcc_constant:
13636 case tcc_declaration:
13637 case tcc_reference:
13638 return integer_valued_real_single_p (t, depth);
13640 default:
13641 break;
13644 switch (code)
13646 case COND_EXPR:
13647 case SSA_NAME:
13648 return integer_valued_real_single_p (t, depth);
13650 case CALL_EXPR:
13652 tree arg0 = (call_expr_nargs (t) > 0
13653 ? CALL_EXPR_ARG (t, 0)
13654 : NULL_TREE);
13655 tree arg1 = (call_expr_nargs (t) > 1
13656 ? CALL_EXPR_ARG (t, 1)
13657 : NULL_TREE);
13658 return integer_valued_real_call_p (get_call_combined_fn (t),
13659 arg0, arg1, depth);
13662 default:
13663 return integer_valued_real_invalid_p (t, depth);
13667 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13668 attempt to fold the expression to a constant without modifying TYPE,
13669 OP0 or OP1.
13671 If the expression could be simplified to a constant, then return
13672 the constant. If the expression would not be simplified to a
13673 constant, then return NULL_TREE. */
13675 tree
13676 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13678 tree tem = fold_binary (code, type, op0, op1);
13679 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13682 /* Given the components of a unary expression CODE, TYPE and OP0,
13683 attempt to fold the expression to a constant without modifying
13684 TYPE or OP0.
13686 If the expression could be simplified to a constant, then return
13687 the constant. If the expression would not be simplified to a
13688 constant, then return NULL_TREE. */
13690 tree
13691 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13693 tree tem = fold_unary (code, type, op0);
13694 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13697 /* If EXP represents referencing an element in a constant string
13698 (either via pointer arithmetic or array indexing), return the
13699 tree representing the value accessed, otherwise return NULL. */
13701 tree
13702 fold_read_from_constant_string (tree exp)
13704 if ((TREE_CODE (exp) == INDIRECT_REF
13705 || TREE_CODE (exp) == ARRAY_REF)
13706 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13708 tree exp1 = TREE_OPERAND (exp, 0);
13709 tree index;
13710 tree string;
13711 location_t loc = EXPR_LOCATION (exp);
13713 if (TREE_CODE (exp) == INDIRECT_REF)
13714 string = string_constant (exp1, &index);
13715 else
13717 tree low_bound = array_ref_low_bound (exp);
13718 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13720 /* Optimize the special-case of a zero lower bound.
13722 We convert the low_bound to sizetype to avoid some problems
13723 with constant folding. (E.g. suppose the lower bound is 1,
13724 and its mode is QI. Without the conversion,l (ARRAY
13725 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13726 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13727 if (! integer_zerop (low_bound))
13728 index = size_diffop_loc (loc, index,
13729 fold_convert_loc (loc, sizetype, low_bound));
13731 string = exp1;
13734 scalar_int_mode char_mode;
13735 if (string
13736 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13737 && TREE_CODE (string) == STRING_CST
13738 && TREE_CODE (index) == INTEGER_CST
13739 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13740 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13741 &char_mode)
13742 && GET_MODE_SIZE (char_mode) == 1)
13743 return build_int_cst_type (TREE_TYPE (exp),
13744 (TREE_STRING_POINTER (string)
13745 [TREE_INT_CST_LOW (index)]));
13747 return NULL;
13750 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13751 an integer constant, real, or fixed-point constant.
13753 TYPE is the type of the result. */
13755 static tree
13756 fold_negate_const (tree arg0, tree type)
13758 tree t = NULL_TREE;
13760 switch (TREE_CODE (arg0))
13762 case REAL_CST:
13763 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13764 break;
13766 case FIXED_CST:
13768 FIXED_VALUE_TYPE f;
13769 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13770 &(TREE_FIXED_CST (arg0)), NULL,
13771 TYPE_SATURATING (type));
13772 t = build_fixed (type, f);
13773 /* Propagate overflow flags. */
13774 if (overflow_p | TREE_OVERFLOW (arg0))
13775 TREE_OVERFLOW (t) = 1;
13776 break;
13779 default:
13780 if (poly_int_tree_p (arg0))
13782 bool overflow;
13783 poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
13784 t = force_fit_type (type, res, 1,
13785 (overflow && ! TYPE_UNSIGNED (type))
13786 || TREE_OVERFLOW (arg0));
13787 break;
13790 gcc_unreachable ();
13793 return t;
13796 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13797 an integer constant or real constant.
13799 TYPE is the type of the result. */
13801 tree
13802 fold_abs_const (tree arg0, tree type)
13804 tree t = NULL_TREE;
13806 switch (TREE_CODE (arg0))
13808 case INTEGER_CST:
13810 /* If the value is unsigned or non-negative, then the absolute value
13811 is the same as the ordinary value. */
13812 if (!wi::neg_p (wi::to_wide (arg0), TYPE_SIGN (type)))
13813 t = arg0;
13815 /* If the value is negative, then the absolute value is
13816 its negation. */
13817 else
13819 bool overflow;
13820 wide_int val = wi::neg (wi::to_wide (arg0), &overflow);
13821 t = force_fit_type (type, val, -1,
13822 overflow | TREE_OVERFLOW (arg0));
13825 break;
13827 case REAL_CST:
13828 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13829 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13830 else
13831 t = arg0;
13832 break;
13834 default:
13835 gcc_unreachable ();
13838 return t;
13841 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13842 constant. TYPE is the type of the result. */
13844 static tree
13845 fold_not_const (const_tree arg0, tree type)
13847 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13849 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
13852 /* Given CODE, a relational operator, the target type, TYPE and two
13853 constant operands OP0 and OP1, return the result of the
13854 relational operation. If the result is not a compile time
13855 constant, then return NULL_TREE. */
13857 static tree
13858 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13860 int result, invert;
13862 /* From here on, the only cases we handle are when the result is
13863 known to be a constant. */
13865 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13867 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13868 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13870 /* Handle the cases where either operand is a NaN. */
13871 if (real_isnan (c0) || real_isnan (c1))
13873 switch (code)
13875 case EQ_EXPR:
13876 case ORDERED_EXPR:
13877 result = 0;
13878 break;
13880 case NE_EXPR:
13881 case UNORDERED_EXPR:
13882 case UNLT_EXPR:
13883 case UNLE_EXPR:
13884 case UNGT_EXPR:
13885 case UNGE_EXPR:
13886 case UNEQ_EXPR:
13887 result = 1;
13888 break;
13890 case LT_EXPR:
13891 case LE_EXPR:
13892 case GT_EXPR:
13893 case GE_EXPR:
13894 case LTGT_EXPR:
13895 if (flag_trapping_math)
13896 return NULL_TREE;
13897 result = 0;
13898 break;
13900 default:
13901 gcc_unreachable ();
13904 return constant_boolean_node (result, type);
13907 return constant_boolean_node (real_compare (code, c0, c1), type);
13910 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13912 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13913 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13914 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13917 /* Handle equality/inequality of complex constants. */
13918 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13920 tree rcond = fold_relational_const (code, type,
13921 TREE_REALPART (op0),
13922 TREE_REALPART (op1));
13923 tree icond = fold_relational_const (code, type,
13924 TREE_IMAGPART (op0),
13925 TREE_IMAGPART (op1));
13926 if (code == EQ_EXPR)
13927 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13928 else if (code == NE_EXPR)
13929 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13930 else
13931 return NULL_TREE;
13934 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13936 if (!VECTOR_TYPE_P (type))
13938 /* Have vector comparison with scalar boolean result. */
13939 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13940 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13941 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13943 tree elem0 = VECTOR_CST_ELT (op0, i);
13944 tree elem1 = VECTOR_CST_ELT (op1, i);
13945 tree tmp = fold_relational_const (code, type, elem0, elem1);
13946 if (tmp == NULL_TREE)
13947 return NULL_TREE;
13948 if (integer_zerop (tmp))
13949 return constant_boolean_node (false, type);
13951 return constant_boolean_node (true, type);
13953 tree_vector_builder elts;
13954 if (!elts.new_binary_operation (type, op0, op1, false))
13955 return NULL_TREE;
13956 unsigned int count = elts.encoded_nelts ();
13957 for (unsigned i = 0; i < count; i++)
13959 tree elem_type = TREE_TYPE (type);
13960 tree elem0 = VECTOR_CST_ELT (op0, i);
13961 tree elem1 = VECTOR_CST_ELT (op1, i);
13963 tree tem = fold_relational_const (code, elem_type,
13964 elem0, elem1);
13966 if (tem == NULL_TREE)
13967 return NULL_TREE;
13969 elts.quick_push (build_int_cst (elem_type,
13970 integer_zerop (tem) ? 0 : -1));
13973 return elts.build ();
13976 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13978 To compute GT, swap the arguments and do LT.
13979 To compute GE, do LT and invert the result.
13980 To compute LE, swap the arguments, do LT and invert the result.
13981 To compute NE, do EQ and invert the result.
13983 Therefore, the code below must handle only EQ and LT. */
13985 if (code == LE_EXPR || code == GT_EXPR)
13987 std::swap (op0, op1);
13988 code = swap_tree_comparison (code);
13991 /* Note that it is safe to invert for real values here because we
13992 have already handled the one case that it matters. */
13994 invert = 0;
13995 if (code == NE_EXPR || code == GE_EXPR)
13997 invert = 1;
13998 code = invert_tree_comparison (code, false);
14001 /* Compute a result for LT or EQ if args permit;
14002 Otherwise return T. */
14003 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14005 if (code == EQ_EXPR)
14006 result = tree_int_cst_equal (op0, op1);
14007 else
14008 result = tree_int_cst_lt (op0, op1);
14010 else
14011 return NULL_TREE;
14013 if (invert)
14014 result ^= 1;
14015 return constant_boolean_node (result, type);
14018 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14019 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14020 itself. */
14022 tree
14023 fold_build_cleanup_point_expr (tree type, tree expr)
14025 /* If the expression does not have side effects then we don't have to wrap
14026 it with a cleanup point expression. */
14027 if (!TREE_SIDE_EFFECTS (expr))
14028 return expr;
14030 /* If the expression is a return, check to see if the expression inside the
14031 return has no side effects or the right hand side of the modify expression
14032 inside the return. If either don't have side effects set we don't need to
14033 wrap the expression in a cleanup point expression. Note we don't check the
14034 left hand side of the modify because it should always be a return decl. */
14035 if (TREE_CODE (expr) == RETURN_EXPR)
14037 tree op = TREE_OPERAND (expr, 0);
14038 if (!op || !TREE_SIDE_EFFECTS (op))
14039 return expr;
14040 op = TREE_OPERAND (op, 1);
14041 if (!TREE_SIDE_EFFECTS (op))
14042 return expr;
14045 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
14048 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14049 of an indirection through OP0, or NULL_TREE if no simplification is
14050 possible. */
14052 tree
14053 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14055 tree sub = op0;
14056 tree subtype;
14057 poly_uint64 const_op01;
14059 STRIP_NOPS (sub);
14060 subtype = TREE_TYPE (sub);
14061 if (!POINTER_TYPE_P (subtype)
14062 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
14063 return NULL_TREE;
14065 if (TREE_CODE (sub) == ADDR_EXPR)
14067 tree op = TREE_OPERAND (sub, 0);
14068 tree optype = TREE_TYPE (op);
14069 /* *&CONST_DECL -> to the value of the const decl. */
14070 if (TREE_CODE (op) == CONST_DECL)
14071 return DECL_INITIAL (op);
14072 /* *&p => p; make sure to handle *&"str"[cst] here. */
14073 if (type == optype)
14075 tree fop = fold_read_from_constant_string (op);
14076 if (fop)
14077 return fop;
14078 else
14079 return op;
14081 /* *(foo *)&fooarray => fooarray[0] */
14082 else if (TREE_CODE (optype) == ARRAY_TYPE
14083 && type == TREE_TYPE (optype)
14084 && (!in_gimple_form
14085 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14087 tree type_domain = TYPE_DOMAIN (optype);
14088 tree min_val = size_zero_node;
14089 if (type_domain && TYPE_MIN_VALUE (type_domain))
14090 min_val = TYPE_MIN_VALUE (type_domain);
14091 if (in_gimple_form
14092 && TREE_CODE (min_val) != INTEGER_CST)
14093 return NULL_TREE;
14094 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14095 NULL_TREE, NULL_TREE);
14097 /* *(foo *)&complexfoo => __real__ complexfoo */
14098 else if (TREE_CODE (optype) == COMPLEX_TYPE
14099 && type == TREE_TYPE (optype))
14100 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14101 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14102 else if (TREE_CODE (optype) == VECTOR_TYPE
14103 && type == TREE_TYPE (optype))
14105 tree part_width = TYPE_SIZE (type);
14106 tree index = bitsize_int (0);
14107 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14111 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14112 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
14114 tree op00 = TREE_OPERAND (sub, 0);
14115 tree op01 = TREE_OPERAND (sub, 1);
14117 STRIP_NOPS (op00);
14118 if (TREE_CODE (op00) == ADDR_EXPR)
14120 tree op00type;
14121 op00 = TREE_OPERAND (op00, 0);
14122 op00type = TREE_TYPE (op00);
14124 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14125 if (TREE_CODE (op00type) == VECTOR_TYPE
14126 && type == TREE_TYPE (op00type))
14128 tree part_width = TYPE_SIZE (type);
14129 poly_uint64 max_offset
14130 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14131 * TYPE_VECTOR_SUBPARTS (op00type));
14132 if (known_lt (const_op01, max_offset))
14134 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
14135 return fold_build3_loc (loc,
14136 BIT_FIELD_REF, type, op00,
14137 part_width, index);
14140 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14141 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14142 && type == TREE_TYPE (op00type))
14144 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
14145 const_op01))
14146 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14148 /* ((foo *)&fooarray)[1] => fooarray[1] */
14149 else if (TREE_CODE (op00type) == ARRAY_TYPE
14150 && type == TREE_TYPE (op00type))
14152 tree type_domain = TYPE_DOMAIN (op00type);
14153 tree min = size_zero_node;
14154 if (type_domain && TYPE_MIN_VALUE (type_domain))
14155 min = TYPE_MIN_VALUE (type_domain);
14156 offset_int off = wi::to_offset (op01);
14157 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type));
14158 offset_int remainder;
14159 off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder);
14160 if (remainder == 0 && TREE_CODE (min) == INTEGER_CST)
14162 off = off + wi::to_offset (min);
14163 op01 = wide_int_to_tree (sizetype, off);
14164 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14165 NULL_TREE, NULL_TREE);
14171 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14172 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14173 && type == TREE_TYPE (TREE_TYPE (subtype))
14174 && (!in_gimple_form
14175 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14177 tree type_domain;
14178 tree min_val = size_zero_node;
14179 sub = build_fold_indirect_ref_loc (loc, sub);
14180 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14181 if (type_domain && TYPE_MIN_VALUE (type_domain))
14182 min_val = TYPE_MIN_VALUE (type_domain);
14183 if (in_gimple_form
14184 && TREE_CODE (min_val) != INTEGER_CST)
14185 return NULL_TREE;
14186 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14187 NULL_TREE);
14190 return NULL_TREE;
14193 /* Builds an expression for an indirection through T, simplifying some
14194 cases. */
14196 tree
14197 build_fold_indirect_ref_loc (location_t loc, tree t)
14199 tree type = TREE_TYPE (TREE_TYPE (t));
14200 tree sub = fold_indirect_ref_1 (loc, type, t);
14202 if (sub)
14203 return sub;
14205 return build1_loc (loc, INDIRECT_REF, type, t);
14208 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14210 tree
14211 fold_indirect_ref_loc (location_t loc, tree t)
14213 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14215 if (sub)
14216 return sub;
14217 else
14218 return t;
14221 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14222 whose result is ignored. The type of the returned tree need not be
14223 the same as the original expression. */
14225 tree
14226 fold_ignored_result (tree t)
14228 if (!TREE_SIDE_EFFECTS (t))
14229 return integer_zero_node;
14231 for (;;)
14232 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14234 case tcc_unary:
14235 t = TREE_OPERAND (t, 0);
14236 break;
14238 case tcc_binary:
14239 case tcc_comparison:
14240 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14241 t = TREE_OPERAND (t, 0);
14242 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14243 t = TREE_OPERAND (t, 1);
14244 else
14245 return t;
14246 break;
14248 case tcc_expression:
14249 switch (TREE_CODE (t))
14251 case COMPOUND_EXPR:
14252 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14253 return t;
14254 t = TREE_OPERAND (t, 0);
14255 break;
14257 case COND_EXPR:
14258 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14259 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14260 return t;
14261 t = TREE_OPERAND (t, 0);
14262 break;
14264 default:
14265 return t;
14267 break;
14269 default:
14270 return t;
14274 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14276 tree
14277 round_up_loc (location_t loc, tree value, unsigned int divisor)
14279 tree div = NULL_TREE;
14281 if (divisor == 1)
14282 return value;
14284 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14285 have to do anything. Only do this when we are not given a const,
14286 because in that case, this check is more expensive than just
14287 doing it. */
14288 if (TREE_CODE (value) != INTEGER_CST)
14290 div = build_int_cst (TREE_TYPE (value), divisor);
14292 if (multiple_of_p (TREE_TYPE (value), value, div))
14293 return value;
14296 /* If divisor is a power of two, simplify this to bit manipulation. */
14297 if (pow2_or_zerop (divisor))
14299 if (TREE_CODE (value) == INTEGER_CST)
14301 wide_int val = wi::to_wide (value);
14302 bool overflow_p;
14304 if ((val & (divisor - 1)) == 0)
14305 return value;
14307 overflow_p = TREE_OVERFLOW (value);
14308 val += divisor - 1;
14309 val &= (int) -divisor;
14310 if (val == 0)
14311 overflow_p = true;
14313 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14315 else
14317 tree t;
14319 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14320 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14321 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14322 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14325 else
14327 if (!div)
14328 div = build_int_cst (TREE_TYPE (value), divisor);
14329 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14330 value = size_binop_loc (loc, MULT_EXPR, value, div);
14333 return value;
14336 /* Likewise, but round down. */
14338 tree
14339 round_down_loc (location_t loc, tree value, int divisor)
14341 tree div = NULL_TREE;
14343 gcc_assert (divisor > 0);
14344 if (divisor == 1)
14345 return value;
14347 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14348 have to do anything. Only do this when we are not given a const,
14349 because in that case, this check is more expensive than just
14350 doing it. */
14351 if (TREE_CODE (value) != INTEGER_CST)
14353 div = build_int_cst (TREE_TYPE (value), divisor);
14355 if (multiple_of_p (TREE_TYPE (value), value, div))
14356 return value;
14359 /* If divisor is a power of two, simplify this to bit manipulation. */
14360 if (pow2_or_zerop (divisor))
14362 tree t;
14364 t = build_int_cst (TREE_TYPE (value), -divisor);
14365 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14367 else
14369 if (!div)
14370 div = build_int_cst (TREE_TYPE (value), divisor);
14371 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14372 value = size_binop_loc (loc, MULT_EXPR, value, div);
14375 return value;
14378 /* Returns the pointer to the base of the object addressed by EXP and
14379 extracts the information about the offset of the access, storing it
14380 to PBITPOS and POFFSET. */
14382 static tree
14383 split_address_to_core_and_offset (tree exp,
14384 poly_int64_pod *pbitpos, tree *poffset)
14386 tree core;
14387 machine_mode mode;
14388 int unsignedp, reversep, volatilep;
14389 poly_int64 bitsize;
14390 location_t loc = EXPR_LOCATION (exp);
14392 if (TREE_CODE (exp) == ADDR_EXPR)
14394 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14395 poffset, &mode, &unsignedp, &reversep,
14396 &volatilep);
14397 core = build_fold_addr_expr_loc (loc, core);
14399 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14401 core = TREE_OPERAND (exp, 0);
14402 STRIP_NOPS (core);
14403 *pbitpos = 0;
14404 *poffset = TREE_OPERAND (exp, 1);
14405 if (poly_int_tree_p (*poffset))
14407 poly_offset_int tem
14408 = wi::sext (wi::to_poly_offset (*poffset),
14409 TYPE_PRECISION (TREE_TYPE (*poffset)));
14410 tem <<= LOG2_BITS_PER_UNIT;
14411 if (tem.to_shwi (pbitpos))
14412 *poffset = NULL_TREE;
14415 else
14417 core = exp;
14418 *pbitpos = 0;
14419 *poffset = NULL_TREE;
14422 return core;
14425 /* Returns true if addresses of E1 and E2 differ by a constant, false
14426 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14428 bool
14429 ptr_difference_const (tree e1, tree e2, poly_int64_pod *diff)
14431 tree core1, core2;
14432 poly_int64 bitpos1, bitpos2;
14433 tree toffset1, toffset2, tdiff, type;
14435 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14436 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14438 poly_int64 bytepos1, bytepos2;
14439 if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
14440 || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
14441 || !operand_equal_p (core1, core2, 0))
14442 return false;
14444 if (toffset1 && toffset2)
14446 type = TREE_TYPE (toffset1);
14447 if (type != TREE_TYPE (toffset2))
14448 toffset2 = fold_convert (type, toffset2);
14450 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14451 if (!cst_and_fits_in_hwi (tdiff))
14452 return false;
14454 *diff = int_cst_value (tdiff);
14456 else if (toffset1 || toffset2)
14458 /* If only one of the offsets is non-constant, the difference cannot
14459 be a constant. */
14460 return false;
14462 else
14463 *diff = 0;
14465 *diff += bytepos1 - bytepos2;
14466 return true;
14469 /* Return OFF converted to a pointer offset type suitable as offset for
14470 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14471 tree
14472 convert_to_ptrofftype_loc (location_t loc, tree off)
14474 return fold_convert_loc (loc, sizetype, off);
14477 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14478 tree
14479 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14481 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14482 ptr, convert_to_ptrofftype_loc (loc, off));
14485 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14486 tree
14487 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14489 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14490 ptr, size_int (off));
14493 /* Return a char pointer for a C string if it is a string constant
14494 or sum of string constant and integer constant. We only support
14495 string constants properly terminated with '\0' character.
14496 If STRLEN is a valid pointer, length (including terminating character)
14497 of returned string is stored to the argument. */
14499 const char *
14500 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14502 tree offset_node;
14504 if (strlen)
14505 *strlen = 0;
14507 src = string_constant (src, &offset_node);
14508 if (src == 0)
14509 return NULL;
14511 unsigned HOST_WIDE_INT offset = 0;
14512 if (offset_node != NULL_TREE)
14514 if (!tree_fits_uhwi_p (offset_node))
14515 return NULL;
14516 else
14517 offset = tree_to_uhwi (offset_node);
14520 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14521 const char *string = TREE_STRING_POINTER (src);
14523 /* Support only properly null-terminated strings. */
14524 if (string_length == 0
14525 || string[string_length - 1] != '\0'
14526 || offset >= string_length)
14527 return NULL;
14529 if (strlen)
14530 *strlen = string_length - offset;
14531 return string + offset;
14534 #if CHECKING_P
14536 namespace selftest {
14538 /* Helper functions for writing tests of folding trees. */
14540 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14542 static void
14543 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14544 tree constant)
14546 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14549 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14550 wrapping WRAPPED_EXPR. */
14552 static void
14553 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14554 tree wrapped_expr)
14556 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14557 ASSERT_NE (wrapped_expr, result);
14558 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14559 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14562 /* Verify that various arithmetic binary operations are folded
14563 correctly. */
14565 static void
14566 test_arithmetic_folding ()
14568 tree type = integer_type_node;
14569 tree x = create_tmp_var_raw (type, "x");
14570 tree zero = build_zero_cst (type);
14571 tree one = build_int_cst (type, 1);
14573 /* Addition. */
14574 /* 1 <-- (0 + 1) */
14575 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14576 one);
14577 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14578 one);
14580 /* (nonlvalue)x <-- (x + 0) */
14581 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14584 /* Subtraction. */
14585 /* 0 <-- (x - x) */
14586 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14587 zero);
14588 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14591 /* Multiplication. */
14592 /* 0 <-- (x * 0) */
14593 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14594 zero);
14596 /* (nonlvalue)x <-- (x * 1) */
14597 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14601 /* Verify that various binary operations on vectors are folded
14602 correctly. */
14604 static void
14605 test_vector_folding ()
14607 tree inner_type = integer_type_node;
14608 tree type = build_vector_type (inner_type, 4);
14609 tree zero = build_zero_cst (type);
14610 tree one = build_one_cst (type);
14612 /* Verify equality tests that return a scalar boolean result. */
14613 tree res_type = boolean_type_node;
14614 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14615 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14616 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14617 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14620 /* Verify folding of VEC_DUPLICATE_EXPRs. */
14622 static void
14623 test_vec_duplicate_folding ()
14625 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
14626 machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
14627 /* This will be 1 if VEC_MODE isn't a vector mode. */
14628 poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
14630 tree type = build_vector_type (ssizetype, nunits);
14631 tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
14632 tree dup5_cst = build_vector_from_val (type, ssize_int (5));
14633 ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
14636 /* Run all of the selftests within this file. */
14638 void
14639 fold_const_c_tests ()
14641 test_arithmetic_folding ();
14642 test_vector_folding ();
14643 test_vec_duplicate_folding ();
14646 } // namespace selftest
14648 #endif /* CHECKING_P */