re PR debug/91929 (missing inline subroutine information in build using sin/cos)
[official-gcc.git] / gcc / fold-const.c
blob58b967e48fee11fee98436e14a50b61c4312fa65
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987-2019 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 bool 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 bool 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_ROUNDEVEN:
333 CASE_CFN_ROUNDEVEN_FN:
334 CASE_CFN_SIN:
335 CASE_CFN_SINH:
336 CASE_CFN_TAN:
337 CASE_CFN_TANH:
338 CASE_CFN_TRUNC:
339 return true;
341 CASE_CFN_LLRINT:
342 CASE_CFN_LRINT:
343 CASE_CFN_NEARBYINT:
344 CASE_CFN_RINT:
345 return !flag_rounding_math;
347 default:
348 break;
350 return false;
353 /* Check whether we may negate an integer constant T without causing
354 overflow. */
356 bool
357 may_negate_without_overflow_p (const_tree t)
359 tree type;
361 gcc_assert (TREE_CODE (t) == INTEGER_CST);
363 type = TREE_TYPE (t);
364 if (TYPE_UNSIGNED (type))
365 return false;
367 return !wi::only_sign_bit_p (wi::to_wide (t));
370 /* Determine whether an expression T can be cheaply negated using
371 the function negate_expr without introducing undefined overflow. */
373 static bool
374 negate_expr_p (tree t)
376 tree type;
378 if (t == 0)
379 return false;
381 type = TREE_TYPE (t);
383 STRIP_SIGN_NOPS (t);
384 switch (TREE_CODE (t))
386 case INTEGER_CST:
387 if (INTEGRAL_TYPE_P (type) && TYPE_UNSIGNED (type))
388 return true;
390 /* Check that -CST will not overflow type. */
391 return may_negate_without_overflow_p (t);
392 case BIT_NOT_EXPR:
393 return (INTEGRAL_TYPE_P (type)
394 && TYPE_OVERFLOW_WRAPS (type));
396 case FIXED_CST:
397 return true;
399 case NEGATE_EXPR:
400 return !TYPE_OVERFLOW_SANITIZED (type);
402 case REAL_CST:
403 /* We want to canonicalize to positive real constants. Pretend
404 that only negative ones can be easily negated. */
405 return REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
407 case COMPLEX_CST:
408 return negate_expr_p (TREE_REALPART (t))
409 && negate_expr_p (TREE_IMAGPART (t));
411 case VECTOR_CST:
413 if (FLOAT_TYPE_P (TREE_TYPE (type)) || TYPE_OVERFLOW_WRAPS (type))
414 return true;
416 /* Steps don't prevent negation. */
417 unsigned int count = vector_cst_encoded_nelts (t);
418 for (unsigned int i = 0; i < count; ++i)
419 if (!negate_expr_p (VECTOR_CST_ENCODED_ELT (t, i)))
420 return false;
422 return true;
425 case COMPLEX_EXPR:
426 return negate_expr_p (TREE_OPERAND (t, 0))
427 && negate_expr_p (TREE_OPERAND (t, 1));
429 case CONJ_EXPR:
430 return negate_expr_p (TREE_OPERAND (t, 0));
432 case PLUS_EXPR:
433 if (HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
434 || HONOR_SIGNED_ZEROS (element_mode (type))
435 || (ANY_INTEGRAL_TYPE_P (type)
436 && ! TYPE_OVERFLOW_WRAPS (type)))
437 return false;
438 /* -(A + B) -> (-B) - A. */
439 if (negate_expr_p (TREE_OPERAND (t, 1)))
440 return true;
441 /* -(A + B) -> (-A) - B. */
442 return negate_expr_p (TREE_OPERAND (t, 0));
444 case MINUS_EXPR:
445 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
446 return !HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
447 && !HONOR_SIGNED_ZEROS (element_mode (type))
448 && (! ANY_INTEGRAL_TYPE_P (type)
449 || TYPE_OVERFLOW_WRAPS (type));
451 case MULT_EXPR:
452 if (TYPE_UNSIGNED (type))
453 break;
454 /* INT_MIN/n * n doesn't overflow while negating one operand it does
455 if n is a (negative) power of two. */
456 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
457 && ! TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
458 && ! ((TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
459 && (wi::popcount
460 (wi::abs (wi::to_wide (TREE_OPERAND (t, 0))))) != 1)
461 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
462 && (wi::popcount
463 (wi::abs (wi::to_wide (TREE_OPERAND (t, 1))))) != 1)))
464 break;
466 /* Fall through. */
468 case RDIV_EXPR:
469 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (TREE_TYPE (t))))
470 return negate_expr_p (TREE_OPERAND (t, 1))
471 || negate_expr_p (TREE_OPERAND (t, 0));
472 break;
474 case TRUNC_DIV_EXPR:
475 case ROUND_DIV_EXPR:
476 case EXACT_DIV_EXPR:
477 if (TYPE_UNSIGNED (type))
478 break;
479 /* In general we can't negate A in A / B, because if A is INT_MIN and
480 B is not 1 we change the sign of the result. */
481 if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
482 && negate_expr_p (TREE_OPERAND (t, 0)))
483 return true;
484 /* In general we can't negate B in A / B, because if A is INT_MIN and
485 B is 1, we may turn this into INT_MIN / -1 which is undefined
486 and actually traps on some architectures. */
487 if (! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
488 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
489 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
490 && ! integer_onep (TREE_OPERAND (t, 1))))
491 return negate_expr_p (TREE_OPERAND (t, 1));
492 break;
494 case NOP_EXPR:
495 /* Negate -((double)float) as (double)(-float). */
496 if (TREE_CODE (type) == REAL_TYPE)
498 tree tem = strip_float_extensions (t);
499 if (tem != t)
500 return negate_expr_p (tem);
502 break;
504 case CALL_EXPR:
505 /* Negate -f(x) as f(-x). */
506 if (negate_mathfn_p (get_call_combined_fn (t)))
507 return negate_expr_p (CALL_EXPR_ARG (t, 0));
508 break;
510 case RSHIFT_EXPR:
511 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
512 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
514 tree op1 = TREE_OPERAND (t, 1);
515 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
516 return true;
518 break;
520 default:
521 break;
523 return false;
526 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
527 simplification is possible.
528 If negate_expr_p would return true for T, NULL_TREE will never be
529 returned. */
531 static tree
532 fold_negate_expr_1 (location_t loc, tree t)
534 tree type = TREE_TYPE (t);
535 tree tem;
537 switch (TREE_CODE (t))
539 /* Convert - (~A) to A + 1. */
540 case BIT_NOT_EXPR:
541 if (INTEGRAL_TYPE_P (type))
542 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
543 build_one_cst (type));
544 break;
546 case INTEGER_CST:
547 tem = fold_negate_const (t, type);
548 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
549 || (ANY_INTEGRAL_TYPE_P (type)
550 && !TYPE_OVERFLOW_TRAPS (type)
551 && TYPE_OVERFLOW_WRAPS (type))
552 || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
553 return tem;
554 break;
556 case POLY_INT_CST:
557 case REAL_CST:
558 case FIXED_CST:
559 tem = fold_negate_const (t, type);
560 return tem;
562 case COMPLEX_CST:
564 tree rpart = fold_negate_expr (loc, TREE_REALPART (t));
565 tree ipart = fold_negate_expr (loc, TREE_IMAGPART (t));
566 if (rpart && ipart)
567 return build_complex (type, rpart, ipart);
569 break;
571 case VECTOR_CST:
573 tree_vector_builder elts;
574 elts.new_unary_operation (type, t, true);
575 unsigned int count = elts.encoded_nelts ();
576 for (unsigned int i = 0; i < count; ++i)
578 tree elt = fold_negate_expr (loc, VECTOR_CST_ELT (t, i));
579 if (elt == NULL_TREE)
580 return NULL_TREE;
581 elts.quick_push (elt);
584 return elts.build ();
587 case COMPLEX_EXPR:
588 if (negate_expr_p (t))
589 return fold_build2_loc (loc, COMPLEX_EXPR, type,
590 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
591 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
592 break;
594 case CONJ_EXPR:
595 if (negate_expr_p (t))
596 return fold_build1_loc (loc, CONJ_EXPR, type,
597 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
598 break;
600 case NEGATE_EXPR:
601 if (!TYPE_OVERFLOW_SANITIZED (type))
602 return TREE_OPERAND (t, 0);
603 break;
605 case PLUS_EXPR:
606 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
607 && !HONOR_SIGNED_ZEROS (element_mode (type)))
609 /* -(A + B) -> (-B) - A. */
610 if (negate_expr_p (TREE_OPERAND (t, 1)))
612 tem = negate_expr (TREE_OPERAND (t, 1));
613 return fold_build2_loc (loc, MINUS_EXPR, type,
614 tem, TREE_OPERAND (t, 0));
617 /* -(A + B) -> (-A) - B. */
618 if (negate_expr_p (TREE_OPERAND (t, 0)))
620 tem = negate_expr (TREE_OPERAND (t, 0));
621 return fold_build2_loc (loc, MINUS_EXPR, type,
622 tem, TREE_OPERAND (t, 1));
625 break;
627 case MINUS_EXPR:
628 /* - (A - B) -> B - A */
629 if (!HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type))
630 && !HONOR_SIGNED_ZEROS (element_mode (type)))
631 return fold_build2_loc (loc, MINUS_EXPR, type,
632 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
633 break;
635 case MULT_EXPR:
636 if (TYPE_UNSIGNED (type))
637 break;
639 /* Fall through. */
641 case RDIV_EXPR:
642 if (! HONOR_SIGN_DEPENDENT_ROUNDING (element_mode (type)))
644 tem = TREE_OPERAND (t, 1);
645 if (negate_expr_p (tem))
646 return fold_build2_loc (loc, TREE_CODE (t), type,
647 TREE_OPERAND (t, 0), negate_expr (tem));
648 tem = TREE_OPERAND (t, 0);
649 if (negate_expr_p (tem))
650 return fold_build2_loc (loc, TREE_CODE (t), type,
651 negate_expr (tem), TREE_OPERAND (t, 1));
653 break;
655 case TRUNC_DIV_EXPR:
656 case ROUND_DIV_EXPR:
657 case EXACT_DIV_EXPR:
658 if (TYPE_UNSIGNED (type))
659 break;
660 /* In general we can't negate A in A / B, because if A is INT_MIN and
661 B is not 1 we change the sign of the result. */
662 if (TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST
663 && negate_expr_p (TREE_OPERAND (t, 0)))
664 return fold_build2_loc (loc, TREE_CODE (t), type,
665 negate_expr (TREE_OPERAND (t, 0)),
666 TREE_OPERAND (t, 1));
667 /* In general we can't negate B in A / B, because if A is INT_MIN and
668 B is 1, we may turn this into INT_MIN / -1 which is undefined
669 and actually traps on some architectures. */
670 if ((! ANY_INTEGRAL_TYPE_P (TREE_TYPE (t))
671 || TYPE_OVERFLOW_WRAPS (TREE_TYPE (t))
672 || (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
673 && ! integer_onep (TREE_OPERAND (t, 1))))
674 && negate_expr_p (TREE_OPERAND (t, 1)))
675 return fold_build2_loc (loc, TREE_CODE (t), type,
676 TREE_OPERAND (t, 0),
677 negate_expr (TREE_OPERAND (t, 1)));
678 break;
680 case NOP_EXPR:
681 /* Convert -((double)float) into (double)(-float). */
682 if (TREE_CODE (type) == REAL_TYPE)
684 tem = strip_float_extensions (t);
685 if (tem != t && negate_expr_p (tem))
686 return fold_convert_loc (loc, type, negate_expr (tem));
688 break;
690 case CALL_EXPR:
691 /* Negate -f(x) as f(-x). */
692 if (negate_mathfn_p (get_call_combined_fn (t))
693 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
695 tree fndecl, arg;
697 fndecl = get_callee_fndecl (t);
698 arg = negate_expr (CALL_EXPR_ARG (t, 0));
699 return build_call_expr_loc (loc, fndecl, 1, arg);
701 break;
703 case RSHIFT_EXPR:
704 /* Optimize -((int)x >> 31) into (unsigned)x >> 31 for int. */
705 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
707 tree op1 = TREE_OPERAND (t, 1);
708 if (wi::to_wide (op1) == TYPE_PRECISION (type) - 1)
710 tree ntype = TYPE_UNSIGNED (type)
711 ? signed_type_for (type)
712 : unsigned_type_for (type);
713 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
714 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
715 return fold_convert_loc (loc, type, temp);
718 break;
720 default:
721 break;
724 return NULL_TREE;
727 /* A wrapper for fold_negate_expr_1. */
729 static tree
730 fold_negate_expr (location_t loc, tree t)
732 tree type = TREE_TYPE (t);
733 STRIP_SIGN_NOPS (t);
734 tree tem = fold_negate_expr_1 (loc, t);
735 if (tem == NULL_TREE)
736 return NULL_TREE;
737 return fold_convert_loc (loc, type, tem);
740 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T cannot be
741 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
742 return NULL_TREE. */
744 static tree
745 negate_expr (tree t)
747 tree type, tem;
748 location_t loc;
750 if (t == NULL_TREE)
751 return NULL_TREE;
753 loc = EXPR_LOCATION (t);
754 type = TREE_TYPE (t);
755 STRIP_SIGN_NOPS (t);
757 tem = fold_negate_expr (loc, t);
758 if (!tem)
759 tem = build1_loc (loc, NEGATE_EXPR, TREE_TYPE (t), t);
760 return fold_convert_loc (loc, type, tem);
763 /* Split a tree IN into a constant, literal and variable parts that could be
764 combined with CODE to make IN. "constant" means an expression with
765 TREE_CONSTANT but that isn't an actual constant. CODE must be a
766 commutative arithmetic operation. Store the constant part into *CONP,
767 the literal in *LITP and return the variable part. If a part isn't
768 present, set it to null. If the tree does not decompose in this way,
769 return the entire tree as the variable part and the other parts as null.
771 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
772 case, we negate an operand that was subtracted. Except if it is a
773 literal for which we use *MINUS_LITP instead.
775 If NEGATE_P is true, we are negating all of IN, again except a literal
776 for which we use *MINUS_LITP instead. If a variable part is of pointer
777 type, it is negated after converting to TYPE. This prevents us from
778 generating illegal MINUS pointer expression. LOC is the location of
779 the converted variable part.
781 If IN is itself a literal or constant, return it as appropriate.
783 Note that we do not guarantee that any of the three values will be the
784 same type as IN, but they will have the same signedness and mode. */
786 static tree
787 split_tree (tree in, tree type, enum tree_code code,
788 tree *minus_varp, tree *conp, tree *minus_conp,
789 tree *litp, tree *minus_litp, int negate_p)
791 tree var = 0;
792 *minus_varp = 0;
793 *conp = 0;
794 *minus_conp = 0;
795 *litp = 0;
796 *minus_litp = 0;
798 /* Strip any conversions that don't change the machine mode or signedness. */
799 STRIP_SIGN_NOPS (in);
801 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
802 || TREE_CODE (in) == FIXED_CST)
803 *litp = in;
804 else if (TREE_CODE (in) == code
805 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
806 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
807 /* We can associate addition and subtraction together (even
808 though the C standard doesn't say so) for integers because
809 the value is not affected. For reals, the value might be
810 affected, so we can't. */
811 && ((code == PLUS_EXPR && TREE_CODE (in) == POINTER_PLUS_EXPR)
812 || (code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
813 || (code == MINUS_EXPR
814 && (TREE_CODE (in) == PLUS_EXPR
815 || TREE_CODE (in) == POINTER_PLUS_EXPR)))))
817 tree op0 = TREE_OPERAND (in, 0);
818 tree op1 = TREE_OPERAND (in, 1);
819 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
820 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
822 /* First see if either of the operands is a literal, then a constant. */
823 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
824 || TREE_CODE (op0) == FIXED_CST)
825 *litp = op0, op0 = 0;
826 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
827 || TREE_CODE (op1) == FIXED_CST)
828 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
830 if (op0 != 0 && TREE_CONSTANT (op0))
831 *conp = op0, op0 = 0;
832 else if (op1 != 0 && TREE_CONSTANT (op1))
833 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
835 /* If we haven't dealt with either operand, this is not a case we can
836 decompose. Otherwise, VAR is either of the ones remaining, if any. */
837 if (op0 != 0 && op1 != 0)
838 var = in;
839 else if (op0 != 0)
840 var = op0;
841 else
842 var = op1, neg_var_p = neg1_p;
844 /* Now do any needed negations. */
845 if (neg_litp_p)
846 *minus_litp = *litp, *litp = 0;
847 if (neg_conp_p && *conp)
848 *minus_conp = *conp, *conp = 0;
849 if (neg_var_p && var)
850 *minus_varp = var, var = 0;
852 else if (TREE_CONSTANT (in))
853 *conp = in;
854 else if (TREE_CODE (in) == BIT_NOT_EXPR
855 && code == PLUS_EXPR)
857 /* -1 - X is folded to ~X, undo that here. Do _not_ do this
858 when IN is constant. */
859 *litp = build_minus_one_cst (type);
860 *minus_varp = TREE_OPERAND (in, 0);
862 else
863 var = in;
865 if (negate_p)
867 if (*litp)
868 *minus_litp = *litp, *litp = 0;
869 else if (*minus_litp)
870 *litp = *minus_litp, *minus_litp = 0;
871 if (*conp)
872 *minus_conp = *conp, *conp = 0;
873 else if (*minus_conp)
874 *conp = *minus_conp, *minus_conp = 0;
875 if (var)
876 *minus_varp = var, var = 0;
877 else if (*minus_varp)
878 var = *minus_varp, *minus_varp = 0;
881 if (*litp
882 && TREE_OVERFLOW_P (*litp))
883 *litp = drop_tree_overflow (*litp);
884 if (*minus_litp
885 && TREE_OVERFLOW_P (*minus_litp))
886 *minus_litp = drop_tree_overflow (*minus_litp);
888 return var;
891 /* Re-associate trees split by the above function. T1 and T2 are
892 either expressions to associate or null. Return the new
893 expression, if any. LOC is the location of the new expression. If
894 we build an operation, do it in TYPE and with CODE. */
896 static tree
897 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
899 if (t1 == 0)
901 gcc_assert (t2 == 0 || code != MINUS_EXPR);
902 return t2;
904 else if (t2 == 0)
905 return t1;
907 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
908 try to fold this since we will have infinite recursion. But do
909 deal with any NEGATE_EXPRs. */
910 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
911 || TREE_CODE (t1) == PLUS_EXPR || TREE_CODE (t2) == PLUS_EXPR
912 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
914 if (code == PLUS_EXPR)
916 if (TREE_CODE (t1) == NEGATE_EXPR)
917 return build2_loc (loc, MINUS_EXPR, type,
918 fold_convert_loc (loc, type, t2),
919 fold_convert_loc (loc, type,
920 TREE_OPERAND (t1, 0)));
921 else if (TREE_CODE (t2) == NEGATE_EXPR)
922 return build2_loc (loc, MINUS_EXPR, type,
923 fold_convert_loc (loc, type, t1),
924 fold_convert_loc (loc, type,
925 TREE_OPERAND (t2, 0)));
926 else if (integer_zerop (t2))
927 return fold_convert_loc (loc, type, t1);
929 else if (code == MINUS_EXPR)
931 if (integer_zerop (t2))
932 return fold_convert_loc (loc, type, t1);
935 return build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
936 fold_convert_loc (loc, type, t2));
939 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
940 fold_convert_loc (loc, type, t2));
943 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
944 for use in int_const_binop, size_binop and size_diffop. */
946 static bool
947 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
949 if (!INTEGRAL_TYPE_P (type1) && !POINTER_TYPE_P (type1))
950 return false;
951 if (!INTEGRAL_TYPE_P (type2) && !POINTER_TYPE_P (type2))
952 return false;
954 switch (code)
956 case LSHIFT_EXPR:
957 case RSHIFT_EXPR:
958 case LROTATE_EXPR:
959 case RROTATE_EXPR:
960 return true;
962 default:
963 break;
966 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
967 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
968 && TYPE_MODE (type1) == TYPE_MODE (type2);
971 /* Combine two wide ints ARG1 and ARG2 under operation CODE to produce
972 a new constant in RES. Return FALSE if we don't know how to
973 evaluate CODE at compile-time. */
975 bool
976 wide_int_binop (wide_int &res,
977 enum tree_code code, const wide_int &arg1, const wide_int &arg2,
978 signop sign, wi::overflow_type *overflow)
980 wide_int tmp;
981 *overflow = wi::OVF_NONE;
982 switch (code)
984 case BIT_IOR_EXPR:
985 res = wi::bit_or (arg1, arg2);
986 break;
988 case BIT_XOR_EXPR:
989 res = wi::bit_xor (arg1, arg2);
990 break;
992 case BIT_AND_EXPR:
993 res = wi::bit_and (arg1, arg2);
994 break;
996 case RSHIFT_EXPR:
997 case LSHIFT_EXPR:
998 if (wi::neg_p (arg2))
1000 tmp = -arg2;
1001 if (code == RSHIFT_EXPR)
1002 code = LSHIFT_EXPR;
1003 else
1004 code = RSHIFT_EXPR;
1006 else
1007 tmp = arg2;
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, tmp, sign);
1014 else
1015 res = wi::lshift (arg1, tmp);
1016 break;
1018 case RROTATE_EXPR:
1019 case LROTATE_EXPR:
1020 if (wi::neg_p (arg2))
1022 tmp = -arg2;
1023 if (code == RROTATE_EXPR)
1024 code = LROTATE_EXPR;
1025 else
1026 code = RROTATE_EXPR;
1028 else
1029 tmp = arg2;
1031 if (code == RROTATE_EXPR)
1032 res = wi::rrotate (arg1, tmp);
1033 else
1034 res = wi::lrotate (arg1, tmp);
1035 break;
1037 case PLUS_EXPR:
1038 res = wi::add (arg1, arg2, sign, overflow);
1039 break;
1041 case MINUS_EXPR:
1042 res = wi::sub (arg1, arg2, sign, overflow);
1043 break;
1045 case MULT_EXPR:
1046 res = wi::mul (arg1, arg2, sign, overflow);
1047 break;
1049 case MULT_HIGHPART_EXPR:
1050 res = wi::mul_high (arg1, arg2, sign);
1051 break;
1053 case TRUNC_DIV_EXPR:
1054 case EXACT_DIV_EXPR:
1055 if (arg2 == 0)
1056 return false;
1057 res = wi::div_trunc (arg1, arg2, sign, overflow);
1058 break;
1060 case FLOOR_DIV_EXPR:
1061 if (arg2 == 0)
1062 return false;
1063 res = wi::div_floor (arg1, arg2, sign, overflow);
1064 break;
1066 case CEIL_DIV_EXPR:
1067 if (arg2 == 0)
1068 return false;
1069 res = wi::div_ceil (arg1, arg2, sign, overflow);
1070 break;
1072 case ROUND_DIV_EXPR:
1073 if (arg2 == 0)
1074 return false;
1075 res = wi::div_round (arg1, arg2, sign, overflow);
1076 break;
1078 case TRUNC_MOD_EXPR:
1079 if (arg2 == 0)
1080 return false;
1081 res = wi::mod_trunc (arg1, arg2, sign, overflow);
1082 break;
1084 case FLOOR_MOD_EXPR:
1085 if (arg2 == 0)
1086 return false;
1087 res = wi::mod_floor (arg1, arg2, sign, overflow);
1088 break;
1090 case CEIL_MOD_EXPR:
1091 if (arg2 == 0)
1092 return false;
1093 res = wi::mod_ceil (arg1, arg2, sign, overflow);
1094 break;
1096 case ROUND_MOD_EXPR:
1097 if (arg2 == 0)
1098 return false;
1099 res = wi::mod_round (arg1, arg2, sign, overflow);
1100 break;
1102 case MIN_EXPR:
1103 res = wi::min (arg1, arg2, sign);
1104 break;
1106 case MAX_EXPR:
1107 res = wi::max (arg1, arg2, sign);
1108 break;
1110 default:
1111 return false;
1113 return true;
1116 /* Combine two poly int's ARG1 and ARG2 under operation CODE to
1117 produce a new constant in RES. Return FALSE if we don't know how
1118 to evaluate CODE at compile-time. */
1120 static bool
1121 poly_int_binop (poly_wide_int &res, enum tree_code code,
1122 const_tree arg1, const_tree arg2,
1123 signop sign, wi::overflow_type *overflow)
1125 gcc_assert (NUM_POLY_INT_COEFFS != 1);
1126 gcc_assert (poly_int_tree_p (arg1) && poly_int_tree_p (arg2));
1127 switch (code)
1129 case PLUS_EXPR:
1130 res = wi::add (wi::to_poly_wide (arg1),
1131 wi::to_poly_wide (arg2), sign, overflow);
1132 break;
1134 case MINUS_EXPR:
1135 res = wi::sub (wi::to_poly_wide (arg1),
1136 wi::to_poly_wide (arg2), sign, overflow);
1137 break;
1139 case MULT_EXPR:
1140 if (TREE_CODE (arg2) == INTEGER_CST)
1141 res = wi::mul (wi::to_poly_wide (arg1),
1142 wi::to_wide (arg2), sign, overflow);
1143 else if (TREE_CODE (arg1) == INTEGER_CST)
1144 res = wi::mul (wi::to_poly_wide (arg2),
1145 wi::to_wide (arg1), sign, overflow);
1146 else
1147 return NULL_TREE;
1148 break;
1150 case LSHIFT_EXPR:
1151 if (TREE_CODE (arg2) == INTEGER_CST)
1152 res = wi::to_poly_wide (arg1) << wi::to_wide (arg2);
1153 else
1154 return false;
1155 break;
1157 case BIT_IOR_EXPR:
1158 if (TREE_CODE (arg2) != INTEGER_CST
1159 || !can_ior_p (wi::to_poly_wide (arg1), wi::to_wide (arg2),
1160 &res))
1161 return false;
1162 break;
1164 default:
1165 return false;
1167 return true;
1170 /* Combine two integer constants ARG1 and ARG2 under operation CODE to
1171 produce a new constant. Return NULL_TREE if we don't know how to
1172 evaluate CODE at compile-time. */
1174 tree
1175 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2,
1176 int overflowable)
1178 poly_wide_int poly_res;
1179 tree type = TREE_TYPE (arg1);
1180 signop sign = TYPE_SIGN (type);
1181 wi::overflow_type overflow = wi::OVF_NONE;
1183 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg2) == INTEGER_CST)
1185 wide_int warg1 = wi::to_wide (arg1), res;
1186 wide_int warg2 = wi::to_wide (arg2, TYPE_PRECISION (type));
1187 if (!wide_int_binop (res, code, warg1, warg2, sign, &overflow))
1188 return NULL_TREE;
1189 poly_res = res;
1191 else if (!poly_int_tree_p (arg1)
1192 || !poly_int_tree_p (arg2)
1193 || !poly_int_binop (poly_res, code, arg1, arg2, sign, &overflow))
1194 return NULL_TREE;
1195 return force_fit_type (type, poly_res, overflowable,
1196 (((sign == SIGNED || overflowable == -1)
1197 && overflow)
1198 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2)));
1201 /* Return true if binary operation OP distributes over addition in operand
1202 OPNO, with the other operand being held constant. OPNO counts from 1. */
1204 static bool
1205 distributes_over_addition_p (tree_code op, int opno)
1207 switch (op)
1209 case PLUS_EXPR:
1210 case MINUS_EXPR:
1211 case MULT_EXPR:
1212 return true;
1214 case LSHIFT_EXPR:
1215 return opno == 1;
1217 default:
1218 return false;
1222 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1223 constant. We assume ARG1 and ARG2 have the same data type, or at least
1224 are the same kind of constant and the same machine mode. Return zero if
1225 combining the constants is not allowed in the current operating mode. */
1227 static tree
1228 const_binop (enum tree_code code, tree arg1, tree arg2)
1230 /* Sanity check for the recursive cases. */
1231 if (!arg1 || !arg2)
1232 return NULL_TREE;
1234 STRIP_NOPS (arg1);
1235 STRIP_NOPS (arg2);
1237 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1239 if (code == POINTER_PLUS_EXPR)
1240 return int_const_binop (PLUS_EXPR,
1241 arg1, fold_convert (TREE_TYPE (arg1), arg2));
1243 return int_const_binop (code, arg1, arg2);
1246 if (TREE_CODE (arg1) == REAL_CST && TREE_CODE (arg2) == REAL_CST)
1248 machine_mode mode;
1249 REAL_VALUE_TYPE d1;
1250 REAL_VALUE_TYPE d2;
1251 REAL_VALUE_TYPE value;
1252 REAL_VALUE_TYPE result;
1253 bool inexact;
1254 tree t, type;
1256 /* The following codes are handled by real_arithmetic. */
1257 switch (code)
1259 case PLUS_EXPR:
1260 case MINUS_EXPR:
1261 case MULT_EXPR:
1262 case RDIV_EXPR:
1263 case MIN_EXPR:
1264 case MAX_EXPR:
1265 break;
1267 default:
1268 return NULL_TREE;
1271 d1 = TREE_REAL_CST (arg1);
1272 d2 = TREE_REAL_CST (arg2);
1274 type = TREE_TYPE (arg1);
1275 mode = TYPE_MODE (type);
1277 /* Don't perform operation if we honor signaling NaNs and
1278 either operand is a signaling NaN. */
1279 if (HONOR_SNANS (mode)
1280 && (REAL_VALUE_ISSIGNALING_NAN (d1)
1281 || REAL_VALUE_ISSIGNALING_NAN (d2)))
1282 return NULL_TREE;
1284 /* Don't perform operation if it would raise a division
1285 by zero exception. */
1286 if (code == RDIV_EXPR
1287 && real_equal (&d2, &dconst0)
1288 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1289 return NULL_TREE;
1291 /* If either operand is a NaN, just return it. Otherwise, set up
1292 for floating-point trap; we return an overflow. */
1293 if (REAL_VALUE_ISNAN (d1))
1295 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1296 is off. */
1297 d1.signalling = 0;
1298 t = build_real (type, d1);
1299 return t;
1301 else if (REAL_VALUE_ISNAN (d2))
1303 /* Make resulting NaN value to be qNaN when flag_signaling_nans
1304 is off. */
1305 d2.signalling = 0;
1306 t = build_real (type, d2);
1307 return t;
1310 inexact = real_arithmetic (&value, code, &d1, &d2);
1311 real_convert (&result, mode, &value);
1313 /* Don't constant fold this floating point operation if
1314 the result has overflowed and flag_trapping_math. */
1315 if (flag_trapping_math
1316 && MODE_HAS_INFINITIES (mode)
1317 && REAL_VALUE_ISINF (result)
1318 && !REAL_VALUE_ISINF (d1)
1319 && !REAL_VALUE_ISINF (d2))
1320 return NULL_TREE;
1322 /* Don't constant fold this floating point operation if the
1323 result may dependent upon the run-time rounding mode and
1324 flag_rounding_math is set, or if GCC's software emulation
1325 is unable to accurately represent the result. */
1326 if ((flag_rounding_math
1327 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1328 && (inexact || !real_identical (&result, &value)))
1329 return NULL_TREE;
1331 t = build_real (type, result);
1333 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1334 return t;
1337 if (TREE_CODE (arg1) == FIXED_CST)
1339 FIXED_VALUE_TYPE f1;
1340 FIXED_VALUE_TYPE f2;
1341 FIXED_VALUE_TYPE result;
1342 tree t, type;
1343 int sat_p;
1344 bool overflow_p;
1346 /* The following codes are handled by fixed_arithmetic. */
1347 switch (code)
1349 case PLUS_EXPR:
1350 case MINUS_EXPR:
1351 case MULT_EXPR:
1352 case TRUNC_DIV_EXPR:
1353 if (TREE_CODE (arg2) != FIXED_CST)
1354 return NULL_TREE;
1355 f2 = TREE_FIXED_CST (arg2);
1356 break;
1358 case LSHIFT_EXPR:
1359 case RSHIFT_EXPR:
1361 if (TREE_CODE (arg2) != INTEGER_CST)
1362 return NULL_TREE;
1363 wi::tree_to_wide_ref w2 = wi::to_wide (arg2);
1364 f2.data.high = w2.elt (1);
1365 f2.data.low = w2.ulow ();
1366 f2.mode = SImode;
1368 break;
1370 default:
1371 return NULL_TREE;
1374 f1 = TREE_FIXED_CST (arg1);
1375 type = TREE_TYPE (arg1);
1376 sat_p = TYPE_SATURATING (type);
1377 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1378 t = build_fixed (type, result);
1379 /* Propagate overflow flags. */
1380 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1381 TREE_OVERFLOW (t) = 1;
1382 return t;
1385 if (TREE_CODE (arg1) == COMPLEX_CST && TREE_CODE (arg2) == COMPLEX_CST)
1387 tree type = TREE_TYPE (arg1);
1388 tree r1 = TREE_REALPART (arg1);
1389 tree i1 = TREE_IMAGPART (arg1);
1390 tree r2 = TREE_REALPART (arg2);
1391 tree i2 = TREE_IMAGPART (arg2);
1392 tree real, imag;
1394 switch (code)
1396 case PLUS_EXPR:
1397 case MINUS_EXPR:
1398 real = const_binop (code, r1, r2);
1399 imag = const_binop (code, i1, i2);
1400 break;
1402 case MULT_EXPR:
1403 if (COMPLEX_FLOAT_TYPE_P (type))
1404 return do_mpc_arg2 (arg1, arg2, type,
1405 /* do_nonfinite= */ folding_initializer,
1406 mpc_mul);
1408 real = const_binop (MINUS_EXPR,
1409 const_binop (MULT_EXPR, r1, r2),
1410 const_binop (MULT_EXPR, i1, i2));
1411 imag = const_binop (PLUS_EXPR,
1412 const_binop (MULT_EXPR, r1, i2),
1413 const_binop (MULT_EXPR, i1, r2));
1414 break;
1416 case RDIV_EXPR:
1417 if (COMPLEX_FLOAT_TYPE_P (type))
1418 return do_mpc_arg2 (arg1, arg2, type,
1419 /* do_nonfinite= */ folding_initializer,
1420 mpc_div);
1421 /* Fallthru. */
1422 case TRUNC_DIV_EXPR:
1423 case CEIL_DIV_EXPR:
1424 case FLOOR_DIV_EXPR:
1425 case ROUND_DIV_EXPR:
1426 if (flag_complex_method == 0)
1428 /* Keep this algorithm in sync with
1429 tree-complex.c:expand_complex_div_straight().
1431 Expand complex division to scalars, straightforward algorithm.
1432 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
1433 t = br*br + bi*bi
1435 tree magsquared
1436 = const_binop (PLUS_EXPR,
1437 const_binop (MULT_EXPR, r2, r2),
1438 const_binop (MULT_EXPR, i2, i2));
1439 tree t1
1440 = const_binop (PLUS_EXPR,
1441 const_binop (MULT_EXPR, r1, r2),
1442 const_binop (MULT_EXPR, i1, i2));
1443 tree t2
1444 = const_binop (MINUS_EXPR,
1445 const_binop (MULT_EXPR, i1, r2),
1446 const_binop (MULT_EXPR, r1, i2));
1448 real = const_binop (code, t1, magsquared);
1449 imag = const_binop (code, t2, magsquared);
1451 else
1453 /* Keep this algorithm in sync with
1454 tree-complex.c:expand_complex_div_wide().
1456 Expand complex division to scalars, modified algorithm to minimize
1457 overflow with wide input ranges. */
1458 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
1459 fold_abs_const (r2, TREE_TYPE (type)),
1460 fold_abs_const (i2, TREE_TYPE (type)));
1462 if (integer_nonzerop (compare))
1464 /* In the TRUE branch, we compute
1465 ratio = br/bi;
1466 div = (br * ratio) + bi;
1467 tr = (ar * ratio) + ai;
1468 ti = (ai * ratio) - ar;
1469 tr = tr / div;
1470 ti = ti / div; */
1471 tree ratio = const_binop (code, r2, i2);
1472 tree div = const_binop (PLUS_EXPR, i2,
1473 const_binop (MULT_EXPR, r2, ratio));
1474 real = const_binop (MULT_EXPR, r1, ratio);
1475 real = const_binop (PLUS_EXPR, real, i1);
1476 real = const_binop (code, real, div);
1478 imag = const_binop (MULT_EXPR, i1, ratio);
1479 imag = const_binop (MINUS_EXPR, imag, r1);
1480 imag = const_binop (code, imag, div);
1482 else
1484 /* In the FALSE branch, we compute
1485 ratio = d/c;
1486 divisor = (d * ratio) + c;
1487 tr = (b * ratio) + a;
1488 ti = b - (a * ratio);
1489 tr = tr / div;
1490 ti = ti / div; */
1491 tree ratio = const_binop (code, i2, r2);
1492 tree div = const_binop (PLUS_EXPR, r2,
1493 const_binop (MULT_EXPR, i2, ratio));
1495 real = const_binop (MULT_EXPR, i1, ratio);
1496 real = const_binop (PLUS_EXPR, real, r1);
1497 real = const_binop (code, real, div);
1499 imag = const_binop (MULT_EXPR, r1, ratio);
1500 imag = const_binop (MINUS_EXPR, i1, imag);
1501 imag = const_binop (code, imag, div);
1504 break;
1506 default:
1507 return NULL_TREE;
1510 if (real && imag)
1511 return build_complex (type, real, imag);
1514 if (TREE_CODE (arg1) == VECTOR_CST
1515 && TREE_CODE (arg2) == VECTOR_CST
1516 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)),
1517 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg2))))
1519 tree type = TREE_TYPE (arg1);
1520 bool step_ok_p;
1521 if (VECTOR_CST_STEPPED_P (arg1)
1522 && VECTOR_CST_STEPPED_P (arg2))
1523 /* We can operate directly on the encoding if:
1525 a3 - a2 == a2 - a1 && b3 - b2 == b2 - b1
1526 implies
1527 (a3 op b3) - (a2 op b2) == (a2 op b2) - (a1 op b1)
1529 Addition and subtraction are the supported operators
1530 for which this is true. */
1531 step_ok_p = (code == PLUS_EXPR || code == MINUS_EXPR);
1532 else if (VECTOR_CST_STEPPED_P (arg1))
1533 /* We can operate directly on stepped encodings if:
1535 a3 - a2 == a2 - a1
1536 implies:
1537 (a3 op c) - (a2 op c) == (a2 op c) - (a1 op c)
1539 which is true if (x -> x op c) distributes over addition. */
1540 step_ok_p = distributes_over_addition_p (code, 1);
1541 else
1542 /* Similarly in reverse. */
1543 step_ok_p = distributes_over_addition_p (code, 2);
1544 tree_vector_builder elts;
1545 if (!elts.new_binary_operation (type, arg1, arg2, step_ok_p))
1546 return NULL_TREE;
1547 unsigned int count = elts.encoded_nelts ();
1548 for (unsigned int i = 0; i < count; ++i)
1550 tree elem1 = VECTOR_CST_ELT (arg1, i);
1551 tree elem2 = VECTOR_CST_ELT (arg2, i);
1553 tree elt = const_binop (code, elem1, elem2);
1555 /* It is possible that const_binop cannot handle the given
1556 code and return NULL_TREE */
1557 if (elt == NULL_TREE)
1558 return NULL_TREE;
1559 elts.quick_push (elt);
1562 return elts.build ();
1565 /* Shifts allow a scalar offset for a vector. */
1566 if (TREE_CODE (arg1) == VECTOR_CST
1567 && TREE_CODE (arg2) == INTEGER_CST)
1569 tree type = TREE_TYPE (arg1);
1570 bool step_ok_p = distributes_over_addition_p (code, 1);
1571 tree_vector_builder elts;
1572 if (!elts.new_unary_operation (type, arg1, step_ok_p))
1573 return NULL_TREE;
1574 unsigned int count = elts.encoded_nelts ();
1575 for (unsigned int i = 0; i < count; ++i)
1577 tree elem1 = VECTOR_CST_ELT (arg1, i);
1579 tree elt = const_binop (code, elem1, arg2);
1581 /* It is possible that const_binop cannot handle the given
1582 code and return NULL_TREE. */
1583 if (elt == NULL_TREE)
1584 return NULL_TREE;
1585 elts.quick_push (elt);
1588 return elts.build ();
1590 return NULL_TREE;
1593 /* Overload that adds a TYPE parameter to be able to dispatch
1594 to fold_relational_const. */
1596 tree
1597 const_binop (enum tree_code code, tree type, tree arg1, tree arg2)
1599 if (TREE_CODE_CLASS (code) == tcc_comparison)
1600 return fold_relational_const (code, type, arg1, arg2);
1602 /* ??? Until we make the const_binop worker take the type of the
1603 result as argument put those cases that need it here. */
1604 switch (code)
1606 case VEC_SERIES_EXPR:
1607 if (CONSTANT_CLASS_P (arg1)
1608 && CONSTANT_CLASS_P (arg2))
1609 return build_vec_series (type, arg1, arg2);
1610 return NULL_TREE;
1612 case COMPLEX_EXPR:
1613 if ((TREE_CODE (arg1) == REAL_CST
1614 && TREE_CODE (arg2) == REAL_CST)
1615 || (TREE_CODE (arg1) == INTEGER_CST
1616 && TREE_CODE (arg2) == INTEGER_CST))
1617 return build_complex (type, arg1, arg2);
1618 return NULL_TREE;
1620 case POINTER_DIFF_EXPR:
1621 if (poly_int_tree_p (arg1) && poly_int_tree_p (arg2))
1623 poly_offset_int res = (wi::to_poly_offset (arg1)
1624 - wi::to_poly_offset (arg2));
1625 return force_fit_type (type, res, 1,
1626 TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1628 return NULL_TREE;
1630 case VEC_PACK_TRUNC_EXPR:
1631 case VEC_PACK_FIX_TRUNC_EXPR:
1632 case VEC_PACK_FLOAT_EXPR:
1634 unsigned int HOST_WIDE_INT out_nelts, in_nelts, i;
1636 if (TREE_CODE (arg1) != VECTOR_CST
1637 || TREE_CODE (arg2) != VECTOR_CST)
1638 return NULL_TREE;
1640 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1641 return NULL_TREE;
1643 out_nelts = in_nelts * 2;
1644 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1645 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1647 tree_vector_builder elts (type, out_nelts, 1);
1648 for (i = 0; i < out_nelts; i++)
1650 tree elt = (i < in_nelts
1651 ? VECTOR_CST_ELT (arg1, i)
1652 : VECTOR_CST_ELT (arg2, i - in_nelts));
1653 elt = fold_convert_const (code == VEC_PACK_TRUNC_EXPR
1654 ? NOP_EXPR
1655 : code == VEC_PACK_FLOAT_EXPR
1656 ? FLOAT_EXPR : FIX_TRUNC_EXPR,
1657 TREE_TYPE (type), elt);
1658 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1659 return NULL_TREE;
1660 elts.quick_push (elt);
1663 return elts.build ();
1666 case VEC_WIDEN_MULT_LO_EXPR:
1667 case VEC_WIDEN_MULT_HI_EXPR:
1668 case VEC_WIDEN_MULT_EVEN_EXPR:
1669 case VEC_WIDEN_MULT_ODD_EXPR:
1671 unsigned HOST_WIDE_INT out_nelts, in_nelts, out, ofs, scale;
1673 if (TREE_CODE (arg1) != VECTOR_CST || TREE_CODE (arg2) != VECTOR_CST)
1674 return NULL_TREE;
1676 if (!VECTOR_CST_NELTS (arg1).is_constant (&in_nelts))
1677 return NULL_TREE;
1678 out_nelts = in_nelts / 2;
1679 gcc_assert (known_eq (in_nelts, VECTOR_CST_NELTS (arg2))
1680 && known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1682 if (code == VEC_WIDEN_MULT_LO_EXPR)
1683 scale = 0, ofs = BYTES_BIG_ENDIAN ? out_nelts : 0;
1684 else if (code == VEC_WIDEN_MULT_HI_EXPR)
1685 scale = 0, ofs = BYTES_BIG_ENDIAN ? 0 : out_nelts;
1686 else if (code == VEC_WIDEN_MULT_EVEN_EXPR)
1687 scale = 1, ofs = 0;
1688 else /* if (code == VEC_WIDEN_MULT_ODD_EXPR) */
1689 scale = 1, ofs = 1;
1691 tree_vector_builder elts (type, out_nelts, 1);
1692 for (out = 0; out < out_nelts; out++)
1694 unsigned int in = (out << scale) + ofs;
1695 tree t1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1696 VECTOR_CST_ELT (arg1, in));
1697 tree t2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type),
1698 VECTOR_CST_ELT (arg2, in));
1700 if (t1 == NULL_TREE || t2 == NULL_TREE)
1701 return NULL_TREE;
1702 tree elt = const_binop (MULT_EXPR, t1, t2);
1703 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1704 return NULL_TREE;
1705 elts.quick_push (elt);
1708 return elts.build ();
1711 default:;
1714 if (TREE_CODE_CLASS (code) != tcc_binary)
1715 return NULL_TREE;
1717 /* Make sure type and arg0 have the same saturating flag. */
1718 gcc_checking_assert (TYPE_SATURATING (type)
1719 == TYPE_SATURATING (TREE_TYPE (arg1)));
1721 return const_binop (code, arg1, arg2);
1724 /* Compute CODE ARG1 with resulting type TYPE with ARG1 being constant.
1725 Return zero if computing the constants is not possible. */
1727 tree
1728 const_unop (enum tree_code code, tree type, tree arg0)
1730 /* Don't perform the operation, other than NEGATE and ABS, if
1731 flag_signaling_nans is on and the operand is a signaling NaN. */
1732 if (TREE_CODE (arg0) == REAL_CST
1733 && HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
1734 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg0))
1735 && code != NEGATE_EXPR
1736 && code != ABS_EXPR
1737 && code != ABSU_EXPR)
1738 return NULL_TREE;
1740 switch (code)
1742 CASE_CONVERT:
1743 case FLOAT_EXPR:
1744 case FIX_TRUNC_EXPR:
1745 case FIXED_CONVERT_EXPR:
1746 return fold_convert_const (code, type, arg0);
1748 case ADDR_SPACE_CONVERT_EXPR:
1749 /* If the source address is 0, and the source address space
1750 cannot have a valid object at 0, fold to dest type null. */
1751 if (integer_zerop (arg0)
1752 && !(targetm.addr_space.zero_address_valid
1753 (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0))))))
1754 return fold_convert_const (code, type, arg0);
1755 break;
1757 case VIEW_CONVERT_EXPR:
1758 return fold_view_convert_expr (type, arg0);
1760 case NEGATE_EXPR:
1762 /* Can't call fold_negate_const directly here as that doesn't
1763 handle all cases and we might not be able to negate some
1764 constants. */
1765 tree tem = fold_negate_expr (UNKNOWN_LOCATION, arg0);
1766 if (tem && CONSTANT_CLASS_P (tem))
1767 return tem;
1768 break;
1771 case ABS_EXPR:
1772 case ABSU_EXPR:
1773 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
1774 return fold_abs_const (arg0, type);
1775 break;
1777 case CONJ_EXPR:
1778 if (TREE_CODE (arg0) == COMPLEX_CST)
1780 tree ipart = fold_negate_const (TREE_IMAGPART (arg0),
1781 TREE_TYPE (type));
1782 return build_complex (type, TREE_REALPART (arg0), ipart);
1784 break;
1786 case BIT_NOT_EXPR:
1787 if (TREE_CODE (arg0) == INTEGER_CST)
1788 return fold_not_const (arg0, type);
1789 else if (POLY_INT_CST_P (arg0))
1790 return wide_int_to_tree (type, -poly_int_cst_value (arg0));
1791 /* Perform BIT_NOT_EXPR on each element individually. */
1792 else if (TREE_CODE (arg0) == VECTOR_CST)
1794 tree elem;
1796 /* This can cope with stepped encodings because ~x == -1 - x. */
1797 tree_vector_builder elements;
1798 elements.new_unary_operation (type, arg0, true);
1799 unsigned int i, count = elements.encoded_nelts ();
1800 for (i = 0; i < count; ++i)
1802 elem = VECTOR_CST_ELT (arg0, i);
1803 elem = const_unop (BIT_NOT_EXPR, TREE_TYPE (type), elem);
1804 if (elem == NULL_TREE)
1805 break;
1806 elements.quick_push (elem);
1808 if (i == count)
1809 return elements.build ();
1811 break;
1813 case TRUTH_NOT_EXPR:
1814 if (TREE_CODE (arg0) == INTEGER_CST)
1815 return constant_boolean_node (integer_zerop (arg0), type);
1816 break;
1818 case REALPART_EXPR:
1819 if (TREE_CODE (arg0) == COMPLEX_CST)
1820 return fold_convert (type, TREE_REALPART (arg0));
1821 break;
1823 case IMAGPART_EXPR:
1824 if (TREE_CODE (arg0) == COMPLEX_CST)
1825 return fold_convert (type, TREE_IMAGPART (arg0));
1826 break;
1828 case VEC_UNPACK_LO_EXPR:
1829 case VEC_UNPACK_HI_EXPR:
1830 case VEC_UNPACK_FLOAT_LO_EXPR:
1831 case VEC_UNPACK_FLOAT_HI_EXPR:
1832 case VEC_UNPACK_FIX_TRUNC_LO_EXPR:
1833 case VEC_UNPACK_FIX_TRUNC_HI_EXPR:
1835 unsigned HOST_WIDE_INT out_nelts, in_nelts, i;
1836 enum tree_code subcode;
1838 if (TREE_CODE (arg0) != VECTOR_CST)
1839 return NULL_TREE;
1841 if (!VECTOR_CST_NELTS (arg0).is_constant (&in_nelts))
1842 return NULL_TREE;
1843 out_nelts = in_nelts / 2;
1844 gcc_assert (known_eq (out_nelts, TYPE_VECTOR_SUBPARTS (type)));
1846 unsigned int offset = 0;
1847 if ((!BYTES_BIG_ENDIAN) ^ (code == VEC_UNPACK_LO_EXPR
1848 || code == VEC_UNPACK_FLOAT_LO_EXPR
1849 || code == VEC_UNPACK_FIX_TRUNC_LO_EXPR))
1850 offset = out_nelts;
1852 if (code == VEC_UNPACK_LO_EXPR || code == VEC_UNPACK_HI_EXPR)
1853 subcode = NOP_EXPR;
1854 else if (code == VEC_UNPACK_FLOAT_LO_EXPR
1855 || code == VEC_UNPACK_FLOAT_HI_EXPR)
1856 subcode = FLOAT_EXPR;
1857 else
1858 subcode = FIX_TRUNC_EXPR;
1860 tree_vector_builder elts (type, out_nelts, 1);
1861 for (i = 0; i < out_nelts; i++)
1863 tree elt = fold_convert_const (subcode, TREE_TYPE (type),
1864 VECTOR_CST_ELT (arg0, i + offset));
1865 if (elt == NULL_TREE || !CONSTANT_CLASS_P (elt))
1866 return NULL_TREE;
1867 elts.quick_push (elt);
1870 return elts.build ();
1873 case VEC_DUPLICATE_EXPR:
1874 if (CONSTANT_CLASS_P (arg0))
1875 return build_vector_from_val (type, arg0);
1876 return NULL_TREE;
1878 default:
1879 break;
1882 return NULL_TREE;
1885 /* Create a sizetype INT_CST node with NUMBER sign extended. KIND
1886 indicates which particular sizetype to create. */
1888 tree
1889 size_int_kind (poly_int64 number, enum size_type_kind kind)
1891 return build_int_cst (sizetype_tab[(int) kind], number);
1894 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1895 is a tree code. The type of the result is taken from the operands.
1896 Both must be equivalent integer types, ala int_binop_types_match_p.
1897 If the operands are constant, so is the result. */
1899 tree
1900 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
1902 tree type = TREE_TYPE (arg0);
1904 if (arg0 == error_mark_node || arg1 == error_mark_node)
1905 return error_mark_node;
1907 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
1908 TREE_TYPE (arg1)));
1910 /* Handle the special case of two poly_int constants faster. */
1911 if (poly_int_tree_p (arg0) && poly_int_tree_p (arg1))
1913 /* And some specific cases even faster than that. */
1914 if (code == PLUS_EXPR)
1916 if (integer_zerop (arg0)
1917 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
1918 return arg1;
1919 if (integer_zerop (arg1)
1920 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
1921 return arg0;
1923 else if (code == MINUS_EXPR)
1925 if (integer_zerop (arg1)
1926 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg1)))
1927 return arg0;
1929 else if (code == MULT_EXPR)
1931 if (integer_onep (arg0)
1932 && !TREE_OVERFLOW (tree_strip_any_location_wrapper (arg0)))
1933 return arg1;
1936 /* Handle general case of two integer constants. For sizetype
1937 constant calculations we always want to know about overflow,
1938 even in the unsigned case. */
1939 tree res = int_const_binop (code, arg0, arg1, -1);
1940 if (res != NULL_TREE)
1941 return res;
1944 return fold_build2_loc (loc, code, type, arg0, arg1);
1947 /* Given two values, either both of sizetype or both of bitsizetype,
1948 compute the difference between the two values. Return the value
1949 in signed type corresponding to the type of the operands. */
1951 tree
1952 size_diffop_loc (location_t loc, tree arg0, tree arg1)
1954 tree type = TREE_TYPE (arg0);
1955 tree ctype;
1957 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
1958 TREE_TYPE (arg1)));
1960 /* If the type is already signed, just do the simple thing. */
1961 if (!TYPE_UNSIGNED (type))
1962 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
1964 if (type == sizetype)
1965 ctype = ssizetype;
1966 else if (type == bitsizetype)
1967 ctype = sbitsizetype;
1968 else
1969 ctype = signed_type_for (type);
1971 /* If either operand is not a constant, do the conversions to the signed
1972 type and subtract. The hardware will do the right thing with any
1973 overflow in the subtraction. */
1974 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1975 return size_binop_loc (loc, MINUS_EXPR,
1976 fold_convert_loc (loc, ctype, arg0),
1977 fold_convert_loc (loc, ctype, arg1));
1979 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1980 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1981 overflow) and negate (which can't either). Special-case a result
1982 of zero while we're here. */
1983 if (tree_int_cst_equal (arg0, arg1))
1984 return build_int_cst (ctype, 0);
1985 else if (tree_int_cst_lt (arg1, arg0))
1986 return fold_convert_loc (loc, ctype,
1987 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
1988 else
1989 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
1990 fold_convert_loc (loc, ctype,
1991 size_binop_loc (loc,
1992 MINUS_EXPR,
1993 arg1, arg0)));
1996 /* A subroutine of fold_convert_const handling conversions of an
1997 INTEGER_CST to another integer type. */
1999 static tree
2000 fold_convert_const_int_from_int (tree type, const_tree arg1)
2002 /* Given an integer constant, make new constant with new type,
2003 appropriately sign-extended or truncated. Use widest_int
2004 so that any extension is done according ARG1's type. */
2005 return force_fit_type (type, wi::to_widest (arg1),
2006 !POINTER_TYPE_P (TREE_TYPE (arg1)),
2007 TREE_OVERFLOW (arg1));
2010 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2011 to an integer type. */
2013 static tree
2014 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2016 bool overflow = false;
2017 tree t;
2019 /* The following code implements the floating point to integer
2020 conversion rules required by the Java Language Specification,
2021 that IEEE NaNs are mapped to zero and values that overflow
2022 the target precision saturate, i.e. values greater than
2023 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2024 are mapped to INT_MIN. These semantics are allowed by the
2025 C and C++ standards that simply state that the behavior of
2026 FP-to-integer conversion is unspecified upon overflow. */
2028 wide_int val;
2029 REAL_VALUE_TYPE r;
2030 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2032 switch (code)
2034 case FIX_TRUNC_EXPR:
2035 real_trunc (&r, VOIDmode, &x);
2036 break;
2038 default:
2039 gcc_unreachable ();
2042 /* If R is NaN, return zero and show we have an overflow. */
2043 if (REAL_VALUE_ISNAN (r))
2045 overflow = true;
2046 val = wi::zero (TYPE_PRECISION (type));
2049 /* See if R is less than the lower bound or greater than the
2050 upper bound. */
2052 if (! overflow)
2054 tree lt = TYPE_MIN_VALUE (type);
2055 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2056 if (real_less (&r, &l))
2058 overflow = true;
2059 val = wi::to_wide (lt);
2063 if (! overflow)
2065 tree ut = TYPE_MAX_VALUE (type);
2066 if (ut)
2068 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2069 if (real_less (&u, &r))
2071 overflow = true;
2072 val = wi::to_wide (ut);
2077 if (! overflow)
2078 val = real_to_integer (&r, &overflow, TYPE_PRECISION (type));
2080 t = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (arg1));
2081 return t;
2084 /* A subroutine of fold_convert_const handling conversions of a
2085 FIXED_CST to an integer type. */
2087 static tree
2088 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2090 tree t;
2091 double_int temp, temp_trunc;
2092 scalar_mode mode;
2094 /* Right shift FIXED_CST to temp by fbit. */
2095 temp = TREE_FIXED_CST (arg1).data;
2096 mode = TREE_FIXED_CST (arg1).mode;
2097 if (GET_MODE_FBIT (mode) < HOST_BITS_PER_DOUBLE_INT)
2099 temp = temp.rshift (GET_MODE_FBIT (mode),
2100 HOST_BITS_PER_DOUBLE_INT,
2101 SIGNED_FIXED_POINT_MODE_P (mode));
2103 /* Left shift temp to temp_trunc by fbit. */
2104 temp_trunc = temp.lshift (GET_MODE_FBIT (mode),
2105 HOST_BITS_PER_DOUBLE_INT,
2106 SIGNED_FIXED_POINT_MODE_P (mode));
2108 else
2110 temp = double_int_zero;
2111 temp_trunc = double_int_zero;
2114 /* If FIXED_CST is negative, we need to round the value toward 0.
2115 By checking if the fractional bits are not zero to add 1 to temp. */
2116 if (SIGNED_FIXED_POINT_MODE_P (mode)
2117 && temp_trunc.is_negative ()
2118 && TREE_FIXED_CST (arg1).data != temp_trunc)
2119 temp += double_int_one;
2121 /* Given a fixed-point constant, make new constant with new type,
2122 appropriately sign-extended or truncated. */
2123 t = force_fit_type (type, temp, -1,
2124 (temp.is_negative ()
2125 && (TYPE_UNSIGNED (type)
2126 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2127 | TREE_OVERFLOW (arg1));
2129 return t;
2132 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2133 to another floating point type. */
2135 static tree
2136 fold_convert_const_real_from_real (tree type, const_tree arg1)
2138 REAL_VALUE_TYPE value;
2139 tree t;
2141 /* Don't perform the operation if flag_signaling_nans is on
2142 and the operand is a signaling NaN. */
2143 if (HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1)))
2144 && REAL_VALUE_ISSIGNALING_NAN (TREE_REAL_CST (arg1)))
2145 return NULL_TREE;
2147 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2148 t = build_real (type, value);
2150 /* If converting an infinity or NAN to a representation that doesn't
2151 have one, set the overflow bit so that we can produce some kind of
2152 error message at the appropriate point if necessary. It's not the
2153 most user-friendly message, but it's better than nothing. */
2154 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2155 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2156 TREE_OVERFLOW (t) = 1;
2157 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2158 && !MODE_HAS_NANS (TYPE_MODE (type)))
2159 TREE_OVERFLOW (t) = 1;
2160 /* Regular overflow, conversion produced an infinity in a mode that
2161 can't represent them. */
2162 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2163 && REAL_VALUE_ISINF (value)
2164 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2165 TREE_OVERFLOW (t) = 1;
2166 else
2167 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2168 return t;
2171 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2172 to a floating point type. */
2174 static tree
2175 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2177 REAL_VALUE_TYPE value;
2178 tree t;
2180 real_convert_from_fixed (&value, SCALAR_FLOAT_TYPE_MODE (type),
2181 &TREE_FIXED_CST (arg1));
2182 t = build_real (type, value);
2184 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2185 return t;
2188 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2189 to another fixed-point type. */
2191 static tree
2192 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2194 FIXED_VALUE_TYPE value;
2195 tree t;
2196 bool overflow_p;
2198 overflow_p = fixed_convert (&value, SCALAR_TYPE_MODE (type),
2199 &TREE_FIXED_CST (arg1), TYPE_SATURATING (type));
2200 t = build_fixed (type, value);
2202 /* Propagate overflow flags. */
2203 if (overflow_p | TREE_OVERFLOW (arg1))
2204 TREE_OVERFLOW (t) = 1;
2205 return t;
2208 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2209 to a fixed-point type. */
2211 static tree
2212 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2214 FIXED_VALUE_TYPE value;
2215 tree t;
2216 bool overflow_p;
2217 double_int di;
2219 gcc_assert (TREE_INT_CST_NUNITS (arg1) <= 2);
2221 di.low = TREE_INT_CST_ELT (arg1, 0);
2222 if (TREE_INT_CST_NUNITS (arg1) == 1)
2223 di.high = (HOST_WIDE_INT) di.low < 0 ? HOST_WIDE_INT_M1 : 0;
2224 else
2225 di.high = TREE_INT_CST_ELT (arg1, 1);
2227 overflow_p = fixed_convert_from_int (&value, SCALAR_TYPE_MODE (type), di,
2228 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2229 TYPE_SATURATING (type));
2230 t = build_fixed (type, value);
2232 /* Propagate overflow flags. */
2233 if (overflow_p | TREE_OVERFLOW (arg1))
2234 TREE_OVERFLOW (t) = 1;
2235 return t;
2238 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2239 to a fixed-point type. */
2241 static tree
2242 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2244 FIXED_VALUE_TYPE value;
2245 tree t;
2246 bool overflow_p;
2248 overflow_p = fixed_convert_from_real (&value, SCALAR_TYPE_MODE (type),
2249 &TREE_REAL_CST (arg1),
2250 TYPE_SATURATING (type));
2251 t = build_fixed (type, value);
2253 /* Propagate overflow flags. */
2254 if (overflow_p | TREE_OVERFLOW (arg1))
2255 TREE_OVERFLOW (t) = 1;
2256 return t;
2259 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2260 type TYPE. If no simplification can be done return NULL_TREE. */
2262 static tree
2263 fold_convert_const (enum tree_code code, tree type, tree arg1)
2265 tree arg_type = TREE_TYPE (arg1);
2266 if (arg_type == type)
2267 return arg1;
2269 /* We can't widen types, since the runtime value could overflow the
2270 original type before being extended to the new type. */
2271 if (POLY_INT_CST_P (arg1)
2272 && (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2273 && TYPE_PRECISION (type) <= TYPE_PRECISION (arg_type))
2274 return build_poly_int_cst (type,
2275 poly_wide_int::from (poly_int_cst_value (arg1),
2276 TYPE_PRECISION (type),
2277 TYPE_SIGN (arg_type)));
2279 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2280 || TREE_CODE (type) == OFFSET_TYPE)
2282 if (TREE_CODE (arg1) == INTEGER_CST)
2283 return fold_convert_const_int_from_int (type, arg1);
2284 else if (TREE_CODE (arg1) == REAL_CST)
2285 return fold_convert_const_int_from_real (code, type, arg1);
2286 else if (TREE_CODE (arg1) == FIXED_CST)
2287 return fold_convert_const_int_from_fixed (type, arg1);
2289 else if (TREE_CODE (type) == REAL_TYPE)
2291 if (TREE_CODE (arg1) == INTEGER_CST)
2292 return build_real_from_int_cst (type, arg1);
2293 else if (TREE_CODE (arg1) == REAL_CST)
2294 return fold_convert_const_real_from_real (type, arg1);
2295 else if (TREE_CODE (arg1) == FIXED_CST)
2296 return fold_convert_const_real_from_fixed (type, arg1);
2298 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2300 if (TREE_CODE (arg1) == FIXED_CST)
2301 return fold_convert_const_fixed_from_fixed (type, arg1);
2302 else if (TREE_CODE (arg1) == INTEGER_CST)
2303 return fold_convert_const_fixed_from_int (type, arg1);
2304 else if (TREE_CODE (arg1) == REAL_CST)
2305 return fold_convert_const_fixed_from_real (type, arg1);
2307 else if (TREE_CODE (type) == VECTOR_TYPE)
2309 if (TREE_CODE (arg1) == VECTOR_CST
2310 && known_eq (TYPE_VECTOR_SUBPARTS (type), VECTOR_CST_NELTS (arg1)))
2312 tree elttype = TREE_TYPE (type);
2313 tree arg1_elttype = TREE_TYPE (TREE_TYPE (arg1));
2314 /* We can't handle steps directly when extending, since the
2315 values need to wrap at the original precision first. */
2316 bool step_ok_p
2317 = (INTEGRAL_TYPE_P (elttype)
2318 && INTEGRAL_TYPE_P (arg1_elttype)
2319 && TYPE_PRECISION (elttype) <= TYPE_PRECISION (arg1_elttype));
2320 tree_vector_builder v;
2321 if (!v.new_unary_operation (type, arg1, step_ok_p))
2322 return NULL_TREE;
2323 unsigned int len = v.encoded_nelts ();
2324 for (unsigned int i = 0; i < len; ++i)
2326 tree elt = VECTOR_CST_ELT (arg1, i);
2327 tree cvt = fold_convert_const (code, elttype, elt);
2328 if (cvt == NULL_TREE)
2329 return NULL_TREE;
2330 v.quick_push (cvt);
2332 return v.build ();
2335 return NULL_TREE;
2338 /* Construct a vector of zero elements of vector type TYPE. */
2340 static tree
2341 build_zero_vector (tree type)
2343 tree t;
2345 t = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2346 return build_vector_from_val (type, t);
2349 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2351 bool
2352 fold_convertible_p (const_tree type, const_tree arg)
2354 tree orig = TREE_TYPE (arg);
2356 if (type == orig)
2357 return true;
2359 if (TREE_CODE (arg) == ERROR_MARK
2360 || TREE_CODE (type) == ERROR_MARK
2361 || TREE_CODE (orig) == ERROR_MARK)
2362 return false;
2364 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2365 return true;
2367 switch (TREE_CODE (type))
2369 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2370 case POINTER_TYPE: case REFERENCE_TYPE:
2371 case OFFSET_TYPE:
2372 return (INTEGRAL_TYPE_P (orig)
2373 || (POINTER_TYPE_P (orig)
2374 && TYPE_PRECISION (type) <= TYPE_PRECISION (orig))
2375 || TREE_CODE (orig) == OFFSET_TYPE);
2377 case REAL_TYPE:
2378 case FIXED_POINT_TYPE:
2379 case VECTOR_TYPE:
2380 case VOID_TYPE:
2381 return TREE_CODE (type) == TREE_CODE (orig);
2383 default:
2384 return false;
2388 /* Convert expression ARG to type TYPE. Used by the middle-end for
2389 simple conversions in preference to calling the front-end's convert. */
2391 tree
2392 fold_convert_loc (location_t loc, tree type, tree arg)
2394 tree orig = TREE_TYPE (arg);
2395 tree tem;
2397 if (type == orig)
2398 return arg;
2400 if (TREE_CODE (arg) == ERROR_MARK
2401 || TREE_CODE (type) == ERROR_MARK
2402 || TREE_CODE (orig) == ERROR_MARK)
2403 return error_mark_node;
2405 switch (TREE_CODE (type))
2407 case POINTER_TYPE:
2408 case REFERENCE_TYPE:
2409 /* Handle conversions between pointers to different address spaces. */
2410 if (POINTER_TYPE_P (orig)
2411 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2412 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2413 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2414 /* fall through */
2416 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2417 case OFFSET_TYPE:
2418 if (TREE_CODE (arg) == INTEGER_CST)
2420 tem = fold_convert_const (NOP_EXPR, type, arg);
2421 if (tem != NULL_TREE)
2422 return tem;
2424 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2425 || TREE_CODE (orig) == OFFSET_TYPE)
2426 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2427 if (TREE_CODE (orig) == COMPLEX_TYPE)
2428 return fold_convert_loc (loc, type,
2429 fold_build1_loc (loc, REALPART_EXPR,
2430 TREE_TYPE (orig), arg));
2431 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2432 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2433 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2435 case REAL_TYPE:
2436 if (TREE_CODE (arg) == INTEGER_CST)
2438 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2439 if (tem != NULL_TREE)
2440 return tem;
2442 else if (TREE_CODE (arg) == REAL_CST)
2444 tem = fold_convert_const (NOP_EXPR, type, arg);
2445 if (tem != NULL_TREE)
2446 return tem;
2448 else if (TREE_CODE (arg) == FIXED_CST)
2450 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2451 if (tem != NULL_TREE)
2452 return tem;
2455 switch (TREE_CODE (orig))
2457 case INTEGER_TYPE:
2458 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2459 case POINTER_TYPE: case REFERENCE_TYPE:
2460 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2462 case REAL_TYPE:
2463 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2465 case FIXED_POINT_TYPE:
2466 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2468 case COMPLEX_TYPE:
2469 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2470 return fold_convert_loc (loc, type, tem);
2472 default:
2473 gcc_unreachable ();
2476 case FIXED_POINT_TYPE:
2477 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2478 || TREE_CODE (arg) == REAL_CST)
2480 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2481 if (tem != NULL_TREE)
2482 goto fold_convert_exit;
2485 switch (TREE_CODE (orig))
2487 case FIXED_POINT_TYPE:
2488 case INTEGER_TYPE:
2489 case ENUMERAL_TYPE:
2490 case BOOLEAN_TYPE:
2491 case REAL_TYPE:
2492 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2494 case COMPLEX_TYPE:
2495 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2496 return fold_convert_loc (loc, type, tem);
2498 default:
2499 gcc_unreachable ();
2502 case COMPLEX_TYPE:
2503 switch (TREE_CODE (orig))
2505 case INTEGER_TYPE:
2506 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2507 case POINTER_TYPE: case REFERENCE_TYPE:
2508 case REAL_TYPE:
2509 case FIXED_POINT_TYPE:
2510 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2511 fold_convert_loc (loc, TREE_TYPE (type), arg),
2512 fold_convert_loc (loc, TREE_TYPE (type),
2513 integer_zero_node));
2514 case COMPLEX_TYPE:
2516 tree rpart, ipart;
2518 if (TREE_CODE (arg) == COMPLEX_EXPR)
2520 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2521 TREE_OPERAND (arg, 0));
2522 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2523 TREE_OPERAND (arg, 1));
2524 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2527 arg = save_expr (arg);
2528 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2529 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2530 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2531 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2532 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2535 default:
2536 gcc_unreachable ();
2539 case VECTOR_TYPE:
2540 if (integer_zerop (arg))
2541 return build_zero_vector (type);
2542 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2543 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2544 || TREE_CODE (orig) == VECTOR_TYPE);
2545 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2547 case VOID_TYPE:
2548 tem = fold_ignored_result (arg);
2549 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2551 default:
2552 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2553 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2554 gcc_unreachable ();
2556 fold_convert_exit:
2557 protected_set_expr_location_unshare (tem, loc);
2558 return tem;
2561 /* Return false if expr can be assumed not to be an lvalue, true
2562 otherwise. */
2564 static bool
2565 maybe_lvalue_p (const_tree x)
2567 /* We only need to wrap lvalue tree codes. */
2568 switch (TREE_CODE (x))
2570 case VAR_DECL:
2571 case PARM_DECL:
2572 case RESULT_DECL:
2573 case LABEL_DECL:
2574 case FUNCTION_DECL:
2575 case SSA_NAME:
2577 case COMPONENT_REF:
2578 case MEM_REF:
2579 case INDIRECT_REF:
2580 case ARRAY_REF:
2581 case ARRAY_RANGE_REF:
2582 case BIT_FIELD_REF:
2583 case OBJ_TYPE_REF:
2585 case REALPART_EXPR:
2586 case IMAGPART_EXPR:
2587 case PREINCREMENT_EXPR:
2588 case PREDECREMENT_EXPR:
2589 case SAVE_EXPR:
2590 case TRY_CATCH_EXPR:
2591 case WITH_CLEANUP_EXPR:
2592 case COMPOUND_EXPR:
2593 case MODIFY_EXPR:
2594 case TARGET_EXPR:
2595 case COND_EXPR:
2596 case BIND_EXPR:
2597 break;
2599 default:
2600 /* Assume the worst for front-end tree codes. */
2601 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2602 break;
2603 return false;
2606 return true;
2609 /* Return an expr equal to X but certainly not valid as an lvalue. */
2611 tree
2612 non_lvalue_loc (location_t loc, tree x)
2614 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2615 us. */
2616 if (in_gimple_form)
2617 return x;
2619 if (! maybe_lvalue_p (x))
2620 return x;
2621 return build1_loc (loc, NON_LVALUE_EXPR, TREE_TYPE (x), x);
2624 /* When pedantic, return an expr equal to X but certainly not valid as a
2625 pedantic lvalue. Otherwise, return X. */
2627 static tree
2628 pedantic_non_lvalue_loc (location_t loc, tree x)
2630 return protected_set_expr_location_unshare (x, loc);
2633 /* Given a tree comparison code, return the code that is the logical inverse.
2634 It is generally not safe to do this for floating-point comparisons, except
2635 for EQ_EXPR, NE_EXPR, ORDERED_EXPR and UNORDERED_EXPR, so we return
2636 ERROR_MARK in this case. */
2638 enum tree_code
2639 invert_tree_comparison (enum tree_code code, bool honor_nans)
2641 if (honor_nans && flag_trapping_math && code != EQ_EXPR && code != NE_EXPR
2642 && code != ORDERED_EXPR && code != UNORDERED_EXPR)
2643 return ERROR_MARK;
2645 switch (code)
2647 case EQ_EXPR:
2648 return NE_EXPR;
2649 case NE_EXPR:
2650 return EQ_EXPR;
2651 case GT_EXPR:
2652 return honor_nans ? UNLE_EXPR : LE_EXPR;
2653 case GE_EXPR:
2654 return honor_nans ? UNLT_EXPR : LT_EXPR;
2655 case LT_EXPR:
2656 return honor_nans ? UNGE_EXPR : GE_EXPR;
2657 case LE_EXPR:
2658 return honor_nans ? UNGT_EXPR : GT_EXPR;
2659 case LTGT_EXPR:
2660 return UNEQ_EXPR;
2661 case UNEQ_EXPR:
2662 return LTGT_EXPR;
2663 case UNGT_EXPR:
2664 return LE_EXPR;
2665 case UNGE_EXPR:
2666 return LT_EXPR;
2667 case UNLT_EXPR:
2668 return GE_EXPR;
2669 case UNLE_EXPR:
2670 return GT_EXPR;
2671 case ORDERED_EXPR:
2672 return UNORDERED_EXPR;
2673 case UNORDERED_EXPR:
2674 return ORDERED_EXPR;
2675 default:
2676 gcc_unreachable ();
2680 /* Similar, but return the comparison that results if the operands are
2681 swapped. This is safe for floating-point. */
2683 enum tree_code
2684 swap_tree_comparison (enum tree_code code)
2686 switch (code)
2688 case EQ_EXPR:
2689 case NE_EXPR:
2690 case ORDERED_EXPR:
2691 case UNORDERED_EXPR:
2692 case LTGT_EXPR:
2693 case UNEQ_EXPR:
2694 return code;
2695 case GT_EXPR:
2696 return LT_EXPR;
2697 case GE_EXPR:
2698 return LE_EXPR;
2699 case LT_EXPR:
2700 return GT_EXPR;
2701 case LE_EXPR:
2702 return GE_EXPR;
2703 case UNGT_EXPR:
2704 return UNLT_EXPR;
2705 case UNGE_EXPR:
2706 return UNLE_EXPR;
2707 case UNLT_EXPR:
2708 return UNGT_EXPR;
2709 case UNLE_EXPR:
2710 return UNGE_EXPR;
2711 default:
2712 gcc_unreachable ();
2717 /* Convert a comparison tree code from an enum tree_code representation
2718 into a compcode bit-based encoding. This function is the inverse of
2719 compcode_to_comparison. */
2721 static enum comparison_code
2722 comparison_to_compcode (enum tree_code code)
2724 switch (code)
2726 case LT_EXPR:
2727 return COMPCODE_LT;
2728 case EQ_EXPR:
2729 return COMPCODE_EQ;
2730 case LE_EXPR:
2731 return COMPCODE_LE;
2732 case GT_EXPR:
2733 return COMPCODE_GT;
2734 case NE_EXPR:
2735 return COMPCODE_NE;
2736 case GE_EXPR:
2737 return COMPCODE_GE;
2738 case ORDERED_EXPR:
2739 return COMPCODE_ORD;
2740 case UNORDERED_EXPR:
2741 return COMPCODE_UNORD;
2742 case UNLT_EXPR:
2743 return COMPCODE_UNLT;
2744 case UNEQ_EXPR:
2745 return COMPCODE_UNEQ;
2746 case UNLE_EXPR:
2747 return COMPCODE_UNLE;
2748 case UNGT_EXPR:
2749 return COMPCODE_UNGT;
2750 case LTGT_EXPR:
2751 return COMPCODE_LTGT;
2752 case UNGE_EXPR:
2753 return COMPCODE_UNGE;
2754 default:
2755 gcc_unreachable ();
2759 /* Convert a compcode bit-based encoding of a comparison operator back
2760 to GCC's enum tree_code representation. This function is the
2761 inverse of comparison_to_compcode. */
2763 static enum tree_code
2764 compcode_to_comparison (enum comparison_code code)
2766 switch (code)
2768 case COMPCODE_LT:
2769 return LT_EXPR;
2770 case COMPCODE_EQ:
2771 return EQ_EXPR;
2772 case COMPCODE_LE:
2773 return LE_EXPR;
2774 case COMPCODE_GT:
2775 return GT_EXPR;
2776 case COMPCODE_NE:
2777 return NE_EXPR;
2778 case COMPCODE_GE:
2779 return GE_EXPR;
2780 case COMPCODE_ORD:
2781 return ORDERED_EXPR;
2782 case COMPCODE_UNORD:
2783 return UNORDERED_EXPR;
2784 case COMPCODE_UNLT:
2785 return UNLT_EXPR;
2786 case COMPCODE_UNEQ:
2787 return UNEQ_EXPR;
2788 case COMPCODE_UNLE:
2789 return UNLE_EXPR;
2790 case COMPCODE_UNGT:
2791 return UNGT_EXPR;
2792 case COMPCODE_LTGT:
2793 return LTGT_EXPR;
2794 case COMPCODE_UNGE:
2795 return UNGE_EXPR;
2796 default:
2797 gcc_unreachable ();
2801 /* Return true if COND1 tests the opposite condition of COND2. */
2803 bool
2804 inverse_conditions_p (const_tree cond1, const_tree cond2)
2806 return (COMPARISON_CLASS_P (cond1)
2807 && COMPARISON_CLASS_P (cond2)
2808 && (invert_tree_comparison
2809 (TREE_CODE (cond1),
2810 HONOR_NANS (TREE_OPERAND (cond1, 0))) == TREE_CODE (cond2))
2811 && operand_equal_p (TREE_OPERAND (cond1, 0),
2812 TREE_OPERAND (cond2, 0), 0)
2813 && operand_equal_p (TREE_OPERAND (cond1, 1),
2814 TREE_OPERAND (cond2, 1), 0));
2817 /* Return a tree for the comparison which is the combination of
2818 doing the AND or OR (depending on CODE) of the two operations LCODE
2819 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2820 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2821 if this makes the transformation invalid. */
2823 tree
2824 combine_comparisons (location_t loc,
2825 enum tree_code code, enum tree_code lcode,
2826 enum tree_code rcode, tree truth_type,
2827 tree ll_arg, tree lr_arg)
2829 bool honor_nans = HONOR_NANS (ll_arg);
2830 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2831 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2832 int compcode;
2834 switch (code)
2836 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2837 compcode = lcompcode & rcompcode;
2838 break;
2840 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2841 compcode = lcompcode | rcompcode;
2842 break;
2844 default:
2845 return NULL_TREE;
2848 if (!honor_nans)
2850 /* Eliminate unordered comparisons, as well as LTGT and ORD
2851 which are not used unless the mode has NaNs. */
2852 compcode &= ~COMPCODE_UNORD;
2853 if (compcode == COMPCODE_LTGT)
2854 compcode = COMPCODE_NE;
2855 else if (compcode == COMPCODE_ORD)
2856 compcode = COMPCODE_TRUE;
2858 else if (flag_trapping_math)
2860 /* Check that the original operation and the optimized ones will trap
2861 under the same condition. */
2862 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2863 && (lcompcode != COMPCODE_EQ)
2864 && (lcompcode != COMPCODE_ORD);
2865 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2866 && (rcompcode != COMPCODE_EQ)
2867 && (rcompcode != COMPCODE_ORD);
2868 bool trap = (compcode & COMPCODE_UNORD) == 0
2869 && (compcode != COMPCODE_EQ)
2870 && (compcode != COMPCODE_ORD);
2872 /* In a short-circuited boolean expression the LHS might be
2873 such that the RHS, if evaluated, will never trap. For
2874 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2875 if neither x nor y is NaN. (This is a mixed blessing: for
2876 example, the expression above will never trap, hence
2877 optimizing it to x < y would be invalid). */
2878 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2879 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2880 rtrap = false;
2882 /* If the comparison was short-circuited, and only the RHS
2883 trapped, we may now generate a spurious trap. */
2884 if (rtrap && !ltrap
2885 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2886 return NULL_TREE;
2888 /* If we changed the conditions that cause a trap, we lose. */
2889 if ((ltrap || rtrap) != trap)
2890 return NULL_TREE;
2893 if (compcode == COMPCODE_TRUE)
2894 return constant_boolean_node (true, truth_type);
2895 else if (compcode == COMPCODE_FALSE)
2896 return constant_boolean_node (false, truth_type);
2897 else
2899 enum tree_code tcode;
2901 tcode = compcode_to_comparison ((enum comparison_code) compcode);
2902 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
2906 /* Return nonzero if two operands (typically of the same tree node)
2907 are necessarily equal. FLAGS modifies behavior as follows:
2909 If OEP_ONLY_CONST is set, only return nonzero for constants.
2910 This function tests whether the operands are indistinguishable;
2911 it does not test whether they are equal using C's == operation.
2912 The distinction is important for IEEE floating point, because
2913 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2914 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2916 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2917 even though it may hold multiple values during a function.
2918 This is because a GCC tree node guarantees that nothing else is
2919 executed between the evaluation of its "operands" (which may often
2920 be evaluated in arbitrary order). Hence if the operands themselves
2921 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2922 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2923 unset means assuming isochronic (or instantaneous) tree equivalence.
2924 Unless comparing arbitrary expression trees, such as from different
2925 statements, this flag can usually be left unset.
2927 If OEP_PURE_SAME is set, then pure functions with identical arguments
2928 are considered the same. It is used when the caller has other ways
2929 to ensure that global memory is unchanged in between.
2931 If OEP_ADDRESS_OF is set, we are actually comparing addresses of objects,
2932 not values of expressions.
2934 If OEP_LEXICOGRAPHIC is set, then also handle expressions with side-effects
2935 such as MODIFY_EXPR, RETURN_EXPR, as well as STATEMENT_LISTs.
2937 Unless OEP_MATCH_SIDE_EFFECTS is set, the function returns false on
2938 any operand with side effect. This is unnecesarily conservative in the
2939 case we know that arg0 and arg1 are in disjoint code paths (such as in
2940 ?: operator). In addition OEP_MATCH_SIDE_EFFECTS is used when comparing
2941 addresses with TREE_CONSTANT flag set so we know that &var == &var
2942 even if var is volatile. */
2944 bool
2945 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2947 /* When checking, verify at the outermost operand_equal_p call that
2948 if operand_equal_p returns non-zero then ARG0 and ARG1 has the same
2949 hash value. */
2950 if (flag_checking && !(flags & OEP_NO_HASH_CHECK))
2952 if (operand_equal_p (arg0, arg1, flags | OEP_NO_HASH_CHECK))
2954 if (arg0 != arg1)
2956 inchash::hash hstate0 (0), hstate1 (0);
2957 inchash::add_expr (arg0, hstate0, flags | OEP_HASH_CHECK);
2958 inchash::add_expr (arg1, hstate1, flags | OEP_HASH_CHECK);
2959 hashval_t h0 = hstate0.end ();
2960 hashval_t h1 = hstate1.end ();
2961 gcc_assert (h0 == h1);
2963 return true;
2965 else
2966 return false;
2969 STRIP_ANY_LOCATION_WRAPPER (arg0);
2970 STRIP_ANY_LOCATION_WRAPPER (arg1);
2972 /* If either is ERROR_MARK, they aren't equal. */
2973 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK
2974 || TREE_TYPE (arg0) == error_mark_node
2975 || TREE_TYPE (arg1) == error_mark_node)
2976 return false;
2978 /* Similar, if either does not have a type (like a template id),
2979 they aren't equal. */
2980 if (!TREE_TYPE (arg0) || !TREE_TYPE (arg1))
2981 return false;
2983 /* We cannot consider pointers to different address space equal. */
2984 if (POINTER_TYPE_P (TREE_TYPE (arg0))
2985 && POINTER_TYPE_P (TREE_TYPE (arg1))
2986 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
2987 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
2988 return false;
2990 /* Check equality of integer constants before bailing out due to
2991 precision differences. */
2992 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2994 /* Address of INTEGER_CST is not defined; check that we did not forget
2995 to drop the OEP_ADDRESS_OF flags. */
2996 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
2997 return tree_int_cst_equal (arg0, arg1);
3000 if (!(flags & OEP_ADDRESS_OF))
3002 /* If both types don't have the same signedness, then we can't consider
3003 them equal. We must check this before the STRIP_NOPS calls
3004 because they may change the signedness of the arguments. As pointers
3005 strictly don't have a signedness, require either two pointers or
3006 two non-pointers as well. */
3007 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3008 || POINTER_TYPE_P (TREE_TYPE (arg0))
3009 != POINTER_TYPE_P (TREE_TYPE (arg1)))
3010 return false;
3012 /* If both types don't have the same precision, then it is not safe
3013 to strip NOPs. */
3014 if (element_precision (TREE_TYPE (arg0))
3015 != element_precision (TREE_TYPE (arg1)))
3016 return false;
3018 STRIP_NOPS (arg0);
3019 STRIP_NOPS (arg1);
3021 #if 0
3022 /* FIXME: Fortran FE currently produce ADDR_EXPR of NOP_EXPR. Enable the
3023 sanity check once the issue is solved. */
3024 else
3025 /* Addresses of conversions and SSA_NAMEs (and many other things)
3026 are not defined. Check that we did not forget to drop the
3027 OEP_ADDRESS_OF/OEP_CONSTANT_ADDRESS_OF flags. */
3028 gcc_checking_assert (!CONVERT_EXPR_P (arg0) && !CONVERT_EXPR_P (arg1)
3029 && TREE_CODE (arg0) != SSA_NAME);
3030 #endif
3032 /* In case both args are comparisons but with different comparison
3033 code, try to swap the comparison operands of one arg to produce
3034 a match and compare that variant. */
3035 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3036 && COMPARISON_CLASS_P (arg0)
3037 && COMPARISON_CLASS_P (arg1))
3039 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3041 if (TREE_CODE (arg0) == swap_code)
3042 return operand_equal_p (TREE_OPERAND (arg0, 0),
3043 TREE_OPERAND (arg1, 1), flags)
3044 && operand_equal_p (TREE_OPERAND (arg0, 1),
3045 TREE_OPERAND (arg1, 0), flags);
3048 if (TREE_CODE (arg0) != TREE_CODE (arg1))
3050 /* NOP_EXPR and CONVERT_EXPR are considered equal. */
3051 if (CONVERT_EXPR_P (arg0) && CONVERT_EXPR_P (arg1))
3053 else if (flags & OEP_ADDRESS_OF)
3055 /* If we are interested in comparing addresses ignore
3056 MEM_REF wrappings of the base that can appear just for
3057 TBAA reasons. */
3058 if (TREE_CODE (arg0) == MEM_REF
3059 && DECL_P (arg1)
3060 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR
3061 && TREE_OPERAND (TREE_OPERAND (arg0, 0), 0) == arg1
3062 && integer_zerop (TREE_OPERAND (arg0, 1)))
3063 return true;
3064 else if (TREE_CODE (arg1) == MEM_REF
3065 && DECL_P (arg0)
3066 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ADDR_EXPR
3067 && TREE_OPERAND (TREE_OPERAND (arg1, 0), 0) == arg0
3068 && integer_zerop (TREE_OPERAND (arg1, 1)))
3069 return true;
3070 return false;
3072 else
3073 return false;
3076 /* When not checking adddresses, this is needed for conversions and for
3077 COMPONENT_REF. Might as well play it safe and always test this. */
3078 if (TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3079 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3080 || (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))
3081 && !(flags & OEP_ADDRESS_OF)))
3082 return false;
3084 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3085 We don't care about side effects in that case because the SAVE_EXPR
3086 takes care of that for us. In all other cases, two expressions are
3087 equal if they have no side effects. If we have two identical
3088 expressions with side effects that should be treated the same due
3089 to the only side effects being identical SAVE_EXPR's, that will
3090 be detected in the recursive calls below.
3091 If we are taking an invariant address of two identical objects
3092 they are necessarily equal as well. */
3093 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3094 && (TREE_CODE (arg0) == SAVE_EXPR
3095 || (flags & OEP_MATCH_SIDE_EFFECTS)
3096 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3097 return true;
3099 /* Next handle constant cases, those for which we can return 1 even
3100 if ONLY_CONST is set. */
3101 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3102 switch (TREE_CODE (arg0))
3104 case INTEGER_CST:
3105 return tree_int_cst_equal (arg0, arg1);
3107 case FIXED_CST:
3108 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3109 TREE_FIXED_CST (arg1));
3111 case REAL_CST:
3112 if (real_identical (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1)))
3113 return true;
3116 if (!HONOR_SIGNED_ZEROS (arg0))
3118 /* If we do not distinguish between signed and unsigned zero,
3119 consider them equal. */
3120 if (real_zerop (arg0) && real_zerop (arg1))
3121 return true;
3123 return false;
3125 case VECTOR_CST:
3127 if (VECTOR_CST_LOG2_NPATTERNS (arg0)
3128 != VECTOR_CST_LOG2_NPATTERNS (arg1))
3129 return false;
3131 if (VECTOR_CST_NELTS_PER_PATTERN (arg0)
3132 != VECTOR_CST_NELTS_PER_PATTERN (arg1))
3133 return false;
3135 unsigned int count = vector_cst_encoded_nelts (arg0);
3136 for (unsigned int i = 0; i < count; ++i)
3137 if (!operand_equal_p (VECTOR_CST_ENCODED_ELT (arg0, i),
3138 VECTOR_CST_ENCODED_ELT (arg1, i), flags))
3139 return false;
3140 return true;
3143 case COMPLEX_CST:
3144 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3145 flags)
3146 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3147 flags));
3149 case STRING_CST:
3150 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3151 && ! memcmp (TREE_STRING_POINTER (arg0),
3152 TREE_STRING_POINTER (arg1),
3153 TREE_STRING_LENGTH (arg0)));
3155 case ADDR_EXPR:
3156 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3157 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3158 flags | OEP_ADDRESS_OF
3159 | OEP_MATCH_SIDE_EFFECTS);
3160 case CONSTRUCTOR:
3161 /* In GIMPLE empty constructors are allowed in initializers of
3162 aggregates. */
3163 return !CONSTRUCTOR_NELTS (arg0) && !CONSTRUCTOR_NELTS (arg1);
3164 default:
3165 break;
3168 if (flags & OEP_ONLY_CONST)
3169 return false;
3171 /* Define macros to test an operand from arg0 and arg1 for equality and a
3172 variant that allows null and views null as being different from any
3173 non-null value. In the latter case, if either is null, the both
3174 must be; otherwise, do the normal comparison. */
3175 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3176 TREE_OPERAND (arg1, N), flags)
3178 #define OP_SAME_WITH_NULL(N) \
3179 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3180 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3182 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3184 case tcc_unary:
3185 /* Two conversions are equal only if signedness and modes match. */
3186 switch (TREE_CODE (arg0))
3188 CASE_CONVERT:
3189 case FIX_TRUNC_EXPR:
3190 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3191 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3192 return false;
3193 break;
3194 default:
3195 break;
3198 return OP_SAME (0);
3201 case tcc_comparison:
3202 case tcc_binary:
3203 if (OP_SAME (0) && OP_SAME (1))
3204 return true;
3206 /* For commutative ops, allow the other order. */
3207 return (commutative_tree_code (TREE_CODE (arg0))
3208 && operand_equal_p (TREE_OPERAND (arg0, 0),
3209 TREE_OPERAND (arg1, 1), flags)
3210 && operand_equal_p (TREE_OPERAND (arg0, 1),
3211 TREE_OPERAND (arg1, 0), flags));
3213 case tcc_reference:
3214 /* If either of the pointer (or reference) expressions we are
3215 dereferencing contain a side effect, these cannot be equal,
3216 but their addresses can be. */
3217 if ((flags & OEP_MATCH_SIDE_EFFECTS) == 0
3218 && (TREE_SIDE_EFFECTS (arg0)
3219 || TREE_SIDE_EFFECTS (arg1)))
3220 return false;
3222 switch (TREE_CODE (arg0))
3224 case INDIRECT_REF:
3225 if (!(flags & OEP_ADDRESS_OF))
3227 if (TYPE_ALIGN (TREE_TYPE (arg0))
3228 != TYPE_ALIGN (TREE_TYPE (arg1)))
3229 return false;
3230 /* Verify that the access types are compatible. */
3231 if (TYPE_MAIN_VARIANT (TREE_TYPE (arg0))
3232 != TYPE_MAIN_VARIANT (TREE_TYPE (arg1)))
3233 return false;
3235 flags &= ~OEP_ADDRESS_OF;
3236 return OP_SAME (0);
3238 case IMAGPART_EXPR:
3239 /* Require the same offset. */
3240 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3241 TYPE_SIZE (TREE_TYPE (arg1)),
3242 flags & ~OEP_ADDRESS_OF))
3243 return false;
3245 /* Fallthru. */
3246 case REALPART_EXPR:
3247 case VIEW_CONVERT_EXPR:
3248 return OP_SAME (0);
3250 case TARGET_MEM_REF:
3251 case MEM_REF:
3252 if (!(flags & OEP_ADDRESS_OF))
3254 /* Require equal access sizes */
3255 if (TYPE_SIZE (TREE_TYPE (arg0)) != TYPE_SIZE (TREE_TYPE (arg1))
3256 && (!TYPE_SIZE (TREE_TYPE (arg0))
3257 || !TYPE_SIZE (TREE_TYPE (arg1))
3258 || !operand_equal_p (TYPE_SIZE (TREE_TYPE (arg0)),
3259 TYPE_SIZE (TREE_TYPE (arg1)),
3260 flags)))
3261 return false;
3262 /* Verify that access happens in similar types. */
3263 if (!types_compatible_p (TREE_TYPE (arg0), TREE_TYPE (arg1)))
3264 return false;
3265 /* Verify that accesses are TBAA compatible. */
3266 if (!alias_ptr_types_compatible_p
3267 (TREE_TYPE (TREE_OPERAND (arg0, 1)),
3268 TREE_TYPE (TREE_OPERAND (arg1, 1)))
3269 || (MR_DEPENDENCE_CLIQUE (arg0)
3270 != MR_DEPENDENCE_CLIQUE (arg1))
3271 || (MR_DEPENDENCE_BASE (arg0)
3272 != MR_DEPENDENCE_BASE (arg1)))
3273 return false;
3274 /* Verify that alignment is compatible. */
3275 if (TYPE_ALIGN (TREE_TYPE (arg0))
3276 != TYPE_ALIGN (TREE_TYPE (arg1)))
3277 return false;
3279 flags &= ~OEP_ADDRESS_OF;
3280 return (OP_SAME (0) && OP_SAME (1)
3281 /* TARGET_MEM_REF require equal extra operands. */
3282 && (TREE_CODE (arg0) != TARGET_MEM_REF
3283 || (OP_SAME_WITH_NULL (2)
3284 && OP_SAME_WITH_NULL (3)
3285 && OP_SAME_WITH_NULL (4))));
3287 case ARRAY_REF:
3288 case ARRAY_RANGE_REF:
3289 if (!OP_SAME (0))
3290 return false;
3291 flags &= ~OEP_ADDRESS_OF;
3292 /* Compare the array index by value if it is constant first as we
3293 may have different types but same value here. */
3294 return ((tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3295 TREE_OPERAND (arg1, 1))
3296 || OP_SAME (1))
3297 && OP_SAME_WITH_NULL (2)
3298 && OP_SAME_WITH_NULL (3)
3299 /* Compare low bound and element size as with OEP_ADDRESS_OF
3300 we have to account for the offset of the ref. */
3301 && (TREE_TYPE (TREE_OPERAND (arg0, 0))
3302 == TREE_TYPE (TREE_OPERAND (arg1, 0))
3303 || (operand_equal_p (array_ref_low_bound
3304 (CONST_CAST_TREE (arg0)),
3305 array_ref_low_bound
3306 (CONST_CAST_TREE (arg1)), flags)
3307 && operand_equal_p (array_ref_element_size
3308 (CONST_CAST_TREE (arg0)),
3309 array_ref_element_size
3310 (CONST_CAST_TREE (arg1)),
3311 flags))));
3313 case COMPONENT_REF:
3314 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3315 may be NULL when we're called to compare MEM_EXPRs. */
3316 if (!OP_SAME_WITH_NULL (0)
3317 || !OP_SAME (1))
3318 return false;
3319 flags &= ~OEP_ADDRESS_OF;
3320 return OP_SAME_WITH_NULL (2);
3322 case BIT_FIELD_REF:
3323 if (!OP_SAME (0))
3324 return false;
3325 flags &= ~OEP_ADDRESS_OF;
3326 return OP_SAME (1) && OP_SAME (2);
3328 default:
3329 return false;
3332 case tcc_expression:
3333 switch (TREE_CODE (arg0))
3335 case ADDR_EXPR:
3336 /* Be sure we pass right ADDRESS_OF flag. */
3337 gcc_checking_assert (!(flags & OEP_ADDRESS_OF));
3338 return operand_equal_p (TREE_OPERAND (arg0, 0),
3339 TREE_OPERAND (arg1, 0),
3340 flags | OEP_ADDRESS_OF);
3342 case TRUTH_NOT_EXPR:
3343 return OP_SAME (0);
3345 case TRUTH_ANDIF_EXPR:
3346 case TRUTH_ORIF_EXPR:
3347 return OP_SAME (0) && OP_SAME (1);
3349 case WIDEN_MULT_PLUS_EXPR:
3350 case WIDEN_MULT_MINUS_EXPR:
3351 if (!OP_SAME (2))
3352 return false;
3353 /* The multiplcation operands are commutative. */
3354 /* FALLTHRU */
3356 case TRUTH_AND_EXPR:
3357 case TRUTH_OR_EXPR:
3358 case TRUTH_XOR_EXPR:
3359 if (OP_SAME (0) && OP_SAME (1))
3360 return true;
3362 /* Otherwise take into account this is a commutative operation. */
3363 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3364 TREE_OPERAND (arg1, 1), flags)
3365 && operand_equal_p (TREE_OPERAND (arg0, 1),
3366 TREE_OPERAND (arg1, 0), flags));
3368 case COND_EXPR:
3369 if (! OP_SAME (1) || ! OP_SAME_WITH_NULL (2))
3370 return false;
3371 flags &= ~OEP_ADDRESS_OF;
3372 return OP_SAME (0);
3374 case BIT_INSERT_EXPR:
3375 /* BIT_INSERT_EXPR has an implict operand as the type precision
3376 of op1. Need to check to make sure they are the same. */
3377 if (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
3378 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
3379 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 1)))
3380 != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 1))))
3381 return false;
3382 /* FALLTHRU */
3384 case VEC_COND_EXPR:
3385 case DOT_PROD_EXPR:
3386 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3388 case MODIFY_EXPR:
3389 case INIT_EXPR:
3390 case COMPOUND_EXPR:
3391 case PREDECREMENT_EXPR:
3392 case PREINCREMENT_EXPR:
3393 case POSTDECREMENT_EXPR:
3394 case POSTINCREMENT_EXPR:
3395 if (flags & OEP_LEXICOGRAPHIC)
3396 return OP_SAME (0) && OP_SAME (1);
3397 return false;
3399 case CLEANUP_POINT_EXPR:
3400 case EXPR_STMT:
3401 case SAVE_EXPR:
3402 if (flags & OEP_LEXICOGRAPHIC)
3403 return OP_SAME (0);
3404 return false;
3406 default:
3407 return false;
3410 case tcc_vl_exp:
3411 switch (TREE_CODE (arg0))
3413 case CALL_EXPR:
3414 if ((CALL_EXPR_FN (arg0) == NULL_TREE)
3415 != (CALL_EXPR_FN (arg1) == NULL_TREE))
3416 /* If not both CALL_EXPRs are either internal or normal function
3417 functions, then they are not equal. */
3418 return false;
3419 else if (CALL_EXPR_FN (arg0) == NULL_TREE)
3421 /* If the CALL_EXPRs call different internal functions, then they
3422 are not equal. */
3423 if (CALL_EXPR_IFN (arg0) != CALL_EXPR_IFN (arg1))
3424 return false;
3426 else
3428 /* If the CALL_EXPRs call different functions, then they are not
3429 equal. */
3430 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3431 flags))
3432 return false;
3435 /* FIXME: We could skip this test for OEP_MATCH_SIDE_EFFECTS. */
3437 unsigned int cef = call_expr_flags (arg0);
3438 if (flags & OEP_PURE_SAME)
3439 cef &= ECF_CONST | ECF_PURE;
3440 else
3441 cef &= ECF_CONST;
3442 if (!cef && !(flags & OEP_LEXICOGRAPHIC))
3443 return false;
3446 /* Now see if all the arguments are the same. */
3448 const_call_expr_arg_iterator iter0, iter1;
3449 const_tree a0, a1;
3450 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3451 a1 = first_const_call_expr_arg (arg1, &iter1);
3452 a0 && a1;
3453 a0 = next_const_call_expr_arg (&iter0),
3454 a1 = next_const_call_expr_arg (&iter1))
3455 if (! operand_equal_p (a0, a1, flags))
3456 return false;
3458 /* If we get here and both argument lists are exhausted
3459 then the CALL_EXPRs are equal. */
3460 return ! (a0 || a1);
3462 default:
3463 return false;
3466 case tcc_declaration:
3467 /* Consider __builtin_sqrt equal to sqrt. */
3468 return (TREE_CODE (arg0) == FUNCTION_DECL
3469 && fndecl_built_in_p (arg0) && fndecl_built_in_p (arg1)
3470 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3471 && (DECL_UNCHECKED_FUNCTION_CODE (arg0)
3472 == DECL_UNCHECKED_FUNCTION_CODE (arg1)));
3474 case tcc_exceptional:
3475 if (TREE_CODE (arg0) == CONSTRUCTOR)
3477 /* In GIMPLE constructors are used only to build vectors from
3478 elements. Individual elements in the constructor must be
3479 indexed in increasing order and form an initial sequence.
3481 We make no effort to compare constructors in generic.
3482 (see sem_variable::equals in ipa-icf which can do so for
3483 constants). */
3484 if (!VECTOR_TYPE_P (TREE_TYPE (arg0))
3485 || !VECTOR_TYPE_P (TREE_TYPE (arg1)))
3486 return false;
3488 /* Be sure that vectors constructed have the same representation.
3489 We only tested element precision and modes to match.
3490 Vectors may be BLKmode and thus also check that the number of
3491 parts match. */
3492 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)),
3493 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1))))
3494 return false;
3496 vec<constructor_elt, va_gc> *v0 = CONSTRUCTOR_ELTS (arg0);
3497 vec<constructor_elt, va_gc> *v1 = CONSTRUCTOR_ELTS (arg1);
3498 unsigned int len = vec_safe_length (v0);
3500 if (len != vec_safe_length (v1))
3501 return false;
3503 for (unsigned int i = 0; i < len; i++)
3505 constructor_elt *c0 = &(*v0)[i];
3506 constructor_elt *c1 = &(*v1)[i];
3508 if (!operand_equal_p (c0->value, c1->value, flags)
3509 /* In GIMPLE the indexes can be either NULL or matching i.
3510 Double check this so we won't get false
3511 positives for GENERIC. */
3512 || (c0->index
3513 && (TREE_CODE (c0->index) != INTEGER_CST
3514 || compare_tree_int (c0->index, i)))
3515 || (c1->index
3516 && (TREE_CODE (c1->index) != INTEGER_CST
3517 || compare_tree_int (c1->index, i))))
3518 return false;
3520 return true;
3522 else if (TREE_CODE (arg0) == STATEMENT_LIST
3523 && (flags & OEP_LEXICOGRAPHIC))
3525 /* Compare the STATEMENT_LISTs. */
3526 tree_stmt_iterator tsi1, tsi2;
3527 tree body1 = CONST_CAST_TREE (arg0);
3528 tree body2 = CONST_CAST_TREE (arg1);
3529 for (tsi1 = tsi_start (body1), tsi2 = tsi_start (body2); ;
3530 tsi_next (&tsi1), tsi_next (&tsi2))
3532 /* The lists don't have the same number of statements. */
3533 if (tsi_end_p (tsi1) ^ tsi_end_p (tsi2))
3534 return false;
3535 if (tsi_end_p (tsi1) && tsi_end_p (tsi2))
3536 return true;
3537 if (!operand_equal_p (tsi_stmt (tsi1), tsi_stmt (tsi2),
3538 flags & (OEP_LEXICOGRAPHIC
3539 | OEP_NO_HASH_CHECK)))
3540 return false;
3543 return false;
3545 case tcc_statement:
3546 switch (TREE_CODE (arg0))
3548 case RETURN_EXPR:
3549 if (flags & OEP_LEXICOGRAPHIC)
3550 return OP_SAME_WITH_NULL (0);
3551 return false;
3552 case DEBUG_BEGIN_STMT:
3553 if (flags & OEP_LEXICOGRAPHIC)
3554 return true;
3555 return false;
3556 default:
3557 return false;
3560 default:
3561 return false;
3564 #undef OP_SAME
3565 #undef OP_SAME_WITH_NULL
3568 /* Similar to operand_equal_p, but see if ARG0 might be a variant of ARG1
3569 with a different signedness or a narrower precision. */
3571 static bool
3572 operand_equal_for_comparison_p (tree arg0, tree arg1)
3574 if (operand_equal_p (arg0, arg1, 0))
3575 return true;
3577 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3578 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3579 return false;
3581 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3582 and see if the inner values are the same. This removes any
3583 signedness comparison, which doesn't matter here. */
3584 tree op0 = arg0;
3585 tree op1 = arg1;
3586 STRIP_NOPS (op0);
3587 STRIP_NOPS (op1);
3588 if (operand_equal_p (op0, op1, 0))
3589 return true;
3591 /* Discard a single widening conversion from ARG1 and see if the inner
3592 value is the same as ARG0. */
3593 if (CONVERT_EXPR_P (arg1)
3594 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3595 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)))
3596 < TYPE_PRECISION (TREE_TYPE (arg1))
3597 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
3598 return true;
3600 return false;
3603 /* See if ARG is an expression that is either a comparison or is performing
3604 arithmetic on comparisons. The comparisons must only be comparing
3605 two different values, which will be stored in *CVAL1 and *CVAL2; if
3606 they are nonzero it means that some operands have already been found.
3607 No variables may be used anywhere else in the expression except in the
3608 comparisons.
3610 If this is true, return 1. Otherwise, return zero. */
3612 static bool
3613 twoval_comparison_p (tree arg, tree *cval1, tree *cval2)
3615 enum tree_code code = TREE_CODE (arg);
3616 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3618 /* We can handle some of the tcc_expression cases here. */
3619 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3620 tclass = tcc_unary;
3621 else if (tclass == tcc_expression
3622 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3623 || code == COMPOUND_EXPR))
3624 tclass = tcc_binary;
3626 switch (tclass)
3628 case tcc_unary:
3629 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2);
3631 case tcc_binary:
3632 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
3633 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2));
3635 case tcc_constant:
3636 return true;
3638 case tcc_expression:
3639 if (code == COND_EXPR)
3640 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2)
3641 && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)
3642 && twoval_comparison_p (TREE_OPERAND (arg, 2), cval1, cval2));
3643 return false;
3645 case tcc_comparison:
3646 /* First see if we can handle the first operand, then the second. For
3647 the second operand, we know *CVAL1 can't be zero. It must be that
3648 one side of the comparison is each of the values; test for the
3649 case where this isn't true by failing if the two operands
3650 are the same. */
3652 if (operand_equal_p (TREE_OPERAND (arg, 0),
3653 TREE_OPERAND (arg, 1), 0))
3654 return false;
3656 if (*cval1 == 0)
3657 *cval1 = TREE_OPERAND (arg, 0);
3658 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3660 else if (*cval2 == 0)
3661 *cval2 = TREE_OPERAND (arg, 0);
3662 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3664 else
3665 return false;
3667 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3669 else if (*cval2 == 0)
3670 *cval2 = TREE_OPERAND (arg, 1);
3671 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3673 else
3674 return false;
3676 return true;
3678 default:
3679 return false;
3683 /* ARG is a tree that is known to contain just arithmetic operations and
3684 comparisons. Evaluate the operations in the tree substituting NEW0 for
3685 any occurrence of OLD0 as an operand of a comparison and likewise for
3686 NEW1 and OLD1. */
3688 static tree
3689 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3690 tree old1, tree new1)
3692 tree type = TREE_TYPE (arg);
3693 enum tree_code code = TREE_CODE (arg);
3694 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3696 /* We can handle some of the tcc_expression cases here. */
3697 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3698 tclass = tcc_unary;
3699 else if (tclass == tcc_expression
3700 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3701 tclass = tcc_binary;
3703 switch (tclass)
3705 case tcc_unary:
3706 return fold_build1_loc (loc, code, type,
3707 eval_subst (loc, TREE_OPERAND (arg, 0),
3708 old0, new0, old1, new1));
3710 case tcc_binary:
3711 return fold_build2_loc (loc, code, type,
3712 eval_subst (loc, TREE_OPERAND (arg, 0),
3713 old0, new0, old1, new1),
3714 eval_subst (loc, TREE_OPERAND (arg, 1),
3715 old0, new0, old1, new1));
3717 case tcc_expression:
3718 switch (code)
3720 case SAVE_EXPR:
3721 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3722 old1, new1);
3724 case COMPOUND_EXPR:
3725 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3726 old1, new1);
3728 case COND_EXPR:
3729 return fold_build3_loc (loc, code, type,
3730 eval_subst (loc, TREE_OPERAND (arg, 0),
3731 old0, new0, old1, new1),
3732 eval_subst (loc, TREE_OPERAND (arg, 1),
3733 old0, new0, old1, new1),
3734 eval_subst (loc, TREE_OPERAND (arg, 2),
3735 old0, new0, old1, new1));
3736 default:
3737 break;
3739 /* Fall through - ??? */
3741 case tcc_comparison:
3743 tree arg0 = TREE_OPERAND (arg, 0);
3744 tree arg1 = TREE_OPERAND (arg, 1);
3746 /* We need to check both for exact equality and tree equality. The
3747 former will be true if the operand has a side-effect. In that
3748 case, we know the operand occurred exactly once. */
3750 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3751 arg0 = new0;
3752 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3753 arg0 = new1;
3755 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3756 arg1 = new0;
3757 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3758 arg1 = new1;
3760 return fold_build2_loc (loc, code, type, arg0, arg1);
3763 default:
3764 return arg;
3768 /* Return a tree for the case when the result of an expression is RESULT
3769 converted to TYPE and OMITTED was previously an operand of the expression
3770 but is now not needed (e.g., we folded OMITTED * 0).
3772 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3773 the conversion of RESULT to TYPE. */
3775 tree
3776 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3778 tree t = fold_convert_loc (loc, type, result);
3780 /* If the resulting operand is an empty statement, just return the omitted
3781 statement casted to void. */
3782 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3783 return build1_loc (loc, NOP_EXPR, void_type_node,
3784 fold_ignored_result (omitted));
3786 if (TREE_SIDE_EFFECTS (omitted))
3787 return build2_loc (loc, COMPOUND_EXPR, type,
3788 fold_ignored_result (omitted), t);
3790 return non_lvalue_loc (loc, t);
3793 /* Return a tree for the case when the result of an expression is RESULT
3794 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3795 of the expression but are now not needed.
3797 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3798 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3799 evaluated before OMITTED2. Otherwise, if neither has side effects,
3800 just do the conversion of RESULT to TYPE. */
3802 tree
3803 omit_two_operands_loc (location_t loc, tree type, tree result,
3804 tree omitted1, tree omitted2)
3806 tree t = fold_convert_loc (loc, type, result);
3808 if (TREE_SIDE_EFFECTS (omitted2))
3809 t = build2_loc (loc, COMPOUND_EXPR, type, omitted2, t);
3810 if (TREE_SIDE_EFFECTS (omitted1))
3811 t = build2_loc (loc, COMPOUND_EXPR, type, omitted1, t);
3813 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3817 /* Return a simplified tree node for the truth-negation of ARG. This
3818 never alters ARG itself. We assume that ARG is an operation that
3819 returns a truth value (0 or 1).
3821 FIXME: one would think we would fold the result, but it causes
3822 problems with the dominator optimizer. */
3824 static tree
3825 fold_truth_not_expr (location_t loc, tree arg)
3827 tree type = TREE_TYPE (arg);
3828 enum tree_code code = TREE_CODE (arg);
3829 location_t loc1, loc2;
3831 /* If this is a comparison, we can simply invert it, except for
3832 floating-point non-equality comparisons, in which case we just
3833 enclose a TRUTH_NOT_EXPR around what we have. */
3835 if (TREE_CODE_CLASS (code) == tcc_comparison)
3837 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3838 if (FLOAT_TYPE_P (op_type)
3839 && flag_trapping_math
3840 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3841 && code != NE_EXPR && code != EQ_EXPR)
3842 return NULL_TREE;
3844 code = invert_tree_comparison (code, HONOR_NANS (op_type));
3845 if (code == ERROR_MARK)
3846 return NULL_TREE;
3848 tree ret = build2_loc (loc, code, type, TREE_OPERAND (arg, 0),
3849 TREE_OPERAND (arg, 1));
3850 if (TREE_NO_WARNING (arg))
3851 TREE_NO_WARNING (ret) = 1;
3852 return ret;
3855 switch (code)
3857 case INTEGER_CST:
3858 return constant_boolean_node (integer_zerop (arg), type);
3860 case TRUTH_AND_EXPR:
3861 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3862 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3863 return build2_loc (loc, TRUTH_OR_EXPR, type,
3864 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3865 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3867 case TRUTH_OR_EXPR:
3868 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3869 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3870 return build2_loc (loc, TRUTH_AND_EXPR, type,
3871 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3872 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3874 case TRUTH_XOR_EXPR:
3875 /* Here we can invert either operand. We invert the first operand
3876 unless the second operand is a TRUTH_NOT_EXPR in which case our
3877 result is the XOR of the first operand with the inside of the
3878 negation of the second operand. */
3880 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3881 return build2_loc (loc, TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3882 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3883 else
3884 return build2_loc (loc, TRUTH_XOR_EXPR, type,
3885 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3886 TREE_OPERAND (arg, 1));
3888 case TRUTH_ANDIF_EXPR:
3889 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3890 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3891 return build2_loc (loc, TRUTH_ORIF_EXPR, type,
3892 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3893 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3895 case TRUTH_ORIF_EXPR:
3896 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3897 loc2 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3898 return build2_loc (loc, TRUTH_ANDIF_EXPR, type,
3899 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3900 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3902 case TRUTH_NOT_EXPR:
3903 return TREE_OPERAND (arg, 0);
3905 case COND_EXPR:
3907 tree arg1 = TREE_OPERAND (arg, 1);
3908 tree arg2 = TREE_OPERAND (arg, 2);
3910 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3911 loc2 = expr_location_or (TREE_OPERAND (arg, 2), loc);
3913 /* A COND_EXPR may have a throw as one operand, which
3914 then has void type. Just leave void operands
3915 as they are. */
3916 return build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg, 0),
3917 VOID_TYPE_P (TREE_TYPE (arg1))
3918 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3919 VOID_TYPE_P (TREE_TYPE (arg2))
3920 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3923 case COMPOUND_EXPR:
3924 loc1 = expr_location_or (TREE_OPERAND (arg, 1), loc);
3925 return build2_loc (loc, COMPOUND_EXPR, type,
3926 TREE_OPERAND (arg, 0),
3927 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3929 case NON_LVALUE_EXPR:
3930 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3931 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3933 CASE_CONVERT:
3934 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3935 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3937 /* fall through */
3939 case FLOAT_EXPR:
3940 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3941 return build1_loc (loc, TREE_CODE (arg), type,
3942 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3944 case BIT_AND_EXPR:
3945 if (!integer_onep (TREE_OPERAND (arg, 1)))
3946 return NULL_TREE;
3947 return build2_loc (loc, EQ_EXPR, type, arg, build_int_cst (type, 0));
3949 case SAVE_EXPR:
3950 return build1_loc (loc, TRUTH_NOT_EXPR, type, arg);
3952 case CLEANUP_POINT_EXPR:
3953 loc1 = expr_location_or (TREE_OPERAND (arg, 0), loc);
3954 return build1_loc (loc, CLEANUP_POINT_EXPR, type,
3955 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3957 default:
3958 return NULL_TREE;
3962 /* Fold the truth-negation of ARG. This never alters ARG itself. We
3963 assume that ARG is an operation that returns a truth value (0 or 1
3964 for scalars, 0 or -1 for vectors). Return the folded expression if
3965 folding is successful. Otherwise, return NULL_TREE. */
3967 static tree
3968 fold_invert_truthvalue (location_t loc, tree arg)
3970 tree type = TREE_TYPE (arg);
3971 return fold_unary_loc (loc, VECTOR_TYPE_P (type)
3972 ? BIT_NOT_EXPR
3973 : TRUTH_NOT_EXPR,
3974 type, arg);
3977 /* Return a simplified tree node for the truth-negation of ARG. This
3978 never alters ARG itself. We assume that ARG is an operation that
3979 returns a truth value (0 or 1 for scalars, 0 or -1 for vectors). */
3981 tree
3982 invert_truthvalue_loc (location_t loc, tree arg)
3984 if (TREE_CODE (arg) == ERROR_MARK)
3985 return arg;
3987 tree type = TREE_TYPE (arg);
3988 return fold_build1_loc (loc, VECTOR_TYPE_P (type)
3989 ? BIT_NOT_EXPR
3990 : TRUTH_NOT_EXPR,
3991 type, arg);
3994 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3995 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero
3996 and uses reverse storage order if REVERSEP is nonzero. ORIG_INNER
3997 is the original memory reference used to preserve the alias set of
3998 the access. */
4000 static tree
4001 make_bit_field_ref (location_t loc, tree inner, tree orig_inner, tree type,
4002 HOST_WIDE_INT bitsize, poly_int64 bitpos,
4003 int unsignedp, int reversep)
4005 tree result, bftype;
4007 /* Attempt not to lose the access path if possible. */
4008 if (TREE_CODE (orig_inner) == COMPONENT_REF)
4010 tree ninner = TREE_OPERAND (orig_inner, 0);
4011 machine_mode nmode;
4012 poly_int64 nbitsize, nbitpos;
4013 tree noffset;
4014 int nunsignedp, nreversep, nvolatilep = 0;
4015 tree base = get_inner_reference (ninner, &nbitsize, &nbitpos,
4016 &noffset, &nmode, &nunsignedp,
4017 &nreversep, &nvolatilep);
4018 if (base == inner
4019 && noffset == NULL_TREE
4020 && known_subrange_p (bitpos, bitsize, nbitpos, nbitsize)
4021 && !reversep
4022 && !nreversep
4023 && !nvolatilep)
4025 inner = ninner;
4026 bitpos -= nbitpos;
4030 alias_set_type iset = get_alias_set (orig_inner);
4031 if (iset == 0 && get_alias_set (inner) != iset)
4032 inner = fold_build2 (MEM_REF, TREE_TYPE (inner),
4033 build_fold_addr_expr (inner),
4034 build_int_cst (ptr_type_node, 0));
4036 if (known_eq (bitpos, 0) && !reversep)
4038 tree size = TYPE_SIZE (TREE_TYPE (inner));
4039 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4040 || POINTER_TYPE_P (TREE_TYPE (inner)))
4041 && tree_fits_shwi_p (size)
4042 && tree_to_shwi (size) == bitsize)
4043 return fold_convert_loc (loc, type, inner);
4046 bftype = type;
4047 if (TYPE_PRECISION (bftype) != bitsize
4048 || TYPE_UNSIGNED (bftype) == !unsignedp)
4049 bftype = build_nonstandard_integer_type (bitsize, 0);
4051 result = build3_loc (loc, BIT_FIELD_REF, bftype, inner,
4052 bitsize_int (bitsize), bitsize_int (bitpos));
4053 REF_REVERSE_STORAGE_ORDER (result) = reversep;
4055 if (bftype != type)
4056 result = fold_convert_loc (loc, type, result);
4058 return result;
4061 /* Optimize a bit-field compare.
4063 There are two cases: First is a compare against a constant and the
4064 second is a comparison of two items where the fields are at the same
4065 bit position relative to the start of a chunk (byte, halfword, word)
4066 large enough to contain it. In these cases we can avoid the shift
4067 implicit in bitfield extractions.
4069 For constants, we emit a compare of the shifted constant with the
4070 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4071 compared. For two fields at the same position, we do the ANDs with the
4072 similar mask and compare the result of the ANDs.
4074 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4075 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4076 are the left and right operands of the comparison, respectively.
4078 If the optimization described above can be done, we return the resulting
4079 tree. Otherwise we return zero. */
4081 static tree
4082 optimize_bit_field_compare (location_t loc, enum tree_code code,
4083 tree compare_type, tree lhs, tree rhs)
4085 poly_int64 plbitpos, plbitsize, rbitpos, rbitsize;
4086 HOST_WIDE_INT lbitpos, lbitsize, nbitpos, nbitsize;
4087 tree type = TREE_TYPE (lhs);
4088 tree unsigned_type;
4089 int const_p = TREE_CODE (rhs) == INTEGER_CST;
4090 machine_mode lmode, rmode;
4091 scalar_int_mode nmode;
4092 int lunsignedp, runsignedp;
4093 int lreversep, rreversep;
4094 int lvolatilep = 0, rvolatilep = 0;
4095 tree linner, rinner = NULL_TREE;
4096 tree mask;
4097 tree offset;
4099 /* Get all the information about the extractions being done. If the bit size
4100 is the same as the size of the underlying object, we aren't doing an
4101 extraction at all and so can do nothing. We also don't want to
4102 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4103 then will no longer be able to replace it. */
4104 linner = get_inner_reference (lhs, &plbitsize, &plbitpos, &offset, &lmode,
4105 &lunsignedp, &lreversep, &lvolatilep);
4106 if (linner == lhs
4107 || !known_size_p (plbitsize)
4108 || !plbitsize.is_constant (&lbitsize)
4109 || !plbitpos.is_constant (&lbitpos)
4110 || known_eq (lbitsize, GET_MODE_BITSIZE (lmode))
4111 || offset != 0
4112 || TREE_CODE (linner) == PLACEHOLDER_EXPR
4113 || lvolatilep)
4114 return 0;
4116 if (const_p)
4117 rreversep = lreversep;
4118 else
4120 /* If this is not a constant, we can only do something if bit positions,
4121 sizes, signedness and storage order are the same. */
4122 rinner
4123 = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4124 &runsignedp, &rreversep, &rvolatilep);
4126 if (rinner == rhs
4127 || maybe_ne (lbitpos, rbitpos)
4128 || maybe_ne (lbitsize, rbitsize)
4129 || lunsignedp != runsignedp
4130 || lreversep != rreversep
4131 || offset != 0
4132 || TREE_CODE (rinner) == PLACEHOLDER_EXPR
4133 || rvolatilep)
4134 return 0;
4137 /* Honor the C++ memory model and mimic what RTL expansion does. */
4138 poly_uint64 bitstart = 0;
4139 poly_uint64 bitend = 0;
4140 if (TREE_CODE (lhs) == COMPONENT_REF)
4142 get_bit_range (&bitstart, &bitend, lhs, &plbitpos, &offset);
4143 if (!plbitpos.is_constant (&lbitpos) || offset != NULL_TREE)
4144 return 0;
4147 /* See if we can find a mode to refer to this field. We should be able to,
4148 but fail if we can't. */
4149 if (!get_best_mode (lbitsize, lbitpos, bitstart, bitend,
4150 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4151 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4152 TYPE_ALIGN (TREE_TYPE (rinner))),
4153 BITS_PER_WORD, false, &nmode))
4154 return 0;
4156 /* Set signed and unsigned types of the precision of this mode for the
4157 shifts below. */
4158 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4160 /* Compute the bit position and size for the new reference and our offset
4161 within it. If the new reference is the same size as the original, we
4162 won't optimize anything, so return zero. */
4163 nbitsize = GET_MODE_BITSIZE (nmode);
4164 nbitpos = lbitpos & ~ (nbitsize - 1);
4165 lbitpos -= nbitpos;
4166 if (nbitsize == lbitsize)
4167 return 0;
4169 if (lreversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
4170 lbitpos = nbitsize - lbitsize - lbitpos;
4172 /* Make the mask to be used against the extracted field. */
4173 mask = build_int_cst_type (unsigned_type, -1);
4174 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize));
4175 mask = const_binop (RSHIFT_EXPR, mask,
4176 size_int (nbitsize - lbitsize - lbitpos));
4178 if (! const_p)
4180 if (nbitpos < 0)
4181 return 0;
4183 /* If not comparing with constant, just rework the comparison
4184 and return. */
4185 tree t1 = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4186 nbitsize, nbitpos, 1, lreversep);
4187 t1 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t1, mask);
4188 tree t2 = make_bit_field_ref (loc, rinner, rhs, unsigned_type,
4189 nbitsize, nbitpos, 1, rreversep);
4190 t2 = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type, t2, mask);
4191 return fold_build2_loc (loc, code, compare_type, t1, t2);
4194 /* Otherwise, we are handling the constant case. See if the constant is too
4195 big for the field. Warn and return a tree for 0 (false) if so. We do
4196 this not only for its own sake, but to avoid having to test for this
4197 error case below. If we didn't, we might generate wrong code.
4199 For unsigned fields, the constant shifted right by the field length should
4200 be all zero. For signed fields, the high-order bits should agree with
4201 the sign bit. */
4203 if (lunsignedp)
4205 if (wi::lrshift (wi::to_wide (rhs), lbitsize) != 0)
4207 warning (0, "comparison is always %d due to width of bit-field",
4208 code == NE_EXPR);
4209 return constant_boolean_node (code == NE_EXPR, compare_type);
4212 else
4214 wide_int tem = wi::arshift (wi::to_wide (rhs), lbitsize - 1);
4215 if (tem != 0 && tem != -1)
4217 warning (0, "comparison is always %d due to width of bit-field",
4218 code == NE_EXPR);
4219 return constant_boolean_node (code == NE_EXPR, compare_type);
4223 if (nbitpos < 0)
4224 return 0;
4226 /* Single-bit compares should always be against zero. */
4227 if (lbitsize == 1 && ! integer_zerop (rhs))
4229 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4230 rhs = build_int_cst (type, 0);
4233 /* Make a new bitfield reference, shift the constant over the
4234 appropriate number of bits and mask it with the computed mask
4235 (in case this was a signed field). If we changed it, make a new one. */
4236 lhs = make_bit_field_ref (loc, linner, lhs, unsigned_type,
4237 nbitsize, nbitpos, 1, lreversep);
4239 rhs = const_binop (BIT_AND_EXPR,
4240 const_binop (LSHIFT_EXPR,
4241 fold_convert_loc (loc, unsigned_type, rhs),
4242 size_int (lbitpos)),
4243 mask);
4245 lhs = build2_loc (loc, code, compare_type,
4246 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask), rhs);
4247 return lhs;
4250 /* Subroutine for fold_truth_andor_1: decode a field reference.
4252 If EXP is a comparison reference, we return the innermost reference.
4254 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4255 set to the starting bit number.
4257 If the innermost field can be completely contained in a mode-sized
4258 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4260 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4261 otherwise it is not changed.
4263 *PUNSIGNEDP is set to the signedness of the field.
4265 *PREVERSEP is set to the storage order of the field.
4267 *PMASK is set to the mask used. This is either contained in a
4268 BIT_AND_EXPR or derived from the width of the field.
4270 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4272 Return 0 if this is not a component reference or is one that we can't
4273 do anything with. */
4275 static tree
4276 decode_field_reference (location_t loc, tree *exp_, HOST_WIDE_INT *pbitsize,
4277 HOST_WIDE_INT *pbitpos, machine_mode *pmode,
4278 int *punsignedp, int *preversep, int *pvolatilep,
4279 tree *pmask, tree *pand_mask)
4281 tree exp = *exp_;
4282 tree outer_type = 0;
4283 tree and_mask = 0;
4284 tree mask, inner, offset;
4285 tree unsigned_type;
4286 unsigned int precision;
4288 /* All the optimizations using this function assume integer fields.
4289 There are problems with FP fields since the type_for_size call
4290 below can fail for, e.g., XFmode. */
4291 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4292 return NULL_TREE;
4294 /* We are interested in the bare arrangement of bits, so strip everything
4295 that doesn't affect the machine mode. However, record the type of the
4296 outermost expression if it may matter below. */
4297 if (CONVERT_EXPR_P (exp)
4298 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4299 outer_type = TREE_TYPE (exp);
4300 STRIP_NOPS (exp);
4302 if (TREE_CODE (exp) == BIT_AND_EXPR)
4304 and_mask = TREE_OPERAND (exp, 1);
4305 exp = TREE_OPERAND (exp, 0);
4306 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4307 if (TREE_CODE (and_mask) != INTEGER_CST)
4308 return NULL_TREE;
4311 poly_int64 poly_bitsize, poly_bitpos;
4312 inner = get_inner_reference (exp, &poly_bitsize, &poly_bitpos, &offset,
4313 pmode, punsignedp, preversep, pvolatilep);
4314 if ((inner == exp && and_mask == 0)
4315 || !poly_bitsize.is_constant (pbitsize)
4316 || !poly_bitpos.is_constant (pbitpos)
4317 || *pbitsize < 0
4318 || offset != 0
4319 || TREE_CODE (inner) == PLACEHOLDER_EXPR
4320 /* Reject out-of-bound accesses (PR79731). */
4321 || (! AGGREGATE_TYPE_P (TREE_TYPE (inner))
4322 && compare_tree_int (TYPE_SIZE (TREE_TYPE (inner)),
4323 *pbitpos + *pbitsize) < 0))
4324 return NULL_TREE;
4326 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4327 if (unsigned_type == NULL_TREE)
4328 return NULL_TREE;
4330 *exp_ = exp;
4332 /* If the number of bits in the reference is the same as the bitsize of
4333 the outer type, then the outer type gives the signedness. Otherwise
4334 (in case of a small bitfield) the signedness is unchanged. */
4335 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4336 *punsignedp = TYPE_UNSIGNED (outer_type);
4338 /* Compute the mask to access the bitfield. */
4339 precision = TYPE_PRECISION (unsigned_type);
4341 mask = build_int_cst_type (unsigned_type, -1);
4343 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4344 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize));
4346 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4347 if (and_mask != 0)
4348 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4349 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4351 *pmask = mask;
4352 *pand_mask = and_mask;
4353 return inner;
4356 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4357 bit positions and MASK is SIGNED. */
4359 static bool
4360 all_ones_mask_p (const_tree mask, unsigned int size)
4362 tree type = TREE_TYPE (mask);
4363 unsigned int precision = TYPE_PRECISION (type);
4365 /* If this function returns true when the type of the mask is
4366 UNSIGNED, then there will be errors. In particular see
4367 gcc.c-torture/execute/990326-1.c. There does not appear to be
4368 any documentation paper trail as to why this is so. But the pre
4369 wide-int worked with that restriction and it has been preserved
4370 here. */
4371 if (size > precision || TYPE_SIGN (type) == UNSIGNED)
4372 return false;
4374 return wi::mask (size, false, precision) == wi::to_wide (mask);
4377 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4378 represents the sign bit of EXP's type. If EXP represents a sign
4379 or zero extension, also test VAL against the unextended type.
4380 The return value is the (sub)expression whose sign bit is VAL,
4381 or NULL_TREE otherwise. */
4383 tree
4384 sign_bit_p (tree exp, const_tree val)
4386 int width;
4387 tree t;
4389 /* Tree EXP must have an integral type. */
4390 t = TREE_TYPE (exp);
4391 if (! INTEGRAL_TYPE_P (t))
4392 return NULL_TREE;
4394 /* Tree VAL must be an integer constant. */
4395 if (TREE_CODE (val) != INTEGER_CST
4396 || TREE_OVERFLOW (val))
4397 return NULL_TREE;
4399 width = TYPE_PRECISION (t);
4400 if (wi::only_sign_bit_p (wi::to_wide (val), width))
4401 return exp;
4403 /* Handle extension from a narrower type. */
4404 if (TREE_CODE (exp) == NOP_EXPR
4405 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4406 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4408 return NULL_TREE;
4411 /* Subroutine for fold_truth_andor_1: determine if an operand is simple enough
4412 to be evaluated unconditionally. */
4414 static bool
4415 simple_operand_p (const_tree exp)
4417 /* Strip any conversions that don't change the machine mode. */
4418 STRIP_NOPS (exp);
4420 return (CONSTANT_CLASS_P (exp)
4421 || TREE_CODE (exp) == SSA_NAME
4422 || (DECL_P (exp)
4423 && ! TREE_ADDRESSABLE (exp)
4424 && ! TREE_THIS_VOLATILE (exp)
4425 && ! DECL_NONLOCAL (exp)
4426 /* Don't regard global variables as simple. They may be
4427 allocated in ways unknown to the compiler (shared memory,
4428 #pragma weak, etc). */
4429 && ! TREE_PUBLIC (exp)
4430 && ! DECL_EXTERNAL (exp)
4431 /* Weakrefs are not safe to be read, since they can be NULL.
4432 They are !TREE_PUBLIC && !DECL_EXTERNAL but still
4433 have DECL_WEAK flag set. */
4434 && (! VAR_OR_FUNCTION_DECL_P (exp) || ! DECL_WEAK (exp))
4435 /* Loading a static variable is unduly expensive, but global
4436 registers aren't expensive. */
4437 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4440 /* Subroutine for fold_truth_andor: determine if an operand is simple enough
4441 to be evaluated unconditionally.
4442 I addition to simple_operand_p, we assume that comparisons, conversions,
4443 and logic-not operations are simple, if their operands are simple, too. */
4445 static bool
4446 simple_operand_p_2 (tree exp)
4448 enum tree_code code;
4450 if (TREE_SIDE_EFFECTS (exp) || generic_expr_could_trap_p (exp))
4451 return false;
4453 while (CONVERT_EXPR_P (exp))
4454 exp = TREE_OPERAND (exp, 0);
4456 code = TREE_CODE (exp);
4458 if (TREE_CODE_CLASS (code) == tcc_comparison)
4459 return (simple_operand_p (TREE_OPERAND (exp, 0))
4460 && simple_operand_p (TREE_OPERAND (exp, 1)));
4462 if (code == TRUTH_NOT_EXPR)
4463 return simple_operand_p_2 (TREE_OPERAND (exp, 0));
4465 return simple_operand_p (exp);
4469 /* The following functions are subroutines to fold_range_test and allow it to
4470 try to change a logical combination of comparisons into a range test.
4472 For example, both
4473 X == 2 || X == 3 || X == 4 || X == 5
4475 X >= 2 && X <= 5
4476 are converted to
4477 (unsigned) (X - 2) <= 3
4479 We describe each set of comparisons as being either inside or outside
4480 a range, using a variable named like IN_P, and then describe the
4481 range with a lower and upper bound. If one of the bounds is omitted,
4482 it represents either the highest or lowest value of the type.
4484 In the comments below, we represent a range by two numbers in brackets
4485 preceded by a "+" to designate being inside that range, or a "-" to
4486 designate being outside that range, so the condition can be inverted by
4487 flipping the prefix. An omitted bound is represented by a "-". For
4488 example, "- [-, 10]" means being outside the range starting at the lowest
4489 possible value and ending at 10, in other words, being greater than 10.
4490 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4491 always false.
4493 We set up things so that the missing bounds are handled in a consistent
4494 manner so neither a missing bound nor "true" and "false" need to be
4495 handled using a special case. */
4497 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4498 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4499 and UPPER1_P are nonzero if the respective argument is an upper bound
4500 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4501 must be specified for a comparison. ARG1 will be converted to ARG0's
4502 type if both are specified. */
4504 static tree
4505 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4506 tree arg1, int upper1_p)
4508 tree tem;
4509 int result;
4510 int sgn0, sgn1;
4512 /* If neither arg represents infinity, do the normal operation.
4513 Else, if not a comparison, return infinity. Else handle the special
4514 comparison rules. Note that most of the cases below won't occur, but
4515 are handled for consistency. */
4517 if (arg0 != 0 && arg1 != 0)
4519 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4520 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4521 STRIP_NOPS (tem);
4522 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4525 if (TREE_CODE_CLASS (code) != tcc_comparison)
4526 return 0;
4528 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4529 for neither. In real maths, we cannot assume open ended ranges are
4530 the same. But, this is computer arithmetic, where numbers are finite.
4531 We can therefore make the transformation of any unbounded range with
4532 the value Z, Z being greater than any representable number. This permits
4533 us to treat unbounded ranges as equal. */
4534 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4535 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4536 switch (code)
4538 case EQ_EXPR:
4539 result = sgn0 == sgn1;
4540 break;
4541 case NE_EXPR:
4542 result = sgn0 != sgn1;
4543 break;
4544 case LT_EXPR:
4545 result = sgn0 < sgn1;
4546 break;
4547 case LE_EXPR:
4548 result = sgn0 <= sgn1;
4549 break;
4550 case GT_EXPR:
4551 result = sgn0 > sgn1;
4552 break;
4553 case GE_EXPR:
4554 result = sgn0 >= sgn1;
4555 break;
4556 default:
4557 gcc_unreachable ();
4560 return constant_boolean_node (result, type);
4563 /* Helper routine for make_range. Perform one step for it, return
4564 new expression if the loop should continue or NULL_TREE if it should
4565 stop. */
4567 tree
4568 make_range_step (location_t loc, enum tree_code code, tree arg0, tree arg1,
4569 tree exp_type, tree *p_low, tree *p_high, int *p_in_p,
4570 bool *strict_overflow_p)
4572 tree arg0_type = TREE_TYPE (arg0);
4573 tree n_low, n_high, low = *p_low, high = *p_high;
4574 int in_p = *p_in_p, n_in_p;
4576 switch (code)
4578 case TRUTH_NOT_EXPR:
4579 /* We can only do something if the range is testing for zero. */
4580 if (low == NULL_TREE || high == NULL_TREE
4581 || ! integer_zerop (low) || ! integer_zerop (high))
4582 return NULL_TREE;
4583 *p_in_p = ! in_p;
4584 return arg0;
4586 case EQ_EXPR: case NE_EXPR:
4587 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4588 /* We can only do something if the range is testing for zero
4589 and if the second operand is an integer constant. Note that
4590 saying something is "in" the range we make is done by
4591 complementing IN_P since it will set in the initial case of
4592 being not equal to zero; "out" is leaving it alone. */
4593 if (low == NULL_TREE || high == NULL_TREE
4594 || ! integer_zerop (low) || ! integer_zerop (high)
4595 || TREE_CODE (arg1) != INTEGER_CST)
4596 return NULL_TREE;
4598 switch (code)
4600 case NE_EXPR: /* - [c, c] */
4601 low = high = arg1;
4602 break;
4603 case EQ_EXPR: /* + [c, c] */
4604 in_p = ! in_p, low = high = arg1;
4605 break;
4606 case GT_EXPR: /* - [-, c] */
4607 low = 0, high = arg1;
4608 break;
4609 case GE_EXPR: /* + [c, -] */
4610 in_p = ! in_p, low = arg1, high = 0;
4611 break;
4612 case LT_EXPR: /* - [c, -] */
4613 low = arg1, high = 0;
4614 break;
4615 case LE_EXPR: /* + [-, c] */
4616 in_p = ! in_p, low = 0, high = arg1;
4617 break;
4618 default:
4619 gcc_unreachable ();
4622 /* If this is an unsigned comparison, we also know that EXP is
4623 greater than or equal to zero. We base the range tests we make
4624 on that fact, so we record it here so we can parse existing
4625 range tests. We test arg0_type since often the return type
4626 of, e.g. EQ_EXPR, is boolean. */
4627 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4629 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4630 in_p, low, high, 1,
4631 build_int_cst (arg0_type, 0),
4632 NULL_TREE))
4633 return NULL_TREE;
4635 in_p = n_in_p, low = n_low, high = n_high;
4637 /* If the high bound is missing, but we have a nonzero low
4638 bound, reverse the range so it goes from zero to the low bound
4639 minus 1. */
4640 if (high == 0 && low && ! integer_zerop (low))
4642 in_p = ! in_p;
4643 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4644 build_int_cst (TREE_TYPE (low), 1), 0);
4645 low = build_int_cst (arg0_type, 0);
4649 *p_low = low;
4650 *p_high = high;
4651 *p_in_p = in_p;
4652 return arg0;
4654 case NEGATE_EXPR:
4655 /* If flag_wrapv and ARG0_TYPE is signed, make sure
4656 low and high are non-NULL, then normalize will DTRT. */
4657 if (!TYPE_UNSIGNED (arg0_type)
4658 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4660 if (low == NULL_TREE)
4661 low = TYPE_MIN_VALUE (arg0_type);
4662 if (high == NULL_TREE)
4663 high = TYPE_MAX_VALUE (arg0_type);
4666 /* (-x) IN [a,b] -> x in [-b, -a] */
4667 n_low = range_binop (MINUS_EXPR, exp_type,
4668 build_int_cst (exp_type, 0),
4669 0, high, 1);
4670 n_high = range_binop (MINUS_EXPR, exp_type,
4671 build_int_cst (exp_type, 0),
4672 0, low, 0);
4673 if (n_high != 0 && TREE_OVERFLOW (n_high))
4674 return NULL_TREE;
4675 goto normalize;
4677 case BIT_NOT_EXPR:
4678 /* ~ X -> -X - 1 */
4679 return build2_loc (loc, MINUS_EXPR, exp_type, negate_expr (arg0),
4680 build_int_cst (exp_type, 1));
4682 case PLUS_EXPR:
4683 case MINUS_EXPR:
4684 if (TREE_CODE (arg1) != INTEGER_CST)
4685 return NULL_TREE;
4687 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4688 move a constant to the other side. */
4689 if (!TYPE_UNSIGNED (arg0_type)
4690 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4691 return NULL_TREE;
4693 /* If EXP is signed, any overflow in the computation is undefined,
4694 so we don't worry about it so long as our computations on
4695 the bounds don't overflow. For unsigned, overflow is defined
4696 and this is exactly the right thing. */
4697 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4698 arg0_type, low, 0, arg1, 0);
4699 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4700 arg0_type, high, 1, arg1, 0);
4701 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4702 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4703 return NULL_TREE;
4705 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4706 *strict_overflow_p = true;
4708 normalize:
4709 /* Check for an unsigned range which has wrapped around the maximum
4710 value thus making n_high < n_low, and normalize it. */
4711 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4713 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4714 build_int_cst (TREE_TYPE (n_high), 1), 0);
4715 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4716 build_int_cst (TREE_TYPE (n_low), 1), 0);
4718 /* If the range is of the form +/- [ x+1, x ], we won't
4719 be able to normalize it. But then, it represents the
4720 whole range or the empty set, so make it
4721 +/- [ -, - ]. */
4722 if (tree_int_cst_equal (n_low, low)
4723 && tree_int_cst_equal (n_high, high))
4724 low = high = 0;
4725 else
4726 in_p = ! in_p;
4728 else
4729 low = n_low, high = n_high;
4731 *p_low = low;
4732 *p_high = high;
4733 *p_in_p = in_p;
4734 return arg0;
4736 CASE_CONVERT:
4737 case NON_LVALUE_EXPR:
4738 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4739 return NULL_TREE;
4741 if (! INTEGRAL_TYPE_P (arg0_type)
4742 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4743 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4744 return NULL_TREE;
4746 n_low = low, n_high = high;
4748 if (n_low != 0)
4749 n_low = fold_convert_loc (loc, arg0_type, n_low);
4751 if (n_high != 0)
4752 n_high = fold_convert_loc (loc, arg0_type, n_high);
4754 /* If we're converting arg0 from an unsigned type, to exp,
4755 a signed type, we will be doing the comparison as unsigned.
4756 The tests above have already verified that LOW and HIGH
4757 are both positive.
4759 So we have to ensure that we will handle large unsigned
4760 values the same way that the current signed bounds treat
4761 negative values. */
4763 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4765 tree high_positive;
4766 tree equiv_type;
4767 /* For fixed-point modes, we need to pass the saturating flag
4768 as the 2nd parameter. */
4769 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4770 equiv_type
4771 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type),
4772 TYPE_SATURATING (arg0_type));
4773 else
4774 equiv_type
4775 = lang_hooks.types.type_for_mode (TYPE_MODE (arg0_type), 1);
4777 /* A range without an upper bound is, naturally, unbounded.
4778 Since convert would have cropped a very large value, use
4779 the max value for the destination type. */
4780 high_positive
4781 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4782 : TYPE_MAX_VALUE (arg0_type);
4784 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4785 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4786 fold_convert_loc (loc, arg0_type,
4787 high_positive),
4788 build_int_cst (arg0_type, 1));
4790 /* If the low bound is specified, "and" the range with the
4791 range for which the original unsigned value will be
4792 positive. */
4793 if (low != 0)
4795 if (! merge_ranges (&n_in_p, &n_low, &n_high, 1, n_low, n_high,
4796 1, fold_convert_loc (loc, arg0_type,
4797 integer_zero_node),
4798 high_positive))
4799 return NULL_TREE;
4801 in_p = (n_in_p == in_p);
4803 else
4805 /* Otherwise, "or" the range with the range of the input
4806 that will be interpreted as negative. */
4807 if (! merge_ranges (&n_in_p, &n_low, &n_high, 0, n_low, n_high,
4808 1, fold_convert_loc (loc, arg0_type,
4809 integer_zero_node),
4810 high_positive))
4811 return NULL_TREE;
4813 in_p = (in_p != n_in_p);
4817 *p_low = n_low;
4818 *p_high = n_high;
4819 *p_in_p = in_p;
4820 return arg0;
4822 default:
4823 return NULL_TREE;
4827 /* Given EXP, a logical expression, set the range it is testing into
4828 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4829 actually being tested. *PLOW and *PHIGH will be made of the same
4830 type as the returned expression. If EXP is not a comparison, we
4831 will most likely not be returning a useful value and range. Set
4832 *STRICT_OVERFLOW_P to true if the return value is only valid
4833 because signed overflow is undefined; otherwise, do not change
4834 *STRICT_OVERFLOW_P. */
4836 tree
4837 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4838 bool *strict_overflow_p)
4840 enum tree_code code;
4841 tree arg0, arg1 = NULL_TREE;
4842 tree exp_type, nexp;
4843 int in_p;
4844 tree low, high;
4845 location_t loc = EXPR_LOCATION (exp);
4847 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4848 and see if we can refine the range. Some of the cases below may not
4849 happen, but it doesn't seem worth worrying about this. We "continue"
4850 the outer loop when we've changed something; otherwise we "break"
4851 the switch, which will "break" the while. */
4853 in_p = 0;
4854 low = high = build_int_cst (TREE_TYPE (exp), 0);
4856 while (1)
4858 code = TREE_CODE (exp);
4859 exp_type = TREE_TYPE (exp);
4860 arg0 = NULL_TREE;
4862 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4864 if (TREE_OPERAND_LENGTH (exp) > 0)
4865 arg0 = TREE_OPERAND (exp, 0);
4866 if (TREE_CODE_CLASS (code) == tcc_binary
4867 || TREE_CODE_CLASS (code) == tcc_comparison
4868 || (TREE_CODE_CLASS (code) == tcc_expression
4869 && TREE_OPERAND_LENGTH (exp) > 1))
4870 arg1 = TREE_OPERAND (exp, 1);
4872 if (arg0 == NULL_TREE)
4873 break;
4875 nexp = make_range_step (loc, code, arg0, arg1, exp_type, &low,
4876 &high, &in_p, strict_overflow_p);
4877 if (nexp == NULL_TREE)
4878 break;
4879 exp = nexp;
4882 /* If EXP is a constant, we can evaluate whether this is true or false. */
4883 if (TREE_CODE (exp) == INTEGER_CST)
4885 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4886 exp, 0, low, 0))
4887 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4888 exp, 1, high, 1)));
4889 low = high = 0;
4890 exp = 0;
4893 *pin_p = in_p, *plow = low, *phigh = high;
4894 return exp;
4897 /* Returns TRUE if [LOW, HIGH] range check can be optimized to
4898 a bitwise check i.e. when
4899 LOW == 0xXX...X00...0
4900 HIGH == 0xXX...X11...1
4901 Return corresponding mask in MASK and stem in VALUE. */
4903 static bool
4904 maskable_range_p (const_tree low, const_tree high, tree type, tree *mask,
4905 tree *value)
4907 if (TREE_CODE (low) != INTEGER_CST
4908 || TREE_CODE (high) != INTEGER_CST)
4909 return false;
4911 unsigned prec = TYPE_PRECISION (type);
4912 wide_int lo = wi::to_wide (low, prec);
4913 wide_int hi = wi::to_wide (high, prec);
4915 wide_int end_mask = lo ^ hi;
4916 if ((end_mask & (end_mask + 1)) != 0
4917 || (lo & end_mask) != 0)
4918 return false;
4920 wide_int stem_mask = ~end_mask;
4921 wide_int stem = lo & stem_mask;
4922 if (stem != (hi & stem_mask))
4923 return false;
4925 *mask = wide_int_to_tree (type, stem_mask);
4926 *value = wide_int_to_tree (type, stem);
4928 return true;
4931 /* Helper routine for build_range_check and match.pd. Return the type to
4932 perform the check or NULL if it shouldn't be optimized. */
4934 tree
4935 range_check_type (tree etype)
4937 /* First make sure that arithmetics in this type is valid, then make sure
4938 that it wraps around. */
4939 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4940 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype), 1);
4942 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_UNSIGNED (etype))
4944 tree utype, minv, maxv;
4946 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4947 for the type in question, as we rely on this here. */
4948 utype = unsigned_type_for (etype);
4949 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4950 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4951 build_int_cst (TREE_TYPE (maxv), 1), 1);
4952 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4954 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4955 minv, 1, maxv, 1)))
4956 etype = utype;
4957 else
4958 return NULL_TREE;
4960 else if (POINTER_TYPE_P (etype))
4961 etype = unsigned_type_for (etype);
4962 return etype;
4965 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4966 type, TYPE, return an expression to test if EXP is in (or out of, depending
4967 on IN_P) the range. Return 0 if the test couldn't be created. */
4969 tree
4970 build_range_check (location_t loc, tree type, tree exp, int in_p,
4971 tree low, tree high)
4973 tree etype = TREE_TYPE (exp), mask, value;
4975 /* Disable this optimization for function pointer expressions
4976 on targets that require function pointer canonicalization. */
4977 if (targetm.have_canonicalize_funcptr_for_compare ()
4978 && POINTER_TYPE_P (etype)
4979 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (etype)))
4980 return NULL_TREE;
4982 if (! in_p)
4984 value = build_range_check (loc, type, exp, 1, low, high);
4985 if (value != 0)
4986 return invert_truthvalue_loc (loc, value);
4988 return 0;
4991 if (low == 0 && high == 0)
4992 return omit_one_operand_loc (loc, type, build_int_cst (type, 1), exp);
4994 if (low == 0)
4995 return fold_build2_loc (loc, LE_EXPR, type, exp,
4996 fold_convert_loc (loc, etype, high));
4998 if (high == 0)
4999 return fold_build2_loc (loc, GE_EXPR, type, exp,
5000 fold_convert_loc (loc, etype, low));
5002 if (operand_equal_p (low, high, 0))
5003 return fold_build2_loc (loc, EQ_EXPR, type, exp,
5004 fold_convert_loc (loc, etype, low));
5006 if (TREE_CODE (exp) == BIT_AND_EXPR
5007 && maskable_range_p (low, high, etype, &mask, &value))
5008 return fold_build2_loc (loc, EQ_EXPR, type,
5009 fold_build2_loc (loc, BIT_AND_EXPR, etype,
5010 exp, mask),
5011 value);
5013 if (integer_zerop (low))
5015 if (! TYPE_UNSIGNED (etype))
5017 etype = unsigned_type_for (etype);
5018 high = fold_convert_loc (loc, etype, high);
5019 exp = fold_convert_loc (loc, etype, exp);
5021 return build_range_check (loc, type, exp, 1, 0, high);
5024 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
5025 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
5027 int prec = TYPE_PRECISION (etype);
5029 if (wi::mask <widest_int> (prec - 1, false) == wi::to_widest (high))
5031 if (TYPE_UNSIGNED (etype))
5033 tree signed_etype = signed_type_for (etype);
5034 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
5035 etype
5036 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
5037 else
5038 etype = signed_etype;
5039 exp = fold_convert_loc (loc, etype, exp);
5041 return fold_build2_loc (loc, GT_EXPR, type, exp,
5042 build_int_cst (etype, 0));
5046 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
5047 This requires wrap-around arithmetics for the type of the expression. */
5048 etype = range_check_type (etype);
5049 if (etype == NULL_TREE)
5050 return NULL_TREE;
5052 high = fold_convert_loc (loc, etype, high);
5053 low = fold_convert_loc (loc, etype, low);
5054 exp = fold_convert_loc (loc, etype, exp);
5056 value = const_binop (MINUS_EXPR, high, low);
5058 if (value != 0 && !TREE_OVERFLOW (value))
5059 return build_range_check (loc, type,
5060 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5061 1, build_int_cst (etype, 0), value);
5063 return 0;
5066 /* Return the predecessor of VAL in its type, handling the infinite case. */
5068 static tree
5069 range_predecessor (tree val)
5071 tree type = TREE_TYPE (val);
5073 if (INTEGRAL_TYPE_P (type)
5074 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5075 return 0;
5076 else
5077 return range_binop (MINUS_EXPR, NULL_TREE, val, 0,
5078 build_int_cst (TREE_TYPE (val), 1), 0);
5081 /* Return the successor of VAL in its type, handling the infinite case. */
5083 static tree
5084 range_successor (tree val)
5086 tree type = TREE_TYPE (val);
5088 if (INTEGRAL_TYPE_P (type)
5089 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5090 return 0;
5091 else
5092 return range_binop (PLUS_EXPR, NULL_TREE, val, 0,
5093 build_int_cst (TREE_TYPE (val), 1), 0);
5096 /* Given two ranges, see if we can merge them into one. Return 1 if we
5097 can, 0 if we can't. Set the output range into the specified parameters. */
5099 bool
5100 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5101 tree high0, int in1_p, tree low1, tree high1)
5103 int no_overlap;
5104 int subset;
5105 int temp;
5106 tree tem;
5107 int in_p;
5108 tree low, high;
5109 int lowequal = ((low0 == 0 && low1 == 0)
5110 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5111 low0, 0, low1, 0)));
5112 int highequal = ((high0 == 0 && high1 == 0)
5113 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5114 high0, 1, high1, 1)));
5116 /* Make range 0 be the range that starts first, or ends last if they
5117 start at the same value. Swap them if it isn't. */
5118 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5119 low0, 0, low1, 0))
5120 || (lowequal
5121 && integer_onep (range_binop (GT_EXPR, integer_type_node,
5122 high1, 1, high0, 1))))
5124 temp = in0_p, in0_p = in1_p, in1_p = temp;
5125 tem = low0, low0 = low1, low1 = tem;
5126 tem = high0, high0 = high1, high1 = tem;
5129 /* If the second range is != high1 where high1 is the type maximum of
5130 the type, try first merging with < high1 range. */
5131 if (low1
5132 && high1
5133 && TREE_CODE (low1) == INTEGER_CST
5134 && (TREE_CODE (TREE_TYPE (low1)) == INTEGER_TYPE
5135 || (TREE_CODE (TREE_TYPE (low1)) == ENUMERAL_TYPE
5136 && known_eq (TYPE_PRECISION (TREE_TYPE (low1)),
5137 GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low1))))))
5138 && operand_equal_p (low1, high1, 0))
5140 if (tree_int_cst_equal (low1, TYPE_MAX_VALUE (TREE_TYPE (low1)))
5141 && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5142 !in1_p, NULL_TREE, range_predecessor (low1)))
5143 return true;
5144 /* Similarly for the second range != low1 where low1 is the type minimum
5145 of the type, try first merging with > low1 range. */
5146 if (tree_int_cst_equal (low1, TYPE_MIN_VALUE (TREE_TYPE (low1)))
5147 && merge_ranges (pin_p, plow, phigh, in0_p, low0, high0,
5148 !in1_p, range_successor (low1), NULL_TREE))
5149 return true;
5152 /* Now flag two cases, whether the ranges are disjoint or whether the
5153 second range is totally subsumed in the first. Note that the tests
5154 below are simplified by the ones above. */
5155 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5156 high0, 1, low1, 0));
5157 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5158 high1, 1, high0, 1));
5160 /* We now have four cases, depending on whether we are including or
5161 excluding the two ranges. */
5162 if (in0_p && in1_p)
5164 /* If they don't overlap, the result is false. If the second range
5165 is a subset it is the result. Otherwise, the range is from the start
5166 of the second to the end of the first. */
5167 if (no_overlap)
5168 in_p = 0, low = high = 0;
5169 else if (subset)
5170 in_p = 1, low = low1, high = high1;
5171 else
5172 in_p = 1, low = low1, high = high0;
5175 else if (in0_p && ! in1_p)
5177 /* If they don't overlap, the result is the first range. If they are
5178 equal, the result is false. If the second range is a subset of the
5179 first, and the ranges begin at the same place, we go from just after
5180 the end of the second range to the end of the first. If the second
5181 range is not a subset of the first, or if it is a subset and both
5182 ranges end at the same place, the range starts at the start of the
5183 first range and ends just before the second range.
5184 Otherwise, we can't describe this as a single range. */
5185 if (no_overlap)
5186 in_p = 1, low = low0, high = high0;
5187 else if (lowequal && highequal)
5188 in_p = 0, low = high = 0;
5189 else if (subset && lowequal)
5191 low = range_successor (high1);
5192 high = high0;
5193 in_p = 1;
5194 if (low == 0)
5196 /* We are in the weird situation where high0 > high1 but
5197 high1 has no successor. Punt. */
5198 return 0;
5201 else if (! subset || highequal)
5203 low = low0;
5204 high = range_predecessor (low1);
5205 in_p = 1;
5206 if (high == 0)
5208 /* low0 < low1 but low1 has no predecessor. Punt. */
5209 return 0;
5212 else
5213 return 0;
5216 else if (! in0_p && in1_p)
5218 /* If they don't overlap, the result is the second range. If the second
5219 is a subset of the first, the result is false. Otherwise,
5220 the range starts just after the first range and ends at the
5221 end of the second. */
5222 if (no_overlap)
5223 in_p = 1, low = low1, high = high1;
5224 else if (subset || highequal)
5225 in_p = 0, low = high = 0;
5226 else
5228 low = range_successor (high0);
5229 high = high1;
5230 in_p = 1;
5231 if (low == 0)
5233 /* high1 > high0 but high0 has no successor. Punt. */
5234 return 0;
5239 else
5241 /* The case where we are excluding both ranges. Here the complex case
5242 is if they don't overlap. In that case, the only time we have a
5243 range is if they are adjacent. If the second is a subset of the
5244 first, the result is the first. Otherwise, the range to exclude
5245 starts at the beginning of the first range and ends at the end of the
5246 second. */
5247 if (no_overlap)
5249 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5250 range_successor (high0),
5251 1, low1, 0)))
5252 in_p = 0, low = low0, high = high1;
5253 else
5255 /* Canonicalize - [min, x] into - [-, x]. */
5256 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5257 switch (TREE_CODE (TREE_TYPE (low0)))
5259 case ENUMERAL_TYPE:
5260 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (low0)),
5261 GET_MODE_BITSIZE
5262 (TYPE_MODE (TREE_TYPE (low0)))))
5263 break;
5264 /* FALLTHROUGH */
5265 case INTEGER_TYPE:
5266 if (tree_int_cst_equal (low0,
5267 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5268 low0 = 0;
5269 break;
5270 case POINTER_TYPE:
5271 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5272 && integer_zerop (low0))
5273 low0 = 0;
5274 break;
5275 default:
5276 break;
5279 /* Canonicalize - [x, max] into - [x, -]. */
5280 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5281 switch (TREE_CODE (TREE_TYPE (high1)))
5283 case ENUMERAL_TYPE:
5284 if (maybe_ne (TYPE_PRECISION (TREE_TYPE (high1)),
5285 GET_MODE_BITSIZE
5286 (TYPE_MODE (TREE_TYPE (high1)))))
5287 break;
5288 /* FALLTHROUGH */
5289 case INTEGER_TYPE:
5290 if (tree_int_cst_equal (high1,
5291 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5292 high1 = 0;
5293 break;
5294 case POINTER_TYPE:
5295 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5296 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5297 high1, 1,
5298 build_int_cst (TREE_TYPE (high1), 1),
5299 1)))
5300 high1 = 0;
5301 break;
5302 default:
5303 break;
5306 /* The ranges might be also adjacent between the maximum and
5307 minimum values of the given type. For
5308 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5309 return + [x + 1, y - 1]. */
5310 if (low0 == 0 && high1 == 0)
5312 low = range_successor (high0);
5313 high = range_predecessor (low1);
5314 if (low == 0 || high == 0)
5315 return 0;
5317 in_p = 1;
5319 else
5320 return 0;
5323 else if (subset)
5324 in_p = 0, low = low0, high = high0;
5325 else
5326 in_p = 0, low = low0, high = high1;
5329 *pin_p = in_p, *plow = low, *phigh = high;
5330 return 1;
5334 /* Subroutine of fold, looking inside expressions of the form
5335 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5336 of the COND_EXPR. This function is being used also to optimize
5337 A op B ? C : A, by reversing the comparison first.
5339 Return a folded expression whose code is not a COND_EXPR
5340 anymore, or NULL_TREE if no folding opportunity is found. */
5342 static tree
5343 fold_cond_expr_with_comparison (location_t loc, tree type,
5344 tree arg0, tree arg1, tree arg2)
5346 enum tree_code comp_code = TREE_CODE (arg0);
5347 tree arg00 = TREE_OPERAND (arg0, 0);
5348 tree arg01 = TREE_OPERAND (arg0, 1);
5349 tree arg1_type = TREE_TYPE (arg1);
5350 tree tem;
5352 STRIP_NOPS (arg1);
5353 STRIP_NOPS (arg2);
5355 /* If we have A op 0 ? A : -A, consider applying the following
5356 transformations:
5358 A == 0? A : -A same as -A
5359 A != 0? A : -A same as A
5360 A >= 0? A : -A same as abs (A)
5361 A > 0? A : -A same as abs (A)
5362 A <= 0? A : -A same as -abs (A)
5363 A < 0? A : -A same as -abs (A)
5365 None of these transformations work for modes with signed
5366 zeros. If A is +/-0, the first two transformations will
5367 change the sign of the result (from +0 to -0, or vice
5368 versa). The last four will fix the sign of the result,
5369 even though the original expressions could be positive or
5370 negative, depending on the sign of A.
5372 Note that all these transformations are correct if A is
5373 NaN, since the two alternatives (A and -A) are also NaNs. */
5374 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5375 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5376 ? real_zerop (arg01)
5377 : integer_zerop (arg01))
5378 && ((TREE_CODE (arg2) == NEGATE_EXPR
5379 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5380 /* In the case that A is of the form X-Y, '-A' (arg2) may
5381 have already been folded to Y-X, check for that. */
5382 || (TREE_CODE (arg1) == MINUS_EXPR
5383 && TREE_CODE (arg2) == MINUS_EXPR
5384 && operand_equal_p (TREE_OPERAND (arg1, 0),
5385 TREE_OPERAND (arg2, 1), 0)
5386 && operand_equal_p (TREE_OPERAND (arg1, 1),
5387 TREE_OPERAND (arg2, 0), 0))))
5388 switch (comp_code)
5390 case EQ_EXPR:
5391 case UNEQ_EXPR:
5392 tem = fold_convert_loc (loc, arg1_type, arg1);
5393 return fold_convert_loc (loc, type, negate_expr (tem));
5394 case NE_EXPR:
5395 case LTGT_EXPR:
5396 return fold_convert_loc (loc, type, arg1);
5397 case UNGE_EXPR:
5398 case UNGT_EXPR:
5399 if (flag_trapping_math)
5400 break;
5401 /* Fall through. */
5402 case GE_EXPR:
5403 case GT_EXPR:
5404 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5405 break;
5406 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5407 return fold_convert_loc (loc, type, tem);
5408 case UNLE_EXPR:
5409 case UNLT_EXPR:
5410 if (flag_trapping_math)
5411 break;
5412 /* FALLTHRU */
5413 case LE_EXPR:
5414 case LT_EXPR:
5415 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5416 break;
5417 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5418 return negate_expr (fold_convert_loc (loc, type, tem));
5419 default:
5420 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5421 break;
5424 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5425 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5426 both transformations are correct when A is NaN: A != 0
5427 is then true, and A == 0 is false. */
5429 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5430 && integer_zerop (arg01) && integer_zerop (arg2))
5432 if (comp_code == NE_EXPR)
5433 return fold_convert_loc (loc, type, arg1);
5434 else if (comp_code == EQ_EXPR)
5435 return build_zero_cst (type);
5438 /* Try some transformations of A op B ? A : B.
5440 A == B? A : B same as B
5441 A != B? A : B same as A
5442 A >= B? A : B same as max (A, B)
5443 A > B? A : B same as max (B, A)
5444 A <= B? A : B same as min (A, B)
5445 A < B? A : B same as min (B, A)
5447 As above, these transformations don't work in the presence
5448 of signed zeros. For example, if A and B are zeros of
5449 opposite sign, the first two transformations will change
5450 the sign of the result. In the last four, the original
5451 expressions give different results for (A=+0, B=-0) and
5452 (A=-0, B=+0), but the transformed expressions do not.
5454 The first two transformations are correct if either A or B
5455 is a NaN. In the first transformation, the condition will
5456 be false, and B will indeed be chosen. In the case of the
5457 second transformation, the condition A != B will be true,
5458 and A will be chosen.
5460 The conversions to max() and min() are not correct if B is
5461 a number and A is not. The conditions in the original
5462 expressions will be false, so all four give B. The min()
5463 and max() versions would give a NaN instead. */
5464 if (!HONOR_SIGNED_ZEROS (element_mode (type))
5465 && operand_equal_for_comparison_p (arg01, arg2)
5466 /* Avoid these transformations if the COND_EXPR may be used
5467 as an lvalue in the C++ front-end. PR c++/19199. */
5468 && (in_gimple_form
5469 || VECTOR_TYPE_P (type)
5470 || (! lang_GNU_CXX ()
5471 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5472 || ! maybe_lvalue_p (arg1)
5473 || ! maybe_lvalue_p (arg2)))
5475 tree comp_op0 = arg00;
5476 tree comp_op1 = arg01;
5477 tree comp_type = TREE_TYPE (comp_op0);
5479 switch (comp_code)
5481 case EQ_EXPR:
5482 return fold_convert_loc (loc, type, arg2);
5483 case NE_EXPR:
5484 return fold_convert_loc (loc, type, arg1);
5485 case LE_EXPR:
5486 case LT_EXPR:
5487 case UNLE_EXPR:
5488 case UNLT_EXPR:
5489 /* In C++ a ?: expression can be an lvalue, so put the
5490 operand which will be used if they are equal first
5491 so that we can convert this back to the
5492 corresponding COND_EXPR. */
5493 if (!HONOR_NANS (arg1))
5495 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5496 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5497 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5498 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5499 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5500 comp_op1, comp_op0);
5501 return fold_convert_loc (loc, type, tem);
5503 break;
5504 case GE_EXPR:
5505 case GT_EXPR:
5506 case UNGE_EXPR:
5507 case UNGT_EXPR:
5508 if (!HONOR_NANS (arg1))
5510 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5511 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5512 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5513 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5514 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5515 comp_op1, comp_op0);
5516 return fold_convert_loc (loc, type, tem);
5518 break;
5519 case UNEQ_EXPR:
5520 if (!HONOR_NANS (arg1))
5521 return fold_convert_loc (loc, type, arg2);
5522 break;
5523 case LTGT_EXPR:
5524 if (!HONOR_NANS (arg1))
5525 return fold_convert_loc (loc, type, arg1);
5526 break;
5527 default:
5528 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5529 break;
5533 return NULL_TREE;
5538 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5539 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5540 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5541 false) >= 2)
5542 #endif
5544 /* EXP is some logical combination of boolean tests. See if we can
5545 merge it into some range test. Return the new tree if so. */
5547 static tree
5548 fold_range_test (location_t loc, enum tree_code code, tree type,
5549 tree op0, tree op1)
5551 int or_op = (code == TRUTH_ORIF_EXPR
5552 || code == TRUTH_OR_EXPR);
5553 int in0_p, in1_p, in_p;
5554 tree low0, low1, low, high0, high1, high;
5555 bool strict_overflow_p = false;
5556 tree tem, lhs, rhs;
5557 const char * const warnmsg = G_("assuming signed overflow does not occur "
5558 "when simplifying range test");
5560 if (!INTEGRAL_TYPE_P (type))
5561 return 0;
5563 lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5564 rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5566 /* If this is an OR operation, invert both sides; we will invert
5567 again at the end. */
5568 if (or_op)
5569 in0_p = ! in0_p, in1_p = ! in1_p;
5571 /* If both expressions are the same, if we can merge the ranges, and we
5572 can build the range test, return it or it inverted. If one of the
5573 ranges is always true or always false, consider it to be the same
5574 expression as the other. */
5575 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5576 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5577 in1_p, low1, high1)
5578 && (tem = (build_range_check (loc, type,
5579 lhs != 0 ? lhs
5580 : rhs != 0 ? rhs : integer_zero_node,
5581 in_p, low, high))) != 0)
5583 if (strict_overflow_p)
5584 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5585 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5588 /* On machines where the branch cost is expensive, if this is a
5589 short-circuited branch and the underlying object on both sides
5590 is the same, make a non-short-circuit operation. */
5591 bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
5592 if (PARAM_VALUE (PARAM_LOGICAL_OP_NON_SHORT_CIRCUIT) != -1)
5593 logical_op_non_short_circuit
5594 = PARAM_VALUE (PARAM_LOGICAL_OP_NON_SHORT_CIRCUIT);
5595 if (logical_op_non_short_circuit
5596 && !flag_sanitize_coverage
5597 && lhs != 0 && rhs != 0
5598 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
5599 && operand_equal_p (lhs, rhs, 0))
5601 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5602 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5603 which cases we can't do this. */
5604 if (simple_operand_p (lhs))
5605 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5606 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5607 type, op0, op1);
5609 else if (!lang_hooks.decls.global_bindings_p ()
5610 && !CONTAINS_PLACEHOLDER_P (lhs))
5612 tree common = save_expr (lhs);
5614 if ((lhs = build_range_check (loc, type, common,
5615 or_op ? ! in0_p : in0_p,
5616 low0, high0)) != 0
5617 && (rhs = build_range_check (loc, type, common,
5618 or_op ? ! in1_p : in1_p,
5619 low1, high1)) != 0)
5621 if (strict_overflow_p)
5622 fold_overflow_warning (warnmsg,
5623 WARN_STRICT_OVERFLOW_COMPARISON);
5624 return build2_loc (loc, code == TRUTH_ANDIF_EXPR
5625 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5626 type, lhs, rhs);
5631 return 0;
5634 /* Subroutine for fold_truth_andor_1: C is an INTEGER_CST interpreted as a P
5635 bit value. Arrange things so the extra bits will be set to zero if and
5636 only if C is signed-extended to its full width. If MASK is nonzero,
5637 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5639 static tree
5640 unextend (tree c, int p, int unsignedp, tree mask)
5642 tree type = TREE_TYPE (c);
5643 int modesize = GET_MODE_BITSIZE (SCALAR_INT_TYPE_MODE (type));
5644 tree temp;
5646 if (p == modesize || unsignedp)
5647 return c;
5649 /* We work by getting just the sign bit into the low-order bit, then
5650 into the high-order bit, then sign-extend. We then XOR that value
5651 with C. */
5652 temp = build_int_cst (TREE_TYPE (c),
5653 wi::extract_uhwi (wi::to_wide (c), p - 1, 1));
5655 /* We must use a signed type in order to get an arithmetic right shift.
5656 However, we must also avoid introducing accidental overflows, so that
5657 a subsequent call to integer_zerop will work. Hence we must
5658 do the type conversion here. At this point, the constant is either
5659 zero or one, and the conversion to a signed type can never overflow.
5660 We could get an overflow if this conversion is done anywhere else. */
5661 if (TYPE_UNSIGNED (type))
5662 temp = fold_convert (signed_type_for (type), temp);
5664 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1));
5665 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1));
5666 if (mask != 0)
5667 temp = const_binop (BIT_AND_EXPR, temp,
5668 fold_convert (TREE_TYPE (c), mask));
5669 /* If necessary, convert the type back to match the type of C. */
5670 if (TYPE_UNSIGNED (type))
5671 temp = fold_convert (type, temp);
5673 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp));
5676 /* For an expression that has the form
5677 (A && B) || ~B
5679 (A || B) && ~B,
5680 we can drop one of the inner expressions and simplify to
5681 A || ~B
5683 A && ~B
5684 LOC is the location of the resulting expression. OP is the inner
5685 logical operation; the left-hand side in the examples above, while CMPOP
5686 is the right-hand side. RHS_ONLY is used to prevent us from accidentally
5687 removing a condition that guards another, as in
5688 (A != NULL && A->...) || A == NULL
5689 which we must not transform. If RHS_ONLY is true, only eliminate the
5690 right-most operand of the inner logical operation. */
5692 static tree
5693 merge_truthop_with_opposite_arm (location_t loc, tree op, tree cmpop,
5694 bool rhs_only)
5696 tree type = TREE_TYPE (cmpop);
5697 enum tree_code code = TREE_CODE (cmpop);
5698 enum tree_code truthop_code = TREE_CODE (op);
5699 tree lhs = TREE_OPERAND (op, 0);
5700 tree rhs = TREE_OPERAND (op, 1);
5701 tree orig_lhs = lhs, orig_rhs = rhs;
5702 enum tree_code rhs_code = TREE_CODE (rhs);
5703 enum tree_code lhs_code = TREE_CODE (lhs);
5704 enum tree_code inv_code;
5706 if (TREE_SIDE_EFFECTS (op) || TREE_SIDE_EFFECTS (cmpop))
5707 return NULL_TREE;
5709 if (TREE_CODE_CLASS (code) != tcc_comparison)
5710 return NULL_TREE;
5712 if (rhs_code == truthop_code)
5714 tree newrhs = merge_truthop_with_opposite_arm (loc, rhs, cmpop, rhs_only);
5715 if (newrhs != NULL_TREE)
5717 rhs = newrhs;
5718 rhs_code = TREE_CODE (rhs);
5721 if (lhs_code == truthop_code && !rhs_only)
5723 tree newlhs = merge_truthop_with_opposite_arm (loc, lhs, cmpop, false);
5724 if (newlhs != NULL_TREE)
5726 lhs = newlhs;
5727 lhs_code = TREE_CODE (lhs);
5731 inv_code = invert_tree_comparison (code, HONOR_NANS (type));
5732 if (inv_code == rhs_code
5733 && operand_equal_p (TREE_OPERAND (rhs, 0), TREE_OPERAND (cmpop, 0), 0)
5734 && operand_equal_p (TREE_OPERAND (rhs, 1), TREE_OPERAND (cmpop, 1), 0))
5735 return lhs;
5736 if (!rhs_only && inv_code == lhs_code
5737 && operand_equal_p (TREE_OPERAND (lhs, 0), TREE_OPERAND (cmpop, 0), 0)
5738 && operand_equal_p (TREE_OPERAND (lhs, 1), TREE_OPERAND (cmpop, 1), 0))
5739 return rhs;
5740 if (rhs != orig_rhs || lhs != orig_lhs)
5741 return fold_build2_loc (loc, truthop_code, TREE_TYPE (cmpop),
5742 lhs, rhs);
5743 return NULL_TREE;
5746 /* Find ways of folding logical expressions of LHS and RHS:
5747 Try to merge two comparisons to the same innermost item.
5748 Look for range tests like "ch >= '0' && ch <= '9'".
5749 Look for combinations of simple terms on machines with expensive branches
5750 and evaluate the RHS unconditionally.
5752 For example, if we have p->a == 2 && p->b == 4 and we can make an
5753 object large enough to span both A and B, we can do this with a comparison
5754 against the object ANDed with the a mask.
5756 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5757 operations to do this with one comparison.
5759 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5760 function and the one above.
5762 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5763 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5765 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5766 two operands.
5768 We return the simplified tree or 0 if no optimization is possible. */
5770 static tree
5771 fold_truth_andor_1 (location_t loc, enum tree_code code, tree truth_type,
5772 tree lhs, tree rhs)
5774 /* If this is the "or" of two comparisons, we can do something if
5775 the comparisons are NE_EXPR. If this is the "and", we can do something
5776 if the comparisons are EQ_EXPR. I.e.,
5777 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5779 WANTED_CODE is this operation code. For single bit fields, we can
5780 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5781 comparison for one-bit fields. */
5783 enum tree_code wanted_code;
5784 enum tree_code lcode, rcode;
5785 tree ll_arg, lr_arg, rl_arg, rr_arg;
5786 tree ll_inner, lr_inner, rl_inner, rr_inner;
5787 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5788 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5789 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5790 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5791 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5792 int ll_reversep, lr_reversep, rl_reversep, rr_reversep;
5793 machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5794 scalar_int_mode lnmode, rnmode;
5795 tree ll_mask, lr_mask, rl_mask, rr_mask;
5796 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5797 tree l_const, r_const;
5798 tree lntype, rntype, result;
5799 HOST_WIDE_INT first_bit, end_bit;
5800 int volatilep;
5802 /* Start by getting the comparison codes. Fail if anything is volatile.
5803 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5804 it were surrounded with a NE_EXPR. */
5806 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5807 return 0;
5809 lcode = TREE_CODE (lhs);
5810 rcode = TREE_CODE (rhs);
5812 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5814 lhs = build2 (NE_EXPR, truth_type, lhs,
5815 build_int_cst (TREE_TYPE (lhs), 0));
5816 lcode = NE_EXPR;
5819 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5821 rhs = build2 (NE_EXPR, truth_type, rhs,
5822 build_int_cst (TREE_TYPE (rhs), 0));
5823 rcode = NE_EXPR;
5826 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5827 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5828 return 0;
5830 ll_arg = TREE_OPERAND (lhs, 0);
5831 lr_arg = TREE_OPERAND (lhs, 1);
5832 rl_arg = TREE_OPERAND (rhs, 0);
5833 rr_arg = TREE_OPERAND (rhs, 1);
5835 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5836 if (simple_operand_p (ll_arg)
5837 && simple_operand_p (lr_arg))
5839 if (operand_equal_p (ll_arg, rl_arg, 0)
5840 && operand_equal_p (lr_arg, rr_arg, 0))
5842 result = combine_comparisons (loc, code, lcode, rcode,
5843 truth_type, ll_arg, lr_arg);
5844 if (result)
5845 return result;
5847 else if (operand_equal_p (ll_arg, rr_arg, 0)
5848 && operand_equal_p (lr_arg, rl_arg, 0))
5850 result = combine_comparisons (loc, code, lcode,
5851 swap_tree_comparison (rcode),
5852 truth_type, ll_arg, lr_arg);
5853 if (result)
5854 return result;
5858 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5859 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5861 /* If the RHS can be evaluated unconditionally and its operands are
5862 simple, it wins to evaluate the RHS unconditionally on machines
5863 with expensive branches. In this case, this isn't a comparison
5864 that can be merged. */
5866 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5867 false) >= 2
5868 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5869 && simple_operand_p (rl_arg)
5870 && simple_operand_p (rr_arg))
5872 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5873 if (code == TRUTH_OR_EXPR
5874 && lcode == NE_EXPR && integer_zerop (lr_arg)
5875 && rcode == NE_EXPR && integer_zerop (rr_arg)
5876 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5877 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5878 return build2_loc (loc, NE_EXPR, truth_type,
5879 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5880 ll_arg, rl_arg),
5881 build_int_cst (TREE_TYPE (ll_arg), 0));
5883 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5884 if (code == TRUTH_AND_EXPR
5885 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5886 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5887 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5888 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5889 return build2_loc (loc, EQ_EXPR, truth_type,
5890 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5891 ll_arg, rl_arg),
5892 build_int_cst (TREE_TYPE (ll_arg), 0));
5895 /* See if the comparisons can be merged. Then get all the parameters for
5896 each side. */
5898 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5899 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5900 return 0;
5902 ll_reversep = lr_reversep = rl_reversep = rr_reversep = 0;
5903 volatilep = 0;
5904 ll_inner = decode_field_reference (loc, &ll_arg,
5905 &ll_bitsize, &ll_bitpos, &ll_mode,
5906 &ll_unsignedp, &ll_reversep, &volatilep,
5907 &ll_mask, &ll_and_mask);
5908 lr_inner = decode_field_reference (loc, &lr_arg,
5909 &lr_bitsize, &lr_bitpos, &lr_mode,
5910 &lr_unsignedp, &lr_reversep, &volatilep,
5911 &lr_mask, &lr_and_mask);
5912 rl_inner = decode_field_reference (loc, &rl_arg,
5913 &rl_bitsize, &rl_bitpos, &rl_mode,
5914 &rl_unsignedp, &rl_reversep, &volatilep,
5915 &rl_mask, &rl_and_mask);
5916 rr_inner = decode_field_reference (loc, &rr_arg,
5917 &rr_bitsize, &rr_bitpos, &rr_mode,
5918 &rr_unsignedp, &rr_reversep, &volatilep,
5919 &rr_mask, &rr_and_mask);
5921 /* It must be true that the inner operation on the lhs of each
5922 comparison must be the same if we are to be able to do anything.
5923 Then see if we have constants. If not, the same must be true for
5924 the rhs's. */
5925 if (volatilep
5926 || ll_reversep != rl_reversep
5927 || ll_inner == 0 || rl_inner == 0
5928 || ! operand_equal_p (ll_inner, rl_inner, 0))
5929 return 0;
5931 if (TREE_CODE (lr_arg) == INTEGER_CST
5932 && TREE_CODE (rr_arg) == INTEGER_CST)
5934 l_const = lr_arg, r_const = rr_arg;
5935 lr_reversep = ll_reversep;
5937 else if (lr_reversep != rr_reversep
5938 || lr_inner == 0 || rr_inner == 0
5939 || ! operand_equal_p (lr_inner, rr_inner, 0))
5940 return 0;
5941 else
5942 l_const = r_const = 0;
5944 /* If either comparison code is not correct for our logical operation,
5945 fail. However, we can convert a one-bit comparison against zero into
5946 the opposite comparison against that bit being set in the field. */
5948 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5949 if (lcode != wanted_code)
5951 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5953 /* Make the left operand unsigned, since we are only interested
5954 in the value of one bit. Otherwise we are doing the wrong
5955 thing below. */
5956 ll_unsignedp = 1;
5957 l_const = ll_mask;
5959 else
5960 return 0;
5963 /* This is analogous to the code for l_const above. */
5964 if (rcode != wanted_code)
5966 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5968 rl_unsignedp = 1;
5969 r_const = rl_mask;
5971 else
5972 return 0;
5975 /* See if we can find a mode that contains both fields being compared on
5976 the left. If we can't, fail. Otherwise, update all constants and masks
5977 to be relative to a field of that size. */
5978 first_bit = MIN (ll_bitpos, rl_bitpos);
5979 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5980 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
5981 TYPE_ALIGN (TREE_TYPE (ll_inner)), BITS_PER_WORD,
5982 volatilep, &lnmode))
5983 return 0;
5985 lnbitsize = GET_MODE_BITSIZE (lnmode);
5986 lnbitpos = first_bit & ~ (lnbitsize - 1);
5987 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5988 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5990 if (ll_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5992 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5993 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5996 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
5997 size_int (xll_bitpos));
5998 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
5999 size_int (xrl_bitpos));
6001 if (l_const)
6003 l_const = fold_convert_loc (loc, lntype, l_const);
6004 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
6005 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos));
6006 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
6007 fold_build1_loc (loc, BIT_NOT_EXPR,
6008 lntype, ll_mask))))
6010 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6012 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6015 if (r_const)
6017 r_const = fold_convert_loc (loc, lntype, r_const);
6018 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
6019 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos));
6020 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
6021 fold_build1_loc (loc, BIT_NOT_EXPR,
6022 lntype, rl_mask))))
6024 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6026 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6030 /* If the right sides are not constant, do the same for it. Also,
6031 disallow this optimization if a size, signedness or storage order
6032 mismatch occurs between the left and right sides. */
6033 if (l_const == 0)
6035 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
6036 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
6037 || ll_reversep != lr_reversep
6038 /* Make sure the two fields on the right
6039 correspond to the left without being swapped. */
6040 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
6041 return 0;
6043 first_bit = MIN (lr_bitpos, rr_bitpos);
6044 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
6045 if (!get_best_mode (end_bit - first_bit, first_bit, 0, 0,
6046 TYPE_ALIGN (TREE_TYPE (lr_inner)), BITS_PER_WORD,
6047 volatilep, &rnmode))
6048 return 0;
6050 rnbitsize = GET_MODE_BITSIZE (rnmode);
6051 rnbitpos = first_bit & ~ (rnbitsize - 1);
6052 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
6053 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
6055 if (lr_reversep ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
6057 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
6058 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
6061 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6062 rntype, lr_mask),
6063 size_int (xlr_bitpos));
6064 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6065 rntype, rr_mask),
6066 size_int (xrr_bitpos));
6068 /* Make a mask that corresponds to both fields being compared.
6069 Do this for both items being compared. If the operands are the
6070 same size and the bits being compared are in the same position
6071 then we can do this by masking both and comparing the masked
6072 results. */
6073 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6074 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask);
6075 if (lnbitsize == rnbitsize
6076 && xll_bitpos == xlr_bitpos
6077 && lnbitpos >= 0
6078 && rnbitpos >= 0)
6080 lhs = make_bit_field_ref (loc, ll_inner, ll_arg,
6081 lntype, lnbitsize, lnbitpos,
6082 ll_unsignedp || rl_unsignedp, ll_reversep);
6083 if (! all_ones_mask_p (ll_mask, lnbitsize))
6084 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6086 rhs = make_bit_field_ref (loc, lr_inner, lr_arg,
6087 rntype, rnbitsize, rnbitpos,
6088 lr_unsignedp || rr_unsignedp, lr_reversep);
6089 if (! all_ones_mask_p (lr_mask, rnbitsize))
6090 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6092 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6095 /* There is still another way we can do something: If both pairs of
6096 fields being compared are adjacent, we may be able to make a wider
6097 field containing them both.
6099 Note that we still must mask the lhs/rhs expressions. Furthermore,
6100 the mask must be shifted to account for the shift done by
6101 make_bit_field_ref. */
6102 if (((ll_bitsize + ll_bitpos == rl_bitpos
6103 && lr_bitsize + lr_bitpos == rr_bitpos)
6104 || (ll_bitpos == rl_bitpos + rl_bitsize
6105 && lr_bitpos == rr_bitpos + rr_bitsize))
6106 && ll_bitpos >= 0
6107 && rl_bitpos >= 0
6108 && lr_bitpos >= 0
6109 && rr_bitpos >= 0)
6111 tree type;
6113 lhs = make_bit_field_ref (loc, ll_inner, ll_arg, lntype,
6114 ll_bitsize + rl_bitsize,
6115 MIN (ll_bitpos, rl_bitpos),
6116 ll_unsignedp, ll_reversep);
6117 rhs = make_bit_field_ref (loc, lr_inner, lr_arg, rntype,
6118 lr_bitsize + rr_bitsize,
6119 MIN (lr_bitpos, rr_bitpos),
6120 lr_unsignedp, lr_reversep);
6122 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6123 size_int (MIN (xll_bitpos, xrl_bitpos)));
6124 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6125 size_int (MIN (xlr_bitpos, xrr_bitpos)));
6127 /* Convert to the smaller type before masking out unwanted bits. */
6128 type = lntype;
6129 if (lntype != rntype)
6131 if (lnbitsize > rnbitsize)
6133 lhs = fold_convert_loc (loc, rntype, lhs);
6134 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6135 type = rntype;
6137 else if (lnbitsize < rnbitsize)
6139 rhs = fold_convert_loc (loc, lntype, rhs);
6140 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6141 type = lntype;
6145 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6146 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6148 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6149 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6151 return build2_loc (loc, wanted_code, truth_type, lhs, rhs);
6154 return 0;
6157 /* Handle the case of comparisons with constants. If there is something in
6158 common between the masks, those bits of the constants must be the same.
6159 If not, the condition is always false. Test for this to avoid generating
6160 incorrect code below. */
6161 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask);
6162 if (! integer_zerop (result)
6163 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const),
6164 const_binop (BIT_AND_EXPR, result, r_const)) != 1)
6166 if (wanted_code == NE_EXPR)
6168 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6169 return constant_boolean_node (true, truth_type);
6171 else
6173 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6174 return constant_boolean_node (false, truth_type);
6178 if (lnbitpos < 0)
6179 return 0;
6181 /* Construct the expression we will return. First get the component
6182 reference we will make. Unless the mask is all ones the width of
6183 that field, perform the mask operation. Then compare with the
6184 merged constant. */
6185 result = make_bit_field_ref (loc, ll_inner, ll_arg,
6186 lntype, lnbitsize, lnbitpos,
6187 ll_unsignedp || rl_unsignedp, ll_reversep);
6189 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask);
6190 if (! all_ones_mask_p (ll_mask, lnbitsize))
6191 result = build2_loc (loc, BIT_AND_EXPR, lntype, result, ll_mask);
6193 return build2_loc (loc, wanted_code, truth_type, result,
6194 const_binop (BIT_IOR_EXPR, l_const, r_const));
6197 /* T is an integer expression that is being multiplied, divided, or taken a
6198 modulus (CODE says which and what kind of divide or modulus) by a
6199 constant C. See if we can eliminate that operation by folding it with
6200 other operations already in T. WIDE_TYPE, if non-null, is a type that
6201 should be used for the computation if wider than our type.
6203 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6204 (X * 2) + (Y * 4). We must, however, be assured that either the original
6205 expression would not overflow or that overflow is undefined for the type
6206 in the language in question.
6208 If we return a non-null expression, it is an equivalent form of the
6209 original computation, but need not be in the original type.
6211 We set *STRICT_OVERFLOW_P to true if the return values depends on
6212 signed overflow being undefined. Otherwise we do not change
6213 *STRICT_OVERFLOW_P. */
6215 static tree
6216 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6217 bool *strict_overflow_p)
6219 /* To avoid exponential search depth, refuse to allow recursion past
6220 three levels. Beyond that (1) it's highly unlikely that we'll find
6221 something interesting and (2) we've probably processed it before
6222 when we built the inner expression. */
6224 static int depth;
6225 tree ret;
6227 if (depth > 3)
6228 return NULL;
6230 depth++;
6231 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6232 depth--;
6234 return ret;
6237 static tree
6238 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6239 bool *strict_overflow_p)
6241 tree type = TREE_TYPE (t);
6242 enum tree_code tcode = TREE_CODE (t);
6243 tree ctype = (wide_type != 0
6244 && (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (wide_type))
6245 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type)))
6246 ? wide_type : type);
6247 tree t1, t2;
6248 int same_p = tcode == code;
6249 tree op0 = NULL_TREE, op1 = NULL_TREE;
6250 bool sub_strict_overflow_p;
6252 /* Don't deal with constants of zero here; they confuse the code below. */
6253 if (integer_zerop (c))
6254 return NULL_TREE;
6256 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6257 op0 = TREE_OPERAND (t, 0);
6259 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6260 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6262 /* Note that we need not handle conditional operations here since fold
6263 already handles those cases. So just do arithmetic here. */
6264 switch (tcode)
6266 case INTEGER_CST:
6267 /* For a constant, we can always simplify if we are a multiply
6268 or (for divide and modulus) if it is a multiple of our constant. */
6269 if (code == MULT_EXPR
6270 || wi::multiple_of_p (wi::to_wide (t), wi::to_wide (c),
6271 TYPE_SIGN (type)))
6273 tree tem = const_binop (code, fold_convert (ctype, t),
6274 fold_convert (ctype, c));
6275 /* If the multiplication overflowed, we lost information on it.
6276 See PR68142 and PR69845. */
6277 if (TREE_OVERFLOW (tem))
6278 return NULL_TREE;
6279 return tem;
6281 break;
6283 CASE_CONVERT: case NON_LVALUE_EXPR:
6284 /* If op0 is an expression ... */
6285 if ((COMPARISON_CLASS_P (op0)
6286 || UNARY_CLASS_P (op0)
6287 || BINARY_CLASS_P (op0)
6288 || VL_EXP_CLASS_P (op0)
6289 || EXPRESSION_CLASS_P (op0))
6290 /* ... and has wrapping overflow, and its type is smaller
6291 than ctype, then we cannot pass through as widening. */
6292 && (((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6293 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0)))
6294 && (TYPE_PRECISION (ctype)
6295 > TYPE_PRECISION (TREE_TYPE (op0))))
6296 /* ... or this is a truncation (t is narrower than op0),
6297 then we cannot pass through this narrowing. */
6298 || (TYPE_PRECISION (type)
6299 < TYPE_PRECISION (TREE_TYPE (op0)))
6300 /* ... or signedness changes for division or modulus,
6301 then we cannot pass through this conversion. */
6302 || (code != MULT_EXPR
6303 && (TYPE_UNSIGNED (ctype)
6304 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6305 /* ... or has undefined overflow while the converted to
6306 type has not, we cannot do the operation in the inner type
6307 as that would introduce undefined overflow. */
6308 || ((ANY_INTEGRAL_TYPE_P (TREE_TYPE (op0))
6309 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0)))
6310 && !TYPE_OVERFLOW_UNDEFINED (type))))
6311 break;
6313 /* Pass the constant down and see if we can make a simplification. If
6314 we can, replace this expression with the inner simplification for
6315 possible later conversion to our or some other type. */
6316 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6317 && TREE_CODE (t2) == INTEGER_CST
6318 && !TREE_OVERFLOW (t2)
6319 && (t1 = extract_muldiv (op0, t2, code,
6320 code == MULT_EXPR ? ctype : NULL_TREE,
6321 strict_overflow_p)) != 0)
6322 return t1;
6323 break;
6325 case ABS_EXPR:
6326 /* If widening the type changes it from signed to unsigned, then we
6327 must avoid building ABS_EXPR itself as unsigned. */
6328 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6330 tree cstype = (*signed_type_for) (ctype);
6331 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6332 != 0)
6334 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6335 return fold_convert (ctype, t1);
6337 break;
6339 /* If the constant is negative, we cannot simplify this. */
6340 if (tree_int_cst_sgn (c) == -1)
6341 break;
6342 /* FALLTHROUGH */
6343 case NEGATE_EXPR:
6344 /* For division and modulus, type can't be unsigned, as e.g.
6345 (-(x / 2U)) / 2U isn't equal to -((x / 2U) / 2U) for x >= 2.
6346 For signed types, even with wrapping overflow, this is fine. */
6347 if (code != MULT_EXPR && TYPE_UNSIGNED (type))
6348 break;
6349 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6350 != 0)
6351 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6352 break;
6354 case MIN_EXPR: case MAX_EXPR:
6355 /* If widening the type changes the signedness, then we can't perform
6356 this optimization as that changes the result. */
6357 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6358 break;
6360 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6361 sub_strict_overflow_p = false;
6362 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6363 &sub_strict_overflow_p)) != 0
6364 && (t2 = extract_muldiv (op1, c, code, wide_type,
6365 &sub_strict_overflow_p)) != 0)
6367 if (tree_int_cst_sgn (c) < 0)
6368 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6369 if (sub_strict_overflow_p)
6370 *strict_overflow_p = true;
6371 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6372 fold_convert (ctype, t2));
6374 break;
6376 case LSHIFT_EXPR: case RSHIFT_EXPR:
6377 /* If the second operand is constant, this is a multiplication
6378 or floor division, by a power of two, so we can treat it that
6379 way unless the multiplier or divisor overflows. Signed
6380 left-shift overflow is implementation-defined rather than
6381 undefined in C90, so do not convert signed left shift into
6382 multiplication. */
6383 if (TREE_CODE (op1) == INTEGER_CST
6384 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6385 /* const_binop may not detect overflow correctly,
6386 so check for it explicitly here. */
6387 && wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
6388 wi::to_wide (op1))
6389 && (t1 = fold_convert (ctype,
6390 const_binop (LSHIFT_EXPR, size_one_node,
6391 op1))) != 0
6392 && !TREE_OVERFLOW (t1))
6393 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6394 ? MULT_EXPR : FLOOR_DIV_EXPR,
6395 ctype,
6396 fold_convert (ctype, op0),
6397 t1),
6398 c, code, wide_type, strict_overflow_p);
6399 break;
6401 case PLUS_EXPR: case MINUS_EXPR:
6402 /* See if we can eliminate the operation on both sides. If we can, we
6403 can return a new PLUS or MINUS. If we can't, the only remaining
6404 cases where we can do anything are if the second operand is a
6405 constant. */
6406 sub_strict_overflow_p = false;
6407 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6408 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6409 if (t1 != 0 && t2 != 0
6410 && TYPE_OVERFLOW_WRAPS (ctype)
6411 && (code == MULT_EXPR
6412 /* If not multiplication, we can only do this if both operands
6413 are divisible by c. */
6414 || (multiple_of_p (ctype, op0, c)
6415 && multiple_of_p (ctype, op1, c))))
6417 if (sub_strict_overflow_p)
6418 *strict_overflow_p = true;
6419 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6420 fold_convert (ctype, t2));
6423 /* If this was a subtraction, negate OP1 and set it to be an addition.
6424 This simplifies the logic below. */
6425 if (tcode == MINUS_EXPR)
6427 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6428 /* If OP1 was not easily negatable, the constant may be OP0. */
6429 if (TREE_CODE (op0) == INTEGER_CST)
6431 std::swap (op0, op1);
6432 std::swap (t1, t2);
6436 if (TREE_CODE (op1) != INTEGER_CST)
6437 break;
6439 /* If either OP1 or C are negative, this optimization is not safe for
6440 some of the division and remainder types while for others we need
6441 to change the code. */
6442 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6444 if (code == CEIL_DIV_EXPR)
6445 code = FLOOR_DIV_EXPR;
6446 else if (code == FLOOR_DIV_EXPR)
6447 code = CEIL_DIV_EXPR;
6448 else if (code != MULT_EXPR
6449 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6450 break;
6453 /* If it's a multiply or a division/modulus operation of a multiple
6454 of our constant, do the operation and verify it doesn't overflow. */
6455 if (code == MULT_EXPR
6456 || wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6457 TYPE_SIGN (type)))
6459 op1 = const_binop (code, fold_convert (ctype, op1),
6460 fold_convert (ctype, c));
6461 /* We allow the constant to overflow with wrapping semantics. */
6462 if (op1 == 0
6463 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6464 break;
6466 else
6467 break;
6469 /* If we have an unsigned type, we cannot widen the operation since it
6470 will change the result if the original computation overflowed. */
6471 if (TYPE_UNSIGNED (ctype) && ctype != type)
6472 break;
6474 /* The last case is if we are a multiply. In that case, we can
6475 apply the distributive law to commute the multiply and addition
6476 if the multiplication of the constants doesn't overflow
6477 and overflow is defined. With undefined overflow
6478 op0 * c might overflow, while (op0 + orig_op1) * c doesn't.
6479 But fold_plusminus_mult_expr would factor back any power-of-two
6480 value so do not distribute in the first place in this case. */
6481 if (code == MULT_EXPR
6482 && TYPE_OVERFLOW_WRAPS (ctype)
6483 && !(tree_fits_shwi_p (c) && pow2p_hwi (absu_hwi (tree_to_shwi (c)))))
6484 return fold_build2 (tcode, ctype,
6485 fold_build2 (code, ctype,
6486 fold_convert (ctype, op0),
6487 fold_convert (ctype, c)),
6488 op1);
6490 break;
6492 case MULT_EXPR:
6493 /* We have a special case here if we are doing something like
6494 (C * 8) % 4 since we know that's zero. */
6495 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6496 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6497 /* If the multiplication can overflow we cannot optimize this. */
6498 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6499 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6500 && wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6501 TYPE_SIGN (type)))
6503 *strict_overflow_p = true;
6504 return omit_one_operand (type, integer_zero_node, op0);
6507 /* ... fall through ... */
6509 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6510 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6511 /* If we can extract our operation from the LHS, do so and return a
6512 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6513 do something only if the second operand is a constant. */
6514 if (same_p
6515 && TYPE_OVERFLOW_WRAPS (ctype)
6516 && (t1 = extract_muldiv (op0, c, code, wide_type,
6517 strict_overflow_p)) != 0)
6518 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6519 fold_convert (ctype, op1));
6520 else if (tcode == MULT_EXPR && code == MULT_EXPR
6521 && TYPE_OVERFLOW_WRAPS (ctype)
6522 && (t1 = extract_muldiv (op1, c, code, wide_type,
6523 strict_overflow_p)) != 0)
6524 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6525 fold_convert (ctype, t1));
6526 else if (TREE_CODE (op1) != INTEGER_CST)
6527 return 0;
6529 /* If these are the same operation types, we can associate them
6530 assuming no overflow. */
6531 if (tcode == code)
6533 bool overflow_p = false;
6534 wi::overflow_type overflow_mul;
6535 signop sign = TYPE_SIGN (ctype);
6536 unsigned prec = TYPE_PRECISION (ctype);
6537 wide_int mul = wi::mul (wi::to_wide (op1, prec),
6538 wi::to_wide (c, prec),
6539 sign, &overflow_mul);
6540 overflow_p = TREE_OVERFLOW (c) | TREE_OVERFLOW (op1);
6541 if (overflow_mul
6542 && ((sign == UNSIGNED && tcode != MULT_EXPR) || sign == SIGNED))
6543 overflow_p = true;
6544 if (!overflow_p)
6545 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6546 wide_int_to_tree (ctype, mul));
6549 /* If these operations "cancel" each other, we have the main
6550 optimizations of this pass, which occur when either constant is a
6551 multiple of the other, in which case we replace this with either an
6552 operation or CODE or TCODE.
6554 If we have an unsigned type, we cannot do this since it will change
6555 the result if the original computation overflowed. */
6556 if (TYPE_OVERFLOW_UNDEFINED (ctype)
6557 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6558 || (tcode == MULT_EXPR
6559 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6560 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6561 && code != MULT_EXPR)))
6563 if (wi::multiple_of_p (wi::to_wide (op1), wi::to_wide (c),
6564 TYPE_SIGN (type)))
6566 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6567 *strict_overflow_p = true;
6568 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6569 fold_convert (ctype,
6570 const_binop (TRUNC_DIV_EXPR,
6571 op1, c)));
6573 else if (wi::multiple_of_p (wi::to_wide (c), wi::to_wide (op1),
6574 TYPE_SIGN (type)))
6576 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6577 *strict_overflow_p = true;
6578 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6579 fold_convert (ctype,
6580 const_binop (TRUNC_DIV_EXPR,
6581 c, op1)));
6584 break;
6586 default:
6587 break;
6590 return 0;
6593 /* Return a node which has the indicated constant VALUE (either 0 or
6594 1 for scalars or {-1,-1,..} or {0,0,...} for vectors),
6595 and is of the indicated TYPE. */
6597 tree
6598 constant_boolean_node (bool value, tree type)
6600 if (type == integer_type_node)
6601 return value ? integer_one_node : integer_zero_node;
6602 else if (type == boolean_type_node)
6603 return value ? boolean_true_node : boolean_false_node;
6604 else if (TREE_CODE (type) == VECTOR_TYPE)
6605 return build_vector_from_val (type,
6606 build_int_cst (TREE_TYPE (type),
6607 value ? -1 : 0));
6608 else
6609 return fold_convert (type, value ? integer_one_node : integer_zero_node);
6613 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6614 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6615 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6616 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6617 COND is the first argument to CODE; otherwise (as in the example
6618 given here), it is the second argument. TYPE is the type of the
6619 original expression. Return NULL_TREE if no simplification is
6620 possible. */
6622 static tree
6623 fold_binary_op_with_conditional_arg (location_t loc,
6624 enum tree_code code,
6625 tree type, tree op0, tree op1,
6626 tree cond, tree arg, int cond_first_p)
6628 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6629 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6630 tree test, true_value, false_value;
6631 tree lhs = NULL_TREE;
6632 tree rhs = NULL_TREE;
6633 enum tree_code cond_code = COND_EXPR;
6635 /* Do not move possibly trapping operations into the conditional as this
6636 pessimizes code and causes gimplification issues when applied late. */
6637 if (operation_could_trap_p (code, FLOAT_TYPE_P (type),
6638 ANY_INTEGRAL_TYPE_P (type)
6639 && TYPE_OVERFLOW_TRAPS (type), op1))
6640 return NULL_TREE;
6642 if (TREE_CODE (cond) == COND_EXPR
6643 || TREE_CODE (cond) == VEC_COND_EXPR)
6645 test = TREE_OPERAND (cond, 0);
6646 true_value = TREE_OPERAND (cond, 1);
6647 false_value = TREE_OPERAND (cond, 2);
6648 /* If this operand throws an expression, then it does not make
6649 sense to try to perform a logical or arithmetic operation
6650 involving it. */
6651 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6652 lhs = true_value;
6653 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6654 rhs = false_value;
6656 else if (!(TREE_CODE (type) != VECTOR_TYPE
6657 && TREE_CODE (TREE_TYPE (cond)) == VECTOR_TYPE))
6659 tree testtype = TREE_TYPE (cond);
6660 test = cond;
6661 true_value = constant_boolean_node (true, testtype);
6662 false_value = constant_boolean_node (false, testtype);
6664 else
6665 /* Detect the case of mixing vector and scalar types - bail out. */
6666 return NULL_TREE;
6668 if (TREE_CODE (TREE_TYPE (test)) == VECTOR_TYPE)
6669 cond_code = VEC_COND_EXPR;
6671 /* This transformation is only worthwhile if we don't have to wrap ARG
6672 in a SAVE_EXPR and the operation can be simplified without recursing
6673 on at least one of the branches once its pushed inside the COND_EXPR. */
6674 if (!TREE_CONSTANT (arg)
6675 && (TREE_SIDE_EFFECTS (arg)
6676 || TREE_CODE (arg) == COND_EXPR || TREE_CODE (arg) == VEC_COND_EXPR
6677 || TREE_CONSTANT (true_value) || TREE_CONSTANT (false_value)))
6678 return NULL_TREE;
6680 arg = fold_convert_loc (loc, arg_type, arg);
6681 if (lhs == 0)
6683 true_value = fold_convert_loc (loc, cond_type, true_value);
6684 if (cond_first_p)
6685 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6686 else
6687 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6689 if (rhs == 0)
6691 false_value = fold_convert_loc (loc, cond_type, false_value);
6692 if (cond_first_p)
6693 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6694 else
6695 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6698 /* Check that we have simplified at least one of the branches. */
6699 if (!TREE_CONSTANT (arg) && !TREE_CONSTANT (lhs) && !TREE_CONSTANT (rhs))
6700 return NULL_TREE;
6702 return fold_build3_loc (loc, cond_code, type, test, lhs, rhs);
6706 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6708 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6709 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6710 ADDEND is the same as X.
6712 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6713 and finite. The problematic cases are when X is zero, and its mode
6714 has signed zeros. In the case of rounding towards -infinity,
6715 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6716 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6718 bool
6719 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6721 if (!real_zerop (addend))
6722 return false;
6724 /* Don't allow the fold with -fsignaling-nans. */
6725 if (HONOR_SNANS (type))
6726 return false;
6728 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6729 if (!HONOR_SIGNED_ZEROS (type))
6730 return true;
6732 /* There is no case that is safe for all rounding modes. */
6733 if (HONOR_SIGN_DEPENDENT_ROUNDING (type))
6734 return false;
6736 /* In a vector or complex, we would need to check the sign of all zeros. */
6737 if (TREE_CODE (addend) == VECTOR_CST)
6738 addend = uniform_vector_p (addend);
6739 if (!addend || TREE_CODE (addend) != REAL_CST)
6740 return false;
6742 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6743 if (REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6744 negate = !negate;
6746 /* The mode has signed zeros, and we have to honor their sign.
6747 In this situation, there is only one case we can return true for.
6748 X - 0 is the same as X with default rounding. */
6749 return negate;
6752 /* Subroutine of match.pd that optimizes comparisons of a division by
6753 a nonzero integer constant against an integer constant, i.e.
6754 X/C1 op C2.
6756 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6757 GE_EXPR or LE_EXPR. ARG01 and ARG1 must be a INTEGER_CST. */
6759 enum tree_code
6760 fold_div_compare (enum tree_code code, tree c1, tree c2, tree *lo,
6761 tree *hi, bool *neg_overflow)
6763 tree prod, tmp, type = TREE_TYPE (c1);
6764 signop sign = TYPE_SIGN (type);
6765 wi::overflow_type overflow;
6767 /* We have to do this the hard way to detect unsigned overflow.
6768 prod = int_const_binop (MULT_EXPR, c1, c2); */
6769 wide_int val = wi::mul (wi::to_wide (c1), wi::to_wide (c2), sign, &overflow);
6770 prod = force_fit_type (type, val, -1, overflow);
6771 *neg_overflow = false;
6773 if (sign == UNSIGNED)
6775 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6776 *lo = prod;
6778 /* Likewise *hi = int_const_binop (PLUS_EXPR, prod, tmp). */
6779 val = wi::add (wi::to_wide (prod), wi::to_wide (tmp), sign, &overflow);
6780 *hi = force_fit_type (type, val, -1, overflow | TREE_OVERFLOW (prod));
6782 else if (tree_int_cst_sgn (c1) >= 0)
6784 tmp = int_const_binop (MINUS_EXPR, c1, build_int_cst (type, 1));
6785 switch (tree_int_cst_sgn (c2))
6787 case -1:
6788 *neg_overflow = true;
6789 *lo = int_const_binop (MINUS_EXPR, prod, tmp);
6790 *hi = prod;
6791 break;
6793 case 0:
6794 *lo = fold_negate_const (tmp, type);
6795 *hi = tmp;
6796 break;
6798 case 1:
6799 *hi = int_const_binop (PLUS_EXPR, prod, tmp);
6800 *lo = prod;
6801 break;
6803 default:
6804 gcc_unreachable ();
6807 else
6809 /* A negative divisor reverses the relational operators. */
6810 code = swap_tree_comparison (code);
6812 tmp = int_const_binop (PLUS_EXPR, c1, build_int_cst (type, 1));
6813 switch (tree_int_cst_sgn (c2))
6815 case -1:
6816 *hi = int_const_binop (MINUS_EXPR, prod, tmp);
6817 *lo = prod;
6818 break;
6820 case 0:
6821 *hi = fold_negate_const (tmp, type);
6822 *lo = tmp;
6823 break;
6825 case 1:
6826 *neg_overflow = true;
6827 *lo = int_const_binop (PLUS_EXPR, prod, tmp);
6828 *hi = prod;
6829 break;
6831 default:
6832 gcc_unreachable ();
6836 if (code != EQ_EXPR && code != NE_EXPR)
6837 return code;
6839 if (TREE_OVERFLOW (*lo)
6840 || operand_equal_p (*lo, TYPE_MIN_VALUE (type), 0))
6841 *lo = NULL_TREE;
6842 if (TREE_OVERFLOW (*hi)
6843 || operand_equal_p (*hi, TYPE_MAX_VALUE (type), 0))
6844 *hi = NULL_TREE;
6846 return code;
6850 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6851 equality/inequality test, then return a simplified form of the test
6852 using a sign testing. Otherwise return NULL. TYPE is the desired
6853 result type. */
6855 static tree
6856 fold_single_bit_test_into_sign_test (location_t loc,
6857 enum tree_code code, tree arg0, tree arg1,
6858 tree result_type)
6860 /* If this is testing a single bit, we can optimize the test. */
6861 if ((code == NE_EXPR || code == EQ_EXPR)
6862 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6863 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6865 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6866 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6867 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6869 if (arg00 != NULL_TREE
6870 /* This is only a win if casting to a signed type is cheap,
6871 i.e. when arg00's type is not a partial mode. */
6872 && type_has_mode_precision_p (TREE_TYPE (arg00)))
6874 tree stype = signed_type_for (TREE_TYPE (arg00));
6875 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6876 result_type,
6877 fold_convert_loc (loc, stype, arg00),
6878 build_int_cst (stype, 0));
6882 return NULL_TREE;
6885 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6886 equality/inequality test, then return a simplified form of
6887 the test using shifts and logical operations. Otherwise return
6888 NULL. TYPE is the desired result type. */
6890 tree
6891 fold_single_bit_test (location_t loc, enum tree_code code,
6892 tree arg0, tree arg1, tree result_type)
6894 /* If this is testing a single bit, we can optimize the test. */
6895 if ((code == NE_EXPR || code == EQ_EXPR)
6896 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6897 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6899 tree inner = TREE_OPERAND (arg0, 0);
6900 tree type = TREE_TYPE (arg0);
6901 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6902 scalar_int_mode operand_mode = SCALAR_INT_TYPE_MODE (type);
6903 int ops_unsigned;
6904 tree signed_type, unsigned_type, intermediate_type;
6905 tree tem, one;
6907 /* First, see if we can fold the single bit test into a sign-bit
6908 test. */
6909 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
6910 result_type);
6911 if (tem)
6912 return tem;
6914 /* Otherwise we have (A & C) != 0 where C is a single bit,
6915 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6916 Similarly for (A & C) == 0. */
6918 /* If INNER is a right shift of a constant and it plus BITNUM does
6919 not overflow, adjust BITNUM and INNER. */
6920 if (TREE_CODE (inner) == RSHIFT_EXPR
6921 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6922 && bitnum < TYPE_PRECISION (type)
6923 && wi::ltu_p (wi::to_wide (TREE_OPERAND (inner, 1)),
6924 TYPE_PRECISION (type) - bitnum))
6926 bitnum += tree_to_uhwi (TREE_OPERAND (inner, 1));
6927 inner = TREE_OPERAND (inner, 0);
6930 /* If we are going to be able to omit the AND below, we must do our
6931 operations as unsigned. If we must use the AND, we have a choice.
6932 Normally unsigned is faster, but for some machines signed is. */
6933 ops_unsigned = (load_extend_op (operand_mode) == SIGN_EXTEND
6934 && !flag_syntax_only) ? 0 : 1;
6936 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6937 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6938 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6939 inner = fold_convert_loc (loc, intermediate_type, inner);
6941 if (bitnum != 0)
6942 inner = build2 (RSHIFT_EXPR, intermediate_type,
6943 inner, size_int (bitnum));
6945 one = build_int_cst (intermediate_type, 1);
6947 if (code == EQ_EXPR)
6948 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
6950 /* Put the AND last so it can combine with more things. */
6951 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6953 /* Make sure to return the proper type. */
6954 inner = fold_convert_loc (loc, result_type, inner);
6956 return inner;
6958 return NULL_TREE;
6961 /* Test whether it is preferable two swap two operands, ARG0 and
6962 ARG1, for example because ARG0 is an integer constant and ARG1
6963 isn't. */
6965 bool
6966 tree_swap_operands_p (const_tree arg0, const_tree arg1)
6968 if (CONSTANT_CLASS_P (arg1))
6969 return 0;
6970 if (CONSTANT_CLASS_P (arg0))
6971 return 1;
6973 STRIP_NOPS (arg0);
6974 STRIP_NOPS (arg1);
6976 if (TREE_CONSTANT (arg1))
6977 return 0;
6978 if (TREE_CONSTANT (arg0))
6979 return 1;
6981 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6982 for commutative and comparison operators. Ensuring a canonical
6983 form allows the optimizers to find additional redundancies without
6984 having to explicitly check for both orderings. */
6985 if (TREE_CODE (arg0) == SSA_NAME
6986 && TREE_CODE (arg1) == SSA_NAME
6987 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6988 return 1;
6990 /* Put SSA_NAMEs last. */
6991 if (TREE_CODE (arg1) == SSA_NAME)
6992 return 0;
6993 if (TREE_CODE (arg0) == SSA_NAME)
6994 return 1;
6996 /* Put variables last. */
6997 if (DECL_P (arg1))
6998 return 0;
6999 if (DECL_P (arg0))
7000 return 1;
7002 return 0;
7006 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7007 means A >= Y && A != MAX, but in this case we know that
7008 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7010 static tree
7011 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7013 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7015 if (TREE_CODE (bound) == LT_EXPR)
7016 a = TREE_OPERAND (bound, 0);
7017 else if (TREE_CODE (bound) == GT_EXPR)
7018 a = TREE_OPERAND (bound, 1);
7019 else
7020 return NULL_TREE;
7022 typea = TREE_TYPE (a);
7023 if (!INTEGRAL_TYPE_P (typea)
7024 && !POINTER_TYPE_P (typea))
7025 return NULL_TREE;
7027 if (TREE_CODE (ineq) == LT_EXPR)
7029 a1 = TREE_OPERAND (ineq, 1);
7030 y = TREE_OPERAND (ineq, 0);
7032 else if (TREE_CODE (ineq) == GT_EXPR)
7034 a1 = TREE_OPERAND (ineq, 0);
7035 y = TREE_OPERAND (ineq, 1);
7037 else
7038 return NULL_TREE;
7040 if (TREE_TYPE (a1) != typea)
7041 return NULL_TREE;
7043 if (POINTER_TYPE_P (typea))
7045 /* Convert the pointer types into integer before taking the difference. */
7046 tree ta = fold_convert_loc (loc, ssizetype, a);
7047 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7048 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7050 else
7051 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7053 if (!diff || !integer_onep (diff))
7054 return NULL_TREE;
7056 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7059 /* Fold a sum or difference of at least one multiplication.
7060 Returns the folded tree or NULL if no simplification could be made. */
7062 static tree
7063 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7064 tree arg0, tree arg1)
7066 tree arg00, arg01, arg10, arg11;
7067 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7069 /* (A * C) +- (B * C) -> (A+-B) * C.
7070 (A * C) +- A -> A * (C+-1).
7071 We are most concerned about the case where C is a constant,
7072 but other combinations show up during loop reduction. Since
7073 it is not difficult, try all four possibilities. */
7075 if (TREE_CODE (arg0) == MULT_EXPR)
7077 arg00 = TREE_OPERAND (arg0, 0);
7078 arg01 = TREE_OPERAND (arg0, 1);
7080 else if (TREE_CODE (arg0) == INTEGER_CST)
7082 arg00 = build_one_cst (type);
7083 arg01 = arg0;
7085 else
7087 /* We cannot generate constant 1 for fract. */
7088 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7089 return NULL_TREE;
7090 arg00 = arg0;
7091 arg01 = build_one_cst (type);
7093 if (TREE_CODE (arg1) == MULT_EXPR)
7095 arg10 = TREE_OPERAND (arg1, 0);
7096 arg11 = TREE_OPERAND (arg1, 1);
7098 else if (TREE_CODE (arg1) == INTEGER_CST)
7100 arg10 = build_one_cst (type);
7101 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7102 the purpose of this canonicalization. */
7103 if (wi::neg_p (wi::to_wide (arg1), TYPE_SIGN (TREE_TYPE (arg1)))
7104 && negate_expr_p (arg1)
7105 && code == PLUS_EXPR)
7107 arg11 = negate_expr (arg1);
7108 code = MINUS_EXPR;
7110 else
7111 arg11 = arg1;
7113 else
7115 /* We cannot generate constant 1 for fract. */
7116 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7117 return NULL_TREE;
7118 arg10 = arg1;
7119 arg11 = build_one_cst (type);
7121 same = NULL_TREE;
7123 /* Prefer factoring a common non-constant. */
7124 if (operand_equal_p (arg00, arg10, 0))
7125 same = arg00, alt0 = arg01, alt1 = arg11;
7126 else if (operand_equal_p (arg01, arg11, 0))
7127 same = arg01, alt0 = arg00, alt1 = arg10;
7128 else if (operand_equal_p (arg00, arg11, 0))
7129 same = arg00, alt0 = arg01, alt1 = arg10;
7130 else if (operand_equal_p (arg01, arg10, 0))
7131 same = arg01, alt0 = arg00, alt1 = arg11;
7133 /* No identical multiplicands; see if we can find a common
7134 power-of-two factor in non-power-of-two multiplies. This
7135 can help in multi-dimensional array access. */
7136 else if (tree_fits_shwi_p (arg01) && tree_fits_shwi_p (arg11))
7138 HOST_WIDE_INT int01 = tree_to_shwi (arg01);
7139 HOST_WIDE_INT int11 = tree_to_shwi (arg11);
7140 HOST_WIDE_INT tmp;
7141 bool swap = false;
7142 tree maybe_same;
7144 /* Move min of absolute values to int11. */
7145 if (absu_hwi (int01) < absu_hwi (int11))
7147 tmp = int01, int01 = int11, int11 = tmp;
7148 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7149 maybe_same = arg01;
7150 swap = true;
7152 else
7153 maybe_same = arg11;
7155 const unsigned HOST_WIDE_INT factor = absu_hwi (int11);
7156 if (factor > 1
7157 && pow2p_hwi (factor)
7158 && (int01 & (factor - 1)) == 0
7159 /* The remainder should not be a constant, otherwise we
7160 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7161 increased the number of multiplications necessary. */
7162 && TREE_CODE (arg10) != INTEGER_CST)
7164 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7165 build_int_cst (TREE_TYPE (arg00),
7166 int01 / int11));
7167 alt1 = arg10;
7168 same = maybe_same;
7169 if (swap)
7170 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7174 if (!same)
7175 return NULL_TREE;
7177 if (! ANY_INTEGRAL_TYPE_P (type)
7178 || TYPE_OVERFLOW_WRAPS (type)
7179 /* We are neither factoring zero nor minus one. */
7180 || TREE_CODE (same) == INTEGER_CST)
7181 return fold_build2_loc (loc, MULT_EXPR, type,
7182 fold_build2_loc (loc, code, type,
7183 fold_convert_loc (loc, type, alt0),
7184 fold_convert_loc (loc, type, alt1)),
7185 fold_convert_loc (loc, type, same));
7187 /* Same may be zero and thus the operation 'code' may overflow. Likewise
7188 same may be minus one and thus the multiplication may overflow. Perform
7189 the sum operation in an unsigned type. */
7190 tree utype = unsigned_type_for (type);
7191 tree tem = fold_build2_loc (loc, code, utype,
7192 fold_convert_loc (loc, utype, alt0),
7193 fold_convert_loc (loc, utype, alt1));
7194 /* If the sum evaluated to a constant that is not -INF the multiplication
7195 cannot overflow. */
7196 if (TREE_CODE (tem) == INTEGER_CST
7197 && (wi::to_wide (tem)
7198 != wi::min_value (TYPE_PRECISION (utype), SIGNED)))
7199 return fold_build2_loc (loc, MULT_EXPR, type,
7200 fold_convert (type, tem), same);
7202 /* Do not resort to unsigned multiplication because
7203 we lose the no-overflow property of the expression. */
7204 return NULL_TREE;
7207 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7208 specified by EXPR into the buffer PTR of length LEN bytes.
7209 Return the number of bytes placed in the buffer, or zero
7210 upon failure. */
7212 static int
7213 native_encode_int (const_tree expr, unsigned char *ptr, int len, int off)
7215 tree type = TREE_TYPE (expr);
7216 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7217 int byte, offset, word, words;
7218 unsigned char value;
7220 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7221 return 0;
7222 if (off == -1)
7223 off = 0;
7225 if (ptr == NULL)
7226 /* Dry run. */
7227 return MIN (len, total_bytes - off);
7229 words = total_bytes / UNITS_PER_WORD;
7231 for (byte = 0; byte < total_bytes; byte++)
7233 int bitpos = byte * BITS_PER_UNIT;
7234 /* Extend EXPR according to TYPE_SIGN if the precision isn't a whole
7235 number of bytes. */
7236 value = wi::extract_uhwi (wi::to_widest (expr), bitpos, BITS_PER_UNIT);
7238 if (total_bytes > UNITS_PER_WORD)
7240 word = byte / UNITS_PER_WORD;
7241 if (WORDS_BIG_ENDIAN)
7242 word = (words - 1) - word;
7243 offset = word * UNITS_PER_WORD;
7244 if (BYTES_BIG_ENDIAN)
7245 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7246 else
7247 offset += byte % UNITS_PER_WORD;
7249 else
7250 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7251 if (offset >= off && offset - off < len)
7252 ptr[offset - off] = value;
7254 return MIN (len, total_bytes - off);
7258 /* Subroutine of native_encode_expr. Encode the FIXED_CST
7259 specified by EXPR into the buffer PTR of length LEN bytes.
7260 Return the number of bytes placed in the buffer, or zero
7261 upon failure. */
7263 static int
7264 native_encode_fixed (const_tree expr, unsigned char *ptr, int len, int off)
7266 tree type = TREE_TYPE (expr);
7267 scalar_mode mode = SCALAR_TYPE_MODE (type);
7268 int total_bytes = GET_MODE_SIZE (mode);
7269 FIXED_VALUE_TYPE value;
7270 tree i_value, i_type;
7272 if (total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7273 return 0;
7275 i_type = lang_hooks.types.type_for_size (GET_MODE_BITSIZE (mode), 1);
7277 if (NULL_TREE == i_type || TYPE_PRECISION (i_type) != total_bytes)
7278 return 0;
7280 value = TREE_FIXED_CST (expr);
7281 i_value = double_int_to_tree (i_type, value.data);
7283 return native_encode_int (i_value, ptr, len, off);
7287 /* Subroutine of native_encode_expr. Encode the REAL_CST
7288 specified by EXPR into the buffer PTR of length LEN bytes.
7289 Return the number of bytes placed in the buffer, or zero
7290 upon failure. */
7292 static int
7293 native_encode_real (const_tree expr, unsigned char *ptr, int len, int off)
7295 tree type = TREE_TYPE (expr);
7296 int total_bytes = GET_MODE_SIZE (SCALAR_FLOAT_TYPE_MODE (type));
7297 int byte, offset, word, words, bitpos;
7298 unsigned char value;
7300 /* There are always 32 bits in each long, no matter the size of
7301 the hosts long. We handle floating point representations with
7302 up to 192 bits. */
7303 long tmp[6];
7305 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7306 return 0;
7307 if (off == -1)
7308 off = 0;
7310 if (ptr == NULL)
7311 /* Dry run. */
7312 return MIN (len, total_bytes - off);
7314 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7316 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7318 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7319 bitpos += BITS_PER_UNIT)
7321 byte = (bitpos / BITS_PER_UNIT) & 3;
7322 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7324 if (UNITS_PER_WORD < 4)
7326 word = byte / UNITS_PER_WORD;
7327 if (WORDS_BIG_ENDIAN)
7328 word = (words - 1) - word;
7329 offset = word * UNITS_PER_WORD;
7330 if (BYTES_BIG_ENDIAN)
7331 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7332 else
7333 offset += byte % UNITS_PER_WORD;
7335 else
7337 offset = byte;
7338 if (BYTES_BIG_ENDIAN)
7340 /* Reverse bytes within each long, or within the entire float
7341 if it's smaller than a long (for HFmode). */
7342 offset = MIN (3, total_bytes - 1) - offset;
7343 gcc_assert (offset >= 0);
7346 offset = offset + ((bitpos / BITS_PER_UNIT) & ~3);
7347 if (offset >= off
7348 && offset - off < len)
7349 ptr[offset - off] = value;
7351 return MIN (len, total_bytes - off);
7354 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7355 specified by EXPR into the buffer PTR of length LEN bytes.
7356 Return the number of bytes placed in the buffer, or zero
7357 upon failure. */
7359 static int
7360 native_encode_complex (const_tree expr, unsigned char *ptr, int len, int off)
7362 int rsize, isize;
7363 tree part;
7365 part = TREE_REALPART (expr);
7366 rsize = native_encode_expr (part, ptr, len, off);
7367 if (off == -1 && rsize == 0)
7368 return 0;
7369 part = TREE_IMAGPART (expr);
7370 if (off != -1)
7371 off = MAX (0, off - GET_MODE_SIZE (SCALAR_TYPE_MODE (TREE_TYPE (part))));
7372 isize = native_encode_expr (part, ptr ? ptr + rsize : NULL,
7373 len - rsize, off);
7374 if (off == -1 && isize != rsize)
7375 return 0;
7376 return rsize + isize;
7380 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7381 specified by EXPR into the buffer PTR of length LEN bytes.
7382 Return the number of bytes placed in the buffer, or zero
7383 upon failure. */
7385 static int
7386 native_encode_vector (const_tree expr, unsigned char *ptr, int len, int off)
7388 unsigned HOST_WIDE_INT i, count;
7389 int size, offset;
7390 tree itype, elem;
7392 offset = 0;
7393 if (!VECTOR_CST_NELTS (expr).is_constant (&count))
7394 return 0;
7395 itype = TREE_TYPE (TREE_TYPE (expr));
7396 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (itype));
7397 for (i = 0; i < count; i++)
7399 if (off >= size)
7401 off -= size;
7402 continue;
7404 elem = VECTOR_CST_ELT (expr, i);
7405 int res = native_encode_expr (elem, ptr ? ptr + offset : NULL,
7406 len - offset, off);
7407 if ((off == -1 && res != size) || res == 0)
7408 return 0;
7409 offset += res;
7410 if (offset >= len)
7411 return (off == -1 && i < count - 1) ? 0 : offset;
7412 if (off != -1)
7413 off = 0;
7415 return offset;
7419 /* Subroutine of native_encode_expr. Encode the STRING_CST
7420 specified by EXPR into the buffer PTR of length LEN bytes.
7421 Return the number of bytes placed in the buffer, or zero
7422 upon failure. */
7424 static int
7425 native_encode_string (const_tree expr, unsigned char *ptr, int len, int off)
7427 tree type = TREE_TYPE (expr);
7429 /* Wide-char strings are encoded in target byte-order so native
7430 encoding them is trivial. */
7431 if (BITS_PER_UNIT != CHAR_BIT
7432 || TREE_CODE (type) != ARRAY_TYPE
7433 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7434 || !tree_fits_shwi_p (TYPE_SIZE_UNIT (type)))
7435 return 0;
7437 HOST_WIDE_INT total_bytes = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (expr)));
7438 if ((off == -1 && total_bytes > len) || off >= total_bytes)
7439 return 0;
7440 if (off == -1)
7441 off = 0;
7442 if (ptr == NULL)
7443 /* Dry run. */;
7444 else if (TREE_STRING_LENGTH (expr) - off < MIN (total_bytes, len))
7446 int written = 0;
7447 if (off < TREE_STRING_LENGTH (expr))
7449 written = MIN (len, TREE_STRING_LENGTH (expr) - off);
7450 memcpy (ptr, TREE_STRING_POINTER (expr) + off, written);
7452 memset (ptr + written, 0,
7453 MIN (total_bytes - written, len - written));
7455 else
7456 memcpy (ptr, TREE_STRING_POINTER (expr) + off, MIN (total_bytes, len));
7457 return MIN (total_bytes - off, len);
7461 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7462 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7463 buffer PTR of length LEN bytes. If PTR is NULL, don't actually store
7464 anything, just do a dry run. If OFF is not -1 then start
7465 the encoding at byte offset OFF and encode at most LEN bytes.
7466 Return the number of bytes placed in the buffer, or zero upon failure. */
7469 native_encode_expr (const_tree expr, unsigned char *ptr, int len, int off)
7471 /* We don't support starting at negative offset and -1 is special. */
7472 if (off < -1)
7473 return 0;
7475 switch (TREE_CODE (expr))
7477 case INTEGER_CST:
7478 return native_encode_int (expr, ptr, len, off);
7480 case REAL_CST:
7481 return native_encode_real (expr, ptr, len, off);
7483 case FIXED_CST:
7484 return native_encode_fixed (expr, ptr, len, off);
7486 case COMPLEX_CST:
7487 return native_encode_complex (expr, ptr, len, off);
7489 case VECTOR_CST:
7490 return native_encode_vector (expr, ptr, len, off);
7492 case STRING_CST:
7493 return native_encode_string (expr, ptr, len, off);
7495 default:
7496 return 0;
7501 /* Subroutine of native_interpret_expr. Interpret the contents of
7502 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7503 If the buffer cannot be interpreted, return NULL_TREE. */
7505 static tree
7506 native_interpret_int (tree type, const unsigned char *ptr, int len)
7508 int total_bytes = GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (type));
7510 if (total_bytes > len
7511 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7512 return NULL_TREE;
7514 wide_int result = wi::from_buffer (ptr, total_bytes);
7516 return wide_int_to_tree (type, result);
7520 /* Subroutine of native_interpret_expr. Interpret the contents of
7521 the buffer PTR of length LEN as a FIXED_CST of type TYPE.
7522 If the buffer cannot be interpreted, return NULL_TREE. */
7524 static tree
7525 native_interpret_fixed (tree type, const unsigned char *ptr, int len)
7527 scalar_mode mode = SCALAR_TYPE_MODE (type);
7528 int total_bytes = GET_MODE_SIZE (mode);
7529 double_int result;
7530 FIXED_VALUE_TYPE fixed_value;
7532 if (total_bytes > len
7533 || total_bytes * BITS_PER_UNIT > HOST_BITS_PER_DOUBLE_INT)
7534 return NULL_TREE;
7536 result = double_int::from_buffer (ptr, total_bytes);
7537 fixed_value = fixed_from_double_int (result, mode);
7539 return build_fixed (type, fixed_value);
7543 /* Subroutine of native_interpret_expr. Interpret the contents of
7544 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7545 If the buffer cannot be interpreted, return NULL_TREE. */
7547 static tree
7548 native_interpret_real (tree type, const unsigned char *ptr, int len)
7550 scalar_float_mode mode = SCALAR_FLOAT_TYPE_MODE (type);
7551 int total_bytes = GET_MODE_SIZE (mode);
7552 unsigned char value;
7553 /* There are always 32 bits in each long, no matter the size of
7554 the hosts long. We handle floating point representations with
7555 up to 192 bits. */
7556 REAL_VALUE_TYPE r;
7557 long tmp[6];
7559 if (total_bytes > len || total_bytes > 24)
7560 return NULL_TREE;
7561 int words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7563 memset (tmp, 0, sizeof (tmp));
7564 for (int bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7565 bitpos += BITS_PER_UNIT)
7567 /* Both OFFSET and BYTE index within a long;
7568 bitpos indexes the whole float. */
7569 int offset, byte = (bitpos / BITS_PER_UNIT) & 3;
7570 if (UNITS_PER_WORD < 4)
7572 int word = byte / UNITS_PER_WORD;
7573 if (WORDS_BIG_ENDIAN)
7574 word = (words - 1) - word;
7575 offset = word * UNITS_PER_WORD;
7576 if (BYTES_BIG_ENDIAN)
7577 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7578 else
7579 offset += byte % UNITS_PER_WORD;
7581 else
7583 offset = byte;
7584 if (BYTES_BIG_ENDIAN)
7586 /* Reverse bytes within each long, or within the entire float
7587 if it's smaller than a long (for HFmode). */
7588 offset = MIN (3, total_bytes - 1) - offset;
7589 gcc_assert (offset >= 0);
7592 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7594 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7597 real_from_target (&r, tmp, mode);
7598 return build_real (type, r);
7602 /* Subroutine of native_interpret_expr. Interpret the contents of
7603 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7604 If the buffer cannot be interpreted, return NULL_TREE. */
7606 static tree
7607 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7609 tree etype, rpart, ipart;
7610 int size;
7612 etype = TREE_TYPE (type);
7613 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7614 if (size * 2 > len)
7615 return NULL_TREE;
7616 rpart = native_interpret_expr (etype, ptr, size);
7617 if (!rpart)
7618 return NULL_TREE;
7619 ipart = native_interpret_expr (etype, ptr+size, size);
7620 if (!ipart)
7621 return NULL_TREE;
7622 return build_complex (type, rpart, ipart);
7626 /* Subroutine of native_interpret_expr. Interpret the contents of
7627 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7628 If the buffer cannot be interpreted, return NULL_TREE. */
7630 static tree
7631 native_interpret_vector (tree type, const unsigned char *ptr, unsigned int len)
7633 tree etype, elem;
7634 unsigned int i, size;
7635 unsigned HOST_WIDE_INT count;
7637 etype = TREE_TYPE (type);
7638 size = GET_MODE_SIZE (SCALAR_TYPE_MODE (etype));
7639 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&count)
7640 || size * count > len)
7641 return NULL_TREE;
7643 tree_vector_builder elements (type, count, 1);
7644 for (i = 0; i < count; ++i)
7646 elem = native_interpret_expr (etype, ptr+(i*size), size);
7647 if (!elem)
7648 return NULL_TREE;
7649 elements.quick_push (elem);
7651 return elements.build ();
7655 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7656 the buffer PTR of length LEN as a constant of type TYPE. For
7657 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7658 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7659 return NULL_TREE. */
7661 tree
7662 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7664 switch (TREE_CODE (type))
7666 case INTEGER_TYPE:
7667 case ENUMERAL_TYPE:
7668 case BOOLEAN_TYPE:
7669 case POINTER_TYPE:
7670 case REFERENCE_TYPE:
7671 return native_interpret_int (type, ptr, len);
7673 case REAL_TYPE:
7674 return native_interpret_real (type, ptr, len);
7676 case FIXED_POINT_TYPE:
7677 return native_interpret_fixed (type, ptr, len);
7679 case COMPLEX_TYPE:
7680 return native_interpret_complex (type, ptr, len);
7682 case VECTOR_TYPE:
7683 return native_interpret_vector (type, ptr, len);
7685 default:
7686 return NULL_TREE;
7690 /* Returns true if we can interpret the contents of a native encoding
7691 as TYPE. */
7693 static bool
7694 can_native_interpret_type_p (tree type)
7696 switch (TREE_CODE (type))
7698 case INTEGER_TYPE:
7699 case ENUMERAL_TYPE:
7700 case BOOLEAN_TYPE:
7701 case POINTER_TYPE:
7702 case REFERENCE_TYPE:
7703 case FIXED_POINT_TYPE:
7704 case REAL_TYPE:
7705 case COMPLEX_TYPE:
7706 case VECTOR_TYPE:
7707 return true;
7708 default:
7709 return false;
7714 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7715 TYPE at compile-time. If we're unable to perform the conversion
7716 return NULL_TREE. */
7718 static tree
7719 fold_view_convert_expr (tree type, tree expr)
7721 /* We support up to 512-bit values (for V8DFmode). */
7722 unsigned char buffer[64];
7723 int len;
7725 /* Check that the host and target are sane. */
7726 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7727 return NULL_TREE;
7729 len = native_encode_expr (expr, buffer, sizeof (buffer));
7730 if (len == 0)
7731 return NULL_TREE;
7733 return native_interpret_expr (type, buffer, len);
7736 /* Build an expression for the address of T. Folds away INDIRECT_REF
7737 to avoid confusing the gimplify process. */
7739 tree
7740 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
7742 /* The size of the object is not relevant when talking about its address. */
7743 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7744 t = TREE_OPERAND (t, 0);
7746 if (TREE_CODE (t) == INDIRECT_REF)
7748 t = TREE_OPERAND (t, 0);
7750 if (TREE_TYPE (t) != ptrtype)
7751 t = build1_loc (loc, NOP_EXPR, ptrtype, t);
7753 else if (TREE_CODE (t) == MEM_REF
7754 && integer_zerop (TREE_OPERAND (t, 1)))
7755 return TREE_OPERAND (t, 0);
7756 else if (TREE_CODE (t) == MEM_REF
7757 && TREE_CODE (TREE_OPERAND (t, 0)) == INTEGER_CST)
7758 return fold_binary (POINTER_PLUS_EXPR, ptrtype,
7759 TREE_OPERAND (t, 0),
7760 convert_to_ptrofftype (TREE_OPERAND (t, 1)));
7761 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
7763 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
7765 if (TREE_TYPE (t) != ptrtype)
7766 t = fold_convert_loc (loc, ptrtype, t);
7768 else
7769 t = build1_loc (loc, ADDR_EXPR, ptrtype, t);
7771 return t;
7774 /* Build an expression for the address of T. */
7776 tree
7777 build_fold_addr_expr_loc (location_t loc, tree t)
7779 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7781 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
7784 /* Fold a unary expression of code CODE and type TYPE with operand
7785 OP0. Return the folded expression if folding is successful.
7786 Otherwise, return NULL_TREE. */
7788 tree
7789 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
7791 tree tem;
7792 tree arg0;
7793 enum tree_code_class kind = TREE_CODE_CLASS (code);
7795 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7796 && TREE_CODE_LENGTH (code) == 1);
7798 arg0 = op0;
7799 if (arg0)
7801 if (CONVERT_EXPR_CODE_P (code)
7802 || code == FLOAT_EXPR || code == ABS_EXPR || code == NEGATE_EXPR)
7804 /* Don't use STRIP_NOPS, because signedness of argument type
7805 matters. */
7806 STRIP_SIGN_NOPS (arg0);
7808 else
7810 /* Strip any conversions that don't change the mode. This
7811 is safe for every expression, except for a comparison
7812 expression because its signedness is derived from its
7813 operands.
7815 Note that this is done as an internal manipulation within
7816 the constant folder, in order to find the simplest
7817 representation of the arguments so that their form can be
7818 studied. In any cases, the appropriate type conversions
7819 should be put back in the tree that will get out of the
7820 constant folder. */
7821 STRIP_NOPS (arg0);
7824 if (CONSTANT_CLASS_P (arg0))
7826 tree tem = const_unop (code, type, arg0);
7827 if (tem)
7829 if (TREE_TYPE (tem) != type)
7830 tem = fold_convert_loc (loc, type, tem);
7831 return tem;
7836 tem = generic_simplify (loc, code, type, op0);
7837 if (tem)
7838 return tem;
7840 if (TREE_CODE_CLASS (code) == tcc_unary)
7842 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7843 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7844 fold_build1_loc (loc, code, type,
7845 fold_convert_loc (loc, TREE_TYPE (op0),
7846 TREE_OPERAND (arg0, 1))));
7847 else if (TREE_CODE (arg0) == COND_EXPR)
7849 tree arg01 = TREE_OPERAND (arg0, 1);
7850 tree arg02 = TREE_OPERAND (arg0, 2);
7851 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7852 arg01 = fold_build1_loc (loc, code, type,
7853 fold_convert_loc (loc,
7854 TREE_TYPE (op0), arg01));
7855 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7856 arg02 = fold_build1_loc (loc, code, type,
7857 fold_convert_loc (loc,
7858 TREE_TYPE (op0), arg02));
7859 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
7860 arg01, arg02);
7862 /* If this was a conversion, and all we did was to move into
7863 inside the COND_EXPR, bring it back out. But leave it if
7864 it is a conversion from integer to integer and the
7865 result precision is no wider than a word since such a
7866 conversion is cheap and may be optimized away by combine,
7867 while it couldn't if it were outside the COND_EXPR. Then return
7868 so we don't get into an infinite recursion loop taking the
7869 conversion out and then back in. */
7871 if ((CONVERT_EXPR_CODE_P (code)
7872 || code == NON_LVALUE_EXPR)
7873 && TREE_CODE (tem) == COND_EXPR
7874 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7875 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7876 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7877 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7878 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7879 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7880 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7881 && (INTEGRAL_TYPE_P
7882 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7883 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7884 || flag_syntax_only))
7885 tem = build1_loc (loc, code, type,
7886 build3 (COND_EXPR,
7887 TREE_TYPE (TREE_OPERAND
7888 (TREE_OPERAND (tem, 1), 0)),
7889 TREE_OPERAND (tem, 0),
7890 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7891 TREE_OPERAND (TREE_OPERAND (tem, 2),
7892 0)));
7893 return tem;
7897 switch (code)
7899 case NON_LVALUE_EXPR:
7900 if (!maybe_lvalue_p (op0))
7901 return fold_convert_loc (loc, type, op0);
7902 return NULL_TREE;
7904 CASE_CONVERT:
7905 case FLOAT_EXPR:
7906 case FIX_TRUNC_EXPR:
7907 if (COMPARISON_CLASS_P (op0))
7909 /* If we have (type) (a CMP b) and type is an integral type, return
7910 new expression involving the new type. Canonicalize
7911 (type) (a CMP b) to (a CMP b) ? (type) true : (type) false for
7912 non-integral type.
7913 Do not fold the result as that would not simplify further, also
7914 folding again results in recursions. */
7915 if (TREE_CODE (type) == BOOLEAN_TYPE)
7916 return build2_loc (loc, TREE_CODE (op0), type,
7917 TREE_OPERAND (op0, 0),
7918 TREE_OPERAND (op0, 1));
7919 else if (!INTEGRAL_TYPE_P (type) && !VOID_TYPE_P (type)
7920 && TREE_CODE (type) != VECTOR_TYPE)
7921 return build3_loc (loc, COND_EXPR, type, op0,
7922 constant_boolean_node (true, type),
7923 constant_boolean_node (false, type));
7926 /* Handle (T *)&A.B.C for A being of type T and B and C
7927 living at offset zero. This occurs frequently in
7928 C++ upcasting and then accessing the base. */
7929 if (TREE_CODE (op0) == ADDR_EXPR
7930 && POINTER_TYPE_P (type)
7931 && handled_component_p (TREE_OPERAND (op0, 0)))
7933 poly_int64 bitsize, bitpos;
7934 tree offset;
7935 machine_mode mode;
7936 int unsignedp, reversep, volatilep;
7937 tree base
7938 = get_inner_reference (TREE_OPERAND (op0, 0), &bitsize, &bitpos,
7939 &offset, &mode, &unsignedp, &reversep,
7940 &volatilep);
7941 /* If the reference was to a (constant) zero offset, we can use
7942 the address of the base if it has the same base type
7943 as the result type and the pointer type is unqualified. */
7944 if (!offset
7945 && known_eq (bitpos, 0)
7946 && (TYPE_MAIN_VARIANT (TREE_TYPE (type))
7947 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7948 && TYPE_QUALS (type) == TYPE_UNQUALIFIED)
7949 return fold_convert_loc (loc, type,
7950 build_fold_addr_expr_loc (loc, base));
7953 if (TREE_CODE (op0) == MODIFY_EXPR
7954 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7955 /* Detect assigning a bitfield. */
7956 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7957 && DECL_BIT_FIELD
7958 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7960 /* Don't leave an assignment inside a conversion
7961 unless assigning a bitfield. */
7962 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
7963 /* First do the assignment, then return converted constant. */
7964 tem = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7965 TREE_NO_WARNING (tem) = 1;
7966 TREE_USED (tem) = 1;
7967 return tem;
7970 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7971 constants (if x has signed type, the sign bit cannot be set
7972 in c). This folds extension into the BIT_AND_EXPR.
7973 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7974 very likely don't have maximal range for their precision and this
7975 transformation effectively doesn't preserve non-maximal ranges. */
7976 if (TREE_CODE (type) == INTEGER_TYPE
7977 && TREE_CODE (op0) == BIT_AND_EXPR
7978 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7980 tree and_expr = op0;
7981 tree and0 = TREE_OPERAND (and_expr, 0);
7982 tree and1 = TREE_OPERAND (and_expr, 1);
7983 int change = 0;
7985 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
7986 || (TYPE_PRECISION (type)
7987 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
7988 change = 1;
7989 else if (TYPE_PRECISION (TREE_TYPE (and1))
7990 <= HOST_BITS_PER_WIDE_INT
7991 && tree_fits_uhwi_p (and1))
7993 unsigned HOST_WIDE_INT cst;
7995 cst = tree_to_uhwi (and1);
7996 cst &= HOST_WIDE_INT_M1U
7997 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7998 change = (cst == 0);
7999 if (change
8000 && !flag_syntax_only
8001 && (load_extend_op (TYPE_MODE (TREE_TYPE (and0)))
8002 == ZERO_EXTEND))
8004 tree uns = unsigned_type_for (TREE_TYPE (and0));
8005 and0 = fold_convert_loc (loc, uns, and0);
8006 and1 = fold_convert_loc (loc, uns, and1);
8009 if (change)
8011 tem = force_fit_type (type, wi::to_widest (and1), 0,
8012 TREE_OVERFLOW (and1));
8013 return fold_build2_loc (loc, BIT_AND_EXPR, type,
8014 fold_convert_loc (loc, type, and0), tem);
8018 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type, when the new
8019 cast (T1)X will fold away. We assume that this happens when X itself
8020 is a cast. */
8021 if (POINTER_TYPE_P (type)
8022 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8023 && CONVERT_EXPR_P (TREE_OPERAND (arg0, 0)))
8025 tree arg00 = TREE_OPERAND (arg0, 0);
8026 tree arg01 = TREE_OPERAND (arg0, 1);
8028 return fold_build_pointer_plus_loc
8029 (loc, fold_convert_loc (loc, type, arg00), arg01);
8032 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8033 of the same precision, and X is an integer type not narrower than
8034 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8035 if (INTEGRAL_TYPE_P (type)
8036 && TREE_CODE (op0) == BIT_NOT_EXPR
8037 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8038 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8039 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8041 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8042 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8043 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8044 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
8045 fold_convert_loc (loc, type, tem));
8048 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8049 type of X and Y (integer types only). */
8050 if (INTEGRAL_TYPE_P (type)
8051 && TREE_CODE (op0) == MULT_EXPR
8052 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8053 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8055 /* Be careful not to introduce new overflows. */
8056 tree mult_type;
8057 if (TYPE_OVERFLOW_WRAPS (type))
8058 mult_type = type;
8059 else
8060 mult_type = unsigned_type_for (type);
8062 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8064 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8065 fold_convert_loc (loc, mult_type,
8066 TREE_OPERAND (op0, 0)),
8067 fold_convert_loc (loc, mult_type,
8068 TREE_OPERAND (op0, 1)));
8069 return fold_convert_loc (loc, type, tem);
8073 return NULL_TREE;
8075 case VIEW_CONVERT_EXPR:
8076 if (TREE_CODE (op0) == MEM_REF)
8078 if (TYPE_ALIGN (TREE_TYPE (op0)) != TYPE_ALIGN (type))
8079 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op0)));
8080 tem = fold_build2_loc (loc, MEM_REF, type,
8081 TREE_OPERAND (op0, 0), TREE_OPERAND (op0, 1));
8082 REF_REVERSE_STORAGE_ORDER (tem) = REF_REVERSE_STORAGE_ORDER (op0);
8083 return tem;
8086 return NULL_TREE;
8088 case NEGATE_EXPR:
8089 tem = fold_negate_expr (loc, arg0);
8090 if (tem)
8091 return fold_convert_loc (loc, type, tem);
8092 return NULL_TREE;
8094 case ABS_EXPR:
8095 /* Convert fabs((double)float) into (double)fabsf(float). */
8096 if (TREE_CODE (arg0) == NOP_EXPR
8097 && TREE_CODE (type) == REAL_TYPE)
8099 tree targ0 = strip_float_extensions (arg0);
8100 if (targ0 != arg0)
8101 return fold_convert_loc (loc, type,
8102 fold_build1_loc (loc, ABS_EXPR,
8103 TREE_TYPE (targ0),
8104 targ0));
8106 return NULL_TREE;
8108 case BIT_NOT_EXPR:
8109 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8110 if (TREE_CODE (arg0) == BIT_XOR_EXPR
8111 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8112 fold_convert_loc (loc, type,
8113 TREE_OPERAND (arg0, 0)))))
8114 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8115 fold_convert_loc (loc, type,
8116 TREE_OPERAND (arg0, 1)));
8117 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8118 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8119 fold_convert_loc (loc, type,
8120 TREE_OPERAND (arg0, 1)))))
8121 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8122 fold_convert_loc (loc, type,
8123 TREE_OPERAND (arg0, 0)), tem);
8125 return NULL_TREE;
8127 case TRUTH_NOT_EXPR:
8128 /* Note that the operand of this must be an int
8129 and its values must be 0 or 1.
8130 ("true" is a fixed value perhaps depending on the language,
8131 but we don't handle values other than 1 correctly yet.) */
8132 tem = fold_truth_not_expr (loc, arg0);
8133 if (!tem)
8134 return NULL_TREE;
8135 return fold_convert_loc (loc, type, tem);
8137 case INDIRECT_REF:
8138 /* Fold *&X to X if X is an lvalue. */
8139 if (TREE_CODE (op0) == ADDR_EXPR)
8141 tree op00 = TREE_OPERAND (op0, 0);
8142 if ((VAR_P (op00)
8143 || TREE_CODE (op00) == PARM_DECL
8144 || TREE_CODE (op00) == RESULT_DECL)
8145 && !TREE_READONLY (op00))
8146 return op00;
8148 return NULL_TREE;
8150 default:
8151 return NULL_TREE;
8152 } /* switch (code) */
8156 /* If the operation was a conversion do _not_ mark a resulting constant
8157 with TREE_OVERFLOW if the original constant was not. These conversions
8158 have implementation defined behavior and retaining the TREE_OVERFLOW
8159 flag here would confuse later passes such as VRP. */
8160 tree
8161 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8162 tree type, tree op0)
8164 tree res = fold_unary_loc (loc, code, type, op0);
8165 if (res
8166 && TREE_CODE (res) == INTEGER_CST
8167 && TREE_CODE (op0) == INTEGER_CST
8168 && CONVERT_EXPR_CODE_P (code))
8169 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8171 return res;
8174 /* Fold a binary bitwise/truth expression of code CODE and type TYPE with
8175 operands OP0 and OP1. LOC is the location of the resulting expression.
8176 ARG0 and ARG1 are the NOP_STRIPed results of OP0 and OP1.
8177 Return the folded expression if folding is successful. Otherwise,
8178 return NULL_TREE. */
8179 static tree
8180 fold_truth_andor (location_t loc, enum tree_code code, tree type,
8181 tree arg0, tree arg1, tree op0, tree op1)
8183 tree tem;
8185 /* We only do these simplifications if we are optimizing. */
8186 if (!optimize)
8187 return NULL_TREE;
8189 /* Check for things like (A || B) && (A || C). We can convert this
8190 to A || (B && C). Note that either operator can be any of the four
8191 truth and/or operations and the transformation will still be
8192 valid. Also note that we only care about order for the
8193 ANDIF and ORIF operators. If B contains side effects, this
8194 might change the truth-value of A. */
8195 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8196 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8197 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8198 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8199 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8200 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8202 tree a00 = TREE_OPERAND (arg0, 0);
8203 tree a01 = TREE_OPERAND (arg0, 1);
8204 tree a10 = TREE_OPERAND (arg1, 0);
8205 tree a11 = TREE_OPERAND (arg1, 1);
8206 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8207 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8208 && (code == TRUTH_AND_EXPR
8209 || code == TRUTH_OR_EXPR));
8211 if (operand_equal_p (a00, a10, 0))
8212 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8213 fold_build2_loc (loc, code, type, a01, a11));
8214 else if (commutative && operand_equal_p (a00, a11, 0))
8215 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
8216 fold_build2_loc (loc, code, type, a01, a10));
8217 else if (commutative && operand_equal_p (a01, a10, 0))
8218 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
8219 fold_build2_loc (loc, code, type, a00, a11));
8221 /* This case if tricky because we must either have commutative
8222 operators or else A10 must not have side-effects. */
8224 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8225 && operand_equal_p (a01, a11, 0))
8226 return fold_build2_loc (loc, TREE_CODE (arg0), type,
8227 fold_build2_loc (loc, code, type, a00, a10),
8228 a01);
8231 /* See if we can build a range comparison. */
8232 if ((tem = fold_range_test (loc, code, type, op0, op1)) != 0)
8233 return tem;
8235 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg0) == TRUTH_ORIF_EXPR)
8236 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg0) == TRUTH_ANDIF_EXPR))
8238 tem = merge_truthop_with_opposite_arm (loc, arg0, arg1, true);
8239 if (tem)
8240 return fold_build2_loc (loc, code, type, tem, arg1);
8243 if ((code == TRUTH_ANDIF_EXPR && TREE_CODE (arg1) == TRUTH_ORIF_EXPR)
8244 || (code == TRUTH_ORIF_EXPR && TREE_CODE (arg1) == TRUTH_ANDIF_EXPR))
8246 tem = merge_truthop_with_opposite_arm (loc, arg1, arg0, false);
8247 if (tem)
8248 return fold_build2_loc (loc, code, type, arg0, tem);
8251 /* Check for the possibility of merging component references. If our
8252 lhs is another similar operation, try to merge its rhs with our
8253 rhs. Then try to merge our lhs and rhs. */
8254 if (TREE_CODE (arg0) == code
8255 && (tem = fold_truth_andor_1 (loc, code, type,
8256 TREE_OPERAND (arg0, 1), arg1)) != 0)
8257 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
8259 if ((tem = fold_truth_andor_1 (loc, code, type, arg0, arg1)) != 0)
8260 return tem;
8262 bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
8263 if (PARAM_VALUE (PARAM_LOGICAL_OP_NON_SHORT_CIRCUIT) != -1)
8264 logical_op_non_short_circuit
8265 = PARAM_VALUE (PARAM_LOGICAL_OP_NON_SHORT_CIRCUIT);
8266 if (logical_op_non_short_circuit
8267 && !flag_sanitize_coverage
8268 && (code == TRUTH_AND_EXPR
8269 || code == TRUTH_ANDIF_EXPR
8270 || code == TRUTH_OR_EXPR
8271 || code == TRUTH_ORIF_EXPR))
8273 enum tree_code ncode, icode;
8275 ncode = (code == TRUTH_ANDIF_EXPR || code == TRUTH_AND_EXPR)
8276 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR;
8277 icode = ncode == TRUTH_AND_EXPR ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
8279 /* Transform ((A AND-IF B) AND[-IF] C) into (A AND-IF (B AND C)),
8280 or ((A OR-IF B) OR[-IF] C) into (A OR-IF (B OR C))
8281 We don't want to pack more than two leafs to a non-IF AND/OR
8282 expression.
8283 If tree-code of left-hand operand isn't an AND/OR-IF code and not
8284 equal to IF-CODE, then we don't want to add right-hand operand.
8285 If the inner right-hand side of left-hand operand has
8286 side-effects, or isn't simple, then we can't add to it,
8287 as otherwise we might destroy if-sequence. */
8288 if (TREE_CODE (arg0) == icode
8289 && simple_operand_p_2 (arg1)
8290 /* Needed for sequence points to handle trappings, and
8291 side-effects. */
8292 && simple_operand_p_2 (TREE_OPERAND (arg0, 1)))
8294 tem = fold_build2_loc (loc, ncode, type, TREE_OPERAND (arg0, 1),
8295 arg1);
8296 return fold_build2_loc (loc, icode, type, TREE_OPERAND (arg0, 0),
8297 tem);
8299 /* Same as above but for (A AND[-IF] (B AND-IF C)) -> ((A AND B) AND-IF C),
8300 or (A OR[-IF] (B OR-IF C) -> ((A OR B) OR-IF C). */
8301 else if (TREE_CODE (arg1) == icode
8302 && simple_operand_p_2 (arg0)
8303 /* Needed for sequence points to handle trappings, and
8304 side-effects. */
8305 && simple_operand_p_2 (TREE_OPERAND (arg1, 0)))
8307 tem = fold_build2_loc (loc, ncode, type,
8308 arg0, TREE_OPERAND (arg1, 0));
8309 return fold_build2_loc (loc, icode, type, tem,
8310 TREE_OPERAND (arg1, 1));
8312 /* Transform (A AND-IF B) into (A AND B), or (A OR-IF B)
8313 into (A OR B).
8314 For sequence point consistancy, we need to check for trapping,
8315 and side-effects. */
8316 else if (code == icode && simple_operand_p_2 (arg0)
8317 && simple_operand_p_2 (arg1))
8318 return fold_build2_loc (loc, ncode, type, arg0, arg1);
8321 return NULL_TREE;
8324 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8325 by changing CODE to reduce the magnitude of constants involved in
8326 ARG0 of the comparison.
8327 Returns a canonicalized comparison tree if a simplification was
8328 possible, otherwise returns NULL_TREE.
8329 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8330 valid if signed overflow is undefined. */
8332 static tree
8333 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
8334 tree arg0, tree arg1,
8335 bool *strict_overflow_p)
8337 enum tree_code code0 = TREE_CODE (arg0);
8338 tree t, cst0 = NULL_TREE;
8339 int sgn0;
8341 /* Match A +- CST code arg1. We can change this only if overflow
8342 is undefined. */
8343 if (!((ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8344 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0)))
8345 /* In principle pointers also have undefined overflow behavior,
8346 but that causes problems elsewhere. */
8347 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8348 && (code0 == MINUS_EXPR
8349 || code0 == PLUS_EXPR)
8350 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST))
8351 return NULL_TREE;
8353 /* Identify the constant in arg0 and its sign. */
8354 cst0 = TREE_OPERAND (arg0, 1);
8355 sgn0 = tree_int_cst_sgn (cst0);
8357 /* Overflowed constants and zero will cause problems. */
8358 if (integer_zerop (cst0)
8359 || TREE_OVERFLOW (cst0))
8360 return NULL_TREE;
8362 /* See if we can reduce the magnitude of the constant in
8363 arg0 by changing the comparison code. */
8364 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8365 if (code == LT_EXPR
8366 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8367 code = LE_EXPR;
8368 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8369 else if (code == GT_EXPR
8370 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8371 code = GE_EXPR;
8372 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8373 else if (code == LE_EXPR
8374 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8375 code = LT_EXPR;
8376 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8377 else if (code == GE_EXPR
8378 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8379 code = GT_EXPR;
8380 else
8381 return NULL_TREE;
8382 *strict_overflow_p = true;
8384 /* Now build the constant reduced in magnitude. But not if that
8385 would produce one outside of its types range. */
8386 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8387 && ((sgn0 == 1
8388 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8389 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8390 || (sgn0 == -1
8391 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8392 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8393 return NULL_TREE;
8395 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8396 cst0, build_int_cst (TREE_TYPE (cst0), 1));
8397 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8398 t = fold_convert (TREE_TYPE (arg1), t);
8400 return fold_build2_loc (loc, code, type, t, arg1);
8403 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8404 overflow further. Try to decrease the magnitude of constants involved
8405 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8406 and put sole constants at the second argument position.
8407 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8409 static tree
8410 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
8411 tree arg0, tree arg1)
8413 tree t;
8414 bool strict_overflow_p;
8415 const char * const warnmsg = G_("assuming signed overflow does not occur "
8416 "when reducing constant in comparison");
8418 /* Try canonicalization by simplifying arg0. */
8419 strict_overflow_p = false;
8420 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
8421 &strict_overflow_p);
8422 if (t)
8424 if (strict_overflow_p)
8425 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8426 return t;
8429 /* Try canonicalization by simplifying arg1 using the swapped
8430 comparison. */
8431 code = swap_tree_comparison (code);
8432 strict_overflow_p = false;
8433 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
8434 &strict_overflow_p);
8435 if (t && strict_overflow_p)
8436 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8437 return t;
8440 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8441 space. This is used to avoid issuing overflow warnings for
8442 expressions like &p->x which cannot wrap. */
8444 static bool
8445 pointer_may_wrap_p (tree base, tree offset, poly_int64 bitpos)
8447 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8448 return true;
8450 if (maybe_lt (bitpos, 0))
8451 return true;
8453 poly_wide_int wi_offset;
8454 int precision = TYPE_PRECISION (TREE_TYPE (base));
8455 if (offset == NULL_TREE)
8456 wi_offset = wi::zero (precision);
8457 else if (!poly_int_tree_p (offset) || TREE_OVERFLOW (offset))
8458 return true;
8459 else
8460 wi_offset = wi::to_poly_wide (offset);
8462 wi::overflow_type overflow;
8463 poly_wide_int units = wi::shwi (bits_to_bytes_round_down (bitpos),
8464 precision);
8465 poly_wide_int total = wi::add (wi_offset, units, UNSIGNED, &overflow);
8466 if (overflow)
8467 return true;
8469 poly_uint64 total_hwi, size;
8470 if (!total.to_uhwi (&total_hwi)
8471 || !poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (base))),
8472 &size)
8473 || known_eq (size, 0U))
8474 return true;
8476 if (known_le (total_hwi, size))
8477 return false;
8479 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8480 array. */
8481 if (TREE_CODE (base) == ADDR_EXPR
8482 && poly_int_tree_p (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (base, 0))),
8483 &size)
8484 && maybe_ne (size, 0U)
8485 && known_le (total_hwi, size))
8486 return false;
8488 return true;
8491 /* Return a positive integer when the symbol DECL is known to have
8492 a nonzero address, zero when it's known not to (e.g., it's a weak
8493 symbol), and a negative integer when the symbol is not yet in the
8494 symbol table and so whether or not its address is zero is unknown.
8495 For function local objects always return positive integer. */
8496 static int
8497 maybe_nonzero_address (tree decl)
8499 if (DECL_P (decl) && decl_in_symtab_p (decl))
8500 if (struct symtab_node *symbol = symtab_node::get_create (decl))
8501 return symbol->nonzero_address ();
8503 /* Function local objects are never NULL. */
8504 if (DECL_P (decl)
8505 && (DECL_CONTEXT (decl)
8506 && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL
8507 && auto_var_in_fn_p (decl, DECL_CONTEXT (decl))))
8508 return 1;
8510 return -1;
8513 /* Subroutine of fold_binary. This routine performs all of the
8514 transformations that are common to the equality/inequality
8515 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8516 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8517 fold_binary should call fold_binary. Fold a comparison with
8518 tree code CODE and type TYPE with operands OP0 and OP1. Return
8519 the folded comparison or NULL_TREE. */
8521 static tree
8522 fold_comparison (location_t loc, enum tree_code code, tree type,
8523 tree op0, tree op1)
8525 const bool equality_code = (code == EQ_EXPR || code == NE_EXPR);
8526 tree arg0, arg1, tem;
8528 arg0 = op0;
8529 arg1 = op1;
8531 STRIP_SIGN_NOPS (arg0);
8532 STRIP_SIGN_NOPS (arg1);
8534 /* For comparisons of pointers we can decompose it to a compile time
8535 comparison of the base objects and the offsets into the object.
8536 This requires at least one operand being an ADDR_EXPR or a
8537 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8538 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8539 && (TREE_CODE (arg0) == ADDR_EXPR
8540 || TREE_CODE (arg1) == ADDR_EXPR
8541 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8542 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8544 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8545 poly_int64 bitsize, bitpos0 = 0, bitpos1 = 0;
8546 machine_mode mode;
8547 int volatilep, reversep, unsignedp;
8548 bool indirect_base0 = false, indirect_base1 = false;
8550 /* Get base and offset for the access. Strip ADDR_EXPR for
8551 get_inner_reference, but put it back by stripping INDIRECT_REF
8552 off the base object if possible. indirect_baseN will be true
8553 if baseN is not an address but refers to the object itself. */
8554 base0 = arg0;
8555 if (TREE_CODE (arg0) == ADDR_EXPR)
8557 base0
8558 = get_inner_reference (TREE_OPERAND (arg0, 0),
8559 &bitsize, &bitpos0, &offset0, &mode,
8560 &unsignedp, &reversep, &volatilep);
8561 if (TREE_CODE (base0) == INDIRECT_REF)
8562 base0 = TREE_OPERAND (base0, 0);
8563 else
8564 indirect_base0 = true;
8566 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8568 base0 = TREE_OPERAND (arg0, 0);
8569 STRIP_SIGN_NOPS (base0);
8570 if (TREE_CODE (base0) == ADDR_EXPR)
8572 base0
8573 = get_inner_reference (TREE_OPERAND (base0, 0),
8574 &bitsize, &bitpos0, &offset0, &mode,
8575 &unsignedp, &reversep, &volatilep);
8576 if (TREE_CODE (base0) == INDIRECT_REF)
8577 base0 = TREE_OPERAND (base0, 0);
8578 else
8579 indirect_base0 = true;
8581 if (offset0 == NULL_TREE || integer_zerop (offset0))
8582 offset0 = TREE_OPERAND (arg0, 1);
8583 else
8584 offset0 = size_binop (PLUS_EXPR, offset0,
8585 TREE_OPERAND (arg0, 1));
8586 if (poly_int_tree_p (offset0))
8588 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset0),
8589 TYPE_PRECISION (sizetype));
8590 tem <<= LOG2_BITS_PER_UNIT;
8591 tem += bitpos0;
8592 if (tem.to_shwi (&bitpos0))
8593 offset0 = NULL_TREE;
8597 base1 = arg1;
8598 if (TREE_CODE (arg1) == ADDR_EXPR)
8600 base1
8601 = get_inner_reference (TREE_OPERAND (arg1, 0),
8602 &bitsize, &bitpos1, &offset1, &mode,
8603 &unsignedp, &reversep, &volatilep);
8604 if (TREE_CODE (base1) == INDIRECT_REF)
8605 base1 = TREE_OPERAND (base1, 0);
8606 else
8607 indirect_base1 = true;
8609 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8611 base1 = TREE_OPERAND (arg1, 0);
8612 STRIP_SIGN_NOPS (base1);
8613 if (TREE_CODE (base1) == ADDR_EXPR)
8615 base1
8616 = get_inner_reference (TREE_OPERAND (base1, 0),
8617 &bitsize, &bitpos1, &offset1, &mode,
8618 &unsignedp, &reversep, &volatilep);
8619 if (TREE_CODE (base1) == INDIRECT_REF)
8620 base1 = TREE_OPERAND (base1, 0);
8621 else
8622 indirect_base1 = true;
8624 if (offset1 == NULL_TREE || integer_zerop (offset1))
8625 offset1 = TREE_OPERAND (arg1, 1);
8626 else
8627 offset1 = size_binop (PLUS_EXPR, offset1,
8628 TREE_OPERAND (arg1, 1));
8629 if (poly_int_tree_p (offset1))
8631 poly_offset_int tem = wi::sext (wi::to_poly_offset (offset1),
8632 TYPE_PRECISION (sizetype));
8633 tem <<= LOG2_BITS_PER_UNIT;
8634 tem += bitpos1;
8635 if (tem.to_shwi (&bitpos1))
8636 offset1 = NULL_TREE;
8640 /* If we have equivalent bases we might be able to simplify. */
8641 if (indirect_base0 == indirect_base1
8642 && operand_equal_p (base0, base1,
8643 indirect_base0 ? OEP_ADDRESS_OF : 0))
8645 /* We can fold this expression to a constant if the non-constant
8646 offset parts are equal. */
8647 if ((offset0 == offset1
8648 || (offset0 && offset1
8649 && operand_equal_p (offset0, offset1, 0)))
8650 && (equality_code
8651 || (indirect_base0
8652 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8653 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8655 if (!equality_code
8656 && maybe_ne (bitpos0, bitpos1)
8657 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8658 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8659 fold_overflow_warning (("assuming pointer wraparound does not "
8660 "occur when comparing P +- C1 with "
8661 "P +- C2"),
8662 WARN_STRICT_OVERFLOW_CONDITIONAL);
8664 switch (code)
8666 case EQ_EXPR:
8667 if (known_eq (bitpos0, bitpos1))
8668 return constant_boolean_node (true, type);
8669 if (known_ne (bitpos0, bitpos1))
8670 return constant_boolean_node (false, type);
8671 break;
8672 case NE_EXPR:
8673 if (known_ne (bitpos0, bitpos1))
8674 return constant_boolean_node (true, type);
8675 if (known_eq (bitpos0, bitpos1))
8676 return constant_boolean_node (false, type);
8677 break;
8678 case LT_EXPR:
8679 if (known_lt (bitpos0, bitpos1))
8680 return constant_boolean_node (true, type);
8681 if (known_ge (bitpos0, bitpos1))
8682 return constant_boolean_node (false, type);
8683 break;
8684 case LE_EXPR:
8685 if (known_le (bitpos0, bitpos1))
8686 return constant_boolean_node (true, type);
8687 if (known_gt (bitpos0, bitpos1))
8688 return constant_boolean_node (false, type);
8689 break;
8690 case GE_EXPR:
8691 if (known_ge (bitpos0, bitpos1))
8692 return constant_boolean_node (true, type);
8693 if (known_lt (bitpos0, bitpos1))
8694 return constant_boolean_node (false, type);
8695 break;
8696 case GT_EXPR:
8697 if (known_gt (bitpos0, bitpos1))
8698 return constant_boolean_node (true, type);
8699 if (known_le (bitpos0, bitpos1))
8700 return constant_boolean_node (false, type);
8701 break;
8702 default:;
8705 /* We can simplify the comparison to a comparison of the variable
8706 offset parts if the constant offset parts are equal.
8707 Be careful to use signed sizetype here because otherwise we
8708 mess with array offsets in the wrong way. This is possible
8709 because pointer arithmetic is restricted to retain within an
8710 object and overflow on pointer differences is undefined as of
8711 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8712 else if (known_eq (bitpos0, bitpos1)
8713 && (equality_code
8714 || (indirect_base0
8715 && (DECL_P (base0) || CONSTANT_CLASS_P (base0)))
8716 || TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
8718 /* By converting to signed sizetype we cover middle-end pointer
8719 arithmetic which operates on unsigned pointer types of size
8720 type size and ARRAY_REF offsets which are properly sign or
8721 zero extended from their type in case it is narrower than
8722 sizetype. */
8723 if (offset0 == NULL_TREE)
8724 offset0 = build_int_cst (ssizetype, 0);
8725 else
8726 offset0 = fold_convert_loc (loc, ssizetype, offset0);
8727 if (offset1 == NULL_TREE)
8728 offset1 = build_int_cst (ssizetype, 0);
8729 else
8730 offset1 = fold_convert_loc (loc, ssizetype, offset1);
8732 if (!equality_code
8733 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8734 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8735 fold_overflow_warning (("assuming pointer wraparound does not "
8736 "occur when comparing P +- C1 with "
8737 "P +- C2"),
8738 WARN_STRICT_OVERFLOW_COMPARISON);
8740 return fold_build2_loc (loc, code, type, offset0, offset1);
8743 /* For equal offsets we can simplify to a comparison of the
8744 base addresses. */
8745 else if (known_eq (bitpos0, bitpos1)
8746 && (indirect_base0
8747 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8748 && (indirect_base1
8749 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8750 && ((offset0 == offset1)
8751 || (offset0 && offset1
8752 && operand_equal_p (offset0, offset1, 0))))
8754 if (indirect_base0)
8755 base0 = build_fold_addr_expr_loc (loc, base0);
8756 if (indirect_base1)
8757 base1 = build_fold_addr_expr_loc (loc, base1);
8758 return fold_build2_loc (loc, code, type, base0, base1);
8760 /* Comparison between an ordinary (non-weak) symbol and a null
8761 pointer can be eliminated since such symbols must have a non
8762 null address. In C, relational expressions between pointers
8763 to objects and null pointers are undefined. The results
8764 below follow the C++ rules with the additional property that
8765 every object pointer compares greater than a null pointer.
8767 else if (((DECL_P (base0)
8768 && maybe_nonzero_address (base0) > 0
8769 /* Avoid folding references to struct members at offset 0 to
8770 prevent tests like '&ptr->firstmember == 0' from getting
8771 eliminated. When ptr is null, although the -> expression
8772 is strictly speaking invalid, GCC retains it as a matter
8773 of QoI. See PR c/44555. */
8774 && (offset0 == NULL_TREE && known_ne (bitpos0, 0)))
8775 || CONSTANT_CLASS_P (base0))
8776 && indirect_base0
8777 /* The caller guarantees that when one of the arguments is
8778 constant (i.e., null in this case) it is second. */
8779 && integer_zerop (arg1))
8781 switch (code)
8783 case EQ_EXPR:
8784 case LE_EXPR:
8785 case LT_EXPR:
8786 return constant_boolean_node (false, type);
8787 case GE_EXPR:
8788 case GT_EXPR:
8789 case NE_EXPR:
8790 return constant_boolean_node (true, type);
8791 default:
8792 gcc_unreachable ();
8797 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8798 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8799 the resulting offset is smaller in absolute value than the
8800 original one and has the same sign. */
8801 if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8802 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8803 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8804 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8805 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8806 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8807 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8808 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8810 tree const1 = TREE_OPERAND (arg0, 1);
8811 tree const2 = TREE_OPERAND (arg1, 1);
8812 tree variable1 = TREE_OPERAND (arg0, 0);
8813 tree variable2 = TREE_OPERAND (arg1, 0);
8814 tree cst;
8815 const char * const warnmsg = G_("assuming signed overflow does not "
8816 "occur when combining constants around "
8817 "a comparison");
8819 /* Put the constant on the side where it doesn't overflow and is
8820 of lower absolute value and of same sign than before. */
8821 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8822 ? MINUS_EXPR : PLUS_EXPR,
8823 const2, const1);
8824 if (!TREE_OVERFLOW (cst)
8825 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2)
8826 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const2))
8828 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8829 return fold_build2_loc (loc, code, type,
8830 variable1,
8831 fold_build2_loc (loc, TREE_CODE (arg1),
8832 TREE_TYPE (arg1),
8833 variable2, cst));
8836 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8837 ? MINUS_EXPR : PLUS_EXPR,
8838 const1, const2);
8839 if (!TREE_OVERFLOW (cst)
8840 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1)
8841 && tree_int_cst_sgn (cst) == tree_int_cst_sgn (const1))
8843 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8844 return fold_build2_loc (loc, code, type,
8845 fold_build2_loc (loc, TREE_CODE (arg0),
8846 TREE_TYPE (arg0),
8847 variable1, cst),
8848 variable2);
8852 tem = maybe_canonicalize_comparison (loc, code, type, arg0, arg1);
8853 if (tem)
8854 return tem;
8856 /* If we are comparing an expression that just has comparisons
8857 of two integer values, arithmetic expressions of those comparisons,
8858 and constants, we can simplify it. There are only three cases
8859 to check: the two values can either be equal, the first can be
8860 greater, or the second can be greater. Fold the expression for
8861 those three values. Since each value must be 0 or 1, we have
8862 eight possibilities, each of which corresponds to the constant 0
8863 or 1 or one of the six possible comparisons.
8865 This handles common cases like (a > b) == 0 but also handles
8866 expressions like ((x > y) - (y > x)) > 0, which supposedly
8867 occur in macroized code. */
8869 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8871 tree cval1 = 0, cval2 = 0;
8873 if (twoval_comparison_p (arg0, &cval1, &cval2)
8874 /* Don't handle degenerate cases here; they should already
8875 have been handled anyway. */
8876 && cval1 != 0 && cval2 != 0
8877 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8878 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8879 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8880 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8881 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8882 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8883 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8885 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8886 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8888 /* We can't just pass T to eval_subst in case cval1 or cval2
8889 was the same as ARG1. */
8891 tree high_result
8892 = fold_build2_loc (loc, code, type,
8893 eval_subst (loc, arg0, cval1, maxval,
8894 cval2, minval),
8895 arg1);
8896 tree equal_result
8897 = fold_build2_loc (loc, code, type,
8898 eval_subst (loc, arg0, cval1, maxval,
8899 cval2, maxval),
8900 arg1);
8901 tree low_result
8902 = fold_build2_loc (loc, code, type,
8903 eval_subst (loc, arg0, cval1, minval,
8904 cval2, maxval),
8905 arg1);
8907 /* All three of these results should be 0 or 1. Confirm they are.
8908 Then use those values to select the proper code to use. */
8910 if (TREE_CODE (high_result) == INTEGER_CST
8911 && TREE_CODE (equal_result) == INTEGER_CST
8912 && TREE_CODE (low_result) == INTEGER_CST)
8914 /* Make a 3-bit mask with the high-order bit being the
8915 value for `>', the next for '=', and the low for '<'. */
8916 switch ((integer_onep (high_result) * 4)
8917 + (integer_onep (equal_result) * 2)
8918 + integer_onep (low_result))
8920 case 0:
8921 /* Always false. */
8922 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
8923 case 1:
8924 code = LT_EXPR;
8925 break;
8926 case 2:
8927 code = EQ_EXPR;
8928 break;
8929 case 3:
8930 code = LE_EXPR;
8931 break;
8932 case 4:
8933 code = GT_EXPR;
8934 break;
8935 case 5:
8936 code = NE_EXPR;
8937 break;
8938 case 6:
8939 code = GE_EXPR;
8940 break;
8941 case 7:
8942 /* Always true. */
8943 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
8946 return fold_build2_loc (loc, code, type, cval1, cval2);
8951 return NULL_TREE;
8955 /* Subroutine of fold_binary. Optimize complex multiplications of the
8956 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
8957 argument EXPR represents the expression "z" of type TYPE. */
8959 static tree
8960 fold_mult_zconjz (location_t loc, tree type, tree expr)
8962 tree itype = TREE_TYPE (type);
8963 tree rpart, ipart, tem;
8965 if (TREE_CODE (expr) == COMPLEX_EXPR)
8967 rpart = TREE_OPERAND (expr, 0);
8968 ipart = TREE_OPERAND (expr, 1);
8970 else if (TREE_CODE (expr) == COMPLEX_CST)
8972 rpart = TREE_REALPART (expr);
8973 ipart = TREE_IMAGPART (expr);
8975 else
8977 expr = save_expr (expr);
8978 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
8979 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
8982 rpart = save_expr (rpart);
8983 ipart = save_expr (ipart);
8984 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
8985 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
8986 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
8987 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
8988 build_zero_cst (itype));
8992 /* Helper function for fold_vec_perm. Store elements of VECTOR_CST or
8993 CONSTRUCTOR ARG into array ELTS, which has NELTS elements, and return
8994 true if successful. */
8996 static bool
8997 vec_cst_ctor_to_array (tree arg, unsigned int nelts, tree *elts)
8999 unsigned HOST_WIDE_INT i, nunits;
9001 if (TREE_CODE (arg) == VECTOR_CST
9002 && VECTOR_CST_NELTS (arg).is_constant (&nunits))
9004 for (i = 0; i < nunits; ++i)
9005 elts[i] = VECTOR_CST_ELT (arg, i);
9007 else if (TREE_CODE (arg) == CONSTRUCTOR)
9009 constructor_elt *elt;
9011 FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (arg), i, elt)
9012 if (i >= nelts || TREE_CODE (TREE_TYPE (elt->value)) == VECTOR_TYPE)
9013 return false;
9014 else
9015 elts[i] = elt->value;
9017 else
9018 return false;
9019 for (; i < nelts; i++)
9020 elts[i]
9021 = fold_convert (TREE_TYPE (TREE_TYPE (arg)), integer_zero_node);
9022 return true;
9025 /* Attempt to fold vector permutation of ARG0 and ARG1 vectors using SEL
9026 selector. Return the folded VECTOR_CST or CONSTRUCTOR if successful,
9027 NULL_TREE otherwise. */
9029 tree
9030 fold_vec_perm (tree type, tree arg0, tree arg1, const vec_perm_indices &sel)
9032 unsigned int i;
9033 unsigned HOST_WIDE_INT nelts;
9034 bool need_ctor = false;
9036 if (!sel.length ().is_constant (&nelts))
9037 return NULL_TREE;
9038 gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type), nelts)
9039 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)), nelts)
9040 && known_eq (TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg1)), nelts));
9041 if (TREE_TYPE (TREE_TYPE (arg0)) != TREE_TYPE (type)
9042 || TREE_TYPE (TREE_TYPE (arg1)) != TREE_TYPE (type))
9043 return NULL_TREE;
9045 tree *in_elts = XALLOCAVEC (tree, nelts * 2);
9046 if (!vec_cst_ctor_to_array (arg0, nelts, in_elts)
9047 || !vec_cst_ctor_to_array (arg1, nelts, in_elts + nelts))
9048 return NULL_TREE;
9050 tree_vector_builder out_elts (type, nelts, 1);
9051 for (i = 0; i < nelts; i++)
9053 HOST_WIDE_INT index;
9054 if (!sel[i].is_constant (&index))
9055 return NULL_TREE;
9056 if (!CONSTANT_CLASS_P (in_elts[index]))
9057 need_ctor = true;
9058 out_elts.quick_push (unshare_expr (in_elts[index]));
9061 if (need_ctor)
9063 vec<constructor_elt, va_gc> *v;
9064 vec_alloc (v, nelts);
9065 for (i = 0; i < nelts; i++)
9066 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, out_elts[i]);
9067 return build_constructor (type, v);
9069 else
9070 return out_elts.build ();
9073 /* Try to fold a pointer difference of type TYPE two address expressions of
9074 array references AREF0 and AREF1 using location LOC. Return a
9075 simplified expression for the difference or NULL_TREE. */
9077 static tree
9078 fold_addr_of_array_ref_difference (location_t loc, tree type,
9079 tree aref0, tree aref1,
9080 bool use_pointer_diff)
9082 tree base0 = TREE_OPERAND (aref0, 0);
9083 tree base1 = TREE_OPERAND (aref1, 0);
9084 tree base_offset = build_int_cst (type, 0);
9086 /* If the bases are array references as well, recurse. If the bases
9087 are pointer indirections compute the difference of the pointers.
9088 If the bases are equal, we are set. */
9089 if ((TREE_CODE (base0) == ARRAY_REF
9090 && TREE_CODE (base1) == ARRAY_REF
9091 && (base_offset
9092 = fold_addr_of_array_ref_difference (loc, type, base0, base1,
9093 use_pointer_diff)))
9094 || (INDIRECT_REF_P (base0)
9095 && INDIRECT_REF_P (base1)
9096 && (base_offset
9097 = use_pointer_diff
9098 ? fold_binary_loc (loc, POINTER_DIFF_EXPR, type,
9099 TREE_OPERAND (base0, 0),
9100 TREE_OPERAND (base1, 0))
9101 : fold_binary_loc (loc, MINUS_EXPR, type,
9102 fold_convert (type,
9103 TREE_OPERAND (base0, 0)),
9104 fold_convert (type,
9105 TREE_OPERAND (base1, 0)))))
9106 || operand_equal_p (base0, base1, OEP_ADDRESS_OF))
9108 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
9109 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
9110 tree esz = fold_convert_loc (loc, type, array_ref_element_size (aref0));
9111 tree diff = fold_build2_loc (loc, MINUS_EXPR, type, op0, op1);
9112 return fold_build2_loc (loc, PLUS_EXPR, type,
9113 base_offset,
9114 fold_build2_loc (loc, MULT_EXPR, type,
9115 diff, esz));
9117 return NULL_TREE;
9120 /* If the real or vector real constant CST of type TYPE has an exact
9121 inverse, return it, else return NULL. */
9123 tree
9124 exact_inverse (tree type, tree cst)
9126 REAL_VALUE_TYPE r;
9127 tree unit_type;
9128 machine_mode mode;
9130 switch (TREE_CODE (cst))
9132 case REAL_CST:
9133 r = TREE_REAL_CST (cst);
9135 if (exact_real_inverse (TYPE_MODE (type), &r))
9136 return build_real (type, r);
9138 return NULL_TREE;
9140 case VECTOR_CST:
9142 unit_type = TREE_TYPE (type);
9143 mode = TYPE_MODE (unit_type);
9145 tree_vector_builder elts;
9146 if (!elts.new_unary_operation (type, cst, false))
9147 return NULL_TREE;
9148 unsigned int count = elts.encoded_nelts ();
9149 for (unsigned int i = 0; i < count; ++i)
9151 r = TREE_REAL_CST (VECTOR_CST_ELT (cst, i));
9152 if (!exact_real_inverse (mode, &r))
9153 return NULL_TREE;
9154 elts.quick_push (build_real (unit_type, r));
9157 return elts.build ();
9160 default:
9161 return NULL_TREE;
9165 /* Mask out the tz least significant bits of X of type TYPE where
9166 tz is the number of trailing zeroes in Y. */
9167 static wide_int
9168 mask_with_tz (tree type, const wide_int &x, const wide_int &y)
9170 int tz = wi::ctz (y);
9171 if (tz > 0)
9172 return wi::mask (tz, true, TYPE_PRECISION (type)) & x;
9173 return x;
9176 /* Return true when T is an address and is known to be nonzero.
9177 For floating point we further ensure that T is not denormal.
9178 Similar logic is present in nonzero_address in rtlanal.h.
9180 If the return value is based on the assumption that signed overflow
9181 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
9182 change *STRICT_OVERFLOW_P. */
9184 static bool
9185 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
9187 tree type = TREE_TYPE (t);
9188 enum tree_code code;
9190 /* Doing something useful for floating point would need more work. */
9191 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9192 return false;
9194 code = TREE_CODE (t);
9195 switch (TREE_CODE_CLASS (code))
9197 case tcc_unary:
9198 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9199 strict_overflow_p);
9200 case tcc_binary:
9201 case tcc_comparison:
9202 return tree_binary_nonzero_warnv_p (code, type,
9203 TREE_OPERAND (t, 0),
9204 TREE_OPERAND (t, 1),
9205 strict_overflow_p);
9206 case tcc_constant:
9207 case tcc_declaration:
9208 case tcc_reference:
9209 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9211 default:
9212 break;
9215 switch (code)
9217 case TRUTH_NOT_EXPR:
9218 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
9219 strict_overflow_p);
9221 case TRUTH_AND_EXPR:
9222 case TRUTH_OR_EXPR:
9223 case TRUTH_XOR_EXPR:
9224 return tree_binary_nonzero_warnv_p (code, type,
9225 TREE_OPERAND (t, 0),
9226 TREE_OPERAND (t, 1),
9227 strict_overflow_p);
9229 case COND_EXPR:
9230 case CONSTRUCTOR:
9231 case OBJ_TYPE_REF:
9232 case ASSERT_EXPR:
9233 case ADDR_EXPR:
9234 case WITH_SIZE_EXPR:
9235 case SSA_NAME:
9236 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
9238 case COMPOUND_EXPR:
9239 case MODIFY_EXPR:
9240 case BIND_EXPR:
9241 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
9242 strict_overflow_p);
9244 case SAVE_EXPR:
9245 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
9246 strict_overflow_p);
9248 case CALL_EXPR:
9250 tree fndecl = get_callee_fndecl (t);
9251 if (!fndecl) return false;
9252 if (flag_delete_null_pointer_checks && !flag_check_new
9253 && DECL_IS_OPERATOR_NEW_P (fndecl)
9254 && !TREE_NOTHROW (fndecl))
9255 return true;
9256 if (flag_delete_null_pointer_checks
9257 && lookup_attribute ("returns_nonnull",
9258 TYPE_ATTRIBUTES (TREE_TYPE (fndecl))))
9259 return true;
9260 return alloca_call_p (t);
9263 default:
9264 break;
9266 return false;
9269 /* Return true when T is an address and is known to be nonzero.
9270 Handle warnings about undefined signed overflow. */
9272 bool
9273 tree_expr_nonzero_p (tree t)
9275 bool ret, strict_overflow_p;
9277 strict_overflow_p = false;
9278 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
9279 if (strict_overflow_p)
9280 fold_overflow_warning (("assuming signed overflow does not occur when "
9281 "determining that expression is always "
9282 "non-zero"),
9283 WARN_STRICT_OVERFLOW_MISC);
9284 return ret;
9287 /* Return true if T is known not to be equal to an integer W. */
9289 bool
9290 expr_not_equal_to (tree t, const wide_int &w)
9292 wide_int min, max, nz;
9293 value_range_kind rtype;
9294 switch (TREE_CODE (t))
9296 case INTEGER_CST:
9297 return wi::to_wide (t) != w;
9299 case SSA_NAME:
9300 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
9301 return false;
9302 rtype = get_range_info (t, &min, &max);
9303 if (rtype == VR_RANGE)
9305 if (wi::lt_p (max, w, TYPE_SIGN (TREE_TYPE (t))))
9306 return true;
9307 if (wi::lt_p (w, min, TYPE_SIGN (TREE_TYPE (t))))
9308 return true;
9310 else if (rtype == VR_ANTI_RANGE
9311 && wi::le_p (min, w, TYPE_SIGN (TREE_TYPE (t)))
9312 && wi::le_p (w, max, TYPE_SIGN (TREE_TYPE (t))))
9313 return true;
9314 /* If T has some known zero bits and W has any of those bits set,
9315 then T is known not to be equal to W. */
9316 if (wi::ne_p (wi::zext (wi::bit_and_not (w, get_nonzero_bits (t)),
9317 TYPE_PRECISION (TREE_TYPE (t))), 0))
9318 return true;
9319 return false;
9321 default:
9322 return false;
9326 /* Fold a binary expression of code CODE and type TYPE with operands
9327 OP0 and OP1. LOC is the location of the resulting expression.
9328 Return the folded expression if folding is successful. Otherwise,
9329 return NULL_TREE. */
9331 tree
9332 fold_binary_loc (location_t loc, enum tree_code code, tree type,
9333 tree op0, tree op1)
9335 enum tree_code_class kind = TREE_CODE_CLASS (code);
9336 tree arg0, arg1, tem;
9337 tree t1 = NULL_TREE;
9338 bool strict_overflow_p;
9339 unsigned int prec;
9341 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9342 && TREE_CODE_LENGTH (code) == 2
9343 && op0 != NULL_TREE
9344 && op1 != NULL_TREE);
9346 arg0 = op0;
9347 arg1 = op1;
9349 /* Strip any conversions that don't change the mode. This is
9350 safe for every expression, except for a comparison expression
9351 because its signedness is derived from its operands. So, in
9352 the latter case, only strip conversions that don't change the
9353 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9354 preserved.
9356 Note that this is done as an internal manipulation within the
9357 constant folder, in order to find the simplest representation
9358 of the arguments so that their form can be studied. In any
9359 cases, the appropriate type conversions should be put back in
9360 the tree that will get out of the constant folder. */
9362 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9364 STRIP_SIGN_NOPS (arg0);
9365 STRIP_SIGN_NOPS (arg1);
9367 else
9369 STRIP_NOPS (arg0);
9370 STRIP_NOPS (arg1);
9373 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9374 constant but we can't do arithmetic on them. */
9375 if (CONSTANT_CLASS_P (arg0) && CONSTANT_CLASS_P (arg1))
9377 tem = const_binop (code, type, arg0, arg1);
9378 if (tem != NULL_TREE)
9380 if (TREE_TYPE (tem) != type)
9381 tem = fold_convert_loc (loc, type, tem);
9382 return tem;
9386 /* If this is a commutative operation, and ARG0 is a constant, move it
9387 to ARG1 to reduce the number of tests below. */
9388 if (commutative_tree_code (code)
9389 && tree_swap_operands_p (arg0, arg1))
9390 return fold_build2_loc (loc, code, type, op1, op0);
9392 /* Likewise if this is a comparison, and ARG0 is a constant, move it
9393 to ARG1 to reduce the number of tests below. */
9394 if (kind == tcc_comparison
9395 && tree_swap_operands_p (arg0, arg1))
9396 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9398 tem = generic_simplify (loc, code, type, op0, op1);
9399 if (tem)
9400 return tem;
9402 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9404 First check for cases where an arithmetic operation is applied to a
9405 compound, conditional, or comparison operation. Push the arithmetic
9406 operation inside the compound or conditional to see if any folding
9407 can then be done. Convert comparison to conditional for this purpose.
9408 The also optimizes non-constant cases that used to be done in
9409 expand_expr.
9411 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9412 one of the operands is a comparison and the other is a comparison, a
9413 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9414 code below would make the expression more complex. Change it to a
9415 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9416 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9418 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9419 || code == EQ_EXPR || code == NE_EXPR)
9420 && !VECTOR_TYPE_P (TREE_TYPE (arg0))
9421 && ((truth_value_p (TREE_CODE (arg0))
9422 && (truth_value_p (TREE_CODE (arg1))
9423 || (TREE_CODE (arg1) == BIT_AND_EXPR
9424 && integer_onep (TREE_OPERAND (arg1, 1)))))
9425 || (truth_value_p (TREE_CODE (arg1))
9426 && (truth_value_p (TREE_CODE (arg0))
9427 || (TREE_CODE (arg0) == BIT_AND_EXPR
9428 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9430 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9431 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9432 : TRUTH_XOR_EXPR,
9433 boolean_type_node,
9434 fold_convert_loc (loc, boolean_type_node, arg0),
9435 fold_convert_loc (loc, boolean_type_node, arg1));
9437 if (code == EQ_EXPR)
9438 tem = invert_truthvalue_loc (loc, tem);
9440 return fold_convert_loc (loc, type, tem);
9443 if (TREE_CODE_CLASS (code) == tcc_binary
9444 || TREE_CODE_CLASS (code) == tcc_comparison)
9446 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9448 tem = fold_build2_loc (loc, code, type,
9449 fold_convert_loc (loc, TREE_TYPE (op0),
9450 TREE_OPERAND (arg0, 1)), op1);
9451 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9452 tem);
9454 if (TREE_CODE (arg1) == COMPOUND_EXPR)
9456 tem = fold_build2_loc (loc, code, type, op0,
9457 fold_convert_loc (loc, TREE_TYPE (op1),
9458 TREE_OPERAND (arg1, 1)));
9459 return build2_loc (loc, COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9460 tem);
9463 if (TREE_CODE (arg0) == COND_EXPR
9464 || TREE_CODE (arg0) == VEC_COND_EXPR
9465 || COMPARISON_CLASS_P (arg0))
9467 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9468 arg0, arg1,
9469 /*cond_first_p=*/1);
9470 if (tem != NULL_TREE)
9471 return tem;
9474 if (TREE_CODE (arg1) == COND_EXPR
9475 || TREE_CODE (arg1) == VEC_COND_EXPR
9476 || COMPARISON_CLASS_P (arg1))
9478 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
9479 arg1, arg0,
9480 /*cond_first_p=*/0);
9481 if (tem != NULL_TREE)
9482 return tem;
9486 switch (code)
9488 case MEM_REF:
9489 /* MEM[&MEM[p, CST1], CST2] -> MEM[p, CST1 + CST2]. */
9490 if (TREE_CODE (arg0) == ADDR_EXPR
9491 && TREE_CODE (TREE_OPERAND (arg0, 0)) == MEM_REF)
9493 tree iref = TREE_OPERAND (arg0, 0);
9494 return fold_build2 (MEM_REF, type,
9495 TREE_OPERAND (iref, 0),
9496 int_const_binop (PLUS_EXPR, arg1,
9497 TREE_OPERAND (iref, 1)));
9500 /* MEM[&a.b, CST2] -> MEM[&a, offsetof (a, b) + CST2]. */
9501 if (TREE_CODE (arg0) == ADDR_EXPR
9502 && handled_component_p (TREE_OPERAND (arg0, 0)))
9504 tree base;
9505 poly_int64 coffset;
9506 base = get_addr_base_and_unit_offset (TREE_OPERAND (arg0, 0),
9507 &coffset);
9508 if (!base)
9509 return NULL_TREE;
9510 return fold_build2 (MEM_REF, type,
9511 build_fold_addr_expr (base),
9512 int_const_binop (PLUS_EXPR, arg1,
9513 size_int (coffset)));
9516 return NULL_TREE;
9518 case POINTER_PLUS_EXPR:
9519 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9520 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9521 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9522 return fold_convert_loc (loc, type,
9523 fold_build2_loc (loc, PLUS_EXPR, sizetype,
9524 fold_convert_loc (loc, sizetype,
9525 arg1),
9526 fold_convert_loc (loc, sizetype,
9527 arg0)));
9529 return NULL_TREE;
9531 case PLUS_EXPR:
9532 if (INTEGRAL_TYPE_P (type) || VECTOR_INTEGER_TYPE_P (type))
9534 /* X + (X / CST) * -CST is X % CST. */
9535 if (TREE_CODE (arg1) == MULT_EXPR
9536 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9537 && operand_equal_p (arg0,
9538 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9540 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9541 tree cst1 = TREE_OPERAND (arg1, 1);
9542 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
9543 cst1, cst0);
9544 if (sum && integer_zerop (sum))
9545 return fold_convert_loc (loc, type,
9546 fold_build2_loc (loc, TRUNC_MOD_EXPR,
9547 TREE_TYPE (arg0), arg0,
9548 cst0));
9552 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the same or
9553 one. Make sure the type is not saturating and has the signedness of
9554 the stripped operands, as fold_plusminus_mult_expr will re-associate.
9555 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
9556 if ((TREE_CODE (arg0) == MULT_EXPR
9557 || TREE_CODE (arg1) == MULT_EXPR)
9558 && !TYPE_SATURATING (type)
9559 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
9560 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
9561 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9563 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
9564 if (tem)
9565 return tem;
9568 if (! FLOAT_TYPE_P (type))
9570 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9571 (plus (plus (mult) (mult)) (foo)) so that we can
9572 take advantage of the factoring cases below. */
9573 if (ANY_INTEGRAL_TYPE_P (type)
9574 && TYPE_OVERFLOW_WRAPS (type)
9575 && (((TREE_CODE (arg0) == PLUS_EXPR
9576 || TREE_CODE (arg0) == MINUS_EXPR)
9577 && TREE_CODE (arg1) == MULT_EXPR)
9578 || ((TREE_CODE (arg1) == PLUS_EXPR
9579 || TREE_CODE (arg1) == MINUS_EXPR)
9580 && TREE_CODE (arg0) == MULT_EXPR)))
9582 tree parg0, parg1, parg, marg;
9583 enum tree_code pcode;
9585 if (TREE_CODE (arg1) == MULT_EXPR)
9586 parg = arg0, marg = arg1;
9587 else
9588 parg = arg1, marg = arg0;
9589 pcode = TREE_CODE (parg);
9590 parg0 = TREE_OPERAND (parg, 0);
9591 parg1 = TREE_OPERAND (parg, 1);
9592 STRIP_NOPS (parg0);
9593 STRIP_NOPS (parg1);
9595 if (TREE_CODE (parg0) == MULT_EXPR
9596 && TREE_CODE (parg1) != MULT_EXPR)
9597 return fold_build2_loc (loc, pcode, type,
9598 fold_build2_loc (loc, PLUS_EXPR, type,
9599 fold_convert_loc (loc, type,
9600 parg0),
9601 fold_convert_loc (loc, type,
9602 marg)),
9603 fold_convert_loc (loc, type, parg1));
9604 if (TREE_CODE (parg0) != MULT_EXPR
9605 && TREE_CODE (parg1) == MULT_EXPR)
9606 return
9607 fold_build2_loc (loc, PLUS_EXPR, type,
9608 fold_convert_loc (loc, type, parg0),
9609 fold_build2_loc (loc, pcode, type,
9610 fold_convert_loc (loc, type, marg),
9611 fold_convert_loc (loc, type,
9612 parg1)));
9615 else
9617 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9618 to __complex__ ( x, y ). This is not the same for SNaNs or
9619 if signed zeros are involved. */
9620 if (!HONOR_SNANS (element_mode (arg0))
9621 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
9622 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9624 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9625 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
9626 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
9627 bool arg0rz = false, arg0iz = false;
9628 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9629 || (arg0i && (arg0iz = real_zerop (arg0i))))
9631 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
9632 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
9633 if (arg0rz && arg1i && real_zerop (arg1i))
9635 tree rp = arg1r ? arg1r
9636 : build1 (REALPART_EXPR, rtype, arg1);
9637 tree ip = arg0i ? arg0i
9638 : build1 (IMAGPART_EXPR, rtype, arg0);
9639 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9641 else if (arg0iz && arg1r && real_zerop (arg1r))
9643 tree rp = arg0r ? arg0r
9644 : build1 (REALPART_EXPR, rtype, arg0);
9645 tree ip = arg1i ? arg1i
9646 : build1 (IMAGPART_EXPR, rtype, arg1);
9647 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
9652 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9653 We associate floats only if the user has specified
9654 -fassociative-math. */
9655 if (flag_associative_math
9656 && TREE_CODE (arg1) == PLUS_EXPR
9657 && TREE_CODE (arg0) != MULT_EXPR)
9659 tree tree10 = TREE_OPERAND (arg1, 0);
9660 tree tree11 = TREE_OPERAND (arg1, 1);
9661 if (TREE_CODE (tree11) == MULT_EXPR
9662 && TREE_CODE (tree10) == MULT_EXPR)
9664 tree tree0;
9665 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
9666 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
9669 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9670 We associate floats only if the user has specified
9671 -fassociative-math. */
9672 if (flag_associative_math
9673 && TREE_CODE (arg0) == PLUS_EXPR
9674 && TREE_CODE (arg1) != MULT_EXPR)
9676 tree tree00 = TREE_OPERAND (arg0, 0);
9677 tree tree01 = TREE_OPERAND (arg0, 1);
9678 if (TREE_CODE (tree01) == MULT_EXPR
9679 && TREE_CODE (tree00) == MULT_EXPR)
9681 tree tree0;
9682 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
9683 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
9688 bit_rotate:
9689 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9690 is a rotate of A by C1 bits. */
9691 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9692 is a rotate of A by B bits.
9693 Similarly for (A << B) | (A >> (-B & C3)) where C3 is Z-1,
9694 though in this case CODE must be | and not + or ^, otherwise
9695 it doesn't return A when B is 0. */
9697 enum tree_code code0, code1;
9698 tree rtype;
9699 code0 = TREE_CODE (arg0);
9700 code1 = TREE_CODE (arg1);
9701 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9702 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9703 && operand_equal_p (TREE_OPERAND (arg0, 0),
9704 TREE_OPERAND (arg1, 0), 0)
9705 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9706 TYPE_UNSIGNED (rtype))
9707 /* Only create rotates in complete modes. Other cases are not
9708 expanded properly. */
9709 && (element_precision (rtype)
9710 == GET_MODE_UNIT_PRECISION (TYPE_MODE (rtype))))
9712 tree tree01, tree11;
9713 tree orig_tree01, orig_tree11;
9714 enum tree_code code01, code11;
9716 tree01 = orig_tree01 = TREE_OPERAND (arg0, 1);
9717 tree11 = orig_tree11 = TREE_OPERAND (arg1, 1);
9718 STRIP_NOPS (tree01);
9719 STRIP_NOPS (tree11);
9720 code01 = TREE_CODE (tree01);
9721 code11 = TREE_CODE (tree11);
9722 if (code11 != MINUS_EXPR
9723 && (code01 == MINUS_EXPR || code01 == BIT_AND_EXPR))
9725 std::swap (code0, code1);
9726 std::swap (code01, code11);
9727 std::swap (tree01, tree11);
9728 std::swap (orig_tree01, orig_tree11);
9730 if (code01 == INTEGER_CST
9731 && code11 == INTEGER_CST
9732 && (wi::to_widest (tree01) + wi::to_widest (tree11)
9733 == element_precision (rtype)))
9735 tem = build2_loc (loc, LROTATE_EXPR,
9736 rtype, TREE_OPERAND (arg0, 0),
9737 code0 == LSHIFT_EXPR
9738 ? orig_tree01 : orig_tree11);
9739 return fold_convert_loc (loc, type, tem);
9741 else if (code11 == MINUS_EXPR)
9743 tree tree110, tree111;
9744 tree110 = TREE_OPERAND (tree11, 0);
9745 tree111 = TREE_OPERAND (tree11, 1);
9746 STRIP_NOPS (tree110);
9747 STRIP_NOPS (tree111);
9748 if (TREE_CODE (tree110) == INTEGER_CST
9749 && compare_tree_int (tree110,
9750 element_precision (rtype)) == 0
9751 && operand_equal_p (tree01, tree111, 0))
9753 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9754 ? LROTATE_EXPR : RROTATE_EXPR),
9755 rtype, TREE_OPERAND (arg0, 0),
9756 orig_tree01);
9757 return fold_convert_loc (loc, type, tem);
9760 else if (code == BIT_IOR_EXPR
9761 && code11 == BIT_AND_EXPR
9762 && pow2p_hwi (element_precision (rtype)))
9764 tree tree110, tree111;
9765 tree110 = TREE_OPERAND (tree11, 0);
9766 tree111 = TREE_OPERAND (tree11, 1);
9767 STRIP_NOPS (tree110);
9768 STRIP_NOPS (tree111);
9769 if (TREE_CODE (tree110) == NEGATE_EXPR
9770 && TREE_CODE (tree111) == INTEGER_CST
9771 && compare_tree_int (tree111,
9772 element_precision (rtype) - 1) == 0
9773 && operand_equal_p (tree01, TREE_OPERAND (tree110, 0), 0))
9775 tem = build2_loc (loc, (code0 == LSHIFT_EXPR
9776 ? LROTATE_EXPR : RROTATE_EXPR),
9777 rtype, TREE_OPERAND (arg0, 0),
9778 orig_tree01);
9779 return fold_convert_loc (loc, type, tem);
9785 associate:
9786 /* In most languages, can't associate operations on floats through
9787 parentheses. Rather than remember where the parentheses were, we
9788 don't associate floats at all, unless the user has specified
9789 -fassociative-math.
9790 And, we need to make sure type is not saturating. */
9792 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9793 && !TYPE_SATURATING (type))
9795 tree var0, minus_var0, con0, minus_con0, lit0, minus_lit0;
9796 tree var1, minus_var1, con1, minus_con1, lit1, minus_lit1;
9797 tree atype = type;
9798 bool ok = true;
9800 /* Split both trees into variables, constants, and literals. Then
9801 associate each group together, the constants with literals,
9802 then the result with variables. This increases the chances of
9803 literals being recombined later and of generating relocatable
9804 expressions for the sum of a constant and literal. */
9805 var0 = split_tree (arg0, type, code,
9806 &minus_var0, &con0, &minus_con0,
9807 &lit0, &minus_lit0, 0);
9808 var1 = split_tree (arg1, type, code,
9809 &minus_var1, &con1, &minus_con1,
9810 &lit1, &minus_lit1, code == MINUS_EXPR);
9812 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9813 if (code == MINUS_EXPR)
9814 code = PLUS_EXPR;
9816 /* With undefined overflow prefer doing association in a type
9817 which wraps on overflow, if that is one of the operand types. */
9818 if ((POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
9819 && !TYPE_OVERFLOW_WRAPS (type))
9821 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
9822 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg0)))
9823 atype = TREE_TYPE (arg0);
9824 else if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9825 && TYPE_OVERFLOW_WRAPS (TREE_TYPE (arg1)))
9826 atype = TREE_TYPE (arg1);
9827 gcc_assert (TYPE_PRECISION (atype) == TYPE_PRECISION (type));
9830 /* With undefined overflow we can only associate constants with one
9831 variable, and constants whose association doesn't overflow. */
9832 if ((POINTER_TYPE_P (atype) || INTEGRAL_TYPE_P (atype))
9833 && !TYPE_OVERFLOW_WRAPS (atype))
9835 if ((var0 && var1) || (minus_var0 && minus_var1))
9837 /* ??? If split_tree would handle NEGATE_EXPR we could
9838 simply reject these cases and the allowed cases would
9839 be the var0/minus_var1 ones. */
9840 tree tmp0 = var0 ? var0 : minus_var0;
9841 tree tmp1 = var1 ? var1 : minus_var1;
9842 bool one_neg = false;
9844 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9846 tmp0 = TREE_OPERAND (tmp0, 0);
9847 one_neg = !one_neg;
9849 if (CONVERT_EXPR_P (tmp0)
9850 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9851 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
9852 <= TYPE_PRECISION (atype)))
9853 tmp0 = TREE_OPERAND (tmp0, 0);
9854 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9856 tmp1 = TREE_OPERAND (tmp1, 0);
9857 one_neg = !one_neg;
9859 if (CONVERT_EXPR_P (tmp1)
9860 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9861 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
9862 <= TYPE_PRECISION (atype)))
9863 tmp1 = TREE_OPERAND (tmp1, 0);
9864 /* The only case we can still associate with two variables
9865 is if they cancel out. */
9866 if (!one_neg
9867 || !operand_equal_p (tmp0, tmp1, 0))
9868 ok = false;
9870 else if ((var0 && minus_var1
9871 && ! operand_equal_p (var0, minus_var1, 0))
9872 || (minus_var0 && var1
9873 && ! operand_equal_p (minus_var0, var1, 0)))
9874 ok = false;
9877 /* Only do something if we found more than two objects. Otherwise,
9878 nothing has changed and we risk infinite recursion. */
9879 if (ok
9880 && ((var0 != 0) + (var1 != 0)
9881 + (minus_var0 != 0) + (minus_var1 != 0)
9882 + (con0 != 0) + (con1 != 0)
9883 + (minus_con0 != 0) + (minus_con1 != 0)
9884 + (lit0 != 0) + (lit1 != 0)
9885 + (minus_lit0 != 0) + (minus_lit1 != 0)) > 2)
9887 var0 = associate_trees (loc, var0, var1, code, atype);
9888 minus_var0 = associate_trees (loc, minus_var0, minus_var1,
9889 code, atype);
9890 con0 = associate_trees (loc, con0, con1, code, atype);
9891 minus_con0 = associate_trees (loc, minus_con0, minus_con1,
9892 code, atype);
9893 lit0 = associate_trees (loc, lit0, lit1, code, atype);
9894 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1,
9895 code, atype);
9897 if (minus_var0 && var0)
9899 var0 = associate_trees (loc, var0, minus_var0,
9900 MINUS_EXPR, atype);
9901 minus_var0 = 0;
9903 if (minus_con0 && con0)
9905 con0 = associate_trees (loc, con0, minus_con0,
9906 MINUS_EXPR, atype);
9907 minus_con0 = 0;
9910 /* Preserve the MINUS_EXPR if the negative part of the literal is
9911 greater than the positive part. Otherwise, the multiplicative
9912 folding code (i.e extract_muldiv) may be fooled in case
9913 unsigned constants are subtracted, like in the following
9914 example: ((X*2 + 4) - 8U)/2. */
9915 if (minus_lit0 && lit0)
9917 if (TREE_CODE (lit0) == INTEGER_CST
9918 && TREE_CODE (minus_lit0) == INTEGER_CST
9919 && tree_int_cst_lt (lit0, minus_lit0)
9920 /* But avoid ending up with only negated parts. */
9921 && (var0 || con0))
9923 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
9924 MINUS_EXPR, atype);
9925 lit0 = 0;
9927 else
9929 lit0 = associate_trees (loc, lit0, minus_lit0,
9930 MINUS_EXPR, atype);
9931 minus_lit0 = 0;
9935 /* Don't introduce overflows through reassociation. */
9936 if ((lit0 && TREE_OVERFLOW_P (lit0))
9937 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
9938 return NULL_TREE;
9940 /* Eliminate lit0 and minus_lit0 to con0 and minus_con0. */
9941 con0 = associate_trees (loc, con0, lit0, code, atype);
9942 lit0 = 0;
9943 minus_con0 = associate_trees (loc, minus_con0, minus_lit0,
9944 code, atype);
9945 minus_lit0 = 0;
9947 /* Eliminate minus_con0. */
9948 if (minus_con0)
9950 if (con0)
9951 con0 = associate_trees (loc, con0, minus_con0,
9952 MINUS_EXPR, atype);
9953 else if (var0)
9954 var0 = associate_trees (loc, var0, minus_con0,
9955 MINUS_EXPR, atype);
9956 else
9957 gcc_unreachable ();
9958 minus_con0 = 0;
9961 /* Eliminate minus_var0. */
9962 if (minus_var0)
9964 if (con0)
9965 con0 = associate_trees (loc, con0, minus_var0,
9966 MINUS_EXPR, atype);
9967 else
9968 gcc_unreachable ();
9969 minus_var0 = 0;
9972 return
9973 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
9974 code, atype));
9978 return NULL_TREE;
9980 case POINTER_DIFF_EXPR:
9981 case MINUS_EXPR:
9982 /* Fold &a[i] - &a[j] to i-j. */
9983 if (TREE_CODE (arg0) == ADDR_EXPR
9984 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
9985 && TREE_CODE (arg1) == ADDR_EXPR
9986 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
9988 tree tem = fold_addr_of_array_ref_difference (loc, type,
9989 TREE_OPERAND (arg0, 0),
9990 TREE_OPERAND (arg1, 0),
9991 code
9992 == POINTER_DIFF_EXPR);
9993 if (tem)
9994 return tem;
9997 /* Further transformations are not for pointers. */
9998 if (code == POINTER_DIFF_EXPR)
9999 return NULL_TREE;
10001 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10002 if (TREE_CODE (arg0) == NEGATE_EXPR
10003 && negate_expr_p (op1)
10004 /* If arg0 is e.g. unsigned int and type is int, then this could
10005 introduce UB, because if A is INT_MIN at runtime, the original
10006 expression can be well defined while the latter is not.
10007 See PR83269. */
10008 && !(ANY_INTEGRAL_TYPE_P (type)
10009 && TYPE_OVERFLOW_UNDEFINED (type)
10010 && ANY_INTEGRAL_TYPE_P (TREE_TYPE (arg0))
10011 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))))
10012 return fold_build2_loc (loc, MINUS_EXPR, type, negate_expr (op1),
10013 fold_convert_loc (loc, type,
10014 TREE_OPERAND (arg0, 0)));
10016 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10017 __complex__ ( x, -y ). This is not the same for SNaNs or if
10018 signed zeros are involved. */
10019 if (!HONOR_SNANS (element_mode (arg0))
10020 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10021 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10023 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10024 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10025 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10026 bool arg0rz = false, arg0iz = false;
10027 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10028 || (arg0i && (arg0iz = real_zerop (arg0i))))
10030 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10031 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10032 if (arg0rz && arg1i && real_zerop (arg1i))
10034 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10035 arg1r ? arg1r
10036 : build1 (REALPART_EXPR, rtype, arg1));
10037 tree ip = arg0i ? arg0i
10038 : build1 (IMAGPART_EXPR, rtype, arg0);
10039 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10041 else if (arg0iz && arg1r && real_zerop (arg1r))
10043 tree rp = arg0r ? arg0r
10044 : build1 (REALPART_EXPR, rtype, arg0);
10045 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10046 arg1i ? arg1i
10047 : build1 (IMAGPART_EXPR, rtype, arg1));
10048 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10053 /* A - B -> A + (-B) if B is easily negatable. */
10054 if (negate_expr_p (op1)
10055 && ! TYPE_OVERFLOW_SANITIZED (type)
10056 && ((FLOAT_TYPE_P (type)
10057 /* Avoid this transformation if B is a positive REAL_CST. */
10058 && (TREE_CODE (op1) != REAL_CST
10059 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (op1))))
10060 || INTEGRAL_TYPE_P (type)))
10061 return fold_build2_loc (loc, PLUS_EXPR, type,
10062 fold_convert_loc (loc, type, arg0),
10063 negate_expr (op1));
10065 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the same or
10066 one. Make sure the type is not saturating and has the signedness of
10067 the stripped operands, as fold_plusminus_mult_expr will re-associate.
10068 ??? The latter condition should use TYPE_OVERFLOW_* flags instead. */
10069 if ((TREE_CODE (arg0) == MULT_EXPR
10070 || TREE_CODE (arg1) == MULT_EXPR)
10071 && !TYPE_SATURATING (type)
10072 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg0))
10073 && TYPE_UNSIGNED (type) == TYPE_UNSIGNED (TREE_TYPE (arg1))
10074 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10076 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10077 if (tem)
10078 return tem;
10081 goto associate;
10083 case MULT_EXPR:
10084 if (! FLOAT_TYPE_P (type))
10086 /* Transform x * -C into -x * C if x is easily negatable. */
10087 if (TREE_CODE (op1) == INTEGER_CST
10088 && tree_int_cst_sgn (op1) == -1
10089 && negate_expr_p (op0)
10090 && negate_expr_p (op1)
10091 && (tem = negate_expr (op1)) != op1
10092 && ! TREE_OVERFLOW (tem))
10093 return fold_build2_loc (loc, MULT_EXPR, type,
10094 fold_convert_loc (loc, type,
10095 negate_expr (op0)), tem);
10097 strict_overflow_p = false;
10098 if (TREE_CODE (arg1) == INTEGER_CST
10099 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10100 &strict_overflow_p)) != 0)
10102 if (strict_overflow_p)
10103 fold_overflow_warning (("assuming signed overflow does not "
10104 "occur when simplifying "
10105 "multiplication"),
10106 WARN_STRICT_OVERFLOW_MISC);
10107 return fold_convert_loc (loc, type, tem);
10110 /* Optimize z * conj(z) for integer complex numbers. */
10111 if (TREE_CODE (arg0) == CONJ_EXPR
10112 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10113 return fold_mult_zconjz (loc, type, arg1);
10114 if (TREE_CODE (arg1) == CONJ_EXPR
10115 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10116 return fold_mult_zconjz (loc, type, arg0);
10118 else
10120 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10121 This is not the same for NaNs or if signed zeros are
10122 involved. */
10123 if (!HONOR_NANS (arg0)
10124 && !HONOR_SIGNED_ZEROS (element_mode (arg0))
10125 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10126 && TREE_CODE (arg1) == COMPLEX_CST
10127 && real_zerop (TREE_REALPART (arg1)))
10129 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10130 if (real_onep (TREE_IMAGPART (arg1)))
10131 return
10132 fold_build2_loc (loc, COMPLEX_EXPR, type,
10133 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
10134 rtype, arg0)),
10135 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
10136 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10137 return
10138 fold_build2_loc (loc, COMPLEX_EXPR, type,
10139 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
10140 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
10141 rtype, arg0)));
10144 /* Optimize z * conj(z) for floating point complex numbers.
10145 Guarded by flag_unsafe_math_optimizations as non-finite
10146 imaginary components don't produce scalar results. */
10147 if (flag_unsafe_math_optimizations
10148 && TREE_CODE (arg0) == CONJ_EXPR
10149 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10150 return fold_mult_zconjz (loc, type, arg1);
10151 if (flag_unsafe_math_optimizations
10152 && TREE_CODE (arg1) == CONJ_EXPR
10153 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10154 return fold_mult_zconjz (loc, type, arg0);
10156 goto associate;
10158 case BIT_IOR_EXPR:
10159 /* Canonicalize (X & C1) | C2. */
10160 if (TREE_CODE (arg0) == BIT_AND_EXPR
10161 && TREE_CODE (arg1) == INTEGER_CST
10162 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10164 int width = TYPE_PRECISION (type), w;
10165 wide_int c1 = wi::to_wide (TREE_OPERAND (arg0, 1));
10166 wide_int c2 = wi::to_wide (arg1);
10168 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10169 if ((c1 & c2) == c1)
10170 return omit_one_operand_loc (loc, type, arg1,
10171 TREE_OPERAND (arg0, 0));
10173 wide_int msk = wi::mask (width, false,
10174 TYPE_PRECISION (TREE_TYPE (arg1)));
10176 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10177 if (wi::bit_and_not (msk, c1 | c2) == 0)
10179 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10180 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10183 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10184 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10185 mode which allows further optimizations. */
10186 c1 &= msk;
10187 c2 &= msk;
10188 wide_int c3 = wi::bit_and_not (c1, c2);
10189 for (w = BITS_PER_UNIT; w <= width; w <<= 1)
10191 wide_int mask = wi::mask (w, false,
10192 TYPE_PRECISION (type));
10193 if (((c1 | c2) & mask) == mask
10194 && wi::bit_and_not (c1, mask) == 0)
10196 c3 = mask;
10197 break;
10201 if (c3 != c1)
10203 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10204 tem = fold_build2_loc (loc, BIT_AND_EXPR, type, tem,
10205 wide_int_to_tree (type, c3));
10206 return fold_build2_loc (loc, BIT_IOR_EXPR, type, tem, arg1);
10210 /* See if this can be simplified into a rotate first. If that
10211 is unsuccessful continue in the association code. */
10212 goto bit_rotate;
10214 case BIT_XOR_EXPR:
10215 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10216 if (TREE_CODE (arg0) == BIT_AND_EXPR
10217 && INTEGRAL_TYPE_P (type)
10218 && integer_onep (TREE_OPERAND (arg0, 1))
10219 && integer_onep (arg1))
10220 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
10221 build_zero_cst (TREE_TYPE (arg0)));
10223 /* See if this can be simplified into a rotate first. If that
10224 is unsuccessful continue in the association code. */
10225 goto bit_rotate;
10227 case BIT_AND_EXPR:
10228 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10229 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10230 && INTEGRAL_TYPE_P (type)
10231 && integer_onep (TREE_OPERAND (arg0, 1))
10232 && integer_onep (arg1))
10234 tree tem2;
10235 tem = TREE_OPERAND (arg0, 0);
10236 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10237 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10238 tem, tem2);
10239 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10240 build_zero_cst (TREE_TYPE (tem)));
10242 /* Fold ~X & 1 as (X & 1) == 0. */
10243 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10244 && INTEGRAL_TYPE_P (type)
10245 && integer_onep (arg1))
10247 tree tem2;
10248 tem = TREE_OPERAND (arg0, 0);
10249 tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
10250 tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
10251 tem, tem2);
10252 return fold_build2_loc (loc, EQ_EXPR, type, tem2,
10253 build_zero_cst (TREE_TYPE (tem)));
10255 /* Fold !X & 1 as X == 0. */
10256 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10257 && integer_onep (arg1))
10259 tem = TREE_OPERAND (arg0, 0);
10260 return fold_build2_loc (loc, EQ_EXPR, type, tem,
10261 build_zero_cst (TREE_TYPE (tem)));
10264 /* Fold (X * Y) & -(1 << CST) to X * Y if Y is a constant
10265 multiple of 1 << CST. */
10266 if (TREE_CODE (arg1) == INTEGER_CST)
10268 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
10269 wide_int ncst1 = -cst1;
10270 if ((cst1 & ncst1) == ncst1
10271 && multiple_of_p (type, arg0,
10272 wide_int_to_tree (TREE_TYPE (arg1), ncst1)))
10273 return fold_convert_loc (loc, type, arg0);
10276 /* Fold (X * CST1) & CST2 to zero if we can, or drop known zero
10277 bits from CST2. */
10278 if (TREE_CODE (arg1) == INTEGER_CST
10279 && TREE_CODE (arg0) == MULT_EXPR
10280 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10282 wi::tree_to_wide_ref warg1 = wi::to_wide (arg1);
10283 wide_int masked
10284 = mask_with_tz (type, warg1, wi::to_wide (TREE_OPERAND (arg0, 1)));
10286 if (masked == 0)
10287 return omit_two_operands_loc (loc, type, build_zero_cst (type),
10288 arg0, arg1);
10289 else if (masked != warg1)
10291 /* Avoid the transform if arg1 is a mask of some
10292 mode which allows further optimizations. */
10293 int pop = wi::popcount (warg1);
10294 if (!(pop >= BITS_PER_UNIT
10295 && pow2p_hwi (pop)
10296 && wi::mask (pop, false, warg1.get_precision ()) == warg1))
10297 return fold_build2_loc (loc, code, type, op0,
10298 wide_int_to_tree (type, masked));
10302 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10303 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10304 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10306 prec = element_precision (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10308 wide_int mask = wide_int::from (wi::to_wide (arg1), prec, UNSIGNED);
10309 if (mask == -1)
10310 return
10311 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10314 goto associate;
10316 case RDIV_EXPR:
10317 /* Don't touch a floating-point divide by zero unless the mode
10318 of the constant can represent infinity. */
10319 if (TREE_CODE (arg1) == REAL_CST
10320 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10321 && real_zerop (arg1))
10322 return NULL_TREE;
10324 /* (-A) / (-B) -> A / B */
10325 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10326 return fold_build2_loc (loc, RDIV_EXPR, type,
10327 TREE_OPERAND (arg0, 0),
10328 negate_expr (arg1));
10329 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10330 return fold_build2_loc (loc, RDIV_EXPR, type,
10331 negate_expr (arg0),
10332 TREE_OPERAND (arg1, 0));
10333 return NULL_TREE;
10335 case TRUNC_DIV_EXPR:
10336 /* Fall through */
10338 case FLOOR_DIV_EXPR:
10339 /* Simplify A / (B << N) where A and B are positive and B is
10340 a power of 2, to A >> (N + log2(B)). */
10341 strict_overflow_p = false;
10342 if (TREE_CODE (arg1) == LSHIFT_EXPR
10343 && (TYPE_UNSIGNED (type)
10344 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
10346 tree sval = TREE_OPERAND (arg1, 0);
10347 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
10349 tree sh_cnt = TREE_OPERAND (arg1, 1);
10350 tree pow2 = build_int_cst (TREE_TYPE (sh_cnt),
10351 wi::exact_log2 (wi::to_wide (sval)));
10353 if (strict_overflow_p)
10354 fold_overflow_warning (("assuming signed overflow does not "
10355 "occur when simplifying A / (B << N)"),
10356 WARN_STRICT_OVERFLOW_MISC);
10358 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
10359 sh_cnt, pow2);
10360 return fold_build2_loc (loc, RSHIFT_EXPR, type,
10361 fold_convert_loc (loc, type, arg0), sh_cnt);
10365 /* Fall through */
10367 case ROUND_DIV_EXPR:
10368 case CEIL_DIV_EXPR:
10369 case EXACT_DIV_EXPR:
10370 if (integer_zerop (arg1))
10371 return NULL_TREE;
10373 /* Convert -A / -B to A / B when the type is signed and overflow is
10374 undefined. */
10375 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10376 && TREE_CODE (op0) == NEGATE_EXPR
10377 && negate_expr_p (op1))
10379 if (INTEGRAL_TYPE_P (type))
10380 fold_overflow_warning (("assuming signed overflow does not occur "
10381 "when distributing negation across "
10382 "division"),
10383 WARN_STRICT_OVERFLOW_MISC);
10384 return fold_build2_loc (loc, code, type,
10385 fold_convert_loc (loc, type,
10386 TREE_OPERAND (arg0, 0)),
10387 negate_expr (op1));
10389 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
10390 && TREE_CODE (arg1) == NEGATE_EXPR
10391 && negate_expr_p (op0))
10393 if (INTEGRAL_TYPE_P (type))
10394 fold_overflow_warning (("assuming signed overflow does not occur "
10395 "when distributing negation across "
10396 "division"),
10397 WARN_STRICT_OVERFLOW_MISC);
10398 return fold_build2_loc (loc, code, type,
10399 negate_expr (op0),
10400 fold_convert_loc (loc, type,
10401 TREE_OPERAND (arg1, 0)));
10404 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
10405 operation, EXACT_DIV_EXPR.
10407 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
10408 At one time others generated faster code, it's not clear if they do
10409 after the last round to changes to the DIV code in expmed.c. */
10410 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
10411 && multiple_of_p (type, arg0, arg1))
10412 return fold_build2_loc (loc, EXACT_DIV_EXPR, type,
10413 fold_convert (type, arg0),
10414 fold_convert (type, arg1));
10416 strict_overflow_p = false;
10417 if (TREE_CODE (arg1) == INTEGER_CST
10418 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10419 &strict_overflow_p)) != 0)
10421 if (strict_overflow_p)
10422 fold_overflow_warning (("assuming signed overflow does not occur "
10423 "when simplifying division"),
10424 WARN_STRICT_OVERFLOW_MISC);
10425 return fold_convert_loc (loc, type, tem);
10428 return NULL_TREE;
10430 case CEIL_MOD_EXPR:
10431 case FLOOR_MOD_EXPR:
10432 case ROUND_MOD_EXPR:
10433 case TRUNC_MOD_EXPR:
10434 strict_overflow_p = false;
10435 if (TREE_CODE (arg1) == INTEGER_CST
10436 && (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10437 &strict_overflow_p)) != 0)
10439 if (strict_overflow_p)
10440 fold_overflow_warning (("assuming signed overflow does not occur "
10441 "when simplifying modulus"),
10442 WARN_STRICT_OVERFLOW_MISC);
10443 return fold_convert_loc (loc, type, tem);
10446 return NULL_TREE;
10448 case LROTATE_EXPR:
10449 case RROTATE_EXPR:
10450 case RSHIFT_EXPR:
10451 case LSHIFT_EXPR:
10452 /* Since negative shift count is not well-defined,
10453 don't try to compute it in the compiler. */
10454 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
10455 return NULL_TREE;
10457 prec = element_precision (type);
10459 /* If we have a rotate of a bit operation with the rotate count and
10460 the second operand of the bit operation both constant,
10461 permute the two operations. */
10462 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10463 && (TREE_CODE (arg0) == BIT_AND_EXPR
10464 || TREE_CODE (arg0) == BIT_IOR_EXPR
10465 || TREE_CODE (arg0) == BIT_XOR_EXPR)
10466 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10468 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10469 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10470 return fold_build2_loc (loc, TREE_CODE (arg0), type,
10471 fold_build2_loc (loc, code, type,
10472 arg00, arg1),
10473 fold_build2_loc (loc, code, type,
10474 arg01, arg1));
10477 /* Two consecutive rotates adding up to the some integer
10478 multiple of the precision of the type can be ignored. */
10479 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
10480 && TREE_CODE (arg0) == RROTATE_EXPR
10481 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10482 && wi::umod_trunc (wi::to_wide (arg1)
10483 + wi::to_wide (TREE_OPERAND (arg0, 1)),
10484 prec) == 0)
10485 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10487 return NULL_TREE;
10489 case MIN_EXPR:
10490 case MAX_EXPR:
10491 goto associate;
10493 case TRUTH_ANDIF_EXPR:
10494 /* Note that the operands of this must be ints
10495 and their values must be 0 or 1.
10496 ("true" is a fixed value perhaps depending on the language.) */
10497 /* If first arg is constant zero, return it. */
10498 if (integer_zerop (arg0))
10499 return fold_convert_loc (loc, type, arg0);
10500 /* FALLTHRU */
10501 case TRUTH_AND_EXPR:
10502 /* If either arg is constant true, drop it. */
10503 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10504 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10505 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
10506 /* Preserve sequence points. */
10507 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10508 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10509 /* If second arg is constant zero, result is zero, but first arg
10510 must be evaluated. */
10511 if (integer_zerop (arg1))
10512 return omit_one_operand_loc (loc, type, arg1, arg0);
10513 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10514 case will be handled here. */
10515 if (integer_zerop (arg0))
10516 return omit_one_operand_loc (loc, type, arg0, arg1);
10518 /* !X && X is always false. */
10519 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10520 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10521 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
10522 /* X && !X is always false. */
10523 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10524 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10525 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10527 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
10528 means A >= Y && A != MAX, but in this case we know that
10529 A < X <= MAX. */
10531 if (!TREE_SIDE_EFFECTS (arg0)
10532 && !TREE_SIDE_EFFECTS (arg1))
10534 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
10535 if (tem && !operand_equal_p (tem, arg0, 0))
10536 return fold_build2_loc (loc, code, type, tem, arg1);
10538 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
10539 if (tem && !operand_equal_p (tem, arg1, 0))
10540 return fold_build2_loc (loc, code, type, arg0, tem);
10543 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10544 != NULL_TREE)
10545 return tem;
10547 return NULL_TREE;
10549 case TRUTH_ORIF_EXPR:
10550 /* Note that the operands of this must be ints
10551 and their values must be 0 or true.
10552 ("true" is a fixed value perhaps depending on the language.) */
10553 /* If first arg is constant true, return it. */
10554 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10555 return fold_convert_loc (loc, type, arg0);
10556 /* FALLTHRU */
10557 case TRUTH_OR_EXPR:
10558 /* If either arg is constant zero, drop it. */
10559 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
10560 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10561 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
10562 /* Preserve sequence points. */
10563 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
10564 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10565 /* If second arg is constant true, result is true, but we must
10566 evaluate first arg. */
10567 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
10568 return omit_one_operand_loc (loc, type, arg1, arg0);
10569 /* Likewise for first arg, but note this only occurs here for
10570 TRUTH_OR_EXPR. */
10571 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
10572 return omit_one_operand_loc (loc, type, arg0, arg1);
10574 /* !X || X is always true. */
10575 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10576 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10577 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10578 /* X || !X is always true. */
10579 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10580 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10581 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10583 /* (X && !Y) || (!X && Y) is X ^ Y */
10584 if (TREE_CODE (arg0) == TRUTH_AND_EXPR
10585 && TREE_CODE (arg1) == TRUTH_AND_EXPR)
10587 tree a0, a1, l0, l1, n0, n1;
10589 a0 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10590 a1 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10592 l0 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10593 l1 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10595 n0 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l0);
10596 n1 = fold_build1_loc (loc, TRUTH_NOT_EXPR, type, l1);
10598 if ((operand_equal_p (n0, a0, 0)
10599 && operand_equal_p (n1, a1, 0))
10600 || (operand_equal_p (n0, a1, 0)
10601 && operand_equal_p (n1, a0, 0)))
10602 return fold_build2_loc (loc, TRUTH_XOR_EXPR, type, l0, n1);
10605 if ((tem = fold_truth_andor (loc, code, type, arg0, arg1, op0, op1))
10606 != NULL_TREE)
10607 return tem;
10609 return NULL_TREE;
10611 case TRUTH_XOR_EXPR:
10612 /* If the second arg is constant zero, drop it. */
10613 if (integer_zerop (arg1))
10614 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10615 /* If the second arg is constant true, this is a logical inversion. */
10616 if (integer_onep (arg1))
10618 tem = invert_truthvalue_loc (loc, arg0);
10619 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
10621 /* Identical arguments cancel to zero. */
10622 if (operand_equal_p (arg0, arg1, 0))
10623 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
10625 /* !X ^ X is always true. */
10626 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
10627 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10628 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
10630 /* X ^ !X is always true. */
10631 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
10632 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10633 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
10635 return NULL_TREE;
10637 case EQ_EXPR:
10638 case NE_EXPR:
10639 STRIP_NOPS (arg0);
10640 STRIP_NOPS (arg1);
10642 tem = fold_comparison (loc, code, type, op0, op1);
10643 if (tem != NULL_TREE)
10644 return tem;
10646 /* bool_var != 1 becomes !bool_var. */
10647 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
10648 && code == NE_EXPR)
10649 return fold_convert_loc (loc, type,
10650 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10651 TREE_TYPE (arg0), arg0));
10653 /* bool_var == 0 becomes !bool_var. */
10654 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
10655 && code == EQ_EXPR)
10656 return fold_convert_loc (loc, type,
10657 fold_build1_loc (loc, TRUTH_NOT_EXPR,
10658 TREE_TYPE (arg0), arg0));
10660 /* !exp != 0 becomes !exp */
10661 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
10662 && code == NE_EXPR)
10663 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10665 /* If this is an EQ or NE comparison with zero and ARG0 is
10666 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
10667 two operations, but the latter can be done in one less insn
10668 on machines that have only two-operand insns or on which a
10669 constant cannot be the first operand. */
10670 if (TREE_CODE (arg0) == BIT_AND_EXPR
10671 && integer_zerop (arg1))
10673 tree arg00 = TREE_OPERAND (arg0, 0);
10674 tree arg01 = TREE_OPERAND (arg0, 1);
10675 if (TREE_CODE (arg00) == LSHIFT_EXPR
10676 && integer_onep (TREE_OPERAND (arg00, 0)))
10678 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
10679 arg01, TREE_OPERAND (arg00, 1));
10680 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10681 build_int_cst (TREE_TYPE (arg0), 1));
10682 return fold_build2_loc (loc, code, type,
10683 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10684 arg1);
10686 else if (TREE_CODE (arg01) == LSHIFT_EXPR
10687 && integer_onep (TREE_OPERAND (arg01, 0)))
10689 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
10690 arg00, TREE_OPERAND (arg01, 1));
10691 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
10692 build_int_cst (TREE_TYPE (arg0), 1));
10693 return fold_build2_loc (loc, code, type,
10694 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
10695 arg1);
10699 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
10700 C1 is a valid shift constant, and C2 is a power of two, i.e.
10701 a single bit. */
10702 if (TREE_CODE (arg0) == BIT_AND_EXPR
10703 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
10704 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
10705 == INTEGER_CST
10706 && integer_pow2p (TREE_OPERAND (arg0, 1))
10707 && integer_zerop (arg1))
10709 tree itype = TREE_TYPE (arg0);
10710 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
10711 prec = TYPE_PRECISION (itype);
10713 /* Check for a valid shift count. */
10714 if (wi::ltu_p (wi::to_wide (arg001), prec))
10716 tree arg01 = TREE_OPERAND (arg0, 1);
10717 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10718 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
10719 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
10720 can be rewritten as (X & (C2 << C1)) != 0. */
10721 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
10723 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
10724 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
10725 return fold_build2_loc (loc, code, type, tem,
10726 fold_convert_loc (loc, itype, arg1));
10728 /* Otherwise, for signed (arithmetic) shifts,
10729 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
10730 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
10731 else if (!TYPE_UNSIGNED (itype))
10732 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
10733 arg000, build_int_cst (itype, 0));
10734 /* Otherwise, of unsigned (logical) shifts,
10735 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
10736 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
10737 else
10738 return omit_one_operand_loc (loc, type,
10739 code == EQ_EXPR ? integer_one_node
10740 : integer_zero_node,
10741 arg000);
10745 /* If this is a comparison of a field, we may be able to simplify it. */
10746 if ((TREE_CODE (arg0) == COMPONENT_REF
10747 || TREE_CODE (arg0) == BIT_FIELD_REF)
10748 /* Handle the constant case even without -O
10749 to make sure the warnings are given. */
10750 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
10752 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
10753 if (t1)
10754 return t1;
10757 /* Optimize comparisons of strlen vs zero to a compare of the
10758 first character of the string vs zero. To wit,
10759 strlen(ptr) == 0 => *ptr == 0
10760 strlen(ptr) != 0 => *ptr != 0
10761 Other cases should reduce to one of these two (or a constant)
10762 due to the return value of strlen being unsigned. */
10763 if (TREE_CODE (arg0) == CALL_EXPR && integer_zerop (arg1))
10765 tree fndecl = get_callee_fndecl (arg0);
10767 if (fndecl
10768 && fndecl_built_in_p (fndecl, BUILT_IN_STRLEN)
10769 && call_expr_nargs (arg0) == 1
10770 && (TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0)))
10771 == POINTER_TYPE))
10773 tree ptrtype
10774 = build_pointer_type (build_qualified_type (char_type_node,
10775 TYPE_QUAL_CONST));
10776 tree ptr = fold_convert_loc (loc, ptrtype,
10777 CALL_EXPR_ARG (arg0, 0));
10778 tree iref = build_fold_indirect_ref_loc (loc, ptr);
10779 return fold_build2_loc (loc, code, type, iref,
10780 build_int_cst (TREE_TYPE (iref), 0));
10784 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
10785 of X. Similarly fold (X >> C) == 0 into X >= 0. */
10786 if (TREE_CODE (arg0) == RSHIFT_EXPR
10787 && integer_zerop (arg1)
10788 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10790 tree arg00 = TREE_OPERAND (arg0, 0);
10791 tree arg01 = TREE_OPERAND (arg0, 1);
10792 tree itype = TREE_TYPE (arg00);
10793 if (wi::to_wide (arg01) == element_precision (itype) - 1)
10795 if (TYPE_UNSIGNED (itype))
10797 itype = signed_type_for (itype);
10798 arg00 = fold_convert_loc (loc, itype, arg00);
10800 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
10801 type, arg00, build_zero_cst (itype));
10805 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
10806 (X & C) == 0 when C is a single bit. */
10807 if (TREE_CODE (arg0) == BIT_AND_EXPR
10808 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
10809 && integer_zerop (arg1)
10810 && integer_pow2p (TREE_OPERAND (arg0, 1)))
10812 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
10813 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
10814 TREE_OPERAND (arg0, 1));
10815 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
10816 type, tem,
10817 fold_convert_loc (loc, TREE_TYPE (arg0),
10818 arg1));
10821 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
10822 constant C is a power of two, i.e. a single bit. */
10823 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10824 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
10825 && integer_zerop (arg1)
10826 && integer_pow2p (TREE_OPERAND (arg0, 1))
10827 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10828 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10830 tree arg00 = TREE_OPERAND (arg0, 0);
10831 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10832 arg00, build_int_cst (TREE_TYPE (arg00), 0));
10835 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
10836 when is C is a power of two, i.e. a single bit. */
10837 if (TREE_CODE (arg0) == BIT_AND_EXPR
10838 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
10839 && integer_zerop (arg1)
10840 && integer_pow2p (TREE_OPERAND (arg0, 1))
10841 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
10842 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
10844 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
10845 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
10846 arg000, TREE_OPERAND (arg0, 1));
10847 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
10848 tem, build_int_cst (TREE_TYPE (tem), 0));
10851 if (integer_zerop (arg1)
10852 && tree_expr_nonzero_p (arg0))
10854 tree res = constant_boolean_node (code==NE_EXPR, type);
10855 return omit_one_operand_loc (loc, type, res, arg0);
10858 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
10859 if (TREE_CODE (arg0) == BIT_AND_EXPR
10860 && TREE_CODE (arg1) == BIT_AND_EXPR)
10862 tree arg00 = TREE_OPERAND (arg0, 0);
10863 tree arg01 = TREE_OPERAND (arg0, 1);
10864 tree arg10 = TREE_OPERAND (arg1, 0);
10865 tree arg11 = TREE_OPERAND (arg1, 1);
10866 tree itype = TREE_TYPE (arg0);
10868 if (operand_equal_p (arg01, arg11, 0))
10870 tem = fold_convert_loc (loc, itype, arg10);
10871 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10872 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10873 return fold_build2_loc (loc, code, type, tem,
10874 build_zero_cst (itype));
10876 if (operand_equal_p (arg01, arg10, 0))
10878 tem = fold_convert_loc (loc, itype, arg11);
10879 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10880 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg01);
10881 return fold_build2_loc (loc, code, type, tem,
10882 build_zero_cst (itype));
10884 if (operand_equal_p (arg00, arg11, 0))
10886 tem = fold_convert_loc (loc, itype, arg10);
10887 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10888 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10889 return fold_build2_loc (loc, code, type, tem,
10890 build_zero_cst (itype));
10892 if (operand_equal_p (arg00, arg10, 0))
10894 tem = fold_convert_loc (loc, itype, arg11);
10895 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01, tem);
10896 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, tem, arg00);
10897 return fold_build2_loc (loc, code, type, tem,
10898 build_zero_cst (itype));
10902 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10903 && TREE_CODE (arg1) == BIT_XOR_EXPR)
10905 tree arg00 = TREE_OPERAND (arg0, 0);
10906 tree arg01 = TREE_OPERAND (arg0, 1);
10907 tree arg10 = TREE_OPERAND (arg1, 0);
10908 tree arg11 = TREE_OPERAND (arg1, 1);
10909 tree itype = TREE_TYPE (arg0);
10911 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
10912 operand_equal_p guarantees no side-effects so we don't need
10913 to use omit_one_operand on Z. */
10914 if (operand_equal_p (arg01, arg11, 0))
10915 return fold_build2_loc (loc, code, type, arg00,
10916 fold_convert_loc (loc, TREE_TYPE (arg00),
10917 arg10));
10918 if (operand_equal_p (arg01, arg10, 0))
10919 return fold_build2_loc (loc, code, type, arg00,
10920 fold_convert_loc (loc, TREE_TYPE (arg00),
10921 arg11));
10922 if (operand_equal_p (arg00, arg11, 0))
10923 return fold_build2_loc (loc, code, type, arg01,
10924 fold_convert_loc (loc, TREE_TYPE (arg01),
10925 arg10));
10926 if (operand_equal_p (arg00, arg10, 0))
10927 return fold_build2_loc (loc, code, type, arg01,
10928 fold_convert_loc (loc, TREE_TYPE (arg01),
10929 arg11));
10931 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
10932 if (TREE_CODE (arg01) == INTEGER_CST
10933 && TREE_CODE (arg11) == INTEGER_CST)
10935 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg01,
10936 fold_convert_loc (loc, itype, arg11));
10937 tem = fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00, tem);
10938 return fold_build2_loc (loc, code, type, tem,
10939 fold_convert_loc (loc, itype, arg10));
10943 /* Attempt to simplify equality/inequality comparisons of complex
10944 values. Only lower the comparison if the result is known or
10945 can be simplified to a single scalar comparison. */
10946 if ((TREE_CODE (arg0) == COMPLEX_EXPR
10947 || TREE_CODE (arg0) == COMPLEX_CST)
10948 && (TREE_CODE (arg1) == COMPLEX_EXPR
10949 || TREE_CODE (arg1) == COMPLEX_CST))
10951 tree real0, imag0, real1, imag1;
10952 tree rcond, icond;
10954 if (TREE_CODE (arg0) == COMPLEX_EXPR)
10956 real0 = TREE_OPERAND (arg0, 0);
10957 imag0 = TREE_OPERAND (arg0, 1);
10959 else
10961 real0 = TREE_REALPART (arg0);
10962 imag0 = TREE_IMAGPART (arg0);
10965 if (TREE_CODE (arg1) == COMPLEX_EXPR)
10967 real1 = TREE_OPERAND (arg1, 0);
10968 imag1 = TREE_OPERAND (arg1, 1);
10970 else
10972 real1 = TREE_REALPART (arg1);
10973 imag1 = TREE_IMAGPART (arg1);
10976 rcond = fold_binary_loc (loc, code, type, real0, real1);
10977 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
10979 if (integer_zerop (rcond))
10981 if (code == EQ_EXPR)
10982 return omit_two_operands_loc (loc, type, boolean_false_node,
10983 imag0, imag1);
10984 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
10986 else
10988 if (code == NE_EXPR)
10989 return omit_two_operands_loc (loc, type, boolean_true_node,
10990 imag0, imag1);
10991 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
10995 icond = fold_binary_loc (loc, code, type, imag0, imag1);
10996 if (icond && TREE_CODE (icond) == INTEGER_CST)
10998 if (integer_zerop (icond))
11000 if (code == EQ_EXPR)
11001 return omit_two_operands_loc (loc, type, boolean_false_node,
11002 real0, real1);
11003 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
11005 else
11007 if (code == NE_EXPR)
11008 return omit_two_operands_loc (loc, type, boolean_true_node,
11009 real0, real1);
11010 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
11015 return NULL_TREE;
11017 case LT_EXPR:
11018 case GT_EXPR:
11019 case LE_EXPR:
11020 case GE_EXPR:
11021 tem = fold_comparison (loc, code, type, op0, op1);
11022 if (tem != NULL_TREE)
11023 return tem;
11025 /* Transform comparisons of the form X +- C CMP X. */
11026 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11027 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11028 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
11029 && !HONOR_SNANS (arg0))
11031 tree arg01 = TREE_OPERAND (arg0, 1);
11032 enum tree_code code0 = TREE_CODE (arg0);
11033 int is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
11035 /* (X - c) > X becomes false. */
11036 if (code == GT_EXPR
11037 && ((code0 == MINUS_EXPR && is_positive >= 0)
11038 || (code0 == PLUS_EXPR && is_positive <= 0)))
11039 return constant_boolean_node (0, type);
11041 /* Likewise (X + c) < X becomes false. */
11042 if (code == LT_EXPR
11043 && ((code0 == PLUS_EXPR && is_positive >= 0)
11044 || (code0 == MINUS_EXPR && is_positive <= 0)))
11045 return constant_boolean_node (0, type);
11047 /* Convert (X - c) <= X to true. */
11048 if (!HONOR_NANS (arg1)
11049 && code == LE_EXPR
11050 && ((code0 == MINUS_EXPR && is_positive >= 0)
11051 || (code0 == PLUS_EXPR && is_positive <= 0)))
11052 return constant_boolean_node (1, type);
11054 /* Convert (X + c) >= X to true. */
11055 if (!HONOR_NANS (arg1)
11056 && code == GE_EXPR
11057 && ((code0 == PLUS_EXPR && is_positive >= 0)
11058 || (code0 == MINUS_EXPR && is_positive <= 0)))
11059 return constant_boolean_node (1, type);
11062 /* If we are comparing an ABS_EXPR with a constant, we can
11063 convert all the cases into explicit comparisons, but they may
11064 well not be faster than doing the ABS and one comparison.
11065 But ABS (X) <= C is a range comparison, which becomes a subtraction
11066 and a comparison, and is probably faster. */
11067 if (code == LE_EXPR
11068 && TREE_CODE (arg1) == INTEGER_CST
11069 && TREE_CODE (arg0) == ABS_EXPR
11070 && ! TREE_SIDE_EFFECTS (arg0)
11071 && (tem = negate_expr (arg1)) != 0
11072 && TREE_CODE (tem) == INTEGER_CST
11073 && !TREE_OVERFLOW (tem))
11074 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
11075 build2 (GE_EXPR, type,
11076 TREE_OPERAND (arg0, 0), tem),
11077 build2 (LE_EXPR, type,
11078 TREE_OPERAND (arg0, 0), arg1));
11080 /* Convert ABS_EXPR<x> >= 0 to true. */
11081 strict_overflow_p = false;
11082 if (code == GE_EXPR
11083 && (integer_zerop (arg1)
11084 || (! HONOR_NANS (arg0)
11085 && real_zerop (arg1)))
11086 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11088 if (strict_overflow_p)
11089 fold_overflow_warning (("assuming signed overflow does not occur "
11090 "when simplifying comparison of "
11091 "absolute value and zero"),
11092 WARN_STRICT_OVERFLOW_CONDITIONAL);
11093 return omit_one_operand_loc (loc, type,
11094 constant_boolean_node (true, type),
11095 arg0);
11098 /* Convert ABS_EXPR<x> < 0 to false. */
11099 strict_overflow_p = false;
11100 if (code == LT_EXPR
11101 && (integer_zerop (arg1) || real_zerop (arg1))
11102 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
11104 if (strict_overflow_p)
11105 fold_overflow_warning (("assuming signed overflow does not occur "
11106 "when simplifying comparison of "
11107 "absolute value and zero"),
11108 WARN_STRICT_OVERFLOW_CONDITIONAL);
11109 return omit_one_operand_loc (loc, type,
11110 constant_boolean_node (false, type),
11111 arg0);
11114 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
11115 and similarly for >= into !=. */
11116 if ((code == LT_EXPR || code == GE_EXPR)
11117 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11118 && TREE_CODE (arg1) == LSHIFT_EXPR
11119 && integer_onep (TREE_OPERAND (arg1, 0)))
11120 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11121 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11122 TREE_OPERAND (arg1, 1)),
11123 build_zero_cst (TREE_TYPE (arg0)));
11125 /* Similarly for X < (cast) (1 << Y). But cast can't be narrowing,
11126 otherwise Y might be >= # of bits in X's type and thus e.g.
11127 (unsigned char) (1 << Y) for Y 15 might be 0.
11128 If the cast is widening, then 1 << Y should have unsigned type,
11129 otherwise if Y is number of bits in the signed shift type minus 1,
11130 we can't optimize this. E.g. (unsigned long long) (1 << Y) for Y
11131 31 might be 0xffffffff80000000. */
11132 if ((code == LT_EXPR || code == GE_EXPR)
11133 && TYPE_UNSIGNED (TREE_TYPE (arg0))
11134 && CONVERT_EXPR_P (arg1)
11135 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
11136 && (element_precision (TREE_TYPE (arg1))
11137 >= element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0))))
11138 && (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))
11139 || (element_precision (TREE_TYPE (arg1))
11140 == element_precision (TREE_TYPE (TREE_OPERAND (arg1, 0)))))
11141 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
11143 tem = build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
11144 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1));
11145 return build2_loc (loc, code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
11146 fold_convert_loc (loc, TREE_TYPE (arg0), tem),
11147 build_zero_cst (TREE_TYPE (arg0)));
11150 return NULL_TREE;
11152 case UNORDERED_EXPR:
11153 case ORDERED_EXPR:
11154 case UNLT_EXPR:
11155 case UNLE_EXPR:
11156 case UNGT_EXPR:
11157 case UNGE_EXPR:
11158 case UNEQ_EXPR:
11159 case LTGT_EXPR:
11160 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
11162 tree targ0 = strip_float_extensions (arg0);
11163 tree targ1 = strip_float_extensions (arg1);
11164 tree newtype = TREE_TYPE (targ0);
11166 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
11167 newtype = TREE_TYPE (targ1);
11169 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
11170 return fold_build2_loc (loc, code, type,
11171 fold_convert_loc (loc, newtype, targ0),
11172 fold_convert_loc (loc, newtype, targ1));
11175 return NULL_TREE;
11177 case COMPOUND_EXPR:
11178 /* When pedantic, a compound expression can be neither an lvalue
11179 nor an integer constant expression. */
11180 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
11181 return NULL_TREE;
11182 /* Don't let (0, 0) be null pointer constant. */
11183 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
11184 : fold_convert_loc (loc, type, arg1);
11185 return pedantic_non_lvalue_loc (loc, tem);
11187 case ASSERT_EXPR:
11188 /* An ASSERT_EXPR should never be passed to fold_binary. */
11189 gcc_unreachable ();
11191 default:
11192 return NULL_TREE;
11193 } /* switch (code) */
11196 /* For constants M and N, if M == (1LL << cst) - 1 && (N & M) == M,
11197 ((A & N) + B) & M -> (A + B) & M
11198 Similarly if (N & M) == 0,
11199 ((A | N) + B) & M -> (A + B) & M
11200 and for - instead of + (or unary - instead of +)
11201 and/or ^ instead of |.
11202 If B is constant and (B & M) == 0, fold into A & M.
11204 This function is a helper for match.pd patterns. Return non-NULL
11205 type in which the simplified operation should be performed only
11206 if any optimization is possible.
11208 ARG1 is M above, ARG00 is left operand of +/-, if CODE00 is BIT_*_EXPR,
11209 then ARG00{0,1} are operands of that bitop, otherwise CODE00 is ERROR_MARK.
11210 Similarly for ARG01, CODE01 and ARG01{0,1}, just for the right operand of
11211 +/-. */
11212 tree
11213 fold_bit_and_mask (tree type, tree arg1, enum tree_code code,
11214 tree arg00, enum tree_code code00, tree arg000, tree arg001,
11215 tree arg01, enum tree_code code01, tree arg010, tree arg011,
11216 tree *pmop)
11218 gcc_assert (TREE_CODE (arg1) == INTEGER_CST);
11219 gcc_assert (code == PLUS_EXPR || code == MINUS_EXPR || code == NEGATE_EXPR);
11220 wi::tree_to_wide_ref cst1 = wi::to_wide (arg1);
11221 if (~cst1 == 0
11222 || (cst1 & (cst1 + 1)) != 0
11223 || !INTEGRAL_TYPE_P (type)
11224 || (!TYPE_OVERFLOW_WRAPS (type)
11225 && TREE_CODE (type) != INTEGER_TYPE)
11226 || (wi::max_value (type) & cst1) != cst1)
11227 return NULL_TREE;
11229 enum tree_code codes[2] = { code00, code01 };
11230 tree arg0xx[4] = { arg000, arg001, arg010, arg011 };
11231 int which = 0;
11232 wide_int cst0;
11234 /* Now we know that arg0 is (C + D) or (C - D) or -C and
11235 arg1 (M) is == (1LL << cst) - 1.
11236 Store C into PMOP[0] and D into PMOP[1]. */
11237 pmop[0] = arg00;
11238 pmop[1] = arg01;
11239 which = code != NEGATE_EXPR;
11241 for (; which >= 0; which--)
11242 switch (codes[which])
11244 case BIT_AND_EXPR:
11245 case BIT_IOR_EXPR:
11246 case BIT_XOR_EXPR:
11247 gcc_assert (TREE_CODE (arg0xx[2 * which + 1]) == INTEGER_CST);
11248 cst0 = wi::to_wide (arg0xx[2 * which + 1]) & cst1;
11249 if (codes[which] == BIT_AND_EXPR)
11251 if (cst0 != cst1)
11252 break;
11254 else if (cst0 != 0)
11255 break;
11256 /* If C or D is of the form (A & N) where
11257 (N & M) == M, or of the form (A | N) or
11258 (A ^ N) where (N & M) == 0, replace it with A. */
11259 pmop[which] = arg0xx[2 * which];
11260 break;
11261 case ERROR_MARK:
11262 if (TREE_CODE (pmop[which]) != INTEGER_CST)
11263 break;
11264 /* If C or D is a N where (N & M) == 0, it can be
11265 omitted (replaced with 0). */
11266 if ((code == PLUS_EXPR
11267 || (code == MINUS_EXPR && which == 0))
11268 && (cst1 & wi::to_wide (pmop[which])) == 0)
11269 pmop[which] = build_int_cst (type, 0);
11270 /* Similarly, with C - N where (-N & M) == 0. */
11271 if (code == MINUS_EXPR
11272 && which == 1
11273 && (cst1 & -wi::to_wide (pmop[which])) == 0)
11274 pmop[which] = build_int_cst (type, 0);
11275 break;
11276 default:
11277 gcc_unreachable ();
11280 /* Only build anything new if we optimized one or both arguments above. */
11281 if (pmop[0] == arg00 && pmop[1] == arg01)
11282 return NULL_TREE;
11284 if (TYPE_OVERFLOW_WRAPS (type))
11285 return type;
11286 else
11287 return unsigned_type_for (type);
11290 /* Used by contains_label_[p1]. */
11292 struct contains_label_data
11294 hash_set<tree> *pset;
11295 bool inside_switch_p;
11298 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
11299 a LABEL_EXPR or CASE_LABEL_EXPR not inside of another SWITCH_EXPR; otherwise
11300 return NULL_TREE. Do not check the subtrees of GOTO_EXPR. */
11302 static tree
11303 contains_label_1 (tree *tp, int *walk_subtrees, void *data)
11305 contains_label_data *d = (contains_label_data *) data;
11306 switch (TREE_CODE (*tp))
11308 case LABEL_EXPR:
11309 return *tp;
11311 case CASE_LABEL_EXPR:
11312 if (!d->inside_switch_p)
11313 return *tp;
11314 return NULL_TREE;
11316 case SWITCH_EXPR:
11317 if (!d->inside_switch_p)
11319 if (walk_tree (&SWITCH_COND (*tp), contains_label_1, data, d->pset))
11320 return *tp;
11321 d->inside_switch_p = true;
11322 if (walk_tree (&SWITCH_BODY (*tp), contains_label_1, data, d->pset))
11323 return *tp;
11324 d->inside_switch_p = false;
11325 *walk_subtrees = 0;
11327 return NULL_TREE;
11329 case GOTO_EXPR:
11330 *walk_subtrees = 0;
11331 return NULL_TREE;
11333 default:
11334 return NULL_TREE;
11338 /* Return whether the sub-tree ST contains a label which is accessible from
11339 outside the sub-tree. */
11341 static bool
11342 contains_label_p (tree st)
11344 hash_set<tree> pset;
11345 contains_label_data data = { &pset, false };
11346 return walk_tree (&st, contains_label_1, &data, &pset) != NULL_TREE;
11349 /* Fold a ternary expression of code CODE and type TYPE with operands
11350 OP0, OP1, and OP2. Return the folded expression if folding is
11351 successful. Otherwise, return NULL_TREE. */
11353 tree
11354 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
11355 tree op0, tree op1, tree op2)
11357 tree tem;
11358 tree arg0 = NULL_TREE, arg1 = NULL_TREE, arg2 = NULL_TREE;
11359 enum tree_code_class kind = TREE_CODE_CLASS (code);
11361 gcc_assert (IS_EXPR_CODE_CLASS (kind)
11362 && TREE_CODE_LENGTH (code) == 3);
11364 /* If this is a commutative operation, and OP0 is a constant, move it
11365 to OP1 to reduce the number of tests below. */
11366 if (commutative_ternary_tree_code (code)
11367 && tree_swap_operands_p (op0, op1))
11368 return fold_build3_loc (loc, code, type, op1, op0, op2);
11370 tem = generic_simplify (loc, code, type, op0, op1, op2);
11371 if (tem)
11372 return tem;
11374 /* Strip any conversions that don't change the mode. This is safe
11375 for every expression, except for a comparison expression because
11376 its signedness is derived from its operands. So, in the latter
11377 case, only strip conversions that don't change the signedness.
11379 Note that this is done as an internal manipulation within the
11380 constant folder, in order to find the simplest representation of
11381 the arguments so that their form can be studied. In any cases,
11382 the appropriate type conversions should be put back in the tree
11383 that will get out of the constant folder. */
11384 if (op0)
11386 arg0 = op0;
11387 STRIP_NOPS (arg0);
11390 if (op1)
11392 arg1 = op1;
11393 STRIP_NOPS (arg1);
11396 if (op2)
11398 arg2 = op2;
11399 STRIP_NOPS (arg2);
11402 switch (code)
11404 case COMPONENT_REF:
11405 if (TREE_CODE (arg0) == CONSTRUCTOR
11406 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
11408 unsigned HOST_WIDE_INT idx;
11409 tree field, value;
11410 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
11411 if (field == arg1)
11412 return value;
11414 return NULL_TREE;
11416 case COND_EXPR:
11417 case VEC_COND_EXPR:
11418 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
11419 so all simple results must be passed through pedantic_non_lvalue. */
11420 if (TREE_CODE (arg0) == INTEGER_CST)
11422 tree unused_op = integer_zerop (arg0) ? op1 : op2;
11423 tem = integer_zerop (arg0) ? op2 : op1;
11424 /* Only optimize constant conditions when the selected branch
11425 has the same type as the COND_EXPR. This avoids optimizing
11426 away "c ? x : throw", where the throw has a void type.
11427 Avoid throwing away that operand which contains label. */
11428 if ((!TREE_SIDE_EFFECTS (unused_op)
11429 || !contains_label_p (unused_op))
11430 && (! VOID_TYPE_P (TREE_TYPE (tem))
11431 || VOID_TYPE_P (type)))
11432 return pedantic_non_lvalue_loc (loc, tem);
11433 return NULL_TREE;
11435 else if (TREE_CODE (arg0) == VECTOR_CST)
11437 unsigned HOST_WIDE_INT nelts;
11438 if ((TREE_CODE (arg1) == VECTOR_CST
11439 || TREE_CODE (arg1) == CONSTRUCTOR)
11440 && (TREE_CODE (arg2) == VECTOR_CST
11441 || TREE_CODE (arg2) == CONSTRUCTOR)
11442 && TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
11444 vec_perm_builder sel (nelts, nelts, 1);
11445 for (unsigned int i = 0; i < nelts; i++)
11447 tree val = VECTOR_CST_ELT (arg0, i);
11448 if (integer_all_onesp (val))
11449 sel.quick_push (i);
11450 else if (integer_zerop (val))
11451 sel.quick_push (nelts + i);
11452 else /* Currently unreachable. */
11453 return NULL_TREE;
11455 vec_perm_indices indices (sel, 2, nelts);
11456 tree t = fold_vec_perm (type, arg1, arg2, indices);
11457 if (t != NULL_TREE)
11458 return t;
11462 /* If we have A op B ? A : C, we may be able to convert this to a
11463 simpler expression, depending on the operation and the values
11464 of B and C. Signed zeros prevent all of these transformations,
11465 for reasons given above each one.
11467 Also try swapping the arguments and inverting the conditional. */
11468 if (COMPARISON_CLASS_P (arg0)
11469 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op1)
11470 && !HONOR_SIGNED_ZEROS (element_mode (op1)))
11472 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
11473 if (tem)
11474 return tem;
11477 if (COMPARISON_CLASS_P (arg0)
11478 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), op2)
11479 && !HONOR_SIGNED_ZEROS (element_mode (op2)))
11481 location_t loc0 = expr_location_or (arg0, loc);
11482 tem = fold_invert_truthvalue (loc0, arg0);
11483 if (tem && COMPARISON_CLASS_P (tem))
11485 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
11486 if (tem)
11487 return tem;
11491 /* If the second operand is simpler than the third, swap them
11492 since that produces better jump optimization results. */
11493 if (truth_value_p (TREE_CODE (arg0))
11494 && tree_swap_operands_p (op1, op2))
11496 location_t loc0 = expr_location_or (arg0, loc);
11497 /* See if this can be inverted. If it can't, possibly because
11498 it was a floating-point inequality comparison, don't do
11499 anything. */
11500 tem = fold_invert_truthvalue (loc0, arg0);
11501 if (tem)
11502 return fold_build3_loc (loc, code, type, tem, op2, op1);
11505 /* Convert A ? 1 : 0 to simply A. */
11506 if ((code == VEC_COND_EXPR ? integer_all_onesp (op1)
11507 : (integer_onep (op1)
11508 && !VECTOR_TYPE_P (type)))
11509 && integer_zerop (op2)
11510 /* If we try to convert OP0 to our type, the
11511 call to fold will try to move the conversion inside
11512 a COND, which will recurse. In that case, the COND_EXPR
11513 is probably the best choice, so leave it alone. */
11514 && type == TREE_TYPE (arg0))
11515 return pedantic_non_lvalue_loc (loc, arg0);
11517 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
11518 over COND_EXPR in cases such as floating point comparisons. */
11519 if (integer_zerop (op1)
11520 && code == COND_EXPR
11521 && integer_onep (op2)
11522 && !VECTOR_TYPE_P (type)
11523 && truth_value_p (TREE_CODE (arg0)))
11524 return pedantic_non_lvalue_loc (loc,
11525 fold_convert_loc (loc, type,
11526 invert_truthvalue_loc (loc,
11527 arg0)));
11529 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
11530 if (TREE_CODE (arg0) == LT_EXPR
11531 && integer_zerop (TREE_OPERAND (arg0, 1))
11532 && integer_zerop (op2)
11533 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
11535 /* sign_bit_p looks through both zero and sign extensions,
11536 but for this optimization only sign extensions are
11537 usable. */
11538 tree tem2 = TREE_OPERAND (arg0, 0);
11539 while (tem != tem2)
11541 if (TREE_CODE (tem2) != NOP_EXPR
11542 || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (tem2, 0))))
11544 tem = NULL_TREE;
11545 break;
11547 tem2 = TREE_OPERAND (tem2, 0);
11549 /* sign_bit_p only checks ARG1 bits within A's precision.
11550 If <sign bit of A> has wider type than A, bits outside
11551 of A's precision in <sign bit of A> need to be checked.
11552 If they are all 0, this optimization needs to be done
11553 in unsigned A's type, if they are all 1 in signed A's type,
11554 otherwise this can't be done. */
11555 if (tem
11556 && TYPE_PRECISION (TREE_TYPE (tem))
11557 < TYPE_PRECISION (TREE_TYPE (arg1))
11558 && TYPE_PRECISION (TREE_TYPE (tem))
11559 < TYPE_PRECISION (type))
11561 int inner_width, outer_width;
11562 tree tem_type;
11564 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
11565 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
11566 if (outer_width > TYPE_PRECISION (type))
11567 outer_width = TYPE_PRECISION (type);
11569 wide_int mask = wi::shifted_mask
11570 (inner_width, outer_width - inner_width, false,
11571 TYPE_PRECISION (TREE_TYPE (arg1)));
11573 wide_int common = mask & wi::to_wide (arg1);
11574 if (common == mask)
11576 tem_type = signed_type_for (TREE_TYPE (tem));
11577 tem = fold_convert_loc (loc, tem_type, tem);
11579 else if (common == 0)
11581 tem_type = unsigned_type_for (TREE_TYPE (tem));
11582 tem = fold_convert_loc (loc, tem_type, tem);
11584 else
11585 tem = NULL;
11588 if (tem)
11589 return
11590 fold_convert_loc (loc, type,
11591 fold_build2_loc (loc, BIT_AND_EXPR,
11592 TREE_TYPE (tem), tem,
11593 fold_convert_loc (loc,
11594 TREE_TYPE (tem),
11595 arg1)));
11598 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
11599 already handled above. */
11600 if (TREE_CODE (arg0) == BIT_AND_EXPR
11601 && integer_onep (TREE_OPERAND (arg0, 1))
11602 && integer_zerop (op2)
11603 && integer_pow2p (arg1))
11605 tree tem = TREE_OPERAND (arg0, 0);
11606 STRIP_NOPS (tem);
11607 if (TREE_CODE (tem) == RSHIFT_EXPR
11608 && tree_fits_uhwi_p (TREE_OPERAND (tem, 1))
11609 && (unsigned HOST_WIDE_INT) tree_log2 (arg1)
11610 == tree_to_uhwi (TREE_OPERAND (tem, 1)))
11611 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11612 fold_convert_loc (loc, type,
11613 TREE_OPERAND (tem, 0)),
11614 op1);
11617 /* A & N ? N : 0 is simply A & N if N is a power of two. This
11618 is probably obsolete because the first operand should be a
11619 truth value (that's why we have the two cases above), but let's
11620 leave it in until we can confirm this for all front-ends. */
11621 if (integer_zerop (op2)
11622 && TREE_CODE (arg0) == NE_EXPR
11623 && integer_zerop (TREE_OPERAND (arg0, 1))
11624 && integer_pow2p (arg1)
11625 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11626 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
11627 arg1, OEP_ONLY_CONST)
11628 /* operand_equal_p compares just value, not precision, so e.g.
11629 arg1 could be 8-bit -128 and be power of two, but BIT_AND_EXPR
11630 second operand 32-bit -128, which is not a power of two (or vice
11631 versa. */
11632 && integer_pow2p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)))
11633 return pedantic_non_lvalue_loc (loc,
11634 fold_convert_loc (loc, type,
11635 TREE_OPERAND (arg0,
11636 0)));
11638 /* Disable the transformations below for vectors, since
11639 fold_binary_op_with_conditional_arg may undo them immediately,
11640 yielding an infinite loop. */
11641 if (code == VEC_COND_EXPR)
11642 return NULL_TREE;
11644 /* Convert A ? B : 0 into A && B if A and B are truth values. */
11645 if (integer_zerop (op2)
11646 && truth_value_p (TREE_CODE (arg0))
11647 && truth_value_p (TREE_CODE (arg1))
11648 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11649 return fold_build2_loc (loc, code == VEC_COND_EXPR ? BIT_AND_EXPR
11650 : TRUTH_ANDIF_EXPR,
11651 type, fold_convert_loc (loc, type, arg0), op1);
11653 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
11654 if (code == VEC_COND_EXPR ? integer_all_onesp (op2) : integer_onep (op2)
11655 && truth_value_p (TREE_CODE (arg0))
11656 && truth_value_p (TREE_CODE (arg1))
11657 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11659 location_t loc0 = expr_location_or (arg0, loc);
11660 /* Only perform transformation if ARG0 is easily inverted. */
11661 tem = fold_invert_truthvalue (loc0, arg0);
11662 if (tem)
11663 return fold_build2_loc (loc, code == VEC_COND_EXPR
11664 ? BIT_IOR_EXPR
11665 : TRUTH_ORIF_EXPR,
11666 type, fold_convert_loc (loc, type, tem),
11667 op1);
11670 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
11671 if (integer_zerop (arg1)
11672 && truth_value_p (TREE_CODE (arg0))
11673 && truth_value_p (TREE_CODE (op2))
11674 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11676 location_t loc0 = expr_location_or (arg0, loc);
11677 /* Only perform transformation if ARG0 is easily inverted. */
11678 tem = fold_invert_truthvalue (loc0, arg0);
11679 if (tem)
11680 return fold_build2_loc (loc, code == VEC_COND_EXPR
11681 ? BIT_AND_EXPR : TRUTH_ANDIF_EXPR,
11682 type, fold_convert_loc (loc, type, tem),
11683 op2);
11686 /* Convert A ? 1 : B into A || B if A and B are truth values. */
11687 if (code == VEC_COND_EXPR ? integer_all_onesp (arg1) : integer_onep (arg1)
11688 && truth_value_p (TREE_CODE (arg0))
11689 && truth_value_p (TREE_CODE (op2))
11690 && (code == VEC_COND_EXPR || !VECTOR_TYPE_P (type)))
11691 return fold_build2_loc (loc, code == VEC_COND_EXPR
11692 ? BIT_IOR_EXPR : TRUTH_ORIF_EXPR,
11693 type, fold_convert_loc (loc, type, arg0), op2);
11695 return NULL_TREE;
11697 case CALL_EXPR:
11698 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
11699 of fold_ternary on them. */
11700 gcc_unreachable ();
11702 case BIT_FIELD_REF:
11703 if (TREE_CODE (arg0) == VECTOR_CST
11704 && (type == TREE_TYPE (TREE_TYPE (arg0))
11705 || (VECTOR_TYPE_P (type)
11706 && TREE_TYPE (type) == TREE_TYPE (TREE_TYPE (arg0))))
11707 && tree_fits_uhwi_p (op1)
11708 && tree_fits_uhwi_p (op2))
11710 tree eltype = TREE_TYPE (TREE_TYPE (arg0));
11711 unsigned HOST_WIDE_INT width = tree_to_uhwi (TYPE_SIZE (eltype));
11712 unsigned HOST_WIDE_INT n = tree_to_uhwi (arg1);
11713 unsigned HOST_WIDE_INT idx = tree_to_uhwi (op2);
11715 if (n != 0
11716 && (idx % width) == 0
11717 && (n % width) == 0
11718 && known_le ((idx + n) / width,
11719 TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0))))
11721 idx = idx / width;
11722 n = n / width;
11724 if (TREE_CODE (arg0) == VECTOR_CST)
11726 if (n == 1)
11728 tem = VECTOR_CST_ELT (arg0, idx);
11729 if (VECTOR_TYPE_P (type))
11730 tem = fold_build1 (VIEW_CONVERT_EXPR, type, tem);
11731 return tem;
11734 tree_vector_builder vals (type, n, 1);
11735 for (unsigned i = 0; i < n; ++i)
11736 vals.quick_push (VECTOR_CST_ELT (arg0, idx + i));
11737 return vals.build ();
11742 /* On constants we can use native encode/interpret to constant
11743 fold (nearly) all BIT_FIELD_REFs. */
11744 if (CONSTANT_CLASS_P (arg0)
11745 && can_native_interpret_type_p (type)
11746 && BITS_PER_UNIT == 8
11747 && tree_fits_uhwi_p (op1)
11748 && tree_fits_uhwi_p (op2))
11750 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11751 unsigned HOST_WIDE_INT bitsize = tree_to_uhwi (op1);
11752 /* Limit us to a reasonable amount of work. To relax the
11753 other limitations we need bit-shifting of the buffer
11754 and rounding up the size. */
11755 if (bitpos % BITS_PER_UNIT == 0
11756 && bitsize % BITS_PER_UNIT == 0
11757 && bitsize <= MAX_BITSIZE_MODE_ANY_MODE)
11759 unsigned char b[MAX_BITSIZE_MODE_ANY_MODE / BITS_PER_UNIT];
11760 unsigned HOST_WIDE_INT len
11761 = native_encode_expr (arg0, b, bitsize / BITS_PER_UNIT,
11762 bitpos / BITS_PER_UNIT);
11763 if (len > 0
11764 && len * BITS_PER_UNIT >= bitsize)
11766 tree v = native_interpret_expr (type, b,
11767 bitsize / BITS_PER_UNIT);
11768 if (v)
11769 return v;
11774 return NULL_TREE;
11776 case VEC_PERM_EXPR:
11777 /* Perform constant folding of BIT_INSERT_EXPR. */
11778 if (TREE_CODE (arg2) == VECTOR_CST
11779 && TREE_CODE (op0) == VECTOR_CST
11780 && TREE_CODE (op1) == VECTOR_CST)
11782 /* Build a vector of integers from the tree mask. */
11783 vec_perm_builder builder;
11784 if (!tree_to_vec_perm_builder (&builder, arg2))
11785 return NULL_TREE;
11787 /* Create a vec_perm_indices for the integer vector. */
11788 poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (type);
11789 bool single_arg = (op0 == op1);
11790 vec_perm_indices sel (builder, single_arg ? 1 : 2, nelts);
11791 return fold_vec_perm (type, op0, op1, sel);
11793 return NULL_TREE;
11795 case BIT_INSERT_EXPR:
11796 /* Perform (partial) constant folding of BIT_INSERT_EXPR. */
11797 if (TREE_CODE (arg0) == INTEGER_CST
11798 && TREE_CODE (arg1) == INTEGER_CST)
11800 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11801 unsigned bitsize = TYPE_PRECISION (TREE_TYPE (arg1));
11802 wide_int tem = (wi::to_wide (arg0)
11803 & wi::shifted_mask (bitpos, bitsize, true,
11804 TYPE_PRECISION (type)));
11805 wide_int tem2
11806 = wi::lshift (wi::zext (wi::to_wide (arg1, TYPE_PRECISION (type)),
11807 bitsize), bitpos);
11808 return wide_int_to_tree (type, wi::bit_or (tem, tem2));
11810 else if (TREE_CODE (arg0) == VECTOR_CST
11811 && CONSTANT_CLASS_P (arg1)
11812 && types_compatible_p (TREE_TYPE (TREE_TYPE (arg0)),
11813 TREE_TYPE (arg1)))
11815 unsigned HOST_WIDE_INT bitpos = tree_to_uhwi (op2);
11816 unsigned HOST_WIDE_INT elsize
11817 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (arg1)));
11818 if (bitpos % elsize == 0)
11820 unsigned k = bitpos / elsize;
11821 unsigned HOST_WIDE_INT nelts;
11822 if (operand_equal_p (VECTOR_CST_ELT (arg0, k), arg1, 0))
11823 return arg0;
11824 else if (VECTOR_CST_NELTS (arg0).is_constant (&nelts))
11826 tree_vector_builder elts (type, nelts, 1);
11827 elts.quick_grow (nelts);
11828 for (unsigned HOST_WIDE_INT i = 0; i < nelts; ++i)
11829 elts[i] = (i == k ? arg1 : VECTOR_CST_ELT (arg0, i));
11830 return elts.build ();
11834 return NULL_TREE;
11836 default:
11837 return NULL_TREE;
11838 } /* switch (code) */
11841 /* Gets the element ACCESS_INDEX from CTOR, which must be a CONSTRUCTOR
11842 of an array (or vector). *CTOR_IDX if non-NULL is updated with the
11843 constructor element index of the value returned. If the element is
11844 not found NULL_TREE is returned and *CTOR_IDX is updated to
11845 the index of the element after the ACCESS_INDEX position (which
11846 may be outside of the CTOR array). */
11848 tree
11849 get_array_ctor_element_at_index (tree ctor, offset_int access_index,
11850 unsigned *ctor_idx)
11852 tree index_type = NULL_TREE;
11853 signop index_sgn = UNSIGNED;
11854 offset_int low_bound = 0;
11856 if (TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE)
11858 tree domain_type = TYPE_DOMAIN (TREE_TYPE (ctor));
11859 if (domain_type && TYPE_MIN_VALUE (domain_type))
11861 /* Static constructors for variably sized objects makes no sense. */
11862 gcc_assert (TREE_CODE (TYPE_MIN_VALUE (domain_type)) == INTEGER_CST);
11863 index_type = TREE_TYPE (TYPE_MIN_VALUE (domain_type));
11864 /* ??? When it is obvious that the range is signed, treat it so. */
11865 if (TYPE_UNSIGNED (index_type)
11866 && TYPE_MAX_VALUE (domain_type)
11867 && tree_int_cst_lt (TYPE_MAX_VALUE (domain_type),
11868 TYPE_MIN_VALUE (domain_type)))
11870 index_sgn = SIGNED;
11871 low_bound
11872 = offset_int::from (wi::to_wide (TYPE_MIN_VALUE (domain_type)),
11873 SIGNED);
11875 else
11877 index_sgn = TYPE_SIGN (index_type);
11878 low_bound = wi::to_offset (TYPE_MIN_VALUE (domain_type));
11883 if (index_type)
11884 access_index = wi::ext (access_index, TYPE_PRECISION (index_type),
11885 index_sgn);
11887 offset_int index = low_bound;
11888 if (index_type)
11889 index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
11891 offset_int max_index = index;
11892 unsigned cnt;
11893 tree cfield, cval;
11894 bool first_p = true;
11896 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), cnt, cfield, cval)
11898 /* Array constructor might explicitly set index, or specify a range,
11899 or leave index NULL meaning that it is next index after previous
11900 one. */
11901 if (cfield)
11903 if (TREE_CODE (cfield) == INTEGER_CST)
11904 max_index = index
11905 = offset_int::from (wi::to_wide (cfield), index_sgn);
11906 else
11908 gcc_assert (TREE_CODE (cfield) == RANGE_EXPR);
11909 index = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 0)),
11910 index_sgn);
11911 max_index
11912 = offset_int::from (wi::to_wide (TREE_OPERAND (cfield, 1)),
11913 index_sgn);
11914 gcc_checking_assert (wi::le_p (index, max_index, index_sgn));
11917 else if (!first_p)
11919 index = max_index + 1;
11920 if (index_type)
11921 index = wi::ext (index, TYPE_PRECISION (index_type), index_sgn);
11922 gcc_checking_assert (wi::gt_p (index, max_index, index_sgn));
11923 max_index = index;
11925 else
11926 first_p = false;
11928 /* Do we have match? */
11929 if (wi::cmp (access_index, index, index_sgn) >= 0)
11931 if (wi::cmp (access_index, max_index, index_sgn) <= 0)
11933 if (ctor_idx)
11934 *ctor_idx = cnt;
11935 return cval;
11938 else if (in_gimple_form)
11939 /* We're past the element we search for. Note during parsing
11940 the elements might not be sorted.
11941 ??? We should use a binary search and a flag on the
11942 CONSTRUCTOR as to whether elements are sorted in declaration
11943 order. */
11944 break;
11946 if (ctor_idx)
11947 *ctor_idx = cnt;
11948 return NULL_TREE;
11951 /* Perform constant folding and related simplification of EXPR.
11952 The related simplifications include x*1 => x, x*0 => 0, etc.,
11953 and application of the associative law.
11954 NOP_EXPR conversions may be removed freely (as long as we
11955 are careful not to change the type of the overall expression).
11956 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
11957 but we can constant-fold them if they have constant operands. */
11959 #ifdef ENABLE_FOLD_CHECKING
11960 # define fold(x) fold_1 (x)
11961 static tree fold_1 (tree);
11962 static
11963 #endif
11964 tree
11965 fold (tree expr)
11967 const tree t = expr;
11968 enum tree_code code = TREE_CODE (t);
11969 enum tree_code_class kind = TREE_CODE_CLASS (code);
11970 tree tem;
11971 location_t loc = EXPR_LOCATION (expr);
11973 /* Return right away if a constant. */
11974 if (kind == tcc_constant)
11975 return t;
11977 /* CALL_EXPR-like objects with variable numbers of operands are
11978 treated specially. */
11979 if (kind == tcc_vl_exp)
11981 if (code == CALL_EXPR)
11983 tem = fold_call_expr (loc, expr, false);
11984 return tem ? tem : expr;
11986 return expr;
11989 if (IS_EXPR_CODE_CLASS (kind))
11991 tree type = TREE_TYPE (t);
11992 tree op0, op1, op2;
11994 switch (TREE_CODE_LENGTH (code))
11996 case 1:
11997 op0 = TREE_OPERAND (t, 0);
11998 tem = fold_unary_loc (loc, code, type, op0);
11999 return tem ? tem : expr;
12000 case 2:
12001 op0 = TREE_OPERAND (t, 0);
12002 op1 = TREE_OPERAND (t, 1);
12003 tem = fold_binary_loc (loc, code, type, op0, op1);
12004 return tem ? tem : expr;
12005 case 3:
12006 op0 = TREE_OPERAND (t, 0);
12007 op1 = TREE_OPERAND (t, 1);
12008 op2 = TREE_OPERAND (t, 2);
12009 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12010 return tem ? tem : expr;
12011 default:
12012 break;
12016 switch (code)
12018 case ARRAY_REF:
12020 tree op0 = TREE_OPERAND (t, 0);
12021 tree op1 = TREE_OPERAND (t, 1);
12023 if (TREE_CODE (op1) == INTEGER_CST
12024 && TREE_CODE (op0) == CONSTRUCTOR
12025 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
12027 tree val = get_array_ctor_element_at_index (op0,
12028 wi::to_offset (op1));
12029 if (val)
12030 return val;
12033 return t;
12036 /* Return a VECTOR_CST if possible. */
12037 case CONSTRUCTOR:
12039 tree type = TREE_TYPE (t);
12040 if (TREE_CODE (type) != VECTOR_TYPE)
12041 return t;
12043 unsigned i;
12044 tree val;
12045 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
12046 if (! CONSTANT_CLASS_P (val))
12047 return t;
12049 return build_vector_from_ctor (type, CONSTRUCTOR_ELTS (t));
12052 case CONST_DECL:
12053 return fold (DECL_INITIAL (t));
12055 default:
12056 return t;
12057 } /* switch (code) */
12060 #ifdef ENABLE_FOLD_CHECKING
12061 #undef fold
12063 static void fold_checksum_tree (const_tree, struct md5_ctx *,
12064 hash_table<nofree_ptr_hash<const tree_node> > *);
12065 static void fold_check_failed (const_tree, const_tree);
12066 void print_fold_checksum (const_tree);
12068 /* When --enable-checking=fold, compute a digest of expr before
12069 and after actual fold call to see if fold did not accidentally
12070 change original expr. */
12072 tree
12073 fold (tree expr)
12075 tree ret;
12076 struct md5_ctx ctx;
12077 unsigned char checksum_before[16], checksum_after[16];
12078 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12080 md5_init_ctx (&ctx);
12081 fold_checksum_tree (expr, &ctx, &ht);
12082 md5_finish_ctx (&ctx, checksum_before);
12083 ht.empty ();
12085 ret = fold_1 (expr);
12087 md5_init_ctx (&ctx);
12088 fold_checksum_tree (expr, &ctx, &ht);
12089 md5_finish_ctx (&ctx, checksum_after);
12091 if (memcmp (checksum_before, checksum_after, 16))
12092 fold_check_failed (expr, ret);
12094 return ret;
12097 void
12098 print_fold_checksum (const_tree expr)
12100 struct md5_ctx ctx;
12101 unsigned char checksum[16], cnt;
12102 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12104 md5_init_ctx (&ctx);
12105 fold_checksum_tree (expr, &ctx, &ht);
12106 md5_finish_ctx (&ctx, checksum);
12107 for (cnt = 0; cnt < 16; ++cnt)
12108 fprintf (stderr, "%02x", checksum[cnt]);
12109 putc ('\n', stderr);
12112 static void
12113 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
12115 internal_error ("fold check: original tree changed by fold");
12118 static void
12119 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx,
12120 hash_table<nofree_ptr_hash <const tree_node> > *ht)
12122 const tree_node **slot;
12123 enum tree_code code;
12124 union tree_node *buf;
12125 int i, len;
12127 recursive_label:
12128 if (expr == NULL)
12129 return;
12130 slot = ht->find_slot (expr, INSERT);
12131 if (*slot != NULL)
12132 return;
12133 *slot = expr;
12134 code = TREE_CODE (expr);
12135 if (TREE_CODE_CLASS (code) == tcc_declaration
12136 && HAS_DECL_ASSEMBLER_NAME_P (expr))
12138 /* Allow DECL_ASSEMBLER_NAME and symtab_node to be modified. */
12139 size_t sz = tree_size (expr);
12140 buf = XALLOCAVAR (union tree_node, sz);
12141 memcpy ((char *) buf, expr, sz);
12142 SET_DECL_ASSEMBLER_NAME ((tree) buf, NULL);
12143 buf->decl_with_vis.symtab_node = NULL;
12144 buf->base.nowarning_flag = 0;
12145 expr = (tree) buf;
12147 else if (TREE_CODE_CLASS (code) == tcc_type
12148 && (TYPE_POINTER_TO (expr)
12149 || TYPE_REFERENCE_TO (expr)
12150 || TYPE_CACHED_VALUES_P (expr)
12151 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
12152 || TYPE_NEXT_VARIANT (expr)
12153 || TYPE_ALIAS_SET_KNOWN_P (expr)))
12155 /* Allow these fields to be modified. */
12156 tree tmp;
12157 size_t sz = tree_size (expr);
12158 buf = XALLOCAVAR (union tree_node, sz);
12159 memcpy ((char *) buf, expr, sz);
12160 expr = tmp = (tree) buf;
12161 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
12162 TYPE_POINTER_TO (tmp) = NULL;
12163 TYPE_REFERENCE_TO (tmp) = NULL;
12164 TYPE_NEXT_VARIANT (tmp) = NULL;
12165 TYPE_ALIAS_SET (tmp) = -1;
12166 if (TYPE_CACHED_VALUES_P (tmp))
12168 TYPE_CACHED_VALUES_P (tmp) = 0;
12169 TYPE_CACHED_VALUES (tmp) = NULL;
12172 else if (TREE_NO_WARNING (expr) && (DECL_P (expr) || EXPR_P (expr)))
12174 /* Allow TREE_NO_WARNING to be set. Perhaps we shouldn't allow that
12175 and change builtins.c etc. instead - see PR89543. */
12176 size_t sz = tree_size (expr);
12177 buf = XALLOCAVAR (union tree_node, sz);
12178 memcpy ((char *) buf, expr, sz);
12179 buf->base.nowarning_flag = 0;
12180 expr = (tree) buf;
12182 md5_process_bytes (expr, tree_size (expr), ctx);
12183 if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
12184 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
12185 if (TREE_CODE_CLASS (code) != tcc_type
12186 && TREE_CODE_CLASS (code) != tcc_declaration
12187 && code != TREE_LIST
12188 && code != SSA_NAME
12189 && CODE_CONTAINS_STRUCT (code, TS_COMMON))
12190 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
12191 switch (TREE_CODE_CLASS (code))
12193 case tcc_constant:
12194 switch (code)
12196 case STRING_CST:
12197 md5_process_bytes (TREE_STRING_POINTER (expr),
12198 TREE_STRING_LENGTH (expr), ctx);
12199 break;
12200 case COMPLEX_CST:
12201 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
12202 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
12203 break;
12204 case VECTOR_CST:
12205 len = vector_cst_encoded_nelts (expr);
12206 for (i = 0; i < len; ++i)
12207 fold_checksum_tree (VECTOR_CST_ENCODED_ELT (expr, i), ctx, ht);
12208 break;
12209 default:
12210 break;
12212 break;
12213 case tcc_exceptional:
12214 switch (code)
12216 case TREE_LIST:
12217 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
12218 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
12219 expr = TREE_CHAIN (expr);
12220 goto recursive_label;
12221 break;
12222 case TREE_VEC:
12223 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
12224 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
12225 break;
12226 default:
12227 break;
12229 break;
12230 case tcc_expression:
12231 case tcc_reference:
12232 case tcc_comparison:
12233 case tcc_unary:
12234 case tcc_binary:
12235 case tcc_statement:
12236 case tcc_vl_exp:
12237 len = TREE_OPERAND_LENGTH (expr);
12238 for (i = 0; i < len; ++i)
12239 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
12240 break;
12241 case tcc_declaration:
12242 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
12243 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
12244 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
12246 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
12247 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
12248 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
12249 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
12250 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
12253 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
12255 if (TREE_CODE (expr) == FUNCTION_DECL)
12257 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
12258 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
12260 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
12262 break;
12263 case tcc_type:
12264 if (TREE_CODE (expr) == ENUMERAL_TYPE)
12265 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
12266 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
12267 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
12268 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
12269 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
12270 if (INTEGRAL_TYPE_P (expr)
12271 || SCALAR_FLOAT_TYPE_P (expr))
12273 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
12274 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
12276 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
12277 if (TREE_CODE (expr) == RECORD_TYPE
12278 || TREE_CODE (expr) == UNION_TYPE
12279 || TREE_CODE (expr) == QUAL_UNION_TYPE)
12280 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
12281 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
12282 break;
12283 default:
12284 break;
12288 /* Helper function for outputting the checksum of a tree T. When
12289 debugging with gdb, you can "define mynext" to be "next" followed
12290 by "call debug_fold_checksum (op0)", then just trace down till the
12291 outputs differ. */
12293 DEBUG_FUNCTION void
12294 debug_fold_checksum (const_tree t)
12296 int i;
12297 unsigned char checksum[16];
12298 struct md5_ctx ctx;
12299 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12301 md5_init_ctx (&ctx);
12302 fold_checksum_tree (t, &ctx, &ht);
12303 md5_finish_ctx (&ctx, checksum);
12304 ht.empty ();
12306 for (i = 0; i < 16; i++)
12307 fprintf (stderr, "%d ", checksum[i]);
12309 fprintf (stderr, "\n");
12312 #endif
12314 /* Fold a unary tree expression with code CODE of type TYPE with an
12315 operand OP0. LOC is the location of the resulting expression.
12316 Return a folded expression if successful. Otherwise, return a tree
12317 expression with code CODE of type TYPE with an operand OP0. */
12319 tree
12320 fold_build1_loc (location_t loc,
12321 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
12323 tree tem;
12324 #ifdef ENABLE_FOLD_CHECKING
12325 unsigned char checksum_before[16], checksum_after[16];
12326 struct md5_ctx ctx;
12327 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12329 md5_init_ctx (&ctx);
12330 fold_checksum_tree (op0, &ctx, &ht);
12331 md5_finish_ctx (&ctx, checksum_before);
12332 ht.empty ();
12333 #endif
12335 tem = fold_unary_loc (loc, code, type, op0);
12336 if (!tem)
12337 tem = build1_loc (loc, code, type, op0 PASS_MEM_STAT);
12339 #ifdef ENABLE_FOLD_CHECKING
12340 md5_init_ctx (&ctx);
12341 fold_checksum_tree (op0, &ctx, &ht);
12342 md5_finish_ctx (&ctx, checksum_after);
12344 if (memcmp (checksum_before, checksum_after, 16))
12345 fold_check_failed (op0, tem);
12346 #endif
12347 return tem;
12350 /* Fold a binary tree expression with code CODE of type TYPE with
12351 operands OP0 and OP1. LOC is the location of the resulting
12352 expression. Return a folded expression if successful. Otherwise,
12353 return a tree expression with code CODE of type TYPE with operands
12354 OP0 and OP1. */
12356 tree
12357 fold_build2_loc (location_t loc,
12358 enum tree_code code, tree type, tree op0, tree op1
12359 MEM_STAT_DECL)
12361 tree tem;
12362 #ifdef ENABLE_FOLD_CHECKING
12363 unsigned char checksum_before_op0[16],
12364 checksum_before_op1[16],
12365 checksum_after_op0[16],
12366 checksum_after_op1[16];
12367 struct md5_ctx ctx;
12368 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12370 md5_init_ctx (&ctx);
12371 fold_checksum_tree (op0, &ctx, &ht);
12372 md5_finish_ctx (&ctx, checksum_before_op0);
12373 ht.empty ();
12375 md5_init_ctx (&ctx);
12376 fold_checksum_tree (op1, &ctx, &ht);
12377 md5_finish_ctx (&ctx, checksum_before_op1);
12378 ht.empty ();
12379 #endif
12381 tem = fold_binary_loc (loc, code, type, op0, op1);
12382 if (!tem)
12383 tem = build2_loc (loc, code, type, op0, op1 PASS_MEM_STAT);
12385 #ifdef ENABLE_FOLD_CHECKING
12386 md5_init_ctx (&ctx);
12387 fold_checksum_tree (op0, &ctx, &ht);
12388 md5_finish_ctx (&ctx, checksum_after_op0);
12389 ht.empty ();
12391 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12392 fold_check_failed (op0, tem);
12394 md5_init_ctx (&ctx);
12395 fold_checksum_tree (op1, &ctx, &ht);
12396 md5_finish_ctx (&ctx, checksum_after_op1);
12398 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12399 fold_check_failed (op1, tem);
12400 #endif
12401 return tem;
12404 /* Fold a ternary tree expression with code CODE of type TYPE with
12405 operands OP0, OP1, and OP2. Return a folded expression if
12406 successful. Otherwise, return a tree expression with code CODE of
12407 type TYPE with operands OP0, OP1, and OP2. */
12409 tree
12410 fold_build3_loc (location_t loc, enum tree_code code, tree type,
12411 tree op0, tree op1, tree op2 MEM_STAT_DECL)
12413 tree tem;
12414 #ifdef ENABLE_FOLD_CHECKING
12415 unsigned char checksum_before_op0[16],
12416 checksum_before_op1[16],
12417 checksum_before_op2[16],
12418 checksum_after_op0[16],
12419 checksum_after_op1[16],
12420 checksum_after_op2[16];
12421 struct md5_ctx ctx;
12422 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12424 md5_init_ctx (&ctx);
12425 fold_checksum_tree (op0, &ctx, &ht);
12426 md5_finish_ctx (&ctx, checksum_before_op0);
12427 ht.empty ();
12429 md5_init_ctx (&ctx);
12430 fold_checksum_tree (op1, &ctx, &ht);
12431 md5_finish_ctx (&ctx, checksum_before_op1);
12432 ht.empty ();
12434 md5_init_ctx (&ctx);
12435 fold_checksum_tree (op2, &ctx, &ht);
12436 md5_finish_ctx (&ctx, checksum_before_op2);
12437 ht.empty ();
12438 #endif
12440 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
12441 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
12442 if (!tem)
12443 tem = build3_loc (loc, code, type, op0, op1, op2 PASS_MEM_STAT);
12445 #ifdef ENABLE_FOLD_CHECKING
12446 md5_init_ctx (&ctx);
12447 fold_checksum_tree (op0, &ctx, &ht);
12448 md5_finish_ctx (&ctx, checksum_after_op0);
12449 ht.empty ();
12451 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
12452 fold_check_failed (op0, tem);
12454 md5_init_ctx (&ctx);
12455 fold_checksum_tree (op1, &ctx, &ht);
12456 md5_finish_ctx (&ctx, checksum_after_op1);
12457 ht.empty ();
12459 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
12460 fold_check_failed (op1, tem);
12462 md5_init_ctx (&ctx);
12463 fold_checksum_tree (op2, &ctx, &ht);
12464 md5_finish_ctx (&ctx, checksum_after_op2);
12466 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
12467 fold_check_failed (op2, tem);
12468 #endif
12469 return tem;
12472 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
12473 arguments in ARGARRAY, and a null static chain.
12474 Return a folded expression if successful. Otherwise, return a CALL_EXPR
12475 of type TYPE from the given operands as constructed by build_call_array. */
12477 tree
12478 fold_build_call_array_loc (location_t loc, tree type, tree fn,
12479 int nargs, tree *argarray)
12481 tree tem;
12482 #ifdef ENABLE_FOLD_CHECKING
12483 unsigned char checksum_before_fn[16],
12484 checksum_before_arglist[16],
12485 checksum_after_fn[16],
12486 checksum_after_arglist[16];
12487 struct md5_ctx ctx;
12488 hash_table<nofree_ptr_hash<const tree_node> > ht (32);
12489 int i;
12491 md5_init_ctx (&ctx);
12492 fold_checksum_tree (fn, &ctx, &ht);
12493 md5_finish_ctx (&ctx, checksum_before_fn);
12494 ht.empty ();
12496 md5_init_ctx (&ctx);
12497 for (i = 0; i < nargs; i++)
12498 fold_checksum_tree (argarray[i], &ctx, &ht);
12499 md5_finish_ctx (&ctx, checksum_before_arglist);
12500 ht.empty ();
12501 #endif
12503 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
12504 if (!tem)
12505 tem = build_call_array_loc (loc, type, fn, nargs, argarray);
12507 #ifdef ENABLE_FOLD_CHECKING
12508 md5_init_ctx (&ctx);
12509 fold_checksum_tree (fn, &ctx, &ht);
12510 md5_finish_ctx (&ctx, checksum_after_fn);
12511 ht.empty ();
12513 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
12514 fold_check_failed (fn, tem);
12516 md5_init_ctx (&ctx);
12517 for (i = 0; i < nargs; i++)
12518 fold_checksum_tree (argarray[i], &ctx, &ht);
12519 md5_finish_ctx (&ctx, checksum_after_arglist);
12521 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
12522 fold_check_failed (NULL_TREE, tem);
12523 #endif
12524 return tem;
12527 /* Perform constant folding and related simplification of initializer
12528 expression EXPR. These behave identically to "fold_buildN" but ignore
12529 potential run-time traps and exceptions that fold must preserve. */
12531 #define START_FOLD_INIT \
12532 int saved_signaling_nans = flag_signaling_nans;\
12533 int saved_trapping_math = flag_trapping_math;\
12534 int saved_rounding_math = flag_rounding_math;\
12535 int saved_trapv = flag_trapv;\
12536 int saved_folding_initializer = folding_initializer;\
12537 flag_signaling_nans = 0;\
12538 flag_trapping_math = 0;\
12539 flag_rounding_math = 0;\
12540 flag_trapv = 0;\
12541 folding_initializer = 1;
12543 #define END_FOLD_INIT \
12544 flag_signaling_nans = saved_signaling_nans;\
12545 flag_trapping_math = saved_trapping_math;\
12546 flag_rounding_math = saved_rounding_math;\
12547 flag_trapv = saved_trapv;\
12548 folding_initializer = saved_folding_initializer;
12550 tree
12551 fold_build1_initializer_loc (location_t loc, enum tree_code code,
12552 tree type, tree op)
12554 tree result;
12555 START_FOLD_INIT;
12557 result = fold_build1_loc (loc, code, type, op);
12559 END_FOLD_INIT;
12560 return result;
12563 tree
12564 fold_build2_initializer_loc (location_t loc, enum tree_code code,
12565 tree type, tree op0, tree op1)
12567 tree result;
12568 START_FOLD_INIT;
12570 result = fold_build2_loc (loc, code, type, op0, op1);
12572 END_FOLD_INIT;
12573 return result;
12576 tree
12577 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
12578 int nargs, tree *argarray)
12580 tree result;
12581 START_FOLD_INIT;
12583 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
12585 END_FOLD_INIT;
12586 return result;
12589 #undef START_FOLD_INIT
12590 #undef END_FOLD_INIT
12592 /* Determine if first argument is a multiple of second argument. Return 0 if
12593 it is not, or we cannot easily determined it to be.
12595 An example of the sort of thing we care about (at this point; this routine
12596 could surely be made more general, and expanded to do what the *_DIV_EXPR's
12597 fold cases do now) is discovering that
12599 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12601 is a multiple of
12603 SAVE_EXPR (J * 8)
12605 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
12607 This code also handles discovering that
12609 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
12611 is a multiple of 8 so we don't have to worry about dealing with a
12612 possible remainder.
12614 Note that we *look* inside a SAVE_EXPR only to determine how it was
12615 calculated; it is not safe for fold to do much of anything else with the
12616 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
12617 at run time. For example, the latter example above *cannot* be implemented
12618 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
12619 evaluation time of the original SAVE_EXPR is not necessarily the same at
12620 the time the new expression is evaluated. The only optimization of this
12621 sort that would be valid is changing
12623 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
12625 divided by 8 to
12627 SAVE_EXPR (I) * SAVE_EXPR (J)
12629 (where the same SAVE_EXPR (J) is used in the original and the
12630 transformed version). */
12633 multiple_of_p (tree type, const_tree top, const_tree bottom)
12635 gimple *stmt;
12636 tree t1, op1, op2;
12638 if (operand_equal_p (top, bottom, 0))
12639 return 1;
12641 if (TREE_CODE (type) != INTEGER_TYPE)
12642 return 0;
12644 switch (TREE_CODE (top))
12646 case BIT_AND_EXPR:
12647 /* Bitwise and provides a power of two multiple. If the mask is
12648 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
12649 if (!integer_pow2p (bottom))
12650 return 0;
12651 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12652 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12654 case MULT_EXPR:
12655 if (TREE_CODE (bottom) == INTEGER_CST)
12657 op1 = TREE_OPERAND (top, 0);
12658 op2 = TREE_OPERAND (top, 1);
12659 if (TREE_CODE (op1) == INTEGER_CST)
12660 std::swap (op1, op2);
12661 if (TREE_CODE (op2) == INTEGER_CST)
12663 if (multiple_of_p (type, op2, bottom))
12664 return 1;
12665 /* Handle multiple_of_p ((x * 2 + 2) * 4, 8). */
12666 if (multiple_of_p (type, bottom, op2))
12668 widest_int w = wi::sdiv_trunc (wi::to_widest (bottom),
12669 wi::to_widest (op2));
12670 if (wi::fits_to_tree_p (w, TREE_TYPE (bottom)))
12672 op2 = wide_int_to_tree (TREE_TYPE (bottom), w);
12673 return multiple_of_p (type, op1, op2);
12676 return multiple_of_p (type, op1, bottom);
12679 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12680 || multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12682 case MINUS_EXPR:
12683 /* It is impossible to prove if op0 - op1 is multiple of bottom
12684 precisely, so be conservative here checking if both op0 and op1
12685 are multiple of bottom. Note we check the second operand first
12686 since it's usually simpler. */
12687 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12688 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12690 case PLUS_EXPR:
12691 /* The same as MINUS_EXPR, but handle cases like op0 + 0xfffffffd
12692 as op0 - 3 if the expression has unsigned type. For example,
12693 (X / 3) + 0xfffffffd is multiple of 3, but 0xfffffffd is not. */
12694 op1 = TREE_OPERAND (top, 1);
12695 if (TYPE_UNSIGNED (type)
12696 && TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sign_bit (op1))
12697 op1 = fold_build1 (NEGATE_EXPR, type, op1);
12698 return (multiple_of_p (type, op1, bottom)
12699 && multiple_of_p (type, TREE_OPERAND (top, 0), bottom));
12701 case LSHIFT_EXPR:
12702 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
12704 op1 = TREE_OPERAND (top, 1);
12705 /* const_binop may not detect overflow correctly,
12706 so check for it explicitly here. */
12707 if (wi::gtu_p (TYPE_PRECISION (TREE_TYPE (size_one_node)),
12708 wi::to_wide (op1))
12709 && (t1 = fold_convert (type,
12710 const_binop (LSHIFT_EXPR, size_one_node,
12711 op1))) != 0
12712 && !TREE_OVERFLOW (t1))
12713 return multiple_of_p (type, t1, bottom);
12715 return 0;
12717 case NOP_EXPR:
12718 /* Can't handle conversions from non-integral or wider integral type. */
12719 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
12720 || (TYPE_PRECISION (type)
12721 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
12722 return 0;
12724 /* fall through */
12726 case SAVE_EXPR:
12727 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
12729 case COND_EXPR:
12730 return (multiple_of_p (type, TREE_OPERAND (top, 1), bottom)
12731 && multiple_of_p (type, TREE_OPERAND (top, 2), bottom));
12733 case INTEGER_CST:
12734 if (TREE_CODE (bottom) != INTEGER_CST
12735 || integer_zerop (bottom)
12736 || (TYPE_UNSIGNED (type)
12737 && (tree_int_cst_sgn (top) < 0
12738 || tree_int_cst_sgn (bottom) < 0)))
12739 return 0;
12740 return wi::multiple_of_p (wi::to_widest (top), wi::to_widest (bottom),
12741 SIGNED);
12743 case SSA_NAME:
12744 if (TREE_CODE (bottom) == INTEGER_CST
12745 && (stmt = SSA_NAME_DEF_STMT (top)) != NULL
12746 && gimple_code (stmt) == GIMPLE_ASSIGN)
12748 enum tree_code code = gimple_assign_rhs_code (stmt);
12750 /* Check for special cases to see if top is defined as multiple
12751 of bottom:
12753 top = (X & ~(bottom - 1) ; bottom is power of 2
12757 Y = X % bottom
12758 top = X - Y. */
12759 if (code == BIT_AND_EXPR
12760 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12761 && TREE_CODE (op2) == INTEGER_CST
12762 && integer_pow2p (bottom)
12763 && wi::multiple_of_p (wi::to_widest (op2),
12764 wi::to_widest (bottom), UNSIGNED))
12765 return 1;
12767 op1 = gimple_assign_rhs1 (stmt);
12768 if (code == MINUS_EXPR
12769 && (op2 = gimple_assign_rhs2 (stmt)) != NULL_TREE
12770 && TREE_CODE (op2) == SSA_NAME
12771 && (stmt = SSA_NAME_DEF_STMT (op2)) != NULL
12772 && gimple_code (stmt) == GIMPLE_ASSIGN
12773 && (code = gimple_assign_rhs_code (stmt)) == TRUNC_MOD_EXPR
12774 && operand_equal_p (op1, gimple_assign_rhs1 (stmt), 0)
12775 && operand_equal_p (bottom, gimple_assign_rhs2 (stmt), 0))
12776 return 1;
12779 /* fall through */
12781 default:
12782 if (POLY_INT_CST_P (top) && poly_int_tree_p (bottom))
12783 return multiple_p (wi::to_poly_widest (top),
12784 wi::to_poly_widest (bottom));
12786 return 0;
12790 #define tree_expr_nonnegative_warnv_p(X, Y) \
12791 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
12793 #define RECURSE(X) \
12794 ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1))
12796 /* Return true if CODE or TYPE is known to be non-negative. */
12798 static bool
12799 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
12801 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
12802 && truth_value_p (code))
12803 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
12804 have a signed:1 type (where the value is -1 and 0). */
12805 return true;
12806 return false;
12809 /* Return true if (CODE OP0) is known to be non-negative. If the return
12810 value is based on the assumption that signed overflow is undefined,
12811 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12812 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12814 bool
12815 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12816 bool *strict_overflow_p, int depth)
12818 if (TYPE_UNSIGNED (type))
12819 return true;
12821 switch (code)
12823 case ABS_EXPR:
12824 /* We can't return 1 if flag_wrapv is set because
12825 ABS_EXPR<INT_MIN> = INT_MIN. */
12826 if (!ANY_INTEGRAL_TYPE_P (type))
12827 return true;
12828 if (TYPE_OVERFLOW_UNDEFINED (type))
12830 *strict_overflow_p = true;
12831 return true;
12833 break;
12835 case NON_LVALUE_EXPR:
12836 case FLOAT_EXPR:
12837 case FIX_TRUNC_EXPR:
12838 return RECURSE (op0);
12840 CASE_CONVERT:
12842 tree inner_type = TREE_TYPE (op0);
12843 tree outer_type = type;
12845 if (TREE_CODE (outer_type) == REAL_TYPE)
12847 if (TREE_CODE (inner_type) == REAL_TYPE)
12848 return RECURSE (op0);
12849 if (INTEGRAL_TYPE_P (inner_type))
12851 if (TYPE_UNSIGNED (inner_type))
12852 return true;
12853 return RECURSE (op0);
12856 else if (INTEGRAL_TYPE_P (outer_type))
12858 if (TREE_CODE (inner_type) == REAL_TYPE)
12859 return RECURSE (op0);
12860 if (INTEGRAL_TYPE_P (inner_type))
12861 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
12862 && TYPE_UNSIGNED (inner_type);
12865 break;
12867 default:
12868 return tree_simple_nonnegative_warnv_p (code, type);
12871 /* We don't know sign of `t', so be conservative and return false. */
12872 return false;
12875 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
12876 value is based on the assumption that signed overflow is undefined,
12877 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12878 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
12880 bool
12881 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
12882 tree op1, bool *strict_overflow_p,
12883 int depth)
12885 if (TYPE_UNSIGNED (type))
12886 return true;
12888 switch (code)
12890 case POINTER_PLUS_EXPR:
12891 case PLUS_EXPR:
12892 if (FLOAT_TYPE_P (type))
12893 return RECURSE (op0) && RECURSE (op1);
12895 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
12896 both unsigned and at least 2 bits shorter than the result. */
12897 if (TREE_CODE (type) == INTEGER_TYPE
12898 && TREE_CODE (op0) == NOP_EXPR
12899 && TREE_CODE (op1) == NOP_EXPR)
12901 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
12902 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
12903 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
12904 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
12906 unsigned int prec = MAX (TYPE_PRECISION (inner1),
12907 TYPE_PRECISION (inner2)) + 1;
12908 return prec < TYPE_PRECISION (type);
12911 break;
12913 case MULT_EXPR:
12914 if (FLOAT_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12916 /* x * x is always non-negative for floating point x
12917 or without overflow. */
12918 if (operand_equal_p (op0, op1, 0)
12919 || (RECURSE (op0) && RECURSE (op1)))
12921 if (ANY_INTEGRAL_TYPE_P (type)
12922 && TYPE_OVERFLOW_UNDEFINED (type))
12923 *strict_overflow_p = true;
12924 return true;
12928 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
12929 both unsigned and their total bits is shorter than the result. */
12930 if (TREE_CODE (type) == INTEGER_TYPE
12931 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
12932 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
12934 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
12935 ? TREE_TYPE (TREE_OPERAND (op0, 0))
12936 : TREE_TYPE (op0);
12937 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
12938 ? TREE_TYPE (TREE_OPERAND (op1, 0))
12939 : TREE_TYPE (op1);
12941 bool unsigned0 = TYPE_UNSIGNED (inner0);
12942 bool unsigned1 = TYPE_UNSIGNED (inner1);
12944 if (TREE_CODE (op0) == INTEGER_CST)
12945 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
12947 if (TREE_CODE (op1) == INTEGER_CST)
12948 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
12950 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
12951 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
12953 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
12954 ? tree_int_cst_min_precision (op0, UNSIGNED)
12955 : TYPE_PRECISION (inner0);
12957 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
12958 ? tree_int_cst_min_precision (op1, UNSIGNED)
12959 : TYPE_PRECISION (inner1);
12961 return precision0 + precision1 < TYPE_PRECISION (type);
12964 return false;
12966 case BIT_AND_EXPR:
12967 case MAX_EXPR:
12968 return RECURSE (op0) || RECURSE (op1);
12970 case BIT_IOR_EXPR:
12971 case BIT_XOR_EXPR:
12972 case MIN_EXPR:
12973 case RDIV_EXPR:
12974 case TRUNC_DIV_EXPR:
12975 case CEIL_DIV_EXPR:
12976 case FLOOR_DIV_EXPR:
12977 case ROUND_DIV_EXPR:
12978 return RECURSE (op0) && RECURSE (op1);
12980 case TRUNC_MOD_EXPR:
12981 return RECURSE (op0);
12983 case FLOOR_MOD_EXPR:
12984 return RECURSE (op1);
12986 case CEIL_MOD_EXPR:
12987 case ROUND_MOD_EXPR:
12988 default:
12989 return tree_simple_nonnegative_warnv_p (code, type);
12992 /* We don't know sign of `t', so be conservative and return false. */
12993 return false;
12996 /* Return true if T is known to be non-negative. If the return
12997 value is based on the assumption that signed overflow is undefined,
12998 set *STRICT_OVERFLOW_P to true; otherwise, don't change
12999 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13001 bool
13002 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13004 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13005 return true;
13007 switch (TREE_CODE (t))
13009 case INTEGER_CST:
13010 return tree_int_cst_sgn (t) >= 0;
13012 case REAL_CST:
13013 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
13015 case FIXED_CST:
13016 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
13018 case COND_EXPR:
13019 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13021 case SSA_NAME:
13022 /* Limit the depth of recursion to avoid quadratic behavior.
13023 This is expected to catch almost all occurrences in practice.
13024 If this code misses important cases that unbounded recursion
13025 would not, passes that need this information could be revised
13026 to provide it through dataflow propagation. */
13027 return (!name_registered_for_update_p (t)
13028 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13029 && gimple_stmt_nonnegative_warnv_p (SSA_NAME_DEF_STMT (t),
13030 strict_overflow_p, depth));
13032 default:
13033 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13037 /* Return true if T is known to be non-negative. If the return
13038 value is based on the assumption that signed overflow is undefined,
13039 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13040 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13042 bool
13043 tree_call_nonnegative_warnv_p (tree type, combined_fn fn, tree arg0, tree arg1,
13044 bool *strict_overflow_p, int depth)
13046 switch (fn)
13048 CASE_CFN_ACOS:
13049 CASE_CFN_ACOSH:
13050 CASE_CFN_CABS:
13051 CASE_CFN_COSH:
13052 CASE_CFN_ERFC:
13053 CASE_CFN_EXP:
13054 CASE_CFN_EXP10:
13055 CASE_CFN_EXP2:
13056 CASE_CFN_FABS:
13057 CASE_CFN_FDIM:
13058 CASE_CFN_HYPOT:
13059 CASE_CFN_POW10:
13060 CASE_CFN_FFS:
13061 CASE_CFN_PARITY:
13062 CASE_CFN_POPCOUNT:
13063 CASE_CFN_CLZ:
13064 CASE_CFN_CLRSB:
13065 case CFN_BUILT_IN_BSWAP32:
13066 case CFN_BUILT_IN_BSWAP64:
13067 /* Always true. */
13068 return true;
13070 CASE_CFN_SQRT:
13071 CASE_CFN_SQRT_FN:
13072 /* sqrt(-0.0) is -0.0. */
13073 if (!HONOR_SIGNED_ZEROS (element_mode (type)))
13074 return true;
13075 return RECURSE (arg0);
13077 CASE_CFN_ASINH:
13078 CASE_CFN_ATAN:
13079 CASE_CFN_ATANH:
13080 CASE_CFN_CBRT:
13081 CASE_CFN_CEIL:
13082 CASE_CFN_CEIL_FN:
13083 CASE_CFN_ERF:
13084 CASE_CFN_EXPM1:
13085 CASE_CFN_FLOOR:
13086 CASE_CFN_FLOOR_FN:
13087 CASE_CFN_FMOD:
13088 CASE_CFN_FREXP:
13089 CASE_CFN_ICEIL:
13090 CASE_CFN_IFLOOR:
13091 CASE_CFN_IRINT:
13092 CASE_CFN_IROUND:
13093 CASE_CFN_LCEIL:
13094 CASE_CFN_LDEXP:
13095 CASE_CFN_LFLOOR:
13096 CASE_CFN_LLCEIL:
13097 CASE_CFN_LLFLOOR:
13098 CASE_CFN_LLRINT:
13099 CASE_CFN_LLROUND:
13100 CASE_CFN_LRINT:
13101 CASE_CFN_LROUND:
13102 CASE_CFN_MODF:
13103 CASE_CFN_NEARBYINT:
13104 CASE_CFN_NEARBYINT_FN:
13105 CASE_CFN_RINT:
13106 CASE_CFN_RINT_FN:
13107 CASE_CFN_ROUND:
13108 CASE_CFN_ROUND_FN:
13109 CASE_CFN_ROUNDEVEN:
13110 CASE_CFN_ROUNDEVEN_FN:
13111 CASE_CFN_SCALB:
13112 CASE_CFN_SCALBLN:
13113 CASE_CFN_SCALBN:
13114 CASE_CFN_SIGNBIT:
13115 CASE_CFN_SIGNIFICAND:
13116 CASE_CFN_SINH:
13117 CASE_CFN_TANH:
13118 CASE_CFN_TRUNC:
13119 CASE_CFN_TRUNC_FN:
13120 /* True if the 1st argument is nonnegative. */
13121 return RECURSE (arg0);
13123 CASE_CFN_FMAX:
13124 CASE_CFN_FMAX_FN:
13125 /* True if the 1st OR 2nd arguments are nonnegative. */
13126 return RECURSE (arg0) || RECURSE (arg1);
13128 CASE_CFN_FMIN:
13129 CASE_CFN_FMIN_FN:
13130 /* True if the 1st AND 2nd arguments are nonnegative. */
13131 return RECURSE (arg0) && RECURSE (arg1);
13133 CASE_CFN_COPYSIGN:
13134 CASE_CFN_COPYSIGN_FN:
13135 /* True if the 2nd argument is nonnegative. */
13136 return RECURSE (arg1);
13138 CASE_CFN_POWI:
13139 /* True if the 1st argument is nonnegative or the second
13140 argument is an even integer. */
13141 if (TREE_CODE (arg1) == INTEGER_CST
13142 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
13143 return true;
13144 return RECURSE (arg0);
13146 CASE_CFN_POW:
13147 /* True if the 1st argument is nonnegative or the second
13148 argument is an even integer valued real. */
13149 if (TREE_CODE (arg1) == REAL_CST)
13151 REAL_VALUE_TYPE c;
13152 HOST_WIDE_INT n;
13154 c = TREE_REAL_CST (arg1);
13155 n = real_to_integer (&c);
13156 if ((n & 1) == 0)
13158 REAL_VALUE_TYPE cint;
13159 real_from_integer (&cint, VOIDmode, n, SIGNED);
13160 if (real_identical (&c, &cint))
13161 return true;
13164 return RECURSE (arg0);
13166 default:
13167 break;
13169 return tree_simple_nonnegative_warnv_p (CALL_EXPR, type);
13172 /* Return true if T is known to be non-negative. If the return
13173 value is based on the assumption that signed overflow is undefined,
13174 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13175 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13177 static bool
13178 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13180 enum tree_code code = TREE_CODE (t);
13181 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13182 return true;
13184 switch (code)
13186 case TARGET_EXPR:
13188 tree temp = TARGET_EXPR_SLOT (t);
13189 t = TARGET_EXPR_INITIAL (t);
13191 /* If the initializer is non-void, then it's a normal expression
13192 that will be assigned to the slot. */
13193 if (!VOID_TYPE_P (t))
13194 return RECURSE (t);
13196 /* Otherwise, the initializer sets the slot in some way. One common
13197 way is an assignment statement at the end of the initializer. */
13198 while (1)
13200 if (TREE_CODE (t) == BIND_EXPR)
13201 t = expr_last (BIND_EXPR_BODY (t));
13202 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13203 || TREE_CODE (t) == TRY_CATCH_EXPR)
13204 t = expr_last (TREE_OPERAND (t, 0));
13205 else if (TREE_CODE (t) == STATEMENT_LIST)
13206 t = expr_last (t);
13207 else
13208 break;
13210 if (TREE_CODE (t) == MODIFY_EXPR
13211 && TREE_OPERAND (t, 0) == temp)
13212 return RECURSE (TREE_OPERAND (t, 1));
13214 return false;
13217 case CALL_EXPR:
13219 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
13220 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
13222 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
13223 get_call_combined_fn (t),
13224 arg0,
13225 arg1,
13226 strict_overflow_p, depth);
13228 case COMPOUND_EXPR:
13229 case MODIFY_EXPR:
13230 return RECURSE (TREE_OPERAND (t, 1));
13232 case BIND_EXPR:
13233 return RECURSE (expr_last (TREE_OPERAND (t, 1)));
13235 case SAVE_EXPR:
13236 return RECURSE (TREE_OPERAND (t, 0));
13238 default:
13239 return tree_simple_nonnegative_warnv_p (TREE_CODE (t), TREE_TYPE (t));
13243 #undef RECURSE
13244 #undef tree_expr_nonnegative_warnv_p
13246 /* Return true if T is known to be non-negative. If the return
13247 value is based on the assumption that signed overflow is undefined,
13248 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13249 *STRICT_OVERFLOW_P. DEPTH is the current nesting depth of the query. */
13251 bool
13252 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p, int depth)
13254 enum tree_code code;
13255 if (t == error_mark_node)
13256 return false;
13258 code = TREE_CODE (t);
13259 switch (TREE_CODE_CLASS (code))
13261 case tcc_binary:
13262 case tcc_comparison:
13263 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13264 TREE_TYPE (t),
13265 TREE_OPERAND (t, 0),
13266 TREE_OPERAND (t, 1),
13267 strict_overflow_p, depth);
13269 case tcc_unary:
13270 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13271 TREE_TYPE (t),
13272 TREE_OPERAND (t, 0),
13273 strict_overflow_p, depth);
13275 case tcc_constant:
13276 case tcc_declaration:
13277 case tcc_reference:
13278 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13280 default:
13281 break;
13284 switch (code)
13286 case TRUTH_AND_EXPR:
13287 case TRUTH_OR_EXPR:
13288 case TRUTH_XOR_EXPR:
13289 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
13290 TREE_TYPE (t),
13291 TREE_OPERAND (t, 0),
13292 TREE_OPERAND (t, 1),
13293 strict_overflow_p, depth);
13294 case TRUTH_NOT_EXPR:
13295 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
13296 TREE_TYPE (t),
13297 TREE_OPERAND (t, 0),
13298 strict_overflow_p, depth);
13300 case COND_EXPR:
13301 case CONSTRUCTOR:
13302 case OBJ_TYPE_REF:
13303 case ASSERT_EXPR:
13304 case ADDR_EXPR:
13305 case WITH_SIZE_EXPR:
13306 case SSA_NAME:
13307 return tree_single_nonnegative_warnv_p (t, strict_overflow_p, depth);
13309 default:
13310 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p, depth);
13314 /* Return true if `t' is known to be non-negative. Handle warnings
13315 about undefined signed overflow. */
13317 bool
13318 tree_expr_nonnegative_p (tree t)
13320 bool ret, strict_overflow_p;
13322 strict_overflow_p = false;
13323 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
13324 if (strict_overflow_p)
13325 fold_overflow_warning (("assuming signed overflow does not occur when "
13326 "determining that expression is always "
13327 "non-negative"),
13328 WARN_STRICT_OVERFLOW_MISC);
13329 return ret;
13333 /* Return true when (CODE OP0) 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_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
13343 bool *strict_overflow_p)
13345 switch (code)
13347 case ABS_EXPR:
13348 return tree_expr_nonzero_warnv_p (op0,
13349 strict_overflow_p);
13351 case NOP_EXPR:
13353 tree inner_type = TREE_TYPE (op0);
13354 tree outer_type = type;
13356 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
13357 && tree_expr_nonzero_warnv_p (op0,
13358 strict_overflow_p));
13360 break;
13362 case NON_LVALUE_EXPR:
13363 return tree_expr_nonzero_warnv_p (op0,
13364 strict_overflow_p);
13366 default:
13367 break;
13370 return false;
13373 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
13374 For floating point we further ensure that T is not denormal.
13375 Similar logic is present in nonzero_address in rtlanal.h.
13377 If the return value is based on the assumption that signed overflow
13378 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13379 change *STRICT_OVERFLOW_P. */
13381 bool
13382 tree_binary_nonzero_warnv_p (enum tree_code code,
13383 tree type,
13384 tree op0,
13385 tree op1, bool *strict_overflow_p)
13387 bool sub_strict_overflow_p;
13388 switch (code)
13390 case POINTER_PLUS_EXPR:
13391 case PLUS_EXPR:
13392 if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
13394 /* With the presence of negative values it is hard
13395 to say something. */
13396 sub_strict_overflow_p = false;
13397 if (!tree_expr_nonnegative_warnv_p (op0,
13398 &sub_strict_overflow_p)
13399 || !tree_expr_nonnegative_warnv_p (op1,
13400 &sub_strict_overflow_p))
13401 return false;
13402 /* One of operands must be positive and the other non-negative. */
13403 /* We don't set *STRICT_OVERFLOW_P here: even if this value
13404 overflows, on a twos-complement machine the sum of two
13405 nonnegative numbers can never be zero. */
13406 return (tree_expr_nonzero_warnv_p (op0,
13407 strict_overflow_p)
13408 || tree_expr_nonzero_warnv_p (op1,
13409 strict_overflow_p));
13411 break;
13413 case MULT_EXPR:
13414 if (TYPE_OVERFLOW_UNDEFINED (type))
13416 if (tree_expr_nonzero_warnv_p (op0,
13417 strict_overflow_p)
13418 && tree_expr_nonzero_warnv_p (op1,
13419 strict_overflow_p))
13421 *strict_overflow_p = true;
13422 return true;
13425 break;
13427 case MIN_EXPR:
13428 sub_strict_overflow_p = false;
13429 if (tree_expr_nonzero_warnv_p (op0,
13430 &sub_strict_overflow_p)
13431 && tree_expr_nonzero_warnv_p (op1,
13432 &sub_strict_overflow_p))
13434 if (sub_strict_overflow_p)
13435 *strict_overflow_p = true;
13437 break;
13439 case MAX_EXPR:
13440 sub_strict_overflow_p = false;
13441 if (tree_expr_nonzero_warnv_p (op0,
13442 &sub_strict_overflow_p))
13444 if (sub_strict_overflow_p)
13445 *strict_overflow_p = true;
13447 /* When both operands are nonzero, then MAX must be too. */
13448 if (tree_expr_nonzero_warnv_p (op1,
13449 strict_overflow_p))
13450 return true;
13452 /* MAX where operand 0 is positive is positive. */
13453 return tree_expr_nonnegative_warnv_p (op0,
13454 strict_overflow_p);
13456 /* MAX where operand 1 is positive is positive. */
13457 else if (tree_expr_nonzero_warnv_p (op1,
13458 &sub_strict_overflow_p)
13459 && tree_expr_nonnegative_warnv_p (op1,
13460 &sub_strict_overflow_p))
13462 if (sub_strict_overflow_p)
13463 *strict_overflow_p = true;
13464 return true;
13466 break;
13468 case BIT_IOR_EXPR:
13469 return (tree_expr_nonzero_warnv_p (op1,
13470 strict_overflow_p)
13471 || tree_expr_nonzero_warnv_p (op0,
13472 strict_overflow_p));
13474 default:
13475 break;
13478 return false;
13481 /* Return true when T is an address and is known to be nonzero.
13482 For floating point we further ensure that T is not denormal.
13483 Similar logic is present in nonzero_address in rtlanal.h.
13485 If the return value is based on the assumption that signed overflow
13486 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
13487 change *STRICT_OVERFLOW_P. */
13489 bool
13490 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
13492 bool sub_strict_overflow_p;
13493 switch (TREE_CODE (t))
13495 case INTEGER_CST:
13496 return !integer_zerop (t);
13498 case ADDR_EXPR:
13500 tree base = TREE_OPERAND (t, 0);
13502 if (!DECL_P (base))
13503 base = get_base_address (base);
13505 if (base && TREE_CODE (base) == TARGET_EXPR)
13506 base = TARGET_EXPR_SLOT (base);
13508 if (!base)
13509 return false;
13511 /* For objects in symbol table check if we know they are non-zero.
13512 Don't do anything for variables and functions before symtab is built;
13513 it is quite possible that they will be declared weak later. */
13514 int nonzero_addr = maybe_nonzero_address (base);
13515 if (nonzero_addr >= 0)
13516 return nonzero_addr;
13518 /* Constants are never weak. */
13519 if (CONSTANT_CLASS_P (base))
13520 return true;
13522 return false;
13525 case COND_EXPR:
13526 sub_strict_overflow_p = false;
13527 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
13528 &sub_strict_overflow_p)
13529 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
13530 &sub_strict_overflow_p))
13532 if (sub_strict_overflow_p)
13533 *strict_overflow_p = true;
13534 return true;
13536 break;
13538 case SSA_NAME:
13539 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13540 break;
13541 return expr_not_equal_to (t, wi::zero (TYPE_PRECISION (TREE_TYPE (t))));
13543 default:
13544 break;
13546 return false;
13549 #define integer_valued_real_p(X) \
13550 _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0
13552 #define RECURSE(X) \
13553 ((integer_valued_real_p) (X, depth + 1))
13555 /* Return true if the floating point result of (CODE OP0) has an
13556 integer value. We also allow +Inf, -Inf and NaN to be considered
13557 integer values. Return false for signaling NaN.
13559 DEPTH is the current nesting depth of the query. */
13561 bool
13562 integer_valued_real_unary_p (tree_code code, tree op0, int depth)
13564 switch (code)
13566 case FLOAT_EXPR:
13567 return true;
13569 case ABS_EXPR:
13570 return RECURSE (op0);
13572 CASE_CONVERT:
13574 tree type = TREE_TYPE (op0);
13575 if (TREE_CODE (type) == INTEGER_TYPE)
13576 return true;
13577 if (TREE_CODE (type) == REAL_TYPE)
13578 return RECURSE (op0);
13579 break;
13582 default:
13583 break;
13585 return false;
13588 /* Return true if the floating point result of (CODE OP0 OP1) has an
13589 integer value. We also allow +Inf, -Inf and NaN to be considered
13590 integer values. Return false for signaling NaN.
13592 DEPTH is the current nesting depth of the query. */
13594 bool
13595 integer_valued_real_binary_p (tree_code code, tree op0, tree op1, int depth)
13597 switch (code)
13599 case PLUS_EXPR:
13600 case MINUS_EXPR:
13601 case MULT_EXPR:
13602 case MIN_EXPR:
13603 case MAX_EXPR:
13604 return RECURSE (op0) && RECURSE (op1);
13606 default:
13607 break;
13609 return false;
13612 /* Return true if the floating point result of calling FNDECL with arguments
13613 ARG0 and ARG1 has an integer value. We also allow +Inf, -Inf and NaN to be
13614 considered integer values. Return false for signaling NaN. If FNDECL
13615 takes fewer than 2 arguments, the remaining ARGn are null.
13617 DEPTH is the current nesting depth of the query. */
13619 bool
13620 integer_valued_real_call_p (combined_fn fn, tree arg0, tree arg1, int depth)
13622 switch (fn)
13624 CASE_CFN_CEIL:
13625 CASE_CFN_CEIL_FN:
13626 CASE_CFN_FLOOR:
13627 CASE_CFN_FLOOR_FN:
13628 CASE_CFN_NEARBYINT:
13629 CASE_CFN_NEARBYINT_FN:
13630 CASE_CFN_RINT:
13631 CASE_CFN_RINT_FN:
13632 CASE_CFN_ROUND:
13633 CASE_CFN_ROUND_FN:
13634 CASE_CFN_ROUNDEVEN:
13635 CASE_CFN_ROUNDEVEN_FN:
13636 CASE_CFN_TRUNC:
13637 CASE_CFN_TRUNC_FN:
13638 return true;
13640 CASE_CFN_FMIN:
13641 CASE_CFN_FMIN_FN:
13642 CASE_CFN_FMAX:
13643 CASE_CFN_FMAX_FN:
13644 return RECURSE (arg0) && RECURSE (arg1);
13646 default:
13647 break;
13649 return false;
13652 /* Return true if the floating point expression T (a GIMPLE_SINGLE_RHS)
13653 has an integer value. We also allow +Inf, -Inf and NaN to be
13654 considered integer values. Return false for signaling NaN.
13656 DEPTH is the current nesting depth of the query. */
13658 bool
13659 integer_valued_real_single_p (tree t, int depth)
13661 switch (TREE_CODE (t))
13663 case REAL_CST:
13664 return real_isinteger (TREE_REAL_CST_PTR (t), TYPE_MODE (TREE_TYPE (t)));
13666 case COND_EXPR:
13667 return RECURSE (TREE_OPERAND (t, 1)) && RECURSE (TREE_OPERAND (t, 2));
13669 case SSA_NAME:
13670 /* Limit the depth of recursion to avoid quadratic behavior.
13671 This is expected to catch almost all occurrences in practice.
13672 If this code misses important cases that unbounded recursion
13673 would not, passes that need this information could be revised
13674 to provide it through dataflow propagation. */
13675 return (!name_registered_for_update_p (t)
13676 && depth < PARAM_VALUE (PARAM_MAX_SSA_NAME_QUERY_DEPTH)
13677 && gimple_stmt_integer_valued_real_p (SSA_NAME_DEF_STMT (t),
13678 depth));
13680 default:
13681 break;
13683 return false;
13686 /* Return true if the floating point expression T (a GIMPLE_INVALID_RHS)
13687 has an integer value. We also allow +Inf, -Inf and NaN to be
13688 considered integer values. Return false for signaling NaN.
13690 DEPTH is the current nesting depth of the query. */
13692 static bool
13693 integer_valued_real_invalid_p (tree t, int depth)
13695 switch (TREE_CODE (t))
13697 case COMPOUND_EXPR:
13698 case MODIFY_EXPR:
13699 case BIND_EXPR:
13700 return RECURSE (TREE_OPERAND (t, 1));
13702 case SAVE_EXPR:
13703 return RECURSE (TREE_OPERAND (t, 0));
13705 default:
13706 break;
13708 return false;
13711 #undef RECURSE
13712 #undef integer_valued_real_p
13714 /* Return true if the floating point expression T has an integer value.
13715 We also allow +Inf, -Inf and NaN to be considered integer values.
13716 Return false for signaling NaN.
13718 DEPTH is the current nesting depth of the query. */
13720 bool
13721 integer_valued_real_p (tree t, int depth)
13723 if (t == error_mark_node)
13724 return false;
13726 STRIP_ANY_LOCATION_WRAPPER (t);
13728 tree_code code = TREE_CODE (t);
13729 switch (TREE_CODE_CLASS (code))
13731 case tcc_binary:
13732 case tcc_comparison:
13733 return integer_valued_real_binary_p (code, TREE_OPERAND (t, 0),
13734 TREE_OPERAND (t, 1), depth);
13736 case tcc_unary:
13737 return integer_valued_real_unary_p (code, TREE_OPERAND (t, 0), depth);
13739 case tcc_constant:
13740 case tcc_declaration:
13741 case tcc_reference:
13742 return integer_valued_real_single_p (t, depth);
13744 default:
13745 break;
13748 switch (code)
13750 case COND_EXPR:
13751 case SSA_NAME:
13752 return integer_valued_real_single_p (t, depth);
13754 case CALL_EXPR:
13756 tree arg0 = (call_expr_nargs (t) > 0
13757 ? CALL_EXPR_ARG (t, 0)
13758 : NULL_TREE);
13759 tree arg1 = (call_expr_nargs (t) > 1
13760 ? CALL_EXPR_ARG (t, 1)
13761 : NULL_TREE);
13762 return integer_valued_real_call_p (get_call_combined_fn (t),
13763 arg0, arg1, depth);
13766 default:
13767 return integer_valued_real_invalid_p (t, depth);
13771 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
13772 attempt to fold the expression to a constant without modifying TYPE,
13773 OP0 or OP1.
13775 If the expression could be simplified to a constant, then return
13776 the constant. If the expression would not be simplified to a
13777 constant, then return NULL_TREE. */
13779 tree
13780 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
13782 tree tem = fold_binary (code, type, op0, op1);
13783 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13786 /* Given the components of a unary expression CODE, TYPE and OP0,
13787 attempt to fold the expression to a constant without modifying
13788 TYPE or OP0.
13790 If the expression could be simplified to a constant, then return
13791 the constant. If the expression would not be simplified to a
13792 constant, then return NULL_TREE. */
13794 tree
13795 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
13797 tree tem = fold_unary (code, type, op0);
13798 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
13801 /* If EXP represents referencing an element in a constant string
13802 (either via pointer arithmetic or array indexing), return the
13803 tree representing the value accessed, otherwise return NULL. */
13805 tree
13806 fold_read_from_constant_string (tree exp)
13808 if ((TREE_CODE (exp) == INDIRECT_REF
13809 || TREE_CODE (exp) == ARRAY_REF)
13810 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
13812 tree exp1 = TREE_OPERAND (exp, 0);
13813 tree index;
13814 tree string;
13815 location_t loc = EXPR_LOCATION (exp);
13817 if (TREE_CODE (exp) == INDIRECT_REF)
13818 string = string_constant (exp1, &index, NULL, NULL);
13819 else
13821 tree low_bound = array_ref_low_bound (exp);
13822 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
13824 /* Optimize the special-case of a zero lower bound.
13826 We convert the low_bound to sizetype to avoid some problems
13827 with constant folding. (E.g. suppose the lower bound is 1,
13828 and its mode is QI. Without the conversion,l (ARRAY
13829 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
13830 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
13831 if (! integer_zerop (low_bound))
13832 index = size_diffop_loc (loc, index,
13833 fold_convert_loc (loc, sizetype, low_bound));
13835 string = exp1;
13838 scalar_int_mode char_mode;
13839 if (string
13840 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
13841 && TREE_CODE (string) == STRING_CST
13842 && TREE_CODE (index) == INTEGER_CST
13843 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
13844 && is_int_mode (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))),
13845 &char_mode)
13846 && GET_MODE_SIZE (char_mode) == 1)
13847 return build_int_cst_type (TREE_TYPE (exp),
13848 (TREE_STRING_POINTER (string)
13849 [TREE_INT_CST_LOW (index)]));
13851 return NULL;
13854 /* Folds a read from vector element at IDX of vector ARG. */
13856 tree
13857 fold_read_from_vector (tree arg, poly_uint64 idx)
13859 unsigned HOST_WIDE_INT i;
13860 if (known_lt (idx, TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg)))
13861 && known_ge (idx, 0u)
13862 && idx.is_constant (&i))
13864 if (TREE_CODE (arg) == VECTOR_CST)
13865 return VECTOR_CST_ELT (arg, i);
13866 else if (TREE_CODE (arg) == CONSTRUCTOR)
13868 if (i >= CONSTRUCTOR_NELTS (arg))
13869 return build_zero_cst (TREE_TYPE (TREE_TYPE (arg)));
13870 return CONSTRUCTOR_ELT (arg, i)->value;
13873 return NULL_TREE;
13876 /* Return the tree for neg (ARG0) when ARG0 is known to be either
13877 an integer constant, real, or fixed-point constant.
13879 TYPE is the type of the result. */
13881 static tree
13882 fold_negate_const (tree arg0, tree type)
13884 tree t = NULL_TREE;
13886 switch (TREE_CODE (arg0))
13888 case REAL_CST:
13889 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13890 break;
13892 case FIXED_CST:
13894 FIXED_VALUE_TYPE f;
13895 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
13896 &(TREE_FIXED_CST (arg0)), NULL,
13897 TYPE_SATURATING (type));
13898 t = build_fixed (type, f);
13899 /* Propagate overflow flags. */
13900 if (overflow_p | TREE_OVERFLOW (arg0))
13901 TREE_OVERFLOW (t) = 1;
13902 break;
13905 default:
13906 if (poly_int_tree_p (arg0))
13908 wi::overflow_type overflow;
13909 poly_wide_int res = wi::neg (wi::to_poly_wide (arg0), &overflow);
13910 t = force_fit_type (type, res, 1,
13911 (overflow && ! TYPE_UNSIGNED (type))
13912 || TREE_OVERFLOW (arg0));
13913 break;
13916 gcc_unreachable ();
13919 return t;
13922 /* Return the tree for abs (ARG0) when ARG0 is known to be either
13923 an integer constant or real constant.
13925 TYPE is the type of the result. */
13927 tree
13928 fold_abs_const (tree arg0, tree type)
13930 tree t = NULL_TREE;
13932 switch (TREE_CODE (arg0))
13934 case INTEGER_CST:
13936 /* If the value is unsigned or non-negative, then the absolute value
13937 is the same as the ordinary value. */
13938 wide_int val = wi::to_wide (arg0);
13939 wi::overflow_type overflow = wi::OVF_NONE;
13940 if (!wi::neg_p (val, TYPE_SIGN (TREE_TYPE (arg0))))
13943 /* If the value is negative, then the absolute value is
13944 its negation. */
13945 else
13946 val = wi::neg (val, &overflow);
13948 /* Force to the destination type, set TREE_OVERFLOW for signed
13949 TYPE only. */
13950 t = force_fit_type (type, val, 1, overflow | TREE_OVERFLOW (arg0));
13952 break;
13954 case REAL_CST:
13955 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
13956 t = build_real (type, real_value_negate (&TREE_REAL_CST (arg0)));
13957 else
13958 t = arg0;
13959 break;
13961 default:
13962 gcc_unreachable ();
13965 return t;
13968 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
13969 constant. TYPE is the type of the result. */
13971 static tree
13972 fold_not_const (const_tree arg0, tree type)
13974 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
13976 return force_fit_type (type, ~wi::to_wide (arg0), 0, TREE_OVERFLOW (arg0));
13979 /* Given CODE, a relational operator, the target type, TYPE and two
13980 constant operands OP0 and OP1, return the result of the
13981 relational operation. If the result is not a compile time
13982 constant, then return NULL_TREE. */
13984 static tree
13985 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
13987 int result, invert;
13989 /* From here on, the only cases we handle are when the result is
13990 known to be a constant. */
13992 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
13994 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
13995 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
13997 /* Handle the cases where either operand is a NaN. */
13998 if (real_isnan (c0) || real_isnan (c1))
14000 switch (code)
14002 case EQ_EXPR:
14003 case ORDERED_EXPR:
14004 result = 0;
14005 break;
14007 case NE_EXPR:
14008 case UNORDERED_EXPR:
14009 case UNLT_EXPR:
14010 case UNLE_EXPR:
14011 case UNGT_EXPR:
14012 case UNGE_EXPR:
14013 case UNEQ_EXPR:
14014 result = 1;
14015 break;
14017 case LT_EXPR:
14018 case LE_EXPR:
14019 case GT_EXPR:
14020 case GE_EXPR:
14021 case LTGT_EXPR:
14022 if (flag_trapping_math)
14023 return NULL_TREE;
14024 result = 0;
14025 break;
14027 default:
14028 gcc_unreachable ();
14031 return constant_boolean_node (result, type);
14034 return constant_boolean_node (real_compare (code, c0, c1), type);
14037 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
14039 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
14040 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
14041 return constant_boolean_node (fixed_compare (code, c0, c1), type);
14044 /* Handle equality/inequality of complex constants. */
14045 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
14047 tree rcond = fold_relational_const (code, type,
14048 TREE_REALPART (op0),
14049 TREE_REALPART (op1));
14050 tree icond = fold_relational_const (code, type,
14051 TREE_IMAGPART (op0),
14052 TREE_IMAGPART (op1));
14053 if (code == EQ_EXPR)
14054 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
14055 else if (code == NE_EXPR)
14056 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
14057 else
14058 return NULL_TREE;
14061 if (TREE_CODE (op0) == VECTOR_CST && TREE_CODE (op1) == VECTOR_CST)
14063 if (!VECTOR_TYPE_P (type))
14065 /* Have vector comparison with scalar boolean result. */
14066 gcc_assert ((code == EQ_EXPR || code == NE_EXPR)
14067 && known_eq (VECTOR_CST_NELTS (op0),
14068 VECTOR_CST_NELTS (op1)));
14069 unsigned HOST_WIDE_INT nunits;
14070 if (!VECTOR_CST_NELTS (op0).is_constant (&nunits))
14071 return NULL_TREE;
14072 for (unsigned i = 0; i < nunits; i++)
14074 tree elem0 = VECTOR_CST_ELT (op0, i);
14075 tree elem1 = VECTOR_CST_ELT (op1, i);
14076 tree tmp = fold_relational_const (EQ_EXPR, type, elem0, elem1);
14077 if (tmp == NULL_TREE)
14078 return NULL_TREE;
14079 if (integer_zerop (tmp))
14080 return constant_boolean_node (code == NE_EXPR, type);
14082 return constant_boolean_node (code == EQ_EXPR, type);
14084 tree_vector_builder elts;
14085 if (!elts.new_binary_operation (type, op0, op1, false))
14086 return NULL_TREE;
14087 unsigned int count = elts.encoded_nelts ();
14088 for (unsigned i = 0; i < count; i++)
14090 tree elem_type = TREE_TYPE (type);
14091 tree elem0 = VECTOR_CST_ELT (op0, i);
14092 tree elem1 = VECTOR_CST_ELT (op1, i);
14094 tree tem = fold_relational_const (code, elem_type,
14095 elem0, elem1);
14097 if (tem == NULL_TREE)
14098 return NULL_TREE;
14100 elts.quick_push (build_int_cst (elem_type,
14101 integer_zerop (tem) ? 0 : -1));
14104 return elts.build ();
14107 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14109 To compute GT, swap the arguments and do LT.
14110 To compute GE, do LT and invert the result.
14111 To compute LE, swap the arguments, do LT and invert the result.
14112 To compute NE, do EQ and invert the result.
14114 Therefore, the code below must handle only EQ and LT. */
14116 if (code == LE_EXPR || code == GT_EXPR)
14118 std::swap (op0, op1);
14119 code = swap_tree_comparison (code);
14122 /* Note that it is safe to invert for real values here because we
14123 have already handled the one case that it matters. */
14125 invert = 0;
14126 if (code == NE_EXPR || code == GE_EXPR)
14128 invert = 1;
14129 code = invert_tree_comparison (code, false);
14132 /* Compute a result for LT or EQ if args permit;
14133 Otherwise return T. */
14134 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14136 if (code == EQ_EXPR)
14137 result = tree_int_cst_equal (op0, op1);
14138 else
14139 result = tree_int_cst_lt (op0, op1);
14141 else
14142 return NULL_TREE;
14144 if (invert)
14145 result ^= 1;
14146 return constant_boolean_node (result, type);
14149 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14150 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14151 itself. */
14153 tree
14154 fold_build_cleanup_point_expr (tree type, tree expr)
14156 /* If the expression does not have side effects then we don't have to wrap
14157 it with a cleanup point expression. */
14158 if (!TREE_SIDE_EFFECTS (expr))
14159 return expr;
14161 /* If the expression is a return, check to see if the expression inside the
14162 return has no side effects or the right hand side of the modify expression
14163 inside the return. If either don't have side effects set we don't need to
14164 wrap the expression in a cleanup point expression. Note we don't check the
14165 left hand side of the modify because it should always be a return decl. */
14166 if (TREE_CODE (expr) == RETURN_EXPR)
14168 tree op = TREE_OPERAND (expr, 0);
14169 if (!op || !TREE_SIDE_EFFECTS (op))
14170 return expr;
14171 op = TREE_OPERAND (op, 1);
14172 if (!TREE_SIDE_EFFECTS (op))
14173 return expr;
14176 return build1_loc (EXPR_LOCATION (expr), CLEANUP_POINT_EXPR, type, expr);
14179 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14180 of an indirection through OP0, or NULL_TREE if no simplification is
14181 possible. */
14183 tree
14184 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
14186 tree sub = op0;
14187 tree subtype;
14188 poly_uint64 const_op01;
14190 STRIP_NOPS (sub);
14191 subtype = TREE_TYPE (sub);
14192 if (!POINTER_TYPE_P (subtype)
14193 || TYPE_REF_CAN_ALIAS_ALL (TREE_TYPE (op0)))
14194 return NULL_TREE;
14196 if (TREE_CODE (sub) == ADDR_EXPR)
14198 tree op = TREE_OPERAND (sub, 0);
14199 tree optype = TREE_TYPE (op);
14201 /* *&CONST_DECL -> to the value of the const decl. */
14202 if (TREE_CODE (op) == CONST_DECL)
14203 return DECL_INITIAL (op);
14204 /* *&p => p; make sure to handle *&"str"[cst] here. */
14205 if (type == optype)
14207 tree fop = fold_read_from_constant_string (op);
14208 if (fop)
14209 return fop;
14210 else
14211 return op;
14213 /* *(foo *)&fooarray => fooarray[0] */
14214 else if (TREE_CODE (optype) == ARRAY_TYPE
14215 && type == TREE_TYPE (optype)
14216 && (!in_gimple_form
14217 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14219 tree type_domain = TYPE_DOMAIN (optype);
14220 tree min_val = size_zero_node;
14221 if (type_domain && TYPE_MIN_VALUE (type_domain))
14222 min_val = TYPE_MIN_VALUE (type_domain);
14223 if (in_gimple_form
14224 && TREE_CODE (min_val) != INTEGER_CST)
14225 return NULL_TREE;
14226 return build4_loc (loc, ARRAY_REF, type, op, min_val,
14227 NULL_TREE, NULL_TREE);
14229 /* *(foo *)&complexfoo => __real__ complexfoo */
14230 else if (TREE_CODE (optype) == COMPLEX_TYPE
14231 && type == TREE_TYPE (optype))
14232 return fold_build1_loc (loc, REALPART_EXPR, type, op);
14233 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14234 else if (VECTOR_TYPE_P (optype)
14235 && type == TREE_TYPE (optype))
14237 tree part_width = TYPE_SIZE (type);
14238 tree index = bitsize_int (0);
14239 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width,
14240 index);
14244 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14245 && poly_int_tree_p (TREE_OPERAND (sub, 1), &const_op01))
14247 tree op00 = TREE_OPERAND (sub, 0);
14248 tree op01 = TREE_OPERAND (sub, 1);
14250 STRIP_NOPS (op00);
14251 if (TREE_CODE (op00) == ADDR_EXPR)
14253 tree op00type;
14254 op00 = TREE_OPERAND (op00, 0);
14255 op00type = TREE_TYPE (op00);
14257 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
14258 if (VECTOR_TYPE_P (op00type)
14259 && type == TREE_TYPE (op00type)
14260 /* POINTER_PLUS_EXPR second operand is sizetype, unsigned,
14261 but we want to treat offsets with MSB set as negative.
14262 For the code below negative offsets are invalid and
14263 TYPE_SIZE of the element is something unsigned, so
14264 check whether op01 fits into poly_int64, which implies
14265 it is from 0 to INTTYPE_MAXIMUM (HOST_WIDE_INT), and
14266 then just use poly_uint64 because we want to treat the
14267 value as unsigned. */
14268 && tree_fits_poly_int64_p (op01))
14270 tree part_width = TYPE_SIZE (type);
14271 poly_uint64 max_offset
14272 = (tree_to_uhwi (part_width) / BITS_PER_UNIT
14273 * TYPE_VECTOR_SUBPARTS (op00type));
14274 if (known_lt (const_op01, max_offset))
14276 tree index = bitsize_int (const_op01 * BITS_PER_UNIT);
14277 return fold_build3_loc (loc,
14278 BIT_FIELD_REF, type, op00,
14279 part_width, index);
14282 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14283 else if (TREE_CODE (op00type) == COMPLEX_TYPE
14284 && type == TREE_TYPE (op00type))
14286 if (known_eq (wi::to_poly_offset (TYPE_SIZE_UNIT (type)),
14287 const_op01))
14288 return fold_build1_loc (loc, IMAGPART_EXPR, type, op00);
14290 /* ((foo *)&fooarray)[1] => fooarray[1] */
14291 else if (TREE_CODE (op00type) == ARRAY_TYPE
14292 && type == TREE_TYPE (op00type))
14294 tree type_domain = TYPE_DOMAIN (op00type);
14295 tree min_val = size_zero_node;
14296 if (type_domain && TYPE_MIN_VALUE (type_domain))
14297 min_val = TYPE_MIN_VALUE (type_domain);
14298 poly_uint64 type_size, index;
14299 if (poly_int_tree_p (min_val)
14300 && poly_int_tree_p (TYPE_SIZE_UNIT (type), &type_size)
14301 && multiple_p (const_op01, type_size, &index))
14303 poly_offset_int off = index + wi::to_poly_offset (min_val);
14304 op01 = wide_int_to_tree (sizetype, off);
14305 return build4_loc (loc, ARRAY_REF, type, op00, op01,
14306 NULL_TREE, NULL_TREE);
14312 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14313 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14314 && type == TREE_TYPE (TREE_TYPE (subtype))
14315 && (!in_gimple_form
14316 || TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST))
14318 tree type_domain;
14319 tree min_val = size_zero_node;
14320 sub = build_fold_indirect_ref_loc (loc, sub);
14321 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14322 if (type_domain && TYPE_MIN_VALUE (type_domain))
14323 min_val = TYPE_MIN_VALUE (type_domain);
14324 if (in_gimple_form
14325 && TREE_CODE (min_val) != INTEGER_CST)
14326 return NULL_TREE;
14327 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
14328 NULL_TREE);
14331 return NULL_TREE;
14334 /* Builds an expression for an indirection through T, simplifying some
14335 cases. */
14337 tree
14338 build_fold_indirect_ref_loc (location_t loc, tree t)
14340 tree type = TREE_TYPE (TREE_TYPE (t));
14341 tree sub = fold_indirect_ref_1 (loc, type, t);
14343 if (sub)
14344 return sub;
14346 return build1_loc (loc, INDIRECT_REF, type, t);
14349 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14351 tree
14352 fold_indirect_ref_loc (location_t loc, tree t)
14354 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
14356 if (sub)
14357 return sub;
14358 else
14359 return t;
14362 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14363 whose result is ignored. The type of the returned tree need not be
14364 the same as the original expression. */
14366 tree
14367 fold_ignored_result (tree t)
14369 if (!TREE_SIDE_EFFECTS (t))
14370 return integer_zero_node;
14372 for (;;)
14373 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14375 case tcc_unary:
14376 t = TREE_OPERAND (t, 0);
14377 break;
14379 case tcc_binary:
14380 case tcc_comparison:
14381 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14382 t = TREE_OPERAND (t, 0);
14383 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14384 t = TREE_OPERAND (t, 1);
14385 else
14386 return t;
14387 break;
14389 case tcc_expression:
14390 switch (TREE_CODE (t))
14392 case COMPOUND_EXPR:
14393 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14394 return t;
14395 t = TREE_OPERAND (t, 0);
14396 break;
14398 case COND_EXPR:
14399 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14400 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14401 return t;
14402 t = TREE_OPERAND (t, 0);
14403 break;
14405 default:
14406 return t;
14408 break;
14410 default:
14411 return t;
14415 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
14417 tree
14418 round_up_loc (location_t loc, tree value, unsigned int divisor)
14420 tree div = NULL_TREE;
14422 if (divisor == 1)
14423 return value;
14425 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14426 have to do anything. Only do this when we are not given a const,
14427 because in that case, this check is more expensive than just
14428 doing it. */
14429 if (TREE_CODE (value) != INTEGER_CST)
14431 div = build_int_cst (TREE_TYPE (value), divisor);
14433 if (multiple_of_p (TREE_TYPE (value), value, div))
14434 return value;
14437 /* If divisor is a power of two, simplify this to bit manipulation. */
14438 if (pow2_or_zerop (divisor))
14440 if (TREE_CODE (value) == INTEGER_CST)
14442 wide_int val = wi::to_wide (value);
14443 bool overflow_p;
14445 if ((val & (divisor - 1)) == 0)
14446 return value;
14448 overflow_p = TREE_OVERFLOW (value);
14449 val += divisor - 1;
14450 val &= (int) -divisor;
14451 if (val == 0)
14452 overflow_p = true;
14454 return force_fit_type (TREE_TYPE (value), val, -1, overflow_p);
14456 else
14458 tree t;
14460 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14461 value = size_binop_loc (loc, PLUS_EXPR, value, t);
14462 t = build_int_cst (TREE_TYPE (value), - (int) divisor);
14463 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14466 else
14468 if (!div)
14469 div = build_int_cst (TREE_TYPE (value), divisor);
14470 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
14471 value = size_binop_loc (loc, MULT_EXPR, value, div);
14474 return value;
14477 /* Likewise, but round down. */
14479 tree
14480 round_down_loc (location_t loc, tree value, int divisor)
14482 tree div = NULL_TREE;
14484 gcc_assert (divisor > 0);
14485 if (divisor == 1)
14486 return value;
14488 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14489 have to do anything. Only do this when we are not given a const,
14490 because in that case, this check is more expensive than just
14491 doing it. */
14492 if (TREE_CODE (value) != INTEGER_CST)
14494 div = build_int_cst (TREE_TYPE (value), divisor);
14496 if (multiple_of_p (TREE_TYPE (value), value, div))
14497 return value;
14500 /* If divisor is a power of two, simplify this to bit manipulation. */
14501 if (pow2_or_zerop (divisor))
14503 tree t;
14505 t = build_int_cst (TREE_TYPE (value), -divisor);
14506 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
14508 else
14510 if (!div)
14511 div = build_int_cst (TREE_TYPE (value), divisor);
14512 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
14513 value = size_binop_loc (loc, MULT_EXPR, value, div);
14516 return value;
14519 /* Returns the pointer to the base of the object addressed by EXP and
14520 extracts the information about the offset of the access, storing it
14521 to PBITPOS and POFFSET. */
14523 static tree
14524 split_address_to_core_and_offset (tree exp,
14525 poly_int64_pod *pbitpos, tree *poffset)
14527 tree core;
14528 machine_mode mode;
14529 int unsignedp, reversep, volatilep;
14530 poly_int64 bitsize;
14531 location_t loc = EXPR_LOCATION (exp);
14533 if (TREE_CODE (exp) == ADDR_EXPR)
14535 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14536 poffset, &mode, &unsignedp, &reversep,
14537 &volatilep);
14538 core = build_fold_addr_expr_loc (loc, core);
14540 else if (TREE_CODE (exp) == POINTER_PLUS_EXPR)
14542 core = TREE_OPERAND (exp, 0);
14543 STRIP_NOPS (core);
14544 *pbitpos = 0;
14545 *poffset = TREE_OPERAND (exp, 1);
14546 if (poly_int_tree_p (*poffset))
14548 poly_offset_int tem
14549 = wi::sext (wi::to_poly_offset (*poffset),
14550 TYPE_PRECISION (TREE_TYPE (*poffset)));
14551 tem <<= LOG2_BITS_PER_UNIT;
14552 if (tem.to_shwi (pbitpos))
14553 *poffset = NULL_TREE;
14556 else
14558 core = exp;
14559 *pbitpos = 0;
14560 *poffset = NULL_TREE;
14563 return core;
14566 /* Returns true if addresses of E1 and E2 differ by a constant, false
14567 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14569 bool
14570 ptr_difference_const (tree e1, tree e2, poly_int64_pod *diff)
14572 tree core1, core2;
14573 poly_int64 bitpos1, bitpos2;
14574 tree toffset1, toffset2, tdiff, type;
14576 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14577 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14579 poly_int64 bytepos1, bytepos2;
14580 if (!multiple_p (bitpos1, BITS_PER_UNIT, &bytepos1)
14581 || !multiple_p (bitpos2, BITS_PER_UNIT, &bytepos2)
14582 || !operand_equal_p (core1, core2, 0))
14583 return false;
14585 if (toffset1 && toffset2)
14587 type = TREE_TYPE (toffset1);
14588 if (type != TREE_TYPE (toffset2))
14589 toffset2 = fold_convert (type, toffset2);
14591 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
14592 if (!cst_and_fits_in_hwi (tdiff))
14593 return false;
14595 *diff = int_cst_value (tdiff);
14597 else if (toffset1 || toffset2)
14599 /* If only one of the offsets is non-constant, the difference cannot
14600 be a constant. */
14601 return false;
14603 else
14604 *diff = 0;
14606 *diff += bytepos1 - bytepos2;
14607 return true;
14610 /* Return OFF converted to a pointer offset type suitable as offset for
14611 POINTER_PLUS_EXPR. Use location LOC for this conversion. */
14612 tree
14613 convert_to_ptrofftype_loc (location_t loc, tree off)
14615 return fold_convert_loc (loc, sizetype, off);
14618 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14619 tree
14620 fold_build_pointer_plus_loc (location_t loc, tree ptr, tree off)
14622 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14623 ptr, convert_to_ptrofftype_loc (loc, off));
14626 /* Build and fold a POINTER_PLUS_EXPR at LOC offsetting PTR by OFF. */
14627 tree
14628 fold_build_pointer_plus_hwi_loc (location_t loc, tree ptr, HOST_WIDE_INT off)
14630 return fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (ptr),
14631 ptr, size_int (off));
14634 /* Return a pointer P to a NUL-terminated string representing the sequence
14635 of constant characters referred to by SRC (or a subsequence of such
14636 characters within it if SRC is a reference to a string plus some
14637 constant offset). If STRLEN is non-null, store the number of bytes
14638 in the string constant including the terminating NUL char. *STRLEN is
14639 typically strlen(P) + 1 in the absence of embedded NUL characters. */
14641 const char *
14642 c_getstr (tree src, unsigned HOST_WIDE_INT *strlen /* = NULL */)
14644 tree offset_node;
14645 tree mem_size;
14647 if (strlen)
14648 *strlen = 0;
14650 src = string_constant (src, &offset_node, &mem_size, NULL);
14651 if (src == 0)
14652 return NULL;
14654 unsigned HOST_WIDE_INT offset = 0;
14655 if (offset_node != NULL_TREE)
14657 if (!tree_fits_uhwi_p (offset_node))
14658 return NULL;
14659 else
14660 offset = tree_to_uhwi (offset_node);
14663 if (!tree_fits_uhwi_p (mem_size))
14664 return NULL;
14666 /* STRING_LENGTH is the size of the string literal, including any
14667 embedded NULs. STRING_SIZE is the size of the array the string
14668 literal is stored in. */
14669 unsigned HOST_WIDE_INT string_length = TREE_STRING_LENGTH (src);
14670 unsigned HOST_WIDE_INT string_size = tree_to_uhwi (mem_size);
14672 /* Ideally this would turn into a gcc_checking_assert over time. */
14673 if (string_length > string_size)
14674 string_length = string_size;
14676 const char *string = TREE_STRING_POINTER (src);
14678 /* Ideally this would turn into a gcc_checking_assert over time. */
14679 if (string_length > string_size)
14680 string_length = string_size;
14682 if (string_length == 0
14683 || offset >= string_size)
14684 return NULL;
14686 if (strlen)
14688 /* Compute and store the length of the substring at OFFSET.
14689 All offsets past the initial length refer to null strings. */
14690 if (offset < string_length)
14691 *strlen = string_length - offset;
14692 else
14693 *strlen = 1;
14695 else
14697 tree eltype = TREE_TYPE (TREE_TYPE (src));
14698 /* Support only properly NUL-terminated single byte strings. */
14699 if (tree_to_uhwi (TYPE_SIZE_UNIT (eltype)) != 1)
14700 return NULL;
14701 if (string[string_length - 1] != '\0')
14702 return NULL;
14705 return offset < string_length ? string + offset : "";
14708 /* Given a tree T, compute which bits in T may be nonzero. */
14710 wide_int
14711 tree_nonzero_bits (const_tree t)
14713 switch (TREE_CODE (t))
14715 case INTEGER_CST:
14716 return wi::to_wide (t);
14717 case SSA_NAME:
14718 return get_nonzero_bits (t);
14719 case NON_LVALUE_EXPR:
14720 case SAVE_EXPR:
14721 return tree_nonzero_bits (TREE_OPERAND (t, 0));
14722 case BIT_AND_EXPR:
14723 return wi::bit_and (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14724 tree_nonzero_bits (TREE_OPERAND (t, 1)));
14725 case BIT_IOR_EXPR:
14726 case BIT_XOR_EXPR:
14727 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14728 tree_nonzero_bits (TREE_OPERAND (t, 1)));
14729 case COND_EXPR:
14730 return wi::bit_or (tree_nonzero_bits (TREE_OPERAND (t, 1)),
14731 tree_nonzero_bits (TREE_OPERAND (t, 2)));
14732 CASE_CONVERT:
14733 return wide_int::from (tree_nonzero_bits (TREE_OPERAND (t, 0)),
14734 TYPE_PRECISION (TREE_TYPE (t)),
14735 TYPE_SIGN (TREE_TYPE (TREE_OPERAND (t, 0))));
14736 case PLUS_EXPR:
14737 if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
14739 wide_int nzbits1 = tree_nonzero_bits (TREE_OPERAND (t, 0));
14740 wide_int nzbits2 = tree_nonzero_bits (TREE_OPERAND (t, 1));
14741 if (wi::bit_and (nzbits1, nzbits2) == 0)
14742 return wi::bit_or (nzbits1, nzbits2);
14744 break;
14745 case LSHIFT_EXPR:
14746 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
14748 tree type = TREE_TYPE (t);
14749 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
14750 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
14751 TYPE_PRECISION (type));
14752 return wi::neg_p (arg1)
14753 ? wi::rshift (nzbits, -arg1, TYPE_SIGN (type))
14754 : wi::lshift (nzbits, arg1);
14756 break;
14757 case RSHIFT_EXPR:
14758 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
14760 tree type = TREE_TYPE (t);
14761 wide_int nzbits = tree_nonzero_bits (TREE_OPERAND (t, 0));
14762 wide_int arg1 = wi::to_wide (TREE_OPERAND (t, 1),
14763 TYPE_PRECISION (type));
14764 return wi::neg_p (arg1)
14765 ? wi::lshift (nzbits, -arg1)
14766 : wi::rshift (nzbits, arg1, TYPE_SIGN (type));
14768 break;
14769 default:
14770 break;
14773 return wi::shwi (-1, TYPE_PRECISION (TREE_TYPE (t)));
14776 #if CHECKING_P
14778 namespace selftest {
14780 /* Helper functions for writing tests of folding trees. */
14782 /* Verify that the binary op (LHS CODE RHS) folds to CONSTANT. */
14784 static void
14785 assert_binop_folds_to_const (tree lhs, enum tree_code code, tree rhs,
14786 tree constant)
14788 ASSERT_EQ (constant, fold_build2 (code, TREE_TYPE (lhs), lhs, rhs));
14791 /* Verify that the binary op (LHS CODE RHS) folds to an NON_LVALUE_EXPR
14792 wrapping WRAPPED_EXPR. */
14794 static void
14795 assert_binop_folds_to_nonlvalue (tree lhs, enum tree_code code, tree rhs,
14796 tree wrapped_expr)
14798 tree result = fold_build2 (code, TREE_TYPE (lhs), lhs, rhs);
14799 ASSERT_NE (wrapped_expr, result);
14800 ASSERT_EQ (NON_LVALUE_EXPR, TREE_CODE (result));
14801 ASSERT_EQ (wrapped_expr, TREE_OPERAND (result, 0));
14804 /* Verify that various arithmetic binary operations are folded
14805 correctly. */
14807 static void
14808 test_arithmetic_folding ()
14810 tree type = integer_type_node;
14811 tree x = create_tmp_var_raw (type, "x");
14812 tree zero = build_zero_cst (type);
14813 tree one = build_int_cst (type, 1);
14815 /* Addition. */
14816 /* 1 <-- (0 + 1) */
14817 assert_binop_folds_to_const (zero, PLUS_EXPR, one,
14818 one);
14819 assert_binop_folds_to_const (one, PLUS_EXPR, zero,
14820 one);
14822 /* (nonlvalue)x <-- (x + 0) */
14823 assert_binop_folds_to_nonlvalue (x, PLUS_EXPR, zero,
14826 /* Subtraction. */
14827 /* 0 <-- (x - x) */
14828 assert_binop_folds_to_const (x, MINUS_EXPR, x,
14829 zero);
14830 assert_binop_folds_to_nonlvalue (x, MINUS_EXPR, zero,
14833 /* Multiplication. */
14834 /* 0 <-- (x * 0) */
14835 assert_binop_folds_to_const (x, MULT_EXPR, zero,
14836 zero);
14838 /* (nonlvalue)x <-- (x * 1) */
14839 assert_binop_folds_to_nonlvalue (x, MULT_EXPR, one,
14843 /* Verify that various binary operations on vectors are folded
14844 correctly. */
14846 static void
14847 test_vector_folding ()
14849 tree inner_type = integer_type_node;
14850 tree type = build_vector_type (inner_type, 4);
14851 tree zero = build_zero_cst (type);
14852 tree one = build_one_cst (type);
14853 tree index = build_index_vector (type, 0, 1);
14855 /* Verify equality tests that return a scalar boolean result. */
14856 tree res_type = boolean_type_node;
14857 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, one)));
14858 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type, zero, zero)));
14859 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, zero, one)));
14860 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one)));
14861 ASSERT_TRUE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, index, one)));
14862 ASSERT_FALSE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
14863 index, one)));
14864 ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type,
14865 index, index)));
14866 ASSERT_TRUE (integer_nonzerop (fold_build2 (EQ_EXPR, res_type,
14867 index, index)));
14870 /* Verify folding of VEC_DUPLICATE_EXPRs. */
14872 static void
14873 test_vec_duplicate_folding ()
14875 scalar_int_mode int_mode = SCALAR_INT_TYPE_MODE (ssizetype);
14876 machine_mode vec_mode = targetm.vectorize.preferred_simd_mode (int_mode);
14877 /* This will be 1 if VEC_MODE isn't a vector mode. */
14878 poly_uint64 nunits = GET_MODE_NUNITS (vec_mode);
14880 tree type = build_vector_type (ssizetype, nunits);
14881 tree dup5_expr = fold_unary (VEC_DUPLICATE_EXPR, type, ssize_int (5));
14882 tree dup5_cst = build_vector_from_val (type, ssize_int (5));
14883 ASSERT_TRUE (operand_equal_p (dup5_expr, dup5_cst, 0));
14886 /* Run all of the selftests within this file. */
14888 void
14889 fold_const_c_tests ()
14891 test_arithmetic_folding ();
14892 test_vector_folding ();
14893 test_vec_duplicate_folding ();
14896 } // namespace selftest
14898 #endif /* CHECKING_P */