Transform switch_conversion into a class.
[official-gcc.git] / gcc / fold-const.c
blob4568e1e2b5744cf7a2f8895422cafa5a0a509bba
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /*@@ This file should be rewritten to use an arbitrary precision
21 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
22 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
23 @@ The routines that translate from the ap rep should
24 @@ warn if precision et. al. is lost.
25 @@ This would also make life easier when this technology is used
26 @@ for cross-compilers. */
28 /* The entry points in this file are fold, size_int_wide and size_binop.
30 fold takes a tree as argument and returns a simplified tree.
32 size_binop takes a tree code for an arithmetic operation
33 and two operands that are trees, and produces a tree for the
34 result, assuming the type comes from `sizetype'.
36 size_int takes an integer value, and creates a tree constant
37 with type from `sizetype'.
39 Note: Since the folders get called on non-gimple code as well as
40 gimple code, we need to handle GIMPLE tuples as well as their
41 corresponding tree equivalents. */
43 #include "config.h"
44 #include "system.h"
45 #include "coretypes.h"
46 #include "backend.h"
47 #include "target.h"
48 #include "rtl.h"
49 #include "tree.h"
50 #include "gimple.h"
51 #include "predict.h"
52 #include "memmodel.h"
53 #include "tm_p.h"
54 #include "tree-ssa-operands.h"
55 #include "optabs-query.h"
56 #include "cgraph.h"
57 #include "diagnostic-core.h"
58 #include "flags.h"
59 #include "alias.h"
60 #include "fold-const.h"
61 #include "fold-const-call.h"
62 #include "stor-layout.h"
63 #include "calls.h"
64 #include "tree-iterator.h"
65 #include "expr.h"
66 #include "intl.h"
67 #include "langhooks.h"
68 #include "tree-eh.h"
69 #include "gimplify.h"
70 #include "tree-dfa.h"
71 #include "builtins.h"
72 #include "generic-match.h"
73 #include "gimple-fold.h"
74 #include "params.h"
75 #include "tree-into-ssa.h"
76 #include "md5.h"
77 #include "case-cfn-macros.h"
78 #include "stringpool.h"
79 #include "tree-vrp.h"
80 #include "tree-ssanames.h"
81 #include "selftest.h"
82 #include "stringpool.h"
83 #include "attribs.h"
84 #include "tree-vector-builder.h"
85 #include "vec-perm-indices.h"
87 /* Nonzero if we are folding constants inside an initializer; zero
88 otherwise. */
89 int folding_initializer = 0;
91 /* The following constants represent a bit based encoding of GCC's
92 comparison operators. This encoding simplifies transformations
93 on relational comparison operators, such as AND and OR. */
94 enum comparison_code {
95 COMPCODE_FALSE = 0,
96 COMPCODE_LT = 1,
97 COMPCODE_EQ = 2,
98 COMPCODE_LE = 3,
99 COMPCODE_GT = 4,
100 COMPCODE_LTGT = 5,
101 COMPCODE_GE = 6,
102 COMPCODE_ORD = 7,
103 COMPCODE_UNORD = 8,
104 COMPCODE_UNLT = 9,
105 COMPCODE_UNEQ = 10,
106 COMPCODE_UNLE = 11,
107 COMPCODE_UNGT = 12,
108 COMPCODE_NE = 13,
109 COMPCODE_UNGE = 14,
110 COMPCODE_TRUE = 15
113 static bool negate_expr_p (tree);
114 static tree negate_expr (tree);
115 static tree associate_trees (location_t, tree, tree, enum tree_code, tree);
116 static enum comparison_code comparison_to_compcode (enum tree_code);
117 static enum tree_code compcode_to_comparison (enum comparison_code);
118 static int twoval_comparison_p (tree, tree *, tree *);
119 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
120 static tree optimize_bit_field_compare (location_t, enum tree_code,
121 tree, tree, tree);
122 static int simple_operand_p (const_tree);
123 static bool simple_operand_p_2 (tree);
124 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
125 static tree range_predecessor (tree);
126 static tree range_successor (tree);
127 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
128 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
129 static tree unextend (tree, int, int, tree);
130 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
131 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
132 static tree fold_binary_op_with_conditional_arg (location_t,
133 enum tree_code, tree,
134 tree, tree,
135 tree, tree, int);
136 static tree fold_negate_const (tree, tree);
137 static tree fold_not_const (const_tree, tree);
138 static tree fold_relational_const (enum tree_code, tree, tree, tree);
139 static tree fold_convert_const (enum tree_code, tree, tree);
140 static tree fold_view_convert_expr (tree, tree);
141 static tree fold_negate_expr (location_t, tree);
144 /* Return EXPR_LOCATION of T if it is not UNKNOWN_LOCATION.
145 Otherwise, return LOC. */
147 static location_t
148 expr_location_or (tree t, location_t loc)
150 location_t tloc = EXPR_LOCATION (t);
151 return tloc == UNKNOWN_LOCATION ? loc : tloc;
154 /* Similar to protected_set_expr_location, but never modify x in place,
155 if location can and needs to be set, unshare it. */
157 static inline tree
158 protected_set_expr_location_unshare (tree x, location_t loc)
160 if (CAN_HAVE_LOCATION_P (x)
161 && EXPR_LOCATION (x) != loc
162 && !(TREE_CODE (x) == SAVE_EXPR
163 || TREE_CODE (x) == TARGET_EXPR
164 || TREE_CODE (x) == BIND_EXPR))
166 x = copy_node (x);
167 SET_EXPR_LOCATION (x, loc);
169 return x;
172 /* If ARG2 divides ARG1 with zero remainder, carries out the exact
173 division and returns the quotient. Otherwise returns
174 NULL_TREE. */
176 tree
177 div_if_zero_remainder (const_tree arg1, const_tree arg2)
179 widest_int quo;
181 if (wi::multiple_of_p (wi::to_widest (arg1), wi::to_widest (arg2),
182 SIGNED, &quo))
183 return wide_int_to_tree (TREE_TYPE (arg1), quo);
185 return NULL_TREE;
188 /* This is nonzero if we should defer warnings about undefined
189 overflow. This facility exists because these warnings are a
190 special case. The code to estimate loop iterations does not want
191 to issue any warnings, since it works with expressions which do not
192 occur in user code. Various bits of cleanup code call fold(), but
193 only use the result if it has certain characteristics (e.g., is a
194 constant); that code only wants to issue a warning if the result is
195 used. */
197 static int fold_deferring_overflow_warnings;
199 /* If a warning about undefined overflow is deferred, this is the
200 warning. Note that this may cause us to turn two warnings into
201 one, but that is fine since it is sufficient to only give one
202 warning per expression. */
204 static const char* fold_deferred_overflow_warning;
206 /* If a warning about undefined overflow is deferred, this is the
207 level at which the warning should be emitted. */
209 static enum warn_strict_overflow_code fold_deferred_overflow_code;
211 /* Start deferring overflow warnings. We could use a stack here to
212 permit nested calls, but at present it is not necessary. */
214 void
215 fold_defer_overflow_warnings (void)
217 ++fold_deferring_overflow_warnings;
220 /* Stop deferring overflow warnings. If there is a pending warning,
221 and ISSUE is true, then issue the warning if appropriate. STMT is
222 the statement with which the warning should be associated (used for
223 location information); STMT may be NULL. CODE is the level of the
224 warning--a warn_strict_overflow_code value. This function will use
225 the smaller of CODE and the deferred code when deciding whether to
226 issue the warning. CODE may be zero to mean to always use the
227 deferred code. */
229 void
230 fold_undefer_overflow_warnings (bool issue, const gimple *stmt, int code)
232 const char *warnmsg;
233 location_t locus;
235 gcc_assert (fold_deferring_overflow_warnings > 0);
236 --fold_deferring_overflow_warnings;
237 if (fold_deferring_overflow_warnings > 0)
239 if (fold_deferred_overflow_warning != NULL
240 && code != 0
241 && code < (int) fold_deferred_overflow_code)
242 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
243 return;
246 warnmsg = fold_deferred_overflow_warning;
247 fold_deferred_overflow_warning = NULL;
249 if (!issue || warnmsg == NULL)
250 return;
252 if (gimple_no_warning_p (stmt))
253 return;
255 /* Use the smallest code level when deciding to issue the
256 warning. */
257 if (code == 0 || code > (int) fold_deferred_overflow_code)
258 code = fold_deferred_overflow_code;
260 if (!issue_strict_overflow_warning (code))
261 return;
263 if (stmt == NULL)
264 locus = input_location;
265 else
266 locus = gimple_location (stmt);
267 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
270 /* Stop deferring overflow warnings, ignoring any deferred
271 warnings. */
273 void
274 fold_undefer_and_ignore_overflow_warnings (void)
276 fold_undefer_overflow_warnings (false, NULL, 0);
279 /* Whether we are deferring overflow warnings. */
281 bool
282 fold_deferring_overflow_warnings_p (void)
284 return fold_deferring_overflow_warnings > 0;
287 /* This is called when we fold something based on the fact that signed
288 overflow is undefined. */
290 void
291 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
293 if (fold_deferring_overflow_warnings > 0)
295 if (fold_deferred_overflow_warning == NULL
296 || wc < fold_deferred_overflow_code)
298 fold_deferred_overflow_warning = gmsgid;
299 fold_deferred_overflow_code = wc;
302 else if (issue_strict_overflow_warning (wc))
303 warning (OPT_Wstrict_overflow, gmsgid);
306 /* Return true if the built-in mathematical function specified by CODE
307 is odd, i.e. -f(x) == f(-x). */
309 bool
310 negate_mathfn_p (combined_fn fn)
312 switch (fn)
314 CASE_CFN_ASIN:
315 CASE_CFN_ASINH:
316 CASE_CFN_ATAN:
317 CASE_CFN_ATANH:
318 CASE_CFN_CASIN:
319 CASE_CFN_CASINH:
320 CASE_CFN_CATAN:
321 CASE_CFN_CATANH:
322 CASE_CFN_CBRT:
323 CASE_CFN_CPROJ:
324 CASE_CFN_CSIN:
325 CASE_CFN_CSINH:
326 CASE_CFN_CTAN:
327 CASE_CFN_CTANH:
328 CASE_CFN_ERF:
329 CASE_CFN_LLROUND:
330 CASE_CFN_LROUND:
331 CASE_CFN_ROUND:
332 CASE_CFN_SIN:
333 CASE_CFN_SINH:
334 CASE_CFN_TAN:
335 CASE_CFN_TANH:
336 CASE_CFN_TRUNC:
337 return true;
339 CASE_CFN_LLRINT:
340 CASE_CFN_LRINT:
341 CASE_CFN_NEARBYINT:
342 CASE_CFN_RINT:
343 return !flag_rounding_math;
345 default:
346 break;
348 return false;
351 /* Check whether we may negate an integer constant T without causing
352 overflow. */
354 bool
355 may_negate_without_overflow_p (const_tree t)
357 tree type;
359 gcc_assert (TREE_CODE (t) == INTEGER_CST);
361 type = TREE_TYPE (t);
362 if (TYPE_UNSIGNED (type))
363 return false;
365 return !wi::only_sign_bit_p (wi::to_wide (t));
368 /* Determine whether an expression T can be cheaply negated using
369 the function negate_expr without introducing undefined overflow. */
371 static bool
372 negate_expr_p (tree t)
374 tree type;
376 if (t == 0)
377 return false;
379 type = TREE_TYPE (t);
381 STRIP_SIGN_NOPS (t);
382 switch (TREE_CODE (t))
384 case INTEGER_CST:
385 if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
386 return true;
388 /* Check that -CST will not overflow type. */
389 return may_negate_without_overflow_p (t);
390 case BIT_NOT_EXPR:
391 return (INTEGRAL_TYPE_P (type)
392 && TYPE_OVERFLOW_WRAPS (type));
394 case FIXED_CST:
395 return true;
397 case NEGATE_EXPR:
398 return !TYPE_OVERFLOW_SANITIZED (type);
400 case REAL_CST:
401 /* We want to canonicalize to positive real constants. Pretend
402 that only negative ones can be easily negated. */
403 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
405 case COMPLEX_CST:
406 return negate_expr_p (TREE_REALPART (t))
407 && negate_expr_p (TREE_IMAGPART (t));
409 case VECTOR_CST:
411 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
412 return true;
414 /* Steps don't prevent negation. */
415 unsigned int count = vector_cst_encoded_nelts (t);
416 for (unsigned int i = 0; i < count; ++i)
417 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
418 return false;
420 return true;
423 case COMPLEX_EXPR:
424 return negate_expr_p (TREE_OPERAND (t, 0))
425 && negate_expr_p (TREE_OPERAND (t, 1));
427 case CONJ_EXPR:
428 return negate_expr_p (TREE_OPERAND (t, 0));
430 case PLUS_EXPR:
431 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
432 || HONOR_SIGNED_ZEROS (element_mode (type))
433 || (ANY_INTEGRAL_TYPE_P (type)
434 && ! TYPE_OVERFLOW_WRAPS (type)))
435 return false;
436 /* -(A + B) -> (-B) - A. */
437 if (negate_expr_p (TREE_OPERAND (t, 1)))
438 return true;
439 /* -(A + B) -> (-A) - B. */
440 return negate_expr_p (TREE_OPERAND (t, 0));
442 case MINUS_EXPR:
443 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
444 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
445 && !HONOR_SIGNED_ZEROS (element_mode (type))
446 && (! ANY_INTEGRAL_TYPE_P (type)
447 || TYPE_OVERFLOW_WRAPS (type));
449 case MULT_EXPR:
450 if (TYPE_UNSIGNED (type))
451 break;
452 /* INT_MIN/n * n doesn't overflow while negating one operand it does
453 if n is a (negative) power of two. */
454 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
455 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
456 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
457 && (wi::popcount
458 (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
459 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
460 && (wi::popcount
461 (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
462 break;
464 /* Fall through. */
466 case RDIV_EXPR:
467 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
468 return negate_expr_p (TREE_OPERAND (t, 1))
469 || negate_expr_p (TREE_OPERAND (t, 0));
470 break;
472 case TRUNC_DIV_EXPR:
473 case ROUND_DIV_EXPR:
474 case EXACT_DIV_EXPR:
475 if (TYPE_UNSIGNED (type))
476 break;
477 /* In general we can't negate A in A / B, because if A is INT_MIN and
478 B is not 1 we change the sign of the result. */
479 if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
480 && negate_expr_p (TREE_OPERAND (t, 0)))
481 return true;
482 /* In general we can't negate B in A / B, because if A is INT_MIN and
483 B is 1, we may turn this into INT_MIN / -1 which is undefined
484 and actually traps on some architectures. */
485 if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
486 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
487 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
488 && ! integer_onep (TREE_OPERAND (t, 1))))
489 return negate_expr_p (TREE_OPERAND (t, 1));
490 break;
492 case NOP_EXPR:
493 /* Negate -((double)float) as (double)(-float). */
494 if (TREE_CODE (type) == REAL_TYPE)
496 tree tem = strip_float_extensions (t);
497 if (tem != t)
498 return negate_expr_p (tem);
500 break;
502 case CALL_EXPR:
503 /* Negate -f(x) as f(-x). */
504 if (negate_mathfn_p (get_call_combined_fn (t)))
505 return negate_expr_p (CALL_EXPR_ARG (t, 0));
506 break;
508 case RSHIFT_EXPR:
509 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
510 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
512 tree op1 = TREE_OPERAND (t, 1);
513 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
514 return true;
516 break;
518 default:
519 break;
521 return false;
524 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
525 simplification is possible.
526 If negate_expr_p would return true for T, NULL_TREE will never be
527 returned. */
529 static tree
530 fold_negate_expr_1 (location_t loc, tree t)
532 tree type = TREE_TYPE (t);
533 tree tem;
535 switch (TREE_CODE (t))
537 /* Convert - (~A) to A + 1. */
538 case BIT_NOT_EXPR:
539 if (INTEGRAL_TYPE_P (type))
540 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
541 build_one_cst (type));
542 break;
544 case INTEGER_CST:
545 tem = fold_negate_const (t, type);
546 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
547 || (ANY_INTEGRAL_TYPE_P (type)
548 && !TYPE_OVERFLOW_TRAPS (type)
549 && TYPE_OVERFLOW_WRAPS (type))
550 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
551 return tem;
552 break;
554 case POLY_INT_CST:
555 case REAL_CST:
556 case FIXED_CST:
557 tem = fold_negate_const (t, type);
558 return tem;
560 case COMPLEX_CST:
562 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
563 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
564 if (rpart && ipart)
565 return build_complex (type, rpart, ipart);
567 break;
569 case VECTOR_CST:
571 tree_vector_builder elts;
572 elts.new_unary_operation (type, t, true);
573 unsigned int count = elts.encoded_nelts ();
574 for (unsigned int i = 0; i < count; ++i)
576 tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
577 if (elt == NULL_TREE)
578 return NULL_TREE;
579 elts.quick_push (elt);
582 return elts.build ();
585 case COMPLEX_EXPR:
586 if (negate_expr_p (t))
587 return fold_build2_loc (loc, COMPLEX_EXPR, type,
588 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
589 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
590 break;
592 case CONJ_EXPR:
593 if (negate_expr_p (t))
594 return fold_build1_loc (loc, CONJ_EXPR, type,
595 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
596 break;
598 case NEGATE_EXPR:
599 if (!TYPE_OVERFLOW_SANITIZED (type))
600 return TREE_OPERAND (t, 0);
601 break;
603 case PLUS_EXPR:
604 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
605 && !HONOR_SIGNED_ZEROS (element_mode (type)))
607 /* -(A + B) -> (-B) - A. */
608 if (negate_expr_p (TREE_OPERAND (t, 1)))
610 tem = negate_expr (TREE_OPERAND (t, 1));
611 return fold_build2_loc (loc, MINUS_EXPR, type,
612 tem, TREE_OPERAND (t, 0));
615 /* -(A + B) -> (-A) - B. */
616 if (negate_expr_p (TREE_OPERAND (t, 0)))
618 tem = negate_expr (TREE_OPERAND (t, 0));
619 return fold_build2_loc (loc, MINUS_EXPR, type,
620 tem, TREE_OPERAND (t, 1));
623 break;
625 case MINUS_EXPR:
626 /* - (A - B) -> B - A */
627 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
628 && !HONOR_SIGNED_ZEROS (element_mode (type)))
629 return fold_build2_loc (loc, MINUS_EXPR, type,
630 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
631 break;
633 case MULT_EXPR:
634 if (TYPE_UNSIGNED (type))
635 break;
637 /* Fall through. */
639 case RDIV_EXPR:
640 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
642 tem = TREE_OPERAND (t, 1);
643 if (negate_expr_p (tem))
644 return fold_build2_loc (loc, TREE_CODE (t), type,
645 TREE_OPERAND (t, 0), negate_expr (tem));
646 tem = TREE_OPERAND (t, 0);
647 if (negate_expr_p (tem))
648 return fold_build2_loc (loc, TREE_CODE (t), type,
649 negate_expr (tem), TREE_OPERAND (t, 1));
651 break;
653 case TRUNC_DIV_EXPR:
654 case ROUND_DIV_EXPR:
655 case EXACT_DIV_EXPR:
656 if (TYPE_UNSIGNED (type))
657 break;
658 /* In general we can't negate A in A / B, because if A is INT_MIN and
659 B is not 1 we change the sign of the result. */
660 if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
661 && negate_expr_p (TREE_OPERAND (t, 0)))
662 return fold_build2_loc (loc, TREE_CODE (t), type,
663 negate_expr (TREE_OPERAND (t, 0)),
664 TREE_OPERAND (t, 1));
665 /* In general we can't negate B in A / B, because if A is INT_MIN and
666 B is 1, we may turn this into INT_MIN / -1 which is undefined
667 and actually traps on some architectures. */
668 if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
669 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
670 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
671 && ! integer_onep (TREE_OPERAND (t, 1))))
672 && negate_expr_p (TREE_OPERAND (t, 1)))
673 return fold_build2_loc (loc, TREE_CODE (t), type,
674 TREE_OPERAND (t, 0),
675 negate_expr (TREE_OPERAND (t, 1)));
676 break;
678 case NOP_EXPR:
679 /* Convert -((double)float) into (double)(-float). */
680 if (TREE_CODE (type) == REAL_TYPE)
682 tem = strip_float_extensions (t);
683 if (tem != t && negate_expr_p (tem))
684 return fold_convert_loc (loc, type, negate_expr (tem));
686 break;
688 case CALL_EXPR:
689 /* Negate -f(x) as f(-x). */
690 if (negate_mathfn_p (get_call_combined_fn (t))
691 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
693 tree fndecl, arg;
695 fndecl = get_callee_fndecl (t);
696 arg = negate_expr (CALL_EXPR_ARG (t, 0));
697 return build_call_expr_loc (loc, fndecl, 1, arg);
699 break;
701 case RSHIFT_EXPR:
702 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
703 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
705 tree op1 = TREE_OPERAND (t, 1);
706 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
708 tree ntype = TYPE_UNSIGNED (type)
709 ? signed_type_for (type)
710 : unsigned_type_for (type);
711 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
712 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
713 return fold_convert_loc (loc, type, temp);
716 break;
718 default:
719 break;
722 return NULL_TREE;
725 /* A wrapper for fold_negate_expr_1. */
727 static tree
728 fold_negate_expr (location_t loc, tree t)
730 tree type = TREE_TYPE (t);
731 STRIP_SIGN_NOPS (t);
732 tree tem = fold_negate_expr_1 (loc, t);
733 if (tem == NULL_TREE)
734 return NULL_TREE;
735 return fold_convert_loc (loc, type, tem);
738 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
739 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
740 return NULL_TREE. */
742 static tree
743 negate_expr (tree t)
745 tree type, tem;
746 location_t loc;
748 if (t == NULL_TREE)
749 return NULL_TREE;
751 loc = EXPR_LOCATION (t);
752 type = TREE_TYPE (t);
753 STRIP_SIGN_NOPS (t);
755 tem = fold_negate_expr (loc, t);
756 if (!tem)
757 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
758 return fold_convert_loc (loc, type, tem);
761 /* Split a tree IN into a constant, literal and variable parts that could be
762 combined with CODE to make IN. "constant" means an expression with
763 TREE_CONSTANT but that isn't an actual constant. CODE must be a
764 commutative arithmetic operation. Store the constant part into *CONP,
765 the literal in *LITP and return the variable part. If a part isn't
766 present, set it to null. If the tree does not decompose in this way,
767 return the entire tree as the variable part and the other parts as null.
769 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
770 case, we negate an operand that was subtracted. Except if it is a
771 literal for which we use *MINUS_LITP instead.
773 If NEGATE_P is true, we are negating all of IN, again except a literal
774 for which we use *MINUS_LITP instead. If a variable part is of pointer
775 type, it is negated after converting to TYPE. This prevents us from
776 generating illegal MINUS pointer expression. LOC is the location of
777 the converted variable part.
779 If IN is itself a literal or constant, return it as appropriate.
781 Note that we do not guarantee that any of the three values will be the
782 same type as IN, but they will have the same signedness and mode. */
784 static tree
785 split_tree (tree in, tree type, enum tree_code code,
786 tree *minus_varp, tree *conp, tree *minus_conp,
787 tree *litp, tree *minus_litp, int negate_p)
789 tree var = 0;
790 *minus_varp = 0;
791 *conp = 0;
792 *minus_conp = 0;
793 *litp = 0;
794 *minus_litp = 0;
796 /* Strip any conversions that don't change the machine mode or signedness. */
797 STRIP_SIGN_NOPS (in);
799 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
800 || TREE_CODE (in) == FIXED_CST)
801 *litp = in;
802 else if (TREE_CODE (in) == code
803 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
804 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
805 /* We can associate addition and subtraction together (even
806 though the C standard doesn't say so) for integers because
807 the value is not affected. For reals, the value might be
808 affected, so we can't. */
809 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
810 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
811 || (code == MINUS_EXPR
812 && (TREE_CODE (in) == PLUS_EXPR
813 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
815 tree op0 = TREE_OPERAND (in, 0);
816 tree op1 = TREE_OPERAND (in, 1);
817 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
818 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
820 /* First see if either of the operands is a literal, then a constant. */
821 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
822 || TREE_CODE (op0) == FIXED_CST)
823 *litp = op0, op0 = 0;
824 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
825 || TREE_CODE (op1) == FIXED_CST)
826 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
828 if (op0 != 0 && TREE_CONSTANT (op0))
829 *conp = op0, op0 = 0;
830 else if (op1 != 0 && TREE_CONSTANT (op1))
831 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
833 /* If we haven't dealt with either operand, this is not a case we can
834 decompose. Otherwise, VAR is either of the ones remaining, if any. */
835 if (op0 != 0 && op1 != 0)
836 var = in;
837 else if (op0 != 0)
838 var = op0;
839 else
840 var = op1, neg_var_p = neg1_p;
842 /* Now do any needed negations. */
843 if (neg_litp_p)
844 *minus_litp = *litp, *litp = 0;
845 if (neg_conp_p && *conp)
846 *minus_conp = *conp, *conp = 0;
847 if (neg_var_p && var)
848 *minus_varp = var, var = 0;
850 else if (TREE_CONSTANT (in))
851 *conp = in;
852 else if (TREE_CODE (in) == BIT_NOT_EXPR
853 && code == PLUS_EXPR)
855 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
856 when IN is constant. */
857 *litp = build_minus_one_cst (type);
858 *minus_varp = TREE_OPERAND (in, 0);
860 else
861 var = in;
863 if (negate_p)
865 if (*litp)
866 *minus_litp = *litp, *litp = 0;
867 else if (*minus_litp)
868 *litp = *minus_litp, *minus_litp = 0;
869 if (*conp)
870 *minus_conp = *conp, *conp = 0;
871 else if (*minus_conp)
872 *conp = *minus_conp, *minus_conp = 0;
873 if (var)
874 *minus_varp = var, var = 0;
875 else if (*minus_varp)
876 var = *minus_varp, *minus_varp = 0;
879 if (*litp
880 && TREE_OVERFLOW_P (*litp))
881 *litp = drop_tree_overflow (*litp);
882 if (*minus_litp
883 && TREE_OVERFLOW_P (*minus_litp))
884 *minus_litp = drop_tree_overflow (*minus_litp);
886 return var;
889 /* Re-associate trees split by the above function. T1 and T2 are
890 either expressions to associate or null. Return the new
891 expression, if any. LOC is the location of the new expression. If
892 we build an operation, do it in TYPE and with CODE. */
894 static tree
895 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
897 if (t1 == 0)
899 gcc_assert (t2 == 0 || code != MINUS_EXPR);
900 return t2;
902 else if (t2 == 0)
903 return t1;
905 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
906 try to fold this since we will have infinite recursion. But do
907 deal with any NEGATE_EXPRs. */
908 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
909 || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
910 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
912 if (code == PLUS_EXPR)
914 if (TREE_CODE (t1) == NEGATE_EXPR)
915 return build2_loc (loc, MINUS_EXPR, type,
916 fold_convert_loc (loc, type, t2),
917 fold_convert_loc (loc, type,
918 TREE_OPERAND (t1, 0)));
919 else if (TREE_CODE (t2) == NEGATE_EXPR)
920 return build2_loc (loc, MINUS_EXPR, type,
921 fold_convert_loc (loc, type, t1),
922 fold_convert_loc (loc, type,
923 TREE_OPERAND (t2, 0)));
924 else if (integer_zerop (t2))
925 return fold_convert_loc (loc, type, t1);
927 else if (code == MINUS_EXPR)
929 if (integer_zerop (t2))
930 return fold_convert_loc (loc, type, t1);
933 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
934 fold_convert_loc (loc, type, t2));
937 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
938 fold_convert_loc (loc, type, t2));
941 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
942 for use in int_const_binop, size_binop and size_diffop. */
944 static bool
945 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
947 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
948 return false;
949 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
950 return false;
952 switch (code)
954 case LSHIFT_EXPR:
955 case RSHIFT_EXPR:
956 case LROTATE_EXPR:
957 case RROTATE_EXPR:
958 return true;
960 default:
961 break;
964 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
965 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
966 && TYPE_MODE (type1) == TYPE_MODE (type2);
969 /* Subroutine of int_const_binop_1 that handles two INTEGER_CSTs. */
971 static tree
972 int_const_binop_2 (enum tree_code code, const_tree parg1, const_tree parg2,
973 int overflowable)
975 wide_int res;
976 tree t;
977 tree type = TREE_TYPE (parg1);
978 signop sign = TYPE_SIGN (type);
979 bool overflow = false;
981 wi::tree_to_wide_ref arg1 = wi::to_wide (parg1);
982 wide_int arg2 = wi::to_wide (parg2, TYPE_PRECISION (type));
984 switch (code)
986 case BIT_IOR_EXPR:
987 res = wi::bit_or (arg1, arg2);
988 break;
990 case BIT_XOR_EXPR:
991 res = wi::bit_xor (arg1, arg2);
992 break;
994 case BIT_AND_EXPR:
995 res = wi::bit_and (arg1, arg2);
996 break;
998 case RSHIFT_EXPR:
999 case LSHIFT_EXPR:
1000 if (wi::neg_p (arg2))
1002 arg2 = -arg2;
1003 if (code == RSHIFT_EXPR)
1004 code = LSHIFT_EXPR;
1005 else
1006 code = RSHIFT_EXPR;
1009 if (code == RSHIFT_EXPR)
1010 /* It's unclear from the C standard whether shifts can overflow.
1011 The following code ignores overflow; perhaps a C standard
1012 interpretation ruling is needed. */
1013 res = wi::rshift (arg1, arg2, sign);
1014 else
1015 res = wi::lshift (arg1, arg2);
1016 break;
1018 case RROTATE_EXPR:
1019 case LROTATE_EXPR:
1020 if (wi::neg_p (arg2))
1022 arg2 = -arg2;
1023 if (code == RROTATE_EXPR)
1024 code = LROTATE_EXPR;
1025 else
1026 code = RROTATE_EXPR;
1029 if (code == RROTATE_EXPR)
1030 res = wi::rrotate (arg1, arg2);
1031 else
1032 res = wi::lrotate (arg1, arg2);
1033 break;
1035 case PLUS_EXPR:
1036 res = wi::add (arg1, arg2, sign, &overflow);
1037 break;
1039 case MINUS_EXPR:
1040 res = wi::sub (arg1, arg2, sign, &overflow);
1041 break;
1043 case MULT_EXPR:
1044 res = wi::mul (arg1, arg2, sign, &overflow);
1045 break;
1047 case MULT_HIGHPART_EXPR:
1048 res = wi::mul_high (arg1, arg2, sign);
1049 break;
1051 case TRUNC_DIV_EXPR:
1052 case EXACT_DIV_EXPR:
1053 if (arg2 == 0)
1054 return NULL_TREE;
1055 res = wi::div_trunc (arg1, arg2, sign, &overflow);
1056 break;
1058 case FLOOR_DIV_EXPR:
1059 if (arg2 == 0)
1060 return NULL_TREE;
1061 res = wi::div_floor (arg1, arg2, sign, &overflow);
1062 break;
1064 case CEIL_DIV_EXPR:
1065 if (arg2 == 0)
1066 return NULL_TREE;
1067 res = wi::div_ceil (arg1, arg2, sign, &overflow);
1068 break;
1070 case ROUND_DIV_EXPR:
1071 if (arg2 == 0)
1072 return NULL_TREE;
1073 res = wi::div_round (arg1, arg2, sign, &overflow);
1074 break;
1076 case TRUNC_MOD_EXPR:
1077 if (arg2 == 0)
1078 return NULL_TREE;
1079 res = wi::mod_trunc (arg1, arg2, sign, &overflow);
1080 break;
1082 case FLOOR_MOD_EXPR:
1083 if (arg2 == 0)
1084 return NULL_TREE;
1085 res = wi::mod_floor (arg1, arg2, sign, &overflow);
1086 break;
1088 case CEIL_MOD_EXPR:
1089 if (arg2 == 0)
1090 return NULL_TREE;
1091 res = wi::mod_ceil (arg1, arg2, sign, &overflow);
1092 break;
1094 case ROUND_MOD_EXPR:
1095 if (arg2 == 0)
1096 return NULL_TREE;
1097 res = wi::mod_round (arg1, arg2, sign, &overflow);
1098 break;
1100 case MIN_EXPR:
1101 res = wi::min (arg1, arg2, sign);
1102 break;
1104 case MAX_EXPR:
1105 res = wi::max (arg1, arg2, sign);
1106 break;
1108 default:
1109 return NULL_TREE;
1112 t = force_fit_type (type, res, overflowable,
1113 (((sign == SIGNED || overflowable == -1)
1114 && overflow)
1115 | TREE_OVERFLOW (parg1) | TREE_OVERFLOW (parg2)));
1117 return t;
1120 /* Combine two integer constants PARG1 and PARG2 under operation CODE
1121 to produce a new constant. Return NULL_TREE if we don't know how
1122 to evaluate CODE at compile-time. */
1124 static tree
1125 int_const_binop_1 (enum tree_code code, const_tree arg1, const_tree arg2,
1126 int overflowable)
1128 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1129 return int_const_binop_2 (code, arg1, arg2, overflowable);
1131 gcc_assert (NUM_POLY_INT_COEFFS != 1);
1133 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1135 poly_wide_int res;
1136 bool overflow;
1137 tree type = TREE_TYPE (arg1);
1138 signop sign = TYPE_SIGN (type);
1139 switch (code)
1141 case PLUS_EXPR:
1142 res = wi::add (wi::to_poly_wide (arg1),
1143 wi::to_poly_wide (arg2), sign, &overflow);
1144 break;
1146 case MINUS_EXPR:
1147 res = wi::sub (wi::to_poly_wide (arg1),
1148 wi::to_poly_wide (arg2), sign, &overflow);
1149 break;
1151 case MULT_EXPR:
1152 if (TREE_CODE (arg2) == INTEGER_CST)
1153 res = wi::mul (wi::to_poly_wide (arg1),
1154 wi::to_wide (arg2), sign, &overflow);
1155 else if (TREE_CODE (arg1) == INTEGER_CST)
1156 res = wi::mul (wi::to_poly_wide (arg2),
1157 wi::to_wide (arg1), sign, &overflow);
1158 else
1159 return NULL_TREE;
1160 break;
1162 case LSHIFT_EXPR:
1163 if (TREE_CODE (arg2) == INTEGER_CST)
1164 res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1165 else
1166 return NULL_TREE;
1167 break;
1169 case BIT_IOR_EXPR:
1170 if (TREE_CODE (arg2) != INTEGER_CST
1171 || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1172 &res))
1173 return NULL_TREE;
1174 break;
1176 default:
1177 return NULL_TREE;
1179 return force_fit_type (type, res, overflowable,
1180 (((sign == SIGNED || overflowable == -1)
1181 && overflow)
1182 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1185 return NULL_TREE;
1188 tree
1189 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2)
1191 return int_const_binop_1 (code, arg1, arg2, 1);
1194 /* Return true if binary operation OP distributes over addition in operand
1195 OPNO, with the other operand being held constant. OPNO counts from 1. */
1197 static bool
1198 distributes_over_addition_p (tree_code op, int opno)
1200 switch (op)
1202 case PLUS_EXPR:
1203 case MINUS_EXPR:
1204 case MULT_EXPR:
1205 return true;
1207 case LSHIFT_EXPR:
1208 return opno == 1;
1210 default:
1211 return false;
1215 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1216 constant. We assume ARG1 and ARG2 have the same data type, or at least
1217 are the same kind of constant and the same machine mode. Return zero if
1218 combining the constants is not allowed in the current operating mode. */
1220 static tree
1221 const_binop (enum tree_code code, tree arg1, tree arg2)
1223 /* Sanity check for the recursive cases. */
1224 if (!arg1 || !arg2)
1225 return NULL_TREE;
1227 STRIP_NOPS (arg1);
1228 STRIP_NOPS (arg2);
1230 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1232 if (code == POINTER_PLUS_EXPR)
1233 return int_const_binop (PLUS_EXPR,
1234 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1236 return int_const_binop (code, arg1, arg2);
1239 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1241 machine_mode mode;
1242 REAL_VALUE_TYPE d1;
1243 REAL_VALUE_TYPE d2;
1244 REAL_VALUE_TYPE value;
1245 REAL_VALUE_TYPE result;
1246 bool inexact;
1247 tree t, type;
1249 /* The following codes are handled by real_arithmetic. */
1250 switch (code)
1252 case PLUS_EXPR:
1253 case MINUS_EXPR:
1254 case MULT_EXPR:
1255 case RDIV_EXPR:
1256 case MIN_EXPR:
1257 case MAX_EXPR:
1258 break;
1260 default:
1261 return NULL_TREE;
1264 d1 = TREE_REAL_CST (arg1);
1265 d2 = TREE_REAL_CST (arg2);
1267 type = TREE_TYPE (arg1);
1268 mode = TYPE_MODE (type);
1270 /* Don't perform operation if we honor signaling NaNs and
1271 either operand is a signaling NaN. */
1272 if (HONOR_SNANS (mode)
1273 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1274 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1275 return NULL_TREE;
1277 /* Don't perform operation if it would raise a division
1278 by zero exception. */
1279 if (code == RDIV_EXPR
1280 && real_equal (&d2, &dconst0)
1281 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1282 return NULL_TREE;
1284 /* If either operand is a NaN, just return it. Otherwise, set up
1285 for floating-point trap; we return an overflow. */
1286 if (REAL_VALUE_ISNAN (d1))
1288 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1289 is off. */
1290 d1.signalling = 0;
1291 t = build_real (type, d1);
1292 return t;
1294 else if (REAL_VALUE_ISNAN (d2))
1296 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1297 is off. */
1298 d2.signalling = 0;
1299 t = build_real (type, d2);
1300 return t;
1303 inexact = real_arithmetic (&value, code, &d1, &d2);
1304 real_convert (&result, mode, &value);
1306 /* Don't constant fold this floating point operation if
1307 the result has overflowed and flag_trapping_math. */
1308 if (flag_trapping_math
1309 && MODE_HAS_INFINITIES (mode)
1310 && REAL_VALUE_ISINF (result)
1311 && !REAL_VALUE_ISINF (d1)
1312 && !REAL_VALUE_ISINF (d2))
1313 return NULL_TREE;
1315 /* Don't constant fold this floating point operation if the
1316 result may dependent upon the run-time rounding mode and
1317 flag_rounding_math is set, or if GCC's software emulation
1318 is unable to accurately represent the result. */
1319 if ((flag_rounding_math
1320 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1321 && (inexact || !real_identical (&result, &value)))
1322 return NULL_TREE;
1324 t = build_real (type, result);
1326 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1327 return t;
1330 if (TREE_CODE (arg1) == FIXED_CST)
1332 FIXED_VALUE_TYPE f1;
1333 FIXED_VALUE_TYPE f2;
1334 FIXED_VALUE_TYPE result;
1335 tree t, type;
1336 int sat_p;
1337 bool overflow_p;
1339 /* The following codes are handled by fixed_arithmetic. */
1340 switch (code)
1342 case PLUS_EXPR:
1343 case MINUS_EXPR:
1344 case MULT_EXPR:
1345 case TRUNC_DIV_EXPR:
1346 if (TREE_CODE (arg2) != FIXED_CST)
1347 return NULL_TREE;
1348 f2 = TREE_FIXED_CST (arg2);
1349 break;
1351 case LSHIFT_EXPR:
1352 case RSHIFT_EXPR:
1354 if (TREE_CODE (arg2) != INTEGER_CST)
1355 return NULL_TREE;
1356 wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1357 f2.data.high = w2.elt (1);
1358 f2.data.low = w2.ulow ();
1359 f2.mode = SImode;
1361 break;
1363 default:
1364 return NULL_TREE;
1367 f1 = TREE_FIXED_CST (arg1);
1368 type = TREE_TYPE (arg1);
1369 sat_p = TYPE_SATURATING (type);
1370 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1371 t = build_fixed (type, result);
1372 /* Propagate overflow flags. */
1373 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1374 TREE_OVERFLOW (t) = 1;
1375 return t;
1378 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1380 tree type = TREE_TYPE (arg1);
1381 tree r1 = TREE_REALPART (arg1);
1382 tree i1 = TREE_IMAGPART (arg1);
1383 tree r2 = TREE_REALPART (arg2);
1384 tree i2 = TREE_IMAGPART (arg2);
1385 tree real, imag;
1387 switch (code)
1389 case PLUS_EXPR:
1390 case MINUS_EXPR:
1391 real = const_binop (code, r1, r2);
1392 imag = const_binop (code, i1, i2);
1393 break;
1395 case MULT_EXPR:
1396 if (COMPLEX_FLOAT_TYPE_P (type))
1397 return do_mpc_arg2 (arg1, arg2, type,
1398 /* do_nonfinite= */ folding_initializer,
1399 mpc_mul);
1401 real = const_binop (MINUS_EXPR,
1402 const_binop (MULT_EXPR, r1, r2),
1403 const_binop (MULT_EXPR, i1, i2));
1404 imag = const_binop (PLUS_EXPR,
1405 const_binop (MULT_EXPR, r1, i2),
1406 const_binop (MULT_EXPR, i1, r2));
1407 break;
1409 case RDIV_EXPR:
1410 if (COMPLEX_FLOAT_TYPE_P (type))
1411 return do_mpc_arg2 (arg1, arg2, type,
1412 /* do_nonfinite= */ folding_initializer,
1413 mpc_div);
1414 /* Fallthru. */
1415 case TRUNC_DIV_EXPR:
1416 case CEIL_DIV_EXPR:
1417 case FLOOR_DIV_EXPR:
1418 case ROUND_DIV_EXPR:
1419 if (flag_complex_method == 0)
1421 /* Keep this algorithm in sync with
1422 tree-complex.c:expand_complex_div_straight().
1424 Expand complex division to scalars, straightforward algorithm.
1425 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1426 t = br*br + bi*bi
1428 tree magsquared
1429 = const_binop (PLUS_EXPR,
1430 const_binop (MULT_EXPR, r2, r2),
1431 const_binop (MULT_EXPR, i2, i2));
1432 tree t1
1433 = const_binop (PLUS_EXPR,
1434 const_binop (MULT_EXPR, r1, r2),
1435 const_binop (MULT_EXPR, i1, i2));
1436 tree t2
1437 = const_binop (MINUS_EXPR,
1438 const_binop (MULT_EXPR, i1, r2),
1439 const_binop (MULT_EXPR, r1, i2));
1441 real = const_binop (code, t1, magsquared);
1442 imag = const_binop (code, t2, magsquared);
1444 else
1446 /* Keep this algorithm in sync with
1447 tree-complex.c:expand_complex_div_wide().
1449 Expand complex division to scalars, modified algorithm to minimize
1450 overflow with wide input ranges. */
1451 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1452 fold_abs_const (r2, TREE_TYPE (type)),
1453 fold_abs_const (i2, TREE_TYPE (type)));
1455 if (integer_nonzerop (compare))
1457 /* In the TRUE branch, we compute
1458 ratio = br/bi;
1459 div = (br * ratio) + bi;
1460 tr = (ar * ratio) + ai;
1461 ti = (ai * ratio) - ar;
1462 tr = tr / div;
1463 ti = ti / div; */
1464 tree ratio = const_binop (code, r2, i2);
1465 tree div = const_binop (PLUS_EXPR, i2,
1466 const_binop (MULT_EXPR, r2, ratio));
1467 real = const_binop (MULT_EXPR, r1, ratio);
1468 real = const_binop (PLUS_EXPR, real, i1);
1469 real = const_binop (code, real, div);
1471 imag = const_binop (MULT_EXPR, i1, ratio);
1472 imag = const_binop (MINUS_EXPR, imag, r1);
1473 imag = const_binop (code, imag, div);
1475 else
1477 /* In the FALSE branch, we compute
1478 ratio = d/c;
1479 divisor = (d * ratio) + c;
1480 tr = (b * ratio) + a;
1481 ti = b - (a * ratio);
1482 tr = tr / div;
1483 ti = ti / div; */
1484 tree ratio = const_binop (code, i2, r2);
1485 tree div = const_binop (PLUS_EXPR, r2,
1486 const_binop (MULT_EXPR, i2, ratio));
1488 real = const_binop (MULT_EXPR, i1, ratio);
1489 real = const_binop (PLUS_EXPR, real, r1);
1490 real = const_binop (code, real, div);
1492 imag = const_binop (MULT_EXPR, r1, ratio);
1493 imag = const_binop (MINUS_EXPR, i1, imag);
1494 imag = const_binop (code, imag, div);
1497 break;
1499 default:
1500 return NULL_TREE;
1503 if (real && imag)
1504 return build_complex (type, real, imag);
1507 if (TREE_CODE (arg1) == VECTOR_CST
1508 && TREE_CODE (arg2) == VECTOR_CST
1509 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1510 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1512 tree type = TREE_TYPE (arg1);
1513 bool step_ok_p;
1514 if (VECTOR_CST_STEPPED_P (arg1)
1515 && VECTOR_CST_STEPPED_P (arg2))
1516 /* We can operate directly on the encoding if:
1518 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1519 implies
1520 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1522 Addition and subtraction are the supported operators
1523 for which this is true. */
1524 step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1525 else if (VECTOR_CST_STEPPED_P (arg1))
1526 /* We can operate directly on stepped encodings if:
1528 a3 - a2 == a2 - a1
1529 implies:
1530 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1532 which is true if (x -> x op c) distributes over addition. */
1533 step_ok_p = distributes_over_addition_p (code, 1);
1534 else
1535 /* Similarly in reverse. */
1536 step_ok_p = distributes_over_addition_p (code, 2);
1537 tree_vector_builder elts;
1538 if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1539 return NULL_TREE;
1540 unsigned int count = elts.encoded_nelts ();
1541 for (unsigned int i = 0; i < count; ++i)
1543 tree elem1 = VECTOR_CST_ELT (arg1, i);
1544 tree elem2 = VECTOR_CST_ELT (arg2, i);
1546 tree elt = const_binop (code, elem1, elem2);
1548 /* It is possible that const_binop cannot handle the given
1549 code and return NULL_TREE */
1550 if (elt == NULL_TREE)
1551 return NULL_TREE;
1552 elts.quick_push (elt);
1555 return elts.build ();
1558 /* Shifts allow a scalar offset for a vector. */
1559 if (TREE_CODE (arg1) == VECTOR_CST
1560 && TREE_CODE (arg2) == INTEGER_CST)
1562 tree type = TREE_TYPE (arg1);
1563 bool step_ok_p = distributes_over_addition_p (code, 1);
1564 tree_vector_builder elts;
1565 if (!elts.new_unary_operation (type, arg1, step_ok_p))
1566 return NULL_TREE;
1567 unsigned int count = elts.encoded_nelts ();
1568 for (unsigned int i = 0; i < count; ++i)
1570 tree elem1 = VECTOR_CST_ELT (arg1, i);
1572 tree elt = const_binop (code, elem1, arg2);
1574 /* It is possible that const_binop cannot handle the given
1575 code and return NULL_TREE. */
1576 if (elt == NULL_TREE)
1577 return NULL_TREE;
1578 elts.quick_push (elt);
1581 return elts.build ();
1583 return NULL_TREE;
1586 /* Overload that adds a TYPE parameter to be able to dispatch
1587 to fold_relational_const. */
1589 tree
1590 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1592 if (TREE_CODE_CLASS (code) == tcc_comparison)
1593 return fold_relational_const (code, type, arg1, arg2);
1595 /* ??? Until we make the const_binop worker take the type of the
1596 result as argument put those cases that need it here. */
1597 switch (code)
1599 case VEC_SERIES_EXPR:
1600 if (CONSTANT_CLASS_P (arg1)
1601 && CONSTANT_CLASS_P (arg2))
1602 return build_vec_series (type, arg1, arg2);
1603 return NULL_TREE;
1605 case COMPLEX_EXPR:
1606 if ((TREE_CODE (arg1) == REAL_CST
1607 && TREE_CODE (arg2) == REAL_CST)
1608 || (TREE_CODE (arg1) == INTEGER_CST
1609 && TREE_CODE (arg2) == INTEGER_CST))
1610 return build_complex (type, arg1, arg2);
1611 return NULL_TREE;
1613 case POINTER_DIFF_EXPR:
1614 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1616 poly_offset_int res = (wi::to_poly_offset (arg1)
1617 - wi::to_poly_offset (arg2));
1618 return force_fit_type (type, res, 1,
1619 TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1621 return NULL_TREE;
1623 case VEC_PACK_TRUNC_EXPR:
1624 case VEC_PACK_FIX_TRUNC_EXPR:
1625 case VEC_PACK_FLOAT_EXPR:
1627 unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1629 if (TREE_CODE (arg1) != VECTOR_CST
1630 || TREE_CODE (arg2) != VECTOR_CST)
1631 return NULL_TREE;
1633 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1634 return NULL_TREE;
1636 out_nelts = in_nelts * 2;
1637 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1638 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1640 tree_vector_builder elts (type, out_nelts, 1);
1641 for (i = 0; i < out_nelts; i++)
1643 tree elt = (i < in_nelts
1644 ? VECTOR_CST_ELT (arg1, i)
1645 : VECTOR_CST_ELT (arg2, i - in_nelts));
1646 elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1647 ? NOP_EXPR
1648 : code == VEC_PACK_FLOAT_EXPR
1649 ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1650 TREE_TYPE (type), elt);
1651 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1652 return NULL_TREE;
1653 elts.quick_push (elt);
1656 return elts.build ();
1659 case VEC_WIDEN_MULT_LO_EXPR:
1660 case VEC_WIDEN_MULT_HI_EXPR:
1661 case VEC_WIDEN_MULT_EVEN_EXPR:
1662 case VEC_WIDEN_MULT_ODD_EXPR:
1664 unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1666 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1667 return NULL_TREE;
1669 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1670 return NULL_TREE;
1671 out_nelts = in_nelts / 2;
1672 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1673 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1675 if (code == VEC_WIDEN_MULT_LO_EXPR)
1676 scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1677 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1678 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1679 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1680 scale = 1, ofs = 0;
1681 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1682 scale = 1, ofs = 1;
1684 tree_vector_builder elts (type, out_nelts, 1);
1685 for (out = 0; out < out_nelts; out++)
1687 unsigned int in = (out << scale) + ofs;
1688 tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1689 VECTOR_CST_ELT (arg1, in));
1690 tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1691 VECTOR_CST_ELT (arg2, in));
1693 if (t1 == NULL_TREE || t2 == NULL_TREE)
1694 return NULL_TREE;
1695 tree elt = const_binop (MULT_EXPR, t1, t2);
1696 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1697 return NULL_TREE;
1698 elts.quick_push (elt);
1701 return elts.build ();
1704 default:;
1707 if (TREE_CODE_CLASS (code) != tcc_binary)
1708 return NULL_TREE;
1710 /* Make sure type and arg0 have the same saturating flag. */
1711 gcc_checking_assert (TYPE_SATURATING (type)
1712 == TYPE_SATURATING (TREE_TYPE (arg1)));
1714 return const_binop (code, arg1, arg2);
1717 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1718 Return zero if computing the constants is not possible. */
1720 tree
1721 const_unop (enum tree_code code, tree type, tree arg0)
1723 /* Don't perform the operation, other than NEGATE and ABS, if
1724 flag_signaling_nans is on and the operand is a signaling NaN. */
1725 if (TREE_CODE (arg0) == REAL_CST
1726 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1727 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1728 && code != NEGATE_EXPR
1729 && code != ABS_EXPR
1730 && code != ABSU_EXPR)
1731 return NULL_TREE;
1733 switch (code)
1735 CASE_CONVERT:
1736 case FLOAT_EXPR:
1737 case FIX_TRUNC_EXPR:
1738 case FIXED_CONVERT_EXPR:
1739 return fold_convert_const (code, type, arg0);
1741 case ADDR_SPACE_CONVERT_EXPR:
1742 /* If the source address is 0, and the source address space
1743 cannot have a valid object at 0, fold to dest type null. */
1744 if (integer_zerop (arg0)
1745 && !(targetm.addr_space.zero_address_valid
1746 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1747 return fold_convert_const (code, type, arg0);
1748 break;
1750 case VIEW_CONVERT_EXPR:
1751 return fold_view_convert_expr (type, arg0);
1753 case NEGATE_EXPR:
1755 /* Can't call fold_negate_const directly here as that doesn't
1756 handle all cases and we might not be able to negate some
1757 constants. */
1758 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1759 if (tem && CONSTANT_CLASS_P (tem))
1760 return tem;
1761 break;
1764 case ABS_EXPR:
1765 case ABSU_EXPR:
1766 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1767 return fold_abs_const (arg0, type);
1768 break;
1770 case CONJ_EXPR:
1771 if (TREE_CODE (arg0) == COMPLEX_CST)
1773 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1774 TREE_TYPE (type));
1775 return build_complex (type, TREE_REALPART (arg0), ipart);
1777 break;
1779 case BIT_NOT_EXPR:
1780 if (TREE_CODE (arg0) == INTEGER_CST)
1781 return fold_not_const (arg0, type);
1782 else if (POLY_INT_CST_P (arg0))
1783 return wide_int_to_tree (type, -poly_int_cst_value (arg0));
1784 /* Perform BIT_NOT_EXPR on each element individually. */
1785 else if (TREE_CODE (arg0) == VECTOR_CST)
1787 tree elem;
1789 /* This can cope with stepped encodings because ~x == -1 - x. */
1790 tree_vector_builder elements;
1791 elements.new_unary_operation (type, arg0, true);
1792 unsigned int i, count = elements.encoded_nelts ();
1793 for (i = 0; i < count; ++i)
1795 elem = VECTOR_CST_ELT (arg0, i);
1796 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1797 if (elem == NULL_TREE)
1798 break;
1799 elements.quick_push (elem);
1801 if (i == count)
1802 return elements.build ();
1804 break;
1806 case TRUTH_NOT_EXPR:
1807 if (TREE_CODE (arg0) == INTEGER_CST)
1808 return constant_boolean_node (integer_zerop (arg0), type);
1809 break;
1811 case REALPART_EXPR:
1812 if (TREE_CODE (arg0) == COMPLEX_CST)
1813 return fold_convert (type, TREE_REALPART (arg0));
1814 break;
1816 case IMAGPART_EXPR:
1817 if (TREE_CODE (arg0) == COMPLEX_CST)
1818 return fold_convert (type, TREE_IMAGPART (arg0));
1819 break;
1821 case VEC_UNPACK_LO_EXPR:
1822 case VEC_UNPACK_HI_EXPR:
1823 case VEC_UNPACK_FLOAT_LO_EXPR:
1824 case VEC_UNPACK_FLOAT_HI_EXPR:
1825 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1826 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1828 unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
1829 enum tree_code subcode;
1831 if (TREE_CODE (arg0) != VECTOR_CST)
1832 return NULL_TREE;
1834 if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
1835 return NULL_TREE;
1836 out_nelts = in_nelts / 2;
1837 gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1839 unsigned int offset = 0;
1840 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1841 || code == VEC_UNPACK_FLOAT_LO_EXPR
1842 || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
1843 offset = out_nelts;
1845 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1846 subcode = NOP_EXPR;
1847 else if (code == VEC_UNPACK_FLOAT_LO_EXPR
1848 || code == VEC_UNPACK_FLOAT_HI_EXPR)
1849 subcode = FLOAT_EXPR;
1850 else
1851 subcode = FIX_TRUNC_EXPR;
1853 tree_vector_builder elts (type, out_nelts, 1);
1854 for (i = 0; i < out_nelts; i++)
1856 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1857 VECTOR_CST_ELT (arg0, i + offset));
1858 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1859 return NULL_TREE;
1860 elts.quick_push (elt);
1863 return elts.build ();
1866 case VEC_DUPLICATE_EXPR:
1867 if (CONSTANT_CLASS_P (arg0))
1868 return build_vector_from_val (type, arg0);
1869 return NULL_TREE;
1871 default:
1872 break;
1875 return NULL_TREE;
1878 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1879 indicates which particular sizetype to create. */
1881 tree
1882 size_int_kind (poly_int64 number, enum size_type_kind kind)
1884 return build_int_cst (sizetype_tab[(int) kind], number);
1887 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1888 is a tree code. The type of the result is taken from the operands.
1889 Both must be equivalent integer types, ala int_binop_types_match_p.
1890 If the operands are constant, so is the result. */
1892 tree
1893 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1895 tree type = TREE_TYPE (arg0);
1897 if (arg0 == error_mark_node || arg1 == error_mark_node)
1898 return error_mark_node;
1900 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1901 TREE_TYPE (arg1)));
1903 /* Handle the special case of two poly_int constants faster. */
1904 if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1906 /* And some specific cases even faster than that. */
1907 if (code == PLUS_EXPR)
1909 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
1910 return arg1;
1911 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1912 return arg0;
1914 else if (code == MINUS_EXPR)
1916 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
1917 return arg0;
1919 else if (code == MULT_EXPR)
1921 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
1922 return arg1;
1925 /* Handle general case of two integer constants. For sizetype
1926 constant calculations we always want to know about overflow,
1927 even in the unsigned case. */
1928 tree res = int_const_binop_1 (code, arg0, arg1, -1);
1929 if (res != NULL_TREE)
1930 return res;
1933 return fold_build2_loc (loc, code, type, arg0, arg1);
1936 /* Given two values, either both of sizetype or both of bitsizetype,
1937 compute the difference between the two values. Return the value
1938 in signed type corresponding to the type of the operands. */
1940 tree
1941 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1943 tree type = TREE_TYPE (arg0);
1944 tree ctype;
1946 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1947 TREE_TYPE (arg1)));
1949 /* If the type is already signed, just do the simple thing. */
1950 if (!TYPE_UNSIGNED (type))
1951 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1953 if (type == sizetype)
1954 ctype = ssizetype;
1955 else if (type == bitsizetype)
1956 ctype = sbitsizetype;
1957 else
1958 ctype = signed_type_for (type);
1960 /* If either operand is not a constant, do the conversions to the signed
1961 type and subtract. The hardware will do the right thing with any
1962 overflow in the subtraction. */
1963 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1964 return size_binop_loc (loc, MINUS_EXPR,
1965 fold_convert_loc (loc, ctype, arg0),
1966 fold_convert_loc (loc, ctype, arg1));
1968 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1969 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1970 overflow) and negate (which can't either). Special-case a result
1971 of zero while we're here. */
1972 if (tree_int_cst_equal (arg0, arg1))
1973 return build_int_cst (ctype, 0);
1974 else if (tree_int_cst_lt (arg1, arg0))
1975 return fold_convert_loc (loc, ctype,
1976 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1977 else
1978 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1979 fold_convert_loc (loc, ctype,
1980 size_binop_loc (loc,
1981 MINUS_EXPR,
1982 arg1, arg0)));
1985 /* A subroutine of fold_convert_const handling conversions of an
1986 INTEGER_CST to another integer type. */
1988 static tree
1989 fold_convert_const_int_from_int (tree type, const_tree arg1)
1991 /* Given an integer constant, make new constant with new type,
1992 appropriately sign-extended or truncated. Use widest_int
1993 so that any extension is done according ARG1's type. */
1994 return force_fit_type (type, wi::to_widest (arg1),
1995 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1996 TREE_OVERFLOW (arg1));
1999 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2000 to an integer type. */
2002 static tree
2003 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2005 bool overflow = false;
2006 tree t;
2008 /* The following code implements the floating point to integer
2009 conversion rules required by the Java Language Specification,
2010 that IEEE NaNs are mapped to zero and values that overflow
2011 the target precision saturate, i.e. values greater than
2012 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2013 are mapped to INT_MIN. These semantics are allowed by the
2014 C and C++ standards that simply state that the behavior of
2015 FP-to-integer conversion is unspecified upon overflow. */
2017 wide_int val;
2018 REAL_VALUE_TYPE r;
2019 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2021 switch (code)
2023 case FIX_TRUNC_EXPR:
2024 real_trunc (&r, VOIDmode, &x);
2025 break;
2027 default:
2028 gcc_unreachable ();
2031 /* If R is NaN, return zero and show we have an overflow. */
2032 if (REAL_VALUE_ISNAN (r))
2034 overflow = true;
2035 val = wi::zero (TYPE_PRECISION (type));
2038 /* See if R is less than the lower bound or greater than the
2039 upper bound. */
2041 if (! overflow)
2043 tree lt = TYPE_MIN_VALUE (type);
2044 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2045 if (real_less (&r, &l))
2047 overflow = true;
2048 val = wi::to_wide (lt);
2052 if (! overflow)
2054 tree ut = TYPE_MAX_VALUE (type);
2055 if (ut)
2057 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2058 if (real_less (&u, &r))
2060 overflow = true;
2061 val = wi::to_wide (ut);
2066 if (! overflow)
2067 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2069 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2070 return t;
2073 /* A subroutine of fold_convert_const handling conversions of a
2074 FIXED_CST to an integer type. */
2076 static tree
2077 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2079 tree t;
2080 double_int temp, temp_trunc;
2081 scalar_mode mode;
2083 /* Right shift FIXED_CST to temp by fbit. */
2084 temp = TREE_FIXED_CST (arg1).data;
2085 mode = TREE_FIXED_CST (arg1).mode;
2086 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2088 temp = temp.rshift (GET_MODE_FBIT (mode),
2089 HOST_BITS_PER_DOUBLE_INT,
2090 SIGNED_FIXED_POINT_MODE_P (mode));
2092 /* Left shift temp to temp_trunc by fbit. */
2093 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2094 HOST_BITS_PER_DOUBLE_INT,
2095 SIGNED_FIXED_POINT_MODE_P (mode));
2097 else
2099 temp = double_int_zero;
2100 temp_trunc = double_int_zero;
2103 /* If FIXED_CST is negative, we need to round the value toward 0.
2104 By checking if the fractional bits are not zero to add 1 to temp. */
2105 if (SIGNED_FIXED_POINT_MODE_P (mode)
2106 && temp_trunc.is_negative ()
2107 && TREE_FIXED_CST (arg1).data != temp_trunc)
2108 temp += double_int_one;
2110 /* Given a fixed-point constant, make new constant with new type,
2111 appropriately sign-extended or truncated. */
2112 t = force_fit_type (type, temp, -1,
2113 (temp.is_negative ()
2114 && (TYPE_UNSIGNED (type)
2115 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2116 | TREE_OVERFLOW (arg1));
2118 return t;
2121 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2122 to another floating point type. */
2124 static tree
2125 fold_convert_const_real_from_real (tree type, const_tree arg1)
2127 REAL_VALUE_TYPE value;
2128 tree t;
2130 /* Don't perform the operation if flag_signaling_nans is on
2131 and the operand is a signaling NaN. */
2132 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2133 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2134 return NULL_TREE;
2136 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2137 t = build_real (type, value);
2139 /* If converting an infinity or NAN to a representation that doesn't
2140 have one, set the overflow bit so that we can produce some kind of
2141 error message at the appropriate point if necessary. It's not the
2142 most user-friendly message, but it's better than nothing. */
2143 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2144 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2145 TREE_OVERFLOW (t) = 1;
2146 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2147 && !MODE_HAS_NANS (TYPE_MODE (type)))
2148 TREE_OVERFLOW (t) = 1;
2149 /* Regular overflow, conversion produced an infinity in a mode that
2150 can't represent them. */
2151 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2152 && REAL_VALUE_ISINF (value)
2153 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2154 TREE_OVERFLOW (t) = 1;
2155 else
2156 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2157 return t;
2160 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2161 to a floating point type. */
2163 static tree
2164 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2166 REAL_VALUE_TYPE value;
2167 tree t;
2169 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2170 &TREE_FIXED_CST (arg1));
2171 t = build_real (type, value);
2173 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2174 return t;
2177 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2178 to another fixed-point type. */
2180 static tree
2181 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2183 FIXED_VALUE_TYPE value;
2184 tree t;
2185 bool overflow_p;
2187 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2188 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2189 t = build_fixed (type, value);
2191 /* Propagate overflow flags. */
2192 if (overflow_p | TREE_OVERFLOW (arg1))
2193 TREE_OVERFLOW (t) = 1;
2194 return t;
2197 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2198 to a fixed-point type. */
2200 static tree
2201 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2203 FIXED_VALUE_TYPE value;
2204 tree t;
2205 bool overflow_p;
2206 double_int di;
2208 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2210 di.low = TREE_INT_CST_ELT (arg1, 0);
2211 if (TREE_INT_CST_NUNITS (arg1) == 1)
2212 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2213 else
2214 di.high = TREE_INT_CST_ELT (arg1, 1);
2216 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2217 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2218 TYPE_SATURATING (type));
2219 t = build_fixed (type, value);
2221 /* Propagate overflow flags. */
2222 if (overflow_p | TREE_OVERFLOW (arg1))
2223 TREE_OVERFLOW (t) = 1;
2224 return t;
2227 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2228 to a fixed-point type. */
2230 static tree
2231 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2233 FIXED_VALUE_TYPE value;
2234 tree t;
2235 bool overflow_p;
2237 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2238 &TREE_REAL_CST (arg1),
2239 TYPE_SATURATING (type));
2240 t = build_fixed (type, value);
2242 /* Propagate overflow flags. */
2243 if (overflow_p | TREE_OVERFLOW (arg1))
2244 TREE_OVERFLOW (t) = 1;
2245 return t;
2248 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2249 type TYPE. If no simplification can be done return NULL_TREE. */
2251 static tree
2252 fold_convert_const (enum tree_code code, tree type, tree arg1)
2254 tree arg_type = TREE_TYPE (arg1);
2255 if (arg_type == type)
2256 return arg1;
2258 /* We can't widen types, since the runtime value could overflow the
2259 original type before being extended to the new type. */
2260 if (POLY_INT_CST_P (arg1)
2261 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2262 && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2263 return build_poly_int_cst (type,
2264 poly_wide_int::from (poly_int_cst_value (arg1),
2265 TYPE_PRECISION (type),
2266 TYPE_SIGN (arg_type)));
2268 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2269 || TREE_CODE (type) == OFFSET_TYPE)
2271 if (TREE_CODE (arg1) == INTEGER_CST)
2272 return fold_convert_const_int_from_int (type, arg1);
2273 else if (TREE_CODE (arg1) == REAL_CST)
2274 return fold_convert_const_int_from_real (code, type, arg1);
2275 else if (TREE_CODE (arg1) == FIXED_CST)
2276 return fold_convert_const_int_from_fixed (type, arg1);
2278 else if (TREE_CODE (type) == REAL_TYPE)
2280 if (TREE_CODE (arg1) == INTEGER_CST)
2281 return build_real_from_int_cst (type, arg1);
2282 else if (TREE_CODE (arg1) == REAL_CST)
2283 return fold_convert_const_real_from_real (type, arg1);
2284 else if (TREE_CODE (arg1) == FIXED_CST)
2285 return fold_convert_const_real_from_fixed (type, arg1);
2287 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2289 if (TREE_CODE (arg1) == FIXED_CST)
2290 return fold_convert_const_fixed_from_fixed (type, arg1);
2291 else if (TREE_CODE (arg1) == INTEGER_CST)
2292 return fold_convert_const_fixed_from_int (type, arg1);
2293 else if (TREE_CODE (arg1) == REAL_CST)
2294 return fold_convert_const_fixed_from_real (type, arg1);
2296 else if (TREE_CODE (type) == VECTOR_TYPE)
2298 if (TREE_CODE (arg1) == VECTOR_CST
2299 && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2301 tree elttype = TREE_TYPE (type);
2302 tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2303 /* We can't handle steps directly when extending, since the
2304 values need to wrap at the original precision first. */
2305 bool step_ok_p
2306 = (INTEGRAL_TYPE_P (elttype)
2307 && INTEGRAL_TYPE_P (arg1_elttype)
2308 && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2309 tree_vector_builder v;
2310 if (!v.new_unary_operation (type, arg1, step_ok_p))
2311 return NULL_TREE;
2312 unsigned int len = v.encoded_nelts ();
2313 for (unsigned int i = 0; i < len; ++i)
2315 tree elt = VECTOR_CST_ELT (arg1, i);
2316 tree cvt = fold_convert_const (code, elttype, elt);
2317 if (cvt == NULL_TREE)
2318 return NULL_TREE;
2319 v.quick_push (cvt);
2321 return v.build ();
2324 return NULL_TREE;
2327 /* Construct a vector of zero elements of vector type TYPE. */
2329 static tree
2330 build_zero_vector (tree type)
2332 tree t;
2334 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2335 return build_vector_from_val (type, t);
2338 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2340 bool
2341 fold_convertible_p (const_tree type, const_tree arg)
2343 tree orig = TREE_TYPE (arg);
2345 if (type == orig)
2346 return true;
2348 if (TREE_CODE (arg) == ERROR_MARK
2349 || TREE_CODE (type) == ERROR_MARK
2350 || TREE_CODE (orig) == ERROR_MARK)
2351 return false;
2353 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2354 return true;
2356 switch (TREE_CODE (type))
2358 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2359 case POINTER_TYPE: case REFERENCE_TYPE:
2360 case OFFSET_TYPE:
2361 return (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2362 || TREE_CODE (orig) == OFFSET_TYPE);
2364 case REAL_TYPE:
2365 case FIXED_POINT_TYPE:
2366 case VECTOR_TYPE:
2367 case VOID_TYPE:
2368 return TREE_CODE (type) == TREE_CODE (orig);
2370 default:
2371 return false;
2375 /* Convert expression ARG to type TYPE. Used by the middle-end for
2376 simple conversions in preference to calling the front-end's convert. */
2378 tree
2379 fold_convert_loc (location_t loc, tree type, tree arg)
2381 tree orig = TREE_TYPE (arg);
2382 tree tem;
2384 if (type == orig)
2385 return arg;
2387 if (TREE_CODE (arg) == ERROR_MARK
2388 || TREE_CODE (type) == ERROR_MARK
2389 || TREE_CODE (orig) == ERROR_MARK)
2390 return error_mark_node;
2392 switch (TREE_CODE (type))
2394 case POINTER_TYPE:
2395 case REFERENCE_TYPE:
2396 /* Handle conversions between pointers to different address spaces. */
2397 if (POINTER_TYPE_P (orig)
2398 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2399 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2400 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2401 /* fall through */
2403 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2404 case OFFSET_TYPE:
2405 if (TREE_CODE (arg) == INTEGER_CST)
2407 tem = fold_convert_const (NOP_EXPR, type, arg);
2408 if (tem != NULL_TREE)
2409 return tem;
2411 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2412 || TREE_CODE (orig) == OFFSET_TYPE)
2413 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2414 if (TREE_CODE (orig) == COMPLEX_TYPE)
2415 return fold_convert_loc (loc, type,
2416 fold_build1_loc (loc, REALPART_EXPR,
2417 TREE_TYPE (orig), arg));
2418 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2419 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2420 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2422 case REAL_TYPE:
2423 if (TREE_CODE (arg) == INTEGER_CST)
2425 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2426 if (tem != NULL_TREE)
2427 return tem;
2429 else if (TREE_CODE (arg) == REAL_CST)
2431 tem = fold_convert_const (NOP_EXPR, type, arg);
2432 if (tem != NULL_TREE)
2433 return tem;
2435 else if (TREE_CODE (arg) == FIXED_CST)
2437 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2438 if (tem != NULL_TREE)
2439 return tem;
2442 switch (TREE_CODE (orig))
2444 case INTEGER_TYPE:
2445 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2446 case POINTER_TYPE: case REFERENCE_TYPE:
2447 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2449 case REAL_TYPE:
2450 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2452 case FIXED_POINT_TYPE:
2453 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2455 case COMPLEX_TYPE:
2456 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2457 return fold_convert_loc (loc, type, tem);
2459 default:
2460 gcc_unreachable ();
2463 case FIXED_POINT_TYPE:
2464 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2465 || TREE_CODE (arg) == REAL_CST)
2467 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2468 if (tem != NULL_TREE)
2469 goto fold_convert_exit;
2472 switch (TREE_CODE (orig))
2474 case FIXED_POINT_TYPE:
2475 case INTEGER_TYPE:
2476 case ENUMERAL_TYPE:
2477 case BOOLEAN_TYPE:
2478 case REAL_TYPE:
2479 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2481 case COMPLEX_TYPE:
2482 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2483 return fold_convert_loc (loc, type, tem);
2485 default:
2486 gcc_unreachable ();
2489 case COMPLEX_TYPE:
2490 switch (TREE_CODE (orig))
2492 case INTEGER_TYPE:
2493 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2494 case POINTER_TYPE: case REFERENCE_TYPE:
2495 case REAL_TYPE:
2496 case FIXED_POINT_TYPE:
2497 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2498 fold_convert_loc (loc, TREE_TYPE (type), arg),
2499 fold_convert_loc (loc, TREE_TYPE (type),
2500 integer_zero_node));
2501 case COMPLEX_TYPE:
2503 tree rpart, ipart;
2505 if (TREE_CODE (arg) == COMPLEX_EXPR)
2507 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2508 TREE_OPERAND (arg, 0));
2509 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2510 TREE_OPERAND (arg, 1));
2511 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2514 arg = save_expr (arg);
2515 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2516 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2517 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2518 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2519 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2522 default:
2523 gcc_unreachable ();
2526 case VECTOR_TYPE:
2527 if (integer_zerop (arg))
2528 return build_zero_vector (type);
2529 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2530 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2531 || TREE_CODE (orig) == VECTOR_TYPE);
2532 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2534 case VOID_TYPE:
2535 tem = fold_ignored_result (arg);
2536 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2538 default:
2539 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2540 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2541 gcc_unreachable ();
2543 fold_convert_exit:
2544 protected_set_expr_location_unshare (tem, loc);
2545 return tem;
2548 /* Return false if expr can be assumed not to be an lvalue, true
2549 otherwise. */
2551 static bool
2552 maybe_lvalue_p (const_tree x)
2554 /* We only need to wrap lvalue tree codes. */
2555 switch (TREE_CODE (x))
2557 case VAR_DECL:
2558 case PARM_DECL:
2559 case RESULT_DECL:
2560 case LABEL_DECL:
2561 case FUNCTION_DECL:
2562 case SSA_NAME:
2564 case COMPONENT_REF:
2565 case MEM_REF:
2566 case INDIRECT_REF:
2567 case ARRAY_REF:
2568 case ARRAY_RANGE_REF:
2569 case BIT_FIELD_REF:
2570 case OBJ_TYPE_REF:
2572 case REALPART_EXPR:
2573 case IMAGPART_EXPR:
2574 case PREINCREMENT_EXPR:
2575 case PREDECREMENT_EXPR:
2576 case SAVE_EXPR:
2577 case TRY_CATCH_EXPR:
2578 case WITH_CLEANUP_EXPR:
2579 case COMPOUND_EXPR:
2580 case MODIFY_EXPR:
2581 case TARGET_EXPR:
2582 case COND_EXPR:
2583 case BIND_EXPR:
2584 break;
2586 default:
2587 /* Assume the worst for front-end tree codes. */
2588 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2589 break;
2590 return false;
2593 return true;
2596 /* Return an expr equal to X but certainly not valid as an lvalue. */
2598 tree
2599 non_lvalue_loc (location_t loc, tree x)
2601 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2602 us. */
2603 if (in_gimple_form)
2604 return x;
2606 if (! maybe_lvalue_p (x))
2607 return x;
2608 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2611 /* When pedantic, return an expr equal to X but certainly not valid as a
2612 pedantic lvalue. Otherwise, return X. */
2614 static tree
2615 pedantic_non_lvalue_loc (location_t loc, tree x)
2617 return protected_set_expr_location_unshare (x, loc);
2620 /* Given a tree comparison code, return the code that is the logical inverse.
2621 It is generally not safe to do this for floating-point comparisons, except
2622 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2623 ERROR_MARK in this case. */
2625 enum tree_code
2626 invert_tree_comparison (enum tree_code code, bool honor_nans)
2628 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2629 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2630 return ERROR_MARK;
2632 switch (code)
2634 case EQ_EXPR:
2635 return NE_EXPR;
2636 case NE_EXPR:
2637 return EQ_EXPR;
2638 case GT_EXPR:
2639 return honor_nans ? UNLE_EXPR : LE_EXPR;
2640 case GE_EXPR:
2641 return honor_nans ? UNLT_EXPR : LT_EXPR;
2642 case LT_EXPR:
2643 return honor_nans ? UNGE_EXPR : GE_EXPR;
2644 case LE_EXPR:
2645 return honor_nans ? UNGT_EXPR : GT_EXPR;
2646 case LTGT_EXPR:
2647 return UNEQ_EXPR;
2648 case UNEQ_EXPR:
2649 return LTGT_EXPR;
2650 case UNGT_EXPR:
2651 return LE_EXPR;
2652 case UNGE_EXPR:
2653 return LT_EXPR;
2654 case UNLT_EXPR:
2655 return GE_EXPR;
2656 case UNLE_EXPR:
2657 return GT_EXPR;
2658 case ORDERED_EXPR:
2659 return UNORDERED_EXPR;
2660 case UNORDERED_EXPR:
2661 return ORDERED_EXPR;
2662 default:
2663 gcc_unreachable ();
2667 /* Similar, but return the comparison that results if the operands are
2668 swapped. This is safe for floating-point. */
2670 enum tree_code
2671 swap_tree_comparison (enum tree_code code)
2673 switch (code)
2675 case EQ_EXPR:
2676 case NE_EXPR:
2677 case ORDERED_EXPR:
2678 case UNORDERED_EXPR:
2679 case LTGT_EXPR:
2680 case UNEQ_EXPR:
2681 return code;
2682 case GT_EXPR:
2683 return LT_EXPR;
2684 case GE_EXPR:
2685 return LE_EXPR;
2686 case LT_EXPR:
2687 return GT_EXPR;
2688 case LE_EXPR:
2689 return GE_EXPR;
2690 case UNGT_EXPR:
2691 return UNLT_EXPR;
2692 case UNGE_EXPR:
2693 return UNLE_EXPR;
2694 case UNLT_EXPR:
2695 return UNGT_EXPR;
2696 case UNLE_EXPR:
2697 return UNGE_EXPR;
2698 default:
2699 gcc_unreachable ();
2704 /* Convert a comparison tree code from an enum tree_code representation
2705 into a compcode bit-based encoding. This function is the inverse of
2706 compcode_to_comparison. */
2708 static enum comparison_code
2709 comparison_to_compcode (enum tree_code code)
2711 switch (code)
2713 case LT_EXPR:
2714 return COMPCODE_LT;
2715 case EQ_EXPR:
2716 return COMPCODE_EQ;
2717 case LE_EXPR:
2718 return COMPCODE_LE;
2719 case GT_EXPR:
2720 return COMPCODE_GT;
2721 case NE_EXPR:
2722 return COMPCODE_NE;
2723 case GE_EXPR:
2724 return COMPCODE_GE;
2725 case ORDERED_EXPR:
2726 return COMPCODE_ORD;
2727 case UNORDERED_EXPR:
2728 return COMPCODE_UNORD;
2729 case UNLT_EXPR:
2730 return COMPCODE_UNLT;
2731 case UNEQ_EXPR:
2732 return COMPCODE_UNEQ;
2733 case UNLE_EXPR:
2734 return COMPCODE_UNLE;
2735 case UNGT_EXPR:
2736 return COMPCODE_UNGT;
2737 case LTGT_EXPR:
2738 return COMPCODE_LTGT;
2739 case UNGE_EXPR:
2740 return COMPCODE_UNGE;
2741 default:
2742 gcc_unreachable ();
2746 /* Convert a compcode bit-based encoding of a comparison operator back
2747 to GCC's enum tree_code representation. This function is the
2748 inverse of comparison_to_compcode. */
2750 static enum tree_code
2751 compcode_to_comparison (enum comparison_code code)
2753 switch (code)
2755 case COMPCODE_LT:
2756 return LT_EXPR;
2757 case COMPCODE_EQ:
2758 return EQ_EXPR;
2759 case COMPCODE_LE:
2760 return LE_EXPR;
2761 case COMPCODE_GT:
2762 return GT_EXPR;
2763 case COMPCODE_NE:
2764 return NE_EXPR;
2765 case COMPCODE_GE:
2766 return GE_EXPR;
2767 case COMPCODE_ORD:
2768 return ORDERED_EXPR;
2769 case COMPCODE_UNORD:
2770 return UNORDERED_EXPR;
2771 case COMPCODE_UNLT:
2772 return UNLT_EXPR;
2773 case COMPCODE_UNEQ:
2774 return UNEQ_EXPR;
2775 case COMPCODE_UNLE:
2776 return UNLE_EXPR;
2777 case COMPCODE_UNGT:
2778 return UNGT_EXPR;
2779 case COMPCODE_LTGT:
2780 return LTGT_EXPR;
2781 case COMPCODE_UNGE:
2782 return UNGE_EXPR;
2783 default:
2784 gcc_unreachable ();
2788 /* Return a tree for the comparison which is the combination of
2789 doing the AND or OR (depending on CODE) of the two operations LCODE
2790 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2791 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2792 if this makes the transformation invalid. */
2794 tree
2795 combine_comparisons (location_t loc,
2796 enum tree_code code, enum tree_code lcode,
2797 enum tree_code rcode, tree truth_type,
2798 tree ll_arg, tree lr_arg)
2800 bool honor_nans = HONOR_NANS (ll_arg);
2801 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2802 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2803 int compcode;
2805 switch (code)
2807 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2808 compcode = lcompcode & rcompcode;
2809 break;
2811 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2812 compcode = lcompcode | rcompcode;
2813 break;
2815 default:
2816 return NULL_TREE;
2819 if (!honor_nans)
2821 /* Eliminate unordered comparisons, as well as LTGT and ORD
2822 which are not used unless the mode has NaNs. */
2823 compcode &= ~COMPCODE_UNORD;
2824 if (compcode == COMPCODE_LTGT)
2825 compcode = COMPCODE_NE;
2826 else if (compcode == COMPCODE_ORD)
2827 compcode = COMPCODE_TRUE;
2829 else if (flag_trapping_math)
2831 /* Check that the original operation and the optimized ones will trap
2832 under the same condition. */
2833 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2834 && (lcompcode != COMPCODE_EQ)
2835 && (lcompcode != COMPCODE_ORD);
2836 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2837 && (rcompcode != COMPCODE_EQ)
2838 && (rcompcode != COMPCODE_ORD);
2839 bool trap = (compcode & COMPCODE_UNORD) == 0
2840 && (compcode != COMPCODE_EQ)
2841 && (compcode != COMPCODE_ORD);
2843 /* In a short-circuited boolean expression the LHS might be
2844 such that the RHS, if evaluated, will never trap. For
2845 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2846 if neither x nor y is NaN. (This is a mixed blessing: for
2847 example, the expression above will never trap, hence
2848 optimizing it to x < y would be invalid). */
2849 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2850 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2851 rtrap = false;
2853 /* If the comparison was short-circuited, and only the RHS
2854 trapped, we may now generate a spurious trap. */
2855 if (rtrap && !ltrap
2856 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2857 return NULL_TREE;
2859 /* If we changed the conditions that cause a trap, we lose. */
2860 if ((ltrap || rtrap) != trap)
2861 return NULL_TREE;
2864 if (compcode == COMPCODE_TRUE)
2865 return constant_boolean_node (true, truth_type);
2866 else if (compcode == COMPCODE_FALSE)
2867 return constant_boolean_node (false, truth_type);
2868 else
2870 enum tree_code tcode;
2872 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2873 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2877 /* Return nonzero if two operands (typically of the same tree node)
2878 are necessarily equal. FLAGS modifies behavior as follows:
2880 If OEP_ONLY_CONST is set, only return nonzero for constants.
2881 This function tests whether the operands are indistinguishable;
2882 it does not test whether they are equal using C's == operation.
2883 The distinction is important for IEEE floating point, because
2884 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2885 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2887 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2888 even though it may hold multiple values during a function.
2889 This is because a GCC tree node guarantees that nothing else is
2890 executed between the evaluation of its "operands" (which may often
2891 be evaluated in arbitrary order). Hence if the operands themselves
2892 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2893 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2894 unset means assuming isochronic (or instantaneous) tree equivalence.
2895 Unless comparing arbitrary expression trees, such as from different
2896 statements, this flag can usually be left unset.
2898 If OEP_PURE_SAME is set, then pure functions with identical arguments
2899 are considered the same. It is used when the caller has other ways
2900 to ensure that global memory is unchanged in between.
2902 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2903 not values of expressions.
2905 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2906 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2908 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2909 any operand with side effect. This is unnecesarily conservative in the
2910 case we know that arg0 and arg1 are in disjoint code paths (such as in
2911 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2912 addresses with TREE_CONSTANT flag set so we know that &var == &var
2913 even if var is volatile. */
2916 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2918 /* When checking, verify at the outermost operand_equal_p call that
2919 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2920 hash value. */
2921 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2923 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2925 if (arg0 != arg1)
2927 inchash::hash hstate0 (0), hstate1 (0);
2928 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2929 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2930 hashval_t h0 = hstate0.end ();
2931 hashval_t h1 = hstate1.end ();
2932 gcc_assert (h0 == h1);
2934 return 1;
2936 else
2937 return 0;
2940 /* If either is ERROR_MARK, they aren't equal. */
2941 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2942 || TREE_TYPE (arg0) == error_mark_node
2943 || TREE_TYPE (arg1) == error_mark_node)
2944 return 0;
2946 /* Similar, if either does not have a type (like a released SSA name),
2947 they aren't equal. */
2948 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2949 return 0;
2951 /* We cannot consider pointers to different address space equal. */
2952 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2953 && POINTER_TYPE_P (TREE_TYPE (arg1))
2954 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2955 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2956 return 0;
2958 /* Check equality of integer constants before bailing out due to
2959 precision differences. */
2960 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2962 /* Address of INTEGER_CST is not defined; check that we did not forget
2963 to drop the OEP_ADDRESS_OF flags. */
2964 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2965 return tree_int_cst_equal (arg0, arg1);
2968 if (!(flags & OEP_ADDRESS_OF))
2970 /* If both types don't have the same signedness, then we can't consider
2971 them equal. We must check this before the STRIP_NOPS calls
2972 because they may change the signedness of the arguments. As pointers
2973 strictly don't have a signedness, require either two pointers or
2974 two non-pointers as well. */
2975 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
2976 || POINTER_TYPE_P (TREE_TYPE (arg0))
2977 != POINTER_TYPE_P (TREE_TYPE (arg1)))
2978 return 0;
2980 /* If both types don't have the same precision, then it is not safe
2981 to strip NOPs. */
2982 if (element_precision (TREE_TYPE (arg0))
2983 != element_precision (TREE_TYPE (arg1)))
2984 return 0;
2986 STRIP_NOPS (arg0);
2987 STRIP_NOPS (arg1);
2989 #if 0
2990 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
2991 sanity check once the issue is solved. */
2992 else
2993 /* Addresses of conversions and SSA_NAMEs (and many other things)
2994 are not defined. Check that we did not forget to drop the
2995 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
2996 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
2997 && TREE_CODE (arg0) != SSA_NAME);
2998 #endif
3000 /* In case both args are comparisons but with different comparison
3001 code, try to swap the comparison operands of one arg to produce
3002 a match and compare that variant. */
3003 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3004 && COMPARISON_CLASS_P (arg0)
3005 && COMPARISON_CLASS_P (arg1))
3007 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3009 if (TREE_CODE (arg0) == swap_code)
3010 return operand_equal_p (TREE_OPERAND (arg0, 0),
3011 TREE_OPERAND (arg1, 1), flags)
3012 && operand_equal_p (TREE_OPERAND (arg0, 1),
3013 TREE_OPERAND (arg1, 0), flags);
3016 if (TREE_CODE (arg0) != TREE_CODE (arg1))
3018 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3019 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3021 else if (flags & OEP_ADDRESS_OF)
3023 /* If we are interested in comparing addresses ignore
3024 MEM_REF wrappings of the base that can appear just for
3025 TBAA reasons. */
3026 if (TREE_CODE (arg0) == MEM_REF
3027 && DECL_P (arg1)
3028 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3029 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3030 && integer_zerop (TREE_OPERAND (arg0, 1)))
3031 return 1;
3032 else if (TREE_CODE (arg1) == MEM_REF
3033 && DECL_P (arg0)
3034 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3035 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3036 && integer_zerop (TREE_OPERAND (arg1, 1)))
3037 return 1;
3038 return 0;
3040 else
3041 return 0;
3044 /* When not checking adddresses, this is needed for conversions and for
3045 COMPONENT_REF. Might as well play it safe and always test this. */
3046 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3047 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3048 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
3049 && !(flags & OEP_ADDRESS_OF)))
3050 return 0;
3052 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3053 We don't care about side effects in that case because the SAVE_EXPR
3054 takes care of that for us. In all other cases, two expressions are
3055 equal if they have no side effects. If we have two identical
3056 expressions with side effects that should be treated the same due
3057 to the only side effects being identical SAVE_EXPR's, that will
3058 be detected in the recursive calls below.
3059 If we are taking an invariant address of two identical objects
3060 they are necessarily equal as well. */
3061 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3062 && (TREE_CODE (arg0) == SAVE_EXPR
3063 || (flags & OEP_MATCH_SIDE_EFFECTS)
3064 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3065 return 1;
3067 /* Next handle constant cases, those for which we can return 1 even
3068 if ONLY_CONST is set. */
3069 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3070 switch (TREE_CODE (arg0))
3072 case INTEGER_CST:
3073 return tree_int_cst_equal (arg0, arg1);
3075 case FIXED_CST:
3076 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3077 TREE_FIXED_CST (arg1));
3079 case REAL_CST:
3080 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3081 return 1;
3084 if (!HONOR_SIGNED_ZEROS (arg0))
3086 /* If we do not distinguish between signed and unsigned zero,
3087 consider them equal. */
3088 if (real_zerop (arg0) && real_zerop (arg1))
3089 return 1;
3091 return 0;
3093 case VECTOR_CST:
3095 if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3096 != VECTOR_CST_LOG2_NPATTERNS (arg1))
3097 return 0;
3099 if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3100 != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3101 return 0;
3103 unsigned int count = vector_cst_encoded_nelts (arg0);
3104 for (unsigned int i = 0; i < count; ++i)
3105 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3106 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3107 return 0;
3108 return 1;
3111 case COMPLEX_CST:
3112 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3113 flags)
3114 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3115 flags));
3117 case STRING_CST:
3118 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3119 && ! memcmp (TREE_STRING_POINTER (arg0),
3120 TREE_STRING_POINTER (arg1),
3121 TREE_STRING_LENGTH (arg0)));
3123 case ADDR_EXPR:
3124 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3125 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3126 flags | OEP_ADDRESS_OF
3127 | OEP_MATCH_SIDE_EFFECTS);
3128 case CONSTRUCTOR:
3129 /* In GIMPLE empty constructors are allowed in initializers of
3130 aggregates. */
3131 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
3132 default:
3133 break;
3136 if (flags & OEP_ONLY_CONST)
3137 return 0;
3139 /* Define macros to test an operand from arg0 and arg1 for equality and a
3140 variant that allows null and views null as being different from any
3141 non-null value. In the latter case, if either is null, the both
3142 must be; otherwise, do the normal comparison. */
3143 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3144 TREE_OPERAND (arg1, N), flags)
3146 #define OP_SAME_WITH_NULL(N) \
3147 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3148 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3150 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3152 case tcc_unary:
3153 /* Two conversions are equal only if signedness and modes match. */
3154 switch (TREE_CODE (arg0))
3156 CASE_CONVERT:
3157 case FIX_TRUNC_EXPR:
3158 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3159 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3160 return 0;
3161 break;
3162 default:
3163 break;
3166 return OP_SAME (0);
3169 case tcc_comparison:
3170 case tcc_binary:
3171 if (OP_SAME (0) && OP_SAME (1))
3172 return 1;
3174 /* For commutative ops, allow the other order. */
3175 return (commutative_tree_code (TREE_CODE (arg0))
3176 && operand_equal_p (TREE_OPERAND (arg0, 0),
3177 TREE_OPERAND (arg1, 1), flags)
3178 && operand_equal_p (TREE_OPERAND (arg0, 1),
3179 TREE_OPERAND (arg1, 0), flags));
3181 case tcc_reference:
3182 /* If either of the pointer (or reference) expressions we are
3183 dereferencing contain a side effect, these cannot be equal,
3184 but their addresses can be. */
3185 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3186 && (TREE_SIDE_EFFECTS (arg0)
3187 || TREE_SIDE_EFFECTS (arg1)))
3188 return 0;
3190 switch (TREE_CODE (arg0))
3192 case INDIRECT_REF:
3193 if (!(flags & OEP_ADDRESS_OF)
3194 && (TYPE_ALIGN (TREE_TYPE (arg0))
3195 != TYPE_ALIGN (TREE_TYPE (arg1))))
3196 return 0;
3197 flags &= ~OEP_ADDRESS_OF;
3198 return OP_SAME (0);
3200 case IMAGPART_EXPR:
3201 /* Require the same offset. */
3202 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3203 TYPE_SIZE (TREE_TYPE (arg1)),
3204 flags & ~OEP_ADDRESS_OF))
3205 return 0;
3207 /* Fallthru. */
3208 case REALPART_EXPR:
3209 case VIEW_CONVERT_EXPR:
3210 return OP_SAME (0);
3212 case TARGET_MEM_REF:
3213 case MEM_REF:
3214 if (!(flags & OEP_ADDRESS_OF))
3216 /* Require equal access sizes */
3217 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3218 && (!TYPE_SIZE (TREE_TYPE (arg0))
3219 || !TYPE_SIZE (TREE_TYPE (arg1))
3220 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3221 TYPE_SIZE (TREE_TYPE (arg1)),
3222 flags)))
3223 return 0;
3224 /* Verify that access happens in similar types. */
3225 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3226 return 0;
3227 /* Verify that accesses are TBAA compatible. */
3228 if (!alias_ptr_types_compatible_p
3229 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3230 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3231 || (MR_DEPENDENCE_CLIQUE (arg0)
3232 != MR_DEPENDENCE_CLIQUE (arg1))
3233 || (MR_DEPENDENCE_BASE (arg0)
3234 != MR_DEPENDENCE_BASE (arg1)))
3235 return 0;
3236 /* Verify that alignment is compatible. */
3237 if (TYPE_ALIGN (TREE_TYPE (arg0))
3238 != TYPE_ALIGN (TREE_TYPE (arg1)))
3239 return 0;
3241 flags &= ~OEP_ADDRESS_OF;
3242 return (OP_SAME (0) && OP_SAME (1)
3243 /* TARGET_MEM_REF require equal extra operands. */
3244 && (TREE_CODE (arg0) != TARGET_MEM_REF
3245 || (OP_SAME_WITH_NULL (2)
3246 && OP_SAME_WITH_NULL (3)
3247 && OP_SAME_WITH_NULL (4))));
3249 case ARRAY_REF:
3250 case ARRAY_RANGE_REF:
3251 if (!OP_SAME (0))
3252 return 0;
3253 flags &= ~OEP_ADDRESS_OF;
3254 /* Compare the array index by value if it is constant first as we
3255 may have different types but same value here. */
3256 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3257 TREE_OPERAND (arg1, 1))
3258 || OP_SAME (1))
3259 && OP_SAME_WITH_NULL (2)
3260 && OP_SAME_WITH_NULL (3)
3261 /* Compare low bound and element size as with OEP_ADDRESS_OF
3262 we have to account for the offset of the ref. */
3263 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3264 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3265 || (operand_equal_p (array_ref_low_bound
3266 (CONST_CAST_TREE (arg0)),
3267 array_ref_low_bound
3268 (CONST_CAST_TREE (arg1)), flags)
3269 && operand_equal_p (array_ref_element_size
3270 (CONST_CAST_TREE (arg0)),
3271 array_ref_element_size
3272 (CONST_CAST_TREE (arg1)),
3273 flags))));
3275 case COMPONENT_REF:
3276 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3277 may be NULL when we're called to compare MEM_EXPRs. */
3278 if (!OP_SAME_WITH_NULL (0)
3279 || !OP_SAME (1))
3280 return 0;
3281 flags &= ~OEP_ADDRESS_OF;
3282 return OP_SAME_WITH_NULL (2);
3284 case BIT_FIELD_REF:
3285 if (!OP_SAME (0))
3286 return 0;
3287 flags &= ~OEP_ADDRESS_OF;
3288 return OP_SAME (1) && OP_SAME (2);
3290 default:
3291 return 0;
3294 case tcc_expression:
3295 switch (TREE_CODE (arg0))
3297 case ADDR_EXPR:
3298 /* Be sure we pass right ADDRESS_OF flag. */
3299 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3300 return operand_equal_p (TREE_OPERAND (arg0, 0),
3301 TREE_OPERAND (arg1, 0),
3302 flags | OEP_ADDRESS_OF);
3304 case TRUTH_NOT_EXPR:
3305 return OP_SAME (0);
3307 case TRUTH_ANDIF_EXPR:
3308 case TRUTH_ORIF_EXPR:
3309 return OP_SAME (0) && OP_SAME (1);
3311 case WIDEN_MULT_PLUS_EXPR:
3312 case WIDEN_MULT_MINUS_EXPR:
3313 if (!OP_SAME (2))
3314 return 0;
3315 /* The multiplcation operands are commutative. */
3316 /* FALLTHRU */
3318 case TRUTH_AND_EXPR:
3319 case TRUTH_OR_EXPR:
3320 case TRUTH_XOR_EXPR:
3321 if (OP_SAME (0) && OP_SAME (1))
3322 return 1;
3324 /* Otherwise take into account this is a commutative operation. */
3325 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3326 TREE_OPERAND (arg1, 1), flags)
3327 && operand_equal_p (TREE_OPERAND (arg0, 1),
3328 TREE_OPERAND (arg1, 0), flags));
3330 case COND_EXPR:
3331 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3332 return 0;
3333 flags &= ~OEP_ADDRESS_OF;
3334 return OP_SAME (0);
3336 case BIT_INSERT_EXPR:
3337 /* BIT_INSERT_EXPR has an implict operand as the type precision
3338 of op1. Need to check to make sure they are the same. */
3339 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3340 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3341 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3342 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3343 return false;
3344 /* FALLTHRU */
3346 case VEC_COND_EXPR:
3347 case DOT_PROD_EXPR:
3348 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3350 case MODIFY_EXPR:
3351 case INIT_EXPR:
3352 case COMPOUND_EXPR:
3353 case PREDECREMENT_EXPR:
3354 case PREINCREMENT_EXPR:
3355 case POSTDECREMENT_EXPR:
3356 case POSTINCREMENT_EXPR:
3357 if (flags & OEP_LEXICOGRAPHIC)
3358 return OP_SAME (0) && OP_SAME (1);
3359 return 0;
3361 case CLEANUP_POINT_EXPR:
3362 case EXPR_STMT:
3363 case SAVE_EXPR:
3364 if (flags & OEP_LEXICOGRAPHIC)
3365 return OP_SAME (0);
3366 return 0;
3368 default:
3369 return 0;
3372 case tcc_vl_exp:
3373 switch (TREE_CODE (arg0))
3375 case CALL_EXPR:
3376 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3377 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3378 /* If not both CALL_EXPRs are either internal or normal function
3379 functions, then they are not equal. */
3380 return 0;
3381 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3383 /* If the CALL_EXPRs call different internal functions, then they
3384 are not equal. */
3385 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3386 return 0;
3388 else
3390 /* If the CALL_EXPRs call different functions, then they are not
3391 equal. */
3392 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3393 flags))
3394 return 0;
3397 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3399 unsigned int cef = call_expr_flags (arg0);
3400 if (flags & OEP_PURE_SAME)
3401 cef &= ECF_CONST | ECF_PURE;
3402 else
3403 cef &= ECF_CONST;
3404 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3405 return 0;
3408 /* Now see if all the arguments are the same. */
3410 const_call_expr_arg_iterator iter0, iter1;
3411 const_tree a0, a1;
3412 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3413 a1 = first_const_call_expr_arg (arg1, &iter1);
3414 a0 && a1;
3415 a0 = next_const_call_expr_arg (&iter0),
3416 a1 = next_const_call_expr_arg (&iter1))
3417 if (! operand_equal_p (a0, a1, flags))
3418 return 0;
3420 /* If we get here and both argument lists are exhausted
3421 then the CALL_EXPRs are equal. */
3422 return ! (a0 || a1);
3424 default:
3425 return 0;
3428 case tcc_declaration:
3429 /* Consider __builtin_sqrt equal to sqrt. */
3430 return (TREE_CODE (arg0) == FUNCTION_DECL
3431 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3432 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3433 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3435 case tcc_exceptional:
3436 if (TREE_CODE (arg0) == CONSTRUCTOR)
3438 /* In GIMPLE constructors are used only to build vectors from
3439 elements. Individual elements in the constructor must be
3440 indexed in increasing order and form an initial sequence.
3442 We make no effort to compare constructors in generic.
3443 (see sem_variable::equals in ipa-icf which can do so for
3444 constants). */
3445 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3446 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3447 return 0;
3449 /* Be sure that vectors constructed have the same representation.
3450 We only tested element precision and modes to match.
3451 Vectors may be BLKmode and thus also check that the number of
3452 parts match. */
3453 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
3454 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))))
3455 return 0;
3457 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3458 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3459 unsigned int len = vec_safe_length (v0);
3461 if (len != vec_safe_length (v1))
3462 return 0;
3464 for (unsigned int i = 0; i < len; i++)
3466 constructor_elt *c0 = &(*v0)[i];
3467 constructor_elt *c1 = &(*v1)[i];
3469 if (!operand_equal_p (c0->value, c1->value, flags)
3470 /* In GIMPLE the indexes can be either NULL or matching i.
3471 Double check this so we won't get false
3472 positives for GENERIC. */
3473 || (c0->index
3474 && (TREE_CODE (c0->index) != INTEGER_CST
3475 || !compare_tree_int (c0->index, i)))
3476 || (c1->index
3477 && (TREE_CODE (c1->index) != INTEGER_CST
3478 || !compare_tree_int (c1->index, i))))
3479 return 0;
3481 return 1;
3483 else if (TREE_CODE (arg0) == STATEMENT_LIST
3484 && (flags & OEP_LEXICOGRAPHIC))
3486 /* Compare the STATEMENT_LISTs. */
3487 tree_stmt_iterator tsi1, tsi2;
3488 tree body1 = CONST_CAST_TREE (arg0);
3489 tree body2 = CONST_CAST_TREE (arg1);
3490 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3491 tsi_next (&tsi1), tsi_next (&tsi2))
3493 /* The lists don't have the same number of statements. */
3494 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3495 return 0;
3496 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3497 return 1;
3498 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3499 flags & (OEP_LEXICOGRAPHIC
3500 | OEP_NO_HASH_CHECK)))
3501 return 0;
3504 return 0;
3506 case tcc_statement:
3507 switch (TREE_CODE (arg0))
3509 case RETURN_EXPR:
3510 if (flags & OEP_LEXICOGRAPHIC)
3511 return OP_SAME_WITH_NULL (0);
3512 return 0;
3513 case DEBUG_BEGIN_STMT:
3514 if (flags & OEP_LEXICOGRAPHIC)
3515 return 1;
3516 return 0;
3517 default:
3518 return 0;
3521 default:
3522 return 0;
3525 #undef OP_SAME
3526 #undef OP_SAME_WITH_NULL
3529 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
3530 with a different signedness or a narrower precision. */
3532 static bool
3533 operand_equal_for_comparison_p (tree arg0, tree arg1)
3535 if (operand_equal_p (arg0, arg1, 0))
3536 return true;
3538 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3539 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3540 return false;
3542 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3543 and see if the inner values are the same. This removes any
3544 signedness comparison, which doesn't matter here. */
3545 tree op0 = arg0;
3546 tree op1 = arg1;
3547 STRIP_NOPS (op0);
3548 STRIP_NOPS (op1);
3549 if (operand_equal_p (op0, op1, 0))
3550 return true;
3552 /* Discard a single widening conversion from ARG1 and see if the inner
3553 value is the same as ARG0. */
3554 if (CONVERT_EXPR_P (arg1)
3555 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3556 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3557 < TYPE_PRECISION (TREE_TYPE (arg1))
3558 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
3559 return true;
3561 return false;
3564 /* See if ARG is an expression that is either a comparison or is performing
3565 arithmetic on comparisons. The comparisons must only be comparing
3566 two different values, which will be stored in *CVAL1 and *CVAL2; if
3567 they are nonzero it means that some operands have already been found.
3568 No variables may be used anywhere else in the expression except in the
3569 comparisons.
3571 If this is true, return 1. Otherwise, return zero. */
3573 static int
3574 twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
3576 enum tree_code code = TREE_CODE (arg);
3577 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3579 /* We can handle some of the tcc_expression cases here. */
3580 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3581 tclass = tcc_unary;
3582 else if (tclass == tcc_expression
3583 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3584 || code == COMPOUND_EXPR))
3585 tclass = tcc_binary;
3587 switch (tclass)
3589 case tcc_unary:
3590 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
3592 case tcc_binary:
3593 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
3594 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
3596 case tcc_constant:
3597 return 1;
3599 case tcc_expression:
3600 if (code == COND_EXPR)
3601 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
3602 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
3603 && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
3604 return 0;
3606 case tcc_comparison:
3607 /* First see if we can handle the first operand, then the second. For
3608 the second operand, we know *CVAL1 can't be zero. It must be that
3609 one side of the comparison is each of the values; test for the
3610 case where this isn't true by failing if the two operands
3611 are the same. */
3613 if (operand_equal_p (TREE_OPERAND (arg, 0),
3614 TREE_OPERAND (arg, 1), 0))
3615 return 0;
3617 if (*cval1 == 0)
3618 *cval1 = TREE_OPERAND (arg, 0);
3619 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3621 else if (*cval2 == 0)
3622 *cval2 = TREE_OPERAND (arg, 0);
3623 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3625 else
3626 return 0;
3628 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3630 else if (*cval2 == 0)
3631 *cval2 = TREE_OPERAND (arg, 1);
3632 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3634 else
3635 return 0;
3637 return 1;
3639 default:
3640 return 0;
3644 /* ARG is a tree that is known to contain just arithmetic operations and
3645 comparisons. Evaluate the operations in the tree substituting NEW0 for
3646 any occurrence of OLD0 as an operand of a comparison and likewise for
3647 NEW1 and OLD1. */
3649 static tree
3650 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3651 tree old1, tree new1)
3653 tree type = TREE_TYPE (arg);
3654 enum tree_code code = TREE_CODE (arg);
3655 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3657 /* We can handle some of the tcc_expression cases here. */
3658 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3659 tclass = tcc_unary;
3660 else if (tclass == tcc_expression
3661 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3662 tclass = tcc_binary;
3664 switch (tclass)
3666 case tcc_unary:
3667 return fold_build1_loc (loc, code, type,
3668 eval_subst (loc, TREE_OPERAND (arg, 0),
3669 old0, new0, old1, new1));
3671 case tcc_binary:
3672 return fold_build2_loc (loc, code, type,
3673 eval_subst (loc, TREE_OPERAND (arg, 0),
3674 old0, new0, old1, new1),
3675 eval_subst (loc, TREE_OPERAND (arg, 1),
3676 old0, new0, old1, new1));
3678 case tcc_expression:
3679 switch (code)
3681 case SAVE_EXPR:
3682 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3683 old1, new1);
3685 case COMPOUND_EXPR:
3686 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3687 old1, new1);
3689 case COND_EXPR:
3690 return fold_build3_loc (loc, code, type,
3691 eval_subst (loc, TREE_OPERAND (arg, 0),
3692 old0, new0, old1, new1),
3693 eval_subst (loc, TREE_OPERAND (arg, 1),
3694 old0, new0, old1, new1),
3695 eval_subst (loc, TREE_OPERAND (arg, 2),
3696 old0, new0, old1, new1));
3697 default:
3698 break;
3700 /* Fall through - ??? */
3702 case tcc_comparison:
3704 tree arg0 = TREE_OPERAND (arg, 0);
3705 tree arg1 = TREE_OPERAND (arg, 1);
3707 /* We need to check both for exact equality and tree equality. The
3708 former will be true if the operand has a side-effect. In that
3709 case, we know the operand occurred exactly once. */
3711 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3712 arg0 = new0;
3713 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3714 arg0 = new1;
3716 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3717 arg1 = new0;
3718 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3719 arg1 = new1;
3721 return fold_build2_loc (loc, code, type, arg0, arg1);
3724 default:
3725 return arg;
3729 /* Return a tree for the case when the result of an expression is RESULT
3730 converted to TYPE and OMITTED was previously an operand of the expression
3731 but is now not needed (e.g., we folded OMITTED * 0).
3733 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3734 the conversion of RESULT to TYPE. */
3736 tree
3737 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3739 tree t = fold_convert_loc (loc, type, result);
3741 /* If the resulting operand is an empty statement, just return the omitted
3742 statement casted to void. */
3743 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3744 return build1_loc (loc, NOP_EXPR, void_type_node,
3745 fold_ignored_result (omitted));
3747 if (TREE_SIDE_EFFECTS (omitted))
3748 return build2_loc (loc, COMPOUND_EXPR, type,
3749 fold_ignored_result (omitted), t);
3751 return non_lvalue_loc (loc, t);
3754 /* Return a tree for the case when the result of an expression is RESULT
3755 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3756 of the expression but are now not needed.
3758 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3759 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3760 evaluated before OMITTED2. Otherwise, if neither has side effects,
3761 just do the conversion of RESULT to TYPE. */
3763 tree
3764 omit_two_operands_loc (location_t loc, tree type, tree result,
3765 tree omitted1, tree omitted2)
3767 tree t = fold_convert_loc (loc, type, result);
3769 if (TREE_SIDE_EFFECTS (omitted2))
3770 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3771 if (TREE_SIDE_EFFECTS (omitted1))
3772 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3774 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3778 /* Return a simplified tree node for the truth-negation of ARG. This
3779 never alters ARG itself. We assume that ARG is an operation that
3780 returns a truth value (0 or 1).
3782 FIXME: one would think we would fold the result, but it causes
3783 problems with the dominator optimizer. */
3785 static tree
3786 fold_truth_not_expr (location_t loc, tree arg)
3788 tree type = TREE_TYPE (arg);
3789 enum tree_code code = TREE_CODE (arg);
3790 location_t loc1, loc2;
3792 /* If this is a comparison, we can simply invert it, except for
3793 floating-point non-equality comparisons, in which case we just
3794 enclose a TRUTH_NOT_EXPR around what we have. */
3796 if (TREE_CODE_CLASS (code) == tcc_comparison)
3798 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3799 if (FLOAT_TYPE_P (op_type)
3800 && flag_trapping_math
3801 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3802 && code != NE_EXPR && code != EQ_EXPR)
3803 return NULL_TREE;
3805 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3806 if (code == ERROR_MARK)
3807 return NULL_TREE;
3809 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3810 TREE_OPERAND (arg, 1));
3811 if (TREE_NO_WARNING (arg))
3812 TREE_NO_WARNING (ret) = 1;
3813 return ret;
3816 switch (code)
3818 case INTEGER_CST:
3819 return constant_boolean_node (integer_zerop (arg), type);
3821 case TRUTH_AND_EXPR:
3822 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3823 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3824 return build2_loc (loc, TRUTH_OR_EXPR, type,
3825 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3826 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3828 case TRUTH_OR_EXPR:
3829 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3830 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3831 return build2_loc (loc, TRUTH_AND_EXPR, type,
3832 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3833 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3835 case TRUTH_XOR_EXPR:
3836 /* Here we can invert either operand. We invert the first operand
3837 unless the second operand is a TRUTH_NOT_EXPR in which case our
3838 result is the XOR of the first operand with the inside of the
3839 negation of the second operand. */
3841 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3842 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3843 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3844 else
3845 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3846 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3847 TREE_OPERAND (arg, 1));
3849 case TRUTH_ANDIF_EXPR:
3850 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3851 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3852 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3853 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3854 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3856 case TRUTH_ORIF_EXPR:
3857 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3858 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3859 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3860 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3861 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3863 case TRUTH_NOT_EXPR:
3864 return TREE_OPERAND (arg, 0);
3866 case COND_EXPR:
3868 tree arg1 = TREE_OPERAND (arg, 1);
3869 tree arg2 = TREE_OPERAND (arg, 2);
3871 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3872 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3874 /* A COND_EXPR may have a throw as one operand, which
3875 then has void type. Just leave void operands
3876 as they are. */
3877 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3878 VOID_TYPE_P (TREE_TYPE (arg1))
3879 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3880 VOID_TYPE_P (TREE_TYPE (arg2))
3881 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3884 case COMPOUND_EXPR:
3885 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3886 return build2_loc (loc, COMPOUND_EXPR, type,
3887 TREE_OPERAND (arg, 0),
3888 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3890 case NON_LVALUE_EXPR:
3891 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3892 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3894 CASE_CONVERT:
3895 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3896 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3898 /* fall through */
3900 case FLOAT_EXPR:
3901 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3902 return build1_loc (loc, TREE_CODE (arg), type,
3903 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3905 case BIT_AND_EXPR:
3906 if (!integer_onep (TREE_OPERAND (arg, 1)))
3907 return NULL_TREE;
3908 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3910 case SAVE_EXPR:
3911 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3913 case CLEANUP_POINT_EXPR:
3914 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3915 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3916 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3918 default:
3919 return NULL_TREE;
3923 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3924 assume that ARG is an operation that returns a truth value (0 or 1
3925 for scalars, 0 or -1 for vectors). Return the folded expression if
3926 folding is successful. Otherwise, return NULL_TREE. */
3928 static tree
3929 fold_invert_truthvalue (location_t loc, tree arg)
3931 tree type = TREE_TYPE (arg);
3932 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3933 ? BIT_NOT_EXPR
3934 : TRUTH_NOT_EXPR,
3935 type, arg);
3938 /* Return a simplified tree node for the truth-negation of ARG. This
3939 never alters ARG itself. We assume that ARG is an operation that
3940 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3942 tree
3943 invert_truthvalue_loc (location_t loc, tree arg)
3945 if (TREE_CODE (arg) == ERROR_MARK)
3946 return arg;
3948 tree type = TREE_TYPE (arg);
3949 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3950 ? BIT_NOT_EXPR
3951 : TRUTH_NOT_EXPR,
3952 type, arg);
3955 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3956 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3957 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3958 is the original memory reference used to preserve the alias set of
3959 the access. */
3961 static tree
3962 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
3963 HOST_WIDE_INT bitsize, poly_int64 bitpos,
3964 int unsignedp, int reversep)
3966 tree result, bftype;
3968 /* Attempt not to lose the access path if possible. */
3969 if (TREE_CODE (orig_inner) == COMPONENT_REF)
3971 tree ninner = TREE_OPERAND (orig_inner, 0);
3972 machine_mode nmode;
3973 poly_int64 nbitsize, nbitpos;
3974 tree noffset;
3975 int nunsignedp, nreversep, nvolatilep = 0;
3976 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
3977 &noffset, &nmode, &nunsignedp,
3978 &nreversep, &nvolatilep);
3979 if (base == inner
3980 && noffset == NULL_TREE
3981 && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
3982 && !reversep
3983 && !nreversep
3984 && !nvolatilep)
3986 inner = ninner;
3987 bitpos -= nbitpos;
3991 alias_set_type iset = get_alias_set (orig_inner);
3992 if (iset == 0 && get_alias_set (inner) != iset)
3993 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
3994 build_fold_addr_expr (inner),
3995 build_int_cst (ptr_type_node, 0));
3997 if (known_eq (bitpos, 0) && !reversep)
3999 tree size = TYPE_SIZE (TREE_TYPE (inner));
4000 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4001 || POINTER_TYPE_P (TREE_TYPE (inner)))
4002 && tree_fits_shwi_p (size)
4003 && tree_to_shwi (size) == bitsize)
4004 return fold_convert_loc (loc, type, inner);
4007 bftype = type;
4008 if (TYPE_PRECISION (bftype) != bitsize
4009 || TYPE_UNSIGNED (bftype) == !unsignedp)
4010 bftype = build_nonstandard_integer_type (bitsize, 0);
4012 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4013 bitsize_int (bitsize), bitsize_int (bitpos));
4014 REF_REVERSE_STORAGE_ORDER (result) = reversep;
4016 if (bftype != type)
4017 result = fold_convert_loc (loc, type, result);
4019 return result;
4022 /* Optimize a bit-field compare.
4024 There are two cases: First is a compare against a constant and the
4025 second is a comparison of two items where the fields are at the same
4026 bit position relative to the start of a chunk (byte, halfword, word)
4027 large enough to contain it. In these cases we can avoid the shift
4028 implicit in bitfield extractions.
4030 For constants, we emit a compare of the shifted constant with the
4031 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4032 compared. For two fields at the same position, we do the ANDs with the
4033 similar mask and compare the result of the ANDs.
4035 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4036 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4037 are the left and right operands of the comparison, respectively.
4039 If the optimization described above can be done, we return the resulting
4040 tree. Otherwise we return zero. */
4042 static tree
4043 optimize_bit_field_compare (location_t loc, enum tree_code code,
4044 tree compare_type, tree lhs, tree rhs)
4046 poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4047 HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4048 tree type = TREE_TYPE (lhs);
4049 tree unsigned_type;
4050 int const_p = TREE_CODE (rhs) == INTEGER_CST;
4051 machine_mode lmode, rmode;
4052 scalar_int_mode nmode;
4053 int lunsignedp, runsignedp;
4054 int lreversep, rreversep;
4055 int lvolatilep = 0, rvolatilep = 0;
4056 tree linner, rinner = NULL_TREE;
4057 tree mask;
4058 tree offset;
4060 /* Get all the information about the extractions being done. If the bit size
4061 is the same as the size of the underlying object, we aren't doing an
4062 extraction at all and so can do nothing. We also don't want to
4063 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4064 then will no longer be able to replace it. */
4065 linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4066 &lunsignedp, &lreversep, &lvolatilep);
4067 if (linner == lhs
4068 || !known_size_p (plbitsize)
4069 || !plbitsize.is_constant (&lbitsize)
4070 || !plbitpos.is_constant (&lbitpos)
4071 || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4072 || offset != 0
4073 || TREE_CODE (linner) == PLACEHOLDER_EXPR
4074 || lvolatilep)
4075 return 0;
4077 if (const_p)
4078 rreversep = lreversep;
4079 else
4081 /* If this is not a constant, we can only do something if bit positions,
4082 sizes, signedness and storage order are the same. */
4083 rinner
4084 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4085 &runsignedp, &rreversep, &rvolatilep);
4087 if (rinner == rhs
4088 || maybe_ne (lbitpos, rbitpos)
4089 || maybe_ne (lbitsize, rbitsize)
4090 || lunsignedp != runsignedp
4091 || lreversep != rreversep
4092 || offset != 0
4093 || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4094 || rvolatilep)
4095 return 0;
4098 /* Honor the C++ memory model and mimic what RTL expansion does. */
4099 poly_uint64 bitstart = 0;
4100 poly_uint64 bitend = 0;
4101 if (TREE_CODE (lhs) == COMPONENT_REF)
4103 get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4104 if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4105 return 0;
4108 /* See if we can find a mode to refer to this field. We should be able to,
4109 but fail if we can't. */
4110 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4111 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4112 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4113 TYPE_ALIGN (TREE_TYPE (rinner))),
4114 BITS_PER_WORD, false, &nmode))
4115 return 0;
4117 /* Set signed and unsigned types of the precision of this mode for the
4118 shifts below. */
4119 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4121 /* Compute the bit position and size for the new reference and our offset
4122 within it. If the new reference is the same size as the original, we
4123 won't optimize anything, so return zero. */
4124 nbitsize = GET_MODE_BITSIZE (nmode);
4125 nbitpos = lbitpos & ~ (nbitsize - 1);
4126 lbitpos -= nbitpos;
4127 if (nbitsize == lbitsize)
4128 return 0;
4130 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4131 lbitpos = nbitsize - lbitsize - lbitpos;
4133 /* Make the mask to be used against the extracted field. */
4134 mask = build_int_cst_type (unsigned_type, -1);
4135 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4136 mask = const_binop (RSHIFT_EXPR, mask,
4137 size_int (nbitsize - lbitsize - lbitpos));
4139 if (! const_p)
4141 if (nbitpos < 0)
4142 return 0;
4144 /* If not comparing with constant, just rework the comparison
4145 and return. */
4146 tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4147 nbitsize, nbitpos, 1, lreversep);
4148 t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4149 tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4150 nbitsize, nbitpos, 1, rreversep);
4151 t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4152 return fold_build2_loc (loc, code, compare_type, t1, t2);
4155 /* Otherwise, we are handling the constant case. See if the constant is too
4156 big for the field. Warn and return a tree for 0 (false) if so. We do
4157 this not only for its own sake, but to avoid having to test for this
4158 error case below. If we didn't, we might generate wrong code.
4160 For unsigned fields, the constant shifted right by the field length should
4161 be all zero. For signed fields, the high-order bits should agree with
4162 the sign bit. */
4164 if (lunsignedp)
4166 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
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);
4173 else
4175 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4176 if (tem != 0 && tem != -1)
4178 warning (0, "comparison is always %d due to width of bit-field",
4179 code == NE_EXPR);
4180 return constant_boolean_node (code == NE_EXPR, compare_type);
4184 if (nbitpos < 0)
4185 return 0;
4187 /* Single-bit compares should always be against zero. */
4188 if (lbitsize == 1 && ! integer_zerop (rhs))
4190 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4191 rhs = build_int_cst (type, 0);
4194 /* Make a new bitfield reference, shift the constant over the
4195 appropriate number of bits and mask it with the computed mask
4196 (in case this was a signed field). If we changed it, make a new one. */
4197 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4198 nbitsize, nbitpos, 1, lreversep);
4200 rhs = const_binop (BIT_AND_EXPR,
4201 const_binop (LSHIFT_EXPR,
4202 fold_convert_loc (loc, unsigned_type, rhs),
4203 size_int (lbitpos)),
4204 mask);
4206 lhs = build2_loc (loc, code, compare_type,
4207 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4208 return lhs;
4211 /* Subroutine for fold_truth_andor_1: decode a field reference.
4213 If EXP is a comparison reference, we return the innermost reference.
4215 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4216 set to the starting bit number.
4218 If the innermost field can be completely contained in a mode-sized
4219 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4221 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4222 otherwise it is not changed.
4224 *PUNSIGNEDP is set to the signedness of the field.
4226 *PREVERSEP is set to the storage order of the field.
4228 *PMASK is set to the mask used. This is either contained in a
4229 BIT_AND_EXPR or derived from the width of the field.
4231 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4233 Return 0 if this is not a component reference or is one that we can't
4234 do anything with. */
4236 static tree
4237 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4238 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4239 int *punsignedp, int *preversep, int *pvolatilep,
4240 tree *pmask, tree *pand_mask)
4242 tree exp = *exp_;
4243 tree outer_type = 0;
4244 tree and_mask = 0;
4245 tree mask, inner, offset;
4246 tree unsigned_type;
4247 unsigned int precision;
4249 /* All the optimizations using this function assume integer fields.
4250 There are problems with FP fields since the type_for_size call
4251 below can fail for, e.g., XFmode. */
4252 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4253 return 0;
4255 /* We are interested in the bare arrangement of bits, so strip everything
4256 that doesn't affect the machine mode. However, record the type of the
4257 outermost expression if it may matter below. */
4258 if (CONVERT_EXPR_P (exp)
4259 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4260 outer_type = TREE_TYPE (exp);
4261 STRIP_NOPS (exp);
4263 if (TREE_CODE (exp) == BIT_AND_EXPR)
4265 and_mask = TREE_OPERAND (exp, 1);
4266 exp = TREE_OPERAND (exp, 0);
4267 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4268 if (TREE_CODE (and_mask) != INTEGER_CST)
4269 return 0;
4272 poly_int64 poly_bitsize, poly_bitpos;
4273 inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
4274 pmode, punsignedp, preversep, pvolatilep);
4275 if ((inner == exp && and_mask == 0)
4276 || !poly_bitsize.is_constant (pbitsize)
4277 || !poly_bitpos.is_constant (pbitpos)
4278 || *pbitsize < 0
4279 || offset != 0
4280 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4281 /* Reject out-of-bound accesses (PR79731). */
4282 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4283 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4284 *pbitpos + *pbitsize) < 0))
4285 return 0;
4287 *exp_ = exp;
4289 /* If the number of bits in the reference is the same as the bitsize of
4290 the outer type, then the outer type gives the signedness. Otherwise
4291 (in case of a small bitfield) the signedness is unchanged. */
4292 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4293 *punsignedp = TYPE_UNSIGNED (outer_type);
4295 /* Compute the mask to access the bitfield. */
4296 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4297 precision = TYPE_PRECISION (unsigned_type);
4299 mask = build_int_cst_type (unsigned_type, -1);
4301 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4302 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4304 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4305 if (and_mask != 0)
4306 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4307 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4309 *pmask = mask;
4310 *pand_mask = and_mask;
4311 return inner;
4314 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4315 bit positions and MASK is SIGNED. */
4317 static int
4318 all_ones_mask_p (const_tree mask, unsigned int size)
4320 tree type = TREE_TYPE (mask);
4321 unsigned int precision = TYPE_PRECISION (type);
4323 /* If this function returns true when the type of the mask is
4324 UNSIGNED, then there will be errors. In particular see
4325 gcc.c-torture/execute/990326-1.c. There does not appear to be
4326 any documentation paper trail as to why this is so. But the pre
4327 wide-int worked with that restriction and it has been preserved
4328 here. */
4329 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4330 return false;
4332 return wi::mask (size, false, precision) == wi::to_wide (mask);
4335 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4336 represents the sign bit of EXP's type. If EXP represents a sign
4337 or zero extension, also test VAL against the unextended type.
4338 The return value is the (sub)expression whose sign bit is VAL,
4339 or NULL_TREE otherwise. */
4341 tree
4342 sign_bit_p (tree exp, const_tree val)
4344 int width;
4345 tree t;
4347 /* Tree EXP must have an integral type. */
4348 t = TREE_TYPE (exp);
4349 if (! INTEGRAL_TYPE_P (t))
4350 return NULL_TREE;
4352 /* Tree VAL must be an integer constant. */
4353 if (TREE_CODE (val) != INTEGER_CST
4354 || TREE_OVERFLOW (val))
4355 return NULL_TREE;
4357 width = TYPE_PRECISION (t);
4358 if (wi::only_sign_bit_p (wi::to_wide (val), width))
4359 return exp;
4361 /* Handle extension from a narrower type. */
4362 if (TREE_CODE (exp) == NOP_EXPR
4363 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4364 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4366 return NULL_TREE;
4369 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4370 to be evaluated unconditionally. */
4372 static int
4373 simple_operand_p (const_tree exp)
4375 /* Strip any conversions that don't change the machine mode. */
4376 STRIP_NOPS (exp);
4378 return (CONSTANT_CLASS_P (exp)
4379 || TREE_CODE (exp) == SSA_NAME
4380 || (DECL_P (exp)
4381 && ! TREE_ADDRESSABLE (exp)
4382 && ! TREE_THIS_VOLATILE (exp)
4383 && ! DECL_NONLOCAL (exp)
4384 /* Don't regard global variables as simple. They may be
4385 allocated in ways unknown to the compiler (shared memory,
4386 #pragma weak, etc). */
4387 && ! TREE_PUBLIC (exp)
4388 && ! DECL_EXTERNAL (exp)
4389 /* Weakrefs are not safe to be read, since they can be NULL.
4390 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4391 have DECL_WEAK flag set. */
4392 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4393 /* Loading a static variable is unduly expensive, but global
4394 registers aren't expensive. */
4395 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4398 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4399 to be evaluated unconditionally.
4400 I addition to simple_operand_p, we assume that comparisons, conversions,
4401 and logic-not operations are simple, if their operands are simple, too. */
4403 static bool
4404 simple_operand_p_2 (tree exp)
4406 enum tree_code code;
4408 if (TREE_SIDE_EFFECTS (exp)
4409 || tree_could_trap_p (exp))
4410 return false;
4412 while (CONVERT_EXPR_P (exp))
4413 exp = TREE_OPERAND (exp, 0);
4415 code = TREE_CODE (exp);
4417 if (TREE_CODE_CLASS (code) == tcc_comparison)
4418 return (simple_operand_p (TREE_OPERAND (exp, 0))
4419 && simple_operand_p (TREE_OPERAND (exp, 1)));
4421 if (code == TRUTH_NOT_EXPR)
4422 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4424 return simple_operand_p (exp);
4428 /* The following functions are subroutines to fold_range_test and allow it to
4429 try to change a logical combination of comparisons into a range test.
4431 For example, both
4432 X == 2 || X == 3 || X == 4 || X == 5
4434 X >= 2 && X <= 5
4435 are converted to
4436 (unsigned) (X - 2) <= 3
4438 We describe each set of comparisons as being either inside or outside
4439 a range, using a variable named like IN_P, and then describe the
4440 range with a lower and upper bound. If one of the bounds is omitted,
4441 it represents either the highest or lowest value of the type.
4443 In the comments below, we represent a range by two numbers in brackets
4444 preceded by a "+" to designate being inside that range, or a "-" to
4445 designate being outside that range, so the condition can be inverted by
4446 flipping the prefix. An omitted bound is represented by a "-". For
4447 example, "- [-, 10]" means being outside the range starting at the lowest
4448 possible value and ending at 10, in other words, being greater than 10.
4449 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4450 always false.
4452 We set up things so that the missing bounds are handled in a consistent
4453 manner so neither a missing bound nor "true" and "false" need to be
4454 handled using a special case. */
4456 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4457 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4458 and UPPER1_P are nonzero if the respective argument is an upper bound
4459 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4460 must be specified for a comparison. ARG1 will be converted to ARG0's
4461 type if both are specified. */
4463 static tree
4464 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4465 tree arg1, int upper1_p)
4467 tree tem;
4468 int result;
4469 int sgn0, sgn1;
4471 /* If neither arg represents infinity, do the normal operation.
4472 Else, if not a comparison, return infinity. Else handle the special
4473 comparison rules. Note that most of the cases below won't occur, but
4474 are handled for consistency. */
4476 if (arg0 != 0 && arg1 != 0)
4478 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4479 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4480 STRIP_NOPS (tem);
4481 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4484 if (TREE_CODE_CLASS (code) != tcc_comparison)
4485 return 0;
4487 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4488 for neither. In real maths, we cannot assume open ended ranges are
4489 the same. But, this is computer arithmetic, where numbers are finite.
4490 We can therefore make the transformation of any unbounded range with
4491 the value Z, Z being greater than any representable number. This permits
4492 us to treat unbounded ranges as equal. */
4493 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4494 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4495 switch (code)
4497 case EQ_EXPR:
4498 result = sgn0 == sgn1;
4499 break;
4500 case NE_EXPR:
4501 result = sgn0 != sgn1;
4502 break;
4503 case LT_EXPR:
4504 result = sgn0 < sgn1;
4505 break;
4506 case LE_EXPR:
4507 result = sgn0 <= sgn1;
4508 break;
4509 case GT_EXPR:
4510 result = sgn0 > sgn1;
4511 break;
4512 case GE_EXPR:
4513 result = sgn0 >= sgn1;
4514 break;
4515 default:
4516 gcc_unreachable ();
4519 return constant_boolean_node (result, type);
4522 /* Helper routine for make_range. Perform one step for it, return
4523 new expression if the loop should continue or NULL_TREE if it should
4524 stop. */
4526 tree
4527 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4528 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4529 bool *strict_overflow_p)
4531 tree arg0_type = TREE_TYPE (arg0);
4532 tree n_low, n_high, low = *p_low, high = *p_high;
4533 int in_p = *p_in_p, n_in_p;
4535 switch (code)
4537 case TRUTH_NOT_EXPR:
4538 /* We can only do something if the range is testing for zero. */
4539 if (low == NULL_TREE || high == NULL_TREE
4540 || ! integer_zerop (low) || ! integer_zerop (high))
4541 return NULL_TREE;
4542 *p_in_p = ! in_p;
4543 return arg0;
4545 case EQ_EXPR: case NE_EXPR:
4546 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4547 /* We can only do something if the range is testing for zero
4548 and if the second operand is an integer constant. Note that
4549 saying something is "in" the range we make is done by
4550 complementing IN_P since it will set in the initial case of
4551 being not equal to zero; "out" is leaving it alone. */
4552 if (low == NULL_TREE || high == NULL_TREE
4553 || ! integer_zerop (low) || ! integer_zerop (high)
4554 || TREE_CODE (arg1) != INTEGER_CST)
4555 return NULL_TREE;
4557 switch (code)
4559 case NE_EXPR: /* - [c, c] */
4560 low = high = arg1;
4561 break;
4562 case EQ_EXPR: /* + [c, c] */
4563 in_p = ! in_p, low = high = arg1;
4564 break;
4565 case GT_EXPR: /* - [-, c] */
4566 low = 0, high = arg1;
4567 break;
4568 case GE_EXPR: /* + [c, -] */
4569 in_p = ! in_p, low = arg1, high = 0;
4570 break;
4571 case LT_EXPR: /* - [c, -] */
4572 low = arg1, high = 0;
4573 break;
4574 case LE_EXPR: /* + [-, c] */
4575 in_p = ! in_p, low = 0, high = arg1;
4576 break;
4577 default:
4578 gcc_unreachable ();
4581 /* If this is an unsigned comparison, we also know that EXP is
4582 greater than or equal to zero. We base the range tests we make
4583 on that fact, so we record it here so we can parse existing
4584 range tests. We test arg0_type since often the return type
4585 of, e.g. EQ_EXPR, is boolean. */
4586 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4588 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4589 in_p, low, high, 1,
4590 build_int_cst (arg0_type, 0),
4591 NULL_TREE))
4592 return NULL_TREE;
4594 in_p = n_in_p, low = n_low, high = n_high;
4596 /* If the high bound is missing, but we have a nonzero low
4597 bound, reverse the range so it goes from zero to the low bound
4598 minus 1. */
4599 if (high == 0 && low && ! integer_zerop (low))
4601 in_p = ! in_p;
4602 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4603 build_int_cst (TREE_TYPE (low), 1), 0);
4604 low = build_int_cst (arg0_type, 0);
4608 *p_low = low;
4609 *p_high = high;
4610 *p_in_p = in_p;
4611 return arg0;
4613 case NEGATE_EXPR:
4614 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4615 low and high are non-NULL, then normalize will DTRT. */
4616 if (!TYPE_UNSIGNED (arg0_type)
4617 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4619 if (low == NULL_TREE)
4620 low = TYPE_MIN_VALUE (arg0_type);
4621 if (high == NULL_TREE)
4622 high = TYPE_MAX_VALUE (arg0_type);
4625 /* (-x) IN [a,b] -> x in [-b, -a] */
4626 n_low = range_binop (MINUS_EXPR, exp_type,
4627 build_int_cst (exp_type, 0),
4628 0, high, 1);
4629 n_high = range_binop (MINUS_EXPR, exp_type,
4630 build_int_cst (exp_type, 0),
4631 0, low, 0);
4632 if (n_high != 0 && TREE_OVERFLOW (n_high))
4633 return NULL_TREE;
4634 goto normalize;
4636 case BIT_NOT_EXPR:
4637 /* ~ X -> -X - 1 */
4638 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4639 build_int_cst (exp_type, 1));
4641 case PLUS_EXPR:
4642 case MINUS_EXPR:
4643 if (TREE_CODE (arg1) != INTEGER_CST)
4644 return NULL_TREE;
4646 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4647 move a constant to the other side. */
4648 if (!TYPE_UNSIGNED (arg0_type)
4649 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4650 return NULL_TREE;
4652 /* If EXP is signed, any overflow in the computation is undefined,
4653 so we don't worry about it so long as our computations on
4654 the bounds don't overflow. For unsigned, overflow is defined
4655 and this is exactly the right thing. */
4656 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4657 arg0_type, low, 0, arg1, 0);
4658 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4659 arg0_type, high, 1, arg1, 0);
4660 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4661 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4662 return NULL_TREE;
4664 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4665 *strict_overflow_p = true;
4667 normalize:
4668 /* Check for an unsigned range which has wrapped around the maximum
4669 value thus making n_high < n_low, and normalize it. */
4670 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4672 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4673 build_int_cst (TREE_TYPE (n_high), 1), 0);
4674 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4675 build_int_cst (TREE_TYPE (n_low), 1), 0);
4677 /* If the range is of the form +/- [ x+1, x ], we won't
4678 be able to normalize it. But then, it represents the
4679 whole range or the empty set, so make it
4680 +/- [ -, - ]. */
4681 if (tree_int_cst_equal (n_low, low)
4682 && tree_int_cst_equal (n_high, high))
4683 low = high = 0;
4684 else
4685 in_p = ! in_p;
4687 else
4688 low = n_low, high = n_high;
4690 *p_low = low;
4691 *p_high = high;
4692 *p_in_p = in_p;
4693 return arg0;
4695 CASE_CONVERT:
4696 case NON_LVALUE_EXPR:
4697 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4698 return NULL_TREE;
4700 if (! INTEGRAL_TYPE_P (arg0_type)
4701 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4702 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4703 return NULL_TREE;
4705 n_low = low, n_high = high;
4707 if (n_low != 0)
4708 n_low = fold_convert_loc (loc, arg0_type, n_low);
4710 if (n_high != 0)
4711 n_high = fold_convert_loc (loc, arg0_type, n_high);
4713 /* If we're converting arg0 from an unsigned type, to exp,
4714 a signed type, we will be doing the comparison as unsigned.
4715 The tests above have already verified that LOW and HIGH
4716 are both positive.
4718 So we have to ensure that we will handle large unsigned
4719 values the same way that the current signed bounds treat
4720 negative values. */
4722 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4724 tree high_positive;
4725 tree equiv_type;
4726 /* For fixed-point modes, we need to pass the saturating flag
4727 as the 2nd parameter. */
4728 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4729 equiv_type
4730 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4731 TYPE_SATURATING (arg0_type));
4732 else
4733 equiv_type
4734 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4736 /* A range without an upper bound is, naturally, unbounded.
4737 Since convert would have cropped a very large value, use
4738 the max value for the destination type. */
4739 high_positive
4740 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4741 : TYPE_MAX_VALUE (arg0_type);
4743 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4744 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4745 fold_convert_loc (loc, arg0_type,
4746 high_positive),
4747 build_int_cst (arg0_type, 1));
4749 /* If the low bound is specified, "and" the range with the
4750 range for which the original unsigned value will be
4751 positive. */
4752 if (low != 0)
4754 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4755 1, fold_convert_loc (loc, arg0_type,
4756 integer_zero_node),
4757 high_positive))
4758 return NULL_TREE;
4760 in_p = (n_in_p == in_p);
4762 else
4764 /* Otherwise, "or" the range with the range of the input
4765 that will be interpreted as negative. */
4766 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4767 1, fold_convert_loc (loc, arg0_type,
4768 integer_zero_node),
4769 high_positive))
4770 return NULL_TREE;
4772 in_p = (in_p != n_in_p);
4776 *p_low = n_low;
4777 *p_high = n_high;
4778 *p_in_p = in_p;
4779 return arg0;
4781 default:
4782 return NULL_TREE;
4786 /* Given EXP, a logical expression, set the range it is testing into
4787 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4788 actually being tested. *PLOW and *PHIGH will be made of the same
4789 type as the returned expression. If EXP is not a comparison, we
4790 will most likely not be returning a useful value and range. Set
4791 *STRICT_OVERFLOW_P to true if the return value is only valid
4792 because signed overflow is undefined; otherwise, do not change
4793 *STRICT_OVERFLOW_P. */
4795 tree
4796 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4797 bool *strict_overflow_p)
4799 enum tree_code code;
4800 tree arg0, arg1 = NULL_TREE;
4801 tree exp_type, nexp;
4802 int in_p;
4803 tree low, high;
4804 location_t loc = EXPR_LOCATION (exp);
4806 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4807 and see if we can refine the range. Some of the cases below may not
4808 happen, but it doesn't seem worth worrying about this. We "continue"
4809 the outer loop when we've changed something; otherwise we "break"
4810 the switch, which will "break" the while. */
4812 in_p = 0;
4813 low = high = build_int_cst (TREE_TYPE (exp), 0);
4815 while (1)
4817 code = TREE_CODE (exp);
4818 exp_type = TREE_TYPE (exp);
4819 arg0 = NULL_TREE;
4821 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4823 if (TREE_OPERAND_LENGTH (exp) > 0)
4824 arg0 = TREE_OPERAND (exp, 0);
4825 if (TREE_CODE_CLASS (code) == tcc_binary
4826 || TREE_CODE_CLASS (code) == tcc_comparison
4827 || (TREE_CODE_CLASS (code) == tcc_expression
4828 && TREE_OPERAND_LENGTH (exp) > 1))
4829 arg1 = TREE_OPERAND (exp, 1);
4831 if (arg0 == NULL_TREE)
4832 break;
4834 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4835 &high, &in_p, strict_overflow_p);
4836 if (nexp == NULL_TREE)
4837 break;
4838 exp = nexp;
4841 /* If EXP is a constant, we can evaluate whether this is true or false. */
4842 if (TREE_CODE (exp) == INTEGER_CST)
4844 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4845 exp, 0, low, 0))
4846 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4847 exp, 1, high, 1)));
4848 low = high = 0;
4849 exp = 0;
4852 *pin_p = in_p, *plow = low, *phigh = high;
4853 return exp;
4856 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4857 a bitwise check i.e. when
4858 LOW == 0xXX...X00...0
4859 HIGH == 0xXX...X11...1
4860 Return corresponding mask in MASK and stem in VALUE. */
4862 static bool
4863 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4864 tree *value)
4866 if (TREE_CODE (low) != INTEGER_CST
4867 || TREE_CODE (high) != INTEGER_CST)
4868 return false;
4870 unsigned prec = TYPE_PRECISION (type);
4871 wide_int lo = wi::to_wide (low, prec);
4872 wide_int hi = wi::to_wide (high, prec);
4874 wide_int end_mask = lo ^ hi;
4875 if ((end_mask & (end_mask + 1)) != 0
4876 || (lo & end_mask) != 0)
4877 return false;
4879 wide_int stem_mask = ~end_mask;
4880 wide_int stem = lo & stem_mask;
4881 if (stem != (hi & stem_mask))
4882 return false;
4884 *mask = wide_int_to_tree (type, stem_mask);
4885 *value = wide_int_to_tree (type, stem);
4887 return true;
4890 /* Helper routine for build_range_check and match.pd. Return the type to
4891 perform the check or NULL if it shouldn't be optimized. */
4893 tree
4894 range_check_type (tree etype)
4896 /* First make sure that arithmetics in this type is valid, then make sure
4897 that it wraps around. */
4898 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4899 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4900 TYPE_UNSIGNED (etype));
4902 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
4904 tree utype, minv, maxv;
4906 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4907 for the type in question, as we rely on this here. */
4908 utype = unsigned_type_for (etype);
4909 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4910 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4911 build_int_cst (TREE_TYPE (maxv), 1), 1);
4912 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4914 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4915 minv, 1, maxv, 1)))
4916 etype = utype;
4917 else
4918 return NULL_TREE;
4920 return etype;
4923 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4924 type, TYPE, return an expression to test if EXP is in (or out of, depending
4925 on IN_P) the range. Return 0 if the test couldn't be created. */
4927 tree
4928 build_range_check (location_t loc, tree type, tree exp, int in_p,
4929 tree low, tree high)
4931 tree etype = TREE_TYPE (exp), mask, value;
4933 /* Disable this optimization for function pointer expressions
4934 on targets that require function pointer canonicalization. */
4935 if (targetm.have_canonicalize_funcptr_for_compare ()
4936 && TREE_CODE (etype) == POINTER_TYPE
4937 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4938 return NULL_TREE;
4940 if (! in_p)
4942 value = build_range_check (loc, type, exp, 1, low, high);
4943 if (value != 0)
4944 return invert_truthvalue_loc (loc, value);
4946 return 0;
4949 if (low == 0 && high == 0)
4950 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4952 if (low == 0)
4953 return fold_build2_loc (loc, LE_EXPR, type, exp,
4954 fold_convert_loc (loc, etype, high));
4956 if (high == 0)
4957 return fold_build2_loc (loc, GE_EXPR, type, exp,
4958 fold_convert_loc (loc, etype, low));
4960 if (operand_equal_p (low, high, 0))
4961 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4962 fold_convert_loc (loc, etype, low));
4964 if (TREE_CODE (exp) == BIT_AND_EXPR
4965 && maskable_range_p (low, high, etype, &mask, &value))
4966 return fold_build2_loc (loc, EQ_EXPR, type,
4967 fold_build2_loc (loc, BIT_AND_EXPR, etype,
4968 exp, mask),
4969 value);
4971 if (integer_zerop (low))
4973 if (! TYPE_UNSIGNED (etype))
4975 etype = unsigned_type_for (etype);
4976 high = fold_convert_loc (loc, etype, high);
4977 exp = fold_convert_loc (loc, etype, exp);
4979 return build_range_check (loc, type, exp, 1, 0, high);
4982 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4983 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4985 int prec = TYPE_PRECISION (etype);
4987 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
4989 if (TYPE_UNSIGNED (etype))
4991 tree signed_etype = signed_type_for (etype);
4992 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4993 etype
4994 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4995 else
4996 etype = signed_etype;
4997 exp = fold_convert_loc (loc, etype, exp);
4999 return fold_build2_loc (loc, GT_EXPR, type, exp,
5000 build_int_cst (etype, 0));
5004 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5005 This requires wrap-around arithmetics for the type of the expression. */
5006 etype = range_check_type (etype);
5007 if (etype == NULL_TREE)
5008 return NULL_TREE;
5010 if (POINTER_TYPE_P (etype))
5011 etype = unsigned_type_for (etype);
5013 high = fold_convert_loc (loc, etype, high);
5014 low = fold_convert_loc (loc, etype, low);
5015 exp = fold_convert_loc (loc, etype, exp);
5017 value = const_binop (MINUS_EXPR, high, low);
5019 if (value != 0 && !TREE_OVERFLOW (value))
5020 return build_range_check (loc, type,
5021 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5022 1, build_int_cst (etype, 0), value);
5024 return 0;
5027 /* Return the predecessor of VAL in its type, handling the infinite case. */
5029 static tree
5030 range_predecessor (tree val)
5032 tree type = TREE_TYPE (val);
5034 if (INTEGRAL_TYPE_P (type)
5035 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5036 return 0;
5037 else
5038 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5039 build_int_cst (TREE_TYPE (val), 1), 0);
5042 /* Return the successor of VAL in its type, handling the infinite case. */
5044 static tree
5045 range_successor (tree val)
5047 tree type = TREE_TYPE (val);
5049 if (INTEGRAL_TYPE_P (type)
5050 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5051 return 0;
5052 else
5053 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5054 build_int_cst (TREE_TYPE (val), 1), 0);
5057 /* Given two ranges, see if we can merge them into one. Return 1 if we
5058 can, 0 if we can't. Set the output range into the specified parameters. */
5060 bool
5061 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5062 tree high0, int in1_p, tree low1, tree high1)
5064 int no_overlap;
5065 int subset;
5066 int temp;
5067 tree tem;
5068 int in_p;
5069 tree low, high;
5070 int lowequal = ((low0 == 0 && low1 == 0)
5071 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5072 low0, 0, low1, 0)));
5073 int highequal = ((high0 == 0 && high1 == 0)
5074 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5075 high0, 1, high1, 1)));
5077 /* Make range 0 be the range that starts first, or ends last if they
5078 start at the same value. Swap them if it isn't. */
5079 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5080 low0, 0, low1, 0))
5081 || (lowequal
5082 && integer_onep (range_binop (GT_EXPR, integer_type_node,
5083 high1, 1, high0, 1))))
5085 temp = in0_p, in0_p = in1_p, in1_p = temp;
5086 tem = low0, low0 = low1, low1 = tem;
5087 tem = high0, high0 = high1, high1 = tem;
5090 /* If the second range is != high1 where high1 is the type maximum of
5091 the type, try first merging with < high1 range. */
5092 if (low1
5093 && high1
5094 && TREE_CODE (low1) == INTEGER_CST
5095 && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5096 || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5097 && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5098 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5099 && operand_equal_p (low1, high1, 0))
5101 if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5102 && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5103 !in1_p, NULL_TREE, range_predecessor (low1)))
5104 return true;
5105 /* Similarly for the second range != low1 where low1 is the type minimum
5106 of the type, try first merging with > low1 range. */
5107 if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5108 && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5109 !in1_p, range_successor (low1), NULL_TREE))
5110 return true;
5113 /* Now flag two cases, whether the ranges are disjoint or whether the
5114 second range is totally subsumed in the first. Note that the tests
5115 below are simplified by the ones above. */
5116 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5117 high0, 1, low1, 0));
5118 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5119 high1, 1, high0, 1));
5121 /* We now have four cases, depending on whether we are including or
5122 excluding the two ranges. */
5123 if (in0_p && in1_p)
5125 /* If they don't overlap, the result is false. If the second range
5126 is a subset it is the result. Otherwise, the range is from the start
5127 of the second to the end of the first. */
5128 if (no_overlap)
5129 in_p = 0, low = high = 0;
5130 else if (subset)
5131 in_p = 1, low = low1, high = high1;
5132 else
5133 in_p = 1, low = low1, high = high0;
5136 else if (in0_p && ! in1_p)
5138 /* If they don't overlap, the result is the first range. If they are
5139 equal, the result is false. If the second range is a subset of the
5140 first, and the ranges begin at the same place, we go from just after
5141 the end of the second range to the end of the first. If the second
5142 range is not a subset of the first, or if it is a subset and both
5143 ranges end at the same place, the range starts at the start of the
5144 first range and ends just before the second range.
5145 Otherwise, we can't describe this as a single range. */
5146 if (no_overlap)
5147 in_p = 1, low = low0, high = high0;
5148 else if (lowequal && highequal)
5149 in_p = 0, low = high = 0;
5150 else if (subset && lowequal)
5152 low = range_successor (high1);
5153 high = high0;
5154 in_p = 1;
5155 if (low == 0)
5157 /* We are in the weird situation where high0 > high1 but
5158 high1 has no successor. Punt. */
5159 return 0;
5162 else if (! subset || highequal)
5164 low = low0;
5165 high = range_predecessor (low1);
5166 in_p = 1;
5167 if (high == 0)
5169 /* low0 < low1 but low1 has no predecessor. Punt. */
5170 return 0;
5173 else
5174 return 0;
5177 else if (! in0_p && in1_p)
5179 /* If they don't overlap, the result is the second range. If the second
5180 is a subset of the first, the result is false. Otherwise,
5181 the range starts just after the first range and ends at the
5182 end of the second. */
5183 if (no_overlap)
5184 in_p = 1, low = low1, high = high1;
5185 else if (subset || highequal)
5186 in_p = 0, low = high = 0;
5187 else
5189 low = range_successor (high0);
5190 high = high1;
5191 in_p = 1;
5192 if (low == 0)
5194 /* high1 > high0 but high0 has no successor. Punt. */
5195 return 0;
5200 else
5202 /* The case where we are excluding both ranges. Here the complex case
5203 is if they don't overlap. In that case, the only time we have a
5204 range is if they are adjacent. If the second is a subset of the
5205 first, the result is the first. Otherwise, the range to exclude
5206 starts at the beginning of the first range and ends at the end of the
5207 second. */
5208 if (no_overlap)
5210 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5211 range_successor (high0),
5212 1, low1, 0)))
5213 in_p = 0, low = low0, high = high1;
5214 else
5216 /* Canonicalize - [min, x] into - [-, x]. */
5217 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5218 switch (TREE_CODE (TREE_TYPE (low0)))
5220 case ENUMERAL_TYPE:
5221 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5222 GET_MODE_BITSIZE
5223 (TYPE_MODE (TREE_TYPE (low0)))))
5224 break;
5225 /* FALLTHROUGH */
5226 case INTEGER_TYPE:
5227 if (tree_int_cst_equal (low0,
5228 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5229 low0 = 0;
5230 break;
5231 case POINTER_TYPE:
5232 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5233 && integer_zerop (low0))
5234 low0 = 0;
5235 break;
5236 default:
5237 break;
5240 /* Canonicalize - [x, max] into - [x, -]. */
5241 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5242 switch (TREE_CODE (TREE_TYPE (high1)))
5244 case ENUMERAL_TYPE:
5245 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5246 GET_MODE_BITSIZE
5247 (TYPE_MODE (TREE_TYPE (high1)))))
5248 break;
5249 /* FALLTHROUGH */
5250 case INTEGER_TYPE:
5251 if (tree_int_cst_equal (high1,
5252 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5253 high1 = 0;
5254 break;
5255 case POINTER_TYPE:
5256 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5257 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5258 high1, 1,
5259 build_int_cst (TREE_TYPE (high1), 1),
5260 1)))
5261 high1 = 0;
5262 break;
5263 default:
5264 break;
5267 /* The ranges might be also adjacent between the maximum and
5268 minimum values of the given type. For
5269 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5270 return + [x + 1, y - 1]. */
5271 if (low0 == 0 && high1 == 0)
5273 low = range_successor (high0);
5274 high = range_predecessor (low1);
5275 if (low == 0 || high == 0)
5276 return 0;
5278 in_p = 1;
5280 else
5281 return 0;
5284 else if (subset)
5285 in_p = 0, low = low0, high = high0;
5286 else
5287 in_p = 0, low = low0, high = high1;
5290 *pin_p = in_p, *plow = low, *phigh = high;
5291 return 1;
5295 /* Subroutine of fold, looking inside expressions of the form
5296 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5297 of the COND_EXPR. This function is being used also to optimize
5298 A op B ? C : A, by reversing the comparison first.
5300 Return a folded expression whose code is not a COND_EXPR
5301 anymore, or NULL_TREE if no folding opportunity is found. */
5303 static tree
5304 fold_cond_expr_with_comparison (location_t loc, tree type,
5305 tree arg0, tree arg1, tree arg2)
5307 enum tree_code comp_code = TREE_CODE (arg0);
5308 tree arg00 = TREE_OPERAND (arg0, 0);
5309 tree arg01 = TREE_OPERAND (arg0, 1);
5310 tree arg1_type = TREE_TYPE (arg1);
5311 tree tem;
5313 STRIP_NOPS (arg1);
5314 STRIP_NOPS (arg2);
5316 /* If we have A op 0 ? A : -A, consider applying the following
5317 transformations:
5319 A == 0? A : -A same as -A
5320 A != 0? A : -A same as A
5321 A >= 0? A : -A same as abs (A)
5322 A > 0? A : -A same as abs (A)
5323 A <= 0? A : -A same as -abs (A)
5324 A < 0? A : -A same as -abs (A)
5326 None of these transformations work for modes with signed
5327 zeros. If A is +/-0, the first two transformations will
5328 change the sign of the result (from +0 to -0, or vice
5329 versa). The last four will fix the sign of the result,
5330 even though the original expressions could be positive or
5331 negative, depending on the sign of A.
5333 Note that all these transformations are correct if A is
5334 NaN, since the two alternatives (A and -A) are also NaNs. */
5335 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5336 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5337 ? real_zerop (arg01)
5338 : integer_zerop (arg01))
5339 && ((TREE_CODE (arg2) == NEGATE_EXPR
5340 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5341 /* In the case that A is of the form X-Y, '-A' (arg2) may
5342 have already been folded to Y-X, check for that. */
5343 || (TREE_CODE (arg1) == MINUS_EXPR
5344 && TREE_CODE (arg2) == MINUS_EXPR
5345 && operand_equal_p (TREE_OPERAND (arg1, 0),
5346 TREE_OPERAND (arg2, 1), 0)
5347 && operand_equal_p (TREE_OPERAND (arg1, 1),
5348 TREE_OPERAND (arg2, 0), 0))))
5349 switch (comp_code)
5351 case EQ_EXPR:
5352 case UNEQ_EXPR:
5353 tem = fold_convert_loc (loc, arg1_type, arg1);
5354 return fold_convert_loc (loc, type, negate_expr (tem));
5355 case NE_EXPR:
5356 case LTGT_EXPR:
5357 return fold_convert_loc (loc, type, arg1);
5358 case UNGE_EXPR:
5359 case UNGT_EXPR:
5360 if (flag_trapping_math)
5361 break;
5362 /* Fall through. */
5363 case GE_EXPR:
5364 case GT_EXPR:
5365 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5366 break;
5367 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5368 return fold_convert_loc (loc, type, tem);
5369 case UNLE_EXPR:
5370 case UNLT_EXPR:
5371 if (flag_trapping_math)
5372 break;
5373 /* FALLTHRU */
5374 case LE_EXPR:
5375 case LT_EXPR:
5376 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5377 break;
5378 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5379 return negate_expr (fold_convert_loc (loc, type, tem));
5380 default:
5381 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5382 break;
5385 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5386 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5387 both transformations are correct when A is NaN: A != 0
5388 is then true, and A == 0 is false. */
5390 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5391 && integer_zerop (arg01) && integer_zerop (arg2))
5393 if (comp_code == NE_EXPR)
5394 return fold_convert_loc (loc, type, arg1);
5395 else if (comp_code == EQ_EXPR)
5396 return build_zero_cst (type);
5399 /* Try some transformations of A op B ? A : B.
5401 A == B? A : B same as B
5402 A != B? A : B same as A
5403 A >= B? A : B same as max (A, B)
5404 A > B? A : B same as max (B, A)
5405 A <= B? A : B same as min (A, B)
5406 A < B? A : B same as min (B, A)
5408 As above, these transformations don't work in the presence
5409 of signed zeros. For example, if A and B are zeros of
5410 opposite sign, the first two transformations will change
5411 the sign of the result. In the last four, the original
5412 expressions give different results for (A=+0, B=-0) and
5413 (A=-0, B=+0), but the transformed expressions do not.
5415 The first two transformations are correct if either A or B
5416 is a NaN. In the first transformation, the condition will
5417 be false, and B will indeed be chosen. In the case of the
5418 second transformation, the condition A != B will be true,
5419 and A will be chosen.
5421 The conversions to max() and min() are not correct if B is
5422 a number and A is not. The conditions in the original
5423 expressions will be false, so all four give B. The min()
5424 and max() versions would give a NaN instead. */
5425 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5426 && operand_equal_for_comparison_p (arg01, arg2)
5427 /* Avoid these transformations if the COND_EXPR may be used
5428 as an lvalue in the C++ front-end. PR c++/19199. */
5429 && (in_gimple_form
5430 || VECTOR_TYPE_P (type)
5431 || (! lang_GNU_CXX ()
5432 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5433 || ! maybe_lvalue_p (arg1)
5434 || ! maybe_lvalue_p (arg2)))
5436 tree comp_op0 = arg00;
5437 tree comp_op1 = arg01;
5438 tree comp_type = TREE_TYPE (comp_op0);
5440 switch (comp_code)
5442 case EQ_EXPR:
5443 return fold_convert_loc (loc, type, arg2);
5444 case NE_EXPR:
5445 return fold_convert_loc (loc, type, arg1);
5446 case LE_EXPR:
5447 case LT_EXPR:
5448 case UNLE_EXPR:
5449 case UNLT_EXPR:
5450 /* In C++ a ?: expression can be an lvalue, so put the
5451 operand which will be used if they are equal first
5452 so that we can convert this back to the
5453 corresponding COND_EXPR. */
5454 if (!HONOR_NANS (arg1))
5456 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5457 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5458 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5459 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5460 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5461 comp_op1, comp_op0);
5462 return fold_convert_loc (loc, type, tem);
5464 break;
5465 case GE_EXPR:
5466 case GT_EXPR:
5467 case UNGE_EXPR:
5468 case UNGT_EXPR:
5469 if (!HONOR_NANS (arg1))
5471 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5472 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5473 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5474 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5475 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5476 comp_op1, comp_op0);
5477 return fold_convert_loc (loc, type, tem);
5479 break;
5480 case UNEQ_EXPR:
5481 if (!HONOR_NANS (arg1))
5482 return fold_convert_loc (loc, type, arg2);
5483 break;
5484 case LTGT_EXPR:
5485 if (!HONOR_NANS (arg1))
5486 return fold_convert_loc (loc, type, arg1);
5487 break;
5488 default:
5489 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5490 break;
5494 return NULL_TREE;
5499 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5500 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5501 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5502 false) >= 2)
5503 #endif
5505 /* EXP is some logical combination of boolean tests. See if we can
5506 merge it into some range test. Return the new tree if so. */
5508 static tree
5509 fold_range_test (location_t loc, enum tree_code code, tree type,
5510 tree op0, tree op1)
5512 int or_op = (code == TRUTH_ORIF_EXPR
5513 || code == TRUTH_OR_EXPR);
5514 int in0_p, in1_p, in_p;
5515 tree low0, low1, low, high0, high1, high;
5516 bool strict_overflow_p = false;
5517 tree tem, lhs, rhs;
5518 const char * const warnmsg = G_("assuming signed overflow does not occur "
5519 "when simplifying range test");
5521 if (!INTEGRAL_TYPE_P (type))
5522 return 0;
5524 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5525 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5527 /* If this is an OR operation, invert both sides; we will invert
5528 again at the end. */
5529 if (or_op)
5530 in0_p = ! in0_p, in1_p = ! in1_p;
5532 /* If both expressions are the same, if we can merge the ranges, and we
5533 can build the range test, return it or it inverted. If one of the
5534 ranges is always true or always false, consider it to be the same
5535 expression as the other. */
5536 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5537 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5538 in1_p, low1, high1)
5539 && (tem = (build_range_check (loc, type,
5540 lhs != 0 ? lhs
5541 : rhs != 0 ? rhs : integer_zero_node,
5542 in_p, low, high))) != 0)
5544 if (strict_overflow_p)
5545 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5546 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5549 /* On machines where the branch cost is expensive, if this is a
5550 short-circuited branch and the underlying object on both sides
5551 is the same, make a non-short-circuit operation. */
5552 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5553 && !flag_sanitize_coverage
5554 && lhs != 0 && rhs != 0
5555 && (code == TRUTH_ANDIF_EXPR
5556 || code == TRUTH_ORIF_EXPR)
5557 && operand_equal_p (lhs, rhs, 0))
5559 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5560 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5561 which cases we can't do this. */
5562 if (simple_operand_p (lhs))
5563 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5564 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5565 type, op0, op1);
5567 else if (!lang_hooks.decls.global_bindings_p ()
5568 && !CONTAINS_PLACEHOLDER_P (lhs))
5570 tree common = save_expr (lhs);
5572 if ((lhs = build_range_check (loc, type, common,
5573 or_op ? ! in0_p : in0_p,
5574 low0, high0)) != 0
5575 && (rhs = build_range_check (loc, type, common,
5576 or_op ? ! in1_p : in1_p,
5577 low1, high1)) != 0)
5579 if (strict_overflow_p)
5580 fold_overflow_warning (warnmsg,
5581 WARN_STRICT_OVERFLOW_COMPARISON);
5582 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5583 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5584 type, lhs, rhs);
5589 return 0;
5592 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5593 bit value. Arrange things so the extra bits will be set to zero if and
5594 only if C is signed-extended to its full width. If MASK is nonzero,
5595 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5597 static tree
5598 unextend (tree c, int p, int unsignedp, tree mask)
5600 tree type = TREE_TYPE (c);
5601 int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
5602 tree temp;
5604 if (p == modesize || unsignedp)
5605 return c;
5607 /* We work by getting just the sign bit into the low-order bit, then
5608 into the high-order bit, then sign-extend. We then XOR that value
5609 with C. */
5610 temp = build_int_cst (TREE_TYPE (c),
5611 wi::extract_uhwi (wi::to_wide (c), p - 1, 1));
5613 /* We must use a signed type in order to get an arithmetic right shift.
5614 However, we must also avoid introducing accidental overflows, so that
5615 a subsequent call to integer_zerop will work. Hence we must
5616 do the type conversion here. At this point, the constant is either
5617 zero or one, and the conversion to a signed type can never overflow.
5618 We could get an overflow if this conversion is done anywhere else. */
5619 if (TYPE_UNSIGNED (type))
5620 temp = fold_convert (signed_type_for (type), temp);
5622 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5623 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5624 if (mask != 0)
5625 temp = const_binop (BIT_AND_EXPR, temp,
5626 fold_convert (TREE_TYPE (c), mask));
5627 /* If necessary, convert the type back to match the type of C. */
5628 if (TYPE_UNSIGNED (type))
5629 temp = fold_convert (type, temp);
5631 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5634 /* For an expression that has the form
5635 (A && B) || ~B
5637 (A || B) && ~B,
5638 we can drop one of the inner expressions and simplify to
5639 A || ~B
5641 A && ~B
5642 LOC is the location of the resulting expression. OP is the inner
5643 logical operation; the left-hand side in the examples above, while CMPOP
5644 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5645 removing a condition that guards another, as in
5646 (A != NULL && A->...) || A == NULL
5647 which we must not transform. If RHS_ONLY is true, only eliminate the
5648 right-most operand of the inner logical operation. */
5650 static tree
5651 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5652 bool rhs_only)
5654 tree type = TREE_TYPE (cmpop);
5655 enum tree_code code = TREE_CODE (cmpop);
5656 enum tree_code truthop_code = TREE_CODE (op);
5657 tree lhs = TREE_OPERAND (op, 0);
5658 tree rhs = TREE_OPERAND (op, 1);
5659 tree orig_lhs = lhs, orig_rhs = rhs;
5660 enum tree_code rhs_code = TREE_CODE (rhs);
5661 enum tree_code lhs_code = TREE_CODE (lhs);
5662 enum tree_code inv_code;
5664 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5665 return NULL_TREE;
5667 if (TREE_CODE_CLASS (code) != tcc_comparison)
5668 return NULL_TREE;
5670 if (rhs_code == truthop_code)
5672 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5673 if (newrhs != NULL_TREE)
5675 rhs = newrhs;
5676 rhs_code = TREE_CODE (rhs);
5679 if (lhs_code == truthop_code && !rhs_only)
5681 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5682 if (newlhs != NULL_TREE)
5684 lhs = newlhs;
5685 lhs_code = TREE_CODE (lhs);
5689 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5690 if (inv_code == rhs_code
5691 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5692 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5693 return lhs;
5694 if (!rhs_only && inv_code == lhs_code
5695 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5696 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5697 return rhs;
5698 if (rhs != orig_rhs || lhs != orig_lhs)
5699 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5700 lhs, rhs);
5701 return NULL_TREE;
5704 /* Find ways of folding logical expressions of LHS and RHS:
5705 Try to merge two comparisons to the same innermost item.
5706 Look for range tests like "ch >= '0' && ch <= '9'".
5707 Look for combinations of simple terms on machines with expensive branches
5708 and evaluate the RHS unconditionally.
5710 For example, if we have p->a == 2 && p->b == 4 and we can make an
5711 object large enough to span both A and B, we can do this with a comparison
5712 against the object ANDed with the a mask.
5714 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5715 operations to do this with one comparison.
5717 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5718 function and the one above.
5720 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5721 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5723 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5724 two operands.
5726 We return the simplified tree or 0 if no optimization is possible. */
5728 static tree
5729 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5730 tree lhs, tree rhs)
5732 /* If this is the "or" of two comparisons, we can do something if
5733 the comparisons are NE_EXPR. If this is the "and", we can do something
5734 if the comparisons are EQ_EXPR. I.e.,
5735 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5737 WANTED_CODE is this operation code. For single bit fields, we can
5738 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5739 comparison for one-bit fields. */
5741 enum tree_code wanted_code;
5742 enum tree_code lcode, rcode;
5743 tree ll_arg, lr_arg, rl_arg, rr_arg;
5744 tree ll_inner, lr_inner, rl_inner, rr_inner;
5745 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5746 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5747 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5748 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5749 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5750 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5751 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5752 scalar_int_mode lnmode, rnmode;
5753 tree ll_mask, lr_mask, rl_mask, rr_mask;
5754 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5755 tree l_const, r_const;
5756 tree lntype, rntype, result;
5757 HOST_WIDE_INT first_bit, end_bit;
5758 int volatilep;
5760 /* Start by getting the comparison codes. Fail if anything is volatile.
5761 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5762 it were surrounded with a NE_EXPR. */
5764 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5765 return 0;
5767 lcode = TREE_CODE (lhs);
5768 rcode = TREE_CODE (rhs);
5770 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5772 lhs = build2 (NE_EXPR, truth_type, lhs,
5773 build_int_cst (TREE_TYPE (lhs), 0));
5774 lcode = NE_EXPR;
5777 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5779 rhs = build2 (NE_EXPR, truth_type, rhs,
5780 build_int_cst (TREE_TYPE (rhs), 0));
5781 rcode = NE_EXPR;
5784 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5785 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5786 return 0;
5788 ll_arg = TREE_OPERAND (lhs, 0);
5789 lr_arg = TREE_OPERAND (lhs, 1);
5790 rl_arg = TREE_OPERAND (rhs, 0);
5791 rr_arg = TREE_OPERAND (rhs, 1);
5793 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5794 if (simple_operand_p (ll_arg)
5795 && simple_operand_p (lr_arg))
5797 if (operand_equal_p (ll_arg, rl_arg, 0)
5798 && operand_equal_p (lr_arg, rr_arg, 0))
5800 result = combine_comparisons (loc, code, lcode, rcode,
5801 truth_type, ll_arg, lr_arg);
5802 if (result)
5803 return result;
5805 else if (operand_equal_p (ll_arg, rr_arg, 0)
5806 && operand_equal_p (lr_arg, rl_arg, 0))
5808 result = combine_comparisons (loc, code, lcode,
5809 swap_tree_comparison (rcode),
5810 truth_type, ll_arg, lr_arg);
5811 if (result)
5812 return result;
5816 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5817 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5819 /* If the RHS can be evaluated unconditionally and its operands are
5820 simple, it wins to evaluate the RHS unconditionally on machines
5821 with expensive branches. In this case, this isn't a comparison
5822 that can be merged. */
5824 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5825 false) >= 2
5826 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5827 && simple_operand_p (rl_arg)
5828 && simple_operand_p (rr_arg))
5830 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5831 if (code == TRUTH_OR_EXPR
5832 && lcode == NE_EXPR && integer_zerop (lr_arg)
5833 && rcode == NE_EXPR && integer_zerop (rr_arg)
5834 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5835 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5836 return build2_loc (loc, NE_EXPR, truth_type,
5837 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5838 ll_arg, rl_arg),
5839 build_int_cst (TREE_TYPE (ll_arg), 0));
5841 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5842 if (code == TRUTH_AND_EXPR
5843 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5844 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5845 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5846 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5847 return build2_loc (loc, EQ_EXPR, truth_type,
5848 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5849 ll_arg, rl_arg),
5850 build_int_cst (TREE_TYPE (ll_arg), 0));
5853 /* See if the comparisons can be merged. Then get all the parameters for
5854 each side. */
5856 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5857 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5858 return 0;
5860 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5861 volatilep = 0;
5862 ll_inner = decode_field_reference (loc, &ll_arg,
5863 &ll_bitsize, &ll_bitpos, &ll_mode,
5864 &ll_unsignedp, &ll_reversep, &volatilep,
5865 &ll_mask, &ll_and_mask);
5866 lr_inner = decode_field_reference (loc, &lr_arg,
5867 &lr_bitsize, &lr_bitpos, &lr_mode,
5868 &lr_unsignedp, &lr_reversep, &volatilep,
5869 &lr_mask, &lr_and_mask);
5870 rl_inner = decode_field_reference (loc, &rl_arg,
5871 &rl_bitsize, &rl_bitpos, &rl_mode,
5872 &rl_unsignedp, &rl_reversep, &volatilep,
5873 &rl_mask, &rl_and_mask);
5874 rr_inner = decode_field_reference (loc, &rr_arg,
5875 &rr_bitsize, &rr_bitpos, &rr_mode,
5876 &rr_unsignedp, &rr_reversep, &volatilep,
5877 &rr_mask, &rr_and_mask);
5879 /* It must be true that the inner operation on the lhs of each
5880 comparison must be the same if we are to be able to do anything.
5881 Then see if we have constants. If not, the same must be true for
5882 the rhs's. */
5883 if (volatilep
5884 || ll_reversep != rl_reversep
5885 || ll_inner == 0 || rl_inner == 0
5886 || ! operand_equal_p (ll_inner, rl_inner, 0))
5887 return 0;
5889 if (TREE_CODE (lr_arg) == INTEGER_CST
5890 && TREE_CODE (rr_arg) == INTEGER_CST)
5892 l_const = lr_arg, r_const = rr_arg;
5893 lr_reversep = ll_reversep;
5895 else if (lr_reversep != rr_reversep
5896 || lr_inner == 0 || rr_inner == 0
5897 || ! operand_equal_p (lr_inner, rr_inner, 0))
5898 return 0;
5899 else
5900 l_const = r_const = 0;
5902 /* If either comparison code is not correct for our logical operation,
5903 fail. However, we can convert a one-bit comparison against zero into
5904 the opposite comparison against that bit being set in the field. */
5906 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5907 if (lcode != wanted_code)
5909 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5911 /* Make the left operand unsigned, since we are only interested
5912 in the value of one bit. Otherwise we are doing the wrong
5913 thing below. */
5914 ll_unsignedp = 1;
5915 l_const = ll_mask;
5917 else
5918 return 0;
5921 /* This is analogous to the code for l_const above. */
5922 if (rcode != wanted_code)
5924 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5926 rl_unsignedp = 1;
5927 r_const = rl_mask;
5929 else
5930 return 0;
5933 /* See if we can find a mode that contains both fields being compared on
5934 the left. If we can't, fail. Otherwise, update all constants and masks
5935 to be relative to a field of that size. */
5936 first_bit = MIN (ll_bitpos, rl_bitpos);
5937 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5938 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5939 TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
5940 volatilep, &lnmode))
5941 return 0;
5943 lnbitsize = GET_MODE_BITSIZE (lnmode);
5944 lnbitpos = first_bit & ~ (lnbitsize - 1);
5945 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5946 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5948 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5950 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5951 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5954 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5955 size_int (xll_bitpos));
5956 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5957 size_int (xrl_bitpos));
5959 if (l_const)
5961 l_const = fold_convert_loc (loc, lntype, l_const);
5962 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5963 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
5964 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5965 fold_build1_loc (loc, BIT_NOT_EXPR,
5966 lntype, ll_mask))))
5968 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5970 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5973 if (r_const)
5975 r_const = fold_convert_loc (loc, lntype, r_const);
5976 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5977 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
5978 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5979 fold_build1_loc (loc, BIT_NOT_EXPR,
5980 lntype, rl_mask))))
5982 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5984 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5988 /* If the right sides are not constant, do the same for it. Also,
5989 disallow this optimization if a size or signedness mismatch occurs
5990 between the left and right sides. */
5991 if (l_const == 0)
5993 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5994 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5995 /* Make sure the two fields on the right
5996 correspond to the left without being swapped. */
5997 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5998 return 0;
6000 first_bit = MIN (lr_bitpos, rr_bitpos);
6001 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
6002 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
6003 TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
6004 volatilep, &rnmode))
6005 return 0;
6007 rnbitsize = GET_MODE_BITSIZE (rnmode);
6008 rnbitpos = first_bit & ~ (rnbitsize - 1);
6009 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
6010 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
6012 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
6014 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
6015 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
6018 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6019 rntype, lr_mask),
6020 size_int (xlr_bitpos));
6021 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6022 rntype, rr_mask),
6023 size_int (xrr_bitpos));
6025 /* Make a mask that corresponds to both fields being compared.
6026 Do this for both items being compared. If the operands are the
6027 same size and the bits being compared are in the same position
6028 then we can do this by masking both and comparing the masked
6029 results. */
6030 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6031 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
6032 if (lnbitsize == rnbitsize
6033 && xll_bitpos == xlr_bitpos
6034 && lnbitpos >= 0
6035 && rnbitpos >= 0)
6037 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
6038 lntype, lnbitsize, lnbitpos,
6039 ll_unsignedp || rl_unsignedp, ll_reversep);
6040 if (! all_ones_mask_p (ll_mask, lnbitsize))
6041 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6043 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
6044 rntype, rnbitsize, rnbitpos,
6045 lr_unsignedp || rr_unsignedp, lr_reversep);
6046 if (! all_ones_mask_p (lr_mask, rnbitsize))
6047 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6049 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6052 /* There is still another way we can do something: If both pairs of
6053 fields being compared are adjacent, we may be able to make a wider
6054 field containing them both.
6056 Note that we still must mask the lhs/rhs expressions. Furthermore,
6057 the mask must be shifted to account for the shift done by
6058 make_bit_field_ref. */
6059 if (((ll_bitsize + ll_bitpos == rl_bitpos
6060 && lr_bitsize + lr_bitpos == rr_bitpos)
6061 || (ll_bitpos == rl_bitpos + rl_bitsize
6062 && lr_bitpos == rr_bitpos + rr_bitsize))
6063 && ll_bitpos >= 0
6064 && rl_bitpos >= 0
6065 && lr_bitpos >= 0
6066 && rr_bitpos >= 0)
6068 tree type;
6070 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
6071 ll_bitsize + rl_bitsize,
6072 MIN (ll_bitpos, rl_bitpos),
6073 ll_unsignedp, ll_reversep);
6074 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
6075 lr_bitsize + rr_bitsize,
6076 MIN (lr_bitpos, rr_bitpos),
6077 lr_unsignedp, lr_reversep);
6079 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6080 size_int (MIN (xll_bitpos, xrl_bitpos)));
6081 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6082 size_int (MIN (xlr_bitpos, xrr_bitpos)));
6084 /* Convert to the smaller type before masking out unwanted bits. */
6085 type = lntype;
6086 if (lntype != rntype)
6088 if (lnbitsize > rnbitsize)
6090 lhs = fold_convert_loc (loc, rntype, lhs);
6091 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6092 type = rntype;
6094 else if (lnbitsize < rnbitsize)
6096 rhs = fold_convert_loc (loc, lntype, rhs);
6097 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6098 type = lntype;
6102 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6103 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6105 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6106 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6108 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6111 return 0;
6114 /* Handle the case of comparisons with constants. If there is something in
6115 common between the masks, those bits of the constants must be the same.
6116 If not, the condition is always false. Test for this to avoid generating
6117 incorrect code below. */
6118 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
6119 if (! integer_zerop (result)
6120 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
6121 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
6123 if (wanted_code == NE_EXPR)
6125 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6126 return constant_boolean_node (true, truth_type);
6128 else
6130 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6131 return constant_boolean_node (false, truth_type);
6135 if (lnbitpos < 0)
6136 return 0;
6138 /* Construct the expression we will return. First get the component
6139 reference we will make. Unless the mask is all ones the width of
6140 that field, perform the mask operation. Then compare with the
6141 merged constant. */
6142 result = make_bit_field_ref (loc, ll_inner, ll_arg,
6143 lntype, lnbitsize, lnbitpos,
6144 ll_unsignedp || rl_unsignedp, ll_reversep);
6146 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6147 if (! all_ones_mask_p (ll_mask, lnbitsize))
6148 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
6150 return build2_loc (loc, wanted_code, truth_type, result,
6151 const_binop (BIT_IOR_EXPR, l_const, r_const));
6154 /* T is an integer expression that is being multiplied, divided, or taken a
6155 modulus (CODE says which and what kind of divide or modulus) by a
6156 constant C. See if we can eliminate that operation by folding it with
6157 other operations already in T. WIDE_TYPE, if non-null, is a type that
6158 should be used for the computation if wider than our type.
6160 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6161 (X * 2) + (Y * 4). We must, however, be assured that either the original
6162 expression would not overflow or that overflow is undefined for the type
6163 in the language in question.
6165 If we return a non-null expression, it is an equivalent form of the
6166 original computation, but need not be in the original type.
6168 We set *STRICT_OVERFLOW_P to true if the return values depends on
6169 signed overflow being undefined. Otherwise we do not change
6170 *STRICT_OVERFLOW_P. */
6172 static tree
6173 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6174 bool *strict_overflow_p)
6176 /* To avoid exponential search depth, refuse to allow recursion past
6177 three levels. Beyond that (1) it's highly unlikely that we'll find
6178 something interesting and (2) we've probably processed it before
6179 when we built the inner expression. */
6181 static int depth;
6182 tree ret;
6184 if (depth > 3)
6185 return NULL;
6187 depth++;
6188 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6189 depth--;
6191 return ret;
6194 static tree
6195 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6196 bool *strict_overflow_p)
6198 tree type = TREE_TYPE (t);
6199 enum tree_code tcode = TREE_CODE (t);
6200 tree ctype = (wide_type != 0
6201 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6202 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6203 ? wide_type : type);
6204 tree t1, t2;
6205 int same_p = tcode == code;
6206 tree op0 = NULL_TREE, op1 = NULL_TREE;
6207 bool sub_strict_overflow_p;
6209 /* Don't deal with constants of zero here; they confuse the code below. */
6210 if (integer_zerop (c))
6211 return NULL_TREE;
6213 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6214 op0 = TREE_OPERAND (t, 0);
6216 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6217 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6219 /* Note that we need not handle conditional operations here since fold
6220 already handles those cases. So just do arithmetic here. */
6221 switch (tcode)
6223 case INTEGER_CST:
6224 /* For a constant, we can always simplify if we are a multiply
6225 or (for divide and modulus) if it is a multiple of our constant. */
6226 if (code == MULT_EXPR
6227 || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6228 TYPE_SIGN (type)))
6230 tree tem = const_binop (code, fold_convert (ctype, t),
6231 fold_convert (ctype, c));
6232 /* If the multiplication overflowed, we lost information on it.
6233 See PR68142 and PR69845. */
6234 if (TREE_OVERFLOW (tem))
6235 return NULL_TREE;
6236 return tem;
6238 break;
6240 CASE_CONVERT: case NON_LVALUE_EXPR:
6241 /* If op0 is an expression ... */
6242 if ((COMPARISON_CLASS_P (op0)
6243 || UNARY_CLASS_P (op0)
6244 || BINARY_CLASS_P (op0)
6245 || VL_EXP_CLASS_P (op0)
6246 || EXPRESSION_CLASS_P (op0))
6247 /* ... and has wrapping overflow, and its type is smaller
6248 than ctype, then we cannot pass through as widening. */
6249 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6250 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6251 && (TYPE_PRECISION (ctype)
6252 > TYPE_PRECISION (TREE_TYPE (op0))))
6253 /* ... or this is a truncation (t is narrower than op0),
6254 then we cannot pass through this narrowing. */
6255 || (TYPE_PRECISION (type)
6256 < TYPE_PRECISION (TREE_TYPE (op0)))
6257 /* ... or signedness changes for division or modulus,
6258 then we cannot pass through this conversion. */
6259 || (code != MULT_EXPR
6260 && (TYPE_UNSIGNED (ctype)
6261 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6262 /* ... or has undefined overflow while the converted to
6263 type has not, we cannot do the operation in the inner type
6264 as that would introduce undefined overflow. */
6265 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6266 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6267 && !TYPE_OVERFLOW_UNDEFINED (type))))
6268 break;
6270 /* Pass the constant down and see if we can make a simplification. If
6271 we can, replace this expression with the inner simplification for
6272 possible later conversion to our or some other type. */
6273 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6274 && TREE_CODE (t2) == INTEGER_CST
6275 && !TREE_OVERFLOW (t2)
6276 && (t1 = extract_muldiv (op0, t2, code,
6277 code == MULT_EXPR ? ctype : NULL_TREE,
6278 strict_overflow_p)) != 0)
6279 return t1;
6280 break;
6282 case ABS_EXPR:
6283 /* If widening the type changes it from signed to unsigned, then we
6284 must avoid building ABS_EXPR itself as unsigned. */
6285 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6287 tree cstype = (*signed_type_for) (ctype);
6288 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6289 != 0)
6291 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6292 return fold_convert (ctype, t1);
6294 break;
6296 /* If the constant is negative, we cannot simplify this. */
6297 if (tree_int_cst_sgn (c) == -1)
6298 break;
6299 /* FALLTHROUGH */
6300 case NEGATE_EXPR:
6301 /* For division and modulus, type can't be unsigned, as e.g.
6302 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6303 For signed types, even with wrapping overflow, this is fine. */
6304 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6305 break;
6306 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6307 != 0)
6308 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6309 break;
6311 case MIN_EXPR: case MAX_EXPR:
6312 /* If widening the type changes the signedness, then we can't perform
6313 this optimization as that changes the result. */
6314 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6315 break;
6317 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6318 sub_strict_overflow_p = false;
6319 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6320 &sub_strict_overflow_p)) != 0
6321 && (t2 = extract_muldiv (op1, c, code, wide_type,
6322 &sub_strict_overflow_p)) != 0)
6324 if (tree_int_cst_sgn (c) < 0)
6325 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6326 if (sub_strict_overflow_p)
6327 *strict_overflow_p = true;
6328 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6329 fold_convert (ctype, t2));
6331 break;
6333 case LSHIFT_EXPR: case RSHIFT_EXPR:
6334 /* If the second operand is constant, this is a multiplication
6335 or floor division, by a power of two, so we can treat it that
6336 way unless the multiplier or divisor overflows. Signed
6337 left-shift overflow is implementation-defined rather than
6338 undefined in C90, so do not convert signed left shift into
6339 multiplication. */
6340 if (TREE_CODE (op1) == INTEGER_CST
6341 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6342 /* const_binop may not detect overflow correctly,
6343 so check for it explicitly here. */
6344 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6345 wi::to_wide (op1))
6346 && (t1 = fold_convert (ctype,
6347 const_binop (LSHIFT_EXPR, size_one_node,
6348 op1))) != 0
6349 && !TREE_OVERFLOW (t1))
6350 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6351 ? MULT_EXPR : FLOOR_DIV_EXPR,
6352 ctype,
6353 fold_convert (ctype, op0),
6354 t1),
6355 c, code, wide_type, strict_overflow_p);
6356 break;
6358 case PLUS_EXPR: case MINUS_EXPR:
6359 /* See if we can eliminate the operation on both sides. If we can, we
6360 can return a new PLUS or MINUS. If we can't, the only remaining
6361 cases where we can do anything are if the second operand is a
6362 constant. */
6363 sub_strict_overflow_p = false;
6364 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6365 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6366 if (t1 != 0 && t2 != 0
6367 && TYPE_OVERFLOW_WRAPS (ctype)
6368 && (code == MULT_EXPR
6369 /* If not multiplication, we can only do this if both operands
6370 are divisible by c. */
6371 || (multiple_of_p (ctype, op0, c)
6372 && multiple_of_p (ctype, op1, c))))
6374 if (sub_strict_overflow_p)
6375 *strict_overflow_p = true;
6376 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6377 fold_convert (ctype, t2));
6380 /* If this was a subtraction, negate OP1 and set it to be an addition.
6381 This simplifies the logic below. */
6382 if (tcode == MINUS_EXPR)
6384 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6385 /* If OP1 was not easily negatable, the constant may be OP0. */
6386 if (TREE_CODE (op0) == INTEGER_CST)
6388 std::swap (op0, op1);
6389 std::swap (t1, t2);
6393 if (TREE_CODE (op1) != INTEGER_CST)
6394 break;
6396 /* If either OP1 or C are negative, this optimization is not safe for
6397 some of the division and remainder types while for others we need
6398 to change the code. */
6399 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6401 if (code == CEIL_DIV_EXPR)
6402 code = FLOOR_DIV_EXPR;
6403 else if (code == FLOOR_DIV_EXPR)
6404 code = CEIL_DIV_EXPR;
6405 else if (code != MULT_EXPR
6406 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6407 break;
6410 /* If it's a multiply or a division/modulus operation of a multiple
6411 of our constant, do the operation and verify it doesn't overflow. */
6412 if (code == MULT_EXPR
6413 || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6414 TYPE_SIGN (type)))
6416 op1 = const_binop (code, fold_convert (ctype, op1),
6417 fold_convert (ctype, c));
6418 /* We allow the constant to overflow with wrapping semantics. */
6419 if (op1 == 0
6420 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6421 break;
6423 else
6424 break;
6426 /* If we have an unsigned type, we cannot widen the operation since it
6427 will change the result if the original computation overflowed. */
6428 if (TYPE_UNSIGNED (ctype) && ctype != type)
6429 break;
6431 /* The last case is if we are a multiply. In that case, we can
6432 apply the distributive law to commute the multiply and addition
6433 if the multiplication of the constants doesn't overflow
6434 and overflow is defined. With undefined overflow
6435 op0 * c might overflow, while (op0 + orig_op1) * c doesn't. */
6436 if (code == MULT_EXPR && TYPE_OVERFLOW_WRAPS (ctype))
6437 return fold_build2 (tcode, ctype,
6438 fold_build2 (code, ctype,
6439 fold_convert (ctype, op0),
6440 fold_convert (ctype, c)),
6441 op1);
6443 break;
6445 case MULT_EXPR:
6446 /* We have a special case here if we are doing something like
6447 (C * 8) % 4 since we know that's zero. */
6448 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6449 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6450 /* If the multiplication can overflow we cannot optimize this. */
6451 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6452 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6453 && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6454 TYPE_SIGN (type)))
6456 *strict_overflow_p = true;
6457 return omit_one_operand (type, integer_zero_node, op0);
6460 /* ... fall through ... */
6462 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6463 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6464 /* If we can extract our operation from the LHS, do so and return a
6465 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6466 do something only if the second operand is a constant. */
6467 if (same_p
6468 && TYPE_OVERFLOW_WRAPS (ctype)
6469 && (t1 = extract_muldiv (op0, c, code, wide_type,
6470 strict_overflow_p)) != 0)
6471 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6472 fold_convert (ctype, op1));
6473 else if (tcode == MULT_EXPR && code == MULT_EXPR
6474 && TYPE_OVERFLOW_WRAPS (ctype)
6475 && (t1 = extract_muldiv (op1, c, code, wide_type,
6476 strict_overflow_p)) != 0)
6477 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6478 fold_convert (ctype, t1));
6479 else if (TREE_CODE (op1) != INTEGER_CST)
6480 return 0;
6482 /* If these are the same operation types, we can associate them
6483 assuming no overflow. */
6484 if (tcode == code)
6486 bool overflow_p = false;
6487 bool overflow_mul_p;
6488 signop sign = TYPE_SIGN (ctype);
6489 unsigned prec = TYPE_PRECISION (ctype);
6490 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6491 wi::to_wide (c, prec),
6492 sign, &overflow_mul_p);
6493 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6494 if (overflow_mul_p
6495 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6496 overflow_p = true;
6497 if (!overflow_p)
6498 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6499 wide_int_to_tree (ctype, mul));
6502 /* If these operations "cancel" each other, we have the main
6503 optimizations of this pass, which occur when either constant is a
6504 multiple of the other, in which case we replace this with either an
6505 operation or CODE or TCODE.
6507 If we have an unsigned type, we cannot do this since it will change
6508 the result if the original computation overflowed. */
6509 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6510 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6511 || (tcode == MULT_EXPR
6512 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6513 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6514 && code != MULT_EXPR)))
6516 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6517 TYPE_SIGN (type)))
6519 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6520 *strict_overflow_p = true;
6521 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6522 fold_convert (ctype,
6523 const_binop (TRUNC_DIV_EXPR,
6524 op1, c)));
6526 else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6527 TYPE_SIGN (type)))
6529 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6530 *strict_overflow_p = true;
6531 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6532 fold_convert (ctype,
6533 const_binop (TRUNC_DIV_EXPR,
6534 c, op1)));
6537 break;
6539 default:
6540 break;
6543 return 0;
6546 /* Return a node which has the indicated constant VALUE (either 0 or
6547 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6548 and is of the indicated TYPE. */
6550 tree
6551 constant_boolean_node (bool value, tree type)
6553 if (type == integer_type_node)
6554 return value ? integer_one_node : integer_zero_node;
6555 else if (type == boolean_type_node)
6556 return value ? boolean_true_node : boolean_false_node;
6557 else if (TREE_CODE (type) == VECTOR_TYPE)
6558 return build_vector_from_val (type,
6559 build_int_cst (TREE_TYPE (type),
6560 value ? -1 : 0));
6561 else
6562 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6566 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6567 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6568 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6569 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6570 COND is the first argument to CODE; otherwise (as in the example
6571 given here), it is the second argument. TYPE is the type of the
6572 original expression. Return NULL_TREE if no simplification is
6573 possible. */
6575 static tree
6576 fold_binary_op_with_conditional_arg (location_t loc,
6577 enum tree_code code,
6578 tree type, tree op0, tree op1,
6579 tree cond, tree arg, int cond_first_p)
6581 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6582 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6583 tree test, true_value, false_value;
6584 tree lhs = NULL_TREE;
6585 tree rhs = NULL_TREE;
6586 enum tree_code cond_code = COND_EXPR;
6588 if (TREE_CODE (cond) == COND_EXPR
6589 || TREE_CODE (cond) == VEC_COND_EXPR)
6591 test = TREE_OPERAND (cond, 0);
6592 true_value = TREE_OPERAND (cond, 1);
6593 false_value = TREE_OPERAND (cond, 2);
6594 /* If this operand throws an expression, then it does not make
6595 sense to try to perform a logical or arithmetic operation
6596 involving it. */
6597 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6598 lhs = true_value;
6599 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6600 rhs = false_value;
6602 else if (!(TREE_CODE (type) != VECTOR_TYPE
6603 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6605 tree testtype = TREE_TYPE (cond);
6606 test = cond;
6607 true_value = constant_boolean_node (true, testtype);
6608 false_value = constant_boolean_node (false, testtype);
6610 else
6611 /* Detect the case of mixing vector and scalar types - bail out. */
6612 return NULL_TREE;
6614 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6615 cond_code = VEC_COND_EXPR;
6617 /* This transformation is only worthwhile if we don't have to wrap ARG
6618 in a SAVE_EXPR and the operation can be simplified without recursing
6619 on at least one of the branches once its pushed inside the COND_EXPR. */
6620 if (!TREE_CONSTANT (arg)
6621 && (TREE_SIDE_EFFECTS (arg)
6622 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6623 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6624 return NULL_TREE;
6626 arg = fold_convert_loc (loc, arg_type, arg);
6627 if (lhs == 0)
6629 true_value = fold_convert_loc (loc, cond_type, true_value);
6630 if (cond_first_p)
6631 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6632 else
6633 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6635 if (rhs == 0)
6637 false_value = fold_convert_loc (loc, cond_type, false_value);
6638 if (cond_first_p)
6639 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6640 else
6641 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6644 /* Check that we have simplified at least one of the branches. */
6645 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6646 return NULL_TREE;
6648 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6652 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6654 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6655 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6656 ADDEND is the same as X.
6658 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6659 and finite. The problematic cases are when X is zero, and its mode
6660 has signed zeros. In the case of rounding towards -infinity,
6661 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6662 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6664 bool
6665 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6667 if (!real_zerop (addend))
6668 return false;
6670 /* Don't allow the fold with -fsignaling-nans. */
6671 if (HONOR_SNANS (element_mode (type)))
6672 return false;
6674 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6675 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
6676 return true;
6678 /* In a vector or complex, we would need to check the sign of all zeros. */
6679 if (TREE_CODE (addend) != REAL_CST)
6680 return false;
6682 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6683 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6684 negate = !negate;
6686 /* The mode has signed zeros, and we have to honor their sign.
6687 In this situation, there is only one case we can return true for.
6688 X - 0 is the same as X unless rounding towards -infinity is
6689 supported. */
6690 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type));
6693 /* Subroutine of match.pd that optimizes comparisons of a division by
6694 a nonzero integer constant against an integer constant, i.e.
6695 X/C1 op C2.
6697 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6698 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6700 enum tree_code
6701 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
6702 tree *hi, bool *neg_overflow)
6704 tree prod, tmp, type = TREE_TYPE (c1);
6705 signop sign = TYPE_SIGN (type);
6706 bool overflow;
6708 /* We have to do this the hard way to detect unsigned overflow.
6709 prod = int_const_binop (MULT_EXPR, c1, c2); */
6710 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
6711 prod = force_fit_type (type, val, -1, overflow);
6712 *neg_overflow = false;
6714 if (sign == UNSIGNED)
6716 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6717 *lo = prod;
6719 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6720 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
6721 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
6723 else if (tree_int_cst_sgn (c1) >= 0)
6725 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6726 switch (tree_int_cst_sgn (c2))
6728 case -1:
6729 *neg_overflow = true;
6730 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
6731 *hi = prod;
6732 break;
6734 case 0:
6735 *lo = fold_negate_const (tmp, type);
6736 *hi = tmp;
6737 break;
6739 case 1:
6740 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
6741 *lo = prod;
6742 break;
6744 default:
6745 gcc_unreachable ();
6748 else
6750 /* A negative divisor reverses the relational operators. */
6751 code = swap_tree_comparison (code);
6753 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
6754 switch (tree_int_cst_sgn (c2))
6756 case -1:
6757 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
6758 *lo = prod;
6759 break;
6761 case 0:
6762 *hi = fold_negate_const (tmp, type);
6763 *lo = tmp;
6764 break;
6766 case 1:
6767 *neg_overflow = true;
6768 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
6769 *hi = prod;
6770 break;
6772 default:
6773 gcc_unreachable ();
6777 if (code != EQ_EXPR && code != NE_EXPR)
6778 return code;
6780 if (TREE_OVERFLOW (*lo)
6781 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
6782 *lo = NULL_TREE;
6783 if (TREE_OVERFLOW (*hi)
6784 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
6785 *hi = NULL_TREE;
6787 return code;
6791 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6792 equality/inequality test, then return a simplified form of the test
6793 using a sign testing. Otherwise return NULL. TYPE is the desired
6794 result type. */
6796 static tree
6797 fold_single_bit_test_into_sign_test (location_t loc,
6798 enum tree_code code, tree arg0, tree arg1,
6799 tree result_type)
6801 /* If this is testing a single bit, we can optimize the test. */
6802 if ((code == NE_EXPR || code == EQ_EXPR)
6803 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6804 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6806 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6807 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6808 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6810 if (arg00 != NULL_TREE
6811 /* This is only a win if casting to a signed type is cheap,
6812 i.e. when arg00's type is not a partial mode. */
6813 && type_has_mode_precision_p (TREE_TYPE (arg00)))
6815 tree stype = signed_type_for (TREE_TYPE (arg00));
6816 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6817 result_type,
6818 fold_convert_loc (loc, stype, arg00),
6819 build_int_cst (stype, 0));
6823 return NULL_TREE;
6826 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6827 equality/inequality test, then return a simplified form of
6828 the test using shifts and logical operations. Otherwise return
6829 NULL. TYPE is the desired result type. */
6831 tree
6832 fold_single_bit_test (location_t loc, enum tree_code code,
6833 tree arg0, tree arg1, tree result_type)
6835 /* If this is testing a single bit, we can optimize the test. */
6836 if ((code == NE_EXPR || code == EQ_EXPR)
6837 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6838 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6840 tree inner = TREE_OPERAND (arg0, 0);
6841 tree type = TREE_TYPE (arg0);
6842 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6843 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
6844 int ops_unsigned;
6845 tree signed_type, unsigned_type, intermediate_type;
6846 tree tem, one;
6848 /* First, see if we can fold the single bit test into a sign-bit
6849 test. */
6850 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6851 result_type);
6852 if (tem)
6853 return tem;
6855 /* Otherwise we have (A & C) != 0 where C is a single bit,
6856 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6857 Similarly for (A & C) == 0. */
6859 /* If INNER is a right shift of a constant and it plus BITNUM does
6860 not overflow, adjust BITNUM and INNER. */
6861 if (TREE_CODE (inner) == RSHIFT_EXPR
6862 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6863 && bitnum < TYPE_PRECISION (type)
6864 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner, 1)),
6865 TYPE_PRECISION (type) - bitnum))
6867 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6868 inner = TREE_OPERAND (inner, 0);
6871 /* If we are going to be able to omit the AND below, we must do our
6872 operations as unsigned. If we must use the AND, we have a choice.
6873 Normally unsigned is faster, but for some machines signed is. */
6874 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6875 && !flag_syntax_only) ? 0 : 1;
6877 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6878 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6879 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6880 inner = fold_convert_loc (loc, intermediate_type, inner);
6882 if (bitnum != 0)
6883 inner = build2 (RSHIFT_EXPR, intermediate_type,
6884 inner, size_int (bitnum));
6886 one = build_int_cst (intermediate_type, 1);
6888 if (code == EQ_EXPR)
6889 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6891 /* Put the AND last so it can combine with more things. */
6892 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6894 /* Make sure to return the proper type. */
6895 inner = fold_convert_loc (loc, result_type, inner);
6897 return inner;
6899 return NULL_TREE;
6902 /* Test whether it is preferable two swap two operands, ARG0 and
6903 ARG1, for example because ARG0 is an integer constant and ARG1
6904 isn't. */
6906 bool
6907 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6909 if (CONSTANT_CLASS_P (arg1))
6910 return 0;
6911 if (CONSTANT_CLASS_P (arg0))
6912 return 1;
6914 STRIP_NOPS (arg0);
6915 STRIP_NOPS (arg1);
6917 if (TREE_CONSTANT (arg1))
6918 return 0;
6919 if (TREE_CONSTANT (arg0))
6920 return 1;
6922 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6923 for commutative and comparison operators. Ensuring a canonical
6924 form allows the optimizers to find additional redundancies without
6925 having to explicitly check for both orderings. */
6926 if (TREE_CODE (arg0) == SSA_NAME
6927 && TREE_CODE (arg1) == SSA_NAME
6928 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6929 return 1;
6931 /* Put SSA_NAMEs last. */
6932 if (TREE_CODE (arg1) == SSA_NAME)
6933 return 0;
6934 if (TREE_CODE (arg0) == SSA_NAME)
6935 return 1;
6937 /* Put variables last. */
6938 if (DECL_P (arg1))
6939 return 0;
6940 if (DECL_P (arg0))
6941 return 1;
6943 return 0;
6947 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6948 means A >= Y && A != MAX, but in this case we know that
6949 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6951 static tree
6952 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
6954 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6956 if (TREE_CODE (bound) == LT_EXPR)
6957 a = TREE_OPERAND (bound, 0);
6958 else if (TREE_CODE (bound) == GT_EXPR)
6959 a = TREE_OPERAND (bound, 1);
6960 else
6961 return NULL_TREE;
6963 typea = TREE_TYPE (a);
6964 if (!INTEGRAL_TYPE_P (typea)
6965 && !POINTER_TYPE_P (typea))
6966 return NULL_TREE;
6968 if (TREE_CODE (ineq) == LT_EXPR)
6970 a1 = TREE_OPERAND (ineq, 1);
6971 y = TREE_OPERAND (ineq, 0);
6973 else if (TREE_CODE (ineq) == GT_EXPR)
6975 a1 = TREE_OPERAND (ineq, 0);
6976 y = TREE_OPERAND (ineq, 1);
6978 else
6979 return NULL_TREE;
6981 if (TREE_TYPE (a1) != typea)
6982 return NULL_TREE;
6984 if (POINTER_TYPE_P (typea))
6986 /* Convert the pointer types into integer before taking the difference. */
6987 tree ta = fold_convert_loc (loc, ssizetype, a);
6988 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
6989 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
6991 else
6992 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
6994 if (!diff || !integer_onep (diff))
6995 return NULL_TREE;
6997 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7000 /* Fold a sum or difference of at least one multiplication.
7001 Returns the folded tree or NULL if no simplification could be made. */
7003 static tree
7004 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7005 tree arg0, tree arg1)
7007 tree arg00, arg01, arg10, arg11;
7008 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7010 /* (A * C) +- (B * C) -> (A+-B) * C.
7011 (A * C) +- A -> A * (C+-1).
7012 We are most concerned about the case where C is a constant,
7013 but other combinations show up during loop reduction. Since
7014 it is not difficult, try all four possibilities. */
7016 if (TREE_CODE (arg0) == MULT_EXPR)
7018 arg00 = TREE_OPERAND (arg0, 0);
7019 arg01 = TREE_OPERAND (arg0, 1);
7021 else if (TREE_CODE (arg0) == INTEGER_CST)
7023 arg00 = build_one_cst (type);
7024 arg01 = arg0;
7026 else
7028 /* We cannot generate constant 1 for fract. */
7029 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7030 return NULL_TREE;
7031 arg00 = arg0;
7032 arg01 = build_one_cst (type);
7034 if (TREE_CODE (arg1) == MULT_EXPR)
7036 arg10 = TREE_OPERAND (arg1, 0);
7037 arg11 = TREE_OPERAND (arg1, 1);
7039 else if (TREE_CODE (arg1) == INTEGER_CST)
7041 arg10 = build_one_cst (type);
7042 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7043 the purpose of this canonicalization. */
7044 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7045 && negate_expr_p (arg1)
7046 && code == PLUS_EXPR)
7048 arg11 = negate_expr (arg1);
7049 code = MINUS_EXPR;
7051 else
7052 arg11 = arg1;
7054 else
7056 /* We cannot generate constant 1 for fract. */
7057 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7058 return NULL_TREE;
7059 arg10 = arg1;
7060 arg11 = build_one_cst (type);
7062 same = NULL_TREE;
7064 /* Prefer factoring a common non-constant. */
7065 if (operand_equal_p (arg00, arg10, 0))
7066 same = arg00, alt0 = arg01, alt1 = arg11;
7067 else if (operand_equal_p (arg01, arg11, 0))
7068 same = arg01, alt0 = arg00, alt1 = arg10;
7069 else if (operand_equal_p (arg00, arg11, 0))
7070 same = arg00, alt0 = arg01, alt1 = arg10;
7071 else if (operand_equal_p (arg01, arg10, 0))
7072 same = arg01, alt0 = arg00, alt1 = arg11;
7074 /* No identical multiplicands; see if we can find a common
7075 power-of-two factor in non-power-of-two multiplies. This
7076 can help in multi-dimensional array access. */
7077 else if (tree_fits_shwi_p (arg01)
7078 && tree_fits_shwi_p (arg11))
7080 HOST_WIDE_INT int01, int11, tmp;
7081 bool swap = false;
7082 tree maybe_same;
7083 int01 = tree_to_shwi (arg01);
7084 int11 = tree_to_shwi (arg11);
7086 /* Move min of absolute values to int11. */
7087 if (absu_hwi (int01) < absu_hwi (int11))
7089 tmp = int01, int01 = int11, int11 = tmp;
7090 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7091 maybe_same = arg01;
7092 swap = true;
7094 else
7095 maybe_same = arg11;
7097 if (exact_log2 (absu_hwi (int11)) > 0 && int01 % int11 == 0
7098 /* The remainder should not be a constant, otherwise we
7099 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7100 increased the number of multiplications necessary. */
7101 && TREE_CODE (arg10) != INTEGER_CST)
7103 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7104 build_int_cst (TREE_TYPE (arg00),
7105 int01 / int11));
7106 alt1 = arg10;
7107 same = maybe_same;
7108 if (swap)
7109 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7113 if (!same)
7114 return NULL_TREE;
7116 if (! INTEGRAL_TYPE_P (type)
7117 || TYPE_OVERFLOW_WRAPS (type)
7118 /* We are neither factoring zero nor minus one. */
7119 || TREE_CODE (same) == INTEGER_CST)
7120 return fold_build2_loc (loc, MULT_EXPR, type,
7121 fold_build2_loc (loc, code, type,
7122 fold_convert_loc (loc, type, alt0),
7123 fold_convert_loc (loc, type, alt1)),
7124 fold_convert_loc (loc, type, same));
7126 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7127 same may be minus one and thus the multiplication may overflow. Perform
7128 the sum operation in an unsigned type. */
7129 tree utype = unsigned_type_for (type);
7130 tree tem = fold_build2_loc (loc, code, utype,
7131 fold_convert_loc (loc, utype, alt0),
7132 fold_convert_loc (loc, utype, alt1));
7133 /* If the sum evaluated to a constant that is not -INF the multiplication
7134 cannot overflow. */
7135 if (TREE_CODE (tem) == INTEGER_CST
7136 && (wi::to_wide (tem)
7137 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7138 return fold_build2_loc (loc, MULT_EXPR, type,
7139 fold_convert (type, tem), same);
7141 /* Do not resort to unsigned multiplication because
7142 we lose the no-overflow property of the expression. */
7143 return NULL_TREE;
7146 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7147 specified by EXPR into the buffer PTR of length LEN bytes.
7148 Return the number of bytes placed in the buffer, or zero
7149 upon failure. */
7151 static int
7152 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7154 tree type = TREE_TYPE (expr);
7155 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7156 int byte, offset, word, words;
7157 unsigned char value;
7159 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7160 return 0;
7161 if (off == -1)
7162 off = 0;
7164 if (ptr == NULL)
7165 /* Dry run. */
7166 return MIN (len, total_bytes - off);
7168 words = total_bytes / UNITS_PER_WORD;
7170 for (byte = 0; byte < total_bytes; byte++)
7172 int bitpos = byte * BITS_PER_UNIT;
7173 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7174 number of bytes. */
7175 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7177 if (total_bytes > UNITS_PER_WORD)
7179 word = byte / UNITS_PER_WORD;
7180 if (WORDS_BIG_ENDIAN)
7181 word = (words - 1) - word;
7182 offset = word * UNITS_PER_WORD;
7183 if (BYTES_BIG_ENDIAN)
7184 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7185 else
7186 offset += byte % UNITS_PER_WORD;
7188 else
7189 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7190 if (offset >= off && offset - off < len)
7191 ptr[offset - off] = value;
7193 return MIN (len, total_bytes - off);
7197 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7198 specified by EXPR into the buffer PTR of length LEN bytes.
7199 Return the number of bytes placed in the buffer, or zero
7200 upon failure. */
7202 static int
7203 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7205 tree type = TREE_TYPE (expr);
7206 scalar_mode mode = SCALAR_TYPE_MODE (type);
7207 int total_bytes = GET_MODE_SIZE (mode);
7208 FIXED_VALUE_TYPE value;
7209 tree i_value, i_type;
7211 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7212 return 0;
7214 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7216 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7217 return 0;
7219 value = TREE_FIXED_CST (expr);
7220 i_value = double_int_to_tree (i_type, value.data);
7222 return native_encode_int (i_value, ptr, len, off);
7226 /* Subroutine of native_encode_expr. Encode the REAL_CST
7227 specified by EXPR into the buffer PTR of length LEN bytes.
7228 Return the number of bytes placed in the buffer, or zero
7229 upon failure. */
7231 static int
7232 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7234 tree type = TREE_TYPE (expr);
7235 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7236 int byte, offset, word, words, bitpos;
7237 unsigned char value;
7239 /* There are always 32 bits in each long, no matter the size of
7240 the hosts long. We handle floating point representations with
7241 up to 192 bits. */
7242 long tmp[6];
7244 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7245 return 0;
7246 if (off == -1)
7247 off = 0;
7249 if (ptr == NULL)
7250 /* Dry run. */
7251 return MIN (len, total_bytes - off);
7253 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7255 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7257 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7258 bitpos += BITS_PER_UNIT)
7260 byte = (bitpos / BITS_PER_UNIT) & 3;
7261 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7263 if (UNITS_PER_WORD < 4)
7265 word = byte / UNITS_PER_WORD;
7266 if (WORDS_BIG_ENDIAN)
7267 word = (words - 1) - word;
7268 offset = word * UNITS_PER_WORD;
7269 if (BYTES_BIG_ENDIAN)
7270 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7271 else
7272 offset += byte % UNITS_PER_WORD;
7274 else
7276 offset = byte;
7277 if (BYTES_BIG_ENDIAN)
7279 /* Reverse bytes within each long, or within the entire float
7280 if it's smaller than a long (for HFmode). */
7281 offset = MIN (3, total_bytes - 1) - offset;
7282 gcc_assert (offset >= 0);
7285 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7286 if (offset >= off
7287 && offset - off < len)
7288 ptr[offset - off] = value;
7290 return MIN (len, total_bytes - off);
7293 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7294 specified by EXPR into the buffer PTR of length LEN bytes.
7295 Return the number of bytes placed in the buffer, or zero
7296 upon failure. */
7298 static int
7299 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7301 int rsize, isize;
7302 tree part;
7304 part = TREE_REALPART (expr);
7305 rsize = native_encode_expr (part, ptr, len, off);
7306 if (off == -1 && rsize == 0)
7307 return 0;
7308 part = TREE_IMAGPART (expr);
7309 if (off != -1)
7310 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7311 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7312 len - rsize, off);
7313 if (off == -1 && isize != rsize)
7314 return 0;
7315 return rsize + isize;
7319 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7320 specified by EXPR into the buffer PTR of length LEN bytes.
7321 Return the number of bytes placed in the buffer, or zero
7322 upon failure. */
7324 static int
7325 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7327 unsigned HOST_WIDE_INT i, count;
7328 int size, offset;
7329 tree itype, elem;
7331 offset = 0;
7332 if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7333 return 0;
7334 itype = TREE_TYPE (TREE_TYPE (expr));
7335 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7336 for (i = 0; i < count; i++)
7338 if (off >= size)
7340 off -= size;
7341 continue;
7343 elem = VECTOR_CST_ELT (expr, i);
7344 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7345 len - offset, off);
7346 if ((off == -1 && res != size) || res == 0)
7347 return 0;
7348 offset += res;
7349 if (offset >= len)
7350 return (off == -1 && i < count - 1) ? 0 : offset;
7351 if (off != -1)
7352 off = 0;
7354 return offset;
7358 /* Subroutine of native_encode_expr. Encode the STRING_CST
7359 specified by EXPR into the buffer PTR of length LEN bytes.
7360 Return the number of bytes placed in the buffer, or zero
7361 upon failure. */
7363 static int
7364 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7366 tree type = TREE_TYPE (expr);
7368 /* Wide-char strings are encoded in target byte-order so native
7369 encoding them is trivial. */
7370 if (BITS_PER_UNIT != CHAR_BIT
7371 || TREE_CODE (type) != ARRAY_TYPE
7372 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7373 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7374 return 0;
7376 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7377 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7378 return 0;
7379 if (off == -1)
7380 off = 0;
7381 if (ptr == NULL)
7382 /* Dry run. */;
7383 else if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7385 int written = 0;
7386 if (off < TREE_STRING_LENGTH (expr))
7388 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7389 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7391 memset (ptr + written, 0,
7392 MIN (total_bytes - written, len - written));
7394 else
7395 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7396 return MIN (total_bytes - off, len);
7400 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7401 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7402 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7403 anything, just do a dry run. If OFF is not -1 then start
7404 the encoding at byte offset OFF and encode at most LEN bytes.
7405 Return the number of bytes placed in the buffer, or zero upon failure. */
7408 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7410 /* We don't support starting at negative offset and -1 is special. */
7411 if (off < -1)
7412 return 0;
7414 switch (TREE_CODE (expr))
7416 case INTEGER_CST:
7417 return native_encode_int (expr, ptr, len, off);
7419 case REAL_CST:
7420 return native_encode_real (expr, ptr, len, off);
7422 case FIXED_CST:
7423 return native_encode_fixed (expr, ptr, len, off);
7425 case COMPLEX_CST:
7426 return native_encode_complex (expr, ptr, len, off);
7428 case VECTOR_CST:
7429 return native_encode_vector (expr, ptr, len, off);
7431 case STRING_CST:
7432 return native_encode_string (expr, ptr, len, off);
7434 default:
7435 return 0;
7440 /* Subroutine of native_interpret_expr. Interpret the contents of
7441 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7442 If the buffer cannot be interpreted, return NULL_TREE. */
7444 static tree
7445 native_interpret_int (tree type, const unsigned char *ptr, int len)
7447 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7449 if (total_bytes > len
7450 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7451 return NULL_TREE;
7453 wide_int result = wi::from_buffer (ptr, total_bytes);
7455 return wide_int_to_tree (type, result);
7459 /* Subroutine of native_interpret_expr. Interpret the contents of
7460 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7461 If the buffer cannot be interpreted, return NULL_TREE. */
7463 static tree
7464 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7466 scalar_mode mode = SCALAR_TYPE_MODE (type);
7467 int total_bytes = GET_MODE_SIZE (mode);
7468 double_int result;
7469 FIXED_VALUE_TYPE fixed_value;
7471 if (total_bytes > len
7472 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7473 return NULL_TREE;
7475 result = double_int::from_buffer (ptr, total_bytes);
7476 fixed_value = fixed_from_double_int (result, mode);
7478 return build_fixed (type, fixed_value);
7482 /* Subroutine of native_interpret_expr. Interpret the contents of
7483 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7484 If the buffer cannot be interpreted, return NULL_TREE. */
7486 static tree
7487 native_interpret_real (tree type, const unsigned char *ptr, int len)
7489 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
7490 int total_bytes = GET_MODE_SIZE (mode);
7491 unsigned char value;
7492 /* There are always 32 bits in each long, no matter the size of
7493 the hosts long. We handle floating point representations with
7494 up to 192 bits. */
7495 REAL_VALUE_TYPE r;
7496 long tmp[6];
7498 if (total_bytes > len || total_bytes > 24)
7499 return NULL_TREE;
7500 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7502 memset (tmp, 0, sizeof (tmp));
7503 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7504 bitpos += BITS_PER_UNIT)
7506 /* Both OFFSET and BYTE index within a long;
7507 bitpos indexes the whole float. */
7508 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7509 if (UNITS_PER_WORD < 4)
7511 int word = byte / UNITS_PER_WORD;
7512 if (WORDS_BIG_ENDIAN)
7513 word = (words - 1) - word;
7514 offset = word * UNITS_PER_WORD;
7515 if (BYTES_BIG_ENDIAN)
7516 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7517 else
7518 offset += byte % UNITS_PER_WORD;
7520 else
7522 offset = byte;
7523 if (BYTES_BIG_ENDIAN)
7525 /* Reverse bytes within each long, or within the entire float
7526 if it's smaller than a long (for HFmode). */
7527 offset = MIN (3, total_bytes - 1) - offset;
7528 gcc_assert (offset >= 0);
7531 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7533 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7536 real_from_target (&r, tmp, mode);
7537 return build_real (type, r);
7541 /* Subroutine of native_interpret_expr. Interpret the contents of
7542 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7543 If the buffer cannot be interpreted, return NULL_TREE. */
7545 static tree
7546 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7548 tree etype, rpart, ipart;
7549 int size;
7551 etype = TREE_TYPE (type);
7552 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7553 if (size * 2 > len)
7554 return NULL_TREE;
7555 rpart = native_interpret_expr (etype, ptr, size);
7556 if (!rpart)
7557 return NULL_TREE;
7558 ipart = native_interpret_expr (etype, ptr+size, size);
7559 if (!ipart)
7560 return NULL_TREE;
7561 return build_complex (type, rpart, ipart);
7565 /* Subroutine of native_interpret_expr. Interpret the contents of
7566 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7567 If the buffer cannot be interpreted, return NULL_TREE. */
7569 static tree
7570 native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
7572 tree etype, elem;
7573 unsigned int i, size;
7574 unsigned HOST_WIDE_INT count;
7576 etype = TREE_TYPE (type);
7577 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7578 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&count)
7579 || size * count > len)
7580 return NULL_TREE;
7582 tree_vector_builder elements (type, count, 1);
7583 for (i = 0; i < count; ++i)
7585 elem = native_interpret_expr (etype, ptr+(i*size), size);
7586 if (!elem)
7587 return NULL_TREE;
7588 elements.quick_push (elem);
7590 return elements.build ();
7594 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7595 the buffer PTR of length LEN as a constant of type TYPE. For
7596 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7597 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7598 return NULL_TREE. */
7600 tree
7601 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7603 switch (TREE_CODE (type))
7605 case INTEGER_TYPE:
7606 case ENUMERAL_TYPE:
7607 case BOOLEAN_TYPE:
7608 case POINTER_TYPE:
7609 case REFERENCE_TYPE:
7610 return native_interpret_int (type, ptr, len);
7612 case REAL_TYPE:
7613 return native_interpret_real (type, ptr, len);
7615 case FIXED_POINT_TYPE:
7616 return native_interpret_fixed (type, ptr, len);
7618 case COMPLEX_TYPE:
7619 return native_interpret_complex (type, ptr, len);
7621 case VECTOR_TYPE:
7622 return native_interpret_vector (type, ptr, len);
7624 default:
7625 return NULL_TREE;
7629 /* Returns true if we can interpret the contents of a native encoding
7630 as TYPE. */
7632 static bool
7633 can_native_interpret_type_p (tree type)
7635 switch (TREE_CODE (type))
7637 case INTEGER_TYPE:
7638 case ENUMERAL_TYPE:
7639 case BOOLEAN_TYPE:
7640 case POINTER_TYPE:
7641 case REFERENCE_TYPE:
7642 case FIXED_POINT_TYPE:
7643 case REAL_TYPE:
7644 case COMPLEX_TYPE:
7645 case VECTOR_TYPE:
7646 return true;
7647 default:
7648 return false;
7653 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7654 TYPE at compile-time. If we're unable to perform the conversion
7655 return NULL_TREE. */
7657 static tree
7658 fold_view_convert_expr (tree type, tree expr)
7660 /* We support up to 512-bit values (for V8DFmode). */
7661 unsigned char buffer[64];
7662 int len;
7664 /* Check that the host and target are sane. */
7665 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7666 return NULL_TREE;
7668 len = native_encode_expr (expr, buffer, sizeof (buffer));
7669 if (len == 0)
7670 return NULL_TREE;
7672 return native_interpret_expr (type, buffer, len);
7675 /* Build an expression for the address of T. Folds away INDIRECT_REF
7676 to avoid confusing the gimplify process. */
7678 tree
7679 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7681 /* The size of the object is not relevant when talking about its address. */
7682 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7683 t = TREE_OPERAND (t, 0);
7685 if (TREE_CODE (t) == INDIRECT_REF)
7687 t = TREE_OPERAND (t, 0);
7689 if (TREE_TYPE (t) != ptrtype)
7690 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7692 else if (TREE_CODE (t) == MEM_REF
7693 && integer_zerop (TREE_OPERAND (t, 1)))
7694 return TREE_OPERAND (t, 0);
7695 else if (TREE_CODE (t) == MEM_REF
7696 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7697 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7698 TREE_OPERAND (t, 0),
7699 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7700 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7702 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7704 if (TREE_TYPE (t) != ptrtype)
7705 t = fold_convert_loc (loc, ptrtype, t);
7707 else
7708 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7710 return t;
7713 /* Build an expression for the address of T. */
7715 tree
7716 build_fold_addr_expr_loc (location_t loc, tree t)
7718 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7720 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7723 /* Fold a unary expression of code CODE and type TYPE with operand
7724 OP0. Return the folded expression if folding is successful.
7725 Otherwise, return NULL_TREE. */
7727 tree
7728 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7730 tree tem;
7731 tree arg0;
7732 enum tree_code_class kind = TREE_CODE_CLASS (code);
7734 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7735 && TREE_CODE_LENGTH (code) == 1);
7737 arg0 = op0;
7738 if (arg0)
7740 if (CONVERT_EXPR_CODE_P (code)
7741 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7743 /* Don't use STRIP_NOPS, because signedness of argument type
7744 matters. */
7745 STRIP_SIGN_NOPS (arg0);
7747 else
7749 /* Strip any conversions that don't change the mode. This
7750 is safe for every expression, except for a comparison
7751 expression because its signedness is derived from its
7752 operands.
7754 Note that this is done as an internal manipulation within
7755 the constant folder, in order to find the simplest
7756 representation of the arguments so that their form can be
7757 studied. In any cases, the appropriate type conversions
7758 should be put back in the tree that will get out of the
7759 constant folder. */
7760 STRIP_NOPS (arg0);
7763 if (CONSTANT_CLASS_P (arg0))
7765 tree tem = const_unop (code, type, arg0);
7766 if (tem)
7768 if (TREE_TYPE (tem) != type)
7769 tem = fold_convert_loc (loc, type, tem);
7770 return tem;
7775 tem = generic_simplify (loc, code, type, op0);
7776 if (tem)
7777 return tem;
7779 if (TREE_CODE_CLASS (code) == tcc_unary)
7781 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7782 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7783 fold_build1_loc (loc, code, type,
7784 fold_convert_loc (loc, TREE_TYPE (op0),
7785 TREE_OPERAND (arg0, 1))));
7786 else if (TREE_CODE (arg0) == COND_EXPR)
7788 tree arg01 = TREE_OPERAND (arg0, 1);
7789 tree arg02 = TREE_OPERAND (arg0, 2);
7790 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7791 arg01 = fold_build1_loc (loc, code, type,
7792 fold_convert_loc (loc,
7793 TREE_TYPE (op0), arg01));
7794 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7795 arg02 = fold_build1_loc (loc, code, type,
7796 fold_convert_loc (loc,
7797 TREE_TYPE (op0), arg02));
7798 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7799 arg01, arg02);
7801 /* If this was a conversion, and all we did was to move into
7802 inside the COND_EXPR, bring it back out. But leave it if
7803 it is a conversion from integer to integer and the
7804 result precision is no wider than a word since such a
7805 conversion is cheap and may be optimized away by combine,
7806 while it couldn't if it were outside the COND_EXPR. Then return
7807 so we don't get into an infinite recursion loop taking the
7808 conversion out and then back in. */
7810 if ((CONVERT_EXPR_CODE_P (code)
7811 || code == NON_LVALUE_EXPR)
7812 && TREE_CODE (tem) == COND_EXPR
7813 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7814 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7815 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7816 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7817 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7818 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7819 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7820 && (INTEGRAL_TYPE_P
7821 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7822 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7823 || flag_syntax_only))
7824 tem = build1_loc (loc, code, type,
7825 build3 (COND_EXPR,
7826 TREE_TYPE (TREE_OPERAND
7827 (TREE_OPERAND (tem, 1), 0)),
7828 TREE_OPERAND (tem, 0),
7829 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7830 TREE_OPERAND (TREE_OPERAND (tem, 2),
7831 0)));
7832 return tem;
7836 switch (code)
7838 case NON_LVALUE_EXPR:
7839 if (!maybe_lvalue_p (op0))
7840 return fold_convert_loc (loc, type, op0);
7841 return NULL_TREE;
7843 CASE_CONVERT:
7844 case FLOAT_EXPR:
7845 case FIX_TRUNC_EXPR:
7846 if (COMPARISON_CLASS_P (op0))
7848 /* If we have (type) (a CMP b) and type is an integral type, return
7849 new expression involving the new type. Canonicalize
7850 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7851 non-integral type.
7852 Do not fold the result as that would not simplify further, also
7853 folding again results in recursions. */
7854 if (TREE_CODE (type) == BOOLEAN_TYPE)
7855 return build2_loc (loc, TREE_CODE (op0), type,
7856 TREE_OPERAND (op0, 0),
7857 TREE_OPERAND (op0, 1));
7858 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7859 && TREE_CODE (type) != VECTOR_TYPE)
7860 return build3_loc (loc, COND_EXPR, type, op0,
7861 constant_boolean_node (true, type),
7862 constant_boolean_node (false, type));
7865 /* Handle (T *)&A.B.C for A being of type T and B and C
7866 living at offset zero. This occurs frequently in
7867 C++ upcasting and then accessing the base. */
7868 if (TREE_CODE (op0) == ADDR_EXPR
7869 && POINTER_TYPE_P (type)
7870 && handled_component_p (TREE_OPERAND (op0, 0)))
7872 poly_int64 bitsize, bitpos;
7873 tree offset;
7874 machine_mode mode;
7875 int unsignedp, reversep, volatilep;
7876 tree base
7877 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7878 &offset, &mode, &unsignedp, &reversep,
7879 &volatilep);
7880 /* If the reference was to a (constant) zero offset, we can use
7881 the address of the base if it has the same base type
7882 as the result type and the pointer type is unqualified. */
7883 if (!offset
7884 && known_eq (bitpos, 0)
7885 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7886 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7887 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7888 return fold_convert_loc (loc, type,
7889 build_fold_addr_expr_loc (loc, base));
7892 if (TREE_CODE (op0) == MODIFY_EXPR
7893 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7894 /* Detect assigning a bitfield. */
7895 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7896 && DECL_BIT_FIELD
7897 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7899 /* Don't leave an assignment inside a conversion
7900 unless assigning a bitfield. */
7901 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7902 /* First do the assignment, then return converted constant. */
7903 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7904 TREE_NO_WARNING (tem) = 1;
7905 TREE_USED (tem) = 1;
7906 return tem;
7909 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7910 constants (if x has signed type, the sign bit cannot be set
7911 in c). This folds extension into the BIT_AND_EXPR.
7912 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7913 very likely don't have maximal range for their precision and this
7914 transformation effectively doesn't preserve non-maximal ranges. */
7915 if (TREE_CODE (type) == INTEGER_TYPE
7916 && TREE_CODE (op0) == BIT_AND_EXPR
7917 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7919 tree and_expr = op0;
7920 tree and0 = TREE_OPERAND (and_expr, 0);
7921 tree and1 = TREE_OPERAND (and_expr, 1);
7922 int change = 0;
7924 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7925 || (TYPE_PRECISION (type)
7926 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7927 change = 1;
7928 else if (TYPE_PRECISION (TREE_TYPE (and1))
7929 <= HOST_BITS_PER_WIDE_INT
7930 && tree_fits_uhwi_p (and1))
7932 unsigned HOST_WIDE_INT cst;
7934 cst = tree_to_uhwi (and1);
7935 cst &= HOST_WIDE_INT_M1U
7936 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7937 change = (cst == 0);
7938 if (change
7939 && !flag_syntax_only
7940 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
7941 == ZERO_EXTEND))
7943 tree uns = unsigned_type_for (TREE_TYPE (and0));
7944 and0 = fold_convert_loc (loc, uns, and0);
7945 and1 = fold_convert_loc (loc, uns, and1);
7948 if (change)
7950 tem = force_fit_type (type, wi::to_widest (and1), 0,
7951 TREE_OVERFLOW (and1));
7952 return fold_build2_loc (loc, BIT_AND_EXPR, type,
7953 fold_convert_loc (loc, type, and0), tem);
7957 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
7958 cast (T1)X will fold away. We assume that this happens when X itself
7959 is a cast. */
7960 if (POINTER_TYPE_P (type)
7961 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7962 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
7964 tree arg00 = TREE_OPERAND (arg0, 0);
7965 tree arg01 = TREE_OPERAND (arg0, 1);
7967 return fold_build_pointer_plus_loc
7968 (loc, fold_convert_loc (loc, type, arg00), arg01);
7971 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7972 of the same precision, and X is an integer type not narrower than
7973 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7974 if (INTEGRAL_TYPE_P (type)
7975 && TREE_CODE (op0) == BIT_NOT_EXPR
7976 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7977 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7978 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7980 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7981 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7982 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7983 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
7984 fold_convert_loc (loc, type, tem));
7987 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7988 type of X and Y (integer types only). */
7989 if (INTEGRAL_TYPE_P (type)
7990 && TREE_CODE (op0) == MULT_EXPR
7991 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7992 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7994 /* Be careful not to introduce new overflows. */
7995 tree mult_type;
7996 if (TYPE_OVERFLOW_WRAPS (type))
7997 mult_type = type;
7998 else
7999 mult_type = unsigned_type_for (type);
8001 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8003 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8004 fold_convert_loc (loc, mult_type,
8005 TREE_OPERAND (op0, 0)),
8006 fold_convert_loc (loc, mult_type,
8007 TREE_OPERAND (op0, 1)));
8008 return fold_convert_loc (loc, type, tem);
8012 return NULL_TREE;
8014 case VIEW_CONVERT_EXPR:
8015 if (TREE_CODE (op0) == MEM_REF)
8017 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
8018 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
8019 tem = fold_build2_loc (loc, MEM_REF, type,
8020 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8021 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
8022 return tem;
8025 return NULL_TREE;
8027 case NEGATE_EXPR:
8028 tem = fold_negate_expr (loc, arg0);
8029 if (tem)
8030 return fold_convert_loc (loc, type, tem);
8031 return NULL_TREE;
8033 case ABS_EXPR:
8034 /* Convert fabs((double)float) into (double)fabsf(float). */
8035 if (TREE_CODE (arg0) == NOP_EXPR
8036 && TREE_CODE (type) == REAL_TYPE)
8038 tree targ0 = strip_float_extensions (arg0);
8039 if (targ0 != arg0)
8040 return fold_convert_loc (loc, type,
8041 fold_build1_loc (loc, ABS_EXPR,
8042 TREE_TYPE (targ0),
8043 targ0));
8045 return NULL_TREE;
8047 case BIT_NOT_EXPR:
8048 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8049 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8050 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8051 fold_convert_loc (loc, type,
8052 TREE_OPERAND (arg0, 0)))))
8053 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8054 fold_convert_loc (loc, type,
8055 TREE_OPERAND (arg0, 1)));
8056 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8057 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8058 fold_convert_loc (loc, type,
8059 TREE_OPERAND (arg0, 1)))))
8060 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8061 fold_convert_loc (loc, type,
8062 TREE_OPERAND (arg0, 0)), tem);
8064 return NULL_TREE;
8066 case TRUTH_NOT_EXPR:
8067 /* Note that the operand of this must be an int
8068 and its values must be 0 or 1.
8069 ("true" is a fixed value perhaps depending on the language,
8070 but we don't handle values other than 1 correctly yet.) */
8071 tem = fold_truth_not_expr (loc, arg0);
8072 if (!tem)
8073 return NULL_TREE;
8074 return fold_convert_loc (loc, type, tem);
8076 case INDIRECT_REF:
8077 /* Fold *&X to X if X is an lvalue. */
8078 if (TREE_CODE (op0) == ADDR_EXPR)
8080 tree op00 = TREE_OPERAND (op0, 0);
8081 if ((VAR_P (op00)
8082 || TREE_CODE (op00) == PARM_DECL
8083 || TREE_CODE (op00) == RESULT_DECL)
8084 && !TREE_READONLY (op00))
8085 return op00;
8087 return NULL_TREE;
8089 default:
8090 return NULL_TREE;
8091 } /* switch (code) */
8095 /* If the operation was a conversion do _not_ mark a resulting constant
8096 with TREE_OVERFLOW if the original constant was not. These conversions
8097 have implementation defined behavior and retaining the TREE_OVERFLOW
8098 flag here would confuse later passes such as VRP. */
8099 tree
8100 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8101 tree type, tree op0)
8103 tree res = fold_unary_loc (loc, code, type, op0);
8104 if (res
8105 && TREE_CODE (res) == INTEGER_CST
8106 && TREE_CODE (op0) == INTEGER_CST
8107 && CONVERT_EXPR_CODE_P (code))
8108 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8110 return res;
8113 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8114 operands OP0 and OP1. LOC is the location of the resulting expression.
8115 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8116 Return the folded expression if folding is successful. Otherwise,
8117 return NULL_TREE. */
8118 static tree
8119 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8120 tree arg0, tree arg1, tree op0, tree op1)
8122 tree tem;
8124 /* We only do these simplifications if we are optimizing. */
8125 if (!optimize)
8126 return NULL_TREE;
8128 /* Check for things like (A || B) && (A || C). We can convert this
8129 to A || (B && C). Note that either operator can be any of the four
8130 truth and/or operations and the transformation will still be
8131 valid. Also note that we only care about order for the
8132 ANDIF and ORIF operators. If B contains side effects, this
8133 might change the truth-value of A. */
8134 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8135 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8136 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8137 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8138 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8139 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8141 tree a00 = TREE_OPERAND (arg0, 0);
8142 tree a01 = TREE_OPERAND (arg0, 1);
8143 tree a10 = TREE_OPERAND (arg1, 0);
8144 tree a11 = TREE_OPERAND (arg1, 1);
8145 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8146 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8147 && (code == TRUTH_AND_EXPR
8148 || code == TRUTH_OR_EXPR));
8150 if (operand_equal_p (a00, a10, 0))
8151 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8152 fold_build2_loc (loc, code, type, a01, a11));
8153 else if (commutative && operand_equal_p (a00, a11, 0))
8154 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8155 fold_build2_loc (loc, code, type, a01, a10));
8156 else if (commutative && operand_equal_p (a01, a10, 0))
8157 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8158 fold_build2_loc (loc, code, type, a00, a11));
8160 /* This case if tricky because we must either have commutative
8161 operators or else A10 must not have side-effects. */
8163 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8164 && operand_equal_p (a01, a11, 0))
8165 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8166 fold_build2_loc (loc, code, type, a00, a10),
8167 a01);
8170 /* See if we can build a range comparison. */
8171 if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
8172 return tem;
8174 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8175 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8177 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8178 if (tem)
8179 return fold_build2_loc (loc, code, type, tem, arg1);
8182 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8183 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8185 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8186 if (tem)
8187 return fold_build2_loc (loc, code, type, arg0, tem);
8190 /* Check for the possibility of merging component references. If our
8191 lhs is another similar operation, try to merge its rhs with our
8192 rhs. Then try to merge our lhs and rhs. */
8193 if (TREE_CODE (arg0) == code
8194 && (tem = fold_truth_andor_1 (loc, code, type,
8195 TREE_OPERAND (arg0, 1), arg1)) != 0)
8196 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8198 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8199 return tem;
8201 if (LOGICAL_OP_NON_SHORT_CIRCUIT
8202 && !flag_sanitize_coverage
8203 && (code == TRUTH_AND_EXPR
8204 || code == TRUTH_ANDIF_EXPR
8205 || code == TRUTH_OR_EXPR
8206 || code == TRUTH_ORIF_EXPR))
8208 enum tree_code ncode, icode;
8210 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8211 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8212 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8214 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8215 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8216 We don't want to pack more than two leafs to a non-IF AND/OR
8217 expression.
8218 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8219 equal to IF-CODE, then we don't want to add right-hand operand.
8220 If the inner right-hand side of left-hand operand has
8221 side-effects, or isn't simple, then we can't add to it,
8222 as otherwise we might destroy if-sequence. */
8223 if (TREE_CODE (arg0) == icode
8224 && simple_operand_p_2 (arg1)
8225 /* Needed for sequence points to handle trappings, and
8226 side-effects. */
8227 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8229 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8230 arg1);
8231 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8232 tem);
8234 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8235 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8236 else if (TREE_CODE (arg1) == icode
8237 && simple_operand_p_2 (arg0)
8238 /* Needed for sequence points to handle trappings, and
8239 side-effects. */
8240 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8242 tem = fold_build2_loc (loc, ncode, type,
8243 arg0, TREE_OPERAND (arg1, 0));
8244 return fold_build2_loc (loc, icode, type, tem,
8245 TREE_OPERAND (arg1, 1));
8247 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8248 into (A OR B).
8249 For sequence point consistancy, we need to check for trapping,
8250 and side-effects. */
8251 else if (code == icode && simple_operand_p_2 (arg0)
8252 && simple_operand_p_2 (arg1))
8253 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8256 return NULL_TREE;
8259 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8260 by changing CODE to reduce the magnitude of constants involved in
8261 ARG0 of the comparison.
8262 Returns a canonicalized comparison tree if a simplification was
8263 possible, otherwise returns NULL_TREE.
8264 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8265 valid if signed overflow is undefined. */
8267 static tree
8268 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8269 tree arg0, tree arg1,
8270 bool *strict_overflow_p)
8272 enum tree_code code0 = TREE_CODE (arg0);
8273 tree t, cst0 = NULL_TREE;
8274 int sgn0;
8276 /* Match A +- CST code arg1. We can change this only if overflow
8277 is undefined. */
8278 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8279 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8280 /* In principle pointers also have undefined overflow behavior,
8281 but that causes problems elsewhere. */
8282 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8283 && (code0 == MINUS_EXPR
8284 || code0 == PLUS_EXPR)
8285 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8286 return NULL_TREE;
8288 /* Identify the constant in arg0 and its sign. */
8289 cst0 = TREE_OPERAND (arg0, 1);
8290 sgn0 = tree_int_cst_sgn (cst0);
8292 /* Overflowed constants and zero will cause problems. */
8293 if (integer_zerop (cst0)
8294 || TREE_OVERFLOW (cst0))
8295 return NULL_TREE;
8297 /* See if we can reduce the magnitude of the constant in
8298 arg0 by changing the comparison code. */
8299 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8300 if (code == LT_EXPR
8301 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8302 code = LE_EXPR;
8303 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8304 else if (code == GT_EXPR
8305 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8306 code = GE_EXPR;
8307 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8308 else if (code == LE_EXPR
8309 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8310 code = LT_EXPR;
8311 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8312 else if (code == GE_EXPR
8313 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8314 code = GT_EXPR;
8315 else
8316 return NULL_TREE;
8317 *strict_overflow_p = true;
8319 /* Now build the constant reduced in magnitude. But not if that
8320 would produce one outside of its types range. */
8321 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8322 && ((sgn0 == 1
8323 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8324 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8325 || (sgn0 == -1
8326 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8327 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8328 return NULL_TREE;
8330 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8331 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8332 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8333 t = fold_convert (TREE_TYPE (arg1), t);
8335 return fold_build2_loc (loc, code, type, t, arg1);
8338 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8339 overflow further. Try to decrease the magnitude of constants involved
8340 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8341 and put sole constants at the second argument position.
8342 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8344 static tree
8345 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8346 tree arg0, tree arg1)
8348 tree t;
8349 bool strict_overflow_p;
8350 const char * const warnmsg = G_("assuming signed overflow does not occur "
8351 "when reducing constant in comparison");
8353 /* Try canonicalization by simplifying arg0. */
8354 strict_overflow_p = false;
8355 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8356 &strict_overflow_p);
8357 if (t)
8359 if (strict_overflow_p)
8360 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8361 return t;
8364 /* Try canonicalization by simplifying arg1 using the swapped
8365 comparison. */
8366 code = swap_tree_comparison (code);
8367 strict_overflow_p = false;
8368 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8369 &strict_overflow_p);
8370 if (t && strict_overflow_p)
8371 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8372 return t;
8375 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8376 space. This is used to avoid issuing overflow warnings for
8377 expressions like &p->x which can not wrap. */
8379 static bool
8380 pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
8382 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8383 return true;
8385 if (maybe_lt (bitpos, 0))
8386 return true;
8388 poly_wide_int wi_offset;
8389 int precision = TYPE_PRECISION (TREE_TYPE (base));
8390 if (offset == NULL_TREE)
8391 wi_offset = wi::zero (precision);
8392 else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
8393 return true;
8394 else
8395 wi_offset = wi::to_poly_wide (offset);
8397 bool overflow;
8398 poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
8399 precision);
8400 poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8401 if (overflow)
8402 return true;
8404 poly_uint64 total_hwi, size;
8405 if (!total.to_uhwi (&total_hwi)
8406 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
8407 &size)
8408 || known_eq (size, 0U))
8409 return true;
8411 if (known_le (total_hwi, size))
8412 return false;
8414 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8415 array. */
8416 if (TREE_CODE (base) == ADDR_EXPR
8417 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
8418 &size)
8419 && maybe_ne (size, 0U)
8420 && known_le (total_hwi, size))
8421 return false;
8423 return true;
8426 /* Return a positive integer when the symbol DECL is known to have
8427 a nonzero address, zero when it's known not to (e.g., it's a weak
8428 symbol), and a negative integer when the symbol is not yet in the
8429 symbol table and so whether or not its address is zero is unknown.
8430 For function local objects always return positive integer. */
8431 static int
8432 maybe_nonzero_address (tree decl)
8434 if (DECL_P (decl) && decl_in_symtab_p (decl))
8435 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8436 return symbol->nonzero_address ();
8438 /* Function local objects are never NULL. */
8439 if (DECL_P (decl)
8440 && (DECL_CONTEXT (decl)
8441 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8442 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8443 return 1;
8445 return -1;
8448 /* Subroutine of fold_binary. This routine performs all of the
8449 transformations that are common to the equality/inequality
8450 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8451 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8452 fold_binary should call fold_binary. Fold a comparison with
8453 tree code CODE and type TYPE with operands OP0 and OP1. Return
8454 the folded comparison or NULL_TREE. */
8456 static tree
8457 fold_comparison (location_t loc, enum tree_code code, tree type,
8458 tree op0, tree op1)
8460 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8461 tree arg0, arg1, tem;
8463 arg0 = op0;
8464 arg1 = op1;
8466 STRIP_SIGN_NOPS (arg0);
8467 STRIP_SIGN_NOPS (arg1);
8469 /* For comparisons of pointers we can decompose it to a compile time
8470 comparison of the base objects and the offsets into the object.
8471 This requires at least one operand being an ADDR_EXPR or a
8472 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8473 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8474 && (TREE_CODE (arg0) == ADDR_EXPR
8475 || TREE_CODE (arg1) == ADDR_EXPR
8476 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8477 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8479 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8480 poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
8481 machine_mode mode;
8482 int volatilep, reversep, unsignedp;
8483 bool indirect_base0 = false, indirect_base1 = false;
8485 /* Get base and offset for the access. Strip ADDR_EXPR for
8486 get_inner_reference, but put it back by stripping INDIRECT_REF
8487 off the base object if possible. indirect_baseN will be true
8488 if baseN is not an address but refers to the object itself. */
8489 base0 = arg0;
8490 if (TREE_CODE (arg0) == ADDR_EXPR)
8492 base0
8493 = get_inner_reference (TREE_OPERAND (arg0, 0),
8494 &bitsize, &bitpos0, &offset0, &mode,
8495 &unsignedp, &reversep, &volatilep);
8496 if (TREE_CODE (base0) == INDIRECT_REF)
8497 base0 = TREE_OPERAND (base0, 0);
8498 else
8499 indirect_base0 = true;
8501 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8503 base0 = TREE_OPERAND (arg0, 0);
8504 STRIP_SIGN_NOPS (base0);
8505 if (TREE_CODE (base0) == ADDR_EXPR)
8507 base0
8508 = get_inner_reference (TREE_OPERAND (base0, 0),
8509 &bitsize, &bitpos0, &offset0, &mode,
8510 &unsignedp, &reversep, &volatilep);
8511 if (TREE_CODE (base0) == INDIRECT_REF)
8512 base0 = TREE_OPERAND (base0, 0);
8513 else
8514 indirect_base0 = true;
8516 if (offset0 == NULL_TREE || integer_zerop (offset0))
8517 offset0 = TREE_OPERAND (arg0, 1);
8518 else
8519 offset0 = size_binop (PLUS_EXPR, offset0,
8520 TREE_OPERAND (arg0, 1));
8521 if (poly_int_tree_p (offset0))
8523 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
8524 TYPE_PRECISION (sizetype));
8525 tem <<= LOG2_BITS_PER_UNIT;
8526 tem += bitpos0;
8527 if (tem.to_shwi (&bitpos0))
8528 offset0 = NULL_TREE;
8532 base1 = arg1;
8533 if (TREE_CODE (arg1) == ADDR_EXPR)
8535 base1
8536 = get_inner_reference (TREE_OPERAND (arg1, 0),
8537 &bitsize, &bitpos1, &offset1, &mode,
8538 &unsignedp, &reversep, &volatilep);
8539 if (TREE_CODE (base1) == INDIRECT_REF)
8540 base1 = TREE_OPERAND (base1, 0);
8541 else
8542 indirect_base1 = true;
8544 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8546 base1 = TREE_OPERAND (arg1, 0);
8547 STRIP_SIGN_NOPS (base1);
8548 if (TREE_CODE (base1) == ADDR_EXPR)
8550 base1
8551 = get_inner_reference (TREE_OPERAND (base1, 0),
8552 &bitsize, &bitpos1, &offset1, &mode,
8553 &unsignedp, &reversep, &volatilep);
8554 if (TREE_CODE (base1) == INDIRECT_REF)
8555 base1 = TREE_OPERAND (base1, 0);
8556 else
8557 indirect_base1 = true;
8559 if (offset1 == NULL_TREE || integer_zerop (offset1))
8560 offset1 = TREE_OPERAND (arg1, 1);
8561 else
8562 offset1 = size_binop (PLUS_EXPR, offset1,
8563 TREE_OPERAND (arg1, 1));
8564 if (poly_int_tree_p (offset1))
8566 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
8567 TYPE_PRECISION (sizetype));
8568 tem <<= LOG2_BITS_PER_UNIT;
8569 tem += bitpos1;
8570 if (tem.to_shwi (&bitpos1))
8571 offset1 = NULL_TREE;
8575 /* If we have equivalent bases we might be able to simplify. */
8576 if (indirect_base0 == indirect_base1
8577 && operand_equal_p (base0, base1,
8578 indirect_base0 ? OEP_ADDRESS_OF : 0))
8580 /* We can fold this expression to a constant if the non-constant
8581 offset parts are equal. */
8582 if ((offset0 == offset1
8583 || (offset0 && offset1
8584 && operand_equal_p (offset0, offset1, 0)))
8585 && (equality_code
8586 || (indirect_base0
8587 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8588 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8590 if (!equality_code
8591 && maybe_ne (bitpos0, bitpos1)
8592 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8593 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8594 fold_overflow_warning (("assuming pointer wraparound does not "
8595 "occur when comparing P +- C1 with "
8596 "P +- C2"),
8597 WARN_STRICT_OVERFLOW_CONDITIONAL);
8599 switch (code)
8601 case EQ_EXPR:
8602 if (known_eq (bitpos0, bitpos1))
8603 return constant_boolean_node (true, type);
8604 if (known_ne (bitpos0, bitpos1))
8605 return constant_boolean_node (false, type);
8606 break;
8607 case NE_EXPR:
8608 if (known_ne (bitpos0, bitpos1))
8609 return constant_boolean_node (true, type);
8610 if (known_eq (bitpos0, bitpos1))
8611 return constant_boolean_node (false, type);
8612 break;
8613 case LT_EXPR:
8614 if (known_lt (bitpos0, bitpos1))
8615 return constant_boolean_node (true, type);
8616 if (known_ge (bitpos0, bitpos1))
8617 return constant_boolean_node (false, type);
8618 break;
8619 case LE_EXPR:
8620 if (known_le (bitpos0, bitpos1))
8621 return constant_boolean_node (true, type);
8622 if (known_gt (bitpos0, bitpos1))
8623 return constant_boolean_node (false, type);
8624 break;
8625 case GE_EXPR:
8626 if (known_ge (bitpos0, bitpos1))
8627 return constant_boolean_node (true, type);
8628 if (known_lt (bitpos0, bitpos1))
8629 return constant_boolean_node (false, type);
8630 break;
8631 case GT_EXPR:
8632 if (known_gt (bitpos0, bitpos1))
8633 return constant_boolean_node (true, type);
8634 if (known_le (bitpos0, bitpos1))
8635 return constant_boolean_node (false, type);
8636 break;
8637 default:;
8640 /* We can simplify the comparison to a comparison of the variable
8641 offset parts if the constant offset parts are equal.
8642 Be careful to use signed sizetype here because otherwise we
8643 mess with array offsets in the wrong way. This is possible
8644 because pointer arithmetic is restricted to retain within an
8645 object and overflow on pointer differences is undefined as of
8646 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8647 else if (known_eq (bitpos0, bitpos1)
8648 && (equality_code
8649 || (indirect_base0
8650 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8651 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8653 /* By converting to signed sizetype we cover middle-end pointer
8654 arithmetic which operates on unsigned pointer types of size
8655 type size and ARRAY_REF offsets which are properly sign or
8656 zero extended from their type in case it is narrower than
8657 sizetype. */
8658 if (offset0 == NULL_TREE)
8659 offset0 = build_int_cst (ssizetype, 0);
8660 else
8661 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8662 if (offset1 == NULL_TREE)
8663 offset1 = build_int_cst (ssizetype, 0);
8664 else
8665 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8667 if (!equality_code
8668 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8669 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8670 fold_overflow_warning (("assuming pointer wraparound does not "
8671 "occur when comparing P +- C1 with "
8672 "P +- C2"),
8673 WARN_STRICT_OVERFLOW_COMPARISON);
8675 return fold_build2_loc (loc, code, type, offset0, offset1);
8678 /* For equal offsets we can simplify to a comparison of the
8679 base addresses. */
8680 else if (known_eq (bitpos0, bitpos1)
8681 && (indirect_base0
8682 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8683 && (indirect_base1
8684 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8685 && ((offset0 == offset1)
8686 || (offset0 && offset1
8687 && operand_equal_p (offset0, offset1, 0))))
8689 if (indirect_base0)
8690 base0 = build_fold_addr_expr_loc (loc, base0);
8691 if (indirect_base1)
8692 base1 = build_fold_addr_expr_loc (loc, base1);
8693 return fold_build2_loc (loc, code, type, base0, base1);
8695 /* Comparison between an ordinary (non-weak) symbol and a null
8696 pointer can be eliminated since such symbols must have a non
8697 null address. In C, relational expressions between pointers
8698 to objects and null pointers are undefined. The results
8699 below follow the C++ rules with the additional property that
8700 every object pointer compares greater than a null pointer.
8702 else if (((DECL_P (base0)
8703 && maybe_nonzero_address (base0) > 0
8704 /* Avoid folding references to struct members at offset 0 to
8705 prevent tests like '&ptr->firstmember == 0' from getting
8706 eliminated. When ptr is null, although the -> expression
8707 is strictly speaking invalid, GCC retains it as a matter
8708 of QoI. See PR c/44555. */
8709 && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
8710 || CONSTANT_CLASS_P (base0))
8711 && indirect_base0
8712 /* The caller guarantees that when one of the arguments is
8713 constant (i.e., null in this case) it is second. */
8714 && integer_zerop (arg1))
8716 switch (code)
8718 case EQ_EXPR:
8719 case LE_EXPR:
8720 case LT_EXPR:
8721 return constant_boolean_node (false, type);
8722 case GE_EXPR:
8723 case GT_EXPR:
8724 case NE_EXPR:
8725 return constant_boolean_node (true, type);
8726 default:
8727 gcc_unreachable ();
8732 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8733 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8734 the resulting offset is smaller in absolute value than the
8735 original one and has the same sign. */
8736 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8737 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8738 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8739 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8740 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8741 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8742 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8743 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8745 tree const1 = TREE_OPERAND (arg0, 1);
8746 tree const2 = TREE_OPERAND (arg1, 1);
8747 tree variable1 = TREE_OPERAND (arg0, 0);
8748 tree variable2 = TREE_OPERAND (arg1, 0);
8749 tree cst;
8750 const char * const warnmsg = G_("assuming signed overflow does not "
8751 "occur when combining constants around "
8752 "a comparison");
8754 /* Put the constant on the side where it doesn't overflow and is
8755 of lower absolute value and of same sign than before. */
8756 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8757 ? MINUS_EXPR : PLUS_EXPR,
8758 const2, const1);
8759 if (!TREE_OVERFLOW (cst)
8760 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8761 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8763 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8764 return fold_build2_loc (loc, code, type,
8765 variable1,
8766 fold_build2_loc (loc, TREE_CODE (arg1),
8767 TREE_TYPE (arg1),
8768 variable2, cst));
8771 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8772 ? MINUS_EXPR : PLUS_EXPR,
8773 const1, const2);
8774 if (!TREE_OVERFLOW (cst)
8775 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8776 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8778 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8779 return fold_build2_loc (loc, code, type,
8780 fold_build2_loc (loc, TREE_CODE (arg0),
8781 TREE_TYPE (arg0),
8782 variable1, cst),
8783 variable2);
8787 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8788 if (tem)
8789 return tem;
8791 /* If we are comparing an expression that just has comparisons
8792 of two integer values, arithmetic expressions of those comparisons,
8793 and constants, we can simplify it. There are only three cases
8794 to check: the two values can either be equal, the first can be
8795 greater, or the second can be greater. Fold the expression for
8796 those three values. Since each value must be 0 or 1, we have
8797 eight possibilities, each of which corresponds to the constant 0
8798 or 1 or one of the six possible comparisons.
8800 This handles common cases like (a > b) == 0 but also handles
8801 expressions like ((x > y) - (y > x)) > 0, which supposedly
8802 occur in macroized code. */
8804 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8806 tree cval1 = 0, cval2 = 0;
8808 if (twoval_comparison_p (arg0, &cval1, &cval2)
8809 /* Don't handle degenerate cases here; they should already
8810 have been handled anyway. */
8811 && cval1 != 0 && cval2 != 0
8812 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8813 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8814 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8815 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8816 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8817 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8818 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8820 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8821 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8823 /* We can't just pass T to eval_subst in case cval1 or cval2
8824 was the same as ARG1. */
8826 tree high_result
8827 = fold_build2_loc (loc, code, type,
8828 eval_subst (loc, arg0, cval1, maxval,
8829 cval2, minval),
8830 arg1);
8831 tree equal_result
8832 = fold_build2_loc (loc, code, type,
8833 eval_subst (loc, arg0, cval1, maxval,
8834 cval2, maxval),
8835 arg1);
8836 tree low_result
8837 = fold_build2_loc (loc, code, type,
8838 eval_subst (loc, arg0, cval1, minval,
8839 cval2, maxval),
8840 arg1);
8842 /* All three of these results should be 0 or 1. Confirm they are.
8843 Then use those values to select the proper code to use. */
8845 if (TREE_CODE (high_result) == INTEGER_CST
8846 && TREE_CODE (equal_result) == INTEGER_CST
8847 && TREE_CODE (low_result) == INTEGER_CST)
8849 /* Make a 3-bit mask with the high-order bit being the
8850 value for `>', the next for '=', and the low for '<'. */
8851 switch ((integer_onep (high_result) * 4)
8852 + (integer_onep (equal_result) * 2)
8853 + integer_onep (low_result))
8855 case 0:
8856 /* Always false. */
8857 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8858 case 1:
8859 code = LT_EXPR;
8860 break;
8861 case 2:
8862 code = EQ_EXPR;
8863 break;
8864 case 3:
8865 code = LE_EXPR;
8866 break;
8867 case 4:
8868 code = GT_EXPR;
8869 break;
8870 case 5:
8871 code = NE_EXPR;
8872 break;
8873 case 6:
8874 code = GE_EXPR;
8875 break;
8876 case 7:
8877 /* Always true. */
8878 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8881 return fold_build2_loc (loc, code, type, cval1, cval2);
8886 return NULL_TREE;
8890 /* Subroutine of fold_binary. Optimize complex multiplications of the
8891 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8892 argument EXPR represents the expression "z" of type TYPE. */
8894 static tree
8895 fold_mult_zconjz (location_t loc, tree type, tree expr)
8897 tree itype = TREE_TYPE (type);
8898 tree rpart, ipart, tem;
8900 if (TREE_CODE (expr) == COMPLEX_EXPR)
8902 rpart = TREE_OPERAND (expr, 0);
8903 ipart = TREE_OPERAND (expr, 1);
8905 else if (TREE_CODE (expr) == COMPLEX_CST)
8907 rpart = TREE_REALPART (expr);
8908 ipart = TREE_IMAGPART (expr);
8910 else
8912 expr = save_expr (expr);
8913 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8914 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8917 rpart = save_expr (rpart);
8918 ipart = save_expr (ipart);
8919 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8920 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8921 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8922 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8923 build_zero_cst (itype));
8927 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8928 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8929 true if successful. */
8931 static bool
8932 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
8934 unsigned HOST_WIDE_INT i, nunits;
8936 if (TREE_CODE (arg) == VECTOR_CST
8937 && VECTOR_CST_NELTS (arg).is_constant (&nunits))
8939 for (i = 0; i < nunits; ++i)
8940 elts[i] = VECTOR_CST_ELT (arg, i);
8942 else if (TREE_CODE (arg) == CONSTRUCTOR)
8944 constructor_elt *elt;
8946 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
8947 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
8948 return false;
8949 else
8950 elts[i] = elt->value;
8952 else
8953 return false;
8954 for (; i < nelts; i++)
8955 elts[i]
8956 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
8957 return true;
8960 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
8961 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
8962 NULL_TREE otherwise. */
8964 static tree
8965 fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
8967 unsigned int i;
8968 unsigned HOST_WIDE_INT nelts;
8969 bool need_ctor = false;
8971 if (!sel.length ().is_constant (&nelts))
8972 return NULL_TREE;
8973 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), nelts)
8974 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)), nelts)
8975 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)), nelts));
8976 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
8977 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
8978 return NULL_TREE;
8980 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
8981 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
8982 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
8983 return NULL_TREE;
8985 tree_vector_builder out_elts (type, nelts, 1);
8986 for (i = 0; i < nelts; i++)
8988 HOST_WIDE_INT index;
8989 if (!sel[i].is_constant (&index))
8990 return NULL_TREE;
8991 if (!CONSTANT_CLASS_P (in_elts[index]))
8992 need_ctor = true;
8993 out_elts.quick_push (unshare_expr (in_elts[index]));
8996 if (need_ctor)
8998 vec<constructor_elt, va_gc> *v;
8999 vec_alloc (v, nelts);
9000 for (i = 0; i < nelts; i++)
9001 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
9002 return build_constructor (type, v);
9004 else
9005 return out_elts.build ();
9008 /* Try to fold a pointer difference of type TYPE two address expressions of
9009 array references AREF0 and AREF1 using location LOC. Return a
9010 simplified expression for the difference or NULL_TREE. */
9012 static tree
9013 fold_addr_of_array_ref_difference (location_t loc, tree type,
9014 tree aref0, tree aref1,
9015 bool use_pointer_diff)
9017 tree base0 = TREE_OPERAND (aref0, 0);
9018 tree base1 = TREE_OPERAND (aref1, 0);
9019 tree base_offset = build_int_cst (type, 0);
9021 /* If the bases are array references as well, recurse. If the bases
9022 are pointer indirections compute the difference of the pointers.
9023 If the bases are equal, we are set. */
9024 if ((TREE_CODE (base0) == ARRAY_REF
9025 && TREE_CODE (base1) == ARRAY_REF
9026 && (base_offset
9027 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
9028 use_pointer_diff)))
9029 || (INDIRECT_REF_P (base0)
9030 && INDIRECT_REF_P (base1)
9031 && (base_offset
9032 = use_pointer_diff
9033 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
9034 TREE_OPERAND (base0, 0),
9035 TREE_OPERAND (base1, 0))
9036 : fold_binary_loc (loc, MINUS_EXPR, type,
9037 fold_convert (type,
9038 TREE_OPERAND (base0, 0)),
9039 fold_convert (type,
9040 TREE_OPERAND (base1, 0)))))
9041 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
9043 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9044 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9045 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9046 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
9047 return fold_build2_loc (loc, PLUS_EXPR, type,
9048 base_offset,
9049 fold_build2_loc (loc, MULT_EXPR, type,
9050 diff, esz));
9052 return NULL_TREE;
9055 /* If the real or vector real constant CST of type TYPE has an exact
9056 inverse, return it, else return NULL. */
9058 tree
9059 exact_inverse (tree type, tree cst)
9061 REAL_VALUE_TYPE r;
9062 tree unit_type;
9063 machine_mode mode;
9065 switch (TREE_CODE (cst))
9067 case REAL_CST:
9068 r = TREE_REAL_CST (cst);
9070 if (exact_real_inverse (TYPE_MODE (type), &r))
9071 return build_real (type, r);
9073 return NULL_TREE;
9075 case VECTOR_CST:
9077 unit_type = TREE_TYPE (type);
9078 mode = TYPE_MODE (unit_type);
9080 tree_vector_builder elts;
9081 if (!elts.new_unary_operation (type, cst, false))
9082 return NULL_TREE;
9083 unsigned int count = elts.encoded_nelts ();
9084 for (unsigned int i = 0; i < count; ++i)
9086 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9087 if (!exact_real_inverse (mode, &r))
9088 return NULL_TREE;
9089 elts.quick_push (build_real (unit_type, r));
9092 return elts.build ();
9095 default:
9096 return NULL_TREE;
9100 /* Mask out the tz least significant bits of X of type TYPE where
9101 tz is the number of trailing zeroes in Y. */
9102 static wide_int
9103 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9105 int tz = wi::ctz (y);
9106 if (tz > 0)
9107 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9108 return x;
9111 /* Return true when T is an address and is known to be nonzero.
9112 For floating point we further ensure that T is not denormal.
9113 Similar logic is present in nonzero_address in rtlanal.h.
9115 If the return value is based on the assumption that signed overflow
9116 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9117 change *STRICT_OVERFLOW_P. */
9119 static bool
9120 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9122 tree type = TREE_TYPE (t);
9123 enum tree_code code;
9125 /* Doing something useful for floating point would need more work. */
9126 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9127 return false;
9129 code = TREE_CODE (t);
9130 switch (TREE_CODE_CLASS (code))
9132 case tcc_unary:
9133 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9134 strict_overflow_p);
9135 case tcc_binary:
9136 case tcc_comparison:
9137 return tree_binary_nonzero_warnv_p (code, type,
9138 TREE_OPERAND (t, 0),
9139 TREE_OPERAND (t, 1),
9140 strict_overflow_p);
9141 case tcc_constant:
9142 case tcc_declaration:
9143 case tcc_reference:
9144 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9146 default:
9147 break;
9150 switch (code)
9152 case TRUTH_NOT_EXPR:
9153 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9154 strict_overflow_p);
9156 case TRUTH_AND_EXPR:
9157 case TRUTH_OR_EXPR:
9158 case TRUTH_XOR_EXPR:
9159 return tree_binary_nonzero_warnv_p (code, type,
9160 TREE_OPERAND (t, 0),
9161 TREE_OPERAND (t, 1),
9162 strict_overflow_p);
9164 case COND_EXPR:
9165 case CONSTRUCTOR:
9166 case OBJ_TYPE_REF:
9167 case ASSERT_EXPR:
9168 case ADDR_EXPR:
9169 case WITH_SIZE_EXPR:
9170 case SSA_NAME:
9171 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9173 case COMPOUND_EXPR:
9174 case MODIFY_EXPR:
9175 case BIND_EXPR:
9176 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9177 strict_overflow_p);
9179 case SAVE_EXPR:
9180 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9181 strict_overflow_p);
9183 case CALL_EXPR:
9185 tree fndecl = get_callee_fndecl (t);
9186 if (!fndecl) return false;
9187 if (flag_delete_null_pointer_checks && !flag_check_new
9188 && DECL_IS_OPERATOR_NEW (fndecl)
9189 && !TREE_NOTHROW (fndecl))
9190 return true;
9191 if (flag_delete_null_pointer_checks
9192 && lookup_attribute ("returns_nonnull",
9193 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9194 return true;
9195 return alloca_call_p (t);
9198 default:
9199 break;
9201 return false;
9204 /* Return true when T is an address and is known to be nonzero.
9205 Handle warnings about undefined signed overflow. */
9207 bool
9208 tree_expr_nonzero_p (tree t)
9210 bool ret, strict_overflow_p;
9212 strict_overflow_p = false;
9213 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9214 if (strict_overflow_p)
9215 fold_overflow_warning (("assuming signed overflow does not occur when "
9216 "determining that expression is always "
9217 "non-zero"),
9218 WARN_STRICT_OVERFLOW_MISC);
9219 return ret;
9222 /* Return true if T is known not to be equal to an integer W. */
9224 bool
9225 expr_not_equal_to (tree t, const wide_int &w)
9227 wide_int min, max, nz;
9228 value_range_type rtype;
9229 switch (TREE_CODE (t))
9231 case INTEGER_CST:
9232 return wi::to_wide (t) != w;
9234 case SSA_NAME:
9235 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9236 return false;
9237 rtype = get_range_info (t, &min, &max);
9238 if (rtype == VR_RANGE)
9240 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9241 return true;
9242 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9243 return true;
9245 else if (rtype == VR_ANTI_RANGE
9246 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9247 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9248 return true;
9249 /* If T has some known zero bits and W has any of those bits set,
9250 then T is known not to be equal to W. */
9251 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9252 TYPE_PRECISION (TREE_TYPE (t))), 0))
9253 return true;
9254 return false;
9256 default:
9257 return false;
9261 /* Fold a binary expression of code CODE and type TYPE with operands
9262 OP0 and OP1. LOC is the location of the resulting expression.
9263 Return the folded expression if folding is successful. Otherwise,
9264 return NULL_TREE. */
9266 tree
9267 fold_binary_loc (location_t loc, enum tree_code code, tree type,
9268 tree op0, tree op1)
9270 enum tree_code_class kind = TREE_CODE_CLASS (code);
9271 tree arg0, arg1, tem;
9272 tree t1 = NULL_TREE;
9273 bool strict_overflow_p;
9274 unsigned int prec;
9276 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9277 && TREE_CODE_LENGTH (code) == 2
9278 && op0 != NULL_TREE
9279 && op1 != NULL_TREE);
9281 arg0 = op0;
9282 arg1 = op1;
9284 /* Strip any conversions that don't change the mode. This is
9285 safe for every expression, except for a comparison expression
9286 because its signedness is derived from its operands. So, in
9287 the latter case, only strip conversions that don't change the
9288 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9289 preserved.
9291 Note that this is done as an internal manipulation within the
9292 constant folder, in order to find the simplest representation
9293 of the arguments so that their form can be studied. In any
9294 cases, the appropriate type conversions should be put back in
9295 the tree that will get out of the constant folder. */
9297 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9299 STRIP_SIGN_NOPS (arg0);
9300 STRIP_SIGN_NOPS (arg1);
9302 else
9304 STRIP_NOPS (arg0);
9305 STRIP_NOPS (arg1);
9308 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9309 constant but we can't do arithmetic on them. */
9310 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9312 tem = const_binop (code, type, arg0, arg1);
9313 if (tem != NULL_TREE)
9315 if (TREE_TYPE (tem) != type)
9316 tem = fold_convert_loc (loc, type, tem);
9317 return tem;
9321 /* If this is a commutative operation, and ARG0 is a constant, move it
9322 to ARG1 to reduce the number of tests below. */
9323 if (commutative_tree_code (code)
9324 && tree_swap_operands_p (arg0, arg1))
9325 return fold_build2_loc (loc, code, type, op1, op0);
9327 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9328 to ARG1 to reduce the number of tests below. */
9329 if (kind == tcc_comparison
9330 && tree_swap_operands_p (arg0, arg1))
9331 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9333 tem = generic_simplify (loc, code, type, op0, op1);
9334 if (tem)
9335 return tem;
9337 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9339 First check for cases where an arithmetic operation is applied to a
9340 compound, conditional, or comparison operation. Push the arithmetic
9341 operation inside the compound or conditional to see if any folding
9342 can then be done. Convert comparison to conditional for this purpose.
9343 The also optimizes non-constant cases that used to be done in
9344 expand_expr.
9346 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9347 one of the operands is a comparison and the other is a comparison, a
9348 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9349 code below would make the expression more complex. Change it to a
9350 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9351 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9353 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9354 || code == EQ_EXPR || code == NE_EXPR)
9355 && !VECTOR_TYPE_P (TREE_TYPE (arg0))
9356 && ((truth_value_p (TREE_CODE (arg0))
9357 && (truth_value_p (TREE_CODE (arg1))
9358 || (TREE_CODE (arg1) == BIT_AND_EXPR
9359 && integer_onep (TREE_OPERAND (arg1, 1)))))
9360 || (truth_value_p (TREE_CODE (arg1))
9361 && (truth_value_p (TREE_CODE (arg0))
9362 || (TREE_CODE (arg0) == BIT_AND_EXPR
9363 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9365 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9366 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9367 : TRUTH_XOR_EXPR,
9368 boolean_type_node,
9369 fold_convert_loc (loc, boolean_type_node, arg0),
9370 fold_convert_loc (loc, boolean_type_node, arg1));
9372 if (code == EQ_EXPR)
9373 tem = invert_truthvalue_loc (loc, tem);
9375 return fold_convert_loc (loc, type, tem);
9378 if (TREE_CODE_CLASS (code) == tcc_binary
9379 || TREE_CODE_CLASS (code) == tcc_comparison)
9381 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9383 tem = fold_build2_loc (loc, code, type,
9384 fold_convert_loc (loc, TREE_TYPE (op0),
9385 TREE_OPERAND (arg0, 1)), op1);
9386 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9387 tem);
9389 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9391 tem = fold_build2_loc (loc, code, type, op0,
9392 fold_convert_loc (loc, TREE_TYPE (op1),
9393 TREE_OPERAND (arg1, 1)));
9394 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9395 tem);
9398 if (TREE_CODE (arg0) == COND_EXPR
9399 || TREE_CODE (arg0) == VEC_COND_EXPR
9400 || COMPARISON_CLASS_P (arg0))
9402 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9403 arg0, arg1,
9404 /*cond_first_p=*/1);
9405 if (tem != NULL_TREE)
9406 return tem;
9409 if (TREE_CODE (arg1) == COND_EXPR
9410 || TREE_CODE (arg1) == VEC_COND_EXPR
9411 || COMPARISON_CLASS_P (arg1))
9413 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9414 arg1, arg0,
9415 /*cond_first_p=*/0);
9416 if (tem != NULL_TREE)
9417 return tem;
9421 switch (code)
9423 case MEM_REF:
9424 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9425 if (TREE_CODE (arg0) == ADDR_EXPR
9426 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9428 tree iref = TREE_OPERAND (arg0, 0);
9429 return fold_build2 (MEM_REF, type,
9430 TREE_OPERAND (iref, 0),
9431 int_const_binop (PLUS_EXPR, arg1,
9432 TREE_OPERAND (iref, 1)));
9435 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9436 if (TREE_CODE (arg0) == ADDR_EXPR
9437 && handled_component_p (TREE_OPERAND (arg0, 0)))
9439 tree base;
9440 poly_int64 coffset;
9441 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9442 &coffset);
9443 if (!base)
9444 return NULL_TREE;
9445 return fold_build2 (MEM_REF, type,
9446 build_fold_addr_expr (base),
9447 int_const_binop (PLUS_EXPR, arg1,
9448 size_int (coffset)));
9451 return NULL_TREE;
9453 case POINTER_PLUS_EXPR:
9454 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9455 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9456 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9457 return fold_convert_loc (loc, type,
9458 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9459 fold_convert_loc (loc, sizetype,
9460 arg1),
9461 fold_convert_loc (loc, sizetype,
9462 arg0)));
9464 return NULL_TREE;
9466 case PLUS_EXPR:
9467 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9469 /* X + (X / CST) * -CST is X % CST. */
9470 if (TREE_CODE (arg1) == MULT_EXPR
9471 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9472 && operand_equal_p (arg0,
9473 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9475 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9476 tree cst1 = TREE_OPERAND (arg1, 1);
9477 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9478 cst1, cst0);
9479 if (sum && integer_zerop (sum))
9480 return fold_convert_loc (loc, type,
9481 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9482 TREE_TYPE (arg0), arg0,
9483 cst0));
9487 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9488 one. Make sure the type is not saturating and has the signedness of
9489 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9490 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9491 if ((TREE_CODE (arg0) == MULT_EXPR
9492 || TREE_CODE (arg1) == MULT_EXPR)
9493 && !TYPE_SATURATING (type)
9494 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9495 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9496 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9498 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9499 if (tem)
9500 return tem;
9503 if (! FLOAT_TYPE_P (type))
9505 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9506 (plus (plus (mult) (mult)) (foo)) so that we can
9507 take advantage of the factoring cases below. */
9508 if (ANY_INTEGRAL_TYPE_P (type)
9509 && TYPE_OVERFLOW_WRAPS (type)
9510 && (((TREE_CODE (arg0) == PLUS_EXPR
9511 || TREE_CODE (arg0) == MINUS_EXPR)
9512 && TREE_CODE (arg1) == MULT_EXPR)
9513 || ((TREE_CODE (arg1) == PLUS_EXPR
9514 || TREE_CODE (arg1) == MINUS_EXPR)
9515 && TREE_CODE (arg0) == MULT_EXPR)))
9517 tree parg0, parg1, parg, marg;
9518 enum tree_code pcode;
9520 if (TREE_CODE (arg1) == MULT_EXPR)
9521 parg = arg0, marg = arg1;
9522 else
9523 parg = arg1, marg = arg0;
9524 pcode = TREE_CODE (parg);
9525 parg0 = TREE_OPERAND (parg, 0);
9526 parg1 = TREE_OPERAND (parg, 1);
9527 STRIP_NOPS (parg0);
9528 STRIP_NOPS (parg1);
9530 if (TREE_CODE (parg0) == MULT_EXPR
9531 && TREE_CODE (parg1) != MULT_EXPR)
9532 return fold_build2_loc (loc, pcode, type,
9533 fold_build2_loc (loc, PLUS_EXPR, type,
9534 fold_convert_loc (loc, type,
9535 parg0),
9536 fold_convert_loc (loc, type,
9537 marg)),
9538 fold_convert_loc (loc, type, parg1));
9539 if (TREE_CODE (parg0) != MULT_EXPR
9540 && TREE_CODE (parg1) == MULT_EXPR)
9541 return
9542 fold_build2_loc (loc, PLUS_EXPR, type,
9543 fold_convert_loc (loc, type, parg0),
9544 fold_build2_loc (loc, pcode, type,
9545 fold_convert_loc (loc, type, marg),
9546 fold_convert_loc (loc, type,
9547 parg1)));
9550 else
9552 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9553 to __complex__ ( x, y ). This is not the same for SNaNs or
9554 if signed zeros are involved. */
9555 if (!HONOR_SNANS (element_mode (arg0))
9556 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9557 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9559 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9560 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9561 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9562 bool arg0rz = false, arg0iz = false;
9563 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9564 || (arg0i && (arg0iz = real_zerop (arg0i))))
9566 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9567 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9568 if (arg0rz && arg1i && real_zerop (arg1i))
9570 tree rp = arg1r ? arg1r
9571 : build1 (REALPART_EXPR, rtype, arg1);
9572 tree ip = arg0i ? arg0i
9573 : build1 (IMAGPART_EXPR, rtype, arg0);
9574 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9576 else if (arg0iz && arg1r && real_zerop (arg1r))
9578 tree rp = arg0r ? arg0r
9579 : build1 (REALPART_EXPR, rtype, arg0);
9580 tree ip = arg1i ? arg1i
9581 : build1 (IMAGPART_EXPR, rtype, arg1);
9582 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9587 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9588 We associate floats only if the user has specified
9589 -fassociative-math. */
9590 if (flag_associative_math
9591 && TREE_CODE (arg1) == PLUS_EXPR
9592 && TREE_CODE (arg0) != MULT_EXPR)
9594 tree tree10 = TREE_OPERAND (arg1, 0);
9595 tree tree11 = TREE_OPERAND (arg1, 1);
9596 if (TREE_CODE (tree11) == MULT_EXPR
9597 && TREE_CODE (tree10) == MULT_EXPR)
9599 tree tree0;
9600 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9601 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9604 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9605 We associate floats only if the user has specified
9606 -fassociative-math. */
9607 if (flag_associative_math
9608 && TREE_CODE (arg0) == PLUS_EXPR
9609 && TREE_CODE (arg1) != MULT_EXPR)
9611 tree tree00 = TREE_OPERAND (arg0, 0);
9612 tree tree01 = TREE_OPERAND (arg0, 1);
9613 if (TREE_CODE (tree01) == MULT_EXPR
9614 && TREE_CODE (tree00) == MULT_EXPR)
9616 tree tree0;
9617 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9618 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9623 bit_rotate:
9624 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9625 is a rotate of A by C1 bits. */
9626 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9627 is a rotate of A by B bits.
9628 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
9629 though in this case CODE must be | and not + or ^, otherwise
9630 it doesn't return A when B is 0. */
9632 enum tree_code code0, code1;
9633 tree rtype;
9634 code0 = TREE_CODE (arg0);
9635 code1 = TREE_CODE (arg1);
9636 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9637 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9638 && operand_equal_p (TREE_OPERAND (arg0, 0),
9639 TREE_OPERAND (arg1, 0), 0)
9640 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9641 TYPE_UNSIGNED (rtype))
9642 /* Only create rotates in complete modes. Other cases are not
9643 expanded properly. */
9644 && (element_precision (rtype)
9645 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9647 tree tree01, tree11;
9648 tree orig_tree01, orig_tree11;
9649 enum tree_code code01, code11;
9651 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
9652 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
9653 STRIP_NOPS (tree01);
9654 STRIP_NOPS (tree11);
9655 code01 = TREE_CODE (tree01);
9656 code11 = TREE_CODE (tree11);
9657 if (code11 != MINUS_EXPR
9658 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
9660 std::swap (code0, code1);
9661 std::swap (code01, code11);
9662 std::swap (tree01, tree11);
9663 std::swap (orig_tree01, orig_tree11);
9665 if (code01 == INTEGER_CST
9666 && code11 == INTEGER_CST
9667 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9668 == element_precision (rtype)))
9670 tem = build2_loc (loc, LROTATE_EXPR,
9671 rtype, TREE_OPERAND (arg0, 0),
9672 code0 == LSHIFT_EXPR
9673 ? orig_tree01 : orig_tree11);
9674 return fold_convert_loc (loc, type, tem);
9676 else if (code11 == MINUS_EXPR)
9678 tree tree110, tree111;
9679 tree110 = TREE_OPERAND (tree11, 0);
9680 tree111 = TREE_OPERAND (tree11, 1);
9681 STRIP_NOPS (tree110);
9682 STRIP_NOPS (tree111);
9683 if (TREE_CODE (tree110) == INTEGER_CST
9684 && compare_tree_int (tree110,
9685 element_precision (rtype)) == 0
9686 && operand_equal_p (tree01, tree111, 0))
9688 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9689 ? LROTATE_EXPR : RROTATE_EXPR),
9690 rtype, TREE_OPERAND (arg0, 0),
9691 orig_tree01);
9692 return fold_convert_loc (loc, type, tem);
9695 else if (code == BIT_IOR_EXPR
9696 && code11 == BIT_AND_EXPR
9697 && pow2p_hwi (element_precision (rtype)))
9699 tree tree110, tree111;
9700 tree110 = TREE_OPERAND (tree11, 0);
9701 tree111 = TREE_OPERAND (tree11, 1);
9702 STRIP_NOPS (tree110);
9703 STRIP_NOPS (tree111);
9704 if (TREE_CODE (tree110) == NEGATE_EXPR
9705 && TREE_CODE (tree111) == INTEGER_CST
9706 && compare_tree_int (tree111,
9707 element_precision (rtype) - 1) == 0
9708 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
9710 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9711 ? LROTATE_EXPR : RROTATE_EXPR),
9712 rtype, TREE_OPERAND (arg0, 0),
9713 orig_tree01);
9714 return fold_convert_loc (loc, type, tem);
9720 associate:
9721 /* In most languages, can't associate operations on floats through
9722 parentheses. Rather than remember where the parentheses were, we
9723 don't associate floats at all, unless the user has specified
9724 -fassociative-math.
9725 And, we need to make sure type is not saturating. */
9727 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9728 && !TYPE_SATURATING (type))
9730 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9731 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9732 tree atype = type;
9733 bool ok = true;
9735 /* Split both trees into variables, constants, and literals. Then
9736 associate each group together, the constants with literals,
9737 then the result with variables. This increases the chances of
9738 literals being recombined later and of generating relocatable
9739 expressions for the sum of a constant and literal. */
9740 var0 = split_tree (arg0, type, code,
9741 &minus_var0, &con0, &minus_con0,
9742 &lit0, &minus_lit0, 0);
9743 var1 = split_tree (arg1, type, code,
9744 &minus_var1, &con1, &minus_con1,
9745 &lit1, &minus_lit1, code == MINUS_EXPR);
9747 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9748 if (code == MINUS_EXPR)
9749 code = PLUS_EXPR;
9751 /* With undefined overflow prefer doing association in a type
9752 which wraps on overflow, if that is one of the operand types. */
9753 if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
9754 && !TYPE_OVERFLOW_WRAPS (type))
9756 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9757 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9758 atype = TREE_TYPE (arg0);
9759 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9760 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9761 atype = TREE_TYPE (arg1);
9762 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9765 /* With undefined overflow we can only associate constants with one
9766 variable, and constants whose association doesn't overflow. */
9767 if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
9768 && !TYPE_OVERFLOW_WRAPS (atype))
9770 if ((var0 && var1) || (minus_var0 && minus_var1))
9772 /* ??? If split_tree would handle NEGATE_EXPR we could
9773 simply reject these cases and the allowed cases would
9774 be the var0/minus_var1 ones. */
9775 tree tmp0 = var0 ? var0 : minus_var0;
9776 tree tmp1 = var1 ? var1 : minus_var1;
9777 bool one_neg = false;
9779 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9781 tmp0 = TREE_OPERAND (tmp0, 0);
9782 one_neg = !one_neg;
9784 if (CONVERT_EXPR_P (tmp0)
9785 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9786 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9787 <= TYPE_PRECISION (atype)))
9788 tmp0 = TREE_OPERAND (tmp0, 0);
9789 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9791 tmp1 = TREE_OPERAND (tmp1, 0);
9792 one_neg = !one_neg;
9794 if (CONVERT_EXPR_P (tmp1)
9795 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9796 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9797 <= TYPE_PRECISION (atype)))
9798 tmp1 = TREE_OPERAND (tmp1, 0);
9799 /* The only case we can still associate with two variables
9800 is if they cancel out. */
9801 if (!one_neg
9802 || !operand_equal_p (tmp0, tmp1, 0))
9803 ok = false;
9805 else if ((var0 && minus_var1
9806 && ! operand_equal_p (var0, minus_var1, 0))
9807 || (minus_var0 && var1
9808 && ! operand_equal_p (minus_var0, var1, 0)))
9809 ok = false;
9812 /* Only do something if we found more than two objects. Otherwise,
9813 nothing has changed and we risk infinite recursion. */
9814 if (ok
9815 && ((var0 != 0) + (var1 != 0)
9816 + (minus_var0 != 0) + (minus_var1 != 0)
9817 + (con0 != 0) + (con1 != 0)
9818 + (minus_con0 != 0) + (minus_con1 != 0)
9819 + (lit0 != 0) + (lit1 != 0)
9820 + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
9822 var0 = associate_trees (loc, var0, var1, code, atype);
9823 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9824 code, atype);
9825 con0 = associate_trees (loc, con0, con1, code, atype);
9826 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9827 code, atype);
9828 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9829 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9830 code, atype);
9832 if (minus_var0 && var0)
9834 var0 = associate_trees (loc, var0, minus_var0,
9835 MINUS_EXPR, atype);
9836 minus_var0 = 0;
9838 if (minus_con0 && con0)
9840 con0 = associate_trees (loc, con0, minus_con0,
9841 MINUS_EXPR, atype);
9842 minus_con0 = 0;
9845 /* Preserve the MINUS_EXPR if the negative part of the literal is
9846 greater than the positive part. Otherwise, the multiplicative
9847 folding code (i.e extract_muldiv) may be fooled in case
9848 unsigned constants are subtracted, like in the following
9849 example: ((X*2 + 4) - 8U)/2. */
9850 if (minus_lit0 && lit0)
9852 if (TREE_CODE (lit0) == INTEGER_CST
9853 && TREE_CODE (minus_lit0) == INTEGER_CST
9854 && tree_int_cst_lt (lit0, minus_lit0)
9855 /* But avoid ending up with only negated parts. */
9856 && (var0 || con0))
9858 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9859 MINUS_EXPR, atype);
9860 lit0 = 0;
9862 else
9864 lit0 = associate_trees (loc, lit0, minus_lit0,
9865 MINUS_EXPR, atype);
9866 minus_lit0 = 0;
9870 /* Don't introduce overflows through reassociation. */
9871 if ((lit0 && TREE_OVERFLOW_P (lit0))
9872 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9873 return NULL_TREE;
9875 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9876 con0 = associate_trees (loc, con0, lit0, code, atype);
9877 lit0 = 0;
9878 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9879 code, atype);
9880 minus_lit0 = 0;
9882 /* Eliminate minus_con0. */
9883 if (minus_con0)
9885 if (con0)
9886 con0 = associate_trees (loc, con0, minus_con0,
9887 MINUS_EXPR, atype);
9888 else if (var0)
9889 var0 = associate_trees (loc, var0, minus_con0,
9890 MINUS_EXPR, atype);
9891 else
9892 gcc_unreachable ();
9893 minus_con0 = 0;
9896 /* Eliminate minus_var0. */
9897 if (minus_var0)
9899 if (con0)
9900 con0 = associate_trees (loc, con0, minus_var0,
9901 MINUS_EXPR, atype);
9902 else
9903 gcc_unreachable ();
9904 minus_var0 = 0;
9907 return
9908 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9909 code, atype));
9913 return NULL_TREE;
9915 case POINTER_DIFF_EXPR:
9916 case MINUS_EXPR:
9917 /* Fold &a[i] - &a[j] to i-j. */
9918 if (TREE_CODE (arg0) == ADDR_EXPR
9919 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9920 && TREE_CODE (arg1) == ADDR_EXPR
9921 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9923 tree tem = fold_addr_of_array_ref_difference (loc, type,
9924 TREE_OPERAND (arg0, 0),
9925 TREE_OPERAND (arg1, 0),
9926 code
9927 == POINTER_DIFF_EXPR);
9928 if (tem)
9929 return tem;
9932 /* Further transformations are not for pointers. */
9933 if (code == POINTER_DIFF_EXPR)
9934 return NULL_TREE;
9936 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9937 if (TREE_CODE (arg0) == NEGATE_EXPR
9938 && negate_expr_p (op1)
9939 /* If arg0 is e.g. unsigned int and type is int, then this could
9940 introduce UB, because if A is INT_MIN at runtime, the original
9941 expression can be well defined while the latter is not.
9942 See PR83269. */
9943 && !(ANY_INTEGRAL_TYPE_P (type)
9944 && TYPE_OVERFLOW_UNDEFINED (type)
9945 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9946 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
9947 return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
9948 fold_convert_loc (loc, type,
9949 TREE_OPERAND (arg0, 0)));
9951 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
9952 __complex__ ( x, -y ). This is not the same for SNaNs or if
9953 signed zeros are involved. */
9954 if (!HONOR_SNANS (element_mode (arg0))
9955 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9956 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9958 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9959 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9960 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9961 bool arg0rz = false, arg0iz = false;
9962 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9963 || (arg0i && (arg0iz = real_zerop (arg0i))))
9965 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9966 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9967 if (arg0rz && arg1i && real_zerop (arg1i))
9969 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9970 arg1r ? arg1r
9971 : build1 (REALPART_EXPR, rtype, arg1));
9972 tree ip = arg0i ? arg0i
9973 : build1 (IMAGPART_EXPR, rtype, arg0);
9974 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9976 else if (arg0iz && arg1r && real_zerop (arg1r))
9978 tree rp = arg0r ? arg0r
9979 : build1 (REALPART_EXPR, rtype, arg0);
9980 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
9981 arg1i ? arg1i
9982 : build1 (IMAGPART_EXPR, rtype, arg1));
9983 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9988 /* A - B -> A + (-B) if B is easily negatable. */
9989 if (negate_expr_p (op1)
9990 && ! TYPE_OVERFLOW_SANITIZED (type)
9991 && ((FLOAT_TYPE_P (type)
9992 /* Avoid this transformation if B is a positive REAL_CST. */
9993 && (TREE_CODE (op1) != REAL_CST
9994 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
9995 || INTEGRAL_TYPE_P (type)))
9996 return fold_build2_loc (loc, PLUS_EXPR, type,
9997 fold_convert_loc (loc, type, arg0),
9998 negate_expr (op1));
10000 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
10001 one. Make sure the type is not saturating and has the signedness of
10002 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10003 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10004 if ((TREE_CODE (arg0) == MULT_EXPR
10005 || TREE_CODE (arg1) == MULT_EXPR)
10006 && !TYPE_SATURATING (type)
10007 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10008 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10009 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10011 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10012 if (tem)
10013 return tem;
10016 goto associate;
10018 case MULT_EXPR:
10019 if (! FLOAT_TYPE_P (type))
10021 /* Transform x * -C into -x * C if x is easily negatable. */
10022 if (TREE_CODE (op1) == INTEGER_CST
10023 && tree_int_cst_sgn (op1) == -1
10024 && negate_expr_p (op0)
10025 && negate_expr_p (op1)
10026 && (tem = negate_expr (op1)) != op1
10027 && ! TREE_OVERFLOW (tem))
10028 return fold_build2_loc (loc, MULT_EXPR, type,
10029 fold_convert_loc (loc, type,
10030 negate_expr (op0)), tem);
10032 strict_overflow_p = false;
10033 if (TREE_CODE (arg1) == INTEGER_CST
10034 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10035 &strict_overflow_p)) != 0)
10037 if (strict_overflow_p)
10038 fold_overflow_warning (("assuming signed overflow does not "
10039 "occur when simplifying "
10040 "multiplication"),
10041 WARN_STRICT_OVERFLOW_MISC);
10042 return fold_convert_loc (loc, type, tem);
10045 /* Optimize z * conj(z) for integer complex numbers. */
10046 if (TREE_CODE (arg0) == CONJ_EXPR
10047 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10048 return fold_mult_zconjz (loc, type, arg1);
10049 if (TREE_CODE (arg1) == CONJ_EXPR
10050 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10051 return fold_mult_zconjz (loc, type, arg0);
10053 else
10055 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10056 This is not the same for NaNs or if signed zeros are
10057 involved. */
10058 if (!HONOR_NANS (arg0)
10059 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10060 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10061 && TREE_CODE (arg1) == COMPLEX_CST
10062 && real_zerop (TREE_REALPART (arg1)))
10064 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10065 if (real_onep (TREE_IMAGPART (arg1)))
10066 return
10067 fold_build2_loc (loc, COMPLEX_EXPR, type,
10068 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10069 rtype, arg0)),
10070 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10071 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10072 return
10073 fold_build2_loc (loc, COMPLEX_EXPR, type,
10074 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10075 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10076 rtype, arg0)));
10079 /* Optimize z * conj(z) for floating point complex numbers.
10080 Guarded by flag_unsafe_math_optimizations as non-finite
10081 imaginary components don't produce scalar results. */
10082 if (flag_unsafe_math_optimizations
10083 && TREE_CODE (arg0) == CONJ_EXPR
10084 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10085 return fold_mult_zconjz (loc, type, arg1);
10086 if (flag_unsafe_math_optimizations
10087 && TREE_CODE (arg1) == CONJ_EXPR
10088 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10089 return fold_mult_zconjz (loc, type, arg0);
10091 goto associate;
10093 case BIT_IOR_EXPR:
10094 /* Canonicalize (X & C1) | C2. */
10095 if (TREE_CODE (arg0) == BIT_AND_EXPR
10096 && TREE_CODE (arg1) == INTEGER_CST
10097 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10099 int width = TYPE_PRECISION (type), w;
10100 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
10101 wide_int c2 = wi::to_wide (arg1);
10103 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10104 if ((c1 & c2) == c1)
10105 return omit_one_operand_loc (loc, type, arg1,
10106 TREE_OPERAND (arg0, 0));
10108 wide_int msk = wi::mask (width, false,
10109 TYPE_PRECISION (TREE_TYPE (arg1)));
10111 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10112 if (wi::bit_and_not (msk, c1 | c2) == 0)
10114 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10115 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10118 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10119 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10120 mode which allows further optimizations. */
10121 c1 &= msk;
10122 c2 &= msk;
10123 wide_int c3 = wi::bit_and_not (c1, c2);
10124 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10126 wide_int mask = wi::mask (w, false,
10127 TYPE_PRECISION (type));
10128 if (((c1 | c2) & mask) == mask
10129 && wi::bit_and_not (c1, mask) == 0)
10131 c3 = mask;
10132 break;
10136 if (c3 != c1)
10138 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10139 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
10140 wide_int_to_tree (type, c3));
10141 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10145 /* See if this can be simplified into a rotate first. If that
10146 is unsuccessful continue in the association code. */
10147 goto bit_rotate;
10149 case BIT_XOR_EXPR:
10150 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10151 if (TREE_CODE (arg0) == BIT_AND_EXPR
10152 && INTEGRAL_TYPE_P (type)
10153 && integer_onep (TREE_OPERAND (arg0, 1))
10154 && integer_onep (arg1))
10155 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10156 build_zero_cst (TREE_TYPE (arg0)));
10158 /* See if this can be simplified into a rotate first. If that
10159 is unsuccessful continue in the association code. */
10160 goto bit_rotate;
10162 case BIT_AND_EXPR:
10163 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10164 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10165 && INTEGRAL_TYPE_P (type)
10166 && integer_onep (TREE_OPERAND (arg0, 1))
10167 && integer_onep (arg1))
10169 tree tem2;
10170 tem = TREE_OPERAND (arg0, 0);
10171 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10172 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10173 tem, tem2);
10174 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10175 build_zero_cst (TREE_TYPE (tem)));
10177 /* Fold ~X & 1 as (X & 1) == 0. */
10178 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10179 && INTEGRAL_TYPE_P (type)
10180 && integer_onep (arg1))
10182 tree tem2;
10183 tem = TREE_OPERAND (arg0, 0);
10184 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10185 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10186 tem, tem2);
10187 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10188 build_zero_cst (TREE_TYPE (tem)));
10190 /* Fold !X & 1 as X == 0. */
10191 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10192 && integer_onep (arg1))
10194 tem = TREE_OPERAND (arg0, 0);
10195 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10196 build_zero_cst (TREE_TYPE (tem)));
10199 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10200 multiple of 1 << CST. */
10201 if (TREE_CODE (arg1) == INTEGER_CST)
10203 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10204 wide_int ncst1 = -cst1;
10205 if ((cst1 & ncst1) == ncst1
10206 && multiple_of_p (type, arg0,
10207 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10208 return fold_convert_loc (loc, type, arg0);
10211 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10212 bits from CST2. */
10213 if (TREE_CODE (arg1) == INTEGER_CST
10214 && TREE_CODE (arg0) == MULT_EXPR
10215 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10217 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
10218 wide_int masked
10219 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
10221 if (masked == 0)
10222 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10223 arg0, arg1);
10224 else if (masked != warg1)
10226 /* Avoid the transform if arg1 is a mask of some
10227 mode which allows further optimizations. */
10228 int pop = wi::popcount (warg1);
10229 if (!(pop >= BITS_PER_UNIT
10230 && pow2p_hwi (pop)
10231 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10232 return fold_build2_loc (loc, code, type, op0,
10233 wide_int_to_tree (type, masked));
10237 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
10238 ((A & N) + B) & M -> (A + B) & M
10239 Similarly if (N & M) == 0,
10240 ((A | N) + B) & M -> (A + B) & M
10241 and for - instead of + (or unary - instead of +)
10242 and/or ^ instead of |.
10243 If B is constant and (B & M) == 0, fold into A & M. */
10244 if (TREE_CODE (arg1) == INTEGER_CST)
10246 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10247 if ((~cst1 != 0) && (cst1 & (cst1 + 1)) == 0
10248 && INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10249 && (TREE_CODE (arg0) == PLUS_EXPR
10250 || TREE_CODE (arg0) == MINUS_EXPR
10251 || TREE_CODE (arg0) == NEGATE_EXPR)
10252 && (TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0))
10253 || TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE))
10255 tree pmop[2];
10256 int which = 0;
10257 wide_int cst0;
10259 /* Now we know that arg0 is (C + D) or (C - D) or
10260 -C and arg1 (M) is == (1LL << cst) - 1.
10261 Store C into PMOP[0] and D into PMOP[1]. */
10262 pmop[0] = TREE_OPERAND (arg0, 0);
10263 pmop[1] = NULL;
10264 if (TREE_CODE (arg0) != NEGATE_EXPR)
10266 pmop[1] = TREE_OPERAND (arg0, 1);
10267 which = 1;
10270 if ((wi::max_value (TREE_TYPE (arg0)) & cst1) != cst1)
10271 which = -1;
10273 for (; which >= 0; which--)
10274 switch (TREE_CODE (pmop[which]))
10276 case BIT_AND_EXPR:
10277 case BIT_IOR_EXPR:
10278 case BIT_XOR_EXPR:
10279 if (TREE_CODE (TREE_OPERAND (pmop[which], 1))
10280 != INTEGER_CST)
10281 break;
10282 cst0 = wi::to_wide (TREE_OPERAND (pmop[which], 1)) & cst1;
10283 if (TREE_CODE (pmop[which]) == BIT_AND_EXPR)
10285 if (cst0 != cst1)
10286 break;
10288 else if (cst0 != 0)
10289 break;
10290 /* If C or D is of the form (A & N) where
10291 (N & M) == M, or of the form (A | N) or
10292 (A ^ N) where (N & M) == 0, replace it with A. */
10293 pmop[which] = TREE_OPERAND (pmop[which], 0);
10294 break;
10295 case INTEGER_CST:
10296 /* If C or D is a N where (N & M) == 0, it can be
10297 omitted (assumed 0). */
10298 if ((TREE_CODE (arg0) == PLUS_EXPR
10299 || (TREE_CODE (arg0) == MINUS_EXPR && which == 0))
10300 && (cst1 & wi::to_wide (pmop[which])) == 0)
10301 pmop[which] = NULL;
10302 break;
10303 default:
10304 break;
10307 /* Only build anything new if we optimized one or both arguments
10308 above. */
10309 if (pmop[0] != TREE_OPERAND (arg0, 0)
10310 || (TREE_CODE (arg0) != NEGATE_EXPR
10311 && pmop[1] != TREE_OPERAND (arg0, 1)))
10313 tree utype = TREE_TYPE (arg0);
10314 if (! TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
10316 /* Perform the operations in a type that has defined
10317 overflow behavior. */
10318 utype = unsigned_type_for (TREE_TYPE (arg0));
10319 if (pmop[0] != NULL)
10320 pmop[0] = fold_convert_loc (loc, utype, pmop[0]);
10321 if (pmop[1] != NULL)
10322 pmop[1] = fold_convert_loc (loc, utype, pmop[1]);
10325 if (TREE_CODE (arg0) == NEGATE_EXPR)
10326 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[0]);
10327 else if (TREE_CODE (arg0) == PLUS_EXPR)
10329 if (pmop[0] != NULL && pmop[1] != NULL)
10330 tem = fold_build2_loc (loc, PLUS_EXPR, utype,
10331 pmop[0], pmop[1]);
10332 else if (pmop[0] != NULL)
10333 tem = pmop[0];
10334 else if (pmop[1] != NULL)
10335 tem = pmop[1];
10336 else
10337 return build_int_cst (type, 0);
10339 else if (pmop[0] == NULL)
10340 tem = fold_build1_loc (loc, NEGATE_EXPR, utype, pmop[1]);
10341 else
10342 tem = fold_build2_loc (loc, MINUS_EXPR, utype,
10343 pmop[0], pmop[1]);
10344 /* TEM is now the new binary +, - or unary - replacement. */
10345 tem = fold_build2_loc (loc, BIT_AND_EXPR, utype, tem,
10346 fold_convert_loc (loc, utype, arg1));
10347 return fold_convert_loc (loc, type, tem);
10352 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10353 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10354 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10356 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10358 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
10359 if (mask == -1)
10360 return
10361 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10364 goto associate;
10366 case RDIV_EXPR:
10367 /* Don't touch a floating-point divide by zero unless the mode
10368 of the constant can represent infinity. */
10369 if (TREE_CODE (arg1) == REAL_CST
10370 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10371 && real_zerop (arg1))
10372 return NULL_TREE;
10374 /* (-A) / (-B) -> A / B */
10375 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10376 return fold_build2_loc (loc, RDIV_EXPR, type,
10377 TREE_OPERAND (arg0, 0),
10378 negate_expr (arg1));
10379 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10380 return fold_build2_loc (loc, RDIV_EXPR, type,
10381 negate_expr (arg0),
10382 TREE_OPERAND (arg1, 0));
10383 return NULL_TREE;
10385 case TRUNC_DIV_EXPR:
10386 /* Fall through */
10388 case FLOOR_DIV_EXPR:
10389 /* Simplify A / (B << N) where A and B are positive and B is
10390 a power of 2, to A >> (N + log2(B)). */
10391 strict_overflow_p = false;
10392 if (TREE_CODE (arg1) == LSHIFT_EXPR
10393 && (TYPE_UNSIGNED (type)
10394 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10396 tree sval = TREE_OPERAND (arg1, 0);
10397 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10399 tree sh_cnt = TREE_OPERAND (arg1, 1);
10400 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10401 wi::exact_log2 (wi::to_wide (sval)));
10403 if (strict_overflow_p)
10404 fold_overflow_warning (("assuming signed overflow does not "
10405 "occur when simplifying A / (B << N)"),
10406 WARN_STRICT_OVERFLOW_MISC);
10408 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10409 sh_cnt, pow2);
10410 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10411 fold_convert_loc (loc, type, arg0), sh_cnt);
10415 /* Fall through */
10417 case ROUND_DIV_EXPR:
10418 case CEIL_DIV_EXPR:
10419 case EXACT_DIV_EXPR:
10420 if (integer_zerop (arg1))
10421 return NULL_TREE;
10423 /* Convert -A / -B to A / B when the type is signed and overflow is
10424 undefined. */
10425 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10426 && TREE_CODE (op0) == NEGATE_EXPR
10427 && negate_expr_p (op1))
10429 if (INTEGRAL_TYPE_P (type))
10430 fold_overflow_warning (("assuming signed overflow does not occur "
10431 "when distributing negation across "
10432 "division"),
10433 WARN_STRICT_OVERFLOW_MISC);
10434 return fold_build2_loc (loc, code, type,
10435 fold_convert_loc (loc, type,
10436 TREE_OPERAND (arg0, 0)),
10437 negate_expr (op1));
10439 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10440 && TREE_CODE (arg1) == NEGATE_EXPR
10441 && negate_expr_p (op0))
10443 if (INTEGRAL_TYPE_P (type))
10444 fold_overflow_warning (("assuming signed overflow does not occur "
10445 "when distributing negation across "
10446 "division"),
10447 WARN_STRICT_OVERFLOW_MISC);
10448 return fold_build2_loc (loc, code, type,
10449 negate_expr (op0),
10450 fold_convert_loc (loc, type,
10451 TREE_OPERAND (arg1, 0)));
10454 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10455 operation, EXACT_DIV_EXPR.
10457 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10458 At one time others generated faster code, it's not clear if they do
10459 after the last round to changes to the DIV code in expmed.c. */
10460 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10461 && multiple_of_p (type, arg0, arg1))
10462 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10463 fold_convert (type, arg0),
10464 fold_convert (type, arg1));
10466 strict_overflow_p = false;
10467 if (TREE_CODE (arg1) == INTEGER_CST
10468 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10469 &strict_overflow_p)) != 0)
10471 if (strict_overflow_p)
10472 fold_overflow_warning (("assuming signed overflow does not occur "
10473 "when simplifying division"),
10474 WARN_STRICT_OVERFLOW_MISC);
10475 return fold_convert_loc (loc, type, tem);
10478 return NULL_TREE;
10480 case CEIL_MOD_EXPR:
10481 case FLOOR_MOD_EXPR:
10482 case ROUND_MOD_EXPR:
10483 case TRUNC_MOD_EXPR:
10484 strict_overflow_p = false;
10485 if (TREE_CODE (arg1) == INTEGER_CST
10486 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10487 &strict_overflow_p)) != 0)
10489 if (strict_overflow_p)
10490 fold_overflow_warning (("assuming signed overflow does not occur "
10491 "when simplifying modulus"),
10492 WARN_STRICT_OVERFLOW_MISC);
10493 return fold_convert_loc (loc, type, tem);
10496 return NULL_TREE;
10498 case LROTATE_EXPR:
10499 case RROTATE_EXPR:
10500 case RSHIFT_EXPR:
10501 case LSHIFT_EXPR:
10502 /* Since negative shift count is not well-defined,
10503 don't try to compute it in the compiler. */
10504 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10505 return NULL_TREE;
10507 prec = element_precision (type);
10509 /* If we have a rotate of a bit operation with the rotate count and
10510 the second operand of the bit operation both constant,
10511 permute the two operations. */
10512 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10513 && (TREE_CODE (arg0) == BIT_AND_EXPR
10514 || TREE_CODE (arg0) == BIT_IOR_EXPR
10515 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10516 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10518 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10519 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10520 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10521 fold_build2_loc (loc, code, type,
10522 arg00, arg1),
10523 fold_build2_loc (loc, code, type,
10524 arg01, arg1));
10527 /* Two consecutive rotates adding up to the some integer
10528 multiple of the precision of the type can be ignored. */
10529 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10530 && TREE_CODE (arg0) == RROTATE_EXPR
10531 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10532 && wi::umod_trunc (wi::to_wide (arg1)
10533 + wi::to_wide (TREE_OPERAND (arg0, 1)),
10534 prec) == 0)
10535 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10537 return NULL_TREE;
10539 case MIN_EXPR:
10540 case MAX_EXPR:
10541 goto associate;
10543 case TRUTH_ANDIF_EXPR:
10544 /* Note that the operands of this must be ints
10545 and their values must be 0 or 1.
10546 ("true" is a fixed value perhaps depending on the language.) */
10547 /* If first arg is constant zero, return it. */
10548 if (integer_zerop (arg0))
10549 return fold_convert_loc (loc, type, arg0);
10550 /* FALLTHRU */
10551 case TRUTH_AND_EXPR:
10552 /* If either arg is constant true, drop it. */
10553 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10554 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10555 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10556 /* Preserve sequence points. */
10557 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10558 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10559 /* If second arg is constant zero, result is zero, but first arg
10560 must be evaluated. */
10561 if (integer_zerop (arg1))
10562 return omit_one_operand_loc (loc, type, arg1, arg0);
10563 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10564 case will be handled here. */
10565 if (integer_zerop (arg0))
10566 return omit_one_operand_loc (loc, type, arg0, arg1);
10568 /* !X && X is always false. */
10569 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10570 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10571 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10572 /* X && !X is always false. */
10573 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10574 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10575 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10577 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10578 means A >= Y && A != MAX, but in this case we know that
10579 A < X <= MAX. */
10581 if (!TREE_SIDE_EFFECTS (arg0)
10582 && !TREE_SIDE_EFFECTS (arg1))
10584 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10585 if (tem && !operand_equal_p (tem, arg0, 0))
10586 return fold_build2_loc (loc, code, type, tem, arg1);
10588 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10589 if (tem && !operand_equal_p (tem, arg1, 0))
10590 return fold_build2_loc (loc, code, type, arg0, tem);
10593 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10594 != NULL_TREE)
10595 return tem;
10597 return NULL_TREE;
10599 case TRUTH_ORIF_EXPR:
10600 /* Note that the operands of this must be ints
10601 and their values must be 0 or true.
10602 ("true" is a fixed value perhaps depending on the language.) */
10603 /* If first arg is constant true, return it. */
10604 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10605 return fold_convert_loc (loc, type, arg0);
10606 /* FALLTHRU */
10607 case TRUTH_OR_EXPR:
10608 /* If either arg is constant zero, drop it. */
10609 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10610 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10611 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10612 /* Preserve sequence points. */
10613 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10614 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10615 /* If second arg is constant true, result is true, but we must
10616 evaluate first arg. */
10617 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10618 return omit_one_operand_loc (loc, type, arg1, arg0);
10619 /* Likewise for first arg, but note this only occurs here for
10620 TRUTH_OR_EXPR. */
10621 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10622 return omit_one_operand_loc (loc, type, arg0, arg1);
10624 /* !X || X is always true. */
10625 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10626 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10627 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10628 /* X || !X is always true. */
10629 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10630 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10631 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10633 /* (X && !Y) || (!X && Y) is X ^ Y */
10634 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10635 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10637 tree a0, a1, l0, l1, n0, n1;
10639 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10640 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10642 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10643 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10645 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10646 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10648 if ((operand_equal_p (n0, a0, 0)
10649 && operand_equal_p (n1, a1, 0))
10650 || (operand_equal_p (n0, a1, 0)
10651 && operand_equal_p (n1, a0, 0)))
10652 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10655 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10656 != NULL_TREE)
10657 return tem;
10659 return NULL_TREE;
10661 case TRUTH_XOR_EXPR:
10662 /* If the second arg is constant zero, drop it. */
10663 if (integer_zerop (arg1))
10664 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10665 /* If the second arg is constant true, this is a logical inversion. */
10666 if (integer_onep (arg1))
10668 tem = invert_truthvalue_loc (loc, arg0);
10669 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10671 /* Identical arguments cancel to zero. */
10672 if (operand_equal_p (arg0, arg1, 0))
10673 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10675 /* !X ^ X is always true. */
10676 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10677 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10678 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10680 /* X ^ !X is always true. */
10681 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10682 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10683 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10685 return NULL_TREE;
10687 case EQ_EXPR:
10688 case NE_EXPR:
10689 STRIP_NOPS (arg0);
10690 STRIP_NOPS (arg1);
10692 tem = fold_comparison (loc, code, type, op0, op1);
10693 if (tem != NULL_TREE)
10694 return tem;
10696 /* bool_var != 1 becomes !bool_var. */
10697 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10698 && code == NE_EXPR)
10699 return fold_convert_loc (loc, type,
10700 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10701 TREE_TYPE (arg0), arg0));
10703 /* bool_var == 0 becomes !bool_var. */
10704 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10705 && code == EQ_EXPR)
10706 return fold_convert_loc (loc, type,
10707 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10708 TREE_TYPE (arg0), arg0));
10710 /* !exp != 0 becomes !exp */
10711 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10712 && code == NE_EXPR)
10713 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10715 /* If this is an EQ or NE comparison with zero and ARG0 is
10716 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10717 two operations, but the latter can be done in one less insn
10718 on machines that have only two-operand insns or on which a
10719 constant cannot be the first operand. */
10720 if (TREE_CODE (arg0) == BIT_AND_EXPR
10721 && integer_zerop (arg1))
10723 tree arg00 = TREE_OPERAND (arg0, 0);
10724 tree arg01 = TREE_OPERAND (arg0, 1);
10725 if (TREE_CODE (arg00) == LSHIFT_EXPR
10726 && integer_onep (TREE_OPERAND (arg00, 0)))
10728 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10729 arg01, TREE_OPERAND (arg00, 1));
10730 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10731 build_int_cst (TREE_TYPE (arg0), 1));
10732 return fold_build2_loc (loc, code, type,
10733 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10734 arg1);
10736 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10737 && integer_onep (TREE_OPERAND (arg01, 0)))
10739 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10740 arg00, TREE_OPERAND (arg01, 1));
10741 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10742 build_int_cst (TREE_TYPE (arg0), 1));
10743 return fold_build2_loc (loc, code, type,
10744 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10745 arg1);
10749 /* If this is an NE or EQ comparison of zero against the result of a
10750 signed MOD operation whose second operand is a power of 2, make
10751 the MOD operation unsigned since it is simpler and equivalent. */
10752 if (integer_zerop (arg1)
10753 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
10754 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
10755 || TREE_CODE (arg0) == CEIL_MOD_EXPR
10756 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
10757 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
10758 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10760 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
10761 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
10762 fold_convert_loc (loc, newtype,
10763 TREE_OPERAND (arg0, 0)),
10764 fold_convert_loc (loc, newtype,
10765 TREE_OPERAND (arg0, 1)));
10767 return fold_build2_loc (loc, code, type, newmod,
10768 fold_convert_loc (loc, newtype, arg1));
10771 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10772 C1 is a valid shift constant, and C2 is a power of two, i.e.
10773 a single bit. */
10774 if (TREE_CODE (arg0) == BIT_AND_EXPR
10775 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10776 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10777 == INTEGER_CST
10778 && integer_pow2p (TREE_OPERAND (arg0, 1))
10779 && integer_zerop (arg1))
10781 tree itype = TREE_TYPE (arg0);
10782 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10783 prec = TYPE_PRECISION (itype);
10785 /* Check for a valid shift count. */
10786 if (wi::ltu_p (wi::to_wide (arg001), prec))
10788 tree arg01 = TREE_OPERAND (arg0, 1);
10789 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10790 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10791 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10792 can be rewritten as (X & (C2 << C1)) != 0. */
10793 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10795 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10796 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10797 return fold_build2_loc (loc, code, type, tem,
10798 fold_convert_loc (loc, itype, arg1));
10800 /* Otherwise, for signed (arithmetic) shifts,
10801 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10802 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10803 else if (!TYPE_UNSIGNED (itype))
10804 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10805 arg000, build_int_cst (itype, 0));
10806 /* Otherwise, of unsigned (logical) shifts,
10807 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10808 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10809 else
10810 return omit_one_operand_loc (loc, type,
10811 code == EQ_EXPR ? integer_one_node
10812 : integer_zero_node,
10813 arg000);
10817 /* If this is a comparison of a field, we may be able to simplify it. */
10818 if ((TREE_CODE (arg0) == COMPONENT_REF
10819 || TREE_CODE (arg0) == BIT_FIELD_REF)
10820 /* Handle the constant case even without -O
10821 to make sure the warnings are given. */
10822 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10824 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10825 if (t1)
10826 return t1;
10829 /* Optimize comparisons of strlen vs zero to a compare of the
10830 first character of the string vs zero. To wit,
10831 strlen(ptr) == 0 => *ptr == 0
10832 strlen(ptr) != 0 => *ptr != 0
10833 Other cases should reduce to one of these two (or a constant)
10834 due to the return value of strlen being unsigned. */
10835 if (TREE_CODE (arg0) == CALL_EXPR
10836 && integer_zerop (arg1))
10838 tree fndecl = get_callee_fndecl (arg0);
10840 if (fndecl
10841 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
10842 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
10843 && call_expr_nargs (arg0) == 1
10844 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
10846 tree iref = build_fold_indirect_ref_loc (loc,
10847 CALL_EXPR_ARG (arg0, 0));
10848 return fold_build2_loc (loc, code, type, iref,
10849 build_int_cst (TREE_TYPE (iref), 0));
10853 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10854 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10855 if (TREE_CODE (arg0) == RSHIFT_EXPR
10856 && integer_zerop (arg1)
10857 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10859 tree arg00 = TREE_OPERAND (arg0, 0);
10860 tree arg01 = TREE_OPERAND (arg0, 1);
10861 tree itype = TREE_TYPE (arg00);
10862 if (wi::to_wide (arg01) == element_precision (itype) - 1)
10864 if (TYPE_UNSIGNED (itype))
10866 itype = signed_type_for (itype);
10867 arg00 = fold_convert_loc (loc, itype, arg00);
10869 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10870 type, arg00, build_zero_cst (itype));
10874 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10875 (X & C) == 0 when C is a single bit. */
10876 if (TREE_CODE (arg0) == BIT_AND_EXPR
10877 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10878 && integer_zerop (arg1)
10879 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10881 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10882 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10883 TREE_OPERAND (arg0, 1));
10884 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10885 type, tem,
10886 fold_convert_loc (loc, TREE_TYPE (arg0),
10887 arg1));
10890 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10891 constant C is a power of two, i.e. a single bit. */
10892 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10893 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10894 && integer_zerop (arg1)
10895 && integer_pow2p (TREE_OPERAND (arg0, 1))
10896 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10897 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10899 tree arg00 = TREE_OPERAND (arg0, 0);
10900 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10901 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10904 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10905 when is C is a power of two, i.e. a single bit. */
10906 if (TREE_CODE (arg0) == BIT_AND_EXPR
10907 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10908 && integer_zerop (arg1)
10909 && integer_pow2p (TREE_OPERAND (arg0, 1))
10910 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10911 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10913 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10914 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10915 arg000, TREE_OPERAND (arg0, 1));
10916 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10917 tem, build_int_cst (TREE_TYPE (tem), 0));
10920 if (integer_zerop (arg1)
10921 && tree_expr_nonzero_p (arg0))
10923 tree res = constant_boolean_node (code==NE_EXPR, type);
10924 return omit_one_operand_loc (loc, type, res, arg0);
10927 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10928 if (TREE_CODE (arg0) == BIT_AND_EXPR
10929 && TREE_CODE (arg1) == BIT_AND_EXPR)
10931 tree arg00 = TREE_OPERAND (arg0, 0);
10932 tree arg01 = TREE_OPERAND (arg0, 1);
10933 tree arg10 = TREE_OPERAND (arg1, 0);
10934 tree arg11 = TREE_OPERAND (arg1, 1);
10935 tree itype = TREE_TYPE (arg0);
10937 if (operand_equal_p (arg01, arg11, 0))
10939 tem = fold_convert_loc (loc, itype, arg10);
10940 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10941 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10942 return fold_build2_loc (loc, code, type, tem,
10943 build_zero_cst (itype));
10945 if (operand_equal_p (arg01, arg10, 0))
10947 tem = fold_convert_loc (loc, itype, arg11);
10948 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10949 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10950 return fold_build2_loc (loc, code, type, tem,
10951 build_zero_cst (itype));
10953 if (operand_equal_p (arg00, arg11, 0))
10955 tem = fold_convert_loc (loc, itype, arg10);
10956 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10957 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10958 return fold_build2_loc (loc, code, type, tem,
10959 build_zero_cst (itype));
10961 if (operand_equal_p (arg00, arg10, 0))
10963 tem = fold_convert_loc (loc, itype, arg11);
10964 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10965 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10966 return fold_build2_loc (loc, code, type, tem,
10967 build_zero_cst (itype));
10971 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10972 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10974 tree arg00 = TREE_OPERAND (arg0, 0);
10975 tree arg01 = TREE_OPERAND (arg0, 1);
10976 tree arg10 = TREE_OPERAND (arg1, 0);
10977 tree arg11 = TREE_OPERAND (arg1, 1);
10978 tree itype = TREE_TYPE (arg0);
10980 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10981 operand_equal_p guarantees no side-effects so we don't need
10982 to use omit_one_operand on Z. */
10983 if (operand_equal_p (arg01, arg11, 0))
10984 return fold_build2_loc (loc, code, type, arg00,
10985 fold_convert_loc (loc, TREE_TYPE (arg00),
10986 arg10));
10987 if (operand_equal_p (arg01, arg10, 0))
10988 return fold_build2_loc (loc, code, type, arg00,
10989 fold_convert_loc (loc, TREE_TYPE (arg00),
10990 arg11));
10991 if (operand_equal_p (arg00, arg11, 0))
10992 return fold_build2_loc (loc, code, type, arg01,
10993 fold_convert_loc (loc, TREE_TYPE (arg01),
10994 arg10));
10995 if (operand_equal_p (arg00, arg10, 0))
10996 return fold_build2_loc (loc, code, type, arg01,
10997 fold_convert_loc (loc, TREE_TYPE (arg01),
10998 arg11));
11000 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
11001 if (TREE_CODE (arg01) == INTEGER_CST
11002 && TREE_CODE (arg11) == INTEGER_CST)
11004 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
11005 fold_convert_loc (loc, itype, arg11));
11006 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
11007 return fold_build2_loc (loc, code, type, tem,
11008 fold_convert_loc (loc, itype, arg10));
11012 /* Attempt to simplify equality/inequality comparisons of complex
11013 values. Only lower the comparison if the result is known or
11014 can be simplified to a single scalar comparison. */
11015 if ((TREE_CODE (arg0) == COMPLEX_EXPR
11016 || TREE_CODE (arg0) == COMPLEX_CST)
11017 && (TREE_CODE (arg1) == COMPLEX_EXPR
11018 || TREE_CODE (arg1) == COMPLEX_CST))
11020 tree real0, imag0, real1, imag1;
11021 tree rcond, icond;
11023 if (TREE_CODE (arg0) == COMPLEX_EXPR)
11025 real0 = TREE_OPERAND (arg0, 0);
11026 imag0 = TREE_OPERAND (arg0, 1);
11028 else
11030 real0 = TREE_REALPART (arg0);
11031 imag0 = TREE_IMAGPART (arg0);
11034 if (TREE_CODE (arg1) == COMPLEX_EXPR)
11036 real1 = TREE_OPERAND (arg1, 0);
11037 imag1 = TREE_OPERAND (arg1, 1);
11039 else
11041 real1 = TREE_REALPART (arg1);
11042 imag1 = TREE_IMAGPART (arg1);
11045 rcond = fold_binary_loc (loc, code, type, real0, real1);
11046 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
11048 if (integer_zerop (rcond))
11050 if (code == EQ_EXPR)
11051 return omit_two_operands_loc (loc, type, boolean_false_node,
11052 imag0, imag1);
11053 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
11055 else
11057 if (code == NE_EXPR)
11058 return omit_two_operands_loc (loc, type, boolean_true_node,
11059 imag0, imag1);
11060 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
11064 icond = fold_binary_loc (loc, code, type, imag0, imag1);
11065 if (icond && TREE_CODE (icond) == INTEGER_CST)
11067 if (integer_zerop (icond))
11069 if (code == EQ_EXPR)
11070 return omit_two_operands_loc (loc, type, boolean_false_node,
11071 real0, real1);
11072 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11074 else
11076 if (code == NE_EXPR)
11077 return omit_two_operands_loc (loc, type, boolean_true_node,
11078 real0, real1);
11079 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11084 return NULL_TREE;
11086 case LT_EXPR:
11087 case GT_EXPR:
11088 case LE_EXPR:
11089 case GE_EXPR:
11090 tem = fold_comparison (loc, code, type, op0, op1);
11091 if (tem != NULL_TREE)
11092 return tem;
11094 /* Transform comparisons of the form X +- C CMP X. */
11095 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11096 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11097 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11098 && !HONOR_SNANS (arg0))
11100 tree arg01 = TREE_OPERAND (arg0, 1);
11101 enum tree_code code0 = TREE_CODE (arg0);
11102 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11104 /* (X - c) > X becomes false. */
11105 if (code == GT_EXPR
11106 && ((code0 == MINUS_EXPR && is_positive >= 0)
11107 || (code0 == PLUS_EXPR && is_positive <= 0)))
11108 return constant_boolean_node (0, type);
11110 /* Likewise (X + c) < X becomes false. */
11111 if (code == LT_EXPR
11112 && ((code0 == PLUS_EXPR && is_positive >= 0)
11113 || (code0 == MINUS_EXPR && is_positive <= 0)))
11114 return constant_boolean_node (0, type);
11116 /* Convert (X - c) <= X to true. */
11117 if (!HONOR_NANS (arg1)
11118 && code == LE_EXPR
11119 && ((code0 == MINUS_EXPR && is_positive >= 0)
11120 || (code0 == PLUS_EXPR && is_positive <= 0)))
11121 return constant_boolean_node (1, type);
11123 /* Convert (X + c) >= X to true. */
11124 if (!HONOR_NANS (arg1)
11125 && code == GE_EXPR
11126 && ((code0 == PLUS_EXPR && is_positive >= 0)
11127 || (code0 == MINUS_EXPR && is_positive <= 0)))
11128 return constant_boolean_node (1, type);
11131 /* If we are comparing an ABS_EXPR with a constant, we can
11132 convert all the cases into explicit comparisons, but they may
11133 well not be faster than doing the ABS and one comparison.
11134 But ABS (X) <= C is a range comparison, which becomes a subtraction
11135 and a comparison, and is probably faster. */
11136 if (code == LE_EXPR
11137 && TREE_CODE (arg1) == INTEGER_CST
11138 && TREE_CODE (arg0) == ABS_EXPR
11139 && ! TREE_SIDE_EFFECTS (arg0)
11140 && (tem = negate_expr (arg1)) != 0
11141 && TREE_CODE (tem) == INTEGER_CST
11142 && !TREE_OVERFLOW (tem))
11143 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11144 build2 (GE_EXPR, type,
11145 TREE_OPERAND (arg0, 0), tem),
11146 build2 (LE_EXPR, type,
11147 TREE_OPERAND (arg0, 0), arg1));
11149 /* Convert ABS_EXPR<x> >= 0 to true. */
11150 strict_overflow_p = false;
11151 if (code == GE_EXPR
11152 && (integer_zerop (arg1)
11153 || (! HONOR_NANS (arg0)
11154 && real_zerop (arg1)))
11155 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11157 if (strict_overflow_p)
11158 fold_overflow_warning (("assuming signed overflow does not occur "
11159 "when simplifying comparison of "
11160 "absolute value and zero"),
11161 WARN_STRICT_OVERFLOW_CONDITIONAL);
11162 return omit_one_operand_loc (loc, type,
11163 constant_boolean_node (true, type),
11164 arg0);
11167 /* Convert ABS_EXPR<x> < 0 to false. */
11168 strict_overflow_p = false;
11169 if (code == LT_EXPR
11170 && (integer_zerop (arg1) || real_zerop (arg1))
11171 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11173 if (strict_overflow_p)
11174 fold_overflow_warning (("assuming signed overflow does not occur "
11175 "when simplifying comparison of "
11176 "absolute value and zero"),
11177 WARN_STRICT_OVERFLOW_CONDITIONAL);
11178 return omit_one_operand_loc (loc, type,
11179 constant_boolean_node (false, type),
11180 arg0);
11183 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11184 and similarly for >= into !=. */
11185 if ((code == LT_EXPR || code == GE_EXPR)
11186 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11187 && TREE_CODE (arg1) == LSHIFT_EXPR
11188 && integer_onep (TREE_OPERAND (arg1, 0)))
11189 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11190 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11191 TREE_OPERAND (arg1, 1)),
11192 build_zero_cst (TREE_TYPE (arg0)));
11194 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11195 otherwise Y might be >= # of bits in X's type and thus e.g.
11196 (unsigned char) (1 << Y) for Y 15 might be 0.
11197 If the cast is widening, then 1 << Y should have unsigned type,
11198 otherwise if Y is number of bits in the signed shift type minus 1,
11199 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11200 31 might be 0xffffffff80000000. */
11201 if ((code == LT_EXPR || code == GE_EXPR)
11202 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11203 && CONVERT_EXPR_P (arg1)
11204 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11205 && (element_precision (TREE_TYPE (arg1))
11206 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11207 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11208 || (element_precision (TREE_TYPE (arg1))
11209 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11210 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11212 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11213 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11214 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11215 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11216 build_zero_cst (TREE_TYPE (arg0)));
11219 return NULL_TREE;
11221 case UNORDERED_EXPR:
11222 case ORDERED_EXPR:
11223 case UNLT_EXPR:
11224 case UNLE_EXPR:
11225 case UNGT_EXPR:
11226 case UNGE_EXPR:
11227 case UNEQ_EXPR:
11228 case LTGT_EXPR:
11229 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11231 tree targ0 = strip_float_extensions (arg0);
11232 tree targ1 = strip_float_extensions (arg1);
11233 tree newtype = TREE_TYPE (targ0);
11235 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11236 newtype = TREE_TYPE (targ1);
11238 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11239 return fold_build2_loc (loc, code, type,
11240 fold_convert_loc (loc, newtype, targ0),
11241 fold_convert_loc (loc, newtype, targ1));
11244 return NULL_TREE;
11246 case COMPOUND_EXPR:
11247 /* When pedantic, a compound expression can be neither an lvalue
11248 nor an integer constant expression. */
11249 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11250 return NULL_TREE;
11251 /* Don't let (0, 0) be null pointer constant. */
11252 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11253 : fold_convert_loc (loc, type, arg1);
11254 return pedantic_non_lvalue_loc (loc, tem);
11256 case ASSERT_EXPR:
11257 /* An ASSERT_EXPR should never be passed to fold_binary. */
11258 gcc_unreachable ();
11260 default:
11261 return NULL_TREE;
11262 } /* switch (code) */
11265 /* Used by contains_label_[p1]. */
11267 struct contains_label_data
11269 hash_set<tree> *pset;
11270 bool inside_switch_p;
11273 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11274 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
11275 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
11277 static tree
11278 contains_label_1 (tree *tp, int *walk_subtrees, void *data)
11280 contains_label_data *d = (contains_label_data *) data;
11281 switch (TREE_CODE (*tp))
11283 case LABEL_EXPR:
11284 return *tp;
11286 case CASE_LABEL_EXPR:
11287 if (!d->inside_switch_p)
11288 return *tp;
11289 return NULL_TREE;
11291 case SWITCH_EXPR:
11292 if (!d->inside_switch_p)
11294 if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
11295 return *tp;
11296 d->inside_switch_p = true;
11297 if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
11298 return *tp;
11299 d->inside_switch_p = false;
11300 *walk_subtrees = 0;
11302 return NULL_TREE;
11304 case GOTO_EXPR:
11305 *walk_subtrees = 0;
11306 return NULL_TREE;
11308 default:
11309 return NULL_TREE;
11313 /* Return whether the sub-tree ST contains a label which is accessible from
11314 outside the sub-tree. */
11316 static bool
11317 contains_label_p (tree st)
11319 hash_set<tree> pset;
11320 contains_label_data data = { &pset, false };
11321 return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
11324 /* Fold a ternary expression of code CODE and type TYPE with operands
11325 OP0, OP1, and OP2. Return the folded expression if folding is
11326 successful. Otherwise, return NULL_TREE. */
11328 tree
11329 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11330 tree op0, tree op1, tree op2)
11332 tree tem;
11333 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11334 enum tree_code_class kind = TREE_CODE_CLASS (code);
11336 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11337 && TREE_CODE_LENGTH (code) == 3);
11339 /* If this is a commutative operation, and OP0 is a constant, move it
11340 to OP1 to reduce the number of tests below. */
11341 if (commutative_ternary_tree_code (code)
11342 && tree_swap_operands_p (op0, op1))
11343 return fold_build3_loc (loc, code, type, op1, op0, op2);
11345 tem = generic_simplify (loc, code, type, op0, op1, op2);
11346 if (tem)
11347 return tem;
11349 /* Strip any conversions that don't change the mode. This is safe
11350 for every expression, except for a comparison expression because
11351 its signedness is derived from its operands. So, in the latter
11352 case, only strip conversions that don't change the signedness.
11354 Note that this is done as an internal manipulation within the
11355 constant folder, in order to find the simplest representation of
11356 the arguments so that their form can be studied. In any cases,
11357 the appropriate type conversions should be put back in the tree
11358 that will get out of the constant folder. */
11359 if (op0)
11361 arg0 = op0;
11362 STRIP_NOPS (arg0);
11365 if (op1)
11367 arg1 = op1;
11368 STRIP_NOPS (arg1);
11371 if (op2)
11373 arg2 = op2;
11374 STRIP_NOPS (arg2);
11377 switch (code)
11379 case COMPONENT_REF:
11380 if (TREE_CODE (arg0) == CONSTRUCTOR
11381 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11383 unsigned HOST_WIDE_INT idx;
11384 tree field, value;
11385 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11386 if (field == arg1)
11387 return value;
11389 return NULL_TREE;
11391 case COND_EXPR:
11392 case VEC_COND_EXPR:
11393 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11394 so all simple results must be passed through pedantic_non_lvalue. */
11395 if (TREE_CODE (arg0) == INTEGER_CST)
11397 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11398 tem = integer_zerop (arg0) ? op2 : op1;
11399 /* Only optimize constant conditions when the selected branch
11400 has the same type as the COND_EXPR. This avoids optimizing
11401 away "c ? x : throw", where the throw has a void type.
11402 Avoid throwing away that operand which contains label. */
11403 if ((!TREE_SIDE_EFFECTS (unused_op)
11404 || !contains_label_p (unused_op))
11405 && (! VOID_TYPE_P (TREE_TYPE (tem))
11406 || VOID_TYPE_P (type)))
11407 return pedantic_non_lvalue_loc (loc, tem);
11408 return NULL_TREE;
11410 else if (TREE_CODE (arg0) == VECTOR_CST)
11412 unsigned HOST_WIDE_INT nelts;
11413 if ((TREE_CODE (arg1) == VECTOR_CST
11414 || TREE_CODE (arg1) == CONSTRUCTOR)
11415 && (TREE_CODE (arg2) == VECTOR_CST
11416 || TREE_CODE (arg2) == CONSTRUCTOR)
11417 && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
11419 vec_perm_builder sel (nelts, nelts, 1);
11420 for (unsigned int i = 0; i < nelts; i++)
11422 tree val = VECTOR_CST_ELT (arg0, i);
11423 if (integer_all_onesp (val))
11424 sel.quick_push (i);
11425 else if (integer_zerop (val))
11426 sel.quick_push (nelts + i);
11427 else /* Currently unreachable. */
11428 return NULL_TREE;
11430 vec_perm_indices indices (sel, 2, nelts);
11431 tree t = fold_vec_perm (type, arg1, arg2, indices);
11432 if (t != NULL_TREE)
11433 return t;
11437 /* If we have A op B ? A : C, we may be able to convert this to a
11438 simpler expression, depending on the operation and the values
11439 of B and C. Signed zeros prevent all of these transformations,
11440 for reasons given above each one.
11442 Also try swapping the arguments and inverting the conditional. */
11443 if (COMPARISON_CLASS_P (arg0)
11444 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
11445 && !HONOR_SIGNED_ZEROS (element_mode (op1)))
11447 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11448 if (tem)
11449 return tem;
11452 if (COMPARISON_CLASS_P (arg0)
11453 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11454 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11456 location_t loc0 = expr_location_or (arg0, loc);
11457 tem = fold_invert_truthvalue (loc0, arg0);
11458 if (tem && COMPARISON_CLASS_P (tem))
11460 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11461 if (tem)
11462 return tem;
11466 /* If the second operand is simpler than the third, swap them
11467 since that produces better jump optimization results. */
11468 if (truth_value_p (TREE_CODE (arg0))
11469 && tree_swap_operands_p (op1, op2))
11471 location_t loc0 = expr_location_or (arg0, loc);
11472 /* See if this can be inverted. If it can't, possibly because
11473 it was a floating-point inequality comparison, don't do
11474 anything. */
11475 tem = fold_invert_truthvalue (loc0, arg0);
11476 if (tem)
11477 return fold_build3_loc (loc, code, type, tem, op2, op1);
11480 /* Convert A ? 1 : 0 to simply A. */
11481 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11482 : (integer_onep (op1)
11483 && !VECTOR_TYPE_P (type)))
11484 && integer_zerop (op2)
11485 /* If we try to convert OP0 to our type, the
11486 call to fold will try to move the conversion inside
11487 a COND, which will recurse. In that case, the COND_EXPR
11488 is probably the best choice, so leave it alone. */
11489 && type == TREE_TYPE (arg0))
11490 return pedantic_non_lvalue_loc (loc, arg0);
11492 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11493 over COND_EXPR in cases such as floating point comparisons. */
11494 if (integer_zerop (op1)
11495 && code == COND_EXPR
11496 && integer_onep (op2)
11497 && !VECTOR_TYPE_P (type)
11498 && truth_value_p (TREE_CODE (arg0)))
11499 return pedantic_non_lvalue_loc (loc,
11500 fold_convert_loc (loc, type,
11501 invert_truthvalue_loc (loc,
11502 arg0)));
11504 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11505 if (TREE_CODE (arg0) == LT_EXPR
11506 && integer_zerop (TREE_OPERAND (arg0, 1))
11507 && integer_zerop (op2)
11508 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11510 /* sign_bit_p looks through both zero and sign extensions,
11511 but for this optimization only sign extensions are
11512 usable. */
11513 tree tem2 = TREE_OPERAND (arg0, 0);
11514 while (tem != tem2)
11516 if (TREE_CODE (tem2) != NOP_EXPR
11517 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11519 tem = NULL_TREE;
11520 break;
11522 tem2 = TREE_OPERAND (tem2, 0);
11524 /* sign_bit_p only checks ARG1 bits within A's precision.
11525 If <sign bit of A> has wider type than A, bits outside
11526 of A's precision in <sign bit of A> need to be checked.
11527 If they are all 0, this optimization needs to be done
11528 in unsigned A's type, if they are all 1 in signed A's type,
11529 otherwise this can't be done. */
11530 if (tem
11531 && TYPE_PRECISION (TREE_TYPE (tem))
11532 < TYPE_PRECISION (TREE_TYPE (arg1))
11533 && TYPE_PRECISION (TREE_TYPE (tem))
11534 < TYPE_PRECISION (type))
11536 int inner_width, outer_width;
11537 tree tem_type;
11539 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11540 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11541 if (outer_width > TYPE_PRECISION (type))
11542 outer_width = TYPE_PRECISION (type);
11544 wide_int mask = wi::shifted_mask
11545 (inner_width, outer_width - inner_width, false,
11546 TYPE_PRECISION (TREE_TYPE (arg1)));
11548 wide_int common = mask & wi::to_wide (arg1);
11549 if (common == mask)
11551 tem_type = signed_type_for (TREE_TYPE (tem));
11552 tem = fold_convert_loc (loc, tem_type, tem);
11554 else if (common == 0)
11556 tem_type = unsigned_type_for (TREE_TYPE (tem));
11557 tem = fold_convert_loc (loc, tem_type, tem);
11559 else
11560 tem = NULL;
11563 if (tem)
11564 return
11565 fold_convert_loc (loc, type,
11566 fold_build2_loc (loc, BIT_AND_EXPR,
11567 TREE_TYPE (tem), tem,
11568 fold_convert_loc (loc,
11569 TREE_TYPE (tem),
11570 arg1)));
11573 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11574 already handled above. */
11575 if (TREE_CODE (arg0) == BIT_AND_EXPR
11576 && integer_onep (TREE_OPERAND (arg0, 1))
11577 && integer_zerop (op2)
11578 && integer_pow2p (arg1))
11580 tree tem = TREE_OPERAND (arg0, 0);
11581 STRIP_NOPS (tem);
11582 if (TREE_CODE (tem) == RSHIFT_EXPR
11583 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11584 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11585 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11586 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11587 fold_convert_loc (loc, type,
11588 TREE_OPERAND (tem, 0)),
11589 op1);
11592 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11593 is probably obsolete because the first operand should be a
11594 truth value (that's why we have the two cases above), but let's
11595 leave it in until we can confirm this for all front-ends. */
11596 if (integer_zerop (op2)
11597 && TREE_CODE (arg0) == NE_EXPR
11598 && integer_zerop (TREE_OPERAND (arg0, 1))
11599 && integer_pow2p (arg1)
11600 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11601 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11602 arg1, OEP_ONLY_CONST))
11603 return pedantic_non_lvalue_loc (loc,
11604 fold_convert_loc (loc, type,
11605 TREE_OPERAND (arg0, 0)));
11607 /* Disable the transformations below for vectors, since
11608 fold_binary_op_with_conditional_arg may undo them immediately,
11609 yielding an infinite loop. */
11610 if (code == VEC_COND_EXPR)
11611 return NULL_TREE;
11613 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11614 if (integer_zerop (op2)
11615 && truth_value_p (TREE_CODE (arg0))
11616 && truth_value_p (TREE_CODE (arg1))
11617 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11618 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11619 : TRUTH_ANDIF_EXPR,
11620 type, fold_convert_loc (loc, type, arg0), op1);
11622 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11623 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11624 && truth_value_p (TREE_CODE (arg0))
11625 && truth_value_p (TREE_CODE (arg1))
11626 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11628 location_t loc0 = expr_location_or (arg0, loc);
11629 /* Only perform transformation if ARG0 is easily inverted. */
11630 tem = fold_invert_truthvalue (loc0, arg0);
11631 if (tem)
11632 return fold_build2_loc (loc, code == VEC_COND_EXPR
11633 ? BIT_IOR_EXPR
11634 : TRUTH_ORIF_EXPR,
11635 type, fold_convert_loc (loc, type, tem),
11636 op1);
11639 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11640 if (integer_zerop (arg1)
11641 && truth_value_p (TREE_CODE (arg0))
11642 && truth_value_p (TREE_CODE (op2))
11643 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11645 location_t loc0 = expr_location_or (arg0, loc);
11646 /* Only perform transformation if ARG0 is easily inverted. */
11647 tem = fold_invert_truthvalue (loc0, arg0);
11648 if (tem)
11649 return fold_build2_loc (loc, code == VEC_COND_EXPR
11650 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11651 type, fold_convert_loc (loc, type, tem),
11652 op2);
11655 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11656 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11657 && truth_value_p (TREE_CODE (arg0))
11658 && truth_value_p (TREE_CODE (op2))
11659 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11660 return fold_build2_loc (loc, code == VEC_COND_EXPR
11661 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11662 type, fold_convert_loc (loc, type, arg0), op2);
11664 return NULL_TREE;
11666 case CALL_EXPR:
11667 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11668 of fold_ternary on them. */
11669 gcc_unreachable ();
11671 case BIT_FIELD_REF:
11672 if (TREE_CODE (arg0) == VECTOR_CST
11673 && (type == TREE_TYPE (TREE_TYPE (arg0))
11674 || (VECTOR_TYPE_P (type)
11675 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
11676 && tree_fits_uhwi_p (op1)
11677 && tree_fits_uhwi_p (op2))
11679 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11680 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11681 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11682 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11684 if (n != 0
11685 && (idx % width) == 0
11686 && (n % width) == 0
11687 && known_le ((idx + n) / width,
11688 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
11690 idx = idx / width;
11691 n = n / width;
11693 if (TREE_CODE (arg0) == VECTOR_CST)
11695 if (n == 1)
11697 tem = VECTOR_CST_ELT (arg0, idx);
11698 if (VECTOR_TYPE_P (type))
11699 tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
11700 return tem;
11703 tree_vector_builder vals (type, n, 1);
11704 for (unsigned i = 0; i < n; ++i)
11705 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
11706 return vals.build ();
11711 /* On constants we can use native encode/interpret to constant
11712 fold (nearly) all BIT_FIELD_REFs. */
11713 if (CONSTANT_CLASS_P (arg0)
11714 && can_native_interpret_type_p (type)
11715 && BITS_PER_UNIT == 8
11716 && tree_fits_uhwi_p (op1)
11717 && tree_fits_uhwi_p (op2))
11719 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11720 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11721 /* Limit us to a reasonable amount of work. To relax the
11722 other limitations we need bit-shifting of the buffer
11723 and rounding up the size. */
11724 if (bitpos % BITS_PER_UNIT == 0
11725 && bitsize % BITS_PER_UNIT == 0
11726 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11728 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11729 unsigned HOST_WIDE_INT len
11730 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11731 bitpos / BITS_PER_UNIT);
11732 if (len > 0
11733 && len * BITS_PER_UNIT >= bitsize)
11735 tree v = native_interpret_expr (type, b,
11736 bitsize / BITS_PER_UNIT);
11737 if (v)
11738 return v;
11743 return NULL_TREE;
11745 case VEC_PERM_EXPR:
11746 if (TREE_CODE (arg2) == VECTOR_CST)
11748 /* Build a vector of integers from the tree mask. */
11749 vec_perm_builder builder;
11750 if (!tree_to_vec_perm_builder (&builder, arg2))
11751 return NULL_TREE;
11753 /* Create a vec_perm_indices for the integer vector. */
11754 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
11755 bool single_arg = (op0 == op1);
11756 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
11758 /* Check for cases that fold to OP0 or OP1 in their original
11759 element order. */
11760 if (sel.series_p (0, 1, 0, 1))
11761 return op0;
11762 if (sel.series_p (0, 1, nelts, 1))
11763 return op1;
11765 if (!single_arg)
11767 if (sel.all_from_input_p (0))
11768 op1 = op0;
11769 else if (sel.all_from_input_p (1))
11771 op0 = op1;
11772 sel.rotate_inputs (1);
11776 if ((TREE_CODE (op0) == VECTOR_CST
11777 || TREE_CODE (op0) == CONSTRUCTOR)
11778 && (TREE_CODE (op1) == VECTOR_CST
11779 || TREE_CODE (op1) == CONSTRUCTOR))
11781 tree t = fold_vec_perm (type, op0, op1, sel);
11782 if (t != NULL_TREE)
11783 return t;
11786 bool changed = (op0 == op1 && !single_arg);
11788 /* Generate a canonical form of the selector. */
11789 if (arg2 == op2 && sel.encoding () != builder)
11791 /* Some targets are deficient and fail to expand a single
11792 argument permutation while still allowing an equivalent
11793 2-argument version. */
11794 if (sel.ninputs () == 2
11795 || can_vec_perm_const_p (TYPE_MODE (type), sel, false))
11796 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11797 else
11799 vec_perm_indices sel2 (builder, 2, nelts);
11800 if (can_vec_perm_const_p (TYPE_MODE (type), sel2, false))
11801 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel2);
11802 else
11803 /* Not directly supported with either encoding,
11804 so use the preferred form. */
11805 op2 = vec_perm_indices_to_tree (TREE_TYPE (arg2), sel);
11807 changed = true;
11810 if (changed)
11811 return build3_loc (loc, VEC_PERM_EXPR, type, op0, op1, op2);
11813 return NULL_TREE;
11815 case BIT_INSERT_EXPR:
11816 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11817 if (TREE_CODE (arg0) == INTEGER_CST
11818 && TREE_CODE (arg1) == INTEGER_CST)
11820 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11821 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11822 wide_int tem = (wi::to_wide (arg0)
11823 & wi::shifted_mask (bitpos, bitsize, true,
11824 TYPE_PRECISION (type)));
11825 wide_int tem2
11826 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11827 bitsize), bitpos);
11828 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11830 else if (TREE_CODE (arg0) == VECTOR_CST
11831 && CONSTANT_CLASS_P (arg1)
11832 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11833 TREE_TYPE (arg1)))
11835 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11836 unsigned HOST_WIDE_INT elsize
11837 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11838 if (bitpos % elsize == 0)
11840 unsigned k = bitpos / elsize;
11841 unsigned HOST_WIDE_INT nelts;
11842 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11843 return arg0;
11844 else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
11846 tree_vector_builder elts (type, nelts, 1);
11847 elts.quick_grow (nelts);
11848 for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
11849 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
11850 return elts.build ();
11854 return NULL_TREE;
11856 default:
11857 return NULL_TREE;
11858 } /* switch (code) */
11861 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11862 of an array (or vector). */
11864 tree
11865 get_array_ctor_element_at_index (tree ctor, offset_int access_index)
11867 tree index_type = NULL_TREE;
11868 offset_int low_bound = 0;
11870 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11872 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11873 if (domain_type && TYPE_MIN_VALUE (domain_type))
11875 /* Static constructors for variably sized objects makes no sense. */
11876 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11877 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11878 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11882 if (index_type)
11883 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11884 TYPE_SIGN (index_type));
11886 offset_int index = low_bound - 1;
11887 if (index_type)
11888 index = wi::ext (index, TYPE_PRECISION (index_type),
11889 TYPE_SIGN (index_type));
11891 offset_int max_index;
11892 unsigned HOST_WIDE_INT cnt;
11893 tree cfield, cval;
11895 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11897 /* Array constructor might explicitly set index, or specify a range,
11898 or leave index NULL meaning that it is next index after previous
11899 one. */
11900 if (cfield)
11902 if (TREE_CODE (cfield) == INTEGER_CST)
11903 max_index = index = wi::to_offset (cfield);
11904 else
11906 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11907 index = wi::to_offset (TREE_OPERAND (cfield, 0));
11908 max_index = wi::to_offset (TREE_OPERAND (cfield, 1));
11911 else
11913 index += 1;
11914 if (index_type)
11915 index = wi::ext (index, TYPE_PRECISION (index_type),
11916 TYPE_SIGN (index_type));
11917 max_index = index;
11920 /* Do we have match? */
11921 if (wi::cmpu (access_index, index) >= 0
11922 && wi::cmpu (access_index, max_index) <= 0)
11923 return cval;
11925 return NULL_TREE;
11928 /* Perform constant folding and related simplification of EXPR.
11929 The related simplifications include x*1 => x, x*0 => 0, etc.,
11930 and application of the associative law.
11931 NOP_EXPR conversions may be removed freely (as long as we
11932 are careful not to change the type of the overall expression).
11933 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11934 but we can constant-fold them if they have constant operands. */
11936 #ifdef ENABLE_FOLD_CHECKING
11937 # define fold(x) fold_1 (x)
11938 static tree fold_1 (tree);
11939 static
11940 #endif
11941 tree
11942 fold (tree expr)
11944 const tree t = expr;
11945 enum tree_code code = TREE_CODE (t);
11946 enum tree_code_class kind = TREE_CODE_CLASS (code);
11947 tree tem;
11948 location_t loc = EXPR_LOCATION (expr);
11950 /* Return right away if a constant. */
11951 if (kind == tcc_constant)
11952 return t;
11954 /* CALL_EXPR-like objects with variable numbers of operands are
11955 treated specially. */
11956 if (kind == tcc_vl_exp)
11958 if (code == CALL_EXPR)
11960 tem = fold_call_expr (loc, expr, false);
11961 return tem ? tem : expr;
11963 return expr;
11966 if (IS_EXPR_CODE_CLASS (kind))
11968 tree type = TREE_TYPE (t);
11969 tree op0, op1, op2;
11971 switch (TREE_CODE_LENGTH (code))
11973 case 1:
11974 op0 = TREE_OPERAND (t, 0);
11975 tem = fold_unary_loc (loc, code, type, op0);
11976 return tem ? tem : expr;
11977 case 2:
11978 op0 = TREE_OPERAND (t, 0);
11979 op1 = TREE_OPERAND (t, 1);
11980 tem = fold_binary_loc (loc, code, type, op0, op1);
11981 return tem ? tem : expr;
11982 case 3:
11983 op0 = TREE_OPERAND (t, 0);
11984 op1 = TREE_OPERAND (t, 1);
11985 op2 = TREE_OPERAND (t, 2);
11986 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
11987 return tem ? tem : expr;
11988 default:
11989 break;
11993 switch (code)
11995 case ARRAY_REF:
11997 tree op0 = TREE_OPERAND (t, 0);
11998 tree op1 = TREE_OPERAND (t, 1);
12000 if (TREE_CODE (op1) == INTEGER_CST
12001 && TREE_CODE (op0) == CONSTRUCTOR
12002 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
12004 tree val = get_array_ctor_element_at_index (op0,
12005 wi::to_offset (op1));
12006 if (val)
12007 return val;
12010 return t;
12013 /* Return a VECTOR_CST if possible. */
12014 case CONSTRUCTOR:
12016 tree type = TREE_TYPE (t);
12017 if (TREE_CODE (type) != VECTOR_TYPE)
12018 return t;
12020 unsigned i;
12021 tree val;
12022 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
12023 if (! CONSTANT_CLASS_P (val))
12024 return t;
12026 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12029 case CONST_DECL:
12030 return fold (DECL_INITIAL (t));
12032 default:
12033 return t;
12034 } /* switch (code) */
12037 #ifdef ENABLE_FOLD_CHECKING
12038 #undef fold
12040 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12041 hash_table<nofree_ptr_hash<const tree_node> > *);
12042 static void fold_check_failed (const_tree, const_tree);
12043 void print_fold_checksum (const_tree);
12045 /* When --enable-checking=fold, compute a digest of expr before
12046 and after actual fold call to see if fold did not accidentally
12047 change original expr. */
12049 tree
12050 fold (tree expr)
12052 tree ret;
12053 struct md5_ctx ctx;
12054 unsigned char checksum_before[16], checksum_after[16];
12055 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12057 md5_init_ctx (&ctx);
12058 fold_checksum_tree (expr, &ctx, &ht);
12059 md5_finish_ctx (&ctx, checksum_before);
12060 ht.empty ();
12062 ret = fold_1 (expr);
12064 md5_init_ctx (&ctx);
12065 fold_checksum_tree (expr, &ctx, &ht);
12066 md5_finish_ctx (&ctx, checksum_after);
12068 if (memcmp (checksum_before, checksum_after, 16))
12069 fold_check_failed (expr, ret);
12071 return ret;
12074 void
12075 print_fold_checksum (const_tree expr)
12077 struct md5_ctx ctx;
12078 unsigned char checksum[16], cnt;
12079 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12081 md5_init_ctx (&ctx);
12082 fold_checksum_tree (expr, &ctx, &ht);
12083 md5_finish_ctx (&ctx, checksum);
12084 for (cnt = 0; cnt < 16; ++cnt)
12085 fprintf (stderr, "%02x", checksum[cnt]);
12086 putc ('\n', stderr);
12089 static void
12090 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12092 internal_error ("fold check: original tree changed by fold");
12095 static void
12096 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12097 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12099 const tree_node **slot;
12100 enum tree_code code;
12101 union tree_node buf;
12102 int i, len;
12104 recursive_label:
12105 if (expr == NULL)
12106 return;
12107 slot = ht->find_slot (expr, INSERT);
12108 if (*slot != NULL)
12109 return;
12110 *slot = expr;
12111 code = TREE_CODE (expr);
12112 if (TREE_CODE_CLASS (code) == tcc_declaration
12113 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12115 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12116 memcpy ((char *) &buf, expr, tree_size (expr));
12117 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
12118 buf.decl_with_vis.symtab_node = NULL;
12119 expr = (tree) &buf;
12121 else if (TREE_CODE_CLASS (code) == tcc_type
12122 && (TYPE_POINTER_TO (expr)
12123 || TYPE_REFERENCE_TO (expr)
12124 || TYPE_CACHED_VALUES_P (expr)
12125 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12126 || TYPE_NEXT_VARIANT (expr)
12127 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12129 /* Allow these fields to be modified. */
12130 tree tmp;
12131 memcpy ((char *) &buf, expr, tree_size (expr));
12132 expr = tmp = (tree) &buf;
12133 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12134 TYPE_POINTER_TO (tmp) = NULL;
12135 TYPE_REFERENCE_TO (tmp) = NULL;
12136 TYPE_NEXT_VARIANT (tmp) = NULL;
12137 TYPE_ALIAS_SET (tmp) = -1;
12138 if (TYPE_CACHED_VALUES_P (tmp))
12140 TYPE_CACHED_VALUES_P (tmp) = 0;
12141 TYPE_CACHED_VALUES (tmp) = NULL;
12144 md5_process_bytes (expr, tree_size (expr), ctx);
12145 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12146 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12147 if (TREE_CODE_CLASS (code) != tcc_type
12148 && TREE_CODE_CLASS (code) != tcc_declaration
12149 && code != TREE_LIST
12150 && code != SSA_NAME
12151 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12152 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12153 switch (TREE_CODE_CLASS (code))
12155 case tcc_constant:
12156 switch (code)
12158 case STRING_CST:
12159 md5_process_bytes (TREE_STRING_POINTER (expr),
12160 TREE_STRING_LENGTH (expr), ctx);
12161 break;
12162 case COMPLEX_CST:
12163 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12164 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12165 break;
12166 case VECTOR_CST:
12167 len = vector_cst_encoded_nelts (expr);
12168 for (i = 0; i < len; ++i)
12169 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
12170 break;
12171 default:
12172 break;
12174 break;
12175 case tcc_exceptional:
12176 switch (code)
12178 case TREE_LIST:
12179 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12180 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12181 expr = TREE_CHAIN (expr);
12182 goto recursive_label;
12183 break;
12184 case TREE_VEC:
12185 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12186 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12187 break;
12188 default:
12189 break;
12191 break;
12192 case tcc_expression:
12193 case tcc_reference:
12194 case tcc_comparison:
12195 case tcc_unary:
12196 case tcc_binary:
12197 case tcc_statement:
12198 case tcc_vl_exp:
12199 len = TREE_OPERAND_LENGTH (expr);
12200 for (i = 0; i < len; ++i)
12201 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12202 break;
12203 case tcc_declaration:
12204 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12205 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12206 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12208 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12209 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12210 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12211 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12212 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12215 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12217 if (TREE_CODE (expr) == FUNCTION_DECL)
12219 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12220 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12222 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12224 break;
12225 case tcc_type:
12226 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12227 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12228 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12229 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12230 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12231 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12232 if (INTEGRAL_TYPE_P (expr)
12233 || SCALAR_FLOAT_TYPE_P (expr))
12235 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12236 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12238 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12239 if (TREE_CODE (expr) == RECORD_TYPE
12240 || TREE_CODE (expr) == UNION_TYPE
12241 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12242 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12243 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12244 break;
12245 default:
12246 break;
12250 /* Helper function for outputting the checksum of a tree T. When
12251 debugging with gdb, you can "define mynext" to be "next" followed
12252 by "call debug_fold_checksum (op0)", then just trace down till the
12253 outputs differ. */
12255 DEBUG_FUNCTION void
12256 debug_fold_checksum (const_tree t)
12258 int i;
12259 unsigned char checksum[16];
12260 struct md5_ctx ctx;
12261 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12263 md5_init_ctx (&ctx);
12264 fold_checksum_tree (t, &ctx, &ht);
12265 md5_finish_ctx (&ctx, checksum);
12266 ht.empty ();
12268 for (i = 0; i < 16; i++)
12269 fprintf (stderr, "%d ", checksum[i]);
12271 fprintf (stderr, "\n");
12274 #endif
12276 /* Fold a unary tree expression with code CODE of type TYPE with an
12277 operand OP0. LOC is the location of the resulting expression.
12278 Return a folded expression if successful. Otherwise, return a tree
12279 expression with code CODE of type TYPE with an operand OP0. */
12281 tree
12282 fold_build1_loc (location_t loc,
12283 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12285 tree tem;
12286 #ifdef ENABLE_FOLD_CHECKING
12287 unsigned char checksum_before[16], checksum_after[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);
12294 ht.empty ();
12295 #endif
12297 tem = fold_unary_loc (loc, code, type, op0);
12298 if (!tem)
12299 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12301 #ifdef ENABLE_FOLD_CHECKING
12302 md5_init_ctx (&ctx);
12303 fold_checksum_tree (op0, &ctx, &ht);
12304 md5_finish_ctx (&ctx, checksum_after);
12306 if (memcmp (checksum_before, checksum_after, 16))
12307 fold_check_failed (op0, tem);
12308 #endif
12309 return tem;
12312 /* Fold a binary tree expression with code CODE of type TYPE with
12313 operands OP0 and OP1. LOC is the location of the resulting
12314 expression. Return a folded expression if successful. Otherwise,
12315 return a tree expression with code CODE of type TYPE with operands
12316 OP0 and OP1. */
12318 tree
12319 fold_build2_loc (location_t loc,
12320 enum tree_code code, tree type, tree op0, tree op1
12321 MEM_STAT_DECL)
12323 tree tem;
12324 #ifdef ENABLE_FOLD_CHECKING
12325 unsigned char checksum_before_op0[16],
12326 checksum_before_op1[16],
12327 checksum_after_op0[16],
12328 checksum_after_op1[16];
12329 struct md5_ctx ctx;
12330 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12332 md5_init_ctx (&ctx);
12333 fold_checksum_tree (op0, &ctx, &ht);
12334 md5_finish_ctx (&ctx, checksum_before_op0);
12335 ht.empty ();
12337 md5_init_ctx (&ctx);
12338 fold_checksum_tree (op1, &ctx, &ht);
12339 md5_finish_ctx (&ctx, checksum_before_op1);
12340 ht.empty ();
12341 #endif
12343 tem = fold_binary_loc (loc, code, type, op0, op1);
12344 if (!tem)
12345 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12347 #ifdef ENABLE_FOLD_CHECKING
12348 md5_init_ctx (&ctx);
12349 fold_checksum_tree (op0, &ctx, &ht);
12350 md5_finish_ctx (&ctx, checksum_after_op0);
12351 ht.empty ();
12353 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12354 fold_check_failed (op0, tem);
12356 md5_init_ctx (&ctx);
12357 fold_checksum_tree (op1, &ctx, &ht);
12358 md5_finish_ctx (&ctx, checksum_after_op1);
12360 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12361 fold_check_failed (op1, tem);
12362 #endif
12363 return tem;
12366 /* Fold a ternary tree expression with code CODE of type TYPE with
12367 operands OP0, OP1, and OP2. Return a folded expression if
12368 successful. Otherwise, return a tree expression with code CODE of
12369 type TYPE with operands OP0, OP1, and OP2. */
12371 tree
12372 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12373 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12375 tree tem;
12376 #ifdef ENABLE_FOLD_CHECKING
12377 unsigned char checksum_before_op0[16],
12378 checksum_before_op1[16],
12379 checksum_before_op2[16],
12380 checksum_after_op0[16],
12381 checksum_after_op1[16],
12382 checksum_after_op2[16];
12383 struct md5_ctx ctx;
12384 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12386 md5_init_ctx (&ctx);
12387 fold_checksum_tree (op0, &ctx, &ht);
12388 md5_finish_ctx (&ctx, checksum_before_op0);
12389 ht.empty ();
12391 md5_init_ctx (&ctx);
12392 fold_checksum_tree (op1, &ctx, &ht);
12393 md5_finish_ctx (&ctx, checksum_before_op1);
12394 ht.empty ();
12396 md5_init_ctx (&ctx);
12397 fold_checksum_tree (op2, &ctx, &ht);
12398 md5_finish_ctx (&ctx, checksum_before_op2);
12399 ht.empty ();
12400 #endif
12402 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12403 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12404 if (!tem)
12405 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12407 #ifdef ENABLE_FOLD_CHECKING
12408 md5_init_ctx (&ctx);
12409 fold_checksum_tree (op0, &ctx, &ht);
12410 md5_finish_ctx (&ctx, checksum_after_op0);
12411 ht.empty ();
12413 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12414 fold_check_failed (op0, tem);
12416 md5_init_ctx (&ctx);
12417 fold_checksum_tree (op1, &ctx, &ht);
12418 md5_finish_ctx (&ctx, checksum_after_op1);
12419 ht.empty ();
12421 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12422 fold_check_failed (op1, tem);
12424 md5_init_ctx (&ctx);
12425 fold_checksum_tree (op2, &ctx, &ht);
12426 md5_finish_ctx (&ctx, checksum_after_op2);
12428 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12429 fold_check_failed (op2, tem);
12430 #endif
12431 return tem;
12434 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12435 arguments in ARGARRAY, and a null static chain.
12436 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12437 of type TYPE from the given operands as constructed by build_call_array. */
12439 tree
12440 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12441 int nargs, tree *argarray)
12443 tree tem;
12444 #ifdef ENABLE_FOLD_CHECKING
12445 unsigned char checksum_before_fn[16],
12446 checksum_before_arglist[16],
12447 checksum_after_fn[16],
12448 checksum_after_arglist[16];
12449 struct md5_ctx ctx;
12450 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12451 int i;
12453 md5_init_ctx (&ctx);
12454 fold_checksum_tree (fn, &ctx, &ht);
12455 md5_finish_ctx (&ctx, checksum_before_fn);
12456 ht.empty ();
12458 md5_init_ctx (&ctx);
12459 for (i = 0; i < nargs; i++)
12460 fold_checksum_tree (argarray[i], &ctx, &ht);
12461 md5_finish_ctx (&ctx, checksum_before_arglist);
12462 ht.empty ();
12463 #endif
12465 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12466 if (!tem)
12467 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12469 #ifdef ENABLE_FOLD_CHECKING
12470 md5_init_ctx (&ctx);
12471 fold_checksum_tree (fn, &ctx, &ht);
12472 md5_finish_ctx (&ctx, checksum_after_fn);
12473 ht.empty ();
12475 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12476 fold_check_failed (fn, tem);
12478 md5_init_ctx (&ctx);
12479 for (i = 0; i < nargs; i++)
12480 fold_checksum_tree (argarray[i], &ctx, &ht);
12481 md5_finish_ctx (&ctx, checksum_after_arglist);
12483 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12484 fold_check_failed (NULL_TREE, tem);
12485 #endif
12486 return tem;
12489 /* Perform constant folding and related simplification of initializer
12490 expression EXPR. These behave identically to "fold_buildN" but ignore
12491 potential run-time traps and exceptions that fold must preserve. */
12493 #define START_FOLD_INIT \
12494 int saved_signaling_nans = flag_signaling_nans;\
12495 int saved_trapping_math = flag_trapping_math;\
12496 int saved_rounding_math = flag_rounding_math;\
12497 int saved_trapv = flag_trapv;\
12498 int saved_folding_initializer = folding_initializer;\
12499 flag_signaling_nans = 0;\
12500 flag_trapping_math = 0;\
12501 flag_rounding_math = 0;\
12502 flag_trapv = 0;\
12503 folding_initializer = 1;
12505 #define END_FOLD_INIT \
12506 flag_signaling_nans = saved_signaling_nans;\
12507 flag_trapping_math = saved_trapping_math;\
12508 flag_rounding_math = saved_rounding_math;\
12509 flag_trapv = saved_trapv;\
12510 folding_initializer = saved_folding_initializer;
12512 tree
12513 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12514 tree type, tree op)
12516 tree result;
12517 START_FOLD_INIT;
12519 result = fold_build1_loc (loc, code, type, op);
12521 END_FOLD_INIT;
12522 return result;
12525 tree
12526 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12527 tree type, tree op0, tree op1)
12529 tree result;
12530 START_FOLD_INIT;
12532 result = fold_build2_loc (loc, code, type, op0, op1);
12534 END_FOLD_INIT;
12535 return result;
12538 tree
12539 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12540 int nargs, tree *argarray)
12542 tree result;
12543 START_FOLD_INIT;
12545 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12547 END_FOLD_INIT;
12548 return result;
12551 #undef START_FOLD_INIT
12552 #undef END_FOLD_INIT
12554 /* Determine if first argument is a multiple of second argument. Return 0 if
12555 it is not, or we cannot easily determined it to be.
12557 An example of the sort of thing we care about (at this point; this routine
12558 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12559 fold cases do now) is discovering that
12561 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12563 is a multiple of
12565 SAVE_EXPR (J * 8)
12567 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12569 This code also handles discovering that
12571 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12573 is a multiple of 8 so we don't have to worry about dealing with a
12574 possible remainder.
12576 Note that we *look* inside a SAVE_EXPR only to determine how it was
12577 calculated; it is not safe for fold to do much of anything else with the
12578 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12579 at run time. For example, the latter example above *cannot* be implemented
12580 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12581 evaluation time of the original SAVE_EXPR is not necessarily the same at
12582 the time the new expression is evaluated. The only optimization of this
12583 sort that would be valid is changing
12585 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12587 divided by 8 to
12589 SAVE_EXPR (I) * SAVE_EXPR (J)
12591 (where the same SAVE_EXPR (J) is used in the original and the
12592 transformed version). */
12595 multiple_of_p (tree type, const_tree top, const_tree bottom)
12597 gimple *stmt;
12598 tree t1, op1, op2;
12600 if (operand_equal_p (top, bottom, 0))
12601 return 1;
12603 if (TREE_CODE (type) != INTEGER_TYPE)
12604 return 0;
12606 switch (TREE_CODE (top))
12608 case BIT_AND_EXPR:
12609 /* Bitwise and provides a power of two multiple. If the mask is
12610 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12611 if (!integer_pow2p (bottom))
12612 return 0;
12613 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12614 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12616 case MULT_EXPR:
12617 if (TREE_CODE (bottom) == INTEGER_CST)
12619 op1 = TREE_OPERAND (top, 0);
12620 op2 = TREE_OPERAND (top, 1);
12621 if (TREE_CODE (op1) == INTEGER_CST)
12622 std::swap (op1, op2);
12623 if (TREE_CODE (op2) == INTEGER_CST)
12625 if (multiple_of_p (type, op2, bottom))
12626 return 1;
12627 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
12628 if (multiple_of_p (type, bottom, op2))
12630 widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
12631 wi::to_widest (op2));
12632 if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
12634 op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
12635 return multiple_of_p (type, op1, op2);
12638 return multiple_of_p (type, op1, bottom);
12641 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12642 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12644 case MINUS_EXPR:
12645 /* It is impossible to prove if op0 - op1 is multiple of bottom
12646 precisely, so be conservative here checking if both op0 and op1
12647 are multiple of bottom. Note we check the second operand first
12648 since it's usually simpler. */
12649 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12650 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12652 case PLUS_EXPR:
12653 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12654 as op0 - 3 if the expression has unsigned type. For example,
12655 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12656 op1 = TREE_OPERAND (top, 1);
12657 if (TYPE_UNSIGNED (type)
12658 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12659 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12660 return (multiple_of_p (type, op1, bottom)
12661 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12663 case LSHIFT_EXPR:
12664 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12666 op1 = TREE_OPERAND (top, 1);
12667 /* const_binop may not detect overflow correctly,
12668 so check for it explicitly here. */
12669 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
12670 wi::to_wide (op1))
12671 && (t1 = fold_convert (type,
12672 const_binop (LSHIFT_EXPR, size_one_node,
12673 op1))) != 0
12674 && !TREE_OVERFLOW (t1))
12675 return multiple_of_p (type, t1, bottom);
12677 return 0;
12679 case NOP_EXPR:
12680 /* Can't handle conversions from non-integral or wider integral type. */
12681 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12682 || (TYPE_PRECISION (type)
12683 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12684 return 0;
12686 /* fall through */
12688 case SAVE_EXPR:
12689 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12691 case COND_EXPR:
12692 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12693 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12695 case INTEGER_CST:
12696 if (TREE_CODE (bottom) != INTEGER_CST
12697 || integer_zerop (bottom)
12698 || (TYPE_UNSIGNED (type)
12699 && (tree_int_cst_sgn (top) < 0
12700 || tree_int_cst_sgn (bottom) < 0)))
12701 return 0;
12702 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12703 SIGNED);
12705 case SSA_NAME:
12706 if (TREE_CODE (bottom) == INTEGER_CST
12707 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12708 && gimple_code (stmt) == GIMPLE_ASSIGN)
12710 enum tree_code code = gimple_assign_rhs_code (stmt);
12712 /* Check for special cases to see if top is defined as multiple
12713 of bottom:
12715 top = (X & ~(bottom - 1) ; bottom is power of 2
12719 Y = X % bottom
12720 top = X - Y. */
12721 if (code == BIT_AND_EXPR
12722 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12723 && TREE_CODE (op2) == INTEGER_CST
12724 && integer_pow2p (bottom)
12725 && wi::multiple_of_p (wi::to_widest (op2),
12726 wi::to_widest (bottom), UNSIGNED))
12727 return 1;
12729 op1 = gimple_assign_rhs1 (stmt);
12730 if (code == MINUS_EXPR
12731 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12732 && TREE_CODE (op2) == SSA_NAME
12733 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12734 && gimple_code (stmt) == GIMPLE_ASSIGN
12735 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12736 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12737 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12738 return 1;
12741 /* fall through */
12743 default:
12744 if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
12745 return multiple_p (wi::to_poly_widest (top),
12746 wi::to_poly_widest (bottom));
12748 return 0;
12752 #define tree_expr_nonnegative_warnv_p(X, Y) \
12753 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12755 #define RECURSE(X) \
12756 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12758 /* Return true if CODE or TYPE is known to be non-negative. */
12760 static bool
12761 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12763 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12764 && truth_value_p (code))
12765 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12766 have a signed:1 type (where the value is -1 and 0). */
12767 return true;
12768 return false;
12771 /* Return true if (CODE OP0) 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_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12778 bool *strict_overflow_p, int depth)
12780 if (TYPE_UNSIGNED (type))
12781 return true;
12783 switch (code)
12785 case ABS_EXPR:
12786 /* We can't return 1 if flag_wrapv is set because
12787 ABS_EXPR<INT_MIN> = INT_MIN. */
12788 if (!ANY_INTEGRAL_TYPE_P (type))
12789 return true;
12790 if (TYPE_OVERFLOW_UNDEFINED (type))
12792 *strict_overflow_p = true;
12793 return true;
12795 break;
12797 case NON_LVALUE_EXPR:
12798 case FLOAT_EXPR:
12799 case FIX_TRUNC_EXPR:
12800 return RECURSE (op0);
12802 CASE_CONVERT:
12804 tree inner_type = TREE_TYPE (op0);
12805 tree outer_type = type;
12807 if (TREE_CODE (outer_type) == REAL_TYPE)
12809 if (TREE_CODE (inner_type) == REAL_TYPE)
12810 return RECURSE (op0);
12811 if (INTEGRAL_TYPE_P (inner_type))
12813 if (TYPE_UNSIGNED (inner_type))
12814 return true;
12815 return RECURSE (op0);
12818 else if (INTEGRAL_TYPE_P (outer_type))
12820 if (TREE_CODE (inner_type) == REAL_TYPE)
12821 return RECURSE (op0);
12822 if (INTEGRAL_TYPE_P (inner_type))
12823 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12824 && TYPE_UNSIGNED (inner_type);
12827 break;
12829 default:
12830 return tree_simple_nonnegative_warnv_p (code, type);
12833 /* We don't know sign of `t', so be conservative and return false. */
12834 return false;
12837 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12838 value is based on the assumption that signed overflow is undefined,
12839 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12840 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12842 bool
12843 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12844 tree op1, bool *strict_overflow_p,
12845 int depth)
12847 if (TYPE_UNSIGNED (type))
12848 return true;
12850 switch (code)
12852 case POINTER_PLUS_EXPR:
12853 case PLUS_EXPR:
12854 if (FLOAT_TYPE_P (type))
12855 return RECURSE (op0) && RECURSE (op1);
12857 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12858 both unsigned and at least 2 bits shorter than the result. */
12859 if (TREE_CODE (type) == INTEGER_TYPE
12860 && TREE_CODE (op0) == NOP_EXPR
12861 && TREE_CODE (op1) == NOP_EXPR)
12863 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12864 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12865 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12866 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12868 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12869 TYPE_PRECISION (inner2)) + 1;
12870 return prec < TYPE_PRECISION (type);
12873 break;
12875 case MULT_EXPR:
12876 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12878 /* x * x is always non-negative for floating point x
12879 or without overflow. */
12880 if (operand_equal_p (op0, op1, 0)
12881 || (RECURSE (op0) && RECURSE (op1)))
12883 if (ANY_INTEGRAL_TYPE_P (type)
12884 && TYPE_OVERFLOW_UNDEFINED (type))
12885 *strict_overflow_p = true;
12886 return true;
12890 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12891 both unsigned and their total bits is shorter than the result. */
12892 if (TREE_CODE (type) == INTEGER_TYPE
12893 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12894 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12896 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12897 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12898 : TREE_TYPE (op0);
12899 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12900 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12901 : TREE_TYPE (op1);
12903 bool unsigned0 = TYPE_UNSIGNED (inner0);
12904 bool unsigned1 = TYPE_UNSIGNED (inner1);
12906 if (TREE_CODE (op0) == INTEGER_CST)
12907 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12909 if (TREE_CODE (op1) == INTEGER_CST)
12910 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12912 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12913 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12915 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12916 ? tree_int_cst_min_precision (op0, UNSIGNED)
12917 : TYPE_PRECISION (inner0);
12919 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12920 ? tree_int_cst_min_precision (op1, UNSIGNED)
12921 : TYPE_PRECISION (inner1);
12923 return precision0 + precision1 < TYPE_PRECISION (type);
12926 return false;
12928 case BIT_AND_EXPR:
12929 case MAX_EXPR:
12930 return RECURSE (op0) || RECURSE (op1);
12932 case BIT_IOR_EXPR:
12933 case BIT_XOR_EXPR:
12934 case MIN_EXPR:
12935 case RDIV_EXPR:
12936 case TRUNC_DIV_EXPR:
12937 case CEIL_DIV_EXPR:
12938 case FLOOR_DIV_EXPR:
12939 case ROUND_DIV_EXPR:
12940 return RECURSE (op0) && RECURSE (op1);
12942 case TRUNC_MOD_EXPR:
12943 return RECURSE (op0);
12945 case FLOOR_MOD_EXPR:
12946 return RECURSE (op1);
12948 case CEIL_MOD_EXPR:
12949 case ROUND_MOD_EXPR:
12950 default:
12951 return tree_simple_nonnegative_warnv_p (code, type);
12954 /* We don't know sign of `t', so be conservative and return false. */
12955 return false;
12958 /* Return true if T is known to be non-negative. If the return
12959 value is based on the assumption that signed overflow is undefined,
12960 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12961 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12963 bool
12964 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
12966 if (TYPE_UNSIGNED (TREE_TYPE (t)))
12967 return true;
12969 switch (TREE_CODE (t))
12971 case INTEGER_CST:
12972 return tree_int_cst_sgn (t) >= 0;
12974 case REAL_CST:
12975 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
12977 case FIXED_CST:
12978 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
12980 case COND_EXPR:
12981 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
12983 case SSA_NAME:
12984 /* Limit the depth of recursion to avoid quadratic behavior.
12985 This is expected to catch almost all occurrences in practice.
12986 If this code misses important cases that unbounded recursion
12987 would not, passes that need this information could be revised
12988 to provide it through dataflow propagation. */
12989 return (!name_registered_for_update_p (t)
12990 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
12991 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
12992 strict_overflow_p, depth));
12994 default:
12995 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
12999 /* Return true if T is known to be non-negative. If the return
13000 value is based on the assumption that signed overflow is undefined,
13001 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13002 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13004 bool
13005 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
13006 bool *strict_overflow_p, int depth)
13008 switch (fn)
13010 CASE_CFN_ACOS:
13011 CASE_CFN_ACOSH:
13012 CASE_CFN_CABS:
13013 CASE_CFN_COSH:
13014 CASE_CFN_ERFC:
13015 CASE_CFN_EXP:
13016 CASE_CFN_EXP10:
13017 CASE_CFN_EXP2:
13018 CASE_CFN_FABS:
13019 CASE_CFN_FDIM:
13020 CASE_CFN_HYPOT:
13021 CASE_CFN_POW10:
13022 CASE_CFN_FFS:
13023 CASE_CFN_PARITY:
13024 CASE_CFN_POPCOUNT:
13025 CASE_CFN_CLZ:
13026 CASE_CFN_CLRSB:
13027 case CFN_BUILT_IN_BSWAP32:
13028 case CFN_BUILT_IN_BSWAP64:
13029 /* Always true. */
13030 return true;
13032 CASE_CFN_SQRT:
13033 CASE_CFN_SQRT_FN:
13034 /* sqrt(-0.0) is -0.0. */
13035 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13036 return true;
13037 return RECURSE (arg0);
13039 CASE_CFN_ASINH:
13040 CASE_CFN_ATAN:
13041 CASE_CFN_ATANH:
13042 CASE_CFN_CBRT:
13043 CASE_CFN_CEIL:
13044 CASE_CFN_CEIL_FN:
13045 CASE_CFN_ERF:
13046 CASE_CFN_EXPM1:
13047 CASE_CFN_FLOOR:
13048 CASE_CFN_FLOOR_FN:
13049 CASE_CFN_FMOD:
13050 CASE_CFN_FREXP:
13051 CASE_CFN_ICEIL:
13052 CASE_CFN_IFLOOR:
13053 CASE_CFN_IRINT:
13054 CASE_CFN_IROUND:
13055 CASE_CFN_LCEIL:
13056 CASE_CFN_LDEXP:
13057 CASE_CFN_LFLOOR:
13058 CASE_CFN_LLCEIL:
13059 CASE_CFN_LLFLOOR:
13060 CASE_CFN_LLRINT:
13061 CASE_CFN_LLROUND:
13062 CASE_CFN_LRINT:
13063 CASE_CFN_LROUND:
13064 CASE_CFN_MODF:
13065 CASE_CFN_NEARBYINT:
13066 CASE_CFN_NEARBYINT_FN:
13067 CASE_CFN_RINT:
13068 CASE_CFN_RINT_FN:
13069 CASE_CFN_ROUND:
13070 CASE_CFN_ROUND_FN:
13071 CASE_CFN_SCALB:
13072 CASE_CFN_SCALBLN:
13073 CASE_CFN_SCALBN:
13074 CASE_CFN_SIGNBIT:
13075 CASE_CFN_SIGNIFICAND:
13076 CASE_CFN_SINH:
13077 CASE_CFN_TANH:
13078 CASE_CFN_TRUNC:
13079 CASE_CFN_TRUNC_FN:
13080 /* True if the 1st argument is nonnegative. */
13081 return RECURSE (arg0);
13083 CASE_CFN_FMAX:
13084 CASE_CFN_FMAX_FN:
13085 /* True if the 1st OR 2nd arguments are nonnegative. */
13086 return RECURSE (arg0) || RECURSE (arg1);
13088 CASE_CFN_FMIN:
13089 CASE_CFN_FMIN_FN:
13090 /* True if the 1st AND 2nd arguments are nonnegative. */
13091 return RECURSE (arg0) && RECURSE (arg1);
13093 CASE_CFN_COPYSIGN:
13094 CASE_CFN_COPYSIGN_FN:
13095 /* True if the 2nd argument is nonnegative. */
13096 return RECURSE (arg1);
13098 CASE_CFN_POWI:
13099 /* True if the 1st argument is nonnegative or the second
13100 argument is an even integer. */
13101 if (TREE_CODE (arg1) == INTEGER_CST
13102 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13103 return true;
13104 return RECURSE (arg0);
13106 CASE_CFN_POW:
13107 /* True if the 1st argument is nonnegative or the second
13108 argument is an even integer valued real. */
13109 if (TREE_CODE (arg1) == REAL_CST)
13111 REAL_VALUE_TYPE c;
13112 HOST_WIDE_INT n;
13114 c = TREE_REAL_CST (arg1);
13115 n = real_to_integer (&c);
13116 if ((n & 1) == 0)
13118 REAL_VALUE_TYPE cint;
13119 real_from_integer (&cint, VOIDmode, n, SIGNED);
13120 if (real_identical (&c, &cint))
13121 return true;
13124 return RECURSE (arg0);
13126 default:
13127 break;
13129 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13132 /* Return true if T is known to be non-negative. If the return
13133 value is based on the assumption that signed overflow is undefined,
13134 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13135 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13137 static bool
13138 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13140 enum tree_code code = TREE_CODE (t);
13141 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13142 return true;
13144 switch (code)
13146 case TARGET_EXPR:
13148 tree temp = TARGET_EXPR_SLOT (t);
13149 t = TARGET_EXPR_INITIAL (t);
13151 /* If the initializer is non-void, then it's a normal expression
13152 that will be assigned to the slot. */
13153 if (!VOID_TYPE_P (t))
13154 return RECURSE (t);
13156 /* Otherwise, the initializer sets the slot in some way. One common
13157 way is an assignment statement at the end of the initializer. */
13158 while (1)
13160 if (TREE_CODE (t) == BIND_EXPR)
13161 t = expr_last (BIND_EXPR_BODY (t));
13162 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13163 || TREE_CODE (t) == TRY_CATCH_EXPR)
13164 t = expr_last (TREE_OPERAND (t, 0));
13165 else if (TREE_CODE (t) == STATEMENT_LIST)
13166 t = expr_last (t);
13167 else
13168 break;
13170 if (TREE_CODE (t) == MODIFY_EXPR
13171 && TREE_OPERAND (t, 0) == temp)
13172 return RECURSE (TREE_OPERAND (t, 1));
13174 return false;
13177 case CALL_EXPR:
13179 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13180 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13182 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13183 get_call_combined_fn (t),
13184 arg0,
13185 arg1,
13186 strict_overflow_p, depth);
13188 case COMPOUND_EXPR:
13189 case MODIFY_EXPR:
13190 return RECURSE (TREE_OPERAND (t, 1));
13192 case BIND_EXPR:
13193 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13195 case SAVE_EXPR:
13196 return RECURSE (TREE_OPERAND (t, 0));
13198 default:
13199 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13203 #undef RECURSE
13204 #undef tree_expr_nonnegative_warnv_p
13206 /* Return true if T is known to be non-negative. If the return
13207 value is based on the assumption that signed overflow is undefined,
13208 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13209 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13211 bool
13212 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13214 enum tree_code code;
13215 if (t == error_mark_node)
13216 return false;
13218 code = TREE_CODE (t);
13219 switch (TREE_CODE_CLASS (code))
13221 case tcc_binary:
13222 case tcc_comparison:
13223 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13224 TREE_TYPE (t),
13225 TREE_OPERAND (t, 0),
13226 TREE_OPERAND (t, 1),
13227 strict_overflow_p, depth);
13229 case tcc_unary:
13230 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13231 TREE_TYPE (t),
13232 TREE_OPERAND (t, 0),
13233 strict_overflow_p, depth);
13235 case tcc_constant:
13236 case tcc_declaration:
13237 case tcc_reference:
13238 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13240 default:
13241 break;
13244 switch (code)
13246 case TRUTH_AND_EXPR:
13247 case TRUTH_OR_EXPR:
13248 case TRUTH_XOR_EXPR:
13249 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13250 TREE_TYPE (t),
13251 TREE_OPERAND (t, 0),
13252 TREE_OPERAND (t, 1),
13253 strict_overflow_p, depth);
13254 case TRUTH_NOT_EXPR:
13255 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13256 TREE_TYPE (t),
13257 TREE_OPERAND (t, 0),
13258 strict_overflow_p, depth);
13260 case COND_EXPR:
13261 case CONSTRUCTOR:
13262 case OBJ_TYPE_REF:
13263 case ASSERT_EXPR:
13264 case ADDR_EXPR:
13265 case WITH_SIZE_EXPR:
13266 case SSA_NAME:
13267 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13269 default:
13270 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13274 /* Return true if `t' is known to be non-negative. Handle warnings
13275 about undefined signed overflow. */
13277 bool
13278 tree_expr_nonnegative_p (tree t)
13280 bool ret, strict_overflow_p;
13282 strict_overflow_p = false;
13283 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13284 if (strict_overflow_p)
13285 fold_overflow_warning (("assuming signed overflow does not occur when "
13286 "determining that expression is always "
13287 "non-negative"),
13288 WARN_STRICT_OVERFLOW_MISC);
13289 return ret;
13293 /* Return true when (CODE OP0) is an address and is known to be nonzero.
13294 For floating point we further ensure that T is not denormal.
13295 Similar logic is present in nonzero_address in rtlanal.h.
13297 If the return value is based on the assumption that signed overflow
13298 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13299 change *STRICT_OVERFLOW_P. */
13301 bool
13302 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13303 bool *strict_overflow_p)
13305 switch (code)
13307 case ABS_EXPR:
13308 return tree_expr_nonzero_warnv_p (op0,
13309 strict_overflow_p);
13311 case NOP_EXPR:
13313 tree inner_type = TREE_TYPE (op0);
13314 tree outer_type = type;
13316 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13317 && tree_expr_nonzero_warnv_p (op0,
13318 strict_overflow_p));
13320 break;
13322 case NON_LVALUE_EXPR:
13323 return tree_expr_nonzero_warnv_p (op0,
13324 strict_overflow_p);
13326 default:
13327 break;
13330 return false;
13333 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13334 For floating point we further ensure that T is not denormal.
13335 Similar logic is present in nonzero_address in rtlanal.h.
13337 If the return value is based on the assumption that signed overflow
13338 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13339 change *STRICT_OVERFLOW_P. */
13341 bool
13342 tree_binary_nonzero_warnv_p (enum tree_code code,
13343 tree type,
13344 tree op0,
13345 tree op1, bool *strict_overflow_p)
13347 bool sub_strict_overflow_p;
13348 switch (code)
13350 case POINTER_PLUS_EXPR:
13351 case PLUS_EXPR:
13352 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13354 /* With the presence of negative values it is hard
13355 to say something. */
13356 sub_strict_overflow_p = false;
13357 if (!tree_expr_nonnegative_warnv_p (op0,
13358 &sub_strict_overflow_p)
13359 || !tree_expr_nonnegative_warnv_p (op1,
13360 &sub_strict_overflow_p))
13361 return false;
13362 /* One of operands must be positive and the other non-negative. */
13363 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13364 overflows, on a twos-complement machine the sum of two
13365 nonnegative numbers can never be zero. */
13366 return (tree_expr_nonzero_warnv_p (op0,
13367 strict_overflow_p)
13368 || tree_expr_nonzero_warnv_p (op1,
13369 strict_overflow_p));
13371 break;
13373 case MULT_EXPR:
13374 if (TYPE_OVERFLOW_UNDEFINED (type))
13376 if (tree_expr_nonzero_warnv_p (op0,
13377 strict_overflow_p)
13378 && tree_expr_nonzero_warnv_p (op1,
13379 strict_overflow_p))
13381 *strict_overflow_p = true;
13382 return true;
13385 break;
13387 case MIN_EXPR:
13388 sub_strict_overflow_p = false;
13389 if (tree_expr_nonzero_warnv_p (op0,
13390 &sub_strict_overflow_p)
13391 && tree_expr_nonzero_warnv_p (op1,
13392 &sub_strict_overflow_p))
13394 if (sub_strict_overflow_p)
13395 *strict_overflow_p = true;
13397 break;
13399 case MAX_EXPR:
13400 sub_strict_overflow_p = false;
13401 if (tree_expr_nonzero_warnv_p (op0,
13402 &sub_strict_overflow_p))
13404 if (sub_strict_overflow_p)
13405 *strict_overflow_p = true;
13407 /* When both operands are nonzero, then MAX must be too. */
13408 if (tree_expr_nonzero_warnv_p (op1,
13409 strict_overflow_p))
13410 return true;
13412 /* MAX where operand 0 is positive is positive. */
13413 return tree_expr_nonnegative_warnv_p (op0,
13414 strict_overflow_p);
13416 /* MAX where operand 1 is positive is positive. */
13417 else if (tree_expr_nonzero_warnv_p (op1,
13418 &sub_strict_overflow_p)
13419 && tree_expr_nonnegative_warnv_p (op1,
13420 &sub_strict_overflow_p))
13422 if (sub_strict_overflow_p)
13423 *strict_overflow_p = true;
13424 return true;
13426 break;
13428 case BIT_IOR_EXPR:
13429 return (tree_expr_nonzero_warnv_p (op1,
13430 strict_overflow_p)
13431 || tree_expr_nonzero_warnv_p (op0,
13432 strict_overflow_p));
13434 default:
13435 break;
13438 return false;
13441 /* Return true when T is an address and is known to be nonzero.
13442 For floating point we further ensure that T is not denormal.
13443 Similar logic is present in nonzero_address in rtlanal.h.
13445 If the return value is based on the assumption that signed overflow
13446 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13447 change *STRICT_OVERFLOW_P. */
13449 bool
13450 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13452 bool sub_strict_overflow_p;
13453 switch (TREE_CODE (t))
13455 case INTEGER_CST:
13456 return !integer_zerop (t);
13458 case ADDR_EXPR:
13460 tree base = TREE_OPERAND (t, 0);
13462 if (!DECL_P (base))
13463 base = get_base_address (base);
13465 if (base && TREE_CODE (base) == TARGET_EXPR)
13466 base = TARGET_EXPR_SLOT (base);
13468 if (!base)
13469 return false;
13471 /* For objects in symbol table check if we know they are non-zero.
13472 Don't do anything for variables and functions before symtab is built;
13473 it is quite possible that they will be declared weak later. */
13474 int nonzero_addr = maybe_nonzero_address (base);
13475 if (nonzero_addr >= 0)
13476 return nonzero_addr;
13478 /* Constants are never weak. */
13479 if (CONSTANT_CLASS_P (base))
13480 return true;
13482 return false;
13485 case COND_EXPR:
13486 sub_strict_overflow_p = false;
13487 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13488 &sub_strict_overflow_p)
13489 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13490 &sub_strict_overflow_p))
13492 if (sub_strict_overflow_p)
13493 *strict_overflow_p = true;
13494 return true;
13496 break;
13498 case SSA_NAME:
13499 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13500 break;
13501 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13503 default:
13504 break;
13506 return false;
13509 #define integer_valued_real_p(X) \
13510 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13512 #define RECURSE(X) \
13513 ((integer_valued_real_p) (X, depth + 1))
13515 /* Return true if the floating point result of (CODE OP0) has an
13516 integer value. We also allow +Inf, -Inf and NaN to be considered
13517 integer values. Return false for signaling NaN.
13519 DEPTH is the current nesting depth of the query. */
13521 bool
13522 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13524 switch (code)
13526 case FLOAT_EXPR:
13527 return true;
13529 case ABS_EXPR:
13530 return RECURSE (op0);
13532 CASE_CONVERT:
13534 tree type = TREE_TYPE (op0);
13535 if (TREE_CODE (type) == INTEGER_TYPE)
13536 return true;
13537 if (TREE_CODE (type) == REAL_TYPE)
13538 return RECURSE (op0);
13539 break;
13542 default:
13543 break;
13545 return false;
13548 /* Return true if the floating point result of (CODE OP0 OP1) has an
13549 integer value. We also allow +Inf, -Inf and NaN to be considered
13550 integer values. Return false for signaling NaN.
13552 DEPTH is the current nesting depth of the query. */
13554 bool
13555 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13557 switch (code)
13559 case PLUS_EXPR:
13560 case MINUS_EXPR:
13561 case MULT_EXPR:
13562 case MIN_EXPR:
13563 case MAX_EXPR:
13564 return RECURSE (op0) && RECURSE (op1);
13566 default:
13567 break;
13569 return false;
13572 /* Return true if the floating point result of calling FNDECL with arguments
13573 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13574 considered integer values. Return false for signaling NaN. If FNDECL
13575 takes fewer than 2 arguments, the remaining ARGn are null.
13577 DEPTH is the current nesting depth of the query. */
13579 bool
13580 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13582 switch (fn)
13584 CASE_CFN_CEIL:
13585 CASE_CFN_CEIL_FN:
13586 CASE_CFN_FLOOR:
13587 CASE_CFN_FLOOR_FN:
13588 CASE_CFN_NEARBYINT:
13589 CASE_CFN_NEARBYINT_FN:
13590 CASE_CFN_RINT:
13591 CASE_CFN_RINT_FN:
13592 CASE_CFN_ROUND:
13593 CASE_CFN_ROUND_FN:
13594 CASE_CFN_TRUNC:
13595 CASE_CFN_TRUNC_FN:
13596 return true;
13598 CASE_CFN_FMIN:
13599 CASE_CFN_FMIN_FN:
13600 CASE_CFN_FMAX:
13601 CASE_CFN_FMAX_FN:
13602 return RECURSE (arg0) && RECURSE (arg1);
13604 default:
13605 break;
13607 return false;
13610 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13611 has an integer value. We also allow +Inf, -Inf and NaN to be
13612 considered integer values. Return false for signaling NaN.
13614 DEPTH is the current nesting depth of the query. */
13616 bool
13617 integer_valued_real_single_p (tree t, int depth)
13619 switch (TREE_CODE (t))
13621 case REAL_CST:
13622 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13624 case COND_EXPR:
13625 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13627 case SSA_NAME:
13628 /* Limit the depth of recursion to avoid quadratic behavior.
13629 This is expected to catch almost all occurrences in practice.
13630 If this code misses important cases that unbounded recursion
13631 would not, passes that need this information could be revised
13632 to provide it through dataflow propagation. */
13633 return (!name_registered_for_update_p (t)
13634 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13635 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13636 depth));
13638 default:
13639 break;
13641 return false;
13644 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13645 has an integer value. We also allow +Inf, -Inf and NaN to be
13646 considered integer values. Return false for signaling NaN.
13648 DEPTH is the current nesting depth of the query. */
13650 static bool
13651 integer_valued_real_invalid_p (tree t, int depth)
13653 switch (TREE_CODE (t))
13655 case COMPOUND_EXPR:
13656 case MODIFY_EXPR:
13657 case BIND_EXPR:
13658 return RECURSE (TREE_OPERAND (t, 1));
13660 case SAVE_EXPR:
13661 return RECURSE (TREE_OPERAND (t, 0));
13663 default:
13664 break;
13666 return false;
13669 #undef RECURSE
13670 #undef integer_valued_real_p
13672 /* Return true if the floating point expression T has an integer value.
13673 We also allow +Inf, -Inf and NaN to be considered integer values.
13674 Return false for signaling NaN.
13676 DEPTH is the current nesting depth of the query. */
13678 bool
13679 integer_valued_real_p (tree t, int depth)
13681 if (t == error_mark_node)
13682 return false;
13684 tree_code code = TREE_CODE (t);
13685 switch (TREE_CODE_CLASS (code))
13687 case tcc_binary:
13688 case tcc_comparison:
13689 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13690 TREE_OPERAND (t, 1), depth);
13692 case tcc_unary:
13693 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13695 case tcc_constant:
13696 case tcc_declaration:
13697 case tcc_reference:
13698 return integer_valued_real_single_p (t, depth);
13700 default:
13701 break;
13704 switch (code)
13706 case COND_EXPR:
13707 case SSA_NAME:
13708 return integer_valued_real_single_p (t, depth);
13710 case CALL_EXPR:
13712 tree arg0 = (call_expr_nargs (t) > 0
13713 ? CALL_EXPR_ARG (t, 0)
13714 : NULL_TREE);
13715 tree arg1 = (call_expr_nargs (t) > 1
13716 ? CALL_EXPR_ARG (t, 1)
13717 : NULL_TREE);
13718 return integer_valued_real_call_p (get_call_combined_fn (t),
13719 arg0, arg1, depth);
13722 default:
13723 return integer_valued_real_invalid_p (t, depth);
13727 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13728 attempt to fold the expression to a constant without modifying TYPE,
13729 OP0 or OP1.
13731 If the expression could be simplified to a constant, then return
13732 the constant. If the expression would not be simplified to a
13733 constant, then return NULL_TREE. */
13735 tree
13736 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13738 tree tem = fold_binary (code, type, op0, op1);
13739 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13742 /* Given the components of a unary expression CODE, TYPE and OP0,
13743 attempt to fold the expression to a constant without modifying
13744 TYPE or OP0.
13746 If the expression could be simplified to a constant, then return
13747 the constant. If the expression would not be simplified to a
13748 constant, then return NULL_TREE. */
13750 tree
13751 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13753 tree tem = fold_unary (code, type, op0);
13754 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13757 /* If EXP represents referencing an element in a constant string
13758 (either via pointer arithmetic or array indexing), return the
13759 tree representing the value accessed, otherwise return NULL. */
13761 tree
13762 fold_read_from_constant_string (tree exp)
13764 if ((TREE_CODE (exp) == INDIRECT_REF
13765 || TREE_CODE (exp) == ARRAY_REF)
13766 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13768 tree exp1 = TREE_OPERAND (exp, 0);
13769 tree index;
13770 tree string;
13771 location_t loc = EXPR_LOCATION (exp);
13773 if (TREE_CODE (exp) == INDIRECT_REF)
13774 string = string_constant (exp1, &index);
13775 else
13777 tree low_bound = array_ref_low_bound (exp);
13778 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13780 /* Optimize the special-case of a zero lower bound.
13782 We convert the low_bound to sizetype to avoid some problems
13783 with constant folding. (E.g. suppose the lower bound is 1,
13784 and its mode is QI. Without the conversion,l (ARRAY
13785 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13786 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13787 if (! integer_zerop (low_bound))
13788 index = size_diffop_loc (loc, index,
13789 fold_convert_loc (loc, sizetype, low_bound));
13791 string = exp1;
13794 scalar_int_mode char_mode;
13795 if (string
13796 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13797 && TREE_CODE (string) == STRING_CST
13798 && TREE_CODE (index) == INTEGER_CST
13799 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13800 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13801 &char_mode)
13802 && GET_MODE_SIZE (char_mode) == 1)
13803 return build_int_cst_type (TREE_TYPE (exp),
13804 (TREE_STRING_POINTER (string)
13805 [TREE_INT_CST_LOW (index)]));
13807 return NULL;
13810 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13811 an integer constant, real, or fixed-point constant.
13813 TYPE is the type of the result. */
13815 static tree
13816 fold_negate_const (tree arg0, tree type)
13818 tree t = NULL_TREE;
13820 switch (TREE_CODE (arg0))
13822 case REAL_CST:
13823 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13824 break;
13826 case FIXED_CST:
13828 FIXED_VALUE_TYPE f;
13829 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13830 &(TREE_FIXED_CST (arg0)), NULL,
13831 TYPE_SATURATING (type));
13832 t = build_fixed (type, f);
13833 /* Propagate overflow flags. */
13834 if (overflow_p | TREE_OVERFLOW (arg0))
13835 TREE_OVERFLOW (t) = 1;
13836 break;
13839 default:
13840 if (poly_int_tree_p (arg0))
13842 bool overflow;
13843 poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
13844 t = force_fit_type (type, res, 1,
13845 (overflow && ! TYPE_UNSIGNED (type))
13846 || TREE_OVERFLOW (arg0));
13847 break;
13850 gcc_unreachable ();
13853 return t;
13856 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13857 an integer constant or real constant.
13859 TYPE is the type of the result. */
13861 tree
13862 fold_abs_const (tree arg0, tree type)
13864 tree t = NULL_TREE;
13866 switch (TREE_CODE (arg0))
13868 case INTEGER_CST:
13870 /* If the value is unsigned or non-negative, then the absolute value
13871 is the same as the ordinary value. */
13872 wide_int val = wi::to_wide (arg0);
13873 bool overflow = false;
13874 if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
13877 /* If the value is negative, then the absolute value is
13878 its negation. */
13879 else
13880 val = wi::neg (val, &overflow);
13882 /* Force to the destination type, set TREE_OVERFLOW for signed
13883 TYPE only. */
13884 t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
13886 break;
13888 case REAL_CST:
13889 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13890 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13891 else
13892 t = arg0;
13893 break;
13895 default:
13896 gcc_unreachable ();
13899 return t;
13902 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13903 constant. TYPE is the type of the result. */
13905 static tree
13906 fold_not_const (const_tree arg0, tree type)
13908 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13910 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
13913 /* Given CODE, a relational operator, the target type, TYPE and two
13914 constant operands OP0 and OP1, return the result of the
13915 relational operation. If the result is not a compile time
13916 constant, then return NULL_TREE. */
13918 static tree
13919 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13921 int result, invert;
13923 /* From here on, the only cases we handle are when the result is
13924 known to be a constant. */
13926 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13928 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13929 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13931 /* Handle the cases where either operand is a NaN. */
13932 if (real_isnan (c0) || real_isnan (c1))
13934 switch (code)
13936 case EQ_EXPR:
13937 case ORDERED_EXPR:
13938 result = 0;
13939 break;
13941 case NE_EXPR:
13942 case UNORDERED_EXPR:
13943 case UNLT_EXPR:
13944 case UNLE_EXPR:
13945 case UNGT_EXPR:
13946 case UNGE_EXPR:
13947 case UNEQ_EXPR:
13948 result = 1;
13949 break;
13951 case LT_EXPR:
13952 case LE_EXPR:
13953 case GT_EXPR:
13954 case GE_EXPR:
13955 case LTGT_EXPR:
13956 if (flag_trapping_math)
13957 return NULL_TREE;
13958 result = 0;
13959 break;
13961 default:
13962 gcc_unreachable ();
13965 return constant_boolean_node (result, type);
13968 return constant_boolean_node (real_compare (code, c0, c1), type);
13971 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
13973 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
13974 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
13975 return constant_boolean_node (fixed_compare (code, c0, c1), type);
13978 /* Handle equality/inequality of complex constants. */
13979 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
13981 tree rcond = fold_relational_const (code, type,
13982 TREE_REALPART (op0),
13983 TREE_REALPART (op1));
13984 tree icond = fold_relational_const (code, type,
13985 TREE_IMAGPART (op0),
13986 TREE_IMAGPART (op1));
13987 if (code == EQ_EXPR)
13988 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
13989 else if (code == NE_EXPR)
13990 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
13991 else
13992 return NULL_TREE;
13995 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
13997 if (!VECTOR_TYPE_P (type))
13999 /* Have vector comparison with scalar boolean result. */
14000 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
14001 && known_eq (VECTOR_CST_NELTS (op0),
14002 VECTOR_CST_NELTS (op1)));
14003 unsigned HOST_WIDE_INT nunits;
14004 if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
14005 return NULL_TREE;
14006 for (unsigned i = 0; i < nunits; i++)
14008 tree elem0 = VECTOR_CST_ELT (op0, i);
14009 tree elem1 = VECTOR_CST_ELT (op1, i);
14010 tree tmp = fold_relational_const (code, type, elem0, elem1);
14011 if (tmp == NULL_TREE)
14012 return NULL_TREE;
14013 if (integer_zerop (tmp))
14014 return constant_boolean_node (false, type);
14016 return constant_boolean_node (true, type);
14018 tree_vector_builder elts;
14019 if (!elts.new_binary_operation (type, op0, op1, false))
14020 return NULL_TREE;
14021 unsigned int count = elts.encoded_nelts ();
14022 for (unsigned i = 0; i < count; i++)
14024 tree elem_type = TREE_TYPE (type);
14025 tree elem0 = VECTOR_CST_ELT (op0, i);
14026 tree elem1 = VECTOR_CST_ELT (op1, i);
14028 tree tem = fold_relational_const (code, elem_type,
14029 elem0, elem1);
14031 if (tem == NULL_TREE)
14032 return NULL_TREE;
14034 elts.quick_push (build_int_cst (elem_type,
14035 integer_zerop (tem) ? 0 : -1));
14038 return elts.build ();
14041 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14043 To compute GT, swap the arguments and do LT.
14044 To compute GE, do LT and invert the result.
14045 To compute LE, swap the arguments, do LT and invert the result.
14046 To compute NE, do EQ and invert the result.
14048 Therefore, the code below must handle only EQ and LT. */
14050 if (code == LE_EXPR || code == GT_EXPR)
14052 std::swap (op0, op1);
14053 code = swap_tree_comparison (code);
14056 /* Note that it is safe to invert for real values here because we
14057 have already handled the one case that it matters. */
14059 invert = 0;
14060 if (code == NE_EXPR || code == GE_EXPR)
14062 invert = 1;
14063 code = invert_tree_comparison (code, false);
14066 /* Compute a result for LT or EQ if args permit;
14067 Otherwise return T. */
14068 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14070 if (code == EQ_EXPR)
14071 result = tree_int_cst_equal (op0, op1);
14072 else
14073 result = tree_int_cst_lt (op0, op1);
14075 else
14076 return NULL_TREE;
14078 if (invert)
14079 result ^= 1;
14080 return constant_boolean_node (result, type);
14083 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14084 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14085 itself. */
14087 tree
14088 fold_build_cleanup_point_expr (tree type, tree expr)
14090 /* If the expression does not have side effects then we don't have to wrap
14091 it with a cleanup point expression. */
14092 if (!TREE_SIDE_EFFECTS (expr))
14093 return expr;
14095 /* If the expression is a return, check to see if the expression inside the
14096 return has no side effects or the right hand side of the modify expression
14097 inside the return. If either don't have side effects set we don't need to
14098 wrap the expression in a cleanup point expression. Note we don't check the
14099 left hand side of the modify because it should always be a return decl. */
14100 if (TREE_CODE (expr) == RETURN_EXPR)
14102 tree op = TREE_OPERAND (expr, 0);
14103 if (!op || !TREE_SIDE_EFFECTS (op))
14104 return expr;
14105 op = TREE_OPERAND (op, 1);
14106 if (!TREE_SIDE_EFFECTS (op))
14107 return expr;
14110 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
14113 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14114 of an indirection through OP0, or NULL_TREE if no simplification is
14115 possible. */
14117 tree
14118 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14120 tree sub = op0;
14121 tree subtype;
14122 poly_uint64 const_op01;
14124 STRIP_NOPS (sub);
14125 subtype = TREE_TYPE (sub);
14126 if (!POINTER_TYPE_P (subtype)
14127 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
14128 return NULL_TREE;
14130 if (TREE_CODE (sub) == ADDR_EXPR)
14132 tree op = TREE_OPERAND (sub, 0);
14133 tree optype = TREE_TYPE (op);
14135 /* *&CONST_DECL -> to the value of the const decl. */
14136 if (TREE_CODE (op) == CONST_DECL)
14137 return DECL_INITIAL (op);
14138 /* *&p => p; make sure to handle *&"str"[cst] here. */
14139 if (type == optype)
14141 tree fop = fold_read_from_constant_string (op);
14142 if (fop)
14143 return fop;
14144 else
14145 return op;
14147 /* *(foo *)&fooarray => fooarray[0] */
14148 else if (TREE_CODE (optype) == ARRAY_TYPE
14149 && type == TREE_TYPE (optype)
14150 && (!in_gimple_form
14151 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14153 tree type_domain = TYPE_DOMAIN (optype);
14154 tree min_val = size_zero_node;
14155 if (type_domain && TYPE_MIN_VALUE (type_domain))
14156 min_val = TYPE_MIN_VALUE (type_domain);
14157 if (in_gimple_form
14158 && TREE_CODE (min_val) != INTEGER_CST)
14159 return NULL_TREE;
14160 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14161 NULL_TREE, NULL_TREE);
14163 /* *(foo *)&complexfoo => __real__ complexfoo */
14164 else if (TREE_CODE (optype) == COMPLEX_TYPE
14165 && type == TREE_TYPE (optype))
14166 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14167 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14168 else if (VECTOR_TYPE_P (optype)
14169 && type == TREE_TYPE (optype))
14171 tree part_width = TYPE_SIZE (type);
14172 tree index = bitsize_int (0);
14173 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
14174 index);
14178 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14179 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
14181 tree op00 = TREE_OPERAND (sub, 0);
14182 tree op01 = TREE_OPERAND (sub, 1);
14184 STRIP_NOPS (op00);
14185 if (TREE_CODE (op00) == ADDR_EXPR)
14187 tree op00type;
14188 op00 = TREE_OPERAND (op00, 0);
14189 op00type = TREE_TYPE (op00);
14191 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14192 if (VECTOR_TYPE_P (op00type)
14193 && type == TREE_TYPE (op00type)
14194 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
14195 but we want to treat offsets with MSB set as negative.
14196 For the code below negative offsets are invalid and
14197 TYPE_SIZE of the element is something unsigned, so
14198 check whether op01 fits into poly_int64, which implies
14199 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
14200 then just use poly_uint64 because we want to treat the
14201 value as unsigned. */
14202 && tree_fits_poly_int64_p (op01))
14204 tree part_width = TYPE_SIZE (type);
14205 poly_uint64 max_offset
14206 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14207 * TYPE_VECTOR_SUBPARTS (op00type));
14208 if (known_lt (const_op01, max_offset))
14210 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
14211 return fold_build3_loc (loc,
14212 BIT_FIELD_REF, type, op00,
14213 part_width, index);
14216 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14217 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14218 && type == TREE_TYPE (op00type))
14220 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
14221 const_op01))
14222 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14224 /* ((foo *)&fooarray)[1] => fooarray[1] */
14225 else if (TREE_CODE (op00type) == ARRAY_TYPE
14226 && type == TREE_TYPE (op00type))
14228 tree type_domain = TYPE_DOMAIN (op00type);
14229 tree min_val = size_zero_node;
14230 if (type_domain && TYPE_MIN_VALUE (type_domain))
14231 min_val = TYPE_MIN_VALUE (type_domain);
14232 poly_uint64 type_size, index;
14233 if (poly_int_tree_p (min_val)
14234 && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
14235 && multiple_p (const_op01, type_size, &index))
14237 poly_offset_int off = index + wi::to_poly_offset (min_val);
14238 op01 = wide_int_to_tree (sizetype, off);
14239 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14240 NULL_TREE, NULL_TREE);
14246 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14247 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14248 && type == TREE_TYPE (TREE_TYPE (subtype))
14249 && (!in_gimple_form
14250 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14252 tree type_domain;
14253 tree min_val = size_zero_node;
14254 sub = build_fold_indirect_ref_loc (loc, sub);
14255 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14256 if (type_domain && TYPE_MIN_VALUE (type_domain))
14257 min_val = TYPE_MIN_VALUE (type_domain);
14258 if (in_gimple_form
14259 && TREE_CODE (min_val) != INTEGER_CST)
14260 return NULL_TREE;
14261 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14262 NULL_TREE);
14265 return NULL_TREE;
14268 /* Builds an expression for an indirection through T, simplifying some
14269 cases. */
14271 tree
14272 build_fold_indirect_ref_loc (location_t loc, tree t)
14274 tree type = TREE_TYPE (TREE_TYPE (t));
14275 tree sub = fold_indirect_ref_1 (loc, type, t);
14277 if (sub)
14278 return sub;
14280 return build1_loc (loc, INDIRECT_REF, type, t);
14283 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14285 tree
14286 fold_indirect_ref_loc (location_t loc, tree t)
14288 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14290 if (sub)
14291 return sub;
14292 else
14293 return t;
14296 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14297 whose result is ignored. The type of the returned tree need not be
14298 the same as the original expression. */
14300 tree
14301 fold_ignored_result (tree t)
14303 if (!TREE_SIDE_EFFECTS (t))
14304 return integer_zero_node;
14306 for (;;)
14307 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14309 case tcc_unary:
14310 t = TREE_OPERAND (t, 0);
14311 break;
14313 case tcc_binary:
14314 case tcc_comparison:
14315 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14316 t = TREE_OPERAND (t, 0);
14317 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14318 t = TREE_OPERAND (t, 1);
14319 else
14320 return t;
14321 break;
14323 case tcc_expression:
14324 switch (TREE_CODE (t))
14326 case COMPOUND_EXPR:
14327 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14328 return t;
14329 t = TREE_OPERAND (t, 0);
14330 break;
14332 case COND_EXPR:
14333 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14334 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14335 return t;
14336 t = TREE_OPERAND (t, 0);
14337 break;
14339 default:
14340 return t;
14342 break;
14344 default:
14345 return t;
14349 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14351 tree
14352 round_up_loc (location_t loc, tree value, unsigned int divisor)
14354 tree div = NULL_TREE;
14356 if (divisor == 1)
14357 return value;
14359 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14360 have to do anything. Only do this when we are not given a const,
14361 because in that case, this check is more expensive than just
14362 doing it. */
14363 if (TREE_CODE (value) != INTEGER_CST)
14365 div = build_int_cst (TREE_TYPE (value), divisor);
14367 if (multiple_of_p (TREE_TYPE (value), value, div))
14368 return value;
14371 /* If divisor is a power of two, simplify this to bit manipulation. */
14372 if (pow2_or_zerop (divisor))
14374 if (TREE_CODE (value) == INTEGER_CST)
14376 wide_int val = wi::to_wide (value);
14377 bool overflow_p;
14379 if ((val & (divisor - 1)) == 0)
14380 return value;
14382 overflow_p = TREE_OVERFLOW (value);
14383 val += divisor - 1;
14384 val &= (int) -divisor;
14385 if (val == 0)
14386 overflow_p = true;
14388 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14390 else
14392 tree t;
14394 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14395 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14396 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14397 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14400 else
14402 if (!div)
14403 div = build_int_cst (TREE_TYPE (value), divisor);
14404 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14405 value = size_binop_loc (loc, MULT_EXPR, value, div);
14408 return value;
14411 /* Likewise, but round down. */
14413 tree
14414 round_down_loc (location_t loc, tree value, int divisor)
14416 tree div = NULL_TREE;
14418 gcc_assert (divisor > 0);
14419 if (divisor == 1)
14420 return value;
14422 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14423 have to do anything. Only do this when we are not given a const,
14424 because in that case, this check is more expensive than just
14425 doing it. */
14426 if (TREE_CODE (value) != INTEGER_CST)
14428 div = build_int_cst (TREE_TYPE (value), divisor);
14430 if (multiple_of_p (TREE_TYPE (value), value, div))
14431 return value;
14434 /* If divisor is a power of two, simplify this to bit manipulation. */
14435 if (pow2_or_zerop (divisor))
14437 tree t;
14439 t = build_int_cst (TREE_TYPE (value), -divisor);
14440 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14442 else
14444 if (!div)
14445 div = build_int_cst (TREE_TYPE (value), divisor);
14446 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14447 value = size_binop_loc (loc, MULT_EXPR, value, div);
14450 return value;
14453 /* Returns the pointer to the base of the object addressed by EXP and
14454 extracts the information about the offset of the access, storing it
14455 to PBITPOS and POFFSET. */
14457 static tree
14458 split_address_to_core_and_offset (tree exp,
14459 poly_int64_pod *pbitpos, tree *poffset)
14461 tree core;
14462 machine_mode mode;
14463 int unsignedp, reversep, volatilep;
14464 poly_int64 bitsize;
14465 location_t loc = EXPR_LOCATION (exp);
14467 if (TREE_CODE (exp) == ADDR_EXPR)
14469 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14470 poffset, &mode, &unsignedp, &reversep,
14471 &volatilep);
14472 core = build_fold_addr_expr_loc (loc, core);
14474 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14476 core = TREE_OPERAND (exp, 0);
14477 STRIP_NOPS (core);
14478 *pbitpos = 0;
14479 *poffset = TREE_OPERAND (exp, 1);
14480 if (poly_int_tree_p (*poffset))
14482 poly_offset_int tem
14483 = wi::sext (wi::to_poly_offset (*poffset),
14484 TYPE_PRECISION (TREE_TYPE (*poffset)));
14485 tem <<= LOG2_BITS_PER_UNIT;
14486 if (tem.to_shwi (pbitpos))
14487 *poffset = NULL_TREE;
14490 else
14492 core = exp;
14493 *pbitpos = 0;
14494 *poffset = NULL_TREE;
14497 return core;
14500 /* Returns true if addresses of E1 and E2 differ by a constant, false
14501 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14503 bool
14504 ptr_difference_const (tree e1, tree e2, poly_int64_pod *diff)
14506 tree core1, core2;
14507 poly_int64 bitpos1, bitpos2;
14508 tree toffset1, toffset2, tdiff, type;
14510 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14511 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14513 poly_int64 bytepos1, bytepos2;
14514 if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
14515 || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
14516 || !operand_equal_p (core1, core2, 0))
14517 return false;
14519 if (toffset1 && toffset2)
14521 type = TREE_TYPE (toffset1);
14522 if (type != TREE_TYPE (toffset2))
14523 toffset2 = fold_convert (type, toffset2);
14525 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14526 if (!cst_and_fits_in_hwi (tdiff))
14527 return false;
14529 *diff = int_cst_value (tdiff);
14531 else if (toffset1 || toffset2)
14533 /* If only one of the offsets is non-constant, the difference cannot
14534 be a constant. */
14535 return false;
14537 else
14538 *diff = 0;
14540 *diff += bytepos1 - bytepos2;
14541 return true;
14544 /* Return OFF converted to a pointer offset type suitable as offset for
14545 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14546 tree
14547 convert_to_ptrofftype_loc (location_t loc, tree off)
14549 return fold_convert_loc (loc, sizetype, off);
14552 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14553 tree
14554 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14556 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14557 ptr, convert_to_ptrofftype_loc (loc, off));
14560 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14561 tree
14562 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14564 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14565 ptr, size_int (off));
14568 /* Return a char pointer for a C string if it is a string constant
14569 or sum of string constant and integer constant. We only support
14570 string constants properly terminated with '\0' character.
14571 If STRLEN is a valid pointer, length (including terminating character)
14572 of returned string is stored to the argument. */
14574 const char *
14575 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen)
14577 tree offset_node;
14579 if (strlen)
14580 *strlen = 0;
14582 src = string_constant (src, &offset_node);
14583 if (src == 0)
14584 return NULL;
14586 unsigned HOST_WIDE_INT offset = 0;
14587 if (offset_node != NULL_TREE)
14589 if (!tree_fits_uhwi_p (offset_node))
14590 return NULL;
14591 else
14592 offset = tree_to_uhwi (offset_node);
14595 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14596 const char *string = TREE_STRING_POINTER (src);
14598 /* Support only properly null-terminated strings. */
14599 if (string_length == 0
14600 || string[string_length - 1] != '\0'
14601 || offset >= string_length)
14602 return NULL;
14604 if (strlen)
14605 *strlen = string_length - offset;
14606 return string + offset;
14609 /* Given a tree T, compute which bits in T may be nonzero. */
14611 wide_int
14612 tree_nonzero_bits (const_tree t)
14614 switch (TREE_CODE (t))
14616 case INTEGER_CST:
14617 return wi::to_wide (t);
14618 case SSA_NAME:
14619 return get_nonzero_bits (t);
14620 case NON_LVALUE_EXPR:
14621 case SAVE_EXPR:
14622 return tree_nonzero_bits (TREE_OPERAND (t, 0));
14623 case BIT_AND_EXPR:
14624 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14625 tree_nonzero_bits (TREE_OPERAND (t, 1)));
14626 case BIT_IOR_EXPR:
14627 case BIT_XOR_EXPR:
14628 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14629 tree_nonzero_bits (TREE_OPERAND (t, 1)));
14630 case COND_EXPR:
14631 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1)),
14632 tree_nonzero_bits (TREE_OPERAND (t, 2)));
14633 CASE_CONVERT:
14634 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14635 TYPE_PRECISION (TREE_TYPE (t)),
14636 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0))));
14637 case PLUS_EXPR:
14638 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
14640 wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0));
14641 wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1));
14642 if (wi::bit_and (nzbits1, nzbits2) == 0)
14643 return wi::bit_or (nzbits1, nzbits2);
14645 break;
14646 case LSHIFT_EXPR:
14647 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
14649 tree type = TREE_TYPE (t);
14650 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
14651 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
14652 TYPE_PRECISION (type));
14653 return wi::neg_p (arg1)
14654 ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
14655 : wi::lshift (nzbits, arg1);
14657 break;
14658 case RSHIFT_EXPR:
14659 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
14661 tree type = TREE_TYPE (t);
14662 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
14663 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
14664 TYPE_PRECISION (type));
14665 return wi::neg_p (arg1)
14666 ? wi::lshift (nzbits, -arg1)
14667 : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
14669 break;
14670 default:
14671 break;
14674 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t)));
14677 #if CHECKING_P
14679 namespace selftest {
14681 /* Helper functions for writing tests of folding trees. */
14683 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14685 static void
14686 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14687 tree constant)
14689 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14692 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14693 wrapping WRAPPED_EXPR. */
14695 static void
14696 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14697 tree wrapped_expr)
14699 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14700 ASSERT_NE (wrapped_expr, result);
14701 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14702 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14705 /* Verify that various arithmetic binary operations are folded
14706 correctly. */
14708 static void
14709 test_arithmetic_folding ()
14711 tree type = integer_type_node;
14712 tree x = create_tmp_var_raw (type, "x");
14713 tree zero = build_zero_cst (type);
14714 tree one = build_int_cst (type, 1);
14716 /* Addition. */
14717 /* 1 <-- (0 + 1) */
14718 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14719 one);
14720 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14721 one);
14723 /* (nonlvalue)x <-- (x + 0) */
14724 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14727 /* Subtraction. */
14728 /* 0 <-- (x - x) */
14729 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14730 zero);
14731 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14734 /* Multiplication. */
14735 /* 0 <-- (x * 0) */
14736 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14737 zero);
14739 /* (nonlvalue)x <-- (x * 1) */
14740 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14744 /* Verify that various binary operations on vectors are folded
14745 correctly. */
14747 static void
14748 test_vector_folding ()
14750 tree inner_type = integer_type_node;
14751 tree type = build_vector_type (inner_type, 4);
14752 tree zero = build_zero_cst (type);
14753 tree one = build_one_cst (type);
14755 /* Verify equality tests that return a scalar boolean result. */
14756 tree res_type = boolean_type_node;
14757 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14758 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14759 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14760 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14763 /* Verify folding of VEC_DUPLICATE_EXPRs. */
14765 static void
14766 test_vec_duplicate_folding ()
14768 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
14769 machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
14770 /* This will be 1 if VEC_MODE isn't a vector mode. */
14771 poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
14773 tree type = build_vector_type (ssizetype, nunits);
14774 tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
14775 tree dup5_cst = build_vector_from_val (type, ssize_int (5));
14776 ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
14779 /* Run all of the selftests within this file. */
14781 void
14782 fold_const_c_tests ()
14784 test_arithmetic_folding ();
14785 test_vector_folding ();
14786 test_vec_duplicate_folding ();
14789 } // namespace selftest
14791 #endif /* CHECKING_P */