poly_int: get_inner_reference & co.
[official-gcc.git] / gcc / fold-const.c
blob2039d4dd2f9e3deab1a8cad4d6639cf36347d541
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2017 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"
86 /* Nonzero if we are folding constants inside an initializer; zero
87 otherwise. */
88 int folding_initializer = 0;
90 /* The following constants represent a bit based encoding of GCC's
91 comparison operators. This encoding simplifies transformations
92 on relational comparison operators, such as AND and OR. */
93 enum comparison_code {
94 COMPCODE_FALSE = 0,
95 COMPCODE_LT = 1,
96 COMPCODE_EQ = 2,
97 COMPCODE_LE = 3,
98 COMPCODE_GT = 4,
99 COMPCODE_LTGT = 5,
100 COMPCODE_GE = 6,
101 COMPCODE_ORD = 7,
102 COMPCODE_UNORD = 8,
103 COMPCODE_UNLT = 9,
104 COMPCODE_UNEQ = 10,
105 COMPCODE_UNLE = 11,
106 COMPCODE_UNGT = 12,
107 COMPCODE_NE = 13,
108 COMPCODE_UNGE = 14,
109 COMPCODE_TRUE = 15
112 static bool negate_expr_p (tree);
113 static tree negate_expr (tree);
114 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
115 static enum comparison_code comparison_to_compcode (enum tree_code);
116 static enum tree_code compcode_to_comparison (enum comparison_code);
117 static int twoval_comparison_p (tree, tree *, tree *, int *);
118 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
119 static tree optimize_bit_field_compare (location_t, enum tree_code,
120 tree, tree, tree);
121 static int simple_operand_p (const_tree);
122 static bool simple_operand_p_2 (tree);
123 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
124 static tree range_predecessor (tree);
125 static tree range_successor (tree);
126 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
127 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
128 static tree unextend (tree, int, int, tree);
129 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
130 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
131 static tree fold_binary_op_with_conditional_arg (location_t,
132 enum tree_code, tree,
133 tree, tree,
134 tree, tree, int);
135 static tree fold_negate_const (tree, tree);
136 static tree fold_not_const (const_tree, tree);
137 static tree fold_relational_const (enum tree_code, tree, tree, tree);
138 static tree fold_convert_const (enum tree_code, tree, tree);
139 static tree fold_view_convert_expr (tree, tree);
140 static tree fold_negate_expr (location_t, tree);
143 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
144 Otherwise, return LOC. */
146 static location_t
147 expr_location_or (tree t, location_t loc)
149 location_t tloc = EXPR_LOCATION (t);
150 return tloc == UNKNOWN_LOCATION ? loc : tloc;
153 /* Similar to protected_set_expr_location, but never modify x in place,
154 if location can and needs to be set, unshare it. */
156 static inline tree
157 protected_set_expr_location_unshare (tree x, location_t loc)
159 if (CAN_HAVE_LOCATION_P (x)
160 && EXPR_LOCATION (x) != loc
161 && !(TREE_CODE (x) == SAVE_EXPR
162 || TREE_CODE (x) == TARGET_EXPR
163 || TREE_CODE (x) == BIND_EXPR))
165 x = copy_node (x);
166 SET_EXPR_LOCATION (x, loc);
168 return x;
171 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
172 division and returns the quotient. Otherwise returns
173 NULL_TREE. */
175 tree
176 div_if_zero_remainder (const_tree arg1, const_tree arg2)
178 widest_int quo;
180 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
181 SIGNED, &quo))
182 return wide_int_to_tree (TREE_TYPE (arg1), quo);
184 return NULL_TREE;
187 /* This is nonzero if we should defer warnings about undefined
188 overflow. This facility exists because these warnings are a
189 special case. The code to estimate loop iterations does not want
190 to issue any warnings, since it works with expressions which do not
191 occur in user code. Various bits of cleanup code call fold(), but
192 only use the result if it has certain characteristics (e.g., is a
193 constant); that code only wants to issue a warning if the result is
194 used. */
196 static int fold_deferring_overflow_warnings;
198 /* If a warning about undefined overflow is deferred, this is the
199 warning. Note that this may cause us to turn two warnings into
200 one, but that is fine since it is sufficient to only give one
201 warning per expression. */
203 static const char* fold_deferred_overflow_warning;
205 /* If a warning about undefined overflow is deferred, this is the
206 level at which the warning should be emitted. */
208 static enum warn_strict_overflow_code fold_deferred_overflow_code;
210 /* Start deferring overflow warnings. We could use a stack here to
211 permit nested calls, but at present it is not necessary. */
213 void
214 fold_defer_overflow_warnings (void)
216 ++fold_deferring_overflow_warnings;
219 /* Stop deferring overflow warnings. If there is a pending warning,
220 and ISSUE is true, then issue the warning if appropriate. STMT is
221 the statement with which the warning should be associated (used for
222 location information); STMT may be NULL. CODE is the level of the
223 warning--a warn_strict_overflow_code value. This function will use
224 the smaller of CODE and the deferred code when deciding whether to
225 issue the warning. CODE may be zero to mean to always use the
226 deferred code. */
228 void
229 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
231 const char *warnmsg;
232 location_t locus;
234 gcc_assert (fold_deferring_overflow_warnings > 0);
235 --fold_deferring_overflow_warnings;
236 if (fold_deferring_overflow_warnings > 0)
238 if (fold_deferred_overflow_warning != NULL
239 && code != 0
240 && code < (int) fold_deferred_overflow_code)
241 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
242 return;
245 warnmsg = fold_deferred_overflow_warning;
246 fold_deferred_overflow_warning = NULL;
248 if (!issue || warnmsg == NULL)
249 return;
251 if (gimple_no_warning_p (stmt))
252 return;
254 /* Use the smallest code level when deciding to issue the
255 warning. */
256 if (code == 0 || code > (int) fold_deferred_overflow_code)
257 code = fold_deferred_overflow_code;
259 if (!issue_strict_overflow_warning (code))
260 return;
262 if (stmt == NULL)
263 locus = input_location;
264 else
265 locus = gimple_location (stmt);
266 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
269 /* Stop deferring overflow warnings, ignoring any deferred
270 warnings. */
272 void
273 fold_undefer_and_ignore_overflow_warnings (void)
275 fold_undefer_overflow_warnings (false, NULL, 0);
278 /* Whether we are deferring overflow warnings. */
280 bool
281 fold_deferring_overflow_warnings_p (void)
283 return fold_deferring_overflow_warnings > 0;
286 /* This is called when we fold something based on the fact that signed
287 overflow is undefined. */
289 void
290 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
292 if (fold_deferring_overflow_warnings > 0)
294 if (fold_deferred_overflow_warning == NULL
295 || wc < fold_deferred_overflow_code)
297 fold_deferred_overflow_warning = gmsgid;
298 fold_deferred_overflow_code = wc;
301 else if (issue_strict_overflow_warning (wc))
302 warning (OPT_Wstrict_overflow, gmsgid);
305 /* Return true if the built-in mathematical function specified by CODE
306 is odd, i.e. -f(x) == f(-x). */
308 bool
309 negate_mathfn_p (combined_fn fn)
311 switch (fn)
313 CASE_CFN_ASIN:
314 CASE_CFN_ASINH:
315 CASE_CFN_ATAN:
316 CASE_CFN_ATANH:
317 CASE_CFN_CASIN:
318 CASE_CFN_CASINH:
319 CASE_CFN_CATAN:
320 CASE_CFN_CATANH:
321 CASE_CFN_CBRT:
322 CASE_CFN_CPROJ:
323 CASE_CFN_CSIN:
324 CASE_CFN_CSINH:
325 CASE_CFN_CTAN:
326 CASE_CFN_CTANH:
327 CASE_CFN_ERF:
328 CASE_CFN_LLROUND:
329 CASE_CFN_LROUND:
330 CASE_CFN_ROUND:
331 CASE_CFN_SIN:
332 CASE_CFN_SINH:
333 CASE_CFN_TAN:
334 CASE_CFN_TANH:
335 CASE_CFN_TRUNC:
336 return true;
338 CASE_CFN_LLRINT:
339 CASE_CFN_LRINT:
340 CASE_CFN_NEARBYINT:
341 CASE_CFN_RINT:
342 return !flag_rounding_math;
344 default:
345 break;
347 return false;
350 /* Check whether we may negate an integer constant T without causing
351 overflow. */
353 bool
354 may_negate_without_overflow_p (const_tree t)
356 tree type;
358 gcc_assert (TREE_CODE (t) == INTEGER_CST);
360 type = TREE_TYPE (t);
361 if (TYPE_UNSIGNED (type))
362 return false;
364 return !wi::only_sign_bit_p (wi::to_wide (t));
367 /* Determine whether an expression T can be cheaply negated using
368 the function negate_expr without introducing undefined overflow. */
370 static bool
371 negate_expr_p (tree t)
373 tree type;
375 if (t == 0)
376 return false;
378 type = TREE_TYPE (t);
380 STRIP_SIGN_NOPS (t);
381 switch (TREE_CODE (t))
383 case INTEGER_CST:
384 if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
385 return true;
387 /* Check that -CST will not overflow type. */
388 return may_negate_without_overflow_p (t);
389 case BIT_NOT_EXPR:
390 return (INTEGRAL_TYPE_P (type)
391 && TYPE_OVERFLOW_WRAPS (type));
393 case FIXED_CST:
394 return true;
396 case NEGATE_EXPR:
397 return !TYPE_OVERFLOW_SANITIZED (type);
399 case REAL_CST:
400 /* We want to canonicalize to positive real constants. Pretend
401 that only negative ones can be easily negated. */
402 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
404 case COMPLEX_CST:
405 return negate_expr_p (TREE_REALPART (t))
406 && negate_expr_p (TREE_IMAGPART (t));
408 case VECTOR_CST:
410 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
411 return true;
413 /* Steps don't prevent negation. */
414 unsigned int count = vector_cst_encoded_nelts (t);
415 for (unsigned int i = 0; i < count; ++i)
416 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
417 return false;
419 return true;
422 case COMPLEX_EXPR:
423 return negate_expr_p (TREE_OPERAND (t, 0))
424 && negate_expr_p (TREE_OPERAND (t, 1));
426 case CONJ_EXPR:
427 return negate_expr_p (TREE_OPERAND (t, 0));
429 case PLUS_EXPR:
430 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
431 || HONOR_SIGNED_ZEROS (element_mode (type))
432 || (ANY_INTEGRAL_TYPE_P (type)
433 && ! TYPE_OVERFLOW_WRAPS (type)))
434 return false;
435 /* -(A + B) -> (-B) - A. */
436 if (negate_expr_p (TREE_OPERAND (t, 1)))
437 return true;
438 /* -(A + B) -> (-A) - B. */
439 return negate_expr_p (TREE_OPERAND (t, 0));
441 case MINUS_EXPR:
442 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
443 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
444 && !HONOR_SIGNED_ZEROS (element_mode (type))
445 && (! ANY_INTEGRAL_TYPE_P (type)
446 || TYPE_OVERFLOW_WRAPS (type));
448 case MULT_EXPR:
449 if (TYPE_UNSIGNED (type))
450 break;
451 /* INT_MIN/n * n doesn't overflow while negating one operand it does
452 if n is a (negative) power of two. */
453 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
454 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
455 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
456 && (wi::popcount
457 (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
458 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
459 && (wi::popcount
460 (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
461 break;
463 /* Fall through. */
465 case RDIV_EXPR:
466 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
467 return negate_expr_p (TREE_OPERAND (t, 1))
468 || negate_expr_p (TREE_OPERAND (t, 0));
469 break;
471 case TRUNC_DIV_EXPR:
472 case ROUND_DIV_EXPR:
473 case EXACT_DIV_EXPR:
474 if (TYPE_UNSIGNED (type))
475 break;
476 if (negate_expr_p (TREE_OPERAND (t, 0)))
477 return true;
478 /* In general we can't negate B in A / B, because if A is INT_MIN and
479 B is 1, we may turn this into INT_MIN / -1 which is undefined
480 and actually traps on some architectures. */
481 if (! INTEGRAL_TYPE_P (TREE_TYPE (t))
482 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
483 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
484 && ! integer_onep (TREE_OPERAND (t, 1))))
485 return negate_expr_p (TREE_OPERAND (t, 1));
486 break;
488 case NOP_EXPR:
489 /* Negate -((double)float) as (double)(-float). */
490 if (TREE_CODE (type) == REAL_TYPE)
492 tree tem = strip_float_extensions (t);
493 if (tem != t)
494 return negate_expr_p (tem);
496 break;
498 case CALL_EXPR:
499 /* Negate -f(x) as f(-x). */
500 if (negate_mathfn_p (get_call_combined_fn (t)))
501 return negate_expr_p (CALL_EXPR_ARG (t, 0));
502 break;
504 case RSHIFT_EXPR:
505 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
506 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
508 tree op1 = TREE_OPERAND (t, 1);
509 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
510 return true;
512 break;
514 default:
515 break;
517 return false;
520 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
521 simplification is possible.
522 If negate_expr_p would return true for T, NULL_TREE will never be
523 returned. */
525 static tree
526 fold_negate_expr_1 (location_t loc, tree t)
528 tree type = TREE_TYPE (t);
529 tree tem;
531 switch (TREE_CODE (t))
533 /* Convert - (~A) to A + 1. */
534 case BIT_NOT_EXPR:
535 if (INTEGRAL_TYPE_P (type))
536 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
537 build_one_cst (type));
538 break;
540 case INTEGER_CST:
541 tem = fold_negate_const (t, type);
542 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
543 || (ANY_INTEGRAL_TYPE_P (type)
544 && !TYPE_OVERFLOW_TRAPS (type)
545 && TYPE_OVERFLOW_WRAPS (type))
546 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
547 return tem;
548 break;
550 case POLY_INT_CST:
551 case REAL_CST:
552 case FIXED_CST:
553 tem = fold_negate_const (t, type);
554 return tem;
556 case COMPLEX_CST:
558 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
559 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
560 if (rpart && ipart)
561 return build_complex (type, rpart, ipart);
563 break;
565 case VECTOR_CST:
567 tree_vector_builder elts;
568 elts.new_unary_operation (type, t, true);
569 unsigned int count = elts.encoded_nelts ();
570 for (unsigned int i = 0; i < count; ++i)
572 tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
573 if (elt == NULL_TREE)
574 return NULL_TREE;
575 elts.quick_push (elt);
578 return elts.build ();
581 case COMPLEX_EXPR:
582 if (negate_expr_p (t))
583 return fold_build2_loc (loc, COMPLEX_EXPR, type,
584 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
585 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
586 break;
588 case CONJ_EXPR:
589 if (negate_expr_p (t))
590 return fold_build1_loc (loc, CONJ_EXPR, type,
591 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
592 break;
594 case NEGATE_EXPR:
595 if (!TYPE_OVERFLOW_SANITIZED (type))
596 return TREE_OPERAND (t, 0);
597 break;
599 case PLUS_EXPR:
600 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
601 && !HONOR_SIGNED_ZEROS (element_mode (type)))
603 /* -(A + B) -> (-B) - A. */
604 if (negate_expr_p (TREE_OPERAND (t, 1)))
606 tem = negate_expr (TREE_OPERAND (t, 1));
607 return fold_build2_loc (loc, MINUS_EXPR, type,
608 tem, TREE_OPERAND (t, 0));
611 /* -(A + B) -> (-A) - B. */
612 if (negate_expr_p (TREE_OPERAND (t, 0)))
614 tem = negate_expr (TREE_OPERAND (t, 0));
615 return fold_build2_loc (loc, MINUS_EXPR, type,
616 tem, TREE_OPERAND (t, 1));
619 break;
621 case MINUS_EXPR:
622 /* - (A - B) -> B - A */
623 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
624 && !HONOR_SIGNED_ZEROS (element_mode (type)))
625 return fold_build2_loc (loc, MINUS_EXPR, type,
626 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
627 break;
629 case MULT_EXPR:
630 if (TYPE_UNSIGNED (type))
631 break;
633 /* Fall through. */
635 case RDIV_EXPR:
636 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
638 tem = TREE_OPERAND (t, 1);
639 if (negate_expr_p (tem))
640 return fold_build2_loc (loc, TREE_CODE (t), type,
641 TREE_OPERAND (t, 0), negate_expr (tem));
642 tem = TREE_OPERAND (t, 0);
643 if (negate_expr_p (tem))
644 return fold_build2_loc (loc, TREE_CODE (t), type,
645 negate_expr (tem), TREE_OPERAND (t, 1));
647 break;
649 case TRUNC_DIV_EXPR:
650 case ROUND_DIV_EXPR:
651 case EXACT_DIV_EXPR:
652 if (TYPE_UNSIGNED (type))
653 break;
654 if (negate_expr_p (TREE_OPERAND (t, 0)))
655 return fold_build2_loc (loc, TREE_CODE (t), type,
656 negate_expr (TREE_OPERAND (t, 0)),
657 TREE_OPERAND (t, 1));
658 /* In general we can't negate B in A / B, because if A is INT_MIN and
659 B is 1, we may turn this into INT_MIN / -1 which is undefined
660 and actually traps on some architectures. */
661 if ((! INTEGRAL_TYPE_P (TREE_TYPE (t))
662 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
663 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
664 && ! integer_onep (TREE_OPERAND (t, 1))))
665 && negate_expr_p (TREE_OPERAND (t, 1)))
666 return fold_build2_loc (loc, TREE_CODE (t), type,
667 TREE_OPERAND (t, 0),
668 negate_expr (TREE_OPERAND (t, 1)));
669 break;
671 case NOP_EXPR:
672 /* Convert -((double)float) into (double)(-float). */
673 if (TREE_CODE (type) == REAL_TYPE)
675 tem = strip_float_extensions (t);
676 if (tem != t && negate_expr_p (tem))
677 return fold_convert_loc (loc, type, negate_expr (tem));
679 break;
681 case CALL_EXPR:
682 /* Negate -f(x) as f(-x). */
683 if (negate_mathfn_p (get_call_combined_fn (t))
684 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
686 tree fndecl, arg;
688 fndecl = get_callee_fndecl (t);
689 arg = negate_expr (CALL_EXPR_ARG (t, 0));
690 return build_call_expr_loc (loc, fndecl, 1, arg);
692 break;
694 case RSHIFT_EXPR:
695 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
696 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
698 tree op1 = TREE_OPERAND (t, 1);
699 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
701 tree ntype = TYPE_UNSIGNED (type)
702 ? signed_type_for (type)
703 : unsigned_type_for (type);
704 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
705 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
706 return fold_convert_loc (loc, type, temp);
709 break;
711 default:
712 break;
715 return NULL_TREE;
718 /* A wrapper for fold_negate_expr_1. */
720 static tree
721 fold_negate_expr (location_t loc, tree t)
723 tree type = TREE_TYPE (t);
724 STRIP_SIGN_NOPS (t);
725 tree tem = fold_negate_expr_1 (loc, t);
726 if (tem == NULL_TREE)
727 return NULL_TREE;
728 return fold_convert_loc (loc, type, tem);
731 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
732 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
733 return NULL_TREE. */
735 static tree
736 negate_expr (tree t)
738 tree type, tem;
739 location_t loc;
741 if (t == NULL_TREE)
742 return NULL_TREE;
744 loc = EXPR_LOCATION (t);
745 type = TREE_TYPE (t);
746 STRIP_SIGN_NOPS (t);
748 tem = fold_negate_expr (loc, t);
749 if (!tem)
750 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
751 return fold_convert_loc (loc, type, tem);
754 /* Split a tree IN into a constant, literal and variable parts that could be
755 combined with CODE to make IN. "constant" means an expression with
756 TREE_CONSTANT but that isn't an actual constant. CODE must be a
757 commutative arithmetic operation. Store the constant part into *CONP,
758 the literal in *LITP and return the variable part. If a part isn't
759 present, set it to null. If the tree does not decompose in this way,
760 return the entire tree as the variable part and the other parts as null.
762 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
763 case, we negate an operand that was subtracted. Except if it is a
764 literal for which we use *MINUS_LITP instead.
766 If NEGATE_P is true, we are negating all of IN, again except a literal
767 for which we use *MINUS_LITP instead. If a variable part is of pointer
768 type, it is negated after converting to TYPE. This prevents us from
769 generating illegal MINUS pointer expression. LOC is the location of
770 the converted variable part.
772 If IN is itself a literal or constant, return it as appropriate.
774 Note that we do not guarantee that any of the three values will be the
775 same type as IN, but they will have the same signedness and mode. */
777 static tree
778 split_tree (tree in, tree type, enum tree_code code,
779 tree *minus_varp, tree *conp, tree *minus_conp,
780 tree *litp, tree *minus_litp, int negate_p)
782 tree var = 0;
783 *minus_varp = 0;
784 *conp = 0;
785 *minus_conp = 0;
786 *litp = 0;
787 *minus_litp = 0;
789 /* Strip any conversions that don't change the machine mode or signedness. */
790 STRIP_SIGN_NOPS (in);
792 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
793 || TREE_CODE (in) == FIXED_CST)
794 *litp = in;
795 else if (TREE_CODE (in) == code
796 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
797 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
798 /* We can associate addition and subtraction together (even
799 though the C standard doesn't say so) for integers because
800 the value is not affected. For reals, the value might be
801 affected, so we can't. */
802 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
803 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
804 || (code == MINUS_EXPR
805 && (TREE_CODE (in) == PLUS_EXPR
806 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
808 tree op0 = TREE_OPERAND (in, 0);
809 tree op1 = TREE_OPERAND (in, 1);
810 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
811 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
813 /* First see if either of the operands is a literal, then a constant. */
814 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
815 || TREE_CODE (op0) == FIXED_CST)
816 *litp = op0, op0 = 0;
817 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
818 || TREE_CODE (op1) == FIXED_CST)
819 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
821 if (op0 != 0 && TREE_CONSTANT (op0))
822 *conp = op0, op0 = 0;
823 else if (op1 != 0 && TREE_CONSTANT (op1))
824 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
826 /* If we haven't dealt with either operand, this is not a case we can
827 decompose. Otherwise, VAR is either of the ones remaining, if any. */
828 if (op0 != 0 && op1 != 0)
829 var = in;
830 else if (op0 != 0)
831 var = op0;
832 else
833 var = op1, neg_var_p = neg1_p;
835 /* Now do any needed negations. */
836 if (neg_litp_p)
837 *minus_litp = *litp, *litp = 0;
838 if (neg_conp_p && *conp)
839 *minus_conp = *conp, *conp = 0;
840 if (neg_var_p && var)
841 *minus_varp = var, var = 0;
843 else if (TREE_CONSTANT (in))
844 *conp = in;
845 else if (TREE_CODE (in) == BIT_NOT_EXPR
846 && code == PLUS_EXPR)
848 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
849 when IN is constant. */
850 *litp = build_minus_one_cst (type);
851 *minus_varp = TREE_OPERAND (in, 0);
853 else
854 var = in;
856 if (negate_p)
858 if (*litp)
859 *minus_litp = *litp, *litp = 0;
860 else if (*minus_litp)
861 *litp = *minus_litp, *minus_litp = 0;
862 if (*conp)
863 *minus_conp = *conp, *conp = 0;
864 else if (*minus_conp)
865 *conp = *minus_conp, *minus_conp = 0;
866 if (var)
867 *minus_varp = var, var = 0;
868 else if (*minus_varp)
869 var = *minus_varp, *minus_varp = 0;
872 if (*litp
873 && TREE_OVERFLOW_P (*litp))
874 *litp = drop_tree_overflow (*litp);
875 if (*minus_litp
876 && TREE_OVERFLOW_P (*minus_litp))
877 *minus_litp = drop_tree_overflow (*minus_litp);
879 return var;
882 /* Re-associate trees split by the above function. T1 and T2 are
883 either expressions to associate or null. Return the new
884 expression, if any. LOC is the location of the new expression. If
885 we build an operation, do it in TYPE and with CODE. */
887 static tree
888 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
890 if (t1 == 0)
892 gcc_assert (t2 == 0 || code != MINUS_EXPR);
893 return t2;
895 else if (t2 == 0)
896 return t1;
898 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
899 try to fold this since we will have infinite recursion. But do
900 deal with any NEGATE_EXPRs. */
901 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
902 || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
903 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
905 if (code == PLUS_EXPR)
907 if (TREE_CODE (t1) == NEGATE_EXPR)
908 return build2_loc (loc, MINUS_EXPR, type,
909 fold_convert_loc (loc, type, t2),
910 fold_convert_loc (loc, type,
911 TREE_OPERAND (t1, 0)));
912 else if (TREE_CODE (t2) == NEGATE_EXPR)
913 return build2_loc (loc, MINUS_EXPR, type,
914 fold_convert_loc (loc, type, t1),
915 fold_convert_loc (loc, type,
916 TREE_OPERAND (t2, 0)));
917 else if (integer_zerop (t2))
918 return fold_convert_loc (loc, type, t1);
920 else if (code == MINUS_EXPR)
922 if (integer_zerop (t2))
923 return fold_convert_loc (loc, type, t1);
926 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
927 fold_convert_loc (loc, type, t2));
930 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
931 fold_convert_loc (loc, type, t2));
934 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
935 for use in int_const_binop, size_binop and size_diffop. */
937 static bool
938 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
940 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
941 return false;
942 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
943 return false;
945 switch (code)
947 case LSHIFT_EXPR:
948 case RSHIFT_EXPR:
949 case LROTATE_EXPR:
950 case RROTATE_EXPR:
951 return true;
953 default:
954 break;
957 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
958 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
959 && TYPE_MODE (type1) == TYPE_MODE (type2);
962 /* Subroutine of int_const_binop_1 that handles two INTEGER_CSTs. */
964 static tree
965 int_const_binop_2 (enum tree_code code, const_tree parg1, const_tree parg2,
966 int overflowable)
968 wide_int res;
969 tree t;
970 tree type = TREE_TYPE (parg1);
971 signop sign = TYPE_SIGN (type);
972 bool overflow = false;
974 wi::tree_to_wide_ref arg1 = wi::to_wide (parg1);
975 wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type));
977 switch (code)
979 case BIT_IOR_EXPR:
980 res = wi::bit_or (arg1, arg2);
981 break;
983 case BIT_XOR_EXPR:
984 res = wi::bit_xor (arg1, arg2);
985 break;
987 case BIT_AND_EXPR:
988 res = wi::bit_and (arg1, arg2);
989 break;
991 case RSHIFT_EXPR:
992 case LSHIFT_EXPR:
993 if (wi::neg_p (arg2))
995 arg2 = -arg2;
996 if (code == RSHIFT_EXPR)
997 code = LSHIFT_EXPR;
998 else
999 code = RSHIFT_EXPR;
1002 if (code == RSHIFT_EXPR)
1003 /* It's unclear from the C standard whether shifts can overflow.
1004 The following code ignores overflow; perhaps a C standard
1005 interpretation ruling is needed. */
1006 res = wi::rshift (arg1, arg2, sign);
1007 else
1008 res = wi::lshift (arg1, arg2);
1009 break;
1011 case RROTATE_EXPR:
1012 case LROTATE_EXPR:
1013 if (wi::neg_p (arg2))
1015 arg2 = -arg2;
1016 if (code == RROTATE_EXPR)
1017 code = LROTATE_EXPR;
1018 else
1019 code = RROTATE_EXPR;
1022 if (code == RROTATE_EXPR)
1023 res = wi::rrotate (arg1, arg2);
1024 else
1025 res = wi::lrotate (arg1, arg2);
1026 break;
1028 case PLUS_EXPR:
1029 res = wi::add (arg1, arg2, sign, &overflow);
1030 break;
1032 case MINUS_EXPR:
1033 res = wi::sub (arg1, arg2, sign, &overflow);
1034 break;
1036 case MULT_EXPR:
1037 res = wi::mul (arg1, arg2, sign, &overflow);
1038 break;
1040 case MULT_HIGHPART_EXPR:
1041 res = wi::mul_high (arg1, arg2, sign);
1042 break;
1044 case TRUNC_DIV_EXPR:
1045 case EXACT_DIV_EXPR:
1046 if (arg2 == 0)
1047 return NULL_TREE;
1048 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1049 break;
1051 case FLOOR_DIV_EXPR:
1052 if (arg2 == 0)
1053 return NULL_TREE;
1054 res = wi::div_floor (arg1, arg2, sign, &overflow);
1055 break;
1057 case CEIL_DIV_EXPR:
1058 if (arg2 == 0)
1059 return NULL_TREE;
1060 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1061 break;
1063 case ROUND_DIV_EXPR:
1064 if (arg2 == 0)
1065 return NULL_TREE;
1066 res = wi::div_round (arg1, arg2, sign, &overflow);
1067 break;
1069 case TRUNC_MOD_EXPR:
1070 if (arg2 == 0)
1071 return NULL_TREE;
1072 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1073 break;
1075 case FLOOR_MOD_EXPR:
1076 if (arg2 == 0)
1077 return NULL_TREE;
1078 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1079 break;
1081 case CEIL_MOD_EXPR:
1082 if (arg2 == 0)
1083 return NULL_TREE;
1084 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1085 break;
1087 case ROUND_MOD_EXPR:
1088 if (arg2 == 0)
1089 return NULL_TREE;
1090 res = wi::mod_round (arg1, arg2, sign, &overflow);
1091 break;
1093 case MIN_EXPR:
1094 res = wi::min (arg1, arg2, sign);
1095 break;
1097 case MAX_EXPR:
1098 res = wi::max (arg1, arg2, sign);
1099 break;
1101 default:
1102 return NULL_TREE;
1105 t = force_fit_type (type, res, overflowable,
1106 (((sign == SIGNED || overflowable == -1)
1107 && overflow)
1108 | TREE_OVERFLOW (parg1) | TREE_OVERFLOW (parg2)));
1110 return t;
1113 /* Combine two integer constants PARG1 and PARG2 under operation CODE
1114 to produce a new constant. Return NULL_TREE if we don't know how
1115 to evaluate CODE at compile-time. */
1117 static tree
1118 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree arg2,
1119 int overflowable)
1121 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1122 return int_const_binop_2 (code, arg1, arg2, overflowable);
1124 gcc_assert (NUM_POLY_INT_COEFFS != 1);
1126 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1128 poly_wide_int res;
1129 bool overflow;
1130 tree type = TREE_TYPE (arg1);
1131 signop sign = TYPE_SIGN (type);
1132 switch (code)
1134 case PLUS_EXPR:
1135 res = wi::add (wi::to_poly_wide (arg1),
1136 wi::to_poly_wide (arg2), sign, &overflow);
1137 break;
1139 case MINUS_EXPR:
1140 res = wi::sub (wi::to_poly_wide (arg1),
1141 wi::to_poly_wide (arg2), sign, &overflow);
1142 break;
1144 case MULT_EXPR:
1145 if (TREE_CODE (arg2) == INTEGER_CST)
1146 res = wi::mul (wi::to_poly_wide (arg1),
1147 wi::to_wide (arg2), sign, &overflow);
1148 else if (TREE_CODE (arg1) == INTEGER_CST)
1149 res = wi::mul (wi::to_poly_wide (arg2),
1150 wi::to_wide (arg1), sign, &overflow);
1151 else
1152 return NULL_TREE;
1153 break;
1155 case LSHIFT_EXPR:
1156 if (TREE_CODE (arg2) == INTEGER_CST)
1157 res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1158 else
1159 return NULL_TREE;
1160 break;
1162 case BIT_IOR_EXPR:
1163 if (TREE_CODE (arg2) != INTEGER_CST
1164 || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1165 &res))
1166 return NULL_TREE;
1167 break;
1169 default:
1170 return NULL_TREE;
1172 return force_fit_type (type, res, overflowable,
1173 (((sign == SIGNED || overflowable == -1)
1174 && overflow)
1175 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1178 return NULL_TREE;
1181 tree
1182 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1184 return int_const_binop_1 (code, arg1, arg2, 1);
1187 /* Return true if binary operation OP distributes over addition in operand
1188 OPNO, with the other operand being held constant. OPNO counts from 1. */
1190 static bool
1191 distributes_over_addition_p (tree_code op, int opno)
1193 switch (op)
1195 case PLUS_EXPR:
1196 case MINUS_EXPR:
1197 case MULT_EXPR:
1198 return true;
1200 case LSHIFT_EXPR:
1201 return opno == 1;
1203 default:
1204 return false;
1208 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1209 constant. We assume ARG1 and ARG2 have the same data type, or at least
1210 are the same kind of constant and the same machine mode. Return zero if
1211 combining the constants is not allowed in the current operating mode. */
1213 static tree
1214 const_binop (enum tree_code code, tree arg1, tree arg2)
1216 /* Sanity check for the recursive cases. */
1217 if (!arg1 || !arg2)
1218 return NULL_TREE;
1220 STRIP_NOPS (arg1);
1221 STRIP_NOPS (arg2);
1223 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1225 if (code == POINTER_PLUS_EXPR)
1226 return int_const_binop (PLUS_EXPR,
1227 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1229 return int_const_binop (code, arg1, arg2);
1232 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1234 machine_mode mode;
1235 REAL_VALUE_TYPE d1;
1236 REAL_VALUE_TYPE d2;
1237 REAL_VALUE_TYPE value;
1238 REAL_VALUE_TYPE result;
1239 bool inexact;
1240 tree t, type;
1242 /* The following codes are handled by real_arithmetic. */
1243 switch (code)
1245 case PLUS_EXPR:
1246 case MINUS_EXPR:
1247 case MULT_EXPR:
1248 case RDIV_EXPR:
1249 case MIN_EXPR:
1250 case MAX_EXPR:
1251 break;
1253 default:
1254 return NULL_TREE;
1257 d1 = TREE_REAL_CST (arg1);
1258 d2 = TREE_REAL_CST (arg2);
1260 type = TREE_TYPE (arg1);
1261 mode = TYPE_MODE (type);
1263 /* Don't perform operation if we honor signaling NaNs and
1264 either operand is a signaling NaN. */
1265 if (HONOR_SNANS (mode)
1266 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1267 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1268 return NULL_TREE;
1270 /* Don't perform operation if it would raise a division
1271 by zero exception. */
1272 if (code == RDIV_EXPR
1273 && real_equal (&d2, &dconst0)
1274 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1275 return NULL_TREE;
1277 /* If either operand is a NaN, just return it. Otherwise, set up
1278 for floating-point trap; we return an overflow. */
1279 if (REAL_VALUE_ISNAN (d1))
1281 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1282 is off. */
1283 d1.signalling = 0;
1284 t = build_real (type, d1);
1285 return t;
1287 else if (REAL_VALUE_ISNAN (d2))
1289 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1290 is off. */
1291 d2.signalling = 0;
1292 t = build_real (type, d2);
1293 return t;
1296 inexact = real_arithmetic (&value, code, &d1, &d2);
1297 real_convert (&result, mode, &value);
1299 /* Don't constant fold this floating point operation if
1300 the result has overflowed and flag_trapping_math. */
1301 if (flag_trapping_math
1302 && MODE_HAS_INFINITIES (mode)
1303 && REAL_VALUE_ISINF (result)
1304 && !REAL_VALUE_ISINF (d1)
1305 && !REAL_VALUE_ISINF (d2))
1306 return NULL_TREE;
1308 /* Don't constant fold this floating point operation if the
1309 result may dependent upon the run-time rounding mode and
1310 flag_rounding_math is set, or if GCC's software emulation
1311 is unable to accurately represent the result. */
1312 if ((flag_rounding_math
1313 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1314 && (inexact || !real_identical (&result, &value)))
1315 return NULL_TREE;
1317 t = build_real (type, result);
1319 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1320 return t;
1323 if (TREE_CODE (arg1) == FIXED_CST)
1325 FIXED_VALUE_TYPE f1;
1326 FIXED_VALUE_TYPE f2;
1327 FIXED_VALUE_TYPE result;
1328 tree t, type;
1329 int sat_p;
1330 bool overflow_p;
1332 /* The following codes are handled by fixed_arithmetic. */
1333 switch (code)
1335 case PLUS_EXPR:
1336 case MINUS_EXPR:
1337 case MULT_EXPR:
1338 case TRUNC_DIV_EXPR:
1339 if (TREE_CODE (arg2) != FIXED_CST)
1340 return NULL_TREE;
1341 f2 = TREE_FIXED_CST (arg2);
1342 break;
1344 case LSHIFT_EXPR:
1345 case RSHIFT_EXPR:
1347 if (TREE_CODE (arg2) != INTEGER_CST)
1348 return NULL_TREE;
1349 wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1350 f2.data.high = w2.elt (1);
1351 f2.data.low = w2.ulow ();
1352 f2.mode = SImode;
1354 break;
1356 default:
1357 return NULL_TREE;
1360 f1 = TREE_FIXED_CST (arg1);
1361 type = TREE_TYPE (arg1);
1362 sat_p = TYPE_SATURATING (type);
1363 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1364 t = build_fixed (type, result);
1365 /* Propagate overflow flags. */
1366 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1367 TREE_OVERFLOW (t) = 1;
1368 return t;
1371 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1373 tree type = TREE_TYPE (arg1);
1374 tree r1 = TREE_REALPART (arg1);
1375 tree i1 = TREE_IMAGPART (arg1);
1376 tree r2 = TREE_REALPART (arg2);
1377 tree i2 = TREE_IMAGPART (arg2);
1378 tree real, imag;
1380 switch (code)
1382 case PLUS_EXPR:
1383 case MINUS_EXPR:
1384 real = const_binop (code, r1, r2);
1385 imag = const_binop (code, i1, i2);
1386 break;
1388 case MULT_EXPR:
1389 if (COMPLEX_FLOAT_TYPE_P (type))
1390 return do_mpc_arg2 (arg1, arg2, type,
1391 /* do_nonfinite= */ folding_initializer,
1392 mpc_mul);
1394 real = const_binop (MINUS_EXPR,
1395 const_binop (MULT_EXPR, r1, r2),
1396 const_binop (MULT_EXPR, i1, i2));
1397 imag = const_binop (PLUS_EXPR,
1398 const_binop (MULT_EXPR, r1, i2),
1399 const_binop (MULT_EXPR, i1, r2));
1400 break;
1402 case RDIV_EXPR:
1403 if (COMPLEX_FLOAT_TYPE_P (type))
1404 return do_mpc_arg2 (arg1, arg2, type,
1405 /* do_nonfinite= */ folding_initializer,
1406 mpc_div);
1407 /* Fallthru. */
1408 case TRUNC_DIV_EXPR:
1409 case CEIL_DIV_EXPR:
1410 case FLOOR_DIV_EXPR:
1411 case ROUND_DIV_EXPR:
1412 if (flag_complex_method == 0)
1414 /* Keep this algorithm in sync with
1415 tree-complex.c:expand_complex_div_straight().
1417 Expand complex division to scalars, straightforward algorithm.
1418 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1419 t = br*br + bi*bi
1421 tree magsquared
1422 = const_binop (PLUS_EXPR,
1423 const_binop (MULT_EXPR, r2, r2),
1424 const_binop (MULT_EXPR, i2, i2));
1425 tree t1
1426 = const_binop (PLUS_EXPR,
1427 const_binop (MULT_EXPR, r1, r2),
1428 const_binop (MULT_EXPR, i1, i2));
1429 tree t2
1430 = const_binop (MINUS_EXPR,
1431 const_binop (MULT_EXPR, i1, r2),
1432 const_binop (MULT_EXPR, r1, i2));
1434 real = const_binop (code, t1, magsquared);
1435 imag = const_binop (code, t2, magsquared);
1437 else
1439 /* Keep this algorithm in sync with
1440 tree-complex.c:expand_complex_div_wide().
1442 Expand complex division to scalars, modified algorithm to minimize
1443 overflow with wide input ranges. */
1444 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1445 fold_abs_const (r2, TREE_TYPE (type)),
1446 fold_abs_const (i2, TREE_TYPE (type)));
1448 if (integer_nonzerop (compare))
1450 /* In the TRUE branch, we compute
1451 ratio = br/bi;
1452 div = (br * ratio) + bi;
1453 tr = (ar * ratio) + ai;
1454 ti = (ai * ratio) - ar;
1455 tr = tr / div;
1456 ti = ti / div; */
1457 tree ratio = const_binop (code, r2, i2);
1458 tree div = const_binop (PLUS_EXPR, i2,
1459 const_binop (MULT_EXPR, r2, ratio));
1460 real = const_binop (MULT_EXPR, r1, ratio);
1461 real = const_binop (PLUS_EXPR, real, i1);
1462 real = const_binop (code, real, div);
1464 imag = const_binop (MULT_EXPR, i1, ratio);
1465 imag = const_binop (MINUS_EXPR, imag, r1);
1466 imag = const_binop (code, imag, div);
1468 else
1470 /* In the FALSE branch, we compute
1471 ratio = d/c;
1472 divisor = (d * ratio) + c;
1473 tr = (b * ratio) + a;
1474 ti = b - (a * ratio);
1475 tr = tr / div;
1476 ti = ti / div; */
1477 tree ratio = const_binop (code, i2, r2);
1478 tree div = const_binop (PLUS_EXPR, r2,
1479 const_binop (MULT_EXPR, i2, ratio));
1481 real = const_binop (MULT_EXPR, i1, ratio);
1482 real = const_binop (PLUS_EXPR, real, r1);
1483 real = const_binop (code, real, div);
1485 imag = const_binop (MULT_EXPR, r1, ratio);
1486 imag = const_binop (MINUS_EXPR, i1, imag);
1487 imag = const_binop (code, imag, div);
1490 break;
1492 default:
1493 return NULL_TREE;
1496 if (real && imag)
1497 return build_complex (type, real, imag);
1500 if (TREE_CODE (arg1) == VECTOR_CST
1501 && TREE_CODE (arg2) == VECTOR_CST
1502 && (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))
1503 == TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1505 tree type = TREE_TYPE (arg1);
1506 bool step_ok_p;
1507 if (VECTOR_CST_STEPPED_P (arg1)
1508 && VECTOR_CST_STEPPED_P (arg2))
1509 /* We can operate directly on the encoding if:
1511 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1512 implies
1513 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1515 Addition and subtraction are the supported operators
1516 for which this is true. */
1517 step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1518 else if (VECTOR_CST_STEPPED_P (arg1))
1519 /* We can operate directly on stepped encodings if:
1521 a3 - a2 == a2 - a1
1522 implies:
1523 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1525 which is true if (x -> x op c) distributes over addition. */
1526 step_ok_p = distributes_over_addition_p (code, 1);
1527 else
1528 /* Similarly in reverse. */
1529 step_ok_p = distributes_over_addition_p (code, 2);
1530 tree_vector_builder elts;
1531 if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1532 return NULL_TREE;
1533 unsigned int count = elts.encoded_nelts ();
1534 for (unsigned int i = 0; i < count; ++i)
1536 tree elem1 = VECTOR_CST_ELT (arg1, i);
1537 tree elem2 = VECTOR_CST_ELT (arg2, i);
1539 tree elt = const_binop (code, elem1, elem2);
1541 /* It is possible that const_binop cannot handle the given
1542 code and return NULL_TREE */
1543 if (elt == NULL_TREE)
1544 return NULL_TREE;
1545 elts.quick_push (elt);
1548 return elts.build ();
1551 /* Shifts allow a scalar offset for a vector. */
1552 if (TREE_CODE (arg1) == VECTOR_CST
1553 && TREE_CODE (arg2) == INTEGER_CST)
1555 tree type = TREE_TYPE (arg1);
1556 bool step_ok_p = distributes_over_addition_p (code, 1);
1557 tree_vector_builder elts;
1558 if (!elts.new_unary_operation (type, arg1, step_ok_p))
1559 return NULL_TREE;
1560 unsigned int count = elts.encoded_nelts ();
1561 for (unsigned int i = 0; i < count; ++i)
1563 tree elem1 = VECTOR_CST_ELT (arg1, i);
1565 tree elt = const_binop (code, elem1, arg2);
1567 /* It is possible that const_binop cannot handle the given
1568 code and return NULL_TREE. */
1569 if (elt == NULL_TREE)
1570 return NULL_TREE;
1571 elts.quick_push (elt);
1574 return elts.build ();
1576 return NULL_TREE;
1579 /* Overload that adds a TYPE parameter to be able to dispatch
1580 to fold_relational_const. */
1582 tree
1583 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1585 if (TREE_CODE_CLASS (code) == tcc_comparison)
1586 return fold_relational_const (code, type, arg1, arg2);
1588 /* ??? Until we make the const_binop worker take the type of the
1589 result as argument put those cases that need it here. */
1590 switch (code)
1592 case VEC_SERIES_EXPR:
1593 if (CONSTANT_CLASS_P (arg1)
1594 && CONSTANT_CLASS_P (arg2))
1595 return build_vec_series (type, arg1, arg2);
1596 return NULL_TREE;
1598 case COMPLEX_EXPR:
1599 if ((TREE_CODE (arg1) == REAL_CST
1600 && TREE_CODE (arg2) == REAL_CST)
1601 || (TREE_CODE (arg1) == INTEGER_CST
1602 && TREE_CODE (arg2) == INTEGER_CST))
1603 return build_complex (type, arg1, arg2);
1604 return NULL_TREE;
1606 case POINTER_DIFF_EXPR:
1607 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1609 offset_int res = wi::sub (wi::to_offset (arg1),
1610 wi::to_offset (arg2));
1611 return force_fit_type (type, res, 1,
1612 TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1614 return NULL_TREE;
1616 case VEC_PACK_TRUNC_EXPR:
1617 case VEC_PACK_FIX_TRUNC_EXPR:
1619 unsigned int out_nelts, in_nelts, i;
1621 if (TREE_CODE (arg1) != VECTOR_CST
1622 || TREE_CODE (arg2) != VECTOR_CST)
1623 return NULL_TREE;
1625 in_nelts = VECTOR_CST_NELTS (arg1);
1626 out_nelts = in_nelts * 2;
1627 gcc_assert (in_nelts == VECTOR_CST_NELTS (arg2)
1628 && out_nelts == TYPE_VECTOR_SUBPARTS (type));
1630 tree_vector_builder elts (type, out_nelts, 1);
1631 for (i = 0; i < out_nelts; i++)
1633 tree elt = (i < in_nelts
1634 ? VECTOR_CST_ELT (arg1, i)
1635 : VECTOR_CST_ELT (arg2, i - in_nelts));
1636 elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1637 ? NOP_EXPR : FIX_TRUNC_EXPR,
1638 TREE_TYPE (type), elt);
1639 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1640 return NULL_TREE;
1641 elts.quick_push (elt);
1644 return elts.build ();
1647 case VEC_WIDEN_MULT_LO_EXPR:
1648 case VEC_WIDEN_MULT_HI_EXPR:
1649 case VEC_WIDEN_MULT_EVEN_EXPR:
1650 case VEC_WIDEN_MULT_ODD_EXPR:
1652 unsigned int out_nelts, in_nelts, out, ofs, scale;
1654 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1655 return NULL_TREE;
1657 in_nelts = VECTOR_CST_NELTS (arg1);
1658 out_nelts = in_nelts / 2;
1659 gcc_assert (in_nelts == VECTOR_CST_NELTS (arg2)
1660 && out_nelts == TYPE_VECTOR_SUBPARTS (type));
1662 if (code == VEC_WIDEN_MULT_LO_EXPR)
1663 scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1664 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1665 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1666 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1667 scale = 1, ofs = 0;
1668 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1669 scale = 1, ofs = 1;
1671 tree_vector_builder elts (type, out_nelts, 1);
1672 for (out = 0; out < out_nelts; out++)
1674 unsigned int in = (out << scale) + ofs;
1675 tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1676 VECTOR_CST_ELT (arg1, in));
1677 tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1678 VECTOR_CST_ELT (arg2, in));
1680 if (t1 == NULL_TREE || t2 == NULL_TREE)
1681 return NULL_TREE;
1682 tree elt = const_binop (MULT_EXPR, t1, t2);
1683 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1684 return NULL_TREE;
1685 elts.quick_push (elt);
1688 return elts.build ();
1691 default:;
1694 if (TREE_CODE_CLASS (code) != tcc_binary)
1695 return NULL_TREE;
1697 /* Make sure type and arg0 have the same saturating flag. */
1698 gcc_checking_assert (TYPE_SATURATING (type)
1699 == TYPE_SATURATING (TREE_TYPE (arg1)));
1701 return const_binop (code, arg1, arg2);
1704 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1705 Return zero if computing the constants is not possible. */
1707 tree
1708 const_unop (enum tree_code code, tree type, tree arg0)
1710 /* Don't perform the operation, other than NEGATE and ABS, if
1711 flag_signaling_nans is on and the operand is a signaling NaN. */
1712 if (TREE_CODE (arg0) == REAL_CST
1713 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1714 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1715 && code != NEGATE_EXPR
1716 && code != ABS_EXPR)
1717 return NULL_TREE;
1719 switch (code)
1721 CASE_CONVERT:
1722 case FLOAT_EXPR:
1723 case FIX_TRUNC_EXPR:
1724 case FIXED_CONVERT_EXPR:
1725 return fold_convert_const (code, type, arg0);
1727 case ADDR_SPACE_CONVERT_EXPR:
1728 /* If the source address is 0, and the source address space
1729 cannot have a valid object at 0, fold to dest type null. */
1730 if (integer_zerop (arg0)
1731 && !(targetm.addr_space.zero_address_valid
1732 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1733 return fold_convert_const (code, type, arg0);
1734 break;
1736 case VIEW_CONVERT_EXPR:
1737 return fold_view_convert_expr (type, arg0);
1739 case NEGATE_EXPR:
1741 /* Can't call fold_negate_const directly here as that doesn't
1742 handle all cases and we might not be able to negate some
1743 constants. */
1744 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1745 if (tem && CONSTANT_CLASS_P (tem))
1746 return tem;
1747 break;
1750 case ABS_EXPR:
1751 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1752 return fold_abs_const (arg0, type);
1753 break;
1755 case CONJ_EXPR:
1756 if (TREE_CODE (arg0) == COMPLEX_CST)
1758 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1759 TREE_TYPE (type));
1760 return build_complex (type, TREE_REALPART (arg0), ipart);
1762 break;
1764 case BIT_NOT_EXPR:
1765 if (TREE_CODE (arg0) == INTEGER_CST)
1766 return fold_not_const (arg0, type);
1767 else if (POLY_INT_CST_P (arg0))
1768 return wide_int_to_tree (type, -poly_int_cst_value (arg0));
1769 /* Perform BIT_NOT_EXPR on each element individually. */
1770 else if (TREE_CODE (arg0) == VECTOR_CST)
1772 tree elem;
1774 /* This can cope with stepped encodings because ~x == -1 - x. */
1775 tree_vector_builder elements;
1776 elements.new_unary_operation (type, arg0, true);
1777 unsigned int i, count = elements.encoded_nelts ();
1778 for (i = 0; i < count; ++i)
1780 elem = VECTOR_CST_ELT (arg0, i);
1781 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1782 if (elem == NULL_TREE)
1783 break;
1784 elements.quick_push (elem);
1786 if (i == count)
1787 return elements.build ();
1789 break;
1791 case TRUTH_NOT_EXPR:
1792 if (TREE_CODE (arg0) == INTEGER_CST)
1793 return constant_boolean_node (integer_zerop (arg0), type);
1794 break;
1796 case REALPART_EXPR:
1797 if (TREE_CODE (arg0) == COMPLEX_CST)
1798 return fold_convert (type, TREE_REALPART (arg0));
1799 break;
1801 case IMAGPART_EXPR:
1802 if (TREE_CODE (arg0) == COMPLEX_CST)
1803 return fold_convert (type, TREE_IMAGPART (arg0));
1804 break;
1806 case VEC_UNPACK_LO_EXPR:
1807 case VEC_UNPACK_HI_EXPR:
1808 case VEC_UNPACK_FLOAT_LO_EXPR:
1809 case VEC_UNPACK_FLOAT_HI_EXPR:
1811 unsigned int out_nelts, in_nelts, i;
1812 enum tree_code subcode;
1814 if (TREE_CODE (arg0) != VECTOR_CST)
1815 return NULL_TREE;
1817 in_nelts = VECTOR_CST_NELTS (arg0);
1818 out_nelts = in_nelts / 2;
1819 gcc_assert (out_nelts == TYPE_VECTOR_SUBPARTS (type));
1821 unsigned int offset = 0;
1822 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1823 || code == VEC_UNPACK_FLOAT_LO_EXPR))
1824 offset = out_nelts;
1826 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1827 subcode = NOP_EXPR;
1828 else
1829 subcode = FLOAT_EXPR;
1831 tree_vector_builder elts (type, out_nelts, 1);
1832 for (i = 0; i < out_nelts; i++)
1834 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1835 VECTOR_CST_ELT (arg0, i + offset));
1836 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1837 return NULL_TREE;
1838 elts.quick_push (elt);
1841 return elts.build ();
1844 case VEC_DUPLICATE_EXPR:
1845 if (CONSTANT_CLASS_P (arg0))
1846 return build_vector_from_val (type, arg0);
1847 return NULL_TREE;
1849 default:
1850 break;
1853 return NULL_TREE;
1856 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1857 indicates which particular sizetype to create. */
1859 tree
1860 size_int_kind (poly_int64 number, enum size_type_kind kind)
1862 return build_int_cst (sizetype_tab[(int) kind], number);
1865 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1866 is a tree code. The type of the result is taken from the operands.
1867 Both must be equivalent integer types, ala int_binop_types_match_p.
1868 If the operands are constant, so is the result. */
1870 tree
1871 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1873 tree type = TREE_TYPE (arg0);
1875 if (arg0 == error_mark_node || arg1 == error_mark_node)
1876 return error_mark_node;
1878 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1879 TREE_TYPE (arg1)));
1881 /* Handle the special case of two poly_int constants faster. */
1882 if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1884 /* And some specific cases even faster than that. */
1885 if (code == PLUS_EXPR)
1887 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1888 return arg1;
1889 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1890 return arg0;
1892 else if (code == MINUS_EXPR)
1894 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1895 return arg0;
1897 else if (code == MULT_EXPR)
1899 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1900 return arg1;
1903 /* Handle general case of two integer constants. For sizetype
1904 constant calculations we always want to know about overflow,
1905 even in the unsigned case. */
1906 tree res = int_const_binop_1 (code, arg0, arg1, -1);
1907 if (res != NULL_TREE)
1908 return res;
1911 return fold_build2_loc (loc, code, type, arg0, arg1);
1914 /* Given two values, either both of sizetype or both of bitsizetype,
1915 compute the difference between the two values. Return the value
1916 in signed type corresponding to the type of the operands. */
1918 tree
1919 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1921 tree type = TREE_TYPE (arg0);
1922 tree ctype;
1924 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1925 TREE_TYPE (arg1)));
1927 /* If the type is already signed, just do the simple thing. */
1928 if (!TYPE_UNSIGNED (type))
1929 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1931 if (type == sizetype)
1932 ctype = ssizetype;
1933 else if (type == bitsizetype)
1934 ctype = sbitsizetype;
1935 else
1936 ctype = signed_type_for (type);
1938 /* If either operand is not a constant, do the conversions to the signed
1939 type and subtract. The hardware will do the right thing with any
1940 overflow in the subtraction. */
1941 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1942 return size_binop_loc (loc, MINUS_EXPR,
1943 fold_convert_loc (loc, ctype, arg0),
1944 fold_convert_loc (loc, ctype, arg1));
1946 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1947 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1948 overflow) and negate (which can't either). Special-case a result
1949 of zero while we're here. */
1950 if (tree_int_cst_equal (arg0, arg1))
1951 return build_int_cst (ctype, 0);
1952 else if (tree_int_cst_lt (arg1, arg0))
1953 return fold_convert_loc (loc, ctype,
1954 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1955 else
1956 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1957 fold_convert_loc (loc, ctype,
1958 size_binop_loc (loc,
1959 MINUS_EXPR,
1960 arg1, arg0)));
1963 /* A subroutine of fold_convert_const handling conversions of an
1964 INTEGER_CST to another integer type. */
1966 static tree
1967 fold_convert_const_int_from_int (tree type, const_tree arg1)
1969 /* Given an integer constant, make new constant with new type,
1970 appropriately sign-extended or truncated. Use widest_int
1971 so that any extension is done according ARG1's type. */
1972 return force_fit_type (type, wi::to_widest (arg1),
1973 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1974 TREE_OVERFLOW (arg1));
1977 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1978 to an integer type. */
1980 static tree
1981 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
1983 bool overflow = false;
1984 tree t;
1986 /* The following code implements the floating point to integer
1987 conversion rules required by the Java Language Specification,
1988 that IEEE NaNs are mapped to zero and values that overflow
1989 the target precision saturate, i.e. values greater than
1990 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1991 are mapped to INT_MIN. These semantics are allowed by the
1992 C and C++ standards that simply state that the behavior of
1993 FP-to-integer conversion is unspecified upon overflow. */
1995 wide_int val;
1996 REAL_VALUE_TYPE r;
1997 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1999 switch (code)
2001 case FIX_TRUNC_EXPR:
2002 real_trunc (&r, VOIDmode, &x);
2003 break;
2005 default:
2006 gcc_unreachable ();
2009 /* If R is NaN, return zero and show we have an overflow. */
2010 if (REAL_VALUE_ISNAN (r))
2012 overflow = true;
2013 val = wi::zero (TYPE_PRECISION (type));
2016 /* See if R is less than the lower bound or greater than the
2017 upper bound. */
2019 if (! overflow)
2021 tree lt = TYPE_MIN_VALUE (type);
2022 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2023 if (real_less (&r, &l))
2025 overflow = true;
2026 val = wi::to_wide (lt);
2030 if (! overflow)
2032 tree ut = TYPE_MAX_VALUE (type);
2033 if (ut)
2035 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2036 if (real_less (&u, &r))
2038 overflow = true;
2039 val = wi::to_wide (ut);
2044 if (! overflow)
2045 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2047 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2048 return t;
2051 /* A subroutine of fold_convert_const handling conversions of a
2052 FIXED_CST to an integer type. */
2054 static tree
2055 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2057 tree t;
2058 double_int temp, temp_trunc;
2059 scalar_mode mode;
2061 /* Right shift FIXED_CST to temp by fbit. */
2062 temp = TREE_FIXED_CST (arg1).data;
2063 mode = TREE_FIXED_CST (arg1).mode;
2064 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2066 temp = temp.rshift (GET_MODE_FBIT (mode),
2067 HOST_BITS_PER_DOUBLE_INT,
2068 SIGNED_FIXED_POINT_MODE_P (mode));
2070 /* Left shift temp to temp_trunc by fbit. */
2071 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2072 HOST_BITS_PER_DOUBLE_INT,
2073 SIGNED_FIXED_POINT_MODE_P (mode));
2075 else
2077 temp = double_int_zero;
2078 temp_trunc = double_int_zero;
2081 /* If FIXED_CST is negative, we need to round the value toward 0.
2082 By checking if the fractional bits are not zero to add 1 to temp. */
2083 if (SIGNED_FIXED_POINT_MODE_P (mode)
2084 && temp_trunc.is_negative ()
2085 && TREE_FIXED_CST (arg1).data != temp_trunc)
2086 temp += double_int_one;
2088 /* Given a fixed-point constant, make new constant with new type,
2089 appropriately sign-extended or truncated. */
2090 t = force_fit_type (type, temp, -1,
2091 (temp.is_negative ()
2092 && (TYPE_UNSIGNED (type)
2093 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2094 | TREE_OVERFLOW (arg1));
2096 return t;
2099 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2100 to another floating point type. */
2102 static tree
2103 fold_convert_const_real_from_real (tree type, const_tree arg1)
2105 REAL_VALUE_TYPE value;
2106 tree t;
2108 /* Don't perform the operation if flag_signaling_nans is on
2109 and the operand is a signaling NaN. */
2110 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2111 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2112 return NULL_TREE;
2114 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2115 t = build_real (type, value);
2117 /* If converting an infinity or NAN to a representation that doesn't
2118 have one, set the overflow bit so that we can produce some kind of
2119 error message at the appropriate point if necessary. It's not the
2120 most user-friendly message, but it's better than nothing. */
2121 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2122 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2123 TREE_OVERFLOW (t) = 1;
2124 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2125 && !MODE_HAS_NANS (TYPE_MODE (type)))
2126 TREE_OVERFLOW (t) = 1;
2127 /* Regular overflow, conversion produced an infinity in a mode that
2128 can't represent them. */
2129 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2130 && REAL_VALUE_ISINF (value)
2131 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2132 TREE_OVERFLOW (t) = 1;
2133 else
2134 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2135 return t;
2138 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2139 to a floating point type. */
2141 static tree
2142 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2144 REAL_VALUE_TYPE value;
2145 tree t;
2147 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2148 &TREE_FIXED_CST (arg1));
2149 t = build_real (type, value);
2151 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2152 return t;
2155 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2156 to another fixed-point type. */
2158 static tree
2159 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2161 FIXED_VALUE_TYPE value;
2162 tree t;
2163 bool overflow_p;
2165 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2166 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2167 t = build_fixed (type, value);
2169 /* Propagate overflow flags. */
2170 if (overflow_p | TREE_OVERFLOW (arg1))
2171 TREE_OVERFLOW (t) = 1;
2172 return t;
2175 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2176 to a fixed-point type. */
2178 static tree
2179 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2181 FIXED_VALUE_TYPE value;
2182 tree t;
2183 bool overflow_p;
2184 double_int di;
2186 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2188 di.low = TREE_INT_CST_ELT (arg1, 0);
2189 if (TREE_INT_CST_NUNITS (arg1) == 1)
2190 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2191 else
2192 di.high = TREE_INT_CST_ELT (arg1, 1);
2194 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2195 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2196 TYPE_SATURATING (type));
2197 t = build_fixed (type, value);
2199 /* Propagate overflow flags. */
2200 if (overflow_p | TREE_OVERFLOW (arg1))
2201 TREE_OVERFLOW (t) = 1;
2202 return t;
2205 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2206 to a fixed-point type. */
2208 static tree
2209 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2211 FIXED_VALUE_TYPE value;
2212 tree t;
2213 bool overflow_p;
2215 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2216 &TREE_REAL_CST (arg1),
2217 TYPE_SATURATING (type));
2218 t = build_fixed (type, value);
2220 /* Propagate overflow flags. */
2221 if (overflow_p | TREE_OVERFLOW (arg1))
2222 TREE_OVERFLOW (t) = 1;
2223 return t;
2226 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2227 type TYPE. If no simplification can be done return NULL_TREE. */
2229 static tree
2230 fold_convert_const (enum tree_code code, tree type, tree arg1)
2232 tree arg_type = TREE_TYPE (arg1);
2233 if (arg_type == type)
2234 return arg1;
2236 /* We can't widen types, since the runtime value could overflow the
2237 original type before being extended to the new type. */
2238 if (POLY_INT_CST_P (arg1)
2239 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2240 && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2241 return build_poly_int_cst (type,
2242 poly_wide_int::from (poly_int_cst_value (arg1),
2243 TYPE_PRECISION (type),
2244 TYPE_SIGN (arg_type)));
2246 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2247 || TREE_CODE (type) == OFFSET_TYPE)
2249 if (TREE_CODE (arg1) == INTEGER_CST)
2250 return fold_convert_const_int_from_int (type, arg1);
2251 else if (TREE_CODE (arg1) == REAL_CST)
2252 return fold_convert_const_int_from_real (code, type, arg1);
2253 else if (TREE_CODE (arg1) == FIXED_CST)
2254 return fold_convert_const_int_from_fixed (type, arg1);
2256 else if (TREE_CODE (type) == REAL_TYPE)
2258 if (TREE_CODE (arg1) == INTEGER_CST)
2259 return build_real_from_int_cst (type, arg1);
2260 else if (TREE_CODE (arg1) == REAL_CST)
2261 return fold_convert_const_real_from_real (type, arg1);
2262 else if (TREE_CODE (arg1) == FIXED_CST)
2263 return fold_convert_const_real_from_fixed (type, arg1);
2265 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2267 if (TREE_CODE (arg1) == FIXED_CST)
2268 return fold_convert_const_fixed_from_fixed (type, arg1);
2269 else if (TREE_CODE (arg1) == INTEGER_CST)
2270 return fold_convert_const_fixed_from_int (type, arg1);
2271 else if (TREE_CODE (arg1) == REAL_CST)
2272 return fold_convert_const_fixed_from_real (type, arg1);
2274 else if (TREE_CODE (type) == VECTOR_TYPE)
2276 if (TREE_CODE (arg1) == VECTOR_CST
2277 && TYPE_VECTOR_SUBPARTS (type) == VECTOR_CST_NELTS (arg1))
2279 tree elttype = TREE_TYPE (type);
2280 tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2281 /* We can't handle steps directly when extending, since the
2282 values need to wrap at the original precision first. */
2283 bool step_ok_p
2284 = (INTEGRAL_TYPE_P (elttype)
2285 && INTEGRAL_TYPE_P (arg1_elttype)
2286 && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2287 tree_vector_builder v;
2288 if (!v.new_unary_operation (type, arg1, step_ok_p))
2289 return NULL_TREE;
2290 unsigned int len = v.encoded_nelts ();
2291 for (unsigned int i = 0; i < len; ++i)
2293 tree elt = VECTOR_CST_ELT (arg1, i);
2294 tree cvt = fold_convert_const (code, elttype, elt);
2295 if (cvt == NULL_TREE)
2296 return NULL_TREE;
2297 v.quick_push (cvt);
2299 return v.build ();
2302 return NULL_TREE;
2305 /* Construct a vector of zero elements of vector type TYPE. */
2307 static tree
2308 build_zero_vector (tree type)
2310 tree t;
2312 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2313 return build_vector_from_val (type, t);
2316 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2318 bool
2319 fold_convertible_p (const_tree type, const_tree arg)
2321 tree orig = TREE_TYPE (arg);
2323 if (type == orig)
2324 return true;
2326 if (TREE_CODE (arg) == ERROR_MARK
2327 || TREE_CODE (type) == ERROR_MARK
2328 || TREE_CODE (orig) == ERROR_MARK)
2329 return false;
2331 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2332 return true;
2334 switch (TREE_CODE (type))
2336 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2337 case POINTER_TYPE: case REFERENCE_TYPE:
2338 case OFFSET_TYPE:
2339 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2340 || TREE_CODE (orig) == OFFSET_TYPE);
2342 case REAL_TYPE:
2343 case FIXED_POINT_TYPE:
2344 case VECTOR_TYPE:
2345 case VOID_TYPE:
2346 return TREE_CODE (type) == TREE_CODE (orig);
2348 default:
2349 return false;
2353 /* Convert expression ARG to type TYPE. Used by the middle-end for
2354 simple conversions in preference to calling the front-end's convert. */
2356 tree
2357 fold_convert_loc (location_t loc, tree type, tree arg)
2359 tree orig = TREE_TYPE (arg);
2360 tree tem;
2362 if (type == orig)
2363 return arg;
2365 if (TREE_CODE (arg) == ERROR_MARK
2366 || TREE_CODE (type) == ERROR_MARK
2367 || TREE_CODE (orig) == ERROR_MARK)
2368 return error_mark_node;
2370 switch (TREE_CODE (type))
2372 case POINTER_TYPE:
2373 case REFERENCE_TYPE:
2374 /* Handle conversions between pointers to different address spaces. */
2375 if (POINTER_TYPE_P (orig)
2376 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2377 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2378 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2379 /* fall through */
2381 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2382 case OFFSET_TYPE:
2383 if (TREE_CODE (arg) == INTEGER_CST)
2385 tem = fold_convert_const (NOP_EXPR, type, arg);
2386 if (tem != NULL_TREE)
2387 return tem;
2389 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2390 || TREE_CODE (orig) == OFFSET_TYPE)
2391 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2392 if (TREE_CODE (orig) == COMPLEX_TYPE)
2393 return fold_convert_loc (loc, type,
2394 fold_build1_loc (loc, REALPART_EXPR,
2395 TREE_TYPE (orig), arg));
2396 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2397 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2398 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2400 case REAL_TYPE:
2401 if (TREE_CODE (arg) == INTEGER_CST)
2403 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2404 if (tem != NULL_TREE)
2405 return tem;
2407 else if (TREE_CODE (arg) == REAL_CST)
2409 tem = fold_convert_const (NOP_EXPR, type, arg);
2410 if (tem != NULL_TREE)
2411 return tem;
2413 else if (TREE_CODE (arg) == FIXED_CST)
2415 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2416 if (tem != NULL_TREE)
2417 return tem;
2420 switch (TREE_CODE (orig))
2422 case INTEGER_TYPE:
2423 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2424 case POINTER_TYPE: case REFERENCE_TYPE:
2425 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2427 case REAL_TYPE:
2428 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2430 case FIXED_POINT_TYPE:
2431 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2433 case COMPLEX_TYPE:
2434 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2435 return fold_convert_loc (loc, type, tem);
2437 default:
2438 gcc_unreachable ();
2441 case FIXED_POINT_TYPE:
2442 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2443 || TREE_CODE (arg) == REAL_CST)
2445 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2446 if (tem != NULL_TREE)
2447 goto fold_convert_exit;
2450 switch (TREE_CODE (orig))
2452 case FIXED_POINT_TYPE:
2453 case INTEGER_TYPE:
2454 case ENUMERAL_TYPE:
2455 case BOOLEAN_TYPE:
2456 case REAL_TYPE:
2457 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2459 case COMPLEX_TYPE:
2460 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2461 return fold_convert_loc (loc, type, tem);
2463 default:
2464 gcc_unreachable ();
2467 case COMPLEX_TYPE:
2468 switch (TREE_CODE (orig))
2470 case INTEGER_TYPE:
2471 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2472 case POINTER_TYPE: case REFERENCE_TYPE:
2473 case REAL_TYPE:
2474 case FIXED_POINT_TYPE:
2475 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2476 fold_convert_loc (loc, TREE_TYPE (type), arg),
2477 fold_convert_loc (loc, TREE_TYPE (type),
2478 integer_zero_node));
2479 case COMPLEX_TYPE:
2481 tree rpart, ipart;
2483 if (TREE_CODE (arg) == COMPLEX_EXPR)
2485 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2486 TREE_OPERAND (arg, 0));
2487 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2488 TREE_OPERAND (arg, 1));
2489 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2492 arg = save_expr (arg);
2493 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2494 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2495 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2496 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2497 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2500 default:
2501 gcc_unreachable ();
2504 case VECTOR_TYPE:
2505 if (integer_zerop (arg))
2506 return build_zero_vector (type);
2507 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2508 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2509 || TREE_CODE (orig) == VECTOR_TYPE);
2510 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2512 case VOID_TYPE:
2513 tem = fold_ignored_result (arg);
2514 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2516 default:
2517 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2518 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2519 gcc_unreachable ();
2521 fold_convert_exit:
2522 protected_set_expr_location_unshare (tem, loc);
2523 return tem;
2526 /* Return false if expr can be assumed not to be an lvalue, true
2527 otherwise. */
2529 static bool
2530 maybe_lvalue_p (const_tree x)
2532 /* We only need to wrap lvalue tree codes. */
2533 switch (TREE_CODE (x))
2535 case VAR_DECL:
2536 case PARM_DECL:
2537 case RESULT_DECL:
2538 case LABEL_DECL:
2539 case FUNCTION_DECL:
2540 case SSA_NAME:
2542 case COMPONENT_REF:
2543 case MEM_REF:
2544 case INDIRECT_REF:
2545 case ARRAY_REF:
2546 case ARRAY_RANGE_REF:
2547 case BIT_FIELD_REF:
2548 case OBJ_TYPE_REF:
2550 case REALPART_EXPR:
2551 case IMAGPART_EXPR:
2552 case PREINCREMENT_EXPR:
2553 case PREDECREMENT_EXPR:
2554 case SAVE_EXPR:
2555 case TRY_CATCH_EXPR:
2556 case WITH_CLEANUP_EXPR:
2557 case COMPOUND_EXPR:
2558 case MODIFY_EXPR:
2559 case TARGET_EXPR:
2560 case COND_EXPR:
2561 case BIND_EXPR:
2562 break;
2564 default:
2565 /* Assume the worst for front-end tree codes. */
2566 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2567 break;
2568 return false;
2571 return true;
2574 /* Return an expr equal to X but certainly not valid as an lvalue. */
2576 tree
2577 non_lvalue_loc (location_t loc, tree x)
2579 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2580 us. */
2581 if (in_gimple_form)
2582 return x;
2584 if (! maybe_lvalue_p (x))
2585 return x;
2586 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2589 /* When pedantic, return an expr equal to X but certainly not valid as a
2590 pedantic lvalue. Otherwise, return X. */
2592 static tree
2593 pedantic_non_lvalue_loc (location_t loc, tree x)
2595 return protected_set_expr_location_unshare (x, loc);
2598 /* Given a tree comparison code, return the code that is the logical inverse.
2599 It is generally not safe to do this for floating-point comparisons, except
2600 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2601 ERROR_MARK in this case. */
2603 enum tree_code
2604 invert_tree_comparison (enum tree_code code, bool honor_nans)
2606 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2607 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2608 return ERROR_MARK;
2610 switch (code)
2612 case EQ_EXPR:
2613 return NE_EXPR;
2614 case NE_EXPR:
2615 return EQ_EXPR;
2616 case GT_EXPR:
2617 return honor_nans ? UNLE_EXPR : LE_EXPR;
2618 case GE_EXPR:
2619 return honor_nans ? UNLT_EXPR : LT_EXPR;
2620 case LT_EXPR:
2621 return honor_nans ? UNGE_EXPR : GE_EXPR;
2622 case LE_EXPR:
2623 return honor_nans ? UNGT_EXPR : GT_EXPR;
2624 case LTGT_EXPR:
2625 return UNEQ_EXPR;
2626 case UNEQ_EXPR:
2627 return LTGT_EXPR;
2628 case UNGT_EXPR:
2629 return LE_EXPR;
2630 case UNGE_EXPR:
2631 return LT_EXPR;
2632 case UNLT_EXPR:
2633 return GE_EXPR;
2634 case UNLE_EXPR:
2635 return GT_EXPR;
2636 case ORDERED_EXPR:
2637 return UNORDERED_EXPR;
2638 case UNORDERED_EXPR:
2639 return ORDERED_EXPR;
2640 default:
2641 gcc_unreachable ();
2645 /* Similar, but return the comparison that results if the operands are
2646 swapped. This is safe for floating-point. */
2648 enum tree_code
2649 swap_tree_comparison (enum tree_code code)
2651 switch (code)
2653 case EQ_EXPR:
2654 case NE_EXPR:
2655 case ORDERED_EXPR:
2656 case UNORDERED_EXPR:
2657 case LTGT_EXPR:
2658 case UNEQ_EXPR:
2659 return code;
2660 case GT_EXPR:
2661 return LT_EXPR;
2662 case GE_EXPR:
2663 return LE_EXPR;
2664 case LT_EXPR:
2665 return GT_EXPR;
2666 case LE_EXPR:
2667 return GE_EXPR;
2668 case UNGT_EXPR:
2669 return UNLT_EXPR;
2670 case UNGE_EXPR:
2671 return UNLE_EXPR;
2672 case UNLT_EXPR:
2673 return UNGT_EXPR;
2674 case UNLE_EXPR:
2675 return UNGE_EXPR;
2676 default:
2677 gcc_unreachable ();
2682 /* Convert a comparison tree code from an enum tree_code representation
2683 into a compcode bit-based encoding. This function is the inverse of
2684 compcode_to_comparison. */
2686 static enum comparison_code
2687 comparison_to_compcode (enum tree_code code)
2689 switch (code)
2691 case LT_EXPR:
2692 return COMPCODE_LT;
2693 case EQ_EXPR:
2694 return COMPCODE_EQ;
2695 case LE_EXPR:
2696 return COMPCODE_LE;
2697 case GT_EXPR:
2698 return COMPCODE_GT;
2699 case NE_EXPR:
2700 return COMPCODE_NE;
2701 case GE_EXPR:
2702 return COMPCODE_GE;
2703 case ORDERED_EXPR:
2704 return COMPCODE_ORD;
2705 case UNORDERED_EXPR:
2706 return COMPCODE_UNORD;
2707 case UNLT_EXPR:
2708 return COMPCODE_UNLT;
2709 case UNEQ_EXPR:
2710 return COMPCODE_UNEQ;
2711 case UNLE_EXPR:
2712 return COMPCODE_UNLE;
2713 case UNGT_EXPR:
2714 return COMPCODE_UNGT;
2715 case LTGT_EXPR:
2716 return COMPCODE_LTGT;
2717 case UNGE_EXPR:
2718 return COMPCODE_UNGE;
2719 default:
2720 gcc_unreachable ();
2724 /* Convert a compcode bit-based encoding of a comparison operator back
2725 to GCC's enum tree_code representation. This function is the
2726 inverse of comparison_to_compcode. */
2728 static enum tree_code
2729 compcode_to_comparison (enum comparison_code code)
2731 switch (code)
2733 case COMPCODE_LT:
2734 return LT_EXPR;
2735 case COMPCODE_EQ:
2736 return EQ_EXPR;
2737 case COMPCODE_LE:
2738 return LE_EXPR;
2739 case COMPCODE_GT:
2740 return GT_EXPR;
2741 case COMPCODE_NE:
2742 return NE_EXPR;
2743 case COMPCODE_GE:
2744 return GE_EXPR;
2745 case COMPCODE_ORD:
2746 return ORDERED_EXPR;
2747 case COMPCODE_UNORD:
2748 return UNORDERED_EXPR;
2749 case COMPCODE_UNLT:
2750 return UNLT_EXPR;
2751 case COMPCODE_UNEQ:
2752 return UNEQ_EXPR;
2753 case COMPCODE_UNLE:
2754 return UNLE_EXPR;
2755 case COMPCODE_UNGT:
2756 return UNGT_EXPR;
2757 case COMPCODE_LTGT:
2758 return LTGT_EXPR;
2759 case COMPCODE_UNGE:
2760 return UNGE_EXPR;
2761 default:
2762 gcc_unreachable ();
2766 /* Return a tree for the comparison which is the combination of
2767 doing the AND or OR (depending on CODE) of the two operations LCODE
2768 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2769 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2770 if this makes the transformation invalid. */
2772 tree
2773 combine_comparisons (location_t loc,
2774 enum tree_code code, enum tree_code lcode,
2775 enum tree_code rcode, tree truth_type,
2776 tree ll_arg, tree lr_arg)
2778 bool honor_nans = HONOR_NANS (ll_arg);
2779 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2780 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2781 int compcode;
2783 switch (code)
2785 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2786 compcode = lcompcode & rcompcode;
2787 break;
2789 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2790 compcode = lcompcode | rcompcode;
2791 break;
2793 default:
2794 return NULL_TREE;
2797 if (!honor_nans)
2799 /* Eliminate unordered comparisons, as well as LTGT and ORD
2800 which are not used unless the mode has NaNs. */
2801 compcode &= ~COMPCODE_UNORD;
2802 if (compcode == COMPCODE_LTGT)
2803 compcode = COMPCODE_NE;
2804 else if (compcode == COMPCODE_ORD)
2805 compcode = COMPCODE_TRUE;
2807 else if (flag_trapping_math)
2809 /* Check that the original operation and the optimized ones will trap
2810 under the same condition. */
2811 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2812 && (lcompcode != COMPCODE_EQ)
2813 && (lcompcode != COMPCODE_ORD);
2814 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2815 && (rcompcode != COMPCODE_EQ)
2816 && (rcompcode != COMPCODE_ORD);
2817 bool trap = (compcode & COMPCODE_UNORD) == 0
2818 && (compcode != COMPCODE_EQ)
2819 && (compcode != COMPCODE_ORD);
2821 /* In a short-circuited boolean expression the LHS might be
2822 such that the RHS, if evaluated, will never trap. For
2823 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2824 if neither x nor y is NaN. (This is a mixed blessing: for
2825 example, the expression above will never trap, hence
2826 optimizing it to x < y would be invalid). */
2827 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2828 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2829 rtrap = false;
2831 /* If the comparison was short-circuited, and only the RHS
2832 trapped, we may now generate a spurious trap. */
2833 if (rtrap && !ltrap
2834 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2835 return NULL_TREE;
2837 /* If we changed the conditions that cause a trap, we lose. */
2838 if ((ltrap || rtrap) != trap)
2839 return NULL_TREE;
2842 if (compcode == COMPCODE_TRUE)
2843 return constant_boolean_node (true, truth_type);
2844 else if (compcode == COMPCODE_FALSE)
2845 return constant_boolean_node (false, truth_type);
2846 else
2848 enum tree_code tcode;
2850 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2851 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2855 /* Return nonzero if two operands (typically of the same tree node)
2856 are necessarily equal. FLAGS modifies behavior as follows:
2858 If OEP_ONLY_CONST is set, only return nonzero for constants.
2859 This function tests whether the operands are indistinguishable;
2860 it does not test whether they are equal using C's == operation.
2861 The distinction is important for IEEE floating point, because
2862 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2863 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2865 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2866 even though it may hold multiple values during a function.
2867 This is because a GCC tree node guarantees that nothing else is
2868 executed between the evaluation of its "operands" (which may often
2869 be evaluated in arbitrary order). Hence if the operands themselves
2870 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2871 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2872 unset means assuming isochronic (or instantaneous) tree equivalence.
2873 Unless comparing arbitrary expression trees, such as from different
2874 statements, this flag can usually be left unset.
2876 If OEP_PURE_SAME is set, then pure functions with identical arguments
2877 are considered the same. It is used when the caller has other ways
2878 to ensure that global memory is unchanged in between.
2880 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2881 not values of expressions.
2883 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2884 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2886 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2887 any operand with side effect. This is unnecesarily conservative in the
2888 case we know that arg0 and arg1 are in disjoint code paths (such as in
2889 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2890 addresses with TREE_CONSTANT flag set so we know that &var == &var
2891 even if var is volatile. */
2894 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2896 /* When checking, verify at the outermost operand_equal_p call that
2897 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2898 hash value. */
2899 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2901 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2903 if (arg0 != arg1)
2905 inchash::hash hstate0 (0), hstate1 (0);
2906 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2907 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2908 hashval_t h0 = hstate0.end ();
2909 hashval_t h1 = hstate1.end ();
2910 gcc_assert (h0 == h1);
2912 return 1;
2914 else
2915 return 0;
2918 /* If either is ERROR_MARK, they aren't equal. */
2919 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2920 || TREE_TYPE (arg0) == error_mark_node
2921 || TREE_TYPE (arg1) == error_mark_node)
2922 return 0;
2924 /* Similar, if either does not have a type (like a released SSA name),
2925 they aren't equal. */
2926 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2927 return 0;
2929 /* We cannot consider pointers to different address space equal. */
2930 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2931 && POINTER_TYPE_P (TREE_TYPE (arg1))
2932 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2933 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2934 return 0;
2936 /* Check equality of integer constants before bailing out due to
2937 precision differences. */
2938 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2940 /* Address of INTEGER_CST is not defined; check that we did not forget
2941 to drop the OEP_ADDRESS_OF flags. */
2942 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2943 return tree_int_cst_equal (arg0, arg1);
2946 if (!(flags & OEP_ADDRESS_OF))
2948 /* If both types don't have the same signedness, then we can't consider
2949 them equal. We must check this before the STRIP_NOPS calls
2950 because they may change the signedness of the arguments. As pointers
2951 strictly don't have a signedness, require either two pointers or
2952 two non-pointers as well. */
2953 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2954 || POINTER_TYPE_P (TREE_TYPE (arg0))
2955 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2956 return 0;
2958 /* If both types don't have the same precision, then it is not safe
2959 to strip NOPs. */
2960 if (element_precision (TREE_TYPE (arg0))
2961 != element_precision (TREE_TYPE (arg1)))
2962 return 0;
2964 STRIP_NOPS (arg0);
2965 STRIP_NOPS (arg1);
2967 #if 0
2968 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2969 sanity check once the issue is solved. */
2970 else
2971 /* Addresses of conversions and SSA_NAMEs (and many other things)
2972 are not defined. Check that we did not forget to drop the
2973 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2974 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2975 && TREE_CODE (arg0) != SSA_NAME);
2976 #endif
2978 /* In case both args are comparisons but with different comparison
2979 code, try to swap the comparison operands of one arg to produce
2980 a match and compare that variant. */
2981 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2982 && COMPARISON_CLASS_P (arg0)
2983 && COMPARISON_CLASS_P (arg1))
2985 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
2987 if (TREE_CODE (arg0) == swap_code)
2988 return operand_equal_p (TREE_OPERAND (arg0, 0),
2989 TREE_OPERAND (arg1, 1), flags)
2990 && operand_equal_p (TREE_OPERAND (arg0, 1),
2991 TREE_OPERAND (arg1, 0), flags);
2994 if (TREE_CODE (arg0) != TREE_CODE (arg1))
2996 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
2997 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
2999 else if (flags & OEP_ADDRESS_OF)
3001 /* If we are interested in comparing addresses ignore
3002 MEM_REF wrappings of the base that can appear just for
3003 TBAA reasons. */
3004 if (TREE_CODE (arg0) == MEM_REF
3005 && DECL_P (arg1)
3006 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3007 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3008 && integer_zerop (TREE_OPERAND (arg0, 1)))
3009 return 1;
3010 else if (TREE_CODE (arg1) == MEM_REF
3011 && DECL_P (arg0)
3012 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3013 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3014 && integer_zerop (TREE_OPERAND (arg1, 1)))
3015 return 1;
3016 return 0;
3018 else
3019 return 0;
3022 /* When not checking adddresses, this is needed for conversions and for
3023 COMPONENT_REF. Might as well play it safe and always test this. */
3024 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3025 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3026 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
3027 && !(flags & OEP_ADDRESS_OF)))
3028 return 0;
3030 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3031 We don't care about side effects in that case because the SAVE_EXPR
3032 takes care of that for us. In all other cases, two expressions are
3033 equal if they have no side effects. If we have two identical
3034 expressions with side effects that should be treated the same due
3035 to the only side effects being identical SAVE_EXPR's, that will
3036 be detected in the recursive calls below.
3037 If we are taking an invariant address of two identical objects
3038 they are necessarily equal as well. */
3039 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3040 && (TREE_CODE (arg0) == SAVE_EXPR
3041 || (flags & OEP_MATCH_SIDE_EFFECTS)
3042 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3043 return 1;
3045 /* Next handle constant cases, those for which we can return 1 even
3046 if ONLY_CONST is set. */
3047 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3048 switch (TREE_CODE (arg0))
3050 case INTEGER_CST:
3051 return tree_int_cst_equal (arg0, arg1);
3053 case FIXED_CST:
3054 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3055 TREE_FIXED_CST (arg1));
3057 case REAL_CST:
3058 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3059 return 1;
3062 if (!HONOR_SIGNED_ZEROS (arg0))
3064 /* If we do not distinguish between signed and unsigned zero,
3065 consider them equal. */
3066 if (real_zerop (arg0) && real_zerop (arg1))
3067 return 1;
3069 return 0;
3071 case VECTOR_CST:
3073 if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3074 != VECTOR_CST_LOG2_NPATTERNS (arg1))
3075 return 0;
3077 if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3078 != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3079 return 0;
3081 unsigned int count = vector_cst_encoded_nelts (arg0);
3082 for (unsigned int i = 0; i < count; ++i)
3083 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3084 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3085 return 0;
3086 return 1;
3089 case COMPLEX_CST:
3090 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3091 flags)
3092 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3093 flags));
3095 case STRING_CST:
3096 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3097 && ! memcmp (TREE_STRING_POINTER (arg0),
3098 TREE_STRING_POINTER (arg1),
3099 TREE_STRING_LENGTH (arg0)));
3101 case ADDR_EXPR:
3102 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3103 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3104 flags | OEP_ADDRESS_OF
3105 | OEP_MATCH_SIDE_EFFECTS);
3106 case CONSTRUCTOR:
3107 /* In GIMPLE empty constructors are allowed in initializers of
3108 aggregates. */
3109 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
3110 default:
3111 break;
3114 if (flags & OEP_ONLY_CONST)
3115 return 0;
3117 /* Define macros to test an operand from arg0 and arg1 for equality and a
3118 variant that allows null and views null as being different from any
3119 non-null value. In the latter case, if either is null, the both
3120 must be; otherwise, do the normal comparison. */
3121 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3122 TREE_OPERAND (arg1, N), flags)
3124 #define OP_SAME_WITH_NULL(N) \
3125 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3126 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3128 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3130 case tcc_unary:
3131 /* Two conversions are equal only if signedness and modes match. */
3132 switch (TREE_CODE (arg0))
3134 CASE_CONVERT:
3135 case FIX_TRUNC_EXPR:
3136 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3137 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3138 return 0;
3139 break;
3140 default:
3141 break;
3144 return OP_SAME (0);
3147 case tcc_comparison:
3148 case tcc_binary:
3149 if (OP_SAME (0) && OP_SAME (1))
3150 return 1;
3152 /* For commutative ops, allow the other order. */
3153 return (commutative_tree_code (TREE_CODE (arg0))
3154 && operand_equal_p (TREE_OPERAND (arg0, 0),
3155 TREE_OPERAND (arg1, 1), flags)
3156 && operand_equal_p (TREE_OPERAND (arg0, 1),
3157 TREE_OPERAND (arg1, 0), flags));
3159 case tcc_reference:
3160 /* If either of the pointer (or reference) expressions we are
3161 dereferencing contain a side effect, these cannot be equal,
3162 but their addresses can be. */
3163 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3164 && (TREE_SIDE_EFFECTS (arg0)
3165 || TREE_SIDE_EFFECTS (arg1)))
3166 return 0;
3168 switch (TREE_CODE (arg0))
3170 case INDIRECT_REF:
3171 if (!(flags & OEP_ADDRESS_OF)
3172 && (TYPE_ALIGN (TREE_TYPE (arg0))
3173 != TYPE_ALIGN (TREE_TYPE (arg1))))
3174 return 0;
3175 flags &= ~OEP_ADDRESS_OF;
3176 return OP_SAME (0);
3178 case IMAGPART_EXPR:
3179 /* Require the same offset. */
3180 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3181 TYPE_SIZE (TREE_TYPE (arg1)),
3182 flags & ~OEP_ADDRESS_OF))
3183 return 0;
3185 /* Fallthru. */
3186 case REALPART_EXPR:
3187 case VIEW_CONVERT_EXPR:
3188 return OP_SAME (0);
3190 case TARGET_MEM_REF:
3191 case MEM_REF:
3192 if (!(flags & OEP_ADDRESS_OF))
3194 /* Require equal access sizes */
3195 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3196 && (!TYPE_SIZE (TREE_TYPE (arg0))
3197 || !TYPE_SIZE (TREE_TYPE (arg1))
3198 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3199 TYPE_SIZE (TREE_TYPE (arg1)),
3200 flags)))
3201 return 0;
3202 /* Verify that access happens in similar types. */
3203 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3204 return 0;
3205 /* Verify that accesses are TBAA compatible. */
3206 if (!alias_ptr_types_compatible_p
3207 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3208 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3209 || (MR_DEPENDENCE_CLIQUE (arg0)
3210 != MR_DEPENDENCE_CLIQUE (arg1))
3211 || (MR_DEPENDENCE_BASE (arg0)
3212 != MR_DEPENDENCE_BASE (arg1)))
3213 return 0;
3214 /* Verify that alignment is compatible. */
3215 if (TYPE_ALIGN (TREE_TYPE (arg0))
3216 != TYPE_ALIGN (TREE_TYPE (arg1)))
3217 return 0;
3219 flags &= ~OEP_ADDRESS_OF;
3220 return (OP_SAME (0) && OP_SAME (1)
3221 /* TARGET_MEM_REF require equal extra operands. */
3222 && (TREE_CODE (arg0) != TARGET_MEM_REF
3223 || (OP_SAME_WITH_NULL (2)
3224 && OP_SAME_WITH_NULL (3)
3225 && OP_SAME_WITH_NULL (4))));
3227 case ARRAY_REF:
3228 case ARRAY_RANGE_REF:
3229 if (!OP_SAME (0))
3230 return 0;
3231 flags &= ~OEP_ADDRESS_OF;
3232 /* Compare the array index by value if it is constant first as we
3233 may have different types but same value here. */
3234 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3235 TREE_OPERAND (arg1, 1))
3236 || OP_SAME (1))
3237 && OP_SAME_WITH_NULL (2)
3238 && OP_SAME_WITH_NULL (3)
3239 /* Compare low bound and element size as with OEP_ADDRESS_OF
3240 we have to account for the offset of the ref. */
3241 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3242 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3243 || (operand_equal_p (array_ref_low_bound
3244 (CONST_CAST_TREE (arg0)),
3245 array_ref_low_bound
3246 (CONST_CAST_TREE (arg1)), flags)
3247 && operand_equal_p (array_ref_element_size
3248 (CONST_CAST_TREE (arg0)),
3249 array_ref_element_size
3250 (CONST_CAST_TREE (arg1)),
3251 flags))));
3253 case COMPONENT_REF:
3254 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3255 may be NULL when we're called to compare MEM_EXPRs. */
3256 if (!OP_SAME_WITH_NULL (0)
3257 || !OP_SAME (1))
3258 return 0;
3259 flags &= ~OEP_ADDRESS_OF;
3260 return OP_SAME_WITH_NULL (2);
3262 case BIT_FIELD_REF:
3263 if (!OP_SAME (0))
3264 return 0;
3265 flags &= ~OEP_ADDRESS_OF;
3266 return OP_SAME (1) && OP_SAME (2);
3268 default:
3269 return 0;
3272 case tcc_expression:
3273 switch (TREE_CODE (arg0))
3275 case ADDR_EXPR:
3276 /* Be sure we pass right ADDRESS_OF flag. */
3277 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3278 return operand_equal_p (TREE_OPERAND (arg0, 0),
3279 TREE_OPERAND (arg1, 0),
3280 flags | OEP_ADDRESS_OF);
3282 case TRUTH_NOT_EXPR:
3283 return OP_SAME (0);
3285 case TRUTH_ANDIF_EXPR:
3286 case TRUTH_ORIF_EXPR:
3287 return OP_SAME (0) && OP_SAME (1);
3289 case FMA_EXPR:
3290 case WIDEN_MULT_PLUS_EXPR:
3291 case WIDEN_MULT_MINUS_EXPR:
3292 if (!OP_SAME (2))
3293 return 0;
3294 /* The multiplcation operands are commutative. */
3295 /* FALLTHRU */
3297 case TRUTH_AND_EXPR:
3298 case TRUTH_OR_EXPR:
3299 case TRUTH_XOR_EXPR:
3300 if (OP_SAME (0) && OP_SAME (1))
3301 return 1;
3303 /* Otherwise take into account this is a commutative operation. */
3304 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3305 TREE_OPERAND (arg1, 1), flags)
3306 && operand_equal_p (TREE_OPERAND (arg0, 1),
3307 TREE_OPERAND (arg1, 0), flags));
3309 case COND_EXPR:
3310 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3311 return 0;
3312 flags &= ~OEP_ADDRESS_OF;
3313 return OP_SAME (0);
3315 case BIT_INSERT_EXPR:
3316 /* BIT_INSERT_EXPR has an implict operand as the type precision
3317 of op1. Need to check to make sure they are the same. */
3318 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3319 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3320 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3321 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3322 return false;
3323 /* FALLTHRU */
3325 case VEC_COND_EXPR:
3326 case DOT_PROD_EXPR:
3327 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3329 case MODIFY_EXPR:
3330 case INIT_EXPR:
3331 case COMPOUND_EXPR:
3332 case PREDECREMENT_EXPR:
3333 case PREINCREMENT_EXPR:
3334 case POSTDECREMENT_EXPR:
3335 case POSTINCREMENT_EXPR:
3336 if (flags & OEP_LEXICOGRAPHIC)
3337 return OP_SAME (0) && OP_SAME (1);
3338 return 0;
3340 case CLEANUP_POINT_EXPR:
3341 case EXPR_STMT:
3342 if (flags & OEP_LEXICOGRAPHIC)
3343 return OP_SAME (0);
3344 return 0;
3346 default:
3347 return 0;
3350 case tcc_vl_exp:
3351 switch (TREE_CODE (arg0))
3353 case CALL_EXPR:
3354 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3355 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3356 /* If not both CALL_EXPRs are either internal or normal function
3357 functions, then they are not equal. */
3358 return 0;
3359 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3361 /* If the CALL_EXPRs call different internal functions, then they
3362 are not equal. */
3363 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3364 return 0;
3366 else
3368 /* If the CALL_EXPRs call different functions, then they are not
3369 equal. */
3370 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3371 flags))
3372 return 0;
3375 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3377 unsigned int cef = call_expr_flags (arg0);
3378 if (flags & OEP_PURE_SAME)
3379 cef &= ECF_CONST | ECF_PURE;
3380 else
3381 cef &= ECF_CONST;
3382 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3383 return 0;
3386 /* Now see if all the arguments are the same. */
3388 const_call_expr_arg_iterator iter0, iter1;
3389 const_tree a0, a1;
3390 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3391 a1 = first_const_call_expr_arg (arg1, &iter1);
3392 a0 && a1;
3393 a0 = next_const_call_expr_arg (&iter0),
3394 a1 = next_const_call_expr_arg (&iter1))
3395 if (! operand_equal_p (a0, a1, flags))
3396 return 0;
3398 /* If we get here and both argument lists are exhausted
3399 then the CALL_EXPRs are equal. */
3400 return ! (a0 || a1);
3402 default:
3403 return 0;
3406 case tcc_declaration:
3407 /* Consider __builtin_sqrt equal to sqrt. */
3408 return (TREE_CODE (arg0) == FUNCTION_DECL
3409 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3410 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3411 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3413 case tcc_exceptional:
3414 if (TREE_CODE (arg0) == CONSTRUCTOR)
3416 /* In GIMPLE constructors are used only to build vectors from
3417 elements. Individual elements in the constructor must be
3418 indexed in increasing order and form an initial sequence.
3420 We make no effort to compare constructors in generic.
3421 (see sem_variable::equals in ipa-icf which can do so for
3422 constants). */
3423 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3424 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3425 return 0;
3427 /* Be sure that vectors constructed have the same representation.
3428 We only tested element precision and modes to match.
3429 Vectors may be BLKmode and thus also check that the number of
3430 parts match. */
3431 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))
3432 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)))
3433 return 0;
3435 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3436 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3437 unsigned int len = vec_safe_length (v0);
3439 if (len != vec_safe_length (v1))
3440 return 0;
3442 for (unsigned int i = 0; i < len; i++)
3444 constructor_elt *c0 = &(*v0)[i];
3445 constructor_elt *c1 = &(*v1)[i];
3447 if (!operand_equal_p (c0->value, c1->value, flags)
3448 /* In GIMPLE the indexes can be either NULL or matching i.
3449 Double check this so we won't get false
3450 positives for GENERIC. */
3451 || (c0->index
3452 && (TREE_CODE (c0->index) != INTEGER_CST
3453 || !compare_tree_int (c0->index, i)))
3454 || (c1->index
3455 && (TREE_CODE (c1->index) != INTEGER_CST
3456 || !compare_tree_int (c1->index, i))))
3457 return 0;
3459 return 1;
3461 else if (TREE_CODE (arg0) == STATEMENT_LIST
3462 && (flags & OEP_LEXICOGRAPHIC))
3464 /* Compare the STATEMENT_LISTs. */
3465 tree_stmt_iterator tsi1, tsi2;
3466 tree body1 = CONST_CAST_TREE (arg0);
3467 tree body2 = CONST_CAST_TREE (arg1);
3468 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3469 tsi_next (&tsi1), tsi_next (&tsi2))
3471 /* The lists don't have the same number of statements. */
3472 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3473 return 0;
3474 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3475 return 1;
3476 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3477 OEP_LEXICOGRAPHIC))
3478 return 0;
3481 return 0;
3483 case tcc_statement:
3484 switch (TREE_CODE (arg0))
3486 case RETURN_EXPR:
3487 if (flags & OEP_LEXICOGRAPHIC)
3488 return OP_SAME_WITH_NULL (0);
3489 return 0;
3490 default:
3491 return 0;
3494 default:
3495 return 0;
3498 #undef OP_SAME
3499 #undef OP_SAME_WITH_NULL
3502 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
3503 with a different signedness or a narrower precision. */
3505 static bool
3506 operand_equal_for_comparison_p (tree arg0, tree arg1)
3508 if (operand_equal_p (arg0, arg1, 0))
3509 return true;
3511 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3512 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3513 return false;
3515 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3516 and see if the inner values are the same. This removes any
3517 signedness comparison, which doesn't matter here. */
3518 tree op0 = arg0;
3519 tree op1 = arg1;
3520 STRIP_NOPS (op0);
3521 STRIP_NOPS (op1);
3522 if (operand_equal_p (op0, op1, 0))
3523 return true;
3525 /* Discard a single widening conversion from ARG1 and see if the inner
3526 value is the same as ARG0. */
3527 if (CONVERT_EXPR_P (arg1)
3528 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3529 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3530 < TYPE_PRECISION (TREE_TYPE (arg1))
3531 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
3532 return true;
3534 return false;
3537 /* See if ARG is an expression that is either a comparison or is performing
3538 arithmetic on comparisons. The comparisons must only be comparing
3539 two different values, which will be stored in *CVAL1 and *CVAL2; if
3540 they are nonzero it means that some operands have already been found.
3541 No variables may be used anywhere else in the expression except in the
3542 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3543 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3545 If this is true, return 1. Otherwise, return zero. */
3547 static int
3548 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3550 enum tree_code code = TREE_CODE (arg);
3551 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3553 /* We can handle some of the tcc_expression cases here. */
3554 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3555 tclass = tcc_unary;
3556 else if (tclass == tcc_expression
3557 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3558 || code == COMPOUND_EXPR))
3559 tclass = tcc_binary;
3561 else if (tclass == tcc_expression && code == SAVE_EXPR
3562 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3564 /* If we've already found a CVAL1 or CVAL2, this expression is
3565 two complex to handle. */
3566 if (*cval1 || *cval2)
3567 return 0;
3569 tclass = tcc_unary;
3570 *save_p = 1;
3573 switch (tclass)
3575 case tcc_unary:
3576 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3578 case tcc_binary:
3579 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3580 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3581 cval1, cval2, save_p));
3583 case tcc_constant:
3584 return 1;
3586 case tcc_expression:
3587 if (code == COND_EXPR)
3588 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3589 cval1, cval2, save_p)
3590 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3591 cval1, cval2, save_p)
3592 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3593 cval1, cval2, save_p));
3594 return 0;
3596 case tcc_comparison:
3597 /* First see if we can handle the first operand, then the second. For
3598 the second operand, we know *CVAL1 can't be zero. It must be that
3599 one side of the comparison is each of the values; test for the
3600 case where this isn't true by failing if the two operands
3601 are the same. */
3603 if (operand_equal_p (TREE_OPERAND (arg, 0),
3604 TREE_OPERAND (arg, 1), 0))
3605 return 0;
3607 if (*cval1 == 0)
3608 *cval1 = TREE_OPERAND (arg, 0);
3609 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3611 else if (*cval2 == 0)
3612 *cval2 = TREE_OPERAND (arg, 0);
3613 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3615 else
3616 return 0;
3618 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3620 else if (*cval2 == 0)
3621 *cval2 = TREE_OPERAND (arg, 1);
3622 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3624 else
3625 return 0;
3627 return 1;
3629 default:
3630 return 0;
3634 /* ARG is a tree that is known to contain just arithmetic operations and
3635 comparisons. Evaluate the operations in the tree substituting NEW0 for
3636 any occurrence of OLD0 as an operand of a comparison and likewise for
3637 NEW1 and OLD1. */
3639 static tree
3640 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3641 tree old1, tree new1)
3643 tree type = TREE_TYPE (arg);
3644 enum tree_code code = TREE_CODE (arg);
3645 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3647 /* We can handle some of the tcc_expression cases here. */
3648 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3649 tclass = tcc_unary;
3650 else if (tclass == tcc_expression
3651 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3652 tclass = tcc_binary;
3654 switch (tclass)
3656 case tcc_unary:
3657 return fold_build1_loc (loc, code, type,
3658 eval_subst (loc, TREE_OPERAND (arg, 0),
3659 old0, new0, old1, new1));
3661 case tcc_binary:
3662 return fold_build2_loc (loc, code, type,
3663 eval_subst (loc, TREE_OPERAND (arg, 0),
3664 old0, new0, old1, new1),
3665 eval_subst (loc, TREE_OPERAND (arg, 1),
3666 old0, new0, old1, new1));
3668 case tcc_expression:
3669 switch (code)
3671 case SAVE_EXPR:
3672 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3673 old1, new1);
3675 case COMPOUND_EXPR:
3676 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3677 old1, new1);
3679 case COND_EXPR:
3680 return fold_build3_loc (loc, code, type,
3681 eval_subst (loc, TREE_OPERAND (arg, 0),
3682 old0, new0, old1, new1),
3683 eval_subst (loc, TREE_OPERAND (arg, 1),
3684 old0, new0, old1, new1),
3685 eval_subst (loc, TREE_OPERAND (arg, 2),
3686 old0, new0, old1, new1));
3687 default:
3688 break;
3690 /* Fall through - ??? */
3692 case tcc_comparison:
3694 tree arg0 = TREE_OPERAND (arg, 0);
3695 tree arg1 = TREE_OPERAND (arg, 1);
3697 /* We need to check both for exact equality and tree equality. The
3698 former will be true if the operand has a side-effect. In that
3699 case, we know the operand occurred exactly once. */
3701 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3702 arg0 = new0;
3703 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3704 arg0 = new1;
3706 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3707 arg1 = new0;
3708 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3709 arg1 = new1;
3711 return fold_build2_loc (loc, code, type, arg0, arg1);
3714 default:
3715 return arg;
3719 /* Return a tree for the case when the result of an expression is RESULT
3720 converted to TYPE and OMITTED was previously an operand of the expression
3721 but is now not needed (e.g., we folded OMITTED * 0).
3723 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3724 the conversion of RESULT to TYPE. */
3726 tree
3727 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3729 tree t = fold_convert_loc (loc, type, result);
3731 /* If the resulting operand is an empty statement, just return the omitted
3732 statement casted to void. */
3733 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3734 return build1_loc (loc, NOP_EXPR, void_type_node,
3735 fold_ignored_result (omitted));
3737 if (TREE_SIDE_EFFECTS (omitted))
3738 return build2_loc (loc, COMPOUND_EXPR, type,
3739 fold_ignored_result (omitted), t);
3741 return non_lvalue_loc (loc, t);
3744 /* Return a tree for the case when the result of an expression is RESULT
3745 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3746 of the expression but are now not needed.
3748 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3749 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3750 evaluated before OMITTED2. Otherwise, if neither has side effects,
3751 just do the conversion of RESULT to TYPE. */
3753 tree
3754 omit_two_operands_loc (location_t loc, tree type, tree result,
3755 tree omitted1, tree omitted2)
3757 tree t = fold_convert_loc (loc, type, result);
3759 if (TREE_SIDE_EFFECTS (omitted2))
3760 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3761 if (TREE_SIDE_EFFECTS (omitted1))
3762 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3764 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3768 /* Return a simplified tree node for the truth-negation of ARG. This
3769 never alters ARG itself. We assume that ARG is an operation that
3770 returns a truth value (0 or 1).
3772 FIXME: one would think we would fold the result, but it causes
3773 problems with the dominator optimizer. */
3775 static tree
3776 fold_truth_not_expr (location_t loc, tree arg)
3778 tree type = TREE_TYPE (arg);
3779 enum tree_code code = TREE_CODE (arg);
3780 location_t loc1, loc2;
3782 /* If this is a comparison, we can simply invert it, except for
3783 floating-point non-equality comparisons, in which case we just
3784 enclose a TRUTH_NOT_EXPR around what we have. */
3786 if (TREE_CODE_CLASS (code) == tcc_comparison)
3788 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3789 if (FLOAT_TYPE_P (op_type)
3790 && flag_trapping_math
3791 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3792 && code != NE_EXPR && code != EQ_EXPR)
3793 return NULL_TREE;
3795 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3796 if (code == ERROR_MARK)
3797 return NULL_TREE;
3799 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3800 TREE_OPERAND (arg, 1));
3801 if (TREE_NO_WARNING (arg))
3802 TREE_NO_WARNING (ret) = 1;
3803 return ret;
3806 switch (code)
3808 case INTEGER_CST:
3809 return constant_boolean_node (integer_zerop (arg), type);
3811 case TRUTH_AND_EXPR:
3812 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3813 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3814 return build2_loc (loc, TRUTH_OR_EXPR, type,
3815 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3816 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3818 case TRUTH_OR_EXPR:
3819 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3820 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3821 return build2_loc (loc, TRUTH_AND_EXPR, type,
3822 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3823 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3825 case TRUTH_XOR_EXPR:
3826 /* Here we can invert either operand. We invert the first operand
3827 unless the second operand is a TRUTH_NOT_EXPR in which case our
3828 result is the XOR of the first operand with the inside of the
3829 negation of the second operand. */
3831 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3832 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3833 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3834 else
3835 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3836 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3837 TREE_OPERAND (arg, 1));
3839 case TRUTH_ANDIF_EXPR:
3840 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3841 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3842 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3843 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3844 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3846 case TRUTH_ORIF_EXPR:
3847 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3848 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3849 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3850 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3851 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3853 case TRUTH_NOT_EXPR:
3854 return TREE_OPERAND (arg, 0);
3856 case COND_EXPR:
3858 tree arg1 = TREE_OPERAND (arg, 1);
3859 tree arg2 = TREE_OPERAND (arg, 2);
3861 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3862 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3864 /* A COND_EXPR may have a throw as one operand, which
3865 then has void type. Just leave void operands
3866 as they are. */
3867 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3868 VOID_TYPE_P (TREE_TYPE (arg1))
3869 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3870 VOID_TYPE_P (TREE_TYPE (arg2))
3871 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3874 case COMPOUND_EXPR:
3875 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3876 return build2_loc (loc, COMPOUND_EXPR, type,
3877 TREE_OPERAND (arg, 0),
3878 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3880 case NON_LVALUE_EXPR:
3881 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3882 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3884 CASE_CONVERT:
3885 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3886 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3888 /* fall through */
3890 case FLOAT_EXPR:
3891 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3892 return build1_loc (loc, TREE_CODE (arg), type,
3893 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3895 case BIT_AND_EXPR:
3896 if (!integer_onep (TREE_OPERAND (arg, 1)))
3897 return NULL_TREE;
3898 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3900 case SAVE_EXPR:
3901 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3903 case CLEANUP_POINT_EXPR:
3904 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3905 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3906 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3908 default:
3909 return NULL_TREE;
3913 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3914 assume that ARG is an operation that returns a truth value (0 or 1
3915 for scalars, 0 or -1 for vectors). Return the folded expression if
3916 folding is successful. Otherwise, return NULL_TREE. */
3918 static tree
3919 fold_invert_truthvalue (location_t loc, tree arg)
3921 tree type = TREE_TYPE (arg);
3922 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3923 ? BIT_NOT_EXPR
3924 : TRUTH_NOT_EXPR,
3925 type, arg);
3928 /* Return a simplified tree node for the truth-negation of ARG. This
3929 never alters ARG itself. We assume that ARG is an operation that
3930 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3932 tree
3933 invert_truthvalue_loc (location_t loc, tree arg)
3935 if (TREE_CODE (arg) == ERROR_MARK)
3936 return arg;
3938 tree type = TREE_TYPE (arg);
3939 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3940 ? BIT_NOT_EXPR
3941 : TRUTH_NOT_EXPR,
3942 type, arg);
3945 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3946 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3947 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3948 is the original memory reference used to preserve the alias set of
3949 the access. */
3951 static tree
3952 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3953 HOST_WIDE_INT bitsize, poly_int64 bitpos,
3954 int unsignedp, int reversep)
3956 tree result, bftype;
3958 /* Attempt not to lose the access path if possible. */
3959 if (TREE_CODE (orig_inner) == COMPONENT_REF)
3961 tree ninner = TREE_OPERAND (orig_inner, 0);
3962 machine_mode nmode;
3963 poly_int64 nbitsize, nbitpos;
3964 tree noffset;
3965 int nunsignedp, nreversep, nvolatilep = 0;
3966 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
3967 &noffset, &nmode, &nunsignedp,
3968 &nreversep, &nvolatilep);
3969 if (base == inner
3970 && noffset == NULL_TREE
3971 && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
3972 && !reversep
3973 && !nreversep
3974 && !nvolatilep)
3976 inner = ninner;
3977 bitpos -= nbitpos;
3981 alias_set_type iset = get_alias_set (orig_inner);
3982 if (iset == 0 && get_alias_set (inner) != iset)
3983 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3984 build_fold_addr_expr (inner),
3985 build_int_cst (ptr_type_node, 0));
3987 if (known_eq (bitpos, 0) && !reversep)
3989 tree size = TYPE_SIZE (TREE_TYPE (inner));
3990 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3991 || POINTER_TYPE_P (TREE_TYPE (inner)))
3992 && tree_fits_shwi_p (size)
3993 && tree_to_shwi (size) == bitsize)
3994 return fold_convert_loc (loc, type, inner);
3997 bftype = type;
3998 if (TYPE_PRECISION (bftype) != bitsize
3999 || TYPE_UNSIGNED (bftype) == !unsignedp)
4000 bftype = build_nonstandard_integer_type (bitsize, 0);
4002 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4003 bitsize_int (bitsize), bitsize_int (bitpos));
4004 REF_REVERSE_STORAGE_ORDER (result) = reversep;
4006 if (bftype != type)
4007 result = fold_convert_loc (loc, type, result);
4009 return result;
4012 /* Optimize a bit-field compare.
4014 There are two cases: First is a compare against a constant and the
4015 second is a comparison of two items where the fields are at the same
4016 bit position relative to the start of a chunk (byte, halfword, word)
4017 large enough to contain it. In these cases we can avoid the shift
4018 implicit in bitfield extractions.
4020 For constants, we emit a compare of the shifted constant with the
4021 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4022 compared. For two fields at the same position, we do the ANDs with the
4023 similar mask and compare the result of the ANDs.
4025 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4026 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4027 are the left and right operands of the comparison, respectively.
4029 If the optimization described above can be done, we return the resulting
4030 tree. Otherwise we return zero. */
4032 static tree
4033 optimize_bit_field_compare (location_t loc, enum tree_code code,
4034 tree compare_type, tree lhs, tree rhs)
4036 poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4037 HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4038 tree type = TREE_TYPE (lhs);
4039 tree unsigned_type;
4040 int const_p = TREE_CODE (rhs) == INTEGER_CST;
4041 machine_mode lmode, rmode;
4042 scalar_int_mode nmode;
4043 int lunsignedp, runsignedp;
4044 int lreversep, rreversep;
4045 int lvolatilep = 0, rvolatilep = 0;
4046 tree linner, rinner = NULL_TREE;
4047 tree mask;
4048 tree offset;
4050 /* Get all the information about the extractions being done. If the bit size
4051 is the same as the size of the underlying object, we aren't doing an
4052 extraction at all and so can do nothing. We also don't want to
4053 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4054 then will no longer be able to replace it. */
4055 linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4056 &lunsignedp, &lreversep, &lvolatilep);
4057 if (linner == lhs
4058 || !known_size_p (plbitsize)
4059 || !plbitsize.is_constant (&lbitsize)
4060 || !plbitpos.is_constant (&lbitpos)
4061 || lbitsize == GET_MODE_BITSIZE (lmode)
4062 || offset != 0
4063 || TREE_CODE (linner) == PLACEHOLDER_EXPR
4064 || lvolatilep)
4065 return 0;
4067 if (const_p)
4068 rreversep = lreversep;
4069 else
4071 /* If this is not a constant, we can only do something if bit positions,
4072 sizes, signedness and storage order are the same. */
4073 rinner
4074 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4075 &runsignedp, &rreversep, &rvolatilep);
4077 if (rinner == rhs
4078 || maybe_ne (lbitpos, rbitpos)
4079 || maybe_ne (lbitsize, rbitsize)
4080 || lunsignedp != runsignedp
4081 || lreversep != rreversep
4082 || offset != 0
4083 || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4084 || rvolatilep)
4085 return 0;
4088 /* Honor the C++ memory model and mimic what RTL expansion does. */
4089 poly_uint64 bitstart = 0;
4090 poly_uint64 bitend = 0;
4091 if (TREE_CODE (lhs) == COMPONENT_REF)
4093 get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4094 if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4095 return 0;
4098 /* See if we can find a mode to refer to this field. We should be able to,
4099 but fail if we can't. */
4100 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4101 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4102 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4103 TYPE_ALIGN (TREE_TYPE (rinner))),
4104 BITS_PER_WORD, false, &nmode))
4105 return 0;
4107 /* Set signed and unsigned types of the precision of this mode for the
4108 shifts below. */
4109 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4111 /* Compute the bit position and size for the new reference and our offset
4112 within it. If the new reference is the same size as the original, we
4113 won't optimize anything, so return zero. */
4114 nbitsize = GET_MODE_BITSIZE (nmode);
4115 nbitpos = lbitpos & ~ (nbitsize - 1);
4116 lbitpos -= nbitpos;
4117 if (nbitsize == lbitsize)
4118 return 0;
4120 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4121 lbitpos = nbitsize - lbitsize - lbitpos;
4123 /* Make the mask to be used against the extracted field. */
4124 mask = build_int_cst_type (unsigned_type, -1);
4125 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4126 mask = const_binop (RSHIFT_EXPR, mask,
4127 size_int (nbitsize - lbitsize - lbitpos));
4129 if (! const_p)
4131 if (nbitpos < 0)
4132 return 0;
4134 /* If not comparing with constant, just rework the comparison
4135 and return. */
4136 tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4137 nbitsize, nbitpos, 1, lreversep);
4138 t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4139 tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4140 nbitsize, nbitpos, 1, rreversep);
4141 t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4142 return fold_build2_loc (loc, code, compare_type, t1, t2);
4145 /* Otherwise, we are handling the constant case. See if the constant is too
4146 big for the field. Warn and return a tree for 0 (false) if so. We do
4147 this not only for its own sake, but to avoid having to test for this
4148 error case below. If we didn't, we might generate wrong code.
4150 For unsigned fields, the constant shifted right by the field length should
4151 be all zero. For signed fields, the high-order bits should agree with
4152 the sign bit. */
4154 if (lunsignedp)
4156 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4158 warning (0, "comparison is always %d due to width of bit-field",
4159 code == NE_EXPR);
4160 return constant_boolean_node (code == NE_EXPR, compare_type);
4163 else
4165 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4166 if (tem != 0 && tem != -1)
4168 warning (0, "comparison is always %d due to width of bit-field",
4169 code == NE_EXPR);
4170 return constant_boolean_node (code == NE_EXPR, compare_type);
4174 if (nbitpos < 0)
4175 return 0;
4177 /* Single-bit compares should always be against zero. */
4178 if (lbitsize == 1 && ! integer_zerop (rhs))
4180 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4181 rhs = build_int_cst (type, 0);
4184 /* Make a new bitfield reference, shift the constant over the
4185 appropriate number of bits and mask it with the computed mask
4186 (in case this was a signed field). If we changed it, make a new one. */
4187 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4188 nbitsize, nbitpos, 1, lreversep);
4190 rhs = const_binop (BIT_AND_EXPR,
4191 const_binop (LSHIFT_EXPR,
4192 fold_convert_loc (loc, unsigned_type, rhs),
4193 size_int (lbitpos)),
4194 mask);
4196 lhs = build2_loc (loc, code, compare_type,
4197 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4198 return lhs;
4201 /* Subroutine for fold_truth_andor_1: decode a field reference.
4203 If EXP is a comparison reference, we return the innermost reference.
4205 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4206 set to the starting bit number.
4208 If the innermost field can be completely contained in a mode-sized
4209 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4211 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4212 otherwise it is not changed.
4214 *PUNSIGNEDP is set to the signedness of the field.
4216 *PREVERSEP is set to the storage order of the field.
4218 *PMASK is set to the mask used. This is either contained in a
4219 BIT_AND_EXPR or derived from the width of the field.
4221 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4223 Return 0 if this is not a component reference or is one that we can't
4224 do anything with. */
4226 static tree
4227 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4228 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4229 int *punsignedp, int *preversep, int *pvolatilep,
4230 tree *pmask, tree *pand_mask)
4232 tree exp = *exp_;
4233 tree outer_type = 0;
4234 tree and_mask = 0;
4235 tree mask, inner, offset;
4236 tree unsigned_type;
4237 unsigned int precision;
4239 /* All the optimizations using this function assume integer fields.
4240 There are problems with FP fields since the type_for_size call
4241 below can fail for, e.g., XFmode. */
4242 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4243 return 0;
4245 /* We are interested in the bare arrangement of bits, so strip everything
4246 that doesn't affect the machine mode. However, record the type of the
4247 outermost expression if it may matter below. */
4248 if (CONVERT_EXPR_P (exp)
4249 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4250 outer_type = TREE_TYPE (exp);
4251 STRIP_NOPS (exp);
4253 if (TREE_CODE (exp) == BIT_AND_EXPR)
4255 and_mask = TREE_OPERAND (exp, 1);
4256 exp = TREE_OPERAND (exp, 0);
4257 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4258 if (TREE_CODE (and_mask) != INTEGER_CST)
4259 return 0;
4262 poly_int64 poly_bitsize, poly_bitpos;
4263 inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
4264 pmode, punsignedp, preversep, pvolatilep);
4265 if ((inner == exp && and_mask == 0)
4266 || !poly_bitsize.is_constant (pbitsize)
4267 || !poly_bitpos.is_constant (pbitpos)
4268 || *pbitsize < 0
4269 || offset != 0
4270 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4271 /* Reject out-of-bound accesses (PR79731). */
4272 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4273 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4274 *pbitpos + *pbitsize) < 0))
4275 return 0;
4277 *exp_ = exp;
4279 /* If the number of bits in the reference is the same as the bitsize of
4280 the outer type, then the outer type gives the signedness. Otherwise
4281 (in case of a small bitfield) the signedness is unchanged. */
4282 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4283 *punsignedp = TYPE_UNSIGNED (outer_type);
4285 /* Compute the mask to access the bitfield. */
4286 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4287 precision = TYPE_PRECISION (unsigned_type);
4289 mask = build_int_cst_type (unsigned_type, -1);
4291 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4292 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4294 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4295 if (and_mask != 0)
4296 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4297 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4299 *pmask = mask;
4300 *pand_mask = and_mask;
4301 return inner;
4304 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4305 bit positions and MASK is SIGNED. */
4307 static int
4308 all_ones_mask_p (const_tree mask, unsigned int size)
4310 tree type = TREE_TYPE (mask);
4311 unsigned int precision = TYPE_PRECISION (type);
4313 /* If this function returns true when the type of the mask is
4314 UNSIGNED, then there will be errors. In particular see
4315 gcc.c-torture/execute/990326-1.c. There does not appear to be
4316 any documentation paper trail as to why this is so. But the pre
4317 wide-int worked with that restriction and it has been preserved
4318 here. */
4319 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4320 return false;
4322 return wi::mask (size, false, precision) == wi::to_wide (mask);
4325 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4326 represents the sign bit of EXP's type. If EXP represents a sign
4327 or zero extension, also test VAL against the unextended type.
4328 The return value is the (sub)expression whose sign bit is VAL,
4329 or NULL_TREE otherwise. */
4331 tree
4332 sign_bit_p (tree exp, const_tree val)
4334 int width;
4335 tree t;
4337 /* Tree EXP must have an integral type. */
4338 t = TREE_TYPE (exp);
4339 if (! INTEGRAL_TYPE_P (t))
4340 return NULL_TREE;
4342 /* Tree VAL must be an integer constant. */
4343 if (TREE_CODE (val) != INTEGER_CST
4344 || TREE_OVERFLOW (val))
4345 return NULL_TREE;
4347 width = TYPE_PRECISION (t);
4348 if (wi::only_sign_bit_p (wi::to_wide (val), width))
4349 return exp;
4351 /* Handle extension from a narrower type. */
4352 if (TREE_CODE (exp) == NOP_EXPR
4353 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4354 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4356 return NULL_TREE;
4359 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4360 to be evaluated unconditionally. */
4362 static int
4363 simple_operand_p (const_tree exp)
4365 /* Strip any conversions that don't change the machine mode. */
4366 STRIP_NOPS (exp);
4368 return (CONSTANT_CLASS_P (exp)
4369 || TREE_CODE (exp) == SSA_NAME
4370 || (DECL_P (exp)
4371 && ! TREE_ADDRESSABLE (exp)
4372 && ! TREE_THIS_VOLATILE (exp)
4373 && ! DECL_NONLOCAL (exp)
4374 /* Don't regard global variables as simple. They may be
4375 allocated in ways unknown to the compiler (shared memory,
4376 #pragma weak, etc). */
4377 && ! TREE_PUBLIC (exp)
4378 && ! DECL_EXTERNAL (exp)
4379 /* Weakrefs are not safe to be read, since they can be NULL.
4380 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4381 have DECL_WEAK flag set. */
4382 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4383 /* Loading a static variable is unduly expensive, but global
4384 registers aren't expensive. */
4385 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4388 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4389 to be evaluated unconditionally.
4390 I addition to simple_operand_p, we assume that comparisons, conversions,
4391 and logic-not operations are simple, if their operands are simple, too. */
4393 static bool
4394 simple_operand_p_2 (tree exp)
4396 enum tree_code code;
4398 if (TREE_SIDE_EFFECTS (exp)
4399 || tree_could_trap_p (exp))
4400 return false;
4402 while (CONVERT_EXPR_P (exp))
4403 exp = TREE_OPERAND (exp, 0);
4405 code = TREE_CODE (exp);
4407 if (TREE_CODE_CLASS (code) == tcc_comparison)
4408 return (simple_operand_p (TREE_OPERAND (exp, 0))
4409 && simple_operand_p (TREE_OPERAND (exp, 1)));
4411 if (code == TRUTH_NOT_EXPR)
4412 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4414 return simple_operand_p (exp);
4418 /* The following functions are subroutines to fold_range_test and allow it to
4419 try to change a logical combination of comparisons into a range test.
4421 For example, both
4422 X == 2 || X == 3 || X == 4 || X == 5
4424 X >= 2 && X <= 5
4425 are converted to
4426 (unsigned) (X - 2) <= 3
4428 We describe each set of comparisons as being either inside or outside
4429 a range, using a variable named like IN_P, and then describe the
4430 range with a lower and upper bound. If one of the bounds is omitted,
4431 it represents either the highest or lowest value of the type.
4433 In the comments below, we represent a range by two numbers in brackets
4434 preceded by a "+" to designate being inside that range, or a "-" to
4435 designate being outside that range, so the condition can be inverted by
4436 flipping the prefix. An omitted bound is represented by a "-". For
4437 example, "- [-, 10]" means being outside the range starting at the lowest
4438 possible value and ending at 10, in other words, being greater than 10.
4439 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4440 always false.
4442 We set up things so that the missing bounds are handled in a consistent
4443 manner so neither a missing bound nor "true" and "false" need to be
4444 handled using a special case. */
4446 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4447 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4448 and UPPER1_P are nonzero if the respective argument is an upper bound
4449 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4450 must be specified for a comparison. ARG1 will be converted to ARG0's
4451 type if both are specified. */
4453 static tree
4454 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4455 tree arg1, int upper1_p)
4457 tree tem;
4458 int result;
4459 int sgn0, sgn1;
4461 /* If neither arg represents infinity, do the normal operation.
4462 Else, if not a comparison, return infinity. Else handle the special
4463 comparison rules. Note that most of the cases below won't occur, but
4464 are handled for consistency. */
4466 if (arg0 != 0 && arg1 != 0)
4468 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4469 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4470 STRIP_NOPS (tem);
4471 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4474 if (TREE_CODE_CLASS (code) != tcc_comparison)
4475 return 0;
4477 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4478 for neither. In real maths, we cannot assume open ended ranges are
4479 the same. But, this is computer arithmetic, where numbers are finite.
4480 We can therefore make the transformation of any unbounded range with
4481 the value Z, Z being greater than any representable number. This permits
4482 us to treat unbounded ranges as equal. */
4483 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4484 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4485 switch (code)
4487 case EQ_EXPR:
4488 result = sgn0 == sgn1;
4489 break;
4490 case NE_EXPR:
4491 result = sgn0 != sgn1;
4492 break;
4493 case LT_EXPR:
4494 result = sgn0 < sgn1;
4495 break;
4496 case LE_EXPR:
4497 result = sgn0 <= sgn1;
4498 break;
4499 case GT_EXPR:
4500 result = sgn0 > sgn1;
4501 break;
4502 case GE_EXPR:
4503 result = sgn0 >= sgn1;
4504 break;
4505 default:
4506 gcc_unreachable ();
4509 return constant_boolean_node (result, type);
4512 /* Helper routine for make_range. Perform one step for it, return
4513 new expression if the loop should continue or NULL_TREE if it should
4514 stop. */
4516 tree
4517 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4518 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4519 bool *strict_overflow_p)
4521 tree arg0_type = TREE_TYPE (arg0);
4522 tree n_low, n_high, low = *p_low, high = *p_high;
4523 int in_p = *p_in_p, n_in_p;
4525 switch (code)
4527 case TRUTH_NOT_EXPR:
4528 /* We can only do something if the range is testing for zero. */
4529 if (low == NULL_TREE || high == NULL_TREE
4530 || ! integer_zerop (low) || ! integer_zerop (high))
4531 return NULL_TREE;
4532 *p_in_p = ! in_p;
4533 return arg0;
4535 case EQ_EXPR: case NE_EXPR:
4536 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4537 /* We can only do something if the range is testing for zero
4538 and if the second operand is an integer constant. Note that
4539 saying something is "in" the range we make is done by
4540 complementing IN_P since it will set in the initial case of
4541 being not equal to zero; "out" is leaving it alone. */
4542 if (low == NULL_TREE || high == NULL_TREE
4543 || ! integer_zerop (low) || ! integer_zerop (high)
4544 || TREE_CODE (arg1) != INTEGER_CST)
4545 return NULL_TREE;
4547 switch (code)
4549 case NE_EXPR: /* - [c, c] */
4550 low = high = arg1;
4551 break;
4552 case EQ_EXPR: /* + [c, c] */
4553 in_p = ! in_p, low = high = arg1;
4554 break;
4555 case GT_EXPR: /* - [-, c] */
4556 low = 0, high = arg1;
4557 break;
4558 case GE_EXPR: /* + [c, -] */
4559 in_p = ! in_p, low = arg1, high = 0;
4560 break;
4561 case LT_EXPR: /* - [c, -] */
4562 low = arg1, high = 0;
4563 break;
4564 case LE_EXPR: /* + [-, c] */
4565 in_p = ! in_p, low = 0, high = arg1;
4566 break;
4567 default:
4568 gcc_unreachable ();
4571 /* If this is an unsigned comparison, we also know that EXP is
4572 greater than or equal to zero. We base the range tests we make
4573 on that fact, so we record it here so we can parse existing
4574 range tests. We test arg0_type since often the return type
4575 of, e.g. EQ_EXPR, is boolean. */
4576 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4578 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4579 in_p, low, high, 1,
4580 build_int_cst (arg0_type, 0),
4581 NULL_TREE))
4582 return NULL_TREE;
4584 in_p = n_in_p, low = n_low, high = n_high;
4586 /* If the high bound is missing, but we have a nonzero low
4587 bound, reverse the range so it goes from zero to the low bound
4588 minus 1. */
4589 if (high == 0 && low && ! integer_zerop (low))
4591 in_p = ! in_p;
4592 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4593 build_int_cst (TREE_TYPE (low), 1), 0);
4594 low = build_int_cst (arg0_type, 0);
4598 *p_low = low;
4599 *p_high = high;
4600 *p_in_p = in_p;
4601 return arg0;
4603 case NEGATE_EXPR:
4604 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4605 low and high are non-NULL, then normalize will DTRT. */
4606 if (!TYPE_UNSIGNED (arg0_type)
4607 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4609 if (low == NULL_TREE)
4610 low = TYPE_MIN_VALUE (arg0_type);
4611 if (high == NULL_TREE)
4612 high = TYPE_MAX_VALUE (arg0_type);
4615 /* (-x) IN [a,b] -> x in [-b, -a] */
4616 n_low = range_binop (MINUS_EXPR, exp_type,
4617 build_int_cst (exp_type, 0),
4618 0, high, 1);
4619 n_high = range_binop (MINUS_EXPR, exp_type,
4620 build_int_cst (exp_type, 0),
4621 0, low, 0);
4622 if (n_high != 0 && TREE_OVERFLOW (n_high))
4623 return NULL_TREE;
4624 goto normalize;
4626 case BIT_NOT_EXPR:
4627 /* ~ X -> -X - 1 */
4628 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4629 build_int_cst (exp_type, 1));
4631 case PLUS_EXPR:
4632 case MINUS_EXPR:
4633 if (TREE_CODE (arg1) != INTEGER_CST)
4634 return NULL_TREE;
4636 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4637 move a constant to the other side. */
4638 if (!TYPE_UNSIGNED (arg0_type)
4639 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4640 return NULL_TREE;
4642 /* If EXP is signed, any overflow in the computation is undefined,
4643 so we don't worry about it so long as our computations on
4644 the bounds don't overflow. For unsigned, overflow is defined
4645 and this is exactly the right thing. */
4646 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4647 arg0_type, low, 0, arg1, 0);
4648 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4649 arg0_type, high, 1, arg1, 0);
4650 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4651 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4652 return NULL_TREE;
4654 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4655 *strict_overflow_p = true;
4657 normalize:
4658 /* Check for an unsigned range which has wrapped around the maximum
4659 value thus making n_high < n_low, and normalize it. */
4660 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4662 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4663 build_int_cst (TREE_TYPE (n_high), 1), 0);
4664 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4665 build_int_cst (TREE_TYPE (n_low), 1), 0);
4667 /* If the range is of the form +/- [ x+1, x ], we won't
4668 be able to normalize it. But then, it represents the
4669 whole range or the empty set, so make it
4670 +/- [ -, - ]. */
4671 if (tree_int_cst_equal (n_low, low)
4672 && tree_int_cst_equal (n_high, high))
4673 low = high = 0;
4674 else
4675 in_p = ! in_p;
4677 else
4678 low = n_low, high = n_high;
4680 *p_low = low;
4681 *p_high = high;
4682 *p_in_p = in_p;
4683 return arg0;
4685 CASE_CONVERT:
4686 case NON_LVALUE_EXPR:
4687 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4688 return NULL_TREE;
4690 if (! INTEGRAL_TYPE_P (arg0_type)
4691 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4692 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4693 return NULL_TREE;
4695 n_low = low, n_high = high;
4697 if (n_low != 0)
4698 n_low = fold_convert_loc (loc, arg0_type, n_low);
4700 if (n_high != 0)
4701 n_high = fold_convert_loc (loc, arg0_type, n_high);
4703 /* If we're converting arg0 from an unsigned type, to exp,
4704 a signed type, we will be doing the comparison as unsigned.
4705 The tests above have already verified that LOW and HIGH
4706 are both positive.
4708 So we have to ensure that we will handle large unsigned
4709 values the same way that the current signed bounds treat
4710 negative values. */
4712 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4714 tree high_positive;
4715 tree equiv_type;
4716 /* For fixed-point modes, we need to pass the saturating flag
4717 as the 2nd parameter. */
4718 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4719 equiv_type
4720 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4721 TYPE_SATURATING (arg0_type));
4722 else
4723 equiv_type
4724 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4726 /* A range without an upper bound is, naturally, unbounded.
4727 Since convert would have cropped a very large value, use
4728 the max value for the destination type. */
4729 high_positive
4730 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4731 : TYPE_MAX_VALUE (arg0_type);
4733 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4734 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4735 fold_convert_loc (loc, arg0_type,
4736 high_positive),
4737 build_int_cst (arg0_type, 1));
4739 /* If the low bound is specified, "and" the range with the
4740 range for which the original unsigned value will be
4741 positive. */
4742 if (low != 0)
4744 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4745 1, fold_convert_loc (loc, arg0_type,
4746 integer_zero_node),
4747 high_positive))
4748 return NULL_TREE;
4750 in_p = (n_in_p == in_p);
4752 else
4754 /* Otherwise, "or" the range with the range of the input
4755 that will be interpreted as negative. */
4756 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4757 1, fold_convert_loc (loc, arg0_type,
4758 integer_zero_node),
4759 high_positive))
4760 return NULL_TREE;
4762 in_p = (in_p != n_in_p);
4766 *p_low = n_low;
4767 *p_high = n_high;
4768 *p_in_p = in_p;
4769 return arg0;
4771 default:
4772 return NULL_TREE;
4776 /* Given EXP, a logical expression, set the range it is testing into
4777 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4778 actually being tested. *PLOW and *PHIGH will be made of the same
4779 type as the returned expression. If EXP is not a comparison, we
4780 will most likely not be returning a useful value and range. Set
4781 *STRICT_OVERFLOW_P to true if the return value is only valid
4782 because signed overflow is undefined; otherwise, do not change
4783 *STRICT_OVERFLOW_P. */
4785 tree
4786 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4787 bool *strict_overflow_p)
4789 enum tree_code code;
4790 tree arg0, arg1 = NULL_TREE;
4791 tree exp_type, nexp;
4792 int in_p;
4793 tree low, high;
4794 location_t loc = EXPR_LOCATION (exp);
4796 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4797 and see if we can refine the range. Some of the cases below may not
4798 happen, but it doesn't seem worth worrying about this. We "continue"
4799 the outer loop when we've changed something; otherwise we "break"
4800 the switch, which will "break" the while. */
4802 in_p = 0;
4803 low = high = build_int_cst (TREE_TYPE (exp), 0);
4805 while (1)
4807 code = TREE_CODE (exp);
4808 exp_type = TREE_TYPE (exp);
4809 arg0 = NULL_TREE;
4811 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4813 if (TREE_OPERAND_LENGTH (exp) > 0)
4814 arg0 = TREE_OPERAND (exp, 0);
4815 if (TREE_CODE_CLASS (code) == tcc_binary
4816 || TREE_CODE_CLASS (code) == tcc_comparison
4817 || (TREE_CODE_CLASS (code) == tcc_expression
4818 && TREE_OPERAND_LENGTH (exp) > 1))
4819 arg1 = TREE_OPERAND (exp, 1);
4821 if (arg0 == NULL_TREE)
4822 break;
4824 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4825 &high, &in_p, strict_overflow_p);
4826 if (nexp == NULL_TREE)
4827 break;
4828 exp = nexp;
4831 /* If EXP is a constant, we can evaluate whether this is true or false. */
4832 if (TREE_CODE (exp) == INTEGER_CST)
4834 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4835 exp, 0, low, 0))
4836 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4837 exp, 1, high, 1)));
4838 low = high = 0;
4839 exp = 0;
4842 *pin_p = in_p, *plow = low, *phigh = high;
4843 return exp;
4846 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4847 a bitwise check i.e. when
4848 LOW == 0xXX...X00...0
4849 HIGH == 0xXX...X11...1
4850 Return corresponding mask in MASK and stem in VALUE. */
4852 static bool
4853 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4854 tree *value)
4856 if (TREE_CODE (low) != INTEGER_CST
4857 || TREE_CODE (high) != INTEGER_CST)
4858 return false;
4860 unsigned prec = TYPE_PRECISION (type);
4861 wide_int lo = wi::to_wide (low, prec);
4862 wide_int hi = wi::to_wide (high, prec);
4864 wide_int end_mask = lo ^ hi;
4865 if ((end_mask & (end_mask + 1)) != 0
4866 || (lo & end_mask) != 0)
4867 return false;
4869 wide_int stem_mask = ~end_mask;
4870 wide_int stem = lo & stem_mask;
4871 if (stem != (hi & stem_mask))
4872 return false;
4874 *mask = wide_int_to_tree (type, stem_mask);
4875 *value = wide_int_to_tree (type, stem);
4877 return true;
4880 /* Helper routine for build_range_check and match.pd. Return the type to
4881 perform the check or NULL if it shouldn't be optimized. */
4883 tree
4884 range_check_type (tree etype)
4886 /* First make sure that arithmetics in this type is valid, then make sure
4887 that it wraps around. */
4888 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4889 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4890 TYPE_UNSIGNED (etype));
4892 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4894 tree utype, minv, maxv;
4896 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4897 for the type in question, as we rely on this here. */
4898 utype = unsigned_type_for (etype);
4899 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4900 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4901 build_int_cst (TREE_TYPE (maxv), 1), 1);
4902 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4904 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4905 minv, 1, maxv, 1)))
4906 etype = utype;
4907 else
4908 return NULL_TREE;
4910 return etype;
4913 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4914 type, TYPE, return an expression to test if EXP is in (or out of, depending
4915 on IN_P) the range. Return 0 if the test couldn't be created. */
4917 tree
4918 build_range_check (location_t loc, tree type, tree exp, int in_p,
4919 tree low, tree high)
4921 tree etype = TREE_TYPE (exp), mask, value;
4923 /* Disable this optimization for function pointer expressions
4924 on targets that require function pointer canonicalization. */
4925 if (targetm.have_canonicalize_funcptr_for_compare ()
4926 && TREE_CODE (etype) == POINTER_TYPE
4927 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4928 return NULL_TREE;
4930 if (! in_p)
4932 value = build_range_check (loc, type, exp, 1, low, high);
4933 if (value != 0)
4934 return invert_truthvalue_loc (loc, value);
4936 return 0;
4939 if (low == 0 && high == 0)
4940 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4942 if (low == 0)
4943 return fold_build2_loc (loc, LE_EXPR, type, exp,
4944 fold_convert_loc (loc, etype, high));
4946 if (high == 0)
4947 return fold_build2_loc (loc, GE_EXPR, type, exp,
4948 fold_convert_loc (loc, etype, low));
4950 if (operand_equal_p (low, high, 0))
4951 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4952 fold_convert_loc (loc, etype, low));
4954 if (TREE_CODE (exp) == BIT_AND_EXPR
4955 && maskable_range_p (low, high, etype, &mask, &value))
4956 return fold_build2_loc (loc, EQ_EXPR, type,
4957 fold_build2_loc (loc, BIT_AND_EXPR, etype,
4958 exp, mask),
4959 value);
4961 if (integer_zerop (low))
4963 if (! TYPE_UNSIGNED (etype))
4965 etype = unsigned_type_for (etype);
4966 high = fold_convert_loc (loc, etype, high);
4967 exp = fold_convert_loc (loc, etype, exp);
4969 return build_range_check (loc, type, exp, 1, 0, high);
4972 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4973 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4975 int prec = TYPE_PRECISION (etype);
4977 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
4979 if (TYPE_UNSIGNED (etype))
4981 tree signed_etype = signed_type_for (etype);
4982 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4983 etype
4984 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4985 else
4986 etype = signed_etype;
4987 exp = fold_convert_loc (loc, etype, exp);
4989 return fold_build2_loc (loc, GT_EXPR, type, exp,
4990 build_int_cst (etype, 0));
4994 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4995 This requires wrap-around arithmetics for the type of the expression. */
4996 etype = range_check_type (etype);
4997 if (etype == NULL_TREE)
4998 return NULL_TREE;
5000 if (POINTER_TYPE_P (etype))
5001 etype = unsigned_type_for (etype);
5003 high = fold_convert_loc (loc, etype, high);
5004 low = fold_convert_loc (loc, etype, low);
5005 exp = fold_convert_loc (loc, etype, exp);
5007 value = const_binop (MINUS_EXPR, high, low);
5009 if (value != 0 && !TREE_OVERFLOW (value))
5010 return build_range_check (loc, type,
5011 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5012 1, build_int_cst (etype, 0), value);
5014 return 0;
5017 /* Return the predecessor of VAL in its type, handling the infinite case. */
5019 static tree
5020 range_predecessor (tree val)
5022 tree type = TREE_TYPE (val);
5024 if (INTEGRAL_TYPE_P (type)
5025 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5026 return 0;
5027 else
5028 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5029 build_int_cst (TREE_TYPE (val), 1), 0);
5032 /* Return the successor of VAL in its type, handling the infinite case. */
5034 static tree
5035 range_successor (tree val)
5037 tree type = TREE_TYPE (val);
5039 if (INTEGRAL_TYPE_P (type)
5040 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5041 return 0;
5042 else
5043 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5044 build_int_cst (TREE_TYPE (val), 1), 0);
5047 /* Given two ranges, see if we can merge them into one. Return 1 if we
5048 can, 0 if we can't. Set the output range into the specified parameters. */
5050 bool
5051 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5052 tree high0, int in1_p, tree low1, tree high1)
5054 int no_overlap;
5055 int subset;
5056 int temp;
5057 tree tem;
5058 int in_p;
5059 tree low, high;
5060 int lowequal = ((low0 == 0 && low1 == 0)
5061 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5062 low0, 0, low1, 0)));
5063 int highequal = ((high0 == 0 && high1 == 0)
5064 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5065 high0, 1, high1, 1)));
5067 /* Make range 0 be the range that starts first, or ends last if they
5068 start at the same value. Swap them if it isn't. */
5069 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5070 low0, 0, low1, 0))
5071 || (lowequal
5072 && integer_onep (range_binop (GT_EXPR, integer_type_node,
5073 high1, 1, high0, 1))))
5075 temp = in0_p, in0_p = in1_p, in1_p = temp;
5076 tem = low0, low0 = low1, low1 = tem;
5077 tem = high0, high0 = high1, high1 = tem;
5080 /* Now flag two cases, whether the ranges are disjoint or whether the
5081 second range is totally subsumed in the first. Note that the tests
5082 below are simplified by the ones above. */
5083 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5084 high0, 1, low1, 0));
5085 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5086 high1, 1, high0, 1));
5088 /* We now have four cases, depending on whether we are including or
5089 excluding the two ranges. */
5090 if (in0_p && in1_p)
5092 /* If they don't overlap, the result is false. If the second range
5093 is a subset it is the result. Otherwise, the range is from the start
5094 of the second to the end of the first. */
5095 if (no_overlap)
5096 in_p = 0, low = high = 0;
5097 else if (subset)
5098 in_p = 1, low = low1, high = high1;
5099 else
5100 in_p = 1, low = low1, high = high0;
5103 else if (in0_p && ! in1_p)
5105 /* If they don't overlap, the result is the first range. If they are
5106 equal, the result is false. If the second range is a subset of the
5107 first, and the ranges begin at the same place, we go from just after
5108 the end of the second range to the end of the first. If the second
5109 range is not a subset of the first, or if it is a subset and both
5110 ranges end at the same place, the range starts at the start of the
5111 first range and ends just before the second range.
5112 Otherwise, we can't describe this as a single range. */
5113 if (no_overlap)
5114 in_p = 1, low = low0, high = high0;
5115 else if (lowequal && highequal)
5116 in_p = 0, low = high = 0;
5117 else if (subset && lowequal)
5119 low = range_successor (high1);
5120 high = high0;
5121 in_p = 1;
5122 if (low == 0)
5124 /* We are in the weird situation where high0 > high1 but
5125 high1 has no successor. Punt. */
5126 return 0;
5129 else if (! subset || highequal)
5131 low = low0;
5132 high = range_predecessor (low1);
5133 in_p = 1;
5134 if (high == 0)
5136 /* low0 < low1 but low1 has no predecessor. Punt. */
5137 return 0;
5140 else
5141 return 0;
5144 else if (! in0_p && in1_p)
5146 /* If they don't overlap, the result is the second range. If the second
5147 is a subset of the first, the result is false. Otherwise,
5148 the range starts just after the first range and ends at the
5149 end of the second. */
5150 if (no_overlap)
5151 in_p = 1, low = low1, high = high1;
5152 else if (subset || highequal)
5153 in_p = 0, low = high = 0;
5154 else
5156 low = range_successor (high0);
5157 high = high1;
5158 in_p = 1;
5159 if (low == 0)
5161 /* high1 > high0 but high0 has no successor. Punt. */
5162 return 0;
5167 else
5169 /* The case where we are excluding both ranges. Here the complex case
5170 is if they don't overlap. In that case, the only time we have a
5171 range is if they are adjacent. If the second is a subset of the
5172 first, the result is the first. Otherwise, the range to exclude
5173 starts at the beginning of the first range and ends at the end of the
5174 second. */
5175 if (no_overlap)
5177 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5178 range_successor (high0),
5179 1, low1, 0)))
5180 in_p = 0, low = low0, high = high1;
5181 else
5183 /* Canonicalize - [min, x] into - [-, x]. */
5184 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5185 switch (TREE_CODE (TREE_TYPE (low0)))
5187 case ENUMERAL_TYPE:
5188 if (TYPE_PRECISION (TREE_TYPE (low0))
5189 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
5190 break;
5191 /* FALLTHROUGH */
5192 case INTEGER_TYPE:
5193 if (tree_int_cst_equal (low0,
5194 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5195 low0 = 0;
5196 break;
5197 case POINTER_TYPE:
5198 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5199 && integer_zerop (low0))
5200 low0 = 0;
5201 break;
5202 default:
5203 break;
5206 /* Canonicalize - [x, max] into - [x, -]. */
5207 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5208 switch (TREE_CODE (TREE_TYPE (high1)))
5210 case ENUMERAL_TYPE:
5211 if (TYPE_PRECISION (TREE_TYPE (high1))
5212 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5213 break;
5214 /* FALLTHROUGH */
5215 case INTEGER_TYPE:
5216 if (tree_int_cst_equal (high1,
5217 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5218 high1 = 0;
5219 break;
5220 case POINTER_TYPE:
5221 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5222 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5223 high1, 1,
5224 build_int_cst (TREE_TYPE (high1), 1),
5225 1)))
5226 high1 = 0;
5227 break;
5228 default:
5229 break;
5232 /* The ranges might be also adjacent between the maximum and
5233 minimum values of the given type. For
5234 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5235 return + [x + 1, y - 1]. */
5236 if (low0 == 0 && high1 == 0)
5238 low = range_successor (high0);
5239 high = range_predecessor (low1);
5240 if (low == 0 || high == 0)
5241 return 0;
5243 in_p = 1;
5245 else
5246 return 0;
5249 else if (subset)
5250 in_p = 0, low = low0, high = high0;
5251 else
5252 in_p = 0, low = low0, high = high1;
5255 *pin_p = in_p, *plow = low, *phigh = high;
5256 return 1;
5260 /* Subroutine of fold, looking inside expressions of the form
5261 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5262 of the COND_EXPR. This function is being used also to optimize
5263 A op B ? C : A, by reversing the comparison first.
5265 Return a folded expression whose code is not a COND_EXPR
5266 anymore, or NULL_TREE if no folding opportunity is found. */
5268 static tree
5269 fold_cond_expr_with_comparison (location_t loc, tree type,
5270 tree arg0, tree arg1, tree arg2)
5272 enum tree_code comp_code = TREE_CODE (arg0);
5273 tree arg00 = TREE_OPERAND (arg0, 0);
5274 tree arg01 = TREE_OPERAND (arg0, 1);
5275 tree arg1_type = TREE_TYPE (arg1);
5276 tree tem;
5278 STRIP_NOPS (arg1);
5279 STRIP_NOPS (arg2);
5281 /* If we have A op 0 ? A : -A, consider applying the following
5282 transformations:
5284 A == 0? A : -A same as -A
5285 A != 0? A : -A same as A
5286 A >= 0? A : -A same as abs (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)
5291 None of these transformations work for modes with signed
5292 zeros. If A is +/-0, the first two transformations will
5293 change the sign of the result (from +0 to -0, or vice
5294 versa). The last four will fix the sign of the result,
5295 even though the original expressions could be positive or
5296 negative, depending on the sign of A.
5298 Note that all these transformations are correct if A is
5299 NaN, since the two alternatives (A and -A) are also NaNs. */
5300 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5301 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5302 ? real_zerop (arg01)
5303 : integer_zerop (arg01))
5304 && ((TREE_CODE (arg2) == NEGATE_EXPR
5305 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5306 /* In the case that A is of the form X-Y, '-A' (arg2) may
5307 have already been folded to Y-X, check for that. */
5308 || (TREE_CODE (arg1) == MINUS_EXPR
5309 && TREE_CODE (arg2) == MINUS_EXPR
5310 && operand_equal_p (TREE_OPERAND (arg1, 0),
5311 TREE_OPERAND (arg2, 1), 0)
5312 && operand_equal_p (TREE_OPERAND (arg1, 1),
5313 TREE_OPERAND (arg2, 0), 0))))
5314 switch (comp_code)
5316 case EQ_EXPR:
5317 case UNEQ_EXPR:
5318 tem = fold_convert_loc (loc, arg1_type, arg1);
5319 return fold_convert_loc (loc, type, negate_expr (tem));
5320 case NE_EXPR:
5321 case LTGT_EXPR:
5322 return fold_convert_loc (loc, type, arg1);
5323 case UNGE_EXPR:
5324 case UNGT_EXPR:
5325 if (flag_trapping_math)
5326 break;
5327 /* Fall through. */
5328 case GE_EXPR:
5329 case GT_EXPR:
5330 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5331 break;
5332 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5333 return fold_convert_loc (loc, type, tem);
5334 case UNLE_EXPR:
5335 case UNLT_EXPR:
5336 if (flag_trapping_math)
5337 break;
5338 /* FALLTHRU */
5339 case LE_EXPR:
5340 case LT_EXPR:
5341 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5342 break;
5343 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5344 return negate_expr (fold_convert_loc (loc, type, tem));
5345 default:
5346 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5347 break;
5350 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5351 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5352 both transformations are correct when A is NaN: A != 0
5353 is then true, and A == 0 is false. */
5355 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5356 && integer_zerop (arg01) && integer_zerop (arg2))
5358 if (comp_code == NE_EXPR)
5359 return fold_convert_loc (loc, type, arg1);
5360 else if (comp_code == EQ_EXPR)
5361 return build_zero_cst (type);
5364 /* Try some transformations of A op B ? A : B.
5366 A == B? A : B same as B
5367 A != B? A : B same as A
5368 A >= B? A : B same as max (A, B)
5369 A > B? A : B same as max (B, A)
5370 A <= B? A : B same as min (A, B)
5371 A < B? A : B same as min (B, A)
5373 As above, these transformations don't work in the presence
5374 of signed zeros. For example, if A and B are zeros of
5375 opposite sign, the first two transformations will change
5376 the sign of the result. In the last four, the original
5377 expressions give different results for (A=+0, B=-0) and
5378 (A=-0, B=+0), but the transformed expressions do not.
5380 The first two transformations are correct if either A or B
5381 is a NaN. In the first transformation, the condition will
5382 be false, and B will indeed be chosen. In the case of the
5383 second transformation, the condition A != B will be true,
5384 and A will be chosen.
5386 The conversions to max() and min() are not correct if B is
5387 a number and A is not. The conditions in the original
5388 expressions will be false, so all four give B. The min()
5389 and max() versions would give a NaN instead. */
5390 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5391 && operand_equal_for_comparison_p (arg01, arg2)
5392 /* Avoid these transformations if the COND_EXPR may be used
5393 as an lvalue in the C++ front-end. PR c++/19199. */
5394 && (in_gimple_form
5395 || VECTOR_TYPE_P (type)
5396 || (! lang_GNU_CXX ()
5397 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5398 || ! maybe_lvalue_p (arg1)
5399 || ! maybe_lvalue_p (arg2)))
5401 tree comp_op0 = arg00;
5402 tree comp_op1 = arg01;
5403 tree comp_type = TREE_TYPE (comp_op0);
5405 switch (comp_code)
5407 case EQ_EXPR:
5408 return fold_convert_loc (loc, type, arg2);
5409 case NE_EXPR:
5410 return fold_convert_loc (loc, type, arg1);
5411 case LE_EXPR:
5412 case LT_EXPR:
5413 case UNLE_EXPR:
5414 case UNLT_EXPR:
5415 /* In C++ a ?: expression can be an lvalue, so put the
5416 operand which will be used if they are equal first
5417 so that we can convert this back to the
5418 corresponding COND_EXPR. */
5419 if (!HONOR_NANS (arg1))
5421 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5422 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5423 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5424 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5425 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5426 comp_op1, comp_op0);
5427 return fold_convert_loc (loc, type, tem);
5429 break;
5430 case GE_EXPR:
5431 case GT_EXPR:
5432 case UNGE_EXPR:
5433 case UNGT_EXPR:
5434 if (!HONOR_NANS (arg1))
5436 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5437 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5438 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5439 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5440 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5441 comp_op1, comp_op0);
5442 return fold_convert_loc (loc, type, tem);
5444 break;
5445 case UNEQ_EXPR:
5446 if (!HONOR_NANS (arg1))
5447 return fold_convert_loc (loc, type, arg2);
5448 break;
5449 case LTGT_EXPR:
5450 if (!HONOR_NANS (arg1))
5451 return fold_convert_loc (loc, type, arg1);
5452 break;
5453 default:
5454 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5455 break;
5459 return NULL_TREE;
5464 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5465 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5466 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5467 false) >= 2)
5468 #endif
5470 /* EXP is some logical combination of boolean tests. See if we can
5471 merge it into some range test. Return the new tree if so. */
5473 static tree
5474 fold_range_test (location_t loc, enum tree_code code, tree type,
5475 tree op0, tree op1)
5477 int or_op = (code == TRUTH_ORIF_EXPR
5478 || code == TRUTH_OR_EXPR);
5479 int in0_p, in1_p, in_p;
5480 tree low0, low1, low, high0, high1, high;
5481 bool strict_overflow_p = false;
5482 tree tem, lhs, rhs;
5483 const char * const warnmsg = G_("assuming signed overflow does not occur "
5484 "when simplifying range test");
5486 if (!INTEGRAL_TYPE_P (type))
5487 return 0;
5489 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5490 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5492 /* If this is an OR operation, invert both sides; we will invert
5493 again at the end. */
5494 if (or_op)
5495 in0_p = ! in0_p, in1_p = ! in1_p;
5497 /* If both expressions are the same, if we can merge the ranges, and we
5498 can build the range test, return it or it inverted. If one of the
5499 ranges is always true or always false, consider it to be the same
5500 expression as the other. */
5501 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5502 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5503 in1_p, low1, high1)
5504 && (tem = (build_range_check (loc, type,
5505 lhs != 0 ? lhs
5506 : rhs != 0 ? rhs : integer_zero_node,
5507 in_p, low, high))) != 0)
5509 if (strict_overflow_p)
5510 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5511 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5514 /* On machines where the branch cost is expensive, if this is a
5515 short-circuited branch and the underlying object on both sides
5516 is the same, make a non-short-circuit operation. */
5517 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5518 && !flag_sanitize_coverage
5519 && lhs != 0 && rhs != 0
5520 && (code == TRUTH_ANDIF_EXPR
5521 || code == TRUTH_ORIF_EXPR)
5522 && operand_equal_p (lhs, rhs, 0))
5524 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5525 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5526 which cases we can't do this. */
5527 if (simple_operand_p (lhs))
5528 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5529 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5530 type, op0, op1);
5532 else if (!lang_hooks.decls.global_bindings_p ()
5533 && !CONTAINS_PLACEHOLDER_P (lhs))
5535 tree common = save_expr (lhs);
5537 if ((lhs = build_range_check (loc, type, common,
5538 or_op ? ! in0_p : in0_p,
5539 low0, high0)) != 0
5540 && (rhs = build_range_check (loc, type, common,
5541 or_op ? ! in1_p : in1_p,
5542 low1, high1)) != 0)
5544 if (strict_overflow_p)
5545 fold_overflow_warning (warnmsg,
5546 WARN_STRICT_OVERFLOW_COMPARISON);
5547 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5548 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5549 type, lhs, rhs);
5554 return 0;
5557 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5558 bit value. Arrange things so the extra bits will be set to zero if and
5559 only if C is signed-extended to its full width. If MASK is nonzero,
5560 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5562 static tree
5563 unextend (tree c, int p, int unsignedp, tree mask)
5565 tree type = TREE_TYPE (c);
5566 int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
5567 tree temp;
5569 if (p == modesize || unsignedp)
5570 return c;
5572 /* We work by getting just the sign bit into the low-order bit, then
5573 into the high-order bit, then sign-extend. We then XOR that value
5574 with C. */
5575 temp = build_int_cst (TREE_TYPE (c),
5576 wi::extract_uhwi (wi::to_wide (c), p - 1, 1));
5578 /* We must use a signed type in order to get an arithmetic right shift.
5579 However, we must also avoid introducing accidental overflows, so that
5580 a subsequent call to integer_zerop will work. Hence we must
5581 do the type conversion here. At this point, the constant is either
5582 zero or one, and the conversion to a signed type can never overflow.
5583 We could get an overflow if this conversion is done anywhere else. */
5584 if (TYPE_UNSIGNED (type))
5585 temp = fold_convert (signed_type_for (type), temp);
5587 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5588 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5589 if (mask != 0)
5590 temp = const_binop (BIT_AND_EXPR, temp,
5591 fold_convert (TREE_TYPE (c), mask));
5592 /* If necessary, convert the type back to match the type of C. */
5593 if (TYPE_UNSIGNED (type))
5594 temp = fold_convert (type, temp);
5596 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5599 /* For an expression that has the form
5600 (A && B) || ~B
5602 (A || B) && ~B,
5603 we can drop one of the inner expressions and simplify to
5604 A || ~B
5606 A && ~B
5607 LOC is the location of the resulting expression. OP is the inner
5608 logical operation; the left-hand side in the examples above, while CMPOP
5609 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5610 removing a condition that guards another, as in
5611 (A != NULL && A->...) || A == NULL
5612 which we must not transform. If RHS_ONLY is true, only eliminate the
5613 right-most operand of the inner logical operation. */
5615 static tree
5616 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5617 bool rhs_only)
5619 tree type = TREE_TYPE (cmpop);
5620 enum tree_code code = TREE_CODE (cmpop);
5621 enum tree_code truthop_code = TREE_CODE (op);
5622 tree lhs = TREE_OPERAND (op, 0);
5623 tree rhs = TREE_OPERAND (op, 1);
5624 tree orig_lhs = lhs, orig_rhs = rhs;
5625 enum tree_code rhs_code = TREE_CODE (rhs);
5626 enum tree_code lhs_code = TREE_CODE (lhs);
5627 enum tree_code inv_code;
5629 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5630 return NULL_TREE;
5632 if (TREE_CODE_CLASS (code) != tcc_comparison)
5633 return NULL_TREE;
5635 if (rhs_code == truthop_code)
5637 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5638 if (newrhs != NULL_TREE)
5640 rhs = newrhs;
5641 rhs_code = TREE_CODE (rhs);
5644 if (lhs_code == truthop_code && !rhs_only)
5646 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5647 if (newlhs != NULL_TREE)
5649 lhs = newlhs;
5650 lhs_code = TREE_CODE (lhs);
5654 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5655 if (inv_code == rhs_code
5656 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5657 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5658 return lhs;
5659 if (!rhs_only && inv_code == lhs_code
5660 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5661 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5662 return rhs;
5663 if (rhs != orig_rhs || lhs != orig_lhs)
5664 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5665 lhs, rhs);
5666 return NULL_TREE;
5669 /* Find ways of folding logical expressions of LHS and RHS:
5670 Try to merge two comparisons to the same innermost item.
5671 Look for range tests like "ch >= '0' && ch <= '9'".
5672 Look for combinations of simple terms on machines with expensive branches
5673 and evaluate the RHS unconditionally.
5675 For example, if we have p->a == 2 && p->b == 4 and we can make an
5676 object large enough to span both A and B, we can do this with a comparison
5677 against the object ANDed with the a mask.
5679 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5680 operations to do this with one comparison.
5682 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5683 function and the one above.
5685 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5686 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5688 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5689 two operands.
5691 We return the simplified tree or 0 if no optimization is possible. */
5693 static tree
5694 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5695 tree lhs, tree rhs)
5697 /* If this is the "or" of two comparisons, we can do something if
5698 the comparisons are NE_EXPR. If this is the "and", we can do something
5699 if the comparisons are EQ_EXPR. I.e.,
5700 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5702 WANTED_CODE is this operation code. For single bit fields, we can
5703 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5704 comparison for one-bit fields. */
5706 enum tree_code wanted_code;
5707 enum tree_code lcode, rcode;
5708 tree ll_arg, lr_arg, rl_arg, rr_arg;
5709 tree ll_inner, lr_inner, rl_inner, rr_inner;
5710 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5711 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5712 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5713 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5714 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5715 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5716 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5717 scalar_int_mode lnmode, rnmode;
5718 tree ll_mask, lr_mask, rl_mask, rr_mask;
5719 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5720 tree l_const, r_const;
5721 tree lntype, rntype, result;
5722 HOST_WIDE_INT first_bit, end_bit;
5723 int volatilep;
5725 /* Start by getting the comparison codes. Fail if anything is volatile.
5726 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5727 it were surrounded with a NE_EXPR. */
5729 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5730 return 0;
5732 lcode = TREE_CODE (lhs);
5733 rcode = TREE_CODE (rhs);
5735 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5737 lhs = build2 (NE_EXPR, truth_type, lhs,
5738 build_int_cst (TREE_TYPE (lhs), 0));
5739 lcode = NE_EXPR;
5742 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5744 rhs = build2 (NE_EXPR, truth_type, rhs,
5745 build_int_cst (TREE_TYPE (rhs), 0));
5746 rcode = NE_EXPR;
5749 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5750 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5751 return 0;
5753 ll_arg = TREE_OPERAND (lhs, 0);
5754 lr_arg = TREE_OPERAND (lhs, 1);
5755 rl_arg = TREE_OPERAND (rhs, 0);
5756 rr_arg = TREE_OPERAND (rhs, 1);
5758 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5759 if (simple_operand_p (ll_arg)
5760 && simple_operand_p (lr_arg))
5762 if (operand_equal_p (ll_arg, rl_arg, 0)
5763 && operand_equal_p (lr_arg, rr_arg, 0))
5765 result = combine_comparisons (loc, code, lcode, rcode,
5766 truth_type, ll_arg, lr_arg);
5767 if (result)
5768 return result;
5770 else if (operand_equal_p (ll_arg, rr_arg, 0)
5771 && operand_equal_p (lr_arg, rl_arg, 0))
5773 result = combine_comparisons (loc, code, lcode,
5774 swap_tree_comparison (rcode),
5775 truth_type, ll_arg, lr_arg);
5776 if (result)
5777 return result;
5781 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5782 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5784 /* If the RHS can be evaluated unconditionally and its operands are
5785 simple, it wins to evaluate the RHS unconditionally on machines
5786 with expensive branches. In this case, this isn't a comparison
5787 that can be merged. */
5789 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5790 false) >= 2
5791 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5792 && simple_operand_p (rl_arg)
5793 && simple_operand_p (rr_arg))
5795 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5796 if (code == TRUTH_OR_EXPR
5797 && lcode == NE_EXPR && integer_zerop (lr_arg)
5798 && rcode == NE_EXPR && integer_zerop (rr_arg)
5799 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5800 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5801 return build2_loc (loc, NE_EXPR, truth_type,
5802 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5803 ll_arg, rl_arg),
5804 build_int_cst (TREE_TYPE (ll_arg), 0));
5806 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5807 if (code == TRUTH_AND_EXPR
5808 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5809 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5810 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5811 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5812 return build2_loc (loc, EQ_EXPR, truth_type,
5813 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5814 ll_arg, rl_arg),
5815 build_int_cst (TREE_TYPE (ll_arg), 0));
5818 /* See if the comparisons can be merged. Then get all the parameters for
5819 each side. */
5821 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5822 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5823 return 0;
5825 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5826 volatilep = 0;
5827 ll_inner = decode_field_reference (loc, &ll_arg,
5828 &ll_bitsize, &ll_bitpos, &ll_mode,
5829 &ll_unsignedp, &ll_reversep, &volatilep,
5830 &ll_mask, &ll_and_mask);
5831 lr_inner = decode_field_reference (loc, &lr_arg,
5832 &lr_bitsize, &lr_bitpos, &lr_mode,
5833 &lr_unsignedp, &lr_reversep, &volatilep,
5834 &lr_mask, &lr_and_mask);
5835 rl_inner = decode_field_reference (loc, &rl_arg,
5836 &rl_bitsize, &rl_bitpos, &rl_mode,
5837 &rl_unsignedp, &rl_reversep, &volatilep,
5838 &rl_mask, &rl_and_mask);
5839 rr_inner = decode_field_reference (loc, &rr_arg,
5840 &rr_bitsize, &rr_bitpos, &rr_mode,
5841 &rr_unsignedp, &rr_reversep, &volatilep,
5842 &rr_mask, &rr_and_mask);
5844 /* It must be true that the inner operation on the lhs of each
5845 comparison must be the same if we are to be able to do anything.
5846 Then see if we have constants. If not, the same must be true for
5847 the rhs's. */
5848 if (volatilep
5849 || ll_reversep != rl_reversep
5850 || ll_inner == 0 || rl_inner == 0
5851 || ! operand_equal_p (ll_inner, rl_inner, 0))
5852 return 0;
5854 if (TREE_CODE (lr_arg) == INTEGER_CST
5855 && TREE_CODE (rr_arg) == INTEGER_CST)
5857 l_const = lr_arg, r_const = rr_arg;
5858 lr_reversep = ll_reversep;
5860 else if (lr_reversep != rr_reversep
5861 || lr_inner == 0 || rr_inner == 0
5862 || ! operand_equal_p (lr_inner, rr_inner, 0))
5863 return 0;
5864 else
5865 l_const = r_const = 0;
5867 /* If either comparison code is not correct for our logical operation,
5868 fail. However, we can convert a one-bit comparison against zero into
5869 the opposite comparison against that bit being set in the field. */
5871 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5872 if (lcode != wanted_code)
5874 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5876 /* Make the left operand unsigned, since we are only interested
5877 in the value of one bit. Otherwise we are doing the wrong
5878 thing below. */
5879 ll_unsignedp = 1;
5880 l_const = ll_mask;
5882 else
5883 return 0;
5886 /* This is analogous to the code for l_const above. */
5887 if (rcode != wanted_code)
5889 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5891 rl_unsignedp = 1;
5892 r_const = rl_mask;
5894 else
5895 return 0;
5898 /* See if we can find a mode that contains both fields being compared on
5899 the left. If we can't, fail. Otherwise, update all constants and masks
5900 to be relative to a field of that size. */
5901 first_bit = MIN (ll_bitpos, rl_bitpos);
5902 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5903 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5904 TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
5905 volatilep, &lnmode))
5906 return 0;
5908 lnbitsize = GET_MODE_BITSIZE (lnmode);
5909 lnbitpos = first_bit & ~ (lnbitsize - 1);
5910 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5911 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5913 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5915 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5916 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5919 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5920 size_int (xll_bitpos));
5921 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5922 size_int (xrl_bitpos));
5924 if (l_const)
5926 l_const = fold_convert_loc (loc, lntype, l_const);
5927 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5928 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5929 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5930 fold_build1_loc (loc, BIT_NOT_EXPR,
5931 lntype, ll_mask))))
5933 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5935 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5938 if (r_const)
5940 r_const = fold_convert_loc (loc, lntype, r_const);
5941 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5942 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5943 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5944 fold_build1_loc (loc, BIT_NOT_EXPR,
5945 lntype, rl_mask))))
5947 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5949 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5953 /* If the right sides are not constant, do the same for it. Also,
5954 disallow this optimization if a size or signedness mismatch occurs
5955 between the left and right sides. */
5956 if (l_const == 0)
5958 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5959 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5960 /* Make sure the two fields on the right
5961 correspond to the left without being swapped. */
5962 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5963 return 0;
5965 first_bit = MIN (lr_bitpos, rr_bitpos);
5966 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5967 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5968 TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
5969 volatilep, &rnmode))
5970 return 0;
5972 rnbitsize = GET_MODE_BITSIZE (rnmode);
5973 rnbitpos = first_bit & ~ (rnbitsize - 1);
5974 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5975 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5977 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5979 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5980 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5983 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5984 rntype, lr_mask),
5985 size_int (xlr_bitpos));
5986 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
5987 rntype, rr_mask),
5988 size_int (xrr_bitpos));
5990 /* Make a mask that corresponds to both fields being compared.
5991 Do this for both items being compared. If the operands are the
5992 same size and the bits being compared are in the same position
5993 then we can do this by masking both and comparing the masked
5994 results. */
5995 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
5996 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
5997 if (lnbitsize == rnbitsize
5998 && xll_bitpos == xlr_bitpos
5999 && lnbitpos >= 0
6000 && rnbitpos >= 0)
6002 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
6003 lntype, lnbitsize, lnbitpos,
6004 ll_unsignedp || rl_unsignedp, ll_reversep);
6005 if (! all_ones_mask_p (ll_mask, lnbitsize))
6006 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6008 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
6009 rntype, rnbitsize, rnbitpos,
6010 lr_unsignedp || rr_unsignedp, lr_reversep);
6011 if (! all_ones_mask_p (lr_mask, rnbitsize))
6012 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6014 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6017 /* There is still another way we can do something: If both pairs of
6018 fields being compared are adjacent, we may be able to make a wider
6019 field containing them both.
6021 Note that we still must mask the lhs/rhs expressions. Furthermore,
6022 the mask must be shifted to account for the shift done by
6023 make_bit_field_ref. */
6024 if (((ll_bitsize + ll_bitpos == rl_bitpos
6025 && lr_bitsize + lr_bitpos == rr_bitpos)
6026 || (ll_bitpos == rl_bitpos + rl_bitsize
6027 && lr_bitpos == rr_bitpos + rr_bitsize))
6028 && ll_bitpos >= 0
6029 && rl_bitpos >= 0
6030 && lr_bitpos >= 0
6031 && rr_bitpos >= 0)
6033 tree type;
6035 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
6036 ll_bitsize + rl_bitsize,
6037 MIN (ll_bitpos, rl_bitpos),
6038 ll_unsignedp, ll_reversep);
6039 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
6040 lr_bitsize + rr_bitsize,
6041 MIN (lr_bitpos, rr_bitpos),
6042 lr_unsignedp, lr_reversep);
6044 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6045 size_int (MIN (xll_bitpos, xrl_bitpos)));
6046 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6047 size_int (MIN (xlr_bitpos, xrr_bitpos)));
6049 /* Convert to the smaller type before masking out unwanted bits. */
6050 type = lntype;
6051 if (lntype != rntype)
6053 if (lnbitsize > rnbitsize)
6055 lhs = fold_convert_loc (loc, rntype, lhs);
6056 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6057 type = rntype;
6059 else if (lnbitsize < rnbitsize)
6061 rhs = fold_convert_loc (loc, lntype, rhs);
6062 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6063 type = lntype;
6067 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6068 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6070 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6071 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6073 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6076 return 0;
6079 /* Handle the case of comparisons with constants. If there is something in
6080 common between the masks, those bits of the constants must be the same.
6081 If not, the condition is always false. Test for this to avoid generating
6082 incorrect code below. */
6083 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
6084 if (! integer_zerop (result)
6085 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
6086 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
6088 if (wanted_code == NE_EXPR)
6090 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6091 return constant_boolean_node (true, truth_type);
6093 else
6095 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6096 return constant_boolean_node (false, truth_type);
6100 if (lnbitpos < 0)
6101 return 0;
6103 /* Construct the expression we will return. First get the component
6104 reference we will make. Unless the mask is all ones the width of
6105 that field, perform the mask operation. Then compare with the
6106 merged constant. */
6107 result = make_bit_field_ref (loc, ll_inner, ll_arg,
6108 lntype, lnbitsize, lnbitpos,
6109 ll_unsignedp || rl_unsignedp, ll_reversep);
6111 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6112 if (! all_ones_mask_p (ll_mask, lnbitsize))
6113 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
6115 return build2_loc (loc, wanted_code, truth_type, result,
6116 const_binop (BIT_IOR_EXPR, l_const, r_const));
6119 /* T is an integer expression that is being multiplied, divided, or taken a
6120 modulus (CODE says which and what kind of divide or modulus) by a
6121 constant C. See if we can eliminate that operation by folding it with
6122 other operations already in T. WIDE_TYPE, if non-null, is a type that
6123 should be used for the computation if wider than our type.
6125 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6126 (X * 2) + (Y * 4). We must, however, be assured that either the original
6127 expression would not overflow or that overflow is undefined for the type
6128 in the language in question.
6130 If we return a non-null expression, it is an equivalent form of the
6131 original computation, but need not be in the original type.
6133 We set *STRICT_OVERFLOW_P to true if the return values depends on
6134 signed overflow being undefined. Otherwise we do not change
6135 *STRICT_OVERFLOW_P. */
6137 static tree
6138 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6139 bool *strict_overflow_p)
6141 /* To avoid exponential search depth, refuse to allow recursion past
6142 three levels. Beyond that (1) it's highly unlikely that we'll find
6143 something interesting and (2) we've probably processed it before
6144 when we built the inner expression. */
6146 static int depth;
6147 tree ret;
6149 if (depth > 3)
6150 return NULL;
6152 depth++;
6153 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6154 depth--;
6156 return ret;
6159 static tree
6160 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6161 bool *strict_overflow_p)
6163 tree type = TREE_TYPE (t);
6164 enum tree_code tcode = TREE_CODE (t);
6165 tree ctype = (wide_type != 0
6166 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6167 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6168 ? wide_type : type);
6169 tree t1, t2;
6170 int same_p = tcode == code;
6171 tree op0 = NULL_TREE, op1 = NULL_TREE;
6172 bool sub_strict_overflow_p;
6174 /* Don't deal with constants of zero here; they confuse the code below. */
6175 if (integer_zerop (c))
6176 return NULL_TREE;
6178 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6179 op0 = TREE_OPERAND (t, 0);
6181 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6182 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6184 /* Note that we need not handle conditional operations here since fold
6185 already handles those cases. So just do arithmetic here. */
6186 switch (tcode)
6188 case INTEGER_CST:
6189 /* For a constant, we can always simplify if we are a multiply
6190 or (for divide and modulus) if it is a multiple of our constant. */
6191 if (code == MULT_EXPR
6192 || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6193 TYPE_SIGN (type)))
6195 tree tem = const_binop (code, fold_convert (ctype, t),
6196 fold_convert (ctype, c));
6197 /* If the multiplication overflowed, we lost information on it.
6198 See PR68142 and PR69845. */
6199 if (TREE_OVERFLOW (tem))
6200 return NULL_TREE;
6201 return tem;
6203 break;
6205 CASE_CONVERT: case NON_LVALUE_EXPR:
6206 /* If op0 is an expression ... */
6207 if ((COMPARISON_CLASS_P (op0)
6208 || UNARY_CLASS_P (op0)
6209 || BINARY_CLASS_P (op0)
6210 || VL_EXP_CLASS_P (op0)
6211 || EXPRESSION_CLASS_P (op0))
6212 /* ... and has wrapping overflow, and its type is smaller
6213 than ctype, then we cannot pass through as widening. */
6214 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6215 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6216 && (TYPE_PRECISION (ctype)
6217 > TYPE_PRECISION (TREE_TYPE (op0))))
6218 /* ... or this is a truncation (t is narrower than op0),
6219 then we cannot pass through this narrowing. */
6220 || (TYPE_PRECISION (type)
6221 < TYPE_PRECISION (TREE_TYPE (op0)))
6222 /* ... or signedness changes for division or modulus,
6223 then we cannot pass through this conversion. */
6224 || (code != MULT_EXPR
6225 && (TYPE_UNSIGNED (ctype)
6226 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6227 /* ... or has undefined overflow while the converted to
6228 type has not, we cannot do the operation in the inner type
6229 as that would introduce undefined overflow. */
6230 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6231 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6232 && !TYPE_OVERFLOW_UNDEFINED (type))))
6233 break;
6235 /* Pass the constant down and see if we can make a simplification. If
6236 we can, replace this expression with the inner simplification for
6237 possible later conversion to our or some other type. */
6238 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6239 && TREE_CODE (t2) == INTEGER_CST
6240 && !TREE_OVERFLOW (t2)
6241 && (t1 = extract_muldiv (op0, t2, code,
6242 code == MULT_EXPR ? ctype : NULL_TREE,
6243 strict_overflow_p)) != 0)
6244 return t1;
6245 break;
6247 case ABS_EXPR:
6248 /* If widening the type changes it from signed to unsigned, then we
6249 must avoid building ABS_EXPR itself as unsigned. */
6250 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6252 tree cstype = (*signed_type_for) (ctype);
6253 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6254 != 0)
6256 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6257 return fold_convert (ctype, t1);
6259 break;
6261 /* If the constant is negative, we cannot simplify this. */
6262 if (tree_int_cst_sgn (c) == -1)
6263 break;
6264 /* FALLTHROUGH */
6265 case NEGATE_EXPR:
6266 /* For division and modulus, type can't be unsigned, as e.g.
6267 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6268 For signed types, even with wrapping overflow, this is fine. */
6269 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6270 break;
6271 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6272 != 0)
6273 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6274 break;
6276 case MIN_EXPR: case MAX_EXPR:
6277 /* If widening the type changes the signedness, then we can't perform
6278 this optimization as that changes the result. */
6279 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6280 break;
6282 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6283 sub_strict_overflow_p = false;
6284 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6285 &sub_strict_overflow_p)) != 0
6286 && (t2 = extract_muldiv (op1, c, code, wide_type,
6287 &sub_strict_overflow_p)) != 0)
6289 if (tree_int_cst_sgn (c) < 0)
6290 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6291 if (sub_strict_overflow_p)
6292 *strict_overflow_p = true;
6293 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6294 fold_convert (ctype, t2));
6296 break;
6298 case LSHIFT_EXPR: case RSHIFT_EXPR:
6299 /* If the second operand is constant, this is a multiplication
6300 or floor division, by a power of two, so we can treat it that
6301 way unless the multiplier or divisor overflows. Signed
6302 left-shift overflow is implementation-defined rather than
6303 undefined in C90, so do not convert signed left shift into
6304 multiplication. */
6305 if (TREE_CODE (op1) == INTEGER_CST
6306 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6307 /* const_binop may not detect overflow correctly,
6308 so check for it explicitly here. */
6309 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6310 wi::to_wide (op1))
6311 && (t1 = fold_convert (ctype,
6312 const_binop (LSHIFT_EXPR, size_one_node,
6313 op1))) != 0
6314 && !TREE_OVERFLOW (t1))
6315 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6316 ? MULT_EXPR : FLOOR_DIV_EXPR,
6317 ctype,
6318 fold_convert (ctype, op0),
6319 t1),
6320 c, code, wide_type, strict_overflow_p);
6321 break;
6323 case PLUS_EXPR: case MINUS_EXPR:
6324 /* See if we can eliminate the operation on both sides. If we can, we
6325 can return a new PLUS or MINUS. If we can't, the only remaining
6326 cases where we can do anything are if the second operand is a
6327 constant. */
6328 sub_strict_overflow_p = false;
6329 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6330 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6331 if (t1 != 0 && t2 != 0
6332 && TYPE_OVERFLOW_WRAPS (ctype)
6333 && (code == MULT_EXPR
6334 /* If not multiplication, we can only do this if both operands
6335 are divisible by c. */
6336 || (multiple_of_p (ctype, op0, c)
6337 && multiple_of_p (ctype, op1, c))))
6339 if (sub_strict_overflow_p)
6340 *strict_overflow_p = true;
6341 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6342 fold_convert (ctype, t2));
6345 /* If this was a subtraction, negate OP1 and set it to be an addition.
6346 This simplifies the logic below. */
6347 if (tcode == MINUS_EXPR)
6349 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6350 /* If OP1 was not easily negatable, the constant may be OP0. */
6351 if (TREE_CODE (op0) == INTEGER_CST)
6353 std::swap (op0, op1);
6354 std::swap (t1, t2);
6358 if (TREE_CODE (op1) != INTEGER_CST)
6359 break;
6361 /* If either OP1 or C are negative, this optimization is not safe for
6362 some of the division and remainder types while for others we need
6363 to change the code. */
6364 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6366 if (code == CEIL_DIV_EXPR)
6367 code = FLOOR_DIV_EXPR;
6368 else if (code == FLOOR_DIV_EXPR)
6369 code = CEIL_DIV_EXPR;
6370 else if (code != MULT_EXPR
6371 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6372 break;
6375 /* If it's a multiply or a division/modulus operation of a multiple
6376 of our constant, do the operation and verify it doesn't overflow. */
6377 if (code == MULT_EXPR
6378 || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6379 TYPE_SIGN (type)))
6381 op1 = const_binop (code, fold_convert (ctype, op1),
6382 fold_convert (ctype, c));
6383 /* We allow the constant to overflow with wrapping semantics. */
6384 if (op1 == 0
6385 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6386 break;
6388 else
6389 break;
6391 /* If we have an unsigned type, we cannot widen the operation since it
6392 will change the result if the original computation overflowed. */
6393 if (TYPE_UNSIGNED (ctype) && ctype != type)
6394 break;
6396 /* The last case is if we are a multiply. In that case, we can
6397 apply the distributive law to commute the multiply and addition
6398 if the multiplication of the constants doesn't overflow
6399 and overflow is defined. With undefined overflow
6400 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6401 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6402 return fold_build2 (tcode, ctype,
6403 fold_build2 (code, ctype,
6404 fold_convert (ctype, op0),
6405 fold_convert (ctype, c)),
6406 op1);
6408 break;
6410 case MULT_EXPR:
6411 /* We have a special case here if we are doing something like
6412 (C * 8) % 4 since we know that's zero. */
6413 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6414 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6415 /* If the multiplication can overflow we cannot optimize this. */
6416 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6417 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6418 && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6419 TYPE_SIGN (type)))
6421 *strict_overflow_p = true;
6422 return omit_one_operand (type, integer_zero_node, op0);
6425 /* ... fall through ... */
6427 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6428 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6429 /* If we can extract our operation from the LHS, do so and return a
6430 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6431 do something only if the second operand is a constant. */
6432 if (same_p
6433 && TYPE_OVERFLOW_WRAPS (ctype)
6434 && (t1 = extract_muldiv (op0, c, code, wide_type,
6435 strict_overflow_p)) != 0)
6436 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6437 fold_convert (ctype, op1));
6438 else if (tcode == MULT_EXPR && code == MULT_EXPR
6439 && TYPE_OVERFLOW_WRAPS (ctype)
6440 && (t1 = extract_muldiv (op1, c, code, wide_type,
6441 strict_overflow_p)) != 0)
6442 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6443 fold_convert (ctype, t1));
6444 else if (TREE_CODE (op1) != INTEGER_CST)
6445 return 0;
6447 /* If these are the same operation types, we can associate them
6448 assuming no overflow. */
6449 if (tcode == code)
6451 bool overflow_p = false;
6452 bool overflow_mul_p;
6453 signop sign = TYPE_SIGN (ctype);
6454 unsigned prec = TYPE_PRECISION (ctype);
6455 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6456 wi::to_wide (c, prec),
6457 sign, &overflow_mul_p);
6458 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6459 if (overflow_mul_p
6460 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6461 overflow_p = true;
6462 if (!overflow_p)
6463 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6464 wide_int_to_tree (ctype, mul));
6467 /* If these operations "cancel" each other, we have the main
6468 optimizations of this pass, which occur when either constant is a
6469 multiple of the other, in which case we replace this with either an
6470 operation or CODE or TCODE.
6472 If we have an unsigned type, we cannot do this since it will change
6473 the result if the original computation overflowed. */
6474 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6475 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6476 || (tcode == MULT_EXPR
6477 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6478 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6479 && code != MULT_EXPR)))
6481 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6482 TYPE_SIGN (type)))
6484 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6485 *strict_overflow_p = true;
6486 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6487 fold_convert (ctype,
6488 const_binop (TRUNC_DIV_EXPR,
6489 op1, c)));
6491 else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6492 TYPE_SIGN (type)))
6494 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6495 *strict_overflow_p = true;
6496 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6497 fold_convert (ctype,
6498 const_binop (TRUNC_DIV_EXPR,
6499 c, op1)));
6502 break;
6504 default:
6505 break;
6508 return 0;
6511 /* Return a node which has the indicated constant VALUE (either 0 or
6512 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6513 and is of the indicated TYPE. */
6515 tree
6516 constant_boolean_node (bool value, tree type)
6518 if (type == integer_type_node)
6519 return value ? integer_one_node : integer_zero_node;
6520 else if (type == boolean_type_node)
6521 return value ? boolean_true_node : boolean_false_node;
6522 else if (TREE_CODE (type) == VECTOR_TYPE)
6523 return build_vector_from_val (type,
6524 build_int_cst (TREE_TYPE (type),
6525 value ? -1 : 0));
6526 else
6527 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6531 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6532 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6533 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6534 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6535 COND is the first argument to CODE; otherwise (as in the example
6536 given here), it is the second argument. TYPE is the type of the
6537 original expression. Return NULL_TREE if no simplification is
6538 possible. */
6540 static tree
6541 fold_binary_op_with_conditional_arg (location_t loc,
6542 enum tree_code code,
6543 tree type, tree op0, tree op1,
6544 tree cond, tree arg, int cond_first_p)
6546 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6547 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6548 tree test, true_value, false_value;
6549 tree lhs = NULL_TREE;
6550 tree rhs = NULL_TREE;
6551 enum tree_code cond_code = COND_EXPR;
6553 if (TREE_CODE (cond) == COND_EXPR
6554 || TREE_CODE (cond) == VEC_COND_EXPR)
6556 test = TREE_OPERAND (cond, 0);
6557 true_value = TREE_OPERAND (cond, 1);
6558 false_value = TREE_OPERAND (cond, 2);
6559 /* If this operand throws an expression, then it does not make
6560 sense to try to perform a logical or arithmetic operation
6561 involving it. */
6562 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6563 lhs = true_value;
6564 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6565 rhs = false_value;
6567 else if (!(TREE_CODE (type) != VECTOR_TYPE
6568 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6570 tree testtype = TREE_TYPE (cond);
6571 test = cond;
6572 true_value = constant_boolean_node (true, testtype);
6573 false_value = constant_boolean_node (false, testtype);
6575 else
6576 /* Detect the case of mixing vector and scalar types - bail out. */
6577 return NULL_TREE;
6579 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6580 cond_code = VEC_COND_EXPR;
6582 /* This transformation is only worthwhile if we don't have to wrap ARG
6583 in a SAVE_EXPR and the operation can be simplified without recursing
6584 on at least one of the branches once its pushed inside the COND_EXPR. */
6585 if (!TREE_CONSTANT (arg)
6586 && (TREE_SIDE_EFFECTS (arg)
6587 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6588 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6589 return NULL_TREE;
6591 arg = fold_convert_loc (loc, arg_type, arg);
6592 if (lhs == 0)
6594 true_value = fold_convert_loc (loc, cond_type, true_value);
6595 if (cond_first_p)
6596 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6597 else
6598 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6600 if (rhs == 0)
6602 false_value = fold_convert_loc (loc, cond_type, false_value);
6603 if (cond_first_p)
6604 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6605 else
6606 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6609 /* Check that we have simplified at least one of the branches. */
6610 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6611 return NULL_TREE;
6613 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6617 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6619 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6620 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6621 ADDEND is the same as X.
6623 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6624 and finite. The problematic cases are when X is zero, and its mode
6625 has signed zeros. In the case of rounding towards -infinity,
6626 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6627 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6629 bool
6630 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6632 if (!real_zerop (addend))
6633 return false;
6635 /* Don't allow the fold with -fsignaling-nans. */
6636 if (HONOR_SNANS (element_mode (type)))
6637 return false;
6639 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6640 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6641 return true;
6643 /* In a vector or complex, we would need to check the sign of all zeros. */
6644 if (TREE_CODE (addend) != REAL_CST)
6645 return false;
6647 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6648 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6649 negate = !negate;
6651 /* The mode has signed zeros, and we have to honor their sign.
6652 In this situation, there is only one case we can return true for.
6653 X - 0 is the same as X unless rounding towards -infinity is
6654 supported. */
6655 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6658 /* Subroutine of match.pd that optimizes comparisons of a division by
6659 a nonzero integer constant against an integer constant, i.e.
6660 X/C1 op C2.
6662 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6663 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6665 enum tree_code
6666 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
6667 tree *hi, bool *neg_overflow)
6669 tree prod, tmp, type = TREE_TYPE (c1);
6670 signop sign = TYPE_SIGN (type);
6671 bool overflow;
6673 /* We have to do this the hard way to detect unsigned overflow.
6674 prod = int_const_binop (MULT_EXPR, c1, c2); */
6675 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
6676 prod = force_fit_type (type, val, -1, overflow);
6677 *neg_overflow = false;
6679 if (sign == UNSIGNED)
6681 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6682 *lo = prod;
6684 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6685 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
6686 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
6688 else if (tree_int_cst_sgn (c1) >= 0)
6690 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6691 switch (tree_int_cst_sgn (c2))
6693 case -1:
6694 *neg_overflow = true;
6695 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
6696 *hi = prod;
6697 break;
6699 case 0:
6700 *lo = fold_negate_const (tmp, type);
6701 *hi = tmp;
6702 break;
6704 case 1:
6705 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
6706 *lo = prod;
6707 break;
6709 default:
6710 gcc_unreachable ();
6713 else
6715 /* A negative divisor reverses the relational operators. */
6716 code = swap_tree_comparison (code);
6718 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
6719 switch (tree_int_cst_sgn (c2))
6721 case -1:
6722 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
6723 *lo = prod;
6724 break;
6726 case 0:
6727 *hi = fold_negate_const (tmp, type);
6728 *lo = tmp;
6729 break;
6731 case 1:
6732 *neg_overflow = true;
6733 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
6734 *hi = prod;
6735 break;
6737 default:
6738 gcc_unreachable ();
6742 if (code != EQ_EXPR && code != NE_EXPR)
6743 return code;
6745 if (TREE_OVERFLOW (*lo)
6746 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
6747 *lo = NULL_TREE;
6748 if (TREE_OVERFLOW (*hi)
6749 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
6750 *hi = NULL_TREE;
6752 return code;
6756 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6757 equality/inequality test, then return a simplified form of the test
6758 using a sign testing. Otherwise return NULL. TYPE is the desired
6759 result type. */
6761 static tree
6762 fold_single_bit_test_into_sign_test (location_t loc,
6763 enum tree_code code, tree arg0, tree arg1,
6764 tree result_type)
6766 /* If this is testing a single bit, we can optimize the test. */
6767 if ((code == NE_EXPR || code == EQ_EXPR)
6768 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6769 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6771 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6772 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6773 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6775 if (arg00 != NULL_TREE
6776 /* This is only a win if casting to a signed type is cheap,
6777 i.e. when arg00's type is not a partial mode. */
6778 && type_has_mode_precision_p (TREE_TYPE (arg00)))
6780 tree stype = signed_type_for (TREE_TYPE (arg00));
6781 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6782 result_type,
6783 fold_convert_loc (loc, stype, arg00),
6784 build_int_cst (stype, 0));
6788 return NULL_TREE;
6791 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6792 equality/inequality test, then return a simplified form of
6793 the test using shifts and logical operations. Otherwise return
6794 NULL. TYPE is the desired result type. */
6796 tree
6797 fold_single_bit_test (location_t loc, enum tree_code code,
6798 tree arg0, tree arg1, tree result_type)
6800 /* If this is testing a single bit, we can optimize the test. */
6801 if ((code == NE_EXPR || code == EQ_EXPR)
6802 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6803 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6805 tree inner = TREE_OPERAND (arg0, 0);
6806 tree type = TREE_TYPE (arg0);
6807 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6808 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
6809 int ops_unsigned;
6810 tree signed_type, unsigned_type, intermediate_type;
6811 tree tem, one;
6813 /* First, see if we can fold the single bit test into a sign-bit
6814 test. */
6815 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6816 result_type);
6817 if (tem)
6818 return tem;
6820 /* Otherwise we have (A & C) != 0 where C is a single bit,
6821 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6822 Similarly for (A & C) == 0. */
6824 /* If INNER is a right shift of a constant and it plus BITNUM does
6825 not overflow, adjust BITNUM and INNER. */
6826 if (TREE_CODE (inner) == RSHIFT_EXPR
6827 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6828 && bitnum < TYPE_PRECISION (type)
6829 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner, 1)),
6830 TYPE_PRECISION (type) - bitnum))
6832 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6833 inner = TREE_OPERAND (inner, 0);
6836 /* If we are going to be able to omit the AND below, we must do our
6837 operations as unsigned. If we must use the AND, we have a choice.
6838 Normally unsigned is faster, but for some machines signed is. */
6839 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6840 && !flag_syntax_only) ? 0 : 1;
6842 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6843 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6844 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6845 inner = fold_convert_loc (loc, intermediate_type, inner);
6847 if (bitnum != 0)
6848 inner = build2 (RSHIFT_EXPR, intermediate_type,
6849 inner, size_int (bitnum));
6851 one = build_int_cst (intermediate_type, 1);
6853 if (code == EQ_EXPR)
6854 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6856 /* Put the AND last so it can combine with more things. */
6857 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6859 /* Make sure to return the proper type. */
6860 inner = fold_convert_loc (loc, result_type, inner);
6862 return inner;
6864 return NULL_TREE;
6867 /* Test whether it is preferable two swap two operands, ARG0 and
6868 ARG1, for example because ARG0 is an integer constant and ARG1
6869 isn't. */
6871 bool
6872 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6874 if (CONSTANT_CLASS_P (arg1))
6875 return 0;
6876 if (CONSTANT_CLASS_P (arg0))
6877 return 1;
6879 STRIP_NOPS (arg0);
6880 STRIP_NOPS (arg1);
6882 if (TREE_CONSTANT (arg1))
6883 return 0;
6884 if (TREE_CONSTANT (arg0))
6885 return 1;
6887 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6888 for commutative and comparison operators. Ensuring a canonical
6889 form allows the optimizers to find additional redundancies without
6890 having to explicitly check for both orderings. */
6891 if (TREE_CODE (arg0) == SSA_NAME
6892 && TREE_CODE (arg1) == SSA_NAME
6893 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6894 return 1;
6896 /* Put SSA_NAMEs last. */
6897 if (TREE_CODE (arg1) == SSA_NAME)
6898 return 0;
6899 if (TREE_CODE (arg0) == SSA_NAME)
6900 return 1;
6902 /* Put variables last. */
6903 if (DECL_P (arg1))
6904 return 0;
6905 if (DECL_P (arg0))
6906 return 1;
6908 return 0;
6912 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6913 means A >= Y && A != MAX, but in this case we know that
6914 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6916 static tree
6917 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6919 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6921 if (TREE_CODE (bound) == LT_EXPR)
6922 a = TREE_OPERAND (bound, 0);
6923 else if (TREE_CODE (bound) == GT_EXPR)
6924 a = TREE_OPERAND (bound, 1);
6925 else
6926 return NULL_TREE;
6928 typea = TREE_TYPE (a);
6929 if (!INTEGRAL_TYPE_P (typea)
6930 && !POINTER_TYPE_P (typea))
6931 return NULL_TREE;
6933 if (TREE_CODE (ineq) == LT_EXPR)
6935 a1 = TREE_OPERAND (ineq, 1);
6936 y = TREE_OPERAND (ineq, 0);
6938 else if (TREE_CODE (ineq) == GT_EXPR)
6940 a1 = TREE_OPERAND (ineq, 0);
6941 y = TREE_OPERAND (ineq, 1);
6943 else
6944 return NULL_TREE;
6946 if (TREE_TYPE (a1) != typea)
6947 return NULL_TREE;
6949 if (POINTER_TYPE_P (typea))
6951 /* Convert the pointer types into integer before taking the difference. */
6952 tree ta = fold_convert_loc (loc, ssizetype, a);
6953 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6954 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6956 else
6957 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6959 if (!diff || !integer_onep (diff))
6960 return NULL_TREE;
6962 return fold_build2_loc (loc, GE_EXPR, type, a, y);
6965 /* Fold a sum or difference of at least one multiplication.
6966 Returns the folded tree or NULL if no simplification could be made. */
6968 static tree
6969 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
6970 tree arg0, tree arg1)
6972 tree arg00, arg01, arg10, arg11;
6973 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6975 /* (A * C) +- (B * C) -> (A+-B) * C.
6976 (A * C) +- A -> A * (C+-1).
6977 We are most concerned about the case where C is a constant,
6978 but other combinations show up during loop reduction. Since
6979 it is not difficult, try all four possibilities. */
6981 if (TREE_CODE (arg0) == MULT_EXPR)
6983 arg00 = TREE_OPERAND (arg0, 0);
6984 arg01 = TREE_OPERAND (arg0, 1);
6986 else if (TREE_CODE (arg0) == INTEGER_CST)
6988 arg00 = build_one_cst (type);
6989 arg01 = arg0;
6991 else
6993 /* We cannot generate constant 1 for fract. */
6994 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
6995 return NULL_TREE;
6996 arg00 = arg0;
6997 arg01 = build_one_cst (type);
6999 if (TREE_CODE (arg1) == MULT_EXPR)
7001 arg10 = TREE_OPERAND (arg1, 0);
7002 arg11 = TREE_OPERAND (arg1, 1);
7004 else if (TREE_CODE (arg1) == INTEGER_CST)
7006 arg10 = build_one_cst (type);
7007 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7008 the purpose of this canonicalization. */
7009 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7010 && negate_expr_p (arg1)
7011 && code == PLUS_EXPR)
7013 arg11 = negate_expr (arg1);
7014 code = MINUS_EXPR;
7016 else
7017 arg11 = arg1;
7019 else
7021 /* We cannot generate constant 1 for fract. */
7022 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7023 return NULL_TREE;
7024 arg10 = arg1;
7025 arg11 = build_one_cst (type);
7027 same = NULL_TREE;
7029 /* Prefer factoring a common non-constant. */
7030 if (operand_equal_p (arg00, arg10, 0))
7031 same = arg00, alt0 = arg01, alt1 = arg11;
7032 else if (operand_equal_p (arg01, arg11, 0))
7033 same = arg01, alt0 = arg00, alt1 = arg10;
7034 else if (operand_equal_p (arg00, arg11, 0))
7035 same = arg00, alt0 = arg01, alt1 = arg10;
7036 else if (operand_equal_p (arg01, arg10, 0))
7037 same = arg01, alt0 = arg00, alt1 = arg11;
7039 /* No identical multiplicands; see if we can find a common
7040 power-of-two factor in non-power-of-two multiplies. This
7041 can help in multi-dimensional array access. */
7042 else if (tree_fits_shwi_p (arg01)
7043 && tree_fits_shwi_p (arg11))
7045 HOST_WIDE_INT int01, int11, tmp;
7046 bool swap = false;
7047 tree maybe_same;
7048 int01 = tree_to_shwi (arg01);
7049 int11 = tree_to_shwi (arg11);
7051 /* Move min of absolute values to int11. */
7052 if (absu_hwi (int01) < absu_hwi (int11))
7054 tmp = int01, int01 = int11, int11 = tmp;
7055 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7056 maybe_same = arg01;
7057 swap = true;
7059 else
7060 maybe_same = arg11;
7062 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7063 /* The remainder should not be a constant, otherwise we
7064 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7065 increased the number of multiplications necessary. */
7066 && TREE_CODE (arg10) != INTEGER_CST)
7068 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7069 build_int_cst (TREE_TYPE (arg00),
7070 int01 / int11));
7071 alt1 = arg10;
7072 same = maybe_same;
7073 if (swap)
7074 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7078 if (!same)
7079 return NULL_TREE;
7081 if (! INTEGRAL_TYPE_P (type)
7082 || TYPE_OVERFLOW_WRAPS (type)
7083 /* We are neither factoring zero nor minus one. */
7084 || TREE_CODE (same) == INTEGER_CST)
7085 return fold_build2_loc (loc, MULT_EXPR, type,
7086 fold_build2_loc (loc, code, type,
7087 fold_convert_loc (loc, type, alt0),
7088 fold_convert_loc (loc, type, alt1)),
7089 fold_convert_loc (loc, type, same));
7091 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7092 same may be minus one and thus the multiplication may overflow. Perform
7093 the operations in an unsigned type. */
7094 tree utype = unsigned_type_for (type);
7095 tree tem = fold_build2_loc (loc, code, utype,
7096 fold_convert_loc (loc, utype, alt0),
7097 fold_convert_loc (loc, utype, alt1));
7098 /* If the sum evaluated to a constant that is not -INF the multiplication
7099 cannot overflow. */
7100 if (TREE_CODE (tem) == INTEGER_CST
7101 && (wi::to_wide (tem)
7102 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7103 return fold_build2_loc (loc, MULT_EXPR, type,
7104 fold_convert (type, tem), same);
7106 return fold_convert_loc (loc, type,
7107 fold_build2_loc (loc, MULT_EXPR, utype, tem,
7108 fold_convert_loc (loc, utype, same)));
7111 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7112 specified by EXPR into the buffer PTR of length LEN bytes.
7113 Return the number of bytes placed in the buffer, or zero
7114 upon failure. */
7116 static int
7117 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7119 tree type = TREE_TYPE (expr);
7120 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7121 int byte, offset, word, words;
7122 unsigned char value;
7124 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7125 return 0;
7126 if (off == -1)
7127 off = 0;
7129 if (ptr == NULL)
7130 /* Dry run. */
7131 return MIN (len, total_bytes - off);
7133 words = total_bytes / UNITS_PER_WORD;
7135 for (byte = 0; byte < total_bytes; byte++)
7137 int bitpos = byte * BITS_PER_UNIT;
7138 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7139 number of bytes. */
7140 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7142 if (total_bytes > UNITS_PER_WORD)
7144 word = byte / UNITS_PER_WORD;
7145 if (WORDS_BIG_ENDIAN)
7146 word = (words - 1) - word;
7147 offset = word * UNITS_PER_WORD;
7148 if (BYTES_BIG_ENDIAN)
7149 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7150 else
7151 offset += byte % UNITS_PER_WORD;
7153 else
7154 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7155 if (offset >= off && offset - off < len)
7156 ptr[offset - off] = value;
7158 return MIN (len, total_bytes - off);
7162 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7163 specified by EXPR into the buffer PTR of length LEN bytes.
7164 Return the number of bytes placed in the buffer, or zero
7165 upon failure. */
7167 static int
7168 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7170 tree type = TREE_TYPE (expr);
7171 scalar_mode mode = SCALAR_TYPE_MODE (type);
7172 int total_bytes = GET_MODE_SIZE (mode);
7173 FIXED_VALUE_TYPE value;
7174 tree i_value, i_type;
7176 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7177 return 0;
7179 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7181 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7182 return 0;
7184 value = TREE_FIXED_CST (expr);
7185 i_value = double_int_to_tree (i_type, value.data);
7187 return native_encode_int (i_value, ptr, len, off);
7191 /* Subroutine of native_encode_expr. Encode the REAL_CST
7192 specified by EXPR into the buffer PTR of length LEN bytes.
7193 Return the number of bytes placed in the buffer, or zero
7194 upon failure. */
7196 static int
7197 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7199 tree type = TREE_TYPE (expr);
7200 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7201 int byte, offset, word, words, bitpos;
7202 unsigned char value;
7204 /* There are always 32 bits in each long, no matter the size of
7205 the hosts long. We handle floating point representations with
7206 up to 192 bits. */
7207 long tmp[6];
7209 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7210 return 0;
7211 if (off == -1)
7212 off = 0;
7214 if (ptr == NULL)
7215 /* Dry run. */
7216 return MIN (len, total_bytes - off);
7218 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7220 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7222 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7223 bitpos += BITS_PER_UNIT)
7225 byte = (bitpos / BITS_PER_UNIT) & 3;
7226 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7228 if (UNITS_PER_WORD < 4)
7230 word = byte / UNITS_PER_WORD;
7231 if (WORDS_BIG_ENDIAN)
7232 word = (words - 1) - word;
7233 offset = word * UNITS_PER_WORD;
7234 if (BYTES_BIG_ENDIAN)
7235 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7236 else
7237 offset += byte % UNITS_PER_WORD;
7239 else
7241 offset = byte;
7242 if (BYTES_BIG_ENDIAN)
7244 /* Reverse bytes within each long, or within the entire float
7245 if it's smaller than a long (for HFmode). */
7246 offset = MIN (3, total_bytes - 1) - offset;
7247 gcc_assert (offset >= 0);
7250 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7251 if (offset >= off
7252 && offset - off < len)
7253 ptr[offset - off] = value;
7255 return MIN (len, total_bytes - off);
7258 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7259 specified by EXPR into the buffer PTR of length LEN bytes.
7260 Return the number of bytes placed in the buffer, or zero
7261 upon failure. */
7263 static int
7264 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7266 int rsize, isize;
7267 tree part;
7269 part = TREE_REALPART (expr);
7270 rsize = native_encode_expr (part, ptr, len, off);
7271 if (off == -1 && rsize == 0)
7272 return 0;
7273 part = TREE_IMAGPART (expr);
7274 if (off != -1)
7275 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7276 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7277 len - rsize, off);
7278 if (off == -1 && isize != rsize)
7279 return 0;
7280 return rsize + isize;
7284 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7285 specified by EXPR into the buffer PTR of length LEN bytes.
7286 Return the number of bytes placed in the buffer, or zero
7287 upon failure. */
7289 static int
7290 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7292 unsigned i, count;
7293 int size, offset;
7294 tree itype, elem;
7296 offset = 0;
7297 count = VECTOR_CST_NELTS (expr);
7298 itype = TREE_TYPE (TREE_TYPE (expr));
7299 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7300 for (i = 0; i < count; i++)
7302 if (off >= size)
7304 off -= size;
7305 continue;
7307 elem = VECTOR_CST_ELT (expr, i);
7308 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7309 len - offset, off);
7310 if ((off == -1 && res != size) || res == 0)
7311 return 0;
7312 offset += res;
7313 if (offset >= len)
7314 return offset;
7315 if (off != -1)
7316 off = 0;
7318 return offset;
7322 /* Subroutine of native_encode_expr. Encode the STRING_CST
7323 specified by EXPR into the buffer PTR of length LEN bytes.
7324 Return the number of bytes placed in the buffer, or zero
7325 upon failure. */
7327 static int
7328 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7330 tree type = TREE_TYPE (expr);
7332 /* Wide-char strings are encoded in target byte-order so native
7333 encoding them is trivial. */
7334 if (BITS_PER_UNIT != CHAR_BIT
7335 || TREE_CODE (type) != ARRAY_TYPE
7336 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7337 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7338 return 0;
7340 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7341 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7342 return 0;
7343 if (off == -1)
7344 off = 0;
7345 if (ptr == NULL)
7346 /* Dry run. */;
7347 else if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7349 int written = 0;
7350 if (off < TREE_STRING_LENGTH (expr))
7352 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7353 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7355 memset (ptr + written, 0,
7356 MIN (total_bytes - written, len - written));
7358 else
7359 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7360 return MIN (total_bytes - off, len);
7364 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7365 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7366 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7367 anything, just do a dry run. If OFF is not -1 then start
7368 the encoding at byte offset OFF and encode at most LEN bytes.
7369 Return the number of bytes placed in the buffer, or zero upon failure. */
7372 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7374 /* We don't support starting at negative offset and -1 is special. */
7375 if (off < -1)
7376 return 0;
7378 switch (TREE_CODE (expr))
7380 case INTEGER_CST:
7381 return native_encode_int (expr, ptr, len, off);
7383 case REAL_CST:
7384 return native_encode_real (expr, ptr, len, off);
7386 case FIXED_CST:
7387 return native_encode_fixed (expr, ptr, len, off);
7389 case COMPLEX_CST:
7390 return native_encode_complex (expr, ptr, len, off);
7392 case VECTOR_CST:
7393 return native_encode_vector (expr, ptr, len, off);
7395 case STRING_CST:
7396 return native_encode_string (expr, ptr, len, off);
7398 default:
7399 return 0;
7404 /* Subroutine of native_interpret_expr. Interpret the contents of
7405 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7406 If the buffer cannot be interpreted, return NULL_TREE. */
7408 static tree
7409 native_interpret_int (tree type, const unsigned char *ptr, int len)
7411 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7413 if (total_bytes > len
7414 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7415 return NULL_TREE;
7417 wide_int result = wi::from_buffer (ptr, total_bytes);
7419 return wide_int_to_tree (type, result);
7423 /* Subroutine of native_interpret_expr. Interpret the contents of
7424 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7425 If the buffer cannot be interpreted, return NULL_TREE. */
7427 static tree
7428 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7430 scalar_mode mode = SCALAR_TYPE_MODE (type);
7431 int total_bytes = GET_MODE_SIZE (mode);
7432 double_int result;
7433 FIXED_VALUE_TYPE fixed_value;
7435 if (total_bytes > len
7436 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7437 return NULL_TREE;
7439 result = double_int::from_buffer (ptr, total_bytes);
7440 fixed_value = fixed_from_double_int (result, mode);
7442 return build_fixed (type, fixed_value);
7446 /* Subroutine of native_interpret_expr. Interpret the contents of
7447 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7448 If the buffer cannot be interpreted, return NULL_TREE. */
7450 static tree
7451 native_interpret_real (tree type, const unsigned char *ptr, int len)
7453 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
7454 int total_bytes = GET_MODE_SIZE (mode);
7455 unsigned char value;
7456 /* There are always 32 bits in each long, no matter the size of
7457 the hosts long. We handle floating point representations with
7458 up to 192 bits. */
7459 REAL_VALUE_TYPE r;
7460 long tmp[6];
7462 if (total_bytes > len || total_bytes > 24)
7463 return NULL_TREE;
7464 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7466 memset (tmp, 0, sizeof (tmp));
7467 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7468 bitpos += BITS_PER_UNIT)
7470 /* Both OFFSET and BYTE index within a long;
7471 bitpos indexes the whole float. */
7472 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7473 if (UNITS_PER_WORD < 4)
7475 int word = byte / UNITS_PER_WORD;
7476 if (WORDS_BIG_ENDIAN)
7477 word = (words - 1) - word;
7478 offset = word * UNITS_PER_WORD;
7479 if (BYTES_BIG_ENDIAN)
7480 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7481 else
7482 offset += byte % UNITS_PER_WORD;
7484 else
7486 offset = byte;
7487 if (BYTES_BIG_ENDIAN)
7489 /* Reverse bytes within each long, or within the entire float
7490 if it's smaller than a long (for HFmode). */
7491 offset = MIN (3, total_bytes - 1) - offset;
7492 gcc_assert (offset >= 0);
7495 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7497 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7500 real_from_target (&r, tmp, mode);
7501 return build_real (type, r);
7505 /* Subroutine of native_interpret_expr. Interpret the contents of
7506 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7507 If the buffer cannot be interpreted, return NULL_TREE. */
7509 static tree
7510 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7512 tree etype, rpart, ipart;
7513 int size;
7515 etype = TREE_TYPE (type);
7516 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7517 if (size * 2 > len)
7518 return NULL_TREE;
7519 rpart = native_interpret_expr (etype, ptr, size);
7520 if (!rpart)
7521 return NULL_TREE;
7522 ipart = native_interpret_expr (etype, ptr+size, size);
7523 if (!ipart)
7524 return NULL_TREE;
7525 return build_complex (type, rpart, ipart);
7529 /* Subroutine of native_interpret_expr. Interpret the contents of
7530 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7531 If the buffer cannot be interpreted, return NULL_TREE. */
7533 static tree
7534 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7536 tree etype, elem;
7537 int i, size, count;
7539 etype = TREE_TYPE (type);
7540 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7541 count = TYPE_VECTOR_SUBPARTS (type);
7542 if (size * count > len)
7543 return NULL_TREE;
7545 tree_vector_builder elements (type, count, 1);
7546 for (i = 0; i < count; ++i)
7548 elem = native_interpret_expr (etype, ptr+(i*size), size);
7549 if (!elem)
7550 return NULL_TREE;
7551 elements.quick_push (elem);
7553 return elements.build ();
7557 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7558 the buffer PTR of length LEN as a constant of type TYPE. For
7559 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7560 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7561 return NULL_TREE. */
7563 tree
7564 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7566 switch (TREE_CODE (type))
7568 case INTEGER_TYPE:
7569 case ENUMERAL_TYPE:
7570 case BOOLEAN_TYPE:
7571 case POINTER_TYPE:
7572 case REFERENCE_TYPE:
7573 return native_interpret_int (type, ptr, len);
7575 case REAL_TYPE:
7576 return native_interpret_real (type, ptr, len);
7578 case FIXED_POINT_TYPE:
7579 return native_interpret_fixed (type, ptr, len);
7581 case COMPLEX_TYPE:
7582 return native_interpret_complex (type, ptr, len);
7584 case VECTOR_TYPE:
7585 return native_interpret_vector (type, ptr, len);
7587 default:
7588 return NULL_TREE;
7592 /* Returns true if we can interpret the contents of a native encoding
7593 as TYPE. */
7595 static bool
7596 can_native_interpret_type_p (tree type)
7598 switch (TREE_CODE (type))
7600 case INTEGER_TYPE:
7601 case ENUMERAL_TYPE:
7602 case BOOLEAN_TYPE:
7603 case POINTER_TYPE:
7604 case REFERENCE_TYPE:
7605 case FIXED_POINT_TYPE:
7606 case REAL_TYPE:
7607 case COMPLEX_TYPE:
7608 case VECTOR_TYPE:
7609 return true;
7610 default:
7611 return false;
7616 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7617 TYPE at compile-time. If we're unable to perform the conversion
7618 return NULL_TREE. */
7620 static tree
7621 fold_view_convert_expr (tree type, tree expr)
7623 /* We support up to 512-bit values (for V8DFmode). */
7624 unsigned char buffer[64];
7625 int len;
7627 /* Check that the host and target are sane. */
7628 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7629 return NULL_TREE;
7631 len = native_encode_expr (expr, buffer, sizeof (buffer));
7632 if (len == 0)
7633 return NULL_TREE;
7635 return native_interpret_expr (type, buffer, len);
7638 /* Build an expression for the address of T. Folds away INDIRECT_REF
7639 to avoid confusing the gimplify process. */
7641 tree
7642 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7644 /* The size of the object is not relevant when talking about its address. */
7645 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7646 t = TREE_OPERAND (t, 0);
7648 if (TREE_CODE (t) == INDIRECT_REF)
7650 t = TREE_OPERAND (t, 0);
7652 if (TREE_TYPE (t) != ptrtype)
7653 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7655 else if (TREE_CODE (t) == MEM_REF
7656 && integer_zerop (TREE_OPERAND (t, 1)))
7657 return TREE_OPERAND (t, 0);
7658 else if (TREE_CODE (t) == MEM_REF
7659 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7660 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7661 TREE_OPERAND (t, 0),
7662 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7663 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7665 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7667 if (TREE_TYPE (t) != ptrtype)
7668 t = fold_convert_loc (loc, ptrtype, t);
7670 else
7671 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7673 return t;
7676 /* Build an expression for the address of T. */
7678 tree
7679 build_fold_addr_expr_loc (location_t loc, tree t)
7681 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7683 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7686 /* Fold a unary expression of code CODE and type TYPE with operand
7687 OP0. Return the folded expression if folding is successful.
7688 Otherwise, return NULL_TREE. */
7690 tree
7691 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7693 tree tem;
7694 tree arg0;
7695 enum tree_code_class kind = TREE_CODE_CLASS (code);
7697 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7698 && TREE_CODE_LENGTH (code) == 1);
7700 arg0 = op0;
7701 if (arg0)
7703 if (CONVERT_EXPR_CODE_P (code)
7704 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7706 /* Don't use STRIP_NOPS, because signedness of argument type
7707 matters. */
7708 STRIP_SIGN_NOPS (arg0);
7710 else
7712 /* Strip any conversions that don't change the mode. This
7713 is safe for every expression, except for a comparison
7714 expression because its signedness is derived from its
7715 operands.
7717 Note that this is done as an internal manipulation within
7718 the constant folder, in order to find the simplest
7719 representation of the arguments so that their form can be
7720 studied. In any cases, the appropriate type conversions
7721 should be put back in the tree that will get out of the
7722 constant folder. */
7723 STRIP_NOPS (arg0);
7726 if (CONSTANT_CLASS_P (arg0))
7728 tree tem = const_unop (code, type, arg0);
7729 if (tem)
7731 if (TREE_TYPE (tem) != type)
7732 tem = fold_convert_loc (loc, type, tem);
7733 return tem;
7738 tem = generic_simplify (loc, code, type, op0);
7739 if (tem)
7740 return tem;
7742 if (TREE_CODE_CLASS (code) == tcc_unary)
7744 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7745 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7746 fold_build1_loc (loc, code, type,
7747 fold_convert_loc (loc, TREE_TYPE (op0),
7748 TREE_OPERAND (arg0, 1))));
7749 else if (TREE_CODE (arg0) == COND_EXPR)
7751 tree arg01 = TREE_OPERAND (arg0, 1);
7752 tree arg02 = TREE_OPERAND (arg0, 2);
7753 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7754 arg01 = fold_build1_loc (loc, code, type,
7755 fold_convert_loc (loc,
7756 TREE_TYPE (op0), arg01));
7757 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7758 arg02 = fold_build1_loc (loc, code, type,
7759 fold_convert_loc (loc,
7760 TREE_TYPE (op0), arg02));
7761 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7762 arg01, arg02);
7764 /* If this was a conversion, and all we did was to move into
7765 inside the COND_EXPR, bring it back out. But leave it if
7766 it is a conversion from integer to integer and the
7767 result precision is no wider than a word since such a
7768 conversion is cheap and may be optimized away by combine,
7769 while it couldn't if it were outside the COND_EXPR. Then return
7770 so we don't get into an infinite recursion loop taking the
7771 conversion out and then back in. */
7773 if ((CONVERT_EXPR_CODE_P (code)
7774 || code == NON_LVALUE_EXPR)
7775 && TREE_CODE (tem) == COND_EXPR
7776 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7777 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7778 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7779 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7780 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7781 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7782 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7783 && (INTEGRAL_TYPE_P
7784 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7785 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7786 || flag_syntax_only))
7787 tem = build1_loc (loc, code, type,
7788 build3 (COND_EXPR,
7789 TREE_TYPE (TREE_OPERAND
7790 (TREE_OPERAND (tem, 1), 0)),
7791 TREE_OPERAND (tem, 0),
7792 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7793 TREE_OPERAND (TREE_OPERAND (tem, 2),
7794 0)));
7795 return tem;
7799 switch (code)
7801 case NON_LVALUE_EXPR:
7802 if (!maybe_lvalue_p (op0))
7803 return fold_convert_loc (loc, type, op0);
7804 return NULL_TREE;
7806 CASE_CONVERT:
7807 case FLOAT_EXPR:
7808 case FIX_TRUNC_EXPR:
7809 if (COMPARISON_CLASS_P (op0))
7811 /* If we have (type) (a CMP b) and type is an integral type, return
7812 new expression involving the new type. Canonicalize
7813 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7814 non-integral type.
7815 Do not fold the result as that would not simplify further, also
7816 folding again results in recursions. */
7817 if (TREE_CODE (type) == BOOLEAN_TYPE)
7818 return build2_loc (loc, TREE_CODE (op0), type,
7819 TREE_OPERAND (op0, 0),
7820 TREE_OPERAND (op0, 1));
7821 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7822 && TREE_CODE (type) != VECTOR_TYPE)
7823 return build3_loc (loc, COND_EXPR, type, op0,
7824 constant_boolean_node (true, type),
7825 constant_boolean_node (false, type));
7828 /* Handle (T *)&A.B.C for A being of type T and B and C
7829 living at offset zero. This occurs frequently in
7830 C++ upcasting and then accessing the base. */
7831 if (TREE_CODE (op0) == ADDR_EXPR
7832 && POINTER_TYPE_P (type)
7833 && handled_component_p (TREE_OPERAND (op0, 0)))
7835 poly_int64 bitsize, bitpos;
7836 tree offset;
7837 machine_mode mode;
7838 int unsignedp, reversep, volatilep;
7839 tree base
7840 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7841 &offset, &mode, &unsignedp, &reversep,
7842 &volatilep);
7843 /* If the reference was to a (constant) zero offset, we can use
7844 the address of the base if it has the same base type
7845 as the result type and the pointer type is unqualified. */
7846 if (!offset
7847 && known_eq (bitpos, 0)
7848 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7849 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7850 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7851 return fold_convert_loc (loc, type,
7852 build_fold_addr_expr_loc (loc, base));
7855 if (TREE_CODE (op0) == MODIFY_EXPR
7856 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7857 /* Detect assigning a bitfield. */
7858 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7859 && DECL_BIT_FIELD
7860 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7862 /* Don't leave an assignment inside a conversion
7863 unless assigning a bitfield. */
7864 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7865 /* First do the assignment, then return converted constant. */
7866 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7867 TREE_NO_WARNING (tem) = 1;
7868 TREE_USED (tem) = 1;
7869 return tem;
7872 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7873 constants (if x has signed type, the sign bit cannot be set
7874 in c). This folds extension into the BIT_AND_EXPR.
7875 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7876 very likely don't have maximal range for their precision and this
7877 transformation effectively doesn't preserve non-maximal ranges. */
7878 if (TREE_CODE (type) == INTEGER_TYPE
7879 && TREE_CODE (op0) == BIT_AND_EXPR
7880 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7882 tree and_expr = op0;
7883 tree and0 = TREE_OPERAND (and_expr, 0);
7884 tree and1 = TREE_OPERAND (and_expr, 1);
7885 int change = 0;
7887 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7888 || (TYPE_PRECISION (type)
7889 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7890 change = 1;
7891 else if (TYPE_PRECISION (TREE_TYPE (and1))
7892 <= HOST_BITS_PER_WIDE_INT
7893 && tree_fits_uhwi_p (and1))
7895 unsigned HOST_WIDE_INT cst;
7897 cst = tree_to_uhwi (and1);
7898 cst &= HOST_WIDE_INT_M1U
7899 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7900 change = (cst == 0);
7901 if (change
7902 && !flag_syntax_only
7903 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7904 == ZERO_EXTEND))
7906 tree uns = unsigned_type_for (TREE_TYPE (and0));
7907 and0 = fold_convert_loc (loc, uns, and0);
7908 and1 = fold_convert_loc (loc, uns, and1);
7911 if (change)
7913 tem = force_fit_type (type, wi::to_widest (and1), 0,
7914 TREE_OVERFLOW (and1));
7915 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7916 fold_convert_loc (loc, type, and0), tem);
7920 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7921 cast (T1)X will fold away. We assume that this happens when X itself
7922 is a cast. */
7923 if (POINTER_TYPE_P (type)
7924 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7925 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7927 tree arg00 = TREE_OPERAND (arg0, 0);
7928 tree arg01 = TREE_OPERAND (arg0, 1);
7930 return fold_build_pointer_plus_loc
7931 (loc, fold_convert_loc (loc, type, arg00), arg01);
7934 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7935 of the same precision, and X is an integer type not narrower than
7936 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7937 if (INTEGRAL_TYPE_P (type)
7938 && TREE_CODE (op0) == BIT_NOT_EXPR
7939 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7940 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7941 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7943 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7944 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7945 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7946 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7947 fold_convert_loc (loc, type, tem));
7950 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7951 type of X and Y (integer types only). */
7952 if (INTEGRAL_TYPE_P (type)
7953 && TREE_CODE (op0) == MULT_EXPR
7954 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7955 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7957 /* Be careful not to introduce new overflows. */
7958 tree mult_type;
7959 if (TYPE_OVERFLOW_WRAPS (type))
7960 mult_type = type;
7961 else
7962 mult_type = unsigned_type_for (type);
7964 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
7966 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
7967 fold_convert_loc (loc, mult_type,
7968 TREE_OPERAND (op0, 0)),
7969 fold_convert_loc (loc, mult_type,
7970 TREE_OPERAND (op0, 1)));
7971 return fold_convert_loc (loc, type, tem);
7975 return NULL_TREE;
7977 case VIEW_CONVERT_EXPR:
7978 if (TREE_CODE (op0) == MEM_REF)
7980 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
7981 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
7982 tem = fold_build2_loc (loc, MEM_REF, type,
7983 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
7984 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
7985 return tem;
7988 return NULL_TREE;
7990 case NEGATE_EXPR:
7991 tem = fold_negate_expr (loc, arg0);
7992 if (tem)
7993 return fold_convert_loc (loc, type, tem);
7994 return NULL_TREE;
7996 case ABS_EXPR:
7997 /* Convert fabs((double)float) into (double)fabsf(float). */
7998 if (TREE_CODE (arg0) == NOP_EXPR
7999 && TREE_CODE (type) == REAL_TYPE)
8001 tree targ0 = strip_float_extensions (arg0);
8002 if (targ0 != arg0)
8003 return fold_convert_loc (loc, type,
8004 fold_build1_loc (loc, ABS_EXPR,
8005 TREE_TYPE (targ0),
8006 targ0));
8008 return NULL_TREE;
8010 case BIT_NOT_EXPR:
8011 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8012 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8013 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8014 fold_convert_loc (loc, type,
8015 TREE_OPERAND (arg0, 0)))))
8016 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8017 fold_convert_loc (loc, type,
8018 TREE_OPERAND (arg0, 1)));
8019 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8020 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8021 fold_convert_loc (loc, type,
8022 TREE_OPERAND (arg0, 1)))))
8023 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8024 fold_convert_loc (loc, type,
8025 TREE_OPERAND (arg0, 0)), tem);
8027 return NULL_TREE;
8029 case TRUTH_NOT_EXPR:
8030 /* Note that the operand of this must be an int
8031 and its values must be 0 or 1.
8032 ("true" is a fixed value perhaps depending on the language,
8033 but we don't handle values other than 1 correctly yet.) */
8034 tem = fold_truth_not_expr (loc, arg0);
8035 if (!tem)
8036 return NULL_TREE;
8037 return fold_convert_loc (loc, type, tem);
8039 case INDIRECT_REF:
8040 /* Fold *&X to X if X is an lvalue. */
8041 if (TREE_CODE (op0) == ADDR_EXPR)
8043 tree op00 = TREE_OPERAND (op0, 0);
8044 if ((VAR_P (op00)
8045 || TREE_CODE (op00) == PARM_DECL
8046 || TREE_CODE (op00) == RESULT_DECL)
8047 && !TREE_READONLY (op00))
8048 return op00;
8050 return NULL_TREE;
8052 default:
8053 return NULL_TREE;
8054 } /* switch (code) */
8058 /* If the operation was a conversion do _not_ mark a resulting constant
8059 with TREE_OVERFLOW if the original constant was not. These conversions
8060 have implementation defined behavior and retaining the TREE_OVERFLOW
8061 flag here would confuse later passes such as VRP. */
8062 tree
8063 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8064 tree type, tree op0)
8066 tree res = fold_unary_loc (loc, code, type, op0);
8067 if (res
8068 && TREE_CODE (res) == INTEGER_CST
8069 && TREE_CODE (op0) == INTEGER_CST
8070 && CONVERT_EXPR_CODE_P (code))
8071 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8073 return res;
8076 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8077 operands OP0 and OP1. LOC is the location of the resulting expression.
8078 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8079 Return the folded expression if folding is successful. Otherwise,
8080 return NULL_TREE. */
8081 static tree
8082 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8083 tree arg0, tree arg1, tree op0, tree op1)
8085 tree tem;
8087 /* We only do these simplifications if we are optimizing. */
8088 if (!optimize)
8089 return NULL_TREE;
8091 /* Check for things like (A || B) && (A || C). We can convert this
8092 to A || (B && C). Note that either operator can be any of the four
8093 truth and/or operations and the transformation will still be
8094 valid. Also note that we only care about order for the
8095 ANDIF and ORIF operators. If B contains side effects, this
8096 might change the truth-value of A. */
8097 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8098 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8099 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8100 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8101 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8102 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8104 tree a00 = TREE_OPERAND (arg0, 0);
8105 tree a01 = TREE_OPERAND (arg0, 1);
8106 tree a10 = TREE_OPERAND (arg1, 0);
8107 tree a11 = TREE_OPERAND (arg1, 1);
8108 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8109 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8110 && (code == TRUTH_AND_EXPR
8111 || code == TRUTH_OR_EXPR));
8113 if (operand_equal_p (a00, a10, 0))
8114 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8115 fold_build2_loc (loc, code, type, a01, a11));
8116 else if (commutative && operand_equal_p (a00, a11, 0))
8117 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8118 fold_build2_loc (loc, code, type, a01, a10));
8119 else if (commutative && operand_equal_p (a01, a10, 0))
8120 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8121 fold_build2_loc (loc, code, type, a00, a11));
8123 /* This case if tricky because we must either have commutative
8124 operators or else A10 must not have side-effects. */
8126 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8127 && operand_equal_p (a01, a11, 0))
8128 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8129 fold_build2_loc (loc, code, type, a00, a10),
8130 a01);
8133 /* See if we can build a range comparison. */
8134 if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
8135 return tem;
8137 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8138 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8140 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8141 if (tem)
8142 return fold_build2_loc (loc, code, type, tem, arg1);
8145 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8146 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8148 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8149 if (tem)
8150 return fold_build2_loc (loc, code, type, arg0, tem);
8153 /* Check for the possibility of merging component references. If our
8154 lhs is another similar operation, try to merge its rhs with our
8155 rhs. Then try to merge our lhs and rhs. */
8156 if (TREE_CODE (arg0) == code
8157 && (tem = fold_truth_andor_1 (loc, code, type,
8158 TREE_OPERAND (arg0, 1), arg1)) != 0)
8159 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8161 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8162 return tem;
8164 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8165 && !flag_sanitize_coverage
8166 && (code == TRUTH_AND_EXPR
8167 || code == TRUTH_ANDIF_EXPR
8168 || code == TRUTH_OR_EXPR
8169 || code == TRUTH_ORIF_EXPR))
8171 enum tree_code ncode, icode;
8173 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8174 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8175 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8177 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8178 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8179 We don't want to pack more than two leafs to a non-IF AND/OR
8180 expression.
8181 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8182 equal to IF-CODE, then we don't want to add right-hand operand.
8183 If the inner right-hand side of left-hand operand has
8184 side-effects, or isn't simple, then we can't add to it,
8185 as otherwise we might destroy if-sequence. */
8186 if (TREE_CODE (arg0) == icode
8187 && simple_operand_p_2 (arg1)
8188 /* Needed for sequence points to handle trappings, and
8189 side-effects. */
8190 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8192 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8193 arg1);
8194 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8195 tem);
8197 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8198 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8199 else if (TREE_CODE (arg1) == icode
8200 && simple_operand_p_2 (arg0)
8201 /* Needed for sequence points to handle trappings, and
8202 side-effects. */
8203 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8205 tem = fold_build2_loc (loc, ncode, type,
8206 arg0, TREE_OPERAND (arg1, 0));
8207 return fold_build2_loc (loc, icode, type, tem,
8208 TREE_OPERAND (arg1, 1));
8210 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8211 into (A OR B).
8212 For sequence point consistancy, we need to check for trapping,
8213 and side-effects. */
8214 else if (code == icode && simple_operand_p_2 (arg0)
8215 && simple_operand_p_2 (arg1))
8216 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8219 return NULL_TREE;
8222 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8223 by changing CODE to reduce the magnitude of constants involved in
8224 ARG0 of the comparison.
8225 Returns a canonicalized comparison tree if a simplification was
8226 possible, otherwise returns NULL_TREE.
8227 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8228 valid if signed overflow is undefined. */
8230 static tree
8231 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8232 tree arg0, tree arg1,
8233 bool *strict_overflow_p)
8235 enum tree_code code0 = TREE_CODE (arg0);
8236 tree t, cst0 = NULL_TREE;
8237 int sgn0;
8239 /* Match A +- CST code arg1. We can change this only if overflow
8240 is undefined. */
8241 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8242 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8243 /* In principle pointers also have undefined overflow behavior,
8244 but that causes problems elsewhere. */
8245 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8246 && (code0 == MINUS_EXPR
8247 || code0 == PLUS_EXPR)
8248 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8249 return NULL_TREE;
8251 /* Identify the constant in arg0 and its sign. */
8252 cst0 = TREE_OPERAND (arg0, 1);
8253 sgn0 = tree_int_cst_sgn (cst0);
8255 /* Overflowed constants and zero will cause problems. */
8256 if (integer_zerop (cst0)
8257 || TREE_OVERFLOW (cst0))
8258 return NULL_TREE;
8260 /* See if we can reduce the magnitude of the constant in
8261 arg0 by changing the comparison code. */
8262 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8263 if (code == LT_EXPR
8264 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8265 code = LE_EXPR;
8266 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8267 else if (code == GT_EXPR
8268 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8269 code = GE_EXPR;
8270 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8271 else if (code == LE_EXPR
8272 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8273 code = LT_EXPR;
8274 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8275 else if (code == GE_EXPR
8276 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8277 code = GT_EXPR;
8278 else
8279 return NULL_TREE;
8280 *strict_overflow_p = true;
8282 /* Now build the constant reduced in magnitude. But not if that
8283 would produce one outside of its types range. */
8284 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8285 && ((sgn0 == 1
8286 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8287 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8288 || (sgn0 == -1
8289 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8290 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8291 return NULL_TREE;
8293 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8294 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8295 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8296 t = fold_convert (TREE_TYPE (arg1), t);
8298 return fold_build2_loc (loc, code, type, t, arg1);
8301 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8302 overflow further. Try to decrease the magnitude of constants involved
8303 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8304 and put sole constants at the second argument position.
8305 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8307 static tree
8308 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8309 tree arg0, tree arg1)
8311 tree t;
8312 bool strict_overflow_p;
8313 const char * const warnmsg = G_("assuming signed overflow does not occur "
8314 "when reducing constant in comparison");
8316 /* Try canonicalization by simplifying arg0. */
8317 strict_overflow_p = false;
8318 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8319 &strict_overflow_p);
8320 if (t)
8322 if (strict_overflow_p)
8323 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8324 return t;
8327 /* Try canonicalization by simplifying arg1 using the swapped
8328 comparison. */
8329 code = swap_tree_comparison (code);
8330 strict_overflow_p = false;
8331 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8332 &strict_overflow_p);
8333 if (t && strict_overflow_p)
8334 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8335 return t;
8338 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8339 space. This is used to avoid issuing overflow warnings for
8340 expressions like &p->x which can not wrap. */
8342 static bool
8343 pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
8345 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8346 return true;
8348 if (maybe_lt (bitpos, 0))
8349 return true;
8351 poly_wide_int wi_offset;
8352 int precision = TYPE_PRECISION (TREE_TYPE (base));
8353 if (offset == NULL_TREE)
8354 wi_offset = wi::zero (precision);
8355 else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
8356 return true;
8357 else
8358 wi_offset = wi::to_poly_wide (offset);
8360 bool overflow;
8361 poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
8362 precision);
8363 poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8364 if (overflow)
8365 return true;
8367 poly_uint64 total_hwi, size;
8368 if (!total.to_uhwi (&total_hwi)
8369 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
8370 &size)
8371 || known_eq (size, 0U))
8372 return true;
8374 if (known_le (total_hwi, size))
8375 return false;
8377 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8378 array. */
8379 if (TREE_CODE (base) == ADDR_EXPR
8380 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
8381 &size)
8382 && maybe_ne (size, 0U)
8383 && known_le (total_hwi, size))
8384 return false;
8386 return true;
8389 /* Return a positive integer when the symbol DECL is known to have
8390 a nonzero address, zero when it's known not to (e.g., it's a weak
8391 symbol), and a negative integer when the symbol is not yet in the
8392 symbol table and so whether or not its address is zero is unknown.
8393 For function local objects always return positive integer. */
8394 static int
8395 maybe_nonzero_address (tree decl)
8397 if (DECL_P (decl) && decl_in_symtab_p (decl))
8398 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8399 return symbol->nonzero_address ();
8401 /* Function local objects are never NULL. */
8402 if (DECL_P (decl)
8403 && (DECL_CONTEXT (decl)
8404 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8405 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8406 return 1;
8408 return -1;
8411 /* Subroutine of fold_binary. This routine performs all of the
8412 transformations that are common to the equality/inequality
8413 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8414 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8415 fold_binary should call fold_binary. Fold a comparison with
8416 tree code CODE and type TYPE with operands OP0 and OP1. Return
8417 the folded comparison or NULL_TREE. */
8419 static tree
8420 fold_comparison (location_t loc, enum tree_code code, tree type,
8421 tree op0, tree op1)
8423 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8424 tree arg0, arg1, tem;
8426 arg0 = op0;
8427 arg1 = op1;
8429 STRIP_SIGN_NOPS (arg0);
8430 STRIP_SIGN_NOPS (arg1);
8432 /* For comparisons of pointers we can decompose it to a compile time
8433 comparison of the base objects and the offsets into the object.
8434 This requires at least one operand being an ADDR_EXPR or a
8435 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8436 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8437 && (TREE_CODE (arg0) == ADDR_EXPR
8438 || TREE_CODE (arg1) == ADDR_EXPR
8439 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8440 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8442 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8443 poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
8444 machine_mode mode;
8445 int volatilep, reversep, unsignedp;
8446 bool indirect_base0 = false, indirect_base1 = false;
8448 /* Get base and offset for the access. Strip ADDR_EXPR for
8449 get_inner_reference, but put it back by stripping INDIRECT_REF
8450 off the base object if possible. indirect_baseN will be true
8451 if baseN is not an address but refers to the object itself. */
8452 base0 = arg0;
8453 if (TREE_CODE (arg0) == ADDR_EXPR)
8455 base0
8456 = get_inner_reference (TREE_OPERAND (arg0, 0),
8457 &bitsize, &bitpos0, &offset0, &mode,
8458 &unsignedp, &reversep, &volatilep);
8459 if (TREE_CODE (base0) == INDIRECT_REF)
8460 base0 = TREE_OPERAND (base0, 0);
8461 else
8462 indirect_base0 = true;
8464 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8466 base0 = TREE_OPERAND (arg0, 0);
8467 STRIP_SIGN_NOPS (base0);
8468 if (TREE_CODE (base0) == ADDR_EXPR)
8470 base0
8471 = get_inner_reference (TREE_OPERAND (base0, 0),
8472 &bitsize, &bitpos0, &offset0, &mode,
8473 &unsignedp, &reversep, &volatilep);
8474 if (TREE_CODE (base0) == INDIRECT_REF)
8475 base0 = TREE_OPERAND (base0, 0);
8476 else
8477 indirect_base0 = true;
8479 if (offset0 == NULL_TREE || integer_zerop (offset0))
8480 offset0 = TREE_OPERAND (arg0, 1);
8481 else
8482 offset0 = size_binop (PLUS_EXPR, offset0,
8483 TREE_OPERAND (arg0, 1));
8484 if (poly_int_tree_p (offset0))
8486 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
8487 TYPE_PRECISION (sizetype));
8488 tem <<= LOG2_BITS_PER_UNIT;
8489 tem += bitpos0;
8490 if (tem.to_shwi (&bitpos0))
8491 offset0 = NULL_TREE;
8495 base1 = arg1;
8496 if (TREE_CODE (arg1) == ADDR_EXPR)
8498 base1
8499 = get_inner_reference (TREE_OPERAND (arg1, 0),
8500 &bitsize, &bitpos1, &offset1, &mode,
8501 &unsignedp, &reversep, &volatilep);
8502 if (TREE_CODE (base1) == INDIRECT_REF)
8503 base1 = TREE_OPERAND (base1, 0);
8504 else
8505 indirect_base1 = true;
8507 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8509 base1 = TREE_OPERAND (arg1, 0);
8510 STRIP_SIGN_NOPS (base1);
8511 if (TREE_CODE (base1) == ADDR_EXPR)
8513 base1
8514 = get_inner_reference (TREE_OPERAND (base1, 0),
8515 &bitsize, &bitpos1, &offset1, &mode,
8516 &unsignedp, &reversep, &volatilep);
8517 if (TREE_CODE (base1) == INDIRECT_REF)
8518 base1 = TREE_OPERAND (base1, 0);
8519 else
8520 indirect_base1 = true;
8522 if (offset1 == NULL_TREE || integer_zerop (offset1))
8523 offset1 = TREE_OPERAND (arg1, 1);
8524 else
8525 offset1 = size_binop (PLUS_EXPR, offset1,
8526 TREE_OPERAND (arg1, 1));
8527 if (poly_int_tree_p (offset1))
8529 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
8530 TYPE_PRECISION (sizetype));
8531 tem <<= LOG2_BITS_PER_UNIT;
8532 tem += bitpos1;
8533 if (tem.to_shwi (&bitpos1))
8534 offset1 = NULL_TREE;
8538 /* If we have equivalent bases we might be able to simplify. */
8539 if (indirect_base0 == indirect_base1
8540 && operand_equal_p (base0, base1,
8541 indirect_base0 ? OEP_ADDRESS_OF : 0))
8543 /* We can fold this expression to a constant if the non-constant
8544 offset parts are equal. */
8545 if (offset0 == offset1
8546 || (offset0 && offset1
8547 && operand_equal_p (offset0, offset1, 0)))
8549 if (!equality_code
8550 && maybe_ne (bitpos0, bitpos1)
8551 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8552 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8553 fold_overflow_warning (("assuming pointer wraparound does not "
8554 "occur when comparing P +- C1 with "
8555 "P +- C2"),
8556 WARN_STRICT_OVERFLOW_CONDITIONAL);
8558 switch (code)
8560 case EQ_EXPR:
8561 if (known_eq (bitpos0, bitpos1))
8562 return boolean_true_node;
8563 if (known_ne (bitpos0, bitpos1))
8564 return boolean_false_node;
8565 break;
8566 case NE_EXPR:
8567 if (known_ne (bitpos0, bitpos1))
8568 return boolean_true_node;
8569 if (known_eq (bitpos0, bitpos1))
8570 return boolean_false_node;
8571 break;
8572 case LT_EXPR:
8573 if (known_lt (bitpos0, bitpos1))
8574 return boolean_true_node;
8575 if (known_ge (bitpos0, bitpos1))
8576 return boolean_false_node;
8577 break;
8578 case LE_EXPR:
8579 if (known_le (bitpos0, bitpos1))
8580 return boolean_true_node;
8581 if (known_gt (bitpos0, bitpos1))
8582 return boolean_false_node;
8583 break;
8584 case GE_EXPR:
8585 if (known_ge (bitpos0, bitpos1))
8586 return boolean_true_node;
8587 if (known_lt (bitpos0, bitpos1))
8588 return boolean_false_node;
8589 break;
8590 case GT_EXPR:
8591 if (known_gt (bitpos0, bitpos1))
8592 return boolean_true_node;
8593 if (known_le (bitpos0, bitpos1))
8594 return boolean_false_node;
8595 break;
8596 default:;
8599 /* We can simplify the comparison to a comparison of the variable
8600 offset parts if the constant offset parts are equal.
8601 Be careful to use signed sizetype here because otherwise we
8602 mess with array offsets in the wrong way. This is possible
8603 because pointer arithmetic is restricted to retain within an
8604 object and overflow on pointer differences is undefined as of
8605 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8606 else if (known_eq (bitpos0, bitpos1))
8608 /* By converting to signed sizetype we cover middle-end pointer
8609 arithmetic which operates on unsigned pointer types of size
8610 type size and ARRAY_REF offsets which are properly sign or
8611 zero extended from their type in case it is narrower than
8612 sizetype. */
8613 if (offset0 == NULL_TREE)
8614 offset0 = build_int_cst (ssizetype, 0);
8615 else
8616 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8617 if (offset1 == NULL_TREE)
8618 offset1 = build_int_cst (ssizetype, 0);
8619 else
8620 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8622 if (!equality_code
8623 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8624 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8625 fold_overflow_warning (("assuming pointer wraparound does not "
8626 "occur when comparing P +- C1 with "
8627 "P +- C2"),
8628 WARN_STRICT_OVERFLOW_COMPARISON);
8630 return fold_build2_loc (loc, code, type, offset0, offset1);
8633 /* For equal offsets we can simplify to a comparison of the
8634 base addresses. */
8635 else if (known_eq (bitpos0, bitpos1)
8636 && (indirect_base0
8637 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8638 && (indirect_base1
8639 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8640 && ((offset0 == offset1)
8641 || (offset0 && offset1
8642 && operand_equal_p (offset0, offset1, 0))))
8644 if (indirect_base0)
8645 base0 = build_fold_addr_expr_loc (loc, base0);
8646 if (indirect_base1)
8647 base1 = build_fold_addr_expr_loc (loc, base1);
8648 return fold_build2_loc (loc, code, type, base0, base1);
8650 /* Comparison between an ordinary (non-weak) symbol and a null
8651 pointer can be eliminated since such symbols must have a non
8652 null address. In C, relational expressions between pointers
8653 to objects and null pointers are undefined. The results
8654 below follow the C++ rules with the additional property that
8655 every object pointer compares greater than a null pointer.
8657 else if (((DECL_P (base0)
8658 && maybe_nonzero_address (base0) > 0
8659 /* Avoid folding references to struct members at offset 0 to
8660 prevent tests like '&ptr->firstmember == 0' from getting
8661 eliminated. When ptr is null, although the -> expression
8662 is strictly speaking invalid, GCC retains it as a matter
8663 of QoI. See PR c/44555. */
8664 && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
8665 || CONSTANT_CLASS_P (base0))
8666 && indirect_base0
8667 /* The caller guarantees that when one of the arguments is
8668 constant (i.e., null in this case) it is second. */
8669 && integer_zerop (arg1))
8671 switch (code)
8673 case EQ_EXPR:
8674 case LE_EXPR:
8675 case LT_EXPR:
8676 return constant_boolean_node (false, type);
8677 case GE_EXPR:
8678 case GT_EXPR:
8679 case NE_EXPR:
8680 return constant_boolean_node (true, type);
8681 default:
8682 gcc_unreachable ();
8687 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8688 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8689 the resulting offset is smaller in absolute value than the
8690 original one and has the same sign. */
8691 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8692 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8693 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8694 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8695 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8696 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8697 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8698 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8700 tree const1 = TREE_OPERAND (arg0, 1);
8701 tree const2 = TREE_OPERAND (arg1, 1);
8702 tree variable1 = TREE_OPERAND (arg0, 0);
8703 tree variable2 = TREE_OPERAND (arg1, 0);
8704 tree cst;
8705 const char * const warnmsg = G_("assuming signed overflow does not "
8706 "occur when combining constants around "
8707 "a comparison");
8709 /* Put the constant on the side where it doesn't overflow and is
8710 of lower absolute value and of same sign than before. */
8711 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8712 ? MINUS_EXPR : PLUS_EXPR,
8713 const2, const1);
8714 if (!TREE_OVERFLOW (cst)
8715 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8716 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8718 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8719 return fold_build2_loc (loc, code, type,
8720 variable1,
8721 fold_build2_loc (loc, TREE_CODE (arg1),
8722 TREE_TYPE (arg1),
8723 variable2, cst));
8726 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8727 ? MINUS_EXPR : PLUS_EXPR,
8728 const1, const2);
8729 if (!TREE_OVERFLOW (cst)
8730 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8731 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8733 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8734 return fold_build2_loc (loc, code, type,
8735 fold_build2_loc (loc, TREE_CODE (arg0),
8736 TREE_TYPE (arg0),
8737 variable1, cst),
8738 variable2);
8742 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8743 if (tem)
8744 return tem;
8746 /* If we are comparing an expression that just has comparisons
8747 of two integer values, arithmetic expressions of those comparisons,
8748 and constants, we can simplify it. There are only three cases
8749 to check: the two values can either be equal, the first can be
8750 greater, or the second can be greater. Fold the expression for
8751 those three values. Since each value must be 0 or 1, we have
8752 eight possibilities, each of which corresponds to the constant 0
8753 or 1 or one of the six possible comparisons.
8755 This handles common cases like (a > b) == 0 but also handles
8756 expressions like ((x > y) - (y > x)) > 0, which supposedly
8757 occur in macroized code. */
8759 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8761 tree cval1 = 0, cval2 = 0;
8762 int save_p = 0;
8764 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8765 /* Don't handle degenerate cases here; they should already
8766 have been handled anyway. */
8767 && cval1 != 0 && cval2 != 0
8768 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8769 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8770 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8771 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8772 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8773 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8774 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8776 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8777 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8779 /* We can't just pass T to eval_subst in case cval1 or cval2
8780 was the same as ARG1. */
8782 tree high_result
8783 = fold_build2_loc (loc, code, type,
8784 eval_subst (loc, arg0, cval1, maxval,
8785 cval2, minval),
8786 arg1);
8787 tree equal_result
8788 = fold_build2_loc (loc, code, type,
8789 eval_subst (loc, arg0, cval1, maxval,
8790 cval2, maxval),
8791 arg1);
8792 tree low_result
8793 = fold_build2_loc (loc, code, type,
8794 eval_subst (loc, arg0, cval1, minval,
8795 cval2, maxval),
8796 arg1);
8798 /* All three of these results should be 0 or 1. Confirm they are.
8799 Then use those values to select the proper code to use. */
8801 if (TREE_CODE (high_result) == INTEGER_CST
8802 && TREE_CODE (equal_result) == INTEGER_CST
8803 && TREE_CODE (low_result) == INTEGER_CST)
8805 /* Make a 3-bit mask with the high-order bit being the
8806 value for `>', the next for '=', and the low for '<'. */
8807 switch ((integer_onep (high_result) * 4)
8808 + (integer_onep (equal_result) * 2)
8809 + integer_onep (low_result))
8811 case 0:
8812 /* Always false. */
8813 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8814 case 1:
8815 code = LT_EXPR;
8816 break;
8817 case 2:
8818 code = EQ_EXPR;
8819 break;
8820 case 3:
8821 code = LE_EXPR;
8822 break;
8823 case 4:
8824 code = GT_EXPR;
8825 break;
8826 case 5:
8827 code = NE_EXPR;
8828 break;
8829 case 6:
8830 code = GE_EXPR;
8831 break;
8832 case 7:
8833 /* Always true. */
8834 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8837 if (save_p)
8839 tem = save_expr (build2 (code, type, cval1, cval2));
8840 protected_set_expr_location (tem, loc);
8841 return tem;
8843 return fold_build2_loc (loc, code, type, cval1, cval2);
8848 return NULL_TREE;
8852 /* Subroutine of fold_binary. Optimize complex multiplications of the
8853 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8854 argument EXPR represents the expression "z" of type TYPE. */
8856 static tree
8857 fold_mult_zconjz (location_t loc, tree type, tree expr)
8859 tree itype = TREE_TYPE (type);
8860 tree rpart, ipart, tem;
8862 if (TREE_CODE (expr) == COMPLEX_EXPR)
8864 rpart = TREE_OPERAND (expr, 0);
8865 ipart = TREE_OPERAND (expr, 1);
8867 else if (TREE_CODE (expr) == COMPLEX_CST)
8869 rpart = TREE_REALPART (expr);
8870 ipart = TREE_IMAGPART (expr);
8872 else
8874 expr = save_expr (expr);
8875 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8876 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8879 rpart = save_expr (rpart);
8880 ipart = save_expr (ipart);
8881 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8882 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8883 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8884 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8885 build_zero_cst (itype));
8889 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8890 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8891 true if successful. */
8893 static bool
8894 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
8896 unsigned int i;
8898 if (TREE_CODE (arg) == VECTOR_CST)
8900 for (i = 0; i < VECTOR_CST_NELTS (arg); ++i)
8901 elts[i] = VECTOR_CST_ELT (arg, i);
8903 else if (TREE_CODE (arg) == CONSTRUCTOR)
8905 constructor_elt *elt;
8907 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8908 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8909 return false;
8910 else
8911 elts[i] = elt->value;
8913 else
8914 return false;
8915 for (; i < nelts; i++)
8916 elts[i]
8917 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8918 return true;
8921 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8922 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8923 NULL_TREE otherwise. */
8925 static tree
8926 fold_vec_perm (tree type, tree arg0, tree arg1, vec_perm_indices sel)
8928 unsigned int i;
8929 bool need_ctor = false;
8931 unsigned int nelts = sel.length ();
8932 gcc_assert (TYPE_VECTOR_SUBPARTS (type) == nelts
8933 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)) == nelts
8934 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)) == nelts);
8935 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8936 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8937 return NULL_TREE;
8939 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
8940 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
8941 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
8942 return NULL_TREE;
8944 tree_vector_builder out_elts (type, nelts, 1);
8945 for (i = 0; i < nelts; i++)
8947 if (!CONSTANT_CLASS_P (in_elts[sel[i]]))
8948 need_ctor = true;
8949 out_elts.quick_push (unshare_expr (in_elts[sel[i]]));
8952 if (need_ctor)
8954 vec<constructor_elt, va_gc> *v;
8955 vec_alloc (v, nelts);
8956 for (i = 0; i < nelts; i++)
8957 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
8958 return build_constructor (type, v);
8960 else
8961 return out_elts.build ();
8964 /* Try to fold a pointer difference of type TYPE two address expressions of
8965 array references AREF0 and AREF1 using location LOC. Return a
8966 simplified expression for the difference or NULL_TREE. */
8968 static tree
8969 fold_addr_of_array_ref_difference (location_t loc, tree type,
8970 tree aref0, tree aref1,
8971 bool use_pointer_diff)
8973 tree base0 = TREE_OPERAND (aref0, 0);
8974 tree base1 = TREE_OPERAND (aref1, 0);
8975 tree base_offset = build_int_cst (type, 0);
8977 /* If the bases are array references as well, recurse. If the bases
8978 are pointer indirections compute the difference of the pointers.
8979 If the bases are equal, we are set. */
8980 if ((TREE_CODE (base0) == ARRAY_REF
8981 && TREE_CODE (base1) == ARRAY_REF
8982 && (base_offset
8983 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
8984 use_pointer_diff)))
8985 || (INDIRECT_REF_P (base0)
8986 && INDIRECT_REF_P (base1)
8987 && (base_offset
8988 = use_pointer_diff
8989 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
8990 TREE_OPERAND (base0, 0),
8991 TREE_OPERAND (base1, 0))
8992 : fold_binary_loc (loc, MINUS_EXPR, type,
8993 fold_convert (type,
8994 TREE_OPERAND (base0, 0)),
8995 fold_convert (type,
8996 TREE_OPERAND (base1, 0)))))
8997 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
8999 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9000 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9001 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9002 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
9003 return fold_build2_loc (loc, PLUS_EXPR, type,
9004 base_offset,
9005 fold_build2_loc (loc, MULT_EXPR, type,
9006 diff, esz));
9008 return NULL_TREE;
9011 /* If the real or vector real constant CST of type TYPE has an exact
9012 inverse, return it, else return NULL. */
9014 tree
9015 exact_inverse (tree type, tree cst)
9017 REAL_VALUE_TYPE r;
9018 tree unit_type;
9019 machine_mode mode;
9021 switch (TREE_CODE (cst))
9023 case REAL_CST:
9024 r = TREE_REAL_CST (cst);
9026 if (exact_real_inverse (TYPE_MODE (type), &r))
9027 return build_real (type, r);
9029 return NULL_TREE;
9031 case VECTOR_CST:
9033 unit_type = TREE_TYPE (type);
9034 mode = TYPE_MODE (unit_type);
9036 tree_vector_builder elts;
9037 if (!elts.new_unary_operation (type, cst, false))
9038 return NULL_TREE;
9039 unsigned int count = elts.encoded_nelts ();
9040 for (unsigned int i = 0; i < count; ++i)
9042 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9043 if (!exact_real_inverse (mode, &r))
9044 return NULL_TREE;
9045 elts.quick_push (build_real (unit_type, r));
9048 return elts.build ();
9051 default:
9052 return NULL_TREE;
9056 /* Mask out the tz least significant bits of X of type TYPE where
9057 tz is the number of trailing zeroes in Y. */
9058 static wide_int
9059 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9061 int tz = wi::ctz (y);
9062 if (tz > 0)
9063 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9064 return x;
9067 /* Return true when T is an address and is known to be nonzero.
9068 For floating point we further ensure that T is not denormal.
9069 Similar logic is present in nonzero_address in rtlanal.h.
9071 If the return value is based on the assumption that signed overflow
9072 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9073 change *STRICT_OVERFLOW_P. */
9075 static bool
9076 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9078 tree type = TREE_TYPE (t);
9079 enum tree_code code;
9081 /* Doing something useful for floating point would need more work. */
9082 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9083 return false;
9085 code = TREE_CODE (t);
9086 switch (TREE_CODE_CLASS (code))
9088 case tcc_unary:
9089 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9090 strict_overflow_p);
9091 case tcc_binary:
9092 case tcc_comparison:
9093 return tree_binary_nonzero_warnv_p (code, type,
9094 TREE_OPERAND (t, 0),
9095 TREE_OPERAND (t, 1),
9096 strict_overflow_p);
9097 case tcc_constant:
9098 case tcc_declaration:
9099 case tcc_reference:
9100 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9102 default:
9103 break;
9106 switch (code)
9108 case TRUTH_NOT_EXPR:
9109 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9110 strict_overflow_p);
9112 case TRUTH_AND_EXPR:
9113 case TRUTH_OR_EXPR:
9114 case TRUTH_XOR_EXPR:
9115 return tree_binary_nonzero_warnv_p (code, type,
9116 TREE_OPERAND (t, 0),
9117 TREE_OPERAND (t, 1),
9118 strict_overflow_p);
9120 case COND_EXPR:
9121 case CONSTRUCTOR:
9122 case OBJ_TYPE_REF:
9123 case ASSERT_EXPR:
9124 case ADDR_EXPR:
9125 case WITH_SIZE_EXPR:
9126 case SSA_NAME:
9127 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9129 case COMPOUND_EXPR:
9130 case MODIFY_EXPR:
9131 case BIND_EXPR:
9132 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9133 strict_overflow_p);
9135 case SAVE_EXPR:
9136 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9137 strict_overflow_p);
9139 case CALL_EXPR:
9141 tree fndecl = get_callee_fndecl (t);
9142 if (!fndecl) return false;
9143 if (flag_delete_null_pointer_checks && !flag_check_new
9144 && DECL_IS_OPERATOR_NEW (fndecl)
9145 && !TREE_NOTHROW (fndecl))
9146 return true;
9147 if (flag_delete_null_pointer_checks
9148 && lookup_attribute ("returns_nonnull",
9149 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9150 return true;
9151 return alloca_call_p (t);
9154 default:
9155 break;
9157 return false;
9160 /* Return true when T is an address and is known to be nonzero.
9161 Handle warnings about undefined signed overflow. */
9163 bool
9164 tree_expr_nonzero_p (tree t)
9166 bool ret, strict_overflow_p;
9168 strict_overflow_p = false;
9169 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9170 if (strict_overflow_p)
9171 fold_overflow_warning (("assuming signed overflow does not occur when "
9172 "determining that expression is always "
9173 "non-zero"),
9174 WARN_STRICT_OVERFLOW_MISC);
9175 return ret;
9178 /* Return true if T is known not to be equal to an integer W. */
9180 bool
9181 expr_not_equal_to (tree t, const wide_int &w)
9183 wide_int min, max, nz;
9184 value_range_type rtype;
9185 switch (TREE_CODE (t))
9187 case INTEGER_CST:
9188 return wi::to_wide (t) != w;
9190 case SSA_NAME:
9191 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9192 return false;
9193 rtype = get_range_info (t, &min, &max);
9194 if (rtype == VR_RANGE)
9196 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9197 return true;
9198 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9199 return true;
9201 else if (rtype == VR_ANTI_RANGE
9202 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9203 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9204 return true;
9205 /* If T has some known zero bits and W has any of those bits set,
9206 then T is known not to be equal to W. */
9207 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9208 TYPE_PRECISION (TREE_TYPE (t))), 0))
9209 return true;
9210 return false;
9212 default:
9213 return false;
9217 /* Fold a binary expression of code CODE and type TYPE with operands
9218 OP0 and OP1. LOC is the location of the resulting expression.
9219 Return the folded expression if folding is successful. Otherwise,
9220 return NULL_TREE. */
9222 tree
9223 fold_binary_loc (location_t loc, enum tree_code code, tree type,
9224 tree op0, tree op1)
9226 enum tree_code_class kind = TREE_CODE_CLASS (code);
9227 tree arg0, arg1, tem;
9228 tree t1 = NULL_TREE;
9229 bool strict_overflow_p;
9230 unsigned int prec;
9232 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9233 && TREE_CODE_LENGTH (code) == 2
9234 && op0 != NULL_TREE
9235 && op1 != NULL_TREE);
9237 arg0 = op0;
9238 arg1 = op1;
9240 /* Strip any conversions that don't change the mode. This is
9241 safe for every expression, except for a comparison expression
9242 because its signedness is derived from its operands. So, in
9243 the latter case, only strip conversions that don't change the
9244 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9245 preserved.
9247 Note that this is done as an internal manipulation within the
9248 constant folder, in order to find the simplest representation
9249 of the arguments so that their form can be studied. In any
9250 cases, the appropriate type conversions should be put back in
9251 the tree that will get out of the constant folder. */
9253 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9255 STRIP_SIGN_NOPS (arg0);
9256 STRIP_SIGN_NOPS (arg1);
9258 else
9260 STRIP_NOPS (arg0);
9261 STRIP_NOPS (arg1);
9264 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9265 constant but we can't do arithmetic on them. */
9266 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9268 tem = const_binop (code, type, arg0, arg1);
9269 if (tem != NULL_TREE)
9271 if (TREE_TYPE (tem) != type)
9272 tem = fold_convert_loc (loc, type, tem);
9273 return tem;
9277 /* If this is a commutative operation, and ARG0 is a constant, move it
9278 to ARG1 to reduce the number of tests below. */
9279 if (commutative_tree_code (code)
9280 && tree_swap_operands_p (arg0, arg1))
9281 return fold_build2_loc (loc, code, type, op1, op0);
9283 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9284 to ARG1 to reduce the number of tests below. */
9285 if (kind == tcc_comparison
9286 && tree_swap_operands_p (arg0, arg1))
9287 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9289 tem = generic_simplify (loc, code, type, op0, op1);
9290 if (tem)
9291 return tem;
9293 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9295 First check for cases where an arithmetic operation is applied to a
9296 compound, conditional, or comparison operation. Push the arithmetic
9297 operation inside the compound or conditional to see if any folding
9298 can then be done. Convert comparison to conditional for this purpose.
9299 The also optimizes non-constant cases that used to be done in
9300 expand_expr.
9302 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9303 one of the operands is a comparison and the other is a comparison, a
9304 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9305 code below would make the expression more complex. Change it to a
9306 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9307 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9309 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9310 || code == EQ_EXPR || code == NE_EXPR)
9311 && TREE_CODE (type) != VECTOR_TYPE
9312 && ((truth_value_p (TREE_CODE (arg0))
9313 && (truth_value_p (TREE_CODE (arg1))
9314 || (TREE_CODE (arg1) == BIT_AND_EXPR
9315 && integer_onep (TREE_OPERAND (arg1, 1)))))
9316 || (truth_value_p (TREE_CODE (arg1))
9317 && (truth_value_p (TREE_CODE (arg0))
9318 || (TREE_CODE (arg0) == BIT_AND_EXPR
9319 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9321 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9322 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9323 : TRUTH_XOR_EXPR,
9324 boolean_type_node,
9325 fold_convert_loc (loc, boolean_type_node, arg0),
9326 fold_convert_loc (loc, boolean_type_node, arg1));
9328 if (code == EQ_EXPR)
9329 tem = invert_truthvalue_loc (loc, tem);
9331 return fold_convert_loc (loc, type, tem);
9334 if (TREE_CODE_CLASS (code) == tcc_binary
9335 || TREE_CODE_CLASS (code) == tcc_comparison)
9337 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9339 tem = fold_build2_loc (loc, code, type,
9340 fold_convert_loc (loc, TREE_TYPE (op0),
9341 TREE_OPERAND (arg0, 1)), op1);
9342 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9343 tem);
9345 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9347 tem = fold_build2_loc (loc, code, type, op0,
9348 fold_convert_loc (loc, TREE_TYPE (op1),
9349 TREE_OPERAND (arg1, 1)));
9350 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9351 tem);
9354 if (TREE_CODE (arg0) == COND_EXPR
9355 || TREE_CODE (arg0) == VEC_COND_EXPR
9356 || COMPARISON_CLASS_P (arg0))
9358 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9359 arg0, arg1,
9360 /*cond_first_p=*/1);
9361 if (tem != NULL_TREE)
9362 return tem;
9365 if (TREE_CODE (arg1) == COND_EXPR
9366 || TREE_CODE (arg1) == VEC_COND_EXPR
9367 || COMPARISON_CLASS_P (arg1))
9369 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9370 arg1, arg0,
9371 /*cond_first_p=*/0);
9372 if (tem != NULL_TREE)
9373 return tem;
9377 switch (code)
9379 case MEM_REF:
9380 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9381 if (TREE_CODE (arg0) == ADDR_EXPR
9382 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9384 tree iref = TREE_OPERAND (arg0, 0);
9385 return fold_build2 (MEM_REF, type,
9386 TREE_OPERAND (iref, 0),
9387 int_const_binop (PLUS_EXPR, arg1,
9388 TREE_OPERAND (iref, 1)));
9391 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9392 if (TREE_CODE (arg0) == ADDR_EXPR
9393 && handled_component_p (TREE_OPERAND (arg0, 0)))
9395 tree base;
9396 poly_int64 coffset;
9397 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9398 &coffset);
9399 if (!base)
9400 return NULL_TREE;
9401 return fold_build2 (MEM_REF, type,
9402 build_fold_addr_expr (base),
9403 int_const_binop (PLUS_EXPR, arg1,
9404 size_int (coffset)));
9407 return NULL_TREE;
9409 case POINTER_PLUS_EXPR:
9410 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9411 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9412 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9413 return fold_convert_loc (loc, type,
9414 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9415 fold_convert_loc (loc, sizetype,
9416 arg1),
9417 fold_convert_loc (loc, sizetype,
9418 arg0)));
9420 return NULL_TREE;
9422 case PLUS_EXPR:
9423 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9425 /* X + (X / CST) * -CST is X % CST. */
9426 if (TREE_CODE (arg1) == MULT_EXPR
9427 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9428 && operand_equal_p (arg0,
9429 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9431 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9432 tree cst1 = TREE_OPERAND (arg1, 1);
9433 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9434 cst1, cst0);
9435 if (sum && integer_zerop (sum))
9436 return fold_convert_loc (loc, type,
9437 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9438 TREE_TYPE (arg0), arg0,
9439 cst0));
9443 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9444 one. Make sure the type is not saturating and has the signedness of
9445 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9446 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9447 if ((TREE_CODE (arg0) == MULT_EXPR
9448 || TREE_CODE (arg1) == MULT_EXPR)
9449 && !TYPE_SATURATING (type)
9450 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9451 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9452 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9454 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9455 if (tem)
9456 return tem;
9459 if (! FLOAT_TYPE_P (type))
9461 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9462 (plus (plus (mult) (mult)) (foo)) so that we can
9463 take advantage of the factoring cases below. */
9464 if (ANY_INTEGRAL_TYPE_P (type)
9465 && TYPE_OVERFLOW_WRAPS (type)
9466 && (((TREE_CODE (arg0) == PLUS_EXPR
9467 || TREE_CODE (arg0) == MINUS_EXPR)
9468 && TREE_CODE (arg1) == MULT_EXPR)
9469 || ((TREE_CODE (arg1) == PLUS_EXPR
9470 || TREE_CODE (arg1) == MINUS_EXPR)
9471 && TREE_CODE (arg0) == MULT_EXPR)))
9473 tree parg0, parg1, parg, marg;
9474 enum tree_code pcode;
9476 if (TREE_CODE (arg1) == MULT_EXPR)
9477 parg = arg0, marg = arg1;
9478 else
9479 parg = arg1, marg = arg0;
9480 pcode = TREE_CODE (parg);
9481 parg0 = TREE_OPERAND (parg, 0);
9482 parg1 = TREE_OPERAND (parg, 1);
9483 STRIP_NOPS (parg0);
9484 STRIP_NOPS (parg1);
9486 if (TREE_CODE (parg0) == MULT_EXPR
9487 && TREE_CODE (parg1) != MULT_EXPR)
9488 return fold_build2_loc (loc, pcode, type,
9489 fold_build2_loc (loc, PLUS_EXPR, type,
9490 fold_convert_loc (loc, type,
9491 parg0),
9492 fold_convert_loc (loc, type,
9493 marg)),
9494 fold_convert_loc (loc, type, parg1));
9495 if (TREE_CODE (parg0) != MULT_EXPR
9496 && TREE_CODE (parg1) == MULT_EXPR)
9497 return
9498 fold_build2_loc (loc, PLUS_EXPR, type,
9499 fold_convert_loc (loc, type, parg0),
9500 fold_build2_loc (loc, pcode, type,
9501 fold_convert_loc (loc, type, marg),
9502 fold_convert_loc (loc, type,
9503 parg1)));
9506 else
9508 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9509 to __complex__ ( x, y ). This is not the same for SNaNs or
9510 if signed zeros are involved. */
9511 if (!HONOR_SNANS (element_mode (arg0))
9512 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9513 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9515 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9516 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9517 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9518 bool arg0rz = false, arg0iz = false;
9519 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9520 || (arg0i && (arg0iz = real_zerop (arg0i))))
9522 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9523 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9524 if (arg0rz && arg1i && real_zerop (arg1i))
9526 tree rp = arg1r ? arg1r
9527 : build1 (REALPART_EXPR, rtype, arg1);
9528 tree ip = arg0i ? arg0i
9529 : build1 (IMAGPART_EXPR, rtype, arg0);
9530 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9532 else if (arg0iz && arg1r && real_zerop (arg1r))
9534 tree rp = arg0r ? arg0r
9535 : build1 (REALPART_EXPR, rtype, arg0);
9536 tree ip = arg1i ? arg1i
9537 : build1 (IMAGPART_EXPR, rtype, arg1);
9538 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9543 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9544 We associate floats only if the user has specified
9545 -fassociative-math. */
9546 if (flag_associative_math
9547 && TREE_CODE (arg1) == PLUS_EXPR
9548 && TREE_CODE (arg0) != MULT_EXPR)
9550 tree tree10 = TREE_OPERAND (arg1, 0);
9551 tree tree11 = TREE_OPERAND (arg1, 1);
9552 if (TREE_CODE (tree11) == MULT_EXPR
9553 && TREE_CODE (tree10) == MULT_EXPR)
9555 tree tree0;
9556 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9557 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9560 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9561 We associate floats only if the user has specified
9562 -fassociative-math. */
9563 if (flag_associative_math
9564 && TREE_CODE (arg0) == PLUS_EXPR
9565 && TREE_CODE (arg1) != MULT_EXPR)
9567 tree tree00 = TREE_OPERAND (arg0, 0);
9568 tree tree01 = TREE_OPERAND (arg0, 1);
9569 if (TREE_CODE (tree01) == MULT_EXPR
9570 && TREE_CODE (tree00) == MULT_EXPR)
9572 tree tree0;
9573 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9574 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9579 bit_rotate:
9580 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9581 is a rotate of A by C1 bits. */
9582 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9583 is a rotate of A by B bits.
9584 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
9585 though in this case CODE must be | and not + or ^, otherwise
9586 it doesn't return A when B is 0. */
9588 enum tree_code code0, code1;
9589 tree rtype;
9590 code0 = TREE_CODE (arg0);
9591 code1 = TREE_CODE (arg1);
9592 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9593 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9594 && operand_equal_p (TREE_OPERAND (arg0, 0),
9595 TREE_OPERAND (arg1, 0), 0)
9596 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9597 TYPE_UNSIGNED (rtype))
9598 /* Only create rotates in complete modes. Other cases are not
9599 expanded properly. */
9600 && (element_precision (rtype)
9601 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9603 tree tree01, tree11;
9604 tree orig_tree01, orig_tree11;
9605 enum tree_code code01, code11;
9607 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
9608 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
9609 STRIP_NOPS (tree01);
9610 STRIP_NOPS (tree11);
9611 code01 = TREE_CODE (tree01);
9612 code11 = TREE_CODE (tree11);
9613 if (code11 != MINUS_EXPR
9614 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
9616 std::swap (code0, code1);
9617 std::swap (code01, code11);
9618 std::swap (tree01, tree11);
9619 std::swap (orig_tree01, orig_tree11);
9621 if (code01 == INTEGER_CST
9622 && code11 == INTEGER_CST
9623 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9624 == element_precision (rtype)))
9626 tem = build2_loc (loc, LROTATE_EXPR,
9627 rtype, TREE_OPERAND (arg0, 0),
9628 code0 == LSHIFT_EXPR
9629 ? orig_tree01 : orig_tree11);
9630 return fold_convert_loc (loc, type, tem);
9632 else if (code11 == MINUS_EXPR)
9634 tree tree110, tree111;
9635 tree110 = TREE_OPERAND (tree11, 0);
9636 tree111 = TREE_OPERAND (tree11, 1);
9637 STRIP_NOPS (tree110);
9638 STRIP_NOPS (tree111);
9639 if (TREE_CODE (tree110) == INTEGER_CST
9640 && compare_tree_int (tree110,
9641 element_precision (rtype)) == 0
9642 && operand_equal_p (tree01, tree111, 0))
9644 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9645 ? LROTATE_EXPR : RROTATE_EXPR),
9646 rtype, TREE_OPERAND (arg0, 0),
9647 orig_tree01);
9648 return fold_convert_loc (loc, type, tem);
9651 else if (code == BIT_IOR_EXPR
9652 && code11 == BIT_AND_EXPR
9653 && pow2p_hwi (element_precision (rtype)))
9655 tree tree110, tree111;
9656 tree110 = TREE_OPERAND (tree11, 0);
9657 tree111 = TREE_OPERAND (tree11, 1);
9658 STRIP_NOPS (tree110);
9659 STRIP_NOPS (tree111);
9660 if (TREE_CODE (tree110) == NEGATE_EXPR
9661 && TREE_CODE (tree111) == INTEGER_CST
9662 && compare_tree_int (tree111,
9663 element_precision (rtype) - 1) == 0
9664 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
9666 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9667 ? LROTATE_EXPR : RROTATE_EXPR),
9668 rtype, TREE_OPERAND (arg0, 0),
9669 orig_tree01);
9670 return fold_convert_loc (loc, type, tem);
9676 associate:
9677 /* In most languages, can't associate operations on floats through
9678 parentheses. Rather than remember where the parentheses were, we
9679 don't associate floats at all, unless the user has specified
9680 -fassociative-math.
9681 And, we need to make sure type is not saturating. */
9683 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9684 && !TYPE_SATURATING (type))
9686 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9687 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9688 tree atype = type;
9689 bool ok = true;
9691 /* Split both trees into variables, constants, and literals. Then
9692 associate each group together, the constants with literals,
9693 then the result with variables. This increases the chances of
9694 literals being recombined later and of generating relocatable
9695 expressions for the sum of a constant and literal. */
9696 var0 = split_tree (arg0, type, code,
9697 &minus_var0, &con0, &minus_con0,
9698 &lit0, &minus_lit0, 0);
9699 var1 = split_tree (arg1, type, code,
9700 &minus_var1, &con1, &minus_con1,
9701 &lit1, &minus_lit1, code == MINUS_EXPR);
9703 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9704 if (code == MINUS_EXPR)
9705 code = PLUS_EXPR;
9707 /* With undefined overflow prefer doing association in a type
9708 which wraps on overflow, if that is one of the operand types. */
9709 if (POINTER_TYPE_P (type)
9710 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9712 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9713 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9714 atype = TREE_TYPE (arg0);
9715 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9716 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9717 atype = TREE_TYPE (arg1);
9718 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9721 /* With undefined overflow we can only associate constants with one
9722 variable, and constants whose association doesn't overflow. */
9723 if (POINTER_TYPE_P (atype)
9724 || (INTEGRAL_TYPE_P (atype) && !TYPE_OVERFLOW_WRAPS (atype)))
9726 if ((var0 && var1) || (minus_var0 && minus_var1))
9728 /* ??? If split_tree would handle NEGATE_EXPR we could
9729 simply reject these cases and the allowed cases would
9730 be the var0/minus_var1 ones. */
9731 tree tmp0 = var0 ? var0 : minus_var0;
9732 tree tmp1 = var1 ? var1 : minus_var1;
9733 bool one_neg = false;
9735 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9737 tmp0 = TREE_OPERAND (tmp0, 0);
9738 one_neg = !one_neg;
9740 if (CONVERT_EXPR_P (tmp0)
9741 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9742 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9743 <= TYPE_PRECISION (atype)))
9744 tmp0 = TREE_OPERAND (tmp0, 0);
9745 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9747 tmp1 = TREE_OPERAND (tmp1, 0);
9748 one_neg = !one_neg;
9750 if (CONVERT_EXPR_P (tmp1)
9751 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9752 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9753 <= TYPE_PRECISION (atype)))
9754 tmp1 = TREE_OPERAND (tmp1, 0);
9755 /* The only case we can still associate with two variables
9756 is if they cancel out. */
9757 if (!one_neg
9758 || !operand_equal_p (tmp0, tmp1, 0))
9759 ok = false;
9761 else if ((var0 && minus_var1
9762 && ! operand_equal_p (var0, minus_var1, 0))
9763 || (minus_var0 && var1
9764 && ! operand_equal_p (minus_var0, var1, 0)))
9765 ok = false;
9768 /* Only do something if we found more than two objects. Otherwise,
9769 nothing has changed and we risk infinite recursion. */
9770 if (ok
9771 && ((var0 != 0) + (var1 != 0)
9772 + (minus_var0 != 0) + (minus_var1 != 0)
9773 + (con0 != 0) + (con1 != 0)
9774 + (minus_con0 != 0) + (minus_con1 != 0)
9775 + (lit0 != 0) + (lit1 != 0)
9776 + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
9778 var0 = associate_trees (loc, var0, var1, code, atype);
9779 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9780 code, atype);
9781 con0 = associate_trees (loc, con0, con1, code, atype);
9782 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9783 code, atype);
9784 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9785 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9786 code, atype);
9788 if (minus_var0 && var0)
9790 var0 = associate_trees (loc, var0, minus_var0,
9791 MINUS_EXPR, atype);
9792 minus_var0 = 0;
9794 if (minus_con0 && con0)
9796 con0 = associate_trees (loc, con0, minus_con0,
9797 MINUS_EXPR, atype);
9798 minus_con0 = 0;
9801 /* Preserve the MINUS_EXPR if the negative part of the literal is
9802 greater than the positive part. Otherwise, the multiplicative
9803 folding code (i.e extract_muldiv) may be fooled in case
9804 unsigned constants are subtracted, like in the following
9805 example: ((X*2 + 4) - 8U)/2. */
9806 if (minus_lit0 && lit0)
9808 if (TREE_CODE (lit0) == INTEGER_CST
9809 && TREE_CODE (minus_lit0) == INTEGER_CST
9810 && tree_int_cst_lt (lit0, minus_lit0)
9811 /* But avoid ending up with only negated parts. */
9812 && (var0 || con0))
9814 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9815 MINUS_EXPR, atype);
9816 lit0 = 0;
9818 else
9820 lit0 = associate_trees (loc, lit0, minus_lit0,
9821 MINUS_EXPR, atype);
9822 minus_lit0 = 0;
9826 /* Don't introduce overflows through reassociation. */
9827 if ((lit0 && TREE_OVERFLOW_P (lit0))
9828 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9829 return NULL_TREE;
9831 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9832 con0 = associate_trees (loc, con0, lit0, code, atype);
9833 lit0 = 0;
9834 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9835 code, atype);
9836 minus_lit0 = 0;
9838 /* Eliminate minus_con0. */
9839 if (minus_con0)
9841 if (con0)
9842 con0 = associate_trees (loc, con0, minus_con0,
9843 MINUS_EXPR, atype);
9844 else if (var0)
9845 var0 = associate_trees (loc, var0, minus_con0,
9846 MINUS_EXPR, atype);
9847 else
9848 gcc_unreachable ();
9849 minus_con0 = 0;
9852 /* Eliminate minus_var0. */
9853 if (minus_var0)
9855 if (con0)
9856 con0 = associate_trees (loc, con0, minus_var0,
9857 MINUS_EXPR, atype);
9858 else
9859 gcc_unreachable ();
9860 minus_var0 = 0;
9863 return
9864 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9865 code, atype));
9869 return NULL_TREE;
9871 case POINTER_DIFF_EXPR:
9872 case MINUS_EXPR:
9873 /* Fold &a[i] - &a[j] to i-j. */
9874 if (TREE_CODE (arg0) == ADDR_EXPR
9875 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9876 && TREE_CODE (arg1) == ADDR_EXPR
9877 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9879 tree tem = fold_addr_of_array_ref_difference (loc, type,
9880 TREE_OPERAND (arg0, 0),
9881 TREE_OPERAND (arg1, 0),
9882 code
9883 == POINTER_DIFF_EXPR);
9884 if (tem)
9885 return tem;
9888 /* Further transformations are not for pointers. */
9889 if (code == POINTER_DIFF_EXPR)
9890 return NULL_TREE;
9892 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9893 if (TREE_CODE (arg0) == NEGATE_EXPR
9894 && negate_expr_p (op1)
9895 /* If arg0 is e.g. unsigned int and type is int, then this could
9896 introduce UB, because if A is INT_MIN at runtime, the original
9897 expression can be well defined while the latter is not.
9898 See PR83269. */
9899 && !(ANY_INTEGRAL_TYPE_P (type)
9900 && TYPE_OVERFLOW_UNDEFINED (type)
9901 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9902 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
9903 return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
9904 fold_convert_loc (loc, type,
9905 TREE_OPERAND (arg0, 0)));
9907 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9908 __complex__ ( x, -y ). This is not the same for SNaNs or if
9909 signed zeros are involved. */
9910 if (!HONOR_SNANS (element_mode (arg0))
9911 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9912 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9914 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9915 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9916 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9917 bool arg0rz = false, arg0iz = false;
9918 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9919 || (arg0i && (arg0iz = real_zerop (arg0i))))
9921 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9922 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9923 if (arg0rz && arg1i && real_zerop (arg1i))
9925 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9926 arg1r ? arg1r
9927 : build1 (REALPART_EXPR, rtype, arg1));
9928 tree ip = arg0i ? arg0i
9929 : build1 (IMAGPART_EXPR, rtype, arg0);
9930 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9932 else if (arg0iz && arg1r && real_zerop (arg1r))
9934 tree rp = arg0r ? arg0r
9935 : build1 (REALPART_EXPR, rtype, arg0);
9936 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9937 arg1i ? arg1i
9938 : build1 (IMAGPART_EXPR, rtype, arg1));
9939 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9944 /* A - B -> A + (-B) if B is easily negatable. */
9945 if (negate_expr_p (op1)
9946 && ! TYPE_OVERFLOW_SANITIZED (type)
9947 && ((FLOAT_TYPE_P (type)
9948 /* Avoid this transformation if B is a positive REAL_CST. */
9949 && (TREE_CODE (op1) != REAL_CST
9950 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9951 || INTEGRAL_TYPE_P (type)))
9952 return fold_build2_loc (loc, PLUS_EXPR, type,
9953 fold_convert_loc (loc, type, arg0),
9954 negate_expr (op1));
9956 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
9957 one. Make sure the type is not saturating and has the signedness of
9958 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9959 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9960 if ((TREE_CODE (arg0) == MULT_EXPR
9961 || TREE_CODE (arg1) == MULT_EXPR)
9962 && !TYPE_SATURATING (type)
9963 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9964 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9965 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9967 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9968 if (tem)
9969 return tem;
9972 goto associate;
9974 case MULT_EXPR:
9975 if (! FLOAT_TYPE_P (type))
9977 /* Transform x * -C into -x * C if x is easily negatable. */
9978 if (TREE_CODE (op1) == INTEGER_CST
9979 && tree_int_cst_sgn (op1) == -1
9980 && negate_expr_p (op0)
9981 && negate_expr_p (op1)
9982 && (tem = negate_expr (op1)) != op1
9983 && ! TREE_OVERFLOW (tem))
9984 return fold_build2_loc (loc, MULT_EXPR, type,
9985 fold_convert_loc (loc, type,
9986 negate_expr (op0)), tem);
9988 strict_overflow_p = false;
9989 if (TREE_CODE (arg1) == INTEGER_CST
9990 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
9991 &strict_overflow_p)) != 0)
9993 if (strict_overflow_p)
9994 fold_overflow_warning (("assuming signed overflow does not "
9995 "occur when simplifying "
9996 "multiplication"),
9997 WARN_STRICT_OVERFLOW_MISC);
9998 return fold_convert_loc (loc, type, tem);
10001 /* Optimize z * conj(z) for integer complex numbers. */
10002 if (TREE_CODE (arg0) == CONJ_EXPR
10003 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10004 return fold_mult_zconjz (loc, type, arg1);
10005 if (TREE_CODE (arg1) == CONJ_EXPR
10006 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10007 return fold_mult_zconjz (loc, type, arg0);
10009 else
10011 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10012 This is not the same for NaNs or if signed zeros are
10013 involved. */
10014 if (!HONOR_NANS (arg0)
10015 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10016 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10017 && TREE_CODE (arg1) == COMPLEX_CST
10018 && real_zerop (TREE_REALPART (arg1)))
10020 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10021 if (real_onep (TREE_IMAGPART (arg1)))
10022 return
10023 fold_build2_loc (loc, COMPLEX_EXPR, type,
10024 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10025 rtype, arg0)),
10026 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10027 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10028 return
10029 fold_build2_loc (loc, COMPLEX_EXPR, type,
10030 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10031 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10032 rtype, arg0)));
10035 /* Optimize z * conj(z) for floating point complex numbers.
10036 Guarded by flag_unsafe_math_optimizations as non-finite
10037 imaginary components don't produce scalar results. */
10038 if (flag_unsafe_math_optimizations
10039 && TREE_CODE (arg0) == CONJ_EXPR
10040 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10041 return fold_mult_zconjz (loc, type, arg1);
10042 if (flag_unsafe_math_optimizations
10043 && TREE_CODE (arg1) == CONJ_EXPR
10044 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10045 return fold_mult_zconjz (loc, type, arg0);
10047 goto associate;
10049 case BIT_IOR_EXPR:
10050 /* Canonicalize (X & C1) | C2. */
10051 if (TREE_CODE (arg0) == BIT_AND_EXPR
10052 && TREE_CODE (arg1) == INTEGER_CST
10053 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10055 int width = TYPE_PRECISION (type), w;
10056 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
10057 wide_int c2 = wi::to_wide (arg1);
10059 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10060 if ((c1 & c2) == c1)
10061 return omit_one_operand_loc (loc, type, arg1,
10062 TREE_OPERAND (arg0, 0));
10064 wide_int msk = wi::mask (width, false,
10065 TYPE_PRECISION (TREE_TYPE (arg1)));
10067 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10068 if (wi::bit_and_not (msk, c1 | c2) == 0)
10070 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10071 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10074 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10075 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10076 mode which allows further optimizations. */
10077 c1 &= msk;
10078 c2 &= msk;
10079 wide_int c3 = wi::bit_and_not (c1, c2);
10080 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10082 wide_int mask = wi::mask (w, false,
10083 TYPE_PRECISION (type));
10084 if (((c1 | c2) & mask) == mask
10085 && wi::bit_and_not (c1, mask) == 0)
10087 c3 = mask;
10088 break;
10092 if (c3 != c1)
10094 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10095 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
10096 wide_int_to_tree (type, c3));
10097 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10101 /* See if this can be simplified into a rotate first. If that
10102 is unsuccessful continue in the association code. */
10103 goto bit_rotate;
10105 case BIT_XOR_EXPR:
10106 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10107 if (TREE_CODE (arg0) == BIT_AND_EXPR
10108 && INTEGRAL_TYPE_P (type)
10109 && integer_onep (TREE_OPERAND (arg0, 1))
10110 && integer_onep (arg1))
10111 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10112 build_zero_cst (TREE_TYPE (arg0)));
10114 /* See if this can be simplified into a rotate first. If that
10115 is unsuccessful continue in the association code. */
10116 goto bit_rotate;
10118 case BIT_AND_EXPR:
10119 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10120 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10121 && INTEGRAL_TYPE_P (type)
10122 && integer_onep (TREE_OPERAND (arg0, 1))
10123 && integer_onep (arg1))
10125 tree tem2;
10126 tem = TREE_OPERAND (arg0, 0);
10127 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10128 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10129 tem, tem2);
10130 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10131 build_zero_cst (TREE_TYPE (tem)));
10133 /* Fold ~X & 1 as (X & 1) == 0. */
10134 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10135 && INTEGRAL_TYPE_P (type)
10136 && integer_onep (arg1))
10138 tree tem2;
10139 tem = TREE_OPERAND (arg0, 0);
10140 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10141 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10142 tem, tem2);
10143 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10144 build_zero_cst (TREE_TYPE (tem)));
10146 /* Fold !X & 1 as X == 0. */
10147 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10148 && integer_onep (arg1))
10150 tem = TREE_OPERAND (arg0, 0);
10151 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10152 build_zero_cst (TREE_TYPE (tem)));
10155 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10156 multiple of 1 << CST. */
10157 if (TREE_CODE (arg1) == INTEGER_CST)
10159 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10160 wide_int ncst1 = -cst1;
10161 if ((cst1 & ncst1) == ncst1
10162 && multiple_of_p (type, arg0,
10163 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10164 return fold_convert_loc (loc, type, arg0);
10167 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10168 bits from CST2. */
10169 if (TREE_CODE (arg1) == INTEGER_CST
10170 && TREE_CODE (arg0) == MULT_EXPR
10171 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10173 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
10174 wide_int masked
10175 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
10177 if (masked == 0)
10178 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10179 arg0, arg1);
10180 else if (masked != warg1)
10182 /* Avoid the transform if arg1 is a mask of some
10183 mode which allows further optimizations. */
10184 int pop = wi::popcount (warg1);
10185 if (!(pop >= BITS_PER_UNIT
10186 && pow2p_hwi (pop)
10187 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10188 return fold_build2_loc (loc, code, type, op0,
10189 wide_int_to_tree (type, masked));
10193 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10194 ((A & N) + B) & M -> (A + B) & M
10195 Similarly if (N & M) == 0,
10196 ((A | N) + B) & M -> (A + B) & M
10197 and for - instead of + (or unary - instead of +)
10198 and/or ^ instead of |.
10199 If B is constant and (B & M) == 0, fold into A & M. */
10200 if (TREE_CODE (arg1) == INTEGER_CST)
10202 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10203 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10204 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10205 && (TREE_CODE (arg0) == PLUS_EXPR
10206 || TREE_CODE (arg0) == MINUS_EXPR
10207 || TREE_CODE (arg0) == NEGATE_EXPR)
10208 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10209 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10211 tree pmop[2];
10212 int which = 0;
10213 wide_int cst0;
10215 /* Now we know that arg0 is (C + D) or (C - D) or
10216 -C and arg1 (M) is == (1LL << cst) - 1.
10217 Store C into PMOP[0] and D into PMOP[1]. */
10218 pmop[0] = TREE_OPERAND (arg0, 0);
10219 pmop[1] = NULL;
10220 if (TREE_CODE (arg0) != NEGATE_EXPR)
10222 pmop[1] = TREE_OPERAND (arg0, 1);
10223 which = 1;
10226 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10227 which = -1;
10229 for (; which >= 0; which--)
10230 switch (TREE_CODE (pmop[which]))
10232 case BIT_AND_EXPR:
10233 case BIT_IOR_EXPR:
10234 case BIT_XOR_EXPR:
10235 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10236 != INTEGER_CST)
10237 break;
10238 cst0 = wi::to_wide (TREE_OPERAND (pmop[which], 1)) & cst1;
10239 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10241 if (cst0 != cst1)
10242 break;
10244 else if (cst0 != 0)
10245 break;
10246 /* If C or D is of the form (A & N) where
10247 (N & M) == M, or of the form (A | N) or
10248 (A ^ N) where (N & M) == 0, replace it with A. */
10249 pmop[which] = TREE_OPERAND (pmop[which], 0);
10250 break;
10251 case INTEGER_CST:
10252 /* If C or D is a N where (N & M) == 0, it can be
10253 omitted (assumed 0). */
10254 if ((TREE_CODE (arg0) == PLUS_EXPR
10255 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10256 && (cst1 & wi::to_wide (pmop[which])) == 0)
10257 pmop[which] = NULL;
10258 break;
10259 default:
10260 break;
10263 /* Only build anything new if we optimized one or both arguments
10264 above. */
10265 if (pmop[0] != TREE_OPERAND (arg0, 0)
10266 || (TREE_CODE (arg0) != NEGATE_EXPR
10267 && pmop[1] != TREE_OPERAND (arg0, 1)))
10269 tree utype = TREE_TYPE (arg0);
10270 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10272 /* Perform the operations in a type that has defined
10273 overflow behavior. */
10274 utype = unsigned_type_for (TREE_TYPE (arg0));
10275 if (pmop[0] != NULL)
10276 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10277 if (pmop[1] != NULL)
10278 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10281 if (TREE_CODE (arg0) == NEGATE_EXPR)
10282 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10283 else if (TREE_CODE (arg0) == PLUS_EXPR)
10285 if (pmop[0] != NULL && pmop[1] != NULL)
10286 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10287 pmop[0], pmop[1]);
10288 else if (pmop[0] != NULL)
10289 tem = pmop[0];
10290 else if (pmop[1] != NULL)
10291 tem = pmop[1];
10292 else
10293 return build_int_cst (type, 0);
10295 else if (pmop[0] == NULL)
10296 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10297 else
10298 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10299 pmop[0], pmop[1]);
10300 /* TEM is now the new binary +, - or unary - replacement. */
10301 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10302 fold_convert_loc (loc, utype, arg1));
10303 return fold_convert_loc (loc, type, tem);
10308 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10309 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10310 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10312 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10314 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
10315 if (mask == -1)
10316 return
10317 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10320 goto associate;
10322 case RDIV_EXPR:
10323 /* Don't touch a floating-point divide by zero unless the mode
10324 of the constant can represent infinity. */
10325 if (TREE_CODE (arg1) == REAL_CST
10326 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10327 && real_zerop (arg1))
10328 return NULL_TREE;
10330 /* (-A) / (-B) -> A / B */
10331 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10332 return fold_build2_loc (loc, RDIV_EXPR, type,
10333 TREE_OPERAND (arg0, 0),
10334 negate_expr (arg1));
10335 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10336 return fold_build2_loc (loc, RDIV_EXPR, type,
10337 negate_expr (arg0),
10338 TREE_OPERAND (arg1, 0));
10339 return NULL_TREE;
10341 case TRUNC_DIV_EXPR:
10342 /* Fall through */
10344 case FLOOR_DIV_EXPR:
10345 /* Simplify A / (B << N) where A and B are positive and B is
10346 a power of 2, to A >> (N + log2(B)). */
10347 strict_overflow_p = false;
10348 if (TREE_CODE (arg1) == LSHIFT_EXPR
10349 && (TYPE_UNSIGNED (type)
10350 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10352 tree sval = TREE_OPERAND (arg1, 0);
10353 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10355 tree sh_cnt = TREE_OPERAND (arg1, 1);
10356 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10357 wi::exact_log2 (wi::to_wide (sval)));
10359 if (strict_overflow_p)
10360 fold_overflow_warning (("assuming signed overflow does not "
10361 "occur when simplifying A / (B << N)"),
10362 WARN_STRICT_OVERFLOW_MISC);
10364 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10365 sh_cnt, pow2);
10366 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10367 fold_convert_loc (loc, type, arg0), sh_cnt);
10371 /* Fall through */
10373 case ROUND_DIV_EXPR:
10374 case CEIL_DIV_EXPR:
10375 case EXACT_DIV_EXPR:
10376 if (integer_zerop (arg1))
10377 return NULL_TREE;
10379 /* Convert -A / -B to A / B when the type is signed and overflow is
10380 undefined. */
10381 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10382 && TREE_CODE (op0) == NEGATE_EXPR
10383 && negate_expr_p (op1))
10385 if (INTEGRAL_TYPE_P (type))
10386 fold_overflow_warning (("assuming signed overflow does not occur "
10387 "when distributing negation across "
10388 "division"),
10389 WARN_STRICT_OVERFLOW_MISC);
10390 return fold_build2_loc (loc, code, type,
10391 fold_convert_loc (loc, type,
10392 TREE_OPERAND (arg0, 0)),
10393 negate_expr (op1));
10395 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10396 && TREE_CODE (arg1) == NEGATE_EXPR
10397 && negate_expr_p (op0))
10399 if (INTEGRAL_TYPE_P (type))
10400 fold_overflow_warning (("assuming signed overflow does not occur "
10401 "when distributing negation across "
10402 "division"),
10403 WARN_STRICT_OVERFLOW_MISC);
10404 return fold_build2_loc (loc, code, type,
10405 negate_expr (op0),
10406 fold_convert_loc (loc, type,
10407 TREE_OPERAND (arg1, 0)));
10410 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10411 operation, EXACT_DIV_EXPR.
10413 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10414 At one time others generated faster code, it's not clear if they do
10415 after the last round to changes to the DIV code in expmed.c. */
10416 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10417 && multiple_of_p (type, arg0, arg1))
10418 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10419 fold_convert (type, arg0),
10420 fold_convert (type, arg1));
10422 strict_overflow_p = false;
10423 if (TREE_CODE (arg1) == INTEGER_CST
10424 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10425 &strict_overflow_p)) != 0)
10427 if (strict_overflow_p)
10428 fold_overflow_warning (("assuming signed overflow does not occur "
10429 "when simplifying division"),
10430 WARN_STRICT_OVERFLOW_MISC);
10431 return fold_convert_loc (loc, type, tem);
10434 return NULL_TREE;
10436 case CEIL_MOD_EXPR:
10437 case FLOOR_MOD_EXPR:
10438 case ROUND_MOD_EXPR:
10439 case TRUNC_MOD_EXPR:
10440 strict_overflow_p = false;
10441 if (TREE_CODE (arg1) == INTEGER_CST
10442 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10443 &strict_overflow_p)) != 0)
10445 if (strict_overflow_p)
10446 fold_overflow_warning (("assuming signed overflow does not occur "
10447 "when simplifying modulus"),
10448 WARN_STRICT_OVERFLOW_MISC);
10449 return fold_convert_loc (loc, type, tem);
10452 return NULL_TREE;
10454 case LROTATE_EXPR:
10455 case RROTATE_EXPR:
10456 case RSHIFT_EXPR:
10457 case LSHIFT_EXPR:
10458 /* Since negative shift count is not well-defined,
10459 don't try to compute it in the compiler. */
10460 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10461 return NULL_TREE;
10463 prec = element_precision (type);
10465 /* If we have a rotate of a bit operation with the rotate count and
10466 the second operand of the bit operation both constant,
10467 permute the two operations. */
10468 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10469 && (TREE_CODE (arg0) == BIT_AND_EXPR
10470 || TREE_CODE (arg0) == BIT_IOR_EXPR
10471 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10472 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10474 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10475 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10476 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10477 fold_build2_loc (loc, code, type,
10478 arg00, arg1),
10479 fold_build2_loc (loc, code, type,
10480 arg01, arg1));
10483 /* Two consecutive rotates adding up to the some integer
10484 multiple of the precision of the type can be ignored. */
10485 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10486 && TREE_CODE (arg0) == RROTATE_EXPR
10487 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10488 && wi::umod_trunc (wi::to_wide (arg1)
10489 + wi::to_wide (TREE_OPERAND (arg0, 1)),
10490 prec) == 0)
10491 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10493 return NULL_TREE;
10495 case MIN_EXPR:
10496 case MAX_EXPR:
10497 goto associate;
10499 case TRUTH_ANDIF_EXPR:
10500 /* Note that the operands of this must be ints
10501 and their values must be 0 or 1.
10502 ("true" is a fixed value perhaps depending on the language.) */
10503 /* If first arg is constant zero, return it. */
10504 if (integer_zerop (arg0))
10505 return fold_convert_loc (loc, type, arg0);
10506 /* FALLTHRU */
10507 case TRUTH_AND_EXPR:
10508 /* If either arg is constant true, drop it. */
10509 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10510 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10511 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10512 /* Preserve sequence points. */
10513 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10514 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10515 /* If second arg is constant zero, result is zero, but first arg
10516 must be evaluated. */
10517 if (integer_zerop (arg1))
10518 return omit_one_operand_loc (loc, type, arg1, arg0);
10519 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10520 case will be handled here. */
10521 if (integer_zerop (arg0))
10522 return omit_one_operand_loc (loc, type, arg0, arg1);
10524 /* !X && X is always false. */
10525 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10526 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10527 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10528 /* X && !X is always false. */
10529 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10530 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10531 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10533 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10534 means A >= Y && A != MAX, but in this case we know that
10535 A < X <= MAX. */
10537 if (!TREE_SIDE_EFFECTS (arg0)
10538 && !TREE_SIDE_EFFECTS (arg1))
10540 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10541 if (tem && !operand_equal_p (tem, arg0, 0))
10542 return fold_build2_loc (loc, code, type, tem, arg1);
10544 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10545 if (tem && !operand_equal_p (tem, arg1, 0))
10546 return fold_build2_loc (loc, code, type, arg0, tem);
10549 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10550 != NULL_TREE)
10551 return tem;
10553 return NULL_TREE;
10555 case TRUTH_ORIF_EXPR:
10556 /* Note that the operands of this must be ints
10557 and their values must be 0 or true.
10558 ("true" is a fixed value perhaps depending on the language.) */
10559 /* If first arg is constant true, return it. */
10560 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10561 return fold_convert_loc (loc, type, arg0);
10562 /* FALLTHRU */
10563 case TRUTH_OR_EXPR:
10564 /* If either arg is constant zero, drop it. */
10565 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10566 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10567 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10568 /* Preserve sequence points. */
10569 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10570 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10571 /* If second arg is constant true, result is true, but we must
10572 evaluate first arg. */
10573 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10574 return omit_one_operand_loc (loc, type, arg1, arg0);
10575 /* Likewise for first arg, but note this only occurs here for
10576 TRUTH_OR_EXPR. */
10577 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10578 return omit_one_operand_loc (loc, type, arg0, arg1);
10580 /* !X || X is always true. */
10581 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10582 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10583 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10584 /* X || !X is always true. */
10585 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10586 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10587 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10589 /* (X && !Y) || (!X && Y) is X ^ Y */
10590 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10591 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10593 tree a0, a1, l0, l1, n0, n1;
10595 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10596 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10598 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10599 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10601 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10602 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10604 if ((operand_equal_p (n0, a0, 0)
10605 && operand_equal_p (n1, a1, 0))
10606 || (operand_equal_p (n0, a1, 0)
10607 && operand_equal_p (n1, a0, 0)))
10608 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10611 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10612 != NULL_TREE)
10613 return tem;
10615 return NULL_TREE;
10617 case TRUTH_XOR_EXPR:
10618 /* If the second arg is constant zero, drop it. */
10619 if (integer_zerop (arg1))
10620 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10621 /* If the second arg is constant true, this is a logical inversion. */
10622 if (integer_onep (arg1))
10624 tem = invert_truthvalue_loc (loc, arg0);
10625 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10627 /* Identical arguments cancel to zero. */
10628 if (operand_equal_p (arg0, arg1, 0))
10629 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10631 /* !X ^ X is always true. */
10632 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10633 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10634 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10636 /* X ^ !X is always true. */
10637 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10638 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10639 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10641 return NULL_TREE;
10643 case EQ_EXPR:
10644 case NE_EXPR:
10645 STRIP_NOPS (arg0);
10646 STRIP_NOPS (arg1);
10648 tem = fold_comparison (loc, code, type, op0, op1);
10649 if (tem != NULL_TREE)
10650 return tem;
10652 /* bool_var != 1 becomes !bool_var. */
10653 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10654 && code == NE_EXPR)
10655 return fold_convert_loc (loc, type,
10656 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10657 TREE_TYPE (arg0), arg0));
10659 /* bool_var == 0 becomes !bool_var. */
10660 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10661 && code == EQ_EXPR)
10662 return fold_convert_loc (loc, type,
10663 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10664 TREE_TYPE (arg0), arg0));
10666 /* !exp != 0 becomes !exp */
10667 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10668 && code == NE_EXPR)
10669 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10671 /* If this is an EQ or NE comparison with zero and ARG0 is
10672 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10673 two operations, but the latter can be done in one less insn
10674 on machines that have only two-operand insns or on which a
10675 constant cannot be the first operand. */
10676 if (TREE_CODE (arg0) == BIT_AND_EXPR
10677 && integer_zerop (arg1))
10679 tree arg00 = TREE_OPERAND (arg0, 0);
10680 tree arg01 = TREE_OPERAND (arg0, 1);
10681 if (TREE_CODE (arg00) == LSHIFT_EXPR
10682 && integer_onep (TREE_OPERAND (arg00, 0)))
10684 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10685 arg01, TREE_OPERAND (arg00, 1));
10686 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10687 build_int_cst (TREE_TYPE (arg0), 1));
10688 return fold_build2_loc (loc, code, type,
10689 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10690 arg1);
10692 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10693 && integer_onep (TREE_OPERAND (arg01, 0)))
10695 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10696 arg00, TREE_OPERAND (arg01, 1));
10697 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10698 build_int_cst (TREE_TYPE (arg0), 1));
10699 return fold_build2_loc (loc, code, type,
10700 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10701 arg1);
10705 /* If this is an NE or EQ comparison of zero against the result of a
10706 signed MOD operation whose second operand is a power of 2, make
10707 the MOD operation unsigned since it is simpler and equivalent. */
10708 if (integer_zerop (arg1)
10709 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10710 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10711 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10712 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10713 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10714 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10716 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10717 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10718 fold_convert_loc (loc, newtype,
10719 TREE_OPERAND (arg0, 0)),
10720 fold_convert_loc (loc, newtype,
10721 TREE_OPERAND (arg0, 1)));
10723 return fold_build2_loc (loc, code, type, newmod,
10724 fold_convert_loc (loc, newtype, arg1));
10727 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10728 C1 is a valid shift constant, and C2 is a power of two, i.e.
10729 a single bit. */
10730 if (TREE_CODE (arg0) == BIT_AND_EXPR
10731 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10732 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10733 == INTEGER_CST
10734 && integer_pow2p (TREE_OPERAND (arg0, 1))
10735 && integer_zerop (arg1))
10737 tree itype = TREE_TYPE (arg0);
10738 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10739 prec = TYPE_PRECISION (itype);
10741 /* Check for a valid shift count. */
10742 if (wi::ltu_p (wi::to_wide (arg001), prec))
10744 tree arg01 = TREE_OPERAND (arg0, 1);
10745 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10746 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10747 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10748 can be rewritten as (X & (C2 << C1)) != 0. */
10749 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10751 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10752 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10753 return fold_build2_loc (loc, code, type, tem,
10754 fold_convert_loc (loc, itype, arg1));
10756 /* Otherwise, for signed (arithmetic) shifts,
10757 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10758 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10759 else if (!TYPE_UNSIGNED (itype))
10760 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10761 arg000, build_int_cst (itype, 0));
10762 /* Otherwise, of unsigned (logical) shifts,
10763 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10764 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10765 else
10766 return omit_one_operand_loc (loc, type,
10767 code == EQ_EXPR ? integer_one_node
10768 : integer_zero_node,
10769 arg000);
10773 /* If this is a comparison of a field, we may be able to simplify it. */
10774 if ((TREE_CODE (arg0) == COMPONENT_REF
10775 || TREE_CODE (arg0) == BIT_FIELD_REF)
10776 /* Handle the constant case even without -O
10777 to make sure the warnings are given. */
10778 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10780 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10781 if (t1)
10782 return t1;
10785 /* Optimize comparisons of strlen vs zero to a compare of the
10786 first character of the string vs zero. To wit,
10787 strlen(ptr) == 0 => *ptr == 0
10788 strlen(ptr) != 0 => *ptr != 0
10789 Other cases should reduce to one of these two (or a constant)
10790 due to the return value of strlen being unsigned. */
10791 if (TREE_CODE (arg0) == CALL_EXPR
10792 && integer_zerop (arg1))
10794 tree fndecl = get_callee_fndecl (arg0);
10796 if (fndecl
10797 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10798 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10799 && call_expr_nargs (arg0) == 1
10800 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10802 tree iref = build_fold_indirect_ref_loc (loc,
10803 CALL_EXPR_ARG (arg0, 0));
10804 return fold_build2_loc (loc, code, type, iref,
10805 build_int_cst (TREE_TYPE (iref), 0));
10809 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10810 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10811 if (TREE_CODE (arg0) == RSHIFT_EXPR
10812 && integer_zerop (arg1)
10813 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10815 tree arg00 = TREE_OPERAND (arg0, 0);
10816 tree arg01 = TREE_OPERAND (arg0, 1);
10817 tree itype = TREE_TYPE (arg00);
10818 if (wi::to_wide (arg01) == element_precision (itype) - 1)
10820 if (TYPE_UNSIGNED (itype))
10822 itype = signed_type_for (itype);
10823 arg00 = fold_convert_loc (loc, itype, arg00);
10825 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10826 type, arg00, build_zero_cst (itype));
10830 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10831 (X & C) == 0 when C is a single bit. */
10832 if (TREE_CODE (arg0) == BIT_AND_EXPR
10833 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10834 && integer_zerop (arg1)
10835 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10837 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10838 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10839 TREE_OPERAND (arg0, 1));
10840 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10841 type, tem,
10842 fold_convert_loc (loc, TREE_TYPE (arg0),
10843 arg1));
10846 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10847 constant C is a power of two, i.e. a single bit. */
10848 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10849 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10850 && integer_zerop (arg1)
10851 && integer_pow2p (TREE_OPERAND (arg0, 1))
10852 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10853 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10855 tree arg00 = TREE_OPERAND (arg0, 0);
10856 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10857 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10860 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10861 when is C is a power of two, i.e. a single bit. */
10862 if (TREE_CODE (arg0) == BIT_AND_EXPR
10863 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10864 && integer_zerop (arg1)
10865 && integer_pow2p (TREE_OPERAND (arg0, 1))
10866 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10867 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10869 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10870 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10871 arg000, TREE_OPERAND (arg0, 1));
10872 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10873 tem, build_int_cst (TREE_TYPE (tem), 0));
10876 if (integer_zerop (arg1)
10877 && tree_expr_nonzero_p (arg0))
10879 tree res = constant_boolean_node (code==NE_EXPR, type);
10880 return omit_one_operand_loc (loc, type, res, arg0);
10883 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10884 if (TREE_CODE (arg0) == BIT_AND_EXPR
10885 && TREE_CODE (arg1) == BIT_AND_EXPR)
10887 tree arg00 = TREE_OPERAND (arg0, 0);
10888 tree arg01 = TREE_OPERAND (arg0, 1);
10889 tree arg10 = TREE_OPERAND (arg1, 0);
10890 tree arg11 = TREE_OPERAND (arg1, 1);
10891 tree itype = TREE_TYPE (arg0);
10893 if (operand_equal_p (arg01, arg11, 0))
10895 tem = fold_convert_loc (loc, itype, arg10);
10896 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10897 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10898 return fold_build2_loc (loc, code, type, tem,
10899 build_zero_cst (itype));
10901 if (operand_equal_p (arg01, arg10, 0))
10903 tem = fold_convert_loc (loc, itype, arg11);
10904 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10905 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10906 return fold_build2_loc (loc, code, type, tem,
10907 build_zero_cst (itype));
10909 if (operand_equal_p (arg00, arg11, 0))
10911 tem = fold_convert_loc (loc, itype, arg10);
10912 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10913 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10914 return fold_build2_loc (loc, code, type, tem,
10915 build_zero_cst (itype));
10917 if (operand_equal_p (arg00, arg10, 0))
10919 tem = fold_convert_loc (loc, itype, arg11);
10920 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10921 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10922 return fold_build2_loc (loc, code, type, tem,
10923 build_zero_cst (itype));
10927 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10928 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10930 tree arg00 = TREE_OPERAND (arg0, 0);
10931 tree arg01 = TREE_OPERAND (arg0, 1);
10932 tree arg10 = TREE_OPERAND (arg1, 0);
10933 tree arg11 = TREE_OPERAND (arg1, 1);
10934 tree itype = TREE_TYPE (arg0);
10936 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10937 operand_equal_p guarantees no side-effects so we don't need
10938 to use omit_one_operand on Z. */
10939 if (operand_equal_p (arg01, arg11, 0))
10940 return fold_build2_loc (loc, code, type, arg00,
10941 fold_convert_loc (loc, TREE_TYPE (arg00),
10942 arg10));
10943 if (operand_equal_p (arg01, arg10, 0))
10944 return fold_build2_loc (loc, code, type, arg00,
10945 fold_convert_loc (loc, TREE_TYPE (arg00),
10946 arg11));
10947 if (operand_equal_p (arg00, arg11, 0))
10948 return fold_build2_loc (loc, code, type, arg01,
10949 fold_convert_loc (loc, TREE_TYPE (arg01),
10950 arg10));
10951 if (operand_equal_p (arg00, arg10, 0))
10952 return fold_build2_loc (loc, code, type, arg01,
10953 fold_convert_loc (loc, TREE_TYPE (arg01),
10954 arg11));
10956 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10957 if (TREE_CODE (arg01) == INTEGER_CST
10958 && TREE_CODE (arg11) == INTEGER_CST)
10960 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10961 fold_convert_loc (loc, itype, arg11));
10962 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10963 return fold_build2_loc (loc, code, type, tem,
10964 fold_convert_loc (loc, itype, arg10));
10968 /* Attempt to simplify equality/inequality comparisons of complex
10969 values. Only lower the comparison if the result is known or
10970 can be simplified to a single scalar comparison. */
10971 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10972 || TREE_CODE (arg0) == COMPLEX_CST)
10973 && (TREE_CODE (arg1) == COMPLEX_EXPR
10974 || TREE_CODE (arg1) == COMPLEX_CST))
10976 tree real0, imag0, real1, imag1;
10977 tree rcond, icond;
10979 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10981 real0 = TREE_OPERAND (arg0, 0);
10982 imag0 = TREE_OPERAND (arg0, 1);
10984 else
10986 real0 = TREE_REALPART (arg0);
10987 imag0 = TREE_IMAGPART (arg0);
10990 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10992 real1 = TREE_OPERAND (arg1, 0);
10993 imag1 = TREE_OPERAND (arg1, 1);
10995 else
10997 real1 = TREE_REALPART (arg1);
10998 imag1 = TREE_IMAGPART (arg1);
11001 rcond = fold_binary_loc (loc, code, type, real0, real1);
11002 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11004 if (integer_zerop (rcond))
11006 if (code == EQ_EXPR)
11007 return omit_two_operands_loc (loc, type, boolean_false_node,
11008 imag0, imag1);
11009 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
11011 else
11013 if (code == NE_EXPR)
11014 return omit_two_operands_loc (loc, type, boolean_true_node,
11015 imag0, imag1);
11016 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
11020 icond = fold_binary_loc (loc, code, type, imag0, imag1);
11021 if (icond && TREE_CODE (icond) == INTEGER_CST)
11023 if (integer_zerop (icond))
11025 if (code == EQ_EXPR)
11026 return omit_two_operands_loc (loc, type, boolean_false_node,
11027 real0, real1);
11028 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11030 else
11032 if (code == NE_EXPR)
11033 return omit_two_operands_loc (loc, type, boolean_true_node,
11034 real0, real1);
11035 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11040 return NULL_TREE;
11042 case LT_EXPR:
11043 case GT_EXPR:
11044 case LE_EXPR:
11045 case GE_EXPR:
11046 tem = fold_comparison (loc, code, type, op0, op1);
11047 if (tem != NULL_TREE)
11048 return tem;
11050 /* Transform comparisons of the form X +- C CMP X. */
11051 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11052 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11053 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11054 && !HONOR_SNANS (arg0))
11056 tree arg01 = TREE_OPERAND (arg0, 1);
11057 enum tree_code code0 = TREE_CODE (arg0);
11058 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11060 /* (X - c) > X becomes false. */
11061 if (code == GT_EXPR
11062 && ((code0 == MINUS_EXPR && is_positive >= 0)
11063 || (code0 == PLUS_EXPR && is_positive <= 0)))
11064 return constant_boolean_node (0, type);
11066 /* Likewise (X + c) < X becomes false. */
11067 if (code == LT_EXPR
11068 && ((code0 == PLUS_EXPR && is_positive >= 0)
11069 || (code0 == MINUS_EXPR && is_positive <= 0)))
11070 return constant_boolean_node (0, type);
11072 /* Convert (X - c) <= X to true. */
11073 if (!HONOR_NANS (arg1)
11074 && code == LE_EXPR
11075 && ((code0 == MINUS_EXPR && is_positive >= 0)
11076 || (code0 == PLUS_EXPR && is_positive <= 0)))
11077 return constant_boolean_node (1, type);
11079 /* Convert (X + c) >= X to true. */
11080 if (!HONOR_NANS (arg1)
11081 && code == GE_EXPR
11082 && ((code0 == PLUS_EXPR && is_positive >= 0)
11083 || (code0 == MINUS_EXPR && is_positive <= 0)))
11084 return constant_boolean_node (1, type);
11087 /* If we are comparing an ABS_EXPR with a constant, we can
11088 convert all the cases into explicit comparisons, but they may
11089 well not be faster than doing the ABS and one comparison.
11090 But ABS (X) <= C is a range comparison, which becomes a subtraction
11091 and a comparison, and is probably faster. */
11092 if (code == LE_EXPR
11093 && TREE_CODE (arg1) == INTEGER_CST
11094 && TREE_CODE (arg0) == ABS_EXPR
11095 && ! TREE_SIDE_EFFECTS (arg0)
11096 && (tem = negate_expr (arg1)) != 0
11097 && TREE_CODE (tem) == INTEGER_CST
11098 && !TREE_OVERFLOW (tem))
11099 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11100 build2 (GE_EXPR, type,
11101 TREE_OPERAND (arg0, 0), tem),
11102 build2 (LE_EXPR, type,
11103 TREE_OPERAND (arg0, 0), arg1));
11105 /* Convert ABS_EXPR<x> >= 0 to true. */
11106 strict_overflow_p = false;
11107 if (code == GE_EXPR
11108 && (integer_zerop (arg1)
11109 || (! HONOR_NANS (arg0)
11110 && real_zerop (arg1)))
11111 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11113 if (strict_overflow_p)
11114 fold_overflow_warning (("assuming signed overflow does not occur "
11115 "when simplifying comparison of "
11116 "absolute value and zero"),
11117 WARN_STRICT_OVERFLOW_CONDITIONAL);
11118 return omit_one_operand_loc (loc, type,
11119 constant_boolean_node (true, type),
11120 arg0);
11123 /* Convert ABS_EXPR<x> < 0 to false. */
11124 strict_overflow_p = false;
11125 if (code == LT_EXPR
11126 && (integer_zerop (arg1) || real_zerop (arg1))
11127 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11129 if (strict_overflow_p)
11130 fold_overflow_warning (("assuming signed overflow does not occur "
11131 "when simplifying comparison of "
11132 "absolute value and zero"),
11133 WARN_STRICT_OVERFLOW_CONDITIONAL);
11134 return omit_one_operand_loc (loc, type,
11135 constant_boolean_node (false, type),
11136 arg0);
11139 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11140 and similarly for >= into !=. */
11141 if ((code == LT_EXPR || code == GE_EXPR)
11142 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11143 && TREE_CODE (arg1) == LSHIFT_EXPR
11144 && integer_onep (TREE_OPERAND (arg1, 0)))
11145 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11146 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11147 TREE_OPERAND (arg1, 1)),
11148 build_zero_cst (TREE_TYPE (arg0)));
11150 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11151 otherwise Y might be >= # of bits in X's type and thus e.g.
11152 (unsigned char) (1 << Y) for Y 15 might be 0.
11153 If the cast is widening, then 1 << Y should have unsigned type,
11154 otherwise if Y is number of bits in the signed shift type minus 1,
11155 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11156 31 might be 0xffffffff80000000. */
11157 if ((code == LT_EXPR || code == GE_EXPR)
11158 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11159 && CONVERT_EXPR_P (arg1)
11160 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11161 && (element_precision (TREE_TYPE (arg1))
11162 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11163 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11164 || (element_precision (TREE_TYPE (arg1))
11165 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11166 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11168 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11169 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11170 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11171 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11172 build_zero_cst (TREE_TYPE (arg0)));
11175 return NULL_TREE;
11177 case UNORDERED_EXPR:
11178 case ORDERED_EXPR:
11179 case UNLT_EXPR:
11180 case UNLE_EXPR:
11181 case UNGT_EXPR:
11182 case UNGE_EXPR:
11183 case UNEQ_EXPR:
11184 case LTGT_EXPR:
11185 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11187 tree targ0 = strip_float_extensions (arg0);
11188 tree targ1 = strip_float_extensions (arg1);
11189 tree newtype = TREE_TYPE (targ0);
11191 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11192 newtype = TREE_TYPE (targ1);
11194 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11195 return fold_build2_loc (loc, code, type,
11196 fold_convert_loc (loc, newtype, targ0),
11197 fold_convert_loc (loc, newtype, targ1));
11200 return NULL_TREE;
11202 case COMPOUND_EXPR:
11203 /* When pedantic, a compound expression can be neither an lvalue
11204 nor an integer constant expression. */
11205 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11206 return NULL_TREE;
11207 /* Don't let (0, 0) be null pointer constant. */
11208 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11209 : fold_convert_loc (loc, type, arg1);
11210 return pedantic_non_lvalue_loc (loc, tem);
11212 case ASSERT_EXPR:
11213 /* An ASSERT_EXPR should never be passed to fold_binary. */
11214 gcc_unreachable ();
11216 default:
11217 return NULL_TREE;
11218 } /* switch (code) */
11221 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11222 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
11223 of GOTO_EXPR. */
11225 static tree
11226 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
11228 switch (TREE_CODE (*tp))
11230 case LABEL_EXPR:
11231 return *tp;
11233 case GOTO_EXPR:
11234 *walk_subtrees = 0;
11236 /* fall through */
11238 default:
11239 return NULL_TREE;
11243 /* Return whether the sub-tree ST contains a label which is accessible from
11244 outside the sub-tree. */
11246 static bool
11247 contains_label_p (tree st)
11249 return
11250 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
11253 /* Fold a ternary expression of code CODE and type TYPE with operands
11254 OP0, OP1, and OP2. Return the folded expression if folding is
11255 successful. Otherwise, return NULL_TREE. */
11257 tree
11258 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11259 tree op0, tree op1, tree op2)
11261 tree tem;
11262 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11263 enum tree_code_class kind = TREE_CODE_CLASS (code);
11265 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11266 && TREE_CODE_LENGTH (code) == 3);
11268 /* If this is a commutative operation, and OP0 is a constant, move it
11269 to OP1 to reduce the number of tests below. */
11270 if (commutative_ternary_tree_code (code)
11271 && tree_swap_operands_p (op0, op1))
11272 return fold_build3_loc (loc, code, type, op1, op0, op2);
11274 tem = generic_simplify (loc, code, type, op0, op1, op2);
11275 if (tem)
11276 return tem;
11278 /* Strip any conversions that don't change the mode. This is safe
11279 for every expression, except for a comparison expression because
11280 its signedness is derived from its operands. So, in the latter
11281 case, only strip conversions that don't change the signedness.
11283 Note that this is done as an internal manipulation within the
11284 constant folder, in order to find the simplest representation of
11285 the arguments so that their form can be studied. In any cases,
11286 the appropriate type conversions should be put back in the tree
11287 that will get out of the constant folder. */
11288 if (op0)
11290 arg0 = op0;
11291 STRIP_NOPS (arg0);
11294 if (op1)
11296 arg1 = op1;
11297 STRIP_NOPS (arg1);
11300 if (op2)
11302 arg2 = op2;
11303 STRIP_NOPS (arg2);
11306 switch (code)
11308 case COMPONENT_REF:
11309 if (TREE_CODE (arg0) == CONSTRUCTOR
11310 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11312 unsigned HOST_WIDE_INT idx;
11313 tree field, value;
11314 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11315 if (field == arg1)
11316 return value;
11318 return NULL_TREE;
11320 case COND_EXPR:
11321 case VEC_COND_EXPR:
11322 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11323 so all simple results must be passed through pedantic_non_lvalue. */
11324 if (TREE_CODE (arg0) == INTEGER_CST)
11326 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11327 tem = integer_zerop (arg0) ? op2 : op1;
11328 /* Only optimize constant conditions when the selected branch
11329 has the same type as the COND_EXPR. This avoids optimizing
11330 away "c ? x : throw", where the throw has a void type.
11331 Avoid throwing away that operand which contains label. */
11332 if ((!TREE_SIDE_EFFECTS (unused_op)
11333 || !contains_label_p (unused_op))
11334 && (! VOID_TYPE_P (TREE_TYPE (tem))
11335 || VOID_TYPE_P (type)))
11336 return pedantic_non_lvalue_loc (loc, tem);
11337 return NULL_TREE;
11339 else if (TREE_CODE (arg0) == VECTOR_CST)
11341 if ((TREE_CODE (arg1) == VECTOR_CST
11342 || TREE_CODE (arg1) == CONSTRUCTOR)
11343 && (TREE_CODE (arg2) == VECTOR_CST
11344 || TREE_CODE (arg2) == CONSTRUCTOR))
11346 unsigned int nelts = VECTOR_CST_NELTS (arg0), i;
11347 gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
11348 auto_vec_perm_indices sel (nelts);
11349 for (i = 0; i < nelts; i++)
11351 tree val = VECTOR_CST_ELT (arg0, i);
11352 if (integer_all_onesp (val))
11353 sel.quick_push (i);
11354 else if (integer_zerop (val))
11355 sel.quick_push (nelts + i);
11356 else /* Currently unreachable. */
11357 return NULL_TREE;
11359 tree t = fold_vec_perm (type, arg1, arg2, sel);
11360 if (t != NULL_TREE)
11361 return t;
11365 /* If we have A op B ? A : C, we may be able to convert this to a
11366 simpler expression, depending on the operation and the values
11367 of B and C. Signed zeros prevent all of these transformations,
11368 for reasons given above each one.
11370 Also try swapping the arguments and inverting the conditional. */
11371 if (COMPARISON_CLASS_P (arg0)
11372 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
11373 && !HONOR_SIGNED_ZEROS (element_mode (op1)))
11375 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11376 if (tem)
11377 return tem;
11380 if (COMPARISON_CLASS_P (arg0)
11381 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11382 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11384 location_t loc0 = expr_location_or (arg0, loc);
11385 tem = fold_invert_truthvalue (loc0, arg0);
11386 if (tem && COMPARISON_CLASS_P (tem))
11388 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11389 if (tem)
11390 return tem;
11394 /* If the second operand is simpler than the third, swap them
11395 since that produces better jump optimization results. */
11396 if (truth_value_p (TREE_CODE (arg0))
11397 && tree_swap_operands_p (op1, op2))
11399 location_t loc0 = expr_location_or (arg0, loc);
11400 /* See if this can be inverted. If it can't, possibly because
11401 it was a floating-point inequality comparison, don't do
11402 anything. */
11403 tem = fold_invert_truthvalue (loc0, arg0);
11404 if (tem)
11405 return fold_build3_loc (loc, code, type, tem, op2, op1);
11408 /* Convert A ? 1 : 0 to simply A. */
11409 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11410 : (integer_onep (op1)
11411 && !VECTOR_TYPE_P (type)))
11412 && integer_zerop (op2)
11413 /* If we try to convert OP0 to our type, the
11414 call to fold will try to move the conversion inside
11415 a COND, which will recurse. In that case, the COND_EXPR
11416 is probably the best choice, so leave it alone. */
11417 && type == TREE_TYPE (arg0))
11418 return pedantic_non_lvalue_loc (loc, arg0);
11420 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11421 over COND_EXPR in cases such as floating point comparisons. */
11422 if (integer_zerop (op1)
11423 && code == COND_EXPR
11424 && integer_onep (op2)
11425 && !VECTOR_TYPE_P (type)
11426 && truth_value_p (TREE_CODE (arg0)))
11427 return pedantic_non_lvalue_loc (loc,
11428 fold_convert_loc (loc, type,
11429 invert_truthvalue_loc (loc,
11430 arg0)));
11432 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11433 if (TREE_CODE (arg0) == LT_EXPR
11434 && integer_zerop (TREE_OPERAND (arg0, 1))
11435 && integer_zerop (op2)
11436 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11438 /* sign_bit_p looks through both zero and sign extensions,
11439 but for this optimization only sign extensions are
11440 usable. */
11441 tree tem2 = TREE_OPERAND (arg0, 0);
11442 while (tem != tem2)
11444 if (TREE_CODE (tem2) != NOP_EXPR
11445 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11447 tem = NULL_TREE;
11448 break;
11450 tem2 = TREE_OPERAND (tem2, 0);
11452 /* sign_bit_p only checks ARG1 bits within A's precision.
11453 If <sign bit of A> has wider type than A, bits outside
11454 of A's precision in <sign bit of A> need to be checked.
11455 If they are all 0, this optimization needs to be done
11456 in unsigned A's type, if they are all 1 in signed A's type,
11457 otherwise this can't be done. */
11458 if (tem
11459 && TYPE_PRECISION (TREE_TYPE (tem))
11460 < TYPE_PRECISION (TREE_TYPE (arg1))
11461 && TYPE_PRECISION (TREE_TYPE (tem))
11462 < TYPE_PRECISION (type))
11464 int inner_width, outer_width;
11465 tree tem_type;
11467 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11468 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11469 if (outer_width > TYPE_PRECISION (type))
11470 outer_width = TYPE_PRECISION (type);
11472 wide_int mask = wi::shifted_mask
11473 (inner_width, outer_width - inner_width, false,
11474 TYPE_PRECISION (TREE_TYPE (arg1)));
11476 wide_int common = mask & wi::to_wide (arg1);
11477 if (common == mask)
11479 tem_type = signed_type_for (TREE_TYPE (tem));
11480 tem = fold_convert_loc (loc, tem_type, tem);
11482 else if (common == 0)
11484 tem_type = unsigned_type_for (TREE_TYPE (tem));
11485 tem = fold_convert_loc (loc, tem_type, tem);
11487 else
11488 tem = NULL;
11491 if (tem)
11492 return
11493 fold_convert_loc (loc, type,
11494 fold_build2_loc (loc, BIT_AND_EXPR,
11495 TREE_TYPE (tem), tem,
11496 fold_convert_loc (loc,
11497 TREE_TYPE (tem),
11498 arg1)));
11501 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11502 already handled above. */
11503 if (TREE_CODE (arg0) == BIT_AND_EXPR
11504 && integer_onep (TREE_OPERAND (arg0, 1))
11505 && integer_zerop (op2)
11506 && integer_pow2p (arg1))
11508 tree tem = TREE_OPERAND (arg0, 0);
11509 STRIP_NOPS (tem);
11510 if (TREE_CODE (tem) == RSHIFT_EXPR
11511 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11512 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11513 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11514 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11515 fold_convert_loc (loc, type,
11516 TREE_OPERAND (tem, 0)),
11517 op1);
11520 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11521 is probably obsolete because the first operand should be a
11522 truth value (that's why we have the two cases above), but let's
11523 leave it in until we can confirm this for all front-ends. */
11524 if (integer_zerop (op2)
11525 && TREE_CODE (arg0) == NE_EXPR
11526 && integer_zerop (TREE_OPERAND (arg0, 1))
11527 && integer_pow2p (arg1)
11528 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11529 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11530 arg1, OEP_ONLY_CONST))
11531 return pedantic_non_lvalue_loc (loc,
11532 fold_convert_loc (loc, type,
11533 TREE_OPERAND (arg0, 0)));
11535 /* Disable the transformations below for vectors, since
11536 fold_binary_op_with_conditional_arg may undo them immediately,
11537 yielding an infinite loop. */
11538 if (code == VEC_COND_EXPR)
11539 return NULL_TREE;
11541 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11542 if (integer_zerop (op2)
11543 && truth_value_p (TREE_CODE (arg0))
11544 && truth_value_p (TREE_CODE (arg1))
11545 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11546 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11547 : TRUTH_ANDIF_EXPR,
11548 type, fold_convert_loc (loc, type, arg0), op1);
11550 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11551 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11552 && truth_value_p (TREE_CODE (arg0))
11553 && truth_value_p (TREE_CODE (arg1))
11554 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11556 location_t loc0 = expr_location_or (arg0, loc);
11557 /* Only perform transformation if ARG0 is easily inverted. */
11558 tem = fold_invert_truthvalue (loc0, arg0);
11559 if (tem)
11560 return fold_build2_loc (loc, code == VEC_COND_EXPR
11561 ? BIT_IOR_EXPR
11562 : TRUTH_ORIF_EXPR,
11563 type, fold_convert_loc (loc, type, tem),
11564 op1);
11567 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11568 if (integer_zerop (arg1)
11569 && truth_value_p (TREE_CODE (arg0))
11570 && truth_value_p (TREE_CODE (op2))
11571 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11573 location_t loc0 = expr_location_or (arg0, loc);
11574 /* Only perform transformation if ARG0 is easily inverted. */
11575 tem = fold_invert_truthvalue (loc0, arg0);
11576 if (tem)
11577 return fold_build2_loc (loc, code == VEC_COND_EXPR
11578 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11579 type, fold_convert_loc (loc, type, tem),
11580 op2);
11583 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11584 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11585 && truth_value_p (TREE_CODE (arg0))
11586 && truth_value_p (TREE_CODE (op2))
11587 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11588 return fold_build2_loc (loc, code == VEC_COND_EXPR
11589 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11590 type, fold_convert_loc (loc, type, arg0), op2);
11592 return NULL_TREE;
11594 case CALL_EXPR:
11595 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11596 of fold_ternary on them. */
11597 gcc_unreachable ();
11599 case BIT_FIELD_REF:
11600 if (TREE_CODE (arg0) == VECTOR_CST
11601 && (type == TREE_TYPE (TREE_TYPE (arg0))
11602 || (TREE_CODE (type) == VECTOR_TYPE
11603 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0)))))
11605 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11606 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11607 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11608 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11610 if (n != 0
11611 && (idx % width) == 0
11612 && (n % width) == 0
11613 && ((idx + n) / width) <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
11615 idx = idx / width;
11616 n = n / width;
11618 if (TREE_CODE (arg0) == VECTOR_CST)
11620 if (n == 1)
11621 return VECTOR_CST_ELT (arg0, idx);
11623 tree_vector_builder vals (type, n, 1);
11624 for (unsigned i = 0; i < n; ++i)
11625 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
11626 return vals.build ();
11631 /* On constants we can use native encode/interpret to constant
11632 fold (nearly) all BIT_FIELD_REFs. */
11633 if (CONSTANT_CLASS_P (arg0)
11634 && can_native_interpret_type_p (type)
11635 && BITS_PER_UNIT == 8)
11637 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11638 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11639 /* Limit us to a reasonable amount of work. To relax the
11640 other limitations we need bit-shifting of the buffer
11641 and rounding up the size. */
11642 if (bitpos % BITS_PER_UNIT == 0
11643 && bitsize % BITS_PER_UNIT == 0
11644 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11646 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11647 unsigned HOST_WIDE_INT len
11648 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11649 bitpos / BITS_PER_UNIT);
11650 if (len > 0
11651 && len * BITS_PER_UNIT >= bitsize)
11653 tree v = native_interpret_expr (type, b,
11654 bitsize / BITS_PER_UNIT);
11655 if (v)
11656 return v;
11661 return NULL_TREE;
11663 case FMA_EXPR:
11664 /* For integers we can decompose the FMA if possible. */
11665 if (TREE_CODE (arg0) == INTEGER_CST
11666 && TREE_CODE (arg1) == INTEGER_CST)
11667 return fold_build2_loc (loc, PLUS_EXPR, type,
11668 const_binop (MULT_EXPR, arg0, arg1), arg2);
11669 if (integer_zerop (arg2))
11670 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
11672 return fold_fma (loc, type, arg0, arg1, arg2);
11674 case VEC_PERM_EXPR:
11675 if (TREE_CODE (arg2) == VECTOR_CST)
11677 unsigned int nelts = VECTOR_CST_NELTS (arg2), i, mask, mask2;
11678 bool need_mask_canon = false;
11679 bool need_mask_canon2 = false;
11680 bool all_in_vec0 = true;
11681 bool all_in_vec1 = true;
11682 bool maybe_identity = true;
11683 bool single_arg = (op0 == op1);
11684 bool changed = false;
11686 mask2 = 2 * nelts - 1;
11687 mask = single_arg ? (nelts - 1) : mask2;
11688 gcc_assert (nelts == TYPE_VECTOR_SUBPARTS (type));
11689 auto_vec_perm_indices sel (nelts);
11690 auto_vec_perm_indices sel2 (nelts);
11691 for (i = 0; i < nelts; i++)
11693 tree val = VECTOR_CST_ELT (arg2, i);
11694 if (TREE_CODE (val) != INTEGER_CST)
11695 return NULL_TREE;
11697 /* Make sure that the perm value is in an acceptable
11698 range. */
11699 wi::tree_to_wide_ref t = wi::to_wide (val);
11700 need_mask_canon |= wi::gtu_p (t, mask);
11701 need_mask_canon2 |= wi::gtu_p (t, mask2);
11702 unsigned int elt = t.to_uhwi () & mask;
11703 unsigned int elt2 = t.to_uhwi () & mask2;
11705 if (elt < nelts)
11706 all_in_vec1 = false;
11707 else
11708 all_in_vec0 = false;
11710 if ((elt & (nelts - 1)) != i)
11711 maybe_identity = false;
11713 sel.quick_push (elt);
11714 sel2.quick_push (elt2);
11717 if (maybe_identity)
11719 if (all_in_vec0)
11720 return op0;
11721 if (all_in_vec1)
11722 return op1;
11725 if (all_in_vec0)
11726 op1 = op0;
11727 else if (all_in_vec1)
11729 op0 = op1;
11730 for (i = 0; i < nelts; i++)
11731 sel[i] -= nelts;
11732 need_mask_canon = true;
11735 if ((TREE_CODE (op0) == VECTOR_CST
11736 || TREE_CODE (op0) == CONSTRUCTOR)
11737 && (TREE_CODE (op1) == VECTOR_CST
11738 || TREE_CODE (op1) == CONSTRUCTOR))
11740 tree t = fold_vec_perm (type, op0, op1, sel);
11741 if (t != NULL_TREE)
11742 return t;
11745 if (op0 == op1 && !single_arg)
11746 changed = true;
11748 /* Some targets are deficient and fail to expand a single
11749 argument permutation while still allowing an equivalent
11750 2-argument version. */
11751 if (need_mask_canon && arg2 == op2
11752 && !can_vec_perm_p (TYPE_MODE (type), false, &sel)
11753 && can_vec_perm_p (TYPE_MODE (type), false, &sel2))
11755 need_mask_canon = need_mask_canon2;
11756 sel = sel2;
11759 if (need_mask_canon && arg2 == op2)
11761 tree eltype = TREE_TYPE (TREE_TYPE (arg2));
11762 tree_vector_builder tsel (TREE_TYPE (arg2), nelts, 1);
11763 for (i = 0; i < nelts; i++)
11764 tsel.quick_push (build_int_cst (eltype, sel[i]));
11765 op2 = tsel.build ();
11766 changed = true;
11769 if (changed)
11770 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11772 return NULL_TREE;
11774 case BIT_INSERT_EXPR:
11775 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11776 if (TREE_CODE (arg0) == INTEGER_CST
11777 && TREE_CODE (arg1) == INTEGER_CST)
11779 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11780 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11781 wide_int tem = (wi::to_wide (arg0)
11782 & wi::shifted_mask (bitpos, bitsize, true,
11783 TYPE_PRECISION (type)));
11784 wide_int tem2
11785 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11786 bitsize), bitpos);
11787 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11789 else if (TREE_CODE (arg0) == VECTOR_CST
11790 && CONSTANT_CLASS_P (arg1)
11791 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11792 TREE_TYPE (arg1)))
11794 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11795 unsigned HOST_WIDE_INT elsize
11796 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11797 if (bitpos % elsize == 0)
11799 unsigned k = bitpos / elsize;
11800 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11801 return arg0;
11802 else
11804 unsigned int nelts = VECTOR_CST_NELTS (arg0);
11805 tree_vector_builder elts (type, nelts, 1);
11806 elts.quick_grow (nelts);
11807 for (unsigned int i = 0; i < nelts; ++i)
11808 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
11809 return elts.build ();
11813 return NULL_TREE;
11815 default:
11816 return NULL_TREE;
11817 } /* switch (code) */
11820 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11821 of an array (or vector). */
11823 tree
11824 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11826 tree index_type = NULL_TREE;
11827 offset_int low_bound = 0;
11829 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11831 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11832 if (domain_type && TYPE_MIN_VALUE (domain_type))
11834 /* Static constructors for variably sized objects makes no sense. */
11835 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11836 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11837 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11841 if (index_type)
11842 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11843 TYPE_SIGN (index_type));
11845 offset_int index = low_bound - 1;
11846 if (index_type)
11847 index = wi::ext (index, TYPE_PRECISION (index_type),
11848 TYPE_SIGN (index_type));
11850 offset_int max_index;
11851 unsigned HOST_WIDE_INT cnt;
11852 tree cfield, cval;
11854 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11856 /* Array constructor might explicitly set index, or specify a range,
11857 or leave index NULL meaning that it is next index after previous
11858 one. */
11859 if (cfield)
11861 if (TREE_CODE (cfield) == INTEGER_CST)
11862 max_index = index = wi::to_offset (cfield);
11863 else
11865 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11866 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11867 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11870 else
11872 index += 1;
11873 if (index_type)
11874 index = wi::ext (index, TYPE_PRECISION (index_type),
11875 TYPE_SIGN (index_type));
11876 max_index = index;
11879 /* Do we have match? */
11880 if (wi::cmpu (access_index, index) >= 0
11881 && wi::cmpu (access_index, max_index) <= 0)
11882 return cval;
11884 return NULL_TREE;
11887 /* Perform constant folding and related simplification of EXPR.
11888 The related simplifications include x*1 => x, x*0 => 0, etc.,
11889 and application of the associative law.
11890 NOP_EXPR conversions may be removed freely (as long as we
11891 are careful not to change the type of the overall expression).
11892 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11893 but we can constant-fold them if they have constant operands. */
11895 #ifdef ENABLE_FOLD_CHECKING
11896 # define fold(x) fold_1 (x)
11897 static tree fold_1 (tree);
11898 static
11899 #endif
11900 tree
11901 fold (tree expr)
11903 const tree t = expr;
11904 enum tree_code code = TREE_CODE (t);
11905 enum tree_code_class kind = TREE_CODE_CLASS (code);
11906 tree tem;
11907 location_t loc = EXPR_LOCATION (expr);
11909 /* Return right away if a constant. */
11910 if (kind == tcc_constant)
11911 return t;
11913 /* CALL_EXPR-like objects with variable numbers of operands are
11914 treated specially. */
11915 if (kind == tcc_vl_exp)
11917 if (code == CALL_EXPR)
11919 tem = fold_call_expr (loc, expr, false);
11920 return tem ? tem : expr;
11922 return expr;
11925 if (IS_EXPR_CODE_CLASS (kind))
11927 tree type = TREE_TYPE (t);
11928 tree op0, op1, op2;
11930 switch (TREE_CODE_LENGTH (code))
11932 case 1:
11933 op0 = TREE_OPERAND (t, 0);
11934 tem = fold_unary_loc (loc, code, type, op0);
11935 return tem ? tem : expr;
11936 case 2:
11937 op0 = TREE_OPERAND (t, 0);
11938 op1 = TREE_OPERAND (t, 1);
11939 tem = fold_binary_loc (loc, code, type, op0, op1);
11940 return tem ? tem : expr;
11941 case 3:
11942 op0 = TREE_OPERAND (t, 0);
11943 op1 = TREE_OPERAND (t, 1);
11944 op2 = TREE_OPERAND (t, 2);
11945 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11946 return tem ? tem : expr;
11947 default:
11948 break;
11952 switch (code)
11954 case ARRAY_REF:
11956 tree op0 = TREE_OPERAND (t, 0);
11957 tree op1 = TREE_OPERAND (t, 1);
11959 if (TREE_CODE (op1) == INTEGER_CST
11960 && TREE_CODE (op0) == CONSTRUCTOR
11961 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
11963 tree val = get_array_ctor_element_at_index (op0,
11964 wi::to_offset (op1));
11965 if (val)
11966 return val;
11969 return t;
11972 /* Return a VECTOR_CST if possible. */
11973 case CONSTRUCTOR:
11975 tree type = TREE_TYPE (t);
11976 if (TREE_CODE (type) != VECTOR_TYPE)
11977 return t;
11979 unsigned i;
11980 tree val;
11981 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
11982 if (! CONSTANT_CLASS_P (val))
11983 return t;
11985 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
11988 case CONST_DECL:
11989 return fold (DECL_INITIAL (t));
11991 default:
11992 return t;
11993 } /* switch (code) */
11996 #ifdef ENABLE_FOLD_CHECKING
11997 #undef fold
11999 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12000 hash_table<nofree_ptr_hash<const tree_node> > *);
12001 static void fold_check_failed (const_tree, const_tree);
12002 void print_fold_checksum (const_tree);
12004 /* When --enable-checking=fold, compute a digest of expr before
12005 and after actual fold call to see if fold did not accidentally
12006 change original expr. */
12008 tree
12009 fold (tree expr)
12011 tree ret;
12012 struct md5_ctx ctx;
12013 unsigned char checksum_before[16], checksum_after[16];
12014 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12016 md5_init_ctx (&ctx);
12017 fold_checksum_tree (expr, &ctx, &ht);
12018 md5_finish_ctx (&ctx, checksum_before);
12019 ht.empty ();
12021 ret = fold_1 (expr);
12023 md5_init_ctx (&ctx);
12024 fold_checksum_tree (expr, &ctx, &ht);
12025 md5_finish_ctx (&ctx, checksum_after);
12027 if (memcmp (checksum_before, checksum_after, 16))
12028 fold_check_failed (expr, ret);
12030 return ret;
12033 void
12034 print_fold_checksum (const_tree expr)
12036 struct md5_ctx ctx;
12037 unsigned char checksum[16], cnt;
12038 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12040 md5_init_ctx (&ctx);
12041 fold_checksum_tree (expr, &ctx, &ht);
12042 md5_finish_ctx (&ctx, checksum);
12043 for (cnt = 0; cnt < 16; ++cnt)
12044 fprintf (stderr, "%02x", checksum[cnt]);
12045 putc ('\n', stderr);
12048 static void
12049 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12051 internal_error ("fold check: original tree changed by fold");
12054 static void
12055 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12056 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12058 const tree_node **slot;
12059 enum tree_code code;
12060 union tree_node buf;
12061 int i, len;
12063 recursive_label:
12064 if (expr == NULL)
12065 return;
12066 slot = ht->find_slot (expr, INSERT);
12067 if (*slot != NULL)
12068 return;
12069 *slot = expr;
12070 code = TREE_CODE (expr);
12071 if (TREE_CODE_CLASS (code) == tcc_declaration
12072 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12074 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12075 memcpy ((char *) &buf, expr, tree_size (expr));
12076 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12077 buf.decl_with_vis.symtab_node = NULL;
12078 expr = (tree) &buf;
12080 else if (TREE_CODE_CLASS (code) == tcc_type
12081 && (TYPE_POINTER_TO (expr)
12082 || TYPE_REFERENCE_TO (expr)
12083 || TYPE_CACHED_VALUES_P (expr)
12084 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12085 || TYPE_NEXT_VARIANT (expr)
12086 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12088 /* Allow these fields to be modified. */
12089 tree tmp;
12090 memcpy ((char *) &buf, expr, tree_size (expr));
12091 expr = tmp = (tree) &buf;
12092 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12093 TYPE_POINTER_TO (tmp) = NULL;
12094 TYPE_REFERENCE_TO (tmp) = NULL;
12095 TYPE_NEXT_VARIANT (tmp) = NULL;
12096 TYPE_ALIAS_SET (tmp) = -1;
12097 if (TYPE_CACHED_VALUES_P (tmp))
12099 TYPE_CACHED_VALUES_P (tmp) = 0;
12100 TYPE_CACHED_VALUES (tmp) = NULL;
12103 md5_process_bytes (expr, tree_size (expr), ctx);
12104 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12105 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12106 if (TREE_CODE_CLASS (code) != tcc_type
12107 && TREE_CODE_CLASS (code) != tcc_declaration
12108 && code != TREE_LIST
12109 && code != SSA_NAME
12110 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12111 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12112 switch (TREE_CODE_CLASS (code))
12114 case tcc_constant:
12115 switch (code)
12117 case STRING_CST:
12118 md5_process_bytes (TREE_STRING_POINTER (expr),
12119 TREE_STRING_LENGTH (expr), ctx);
12120 break;
12121 case COMPLEX_CST:
12122 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12123 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12124 break;
12125 case VECTOR_CST:
12126 len = vector_cst_encoded_nelts (expr);
12127 for (i = 0; i < len; ++i)
12128 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
12129 break;
12130 default:
12131 break;
12133 break;
12134 case tcc_exceptional:
12135 switch (code)
12137 case TREE_LIST:
12138 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12139 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12140 expr = TREE_CHAIN (expr);
12141 goto recursive_label;
12142 break;
12143 case TREE_VEC:
12144 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12145 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12146 break;
12147 default:
12148 break;
12150 break;
12151 case tcc_expression:
12152 case tcc_reference:
12153 case tcc_comparison:
12154 case tcc_unary:
12155 case tcc_binary:
12156 case tcc_statement:
12157 case tcc_vl_exp:
12158 len = TREE_OPERAND_LENGTH (expr);
12159 for (i = 0; i < len; ++i)
12160 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12161 break;
12162 case tcc_declaration:
12163 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12164 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12165 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12167 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12168 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12169 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12170 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12171 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12174 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12176 if (TREE_CODE (expr) == FUNCTION_DECL)
12178 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12179 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12181 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12183 break;
12184 case tcc_type:
12185 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12186 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12187 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12188 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12189 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12190 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12191 if (INTEGRAL_TYPE_P (expr)
12192 || SCALAR_FLOAT_TYPE_P (expr))
12194 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12195 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12197 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12198 if (TREE_CODE (expr) == RECORD_TYPE
12199 || TREE_CODE (expr) == UNION_TYPE
12200 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12201 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12202 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12203 break;
12204 default:
12205 break;
12209 /* Helper function for outputting the checksum of a tree T. When
12210 debugging with gdb, you can "define mynext" to be "next" followed
12211 by "call debug_fold_checksum (op0)", then just trace down till the
12212 outputs differ. */
12214 DEBUG_FUNCTION void
12215 debug_fold_checksum (const_tree t)
12217 int i;
12218 unsigned char checksum[16];
12219 struct md5_ctx ctx;
12220 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12222 md5_init_ctx (&ctx);
12223 fold_checksum_tree (t, &ctx, &ht);
12224 md5_finish_ctx (&ctx, checksum);
12225 ht.empty ();
12227 for (i = 0; i < 16; i++)
12228 fprintf (stderr, "%d ", checksum[i]);
12230 fprintf (stderr, "\n");
12233 #endif
12235 /* Fold a unary tree expression with code CODE of type TYPE with an
12236 operand OP0. LOC is the location of the resulting expression.
12237 Return a folded expression if successful. Otherwise, return a tree
12238 expression with code CODE of type TYPE with an operand OP0. */
12240 tree
12241 fold_build1_loc (location_t loc,
12242 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12244 tree tem;
12245 #ifdef ENABLE_FOLD_CHECKING
12246 unsigned char checksum_before[16], checksum_after[16];
12247 struct md5_ctx ctx;
12248 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12250 md5_init_ctx (&ctx);
12251 fold_checksum_tree (op0, &ctx, &ht);
12252 md5_finish_ctx (&ctx, checksum_before);
12253 ht.empty ();
12254 #endif
12256 tem = fold_unary_loc (loc, code, type, op0);
12257 if (!tem)
12258 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12260 #ifdef ENABLE_FOLD_CHECKING
12261 md5_init_ctx (&ctx);
12262 fold_checksum_tree (op0, &ctx, &ht);
12263 md5_finish_ctx (&ctx, checksum_after);
12265 if (memcmp (checksum_before, checksum_after, 16))
12266 fold_check_failed (op0, tem);
12267 #endif
12268 return tem;
12271 /* Fold a binary tree expression with code CODE of type TYPE with
12272 operands OP0 and OP1. LOC is the location of the resulting
12273 expression. Return a folded expression if successful. Otherwise,
12274 return a tree expression with code CODE of type TYPE with operands
12275 OP0 and OP1. */
12277 tree
12278 fold_build2_loc (location_t loc,
12279 enum tree_code code, tree type, tree op0, tree op1
12280 MEM_STAT_DECL)
12282 tree tem;
12283 #ifdef ENABLE_FOLD_CHECKING
12284 unsigned char checksum_before_op0[16],
12285 checksum_before_op1[16],
12286 checksum_after_op0[16],
12287 checksum_after_op1[16];
12288 struct md5_ctx ctx;
12289 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12291 md5_init_ctx (&ctx);
12292 fold_checksum_tree (op0, &ctx, &ht);
12293 md5_finish_ctx (&ctx, checksum_before_op0);
12294 ht.empty ();
12296 md5_init_ctx (&ctx);
12297 fold_checksum_tree (op1, &ctx, &ht);
12298 md5_finish_ctx (&ctx, checksum_before_op1);
12299 ht.empty ();
12300 #endif
12302 tem = fold_binary_loc (loc, code, type, op0, op1);
12303 if (!tem)
12304 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12306 #ifdef ENABLE_FOLD_CHECKING
12307 md5_init_ctx (&ctx);
12308 fold_checksum_tree (op0, &ctx, &ht);
12309 md5_finish_ctx (&ctx, checksum_after_op0);
12310 ht.empty ();
12312 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12313 fold_check_failed (op0, tem);
12315 md5_init_ctx (&ctx);
12316 fold_checksum_tree (op1, &ctx, &ht);
12317 md5_finish_ctx (&ctx, checksum_after_op1);
12319 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12320 fold_check_failed (op1, tem);
12321 #endif
12322 return tem;
12325 /* Fold a ternary tree expression with code CODE of type TYPE with
12326 operands OP0, OP1, and OP2. Return a folded expression if
12327 successful. Otherwise, return a tree expression with code CODE of
12328 type TYPE with operands OP0, OP1, and OP2. */
12330 tree
12331 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12332 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12334 tree tem;
12335 #ifdef ENABLE_FOLD_CHECKING
12336 unsigned char checksum_before_op0[16],
12337 checksum_before_op1[16],
12338 checksum_before_op2[16],
12339 checksum_after_op0[16],
12340 checksum_after_op1[16],
12341 checksum_after_op2[16];
12342 struct md5_ctx ctx;
12343 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12345 md5_init_ctx (&ctx);
12346 fold_checksum_tree (op0, &ctx, &ht);
12347 md5_finish_ctx (&ctx, checksum_before_op0);
12348 ht.empty ();
12350 md5_init_ctx (&ctx);
12351 fold_checksum_tree (op1, &ctx, &ht);
12352 md5_finish_ctx (&ctx, checksum_before_op1);
12353 ht.empty ();
12355 md5_init_ctx (&ctx);
12356 fold_checksum_tree (op2, &ctx, &ht);
12357 md5_finish_ctx (&ctx, checksum_before_op2);
12358 ht.empty ();
12359 #endif
12361 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12362 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12363 if (!tem)
12364 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12366 #ifdef ENABLE_FOLD_CHECKING
12367 md5_init_ctx (&ctx);
12368 fold_checksum_tree (op0, &ctx, &ht);
12369 md5_finish_ctx (&ctx, checksum_after_op0);
12370 ht.empty ();
12372 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12373 fold_check_failed (op0, tem);
12375 md5_init_ctx (&ctx);
12376 fold_checksum_tree (op1, &ctx, &ht);
12377 md5_finish_ctx (&ctx, checksum_after_op1);
12378 ht.empty ();
12380 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12381 fold_check_failed (op1, tem);
12383 md5_init_ctx (&ctx);
12384 fold_checksum_tree (op2, &ctx, &ht);
12385 md5_finish_ctx (&ctx, checksum_after_op2);
12387 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12388 fold_check_failed (op2, tem);
12389 #endif
12390 return tem;
12393 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12394 arguments in ARGARRAY, and a null static chain.
12395 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12396 of type TYPE from the given operands as constructed by build_call_array. */
12398 tree
12399 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12400 int nargs, tree *argarray)
12402 tree tem;
12403 #ifdef ENABLE_FOLD_CHECKING
12404 unsigned char checksum_before_fn[16],
12405 checksum_before_arglist[16],
12406 checksum_after_fn[16],
12407 checksum_after_arglist[16];
12408 struct md5_ctx ctx;
12409 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12410 int i;
12412 md5_init_ctx (&ctx);
12413 fold_checksum_tree (fn, &ctx, &ht);
12414 md5_finish_ctx (&ctx, checksum_before_fn);
12415 ht.empty ();
12417 md5_init_ctx (&ctx);
12418 for (i = 0; i < nargs; i++)
12419 fold_checksum_tree (argarray[i], &ctx, &ht);
12420 md5_finish_ctx (&ctx, checksum_before_arglist);
12421 ht.empty ();
12422 #endif
12424 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12425 if (!tem)
12426 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12428 #ifdef ENABLE_FOLD_CHECKING
12429 md5_init_ctx (&ctx);
12430 fold_checksum_tree (fn, &ctx, &ht);
12431 md5_finish_ctx (&ctx, checksum_after_fn);
12432 ht.empty ();
12434 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12435 fold_check_failed (fn, tem);
12437 md5_init_ctx (&ctx);
12438 for (i = 0; i < nargs; i++)
12439 fold_checksum_tree (argarray[i], &ctx, &ht);
12440 md5_finish_ctx (&ctx, checksum_after_arglist);
12442 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12443 fold_check_failed (NULL_TREE, tem);
12444 #endif
12445 return tem;
12448 /* Perform constant folding and related simplification of initializer
12449 expression EXPR. These behave identically to "fold_buildN" but ignore
12450 potential run-time traps and exceptions that fold must preserve. */
12452 #define START_FOLD_INIT \
12453 int saved_signaling_nans = flag_signaling_nans;\
12454 int saved_trapping_math = flag_trapping_math;\
12455 int saved_rounding_math = flag_rounding_math;\
12456 int saved_trapv = flag_trapv;\
12457 int saved_folding_initializer = folding_initializer;\
12458 flag_signaling_nans = 0;\
12459 flag_trapping_math = 0;\
12460 flag_rounding_math = 0;\
12461 flag_trapv = 0;\
12462 folding_initializer = 1;
12464 #define END_FOLD_INIT \
12465 flag_signaling_nans = saved_signaling_nans;\
12466 flag_trapping_math = saved_trapping_math;\
12467 flag_rounding_math = saved_rounding_math;\
12468 flag_trapv = saved_trapv;\
12469 folding_initializer = saved_folding_initializer;
12471 tree
12472 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12473 tree type, tree op)
12475 tree result;
12476 START_FOLD_INIT;
12478 result = fold_build1_loc (loc, code, type, op);
12480 END_FOLD_INIT;
12481 return result;
12484 tree
12485 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12486 tree type, tree op0, tree op1)
12488 tree result;
12489 START_FOLD_INIT;
12491 result = fold_build2_loc (loc, code, type, op0, op1);
12493 END_FOLD_INIT;
12494 return result;
12497 tree
12498 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12499 int nargs, tree *argarray)
12501 tree result;
12502 START_FOLD_INIT;
12504 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12506 END_FOLD_INIT;
12507 return result;
12510 #undef START_FOLD_INIT
12511 #undef END_FOLD_INIT
12513 /* Determine if first argument is a multiple of second argument. Return 0 if
12514 it is not, or we cannot easily determined it to be.
12516 An example of the sort of thing we care about (at this point; this routine
12517 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12518 fold cases do now) is discovering that
12520 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12522 is a multiple of
12524 SAVE_EXPR (J * 8)
12526 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12528 This code also handles discovering that
12530 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12532 is a multiple of 8 so we don't have to worry about dealing with a
12533 possible remainder.
12535 Note that we *look* inside a SAVE_EXPR only to determine how it was
12536 calculated; it is not safe for fold to do much of anything else with the
12537 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12538 at run time. For example, the latter example above *cannot* be implemented
12539 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12540 evaluation time of the original SAVE_EXPR is not necessarily the same at
12541 the time the new expression is evaluated. The only optimization of this
12542 sort that would be valid is changing
12544 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12546 divided by 8 to
12548 SAVE_EXPR (I) * SAVE_EXPR (J)
12550 (where the same SAVE_EXPR (J) is used in the original and the
12551 transformed version). */
12554 multiple_of_p (tree type, const_tree top, const_tree bottom)
12556 gimple *stmt;
12557 tree t1, op1, op2;
12559 if (operand_equal_p (top, bottom, 0))
12560 return 1;
12562 if (TREE_CODE (type) != INTEGER_TYPE)
12563 return 0;
12565 switch (TREE_CODE (top))
12567 case BIT_AND_EXPR:
12568 /* Bitwise and provides a power of two multiple. If the mask is
12569 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12570 if (!integer_pow2p (bottom))
12571 return 0;
12572 /* FALLTHRU */
12574 case MULT_EXPR:
12575 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12576 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12578 case MINUS_EXPR:
12579 /* It is impossible to prove if op0 - op1 is multiple of bottom
12580 precisely, so be conservative here checking if both op0 and op1
12581 are multiple of bottom. Note we check the second operand first
12582 since it's usually simpler. */
12583 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12584 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12586 case PLUS_EXPR:
12587 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12588 as op0 - 3 if the expression has unsigned type. For example,
12589 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12590 op1 = TREE_OPERAND (top, 1);
12591 if (TYPE_UNSIGNED (type)
12592 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12593 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12594 return (multiple_of_p (type, op1, bottom)
12595 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12597 case LSHIFT_EXPR:
12598 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12600 op1 = TREE_OPERAND (top, 1);
12601 /* const_binop may not detect overflow correctly,
12602 so check for it explicitly here. */
12603 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
12604 wi::to_wide (op1))
12605 && (t1 = fold_convert (type,
12606 const_binop (LSHIFT_EXPR, size_one_node,
12607 op1))) != 0
12608 && !TREE_OVERFLOW (t1))
12609 return multiple_of_p (type, t1, bottom);
12611 return 0;
12613 case NOP_EXPR:
12614 /* Can't handle conversions from non-integral or wider integral type. */
12615 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12616 || (TYPE_PRECISION (type)
12617 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12618 return 0;
12620 /* fall through */
12622 case SAVE_EXPR:
12623 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12625 case COND_EXPR:
12626 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12627 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12629 case INTEGER_CST:
12630 if (TREE_CODE (bottom) != INTEGER_CST
12631 || integer_zerop (bottom)
12632 || (TYPE_UNSIGNED (type)
12633 && (tree_int_cst_sgn (top) < 0
12634 || tree_int_cst_sgn (bottom) < 0)))
12635 return 0;
12636 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12637 SIGNED);
12639 case SSA_NAME:
12640 if (TREE_CODE (bottom) == INTEGER_CST
12641 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12642 && gimple_code (stmt) == GIMPLE_ASSIGN)
12644 enum tree_code code = gimple_assign_rhs_code (stmt);
12646 /* Check for special cases to see if top is defined as multiple
12647 of bottom:
12649 top = (X & ~(bottom - 1) ; bottom is power of 2
12653 Y = X % bottom
12654 top = X - Y. */
12655 if (code == BIT_AND_EXPR
12656 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12657 && TREE_CODE (op2) == INTEGER_CST
12658 && integer_pow2p (bottom)
12659 && wi::multiple_of_p (wi::to_widest (op2),
12660 wi::to_widest (bottom), UNSIGNED))
12661 return 1;
12663 op1 = gimple_assign_rhs1 (stmt);
12664 if (code == MINUS_EXPR
12665 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12666 && TREE_CODE (op2) == SSA_NAME
12667 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12668 && gimple_code (stmt) == GIMPLE_ASSIGN
12669 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12670 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12671 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12672 return 1;
12675 /* fall through */
12677 default:
12678 if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
12679 return multiple_p (wi::to_poly_widest (top),
12680 wi::to_poly_widest (bottom));
12682 return 0;
12686 #define tree_expr_nonnegative_warnv_p(X, Y) \
12687 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12689 #define RECURSE(X) \
12690 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12692 /* Return true if CODE or TYPE is known to be non-negative. */
12694 static bool
12695 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12697 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12698 && truth_value_p (code))
12699 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12700 have a signed:1 type (where the value is -1 and 0). */
12701 return true;
12702 return false;
12705 /* Return true if (CODE OP0) is known to be non-negative. If the return
12706 value is based on the assumption that signed overflow is undefined,
12707 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12708 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12710 bool
12711 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12712 bool *strict_overflow_p, int depth)
12714 if (TYPE_UNSIGNED (type))
12715 return true;
12717 switch (code)
12719 case ABS_EXPR:
12720 /* We can't return 1 if flag_wrapv is set because
12721 ABS_EXPR<INT_MIN> = INT_MIN. */
12722 if (!ANY_INTEGRAL_TYPE_P (type))
12723 return true;
12724 if (TYPE_OVERFLOW_UNDEFINED (type))
12726 *strict_overflow_p = true;
12727 return true;
12729 break;
12731 case NON_LVALUE_EXPR:
12732 case FLOAT_EXPR:
12733 case FIX_TRUNC_EXPR:
12734 return RECURSE (op0);
12736 CASE_CONVERT:
12738 tree inner_type = TREE_TYPE (op0);
12739 tree outer_type = type;
12741 if (TREE_CODE (outer_type) == REAL_TYPE)
12743 if (TREE_CODE (inner_type) == REAL_TYPE)
12744 return RECURSE (op0);
12745 if (INTEGRAL_TYPE_P (inner_type))
12747 if (TYPE_UNSIGNED (inner_type))
12748 return true;
12749 return RECURSE (op0);
12752 else if (INTEGRAL_TYPE_P (outer_type))
12754 if (TREE_CODE (inner_type) == REAL_TYPE)
12755 return RECURSE (op0);
12756 if (INTEGRAL_TYPE_P (inner_type))
12757 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12758 && TYPE_UNSIGNED (inner_type);
12761 break;
12763 default:
12764 return tree_simple_nonnegative_warnv_p (code, type);
12767 /* We don't know sign of `t', so be conservative and return false. */
12768 return false;
12771 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12772 value is based on the assumption that signed overflow is undefined,
12773 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12774 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12776 bool
12777 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12778 tree op1, bool *strict_overflow_p,
12779 int depth)
12781 if (TYPE_UNSIGNED (type))
12782 return true;
12784 switch (code)
12786 case POINTER_PLUS_EXPR:
12787 case PLUS_EXPR:
12788 if (FLOAT_TYPE_P (type))
12789 return RECURSE (op0) && RECURSE (op1);
12791 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12792 both unsigned and at least 2 bits shorter than the result. */
12793 if (TREE_CODE (type) == INTEGER_TYPE
12794 && TREE_CODE (op0) == NOP_EXPR
12795 && TREE_CODE (op1) == NOP_EXPR)
12797 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12798 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12799 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12800 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12802 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12803 TYPE_PRECISION (inner2)) + 1;
12804 return prec < TYPE_PRECISION (type);
12807 break;
12809 case MULT_EXPR:
12810 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12812 /* x * x is always non-negative for floating point x
12813 or without overflow. */
12814 if (operand_equal_p (op0, op1, 0)
12815 || (RECURSE (op0) && RECURSE (op1)))
12817 if (ANY_INTEGRAL_TYPE_P (type)
12818 && TYPE_OVERFLOW_UNDEFINED (type))
12819 *strict_overflow_p = true;
12820 return true;
12824 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12825 both unsigned and their total bits is shorter than the result. */
12826 if (TREE_CODE (type) == INTEGER_TYPE
12827 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12828 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12830 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12831 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12832 : TREE_TYPE (op0);
12833 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12834 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12835 : TREE_TYPE (op1);
12837 bool unsigned0 = TYPE_UNSIGNED (inner0);
12838 bool unsigned1 = TYPE_UNSIGNED (inner1);
12840 if (TREE_CODE (op0) == INTEGER_CST)
12841 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12843 if (TREE_CODE (op1) == INTEGER_CST)
12844 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12846 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12847 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12849 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12850 ? tree_int_cst_min_precision (op0, UNSIGNED)
12851 : TYPE_PRECISION (inner0);
12853 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12854 ? tree_int_cst_min_precision (op1, UNSIGNED)
12855 : TYPE_PRECISION (inner1);
12857 return precision0 + precision1 < TYPE_PRECISION (type);
12860 return false;
12862 case BIT_AND_EXPR:
12863 case MAX_EXPR:
12864 return RECURSE (op0) || RECURSE (op1);
12866 case BIT_IOR_EXPR:
12867 case BIT_XOR_EXPR:
12868 case MIN_EXPR:
12869 case RDIV_EXPR:
12870 case TRUNC_DIV_EXPR:
12871 case CEIL_DIV_EXPR:
12872 case FLOOR_DIV_EXPR:
12873 case ROUND_DIV_EXPR:
12874 return RECURSE (op0) && RECURSE (op1);
12876 case TRUNC_MOD_EXPR:
12877 return RECURSE (op0);
12879 case FLOOR_MOD_EXPR:
12880 return RECURSE (op1);
12882 case CEIL_MOD_EXPR:
12883 case ROUND_MOD_EXPR:
12884 default:
12885 return tree_simple_nonnegative_warnv_p (code, type);
12888 /* We don't know sign of `t', so be conservative and return false. */
12889 return false;
12892 /* Return true if T is known to be non-negative. If the return
12893 value is based on the assumption that signed overflow is undefined,
12894 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12895 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12897 bool
12898 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12900 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12901 return true;
12903 switch (TREE_CODE (t))
12905 case INTEGER_CST:
12906 return tree_int_cst_sgn (t) >= 0;
12908 case REAL_CST:
12909 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12911 case FIXED_CST:
12912 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12914 case COND_EXPR:
12915 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12917 case SSA_NAME:
12918 /* Limit the depth of recursion to avoid quadratic behavior.
12919 This is expected to catch almost all occurrences in practice.
12920 If this code misses important cases that unbounded recursion
12921 would not, passes that need this information could be revised
12922 to provide it through dataflow propagation. */
12923 return (!name_registered_for_update_p (t)
12924 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12925 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12926 strict_overflow_p, depth));
12928 default:
12929 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12933 /* Return true if T is known to be non-negative. If the return
12934 value is based on the assumption that signed overflow is undefined,
12935 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12936 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12938 bool
12939 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
12940 bool *strict_overflow_p, int depth)
12942 switch (fn)
12944 CASE_CFN_ACOS:
12945 CASE_CFN_ACOSH:
12946 CASE_CFN_CABS:
12947 CASE_CFN_COSH:
12948 CASE_CFN_ERFC:
12949 CASE_CFN_EXP:
12950 CASE_CFN_EXP10:
12951 CASE_CFN_EXP2:
12952 CASE_CFN_FABS:
12953 CASE_CFN_FDIM:
12954 CASE_CFN_HYPOT:
12955 CASE_CFN_POW10:
12956 CASE_CFN_FFS:
12957 CASE_CFN_PARITY:
12958 CASE_CFN_POPCOUNT:
12959 CASE_CFN_CLZ:
12960 CASE_CFN_CLRSB:
12961 case CFN_BUILT_IN_BSWAP32:
12962 case CFN_BUILT_IN_BSWAP64:
12963 /* Always true. */
12964 return true;
12966 CASE_CFN_SQRT:
12967 CASE_CFN_SQRT_FN:
12968 /* sqrt(-0.0) is -0.0. */
12969 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
12970 return true;
12971 return RECURSE (arg0);
12973 CASE_CFN_ASINH:
12974 CASE_CFN_ATAN:
12975 CASE_CFN_ATANH:
12976 CASE_CFN_CBRT:
12977 CASE_CFN_CEIL:
12978 CASE_CFN_ERF:
12979 CASE_CFN_EXPM1:
12980 CASE_CFN_FLOOR:
12981 CASE_CFN_FMOD:
12982 CASE_CFN_FREXP:
12983 CASE_CFN_ICEIL:
12984 CASE_CFN_IFLOOR:
12985 CASE_CFN_IRINT:
12986 CASE_CFN_IROUND:
12987 CASE_CFN_LCEIL:
12988 CASE_CFN_LDEXP:
12989 CASE_CFN_LFLOOR:
12990 CASE_CFN_LLCEIL:
12991 CASE_CFN_LLFLOOR:
12992 CASE_CFN_LLRINT:
12993 CASE_CFN_LLROUND:
12994 CASE_CFN_LRINT:
12995 CASE_CFN_LROUND:
12996 CASE_CFN_MODF:
12997 CASE_CFN_NEARBYINT:
12998 CASE_CFN_RINT:
12999 CASE_CFN_ROUND:
13000 CASE_CFN_SCALB:
13001 CASE_CFN_SCALBLN:
13002 CASE_CFN_SCALBN:
13003 CASE_CFN_SIGNBIT:
13004 CASE_CFN_SIGNIFICAND:
13005 CASE_CFN_SINH:
13006 CASE_CFN_TANH:
13007 CASE_CFN_TRUNC:
13008 /* True if the 1st argument is nonnegative. */
13009 return RECURSE (arg0);
13011 CASE_CFN_FMAX:
13012 CASE_CFN_FMAX_FN:
13013 /* True if the 1st OR 2nd arguments are nonnegative. */
13014 return RECURSE (arg0) || RECURSE (arg1);
13016 CASE_CFN_FMIN:
13017 CASE_CFN_FMIN_FN:
13018 /* True if the 1st AND 2nd arguments are nonnegative. */
13019 return RECURSE (arg0) && RECURSE (arg1);
13021 CASE_CFN_COPYSIGN:
13022 CASE_CFN_COPYSIGN_FN:
13023 /* True if the 2nd argument is nonnegative. */
13024 return RECURSE (arg1);
13026 CASE_CFN_POWI:
13027 /* True if the 1st argument is nonnegative or the second
13028 argument is an even integer. */
13029 if (TREE_CODE (arg1) == INTEGER_CST
13030 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13031 return true;
13032 return RECURSE (arg0);
13034 CASE_CFN_POW:
13035 /* True if the 1st argument is nonnegative or the second
13036 argument is an even integer valued real. */
13037 if (TREE_CODE (arg1) == REAL_CST)
13039 REAL_VALUE_TYPE c;
13040 HOST_WIDE_INT n;
13042 c = TREE_REAL_CST (arg1);
13043 n = real_to_integer (&c);
13044 if ((n & 1) == 0)
13046 REAL_VALUE_TYPE cint;
13047 real_from_integer (&cint, VOIDmode, n, SIGNED);
13048 if (real_identical (&c, &cint))
13049 return true;
13052 return RECURSE (arg0);
13054 default:
13055 break;
13057 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13060 /* Return true if T is known to be non-negative. If the return
13061 value is based on the assumption that signed overflow is undefined,
13062 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13063 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13065 static bool
13066 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13068 enum tree_code code = TREE_CODE (t);
13069 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13070 return true;
13072 switch (code)
13074 case TARGET_EXPR:
13076 tree temp = TARGET_EXPR_SLOT (t);
13077 t = TARGET_EXPR_INITIAL (t);
13079 /* If the initializer is non-void, then it's a normal expression
13080 that will be assigned to the slot. */
13081 if (!VOID_TYPE_P (t))
13082 return RECURSE (t);
13084 /* Otherwise, the initializer sets the slot in some way. One common
13085 way is an assignment statement at the end of the initializer. */
13086 while (1)
13088 if (TREE_CODE (t) == BIND_EXPR)
13089 t = expr_last (BIND_EXPR_BODY (t));
13090 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13091 || TREE_CODE (t) == TRY_CATCH_EXPR)
13092 t = expr_last (TREE_OPERAND (t, 0));
13093 else if (TREE_CODE (t) == STATEMENT_LIST)
13094 t = expr_last (t);
13095 else
13096 break;
13098 if (TREE_CODE (t) == MODIFY_EXPR
13099 && TREE_OPERAND (t, 0) == temp)
13100 return RECURSE (TREE_OPERAND (t, 1));
13102 return false;
13105 case CALL_EXPR:
13107 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13108 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13110 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13111 get_call_combined_fn (t),
13112 arg0,
13113 arg1,
13114 strict_overflow_p, depth);
13116 case COMPOUND_EXPR:
13117 case MODIFY_EXPR:
13118 return RECURSE (TREE_OPERAND (t, 1));
13120 case BIND_EXPR:
13121 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13123 case SAVE_EXPR:
13124 return RECURSE (TREE_OPERAND (t, 0));
13126 default:
13127 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13131 #undef RECURSE
13132 #undef tree_expr_nonnegative_warnv_p
13134 /* Return true if T is known to be non-negative. If the return
13135 value is based on the assumption that signed overflow is undefined,
13136 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13137 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13139 bool
13140 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13142 enum tree_code code;
13143 if (t == error_mark_node)
13144 return false;
13146 code = TREE_CODE (t);
13147 switch (TREE_CODE_CLASS (code))
13149 case tcc_binary:
13150 case tcc_comparison:
13151 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13152 TREE_TYPE (t),
13153 TREE_OPERAND (t, 0),
13154 TREE_OPERAND (t, 1),
13155 strict_overflow_p, depth);
13157 case tcc_unary:
13158 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13159 TREE_TYPE (t),
13160 TREE_OPERAND (t, 0),
13161 strict_overflow_p, depth);
13163 case tcc_constant:
13164 case tcc_declaration:
13165 case tcc_reference:
13166 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13168 default:
13169 break;
13172 switch (code)
13174 case TRUTH_AND_EXPR:
13175 case TRUTH_OR_EXPR:
13176 case TRUTH_XOR_EXPR:
13177 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13178 TREE_TYPE (t),
13179 TREE_OPERAND (t, 0),
13180 TREE_OPERAND (t, 1),
13181 strict_overflow_p, depth);
13182 case TRUTH_NOT_EXPR:
13183 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13184 TREE_TYPE (t),
13185 TREE_OPERAND (t, 0),
13186 strict_overflow_p, depth);
13188 case COND_EXPR:
13189 case CONSTRUCTOR:
13190 case OBJ_TYPE_REF:
13191 case ASSERT_EXPR:
13192 case ADDR_EXPR:
13193 case WITH_SIZE_EXPR:
13194 case SSA_NAME:
13195 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13197 default:
13198 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13202 /* Return true if `t' is known to be non-negative. Handle warnings
13203 about undefined signed overflow. */
13205 bool
13206 tree_expr_nonnegative_p (tree t)
13208 bool ret, strict_overflow_p;
13210 strict_overflow_p = false;
13211 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13212 if (strict_overflow_p)
13213 fold_overflow_warning (("assuming signed overflow does not occur when "
13214 "determining that expression is always "
13215 "non-negative"),
13216 WARN_STRICT_OVERFLOW_MISC);
13217 return ret;
13221 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13222 For floating point we further ensure that T is not denormal.
13223 Similar logic is present in nonzero_address in rtlanal.h.
13225 If the return value is based on the assumption that signed overflow
13226 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13227 change *STRICT_OVERFLOW_P. */
13229 bool
13230 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13231 bool *strict_overflow_p)
13233 switch (code)
13235 case ABS_EXPR:
13236 return tree_expr_nonzero_warnv_p (op0,
13237 strict_overflow_p);
13239 case NOP_EXPR:
13241 tree inner_type = TREE_TYPE (op0);
13242 tree outer_type = type;
13244 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13245 && tree_expr_nonzero_warnv_p (op0,
13246 strict_overflow_p));
13248 break;
13250 case NON_LVALUE_EXPR:
13251 return tree_expr_nonzero_warnv_p (op0,
13252 strict_overflow_p);
13254 default:
13255 break;
13258 return false;
13261 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13262 For floating point we further ensure that T is not denormal.
13263 Similar logic is present in nonzero_address in rtlanal.h.
13265 If the return value is based on the assumption that signed overflow
13266 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13267 change *STRICT_OVERFLOW_P. */
13269 bool
13270 tree_binary_nonzero_warnv_p (enum tree_code code,
13271 tree type,
13272 tree op0,
13273 tree op1, bool *strict_overflow_p)
13275 bool sub_strict_overflow_p;
13276 switch (code)
13278 case POINTER_PLUS_EXPR:
13279 case PLUS_EXPR:
13280 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13282 /* With the presence of negative values it is hard
13283 to say something. */
13284 sub_strict_overflow_p = false;
13285 if (!tree_expr_nonnegative_warnv_p (op0,
13286 &sub_strict_overflow_p)
13287 || !tree_expr_nonnegative_warnv_p (op1,
13288 &sub_strict_overflow_p))
13289 return false;
13290 /* One of operands must be positive and the other non-negative. */
13291 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13292 overflows, on a twos-complement machine the sum of two
13293 nonnegative numbers can never be zero. */
13294 return (tree_expr_nonzero_warnv_p (op0,
13295 strict_overflow_p)
13296 || tree_expr_nonzero_warnv_p (op1,
13297 strict_overflow_p));
13299 break;
13301 case MULT_EXPR:
13302 if (TYPE_OVERFLOW_UNDEFINED (type))
13304 if (tree_expr_nonzero_warnv_p (op0,
13305 strict_overflow_p)
13306 && tree_expr_nonzero_warnv_p (op1,
13307 strict_overflow_p))
13309 *strict_overflow_p = true;
13310 return true;
13313 break;
13315 case MIN_EXPR:
13316 sub_strict_overflow_p = false;
13317 if (tree_expr_nonzero_warnv_p (op0,
13318 &sub_strict_overflow_p)
13319 && tree_expr_nonzero_warnv_p (op1,
13320 &sub_strict_overflow_p))
13322 if (sub_strict_overflow_p)
13323 *strict_overflow_p = true;
13325 break;
13327 case MAX_EXPR:
13328 sub_strict_overflow_p = false;
13329 if (tree_expr_nonzero_warnv_p (op0,
13330 &sub_strict_overflow_p))
13332 if (sub_strict_overflow_p)
13333 *strict_overflow_p = true;
13335 /* When both operands are nonzero, then MAX must be too. */
13336 if (tree_expr_nonzero_warnv_p (op1,
13337 strict_overflow_p))
13338 return true;
13340 /* MAX where operand 0 is positive is positive. */
13341 return tree_expr_nonnegative_warnv_p (op0,
13342 strict_overflow_p);
13344 /* MAX where operand 1 is positive is positive. */
13345 else if (tree_expr_nonzero_warnv_p (op1,
13346 &sub_strict_overflow_p)
13347 && tree_expr_nonnegative_warnv_p (op1,
13348 &sub_strict_overflow_p))
13350 if (sub_strict_overflow_p)
13351 *strict_overflow_p = true;
13352 return true;
13354 break;
13356 case BIT_IOR_EXPR:
13357 return (tree_expr_nonzero_warnv_p (op1,
13358 strict_overflow_p)
13359 || tree_expr_nonzero_warnv_p (op0,
13360 strict_overflow_p));
13362 default:
13363 break;
13366 return false;
13369 /* Return true when T is an address and is known to be nonzero.
13370 For floating point we further ensure that T is not denormal.
13371 Similar logic is present in nonzero_address in rtlanal.h.
13373 If the return value is based on the assumption that signed overflow
13374 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13375 change *STRICT_OVERFLOW_P. */
13377 bool
13378 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13380 bool sub_strict_overflow_p;
13381 switch (TREE_CODE (t))
13383 case INTEGER_CST:
13384 return !integer_zerop (t);
13386 case ADDR_EXPR:
13388 tree base = TREE_OPERAND (t, 0);
13390 if (!DECL_P (base))
13391 base = get_base_address (base);
13393 if (base && TREE_CODE (base) == TARGET_EXPR)
13394 base = TARGET_EXPR_SLOT (base);
13396 if (!base)
13397 return false;
13399 /* For objects in symbol table check if we know they are non-zero.
13400 Don't do anything for variables and functions before symtab is built;
13401 it is quite possible that they will be declared weak later. */
13402 int nonzero_addr = maybe_nonzero_address (base);
13403 if (nonzero_addr >= 0)
13404 return nonzero_addr;
13406 /* Constants are never weak. */
13407 if (CONSTANT_CLASS_P (base))
13408 return true;
13410 return false;
13413 case COND_EXPR:
13414 sub_strict_overflow_p = false;
13415 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13416 &sub_strict_overflow_p)
13417 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13418 &sub_strict_overflow_p))
13420 if (sub_strict_overflow_p)
13421 *strict_overflow_p = true;
13422 return true;
13424 break;
13426 case SSA_NAME:
13427 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13428 break;
13429 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13431 default:
13432 break;
13434 return false;
13437 #define integer_valued_real_p(X) \
13438 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13440 #define RECURSE(X) \
13441 ((integer_valued_real_p) (X, depth + 1))
13443 /* Return true if the floating point result of (CODE OP0) has an
13444 integer value. We also allow +Inf, -Inf and NaN to be considered
13445 integer values. Return false for signaling NaN.
13447 DEPTH is the current nesting depth of the query. */
13449 bool
13450 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13452 switch (code)
13454 case FLOAT_EXPR:
13455 return true;
13457 case ABS_EXPR:
13458 return RECURSE (op0);
13460 CASE_CONVERT:
13462 tree type = TREE_TYPE (op0);
13463 if (TREE_CODE (type) == INTEGER_TYPE)
13464 return true;
13465 if (TREE_CODE (type) == REAL_TYPE)
13466 return RECURSE (op0);
13467 break;
13470 default:
13471 break;
13473 return false;
13476 /* Return true if the floating point result of (CODE OP0 OP1) has an
13477 integer value. We also allow +Inf, -Inf and NaN to be considered
13478 integer values. Return false for signaling NaN.
13480 DEPTH is the current nesting depth of the query. */
13482 bool
13483 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13485 switch (code)
13487 case PLUS_EXPR:
13488 case MINUS_EXPR:
13489 case MULT_EXPR:
13490 case MIN_EXPR:
13491 case MAX_EXPR:
13492 return RECURSE (op0) && RECURSE (op1);
13494 default:
13495 break;
13497 return false;
13500 /* Return true if the floating point result of calling FNDECL with arguments
13501 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13502 considered integer values. Return false for signaling NaN. If FNDECL
13503 takes fewer than 2 arguments, the remaining ARGn are null.
13505 DEPTH is the current nesting depth of the query. */
13507 bool
13508 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13510 switch (fn)
13512 CASE_CFN_CEIL:
13513 CASE_CFN_FLOOR:
13514 CASE_CFN_NEARBYINT:
13515 CASE_CFN_RINT:
13516 CASE_CFN_ROUND:
13517 CASE_CFN_TRUNC:
13518 return true;
13520 CASE_CFN_FMIN:
13521 CASE_CFN_FMIN_FN:
13522 CASE_CFN_FMAX:
13523 CASE_CFN_FMAX_FN:
13524 return RECURSE (arg0) && RECURSE (arg1);
13526 default:
13527 break;
13529 return false;
13532 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13533 has an integer value. We also allow +Inf, -Inf and NaN to be
13534 considered integer values. Return false for signaling NaN.
13536 DEPTH is the current nesting depth of the query. */
13538 bool
13539 integer_valued_real_single_p (tree t, int depth)
13541 switch (TREE_CODE (t))
13543 case REAL_CST:
13544 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13546 case COND_EXPR:
13547 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13549 case SSA_NAME:
13550 /* Limit the depth of recursion to avoid quadratic behavior.
13551 This is expected to catch almost all occurrences in practice.
13552 If this code misses important cases that unbounded recursion
13553 would not, passes that need this information could be revised
13554 to provide it through dataflow propagation. */
13555 return (!name_registered_for_update_p (t)
13556 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13557 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13558 depth));
13560 default:
13561 break;
13563 return false;
13566 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13567 has an integer value. We also allow +Inf, -Inf and NaN to be
13568 considered integer values. Return false for signaling NaN.
13570 DEPTH is the current nesting depth of the query. */
13572 static bool
13573 integer_valued_real_invalid_p (tree t, int depth)
13575 switch (TREE_CODE (t))
13577 case COMPOUND_EXPR:
13578 case MODIFY_EXPR:
13579 case BIND_EXPR:
13580 return RECURSE (TREE_OPERAND (t, 1));
13582 case SAVE_EXPR:
13583 return RECURSE (TREE_OPERAND (t, 0));
13585 default:
13586 break;
13588 return false;
13591 #undef RECURSE
13592 #undef integer_valued_real_p
13594 /* Return true if the floating point expression T has an integer value.
13595 We also allow +Inf, -Inf and NaN to be considered integer values.
13596 Return false for signaling NaN.
13598 DEPTH is the current nesting depth of the query. */
13600 bool
13601 integer_valued_real_p (tree t, int depth)
13603 if (t == error_mark_node)
13604 return false;
13606 tree_code code = TREE_CODE (t);
13607 switch (TREE_CODE_CLASS (code))
13609 case tcc_binary:
13610 case tcc_comparison:
13611 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13612 TREE_OPERAND (t, 1), depth);
13614 case tcc_unary:
13615 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13617 case tcc_constant:
13618 case tcc_declaration:
13619 case tcc_reference:
13620 return integer_valued_real_single_p (t, depth);
13622 default:
13623 break;
13626 switch (code)
13628 case COND_EXPR:
13629 case SSA_NAME:
13630 return integer_valued_real_single_p (t, depth);
13632 case CALL_EXPR:
13634 tree arg0 = (call_expr_nargs (t) > 0
13635 ? CALL_EXPR_ARG (t, 0)
13636 : NULL_TREE);
13637 tree arg1 = (call_expr_nargs (t) > 1
13638 ? CALL_EXPR_ARG (t, 1)
13639 : NULL_TREE);
13640 return integer_valued_real_call_p (get_call_combined_fn (t),
13641 arg0, arg1, depth);
13644 default:
13645 return integer_valued_real_invalid_p (t, depth);
13649 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13650 attempt to fold the expression to a constant without modifying TYPE,
13651 OP0 or OP1.
13653 If the expression could be simplified to a constant, then return
13654 the constant. If the expression would not be simplified to a
13655 constant, then return NULL_TREE. */
13657 tree
13658 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13660 tree tem = fold_binary (code, type, op0, op1);
13661 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13664 /* Given the components of a unary expression CODE, TYPE and OP0,
13665 attempt to fold the expression to a constant without modifying
13666 TYPE or OP0.
13668 If the expression could be simplified to a constant, then return
13669 the constant. If the expression would not be simplified to a
13670 constant, then return NULL_TREE. */
13672 tree
13673 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13675 tree tem = fold_unary (code, type, op0);
13676 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13679 /* If EXP represents referencing an element in a constant string
13680 (either via pointer arithmetic or array indexing), return the
13681 tree representing the value accessed, otherwise return NULL. */
13683 tree
13684 fold_read_from_constant_string (tree exp)
13686 if ((TREE_CODE (exp) == INDIRECT_REF
13687 || TREE_CODE (exp) == ARRAY_REF)
13688 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13690 tree exp1 = TREE_OPERAND (exp, 0);
13691 tree index;
13692 tree string;
13693 location_t loc = EXPR_LOCATION (exp);
13695 if (TREE_CODE (exp) == INDIRECT_REF)
13696 string = string_constant (exp1, &index);
13697 else
13699 tree low_bound = array_ref_low_bound (exp);
13700 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13702 /* Optimize the special-case of a zero lower bound.
13704 We convert the low_bound to sizetype to avoid some problems
13705 with constant folding. (E.g. suppose the lower bound is 1,
13706 and its mode is QI. Without the conversion,l (ARRAY
13707 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13708 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13709 if (! integer_zerop (low_bound))
13710 index = size_diffop_loc (loc, index,
13711 fold_convert_loc (loc, sizetype, low_bound));
13713 string = exp1;
13716 scalar_int_mode char_mode;
13717 if (string
13718 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13719 && TREE_CODE (string) == STRING_CST
13720 && TREE_CODE (index) == INTEGER_CST
13721 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13722 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13723 &char_mode)
13724 && GET_MODE_SIZE (char_mode) == 1)
13725 return build_int_cst_type (TREE_TYPE (exp),
13726 (TREE_STRING_POINTER (string)
13727 [TREE_INT_CST_LOW (index)]));
13729 return NULL;
13732 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13733 an integer constant, real, or fixed-point constant.
13735 TYPE is the type of the result. */
13737 static tree
13738 fold_negate_const (tree arg0, tree type)
13740 tree t = NULL_TREE;
13742 switch (TREE_CODE (arg0))
13744 case REAL_CST:
13745 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13746 break;
13748 case FIXED_CST:
13750 FIXED_VALUE_TYPE f;
13751 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13752 &(TREE_FIXED_CST (arg0)), NULL,
13753 TYPE_SATURATING (type));
13754 t = build_fixed (type, f);
13755 /* Propagate overflow flags. */
13756 if (overflow_p | TREE_OVERFLOW (arg0))
13757 TREE_OVERFLOW (t) = 1;
13758 break;
13761 default:
13762 if (poly_int_tree_p (arg0))
13764 bool overflow;
13765 poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
13766 t = force_fit_type (type, res, 1,
13767 (overflow && ! TYPE_UNSIGNED (type))
13768 || TREE_OVERFLOW (arg0));
13769 break;
13772 gcc_unreachable ();
13775 return t;
13778 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13779 an integer constant or real constant.
13781 TYPE is the type of the result. */
13783 tree
13784 fold_abs_const (tree arg0, tree type)
13786 tree t = NULL_TREE;
13788 switch (TREE_CODE (arg0))
13790 case INTEGER_CST:
13792 /* If the value is unsigned or non-negative, then the absolute value
13793 is the same as the ordinary value. */
13794 if (!wi::neg_p (wi::to_wide (arg0), TYPE_SIGN (type)))
13795 t = arg0;
13797 /* If the value is negative, then the absolute value is
13798 its negation. */
13799 else
13801 bool overflow;
13802 wide_int val = wi::neg (wi::to_wide (arg0), &overflow);
13803 t = force_fit_type (type, val, -1,
13804 overflow | TREE_OVERFLOW (arg0));
13807 break;
13809 case REAL_CST:
13810 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13811 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13812 else
13813 t = arg0;
13814 break;
13816 default:
13817 gcc_unreachable ();
13820 return t;
13823 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13824 constant. TYPE is the type of the result. */
13826 static tree
13827 fold_not_const (const_tree arg0, tree type)
13829 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13831 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
13834 /* Given CODE, a relational operator, the target type, TYPE and two
13835 constant operands OP0 and OP1, return the result of the
13836 relational operation. If the result is not a compile time
13837 constant, then return NULL_TREE. */
13839 static tree
13840 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13842 int result, invert;
13844 /* From here on, the only cases we handle are when the result is
13845 known to be a constant. */
13847 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13849 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13850 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13852 /* Handle the cases where either operand is a NaN. */
13853 if (real_isnan (c0) || real_isnan (c1))
13855 switch (code)
13857 case EQ_EXPR:
13858 case ORDERED_EXPR:
13859 result = 0;
13860 break;
13862 case NE_EXPR:
13863 case UNORDERED_EXPR:
13864 case UNLT_EXPR:
13865 case UNLE_EXPR:
13866 case UNGT_EXPR:
13867 case UNGE_EXPR:
13868 case UNEQ_EXPR:
13869 result = 1;
13870 break;
13872 case LT_EXPR:
13873 case LE_EXPR:
13874 case GT_EXPR:
13875 case GE_EXPR:
13876 case LTGT_EXPR:
13877 if (flag_trapping_math)
13878 return NULL_TREE;
13879 result = 0;
13880 break;
13882 default:
13883 gcc_unreachable ();
13886 return constant_boolean_node (result, type);
13889 return constant_boolean_node (real_compare (code, c0, c1), type);
13892 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13894 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13895 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13896 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13899 /* Handle equality/inequality of complex constants. */
13900 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13902 tree rcond = fold_relational_const (code, type,
13903 TREE_REALPART (op0),
13904 TREE_REALPART (op1));
13905 tree icond = fold_relational_const (code, type,
13906 TREE_IMAGPART (op0),
13907 TREE_IMAGPART (op1));
13908 if (code == EQ_EXPR)
13909 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13910 else if (code == NE_EXPR)
13911 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13912 else
13913 return NULL_TREE;
13916 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13918 if (!VECTOR_TYPE_P (type))
13920 /* Have vector comparison with scalar boolean result. */
13921 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
13922 && VECTOR_CST_NELTS (op0) == VECTOR_CST_NELTS (op1));
13923 for (unsigned i = 0; i < VECTOR_CST_NELTS (op0); i++)
13925 tree elem0 = VECTOR_CST_ELT (op0, i);
13926 tree elem1 = VECTOR_CST_ELT (op1, i);
13927 tree tmp = fold_relational_const (code, type, elem0, elem1);
13928 if (tmp == NULL_TREE)
13929 return NULL_TREE;
13930 if (integer_zerop (tmp))
13931 return constant_boolean_node (false, type);
13933 return constant_boolean_node (true, type);
13935 tree_vector_builder elts;
13936 if (!elts.new_binary_operation (type, op0, op1, false))
13937 return NULL_TREE;
13938 unsigned int count = elts.encoded_nelts ();
13939 for (unsigned i = 0; i < count; i++)
13941 tree elem_type = TREE_TYPE (type);
13942 tree elem0 = VECTOR_CST_ELT (op0, i);
13943 tree elem1 = VECTOR_CST_ELT (op1, i);
13945 tree tem = fold_relational_const (code, elem_type,
13946 elem0, elem1);
13948 if (tem == NULL_TREE)
13949 return NULL_TREE;
13951 elts.quick_push (build_int_cst (elem_type,
13952 integer_zerop (tem) ? 0 : -1));
13955 return elts.build ();
13958 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
13960 To compute GT, swap the arguments and do LT.
13961 To compute GE, do LT and invert the result.
13962 To compute LE, swap the arguments, do LT and invert the result.
13963 To compute NE, do EQ and invert the result.
13965 Therefore, the code below must handle only EQ and LT. */
13967 if (code == LE_EXPR || code == GT_EXPR)
13969 std::swap (op0, op1);
13970 code = swap_tree_comparison (code);
13973 /* Note that it is safe to invert for real values here because we
13974 have already handled the one case that it matters. */
13976 invert = 0;
13977 if (code == NE_EXPR || code == GE_EXPR)
13979 invert = 1;
13980 code = invert_tree_comparison (code, false);
13983 /* Compute a result for LT or EQ if args permit;
13984 Otherwise return T. */
13985 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
13987 if (code == EQ_EXPR)
13988 result = tree_int_cst_equal (op0, op1);
13989 else
13990 result = tree_int_cst_lt (op0, op1);
13992 else
13993 return NULL_TREE;
13995 if (invert)
13996 result ^= 1;
13997 return constant_boolean_node (result, type);
14000 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14001 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14002 itself. */
14004 tree
14005 fold_build_cleanup_point_expr (tree type, tree expr)
14007 /* If the expression does not have side effects then we don't have to wrap
14008 it with a cleanup point expression. */
14009 if (!TREE_SIDE_EFFECTS (expr))
14010 return expr;
14012 /* If the expression is a return, check to see if the expression inside the
14013 return has no side effects or the right hand side of the modify expression
14014 inside the return. If either don't have side effects set we don't need to
14015 wrap the expression in a cleanup point expression. Note we don't check the
14016 left hand side of the modify because it should always be a return decl. */
14017 if (TREE_CODE (expr) == RETURN_EXPR)
14019 tree op = TREE_OPERAND (expr, 0);
14020 if (!op || !TREE_SIDE_EFFECTS (op))
14021 return expr;
14022 op = TREE_OPERAND (op, 1);
14023 if (!TREE_SIDE_EFFECTS (op))
14024 return expr;
14027 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
14030 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14031 of an indirection through OP0, or NULL_TREE if no simplification is
14032 possible. */
14034 tree
14035 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14037 tree sub = op0;
14038 tree subtype;
14040 STRIP_NOPS (sub);
14041 subtype = TREE_TYPE (sub);
14042 if (!POINTER_TYPE_P (subtype)
14043 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
14044 return NULL_TREE;
14046 if (TREE_CODE (sub) == ADDR_EXPR)
14048 tree op = TREE_OPERAND (sub, 0);
14049 tree optype = TREE_TYPE (op);
14050 /* *&CONST_DECL -> to the value of the const decl. */
14051 if (TREE_CODE (op) == CONST_DECL)
14052 return DECL_INITIAL (op);
14053 /* *&p => p; make sure to handle *&"str"[cst] here. */
14054 if (type == optype)
14056 tree fop = fold_read_from_constant_string (op);
14057 if (fop)
14058 return fop;
14059 else
14060 return op;
14062 /* *(foo *)&fooarray => fooarray[0] */
14063 else if (TREE_CODE (optype) == ARRAY_TYPE
14064 && type == TREE_TYPE (optype)
14065 && (!in_gimple_form
14066 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14068 tree type_domain = TYPE_DOMAIN (optype);
14069 tree min_val = size_zero_node;
14070 if (type_domain && TYPE_MIN_VALUE (type_domain))
14071 min_val = TYPE_MIN_VALUE (type_domain);
14072 if (in_gimple_form
14073 && TREE_CODE (min_val) != INTEGER_CST)
14074 return NULL_TREE;
14075 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14076 NULL_TREE, NULL_TREE);
14078 /* *(foo *)&complexfoo => __real__ complexfoo */
14079 else if (TREE_CODE (optype) == COMPLEX_TYPE
14080 && type == TREE_TYPE (optype))
14081 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14082 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14083 else if (TREE_CODE (optype) == VECTOR_TYPE
14084 && type == TREE_TYPE (optype))
14086 tree part_width = TYPE_SIZE (type);
14087 tree index = bitsize_int (0);
14088 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
14092 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14093 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14095 tree op00 = TREE_OPERAND (sub, 0);
14096 tree op01 = TREE_OPERAND (sub, 1);
14098 STRIP_NOPS (op00);
14099 if (TREE_CODE (op00) == ADDR_EXPR)
14101 tree op00type;
14102 op00 = TREE_OPERAND (op00, 0);
14103 op00type = TREE_TYPE (op00);
14105 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14106 if (TREE_CODE (op00type) == VECTOR_TYPE
14107 && type == TREE_TYPE (op00type))
14109 tree part_width = TYPE_SIZE (type);
14110 unsigned HOST_WIDE_INT max_offset
14111 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14112 * TYPE_VECTOR_SUBPARTS (op00type));
14113 if (tree_int_cst_sign_bit (op01) == 0
14114 && compare_tree_int (op01, max_offset) == -1)
14116 unsigned HOST_WIDE_INT offset = tree_to_uhwi (op01);
14117 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
14118 tree index = bitsize_int (indexi);
14119 return fold_build3_loc (loc,
14120 BIT_FIELD_REF, type, op00,
14121 part_width, index);
14124 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14125 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14126 && type == TREE_TYPE (op00type))
14128 tree size = TYPE_SIZE_UNIT (type);
14129 if (tree_int_cst_equal (size, op01))
14130 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14132 /* ((foo *)&fooarray)[1] => fooarray[1] */
14133 else if (TREE_CODE (op00type) == ARRAY_TYPE
14134 && type == TREE_TYPE (op00type))
14136 tree type_domain = TYPE_DOMAIN (op00type);
14137 tree min = size_zero_node;
14138 if (type_domain && TYPE_MIN_VALUE (type_domain))
14139 min = TYPE_MIN_VALUE (type_domain);
14140 offset_int off = wi::to_offset (op01);
14141 offset_int el_sz = wi::to_offset (TYPE_SIZE_UNIT (type));
14142 offset_int remainder;
14143 off = wi::divmod_trunc (off, el_sz, SIGNED, &remainder);
14144 if (remainder == 0 && TREE_CODE (min) == INTEGER_CST)
14146 off = off + wi::to_offset (min);
14147 op01 = wide_int_to_tree (sizetype, off);
14148 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14149 NULL_TREE, NULL_TREE);
14155 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14156 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14157 && type == TREE_TYPE (TREE_TYPE (subtype))
14158 && (!in_gimple_form
14159 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14161 tree type_domain;
14162 tree min_val = size_zero_node;
14163 sub = build_fold_indirect_ref_loc (loc, sub);
14164 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14165 if (type_domain && TYPE_MIN_VALUE (type_domain))
14166 min_val = TYPE_MIN_VALUE (type_domain);
14167 if (in_gimple_form
14168 && TREE_CODE (min_val) != INTEGER_CST)
14169 return NULL_TREE;
14170 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14171 NULL_TREE);
14174 return NULL_TREE;
14177 /* Builds an expression for an indirection through T, simplifying some
14178 cases. */
14180 tree
14181 build_fold_indirect_ref_loc (location_t loc, tree t)
14183 tree type = TREE_TYPE (TREE_TYPE (t));
14184 tree sub = fold_indirect_ref_1 (loc, type, t);
14186 if (sub)
14187 return sub;
14189 return build1_loc (loc, INDIRECT_REF, type, t);
14192 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14194 tree
14195 fold_indirect_ref_loc (location_t loc, tree t)
14197 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14199 if (sub)
14200 return sub;
14201 else
14202 return t;
14205 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14206 whose result is ignored. The type of the returned tree need not be
14207 the same as the original expression. */
14209 tree
14210 fold_ignored_result (tree t)
14212 if (!TREE_SIDE_EFFECTS (t))
14213 return integer_zero_node;
14215 for (;;)
14216 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14218 case tcc_unary:
14219 t = TREE_OPERAND (t, 0);
14220 break;
14222 case tcc_binary:
14223 case tcc_comparison:
14224 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14225 t = TREE_OPERAND (t, 0);
14226 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14227 t = TREE_OPERAND (t, 1);
14228 else
14229 return t;
14230 break;
14232 case tcc_expression:
14233 switch (TREE_CODE (t))
14235 case COMPOUND_EXPR:
14236 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14237 return t;
14238 t = TREE_OPERAND (t, 0);
14239 break;
14241 case COND_EXPR:
14242 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14243 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14244 return t;
14245 t = TREE_OPERAND (t, 0);
14246 break;
14248 default:
14249 return t;
14251 break;
14253 default:
14254 return t;
14258 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14260 tree
14261 round_up_loc (location_t loc, tree value, unsigned int divisor)
14263 tree div = NULL_TREE;
14265 if (divisor == 1)
14266 return value;
14268 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14269 have to do anything. Only do this when we are not given a const,
14270 because in that case, this check is more expensive than just
14271 doing it. */
14272 if (TREE_CODE (value) != INTEGER_CST)
14274 div = build_int_cst (TREE_TYPE (value), divisor);
14276 if (multiple_of_p (TREE_TYPE (value), value, div))
14277 return value;
14280 /* If divisor is a power of two, simplify this to bit manipulation. */
14281 if (pow2_or_zerop (divisor))
14283 if (TREE_CODE (value) == INTEGER_CST)
14285 wide_int val = wi::to_wide (value);
14286 bool overflow_p;
14288 if ((val & (divisor - 1)) == 0)
14289 return value;
14291 overflow_p = TREE_OVERFLOW (value);
14292 val += divisor - 1;
14293 val &= (int) -divisor;
14294 if (val == 0)
14295 overflow_p = true;
14297 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14299 else
14301 tree t;
14303 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14304 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14305 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14306 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14309 else
14311 if (!div)
14312 div = build_int_cst (TREE_TYPE (value), divisor);
14313 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14314 value = size_binop_loc (loc, MULT_EXPR, value, div);
14317 return value;
14320 /* Likewise, but round down. */
14322 tree
14323 round_down_loc (location_t loc, tree value, int divisor)
14325 tree div = NULL_TREE;
14327 gcc_assert (divisor > 0);
14328 if (divisor == 1)
14329 return value;
14331 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14332 have to do anything. Only do this when we are not given a const,
14333 because in that case, this check is more expensive than just
14334 doing it. */
14335 if (TREE_CODE (value) != INTEGER_CST)
14337 div = build_int_cst (TREE_TYPE (value), divisor);
14339 if (multiple_of_p (TREE_TYPE (value), value, div))
14340 return value;
14343 /* If divisor is a power of two, simplify this to bit manipulation. */
14344 if (pow2_or_zerop (divisor))
14346 tree t;
14348 t = build_int_cst (TREE_TYPE (value), -divisor);
14349 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14351 else
14353 if (!div)
14354 div = build_int_cst (TREE_TYPE (value), divisor);
14355 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14356 value = size_binop_loc (loc, MULT_EXPR, value, div);
14359 return value;
14362 /* Returns the pointer to the base of the object addressed by EXP and
14363 extracts the information about the offset of the access, storing it
14364 to PBITPOS and POFFSET. */
14366 static tree
14367 split_address_to_core_and_offset (tree exp,
14368 poly_int64_pod *pbitpos, tree *poffset)
14370 tree core;
14371 machine_mode mode;
14372 int unsignedp, reversep, volatilep;
14373 poly_int64 bitsize;
14374 location_t loc = EXPR_LOCATION (exp);
14376 if (TREE_CODE (exp) == ADDR_EXPR)
14378 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14379 poffset, &mode, &unsignedp, &reversep,
14380 &volatilep);
14381 core = build_fold_addr_expr_loc (loc, core);
14383 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14385 core = TREE_OPERAND (exp, 0);
14386 STRIP_NOPS (core);
14387 *pbitpos = 0;
14388 *poffset = TREE_OPERAND (exp, 1);
14389 if (poly_int_tree_p (*poffset))
14391 poly_offset_int tem
14392 = wi::sext (wi::to_poly_offset (*poffset),
14393 TYPE_PRECISION (TREE_TYPE (*poffset)));
14394 tem <<= LOG2_BITS_PER_UNIT;
14395 if (tem.to_shwi (pbitpos))
14396 *poffset = NULL_TREE;
14399 else
14401 core = exp;
14402 *pbitpos = 0;
14403 *poffset = NULL_TREE;
14406 return core;
14409 /* Returns true if addresses of E1 and E2 differ by a constant, false
14410 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14412 bool
14413 ptr_difference_const (tree e1, tree e2, poly_int64_pod *diff)
14415 tree core1, core2;
14416 poly_int64 bitpos1, bitpos2;
14417 tree toffset1, toffset2, tdiff, type;
14419 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14420 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14422 poly_int64 bytepos1, bytepos2;
14423 if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
14424 || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
14425 || !operand_equal_p (core1, core2, 0))
14426 return false;
14428 if (toffset1 && toffset2)
14430 type = TREE_TYPE (toffset1);
14431 if (type != TREE_TYPE (toffset2))
14432 toffset2 = fold_convert (type, toffset2);
14434 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14435 if (!cst_and_fits_in_hwi (tdiff))
14436 return false;
14438 *diff = int_cst_value (tdiff);
14440 else if (toffset1 || toffset2)
14442 /* If only one of the offsets is non-constant, the difference cannot
14443 be a constant. */
14444 return false;
14446 else
14447 *diff = 0;
14449 *diff += bytepos1 - bytepos2;
14450 return true;
14453 /* Return OFF converted to a pointer offset type suitable as offset for
14454 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14455 tree
14456 convert_to_ptrofftype_loc (location_t loc, tree off)
14458 return fold_convert_loc (loc, sizetype, off);
14461 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14462 tree
14463 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14465 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14466 ptr, convert_to_ptrofftype_loc (loc, off));
14469 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14470 tree
14471 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14473 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14474 ptr, size_int (off));
14477 /* Return a char pointer for a C string if it is a string constant
14478 or sum of string constant and integer constant. We only support
14479 string constants properly terminated with '\0' character.
14480 If STRLEN is a valid pointer, length (including terminating character)
14481 of returned string is stored to the argument. */
14483 const char *
14484 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14486 tree offset_node;
14488 if (strlen)
14489 *strlen = 0;
14491 src = string_constant (src, &offset_node);
14492 if (src == 0)
14493 return NULL;
14495 unsigned HOST_WIDE_INT offset = 0;
14496 if (offset_node != NULL_TREE)
14498 if (!tree_fits_uhwi_p (offset_node))
14499 return NULL;
14500 else
14501 offset = tree_to_uhwi (offset_node);
14504 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14505 const char *string = TREE_STRING_POINTER (src);
14507 /* Support only properly null-terminated strings. */
14508 if (string_length == 0
14509 || string[string_length - 1] != '\0'
14510 || offset >= string_length)
14511 return NULL;
14513 if (strlen)
14514 *strlen = string_length - offset;
14515 return string + offset;
14518 #if CHECKING_P
14520 namespace selftest {
14522 /* Helper functions for writing tests of folding trees. */
14524 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14526 static void
14527 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14528 tree constant)
14530 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14533 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14534 wrapping WRAPPED_EXPR. */
14536 static void
14537 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14538 tree wrapped_expr)
14540 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14541 ASSERT_NE (wrapped_expr, result);
14542 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14543 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14546 /* Verify that various arithmetic binary operations are folded
14547 correctly. */
14549 static void
14550 test_arithmetic_folding ()
14552 tree type = integer_type_node;
14553 tree x = create_tmp_var_raw (type, "x");
14554 tree zero = build_zero_cst (type);
14555 tree one = build_int_cst (type, 1);
14557 /* Addition. */
14558 /* 1 <-- (0 + 1) */
14559 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14560 one);
14561 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14562 one);
14564 /* (nonlvalue)x <-- (x + 0) */
14565 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14568 /* Subtraction. */
14569 /* 0 <-- (x - x) */
14570 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14571 zero);
14572 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14575 /* Multiplication. */
14576 /* 0 <-- (x * 0) */
14577 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14578 zero);
14580 /* (nonlvalue)x <-- (x * 1) */
14581 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14585 /* Verify that various binary operations on vectors are folded
14586 correctly. */
14588 static void
14589 test_vector_folding ()
14591 tree inner_type = integer_type_node;
14592 tree type = build_vector_type (inner_type, 4);
14593 tree zero = build_zero_cst (type);
14594 tree one = build_one_cst (type);
14596 /* Verify equality tests that return a scalar boolean result. */
14597 tree res_type = boolean_type_node;
14598 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14599 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14600 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14601 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14604 /* Verify folding of VEC_DUPLICATE_EXPRs. */
14606 static void
14607 test_vec_duplicate_folding ()
14609 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
14610 machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
14611 /* This will be 1 if VEC_MODE isn't a vector mode. */
14612 unsigned int nunits = GET_MODE_NUNITS (vec_mode);
14614 tree type = build_vector_type (ssizetype, nunits);
14615 tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
14616 tree dup5_cst = build_vector_from_val (type, ssize_int (5));
14617 ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
14620 /* Run all of the selftests within this file. */
14622 void
14623 fold_const_c_tests ()
14625 test_arithmetic_folding ();
14626 test_vector_folding ();
14627 test_vec_duplicate_folding ();
14630 } // namespace selftest
14632 #endif /* CHECKING_P */